Skip to content

feat(simple_agent): continue an agent session across activations - #5465

Merged
ffrujeri merged 8 commits into
mainfrom
ffrujeri/simple-agent-session-continuation
Nov 23, 2026
Merged

ffrujeri merged 8 commits into
mainfrom
ffrujeri/simple-agent-session-continuation

Conversation

@ffrujeri

@ffrujeri ffrujeri commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

On main (the BaseEnvironmentServerWithTools stack, #5346 → #5917, has merged). This is the first PR of the BIRD-Interact stack: this PR → #5409 (BIRD-Interact). Its diff is simple_agent, plus a small nemo_gym/server_runtime change that lets a tool reply end the agent's activation (second section below).

What changed

simple_agent accepts a seed that asks for continuation (AgentSeedSessionRequest.continuation). Each activation of the session then runs the turn loop on the session's conversation plus its new input, and keeps the first activation's tools and instructions.

  • An activation without new input, or one that changes any setting of the first activation (tools, instructions, tool_choice, temperature, ...), gets 422; only input and metadata change per activation.
  • A retried activation gets its reply without another model call.
  • The close returns the trajectory of every activation, also without observability_enabled, because the seed confirms it. Turns and invocations carry their activation_id. Each invocation keeps only its activation's new input and output; each turn's question still holds the model-visible prefix, as in one activation. A failed or cancelled activation adds a failed or incomplete invocation and an agent_activation_interrupted gap.
  • The session keeps the model server's cookies across activations, so it stays on one model-server session.
  • Only a class that sets continuation_supported in its own body supports continuation, so a subclass that replaces the turn loop, such as DeepAgentsAgent, refuses a continuation seed.
  • max_steps bounds each activation; the Environment Server bounds the activations.
  • With partial-rollout checkpoints on, a continuation seed is accepted and its reply carries CHECKPOINT_RESTART_HEADER (now exported from nemo_gym.server_runtime): a checkpoint exports no conversation, so the episode starts over after a restore. A session restored without its conversation refuses its next activation with AgentSessionConflictError (409), as main's session contract requires since refactor(sessions): raise the session conflict errors for every 409 refusal #5691.

A tool reply can end the activation

  • ToolResult.ends_activation (nemo_gym/server_runtime/episode_tools.py): a BaseEnvironmentServerWithTools tool marks its reply as the end of the agent's activation. Over direct HTTP the reply carries X-NeMo-Gym-End-Activation: true (END_ACTIVATION_HEADER, next to TOOL_ERROR_HEADER). MCP has no field for it, so an MCP agent calls the model again; the Fern MCP guide says so.
  • simple_agent runs the rest of the model response's tool calls and returns the activation without calling the model again.

Why

An Environment Server that simulates the user activates the agent once for each turn and sends only the new user message. BIRD-Interact's c-Interact (#5409) needs it: each user reply is a new activation of the same session. #5202 prototyped it. No public issue: the PRs of the BIRD-Interact stack track the work.

The BIRD-Interact ADK ends the agent's run right after submit_sql: its before_model_callback answers the next model call itself. An Environment Server sees only tool calls, so without a signal simple_agent made one more model call per turn, which the ADK does not. Rewards were the same, but token counts and trajectories were not, and both are compared with auto-ontology-eval and used for training. #5478 uses the signal.

Validation

Command Result
pytest responses_api_agents/simple_agent/tests/test_app.py tests/unit_tests/test_agent_sessions.py 115 passed
pytest responses_api_agents/simple_agent/tests (review fixes) 169 passed, including a test for each fix and one that activation 2's model request carries activation 1's prompt_token_ids, generation_token_ids and generation_log_probs
The same tests with each fix undone (11 mutations) Each fails at least one test
agentic_turn_simulation_agent and labbench2_vlm_agent tests (SimpleAgent subclasses) 93 and 4 passed
pytest tests/unit_tests/server_runtime (after adding ends_activation) 2107 passed; new: only a result marked ends_activation carries the header, a call that never ran does not
pytest responses_api_agents/simple_agent/tests (after adding ends_activation) 183 passed; new: a reply with the header ends the activation after the response's other tool calls, with one model call. It fails without the change
pre-commit run --files on the changed files Passed

Rollout evidence

The c-Interact smoke run in #5409 uses this continuation: 10 rollouts with simple_agent, none failed. That run is from before the rebase onto main at dda609f86 and the split into two stacks, which changed no behavior here.

For ends_activation: the BIRD-Interact server's five example tasks in c-Interact (SQLite, Docker sandboxes, with #5478's use of the signal), before and after. Before, every turn made one model call after submit_sql: 8 extra calls over 8 turns. After, none: each turn made exactly one model call fewer, and all 5 episodes completed. Details in #5478.

Compatibility

Opt-in through the seed. An agent seeded without continuation behaves as before. ends_activation defaults to False, so other tools' replies do not change; an agent that ignores the header calls the model again, as before. SimpleAgent subclasses no longer accept continuation seeds unless they set continuation_supported.

Team review fixes (2026-11-19)

A team review (the repository's review-pr-team skill) found notes only. Fixed in c3167e7:

  • A continuation activation now goes through _create_session_episode, as an activation without continuation does, so a subclass's task context applies on every activation (new test).
  • The trajectory comment said the close payload grows linearly; each turn's question still holds the model-visible prefix.
  • The continuation test checks each activation's model route and its own metadata (each fails when its line is removed).
  • integrate-existing-agents.mdx describes simple_agent's continuation; multi-step-tasks.mdx says which harness can continue in a lent sandbox.

Not changed: #6162 adds the same model_server_cookies field with another type; whichever lands second reconciles them.

Core coverage fix (2026-11-21)

Core coverage measures responses_api_agents/simple_agent/app.py, because core tests import it, but counts only core tests. This PR's continuation code is tested in the server's suite, so the stack's core coverage was 94.95%, under the 95% gate. 2160aa8 adds tests/unit_tests/server_runtime/test_environment_server_with_simple_agent.py: a BaseEnvironmentServerWithTools episode drives a real Simple Agent continuation session. A tool that ends the activation ends the turn with no further model call, the next activation sends the conversation so far, a failed activation stays in the trajectory, and an activation with no new input or changed settings gets a 422 before any model call. Each test fails when the rule it covers is removed. Core suite at the top of the stack: 26,020 passed, coverage 95.09%.

Review fixes, third round (2026-11-21)

01d21ec applies ananthsub's third round:

  • Ending the activation:

    • A tool result that is not JSON no longer ends the activation.
    • The end-activation docs say that only simple_agent honors the header.
  • Session history: with execute_tools on, a function call cut off before its tools ran is not sent to the next activation.

  • New simple_agent tests:

    • close during an activation (incomplete, CancelledError);
    • an activation without new input;
    • the model server's cookie between activations;
    • tool cookies and evidence across activations.

    Each test fails when its rule is removed. simple_agent: 192 passed.

@ffrujeri ffrujeri added feature New capabilities, enhancements, or enablement work area:agent Agent harnesses and Responses API agent behavior labels Nov 2, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 2, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ffrujeri

ffrujeri commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test be98a71

@ffrujeri

ffrujeri commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 371e2bd

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test c725911

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test 94ff125

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test c3167e7

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test b709095

ananthsub
ananthsub previously approved these changes Nov 20, 2026
Comment thread nemo_gym/server_runtime/tool_access.py Resolved
@ananthsub

Copy link
Copy Markdown
Contributor

The inline comments are notes. One point needs the description instead: rollout evidence at this head.

The c-Interact smoke run (10 rollouts through #5409) predates the rebase. It also predates 7d17f1a and b709095, which changed what a continuation session does:

  • the trajectory is always collected;
  • model-server cookies carry across activations;
  • a changed setting gets 422;
  • a failed or cancelled activation is recorded;
  • each activation runs through the session hook.

Could you add a short c-Interact smoke run with #5409 rebased on this head? It should check that the close returns one invocation per activation and that the model server keeps one session for the episode.

Two description fixes:

  • This diff does not export CHECKPOINT_RESTART_HEADER from nemo_gym.server_runtime.
  • The session test file is tests/unit_tests/agents/test_agent_sessions.py.

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44831686166

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test 7886722

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test 2160aa8

@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test 01d21ec

ffrujeri and others added 8 commits November 23, 2026 03:28
A user-simulation Environment Server activates the agent once for each
turn of the conversation and sends only the new user message. Simple
Agent now accepts a seed that asks for continuation: each activation
runs the turn loop on the session's conversation plus its new input,
and keeps the first activation's tools and instructions.

- An activation without new input, or one that replaces the session's
  tools or instructions, gets 422.
- A retried activation gets its reply without another model call.
- The close returns the trajectory of every activation.
- A continuation seed is refused when partial-rollout checkpoints are
  on, because a checkpoint exports no conversation history.

Carried over from #5202, which prototyped it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…able and explicit

Review fixes for simple_agent's continuation sessions:

- Checkpointing: a continuation seed is accepted, and its reply carries
  CHECKPOINT_RESTART_HEADER, because a checkpoint does not export the
  conversation; the episode starts over after a restore. A session
  restored without its conversation refuses its next activation (409).
- Opt-in: only a class that sets continuation_supported in its own body
  supports continuation, so DeepAgentsAgent, which replaces responses(),
  refuses a continuation seed again.
- Settings: a later activation that changes any setting of the first
  (tool_choice, temperature, ...), not only tools or instructions, gets
  422 instead of having it silently dropped.
- Observations: a continuation session always collects the trajectory and
  timing its seed confirms, also without observability_enabled. A failed
  or cancelled activation adds a failed or incomplete invocation and an
  agent_activation_interrupted gap before it re-raises. Turns carry their
  activation_id. Each invocation keeps only its activation's new input
  and output, so the close payload grows linearly.
- Model server: the session keeps the model server's cookies across
  activations, so it stays on one model-server session.
- A test checks that a later activation resends the earlier turns' token
  IDs and log probabilities.

max_steps still bounds each activation, as it bounds one /v1/responses
call; the Environment Server bounds the activations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…base hook

Main checks that no agent overrides seed_agent_session. Simple Agent now
implements the base agent's _seed_restarts hook instead: under
checkpointing, a checkpoint does not export a continuation session's
conversation, so the seed reply marks the episode as a restart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
A protocol whose turn ends at a submission, such as the BIRD-Interact ADK's
c-Interact, must end the agent's activation without another model call. The
ADK does it in a before-model callback; a Gym Environment Server sees only
the tool calls, so it signals it on the reply.

ToolResult.ends_activation sets X-NeMo-Gym-End-Activation: true on the direct
HTTP reply. simple_agent runs the rest of the model response's tool calls and
returns the activation without calling the model again. MCP has no field for
it, so an MCP agent calls the model again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…ntinuation lost its conversation

Main now requires every 409 session refusal in an agent to raise AgentSessionConflictError
(#5691, enforced by tests/unit_tests/test_session_contract_repo.py). The continuation session's
refusal after a restore raised a plain HTTPException(409); it keeps the same status and detail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…ook, and document continuation

A continuation activation called _create_episode directly, so a subclass whose
task context lives in its session state (_create_session_episode) lost it from
the first activation on. It now goes through the hook, as an activation
without continuation does.

The comment on the trajectory said the close payload grows linearly; each
turn's question still holds the model-visible prefix, as in one activation.
The continuation test now checks each activation's model route and metadata.
integrate-existing-agents.mdx describes simple_agent's continuation, and
multi-step-tasks.mdx says which harness can continue in a lent sandbox.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…ent Server in the core suite

Core coverage measures responses_api_agents/simple_agent/app.py, because core tests import it, but counts only
core tests. The continuation code this PR adds is tested in the server's own suite, so core coverage fell to
94.95%, under the 95% gate. These tests drive a real Simple Agent session from a BaseEnvironmentServerWithTools
episode: a tool that ends the activation ends the turn, the next activation continues the conversation, a failed
activation stays in the trajectory, and an activation the session cannot run gets a 422 before any model call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
- A tool result that is not JSON becomes a 500 error that no longer ends the activation, so the agent shows the
  model the error.
- The end-activation docstrings and the MCP tutorial say that only simple_agent honors the header.
- With execute_tools on, a function call cut off before its tools ran is not sent to the next activation.
- Comments say what a failed activation keeps and that each turn's question keeps the whole prefix; the docs say
  close returns every activation's trajectory and use "borrowed sandbox".
- Tests: close during an activation (incomplete, CancelledError), an activation without new input, the model
  server's cookie between activations, tool cookies and evidence across activations, the cut-off call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Felipe Vieira Frujeri <[email protected]>
@ffrujeri

Copy link
Copy Markdown
Contributor Author

/ok to test f4f827c

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/45121219212

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/45123313803

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/45125290520

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 needs-review PR is ready for code review and waiting on a reviewer ready-to-merge PR is approved, current, and waiting for final CI or merge risk:high Autobot PR risk: high sla:review-overdue Review response is over the one-business-day SLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants