The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-relay): settle pre-upgrade claimed envelopes instead of stranding them
Tekniumc2061c458f31
diff --git a/tests/tools/test_bot_relay.py b/tests/tools/test_bot_relay.pyindex d2007cbf209..a68eb2d6066 100644--- a/tests/tools/test_bot_relay.py+++ b/tests/tools/test_bot_relay.py@@ -636,6 +636,32 @@ def test_replayed_unanswered_dm_precedes_mail_queued_after_it(root):assert [e["id"] for e in drained] == [first["id"], second["id"]] # stable ids, no re-mint+def test_pre_upgrade_claim_settles_unknown_instead_of_stranding_its_waiter(root):+ """A claimed envelope from the old lane (no ``canonical_delivery_v1``) is never replayed into the+ authority, but its waiter must still resolve: once the old drain's re-offer point passes it gets+ a terminal ``unknown`` reply (not auto-retryable) and is never handed out again."""+ from tools.bot_failure_reasons import UNKNOWN, is_auto_retryable++ base = bot_relay._ensure_dirs(root)+ legacy = {"id": "a" * 32, "created_at": int(_time2.time()) - 60, "from_profile": "default",+ "from_handle": "hermes", "target_connection": "cloud-1", "target_profile": "default",+ "target_handle": "hermes", "message": "legacy"}+ path = base / bot_relay.CLAIMED_DIR / f"{legacy['id']}.json"+ path.write_text(json.dumps(legacy), encoding="utf-8")+ reply_path = base / bot_relay.REPLIES_DIR / f"{legacy['id']}.json"+ claimed_at = _time2.time() - 60+ _os2.utime(path, (claimed_at, claimed_at))+ assert bot_relay.claim_pending_envelopes(root) == []+ assert not reply_path.exists() # inside the old lane's in-flight window: no verdict yet++ claimed_at = _time2.time() - bot_relay.REOFFER_AFTER_SECONDS - 10+ _os2.utime(path, (claimed_at, claimed_at))+ assert bot_relay.claim_pending_envelopes(root) == []+ reply = json.loads(reply_path.read_text(encoding="utf-8"))+ assert reply["reason"] == UNKNOWN and not is_auto_retryable(reply["reason"])+ assert reply["error"] and not reply["reply"]++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 8ed00384cf4..2dee033be65 100644--- a/tools/bot_relay.py+++ b/tools/bot_relay.py@@ -380,10 +380,13 @@ def _replay_unanswered(root: Path | str, base: Path, seen: set, now: float) -> lenvelope = json.loads(path.read_text(encoding="utf-8-sig"))if not isinstance(envelope, dict):raise ValueError(f"expected a JSON object, got {type(envelope).__name__}")- if envelope.get("canonical_delivery_v1") is not True or envelope.get("id") in seen:+ if envelope.get("id") in seen:continueenv_id = str(envelope.get("id") or "")label = f"@{envelope.get('target_handle') or '?'} on {envelope.get('target_connection') or '?'}"+ if envelope.get("canonical_delivery_v1") is not True:+ _settle_pre_upgrade_claim(root, path, env_id, label, now)+ continuecreated = float(envelope.get("created_at") or path.stat().st_mtime)if now - created > REPLY_WAIT_SECONDS:write_reply(root, env_id, reason="delivery_timeout", error=(@@ -394,6 +397,23 @@ def _replay_unanswered(root: Path | str, base: Path, seen: set, now: float) -> lreturn out+def _settle_pre_upgrade_claim(root: Path | str, path: Path, env_id: str, label: str, now: float) -> None:+ """A ``claimed/`` envelope from before canonical delivery (no ``canonical_delivery_v1``) is never+ replayed: its old lane ran a CLI turn the authority has no record of, so re-admitting the id could+ run the DM twice. Left alone, though, its sender's waiter watched a reply that never came. Once the+ old drain's own re-offer point passes (``REOFFER_AFTER_SECONDS`` after its claim — the claim mtime+ — by which time a live delivery has posted the Desktop's timeout reply) the outcome is settled as+ ``unknown``: the waiter resolves, the envelope is never offered again, and nothing auto-resends."""+ from tools.bot_failure_reasons import UNKNOWN++ if now - path.stat().st_mtime < REOFFER_AFTER_SECONDS:+ return+ write_reply(root, env_id, reason=UNKNOWN, error=(+ f"the message to {label} was picked up before this gateway was upgraded and its delivery was "+ "never reported — it may or may not have been delivered. It will not be retried; check with "+ "the recipient before resending."))++def write_reply(root: Path | str, envelope_id: str, *, reply: str = "", error: str = "", reason: str = "") -> Path:"""Persist the relayed reply (or delivery error) for the waiter. ``reason`` (typedcode, ``tools.bot_failure_reasons``) is classified from ``error`` when omitted.