Skip to content

The agent that grows with you

fix(bot-mode): dial the DM authority from threads with a running loop

PRATHAMESH75862f9c95b216
diff --git a/tests/tools/test_bot_live_owner_delivery.py b/tests/tools/test_bot_live_owner_delivery.py
index ea06b22087b..61968e12a35 100644
--- a/tests/tools/test_bot_live_owner_delivery.py
+++ b/tests/tools/test_bot_live_owner_delivery.py
@@ -306,3 +306,26 @@ def test_mailbox_lock_refuses_a_dir_another_uid_owns_when_chmod_is_denied(tmp_pa
with mailbox._locked(tmp_path):
pass
+
+def test_authority_delivery_dials_from_a_thread_with_a_running_loop(tmp_path, monkeypatch):
+ """A gateway-hosted message_agent call runs on a thread that already has a running loop;
+ the authority request must still execute rather than die in asyncio.run() before dialing."""
+ import asyncio
+ from types import SimpleNamespace
+
+ from hermes_cli import gateway_runtime
+
+ probed = []
+
+ def discover(home, timeout):
+ probed.append(Path(home))
+ return SimpleNamespace(state="stopped", endpoint=None)
+
+ monkeypatch.setattr(gateway_runtime, "discover_gateway_endpoint", discover)
+
+ async def tool_call_on_loop():
+ return mailbox.authority_delivery(tmp_path, {"id": "0" * 64, "message": "probe"})
+
+ with pytest.raises(ValueError, match="profile authority is not ready"):
+ asyncio.run(tool_call_on_loop())
+ assert probed == [tmp_path.resolve()]
diff --git a/tools/bot_live_delivery.py b/tools/bot_live_delivery.py
index 850ff4196b0..ea22bf2996c 100644
--- a/tools/bot_live_delivery.py
+++ b/tools/bot_live_delivery.py
@@ -95,7 +95,14 @@ def _authority_rpc(home, method, params):
async with GatewayClient(ws) as client:
return await client.rpc(method, **params)
- return asyncio.run(request())
+ try:
+ asyncio.get_running_loop()
+ except RuntimeError:
+ return asyncio.run(request())
+ # A gateway-hosted tool call runs on a thread with a live loop, where asyncio.run() raises
+ # before anything is dialed and message_agent misreports the DM as ambiguous (#139303).
+ from model_tools import _run_async
+ return _run_async(request())
def _owner(home: Path | str, owner: dict[str, Any]) -> dict[str, str]: