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 Soliva9e16953a1b6e
diff --git a/tests/tools/test_bot_turn_lock.py b/tests/tools/test_bot_turn_lock.pyindex 0291326df88..5425e1fd816 100644--- a/tests/tools/test_bot_turn_lock.py+++ b/tests/tools/test_bot_turn_lock.py@@ -13,6 +13,7 @@ import fcntlimport jsonimport osimport re+import subprocessimport threadingimport time@@ -307,6 +308,7 @@ def test_relay_deliver_returns_target_busy_error(tmp_path, monkeypatch):assert "error" in outassert out["error"]["code"] == 5096assert "target_busy" in out["error"]["message"]+ assert out["error"]["data"]["reason"] == "target_busy"assert not spawned, "turn must not spawn while the profile is busy"finally:release.set()@@ -343,3 +345,37 @@ def test_relay_deliver_serializes_then_succeeds(tmp_path, monkeypatch):assert "error" not in out, outassert out["result"]["reply"] == "pong"assert time.monotonic() - start >= 0.2, "deliver should have queued"+++ ("failure", "code", "reason"),+ [+ (TurnBusyError("ops", 0.2), 5096, "target_busy"),+ (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"),+ ],+ ids=["busy", "turn-timed-out", "classifiable-failure", "unclassifiable-failure"],+)+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 puts+ it in the sender's reply file, and the sender re-classifies from free text otherwise — which can+ never produce these codes. A refusal that ships only a JSON-RPC code reaches the sending agent as+ `[reason: unknown]`, so it cannot tell "retry shortly" from an auth failure. The turn-failure+ branch already did this; these three did not."""+ import tui_gateway.server as srv++ h = tmp_path / "h"+ (h / "profiles" / "ops").mkdir(parents=True)+ monkeypatch.setenv("HERMES_HOME", str(h))+ monkeypatch.setattr(bot_relay, "local_delivery_command", lambda prof, tmp: ["__delivery__", prof])++ def _raise(argv, **kwargs):+ raise failure++ monkeypatch.setattr("subprocess.run", _raise)++ out = srv._methods["bot_relay.deliver"](1, {"profile": "ops", "message": "x"})++ assert out["error"]["code"] == code+ assert out["error"]["data"]["reason"] == reasondiff --git a/tui_gateway/methods_bot_relay.py b/tui_gateway/methods_bot_relay.pyindex 1a87383be26..28c71b46b73 100644--- a/tui_gateway/methods_bot_relay.py+++ b/tui_gateway/methods_bot_relay.py@@ -172,10 +172,17 @@ def _(rid, params: dict, _root=_relay_root, _run=_run_delivery) -> dict:reply = _bot_mode_delivery_text((proc.stdout or "").strip(), successful=True)return _ok(rid, {"reply": reply})except subprocess.TimeoutExpired:- return _err(rid, 5093, "delivery turn timed out")+ # Every classified refusal has to ride `data.reason`: the Desktop forwards only that field,+ # and the sender re-classifies from free text, which cannot name these. This branch is also+ # `delivery_timeout`'s only producer.+ from tools.bot_failure_reasons import DELIVERY_TIMEOUT+ return _err(rid, 5093, "delivery turn timed out", data={"reason": DELIVERY_TIMEOUT})except Exception as e:- # 'target_busy' extends the structured refusal enum.- return _err(rid, 5096 if getattr(e, "reason", "") == "target_busy" else 5094, str(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))})@method("bot_relay.reply")