Skip to content

The agent that grows with you

fix(cli): never leave a dead launcher in front of a kept one

finn7637b5088e7a4f8
diff --git a/hermes_cli/_launchers.py b/hermes_cli/_launchers.py
index 69e87cae58a..f94b70902d8 100644
--- a/hermes_cli/_launchers.py
+++ b/hermes_cli/_launchers.py
@@ -272,7 +272,10 @@ def mint_launcher(
prefix = existing.read_bytes()[:archive.infolist()[0].header_offset]
shebangs = (f"#!{python_exe} -I\n".encode("utf-8"),
f'#!"{python_exe}" -I\n'.encode("utf-8"))
- if (any(prefix.endswith(shebang) for shebang in shebangs)
+ # Vendored distlib trails the shebang with an extra CRLF
+ # before the zip; compare the shebang line itself.
+ tail = prefix.rstrip(b"\r\n")
+ if (any(tail.endswith(shebang.rstrip(b"\r\n")) for shebang in shebangs)
and archive.read("__main__.py") == script.encode("utf-8")):
return existing
except (OSError, BadZipFile, KeyError):
@@ -483,8 +486,16 @@ def _kept_shared_launcher(name: str, local: Path, store: Path, own: Path | None)
data root must never repoint them at another root's interpreter (#123238):
once that root is deleted, every hermes breaks. Keep the file when it
execs a live interpreter outside *store*. Missing launchers, dead
- interpreters and same-store repins still publish."""
+ interpreters and same-store repins still publish. A launcher whose
+ interpreter is gone is never left in front of a kept one: PATHEXT resolves
+ the ``.exe`` first, so the kept ``.cmd`` would not be the file that runs."""
+ if own is None:
+ # No interpreter recorded for this root: nothing says whether the
+ # launcher is ours, and callers read the returned count as success, so
+ # a keep here would report a half-finished store as healthy.
+ return None
candidates = [local / name] if not _is_windows() else [local / f"{name}.exe", local / f"{name}.cmd"]
+ dead: list[Path] = []
for target in candidates:
try:
present = target.is_file() or target.is_symlink()
@@ -493,7 +504,10 @@ def _kept_shared_launcher(name: str, local: Path, store: Path, own: Path | None)
if not present:
continue
python = _launcher_python(target)
- if python is None or python == own or not python.is_file():
+ if python is None or python == own:
+ continue
+ if not python.is_file():
+ dead.append(target)
continue
try:
# Resolve the store's directories, not the interpreter: a store
@@ -503,6 +517,14 @@ def _kept_shared_launcher(name: str, local: Path, store: Path, own: Path | None)
except (OSError, RuntimeError, ValueError):
continue
if foreign:
+ for stale in dead:
+ # cmd.exe picks the .exe first; leaving a dead one would run
+ # instead of the command just kept. stage_launcher drops the
+ # same shadow when it has to mint a .cmd.
+ try:
+ stale.unlink(missing_ok=True)
+ except OSError:
+ pass
return target
return None
diff --git a/tests/hermes_cli/test_hermes_home_shared_launchers.py b/tests/hermes_cli/test_hermes_home_shared_launchers.py
index a97279fd79b..a0f39056561 100644
--- a/tests/hermes_cli/test_hermes_home_shared_launchers.py
+++ b/tests/hermes_cli/test_hermes_home_shared_launchers.py
@@ -10,6 +10,7 @@ import os
from pathlib import Path
import pm
+import pytest
from hermes_cli import _launchers
from hermes_cli import venv_sync
@@ -154,3 +155,55 @@ def test_launcher_python_reads_windows_cmd_fallback(tmp_path):
encoding="utf-8",
)
assert _launchers._launcher_python(target) == Path("C:\\fake home\\tools\\py\\python.exe")
+
+
+def test_no_store_interpreter_never_keeps_launchers(tmp_path, monkeypatch):
+ """A store with no recorded interpreter stays a failure, not a keep."""
+ default_home, _ = _make_home(tmp_path, "default")
+ temp_home, _ = _make_home(tmp_path, "temp")
+ repo = _make_repo(tmp_path)
+ local = repo / ".hermes" / "bin"
+
+ _isolate(tmp_path, monkeypatch, default_home)
+ _publish(repo)
+
+ # Default's launchers are live and outside temp's store, so temp's launch
+ # would keep them. With no interpreter recorded for temp, keeping would
+ # report a half-finished store as a successful publication.
+ _isolate(tmp_path, monkeypatch, temp_home)
+ (temp_home / "tools" / "facts.json").write_text(
+ json.dumps({"schema": 1, "packages": {}}), encoding="utf-8")
+ assert _launchers.resolve_store_python(repo) is None
+ assert _launchers.ensure_install_launchers(repo, local) == []
+
+
+def _write_dead_exe(target: Path, missing_python: Path) -> None:
+ """distlib-shaped launcher: loader stub, '#!<python> -I' shebang, zip."""
+ import io
+ import zipfile
+
+ payload = io.BytesIO()
+ with zipfile.ZipFile(payload, "w") as archive:
+ archive.writestr("__main__.py", "raise SystemExit(0)\n")
+ target.write_bytes(b"MZ" + b"\x00" * 64 + b"#!" + str(missing_python).encode()
+ + b" -I" + payload.getvalue())
+
+
[email protected](os.name != "nt", reason="PATHEXT picks .exe before .cmd on Windows only")
+def test_dead_exe_never_shadows_kept_cmd(tmp_path, monkeypatch):
+ default_home, default_python = _make_home(tmp_path, "default")
+ temp_home, _ = _make_home(tmp_path, "temp")
+ repo = _make_repo(tmp_path)
+ local = repo / ".hermes" / "bin"
+ local.mkdir(parents=True)
+ _write_dead_exe(local / "hermes.exe", tmp_path / "gone" / "python.exe")
+ (local / "hermes.cmd").write_text(
+ '@echo off\r\n"%s" -I -c "eA==" %%*\r\n' % default_python, encoding="utf-8")
+ assert _launchers._launcher_python(local / "hermes.exe") == tmp_path / "gone" / "python.exe"
+
+ _isolate(tmp_path, monkeypatch, temp_home)
+ written = [Path(path) for path in _launchers.ensure_install_launchers(repo, local)]
+ # The shared .cmd is live and foreign, so it is kept — and the dead .exe
+ # that would have run instead of it is gone.
+ assert local / "hermes.cmd" in written
+ assert not (local / "hermes.exe").exists()