Skip to content

The agent that grows with you

fix(cli): make shallow-boundary repair survive the prune and the graph-safety review findings

kshitijk4poor966fb375fcd3
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.py
index 7374e53e925..a6607329593 100644
--- a/hermes_cli/gitlock.py
+++ b/hermes_cli/gitlock.py
@@ -144,7 +144,12 @@ def _git_stdout_lines(repo_root: Path, args: List[str]) -> List[str]:
def _batch_missing_parents(repo_root: Path, candidates: List[str]) -> set[str]:
- """Return local commit objects whose parent objects are missing."""
+ """Return local commit objects whose parent objects are missing.
+
+ Parents are read from the commit *header* only (lines before the first blank
+ line) — a ``parent <sha>`` line inside a commit message body is prose, not
+ an edge.
+ """
if not candidates:
return set()
try:
@@ -175,13 +180,13 @@ def _batch_missing_parents(repo_root: Path, candidates: List[str]) -> set[str]:
if data[cursor:cursor + 1] != b"\n":
return set()
cursor += 1
- decoded = body.decode(errors="replace")
- commit_parents = {
- fields[1]
- for line in decoded.splitlines()
- for fields in [line.split()]
- if len(fields) >= 2 and fields[0] == "parent"
- }
+ commit_parents = set()
+ for line in body.split(b"\n"):
+ if not line: # blank line ends the commit header block
+ break
+ fields = line.split()
+ if len(fields) >= 2 and fields[0] == b"parent":
+ commit_parents.add(fields[1].decode())
parents_by_commit[candidate] = commit_parents
parents.update(commit_parents)
elif len(header) < 2 or header[1] != b"missing":
@@ -195,6 +200,8 @@ def _batch_missing_parents(repo_root: Path, candidates: List[str]) -> set[str]:
capture_output=True,
timeout=10,
)
+ if check.returncode != 0:
+ return set()
missing = {
line.split()[0]
for line in check.stdout.decode(errors="replace").splitlines()
@@ -206,39 +213,99 @@ def _batch_missing_parents(repo_root: Path, candidates: List[str]) -> set[str]:
return set()
-def repair_broken_shallow_boundaries(repo_root: Path) -> int:
- """Repair reflog-only shallow boundaries dropped by #108286's prune bug.
+def _shallow_file_path(repo_root: Path) -> Optional[Path]:
+ """Resolve ``.git/shallow`` via git, or None when the repo has none."""
+ shallow_rel = _git_stdout_lines(repo_root, ["rev-parse", "--git-path", "shallow"])
+ if not shallow_rel:
+ return None
+ shallow_path = Path(shallow_rel[0])
+ if not shallow_path.is_absolute():
+ shallow_path = Path(repo_root) / shallow_rel[0]
+ return shallow_path if shallow_path.is_file() else None
+
+
+class _ShallowLock:
+ """Git's own ``shallow.lock`` protocol, so a concurrent ``git fetch`` that
+ writes ``.git/shallow`` between our read and our write is never clobbered:
+ the fetch fails fast on the lock and we re-read before writing."""
- This complements the prevention fix in PR #108290: existing corrupted installs need
- boundaries reconstructed because their broken history prevents the reflogs from expiring.
+ def __init__(self, shallow_path: Path):
+ self._path = shallow_path
+ self._lock_path = shallow_path.with_name(shallow_path.name + ".lock")
+
+ def __enter__(self) -> "_ShallowLock":
+ try:
+ fd = os.open(self._lock_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
+ except FileExistsError:
+ raise RuntimeError(f"shallow lock held: {self._lock_path}")
+ except OSError as exc:
+ raise RuntimeError(f"cannot create shallow lock: {exc}") from exc
+ try:
+ os.write(fd, b"hermes shallow maintenance\n")
+ finally:
+ os.close(fd)
+ return self
+
+ def __exit__(self, *exc_info) -> None:
+ try:
+ self._lock_path.unlink()
+ except FileNotFoundError:
+ pass
+
+
+def repair_broken_shallow_boundaries(repo_root: Path) -> int:
+ """Re-append shallow boundaries for reflog-reachable commits whose parents
+ were never fetched (#108286).
+
+ A reflog-only commit whose graft was pruned leaves the repo unwalkable
+ (``gc``/``fsck``/``fetch`` fail) and unable to self-heal: reflog expiry
+ happens during ``git gc``, which is exactly what the corruption breaks.
+ Only reflog-reachable commits are considered, so unrelated object loss is
+ never re-labelled as shallow history. Returns the number of boundaries
+ appended; never raises.
"""
try:
- shallow_rel = _git_stdout_lines(repo_root, ["rev-parse", "--git-path", "shallow"])
- if not shallow_rel:
- return 0
- shallow_path = Path(shallow_rel[0])
- if not shallow_path.is_absolute():
- shallow_path = Path(repo_root) / shallow_path
- if not shallow_path.is_file():
- return 0
- original = shallow_path.read_text(encoding="utf-8")
- existing = {line for line in original.splitlines() if line}
- if not existing:
+ shallow_path = _shallow_file_path(repo_root)
+ if shallow_path is None:
return 0
- candidates = _git_stdout_lines(repo_root, ["cat-file", "--batch-all-objects", "--batch-check"])
- candidates = sorted({line.split()[0] for line in candidates
- if len(line.split()) >= 2 and line.split()[1] == "commit"})
- candidates = sorted(set(candidates + _git_stdout_lines(
- repo_root, ["reflog", "show", "--all", "--format=%H"])))
- broken = _batch_missing_parents(repo_root, candidates)
- repaired = broken - existing
- if not repaired:
+ # Cheap gate: repair only when the walk the corruption breaks already fails.
+ probe = subprocess.run(
+ ["git", "rev-list", "--count", "--all", "--reflog"],
+ cwd=str(repo_root), capture_output=True, timeout=10,
+ )
+ if probe.returncode == 0:
return 0
- tmp_path = shallow_path.with_name(shallow_path.name + ".hermes-repair")
- tmp_path.write_text("\n".join(sorted(existing | repaired)) + "\n", encoding="utf-8")
- os.replace(tmp_path, shallow_path)
+ with _ShallowLock(shallow_path):
+ original = shallow_path.read_text(encoding="utf-8")
+ existing = {line for line in original.splitlines() if line}
+ if not existing:
+ return 0
+ # Boundary candidates: commits recorded as *fetch tips* in remote-tracking
+ # refs' reflogs (NOT --batch-all-objects, and not HEAD's reflog): the bug
+ # class is fetched tips whose graft the prune dropped, and restricting to
+ # fetch-recorded tips is what keeps unrelated object loss (a deleted parent
+ # of a locally-created commit) from being re-labelled as shallow history.
+ # On an already-corrupted repo a rev-list --reflog walk is exactly what
+ # fails, so read the reflog hash list directly.
+ reflog = _git_stdout_lines(
+ repo_root, ["reflog", "show", "--all", "--format=%H%x00%gD"])
+ candidates = sorted({
+ sha
+ for entry in reflog
+ for sha, selector in [entry.split("\x00", 1)]
+ if selector.startswith("refs/remotes/")
+ })
+ broken = _batch_missing_parents(repo_root, candidates)
+ repaired = broken - existing
+ if not repaired:
+ return 0
+ tmp_path = shallow_path.with_name(shallow_path.name + ".hermes-repair")
+ tmp_path.write_text("\n".join(sorted(existing | repaired)) + "\n", encoding="utf-8")
+ os.replace(tmp_path, shallow_path)
if not _git_stdout_lines(repo_root, ["rev-list", "--count", "--all", "--reflog"]):
- shallow_path.write_text(original, encoding="utf-8")
+ with _ShallowLock(shallow_path):
+ shallow_path.write_text(original, encoding="utf-8")
+ logger.debug("shallow boundary repair self-check failed; file restored")
return 0
logger.info("Restored %d broken shallow boundary(ies) in %s", len(repaired), repo_root)
return len(repaired)
@@ -248,7 +315,7 @@ def repair_broken_shallow_boundaries(repo_root: Path) -> int:
def prune_stale_shallow_grafts(repo_root: Path) -> int:
- """Drop ``.git/shallow`` graft lines no live ref still points at (#105951).
+ """Drop ``.git/shallow`` graft lines no live ref or reflog still points at (#105951).
Every ``git fetch --depth 1`` appends the fetched tip to ``.git/shallow`` as a new
graft and never removes the previous one, so a long-lived shallow installer checkout
@@ -257,37 +324,37 @@ def prune_stale_shallow_grafts(repo_root: Path) -> int:
on every run. Keep only the boundaries that still protect referenced tips (HEAD,
FETCH_HEAD, and every ref tip): the dropped commits are already unreachable and their
objects are left for ``git gc``. Returns the number of graft lines removed; never
- raises, and restores the original file if the trimmed set breaks history walking.
+ raises, and restores the original file if the trimmed set breaks history walking
+ (including the ``--reflog`` walk, so a graft a reflog-only commit still needs is
+ never dropped, #108286).
"""
try:
- shallow_rel = _git_stdout_lines(repo_root, ["rev-parse", "--git-path", "shallow"])
- if not shallow_rel:
- return 0
- shallow_path = Path(shallow_rel[0])
- if not shallow_path.is_absolute():
- shallow_path = Path(repo_root) / shallow_path
- if not shallow_path.is_file():
- return 0
- lines = [line for line in shallow_path.read_text(encoding="utf-8").splitlines() if line]
- if not lines:
- return 0
- keep = set(lines) & {
- *(_git_stdout_lines(repo_root, ["rev-parse", "HEAD"]) or []),
- *(_git_stdout_lines(repo_root, ["rev-parse", "--verify", "--quiet", "FETCH_HEAD"]) or []),
- *_git_stdout_lines(repo_root, ["for-each-ref", "--format=%(objectname)"]),
- }
- if len(keep) == len(lines):
+ shallow_path = _shallow_file_path(repo_root)
+ if shallow_path is None:
return 0
- original = shallow_path.read_text(encoding="utf-8")
- tmp_path = shallow_path.with_name(shallow_path.name + ".hermes-prune")
- tmp_path.write_text("\n".join(sorted(keep)) + "\n", encoding="utf-8")
- os.replace(tmp_path, shallow_path)
+ with _ShallowLock(shallow_path):
+ lines = [line for line in shallow_path.read_text(encoding="utf-8").splitlines() if line]
+ if not lines:
+ return 0
+ keep = set(lines) & {
+ *(_git_stdout_lines(repo_root, ["rev-parse", "HEAD"]) or []),
+ *(_git_stdout_lines(repo_root, ["rev-parse", "--verify", "--quiet", "FETCH_HEAD"]) or []),
+ *_git_stdout_lines(repo_root, ["for-each-ref", "--format=%(objectname)"]),
+ }
+ if len(keep) == len(lines):
+ return 0
+ original = shallow_path.read_text(encoding="utf-8")
+ tmp_path = shallow_path.with_name(shallow_path.name + ".hermes-prune")
+ tmp_path.write_text("\n".join(sorted(keep)) + "\n", encoding="utf-8")
+ os.replace(tmp_path, shallow_path)
# Fail-safe: if any reachable walk now crosses a boundary we wrongly removed,
# put the grafts back — a growing file beats a broken repo.
still_walks = _git_stdout_lines(repo_root, ["rev-list", "--count", "HEAD"]) and \
- _git_stdout_lines(repo_root, ["rev-list", "--count", "--all"])
+ _git_stdout_lines(repo_root, ["rev-list", "--count", "--all"]) and \
+ _git_stdout_lines(repo_root, ["rev-list", "--count", "--all", "--reflog"])
if not still_walks:
- shallow_path.write_text(original, encoding="utf-8")
+ with _ShallowLock(shallow_path):
+ shallow_path.write_text(original, encoding="utf-8")
logger.debug("shallow prune self-check failed; grafts restored")
return 0
logger.info("Pruned %d stale shallow graft(s) in %s", len(lines) - len(keep), repo_root)
diff --git a/tests/hermes_cli/test_shallow_boundary_repair.py b/tests/hermes_cli/test_shallow_boundary_repair.py
index 7bd1307c03c..465b894b6d8 100644
--- a/tests/hermes_cli/test_shallow_boundary_repair.py
+++ b/tests/hermes_cli/test_shallow_boundary_repair.py
@@ -1,3 +1,15 @@
+"""Repair of shallow boundaries dropped by the stale-graft prune (#108286).
+
+``prune_stale_shallow_grafts()`` dropped ``.git/shallow`` grafts that reflog-only
+commits still needed, leaving shallow installer checkouts with commits whose parent
+objects were never downloaded — ``git gc`` / ``fsck`` / ``fetch`` all fail, and the
+corruption cannot self-heal because reflog expiry happens during ``git gc``, which is
+exactly what broke. ``repair_broken_shallow_boundaries()`` re-appends the missing
+boundaries; the updater calls it before the prune at both call sites.
+"""
+
+from __future__ import annotations
+
import subprocess
from pathlib import Path
@@ -5,7 +17,10 @@ import hermes_cli.gitlock as gitlock
def git(repo, *args, check=True):
- return subprocess.run(["git", *args], cwd=repo, capture_output=True, text=True, check=check)
+ return subprocess.run(
+ ["git", *args], cwd=repo, capture_output=True, text=True,
+ encoding="utf-8", errors="replace", check=check,
+ )
def fixture(tmp_path):
@@ -14,7 +29,7 @@ def fixture(tmp_path):
git(origin, "config", "user.email", "[email protected]"); git(origin, "config", "user.name", "t")
git(origin, "commit", "--allow-empty", "-qm", "c0")
clone = tmp_path / "clone"
- subprocess.run(["git", "clone", "-q", "--depth", "1", f"file://{origin}", str(clone)], check=True)
+ subprocess.run(["git", "clone", "-q", "--depth", "1", origin.as_uri(), str(clone)], check=True)
git(origin, "commit", "--allow-empty", "-qm", "c1")
git(origin, "commit", "--allow-empty", "-qm", "c2")
git(clone, "fetch", "-q", "--depth", "1", "origin", "main")
@@ -25,27 +40,47 @@ def fixture(tmp_path):
def corrupt_fixture(clone):
path = clone / ".git" / "shallow"
- lines = path.read_text().splitlines()
+ lines = path.read_text(encoding="utf-8").splitlines()
for removed in lines:
- path.write_text("\n".join(x for x in lines if x != removed) + "\n")
+ path.write_text("\n".join(x for x in lines if x != removed) + "\n", encoding="utf-8")
fsck = git(clone, "fsck", "--connectivity-only", check=False)
if "broken link" in (fsck.stdout + fsck.stderr) or "missing commit" in (fsck.stdout + fsck.stderr):
return removed
raise AssertionError("fixture did not create a broken shallow boundary")
+def _walks(clone):
+ return git(clone, "rev-list", "--count", "--all", "--reflog", check=False).returncode == 0
+
+
def test_repair_restores_boundary_for_reflog_only_commit_with_unfetched_parent(tmp_path):
clone = fixture(tmp_path)
corrupt_fixture(clone)
- assert git(clone, "rev-list", "--count", "--all", "--reflog", check=False).returncode != 0
+ assert not _walks(clone)
assert "broken link" in git(clone, "fsck", "--connectivity-only", check=False).stdout
assert gitlock.repair_broken_shallow_boundaries(clone) >= 1
- assert git(clone, "rev-list", "--count", "--all", "--reflog").returncode == 0
+ assert _walks(clone)
fsck = git(clone, "fsck", "--connectivity-only")
assert "broken link" not in (fsck.stdout + fsck.stderr)
assert git(clone, "gc", "-q").returncode == 0
+def test_repair_then_prune_leaves_repo_walkable(tmp_path):
+ """The production updater sequence: repair runs, then the prune immediately after.
+
+ Without the prune's ``--reflog`` fail-safe walk, the prune dropped the boundary
+ repair had just restored, re-breaking the repo on every ``hermes update`` run.
+ """
+ clone = fixture(tmp_path)
+ corrupt_fixture(clone)
+ assert gitlock.repair_broken_shallow_boundaries(clone) >= 1
+ assert _walks(clone)
+ gitlock.prune_stale_shallow_grafts(clone)
+ assert _walks(clone)
+ fsck = git(clone, "fsck", "--connectivity-only", check=False)
+ assert "broken link" not in (fsck.stdout + fsck.stderr)
+
+
def test_repair_is_noop_on_healthy_shallow_checkout(tmp_path):
clone = fixture(tmp_path); path = clone / ".git" / "shallow"; before = path.read_bytes()
assert gitlock.repair_broken_shallow_boundaries(clone) == 0
@@ -74,39 +109,38 @@ def test_repair_is_idempotent(tmp_path):
assert gitlock.repair_broken_shallow_boundaries(clone) == 0
-def test_repair_uses_a_bounded_number_of_git_subprocesses(tmp_path, monkeypatch):
- counts = []
- real_run = gitlock.subprocess.run
-
- for count in (4, 40):
- root = tmp_path / str(count)
- root.mkdir()
- clone = fixture(root)
- for index in range(count):
- git(clone, "commit", "--allow-empty", "-qm", f"extra-{index}")
- corrupt_fixture(clone)
- calls = 0
-
- def counting_run(*args, **kwargs):
- nonlocal calls
- calls += 1
- return real_run(*args, **kwargs)
-
- monkeypatch.setattr(gitlock.subprocess, "run", counting_run)
- assert gitlock.repair_broken_shallow_boundaries(clone) >= 1
- assert git(clone, "rev-list", "--count", "--all", "--reflog").returncode == 0
- counts.append(calls)
- monkeypatch.setattr(gitlock.subprocess, "run", real_run)
-
- assert all(count < 15 for count in counts)
- assert abs(counts[0] - counts[1]) <= 2
-
-
-def test_repair_handles_commit_messages_containing_blank_lines(tmp_path):
+def test_repair_ignores_parent_lines_inside_commit_messages(tmp_path):
+ """A ``parent <sha>`` line in a commit *message body* is prose, not an edge:
+ the parent parser must read only the commit header, so a healthy commit whose
+ message mentions ``parent <sha>`` is never shallow-marked."""
+ origin = tmp_path / "origin"; origin.mkdir()
+ git(origin, "init", "-q", "-b", "main")
+ git(origin, "config", "user.email", "[email protected]"); git(origin, "config", "user.name", "t")
+ git(origin, "commit", "--allow-empty", "-qm", "c0")
+ git(origin, "commit", "--allow-empty", "-qm", "c1")
+ clone = tmp_path / "clone"
+ subprocess.run(["git", "clone", "-q", "--depth", "2", origin.as_uri(), str(clone)], check=True)
+ git(clone, "config", "user.email", "[email protected]"); git(clone, "config", "user.name", "t")
+ git(clone, "commit", "--allow-empty", "-m", "subject\n\nbody line\n\nparent ffffffffffffffffffffffffffffffffffffffff")
+ head = git(clone, "rev-parse", "HEAD").stdout.strip()
+ # HEAD's real parent exists locally; the message-body "parent" line names an
+ # absent object and must NOT register as a missing edge (header-only parsing).
+ assert gitlock._batch_missing_parents(clone, [head]) == set()
+ assert git(clone, "rev-list", "--count", "HEAD").stdout.strip() == "3"
+
+
+def test_repair_does_not_mask_unrelated_object_loss(tmp_path):
+ """Missing objects that are NOT reflog-only boundary commits must stay visible
+ to fsck — repair must not relabel arbitrary object loss as shallow history."""
clone = fixture(tmp_path)
- git(clone, "commit", "--allow-empty", "-m", "subject\n\nbody line\n\ndeadbeef commit 123")
- corrupt_fixture(clone)
- assert gitlock.repair_broken_shallow_boundaries(clone) >= 1
- assert git(clone, "rev-list", "--count", "--all", "--reflog").returncode == 0
- fsck = git(clone, "fsck", "--connectivity-only")
- assert "broken link" not in (fsck.stdout + fsck.stderr)
+ git(clone, "config", "user.email", "[email protected]"); git(clone, "config", "user.name", "t")
+ git(clone, "commit", "--allow-empty", "-qm", "local1")
+ git(clone, "commit", "--allow-empty", "-qm", "local2")
+ # Drop HEAD's parent object from the object store.
+ victim = git(clone, "rev-parse", "HEAD~1").stdout.strip()
+ loose = clone / ".git" / "objects" / victim[:2] / victim[2:]
+ assert loose.is_file(), "expected a loose object to delete"
+ loose.unlink()
+ assert gitlock.repair_broken_shallow_boundaries(clone) == 0
+ fsck = git(clone, "fsck", "--connectivity-only", check=False)
+ assert "missing" in (fsck.stdout + fsck.stderr) or "broken link" in (fsck.stdout + fsck.stderr)