Skip to content

The agent that grows with you

fix(acp): name the busy-session refusal of a mode pick in the editor

Tekniumdc995bbd6355
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index b650ff9986e..192bf27bc9b 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -583,7 +583,17 @@ class GatewayACPAgent(GatewayReplyMixin, acp.Agent):
client = await self._client()
payload = {"policy": policy}
self._unacked.pop(session_id, None)
- await self._mutations.apply(client, session_id, "edit_policy", payload)
+ try:
+ await self._mutations.apply(client, session_id, "edit_policy", payload)
+ except GatewayClientError as exc:
+ if str(exc) not in {"session_busy", "unknown_execution"}:
+ raise
+ # A turn is running: named in the transcript (as a deferred model pick is) and the reply.
+ text = "Mode changes apply when the session is idle; try again after this turn."
+ if self._conn:
+ await self._conn.session_update(session_id=self._editor_id(session_id),
+ update=acp.update_agent_message_text(text + "\n"))
+ raise acp.RequestError(-32603, text) from exc
self._mutations.acknowledge(session_id, "edit_policy", payload)
self._modes[session_id] = EDIT_APPROVAL_POLICY_TO_MODE[policy]
diff --git a/tests/acp_adapter/test_gateway_session_commands.py b/tests/acp_adapter/test_gateway_session_commands.py
index d1133ee75e7..f6d1bea9f63 100644
--- a/tests/acp_adapter/test_gateway_session_commands.py
+++ b/tests/acp_adapter/test_gateway_session_commands.py
@@ -138,6 +138,29 @@ async def test_modes_and_the_config_option_set_the_owners_edit_policy():
assert sent == []
+
[email protected]('pick', [lambda a: a.set_session_mode('dont_ask', 's'),
+ lambda a: a.set_config_option('edit_approval_policy', 's', 'session')])
+async def test_a_mode_pick_during_a_turn_is_refused_with_the_reason(pick):
+ """The owner changes the edit policy on an idle session only; the editor is told why, by name."""
+ from hermes_cli.gateway_client import GatewayRPCError
+
+ async def rpc(method, **params):
+ if method == 'session.resume':
+ return {'revision': 3, 'execution_generation': 7}
+ raise GatewayRPCError('session_busy')
+
+ agent = _agent(rpc)
+ agent._modes['s'] = 'default'
+ with pytest.raises(acp.RequestError) as refused:
+ await pick(agent)
+ assert 'idle' in str(refused.value.data or refused.value), refused.value
+ shown = [call.kwargs['update'] for call in agent._conn.session_update.await_args_list]
+ assert any('idle' in getattr(getattr(u, 'content', None), 'text', '') for u in shown), shown
+ assert agent._modes['s'] == 'default'
+
+
def _context_shown(agent):
return [(call.kwargs['update'].size, call.kwargs['update'].used)
for call in agent._conn.session_update.await_args_list