@giamme/forge 0.14.0 → 0.15.0

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 (44) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/SKILL.md +47 -14
  3. package/VERSION +1 -1
  4. package/docs/artifacts.md +2 -1
  5. package/docs/commands.md +301 -7
  6. package/docs/models.md +53 -0
  7. package/package.json +1 -1
  8. package/references/dataset.md +12 -3
  9. package/references/decompose.md +108 -7
  10. package/references/harnesses.md +21 -1
  11. package/references/planning.md +59 -0
  12. package/references/ui.md +39 -1
  13. package/scripts/forge-compare.py +6 -3
  14. package/scripts/forge-config.py +531 -4
  15. package/scripts/forge-dispatch.sh +132 -22
  16. package/scripts/forge-lib-plans.sh +19 -2
  17. package/scripts/forge-lib-review.sh +5 -3
  18. package/scripts/forge-lib-task.sh +119 -0
  19. package/scripts/forge-parallel-options.sh +12 -6
  20. package/scripts/forge-parallel.sh +572 -26
  21. package/scripts/forge-plan-check.py +591 -8
  22. package/scripts/forge-planner.py +480 -0
  23. package/scripts/forge-route.py +72 -23
  24. package/scripts/forge-runtime.py +157 -1
  25. package/scripts/forge-shadow.py +51 -7
  26. package/scripts/forge-solo.sh +81 -7
  27. package/scripts/forge_dataset/cli.py +10 -0
  28. package/scripts/forge_dataset/efforts.py +215 -0
  29. package/scripts/forge_dataset/harvest.py +44 -0
  30. package/scripts/forge_decide/__init__.py +19 -1
  31. package/scripts/forge_decide/builtin_profiles.json +25 -0
  32. package/scripts/forge_decide/cli.py +12 -0
  33. package/scripts/forge_decide/profiles.py +866 -0
  34. package/scripts/forge_decide/resolve.py +38 -6
  35. package/ui/docs/api.md +87 -8
  36. package/ui/forge_ui/control.py +173 -12
  37. package/ui/forge_ui/executions.py +28 -8
  38. package/ui/forge_ui/models.py +60 -1
  39. package/ui/forge_ui/reader.py +21 -1
  40. package/ui/forge_ui/static/leaderboard.js +5 -1
  41. package/ui/forge_ui/static/modelstats.js +25 -1
  42. package/ui/forge_ui/static/panel.js +19 -2
  43. package/ui/forge_ui/static/settings.js +788 -25
  44. package/ui/forge_ui/static/style.css +23 -0
@@ -6,7 +6,11 @@
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
+ # [--qa-threshold P0|P1|P2|P3|none|default] [--profile NAME | --no-profile]
10
+ # [--collapse-chains]
11
+ # forge-parallel.sh auto <plan-dir> --repo <dir> --goal-file <file> [--planner <spec>] [--decompose-level L]
12
+ # [--plan auto|always|never] [--one-task-difficulty low|medium|high]
13
+ # [--no-collapse] [--approved] [plan flags...] [-- run flags...]
10
14
  # forge-parallel.sh run <plan-dir> [--max-parallel N] [--yolo-dwarf] [--yolo-qa]
11
15
  # [--verify <command>] [--setup <command>] [--dry-run]
12
16
  # [--timeout S] [--retry-failed N] [--infra-retries M]
@@ -18,6 +22,7 @@
18
22
  # [--decide retry=N|threshold=Pn | --dwarf <spec>] --reason "<why>" --approved
19
23
  # (a NEEDS_DECISION task)
20
24
  # [--timeout S] [--qa-threshold Pn|none|default] [--verify-retries N]
25
+ # [--retry-failed N] [--infra-retries M]
21
26
  # forge-parallel.sh integrate <plan-dir> --approved [--verify-retries N] [--final-review <qa-spec>]
22
27
  # forge-parallel.sh review <plan-dir> [--qa <spec>] [--yolo-qa] [--qa-threshold Pn|none|default]
23
28
  # forge-parallel.sh attribute <plan-dir> [--force] (find a merge that breaks the failed verification)
@@ -51,11 +56,25 @@
51
56
  # still what the review judged, and starts fresh whenever it cannot;
52
57
  # each attempt is logged in tasks/<id>/implementer_sessions.tsv.
53
58
  # `--no-implementer-session` turns it off (the default)
59
+ # --fast ROLES which of dwarf, qa, planner, review ask for the fast service tier
60
+ # (comma list, or all, or none). Kept in the plan; a re-plan without
61
+ # the flag keeps it, and none removes it. codex and claude only
62
+ # --profile NAME take the routing and policy values of a named profile (forge config
63
+ # profile list) instead of typing them: frozen into the plan as
64
+ # profile.json, shown in the table, and its decision-model gates and
65
+ # budget stay the plan's whatever decide.json says later. A flag of
66
+ # this call replaces just that one value. --no-profile uses none (and
67
+ # removes the plan's frozen copy); with neither, the plan keeps its
68
+ # copy, and a plan that has none gets the default profile, if one is set
54
69
  # --explore E on by default at 0.2 (0 turns it off, and is kept): a row left
55
70
  # to forge gets, with probability E/2, its neighbouring model
56
71
  # (one tier away in the given pools) instead of its pool's turn;
57
72
  # each decision and its odds go to tasks/<id>/route.jsonl
58
- # (precedence: flag, this plan's earlier choice, FORGE_ROUTE_EXPLORE)
73
+ # (precedence: flag, profile, this plan's earlier choice, FORGE_ROUTE_EXPLORE)
74
+ # --collapse-chains this call only (not kept): after the task table is validated and
75
+ # before routing, merge a chain into one task named combined.
76
+ # A one-task or branching plan is left as it is, and collapsing
77
+ # a plan that is already collapsed is not an error
59
78
  #
60
79
  # The QA threshold (plan, run, retry, review; kept for the plan):
61
80
  # --qa-threshold Pn the least severe CONFIRMED finding that still blocks (P0 worst ...
@@ -80,6 +99,28 @@
80
99
  # --attribute when the run's combined verification fails, search the merge order for
81
100
  # a merge that breaks it (kept for the plan; --no-attribute turns it off)
82
101
  #
102
+ # auto (#87, #88): from a goal to a finished run in one blocking command, with no orchestrating
103
+ # session in between. In a NEW plan dir (created when missing; one that already has tasks.tsv
104
+ # is refused, exit 2): the goal is copied verbatim to <plan-dir>/request.md and `auto` is
105
+ # written to <plan-dir>/orchestrator (the plan event in the dataset records it). Then
106
+ # 1. the gate (--plan): auto = forge-planner.py gate; always = plan; never = one task
107
+ # 2. one task: forge-planner.py one-task --difficulty (--one-task-difficulty, default medium);
108
+ # no planner is dispatched
109
+ # 3. planning: the spec is --planner, else the routing.planner of the profile the plan flags
110
+ # select (neither: exit 2). A preflight (forge-dispatch.sh doctor) runs before any model
111
+ # call, the planner is dispatched into the plan dir (with the fast tier when planner is a
112
+ # fast role), and its reply is parsed strictly by forge-planner.py parse
113
+ # 4. `plan` with every flag before `--` that is not auto's own, plus --collapse-chains unless
114
+ # --no-collapse. When its gates send items back (plan.sendback.md, exit 4), the planner is
115
+ # re-dispatched once with that file appended and the reply planned again
116
+ # 5. without --approved: stop, exit 0, after printing the exact `run` command; nothing is
117
+ # dispatched after the planner. With --approved (the user's explicit pre-approval): `run`
118
+ # with the flags after `--`, and auto exits with its code
119
+ # auto exit codes: 0 stopped at approval, or the run's 0 | 2 usage (no planner, a plan dir that
120
+ # already has tasks.tsv, ...) | 4 the planner failed, its reply was refused, or the plan gates
121
+ # sent items back a second time | 8 the planner stopped on an infrastructure failure (nothing
122
+ # was dispatched after it) | otherwise the exit code of the preflight, of `plan` or of `run`
123
+ #
83
124
  # Exit codes: 0 ok | 2 usage/validation | 3 precondition (not a git repo, etc.)
84
125
  # 5 some task failed QA or verification failed | 6 integration conflict
85
126
  # 8 paused on an infrastructure failure (quota, auth, network): no attempt was
@@ -163,11 +204,113 @@ pool_pick() { # pool_pick <comma-list> <index>
163
204
  echo "$last"
164
205
  }
165
206
 
207
+ # --- profiles (#48) -------------------------------------------------------------
208
+ # A named profile is chosen at `plan` (or by forge-solo.sh), frozen as <dir>/profile.json
209
+ # and applied as the values its flags would have set. forge_decide.profiles does the
210
+ # reading, checking and freezing; these functions only carry its answers to the shell.
211
+ forge_profile_py() { PYTHONPATH="$SKILL_DIR/scripts${PYTHONPATH:+:$PYTHONPATH}" python3 -m forge_decide.profiles "$@"; }
212
+
213
+ # Gates and budget come from the frozen copy: FORGE_DECIDE_PROFILE_JSON is read by the
214
+ # decide resolution at its profile level, for this process and every child of it.
215
+ forge_profile_export() { # forge_profile_export <plan-dir>
216
+ local text
217
+ [ -f "${1:-}/profile.json" ] || return 0
218
+ text="$(forge_profile_py decide "$1/profile.json" 2>/dev/null)" || return 0
219
+ [ -z "$text" ] || export FORGE_DECIDE_PROFILE_JSON="$text"
220
+ return 0
221
+ }
222
+
223
+ # Before anything is written: an unknown or invalid profile stops `plan` here (exit 2).
224
+ forge_profile_precheck() { # forge_profile_precheck <plan-dir> <plan's arguments...>
225
+ local plan="$1" name="" none=0 repo="" prev="" arg; shift
226
+ for arg in "$@"; do
227
+ case "$prev" in
228
+ --profile) name="$arg"; prev=""; continue ;;
229
+ --repo) repo="$arg"; prev=""; continue ;;
230
+ esac
231
+ prev=""
232
+ case "$arg" in --profile|--repo) prev="$arg" ;; --no-profile) none=1 ;; esac
233
+ done
234
+ [ -z "$name" ] || [ "$none" = 0 ] || die "--profile and --no-profile are mutually exclusive"
235
+ [ -n "$repo" ] || repo="$(cat "$plan/repo" 2>/dev/null)"
236
+ local flags=()
237
+ [ -z "$name" ] || flags+=(--profile "$name")
238
+ [ "$none" = 0 ] || flags+=(--no-profile)
239
+ forge_profile_py freeze "$plan" ${repo:+--repo "$repo"} ${flags[@]+"${flags[@]}"} --check-only || exit $?
240
+ }
241
+
242
+ # Chooses, freezes and applies the plan's profile. Reads and sets do_plan's locals (dynamic
243
+ # scope, as forge_fast_parse does): a value a flag of this call gave is never replaced.
244
+ forge_profile_plan() {
245
+ local args=() out key val repo
246
+ [ -z "$D_ANY" ] || args+=(--given routing.dwarf.any)
247
+ [ -z "$D_LO" ] || args+=(--given routing.dwarf.low)
248
+ [ -z "$D_MD" ] || args+=(--given routing.dwarf.medium)
249
+ [ -z "$D_HI" ] || args+=(--given routing.dwarf.high)
250
+ [ -z "$Q_ANY" ] || args+=(--given routing.qa.any)
251
+ [ -z "$Q_LO" ] || args+=(--given routing.qa.low)
252
+ [ -z "$Q_MD" ] || args+=(--given routing.qa.medium)
253
+ [ -z "$Q_HI" ] || args+=(--given routing.qa.high)
254
+ [ -z "$PLANNER" ] || args+=(--given routing.planner)
255
+ [ -z "$SHADOW_QA" ] || args+=(--given policy.shadow_qa)
256
+ [ -z "$SHADOW_RATE" ] || args+=(--given policy.shadow_rate)
257
+ [ -z "$EXPLORE" ] || args+=(--given policy.explore)
258
+ [ "$FAST_GIVEN" != 1 ] || args+=(--given policy.fast)
259
+ [ "$PF_QT" != 1 ] || args+=(--given policy.qa_threshold)
260
+ [ -z "$PF_NAME" ] || args+=(--profile "$PF_NAME")
261
+ [ "$PF_NONE" = 0 ] || args+=(--no-profile)
262
+ repo="$REPO"; [ -n "$repo" ] || repo="$(cat "$PLAN/repo" 2>/dev/null)"
263
+ out="$(forge_profile_py freeze "$PLAN" ${repo:+--repo "$repo"} ${args[@]+"${args[@]}"})" || exit $?
264
+ while IFS="$(printf '\t')" read -r key val; do
265
+ case "$key" in
266
+ routing.dwarf.any) D_ANY="$val" ;;
267
+ routing.dwarf.low) D_LO="$val" ;;
268
+ routing.dwarf.medium) D_MD="$val" ;;
269
+ routing.dwarf.high) D_HI="$val" ;;
270
+ routing.qa.any) Q_ANY="$val" ;;
271
+ routing.qa.low) Q_LO="$val" ;;
272
+ routing.qa.medium) Q_MD="$val" ;;
273
+ routing.qa.high) Q_HI="$val" ;;
274
+ routing.planner) PLANNER="$val" ;;
275
+ policy.shadow_qa) SHADOW_QA="$val" ;;
276
+ policy.shadow_rate) SHADOW_RATE="$val" ;;
277
+ policy.explore) EXPLORE="$val" ;;
278
+ policy.fast) forge_fast_parse "$val" plan; FAST_GIVEN=1 ;;
279
+ esac
280
+ done <<EOF
281
+ $out
282
+ EOF
283
+ forge_profile_export "$PLAN"
284
+ }
285
+
286
+ # What `run` reads from the frozen profile when its flags do not say: the retry budgets,
287
+ # and the Fractal planner (only with --fractal-auto-decompose, which --fractal-planner needs).
288
+ forge_profile_run_values() { # forge_profile_run_values <plan-dir>
289
+ local key val arg auto=0 planner=0
290
+ [ -f "$1/profile.json" ] || return 0
291
+ for arg in ${FRACTAL_ARGS[@]+"${FRACTAL_ARGS[@]}"}; do
292
+ case "$arg" in --fractal-auto-decompose) auto=1 ;; --fractal-planner) planner=1 ;; esac
293
+ done
294
+ while IFS="$(printf '\t')" read -r key val; do
295
+ case "$key" in
296
+ policy.retry_failed) [ "$PP_RETRY_FAILED_SET" = 1 ] || PP_RETRY_FAILED="$val" ;;
297
+ policy.infra_retries) [ "$PP_INFRA_RETRIES_SET" = 1 ] || PP_INFRA_RETRIES="$val" ;;
298
+ routing.fractal_planner) [ "$auto$planner" != 10 ] || FRACTAL_ARGS+=(--fractal-planner "$val") ;;
299
+ esac
300
+ done <<EOF
301
+ $(forge_profile_py value "$1/profile.json" policy.retry_failed policy.infra_retries routing.fractal_planner 2>/dev/null)
302
+ EOF
303
+ }
304
+
166
305
  do_plan() {
167
306
  local PLAN="${1:?plan needs a plan dir}"; shift
307
+ local PF_NAME="" PF_NONE=0 PF_QT=0
308
+ forge_profile_precheck "$PLAN" "$@"
168
309
  local REPO="" D_ANY="" D_HI="" D_MD="" D_LO="" Q_ANY="" Q_HI="" Q_MD="" Q_LO=""
169
310
  PP_INFRA_FALLBACK=""
170
311
  local PLANNER="" NOMEM=0 jev_rc=0 DLEVEL="" SHADOW_QA="" SHADOW_RATE="" EXPLORE="" FAIL_FIRST="" REVIEWER_SESSION="" IMPLEMENTER_SESSION="" PRE_REVIEW="" PRE_REVIEW_PARALLEL=""
312
+ local FAST_GIVEN=0 FAST_PARSED=""
313
+ local COLLAPSE=0 COLLAPSED_NOW=0
171
314
  while [ $# -gt 0 ]; do
172
315
  case "$1" in
173
316
  --repo) REPO="${2:?}"; shift 2 ;;
@@ -177,6 +320,8 @@ do_plan() {
177
320
  *) die "--decompose-level must be low, medium or high" ;; esac ;;
178
321
  --no-ripwire) export FORGE_RIPWIRE=off; shift ;;
179
322
  --no-memory) NOMEM=1; shift ;;
323
+ --profile) PF_NAME="${2:?--profile needs a profile name}"; shift 2 ;;
324
+ --no-profile) PF_NONE=1; shift ;;
180
325
  --shadow-qa)
181
326
  [ -n "${2:-}" ] || die "--shadow-qa needs a QA spec, a comma list of specs, or none"
182
327
  SHADOW_QA="$2"; shift 2 ;;
@@ -184,6 +329,7 @@ do_plan() {
184
329
  printf '%s' "${2:-}" | grep -Eq '^(0?\.[0-9]+|0|1|1\.0+)$' || die "--shadow-rate needs a share between 0 and 1"
185
330
  SHADOW_RATE="$2"; shift 2 ;;
186
331
  --explore) EXPLORE="${2:?--explore needs a share between 0 and 0.5}"; shift 2 ;;
332
+ --collapse-chains) COLLAPSE=1; shift ;;
187
333
  --dwarf) D_ANY="${2:?}"; shift 2 ;;
188
334
  --dwarf-high) D_HI="${2:?}"; shift 2 ;;
189
335
  --dwarf-medium) D_MD="${2:?}"; shift 2 ;;
@@ -205,11 +351,19 @@ do_plan() {
205
351
  --no-reviewer-session) REVIEWER_SESSION=off; shift ;;
206
352
  --implementer-session) IMPLEMENTER_SESSION=on; shift ;;
207
353
  --no-implementer-session) IMPLEMENTER_SESSION=off; shift ;;
354
+ --fast)
355
+ [ -n "${2:-}" ] || die "--fast needs a comma list of roles (dwarf, qa, planner, review), all or none"
356
+ forge_fast_parse "$2" plan
357
+ FAST_GIVEN=1
358
+ shift 2 ;;
208
359
  *)
209
360
  # `case` is not a loop, so `continue` here belongs to the enclosing while.
210
361
  # The shared flags write into the plan dir (--qa-threshold), so it must exist first.
211
362
  [ -d "$PLAN" ] || die "plan dir '$PLAN' does not exist"
212
- if forge_pp_flag plan "$PLAN" "$@"; then shift "$PP_SHIFT"; continue; fi
363
+ if forge_pp_flag plan "$PLAN" "$@"; then
364
+ [ "$1" != --qa-threshold ] || PF_QT=1
365
+ shift "$PP_SHIFT"; continue
366
+ fi
213
367
  if [ "$JEV_OPTS" = 1 ]; then
214
368
  forge_jev_flag "$@"; jev_rc=$?
215
369
  case "$jev_rc" in
@@ -221,8 +375,11 @@ do_plan() {
221
375
  ;;
222
376
  esac
223
377
  done
224
- [ "$JEV_OPTS" = 1 ] && forge_jev_export
225
378
  [ -d "$PLAN" ] || die "plan dir '$PLAN' does not exist"
379
+ # The profile (#48) first: its gates and budget count for every decision-model step below,
380
+ # and its values stand in for the flags this call did not give.
381
+ forge_profile_plan
382
+ [ "$JEV_OPTS" = 1 ] && forge_jev_export
226
383
  # Freeze the selection next to the task table, so run and retry restore it rather
227
384
  # than re-deciding. Written on every plan: a re-plan is a new decision.
228
385
  [ "$JEV_OPTS" = 1 ] && forge_jev_record "$PLAN"
@@ -258,6 +415,8 @@ do_plan() {
258
415
  case "$REVIEWER_SESSION" in on) printf '%s\n' on > "$PLAN/reviewer_session" ;; off) rm -f "$PLAN/reviewer_session" ;; esac
259
416
  # Implementer-session continuation (opt-in, #90): the same `on` file, read the same way.
260
417
  case "$IMPLEMENTER_SESSION" in on) printf '%s\n' on > "$PLAN/implementer_session" ;; off) rm -f "$PLAN/implementer_session" ;; esac
418
+ # Which roles ask for the fast tier. Omitted on a re-plan, so an earlier choice stays.
419
+ forge_fast_store "$PLAN"
261
420
  # Exploration is decided here and only here, so the table and preflight see what will run.
262
421
  # On by default: choosing models from measured pass rates needs some randomized picks, and
263
422
  # history had almost none (9 tasks ever ran on more than one spec). The flag wins, then
@@ -383,7 +542,46 @@ EOF
383
542
  fi
384
543
  fi
385
544
 
386
- # --- validate + resolve routing ---
545
+ # --- collapse a chain, then resolve routing ---
546
+ # --collapse-chains acts on this call only. The gates have already run (drift may have
547
+ # added files). Validate, then collapse, then routing fills combined's `-` columns, and
548
+ # the declared copy is taken again so the declared tier is the combined task's. A second
549
+ # collapse (tasks.precollapse.tsv or collapsed.json already there) is not an error.
550
+ if [ "$COLLAPSE" = 1 ]; then
551
+ local vline vid vdeps vdiff vd
552
+ while IFS= read -r vline; do
553
+ case "$vline" in ''|'#'*) continue ;; esac
554
+ vid="$(printf '%s' "$vline" | awk -F'\t' '{print $1}')"
555
+ vdeps="$(printf '%s' "$vline" | awk -F'\t' '{print $2}')"
556
+ vdiff="$(printf '%s' "$vline" | awk -F'\t' '{print $3}')"
557
+ [ -n "$vid" ] || die "tasks.tsv has a row with no id"
558
+ case "$vdiff" in low|medium|high) ;; *) die "task '$vid' has difficulty '$vdiff' (expected low, medium or high)" ;; esac
559
+ if [ "$vdeps" != "-" ]; then
560
+ for vd in $(printf '%s' "$vdeps" | tr ',' ' '); do
561
+ all_ids "$T" | grep -qx "$vd" || die "task '$vid' depends on unknown task '$vd'"
562
+ done
563
+ fi
564
+ done < "$T"
565
+ local collapse_out collapse_err collapse_rc=0 collapse_msg
566
+ collapse_err="$PLAN/.collapse.err"
567
+ collapse_out="$(python3 "$SKILL_DIR/scripts/forge-plan-check.py" collapse "$PLAN" 2>"$collapse_err")" || collapse_rc=$?
568
+ collapse_msg="$(cat "$collapse_err" 2>/dev/null || true)"
569
+ rm -f "$collapse_err"
570
+ if [ "$collapse_rc" -ne 0 ]; then
571
+ case "$collapse_msg" in
572
+ *already\ exists*) ;;
573
+ *) die "could not collapse the plan${collapse_msg:+: $collapse_msg}" ;;
574
+ esac
575
+ else
576
+ case "$collapse_out" in
577
+ collapsed\ *)
578
+ cp "$T" "$PLAN/tasks.declared.tsv" || die "could not rewrite $PLAN/tasks.declared.tsv"
579
+ COLLAPSED_NOW=1
580
+ ;;
581
+ esac
582
+ fi
583
+ fi
584
+
387
585
  local tmp="$PLAN/.tasks.tsv.new"; : > "$tmp"
388
586
  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
389
587
  printf '# id\tdeps\tdifficulty\tfiles\tdwarf\tqa\ttitle\n' > "$tmp"
@@ -422,11 +620,13 @@ EOF
422
620
  # Exploration (--explore): now and then the neighbouring tier's model instead, with the
423
621
  # odds recorded, so `forge dataset routing` can compare two models on tasks neither was
424
622
  # routed to by design. The pool's turn above was still taken, so other rows are unchanged.
425
- if [ -n "$EXPLORE" ] && [ "$dw" != UNASSIGNED ]; then
426
- local explored
427
- explored="$(python3 "$SKILL_DIR/scripts/forge-route.py" explore --plan "$PLAN" --task "$id" \
428
- --tier "$diff" --epsilon "$EXPLORE" --default "$dw" --pools "$PLAN/fractal-routing.json" 2>/dev/null)"
429
- [ -z "$explored" ] || dw="$explored"
623
+ # Explored or not, the pick's tier is recorded (running.tier), so an escalation or pin that
624
+ # this re-plan replaces no longer says which tier a lateral move stays in (#106).
625
+ if [ "$dw" != UNASSIGNED ]; then
626
+ local assigned
627
+ assigned="$(python3 "$SKILL_DIR/scripts/forge-route.py" assign --plan "$PLAN" --task "$id" \
628
+ --tier "$diff" --epsilon "${EXPLORE:-0}" --default "$dw" --pools "$PLAN/fractal-routing.json" 2>/dev/null)"
629
+ [ -z "$assigned" ] || dw="$assigned"
430
630
  fi
431
631
  fi
432
632
  # resolve qa: the same grammar as the dwarf, a comma-list round-robins within its tier.
@@ -448,6 +648,11 @@ EOF
448
648
  printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$id" "$deps" "$diff" "$files" "$dw" "$qa" "$title" >> "$tmp"
449
649
  done < "$T"
450
650
  mv "$tmp" "$T"
651
+ # Which cells routing filled in, so a re-plan under another profile replaces those and no others.
652
+ # Kept current through a --no-profile re-plan too, once a profile has recorded any.
653
+ if [ -f "$PLAN/profile.json" ] || [ -f "$PLAN/routed.json" ]; then
654
+ forge_profile_py routed "$PLAN" 2>/dev/null || true
655
+ fi
451
656
 
452
657
  compute_waves "$PLAN"
453
658
 
@@ -491,10 +696,22 @@ EOF
491
696
  fi
492
697
  fi
493
698
 
699
+ # After the last wave computation (a forward dep may have just changed it). Advisory:
700
+ # a failure is a note, and the table is still printed.
701
+ local shape_err="" shape_rc=0
702
+ shape_err="$(python3 "$SKILL_DIR/scripts/forge-plan-check.py" shape "$PLAN" --write 2>&1 >/dev/null)" || shape_rc=$?
703
+ if [ "$shape_rc" -ne 0 ]; then
704
+ note "plan shape could not be recorded (advisory)${shape_err:+: $shape_err}"
705
+ fi
706
+
494
707
  render_table "$PLAN"
495
708
  # What the gates did, from plan-gates.json: files and deps they added, prompts and
496
709
  # requirements still flagged. Prints nothing when no gate recorded anything.
497
710
  python3 "$SKILL_DIR/scripts/forge-plan-check.py" gates "$PLAN" 2>/dev/null || true
711
+ # Advisory, under the table: a spec measurably slower than a lower effort of the same model,
712
+ # once per distinct spec (doctor --spec says the same). Read from the dataset for the
713
+ # operator's screen only, so not by forge-plan-check.py, which builds a prompt.
714
+ python3 "$SKILL_DIR/scripts/forge-dataset.py" slow-notes "$PLAN" 2>/dev/null || true
498
715
  # Advisory, under the table: a privacy floor that silences every gate, and which
499
716
  # gates this plan's runs will ask. Prints nothing when it cannot tell.
500
717
  [ "$jev_loaded" = 1 ] && forge_decide_plan_lines "${FORGE_DECIDE_REPO:-$REPO}"
@@ -592,6 +809,15 @@ render_table() {
592
809
  if [ -f "$PLAN/implementer_session" ]; then
593
810
  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)"
594
811
  fi
812
+ if [ -s "$PLAN/fast" ]; then
813
+ local fast_roles
814
+ fast_roles="$(awk 'NF { printf "%s%s", (n++ ? ", " : ""), $0 }' "$PLAN/fast")"
815
+ echo "fast tier: $fast_roles — billed at a higher rate (codex service_tier=fast; claude fast mode needs extra usage enabled)"
816
+ fi
817
+ if [ -f "$PLAN/profile.json" ]; then
818
+ local profile_line; profile_line="$(forge_profile_py line "$PLAN/profile.json" 2>/dev/null)"
819
+ [ -z "$profile_line" ] || echo "$profile_line"
820
+ fi
595
821
  # The plan-level threshold: at plan time no per-task file exists yet.
596
822
  local qa_level qa_src
597
823
  read -r qa_level qa_src <<EOF
@@ -629,6 +855,22 @@ EOF
629
855
  done
630
856
  echo
631
857
  done
858
+ # A chain collapsed on this call, or a chain left as a chain. one and branches say nothing.
859
+ # COLLAPSED_NOW is do_plan's: collapsed.json from an earlier plan is not announced again.
860
+ local shape_note=""
861
+ if [ "${COLLAPSED_NOW:-0}" = 1 ] && [ -f "$PLAN/collapsed.json" ]; then
862
+ shape_note="$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print("collapsed %d" % len(d.get("from") or []))' "$PLAN/collapsed.json" 2>/dev/null)" || shape_note=""
863
+ elif [ -f "$PLAN/plan-shape.json" ]; then
864
+ shape_note="$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print("%s %s" % (d.get("shape"), d.get("tasks")))' "$PLAN/plan-shape.json" 2>/dev/null)" || shape_note=""
865
+ fi
866
+ case "$shape_note" in
867
+ collapsed\ *)
868
+ echo "collapsed a sequential plan of ${shape_note#collapsed } tasks into one (tasks.precollapse.tsv keeps the original)"
869
+ echo ;;
870
+ chain\ *)
871
+ echo "this plan runs sequentially: ${shape_note#chain } tasks, at most 1 in parallel; each pays its own implementation and review. One task would run once: re-plan with --collapse-chains"
872
+ echo ;;
873
+ esac
632
874
  if [ "$starred" = 1 ]; then
633
875
  echo "* picked by exploratory routing (--explore $(cat "$PLAN/explore" 2>/dev/null)) instead of the pool's turn;"
634
876
  echo " to pin another model, put its spec in the dwarf column of tasks.tsv and re-plan"
@@ -677,27 +919,37 @@ dependencies_ready() {
677
919
  }
678
920
 
679
921
  preflight_plan() {
680
- local plan="$1" id role flags
922
+ local plan="$1" id role flags ft spec key seen=";"
681
923
  export FORGE_CAPABILITY_CACHE="$plan/capabilities"
682
924
  for id in $(all_ids "$plan/tasks.tsv"); do
683
925
  for role in dwarf qa; do
684
926
  flags=""; [ -f "$plan/yolo_$role" ] && flags=--yolo
685
- /bin/bash "$DISPATCH" doctor --spec "$(field "$plan/tasks.tsv" "$id" "$role")" --role "$role" $flags || return $?
927
+ ft="$(forge_fast_tier "$plan" "$role")"
928
+ spec="$(field "$plan/tasks.tsv" "$id" "$role")"
929
+ # Once per spec, role and options: the same arguments get the same answer, and a note doctor
930
+ # adds (a slow effort) is said once, not once per task.
931
+ key="$role|$spec|$flags|$ft"
932
+ case "$seen" in *";$key;"*) continue ;; esac
933
+ seen="$seen$key;"
934
+ /bin/bash "$DISPATCH" doctor --spec "$spec" --role "$role" $flags $ft || return $?
686
935
  done
687
936
  done
688
937
  if [ -s "$plan/planner" ]; then
689
- /bin/bash "$DISPATCH" doctor --spec "$(cat "$plan/planner")" --role planner || return $?
938
+ ft="$(forge_fast_tier "$plan" planner)"
939
+ /bin/bash "$DISPATCH" doctor --spec "$(cat "$plan/planner")" --role planner $ft || return $?
690
940
  fi
691
941
  # A shadow reviewer that cannot run here is dropped with a note, never a failed run.
942
+ # A fast-tier refusal is the exception: dispatch already refused it, and skipping the
943
+ # shadow would let the rest of the run be billed. That exit stops the run.
692
944
  if [ -s "$plan/shadow_qa" ]; then
693
- python3 "$SKILL_DIR/scripts/forge-shadow.py" preflight --plan "$plan" || true
945
+ python3 "$SKILL_DIR/scripts/forge-shadow.py" preflight --plan "$plan" || return $?
694
946
  fi
695
947
  }
696
948
 
697
949
  # The implementer a sweep task gets when the plan has a low tier (or a plain --dwarf pool): checked
698
950
  # with the rest, before any model is paid for, since no task may have used it yet.
699
951
  sweep_preflight() { # sweep_preflight <plan>
700
- local spec flags=""
952
+ local spec flags="" ft
701
953
  spec="$(python3 -c 'import json, sys
702
954
  try:
703
955
  data = json.load(open(sys.argv[1]))
@@ -709,7 +961,8 @@ for tier in ("low", "any"):
709
961
  print(specs[0]); break' "$1/fractal-routing.json" 2>/dev/null)"
710
962
  [ -n "$spec" ] || return 0
711
963
  [ -f "$1/yolo_dwarf" ] && flags=--yolo
712
- /bin/bash "$DISPATCH" doctor --spec "$spec" --role dwarf $flags
964
+ ft="$(forge_fast_tier "$1" dwarf)"
965
+ /bin/bash "$DISPATCH" doctor --spec "$spec" --role dwarf $flags $ft
713
966
  }
714
967
 
715
968
  sweep_plan() { # sweep_plan <plan> --severity Pn [--plausible] [--max N]: the scheduler's _sweep
@@ -893,6 +1146,7 @@ do_task_body() (
893
1146
  if [ -s "$PLAN/fractal-selection.json" ]; then
894
1147
  forge_fractal_select "$PLAN" "$REPO" parallel 0 || return $?
895
1148
  fi
1149
+ forge_fast_fractal_note "$PLAN"
896
1150
  forge_metric_begin "$tdir" task
897
1151
  dependencies_ready "$PLAN" "$id" || return 1
898
1152
  echo RUNNING > "$tdir/status"
@@ -934,11 +1188,14 @@ do_task_body() (
934
1188
  base="$(git -C "$(cat "$PLAN/wt_root")/_integration" rev-parse HEAD)"; printf '%s\n' "$base" > "$tdir/base_ref"
935
1189
  fi
936
1190
 
937
- local dw qa yd yq rc
1191
+ local dw qa yd yq rc ft_dwarf ft_qa
938
1192
  dw="$(field "$T" "$id" dwarf)"; qa="$(field "$T" "$id" qa)"
939
1193
  yd=""; yq=""
940
1194
  [ -f "$PLAN/yolo_dwarf" ] && yd="--yolo"
941
1195
  [ -f "$PLAN/yolo_qa" ] && yq="--yolo"
1196
+ # Empty when the role is not listed, so the recorded command line is unchanged.
1197
+ ft_dwarf="$(forge_fast_tier "$PLAN" dwarf)"
1198
+ ft_qa="$(forge_fast_tier "$PLAN" qa)"
942
1199
 
943
1200
  if ! ensure_worktree "$REPO" "$wt" "$br" "$base" "$tdir" "$PLAN" "$id"; then
944
1201
  echo ERROR > "$tdir/status"
@@ -981,7 +1238,7 @@ do_task_body() (
981
1238
  is_reason=work-changed
982
1239
  else
983
1240
  FORGE_CAPABILITY_CACHE="${FORGE_CAPABILITY_CACHE:-$PLAN/capabilities}" \
984
- /bin/bash "$DISPATCH" doctor --spec "$dw" --role dwarf $yd > "$tdir/dwarf.doctor" 2>&1 || : > "$tdir/dwarf.doctor"
1241
+ /bin/bash "$DISPATCH" doctor --spec "$dw" --role dwarf $yd $ft_dwarf > "$tdir/dwarf.doctor" 2>&1 || : > "$tdir/dwarf.doctor"
985
1242
  IFS=$'\t' read -r is_mode is_reason is_id < <(review_session_decide \
986
1243
  "$tdir/retry_findings.dwarf.session" "$tdir/retry_findings.dwarf.resolved" "$tdir/dwarf.doctor" "claude openclaude")
987
1244
  if [ "$is_mode" = continued ]; then
@@ -992,7 +1249,7 @@ do_task_body() (
992
1249
  fi
993
1250
  forge_metric_phase dispatch
994
1251
  local dwarf_cmd=(/bin/bash "$DISPATCH" dwarf "$dw" --repo "$wt" --run-dir "$tdir" \
995
- --ripwire-query-file "$tdir/prompt.md" --prompt-file "$tdir/dwarf.input" ${yd:+"$yd"} --output "${FORGE_OUTPUT:-summary}")
1252
+ --ripwire-query-file "$tdir/prompt.md" --prompt-file "$tdir/dwarf.input" ${yd:+"$yd"} $ft_dwarf --output "${FORGE_OUTPUT:-summary}")
996
1253
  "${dwarf_cmd[@]}" ${is_cont[@]+"${is_cont[@]}"} >"$tdir/dwarf.out" 2>&1
997
1254
  rc=$?
998
1255
  if [ "$rc" = 9 ] && [ "$is_mode" = continued ]; then
@@ -1215,14 +1472,14 @@ do_task_body() (
1215
1472
  rs_reason=first-round
1216
1473
  else
1217
1474
  FORGE_CAPABILITY_CACHE="${FORGE_CAPABILITY_CACHE:-$PLAN/capabilities}" \
1218
- /bin/bash "$DISPATCH" doctor --spec "$qa" --role qa $yq > "$tdir/qa.doctor" 2>&1 || : > "$tdir/qa.doctor"
1475
+ /bin/bash "$DISPATCH" doctor --spec "$qa" --role qa $yq $ft_qa > "$tdir/qa.doctor" 2>&1 || : > "$tdir/qa.doctor"
1219
1476
  IFS=$'\t' read -r rs_mode rs_reason rs_id < <(review_session_decide \
1220
1477
  "$tdir/retry_findings.session" "$tdir/retry_findings.resolved" "$tdir/qa.doctor")
1221
1478
  [ "$rs_mode" = continued ] && rs_cont=(--continue-session "$rs_id")
1222
1479
  fi
1223
1480
  fi
1224
1481
  local qa_cmd=(/bin/bash "$DISPATCH" qa "$qa" --repo "$review" --run-dir "$tdir" \
1225
- --review-base "$(cat "$tdir/review.base")" --ripwire-query-file "$tdir/prompt.md" --prompt-file "$tdir/qa.input" ${yq:+"$yq"} --output "${FORGE_OUTPUT:-summary}")
1482
+ --review-base "$(cat "$tdir/review.base")" --ripwire-query-file "$tdir/prompt.md" --prompt-file "$tdir/qa.input" ${yq:+"$yq"} $ft_qa --output "${FORGE_OUTPUT:-summary}")
1226
1483
  "${qa_cmd[@]}" ${rs_cont[@]+"${rs_cont[@]}"} >"$tdir/qa.out" 2>&1
1227
1484
  rc=$?
1228
1485
  if [ "$rc" = 9 ] && [ "$rs_mode" = continued ]; then
@@ -1395,6 +1652,31 @@ _retry_answer_scan() { # _retry_answer_scan <scratch-dir> <retry args...>
1395
1652
  # A failed task keeps its branch and worktree "so there is something to inspect and
1396
1653
  # retry from" — this is the retry. It is a command a human types, not an automatic
1397
1654
  # repair loop, which is what keeps forge's one-dwarf-then-QA rule intact.
1655
+ # The wait before `retry --infra-retries` dispatches again after an infrastructure stop: the provider's
1656
+ # retry-after when it gave one, else the plan's infra_backoff (the scheduler's own rule, its functions).
1657
+ retry_infra_wait() { # retry_infra_wait <plan> <task-dir> <cycle>
1658
+ local secs
1659
+ secs="$(python3 - "$SKILL_DIR/scripts/forge-schedule.py" "$1" "$2" "$3" <<'PY' 2>/dev/null
1660
+ import importlib.util, sys
1661
+ from pathlib import Path
1662
+ spec = importlib.util.spec_from_file_location('forge_schedule', sys.argv[1])
1663
+ module = importlib.util.module_from_spec(spec)
1664
+ sys.modules['forge_schedule'] = module
1665
+ spec.loader.exec_module(module)
1666
+ backoff = None
1667
+ try:
1668
+ text = (Path(sys.argv[2]) / 'infra_backoff').read_text().strip()
1669
+ backoff = (module.parse_backoff(text), text) if text else None
1670
+ except (OSError, ValueError):
1671
+ pass
1672
+ print(module.infra_wait(module.infra_info(Path(sys.argv[3])), int(sys.argv[4]), backoff)[0])
1673
+ PY
1674
+ )" || secs=""
1675
+ case "$secs" in ''|*[!0-9.]*) secs=60 ;; esac
1676
+ note "waiting ${secs}s for the infrastructure stop to clear"
1677
+ sleep "$secs"
1678
+ }
1679
+
1398
1680
  do_retry() {
1399
1681
  local PLAN="${1:?retry needs a plan dir}" id="${2:?retry needs a task id}"; shift 2
1400
1682
  FORGE_RUN_STARTED="$(forge_now)"
@@ -1471,6 +1753,7 @@ do_retry() {
1471
1753
  ;;
1472
1754
  esac
1473
1755
  done
1756
+ forge_profile_run_values "$PLAN"
1474
1757
  [ "$JEV_OPTS" = 1 ] && forge_jev_settle "$PLAN"
1475
1758
  # Before any decision-model step: the privacy floor and the run budget read these.
1476
1759
  [ "$JEV_OPTS" = 1 ] && [ -s "$PLAN/repo" ] && forge_decide_env "$(cat "$PLAN/repo")" "$PLAN"
@@ -1482,6 +1765,14 @@ do_retry() {
1482
1765
  [ -s "$PLAN/wt_root" ] || die "this plan has never been run — use 'run', not 'retry'"
1483
1766
  local st; st="$(task_status "$PLAN" "$id")"
1484
1767
  [ "$st" = "MERGED" ] && die "task '$id' already merged — nothing to retry"
1768
+ # A decision's grant is exactly what it says, and the profile's default budget of further attempts
1769
+ # does not stretch it: a task waiting for a decision (resumed by --decide or --dwarf), or an
1770
+ # infrastructure stop resumed while a decision's count is kept. Only a --retry-failed typed on this
1771
+ # line adds to a decision.
1772
+ if [ "$PP_RETRY_FAILED_SET" != 1 ] && { [ -n "$DECIDE" ] || [ "$st" = NEEDS_DECISION ] \
1773
+ || { [ "$st" = INFRA ] && [ -n "$(decided_left "$tdir")" ]; }; }; then
1774
+ PP_RETRY_FAILED=0
1775
+ fi
1485
1776
  [ "$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"
1486
1777
  retry_decision_gate "$PLAN" "$id" "$st" "$DECIDE" "$NEWDWARF" "$REASON" "$APPROVED"
1487
1778
  # A task at its attempt cap (#69) is refused with the decision commands unless --decide retry=N reaches past it.
@@ -1529,6 +1820,7 @@ do_retry() {
1529
1820
  rm -f "$PLAN/infra.last"
1530
1821
 
1531
1822
  note "retrying $id with dwarf $(field "$T" "$id" dwarf)"
1823
+ forge_fast_fractal_note "$PLAN"
1532
1824
  export FORGE_FRACTAL_RETRY=1
1533
1825
  export FORGE_DATASET_TRIGGER=human
1534
1826
  do_task "$PLAN" "$id"
@@ -1538,15 +1830,34 @@ do_retry() {
1538
1830
  # stopped for a decision again, an empty diff, or a policy that finds no stronger model.
1539
1831
  # The count is decided.left, so it survives a run that ended before the attempts were used.
1540
1832
  local more; more="$(decided_left "$tdir")"; more="${more:-0}"
1541
- while [ "$more" -gt 0 ] && { [ "$st" = FAIL ] || [ "$st" = UNKNOWN ]; } && [ ! -e "$tdir/noretry" ]; do
1833
+ # --retry-failed N (or the frozen profile's) adds N further attempts for this call alone, spent after the
1834
+ # decided ones; --infra-retries M waits out that many infrastructure stops before giving up (#48).
1835
+ local rf_left="$PP_RETRY_FAILED" inf_left="$PP_INFRA_RETRIES" inf_cycle=0 from_decided
1836
+ while :; do
1837
+ if [ "$st" = INFRA ]; then
1838
+ # No attempt was spent and nothing was judged: wait the stop out as the scheduler does, and dispatch again.
1839
+ [ "$inf_left" -gt 0 ] || break
1840
+ inf_left=$(( inf_left - 1 ))
1841
+ retry_infra_wait "$PLAN" "$tdir" "$inf_cycle"; inf_cycle=$(( inf_cycle + 1 ))
1842
+ prepare_retry "$PLAN" "$id" || break
1843
+ rm -f "$tdir/status" "$PLAN/infra.last"
1844
+ note "retrying $id after an infrastructure stop ($inf_left more wait(s) allowed)"
1845
+ do_task "$PLAN" "$id"
1846
+ st="$(task_status "$PLAN" "$id")"
1847
+ continue
1848
+ fi
1849
+ { [ "$st" = FAIL ] || [ "$st" = UNKNOWN ]; } && [ ! -e "$tdir/noretry" ] || break
1850
+ from_decided=0
1851
+ if [ "$more" -gt 0 ]; then from_decided=1; elif [ "$rf_left" -le 0 ]; then break; fi
1542
1852
  # Each further attempt is spent against the cap (#69) like the scheduler's retries: an UNKNOWN review
1543
1853
  # does not stop the task by itself, so the cap is checked here, and a task at it stops for a decision.
1544
1854
  if ! attempt_cap_gate "$PLAN" "$id" "$st" ""; then st="$(task_status "$PLAN" "$id")"; break; fi
1545
- more=$(( more - 1 ))
1855
+ if [ "$from_decided" = 1 ]; then more=$(( more - 1 )); else rf_left=$(( rf_left - 1 )); fi
1546
1856
  prepare_retry "$PLAN" "$id" || break
1547
- decided_left_set "$tdir" "$more" || break
1857
+ if [ "$from_decided" = 1 ]; then decided_left_set "$tdir" "$more" || break; fi
1548
1858
  rm -f "$tdir/status"
1549
- note "retrying $id (decided retry, $more more after this)"
1859
+ if [ "$from_decided" = 1 ]; then note "retrying $id (decided retry, $more more after this)"
1860
+ else note "retrying $id (--retry-failed, $rf_left more after this)"; fi
1550
1861
  do_task "$PLAN" "$id"
1551
1862
  st="$(task_status "$PLAN" "$id")"
1552
1863
  done
@@ -1943,6 +2254,7 @@ do_run() {
1943
2254
  ;;
1944
2255
  esac
1945
2256
  done
2257
+ forge_profile_run_values "$PLAN"
1946
2258
  [ "$JEV_OPTS" = 1 ] && forge_jev_settle "$PLAN"
1947
2259
  [ -n "$PP_SWEEP" ] || [ "$PP_SWEEP_PLAUSIBLE$PP_SWEEP_MAX_SET" = 00 ] || die "--sweep-plausible and --sweep-max go with --sweep"
1948
2260
  case "$OUTPUT" in summary|full) ;; *) die "--output must be summary or full" ;; esac
@@ -1965,6 +2277,7 @@ do_run() {
1965
2277
  forge_ui_register "$PLAN"
1966
2278
  fi
1967
2279
  forge_fractal_parallel_run "$PLAN" "$REPO" "$DRY" ${fractal_pipeline_args[@]+"${fractal_pipeline_args[@]}"} || return $?
2280
+ forge_fast_fractal_note "$PLAN"
1968
2281
  # Durable, beside the repo — never TMPDIR. A temp worktree root has destroyed
1969
2282
  # real work here before: isolated worktrees are never pushed, so when the root
1970
2283
  # goes, the only copy of that work goes with it.
@@ -2212,6 +2525,17 @@ do_integrate() {
2212
2525
  report_known_issues "$PLAN"
2213
2526
  # Opt-in whole-run review of the combined candidate, after verification and before the
2214
2527
  # user's branch moves. Per-task review only ever saw one slice at a time.
2528
+ # A profile that names a final reviewer (#48) asks for this review: --final-review wins, then the spec
2529
+ # `review` or an earlier --final-review left in final_qa, then the profile's own.
2530
+ if [ -z "$PP_FINAL_REVIEW" ] && [ -f "$PLAN/profile.json" ]; then
2531
+ local pf_final; pf_final="$(forge_profile_py value "$PLAN/profile.json" routing.final_reviewer 2>/dev/null)"
2532
+ pf_final="${pf_final#*$'\t'}"
2533
+ if [ -n "$pf_final" ]; then
2534
+ PP_FINAL_REVIEW="$(head -1 "$PLAN/final_qa" 2>/dev/null | tr -d ' \r')"
2535
+ [ -n "$PP_FINAL_REVIEW" ] || PP_FINAL_REVIEW="$pf_final"
2536
+ note "final review: $PP_FINAL_REVIEW, from the plan's profile"
2537
+ fi
2538
+ fi
2215
2539
  if [ -n "$PP_FINAL_REVIEW" ]; then
2216
2540
  declare -F final_review_for_integrate >/dev/null || die "the final review is not available in this build" 3
2217
2541
  final_review_for_integrate "$PLAN" "$parent" "$candidate_sha" "$candidate" "$out" "$PP_FINAL_REVIEW" || return $?
@@ -2229,13 +2553,231 @@ do_integrate() {
2229
2553
  echo "merged candidate of $int_br into $cur (verification: $(cat "$out/verification.status"))"
2230
2554
  }
2231
2555
 
2556
+ # --- auto (#87, #88) -----------------------------------------------------------------
2557
+ # The profile `plan` will use for these flags, read without writing anything: the planner
2558
+ # spec and the fast-tier roles it names, as `key<TAB>value` lines. The choice is plan's own
2559
+ # (profiles._choose): --no-profile means none; with no --profile the plan's frozen
2560
+ # profile.json stays; otherwise the named profile, else the default. A frozen profile adds no
2561
+ # fast roles: plan stored those when it froze it.
2562
+ forge_auto_profile() { # forge_auto_profile <plan-dir> <repo> <profile name or empty> <no-profile 0|1>
2563
+ PYTHONPATH="$SKILL_DIR/scripts${PYTHONPATH:+:$PYTHONPATH}" python3 - "$@" <<'PY'
2564
+ import sys
2565
+ from pathlib import Path
2566
+ from forge_decide import profiles
2567
+ plan, repo, name, none = Path(sys.argv[1]), sys.argv[2] or None, sys.argv[3] or None, sys.argv[4] == '1'
2568
+ try:
2569
+ action, chosen = profiles._choose(repo, name, none, plan / profiles.FROZEN, False, False)
2570
+ except profiles.ProfileError as exc:
2571
+ print('forge: %s' % exc, file=sys.stderr)
2572
+ sys.exit(2)
2573
+ body = chosen.get('body') if isinstance(chosen, dict) else None
2574
+ values = profiles.flat(body) if body else {}
2575
+ planner = values.get('routing.planner')
2576
+ if isinstance(planner, str) and planner:
2577
+ print('routing.planner\t%s' % planner)
2578
+ print('action\t%s' % action)
2579
+ # As plan stores it: an empty list is `none`, which clears the roles an earlier call stored.
2580
+ if action == 'new' and isinstance(values.get('policy.fast'), list):
2581
+ print('policy.fast\t%s' % profiles._fast_roles(values['policy.fast'], profiles.FAST_ROLES))
2582
+ PY
2583
+ }
2584
+
2585
+ # Forget what `parse` wrote, so the planner's next reply can be parsed: tasks.tsv and each
2586
+ # tasks/<id>/. request.md, the planner's records and plan.sendback.md stay.
2587
+ forge_auto_unparse() { # forge_auto_unparse <plan-dir>
2588
+ local id
2589
+ if [ -f "$1/tasks.tsv" ]; then
2590
+ for id in $(all_ids "$1/tasks.tsv"); do
2591
+ case "$id" in ''|.*|*/*) continue ;; esac
2592
+ rm -rf "$1/tasks/$id"
2593
+ done
2594
+ fi
2595
+ rm -f "$1/tasks.tsv" "$1/tasks.declared.tsv"
2596
+ }
2597
+
2598
+ # One planner dispatch: prompt in $PLAN/planner.prompt, reply in $PLAN/planner.last. Sets
2599
+ # AUTO_RC (0, 4 or 8) for the caller instead of exiting, so a send-back can reuse it.
2600
+ forge_auto_dispatch() { # forge_auto_dispatch <plan-dir> <repo> <spec> <fast-option-or-empty>
2601
+ local plan="$1" repo="$2" spec="$3" tier="$4" rc=0
2602
+ AUTO_RC=0
2603
+ note "auto: dispatching the planner ($spec) for the whole plan"
2604
+ bash "$DISPATCH" planner "$spec" --repo "$repo" --run-dir "$plan" --prompt-file "$plan/planner.prompt" \
2605
+ --output summary ${tier:+$tier} >&2 || rc=$?
2606
+ if [ "$rc" -eq 8 ]; then
2607
+ note "auto: the planner stopped on an infrastructure failure (see $plan/planner.infra). Nothing was dispatched after the planner; fix the cause and run auto again"
2608
+ AUTO_RC=8
2609
+ elif [ "$rc" -ne 0 ]; then
2610
+ note "auto: the planner failed (exit $rc). Its last message is $plan/planner.last and its log $plan/planner.log"
2611
+ AUTO_RC=4
2612
+ fi
2613
+ return 0
2614
+ }
2615
+
2616
+ do_auto() {
2617
+ local PLAN="${1:-}"
2618
+ [ -n "$PLAN" ] && [ "${PLAN#-}" = "$PLAN" ] || die "auto needs a plan dir: forge-parallel.sh auto <plan-dir> --repo DIR --goal-file FILE ..."
2619
+ shift
2620
+ local REPO="" GOAL="" PLANNER="" DLEVEL="" GATE=auto ONE_DIFF=medium COLLAPSE=1 APPROVED=0
2621
+ local PLAN_ARGS=() RUN_ARGS=() PF_NAME="" PF_NONE=0 FAST_GIVEN=0 FAST_PARSED="" arg
2622
+ while [ $# -gt 0 ]; do
2623
+ case "$1" in
2624
+ --) shift; RUN_ARGS=("$@"); break ;;
2625
+ --repo) [ $# -ge 2 ] || die "--repo needs a directory"; REPO="$2"; shift 2 ;;
2626
+ --goal-file) [ $# -ge 2 ] || die "--goal-file needs a file"; GOAL="$2"; shift 2 ;;
2627
+ --planner) [ -n "${2:-}" ] || die "--planner needs a spec"; PLANNER="$2"; PLAN_ARGS+=("$1" "$2"); shift 2 ;;
2628
+ --decompose-level)
2629
+ case "${2:-}" in low|medium|high) DLEVEL="$2"; shift 2 ;;
2630
+ *) die "--decompose-level must be low, medium or high" ;; esac ;;
2631
+ --plan)
2632
+ case "${2:-}" in auto|always|never) GATE="$2"; shift 2 ;;
2633
+ *) die "--plan must be auto, always or never" ;; esac ;;
2634
+ --one-task-difficulty)
2635
+ case "${2:-}" in low|medium|high) ONE_DIFF="$2"; shift 2 ;;
2636
+ *) die "--one-task-difficulty must be low, medium or high" ;; esac ;;
2637
+ --no-collapse) COLLAPSE=0; shift ;;
2638
+ --approved) APPROVED=1; shift ;;
2639
+ --profile) [ -n "${2:-}" ] || die "--profile needs a profile name"; PF_NAME="$2"; PLAN_ARGS+=("$1" "$2"); shift 2 ;;
2640
+ --no-profile) PF_NONE=1; PLAN_ARGS+=("$1"); shift ;;
2641
+ --fast)
2642
+ [ -n "${2:-}" ] || die "--fast needs a comma list of roles (dwarf, qa, planner, review), all or none"
2643
+ forge_fast_parse "$2" plan; FAST_GIVEN=1
2644
+ PLAN_ARGS+=("$1" "$2"); shift 2 ;;
2645
+ --no-ripwire) export FORGE_RIPWIRE=off; PLAN_ARGS+=("$1"); shift ;;
2646
+ *) PLAN_ARGS+=("$1"); shift ;;
2647
+ esac
2648
+ done
2649
+ [ -n "$REPO" ] || die "auto needs --repo DIR"
2650
+ [ -d "$REPO" ] || die "--repo '$REPO' is not a directory"
2651
+ REPO="$(cd "$REPO" && pwd)"
2652
+ [ -n "$GOAL" ] || die "auto needs --goal-file FILE"
2653
+ [ -f "$GOAL" ] && [ -r "$GOAL" ] || die "--goal-file '$GOAL' is not a readable file"
2654
+ [ ! -e "$PLAN/tasks.tsv" ] || die "auto starts a new plan; use plan and run on an existing one"
2655
+ (cd "$REPO" && git rev-parse --git-dir >/dev/null 2>&1) \
2656
+ || die "'$REPO' is not a git repository. Decomposed runs need worktrees, and there is no honest fallback without them." 3
2657
+ mkdir -p "$PLAN" || die "could not create plan dir '$PLAN'" 3
2658
+ PLAN="$(cd "$PLAN" && pwd)"
2659
+
2660
+ # The request, verbatim, and who started this plan.
2661
+ if ! [ "$GOAL" -ef "$PLAN/request.md" ]; then
2662
+ cp "$GOAL" "$PLAN/request.md" || die "could not write $PLAN/request.md" 3
2663
+ fi
2664
+ printf 'auto\n' > "$PLAN/orchestrator" || die "could not write $PLAN/orchestrator" 3
2665
+ # The goal line the table, every task's "Overall goal", the final review and the dataset read:
2666
+ # the request's first non-empty line, a heading's marks dropped. One the user wrote stays.
2667
+ if [ ! -s "$PLAN/goal.txt" ]; then
2668
+ awk 'NF { sub(/^#+[[:space:]]*/, ""); print substr($0, 1, 200); exit }' "$PLAN/request.md" > "$PLAN/goal.txt" \
2669
+ || die "could not write $PLAN/goal.txt" 3
2670
+ fi
2671
+ local PLANNER_PY="$SKILL_DIR/scripts/forge-planner.py" REQUEST="$PLAN/request.md" rc=0 from_planner=0 fast_tier=""
2672
+ PLAN_ARGS=(--repo "$REPO" ${PLAN_ARGS[@]+"${PLAN_ARGS[@]}"})
2673
+ [ -z "$DLEVEL" ] || PLAN_ARGS+=(--decompose-level "$DLEVEL")
2674
+ [ "$COLLAPSE" = 0 ] || PLAN_ARGS+=(--collapse-chains)
2675
+
2676
+ # 1. The gate.
2677
+ local decision
2678
+ case "$GATE" in
2679
+ always) decision=plan ;;
2680
+ never) decision=one-task ;;
2681
+ *)
2682
+ decision="$(python3 "$PLANNER_PY" gate --plan "$PLAN" --goal-file "$REQUEST")" || exit $?
2683
+ note "auto: the gate says $decision" ;;
2684
+ esac
2685
+
2686
+ if [ "$decision" = one-task ]; then
2687
+ # 2. One task, no planner.
2688
+ python3 "$PLANNER_PY" one-task --plan "$PLAN" --goal-file "$REQUEST" --difficulty "$ONE_DIFF" >/dev/null || exit $?
2689
+ else
2690
+ # 3. The planner: its spec, its preflight, its dispatch, its reply.
2691
+ local key val profile_planner="" profile_fast="" profile_action="" profile_lines
2692
+ forge_profile_precheck "$PLAN" "${PLAN_ARGS[@]}"
2693
+ profile_lines="$(forge_auto_profile "$PLAN" "$REPO" "$PF_NAME" "$PF_NONE")" || exit $?
2694
+ while IFS="$(printf '\t')" read -r key val; do
2695
+ case "$key" in
2696
+ routing.planner) profile_planner="$val" ;;
2697
+ policy.fast) profile_fast="$val" ;;
2698
+ action) profile_action="$val" ;;
2699
+ esac
2700
+ done <<AUTO_PROFILE
2701
+ $profile_lines
2702
+ AUTO_PROFILE
2703
+ if [ -z "$PLANNER" ] && [ -n "$profile_planner" ]; then
2704
+ PLANNER="$profile_planner"
2705
+ # `plan` takes routing.planner from a profile only when it freezes a new one. A kept
2706
+ # frozen profile names the planner but plan would not record it: hand it over.
2707
+ [ "$profile_action" = new ] || PLAN_ARGS+=(--planner "$PLANNER")
2708
+ fi
2709
+ [ -n "$PLANNER" ] || die "auto needs --planner SPEC, or a profile with routing.planner"
2710
+ # The fast roles `plan` is about to store: the flag, else the profile's. Written now so the
2711
+ # planner dispatch asks for the tier through the helper the other roles use.
2712
+ if [ "$FAST_GIVEN" != 1 ] && [ -n "$profile_fast" ]; then
2713
+ forge_fast_parse "$profile_fast" plan; FAST_GIVEN=1
2714
+ fi
2715
+ forge_fast_store "$PLAN"
2716
+ fast_tier="$(forge_fast_tier "$PLAN" planner)"
2717
+
2718
+ note "auto: checking the planner ($PLANNER) before any model call"
2719
+ bash "$DISPATCH" doctor --spec "$PLANNER" --role planner ${fast_tier:+$fast_tier} >&2 || exit $?
2720
+ python3 "$PLANNER_PY" prompt --plan "$PLAN" --goal-file "$REQUEST" ${DLEVEL:+--level "$DLEVEL"} >/dev/null || exit $?
2721
+ forge_auto_dispatch "$PLAN" "$REPO" "$PLANNER" "$fast_tier"
2722
+ [ "$AUTO_RC" -eq 0 ] || exit "$AUTO_RC"
2723
+ python3 "$PLANNER_PY" parse --plan "$PLAN" --reply "$PLAN/planner.last" || rc=$?
2724
+ case "$rc" in
2725
+ 0) ;;
2726
+ 4) note "auto: the planner's reply was refused (see $PLAN/planner.refused). Nothing was dispatched after the planner"
2727
+ exit 4 ;;
2728
+ *) exit "$rc" ;;
2729
+ esac
2730
+ from_planner=1
2731
+ fi
2732
+
2733
+ # 4. plan, and one send-back when the plan gates ask for it.
2734
+ bash "$SELF" plan "$PLAN" ${PLAN_ARGS[@]+"${PLAN_ARGS[@]}"}; rc=$?
2735
+ if [ "$rc" -eq 4 ] && [ "$from_planner" = 1 ] && [ -s "$PLAN/plan.sendback.md" ]; then
2736
+ note "auto: the plan gates sent items back; asking the planner once more"
2737
+ cp "$PLAN/planner.last" "$PLAN/planner.first.last" 2>/dev/null || true
2738
+ cp "$PLAN/planner.prompt" "$PLAN/planner.first.prompt" 2>/dev/null || true
2739
+ forge_auto_unparse "$PLAN"
2740
+ python3 "$PLANNER_PY" prompt --plan "$PLAN" --goal-file "$REQUEST" ${DLEVEL:+--level "$DLEVEL"} >/dev/null || exit $?
2741
+ {
2742
+ printf '\n\n## Your plan was sent back\n\n'
2743
+ printf 'forge checked your plan and sent the items below back. Reply with the whole corrected plan, in the same format.\n\n'
2744
+ cat "$PLAN/plan.sendback.md"
2745
+ } >> "$PLAN/planner.prompt"
2746
+ forge_auto_dispatch "$PLAN" "$REPO" "$PLANNER" "$fast_tier"
2747
+ [ "$AUTO_RC" -eq 0 ] || exit "$AUTO_RC"
2748
+ rc=0
2749
+ python3 "$PLANNER_PY" parse --plan "$PLAN" --reply "$PLAN/planner.last" || rc=$?
2750
+ case "$rc" in
2751
+ 0) ;;
2752
+ 4) note "auto: the planner's second reply was refused (see $PLAN/planner.refused). Nothing was dispatched after the planner"
2753
+ exit 4 ;;
2754
+ *) exit "$rc" ;;
2755
+ esac
2756
+ bash "$SELF" plan "$PLAN" ${PLAN_ARGS[@]+"${PLAN_ARGS[@]}"}; rc=$?
2757
+ if [ "$rc" -eq 4 ]; then
2758
+ note "auto: the plan gates sent items back a second time; stopping. See $PLAN/plan.sendback.md"
2759
+ fi
2760
+ fi
2761
+ [ "$rc" -eq 0 ] || exit "$rc"
2762
+
2763
+ # 5. Stop for approval, or run.
2764
+ if [ "$APPROVED" = 1 ]; then
2765
+ bash "$SELF" run "$PLAN" ${RUN_ARGS[@]+"${RUN_ARGS[@]}"}; exit $?
2766
+ fi
2767
+ local command_line
2768
+ command_line="$(printf '%q run %q' "$SELF" "$PLAN")"
2769
+ for arg in ${RUN_ARGS[@]+"${RUN_ARGS[@]}"}; do command_line="$command_line $(printf '%q' "$arg")"; done
2770
+ printf '\nauto: the plan is ready and nothing has run. To start it:\n %s\n' "$command_line"
2771
+ exit 0
2772
+ }
2773
+
2232
2774
  # --- main ---------------------------------------------------------------------
2233
2775
  if [ "${1:-}" = --help ] || [ "${1:-}" = -h ]; then
2234
2776
  awk '/^# Usage:/ {on=1} /^set -uo pipefail/ {on=0} on {sub(/^# ?/, ""); print}' "$SELF"
2235
2777
  forge_fractal_help
2236
2778
  exit 0
2237
2779
  fi
2238
- [ $# -ge 1 ] || die "usage: forge-parallel.sh <plan|run|retry|accept|review|integrate|split|attribute|combine> <plan-dir> [...]"
2780
+ [ $# -ge 1 ] || die "usage: forge-parallel.sh <plan|auto|run|retry|accept|review|integrate|split|attribute|combine> <plan-dir> [...]"
2239
2781
  CMD="$1"; shift
2240
2782
  [ ! -f "${1:-}/no_ripwire" ] || export FORGE_RIPWIRE=off
2241
2783
  if [ -d "${1:-}" ]; then
@@ -2260,8 +2802,12 @@ case "$CMD" in
2260
2802
  esac
2261
2803
  # Subcommands implemented in the optional libraries fail clearly if the library is absent.
2262
2804
  need_fn() { declare -F "$1" >/dev/null || die "'$CMD' is not available: $1 is missing" 3; }
2805
+ # A plan with a frozen profile asks its decision-model gates from that copy (`plan` does it itself,
2806
+ # after it freezes). The internal subcommands inherit it from the `run` that started them.
2807
+ case "$CMD" in run|retry|integrate|accept|review|split|attribute) forge_profile_export "${1:-}" ;; esac
2263
2808
  case "$CMD" in
2264
2809
  plan) do_plan "$@"; rc=$?; forge_update_notice; exit "$rc" ;;
2810
+ auto) do_auto "$@" ;;
2265
2811
  run)
2266
2812
  # The signal handler lives here, at the pid the user started: do_run is a subshell, and
2267
2813
  # bash runs a trap only between commands, so it is started in the background and waited