@giamme/forge 0.12.1 → 0.13.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 (111) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +1 -1
  3. package/SKILL.md +29 -12
  4. package/VERSION +1 -1
  5. package/docs/add-ons.md +11 -6
  6. package/docs/artifacts.md +12 -6
  7. package/docs/commands.md +179 -17
  8. package/docs/decomposed-runs.md +13 -5
  9. package/docs/forge-ui.md +14 -4
  10. package/docs/memory.md +2 -1
  11. package/docs/models.md +4 -2
  12. package/docs/reviews.md +49 -20
  13. package/docs/troubleshooting.md +3 -1
  14. package/forge +17 -1
  15. package/package.json +1 -1
  16. package/references/dataset.md +39 -13
  17. package/references/decision-models.md +763 -0
  18. package/references/decompose.md +166 -29
  19. package/references/harnesses.md +10 -3
  20. package/references/planning.md +8 -0
  21. package/references/solo.md +13 -3
  22. package/scripts/forge-artifact.sh +30 -4
  23. package/scripts/forge-config.py +238 -29
  24. package/scripts/forge-contract.py +95 -56
  25. package/scripts/forge-control.py +40 -9
  26. package/scripts/forge-decide.py +6 -0
  27. package/scripts/forge-dispatch.sh +38 -16
  28. package/scripts/forge-fail-first.py +2 -2
  29. package/scripts/forge-gate-addressed.sh +40 -0
  30. package/scripts/forge-gate-message.sh +64 -0
  31. package/scripts/forge-issues.py +321 -33
  32. package/scripts/forge-jev-options.sh +269 -42
  33. package/scripts/forge-lib-attribute.sh +447 -0
  34. package/scripts/forge-lib-gates.sh +32 -0
  35. package/scripts/forge-lib-plans.sh +22 -6
  36. package/scripts/forge-lib-review.sh +22 -17
  37. package/scripts/forge-lib-task.sh +217 -38
  38. package/scripts/forge-lib-verify.sh +45 -8
  39. package/scripts/forge-memory.sh +3 -3
  40. package/scripts/forge-parallel-options.sh +18 -5
  41. package/scripts/forge-parallel.sh +457 -303
  42. package/scripts/forge-plan-check.py +35 -1
  43. package/scripts/forge-pre-review.py +437 -0
  44. package/scripts/forge-prompt.py +25 -5
  45. package/scripts/forge-route.py +2 -2
  46. package/scripts/forge-runtime.py +10 -4
  47. package/scripts/forge-schedule.py +21 -5
  48. package/scripts/forge-shadow.py +2 -2
  49. package/scripts/forge-solo.sh +39 -9
  50. package/scripts/forge-submodules.py +389 -0
  51. package/scripts/forge-ui-options.sh +25 -0
  52. package/scripts/forge_dataset/cli.py +12 -6
  53. package/scripts/forge_dataset/export.py +140 -84
  54. package/scripts/forge_dataset/harvest.py +33 -5
  55. package/scripts/{forge_jev/outcome.py → forge_dataset/states.py} +21 -7
  56. package/scripts/forge_decide/__init__.py +141 -0
  57. package/scripts/forge_decide/budget.py +180 -0
  58. package/scripts/forge_decide/cache.py +66 -0
  59. package/scripts/forge_decide/cli.py +953 -0
  60. package/scripts/forge_decide/doctor.py +270 -0
  61. package/scripts/forge_decide/label_addressed.py +75 -0
  62. package/scripts/forge_decide/label_attribution.py +62 -0
  63. package/scripts/forge_decide/label_coverage.py +81 -0
  64. package/scripts/forge_decide/label_message.py +38 -0
  65. package/scripts/forge_decide/label_prompt.py +139 -0
  66. package/scripts/forge_decide/labels.py +328 -0
  67. package/scripts/forge_decide/metrics.py +125 -0
  68. package/scripts/forge_decide/migrate.py +226 -0
  69. package/scripts/forge_decide/providers.py +292 -0
  70. package/scripts/forge_decide/replay.py +350 -0
  71. package/scripts/forge_decide/report.py +382 -0
  72. package/scripts/forge_decide/resolve.py +397 -0
  73. package/scripts/forge_decide/sites.py +377 -0
  74. package/scripts/forge_jev/__init__.py +202 -104
  75. package/scripts/forge_jev/addressed.py +171 -0
  76. package/scripts/forge_jev/attribution.py +198 -0
  77. package/scripts/forge_jev/cli.py +460 -382
  78. package/scripts/forge_jev/client.py +155 -77
  79. package/scripts/forge_jev/coupling.py +19 -87
  80. package/scripts/forge_jev/coverage.py +303 -0
  81. package/scripts/forge_jev/findings.py +219 -0
  82. package/scripts/forge_jev/gates.py +225 -27
  83. package/scripts/forge_jev/memory.py +65 -4
  84. package/scripts/forge_jev/message.py +134 -0
  85. package/scripts/forge_jev/plan_gates.py +294 -0
  86. package/scripts/forge_jev/questions.py +173 -313
  87. package/scripts/forge_jev/verify.py +130 -104
  88. package/ui/README.md +2 -2
  89. package/ui/docs/api.md +92 -39
  90. package/ui/forge_ui/control.py +71 -14
  91. package/ui/forge_ui/export.py +2 -1
  92. package/ui/forge_ui/models.py +17 -16
  93. package/ui/forge_ui/reader.py +132 -21
  94. package/ui/forge_ui/repos.py +428 -0
  95. package/ui/forge_ui/static/app.js +24 -5
  96. package/ui/forge_ui/static/cloudmodel.js +8 -5
  97. package/ui/forge_ui/static/controls.js +20 -13
  98. package/ui/forge_ui/static/modelstats.js +12 -8
  99. package/ui/forge_ui/static/panel.js +63 -17
  100. package/ui/forge_ui/static/runmodel.js +48 -7
  101. package/ui/forge_ui/static/scene3d.js +2 -15
  102. package/ui/forge_ui/static/settings.js +481 -68
  103. package/ui/forge_ui/static/style.css +56 -9
  104. package/references/jev.md +0 -865
  105. package/scripts/forge_jev/backtest.py +0 -322
  106. package/scripts/forge_jev/corpus.py +0 -156
  107. package/scripts/forge_jev/features.py +0 -24
  108. package/scripts/forge_jev/predict.py +0 -204
  109. package/scripts/forge_jev/review.py +0 -73
  110. package/scripts/forge_jev/routing.py +0 -271
  111. package/scripts/forge_jev/subset.py +0 -123
@@ -6,16 +6,19 @@
6
6
  #
7
7
  # Usage:
8
8
  # forge-parallel.sh plan <plan-dir> --repo <dir> [routing flags] [--decompose-level low|medium|high] [--ui]
9
+ # [--qa-threshold P0|P1|P2|P3|none|default]
9
10
  # forge-parallel.sh run <plan-dir> [--max-parallel N] [--yolo-dwarf] [--yolo-qa]
10
11
  # [--verify <command>] [--setup <command>] [--dry-run]
11
12
  # [--timeout S] [--retry-failed N] [--infra-retries M]
12
- # [--qa-threshold P0|P1|P2|P3] [--verify-retries N] [--no-forecast]
13
+ # [--qa-threshold P0|P1|P2|P3|none|default] [--verify-retries N] [--no-forecast]
13
14
  # [--sweep P0|P1|P2|P3 [--sweep-plausible] [--sweep-max N]]
14
- # [--sweep-ahead | --no-sweep-ahead]
15
+ # [--sweep-ahead | --no-sweep-ahead] [--attribute | --no-attribute]
15
16
  # forge-parallel.sh retry <plan-dir> <task-id> [--dwarf <spec>] [--setup <command>]
16
- # [--timeout S] [--qa-threshold Pn] [--verify-retries N]
17
+ # [--answer <text> | --answer-file <file>] (a NEEDS_INPUT task)
18
+ # [--timeout S] [--qa-threshold Pn|none|default] [--verify-retries N]
17
19
  # forge-parallel.sh integrate <plan-dir> --approved [--verify-retries N] [--final-review <qa-spec>]
18
- # forge-parallel.sh review <plan-dir> [--qa <spec>] [--yolo-qa] [--qa-threshold Pn]
20
+ # forge-parallel.sh review <plan-dir> [--qa <spec>] [--yolo-qa] [--qa-threshold Pn|none|default]
21
+ # forge-parallel.sh attribute <plan-dir> [--force] (find a merge that breaks the failed verification)
19
22
  # forge-parallel.sh accept <plan-dir> <task-id> --reason "<why>" --approved
20
23
  # forge-parallel.sh split <plan-dir> <new-plan> --tasks <id,id> [--dry-run]
21
24
  # forge-parallel.sh combine <new-plan> <plan-a> <plan-b> [<plan>...] [--verify <command>] [--dry-run]
@@ -41,18 +44,30 @@
41
44
  # unchanged, and starts fresh whenever it cannot; each round is
42
45
  # logged in tasks/<id>/review_sessions.tsv and tagged in the
43
46
  # dataset. `--no-reviewer-session` turns it off (the default)
47
+ # --implementer-session opt-in: a retry continues the implementer's earlier session
48
+ # (claude, openclaude) when its spec is unchanged and its work is
49
+ # still what the review judged, and starts fresh whenever it cannot;
50
+ # each attempt is logged in tasks/<id>/implementer_sessions.tsv.
51
+ # `--no-implementer-session` turns it off (the default)
44
52
  # --explore E on by default at 0.2 (0 turns it off, and is kept): a row left
45
53
  # to forge gets, with probability E/2, its neighbouring model
46
54
  # (one tier away in the given pools) instead of its pool's turn;
47
55
  # each decision and its odds go to tasks/<id>/route.jsonl
48
56
  # (precedence: flag, this plan's earlier choice, FORGE_ROUTE_EXPLORE)
49
57
  #
58
+ # The QA threshold (plan, run, retry, review; kept for the plan):
59
+ # --qa-threshold Pn the least severe CONFIRMED finding that still blocks (P0 worst ...
60
+ # P3 mildest; P2 when not set). forge decides the verdict from the
61
+ # findings: a PASS that lists a CONFIRMED finding at Pn or worse (or
62
+ # one with no severity) becomes FAIL; a FAIL made only of findings
63
+ # less severe than Pn is accepted as PASS-with-known-issues, and those
64
+ # are recorded in known_issues.md. `none` (or `off`) = P3, every
65
+ # CONFIRMED finding blocks; `default` goes back to P2
66
+ #
50
67
  # Opt-in run behaviour (all off unless asked):
51
68
  # --retry-failed N re-queue a task whose reviewer said FAIL up to N more times,
52
69
  # inside the same run, while the other tasks keep going
53
70
  # --infra-retries M wait out quota/auth/network stops up to M times instead of pausing
54
- # --qa-threshold Pn a reviewer FAIL made only of findings less severe than Pn is
55
- # accepted as PASS-with-known-issues (P0 worst ... P3 mildest)
56
71
  # --sweep Pn once every task has merged, before verification: the open findings
57
72
  # at Pn or worse (tolerated, and PLAUSIBLE with --sweep-plausible) become
58
73
  # at most --sweep-max N fix tasks (default 5), one per file, each with a
@@ -60,6 +75,8 @@
60
75
  # plan; what it does not fix stays for `forge issues`
61
76
  # --sweep-ahead a task is also told the open findings in files it owns, to fix only
62
77
  # on its way (kept for the plan; --no-sweep-ahead turns it off)
78
+ # --attribute when the run's combined verification fails, search the merge order for
79
+ # a merge that breaks it (kept for the plan; --no-attribute turns it off)
63
80
  #
64
81
  # Exit codes: 0 ok | 2 usage/validation | 3 precondition (not a git repo, etc.)
65
82
  # 5 some task failed QA or verification failed | 6 integration conflict
@@ -84,7 +101,7 @@ JEV_OPTS=0
84
101
  # Flags shared by several subcommands, then the helper libraries. Functions only, resolved
85
102
  # at call time, so they may use die/note/field and friends defined below.
86
103
  source "$SKILL_DIR/scripts/forge-parallel-options.sh"
87
- for forge_lib in task verify plans review; do
104
+ for forge_lib in task gates verify attribute plans review; do
88
105
  [ -f "$SKILL_DIR/scripts/forge-lib-$forge_lib.sh" ] && source "$SKILL_DIR/scripts/forge-lib-$forge_lib.sh"
89
106
  done
90
107
  OUTPUT=summary
@@ -147,7 +164,7 @@ pool_pick() { # pool_pick <comma-list> <index>
147
164
  do_plan() {
148
165
  local PLAN="${1:?plan needs a plan dir}"; shift
149
166
  local REPO="" D_ANY="" D_HI="" D_MD="" D_LO="" Q_ANY="" Q_HI="" Q_MD="" Q_LO=""
150
- local PLANNER="" NOMEM=0 jev_rc=0 DLEVEL="" SHADOW_QA="" SHADOW_RATE="" EXPLORE="" FAIL_FIRST="" REVIEWER_SESSION=""
167
+ local PLANNER="" NOMEM=0 jev_rc=0 DLEVEL="" SHADOW_QA="" SHADOW_RATE="" EXPLORE="" FAIL_FIRST="" REVIEWER_SESSION="" IMPLEMENTER_SESSION="" PRE_REVIEW="" PRE_REVIEW_PARALLEL=""
151
168
  while [ $# -gt 0 ]; do
152
169
  case "$1" in
153
170
  --repo) REPO="${2:?}"; shift 2 ;;
@@ -175,12 +192,23 @@ do_plan() {
175
192
  --ui|--no-ui) forge_ui_flag "$1"; shift ;;
176
193
  --fail-first) FAIL_FIRST=on; shift ;;
177
194
  --no-fail-first) FAIL_FIRST=off; shift ;;
195
+ --pre-review-tests) PRE_REVIEW=on; shift ;;
196
+ --no-pre-review-tests) PRE_REVIEW=off; shift ;;
197
+ --pre-review-parallel)
198
+ printf '%s' "${2:-}" | grep -Eq '^[0-9]{1,2}$' && [ "$2" -ge 1 ] && [ "$2" -le 64 ] \
199
+ || die "--pre-review-parallel needs an integer from 1 to 64" 2
200
+ PRE_REVIEW_PARALLEL="$((10#$2))"; shift 2 ;;
178
201
  --reviewer-session) REVIEWER_SESSION=on; shift ;;
179
202
  --no-reviewer-session) REVIEWER_SESSION=off; shift ;;
203
+ --implementer-session) IMPLEMENTER_SESSION=on; shift ;;
204
+ --no-implementer-session) IMPLEMENTER_SESSION=off; shift ;;
180
205
  *)
181
206
  # `case` is not a loop, so `continue` here belongs to the enclosing while.
207
+ # The shared flags write into the plan dir (--qa-threshold), so it must exist first.
208
+ [ -d "$PLAN" ] || die "plan dir '$PLAN' does not exist"
209
+ if forge_pp_flag plan "$PLAN" "$@"; then shift "$PP_SHIFT"; continue; fi
182
210
  if [ "$JEV_OPTS" = 1 ]; then
183
- forge_jev_flag "$1"; jev_rc=$?
211
+ forge_jev_flag "$@"; jev_rc=$?
184
212
  case "$jev_rc" in
185
213
  0) shift "$JEV_SHIFT"; continue ;;
186
214
  2) exit 2 ;;
@@ -212,9 +240,16 @@ do_plan() {
212
240
  || printf '%s\n' "$SHADOW_RATE" > "$PLAN/shadow_rate"
213
241
  # Fail-first evidence (opt-in, advisory) is part of the plan too: every task's review gets it.
214
242
  case "$FAIL_FIRST" in on) : > "$PLAN/fail_first" ;; off) rm -f "$PLAN/fail_first" ;; esac
243
+ # Pre-review tests (opt-in, #85): each attempt's affected tests run before its review, and a
244
+ # red result goes back to the implementer without one.
245
+ case "$PRE_REVIEW" in on) : > "$PLAN/pre_review_tests" ;; off) rm -f "$PLAN/pre_review_tests" ;; esac
246
+ # How many checks run at once across the plan's tasks (forge_pre_review's slots; default 2).
247
+ [ -z "$PRE_REVIEW_PARALLEL" ] || printf '%s\n' "$PRE_REVIEW_PARALLEL" > "$PLAN/pre_review_parallel"
215
248
  # Reviewer-session continuation (opt-in experiment): the file holds `on`. Readers only
216
249
  # test that it exists; the dataset records its content.
217
250
  case "$REVIEWER_SESSION" in on) printf '%s\n' on > "$PLAN/reviewer_session" ;; off) rm -f "$PLAN/reviewer_session" ;; esac
251
+ # Implementer-session continuation (opt-in, #90): the same `on` file, read the same way.
252
+ case "$IMPLEMENTER_SESSION" in on) printf '%s\n' on > "$PLAN/implementer_session" ;; off) rm -f "$PLAN/implementer_session" ;; esac
218
253
  # Exploration is decided here and only here, so the table and preflight see what will run.
219
254
  # On by default: choosing models from measured pass rates needs some randomized picks, and
220
255
  # history had almost none (9 tasks ever ran on more than one spec). The flag wins, then
@@ -239,6 +274,10 @@ do_plan() {
239
274
  fi
240
275
  [ -s "$PLAN/repo" ] || die "plan needs --repo the first time"
241
276
  REPO="$(cat "$PLAN/repo")"
277
+ # The repository the checkout belongs to, recorded once: a plan that has none gets one now.
278
+ forge_repo_identity "$REPO" "$PLAN"
279
+ # Before any decision-model step: the privacy floor and the run budget read these.
280
+ [ "$JEV_OPTS" = 1 ] && forge_decide_env "$REPO" "$PLAN"
242
281
  [ -s "$PLAN/run_id" ] || basename "$PLAN" | sed 's/^forge-//' > "$PLAN/run_id"
243
282
  # Every plan, --ui or not: forge-ui's view of this repo's runs reads the registry.
244
283
  forge_ui_register "$PLAN"
@@ -264,54 +303,68 @@ PY
264
303
  note " if the tasks depend on them."
265
304
  fi
266
305
 
267
- # --- jev: advisory difficulty scoring ------------------------------------------
268
- # Advisory only: a suggested tier, and gate warnings about the task text. Writing a Jev
269
- # tier into the difficulty column (`--jev-act`, gated on a per-repo calibration) was
270
- # removed on 2026-10-17 without ever having acted; models are chosen from measured pass
271
- # rates instead (plan --explore, `forge dataset routing`).
306
+ # --- jev: plan-time gates ------------------------------------------------------
307
+ # Prompt adequacy and drift: warnings about the task text and its declared files. Drift
308
+ # `on` adds the files it predicts to tasks.tsv (inside score-plan); `shadow` only records.
272
309
  local jev_loaded=0
273
310
  if [ -f "$SKILL_DIR/scripts/forge-jev-options.sh" ]; then
274
311
  source "$SKILL_DIR/scripts/forge-jev-options.sh" 2>/dev/null && jev_loaded=1
275
312
  fi
276
- if [ "$jev_loaded" = 1 ] && forge_jev_active routing; then
313
+ if [ "$jev_loaded" = 1 ] && { forge_decide_active prompt || forge_decide_active drift; }; then
277
314
  local jev_scores jev_rc
278
315
  jev_scores="$(python3 "$SKILL_DIR/scripts/forge-jev.py" score-plan \
279
316
  --plan "$PLAN" --repo "$REPO" 2>/dev/null)"
280
317
  jev_rc=$?
318
+ # A gate in shadow is asked and recorded by score-plan, which leaves it out of
319
+ # these rows: what is printed here is only what an `on` gate has to say.
320
+ # --jev-shadow before migration keeps its gates `on` and prints a count for the
321
+ # warnings, as it always did; what a gate ADDED is still said, whatever the flag.
281
322
  if [ "$jev_rc" -eq 0 ] && [ -n "$jev_scores" ]; then
282
- # Shadow mode records the judgment and prints nothing per task.
283
- if [ "${FORGE_JEV_SHADOW:-}" = on ]; then
284
- note "jev: scored $(printf '%s\n' "$jev_scores" | grep -c . ) task(s) in shadow mode (no change)"
285
- else
286
- local jid jtier jconf jcomp jesc jdecl jwarn jdrift
287
- # The columns score-plan prints: id, tier, confidence, composite, escalated,
288
- # declared, warnings, drift. Read in lockstep with cmd_score_plan.
289
- while IFS="$(printf '\t')" read -r jid jtier jconf jcomp jesc jdecl jwarn jdrift; do
290
- [ -n "$jid" ] || continue
291
- # score-plan writes "-" for "not escalated"; only a real reason is worth showing.
292
- [ "$jesc" = "-" ] && jesc=""
293
- note "jev: $jid suggests $jtier (confidence $jconf${jesc:+, escalated: $jesc}) — declared $jdecl"
294
- # Gates are warnings: they are about the task text and its declared files, not
295
- # about which model runs it.
296
- if [ -n "$jwarn" ] && [ "$jwarn" != "-" ]; then
297
- case "$jwarn" in
298
- *prompt_adequacy*) note "jev: $jid — the prompt may be too vague for QA to judge correctness against" ;;
299
- esac
300
- case "$jwarn" in
301
- *independently_verifiable*) note "jev: $jid — may not be verifiable on its own (decompose.md forbids splitting this finely)" ;;
302
- esac
303
- fi
304
- if [ -n "$jdrift" ] && [ "$jdrift" != "-" ]; then
305
- note "jev: $jid may also edit undeclared files: $jdrift"
306
- note " undeclared files break wave disjointness — add them to the files column or split the task"
307
- fi
308
- done <<EOF
323
+ local jid jwarn jdrift
324
+ # The columns score-plan prints: id, warnings, drift. Read in lockstep with
325
+ # cmd_score_plan.
326
+ while IFS="$(printf '\t')" read -r jid jwarn jdrift; do
327
+ [ -n "$jid" ] || continue
328
+ if [ "${FORGE_JEV_SHADOW:-}" != on ]; then
329
+ case "$jwarn" in
330
+ *prompt_adequacy*) note "jev: $jid — the prompt may be too vague for QA to judge correctness against" ;;
331
+ esac
332
+ fi
333
+ if [ -n "$jdrift" ] && [ "$jdrift" != "-" ]; then
334
+ # The drift field only names files score-plan ADDED (drift `on`): a shadowed gate
335
+ # prints '-', so there is no "may edit undeclared files" case left to hint at.
336
+ note "drift added to $jid: $jdrift"
337
+ fi
338
+ done <<EOF
309
339
  $jev_scores
310
340
  EOF
341
+ if [ "${FORGE_JEV_SHADOW:-}" = on ]; then
342
+ note "jev: scored $(printf '%s\n' "$jev_scores" | grep -c .) task(s) in shadow mode (no change)"
311
343
  fi
312
344
  fi
313
345
  fi
314
346
 
347
+ # Coverage: does some task's acceptance criteria deliver each requirement of request.md?
348
+ # `on` marks the ones no task covers pending in plan-gates.json; the send-back file below
349
+ # says which, so nothing is printed here.
350
+ if [ "$jev_loaded" = 1 ] && forge_decide_active coverage; then
351
+ python3 "$SKILL_DIR/scripts/forge-jev.py" coverage --plan "$PLAN" --repo "$REPO" >/dev/null 2>&1 || true
352
+ fi
353
+
354
+ # The one send-back step of this call, after every pre-resolve gate: prompt and coverage
355
+ # mark what to rewrite, and exit 4 hands it back before validation, waves and the table.
356
+ # An item is sent back once; the re-plan lists what is still wrong under the table.
357
+ if [ "$jev_loaded" = 1 ] && { forge_decide_active prompt || forge_decide_active coverage; }; then
358
+ local sendback_out sendback_rc=0 sendback_n
359
+ sendback_out="$(python3 "$SKILL_DIR/scripts/forge-plan-check.py" sendback "$PLAN" 2>/dev/null)" || sendback_rc=$?
360
+ if [ "$sendback_rc" -eq 4 ]; then
361
+ printf '%s\n' "$sendback_out"
362
+ sendback_n="$(printf '%s\n' "$sendback_out" | grep -c '^- ')"
363
+ note "plan gates sent $sendback_n item(s) back: rewrite them as $PLAN/plan.sendback.md says, then run plan again (once)"
364
+ return 4
365
+ fi
366
+ fi
367
+
315
368
  # --- validate + resolve routing ---
316
369
  local tmp="$PLAN/.tasks.tsv.new"; : > "$tmp"
317
370
  local hi_i=0 md_i=0 lo_i=0 an_i=0 qhi_i=0 qmd_i=0 qlo_i=0 qan_i=0 unassigned=0
@@ -380,50 +433,53 @@ EOF
380
433
 
381
434
  compute_waves "$PLAN"
382
435
 
383
- # --- clef: silent first-attempt predictions -------------------------------------
384
- # Recorded only (tasks/<id>/clef.first_pass and the run dataset), so a later export can
385
- # measure whether Clef tells which tasks a first attempt will fail. Nothing in a run
386
- # reads them. Without a Cloudflare provider the command returns at once; with one it
387
- # bounds itself (one deadline for the plan, an in-process watchdog).
388
- if [ "${FORGE_JEV:-}" != off ]; then
389
- python3 "$SKILL_DIR/scripts/forge-jev.py" first-pass --plan "$PLAN" --repo "$REPO" >/dev/null 2>&1 || true
390
- fi
391
-
392
- # --- jev: advisory wave coupling ------------------------------------------------
393
- # Disjoint files bound parallelism structurally, but they cannot see the conflict
394
- # where one task in a wave changes an interface another task in the SAME wave
395
- # consumes -- both diffs are clean against their own baseline, and the merge is
396
- # broken. This is advisory only: it never edits tasks.tsv, never adds a dep, and
397
- # never changes waves.tsv, so it must run after compute_waves and before the table
398
- # that reports the plan is printed.
399
- if [ "$jev_loaded" = 1 ] && forge_jev_active gates; then
436
+ # --- jev: forward dependency ----------------------------------------------------
437
+ # Wave order is structural, but it cannot see an earlier-wave task written against
438
+ # an interface a later-wave task builds. Forward `on` adds that dep to tasks.tsv (inside
439
+ # `coupling`, refusing a cycle), so the waves are computed again below when it did;
440
+ # otherwise the gate only advises. It runs after compute_waves and before the table.
441
+ if [ "$jev_loaded" = 1 ] && forge_decide_active forward; then
400
442
  local jev_coupling jev_coupling_rc
401
443
  jev_coupling="$(python3 "$SKILL_DIR/scripts/forge-jev.py" coupling \
402
444
  --plan "$PLAN" --repo "$REPO" 2>/dev/null)"
403
445
  jev_coupling_rc=$?
446
+ # A shadowed forward gate is recorded in jev-coupling.json and prints no row; --jev-shadow
447
+ # before migration counts the advisory rows, as it always did. Rows an `on` gate acted
448
+ # on (`added`, `cycle`) are said and acted on whatever the flag.
404
449
  if [ "$jev_coupling_rc" -eq 0 ] && [ -n "$jev_coupling" ]; then
405
- if [ "${FORGE_JEV_SHADOW:-}" = on ]; then
406
- note "jev: found $(printf '%s\n' "$jev_coupling" | grep -c .) coupled pair(s) in shadow mode (no change)"
407
- else
408
- local ca cb cp ckind
409
- while IFS="$(printf '\t')" read -r ca cb cp ckind; do
410
- [ -n "$ca" ] || continue
411
- # Two different failures, so two different sentences. same-wave is a broken
412
- # merge between concurrent tasks; forward is an earlier task written against
413
- # work that has not been built yet, which its own review cannot catch.
414
- if [ "$ckind" = forward ]; then
415
- note "jev: task '$ca' runs before '$cb' and may depend on what it builds ($cp) — consider a dep"
416
- else
417
- note "jev: tasks '$ca' and '$cb' may conflict despite disjoint files ($cp) — consider a dep"
418
- fi
419
- done <<EOF
450
+ local ca cb cp ckind dep_added=0 advisory=0
451
+ while IFS="$(printf '\t')" read -r ca cb cp ckind; do
452
+ [ -n "$ca" ] || continue
453
+ # An earlier task written against work that has not been built yet, which its own
454
+ # review cannot catch. The last column says what `on` did about it.
455
+ case "$ckind" in
456
+ added)
457
+ dep_added=1
458
+ note "forward: task '$ca' now waits for '$cb' ($cp) — it may depend on what '$cb' builds" ;;
459
+ cycle)
460
+ note "forward: '$ca' may depend on '$cb' ($cp), but that dependency would form a cycle — not added" ;;
461
+ *)
462
+ advisory=$((advisory+1))
463
+ [ "${FORGE_JEV_SHADOW:-}" = on ] ||
464
+ note "jev: task '$ca' runs before '$cb' and may depend on what it builds ($cp) — consider a dep" ;;
465
+ esac
466
+ done <<EOF
420
467
  $jev_coupling
421
468
  EOF
469
+ if [ "$advisory" -gt 0 ] && [ "${FORGE_JEV_SHADOW:-}" = on ]; then
470
+ note "jev: found $advisory coupled pair(s) in shadow mode (no change)"
422
471
  fi
472
+ if [ "$dep_added" = 1 ]; then compute_waves "$PLAN"; fi
423
473
  fi
424
474
  fi
425
475
 
426
476
  render_table "$PLAN"
477
+ # What the gates did, from plan-gates.json: files and deps they added, prompts and
478
+ # requirements still flagged. Prints nothing when no gate recorded anything.
479
+ python3 "$SKILL_DIR/scripts/forge-plan-check.py" gates "$PLAN" 2>/dev/null || true
480
+ # Advisory, under the table: a privacy floor that silences every gate, and which
481
+ # gates this plan's runs will ask. Prints nothing when it cannot tell.
482
+ [ "$jev_loaded" = 1 ] && forge_decide_plan_lines "${FORGE_DECIDE_REPO:-$REPO}"
427
483
  # Before the UNASSIGNED return, so the URL prints under the table either way.
428
484
  forge_ui_launch "$PLAN"
429
485
 
@@ -505,9 +561,34 @@ render_table() {
505
561
  echo "fail-first: on, but $(cat "$PLAN/repo")/.forge/verify-subset (a test command with {files}) is missing, so nothing will run"
506
562
  fi
507
563
  fi
564
+ if [ -f "$PLAN/pre_review_tests" ]; then
565
+ if [ -s "$(cat "$PLAN/repo")/.forge/verify-subset" ]; then
566
+ echo "pre-review tests: on — each attempt's affected tests run before its review; a red attempt goes back to its implementer unreviewed"
567
+ else
568
+ echo "pre-review tests: on, but $(cat "$PLAN/repo")/.forge/verify-subset (a test command with {files} or {changed}) is missing, so nothing will run"
569
+ fi
570
+ fi
508
571
  if [ -f "$PLAN/reviewer_session" ]; then
509
572
  echo "reviewer-session: a later review round continues the reviewer's earlier session when its QA spec is unchanged (experiment; falls back to a fresh session)"
510
573
  fi
574
+ if [ -f "$PLAN/implementer_session" ]; then
575
+ echo "implementer-session: a retry continues the implementer's earlier session when its spec is unchanged and its work is as the review left it (claude, openclaude; falls back to a fresh session)"
576
+ fi
577
+ # The plan-level threshold: at plan time no per-task file exists yet.
578
+ local qa_level qa_src
579
+ read -r qa_level qa_src <<EOF
580
+ $(qa_threshold_walk "$PLAN" "")
581
+ EOF
582
+ case "$qa_level" in
583
+ P3) echo "QA threshold: P3 ($qa_src) — every CONFIRMED finding blocks" ;;
584
+ P0) echo "QA threshold: P0 ($qa_src) — P0 blocks; a CONFIRMED P1–P3 is recorded as a known issue" ;;
585
+ P1) echo "QA threshold: P1 ($qa_src) — P0–P1 block; a CONFIRMED P2–P3 is recorded as a known issue" ;;
586
+ *) echo "QA threshold: P2 ($qa_src) — P0–P2 block; a CONFIRMED P3 is recorded as a known issue" ;;
587
+ esac
588
+ # A reviewer spec that names no effort reviews at forge-dispatch.sh's QA default (#89).
589
+ if awk -F'\t' '$0 !~ /^#/ && NF >= 6 { split($6, s, ":"); if (s[2] == "") found = 1 } END { exit !found }' "$T"; then
590
+ echo "QA effort: high where a reviewer spec names none (forge-dispatch.sh's default; planning stays at xhigh)"
591
+ fi
511
592
  echo
512
593
  local w id dws starred=0
513
594
  for w in $(awk -F'\t' '{print $1}' "$W" | sort -un); do
@@ -655,6 +736,16 @@ write_capsule() { # write_capsule <plan> <id> <role>
655
736
  echo "- MERGED means present in this task's pinned baseline; later merges are not included."
656
737
  echo "- The review diff is only this task's work against that baseline."
657
738
  echo "- Implementation must respect other tasks' ownership; report necessary scope changes."
739
+ if [ -s "$PLAN/tasks/$me/submodules.txt" ]; then
740
+ local sm_st sm_path
741
+ while IFS="$(printf '\t')" read -r sm_st sm_path _; do
742
+ if [ "$sm_st" = missing ]; then
743
+ echo "- Submodule \`$sm_path\` is not available in this worktree (its directory is empty)."
744
+ else
745
+ echo "- \`$sm_path\` is a submodule: its files are here to read and build with. Changes there are not part of your change and are never committed or reviewed."
746
+ fi
747
+ done < "$PLAN/tasks/$me/submodules.txt"
748
+ fi
658
749
 
659
750
  } > "$out"
660
751
  echo "$out"
@@ -695,11 +786,29 @@ worktree_setup_command() { # worktree_setup_command <plan> <repo>
695
786
  return 0
696
787
  }
697
788
 
789
+ # Exports each submodule's files into a fresh worktree (offline, no `.git`, so they stay
790
+ # invisible to every diff and fingerprint). Never fatal: a failure leaves the directory empty.
791
+ populate_submodules() { # populate_submodules <repo> <wt> <tdir> <id>
792
+ local repo="$1" wt="$2" tdir="$3" id="$4" st path _sha detail err
793
+ rm -f "$tdir/submodules.txt"
794
+ err="$(python3 "$SKILL_DIR/scripts/forge-submodules.py" populate --source "$repo" --worktree "$wt" \
795
+ --out "$tdir/submodules.txt" 2>&1 >/dev/null)" \
796
+ || note "$id: submodule population failed (continuing): $(printf '%s' "$err" | tail -1)"
797
+ [ -s "$tdir/submodules.txt" ] || return 0
798
+ while IFS="$(printf '\t')" read -r st path _sha detail; do
799
+ [ "$st" = missing ] && note "$id: submodule $path is not available in this worktree ($detail)"
800
+ done < "$tdir/submodules.txt"
801
+ return 0
802
+ }
803
+
698
804
  # Non-fatal by design. A setup step that fails leaves a worktree the dwarf can still
699
805
  # work in — it just has to install things itself, which is the status quo this exists
700
806
  # to improve on. Failing the task instead would turn a slow run into a dead one.
701
807
  run_worktree_setup() { # run_worktree_setup <plan> <repo> <wt> <tdir> <id>
702
808
  local PLAN="$1" REPO="$2" wt="$3" tdir="$4" id="$5" cmd
809
+ # Every worktree forge makes reaches here once, right after it exists, so the submodules'
810
+ # files are in place before `.forge/setup` runs. The outcome lands in <tdir>/submodules.txt.
811
+ populate_submodules "$REPO" "$wt" "$tdir" "$id"
703
812
  cmd="$(worktree_setup_command "$PLAN" "$REPO")"
704
813
  [ -n "$cmd" ] || return 0
705
814
  if (cd "$wt" && eval "$cmd") >"$tdir/setup.out" 2>&1; then
@@ -790,14 +899,12 @@ do_task_body() (
790
899
  if [ -z "$resume" ]; then
791
900
  attempt=$(( attempt + 1 ))
792
901
  echo "$attempt" > "$tdir/attempt"
793
- # Silent: Clef's guess at whether this retry passes with the findings it was handed.
794
- # Only a real new dwarf attempt (not a QA-only resume); recorded in clef.retry_pass.<n>
795
- # and the run dataset, never read by the run, and it can never fail the task.
796
- if [ "$attempt" -gt 1 ] && [ -s "$tdir/retry_findings.md" ] && [ "${FORGE_JEV:-}" != off ]; then
797
- python3 "$SKILL_DIR/scripts/forge-jev.py" retry-pass --plan "$PLAN" --repo "$REPO" --task "$id" \
798
- --attempt "$attempt" >/dev/null 2>&1 || true
799
- fi
800
902
  fi
903
+ # The gate's decision belongs to the review that made it. A review that ends before the gate
904
+ # (invalidated, timed out, no changes) must not leave an earlier attempt's decision for the
905
+ # next retry note or `accept` to read; prepare_retry has already carried it forward.
906
+ rm -f "$tdir/qa.gate" "$tdir/known_issues.md" "$tdir/invalidated.reason" "$tdir/prereview.failed" \
907
+ "$tdir/bounced" "$tdir/message.failed"
801
908
 
802
909
  # The base is pinned per task on first creation and reused by every retry. The
803
910
  # integration branch moves as later waves land, so re-reading the run-level
@@ -830,18 +937,73 @@ do_task_body() (
830
937
  /bin/bash "$MEMORY" inject "$REPO" dwarf --task "$tdir/prompt.md" \
831
938
  --run-dir "$tdir" > "$tdir/dwarf.memory"
832
939
  {
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"
940
+ python3 "$SKILL_DIR/scripts/forge-prompt.py" --requirements "$tdir/prompt.md" --capsule "$tdir/baseline.capsule" --approach "$tdir/approach.md" --retry "$tdir/retry_findings.md" --retry-source "$(cat "$tdir/retry_findings.source" 2>/dev/null || echo review)" --memory "$tdir/dwarf.memory"
834
941
  [ ! -f "$PLAN/sweep_ahead" ] || python3 "$SKILL_DIR/scripts/forge-sweep.py" ahead "$PLAN" "$id" --role dwarf 2>/dev/null
835
942
  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."
836
943
  echo "On retry, fix the findings in the existing work; preserve parts not criticised. Explain any finding you reject."
837
- echo; python3 "$SKILL_DIR/scripts/forge-contract.py" dwarf
944
+ echo; python3 "$SKILL_DIR/scripts/forge-contract.py" dwarf $([ ! -f "$PLAN/pre_review_tests" ] || echo --pre-review)
838
945
  echo; /bin/bash "$MEMORY" note dwarf
839
946
  } > "$tdir/dwarf.input"
840
947
  guard_before_dwarf "$wt" "$tdir"
948
+ # Whether this attempt continues the implementer's earlier session (plan
949
+ # --implementer-session, #90): only a retry of judged work that is still exactly what was
950
+ # judged, on the harness, model and effort that made it. Every other answer is a fresh
951
+ # dispatch, and nothing here can abort the attempt: a doctor that fails means fresh.
952
+ local is_mode=fresh is_reason="" is_id="" is_fp="" is_cont=()
953
+ if [ -f "$PLAN/implementer_session" ]; then
954
+ if [ -n "${FORGE_FRACTAL_RUN:-}" ] || forge_plan_is_fractal "$PLAN"; then
955
+ is_reason=fractal
956
+ elif [ "$attempt" = 1 ]; then
957
+ is_reason=first-attempt
958
+ elif [ ! -s "$tdir/retry_findings.md" ]; then
959
+ is_reason=no-findings
960
+ elif ! implementer_work_unchanged "$tdir" "$wt"; then
961
+ is_reason=work-changed
962
+ else
963
+ FORGE_CAPABILITY_CACHE="${FORGE_CAPABILITY_CACHE:-$PLAN/capabilities}" \
964
+ /bin/bash "$DISPATCH" doctor --spec "$dw" --role dwarf $yd > "$tdir/dwarf.doctor" 2>&1 || : > "$tdir/dwarf.doctor"
965
+ IFS=$'\t' read -r is_mode is_reason is_id < <(review_session_decide \
966
+ "$tdir/retry_findings.dwarf.session" "$tdir/retry_findings.dwarf.resolved" "$tdir/dwarf.doctor" "claude openclaude")
967
+ if [ "$is_mode" = continued ]; then
968
+ is_cont=(--continue-session "$is_id")
969
+ is_fp="$(forge_fingerprint "$wt")" || is_fp=""
970
+ fi
971
+ fi
972
+ fi
841
973
  forge_metric_phase dispatch
842
- /bin/bash "$DISPATCH" dwarf "$dw" --repo "$wt" --run-dir "$tdir" \
843
- --ripwire-query-file "$tdir/prompt.md" --prompt-file "$tdir/dwarf.input" $yd --output "${FORGE_OUTPUT:-summary}" >"$tdir/dwarf.out" 2>&1
974
+ local dwarf_cmd=(/bin/bash "$DISPATCH" dwarf "$dw" --repo "$wt" --run-dir "$tdir" \
975
+ --ripwire-query-file "$tdir/prompt.md" --prompt-file "$tdir/dwarf.input" ${yd:+"$yd"} --output "${FORGE_OUTPUT:-summary}")
976
+ "${dwarf_cmd[@]}" ${is_cont[@]+"${is_cont[@]}"} >"$tdir/dwarf.out" 2>&1
844
977
  rc=$?
978
+ if [ "$rc" = 9 ] && [ "$is_mode" = continued ]; then
979
+ # The session was not continued (the harness cannot, or it failed). Why comes from the
980
+ # continued dispatch's record, which a fresh one clears.
981
+ if [ "$(sed -n 's/^reason=//p' "$tdir/dwarf.continue" 2>/dev/null | head -1)" = unsupported ]; then
982
+ is_reason=continue-unsupported
983
+ else
984
+ is_reason=continue-failed
985
+ fi
986
+ mv "$tdir/dwarf.out" "$tdir/dwarf.continue.out" 2>/dev/null || true
987
+ if [ -n "$is_fp" ] && [ "$(forge_fingerprint "$wt")" = "$is_fp" ]; then
988
+ # It changed nothing, so the same input goes to a fresh session, which decides the attempt.
989
+ is_mode=fresh
990
+ "${dwarf_cmd[@]}" >"$tdir/dwarf.out" 2>&1
991
+ rc=$?
992
+ else
993
+ # It worked on the task before it failed: that is a failed implementer, as when a fresh
994
+ # one fails, and running a fresh one on top would pay for the attempt twice.
995
+ is_mode=failed
996
+ cp "$tdir/dwarf.continue.out" "$tdir/dwarf.out" 2>/dev/null || true
997
+ rc=4
998
+ note "$id: the continued implementer session failed after changing files — not running a fresh one on top"
999
+ fi
1000
+ fi
1001
+ if [ -f "$PLAN/implementer_session" ]; then
1002
+ # One row per attempt, describing the dispatch whose result counts.
1003
+ local is_row=1
1004
+ [ ! -f "$tdir/implementer_sessions.tsv" ] || is_row=$(( $(wc -l < "$tdir/implementer_sessions.tsv") + 1 ))
1005
+ printf '%s\t%s\t%s\t%s\t%s\n' "$is_row" "$attempt" "$is_mode" "$is_reason" "$is_id" >> "$tdir/implementer_sessions.tsv"
1006
+ fi
845
1007
  if [ "${FORGE_OUTPUT:-summary}" = full ]; then cat "$tdir/dwarf.out"; fi
846
1008
  if [ "$rc" -ne 0 ]; then
847
1009
  case "$rc" in
@@ -857,6 +1019,8 @@ do_task_body() (
857
1019
  --duration "$(dispatch_duration "$tdir" dwarf)" >/dev/null 2>&1
858
1020
 
859
1021
  guard_after_dwarf "$wt" "$tdir" "$id"
1022
+ # Before the snapshot, so a dwarf that only asked a question (and changed nothing) still reaches it.
1023
+ forge_post_dwarf_gates "$PLAN" "$id" "$tdir" "$wt" "$attempt" || return 0
860
1024
  forge_metric_phase snapshot
861
1025
  # Capture the real diff, including new files, then commit on the task branch.
862
1026
  # Diffed against the task's pinned base rather than the branch head, so a retry
@@ -881,7 +1045,11 @@ do_task_body() (
881
1045
  echo "The dwarf produced no changes at all." > "$tdir/qa.last"
882
1046
  note "$id: dwarf produced an empty diff"; return 1
883
1047
  fi
884
- if [ -s "$tdir/changes.prev.diff" ] && cmp -s "$tdir/changes.diff" "$tdir/changes.prev.diff"; then
1048
+ # A bounced attempt (retry_findings.source `bounce`) was never reviewed, so a retry that keeps
1049
+ # its code is reviewed rather than refused as identical: the refusal is for a diff a review
1050
+ # already judged.
1051
+ if [ -s "$tdir/changes.prev.diff" ] && cmp -s "$tdir/changes.diff" "$tdir/changes.prev.diff" \
1052
+ && [ "$(cat "$tdir/retry_findings.source" 2>/dev/null)" != bounce ]; then
885
1053
  echo FAIL > "$tdir/status"
886
1054
  : > "$tdir/noretry"
887
1055
  echo "The retry changed nothing: the diff is byte-identical to the previous attempt." \
@@ -908,47 +1076,46 @@ do_task_body() (
908
1076
  echo INVALIDATED > "$tdir/status"; return 1
909
1077
  fi
910
1078
  echo "$reviewed" > "$tdir/reviewed.commit"
1079
+ # The implementer session that made this commit, and what it ran as: the one a retry of
1080
+ # these findings may continue (plan --implementer-session; carried with the findings).
1081
+ rm -f "$tdir/reviewed.dwarf.session" "$tdir/reviewed.dwarf.resolved"
1082
+ [ ! -s "$tdir/dwarf.session" ] || cp "$tdir/dwarf.session" "$tdir/reviewed.dwarf.session"
1083
+ [ ! -s "$tdir/dwarf.resolved" ] || cp "$tdir/dwarf.resolved" "$tdir/reviewed.dwarf.resolved"
1084
+ forge_pre_qa_gates "$PLAN" "$id" "$tdir" "$wt" "$attempt" "$reviewed" || return 0
911
1085
  else
912
1086
  reviewed="$(cat "$tdir/reviewed.commit")"
913
1087
  fi
914
1088
  review="$tdir/review-$attempt-$$"
915
1089
  forge_review_snapshot "$wt" "$base" "$reviewed" "$review" || { echo ERROR > "$tdir/status"; return 1; }
916
1090
  source_fp="$(forge_fingerprint "$wt")" || return 1
917
- review_fp="$(forge_fingerprint "$review")" || return 1
1091
+ # The review copy is a commit: an untracked file the reviewer leaves there (a test run's
1092
+ # bytecode) changes nothing that merges, so only tracked content counts (#95).
1093
+ review_fp="$(forge_fingerprint "$review" --tracked)" || return 1
918
1094
  echo "$source_fp" > "$tdir/source.fingerprint"
919
1095
  echo "$review_fp" > "$tdir/review.fingerprint"
920
1096
 
921
1097
  forge_metric_phase preparation
922
- local jev_loaded_qa=0 subset_cmd="" qa_thr rs_on=0
1098
+ local qa_thr rs_on=0
923
1099
  qa_thr="$(qa_threshold_for "$PLAN" "$id")"
924
1100
  # Read once per round, like the threshold: continuing the earlier reviewer's session is an
925
1101
  # opt-in experiment (plan --reviewer-session), and a round never changes its mind midway.
926
1102
  [ -f "$PLAN/reviewer_session" ] && rs_on=1
927
1103
 
928
- # A SEPARATE file from .forge/verify, and opt-in. It must contain {files}, because
929
- # only the project knows how its runner takes a file list -- `pytest -q {files}` works,
930
- # `npm test -- {files}` needs the dashes, and `go test ./...` cannot do it at all.
931
- # Absent, this whole feature stays off and nothing extra runs.
932
- [ -s "$REPO/.forge/verify-subset" ] && subset_cmd="$(cat "$REPO/.forge/verify-subset")"
933
- if [ -f "$SKILL_DIR/scripts/forge-jev-options.sh" ]; then
934
- source "$SKILL_DIR/scripts/forge-jev-options.sh" 2>/dev/null && jev_loaded_qa=1
935
- fi
936
- # Early feedback for the reviewer: run the tests this change most likely broke, in a
937
- # throwaway export. Requires a configured verify command -- without one there is no
938
- # runner to point at a file list, and inventing one is not this feature's job.
939
- if [ -n "$subset_cmd" ] && [ "$jev_loaded_qa" = 1 ] && forge_jev_active tests; then
940
- git -C "$wt" diff --name-only "$(cat "$tdir/review.base")" "$reviewed" \
941
- > "$tdir/changed.txt" 2>/dev/null || : > "$tdir/changed.txt"
942
- python3 "$SKILL_DIR/scripts/forge-jev.py" test-subset --repo "$wt" \
943
- --commit "$reviewed" --task "$tdir/prompt.md" --command "$subset_cmd" \
944
- --changed "$tdir/changed.txt" --base "$(cat "$tdir/review.base")" \
945
- --run-dir "$tdir" > "$tdir/subset.jev.txt" 2>/dev/null \
946
- || : > "$tdir/subset.jev.txt"
947
- fi
948
1104
  # Opt-in (plan --fail-first): do this attempt's new tests fail without its change? Read-only
949
- # on the review snapshot, like the subset run above; the reviewer gets the evidence.
1105
+ # on the review snapshot, so the task worktree is untouched; the reviewer gets the evidence.
950
1106
  rm -f "$tdir/failfirst.json" "$tdir/failfirst.txt"
951
1107
  [ ! -f "$PLAN/fail_first" ] || forge_fail_first "$review" "$tdir" "$REPO"
1108
+ # Opt-in (plan --pre-review-tests, #85): the tests this attempt affects, before paying for a
1109
+ # review. Red: the attempt fails unreviewed and its findings are the test output. A QA-only
1110
+ # resume re-reviews work that already passed this check, and keeps its evidence.
1111
+ [ -f "$PLAN/pre_review_tests" ] || rm -f "$tdir/prereview.json" "$tdir/prereview.txt" "$tdir/prereview.md"
1112
+ if [ -f "$PLAN/pre_review_tests" ] && [ -z "$resume" ] && ! forge_pre_review "$review" "$tdir" "$REPO" "$PLAN"; then
1113
+ : > "$tdir/prereview.failed"
1114
+ rm -f "$tdir/qa.last" # an earlier attempt's review describes other code
1115
+ echo FAIL > "$tdir/status"
1116
+ note "$id: the tests this attempt affects fail — sent back to the implementer without a review (see $tdir/prereview.md)"
1117
+ return 0
1118
+ fi
952
1119
  # What changed since the review whose findings this reviewer is handed. Fractal keeps
953
1120
  # today's prompt unless it retries, as changes.prev.diff does. dwarf.last is the reply of
954
1121
  # the attempt under review: a dwarf run just rewrote it, or $resume (the validated QA-only
@@ -974,22 +1141,12 @@ do_task_body() (
974
1141
  /bin/bash "$MEMORY" inject "$REPO" qa --task "$tdir/prompt.md" \
975
1142
  --run-dir "$tdir" > "$tdir/qa.memory"
976
1143
  {
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"
1144
+ python3 "$SKILL_DIR/scripts/forge-prompt.py" --requirements "$tdir/prompt.md" --capsule "$tdir/baseline.capsule" --approach "$tdir/approach.md" --retry "$tdir/retry_findings.md" --retry-source "$(cat "$tdir/retry_findings.source" 2>/dev/null || echo review)" ${prompt_extra[@]+"${prompt_extra[@]}"} --memory "$tdir/dwarf.memory" --memory "$tdir/qa.memory"
978
1145
  python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part head --kind "$qa_kind"
979
1146
  echo
980
1147
  [ ! -f "$PLAN/sweep_ahead" ] || python3 "$SKILL_DIR/scripts/forge-sweep.py" ahead "$PLAN" "$id" --role qa 2>/dev/null
981
1148
  if [ -s "$tdir/failfirst.txt" ]; then cat "$tdir/failfirst.txt"; echo; fi
982
- # A subset failure is the one thing a reviewer most wants to know and cannot see
983
- # from the diff. It runs in a throwaway export of the reviewed commit, so the task
984
- # worktree is untouched and no fingerprint can change. It never replaces
985
- # verification -- the full suite still runs at integration exactly as before.
986
- if [ -s "$tdir/subset.jev.txt" ]; then
987
- cat "$tdir/subset.jev.txt"
988
- echo
989
- echo "That is a subset chosen by relevance, not the full suite, so a pass would"
990
- echo "prove nothing and is not reported. A failure here is real."
991
- echo
992
- fi
1149
+ if [ -s "$tdir/prereview.txt" ]; then cat "$tdir/prereview.txt"; echo; fi
993
1150
  if [ -s "$tdir/guard.txt" ]; then
994
1151
  # Facts forge observed about how the implementer worked. On a first attempt the reviewer
995
1152
  # never sees the implementer's own report (a retry's reviewer gets the reply, to check
@@ -1009,7 +1166,7 @@ do_task_body() (
1009
1166
  echo "matters because another task may own those files."
1010
1167
  echo
1011
1168
  fi
1012
- python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part tail --kind "$qa_kind" ${qa_thr:+--threshold "$qa_thr"}
1169
+ python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part tail --kind "$qa_kind" --threshold "$qa_thr"
1013
1170
  echo
1014
1171
  # --irreversible-delete: a deleted file shows as a header, not every line it had (one deleted
1015
1172
  # data directory once made a 7.1M-character prompt that the reviewer's CLI refused).
@@ -1086,7 +1243,10 @@ do_task_body() (
1086
1243
  rm -f "$tdir/resume"
1087
1244
 
1088
1245
  forge_metric_phase verification
1089
- if [ "$(forge_fingerprint "$wt")" != "$source_fp" ] || [ "$(forge_fingerprint "$review")" != "$review_fp" ]; then
1246
+ if [ "$(forge_fingerprint "$wt")" != "$source_fp" ] || [ "$(forge_fingerprint "$review" --tracked)" != "$review_fp" ]; then
1247
+ # The committed work is untouched, so --retry-failed may review it again (reason `review`);
1248
+ # the merge-time invalidations below leave no reason and wait for a human.
1249
+ echo review > "$tdir/invalidated.reason"
1090
1250
  echo INVALIDATED > "$tdir/status"; note "$id: artifact changed during review"; return 1
1091
1251
  fi
1092
1252
  local verdict
@@ -1103,43 +1263,11 @@ do_task_body() (
1103
1263
  # pass would merge unreviewed code, so it is fail-safe by construction.
1104
1264
  *) echo UNKNOWN > "$tdir/status" ;;
1105
1265
  esac
1106
- # With an explicit --qa-threshold a FAIL made only of tolerated findings becomes an
1107
- # accepted PASS-with-known-issues. No threshold, no change. qa_thr is the value the review
1108
- # prompt was built with, so the prompt and the gate of one attempt agree.
1266
+ # The gate at the effective threshold: a FAIL made only of tolerated findings becomes an
1267
+ # accepted PASS-with-known-issues, a PASS that lists a blocking CONFIRMED finding becomes
1268
+ # FAIL. qa_thr is the value the review prompt was built with, so the prompt and the gate of
1269
+ # one attempt agree. Before record_attempt, so attempts.tsv shows the gated status.
1109
1270
  qa_gate "$PLAN" "$id" "$tdir" "$verdict" "$qa_thr"
1110
-
1111
- # Annotate a FAIL that looks unsound. Note what this does NOT do: the status file is
1112
- # already written above and nothing below rewrites it. A model that could talk a
1113
- # reviewer out of a FAIL would produce confident PASSes on code nobody checked, which
1114
- # is worth less than having no reviewer at all. Only a human acts on this line.
1115
- if [ "$verdict" = FAIL ] && [ "$(cat "$tdir/status")" = FAIL ]; then
1116
- # do_task is a subshell and does not inherit do_plan's copy, so source it here.
1117
- # A missing or broken options file leaves jev_loaded=0 and this block does nothing.
1118
- local jev_loaded=0 jev_rc
1119
- if [ -f "$SKILL_DIR/scripts/forge-jev-options.sh" ]; then
1120
- source "$SKILL_DIR/scripts/forge-jev-options.sh" 2>/dev/null && jev_loaded=1
1121
- fi
1122
- if [ "$jev_loaded" = 1 ] && forge_jev_active gates; then
1123
- # One request, not two: asking the same question twice costs twice and can come
1124
- # back with two different answers, and the record must match what was printed.
1125
- python3 "$SKILL_DIR/scripts/forge-jev.py" review-triage --review "$tdir/qa.last" \
1126
- --diff "$tdir/changes.diff" --run-dir "$tdir" --json > "$tdir/qa.jev.json" 2>/dev/null
1127
- jev_rc=$?
1128
- if [ "$jev_rc" -eq 0 ]; then
1129
- note "$id: FAIL stands, but jev flags the review — $(python3 -c '
1130
- import json, sys
1131
- try:
1132
- data = json.load(open(sys.argv[1]))
1133
- except Exception:
1134
- sys.exit(0)
1135
- print("; ".join(data.get("reasons") or []))
1136
- ' "$tdir/qa.jev.json" 2>/dev/null)"
1137
- else
1138
- # Exit 1 means the review looked sound; there is nothing to say and no file to keep.
1139
- rm -f "$tdir/qa.jev.json" 2>/dev/null || true
1140
- fi
1141
- fi
1142
- fi
1143
1271
  return 0
1144
1272
  )
1145
1273
 
@@ -1204,17 +1332,86 @@ merge_task() { # merge_task <plan> <id> -> 0 merged, 1 conflicted
1204
1332
  }
1205
1333
 
1206
1334
  # --- retry --------------------------------------------------------------------
1335
+ # The answer arguments of `retry`, found by walking the option list as do_retry's own loop does,
1336
+ # so the value of another option (--setup '--answer') is never mistaken for one. Run it in a
1337
+ # subshell with a scratch directory as the plan: the shared option parsers write into it, not
1338
+ # into the real plan. Leaves n (how many answer flags), flag, val and novalue in the scratch dir.
1339
+ _retry_answer_scan() { # _retry_answer_scan <scratch-dir> <retry args...>
1340
+ local sd="$1"; shift
1341
+ local n=0 flag="" val="" novalue=0 rc=0
1342
+ while [ $# -gt 0 ]; do
1343
+ case "$1" in
1344
+ --no-ripwire|--yolo-dwarf|--yolo-qa) shift ;;
1345
+ --output|--dwarf|--verify|--setup) [ $# -ge 2 ] || break; shift 2 ;;
1346
+ --answer|--answer-file)
1347
+ n=$((n + 1)); flag="$1"
1348
+ if [ $# -ge 2 ]; then val="$2"; shift 2; else novalue=1; shift; fi ;;
1349
+ *)
1350
+ if forge_pp_flag retry "$sd" "$@"; then shift "$PP_SHIFT"; continue; fi
1351
+ if [ "${JEV_OPTS:-0}" = 1 ]; then
1352
+ forge_jev_flag "$@"; rc=$?
1353
+ case "$rc" in
1354
+ 0) shift "$JEV_SHIFT"; continue ;;
1355
+ 2) exit 2 ;;
1356
+ esac
1357
+ fi
1358
+ break ;;
1359
+ esac
1360
+ done
1361
+ printf '%s' "$n" > "$sd/n"; printf '%s' "$flag" > "$sd/flag"; printf '%s' "$val" > "$sd/val"
1362
+ [ "$novalue" = 0 ] || : > "$sd/novalue"
1363
+ }
1364
+
1207
1365
  # A failed task keeps its branch and worktree "so there is something to inspect and
1208
1366
  # retry from" — this is the retry. It is a command a human types, not an automatic
1209
1367
  # repair loop, which is what keeps forge's one-dwarf-then-QA rule intact.
1210
1368
  do_retry() {
1211
1369
  local PLAN="${1:?retry needs a plan dir}" id="${2:?retry needs a task id}"; shift 2
1212
- local NEWDWARF="" jev_rc=0
1370
+ FORGE_RUN_STARTED="$(forge_now)"
1371
+ local NEWDWARF="" jev_rc=0 ANSWER="" ANSWER_FILE="" HAVE_ANSWER=0
1372
+ # An answer is validated before any option below writes into the plan: a rejected answer
1373
+ # changes nothing, --setup and --verify included. Only a NEEDS_INPUT task takes one. The
1374
+ # arguments are walked the way the loop below walks them (a value is never taken for a flag),
1375
+ # in a subshell against a scratch plan dir, so nothing real is written by the walk.
1376
+ local pre_dir="$PLAN/.answer-scan.$$" pre_n=0 pre_have="" pre_val="" pre_novalue=0 pre_ok=0
1377
+ rm -rf "$pre_dir"
1378
+ if mkdir "$pre_dir" 2>/dev/null; then
1379
+ if ( _retry_answer_scan "$pre_dir" "$@" ) >/dev/null 2>&1; then
1380
+ pre_ok=1
1381
+ pre_n="$(<"$pre_dir/n")"; pre_have="$(<"$pre_dir/flag")"; pre_val="$(<"$pre_dir/val")"
1382
+ [ ! -f "$pre_dir/novalue" ] || pre_novalue=1
1383
+ # `$(<f)` drops trailing newlines, so an answer of only newlines needs the size check.
1384
+ [ -s "$pre_dir/val" ] || pre_val=""
1385
+ fi
1386
+ rm -rf "$pre_dir"
1387
+ else
1388
+ die "retry: cannot write to the plan dir $PLAN"
1389
+ fi
1390
+ if [ "$pre_ok" = 1 ] && [ "$pre_n" != 0 ]; then
1391
+ [ "$pre_n" = 1 ] || die "retry: give --answer or --answer-file once"
1392
+ [ "$pre_novalue" = 0 ] || die "retry: $pre_have needs a value"
1393
+ [ -s "$PLAN/tasks.tsv" ] && all_ids "$PLAN/tasks.tsv" | grep -qx "$id" || die "no task '$id' in this plan"
1394
+ local pre_st; pre_st="$(task_status "$PLAN" "$id")"
1395
+ [ "$pre_st" = NEEDS_INPUT ] || die "retry: '$id' is $pre_st, not waiting for an answer; --answer and --answer-file only answer a NEEDS_INPUT task"
1396
+ if [ "$pre_have" = --answer-file ]; then
1397
+ [ -f "$pre_val" ] && [ -r "$pre_val" ] || die "retry: --answer-file '$pre_val' is not a readable file"
1398
+ [ -s "$pre_val" ] || die "retry: --answer-file '$pre_val' is empty"
1399
+ else
1400
+ [ -n "$pre_val" ] || die "retry: the answer is empty"
1401
+ fi
1402
+ fi
1213
1403
  while [ $# -gt 0 ]; do
1214
1404
  case "$1" in
1215
1405
  --no-ripwire) export FORGE_RIPWIRE=off; shift ;;
1216
1406
  --output) OUTPUT="${2:?}"; shift 2 ;;
1217
1407
  --dwarf) NEWDWARF="${2:?--dwarf needs a spec}"; shift 2 ;;
1408
+ # The check above refuses an empty answer first; these hold when its walk could not run.
1409
+ --answer) [ "$HAVE_ANSWER" = 0 ] || die "retry: give --answer or --answer-file once"
1410
+ ANSWER="${2?--answer needs the answer text}"; HAVE_ANSWER=1; shift 2
1411
+ [ -n "$ANSWER" ] || die "retry: the answer is empty" ;;
1412
+ --answer-file) [ "$HAVE_ANSWER" = 0 ] || die "retry: give --answer or --answer-file once"
1413
+ ANSWER_FILE="${2:?--answer-file needs a file}"; HAVE_ANSWER=2; shift 2
1414
+ [ -s "$ANSWER_FILE" ] || die "retry: --answer-file '$ANSWER_FILE' is empty or missing" ;;
1218
1415
  --yolo-dwarf) touch "$PLAN/yolo_dwarf"; shift ;;
1219
1416
  --yolo-qa) touch "$PLAN/yolo_qa"; shift ;;
1220
1417
  --verify) printf '%s' "${2:?--verify needs a command}" > "$PLAN/verify_cmd"; rm -rf "$PLAN/verify.d"; PP_LANES_FRESH=0; shift 2 ;;
@@ -1223,7 +1420,7 @@ do_retry() {
1223
1420
  # `case` is not a loop, so `continue` here belongs to the enclosing while.
1224
1421
  if forge_pp_flag retry "$PLAN" "$@"; then shift "$PP_SHIFT"; continue; fi
1225
1422
  if [ "$JEV_OPTS" = 1 ]; then
1226
- forge_jev_flag "$1"; jev_rc=$?
1423
+ forge_jev_flag "$@"; jev_rc=$?
1227
1424
  case "$jev_rc" in
1228
1425
  0) shift "$JEV_SHIFT"; continue ;;
1229
1426
  2) exit 2 ;;
@@ -1234,6 +1431,8 @@ do_retry() {
1234
1431
  esac
1235
1432
  done
1236
1433
  [ "$JEV_OPTS" = 1 ] && forge_jev_settle "$PLAN"
1434
+ # Before any decision-model step: the privacy floor and the run budget read these.
1435
+ [ "$JEV_OPTS" = 1 ] && [ -s "$PLAN/repo" ] && forge_decide_env "$(cat "$PLAN/repo")" "$PLAN"
1237
1436
  case "$OUTPUT" in summary|full) ;; *) die "--output must be summary or full" ;; esac
1238
1437
  export FORGE_OUTPUT="$OUTPUT"
1239
1438
  local T="$PLAN/tasks.tsv" tdir="$PLAN/tasks/$id"
@@ -1242,6 +1441,18 @@ do_retry() {
1242
1441
  [ -s "$PLAN/wt_root" ] || die "this plan has never been run — use 'run', not 'retry'"
1243
1442
  local st; st="$(task_status "$PLAN" "$id")"
1244
1443
  [ "$st" = "MERGED" ] && die "task '$id' already merged — nothing to retry"
1444
+ [ "$HAVE_ANSWER" = 0 ] || [ "$st" = NEEDS_INPUT ] || die "retry: '$id' is $st, not waiting for an answer; --answer and --answer-file only answer a NEEDS_INPUT task"
1445
+ # The user's answer to a NEEDS_INPUT task's question (validated above): written before
1446
+ # anything else is prepared, atomically, and kept when the preparation then refuses (the
1447
+ # same answer can be retried).
1448
+ if [ "$HAVE_ANSWER" != 0 ]; then
1449
+ if [ "$HAVE_ANSWER" = 2 ]; then
1450
+ cp "$ANSWER_FILE" "$tdir/.answer.md.tmp" || { rm -f "$tdir/.answer.md.tmp"; die "retry: could not write the answer" 1; }
1451
+ else
1452
+ printf '%s\n' "$ANSWER" > "$tdir/.answer.md.tmp" || { rm -f "$tdir/.answer.md.tmp"; die "retry: could not write the answer" 1; }
1453
+ fi
1454
+ mv "$tdir/.answer.md.tmp" "$tdir/answer.md" || die "retry: could not write the answer" 1
1455
+ fi
1245
1456
 
1246
1457
  # Findings carry-forward, the optional --dwarf escalation and the QA-only resume
1247
1458
  # decision are shared with the scheduler's automatic retries.
@@ -1288,42 +1499,11 @@ do_retry() {
1288
1499
  echo
1289
1500
  printf ' %-14s %-9s %s\n' id status branch
1290
1501
  awk -F'\t' '{printf " %-14s %-9s %s\n", $1, $2, $3}' "$PLAN/results.tsv"
1291
- forge_jev_review_flags "$PLAN"
1292
1502
  run_report_extras "$PLAN"
1293
1503
  [ "$(task_status "$PLAN" "$id")" = "MERGED" ] || return 5
1294
1504
  return 0
1295
1505
  }
1296
1506
 
1297
- # Print any Jev review-triage flags under the results table.
1298
- #
1299
- # do_task's notes go to tasks/<id>/task.out, which the scheduler only echoes when
1300
- # FORGE_OUTPUT=full -- the right call for parallel dispatch, where interleaved output is
1301
- # unreadable. But it meant this particular note was written where nobody would read it,
1302
- # and it is the only signal that a FAIL may not be sound. A human has to see it to act
1303
- # on it, and the default output mode is `summary`.
1304
- #
1305
- # qa.jev.json exists only when the review was flagged: review-triage deletes it when the
1306
- # review looks sound, so its presence is the signal and no new file is needed.
1307
- forge_jev_review_flags() {
1308
- local plan="$1" id notice printed=0
1309
- for id in $(all_ids "$plan/tasks.tsv"); do
1310
- [ -s "$plan/tasks/$id/qa.jev.json" ] || continue
1311
- notice="$(python3 -c '
1312
- import json, sys
1313
- try:
1314
- data = json.load(open(sys.argv[1]))
1315
- except Exception:
1316
- sys.exit(0)
1317
- print("; ".join(data.get("reasons") or []))
1318
- ' "$plan/tasks/$id/qa.jev.json" 2>/dev/null)"
1319
- [ -n "$notice" ] || continue
1320
- [ "$printed" = 0 ] && { echo; printed=1; }
1321
- printf ' jev: %s — the FAIL stands; %s\n' "$id" "$notice"
1322
- done
1323
- [ "$printed" = 1 ] && printf ' A flagged review is not an overturned one. Read it before retrying.\n'
1324
- return 0
1325
- }
1326
-
1327
1507
  # One row per execution of the verify command, with its own copy of the log: the
1328
1508
  # deterministic rerun and Jev's retry both write verification.retry.log, so without this
1329
1509
  # only the last of them would survive.
@@ -1417,12 +1597,16 @@ verify_stopped() { # verify_stopped <out>
1417
1597
  verify_mark_interrupted "$1"
1418
1598
  }
1419
1599
 
1420
- verify_result() ( verify_result_body "$@" ) # plan, checkout, artifact directory
1600
+ verify_result() ( verify_result_body "$@" ) # plan, checkout, artifact directory[, fixed source]
1421
1601
 
1422
1602
  # The body is a function of its own so `run` can start it as `( verify_result_body ... ) &`:
1423
1603
  # that one subshell is the background job, the home of the signal handler above and of
1424
1604
  # forge_metric_begin's EXIT trap.
1425
- verify_result_body() { # plan, checkout, artifact directory
1605
+ # An optional fourth argument fixes the verification: "command:<file>" or "lanes:<dir>" (a
1606
+ # finished verification's verification.command or verification.lanes.d/). It replaces
1607
+ # verify_source_for and verify-discover, so a re-run judges by the predicate that verification
1608
+ # used, whatever the plan, the repository or discovery would say now.
1609
+ verify_result_body() { # plan, checkout, artifact directory[, fixed source]
1426
1610
  local plan="$1" wt="$2" out="$3" cmd="" before after rc=0 repo jev_loaded=0 vretries
1427
1611
  FORGE_VERIFY_STOP=0 FORGE_VERIFY_GROUP="" FORGE_VERIFY_TIMER="" FORGE_VERIFY_GRACE=5
1428
1612
  [ "${FORGE_RUN_STOPPABLE:-0}" = 1 ] && trap verify_signal TERM INT
@@ -1442,23 +1626,38 @@ verify_result_body() { # plan, checkout, artifact directory
1442
1626
  vretries="$(cat "$plan/verify_retries" 2>/dev/null)"
1443
1627
  case "$vretries" in ''|*[!0-9]*) vretries=1 ;; esac
1444
1628
  # Sourced unconditionally (cheap: local function/var definitions, no network) so
1445
- # forge_jev_active is available below. verify_result is itself a subshell, so this
1629
+ # forge_decide_active is available below. verify_result is itself a subshell, so this
1446
1630
  # never leaks state into the caller. A missing or broken options file just leaves
1447
1631
  # jev_loaded=0 and every Jev branch below falls through to today's behaviour.
1448
1632
  if [ -f "$SKILL_DIR/scripts/forge-jev-options.sh" ]; then
1449
1633
  source "$SKILL_DIR/scripts/forge-jev-options.sh" 2>/dev/null && jev_loaded=1
1450
1634
  fi
1451
- local vsource; vsource="$(verify_source_for "$plan" "$repo")"
1635
+ local vsource fixed="${4:-}"
1636
+ if [ -n "$fixed" ]; then
1637
+ vsource="$(verify_fixed_source "$fixed")" || { echo UNVERIFIED > "$out/verification.status"
1638
+ note "combined result UNVERIFIED: unusable fixed verification source: $fixed"; return 0; }
1639
+ # A fixed source that is missing or empty is not a reason to look elsewhere.
1640
+ case "$vsource" in
1641
+ lanes:*) verify_lanes_present "${vsource#*$'\t'}" || vsource="none" ;;
1642
+ *) [ -s "${vsource#*$'\t'}" ] || vsource="none" ;;
1643
+ esac
1644
+ if [ "$vsource" = none ]; then
1645
+ echo UNVERIFIED > "$out/verification.status"
1646
+ note "combined result UNVERIFIED: the fixed verification source $fixed is missing or empty"; return 0
1647
+ fi
1648
+ else
1649
+ vsource="$(verify_source_for "$plan" "$repo")"
1650
+ fi
1452
1651
  case "$vsource" in command:*) cmd="$(cat "${vsource#*$'\t'}")" ;; esac
1652
+ # A lanes snapshot belongs to the verification that wrote it: a run on a command, or on
1653
+ # other lanes, leaves none behind (verify_lanes_run makes the new one).
1654
+ case "$vsource" in lanes:*) ;; *) rm -rf "$out/verification.lanes.d" ;; esac
1453
1655
  if [ "$(forge_tree "$wt")" != "$(git -C "$wt" rev-parse HEAD^{tree})" ]; then
1454
1656
  echo FAIL > "$out/verification.status"
1455
1657
  note "verification checkout has source changes outside its commit"; return 1
1456
1658
  fi
1457
1659
  case "$vsource" in
1458
1660
  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
1661
  verify_lanes_run "$plan" "$wt" "$out" "${vsource#*$'\t'}" "$vretries"
1463
1662
  return $? ;;
1464
1663
  esac
@@ -1467,8 +1666,8 @@ verify_result_body() { # plan, checkout, artifact directory
1467
1666
  # produced — any non-zero exit (no key, disabled, nothing suitable, a traceback)
1468
1667
  # falls straight through to the exact UNVERIFIED behaviour below. A Jev failure
1469
1668
  # must never change the outcome of a run.
1470
- if [ -z "$cmd" ] && [ "$jev_loaded" = 1 ] && forge_jev_active tests; then
1471
- local jev_json jev_rc jev_cmd
1669
+ if [ -z "$cmd" ] && [ -z "$fixed" ] && [ "$jev_loaded" = 1 ] && forge_decide_active verify-discover; then
1670
+ local jev_json jev_rc jev_cmd jev_note
1472
1671
  # --json rather than the bare form: the record exists so a human can audit why a
1473
1672
  # command ran, and "confidence: null" answers nothing. The exit code still decides.
1474
1673
  jev_json="$(python3 "$SKILL_DIR/scripts/forge-jev.py" verify-discover --repo "$repo" --json --run-dir "$out" 2>/dev/null)"
@@ -1485,9 +1684,22 @@ except ValueError:
1485
1684
  fi
1486
1685
  if [ -n "$jev_cmd" ]; then
1487
1686
  cmd="$jev_cmd"
1687
+ # Coverage (#42): the pick's evidence names fewer languages than the repo has. The
1688
+ # pick stands and its run still decides; only the announcement says so.
1689
+ jev_note="$(printf '%s' "$jev_json" | python3 -c '
1690
+ import json, sys
1691
+ try:
1692
+ coverage = (json.load(sys.stdin) or {}).get("coverage") or {}
1693
+ except ValueError:
1694
+ coverage = {}
1695
+ covered = coverage.get("covered") or []
1696
+ missing = [language for language in coverage.get("present") or [] if language not in covered]
1697
+ if covered and missing:
1698
+ print(" — it may cover only part of the repo (covers %s; the repo also has %s)" % (", ".join(covered), ", ".join(missing)))
1699
+ ' 2>/dev/null)"
1488
1700
  # Loud on purpose: an unreviewed command is about to run on the user's behalf,
1489
1701
  # and they must never discover that later from a log instead of right here.
1490
- note "jev chose a verification command (no --verify or .forge/verify configured): $cmd"
1702
+ note "jev chose a verification command (no --verify or .forge/verify configured): $cmd$jev_note"
1491
1703
  printf '%s' "$jev_json" | python3 -c '
1492
1704
  import json, sys
1493
1705
  out = sys.argv[1]
@@ -1518,11 +1730,10 @@ with open(out + "/verification.jev.json", "w") as fh:
1518
1730
  verify_stopped "$out" && return 130
1519
1731
  echo "$before" > "$out/verification.fingerprint"
1520
1732
  if [ "$rc" != 0 ] || [ "$before" != "$after" ]; then
1521
- local flaked=0 det_retried=0 n_retry=0 rc3 b3 a3
1522
- # Deterministic rerun first: the rerun itself is the evidence, so unlike Jev's triage
1523
- # below it needs no model and no recorded trap. A tree change is never retried.
1733
+ local flaked=0 n_retry=0 rc3 b3 a3
1734
+ # The rerun itself is the evidence: it needs no model and no recorded trap. A tree
1735
+ # change is never retried.
1524
1736
  if [ "$rc" != 0 ] && [ "$before" = "$after" ] && [ "$vretries" -gt 0 ]; then
1525
- det_retried=1
1526
1737
  while [ "$n_retry" -lt "$vretries" ] && [ "$flaked" = 0 ]; do
1527
1738
  n_retry=$((n_retry+1)); rc3=0
1528
1739
  note "verification failed (exit $rc); rerunning to tell a flaky lane from a regression ($n_retry/$vretries)"
@@ -1544,96 +1755,6 @@ with open(out + "/verification.jev.json", "w") as fh:
1544
1755
  fi
1545
1756
  done
1546
1757
  fi
1547
- # A tree change is never a flake — it stays FAIL immediately, no triage. Beyond
1548
- # that, act on a "known_flake" verdict ONLY when Forge's own memory already
1549
- # records a `trap` for this repo: a model alone deciding a failure was a flake
1550
- # is exactly how a real regression ships, so the verdict needs corroboration
1551
- # from Forge's own records, not just the model's word.
1552
- if [ "$rc" != 0 ] && [ "$before" = "$after" ] && [ "$jev_loaded" = 1 ] \
1553
- && [ "$det_retried" = 0 ] \
1554
- && forge_jev_active tests \
1555
- && [ -s "$repo/.forge/memory.md" ] \
1556
- && awk '/^## Known traps$/{f=1;next} /^## /{f=0} f && NF{c++} END{exit !(c>0)}' "$repo/.forge/memory.md"
1557
- then
1558
- local triage_json triage_rc verdict="" confidence="" flake_act="0.90" parsed corroborated=0 retried=0 rc2="" before2 after2
1559
- triage_json="$(python3 "$SKILL_DIR/scripts/forge-jev.py" verify-triage --repo "$repo" \
1560
- --command "$cmd" --log "$out/verification.log" --json --run-dir "$out" 2>/dev/null)"
1561
- triage_rc=$?
1562
- if [ "$triage_rc" -eq 0 ] && [ -n "$triage_json" ]; then
1563
- parsed="$(printf '%s' "$triage_json" | python3 -c '
1564
- import json, sys
1565
- try:
1566
- data = json.load(sys.stdin)
1567
- except ValueError:
1568
- data = {}
1569
- print(data.get("verdict") or "")
1570
- c = data.get("confidence")
1571
- print(c if c is not None else "")
1572
- print(bool(data.get("corroborated")))
1573
- ' 2>/dev/null)"
1574
- verdict="$(printf '%s\n' "$parsed" | sed -n 1p)"
1575
- confidence="$(printf '%s\n' "$parsed" | sed -n 2p)"
1576
- # triage() reports whether a recorded trap shares a distinctive token with this
1577
- # failure's output. Without that, one flaky test noted months ago would excuse
1578
- # every future failure in the repo.
1579
- [ "$(printf '%s\n' "$parsed" | sed -n 3p)" = "True" ] && corroborated=1 || corroborated=0
1580
- if [ -n "$FORGE_JEV_STATUS_JSON" ]; then
1581
- local threshold_read
1582
- threshold_read="$(printf '%s' "$FORGE_JEV_STATUS_JSON" | python3 -c '
1583
- import json, sys
1584
- try:
1585
- data = json.load(sys.stdin)
1586
- except ValueError:
1587
- data = {}
1588
- v = (data.get("thresholds") or {}).get("flake_act")
1589
- print(v if v is not None else "0.90")
1590
- ' 2>/dev/null)"
1591
- [ -n "$threshold_read" ] && flake_act="$threshold_read"
1592
- fi
1593
- if [ "$verdict" = known_flake ] && [ -n "$confidence" ] && [ "$corroborated" = 1 ] \
1594
- && awk -v a="$confidence" -v b="$flake_act" 'BEGIN{exit !(a+0 >= b+0)}'
1595
- then
1596
- retried=1
1597
- note "jev: known_flake (confidence $confidence >= $flake_act), corroborated by a recorded trap — retrying once"
1598
- before2="$(forge_fingerprint "$wt")" || return 1
1599
- vt0="$(date +%s)"
1600
- verify_stopped "$out" && return 130
1601
- verify_exec "$wt" "$cmd" "$out/verification.retry.log" || rc2=$?
1602
- rc2="${rc2:-0}"
1603
- after2="$(forge_fingerprint "$wt")" || { verify_stopped "$out" && return 130; return 1; }
1604
- verify_run_note "$out" jev_retry "$rc2" "$before2" "$after2" verification.retry.log "$vt0"
1605
- verify_stopped "$out" && return 130
1606
- if [ "$rc2" = 0 ] && [ "$before2" = "$after2" ]; then
1607
- flaked=1
1608
- note "jev: retry passed — treating as a corroborated flake, not a regression"
1609
- else
1610
- note "jev: retry also failed (exit $rc2) — keeping FAIL"
1611
- fi
1612
- fi
1613
- python3 -c '
1614
- import json, sys
1615
- out, verdict, confidence, threshold, corroborated, retried, first_rc, retry_rc = sys.argv[1:9]
1616
- path = out + "/verification.jev.json"
1617
- try:
1618
- with open(path) as fh:
1619
- data = json.load(fh)
1620
- except (OSError, ValueError):
1621
- data = {}
1622
- data["triage"] = {
1623
- "verdict": verdict or None,
1624
- "confidence": float(confidence) if confidence else None,
1625
- "flake_act_threshold": float(threshold),
1626
- "trap_corroboration": corroborated == "1",
1627
- "retried": retried == "1",
1628
- "first_exit": int(first_rc),
1629
- "retry_exit": int(retry_rc) if retry_rc != "" else None,
1630
- }
1631
- with open(path, "w") as fh:
1632
- json.dump(data, fh, indent=2, sort_keys=True)
1633
- fh.write("\n")
1634
- ' "$out" "$verdict" "$confidence" "$flake_act" "$corroborated" "$retried" "$rc" "$rc2" 2>/dev/null || true
1635
- fi
1636
- fi
1637
1758
  if [ "$flaked" = 1 ]; then
1638
1759
  echo PASS > "$out/verification.status"
1639
1760
  return 0
@@ -1688,6 +1809,7 @@ do_run() {
1688
1809
  FORGE_RUN_STOPPABLE=1
1689
1810
  trap run_signal TERM INT
1690
1811
  local PLAN="${1:?run needs a plan dir}"; shift
1812
+ FORGE_RUN_STARTED="$(forge_now)" # the summary's overridden verdicts are this run's
1691
1813
  local fractal_pipeline_args=("$@")
1692
1814
  local MAXP=3 DRY=0 jev_rc=0 FORECAST=1
1693
1815
  while [ $# -gt 0 ]; do
@@ -1709,7 +1831,7 @@ do_run() {
1709
1831
  # `case` is not a loop, so `continue` here belongs to the enclosing while.
1710
1832
  if forge_pp_flag run "$PLAN" "$@"; then shift "$PP_SHIFT"; continue; fi
1711
1833
  if [ "$JEV_OPTS" = 1 ]; then
1712
- forge_jev_flag "$1"; jev_rc=$?
1834
+ forge_jev_flag "$@"; jev_rc=$?
1713
1835
  case "$jev_rc" in
1714
1836
  0) shift "$JEV_SHIFT"; continue ;;
1715
1837
  2) exit 2 ;;
@@ -1732,6 +1854,8 @@ do_run() {
1732
1854
  compute_waves "$PLAN"
1733
1855
  local REPO run_id wt_root int_wt int_br
1734
1856
  REPO="$(cat "$PLAN/repo")"; run_id="$(cat "$PLAN/run_id")"
1857
+ # Before any decision-model step: the privacy floor and the run budget read these.
1858
+ [ "$JEV_OPTS" = 1 ] && forge_decide_env "$REPO" "$PLAN"
1735
1859
  if [ "$DRY" != 1 ]; then
1736
1860
  # The concurrency limit this run was given, for forge-ui to draw; a later run rewrites it.
1737
1861
  printf '%s\n' "$MAXP" > "$PLAN/max_parallel"
@@ -1857,9 +1981,13 @@ do_run() {
1857
1981
  echo "artifacts: $PLAN/tasks/<id>/{dwarf,qa}.{last,log}; results: $PLAN/results.tsv"
1858
1982
  printf ' %-14s %-9s %s\n' id status branch
1859
1983
  awk -F'\t' '{printf " %-14s %-9s %s\n", $1, $2, $3}' "$PLAN/results.tsv"
1860
- forge_jev_review_flags "$PLAN"
1861
1984
  echo
1862
1985
  run_report_extras "$PLAN"
1986
+ # Pre-review tests (#85): every check of the plan, and the reviews the red ones saved.
1987
+ local pre_review
1988
+ pre_review="$(cat "$PLAN"/tasks/*/prereview.log 2>/dev/null | awk -F'\t' 'NF >= 3 { n++; if ($2 == "failed") f++; s += $3 }
1989
+ END { if (n) printf "pre-review tests: %d check(s), %d attempt(s) sent back without a review, %ds of test time\n", n, f, s }')"
1990
+ [ -z "$pre_review" ] || { echo "$pre_review"; echo; }
1863
1991
 
1864
1992
  # Undeclared files are reported here rather than buried in a task log, because
1865
1993
  # this is the list that explains any CONFLICT above.
@@ -1884,10 +2012,21 @@ do_run() {
1884
2012
  for id in $stopped_ids; do echo " forge-parallel.sh retry $PLAN $id"; done
1885
2013
  fi
1886
2014
  [ "$interrupted" = 1 ] || [ -n "$stopped_ids" ] && echo
2015
+ # Tasks stopped for the user's answer: the question as it was asked, and the command that
2016
+ # answers it. Not failures, so the retry hint below does not count them.
2017
+ local waiting; waiting="$(awk -F'\t' '$2=="NEEDS_INPUT" {print $1}' "$PLAN/results.tsv")"
2018
+ if [ -n "$waiting" ]; then
2019
+ echo "waiting for your answer:"
2020
+ for id in $waiting; do
2021
+ printf ' %s: %s\n' "$id" "$(awk 'NF { print substr($0, 1, 120); exit }' "$PLAN/tasks/$id/needs_input.md" 2>/dev/null)"
2022
+ echo " answer with: forge-parallel.sh retry $PLAN $id --answer \"<text>\""
2023
+ done
2024
+ echo
2025
+ fi
1887
2026
  # The retry hint is for tasks that actually failed; a run that only failed verification,
1888
2027
  # 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
2028
+ # has its own hint above, a task waiting for an answer its own block.
2029
+ if [ "$(awk -F'\t' '$2!="MERGED" && $2!="INFRA" && $2!="PENDING" && $2!="BLOCKED" && $2!="INTERRUPTED" && $2!="NEEDS_INPUT" {n++} END {print n+0}' "$PLAN/results.tsv")" -gt 0 ]; then
1891
2030
  echo "retry a failed task with its reviewer's findings:"
1892
2031
  echo " forge-parallel.sh retry $PLAN <task-id> [--dwarf <spec>]"
1893
2032
  echo "or let a run retry them itself: forge-parallel.sh run $PLAN --retry-failed 1"
@@ -1896,6 +2035,20 @@ do_run() {
1896
2035
  if [ "$failed" = 1 ] && [ "$paused" = 0 ] && [ "$interrupted" = 0 ] && [ "$(cat "$PLAN/verification.status" 2>/dev/null)" = FAIL ]; then
1897
2036
  echo "combined verification failed: see $PLAN/verification.log"
1898
2037
  echo
2038
+ if [ -f "$PLAN/attribute_on" ] && declare -F attribute_search >/dev/null; then
2039
+ # This run holds the plan lock: the search function, never the subcommand.
2040
+ run_phase=attribute
2041
+ ( attribute_search "$PLAN" 0 ) <&0 &
2042
+ run_child=$!
2043
+ [ "$run_stop" = 1 ] && kill -TERM "$run_child" 2>/dev/null
2044
+ run_wait "$run_child"
2045
+ run_child=""
2046
+ [ "$run_stop" = 1 ] && interrupted=1
2047
+ echo
2048
+ else
2049
+ echo "find a merge that broke it: forge-parallel.sh attribute $PLAN"
2050
+ echo
2051
+ fi
1899
2052
  fi
1900
2053
 
1901
2054
  if [ ! -f "$PLAN/no_memory" ]; then
@@ -1978,7 +2131,7 @@ if [ "${1:-}" = --help ] || [ "${1:-}" = -h ]; then
1978
2131
  forge_fractal_help
1979
2132
  exit 0
1980
2133
  fi
1981
- [ $# -ge 1 ] || die "usage: forge-parallel.sh <plan|run|retry|accept|review|integrate|split|combine> <plan-dir> [...]"
2134
+ [ $# -ge 1 ] || die "usage: forge-parallel.sh <plan|run|retry|accept|review|integrate|split|attribute|combine> <plan-dir> [...]"
1982
2135
  CMD="$1"; shift
1983
2136
  [ ! -f "${1:-}/no_ripwire" ] || export FORGE_RIPWIRE=off
1984
2137
  if [ -d "${1:-}" ]; then
@@ -1986,7 +2139,7 @@ if [ -d "${1:-}" ]; then
1986
2139
  fi
1987
2140
  case "$CMD" in
1988
2141
  # `combine` takes a plan dir that does not exist yet and locks its sources itself.
1989
- run|retry|integrate|accept|review|split)
2142
+ run|retry|integrate|accept|review|split|attribute)
1990
2143
  if [ "${FORGE_PLAN_LOCK:-}" != "${1:-}" ]; then
1991
2144
  exec python3 "$SKILL_DIR/scripts/forge-schedule.py" lock "$SELF" "$CMD" "$@"
1992
2145
  fi ;;
@@ -1995,7 +2148,7 @@ esac
1995
2148
  # path (die included). Installed after the lock re-exec, so it fires once, in the process
1996
2149
  # that did the work; `_task` and `_merge` are parts of a `run` and are not recorded alone.
1997
2150
  case "$CMD" in
1998
- plan|run|retry|integrate|accept|review|split|combine)
2151
+ plan|run|retry|integrate|accept|review|split|attribute|combine)
1999
2152
  FORGE_DATASET_INVOCATION="inv-$(date +%s)-$$-$RANDOM"; export FORGE_DATASET_INVOCATION
2000
2153
  FORGE_DATASET_STARTED="$(date +%s)"
2001
2154
  FORGE_DATASET_ARGV=("$@")
@@ -2023,6 +2176,7 @@ case "$CMD" in
2023
2176
  accept) need_fn do_accept; do_accept "$@" ;;
2024
2177
  review) need_fn do_review; do_review "$@" ;;
2025
2178
  split) need_fn do_split; do_split "$@" ;;
2179
+ attribute) need_fn do_attribute; do_attribute "$@" ;;
2026
2180
  combine) need_fn do_combine; do_combine "$@" ;;
2027
2181
  _task) do_task "$@" ;;
2028
2182
  _merge) merge_task "$@" ;;