Skip to content

The agent that grows with you

fix(gateway): isolate malformed bot receipt schemas

Brian Fernstrom4be62f9ca68d
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index ceba3850ccb..dbd8fdb8eeb 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -16,14 +16,54 @@ from hermes_state_runtime import RuntimeStoreError, get_session_admission
from tools.bot_live_delivery import _delivery_id, _locked, _read, _write
+_CANONICAL_RECEIPT_STATUSES = frozenset({
+ 'canonical', 'queued', 'claimed', 'ambiguous', 'settled', 'failed', 'cancelled',
+})
+
+
+def _canonical_record_error(path, record):
+ """Return why an admission-backed receipt is unsafe for bulk recovery."""
+ delivery_id = record.get('delivery_id')
+ if not isinstance(delivery_id, str) or delivery_id != path.stem:
+ return 'delivery id does not match filename'
+ try:
+ _delivery_id(delivery_id)
+ except ValueError:
+ return 'delivery id is invalid'
+ required_strings = ('admission_id', 'profile_home', 'session_id', 'principal_id')
+ missing = [key for key in required_strings
+ if not isinstance(record.get(key), str) or not record[key]]
+ if missing:
+ return 'missing canonical fields: ' + ', '.join(missing)
+ if not isinstance(record.get('message'), str):
+ return 'message is not a string'
+ status = record.get('status')
+ if not isinstance(status, str) or status not in _CANONICAL_RECEIPT_STATUSES:
+ return f'unknown canonical status {status!r}'
+ return None
+
+
+def _record_shape_error(path, record):
+ """Validate the known canonical or legacy receipt shape for directory scans."""
+ if 'admission_id' in record or record.get('status') == 'canonical' or 'principal_id' in record:
+ return _canonical_record_error(path, record)
+ if 'owner' in record:
+ from tools.bot_live_delivery import _ticket_shape_error
+ return _ticket_shape_error(path, record)
+ return 'unknown receipt schema'
+
+
def _scan_records(root):
- """Bulk mailbox scan: isolate unreadable records without weakening exact-id reads."""
+ """Bulk mailbox scan: isolate unreadable/malformed records; exact-id reads still fail closed."""
for path in root.glob('*.json'):
try:
record = _read(path)
- except (OSError, ValueError):
+ problem = None if record is None else _record_shape_error(path, record)
+ except (OSError, ValueError) as exc:
+ record, problem = None, str(exc)
+ if problem is not None:
logging.getLogger(__name__).warning(
- "Skipping unreadable Bot mailbox receipt %s", path, exc_info=True)
+ "Skipping malformed Bot mailbox receipt %s (%s)", path, problem)
continue
if record is not None:
yield path, record
diff --git a/tests/gateway/test_bot_receipt_corruption.py b/tests/gateway/test_bot_receipt_corruption.py
new file mode 100644
index 00000000000..07a81ab6a52
--- /dev/null
+++ b/tests/gateway/test_bot_receipt_corruption.py
@@ -0,0 +1,183 @@
+"""Bulk Bot receipt recovery isolates bad files without weakening exact-id failures."""
+
+from contextlib import contextmanager
+from types import SimpleNamespace
+
+import pytest
+
+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,
+)
+from tools.bot_live_delivery import _locked, _read, _write
+
+
+def _settled_admission(db, epoch, request_id, text, reply):
+ admission = admit_session_input(
+ db,
+ epoch=epoch,
+ principal_id="owner",
+ session_id="bot",
+ request_id=request_id,
+ payload={"text": text},
+ )
+ row = claim_session_input(db, epoch=epoch, session_id="bot")
+ retain_result(
+ db,
+ epoch=epoch,
+ row=row,
+ result={"result": {"final_response": reply}, "usage": {}},
+ )
+ return admission
+
+
+async def test_recovery_skips_structurally_corrupt_canonical_neighbor(tmp_path):
+ db = SessionDB(tmp_path / "state.db")
+ try:
+ db.create_session("bot", source="gui")
+ epoch = begin_runtime_epoch(db, instance_id="owner")
+ healthy = _settled_admission(db, epoch, "healthy", "hello", "recovered")
+ damaged = _settled_admission(db, epoch, "damaged", "bad", "should-not-be-read")
+
+ healthy_key = "a" * 32
+ damaged_key = "b" * 32
+ with _locked(tmp_path) as root:
+ _write(
+ root / f"{healthy_key}.json",
+ {
+ "status": "canonical",
+ "admission_id": healthy["admission_id"],
+ "delivery_id": healthy_key,
+ "profile_home": str(tmp_path),
+ "session_id": "bot",
+ "principal_id": "owner",
+ "message": "hello",
+ },
+ )
+ _write(
+ root / f"{damaged_key}.json",
+ {
+ "status": "canonical",
+ "admission_id": damaged["admission_id"],
+ "delivery_id": damaged_key,
+ "profile_home": str(tmp_path),
+ "session_id": "bot",
+ "principal_id": "owner",
+ # Valid JSON and a real admission, but structurally incomplete.
+ },
+ )
+
+ await recover_bot_deliveries(SimpleNamespace(db=db, waiters={}))
+
+ with _locked(tmp_path) as root:
+ recovered = _read(root / f"{healthy_key}.json")
+ skipped = _read(root / f"{damaged_key}.json")
+ assert recovered["status"] == "settled"
+ assert recovered["reply"] == "recovered"
+ assert skipped["status"] == "canonical"
+ assert "message" not in skipped
+ finally:
+ db.close()
+
+
+async def test_recovery_does_not_hide_real_admission_lookup_failure(tmp_path):
+ db = SessionDB(tmp_path / "state.db")
+ try:
+ db.create_session("bot", source="gui")
+ begin_runtime_epoch(db, instance_id="owner")
+ key = "c" * 32
+ with _locked(tmp_path) as root:
+ _write(
+ root / f"{key}.json",
+ {
+ "status": "canonical",
+ "admission_id": "missing-admission",
+ "delivery_id": key,
+ "profile_home": str(tmp_path),
+ "session_id": "bot",
+ "principal_id": "owner",
+ "message": "valid receipt, missing durable admission",
+ },
+ )
+
+ with pytest.raises(RuntimeStoreError) as caught:
+ await recover_bot_deliveries(SimpleNamespace(db=db, waiters={}))
+ assert caught.value.reason == "storage_unavailable"
+ finally:
+ db.close()
+
+
+async def test_legacy_migration_skips_structurally_corrupt_neighbor(monkeypatch, tmp_path):
+ healthy_key = "d" * 32
+ damaged_key = "e" * 32
+ owner = {
+ "profile_home": str(tmp_path),
+ "session_id": "legacy-session",
+ "lease_id": "departed-owner",
+ "live_session_id": "old-native",
+ }
+ with _locked(tmp_path) as root:
+ _write(
+ root / f"{healthy_key}.json",
+ {
+ "id": healthy_key,
+ "delivery_id": healthy_key,
+ "status": "queued",
+ "created_at": 1,
+ "sequence": 1,
+ "owner": owner,
+ "message": "healthy legacy",
+ },
+ )
+ _write(root / f"{damaged_key}.json", {"owner": {}})
+
+ authority = SimpleNamespace(
+ db=SimpleNamespace(get_compression_tip=lambda session_id: "tip"),
+ )
+ ref = SimpleNamespace(session_id="tip")
+ live = object()
+ entry = SimpleNamespace(session_id="tip")
+ monkeypatch.setattr(
+ "gateway.session_bot._target",
+ lambda authority, actor: (ref, live, entry),
+ )
+
+ @contextmanager
+ def inactive_session(*args, **kwargs):
+ yield False
+
+ monkeypatch.setattr(
+ "hermes_cli.active_sessions.active_session_liveness_guard",
+ inactive_session,
+ )
+
+ migrated = []
+
+ async def fake_admit(
+ authority,
+ actor,
+ home,
+ root,
+ key,
+ message,
+ ref,
+ live,
+ entry,
+ author=None,
+ legacy=None,
+ ):
+ migrated.append((key, message, legacy["status"]))
+
+ monkeypatch.setattr("gateway.session_bot._admit", fake_admit)
+
+ await _migrate(authority, object(), tmp_path, root)
+
+ assert migrated == [(healthy_key, "healthy legacy", "queued")]