Skip to content

The agent that grows with you

fix(acp): a reconnect re-attach posts the unknown-recovery notice like load/resume

Tekniumdc72b18cffa7
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index b288f93cdb3..e0549a83897 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -211,6 +211,13 @@ class GatewayACPAgent(acp.Agent):
self._snapshots[session_id] = snapshot
for pending in snapshot.get("prompts", []):
self._permission(session_id, pending)
+ await self._notify_unknown(session_id)
+
+ async def _notify_unknown(self, session_id):
+ """The classic chat's resume notice: say why the next prompt will be refused and how to recover."""
+ if self._has_unknown(session_id) and self._conn:
+ await self._conn.session_update(session_id=self._editor_id(session_id), update=acp.update_agent_message_text(
+ self._unknown_recovery(session_id, submitted=False).removeprefix("unknown_execution: ") + "\n"))
def _transport_closed(self):
# The socket reader settles every pending RPC before the pump sees its disconnect frame,
@@ -289,10 +296,7 @@ class GatewayACPAgent(acp.Agent):
await self._conn.session_update(session_id=held_id, update=update)
for pending in snapshot.get("prompts", []):
self._permission(session_id, pending)
- if self._has_unknown(session_id) and self._conn:
- # The classic chat's resume notice: say why the next prompt will be refused and how to recover.
- await self._conn.session_update(session_id=held_id, update=acp.update_agent_message_text(
- self._unknown_recovery(session_id, submitted=False).removeprefix("unknown_execution: ") + "\n"))
+ await self._notify_unknown(session_id)
return snapshot
async def load_session(self, cwd, session_id, mcp_servers=None, **kwargs):
diff --git a/tests/acp_adapter/test_acp_reattach_unknown_notice.py b/tests/acp_adapter/test_acp_reattach_unknown_notice.py
new file mode 100644
index 00000000000..085b6608636
--- /dev/null
+++ b/tests/acp_adapter/test_acp_reattach_unknown_notice.py
@@ -0,0 +1,58 @@
+"""A reconnect that re-attaches a session with a lost turn tells the editor at once, like load/resume
+(JM44 r1-P1-4): real websockets, the real GatewayClient, two owners."""
+import asyncio
+from contextlib import asynccontextmanager
+import json
+from unittest.mock import AsyncMock
+
+import pytest
+from websockets.asyncio.client import connect
+from websockets.asyncio.server import serve
+
+import acp_adapter.gateway_server as server
+from acp_adapter.gateway_server import GatewayACPAgent
+from hermes_cli.gateway_client import GatewayClient
+
+
+def _owner(profile_id, epoch, pending):
+ async def handler(ws):
+ async for raw in ws:
+ frame = json.loads(raw)
+ result = {"profile_id": profile_id} if frame["method"] == "runtime.describe" else {}
+ if frame["method"] == "session.resume":
+ result = {"session_id": frame["params"]["session_id"], "messages": [], "prompts": [],
+ "pending": pending, "replay_epoch": epoch, "last_sequence": 0}
+ await ws.send(json.dumps({"jsonrpc": "2.0", "id": frame["id"], "result": result}))
+ return handler
+
+
+async def test_reattach_after_restart_posts_the_unknown_recovery_notice(monkeypatch):
+ agent = GatewayACPAgent()
+ agent._conn = AsyncMock()
+ home = str(agent._home)
+ first = await serve(_owner(home, "first", []), "127.0.0.1", 0)
+ second = await serve(_owner(home, "second", [{"admission_id": "lost-1", "status": "unknown"}]), "127.0.0.1", 0)
+ order = iter([first, second])
+
+ @asynccontextmanager
+ async def connect_gateway():
+ async with connect(f"ws://127.0.0.1:{next(order).sockets[0].getsockname()[1]}") as ws:
+ async with GatewayClient(ws) as client:
+ yield client
+
+ monkeypatch.setattr(server, "connect_gateway", connect_gateway)
+ try:
+ await agent.load_session(cwd="/", session_id="s")
+ before = agent._conn.session_update.await_count # load's own notices (no unknown yet)
+ first.close() # the gateway restarts; its turn in flight becomes unknown on the new owner
+ await first.wait_closed()
+ await asyncio.wait_for(agent._event_task, 5) # the pump records the transport loss
+ await agent._client()
+ (call,) = agent._conn.session_update.await_args_list[before:]
+ assert call.kwargs["session_id"] == "s"
+ assert "/discard lost-1" in call.kwargs["update"].content.text
+ finally:
+ await agent.aclose()
+ second.close()
+