-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquantum-loop.ps1
More file actions
450 lines (394 loc) · 19 KB
/
quantum-loop.ps1
File metadata and controls
450 lines (394 loc) · 19 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
<#
.SYNOPSIS
Quantum-Loop autonomous development loop for Windows (native PowerShell).
.DESCRIPTION
Sequential execution of stories from quantum.json via Claude Code CLI.
Each iteration spawns a fresh Claude Code instance with CLAUDE.md instructions.
No bash, no WSL, no Git Bash required.
.PARAMETER MaxIterations
Maximum iterations before stopping (default: 20)
.PARAMETER MaxRetries
Max retry attempts per story (default: 3)
.PARAMETER SkipPermissions
Add --dangerously-skip-permissions to Claude CLI calls
.PARAMETER Model
Override the Claude model
.EXAMPLE
.\quantum-loop.ps1 -MaxIterations 20 -SkipPermissions
.\quantum-loop.ps1 -MaxIterations 50 -SkipPermissions -Model "claude-sonnet-4-5-20250514"
#>
param(
[int]$MaxIterations = 20,
[int]$MaxRetries = 3,
[int]$StaleTimeout = 20,
[switch]$SkipPermissions,
[string]$Model = "",
[string]$Tool = "claude",
[switch]$NonInteractive
)
$ErrorActionPreference = "Stop"
# ─── Dependency Check ───
if (-not (Get-Command "jq" -ErrorAction SilentlyContinue)) {
Write-Error "jq not found. Install it: https://jqlang.github.io/jq/download/"
exit 1
}
if (-not (Test-Path "quantum.json")) {
Write-Error "quantum.json not found. Run /quantum-loop:plan first."
exit 1
}
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RunnersDir = Join-Path $ScriptDir "runners"
# ─── Load Runner Manifest ───
$ManifestPath = Join-Path $RunnersDir "$Tool.json"
if (-not (Test-Path $ManifestPath)) {
$available = (Get-ChildItem -Path $RunnersDir -Filter "*.json" -ErrorAction SilentlyContinue | ForEach-Object { $_.BaseName }) -join ", "
Write-Error "Unknown runner '$Tool'. Available: $available"
exit 1
}
$Manifest = Get-Content -Path $ManifestPath -Raw | ConvertFrom-Json
$RunnerName = $Manifest.name
$RunnerBinary = $Manifest.binary
$RunnerTier = $Manifest.tier
$RunnerPromptDelivery = $Manifest.invocation.promptDelivery
$RunnerPromptFlag = $Manifest.invocation.promptFlag
$RunnerHeadlessFlags = $Manifest.invocation.headlessFlags
$RunnerAutoApproveFlags = $Manifest.invocation.autoApproveFlags
$RunnerStdinPipe = $Manifest.invocation.stdinPipe
$RunnerNative = $Manifest.instructionFile.native
$RunnerFallback = $Manifest.instructionFile.fallbackFrom
$RunnerAutoGenerate = $Manifest.instructionFile.autoGenerate
$RunnerPreambleInjection = $Manifest.signals.preambleInjection
$RunnerHeuristicFallback = $Manifest.signals.heuristicFallback
# Validate binary
if (-not (Get-Command $RunnerBinary -ErrorAction SilentlyContinue)) {
Write-Error "$RunnerBinary not found. Install with: $($Manifest.installHint)"
exit 1
}
# Experimental warning
if ($RunnerTier -eq "experimental" -and -not $NonInteractive) {
Write-Host "`nWARNING: Runner '$RunnerName' is experimental (tier: $RunnerTier)." -ForegroundColor Yellow
Write-Host "Experimental runners may not reliably emit quantum signals." -ForegroundColor Yellow
Write-Host "Press Enter to continue or Ctrl-C to abort..."
Read-Host
}
# Instruction file auto-generation
if ($RunnerFallback -and $RunnerNative -ne $RunnerFallback -and -not (Test-Path $RunnerNative)) {
if (Test-Path $RunnerFallback) {
$marker = "<!-- .ql-generated: Auto-generated from CLAUDE.md by quantum-loop. Do not edit manually. -->"
$content = "$marker`n`n" + (Get-Content -Path $RunnerFallback -Raw)
Set-Content -Path $RunnerNative -Value $content -Encoding UTF8
Write-Host "[RUNNER] Generated $RunnerNative from $RunnerFallback"
}
}
# Build preamble if needed
$PreamblePath = Join-Path $RunnersDir "preamble.md"
$PreambleContent = ""
if ($RunnerPreambleInjection -and (Test-Path $PreamblePath)) {
$PreambleContent = Get-Content -Path $PreamblePath -Raw
}
# Prompt file (for Claude, read CLAUDE.md; for others, read the native instruction file)
$PromptFile = $RunnerNative
if (-not (Test-Path $PromptFile)) {
$PromptFile = Join-Path $ScriptDir $RunnerNative
if (-not (Test-Path $PromptFile)) {
if (Test-Path "CLAUDE.md") { $PromptFile = "CLAUDE.md" }
else { Write-Error "$RunnerNative not found."; exit 1 }
}
}
# ─── Update max retries ───
$jqExpr = ".stories |= map(.retries.maxAttempts = $MaxRetries)"
$tmp = jq $jqExpr quantum.json
$tmp | Set-Content -Path quantum.json -Encoding UTF8 -NoNewline
# ─── Header ───
$Branch = jq -r '.branchName' quantum.json
Write-Host ""
Write-Host "===========================================" -ForegroundColor Cyan
Write-Host " Quantum-Loop Autonomous Development" -ForegroundColor Cyan
Write-Host "===========================================" -ForegroundColor Cyan
Write-Host " Branch: $Branch"
Write-Host " Runner: $RunnerName ($RunnerBinary)"
Write-Host " Tier: $RunnerTier"
Write-Host " Instruction: $RunnerNative"
Write-Host " Mode: Sequential (PowerShell native)"
Write-Host " Max Iter: $MaxIterations"
Write-Host " Max Retries: $MaxRetries"
Write-Host "===========================================" -ForegroundColor Cyan
Write-Host ""
# ─── Summary Table ───
function Show-Summary {
Write-Host ""
Write-Host "Summary" -ForegroundColor Yellow
$stories = jq -r '.stories[] | "\(.id)|\(.title)|\(.status)|\(.retries.attempts)/\(.retries.maxAttempts)"' quantum.json
Write-Host ("{0,-10} {1,-40} {2,-8} {3,-8}" -f "Story", "Title", "Status", "Retries")
Write-Host ("{0,-10} {1,-40} {2,-8} {3,-8}" -f "----------", "----------------------------------------", "--------", "--------")
foreach ($line in $stories) {
$parts = $line -split '\|'
if ($parts.Count -ge 4) {
$title = if ($parts[1].Length -gt 40) { $parts[1].Substring(0, 40) } else { $parts[1] }
Write-Host ("{0,-10} {1,-40} {2,-8} {3,-8}" -f $parts[0], $title, $parts[2], $parts[3])
}
}
$total = (jq '.stories | length' quantum.json)
$passed = (jq '[.stories[] | select(.status == "passed")] | length' quantum.json)
Write-Host ""
Write-Host "Result: $passed/$total stories passed"
}
# ─── Final Verification Sweep ───
function Final-VerificationSweep {
Write-Host "`n[FINAL SWEEP] Running test suite before declaring COMPLETE..." -ForegroundColor Cyan
$testCmd = $null
if (Test-Path "package.json") { $testCmd = "npm test" }
elseif (Test-Path "pyproject.toml") { $testCmd = "python -m pytest -x -q" }
elseif (Test-Path "Cargo.toml") { $testCmd = "cargo test" }
elseif (Test-Path "go.mod") { $testCmd = "go test ./..." }
if ($testCmd) {
try {
Invoke-Expression $testCmd 2>&1 | Out-Null
Write-Host "[FINAL SWEEP] Test suite passed." -ForegroundColor Green
} catch {
Write-Host "[FINAL SWEEP] FAILED: test suite. Cannot declare COMPLETE." -ForegroundColor Red
Show-Summary
exit 1
}
} else {
Write-Host "[FINAL SWEEP] No test suite detected, skipping." -ForegroundColor Yellow
}
# Import smoke test (warning only)
if (Test-Path "package.json") {
$entry = jq -r '.main // empty' package.json 2>$null
if ($entry) {
try {
node -e "require('./$entry')" 2>&1 | Out-Null
Write-Host "[FINAL SWEEP] Import smoke test passed." -ForegroundColor Green
} catch {
Write-Host "[FINAL SWEEP] WARNING: Import smoke test failed (non-blocking)." -ForegroundColor Yellow
}
}
}
}
# ─── Stale Story Detection ───
function Detect-StaleStories {
$staleIds = jq -r --argjson threshold $StaleTimeout '
.stories[] |
select(.status == "in_progress" and .startedAt != null) |
select(((now | floor) - (.startedAt | fromdateiso8601)) > ($threshold * 60)) |
.id
' quantum.json 2>$null
if ($staleIds) {
foreach ($sid in $staleIds) {
if ([string]::IsNullOrWhiteSpace($sid)) { continue }
Write-Host "[STALE] $sid - resetting to failed (exceeded $StaleTimeout minute threshold)" -ForegroundColor Yellow
$tmp = jq --arg id $sid --argjson threshold $StaleTimeout '
.stories |= map(if .id == $id then
.status = (if .retries.attempts + 1 >= .retries.maxAttempts then "blocked" else "failed" end) |
.startedAt = null |
.retries.attempts += 1 |
.retries.failureLog += [{"phase": "stale_detection", "timestamp": (now | todate), "error": ("Story exceeded " + ($threshold | tostring) + " minute stale threshold")}]
else . end)
' quantum.json
$tmp | Set-Content -Path quantum.json -Encoding UTF8 -NoNewline
}
}
}
# ─── Main Loop ───
for ($iteration = 1; $iteration -le $MaxIterations; $iteration++) {
Write-Host "`n=== Iteration $iteration / $MaxIterations ===" -ForegroundColor Green
Write-Host ""
# Detect stale stories before DAG query
Detect-StaleStories
# Select next executable story from DAG
$storyId = 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 ([string]::IsNullOrWhiteSpace($storyId) -or $storyId -eq "null") {
$allPassed = jq '[.stories[].status] | all(. == "passed")' quantum.json
if ($allPassed -eq "true") {
Final-VerificationSweep
Write-Host ""
Write-Host "===========================================" -ForegroundColor Green
Write-Host " COMPLETE - All stories passed!" -ForegroundColor Green
Write-Host "===========================================" -ForegroundColor Green
Show-Summary
exit 0
} else {
Write-Host ""
Write-Host "===========================================" -ForegroundColor Red
Write-Host " BLOCKED - No executable stories remain." -ForegroundColor Red
Write-Host "===========================================" -ForegroundColor Red
Show-Summary
exit 1
}
}
$storyTitle = jq -r --arg id $storyId '.stories[] | select(.id == $id) | .title' quantum.json
$storyAttempt = jq -r --arg id $storyId '.stories[] | select(.id == $id) | .retries.attempts' quantum.json
Write-Host "Story: $storyId - $storyTitle"
Write-Host "Attempt: $([int]$storyAttempt + 1)"
Write-Host ""
# Mark story as in_progress and set startedAt
$now = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
$tmp = jq --arg id $storyId --arg now $now '
.stories |= map(if .id == $id then .status = "in_progress" | .startedAt = $now else . end) |
.updatedAt = (now | todate)
' quantum.json
$tmp | Set-Content -Path quantum.json -Encoding UTF8 -NoNewline
# Build prompt with optional preamble
$agentPrompt = "Implement story $storyId from quantum.json. This is iteration $iteration."
$finalPrompt = $agentPrompt
if ($PreambleContent) {
$finalPrompt = "$PreambleContent`n`n---`n`n$agentPrompt"
}
Write-Host "Spawning $RunnerName for story $storyId..."
# Build and execute runner command based on delivery method
$output = ""
try {
switch ($RunnerPromptDelivery) {
"flag" {
$runnerArgs = @()
foreach ($f in $RunnerHeadlessFlags) { $runnerArgs += $f }
foreach ($f in $RunnerAutoApproveFlags) { $runnerArgs += $f }
if ($SkipPermissions -and $RunnerName -eq "claude") { $runnerArgs += "--dangerously-skip-permissions" }
if ($Model -and $RunnerName -eq "claude") { $runnerArgs += @("--model", $Model) }
$runnerArgs += @($RunnerPromptFlag, $finalPrompt)
$output = & $RunnerBinary @runnerArgs 2>&1 | Out-String
}
"positional" {
$runnerArgs = @()
foreach ($f in $RunnerHeadlessFlags) { $runnerArgs += $f }
foreach ($f in $RunnerAutoApproveFlags) { $runnerArgs += $f }
$runnerArgs += $finalPrompt
$output = & $RunnerBinary @runnerArgs 2>&1 | Out-String
}
"stdin" {
$runnerArgs = @()
foreach ($f in $RunnerHeadlessFlags) { $runnerArgs += $f }
foreach ($f in $RunnerAutoApproveFlags) { $runnerArgs += $f }
$output = $finalPrompt | & $RunnerBinary @runnerArgs 2>&1 | Out-String
}
}
} catch {
Write-Host "$RunnerName process error: $_" -ForegroundColor Red
}
# Process output signals (relaxed whitespace regex + heuristic fallback)
$signalResult = $null
$signalRegex = '<quantum>\s*(STORY_PASSED|STORY_FAILED|COMPLETE|BLOCKED)\s*</quantum>'
$matches_found = [regex]::Matches($output, $signalRegex)
if ($matches_found.Count -gt 0) {
$signalResult = $matches_found[$matches_found.Count - 1].Groups[1].Value # last wins
} elseif ($RunnerHeuristicFallback) {
# Heuristic fallback: check for commit and test patterns
$hasCommit = (git log --oneline -1 2>$null) -match "feat:"
$hasTestPass = $output -match "(0 failures|0 failed|all.*pass|tests? passed)"
$hasErrors = $output -match "(error|FAIL:|failed|exception|panic)"
if ($hasCommit -and $hasTestPass -and -not $hasErrors) { $signalResult = "STORY_PASSED" }
elseif ($hasCommit -and $hasErrors) { $signalResult = "STORY_FAILED" }
elseif ($hasCommit) { $signalResult = "STORY_PASSED" }
else { $signalResult = "STORY_FAILED" }
}
switch ($signalResult) {
"COMPLETE" {
Final-VerificationSweep
Write-Host ""
Write-Host "===========================================" -ForegroundColor Green
Write-Host " COMPLETE - All stories passed!" -ForegroundColor Green
Write-Host "===========================================" -ForegroundColor Green
Show-Summary
exit 0
}
"STORY_PASSED" {
Write-Host "Story $storyId PASSED. Continuing..." -ForegroundColor Green
$tmp = jq --arg id $storyId '.stories |= map(if .id == $id then .startedAt = null else . end)' quantum.json
$tmp | Set-Content -Path quantum.json -Encoding UTF8 -NoNewline
}
"STORY_FAILED" {
Write-Host "Story $storyId FAILED (attempt $([int]$storyAttempt + 1)). Will retry if attempts remain." -ForegroundColor Yellow
$tmp = jq --arg id $storyId '.stories |= map(if .id == $id then .startedAt = null else . end)' quantum.json
$tmp | Set-Content -Path quantum.json -Encoding UTF8 -NoNewline
}
"BLOCKED" {
Write-Host ""
Write-Host "===========================================" -ForegroundColor Red
Write-Host " BLOCKED - Agent reports no executable stories." -ForegroundColor Red
Write-Host "===========================================" -ForegroundColor Red
Show-Summary
exit 1
}
default {
Write-Host "WARNING: No recognized signal. Story may not have completed cleanly." -ForegroundColor Yellow
$lastLines = ($output -split "`n") | Select-Object -Last 10
Write-Host "Last 10 lines:"
$lastLines | ForEach-Object { Write-Host " $_" }
}
}
Start-Sleep -Seconds 2
}
# ─── Generate Observations ───
function Generate-Observations {
$branch = jq -r '.branchName' quantum.json
$dateStr = (Get-Date -Format "yyyy-MM-dd")
$safeBranch = $branch -replace '/', '-'
$obsFile = "docs/post-mortems/$dateStr-$safeBranch-observations.md"
if (-not (Test-Path "docs/post-mortems")) { New-Item -ItemType Directory -Path "docs/post-mortems" -Force | Out-Null }
$total = jq '.stories | length' quantum.json
$passed = jq '[.stories[] | select(.status == "passed")] | length' quantum.json
$failed = jq '[.stories[] | select(.status == "failed")] | length' quantum.json
$blocked = jq '[.stories[] | select(.status == "blocked")] | length' quantum.json
$content = @"
# Execution Observations: $branch
**Date:** $dateStr
**Stories:** $passed passed, $failed failed, $blocked blocked (of $total total)
**Mode:** Sequential (PowerShell)
## Failure Summary
$(jq -r '.stories[] | select(.status == "failed" or .status == "blocked") | "- **\(.id)** \(.title) — \(.status) (\(.retries.attempts)/\(.retries.maxAttempts) retries)"' quantum.json 2>$null)
## Raw Data
<details>
<summary>Progress Log</summary>
``````json
$(jq '.progress' quantum.json)
``````
</details>
"@
$content | Set-Content -Path $obsFile -Encoding UTF8
git add $obsFile 2>$null
git commit -m "docs: execution observations for $branch" 2>$null
Write-Host "[OBSERVATIONS] Generated $obsFile" -ForegroundColor Cyan
# Check if observations contain issues worth reporting
$hasBlocked = [int](jq '[.stories[] | select(.status == "blocked" or .status == "failed")] | length' quantum.json)
if ($hasBlocked -gt 0) {
# Skip if non-interactive (piped input)
if ([Environment]::UserInteractive -eq $false) {
Write-Host "[OBSERVATIONS] Skipping GitHub issue prompt (non-interactive)." -ForegroundColor Yellow
return
}
$response = Read-Host "File observations as GitHub issue on quantum-loop? [y/N]"
if ($response -match '^[Yy]$') {
if (Get-Command "gh" -ErrorAction SilentlyContinue) {
try {
$body = Get-Content $obsFile -Raw
gh issue create --repo andyzengmath/quantum-loop --title "Execution observations: $branch ($dateStr)" --body $body --label "execution-feedback" 2>$null
Write-Host "[OBSERVATIONS] GitHub issue filed." -ForegroundColor Green
} catch {
Write-Host "[OBSERVATIONS] Failed to file GitHub issue. Local doc available." -ForegroundColor Yellow
}
} else {
Write-Host "[OBSERVATIONS] gh CLI not found. Local doc available at $obsFile" -ForegroundColor Yellow
}
}
}
}
Write-Host ""
Write-Host "===========================================" -ForegroundColor Yellow
Write-Host " MAX_ITERATIONS reached ($MaxIterations)." -ForegroundColor Yellow
Write-Host "===========================================" -ForegroundColor Yellow
Show-Summary
Generate-Observations
exit 2