The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a completion fold never reorders past a prompt or stores results twice
Teknium3030077ef0f4
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex 61867bca2ed..cb9b96691d7 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -491,9 +491,9 @@ class SessionAuthority:if waiter is not None and not waiter.done():waiter.set_exception(RuntimeStoreError(reason))- async def admit_automation(self, adapter, event, identity, replaces=None):+ async def admit_automation(self, adapter, event, identity, replaces=None, fold=False):from gateway.session_automation import admit_automation- return await admit_automation(self, adapter, event, identity, replaces)+ return await admit_automation(self, adapter, event, identity, replaces, fold)async def admit_native(self, event):"""Await current connector policy, then commit before ACK or scheduling execution."""diff --git a/gateway/session_automation.py b/gateway/session_automation.pyindex 7621c64380a..da5ed97c5c4 100644--- a/gateway/session_automation.py+++ b/gateway/session_automation.py@@ -111,7 +111,8 @@ async def _admit_folded_completion(authority, adapter, event, evt, identity):unfolded = event.text, dict(event.metadata)_fold_completion(event, waiting, identity)try:- return await authority.admit_automation(adapter, event, identity, replaces=waiting['admission_id'])+ return await authority.admit_automation(adapter, event, identity, replaces=waiting['admission_id'],+ fold=True)except RuntimeStoreError as exc:if exc.reason != 'stale_generation':raise@@ -229,6 +230,29 @@ def _replace_queued(admission_id, session_id, replaced):return guard+_FOLDED_TEXT = '[folded into a later completion batch]'+++def _fold_queued(admission_id, session_id, replaced):+ """``_replace_queued`` for a completion fold, which must still be the FIFO tail when it commits+ (a prompt admitted since the tail was read keeps the earlier result ahead of it), and whose+ replaced row drops its text: the batch carries it, so each fold must not store it again."""+ replace = _replace_queued(admission_id, session_id, replaced)++ def guard(conn):+ row = conn.execute("SELECT seq, payload_json FROM session_admissions WHERE admission_id=? "+ "AND target_session_id=? AND status='queued'", (admission_id, session_id)).fetchone()+ if row is None or conn.execute("SELECT 1 FROM session_admissions WHERE target_session_id=? "+ "AND status='queued' AND seq>?", (session_id, row[0])).fetchone():+ raise RuntimeStoreError('stale_generation')+ replace(conn)+ payload = json.loads(row[1])+ payload['text'] = _FOLDED_TEXT+ conn.execute('UPDATE session_admissions SET payload_json=? WHERE admission_id=?',+ (json.dumps(payload, separators=(',', ':'), ensure_ascii=False), admission_id))+ return guard++async def withdraw_process_ticks(runner, event):"""A process's completion is in the session: its ticks still queued there would run after it asstale "still running" turns, and the completion carries the final output, so they are cancelled.@@ -569,7 +593,7 @@ def restore_local_automation(authority, ref, row):return event-async def admit_automation(authority, adapter, event, identity, replaces=None):+async def admit_automation(authority, adapter, event, identity, replaces=None, fold=False):"""``replaces``: a queued automation row of the same session this input supersedes (a livenesstick carrying the output of the one still waiting). Its cancellation commits in this admission'sown transaction, so neither lands without the other."""@@ -596,7 +620,8 @@ async def admit_automation(authority, adapter, event, identity, replaces=None):return await tracked_write(authority, partial(admit_session_input, authority.db, epoch=authority.epoch, principal_id='automation:' + entry.session_key,session_id=ref.session_id, request_id=identity, payload=deepcopy(payload),- _authorize_write=authority._admission_gate(replaces and _replace_queued(replaces, ref.session_id, replaced))),+ _authorize_write=authority._admission_gate(replaces and (_fold_queued if fold else _replace_queued)(+ replaces, ref.session_id, replaced))),then=admitted, ordered=True, after=partial(authority._schedule_admitted, ref))diff --git a/tests/gateway/test_completion_fold.py b/tests/gateway/test_completion_fold.pyindex 599dec6f434..d041fc38724 100644--- a/tests/gateway/test_completion_fold.py+++ b/tests/gateway/test_completion_fold.py@@ -42,6 +42,9 @@ async def _discord_session(tmp_path, monkeypatch):def queued():return [row for row in list_session_admissions(authority.db, session_id=owner) if row['status'] == 'queued']+ def history():+ return list_session_admissions(authority.db, session_id=owner, pending_only=False)+ complete.history = historyreturn authority, human, complete, queued@@ -59,15 +62,24 @@ async def test_completions_queued_behind_a_turn_fold_into_one_turn(tmp_path, monassert all(f'{name} finished' in text for name in ('proc_a', 'proc_b', 'proc_c'))assert text.count('INTERNAL NOTIFICATION') == 1assert len(rows[-1]['payload']['native_text_v1']['automation']['identities']) == 3+ # Each replaced row drops its text (the batch carries it): storage grows with N, not N^2.+ folded = [row for row in complete.history() if row['outcome'] == 'cancelled']+ assert [row['payload']['text'] for row in folded] == ['[folded into a later completion batch]'] * 2finally:authority.db.close()@pytest.mark.asyncio-async def test_a_prompt_queued_between_completions_keeps_them_apart(tmp_path, monkeypatch):[email protected]('stale_read', [False, True])+async def test_a_prompt_queued_between_completions_keeps_them_apart(tmp_path, monkeypatch, stale_read):+ """``stale_read``: the prompt commits after the tail was read but before the fold is written."""+ from gateway import session_automationauthority, human, complete, queued = await _discord_session(tmp_path, monkeypatch)try:await complete('proc_a')+ if stale_read:+ tail = queued()[-1]+ monkeypatch.setattr(session_automation, '_waiting_completion', lambda *args: tail)await human('queued follow-up')await complete('proc_b')rows = queued()