The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(acp): cancel post-attach notices before closing the gateway
unsupportedpastelsf115e20a14fe
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.pyindex 29d4f5fe4fd..475d67d655e 100644--- a/acp_adapter/gateway_server.py+++ b/acp_adapter/gateway_server.py@@ -859,6 +859,13 @@ class GatewayACPAgent(acp.Agent):return Falseasync def aclose(self):+ # Post-attach notices first: one paused mid-notice would otherwise resume after the+ # teardown and open a fresh gateway connection through ``_client``.+ background = list(self._background)+ for task in background:+ task.cancel()+ if background:+ await asyncio.gather(*background, return_exceptions=True)for task in self._permissions.values():task.cancel()if self._permissions:diff --git a/tests/acp_adapter/test_gateway_attach_shutdown.py b/tests/acp_adapter/test_gateway_attach_shutdown.pynew file mode 100644index 00000000000..55454dbf7a3--- /dev/null+++ b/tests/acp_adapter/test_gateway_attach_shutdown.py@@ -0,0 +1,39 @@+"""Post-attach notices (command list, context indicator) never outlive ``aclose``."""+import asyncio+from unittest.mock import AsyncMock++import pytest++from acp_adapter.gateway_server import GatewayACPAgent+++async def test_aclose_stops_a_paused_post_attach_notice_before_it_reconnects(monkeypatch):+ import acp_adapter.gateway_session_meta as meta+ monkeypatch.setattr(meta, 'model_state', lambda info: None)+ monkeypatch.setattr(meta, 'provenance_meta', lambda *args: None)+ connects = []++ def connect_gateway():+ connects.append('after shutdown')+ raise ConnectionRefusedError++ monkeypatch.setattr('acp_adapter.gateway_server.connect_gateway', connect_gateway)+ agent = GatewayACPAgent()+ agent._conn = AsyncMock()+ paused, release = asyncio.Event(), asyncio.Event()++ async def session_update(**kwargs):+ paused.set()+ await release.wait() # an editor slow to read the notice++ agent._conn.session_update.side_effect = session_update+ await agent._session_fields('s', {}, 's')+ await asyncio.wait_for(paused.wait(), 5)+ tasks = list(agent._background)++ await agent.aclose()+ release.set()+ await asyncio.wait_for(asyncio.gather(*tasks, return_exceptions=True), 5)+ assert all(task.cancelled() for task in tasks) and not agent._background+ assert connects == []