Skip to content

fix(capture): wait for claimed model-call writes before snapshotting a rollout - #4593

Merged
wprazuch merged 5 commits into
mainfrom
wprazuch/capture-late-write-barrier
Oct 15, 2026
Merged

wprazuch merged 5 commits into
mainfrom
wprazuch/capture-late-write-barrier

Conversation

@wprazuch

Copy link
Copy Markdown
Contributor

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 flagged
missing_captured_call / trajectory_capture_mismatch even 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:

  • the model server writes a per-call pending marker before queueing the write and clears it when the
    write returns;
  • rollout collection waits for a rollout's outstanding claims to clear before merging capture into the
    record (bounded, default 10s, polled every 50ms, returns immediately when nothing is outstanding);
  • a claim still outstanding at the timeout is recorded as a model_call_capture_pending observation
    gap instead of vanishing.

model_call_capture_pending is deliberately not added to _INCOMPLETE_MODEL_CALL_GAPS. A claimed
write 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 main first):

pytest tests/unit_tests/test_base_responses_api_model.py          # 111 passed
pytest tests/unit_tests/test_rollout_collection.py \
       tests/unit_tests/test_rollout_health.py \
       tests/unit_tests/test_rollout_observability.py \
       tests/unit_tests/test_rollout_correlation.py               # 587 passed
pre-commit run --all-files                                        # passed

Six new cases in tests/unit_tests/test_base_responses_api_model.py cover: claims scoped to one
rollout, an unsafe model_call_id sanitised into the marker filename, the barrier waiting for a
detached 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

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Documentation added or updated, or N/A with justification.
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format checks pass).
  • New source files include the required Apache-2.0 SPDX header.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

…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]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 14, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working complexity:medium Single-domain change with interacting parts or a moderate review surface needs-review PR is ready for code review and waiting on a reviewer labels Oct 14, 2026
… 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]>
@wprazuch

Copy link
Copy Markdown
Contributor Author

/ok to test 3402433

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

@wprazuch

Copy link
Copy Markdown
Contributor Author

/ok to test 6ae925b

@wprazuch
wprazuch added this pull request to the merge queue Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/38732532527

Merged via the queue into main with commit b2605d0 Oct 15, 2026
72 of 81 checks passed
@wprazuch
wprazuch deleted the wprazuch/capture-late-write-barrier branch October 15, 2026 12:25
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

1 inactive deployment
public — 6ae925b0 Deployed Oct 15, 2026 by copy-pr-bot[bot] via release / finalize / notify #5456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working complexity:medium Single-domain change with interacting parts or a moderate review surface needs-review PR is ready for code review and waiting on a reviewer sla:triage-overdue Review assignment is over the one-business-day SLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants