Skip to content
Back to skills

Worktree Start

ASecurity

Set up a git worktree for a parallel-session task and copy required gitignored state

  • 3 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 7, 2026
toolsbashnodegit

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned November 25, 2026

npx -y skills add nirecom/agents --skill worktree-start --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Worktree Start?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Worktree Start
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/nirecom-worktree-start/badge)](https://www.skillsdirectory.com/skills/nirecom-worktree-start)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: worktree-start
description: Set up a git worktree for a parallel-session task and copy required gitignored state
user-invocable: false
---

Set up a new linked worktree and initialize its gitignored state before starting work.

**Personal config:** Unset `WORKTREE_BASE_DIR` → each repo's own `<repo>/.claude/worktrees`; set it (e.g. `D:\src\worktrees`) for one base shared by every repo. `bin/worktree-base` owns the resolution.

## Procedure

Read `rules/branch.md` and `rules/worktree.md` before WS-1 — both on-demand-only, never auto-injected; WS-1 needs the worktree criteria and WS-2 derives the branch name and type from the naming convention.
Read `rules/ops.md` before WS-5 — on-demand-only, never auto-injected; it owns the recovery-options-first decision path any directory or worktree removal a path collision tempts must pass through.

WS-1. Verify the task fits the worktree criteria in `rules/worktree.md` (fit table).
   If it does not fit, report why and stop — set `ENFORCE_WORKTREE=off` in agents config
   and work on main directly instead.

WS-2. Derive the task name and branch type — always automatic, never asked of the user:
   Run `bash "$AGENTS_MAIN_ROOT/skills/worktree-start/scripts/derive-worktree-name.sh"`, adding `--headless <label>` when the caller cannot present `AskUserQuestion` (running as a subagent or forked execution context) or when no workflow session hosts this run. <!-- askuser-none -->
   Read `TASK_NAME=`, `BRANCH_TYPE=`, `REPO_NAME=`, `WORKTREE_BASE=`, and `WORKTREE_PATH=` from its stdout — `REPO_NAME` is the validated `<REPO_NAME>` path component and `WORKTREE_PATH` is `<WORKTREE_BASE>/<TASK_NAME>/<REPO_NAME>`; never infer it yourself. Non-zero exit or a missing `WORKTREE_PATH=` → surface its stderr and stop; never substitute a name of your own and never ask the user for one.
   **Reuse-safety check**: run `git worktree list --porcelain` and find the entry whose `worktree` line equals `<WORKTREE_PATH>` — normalize both sides before comparing: convert every backslash to a forward slash and every MSYS-style `/<drive>/<rest>` path to its `<DRIVE>:/<rest>` form (or vice versa), then strip any trailing slash, then lowercase both sides when the filesystem is case-insensitive (Windows, and default macOS). No entry → continue to WS-3. An entry exists → an existing path is not proof it is safe to attach to, so reuse only when all three hold:
   - Its `branch` line equals `refs/heads/<BRANCH_TYPE>/<TASK_NAME>`; a different branch is a naming collision, not a reusable worktree — surface both paths and branches to the user and stop.
   - It carries neither a `locked` nor a `prunable` line; either means another process or a stale registration owns it — surface the reason and stop.
   - `git -C "<path>" status --porcelain` (standalone command, no chaining) prints nothing; non-empty output is another session's in-flight work — never delete or reset it; report the dirty path and stop.
     The derived task name is deterministic, so resolving the collision requires manual intervention outside this skill (wait for the other session, or remove/relocate that worktree by hand).
   A non-zero exit from `git worktree list --porcelain` or `git -C "<path>" status --porcelain` means ownership cannot be verified — treat it the same as the three failing conditions above: surface the command's stderr and stop.

   All three pass → print the path and branch to stdout, skip WS-3–WS-6 (already exists — do not run `git worktree add`), and continue at WS-7.

WS-3. Worktree path: the `WORKTREE_PATH=` value from WS-2, verbatim. Branch name: `<BRANCH_TYPE>/<TASK_NAME>`. Report both in chat — do not ask for approval.

WS-4. Check for conflicts:
   ```
   git worktree list --porcelain
   ```
   Report any existing worktrees at the same path or on the same branch.

WS-5. Path collision check: the WS-3 path existing without a WS-4 registration is another session's leftover — never remove or reuse it; surface the path and stop.
   `git worktree add` creates the parent directories itself; create none by hand.

WS-6. Create the worktree as its own Bash call — no `;`, `&&`, `||`, `|`, `$()`, or backticks; `enforce-worktree.js` allows only the isolated form from the main worktree:
   ```
   git worktree add <path> -b <BRANCH_TYPE>/<TASK_NAME>
   ```

WS-7. Dispatch the `worktree-copy` worker per `skills/_shared/worker-dispatch.md`. Payload: `worktree_path` (WS-3 path), `branch` (`<BRANCH_TYPE>/<TASK_NAME>`), `session_id` (omit when unknown), `artifact_dir` (the `PLANS_DIR` from WD-1).

   `--headless` (WS-2): skip the gate check and treat it as `proceed` (`CONFIRM_WORKTREE` pinned OFF) — no workflow session, or a subagent/fork context where `AskUserQuestion` is unreachable. <!-- askuser-none -->
   Gate check: apply skills/_shared/confirm-plan.md CPA-3 — run next-step --gate and follow GATE_ACTION.

   Response handling when `GATE_ACTION=proceed`:
   - `status: complete` → surface summary, proceed.
   - `status: partial` → surface warning, proceed (non-blocking).
   - `status: failed` → surface error and stop.

   Response handling when `GATE_ACTION=ask`:
   - `status: complete` → call `AskUserQuestion` (`metadata.source=worktree-start.copy-complete`) to confirm copy results before proceeding.
   - `status: partial` → call `AskUserQuestion` (`metadata.source=worktree-start.copy-partial`) in main (surface denied/errors via artifact log path); user must confirm or abort.
   - `status: failed` → surface error and stop.

WS-7a. Build the CodeGraph index for the new worktree — one Bash call:
   `node "$AGENTS_MAIN_ROOT/bin/codegraph-lifecycle.js" init --path "<WS-3 path>"`
   Always exits 0. Silent no-op when the flag is off or a healthy index is already
   there; it rebuilds an index that exists but is empty, truncated, or corrupt, and
   emits a one-line stderr warning when the tool is missing or a rebuild did not
   finish. Continue to WS-8 in every outcome — indexing accelerates later work, it
   never gates it. It can take tens of seconds on a large repository.

WS-8. Enter the worktree: call the native `EnterWorktree` tool with the WS-3 path.
   Protocol: `skills/_shared/worktree-transition.md`

WS-9. Final report: worktree path, branch, which gitignored state was copied, and whether the session entered the worktree (WS-8).

## Rules

- Never write to `.env` files directly.
- Never copy production secrets (`.env.production`, cloud credentials, deploy keys) to a worktree.
- Always record copied state in `WORKTREE_NOTES.md` so `/worktree-end` can inventory it later.
- Task-name validation lives in `scripts/derive-worktree-name.sh` (D5); it is the sole TASK_NAME validator (CPR-SSOT) and rejects any derived name failing `[a-zA-Z0-9][a-zA-Z0-9_-]*`, exiting non-zero. Never bypass it or hand-write a name.
- `<REPO_NAME>` in the worktree path is validated by the same script (D0) and emitted as `REPO_NAME=`; use that value verbatim.
- Never call `AskUserQuestion` to choose a task name or branch type — WS-2 is fully automatic in every context. <!-- askuser-none -->
- WORKTREE_NOTES.md generation is owned by `bin/worktree-write-notes.js`. Do not write the
  file or edit `.git/info/exclude` manually.
- Report observations via /supervisor-report (trigger conditions: rules/supervisor-reporting.md).

Files in this skill

  • SKILL.md7.3 KB
  • scripts/derive-worktree-name.sh18 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in tools →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.