Skip to content

The agent that grows with you

fix(docker): isolate supervised logs from interactive terminals

Gille94efb7527990
diff --git a/docker/entrypoint-dispatch.sh b/docker/entrypoint-dispatch.sh
index 927ed032f6d..0ac09131943 100644
--- a/docker/entrypoint-dispatch.sh
+++ b/docker/entrypoint-dispatch.sh
@@ -15,6 +15,14 @@
set -e
if [ "$$" -eq 1 ]; then
+ # The interactive CMD owns the terminal. Supervised gateways and dashboard
+ # output must go to s6's logger instead of overwriting the TUI or shell.
+ # Resolve this before /init wires the service descriptors, leaving headless
+ # docker logs and explicit s6 settings unchanged.
+ if [ -t 0 ] && [ -t 1 ]; then
+ export S6_LOGGING="${S6_LOGGING:-2}"
+ export S6_CMD_USE_TERMINAL="${S6_CMD_USE_TERMINAL:-1}"
+ fi
exec /init /opt/hermes/docker/main-wrapper.sh "$@"
fi
diff --git a/tests/docker/test_interactive_logging.py b/tests/docker/test_interactive_logging.py
new file mode 100644
index 00000000000..b52225624cd
--- /dev/null
+++ b/tests/docker/test_interactive_logging.py
@@ -0,0 +1,152 @@
+"""Keep supervised output away from an interactive Docker CMD's terminal."""
+
+from __future__ import annotations
+
+import subprocess
+
+import pytest
+
+from tests.docker.conftest import docker_exec, poll_container, wait_for_docker_logs
+
+_FOREGROUND = "CONSOLE_PROBE_FOREGROUND"
+_STDOUT = "CONSOLE_PROBE_SUPERVISED_STDOUT"
+_WARNING = "CONSOLE_PROBE_SUPERVISED_WARNING"
+_PROFILE = "console-probe"
+_CATCHALL = "/run/uncaught-logs/current"
+
+
+def _start_console(image: str, name: str, *, tty: bool, env: tuple[str, ...]) -> None:
+ args = ["docker", "run", "-d", "--name", name]
+ if tty:
+ args.append("-t")
+ for value in env:
+ args.extend(["-e", value])
+ probe = (
+ "if [ -t 0 ] && [ -t 1 ]; then tty=yes; else tty=no; fi; "
+ f'printf "{_FOREGROUND} tty=%s\\n" "$tty"; exec sleep infinity'
+ )
+ subprocess.run(
+ [*args, image, "sh", "-c", probe],
+ check=True,
+ capture_output=True,
+ text=True,
+ timeout=60,
+ )
+ output = wait_for_docker_logs(name, _FOREGROUND)
+ assert f"{_FOREGROUND} tty={'yes' if tty else 'no'}" in output
+
+
+def _emit_supervised_output(container: str) -> None:
+ # Exercise the production service/log scripts and privilege drop. Only the
+ # executable is synthetic, so no provider or messaging connection is needed.
+ script = (
+ f"#!/bin/sh\nprintf '%s\\n' '{_STDOUT}'\n"
+ f"printf '%s\\n' '{_WARNING}' >&2\nexec sleep infinity\n"
+ )
+ code = (
+ "from pathlib import Path; "
+ "from hermes_cli.service_manager import S6ServiceManager; "
+ "directory = Path('/opt/data/console-probe-bin'); "
+ "directory.mkdir(); "
+ "executable = directory / 'hermes'; "
+ f"executable.write_text({script!r}, encoding='utf-8'); "
+ "executable.chmod(0o755); "
+ "manager = S6ServiceManager(); "
+ f"manager.register_profile_gateway('{_PROFILE}', "
+ "extra_env={'PATH': str(directory) + ':/command:/usr/bin:/bin'})"
+ )
+ result = docker_exec(container, "python", "-c", code)
+ assert result.returncode == 0, result.stderr
+ ready, output = poll_container(
+ container,
+ f"grep -F {_STDOUT} /opt/data/logs/gateways/{_PROFILE}/current",
+ )
+ assert ready, f"Supervised stdout was not retained: {output}"
+
+
+ ("tty", "env", "isolated"),
+ [
+ (True, (), True),
+ (False, (), False),
+ (True, ("S6_LOGGING=0",), False),
+ (True, ("S6_LOGGING=1",), False),
+ ],
+ ids=["interactive", "headless", "explicit-console-logging", "explicit-tee-logging"],
+)
+def test_supervised_output_respects_terminal_ownership(
+ built_image: str,
+ container_name: str,
+ tty: bool,
+ env: tuple[str, ...],
+ isolated: bool,
+) -> None:
+ _start_console(built_image, container_name, tty=tty, env=env)
+ _emit_supervised_output(container_name)
+ if isolated:
+ logs = subprocess.run(
+ ["docker", "logs", container_name],
+ check=True,
+ capture_output=True,
+ text=True,
+ timeout=10,
+ )
+ console = logs.stdout + logs.stderr
+ assert _STDOUT not in console, console
+ assert _WARNING not in console, console
+ retained, output = poll_container(
+ container_name,
+ f"grep -F {_WARNING} {_CATCHALL}",
+ user="root",
+ )
+ assert retained, f"Supervised warning was not retained: {output}"
+ else:
+ console = wait_for_docker_logs(container_name, _WARNING)
+ assert _STDOUT in console, console
+
+
+def test_explicit_logging_preserves_cmd_terminal(
+ built_image: str,
+ container_name: str,
+) -> None:
+ _start_console(built_image, container_name, tty=True, env=("S6_LOGGING=2",))
+ _emit_supervised_output(container_name)
+ retained, output = poll_container(
+ container_name,
+ f"grep -F {_WARNING} {_CATCHALL}",
+ user="root",
+ )
+ assert retained, output
+
+
+def test_explicit_cmd_logging_is_not_overridden(
+ built_image: str,
+ container_name: str,
+) -> None:
+ subprocess.run(
+ [
+ "docker",
+ "run",
+ "-dt",
+ "--name",
+ container_name,
+ "-e",
+ "S6_LOGGING=2",
+ "-e",
+ "S6_CMD_USE_TERMINAL=0",
+ built_image,
+ "sh",
+ "-c",
+ f"echo {_FOREGROUND}; exec sleep infinity",
+ ],
+ check=True,
+ capture_output=True,
+ text=True,
+ timeout=60,
+ )
+ retained, output = poll_container(
+ container_name,
+ f"grep -F {_FOREGROUND} {_CATCHALL}",
+ user="root",
+ )
+ assert retained, output
diff --git a/website/docs/user-guide/docker.md b/website/docs/user-guide/docker.md
index ef9c3794281..1cf9351379e 100644
--- a/website/docs/user-guide/docker.md
+++ b/website/docs/user-guide/docker.md
@@ -358,6 +358,10 @@ The s6 container has four distinct log surfaces, and "why isn't my gateway showi
| **Boot reconciler** (records which profile gateways were restored on each container start) | `${HERMES_HOME}/logs/container-boot.log` (append-only audit log) | `tail -F ~/.hermes/logs/container-boot.log` |
| **Generic Hermes logs** (`agent.log`, `errors.log`) | `${HERMES_HOME}/logs/` (profile-aware) | `docker exec hermes hermes logs --follow [--level WARNING] [--session <id>]` |
+For an interactive container with terminal input and output (such as `docker run -it ... --tui` or `bash`), the foreground command owns the terminal. The entrypoint defaults to `S6_LOGGING=2` and `S6_CMD_USE_TERMINAL=1` so supervised gateway and dashboard output does not overwrite that screen. Their console output goes to s6's rotated catch-all log at `/run/uncaught-logs/current` inside the container. The per-profile gateway file logs and normal Hermes logs remain available at the paths above. The catch-all directory is runtime storage, so it does not survive a container restart or removal.
+
+Headless containers keep forwarding service output to `docker logs`. Explicit `S6_LOGGING` and `S6_CMD_USE_TERMINAL` settings take precedence over these interactive defaults.
+
Two practical consequences worth knowing:
- The file copy at `logs/gateways/<profile>/current` is what survives container restarts. `docker logs` only retains output from the current container's lifetime (and is wiped on `docker rm`); the rotated files persist on the bind-mounted volume.