Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions responses_api_agents/anyswe_agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ agent_kwargs:
terminal_backend: local
```

With training-token capture (`token_id_capture.enabled: true` run-wide and
`token_id_capture: true` on the agent entry), the model URL handed to the
harness carries the `/ng-rollout/<id>/training-token-capture` prefix that the
model server's capture middleware keys on. The in-sandbox runner turns capture
on for the inner agent, so it returns a transcript that terminal attribution can
join to a captured call, and the outer response keeps the inner agent's response
id.

For large runs, bake `/agent_deps_mount/bin/python`, NeMo Gym, and the selected
agent into each task image. `agent_runtime_source` controls other delivery modes:

Expand Down
21 changes: 18 additions & 3 deletions responses_api_agents/anyswe_agent/agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ def _extract_patch(repo: Path, index_path: Path, baseline_tree: str) -> str:
).stdout


def _runner_global_config(model_url: str, server_name: str) -> dict:
"""Global config for the inner agent, which has no Gym config of its own."""
from nemo_gym.config_types import TOKEN_CAPTURE_PATH_SEGMENT
from nemo_gym.global_config import TOKEN_ID_CAPTURE_BLOCK

if not model_url:
return {}
global_config = {server_name: {"responses_api_models": {"model": {"host": "0.0.0.0", "port": 0}}}}
# The outer agent hands over a model URL that already carries the rollout prefix. When that is the
# training-token-capture prefix, the inner agent must know capture is on so it emits a transcript that
# terminal attribution can join to a captured call (claude_code_agent: separate reasoning items and the
# served response id).
if f"/{TOKEN_CAPTURE_PATH_SEGMENT}" in model_url:
global_config[TOKEN_ID_CAPTURE_BLOCK] = {"enabled": True, "all_agents": True}
return global_config


def main() -> None:
model_url = os.environ.get("NGSWE_MODEL_URL", "")
model_name = os.environ["NGSWE_MODEL_NAME"]
Expand All @@ -95,9 +112,7 @@ def main() -> None:
config_class = getattr(module, os.environ["NGSWE_AGENT_CONFIG_CLASS"])

server_name = "policy_model"
global_config = (
{server_name: {"responses_api_models": {"model": {"host": "0.0.0.0", "port": 0}}}} if model_url else {}
)
global_config = _runner_global_config(model_url, server_name)
client = ServerClient.model_construct(global_config_dict=global_config)
client._build_server_base_url = lambda config: model_url
config_sampling = {key: value for key, value in sampling.items() if key in config_class.model_fields}
Expand Down
22 changes: 18 additions & 4 deletions responses_api_agents/anyswe_agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,20 @@ def update_metrics(metrics_fpath: Path, update_dict: Dict[str, Any]) -> None:
metrics_fpath.write_text(json.dumps(existing | update))


def _model_url_for_rollout(model_url: str, rollout_id: Optional[str]) -> str:
return apply_rollout_prefix(model_url, rollout_id) if model_url and rollout_id else model_url
def _model_url_for_rollout(model_url: str, rollout_id: Optional[str], *, token_capture: bool = False) -> str:
# ``token_capture`` selects the ``/ng-rollout/<id>/training-token-capture`` prefix that the model
# server's capture middleware keys on. With the plain eval prefix nothing is captured for the rollout.
if not (model_url and rollout_id):
return model_url
return apply_rollout_prefix(model_url, rollout_id, token_capture=token_capture)


def _outer_response_id(instance_id: str, inner: Optional[NeMoGymResponse], *, token_capture: bool) -> str:
# Under training-token capture the inner agent names its response with the last served model-call id
# (claude_code_agent does). Terminal attribution joins the transcript to a captured call by that id.
if token_capture and inner is not None and inner.id:
return str(inner.id)
return f"anyswe-{instance_id}"


def _safe_config_json(params: "AnySweInstanceConfig", indent: Optional[int] = None) -> str:
Expand Down Expand Up @@ -626,7 +638,7 @@ async def _run_agent_in_sandbox(self, params: AnySweInstanceConfig) -> NeMoGymRe
)

return NeMoGymResponse(
id=f"anyswe-{params.instance_id}",
id=_outer_response_id(params.instance_id, saved, token_capture=self._token_id_capture_enabled()),
Comment thread
linj-glitch marked this conversation as resolved.
Resolved
created_at=int(time.time()),
model=params.body.model,
object="response",
Expand All @@ -653,7 +665,9 @@ def _setup_params(
persistent_dir.mkdir(parents=True, exist_ok=True)
server_config = self._server.model_dump()
if not self.config.sandbox_model_base_url:
server_config["model_server_url"] = _model_url_for_rollout(server_config["model_server_url"], rollout_id)
server_config["model_server_url"] = _model_url_for_rollout(
server_config["model_server_url"], rollout_id, token_capture=self._token_id_capture_enabled()
)
params = AnySweInstanceConfig(
**self.config.model_dump(),
**server_config,
Expand Down
74 changes: 74 additions & 0 deletions responses_api_agents/anyswe_agent/tests/test_token_capture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Training-token capture wiring for anyswe_agent and its in-sandbox runner."""

from pathlib import Path
from types import SimpleNamespace

import pytest

from nemo_gym.openai_utils import NeMoGymResponse, NeMoGymResponseCreateParamsNonStreaming
from responses_api_agents.anyswe_agent.agent_runner import _runner_global_config
from responses_api_agents.anyswe_agent.app import (
AnySweAgent,
AnySweServerConfig,
_model_url_for_rollout,
_outer_response_id,
)
from responses_api_agents.anyswe_agent.tests.test_app import _config


def test_model_url_carries_the_training_capture_prefix() -> None:
assert (
_model_url_for_rollout("http://model-host:8000", "3-1", token_capture=True)
== "http://model-host:8000/ng-rollout/3-1/training-token-capture"
)
assert _model_url_for_rollout("http://model-host:8000", "3-1") == "http://model-host:8000/ng-rollout/3-1"
assert _model_url_for_rollout("", "3-1", token_capture=True) == ""
assert _model_url_for_rollout("http://model-host:8000", None, token_capture=True) == "http://model-host:8000"


@pytest.mark.parametrize("capture", [False, True])
def test_setup_params_requests_the_capture_prefix_when_capture_is_on(tmp_path: Path, capture: bool) -> None:
server = AnySweServerConfig(
run_session_id="s",
base_results_dir=tmp_path,
model_server_url="http://policy:8000",
resolved_sandbox_provider={"opensandbox": {}},
sandbox_default_metadata={},
)
agent = AnySweAgent.__new__(AnySweAgent)
object.__setattr__(
agent,
"__dict__",
{
"config": _config(token_id_capture=True),
"server_client": SimpleNamespace(global_config_dict={"token_id_capture": {"enabled": capture}}),
},
)
object.__setattr__(agent, "__pydantic_fields_set__", set())
object.__setattr__(agent, "__pydantic_extra__", None)
object.__setattr__(agent, "__pydantic_private__", {"_server": server, "_sem": None})
body = NeMoGymResponseCreateParamsNonStreaming(input="fix it", metadata={"instance_id": "astropy__astropy-12907"})

params = agent._setup_params(body, rollout_id="7-2")

suffix = "/training-token-capture" if capture else ""
assert params.model_server_url == f"http://policy:8000/ng-rollout/7-2{suffix}"


def test_inner_agent_learns_that_token_capture_is_on_from_the_model_url() -> None:
assert _runner_global_config("", "policy_model") == {}
eval_config = _runner_global_config("http://policy:8000/ng-rollout/7-2", "policy_model")
assert "token_id_capture" not in eval_config
assert eval_config["policy_model"]["responses_api_models"]["model"]["port"] == 0
capture_config = _runner_global_config("http://policy:8000/ng-rollout/7-2/training-token-capture", "policy_model")
assert capture_config["token_id_capture"] == {"enabled": True, "all_agents": True}


def test_outer_response_keeps_the_inner_served_id_only_under_capture() -> None:
Comment thread
linj-glitch marked this conversation as resolved.
Resolved
inner = NeMoGymResponse.model_construct(id="resp_served123")
instance_id = "astropy__astropy-12907"
assert _outer_response_id(instance_id, inner, token_capture=True) == "resp_served123"
assert _outer_response_id(instance_id, inner, token_capture=False) == "anyswe-astropy__astropy-12907"
assert _outer_response_id(instance_id, None, token_capture=True) == "anyswe-astropy__astropy-12907"
Loading