Ac git worktree

From WaterplanAI/agentic-config · View interactive version

Ac git worktree is an AI agent skill from the GitHub repository WaterplanAI/agentic-config. Creates a git worktree with asset setup, serial branch/bootstrap creation, shared worker-wave environment setup, and direnv wiring. Triggers on keywords: worktree, create worktree, new worktree, git worktree

How to use this skill

Skill file

Create New Worktree - pi adaptation

Create a new git worktree with:

Current shipped boundary

This pi wrapper now ships, but it stays inside the bounded IT003 contract:

Arguments

Required references

Read these when needed:

Pre-flight checks

  1. Verify repo root:

```bash

REPO_ROOT=$(git rev-parse --show-toplevel)

```

  1. Verify the main checkout is clean before branch/bootstrap work:

```bash

git -C "$REPO_ROOT" status --short

```

  1. Validate worktree_name:
  1. Generate the unique branch/worktree name:

```bash

UUID_PREFIX=$(uuidgen | cut -c1-6 | tr 'A-Z' 'a-z')

FULL_NAME="$UUID_PREFIX-<worktree_name>"

WORKTREE_PATH="$REPO_ROOT/trees/$FULL_NAME"

```

  1. Check for existing worktree path:

```bash

test -e "$WORKTREE_PATH"

```

  1. Check for existing branch:

```bash

git -C "$REPO_ROOT" show-ref --verify --quiet "refs/heads/$FULL_NAME"

```

  1. Check for live .worktree.yml files in the main checkout only:

```bash

find "$REPO_ROOT" \

\( -path "$REPO_ROOT/.git" -o -path "$REPO_ROOT/.git/*" \

-o -path "$REPO_ROOT/trees" -o -path "$REPO_ROOT/trees/*" \

-o -path '/templates' -o -path '/templates/*' \

-o -path '/example' -o -path '/example/*' \

-o -path '/examples' -o -path '/examples/*' \

-o -path '/fixture' -o -path '/fixture/*' \

-o -path '/fixtures' -o -path '/fixtures/*' \

-o -path '/test' -o -path '/test/*' \

-o -path '/tests' -o -path '/tests/*' \) -prune -o \

-name ".worktree.yml" -type f -print

```

  1. If assets_path was provided, validate it before continuing:

```bash

test -d "$ASSETS_PATH"

```

Execution overview

  1. Serial branch/bootstrap
  2. Create the worktree
  3. Apply assets from .worktree.yml or inference
  4. Build ordered environment targets
  5. Run one bounded environment worker wave through subagent
  6. Run direnv allow serially
  7. Commit setup changes if any
  8. Report the final summary, including warnings

Step 1: Serial branch/bootstrap

When the branch does not already exist, inline the shipped ac-git-branch flow with the bundled helper script:

source "../../assets/scripts/spec-resolver.sh"

git -C "$REPO_ROOT" checkout -b "$FULL_NAME"

RELATIVE_PATH="$(date +%Y)/$(date +%m)/$FULL_NAME/000-backlog.md"
SPEC_FILE=$(resolve_spec_path "$RELATIVE_PATH")
SPEC_DIR="${SPEC_FILE%/*}"
: > "$SPEC_FILE"

commit_spec_changes "$SPEC_FILE" "CREATE" "000" "backlog"

Record for the final summary:

Why this stays serial and skill-owned:

Step 2: Create the worktree

mkdir -p "$REPO_ROOT/trees"
git -C "$REPO_ROOT" worktree add "$WORKTREE_PATH" "$FULL_NAME"

After creation, verify:

git -C "$WORKTREE_PATH" rev-parse --show-toplevel
git -C "$WORKTREE_PATH" rev-parse --abbrev-ref HEAD

Step 3: Discover and apply assets

Keep this step in the coordinator.

Mode A: explicit `assets_path`

Use the provided path as the root asset source.

Mode B: `.worktree.yml`

When config files exist, process every live .worktree.yml inside the new worktree, excluding template/example/fixture/test directories.

For each config file:

  1. Determine its directory.
  2. Resolve assets.source relative to that config directory, not the repo root.
  3. Apply assets.symlink entries as symlinks.
  4. Apply assets.copy entries as copies.
  5. Apply root_symlinks from the main checkout root, not from assets.source.
  6. Record every applied action for the final summary.

Use yq when available. Otherwise, use a short python3 snippet with yaml.safe_load. If neither yq nor Python YAML parsing is available, STOP and explain that the config cannot be applied honestly.

Representative structure:

for CONFIG_FILE in $(find "$WORKTREE_PATH" \
  \( -path '*/templates' -o -path '*/templates/*' \
     -o -path '*/example' -o -path '*/example/*' \
     -o -path '*/examples' -o -path '*/examples/*' \
     -o -path '*/fixture' -o -path '*/fixture/*' \
     -o -path '*/fixtures' -o -path '*/fixtures/*' \
     -o -path '*/test' -o -path '*/test/*' \
     -o -path '*/tests' -o -path '*/tests/*' \) -prune -o \
  -name ".worktree.yml" -type f -print); do
  CONFIG_DIR=$(dirname "$CONFIG_FILE")
  CONFIG_RELATIVE=$(realpath --relative-to="$WORKTREE_PATH" "$CONFIG_DIR")

  # Resolve assets.source relative to CONFIG_DIR.
  # Apply copy/symlink actions into CONFIG_DIR.
  # Resolve root_symlinks from the main checkout root.
done

Mode C: inference

If no .worktree.yml exists and no explicit assets_path was provided, infer assets in this order:

  1. sibling assets directories:

```bash

REPO_NAME=$(basename "$REPO_ROOT")

REPO_PARENT=$(dirname "$REPO_ROOT")

for candidate in \

"$REPO_PARENT/${REPO_NAME}-assets" \

"$REPO_PARENT/assets" \

"$REPO_PARENT/.${REPO_NAME}-assets"; do

test -d "$candidate" && echo "$candidate"

done

```

  1. ignored local assets:

```bash

git -C "$REPO_ROOT" ls-files --others --ignored --exclude-standard --directory

```

Inference rules:

Step 4: Build the ordered environment target list

Create one ordered target list for the environment wave.

Preferred source of truth:

  1. .worktree.yml environments entries, preserving config order
  2. otherwise infer from project markers inside the worktree

Marker-based inference:

For each target, capture:

Create scratch directories outside the worktree so they are not committed accidentally:

RUN_ROOT="$REPO_ROOT/tmp/worktree/$FULL_NAME"
mkdir -p "$RUN_ROOT/reports" "$RUN_ROOT/results"

If no environment targets are detected, continue without a worker wave and report that no environment bootstrap was required.

Step 5: Run the shared environment worker wave

Use exactly one synchronous subagent wave for the environment setup step.

Worker contract

Every worker task must explicitly tell the worker to:

  1. read node_modules/@agentic-config/pi-compat/assets/orchestration/protocol/worker.md
  2. touch only its assigned target path
  3. write a detailed markdown report to the assigned report path
  4. call:

```bash

node node_modules/@agentic-config/pi-compat/assets/orchestration/tools/write-result.js \

--result-path "$RESULT_PATH" \

--worker-id "$WORKER_ID" \

--status "$STATUS" \

--summary "$SUMMARY" \

--report-path "$REPORT_PATH" \

--target "$TARGET_PATH"

```

  1. return a concise completion message after the result file is written

Worker responsibilities by environment type

Python

```sh

dotenv_if_exists .env

source .venv/bin/activate

```

Node

```sh

dotenv_if_exists .env

PATH_add node_modules/.bin

```

Rust / Go / Ruby

Wave result policy

For worktree, warnings are allowed when setup completed but produced a non-fatal issue.

Failures and missing result files are not allowed.

After the wave finishes, summarize the ordered results:

node node_modules/@agentic-config/pi-compat/assets/orchestration/tools/summarize-results.js \
  --result "$RUN_ROOT/results/01-...json" \
  --result "$RUN_ROOT/results/02-...json" \
  --fail-on-missing \
  --fail-on-status fail \
  --format json

Coordinator rules after summarization:

Step 6: Run `direnv allow` serially

Keep this coordinator-owned.

For each environment target path that now contains .envrc:

if DIRENV_BIN=$(command -v direnv); then
  (cd "$TARGET_PATH" && "$DIRENV_BIN" allow)
else
  echo "WARN: direnv not installed; skipped allow for $TARGET_PATH"
fi

Treat missing direnv as a warning, not a failure.

Step 7: Commit setup changes in the worktree

Leave the new worktree in a clean committed state whenever there are tracked changes.

cd "$WORKTREE_PATH"
git add -A
git status --short

if ! git diff --cached --quiet; then
  git commit -m "chore(worktree): setup $FULL_NAME

- Configure asset links and copies
- Bootstrap development environments
- Add direnv configuration"
fi

Do not commit transient scratch state from tmp/worktree/...; that directory lives outside the worktree root.

Step 8: Final summary

Report:

Error handling

| Condition | Action |

|---|---|

| main checkout is dirty before bootstrap | STOP |

| worktree path already exists | STOP |

| branch already exists | Ask user whether to reuse or abort |

| explicit assets_path missing | STOP |

| .worktree.yml exists but cannot be parsed honestly | STOP |

| inferred asset candidate missing | warn and continue |

| worker result missing | STOP |

| worker status fail | STOP |

| worker status warn | continue and report explicitly |

| direnv missing | warn and continue |

Path resolution rule for `.worktree.yml`

This is the most important correctness rule in the asset step.

Resolve every .worktree.yml path relative to the config file's directory, not the repo root and not the current shell directory.

Correct pattern:

CONFIG_DIR=$(dirname "$CONFIG_FILE")
ASSETS_SOURCE=$(cd "$CONFIG_DIR" && realpath "$SOURCE_VALUE")

Wrong pattern:

ASSETS_SOURCE=$(realpath "$SOURCE_VALUE")

The wrong pattern breaks monorepos with nested .worktree.yml files.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.