-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantum-loop.sh
More file actions
632 lines (560 loc) · 24.4 KB
/
quantum-loop.sh
File metadata and controls
632 lines (560 loc) · 24.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# quantum-loop.sh -- PLUGIN-LEVEL autonomous development loop.
#
# THIS SCRIPT IS FOR THE QUANTUM-LOOP PLUGIN REPO ITSELF.
# It requires lib/*.sh modules and jq. It operates at the STORY level
# (one story per agent invocation) and uses CLAUDE.md as the agent prompt.
#
# FOR USER PROJECTS: Use templates/quantum-loop.sh instead.
# That script is self-contained (no lib/ dependency), uses node for JSON,
# and operates at the TASK level. Download it via:
# curl -sO https://raw.githubusercontent.com/andyzengmath/quantum-loop/main/templates/quantum-loop.sh
#
# Features: DAG-based story selection, two-stage review gates,
# structured error recovery, parallel execution via worktree agents.
#
# Usage:
# ./quantum-loop.sh [OPTIONS]
#
# Options:
# --max-iterations N Maximum iterations before stopping (default: 20)
# --max-retries N Max retry attempts per story (default: 3)
# --tool TOOL AI tool to use: "claude" (default) or "amp"
# --parallel Enable parallel execution of independent stories
# --max-parallel N Maximum concurrent agents in parallel mode (default: 4)
# --help Show this help message
#
# Prerequisites:
# - quantum.json must exist in the current directory (run /quantum-loop:plan first)
# - jq must be installed
# - claude or amp CLI must be installed
# =============================================================================
# Defaults
MAX_ITERATIONS=20
MAX_RETRIES=3
TOOL="claude"
PARALLEL_MODE=false
MAX_PARALLEL=4
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--max-iterations)
MAX_ITERATIONS="$2"
shift 2
;;
--max-retries)
MAX_RETRIES="$2"
shift 2
;;
--tool)
TOOL="$2"
shift 2
;;
--parallel)
PARALLEL_MODE=true
shift
;;
--max-parallel)
MAX_PARALLEL="$2"
shift 2
;;
--help)
head -24 "$0" | tail -19
exit 0
;;
*)
printf "Unknown option: %s\n" "$1"
exit 1
;;
esac
done
# Validate tool
if [[ "$TOOL" != "claude" && "$TOOL" != "amp" ]]; then
printf "ERROR: --tool must be 'claude' or 'amp'. Got: %s\n" "$TOOL"
exit 1
fi
# Validate dependencies
if ! command -v jq &>/dev/null; then
printf "ERROR: jq is required. Install it: https://jqlang.github.io/jq/download/\n"
exit 1
fi
if ! command -v "$TOOL" &>/dev/null; then
printf "ERROR: %s CLI not found. Please install it first.\n" "$TOOL"
exit 1
fi
# Validate quantum.json
if [[ ! -f quantum.json ]]; then
printf "ERROR: quantum.json not found. Run /quantum-loop:plan first to create it.\n"
exit 1
fi
# Source library functions
source "$SCRIPT_DIR/lib/common.sh" || { printf "ERROR: lib/common.sh not found\n"; exit 1; }
source "$SCRIPT_DIR/lib/json-atomic.sh" || { printf "ERROR: lib/json-atomic.sh not found\n"; exit 1; }
if [[ "$PARALLEL_MODE" == "true" ]]; then
source "$SCRIPT_DIR/lib/dag-query.sh" || { printf "ERROR: lib/dag-query.sh not found\n"; exit 1; }
source "$SCRIPT_DIR/lib/worktree.sh" || { printf "ERROR: lib/worktree.sh not found\n"; exit 1; }
source "$SCRIPT_DIR/lib/spawn.sh" || { printf "ERROR: lib/spawn.sh not found\n"; exit 1; }
source "$SCRIPT_DIR/lib/monitor.sh" || { printf "ERROR: lib/monitor.sh not found\n"; exit 1; }
source "$SCRIPT_DIR/lib/crash-recovery.sh" || { printf "ERROR: lib/crash-recovery.sh not found\n"; exit 1; }
fi
# =============================================================================
# Archive previous run if branch changed
# =============================================================================
BRANCH=$(jq -r '.branchName' quantum.json)
LAST_BRANCH_FILE=".last-ql-branch"
if [[ -f "$LAST_BRANCH_FILE" ]]; then
LAST_BRANCH=$(cat "$LAST_BRANCH_FILE")
if [[ "$LAST_BRANCH" != "$BRANCH" ]]; then
ARCHIVE_DIR="archive/$(date +%Y-%m-%d)-${BRANCH//\//-}"
printf "Branch changed from %s to %s\n" "$LAST_BRANCH" "$BRANCH"
printf "Archiving previous run to %s\n" "$ARCHIVE_DIR"
mkdir -p "$ARCHIVE_DIR"
cp quantum.json "$ARCHIVE_DIR/quantum.json" 2>/dev/null || true
printf "Archive complete.\n"
fi
fi
printf "%s" "$BRANCH" > "$LAST_BRANCH_FILE"
# Update maxAttempts in quantum.json if different from default
jq --argjson max "$MAX_RETRIES" '
.stories |= map(.retries.maxAttempts = $max)
' quantum.json > quantum.json.tmp && mv quantum.json.tmp quantum.json
# =============================================================================
# Summary table function
# =============================================================================
print_summary_table() {
printf "\n"
printf "Summary\n"
printf "%-10s %-40s %-8s %-6s %-8s\n" "Story" "Title" "Status" "Wave" "Retries"
printf "%-10s %-40s %-8s %-6s %-8s\n" "----------" "----------------------------------------" "--------" "------" "--------"
jq -r '.stories[] | "\(.id)|\(.title)|\(.status)|\(.retries.attempts)/\(.retries.maxAttempts)"' quantum.json | \
while IFS='|' read -r sid title status retries; do
printf "%-10s %-40s %-8s %-6s %-8s\n" "$sid" "${title:0:40}" "$status" "-" "$retries"
done
printf "\n"
local total passed failed
total=$(jq '.stories | length' quantum.json)
passed=$(jq '[.stories[] | select(.status == "passed")] | length' quantum.json)
failed=$((total - passed))
printf "Result: %d/%d stories passed\n" "$passed" "$total"
}
# =============================================================================
# Main header
# =============================================================================
printf "===========================================\n"
printf " Quantum-Loop Autonomous Development\n"
printf "===========================================\n"
printf " Branch: %s\n" "$BRANCH"
printf " Tool: %s\n" "$TOOL"
printf " Max Iter: %s\n" "$MAX_ITERATIONS"
printf " Max Retries: %s\n" "$MAX_RETRIES"
if [[ "$PARALLEL_MODE" == "true" ]]; then
printf " Mode: Parallel (max %s concurrent)\n" "$MAX_PARALLEL"
else
printf " Mode: Sequential\n"
fi
printf "===========================================\n\n"
# =============================================================================
# Parallel execution mode
# =============================================================================
if [[ "$PARALLEL_MODE" == "true" ]]; then
# Trap SIGINT/SIGTERM to kill background agents and avoid orphaned processes
declare -a AGENT_PIDS=()
cleanup_on_exit() {
printf "\n[INTERRUPT] Cleaning up agents...\n"
for pid in "${AGENT_PIDS[@]+"${AGENT_PIDS[@]}"}"; do
kill "$pid" 2>/dev/null || true
done
for pid in "${AGENT_PIDS[@]+"${AGENT_PIDS[@]}"}"; do
wait "$pid" 2>/dev/null || true
done
exit 130
}
trap cleanup_on_exit INT TERM
# Crash recovery on startup
REPO_ROOT="$(pwd)"
recover_orphaned_worktrees "$REPO_ROOT/quantum.json" "$REPO_ROOT" || true
cleanup_stale_tmp "$REPO_ROOT/quantum.json" || true
WAVE=0
for ITERATION in $(seq 1 "$MAX_ITERATIONS"); do
printf "\n=== Iteration %d / %d ===\n\n" "$ITERATION" "$MAX_ITERATIONS"
# Get executable stories from DAG
EXECUTABLE=$(get_executable_stories "$REPO_ROOT/quantum.json")
if [[ "$EXECUTABLE" == "COMPLETE" ]]; then
printf "\n===========================================\n"
printf " <quantum>COMPLETE</quantum>\n"
printf " All stories passed! Feature is done.\n"
printf "===========================================\n"
print_summary_table
exit 0
fi
if [[ "$EXECUTABLE" == "BLOCKED" ]]; then
printf "\n===========================================\n"
printf " <quantum>BLOCKED</quantum>\n"
printf " No executable stories remain.\n"
printf "===========================================\n"
print_summary_table
exit 1
fi
if [[ -z "$EXECUTABLE" ]]; then
printf "WARNING: No executable stories found\n"
print_summary_table
exit 1
fi
# Count executable stories
EXEC_COUNT=$(echo "$EXECUTABLE" | jq '. | length')
WAVE=$((WAVE + 1))
# Setup execution metadata
update_execution_field "$REPO_ROOT/quantum.json" "parallel" "$MAX_PARALLEL" "$WAVE" || true
# Arrays to track spawned agents
declare -a AGENT_PIDS=()
declare -a AGENT_STORIES=()
declare -a AGENT_WORKTREES=()
declare -a AGENT_START_TIMES=()
# Spawn agents for each executable story (up to MAX_PARALLEL)
SPAWN_COUNT=0
for i in $(seq 0 $((EXEC_COUNT - 1))); do
if [[ "$SPAWN_COUNT" -ge "$MAX_PARALLEL" ]]; then
break
fi
SID=$(echo "$EXECUTABLE" | jq -r ".[$i]")
STITLE=$(jq -r --arg id "$SID" '.stories[] | select(.id == $id) | .title' "$REPO_ROOT/quantum.json")
# Create worktree
WT_PATH="$REPO_ROOT/.ql-wt/$SID"
if ! create_worktree "$SID" "$BRANCH" "$REPO_ROOT"; then
printf "[ERROR] Failed to create worktree for %s\n" "$SID"
continue
fi
# Update quantum.json
set_story_worktree "$REPO_ROOT/quantum.json" "$SID" ".ql-wt/$SID" || true
jq --arg id "$SID" '
.stories |= map(if .id == $id then .status = "in_progress" else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
# Spawn agent
PID_FILE=$(mktemp)
spawn_autonomous "$SID" "$WT_PATH" > "$PID_FILE"
AGENT_PID=$(cat "$PID_FILE")
rm -f "$PID_FILE"
AGENT_PIDS+=("$AGENT_PID")
AGENT_STORIES+=("$SID")
AGENT_WORKTREES+=("$WT_PATH")
AGENT_START_TIMES+=("$(date +%s)")
SPAWN_COUNT=$((SPAWN_COUNT + 1))
printf "[SPAWNED] %s - %s (wave %d, PID %s)\n" "$SID" "$STITLE" "$WAVE" "$AGENT_PID"
done
if [[ "$SPAWN_COUNT" -eq 0 ]]; then
printf "WARNING: No agents spawned this iteration\n"
continue
fi
# Monitoring loop
while [[ ${#AGENT_PIDS[@]} -gt 0 ]]; do
sleep 5
local_completed=()
for idx in "${!AGENT_PIDS[@]}"; do
PID="${AGENT_PIDS[$idx]}"
SID="${AGENT_STORIES[$idx]}"
WT="${AGENT_WORKTREES[$idx]}"
START="${AGENT_START_TIMES[$idx]}"
# Check timeout
TIMED_OUT=$(check_agent_timeout "$START" "$DEFAULT_AGENT_TIMEOUT")
if [[ "$TIMED_OUT" == "true" ]]; then
kill_agent_process "$PID"
printf "[TIMEOUT] %s\n" "$SID"
# Mark failed with phase timeout
jq --arg id "$SID" '
.stories |= map(if .id == $id then
.status = "failed" |
.retries.attempts += 1 |
.retries.failureLog += [{"phase": "timeout", "timestamp": (now | todate)}]
else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
remove_worktree "$SID" "$REPO_ROOT" || true
clear_story_worktree "$REPO_ROOT/quantum.json" "$SID" || true
local_completed+=("$idx")
continue
fi
# Check status
STATUS=$(check_agent_status "$PID" "$WT")
case "$STATUS" in
RUNNING)
;;
STORY_PASSED)
# Give agent a few seconds to finish after signaling (#9: post-signal timeout)
local wait_start
wait_start=$(date +%s)
while kill -0 "$PID" 2>/dev/null; do
local wait_elapsed=$(( $(date +%s) - wait_start ))
if [[ $wait_elapsed -ge 30 ]]; then
kill "$PID" 2>/dev/null || true
break
fi
sleep 1
done
wait "$PID" 2>/dev/null || true
WT_BRANCH="ql-wt/${SID}"
# Safety commit: ensure agent changes are committed before merge
# Exclude junk files (#4) and quantum.json (#5)
if git -C "$WT" status --porcelain 2>/dev/null | grep -q .; then
git -C "$WT" add -A >/dev/null 2>&1 || true
git -C "$WT" reset HEAD -- quantum.json .ql-agent-output.txt quantum.json.tmp >/dev/null 2>&1 || true
git -C "$WT" checkout -- quantum.json >/dev/null 2>&1 || true
if ! git -C "$WT" diff --cached --quiet 2>/dev/null; then
git -C "$WT" commit -m "feat: ${SID} - auto-commit by orchestrator" >/dev/null 2>&1 || true
fi
fi
local STATUS_MERGE=0
# merge_worktree_branch outputs conflict file list on failure (before aborting)
local MERGE_OUTPUT
MERGE_OUTPUT=$(merge_worktree_branch "$REPO_ROOT" "$WT_BRANCH" 2>&1)
if [[ $? -eq 0 ]]; then
# Post-merge regression test: verify the merge didn't break anything
# Detect test command from quantum.json or common patterns
local TEST_CMD
TEST_CMD=$(jq -r '.testCommand // empty' "$REPO_ROOT/quantum.json" 2>/dev/null)
if [[ -z "$TEST_CMD" ]]; then
# Auto-detect: try common test runners
if [[ -f "$REPO_ROOT/package.json" ]]; then TEST_CMD="npm test"
elif [[ -f "$REPO_ROOT/pyproject.toml" ]] || [[ -f "$REPO_ROOT/setup.py" ]]; then TEST_CMD="python -m pytest -x -q"
elif [[ -f "$REPO_ROOT/Cargo.toml" ]]; then TEST_CMD="cargo test"
fi
fi
if [[ -n "$TEST_CMD" ]]; then
if ! (cd "$REPO_ROOT" && eval "$TEST_CMD" >/dev/null 2>&1); then
printf "[REGRESSION] %s - tests fail after merge, reverting\n" "$SID"
git -C "$REPO_ROOT" revert -m 1 HEAD --no-edit >/dev/null 2>&1 || true
jq --arg id "$SID" '
.stories |= map(if .id == $id then
.status = "failed" |
.retries.attempts += 1 |
.retries.failureLog += [{"phase": "merge_regression", "timestamp": (now | todate)}]
else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
STATUS_MERGE=1
fi
fi
if [[ "$STATUS_MERGE" -eq 0 ]]; then
printf "[PASSED] %s\n" "$SID"
jq --arg id "$SID" --argjson wave "$WAVE" '
.stories |= map(if .id == $id then .status = "passed" else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
fi
else
CONFLICT_FILES="${MERGE_OUTPUT:-unknown}"
STATUS_MERGE=1
printf "[CONFLICT] %s - merge conflict in: %s\n" "$SID" "$CONFLICT_FILES"
printf "[INFO] Branch %s preserved for manual resolution\n" "$WT_BRANCH"
jq --arg id "$SID" --arg files "$CONFLICT_FILES" '
.stories |= map(if .id == $id then
.status = "failed" |
.retries.attempts += 1 |
.retries.failureLog += [{"phase": "merge_conflict", "files": $files, "timestamp": (now | todate)}]
else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
fi
# Only remove worktree dir, preserve branch on conflict for manual resolution (#3)
if [[ "$STATUS_MERGE" == "0" ]]; then
remove_worktree "$SID" "$REPO_ROOT" || true
else
# Remove worktree dir but keep the branch
git -C "$REPO_ROOT" worktree remove --force ".ql-wt/$SID" 2>/dev/null || true
fi
clear_story_worktree "$REPO_ROOT/quantum.json" "$SID" || true
local_completed+=("$idx")
;;
STORY_FAILED)
wait "$PID" 2>/dev/null || true
printf "[FAILED] %s\n" "$SID"
jq --arg id "$SID" '
.stories |= map(if .id == $id then
.status = "failed" |
.retries.attempts += 1 |
.retries.failureLog += [{"phase": "agent_failed", "timestamp": (now | todate)}]
else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
remove_worktree "$SID" "$REPO_ROOT" || true
clear_story_worktree "$REPO_ROOT/quantum.json" "$SID" || true
local_completed+=("$idx")
;;
CRASH)
printf "[CRASH] %s\n" "$SID"
jq --arg id "$SID" '
.stories |= map(if .id == $id then
.status = "failed" |
.retries.attempts += 1 |
.retries.failureLog += [{"phase": "crash", "timestamp": (now | todate)}]
else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
remove_worktree "$SID" "$REPO_ROOT" || true
clear_story_worktree "$REPO_ROOT/quantum.json" "$SID" || true
local_completed+=("$idx")
;;
esac
done
# Remove completed agents from tracking arrays (reverse order to preserve indices)
for ((ci=${#local_completed[@]}-1; ci>=0; ci--)); do
ridx="${local_completed[$ci]}"
unset 'AGENT_PIDS[ridx]'
unset 'AGENT_STORIES[ridx]'
unset 'AGENT_WORKTREES[ridx]'
unset 'AGENT_START_TIMES[ridx]'
done
# Re-index arrays
AGENT_PIDS=("${AGENT_PIDS[@]+"${AGENT_PIDS[@]}"}")
AGENT_STORIES=("${AGENT_STORIES[@]+"${AGENT_STORIES[@]}"}")
AGENT_WORKTREES=("${AGENT_WORKTREES[@]+"${AGENT_WORKTREES[@]}"}")
AGENT_START_TIMES=("${AGENT_START_TIMES[@]+"${AGENT_START_TIMES[@]}"}")
# After completions, check if new stories are unblocked
if [[ ${#local_completed[@]} -gt 0 && ${#AGENT_PIDS[@]} -lt $MAX_PARALLEL ]]; then
NEW_EXEC=$(get_executable_stories "$REPO_ROOT/quantum.json")
if [[ "$NEW_EXEC" != "COMPLETE" && "$NEW_EXEC" != "BLOCKED" && -n "$NEW_EXEC" ]]; then
NEW_COUNT=$(echo "$NEW_EXEC" | jq '. | length')
WAVE=$((WAVE + 1))
for ni in $(seq 0 $((NEW_COUNT - 1))); do
if [[ ${#AGENT_PIDS[@]} -ge $MAX_PARALLEL ]]; then
break
fi
NSID=$(echo "$NEW_EXEC" | jq -r ".[$ni]")
NSTITLE=$(jq -r --arg id "$NSID" '.stories[] | select(.id == $id) | .title' "$REPO_ROOT/quantum.json")
NWT="$REPO_ROOT/.ql-wt/$NSID"
if ! create_worktree "$NSID" "$BRANCH" "$REPO_ROOT"; then
printf "[ERROR] Failed to create worktree for %s\n" "$NSID"
continue
fi
set_story_worktree "$REPO_ROOT/quantum.json" "$NSID" ".ql-wt/$NSID" || true
jq --arg id "$NSID" '
.stories |= map(if .id == $id then .status = "in_progress" else . end)
' "$REPO_ROOT/quantum.json" > "$REPO_ROOT/quantum.json.tmp" \
&& mv "$REPO_ROOT/quantum.json.tmp" "$REPO_ROOT/quantum.json"
NPID_FILE=$(mktemp)
spawn_autonomous "$NSID" "$NWT" > "$NPID_FILE"
NAGENT_PID=$(cat "$NPID_FILE")
rm -f "$NPID_FILE"
AGENT_PIDS+=("$NAGENT_PID")
AGENT_STORIES+=("$NSID")
AGENT_WORKTREES+=("$NWT")
AGENT_START_TIMES+=("$(date +%s)")
printf "[SPAWNED] %s - %s (wave %d, PID %s)\n" "$NSID" "$NSTITLE" "$WAVE" "$NAGENT_PID"
done
fi
fi
done
# Brief pause between iterations
sleep 2
done
printf "\n===========================================\n"
printf " <quantum>MAX_ITERATIONS</quantum>\n"
printf " Reached maximum of %d iterations.\n" "$MAX_ITERATIONS"
printf "===========================================\n"
print_summary_table
exit 2
fi
# =============================================================================
# Sequential execution mode (original behavior)
# =============================================================================
for ITERATION in $(seq 1 "$MAX_ITERATIONS"); do
printf "\n=== Iteration %d / %d ===\n\n" "$ITERATION" "$MAX_ITERATIONS"
# -------------------------------------------------------------------------
# Select next executable story from the dependency DAG
# -------------------------------------------------------------------------
STORY_ID=$(jq -r '
.stories as $all |
[.stories[] |
select(
(.status == "pending" or (.status == "failed" and .retries.attempts < .retries.maxAttempts)) and
(if (.dependsOn | length) == 0 then true
else [.dependsOn[] | . as $dep | $all | map(select(.id == $dep)) | .[0].status] | all(. == "passed")
end)
)
] |
sort_by(.priority) |
.[0].id // empty
' quantum.json)
if [[ -z "$STORY_ID" || "$STORY_ID" == "null" ]]; then
# Check if all stories are passed
ALL_PASSED=$(jq '[.stories[].status] | all(. == "passed")' quantum.json)
if [[ "$ALL_PASSED" == "true" ]]; then
printf "\n===========================================\n"
printf " <quantum>COMPLETE</quantum>\n"
printf " All stories passed! Feature is done.\n"
printf "===========================================\n"
print_summary_table
exit 0
else
printf "\n===========================================\n"
printf " <quantum>BLOCKED</quantum>\n"
printf " No executable stories remain.\n"
printf "===========================================\n"
print_summary_table
exit 1
fi
fi
STORY_TITLE=$(jq -r --arg id "$STORY_ID" '.stories[] | select(.id == $id) | .title' quantum.json)
STORY_ATTEMPT=$(jq -r --arg id "$STORY_ID" '.stories[] | select(.id == $id) | .retries.attempts' quantum.json)
printf "Story: %s - %s\n" "$STORY_ID" "$STORY_TITLE"
printf "Attempt: %d\n" "$((STORY_ATTEMPT + 1))"
printf "\n"
# Mark story as in_progress
jq --arg id "$STORY_ID" '
.stories |= map(if .id == $id then .status = "in_progress" else . end) |
.updatedAt = (now | todate)
' quantum.json > quantum.json.tmp && mv quantum.json.tmp quantum.json
# -------------------------------------------------------------------------
# Spawn fresh AI instance
# -------------------------------------------------------------------------
PROMPT_FILE="$SCRIPT_DIR/CLAUDE.md"
if [[ "$TOOL" == "amp" && -f "$SCRIPT_DIR/prompt.md" ]]; then
PROMPT_FILE="$SCRIPT_DIR/prompt.md"
fi
printf "Spawning %s for story %s...\n" "$TOOL" "$STORY_ID"
if [[ "$TOOL" == "claude" ]]; then
OUTPUT=$(claude --dangerously-skip-permissions --print \
-p "$(cat "$PROMPT_FILE")" \
-- "Implement story $STORY_ID from quantum.json. This is iteration $ITERATION." 2>&1) || true
else
OUTPUT=$(printf "%s\n\nImplement story %s from quantum.json. This is iteration %d." \
"$(cat "$PROMPT_FILE")" "$STORY_ID" "$ITERATION" | amp 2>&1) || true
fi
# -------------------------------------------------------------------------
# Process output
# -------------------------------------------------------------------------
if echo "$OUTPUT" | grep -q "<quantum>COMPLETE</quantum>"; then
printf "\n===========================================\n"
printf " <quantum>COMPLETE</quantum>\n"
printf " All stories passed! Feature is done.\n"
printf "===========================================\n"
print_summary_table
exit 0
elif echo "$OUTPUT" | grep -q "<quantum>STORY_PASSED</quantum>"; then
printf "Story %s PASSED. Continuing to next story...\n" "$STORY_ID"
elif echo "$OUTPUT" | grep -q "<quantum>STORY_FAILED</quantum>"; then
printf "Story %s FAILED (attempt %d). Will retry if attempts remain.\n" "$STORY_ID" "$((STORY_ATTEMPT + 1))"
elif echo "$OUTPUT" | grep -q "<quantum>BLOCKED</quantum>"; then
printf "\n===========================================\n"
printf " <quantum>BLOCKED</quantum>\n"
printf " Agent reports no executable stories.\n"
printf "===========================================\n"
print_summary_table
exit 1
else
printf "WARNING: No recognized signal in output. Story may not have completed cleanly.\n"
printf "Last 10 lines of output:\n"
echo "$OUTPUT" | tail -10
fi
# Brief pause between iterations
sleep 2
done
printf "\n===========================================\n"
printf " <quantum>MAX_ITERATIONS</quantum>\n"
printf " Reached maximum of %d iterations.\n" "$MAX_ITERATIONS"
printf "===========================================\n"
print_summary_table
exit 2