Skip to content

The agent that grows with you

fix(disk-cleanup): never classify test_* files inside git worktrees as disposable

liuhao1024b3946bb03222
diff --git a/plugins/disk-cleanup/disk_cleanup.py b/plugins/disk-cleanup/disk_cleanup.py
index 96aa93df069..b634db7166f 100755
--- a/plugins/disk-cleanup/disk_cleanup.py
+++ b/plugins/disk-cleanup/disk_cleanup.py
@@ -336,6 +336,17 @@ _TEST_PATTERNS = ("test_", "tmp_")
_TEST_SUFFIXES = (".test.py", ".test.js", ".test.ts", ".test.md")
+def _inside_git_worktree(path: Path) -> bool:
+ """True if *path* sits inside a Git worktree/checkout: a ``.git`` entry (a directory in a
+ normal checkout, a pointer FILE in a linked worktree) exists anywhere on the directory chain.
+ Files there are Git-owned — a ``test_*`` file in a worktree is typically a committed
+ regression test, not session scratch (#115295)."""
+ for parent in path.resolve().parents:
+ if (parent / ".git").exists():
+ return True
+ return False
+
+
def guess_category(path: Path) -> Optional[str]:
"""Category label for *path*, or None if we shouldn't track it (``post_tool_call`` hook)."""
if not is_safe_path(path):
@@ -352,4 +363,9 @@ def guess_category(path: Path) -> Optional[str]:
if top == "cache":
return "temp"
name = path.name
- return "test" if name.startswith(_TEST_PATTERNS) or name.endswith(_TEST_SUFFIXES) else None
+ if name.startswith(_TEST_PATTERNS) or name.endswith(_TEST_SUFFIXES):
+ # Git-owned trees manage their own files: never classify a test_* there as disposable,
+ # so neither tracking nor quick() (which re-validates stored "test" entries through
+ # this function) touches it (#115295).
+ return None if _inside_git_worktree(path) else "test"
+ return None
diff --git a/tests/plugins/test_disk_cleanup_plugin.py b/tests/plugins/test_disk_cleanup_plugin.py
index c84067894d5..f867ace8de9 100644
--- a/tests/plugins/test_disk_cleanup_plugin.py
+++ b/tests/plugins/test_disk_cleanup_plugin.py
@@ -239,6 +239,68 @@ class TestProtectedDirsNeverRmtreed:
assert dg.load_tracked() == []
+class TestGitWorktreeFilesNeverCleaned:
+ """Regression tests for #115295 — git-owned test_* files (committed regression tests
+ inside worktrees/checkouts) are never tracked or auto-deleted; scratch files outside
+ git trees still are."""
+
+ def test_linked_worktree_test_file_not_tracked(self, _isolate_env):
+ """A test_* file inside a linked git worktree ($HERMES_HOME/worktrees/, .git is a
+ pointer FILE) is not classified as disposable test scratch."""
+ dg = _load_lib()
+ wt = _isolate_env / "worktrees" / "repro-wt"
+ wt.mkdir(parents=True)
+ (wt / ".git").write_text("gitdir: /elsewhere/main/.git/worktrees/repro-wt\n")
+ f = wt / "test_durable.py"
+ f.write_text("x")
+ assert dg.guess_category(f) is None
+
+ def test_checkout_test_file_not_tracked(self, _isolate_env):
+ """Same for a plain checkout outside HERMES_HOME. Must be a literal /tmp/hermes-*
+ path — is_safe_path() only accepts that prefix for out-of-home trees, and the
+ pytest tmp dir (/private/var/folders on macOS) would be rejected before the
+ git-ownership check is ever reached."""
+ dg = _load_lib()
+ checkout = Path("/tmp/hermes-cleanup-test-checkout")
+ try:
+ (checkout / ".git").mkdir(parents=True, exist_ok=True)
+ f = checkout / "test_durable.py"
+ f.write_text("x")
+ assert dg.guess_category(f) is None
+ finally:
+ import shutil
+ shutil.rmtree(checkout, ignore_errors=True)
+
+ def test_quick_drops_stale_tracked_worktree_entry_instead_of_deleting(self, _isolate_env):
+ """A stale pre-fix tracked entry (category "test") inside a worktree is dropped by
+ re-validation, not deleted — durable, git-committed tests survive."""
+ dg = _load_lib()
+ wt = _isolate_env / "worktrees" / "repro-wt"
+ wt.mkdir(parents=True)
+ (wt / ".git").mkdir()
+ f = wt / "test_durable.py"
+ f.write_text("x")
+ dg.save_tracked([{"path": str(f), "category": "test",
+ "timestamp": datetime.now(timezone.utc).isoformat(), "size": 1}])
+ result = dg.quick()
+ assert f.exists(), "git-owned test files must never be auto-deleted"
+ assert result["deleted"] == 0
+ assert dg.load_tracked() == [], "stale entry is dropped from tracking, not kept"
+
+ def test_scratch_outside_git_trees_still_cleaned(self, _isolate_env):
+ """Control: root-level test_* scratch (no .git anywhere on the chain) is still
+ auto-deleted — the fix must not leak protection outside git-owned trees."""
+ dg = _load_lib()
+ scratch = _isolate_env / "test_scratch.py"
+ scratch.write_text("x")
+ assert dg.guess_category(scratch) == "test"
+ dg.save_tracked([{"path": str(scratch), "category": "test",
+ "timestamp": datetime.now(timezone.utc).isoformat(), "size": 1}])
+ result = dg.quick()
+ assert not scratch.exists()
+ assert result["deleted"] == 1
+
+
class TestStaleCronEntryMigration:
"""Regression tests for #37721 — stale cron-output entries in tracked.json."""