Skip to content

The agent that grows with you

fix(scratch): idle clones whose packs are hardlinked to a live repo are pruned again

Teknium15617372b44a
diff --git a/hermes_constants_scratch.py b/hermes_constants_scratch.py
index 81b1c0e2f8c..52bc5b94a97 100644
--- a/hermes_constants_scratch.py
+++ b/hermes_constants_scratch.py
@@ -106,7 +106,9 @@ def subtree_touch_state(path: Path, cutoff: float) -> str:
"""Tri-state form of :func:`subtree_touched_since`: ``touched`` (recent write seen),
``scan-failed`` (unreadable — idle cannot be established, so the entry is kept, but
a failed scan is not activity and is never reported as such), or ``idle``.
- Same walk, same stop-at-first-recent shortcut, same symlink rules."""
+ Same walk, same stop-at-first-recent shortcut, same symlink rules. A hardlinked file
+ (``st_nlink > 1``) does not count either: its mtime is the inode's, and
+ ``git clone --local`` hardlinks packs that git freshens from the source repo."""
try:
if os.lstat(path).st_mtime >= cutoff:
return _TOUCHED
@@ -120,10 +122,11 @@ def subtree_touch_state(path: Path, cutoff: float) -> str:
with os.scandir(stack.pop()) as it:
for child in it:
try:
- if child.stat(follow_symlinks=False).st_mtime >= cutoff:
- return _TOUCHED
+ st = child.stat(follow_symlinks=False)
except OSError:
return _SCAN_FAILED
+ if st.st_mtime >= cutoff and not (st.st_nlink > 1 and child.is_file(follow_symlinks=False)):
+ return _TOUCHED
if child.is_dir(follow_symlinks=False):
stack.append(child.path)
except OSError:
diff --git a/tests/test_scratch_dir.py b/tests/test_scratch_dir.py
index cb6de8b23c8..8917f1d2565 100644
--- a/tests/test_scratch_dir.py
+++ b/tests/test_scratch_dir.py
@@ -72,6 +72,25 @@ def test_prune_removes_idle_entries_and_keeps_trees_written_deep_inside(tmp_path
assert not idle.exists() and live.exists() and fresh.exists()
[email protected]("posix") # DirEntry.stat() reports st_nlink only on POSIX
+def test_prune_ignores_freshened_hardlinks_shared_with_a_live_repo(tmp_path):
+ """``git clone --local`` hardlinks packs into a scratch clone; when git freshens the source
+ repo's copy the shared inode's mtime moves, which must not keep the idle clone alive."""
+ scratch = get_scratch_dir(tmp_path, prune=False)
+ live_pack = tmp_path / "live-repo" / "pack-1.pack"
+ live_pack.parent.mkdir()
+ live_pack.write_bytes(b"P" * 64)
+ clone = scratch / "clone" / ".git" / "objects" / "pack"
+ clone.mkdir(parents=True)
+ os.link(live_pack, clone / "pack-1.pack")
+ ancient = time.time() - 30 * 3600
+ for path in (clone, clone.parent, clone.parent.parent, scratch / "clone"):
+ os.utime(path, (ancient, ancient))
+ os.utime(live_pack) # the live repo freshens its pack: the shared inode is "recent"
+ assert prune_scratch_dir(scratch) == 1
+ assert not (scratch / "clone").exists() and live_pack.read_bytes() == b"P" * 64
+
+
@pytest.mark.platforms("posix") # POSIX directory modes
class TestScratchDirPermissionPolicy:
"""get_scratch_dir must honor the home permission policy instead of a blanket 0700: