Count a mixed rule's ordinary changes as modified, not processed

Merged
JA
12d ago
+335-31

Closes #5458

Fork pre-flight: jaysbeekay/sure#922 (same branch, same commit). Its base is still the fork's copy of main, so since the move onto the AI branch its diff includes the AI stack; it was last a clean pre-flight at 9d5f0ad.

Based on feat/ai-5077-followups (#5608, top of the AI stack #5504), which is merged in, not rebased, as josh-sanders asked. It lands after #5504.

Problem

For a rule with an AI action, the rule run history recorded the AI's queued transactions as "processed" and the other actions' changes were never counted as "modified". The run counts on the rules page were wrong for every mixed rule, and "blocked" was derived from the wrong numbers.

#5608 already counts each AI job before it's queued and stops the runner writing status and pending_jobs_count, which fixes #5546. It doesn't change what processed, modified and blocked mean: enqueue_ai_job still returns the queued count as modified_count, and Rule#apply still adds it in.

Change

  • Rule#apply no longer folds the AI actions' queued count into modified_count, and enqueue_ai_job no longer returns it. The runner stores processed as the matched count and modified as what the ordinary actions changed, the same as for a rule without AI actions. RuleRun#ai_handled! adds the AI jobs' changes on top.
  • Modified is capped at the matched count, in the runner, ai_handled! and complete_job!, so it never exceeds processed and hides "blocked". The cap applies to every run, including rules without an AI action, because the same per-action sum can push their modified count past processed too.
  • An AI job can report through ai_handled! before the runner records the run's own changes. The runner adds its count in one statement, LEAST(transactions_modified + n, transactions_queued), as await_ai! increments its counters. No row lock.
  • transactions_blocked is 0 while any AI job is still pending, so in-flight transactions don't show as blocked.
  • New column rule_runs.job_transactions_modified holds only what the AI jobs changed. The immediate-run toast gates on it (with #5608's finished?), so an AI job that changed nothing (or failed) doesn't offer the toast a second time on a mixed rule.
  • changed_now_count was identical to the run's modified count, so it's dropped from Rule#apply.

Migration notes

Adds rule_runs.job_transactions_modified (integer, default 0, not null): how many transactions a run's AI jobs changed, which the immediate-run toast now checks. Runs whose AI jobs are still pending at deploy (pending_jobs_count > 0) get it backfilled from transactions_modified, which until now held only the AI jobs' changes; other rows stay 0. Rolling back drops the column. Run bin/rails db:migrate. Its timestamp (20261121093556) sorts before #5608's 20261122130000; they touch different tables, so the order doesn't matter.

Tests

  • test/models/rule/runner_test.rb: a mixed rule records its ordinary changes as modified (one match already carries the tag, so modified is 1 of 2, which a count equal to the queued transactions would fail); modified never exceeds matched, with and without an AI action; an AI job that reports before the runner writes keeps its changes (fails with the runner's plain write, "Expected: 5, Actual: 2"); a locked match still counts as processed on an AI-only run.
  • test/models/rule_test.rb: apply with an AI action counts only the other actions' changes.
  • test/models/rule_run_test.rb: on an immediate mixed rule, a job that changed nothing and fail_job! don't offer the toast again, and an AI job that changed something does; ai_handled! and complete_job! stay within the matched count; nothing is blocked while jobs are pending, including a failed run with jobs still out.
  • test/migrations/add_job_transactions_modified_to_rule_runs_migration_test.rb: the backfill touches only runs with pending jobs; down then up restores the column.

Validation on eaadf3d96: bin/rails test full suite, 18,919 runs, 0 failures (the only errors are 8 ReleaseNotesWorkflowTest ones from node missing in my test image). Rubocop clean.

Left out on purpose

  • A transaction changed by two actions still counts twice below the cap. That's older than this change (Rule#apply sums per action) and is #5473.
  • job_transactions_modified itself isn't capped; it's only read as "did the AI change anything".

Summary by cubic

Fixes rule run counts for rules that mix ordinary actions with an AI action: the AI's queued transactions were counted as processed and the ordinary actions' changes were never counted as modified, so processed, modified, and blocked were wrong for every mixed rule.

Bug Fixes

  • Rule#apply no longer folds the AI's queued counts into modified; the runner records matched as processed and ordinary action changes as modified, same as rules without AI actions.
  • Modified is capped at the matched count so it never exceeds processed and inflates blocked.
  • Nothing counts as blocked while any AI job is still pending.
  • If an AI job reports before the runner writes the run's counts, the runner updates under the row lock so the job's recorded changes aren't overwritten by the run's own.
  • Mixed-rule tests now keep one match pre-tagged so they prove modified and queued are tracked separately.
  • A locked match still counts as processed on an AI-only run, even though the AI job only gets the enrichable transactions.

Migration

  • Adds rule_runs.job_transactions_modified to hold only what AI jobs changed; the immediate-run toast now gates on it.
  • Backfills that column from transactions_modified for runs whose jobs are still pending. Run bin/rails db:migrate.

Summary by CodeRabbit

  • Bug Fixes
    • Rule runs now report modified transaction counts more accurately when immediate actions and AI processing both make changes.
    • Counts are capped at the number of matched transactions, and pending AI jobs no longer cause transactions to be reported as blocked.
    • Results are offered again only when AI processing makes changes, preventing unnecessary repeat offers.
    • Asynchronous rule runs now report queued transactions as processed while AI jobs are pending.