Skip to content

The agent that grows with you

fix(acp): a model pick during a turn says it applies next, or why it did not

Teknium0ea563d43d9b
diff --git a/acp_adapter/gateway_commands.py b/acp_adapter/gateway_commands.py
index 7ada3a56067..eeae6decb2c 100644
--- a/acp_adapter/gateway_commands.py
+++ b/acp_adapter/gateway_commands.py
@@ -145,6 +145,9 @@ async def _mutate(agent, client, session_id, command, arg):
if status == 'cancelled':
from agent.i18n import t
reply = t('cli.model.switch_cancelled')
+ elif status == 'deferred':
+ # A pick during a running turn: stored for the next turn, nothing changed yet.
+ reply = deferred_pick_text(result)
elif status == 'preview':
# Read-only report: nothing changed, so the editor's snapshot is still current.
reply = '\n'.join(result['lines'])
@@ -176,6 +179,19 @@ async def _refresh_context(agent, client, session_id, operation):
await agent._project_usage(session_id, agent._editor_id(session_id), stats)
+def deferred_pick_text(result):
+ return f"{result.get('model') or 'The picked model'} applies from your next message."
+
+
+def refused_pick_text(update):
+ """The owner's ``session.updated`` for a deferred pick it dropped at the turn boundary."""
+ current = update.get('model') or 'the current model'
+ if update.get('provider'):
+ current += f" ({update['provider']})"
+ return (f"Model switch to {update.get('target_model') or 'the picked model'} was not applied "
+ f"({update.get('reason') or 'refused'}); still using {current}.")
+
+
def _summary_text(result):
summary = result.get('summary') or {}
return '\n'.join(filter(None, (summary.get('headline'), summary.get('token_line'), summary.get('note'))))
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index c66c02ba0fe..c5c45a725f0 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -452,6 +452,11 @@ class GatewayACPAgent(GatewayReplyMixin, acp.Agent):
raise GatewayClientError('model_switch_cancelled')
self._snapshots[session_id] = await client.rpc('session.resume', session_id=session_id)
self._mutations.acknowledge(session_id, 'model', payload)
+ if result.get('status') == 'deferred' and self._conn:
+ # A pick during a running turn lands for the next one (#139604); say so, as Desktop does.
+ from acp_adapter.gateway_commands import deferred_pick_text
+ await self._conn.session_update(session_id=self._editor_id(session_id),
+ update=acp.update_agent_message_text(deferred_pick_text(result) + "\n"))
return SetSessionModelResponse()
def _model_confirmer(self, session_id):
@@ -771,6 +776,13 @@ class GatewayACPAgent(GatewayReplyMixin, acp.Agent):
await self._conn.session_update(session_id=editor_id, update=acp.update_agent_message_text(
_tools_changed_notice(payload)))
return
+ if kind == "session.updated" and payload.get("operation") == "model" and payload.get("status") == "refused":
+ # A deferred pick that no longer resolved at the turn boundary: dropped, named here.
+ if self._conn:
+ from acp_adapter.gateway_commands import refused_pick_text
+ await self._conn.session_update(session_id=editor_id, update=acp.update_agent_message_text(
+ refused_pick_text(payload) + "\n"))
+ return
if kind == "session.info" and sid in self._snapshots and "pending" in payload:
async with self._changed:
self._snapshots[sid]["pending"] = payload["pending"]
diff --git a/tests/acp_adapter/test_gateway_model_pick_provider.py b/tests/acp_adapter/test_gateway_model_pick_provider.py
new file mode 100644
index 00000000000..495f0134e81
--- /dev/null
+++ b/tests/acp_adapter/test_gateway_model_pick_provider.py
@@ -0,0 +1,104 @@
+"""ACP ``session/set_model`` on the gateway path: the advertised ``provider:model`` id switches the
+session's provider too (#139165, #134889 MP-10), over the real ACP stdio wire and a real gateway."""
+import asyncio
+import json
+import threading
+
+import pytest
+
+from tests.acp_adapter.test_gateway_sessions import editor, viewer
+from tests.waiting import async_wait_until
+
+_SECOND = {'providers': {'second': {'name': 'second', 'base_url': 'http://127.0.0.1:9/v1',
+ 'api_key': 'loopback-only', 'default_model': 'second-model'}}}
+
+
+def _second_on_peer(daemon, model_peer):
+ """Point the named provider at the loopback peer (the daemon reads config per resolve)."""
+ home = daemon[0]
+ config = json.loads((home / 'config.yaml').read_text())
+ config['providers']['second']['base_url'] = f'http://127.0.0.1:{model_peer.server_port}/second/v1'
+ (home / 'config.yaml').write_text(json.dumps(config))
+
+
[email protected]('daemon', [_SECOND], indirect=True)
+async def test_cross_provider_pick_switches_provider_and_runs_the_next_turn_there(daemon, tmp_path, model_peer):
+ _second_on_peer(daemon, model_peer)
+ async with viewer(daemon) as ws:
+ sid = (await ws.rpc('session.create', request_id='acp-pick', source='cli', cwd=str(tmp_path)))['session_id']
+ async with editor(daemon, tmp_path) as acp:
+ await acp.rpc('initialize', protocolVersion=1, clientCapabilities={})
+ assert 'result' in await acp.rpc('session/load', cwd=str(tmp_path), sessionId=sid, mcpServers=[])
+ picked = await acp.rpc('session/set_model', sessionId=sid, modelId='custom:second:second-model')
+ assert 'result' in picked, picked
+ info = await ws.rpc('session.info', session_id=sid)
+ assert (info.get('provider'), info.get('model')) == ('custom:second', 'second-model'), info
+ turn = await acp.rpc('session/prompt', sessionId=sid, prompt=[{'type': 'text', 'text': 'WS_SHARED pick'}])
+ assert turn['result']['stopReason'] == 'end_turn', turn
+ assert model_peer.requests[-1]['model'] == 'second-model', model_peer.requests[-1]
+
+
+def _shown(frames, start=0):
+ return "".join(f["params"]["update"]["content"]["text"] for f in frames[start:]
+ if f.get("method") == "session/update"
+ and f["params"]["update"].get("sessionUpdate") == "agent_message_chunk")
+
+
+async def _pick_during_a_turn(acp, sid, model_peer):
+ model_peer.blocked, model_peer.release = threading.Event(), threading.Event()
+ running = asyncio.create_task(acp.rpc('session/prompt', sessionId=sid,
+ prompt=[{'type': 'text', 'text': 'WS_SHARED BLOCK_FIFO'}]))
+ await asyncio.to_thread(model_peer.blocked.wait, 30)
+ start = len(acp.frames)
+ picked = await acp.rpc('session/set_model', sessionId=sid, modelId='custom:second:second-model')
+ assert 'result' in picked, picked
+ return running, start
+
+
[email protected]('daemon', [_SECOND], indirect=True)
+async def test_pick_during_a_turn_says_it_applies_next_and_lands_there(daemon, tmp_path, model_peer):
+ _second_on_peer(daemon, model_peer)
+ async with viewer(daemon) as ws:
+ sid = (await ws.rpc('session.create', request_id='acp-defer', source='cli', cwd=str(tmp_path)))['session_id']
+ async with editor(daemon, tmp_path) as acp:
+ await acp.rpc('initialize', protocolVersion=1, clientCapabilities={})
+ assert 'result' in await acp.rpc('session/load', cwd=str(tmp_path), sessionId=sid, mcpServers=[])
+ running, start = await _pick_during_a_turn(acp, sid, model_peer)
+ assert 'second-model applies from your next message' in _shown(acp.frames, start), acp.frames[start:]
+ model_peer.release.set()
+ assert (await running)['result']['stopReason'] == 'end_turn'
+ assert model_peer.requests[-1]['model'] == 'local-wire-stub' # the running turn kept its model
+ turn = await acp.rpc('session/prompt', sessionId=sid, prompt=[{'type': 'text', 'text': 'WS_SHARED next'}])
+ assert turn['result']['stopReason'] == 'end_turn', turn
+ assert model_peer.requests[-1]['model'] == 'second-model', model_peer.requests[-1]
+
+
[email protected]('daemon', [_SECOND], indirect=True)
+async def test_deferred_pick_that_no_longer_resolves_is_named_to_the_editor(daemon, tmp_path, model_peer):
+ _second_on_peer(daemon, model_peer)
+ home = daemon[0]
+ async with viewer(daemon) as ws:
+ sid = (await ws.rpc('session.create', request_id='acp-refuse', source='cli', cwd=str(tmp_path)))['session_id']
+ async with editor(daemon, tmp_path) as acp:
+ await acp.rpc('initialize', protocolVersion=1, clientCapabilities={})
+ assert 'result' in await acp.rpc('session/load', cwd=str(tmp_path), sessionId=sid, mcpServers=[])
+ running, start = await _pick_during_a_turn(acp, sid, model_peer)
+ # The picked provider is removed before the boundary: the pick cannot land.
+ config = json.loads((home / 'config.yaml').read_text())
+ config.pop('providers')
+ (home / 'config.yaml').write_text(json.dumps(config))
+ model_peer.release.set()
+ assert (await running)['result']['stopReason'] == 'end_turn'
+ turn = await acp.rpc('session/prompt', sessionId=sid, prompt=[{'type': 'text', 'text': 'WS_SHARED next'}])
+ assert turn['result']['stopReason'] == 'end_turn', turn
+ await async_wait_until(lambda: 'was not applied' in _shown(acp.frames, start), timeout=15,
+ what='the named refusal of the deferred pick')
+ shown = _shown(acp.frames, start)
+ assert 'second-model was not applied' in shown and 'local-wire-stub' in shown, shown
+ assert model_peer.requests[-1]['model'] == 'local-wire-stub', model_peer.requests[-1]