#!/usr/bin/env bash # Create a seeded task worktree and optionally record local task activity. # Binding: wiki/process/agent-scale.md slices A + D. # # Usage: # tools/worktree-new.sh [--class ] \ # [--key path]... [--no-seed] [--no-activity] # # Creates (default; override root with MISALIGNED_WORKTREE_ROOT): # .Codex/worktrees/ on branch worktree- from origin/main # Runs tools/seed-cargo-target.sh unless --no-seed. # Records with the given class/paths unless --no-activity. `--no-claim` # remains a compatibility alias. Overlap is advisory; the worktree is isolation. set -euo pipefail script_root=$(cd "$(dirname "$0")/.." && pwd) primary=$script_root if [ -f "$script_root/.git" ]; then common=$(git -C "$script_root" rev-parse --path-format=absolute --git-common-dir) case "$common" in */.git) primary=${common%/.git} ;; esac fi cd "$primary" worktree_root=${MISALIGNED_WORKTREE_ROOT:-.Codex/worktrees} case "$worktree_root" in /*) ;; *) worktree_root="$primary/$worktree_root" ;; esac usage() { sed -n '2,14p' "$0" | sed 's/^# \{0,1\}//' exit 2 } task="" class="" seed=1 do_claim=1 keys=() while [ $# -gt 0 ]; do case "$1" in --class) shift; class="${1:-}"; shift || true ;; --key) shift; keys+=("${1:-}"); shift || true ;; --no-seed) seed=0; shift ;; --no-activity|--no-claim) do_claim=0; shift ;; -h|--help) usage ;; -*) echo "unknown flag: $1" >&2 usage ;; *) if [ -z "$task" ]; then task=$1 shift else usage fi ;; esac done [ -n "$task" ] || usage case "$task" in */*|*" "*|worktree-*) echo "FAIL: task name should be a short slug (e.g. dark-frame), not '$task'" >&2 exit 2 ;; esac git fetch origin 2>/dev/null || true if ! git rev-parse --verify origin/main >/dev/null 2>&1; then echo "FAIL: origin/main not available; fetch first" >&2 exit 1 fi wt="$worktree_root/$task" branch="worktree-$task" if [ -e "$wt" ]; then echo "FAIL: worktree path already exists: $wt" >&2 exit 1 fi if git show-ref --verify --quiet "refs/heads/$branch"; then echo "FAIL: branch $branch already exists" >&2 exit 1 fi echo "worktree-new: creating $wt on $branch from origin/main" mkdir -p "$worktree_root" git worktree add -b "$branch" "$wt" origin/main if [ "$seed" -eq 1 ]; then echo "worktree-new: seeding cargo target" (cd "$wt" && bash tools/seed-cargo-target.sh) || \ echo "worktree-new: seed skipped or failed (ok if no primary target cache)" fi if [ "$do_claim" -eq 1 ]; then if [ -z "$class" ]; then class=process fi claim_args=(activity "$task" --class "$class") for k in "${keys[@]+"${keys[@]}"}"; do claim_args+=(--key "$k") done bash "$primary/tools/claim.sh" "${claim_args[@]}" fi cat <