Add a Rule health page for duplicate, conflicting and shadowed rules
Part of jaysbeekay/sure#702 (the overlaps slice, jaysbeekay/sure#830, closes it)
Upstream issue: we-promise/sure#5175
Upstream PR: we-promise/sure#5526 (same branch, same commit), in native stack we-promise/sure#5529
Stacked on jaysbeekay/sure#795 (up/rule-runner-claims, the Claims extraction the plan puts first); the diff below is this branch's own commits. The overlaps section is split out to jaysbeekay/sure#830 (up/rule-health-overlaps), stacked on this branch (owner decision, 2026-11-17); jaysbeekay/sure#703's save-time warning, jaysbeekay/sure#797, now stacks on #830.
Summary
Nothing in the app compared one rule with another, so duplicates and contradictions piled up, and a rule that never takes effect looked as live as any other. Settings > Rules now links a read-only Rule health page. It loads its report in a lazy Turbo frame, so the rules page itself is unaffected, and lists, each rule with its run position and an edit link:
- Duplicates: same conditions, effective date, actions and stop-processing setting.
- Conflicts: same conditions setting a field to different values, with the active rule that wins and why (run order, or it stops the rules below). With none active: "none runs on sync".
- Shadowed: active rules that match transactions but change nothing, because every match is stopped above or every field they set is already claimed above, naming those rules.
- Match nothing today, and Couldn't be checked (a rule whose match query failed, e.g. a pattern that timed out).
What changed
- New
app/models/rule/health.rb(Rule::Health.new(family)):- Condition signature, normalised as
Rule::ConditionFiltermatches: whitespace collapsed for text types; case folded only forlike,not_like,starts_with,ends_with(ILIKE);=and!=keep case; amatches_regexvalue compared exactly as written; an additional-detail condition keeps the key before its first=as written (it is looked up exactly) and normalises only the value. Order doesn't count; an AND group reads as its conditions listed flat. - Dry run over the active rules in order, recording claims and stops with
Rule::Runner::Claims(from #795), so it cannot drift from the real run. Each action is judged asRule#applydoes: it skips transactions claimed above for any field it sets. Tags claim nothing; an action that can't be resolved counts as an effect (the real run fails such a rule rather than changing nothing). - Held verdicts. Matches are read before any rule runs, but the real run re-reads them after the rules above apply. A rule whose conditions read a field an active rule above may change (name, merchant, category, transfer kind, tags) gets no shadowed or match-nothing verdict; the doubt passes to rules reading a field it sets, and no rule is reported shadowed by it.
- Each rule's matches are read in their own savepoint; any error marks that rule failed and the report carries on.
- Condition signature, normalised as
RulesController#health+GET /rules/health: the full page renders the shell; a Turbo-Frame request renders the report. Added torequire_non_guest!'sexceptlist, likeindex, since it writes nothing.rules/health.html.erb,rules/_health_report.html.erb(oneDS::Sectionper group),RulesHelper#rule_health_reference(edit link for members, plain text for guests) and#rule_health_attributes, a "Rule health"DS::Linkonrules/index, andconfig/locales/views/rules/en.yml.
Size: 365 non-test lines added (health.rb 240), after moving the overlaps section (97 lines) to #830. That is over the ~300 guide, and it stays one PR on purpose: the held-verdict fix (35 lines) has to sit with the shadowed and match-nothing verdicts it guards, and moving conflicts or the dry run into another slice would split one model across PRs with no reviewable seam.
Deliberately left out
- Bulk delete or merge, persisted results, a background job, and the save-time warning (jaysbeekay/sure#703).
- Attribute locks: the dry run ignores them, as
ApplyAllRulesJobdoes. A rule whose transactions are all locked can read as live here. - The overlaps section: jaysbeekay/sure#830.
- A split action above changes which transactions every rule below sees (the split parent leaves the scope); the dry run does not model it.
- A conflict winner ignores an unrelated stop rule higher up that stops both rules; the page then still names the first active one.
Blast radius: who else hits this line, and what do they pass?
Rule::Runneris untouched in this commit;Rule::Healthonly readsRule#matching_scope,Rule::Action#claimed_attributes/#reserves_claimed_attributes?andRule::Runner::Claims, and writes nothing.require_non_guest!now exceptshealthas well asindex; every other action still requires a member. The report readsCurrent.family.rulesonly.rules/index's page actions gain one link, shown when the family has rules, guests included (the page is read-only).- Siblings:
Rule#active_rules_aboveandRule#affected_resource_countalso read match sets; left alone.
Tests
test/models/rule/health_test.rb (new, 22 tests, each building its own rules and transactions) and test/controllers/rules_controller_test.rb (4 new). Before = parent commit (#795): every model test fails with NameError: uninitialized constant Rule::Health, every controller test with undefined method 'health_rules_url'.
| Test | Kind |
|---|---|
like "woolworths" vs like " WOOLWORTHS " are duplicates | positive |
| an AND group reads like the same conditions flat | positive |
| different effective dates are not duplicates | negative |
= "Uber" vs = "uber" are not duplicates | negative |
matches_regex \d+ vs \D+, and ^Wool worths vs ^Wool worths, are not duplicates | negative |
| same conditions, Groceries vs Dining: higher active wins; higher made inactive, lower wins | positive + boundary |
below a STOP rule matching the same transactions: shadowed (:stopped, by the stop rule); STOP switched off: not shadowed | delta |
field claimed above on every match: shadowed (:claimed, by that rule) | positive |
| tags-only rule below: not shadowed | negative |
| match nothing today | positive |
| regex timeout: reported failed, rest of report returns | positive |
| a query that raises in PostgreSQL (merchant id "not-a-uuid"): failed, rules after it still checked | savepoint |
| an unresolvable action: not shadowed | negative |
| stop rule vs same rule without stop: not duplicates (review F2; before: grouped) | negative |
additional detail like MCC=ABC vs MCC=abc conflict, mcc=abc does not (review F3; before: all three grouped) | positive + negative |
| rename above, then a rule matched by name: neither it nor the rule it claimed for is shadowed, the renamed-to rule is not "match nothing"; rename switched off: both verdicts return (review F1; before: shadowed listed) | delta |
| a rule that may match more after a rename (hardware renamed into its text) is not shadowed; rename off: shadowed (review F1) | delta |
| doubt passes down: renamed-to rule sets a category, a category-condition rule sets a tag, a tag-condition rule; none "match nothing"; rename off: all three (review F1) | delta |
| a rule above that matches nothing leaves the verdict below standing | negative |
cross-check after a rename: every shadowed and match-nothing verdict agrees with a real Rule::Runner run | equivalence |
| claimed rule counts only matches no stop rule above took (review; before: 2, after: 1) | delta |
| controller: report renders the conflicts and shadowed sections | controller |
cross-check: shadowed set equals the rules a real Rule::Runner run records with queued > 0 and modified 0 (2 rules) | equivalence |
| health page renders a lazy frame | controller |
| report lists the family's duplicates, never another family's rule id | tenancy |
| a guest reads page and report, without edit links | guest |
Mutations (applied to the container copy only)
| Mutation | Caught by |
|---|---|
| fold case for every operator | = keeps case |
| never fold case | contains duplicates |
| normalise a pattern like text | pattern compared as written |
| don't flatten an AND group | AND group |
| ignore the effective date | different effective dates |
winner ignores active | conflict winner |
| claims not consulted | cross-check (+ claimed shadow errors) |
| stops not consulted | stop shadow |
| claim-free actions count as no effect | tags-only + cross-check |
| a match failure aborts | timeout test |
| savepoint removed | PostgreSQL-error test |
| unresolvable action counts as no effect | unresolvable action |
| claims recorded on nothing | cross-check |
| every family's rules | other family's rule id |
| guest gets edit links | guest test |
| guest blocked | guest test |
| duplicates key without stop_processing | stop vs no-stop |
| additional detail key folded, or the detail branch removed, or its value not folded | additional detail conflict |
| a rule's own doubt ignored | may match more after a rename |
| a shadow by a doubtful rule kept | rename, and the real-run cross-check |
| match nothing ignores doubt | rename, doubt passes down, real-run cross-check |
| doubt not passed down from a rule with no matches today | doubt passes down |
| every rule above counts as changing its fields | rule above that matches nothing |
| tags not counted as a changed field | doubt passes down |
Screenshots
Before (up/rule-runner-claims): rules list without a link, light and dark. After: the rules list with the "Rule health" link, and the Rule health page, light, dark and mobile.
| Before | After | |
|---|---|---|
| Rules list, light | ![]() | ![]() |
| Rules list, dark | ![]() | ![]() |
| Rule health, light | — | ![]() |
| Rule health, dark | — | ![]() |
| Rule health, mobile | — | ![]() |
Validation
- At 920eaa7be (review fixes, overlaps split out, #795 merged up):
health_test.rb+rules_controller_test.rb88 runs, 0 failures;health_test.rbalone 30 runs before the split, every mutation above run in a container copy. Full suite: CI on this head. - Earlier, at 88d98be0b:
rules_controller_test.rb+test/models/rule: 377 runs, 0 failures, 0 errors;runner_test.rbandrules_rake_test.rbunchanged and passing. - Full
bin/rails testat 88d98be0b: 16446 runs, 104981 assertions, 0 failures, 8 errors, 9 skips. All 8 errors are environmental:ReleaseNotesWorkflowTest,Errno::ENOENT: node. bin/rubocop,erb_linton the changed files: no offenses.bin/brakeman: 0 warnings.- Independent review before commit: 6 points. Fixed: a same-conditions pair differing only in an extra action fell into no group; the savepoint test couldn't fail (now a real PostgreSQL error); an all-unresolvable rule rendered a garbled "shadowed" line; stop rules now sorted by position. Declined with reason: cost (one match query per rule plus pairwise set intersections, on request in a lazy frame, as the issue's "persist results" alternative anticipates), and the stop-rule-above winner case (listed under left out).
- No API or migration changes. Checked against upstream
AGENTS.md(DS::Section / DS::Link, tokens,t()throughout). - Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.
Refresh (2026-11-22)
- Merged
up/rule-runner-claims(with current upstreammain) in; no conflicts. - Full
bin/rails testat a53e59371: 18172 runs, 0 failures, 8 errors (environmentalReleaseNotesWorkflowTest, nonode).






