Skip to content

The agent that grows with you

test(cron,tools): a parseable non-dict receipt is skipped by scans and fails exact-id reads closed

teknium11f4ab2cc7813
diff --git a/tests/cron/test_bot_chat_pending.py b/tests/cron/test_bot_chat_pending.py
index 6c7b8c2e6e4..caa87c7ab09 100644
--- a/tests/cron/test_bot_chat_pending.py
+++ b/tests/cron/test_bot_chat_pending.py
@@ -169,3 +169,26 @@ def test_unreadable_deferred_receipt_does_not_block_siblings(tmp_path, monkeypat
assert [r for r in caplog.records
if "Unreadable deferred Bot Chat receipt" in r.message and "Permission denied" in r.message] and \
sum("Unreadable deferred Bot Chat receipt" in r.message for r in caplog.records) == 1
+
+
[email protected]("payload", ["42", '"oops"', "[1, 2, 3]"])
+def test_non_dict_deferred_receipt_is_skipped_by_the_drain_and_fails_exact_id_reads_closed(
+ tmp_path, monkeypatch, caplog, payload):
+ """A receipt that parses but is not a JSON object is a bad file like any other: the drain
+ and new admissions skip it (warned once, preserved as evidence) and an exact-id read of it
+ never licenses an overwrite."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ queue.defer("a" * 64, {"id": "job"}, "healthy", "", tmp_path)
+ bad = queue._root() / f"{'e' * 64}.json"
+ bad.write_text(payload, encoding="utf-8")
+ seen = []
+ monkeypatch.setattr(delivery, "_deliver_to_bot_chat", lambda j, c, p, **kw: seen.append(c))
+ with caplog.at_level("ERROR", logger=queue.logger.name):
+ queue.drain()
+ queue.drain()
+ later = queue.defer("f" * 64, {"id": "job"}, "later", "", tmp_path)
+ assert seen == ["healthy"] and later["status"] == "queued"
+ assert sum("Unreadable deferred Bot Chat receipt" in r.message for r in caplog.records) == 1
+ with pytest.raises(ValueError):
+ queue.defer("e" * 64, {"id": "job"}, "same id", "", tmp_path)
+ assert bad.read_text(encoding="utf-8") == payload
diff --git a/tests/tools/test_bot_live_owner_delivery.py b/tests/tools/test_bot_live_owner_delivery.py
index ee56c31dcd7..4402ab55ad7 100644
--- a/tests/tools/test_bot_live_owner_delivery.py
+++ b/tests/tools/test_bot_live_owner_delivery.py
@@ -158,3 +158,28 @@ def test_unreadable_ticket_keeps_exact_id_reads_fail_closed(tmp_path):
mailbox.deliver_to_live_owner(tmp_path, owner, "same id", delivery_id="e" * 32)
with pytest.raises(PermissionError):
mailbox.read_delivery_result(tmp_path, "e" * 32)
+
+
+def test_non_dict_ticket_is_skipped_by_scans_and_fails_exact_id_reads_closed(tmp_path, caplog):
+ import logging
+
+ from tools import bot_live_delivery as mailbox
+
+ owner = dict(profile_home=str(tmp_path.resolve()), session_id="chat",
+ lease_id="lease", live_session_id="live")
+ queued = mailbox.deliver_to_live_owner(tmp_path, owner, "readable", delivery_id="d" * 32)
+ bad = tmp_path / "runtime" / mailbox.DELIVERY_DIR_NAME / f"{'e' * 32}.json"
+ bad.write_text('"oops"', encoding="utf-8") # parses, but is not a record
+ with caplog.at_level(logging.WARNING, logger="tools.bot_live_delivery"):
+ admitted = mailbox.deliver_to_live_owner(tmp_path, owner, "second", delivery_id="f" * 32)
+ assert mailbox.claim_pending_delivery(tmp_path, owner)["delivery_id"] == queued["delivery_id"]
+ assert mailbox.claim_pending_delivery(tmp_path, owner)["delivery_id"] == admitted["delivery_id"]
+ assert mailbox.claim_pending_delivery(tmp_path, owner) is None
+ assert sum(r.message.startswith(f"bot_live_delivery: skipping unreadable ticket {'e' * 32}.json")
+ for r in caplog.records) == 1
+ # Malformed is not absent: exact-id reads fail closed rather than overwrite the receipt.
+ with pytest.raises(ValueError):
+ mailbox.deliver_to_live_owner(tmp_path, owner, "same id", delivery_id="e" * 32)
+ with pytest.raises(ValueError):
+ mailbox.read_delivery_result(tmp_path, "e" * 32)
+ assert bad.read_text(encoding="utf-8") == '"oops"'