Repository navigation
fix(capture): wait for claimed model-call writes before snapshotting a rollout - #4593
Merged
Merged
Conversation
…a rollout #4092 shielded the capture write so cancellation can no longer discard it, but a shielded write runs detached: the row can land after the rollout record has been snapshotted. It is then on disk and absent from ng_model_call_capture, which surfaces as missing_captured_call / trajectory_capture_mismatch. The writer now claims a per-call marker before queueing the write and clears it when the write returns, so a reader can tell "not yet" from "never". Rollout collection drains a rollout's claims before merging the capture into its record. A claim that is still outstanding after the timeout becomes a model_call_capture_pending gap rather than a silent omission. The new code is deliberately not added to _INCOMPLETE_MODEL_CALL_GAPS: a stranded claim is real data loss and should stay unhealthy, not be relabelled unobserved. Tests: 6 new cases in tests/unit_tests/test_base_responses_api_model.py, one of which asserts the row is genuinely absent before the barrier and present after. tests/unit_tests/test_base_responses_api_model.py 111 passed; rollout collection / health / observability / correlation 584 passed. Signed-off-by: Wojciech Prazuch <[email protected]>
Signed-off-by: Wojciech Prazuch <[email protected]>
… write Claiming the write just before queueing it left the claim invisible while the call was in flight. Rollout collection could poll, find nothing outstanding, and merge before the row landed -- then see the claim appear and stamp model_call_capture_pending on a healthy rollout. A 40-rollout e2e produced 8, 5 and 9 such gaps out of 40 on three runs. Mark the claim before the call is forwarded and clear it on both write paths. The new test asserts the claim is visible while the call is still in flight; it fails on the old placement. Signed-off-by: Wojciech Prazuch <[email protected]>
Contributor
Author
|
/ok to test 3402433 |
Contributor
Contributor
Author
|
/ok to test 6ae925b |
laszkiewiczp
approved these changes
Oct 15, 2026
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/38732532527 |
7 tasks done
pzelasko
pushed a commit
to pzelasko/Gym
that referenced
this pull request
Dec 1, 2026
… per call (NVIDIA-NeMo#6216) * fix(capture): claim model calls in one file per rollout, not a marker per call Each captured model call created and deleted its own .capture.pending marker, and readers globbed the whole capture dir to find them: at least three full listings per rollout, and one every 50 ms while a claim was open. On a shared filesystem that is a create and an unlink per call plus repeated listings of a directory that grows with the run. Claims now append to <rollout_id>.capture.claims. A claim resolves when the call's row lands in the capture file, or through a release line when the row never will. Readers open the two per-rollout files by name and never list the dir. The drain poll backs off from 50 ms to 1 s. The snapshot removes the claims file once nothing is outstanding, so the dir keeps one file per rollout. The guarantee from NVIDIA-NeMo#4593 is unchanged: a claim exists for the call's whole lifetime, readers in other processes and later jobs see it, and a stranded claim still yields model_call_capture_pending. The capture JSONL format is untouched. The health check still honours .capture.pending markers left in run dirs from older releases. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): release every claim so readers never read the capture file has_pending resolved claims by scanning the rollout's capture file for landed ids. That file holds full request and response payloads and can reach hundreds of MB, and the scan ran on every drain poll and again in the merge, so a rollout's capture was read about three times instead of once. _record now appends a release line after every write, in a finally around the capture append. It runs in the worker thread after the row is written or has failed, even when the awaiting coroutine was cancelled, so a reader that sees no open claim also sees the row. has_pending reads only the small claims file: claims minus releases. Per call this is a claim append, the capture append and a release append to one existing file per rollout, with no create or unlink per call. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): start each claims entry on its own line An append cut short by a crash leaves an unterminated fragment at the end of the claims file. The next entry was appended onto it, the joined line failed to parse and was skipped, and a live claim read as settled. Each entry now starts with a newline, written under the same exclusive lock. The parser already skips blank lines, so a torn tail can no longer swallow a later claim or release. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): retire a claims file only under the appenders' lock The snapshot read the claims as settled and then unlinked the file. A call admitted between the two lost its claim with the file, and a writer that had opened the file before the unlink appended into a dead inode. Either way a later reader saw nothing outstanding. The judge evidence path retired on an even older drain result. CaptureStore.retire_claims_if_settled reads and unlinks under LOCK_EX. After taking that lock, an appender checks that its file is still the one at the path (nlink and inode); if it was retired it reopens and appends to the replacement. Merge, judge evidence and the post-snapshot capture delete all retire through this method, so the capture dir still keeps one file per rollout and no admitted claim can disappear. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): drop older releases' pending markers when a key is reused Older releases claimed each call with a `<rollout id>.<token>.capture.pending` marker, and pre-dispatch cleanup used to remove a key's markers before reusing it. Without that, a marker left by an interrupted older run survived the clear, and the health check kept treating the fresh capture for the same key as pending, so its failed row went unmeasured. Pre-dispatch cleanup now lists each capture dir once per call and removes only the markers whose rollout id is one of the cleared keys, never other attempts or rollouts. Polling, merge and the post-snapshot delete stay listing-free, and an uncleared older run is still read as before. Signed-off-by: Wojciech Prazuch <[email protected]> * refactor(capture): delete claims files, not retire them In Gym, retire means remove and keep a fence. A claims file is removed with no fence, so rename retire_claims_if_settled to delete_claims_if_settled and clear_all_pending to delete_claims. Logs, comments and the glossary now say take and release a capture claim, earlier Gym versions, and directory. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): count a failed claims check as unsettled The judge ignored the settled check, so a judge call claimed after the drain left no gap. It now adds judge_capture_incomplete. An OSError from the check no longer makes the merge drop the calls it already read. Both count it as unsettled. Signed-off-by: Wojciech Prazuch <[email protected]> * refactor(rollouts): split the post-snapshot capture cleanup from the pre-dispatch one clear_model_call_captures_after_snapshot deletes claims only once settled and lists nothing. enrich_run_result calls it. clear_model_call_captures_for_rollouts loses its before_dispatch flag, so a new caller cannot get the pre-dispatch cleanup by default. A test now drives enrich_run_result. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): take the capture claim off the event loop The claim waits on the claims file's lock, which stalled every stream on the model server. It now runs in a thread before the call is forwarded, inside the try, so a call cancelled while claiming is recorded and releases its claim. The record write keeps its shield. Without it, a cancel drops a write still queued behind busy threads, and the claim is never released. Signed-off-by: Wojciech Prazuch <[email protected]> * docs(capture): say claims need a shared lock and can stay on disk A claims file stays while a call is in flight at the snapshot or arrives after it. Claims rely on one file lock every writer shares, so node-local locks can lose claims from a model server on another host. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): read claims off the event loop and keep captures that changed - The drain, merge and capture deletion run in worker threads, and their wait on the claims file's lock is bounded: a lock still held after the wait counts as a claim outstanding. - has_pending rechecks that the locked claims file is still the one at its path, and moves to a replacement. - delete_capture snapshots the capture just before the merge reads it and deletes it only under the claims lock, with no claim outstanding and the capture unchanged since the snapshot. - CaptureStore.clear_all_pending is back as a deprecated alias of delete_claims. - The release-order test asserts after _record, which swallows exceptions raised inside the write. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): release a call cancelled while claiming only after its claim lands A cancel does not stop the claim's worker thread, so the release could land first; a collector pass could then delete that settled claims file and leave the late claim outstanding. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): save the row before deleting its capture and read claims under their lock - A recorded shard writes its row before its capture is deleted. enrich_run_result starts the deletion as it returns instead of awaiting it. - The merge reads the capture while holding the claims lock, so no call claims or releases between the read and the settlement check. - The drain bounds each poll by its deadline, time queued for a worker included. - GDPVal, Agentic IF Realworld and the TB4 smoke script merge off the event loop. - A snapshot that can't open the capture skips the deletion. clear_all_pending finds nothing in a missing directory again. - The cancel-while-claiming test runs on a held executor instead of a sleep. Signed-off-by: Wojciech Prazuch <[email protected]> * test(gdpval): import threading inside the one test that needs it Keeps the module imports clear of main's new import, so the branch merges cleanly. Signed-off-by: Wojciech Prazuch <[email protected]> * fix(capture): keep the capture until its row is saved, settle read-only claims, save GDPVal rollouts through a cancel - A recorded shard deletes a capture only when write() saved its row, and a staged shard only once it is published. - Claims this process cannot write (an archived run) are read under a shared lock; a missing claims file is settled. - GDPVal finishes saving a rollout whose calls are being joined before a cancel goes on. Signed-off-by: Wojciech Prazuch <[email protected]> --------- Signed-off-by: Wojciech Prazuch <[email protected]>
This branch was previously deployed
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 does this PR do?
A model-call capture write can land on disk after the rollout record is snapshotted. The row is
then present in the capture file but missing from
ng_model_call_capture, so the rollout is flaggedmissing_captured_call/trajectory_capture_mismatcheven though nothing about the eval went wrong.This is the race #4092 left open. That PR shielded the capture write so cancellation can no longer
discard it, and a shielded write runs detached — which is exactly the ordering problem fixed here.
The write is now claim-tracked:
write returns;
record (bounded, default 10s, polled every 50ms, returns immediately when nothing is outstanding);
model_call_capture_pendingobservationgap instead of vanishing.
model_call_capture_pendingis deliberately not added to_INCOMPLETE_MODEL_CALL_GAPS. A claimedwrite that never landed is real data loss and should stay unhealthy, not be reclassified as unobserved.
Context: full AutomationBench runs currently report 1.7-18% of rollouts missing a captured model call
while the scores themselves are unaffected, which fails a 0.1% health gate downstream.
Validation
Run against the final diff (branch merged with
mainfirst):Six new cases in
tests/unit_tests/test_base_responses_api_model.pycover: claims scoped to onerollout, an unsafe
model_call_idsanitised into the marker filename, the barrier waiting for adetached write, the barrier giving up on a stranded claim, the merge flagging a claim that never
landed, and stale claims being cleared with the captures. The detached-write case asserts the row is
absent before the barrier and present after, so it fails without this change.
Rollouts
N/A. No environment or agent behaviour changes — this only affects capture bookkeeping on the
collection path. Rewards, responses and rollout outputs are untouched.
Compatibility and benchmark impact
No score impact. Adds a bounded wait before merging capture for a rollout; the common path is a single
check with nothing outstanding. Adds one new observation gap code,
model_call_capture_pending.Checklist
pre-commit run --all-files) (so CI lint/format checks pass).git commit -s) (so the DCO check passes).