Skip to content

The agent that grows with you

fix(launchers): fall back to the runtime store only when the tree has none

unsupportedpastelsa4648c5f58ba
diff --git a/hermes_cli/_launchers.py b/hermes_cli/_launchers.py
index 94a21d960c9..5e530c50b40 100644
--- a/hermes_cli/_launchers.py
+++ b/hermes_cli/_launchers.py
@@ -100,20 +100,24 @@ def _is_windows() -> bool:
return os.name == "nt"
-def resolve_store_python(repo_root: Path, *, honor_runtime_override: bool = True) -> Path | None:
+def resolve_store_python(repo_root: Path, *, publication: bool = False) -> 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).
+ Callers that PERSIST the result in a launcher (``stage_launcher`` and the
+ publication gates below) pass ``publication=True``: the tree's own store
+ wins over an inherited ``HERMES_RUNTIME_DIR``. The override names a store
+ for the running process (an e2e fixture, a desktop toolchain) that a later
+ scratch cleanup may delete, leaving the launcher to exit 127 forever
+ (#131745). It still supplies the store when the tree records none.
"""
- runtime = store_root(repo_root, honor_runtime_override=honor_runtime_override)
+ if publication:
+ own = _store_python(store_root(repo_root, honor_runtime_override=False))
+ if own is not None:
+ return own
+ return _store_python(store_root(repo_root))
+
+
+def _store_python(runtime: Path) -> Path | None:
rel = "python.exe" if _is_windows() else "bin/python3"
facts = runtime / "facts.json"
@@ -386,8 +390,8 @@ 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)
# 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)
+ # runtime override must not displace the tree's own interpreter.
+ store_python = resolve_store_python(repo_root, publication=True)
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":
@@ -457,7 +461,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, honor_runtime_override=False) is None:
+ if resolve_store_python(root, publication=True) is None:
return {"ok": True, "skipped": "no-store-python"}
try:
local = root / ".hermes" / "bin"
@@ -622,7 +626,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, honor_runtime_override=False) is None:
+ if resolve_store_python(repo_root, publication=True) 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 db8a515b390..3eb33eac398 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -3224,10 +3224,9 @@ def _prepare_service_launcher(*, system: bool = False, run_as_user: str | None =
owner = (uid, username)
token = set_hermes_home_override(home)
try:
- # 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:
+ # Publication gate: ask what stage_launcher will bind, so an inherited
+ # HERMES_RUNTIME_DIR cannot stand in for this install's store (#131745).
+ if resolve_store_python(root, publication=True) is None:
return # Externally owned Nix/developer runtime.
local = root / ".hermes" / "bin"
paths = ensure_install_launchers(root, local)
diff --git a/tests/hermes_cli/test_source_launcher_publication.py b/tests/hermes_cli/test_source_launcher_publication.py
index a3d84a0eceb..ff316f50501 100644
--- a/tests/hermes_cli/test_source_launcher_publication.py
+++ b/tests/hermes_cli/test_source_launcher_publication.py
@@ -632,3 +632,50 @@ def test_runtime_override_still_selects_the_runtime_python(tmp_path, monkeypatch
selected = _launchers.resolve_store_python(repo)
assert selected == foreign_store / "python-foreign" / "bin" / "python3"
+
+
+def _record_store_python(store: Path, interpreter: Path) -> Path:
+ python = store / "python-fixture" / "bin" / "python3"
+ python.parent.mkdir(parents=True)
+ python.symlink_to(interpreter)
+ (store / "facts.json").write_text(
+ json.dumps({"packages": {"python": {"entry": "python-fixture"}}}), encoding="utf-8")
+ return python
+
+
+def test_install_launchers_never_bind_a_fixture_store_under_scratch(tmp_path, monkeypatch):
+ # #131745's layout: a process aimed at an e2e fixture home under the
+ # install's own cache/scratch republishes the real install's launchers.
+ # Idle pruning later deletes that store, and the service exits 127.
+ repo, home, interpreter = fixture_tree(tmp_path, monkeypatch)
+ own = _launchers.resolve_store_python(repo)
+ fixture = home / "cache" / "scratch" / "hermes-e2e-media-overlap-run" / "hermes-home" / "tools"
+ _record_store_python(fixture, interpreter)
+ monkeypatch.setenv("HERMES_RUNTIME_DIR", str(fixture))
+
+ written = _launchers.ensure_install_launchers(repo, repo / ".hermes" / "bin")
+
+ assert len(written) == len(_launchers.WINDOWS_BIN_LAUNCHERS)
+ for path in written:
+ wrapper = Path(path).read_text(encoding="utf-8-sig")
+ assert "hermes-e2e-media-overlap-run" not in wrapper
+ assert shlex.quote(str(own)) in wrapper
+
+
+def test_publication_uses_the_runtime_store_when_the_tree_records_none(tmp_path, monkeypatch):
+ # The desktop source-backend contract keeps its store beside HERMES_HOME
+ # and names it through HERMES_RUNTIME_DIR. A tree with no store of its own
+ # still publishes from that one.
+ repo, home, interpreter = fixture_tree(tmp_path, monkeypatch)
+ (home / "tools" / "facts.json").unlink()
+ runtime = tmp_path / "runtime-tools"
+ python = _record_store_python(runtime, interpreter)
+ monkeypatch.setenv("HERMES_RUNTIME_DIR", str(runtime))
+
+ written = _launchers.ensure_install_launchers(repo, repo / ".hermes" / "bin")
+
+ assert len(written) == len(_launchers.WINDOWS_BIN_LAUNCHERS)
+ for path in written:
+ assert shlex.quote(str(python)) in Path(path).read_text(encoding="utf-8-sig")
diff --git a/tests/hermes_cli/test_update_launch_completion.py b/tests/hermes_cli/test_update_launch_completion.py
index a96e6283dcb..bdc09e08eaf 100644
--- a/tests/hermes_cli/test_update_launch_completion.py
+++ b/tests/hermes_cli/test_update_launch_completion.py
@@ -79,7 +79,7 @@ def _committed_checkout(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "home"))
monkeypatch.setattr(Path, "home", lambda: tmp_path)
monkeypatch.delenv("HERMES_DISABLE_LAZY_INSTALLS", raising=False)
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
return root
@@ -161,7 +161,7 @@ def test_failed_completion_tail_is_retried_without_rebuilding_dependencies(tmp_p
fact = runtime_facts_path(root)
syncs = []
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: fact.is_file())
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
def sync(extras=None, **kwargs):
syncs.append(extras)
@@ -206,7 +206,7 @@ def test_prepared_completion_import_does_not_start_another_tail(tmp_path, monkey
worker.touch()
venv_sync.arm_completion(root)
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: True)
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
monkeypatch.setattr(sys, "argv", [str(worker), "--source", str(root), "--finish-update", "--prepared"])
assert venv_sync.prepare_launch(root, sys.argv[1:]) is None
@@ -225,7 +225,7 @@ def test_same_named_script_outside_the_checkout_still_repairs(tmp_path, monkeypa
foreign.touch()
venv_sync.arm_completion(root)
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: True)
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
monkeypatch.setattr(sys, "argv", [str(foreign), "--prepared"])
assert venv_sync.prepare_launch(root, sys.argv[1:]) is None
@@ -241,7 +241,7 @@ def test_completion_tail_output_stays_off_stdout(tmp_path, monkeypatch, completi
root = _self_checkout(tmp_path, monkeypatch)
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: False)
monkeypatch.setattr(pm, "sync_venv", lambda *a, **kw: None)
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
venv_sync.prepare_launch(root, [])
assert completion_tail.kwargs["stdout"] is sys.__stderr__
@@ -262,7 +262,7 @@ def test_first_launch_syncs_without_marker_then_uses_completion_fact(tmp_path, m
fact = runtime_facts_path(root)
calls = []
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: fact.is_file())
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
def sync(extras=None, **kwargs):
calls.append((extras, kwargs))
@@ -361,7 +361,7 @@ def test_blessed_legacy_install_is_adopted_before_sync(tmp_path, monkeypatch, co
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: False)
calls = []
monkeypatch.setattr(pm, "sync_venv", lambda *args, **kw: calls.append(args))
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
assert venv_sync.prepare_launch(root, []) == Path(sys.executable)
assert json.loads((root / "install-stamp.json").read_text())["source"] == "adoption"
assert calls == [(["all"],)]
@@ -397,7 +397,7 @@ def test_live_old_update_blocks_launch_sync(tmp_path, monkeypatch):
# Fresh post-sync verification children may boot under a live updater.
from hermes_cli import _launchers
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: True)
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
assert venv_sync.prepare_launch(root, []) is None
assert marker.is_file()
@@ -447,7 +447,7 @@ def test_supervised_launch_leaves_a_pending_tail_to_the_cli(
pending.parent.mkdir(parents=True)
pending.write_text("owed\n")
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: True)
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
monkeypatch.delenv("HERMES_SUPERVISED_CHILD", raising=False)
monkeypatch.delenv("HERMES_S6_SUPERVISED_CHILD", raising=False)
monkeypatch.setenv(marker, value)
@@ -481,7 +481,7 @@ def test_supervised_launch_with_stale_dependencies_still_syncs(
syncs = []
monkeypatch.setattr(pm, "venv_is_current", lambda **kw: False)
monkeypatch.setattr(pm, "sync_venv", lambda *a, **kw: syncs.append(a))
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda _: Path(sys.executable))
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda _, **__: Path(sys.executable))
monkeypatch.setenv("HERMES_SUPERVISED_CHILD", "1")
venv_sync.prepare_launch(root, ["gateway", "run"])