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

JA
14d ago
+124-29

Part of jaysbeekay/sure#702

Upstream issue: we-promise/sure#5175
Upstream PR: we-promise/sure#5525 (same branch, same commit), in native stack we-promise/sure#5529

First of two PRs for jaysbeekay/sure#702. The issue's plan puts the Claims extraction first, as its own PR, once the whole change passes ~300 non-test lines; the Rule health page is stacked on this branch: jaysbeekay/sure#796 (up/rule-health), with jaysbeekay/sure#797 (jaysbeekay/sure#703) on top.

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.
  • app/models/rule/runner.rb (5 added, 29 removed): holds @claims = Claims.new, calls claims.unstopped_scope, 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

  • claims_test.rb, runner_test.rb, rules_rake_test.rb, test/jobs, rule_test.rb: 363 runs, 0 failures, 0 errors.
  • Full bin/rails test (run on the stacked up/rule-health head, which contains this commit): 16446 runs, 104981 assertions, 0 failures, 8 errors, 9 skips. All 8 errors are environmental: ReleaseNotesWorkflowTest, Errno::ENOENT: node (the test image has no Node).
  • bin/rubocop on the changed files: no offenses. bin/brakeman: 0 warnings.
  • No UI, API or migration changes. Checked against upstream AGENTS.md.
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Refresh (2026-11-22)

  • Merged current upstream main (9e419e7be) in. One conflict, in runner.rb: upstream's immediate runs (we-promise/sure#4347) pass within_ids: transaction_ids to matching_scope; Claims#unstopped_scope now takes within_ids: and the runner passes it.
  • Full bin/rails test at fed592a80: 18146 runs, 0 failures, 8 errors (environmental ReleaseNotesWorkflowTest, no node).