The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot): a queue_full refusal withdraws the Bot Chat delivery's pre-admission pin
Teknium57a43c230faa
diff --git a/gateway/session_bot.py b/gateway/session_bot.pyindex b09f630629d..176eff1030f 100644--- a/gateway/session_bot.py+++ b/gateway/session_bot.py@@ -15,7 +15,7 @@ from gateway.platforms.event import MessageEventfrom hermes_state_runtime import RuntimeStoreError, get_session_admissionfrom tools.bot_live_delivery import _delivery_id, _readfrom gateway.session_bot_mailbox import (mailbox_lock, read_receipt, write_receipt,- scan_receipts, remember_receipt, track_receipt_task)+ scan_receipts, remember_receipt, track_receipt_task, withdraw_pin)_CANONICAL_RECEIPT_STATUSES = frozenset({@@ -298,7 +298,15 @@ async def _maybe_retry(authority, home, path, record):record['retry'] = {'identity': identity}record.update(status='queued', reply='')await write_receipt(home, record)- receipt = await authority.admit_automation(authority.runner._adapter_for_source(live.source), event, identity)+ try:+ receipt = await authority.admit_automation(authority.runner._adapter_for_source(live.source), event, identity)+ except RuntimeStoreError as exc:+ if exc.reason != 'queue_full':+ raise+ # Nothing was admitted: keep the delivery pending (as for a drain) so the next wake or+ # delivery performs the same derived retry once the FIFO has room.+ record.update(status='claimed', reply='')+ returnrecord.update(_result(authority, record))if record['status'] in {'queued', 'claimed'}:_watch_reply(authority, home, record['delivery_id'], receipt.admission_id)@@ -524,7 +532,13 @@ async def _admit(authority, actor, home, root, key, message, ref, live, entry, aif legacy is not None:return Noneraise RuntimeStoreError('admission_conflict')- receipt = await authority.admit_automation(authority.runner._adapter_for_source(live.source), event, 'bot:' + key)+ try:+ receipt = await authority.admit_automation(authority.runner._adapter_for_source(live.source), event, 'bot:' + key)+ except RuntimeStoreError as exc:+ if exc.reason == 'queue_full' and legacy is None:+ # Refused inside the admit transaction before any INSERT: nothing to recover.+ await withdraw_pin(home, key)+ raiserecord.update(status='canonical', admission_id=receipt.admission_id)record.update(_result(authority, record))if record['status'] in {'queued', 'claimed'}:diff --git a/gateway/session_bot_mailbox.py b/gateway/session_bot_mailbox.pyindex 59f079b316e..ab7bb05dbcf 100644--- a/gateway/session_bot_mailbox.py+++ b/gateway/session_bot_mailbox.py@@ -86,6 +86,20 @@ def _scan_receipts(home):return root, list(_scan_records(root))+def _withdraw_pin(home, key):+ with _locked(home) as root:+ path = root / f'{key}.json'+ current = _read(path)+ if current is not None and current.get('status') == 'ambiguous' and not current.get('admission_id'):+ path.unlink()+++async def withdraw_pin(home, key):+ """Drop a pre-admission pin the FIFO definitely refused (nothing committed), only while it is+ still that pin, so the same-id retry admits instead of answering ``unknown_execution``."""+ return await _mailbox_io(_withdraw_pin, home, key)++async def scan_receipts(home):return await _mailbox_io(_scan_receipts, home)diff --git a/tests/gateway/test_bot_receipt_queue_full.py b/tests/gateway/test_bot_receipt_queue_full.pynew file mode 100644index 00000000000..4d1bed1db01--- /dev/null+++ b/tests/gateway/test_bot_receipt_queue_full.py@@ -0,0 +1,34 @@+"""A Bot Chat delivery the FIFO refuses ``queue_full`` withdraws its pre-admission pin, so the+same-id retry admits once the backlog drains instead of answering ``unknown_execution`` forever."""+import contextlib++import pytest++from gateway.session_contract import Principal, SessionRef+from hermes_state_runtime import (ADMISSION_QUEUE_MAX, RuntimeStoreError, admit_session_input,+ list_session_admissions)++pytest_plugins = ['tests.gateway.test_session_bot_retry']++KEY = 'b' * 32+++async def test_queue_full_refusal_withdraws_the_pin_and_the_retry_admits(bot):+ from gateway.session_bot import deliver+ from tools.bot_live_delivery import read_delivery_result+ authority = bot.authority+ authority._schedule = lambda ref: None # hold the backlog: nothing drains until we cancel it+ fillers = [admit_session_input(authority.db, epoch=authority.epoch, principal_id='uid:1000',+ session_id=bot.chat, request_id=f'fill{i}', payload={'text': f'f{i}'})+ for i in range(ADMISSION_QUEUE_MAX)]+ with pytest.raises(RuntimeStoreError, match='queue_full'):+ await deliver(bot.connection, dict(id=KEY, profile='default', message='ping'))+ assert read_delivery_result(bot.home, KEY) is None+ control = Principal('uid:1000', 'default', frozenset({'session:submit', 'session:read', 'session:control'}), 'native')+ for row in fillers:+ with contextlib.suppress(RuntimeStoreError):+ await authority.cancel_queued(control, SessionRef('default', bot.chat), row['admission_id'])+ retried = await deliver(bot.connection, dict(id=KEY, profile='default', message='ping'))+ assert 'error' not in retried and retried['admission_id']+ assert [r['request_id'] for r in list_session_admissions(authority.db, session_id=bot.chat)] == ['bot:' + KEY]