Implement rule priority ordering and conflict resolution
epic-scenario-based-follow-up-prompts-core-logic-task-006 — Extend ScenarioRuleEngine to handle multiple matching rules by applying a priority ordering strategy: explicit priority field on ScenarioRule, then specificity scoring (more conditions = higher specificity), then insertion order as tiebreaker. Return only the highest-priority matching rule per evaluation. Add getPrioritizedMatch() helper to the engine.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
Implement getPrioritizedMatch(List
Update the evaluate() method from task-005 to collect all matching rules into a List
Testing Requirements
Write unit tests using flutter_test. Tests must cover: (1) single rule returns immediately, (2) two rules with different explicit priorities, (3) two rules with equal priority but different specificity scores, (4) two rules with equal priority and equal specificity but different insertion orders, (5) all three tiebreakers needed in sequence, (6) empty list returns null, (7) determinism test — shuffle input and assert same winner across 10 random orderings, (8) specificity score calculation for all 16 permutations of the 4 binary condition dimensions. Verify that evaluate() in task-005 correctly pipes all matching rules through getPrioritizedMatch and that EvaluationResult.candidateRules is populated correctly.
The Rule Engine must support a flexible JSON rule schema that can express compound conditions (e.g., contact_type AND wellbeing_flag AND delay_days). Underestimating schema expressiveness may require breaking changes to the rule format after coordinators have already configured rules.
Mitigation & Contingency
Mitigation: Define and freeze the rule JSON schema (trigger_type enum, metadata_conditions structure, delay logic) before any implementation begins; validate schema against all known HLF scenarios documented in the feature spec.
Contingency: If schema changes are needed after deployment, implement a schema version field and a migration utility that upgrades stored rules to the new format without coordinator intervention.
Deep-link navigation to the activity wizard with pre-filled arguments may fail if the user's session has expired or if the wizard route is not yet mounted in the navigator stack, causing unhandled navigation exceptions.
Mitigation & Contingency
Mitigation: Implement session state check before navigation; if session is expired, redirect to biometric/login screen and store the pending deep-link URI for post-auth redirect using go_router's redirect mechanism.
Contingency: If post-auth redirect proves unreliable, fall back to navigating to the home screen with a visible action banner that re-triggers the wizard with pre-filled arguments.