Skip to content

The agent that grows with you

fix(acp): a retried submit's terminal receipt and the live completion show one reply

Teknium7c672ad5bcab
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index e0549a83897..c49f5a3050f 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -128,8 +128,11 @@ class GatewayACPAgent(acp.Agent):
# editor's retry (that session's next prompt, same content) reuses it, so the authority
# returns the same admission.
self._unacked = {}
- # Admissions whose message.complete the pump is projecting right now.
+ # Admissions whose terminal reply one path (the pump's message.complete or a retry's
+ # terminal receipt) is delivering right now; the other path yields to the claim.
self._settling = set()
+ # Admissions a terminal receipt already delivered: their late message.complete adds nothing.
+ self._receipted = {}
self._pending_cancels = set()
self._tool_args = {}
# Editor-held compression-tip id -> logical session id (see ``_resume``).
@@ -521,13 +524,21 @@ class GatewayACPAgent(acp.Agent):
include_result=True)
if admission_id in self._terminals or admission_id in self._settling:
return # this connection's pump carried the completion itself
- text = (saved.get("result") or {}).get("final_response") or ""
- if text and self._conn:
- await self._conn.session_update(session_id=held_id, update=acp.update_agent_message_text(text))
- outcome = {"interrupted": "cancelled"}.get(receipt.get("outcome"), receipt.get("outcome"))
- async with self._changed:
- self._terminals[admission_id] = {"outcome": outcome}
- self._changed.notify_all()
+ self._settling.add(admission_id) # claimed before the first await; the pump yields to it
+ self._receipted[admission_id] = None
+ while len(self._receipted) > 256:
+ self._receipted.pop(next(iter(self._receipted)))
+ 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))
+ async with self._changed:
+ self._terminals[admission_id] = {"outcome": outcome}
+ self._changed.notify_all()
+ finally:
+ self._settling.discard(admission_id)
def _unknown_recovery(self, session_id, admission_id=None, *, submitted=True):
"""The refusal for a FIFO blocked by a lost turn, naming the existing recovery control.
@@ -640,6 +651,11 @@ class GatewayACPAgent(acp.Agent):
await self._conn.session_update(session_id=editor_id,
update=acp.update_agent_message_text(text + "\n\n"))
elif kind == "message.complete":
+ if aid in self._settling or aid in self._receipted:
+ # A retry's terminal receipt claimed this reply; it is shown once, from there.
+ self._streamed.pop(aid, None)
+ self._segments.pop(aid, None)
+ return
self._settling.add(aid)
try:
remainder = self._final_remainder(aid, payload)
diff --git a/tests/acp_adapter/test_gateway_terminal_receipt_claim.py b/tests/acp_adapter/test_gateway_terminal_receipt_claim.py
new file mode 100644
index 00000000000..7d292c117cb
--- /dev/null
+++ b/tests/acp_adapter/test_gateway_terminal_receipt_claim.py
@@ -0,0 +1,76 @@
+"""A retried submit's terminal receipt and the live message.complete deliver one reply between them."""
+import asyncio
+from unittest.mock import AsyncMock
+
+import pytest
+from acp.schema import TextContentBlock
+
+from acp_adapter.gateway_server import GatewayACPAgent
+
+
+def _agent(final, *, receipt_gate=None):
+ agent = GatewayACPAgent()
+ agent._snapshots['s'] = {}
+ agent._gateway = AsyncMock()
+
+ async def rpc(method, **kwargs):
+ if method == 'prompt.submit':
+ return {'admission_id': 'a1', 'status': 'terminal', 'outcome': 'completed'}
+ assert method == 'prompt.receipt'
+ if receipt_gate is not None:
+ await receipt_gate()
+ return {'result': {'final_response': final}}
+
+ agent._gateway.rpc.side_effect = rpc
+ agent._conn = AsyncMock()
+ return agent
+
+
+def _complete(text):
+ return {'session_id': 's', 'type': 'message.complete', 'admission_id': 'a1',
+ 'payload': {'outcome': 'completed', 'text': text}}
+
+
+def _shown(agent):
+ return [call.kwargs['update'].content.text for call in agent._conn.session_update.await_args_list]
+
+
+async def _prompt(agent):
+ return await asyncio.wait_for(agent.prompt([TextContentBlock(type='text', text='hello')], 's'), 2)
+
+
+async def test_live_completion_during_the_receipt_editor_update_is_not_shown_twice():
+ agent = _agent('Final answer')
+ updates = []
+
+ async def session_update(**kwargs):
+ updates.append(kwargs['update'].content.text)
+ if len(updates) == 1: # the pump projects the queued completion while the receipt is suspended
+ await agent._project(_complete('Final answer'))
+
+ agent._conn.session_update.side_effect = session_update
+ assert (await _prompt(agent)).stop_reason == 'end_turn'
+ assert updates == ['Final answer']
+ # The late completion of that admission paints nothing either.
+ await agent._project(_complete('Final answer'))
+ assert updates == ['Final answer']
+
+
+async def test_receipt_shows_only_what_the_live_stream_did_not():
+ agent = _agent('Hello world')
+ await agent._project({'session_id': 's', 'type': 'message.delta', 'admission_id': 'a1',
+ 'payload': {'text': 'Hello '}})
+ assert (await _prompt(agent)).stop_reason == 'end_turn'
+ assert _shown(agent) == ['Hello ', 'world']
+
+
+async def test_completion_that_lands_before_the_receipt_is_the_one_shown():
+ async def gate():
+ await agent._project(_complete('Final answer'))
+
+ agent = _agent('Final answer', receipt_gate=gate)
+ assert (await _prompt(agent)).stop_reason == 'end_turn'
+ assert _shown(agent) == ['Final answer']