Skip to content

The agent that grows with you

fix(docker): verify TUI supervisor identity and profile ownership

Gille41bde4f96d69
diff --git a/hermes_cli/gateway_runtime_service.py b/hermes_cli/gateway_runtime_service.py
index fae61693d17..d167cc8d731 100644
--- a/hermes_cli/gateway_runtime_service.py
+++ b/hermes_cli/gateway_runtime_service.py
@@ -149,7 +149,7 @@ def _systemd(home: Path, deadline: float) -> ExistingService | None:
return found[0] if found else None
-def _s6(home: Path) -> ExistingService:
+def _s6(home: Path) -> ExistingService | None:
"""Find the container gateway slot that owns ``home``.
TUI clients call ``ensure_gateway_runtime`` inside the Docker image too.
@@ -157,8 +157,25 @@ def _s6(home: Path) -> ExistingService:
through the Linux systemd probe and treating s6 as inaccessible.
"""
from hermes_cli.service_manager import S6ServiceManager
+ from hermes_cli.gateway_runtime_multiplex import implied_host_root
- profile = home.name if home.parent.name == "profiles" else "default"
+ # with-contenv is also the service run script's authority. The initiating
+ # client may have selected a named profile or a different install root.
+ try:
+ container_home = Path("/run/s6/container_environment/HERMES_HOME").read_text(encoding="utf-8-sig").strip()
+ except (OSError, UnicodeError):
+ raise RuntimeStartError("service_identity_unverified") from None
+ if not container_home:
+ raise RuntimeStartError("service_identity_unverified")
+ root = Path(container_home).resolve()
+ home = home.resolve()
+ if home != root and home.parent != root / "profiles":
+ raise RuntimeStartError("profile_mismatch")
+ # A normal named profile belongs to the root multiplexer. Returning None
+ # lets ensure select its host, including a first boot with no saved verdict.
+ if home != root and implied_host_root(home) == root:
+ return None
+ profile = "default" if home == root else home.name
manager = S6ServiceManager()
service_name = f"gateway-{profile}"
if not (manager.scandir / service_name).is_dir():
diff --git a/tests/docker/test_tui_gateway_startup.py b/tests/docker/test_tui_gateway_startup.py
new file mode 100644
index 00000000000..93abdd0c9a2
--- /dev/null
+++ b/tests/docker/test_tui_gateway_startup.py
@@ -0,0 +1,114 @@
+"""Ink's real bootstrap starts its s6 owner without sharing the terminal."""
+
+from __future__ import annotations
+
+import json
+import subprocess
+
+import pytest
+
+from tests.docker.conftest import docker_exec, poll_container
+from tests.docker.test_interactive_logging import (
+ _CATCHALL,
+ _STDOUT,
+ _WARNING,
+ _emit_supervised_output,
+ _start_console,
+)
+
+
[email protected]("profile", ["default", "multiplexed", "standalone"])
+def test_tui_bootstrap_starts_the_correct_supervised_owner(
+ built_image: str,
+ container_name: str,
+ profile: str,
+) -> None:
+ _start_console(built_image, container_name, tty=True, env=())
+ home = "/opt/data" if profile == "default" else "/opt/data/profiles/worker"
+ if profile != "default":
+ config = "gateway:\n standalone: true\n" if profile == "standalone" else "{}\n"
+ setup = docker_exec(
+ container_name,
+ "python",
+ "-c",
+ (
+ "from pathlib import Path; "
+ "from hermes_cli.service_manager import S6ServiceManager; "
+ f"home = Path({home!r}); home.mkdir(parents=True); "
+ f"(home / 'config.yaml').write_text({config!r}); "
+ "S6ServiceManager().register_profile_gateway('worker', start_now=False)"
+ ),
+ )
+ assert setup.returncode == 0, setup.stderr
+
+ # Run the very helper Ink launches, including its authenticated ticket grant.
+ # Keep the token in the container and report only safe identity fields.
+ probe = docker_exec(
+ container_name,
+ "python",
+ "-c",
+ (
+ "import json, subprocess; "
+ "r = subprocess.run(['python', '/opt/hermes/ui-tui/scripts/gateway_bootstrap.py', '--start'], "
+ "capture_output=True, text=True, timeout=45); "
+ "assert r.returncode == 0, r.stderr; "
+ "grant = json.loads(r.stdout); "
+ "assert grant['url'] and grant['protocols'] and grant['instance_id']; "
+ "print(json.dumps({'profile_id': grant['profile_id']}))"
+ ),
+ timeout=60,
+ extra_docker_args=("-e", f"HERMES_HOME={home}"),
+ )
+ assert probe.returncode == 0, probe.stderr
+ assert json.loads(probe.stdout)["profile_id"] == home
+
+ owner = "worker" if profile == "standalone" else "default"
+ identity = docker_exec(
+ container_name,
+ "python",
+ "-c",
+ (
+ "from hermes_cli.service_manager import S6ServiceManager; "
+ "m = S6ServiceManager(); "
+ f"assert m.is_running('gateway-{owner}'); "
+ + (
+ "assert not m.is_running('gateway-worker')"
+ if profile == "multiplexed"
+ else ""
+ )
+ ),
+ )
+ assert identity.returncode == 0, identity.stderr
+ _emit_supervised_output(container_name)
+ retained, output = poll_container(
+ container_name, f"grep -F {_WARNING} {_CATCHALL}", user="root"
+ )
+ assert retained, output
+ logs = subprocess.run(
+ ["docker", "logs", container_name],
+ capture_output=True,
+ text=True,
+ timeout=10,
+ check=True,
+ )
+ assert _STDOUT not in logs.stdout + logs.stderr
+ assert _WARNING not in logs.stdout + logs.stderr
+
+
+def test_s6_discovery_refuses_another_install_root(
+ built_image: str, container_name: str
+) -> None:
+ _start_console(built_image, container_name, tty=True, env=())
+ result = docker_exec(
+ container_name,
+ "python",
+ "-c",
+ (
+ "import time; from pathlib import Path; "
+ "from hermes_cli.gateway_runtime import ensure_gateway_runtime; "
+ "receipt = ensure_gateway_runtime(Path('/tmp/foreign-home'), timeout=5); "
+ "assert receipt.state == 'inaccessible', receipt.state; "
+ "assert receipt.reason_code == 'profile_mismatch', receipt.reason_code"
+ ),
+ )
+ assert result.returncode == 0, result.stderr
diff --git a/tests/hermes_cli/test_gateway_runtime_service.py b/tests/hermes_cli/test_gateway_runtime_service.py
index 6d3756d9128..dfde9344411 100644
--- a/tests/hermes_cli/test_gateway_runtime_service.py
+++ b/tests/hermes_cli/test_gateway_runtime_service.py
@@ -358,6 +358,10 @@ def test_s6_runtime_ensure_starts_the_registered_gateway_slot(tmp_path, monkeypa
monkeypatch.setattr(service_manager, "_s6_running", lambda: True)
monkeypatch.setattr(service_manager, "S6ServiceManager", FakeS6ServiceManager)
+ read_text = Path.read_text
+ monkeypatch.setattr(Path, "read_text", lambda path, *args, **kwargs: (
+ str(home) if str(path).replace("\\", "/") == "/run/s6/container_environment/HERMES_HOME"
+ else read_text(path, *args, **kwargs)))
monkeypatch.setattr(service, "_systemd", lambda *_args: pytest.fail("s6 must not probe systemd"))
found = service.discover_existing_gateway_service(home, deadline=time.monotonic() + 5)
diff --git a/website/docs/user-guide/docker.md b/website/docs/user-guide/docker.md
index 1cf9351379e..dff97a0364a 100644
--- a/website/docs/user-guide/docker.md
+++ b/website/docs/user-guide/docker.md
@@ -86,6 +86,8 @@ This behavior applies to the s6-based image only. Earlier (tini-based) images st
See the [Where the logs go](#where-the-logs-go) section below for the full routing map (per-profile gateways, dashboard, boot reconciler, container-wide `docker logs`).
:::
+The TUI uses this same supervised gateway as its local backend, including when no messaging platform is configured. An interactive `--tui` launch starts the registered s6 gateway automatically and waits for it to become ready. Normal named profiles attach to the root gateway that serves them. Profiles explicitly configured with `gateway.standalone: true` use their own registered slot. Client startup does not install a service or bypass s6 with an unmanaged process.
+
:::note Tool-loop hard stops for unattended gateways
Unattended gateway and cron sessions enable tool-loop hard stops by default through `non_interactive_hard_stop_enabled`. Interactive CLI, TUI, Desktop, and ACP sessions remain warning-only. To opt an unattended deployment out in the profile's `config.yaml`: