Skip to content

The agent that grows with you

fix(tui): import cli before slash-worker MCP discovery

kshitijk4poor98f5ecbd89ab
diff --git a/tests/tui_gateway/test_slash_worker_watchdog.py b/tests/tui_gateway/test_slash_worker_watchdog.py
index 0454572416e..5394aa88495 100644
--- a/tests/tui_gateway/test_slash_worker_watchdog.py
+++ b/tests/tui_gateway/test_slash_worker_watchdog.py
@@ -1,3 +1,5 @@
+import importlib.abc
+import importlib.util
import io
import subprocess
import sys
@@ -31,7 +33,7 @@ def test_is_orphaned_false_when_direct_parent_is_unchanged():
assert slash_worker._is_orphaned(original_ppid, getppid=lambda: original_ppid) is False
-def test_main_arms_watchdog_for_spawn_parent_before_cli_startup(monkeypatch):
+def test_main_arms_watchdog_then_imports_cli_before_runtime_prep(monkeypatch):
parent_pid = 424242
events = []
@@ -39,9 +41,21 @@ def test_main_arms_watchdog_for_spawn_parent_before_cli_startup(monkeypatch):
def __init__(self, **kwargs):
events.append(("cli", kwargs["resume"]))
- cli_module = types.ModuleType("cli")
- setattr(cli_module, "HermesCLI", FakeHermesCLI)
- monkeypatch.setitem(sys.modules, "cli", cli_module)
+ class FakeCliFinder(importlib.abc.MetaPathFinder, importlib.abc.Loader):
+ # Record when ``cli`` is actually imported, not just when HermesCLI is built:
+ # importing cli loads ~/.hermes/.env, which MCP discovery in runtime prep needs.
+ def find_spec(self, name, path=None, target=None):
+ return importlib.util.spec_from_loader(name, self) if name == "cli" else None
+
+ def create_module(self, spec):
+ return None
+
+ def exec_module(self, module):
+ events.append(("cli-import",))
+ module.HermesCLI = FakeHermesCLI
+
+ monkeypatch.delitem(sys.modules, "cli", raising=False)
+ monkeypatch.setattr(sys, "meta_path", [FakeCliFinder(), *sys.meta_path])
monkeypatch.setattr(
slash_worker,
"_start_parent_death_watchdog",
@@ -75,6 +89,7 @@ def test_main_arms_watchdog_for_spawn_parent_before_cli_startup(monkeypatch):
assert events == [
("watchdog", parent_pid),
+ ("cli-import",),
("runtime",),
("cli", "session-1"),
]
diff --git a/tui_gateway/slash_worker.py b/tui_gateway/slash_worker.py
index 81cf621aaa9..2649103674f 100644
--- a/tui_gateway/slash_worker.py
+++ b/tui_gateway/slash_worker.py
@@ -161,11 +161,12 @@ def main():
# parent there, so keep the observed PPID or the worker would exit at once.
orig_ppid = os.getppid() if sys.platform == "win32" else (args.parent_pid or os.getppid())
_start_parent_death_watchdog(orig_ppid)
- _prepare_slash_worker_runtime()
-
- # Keep the heavyweight CLI import behind the watchdog. Importing it at
- # module load left a reparenting window before main() could snapshot PPID.
+ # Keep the heavyweight CLI import behind the watchdog (importing it at module
+ # load left a reparenting window before main() could snapshot PPID), but ahead
+ # of MCP discovery: importing cli loads ~/.hermes/.env and sets HERMES_QUIET,
+ # which MCP ``${VAR}`` interpolation in the runtime prep depends on.
from cli import HermesCLI
+ _prepare_slash_worker_runtime()
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
# --provider pins the CLI to the parent agent's resolved provider (a MoA session's virtual