The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): install works as root and names the host command when sudo is absent
teknium15b1089a19c9b
diff --git a/hermes_cli/subcommands/computer_use_screen.py b/hermes_cli/subcommands/computer_use_screen.pyindex 5e76b521f99..d2bb5d6d026 100644--- a/hermes_cli/subcommands/computer_use_screen.py+++ b/hermes_cli/subcommands/computer_use_screen.py@@ -78,6 +78,9 @@ def _screen_install(args) -> int:except install.InstallBusy as exc:print(f"Bot Desktop: {exc}")return 1+ if rc == install.NO_SUDO:+ print("Bot Desktop: this host has no sudo. Run as root on the host:\n " + cmd.removeprefix("sudo "))+ return 1if rc != 0:print(f"Bot Desktop: installer exited {rc}")return rc or 1diff --git a/tests/tools/test_bot_desktop_install.py b/tests/tools/test_bot_desktop_install.pyindex 076dd2a55ad..96bcdc4de5d 100644--- a/tests/tools/test_bot_desktop_install.py+++ b/tests/tools/test_bot_desktop_install.py@@ -8,6 +8,8 @@ import pytestfrom tools.bot_desktop import install, runtime+_REAL_INSTALL_COMMAND = runtime.install_command # captured before the fixture pins a sudo line+@pytest.fixture(autouse=True)def _isolated_host(tmp_path, monkeypatch):@@ -131,3 +133,39 @@ def test_timeout_returns_and_frees_the_slot_even_when_a_descendant_survives(monkassert any(line.startswith("code ") and line != "code 0" for line in lines), linesassert any("timed out" in line for line in lines), linesinstall.release(install.claim()) # slot is free again+++def test_root_installs_without_sudo_and_without_asking(monkeypatch):+ """The official Docker image runs Hermes as uid 0 with no sudo binary: the package manager must be+ run directly, and the password card must never be raised for a user who already is root."""+ import subprocess++ monkeypatch.setattr(install.os, "geteuid", lambda: 0)+ monkeypatch.setattr("shutil.which", lambda name, *a, **k: None)+ monkeypatch.setattr(runtime, "package_manager", lambda: "apt")+ monkeypatch.setattr(runtime, "install_command", _REAL_INSTALL_COMMAND)+ spawned: list[list[str]] = []+ real_popen = subprocess.Popen++ def popen(argv, **kw):+ spawned.append(list(argv))+ return real_popen(["true"], **kw)++ monkeypatch.setattr(install.subprocess, "Popen", popen)+ code = install.install_packages(ask_password=lambda: pytest.fail("root was asked for a sudo password"),+ on_line=lambda _l: None)+ assert code == 0+ assert spawned and spawned[0][0] == "apt-get", spawned+++def test_no_sudo_binary_returns_the_host_command_instead_of_a_password_card(monkeypatch):+ """Unprivileged with no sudo on the host (minimal containers): a password card would be a dead end,+ the user needs the exact command to run on the host instead."""+ monkeypatch.setattr(install.os, "geteuid", lambda: 1000)+ monkeypatch.setattr("shutil.which", lambda name, *a, **k: None)+ monkeypatch.setattr(install.subprocess, "Popen", lambda *a, **k: pytest.fail("package manager spawned"))+ lines: list[str] = []+ code = install.install_packages(ask_password=lambda: pytest.fail("password card raised without sudo"),+ on_line=lines.append)+ assert code == install.NO_SUDO+ assert any("apt-get install" in line for line in lines), linesdiff --git a/tools/bot_desktop/install.py b/tools/bot_desktop/install.pyindex ef58869da1a..1f66d9ce6b8 100644--- a/tools/bot_desktop/install.py+++ b/tools/bot_desktop/install.py@@ -17,6 +17,7 @@ import loggingimport osimport selectorsimport shlex+import shutilimport signalimport subprocessimport threading@@ -32,6 +33,9 @@ _install_lock = threading.Lock()_running: set[str] = set()+NO_SUDO = -2 # install_packages: unprivileged host without sudo; the on_line stream carried the command to run as root++class InstallBusy(RuntimeError):pass@@ -62,7 +66,8 @@ def release(key: str) -> None:def install_packages(*, ask_password: Callable[[], str], on_line: Callable[[str], None],timeout_seconds: float = 900.0, claimed: bool = False) -> int:- """Run the package install; returns the process exit code (0 = success, ``-1`` = cancelled).+ """Run the package install; returns the process exit code (0 = success, ``-1`` = cancelled,+ :data:`NO_SUDO` = unprivileged host without sudo — the command to run by hand was streamed).``claimed=True``: the caller already holds the slot via :func:`claim`; it is released here either way."""key = hermes_home_key() if claimed else Nonetry:@@ -90,16 +95,21 @@ def _sudo_nopasswd() -> bool:def _run(cmd: str, *, ask_password: Callable[[], str], on_line: Callable[[str], None],timeout_seconds: float) -> int:argv = shlex.split(cmd)- assert argv[0] == "sudo", cmdstdin_payload: Optional[str] = None- if not _sudo_nopasswd():- password = ask_password() or ""- if not password:- on_line("install cancelled: no sudo password provided")- return -1- # -S: read the password from stdin; -p '': no prompt text mixed into the streamed output.- argv = ["sudo", "-S", "-p", "", *argv[1:]]- stdin_payload = password + "\n"+ if argv[0] == "sudo":+ if shutil.which("sudo") is None:+ # Minimal containers ship no sudo: a password card would be a dead end. Hand the human the+ # exact command for the host instead.+ on_line(f"install needs root and this host has no sudo; run on the host as root: {cmd[len('sudo '):]}")+ return NO_SUDO+ if not _sudo_nopasswd():+ password = ask_password() or ""+ if not password:+ on_line("install cancelled: no sudo password provided")+ return -1+ # -S: read the password from stdin; -p '': no prompt text mixed into the streamed output.+ argv = ["sudo", "-S", "-p", "", *argv[1:]]+ stdin_payload = password + "\n"on_line(f"$ {cmd}")env = {"DEBIAN_FRONTEND": "noninteractive", "LC_ALL": "C.UTF-8"}proc = subprocess.Popen( # windows-footgun: ok — Linux-only (is_supported_host)diff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.pyindex a9ce1923f09..eb307695da9 100644--- a/tools/bot_desktop/runtime.py+++ b/tools/bot_desktop/runtime.py@@ -89,15 +89,23 @@ def package_manager() -> Optional[str]:def install_command() -> Optional[str]:+ """The distro command that installs the Bot Desktop packages, as the human would type it on THIS host:+ prefixed with ``sudo`` unless Hermes already runs as root (the official Docker image is uid 0 with no+ sudo binary), so it is both what the pane shows and what :mod:`tools.bot_desktop.install` runs."""pm = package_manager()if pm is None:return Nonepkgs = " ".join(PACKAGES[pm])- return {- "apt": f"sudo apt-get install -y --no-install-recommends {pkgs}",- "dnf": f"sudo dnf install -y {pkgs}",- "pacman": f"sudo pacman -S --needed --noconfirm {pkgs}",+ body = {+ "apt": f"apt-get install -y --no-install-recommends {pkgs}",+ "dnf": f"dnf install -y {pkgs}",+ "pacman": f"pacman -S --needed --noconfirm {pkgs}",}[pm]+ return body if is_root() else f"sudo {body}"+++def is_root() -> bool:+ return hasattr(os, "geteuid") and os.geteuid() == 0@dataclass