@giamme/forge 0.21.0 → 0.23.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 (165) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/README.md +25 -15
  3. package/SKILL.md +137 -216
  4. package/VERSION +1 -1
  5. package/bin/forge.js +104 -0
  6. package/docs/README.md +2 -3
  7. package/docs/add-ons.md +5 -3
  8. package/docs/artifacts.md +17 -2
  9. package/docs/commands.md +85 -63
  10. package/docs/decomposed-runs.md +195 -21
  11. package/docs/development.md +12 -10
  12. package/docs/forge-ui.md +33 -4
  13. package/docs/install.md +127 -6
  14. package/docs/memory.md +6 -1
  15. package/docs/models.md +36 -10
  16. package/docs/reviews.md +50 -29
  17. package/docs/safety.md +27 -2
  18. package/docs/troubleshooting.md +8 -11
  19. package/forge +29 -4
  20. package/forge.cmd +56 -0
  21. package/package.json +6 -3
  22. package/references/dataset.md +10 -9
  23. package/references/decompose-integration.md +337 -0
  24. package/references/decompose-opt-in.md +527 -0
  25. package/references/decompose-recovery.md +569 -0
  26. package/references/decompose-review.md +256 -0
  27. package/references/decompose.md +99 -1606
  28. package/references/findings.md +68 -0
  29. package/references/harnesses.md +7 -12
  30. package/references/memory.md +13 -2
  31. package/references/planning.md +48 -18
  32. package/references/ripwire.md +38 -13
  33. package/references/runtime.md +22 -33
  34. package/references/setup.md +90 -0
  35. package/references/solo.md +13 -42
  36. package/references/ui.md +12 -5
  37. package/registry.tsv +40 -22
  38. package/scripts/forge-artifact.sh +148 -13
  39. package/scripts/forge-compare.py +5 -15
  40. package/scripts/forge-config.py +14 -1
  41. package/scripts/forge-contract.py +216 -53
  42. package/scripts/forge-control.py +781 -51
  43. package/scripts/forge-dispatch.sh +202 -42
  44. package/scripts/forge-du.py +13 -3
  45. package/scripts/forge-guard.py +137 -6
  46. package/scripts/forge-handoff.py +221 -39
  47. package/scripts/forge-install-ripwire.sh +100 -24
  48. package/scripts/forge-install.sh +100 -41
  49. package/scripts/forge-issues.py +6 -4
  50. package/scripts/forge-jev-options.sh +3 -5
  51. package/scripts/forge-lib-adopt.sh +169 -5
  52. package/scripts/forge-lib-attribute.sh +10 -3
  53. package/scripts/forge-lib-decompose.sh +454 -0
  54. package/scripts/forge-lib-env.sh +179 -0
  55. package/scripts/forge-lib-group.sh +9 -6
  56. package/scripts/forge-lib-plans.sh +38 -21
  57. package/scripts/forge-lib-review.sh +28 -8
  58. package/scripts/forge-lib-task.sh +263 -69
  59. package/scripts/forge-lib-verify.sh +59 -10
  60. package/scripts/forge-memory.sh +83 -4
  61. package/scripts/forge-metrics.sh +10 -3
  62. package/scripts/forge-parallel-options.sh +23 -4
  63. package/scripts/forge-parallel.sh +602 -217
  64. package/scripts/forge-plan-check.py +71 -8
  65. package/scripts/forge-planner.py +303 -17
  66. package/scripts/forge-pr.py +9 -1
  67. package/scripts/forge-prompt.py +2 -2
  68. package/scripts/forge-review-links.py +248 -0
  69. package/scripts/forge-ripwire.py +234 -16
  70. package/scripts/forge-route.py +19 -27
  71. package/scripts/forge-runs-rm.py +92 -21
  72. package/scripts/forge-runs.py +46 -5
  73. package/scripts/forge-runtime.py +39 -14
  74. package/scripts/forge-schedule.py +141 -33
  75. package/scripts/forge-setup-stale.py +236 -0
  76. package/scripts/forge-setup.py +804 -0
  77. package/scripts/forge-shadow.py +2 -20
  78. package/scripts/forge-solo-lock.py +46 -0
  79. package/scripts/forge-solo.sh +39 -58
  80. package/scripts/forge-sweep.py +3 -2
  81. package/scripts/forge-ui.py +8 -1
  82. package/scripts/forge-update.sh +8 -3
  83. package/scripts/forge-verify.py +17 -1
  84. package/scripts/forge-version.py +13 -1
  85. package/scripts/forge-watch.py +7 -2
  86. package/scripts/forge-win.sh +156 -0
  87. package/scripts/forge_brief/__init__.py +6 -1
  88. package/scripts/forge_brief/proposals.py +6 -1
  89. package/scripts/forge_dataset/__init__.py +1 -1
  90. package/scripts/forge_dataset/capture.py +0 -34
  91. package/scripts/forge_dataset/checks.py +0 -1
  92. package/scripts/forge_dataset/cli.py +5 -1
  93. package/scripts/forge_dataset/export.py +28 -6
  94. package/scripts/forge_dataset/facts.py +1 -2
  95. package/scripts/forge_dataset/forecast.py +1 -1
  96. package/scripts/forge_dataset/harvest.py +107 -7
  97. package/scripts/forge_dataset/importer.py +3 -2
  98. package/scripts/forge_dataset/pairs.py +3 -3
  99. package/scripts/forge_dataset/reviewers.py +4 -3
  100. package/scripts/forge_dataset/routing.py +6 -3
  101. package/scripts/forge_dataset/tiers.py +1 -1
  102. package/scripts/forge_decide/__init__.py +1 -1
  103. package/scripts/forge_decide/budget.py +12 -7
  104. package/scripts/forge_decide/profiles.py +11 -14
  105. package/scripts/forge_decide/replay.py +9 -4
  106. package/scripts/forge_decompose.py +987 -0
  107. package/scripts/forge_flock.py +17 -2
  108. package/scripts/forge_groups/review.py +6 -1
  109. package/scripts/forge_jev/__init__.py +1 -1
  110. package/scripts/forge_jev/cli.py +10 -1
  111. package/scripts/forge_jev/gates.py +10 -1
  112. package/scripts/forge_jev/verify.py +10 -2
  113. package/scripts/forge_legacy.py +86 -0
  114. package/scripts/forge_memory_home.py +57 -0
  115. package/scripts/forge_models.py +151 -0
  116. package/scripts/forge_posix.py +17 -0
  117. package/scripts/forge_prefs.py +138 -0
  118. package/scripts/forge_proc.py +108 -0
  119. package/scripts/forge_registry.py +2 -2
  120. package/scripts/forge_side/__init__.py +23 -4
  121. package/scripts/forge_storage.py +28 -15
  122. package/ui/README.md +22 -10
  123. package/ui/docs/api.md +121 -99
  124. package/ui/forge_ui/control.py +53 -16
  125. package/ui/forge_ui/export.py +0 -10
  126. package/ui/forge_ui/models.py +15 -2
  127. package/ui/forge_ui/projects.py +4 -1
  128. package/ui/forge_ui/reader.py +462 -60
  129. package/ui/forge_ui/server.py +0 -10
  130. package/ui/forge_ui/static/app.js +28 -9
  131. package/ui/forge_ui/static/checksview.js +12 -3
  132. package/ui/forge_ui/static/controls.js +147 -11
  133. package/ui/forge_ui/static/convo.js +5 -2
  134. package/ui/forge_ui/static/diffview.js +4 -5
  135. package/ui/forge_ui/static/panel.js +180 -77
  136. package/ui/forge_ui/static/projectsview.js +16 -2
  137. package/ui/forge_ui/static/runmodel.js +18 -4
  138. package/ui/forge_ui/static/scene3d.js +52 -31
  139. package/ui/forge_ui/static/settings.js +1 -1
  140. package/ui/forge_ui/static/storageview.js +2 -2
  141. package/ui/forge_ui/static/style.css +23 -6
  142. package/ui/forge_ui/static/worktreesview.js +8 -4
  143. package/ui/forge_ui/transcripts.py +6 -0
  144. package/ui/forge_ui/worktrees.py +13 -1
  145. package/docs/fractal.md +0 -181
  146. package/references/fractal-dashboard.md +0 -21
  147. package/references/fractal.md +0 -307
  148. package/scripts/forge-fractal-dispatch.sh +0 -4
  149. package/scripts/forge-fractal-options.sh +0 -54
  150. package/scripts/forge-fractal.py +0 -6
  151. package/scripts/forge-install-fractal.sh +0 -4
  152. package/scripts/forge_fractal/__init__.py +0 -88
  153. package/scripts/forge_fractal/bridge.py +0 -46
  154. package/scripts/forge_fractal/cli.py +0 -224
  155. package/scripts/forge_fractal/dashboard.css +0 -138
  156. package/scripts/forge_fractal/dashboard.html +0 -41
  157. package/scripts/forge_fractal/dashboard.js +0 -687
  158. package/scripts/forge_fractal/decomposition.py +0 -262
  159. package/scripts/forge_fractal/execution.py +0 -659
  160. package/scripts/forge_fractal/inspection.py +0 -261
  161. package/scripts/forge_fractal/install.py +0 -158
  162. package/scripts/forge_fractal/invocation.py +0 -95
  163. package/scripts/forge_fractal/pipeline.py +0 -125
  164. package/scripts/forge_fractal/selection.py +0 -180
  165. package/ui/forge_ui/fractal.py +0 -582
@@ -0,0 +1,454 @@
1
+ #!/usr/bin/env bash
2
+ # Recursive decomposition for forge-parallel.sh (#153): a task split into subtasks, and a goal
3
+ # split into plans.
4
+ #
5
+ # Sourced by forge-parallel.sh, which provides die/note/field/all_ids/task_status, compute_waves,
6
+ # carry_findings, forge_dataset_event, forge_fast_tier, forge_pools_file, DISPATCH and SELF.
7
+ # Functions only; nothing runs at source time. The node model, the limits and the validation of a
8
+ # split are scripts/forge_decompose.py; the planner's side is scripts/forge-planner.py.
9
+ #
10
+ # plan <plan> --split-depth N --split-children N --split-nodes N the limits, frozen by the first run
11
+ # run <plan> --auto-split [--split-planner SPEC] | --no-auto-split a task that stops on `split` is
12
+ # split by the planner, within the limits
13
+ # retry <plan> <task> --decide split[=fresh] [--subtasks FILE | --planner SPEC] --reason "..." --approved
14
+ # next <plan> [<new-plan-dir>] [--planner SPEC] [plan flags...] plan the next stage of a larger goal
15
+ #
16
+ # A split task's subtasks are a plan of their own, <plan>/tasks/<id>/sub. The task's next entry runs
17
+ # that plan instead of an implementer (run_split_task), merges its integration branch into the task's
18
+ # branch, and the task is then snapshotted and reviewed as any attempt is: on its cumulative diff.
19
+
20
+ forge_decompose_py() { python3 "$SKILL_DIR/scripts/forge_decompose.py" "$@"; }
21
+
22
+ forge_is_split() { # forge_is_split <plan> <id>: the task has a sub-plan
23
+ [ -f "$1/tasks/$2/sub/tasks.tsv" ]
24
+ }
25
+
26
+ # The task's next entry runs its sub-plan: it has one, and its work is not on the task's branch yet.
27
+ forge_split_open() { # forge_split_open <plan> <id>
28
+ [ -f "$1/tasks/$2/sub/tasks.tsv" ] && [ ! -e "$1/tasks/$2/sub.merged" ]
29
+ }
30
+
31
+ forge_is_sub_plan() { # forge_is_sub_plan <plan>
32
+ [ -f "$1/parent.json" ] || return 1
33
+ case "$1" in */tasks/*/sub) return 0 ;; esac
34
+ return 1
35
+ }
36
+
37
+ forge_decide_is_split() { # forge_decide_is_split <--decide value>
38
+ case "${1:-}" in split|split=keep|split=fresh) return 0 ;; esac
39
+ return 1
40
+ }
41
+
42
+ # --- plan ------------------------------------------------------------------------------------
43
+
44
+ # The limits a plan was given, set before anything else of the plan is written: a value out of
45
+ # range, or a change after the first run froze them, refuses the whole `plan` call (exit 2).
46
+ forge_decompose_limits() { # forge_decompose_limits <plan> <depth|""> <children|""> <nodes|"">
47
+ local out args=()
48
+ [ -z "$2" ] || args+=(--depth "$2")
49
+ [ -z "$3" ] || args+=(--children "$3")
50
+ [ -z "$4" ] || args+=(--nodes "$4")
51
+ [ "${#args[@]}" -gt 0 ] || return 0
52
+ out="$(forge_decompose_py limits "$1" "${args[@]}" 2>&1 >/dev/null)" || die "${out#refused: }" 2
53
+ }
54
+
55
+ # One sub-plan made ready to run: its waves, and the tier each subtask's model runs in (what `plan`
56
+ # records for a row it routes, so an escalation of a subtask knows where it starts).
57
+ forge_sub_prepare() { # forge_sub_prepare <sub-plan>
58
+ local sub="$1" id
59
+ ( compute_waves "$sub" ) || return 1
60
+ for id in $(all_ids "$sub/tasks.tsv"); do
61
+ python3 "$SKILL_DIR/scripts/forge-route.py" assign --plan "$sub" --task "$id" \
62
+ --tier "$(field "$sub/tasks.tsv" "$id" difficulty)" --epsilon 0 \
63
+ --default "$(field "$sub/tasks.tsv" "$id" dwarf)" --pools "$(forge_pools_file "$sub")" >/dev/null 2>&1 || true
64
+ done
65
+ forge_decompose_snapshot "$sub"
66
+ }
67
+
68
+ # The dataset's record of a plan (a sub-plan is never made by `plan`, which records the others), and
69
+ # again of the plan a task was just split in: the decision is part of what was planned.
70
+ forge_decompose_snapshot() { # forge_decompose_snapshot <plan>
71
+ forge_dataset_off && return 0
72
+ python3 "$SKILL_DIR/scripts/forge-dataset.py" plan "$1" >/dev/null 2>&1 || true
73
+ }
74
+
75
+ forge_sub_prepare_all() { # forge_sub_prepare_all <plan>: every sub-plan under it that has no waves yet
76
+ local sub rc=0
77
+ while IFS= read -r sub; do
78
+ [ -n "$sub" ] || continue
79
+ [ -s "$sub/waves.tsv" ] && continue
80
+ forge_sub_prepare "$sub" || rc=1
81
+ done <<FORGE_SUBS
82
+ $(forge_decompose_py subs "$1" 2>/dev/null)
83
+ FORGE_SUBS
84
+ return $rc
85
+ }
86
+
87
+ # After `plan` routed the table: the planner's proposed splits become sub-plans within the limits,
88
+ # and every other task is recorded as kept whole. DECOMPOSE_LINES is what happened, for the table.
89
+ forge_decompose_settle() { # forge_decompose_settle <plan>
90
+ local by=default
91
+ DECOMPOSE_LINES=""
92
+ [ ! -s "$1/planner.prompt" ] || by=planner
93
+ DECOMPOSE_LINES="$(forge_decompose_py settle "$1" --by "$by" 2>/dev/null)" || true
94
+ forge_sub_prepare_all "$1" || note "a sub-plan's waves could not be computed; forge-parallel.sh run reports it"
95
+ }
96
+
97
+ # Under the task table: the limits (always: they are what the user approves with the plan), what the
98
+ # planner split, and the stages it proposes after this plan.
99
+ forge_decompose_table() { # forge_decompose_table <plan>
100
+ local plan="$1" tree stages
101
+ forge_decompose_py limits "$plan" 2>/dev/null || return 0
102
+ if [ -f "$plan/auto_split" ]; then
103
+ echo " auto-split is on: a task that stops on \`split\` is split by the planner within these limits"
104
+ else
105
+ echo " a task that stops on \`split\` waits for your choice (run --auto-split lets the planner split it within these limits)"
106
+ fi
107
+ [ -z "${DECOMPOSE_LINES:-}" ] || printf '%s\n' "$DECOMPOSE_LINES" | sed 's/^/ /'
108
+ tree="$(forge_decompose_py tree "$plan" --split-only 2>/dev/null)"
109
+ if [ -n "$tree" ]; then
110
+ echo "subtasks (a split task runs its subtasks as a plan of its own, on its branch, and is then reviewed on all of it):"
111
+ printf '%s\n' "$tree" | sed 's/^/ /'
112
+ fi
113
+ stages="$(forge_decompose_py later "$plan" 2>/dev/null)"
114
+ if [ -n "$stages" ]; then
115
+ echo "later stages (the goal is larger than this plan; each is planned when the one before has merged):"
116
+ printf '%s\n' "$stages" | sed 's/^/ /'
117
+ echo " plan the next one with: forge-parallel.sh next $plan"
118
+ fi
119
+ echo
120
+ }
121
+
122
+ # --- run -------------------------------------------------------------------------------------
123
+
124
+ # The planner a split is asked of: run --split-planner, else the planner the plan was made with.
125
+ forge_split_planner() { # forge_split_planner <plan>
126
+ if [ -s "$1/split_planner" ]; then head -1 "$1/split_planner"
127
+ elif [ -s "$1/planner" ]; then head -1 "$1/planner"; fi
128
+ }
129
+
130
+ # run --auto-split / --no-auto-split / --split-planner, as do_run parsed them. Kept as plan files.
131
+ forge_auto_split_store() { # forge_auto_split_store <plan> <on|off|""> <planner spec|"">
132
+ local plan="$1"
133
+ [ -z "$3" ] || printf '%s\n' "$3" > "$plan/split_planner" || die "could not write $plan/split_planner"
134
+ case "$2" in
135
+ on) : > "$plan/auto_split" || die "could not write $plan/auto_split" ;;
136
+ off) rm -f "$plan/auto_split" ;;
137
+ esac
138
+ if [ -f "$plan/auto_split" ] && [ -z "$(forge_split_planner "$plan")" ]; then
139
+ die "--auto-split needs a planner to ask: give --split-planner SPEC (forge does not pick a model on your behalf)" 2
140
+ fi
141
+ }
142
+
143
+ # The summary block for tasks whose subtasks have not all merged: not failures of the task itself.
144
+ report_subtasks() { # report_subtasks <plan>
145
+ local plan="$1" id any=0
146
+ for id in $(all_ids "$plan/tasks.tsv"); do
147
+ [ -s "$plan/tasks/$id/sub.waiting" ] || continue
148
+ [ "$any" = 1 ] || echo "waiting for their subtasks:"
149
+ any=1
150
+ printf ' %s: %s\n' "$id" "$(tr '\n' ',' < "$plan/tasks/$id/sub.waiting" | sed 's/,$//; s/,/, /g')"
151
+ echo " its plan: $plan/tasks/$id/sub (retry a subtask there: forge-parallel.sh retry $plan/tasks/$id/sub <subtask>)"
152
+ done
153
+ [ "$any" = 0 ] || { echo " once they have merged, forge-parallel.sh run $plan brings them into the task and reviews it"; echo; }
154
+ }
155
+
156
+ # After a run whose every task merged: the stage the planner proposed next, if any.
157
+ report_later() { # report_later <plan>
158
+ local plan="$1" stages
159
+ forge_is_sub_plan "$plan" && return 0
160
+ stages="$(forge_decompose_py later "$plan" 2>/dev/null)"
161
+ [ -n "$stages" ] || return 0
162
+ [ "$(awk -F'\t' '$2 != "MERGED" { n++ } END { print n + 0 }' "$plan/results.tsv" 2>/dev/null)" = 0 ] || return 0
163
+ echo "this plan is one stage of a larger goal; the stages that follow:"
164
+ printf '%s\n' "$stages" | sed 's/^/ /'
165
+ echo " plan the next one (it starts from this plan's integration branch): forge-parallel.sh next $plan"
166
+ echo
167
+ }
168
+
169
+ # One slot of the tree's concurrency bound, held until this task process ends. A split task takes
170
+ # it for its own review: its scheduler gave its slot back while the subtasks ran. Never blocks a
171
+ # task that no scheduler is bounding (a `retry`).
172
+ forge_tree_slot() { # forge_tree_slot <task dir>
173
+ local root="${FORGE_TREE_ROOT:-}" cap="${FORGE_TREE_CAP:-}" ready="$1/.slot.ready.$$" pid
174
+ [ -n "$root" ] && [ -n "$cap" ] || return 0
175
+ rm -f "$ready"
176
+ python3 "$SKILL_DIR/scripts/forge_decompose.py" slot --root "$root" --cap "$cap" --while-pid "$$" --ready "$ready" \
177
+ </dev/null >/dev/null 2>&1 &
178
+ pid=$!
179
+ while [ ! -e "$ready" ] && kill -0 "$pid" 2>/dev/null; do sleep 0.2; done
180
+ rm -f "$ready"
181
+ }
182
+
183
+ # The entry of a split task: runs its sub-plan and, when every subtask has merged, brings the
184
+ # sub-plan's integration branch onto the task's branch. Returns 0 then, and the caller goes on to
185
+ # the snapshot and the review of the cumulative diff. Otherwise the status is written and it
186
+ # returns non-zero: BLOCKED with sub.waiting (a subtask failed, stopped or waits for a person; the
187
+ # attempt is not spent, and the next `run` enters here again), or INTERRUPTED.
188
+ run_split_task() { # run_split_task <plan> <id> <tdir> <wt> <base> <attempt>
189
+ local PLAN="$1" id="$2" tdir="$3" wt="$4" base="$5" attempt="$6"
190
+ local sub="$3/sub" REPO run_id sub_run sub_br start tip rc=0 left args=() grace
191
+ REPO="$(cat "$PLAN/repo")"; run_id="$(cat "$PLAN/run_id")"
192
+ sub_run="$(cat "$sub/run_id")"; sub_br="forge/$sub_run-integration"
193
+ rm -f "$tdir/sub.waiting" "$tdir/sub.infra"
194
+ if ! git -C "$REPO" rev-parse --verify --quiet "$sub_br" >/dev/null 2>&1; then
195
+ # First entry: where the subtasks start. `keep` builds on the task's work as it stands, with
196
+ # whatever an interrupted attempt left uncommitted; `fresh` starts over from the task's base and
197
+ # keeps the earlier work on a ref of its own.
198
+ start="$(python3 -c 'import json, sys
199
+ try:
200
+ print(json.load(open(sys.argv[1])).get("start") or "keep")
201
+ except Exception:
202
+ print("keep")' "$sub/parent.json" 2>/dev/null)"
203
+ if [ "$start" = fresh ]; then
204
+ tip="$(git -C "$wt" rev-parse HEAD)" || { echo ERROR > "$tdir/status"; return 1; }
205
+ if [ "$tip" != "$base" ]; then
206
+ git -C "$REPO" update-ref "refs/forge/kept/$run_id/$id" "$tip" >/dev/null 2>&1 \
207
+ && printf '%s\n' "$tip" > "$tdir/sub.kept" \
208
+ || { echo ERROR > "$tdir/status"; note "$id: could not keep its earlier work on a ref; nothing was reset"; return 1; }
209
+ note "$id: starting over from its base; its earlier work stays on refs/forge/kept/$run_id/$id"
210
+ fi
211
+ if ! { git -C "$wt" reset -q --hard "$base" && git -C "$wt" clean -fdq -e .forge; } >"$tdir/sub.reset.out" 2>&1; then
212
+ echo ERROR > "$tdir/status"; note "$id: could not reset its worktree to its base (see $tdir/sub.reset.out)"; return 1
213
+ fi
214
+ rm -f "$tdir/changes.diff" "$tdir/changes.prev.diff" "$tdir/reviewed.commit"
215
+ elif [ -n "$(git -C "$wt" status --porcelain -- . ':(exclude).forge' 2>/dev/null)" ]; then
216
+ (cd "$wt" && git add -A -- . ':(exclude).forge' && git -c user.name=forge -c user.email=forge@local \
217
+ commit -q -m "forge($id): work in progress, kept for its subtasks") >/dev/null 2>&1 \
218
+ || { echo ERROR > "$tdir/status"; note "$id: could not commit the work its subtasks build on"; return 1; }
219
+ fi
220
+ tip="$(git -C "$wt" rev-parse HEAD)" || { echo ERROR > "$tdir/status"; return 1; }
221
+ git -C "$REPO" branch "$sub_br" "$tip" >/dev/null 2>&1 \
222
+ || { echo ERROR > "$tdir/status"; note "$id: could not create $sub_br for its subtasks"; return 1; }
223
+ printf '%s\n' "$tip" > "$sub/integration.base"
224
+ fi
225
+ [ -s "$sub/waves.tsv" ] || forge_sub_prepare "$sub" \
226
+ || { echo ERROR > "$tdir/status"; note "$id: its sub-plan has no valid task order (see $sub/tasks.tsv)"; return 1; }
227
+ # The run's own budgets and bound apply to the subtasks too. A nested run gets a shorter grace on
228
+ # an interrupt than the run above it, so it has ended before that one stops waiting for it.
229
+ args=(--no-forecast --max-parallel "${FORGE_TREE_CAP:-$(cat "$PLAN/max_parallel" 2>/dev/null || echo 3)}")
230
+ [ "${FORGE_RUN_RETRY_FAILED:-0}" -gt 0 ] 2>/dev/null && args+=(--retry-failed "$FORGE_RUN_RETRY_FAILED")
231
+ [ "${FORGE_RUN_INFRA_RETRIES:-0}" -gt 0 ] 2>/dev/null && args+=(--infra-retries "$FORGE_RUN_INFRA_RETRIES")
232
+ grace="$(awk -v g="${FORGE_INTERRUPT_GRACE:-10}" 'BEGIN { g += 0; if (g <= 0) g = 10; printf "%.1f", g * 0.6 }')"
233
+ note "$id: running its $(all_ids "$sub/tasks.tsv" | grep -c .) subtasks (their plan: $sub)"
234
+ FORGE_INTERRUPT_GRACE="$grace" FORGE_PLAN_LOCK="" FORGE_DATASET_TRIGGER="" \
235
+ /bin/bash "$SELF" run "$sub" "${args[@]}" > "$tdir/sub.out" 2>&1 </dev/null || rc=$?
236
+ left="$(forge_decompose_py waiting "$PLAN" "$id" 2>/dev/null)"
237
+ if [ "$rc" = 130 ] || [ "$rc" -gt 128 ]; then
238
+ echo INTERRUPTED > "$tdir/status"
239
+ [ "$attempt" -gt 0 ] && echo $((attempt - 1)) > "$tdir/attempt"
240
+ note "$id: interrupted while its subtasks ran"; return 1
241
+ fi
242
+ if [ -n "$left" ] || { [ "$rc" != 0 ] && [ "$rc" != 5 ]; }; then
243
+ # Not a failure of this task: its subtasks have not all merged. Nothing was judged, so no
244
+ # attempt is spent; the next run enters here again and the sub-plan resumes where it is.
245
+ [ -n "$left" ] || left="its sub-plan run ended with exit $rc (see $tdir/sub.out)"
246
+ printf '%s\n' "$left" > "$tdir/sub.waiting"
247
+ [ "$rc" != 8 ] || : > "$tdir/sub.infra"
248
+ [ "$attempt" -gt 0 ] && echo $((attempt - 1)) > "$tdir/attempt"
249
+ echo BLOCKED > "$tdir/status"
250
+ if [ "$rc" = 8 ]; then
251
+ note "$id: its subtasks paused on an infrastructure failure ($(printf '%s' "$left" | tr '\n' ',' | sed 's/,$//; s/,/, /g')) — no attempt spent; re-run to resume"
252
+ else
253
+ note "$id: waiting for its subtasks ($(printf '%s' "$left" | tr '\n' ',' | sed 's/,$//; s/,/, /g')) — see $sub and $tdir/sub.out"
254
+ fi
255
+ return 1
256
+ fi
257
+ # Every subtask merged. Usually a fast-forward: the sub-plan's branch started at this task's tip.
258
+ if ! git -C "$wt" -c user.name=forge -c user.email=forge@local merge --no-edit -q "$sub_br" >"$tdir/sub.merge.out" 2>&1; then
259
+ git -C "$wt" merge --abort >/dev/null 2>&1 || true
260
+ echo ERROR > "$tdir/status"
261
+ note "$id: its subtasks' work ($sub_br) did not merge into its branch — see $tdir/sub.merge.out"; return 1
262
+ fi
263
+ git -C "$REPO" rev-parse "$sub_br" > "$tdir/sub.merged.tmp" && mv "$tdir/sub.merged.tmp" "$tdir/sub.merged" \
264
+ || { rm -f "$tdir/sub.merged.tmp"; echo ERROR > "$tdir/status"; return 1; }
265
+ # The sub-plan's integration worktree has done its job; its branch stays, so each subtask's work
266
+ # can still be read.
267
+ local sub_wt; sub_wt="$(cat "$sub/wt_root" 2>/dev/null)/_integration"
268
+ if [ -d "$sub_wt" ] && (cd "$REPO" && git worktree remove --force "$sub_wt" >/dev/null 2>&1); then
269
+ forge_worktree_event removed "$sub" "$REPO" "$sub_wt" "$sub_br" integration
270
+ fi
271
+ note "$id: its subtasks merged into its branch; reviewing the whole task"
272
+ forge_tree_slot "$tdir"
273
+ return 0
274
+ }
275
+
276
+ # --- the split of a task -----------------------------------------------------------------------
277
+
278
+ # One planner dispatch about one task: split it, or keep it whole. 0 when its reply was parsed into
279
+ # tasks/<id>/split.proposal.json, 8 on an infrastructure stop, 4 when it failed or was refused.
280
+ forge_split_ask_planner() { # forge_split_ask_planner <plan> <id> <keep|fresh> <spec>
281
+ local PLAN="$1" id="$2" start="$3" spec="$4" tdir="$1/tasks/$2" where rc=0 tier out
282
+ where="$(cat "$PLAN/wt_root" 2>/dev/null)/$id"
283
+ # The planner reads the task's work so far when the subtasks will build on it.
284
+ if [ "$start" = fresh ] || [ ! -d "$where" ]; then where="$(cat "$PLAN/repo")"; fi
285
+ tier="$(forge_fast_tier "$PLAN" planner)"
286
+ python3 "$SKILL_DIR/scripts/forge-planner.py" node-prompt --plan "$PLAN" --task "$id" --start "$start" >/dev/null || return 4
287
+ mkdir -p "$tdir/split" && cp "$tdir/split.prompt" "$tdir/split/planner.prompt" || return 4
288
+ note "$id: asking the planner ($spec) whether to split it"
289
+ /bin/bash "$DISPATCH" planner "$spec" --repo "$where" --run-dir "$tdir/split" --prompt-file "$tdir/split/planner.prompt" \
290
+ --output summary ${tier:+$tier} > "$tdir/split/planner.out" 2>&1 || rc=$?
291
+ if [ "$rc" = 8 ]; then
292
+ note "$id: the planner stopped on an infrastructure failure (see $tdir/split/planner.infra); the task is as it was"
293
+ return 8
294
+ fi
295
+ [ "$rc" = 0 ] || { note "$id: the planner failed (exit $rc; see $tdir/split/planner.out); the task is as it was"; return 4; }
296
+ out="$(python3 "$SKILL_DIR/scripts/forge-planner.py" node-parse --plan "$PLAN" --task "$id" --reply "$tdir/split/planner.last" 2>&1)" \
297
+ || { note "$id: the planner's ${out:-reply was refused}; the task is as it was"; return 4; }
298
+ }
299
+
300
+ # The split of a task, decided by a person (`retry --decide split`, by `user`) or by a run with
301
+ # --auto-split (by `auto`). The subtasks come from <file> (a person or an orchestrator wrote them),
302
+ # from the planner <spec>, or from a proposal already stored and checked (neither given). On 0 the
303
+ # sub-plan exists, the decision is recorded, and the task's next entry runs it; the caller clears
304
+ # the status. Any other return leaves the task as it was: 2 usage, 3 not split (kept whole, at a
305
+ # limit, or refused; a limit and a kept-whole answer are recorded on the node), 8 the planner
306
+ # stopped on infrastructure.
307
+ retry_split() { # retry_split <plan> <id> <split|split=fresh> <reason> <file|""> <spec|""> <user|auto>
308
+ local PLAN="$1" id="$2" decide="$3" reason="$4" file="$5" spec="$6" by="${7:-user}"
309
+ local tdir="$1/tasks/$2" start=keep out rc=0 attempt ctx="" n
310
+ case "$decide" in split=fresh) start=fresh ;; esac
311
+ if forge_is_split "$PLAN" "$id"; then
312
+ note "$id is already split into subtasks ($tdir/sub): they are kept, never made twice. forge-parallel.sh run $PLAN runs what is left of them"
313
+ return 3
314
+ fi
315
+ if [ -n "$file" ]; then
316
+ [ -f "$file" ] && [ -r "$file" ] || { note "retry: --subtasks '$file' is not a readable file"; return 2; }
317
+ out="$(python3 "$SKILL_DIR/scripts/forge-planner.py" node-parse --plan "$PLAN" --task "$id" --reply "$file" 2>&1)" \
318
+ || { note "$id: the ${out:-subtasks were refused}; nothing was changed"; return 3; }
319
+ elif [ -n "$spec" ]; then
320
+ forge_split_ask_planner "$PLAN" "$id" "$start" "$spec" || return $?
321
+ elif [ ! -s "$tdir/split.proposal.json" ]; then
322
+ note "retry: --decide split needs the subtasks: --subtasks FILE (a forge-plan block or its JSON, as forge-planner.py node-prompt describes it), or --planner SPEC to ask a planner"
323
+ return 2
324
+ fi
325
+ if [ "$start" = keep ] && [ -s "$tdir/qa.last" ] && grep -q 'FORGE_VERDICT' "$tdir/qa.last" 2>/dev/null; then
326
+ ctx="$tdir/.split.context"
327
+ {
328
+ echo "## The work so far, and its last review"
329
+ echo
330
+ echo "The parent task was attempted before it was split. That work is on the branch you start from: build on"
331
+ echo "it, and do not redo what is there and right. Its last review said:"
332
+ echo
333
+ tail -c 6000 "$tdir/qa.last"
334
+ } > "$ctx"
335
+ fi
336
+ out="$(forge_decompose_py split "$PLAN" "$id" --proposal "$tdir/split.proposal.json" --reason "$reason" --by "$by" \
337
+ --start "$start" ${ctx:+--context-file "$ctx"} 2>&1)" || rc=$?
338
+ rm -f "$tdir/split.proposal.json" "$tdir/.split.context"
339
+ case "$rc" in
340
+ 0) ;;
341
+ 4) note "$id: not split — ${out#bounded: }. The node is recorded as bounded and the task stays as it is"; return 3 ;;
342
+ 5) note "$id: kept whole — the answer was one task, not a split. That is recorded with its reason, and the task stays as it is"; return 3 ;;
343
+ *) note "$id: the split was refused — ${out#refused: }; nothing was changed"; return 3 ;;
344
+ esac
345
+ forge_sub_prepare_all "$PLAN" || { note "$id: its sub-plan has no valid task order (see $tdir/sub/tasks.tsv)"; }
346
+ # The decision, as every decision is recorded. It grants the one attempt that reviews the whole
347
+ # task once its subtasks have merged (retry=1 is what the attempt cap reads).
348
+ attempt="$(cat "$tdir/attempt" 2>/dev/null || echo 0)"
349
+ reason="$(printf '%s' "$reason" | tr '\n\r\t' ' ' | sed 's/^ *//; s/ *$//')"
350
+ printf '%s\t%s\t%s\t%s\n' "$(date +%s)" "$attempt" "retry=1 $decide" "$reason" >> "$tdir/decisions.tsv" \
351
+ || note "$id: could not record the decision in decisions.tsv"
352
+ if [ "$start" = fresh ]; then
353
+ rm -f "$tdir/retry_findings.md" "$tdir/retry_findings.commit" "$tdir/retry_findings.source" "$tdir/retry_findings.session" \
354
+ "$tdir/retry_findings.resolved" "$tdir/retry_findings.dwarf.session" "$tdir/retry_findings.dwarf.resolved"
355
+ elif grep -q 'FORGE_VERDICT' "$tdir/qa.last" 2>/dev/null; then
356
+ carry_findings "$tdir" # the review of the whole task is told what the last one found
357
+ fi
358
+ rm -f "$tdir/escalation.stop" "$tdir/split.suggested" "$tdir/escalation.next" "$tdir/decided.left" "$tdir/noretry" \
359
+ "$tdir/decision.md" "$tdir/decision.from" "$tdir/resume" "$tdir/sub.waiting" "$tdir/sub.infra"
360
+ forge_dataset_event decision --dir "$PLAN" --task "$id" --set "choice=$decide" --set "reason=$reason" \
361
+ --set "attempt=$attempt" --set "by=$by"
362
+ forge_decompose_snapshot "$PLAN"
363
+ n="$(all_ids "$tdir/sub/tasks.tsv" | grep -c .)"
364
+ note "$id: split into $n subtasks ($(all_ids "$tdir/sub/tasks.tsv" | tr '\n' ' ' | sed 's/ $//')); their plan is $tdir/sub"
365
+ return 0
366
+ }
367
+
368
+ # run --auto-split (#153): a task that just stopped for a decision with `split` among its reasons is
369
+ # split by the planner, within the limits, without waiting. 0 when it was: the caller enters the
370
+ # task again, which now runs its subtasks. Asked once per task: a task the planner kept whole, or a
371
+ # split a limit refused, waits for the user like any other stop.
372
+ forge_auto_split() { # forge_auto_split <plan> <id>
373
+ local PLAN="$1" id="$2" tdir="$1/tasks/$2" reasons spec rc=0
374
+ [ -f "$PLAN/auto_split" ] || return 1
375
+ [ "$(task_status "$PLAN" "$id")" = NEEDS_DECISION ] || return 1
376
+ reasons="$(sed -n '1s/^reasons: //p' "$tdir/decision.md" 2>/dev/null)"
377
+ case ", $reasons," in *", split,"*) ;; *) return 1 ;; esac
378
+ forge_is_split "$PLAN" "$id" && return 1
379
+ [ ! -f "$tdir/auto_split.asked" ] || return 1
380
+ spec="$(forge_split_planner "$PLAN")"
381
+ [ -n "$spec" ] || { note "$id: --auto-split has no planner to ask (run --split-planner SPEC); the task waits for your decision"; return 1; }
382
+ : > "$tdir/auto_split.asked"
383
+ retry_split "$PLAN" "$id" split "auto-split: the task stopped on split ($reasons)" "" "$spec" auto || rc=$?
384
+ if [ "$rc" != 0 ]; then
385
+ # An infrastructure stop of the planner decided nothing: the next run asks again.
386
+ [ "$rc" != 8 ] || rm -f "$tdir/auto_split.asked"
387
+ return 1
388
+ fi
389
+ rm -f "$tdir/status"
390
+ return 0
391
+ }
392
+
393
+ # --- goal -> plans -----------------------------------------------------------------------------
394
+
395
+ # next <plan> [<new-plan-dir>] [--planner SPEC] [plan flags...]: the plan of the next stage of a
396
+ # larger goal. <plan> must have merged. The new plan starts from its integration branch (--after),
397
+ # takes its routing pools, its options and its limits, and is planned by the planner from the code
398
+ # as it is now. It stops at the table: every plan of a chain is approved on its own.
399
+ do_next() {
400
+ local PLAN="${1:?next needs a plan dir}"; shift
401
+ local NEW="" PLANNER="" EXTRA=() out new repo f key value pools=()
402
+ if [ $# -gt 0 ] && [ "${1#-}" = "$1" ]; then NEW="$1"; shift; fi
403
+ while [ $# -gt 0 ]; do
404
+ case "$1" in
405
+ --planner) PLANNER="${2:?--planner needs a spec}"; shift 2 ;;
406
+ --approved) die "next plans the stage and stops at its table: each plan of a chain is approved on its own, then run with forge-parallel.sh run" 2 ;;
407
+ *) EXTRA+=("$1"); shift ;;
408
+ esac
409
+ done
410
+ [ -s "$PLAN/tasks.tsv" ] || die "no tasks.tsv in '$PLAN'"
411
+ forge_refuse_fractal_run "$PLAN" next
412
+ [ -n "$PLANNER" ] || PLANNER="$(head -1 "$PLAN/planner" 2>/dev/null || true)"
413
+ [ -n "$PLANNER" ] || die "next needs a planner for the new plan: --planner SPEC" 2
414
+ repo="$(cat "$PLAN/repo" 2>/dev/null)"
415
+ [ -n "$repo" ] && [ -d "$repo" ] || die "'$PLAN' has no repository to plan in" 3
416
+ out="$(forge_decompose_py next "$PLAN" ${NEW:+--dir "$NEW"} 2>&1)" || die "next: ${out#refused: }" 3
417
+ new="$out"
418
+ # What the plan before it was made with applies to this one: options, pools, limits.
419
+ for f in no_ripwire no_memory yolo_dwarf yolo_qa reviewer_session implementer_session fast pre_review_tests \
420
+ pre_review_parallel fail_first qa_threshold qa_test_minutes timeout verify_retries verify_cmd verify_parallel setup_cmd setup_before_verify final_qa \
421
+ explore escalate_after retry_per_model max_attempts profile.json retro_mode summaries summaries_model \
422
+ auto_split split_planner; do
423
+ [ ! -f "$PLAN/$f" ] || cp -p "$PLAN/$f" "$new/$f" || die "could not write $new/$f" 3
424
+ done
425
+ [ ! -d "$PLAN/verify.d" ] || cp -Rp "$PLAN/verify.d" "$new/verify.d" || die "could not write $new/verify.d" 3
426
+ while IFS="$(printf '\t')" read -r key value; do
427
+ [ -n "$key" ] && [ -n "$value" ] || continue
428
+ pools+=("$key" "$value")
429
+ done <<FORGE_POOLS
430
+ $(python3 - "$(forge_pools_file "$PLAN")" "$PLAN/qa-routing.json" "$PLAN/decomposition.json" <<'PY'
431
+ import json, sys
432
+ def load(path):
433
+ try:
434
+ data = json.load(open(path, encoding='utf-8'))
435
+ except (OSError, ValueError):
436
+ return {}
437
+ return data if isinstance(data, dict) else {}
438
+ for role, path in (('dwarf', sys.argv[1]), ('qa', sys.argv[2])):
439
+ for tier, flag in (('any', '--' + role), ('low', '--%s-low' % role), ('medium', '--%s-medium' % role), ('high', '--%s-high' % role)):
440
+ specs = load(path).get(tier)
441
+ if isinstance(specs, list) and specs and all(isinstance(s, str) and s and '\t' not in s for s in specs):
442
+ print('%s\t%s' % (flag, ','.join(specs)))
443
+ limits = load(sys.argv[3]).get('limits')
444
+ if isinstance(limits, dict):
445
+ for key in ('depth', 'children', 'nodes'):
446
+ if type(limits.get(key)) is int:
447
+ print('--split-%s\t%d' % (key, limits[key]))
448
+ PY
449
+ )
450
+ FORGE_POOLS
451
+ note "next: planning stage $(python3 -c 'import json, sys; print(json.load(open(sys.argv[1])).get("stage", "?"))' "$new/chain.json" 2>/dev/null) in $new, from the integration branch of $PLAN"
452
+ /bin/bash "$SELF" auto "$new" --repo "$repo" --goal-file "$new/request.md" --plan always --planner "$PLANNER" \
453
+ --after "$PLAN" ${pools[@]+"${pools[@]}"} ${EXTRA[@]+"${EXTRA[@]}"}
454
+ }
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env bash
2
+ # Shared environment helpers for cross-platform shell entrypoints.
3
+ #
4
+ # On Windows (Git Bash / MSYS2), `python3` on PATH is often the Microsoft Store
5
+ # execution alias in WindowsApps while a real Python 3.9+ lives at `python` or
6
+ # `py -3`. Prepending a small per-user wrapper directory to PATH fixes every
7
+ # downstream `python3` call (in bash scripts and Windows subprocesses) with zero
8
+ # overhead or external commands on macOS/Linux.
9
+
10
+ forge_is_windows() {
11
+ case "${OSTYPE:-}" in
12
+ msys*|cygwin*) [ "${FORGE_WIN_MODE:-native}" != msys2 ] ;; # MSYS2 mode (#164) runs forge as on POSIX
13
+ *) return 1 ;;
14
+ esac
15
+ }
16
+
17
+ forge_posix_path() {
18
+ local p="${1:-}"
19
+ p="${p%$'\r'}"
20
+ if [ -n "$p" ] && forge_is_windows && command -v cygpath >/dev/null 2>&1; then
21
+ cygpath -u "$p" 2>/dev/null || printf '%s\n' "$p"
22
+ else
23
+ printf '%s\n' "$p"
24
+ fi
25
+ }
26
+
27
+ forge_win_path() {
28
+ local p="${1:-}"
29
+ p="${p%$'\r'}"
30
+ if [ -n "$p" ] && forge_is_windows && command -v cygpath >/dev/null 2>&1; then
31
+ cygpath -w "$p" 2>/dev/null || printf '%s\n' "$p"
32
+ else
33
+ printf '%s\n' "$p"
34
+ fi
35
+ }
36
+
37
+ forge_host_path() {
38
+ local p="${1:-}"
39
+ p="${p%$'\r'}"
40
+ if [ -n "$p" ] && forge_is_windows && command -v cygpath >/dev/null 2>&1; then
41
+ cygpath -m "$p" 2>/dev/null || printf '%s\n' "$p"
42
+ else
43
+ printf '%s\n' "$p"
44
+ fi
45
+ }
46
+
47
+ # forge_repo_memory_off REPO: `forge setup` chose no project memory for this clone
48
+ # (git config forge.memory off). The env var and --no-memory work as before, on top.
49
+ forge_repo_memory_off() {
50
+ [ "$(git -C "${1:-.}" config --get forge.memory 2>/dev/null)" = off ]
51
+ }
52
+
53
+ forge_ensure_python3() {
54
+ forge_is_windows || return 0
55
+
56
+ local current exe win_exe cand base shim_dir shim cmd_shim expected cmd_expected
57
+ current="$(command -v python3 2>/dev/null || true)"
58
+ case "$current" in
59
+ '') ;;
60
+ *[Ww]indows[Aa]pps*) ;;
61
+ */forge/bin/python3) ;;
62
+ *) return 0 ;;
63
+ esac
64
+
65
+ exe=""
66
+ for cand in python python3; do
67
+ command -v "$cand" >/dev/null 2>&1 || continue
68
+ case "$(command -v "$cand" 2>/dev/null || true)" in
69
+ *[Ww]indows[Aa]pps*) continue ;;
70
+ esac
71
+ exe="$("$cand" -c 'import sys; print(sys.executable if sys.version_info >= (3, 9) else "")' 2>/dev/null || true)"
72
+ [ -n "$exe" ] && break
73
+ done
74
+ if [ -z "$exe" ] && command -v py >/dev/null 2>&1; then
75
+ exe="$(py -3 -c 'import sys; print(sys.executable if sys.version_info >= (3, 9) else "")' 2>/dev/null || true)"
76
+ fi
77
+ [ -n "$exe" ] || return 0
78
+
79
+ exe="${exe%$'\r'}"
80
+ win_exe="$exe"
81
+ if command -v cygpath >/dev/null 2>&1; then
82
+ win_exe="$(cygpath -w "$exe" 2>/dev/null || printf '%s' "$exe")"
83
+ exe="$(cygpath -u "$exe" 2>/dev/null || printf '%s' "$exe")"
84
+ fi
85
+
86
+ base="${LOCALAPPDATA:-${XDG_STATE_HOME:-${HOME:-}/.local/state}}"
87
+ [ -n "$base" ] || return 0
88
+ base="$(forge_posix_path "$base")"
89
+ shim_dir="$base/forge/bin"
90
+ ( umask 077 && mkdir -p "$shim_dir" ) 2>/dev/null || return 0
91
+
92
+ shim="$shim_dir/python3"
93
+ printf -v expected '#!/usr/bin/env bash\nexec %q "$@"\n' "$exe"
94
+ if [ ! -x "$shim" ] || [ "$(cat "$shim" 2>/dev/null)" != "${expected%$'\n'}" ]; then
95
+ printf '%s' "$expected" > "$shim" 2>/dev/null || return 0
96
+ chmod +x "$shim" 2>/dev/null || true
97
+ fi
98
+
99
+ cmd_shim="$shim_dir/python3.cmd"
100
+ printf -v cmd_expected '@echo off\r\n"%s" %%*\r\n' "$win_exe"
101
+ if [ ! -f "$cmd_shim" ] || [ "$(cat "$cmd_shim" 2>/dev/null)" != "${cmd_expected%$'\n'}" ]; then
102
+ printf '%s' "$cmd_expected" > "$cmd_shim" 2>/dev/null || true
103
+ fi
104
+
105
+ case ":$PATH:" in
106
+ *":$shim_dir:"*) ;;
107
+ *) export PATH="$shim_dir:$PATH" ;;
108
+ esac
109
+ }
110
+
111
+ _FORGE_ENV_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
112
+
113
+ forge_winpid() {
114
+ local pid="${1:-}" win=""
115
+ [ -n "$pid" ] || return 0
116
+ # Under MSYS2 mode /proc/<pid>/winpid exists too, but forge works with Cygwin pids there.
117
+ if forge_is_windows && [ -r "/proc/$pid/winpid" ]; then
118
+ read -r win 2>/dev/null < "/proc/$pid/winpid" || win=""
119
+ win="${win%$'\r'}"
120
+ fi
121
+ printf '%s\n' "${win:-$pid}"
122
+ }
123
+
124
+ forge_kill_children() {
125
+ local pid="${1:-}" sig="${2:-TERM}" cpid winpid
126
+ [ -n "$pid" ] || return 0
127
+ if command -v pkill >/dev/null 2>&1; then
128
+ pkill "-$sig" -P "$pid" 2>/dev/null || true
129
+ return 0
130
+ fi
131
+ # Git Bash / MSYS2 has no pkill: signal MSYS children via ps -ef and native children via forge-guard.py.
132
+ if [ -r "/proc/$pid/winpid" ]; then
133
+ while read -r cpid; do
134
+ [ -n "$cpid" ] && kill "-$sig" "$cpid" 2>/dev/null || true
135
+ done < <(ps -ef 2>/dev/null | awk -v p="$pid" '$3 == p { print $2 }')
136
+ winpid="$(forge_winpid "$pid")"
137
+ if [ -r "$_FORGE_ENV_DIR/forge-guard.py" ] && [ -n "$winpid" ]; then
138
+ python3 "$_FORGE_ENV_DIR/forge-guard.py" kill-children "$winpid" "$sig" 2>/dev/null || true
139
+ fi
140
+ fi
141
+ }
142
+
143
+ forge_kill_group() {
144
+ local pid="${1:-}" sig="${2:-TERM}"
145
+ [ -n "$pid" ] || return 0
146
+ kill -s "$sig" -- "-$pid" 2>/dev/null || true
147
+ if forge_is_windows && [ -r "/proc/$pid/winpid" ]; then
148
+ forge_kill_children "$pid" "$sig"
149
+ fi
150
+ }
151
+
152
+ # Create a live directory link (<src> -> <dest>). Uses NTFS directory junctions on Windows
153
+ # (no admin/Developer Mode required; unlike MSYS `ln -sfn`, edits remain live and `-L` works).
154
+ forge_link_dir() {
155
+ local src="${1:-}" dest="${2:-}"
156
+ if forge_is_windows; then
157
+ if [ -L "$dest" ]; then
158
+ rmdir "$dest" 2>/dev/null || rm -f "$dest" 2>/dev/null || true
159
+ fi
160
+ MSYS_NO_PATHCONV=1 cmd /c mklink /J "$(forge_win_path "$dest")" "$(forge_win_path "$src")" >/dev/null 2>&1
161
+ return $?
162
+ fi
163
+ ln -sfn "$src" "$dest"
164
+ }
165
+
166
+ # Mark an earlier verification result STALE when accepted.integration moves past verification.commit
167
+ # (#194): accept and deferred merges leave the new tip unverified until run or integrate verifies it.
168
+ mark_verification_stale() { # mark_verification_stale <plan> <new-tip>
169
+ local plan="$1" tip="${2:-}" cur_status cur_vc
170
+ cur_status="$(cat "$plan/verification.status" 2>/dev/null || true)"
171
+ case "$cur_status" in
172
+ PASS|FAIL|INTERRUPTED)
173
+ cur_vc="$(cat "$plan/verification.commit" 2>/dev/null || true)"
174
+ if [ "$cur_vc" != "$tip" ]; then
175
+ echo STALE > "$plan/verification.status"
176
+ rm -f "$plan/verification.exit"
177
+ fi ;;
178
+ esac
179
+ }