The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(acp): an un-acked prompt's identity is reused only by that session's next prompt
Tekniume87e1c699272
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.pyindex ccb3b2edd98..a61a58e815f 100644--- a/acp_adapter/gateway_server.py+++ b/acp_adapter/gateway_server.py@@ -122,8 +122,9 @@ class GatewayACPAgent(acp.Agent):self._permissions = {}self._admissions = {}self._submitting = set()- # (session, content fingerprint) -> input_id of a submit whose ack never arrived. The- # editor's retry of that prompt reuses it, so the authority returns the same admission.+ # Session -> (content fingerprint, input_id) of a submit whose ack never arrived. The+ # 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.self._settling = set()@@ -444,7 +445,10 @@ class GatewayACPAgent(acp.Agent):if attachments:submit['attachments'] = attachmentsretry_key = await asyncio.to_thread(_submit_fingerprint, session_id, text, attachments)- input_id = self._unacked.pop(retry_key, None) or uuid.uuid4().hex+ # Only this session's very next prompt may be the editor's retry of an un-acked submit; any+ # prompt retires the retained identity, so a deliberately repeated prompt later is new work.+ retained = self._unacked.pop(session_id, None)+ input_id = retained[1] if retained is not None and retained[0] == retry_key else uuid.uuid4().hexself._submitting.add(session_id)try:receipt = await client.rpc("prompt.submit", session_id=session_id, input_id=input_id, **submit)@@ -452,8 +456,8 @@ class GatewayACPAgent(acp.Agent):from hermes_cli.gateway_client import GatewayRPCErrorif not isinstance(exc, GatewayRPCError):# Transport loss, timeout or cancel before the ack: the owner may hold this admission.- # Keep its identity for the editor's retry of the same prompt (latest 64 kept).- self._unacked[retry_key] = input_id+ # Keep its identity for the editor's retry of the same prompt (latest 64 sessions).+ self._unacked[session_id] = (retry_key, input_id)while len(self._unacked) > 64:self._unacked.pop(next(iter(self._unacked)))self._pending_cancels.discard(session_id)diff --git a/tests/acp_adapter/test_gateway_submit_retry.py b/tests/acp_adapter/test_gateway_submit_retry.pyindex 19df2f98c38..7b56466283a 100644--- a/tests/acp_adapter/test_gateway_submit_retry.py+++ b/tests/acp_adapter/test_gateway_submit_retry.py@@ -87,3 +87,43 @@ async def test_retry_after_lost_submit_ack_is_one_admission(tmp_path, monkeypatcagent._event_task.cancel()await wire.viewer.close()store.close_all_db_handles()+++async def test_repeated_prompt_after_other_work_is_new_work(tmp_path, monkeypatch):+ """The retained identity of an un-acked prompt is only for the editor's retry (that session's+ next prompt): once other work was sent, the same words are a new turn, not a silent replay."""+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())+ executed = []++ async def answer(event):+ from gateway.session_results import execution_result+ executed.append(event.text)+ execution_result.get()['result'] = {'final_response': 'ACK_' + event.text, 'completed': True, 'messages': []}+ return 'ACK_' + event.text++ runner = SimpleNamespace(_session_db=store._db, session_store=store, _draining=False,+ _handle_message=answer, _adapter_for_source=lambda source: None)+ store._db.create_session('s', source='telegram')+ authority = await initialize_session_authority(runner, profile_id='p', instance_id='owner')+ authority.sessions['s'] = LiveSession(SessionSource(platform=Platform.TELEGRAM, chat_id='repeat'), 's')+ wire = Wire(asyncio.get_running_loop())+ wire.viewer = AuthorityConnection(authority, wire, {'user_id': 'editor'})+ agent = GatewayACPAgent()+ agent._gateway = wire+ agent._conn = SimpleNamespace(session_update=lambda **kwargs: asyncio.sleep(0))+ agent._snapshots['s'] = await wire.rpc('session.resume', session_id='s')+ agent._event_task = asyncio.create_task(agent._events())+ try:+ with pytest.raises(GatewayClientError, match='outcome is unknown'):+ await agent.prompt([acp.text_block('yes')], 's')+ for words in ('other words', 'yes'):+ response = await asyncio.wait_for(agent.prompt([acp.text_block(words)], 's'), 5)+ assert response.stop_reason == 'end_turn'+ assert executed == ['yes', 'other words', 'yes']+ assert len(list_session_admissions(authority.db, session_id='s', pending_only=False)) == 3+ finally:+ agent._event_task.cancel()+ await wire.viewer.close()+ store.close_all_db_handles()