Skip to content

feat(agents): deliver per-task agent context to agent sessions - #5703

Merged
yaoyu-33 merged 25 commits into
mainfrom
yuya/agent-context-sessions
Nov 13, 2026
Merged

yaoyu-33 merged 25 commits into
mainfrom
yuya/agent-context-sessions

Conversation

@yaoyu-33

@yaoyu-33 yaoyu-33 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Agent sessions have model input and tool/sandbox grants, but no explicit channel for task data used by the harness itself. PDF and vision agents need document or media references; a staged agent may need a formatting prompt for a later model call. This PR adds AgentSeedSessionRequest.agent_context for that data.

A row's context is keyed by participant role. An episode can seed several agents (UserSim seeds a user and an assistant), so a task row's agent_context maps each role to the context that role's session receives. Each Environment Server names its roles, refuses a role it does not have, and sends each role's context in that session's seed. The seed contract stays singular: one AgentContext per session.

flowchart TD
    T["Task row: agent_context by role"] --> E[Environment Server]
    E -->|"role A's context"| A[Agent session A]
    E -->|"role B's context"| B[Agent session B]
Loading

single_agent_turn has one role, agent:

"agent_context": {"agent": {"values": {"tag": "figqa2"}, "artifacts": {"media": {"path": "figs/1"}}}}

Its task-input model lifts the field out of task_data, refuses any other role (including the unkeyed {"values": ..., "artifacts": ...} shape) with 422, and forwards the agent context to the agent seed. Initial messages stay in responses_create_params.input; the Resources Server receives task data without agent context. Environments that send no context (Harbor, UserSim, SWE-Together) refuse the key.

Contract

  • AgentContext.values carries small inline JSON fields. artifacts carries file/directory references relative to an agent-configured root; file contents do not travel in the seed. Each role's context is limited to 64 KiB of UTF-8 JSON, and a key cannot appear in both maps. AgentContext and its artifacts are frozen.
  • An agent declares its accepted fields with agent_context_model and reads the typed value with parse_agent_context(). Seeding returns terminal HTTP 422 for undeclared, missing, mistyped, or incorrectly channelled context. Artifact fields use artifacts; other fields use values. Parsing copies mutable inline input so it cannot rewrite the saved seed.
  • Artifact resolution rejects absolute/traversal/NUL paths, paths escaping the configured root through a symlink, missing or wrong-kind paths, and terminal lookup errors. Other I/O errors remain retryable. Agents resolve context while building session state, including checkpoint restore, and clean up staged content on close or retirement.
  • SimpleAgent._create_session_episode() lets a subclass use session context when preparing a model request while retaining the existing agent loop.
  • Harbor rejects supplied row-level agent_context, at the top level or inside task_data, before sandbox or agent setup. UserSim and SWE-Together already reject unsupported context through their input schemas; this PR records that behavior in tests. Environments must consume supplied context explicitly or reject it.

agent_context remains excluded from Resources Server TaskData schema projection. This does not declare support in every environment: validation leaves the source row unchanged, collation preserves context, and the destination environment validates its own input. A regression test covers that distinction through actual collation and Harbor validation.

Compatibility and consumers

No separate public issue: this PR establishes the session contract used by those focused follow-ups.

Validation

Conflict resolution (39da43a03511064edf4f2e21a4f6600ee14bf35e, 2026-11-12, workstation ws)

Merged main at 1f88c43d55b61143378b0823ad9ce45d85cd4f16. The sole textual conflict was in SWE-Together tests; the resolution preserves both agent-context rejection coverage and main's skip-token-capture activation coverage.

  • Exact source tree verified before testing; core, agent, environment, checkpoint, and test imports resolved to the isolated snapshot with Python 3.13.14 and OpenAI 2.45.0.
  • 2,881 passed, zero failures or skips, including the checkpoint worker test that failed the previous CI run's timing assertion.
  • python -m pre_commit run --all-files and git diff --check passed. Hooks left the source unchanged.
  • No new model rollout: this conflict repair only reconciles existing tests; model evidence below remains tied to its recorded revisions. Workstation checks do not replace CI on the new head.
python -m pytest \
  tests/unit_tests/server_runtime/test_agent_context.py \
  tests/unit_tests/agents/test_agent_context.py \
  tests/unit_tests/agents/test_agent_sessions.py \
  tests/unit_tests/agents/test_base_responses_api_agent.py \
  tests/unit_tests/data/test_task_data.py \
  tests/unit_tests/eval/test_rollout_reverification.py \
  tests/unit_tests/server_runtime/test_checkpoint_identity.py \
  tests/unit_tests/server_runtime/test_checkpoint_participant_workers.py \
  responses_api_agents/simple_agent/tests/test_app.py \
  environment_servers/single_agent_turn/tests \
  environment_servers/single_agent_turn_legacy/tests \
  environment_servers/harbor/tests/test_app.py \
  environment_servers/swe_together/tests/test_environment.py \
  tests/unit_tests/test_public_api.py \
  tests/unit_tests/test_public_api_facades.py \
  tests/unit_tests/test_import_linter_coverage.py \
  tests/unit_tests/test_orchestration_boundary.py \
  --import-mode=importlib -q -o addopts= -o log_cli=false \
  --junitxml="$task_root/evidence/tests.xml"

Main checkpoint integration (dc4c9647f36b709a3fb7b6e8ae2175219420d09e, 2026-11-12, workstation ws)

  • Integrated main's release-workflow update and borrowed-sandbox checkpoint protocol. Context parsing still runs before seed restart/rebind decisions, and context survives export, restore, and reseeding. The restore regression now supplies the protocol's required checkpoint ID.
  • Fresh locked Python 3.13.14 environment; core, agent, both single-agent environment modules, and shared test helpers were verified to import from the assigned source.
  • Eight focused context/session/checkpoint/Simple Agent/single-agent environment suites initially produced 522 passes and 12 stale digest expectations. An untouched-main comparison under the same dependencies proved identical serialization; the changed OpenAI SDK default caused the old hashes.
  • The test-only correction retains all 12 absent/empty-context cases and checks the full pre-context shape. The affected file passed all 46 tests. Together with 488 passing cases in unchanged files, this covers all 534 selected cases; it is not a second full-suite run.
  • pre-commit run --all-files passed on the final tree, with no generated edits. Independent source review found no remaining blocker.
  • No new model rollout was performed for this integration repair; the earlier model evidence below remains historical. The earlier 181-test monitor run had mixed import provenance and is superseded by this fresh-environment validation.

At 79b7c92c1 (role-keyed agent_context, with aeee565f9 merged), on a Linux workstation with Python 3.13.14: the suites listed below plus tests/unit_tests/data/tasks/harbor, tests/unit_tests/test_moved_paths.py and tests/unit_tests/test_public_api.py: 2,980 passed, 35 failed, 31 errors. The same 66 tests fail the same way at ccd50dc04, the previous head (2,966 passed there), from that workstation's environment: no jinja2, no harbor.models, a git without init -b, and no git checkout for repository data discovery. TestRepoDataMatchesSchemas passes in a git checkout of #5704 and #5706, which contain this head. pre-commit run --files <changed files>: clean. New tests: test_single_agent_turn_refuses_a_role_it_does_not_have (the unkeyed shape and unknown roles, at the top level and inside task_data), test_the_size_limit_applies_to_each_roles_context, and digest pins for {"agent": null} and {"agent": {}}.

At 12eb72992: main f636596db (documentation only) merged into 79b7c92c1. Its guide edit conflicted with the agent_context mention in the seed-request paragraph; the resolution keeps both. test_fern_docs_links.py: 29 passed.

Earlier, at ccd50dc04: executed on the Linux workstation with Python 3.13.14 against the source published as ccd50dc046212c3116147b110bb8baa2b2477f8f. Imports were checked to resolve to that source, using a task-specific interpreter; shared environments were not modified.

2,409 tests passed across the context contracts, agent sessions, task-data validation, reverification, Simple Agent, affected environments, and public API checks.

python -m pytest \
  tests/unit_tests/server_runtime/test_agent_context.py \
  tests/unit_tests/agents/test_agent_context.py \
  tests/unit_tests/agents/test_agent_sessions.py \
  tests/unit_tests/data/test_task_data.py \
  tests/unit_tests/eval/test_rollout_reverification.py \
  responses_api_agents/simple_agent/tests/test_app.py \
  environment_servers/single_agent_turn/tests/test_app.py \
  environment_servers/single_agent_turn_legacy/tests/test_app.py \
  environment_servers/harbor/tests/test_app.py \
  environment_servers/swe_together/tests/test_environment.py \
  tests/unit_tests/test_public_api.py \
  tests/unit_tests/test_public_api_facades.py \
  tests/unit_tests/test_import_linter_coverage.py \
  --import-mode=importlib -q -o addopts= -o log_cli=false
  • pre-commit run --all-files: passed, including import-linter.
  • cd fern && npm run check -- --warnings: zero errors; authenticated missing-redirect check skipped because Fern credentials were unavailable.
  • git diff --check: passed.
  • Removing the Harbor rejection produced eight regression failures; the final implementation passes all eight. The validated source was restored and its patch hash checked afterward.
  • Independent source review found no remaining blocker in the context-scoping update.

Model smoke

Real inference through Gym's stock vllm_model adapter against nvidia/nemotron-3.5-super-vl-120b-a12b, with the stock Simple Agent loop and example_single_tool_call weather verifier. A task-only Simple Agent subclass declared a city value plus an instruction-file artifact; a recording resources subclass retained the original tool and verifier behavior.

Case Context Observed tool argument Reward
Tokyo Inline city + artifact get_weather(city="Tokyo") 1
Sydney Inline city + artifact get_weather(city="Sydney") 1
Paris None; stock Simple Agent get_weather(city="Paris") 1

The two context rollouts ran concurrently with distinct session IDs. The stock agent refused undeclared context with HTTP 422. All six resource seed/verify requests excluded agent_context, and all six server processes exited cleanly. Inputs, outputs, model-call capture, seed/resource records, configuration, and cleanup receipts were retained with the workstation evidence. This verifies the shared contract with a real model; downstream PDF/image benchmarks retain their own rollout requirements.

Role-keyed rows, real model (2026-11-09, at this head through #5704 and #5706). The shipped labbench2_vlm and gdp_pdf configs (single_agent_turn_legacy) ran their example rows written as {"agent": {...}} with hosted nvidia/nemotron-3.5-super-vl-120b-a12b as policy and judge. Every policy request (4 labbench2 rows, 3 GDP.pdf documents with 10, 4 and 5 page images) had the same SHA-256 as the session run of the unkeyed rows earlier that day, so keying by role changes nothing the model sees. A row in the unkeyed shape was refused by the Environment Server with 422 at task.task_input.agent_context, with no model call, and the refusal message reached the failures file. Details are in #5704 and #5706.

🤖 Generated with Claude Code

yaoyu-33 and others added 2 commits November 5, 2026 14:23
An agent session gets its initial model input and its grants, but no task
data. Harnesses that build part of their input from the task row, such as
gdp_pdf_agent's document manifest, labbench2_vlm_agent's media directory
and CritPt's formatting prompt, had no supported way to receive it under
single_agent_turn.

A task row now carries that data in a reserved agent_context key: small
inline values (at most 64 KiB) and artifact references, paths relative to
a root the agent configures. SingleAgentTurnTaskInput lifts it out of
task_data, so the Resources Server never receives it, and single_agent_turn
passes it in AgentSeedSessionRequest.agent_context, omitted when empty.

Each agent declares the keys it reads in agent_context_model. The base
seed refuses with a terminal 422 a context the agent declares no model
for, an undeclared key, a missing required key or a mistyped value, so a
task never runs without data it was given. Agents read the typed context
with parse_agent_context() and resolve artifacts with
resolve_agent_context_artifact(), which refuses paths that leave the root.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33 yaoyu-33 added feature New capabilities, enhancements, or enablement work area:agent Agent harnesses and Responses API agent behavior area:core Shared APIs, servers, telemetry, health, and registries labels Nov 5, 2026
@copy-pr-bot

copy-pr-bot Bot commented Nov 5, 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.

@yaoyu-33

yaoyu-33 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test c7831a5

…idation

A server's TaskData schema validates the task-owned fields of a row and reports unknown keys. agent_context goes to the agent session, never to the Resources Server, so it joins RESERVED_ROW_KEYS instead of showing up as an unknown task field.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test fb7fa47

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

…tate

An agent whose task context lives in its session state, such as a
document or media to put in the model request, had to override
responses() to reach that state from the episode it creates. Environment
validation then infers custom-gym-agent-loop for it, though the agent loop
is still Simple Agent's.

responses() now creates a session activation's episode through
_create_session_episode(state, body, **kwargs), which by default calls
_create_episode. A subclass overrides it to use the session's context and
leaves responses() and the agent loop alone. The sessionless path is
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 48257f7

yaoyu-33 and others added 4 commits November 6, 2026 11:14
…erifier

- Refuse an artifact path with a NUL byte at validation, and turn a path the filesystem cannot look up (name
  too long, no permission) into a 422 seed refusal instead of a 500; other OS errors stay retryable.
- Count the 64 KiB values limit in UTF-8 bytes, not ASCII-escaped JSON.
- Treat an empty agent_context as none in the task input and the seed, so the seed and task digest match a row
  without the key and an agent server that predates the key still accepts the seed.
- Skip a task_data container's agent_context in dataset validation, as the task input lifts it out.
- single_agent_turn_legacy: answer a row that fails the episode request model with the typed /run's 422.
- Re-verification no longer sends a stored agent_context to the Resources Server's /verify.
- Docs and helper docstring: a Simple Agent subclass resolves context in _new_session_state (restores rebuild
  through it) and overrides _create_session_episode, never responses(); configure the real directory as the
  root; files under a directory artifact are the agent's to confine.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
@yaoyu-33

yaoyu-33 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 62d6961

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test dee2b2c

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 08032a7

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 7b3a26f

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test a8f97fb

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 3be1f4c

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test dc4c964

@yaoyu-33

Copy link
Copy Markdown
Contributor Author

/ok to test 39da43a

@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/43466167688

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 area:core Shared APIs, servers, telemetry, health, and registries 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.

5 participants