@giamme/forge 0.11.1 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/SKILL.md +7 -3
  3. package/VERSION +1 -1
  4. package/docs/artifacts.md +50 -11
  5. package/docs/commands.md +56 -8
  6. package/docs/decomposed-runs.md +43 -2
  7. package/docs/forge-ui.md +55 -8
  8. package/docs/reviews.md +64 -6
  9. package/package.json +1 -1
  10. package/references/dataset.md +4 -2
  11. package/references/decompose.md +191 -17
  12. package/references/harnesses.md +13 -1
  13. package/references/ui.md +25 -4
  14. package/scripts/forge-contract.py +37 -7
  15. package/scripts/forge-control.py +268 -25
  16. package/scripts/forge-dispatch.sh +90 -3
  17. package/scripts/forge-guard.py +156 -15
  18. package/scripts/forge-issues.py +47 -3
  19. package/scripts/forge-lib-plans.sh +9 -3
  20. package/scripts/forge-lib-review.sh +24 -5
  21. package/scripts/forge-lib-task.sh +53 -5
  22. package/scripts/forge-lib-verify.sh +137 -6
  23. package/scripts/forge-parallel-options.sh +85 -0
  24. package/scripts/forge-parallel.sh +262 -31
  25. package/scripts/forge-ripwire.py +1 -1
  26. package/scripts/forge-route.py +157 -5
  27. package/scripts/forge-runtime.py +49 -1
  28. package/scripts/forge-schedule.py +735 -53
  29. package/scripts/forge-solo.sh +3 -1
  30. package/scripts/forge-sweep.py +542 -0
  31. package/scripts/forge-verify.py +878 -0
  32. package/scripts/forge_dataset/forecast.py +33 -5
  33. package/scripts/forge_dataset/harvest.py +10 -4
  34. package/scripts/forge_verify_reports.py +225 -0
  35. package/ui/README.md +58 -12
  36. package/ui/docs/api.md +277 -28
  37. package/ui/forge_ui/checks.py +331 -0
  38. package/ui/forge_ui/control.py +12 -2
  39. package/ui/forge_ui/export.py +25 -8
  40. package/ui/forge_ui/reader.py +657 -24
  41. package/ui/forge_ui/server.py +29 -0
  42. package/ui/forge_ui/static/app.js +15 -7
  43. package/ui/forge_ui/static/checksview.js +449 -0
  44. package/ui/forge_ui/static/controls.js +217 -33
  45. package/ui/forge_ui/static/convo.js +11 -2
  46. package/ui/forge_ui/static/findingsview.js +6 -4
  47. package/ui/forge_ui/static/gantt.js +14 -9
  48. package/ui/forge_ui/static/index.html +2 -0
  49. package/ui/forge_ui/static/panel.js +44 -1
  50. package/ui/forge_ui/static/runmodel.js +109 -12
  51. package/ui/forge_ui/static/scene3d.js +9 -5
  52. package/ui/forge_ui/static/style.css +113 -0
  53. package/ui/forge_ui/static/verifyview.js +324 -0
  54. package/ui/forge_ui/transcripts.py +225 -54
@@ -10,6 +10,8 @@
10
10
  # [--verify <command>] [--setup <command>] [--dry-run]
11
11
  # [--timeout S] [--retry-failed N] [--infra-retries M]
12
12
  # [--qa-threshold P0|P1|P2|P3] [--verify-retries N] [--no-forecast]
13
+ # [--sweep P0|P1|P2|P3 [--sweep-plausible] [--sweep-max N]]
14
+ # [--sweep-ahead | --no-sweep-ahead]
13
15
  # forge-parallel.sh retry <plan-dir> <task-id> [--dwarf <spec>] [--setup <command>]
14
16
  # [--timeout S] [--qa-threshold Pn] [--verify-retries N]
15
17
  # forge-parallel.sh integrate <plan-dir> --approved [--verify-retries N] [--final-review <qa-spec>]
@@ -18,6 +20,10 @@
18
20
  # forge-parallel.sh split <plan-dir> <new-plan> --tasks <id,id> [--dry-run]
19
21
  # forge-parallel.sh combine <new-plan> <plan-a> <plan-b> [<plan>...] [--verify <command>] [--dry-run]
20
22
  # forge-parallel.sh _task <plan-dir> <task-id> (internal; coordinator target)
23
+ # forge-parallel.sh _qa_only <plan-dir> <task-id> (internal; exit 0 when the next entry only re-runs QA)
24
+ # forge-parallel.sh _waves <plan-dir> (internal; rewrite waves.tsv after a live `forge control add`)
25
+ # forge-parallel.sh _sweep <plan-dir> --severity Pn [--plausible] [--max N]
26
+ # (internal; plan the finding sweep, print its task ids)
21
27
  #
22
28
  # Routing flags (plan):
23
29
  # --dwarf <spec> fallback for any difficulty tier not named below
@@ -47,6 +53,13 @@
47
53
  # --infra-retries M wait out quota/auth/network stops up to M times instead of pausing
48
54
  # --qa-threshold Pn a reviewer FAIL made only of findings less severe than Pn is
49
55
  # accepted as PASS-with-known-issues (P0 worst ... P3 mildest)
56
+ # --sweep Pn once every task has merged, before verification: the open findings
57
+ # at Pn or worse (tolerated, and PLAUSIBLE with --sweep-plausible) become
58
+ # at most --sweep-max N fix tasks (default 5), one per file, each with a
59
+ # narrow review; the sweep plan is printed and kept in sweep/. Once per
60
+ # plan; what it does not fix stays for `forge issues`
61
+ # --sweep-ahead a task is also told the open findings in files it owns, to fix only
62
+ # on its way (kept for the plan; --no-sweep-ahead turns it off)
50
63
  #
51
64
  # Exit codes: 0 ok | 2 usage/validation | 3 precondition (not a git repo, etc.)
52
65
  # 5 some task failed QA or verification failed | 6 integration conflict
@@ -430,8 +443,9 @@ EOF
430
443
  # parallelism: two dwarves editing one file produce a conflict no reviewer can
431
444
  # untangle, so they get separate waves no matter how the goal was decomposed.
432
445
  compute_waves() {
433
- local PLAN="$1" T="$1/tasks.tsv" W="$1/waves.tsv"
434
- : > "$W"; : > "$PLAN/deferrals.txt"
446
+ # Written to temp files and renamed only on success: a cycle leaves the previous files.
447
+ local PLAN="$1" T="$1/tasks.tsv" W="$1/.waves.tsv.$$" D="$1/.deferrals.txt.$$"
448
+ : > "$W"; : > "$D"
435
449
  local remaining done_ids wave n id deps files wave_files ok d progressed
436
450
  remaining="$(all_ids "$T" | tr '\n' ' ')"
437
451
  done_ids=" "; wave=0
@@ -450,7 +464,7 @@ compute_waves() {
450
464
  if [ "$ok" = 1 ] && lists_overlap "$files" "$wave_files"; then
451
465
  ok=0
452
466
  printf 'wave %s: %s deferred (file overlap with a task already in this wave)\n' \
453
- "$wave" "$id" >> "$PLAN/deferrals.txt"
467
+ "$wave" "$id" >> "$D"
454
468
  fi
455
469
  if [ "$ok" = 1 ]; then
456
470
  printf '%s\t%s\n' "$wave" "$id" >> "$W"
@@ -460,10 +474,11 @@ compute_waves() {
460
474
  next_remaining="$next_remaining $id"
461
475
  fi
462
476
  done
463
- [ "$progressed" = 1 ] || die "dependency cycle or unsatisfiable deps among:$next_remaining"
477
+ [ "$progressed" = 1 ] || { rm -f "$W" "$D"; die "dependency cycle or unsatisfiable deps among:$next_remaining"; }
464
478
  for id in $(awk -F'\t' -v w="$wave" '$1==w {print $2}' "$W"); do done_ids="$done_ids$id "; done
465
479
  remaining="$next_remaining"
466
480
  done
481
+ mv -f "$D" "$PLAN/deferrals.txt" && mv -f "$W" "$PLAN/waves.tsv"
467
482
  }
468
483
 
469
484
  render_table() {
@@ -580,6 +595,28 @@ preflight_plan() {
580
595
  fi
581
596
  }
582
597
 
598
+ # The implementer a sweep task gets when the plan has a low tier (or a plain --dwarf pool): checked
599
+ # with the rest, before any model is paid for, since no task may have used it yet.
600
+ sweep_preflight() { # sweep_preflight <plan>
601
+ local spec flags=""
602
+ spec="$(python3 -c 'import json, sys
603
+ try:
604
+ data = json.load(open(sys.argv[1]))
605
+ except Exception:
606
+ sys.exit()
607
+ for tier in ("low", "any"):
608
+ specs = data.get(tier) if isinstance(data, dict) else None
609
+ if isinstance(specs, list) and specs and isinstance(specs[0], str) and specs[0]:
610
+ print(specs[0]); break' "$1/fractal-routing.json" 2>/dev/null)"
611
+ [ -n "$spec" ] || return 0
612
+ [ -f "$1/yolo_dwarf" ] && flags=--yolo
613
+ /bin/bash "$DISPATCH" doctor --spec "$spec" --role dwarf $flags
614
+ }
615
+
616
+ sweep_plan() { # sweep_plan <plan> --severity Pn [--plausible] [--max N]: the scheduler's _sweep
617
+ python3 "$SKILL_DIR/scripts/forge-sweep.py" plan "$@"
618
+ }
619
+
583
620
  write_capsule() { # write_capsule <plan> <id> <role>
584
621
  local PLAN="$1" me="$2" role="$3" T="$1/tasks.tsv"
585
622
  local run_id; run_id="$(cat "$PLAN/run_id")"
@@ -794,6 +831,7 @@ do_task_body() (
794
831
  --run-dir "$tdir" > "$tdir/dwarf.memory"
795
832
  {
796
833
  python3 "$SKILL_DIR/scripts/forge-prompt.py" --requirements "$tdir/prompt.md" --capsule "$tdir/baseline.capsule" --approach "$tdir/approach.md" --retry "$tdir/retry_findings.md" --memory "$tdir/dwarf.memory"
834
+ [ ! -f "$PLAN/sweep_ahead" ] || python3 "$SKILL_DIR/scripts/forge-sweep.py" ahead "$PLAN" "$id" --role dwarf 2>/dev/null
797
835
  echo "Implement this task in the repository. Follow the intended approach unless it is wrong; explain deviations. Run the task's requested verification and report results."
798
836
  echo "On retry, fix the findings in the existing work; preserve parts not criticised. Explain any finding you reject."
799
837
  echo; python3 "$SKILL_DIR/scripts/forge-contract.py" dwarf
@@ -929,13 +967,17 @@ do_task_body() (
929
967
  else
930
968
  rm -f "$tdir/changes.since.diff" "$tdir/changes.since.range" "$tdir/changes.since.stat"
931
969
  fi
970
+ # A finding sweep's fix task gets the narrow review: one FIXED / NOT FIXED line per finding.
971
+ local qa_kind=task
972
+ [ ! -f "$tdir/sweep.json" ] || qa_kind=sweep
932
973
  # QA uses the dispatch-time ownership and baseline, never later merge state.
933
974
  /bin/bash "$MEMORY" inject "$REPO" qa --task "$tdir/prompt.md" \
934
975
  --run-dir "$tdir" > "$tdir/qa.memory"
935
976
  {
936
977
  python3 "$SKILL_DIR/scripts/forge-prompt.py" --requirements "$tdir/prompt.md" --capsule "$tdir/baseline.capsule" --approach "$tdir/approach.md" --retry "$tdir/retry_findings.md" ${prompt_extra[@]+"${prompt_extra[@]}"} --memory "$tdir/dwarf.memory" --memory "$tdir/qa.memory"
937
- python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part head --kind task
978
+ python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part head --kind "$qa_kind"
938
979
  echo
980
+ [ ! -f "$PLAN/sweep_ahead" ] || python3 "$SKILL_DIR/scripts/forge-sweep.py" ahead "$PLAN" "$id" --role qa 2>/dev/null
939
981
  if [ -s "$tdir/failfirst.txt" ]; then cat "$tdir/failfirst.txt"; echo; fi
940
982
  # A subset failure is the one thing a reviewer most wants to know and cannot see
941
983
  # from the diff. It runs in a throwaway export of the reviewed commit, so the task
@@ -967,9 +1009,11 @@ do_task_body() (
967
1009
  echo "matters because another task may own those files."
968
1010
  echo
969
1011
  fi
970
- python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part tail --kind task ${qa_thr:+--threshold "$qa_thr"}
1012
+ python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part tail --kind "$qa_kind" ${qa_thr:+--threshold "$qa_thr"}
971
1013
  echo
972
- echo '```diff'; cat "$tdir/changes.diff"; echo '```'
1014
+ # --irreversible-delete: a deleted file shows as a header, not every line it had (one deleted
1015
+ # data directory once made a 7.1M-character prompt that the reviewer's CLI refused).
1016
+ echo '```diff'; git -C "$wt" diff --binary --irreversible-delete "$base" "$reviewed" -- . ':(exclude).forge'; echo '```'
973
1017
  echo; /bin/bash "$MEMORY" note qa
974
1018
  echo "Place any learning notes before the final FORGE_VERDICT: PASS or FORGE_VERDICT: FAIL line."
975
1019
  } > "$tdir/qa.input"
@@ -1143,6 +1187,8 @@ merge_task() { # merge_task <plan> <id> -> 0 merged, 1 conflicted
1143
1187
  git -C "$int_wt" rev-parse HEAD > "$PLAN/accepted.integration"
1144
1188
  touch "$PLAN/tasks/$id/merged"
1145
1189
  rm -f "$tdir/conflict.files"
1190
+ # Which of a sweep task's findings its reviewer marked fixed: what `forge issues` leaves out.
1191
+ [ ! -f "$tdir/sweep.json" ] || python3 "$SKILL_DIR/scripts/forge-sweep.py" settle "$PLAN" "$id" >/dev/null 2>&1 || true
1146
1192
  forge_dataset_event merge --dir "$PLAN" --task "$id" --set status=MERGED --set commit="$reviewed" \
1147
1193
  --set integration="$(cat "$PLAN/accepted.integration")"
1148
1194
  return 0
@@ -1171,7 +1217,7 @@ do_retry() {
1171
1217
  --dwarf) NEWDWARF="${2:?--dwarf needs a spec}"; shift 2 ;;
1172
1218
  --yolo-dwarf) touch "$PLAN/yolo_dwarf"; shift ;;
1173
1219
  --yolo-qa) touch "$PLAN/yolo_qa"; shift ;;
1174
- --verify) printf '%s' "${2:?--verify needs a command}" > "$PLAN/verify_cmd"; shift 2 ;;
1220
+ --verify) printf '%s' "${2:?--verify needs a command}" > "$PLAN/verify_cmd"; rm -rf "$PLAN/verify.d"; PP_LANES_FRESH=0; shift 2 ;;
1175
1221
  --setup) printf '%s' "${2:?--setup needs a command}" > "$PLAN/setup_cmd"; shift 2 ;;
1176
1222
  *)
1177
1223
  # `case` is not a loop, so `continue` here belongs to the enclosing while.
@@ -1210,6 +1256,9 @@ do_retry() {
1210
1256
  fi
1211
1257
  [ "${FORGE_RIPWIRE:-}" != off ] || touch "$PLAN/no_ripwire"
1212
1258
  dependencies_ready "$PLAN" "$id" || { write_results "$PLAN"; return 5; }
1259
+ local vrepo vcheckout; vrepo="$(cat "$PLAN/repo" 2>/dev/null)"; vcheckout="$(cat "$PLAN/wt_root")/_integration"
1260
+ [ -d "$vcheckout" ] || vcheckout="$vrepo"
1261
+ verify_lanes_check "$PLAN" "$vrepo" "$vcheckout"
1213
1262
  [ ! -f "$PLAN/no_ripwire" ] || export FORGE_RIPWIRE=off
1214
1263
  preflight_plan "$PLAN" || return $?
1215
1264
  /bin/bash "$SKILL_DIR/scripts/forge-install-ripwire.sh" || true
@@ -1286,14 +1335,104 @@ verify_run_note() { # verify_run_note <out> <kind> <rc> <before> <after> <log> <
1286
1335
  "$(( $(date +%s) - $7 ))" "verification.run-$n.log" >> "$out/verification.runs.tsv"
1287
1336
  }
1288
1337
 
1289
- verify_result() ( # plan, checkout, artifact directory
1338
+ # --- stoppable verification --------------------------------------------------
1339
+ # `run` sets FORGE_RUN_STOPPABLE for the verification it supervises. Then the verify command
1340
+ # runs as its own process group, and a TERM to the verification subshell stops that whole
1341
+ # group: TERM at once, KILL after a grace period. `retry` and `integrate` leave it unset and
1342
+ # keep the plain foreground command.
1343
+ verify_group_stop() { # TERM the verify command's group; KILL it after FORGE_VERIFY_GRACE seconds
1344
+ [ -n "$FORGE_VERIFY_GROUP" ] && [ -z "$FORGE_VERIFY_TIMER" ] || return 0
1345
+ local group="$FORGE_VERIFY_GROUP" termed=1
1346
+ # Until the guard's setsid there is no group: the TERM goes to the guard itself, which dies
1347
+ # of it before it starts the command. A group that forms all the same gets its TERM from
1348
+ # the timer.
1349
+ kill -TERM -- "-$group" 2>/dev/null || { termed=0; kill -TERM "$group" 2>/dev/null; }
1350
+ # The timer polls in short naps rather than sleeping the whole grace in one: it ends by
1351
+ # itself within a nap of the group's last member, and a long sleep left behind would hold
1352
+ # the plan lock it inherited.
1353
+ ( trap - TERM INT
1354
+ ticks=$((FORGE_VERIFY_GRACE * 10))
1355
+ while kill -0 -- "-$group" 2>/dev/null || { [ "$termed" = 0 ] && kill -0 "$group" 2>/dev/null; }; do
1356
+ if [ "$termed" = 0 ] && kill -TERM -- "-$group" 2>/dev/null; then termed=1; fi
1357
+ if [ "$ticks" -le 0 ]; then
1358
+ if [ "$termed" = 1 ]; then kill -KILL -- "-$group" 2>/dev/null; else kill -KILL "$group" 2>/dev/null; fi
1359
+ break
1360
+ fi
1361
+ sleep 0.1
1362
+ ticks=$((ticks - 1))
1363
+ done ) &
1364
+ FORGE_VERIFY_TIMER=$!
1365
+ }
1366
+
1367
+ verify_signal() { # trap handler of a stoppable verification
1368
+ FORGE_VERIFY_STOP=1
1369
+ verify_group_stop
1370
+ }
1371
+
1372
+ verify_exec() { # verify_exec <checkout> <command> <log> [grace seconds, default 5] -> the command's exit code, 130 when stopped
1373
+ local wt="$1" cmd="$2" log="$3" rc=0 group deadline
1374
+ if [ "${FORGE_RUN_STOPPABLE:-0}" != 1 ]; then
1375
+ (cd "$wt" && /bin/bash -c "$cmd") > "$log" 2>&1
1376
+ return $?
1377
+ fi
1378
+ FORGE_VERIFY_GRACE="${4:-5}"
1379
+ [ "$FORGE_VERIFY_STOP" = 1 ] && return 130
1380
+ # A background job ignores SIGINT and SIGQUIT, and the command would inherit that; the
1381
+ # wrapper restores both before the guard puts the command in a session of its own.
1382
+ (cd "$wt" && exec python3 -c 'import os, signal, sys
1383
+ for name in (signal.SIGINT, signal.SIGQUIT):
1384
+ signal.signal(name, signal.SIG_DFL)
1385
+ os.execv(sys.executable, [sys.executable] + sys.argv[1:])' \
1386
+ "$SKILL_DIR/scripts/forge-guard.py" session -- /bin/bash -c "$cmd") > "$log" 2>&1 <&0 &
1387
+ group=$!
1388
+ FORGE_VERIFY_GROUP="$group"
1389
+ [ "$FORGE_VERIFY_STOP" = 1 ] && verify_group_stop
1390
+ # `wait` returns at once when a trapped signal arrives, so wait again until the job is gone.
1391
+ wait "$group" || rc=$?
1392
+ while kill -0 "$group" 2>/dev/null; do rc=0; wait "$group" || rc=$?; done
1393
+ if [ "$FORGE_VERIFY_STOP" = 1 ]; then
1394
+ # Members that outlived the leader hold the plan lock; the KILL timer ends them.
1395
+ deadline=$((SECONDS + FORGE_VERIFY_GRACE + 5))
1396
+ while kill -0 -- "-$group" 2>/dev/null && [ "$SECONDS" -lt "$deadline" ]; do sleep 0.1; done
1397
+ fi
1398
+ FORGE_VERIFY_GROUP=""
1399
+ if [ -n "$FORGE_VERIFY_TIMER" ]; then
1400
+ wait "$FORGE_VERIFY_TIMER" 2>/dev/null
1401
+ FORGE_VERIFY_TIMER=""
1402
+ fi
1403
+ [ "$FORGE_VERIFY_STOP" = 1 ] && return 130
1404
+ return "$rc"
1405
+ }
1406
+
1407
+ verify_mark_interrupted() { # verify_mark_interrupted <out>
1408
+ echo INTERRUPTED > "$1/verification.status"
1409
+ echo 130 > "$1/verification.exit"
1410
+ note "verification interrupted; the verify command was stopped and is not rerun"
1411
+ }
1412
+
1413
+ # True, once the verification is marked INTERRUPTED, when a stop has arrived. Checked before
1414
+ # each run of the command (a stop that came first runs nothing and records no run) and after.
1415
+ verify_stopped() { # verify_stopped <out>
1416
+ [ "$FORGE_VERIFY_STOP" = 1 ] || return 1
1417
+ verify_mark_interrupted "$1"
1418
+ }
1419
+
1420
+ verify_result() ( verify_result_body "$@" ) # plan, checkout, artifact directory
1421
+
1422
+ # The body is a function of its own so `run` can start it as `( verify_result_body ... ) &`:
1423
+ # that one subshell is the background job, the home of the signal handler above and of
1424
+ # forge_metric_begin's EXIT trap.
1425
+ verify_result_body() { # plan, checkout, artifact directory
1290
1426
  local plan="$1" wt="$2" out="$3" cmd="" before after rc=0 repo jev_loaded=0 vretries
1427
+ FORGE_VERIFY_STOP=0 FORGE_VERIFY_GROUP="" FORGE_VERIFY_TIMER="" FORGE_VERIFY_GRACE=5
1428
+ [ "${FORGE_RUN_STOPPABLE:-0}" = 1 ] && trap verify_signal TERM INT
1291
1429
  mkdir -p "$out"
1292
1430
  forge_metric_begin "$out" verification
1293
1431
  forge_metric_phase verification
1294
1432
  repo="$(cat "$plan/repo")"
1295
1433
  rm -f "$out/verification.flaky" "$out/verification.retry.log" "$out/verification.coverage.txt" \
1296
- "$out/verification.runs.tsv" "$out"/verification.run-*.log "$out/verification.commit"
1434
+ "$out/verification.runs.tsv" "$out"/verification.run-*.log "$out/verification.commit" \
1435
+ "$out/verification.lanes.json" "$out"/verification.lane.*.log
1297
1436
  # The commit being verified, named before any early return, so every outcome (FAIL and
1298
1437
  # UNVERIFIED included) can be joined to the code it judged.
1299
1438
  git -C "$wt" rev-parse HEAD > "$out/verification.commit" 2>/dev/null || rm -f "$out/verification.commit"
@@ -1309,12 +1448,20 @@ verify_result() ( # plan, checkout, artifact directory
1309
1448
  if [ -f "$SKILL_DIR/scripts/forge-jev-options.sh" ]; then
1310
1449
  source "$SKILL_DIR/scripts/forge-jev-options.sh" 2>/dev/null && jev_loaded=1
1311
1450
  fi
1312
- if [ -s "$plan/verify_cmd" ]; then cmd="$(cat "$plan/verify_cmd")"
1313
- elif [ -s "$repo/.forge/verify" ]; then cmd="$(cat "$repo/.forge/verify")"; fi
1451
+ local vsource; vsource="$(verify_source_for "$plan" "$repo")"
1452
+ case "$vsource" in command:*) cmd="$(cat "${vsource#*$'\t'}")" ;; esac
1314
1453
  if [ "$(forge_tree "$wt")" != "$(git -C "$wt" rev-parse HEAD^{tree})" ]; then
1315
1454
  echo FAIL > "$out/verification.status"
1316
1455
  note "verification checkout has source changes outside its commit"; return 1
1317
1456
  fi
1457
+ case "$vsource" in
1458
+ lanes:*)
1459
+ if [ "$jev_loaded" = 1 ] && forge_jev_active tests; then
1460
+ note "jev: flake triage applies to a single verify command; lanes rerun their own failures"
1461
+ fi
1462
+ verify_lanes_run "$plan" "$wt" "$out" "${vsource#*$'\t'}" "$vretries"
1463
+ return $? ;;
1464
+ esac
1318
1465
  # Only when nothing is configured: ask Jev to name a command that actually exists
1319
1466
  # in the repo. Its exit code, not its stdout text, decides whether a judgment was
1320
1467
  # produced — any non-zero exit (no key, disabled, nothing suitable, a traceback)
@@ -1363,10 +1510,12 @@ with open(out + "/verification.jev.json", "w") as fh:
1363
1510
  printf '%s\n' "$cmd" > "$out/verification.command"
1364
1511
  before="$(forge_fingerprint "$wt")" || return 1
1365
1512
  local vt0; vt0="$(date +%s)"
1366
- (cd "$wt" && /bin/bash -c "$cmd") > "$out/verification.log" 2>&1 || rc=$?
1367
- echo "$rc" > "$out/verification.exit"
1368
- after="$(forge_fingerprint "$wt")" || return 1
1513
+ verify_stopped "$out" && return 130
1514
+ verify_exec "$wt" "$cmd" "$out/verification.log" || rc=$?
1515
+ [ "$FORGE_VERIFY_STOP" = 1 ] || echo "$rc" > "$out/verification.exit"
1516
+ after="$(forge_fingerprint "$wt")" || { verify_stopped "$out" && return 130; return 1; }
1369
1517
  verify_run_note "$out" initial "$rc" "$before" "$after" verification.log "$vt0"
1518
+ verify_stopped "$out" && return 130
1370
1519
  echo "$before" > "$out/verification.fingerprint"
1371
1520
  if [ "$rc" != 0 ] || [ "$before" != "$after" ]; then
1372
1521
  local flaked=0 det_retried=0 n_retry=0 rc3 b3 a3
@@ -1379,9 +1528,11 @@ with open(out + "/verification.jev.json", "w") as fh:
1379
1528
  note "verification failed (exit $rc); rerunning to tell a flaky lane from a regression ($n_retry/$vretries)"
1380
1529
  b3="$(forge_fingerprint "$wt")" || return 1
1381
1530
  vt0="$(date +%s)"
1382
- (cd "$wt" && /bin/bash -c "$cmd") > "$out/verification.retry.log" 2>&1 || rc3=$?
1383
- a3="$(forge_fingerprint "$wt")" || return 1
1531
+ verify_stopped "$out" && return 130
1532
+ verify_exec "$wt" "$cmd" "$out/verification.retry.log" || rc3=$?
1533
+ a3="$(forge_fingerprint "$wt")" || { verify_stopped "$out" && return 130; return 1; }
1384
1534
  verify_run_note "$out" rerun "$rc3" "$b3" "$a3" verification.retry.log "$vt0"
1535
+ verify_stopped "$out" && return 130
1385
1536
  if [ "$rc3" = 0 ] && [ "$b3" = "$a3" ]; then
1386
1537
  flaked=1
1387
1538
  {
@@ -1446,10 +1597,12 @@ print(v if v is not None else "0.90")
1446
1597
  note "jev: known_flake (confidence $confidence >= $flake_act), corroborated by a recorded trap — retrying once"
1447
1598
  before2="$(forge_fingerprint "$wt")" || return 1
1448
1599
  vt0="$(date +%s)"
1449
- (cd "$wt" && /bin/bash -c "$cmd") > "$out/verification.retry.log" 2>&1 || rc2=$?
1600
+ verify_stopped "$out" && return 130
1601
+ verify_exec "$wt" "$cmd" "$out/verification.retry.log" || rc2=$?
1450
1602
  rc2="${rc2:-0}"
1451
- after2="$(forge_fingerprint "$wt")" || return 1
1603
+ after2="$(forge_fingerprint "$wt")" || { verify_stopped "$out" && return 130; return 1; }
1452
1604
  verify_run_note "$out" jev_retry "$rc2" "$before2" "$after2" verification.retry.log "$vt0"
1605
+ verify_stopped "$out" && return 130
1453
1606
  if [ "$rc2" = 0 ] && [ "$before2" = "$after2" ]; then
1454
1607
  flaked=1
1455
1608
  note "jev: retry passed — treating as a corroborated flake, not a regression"
@@ -1490,7 +1643,7 @@ with open(path, "w") as fh:
1490
1643
  return 1
1491
1644
  fi
1492
1645
  echo PASS > "$out/verification.status"
1493
- )
1646
+ }
1494
1647
 
1495
1648
  write_results() { # write_results <plan>
1496
1649
  local PLAN="$1" T="$1/tasks.tsv" run_id id
@@ -1511,7 +1664,29 @@ forecast_report() {
1511
1664
  }
1512
1665
 
1513
1666
  # --- run ----------------------------------------------------------------------
1514
- do_run() (
1667
+ # Waits for a background job and, when a trapped signal ends the wait early, waits again
1668
+ # until the job is gone; its exit code is left in run_rc.
1669
+ run_wait() { # run_wait <pid>
1670
+ run_rc=0
1671
+ wait "$1" || run_rc=$?
1672
+ while kill -0 "$1" 2>/dev/null; do run_rc=0; wait "$1" || run_rc=$?; done
1673
+ }
1674
+
1675
+ # The handler of do_run's subshell: a signal reaches the job in progress (the scheduler,
1676
+ # then the verification) as TERM. Before any job exists there is nothing to clean up.
1677
+ run_signal() {
1678
+ run_stop=1
1679
+ if [ -n "$run_child" ]; then kill -TERM "$run_child" 2>/dev/null
1680
+ elif [ "$run_phase" = pre ]; then exit 130; fi
1681
+ }
1682
+
1683
+ # Not a ( ) function: main runs it as `( do_run ... ) &`, one subshell that is both the
1684
+ # background job and the home of its signal handler. A function called with `&` would be a
1685
+ # second subshell level, and an async function does not run its EXIT trap.
1686
+ do_run() {
1687
+ local run_child="" run_stop=0 run_phase=pre run_rc=0 interrupted=0 vstopped=0
1688
+ FORGE_RUN_STOPPABLE=1
1689
+ trap run_signal TERM INT
1515
1690
  local PLAN="${1:?run needs a plan dir}"; shift
1516
1691
  local fractal_pipeline_args=("$@")
1517
1692
  local MAXP=3 DRY=0 jev_rc=0 FORECAST=1
@@ -1527,7 +1702,7 @@ do_run() (
1527
1702
  --max-parallel) MAXP="${2:?}"; shift 2 ;;
1528
1703
  --yolo-dwarf) touch "$PLAN/yolo_dwarf"; shift ;;
1529
1704
  --yolo-qa) touch "$PLAN/yolo_qa"; shift ;;
1530
- --verify) printf '%s' "${2:?--verify needs a command}" > "$PLAN/verify_cmd"; shift 2 ;;
1705
+ --verify) printf '%s' "${2:?--verify needs a command}" > "$PLAN/verify_cmd"; rm -rf "$PLAN/verify.d"; PP_LANES_FRESH=0; shift 2 ;;
1531
1706
  --setup) printf '%s' "${2:?--setup needs a command}" > "$PLAN/setup_cmd"; shift 2 ;;
1532
1707
  --dry-run) DRY=1; shift ;;
1533
1708
  *)
@@ -1545,6 +1720,7 @@ do_run() (
1545
1720
  esac
1546
1721
  done
1547
1722
  [ "$JEV_OPTS" = 1 ] && forge_jev_settle "$PLAN"
1723
+ [ -n "$PP_SWEEP" ] || [ "$PP_SWEEP_PLAUSIBLE$PP_SWEEP_MAX_SET" = 00 ] || die "--sweep-plausible and --sweep-max go with --sweep"
1548
1724
  case "$OUTPUT" in summary|full) ;; *) die "--output must be summary or full" ;; esac
1549
1725
  export FORGE_OUTPUT="$OUTPUT"
1550
1726
  case "$MAXP" in ''|*[!0-9]*) die "--max-parallel must be positive" ;; esac
@@ -1590,6 +1766,7 @@ do_run() (
1590
1766
  [ "${FORGE_RIPWIRE:-}" != off ] || touch "$PLAN/no_ripwire"
1591
1767
  [ ! -f "$PLAN/no_ripwire" ] || export FORGE_RIPWIRE=off
1592
1768
  preflight_plan "$PLAN" || return $?
1769
+ [ -z "$PP_SWEEP" ] || sweep_preflight "$PLAN" || return $?
1593
1770
  /bin/bash "$SKILL_DIR/scripts/forge-install-ripwire.sh" || true
1594
1771
  forge_metric_phase preparation
1595
1772
  mkdir -p "$wt_root"
@@ -1623,22 +1800,48 @@ do_run() (
1623
1800
  # Also before any model is paid for: what this plan is likely to cost, and which tasks
1624
1801
  # history says may bounce. Advisory, budgeted and silent without history; it never stops a run.
1625
1802
  [ "$FORECAST" = 1 ] && forecast_report "$PLAN"
1803
+ # Invalid verification lanes stop the run here, before any model is paid for.
1804
+ verify_lanes_check "$PLAN" "$REPO" "$int_wt"
1626
1805
  # Say now, before any model is paid for, which suites the verify command will not run.
1627
1806
  verify_coverage_report "$REPO" "$(verify_command_for "$PLAN" "$REPO")" "$PLAN/verification.coverage.txt"
1628
1807
  local sched_rc=0 paused=0 sched_args=()
1629
1808
  [ "$PP_RETRY_FAILED" -gt 0 ] && sched_args+=(--retry-failed "$PP_RETRY_FAILED")
1630
1809
  [ "$PP_INFRA_RETRIES" -gt 0 ] && sched_args+=(--infra-retries "$PP_INFRA_RETRIES")
1810
+ [ -n "$PP_SWEEP" ] && sched_args+=(--sweep "$PP_SWEEP" --sweep-max "$PP_SWEEP_MAX")
1811
+ [ "$PP_SWEEP_PLAUSIBLE" = 1 ] && sched_args+=(--sweep-plausible)
1631
1812
  forge_metric_phase dispatch
1632
- python3 "$SKILL_DIR/scripts/forge-schedule.py" "$SELF" "$PLAN" "$MAXP" ${sched_args[@]+"${sched_args[@]}"} || sched_rc=$?
1813
+ # In the background so a signal to this shell ends the wait; the scheduler gets it as TERM,
1814
+ # stops its tasks and returns 130.
1815
+ run_phase=scheduler
1816
+ python3 "$SKILL_DIR/scripts/forge-schedule.py" "$SELF" "$PLAN" "$MAXP" ${sched_args[@]+"${sched_args[@]}"} <&0 &
1817
+ run_child=$!
1818
+ [ "$run_stop" = 1 ] && kill -TERM "$run_child" 2>/dev/null
1819
+ run_wait "$run_child"; sched_rc=$run_rc
1820
+ run_child=""
1633
1821
  [ "$sched_rc" != 0 ] && failed=1
1634
1822
  [ "$sched_rc" = 8 ] && paused=1
1635
1823
 
1636
1824
  forge_metric_phase verification
1825
+ # Counted after that phase call: a signal that arrived while it ran is handled only once it
1826
+ # returned, and must still keep verification from starting.
1827
+ [ "$sched_rc" = 130 ] || [ "$run_stop" = 1 ] && interrupted=1
1637
1828
  # An infrastructure pause leaves the run unfinished, so there is nothing yet worth
1638
- # verifying; the next `run` resumes and verifies at its end.
1639
- if [ "$paused" = 0 ]; then
1640
- verify_result "$PLAN" "$int_wt" "$PLAN" || failed=1
1829
+ # verifying; the next `run` resumes and verifies at its end. An interrupted run has
1830
+ # merged only part of its work: a verification of that is not worth its minutes, and it
1831
+ # would run again as the rerun if it were killed.
1832
+ if [ "$paused" = 0 ] && [ "$interrupted" = 0 ]; then
1833
+ run_phase=verification
1834
+ ( verify_result_body "$PLAN" "$int_wt" "$PLAN" ) <&0 &
1835
+ run_child=$!
1836
+ [ "$run_stop" = 1 ] && kill -TERM "$run_child" 2>/dev/null
1837
+ run_wait "$run_child"
1838
+ [ "$run_rc" != 0 ] && failed=1
1839
+ [ "$run_rc" = 130 ] && vstopped=1
1840
+ run_child=""
1841
+ # A stop that killed the subshell before its handler was set leaves no record: it counts too.
1842
+ [ "$vstopped" = 1 ] || [ "$run_stop" = 1 ] && interrupted=1
1641
1843
  fi
1844
+ run_phase=results
1642
1845
  write_results "$PLAN"
1643
1846
  # Only prune worktrees for work that is safely merged, and only after the record
1644
1847
  # of it has been written and read back. A failed task keeps its worktree and
@@ -1670,15 +1873,27 @@ do_run() (
1670
1873
  done
1671
1874
  [ "$drift" = 1 ] && echo
1672
1875
 
1876
+ # From this run's own flags, never from a verification.status an earlier run left.
1877
+ if [ "$interrupted" = 1 ]; then
1878
+ if [ "$vstopped" = 1 ]; then echo "interrupted during verification, which was stopped and not rerun."
1879
+ else echo "interrupted; verification was skipped."; fi
1880
+ fi
1881
+ local stopped_ids; stopped_ids="$(awk -F'\t' '$2=="INTERRUPTED" {print $1}' "$PLAN/results.tsv")"
1882
+ if [ -n "$stopped_ids" ]; then
1883
+ echo "A later run does not dispatch an interrupted task; continue each one with:"
1884
+ for id in $stopped_ids; do echo " forge-parallel.sh retry $PLAN $id"; done
1885
+ fi
1886
+ [ "$interrupted" = 1 ] || [ -n "$stopped_ids" ] && echo
1673
1887
  # The retry hint is for tasks that actually failed; a run that only failed verification,
1674
- # or only paused on infrastructure, has nothing to retry task by task.
1675
- if [ "$(awk -F'\t' '$2!="MERGED" && $2!="INFRA" && $2!="PENDING" && $2!="BLOCKED" {n++} END {print n+0}' "$PLAN/results.tsv")" -gt 0 ]; then
1888
+ # or only paused on infrastructure, has nothing to retry task by task. An interrupted task
1889
+ # has its own hint above.
1890
+ if [ "$(awk -F'\t' '$2!="MERGED" && $2!="INFRA" && $2!="PENDING" && $2!="BLOCKED" && $2!="INTERRUPTED" {n++} END {print n+0}' "$PLAN/results.tsv")" -gt 0 ]; then
1676
1891
  echo "retry a failed task with its reviewer's findings:"
1677
1892
  echo " forge-parallel.sh retry $PLAN <task-id> [--dwarf <spec>]"
1678
1893
  echo "or let a run retry them itself: forge-parallel.sh run $PLAN --retry-failed 1"
1679
1894
  echo
1680
1895
  fi
1681
- if [ "$failed" = 1 ] && [ "$paused" = 0 ] && [ "$(cat "$PLAN/verification.status" 2>/dev/null)" = FAIL ]; then
1896
+ if [ "$failed" = 1 ] && [ "$paused" = 0 ] && [ "$interrupted" = 0 ] && [ "$(cat "$PLAN/verification.status" 2>/dev/null)" = FAIL ]; then
1682
1897
  echo "combined verification failed: see $PLAN/verification.log"
1683
1898
  echo
1684
1899
  fi
@@ -1695,10 +1910,11 @@ do_run() (
1695
1910
  else
1696
1911
  echo "Your branch and working tree were not touched."
1697
1912
  fi
1913
+ [ "$interrupted" = 1 ] && return 130
1698
1914
  [ "$paused" = 1 ] && return 8
1699
1915
  [ "$failed" = 1 ] && return 5
1700
1916
  return 0
1701
- )
1917
+ }
1702
1918
 
1703
1919
  # --- integrate ----------------------------------------------------------------
1704
1920
  do_integrate() {
@@ -1789,7 +2005,19 @@ esac
1789
2005
  need_fn() { declare -F "$1" >/dev/null || die "'$CMD' is not available: $1 is missing" 3; }
1790
2006
  case "$CMD" in
1791
2007
  plan) do_plan "$@"; rc=$?; forge_update_notice; exit "$rc" ;;
1792
- run) do_run "$@"; rc=$?; forge_update_notice; exit "$rc" ;;
2008
+ run)
2009
+ # The signal handler lives here, at the pid the user started: do_run is a subshell, and
2010
+ # bash runs a trap only between commands, so it is started in the background and waited
2011
+ # for. A signal goes to it as TERM; do_run stops the scheduler or the verification.
2012
+ run_signalled=0 run_job=""
2013
+ trap 'run_signalled=1; [ -n "$run_job" ] && kill -TERM "$run_job" 2>/dev/null' TERM INT
2014
+ ( do_run "$@" ) <&0 &
2015
+ run_job=$!
2016
+ [ "$run_signalled" = 1 ] && kill -TERM "$run_job" 2>/dev/null
2017
+ run_wait "$run_job"; rc=$run_rc
2018
+ trap - TERM INT
2019
+ [ "$run_signalled" = 1 ] && [ "$rc" -gt 128 ] && rc=130
2020
+ forge_update_notice; exit "$rc" ;;
1793
2021
  retry) do_retry "$@" ;;
1794
2022
  integrate) do_integrate "$@" ;;
1795
2023
  accept) need_fn do_accept; do_accept "$@" ;;
@@ -1799,5 +2027,8 @@ case "$CMD" in
1799
2027
  _task) do_task "$@" ;;
1800
2028
  _merge) merge_task "$@" ;;
1801
2029
  _prepare_retry) retry_for_scheduler "$@" ;;
2030
+ _qa_only) qa_only_next "$@" ;;
2031
+ _waves) compute_waves "$@" ;;
2032
+ _sweep) sweep_plan "$@" ;;
1802
2033
  *) die "unknown subcommand '$CMD'" ;;
1803
2034
  esac
@@ -52,7 +52,7 @@ def bounded(command, cwd, deadline, limit=16384):
52
52
  # Also kill descendants that closed their pipes before the parent exited.
53
53
  try:
54
54
  os.killpg(process.pid, signal.SIGKILL)
55
- except ProcessLookupError:
55
+ except (ProcessLookupError, PermissionError): # macOS: EPERM for a group of zombies only
56
56
  pass
57
57
  process.wait()
58
58
  streams.close()