Skip to content

Claim a fanned-out replay before the agent runs - #715

Merged
TonsOfFun merged 1 commit into
activeagents:mainfrom
yorzi:fix/fan-out-replay-claim
Nov 17, 2026
Merged

TonsOfFun merged 1 commit into
activeagents:mainfrom
yorzi:fix/fan-out-replay-claim

Conversation

@yorzi

@yorzi yorzi commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

What this does

With scenario_evaluation_fan_out on, ScenarioEvaluationRunner#replay_scenario! checked for a stored result, ran the replay, and only then took the run's lock to store it. The lock guarded the store, not the provider call. Two deliveries of one EvaluationReplayJob (a queue redelivering it, or handing it to two workers) both ran the agent: a billed provider call that may call the agent's tools. The second answer was then dropped, leaving an orphaned agent run. Separately, #fail_result! marked any pending, unscored result errored. So a duplicate delivery that failed, for example when enqueuing the judge raised, turned the other delivery's good replay into its own error.

The replay now takes a claim before the agent runs:

  • The claim: a token and expiry per scenario/provider/model, kept in the run's existing fan-out bookkeeping (scores["_fan_out"]["replays"]). It is taken under the run lock that bookkeeping already uses (update_fan_out_state!). The provider call happens outside any transaction, and there is no migration.
  • A delivery that finds a live claim re-enqueues itself wait_until: the claim's expiry, as EvaluationJudgeJob does inside a scoring lease (Prevent overlapping evaluation jobs from scoring twice #614). When it runs again it replays only if nothing was stored, which covers a worker that stopped mid-replay.
  • The owner drops its claim together with the store, fenced on its token, so a late old owner cannot release its successor's claim.
  • #fail_result! writes only for this delivery's own pair: one with no result and no live claim held elsewhere, or the result this runner stored. EvaluationReplayJob hands the failure to the same runner, so it knows which case it is in.

What it promises: one active replay per pair across duplicate deliveries, not exactly once. A worker that stops after the provider answered and before the store is replayed again once its claim expires. A replay still running after REPLAY_LEASE_DURATION (30 minutes) can overlap a takeover, and the first answer stored is kept.

The sequential path, the judge's scoring lease and run completion are unchanged. Completion writes the run's final scores, so claims left by a stopped worker go with the rest of the bookkeeping.

Tests, in actionagent/test/scenario_evaluation_fan_out_test.rb:

  • Fail before the change:
    • A delivery that arrives while another is mid-replay does not call the provider. Before the change it made 2 calls instead of 1; the duplicate now comes back at the claim's expiry and finds the result stored.
    • A duplicate delivery whose judge enqueue fails leaves the other delivery's stored replay pending, and the run then scores and completes. Before the change the row became errored with "queue unavailable".
  • New behavior: a redelivery inside a stopped worker's live claim replays nothing, then replays once after the claim expires and completes the run. A late old owner does not release its successor's claim. A failure while another delivery holds the claim stores nothing over it.
  • Guard existing behavior: a delivery whose own judge cannot be enqueued still stores its replay errored, and the run completes. The existing fan-out, judge-lease and no-lease-columns tests are unchanged and pass.
  • Mutations, each caught: dropping the live-claim check (2 failures), the own-row check, the claimed-elsewhere check, the release token fence, or handing the failure to a fresh runner in the job (1 failure each).
  • A two-thread version (both deliveries really concurrent, with a barrier inside the provider call) gives 1 provider call on Rails 8.1 and 7.2. It is not committed, because the deterministic nested-delivery test covers the same interleaving without threads.

Decision for the maintainer

This PR is also a proposal: it adds replay ownership, the thing fan-out was missing. Please confirm the approach, redirect it, or close it.

  • Where the claim lives. I kept it in the run's _fan_out JSON to avoid a migration and to reuse the lock and bookkeeping reserve_refinement! already uses. The alternative is columns on EvaluationScenarioResult plus a placeholder row created before the replay. That would need migration 036 and a decision about uniqueness, since the sequential path can store two rows for one provider/model under two labels.
  • The claim length. 30 minutes, the same as JUDGE_LEASE_DURATION.
  • One edge case changes. If an owner stores its replay and its process dies before enqueuing the judge, and the redelivery's enqueue also fails, the result now stays pending instead of being marked errored by the redelivery. That needs two failures in a row. The common single failure is still recorded.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the contributing guide
  • This fits activeagent's stated focus
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my use case

Related issue

None filed. Complements #713, which stops a doubled EvaluationRunJob from fanning a run out twice, the largest source of duplicate replay jobs. This covers the redeliveries that remain. Independent of it: no shared lines.

Quality check

  • The project's hooks and linters pass (bin/rubocop on the changed files)
  • The new tests fail before this change and pass after it
  • The full test suite passes. Engine suite on Rails 7.2 (database migrated as the CI job builds it): 2031 runs, 0 failures, 0 errors. On Rails 8.1: 0 failures, and 11 errors, all in CliContractTest (rack-session undefined method 'id' for Hash), which fail identically on an untouched main in this environment
  • I updated documentation where needed (CHANGELOG only)
  • I didn't edit generated files by hand

Ownership

  • I understand every line of this change and can answer questions about it

API changes

  • Breaking change
  • New public methods or classes
  • Changed method signatures
  • No API changes

ScenarioEvaluationRunner#replay_scenario! checked for a stored result,
replayed, and only then took the run's lock to store, so the lock guarded
the store and not the provider call. Two deliveries of one
EvaluationReplayJob both ran the agent (a billed call that may call its
tools) and the second answer was dropped. #fail_result! marked any
pending, unscored result errored, so a duplicate delivery that failed
(an enqueue error, say) turned the other delivery's good replay into its
own error.

The replay now takes a claim before the agent runs: a token and expiry per
scenario/provider/model in the run's fan-out bookkeeping
(scores["_fan_out"]["replays"]), taken under the run lock the bookkeeping
already uses, so no migration. The result row cannot carry it, since it
is created only once there is an answer, and nothing makes a pair's row
unique. A delivery that finds a live claim re-enqueues itself for when
the claim expires, as a judge job does inside a scoring lease, and then
replays only if nothing was stored, which covers a worker that stopped
mid-replay. The owner drops its claim with the store, fenced on its
token. #fail_result! now writes only for this delivery's pair: no result
yet and no live claim held elsewhere, or the result this runner stored.
EvaluationReplayJob hands the failure to the same runner so it knows
which that is.

This is one active replay per pair across duplicate deliveries, not
exactly once: a worker that stops after the provider answered and before
the store is replayed after its claim expires.
@TonsOfFun
TonsOfFun merged commit 4c116b6 into activeagents:main Nov 17, 2026
18 checks passed
TonsOfFun added a commit that referenced this pull request Nov 17, 2026
@TonsOfFun TonsOfFun added this to the 1.9.9 milestone Nov 17, 2026
@TonsOfFun TonsOfFun mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants