Skip to content

The agent that grows with you

fix(tools): block worktree removal and moves of the running source root

Erosika886092bc54b1
diff --git a/tests/tools/test_self_repo_guard.py b/tests/tools/test_self_repo_guard.py
index 75835f2b2a6..e392309409d 100644
--- a/tests/tools/test_self_repo_guard.py
+++ b/tests/tools/test_self_repo_guard.py
@@ -255,6 +255,71 @@ class TestAllowsSafeCommands:
assert msg is None
+class TestWorktreeTargetingSourceRoot:
+ @pytest.mark.parametrize(
+ "sub",
+ [
+ "remove .",
+ "remove -f .",
+ "remove --force .",
+ "remove -- .",
+ "move . {other}",
+ "move -f . {other}",
+ ],
+ )
+ def test_blocks_relative_target_from_inside(self, repo, tmp_path, sub):
+ command = f"git worktree {sub.format(other=tmp_path / 'moved')}"
+ hit, msg = _detect(command, repo, repo)
+ assert hit is True
+ assert str(repo) in msg
+
+ @pytest.mark.parametrize("action", ["remove", "remove -f", "remove --force"])
+ def test_blocks_absolute_target_from_outside(self, repo, tmp_path, action):
+ hit, _ = _detect(f"git worktree {action} {repo}", tmp_path, repo)
+ assert hit is True
+
+ def test_blocks_move_of_root_from_outside(self, repo, tmp_path):
+ command = f"git worktree move {repo} {tmp_path / 'moved'}"
+ hit, _ = _detect(command, tmp_path, repo)
+ assert hit is True
+
+ def test_blocks_dash_c_worktree_remove(self, repo, tmp_path):
+ hit, _ = _detect(f"git -C {tmp_path} worktree remove {repo}", tmp_path, repo)
+ assert hit is True
+
+ def test_blocks_parent_relative_target_from_subdirectory(self, repo):
+ hit, _ = _detect("git worktree remove ..", repo / "agent", repo)
+ assert hit is True
+
+ def test_blocks_sibling_relative_target(self, repo):
+ hit, _ = _detect(f"git worktree remove ../{repo.name}", repo, repo)
+ assert hit is True
+
+ @pytest.mark.parametrize(
+ "sub",
+ [
+ "add {other}",
+ "add -b feature {other}",
+ "list",
+ "list --porcelain",
+ "prune",
+ "lock {other}",
+ "unlock {other}",
+ "remove {other}",
+ "move {other} {other}-dest",
+ ],
+ )
+ def test_allows_other_worktrees_and_add(self, repo, tmp_path, sub):
+ command = f"git worktree {sub.format(other=tmp_path / 'other-wt')}"
+ hit, _ = _detect(command, repo, repo)
+ assert hit is False
+
+ @pytest.mark.parametrize("sub", ["", "remove", "move", "-f"])
+ def test_incomplete_worktree_command_is_not_blocked(self, repo, sub):
+ hit, _ = _detect(f"git worktree {sub}".strip(), repo, repo)
+ assert hit is False
+
+
class TestSourceRootResolution:
def test_resolves_to_repo_when_git_dir_present(self):
root = get_running_source_root()
diff --git a/tools/self_repo_guard.py b/tools/self_repo_guard.py
index 50c843ecfaf..80e1221b818 100644
--- a/tools/self_repo_guard.py
+++ b/tools/self_repo_guard.py
@@ -27,6 +27,7 @@ _WORKTREE_MUTATIONS = frozenset({
"cherry-pick",
"revert",
})
+_WORKTREE_TARGET_ACTIONS = frozenset({"move", "remove"})
_STASH_SAFE_ACTIONS = frozenset({"list", "show", "create", "store", "drop", "clear"})
_RESET_WORKTREE_MODES = frozenset({"--hard", "--merge", "--keep"})
_KNOWN_GIT_BUILTINS = frozenset({
@@ -500,6 +501,34 @@ def _mutates_worktree(subcommand: str, args: list[str]) -> bool:
return subcommand in _WORKTREE_MUTATIONS
+def _next_positional(args: list[str], index: int) -> int:
+ while index < len(args):
+ arg = args[index]
+ if arg == "--":
+ return index + 1
+ if arg.startswith("-") and arg != "-":
+ index += 1
+ continue
+ return index
+ return index
+
+
+def _inspect_git_worktree(args: list[str], cwd: Path, root: Path) -> str | None:
+ """Block `worktree remove|move` aimed at the running root, from any directory."""
+ action_index = _next_positional(args, 0)
+ if action_index >= len(args):
+ return None
+ action = args[action_index].lower()
+ if action not in _WORKTREE_TARGET_ACTIONS:
+ return None
+ target_index = _next_positional(args, action_index + 1)
+ if target_index >= len(args):
+ return None
+ if _resolve(args[target_index], cwd) == root:
+ return f"git worktree {action}"
+ return None
+
+
def _read_git_alias(executable: str, target: Path, alias: str) -> str | None:
try:
result = subprocess.run(
@@ -526,7 +555,12 @@ def _inspect_git(
target, subcommand, sub_args, inline_aliases = _git_target_and_subcommand(
args, current_dir, env
)
- if subcommand is None or not _is_within(target, root):
+ if subcommand is None:
+ return None
+ # `worktree` names its victim as an argument, so the cwd check does not apply.
+ if subcommand == "worktree":
+ return _inspect_git_worktree(sub_args, target, root)
+ if not _is_within(target, root):
return None
if _mutates_worktree(subcommand, sub_args):
return f"git {subcommand}"