The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-relay): drain claimed-unanswered DMs in enqueue order with fresh mail
Teknium07aa1d0095d4
diff --git a/tests/tools/test_bot_relay.py b/tests/tools/test_bot_relay.pyindex cc84a0c6b06..d2007cbf209 100644--- a/tests/tools/test_bot_relay.py+++ b/tests/tools/test_bot_relay.py@@ -612,6 +612,30 @@ def test_the_outbox_is_claimed_oldest_first(root):assert [e["message"] for e in claimed] == ["do this first", "then this"]+def test_replayed_unanswered_dm_precedes_mail_queued_after_it(root):+ """A Desktop that reconnects before the first DM's reply drains it again as a replay, beside a+ second DM queued since. Both reach the target's delivery lane in this list's order, so the+ claimed-but-unanswered one (sent first) must come first — not after the fresh outbox row."""+ first = bot_relay.enqueue_envelope(+ root, target=_target(), message="do this first",+ sender_profile="default", sender_handle="hermes",+ )+ base = bot_relay.relay_root(root)+ now = _time2.time()+ _os2.utime(base / bot_relay.OUTBOX_DIR / f"{first['id']}.json", (now - 2, now - 2))+ assert [e["id"] for e in bot_relay.claim_pending_envelopes(root)] == [first["id"]]+ second = bot_relay.enqueue_envelope(+ root, target=_target(), message="then this",+ sender_profile="default", sender_handle="hermes",+ )+ _os2.utime(base / bot_relay.OUTBOX_DIR / f"{second['id']}.json", (now - 1, now - 1))++ drained = bot_relay.claim_pending_envelopes(root)++ assert [e["message"] for e in drained] == ["do this first", "then this"]+ assert [e["id"] for e in drained] == [first["id"], second["id"]] # stable ids, no re-mint++def test_drain_delivers_fresh_envelope_under_ttl(root):env = bot_relay.enqueue_envelope(root, target=_target(), message="on time",diff --git a/tools/bot_relay.py b/tools/bot_relay.pyindex 018bd1dfad3..8ed00384cf4 100644--- a/tools/bot_relay.py+++ b/tools/bot_relay.py@@ -335,7 +335,7 @@ def claim_pending_envelopes(root: Path | str) -> list[dict]:_sweep_stale(base)ttl = _envelope_ttl_seconds()now = time.time()- out: list[dict] = []+ out: list[tuple[tuple[float, str], dict]] = []# Oldest first: the Desktop delivers each target's claimed envelopes in the order this list# gives them, so a sender's two DMs to one agent arrive in the order they were sent. Sorting# by filename ordered them by ``uuid4().hex`` — at random.@@ -345,18 +345,21 @@ def claim_pending_envelopes(root: Path | str) -> list[dict]:path.unlink()continueclaimed = base / CLAIMED_DIR / path.name+ order = _queued_at(path)with contextlib.suppress(OSError, ValueError):- os.replace(path, claimed) # atomic claim+ os.replace(path, claimed) # atomic claim; the rename keeps the enqueue mtimeenvelope = json.loads(claimed.read_text(encoding="utf-8-sig"))if not isinstance(envelope, dict):raise ValueError(f"expected a JSON object, got {type(envelope).__name__}")- out.append(envelope)- seen = {row["id"] for row in out}+ out.append((order, envelope))+ seen = {row["id"] for _order, row in out}+ # One enqueue-ordered list: a claimed-but-unanswered DM (Desktop reconnected before its reply)+ # is OLDER than mail queued since, so appending replays after fresh rows reversed them.out.extend(_replay_unanswered(root, base, seen, now))- return out+ return [envelope for _order, envelope in sorted(out, key=lambda item: item[0])]-def _replay_unanswered(root: Path | str, base: Path, seen: set, now: float) -> list[dict]:+def _replay_unanswered(root: Path | str, base: Path, seen: set, now: float) -> list[tuple[tuple[float, str], dict]]:"""``claimed/`` canonical envelopes whose reply has not landed yet, replayed on EVERY drain.A drained envelope is not "delivered": its stable id is handed out again until the target's@@ -366,9 +369,10 @@ def _replay_unanswered(root: Path | str, base: Path, seen: set, now: float) -> lauthority, which admits one turn per id and answers a retry with the existing record — never asecond turn. Once ``created_at + REPLY_WAIT_SECONDS`` passes with no reply the waiter is gone(or about to be): a ``delivery_timeout`` reply is written so it learns, and the envelope is- never handed out again.+ never handed out again. Each envelope comes back with its ``_queued_at`` enqueue key so the+ drain can merge it with fresh outbox rows in send order."""- out: list[dict] = []+ out: list[tuple[tuple[float, str], dict]] = []for path in sorted((base / CLAIMED_DIR).glob("*.json"), key=_queued_at):if (base / REPLIES_DIR / path.name).exists():continue@@ -386,7 +390,7 @@ def _replay_unanswered(root: Path | str, base: Path, seen: set, now: float) -> lf"no reply from {label} within {REPLY_WAIT_SECONDS}s of sending — the Desktop picked ""the message up but never reported a delivery. It will not be retried; resend if it matters."))continue- out.append(envelope)+ out.append((_queued_at(path), envelope))return out