Skip to content

The agent that grows with you

fix(linux): a PATH hit inside the checkout never persists as desktop entry Exec

Octopustankb5f3ed77a401
diff --git a/hermes_cli/linux_desktop_entry.py b/hermes_cli/linux_desktop_entry.py
index 44d3c0ea29b..49b726b4724 100644
--- a/hermes_cli/linux_desktop_entry.py
+++ b/hermes_cli/linux_desktop_entry.py
@@ -224,11 +224,22 @@ def _resolve_hermes_bin_for_desktop_entry(
# gnome-shell 50.x crashes when hermes.desktop changes while its ShellApp is STARTING (#110885).
# ``primary is None`` implies ``rerouted is None`` (the rerun only hides argv[0]), so only the
# probe can still find anything.
- if primary and rerouted is not None:
- return rerouted or primary
-
- # argv[0] was checkout-internal AND PATH had no `hermes` — common in stripped systemd user
- # sessions and autostart relaunches. Probe the installer's known wrapper locations; each
+ if primary and rerouted is not None and not _inside_checkout(
+ rerouted, checkout_root, original_argv0
+ ):
+ return rerouted
+ # A PATH hit inside this checkout is the same launch-context artifact as argv[0]: the
+ # desktop-update hand-off hands the updater <checkout>/venv/bin at the front of PATH, so
+ # persisting a reroute to the venv console script pins the entry to WHO wrote it. The next
+ # DE-launched context re-resolves to the durable wrapper and flips the bytes back — and
+ # every flip rewrites hermes.desktop, which arms the gnome-shell 50.x crash this function's
+ # callers guard against when the write lands inside a launch's STARTING window. Fall
+ # through to the durable probe below, exactly as a PATH miss does.
+
+ # argv[0] was checkout-internal AND PATH yielded no DURABLE launcher (miss, or a hit inside
+ # this checkout) — common in stripped systemd user sessions, autostart relaunches, and the
+ # update hand-off with <checkout>/venv/bin on PATH. Probe the installer's known wrapper
+ # locations; each
# candidate must be DE-safe and target THIS checkout (a foreign wrapper would make the entry
# stable-but-wrong). No durable wrapper → None, so resolve_exec_command emits its runnable
# module fallback instead of the self-regenerating checkout-internal form.
diff --git a/tests/hermes_cli/test_linux_desktop_entry.py b/tests/hermes_cli/test_linux_desktop_entry.py
index 1a8551beddc..58857fb08dc 100644
--- a/tests/hermes_cli/test_linux_desktop_entry.py
+++ b/tests/hermes_cli/test_linux_desktop_entry.py
@@ -422,6 +422,61 @@ def test_exec_uses_known_wrapper_when_path_lookup_misses(
assert exec_line == f"{known_wrapper} desktop"
+def test_exec_never_persists_a_checkout_internal_path_hit(tmp_path, xdg_home, monkeypatch):
+ """A PATH hit inside THIS checkout is a launch-context artifact, like argv[0].
+
+ The desktop-update hand-off hands the updater <checkout>/venv/bin at the
+ FRONT of PATH (apps/desktop/electron/main.ts), so argv[0] is the venv
+ console script — checkout-internal, correctly skipped as a durable
+ answer — and the reroute that hides argv[0] re-resolves over PATH and
+ hits THE SAME SCRIPT. The rerouted branch returned that hit outright,
+ persisting the venv form; the next DE launch re-resolves to the durable
+ wrapper and flips the bytes back. Alternating writers alternate the
+ file content (captured: wrapper -> venv -> wrapper inside one update
+ cycle), and every flip rewrites hermes.desktop. A rewrite landing
+ inside a grid launch's STARTING window is the arm for the gnome-shell
+ 50.x crash this module already guards against. A PATH hit inside the
+ checkout must fall through to the durable probe.
+ """
+ root = _make_project(tmp_path)
+ venv_script = root / "venv" / "bin" / "hermes"
+ venv_script.parent.mkdir(parents=True)
+ venv_script.write_text("#!/bin/bash\nexec true\n", encoding="utf-8")
+ venv_script.chmod(0o755)
+
+ known_wrapper = tmp_path / "path-home" / ".local" / "bin" / "hermes"
+ known_wrapper.parent.mkdir(parents=True)
+ known_wrapper.write_text(
+ f'#!/bin/bash\nexec {root / "venv" / "bin" / "python"} {root / "hermes"} "$@"\n',
+ encoding="utf-8",
+ )
+ known_wrapper.chmod(0o755)
+ monkeypatch.setenv("HOME", str(tmp_path / "path-home"))
+
+ # The hand-off's resolver chain: argv[0] = venv console script; with
+ # argv[0] hidden, the PATH rerun yields the SAME script.
+ def fake_resolve():
+ return sys.argv[0] or str(venv_script)
+
+ monkeypatch.setattr("hermes_cli.relaunch.resolve_hermes_bin", fake_resolve)
+ _argv0_context(monkeypatch, str(venv_script))
+ monkeypatch.setattr(lde, "refresh_desktop_databases", lambda _dir: [])
+
+ entry = lde.install_desktop_entry(root)
+ assert entry is not None
+ exec_line = _parse(entry.read_text(encoding="utf-8"))["Exec"]
+
+ assert exec_line == f"{known_wrapper} desktop"
+ assert str(venv_script) not in exec_line
+
+ # …and the same context a second time re-renders byte-identical content:
+ # the no-op guard then skips the rewrite entirely (no write, no rescan).
+ lde._probe_cache.clear()
+ entry2 = lde.install_desktop_entry(root)
+ assert entry2 is not None
+ assert entry2.read_text(encoding="utf-8") == entry.read_text(encoding="utf-8")
+
+
def test_exec_finds_known_wrapper_when_resolver_has_no_candidate(
tmp_path, xdg_home, monkeypatch
):