Skip to content

The agent that grows with you

fix(worktree): run the startup plain remove under the repo-safe git env

kshitijk4poor5a4b3504802d
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index 336164cf966..0a8bc38f32a 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -927,6 +927,7 @@ def _reap_prune_verdicts(repo_root: str, verdicts: list, stale_work_cutoff: floa
*kept_branches* must survive the orphaned-branch pass. Branch deletion is gated on
``worktree remove`` succeeding so a failed removal never orphans reachable commits.
"""
+ from hermes_cli._subprocess_compat import noninteractive_repo_git_env
preserved_stale: list = []
kept_branches: set = set()
for entry, mtime, force, verdict, lock_state in verdicts:
@@ -960,7 +961,12 @@ def _reap_prune_verdicts(repo_root: str, verdicts: list, stale_work_cutoff: floa
# allows; drop them so the plain (non --force) remove still succeeds.
for rel in _include_symlink_paths(str(entry), repo_root):
(entry / rel).unlink()
- remove_result = _git(["worktree", "remove", str(entry)], repo_root, timeout=15)
+ # Without --force, remove reads the tree's index, which runs core.fsmonitor.
+ remove_env = noninteractive_repo_git_env(str(entry))
+ if remove_env is None:
+ continue
+ remove_result = _git(["worktree", "remove", str(entry)], repo_root, timeout=15,
+ stdin=subprocess.DEVNULL, env=remove_env)
if remove_result.returncode != 0:
logger.debug("Failed to remove worktree %s: %s", entry.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 63dac267d67..99ab74793c3 100644
--- a/tests/security/test_gitspawn_config_injection.py
+++ b/tests/security/test_gitspawn_config_injection.py
@@ -220,9 +220,13 @@ def test_index_reading_probes_and_kanban_gc_git_are_safe(malicious_repo, tmp_pat
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")
+ 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) == []
branches = subprocess.run(["git", "-C", str(repo), "branch", "--format=%(refname:short)"],
capture_output=True, text=True, env=_CLEAN_GIT_ENV).stdout.split()
- assert not {"safe2", "pr-1", "safe3"} & set(branches) # the deletions ran
+ assert not {"safe2", "pr-1", "safe3", "safe4"} & set(branches) # the deletions ran
for key, value in {"remote.origin.url": "ssh://[email protected]/x.git",
"core.sshCommand": f"touch '{marker.as_posix()}.ssh'; false"}.items():