Skip to content

The agent that grows with you

fix(gateway): a steer write that may have committed keeps its claim's fence

unsupportedpastels5fdcf870f5e6
diff --git a/gateway/session_queued_controls.py b/gateway/session_queued_controls.py
index 85b1ef79196..6e2c703c911 100644
--- a/gateway/session_queued_controls.py
+++ b/gateway/session_queued_controls.py
@@ -122,6 +122,12 @@ async def steer_queued(connection, ref, params):
authority.authorize(connection.actor, ref, 'session:control')
receipt, row = await _queued(connection, ref, admission_id)
if row['status'] == 'terminal' and row['outcome'] in (STEERING, 'steered'):
+ if row['outcome'] == STEERING:
+ # A transfer committed under an uncertain failure is still fenced to its claim: deliver it.
+ with authority.sessions[ref.session_id].event_stream.lock:
+ agent = _receiving_agent(authority, ref.session_id, row['generation'])
+ if agent is not None:
+ _deliver(authority, ref.session_id, row['generation'], agent, admission_id, row['payload']['text'])
return asdict(receipt) # an exact retry replays the committed transfer
if receipt.status != 'queued':
raise RuntimeStoreError('admission_started')
@@ -144,7 +150,9 @@ async def steer_queued(connection, ref, params):
# The storage wait runs with no lock held; the registration above is the claim's fence.
committed, fresh = await tracked_write(authority, partial(
steer_session_input, authority.db, epoch=authority.epoch, admission_id=admission_id, generation=generation))
- except BaseException:
+ except RuntimeStoreError:
+ # A definite refusal: nothing committed. Any other failure may follow a commit, so the fence
+ # stays and the claim's settlement decides the row (a no-op when nothing committed).
with live.event_stream.lock:
_unregister(authority, session_id, generation, admission_id)
raise
diff --git a/tests/gateway/test_prompt_queued_transfer.py b/tests/gateway/test_prompt_queued_transfer.py
index e1e937cfdb3..ab2708a12d7 100644
--- a/tests/gateway/test_prompt_queued_transfer.py
+++ b/tests/gateway/test_prompt_queued_transfer.py
@@ -129,6 +129,42 @@ async def test_a_steer_settles_steered_only_when_a_drain_of_the_turn_took_it(tmp
assert ran == ['running', 'follow-up'], 'the returned prompt runs once, as its own turn'
[email protected]('retry', [False, True])
+async def test_a_steer_write_that_committed_but_reported_failure_is_still_settled_by_its_claim(
+ tmp_path, monkeypatch, retry):
+ """Reviewer repro: the transfer commits, then the write reports a failure. The claim's fence must
+ survive it: an exact retry delivers the committed transfer, and without one the claim's settlement
+ puts the undelivered prompt back in its queue place (never left ``steering`` until a restart)."""
+ db, authority = _authority(tmp_path, monkeypatch)
+ interactive(db)
+ turn = Turn(monkeypatch)
+ original = session_queued_controls.steer_session_input
+
+ def committed_then_failed(*args, **kwargs):
+ original(*args, **kwargs)
+ raise sqlite3.OperationalError('disk I/O error')
+ monkeypatch.setattr(session_queued_controls, 'steer_session_input', committed_then_failed)
+ with db:
+ await _submit(authority, 'running')
+ await asyncio.wait_for(turn.claimed.wait(), 30)
+ queued = await _submit(authority, 'follow-up')
+ conn = _connection(authority)
+ failed = await _call(conn, 'prompt.steer_queued', admission_id=queued.admission_id)
+ assert 'error' in failed, failed
+ if retry:
+ again = await _call(conn, 'prompt.steer_queued', admission_id=queued.admission_id)
+ assert (again['result']['status'], again['result']['outcome']) == ('started', None), again
+ turn.release.set()
+ await _drained(authority)
+ if retry:
+ assert (turn.agent.steers, turn.ran) == (['follow-up'], ['running'])
+ assert _ledger(db) == [('running', 'terminal', 'completed'), ('follow-up', 'terminal', 'steered')]
+ else:
+ assert (turn.agent.steers, turn.ran) == ([], ['running', 'follow-up'])
+ assert _ledger(db) == [('running', 'terminal', 'completed'), ('follow-up', 'terminal', 'completed')]
+
+
@pytest.mark.asyncio
async def test_a_turn_that_ends_between_the_steer_commit_and_delivery_gets_its_prompt_back(tmp_path, monkeypatch):
"""The receiving execution returns after the steer committed but before the text reached its agent: