Repository navigation
feat(environment): add BaseEnvironmentServerWithTools, which serves an episode's tools - #5347
Conversation
|
/ok to test 7db0586 |
7db0586 to
fb8001f
Compare
|
/ok to test fb8001f |
fb8001f to
cabd187
Compare
|
/ok to test cabd187 |
cabd187 to
24cd72d
Compare
|
/ok to test 24cd72d |
|
/ok to test 474c002 |
|
/review reset=True |
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
SHIP WITH CARE
I found four RISK correctness issues in failure propagation and resend handling, plus a required rollout-evidence gap. The earlier fixes for stale grants, cancellation records, rollout context and keyword-only methods are present in the current source.
RISK — Provide representative rollout evidence for the changed agent path
The PR reports unit tests and transport experiments but marks real rollouts N/A. The changed Simple Agent execution path now sends call IDs and changes tool-failure accounting. The trusted quality bar requires representative real model smoke rollouts for behavior-changing agent work; unit tests alone don't satisfy it.
Could you run a minimal environment subclass with Simple Agent and a real model, inspect tool execution and the final episode result, and record the command/config and artifact evidence? Acceptance should demonstrate a state-changing tool call and final answer, plus a tool-supplied error result recorded as failed/tool_error while its output still reaches the model. Preserve the Resources-backed and legacy paths. This is missing validation evidence, not a claim that a rollout failed.
NOTE — Document the self-contained Environment Server path
The previous bot review's documentation finding remains unresolved. The architecture guide still says Environment Servers don't implement tools or scoring and that environment-specific tools belong to Resources Servers. That contradicts the new public base class.
Could you describe this additional authoring path and add a minimal example showing TypedToolset, serve_tools, the returned agent-session grant, and ToolSpec.function_tool() declarations supplied during activation? Preserve the existing Resources-backed and legacy workflows. State the current single-worker, restart-only and direct-HTTP limits. Static acceptance: the example advertises the tool's schema, uses the returned grant for execution, registers cleanup, and doesn't imply MCP support in this PR. This is an authoring-documentation suggestion, not a runtime failure.
I inspected all nine changed files and traced their directly affected callers, contracts, tests and failure paths. All three delegated inspections reached terminal status; one report wasn't delivered, so I inspected its typed-tool, export, layering and documentation topic directly. This was a source-only review: I didn't execute tests or independently verify the reported validation results. All acceptance checks below are proposed, not executed.
Review generated by codex. This was a full review. Request a full review with /review reset=true.
|
/ok to test 33e4afd |
|
/ok to test 1010c87 |
|
/ok to test 3caf30c |
|
/ok to test 7fcdd65 |
|
/ok to test ea50d56 |
…n episode's tools BaseEnvironmentServerWithTools is the base class for an Environment Server that serves each episode's tools itself and owns the episode's state and verification, with no Resources Server: run() holds the episode's state, and serve_tools() grants the agent the episode's tools until the episode's cleanup runs. - EpisodeToolset: the tools of one episode, as specs plus a call method. TypedToolset builds one from @episode_tool methods with a Pydantic model of arguments; a toolset can also come from the task. - Tool calls go to POST /<tool name> at the server's root, as on a Resources Server, with a signed episode token (EpisodeTokens) in a header, and under a rollout prefix as well. - Calls to one episode run one at a time and are recorded in order. - A call after the episode ends gets 410; a missing or foreign token 401, another grant's nonce 403, an unknown tool 404, arguments the tool rejects 422, and a tool that raises 500. - Until session routing covers these servers, a server refuses num_workers > 1, because a tool call can reach a worker that does not hold the episode. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felipe Vieira Frujeri <[email protected]>
Review of #5347: - A request with an episode token reaches only the episode's tools. A model's call named run (or ../run, which the HTTP client resolves to /run) got the server's /run and could start another episode. - A call that waited for its body or for the calls before it rechecks that its grant is still live, so it cannot run after the episode ends. - When the episode ends, a call still running is cancelled and gets 410, so a late call cannot act on a stopped sandbox. - A call cancelled by its client is recorded, because the tool may already have changed state. - Each call runs under the episode's rollout, whatever its path, so a tool's own model or Resources Server calls carry the episode's capture key. - A call to a tool the episode does not have, and an output that is not JSON (NaN, Infinity), are recorded as errors. - Tokens are signed with the per-run secret (episode_token_secret). - A subclass that sets checkpoint_mode = "exported" is refused: the agent's grant names the attempt that a restore replaces. - @episode_tool refuses a method that is not async, and TypedToolset one whose arguments model is keyword-only. TypedToolset builds the specs of each class once. - ToolSpec requires an object schema, and function_tool() declares the tool to the model with its full schema, $defs included. - The docstring says that a subclass route needs more than one path segment, because the tool route takes every one-segment POST path. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felipe Vieira Frujeri <[email protected]>
Over MCP, a tool's error result carries `isError`. Over direct HTTP, the same result was a plain 200, so simple_agent recorded the call as completed, and tool-error counts in the trajectory depended on the transport. - A direct HTTP tool route answers a call whose result is an error with `X-NeMo-Gym-Tool-Error: true` (TOOL_ERROR_HEADER, next to DirectHTTPToolAccess). BaseEnvironmentServerWithTools sets it on every call it records as an error. - simple_agent records a call whose reply has the header as failed, with error_type "tool_error", as codex_agent does. A non-2xx status still gives http_<status>. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…tool call runs Calls to one episode run one at a time, so a hung call made the episode's next calls wait until the episode ended. A client that disconnects does not end its call either: uvicorn does not cancel the request, on direct HTTP or on MCP, with mcp 1.29, 1.30 or 2.3. - BaseEnvironmentServerWithToolsConfig, with an optional tool_call_timeout_seconds. A call that runs longer is cancelled, recorded as an error and answered 504, and the episode's next call runs. A TimeoutError that the tool raises itself is still a failure of the tool (500). - The test of a cancelled call is renamed: it cancels the request, which a disconnecting client does not do. No separate cap on concurrent tool calls: an episode runs one call at a time, so max_concurrent_episodes already bounds them. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felipe Vieira Frujeri <[email protected]>
…l without running it again A ToolCallRecord had no call ID, so the server's record could not be joined to the agent's trajectory. And simple_agent's HTTP client resends a POST whose connection dropped (_resend_after_disconnect defaults to true), while the server, which uvicorn does not cancel, may already have run it: a tool such as BIRD's submit could charge twice. - An agent may send the model's call_id in X-NeMo-Gym-Tool-Call-Id (TOOL_CALL_ID_HEADER, next to DirectHTTPToolAccess); ToolCallRecord stores it as call_id. simple_agent sends it on direct HTTP tool calls. - Calls to one episode run one at a time, so a resend waits behind the call it repeats. A call with the same ID, tool and arguments as the episode's last recorded call gets that call's reply, and the tool does not run again. A reused ID with other arguments, or not on the last call, runs, because a model can reuse IDs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felipe Vieira Frujeri <[email protected]>
A resend check keyed on the call ID could not tell a resent request from a new model decision with the same ID, tool and arguments, and so could skip a real call. A Resources Server runs every call, so this class does too. The call ID stays on each record, to join it to the agent's trajectory. The docstring now says that a disconnect cancels the call, as Gym's disconnect middleware does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Felipe Vieira Frujeri <[email protected]>
|
/ok to test b1ae342 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/43936967937 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/43943700912 |
Stacked on #5346 (episode tokens). Review that one first; this PR's diff against it is the base class and the toolset.
What changed
BaseEnvironmentServerWithTools(nemo_gym/server_runtime/base_environment_server.py, next toBaseEnvironmentServer) is a base class for an Environment Server that serves each episode's tools itself, so it needs no Resources Server. Other Environment Servers grant the agent a Resources Server's tools; this one owns the tools, the episode's state and verification. It does not impose turns: a subclass runs its episodes as it likes, and the user-simulation layer adds the turn loop on top.BaseEnvironmentServerdoes not change.EpisodeToolset(nemo_gym/server_runtime/episode_tools.py): the tools of one episode, asspecs()andcall(name, arguments).TypedToolsetbuilds one from@episode_toolmethods that take a Pydantic model of arguments; the docstring is the description and the model's JSON schema the parameters. A toolset can also come from the task, asnemo_user_sim's probe tools will.serve_tools(request, toolset, cleanup)grants the toolset until the episode's cleanup runs. It returns aDirectHTTPToolAccessthat points at this server, with a signed episode token (feat(environment): sign episode tokens for Environment Server tool grants #5346) in its headers, and the list that records each call.POST /<tool name>at the server's root, as on a Resources Server, sosimple_agentcalls them unchanged, also under a/ng-rollout/<id>/prefix. A tool cannot take the name of one of the server's routes, such asrun.BaseEnvironmentServerWithToolsConfig(exported fromnemo_gym.server_runtime), with an optionaltool_call_timeout_seconds. A call that runs longer is cancelled, recorded as an error and answered 504, and the episode's next call runs. ATimeoutErrorthat the tool raises itself is still a failure of the tool (500). Calls to one episode run one at a time, so without a timeout a hung call makes the episode's next calls wait until the episode ends. A client that disconnects cancels its call, which is recorded:run_webserverinstalls Gym's disconnect middleware.call_idinX-NeMo-Gym-Tool-Call-Id(TOOL_CALL_ID_HEADER), andToolCallRecord.call_idstores it, so the record joins the agent's trajectory;simple_agentsends it. Every call runs, as on a Resources Server: a model can give two calls the same ID, tool and arguments, so the server cannot tell a resend from a new call. A client that resends a call after a dropped connection runs it again, as it does against a Resources Server.ToolResult(is_error=True)on a 200, carriesX-NeMo-Gym-Tool-Error: true(TOOL_ERROR_HEADER, next toDirectHTTPToolAccess). It is the direct HTTP form of MCP'sisError.simple_agentrecords such a call asfailedwitherror_type="tool_error", ascodex_agentdoes, so tool-error counts do not depend on the transport.nemo_gym.server_runtimeexportsBaseEnvironmentServerWithTools,ServedToolsand the toolset names (EpisodeToolset,TypedToolset,episode_tool,ToolSpec,ToolResult,ToolCallRecord,ToolArgumentsError), next toBaseEnvironmentServer, because a server outsidenemo_gym/imports from the package, not from its submodules.ToolCallRecord: name, arguments, output, error flag, duration).ToolResult(is_error=True)for an error the agent should handle)X-NeMo-Gym-Tool-Error: truefor an error resulttool_call_timeout_secondsWhy
This is the core abstraction for environments that drop their Resources Server: BIRD-Interact first, then
nemo_user_sim(Linear GYMINT-640; the user-simulation RFC's layer 1,BaseEnvironmentServerWithTools). Per review, it is a new base class rather than a change toBaseEnvironmentServer, so existing Environment Servers are untouched. It was briefly namedBaseSelfContainedEnvironmentServer, thenBaseTurnBasedEnvironmentServer; it is now named for what it adds, since it does not impose turns.Interim limit: until session routing covers these servers (the next PR in the stack), a
BaseEnvironmentServerWithToolsrefusesnum_workers > 1. With several workers, a tool call can reach a worker that does not hold the episode. Single-worker is not the target; the routing PR removes the limit.Validation
pytest tests/unit_tests/server_runtime/test_base_environment_server_with_tools.py tests/unit_tests/server_runtime/test_episode_tools.py tests/unit_tests/server_runtime/test_environment_server.py tests/unit_tests/test_public_api.py tests/unit_tests/test_import_linter_coverage.py(after the rebase onto main atdda609f86)pytest tests/unit_tests/server_runtime/test_base_environment_server_with_tools.py tests/unit_tests/server_runtime/test_episode_tools.pyhttpx.ASGITransport) whilerun()is in progress: calls change the episode's state and are recorded in order; a call after the episode ends gets 410; five token cases (missing, garbage, another server's, a guessed nonce, another episode's); unknown tool, rejected arguments, a non-object body and a raising tool; a rollout-prefixed call; two concurrent episodes with separate tokens; conflicting toolsets fail the episode; one grant per episode;num_workers=2refused.test_calls_to_one_episode_run_one_at_a_timefails (three calls ran at once), so the test checks the lockpytest tests/unit_tests/server_runtime/test_environment_server.py tests/unit_tests/server_runtime/test_episode_tokens.pypytest tests/unit_tests/server_runtime/test_base_environment_server_with_tools.py tests/unit_tests/test_public_api.py(tool-error header)test_a_reply_says_whether_the_call_was_an_error: a result has no header; an error result and a rejected call have itpytest responses_api_agents/simple_agent/tests/test_app.py, in the server's venv (tool-error header)test_agent_session_uses_seeded_direct_http_tool_access[error-result]records a 200 with the header asfailed,tool_errorsimple_agentnot reading itsimple_agenterror-result case failspytest tests/unit_tests/server_runtime/ tests/unit_tests/agents/ tests/unit_tests/test_public_api.py tests/unit_tests/test_import_linter_coverage.py tests/unit_tests/test_moved_paths.py(tool-call timeout)test_a_call_that_runs_too_long_is_cancelled_and_frees_the_episode(504, recorded, the next call runs) andtest_a_timeout_the_tool_raises_itself_is_a_failure_of_the_tool(500)ClientDisconnectCancellationMiddleware: a client starts a slow call, then drops the connectionpytest tests/unit_tests/server_runtime/ tests/unit_tests/agents/ ...andsimple_agent's tests (call IDs)test_a_call_records_the_id_the_agent_sent;test_a_call_that_repeats_the_last_one_runs_again(two calls with the same ID, tool and arguments: totals 2 then 4, two records)simple_agentnot sending the headerpre-commit runon the changed filesRollout evidence
N/A: a base class that no environment uses yet. BIRD-Interact's move onto it (Linear GYMINT-646) will run rollouts with
simple_agentand Codex.Compatibility
BaseEnvironmentServerand every existing server are unchanged.BaseEnvironmentServerWithToolssubclasses take the newBaseEnvironmentServerWithToolsConfig.simple_agentnow records a direct HTTP tool call whose reply hasX-NeMo-Gym-Tool-Error: trueas failed; no server set that header before, so existing runs record the same.Next in the stack
MCP exposure of an episode's toolset, multi-worker session routing on the episode token, and running open episodes' cleanups at shutdown.