Skip to content

API Reference parrot.sdd.scripts.remove_worktree

github-actions[bot] edited this page Nov 10, 2026 · 4 revisions

parrot.sdd.scripts.remove_worktree

View source

Inspect and safely remove ai-parrot git worktrees.

Two kinds of leftovers accumulate under the resolved worktree root (default .parrot/worktrees/):

  • registered worktrees — present in git worktree list, holding a real feature branch;
  • orphan directories — a directory git no longer tracks (its admin data under .git/worktrees/ was pruned, or the checkout was moved). These are invisible to git worktree list yet still occupy disk.

Removing either while a claude --agent sdd-worker process is live inside is destructive: the worker commits as it goes, and a removal mid-task drops whatever it has not pushed. Liveness is therefore checked by resolving every process's real working directory via /proc/<pid>/cwd — a substring match on the command line alone misses a worker that cd-ed in, and flags an unrelated process that merely mentions the path.

Usage::

source .venv/bin/activate

parrot sdd worktree list
parrot sdd worktree remove feat-310-eventbus-v2 --dry-run
parrot sdd worktree remove FEAT-417
parrot sdd worktree remove --stale       # orphan dirs only
parrot sdd worktree remove <target> --delete-branch

Safety gates (each overridable only with an explicit flag):

  • never the primary worktree, never main / dev / staging;
  • a live process inside the worktree aborts the removal (no override — stop the worker first);
  • uncommitted changes abort unless --force;
  • commits not present on the upstream branch abort unless --force.

Attribute PROTECTED_BRANCHES

Class Worktree

A worktree candidate for removal.

Parameter Type Default Description
path Path
branch str | None
registered bool
primary bool False
live_processes list[tuple[int, str]] list()
nested_registered list[Path] list()

Fields:

Field Type Default Description
path Path Absolute checkout path.
branch str | None Branch name, or None for a detached / orphan checkout.
registered bool True when git still tracks it in git worktree list.
primary bool False True for the main repository checkout.
live_processes list[tuple[int, str]] field(default_factory=list)
nested_registered list[Path] field(default_factory=list) Registered worktree paths that live BELOW path. Only ever non-empty for an orphan directory — a coder pool dir is not itself registered, but its TASK-* children are.

Attributes:

Attribute Type Description
name str

Method Worktree.dirty_files

Return porcelain status lines, empty when clean.

dirty_files() -> list[str]

Returns: list[str]

Method Worktree.unpushed

Return commits on this branch that origin does not have.

Falls back to comparing against origin/dev when the branch has no upstream configured — a worker branch that was never pushed has no @{u}, and that is exactly the case worth warning about.

unpushed() -> list[str]

Returns: list[str]

Method Worktree.pr_state

Return the PR state for this branch via gh, or None if unknown.

pr_state() -> str | None

Returns: str | None

Function cmd_list

Print every worktree with its safety-relevant state.

cmd_list(args: argparse.Namespace) -> int

Parameter Type Default Description
args argparse.Namespace

Returns: int

Function cmd_remove

Remove one or more worktrees after the safety gates pass.

cmd_remove(args: argparse.Namespace) -> int

Parameter Type Default Description
args argparse.Namespace

Returns: int

Function discover

Enumerate registered worktrees plus orphan directories.

discover() -> list[Worktree]

Returns: list[Worktree]

Function live_processes_in

Return live cwd matches, skipping own PID and explicitly excluded PIDs.

A process counts as "inside" when its resolved cwd is the worktree or a descendant of it. Callers that need to exclude their ancestry must supply those PIDs explicitly.

live_processes_in(path: Path, exclude_pids: Collection[int] = ()) -> list[tuple[int, str]]

Parameter Type Default Description
path Path
exclude_pids Collection[int] ()

Returns: list[tuple[int, str]]

Function main

Run list or remove with the original safety gates and flags.

main(argv: Sequence[str] | None = None) -> int

Parameter Type Default Description
argv Sequence[str] | None None

Returns: int

Function repo_root

Return the primary checkout root from git's common directory.

repo_root() -> Path

Returns: Path

Function run

Run a command, capturing stdout/stderr as text.

run(cmd: list[str], cwd: Path | None = None) -> subprocess.CompletedProcess

Parameter Type Default Description
cmd list[str]
cwd Path | None None

Returns: subprocess.CompletedProcess

Alias worktree_root

Re-exported from parrot.sdd.config.worktree_root.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally