Skip to content

feat(codex): run Codex sessions through an optional sandbox session capture - #4281

Draft
ananthsub wants to merge 8 commits into
mainfrom
ananthsub/codex-session-capture
Draft

ananthsub wants to merge 8 commits into
mainfrom
ananthsub/codex-session-capture

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Codex agent sessions can now send their model calls through a sandbox session capture: a component started inside the task sandbox, for example a proxy next to the harness, that serves the model endpoint and records every call with its token ids. Session close returns what it recorded, so a training run can use token-level data from the calls Codex actually made. Without the new setting, Codex behaves exactly as before.

  • Configuration: CodexAgentConfig.sandbox_session_capture (a SandboxSessionCaptureConfig) selects the capture implementation. A capture replaces the Gym model server for the session's calls, so a config that sets both model_server and sandbox_session_capture fails when it loads ("set model_server: null to use sandbox_session_capture"). The session seed still requires a sandbox-reachable model_server unless a capture is configured, and still rejects openai_base_url, cwd, and extra_config overrides.
  • Protocol: Codex calls the streaming Responses API (POST <base_url>/responses with wire_api = "responses"). The capture endpoint must serve that API; the requirement is documented on the config field and in the README rather than adapted in the agent.
  • Seed: each session's SandboxSession gets the capture config and builds its own capture instance. The seed awaits start_session_capture() after the Codex runtime is installed and keeps the returned ModelEndpoint on the session state.
  • Activation: the provider base_url in the generated Codex config now comes from SimpleResponsesAPIAgent.model_endpoint(...), which prefers the capture's endpoint over the model server. When the capture names a model, the Codex config, the request model check, and the response use that model; otherwise the existing model choice is kept. The legacy direct-agent path keeps its URL choice through the same helper (model server, then openai_base_url, then the OpenAI API) and rejects sandbox_session_capture, because it runs Codex on the agent-server host where no capture runs.
  • Capture failure: when the capture does not start, Codex does not run. The activation returns harness_not_run_response(...) (a failed response with an empty assistant message) and records harness_not_run_observations(...), so the environment server still verifies and closes the episode instead of failing it.
  • Close: AgentCloseSessionResponse.token_capture carries session.token_capture(): the collected capture, a masked one when start or collection failed, or None without a capture.
  • Observations: Codex records no Gym model-server call references (its events have no model response IDs, already reported as the model_call_join_key_unavailable gap), so calls served by a capture need no change there.
flowchart TD
    A["Seed: install Codex runtime"] --> B["start_session_capture()"]
    B -->|"endpoint"| C["Activation: model_endpoint(session_endpoint=capture)"]
    C --> D["Write Codex config with the capture base_url and model"]
    D --> E["Run Codex in the task sandbox"]
    B -->|"did not start"| F["Activation: harness_not_run_response, harness_not_run gap"]
    E --> G["Close: collect capture, return token_capture"]
    F --> H["Close: return masked token_capture"]
Loading

Dependencies

Depends on #4279, and transitively on #4276 (model_endpoint, ModelEndpoint) and #4278 (TokenCapture, AgentCloseSessionResponse.token_capture). Review after those land; this branch is based on #4279.

Linked issue

None; this adopts the sandbox session capture interface from #4279 in the Codex agent.

Validation

New tests in responses_api_agents/codex_agent/tests/test_native_sessions.py use a fake SandboxSessionCapture selected by import path. They cover the config rejection of model_server with sandbox_session_capture; a session whose Codex config uses the capture's base URL and model (and the configured model when the capture names none) and whose close returns the collected token_capture; a capture that does not start, where Codex never launches, the activation returns a failed response, the harness_not_run gap is recorded, the episode is not failed, and close returns a masked token_capture; a session without a capture closing with no token_capture; and the legacy direct-agent path rejecting sandbox_session_capture. With the agent changes reverted, every new test except the no-capture case fails.

NEMO_GYM_EXTRA_ROOTS=$PWD PYTHONPATH=$PWD python -m pytest responses_api_agents/codex_agent/tests tests/unit_tests/test_sandbox_session.py tests/unit_tests/test_base_responses_api_agent.py tests/unit_tests/test_agent_session_retries.py tests/unit_tests/test_agent_sessions.py tests/unit_tests/test_session_contract_repo.py tests/unit_tests/test_harness_conformance_checks.py tests/unit_tests/test_harness_conformance_ci.py tests/unit_tests/test_add_legacy_agent_environment_servers.py -q
pre-commit run --files responses_api_agents/codex_agent/app.py responses_api_agents/codex_agent/sandbox.py responses_api_agents/codex_agent/README.md responses_api_agents/codex_agent/tests/test_native_sessions.py

Rollouts

N/A: no capture implementation ships in Gym, and without sandbox_session_capture the agent's behavior is unchanged (its existing session and legacy tests pass unmodified). Rollouts through a real capture belong with the change that adds an implementation.

Compatibility

No change for existing configs. sandbox_session_capture is a new optional field; _sandbox_input now takes the expected model as a keyword argument (internal).

ananthsub and others added 5 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]>
Agents that drive an external harness each decide on their own where the
harness sends model calls: a Gym model server with the rollout capture
prefix, or a directly configured OpenAI-compatible base URL. A later
session component that records model calls needs to substitute its own
endpoint, and agents should be able to run without a Gym model server
when another endpoint is supplied.

SimpleResponsesAPIAgent.model_endpoint now makes that choice in one
place. It returns a ModelEndpoint (base URL and an optional model name)
in this order: an endpoint the caller passes for the episode's session,
the model server through resolve_model_base_url (keeping the rollout
prefix and the training token capture route), then the agent's
configured base URL verbatim. It raises ValueError when none is set.
No agent is changed here; each adopts the helper in its own change.

Signed-off-by: Ananth Subramaniam <[email protected]>
…ifecycle

Some training setups record a harness's model calls, with their token ids, from a component that runs next to the harness inside the task sandbox, for example a proxy the harness calls instead of the model. SandboxSession, the execute, stop, collect and release lifecycle shared by the Codex, Hermes and Pi agents, now runs such a component when one is configured, so agents do not each implement its start, collection and cleanup ordering.

A capture implements the new SessionCapture interface: start returns the ModelEndpoint the harness must call, collect stops the component and returns a TokenCapture, and abort stops it without collecting. SessionCaptureConfig selects the implementation by import path with keyword options and a collect timeout; the class and its options are validated when the configuration loads, so a bad path, a class that is not a complete SessionCapture, or unknown options fail at agent startup.

The adapter awaits start_session_capture before it builds the harness command and passes the endpoint to model_endpoint. Close collects the capture after the harness has stopped and before the sandbox is released (an owned sandbox is stopped, a borrowed one is handed back), at most once, and token_capture returns it for the agent's close response. A borrowed session whose harness cleanup is not confirmed defers collection to the retried close, because the harness may still be calling the component. Capture problems never fail the episode: a start that fails or is cancelled, a collection that raises or exceeds its timeout, or a session closed before the capture ran produce a masked TokenCapture with the reason, and the component is aborted under a shield so a cancelled caller cannot interrupt it. When the capture did not start, execute refuses to run the harness and session_capture_failed tells the adapter to answer the activation without it.

Without a configured capture the lifecycle is unchanged. No agent opts in here; each does in its own change.

Signed-off-by: Ananth Subramaniam <[email protected]>
…ailing the episode

When a sandbox session's capture does not start, execute refuses to run the harness, but raising from the activation would fail the whole episode for a capture problem. harness_not_run_response gives every agent the same answer instead: a failed response with an empty assistant message, the request's model and tool fields, and the reason as its error message. harness_not_run_observations returns the matching observations, one harness_not_run gap whose detail is the reason, so the close response does not report an unrelated observation failure.

The environment server then still verifies and closes the episode, its result shows why the harness did not run, and the session's masked token capture keeps the sample out of training.

Signed-off-by: Ananth Subramaniam <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 9, 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.

The capture runs only inside a SandboxSession, and agents refuse it on
every other path. Naming it after the sandbox session says so where it
is configured and in its error messages. The module moves to
nemo_gym/agent_utils/sandbox_session_capture.py. SandboxSession keeps
its session_capture field and start_session_capture method, which are
already scoped by the class.

Signed-off-by: Ananth Subramaniam <[email protected]>
Codex agent sessions can now send their model calls through a session
capture: a component started in the task sandbox that serves the model
endpoint and records every call. The new session_capture config field
selects it, and model_server must then be null; a config naming both is
rejected when it loads, and the session seed accepts a missing model
server only when a capture is configured.

The session's SandboxSession builds its own capture instance and starts
it at seed, after the Codex runtime is installed, so its endpoint is
known when the activation writes the Codex config. The provider base URL
now comes from model_endpoint, which prefers the capture's endpoint over
the model server; when the capture names a model, the config and the
response use it. Codex calls the streaming Responses API, so the capture
endpoint must serve it. The host path keeps its URL choice through the
same helper and rejects session_capture, since it cannot capture calls.

When the capture does not start, Codex does not run: the activation
returns a failed response with an empty assistant message and records a
harness_not_run gap, so the episode is still verified. Session close
returns the capture, or the masked one, as token_capture. Without
session_capture nothing changes.

Signed-off-by: Ananth Subramaniam <[email protected]>
…capture

Follows the rename of SessionCapture to SandboxSessionCapture. The field
only takes effect in sandbox sessions, and Codex refuses it elsewhere, so
the name now says where it applies.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/codex-session-capture branch from 5242575 to 39fd081 Compare October 9, 2026 13:20
@ananthsub ananthsub changed the title feat(codex): run Codex sessions through an optional session capture feat(codex): run Codex sessions through an optional sandbox session capture Oct 9, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:agent Agent harnesses and Responses API agent behavior feature New capabilities, enhancements, or enablement work labels Oct 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent harnesses and Responses API agent behavior feature New capabilities, enhancements, or enablement work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants