| name | prune |
| description | Safely remove a finished or abandoned git worktree while preserving unintegrated work. |
prune
Tear down a worktree, safely. The gate is the point: a worktree that is clean
but not yet integrated still holds work, so it is kept by default.
Resolve the bin (cross-agent)
resolve_bonsai_root() {
if [ -n "${BONSAI_BIN:-}" ]; then dirname "$(dirname "$BONSAI_BIN")"; return 0; fi
if [ -n "${CLAUDE_PLUGIN_ROOT:-}" ]; then printf '%s\n' "$CLAUDE_PLUGIN_ROOT"; return 0; fi
if command -v codex >/dev/null 2>&1; then
codex plugin list | awk '$1 == "bonsai@laicluse-agent-fieldkit" { print $NF; found=1; exit } END { exit found ? 0 : 1 }'
return $?
fi
return 1
}
ROOT="$(resolve_bonsai_root)" || { echo "bonsai plugin root not found" >&2; exit 1; }
BONSAI="${BONSAI_BIN:-$ROOT/bin/bonsai}"
Check, then tear down
node "$BONSAI" teardown <worktree|branch> --repo "<root>" --dry-run --json
node "$BONSAI" teardown <worktree|branch> --repo "<root>" --json
A worktree is removed only when it is integrated into the Git default branch
resolved from origin/HEAD, or from init.defaultBranch when that branch exists only locally, or when it is clean with nothing
ahead of that default. Otherwise teardown keeps it and returns removed: false
with a reason. --dry-run reports the same classification and warnings without
removing anything. --force overrides the gate and also deletes an unmerged
branch; reach for it only on a deliberate throwaway, and never wire it into an
unattended caller.