Skip to content

feat(agent): return token capture produced outside the model server from an agent session - #4278

Draft
ananthsub wants to merge 2 commits into
mainfrom
ananthsub/agent-token-capture
Draft

ananthsub wants to merge 2 commits into
mainfrom
ananthsub/agent-token-capture

Conversation

@ananthsub

Copy link
Copy Markdown
Contributor

What changed and why

An agent session can now hand back token-level training data that something other than Gym's model server captured, for example a component that records the harness's model calls next to the task sandbox. Before this change there was no return path for such a capture: the agent session close response had no field for it, and rollout collection rejected any _ng_* key in an environment server's typed result, so an episode carrying capture health failed outright.

The data flows back through the agent session close response and lands at the top level of the collected rollout row:

flowchart LR
    A["Agent session closes"] -->|"AgentCloseSessionResponse.token_capture"| B["single_agent_turn environment server"]
    B -->|"atif_trajectories, _ng_token_capture, mask_sample when masked"| C["Episode result"]
    C -->|"_ng_token_capture accepted"| D["Rollout collection row"]
    C -->|"flat-row wrapper"| E["single_agent_turn_legacy row"]
Loading
  • nemo_gym/base_responses_api_agent.py: a new TokenCapture model holds the captured ATIF trajectories (atif_trajectories), the capture health and metrics (metrics), and a masked flag with a required mask_reason. TokenCapture.result_fields() returns the fields to merge into an episode result: atif_trajectories, _ng_token_capture (the metrics, plus the reason under error when masked, the key Gym already uses for capture failures), and mask_sample only when masked. It reuses TOKEN_CAPTURE_KEY and MASK_SAMPLE_KEY from nemo_gym/token_id_capture/delivery.py, so the row looks the same as one whose capture Gym's model server built. AgentCloseSessionResponse gains an optional token_capture: TokenCapture | None field; existing agents that omit it are unaffected.
  • environment_servers/single_agent_turn/app.py: after the agent session closes and verification succeeds, the environment server merges token_capture.result_fields() into the episode result. The reward is never changed, and an unmasked capture never clears a mask that the verifier set. The single_agent_turn_legacy flat-row wrapper keeps these fields at the top level of the row through its existing _legacy_result flattening.
  • nemo_gym/rollout_collection.py: _is_collector_key now allows exactly _ng_token_capture in an environment server's typed result. Identity, failure, and assembled observation keys (_ng_rollout_id, _ng_task_id, _ng_failure_*, ng_trajectory, ng_model_call_capture, ng_perf) stay reserved for rollout collection. The fix was first written by yaoyu-33, who is credited as co-author on that commit.

Linked issue

None. This is a small generic interface addition that lets an agent session return a capture it produced itself; a companion capture component builds on it separately.

Validation

Tests added:

  • tests/unit_tests/test_base_responses_api_agent.py: TokenCapture.result_fields() for a usable and a masked capture (including that building the result does not mutate the capture's metrics), rejection of a masked capture without a reason, a reason without a mask, and the old token_capture field name, and AgentCloseSessionResponse with and without a capture.
  • environment_servers/single_agent_turn/tests/test_app.py: through fake resources and agent servers, typed single_agent_turn requests and the single_agent_turn_legacy flat-row wrapper both carry the capture at the top level of the result; the mask is set only by a masked capture or by the verifier, the reward stays unchanged, and an episode without a capture gains no capture fields.
  • tests/unit_tests/test_rollout_collection.py: an environment server result carrying _ng_token_capture from a usable or masked capture becomes a rollout record with its trajectories, health, and mask; ng_trajectory, ng_model_call_capture, _ng_rollout_id, _ng_task_id, and _ng_failure_class are still rejected.

The new single_agent_turn and rollout collection tests fail when the corresponding source change is reverted.

Commands:

pytest environment_servers/single_agent_turn/tests -q
pytest environment_servers/single_agent_turn_legacy/tests -q
pytest tests/unit_tests/test_rollout_collection.py tests/unit_tests/test_base_responses_api_agent.py -q
pytest tests/unit_tests/test_agent_sessions.py tests/unit_tests/test_agent_session_retries.py tests/unit_tests/test_session_contract_repo.py -q
pytest tests/unit_tests -q -n 8
pre-commit run --files environment_servers/single_agent_turn/app.py environment_servers/single_agent_turn/tests/test_app.py nemo_gym/base_responses_api_agent.py nemo_gym/rollout_collection.py tests/unit_tests/test_base_responses_api_agent.py tests/unit_tests/test_rollout_collection.py

The full tests/unit_tests run shows the same set of failures with and without this change; those failures come from optional dependencies missing in the local environment.

Rollouts

N/A. This is a core interface addition with no producer in this repository yet: no existing agent sets token_capture, so current rollouts are unchanged. The behavior is covered end to end through the environment server with fake resources and agent servers, and through rollout collection's record assembly.

Compatibility

Backward compatible. token_capture is optional on AgentCloseSessionResponse, and episodes without it produce the same results as before. Environment server results may now contain _ng_token_capture; all other _ng_* keys are still rejected.

ananthsub and others added 2 commits October 8, 2026 22:58
An agent session can now hand back token-level training data that was captured outside Gym's model server, for example by a component that records the harness's model calls next to the task sandbox. `AgentCloseSessionResponse` gains an optional `token_capture` field holding a `TokenCapture`: the captured ATIF trajectories, capture metrics, and a masked flag with its reason.

After the agent session closes, the single_agent_turn environment server adds the capture to the episode result: `atif_trajectories` and `_ng_token_capture` at the top level, and `mask_sample` only when the capture is masked. The reward is unchanged and an unmasked capture never clears a mask the verifier set. The single_agent_turn_legacy flat-row wrapper keeps these fields at the top level of the row.

Signed-off-by: Ananth Subramaniam <[email protected]>
Rollout collection rejected every `_ng_*` key in an environment server's typed result, so any SingleAgentTurnRequest episode that carried `_ng_token_capture` from a capture outside Gym's model server failed. Allow exactly that key; identity, failure, and assembled observation keys remain reserved for rollout collection.

Co-authored-by: yaoyu-33 <[email protected]>
Signed-off-by: Ananth Subramaniam <[email protected]>
@nemo-automation-bot
nemo-automation-bot Bot requested review from a team October 9, 2026 05:59
@yaoyu-33 yaoyu-33 added area:core Shared APIs, servers, telemetry, health, and registries complexity:high Cross-component or shared-contract change with a large review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer labels Oct 9, 2026
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Oct 12, 2026
@yaoyu-33
yaoyu-33 marked this pull request as draft November 18, 2026 01:57
@yaoyu-33

Copy link
Copy Markdown
Contributor

We’re doing some cleanup of the open PR list. This PR has had no activity for over a month, so I’ve moved it back to draft.

If this change is still needed, please resolve any merge conflicts and mark the PR ready for review again when it’s ready to merge. Thanks!

@copy-pr-bot

copy-pr-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@github-actions github-actions Bot removed the sla:review-overdue Review response is over the one-business-day SLA label Nov 18, 2026

This branch was previously deployed

1 inactive deployment
public — e0bd582b Deployed Oct 9, 2026 by copy-pr-bot[bot] via release / finalize / notify #4826
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 complexity:high Cross-component or shared-contract change with a large review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants