The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): make iteration summaries use request-local cancellation
funky-xamarin53e212cf5901
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex d46e79bc4fc..1c666ed44b5 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -2325,7 +2325,8 @@ def _anthropic_summary_attempt(agent, api_messages: list, api_request_id: str):reasoning_config=agent.reasoning_config, is_oauth=agent._is_anthropic_oauth,preserve_dots=agent._anthropic_preserve_dots(), base_url=getattr(agent, "_anthropic_base_url", None))ant_kw = _merge_nous_portal_messages_extra_body(agent, ant_kw)- response = _managed_summary_call(agent, api_request_id, ant_kw, agent._anthropic_messages_create, retry_count=retry_count)+ response = _managed_summary_call(+ agent, api_request_id, ant_kw, agent._interruptible_api_call, retry_count=retry_count)return _summary_text(agent, response, strip_tool_prefix=agent._is_anthropic_oauth)return _attempt@@ -2341,10 +2342,10 @@ def _chat_summary_attempt(agent, api_messages: list, api_request_id: str):sanitize_outbound_kwargs(agent, summary_kwargs)def _attempt(retry_count: int) -> str:- summary_client = agent._ensure_primary_openai_client(reason="iteration_limit_summary_retry" if retry_count else "iteration_limit_summary")+ # Use the ordinary request-local lifecycle: a summary can be interrupted+ # during a long prefill without closing the shared primary client.response = _managed_summary_call(- agent, api_request_id, summary_kwargs,- lambda request: summary_client.chat.completions.create(**bypass_chat_sdk_request_transform(request, summary_client)),+ agent, api_request_id, summary_kwargs, agent._interruptible_api_call,retry_count=retry_count)return _summary_text(agent, response)return _attemptdiff --git a/tests/agent/test_api_content_sidecar.py b/tests/agent/test_api_content_sidecar.pyindex f65e0d146ad..7d279ea9dbd 100644--- a/tests/agent/test_api_content_sidecar.py+++ b/tests/agent/test_api_content_sidecar.py@@ -922,7 +922,7 @@ class TestMaxIterationsSummaryReplay:{"role": "assistant", "content": "a1"},]with patch.object(- agent, "_ensure_primary_openai_client", return_value=client+ agent, "_create_request_openai_client", return_value=client):out = handle_max_iterations(agent, messages, 5)diff --git a/tests/agent/test_chat_sdk_transform_bypass.py b/tests/agent/test_chat_sdk_transform_bypass.pyindex d5d783d4784..8e73608a7c4 100644--- a/tests/agent/test_chat_sdk_transform_bypass.py+++ b/tests/agent/test_chat_sdk_transform_bypass.py@@ -141,8 +141,10 @@ def test_iteration_summary_path_hands_the_sdk_only_the_placeholder(monkeypatch):transport = types.SimpleNamespace(normalize_response=lambda response, **kw: types.SimpleNamespace(content="ok", tool_calls=None))agent = types.SimpleNamespace(provider="p", model="m", api_mode="chat_completions", _force_ascii_payload=False,- _build_api_kwargs=lambda messages: dict(body), _ensure_primary_openai_client=lambda reason: client,+ _build_api_kwargs=lambda messages: dict(body),_get_transport=lambda: transport)+ agent._interruptible_api_call = lambda request: chat_completion_helpers._dispatch_nonstreaming_api_request(+ agent, request, make_client=lambda *args, **kwargs: client)assert chat_completion_helpers._chat_summary_attempt(agent, body["messages"], "req-1")(0) == "ok"assert len(seen) == 1diff --git a/tests/agent/test_run_agent.py b/tests/agent/test_run_agent.pyindex 489ffe4f01f..febf261870c 100644--- a/tests/agent/test_run_agent.py+++ b/tests/agent/test_run_agent.py@@ -2451,6 +2451,59 @@ class TestMcpParallelToolBatch:class TestHandleMaxIterations:+ @pytest.mark.parametrize("api_mode,platform", [+ ("chat_completions", "cli"), ("chat_completions", "cron"),+ ("anthropic_messages", "cli"),+ ])+ def test_summary_interrupt_aborts_only_its_request(self, agent, monkeypatch, api_mode, platform):+ agent.api_mode = api_mode+ agent.platform = platform+ agent._cached_system_prompt = "You are helpful."+ entered, release, finished = threading.Event(), threading.Event(), threading.Event()+ request_client = MagicMock()+ aborted = []++ def blocked(*args, **kwargs):+ entered.set()+ release.wait(10)+ raise OSError("fixture request stopped")++ def abort(client, **kwargs):+ aborted.append(client)+ release.set()++ agent.client.chat.completions.create.side_effect = blocked+ request_client.chat.completions.create.side_effect = blocked+ monkeypatch.setattr(agent, "_create_request_openai_client", lambda **kw: request_client)+ monkeypatch.setattr(agent, "_create_request_anthropic_client", lambda **kw: request_client)+ monkeypatch.setattr(agent, "_abort_request_openai_client", abort)+ monkeypatch.setattr(agent, "_abort_request_anthropic_client", abort)+ monkeypatch.setattr(agent, "_close_request_openai_client", lambda *a, **kw: None)+ monkeypatch.setattr(agent, "_close_request_anthropic_client", lambda *a, **kw: None)+ if api_mode == "anthropic_messages":+ agent._is_anthropic_oauth = False+ transport = SimpleNamespace(build_kwargs=lambda **kw: {"model": "fixture", "messages": kw["messages"]})+ monkeypatch.setattr(agent, "_get_transport", lambda: transport)+ monkeypatch.setattr(agent, "_anthropic_messages_create", blocked)++ def summarize():+ try:+ agent._handle_max_iterations([{"role": "user", "content": "work"}], 1)+ finally:+ finished.set()++ worker = threading.Thread(target=summarize)+ worker.start()+ try:+ assert entered.wait(5), "summary did not reach provider fixture"+ agent.interrupt()+ assert finished.wait(4), "summary ignored interrupt while provider was blocked"+ assert aborted == [request_client]+ agent.client.close.assert_not_called()+ finally:+ release.set()+ worker.join(12)+def test_summary_notice_uses_safe_print(self, agent):agent._print_fn = lambda *_args, **_kwargs: (_ for _ in ()).throw(ValueError("closed"))agent.client.chat.completions.create.return_value = _mock_response(content="Summary")