Skip to content

The agent that grows with you

fix(acp): a reconnect or terminal receipt never leaves a reply id open

Teknium11adaf06b9ae
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index f2ac61c6cdd..7cf03628471 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -320,9 +320,12 @@ class GatewayACPAgent(acp.Agent):
await task
if connection is not None:
await connection.__aexit__(None, None, None)
- # Partial reply segments belonged to admissions whose waiters already failed.
+ # Partial reply segments belonged to admissions whose waiters already failed. A reply cut off here
+ # never gets its ``message.complete`` (re-attach starts at the owner's current sequence), so its
+ # ``messageId`` closes too: the next reply must not reuse it.
self._streamed.clear()
self._segments.clear()
+ self._message_ids.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.
@@ -727,9 +730,7 @@ class GatewayACPAgent(acp.Agent):
try:
outcome = {"interrupted": "cancelled"}.get(receipt.get("outcome"), receipt.get("outcome"))
text = (saved.get("result") or {}).get("final_response") or ""
- remainder = self._final_remainder(admission_id, {"text": text, "outcome": outcome})
- if remainder and self._conn:
- await self._conn.session_update(session_id=held_id, update=acp.update_agent_message_text(remainder))
+ await self._finish_reply(session_id, held_id, admission_id, {"text": text, "outcome": outcome})
async with self._changed:
self._terminals[admission_id] = {"outcome": outcome}
self._changed.notify_all()
@@ -836,13 +837,7 @@ class GatewayACPAgent(acp.Agent):
return
self._settling.add(aid)
try:
- remainder = self._final_remainder(aid, payload)
- if remainder and self._conn:
- await self._conn.session_update(session_id=editor_id, update=self._chunk(sid, remainder))
- # The turn is over: its reply's id closes, and a tool call it left open (blocked by a guard,
- # an approval denied) ends ``failed``, as the in-process agent flushed them at turn end.
- self._end_message(sid)
- await self._flush_open_tools(sid, editor_id, aid)
+ await self._finish_reply(sid, editor_id, aid, payload)
async with self._changed:
self._terminals[aid] = payload
if len(self._terminals) > 256:
@@ -877,6 +872,16 @@ class GatewayACPAgent(acp.Agent):
call_id, name, result=result if isinstance(result, str) else None, function_args=args,
is_error=payload.get("is_error") is True))
+ async def _finish_reply(self, sid, editor_id, aid, payload):
+ """Show the settled reply's unseen remainder, then end the turn: its reply's id closes, and a tool
+ call it left open (blocked by a guard, an approval denied) ends ``failed``, as the in-process agent
+ flushed them at turn end. Shared by ``message.complete`` and a retry's terminal receipt."""
+ remainder = self._final_remainder(aid, payload)
+ if remainder and self._conn:
+ await self._conn.session_update(session_id=editor_id, update=self._chunk(sid, remainder))
+ self._end_message(sid)
+ await self._flush_open_tools(sid, editor_id, aid)
+
async def _flush_open_tools(self, sid, editor_id, aid):
"""Close every call admission *aid* started and never completed (``failed``: it produced no result)."""
from acp_adapter.tools import build_tool_abandoned
diff --git a/tests/acp_adapter/test_gateway_event_mapping.py b/tests/acp_adapter/test_gateway_event_mapping.py
index fa0907bc2d6..554625bb715 100644
--- a/tests/acp_adapter/test_gateway_event_mapping.py
+++ b/tests/acp_adapter/test_gateway_event_mapping.py
@@ -135,3 +135,33 @@ async def test_assistant_chunks_carry_a_message_id_that_changes_at_each_new_mess
assert all(uuid.UUID(i) for i in ids)
# think + 'Let me look.' | 'It says ' + remainder 'x.' | next turn
assert ids[0] == ids[1] != ids[2] == ids[3] != ids[4], ids
+
+
+async def test_a_reconnect_never_reuses_the_open_reply_id():
+ """A reply cut off by a lost connection never receives its ``message.complete`` (the re-attach starts at
+ the owner's current sequence), so the next reply must still open a new ``messageId``."""
+ editor = agent()
+ await project(editor, 'message.delta', {'text': 'half a reply'})
+ await editor._teardown()
+ await project(editor, 'message.delta', {'text': 'the next reply'}, aid='b')
+ first, second = (u.message_id for u in updates(editor))
+ assert first != second
+
+
+async def test_a_terminal_receipt_finishes_the_reply_like_message_complete():
+ """A retry whose turn already settled is reported from its receipt: that remainder carries the
+ ``messageId``, closes it, and fails the turn's still-open tool calls, as ``message.complete`` does."""
+ editor = agent()
+ await project(editor, 'message.delta', {'text': 'Look'})
+ await project(editor, 'tool.start', {'tool_call_id': 'c1', 'tool_name': 'terminal', 'args': {}})
+ editor._gateway.rpc.return_value = {'result': {'final_response': 'Done.'}}
+ await editor._settled_receipt(editor._gateway, 's', 's', {'admission_id': 'a', 'outcome': 'completed'})
+ await project(editor, 'message.delta', {'text': 'next'}, aid='b')
+ sent = updates(editor)
+ chunks = [u for u in sent if u.session_update == 'agent_message_chunk']
+ assert [c.content.text for c in chunks] == ['Look', 'Done.', 'next']
+ assert chunks[1].message_id and chunks[1].message_id != chunks[2].message_id
+ closed = [u for u in sent if u.session_update == 'tool_call_update']
+ assert [(u.tool_call_id, u.status) for u in closed] == [('c1', 'failed')]