Skip to content

The agent that grows with you

fix(acp): the editor's Stop during a remote image download cancels the prompt

Teknium00ab80983de0
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index 9d621a3ed28..f2ac61c6cdd 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -624,10 +624,19 @@ class GatewayACPAgent(acp.Agent):
reused = retained if retained is not None and retained[0] == retry_key else None
if reused is not None:
staged = reused[2]
+ elif _has_remote_image(content):
+ # Only a remote image fetch leaves the loop. The editor's Stop during that network wait is
+ # held like one during the submit itself, and the prompt then ends without being sent.
+ self._submitting.add(session_id)
+ try:
+ staged = await asyncio.to_thread(_stage_user_content, content)
+ finally:
+ self._submitting.discard(session_id)
+ if session_id in self._pending_cancels:
+ self._pending_cancels.discard(session_id)
+ return PromptResponse(stop_reason='cancelled')
else:
- # Only a remote image fetch leaves the loop; every other prompt stages inline, as before.
- staged = (await asyncio.to_thread(_stage_user_content, content)) \
- if _has_remote_image(content) else _stage_user_content(content)
+ staged = _stage_user_content(content)
text, attachments = staged
command = _slash_command(text) if not attachments else None
if command is not None:
diff --git a/tests/acp_adapter/test_gateway_remote_image_submit.py b/tests/acp_adapter/test_gateway_remote_image_submit.py
index bb2e2d5a4f0..d0e79ed5c2f 100644
--- a/tests/acp_adapter/test_gateway_remote_image_submit.py
+++ b/tests/acp_adapter/test_gateway_remote_image_submit.py
@@ -1,6 +1,7 @@
"""A remote image URL is fetched once per logical prompt: an un-acked submit's retry reuses what the first
attempt staged (never a re-download that could change the content and so the input_id), and the editor's
Stop during the download keeps the prompt from being submitted at all (Greptile review on #140188)."""
+import asyncio
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
@@ -83,3 +84,20 @@ async def test_unacked_remote_image_retry_reuses_the_first_staging(tmp_path, mon
assert retry['text'] == first['text'] == 'describe'
assert retry['attachments'] == first['attachments']
+
+async def test_stop_during_a_remote_image_download_submits_nothing(tmp_path, monkeypatch, host):
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ _allow_loopback(monkeypatch, True)
+ host['gate'] = threading.Event()
+ agent = GatewayACPAgent()
+ agent._gateway = _LosesFirstAck(agent)
+ agent._snapshots['s'] = {}
+ task = asyncio.create_task(agent.prompt(_prompt(host['url']), 's'))
+ assert await asyncio.to_thread(host['started'].wait, 10)
+ await agent.cancel('s')
+ host['gate'].set()
+ response = await asyncio.wait_for(task, 10)
+ assert response.stop_reason == 'cancelled'
+ assert agent._gateway.calls == []
+ assert 's' not in agent._pending_cancels and 's' not in agent._submitting