Skip to content

The agent that grows with you

fix(gateway): cancel_queued publishes the terminal message.complete (R1, reported by @rahlquist)

teknium1d2058fd76369
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index d29007a790b..10dc135c157 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -278,12 +278,24 @@ class SessionAuthority:
row = cancel_session_input(self.db, epoch=self.epoch, admission_id=admission_id)
from gateway.session_ingress_media import release_admission_media
release_admission_media(self.db, admission_id)
- self._publish_pending(ref)
if before.status != 'queued' or row['status'] != 'terminal':
+ self._publish_pending(ref)
return self._receipt(row)
- # The only place a queued row becomes terminal: the delivery waiters that wait on
- # the admission (native ingress, API/webhook/hosted) settle here, or a cancelled row
- # that never reaches _drain blocks them forever.
+ # The only place a queued row becomes terminal: every observer kind that waits on
+ # the admission (native delivery, API/webhook/hosted waiters, ACP and viewer streams)
+ # settles here, or a cancelled row that never reaches _drain blocks them forever.
+ live = self.sessions[ref.session_id]
+ with live.event_stream.lock:
+ self._publish_pending(ref)
+ running = live.event_stream.execution
+ # A queued row owns no execution generation; stamp its own identity so the
+ # completion is not attributed to the turn currently running ahead of it.
+ live.event_stream.execution = {'authority_epoch': self.epoch, 'admission_id': admission_id}
+ try:
+ live.event_stream.publish(ref.session_id, {
+ 'text': '', 'content': '', 'admission_id': admission_id, 'outcome': 'cancelled'})
+ finally:
+ live.event_stream.execution = running
self.native_waiters.discard(admission_id)
waiter = self.waiters.pop(admission_id, None)
if waiter is not None and not waiter.done():
diff --git a/tests/gateway/test_session_authority_cancel_settlement.py b/tests/gateway/test_session_authority_cancel_settlement.py
index 2d8ec58b391..e52db7c1314 100644
--- a/tests/gateway/test_session_authority_cancel_settlement.py
+++ b/tests/gateway/test_session_authority_cancel_settlement.py
@@ -31,18 +31,30 @@ def _submit(authority, request_id, text=None):
@pytest.mark.asyncio
-async def test_cancel_queued_settles_the_native_delivery_waiter(tmp_path, monkeypatch):
+async def test_cancel_queued_settles_native_waiter_and_publishes_terminal_completion(tmp_path, monkeypatch):
db, authority = _authority(tmp_path, monkeypatch)
monkeypatch.setattr(authority, '_schedule', lambda ref: None)
+ frames = []
with db:
receipt = await _submit(authority, 'queued-then-cancelled')
- # The native ingress delivery waiter (session_ingress.admit_message) waits on this row.
+ # The native ingress delivery waiter (session_ingress.admit_message) and an
+ # event-stream observer (ACP prompt(), API run projections) both wait on this row.
authority.native_waiters.add(receipt.admission_id)
waiter = authority.waiters.setdefault(receipt.admission_id, asyncio.get_running_loop().create_future())
+ authority.sessions['s'].event_stream.observers.add(frames.append)
cancelled = await authority.cancel_queued(ACTOR, REF, receipt.admission_id)
assert (cancelled.status, cancelled.outcome) == ('terminal', 'cancelled')
assert waiter.done() and receipt.admission_id not in authority.native_waiters
assert receipt.admission_id not in authority.waiters
+ terminal = [f['params'] for f in frames if f['params']['type'] == 'message.complete']
+ assert len(terminal) == 1, [f['params']['type'] for f in frames]
+ assert terminal[0]['admission_id'] == receipt.admission_id
+ assert terminal[0]['payload']['outcome'] == 'cancelled'
+ assert terminal[0]['payload']['admission_id'] == receipt.admission_id
+
+ # Idempotent: a repeated cancel of the terminal row is not a second completion.
+ await authority.cancel_queued(ACTOR, REF, receipt.admission_id)
+ assert len([f for f in frames if f['params']['type'] == 'message.complete']) == 1