Skip to content

The agent that grows with you

fix: ACP reconnects to a restarted gateway instead of latching its failure

Tekniume3b169ddc6fc
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index 56d3604523c..5dc9b9d2cd8 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -102,6 +102,12 @@ class GatewayACPAgent(acp.Agent):
self._segments = {}
self._changed = asyncio.Condition()
self._failure = None
+ # Connection generation: a waiter whose transport was replaced reports that transport's
+ # ambiguous failure (``_lost_failure``) instead of waiting on a connection it never used.
+ self._link = 0
+ self._lost_failure = None
+ # Session -> editor MCP servers a replacement connection must re-attach.
+ self._editor_mcp = {}
self._permissions = {}
self._admissions = {}
self._submitting = set()
@@ -144,6 +150,11 @@ class GatewayACPAgent(acp.Agent):
async def _client(self):
async with self._connect_lock:
+ if self._failure is not None or self._transport_closed():
+ # The event pump died with this transport (gateway restart, socket loss, replay gap).
+ # Its waiters hold the ambiguous failure; the next call gets a fresh, re-verified
+ # connection instead of the stale error forever.
+ await self._teardown()
if self._gateway is None:
connection = connect_gateway()
gateway = await connection.__aenter__()
@@ -151,6 +162,7 @@ class GatewayACPAgent(acp.Agent):
descriptor = await gateway.rpc("runtime.describe")
if descriptor.get("profile_id") != str(self._home):
raise GatewayClientError("profile_mismatch")
+ await self._reattach(gateway)
except BaseException:
await connection.__aexit__(None, None, None)
raise
@@ -160,6 +172,55 @@ class GatewayACPAgent(acp.Agent):
raise self._failure
return self._gateway
+ async def _reattach(self, gateway):
+ """Subscribe a replacement connection to every session this editor holds.
+
+ The fresh snapshot carries the new replay epoch/sequence and the authority's current
+ pending rows (a turn lost in the restart is ``unknown`` there, never resubmitted); the
+ editor's borrowed MCP servers are re-attached because a restarted owner has none."""
+ from hermes_cli.gateway_client import GatewayRPCError
+ for session_id in list(self._snapshots):
+ params = {"editor": self._editor_mcp[session_id]} if session_id in self._editor_mcp else {}
+ try:
+ snapshot = await gateway.rpc("session.resume", session_id=session_id, **params)
+ except GatewayRPCError:
+ # A definitive refusal for this session (deleted, retired) must not keep the
+ # editor's other sessions offline; its next prompt reports not_found.
+ logger.info("ACP session %s not re-attached after reconnect", session_id, exc_info=True)
+ self._snapshots.pop(session_id, None)
+ continue
+ self._snapshots[session_id] = snapshot
+ for pending in snapshot.get("prompts", []):
+ self._permission(session_id, pending)
+
+ def _transport_closed(self):
+ # The socket reader settles every pending RPC before the pump sees its disconnect frame,
+ # so an editor retry can arrive while ``_failure`` is still unset.
+ reader = getattr(self._gateway, "reader", None)
+ return isinstance(reader, asyncio.Future) and reader.done()
+
+ async def _teardown(self):
+ task, connection = self._event_task, self._connection
+ self._event_task = self._connection = self._gateway = None
+ if task is not None:
+ task.cancel()
+ with suppress(asyncio.CancelledError):
+ await task
+ if connection is not None:
+ await connection.__aexit__(None, None, None)
+ # Partial reply segments belonged to admissions whose waiters already failed.
+ self._streamed.clear()
+ self._segments.clear()
+ async with self._changed:
+ # Every waiter on the old transport wakes to its ambiguous failure, even one that had
+ # not yet observed ``_failure`` before it is cleared here.
+ self._link += 1
+ self._lost_failure = self._failure or GatewayClientError(
+ "Gateway disconnected; turn outcome is unknown. Resume the session to check whether "
+ "it completed or was interrupted; do not resend the work.")
+ self._failure = None
+ self._changed.notify_all()
+
async def new_session(self, cwd, mcp_servers=None, **kwargs):
client = await self._client()
descriptor = await client.rpc("runtime.describe")
@@ -171,6 +232,9 @@ class GatewayACPAgent(acp.Agent):
editor={"mcp_servers": [s.model_dump(by_alias=True) for s in mcp_servers or []],
"edit_approval_policy": "ask"})
self._snapshots[snapshot["session_id"]] = snapshot
+ if mcp_servers:
+ self._editor_mcp[snapshot["session_id"]] = {
+ "mcp_servers": [s.model_dump(by_alias=True) for s in mcp_servers]}
return NewSessionResponse(session_id=snapshot["session_id"])
def _editor_id(self, session_id):
@@ -198,6 +262,8 @@ class GatewayACPAgent(acp.Agent):
resume_params['editor'] = {'mcp_servers': [s.model_dump(by_alias=True) for s in mcp_servers]}
snapshot = await client.rpc("session.resume", session_id=session_id, **resume_params)
self._snapshots[session_id] = snapshot
+ if 'editor' in resume_params:
+ self._editor_mcp[session_id] = resume_params['editor']
from acp_adapter.server import _history_replay_updates
if self._conn:
for update in _history_replay_updates(snapshot["messages"]):
@@ -318,6 +384,7 @@ class GatewayACPAgent(acp.Agent):
self._mutations.acknowledge(session_id, operation, payload)
return PromptResponse(stop_reason='end_turn')
client = await self._client()
+ link = self._link
submit = {'text': text}
if attachments:
submit['attachments'] = attachments
@@ -338,9 +405,14 @@ class GatewayACPAgent(acp.Agent):
await self._cancel_admission(session_id, admission_id)
async with self._changed:
await self._changed.wait_for(lambda: admission_id in self._terminals or self._failure is not None
+ or self._link != link
or self._blocked_admission(session_id, admission_id))
# A successor's unknown state cannot replace our committed terminal result.
if admission_id not in self._terminals:
+ if self._link != link:
+ # Our transport died before this turn's outcome reached us: ambiguous,
+ # never retried on the replacement connection.
+ raise self._lost_failure
if self._failure:
raise self._failure
raise GatewayClientError("unknown_execution: do not resend accepted input; "
@@ -370,9 +442,10 @@ class GatewayACPAgent(acp.Agent):
for row in self._snapshots[session_id].get("pending", []))
async def _events(self):
+ gateway = self._gateway
try:
while True:
- frame = await self._gateway.events.get()
+ frame = await gateway.events.get()
if isinstance(frame, Exception):
raise frame
event = frame.get("params", {})
@@ -560,11 +633,4 @@ class GatewayACPAgent(acp.Agent):
if self._permissions:
await asyncio.gather(*self._permissions.values(), return_exceptions=True)
self._permissions.clear()
- if self._event_task:
- self._event_task.cancel()
- with suppress(asyncio.CancelledError):
- await self._event_task
- if self._connection:
- await self._connection.__aexit__(None, None, None)
- self._connection = None
- self._gateway = None
+ await self._teardown()
diff --git a/tests/acp_adapter/test_gateway_reconnect.py b/tests/acp_adapter/test_gateway_reconnect.py
new file mode 100644
index 00000000000..c37f2bbeb9a
--- /dev/null
+++ b/tests/acp_adapter/test_gateway_reconnect.py
@@ -0,0 +1,90 @@
+"""An ACP editor survives a gateway restart: real websockets, the real GatewayClient, two owners."""
+import asyncio
+from contextlib import asynccontextmanager
+import json
+from unittest.mock import AsyncMock
+
+import acp
+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
+
+
+class Owner:
+ """One gateway instance: answers the canonical RPCs ACP uses and records them."""
+
+ def __init__(self, profile_id, epoch, *, die_on_submit=None):
+ self.profile_id, self.epoch, self.die_on_submit = profile_id, epoch, die_on_submit
+ self.calls = []
+ self.server = None
+
+ async def handler(self, ws):
+ async for raw in ws:
+ frame = json.loads(raw)
+ method, params = frame["method"], frame["params"]
+ self.calls.append(method)
+ if method == "prompt.submit" and self.die_on_submit == "before_ack":
+ await ws.close() # the owner dies with the submit in flight
+ return
+ result = {"profile_id": self.profile_id} if method == "runtime.describe" else {}
+ if method == "session.resume":
+ result = {"session_id": params["session_id"], "messages": [], "pending": [], "prompts": [],
+ "replay_epoch": self.epoch, "last_sequence": 0}
+ if method == "prompt.submit":
+ result = {"admission_id": f"{self.epoch}-{params['input_id']}"}
+ await ws.send(json.dumps({"jsonrpc": "2.0", "id": frame["id"], "result": result}))
+ if method == "prompt.submit" and self.die_on_submit == "after_ack":
+ await ws.close()
+ return
+ if method == "prompt.submit":
+ await ws.send(json.dumps({"jsonrpc": "2.0", "method": "event", "params": {
+ "session_id": params["session_id"], "type": "message.complete", "replay_epoch": self.epoch,
+ "seq": 1, "admission_id": result["admission_id"],
+ "payload": {"outcome": "completed", "text": "AFTER-RESTART"}}}))
+
+ @property
+ def url(self):
+ return f"ws://127.0.0.1:{self.server.sockets[0].getsockname()[1]}"
+
+
[email protected]("die", ["before_ack", "after_ack"])
+async def test_editor_reconnects_to_a_restarted_gateway_without_resending(monkeypatch, die):
+ agent = GatewayACPAgent()
+ agent._conn = AsyncMock()
+ home = str(agent._home)
+ owners = [Owner(home, "first", die_on_submit=die), Owner(home, "second")]
+ for owner in owners:
+ owner.server = await serve(owner.handler, "127.0.0.1", 0)
+ order = iter(owners)
+
+ @asynccontextmanager
+ async def connect_gateway():
+ async with connect(next(order).url) 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")
+ # The in-flight turn's outcome is unknown: reported, never silently resubmitted.
+ with pytest.raises(Exception, match="outcome is unknown"):
+ await asyncio.wait_for(agent.prompt([acp.text_block("in flight")], "s"), 5)
+ response = await asyncio.wait_for(agent.prompt([acp.text_block("after restart")], "s"), 5)
+ assert response.stop_reason == "end_turn"
+ assert agent._failure is None
+ # The replacement was re-verified and re-subscribed before the new submit; the
+ # lost input was submitted to the first owner only.
+ assert owners[1].calls == ["runtime.describe", "session.resume", "prompt.submit"]
+ assert owners[0].calls.count("prompt.submit") == 1
+ assert agent._snapshots["s"]["replay_epoch"] == "second"
+ texts = [call.kwargs["update"].content.text for call in agent._conn.session_update.await_args_list]
+ assert texts.count("AFTER-RESTART") == 1
+ finally:
+ await agent.aclose()
+ for owner in owners:
+ owner.server.close()