Skip to content

The agent that grows with you

fix(gateway): refuse a cancel that races a queued steer instead of reporting success

unsupportedpastels4725b95e1179
diff --git a/hermes_state_runtime.py b/hermes_state_runtime.py
index a54c923fccb..2c9500c449e 100644
--- a/hermes_state_runtime.py
+++ b/hermes_state_runtime.py
@@ -260,7 +260,8 @@ def cancel_session_input(db, *, epoch: int, admission_id: str) -> dict:
row = _admission(conn, admission_id)
if row['status'] == 'unknown':
raise RuntimeStoreError('unknown_execution')
- if row['status'] == 'started':
+ # A steer in transfer reads as its running claim and settles with it: as started, not cancellable.
+ if row['status'] == 'started' or row['outcome'] == STEERING:
raise RuntimeStoreError('stale_generation')
if row['status'] == 'queued':
conn.execute(_CANCEL_QUEUED, (admission_id,))
diff --git a/tests/gateway/test_prompt_queued_transfer.py b/tests/gateway/test_prompt_queued_transfer.py
index a75392f2250..6dad9cbadf6 100644
--- a/tests/gateway/test_prompt_queued_transfer.py
+++ b/tests/gateway/test_prompt_queued_transfer.py
@@ -240,6 +240,33 @@ async def test_a_late_transfer_is_never_a_follow_up_that_a_restart_can_discard(t
assert (_ledger(db)[1], handled) == (('follow-up', 'terminal', 'completed'), ['running', 'follow-up'])
+async def test_a_cancel_that_read_the_row_queued_before_a_steer_took_it_is_refused(tmp_path, monkeypatch):
+ """Reviewer repro (round 3): prompt.cancel read the row queued, then prompt.steer_queued handed it to
+ the running turn before the cancel's write. The cancel must not succeed while that transfer goes
+ on: it is refused by name, nothing stays owed, and the transfer still settles with its claim."""
+ db, authority = _authority(tmp_path, monkeypatch)
+ interactive(db)
+ turn = Turn(monkeypatch)
+ with db:
+ await _submit(authority, 'running')
+ await asyncio.wait_for(turn.claimed.wait(), 30)
+ queued = await _submit(authority, 'follow-up')
+ conn = _connection(authority)
+ stale = await authority.receipt(ACTOR, REF, queued.admission_id)
+ assert 'error' not in await _call(conn, 'prompt.steer_queued', admission_id=queued.admission_id)
+ with monkeypatch.context() as patched:
+ async def read_before_the_steer(*args):
+ return stale
+ patched.setattr(authority, 'receipt', read_before_the_steer) # both of the cancel's reads
+ reply = await _call(conn, 'prompt.cancel', admission_id=queued.admission_id)
+ turn.release.set()
+ await _drained(authority)
+ assert reply.get('error', {}).get('data', {}).get('reason') == 'stale_generation', reply
+ assert queued.admission_id not in authority.cancel_obligations
+ assert _ledger(db) == [('running', 'terminal', 'completed'), ('follow-up', 'terminal', 'steered')]
+
+
@pytest.mark.asyncio
async def test_a_transfer_stays_listed_as_steering_until_its_claim_settles(tmp_path, monkeypatch):
"""Reviewer repro (returned prompt stays hidden): a pending snapshot taken while a steer is in