The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): an index.lock judged foreign after our git exited is never reclaimed by a later launch (review F06)
Teknium354965347152
diff --git a/hermes_cli/_early_recovery.py b/hermes_cli/_early_recovery.pyindex 1a96a7424df..81bfcb6fd08 100644--- a/hermes_cli/_early_recovery.py+++ b/hermes_cli/_early_recovery.py@@ -960,8 +960,15 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa# A killed git's index.lock goes first, before any git runs (an unresolvable or failing git# would otherwise strand it, and it refuses every later git command). Proven-dead only; after a# git that EXITED (``after_failure``) a lock now is another git's, never ours to drop.- foreign_lock = after_failure and (git_dir / "index.lock").exists()- index_free = foreign_lock or _release_dead_index_lock(git_dir, root)+ lock = git_dir / "index.lock"+ foreign_lock = after_failure and lock.exists()+ if foreign_lock:+ _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.+ known_foreign = bool(fields.get("foreign_lock", "").strip()) and \+ fields["foreign_lock"].strip() == _lock_identity(lock)+ index_free = foreign_lock or (not known_foreign and _release_dead_index_lock(git_dir, root))rollback = fields.get("rollback", "").strip()rollback = rollback if rollback in ("branch", "detach") else ""@@ -984,7 +991,7 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fa# so the restore below lands on ``pre`` (the code the update started from), not the broken tree.# Every step is checked, and the killed step's ``index.lock`` goes first, only once its git is# proven gone: until HEAD is on ``pre`` this marker is the rollback's only record.- reason = _redo_rollback_head(git, git_dir, root, pre, rollback, after_failure=after_failure)+ reason = _redo_rollback_head(git, git_dir, root, pre, rollback, after_failure=after_failure or known_foreign)if reason is not None:print(f"⚠ An interrupted `hermes update` rollback to {pre[:10]} cannot resume yet ({reason}); ""the next launch retries.", file=sys.stderr)@@ -1057,6 +1064,27 @@ def _restore_holding_claim(root: Path, marker: Path, *, after_failure: bool = Fareturn True+def _lock_identity(lock: Path) -> str:+ """``inode:mtime_ns`` of a lock file, "" when there is none: a recreated lock is a new generation."""+ try:+ st = lock.stat()+ except OSError:+ return ""+ return f"{st.st_ino}:{st.st_mtime_ns}"+++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)+ if not identity:+ return+ lines = [line for line in marker.read_text(encoding="utf-8-sig").splitlines()+ if not line.startswith("foreign_lock=")]+ tmp = marker.with_name(marker.name + ".tmp")+ tmp.write_text("\n".join([*lines, f"foreign_lock={identity}"]) + "\n", encoding="utf-8")+ os.replace(tmp, marker)++def _redo_rollback_head(git, git_dir: Path, root: Path, pre: str, mode: str, *, after_failure: bool) -> str | None:"""Move HEAD (and the index) back to ``pre`` for a killed syntax rollback; None, or why not yet.diff --git a/tests/hermes_cli/test_update_interrupted_pull.py b/tests/hermes_cli/test_update_interrupted_pull.pyindex f2bd540c1e6..ca9b2b1741a 100644--- a/tests/hermes_cli/test_update_interrupted_pull.py+++ b/tests/hermes_cli/test_update_interrupted_pull.py@@ -423,6 +423,29 @@ def test_a_rollback_settles_beside_an_unrelated_tracked_edit_and_keeps_it(tmp_paassert _git(root, "status", "--porcelain", "--untracked-files=no") == "M bulk/f1.txt"+def test_an_index_lock_judged_foreign_after_our_git_exited_is_never_reclaimed_later(tmp_path):+ """Our git exited mid-move and an index.lock was there (another git's, e.g. `git commit` in the editor+ with its fd closed): no later launch may delete THAT lock; a new lock generation is judged afresh."""+ root, pre, target = _broken_release(tmp_path, 2)+ _git(root, "reset", "-q", "--hard", pre)+ (root / "module.py").write_text("def broken(:\n", encoding="utf-8", newline="") # git's write landed+ marker = er.interrupted_pull_marker(root)+ marker.write_text(f"pid=0\npre={pre}\ntarget={target}\nstash=\n", encoding="utf-8", newline="")+ lock = root / ".git" / "index.lock"+ lock.write_bytes(b"")+ 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 judged foreign was deleted on the next launch"+ if sys.platform == "darwin" and not shutil.which("lsof"):+ pytest.skip("no lsof: a fresh dead lock cannot be proven dead here")+ lock.unlink()+ lock.write_bytes(b"") # a later generation (our own killed git's): the dead-lock proof applies+ os.utime(lock, ns=(1_000_000_000, 1_000_000_000))+ assert er.restore_interrupted_pull(root) is True+ assert not lock.exists() and not marker.exists()+ assert (root / "module.py").read_text(encoding="utf-8") == "good = True\n"++def test_a_launch_that_cannot_get_the_repair_claim_never_continues_from_the_torn_tree(tmp_path, monkeypatch):"""Another launch holds the restore claim past the wait while the marker says the tree is torn:this launch must stop (fail closed) instead of reporting 'nothing to repair' and importing it."""