Skip to content

feat(pi): capture sandboxed Pi's model calls with a sandbox session capture - #4280

Draft
ananthsub wants to merge 9 commits into
mainfrom
ananthsub/pi-session-capture
Draft

ananthsub wants to merge 9 commits into
mainfrom
ananthsub/pi-session-capture

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

Pi sandbox sessions can now run a configured sandbox session capture: a component in the task sandbox, such as a proxy next to the harness, that records every model call Pi makes and returns those calls when the session closes. This is the first agent adoption of the sandbox session capture hooks in #4279.

With a sandbox_session_capture block on the Pi agent config, each session's SandboxSession builds its own capture and starts it at seed, after the Pi runtime and sandbox_runner.py are staged and before the activation writes Pi's provider config. The activation takes its base URL from model_endpoint(...): the capture's endpoint when one is running, otherwise the Gym model server with the rollout prefix, exactly as before. When the capture supplies a model name, Pi uses it for the nemo provider entry in models.json, for --model, and for the response's model. The session close returns the capture as token_capture on AgentCloseSessionResponse.

A config that sets both model_server and sandbox_session_capture fails validation at load with a message that says to set model_server: null, because each one alone decides where the sandboxed calls go. The existing seed check that rejects a session without a model endpoint now accepts a sandbox session capture in place of a model server. The local /v1/responses and /run paths refuse with a 422 when sandbox_session_capture is set, as the Codex agent does, because they call the model directly and the capture would record nothing.

Calls that a capture serves are not attributed to a Gym model server. Pi already omits model call references when no model_server is configured and records a model_call_ownership_unavailable gap, so the observations follow that existing convention unchanged.

When the capture did not start, Pi does not run. The activation returns harness_not_run_response(...), a failed response with an empty assistant message and the capture's reason; the observations hold one harness_not_run gap with the same reason; and close returns the masked token_capture. The environment server therefore still verifies and closes the episode instead of failing it for a capture problem.

flowchart TD
    A["Seed: install Pi runtime and runner"] --> B{"sandbox_session_capture configured?"}
    B -->|no| D["Activation: Gym model server endpoint"]
    B -->|yes| C["start_session_capture()"]
    C -->|started| E["Activation: capture endpoint and model"]
    C -->|did not start| F["Activation: harness_not_run_response, no Pi run"]
    D --> G["Close: token_capture is null"]
    E --> H["Close: collected token_capture"]
    F --> I["Close: masked token_capture"]
Loading

Without sandbox_session_capture nothing changes: the sandbox path resolves the same model server URL through model_endpoint(...), and the local compatibility path keeps _resolve_model_base_url and _build_models_config, which other harness wrappers patch. The nemo provider entry is now built by a shared _nemo_provider helper used by both paths.

Dependencies

Depends on #4279 (sandbox session capture lifecycle in SandboxSession and the harness-not-run helpers), and transitively on #4276 (SimpleResponsesAPIAgent.model_endpoint and ModelEndpoint) and #4278 (TokenCapture and AgentCloseSessionResponse.token_capture). Review only the top commit until those merge.

Linked issue

None; this is the Pi adoption of the sandbox session capture interface introduced in #4279.

Validation

  • NEMO_GYM_EXTRA_ROOTS=$PWD PYTHONPATH=$PWD python -m pytest responses_api_agents/pi_agent responses_api_agents/pi_sandboxed_agent tests/unit_tests/test_sandbox_session.py tests/unit_tests/test_base_responses_api_agent.py tests/unit_tests/test_harness_conformance_ci.py -q -p no:cacheprovider
  • The only failures are the three tests in responses_api_agents/pi_agent/tests/test_outcome.py that need the pinned Pi CLI installed locally; they fail identically on the base branch without this change.
  • New tests in responses_api_agents/pi_agent/tests/test_sandbox.py, using a fake SandboxSessionCapture selected by import path:
    • the config rejects model_server together with sandbox_session_capture and accepts either alone;
    • with sandbox_session_capture set, the local /v1/responses and /run paths answer 422 before creating an episode, starting the capture, or launching Pi;
    • a session with a capture writes the capture's endpoint and model (or the configured model when the capture names none) into models.json, --model, and the response, does not resolve a Gym model server URL, records no model call references, and returns the collected token_capture on close;
    • a capture that fails to start answers the activation with a failed empty response without launching Pi or staging its input, records the harness_not_run gap, aborts the capture, and returns the masked token_capture on close;
    • the existing borrowed-sandbox flow now also asserts that close returns no token_capture without a capture.
  • Each new test fails when the corresponding behavior is reverted (no capture start at seed, no harness-not-run branch, no token_capture on close, configured model on --model).
  • pre-commit run --files on the changed files.

Rollouts

N/A. The change wires an optional configuration hook; without sandbox_session_capture the sandbox and local paths produce the same provider config, command, and observations as before, which the existing Pi sandbox tests cover. A capture implementation is not part of this repository, so a smoke rollout with a real capture belongs with that implementation.

Compatibility

No change for existing configs. sandbox_session_capture defaults to null. Configs that set it must also set model_server: null. A config with sandbox_session_capture serves only agent sessions; its local paths refuse requests. The response's model reports the capture's model name when a capture supplies one.

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]>
Pi sandbox sessions can now run a configured session capture, a component
in the task sandbox that records the harness's model calls. With a
session_capture block, the session's SandboxSession starts the capture at
seed, after the Pi runtime and runner are staged, so its endpoint is known
when the activation writes Pi's provider config. The activation then takes
its base URL from model_endpoint (the capture's endpoint, else the Gym
model server with the rollout prefix) and uses the capture's model name
for Pi's provider entry, its --model flag, and the response when the
capture supplies one. The session close returns the capture as
token_capture.

A config that sets both model_server and session_capture is rejected when
it loads, since either one alone decides where sandboxed calls go; set
model_server: null to use the capture. The seed check that requires a
model endpoint now accepts a session capture in place of a model server.
Calls a capture serves are not attributed to a Gym model server, so the
observations keep their existing model_call_ownership_unavailable gap.

When the capture did not start, Pi does not run: the activation returns a
failed response with an empty assistant message, the observations record
a harness_not_run gap, and close returns the masked capture, so the
episode is still verified. Without session_capture nothing changes; the
local compatibility path keeps its own provider configuration.

Signed-off-by: Ananth Subramaniam <[email protected]>
Only sandbox sessions run the capture. The local /v1/responses and /run
paths called the model directly and recorded nothing, so a misrouted
request produced an uncaptured rollout. They now fail with 422 before
any work, matching the Codex agent.

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

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

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub force-pushed the ananthsub/pi-session-capture branch from 2c43fa5 to 143129b Compare October 9, 2026 13:20
@ananthsub ananthsub changed the title feat(pi): capture sandboxed Pi's model calls with a session capture feat(pi): capture sandboxed Pi's model calls with a 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