Repository navigation
feat(token-capture): attribute terminals with corroborated item IDs - #3972
Conversation
…from the last model-authored item Terminal attribution joins independent witnesses to name the model call that produced the verified response. A harness that rebuilds its transcript from its own session files (OpenCode and Codex both do) never records the served response id, and when the session ends on a tool result the content witnesses abstain, so such a rollout was masked although every call was captured whole. The item-id witness reads the served id of the response's last model-authored item: a message's id or a tool call's call_id, which the model server assigned and the harness keeps per item. The id must be recorded on exactly one entry's output, and that item's content (the tool's name without any namespace prefix and its canonical arguments, or the message's content parts) must match, so a reused or colliding id cannot name a call whose output the transcript does not end with. Tool calls nested in a chat assistant message's tool_calls list are read as well. An item whose id no entry recorded abstains, and a disagreement with another witness still fails closed. NAMESPACE_TOOL_DELIMITER moves to token_id_capture.records because the witness strips a flattened tool's group name and the package must not import the server stack; responses_streaming imports it from there. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: alexandery <[email protected]>
Signed-off-by: Lin Jia <[email protected]>
linj-glitch
left a comment
There was a problem hiding this comment.
Reviewed the item-ID witness, content corroboration, witness disagreement, ambiguous identities, namespace handling, and dependency-free capture package against current main and recent attribution history. No blocking findings remain.
Validation: 404 tests passed across terminal attribution, trajectory building, token capture, and streaming. Full pre-commit suite and diff checks passed. Additive main merge preserves author history. No fresh model smoke was run locally; existing author-provided downstream harness evidence is retained.
|
/ok to test 375cd78 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/37279775241 |
…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]>
What does this PR do?
Adds a fifth witness to terminal attribution in
nemo_gym/token_id_capture/terminal.py: the item-id witness.Terminal attribution names the model call that produced the verified response by joining independent witnesses (explicit, response id, content). A harness that rebuilds its transcript from its own session files, which OpenCode and Codex both do, never records the served response id, and when the session ends on a tool result (the harness was killed while a tool ran) the content witnesses abstain because the transcript does not end on model output. Such a rollout was masked although every call was captured whole.
The item-id witness reads the served id of the response's last model-authored item: a message's
idor a tool call'scall_id, which the model server assigned and the harness keeps per item. The id must be recorded on exactly one entry's output, and that item's content must match (the tool's name without any namespace prefix plus its canonical arguments, or the message's content parts), so a reused or colliding id cannot name a call whose output the transcript does not end with. Tool calls nested in a chat assistant message'stool_callslist are read as well. An item whose id no entry recorded (a message the harness composed itself) abstains, a shared id abstains as ambiguous, and a disagreement with another witness still fails closed. The witness skips token-free custody rows, as the content witness does.NAMESPACE_TOOL_DELIMITERmoves fromnemo_gym/responses_streaming.pytonemo_gym/token_id_capture/records.py, because the witness strips a flattened tool's group name andtest_package_is_dependency_free_leafrequires the capture package not to import the server stack;responses_streamingimports the constant from there, so its value and users are unchanged.Issue
No issue filed; the masked rollouts were observed while training with OpenCode and Codex as harnesses under token-id capture.
Validation
PYTHONPATH=. python -m pytest -q tests/unit_tests/test_terminal_attribution.py tests/unit_tests/test_trajectory_builder.py tests/unit_tests/test_token_id_capture.py tests/unit_tests/test_responses_api_model_streaming.py: 402 passed, including nine new tests (a transcript ending in a tool result, a served message id corroborated by content, a harness-composed message, disagreement with the response id, chat-nested tool calls, a namespaced tool name, a shared id, a last item without an id, and an id whose content differs).pytest tests/unit_tests, with the telemetry tests andtest_collect_downstream_junit.pyexcluded for missing local packages): 6237 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.pre-commit run --files <changed files>: passed (ruff, ruff-format, end-of-file, trailing whitespace).item_idinstead of masking.🤖 Generated with Claude Code
Maintainer validation
Reviewed the item-ID witness, content corroboration, witness disagreement, ambiguous identities, namespace handling, and dependency-free capture package against current main and recent attribution history. No blocking findings remain.
Validation: 404 tests passed across terminal attribution, trajectory building, token capture, and streaming. Full pre-commit suite and diff checks passed. Additive main merge preserves author history. No fresh model smoke was run locally; existing author-provided downstream harness evidence is retained.