Skip to content

The agent that grows with you

fix(update): a lock our own killed git left is reclaimed by the next launch, not judged foreign

Teknium5d4761f4f722
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.py
index 471efea3b64..4a5b3cad9b6 100644
--- a/hermes_cli/_early_recovery.py
+++ b/hermes_cli/_early_recovery.py
@@ -1031,7 +1031,10 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa
# git that EXITED (``after_failure``) a lock now is another git's, never ours to drop.
lock = git_dir / "index.lock"
foreign_lock = after_failure and lock.exists()
- if foreign_lock:
+ if foreign_lock and _lock_predates_move(fields, lock):
+ # Only the generation that was there before our git ran is another git's for sure. A lock
+ # that appeared during the move can be our own SIGKILLed git's: never judged here (this
+ # process cannot tell), but a later launch reclaims it once no git holds it (C15).
_remember_foreign_lock(marker, lock)
# The lock generation judged foreign when our git exited is never a killed git's to reclaim later:
# a live `git commit` in the editor holds it with its fd closed, which only Linux can still see.
@@ -1127,6 +1130,15 @@ def _lock_identity(lock: Path) -> str:
return f"{st.st_ino}:{st.st_mtime_ns}"
+def _lock_predates_move(fields: dict[str, str], lock: Path) -> bool:
+ """``lock`` is the generation ``arm_tree_move`` saw before the move's git ran. A marker without
+ the record (an older updater's, hand-written) cannot say: every lock counts as foreign."""
+ if "index_lock" not in fields:
+ return True
+ before = fields["index_lock"].strip()
+ return bool(before) and before == _lock_identity(lock)
+
+
def _remember_foreign_lock(marker: Path, lock: Path) -> None:
"""Record ``lock``'s identity in the marker (atomically: the marker is the restore's only record)."""
identity = _lock_identity(lock)
diff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.py
index 447e836e3e4..3e1a9431b33 100644
--- a/hermes_cli/update_cmd_commit.py
+++ b/hermes_cli/update_cmd_commit.py
@@ -28,6 +28,7 @@ from hermes_cli._early_recovery import (
RECOVERY_CLOSURE_INIT,
RECOVERY_CLOSURE_MANIFEST,
blob_id,
+ _lock_identity,
interrupted_pull_marker,
is_object_id,
recovery_closure_dir,
@@ -208,7 +209,10 @@ def arm_tree_move(git_cmd, root: Path, *, pre: str | None, target: str, stash: s
with suppress(OSError, subprocess.SubprocessError): # no closure: the in-tree repair still runs
publish_recovery_closure(git_cmd, root, pre)
# The restore's only record, durable before git writes: temp + fsync + rename, never in place.
+ # ``index_lock``: the lock generation already there before this move's git ran (empty: none). Only
+ # THAT one is another git's for sure; a lock that appears later can be our own killed git's.
write_durable_text(marker, f"pid={os.getpid()}\npre={pre or ''}\ntarget={target}\nstash={stash or ''}\n"
+ + f"index_lock={_lock_identity(marker.parent / 'index.lock')}\n"
+ (f"rollback={rollback}\nref={ref}\n" if rollback else "")
+ (f"git={git}\n" if (git := _absolute_git(git_cmd)) else ""))
return marker
diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.py
index 6e4bb63e265..7fbd8137e34 100644
--- a/tests/hermes_cli/test_update_interrupted_pull.py
+++ b/tests/hermes_cli/test_update_interrupted_pull.py
@@ -860,3 +860,41 @@ def test_a_rollback_whose_branch_was_switched_away_never_advises_a_reset(tmp_pat
err = capsys.readouterr().err
assert "was not resumed" in err and "reset --hard" not in err
assert "checkout main" in err and marker.exists()
+
+
+def test_a_lock_our_killed_rollback_git_left_is_reclaimed_by_the_next_launch(tmp_path):
+ """C15: the syntax rollback's own `reset -q` is SIGKILLed holding index.lock and the updater's
+ in-process settle (after_failure) recorded THAT lock as foreign, so every later launch printed
+ "cannot resume yet" forever. Only the lock generation that predates the move is foreign."""
+ if sys.platform == "darwin" and not shutil.which("lsof"):
+ pytest.skip("no lsof: a fresh dead lock cannot be proven dead here")
+ from hermes_cli import update_cmd_commit
+
+ root, pre, target = _broken_release(tmp_path, 2)
+ marker = update_cmd_commit.arm_tree_move(["git"], root, pre=pre, target=target, stash=None,
+ rollback="branch")
+ lock = root / ".git" / "index.lock"
+ lock.write_bytes(b"") # our killed git's: it appeared after the marker was armed
+ os.utime(lock, ns=(1_000_000_000, 1_000_000_000))
+
+ assert er.restore_interrupted_pull(root, after_failure=True) is False # the updater: never judges it
+ assert "foreign_lock=" not in marker.read_text(encoding="utf-8")
+ assert er.restore_interrupted_pull(root) is True # the next launch: proven dead, reclaimed
+ assert _git(root, "rev-parse", "HEAD") == pre and not lock.exists() and not marker.exists()
+
+
+def test_a_lock_that_predates_the_move_stays_foreign_after_our_git_exited(tmp_path):
+ """F05/F06 unchanged: the lock generation already there when the move was armed is another
+ git's (e.g. `git commit` waiting in the editor, fd closed); no later launch deletes it."""
+ from hermes_cli import update_cmd_commit
+
+ root, pre, target = _broken_release(tmp_path, 2)
+ lock = root / ".git" / "index.lock"
+ lock.write_bytes(b"")
+ marker = update_cmd_commit.arm_tree_move(["git"], root, pre=pre, target=target, stash=None,
+ rollback="branch")
+
+ assert er.restore_interrupted_pull(root, after_failure=True) is False
+ assert er.restore_interrupted_pull(root) is False
+ assert lock.exists() and marker.exists(), "a lock that predates the move was deleted"
+ assert _git(root, "rev-parse", "HEAD") == target