Skip to content

The agent that grows with you

fix: project ACP cancellation and failed admissions accurately

Teknium03c0efc84dd6
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index 6b79d485fd7..7c56ad110ce 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -218,8 +218,11 @@ class GatewayACPAgent(acp.Agent):
await self._changed.wait_for(lambda: admission_id in self._terminals or self._failure is not None)
if self._failure:
raise self._failure
- self._terminals.pop(admission_id)
- return PromptResponse(stop_reason="end_turn")
+ terminal = self._terminals.pop(admission_id)
+ outcome = terminal.get("outcome")
+ if outcome == "failed":
+ raise GatewayClientError("admitted_turn_failed")
+ return PromptResponse(stop_reason="cancelled" if outcome == "cancelled" else "end_turn")
async def _events(self):
try:
@@ -262,7 +265,12 @@ class GatewayACPAgent(acp.Agent):
if text and self._conn:
await self._conn.session_update(session_id=sid, update=acp.update_agent_message_text(text))
elif kind == "message.complete":
+ from agent.conversation_loop import INTERRUPT_WAITING_FOR_MODEL_PREFIX
+
text = payload.get("text", "")
+ # Local interrupt status is metadata; ACP carries it in stop_reason.
+ if payload.get("outcome") == "cancelled" and text.startswith(INTERRUPT_WAITING_FOR_MODEL_PREFIX):
+ text = ""
prefix = self._streamed.pop(aid, "")
remainder = text[len(prefix):] if text.startswith(prefix) else text
if remainder and self._conn:
diff --git a/tests/acp/test_gateway_terminal_projection.py b/tests/acp/test_gateway_terminal_projection.py
new file mode 100644
index 00000000000..3d7a66e9640
--- /dev/null
+++ b/tests/acp/test_gateway_terminal_projection.py
@@ -0,0 +1,58 @@
+"""Terminal ACP responses describe the exact admission, not the session's latest turn."""
+import asyncio
+from unittest.mock import AsyncMock
+
+import pytest
+from acp.schema import TextContentBlock
+
+from acp_adapter.gateway_server import GatewayACPAgent
+from hermes_cli.gateway_client import GatewayClientError
+
+
+def agent_with_admission():
+ agent = GatewayACPAgent()
+ agent._snapshots['s'] = {}
+ agent._gateway = AsyncMock()
+ agent._gateway.rpc.return_value = {'admission_id': 'current'}
+ agent._conn = AsyncMock()
+ return agent
+
+
+async def complete(agent, admission_id, outcome, text):
+ await agent._project({'session_id': 's', 'type': 'message.complete',
+ 'admission_id': admission_id,
+ 'payload': {'outcome': outcome, 'text': text}})
+
+
[email protected]('text,visible', [
+ ('Operation interrupted: waiting for model response (0.3s elapsed).', ''),
+ ('Partial answer', 'Partial answer'),
+])
+async def test_cancelled_admission_suppresses_only_interrupt_metadata(text, visible):
+ agent = agent_with_admission()
+ await complete(agent, 'other', 'completed', '')
+ task = asyncio.create_task(agent.prompt([TextContentBlock(type='text', text='hello')], 's'))
+ await asyncio.sleep(0)
+ assert not task.done()
+ await complete(agent, 'current', 'cancelled', text)
+ response = await asyncio.wait_for(task, 2)
+ assert response.stop_reason == 'cancelled'
+ emitted = ''.join(call.kwargs['update'].content.text
+ for call in agent._conn.session_update.await_args_list)
+ assert emitted == visible
+ assert 'other' in agent._terminals and 'current' not in agent._terminals
+
+
+async def test_failed_admission_raises_without_poisoning_the_next_success():
+ agent = agent_with_admission()
+ await complete(agent, 'current', 'failed', 'The admitted turn failed.')
+ with pytest.raises(GatewayClientError, match='admitted_turn_failed'):
+ await agent.prompt([TextContentBlock(type='text', text='hello')], 's')
+ assert 'current' not in agent._terminals
+ await complete(agent, 'current', 'completed', 'Operation interrupted: quoted model text')
+ response = await agent.prompt([TextContentBlock(type='text', text='retry')], 's')
+ assert response.stop_reason == 'end_turn'
+ assert agent._conn.session_update.await_args.kwargs['update'].content.text == (
+ 'Operation interrupted: quoted model text')
diff --git a/website/docs/developer-guide/acp-internals.md b/website/docs/developer-guide/acp-internals.md
index e739d808711..60c833e64c2 100644
--- a/website/docs/developer-guide/acp-internals.md
+++ b/website/docs/developer-guide/acp-internals.md
@@ -32,6 +32,17 @@ hermes acp / hermes-acp / python -m acp_adapter
Stdout is reserved for ACP JSON-RPC transport. Human-readable logs go to stderr.
+### Gateway terminal projection
+
+`acp_adapter/gateway_server.py` waits for the completion of the exact submitted
+admission. A cancelled admission returns `stopReason: cancelled`; a failed
+admission raises an ACP request error rather than reporting `end_turn`. Ordinary
+completion still returns `end_turn`. Cancellation while waiting for the model
+suppresses the local `Operation interrupted: waiting for model response` status
+message; actual partial assistant text remains visible. Terminal outcomes come
+from the gateway's structured execution result, not from assistant prose or a
+session-wide cancellation flag.
+
## Major components
### `HermesACPAgent`