Skip to content

The agent that grows with you

fix(launchers): never bind a published launcher to an inherited runtime

686f6c612c79189699d7
diff --git a/hermes_cli/_launchers.py b/hermes_cli/_launchers.py
index 2ffbad85aff..94a21d960c9 100644
--- a/hermes_cli/_launchers.py
+++ b/hermes_cli/_launchers.py
@@ -100,9 +100,20 @@ def _is_windows() -> bool:
return os.name == "nt"
-def resolve_store_python(repo_root: Path) -> Path | None:
- """Read PM's committed Python tool, without adopting unrecorded bytes."""
- runtime = store_root(repo_root)
+def resolve_store_python(repo_root: Path, *, honor_runtime_override: bool = True) -> Path | None:
+ """Read PM's committed Python tool, without adopting unrecorded bytes.
+
+ Callers that PERSIST the result inside an artifact (``stage_launcher``
+ and the publication gates below) must pass
+ ``honor_runtime_override=False``: a launcher written into the install's
+ bin dir has to boot the install's own store python, never a runtime
+ directory an inherited ``HERMES_RUNTIME_DIR`` names. An e2e or bundle
+ environment that spawns the real CLI with its own ``HERMES_RUNTIME_DIR``
+ must not be able to repoint the production launchers at a store that a
+ later scratch cleanup deletes (#131745: exit-127 crash loop after
+ reboot).
+ """
+ runtime = store_root(repo_root, honor_runtime_override=honor_runtime_override)
rel = "python.exe" if _is_windows() else "bin/python3"
facts = runtime / "facts.json"
@@ -374,7 +385,9 @@ def _publish_conveniences(root: Path, out_dir: Path, names, *, create: bool = Tr
def stage_launcher(name: str, repo_root: Path, out_dir: Path) -> Path | None:
"""Publish one launcher bound to store Python, or refuse missing tools."""
repo_root = Path(repo_root)
- store_python = resolve_store_python(repo_root)
+ # A launcher outlives the process that writes it, so the inherited
+ # runtime override must not select its interpreter.
+ store_python = resolve_store_python(repo_root, honor_runtime_override=False)
if store_python is not None:
path = mint_launcher(name, repo_root, out_dir, store_python, None)
if path is not None and path.suffix == ".cmd":
@@ -444,7 +457,7 @@ def expose_cli(project_root: Path | None = None, *, create: bool = True) -> dict
return {"ok": True, "skipped": "bundle-owns-launchers"}
if read_install_stamp(root).get("updateMechanism") == "external":
return {"ok": True, "skipped": "externally-owned"}
- if resolve_store_python(root) is None:
+ if resolve_store_python(root, honor_runtime_override=False) is None:
return {"ok": True, "skipped": "no-store-python"}
try:
local = root / ".hermes" / "bin"
@@ -609,7 +622,7 @@ if __name__ == "__main__":
parser.add_argument("out_dir", type=Path)
args = parser.parse_args()
repo_root = Path(__file__).resolve().parents[1]
- if resolve_store_python(repo_root) is None:
+ if resolve_store_python(repo_root, honor_runtime_override=False) is None:
parser.exit(1, "hermes: store interpreter is missing; finish pm install before publishing launchers\n")
args.out_dir.mkdir(parents=True, exist_ok=True)
written = ensure_install_launchers(repo_root, args.out_dir)
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 4730718d3d8..db8a515b390 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -3224,7 +3224,10 @@ def _prepare_service_launcher(*, system: bool = False, run_as_user: str | None =
owner = (uid, username)
token = set_hermes_home_override(home)
try:
- if resolve_store_python(root) is None:
+ # Publication gate: an inherited HERMES_RUNTIME_DIR must not make a
+ # foreign store look publishable, or the service launcher would boot
+ # a runtime the environment owns instead of this install's (#131745).
+ if resolve_store_python(root, honor_runtime_override=False) is None:
return # Externally owned Nix/developer runtime.
local = root / ".hermes" / "bin"
paths = ensure_install_launchers(root, local)
diff --git a/pm/environments.py b/pm/environments.py
index c520fc2e6c8..45734eb05a0 100644
--- a/pm/environments.py
+++ b/pm/environments.py
@@ -112,11 +112,19 @@ def base_venv(project_root: Path) -> Path:
return payload_venv(project_root) or project_venv_dir(Path(project_root).resolve()) or Path(project_root).resolve() / "venv"
-def store_root(project_root: Path) -> Path:
- """Resolve a payload-relative or stamped store before PM imports."""
- override = os.environ.get("HERMES_RUNTIME_DIR")
- if override:
- return Path(override).resolve()
+def store_root(project_root: Path, *, honor_runtime_override: bool = True) -> Path:
+ """Resolve a payload-relative or stamped store before PM imports.
+
+ ``HERMES_RUNTIME_DIR`` exists so a running process can point PM at a
+ non-default runtime location. Publication paths must pass
+ ``honor_runtime_override=False``: a persisted artifact (an installed
+ launcher) has to bind the store of the tree it serves, never a runtime
+ directory inherited through the environment.
+ """
+ if honor_runtime_override:
+ override = os.environ.get("HERMES_RUNTIME_DIR")
+ if override:
+ return Path(override).resolve()
root = Path(project_root).resolve()
manifest_path = root.parent / "manifest.json"
if manifest_path.is_file():
diff --git a/tests/hermes_cli/test_source_launcher_publication.py b/tests/hermes_cli/test_source_launcher_publication.py
index 85e6acc119f..a3d84a0eceb 100644
--- a/tests/hermes_cli/test_source_launcher_publication.py
+++ b/tests/hermes_cli/test_source_launcher_publication.py
@@ -550,3 +550,85 @@ def test_update_import_probe_uses_selected_dependencies(tmp_path, monkeypatch):
(repo / "hermes_integrity_probe.py").write_text("import selected_probe\n", encoding="utf-8")
monkeypatch.setattr(update_cmd, "_UPDATE_CRITICAL_MODULES", ("hermes_integrity_probe",))
assert update_cmd_validation._critical_module_import_failures(repo, report_runtime_errors=True) == {}
+
+
+def test_stage_launcher_ignores_an_inherited_runtime_directory(tmp_path, monkeypatch):
+ # #131745: a persisted launcher must boot the install's own store python.
+ # An inherited HERMES_RUNTIME_DIR (e2e fixture, desktop toolchain, PM
+ # subprocess env) names a runtime directory for the RUNNING process, never
+ # for the artifacts the process publishes — that store can be a scratch
+ # tree a later cleanup deletes, leaving the launcher dead (exit 127).
+ repo, home, interpreter = fixture_tree(tmp_path, monkeypatch)
+ store = home / "tools"
+ tree_python = store / "python-A" / "bin" / "python3"
+ tree_python.parent.mkdir(parents=True)
+ tree_python.symlink_to(interpreter)
+ (store / "facts.json").write_text(
+ json.dumps({"packages": {"python": {"entry": "python-A"}}}), encoding="utf-8")
+ foreign_store = tmp_path / "foreign" / "tools"
+ foreign_store.mkdir(parents=True)
+ foreign_python = foreign_store / "python-foreign" / "bin" / "python3"
+ foreign_python.parent.mkdir(parents=True)
+ foreign_python.symlink_to(interpreter)
+ (foreign_store / "facts.json").write_text(
+ json.dumps({"packages": {"python": {"entry": "python-foreign"}}}), encoding="utf-8")
+ monkeypatch.setenv("HERMES_RUNTIME_DIR", str(foreign_store))
+
+ # Publication callers create the bin dir before staging (see
+ # ensure_install_launchers); the launcher writer does not invent it.
+ out = repo / ".hermes" / "bin"
+ out.mkdir(parents=True, exist_ok=True)
+ published = _launchers.stage_launcher("hermes", repo, out)
+
+ assert published is not None
+ wrapper = published.read_text(encoding="utf-8-sig")
+ assert "python-A" in wrapper
+ assert "python-foreign" not in wrapper
+
+
+def test_service_launcher_binds_the_tree_store_despite_inherited_runtime_override(tmp_path, monkeypatch):
+ from hermes_cli import gateway
+
+ repo, home, interpreter = fixture_tree(tmp_path, monkeypatch)
+ monkeypatch.setattr(gateway, "PROJECT_ROOT", repo)
+ store = home / "tools"
+ tree_python = store / "python-A" / "bin" / "python3"
+ tree_python.parent.mkdir(parents=True)
+ tree_python.symlink_to(interpreter)
+ (store / "facts.json").write_text(
+ json.dumps({"packages": {"python": {"entry": "python-A"}}}), encoding="utf-8")
+ foreign_store = tmp_path / "foreign" / "tools"
+ foreign_store.mkdir(parents=True)
+ foreign_python = foreign_store / "python-foreign" / "bin" / "python3"
+ foreign_python.parent.mkdir(parents=True)
+ foreign_python.symlink_to(interpreter)
+ (foreign_store / "facts.json").write_text(
+ json.dumps({"packages": {"python": {"entry": "python-foreign"}}}), encoding="utf-8")
+ monkeypatch.setenv("HERMES_RUNTIME_DIR", str(foreign_store))
+
+ gateway._prepare_service_launcher()
+
+ wrapper = (repo / ".hermes" / "bin" / "hermes").read_text(encoding="utf-8-sig")
+ assert "python-A" in wrapper
+ assert "python-foreign" not in wrapper
+
+
+def test_runtime_override_still_selects_the_runtime_python(tmp_path, monkeypatch):
+ # The publication split must not mute the override for execution paths:
+ # resolving a python to RUN still honors HERMES_RUNTIME_DIR by default.
+ repo, home, interpreter = fixture_tree(tmp_path, monkeypatch)
+ foreign_store = tmp_path / "foreign" / "tools"
+ foreign_store.mkdir(parents=True)
+ foreign_python = foreign_store / "python-foreign" / "bin" / "python3"
+ foreign_python.parent.mkdir(parents=True)
+ foreign_python.symlink_to(interpreter)
+ (foreign_store / "facts.json").write_text(
+ json.dumps({"packages": {"python": {"entry": "python-foreign"}}}), encoding="utf-8")
+ monkeypatch.setenv("HERMES_RUNTIME_DIR", str(foreign_store))
+
+ selected = _launchers.resolve_store_python(repo)
+
+ assert selected == foreign_store / "python-foreign" / "bin" / "python3"