Skip to content

The agent that grows with you

fix(acp): steer only this editor's own started turn

unsupportedpastelsd4e2b092a080
diff --git a/acp_adapter/gateway_commands.py b/acp_adapter/gateway_commands.py
index ef8bc06e96f..a3b22de0f42 100644
--- a/acp_adapter/gateway_commands.py
+++ b/acp_adapter/gateway_commands.py
@@ -43,13 +43,20 @@ async def run_command(agent, session_id, command, arg):
async def _steer(agent, session_id, text):
"""Guidance for this editor's running turn; None when there is none to steer (main ran an idle
- steer as a plain prompt, and a turn that ended meanwhile leaves the text for the next one)."""
- if session_id not in agent._admissions:
+ steer as a plain prompt, and a turn that ended meanwhile leaves the text for the next one).
+
+ The generation steered is our own admission's (``prompt.receipt``), never the session's latest:
+ while our prompt still waits behind another client's turn, that turn is not ours to redirect."""
+ admission_id = agent._admissions.get(session_id)
+ if admission_id is None:
return None
client = await agent._client()
+ receipt = await client.rpc('prompt.receipt', session_id=session_id, admission_id=admission_id)
+ if receipt.get('status') != 'started':
+ return None
try:
result = await client.rpc('session.steer', session_id=session_id, text=text,
- execution_generation=agent._snapshots[session_id].get('execution_generation'))
+ execution_generation=receipt['execution_generation'])
except GatewayRPCError as exc:
# Only a turn that ended meanwhile (its generation moved on) leaves the text for the next
# prompt; any other refusal (permission, unsupported control, not ready) is the answer.
diff --git a/tests/acp_adapter/test_gateway_session_commands.py b/tests/acp_adapter/test_gateway_session_commands.py
index 3683aa339cd..e0baa1baaee 100644
--- a/tests/acp_adapter/test_gateway_session_commands.py
+++ b/tests/acp_adapter/test_gateway_session_commands.py
@@ -30,21 +30,48 @@ async def test_queue_submits_its_text_as_the_next_prompt():
assert response.stop_reason == 'end_turn'
+def _receipt(status, generation=None):
+ return {'admission_id': 'mine', 'status': status, 'execution_generation': generation}
+
+
@pytest.mark.asyncio
async def test_steer_reaches_this_editors_running_turn():
async def rpc(method, **params):
+ if method == 'prompt.receipt':
+ assert params == {'session_id': 's', 'admission_id': 'mine'}, params
+ return _receipt('started', 9)
assert method == 'session.steer', method
- assert params == {'session_id': 's', 'text': 'use pytest', 'execution_generation': 7}
- return {'status': 'queued', 'text': 'use pytest', 'execution_generation': 7, 'authority_epoch': 1}
+ # Our admission's generation, not the session's latest event (7).
+ assert params == {'session_id': 's', 'text': 'use pytest', 'execution_generation': 9}
+ return {'status': 'queued', 'text': 'use pytest', 'execution_generation': 9, 'authority_epoch': 1}
agent = _agent(rpc)
- agent._admissions['s'] = 'running-turn'
+ agent._admissions['s'] = 'mine'
response = await agent.prompt([acp.text_block('/steer use pytest')], 's')
assert response.stop_reason == 'end_turn'
assert 'Steer queued for the active turn: use pytest' in \
agent._conn.session_update.await_args.kwargs['update'].content.text
+async def test_steer_while_this_editors_prompt_waits_behind_another_clients_turn_is_queued():
+ calls = []
+
+ async def rpc(method, **params):
+ calls.append(method)
+ if method == 'prompt.receipt':
+ return _receipt('queued')
+ assert method == 'prompt.submit' and params['text'] == 'use pytest', (method, params)
+ await agent._project({'session_id': 's', 'type': 'message.complete', 'admission_id': 'a',
+ 'payload': {'outcome': 'completed', 'text': 'done'}})
+ return {'admission_id': 'a'}
+
+ agent = _agent(rpc) # generation 7 is the other client's running turn
+ agent._admissions['s'] = 'mine'
+ response = await asyncio.wait_for(agent.prompt([acp.text_block('/steer use pytest')], 's'), 5)
+ assert response.stop_reason == 'end_turn' and calls == ['prompt.receipt', 'prompt.submit']
+
+
@pytest.mark.asyncio
async def test_a_refused_steer_is_reported_and_never_resent_as_a_prompt():
from hermes_cli.gateway_client import GatewayRPCError
@@ -52,13 +79,15 @@ async def test_a_refused_steer_is_reported_and_never_resent_as_a_prompt():
async def rpc(method, **params):
calls.append(method)
+ if method == 'prompt.receipt':
+ return _receipt('started', 7)
raise GatewayRPCError('unsupported_control')
agent = _agent(rpc)
- agent._admissions['s'] = 'running-turn'
+ agent._admissions['s'] = 'mine'
with pytest.raises(GatewayRPCError, match='unsupported_control'):
await agent.prompt([acp.text_block('/steer use pytest')], 's')
- assert calls == ['session.steer']
+ assert calls == ['prompt.receipt', 'session.steer']
@pytest.mark.asyncio
@@ -68,6 +97,8 @@ async def test_a_steer_for_a_turn_that_just_ended_becomes_the_next_prompt():
async def rpc(method, **params):
calls.append(method)
+ if method == 'prompt.receipt':
+ return _receipt('started', 7)
if method == 'session.steer':
raise GatewayRPCError('stale_generation')
assert method == 'prompt.submit' and params['text'] == 'use pytest', (method, params)
@@ -76,9 +107,9 @@ async def test_a_steer_for_a_turn_that_just_ended_becomes_the_next_prompt():
return {'admission_id': 'a'}
agent = _agent(rpc)
- agent._admissions['s'] = 'running-turn'
+ agent._admissions['s'] = 'mine'
response = await asyncio.wait_for(agent.prompt([acp.text_block('/steer use pytest')], 's'), 5)
- assert response.stop_reason == 'end_turn' and calls == ['session.steer', 'prompt.submit']
+ assert response.stop_reason == 'end_turn' and calls == ['prompt.receipt', 'session.steer', 'prompt.submit']
@pytest.mark.asyncio