Create a new git worktree with branch and .ai-context.json metadata
Creates a new git worktree with branch and AI metadata for isolated development.
/plugin marketplace add poindexter12/waypoint/plugin install workflows@waypoint-marketplace<branch-name> [--mode <mode>] [--description "<text>"]sonnetCreate new branch (if needed), attach git worktree, generate AI metadata files for isolated development.
Important: The command validates that the worktree name will not conflict with existing top-level directories in your project. If a conflict is detected, creation will be blocked and alternative naming patterns will be suggested.
SYNTAX: /working-tree:new <branch-name> [--mode <mode>] [--description "<text>"]
REQUIRED:
<branch-name>
Type: string
Position: 1
Validation: ^[a-zA-Z0-9/_-]+$
Examples: "feature/login", "bugfix/timeout", "main"
OPTIONAL:
--mode <mode>
Type: enum[main, feature, bugfix, experiment, review]
Default: inferred from branch-name (see MODE_INFERENCE_ALGORITHM)
Validation: must match exactly one of the enum values
--description "<text>"
Type: string (quoted if contains spaces)
Default: "" (empty string)
Validation: any string, no restrictions
APPLY rules sequentially, first match wins:
def infer_mode(branch_name: str) -> str:
if branch_name.startswith("feature/"):
return "feature"
elif branch_name.startswith(("bugfix/", "fix/")):
return "bugfix"
elif branch_name.startswith(("exp/", "experiment/")):
return "experiment"
elif branch_name.startswith("review/"):
return "review"
elif branch_name.startswith("i18n/"):
return "feature" # i18n work is feature-type work
elif branch_name in ("main", "master"):
return "main"
else:
return "feature" # DEFAULT
DETERMINISTIC: Given same branch_name, always produces same mode.
Execute steps sequentially. Each step must complete successfully before proceeding.
EXECUTE:
REPO_ROOT=$(git rev-parse --show-toplevel 2>&1)
EXIT_CODE_ROOT=$?
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>&1)
EXIT_CODE_BRANCH=$?
VALIDATION:
DATA EXTRACTION:
REPO_NAME=$(basename "$REPO_ROOT")
PARENT_DIR=$(dirname "$REPO_ROOT")
NEXT:
PARSE:
# Extract branch name (first positional argument)
BRANCH_NAME="$1"
# Parse --mode flag (if present)
if [[ "$@" =~ --mode[[:space:]]+([a-z]+) ]]; then
MODE_ARG="${BASH_REMATCH[1]}"
else
MODE_ARG=""
fi
# Parse --description flag (if present)
if [[ "$@" =~ --description[[:space:]]+\"([^\"]+)\" ]]; then
DESCRIPTION="${BASH_REMATCH[1]}"
elif [[ "$@" =~ --description[[:space:]]+([^[:space:]]+) ]]; then
DESCRIPTION="${BASH_REMATCH[1]}"
else
DESCRIPTION=""
fi
VALIDATION:
NEXT:
EXECUTE:
if [ -n "$MODE_ARG" ]; then
# Explicit mode provided
MODE="$MODE_ARG"
else
# Infer from branch name using MODE_INFERENCE_ALGORITHM
case "$BRANCH_NAME" in
feature/*)
MODE="feature"
;;
bugfix/*|fix/*)
MODE="bugfix"
;;
exp/*|experiment/*)
MODE="experiment"
;;
review/*)
MODE="review"
;;
i18n/*)
MODE="feature" # i18n work is feature-type work
;;
main|master)
MODE="main"
;;
*)
MODE="feature"
;;
esac
fi
VALIDATION:
DATA:
NEXT:
EXECUTE:
git show-ref --verify --quiet refs/heads/$BRANCH_NAME
BRANCH_EXISTS=$?
BRANCH_EXISTS values:
VALIDATION:
ACTION:
NEXT:
EXECUTE (only if BRANCH_EXISTS == 1):
git branch "$BRANCH_NAME" 2>&1
EXIT_CODE=$?
VALIDATION:
NEXT:
EXECUTE:
# Normalize branch name: replace / and _ with -, convert to lowercase
NORMALIZED_BRANCH=$(echo "$BRANCH_NAME" | tr '/_' '-' | tr '[:upper:]' '[:lower:]')
WORKTREE_NAME="${REPO_NAME}-${NORMALIZED_BRANCH}"
WORKTREE_PATH="${PARENT_DIR}/${WORKTREE_NAME}"
NORMALIZATION RULES:
/ with -_ with -EXAMPLES:
DATA:
NEXT:
EXECUTE:
# Get list of top-level directories in the repository
TOP_LEVEL_DIRS=$(cd "$REPO_ROOT" && ls -d */ 2>/dev/null | sed 's|/||g')
# Check if the normalized branch name matches any top-level directory
CONFLICT_DIR=""
for dir in $TOP_LEVEL_DIRS; do
if [ "$NORMALIZED_BRANCH" = "$dir" ]; then
CONFLICT_DIR="$dir"
break
fi
done
VALIDATION:
CONTEXT:
NEXT:
EXECUTE:
EXISTING_WORKTREE=$(git worktree list --porcelain | grep -A 3 "^branch refs/heads/$BRANCH_NAME$" | grep "^worktree " | cut -d' ' -f2)
VALIDATION:
NEXT:
EXECUTE:
test -e "$WORKTREE_PATH"
DIR_EXISTS=$?
VALIDATION:
NEXT:
EXECUTE:
git worktree add "$WORKTREE_PATH" "$BRANCH_NAME" 2>&1
EXIT_CODE=$?
VALIDATION:
NEXT:
EXECUTE:
CREATED_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
FORMAT: ISO 8601 UTC (example: 2025-11-23T12:34:56Z)
VALIDATION:
NEXT:
CONTENT TEMPLATE:
{
"worktree": "{WORKTREE_NAME}",
"branch": "{BRANCH_NAME}",
"mode": "{MODE}",
"created": "{CREATED_TIMESTAMP}",
"description": "{DESCRIPTION}"
}
SUBSTITUTIONS:
EXECUTE:
cat > "$WORKTREE_PATH/.ai-context.json" <<EOF
{
"worktree": "$WORKTREE_NAME",
"branch": "$BRANCH_NAME",
"mode": "$MODE",
"created": "$CREATED_TIMESTAMP",
"description": "$DESCRIPTION"
}
EOF
VALIDATION:
jq empty "$WORKTREE_PATH/.ai-context.json"NEXT:
CONTENT TEMPLATE:
# Worktree: {WORKTREE_NAME}
**Branch:** `{BRANCH_NAME}`
**Mode:** `{MODE}`
**Created:** {CREATED_TIMESTAMP}
## Purpose
{DESCRIPTION_OR_DEFAULT}
## Mode Semantics
- **main**: Minimal changes, stable work only
- **feature**: Active development, larger changes allowed
- **bugfix**: Isolated, surgical fixes only
- **experiment**: Prototypes, large swings, unsafe changes allowed
- **review**: Documentation, analysis, audits
## About This Worktree
This directory is an independent Git worktree attached to the main repository.
- Main repo: {REPO_ROOT}
- Worktree path: {WORKTREE_PATH}
- Branch: {BRANCH_NAME}
See `.ai-context.json` for machine-readable metadata.
SUBSTITUTIONS:
EXECUTE:
DESCRIPTION_TEXT="${DESCRIPTION:-No description provided}"
cat > "$WORKTREE_PATH/README.working-tree.md" <<EOF
# Worktree: $WORKTREE_NAME
**Branch:** \`$BRANCH_NAME\`
**Mode:** \`$MODE\`
**Created:** $CREATED_TIMESTAMP
## Purpose
$DESCRIPTION_TEXT
## Mode Semantics
- **main**: Minimal changes, stable work only
- **feature**: Active development, larger changes allowed
- **bugfix**: Isolated, surgical fixes only
- **experiment**: Prototypes, large swings, unsafe changes allowed
- **review**: Documentation, analysis, audits
## About This Worktree
This directory is an independent Git worktree attached to the main repository.
- Main repo: $REPO_ROOT
- Worktree path: $WORKTREE_PATH
- Branch: $BRANCH_NAME
See \`.ai-context.json\` for machine-readable metadata.
EOF
VALIDATION:
NEXT:
OUTPUT FORMAT (exact):
Created worktree successfully!
Path: {WORKTREE_PATH}
Branch: {BRANCH_NAME}
Mode: {MODE}
Description: {DESCRIPTION_OR_NONE}
Metadata files created:
✓ .ai-context.json
✓ README.working-tree.md
To switch to this worktree:
cd {WORKTREE_PATH}
SUBSTITUTIONS:
NEXT:
DETECTION:
RESPONSE (exact):
Error: Not in a git repository
Run this command from within a git repository.
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Missing required argument <branch-name>
Usage:
/working-tree:new <branch-name> [--mode <mode>] [--description "<text>"]
Example:
/working-tree:new feature/my-feature
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Invalid branch name '{BRANCH_NAME}'
Branch names must contain only:
- Letters (a-z, A-Z)
- Numbers (0-9)
- Forward slashes (/)
- Hyphens (-)
- Underscores (_)
Example valid names:
feature/login
bugfix/timeout-issue
exp/ai_integration
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Invalid mode '{MODE_ARG}'
Valid modes: main, feature, bugfix, experiment, review
Example:
/working-tree:new my-branch --mode feature
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Git command failed
Git error: {GIT_STDERR}
Check that:
- You're in a git repository
- Git is installed and working
- You have necessary permissions
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Failed to create branch '{BRANCH_NAME}'
Git error: {GIT_STDERR}
Check that:
- Branch name is valid
- You're not in detached HEAD state
- You have permission to create branches
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Worktree name conflicts with existing project directory '{CONFLICT_DIR}/'
The proposed worktree name '{WORKTREE_NAME}' would conflict with the existing
'{CONFLICT_DIR}/' directory in your project.
This can cause confusion during git operations and rebases.
Suggested alternatives:
- Use a prefix: /working-tree:new feat/{BRANCH_NAME}
- Use a suffix: /working-tree:new {BRANCH_NAME}-update
- Be more specific: /working-tree:new feature/{BRANCH_NAME}-refactor
Current branch name: {BRANCH_NAME}
Proposed worktree: {WORKTREE_NAME}
Conflicting directory: {CONFLICT_DIR}/
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Branch '{BRANCH_NAME}' already has a worktree at {EXISTING_WORKTREE}
Use one of:
- /working-tree:list to see all worktrees
- cd {EXISTING_WORKTREE} to use the existing worktree
- /working-tree:destroy {EXISTING_WORKTREE} to remove it first
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Directory '{WORKTREE_PATH}' already exists
Choose a different branch name or remove the existing directory.
To remove:
rm -rf {WORKTREE_PATH}
(Be careful - this will delete all contents)
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Failed to create worktree
Git error: {GIT_STDERR}
Check that:
- Parent directory is writable
- Branch name is valid
- No permission issues
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Error: Failed to write .ai-context.json
The worktree was created but metadata file generation failed.
Worktree location: {WORKTREE_PATH}
You can:
1. Manually create .ai-context.json
2. Use /working-tree:adopt to regenerate metadata
3. Remove worktree with /working-tree:destroy {WORKTREE_PATH}
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
DETECTION:
RESPONSE (exact):
Warning: Failed to write README.working-tree.md
The worktree and .ai-context.json were created successfully.
Worktree location: {WORKTREE_PATH}
You can manually create the README if needed.
TEMPLATE SUBSTITUTIONS:
CONTROL FLOW:
| Tool | Pattern | Permission | Pre-Check | Post-Check | On-Deny-Action |
|---|---|---|---|---|---|
| Bash | git:* | ALLOW | command_safe | validate_output | N/A |
| Bash | date:* | ALLOW | N/A | N/A | N/A |
| Bash | test:* | ALLOW | N/A | N/A | N/A |
| Bash | basename:* | ALLOW | N/A | N/A | N/A |
| Bash | dirname:* | ALLOW | N/A | N/A | N/A |
| Bash | tr:* | ALLOW | N/A | N/A | N/A |
| Bash | grep:* | ALLOW | N/A | N/A | N/A |
| Bash | cat > *.json | ALLOW | parent_dir_writable | valid_json | N/A |
| Bash | cat > *.md | ALLOW | parent_dir_writable | N/A | N/A |
| Bash | jq:* | ALLOW | N/A | N/A | N/A |
| Bash | rm -rf:* | DENY | N/A | N/A | ABORT "Destructive operation not allowed" |
| Bash | sudo:* | DENY | N/A | N/A | ABORT "Elevated privileges" |
| Write | $WORKTREE_PATH/.ai-context.json | ALLOW | dir_exists | valid_json | N/A |
| Write | $WORKTREE_PATH/README.working-tree.md | ALLOW | dir_exists | N/A | N/A |
| Write | */.env | DENY | N/A | N/A | ABORT "Secrets file" |
| Read | * | DENY | N/A | N/A | ABORT "Command is write-only" |
SECURITY CONSTRAINTS:
PRECONDITIONS:
INPUT:
/working-tree:new feature/login-refactor
EXPECTED EXECUTION FLOW:
EXPECTED OUTPUT:
Created worktree successfully!
Path: /Users/dev/myapp-feature-login-refactor
Branch: feature/login-refactor
Mode: feature
Description: None
Metadata files created:
✓ .ai-context.json
✓ README.working-tree.md
To switch to this worktree:
cd /Users/dev/myapp-feature-login-refactor
VALIDATION COMMANDS:
# Verify worktree created
test -d /Users/dev/myapp-feature-login-refactor && echo "PASS" || echo "FAIL"
# Verify branch created
git show-ref --verify refs/heads/feature/login-refactor && echo "PASS" || echo "FAIL"
# Verify .ai-context.json
test -f /Users/dev/myapp-feature-login-refactor/.ai-context.json && echo "PASS" || echo "FAIL"
jq -r '.mode' /Users/dev/myapp-feature-login-refactor/.ai-context.json | grep "feature" && echo "PASS" || echo "FAIL"
# Verify README
test -f /Users/dev/myapp-feature-login-refactor/README.working-tree.md && echo "PASS" || echo "FAIL"
PRECONDITIONS:
INPUT:
/working-tree:new my-experiment --mode experiment --description "Testing new architecture"
EXPECTED EXECUTION FLOW: 1-2. Parse arguments → BRANCH_NAME="my-experiment", MODE_ARG="experiment", DESCRIPTION="Testing new architecture" 3. STEP 3 → MODE="experiment" (explicit, not inferred) 4-13. Standard flow
EXPECTED .ai-context.json:
{
"worktree": "myapp-my-experiment",
"branch": "my-experiment",
"mode": "experiment",
"created": "2025-11-23T12:34:56Z",
"description": "Testing new architecture"
}
VALIDATION:
jq -r '.mode' .ai-context.json | grep "experiment" && echo "PASS" || echo "FAIL"
jq -r '.description' .ai-context.json | grep "Testing new architecture" && echo "PASS" || echo "FAIL"
PRECONDITIONS:
INPUT:
/working-tree:new workflows
EXPECTED EXECUTION FLOW:
EXPECTED OUTPUT:
Error: Worktree name conflicts with existing project directory 'workflows/'
The proposed worktree name 'myapp-workflows' would conflict with the existing
'workflows/' directory in your project.
This can cause confusion during git operations and rebases.
Suggested alternatives:
- Use a prefix: /working-tree:new feat/workflows
- Use a suffix: /working-tree:new workflows-update
- Be more specific: /working-tree:new feature/workflows-refactor
Current branch name: workflows
Proposed worktree: myapp-workflows
Conflicting directory: workflows/
POSTCONDITIONS:
VALIDATION COMMANDS:
# Verify branch was created
git show-ref --verify refs/heads/workflows && echo "PASS" || echo "FAIL"
# Verify no worktree created
test ! -d /Users/dev/myapp-workflows && echo "PASS" || echo "FAIL"
PRECONDITIONS:
INPUT:
/working-tree:new feature/existing
EXPECTED EXECUTION FLOW: 1-6. Standard detection and parsing 7. STEP 7 → No directory conflict 8. STEP 8 → EXISTING_WORKTREE="/Users/dev/myapp-feature-existing" 9. ERROR PATTERN "branch-has-worktree" 10. ABORT
EXPECTED OUTPUT:
Error: Branch 'feature/existing' already has a worktree at /Users/dev/myapp-feature-existing
Use one of:
- /working-tree:list to see all worktrees
- cd /Users/dev/myapp-feature-existing to use the existing worktree
- /working-tree:destroy /Users/dev/myapp-feature-existing to remove it first
POSTCONDITIONS:
PRECONDITIONS:
INPUT:
/working-tree:new test-branch --mode production
EXPECTED EXECUTION FLOW:
EXPECTED OUTPUT:
Error: Invalid mode 'production'
Valid modes: main, feature, bugfix, experiment, review
Example:
/working-tree:new my-branch --mode feature
PRECONDITIONS:
INPUT:
/working-tree:new feature/test
EXPECTED EXECUTION FLOW: 1-8. Standard flow 9. STEP 9 → DIR_EXISTS=0 (directory exists) 10. ERROR PATTERN "directory-exists" 11. ABORT
EXPECTED OUTPUT:
Error: Directory '/Users/dev/myapp-feature-test' already exists
Choose a different branch name or remove the existing directory.
To remove:
rm -rf /Users/dev/myapp-feature-test
(Be careful - this will delete all contents)
For complex worktree strategy or organization questions:
Task(
subagent_type='working-tree-consultant',
description='Worktree strategy consultation',
prompt='[question about worktree organization, naming, or workflow]'
)