Count a mixed rule's ordinary changes as modified

Merged
JO
4d ago
+419-29

Replaces #5477, by jaysbeekay, which was merged into feat/ai-5077-followups by mistake (18bba9890) without the author's say, and then reverted there (680608798). GitHub won't reopen a merged PR, so the same branch is opened again here, now on we-promise/sure, so the next stack can build on it. The changes are jaysbeekay's, unchanged: their commits up to eaadf3d96, then a merge of feat/ai-5077-followups and a commit that reverts the revert. See https://github.com/we-promise/sure/pull/5477#issuecomment-7124734218.

The generation outbox PR is stacked on this one.


Closes #5458

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".

šŸ¤– Generated with Claude Code

https://claude.ai/code/session_016zAWUUTovNPgC5oNfUHKhd


Summary by cubic

Fixes rule run counts for rules with an AI action: the ordinary actions' changes now count as modified, and modified never exceeds the matched count.

Rule run counts

  • Rule#apply no longer folds the AI actions' queued count into modified_count; the runner stores processed as matched and modified as what the ordinary actions changed.
  • AI jobs report their own changes through RuleRun#ai_handled!, and complete_job! and ai_handled! cap modified at the matched count.
  • transactions_blocked is 0 while any AI job is still pending, so in-flight transactions don't show as blocked.
  • The runner reloads the run after an AI job reported first, so the run it returns shows the stored counts.
  • When an action raises or a mixed rule's AI job can't be queued, the failed run still counts what the earlier actions changed; Rule#apply raises ApplyFailed carrying that count, and the runner records the original error and adds the count to the run.

Migration

  • Adds rule_runs.job_transactions_modified (integer, default 0, not null), backfilled from transactions_modified for runs with pending AI jobs at deploy. Run bin/rails db:migrate; rolling back drops the column.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected transaction counts for rules that combine immediate changes with AI processing. Counts now include synchronous and AI-reported modifications without exceeding the number of matched transactions.
    • Pending AI work no longer prematurely marks transactions as blocked, including when a run fails.
    • Prevented AI jobs with no changes from triggering result notifications.