Extract the rule runner's claim bookkeeping into Rule::Runner::Claims

JA
9d ago
+124-29

Part of #5175

Fork pre-flight: jaysbeekay/sure#795 (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 is the first: the issue's plan puts the Claims extraction in its own behaviour-preserving PR.

Summary

Rule::Runner kept its "top rule wins" and "stop processing" bookkeeping (the claimed attributes and the stopped transactions) in instance state with three private helpers. They move unchanged into Rule::Runner::Claims, one per run. Behaviour is unchanged. The next PR's read-only dry run records claims with the same object, so it cannot drift from the real run.

What changed

  • New app/models/rule/runner/claims.rb (35 lines): claimed_ids, stopped_ids, #unstopped_scope(rule), #record(rule, matched_ids) and the private #reserved_attributes(action), moved verbatim, including the rescue and its log line. The one change: #unstopped_scope takes within_ids:, which the runner passes its transaction_ids (the immediate runs from #4347), as its own helper did.
  • app/models/rule/runner.rb (6 added, 29 removed): holds @claims = Claims.new, calls claims.unstopped_scope(rule, within_ids: transaction_ids), passes claims.claimed_ids to Rule#apply, and calls claims.record in the same ensure as before.

Deliberately left out

  • Anything else in the runner, Rule#apply (which still takes the claimed_ids hash), and the Rule health page (next PR).

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

CallerCallEffect
ApplyRulesJob (after every sync)Rule::Runner.new(family, rules: active rules, execution_type: "scheduled")unchanged
ApplyAllRulesJoball rules, ignore_attribute_locks: trueunchanged
RuleJobone rule, with the active rules above itunchanged
rules:apply_all rake task (lib/tasks/rules.rake:24)execution_type: "manual", ignore_attribute_locks: trueunchanged; Rule::Runner.new's signature is the same, so test/lib/tasks/rules_rake_test.rb:21 needs no change
Rule#applyreceives claimed_ids:same Hash object as before; it reads with fetch, which doesn't fire the default block

Siblings: a grep for record_claims, reserved_attributes, unstopped_scope, stopped_ids and claimed_ids across app, lib and test finds no other reader of the moved methods.

Tests

New test/models/rule/runner/claims_test.rb; test/models/rule/runner_test.rb and test/lib/tasks/rules_rake_test.rb unchanged.

TestBefore (no Claims)After
a rule claims the attributes it sets on its matchesfails: NameError: uninitialized constant Rule::Runner::Claimspasses
a rule that stops processing hides its matches from the rules below (asserts the unstopped scope before and after the record)fails (same)passes
tags claim nothingfails (same)passes
an action that cannot be resolved claims nothing and does not raisefails (same)passes
claims and stops from several rules add up (review)added after review; pins merge: replacing the claimed set fails line 44, replacing the stopped set fails line 45passes

Mutations (applied to the container copy only, then claims, runner and rake tests run)

MutationCaught by
stops not recordedclaims stop test + 3 runner tests
unstopped_scope ignores stopsclaims stop test + 3 runner tests
non-reserving actions (AI) claim3 runner tests (AI action above, deleted category, blank name)
rescue narrowed so an unknown action raises3 errors (claims + runner unknown-action tests)
record claims nothingclaims test + 5 runner tests
runner no longer records claims9 runner tests + rake "sets the category from the top rule"

Validation

  • Full bin/rails test at fed592a80: 18146 runs, 112285 assertions, 0 failures, 8 errors, 9 skips. All 8 errors are environmental: ReleaseNotesWorkflowTest, Errno::ENOENT: node (the test image has no Node). runner_test.rb, including the #4347 immediate-run tests, passes.
  • bin/rubocop and erb_lint on the changed files: no offenses. bin/brakeman: 0 warnings.
  • Checked against upstream AGENTS.md (blob 1a5dbebd): no deviations.
  • No UI, API or migration changes.

Summary by cubic

Extracts the rule runner's claim bookkeeping — the claimed attributes and stopped transactions behind "top rule wins" and stop-processing — into Rule::Runner::Claims, one per run, with no behavior change. The next PR's read-only dry run records claims with the same object, so it can't drift from the real run.

  • Claims#unstopped_scope now takes within_ids:, which the runner passes its transaction_ids for immediate rule runs.
  • Rule::Runner's constructor and Rule#apply's signature are unchanged, so the jobs and rules:apply_all task need no changes.

Summary by CodeRabbit

  • Bug Fixes
    • Improved rule processing so transactions handled by one rule are not unnecessarily claimed again by later rules.
    • Rules that stop processing now prevent matching transactions from being processed by subsequent rules.
    • Rule processing continues reliably when an action’s claim information cannot be checked.