Skip to content

The agent that grows with you

fix(gateway): refuse cancelling steered rows; drop superseded ACP config projections

unsupportedpastelsd23579a1e53f
diff --git a/acp_adapter/gateway_server.py b/acp_adapter/gateway_server.py
index 40c70beeec6..3d3399063c8 100644
--- a/acp_adapter/gateway_server.py
+++ b/acp_adapter/gateway_server.py
@@ -1101,6 +1101,8 @@ class GatewayACPAgent(GatewayReplyMixin, acp.Agent):
return
from acp.schema import ConfigOptionUpdate
models = await asyncio.to_thread(model_state, shown)
+ if self._configs.get(sid) is not shown:
+ return # a newer projection (an event or ``_adopt``) replaced it during the build and sends its own
await self._conn.session_update(session_id=editor_id, update=ConfigOptionUpdate(
session_update="config_option_update", config_options=config_options(models, shown, shown["mode"])))
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index 2ec98b5e114..63a0f4baef8 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -816,7 +816,11 @@ class AuthorityConnection:
async def cancel(self, ref, params):
admission_id = params.get('admission_id')
receipt = await self.authority.receipt(self.actor, ref, admission_id)
- if receipt.status == 'started':
+ from hermes_state_runtime import get_session_admission
+ # A transferred steer also reads started (as its receiving claim): only its own claim is stopped here;
+ # the transfer is refused below and settles with that claim.
+ if receipt.status == 'started' and get_session_admission(
+ self.authority.db, admission_id=admission_id)['status'] == 'started':
# Stopped in this RPC: an editor that exits right after its cancel never sends a follow-up
# interrupt. A stale generation means the turn already settled.
try:
diff --git a/tests/acp_adapter/test_gateway_config_projection.py b/tests/acp_adapter/test_gateway_config_projection.py
index 7d6096149da..2305defc55c 100644
--- a/tests/acp_adapter/test_gateway_config_projection.py
+++ b/tests/acp_adapter/test_gateway_config_projection.py
@@ -1,5 +1,7 @@
"""Zed reads its pickers only from ``configOptions`` once an agent sends them, so another surface's mode
or effort change must reach it as ``config_option_update``, not ``current_mode_update`` alone."""
+import asyncio
+import threading
from unittest.mock import AsyncMock
import pytest
@@ -20,3 +22,29 @@ async def test_another_surfaces_mode_and_effort_change_resend_the_config_options
for call in agent._conn.session_update.await_args_list
if call.kwargs['update'].session_update == 'config_option_update']
assert shown == [{'mode': 'session', 'thought_level': 'medium'}, {'mode': 'session', 'thought_level': 'high'}]
+
+
+async def test_a_projection_superseded_during_its_catalog_build_never_lands_after_the_newer_one(monkeypatch):
+ """The pump's 'ask' projection is still building its model catalog when a snapshot adoption installs
+ the newer 'session' policy: the editor's last config_option_update must be the newer one."""
+ building, release = threading.Event(), threading.Event()
+
+ def model_state(info):
+ if not building.is_set(): # the pump's projection: held until the adoption has sent its own
+ building.set()
+ release.wait(30)
+ monkeypatch.setattr('acp_adapter.gateway_session_meta.model_state', model_state)
+ agent = GatewayACPAgent()
+ agent._conn = AsyncMock()
+ info = {'edit_approval_policy': 'session', 'model': 'fixture', 'reasoning_effort': 'medium'}
+ await agent._project_mode('s', 's', info)
+ older = asyncio.create_task(agent._project_mode('s', 's', {**info, 'edit_approval_policy': 'ask'}))
+ await asyncio.to_thread(building.wait, 30)
+ await agent._adopt('s', {'info': info})
+ release.set()
+ await older
+ modes = [{o.category: o.current_value for o in call.kwargs['update'].config_options}['mode']
+ for call in agent._conn.session_update.await_args_list
+ if call.kwargs['update'].session_update == 'config_option_update']
+ assert modes[-1] == 'session', modes
diff --git a/tests/gateway/test_prompt_cancel_started.py b/tests/gateway/test_prompt_cancel_started.py
index 2746a9ff9e6..36e066ff050 100644
--- a/tests/gateway/test_prompt_cancel_started.py
+++ b/tests/gateway/test_prompt_cancel_started.py
@@ -38,3 +38,52 @@ async def test_prompt_cancel_stops_a_started_turn_without_a_follow_up_interrupt(
assert 'error' not in reply, reply
assert reply['result']['admission_id'] == receipt.admission_id
assert agent.stops == ['hard'], 'the started turn kept running'
+
+
+async def test_prompt_cancel_of_a_transferred_steer_is_refused_and_leaves_its_receiving_turn_running(
+ tmp_path, monkeypatch):
+ """B was steered into running A and A never read its text. B reads started (as A's claim), but
+ cancelling it must not stop A: it is refused, and B goes back to its queue place and runs once."""
+ from tests.gateway.test_prompt_queued_controls import _call, _connection, _ledger, interactive
+ from tests.gateway.test_prompt_queued_transfer import BufferedAgent, _drained
+ from tests.waiting import async_wait_until
+ from gateway import session_finite
+
+ class StoppableAgent(BufferedAgent): # the real steer buffer, plus the stops a Stop would make
+ def __init__(self):
+ super().__init__()
+ self.stops = []
+
+ def interrupt(self, *args, **kwargs):
+ self.stops.append('soft')
+ return True
+
+ def hard_interrupt(self, *args, **kwargs):
+ self.stops.append('hard')
+
+ db, authority = _authority(tmp_path, monkeypatch)
+ interactive(db)
+ agent, claimed, release, ran = StoppableAgent(), asyncio.Event(), asyncio.Event(), []
+
+ async def execute(authority, ref, row):
+ ran.append(row['payload']['text'])
+ authority.adopt_agent(ref.session_id, row['generation'], agent)
+ claimed.set()
+ await asyncio.wait_for(release.wait(), 30)
+ return 'done'
+ monkeypatch.setattr(session_finite, 'execute_finite_admission', execute)
+ with db:
+ await _submit(authority, 'running')
+ await asyncio.wait_for(claimed.wait(), 30)
+ queued = await _submit(authority, 'follow-up')
+ conn = _connection(authority)
+ assert 'error' not in await _call(conn, 'prompt.steer_queued', admission_id=queued.admission_id)
+ reply = await _call(conn, 'prompt.cancel', admission_id=queued.admission_id)
+ release.set()
+ await _drained(authority)
+ await async_wait_until(lambda: len(ran) == 2, timeout=30, what='the returned admission ran')
+ await _drained(authority)
+ assert agent.stops == [], 'cancelling the transferred prompt stopped the turn it was steered into'
+ assert reply.get('error', {}).get('data', {}).get('reason') == 'stale_generation', reply
+ assert _ledger(db) == [('running', 'terminal', 'completed'), ('follow-up', 'terminal', 'completed')]