@giamme/forge 0.15.0 → 0.17.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 (80) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/VERSION +1 -1
  3. package/docs/artifacts.md +1 -1
  4. package/docs/commands.md +14 -3
  5. package/docs/dataset.md +8 -0
  6. package/docs/forge-ui.md +14 -0
  7. package/docs/memory.md +22 -0
  8. package/docs/reviews.md +22 -1
  9. package/forge +11 -0
  10. package/package.json +1 -1
  11. package/references/dataset.md +129 -7
  12. package/references/decision-models.md +15 -2
  13. package/references/decompose.md +95 -13
  14. package/references/harnesses.md +4 -1
  15. package/references/planning.md +3 -2
  16. package/references/solo.md +5 -1
  17. package/references/ui.md +10 -1
  18. package/scripts/forge-artifact.sh +53 -0
  19. package/scripts/forge-contract.py +107 -9
  20. package/scripts/forge-control.py +0 -2
  21. package/scripts/forge-dispatch.sh +28 -12
  22. package/scripts/forge-fail-first.py +3 -1
  23. package/scripts/forge-guard.py +247 -8
  24. package/scripts/forge-issues.py +16 -3
  25. package/scripts/forge-ledger.py +3 -1
  26. package/scripts/forge-lib-adopt.sh +266 -0
  27. package/scripts/forge-lib-attribute.sh +10 -2
  28. package/scripts/forge-lib-plans.sh +16 -4
  29. package/scripts/forge-lib-review.sh +8 -2
  30. package/scripts/forge-lib-task.sh +19 -5
  31. package/scripts/forge-memory.sh +41 -4
  32. package/scripts/forge-metrics.sh +29 -0
  33. package/scripts/forge-parallel.sh +99 -14
  34. package/scripts/forge-plan-check.py +25 -2
  35. package/scripts/forge-planner.py +20 -6
  36. package/scripts/forge-route.py +16 -2
  37. package/scripts/forge-runs-rm.py +23 -7
  38. package/scripts/forge-runtime.py +25 -2
  39. package/scripts/forge-schedule.py +5 -2
  40. package/scripts/forge-shadow.py +30 -9
  41. package/scripts/forge-solo.sh +6 -0
  42. package/scripts/forge-sweep.py +25 -10
  43. package/scripts/forge_dataset/cli.py +99 -0
  44. package/scripts/forge_dataset/export.py +1 -0
  45. package/scripts/forge_dataset/facts.py +278 -0
  46. package/scripts/forge_dataset/harvest.py +27 -3
  47. package/scripts/forge_dataset/history.py +126 -0
  48. package/scripts/forge_dataset/outcomes.py +206 -58
  49. package/scripts/forge_dataset/pairs.py +174 -0
  50. package/scripts/forge_dataset/risk.py +108 -0
  51. package/scripts/forge_dataset/tiers.py +427 -0
  52. package/scripts/forge_dataset/worktrees.py +95 -0
  53. package/scripts/forge_decide/cli.py +23 -2
  54. package/scripts/forge_decide/labels.py +30 -6
  55. package/scripts/forge_decide/replay.py +10 -4
  56. package/scripts/forge_memory_list.py +153 -0
  57. package/scripts/forge_worktrees.py +88 -0
  58. package/ui/README.md +31 -0
  59. package/ui/docs/api.md +92 -6
  60. package/ui/forge_ui/__init__.py +14 -1
  61. package/ui/forge_ui/__main__.py +1 -1
  62. package/ui/forge_ui/control.py +77 -0
  63. package/ui/forge_ui/export.py +9 -2
  64. package/ui/forge_ui/findings.py +11 -2
  65. package/ui/forge_ui/projects.py +571 -0
  66. package/ui/forge_ui/reader.py +96 -9
  67. package/ui/forge_ui/server.py +8 -0
  68. package/ui/forge_ui/static/app.js +105 -22
  69. package/ui/forge_ui/static/cloudui.js +6 -2
  70. package/ui/forge_ui/static/convo.js +1 -1
  71. package/ui/forge_ui/static/diffview.js +9 -5
  72. package/ui/forge_ui/static/files.js +1 -1
  73. package/ui/forge_ui/static/index.html +5 -1
  74. package/ui/forge_ui/static/panel.js +69 -2
  75. package/ui/forge_ui/static/projectsview.js +372 -0
  76. package/ui/forge_ui/static/settings.js +136 -4
  77. package/ui/forge_ui/static/style.css +94 -3
  78. package/ui/forge_ui/static/uikit.js +94 -1
  79. package/ui/forge_ui/static/worktreesview.js +250 -0
  80. package/ui/forge_ui/worktrees.py +570 -0
@@ -0,0 +1,266 @@
1
+ #!/usr/bin/env bash
2
+ # `adopt` for forge-parallel.sh (#79): a commit made outside forge comes into a run's integration
3
+ # branch, reviewed on its own by the run's QA. Sourced; functions only.
4
+ #
5
+ # forge-parallel.sh adopt <plan-dir> <commit|a..b> --reason "<why>" [--qa <spec>] [--yolo-qa] [--approved]
6
+ # forge-parallel.sh adopt <plan-dir> --head --reason "<why>" [--qa <spec>] [--yolo-qa] [--approved]
7
+ #
8
+ # The first cherry-picks onto the integration branch. --head takes what is already on it past the
9
+ # revision the run accepted, the case `review` refuses. Either way the reviewer sees only that range.
10
+ # PASS moves accepted.integration. A FAIL or an unknown verdict puts the branch back unless --approved
11
+ # overrides it, which is recorded; a review that concluded nothing (an infrastructure stop, a crash, a
12
+ # timeout) always puts it back. A worktree that changed during the review is left as it is: resetting
13
+ # it would destroy the change.
14
+ # adopt-<epoch>-<pid>/ keeps the review; adoptions.tsv holds one row per adopted range: epoch, base,
15
+ # tip, verdict, approved (0|1), spec, the review's directory name, reason. There is no task row: nothing here
16
+ # was a model's implementation, and a final review reads the whole range anyway.
17
+
18
+ adopt_rows() { # adopt_rows <plan>: the adopted ranges, as lines for a prompt
19
+ [ -s "$1/adoptions.tsv" ] || return 0
20
+ echo "Commits adopted from outside this run, each reviewed alone (they are in the diff below):"
21
+ awk -F'\t' '{ printf " - %s..%s (%s%s): %s\n", substr($2, 1, 12), substr($3, 1, 12), $4, ($5 == 1 ? ", approved over it" : ""), $8 }' "$1/adoptions.tsv"
22
+ echo
23
+ }
24
+
25
+ do_adopt() {
26
+ local PLAN="${1:?adopt needs a plan dir}"; shift
27
+ local target="" head=0 reason="" approved=0 spec="" yolo_given=""
28
+ while [ $# -gt 0 ]; do
29
+ case "$1" in
30
+ --head) head=1; shift ;;
31
+ --reason) [ $# -ge 2 ] || die "adopt: --reason needs a text"; reason="$2"; shift 2 ;;
32
+ --qa) [ $# -ge 2 ] || die "adopt: --qa needs a spec"; spec="$2"; shift 2 ;;
33
+ --yolo-qa) yolo_given=1; shift ;;
34
+ --approved) approved=1; shift ;;
35
+ -*) die "adopt: unknown option '$1'" ;;
36
+ *) [ -z "$target" ] || die "adopt: unexpected argument '$1'"; target="$1"; shift ;;
37
+ esac
38
+ done
39
+ { [ -n "$target" ] && [ "$head" = 0 ]; } || { [ -z "$target" ] && [ "$head" = 1 ]; } \
40
+ || die "usage: forge-parallel.sh adopt <plan-dir> <commit|a..b> | --head --reason \"<why>\" [--qa <spec>] [--yolo-qa] [--approved]"
41
+ reason="$(printf '%s' "$reason" | tr '\n\r\t' ' ' | sed 's/^ *//; s/ *$//')"
42
+ [ -n "$reason" ] || die "adopt needs --reason \"<why this commit belongs in the run>\": the reviewer judges against it, and it is recorded"
43
+
44
+ local T="$PLAN/tasks.tsv" int_wt base now tip commits=() n
45
+ [ -s "$T" ] || die "no tasks.tsv in '$PLAN'" 3
46
+ [ -s "$PLAN/wt_root" ] && [ -s "$PLAN/run_id" ] && [ -s "$PLAN/repo" ] && [ -s "$PLAN/accepted.integration" ] \
47
+ || die "this plan has never been run, so it has no integration branch to adopt into" 3
48
+ int_wt="$(cat "$PLAN/wt_root")/_integration"
49
+ [ -e "$int_wt/.git" ] || die "the integration worktree $int_wt is missing" 3
50
+ [ -z "$(git -C "$int_wt" status --porcelain -- . ':(exclude).forge' 2>/dev/null)" ] \
51
+ || die "the integration worktree $int_wt has uncommitted changes; commit them, or clean it, first" 3
52
+ base="$(cat "$PLAN/accepted.integration")"
53
+ now="$(git -C "$int_wt" rev-parse HEAD)" || die "cannot read the integration worktree $int_wt" 3
54
+
55
+ if [ "$head" = 1 ]; then
56
+ [ "$now" != "$base" ] || die "the integration branch is at the revision this run accepted; there is nothing to adopt" 3
57
+ git -C "$int_wt" merge-base --is-ancestor "$base" "$now" \
58
+ || die "the integration branch no longer contains the revision this run accepted ($(printf '%.12s' "$base")); it was rewritten, and adopt only takes commits on top of it" 3
59
+ else
60
+ [ "$now" = "$base" ] || die "the integration branch has moved past the revision this run accepted; adopt that first with --head" 3
61
+ local range
62
+ case "$target" in
63
+ *..*) range="$target" ;;
64
+ *) git -C "$int_wt" rev-parse --verify --quiet "$target^{commit}" >/dev/null || die "adopt: '$target' is not a commit here" 3
65
+ range="$target^!" ;;
66
+ esac
67
+ # shellcheck disable=SC2207 # commit ids are single words
68
+ commits=($(git -C "$int_wt" rev-list --reverse "$range" 2>/dev/null)) || die "adopt: '$target' is not a commit range here" 3
69
+ [ "${#commits[@]}" -gt 0 ] || die "adopt: '$target' holds no commits" 3
70
+ [ -z "$(git -C "$int_wt" rev-list --min-parents=2 "$range" 2>/dev/null)" ] \
71
+ || die "adopt: '$target' includes a merge commit; adopt the commits it merged" 3
72
+ for n in "${commits[@]}"; do
73
+ git -C "$int_wt" merge-base --is-ancestor "$n" "$base" && die "adopt: $(printf '%.12s' "$n") is already on the integration branch" 3
74
+ done
75
+ fi
76
+
77
+ # Everything that can refuse without a model goes first, so a bad spec moves nothing.
78
+ [ -n "$spec" ] || spec="$(cat "$PLAN/final_qa" 2>/dev/null)"
79
+ [ -n "$spec" ] || spec="$(field "$T" "$(all_ids "$T" | head -1)" qa)"
80
+ [ -n "$spec" ] || die "adopt: no QA spec; name one with --qa" 3
81
+ local out ft rc=0 yolo tflag=""
82
+ out="$PLAN/adopt-$(date +%s)-$$"
83
+ mkdir -p "$out" && out="$(cd "$out" && pwd)" || die "adopt: cannot create $out" 3
84
+ ft="$(forge_fast_tier "$PLAN" review)"
85
+ yolo="$(review_yolo_flag "$PLAN" "$yolo_given")"
86
+ [ -s "$PLAN/timeout" ] && tflag="--timeout $(cat "$PLAN/timeout")"
87
+ FORGE_CAPABILITY_CACHE="${FORGE_CAPABILITY_CACHE:-$PLAN/capabilities}" \
88
+ /bin/bash "$DISPATCH" doctor --spec "$spec" --role qa $yolo $ft > "$out/doctor.out" 2>&1 \
89
+ || { cat "$out/doctor.out" >&2; die "adopt: QA spec '$spec' cannot be used (see above); nothing was applied" 3; }
90
+
91
+ # Back to the accepted revision, for every outcome that does not adopt. A range that was only on
92
+ # this branch (--head) keeps a ref: the branch was its one home. A worktree that changed since its
93
+ # fingerprint was taken stays as it is, and says how to go on.
94
+ local source_fp="" review_fp="" tip="$now"
95
+ adopt_rollback() {
96
+ trap - INT TERM
97
+ if [ -n "$source_fp" ] && [ "$(forge_fingerprint "$int_wt" 2>/dev/null)" != "$source_fp" ]; then
98
+ echo "the integration worktree changed during the review, so forge left it as it is. Nothing was adopted: accepted.integration is still $(printf '%.12s' "$base")."
99
+ if [ -n "$(git -C "$int_wt" status --porcelain -- . ':(exclude).forge' 2>/dev/null)" ]; then
100
+ echo "keep what it holds: commit the edits in $int_wt, then: forge-parallel.sh adopt $PLAN --head --reason \"...\""
101
+ else
102
+ echo "keep what it holds with: forge-parallel.sh adopt $PLAN --head --reason \"...\""
103
+ fi
104
+ echo "or put it back with: git -C $int_wt reset --hard $base"
105
+ return 0
106
+ fi
107
+ git -C "$int_wt" cherry-pick --abort >/dev/null 2>&1
108
+ git -C "$int_wt" reset -q --hard "$base" || note "adopt: could not put the integration branch back at $base"
109
+ if [ "$head" = 1 ]; then
110
+ git -C "$int_wt" update-ref "refs/forge/adopt-refused/$(basename "$out")" "$tip" 2>/dev/null
111
+ echo "the integration branch is back at $(printf '%.12s' "$base"); your commits are at $tip (ref refs/forge/adopt-refused/$(basename "$out"))"
112
+ else
113
+ echo "the integration branch is back at $(printf '%.12s' "$base")"
114
+ fi
115
+ }
116
+ # A signal must not leave the range on the branch with accepted.integration behind it: that refuses
117
+ # every later review, run and accept.
118
+ trap 'adopt_rollback; exit 130' INT
119
+ trap 'adopt_rollback; exit 143' TERM
120
+
121
+ if [ "$head" = 0 ]; then
122
+ if ! git -C "$int_wt" cherry-pick "${commits[@]}" > "$out/apply.log" 2>&1; then
123
+ trap - INT TERM
124
+ git -C "$int_wt" cherry-pick --abort >/dev/null 2>&1
125
+ git -C "$int_wt" reset -q --hard "$base"
126
+ die "adopt: '$target' does not apply cleanly on the integration branch (see $out/apply.log); nothing was adopted" 6
127
+ fi
128
+ fi
129
+ tip="$(git -C "$int_wt" rev-parse HEAD)"
130
+
131
+ local review="$out/review" thr verdict gate_line decision kept inline=1 vcmd
132
+ forge_review_snapshot "$int_wt" "$base" "$tip" "$review" > "$out/snapshot.log" 2>&1 \
133
+ || { adopt_rollback; die "adopt: cannot build the review snapshot (see $out/snapshot.log)" 3; }
134
+ forge_review_deps "$int_wt" "$review" "$out/review.deps"
135
+ source_fp="$(forge_fingerprint "$int_wt")" && review_fp="$(forge_fingerprint "$review" --tracked)" \
136
+ || { adopt_rollback; die "adopt: cannot fingerprint the checkouts" 3; }
137
+ thr="$(qa_threshold_for "$PLAN" __adopt__)"
138
+ printf '%s\n' "$thr" > "$out/qa_threshold" # what forge issues reads the kept findings under
139
+ git -C "$int_wt" diff --binary --irreversible-delete "$base" "$tip" -- . ':(exclude).forge' | head -c $((REVIEW_DIFF_LIMIT + 1)) > "$out/range.diff"
140
+ if [ "$(wc -c < "$out/range.diff" | tr -d ' ')" -gt "$REVIEW_DIFF_LIMIT" ]; then inline=0; rm -f "$out/range.diff"; fi
141
+ {
142
+ if [ -s "$PLAN/goal.txt" ]; then echo "## The run's goal"; echo; cat "$PLAN/goal.txt"; echo; fi
143
+ echo "## Why this is being adopted"; echo; printf '%s\n' "$reason"; echo
144
+ echo "## The commits"; echo; git -C "$int_wt" log --format='%h %an: %s' "$base..$tip"; echo
145
+ python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part head --kind adopt
146
+ echo
147
+ cat "$out/review.deps"
148
+ echo "Forge ran no tests of its own on this range, so nothing here stands in for yours."
149
+ # The run's verification, named and not run: it runs after this review, on the whole branch (#131).
150
+ vcmd="$(verify_command_for "$PLAN" "$(cat "$PLAN/repo")" | head -c 4000)"
151
+ if [ -n "$vcmd" ]; then
152
+ echo "The run verifies its integration branch with the command below, after this review. Run the parts of it that touch this range:"
153
+ echo '```sh'; printf '%s\n' "$vcmd"; echo '```'
154
+ fi
155
+ echo
156
+ python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part tail --kind adopt --threshold "$thr"
157
+ echo
158
+ if [ "$inline" = 1 ]; then
159
+ echo '```diff'; cat "$out/range.diff"; echo '```'
160
+ else
161
+ # The snapshot presents the range as one commit over its base, so HEAD~1..HEAD is it.
162
+ echo "The diff is larger than $((REVIEW_DIFF_LIMIT / 1024)) KB, so it is not inlined. Read it in your working directory:"
163
+ echo " git diff HEAD~1 HEAD -- . ':(exclude).forge'"
164
+ fi
165
+ } > "$out/qa.input"
166
+
167
+ FORGE_DATASET_TRIGGER=human FORGE_CAPABILITY_CACHE="${FORGE_CAPABILITY_CACHE:-$PLAN/capabilities}" \
168
+ /bin/bash "$DISPATCH" qa "$spec" --repo "$review" --run-dir "$out" --review-base "$(cat "$out/review.base")" \
169
+ --prompt-file "$out/qa.input" ${yolo:+"$yolo"} $ft $tflag --output "${FORGE_OUTPUT:-summary}" > "$out/qa.out" 2>&1
170
+ rc=$?
171
+ case "$rc" in
172
+ 0) ;;
173
+ 8) note "adopt: the reviewer hit an infrastructure failure; nothing was judged"
174
+ review_infra "$out"; adopt_rollback; return 8 ;;
175
+ 2|3) note "adopt: the QA dispatch could not start (exit $rc); see $out/qa.out"
176
+ adopt_rollback; return 3 ;;
177
+ # A crash or a timeout concluded nothing, whatever the reply says, as in a final review.
178
+ *) note "adopt: the QA dispatch failed (exit $rc), so nothing was concluded and nothing was adopted; see $out/qa.out"
179
+ printf 'UNKNOWN\n' > "$out/verdict"
180
+ adopt_rollback
181
+ forge_dataset_event adopt --dir "$PLAN" --set base="$base" --set tip="$tip" --set verdict=UNKNOWN \
182
+ --set approved=0 --set adopted=0 --set spec="$spec" --set reason="$reason"
183
+ write_results "$PLAN"
184
+ return 5 ;;
185
+ esac
186
+ if [ "$(forge_fingerprint "$int_wt")" != "$source_fp" ] || [ "$(forge_fingerprint "$review" --tracked)" != "$review_fp" ]; then
187
+ note "adopt: a checkout changed during the review, so its verdict says nothing"
188
+ adopt_rollback; return 5
189
+ fi
190
+
191
+ verdict="$(forge_verdict "$out/qa.last" 2>/dev/null)"; verdict="${verdict:-UNKNOWN}"
192
+ gate_line="$(python3 "$SKILL_DIR/scripts/forge-contract.py" gate --verdict "$verdict" --qa-last "$out/qa.last" \
193
+ --threshold "$thr" --known-issues "$out/known_issues.md" 2>/dev/null)" || gate_line=""
194
+ decision="$(printf '%s' "$gate_line" | awk -F'\t' '{print $1}')"
195
+ [ -z "$gate_line" ] || printf '%s\t%s\n' "$thr" "$gate_line" > "$out/qa.gate"
196
+ case "$decision" in accept) verdict=PASS ;; fail) verdict=FAIL ;; esac
197
+ printf '%s\n' "$verdict" > "$out/verdict"
198
+
199
+ kept=0
200
+ if [ "$verdict" != PASS ]; then
201
+ echo
202
+ sed -n '1,200p' "$out/qa.last" 2>/dev/null
203
+ echo
204
+ if [ "$approved" = 1 ]; then
205
+ kept=1; echo "adopt: the reviewer said $verdict; --approved keeps it, and that is recorded"
206
+ # What `accept` writes: forge issues and the run's report read the findings it kept from here.
207
+ local body who
208
+ body="$(plans_reviewer_text "$out" "$verdict")"
209
+ who="${USER:-${LOGNAME:-unknown}}"
210
+ {
211
+ if [ -s "$out/known_issues.md" ]; then cat "$out/known_issues.md"; echo; fi
212
+ echo "## Accepted by a human despite the reviewer's $verdict verdict"
213
+ echo
214
+ echo "Reason: $reason"
215
+ echo "Adopted by $who (range $(printf '%.12s' "$base")..$(printf '%.12s' "$tip")). None of it was fixed."
216
+ echo
217
+ [ -z "$body" ] || printf '%s\n' "$body"
218
+ if [ "$(printf '%s\n' "$body" | grep -c '^- ')" = 0 ]; then
219
+ echo
220
+ echo "- [$verdict] the reviewer left no itemised findings; read $(basename "$out")/qa.last"
221
+ fi
222
+ } > "$out/.known_issues.new" && mv "$out/.known_issues.new" "$out/known_issues.md"
223
+ printf 'status=%s\nreason=%s\nby=%s\nat=%s\nepoch=%s\nreviewed_commit=%s\n' "$verdict" "$reason" "$who" \
224
+ "$(date '+%Y-%m-%dT%H:%M:%S%z')" "$(date +%s)" "$tip" > "$out/accepted.by"
225
+ else
226
+ echo "adopt: the reviewer said $verdict, so nothing was adopted (see $out/qa.last)."
227
+ adopt_rollback
228
+ forge_dataset_event adopt --dir "$PLAN" --set base="$base" --set tip="$tip" --set verdict="$verdict" \
229
+ --set approved=0 --set adopted=0 --set spec="$spec" --set reason="$reason"
230
+ write_results "$PLAN"
231
+ return 5
232
+ fi
233
+ fi
234
+
235
+ trap - INT TERM
236
+ printf '%s\n' "$tip" > "$PLAN/.accepted.integration.tmp" && mv "$PLAN/.accepted.integration.tmp" "$PLAN/accepted.integration" \
237
+ || { adopt_rollback; die "adopt: could not record the accepted revision" 1; }
238
+ printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$(date +%s)" "$base" "$tip" "$verdict" "$kept" "$spec" "$(basename "$out")" "$reason" \
239
+ >> "$PLAN/adoptions.tsv"
240
+ python3 - "$PLAN/control.log" "$base" "$tip" "$verdict" "$kept" "$spec" "$reason" "${USER:-${LOGNAME:-unknown}}" <<'PY' \
241
+ || note "adopt: could not add this to control.log"
242
+ import json, sys, time
243
+ log, base, tip, verdict, kept, spec, reason, who = sys.argv[1:]
244
+ with open(log, 'a') as handle:
245
+ handle.write(json.dumps(dict(at=time.time(), actor='cli', action='adopt', task=None, key='integration', value=tip,
246
+ previous=base, verdict=verdict, approved=kept == '1', spec=spec, reason=reason, by=who)) + '\n')
247
+ PY
248
+ forge_dataset_event adopt --dir "$PLAN" --set base="$base" --set tip="$tip" --set verdict="$verdict" \
249
+ --set approved="$kept" --set adopted=1 --set spec="$spec" --set reason="$reason" \
250
+ --set authors="$(git -C "$int_wt" log --format='%an' "$base..$tip" | sort -u | awk 'NR > 1 { printf "," } { printf "%s", $0 }')"
251
+ echo "adopted $(printf '%.12s' "$base")..$(printf '%.12s' "$tip") ($verdict) into the integration branch"
252
+
253
+ # As a retry does: the verification follows the merge, and never undoes it.
254
+ local left="" lid
255
+ for lid in $(all_ids "$T"); do
256
+ case "$(task_status "$PLAN" "$lid")" in PENDING|BLOCKED) left="${left:+$left }$lid" ;; esac
257
+ done
258
+ write_results "$PLAN"
259
+ if [ -n "$left" ]; then
260
+ note "verification deferred: tasks still to run ($left); the next run, or integrate, verifies the integration branch"
261
+ else
262
+ verify_result "$PLAN" "$int_wt" "$PLAN" || { write_results "$PLAN"; run_report_extras "$PLAN"; return 5; }
263
+ fi
264
+ write_results "$PLAN"
265
+ return 0
266
+ }
@@ -54,7 +54,9 @@ attribute_scratch_pick() { # attribute_scratch_pick <wt_root>
54
54
 
55
55
  attribute_scratch_remove() {
56
56
  [ "${ATTR_SCRATCH_OWNED:-0}" = 1 ] && [ -n "${ATTR_SCRATCH:-}" ] || return 0
57
- git -C "$ATTR_REPO" worktree remove --force "$ATTR_SCRATCH" >/dev/null 2>&1
57
+ if git -C "$ATTR_REPO" worktree remove --force "$ATTR_SCRATCH" >/dev/null 2>&1; then
58
+ forge_worktree_event removed "$ATTR_PLAN" "$ATTR_REPO" "$ATTR_SCRATCH" - attribute
59
+ fi
58
60
  rm -rf "$ATTR_SCRATCH"
59
61
  git -C "$ATTR_REPO" worktree prune >/dev/null 2>&1
60
62
  return 0
@@ -80,6 +82,7 @@ attribute_verify() { # attribute_verify <k>
80
82
  ATTR_REASON="could not create the scratch worktree for prefix $k: $(tail -1 "$dir/worktree.out")"
81
83
  return 2
82
84
  fi
85
+ forge_worktree_event created "$ATTR_PLAN" "$ATTR_REPO" "$ATTR_SCRATCH" - attribute
83
86
  note "attribute: verifying prefix $k of $ATTR_N (${commit:0:12})"
84
87
  populate_submodules "$ATTR_REPO" "$ATTR_SCRATCH" "$dir" "prefix $k"
85
88
  setup_cmd="$(worktree_setup_command "$ATTR_PLAN" "$ATTR_REPO")"
@@ -187,6 +190,7 @@ attribute_write_log() { # attribute_write_log <prefix k>
187
190
  local k="$1" commit id dir log tmp
188
191
  commit="$(attribute_commit "$k")"
189
192
  id="$(awk -F'\t' -v k="$k" '$1 == k { print $3; exit }' "$ATTR_DIR/merges.tsv")"
193
+ case "$id" in adopt-*) return 0 ;; esac # no task dir: the summary names the range
190
194
  dir="$ATTR_DIR/$k-${commit:0:12}"
191
195
  log="$dir/verification.log"
192
196
  [ -f "$log" ] || log="$ATTR_PLAN/verification.log" # the tip, known FAIL from the run itself
@@ -340,8 +344,12 @@ attribute_search() { # attribute_search <plan> <force 0|1>
340
344
  : > "$ATTR_DIR/steps.tsv"
341
345
 
342
346
  # The merges, oldest first, as forge-sweep.py merged_since reads the subjects.
347
+ # An adopted range is a step of its own, named for its tip: it can break verification as a merge can.
343
348
  git -C "$ATTR_REPO" log --first-parent --reverse --format='%H %s' "$ATTR_BASE..$ATTR_TIP" 2>/dev/null \
344
- | awk '{ if (match($0, /^[0-9a-f]+ forge: merge [^ ]+ *$/)) { n++; printf "%d\t%s\t%s\n", n, $1, $4 } }' > "$ATTR_DIR/merges.tsv"
349
+ | awk -v tips="$(awk -F'\t' '{ printf "%s ", $3 }' "$ATTR_PLAN/adoptions.tsv" 2>/dev/null)" '
350
+ BEGIN { split(tips, t, " "); for (i in t) adopted[t[i]] = 1 }
351
+ { if ($1 in adopted) { n++; printf "%d\t%s\tadopt-%s\n", n, $1, substr($1, 1, 12) }
352
+ else if (match($0, /^[0-9a-f]+ forge: merge [^ ]+ *$/)) { n++; printf "%d\t%s\t%s\n", n, $1, $4 } }' > "$ATTR_DIR/merges.tsv"
345
353
  ATTR_N="$(wc -l < "$ATTR_DIR/merges.tsv" | tr -d ' ')"
346
354
 
347
355
  # The run's own predicate. Lanes win: a lanes verification leaves a verification.command that
@@ -222,7 +222,7 @@ do_accept() {
222
222
  done
223
223
  [ -e "$int_wt/.git" ] || die "the integration worktree $int_wt is missing" 3
224
224
  [ -s "$PLAN/accepted.integration" ] && [ "$(git -C "$int_wt" rev-parse HEAD 2>/dev/null)" = "$(cat "$PLAN/accepted.integration")" ] \
225
- || die "integration branch changed outside this run; refusing to merge onto it" 3
225
+ || die "integration branch changed outside this run; refusing to merge onto it. To bring those commits into the run: forge-parallel.sh adopt $PLAN --head --reason \"...\"" 3
226
226
  [ -z "$(git -C "$int_wt" status --porcelain -- . ':(exclude).forge' 2>/dev/null)" ] \
227
227
  || die "the integration worktree $int_wt has uncommitted changes; refusing to merge onto it" 3
228
228
 
@@ -234,8 +234,10 @@ do_accept() {
234
234
  sha="$(cat "$tdir/reviewed.commit")"
235
235
  body="$(plans_reviewer_text "$tdir" "$st")"
236
236
  n="$(printf '%s\n' "$body" | grep -c '^- ')"
237
- local gthr gdec
238
- IFS="$(printf '\t')" read -r gthr gdec _ < "$tdir/qa.gate" 2>/dev/null || true
237
+ local gthr='' gdec=''
238
+ if [ -f "$tdir/qa.gate" ]; then
239
+ IFS="$(printf '\t')" read -r gthr gdec _ < "$tdir/qa.gate" || true
240
+ fi
239
241
  rm -f "$kibak"; [ -f "$ki" ] && cp -p "$ki" "$kibak"
240
242
  {
241
243
  if [ -s "$ki" ]; then cat "$ki"; echo; fi
@@ -516,6 +518,7 @@ Retry or accept them (or split them off into their own plan) first." 3
516
518
  printf '%s\n' "$NEWWTR" > "$NEW/wt_root" || ok=0
517
519
  git -C "$NEWINT" rev-parse HEAD > "$NEW/accepted.integration" || ok=0
518
520
  [ -z "$newbase" ] || printf '%s\n' "$newbase" > "$NEW/integration.base"
521
+ for p in "${SRC[@]}"; do [ ! -s "$p/adoptions.tsv" ] || cat "$p/adoptions.tsv" >> "$NEW/adoptions.tsv" || ok=0; done
519
522
  for p in "${SRC[@]}"; do
520
523
  for id in $(all_ids "$p/tasks.tsv"); do
521
524
  plans_copy_taskdir "$p/tasks/$id" "$NEW/tasks/$id" || ok=0
@@ -605,6 +608,7 @@ sys.exit(0 if key(sys.argv[1]) == key(sys.argv[2]) else 1)' "$p/profile.json" "$
605
608
  # worktree gets: whatever setup the repository asks for. Never fatal.
606
609
  mkdir -p "$NEW/tasks/_integration"
607
610
  run_worktree_setup "$NEW" "$repo_text" "$NEWINT" "$NEW/tasks/_integration" _integration
611
+ forge_worktree_event created "$NEW" "$repo_text" "$NEWINT" "$NEWBR" integration
608
612
 
609
613
  echo "combined $n plans into $NEW (run forge/$NEWRID): $ntasks tasks, all MERGED"
610
614
  echo "integration branch: $NEWBR at $(plans_short "$(cat "$NEW/accepted.integration")")"
@@ -738,7 +742,7 @@ except Exception:
738
742
  plans_make_integration "$REPO" "$NEWBR" "$acc" "$NEWINT" \
739
743
  || { plans_undo; trap - INT TERM; die "could not create $NEWBR and its worktree" 3; }
740
744
 
741
- local oldbr newbr oldwt newwt head_ref old_branches=""
745
+ local oldbr newbr oldwt newwt head_ref old_branches="" moved_wts=""
742
746
  for id in $(all_ids "$T"); do
743
747
  case "$SEL" in *" $id "*) ;; *) continue ;; esac
744
748
  oldbr="forge/$run_id/$id"; newbr="forge/$NEWRID/$id"
@@ -753,6 +757,7 @@ except Exception:
753
757
  git -C "$REPO" worktree move "$oldwt" "$newwt" >/dev/null 2>&1 \
754
758
  || { plans_undo; trap - INT TERM; die "could not move worktree $oldwt to $newwt" 3; }
755
759
  plans_journal move "$oldwt" "$newwt"
760
+ moved_wts="$moved_wts $id"
756
761
  # Same commit, same index, same working tree: only the name HEAD points at changes.
757
762
  if [ "$head_ref" = "refs/heads/$oldbr" ]; then
758
763
  git -C "$newwt" symbolic-ref HEAD "refs/heads/$newbr" >/dev/null 2>&1 \
@@ -784,6 +789,7 @@ except Exception:
784
789
  printf '%s\n' "$NEWRID" > "$NEW/run_id" || ok=0
785
790
  printf '%s\n' "$NEWWTR" > "$NEW/wt_root" || ok=0
786
791
  printf '%s\n' "$acc" > "$NEW/accepted.integration" || ok=0
792
+ [ ! -s "$PLAN/adoptions.tsv" ] || cp -p "$PLAN/adoptions.tsv" "$NEW/adoptions.tsv" || ok=0
787
793
  [ ! -s "$PLAN/integration.base" ] || cp -p "$PLAN/integration.base" "$NEW/integration.base" || ok=0
788
794
  [ ! -s "$PLAN/goal.txt" ] || cp -p "$PLAN/goal.txt" "$NEW/goal.txt" || ok=0
789
795
  for f in $PLANS_OPTION_FILES; do
@@ -824,6 +830,12 @@ except Exception:
824
830
  done
825
831
  mkdir -p "$NEW/tasks/_integration"
826
832
  run_worktree_setup "$NEW" "$repo_text" "$NEWINT" "$NEW/tasks/_integration" _integration
833
+ forge_worktree_event created "$NEW" "$repo_text" "$NEWINT" "$NEWBR" integration
834
+ # A moved worktree is one forge removed from the old run and one it created in the new run.
835
+ for id in $moved_wts; do
836
+ forge_worktree_event removed "$PLAN" "$repo_text" "$old_wtr/$id" "forge/$run_id/$id" task "$id"
837
+ forge_worktree_event created "$NEW" "$repo_text" "$NEWWTR/$id" "forge/$NEWRID/$id" task "$id"
838
+ done
827
839
 
828
840
  echo "split: moved $PICKED from $PLAN to $NEW (run forge/$NEWRID)"
829
841
  echo "integration branch: $NEWBR at $(plans_short "$acc")"
@@ -395,6 +395,7 @@ final_review() {
395
395
  while [ -e "$review" ]; do n=$((n+1)); review="$out/review-$$-$n"; done
396
396
  forge_review_snapshot "$src" "$base" "$tip" "$review" > "$out/snapshot.log" 2>&1 ||
397
397
  { note "final review: cannot build the review snapshot (see $out/snapshot.log)"; return 3; }
398
+ forge_review_deps "$src" "$review" "$out/review.deps"
398
399
  source_fp="$(forge_fingerprint "$src")" && review_fp="$(forge_fingerprint "$review" --tracked)" ||
399
400
  { note "final review: cannot fingerprint the checkouts"; return 3; }
400
401
  printf '%s\n' "$source_fp" > "$out/source.fingerprint"
@@ -466,6 +467,8 @@ final_review() {
466
467
  cat "$out/context.md"
467
468
  python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part head --kind final
468
469
  echo
470
+ cat "$out/review.deps"; echo
471
+ adopt_rows "$plan"
469
472
  cat "$out/notes.md"
470
473
  [ -s "$out/previous.md" ] && cat "$out/previous.md"
471
474
  python3 "$SKILL_DIR/scripts/forge-contract.py" qa --part tail --kind final --threshold "$thr"
@@ -550,6 +553,9 @@ final_review() {
550
553
  verdict=""
551
554
  [ -s "$out/qa.last" ] && verdict="$(forge_verdict "$out/qa.last")"
552
555
  verdict="${verdict:-UNKNOWN}"
556
+ local tests_note
557
+ tests_note="$(forge_tests_note "$out/qa.last")"
558
+ [ -z "$tests_note" ] || note "final review: the reviewer's tests could not run: $tests_note"
553
559
  # Same gate as per-task QA, both ways: a FAIL made only of labelled findings below the
554
560
  # threshold is accepted with known issues; a PASS listing a blocking CONFIRMED finding fails.
555
561
  gate_line="$(python3 "$SKILL_DIR/scripts/forge-contract.py" gate --verdict "$verdict" \
@@ -634,11 +640,11 @@ do_review() {
634
640
  tip="$(git -C "$REPO" rev-parse --verify --quiet "$int_br^{commit}")" ||
635
641
  die "integration branch $int_br does not exist; run the plan first" 3
636
642
  if [ ! -s "$PLAN/accepted.integration" ] || [ "$tip" != "$(cat "$PLAN/accepted.integration")" ]; then
637
- die "$int_br is not at the revision this run accepted (it moved outside forge); refusing to review something forge did not merge" 3
643
+ die "$int_br is not at the revision this run accepted (it moved outside forge); refusing to review something forge did not merge. To bring those commits into the run: forge-parallel.sh adopt $PLAN --head --reason \"...\"" 3
638
644
  fi
639
645
  [ -e "$int_wt/.git" ] || die "the integration worktree $int_wt is missing; nothing to take a trustworthy source fingerprint from" 3
640
646
  for id in $(all_ids "$T"); do [ "$(task_status "$PLAN" "$id")" = MERGED ] && merged=$((merged+1)); done
641
- [ "$merged" -gt 0 ] || die "no task has been merged, so there is no combined result to review" 3
647
+ [ "$merged" -gt 0 ] || [ -s "$PLAN/adoptions.tsv" ] || die "no task has been merged, so there is no combined result to review" 3
642
648
 
643
649
  if [ -s "$PLAN/integration.base" ]; then
644
650
  base="$(cat "$PLAN/integration.base")"
@@ -334,8 +334,10 @@ carry_findings() { # carry_findings <tdir>
334
334
  rm -f "$tdir/retry_findings.session" "$tdir/retry_findings.resolved"
335
335
  carry_implementer_session "$tdir"
336
336
  printf 'review\n' > "$tdir/retry_findings.source"
337
- local gthr gdec
338
- IFS="$(printf '\t')" read -r gthr gdec _ < "$tdir/qa.gate" 2>/dev/null || true
337
+ local gthr='' gdec=''
338
+ if [ -f "$tdir/qa.gate" ]; then
339
+ IFS="$(printf '\t')" read -r gthr gdec _ < "$tdir/qa.gate" || true
340
+ fi
339
341
  if [ "${gdec:-}" = fail ]; then
340
342
  # forge, not the reviewer, failed this attempt: say so above the copied report.
341
343
  { printf "forge: the reviewer's verdict was PASS, but this report lists CONFIRMED findings at or above the \`%s\` blocking threshold, so forge failed the attempt. Fix those findings.\n\n" "$gthr"
@@ -602,6 +604,7 @@ stop_for_decision() { # stop_for_decision <plan> <id> <reason>...
602
604
  echo " lower the threshold: forge-parallel.sh retry $PLAN $id --decide threshold=P1 --reason \"...\" --approved"
603
605
  echo " (this task's blocking threshold, then one retry)"
604
606
  echo " a named model: forge-parallel.sh retry $PLAN $id --dwarf <spec> --reason \"...\" --approved"
607
+ echo " (one attempt; with --decide retry=N, up to N attempts held on that model)"
605
608
  case "$from" in FAIL|UNKNOWN) # with no reviewer verdict, accept has nothing to override
606
609
  echo " accept it as it is: forge-parallel.sh accept $PLAN $id --reason \"...\" --approved"
607
610
  echo " (merges it; its findings become known issues)" ;;
@@ -646,7 +649,6 @@ retry_decision_gate() { # retry_decision_gate <plan> <id> <status> <decide> <dwa
646
649
  $(sed -n '1,3p' "$PLAN/tasks/$id/decision.md" 2>/dev/null)
647
650
  read $PLAN/tasks/$id/decision.md, then retry with --decide retry=N or --decide threshold=Pn, or --dwarf <spec>, each with --reason \"...\" --approved; or accept the task"
648
651
  fi
649
- [ -z "$decide" ] || [ -z "$dwarf" ] || die "retry: give --decide or --dwarf, not both: a decision is one choice"
650
652
  case "$decide" in
651
653
  "") ;;
652
654
  retry=*) n="${decide#retry=}"
@@ -666,7 +668,7 @@ read $PLAN/tasks/$id/decision.md, then retry with --decide retry=N or --decide t
666
668
  retry_decided() { # retry_decided <plan> <id> <decide> <new-dwarf> <reason>
667
669
  local PLAN="$1" id="$2" decide="$3" newdwarf="$4" reason="$5" tdir="$1/tasks/$2" choice f rc=0 attempt
668
670
  local undo="$1/tasks/$2/.decision.undo"
669
- choice="$decide"; [ -n "$choice" ] || choice="dwarf=$newdwarf"
671
+ choice="$decide"; [ -z "$newdwarf" ] || choice="${choice:+$choice }dwarf=$newdwarf"
670
672
  reason="$(printf '%s' "$reason" | tr '\n\r\t' ' ' | sed 's/^ *//; s/ *$//')"
671
673
  attempt="$(cat "$tdir/attempt" 2>/dev/null || echo 0)"
672
674
  rm -rf "${undo:?}"; mkdir "$undo" 2>/dev/null || { note "$id: could not record the decision; nothing was retried"; return 1; }
@@ -708,7 +710,9 @@ decided_attempts() { # decided_attempts <decide>
708
710
  # run that uses it may end first (a quota stop) and a later `run` or `retry` must still honour it.
709
711
  # Absent means no decision governs and the retry budget rules; 0 means the decided attempts are used.
710
712
  decided_left() { # decided_left <task-dir>: the number, or nothing
711
- local v; v="$(tr -dc '0-9' < "$1/decided.left" 2>/dev/null | head -c 3)"
713
+ local v
714
+ [ -f "$1/decided.left" ] || return 0
715
+ v="$(tr -dc '0-9' < "$1/decided.left" | head -c 3)"
712
716
  [ -n "$v" ] && echo $(( 10#$v ))
713
717
  return 0
714
718
  }
@@ -738,6 +742,8 @@ report_decisions() { # report_decisions <plan>
738
742
  echo " forge-parallel.sh retry $plan <task-id> --decide retry=N | --decide threshold=Pn | --dwarf <spec>"
739
743
  [ "$acceptable" = 0 ] || echo " forge-parallel.sh accept $plan <task-id> (merge it with its findings as known issues)"
740
744
  echo " or split it: replace its row in $plan/tasks.tsv with smaller tasks, then forge-parallel.sh run $plan"
745
+ echo "several side by side: add --no-dispatch to each retry, which records the decision and starts nothing, then:"
746
+ echo " forge-parallel.sh run $plan"
741
747
  echo
742
748
  }
743
749
 
@@ -1192,6 +1198,14 @@ report_known_issues() { # report_known_issues <plan>
1192
1198
  n=$((n+1))
1193
1199
  printf ' %-14s %s finding(s) %s\n' "$id" "$(grep -c '^- ' "$plan/tasks/$id/known_issues.md" 2>/dev/null || echo 0)" "$plan/tasks/$id/known_issues.md"
1194
1200
  done
1201
+ # What an adopted range kept: findings below the threshold, or a verdict --approved overrode.
1202
+ local dir
1203
+ for dir in $(awk -F'\t' '{ print $7 }' "$plan/adoptions.tsv" 2>/dev/null); do
1204
+ [ -s "$plan/$dir/known_issues.md" ] || continue
1205
+ [ "$n" = 0 ] && echo "known issues accepted (findings below the QA threshold, or accepted by a person):"
1206
+ n=$((n+1))
1207
+ printf ' %-14s %s finding(s) %s\n' adopted "$(grep -c '^- ' "$plan/$dir/known_issues.md" 2>/dev/null)" "$plan/$dir/known_issues.md"
1208
+ done
1195
1209
  [ "$n" -gt 0 ] && echo
1196
1210
  return 0
1197
1211
  }
@@ -25,6 +25,7 @@
25
25
  # forge-memory.sh show <repo>
26
26
  # forge-memory.sh prune <repo>
27
27
  # forge-memory.sh spend <repo> [--run <run-id>]
28
+ # forge-memory.sh learnings <repo> [--json] every ledger learning and its state, as JSON
28
29
  #
29
30
  # Exit codes: 0 ok | 2 usage error
30
31
  set -uo pipefail
@@ -119,12 +120,14 @@ section_title() {
119
120
  # memory.md is regenerated from the whole ledger rather than mutated in place.
120
121
  # Rebuilding is idempotent and order-independent, so a crashed or concurrent run
121
122
  # cannot leave memory half-updated — it can only leave the ledger short a row.
122
- rebuild() { # rebuild <repo> -> prints "kept<TAB>pruned_stale<TAB>pruned_cap"
123
+ # With <out>, the memory is written there instead of the repo's .forge/memory.md, and the repo is
124
+ # only read: `learnings` uses that to say what a rebuild keeps without changing anything.
125
+ rebuild() { # rebuild <repo> [<out>] -> prints "kept<TAB>pruned_stale<TAB>pruned_cap"
123
126
  local repo="$1" led mem tmp cand jev_loaded=0
124
127
  if [ "${FORGE_MEMORY_DEEP_PRUNE:-0}" = 1 ] && [ -f "$SKILL_DIR/scripts/forge-jev-options.sh" ]; then
125
128
  source "$SKILL_DIR/scripts/forge-jev-options.sh" 2>/dev/null && jev_loaded=1
126
129
  fi
127
- led="$(ledger_for "$repo")"; mem="$(memory_for "$repo")"
130
+ led="$(ledger_for "$repo")"; mem="${2:-$(memory_for "$repo")}"
128
131
  [ -f "$led" ] || return 0
129
132
  tmp="$mem.tmp.$$"; cand="$mem.cand.$$"
130
133
 
@@ -222,7 +225,7 @@ rebuild() { # rebuild <repo> -> prints "kept<TAB>pruned_stale<TAB>pruned_cap"
222
225
  # The cap can eat a section's last fact, leaving a heading over nothing.
223
226
  drop_empty_sections "$header"
224
227
 
225
- mkdir -p "$(dir_for "$repo")"
228
+ [ -n "${2:-}" ] || mkdir -p "$(dir_for "$repo")"
226
229
  mv "$header" "$mem"
227
230
  rm -f "$tmp"
228
231
  printf '%s\t%s\t%s\n' "$kept" "$stale" "$capped"
@@ -495,6 +498,26 @@ do_prune() { # do_prune <repo>
495
498
  rm -f ${MEMORY_DROPS:+"$MEMORY_DROPS"}
496
499
  }
497
500
 
501
+ # --- learnings ----------------------------------------------------------------
502
+ # Every learning in the ledger and what forge does with it, as JSON (forge-ui's Projects screen,
503
+ # #126). The states come from this file's own rebuild, run into a scratch dir: the same grouping,
504
+ # promotion threshold, staleness check and cap that `record` applies, so the listing cannot drift
505
+ # from what memory.md gets. Read-only for the repo. No Jev: deep pruning happens only on `prune`.
506
+ do_learnings() { # do_learnings <repo>
507
+ local repo="${1:?learnings needs a repo}" scratch rc=0
508
+ [ -d "$repo" ] || die "learnings: '$repo' is not a directory"
509
+ scratch="$(mktemp -d "${TMPDIR:-/tmp}/forge-memory-learnings.XXXXXX")" || die "learnings: no scratch dir" 3
510
+ : > "$scratch/drops"
511
+ if [ -f "$(ledger_for "$repo")" ]; then
512
+ MEMORY_DROPS="$scratch/drops" FORGE_MEMORY_DEEP_PRUNE=0 rebuild "$repo" "$scratch/memory.md" > "$scratch/counts"
513
+ fi
514
+ python3 "$SKILL_DIR/scripts/forge_memory_list.py" --repo "$repo" --ledger "$(ledger_for "$repo")" \
515
+ --memory "$(memory_for "$repo")" --rebuilt "$scratch/memory.md" --drops "$scratch/drops" \
516
+ --max-lines "$MAX_LINES" --max-bytes "$MAX_BYTES" --memory-off "$(memory_off && echo 1 || echo 0)" || rc=$?
517
+ rm -rf "$scratch"
518
+ return "$rc"
519
+ }
520
+
498
521
  # --- spend --------------------------------------------------------------------
499
522
  # The ledger already carries one row per dispatch, and it is never injected into a
500
523
  # prompt, so accounting from it costs nothing at all. Time and dispatch counts
@@ -561,14 +584,28 @@ do_spend() {
561
584
  }
562
585
 
563
586
  # --- main ---------------------------------------------------------------------
564
- [ $# -ge 1 ] || die "usage: forge-memory.sh <inject|note|record|show|prune|spend> [repo] [...]"
587
+ memory_usage() {
588
+ cat <<'USAGE'
589
+ usage: forge memory <command> <repo> [...] (scripts/forge-memory.sh)
590
+
591
+ show <repo> the repository's .forge/memory.md
592
+ learnings <repo> [--json] every ledger learning and what forge does with it (in_memory, waiting,
593
+ stale, capped, muted), from forge's own rebuild; changes nothing
594
+ prune <repo> re-apply staleness and the cap now, then print what a dwarf is told
595
+ spend <repo> [--run ID] what forge has cost this repository, from the ledger
596
+ inject <repo> <role> the block forge prepends to a planner, dwarf or qa prompt
597
+ USAGE
598
+ }
599
+ [ $# -ge 1 ] || die "usage: forge-memory.sh <inject|note|record|show|prune|spend|learnings> [repo] [...]"
565
600
  CMD="$1"; shift
566
601
  case "$CMD" in
602
+ -h|--help|help) memory_usage; exit 0 ;;
567
603
  inject) do_inject "$@" ;;
568
604
  note) do_note "$@" ;;
569
605
  record) do_record "$@" ;;
570
606
  show) mem="$(memory_for "${1:?show needs a repo}")"; [ -s "$mem" ] && cat "$mem" ;;
571
607
  spend) do_spend "$@" ;;
608
+ learnings) [ "${2:-}" = --json ] || [ $# -eq 1 ] || die "learnings: usage: learnings <repo> [--json]"; do_learnings "$1" ;;
572
609
  prune) do_prune "${1:?prune needs a repo}" >/dev/null
573
610
  do_inject "${1}" dwarf ;;
574
611
  *) die "unknown subcommand '$CMD'" ;;
@@ -23,6 +23,35 @@ forge_dataset_event() { # forge_dataset_event <name> --dir <dir> [--task id] [--
23
23
  forge_dataset_off && return 0
24
24
  python3 "$SKILL_DIR/scripts/forge-dataset.py" event "$@" >/dev/null 2>&1 || true
25
25
  }
26
+ # A worktree forge added or removed (#125): forge-ui's Worktrees screen lists every one, removed ones too.
27
+ # <kind> is task, integration, candidate or attribute (scripts/forge_worktrees.py); <branch> is - when detached.
28
+ # Off with the dataset (FORGE_DATASET=off), as forge_dataset_event is.
29
+ forge_worktree_event() { # forge_worktree_event created|removed <run dir> <repo> <path> <branch|-> <kind> [task]
30
+ forge_dataset_event "worktree_$1" --dir "$2" ${7:+--task "$7"} --set repo_path="$3" --set path="$4" \
31
+ --set branch="$5" --set kind="$6"
32
+ }
33
+ # The outcomes that came due for a run's repository (#120): what older integrations' lines became,
34
+ # read from git alone. At the end of `run` and `integrate`; bounded (FORGE_OUTCOMES_TIMEOUT seconds,
35
+ # default 20, plus a kill 10 s later), never changes the caller's exit code, logs its errors to the
36
+ # dataset's errors.log, and is off with FORGE_OUTCOMES=off or with the dataset.
37
+ forge_outcomes_harvest() { # forge_outcomes_harvest <plan dir>
38
+ forge_dataset_off && return 0
39
+ case "${FORGE_OUTCOMES:-on}" in off|OFF|Off|0|no|false) return 0 ;; esac
40
+ local repo limit pid watcher
41
+ repo="$(cat "$1/repo" 2>/dev/null)"
42
+ [ -n "$repo" ] && [ -d "$repo" ] || return 0
43
+ limit="${FORGE_OUTCOMES_TIMEOUT:-20}"
44
+ case "$limit" in ''|*[!0-9]*) limit=20 ;; esac
45
+ [ "$limit" -gt 0 ] || return 0
46
+ python3 "$SKILL_DIR/scripts/forge-dataset.py" outcomes --repo "$repo" --deadline "$limit" --quiet \
47
+ </dev/null >/dev/null 2>&1 &
48
+ pid=$!
49
+ ( sleep $(( limit + 10 )); kill -KILL "$pid" 2>/dev/null ) >/dev/null 2>&1 &
50
+ watcher=$!
51
+ wait "$pid" 2>/dev/null
52
+ pkill -P "$watcher" 2>/dev/null; kill "$watcher" 2>/dev/null; wait "$watcher" 2>/dev/null
53
+ return 0
54
+ }
26
55
  forge_dataset_invocation() { # forge_dataset_invocation <command> <rc> <started> [argv...]
27
56
  forge_dataset_off && return 0
28
57
  local command="$1" rc="$2" started="$3"; shift 3