-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathship-parallel.sh
More file actions
executable file
·154 lines (131 loc) · 4.71 KB
/
ship-parallel.sh
File metadata and controls
executable file
·154 lines (131 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
set -e
PROJECT_NAME=${1:-$(basename $(pwd))}
WORKTREE_DIR=".worktrees"
LOG_DIR="logs"
MAX_RETRIES=5
mkdir -p "$WORKTREE_DIR" "$LOG_DIR"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_DIR/ship-parallel.log"
}
usage() {
echo "Usage: $0 <command> [options]"
echo ""
echo "Commands:"
echo " plan <task1,task2,task3> Plan parallel tasks (comma-separated)"
echo " dispatch <num> Dispatch N parallel subagents"
echo " status Show worktree status"
echo " merge Merge all completed worktrees to main"
echo " clean Remove all worktrees"
echo ""
echo "Examples:"
echo " $0 plan 'test-auth,test-api,test-db'"
echo " $0 dispatch 3"
echo " $0 merge"
}
# Ensure on main branch
git checkout main 2>/dev/null || git checkout master 2>/dev/null
case "$1" in
plan)
log "=== Planning parallel tasks ==="
TASKS=$(echo "$2" | tr ',' '\n')
IDX=0
for TASK in $TASKS; do
TASK=$(echo "$TASK" | xargs)
BTASK=$(echo "$TASK" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
log "Task $((++IDX)): $TASK → branch feature/$BTASK"
done
log "Ready to dispatch: $# tasks"
;;
dispatch)
NUM=${2:-3}
log "=== Dispatching $NUM parallel subagents ==="
# Get unchecked items
ITEMS=$(grep "^\- \[ \]" TODO.md 2>/dev/null | head -n "$NUM" || echo "")
if [ -z "$ITEMS" ]; then
log "No unchecked items found in TODO.md"
exit 1
fi
IDX=0
declare -A WORKTREES
# Create worktree for each task
for ITEM in $ITEMS; do
TASK_NAME=$(echo "$ITEM" | sed 's/- \[ \] //' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | cut -c1-30)
BRANCH="feature/$(date +%s)-$TASK_NAME"
log "Creating worktree: $BRANCH"
git worktree add "$WORKTREE_DIR/$TASK_NAME" -b "$BRANCH" 2>/dev/null || {
log "Worktree $TASK_NAME exists, skipping"
continue
}
WORKTREES[$IDX]="$WORKTREE_DIR/$TASK_NAME"
IDX=$((IDX + 1))
if [ $IDX -ge $NUM ]; then
break
fi
done
log "Created ${#WORKTREES[@]} worktrees"
# Dispatch subagent for each worktree
IDX=0
for WT in "${WORKTREES[@]}"; do
TASK_NAME=$(basename "$WT")
WT_ABS=$(realpath "$WT")
log "Dispatching agent $((++IDX)) to $WT_ABS"
(
cd "$WT_ABS"
opencode run "\
You are working in isolated worktree: $WT_ABS
MANDATORY WORKFLOW:
1. Use LeanKG first: mcp_status, then search_code, find_function, get_impact_radius
2. Load superpowers/test-driven-development - RED-GREEN-REFACTOR cycle
3. For your assigned task: implement, write tests, verify tests pass
4. Mark TODO item as [- [x]] in $WT_ABS/TODO.md
5. Commit: 'git add -A && git commit -m \"feat: <task>\"'
6. Return summary of changes
" --project "$PROJECT_NAME" 2>&1 | tee -a "$LOG_DIR/ship-parallel-$TASK_NAME.log"
) &
done
log "All $IDX agents dispatched in background"
log "Monitor with: tail -f logs/ship-parallel-*.log"
;;
status)
log "=== Worktree Status ==="
git worktree list
echo ""
for WT in $(ls -d $WORKTREE_DIR/*/ 2>/dev/null); do
TASK=$(basename "$WT")
COUNT=$(grep -c "^\- \[ \]" "$WT/TODO.md" 2>/dev/null || echo "?")
log "$TASK: $COUNT items remaining"
done
;;
merge)
log "=== Merging worktrees to main ==="
git checkout main || git checkout master
git pull
for WT in $(ls -d $WORKTREE_DIR/*/ 2>/dev/null); do
TASK=$(basename "$WT")
BRANCH=$(git -C "$WT" rev-parse --abbrev-ref HEAD)
log "Merging $BRANCH from $WT"
git merge "$BRANCH" --no-edit || log "Merge conflict in $TASK - resolve manually"
# Prune worktree after merge
git worktree prune
rm -rf "$WT"
done
log "Push changes:"
echo " git push"
;;
clean)
log "=== Cleaning worktrees ==="
for WT in $(ls -d $WORKTREE_DIR/*/ 2>/dev/null); do
TASK=$(basename "$WT")
git worktree remove "$WT" --force 2>/dev/null || true
log "Removed $TASK"
done
git worktree prune
log "Clean complete"
;;
*)
usage
exit 1
;;
esac
log "=== Done ==="