The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-relay): every deliver refusal carries its typed reason, not just a JSON-RPC code
John Paul Solivad72bf7507853
diff --git a/tests/tools/test_bot_turn_lock.py b/tests/tools/test_bot_turn_lock.pyindex 5425e1fd816..d94e868e5cd 100644--- a/tests/tools/test_bot_turn_lock.py+++ b/tests/tools/test_bot_turn_lock.py@@ -347,6 +347,16 @@ def test_relay_deliver_serializes_then_succeeds(tmp_path, monkeypatch):assert time.monotonic() - start >= 0.2, "deliver should have queued"+class _WithReason(RuntimeError):+ """An exception carrying its own ``reason``. ``reason`` is a stdlib attribute on+ ``ssl.SSLError`` and ``urllib.error.URLError`` too, so a refusal must not forward whatever+ it finds there into a channel whose consumers expect a closed vocabulary."""++ def __init__(self, reason: str, message: str) -> None:+ super().__init__(message)+ self.reason = reason++@pytest.mark.parametrize(("failure", "code", "reason"),[@@ -354,8 +364,11 @@ def test_relay_deliver_serializes_then_succeeds(tmp_path, monkeypatch):(subprocess.TimeoutExpired(["hermes"], 600), 5093, "delivery_timeout"),(RuntimeError("Error code: 401 - invalid api key"), 5094, "provider_auth_or_access"),(RuntimeError("something nobody has a rule for"), 5094, "unknown"),+ (_WithReason("CERTIFICATE_VERIFY_FAILED", "ssl handshake failed"), 5094, "unknown"),+ (_WithReason("provider_quota_limit", "quota exhausted"), 5094, "provider_quota_limit"),],- ids=["busy", "turn-timed-out", "classifiable-failure", "unclassifiable-failure"],+ ids=["busy", "turn-timed-out", "classifiable-failure", "unclassifiable-failure",+ "reason-outside-the-vocabulary", "reason-inside-the-vocabulary"],)def test_every_relay_refusal_carries_its_typed_reason(tmp_path, monkeypatch, failure, code, reason):"""`data.reason` is the only channel the Desktop forwards: it reads `error.data.reason` and putsdiff --git a/tui_gateway/methods_bot_relay.py b/tui_gateway/methods_bot_relay.pyindex 28c71b46b73..aa53e4ee8b8 100644--- a/tui_gateway/methods_bot_relay.py+++ b/tui_gateway/methods_bot_relay.py@@ -34,6 +34,23 @@ def _run_delivery(profile: str, tmp: str, env: dict | None = None) -> subprocesserrors="replace", timeout=TURN_ATTEMPT_TIMEOUT_SECONDS, env=env)+def _delivery_failure_reason(error: BaseException) -> str:+ """The typed reason for a refusal, kept inside the documented vocabulary.++ An exception's own ``reason`` is trusted only when it names a real code: ``TurnBusyError``+ carries ``target_busy``, but ``reason`` is also a stdlib attribute on ``ssl.SSLError`` and+ ``urllib.error.URLError``, and forwarding one of those would put free text where consumers+ expect a closed set. Anything else is classified like every other failure.+ """+ from tools.bot_failure_reasons import ALL_REASONS, classify_agent_error++ # 'target_busy' extends the structured refusal enum and predates ALL_REASONS.+ supplied = str(getattr(error, "reason", "") or "").strip()+ if supplied == "target_busy" or supplied in ALL_REASONS:+ return supplied+ return classify_agent_error(str(error))++@method("bot_relay.roster.sync")def _(rid, params: dict, _root=_relay_root) -> dict:"""Replace this gateway's view of agents on OTHER connections → ``{count}`` accepted rows@@ -57,7 +74,8 @@ def _(rid, params: dict, _root=_relay_root) -> dict:@method("bot_relay.deliver")-def _(rid, params: dict, _root=_relay_root, _run=_run_delivery) -> dict:+def _(rid, params: dict, _root=_relay_root, _run=_run_delivery,+ _failure_reason=_delivery_failure_reason) -> dict:"""Deliver a relayed DM (``profile``, attribution-prefixed ``message``) into a Bot Chat ON THISGATEWAY via the one-turn ``hermes -p <profile> chat -c "Bot Chat"`` transport local DMs use →``{reply}``. Blocking by design (Desktop relay worker; the RPC pool keeps it off the reader)."""@@ -178,11 +196,8 @@ def _(rid, params: dict, _root=_relay_root, _run=_run_delivery) -> dict:from tools.bot_failure_reasons import DELIVERY_TIMEOUTreturn _err(rid, 5093, "delivery turn timed out", data={"reason": DELIVERY_TIMEOUT})except Exception as e:- from tools.bot_failure_reasons import classify_agent_error- # 'target_busy' extends the structured refusal enum; the exception carries it already.- reason = str(getattr(e, "reason", "") or "")- return _err(rid, 5096 if reason == "target_busy" else 5094, str(e),- data={"reason": reason or classify_agent_error(str(e))})+ reason = _failure_reason(e)+ return _err(rid, 5096 if reason == "target_busy" else 5094, str(e), data={"reason": reason})@method("bot_relay.reply")