feat(rules): run rules in a fixed order with "stop processing"

Merged
GF
2mo ago
+1397-321

Description

Before: after every sync, each active rule runs as its own background job, in parallel and in no particular order. When two rules set the same field on a transaction, whichever job finishes last wins, so the result can change from one sync to the next. The rules list can be sorted by name or update date, but that sort has no effect on how the rules run.

After: rules run from top to bottom, in an order the user sets by dragging (mouse, touch or keyboard). For each transaction and field, the highest matching rule wins and rules further down leave that field alone. Tags are still added by every matching rule. A new "Stop processing further rules" switch keeps rules further down from touching the transactions a rule matched at all (any field, including tags), which is useful for an exception placed above a broader catch-all rule. "Apply rule" runs the active rules above it first and then the rule itself, so it gives the same result as the scheduled run; rules below it don't run. The confirm dialog says how many rules above will run.

Related to discussion #3476, where a user asks for exactly this: a catch-all rule plus more specific rules, with a defined order instead of "a toss up which category it will choose".

How

  • Migration (reversible): adds rules.position, rules.stop_processing and an index on (family_id, position). Existing rules are numbered by name (A–Z, unnamed last), the default order of the rules page, so the first ordered run matches what users already see.
  • Rule::Runner runs a family's rules serially under a per-family advisory lock. It tracks, per attribute, which transaction ids are already claimed, plus the ids stopped by a "stop processing" rule. It executes the requested rules plus the active rules above them, top to bottom, and stops after the last requested rule, so applying a single rule behaves exactly like its turn in the full run. ignore_attribute_locks only applies to the requested rules. All actions of a rule work on the ids matched before the first action runs. Reading an action's claims is guarded per action, so an action whose type can't be resolved (e.g. from an import) claims nothing instead of aborting the remaining rules. Ids are passed as a single PG array parameter, so large sets stay under PostgreSQL's bind limit. effective_date is still applied through the rule's resource scope.
  • Executors declare claimed_attributes. Tags and the email notification claim nothing. The AI actions skip claimed transactions but don't claim anything themselves: they only fill empty or unlocked fields later in the background, so a broad AI rule must not silently disable the specific rules below it.
  • Jobs: Family::Syncer and Financekit::Downstream enqueue one ApplyRulesJob per family instead of one RuleJob per rule. RuleJob and ApplyAllRulesJob both go through the runner. All three retry while the lock is held. RuleJob fails only when the applied rule fails; a failing rule above is recorded as a failed run.
  • UI: the list is always shown in run order, with a hint line, a drag handle (DS::Button, icon variant) and a "Stop" badge; the form has the new switch. The reports page's sortable Stimulus controller became a shared sortable-list controller (URL and param come from values) used by both pages. If a reorder save fails, the page reloads to show the order that was actually saved.
  • Import/export: the NDJSON export/import includes position and stop_processing; imported rules are appended in their exported order. The CSV export is sorted by run order (stop_processing in CSV is left for a follow-up).
  • API: position and stop_processing are exposed read-only; the rswag schema and docs/api/openapi.yaml are regenerated.

Effects on other parts

  • Balances, reports and budgets are unchanged; rules only change the same transaction fields as before, now deterministically.
  • Fewer jobs per sync (one per family instead of one per rule).
  • The "Sort by name / updated" control on the rules page is removed, because the list order now is the run order.
  • Screenshots
  • Before = main, after = this branch, demo data. Top: rules list (order hint, drag handles and "Stop" badge instead of the sort controls). Bottom: rule form with the new "Stop processing further rules" switch.
before-after
  • Confirm dialog for "Apply rule", before (top) and after (bottom): it now says that the active rules above run first.

Apply rule confirm dialog

Validation

Full bin/rails test: 10605 runs, 0 failures, 0 errors.
New tests cover the runner (top rule wins, reordering, claiming without a change, stop processing, additive tags, locks, single-rule apply, inactive rules above, AI actions above, a rule above that fails to match, effective date, run records, a failing rule, a busy lock, actions pinned to the matched ids, single apply equals the full run (rename → categorize → catch-all), rules below not run, locks ignored only for the applied rule, and a broken action above or before a healthy rule), the jobs, update_positions!, the reorder endpoint including another family's ids, the NDJSON import order and the API fields.
System tests for rules (including keyboard reordering) and reports pass.
RuboCop, erb_lint, Biome and Brakeman are clean.

Summary by CodeRabbit

  • New Features
    • Reorder rules with drag-and-drop, touch gestures, or keyboard controls. Rules now run in the displayed order instead of being sorted by name or update date.
    • Set a rule to stop lower-priority rules from processing matching transactions.
    • Rule exports and API responses include rule order and stop-processing settings, which are preserved on import.
  • Improvements
    • Earlier rules take precedence for fields they handle, preventing later rules from overwriting those values.
    • Reports sections remain sortable with drag, touch, and keyboard controls.