The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(termux): steward-keyed service refusal for apt-termux installs
ethernet80231d87314a39c6
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.pyindex 3a7236127a0..b0dfd2042e9 100644--- a/hermes_cli/gateway.py+++ b/hermes_cli/gateway.py@@ -5873,6 +5873,29 @@ def _no_backend_exit(subcommand: str, reason: str) -> None:sys.exit(code)+def _is_apt_termux_install() -> bool:+ """True when this install is a sealed ``apt-termux`` tree.++ A Termux APT package is a sealed tree owned by the package manager with+ no service manager behind it. Keyed on the steward stamp+ (``sealed_steward``), never a platform probe, so the refusal follows the+ INSTALL PROVENANCE (our apt distribution's own stamp) rather than the+ ambient environment; the platform-shaped ``is_termux()`` lanes stay+ for generic on-device installs. Foreground process management (stop/+ status via the PID registry) is NOT gated: it works on every install.+ """+ from hermes_cli.steward import STEWARD_APT_TERMUX, sealed_steward++ return sealed_steward(PROJECT_ROOT) == STEWARD_APT_TERMUX+++def _service_mgmt_blocked() -> bool:+ """Service lanes are unavailable: a Termux device (platform probe) or a+ sealed apt-termux install (provenance probe). One predicate so a fourth+ service gate cannot forget the disjunction."""+ return is_termux() or _is_apt_termux_install()++def _handle_no_backend(subcommand: str, *, wsl: bool, s6: bool) -> None:"""Fallthrough when no service backend matched. Predicate order: WSL (only when ``wsl``) ->container (s6 slot hint only when ``s6``; ``start`` reaches here only when s6 isn't running) ->@@ -5920,7 +5943,7 @@ def _cmd_install(args):force = getattr(args, "force", False)system = getattr(args, "system", False)run_as_user = getattr(args, "run_as_user", None)- if is_termux():+ if _service_mgmt_blocked():_no_backend_exit("install", "termux")backend = _service_backend()if backend == "systemd":@@ -5944,7 +5967,7 @@ def _cmd_uninstall(args):managed_error("uninstall gateway service")returnsystem = getattr(args, "system", False)- if is_termux():+ if _service_mgmt_blocked():_no_backend_exit("uninstall", "termux")backend = _service_backend()if backend is not None:@@ -5964,7 +5987,7 @@ def _cmd_start(args):print(f"✓ Killed {killed} stale gateway process(es) across all profiles")_wait_for_gateway_exit(timeout=10.0, force_after=5.0)- if is_termux():+ if _service_mgmt_blocked():_no_backend_exit("start", "termux")backend = _service_backend()if backend is not None:@@ -6153,6 +6176,12 @@ def _cmd_migrate_legacy(args):remove_legacy_hermes_units(interactive=not yes, dry_run=dry_run)+def _cmd_service(args):+ """Windows SCM frontend (MSIX HermesGateway service): translate the SCM+ protocol onto the payload launcher (sealed-install surface)."""+ _windows_scm_service_command(getattr(args, "gateway_service_action", None))++_GATEWAY_SUBCOMMANDS = {None: _cmd_run, "run": _cmd_run, "setup": _cmd_setup, "install": _cmd_install,"uninstall": _cmd_uninstall, "start": _cmd_start, "stop": _cmd_stop, "restart": _cmd_restart,@@ -6376,8 +6405,3 @@ def _windows_scm_service_command(action: str | None) -> None:print_error(f"Unknown service action: {action!r} (on|off|status)")-def _cmd_service(args):- """Windows SCM frontend (MSIX HermesGateway service): translate the SCM- protocol onto the payload launcher (sealed-install surface)."""- _windows_scm_service_command(getattr(args, "gateway_service_action", None))-diff --git a/hermes_cli/steward.py b/hermes_cli/steward.pyindex 68d610acac6..0d5eb511ce7 100644--- a/hermes_cli/steward.py+++ b/hermes_cli/steward.py@@ -29,6 +29,7 @@ BUILD_INFO_NAME = "install-stamp.json"STEWARD_DESKTOP = "desktop-app"STEWARD_DOCKER = "docker"STEWARD_NIX = "nix"+STEWARD_APT_TERMUX = "apt-termux"# What `hermes update` says in a sealed tree, per steward — the refusal# table shared by the update admission gate. The fallback covers stewards@@ -48,8 +49,15 @@ STEWARD_UPDATE_MESSAGES = {"The store path is immutable. Update through your flake:\n"" nix flake update && rebuild your profile or system"),+ STEWARD_APT_TERMUX: (+ "✗ This Hermes runs from a Termux APT package.\n"+ "\n"+ "The package manager owns the code tree. Update with:\n"+ " pkg upgrade hermes-agent"+ ),}+_STEWARD_UPDATE_FALLBACK = ("✗ This Hermes install is managed by {steward}.\n""\n"@@ -75,6 +83,15 @@ _STEWARD_UNINSTALL_MESSAGES = {_STEWARD_DELETE_DATA_PREAMBLE +_STEWARD_DELETE_DATA_CLI),+ STEWARD_APT_TERMUX: (+ "✗ This Hermes was installed by a Termux APT package.\n"+ "\n"+ "The package manager owns the code tree — uninstall it with:\n"+ " pkg uninstall hermes-agent\n"+ "\n" ++ _STEWARD_DELETE_DATA_PREAMBLE ++ _STEWARD_DELETE_DATA_CLI+ ),STEWARD_NIX: ("✗ This Hermes was installed by Nix.\n""\n"diff --git a/hermes_cli/update_contract.py b/hermes_cli/update_contract.pyindex a3bdf508a39..b74a2bd7f0c 100644--- a/hermes_cli/update_contract.py+++ b/hermes_cli/update_contract.py@@ -68,6 +68,7 @@ def evaluate_update_admission(project_root: Path) -> Optional[UpdateRefusal]:# detect (the payload has no .install_method stamp and no .git).try:from hermes_cli.steward import (+ STEWARD_APT_TERMUX,STEWARD_DESKTOP,STEWARD_DOCKER,STEWARD_NIX,@@ -93,6 +94,14 @@ def evaluate_update_admission(project_root: Path) -> Optional[UpdateRefusal]:message=steward_update_message(steward),update_command=recommended_update_command_for_method("nix"),)+ if steward == STEWARD_APT_TERMUX:+ # The APT repo owns the code tree; `hermes update` must+ # never run in place — `pkg upgrade` replaces it wholesale.+ return UpdateRefusal(+ code=steward,+ message=steward_update_message(steward),+ update_command=recommended_update_command_for_method("apt"),+ )# desktop-app and future package managers: there is no CLI# remediation command — the steward's own instructions ARE the# remediation (recommended_update_command_for_method woulddiff --git a/tests/hermes_cli/test_gateway.py b/tests/hermes_cli/test_gateway.pyindex 4b914e93ec3..bb56e544493 100644--- a/tests/hermes_cli/test_gateway.py+++ b/tests/hermes_cli/test_gateway.py@@ -1269,3 +1269,65 @@ def test_find_profile_gateway_processes_strict_propagates_profile_listing_failurwith pytest.raises(RuntimeError, match="profile listing failed"):gateway.find_profile_gateway_processes(strict=True)++from pathlib import Path+++# ---------------------------------------------------------------------------+# Steward-keyed gateway posture: apt-termux sealed installs+#+# A Termux APT package ships a sealed tree with no service manager: the+# service install/uninstall/start lanes refuse (keyed on the steward+# stamp, not a platform probe), while foreground process management+# (stop via the PID registry) keeps working on every install.+# ---------------------------------------------------------------------------+++def _apt_termux_tree(tmp_path) -> Path:+ """A sealed (no .git) tree stamped as an apt-termux install."""+ root = tmp_path / "apt-termux"+ root.mkdir(parents=True, exist_ok=True)+ (root / "install-stamp.json").write_text(+ json.dumps({"distribution": "apt-termux"})+ )+ return root+++def test_apt_termux_probe_keys_on_steward_stamp(monkeypatch, tmp_path):+ """The probe fires only for a sealed apt-termux tree, by provenance."""+ root = _apt_termux_tree(tmp_path)+ monkeypatch.setattr(gateway, "PROJECT_ROOT", root)+ assert gateway._is_apt_termux_install() is True++ # A git checkout (the repo itself) is not steward-owned: not apt-termux.+ monkeypatch.setattr(gateway, "PROJECT_ROOT", Path(__file__).parent.parent)+ assert gateway._is_apt_termux_install() is False+++ "cmd_name",+ ["_cmd_install", "_cmd_uninstall", "_cmd_start"],+)+def test_service_commands_refuse_on_sealed_apt_termux(+ monkeypatch, tmp_path, capsys, cmd_name+):+ """Every gated service lane exits 1 with the no-backend message on a+ sealed apt-termux tree -- even without TERMUX env set (the refusal is+ provenance-keyed, not platform-keyed)."""+ import types as _types++ monkeypatch.delenv("TERMUX_VERSION", raising=False)+ monkeypatch.setenv("PREFIX", "/usr/local") # no termux prefix+ monkeypatch.setattr(gateway, "is_termux", lambda: False)+ monkeypatch.setattr(gateway, "is_managed", lambda: False)+ monkeypatch.setattr(gateway, "_refuse_from_inside_gateway", lambda *a: None)+ monkeypatch.setattr(gateway, "PROJECT_ROOT", _apt_termux_tree(tmp_path))++ with pytest.raises(SystemExit) as exc:+ getattr(gateway, cmd_name)(_types.SimpleNamespace(+ force=False, system=False, run_as_user=None, all=False,+ start_now=None, start_on_login=None, elevated_handoff=False,+ ))+ assert exc.value.code == 1+ out = capsys.readouterr().out+ assert "Termux" in outdiff --git a/tests/hermes_cli/test_update_contract.py b/tests/hermes_cli/test_update_contract.pyindex 8e6a328b082..4b574580f05 100644--- a/tests/hermes_cli/test_update_contract.py+++ b/tests/hermes_cli/test_update_contract.py@@ -174,3 +174,61 @@ def test_refusal_receipt_written_as_refused(tmp_path, monkeypatch):steps = {s["name"]: s for s in data["steps"]}assert "admission" in steps and steps["admission"]["ok"] is Falseassert "docker pull" in steps["admission"]["detail"]+++# ---------------------------------------------------------------------------+# Sealed-steward admission gate: apt-termux+# ---------------------------------------------------------------------------+++def _sealed_tree(tmp_path: Path, distribution: str) -> Path:+ """A sealed (no .git) tree stamped with ``distribution``."""+ root = tmp_path / "sealed"+ root.mkdir(parents=True, exist_ok=True)+ (root / "install-stamp.json").write_text(+ json.dumps({"distribution": distribution})+ )+ return root+++def test_admission_apt_termux_refuses_with_pkg_upgrade(tmp_path, monkeypatch):+ """A sealed apt-termux tree (no .git) is refused by the steward gate:+ the package manager owns the code tree, so remediation is pkg upgrade+ — never `hermes update`."""+ import hermes_cli.image_provenance as ip++ monkeypatch.setattr(ip, "IMAGE_PROVENANCE_PATH", tmp_path / "absent.json")+ monkeypatch.setattr(+ "hermes_cli.config.detect_install_method", lambda *a, **k: "git"+ )+ root = _sealed_tree(tmp_path, "apt-termux")+ refusal = evaluate_update_admission(root)+ assert refusal is not None+ assert refusal.code == "apt-termux"+ assert "pkg upgrade hermes-agent" in refusal.message+ assert "pkg upgrade hermes-agent" in refusal.update_command+++def test_admission_apt_termux_command_comes_from_steward_table(tmp_path, monkeypatch):+ """The apt-termux remediation command is read from the config module's+ ``_UPDATE_COMMAND_BY_METHOD`` table (the same one every install method+ reads) — not hardcoded inline in the steward refusal — so there is ONE+ source of truth for the update command."""+ import hermes_cli.config as config_mod+ import hermes_cli.image_provenance as ip++ monkeypatch.setattr(ip, "IMAGE_PROVENANCE_PATH", tmp_path / "absent.json")+ monkeypatch.setattr(+ "hermes_cli.config.detect_install_method", lambda *a, **k: "git"+ )+ # Prove the table is the source: a table edit flows into the refusal.+ monkeypatch.setitem(+ config_mod._UPDATE_COMMAND_BY_METHOD,+ "apt",+ "pkg upgrade hermes-agent --from-table",+ )+ root = _sealed_tree(tmp_path, "apt-termux")+ refusal = evaluate_update_admission(root)+ assert refusal is not None+ assert refusal.code == "apt-termux"+ assert refusal.update_command == "pkg upgrade hermes-agent --from-table"