Skip to content

The agent that grows with you

fix(gateway): bind launchd launchers to this install root

Teknium1d45b7865446
diff --git a/hermes_cli/gateway_runtime_service_identity.py b/hermes_cli/gateway_runtime_service_identity.py
index d58a044b090..14ef3928f79 100644
--- a/hermes_cli/gateway_runtime_service_identity.py
+++ b/hermes_cli/gateway_runtime_service_identity.py
@@ -5,7 +5,6 @@ programs. Manager-loaded values take precedence over filenames and disk copies.
"""
from __future__ import annotations
-import ast
import json
import os
from pathlib import Path
@@ -56,29 +55,24 @@ def verify_home(home: Path, configured: str) -> None:
raise ValueError("profile_mismatch")
-def _runtime_module(python: str, source: str) -> str | None:
- """The module an inline ``-I -c`` source runs when it is ``_launchers.runtime_command``'s bootstrap
- byte for byte (``installation_command`` persists it when there is no store Python); else None."""
- from hermes_cli._launchers import runtime_command
- literal = re.search(r"""sys\.path\.insert\(0, ('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*")\); """, source)
- try:
- root = Path(ast.literal_eval(literal[1])) if literal else None
- return next((module for module in ("hermes_cli.stderr_timestamp", "hermes_cli.main")
- if root and runtime_command(root, module=module, python=python)[3] == source), None)
- except (ValueError, SyntaxError, OSError, RuntimeError):
- return None
-
-
def _launch_module(argv: list[str]) -> tuple[str, list[str]] | None:
- """``(module, its argv)`` for the module launches the installer persists: ``python -m``, the source
- launcher's ``--run-module``, or the inline bootstrap. None for anything else."""
+ """``(module, its argv)`` for the module launches the installer persists: ``python -m``, or
+ ``_launchers.installation_command``'s two forms for THIS install root (gateway.PROJECT_ROOT): the
+ store ``<root>/.hermes/bin/hermes --run-module`` launcher, or ``runtime_command``'s inline bootstrap
+ regenerated byte for byte. A launcher or bootstrap of another checkout is not ours. None otherwise."""
+ from hermes_cli import gateway
+ from hermes_cli._launchers import runtime_command
+ root = Path(gateway.PROJECT_ROOT).resolve()
executable = Path(argv[0]).name.lower() if argv else ""
if re.fullmatch(r"python(?:w|\d+(?:\.\d+)*)?(?:\.exe)?", executable):
if argv[1:2] == ["-m"] and len(argv) > 2:
return argv[2], argv[3:]
- if argv[1:3] == ["-I", "-c"] and len(argv) > 3 and (module := _runtime_module(argv[0], argv[3])):
- return module, argv[4:]
- elif executable in {"hermes", "hermes.exe"} and argv[1:2] == ["--run-module"] and len(argv) > 2:
+ if argv[1:3] == ["-I", "-c"] and len(argv) > 3:
+ module = next((name for name in ("hermes_cli.stderr_timestamp", "hermes_cli.main")
+ if runtime_command(root, module=name, python=argv[0])[3] == argv[3]), None)
+ return (module, argv[4:]) if module else None
+ elif (argv[1:2] == ["--run-module"] and len(argv) > 2
+ and os.path.normcase(str(Path(argv[0]).resolve())) == os.path.normcase(str(root / ".hermes" / "bin" / "hermes"))):
return argv[2], argv[3:]
return None
diff --git a/tests/hermes_cli/test_gateway_runtime_service.py b/tests/hermes_cli/test_gateway_runtime_service.py
index 9588d4ab915..14e1c81f1a9 100644
--- a/tests/hermes_cli/test_gateway_runtime_service.py
+++ b/tests/hermes_cli/test_gateway_runtime_service.py
@@ -154,6 +154,8 @@ def _launchd_gateway_command(tmp_path, monkeypatch, *, store, profile=""):
("extra_jxa", "service_identity_unverified"), ("foreign_osascript", "service_identity_unverified"),
("tampered_bootstrap", "service_identity_unverified"), ("arbitrary_inline", "service_identity_unverified"),
("other_run_module", "service_identity_unverified"),
+ ("foreign_root_store", "service_identity_unverified"), ("foreign_root_inline", "service_identity_unverified"),
+ ("other_home", "foreign_root"), ("osascript_non_gateway", "service_identity_unverified"),
])
def test_launchd_verifies_the_osascript_wrapped_command_the_installer_writes(case, reason, tmp_path, monkeypatch):
"""The Local Network wrapper (#71206) is unwrapped only as its exact template, and the command inside
@@ -162,8 +164,18 @@ def test_launchd_verifies_the_osascript_wrapped_command_the_installer_writes(cas
from hermes_cli.gateway_launchd import launchd_program_arguments
from hermes_cli.gateway_runtime_service_identity import verify_launchd_loaded, verify_launchd_plist
home = tmp_path / ".hermes"
- command = _launchd_gateway_command(tmp_path, monkeypatch, store=case not in {"inline", "tampered_bootstrap"},
+ from hermes_cli import gateway as gw
+ command = _launchd_gateway_command(tmp_path, monkeypatch,
+ store=case not in {"inline", "tampered_bootstrap", "foreign_root_inline"},
profile="--profile stranger" if case == "store_profile" else "")
+ if case.startswith("foreign_root"): # the installer's own output, written for another checkout
+ from hermes_cli.gateway_launchd import _timestamped_stderr_gateway_command
+ root = gw.PROJECT_ROOT
+ monkeypatch.setattr(gw, "PROJECT_ROOT", tmp_path / "other-checkout")
+ command = _timestamped_stderr_gateway_command(tmp_path / "logs" / "gateway.error.log", external_supervisor=True)
+ monkeypatch.setattr(gw, "PROJECT_ROOT", root)
+ if case == "osascript_non_gateway":
+ command = ["/bin/sh", "-c", "id"]
if case == "tampered_bootstrap":
tampered = command[3].replace("import hermes_bootstrap; ", "import hermes_bootstrap; print('injected'); ")
assert tampered != command[3]
@@ -182,7 +194,8 @@ def test_launchd_verifies_the_osascript_wrapped_command_the_installer_writes(cas
argv[4] += ' $.system("id");'
if case == "foreign_osascript":
argv[0] = str(tmp_path / "osascript")
- env = dict(HERMES_HOME=str(home), HERMES_SUPERVISED_CHILD="1")
+ env = dict(HERMES_HOME=str(home if case != "other_home" else tmp_path / "elsewhere" / ".hermes"),
+ HERMES_SUPERVISED_CHILD="1")
definition = plistlib.loads(plistlib.dumps(dict(Label="ai.hermes.gateway", ProgramArguments=argv, EnvironmentVariables=env)))
output = ("gui/123/ai.hermes.gateway = {\n\tstate = not running\n\tprogram = " + argv[0] + "\n\targuments = {\n"
+ "\n".join("\t\t" + arg for arg in argv) + "\n\t}\n\tenvironment = {\n"