Skip to content

The agent that grows with you

fix(worktree): run the attended plain remove under the tree's git env

kshitijk4poora2ecf8333612
diff --git a/hermes_cli/worktree_gc.py b/hermes_cli/worktree_gc.py
index 0aca7ff515b..387e8858121 100644
--- a/hermes_cli/worktree_gc.py
+++ b/hermes_cli/worktree_gc.py
@@ -286,6 +286,7 @@ def reclaim_worktrees(
"""Remove every reap-verdict tree from a frozen audit list — never re-globs inside the
destructive loop, so trees created by concurrent sessions after the audit are out of scope."""
from hermes_cli import worktree_ops as _ops
+ from hermes_cli._subprocess_compat import FILTER_DISCOVERY_FAILED, noninteractive_repo_git_env
if records is None:
records = audit_worktrees(repo_root, with_sizes=False)
@@ -329,14 +330,19 @@ def reclaim_worktrees(
with contextlib.suppress(Exception):
_git(["worktree", "unlock", record.path], cwd=repo_root, timeout=10)
try:
- remove_args = ["worktree", "remove", record.path]
+ # Plain remove reads the TREE's index: its filters (config.worktree) need the tree's env.
+ env = noninteractive_repo_git_env(record.path)
+ if env is None:
+ actions.append(f"kept {record.name} ({FILTER_DISCOVERY_FAILED})")
+ continue
+ remove_args = ["git", "worktree", "remove", record.path]
if record.untracked:
remove_args.append("--force") # only after the audited scratch was archived
- remove_result = _git(remove_args, cwd=repo_root, timeout=30)
+ remove_result = _run(remove_args, 30, repo_root, env=env)
# Plain remove always refuses trees with submodules; --force only after a fresh clean check.
if (remove_result.returncode != 0 and not record.untracked
and "submodules" in remove_result.stderr and _dirty_split(record.path) == (False, [])):
- remove_result = _git([*remove_args, "--force"], cwd=repo_root, timeout=30)
+ remove_result = _run([*remove_args, "--force"], 30, repo_root, env=env)
if remove_result.returncode != 0:
actions.append(f"failed to remove {record.name}: {remove_result.stderr.strip()}")
continue
diff --git a/tests/security/test_gitspawn_config_injection.py b/tests/security/test_gitspawn_config_injection.py
index 99ab74793c3..cde9636a6b2 100644
--- a/tests/security/test_gitspawn_config_injection.py
+++ b/tests/security/test_gitspawn_config_injection.py
@@ -124,7 +124,7 @@ def _make_malicious_repo(tmp: Path) -> tuple[Path, Path]:
def _fired(marker: Path) -> list[str]:
out = []
- for sink in ("fsmonitor", "hook", "extdiff", "textconv", "ssh"):
+ for sink in ("fsmonitor", "hook", "extdiff", "textconv", "ssh", "clean"):
p = Path(f"{marker}.{sink}")
if p.exists():
out.append(sink)
@@ -216,11 +216,22 @@ def test_index_reading_probes_and_kanban_gc_git_are_safe(malicious_repo, tmp_pat
subprocess.run(["git", "-C", str(repo), "-c", "core.hooksPath=/dev/null", "branch", "pr-1"], check=True,
env=_CLEAN_GIT_ENV)
kw._ensure_git_worktree(repo, tmp_path / "wt3", "safe3")
+ # A clean filter defined only in the tree's config.worktree: the plain remove re-hashes a stat-dirty README.
+ subprocess.run(["git", "-C", str(repo), "config", "extensions.worktreeConfig", "true"], check=True,
+ env=_CLEAN_GIT_ENV)
+ (repo / ".git" / "info" / "attributes").write_text("README filter=evil\n")
+
+ def _tree_filter(tree: Path) -> None:
+ subprocess.run(["git", "-C", str(tree), "config", "--worktree", "filter.evil.clean",
+ f"touch '{marker.as_posix()}.clean'; cat"], check=True, env=_CLEAN_GIT_ENV)
+ os.utime(tree / "README", (time.time() + 60, time.time() + 60))
+ _tree_filter(tmp_path / "wt2")
worktree_ops._reap_prune_verdicts(str(repo), [(tmp_path / "wt2", 0.0, False, "reap", None)], 0.0)
worktree_ops._prune_orphaned_branches(str(repo))
worktree_ops._cleanup_failed_worktree_add(str(repo), tmp_path / "wt3", "safe3")
assert _fired(marker) == []
kw._ensure_git_worktree(repo, tmp_path / "wt4", "safe4")
+ _tree_filter(tmp_path / "wt4")
record = worktree_gc.TreeRecord("wt4", str(tmp_path / "wt4"), "safe4", 9.0, None, "reap", "")
assert worktree_gc.reclaim_worktrees(str(repo), records=[record]) == ["removed wt4"] # attended reclaim
assert _fired(marker) == []