Add the overlaps section to Rule health

JA
9d ago
+393-1

Closes #5175

Fork pre-flight: jaysbeekay/sure#830 (same branch, same commit).

Stack: #5525 Claims extraction → #5526 Rule health page → #5527 overlaps section → #5528 save-time warning on the rule form (stack #5529). This PR adds item 4 of the issue, Overlaps, the last part not yet in the stack. It was split from #5526 to keep that PR near the ~300-line guide.

Summary

Adds the Overlaps section to the Rule health page: active rules below another that sets the same field on transactions both match ("1 of its 2 matches is also matched by the rule above"), or whose conditions name the same or contained text (a merchant condition read as the merchant's name) with otherwise the same conditions, so uber AND amount > 100 and uber AND amount < 10 are not listed. Pairs already listed as duplicates or conflicts, and shadowed rules, are left out.

What changed

  • Rule::Health#overlaps, with same_text?, texts, conditions_besides_text and merchant_names (commit "Add the overlaps section to Rule health" restores what #5526's branch moved out).
  • same_text? once folded every condition's text, so = Uber and = uber were listed as the same text although = matches case and the two rules match disjoint transactions. Text is now folded only when one side runs as ILIKE (like, starts_with, ends_with), the same rule condition_value uses.
  • rules/_health_report.html.erb overlaps section and its rules.health.overlaps.* strings. The shared line pluralises on the shared count ("1 of its 2 matches is…", "2 of its 3 matches are…").

Size: 115 non-test lines added.

Deliberately left out

  • Scoping merchant_names to the family's merchants: declined. The ids come from the family's own rule conditions, the name is only compared as text and never rendered, and family.merchants would drop the provider merchants a merchant condition legitimately names (TransactionMerchant#options lists available_merchants_for). Scoping through available_merchants adds a pluck of every transaction's merchant id per report for no exposure.
  • The shared count reads matches before any rule runs, like the rest of the report; it is advisory, and #5526 holds back the shadowed and match-nothing verdicts where a rule above changes what a rule reads.

Blast radius: who else hits this line, and what do they pass?

  • same_text? and texts are read by overlaps here and by Rule::Health#similar_to in #5528, which passes the form's unsaved rule; both get the same case rule.
  • condition_value (duplicates, conflicts) is unchanged here.

Tests

test/models/rule/health_test.rb: the 8 overlap tests moved back from #5526's branch, plus 1 new; rules_controller_test.rb: the report test asserts the overlaps section again, plus 1 new test for the shared line's wording.

TestKind
shared transactions on the same field: overlap, 1 of 2positive
shared transactions, different fields: no overlapnegative
"contains uber" vs "= UBER EATS": overlap with 0 sharedpositive
= "Uber" vs = "uber": not the same text (new; failed before the fix: overlap listed with same_text=true)negative
contained text with contradicting amount conditions: no overlapnegative
contained text inside an OR group: still the same textpositive
merchant condition read by name: overlap; merchant renamed: nonedelta
contained text, different fields: no overlapnegative
same conditions, same value, plus another action: overlappositive
controller: report renders the conflicts, shadowed and overlaps sectionscontroller
controller: shared line reads "1 of its 2 matches is" and "2 of its 3 matches are" (failed before: "1 of its 2 matches are")wording

Mutations (container copy only)

MutationCaught by
fold text for every operator= "Uber" vs = "uber"
never fold textmerchant condition read by name
overlaps ignore the shared field, merchant compared by id, skip every same-conditions pairrun when this code was first written, unchanged since

Validation

  • Full bin/rails test at 2d521e66d: 18187 runs, 112408 assertions, 0 failures, 8 errors, 9 skips. All 8 errors are environmental: ReleaseNotesWorkflowTest, Errno::ENOENT: node.
  • Wording fix: the new test failed before it on 1 of its 2 matches is; with the one: form changed back to "are" it fails again.
  • test/system/rules_test.rb: 13 runs, 1 failure (keeps a rule with long text inside a phone-width screen), which fails the same way on main (9e419e7be) in the same container.
  • bin/rubocop and erb_lint on the changed files: no offenses. bin/brakeman: 0 warnings.
  • Checked against upstream AGENTS.md (blob 1a5dbebd): no deviations.

Screenshots

Before (#5526's head): Duplicates and Shadowed only. After: the Overlaps section, light, dark and mobile.

BeforeAfter
Light702-before-light-health702-after-light-health
Dark702-before-dark-health702-after-dark-health
Mobile702-before-light-mobile-health702-after-light-mobile-health

Summary by cubic

Adds the Overlaps section to the Rule health page, completing item 4 of #5175. The page now lists active rules below another that set the same field on transactions both match, or whose conditions name the same or contained text, alongside the existing duplicates, conflicts, shadowed, and match-nothing sections. Pairs already listed as duplicates or conflicts are left out.

  • Text comparison is case-sensitive for = conditions, so = Uber and = uber no longer count as the same text; text is folded only when one side runs as ILIKE (like, starts_with, ends_with). Two starts_with rules overlap only when one prefix starts with the other, and two ends_with rules likewise for suffixes.
  • Only required conditions (top-level and in AND groups) are compared, so text alternatives inside OR groups don't keep rules apart; a rule whose required name conditions can't all hold at once — two different exact names, or prefixes or suffixes neither of which extends the other — is not listed. An exact = text must also pass the other side's contains, starts with, or ends with check. Merchant conditions compare the merchant's id, and rules that require different merchants never overlap.
  • Shadowed rules are left out entirely, both as the rule below and as the other rule of an overlap.
  • The shared line pluralises on the shared match count ("1 of its 2 matches is…" vs "2 of its 3 matches are…").

Summary by CodeRabbit

  • New Features
    • The rule health report now highlights overlapping active rules that may match the same transactions and affect the same fields.
    • Overlap details show affected fields, shared-match counts when available, and whether rules use the same or related text conditions, such as matching or contained text.
    • Shadowed or failed rules, and rules already reported as duplicates or conflicts, are excluded from overlap results.