Skip to content

The agent that grows with you

fix(git): delete worktree branches under noninteractive_git_env

John Paul Solivaf3767103e89e
diff --git a/hermes_cli/worktree_ops.py b/hermes_cli/worktree_ops.py
index ba4321b5b9f..ce77b82df55 100644
--- a/hermes_cli/worktree_ops.py
+++ b/hermes_cli/worktree_ops.py
@@ -109,7 +109,9 @@ def _cleanup_failed_worktree_add(repo_root: str, wt_path: Path, branch_name: str
# `remove` needs the dir; `prune` drops the admin entry when it is already gone.
_git_quiet(["worktree", "prune"], repo_root, timeout=15,
stdin=subprocess.DEVNULL, env=noninteractive_git_env())
- _git_quiet(["branch", "-D", branch_name], repo_root, timeout=15)
+ # A ref update runs the repository's reference-transaction hook (core.hooksPath).
+ _git_quiet(["branch", "-D", branch_name], repo_root, timeout=15,
+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())
except Exception as e:
logger.debug("cleanup after failed worktree add: %s", e)
@@ -950,7 +952,9 @@ def _reap_prune_verdicts(repo_root: str, verdicts: list, stale_work_cutoff: floa
if branch and verdict == "reap-keep-branch":
kept_branches.add(branch)
elif branch:
- _git(["branch", "-D", branch], repo_root)
+ # Unattended sweep: the repository's reference-transaction hook must not run.
+ _git(["branch", "-D", branch], repo_root,
+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())
logger.debug("Pruned stale worktree: %s (force=%s)", entry.name, force)
except Exception as e:
logger.debug("Failed to prune worktree %s: %s", entry.name, e)
@@ -1040,5 +1044,6 @@ def _prune_orphaned_branches(repo_root: str, protect: Optional[set] = None) -> N
return
for i in range(0, len(orphaned), 50):
_git_quiet(["branch", "-D"] + orphaned[i:i + 50], repo_root, timeout=30,
- log="Failed to prune orphaned branches")
+ log="Failed to prune orphaned branches",
+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())
logger.debug("Pruned %d orphaned branches", len(orphaned))
diff --git a/tests/security/test_gitspawn_config_injection.py b/tests/security/test_gitspawn_config_injection.py
index 0689e8de78e..6be6d2ae688 100644
--- a/tests/security/test_gitspawn_config_injection.py
+++ b/tests/security/test_gitspawn_config_injection.py
@@ -102,10 +102,12 @@ def _make_malicious_repo(tmp: Path) -> tuple[Path, Path]:
marker = tmp / "MARKER"
hooks = repo / "evil-hooks"
hooks.mkdir()
- hook = hooks / "post-checkout"
marker_shell = marker.as_posix()
- hook.write_text(f"#!/bin/sh\ntouch '{marker_shell}.hook'\n")
- hook.chmod(0o755)
+ # post-checkout fires on `worktree add`; reference-transaction on every ref update, `branch -D` included.
+ for name in ("post-checkout", "reference-transaction"):
+ hook = hooks / name
+ hook.write_text(f"#!/bin/sh\ntouch '{marker_shell}.hook'\n")
+ hook.chmod(0o755)
# Let git encode config values; raw Windows backslashes are escapes.
settings = {
"core.fsmonitor": f"touch '{marker_shell}.fsmonitor'",
@@ -192,8 +194,10 @@ def test_subagent_worktree_add_is_safe(malicious_repo, tmp_path):
def test_index_reading_probes_and_kanban_gc_git_are_safe(malicious_repo, tmp_path):
"""``status`` / ``ls-files`` / ``worktree add`` read the index, which runs ``core.fsmonitor``;
- ``worktree add`` also runs the repository's hooks. Recovery hint, completion probe, kanban
- worktree, worktree-gc ``status`` and the reclaimers' dirty probe (kanban teardown)."""
+ ``worktree add`` also runs the repository's hooks, and ``branch -D`` its reference-transaction
+ hook. Recovery hint, completion probe, kanban worktree, worktree-gc ``status``, the reclaimers'
+ dirty probe (kanban teardown), and the three unattended branch deletions: the reclaim sweep,
+ the orphaned-branch pass and the cleanup after a failed ``worktree add``."""
from hermes_cli import kanban_db_workspace as kw
from hermes_cli import worktree_gc, worktree_ops
from tools.async_delegation_recovery_hints import git_state_hint
@@ -208,6 +212,17 @@ def test_index_reading_probes_and_kanban_gc_git_are_safe(malicious_repo, tmp_pat
assert _fired(marker) == []
assert dirty is False # the probe ran: a skipped one reads as dirty
+ clean = {**os.environ, "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_SYSTEM": os.devnull, "GIT_CONFIG_NOSYSTEM": "1"}
+ subprocess.run(["git", "-C", str(repo), "-c", "core.hooksPath=/dev/null", "branch", "pr-1"], check=True, env=clean)
+ kw._ensure_git_worktree(repo, tmp_path / "wt3", "safe3")
+ 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) == []
+ branches = subprocess.run(["git", "-C", str(repo), "branch", "--format=%(refname:short)"],
+ capture_output=True, text=True, env=clean).stdout.split()
+ assert not {"safe2", "pr-1", "safe3"} & set(branches) # the deletions ran
+
def test_repo_named_filters_never_run_from_kanban_gc_or_hints(tmp_path):
"""A filter driver is named by ``.gitattributes``, so the fixed env pins cannot reach it: