Skip to content

The agent that grows with you

fix(bot): a receipt read straddling the settlement commit stays settled

Teknium44bf3ee91cd8
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 6e16c8b635f..f069ccf7de1 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -391,13 +391,18 @@ def _admission_outcome(authority, admission_id, fallback=None):
row = get_session_admission(authority.db, admission_id=admission_id)
if row is None:
return _expired_outcome(authority, fallback)
+ from gateway.session_results import admission_result
+ saved = admission_result(authority.db, admission_id)
+ if saved is not None and row['status'] != 'terminal':
+ # The settlement committed (from its worker thread) between these two reads. Its result and
+ # terminal outcome commit together, so re-read the row: the stale ``started`` row has no
+ # outcome, and a completed turn would project as a terminal ``failed`` receipt.
+ row = get_session_admission(authority.db, admission_id=admission_id)
# A terminal row without a result blob is still definitive when the outcome says the
# input never ran (cancelled) or was refused/failed; ambiguous is reserved for the
# durable unknown state and for missing evidence (completed/interrupted without result).
status = {'queued': 'queued', 'started': 'claimed', 'unknown': 'ambiguous',
'terminal': {'cancelled': 'cancelled', 'rejected': 'failed', 'failed': 'failed'}.get(row['outcome'], 'ambiguous')}[row['status']]
- from gateway.session_results import admission_result
- saved = admission_result(authority.db, admission_id)
reply = (fallback or {}).get('reply', '')
if saved is not None:
reply = saved['result'].get('final_response', '')
diff --git a/tests/gateway/test_bot_mailbox_async.py b/tests/gateway/test_bot_mailbox_async.py
index 60571522890..7b04a731c95 100644
--- a/tests/gateway/test_bot_mailbox_async.py
+++ b/tests/gateway/test_bot_mailbox_async.py
@@ -80,6 +80,36 @@ async def test_paused_receipt_rearms_on_resume_without_polling_rpc(bot, monkeypa
assert (await asyncio.to_thread(read_delivery_result, bot.home, key))['reply'] == 'pong'
+async def test_receipt_read_straddling_the_settlement_commit_projects_the_reply(bot, monkeypatch):
+ """The drain settles from a worker thread while a receipt refresh reads the admission on the
+ loop: a commit landing between the row read and the result read is still a settled reply,
+ never a terminal ``failed`` receipt that no later wake re-reads."""
+ from gateway import session_bot, session_results
+ from gateway.session_contract import SessionRef
+ from hermes_state_runtime import claim_session_input, settle_session_input
+ from tools.bot_live_delivery import read_delivery_result
+ monkeypatch.setattr(bot.authority, '_schedule', lambda ref: None)
+ key = 'f' * 32
+ receipt = await session_bot.deliver(bot.connection, dict(id=key, profile='default', message='straddle'))
+ bot.authority._pause(SessionRef(bot.authority.profile_id, receipt['session_id']), 'unknown_execution')
+ await asyncio.wait_for(asyncio.gather(*list(bot.authority._bot_receipt_tasks)), 5)
+ row = claim_session_input(bot.authority.db, epoch=bot.authority.epoch, session_id=receipt['session_id'])
+ read, settled = session_results.admission_result, []
+
+ def commit_then_read(db, admission_id):
+ if admission_id == row['admission_id'] and not settled:
+ settled.append(settle_session_input(db, epoch=bot.authority.epoch, admission_id=admission_id,
+ generation=row['generation'], outcome='completed',
+ result={'result': {'final_response': 'pong'}, 'usage': {}}))
+ return read(db, admission_id)
+ monkeypatch.setattr(session_results, 'admission_result', commit_then_read)
+ await session_bot.refresh_receipt(bot.authority, bot.home, key)
+ record = await asyncio.to_thread(read_delivery_result, bot.home, key)
+ assert settled, 'the refresh never read the admission result'
+ assert (record['status'], record['reply']) == ('settled', 'pong'), record
+
+
@pytest.mark.asyncio
async def test_adopted_result_during_unknown_projection_is_not_lost(bot, monkeypatch):
from gateway import session_bot