Skip to content

The agent that grows with you

fix(git): graph refresh sweeps only object-store debris and lets git clean up

Tekniuma80b413cd633
diff --git a/hermes_cli/_subprocess_compat.py b/hermes_cli/_subprocess_compat.py
index 8ec065c6380..cb65bb85c8c 100644
--- a/hermes_cli/_subprocess_compat.py
+++ b/hermes_cli/_subprocess_compat.py
@@ -735,9 +735,12 @@ def pid_is_hermes(pid: int, *, expected_start_time: int | None = None) -> bool:
return False
-def kill_process_tree(proc: subprocess.Popen) -> None:
+def kill_process_tree(proc: subprocess.Popen, *, grace: float = 0.0) -> None:
"""Best-effort terminate *proc* and its descendants on both platforms; never raises.
+ ``grace`` > 0 (POSIX): SIGTERM the tree first and SIGKILL only if *proc* outlives ``grace``
+ seconds, so a git can delete its lock and tmp files on the way out (Windows always forces).
+
``proc.kill()`` alone only terminates the direct child. This is cleanup on an already-failing
path whose contract is to fail open, so every failure (access denied, already reaped) is
swallowed rather than escaping the caller's ``except``.
@@ -756,6 +759,15 @@ def kill_process_tree(proc: subprocess.Popen) -> None:
try:
from agent.deadline import kill_process_tree as _deadline_kill_tree
+ if grace > 0 and not IS_WINDOWS:
+ import signal as _signal
+
+ _deadline_kill_tree(proc.pid, sig=_signal.SIGTERM)
+ try:
+ proc.wait(timeout=grace)
+ return
+ except subprocess.TimeoutExpired:
+ pass
_deadline_kill_tree(proc.pid)
except Exception:
_legacy_kill_process_tree(proc)
diff --git a/hermes_cli/git_commit_graph.py b/hermes_cli/git_commit_graph.py
new file mode 100644
index 00000000000..850124c1267
--- /dev/null
+++ b/hermes_cli/git_commit_graph.py
@@ -0,0 +1,208 @@
+"""Keep a checkout's commit-graph fresh: the ``hermes -w`` pass and every ``hermes update``.
+
+Generation numbers in the commit-graph keep ``git tag --merged HEAD`` (the version lookup) and
+ancestry checks off a full history walk: on hermes-agent a graph two months behind cost 0.33 s
+per lookup, a fresh one 0.04 s. Partial clones have git's own graph writers switched off
+(gitlock._TREE0_MAINTENANCE_OFF, #127711), so nothing else refreshes it there. Without Bloom data
+the write reads commits only, never trees, so it cannot start that lazy-fetch loop, and it costs
+~3 s instead of ~27 s when changed paths would be recomputed.
+
+Also home of the per-clone maintenance slot and the bounded git runner the ``-w`` repack shares,
+so the update path imports this module and not all of ``worktree_ops``.
+"""
+
+from __future__ import annotations
+
+import atexit
+import logging
+import os
+import subprocess
+import sys
+import tempfile
+import time
+from pathlib import Path
+from typing import Optional, Sequence
+
+from hermes_cli._subprocess_compat import (
+ IS_WINDOWS,
+ NO_LAZY_FETCH_ENV,
+ bounded_probe_run,
+ kill_process_tree,
+ noninteractive_git_env,
+ windows_detach_flags_without_breakaway,
+ windows_detach_popen_kwargs,
+)
+
+logger = logging.getLogger(__name__)
+
+_WRITE = ("commit-graph", "write", "--reachable", "--split", "--no-changed-paths", "--no-progress")
+_MAX_AGE = 24 * 3600
+_TIMEOUT = 600
+_MIN_INTERVAL = 6 * 3600
+_LOCK = "hermes-commit-graph.lock"
+# A write with no new commits leaves git's files untouched; this stamp records that it succeeded,
+# so an idle repo is not rewritten on every slot.
+_FRESH_STAMP = "hermes-commit-graph.fresh"
+# git deletes its lock and tmp files on SIGTERM; a SIGKILLed writer strands commit-graph-chain.lock
+# and tmp_graph_*, and that lock fails every later write until the stale-lock sweep removes it.
+_KILL_GRACE = 5.0
+_CHILD = ("import sys; sys.path.insert(0, sys.argv[1]); "
+ "from hermes_cli.git_commit_graph import refresh_in_child; refresh_in_child(*sys.argv[2:])")
+
+
+def claim_maintenance_slot(git_dir: Path, lock_name: str, interval: float) -> bool:
+ """Exactly one process per clone gets to run a maintenance job per ``interval``.
+
+ The lock file's mtime is the stamp: younger than the interval means another launch is
+ running the job (or just tried and timed out) — skip. A stale lock is taken over by ``replace``,
+ which only one of N racing processes can win; the O_EXCL create then serializes against a
+ process that found no lock at all.
+ """
+ lock = git_dir / lock_name
+ try:
+ st = lock.stat()
+ except FileNotFoundError:
+ pass
+ else:
+ if time.time() - st.st_mtime < interval:
+ return False
+ try:
+ lock.replace(lock.with_suffix(".stale"))
+ except OSError:
+ return False
+ try:
+ fd = os.open(lock, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
+ except FileExistsError:
+ return False
+ with os.fdopen(fd, "w", encoding="utf-8") as fh:
+ fh.write(f"{os.getpid()}\n")
+ lock.with_suffix(".stale").unlink(missing_ok=True)
+ return True
+
+
+def run_bounded_git(repo_root: str, args: Sequence[str], timeout: float, env: Optional[dict] = None) -> Optional[int]:
+ """Run a niced ``git *args`` whose whole process tree dies with the timeout or with us.
+
+ Returns the exit code, or None when the timeout killed it; a failure is logged with git's last
+ stderr line. ``repack`` forks ``pack-objects``; ``subprocess.run(timeout=)`` killed only the
+ parent and left the grandchild packing for days, and a daemon thread's child outlived the CLI the
+ same way. A new session/process group + ``atexit`` reaps both cases, SIGTERM first so git can
+ delete its own lock and tmp files.
+ """
+ cmd = ["git", *args]
+ if os.name == "posix":
+ cmd = ["nice", "-n", "19", *cmd]
+ group_kw: dict = {"process_group": 0}
+ else:
+ group_kw = {"creationflags": getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)}
+ with tempfile.TemporaryFile() as err:
+ proc = subprocess.Popen(cmd, cwd=repo_root, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
+ stderr=err, env=env, **group_kw)
+
+ def _reap() -> None:
+ if proc.poll() is None:
+ kill_process_tree(proc, grace=_KILL_GRACE)
+
+ atexit.register(_reap)
+ try:
+ rc = proc.wait(timeout=timeout)
+ except subprocess.TimeoutExpired:
+ _reap()
+ logger.info("git %s exceeded %ds; killed", args[0], timeout)
+ return None
+ finally:
+ atexit.unregister(_reap)
+ if rc:
+ err.seek(0)
+ lines = err.read()[-4096:].decode("utf-8", "replace").strip().splitlines()
+ # git's "File exists" ends on advice; the fatal/error line names the cause.
+ cause = next((ln for ln in reversed(lines) if ln.startswith(("fatal:", "error:"))), lines[-1] if lines else "no output")
+ logger.warning("git %s in %s failed (exit %d): %s", args[0], repo_root, rc, cause)
+ return rc
+
+
+def commit_graph_stale(git_dir: Path) -> bool:
+ """No commit-graph, or none written or confirmed current for a day."""
+ info = git_dir / "objects" / "info"
+ graphs = [p.stat().st_mtime for p in (info / "commit-graph", info / "commit-graphs" / "commit-graph-chain")
+ if p.is_file()]
+ if not graphs:
+ return True
+ stamp = git_dir / _FRESH_STAMP
+ newest = max(graphs + ([stamp.stat().st_mtime] if stamp.is_file() else []))
+ return time.time() - newest > _MAX_AGE
+
+
+def claim_commit_graph_refresh(git_dir: Path) -> bool:
+ return commit_graph_stale(git_dir) and claim_maintenance_slot(git_dir, _LOCK, _MIN_INTERVAL)
+
+
+def _commit_graph_disabled(repo_root: str) -> bool:
+ """``core.commitGraph=false``: the owner turned graphs off in this repo; never write one there."""
+ probe = bounded_probe_run(["git", "config", "--type=bool", "--get", "core.commitGraph"],
+ timeout=10, cwd=repo_root, env=noninteractive_git_env())
+ return probe is not None and probe.stdout.strip() == "false"
+
+
+def refresh_commit_graph(repo_root: str) -> None:
+ """Clear what a killed writer stranded, then write the graph (bounded, niced, never lazy-fetching)."""
+ from hermes_cli.gitlock import OBJECT_STORE_LOCK_NAMES, clear_stale_git_locks, clear_stale_tmp_packs
+
+ if _commit_graph_disabled(repo_root):
+ logger.info("core.commitGraph=false in %s; not writing a commit-graph", repo_root)
+ return
+ root = Path(repo_root)
+ # Object-store debris only: ``hermes -w`` runs this in whatever repo it was launched from, where
+ # index.lock / HEAD.lock belong to the user's own git (and pgrep can be blind to it).
+ clear_stale_git_locks(root, names=OBJECT_STORE_LOCK_NAMES)
+ clear_stale_tmp_packs(root)
+ logger.info("git commit-graph missing or stale — writing it in background")
+ if run_bounded_git(repo_root, _WRITE, _TIMEOUT, env={**noninteractive_git_env(), **NO_LAZY_FETCH_ENV}) == 0:
+ (root / ".git" / _FRESH_STAMP).touch()
+
+
+def refresh_in_child(repo_root: str, log_file: str) -> None:
+ """Body of the detached child: a failure is appended to the home's agent.log; never raises.
+
+ A plain handler, not ``setup_logging``: that reads config.yaml and seeds the home's layout,
+ side effects a git maintenance child has no business having in a profile mid-update.
+ ``delay`` opens the log only when there is a failure to record.
+ """
+ handler = logging.FileHandler(log_file, encoding="utf-8", delay=True)
+ handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s"))
+ logging.getLogger().addHandler(handler)
+ try:
+ refresh_commit_graph(repo_root)
+ except Exception:
+ logger.warning("Background commit-graph refresh of %s failed", repo_root, exc_info=True)
+
+
+def spawn_commit_graph_refresh(repo_root: Path) -> bool:
+ """Start the bounded commit-graph refresh in a detached child when it is due; never waits.
+
+ ``hermes update`` is the one regular git pass an install checkout gets (``hermes -w`` is
+ rarely launched from it), so without this its version lookups never get a graph. Detached
+ because the write takes seconds and must neither delay the update nor die with it. A spawn that
+ fails gives the slot back, so the next update retries instead of waiting out the interval.
+ """
+ from hermes_constants import get_hermes_home
+
+ git_dir = Path(repo_root) / ".git"
+ if not (git_dir / "objects").is_dir() or not claim_commit_graph_refresh(git_dir):
+ return False
+ code_root = Path(__file__).resolve().parents[1]
+ argv = [sys.executable, "-c", _CHILD, str(code_root), str(repo_root), str(get_hermes_home() / "logs" / "agent.log")]
+ kwargs: dict = dict(cwd=str(repo_root), stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL, close_fds=True)
+ try:
+ try:
+ subprocess.Popen(argv, **kwargs, **windows_detach_popen_kwargs())
+ except OSError:
+ if not IS_WINDOWS:
+ raise
+ # A job object without BREAKAWAY_OK refuses CREATE_BREAKAWAY_FROM_JOB; retry without it.
+ subprocess.Popen(argv, **kwargs, creationflags=windows_detach_flags_without_breakaway())
+ except OSError:
+ (git_dir / _LOCK).unlink(missing_ok=True)
+ raise
+ return True
diff --git a/hermes_cli/gitlock.py b/hermes_cli/gitlock.py
index 8f868c102a8..cb126a16771 100644
--- a/hermes_cli/gitlock.py
+++ b/hermes_cli/gitlock.py
@@ -47,6 +47,8 @@ OBJECT_STORE_LOCK_NAMES = (
# success; anything left with these names after a fetch died is garbage by definition.
# ``.tmp-<pid>-pack*`` is the same thing from ``pack-objects`` (repack/gc) killed mid-write.
_TMP_PACK_PREFIXES = ("tmp_pack_", "tmp_idx_", "tmp_rev_", "tmp_mtimes_", ".tmp-")
+# A split commit-graph write killed before its rename strands this in objects/info/commit-graphs.
+_TMP_GRAPH_PREFIX = "tmp_graph_"
def _git_proc_running() -> bool:
@@ -101,15 +103,17 @@ def _sweep_stale(directory: Path, candidates: Callable[[], Iterable[Path]], *, m
return removed
-def clear_stale_git_locks(repo_root: Path, *, min_age_seconds: Optional[int] = None) -> list[str]:
+def clear_stale_git_locks(repo_root: Path, *, min_age_seconds: Optional[int] = None,
+ names: Iterable[str] = (*LOCK_NAMES, *OBJECT_STORE_LOCK_NAMES)) -> list[str]:
"""Remove abandoned ``.git`` lock files under ``repo_root``; returns the removed paths.
Removes only when older than the age floor AND no git process is running. Never raises: a lock we cannot
stat/unlink is skipped (it may have been re-created between the age check and the unlink; skipping is safe).
+ ``names`` narrows the sweep (relative to ``.git``) for repos Hermes does not own.
"""
git_dir = Path(repo_root) / ".git"
return _sweep_stale(
- git_dir, lambda: [git_dir / name for name in (*LOCK_NAMES, *OBJECT_STORE_LOCK_NAMES)],
+ git_dir, lambda: [git_dir / name for name in names],
min_age_seconds=min_age_seconds, default_age=STALE_LOCK_MIN_AGE_SECONDS,
skip_msg="git process running; skipping stale-lock sweep",
log_removed=lambda p, _size: logger.info("Removed stale git lock %s", p),
@@ -343,23 +347,28 @@ def _migrate_earlier_maintenance_keys(repo_root: Path) -> None:
def clear_stale_tmp_packs(repo_root: Path, *, min_age_seconds: Optional[int] = None) -> list[str]:
- """Remove aborted-transfer temp pack files; same contract as clear_stale_git_locks.
+ """Remove aborted-transfer temp pack files and killed commit-graph writes' ``tmp_graph_*``;
+ same contract as clear_stale_git_locks.
Resolves ``.git/objects/pack`` for a checkout and ``objects/pack`` for a bare repo such as
the checkpoint store — a ``git gc`` killed by a timeout strands the same debris there."""
pack_dir = _pack_dir(repo_root)
+ graph_dir = pack_dir.parent / "info" / "commit-graphs"
def _candidates():
- try:
- return [e for e in pack_dir.iterdir() if e.name.startswith(_TMP_PACK_PREFIXES)]
- except OSError:
- return []
+ found = []
+ for directory, prefixes in ((pack_dir, _TMP_PACK_PREFIXES), (graph_dir, _TMP_GRAPH_PREFIX)):
+ try:
+ found += [e for e in directory.iterdir() if e.name.startswith(prefixes)]
+ except OSError:
+ pass
+ return found
return _sweep_stale(
pack_dir, _candidates,
min_age_seconds=min_age_seconds, default_age=STALE_TMP_PACK_MIN_AGE_SECONDS,
skip_msg="git process running; skipping tmp-pack sweep",
- log_removed=lambda p, size: logger.info("Removed aborted-fetch pack debris %s (%d bytes)", p, size),
+ log_removed=lambda p, size: logger.info("Removed aborted-write git debris %s (%d bytes)", p, size),
)
diff --git a/hermes_cli/update_cmd_maint.py b/hermes_cli/update_cmd_maint.py
index 73c61932eab..8d4830ea37a 100644
--- a/hermes_cli/update_cmd_maint.py
+++ b/hermes_cli/update_cmd_maint.py
@@ -1030,7 +1030,7 @@ def _run_post_update_maintenance(
print(f" ⚠ Could not refresh release history ({detail[0]}); the version label may be stale or unknown until the next update")
# Keeps every later version lookup off a full history walk; detached, so it never delays this run.
with _best_effort('Commit-graph refresh failed to start: %s'):
- from hermes_cli.worktree_ops import spawn_commit_graph_refresh
+ from hermes_cli.git_commit_graph import spawn_commit_graph_refresh
spawn_commit_graph_refresh(Path(_m().PROJECT_ROOT))
# Seed the model-catalog cache from the checkout instead of a bot-gated, flaky fetch.
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index 52ca623a2bf..ede73e02676 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -4,7 +4,6 @@ Every git call goes through ``_git``/``_git_out``/``_git_quiet`` (UTF-8 text, ca
bounded timeout). Classification helpers fail SAFE toward "preserve". ``cli`` re-exports
these names; ``_cprint`` is imported lazily from ``cli`` to avoid a cycle.
"""
-import atexit
import concurrent.futures
import json
import logging
@@ -19,7 +18,13 @@ import uuid
from pathlib import Path
from typing import Dict, Optional
-from hermes_cli._subprocess_compat import NO_LAZY_FETCH_ENV, kill_process_tree, noninteractive_git_env
+from hermes_cli._subprocess_compat import noninteractive_git_env
+from hermes_cli.git_commit_graph import (
+ claim_commit_graph_refresh,
+ claim_maintenance_slot,
+ refresh_commit_graph,
+ run_bounded_git,
+)
from hermes_constants import get_hermes_home
from utils import atomic_json_write
@@ -123,124 +128,16 @@ _REPACK_TIMEOUT = 1800
# concurrent multi-GB repacks (each too slow under the others to ever finish inside the timeout).
_REPACK_MIN_INTERVAL = 6 * 3600
_REPACK_LOCK = "hermes-repack.lock"
-# Generation numbers in the commit-graph keep ``git tag --merged HEAD`` (the version lookup) and
-# ancestry checks off a full history walk: on hermes-agent a graph two months behind cost 0.33 s
-# per lookup, a fresh one 0.04 s. Partial clones have git's own graph writers switched off
-# (gitlock._TREE0_MAINTENANCE_OFF, #127711), so nothing else refreshes it there. Without Bloom data
-# the write reads commits only, never trees, so it cannot start that lazy-fetch loop, and it costs
-# ~3 s instead of ~27 s when changed paths would be recomputed.
-_COMMIT_GRAPH_WRITE = ("commit-graph", "write", "--reachable", "--split", "--no-changed-paths", "--no-progress")
-_COMMIT_GRAPH_MAX_AGE = 24 * 3600
-_COMMIT_GRAPH_TIMEOUT = 600
-_COMMIT_GRAPH_LOCK = "hermes-commit-graph.lock"
-
-
-def _claim_repack_slot(git_dir: Path, lock_name: str = _REPACK_LOCK) -> bool:
- """Exactly one process per clone gets to run a maintenance job per ``_REPACK_MIN_INTERVAL``.
-
- The lock file's mtime is the stamp: younger than the interval means another launch is
- running the job (or just tried and timed out) — skip. A stale lock is taken over by ``replace``,
- which only one of N racing processes can win; the O_EXCL create then serializes against a
- process that found no lock at all.
- """
- lock = git_dir / lock_name
- try:
- st = lock.stat()
- except FileNotFoundError:
- pass
- else:
- if time.time() - st.st_mtime < _REPACK_MIN_INTERVAL:
- return False
- try:
- lock.replace(lock.with_suffix(".stale"))
- except OSError:
- return False
- try:
- fd = os.open(lock, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
- except FileExistsError:
- return False
- with os.fdopen(fd, "w", encoding="utf-8") as fh:
- fh.write(f"{os.getpid()}\n")
- lock.with_suffix(".stale").unlink(missing_ok=True)
- return True
-
-
-def _run_bounded_repack(repo_root: str) -> None:
- """Incremental geometric repack whose whole process tree dies with the timeout or with us."""
- _run_bounded_git(repo_root, ["repack", "-d", "--geometric=2", "--write-midx", "--quiet"], _REPACK_TIMEOUT)
-
-def _run_bounded_git(repo_root: str, args, timeout: float, env: Optional[dict] = None) -> None:
- """Run a niced ``git *args`` whose whole process tree dies with the timeout or with us.
- ``repack`` forks ``pack-objects``; ``subprocess.run(timeout=)`` killed only the parent and
- left the grandchild packing for days, and a daemon thread's child outlived the CLI the same
- way. A new session/process group + ``atexit`` reaps both cases.
- """
- cmd = ["git", *args]
- if os.name == "posix":
- cmd = ["nice", "-n", "19", *cmd]
- group_kw: dict = {"process_group": 0}
- else:
- group_kw = {"creationflags": getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)}
- proc = subprocess.Popen(cmd, cwd=repo_root, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
- stderr=subprocess.DEVNULL, env=env, **group_kw)
-
- def _reap() -> None:
- if proc.poll() is None:
- kill_process_tree(proc)
-
- atexit.register(_reap)
- try:
- proc.wait(timeout=timeout)
- except subprocess.TimeoutExpired:
- _reap()
- logger.info("git %s exceeded %ds; killed (next attempt in %dh)", args[0], timeout, _REPACK_MIN_INTERVAL // 3600)
+def _claim_repack_slot(git_dir: Path) -> bool:
+ """Exactly one process per clone gets to repack per ``_REPACK_MIN_INTERVAL``."""
+ return claim_maintenance_slot(git_dir, _REPACK_LOCK, _REPACK_MIN_INTERVAL)
-def _commit_graph_stale(git_dir: Path) -> bool:
- """No commit-graph, or none written for a day (a write with no new commits leaves it untouched)."""
- info = git_dir / "objects" / "info"
- stamps = [p.stat().st_mtime for p in (info / "commit-graph", info / "commit-graphs" / "commit-graph-chain")
- if p.is_file()]
- return not stamps or time.time() - max(stamps) > _COMMIT_GRAPH_MAX_AGE
-
-
-def _refresh_commit_graph(repo_root: str) -> None:
- from hermes_cli.gitlock import clear_stale_git_locks
-
- # A writer killed mid-write strands commit-graph-chain.lock and every later write fails on it.
- clear_stale_git_locks(Path(repo_root))
- logger.info("git commit-graph missing or stale — writing it in background")
- _run_bounded_git(repo_root, _COMMIT_GRAPH_WRITE, _COMMIT_GRAPH_TIMEOUT,
- env={**noninteractive_git_env(), **NO_LAZY_FETCH_ENV})
-
-
-def _claim_commit_graph_refresh(git_dir: Path) -> bool:
- return _commit_graph_stale(git_dir) and _claim_repack_slot(git_dir, _COMMIT_GRAPH_LOCK)
-
-
-_REFRESH_CHILD = ("import sys; sys.path.insert(0, sys.argv[1]); "
- "from hermes_cli.worktree_ops import _refresh_commit_graph; _refresh_commit_graph(sys.argv[1])")
-
-
-def spawn_commit_graph_refresh(repo_root: Path) -> bool:
- """Start the bounded commit-graph refresh in a detached child when it is due; never waits.
-
- ``hermes update`` is the one regular git pass an install checkout gets (``hermes -w`` is
- rarely launched from it), so without this its version lookups never get a graph. Detached
- because the write takes seconds and must neither delay the update nor die with it; the child
- is the same runner the ``-w`` pass uses (niced, process tree killed at the timeout).
- """
- from hermes_cli._subprocess_compat import windows_detach_popen_kwargs
-
- git_dir = Path(repo_root) / ".git"
- if not (git_dir / "objects").is_dir() or not _claim_commit_graph_refresh(git_dir):
- return False
- subprocess.Popen([sys.executable, "-c", _REFRESH_CHILD, str(repo_root)], cwd=str(repo_root),
- stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
- close_fds=True, **windows_detach_popen_kwargs())
- return True
+def _run_bounded_repack(repo_root: str) -> None:
+ """Incremental geometric repack whose whole process tree dies with the timeout or with us."""
+ run_bounded_git(repo_root, ["repack", "-d", "--geometric=2", "--write-midx", "--quiet"], _REPACK_TIMEOUT)
def _maintain_pack_health(repo_root: str) -> None:
@@ -263,8 +160,8 @@ def _maintain_pack_health(repo_root: str) -> None:
# Repacking can strand now-duplicated admin files; prune on the same pass.
_git(["worktree", "prune"], repo_root, timeout=60, check=False,
stdin=subprocess.DEVNULL, env=noninteractive_git_env())
- if _claim_commit_graph_refresh(git_dir):
- _refresh_commit_graph(repo_root)
+ if claim_commit_graph_refresh(git_dir):
+ refresh_commit_graph(repo_root)
except Exception as e:
logger.debug("pack maintenance skipped: %s", e)
diff --git a/scripts/ci/classify_changes.py b/scripts/ci/classify_changes.py
index 9f6eb8d831f..d15c2880b32 100644
--- a/scripts/ci/classify_changes.py
+++ b/scripts/ci/classify_changes.py
@@ -262,6 +262,7 @@ _UPDATE_PIPELINE = (
"hermes_cli/_launchers.py",
"hermes_cli/release_channels.py",
"hermes_cli/gitlock.py", # git self-heal + partial-clone fetch
+ "hermes_cli/git_commit_graph.py", # every update starts the install checkout's graph refresh
"hermes_cli/process_identity.py", # marker owner liveness
"hermes_cli/runtime_state.py",
"hermes_cli/relaunch.py",
diff --git a/tests/hermes_cli/conftest.py b/tests/hermes_cli/conftest.py
index 59097561175..17bb4256f78 100644
--- a/tests/hermes_cli/conftest.py
+++ b/tests/hermes_cli/conftest.py
@@ -102,6 +102,15 @@ def _source_channels_resolve_locally(request, monkeypatch):
"stable", "stable", repository, branch="stable"))
[email protected](autouse=True)
+def _no_detached_commit_graph_writer(monkeypatch):
+ """The update's maintenance starts a detached ``git commit-graph write`` on the checkout it ran
+ from: in a test, this repository or a tmp copy deleted under it. In-process runs never spawn it."""
+ from hermes_cli import git_commit_graph
+
+ monkeypatch.setattr(git_commit_graph, "spawn_commit_graph_refresh", lambda root: False)
+
+
@pytest.fixture(autouse=True)
def _discharge_host_update_obligation():
"""Start and end every ``hermes_cli`` test with NO host update-restart obligation.
diff --git a/tests/hermes_cli/test_gitlock.py b/tests/hermes_cli/test_gitlock.py
index e201e493c94..b6cd5454905 100644
--- a/tests/hermes_cli/test_gitlock.py
+++ b/tests/hermes_cli/test_gitlock.py
@@ -91,17 +91,24 @@ def test_clear_removes_all_stale_lock_kinds(repo: Path, no_git_running: None) ->
assert not (repo / ".git" / name).exists()
-def test_clear_removes_stale_commit_graph_and_midx_locks(repo: Path, no_git_running: None) -> None:
- """A killed graph/midx writer's lock fails every later write of that file (git: "File exists")."""
- locks = [repo / ".git" / "objects" / rel for rel in (
- "info/commit-graph.lock", "info/commit-graphs/commit-graph-chain.lock",
+def test_graph_refresh_clears_object_store_debris_and_leaves_the_index_lock(repo: Path, no_git_running: None) -> None:
+ """A killed graph/midx writer's lock fails every later write of that file (git: "File exists"). The
+ refresh also runs in whatever repo ``hermes -w`` started in, where index.lock is the user's git's."""
+ from hermes_cli.git_commit_graph import refresh_commit_graph
+
+ git_dir = repo / ".git"
+ debris = [git_dir / "objects" / rel for rel in (
+ "info/commit-graph.lock", "info/commit-graphs/commit-graph-chain.lock", "info/commit-graphs/tmp_graph_x1",
"pack/multi-pack-index.lock", "pack/multi-pack-index.d/multi-pack-index-chain.lock")]
- for lock in locks:
- lock.parent.mkdir(parents=True, exist_ok=True)
- _touch(lock, STALE_LOCK_MIN_AGE_SECONDS + 60)
- clear_stale_git_locks(repo)
- assert [lock for lock in locks if lock.exists()] == []
- subprocess.run(["git", "commit-graph", "write", "--reachable", "--split", "--no-progress"], cwd=repo, check=True)
+ for path in (*debris, git_dir / "index.lock"):
+ path.parent.mkdir(parents=True, exist_ok=True)
+ _touch(path, STALE_LOCK_MIN_AGE_SECONDS + 60)
+ refresh_commit_graph(str(repo))
+ assert [path for path in debris if path.exists()] == []
+ assert (git_dir / "index.lock").exists()
+ subprocess.run(["git", "commit-graph", "verify"], cwd=repo, check=True)
+ # The install checkout's own sweep (every update) still takes it.
+ assert clear_stale_git_locks(repo) == [str(git_dir / "index.lock")]
def test_clear_skips_sweep_while_git_running(repo: Path, monkeypatch: pytest.MonkeyPatch) -> None:
diff --git a/tests/hermes_cli/test_update_finish.py b/tests/hermes_cli/test_update_finish.py
index ef0c2d1c29a..584313f90e6 100644
--- a/tests/hermes_cli/test_update_finish.py
+++ b/tests/hermes_cli/test_update_finish.py
@@ -196,6 +196,9 @@ def completion(tmp_path, monkeypatch):
macos_tcc_anchor.ensure_tcc_anchor = lambda: None
maint._print_post_update_notices_and_self_heals = lambda: None
maint._sync_profiles_after_update = lambda: None
+ # A detached git writer would outlive the test in its deleted tmp checkout.
+ from hermes_cli import git_commit_graph
+ git_commit_graph.spawn_commit_graph_refresh = lambda root: False
maint._print_bundled_skills_sync_report = lambda: None
fleet_verify._print_legacy_units_warning = lambda: None
maint._refresh_dashboard_after_update = lambda **kwargs: None
diff --git a/tests/hermes_cli/test_update_post_commit_followups.py b/tests/hermes_cli/test_update_post_commit_followups.py
index ab2a19db578..ad2ef2e26ae 100644
--- a/tests/hermes_cli/test_update_post_commit_followups.py
+++ b/tests/hermes_cli/test_update_post_commit_followups.py
@@ -145,7 +145,7 @@ def test_failed_config_migration_is_owed_and_later_maintenance_runs(tmp_path, mo
calls.append(("commit_graph", root))
raise OSError("fork failed") # a refresh that cannot start must not fail or owe anything
- monkeypatch.setattr("hermes_cli.worktree_ops.spawn_commit_graph_refresh", broken_graph_refresh)
+ monkeypatch.setattr("hermes_cli.git_commit_graph.spawn_commit_graph_refresh", broken_graph_refresh)
def broken_migration(**kwargs):
raise SystemExit(1) # a migration helper that exits must not fail the committed update
diff --git a/tests/hermes_cli/test_update_post_commit_safety.py b/tests/hermes_cli/test_update_post_commit_safety.py
index fad22d8bfbe..e5a9769a859 100644
--- a/tests/hermes_cli/test_update_post_commit_safety.py
+++ b/tests/hermes_cli/test_update_post_commit_safety.py
@@ -64,6 +64,9 @@ _CHILD = textwrap.dedent('''
update_cmd._fleet_restart_skip_reason = lambda plan: None
update_cmd._surviving_pre_update_serve_runtimes = lambda plan: []
maint._refresh_dashboard_after_update = lambda **kwargs: None
+ # A detached git writer would outlive the test in its deleted tmp checkout.
+ from hermes_cli import git_commit_graph
+ git_commit_graph.spawn_commit_graph_refresh = lambda root: False
verify._print_legacy_units_warning = lambda: None
verify._collect_fleet_snapshot = lambda *args: []
windows_resume = None
diff --git a/tests/hermes_cli/test_worktree_selfheal.py b/tests/hermes_cli/test_worktree_selfheal.py
index 1e7e344e04a..1bf41ab0ba2 100644
--- a/tests/hermes_cli/test_worktree_selfheal.py
+++ b/tests/hermes_cli/test_worktree_selfheal.py
@@ -143,27 +143,47 @@ class TestMaintainPackHealth:
_maintain_pack_health(str(tmp_path / "not-a-repo")) # must not raise
- def test_pass_refreshes_a_stale_commit_graph(self, repo):
- """A graph that stopped covering HEAD makes every ``git tag --merged HEAD`` walk the gap."""
+ def test_pass_refreshes_a_stale_graph_in_a_treeless_clone_without_fetching(self, tmp_path):
+ """A graph that stopped covering HEAD makes every ``git tag --merged HEAD`` walk the gap. In a
+ partial clone over a Bloom graph, a write that recomputes changed paths lazy-fetches the tree
+ of every new commit (#127711): the refresh must write the graph and download nothing."""
import os
import time
from hermes_cli import worktree_ops
- _git(repo, "commit-graph", "write", "--reachable", "--no-progress")
+ src = tmp_path / "src"
+ src.mkdir()
+ _git(src, "init", "-q")
+ _git(src, "config", "uploadpack.allowFilter", "true")
+
+ def commits(start, n):
+ for i in range(start, start + n):
+ (src / f"d{i % 3}").mkdir(exist_ok=True)
+ (src / f"d{i % 3}" / f"f{i}").write_text(f"{i}\n")
+ _git(src, "add", "-A")
+ _git(src, "-c", "user.name=t", "-c", "user.email=t@t", "-c", "maintenance.auto=false",
+ "-c", "gc.auto=0", "commit", "-qm", f"c{i}")
+
+ commits(0, 8)
+ clone = tmp_path / "clone"
+ _git(tmp_path, "clone", "-q", "--filter=tree:0", "--no-checkout", src.as_uri(), str(clone))
+ _git(clone, "commit-graph", "write", "--reachable", "--split", "--changed-paths", "--no-progress")
+ commits(8, 8)
+ _git(clone, "-c", "fetch.writeCommitGraph=false", "-c", "maintenance.auto=false", "-c", "gc.auto=0",
+ "fetch", "-q", "origin")
+ _git(clone, "update-ref", "HEAD", "FETCH_HEAD")
+ info = clone / ".git" / "objects" / "info"
old = time.time() - 3 * 86400
- os.utime(repo / ".git" / "objects" / "info" / "commit-graph", (old, old))
- (repo / "g.txt").write_text("y\n")
- _git(repo, "add", "-A")
- _git(repo, "-c", "maintenance.auto=false", "-c", "gc.auto=0", "commit", "-qm", "after the graph")
+ for graph in (info / "commit-graphs").iterdir():
+ os.utime(graph, (old, old))
+ packs = sorted((clone / ".git" / "objects" / "pack").glob("*.pack"))
- worktree_ops._maintain_pack_health(str(repo))
+ worktree_ops._maintain_pack_health(str(clone))
- info = repo / ".git" / "objects" / "info"
- written = [p.stat().st_mtime for p in (info / "commit-graph", info / "commit-graphs" / "commit-graph-chain")
- if p.is_file()]
- assert written and max(written) > time.time() - 300, "the pass must write a graph covering the new commit"
- _git(repo, "commit-graph", "verify")
+ assert (info / "commit-graphs" / "commit-graph-chain").stat().st_mtime > time.time() - 300
+ assert sorted((clone / ".git" / "objects" / "pack").glob("*.pack")) == packs, "the write lazy-fetched"
+ _git(clone, "commit-graph", "verify")
class TestRepackStampede:
diff --git a/website/docs/getting-started/updating.md b/website/docs/getting-started/updating.md
index 4b7a767bb81..6b94e8ed51e 100644
--- a/website/docs/getting-started/updating.md
+++ b/website/docs/getting-started/updating.md
@@ -318,8 +318,11 @@ whole history: each `hermes update` (after the new code is in place) and each `h
update a missing or day-old graph in the background, at most once every 6 hours, with
`git commit-graph write --reachable --split --no-changed-paths`, which reads commits only and never
downloads a tree. The update does not wait for it, and a write still running after 10 minutes is
-stopped. A `commit-graph.lock` or `multi-pack-index.lock` that a killed git left behind is
-removed by the same stale-lock sweep as `index.lock` once it is 10 minutes old and no git is running.
+stopped (asked to exit first, so git removes its own lock). A failed write is logged to
+`logs/agent.log`. A repo with `core.commitGraph=false` is left without a graph. A
+`commit-graph.lock` or `multi-pack-index.lock` that a killed git left behind is removed by the same
+stale-lock sweep as `index.lock` once it is 10 minutes old and no git is running; in a repo you
+launch `hermes -w` from, only those object-store locks are swept, never `index.lock` or `HEAD.lock`.
Each `hermes update` also spends at most 60 seconds cleaning those packs up, picking up where the
previous update stopped. It deletes packs whose every object is also stored in another pack, then