Skip to content

The agent that grows with you

fix(gateway): one unanswerable Bot receipt never stops the owner's start

David Dudok de Wit9db9e775dd4a
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index 7e59f21682d..c0be4038f11 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -548,12 +548,19 @@ async def recover_bot_deliveries(authority):
for path, record in records:
if not record or record.get('profile_home') != str(home) or not record.get('admission_id'):
continue
- record.update(_result(authority, record))
- if record['status'] in {'queued', 'claimed'}:
- _watch_reply(authority, home, record['delivery_id'],
- record.get('retry_admission_id') or record['admission_id'])
- if record['status'] not in {'queued', 'claimed'}:
- await _maybe_retry(authority, home, path, record)
+ try:
+ record.update(_result(authority, record))
+ if record['status'] in {'queued', 'claimed'}:
+ _watch_reply(authority, home, record['delivery_id'],
+ record.get('retry_admission_id') or record['admission_id'])
+ if record['status'] not in {'queued', 'claimed'}:
+ await _maybe_retry(authority, home, path, record)
+ except RuntimeStoreError as exc:
+ # One receipt the store cannot answer must not stop the owner (and, multiplexed, every
+ # profile with it): log it, leave the file for the next start, recover the rest.
+ logging.getLogger(__name__).error('Bot delivery %s not recovered: %s',
+ record.get('delivery_id'), exc.reason)
+ continue
await write_receipt(home, record)
remember_receipt(authority, home, record)
row = authority.db.get_session_by_title('Bot Chat')
diff --git a/tests/gateway/test_bot_receipt_corruption.py b/tests/gateway/test_bot_receipt_corruption.py
index 02a94110748..ccfdb6442d9 100644
--- a/tests/gateway/test_bot_receipt_corruption.py
+++ b/tests/gateway/test_bot_receipt_corruption.py
@@ -9,7 +9,6 @@ from gateway.session_bot import _migrate, recover_bot_deliveries
from gateway.session_results import retain_result
from hermes_state import SessionDB
from hermes_state_runtime import (
- RuntimeStoreError,
admit_session_input,
begin_runtime_epoch,
claim_session_input,
@@ -87,7 +86,9 @@ async def test_recovery_skips_structurally_corrupt_canonical_neighbor(tmp_path):
@pytest.mark.asyncio
-async def test_recovery_does_not_hide_real_admission_lookup_failure(tmp_path):
+async def test_recovery_does_not_hide_real_admission_lookup_failure(tmp_path, caplog):
+ """A live session missing a receipt's admission is a storage fault: recovery logs it by
+ delivery and reason and leaves the receipt as it was, but never stops the owner's start."""
db = SessionDB(tmp_path / "state.db")
try:
db.create_session("bot", source="gui")
@@ -107,9 +108,10 @@ async def test_recovery_does_not_hide_real_admission_lookup_failure(tmp_path):
},
)
- with pytest.raises(RuntimeStoreError) as caught:
- await recover_bot_deliveries(SimpleNamespace(db=db, waiters={}))
- assert caught.value.reason == "storage_unavailable"
+ await recover_bot_deliveries(SimpleNamespace(db=db, waiters={}))
+ assert f"Bot delivery {key} not recovered: storage_unavailable" in caplog.text
+ with _locked(tmp_path) as root:
+ assert _read(root / f"{key}.json")["status"] == "canonical"
finally:
db.close()
diff --git a/tests/gateway/test_bot_receipt_tombstone_expiry.py b/tests/gateway/test_bot_receipt_tombstone_expiry.py
index e98437cee1a..043ff2e5092 100644
--- a/tests/gateway/test_bot_receipt_tombstone_expiry.py
+++ b/tests/gateway/test_bot_receipt_tombstone_expiry.py
@@ -88,6 +88,25 @@ def test_a_pending_receipt_behind_an_expired_deleted_chat_becomes_ambiguous(tmp_
db.close()
+def test_one_unanswerable_receipt_does_not_stop_the_others(tmp_path, caplog):
+ from gateway.session_bot import recover_bot_deliveries
+ db = _receipt_then_deleted(tmp_path, status="queued", days_ago=8)
+ db.create_session("live", source="cli")
+ bad = "b" * 32
+ with _locked(tmp_path) as root: # a live session missing its admission: a real storage fault
+ _write(root / f"{bad}.json", dict(status="queued", delivery_id=bad, profile_home=str(tmp_path),
+ session_id="live", admission_id="missing", principal_id="owner",
+ message="ping"))
+ try:
+ asyncio.run(recover_bot_deliveries(SimpleNamespace(db=db, waiters={})))
+ assert _receipt(tmp_path)["status"] == "ambiguous"
+ assert f"Bot delivery {bad} not recovered: storage_unavailable" in caplog.text
+ with _locked(tmp_path) as root:
+ assert _read(root / f"{bad}.json")["status"] == "queued"
+ finally:
+ db.close()
+
+
def test_the_runtime_api_starts_after_a_deleted_bot_chat_expired(tmp_path, monkeypatch):
import gateway.run_api
from gateway.run_runtime import start_gateway_runtime_api