Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(agent-runtimes): give each Hermes agent its own gateway daemon so the key reaches it (#958)
Ruben Lütke25ea7dd07e2b
diff --git a/docs/agent-runtimes.md b/docs/agent-runtimes.mdindex 22955f3e1c..a739326a95 100644--- a/docs/agent-runtimes.md+++ b/docs/agent-runtimes.md@@ -305,6 +305,19 @@ replayed during `session/load` is swallowed because the chat already shows it.otherwise syncs its dependency environment, and retries a pending sourceupdate tail, in front of the turn (60-190 s measured on Windows). Only thesetup job (`prepare_command`) runs without it.+- **Gateway daemon (Hermes after 0.21):** `hermes acp` is only a viewer; the+ session runs in a gateway daemon it starts detached, which keeps the+ environment it was started with. A profile served by the data root's+ multiplexer reads its credentials from files only, so the gateway token+ (process environment only) never reached the model call and every turn+ ended "custom rejected your API key". Every agent profile is therefore+ `gateway.standalone: true` (its own daemon, started from its own launch+ environment), the daemons' host record lives in `<data root>/gateway-locks`+ (`HERMES_GATEWAY_LOCK_DIR`, so the person's own Hermes gateway never meets+ them), and a daemon started for a different launch (another token, URL,+ profile or Hermes build, or left by an earlier app run) is stopped before+ the turn, as are an agent's daemons when Dodaios stops its runtime. Older+ builds run the session in-process and ignore all three.- **Profiles of one Dodaios data root:** every agent folder is a Hermes profile,`HERMES_HOME=<runtimes_root>/hermes-home/profiles/<agent>`. Hermes keeps itsPython dependency environment (about 700 MB) per *data root*, and a profile@@ -332,8 +345,8 @@ replayed during `session/load` is swallowed because the chat already shows it.`computer_use`, `cronjob` and `skills` toolsets always disabled (Dodaios hasa visible browser, its own routines and its own skills; Hermes' browser is aheadless Chromium, and its skill listing was a third of every prompt), and- `SOUL.md` = the agent's Dodaios briefing. The only key is the gateway token,- in the process environment.+ `SOUL.md` = the agent's Dodaios briefing, `gateway.standalone: true` (see+ above). The only key is the gateway token, in the process environment.- **Dodaios tools:** passed per session in `session/new` / `session/load``mcpServers` (HTTP with headers); `HERMES_ACP_SKIP_CONFIGURED_MCP=1` keepsany configured servers out. The server is mounted as `dodaios`, so Hermesdiff --git a/dodaios/agent_runtimes/hermes.py b/dodaios/agent_runtimes/hermes.pyindex 6cd1f6f19d..49ad32f243 100644--- a/dodaios/agent_runtimes/hermes.py+++ b/dodaios/agent_runtimes/hermes.py@@ -30,7 +30,9 @@ option degrades to Hermes' default instead of breaking the turn.from __future__ import annotationsimport asyncio+import contextlibimport hashlib+import jsonimport loggingimport osimport re@@ -245,6 +247,9 @@ class HermesRuntime:self._detect = DetectCache()self._slots = TurnSlots(NAME, lock_dir=lambda: base.runtimes_root() / "locks")self._warm = WarmPool(NAME)+ #: Home key -> digest of the launch its profile's gateway daemon+ #: was started for (this app run only).+ self._daemons: dict[str, str] = {}# ----------------------------------------------------------- detection@@ -381,9 +386,14 @@ class HermesRuntime:return self._slots.busy()async def stop(self, agent_id: str | None = None) -> None:- """Stop the warm processes of ``agent_id`` (all when ``None``)."""+ """Stop the warm processes of ``agent_id`` (all when ``None``) and the+ gateway daemons they ran on."""keys = None if agent_id is None else {agent_id, f"{agent_id}~runs"}await self._warm.stop(keys)+ for key in [key for key in self._daemons if keys is None or key in keys]:+ del self._daemons[key]+ home = hermes_root() / "profiles" / profile_name(key)+ await asyncio.to_thread(_stop_profile_gateway, home)# -------------------------------------------------------------- launch@@ -444,6 +454,12 @@ class HermesRuntime:"approvals": {"mode": "manual"},# Never put the data root on the user's PATH (POSIX; see prepare_root)."cli": {"expose_on_path": False},+ # Newer Hermes runs an ACP session in a gateway daemon that+ # ``hermes acp`` starts. A profile the root's multiplexer serves+ # reads its credentials from files only (the key stays off disk),+ # so each agent gets its own daemon, started from its own+ # process environment (see _stop_profile_gateway).+ "gateway": {"standalone": True},}if turn.effort in _HERMES_EFFORTS:# Hermes' ACP sessions resolve this per model (agent.reasoning_effort;@@ -529,6 +545,8 @@ class HermesRuntime:async def _launch(self, binary: str, turn: RuntimeTurn, release: Callable[[], None]) -> RuntimeLaunch:+ from dodaios.agent_runtimes.warm import signature+home = await asyncio.to_thread(profile_home, home_key(turn.agent_id, turn.session_id))@@ -538,11 +556,27 @@ class HermesRuntime:"HERMES_HOME": str(home),# Only the Jarvis server handed over per session; none from config."HERMES_ACP_SKIP_CONFIGURED_MCP": "1",+ # The gateway daemons' host record stays in Dodaios' root: the+ # person's own Hermes gateway never meets (or refuses) them.+ "HERMES_GATEWAY_LOCK_DIR": str(+ private_dir(hermes_root() / "gateway-locks")+ ),**_TURN_ENV,**turn.route.env(),**_restore_key_aliases(turn.route.base_url, turn.route.api_key),})+ launch_key = home_key(turn.agent_id, turn.session_id)+ served = signature(+ binary, env, str(turn.workspace), self.config_for(turn), self._soul(turn)+ )+ if self._daemons.get(launch_key) != served:+ # A daemon keeps the key, URL and profile it started with: one+ # left by an earlier launch (or app run) would answer this turn+ # with stale ones, so it goes first and ``hermes acp`` starts a+ # fresh one from this environment.+ await asyncio.to_thread(_stop_profile_gateway, home)+ self._daemons[launch_key] = servedservers: list[McpServer] = []if turn.mcp_url and turn.control_key:from dodaios.agent_chat.dodaios_harness import HEADER_NAME@@ -588,6 +622,50 @@ class HermesRuntime:)+def _stop_profile_gateway(home: Path) -> bool:+ """Stop the Hermes gateway daemon of one agent profile, if one runs.++ Hermes builds after 0.21 run an ACP session in a gateway daemon that+ ``hermes acp`` starts detached on first use and that outlives it (it ends+ itself minutes after its last client). It keeps the environment it was+ started with. Only the process recorded in the profile's ``gateway.pid``+ with a Hermes ``gateway run`` command line, and its own children, are+ stopped. Older builds never write that record: nothing to do.+ """+ path = home / "gateway.pid"+ try:+ record = json.loads(path.read_text(encoding="utf-8"))+ pid = int(record["pid"] if isinstance(record, dict) else record)+ except (OSError, ValueError, TypeError, KeyError) as exc:+ # No record (older builds, nothing started) or one Hermes is rewriting.+ log.debug("agent runtimes: no Hermes gateway record in %s: %s", home.name, exc)+ return False+ import psutil++ try:+ proc = psutil.Process(pid)+ cmdline = " ".join(proc.cmdline()).lower()+ if not ("hermes" in cmdline and "gateway" in cmdline and "run" in cmdline):+ return False # the pid now belongs to another process+ started = proc.create_time()+ # Children by creation time too: a pid reused since cannot have a+ # parent born after it.+ doomed = [+ child+ for child in proc.children(recursive=True)+ if child.create_time() >= started+ ]+ for victim in (*doomed, proc):+ with contextlib.suppress(psutil.Error):+ victim.kill()+ psutil.wait_procs([*doomed, proc], timeout=10)+ except psutil.Error as exc:+ log.debug("agent runtimes: Hermes gateway record ignored: %s", exc)+ return False+ log.info("agent runtimes: stopped the Hermes gateway of profile %s", home.name)+ return True++def _refuse_claude_cli(turn: RuntimeTurn) -> None:"""Hermes cannot run an agent on the Claude subscription.diff --git a/scripts/ci/agent_runtime_e2e.py b/scripts/ci/agent_runtime_e2e.pyindex 2ebf59695d..12b34649f0 100644--- a/scripts/ci/agent_runtime_e2e.py+++ b/scripts/ci/agent_runtime_e2e.py@@ -135,6 +135,13 @@ def main(argv: list[str] | None = None) -> int:parser.add_argument("--expect-setup-failure", default="", metavar="TEXT")args = parser.parse_args(argv)+ # Runtime output (setup log tails, error copy) carries non-ASCII text such+ # as arrows; a Windows runner's console code page cannot print it.+ for stream in (sys.stdout, sys.stderr):+ reconfigure = getattr(stream, "reconfigure", None)+ if reconfigure is not None:+ reconfigure(encoding="utf-8", errors="replace")+ os.environ["PYTHONIOENCODING"] = "utf-8"os.environ["PATH"] = desktop_path()sys.path.insert(0, str(REPO_ROOT))env = dict(os.environ)diff --git a/scripts/spikes/agent_runtimes_gateway_e2e.py b/scripts/spikes/agent_runtimes_gateway_e2e.pyindex de4ce5dca8..03e8c8e4d4 100644--- a/scripts/spikes/agent_runtimes_gateway_e2e.py+++ b/scripts/spikes/agent_runtimes_gateway_e2e.py@@ -215,12 +215,7 @@ async def main(name: str, provider: str, wanted: str) -> int:await asyncio.to_thread(ledger.set_ledger_path, previous_ledger)gateway._CATALOG = previous_catalogif result != 0:- log = tmp / "runtime-stderr.log"- if log.exists():- target = ROOT / "eval-results" / f"{name}-{time.time_ns()}.log"- target.parent.mkdir(parents=True, exist_ok=True)- target.write_bytes(log.read_bytes())- print("diagnostic:", target)+ _keep_diagnostics(name, tmp)finally:gateway._client = previous_clientgateway.subscription_ready = previous_ready@@ -231,6 +226,28 @@ async def main(name: str, provider: str, wanted: str) -> int:runtime_refs.set_api_base_url(previous_url or "")+def _keep_diagnostics(name: str, tmp: Path) -> None:+ """Retain the runtime's stderr and its own log files (Hermes writes the+ model call's failure to ``logs/*.log`` in its home, not to stderr)."""+ logs = [tmp / "runtime-stderr.log", *sorted((tmp / "agent_runtimes").rglob("*.log"))]+ parts = [+ f"===== {log.relative_to(tmp)} =====\n".encode() + log.read_bytes()+ for log in logs+ if log.is_file()+ ]+ if not parts:+ return+ target = ROOT / "eval-results" / f"{name}-{time.time_ns()}.log"+ target.parent.mkdir(parents=True, exist_ok=True)+ target.write_bytes(b"\n".join(parts))+ print("diagnostic:", target)+ # Each file's tail inline too: a CI job log is read far more often than its artifacts.+ for part in parts:+ lines = part.decode("utf-8", errors="replace").splitlines()+ for line in [lines[0], *lines[1:][-25:]]:+ print(" |", line)++async def _check(name: str, provider: str, wanted: str, tmp: Path, *, replay: bool) -> int:workspace = tmp / "workspace"workspace.mkdir()diff --git a/tests/unit/agent_runtimes/test_hermes_gateway_daemon.py b/tests/unit/agent_runtimes/test_hermes_gateway_daemon.pynew file mode 100644index 0000000000..3587f615c6--- /dev/null+++ b/tests/unit/agent_runtimes/test_hermes_gateway_daemon.py@@ -0,0 +1,136 @@+"""Hermes builds after 0.21 run ACP sessions in a per-profile gateway daemon.++``hermes acp`` starts that daemon detached and it keeps the environment it+was started with. A profile the data root's multiplexer serves reads its+credentials from files only, so the gateway token (process environment+only) never reached the model call: every turn answered "custom rejected your+API key". Each agent profile is standalone, gets a daemon started from its+own launch, and a daemon started for another launch is stopped first.+"""++from __future__ import annotations++import asyncio+import json+import subprocess+import sys+from pathlib import Path++import pytest++from dodaios.agent_runtimes import base, hermes+from dodaios.agent_runtimes.base import RuntimeTurn+from dodaios.agent_runtimes.model_map import ModelRoute++_GATEWAY = "http://127.0.0.1:47821/api/runtime-gateway/v1"+# Fake gateway tokens for assertions.+_FIRST, _SECOND, _SECRET = "jrg_" + "one", "jrg_" + "two", "jrg_" + "secret"+++def roots(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:+ monkeypatch.setattr(base, "runtimes_root", lambda: tmp_path / "agent_runtimes")+ monkeypatch.setattr(base, "legacy_runtimes_root", lambda: tmp_path / "legacy_runtimes")+ return tmp_path+++def stopped(monkeypatch: pytest.MonkeyPatch) -> list[Path]:+ calls: list[Path] = []+ monkeypatch.setattr(hermes, "_stop_profile_gateway", lambda home: calls.append(home) or False)+ return calls+++def _turn(workspace: Path, *, agent: str = "hermit", grant: str = _FIRST) -> RuntimeTurn:+ return RuntimeTurn(+ agent_id=agent,+ agent_name="Hermit",+ session_id=f"society:{agent}",+ workspace=workspace,+ route=ModelRoute("ollama", "qwen3", _GATEWAY, "chat_completions", grant),+ resume=None,+ auto_approve=True,+ )+++def _launch(runtime: hermes.HermesRuntime, turn: RuntimeTurn):+ return asyncio.run(runtime._launch("hermes", turn, lambda: None))+++def test_every_agent_profile_runs_its_own_gateway_daemon(roots):+ config = hermes.HermesRuntime().config_for(_turn(roots))+ assert config["gateway"]["standalone"] is True+++def test_the_daemons_host_record_stays_in_dodaios_root(roots, stopped):+ launch = _launch(hermes.HermesRuntime(), _turn(roots))+ lock_dir = Path(launch.env["HERMES_GATEWAY_LOCK_DIR"])+ assert lock_dir == hermes.hermes_root() / "gateway-locks"+ assert lock_dir.is_dir()+++def test_the_key_reaches_the_daemon_through_the_launch_environment_only(roots, stopped):+ launch = _launch(hermes.HermesRuntime(), _turn(roots, grant=_SECRET))+ assert launch.env["JARVIS_RUNTIME_API_KEY"] == _SECRET+ home = Path(launch.env["HERMES_HOME"])+ assert _SECRET not in (home / "config.yaml").read_text(encoding="utf-8")+++def test_a_daemon_left_by_another_launch_is_stopped_before_the_turn(roots, stopped):+ runtime = hermes.HermesRuntime()+ first = _launch(runtime, _turn(roots))+ home = Path(first.env["HERMES_HOME"])+ # First launch of this app run: a daemon an earlier run left goes.+ assert stopped == [home]+ # The same launch again (the warm process is reused): the daemon stays.+ _launch(runtime, _turn(roots))+ assert stopped == [home]+ # A new token (revoked grant, app restart) or URL: the old daemon goes.+ _launch(runtime, _turn(roots, grant=_SECOND))+ assert stopped == [home, home]+++def test_stopping_an_agent_stops_only_its_daemons(roots, stopped):+ runtime = hermes.HermesRuntime()+ ada = Path(_launch(runtime, _turn(roots, agent="ada")).env["HERMES_HOME"])+ bob = Path(_launch(runtime, _turn(roots, agent="bob")).env["HERMES_HOME"])+ stopped.clear()+ asyncio.run(runtime.stop("ada"))+ assert stopped == [ada]+ asyncio.run(runtime.stop())+ assert stopped == [ada, bob]+ asyncio.run(runtime.stop()) # nothing left to stop+ assert stopped == [ada, bob]+++def _sleeper(*argv: str) -> subprocess.Popen:+ return subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)", *argv])+++def test_the_recorded_hermes_gateway_process_is_stopped(tmp_path):+ proc = _sleeper("hermes_cli.main", "gateway", "run", "--idle-exit")+ try:+ record = {"pid": proc.pid, "kind": "hermes-gateway"}+ (tmp_path / "gateway.pid").write_text(json.dumps(record))+ assert hermes._stop_profile_gateway(tmp_path) is True+ assert proc.wait(timeout=10) is not None+ finally:+ proc.kill()+++def test_a_reused_pid_is_never_stopped(tmp_path):+ proc = _sleeper("something", "else")+ try:+ (tmp_path / "gateway.pid").write_text(json.dumps({"pid": proc.pid}))+ assert hermes._stop_profile_gateway(tmp_path) is False+ assert proc.poll() is None+ finally:+ proc.kill()+ proc.wait(timeout=10)++[email protected]("content", [None, "", "not json", '{"kind": "x"}', '{"pid": "x"}'])+def test_no_or_a_broken_record_stops_nothing(tmp_path, content):+ if content is not None:+ (tmp_path / "gateway.pid").write_text(content)+ assert hermes._stop_profile_gateway(tmp_path) is False