Skip to content

The agent that grows with you

fix(bot-relay): hand cross-connection DMs to a Bot Chat live in a sibling process

teknium1c748a4b4b417
diff --git a/tests/tui_gateway/test_bot_relay_methods.py b/tests/tui_gateway/test_bot_relay_methods.py
index 9dd42049ebe..fdc99135ce8 100644
--- a/tests/tui_gateway/test_bot_relay_methods.py
+++ b/tests/tui_gateway/test_bot_relay_methods.py
@@ -172,6 +172,52 @@ def test_deliver_lands_in_live_bot_chat_instead_of_subprocess(home, monkeypatch)
assert out["reply"] == "pong" and spawned and not submitted
+def test_deliver_hands_off_to_a_bot_chat_owned_by_another_process(home, monkeypatch):
+ """#113753: the relay RPC lands in whichever backend the Desktop routes the target CONNECTION
+ to, while the Desktop-opened Bot Chat can be live in a sibling process for that profile
+ (per-(connection, profile) pool, per-profile SSH dashboards). That owner's lease refuses the
+ subprocess transport with SESSION_NOT_OWNED, so the handler must hand the DM to the owner
+ through the durable mailbox local DMs use, and never spawn the CLI.
+ """
+ from hermes_cli.active_sessions import try_acquire_active_session
+ from hermes_state import SessionDB
+ from tools import bot_live_delivery as mailbox
+
+ ops_home = home / "profiles" / "ops"
+ db = SessionDB(db_path=ops_home / "state.db")
+ db.create_session(session_id="chat", source="desktop")
+ db.set_session_title("chat", "Bot Chat")
+ db.close()
+ # The sibling process's lease: a mailbox-capable live owner registered in the target's home.
+ lease, refusal = try_acquire_active_session(
+ session_id="chat", surface="desktop", config={}, registry_home=ops_home,
+ metadata={"live_session_id": "live-in-other-process", "bot_live_delivery_consumer": True})
+ assert refusal is None
+ spawned = []
+
+ def _fake_run(argv, *a, **k):
+ if argv and argv[0] != "git":
+ spawned.append(argv)
+ raise AssertionError("the CLI transport collides with the live owner")
+
+ monkeypatch.setattr("subprocess.run", _fake_run)
+ monkeypatch.setattr(srv, "_profile_home", lambda name: ops_home)
+ monkeypatch.setattr(srv, "_sessions", {}) # THIS process hosts nothing for ops
+ try:
+ out = _result(srv._methods["bot_relay.deliver"](1, {
+ "profile": "ops", "message": "ping", "from_profile": "cody", "from_handle": "cody",
+ "from_connection": "conn-a"}))
+ assert not spawned and "open Bot Chat" in out["reply"]
+ (queued,) = [
+ r for p in (ops_home / "runtime" / mailbox.DELIVERY_DIR_NAME).glob("*.json")
+ if (r := json.loads(p.read_text(encoding="utf-8")))]
+ assert queued["status"] == "queued" and queued["message"] == "ping"
+ assert queued["owner"]["lease_id"] == lease.lease_id
+ assert queued["author"]["name"] == "cody" and queued["author"]["is_bot"] is True
+ finally:
+ lease.release()
+
+
def test_reply_roundtrip_and_id_validation(home):
envelope_id = "c" * 32
_result(srv._methods["bot_relay.reply"](1, {"id": envelope_id, "reply": "hi"}))
diff --git a/tui_gateway/methods_bot_relay.py b/tui_gateway/methods_bot_relay.py
index 9cd0fdee7c1..cbec598501c 100644
--- a/tui_gateway/methods_bot_relay.py
+++ b/tui_gateway/methods_bot_relay.py
@@ -112,6 +112,20 @@ def _(rid, params: dict, _root=_relay_root, _run=_run_delivery) -> dict:
reply = f"Delivered into @{resolved}'s open Bot Chat; the reply will appear there."
return _ok(rid, {"reply": reply})
+ # This process's _sessions is not the ownership authority: the Desktop pools one backend per
+ # (connection, profile) and an SSH source runs one remote dashboard per profile, so the
+ # target's Bot Chat can be live in a sibling process on this host while the relay RPC lands
+ # here. The subprocess transport would then be refused SESSION_NOT_OWNED by that owner's
+ # lease (#113753). Hand the DM to the live owner through the same mailbox local DMs use
+ # (tools/bot_mode_dm.py::_run_delivery); its poller admits it at the next idle boundary.
+ from tools.bot_live_delivery import deliver_to_live_owner, find_canonical_live_owner
+ owner_home = live_home if live_home is not None else Path(_hermes_home)
+ owner = find_canonical_live_owner(owner_home)
+ if owner is not None:
+ deliver_to_live_owner(owner_home, owner, message, author=author)
+ reply = f"Delivered into @{resolved}'s open Bot Chat; the reply will appear there."
+ return _ok(rid, {"reply": reply})
+
def _detail(p) -> str:
from tools.bot_failure_reasons import turn_failure_text
return turn_failure_text(p.stdout, p.stderr)