Skip to content

The agent that grows with you

fix(update): the tree-move marker records the updater's absolute git for the launch repair

Teknium47dcc975ce72
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index 51eff977f44..eecb6671500 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -244,16 +244,20 @@ def interrupted_pull_marker(root: Path) -> Path:
return _git_dir(root) / INTERRUPTED_PULL_MARKER
-def _git_executable() -> str:
+def _git_executable(recorded_by_updater: str = "") -> str:
"""The git ``hermes update`` runs (``_subprocess_compat.expose_pm_git``), without installing it.
- PATH's git first. A Windows install whose only git is the one install.ps1 staged in PM's store
+ The absolute git the killed updater recorded in its marker first: finding PM's copy needs
+ ``pm``, which imports ``hermes_constants``, and a merge killed while writing that module (or
+ anything else ``pm`` imports) leaves only this path to the repair. Then PATH's git. A Windows install whose only git is the one install.ps1 staged in PM's store
has none on PATH until the updater exposes it, so this falls back to that copy: PM's recorded
entry, or the lockfile's pinned entry the installer extracted without recording it. A bare
``git`` there dies with WinError 2 and the torn tree this repair exists for stays torn.
"""
import shutil
+ if recorded_by_updater and os.path.isfile(recorded_by_updater):
+ return recorded_by_updater
found = shutil.which("git")
if found:
return found
@@ -813,7 +817,7 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa
pre, target = fields.get("pre", "").strip(), fields.get("target", "").strip()
stash = fields.get("stash", "").strip()
- executable = _git_executable()
+ executable = _git_executable(fields.get("git", "").strip())
try:
from hermes_cli.update_custody import run_git
except Exception: # noqa: BLE001 - torn tree: plain spawns (see _checkout_custody)
diff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.py
index 5b0932e0244..ead24149cf4 100644
--- a/hermes_cli/update_cmd_commit.py
+++ b/hermes_cli/update_cmd_commit.py
@@ -115,10 +115,24 @@ def arm_tree_move(git_cmd, root: Path, *, pre: str | None, target: str, stash: s
"""
marker = interrupted_pull_marker(root)
marker.write_text(f"pid={os.getpid()}\npre={pre or ''}\ntarget={target}\nstash={stash or ''}\n"
- + (f"rollback={rollback}\n" if rollback else ""), encoding="utf-8")
+ + (f"rollback={rollback}\n" if rollback else "")
+ + (f"git={git}\n" if (git := _absolute_git(git_cmd)) else ""), encoding="utf-8")
return marker
+def _absolute_git(git_cmd) -> str:
+ """The git this run moves the tree with, as an absolute path for the next launch's repair.
+
+ A Windows install's git is often PM's store copy, on PATH only inside the updater
+ (``expose_pm_git``); the repair cannot look it up through ``pm`` when the killed move tore a
+ module ``pm`` imports (``hermes_constants``)."""
+ import shutil
+
+ head = (list(git_cmd) or ["git"])[0] if not isinstance(git_cmd, str) else git_cmd
+ found = shutil.which(head)
+ return os.path.abspath(found) if found else ""
+
+
def files_added_by(git_cmd, root: Path, pre: str, target: str | None) -> list[str]:
"""Paths ``target`` adds over ``pre``. A rollback's mixed reset un-tracks them and ``reset --hard``
never touches untracked files, so ``drop_added_files`` removes them by name afterwards."""
diff --git a/tests/fixtures/resolution_allowlist.json b/tests/fixtures/resolution_allowlist.json
index e9738000134..7039ea0b7eb 100644
--- a/tests/fixtures/resolution_allowlist.json
+++ b/tests/fixtures/resolution_allowlist.json
@@ -401,6 +401,12 @@
"symbol": "_serve_unit_recovery_available",
"kind": "bare_which"
},
+ {
+ "path": "hermes_cli/update_cmd_commit.py",
+ "symbol": "_absolute_git",
+ "kind": "bare_which",
+ "reason": "records, as an absolute path, the git this updater already runs (git_cmd[0] on the PATH expose_pm_git extended) so the launch-time repair can rerun it without importing pm"
+ },
{
"path": "hermes_cli/update_restart_recovery.py",
"symbol": "_systemctl_scopes",
diff --git a/tests/hermes_cli/test_source_launcher_publication.py b/tests/hermes_cli/test_source_launcher_publication.py
index ce71bf3e105..9508c40ef0a 100644
--- a/tests/hermes_cli/test_source_launcher_publication.py
+++ b/tests/hermes_cli/test_source_launcher_publication.py
@@ -564,7 +564,10 @@ def _torn_by_a_killed_merge(tmp_path, monkeypatch):
from hermes_cli import _early_recovery
marker = _early_recovery.interrupted_pull_marker(repo)
- marker.write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\n", encoding="utf-8", newline="")
+ # The updater records the git it moved the tree with: a Windows install's only git is PM's
+ # store copy, which the repair cannot look up through ``pm`` (it imports hermes_constants).
+ marker.write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\ngit={shutil.which('git')}\n",
+ encoding="utf-8", newline="")
constants.unlink()
(repo / ".git" / "index.lock").touch()
select_generation(repo, "one", "repaired")
@@ -581,6 +584,9 @@ def test_a_merge_killed_writing_hermes_constants_is_repaired_by_the_next_launch(
env.pop("HERMES_HOME", None) # the default-home pin itself needs hermes_constants
env.pop("HERMES_RUNTIME_DIR", None)
env.pop("PYTEST_CURRENT_TEST", None) # the repair stands down in a checkout pytest itself runs from
+ no_git = tmp_path / "path-without-git"
+ no_git.mkdir()
+ env["PATH"] = str(no_git) # as on that Windows install: no git on PATH outside the updater
if surface == "launcher":
out = tmp_path / "commands"
out.mkdir()
diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.py
index ef67b09e7cf..ce9474dc261 100644
--- a/tests/hermes_cli/test_update_interrupted_pull.py
+++ b/tests/hermes_cli/test_update_interrupted_pull.py
@@ -452,7 +452,7 @@ def test_a_dead_index_lock_is_released_even_when_git_cannot_run(tmp_path, monkey
lock.write_bytes(b"")
if sys.platform == "darwin" and not shutil.which("lsof"):
pytest.skip("no lsof: the dead lock cannot be proven dead here")
- monkeypatch.setattr(er, "_git_executable", lambda: str(tmp_path / "no-such-git"))
+ monkeypatch.setattr(er, "_git_executable", lambda recorded="": str(tmp_path / "no-such-git"))
assert er.restore_interrupted_pull(root) is False
assert not lock.exists(), "a missing git stranded a dead index.lock"
diff --git a/tests/hermes_cli/test_update_post_pull_syntax_guard.py b/tests/hermes_cli/test_update_post_pull_syntax_guard.py
index 8324bf435b1..9b5dd1ffeb9 100644
--- a/tests/hermes_cli/test_update_post_pull_syntax_guard.py
+++ b/tests/hermes_cli/test_update_post_pull_syntax_guard.py
@@ -121,3 +121,24 @@ def test_syntax_guards_skip_a_target_that_requires_a_newer_python(tmp_path, monk
git("checkout", "-q", newer)
update_cmd._rollback_if_pulled_syntax_error(["git"], same)
assert git("rev-parse", "HEAD") == newer
+
+
+def test_the_tree_move_marker_records_the_absolute_git_the_repair_reruns(tmp_path, monkeypatch):
+ """The next launch's repair cannot ask ``pm`` for the store git when the move tore a module ``pm``
+ imports, so the marker carries the absolute git this run resolved (PATH or ``expose_pm_git``)."""
+ from hermes_cli import _early_recovery
+ from hermes_cli import update_cmd_commit as commit
+
+ (tmp_path / ".git").mkdir()
+ fake = tmp_path / "store" / "git" / "cmd" / ("git.exe" if sys.platform == "win32" else "git")
+ fake.parent.mkdir(parents=True)
+ fake.write_text("", encoding="utf-8")
+ monkeypatch.setattr("shutil.which", lambda name: str(fake) if name == "git" else None)
+ marker = commit.arm_tree_move(["git", "-c", "gc.autoDetach=false"], tmp_path, pre="a" * 40,
+ target="b" * 40, stash=None)
+ fields = dict(line.partition("=")[::2] for line in marker.read_text(encoding="utf-8").splitlines())
+ assert fields["git"] == str(fake)
+ # ...and the repair prefers it to PATH (empty here) and to the pm lookup.
+ monkeypatch.setattr("shutil.which", lambda name: None)
+ assert _early_recovery._git_executable(fields["git"]) == str(fake)
+ assert _early_recovery._git_executable(str(tmp_path / "gone")) != str(tmp_path / "gone")