Extract the rule runner's claim bookkeeping into Rule::Runner::Claims
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, callsclaims.unstopped_scope, passesclaims.claimed_idstoRule#apply, and callsclaims.recordin the sameensureas before.
Deliberately left out
- Anything else in the runner,
Rule#apply(which still takes theclaimed_idshash), and the Rule health page (next PR).
Blast radius: who else hits this line, and what do they pass?
| Caller | Call | Effect |
|---|---|---|
ApplyRulesJob (after every sync) | Rule::Runner.new(family, rules: active rules, execution_type: "scheduled") | unchanged |
ApplyAllRulesJob | all rules, ignore_attribute_locks: true | unchanged |
RuleJob | one rule, with the active rules above it | unchanged |
rules:apply_all rake task (lib/tasks/rules.rake:24) | execution_type: "manual", ignore_attribute_locks: true | unchanged; Rule::Runner.new's signature is the same, so test/lib/tasks/rules_rake_test.rb:21 needs no change |
Rule#apply | receives 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.
| Test | Before (no Claims) | After |
|---|---|---|
| a rule claims the attributes it sets on its matches | fails: NameError: uninitialized constant Rule::Runner::Claims | passes |
| 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 nothing | fails (same) | passes |
| an action that cannot be resolved claims nothing and does not raise | fails (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 45 | passes |
Mutations (applied to the container copy only, then claims, runner and rake tests run)
| Mutation | Caught by |
|---|---|
| stops not recorded | claims stop test + 3 runner tests |
unstopped_scope ignores stops | claims stop test + 3 runner tests |
| non-reserving actions (AI) claim | 3 runner tests (AI action above, deleted category, blank name) |
| rescue narrowed so an unknown action raises | 3 errors (claims + runner unknown-action tests) |
record claims nothing | claims test + 5 runner tests |
| runner no longer records claims | 9 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 stackedup/rule-healthhead, 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/rubocopon 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, inrunner.rb: upstream's immediate runs (we-promise/sure#4347) passwithin_ids: transaction_idstomatching_scope;Claims#unstopped_scopenow takeswithin_ids:and the runner passes it. - Full
bin/rails testat fed592a80: 18146 runs, 0 failures, 8 errors (environmentalReleaseNotesWorkflowTest, nonode).