Repository navigation
Claim a fanned-out replay before the agent runs - #715
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
With
scenario_evaluation_fan_outon,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 oneEvaluationReplayJob(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 resulterrored. 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:
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.wait_until:the claim's expiry, asEvaluationJudgeJobdoes 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.#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.EvaluationReplayJobhands 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:erroredwith "queue unavailable".errored, and the run completes. The existing fan-out, judge-lease and no-lease-columns tests are unchanged and pass.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.
_fan_outJSON to avoid a migration and to reuse the lock and bookkeepingreserve_refinement!already uses. The alternative is columns onEvaluationScenarioResultplus 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.JUDGE_LEASE_DURATION.Type of change
Scope check
Related issue
None filed. Complements #713, which stops a doubled
EvaluationRunJobfrom 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
bin/rubocopon the changed files)CliContractTest(rack-sessionundefined method 'id' for Hash), which fail identically on an untouchedmainin this environmentOwnership
API changes