From 010c28bcfbe8d2f916af42c955dee6832e525be0 Mon Sep 17 00:00:00 2001 From: t Date: Thu, 12 Nov 2026 11:16:58 -0500 Subject: [PATCH 1/5] feat(prompt_agent): resume a cut-short user-sim turn as the user When a transient API error cuts a Claude Code turn short, the bridge resumes the session with a prompt that asks the model to finish the request it was given. A user-sim's request is the agent's reply, so a resumed user-sim carried on as the agent. The Claude Code agent now takes a resume_prompt, and prompt_agent sends a user-sim one that keeps it the user, negotiated against the agent's card like model and output_format. Co-Authored-By: Claude Opus 5.5 --- .../agents/agents/claude-code/a2a_server.py | 4 + .../agents/claude-code/claude_bridge.py | 6 +- .../task_step/task_steps/prompt_agent.py | 12 ++- .../agents/claude_code/test_card_contract.py | 2 +- .../claude_code/test_claude_bridge_retry.py | 12 +++ .../test_prompt_agent_user_sim_resume.py | 74 +++++++++++++++++++ 6 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 tst/unit/task_step/test_prompt_agent_user_sim_resume.py diff --git a/src/agent_env/agents/agents/claude-code/a2a_server.py b/src/agent_env/agents/agents/claude-code/a2a_server.py index 7dc27625e..5df5029ca 100644 --- a/src/agent_env/agents/agents/claude-code/a2a_server.py +++ b/src/agent_env/agents/agents/claude-code/a2a_server.py @@ -137,6 +137,9 @@ class ClaudeCodeConfig(AgentConfig): max_thinking_tokens: int | None = None # Which of the CLI's own tools it may use. With any selection, its MCP servers are only the ones agent-env gives it. builtin_tools: ClaudeCodeTools | None = None + # What a turn cut short by a transient API error is resumed with; unset, the bridge asks the model to finish the + # request it was given, which suits a task but not a simulated user. + resume_prompt: str | None = None def _rewrite_session_ids(jsonl_text: str, session_id: str) -> tuple[list[str], int]: @@ -492,6 +495,7 @@ async def _turn(self, request: TaskRequest[ClaudeCodeConfig], cfg: ClaudeCodeCon max_turns=cfg.max_turns, max_thinking_tokens=cfg.max_thinking_tokens, builtin_tools=cfg.builtin_tools, + resume_prompt=cfg.resume_prompt, event_callback=request.trajectory.append, ) except Exception as e: diff --git a/src/agent_env/agents/agents/claude-code/claude_bridge.py b/src/agent_env/agents/agents/claude-code/claude_bridge.py index cecd8286f..a17e3cc70 100644 --- a/src/agent_env/agents/agents/claude-code/claude_bridge.py +++ b/src/agent_env/agents/agents/claude-code/claude_bridge.py @@ -415,10 +415,12 @@ async def run_claude( max_thinking_tokens: int | None = None, builtin_tools: ClaudeCodeTools | None = None, event_callback: Callable[[dict], None] | None = None, + resume_prompt: str | None = None, ) -> ClaudeResult: """Run one turn, resuming the CLI session when a transient upstream failure cuts it short; a resume spends what is left of ``timeout_s``, so the turn never outlives its caller's deadline. ``CLAUDE_API_ERROR_RETRIES=0`` turns - resumes off. ``event_callback`` gets each stdout event as the CLI prints it; an attempt's trailing `result` is + resumes off. The resume is prompted with ``resume_prompt``, else ``_RESUME_PROMPT``, which asks the model to + finish the request it was given: a caller whose model isn't doing a task, such as a simulated user, sets its own. ``event_callback`` gets each stdout event as the CLI prints it; an attempt's trailing `result` is held until it's known not to be resumed.""" deadline = time.monotonic() + timeout_s retries_left = _configured_api_error_retries() @@ -468,7 +470,7 @@ async def run_claude( trajectory.extend(_without_terminal_result(result.trajectory)) first_failure = first_failure or result.error_detail retries_left -= 1 - attempt_prompt, attempt_resume, attempt_session = _RESUME_PROMPT, True, result.session_id + attempt_prompt, attempt_resume, attempt_session = resume_prompt or _RESUME_PROMPT, True, result.session_id backoff = _RETRY_BACKOFF_S[min(attempts - 1, len(_RETRY_BACKOFF_S) - 1)] logger.warning(f"claude CLI turn cut short ({_redact(failure)!r}); resuming session {attempt_session} " f"in {backoff}s with {int(budget_left)}s of the original {timeout_s}s budget left") diff --git a/src/agent_env/task_step/task_steps/prompt_agent.py b/src/agent_env/task_step/task_steps/prompt_agent.py index c11fb716b..9e8026628 100644 --- a/src/agent_env/task_step/task_steps/prompt_agent.py +++ b/src/agent_env/task_step/task_steps/prompt_agent.py @@ -115,6 +115,15 @@ def _file_uris(parts: list[dict]) -> list[str]: # The decide body that asks a user agent to record nothing when no agent trigger fires. _QUIET_DECIDE = ("turn", "solver_message", "context_id", "env_triggers", "record_unfired") +# What a user-sim turn cut short by a transient API error is resumed with, for an agent that takes `resume_prompt`. +# An agent's own resume prompt asks it to finish the request it was given; a user-sim's request is the agent's reply, +# so that prompt has it carry on as the agent. +USER_SIM_RESUME_PROMPT = ( + "Your previous turn was interrupted by a transient API error before you sent your message. You are still the " + "user. Write the user's next message, as you were about to; the assistant's last reply above is what you are " + "answering." +) + _DEFAULT_USER_SIM_OUTPUT_FORMAT: dict[str, Any] = { "type": "json_schema", "schema": { @@ -503,7 +512,8 @@ async def _converse( if is_user_sim: user_output_format = self.user_output_format or _DEFAULT_USER_SIM_OUTPUT_FORMAT user_model = self._resolve_user_model(context) - user_config = {k: v for k, v in {"output_format": user_output_format, "model": user_model}.items() if v is not None} + user_config = {k: v for k, v in {"output_format": user_output_format, "model": user_model, + "resume_prompt": USER_SIM_RESUME_PROMPT}.items() if v is not None} negotiated = A2AAgent.negotiate_agent_config(user_sim.a2a_card or {}, user_config) if negotiated: await protocol.post_agent_config(user_url + negotiated.endpoint, negotiated.fields) diff --git a/tst/unit/agents/claude_code/test_card_contract.py b/tst/unit/agents/claude_code/test_card_contract.py index 0ace58b4f..95b15f08e 100644 --- a/tst/unit/agents/claude_code/test_card_contract.py +++ b/tst/unit/agents/claude_code/test_card_contract.py @@ -68,7 +68,7 @@ def test_the_config_takes_every_field_prompt_agent_sends_that_the_cli_has(app): config = _extensions(app)["urn:agentenv:agent-config/v1"]["params"]["methods"] assert config["set"]["request"]["supported"] == [ "allowed_tools", "builtin_tools", "description", "effort", "max_thinking_tokens", "max_turns", "model", "name", - "output_format", "project_id", "role", "system_prompt", "task_id", "timeout_seconds", + "output_format", "project_id", "resume_prompt", "role", "system_prompt", "task_id", "timeout_seconds", ] assert set(config["set"]["request"]["schema"]["properties"]) == set(config["set"]["request"]["supported"]) assert config["get"] == {"method": "GET", "response": {"required": ["config"]}} diff --git a/tst/unit/agents/claude_code/test_claude_bridge_retry.py b/tst/unit/agents/claude_code/test_claude_bridge_retry.py index 35e1999ea..4ff6210f3 100644 --- a/tst/unit/agents/claude_code/test_claude_bridge_retry.py +++ b/tst/unit/agents/claude_code/test_claude_bridge_retry.py @@ -61,6 +61,18 @@ def _write_session(scripted, session_id="sess-1"): (scripted["session_dir"] / f"{session_id}.jsonl").write_text('{"type":"assistant"}\n') +def test_a_resume_prompt_given_replaces_the_default_one(scripted): + _write_session(scripted) + scripted["queue"].append((_api_error_stdout("API Error: The operation timed out.", tool_uses=1), 1)) + scripted["queue"].append((_ok_stdout("resumed"), 0)) + + result = asyncio.run(claude_bridge.run_claude(prompt="p", session_id="sess-1", resume=False, + resume_prompt="stay the user")) + + assert result.attempts == 2 + assert scripted["procs"][1].stdin.data == b"stay the user" + + def test_success_makes_exactly_one_invocation(scripted): scripted["queue"].append((_ok_stdout(), 0)) result = asyncio.run(claude_bridge.run_claude(prompt="p", session_id="sess-1", resume=False)) diff --git a/tst/unit/task_step/test_prompt_agent_user_sim_resume.py b/tst/unit/task_step/test_prompt_agent_user_sim_resume.py new file mode 100644 index 000000000..df1862746 --- /dev/null +++ b/tst/unit/task_step/test_prompt_agent_user_sim_resume.py @@ -0,0 +1,74 @@ +"""A user simulator is sent the resume prompt that keeps it the user when its card takes one: an agent resumes a +turn cut short by asking its model to finish the request, and a user simulator's request is the agent's reply.""" + +from unittest.mock import MagicMock + +import pytest +from a2a.types import TaskState + +from agent_env.a2a_agent import A2AAgent, protocol +from agent_env.task_step.context import DeployedAgent, TaskStepContext +from agent_env.task_step.task_steps import prompt_agent as pa_mod +from agent_env.task_step.task_steps.prompt_agent import USER_SIM_RESUME_PROMPT, PromptAgentTaskStep + + +def _card(*supported: str) -> dict: + return {"capabilities": {"extensions": [{"uri": A2AAgent.EXT_AGENT_CONFIG, "params": { + "endpoint": "/ext/agent-config", "methods": {"set": {"request": {"supported": list(supported)}}}}}]}} + + +@pytest.fixture +def posted(monkeypatch): + """Mocks the conversation's HTTP and store boundary, and collects each agent config posted, by URL.""" + cfg = MagicMock() + cfg.get_model_params.return_value = {} + cfg.get_model_for_role.return_value = None + monkeypatch.setattr(pa_mod, "get_config", lambda: cfg) + for fn in ("create_conversation", "add_a2a_task", "complete_a2a_task", "mark_closed", "get_conversation"): + monkeypatch.setattr(pa_mod.conversation_store, fn, lambda *a, **kw: None) + configs = {} + + async def post_agent_config(url, fields): + configs[url] = fields + + async def send(url, parts, message_id, context_id, timeout): + return f"task-{message_id}", None + + async def poll(url, task_id, timeout, interval, **_): + text = '{"message": "thanks", "done": true}' if "user-sim" in url else "done" + return {"status": {"state": TaskState.completed, "message": {"parts": [{"kind": "text", "text": text}]}}} + + monkeypatch.setattr(protocol, "post_agent_config", post_agent_config) + monkeypatch.setattr(pa_mod.protocol, "send_a2a_message", send) + monkeypatch.setattr(pa_mod.protocol, "poll_a2a_task", poll) + return configs + + +def _conversation(solver_card: dict, user_card: dict) -> TaskStepContext: + context = TaskStepContext(instance_id="ti-1") + context.deployed_agents.append(DeployedAgent(agent_name="solver", api_url="http://solver", + a2a_url="http://solver", a2a_card=solver_card)) + context.deployed_agents.append(DeployedAgent(agent_name="usersim", api_url="http://user-sim", + a2a_url="http://user-sim", a2a_card=user_card)) + return context + + +def _step() -> PromptAgentTaskStep: + return PromptAgentTaskStep(id="solve", version=None, prompt="hi", agent_name="solver", model="task-solver", + max_conversation_turns=2, user_agent_name="usersim", user_model="task-user") + + +@pytest.mark.asyncio +async def test_a_user_simulator_whose_card_takes_a_resume_prompt_is_sent_the_one_that_keeps_it_the_user(posted): + card = _card("model", "output_format", "resume_prompt") + await _step().execute(_conversation(card, card)) + + assert posted["http://user-sim/ext/agent-config"]["resume_prompt"] == USER_SIM_RESUME_PROMPT + assert "resume_prompt" not in posted["http://solver/ext/agent-config"] + + +@pytest.mark.asyncio +async def test_a_user_simulator_whose_card_takes_no_resume_prompt_is_sent_none(posted): + await _step().execute(_conversation(_card("model"), _card("model", "output_format"))) + + assert "resume_prompt" not in posted["http://user-sim/ext/agent-config"] From 13eee321227c8e9a1af5d9ed2f29ac28b95385b7 Mon Sep 17 00:00:00 2001 From: t Date: Mon, 16 Nov 2026 12:07:43 -0500 Subject: [PATCH 2/5] feat(user sim): the built-in user sim is resumed as the user too deploy_user_sim_agent sends USER_SIM_RESUME_PROMPT when the user's card takes resume_prompt, as prompt_agent does for a peer user sim; a card that takes none gets none. The prompt moves to user_sim_common, which both steps share. Co-Authored-By: Claude Opus 5.5 --- .../task_steps/agent/deploy_user_sim_agent.py | 5 ++++- .../task_steps/agent/user_sim_common.py | 11 +++++++++++ .../task_step/task_steps/prompt_agent.py | 11 +---------- .../task_step/test_deploy_user_sim_agent.py | 17 +++++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/agent_env/task_step/task_steps/agent/deploy_user_sim_agent.py b/src/agent_env/task_step/task_steps/agent/deploy_user_sim_agent.py index 5b222fbd8..eb741587c 100644 --- a/src/agent_env/task_step/task_steps/agent/deploy_user_sim_agent.py +++ b/src/agent_env/task_step/task_steps/agent/deploy_user_sim_agent.py @@ -78,6 +78,7 @@ from agent_env.entity.scopes import FieldScope from agent_env.task_step.context import TaskStepContext from agent_env.task_step.task_steps.agent import user_sim_versions +from agent_env.task_step.task_steps.agent.user_sim_common import USER_SIM_RESUME_PROMPT from agent_env.task_step.task_steps.deploy_agent import DeployAgentTaskStep logger = logging.getLogger(__name__) @@ -273,7 +274,9 @@ async def _set_user(self, context: TaskStepContext, agent, setup: dict) -> TaskS from agent_env.a2a_agent import A2AAgent model, source = self._user_model(context) - wanted = {"model": model, "output_format": setup.get("output_format")} + # The resume prompt keeps a turn cut short by an API error the user's (user_sim_common); a card that takes none + # gets none, and its agent resumes with its own. + wanted = {"model": model, "output_format": setup.get("output_format"), "resume_prompt": USER_SIM_RESUME_PROMPT} negotiated = A2AAgent.negotiate_agent_config(agent.a2a_card or {}, {k: v for k, v in wanted.items() if v}) if negotiated is None or "model" not in negotiated.fields: raise RuntimeError(f"{self.type} '{self.id}': agent '{self.agent_name}' takes no model through " diff --git a/src/agent_env/task_step/task_steps/agent/user_sim_common.py b/src/agent_env/task_step/task_steps/agent/user_sim_common.py index b44276c87..560ace89c 100644 --- a/src/agent_env/task_step/task_steps/agent/user_sim_common.py +++ b/src/agent_env/task_step/task_steps/agent/user_sim_common.py @@ -5,6 +5,17 @@ from typing import Optional +# What a user-sim turn cut short by a transient API error is resumed with, for an agent that takes `resume_prompt` +# (prompt_agent's peer user sim, and the user deploy_user_sim_agent deploys). +# An agent's own resume prompt asks it to finish the request it was given; a user-sim's request is the agent's reply, +# so that prompt has it carry on as the agent. +USER_SIM_RESUME_PROMPT = ( + "Your previous turn was interrupted by a transient API error before you sent your message. You are still the " + "user. Write the user's next message, as you were about to; the assistant's last reply above is what you are " + "answering." +) + + def person_of(us: dict) -> dict: """The user a deploy_user_sim_agent record describes, as the user sim's ``answer`` takes them; the caller adds the run's ``time_zone``, ``opening`` and ``done_ends_run``.""" diff --git a/src/agent_env/task_step/task_steps/prompt_agent.py b/src/agent_env/task_step/task_steps/prompt_agent.py index 7d972f10f..c1ecdc184 100644 --- a/src/agent_env/task_step/task_steps/prompt_agent.py +++ b/src/agent_env/task_step/task_steps/prompt_agent.py @@ -42,7 +42,7 @@ from agent_env.task_step.task_steps.notification_delivery import delivered_parts, next_message, take_due from agent_env.task_step.task_steps.post_turn_hooks import PostTurnHooks, run_post_turn_hooks, validate_hook_specs from agent_env.task_step.task_steps.agent import user_sim_versions -from agent_env.task_step.task_steps.agent.user_sim_common import iso, person_of, task_zone, utc +from agent_env.task_step.task_steps.agent.user_sim_common import USER_SIM_RESUME_PROMPT, iso, person_of, task_zone, utc from agent_env.task_step.task_steps.sandbox_utils.sandbox_utils import ( agent_error_text, fetch_container_logs, @@ -117,15 +117,6 @@ def _file_uris(parts: list[dict]) -> list[str]: # The decide body that asks a user agent to record nothing when no agent trigger fires. _QUIET_DECIDE = ("turn", "solver_message", "context_id", "env_triggers", "record_unfired") -# What a user-sim turn cut short by a transient API error is resumed with, for an agent that takes `resume_prompt`. -# An agent's own resume prompt asks it to finish the request it was given; a user-sim's request is the agent's reply, -# so that prompt has it carry on as the agent. -USER_SIM_RESUME_PROMPT = ( - "Your previous turn was interrupted by a transient API error before you sent your message. You are still the " - "user. Write the user's next message, as you were about to; the assistant's last reply above is what you are " - "answering." -) - _DEFAULT_USER_SIM_OUTPUT_FORMAT: dict[str, Any] = { "type": "json_schema", "schema": { diff --git a/tst/unit/task_step/test_deploy_user_sim_agent.py b/tst/unit/task_step/test_deploy_user_sim_agent.py index 2992a85ba..2843c971d 100644 --- a/tst/unit/task_step/test_deploy_user_sim_agent.py +++ b/tst/unit/task_step/test_deploy_user_sim_agent.py @@ -18,6 +18,7 @@ from agent_env.task_step.context import TaskStepContext from agent_env.task_step.task_steps import always_on as ao from agent_env.task_step.task_steps.agent import user_sim_versions +from agent_env.task_step.task_steps.agent.user_sim_common import USER_SIM_RESUME_PROMPT from agent_env.task_step.task_steps.deploy_agent import DeployAgentTaskStep from agent_env.task_step.task_steps.agent.deploy_user_sim_agent import USER_SIMS_KEY, DeployUserSimAgentTaskStep from tst.unit.task_step.always_on_harness import USER_SIM, Rig @@ -96,6 +97,22 @@ async def test_the_prompt_set_on_the_user_s_agent_is_the_tested_one_byte_for_byt assert context.metadata["models"]["user simulator"]["user"] == "sim-model" +@pytest.mark.asyncio +async def test_a_user_whose_card_takes_a_resume_prompt_is_sent_the_one_that_keeps_it_the_user(monkeypatch): + # An agent's own resume prompt asks it to finish its request; a user sim's request is the assistant's reply. + deploy = _Deploy(monkeypatch, _config_card("name", "model", "system_prompt", "output_format", "resume_prompt")) + context = await deploy.run(_step()) + assert deploy.fields("resume_prompt") == [USER_SIM_RESUME_PROMPT] + assert "resume_prompt" in context.metadata[USER_SIMS_KEY]["hannah"]["setup"]["accepted_fields"] + + +@pytest.mark.asyncio +async def test_a_user_whose_card_takes_no_resume_prompt_is_sent_none(monkeypatch): + deploy = _Deploy(monkeypatch) + await deploy.run(_step()) + assert deploy.fields("resume_prompt") == [] + + @pytest.mark.asyncio async def test_a_one_sitting_user_gets_the_done_is_a_sign_off_line(monkeypatch): deploy = _Deploy(monkeypatch) From d3ed4657742e66a8161c6c8f8bb3c816c5b82b8f Mon Sep 17 00:00:00 2001 From: t Date: Mon, 16 Nov 2026 13:14:57 -0500 Subject: [PATCH 3/5] fix(user sim): a resumed rating gives the rating, not a chat message The user-sim resume prompt keeps its tested wording and adds one sentence: when what the user sim was last sent asks for something else, such as rate_user_satisfaction's rating in the same session, it gives that, in the format asked for. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/task_step/task_steps/agent/user_sim_common.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/agent_env/task_step/task_steps/agent/user_sim_common.py b/src/agent_env/task_step/task_steps/agent/user_sim_common.py index c514549ec..9b9ed2a42 100644 --- a/src/agent_env/task_step/task_steps/agent/user_sim_common.py +++ b/src/agent_env/task_step/task_steps/agent/user_sim_common.py @@ -12,11 +12,13 @@ # What a user-sim turn cut short by a transient API error is resumed with, for an agent that takes `resume_prompt` # (prompt_agent's peer user sim, and the user deploy_user_sim_agent deploys). # An agent's own resume prompt asks it to finish the request it was given; a user-sim's request is the agent's reply, -# so that prompt has it carry on as the agent. +# so that prompt has it carry on as the agent. The last sentence covers a turn that isn't a reply to the assistant: the +# rating rate_user_satisfaction asks for in the same session. USER_SIM_RESUME_PROMPT = ( "Your previous turn was interrupted by a transient API error before you sent your message. You are still the " "user. Write the user's next message, as you were about to; the assistant's last reply above is what you are " - "answering." + "answering. If what you were last sent asks for something else, such as a rating, give that instead, in the " + "format it asks for." ) From 6d7d0024cfe72792575520c11e98ef4b6c9df2d1 Mon Sep 17 00:00:00 2001 From: t Date: Tue, 17 Nov 2026 15:56:22 -0500 Subject: [PATCH 4/5] feat(prompt_agent): keep the user-sim's own trajectory (from #474) When the user-sim's card serves urn:agentenv:trajectory/v1, each of its answers' trajectories is stored under /usersim/ and recorded in metadata.usersim_trajectories (turn, message id, where). Best effort: a failed fetch is logged and the conversation goes on. Folded in from #474 so the user-sim prompt_agent fixes ship together. Co-Authored-By: Claude Opus 5.5 --- .../task_step/task_steps/prompt_agent.py | 26 +++++++- .../task_step/test_prompt_agent_user_side.py | 62 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/agent_env/task_step/task_steps/prompt_agent.py b/src/agent_env/task_step/task_steps/prompt_agent.py index c8afcca44..dfe41ba41 100644 --- a/src/agent_env/task_step/task_steps/prompt_agent.py +++ b/src/agent_env/task_step/task_steps/prompt_agent.py @@ -1209,7 +1209,7 @@ async def _ask_peer(self, number: int, agent_parts: list[dict]) -> dict: try: # A user-sim runs in a sandbox agent-env deployed; a human peer, registered or named by # user_a2a_url, has none and reads the store itself. - _, user_result = await send_and_wait( + user_task_id, user_result = await send_and_wait( self._url, agent_parts, agent=self._user_sim if self._is_user_sim and self._user_sim.sandbox_id else None, shareable=self._shareable, message_id=user_a2a_task_id, context_id=self._conversation_id, @@ -1220,6 +1220,9 @@ async def _ask_peer(self, number: int, agent_parts: list[dict]) -> dict: logger.warning(f"user_a2a_url timeout for conversation {self._conversation_id} ({e}); marking abandoned") return {"end": "user_timeout"} + if self._is_user_sim: + await self._save_user_sim_trajectory(number, user_task_id, user_a2a_task_id) + user_state = (user_result.get("status") or {}).get("state") if user_state != TaskState.completed: logger.info(f"user_a2a_url returned state={user_state}; ending multi-turn") @@ -1249,6 +1252,27 @@ async def _ask_peer(self, number: int, agent_parts: list[dict]) -> dict: return {"end": "user_done"} return {"message": [{"kind": "text", "text": message_text}]} + async def _save_user_sim_trajectory(self, number: int, user_task_id: str, user_a2a_task_id: str) -> None: + """Store the user-sim's own trajectory for this answer, when its card serves one, and record where it went. + Best-effort: a failed fetch is logged and the conversation goes on.""" + card = self._user_sim.a2a_card or {} + traj_ext = A2AAgent.find_extension(card, A2AAgent.EXT_TRAJECTORY) + if not traj_ext: + return + prefix = self._step._trajectory_prefix().rstrip("/") + "/usersim/" + try: + url = await self._step._fetch_trajectory( + self._url, traj_ext, user_task_id, prefix, user_a2a_task_id, + card=card, sandbox_type=self._user_sim.sandbox_type, + ) + except Exception as e: + logger.warning(f"user-sim trajectory fetch at turn {number} failed (continuing): {e}") + return + if url is None: + logger.warning(f"user-sim trajectory fetch at turn {number} returned no trajectory; nothing stored") + return + self._record("usersim_trajectories", {"turn": number, "a2a_task_id": user_a2a_task_id, "url": url}) + def _record(self, key: str, entry: dict) -> None: self._context.metadata.setdefault(key, {}).setdefault(self._step.id, []).append(entry) diff --git a/tst/unit/task_step/test_prompt_agent_user_side.py b/tst/unit/task_step/test_prompt_agent_user_side.py index 0aba179a8..950541d35 100644 --- a/tst/unit/task_step/test_prompt_agent_user_side.py +++ b/tst/unit/task_step/test_prompt_agent_user_side.py @@ -448,6 +448,68 @@ async def test_with_no_agent_trigger_fired_the_llm_user_sim_answers(monkeypatch) assert ctx.metadata["agent_trigger_firings"] == {"talk": [{"turn": 1, "fired": []}]} +# ---- the user-sim's own trajectory ----------------------------------------------------------------------------------- + +_TRAJ = {"uri": "urn:agentenv:trajectory/v1", "params": {"endpoint": "/ext/trajectory"}} +_USER_SIM_CARD_WITH_TRAJ = {"capabilities": {"extensions": [_AGENT_CONFIG, _TRAJ]}} + + +@pytest.mark.asyncio +async def test_the_user_sims_own_trajectory_is_stored_per_answer_under_the_steps_prefix(monkeypatch): + fetched = [] + + async def fake_fetch(self, a2a_url, traj_ext, server_task_id, prefix, name, *, sandbox_type, card=None): + fetched.append((a2a_url, server_task_id, prefix, sandbox_type)) + return f"{prefix}{name}.json" + + monkeypatch.setattr(PromptAgentTaskStep, "_fetch_trajectory", fake_fetch) + peers = Peers(monkeypatch, {AGENT: [text("draft one"), text("draft two"), text("draft three")], USER: [sim("one"), sim("two")]}) + ctx = _context(user_card=_USER_SIM_CARD_WITH_TRAJ) + + await _step(turns=3, trajectory_output_prefix="s3://bucket/traj").execute(ctx) + + peers.requests(ctx) + assert fetched == [(USER, "task-2", "s3://bucket/traj/usersim/", "local"), + (USER, "task-4", "s3://bucket/traj/usersim/", "local")] + # Stored under the id the conversation's user-side message was sent with. + sent_ids = [r[2]["params"]["message"]["messageId"] for r in peers.raw + if r[1] == f"{USER}/a2a" and r[2]["method"] == "message/send"] + assert ctx.metadata["usersim_trajectories"] == {"talk": [ + {"turn": turn, "a2a_task_id": i, "url": f"s3://bucket/traj/usersim/{i}.json"} + for turn, i in enumerate(sent_ids, start=1)]} + assert len(sent_ids) == 2 + + +@pytest.mark.asyncio +async def test_a_failed_user_sim_trajectory_fetch_is_not_recorded_and_the_conversation_goes_on(monkeypatch): + async def failing_fetch(*args, **kwargs): + raise RuntimeError("no trajectory") + + monkeypatch.setattr(PromptAgentTaskStep, "_fetch_trajectory", failing_fetch) + peers = Peers(monkeypatch, {AGENT: [text("draft one"), text("draft two")], USER: [sim("shorter")]}) + ctx = _context(user_card=_USER_SIM_CARD_WITH_TRAJ) + + await _step(turns=2).execute(ctx) + + assert peers.requests(ctx)[-2] == send(AGENT, text("shorter"), "", "") + assert "usersim_trajectories" not in ctx.metadata + + +@pytest.mark.asyncio +async def test_a_user_sim_trajectory_fetch_that_stores_nothing_is_not_recorded(monkeypatch): + async def empty_fetch(*args, **kwargs): + return None + + monkeypatch.setattr(PromptAgentTaskStep, "_fetch_trajectory", empty_fetch) + peers = Peers(monkeypatch, {AGENT: [text("draft one"), text("draft two")], USER: [sim("shorter")]}) + ctx = _context(user_card=_USER_SIM_CARD_WITH_TRAJ) + + await _step(turns=2).execute(ctx) + + assert peers.requests(ctx)[-2] == send(AGENT, text("shorter"), "", "") + assert "usersim_trajectories" not in ctx.metadata + + # ---- a human peer --------------------------------------------------------------------------------------------------- @pytest.mark.asyncio From 98be8a6d84af9322f76284209f7461b7fcabcd1e Mon Sep 17 00:00:00 2001 From: t Date: Tue, 17 Nov 2026 16:03:39 -0500 Subject: [PATCH 5/5] fix(prompt_agent): bound the user-sim trajectory save by its fetch timeout The next turn waits on it and the trace is optional, so a slow trajectory endpoint now costs at most _USER_SIM_TRAJECTORY_FETCH_SECONDS, as _user_sim_trajectory already does. Test: a fetch that hangs is cut off and the conversation goes on. Co-Authored-By: Claude Opus 5.5 --- .../task_step/task_steps/prompt_agent.py | 10 +++++++--- .../task_step/test_prompt_agent_user_side.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/agent_env/task_step/task_steps/prompt_agent.py b/src/agent_env/task_step/task_steps/prompt_agent.py index dfe41ba41..97fc90bd3 100644 --- a/src/agent_env/task_step/task_steps/prompt_agent.py +++ b/src/agent_env/task_step/task_steps/prompt_agent.py @@ -1261,9 +1261,13 @@ async def _save_user_sim_trajectory(self, number: int, user_task_id: str, user_a return prefix = self._step._trajectory_prefix().rstrip("/") + "/usersim/" try: - url = await self._step._fetch_trajectory( - self._url, traj_ext, user_task_id, prefix, user_a2a_task_id, - card=card, sandbox_type=self._user_sim.sandbox_type, + # Bounded like _user_sim_trajectory: the next turn waits on this, and the trace is optional. + url = await asyncio.wait_for( + self._step._fetch_trajectory( + self._url, traj_ext, user_task_id, prefix, user_a2a_task_id, + card=card, sandbox_type=self._user_sim.sandbox_type, + ), + _USER_SIM_TRAJECTORY_FETCH_SECONDS, ) except Exception as e: logger.warning(f"user-sim trajectory fetch at turn {number} failed (continuing): {e}") diff --git a/tst/unit/task_step/test_prompt_agent_user_side.py b/tst/unit/task_step/test_prompt_agent_user_side.py index 950541d35..6f5918ca2 100644 --- a/tst/unit/task_step/test_prompt_agent_user_side.py +++ b/tst/unit/task_step/test_prompt_agent_user_side.py @@ -495,6 +495,25 @@ async def failing_fetch(*args, **kwargs): assert "usersim_trajectories" not in ctx.metadata +@pytest.mark.asyncio +async def test_a_user_sim_trajectory_fetch_that_hangs_is_cut_off_and_the_conversation_goes_on(monkeypatch): + import asyncio + from agent_env.task_step.task_steps import prompt_agent as prompt_agent_module + + async def hanging_fetch(*args, **kwargs): + await asyncio.sleep(3600) + + monkeypatch.setattr(PromptAgentTaskStep, "_fetch_trajectory", hanging_fetch) + monkeypatch.setattr(prompt_agent_module, "_USER_SIM_TRAJECTORY_FETCH_SECONDS", 0.05) + peers = Peers(monkeypatch, {AGENT: [text("draft one"), text("draft two")], USER: [sim("shorter")]}) + ctx = _context(user_card=_USER_SIM_CARD_WITH_TRAJ) + + await asyncio.wait_for(_step(turns=2).execute(ctx), 10) + + assert peers.requests(ctx)[-2] == send(AGENT, text("shorter"), "", "") + assert "usersim_trajectories" not in ctx.metadata + + @pytest.mark.asyncio async def test_a_user_sim_trajectory_fetch_that_stores_nothing_is_not_recorded(monkeypatch): async def empty_fetch(*args, **kwargs):