Skip to content

The agent that grows with you

fix(bot-dm): report a missing Bot Chat owner as a definite, resendable refusal

Tekniumb653bb32b96f
diff --git a/tests/tools/test_bot_mode_dm.py b/tests/tools/test_bot_mode_dm.py
index d4d2091ca29..44ea0e9fb1b 100644
--- a/tests/tools/test_bot_mode_dm.py
+++ b/tests/tools/test_bot_mode_dm.py
@@ -649,15 +649,18 @@ def test_named_profile_sender_prefix(tmp_path, monkeypatch):
assert _runner_author(calls[0]["command"]) == {"id": "bot:coder", "name": "coder", "is_bot": True}
-def test_unavailable_authority_is_reported_never_worked_around(tmp_path, monkeypatch):
- """No canonical target: the sender learns the outcome is unknown and nothing
- else (no CLI, no background runner) is started."""
[email protected]("lookup", ["not_ready", "no_owner"])
+def test_unavailable_authority_is_reported_never_worked_around(tmp_path, monkeypatch, lookup):
+ """No canonical target, found BEFORE any intent is pinned: nothing was admitted, so the sender
+ gets a definite, resendable ``runtime_unavailable`` refusal (never the ambiguous "do not
+ resend" state) and nothing else (no CLI, no background runner) is started."""
home = _managed_home(tmp_path)
agent = _FakeAgent(home, title="Bot Chat")
from tools import bot_live_delivery as live
- def unavailable(home):
- raise ValueError("profile authority is not ready")
+ def unavailable(home): # "no_owner": discovery is ready but the profile has no store (None)
+ if lookup == "not_ready":
+ raise ValueError("profile authority is not ready")
monkeypatch.setattr(live, "find_canonical_live_owner", unavailable)
monkeypatch.setattr(bot_mode_dm, "_dm_dir", lambda: tmp_path)
@@ -669,9 +672,9 @@ def test_unavailable_authority_is_reported_never_worked_around(tmp_path, monkeyp
result = json.loads(
bot_mode_dm.message_agent_tool(target="researcher", message="hi", agent=agent)
)
- assert result["status"] == "ambiguous"
- assert "not ready" in result["error"] and "Do not resend" in result["error"]
- assert Path(result["evidence_file"]).read_text(encoding="utf-8").endswith("hi")
+ assert result["reason"] == "runtime_unavailable" and "status" not in result
+ assert "NOT sent" in result["error"] and "Do not resend" not in result["error"]
+ assert list(tmp_path.glob("*.live.json")) == [] # no intent: nothing can replay this DM
def test_notification_spawn_failure_is_reported_after_admission(tmp_path, monkeypatch):
diff --git a/tools/bot_mode_dm.py b/tools/bot_mode_dm.py
index 72d321a626a..e48c6b5e3a6 100644
--- a/tools/bot_mode_dm.py
+++ b/tools/bot_mode_dm.py
@@ -433,6 +433,18 @@ def _dm_delivery_id(dm_file: "str | os.PathLike") -> str:
return hashlib.sha256(str(Path(dm_file).resolve()).encode()).hexdigest()
+class LiveTargetUnavailableError(RuntimeError):
+ """No canonical Bot Chat owner was found BEFORE any intent was pinned: nothing was admitted, so
+ this is a definite no-send the sender may retry, not the ambiguous post-admission state."""
+
+ reason = "runtime_unavailable"
+
+
+def _unavailable_payload(dm_file: str, exc: BaseException) -> str:
+ return json.dumps({"reason": LiveTargetUnavailableError.reason, "delivery_id": _dm_delivery_id(dm_file),
+ "error": f"{exc} — the message was NOT sent; it is safe to resend once the target is up."})
+
+
def _admit_live_dm(profile_home: Path | None, dm_file: str, author: Optional[dict] = None) -> dict | None:
"""Pin intent before admission; retries may inspect, never change transport."""
from tools.bot_live_delivery import deliver_to_live_owner, find_canonical_live_owner, read_delivery_result
@@ -444,9 +456,13 @@ def _admit_live_dm(profile_home: Path | None, dm_file: str, author: Optional[dic
intent = json.loads(intent_path.read_text(encoding="utf-8-sig"))
else:
assert profile_home is not None
- owner = find_canonical_live_owner(profile_home)
+ try:
+ owner = find_canonical_live_owner(profile_home)
+ except Exception as exc:
+ # Discovery runs before the intent exists and never admits: definite, not ambiguous.
+ raise LiveTargetUnavailableError(f"canonical Bot Chat target is unavailable ({exc})") from exc
if owner is None:
- raise ValueError("canonical Bot Chat target is unavailable; no local fallback")
+ raise LiveTargetUnavailableError("canonical Bot Chat target is unavailable")
intent = dict(owner=owner, message=Path(dm_file).read_text(encoding="utf-8-sig"),
delivery_id=_dm_delivery_id(dm_file),
**({"author": author} if author else {}))
@@ -561,6 +577,9 @@ def _run_delivery(argv: list[str], dm_file: str, *, stdin_file: bool,
if home is not None or os.path.exists(_live_intent_file(dm_file)):
try:
record = _admit_live_dm(home, dm_file, author)
+ except LiveTargetUnavailableError as exc:
+ print(_unavailable_payload(dm_file, exc))
+ return 1
except Exception as exc:
print(_live_outcome_unknown(dm_file, exc))
return 1
@@ -610,6 +629,10 @@ def _start_delivery(argv: list[str], content: str, label: str, *, stdin_file: bo
if profile_home is not None:
try:
record = _admit_live_dm(profile_home, dm_file, author)
+ except LiveTargetUnavailableError as exc:
+ payload = _unavailable_payload(dm_file, exc)
+ _unlink_dm_file(dm_file) # no intent was pinned: nothing will ever replay this file
+ return payload
except Exception as exc:
return json.dumps({"status": "ambiguous", "delivery_id": _dm_delivery_id(dm_file),
"error": f"Live delivery admission could not be confirmed: {exc}. Do not resend.",