Repository navigation
feat(token-capture): sweep abandoned captures safely - #3969
Conversation
A rollout whose request is cancelled between its first captured call and the post-delivery retire (the client-disconnect cancellation middleware does this to every rollout a harness ends mid-call) leaves an unretired capture behind: the token JSONL and its side files, which sweep_retired deliberately skips. Over a long run these accumulate without bound. TokenCaptureStore.sweep_stale(older_than_seconds) removes an unretired capture when every file of it (state, token records, intents, incomplete marker) is older than the cutoff, so an in-flight rollout, whose records are still being appended, is never touched; callers pass a cutoff above the longest possible session. Retired tombstones stay sweep_retired's. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: alexandery <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
linj-glitch
left a comment
There was a problem hiding this comment.
Reviewed stale-capture reclamation against the store locking and lifecycle contract. Fixed a race by retaining the lock inode during stale sweeps so waiting and new writers remain serialized. The patch preserves the intended retention policy.
Validation: 165 capture/conformance/golden-vector tests passed; full pre-commit suite and diff checks passed. The regression check fails with the original lock unlink and passes with the fix. The branch uses additive signed-off commits and a main merge. Model rollouts are not applicable to this filesystem cleanup; existing downstream sweep evidence is recorded above.
|
/ok to test 871757f |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/37279818041 |
…NVIDIA-NeMo#4092) ## Problem `_CaptureMiddleware` awaits the capture write unshielded on the success path while catching only `Exception`: ```python await asyncio.to_thread(_parse_and_record) except Exception: ``` A cancellation arriving after that await but **before the thread-pool executor picks up the job** drops the model-call row with no log line, no `.capture.incomplete` marker, and no trace anywhere. The error path a few lines above already catches `CancelledError`; only the success path does not. The symptom is `trajectory_capture_mismatch` / `missing_captured_call`: the agent's `ng_trajectory` references a `response_id` that `ng_model_call_capture` never recorded. ## Evidence Measured on AutomationBench, 600 tasks per run, oci-hsg, by instrumenting four probe points around the finalization and the writer. | | unshielded | shielded | |---|---|---| | `CAPTURE_FINALIZE` (finalizations entered) | 8989 | 9380 | | `CAPTURE_ATTEMPT` (writer entered) | 8967 | 9380 | | `CAPTURE_WROTE` (writer returned) | 8967 | 9380 | | **`FINALIZE` − `ATTEMPT`** | **22** | **0** | | `CAPTURE_FINALIZE_CANCELLED` | 4501 | 4230 | | `missing_captured_call` findings | 22 | 2 | | unhealthy rollouts | 30/600 (5.0%) | 3/600 (0.5%) | Three things this pins down: 1. **The store is not the problem.** `ATTEMPT` equals `WROTE` in both runs — every writer that started, finished. The `flock` + `fsync` path in `CaptureStore.record` never loses a row. 2. **The loss is pre-execution cancellation.** In the unshielded run the `FINALIZE` − `ATTEMPT` gap is 22, and that run had exactly 22 `missing_captured_call` findings. No capture-failure warnings were logged, so these were not exceptions. 3. **Shielding closes it.** The gap goes to 0 while 4230 cancellations still occur — cancellation is not prevented, it just no longer discards a queued write. Cancellation on this path is constant (~4000 per 600-task run), but only ~0.5% of cancellations lose data, because `asyncio.to_thread` cannot cancel a thread that is already running. Only those landing in the pre-execution window lose the row. Before this, the rate was stable at 5.2–6.3% across four runs that varied checkpoint, serving stack and Gym pin — including a pin carrying NVIDIA-NeMo#3969, NVIDIA-NeMo#3970, NVIDIA-NeMo#3972 and NVIDIA-NeMo#3974, which address a different capture failure and do not move this one. ## Residual: a different mechanism, now isolated Three rollouts still carry a finding after the fix, down from 22. They are **not** the same failure, and the distinction is measurable rather than asserted. Grepping every on-disk `*.capture.jsonl` in the run for the flagged `response_id`s: | | unshielded | shielded | |---|---|---| | flagged ids absent from disk entirely | 39 of 40 | **0 of 3** | | flagged ids present on disk | 1 of 40 | **3 of 3** | So before the fix the rows were overwhelmingly never written at all; after it, every flagged row *is* on disk. What remains is a late write — the row reaches the capture file after the rollout record has been snapshotted, so it is absent from `ng_model_call_capture` while present on disk. That is a separate race with a separate fix, out of scope here. This change closes the "never written" class completely. ## Testing `pytest tests/unit_tests/test_base_responses_api_model.py` — 105 passed. No unit test is added. `asyncio.to_thread` does not cancel an already-running thread, so the defect only reproduces in the window before the executor starts the job, which I could not schedule deterministically. A test that cannot be made to fail without the fix would be vacuous. The two 600-task runs above are the verification instead. Signed-off-by: Wojciech Prazuch <[email protected]>
…IA-NeMo#3939) Branch `ananthsub/token-capture-store-retire`, on NVIDIA-NeMo#3938 in the partial-rollout checkpoint stack, so this diff shows only the complete-record store change. It doesn't need NVIDIA-NeMo#3938: the same commit applied to `main` alone is branch `ananthsub/token-capture-store-retire-independent`, which also carries identical copies of the two helpers NVIDIA-NeMo#3938 adds (`validate_rollout_ids` and `RolloutRemoval`), and passes the unit suite on its own. Related and independent: NVIDIA-NeMo#3937 (striped locks; a small conflict around the removed sweeps) and NVIDIA-NeMo#4268 (bounded ledger cache). ## What changed and why The complete-record token store (`TokenCaptureStore`, behind `TokenSource`) could retire a rollout only through the conditional `TokenSource.drop`, which needs the consumed snapshot. That leaves three gaps: - **Masked and failed captures** are never retired: `rollout_collection` keeps them for diagnosis, and a trainer had no operation to remove them later. - **Abandoned attempts** have no snapshot, so `drop` can't retire them at all. - **Fences** (the `retired` state `drop` leaves behind, which the store's docs called a tombstone) were only removable by `sweep_retired`, an age-based sweep that nothing called, which is a timer in disguise. - **Abandoned captures** were only removable by `sweep_stale` (NVIDIA-NeMo#3969), the same kind of age-based sweep. It guesses that a capture is abandoned once all its files are older than a cutoff, which the caller must keep above the longest possible session. Nothing in Gym called it either. This PR gives the store the same two batched operations NVIDIA-NeMo#3938 gives the capture ledger, with the same names and meaning: | Operation | Capture ledger (NVIDIA-NeMo#3938) | Complete-record token store (this PR) | |---|---|---| | Delete: remove the data and the fence | `CaptureLedger.delete(rollout_ids)` | `TokenSource.delete(rollout_ids)` | | Retire: remove the data, keep a fence | `CaptureLedger.retire(rollout_ids)` | `TokenSource.retire(rollout_ids)`: `drop` without the snapshot check; keeps the `retired` fence | - `TokenCaptureStore.retire` and `delete` are async like the rest of `TokenSource`, with `retire_now` and `delete_now` as synchronous forms, following `freeze` and `freeze_now`. - `drop` and `retire` share one retire step, so both leave the same fence, which refuses later writes, `begin_call`, `mark_incomplete`, and `freeze`. - **A retired rollout refuses them with `TokenCaptureRetiredError`**, a new subclass of `TokenCaptureFrozenError`, instead of a plain `RuntimeError`. The sink already drops a late write to a frozen rollout quietly. Before this, a late call of an abandoned attempt, which is exactly what `retire` is for, was logged with a traceback, counted as a capture failure, and the sink then tried to mark the retired rollout incomplete. Now it is dropped like a late write after a freeze. A late call that failed to build, produced no token ids, or arrived under a mismatched identity is dropped quietly the same way: the sink tries to mark the rollout first, and when the rollout turns out to be frozen or retired it skips its warning and the capture-failure counter. - **Fewer fsyncs.** `retire` syncs the directory once per batch instead of once per rollout, and `drop` syncs it once instead of twice: the directory fsync after the state file is replaced already covers the deletions before it. Retiring takes one file fsync per rollout instead of three. `delete` syncs every non-empty batch, even when it finds nothing to remove, for the same reason as the capture ledger's `delete`: an overlapping delete may not have synced yet, and a reused rollout ID must not see the old fence or records come back after a crash. - **Retire works in two synced phases.** `retire_now` and `drop` first write and sync the fences, then remove the records and sync again. A rollout is therefore never left without records and without a fence, and an overlapping retire of the same rollout cannot return before its fence is durable: the first sync also runs when only records are left to remove. A retry removes records left behind by a retire that failed part way or by a crash. A batch that fails part way still syncs what it changed; a failing sync there is logged, not raised, so it never hides the original error. Every non-empty batch syncs its fences before returning, even when it wrote none, so an overlapping retire can never report success before the fence is durable. The second phase re-checks each fence under the rollout's lock, so a rollout ID that a delete cleared and a new attempt reused between the phases keeps its new records. - The store's docstrings and the external agent harness page now call that `retired` state a fence, the capture ledger's word for it, instead of a tombstone. - `delete` is now batched; it was single-ID and synchronous. Its one caller, `clear_token_captures_for_rollouts`, now makes one batched call. - `sweep_retired` and `sweep_stale` are removed. A capture's end of life is an explicit call from its owner, not its age: `retire` when the training framework is done with a rollout, including an abandoned attempt, and `delete` once nothing can still write. - Both operations validate the whole batch first, reject a bare string, and report `removed` and `absent` like the ledger's, typed as the same `RolloutRemovalPayload` TypedDict. - The conformance suite checks both operations, so an external `TokenSource` has to implement them. Its retirement check requires a late `put`, `mark_incomplete`, and `begin_call` to fail with `TokenCaptureRetiredError`, as `TokenSource.retire` documents, rather than accepting any exception, and lets any other `freeze` error fail the check. - `rollout_collection` behavior is unchanged: it still keeps masked and failed captures. Callers can now remove them. - **Docs:** the external agent harness page describes when to retire and delete. ## How it works ```mermaid flowchart TD A["Absent<br/>no records, no fence"] -- "first put, begin_call, or mark_incomplete" --> L["Live<br/>records on disk"] L -- "freeze" --> FZ["Frozen<br/>snapshot taken"] FZ -- "drop (exact snapshot)" --> R["Retired<br/>records gone, fence kept"] L -- "retire" --> R FZ -- "retire" --> R A -- "retire" --> R R -- "late put, begin_call, or mark_incomplete<br/>(TokenCaptureRetiredError)" --> R R -- "delete" --> A L -- "delete" --> A FZ -- "delete" --> A ``` `drop` stays the way to retire a consumed snapshot, because it refuses when the rollout changed after the snapshot. `retire` is for everything else the consumer is done with; `delete` removes fences once nothing can still write, and resets an ID before reuse. ## What the caller must do | Situation | Call | |---|---| | Built and durably delivered | `retire_rollout_token_capture` (unchanged: conditional `drop`) | | Masked or failed, evidence no longer needed | `retire([ids])` | | Abandoned attempt | `retire([ids])`; its agent may still be running, and the fence makes its later writes fail | | Retired attempts once nothing of them can still write (end of run, or after restarting every Gym server) | `delete([ids])` to remove the fences | | Reusing a rollout ID | `delete([ids])` first (`rollout_collection` already does this before re-dispatch) | Always pass a list; both operations are idempotent, so a failed batch can be retried as is. ## Compatibility - `TokenSource` gains `retire` and `delete`. Gym never checks a `TokenSource` against the protocol at runtime, but the conformance suite now requires both, so an external source must add them to keep passing. - `TokenCaptureStore.delete(rollout_id)` was synchronous and single-ID; it is now `async delete(rollout_ids)`, with `delete_now(rollout_ids)` as the synchronous form. A caller still writing `store.delete("id")` gets an un-awaited coroutine and deletes nothing, so update such calls to `delete_now(["id"])`. Gym and NeMo RL have no such callers outside the one updated here. - Writes, `begin_call`, `mark_incomplete`, and `freeze` on a retired rollout raise `TokenCaptureRetiredError`. It subclasses `TokenCaptureFrozenError` and so `RuntimeError`, so code that caught either still catches it. - `TokenCaptureStore.sweep_retired` and `TokenCaptureStore.sweep_stale` are removed. Nothing in Gym or NeMo RL called either. A caller that swept abandoned captures by age now retires the rollouts it abandoned. ## Issue No tracking issue. This aligns the complete-record store's retirement with the capture ledger's and closes its gaps for masked, failed, and abandoned captures and for fences. ## Validation - New tests in `tests/unit_tests/test_token_id_capture.py` cover retiring live, frozen, and never-captured rollouts (records removed; later writes, `begin_call`, `mark_incomplete`, and `freeze` all raise `TokenCaptureRetiredError`; a repeat is a no-op), a late call after retire dropped by the sink without a capture failure or an incomplete marker, one directory fsync per retire batch and per `drop`, delete removing the fence so the ID can be reused, batch validation with bare-string rejection, every non-empty delete batch syncing, files removed before a later removal failed still being synced, a batch failing part way still syncing what it changed, a retire failing part way leaving the rollout fenced and a retry finishing it, an overlapping retire returning only after the fence is durable, a sync failure after an error not hiding that error, two directory syncs per retire batch and per `drop`, a retire that cannot write its fence changing nothing, and late calls without token ids or with a failed build not reported as failures. Existing tests that used `sweep_retired` or single-ID `delete` now use `delete_now`, and the `sweep_stale` tests are removed with it. - New conformance checks `unconditional_retirement` and `delete_clears_retirement` pass for the file store and the in-memory reference backend, and reject a backend that lets a late `mark_incomplete` through after retire, rejects late writes with a plain or merely frozen error, or whose `freeze` fails after retire. - The new tests fail when the retire step in `retire_now`, the fence removal in `delete_now`, the retired error type, or the per-batch fsync is reverted. - Measured with the token-capture metrics from NVIDIA-NeMo#4218: 50 late calls to retired rollouts were counted as 50 `capture_failed{write}` and 50 `marked_incomplete` before the error-type change, and as 50 `late_write_dropped` after it. Retiring a rollout takes about 330 µs on a local disk, down from about 420 µs, and its system calls drop from three fsyncs to one. - The combined tree (`main` with this PR, NVIDIA-NeMo#3937, NVIDIA-NeMo#3938, NVIDIA-NeMo#4218, and NVIDIA-NeMo#4268): `pytest tests/unit_tests` passed. - `pytest` on every token-capture, staging, trajectory-builder, rollout-collection, and model-server unit test file: passed. - `pytest responses_api_models/vllm_model/tests/test_token_capture_integration.py responses_api_models/vllm_model/tests/test_external_capture_streaming.py`: passed. - `pre-commit run --files <changed files>`: all hooks passed. ## Rollout evidence N/A: the delivery path still retires through the same `drop`, and the new operations run only when a caller invokes them, so rollouts behave as before. --------- Signed-off-by: Ananth Subramaniam <[email protected]>
What does this PR do?
A rollout whose request is cancelled between its first captured model call and the post-delivery retirement leaves an unretired capture behind: the token JSONL and its side files (state, intents, incomplete marker). The client-disconnect cancellation middleware does this to every rollout a harness ends mid-call, and
sweep_retireddeliberately skips unretired captures because a live rollout looks the same. Over a long training run these files accumulate without bound.TokenCaptureStore.sweep_stale(older_than_seconds)removes an unretired capture when every file of it is older than the cutoff. An in-flight rollout keeps appending to its records, so its newest file is always recent and it is never touched; callers pass a cutoff above the longest possible session. Retired tombstones remain the business ofsweep_retired. Nothing calls the new method on its own; a training integration schedules it.Issue
No issue filed; the growth was measured on a multi-day training run whose capture directory held thousands of unretired captures of cancelled rollouts.
Validation
tests/unit_tests/test_token_id_capture.pygainstest_token_store_sweeps_abandoned_unretired_captures: an old abandoned capture is removed whole (records, state, and lock), a capture with one recent file is kept, and a retired tombstone is left tosweep_retired, which still reclaims it.pytest tests/unit_tests, with the telemetry tests andtest_collect_downstream_junit.pyexcluded for missing local packages): 6229 passed, 27 skipped, 1 failed. The one failure istest_slurm_script.py::test_build_vllm_ray_serve_command_single_node_model_with_space_survives_quoting, which needs apipthat this machine's externally managed Python refuses; it fails identically onmain.ruff checkandruff format --checkon the changed files: clean.🤖 Generated with Claude Code
Maintainer validation
Reviewed stale-capture reclamation against the store locking and lifecycle contract. Fixed a race by retaining the lock inode during stale sweeps so waiting and new writers remain serialized. The patch preserves the intended retention policy.
Validation: 165 capture/conformance/golden-vector tests passed; full pre-commit suite and diff checks passed. The regression check fails with the original lock unlink and passes with the fix. The branch uses additive signed-off commits and a main merge. Model rollouts are not applicable to this filesystem cleanup; existing downstream sweep evidence is recorded above.