Skip to content

The agent that grows with you

fix(update): give the lazy-fetch pack fold room to finish and say when it runs out

teknium14e3fcd5cd7e4
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.py
index fb8b2446edd..fb0874cb4ad 100644
--- a/hermes_cli/gitlock.py
+++ b/hermes_cli/gitlock.py
@@ -21,8 +21,11 @@ from hermes_cli._subprocess_compat import (
logger = logging.getLogger(__name__)
-# Folding hundreds of tiny packs takes well under a second; this only bounds a pathological repack.
-LAZY_FETCH_GC_TIMEOUT_SECONDS = 120
+# Folding ~100 small packs takes seconds, but the checkouts this exists for (thousands of packs,
+# tens of GiB) need a full repack. A killed fold restarts from scratch on every update and never
+# converges, so the bound is generous and a timeout is reported, not swallowed.
+LAZY_FETCH_GC_TIMEOUT_SECONDS = 20 * 60
+_GC_AUTO_PACK_LIMIT_DEFAULT = 50
# Files younger than this are presumed live (a fetch may be in flight) and are never removed. Lock
# files live for seconds and a healthy fetch completes in minutes; 10 minutes is abandoned.
@@ -578,7 +581,13 @@ def fetch_with_partial_clone_recovery(runner: Callable[..., subprocess.Completed
return runner(git_cmd, fetch_args)
-def consolidate_lazy_fetch_packs(repo_root: Path) -> int:
+def _gc_auto_pack_limit(repo_root: Path) -> int:
+ lines = _git_stdout_lines(repo_root, ["config", "--int", "--get", "gc.autoPackLimit"])
+ return int(lines[0]) if lines else _GC_AUTO_PACK_LIMIT_DEFAULT
+
+
+def consolidate_lazy_fetch_packs(repo_root: Path, *,
+ on_fold_start: Optional[Callable[[int], None]] = None) -> Optional[int]:
"""Fold a partial clone's lazy-fetch packfiles back into one; returns how many packs went away.
Every on-demand fetch a promisor remote serves writes its own small packfile, and nothing in
@@ -590,19 +599,27 @@ def consolidate_lazy_fetch_packs(repo_root: Path) -> int:
write a commit-graph (see ``_TREE0_MAINTENANCE_OFF``): over a Bloom-carrying graph that is a
lazy fetch per unseen commit, so the same call that folds 100 packs would leave 30 new ones.
Runs under ``bounded_probe_run`` because ``subprocess.run(timeout=)`` kills only ``git gc``
- and leaves its ``pack-objects`` child running. Best-effort like every helper here: never
- raises, returns 0 for a non-partial checkout or when nothing folded.
+ and leaves its ``pack-objects`` child running. ``on_fold_start(pack_count)`` fires just before
+ a fold gc will actually do (pack count past the limit), so the caller can say why the update
+ went quiet. Best-effort like every helper here: never raises, returns 0 for a non-partial
+ checkout or when nothing folded, and ``None`` when the fold hit its time limit.
"""
try:
if _partial_clone_filter(repo_root, creationflags=windows_hide_flags()) is None:
return 0 # only a promisor remote's on-demand fetches write these packs
disable_tree0_auto_maintenance(repo_root)
before = len(list(_pack_dir(repo_root).glob("pack-*.pack")))
- bounded_probe_run(
+ limit = _gc_auto_pack_limit(repo_root)
+ if on_fold_start is not None and 0 < limit < before:
+ on_fold_start(before)
+ if bounded_probe_run(
["git", "-c", "gc.autoDetach=false", "-c", "gc.writeCommitGraph=false", "gc", "--auto"],
timeout=LAZY_FETCH_GC_TIMEOUT_SECONDS, cwd=str(repo_root),
env={**noninteractive_git_env(), **NO_LAZY_FETCH_ENV},
- )
+ ) is None:
+ logger.warning("Folding %d lazy-fetch pack(s) in %s timed out after %ds",
+ before, repo_root, LAZY_FETCH_GC_TIMEOUT_SECONDS)
+ return None
folded = before - len(list(_pack_dir(repo_root).glob("pack-*.pack")))
if folded > 0:
logger.info("Folded %d lazy-fetch pack(s) in %s", folded, repo_root)
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 8cae4d42163..031571ae629 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -1543,10 +1543,7 @@ def _cmd_update_impl(args, gateway_mode: bool):
print(" (removed %d aborted-fetch pack temp file(s))" % len(swept))
# A partial clone's on-demand fetches strand one small packfile each and never
# consolidate on their own (#129712); fold them before this run's fetch adds more.
- from hermes_cli.gitlock import consolidate_lazy_fetch_packs
- folded = consolidate_lazy_fetch_packs(_m().PROJECT_ROOT)
- if folded:
- print(f" (folded {folded} lazy-fetch pack(s) into one)")
+ _check.fold_lazy_fetch_packs(_m().PROJECT_ROOT)
# Shallow installer checkouts collect one `.git/shallow` graft per past depth-1 fetch
# (#105951); stale grafts break merge-base and push this run into the divergence path.
from hermes_cli.gitlock import repair_broken_shallow_boundaries, prune_stale_shallow_grafts
diff --git a/hermes_cli/update_cmd_check.py b/hermes_cli/update_cmd_check.py
index f84c074dace..6f6fd910002 100644
--- a/hermes_cli/update_cmd_check.py
+++ b/hermes_cli/update_cmd_check.py
@@ -46,10 +46,20 @@ def clear_git_debris(root: Path) -> None:
swept = clear_stale_tmp_packs(root)
if swept:
print(f" (removed {len(swept)} aborted-fetch pack temp file(s))")
- from hermes_cli.gitlock import consolidate_lazy_fetch_packs
+ fold_lazy_fetch_packs(root)
- folded = consolidate_lazy_fetch_packs(root)
- if folded:
+
+def fold_lazy_fetch_packs(root: Path) -> None:
+ """Fold a partial clone's lazy-fetch packs (#129712), announcing a slow fold and a timed-out one."""
+ from hermes_cli.gitlock import LAZY_FETCH_GC_TIMEOUT_SECONDS, consolidate_lazy_fetch_packs
+
+ folded = consolidate_lazy_fetch_packs(root, on_fold_start=lambda count: print(
+ f" Folding {count} lazy-fetch packs into one (one-time; can take several minutes)...", flush=True))
+ if folded is None:
+ print(f" ⚠ Folding lazy-fetch packs did not finish within {LAZY_FETCH_GC_TIMEOUT_SECONDS // 60} min."
+ " With Hermes closed, run:")
+ print(f' git -C "{root}" -c gc.writeCommitGraph=false gc --auto')
+ elif folded:
print(f" (folded {folded} lazy-fetch pack(s) into one)")
diff --git a/tests/hermes_cli/test_gitlock.py b/tests/hermes_cli/test_gitlock.py
index 3090e314268..39fb58da5ce 100644
--- a/tests/hermes_cli/test_gitlock.py
+++ b/tests/hermes_cli/test_gitlock.py
@@ -238,6 +238,8 @@ def partial_clone(tmp_path: Path) -> Path:
"""Blobless clone that has lazy-fetched one pack per object it was asked for."""
_seed, _up, clone = _blobless_clone(tmp_path, 3)
_run_git("config", "gc.autoPackLimit", "2", cwd=clone) # the wild default (50) cannot be crossed here
+ # Else each lazy fetch's detached auto-maintenance races the test and folds the packs itself.
+ _run_git("config", "maintenance.auto", "false", cwd=clone)
for name in ("d0/f.txt", "d1/f.txt", "d2/f.txt"):
_run_git("cat-file", "-p", _run_git("rev-parse", f"HEAD:{name}", cwd=clone), cwd=clone)
return clone
@@ -290,10 +292,18 @@ def test_non_partial_checkout_is_left_alone(repo: Path) -> None:
assert keys == "", "a full clone keeps git's stock maintenance"
-def test_update_check_debris_cleanup_is_where_the_packs_get_folded(partial_clone: Path) -> None:
- """`hermes update --check` runs on its own (CLI banner prefetch), so its cleanup is the fold point."""
+def test_update_debris_cleanup_folds_and_reports_a_fold_that_runs_out_of_time(
+ partial_clone: Path, monkeypatch: pytest.MonkeyPatch, capfd: pytest.CaptureFixture[str]) -> None:
+ """A killed fold restarts from scratch every update, so it must say so instead of returning 0."""
+ from hermes_cli import gitlock
from hermes_cli.update_cmd_check import clear_git_debris
before = len(_packs(partial_clone))
+ with monkeypatch.context() as patched:
+ patched.setattr(gitlock, "LAZY_FETCH_GC_TIMEOUT_SECONDS", 0) # the real gc, killed by the real bound
+ clear_git_debris(partial_clone)
+ out = capfd.readouterr().out
+ assert f"Folding {before} lazy-fetch packs" in out and "gc.writeCommitGraph=false gc --auto" in out
+
clear_git_debris(partial_clone)
assert len(_packs(partial_clone)) < before
diff --git a/website/docs/getting-started/updating.md b/website/docs/getting-started/updating.md
index 2daffb0ba83..68fb306940e 100644
--- a/website/docs/getting-started/updating.md
+++ b/website/docs/getting-started/updating.md
@@ -215,7 +215,10 @@ fold them back together with `git gc --auto` (git's own `gc.autoPackLimit`, 50 b
healthy checkout pays a no-op. They also set `maintenance.auto`, `gc.writeCommitGraph` and
`fetch.writeCommitGraph` to `false` in that checkout, because a commit-graph write over commits the
graph has not seen yet downloads every one of their trees. Leave those settings alone, and leave
-`gc.auto` at its default: `gc.auto=0` stops the fold. To fold by hand (with Hermes closed):
+`gc.auto` at its default: `gc.auto=0` stops the fold. The first fold on a checkout that has
+piled up thousands of packs is a full repack and can take several minutes; the update says so
+before it starts, and if the fold runs past 20 minutes it stops and prints the command below.
+To fold by hand (with Hermes closed):
```bash
git -C "$repo" -c gc.writeCommitGraph=false gc --auto