The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): prompt.cancel stops a row the drain claimed under it
Tekniuma514c9e2e59a
diff --git a/gateway/session_controls.py b/gateway/session_controls.pyindex 7e25f40adf1..b8e4f30f6e9 100644--- a/gateway/session_controls.py+++ b/gateway/session_controls.py@@ -492,7 +492,15 @@ class AuthorityConnection:return outasync def cancel(self, ref, params):- return asdict(await self.authority.cancel_queued(self.actor, ref, params.get('admission_id')))+ admission_id = params.get('admission_id')+ if (await self.authority.receipt(self.actor, ref, admission_id)).status != 'queued':+ return asdict(await self.authority.cancel_queued(self.actor, ref, admission_id)) # started: unaffected+ # Queued at this read, claimed by the drain before the cancel commit: Stop that exact claim (as+ # API and ACP do) instead of refusing while the withdrawn prompt runs.+ row = await self.authority.cancel_or_claimed(self.actor, ref, admission_id)+ if row is not None:+ await self.interrupt(ref, {'execution_generation': row['generation']})+ return asdict(await self.authority.receipt(self.actor, ref, admission_id))async def resolve_unknown(self, ref, params):if set(params) != {'session_id', 'admission_id', 'execution_generation'}:diff --git a/tests/gateway/test_prompt_cancel_claimed.py b/tests/gateway/test_prompt_cancel_claimed.pynew file mode 100644index 00000000000..9716ea978e4--- /dev/null+++ b/tests/gateway/test_prompt_cancel_claimed.py@@ -0,0 +1,33 @@+"""prompt.cancel on a row the drain claims between the client's queued read and the cancel commit+stops that claim (as API and ACP do) instead of refusing while the withdrawn prompt runs (N113)."""+import pytest++from tests.gateway.test_canonical_stop_hard_cancel import Agent+from tests.gateway.test_session_authority_cancel_settlement import REF, _authority, _submit+++async def test_prompt_cancel_racing_the_claim_stops_the_claimed_turn(tmp_path, monkeypatch):+ from gateway import session_authority+ from gateway.session_controls import AuthorityConnection+ from hermes_state_runtime import claim_session_input++ db, authority = _authority(tmp_path, monkeypatch)+ monkeypatch.setattr(authority, '_schedule', lambda ref: None)+ agent = Agent()+ authority.runner._cached_agent_for = {'route': agent}.get+ real_cancel = session_authority.cancel_session_input++ def claimed_first(db, **kwargs):+ # The drain's claim commits between the client's queued read and the cancel transaction.+ claim_session_input(db, epoch=authority.epoch, session_id='s')+ return real_cancel(db, **kwargs)+ monkeypatch.setattr(session_authority, 'cancel_session_input', claimed_first)+ with db:+ receipt = await _submit(authority, 'withdrawn')+ conn = AuthorityConnection(authority, object(), {'user_id': 'human'})+ reply = await conn.dispatch({'id': 1, 'method': 'prompt.cancel', 'params': {+ 'session_id': REF.session_id, 'admission_id': receipt.admission_id}})+ assert 'error' not in reply, f'prompt.cancel refused a live claim: {reply}'+ assert reply['result']['status'] == 'started'+ assert agent.stops == ['hard'], 'the withdrawn prompt kept running'