The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-mode): live-delivery intents are swept, and dropped once the owner settled
John Paul Soliva1238dbc09dd2
diff --git a/tests/tools/test_bot_mode_dm.py b/tests/tools/test_bot_mode_dm.pyindex e917c65d3d5..ebbcb70cfe4 100644--- a/tests/tools/test_bot_mode_dm.py+++ b/tests/tools/test_bot_mode_dm.py@@ -939,3 +939,36 @@ def test_dm_dir_rejects_precreated_symlink(tmp_path, monkeypatch):with pytest.raises(PermissionError, match="not a directory"):bot_mode_dm._dm_dir()+++def test_cleanup_sweeps_stale_live_intents_and_keeps_fresh_ones(tmp_path, monkeypatch):+ """``<dm file>.live.json`` holds the DM plaintext and outlives its runner for retries; the+ housekeeping sweep must reap the orphans like it reaps the dm files themselves."""+ import os++ monkeypatch.setattr(bot_mode_dm, "_dm_dir", lambda: tmp_path)+ stale = tmp_path / "dm-old.txt.live.json"+ stale.write_text("{}", encoding="utf-8")+ os.utime(stale, (1, 1))+ fresh = tmp_path / "dm-new.txt.live.json"+ fresh.write_text("{}", encoding="utf-8")++ assert bot_mode_dm.cleanup_bot_dm_cache() >= 1+ assert not stale.exists()+ assert fresh.exists()+++def test_settled_live_wait_unlinks_the_intent_but_a_pending_one_keeps_it(tmp_path, monkeypatch, capsys):+ from tools import bot_live_delivery as live++ intent = tmp_path / "dm-x.txt.live.json"+ intent.write_text("{}", encoding="utf-8")+ monkeypatch.setattr(bot_mode_dm, "_LIVE_WAIT_SECONDS", 0)++ monkeypatch.setattr(live, "read_delivery_result", lambda home, did: {"status": "queued"})+ assert bot_mode_dm._wait_live_dm(str(tmp_path), "d1", intent_path=intent) == 0+ assert intent.exists(), "a pending delivery may still be retried from the same intent"++ monkeypatch.setattr(live, "read_delivery_result", lambda home, did: {"status": "settled", "reply": "ok"})+ assert bot_mode_dm._wait_live_dm(str(tmp_path), "d1", intent_path=intent) == 0+ assert not intent.exists()diff --git a/tools/bot_mode_dm.py b/tools/bot_mode_dm.pyindex 4755cc399e1..993b2b2c75f 100644--- a/tools/bot_mode_dm.py+++ b/tools/bot_mode_dm.py@@ -320,7 +320,12 @@ def cleanup_bot_dm_cache(max_age_hours: float = _DM_STALE_SECONDS / 3600, *, nowtemp_root = Path(tempfile.gettempdir())locations = [(temp_root, "hermes-dm-*.txt"), (temp_root, "hermes-relay-dm-*.txt")]with contextlib.suppress(OSError):- locations.append((_dm_dir(), "*.txt"))+ dm_dir = _dm_dir()+ locations.append((dm_dir, "*.txt"))+ # Live-delivery intents (``<dm file>.live.json``, message plaintext included) outlive+ # their runner on purpose — a retry replays the same delivery id from them — so the+ # orphans of runners that never settled are swept here too.+ locations.append((dm_dir, "*.live.json"))from tools.bot_relay import unlink_files_older_thanreturn sum(unlink_files_older_than(d, pattern, cutoff) for d, pattern in locations)@@ -453,7 +458,7 @@ def _admit_live_dm(profile_home: Path | None, dm_file: str, author: Optional[dicreturn record-def _wait_live_dm(home: str, delivery_id: str) -> int:+def _wait_live_dm(home: str, delivery_id: str, *, intent_path: "str | os.PathLike | None" = None) -> int:from tools.bot_live_delivery import read_delivery_resultdeadline = time.monotonic() + _LIVE_WAIT_SECONDS@@ -467,6 +472,10 @@ def _wait_live_dm(home: str, delivery_id: str) -> int:payload.update(status=status, delivery_id=delivery_id)if status in ("queued", "claimed", "ambiguous"):payload["detail"] = "Delivery remains pending or its outcome is unknown. Do not resend; receipt is retained."+ elif status == "settled" and intent_path is not None:+ # The intent carries the message plaintext so a retry can replay the SAME delivery id;+ # once the owner settled it nothing retries, so it goes the way the dm file does.+ _unlink_dm_file(str(intent_path))print(json.dumps(payload))return 0 if status in ("settled", "queued", "claimed") else 1@@ -507,7 +516,7 @@ def _run_delivery(argv: list[str], dm_file: str, *, stdin_file: bool,"evidence_file": dm_file}))return 1if record is not None:- return _wait_live_dm(record["profile_home"], record["delivery_id"])+ return _wait_live_dm(record["profile_home"], record["delivery_id"], intent_path=dm_file + ".live.json")try:from tools.bot_relay import delivery_env