@plot-pm/board 0.16.1 → 0.16.3

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.
package/plot-deliver.sh CHANGED
@@ -1,735 +1,13 @@
1
1
  #!/usr/bin/env bash
2
- # Plot helper: perform the MECHANICAL half of delivering a plan.
3
- # Usage: plot-deliver.sh [--dry-run] [--who <name>] <slug>
4
- # --dry-run say what would happen; write nothing, push nothing
5
- # --who the name recorded in the `Delivered:` line (default: git user.name)
6
- # <slug> the plan to deliver
7
- # Output: one `step:` line per step, then a machine-countable summary:
8
- # summary: phase=flipped record=written index=moved sprint=updated push=clean tracker=none
9
- # `tracker=` is plot-issue-status.sh's outcome (none|written|no-target|
10
- # unaskable|failed), or `skipped` when the push was rejected. It never
11
- # changes the exit code: a failed status write is reported, not raised.
12
- # Exit 0 when the plan is Delivered on the default branch (whether this
13
- # run did the work or found it already done); 1 on a refusal or a
14
- # failure, with the reason on stderr.
15
- #
16
- # WHY THIS EXISTS. The board computes `allSlicesMerged` — exactly the condition
17
- # that says a plan is ready to deliver — but the transition itself lives only in
18
- # `/plot-deliver`'s prose. So `Delivered` in the board asked for a caller with
19
- # nothing safe to call, and an implementer reaching that point would have
20
- # rebuilt the phase flip, the `Delivered:` record and the symlink move in
21
- # TypeScript. That is precisely the drift the `plot-approve.sh` split removed.
22
- #
23
- # This script is the `plot-approve.sh` of delivery: one implementation, two
24
- # entrances. `/plot-deliver` keeps the judgement — the completeness check, the
25
- # partial-deliverable question — and delegates the writes here. The board calls
26
- # this script directly (or via an agent when a `Deliver command` is set).
27
- #
28
- # Manifesto Principle 3 draws the line: SCRIPTS COLLECT AND REPORT; SKILLS
29
- # INTERPRET AND ADAPT. Flipping a phase and writing a dated record is
30
- # collecting. Deciding whether PARTIAL work counts as delivered is interpreting,
31
- # and stays in the skill.
32
- #
33
- # IT IS IDEMPOTENT, BECAUSE ONE STEP CANNOT BE UNDONE. The push is irreversible;
34
- # everything before it is local. A run interrupted between flipping the phase
35
- # and pushing leaves the plan at `Delivered` in a worktree but still `Approved`
36
- # on main — so `plot-deliver.sh <slug>` may run any number of times, and RUN IT
37
- # AGAIN is the repair for every interruption.
38
- #
39
- # Each step asks THE SOURCE IT WOULD HAVE WRITTEN whether it is already done:
40
- # the plan file for the phase and the record, the index directories for the
41
- # symlink, the sprint file for the tick. Never a progress file of its own.
42
- #
43
- # WHAT IT REFUSES, and why refusing beats guessing:
44
- # - phase is not `approved` — nothing to deliver. (Already-Delivered is NOT a
45
- # refusal: it is the idempotent case, and the run still checks the record
46
- # and index.)
47
- # - any non-deferred branch is unmerged — work is not done. This is one of
48
- # Plot's four phase guardrails, moved from prose into an exit code.
49
- #
50
- # macOS bash 3.2 throughout: no associative arrays, no bash-4 line readers.
2
+ # Launcher. The mechanical half of delivering (or releasing) a plan is
3
+ # `board/plot-deliver.mjs`, a JS entry point over the domain — see its header
4
+ # for what it does. This resolves it, passes arguments and the exit code
5
+ # through, and decides nothing.
51
6
  set -uo pipefail
52
-
53
- script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
54
- . "$script_dir/plot-tmp.sh"
55
-
56
- # The receipt this script leaves for plot-state-gate.sh, which refuses every
57
- # other writer of a `State:` line. Sourced rather than run: the gate and the
58
- # three owning scripts must agree on where a receipt lives, and one file is how.
59
- # shellcheck source=plot-state-receipt.sh
60
- . "$script_dir/plot-state-receipt.sh"
61
-
62
- dry_run=0
63
- who_override=""
64
- slug=""
65
- while [ $# -gt 0 ]; do
66
- case "$1" in
67
- --dry-run) dry_run=1 ;;
68
- --who) who_override="${2:?--who needs a value}"; shift ;;
69
- -h|--help) sed -n '2,12p' "$0"; exit 0 ;;
70
- -*) echo "plot-deliver: unknown flag '$1'" >&2; exit 1 ;;
71
- *) slug="$1" ;;
72
- esac
73
- shift
74
- done
75
-
76
- die() { echo "plot-deliver: $*" >&2; exit 1; }
77
-
78
- [ -n "$slug" ] || die "need a plan slug (usage: plot-deliver.sh [--dry-run] <slug>)"
79
- git rev-parse --git-dir >/dev/null 2>&1 || die "not a git repository — run this from inside the checkout, or 'git init' one here"
80
-
81
- cfg() { bash "$script_dir/plot-config.sh" get "$1" "$2"; }
82
-
83
- repo_root=$(git rev-parse --show-toplevel)
84
- wt_root=$(cd "$repo_root/.." && pwd)
85
-
86
- PLAN_DIR=$(cfg "Plan directory" "docs/plans/")
87
- ACTIVE_DIR=$(cfg "Active index" "docs/plans/active/")
88
- DELIVERED_DIR=$(cfg "Delivered index" "docs/plans/delivered/")
89
- SPRINT_DIR=$(cfg "Sprint directory" "docs/sprints/")
90
-
91
- # ---------------------------------------------------------------------------
92
- # Step 1 — find the plan and read its state
93
- # ---------------------------------------------------------------------------
94
-
95
- plan_file=""
96
- for cand in "$PLAN_DIR"*"$slug".md "$ACTIVE_DIR$slug.md" "$DELIVERED_DIR$slug.md"; do
97
- [ -e "$cand" ] && { plan_file="$cand"; break; }
98
- done
99
- [ -n "$plan_file" ] || die "no plan found for '$slug' — looked in $PLAN_DIR, $ACTIVE_DIR, $DELIVERED_DIR.
100
- Check the slug: ls $PLAN_DIR | grep -i '$slug'"
101
-
102
- meta=$(bash "$script_dir/plot-plan-meta.sh" "$plan_file" 2>/dev/null) || meta=""
103
- [ -n "$meta" ] || die "cannot parse '$plan_file' — refusing rather than guessing.
104
- See what the parser reads: $script_dir/plot-plan-meta.sh $plan_file
105
- A plan needs a '## Status' section with a 'State:' field."
106
-
107
- jfield() { printf '%s' "$meta" | jq -r "$1" 2>/dev/null; }
108
-
109
- phase=$(jfield '.phase')
110
- sprint=$(jfield '.sprint')
111
- delivered_raw=$(jfield '.delivered_raw')
112
-
113
- # --- refusal 1: the phase ---------------------------------------------------
114
- #
115
- # `approved` and `delivered` both proceed: Delivered is the idempotent case.
116
- # A run that finds the phase already flipped still has a record to check, an
117
- # index link to move, and an annotation to update — the very half-states this
118
- # script exists to repair.
119
- case "$phase" in
120
- approved|delivered) ;;
121
- released)
122
- die "plan '$slug' is already released — nothing to deliver.
123
- Nothing to do here; the work shipped." ;;
124
- draft|design)
125
- die "plan '$slug' is still '$phase' — approve it first: /plot-approve $slug" ;;
126
- NONE|"")
127
- die "cannot read the phase of '$slug' ($plan_file) — refusing rather than guessing.
128
- Its '## Status' section needs a line reading '- **State:** Approved'." ;;
129
- *)
130
- die "plan '$slug' is in phase '$phase' — only an Approved plan can be delivered.
131
- If that phase is wrong, correct the 'State:' line in $plan_file and push it." ;;
132
- esac
133
-
134
- # ---------------------------------------------------------------------------
135
- # Step 2 — ask whether every non-deferred branch has merged
136
- # ---------------------------------------------------------------------------
137
- #
138
- # This is one of Plot's four phase guardrails, and it is NO LONGER DECIDED HERE.
139
- #
140
- # THE SCRIPT USED TO PARSE THE PLAN ITSELF. Ninety-odd lines of `sed` and `grep`
141
- # re-derived the branches section, re-derived which lines named a branch, and
142
- # re-derived which of those were deferred — a second implementation of a job
143
- # `plot-plan-meta.sh` already owns, with its own bug history. The measured ones:
144
- # a `## Changelog` bullet read as a branch (four fully-merged plans undeliverable,
145
- # 2026-08-27), and a `## Slices` section that parsed to ZERO branches so the gate
146
- # passed having checked nothing (two approved plans, 2026-08-30).
147
- #
148
- # A THIRD was measured while this block was being removed, and it is the one
149
- # that argues hardest for the removal. On
150
- # `docs/plans/2026-08-21-waves-name-themselves.md` the old range matched a
151
- # `## Waves` heading that opens DESIGN PROSE — the plan argues for that spelling
152
- # and illustrates it — and closed at the next `## `, never reaching the real
153
- # `## Branches` section a hundred lines below. It read three branch names out of
154
- # the illustration, one of them `bug/one` from inside a code fence, and reported
155
- # none of the plan's actual work. `plot-plan-meta.sh` reads the same file
156
- # correctly. That plan was delivered by hand.
157
- #
158
- # So the reading stays in the scripts and the DECISION moves inside:
159
- # `plot-plan-meta.sh` says which branches the plan names and which it gave up,
160
- # `plot-impl-status.sh` says which the host merged, and the domain's `deliver`
161
- # rule says whether that makes the plan deliverable. Manifesto Principle 3 —
162
- # scripts collect and report — with the line drawn where the design plan draws
163
- # it: parsing a plan is adaptation, "these branches make the plan deliverable"
164
- # is a decision.
165
- #
166
- # THE REFUSAL SENTENCE COMES BACK FROM THE DOMAIN, unchanged in wording, so the
167
- # two cannot describe one rule differently.
168
-
169
- ask_mjs="$script_dir/board/plot-ask.mjs"
170
- if [ ! -f "$ask_mjs" ]; then
171
- die "cannot find $ask_mjs — run 'pnpm build:board' to build it."
172
- fi
173
-
174
- verdict=$(PLOT_REPO_ROOT="$repo_root" PLOT_SCRIPTS_DIR="$script_dir" \
175
- node "$ask_mjs" deliverable "$slug" "$plan_file" 2>/dev/null) || verdict=""
176
- [ -n "$verdict" ] || die "cannot determine deliverability of '$slug' — refusing rather than guessing.
177
- See what it said: PLOT_REPO_ROOT=$repo_root PLOT_SCRIPTS_DIR=$script_dir node $ask_mjs deliverable $slug $plan_file"
178
-
179
- vfield() { printf '%s' "$verdict" | jq -r "$1" 2>/dev/null; }
180
-
181
- if [ "$(vfield '.deliverable')" != "true" ]; then
182
- die "$(vfield '.refusal')"
183
- fi
184
-
185
- # THE FINDING — a slice whose merged PR carried no implementation.
186
- #
187
- # IT REPORTS AND DOES NOT REFUSE, which is the harder call and the right one.
188
- # Measured over the last 60 merged PRs on 2026-09-08, seven carried no work and
189
- # only TWO were this defect: three were claim PRs whose slice finished under a
190
- # DIFFERENT PR, and a gate refusing on those would have blocked a delivery whose
191
- # work was complete — right about the PR and wrong about the plan.
192
- #
193
- # So it is worded as a QUESTION and names the next move, because a reader told
194
- # only "this carried nothing" still has the decision to make.
195
- empty_slices=$(printf '%s' "$verdict" | jq -r '.emptySlices // [] | .[]' 2>/dev/null)
196
- if [ -n "$empty_slices" ]; then
197
- echo "note: this plan's merged PR carried no implementation on:"
198
- while IFS= read -r b; do
199
- [ -n "$b" ] || continue
200
- echo " - $b"
201
- done <<< "$empty_slices"
202
- echo " check whether its work landed under another PR, mark it deferred"
203
- echo " (<!-- deferred: <reason> -->), or re-open it. Delivery continues."
204
- fi
205
-
206
- merged_count=$(vfield '.merged')
207
- deferred_count=$(vfield '.deferred')
208
- # Empty rather than `0`, so the suffix below stays absent where the old block
209
- # left it absent — `${x:+...}` fires on any non-empty value, and "0 deferred"
210
- # is a line no run has ever printed.
211
- [ "$deferred_count" = "0" ] && deferred_count=""
212
- if [ "$merged_count" = "0" ] && [ -z "$deferred_count" ]; then
213
- echo "step: no branches found in plan — proceeding (nothing to verify)"
214
- else
215
- echo "step: verified $merged_count branch(es) merged${deferred_count:+, $deferred_count deferred}"
216
- fi
217
-
218
- MAIN=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's#^origin/##')
219
- [ -n "$MAIN" ] || MAIN=$(bash "$script_dir/plot-host.sh" default-branch 2>/dev/null) || MAIN=""
220
- [ -n "$MAIN" ] || MAIN="main"
221
-
222
- who="${who_override:-${PLOT_DELIVER_WHO:-$(git config user.name 2>/dev/null || echo plot)}}"
223
- today=$(date +%Y-%m-%d)
224
-
225
- if [ "$dry_run" = 1 ]; then
226
- echo "step: would flip Phase → Delivered and fill Delivered: $today"
227
- echo "step: would move active/ → delivered/ symlink"
228
- echo "step: would tick the sprint item${sprint:+ (sprint: $sprint)}"
229
- echo "summary: phase=would record=would index=would sprint=would push=would"
230
- exit 0
231
- fi
232
-
233
- # ---------------------------------------------------------------------------
234
- # Steps 3-6 — the local writes, in a booking worktree off origin/<default>
235
- # ---------------------------------------------------------------------------
236
- #
237
- # A SEPARATE WORKTREE, not a checkout here. The caller's working tree may carry
238
- # uncommitted work, and switching it out from under them is exactly the write
239
- # this script otherwise refuses.
240
-
241
- # The CANONICAL plan file, not the index symlink.
242
- real_plan_path() { # $1 = plan file as found
243
- local p="$1" d b t
244
- d=$(cd "$(dirname "$p")" 2>/dev/null && pwd) || return 1
245
- b=$(basename "$p")
246
- t=$(readlink "$d/$b" 2>/dev/null || true)
247
- if [ -n "$t" ]; then
248
- case "$t" in
249
- /*) d=$(cd "$(dirname "$t")" 2>/dev/null && pwd) || return 1 ;;
250
- *) d=$(cd "$d/$(dirname "$t")" 2>/dev/null && pwd) || return 1 ;;
251
- esac
252
- b=$(basename "$t")
253
- fi
254
- case "$d" in
255
- "$repo_root") printf '%s' "$b" ;;
256
- "$repo_root"/*) printf '%s/%s' "${d#$repo_root/}" "$b" ;;
257
- *) return 1 ;;
258
- esac
7
+ script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
8
+ bundle="$script_dir/board/plot-deliver.mjs"
9
+ [ -f "$bundle" ] || {
10
+ echo "plot-deliver: cannot find $bundle — update the plot plugin in this repository, or — in the plot repository itself — run: pnpm build:board" >&2
11
+ exit 2
259
12
  }
260
-
261
- rel=$(cd "$repo_root" && real_plan_path "$plan_file") || rel=""
262
- [ -n "$rel" ] || die "$plan_file is outside the repository root ($repo_root).
263
- Move the plan under $PLAN_DIR inside this checkout and re-run."
264
-
265
- # The filename, for symlink creation.
266
- plan_basename=$(basename "$rel")
267
-
268
- # Flip `**State:** Approved` → `Delivered` in the `## Status` section only.
269
- # Reads `State:` and `Phase:` alike: it changes the VALUE on whichever line
270
- # carries it, so a plan written before the 2026-09-07 rename still delivers.
271
- #
272
- # READS ONE FILE AND WRITES ANOTHER, rather than editing in place. It edited in
273
- # place until 2026-09-02, which is what let the phase land without its record —
274
- # see write_transition() below.
275
- flip_phase() { # $1=in $2=out → 0 if it changed the file, 1 if nothing to flip
276
- awk '
277
- BEGIN { section = ""; done = 0 }
278
- /^## / { section = ($0 ~ /^## Status/) ? "status" : ""; print; next }
279
- section == "status" && !done && tolower($0) ~ /^[ \t]*[-*]?[ \t]*\**(state|phase)[:*]/ {
280
- if (tolower($0) ~ /approved/) {
281
- sub(/[Aa]pproved/, "Delivered")
282
- done = 1
283
- changed = 1
284
- }
285
- }
286
- { print }
287
- END { exit (changed ? 0 : 1) }
288
- ' "$1" > "$2"
289
- }
290
-
291
- # Insert one `- **Delivered:** YYYY-MM-DD` line into the plan's `## Status` section.
292
- # Fills the placeholder first; falls back to appending after the last list item.
293
- #
294
- # IT STOPS AT AN HTML COMMENT, and that is the whole of the fix made 2026-09-01.
295
- # The plan template ends `## Status` with
296
- #
297
- # <!-- Transition records — written by the workflow commands, not by hand:
298
- # - **Started:** <date>, <who>, <branch>
299
- # -->
300
- #
301
- # and those `- **Started:**` lines ARE list items. The scan tracked the last one
302
- # it saw, so on any plan whose `Started:` records had been filled in, the record
303
- # was appended INSIDE the comment — inert, and invisible to
304
- # `plot-plan-meta.sh`, which reported `delivered_raw: ""` for a plan that had
305
- # just been delivered. Measured on `a-browser-test-serves-its-own-state`: phase
306
- # flipped to Delivered, two `Delivered:` lines written into the comment (the
307
- # script is idempotent on the PHASE, so a second run wrote a second line), and
308
- # no readable record at all.
309
- #
310
- # A comment is where a plan keeps the shape of a record rather than a record, so
311
- # the insertion point is the last list item BEFORE one — never inside.
312
- #
313
- # READS ONE FILE AND WRITES ANOTHER, for the reason flip_phase() gives.
314
- append_delivered_line() { # $1=in $2=out $3=record
315
- local line
316
- line="- **Delivered:** $3"
317
- awk -v line="$line" '
318
- { lines[++n] = $0 }
319
- END {
320
- for (i = 1; i <= n; i++) {
321
- if (lines[i] ~ /^##[ \t]*[Ss]tatus[ \t]*$/) { start = i; break }
322
- }
323
- if (!start) exit 1
324
-
325
- insert = start
326
- for (i = start + 1; i <= n; i++) {
327
- if (lines[i] ~ /^##[ \t]/) break
328
- # An HTML comment ends the writable region. Checked BEFORE the
329
- # placeholder arms so a commented-out `- **Delivered:**` template line
330
- # is never mistaken for the slot to fill.
331
- if (lines[i] ~ /<!--/) break
332
- if (lines[i] ~ /^[ \t]*[-*][ \t]*\*\*Delivered:\*\*[ \t]*$/) { slot = i; break }
333
- if (lines[i] ~ /^[ \t]*[-*][ \t]/) insert = i
334
- }
335
-
336
- for (i = 1; i <= n; i++) {
337
- if (i == slot) { print line; continue } # replaces the empty placeholder
338
- print lines[i]
339
- if (!slot && i == insert) print line
340
- }
341
- }
342
- ' "$1" > "$2"
343
- }
344
-
345
- # ---------------------------------------------------------------------------
346
- # THE TRANSITION — one value, decided in the domain, written whole or not at all
347
- # ---------------------------------------------------------------------------
348
- #
349
- # WHAT THIS CLOSES. The phase and the record were two independent steps until
350
- # 2026-09-02: `flip_phase` edited the file, and `append_delivered_line` then
351
- # edited it again. The second could fail — a plan with no `## Status` heading is
352
- # its documented refusal — and the first had already landed. That is not
353
- # theoretical: measured 2026-08-20, a plan carrying `Phase: Delivered` with no
354
- # `Delivered:` line was filtered out of `plot-fleet-scan.sh` ENTIRELY, which
355
- # reads its delivered window from `delivered_raw` — the record itself. The scan
356
- # reported zero plans for it. A phase without its record does not make a plan
357
- # half-delivered; it makes it invisible.
358
- #
359
- # SO THE TWO ARE ONE WRITE. The domain says so in a type: `transitions/plan.ts`
360
- # gives `Decision` a required `phase` AND a required `record`, so a decision
361
- # missing either does not compile. `plot-transition.mjs` carries that value out
362
- # to here, and this function is the half that could still have broken it —
363
- # both edits run against scratch copies, and the plan file is replaced only
364
- # once both have succeeded.
365
- #
366
- # THE DOMAIN DECIDES, THIS PERFORMS. The phase word and the record's text come
367
- # back from `plot-transition.mjs`; the awk that knows where a `## Status` line
368
- # lives stays here, because that is adaptation and it carries bug history worth
369
- # keeping (the HTML-comment fix above, 2026-09-01).
370
- #
371
- # IDEMPOTENCE IS UNCHANGED, and it is still the source that answers. The domain
372
- # reads the phase and the record THIS SCRIPT PARSED FROM THE FILE IT IS ABOUT TO
373
- # WRITE, and answers `already` when both are present — the same question the two
374
- # steps asked separately, asked once. No progress file appears here, because a
375
- # progress file is exactly what would disagree with the repository when somebody
376
- # intervened by hand between two runs.
377
- transition_mjs="$script_dir/board/plot-transition.mjs"
378
-
379
- # Ask the domain for the transition, and refuse in its words.
380
- #
381
- # Called with the file's OWN parse rather than the caller's: on the booking-
382
- # worktree flow those are different files, and the plan on the default branch is
383
- # the one that counts.
384
- decide_transition() { # $1=file → prints "<Phase>\t<record>\t<write|already>"
385
- local f="$1" m answer rc
386
- [ -f "$transition_mjs" ] \
387
- || { echo "plot-deliver: cannot find $transition_mjs — run 'pnpm build:board'." >&2; return 1; }
388
- m=$(bash "$script_dir/plot-plan-meta.sh" "$f" 2>/dev/null) || m=""
389
- [ -n "$m" ] || { echo "plot-deliver: cannot parse $f — refusing rather than guessing." >&2; return 1; }
390
- answer=$(printf 'deliver\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\t\t\n' \
391
- "$slug" \
392
- "$(printf '%s' "$m" | jq -r '.phase // ""')" \
393
- "$(printf '%s' "$m" | jq -r '.review // ""')" \
394
- "$(printf '%s' "$m" | jq -r '.approved_raw // ""')" \
395
- "$(printf '%s' "$m" | jq -r '.delivered_raw // ""')" \
396
- "$(printf '%s' "$m" | jq -r '.released_raw // ""')" \
397
- "$today" \
398
- | node "$transition_mjs" 2>&1)
399
- rc=$?
400
- # Exit 1 is the domain's refusal and its sentence, tab-separated after the
401
- # rule that fired. Exit 2 is this script handing it something unreadable,
402
- # which no operator can act on — so it reports as the bug it is.
403
- if [ "$rc" != 0 ]; then
404
- if [ "$rc" = 1 ]; then
405
- echo "plot-deliver: $(printf '%s' "$answer" | cut -f2-)" >&2
406
- else
407
- echo "plot-deliver: $answer" >&2
408
- fi
409
- return 1
410
- fi
411
- printf '%s' "$answer"
412
- }
413
-
414
- # Apply the decided transition: the phase and the record, or neither.
415
- #
416
- # BOTH EDITS RUN AGAINST SCRATCH FILES and the plan is replaced by one `mv`.
417
- # An `## Status` section that cannot take the record leaves the file exactly as
418
- # it was found — INCLUDING ITS PHASE — so re-running is still the repair, and
419
- # the half-state that made a plan invisible cannot be reached from here.
420
- #
421
- # `$3` says whether the file already carries the record. A plan can carry one
422
- # while its phase lags — written by hand, or an earlier run cut between the two
423
- # writes — and appending a second is how one plan came to hold two `Delivered:`
424
- # lines (2026-09-01). The domain returns the written record unchanged in that
425
- # case, so the phase still flips and nothing is inserted.
426
- # Would the PARSER read this phase out of the file we are about to land?
427
- #
428
- # THE WRITE SUCCEEDING IS NOT THE OUTCOME HOLDING, and that gap is #924. A plan
429
- # carrying BOTH front matter and a `## Status` block was delivered in a project
430
- # repo: `flip_phase` wrote `Delivered` into the block, `mv` landed it, the
431
- # summary said `phase=flipped`, and `plot-plan-meta.sh` went on answering
432
- # `approved` — because it preferred front matter wherever it existed. The write
433
- # took effect on bytes nobody reads.
434
- #
435
- # THAT PRECEDENCE INVERTED IN #933, so the two-record plan now delivers rather
436
- # than refusing here: the parser reads the `## Status` block, which is the field
437
- # every lifecycle script writes. This gate is unchanged and is not softened —
438
- # its condition simply stops holding for that shape. It still fires on a scratch
439
- # copy the parser cannot read, and it still asks the parser rather than trusting
440
- # that awk changed a line.
441
- #
442
- # `flip_phase`'s awk matches only inside `section == "status"`. That one guard
443
- # IS the defect: on a front-matter plan it edits the block and leaves the front
444
- # matter untouched, and returns 0 for having changed something.
445
- #
446
- # SO THE TEST IS WHAT THE PARSER ANSWERS, NEVER WHETHER AWK CHANGED A LINE.
447
- # Refusing on `flipped=0` would break every re-run of a correct delivery — a
448
- # plan already carrying `Delivered` flips nothing and is fine. This asks the one
449
- # question that distinguishes them: read the scratch copy the way every later
450
- # consumer will read the plan, and compare.
451
- #
452
- # IT RUNS ON THE SCRATCH COPY, BEFORE THE `mv`, and the caller passes whichever
453
- # file that arm is about to land — `$a` on the `recorded=yes` arm, `$b` on the
454
- # other. Parsing `$a` on the record arm would check content that never reaches
455
- # the plan. After the `mv` is too late twice over: the script's own header
456
- # documents exit 0 as *"the plan is Delivered on the default branch"*, so
457
- # refusing there would exit 1 on a run meeting the documented success
458
- # condition — and `runAutoDeliver` spawns this detached, logging a non-zero exit
459
- # to nobody while the plan sits delivered on main.
460
- #
461
- # AN UNREADABLE SCRATCH COPY REFUSES. `decide_transition` already takes that
462
- # line — *"refusing rather than guessing"* — and a file the parser cannot read
463
- # is exactly the state this gate exists to keep off the plan.
464
- phase_would_read() { # $1=scratch file $2=expected phase (lowercase) → 0 agrees, 1 refuses
465
- local scratch="$1" want="$2" m got
466
- m=$(bash "$script_dir/plot-plan-meta.sh" "$scratch" 2>/dev/null) || m=""
467
- if [ -z "$m" ]; then
468
- echo "plot-deliver: $rel — the written file does not parse, so the delivery was not landed." >&2
469
- echo " Nothing was written. Re-run after fixing the file." >&2
470
- return 1
471
- fi
472
- got=$(printf '%s' "$m" | jq -r '.phase // ""')
473
- [ "$got" = "$want" ] && return 0
474
-
475
- # THE REFUSAL NAMES BOTH VALUES AND THE FILE. A message saying only "delivery
476
- # failed" throws away the half a person acts on: which phase was written, and
477
- # which one the parser still reads. The cause is named too, because the file
478
- # holding two records of one fact is the thing to fix — and which format ought
479
- # to win is a decision this gate deliberately leaves to a person.
480
- echo "plot-deliver: $rel — wrote phase '$want', but the parser still reads '$got'." >&2
481
- echo " The write landed and the parser reads something else, so the delivery" >&2
482
- echo " would have reported a success it did not achieve." >&2
483
- echo " Nothing was written — the plan is unchanged. Check what the plan says" >&2
484
- echo " its phase is, and where: a plan stating it in two places reports the" >&2
485
- echo " '## Status' block, which is the field every lifecycle script writes." >&2
486
- echo " See what the parser reads: $script_dir/plot-plan-meta.sh $rel" >&2
487
- return 1
488
- }
489
-
490
- write_transition() { # $1=file $2=record $3=recorded(yes|no) → sets phase_report record_report
491
- local f="$1" record="$2" recorded="$3" a="$1.plot-phase" b="$1.plot-record" flipped=0
492
-
493
- if flip_phase "$f" "$a"; then flipped=1; else flipped=0; fi
494
- # awk wrote `$a` either way; where nothing flipped it is a faithful copy, so
495
- # the record still has a file to be inserted into.
496
- [ -s "$a" ] || { rm -f "$a"; echo "plot-deliver: could not read $rel" >&2; return 1; }
497
-
498
- if [ "$recorded" = "yes" ]; then
499
- # THE DRY RUN, on the file this arm is about to land. A refusal discards the
500
- # scratch copy and leaves the plan byte-identical — and never reaches
501
- # `record_state_receipt`, which would otherwise license a commit of a state
502
- # that was refused.
503
- phase_would_read "$a" delivered || { rm -f "$a"; return 1; }
504
- mv "$a" "$f" || { rm -f "$a"; return 1; }
505
- record_report="already"
506
- else
507
- if ! append_delivered_line "$a" "$b" "$record"; then
508
- rm -f "$a" "$b"
509
- echo "plot-deliver: $rel has no '## Status' section — nowhere to record the delivery." >&2
510
- echo " Nothing was written: the phase is not flipped either, because a phase" >&2
511
- echo " with no record is invisible to the scan. Fix the section and re-run." >&2
512
- return 1
513
- fi
514
- # `$b` AND NOT `$a`: this arm lands the file carrying the record, so `$a`
515
- # is content that never reaches the plan.
516
- phase_would_read "$b" delivered || { rm -f "$a" "$b"; return 1; }
517
- mv "$b" "$f" || { rm -f "$a" "$b"; return 1; }
518
- rm -f "$a"
519
- record_report="written"
520
- fi
521
-
522
- phase_report=$([ "$flipped" = 1 ] && echo flipped || echo already)
523
- # The receipt plot-state-gate.sh clears on. Recorded after the `mv`, so it
524
- # names a value the file actually carries — a receipt written before a failed
525
- # write would license a commit of the state that was refused.
526
- record_state_receipt "$f" "Delivered"
527
- return 0
528
- }
529
-
530
- # Tick this plan's sprint item.
531
- #
532
- # THE TICK ONLY, since 2026-09-08. This also wrote a `<!-- status: delivered -->`
533
- # annotation, and `a-withdrawn-item-is-not-open` measured that record dead in
534
- # both directions: 67 lines carried one, none carried a value any reader acted
535
- # on, and `plot-sprint-release.sh` read the field nowhere. The plan file carries
536
- # `State:` and a dated `Delivered:` record, and the estate-outranks-the-checkbox
537
- # rule points at those — a cache nobody refreshes and nobody reads is a second
538
- # answer waiting to contradict the first.
539
- #
540
- # The box is NOT the same record. `/plot-sprint close` step 2a exists to tick
541
- # boxes the estate says are done, and ticking here is what keeps that step's
542
- # work to the items it genuinely cannot resolve.
543
- update_sprint_item() { # $1=worktree root → prints none|updated|already|missing
544
- local root="$1" f found=""
545
- [ -n "$sprint" ] || { printf 'none'; return 0; }
546
- local dir="$root/${SPRINT_DIR#/}"
547
- [ -d "$dir" ] || { printf 'missing'; return 0; }
548
- # Find the sprint file by content (the [<slug>] reference), not by filename.
549
- for f in "$dir"/*.md; do
550
- [ -e "$f" ] || continue
551
- grep -q "\[$slug\]" "$f" 2>/dev/null && { found="$f"; break; }
552
- done
553
- [ -n "$found" ] || { printf 'missing'; return 0; }
554
-
555
- local before after
556
- before=$(cat "$found")
557
- after=$(awk -v slug="$slug" '
558
- index($0, "[" slug "]") == 0 { print; next }
559
- {
560
- line = $0
561
- # Check the box. Nothing else on the line is touched.
562
- sub(/\[ \]/, "[x]", line)
563
- print line
564
- }
565
- ' "$found")
566
- if [ "$before" = "$after" ]; then printf 'already'; return 0; fi
567
- printf '%s\n' "$after" > "$found"
568
- printf 'updated'
569
- }
570
-
571
- # Move the active/ symlink to delivered/.
572
- move_index_symlink() { # $1=worktree root → prints moved|created|already|skipped
573
- local root="$1"
574
- local active_link="$root/${ACTIVE_DIR#/}$slug.md"
575
- local delivered_link="$root/${DELIVERED_DIR#/}$slug.md"
576
- local delivered_dir="$root/${DELIVERED_DIR#/}"
577
-
578
- # Ensure delivered/ directory exists (best effort)
579
- mkdir -p "$delivered_dir" 2>/dev/null || true
580
-
581
- # Check if already in delivered/
582
- if [ -e "$delivered_link" ]; then
583
- # Remove from active/ if still there
584
- git rm -q --ignore-unmatch "$active_link" 2>/dev/null || true
585
- printf 'already'
586
- return 0
587
- fi
588
-
589
- # Create the delivered/ symlink (relative path to the plan)
590
- ln -sfn "../$plan_basename" "$delivered_link" 2>/dev/null || { printf 'skipped'; return 0; }
591
-
592
- # Remove from active/ (best effort, ignore if not there)
593
- git rm -q --ignore-unmatch "$active_link" 2>/dev/null || true
594
-
595
- printf 'moved'
596
- }
597
-
598
- # The whole local half, run inside one directory.
599
- apply_local_writes() { # $1=root → sets phase_report record_report index_report sprint_report
600
- local root="$1" f="$1/$rel"
601
- [ -f "$f" ] || { echo "plot-deliver: $rel is not present in $root" >&2; return 1; }
602
-
603
- # Step 3 — THE TRANSITION: the phase and its record, together or not at all.
604
- #
605
- # ONE STEP WHERE THERE WERE TWO, which is the whole of this change. The domain
606
- # decides what the two lines say and whether they are owed at all; this writes
607
- # them as one replacement of the file. Already-done test: the domain answers
608
- # `already` when the file it is about to write ALREADY carries both, which is
609
- # the source asked directly — never a progress file.
610
- local decided record action recorded
611
- decided=$(decide_transition "$f") || return 1
612
- record=$(printf '%s' "$decided" | cut -f2)
613
- action=$(printf '%s' "$decided" | cut -f3)
614
- recorded=$(printf '%s' "$decided" | cut -f4)
615
- if [ "$action" = "already" ]; then
616
- phase_report="already"
617
- record_report="already"
618
- else
619
- write_transition "$f" "$record" "$recorded" || return 1
620
- fi
621
-
622
- # Step 5 — move the index symlink (best effort).
623
- index_report=$(move_index_symlink "$root")
624
-
625
- # Step 6 — tick the sprint item.
626
- sprint_report=$(update_sprint_item "$root")
627
- return 0
628
- }
629
-
630
- phase_report="" record_report="" index_report="skipped" sprint_report="none"
631
- push_report="n/a"
632
-
633
- # Fetch and create a booking worktree off origin/<default>.
634
- git fetch -q origin "$MAIN" 2>/dev/null
635
-
636
- bookbr="plot/deliver-$slug"
637
- tmpwt="$wt_root/.plot-deliver-$slug.$$"
638
- # -B: a leftover branch from an earlier failed run must not block this one.
639
- git worktree add -q -B "$bookbr" "$tmpwt" "origin/$MAIN" 2>/dev/null \
640
- || die "could not prepare a booking worktree at $tmpwt.
641
- Most often origin/$MAIN is not fetched, or '$bookbr' is checked out in
642
- another worktree. Check both: git fetch origin $MAIN && git worktree list
643
- Nothing has been written locally; the plan is untouched."
644
-
645
- cleanup() {
646
- git worktree remove --force "$tmpwt" >/dev/null 2>&1 || true
647
- git branch -D "$bookbr" >/dev/null 2>&1 || true
648
- }
649
-
650
- if ! apply_local_writes "$tmpwt"; then
651
- cleanup
652
- exit 1
653
- fi
654
-
655
- # Stage the plan file and index changes.
656
- git -C "$tmpwt" add -- "$rel" >/dev/null 2>&1 || true
657
- git -C "$tmpwt" add -- "${ACTIVE_DIR#/}" >/dev/null 2>&1 || true
658
- git -C "$tmpwt" add -- "${DELIVERED_DIR#/}" >/dev/null 2>&1 || true
659
- [ "$sprint_report" = "updated" ] && git -C "$tmpwt" add -- "${SPRINT_DIR#/}" >/dev/null 2>&1
660
-
661
- # THE BOOKED PLAN, KEPT FOR THE TRACKER. The booking worktree is removed on
662
- # every exit below, and the working tree may still read `Approved`; the issue
663
- # status is decided from the file that reached the default branch.
664
- booked_plan=""
665
- plot_tmpfile booked_plan deliver-plan
666
- cp "$tmpwt/$rel" "$booked_plan" 2>/dev/null || : > "$booked_plan"
667
-
668
- if git -C "$tmpwt" diff --cached --quiet 2>/dev/null; then
669
- # THE IDEMPOTENT EXIT. Everything this run would have written was already
670
- # on the default branch, so there is nothing to push and nothing wrong.
671
- push_report="nothing-to-commit"
672
- echo "step: nothing to commit — the delivery is already recorded on $MAIN"
673
- cleanup
674
- else
675
- if ! git -C "$tmpwt" -c "user.name=$who" commit -q -m "plot: deliver $slug"; then
676
- cleanup
677
- die "could not commit the delivery.
678
- See what git refused: git -C $tmpwt status
679
- Nothing was pushed; re-run this — it is idempotent."
680
- fi
681
-
682
- push_out=$(bash "$script_dir/plot-push-main.sh" "$bookbr" "$MAIN" 2>&1)
683
- push_rc=$?
684
- printf '%s\n' "$push_out" | sed 's/^/ /'
685
- if [ "$push_rc" = 0 ]; then
686
- push_report=$(printf '%s' "$push_out" | sed -n 's/^push: \([a-z]*\).*/\1/p' | head -1)
687
- [ -n "$push_report" ] || push_report="unknown"
688
- cleanup
689
- else
690
- # BRANCH PROTECTION FALLBACK — open a micro-PR if push is rejected.
691
- echo "step: push rejected — opening a micro-PR instead"
692
- if git push -q origin "$bookbr" 2>/dev/null \
693
- && micro_url=$(bash "$script_dir/plot-host.sh" pr-create \
694
- --title "plot: deliver $slug" \
695
- --body "Records the delivery of \`$slug\`." \
696
- --base "$MAIN" --head "$bookbr" 2>/dev/null) \
697
- && micro_num=$(printf '%s' "$micro_url" | sed 's#.*/##') \
698
- && bash "$script_dir/plot-host.sh" pr-merge "$micro_num" --delete-branch >/dev/null 2>&1
699
- then
700
- push_report="micro-pr"
701
- echo "step: delivery landed via micro-PR $micro_url"
702
- cleanup
703
- else
704
- push_report="rejected"
705
- echo "plot-deliver: the delivery is committed on '$bookbr' but could not reach $MAIN." >&2
706
- echo " Land '$bookbr' by hand, or re-run this command once the push works." >&2
707
- git worktree remove --force "$tmpwt" >/dev/null 2>&1 || true
708
- rm -f "$booked_plan"
709
- # NOTHING REACHED THE DEFAULT BRANCH, so no status is owed yet.
710
- echo "summary: phase=$phase_report record=$record_report index=$index_report sprint=$sprint_report push=$push_report tracker=skipped"
711
- exit 1
712
- fi
713
- fi
714
- fi
715
-
716
- # THE TRACKER HEARS AFTER THE DELIVERY LANDED, and never decides it. The plan is
717
- # delivered; the tracker holds a copy of one fact about it. Every outcome of
718
- # plot-issue-status.sh, a failed write and an unreadable bundle included, is a
719
- # report on the summary line and leaves this script's exit code alone.
720
- tracker_out=$(bash "$script_dir/plot-issue-status.sh" "$booked_plan" 2>&1)
721
- printf '%s\n' "$tracker_out" | grep -v '^summary: ' | sed '/^$/d; s/^/ tracker: /'
722
- tracker_report=$(printf '%s' "$tracker_out" | sed -n 's/^summary: tracker=\([a-z-]*\).*/\1/p' | tail -1)
723
- [ -n "$tracker_report" ] || tracker_report="failed"
724
- rm -f "$booked_plan"
725
-
726
- echo "summary: phase=$phase_report record=$record_report index=$index_report sprint=$sprint_report push=$push_report tracker=$tracker_report"
727
- # THE RECEIPT IS SPENT HERE, on the action COMPLETING — never at the gate.
728
- # `plot-controller-gate.sh` clears on a receipt and LEAVES it, so an
729
- # interrupted run can be repeated on the same licence: this script documents
730
- # re-running as the repair for any interruption after its irreversible step,
731
- # and a receipt spent at the gate would refuse that repair in the case it is
732
- # most needed. One authorisation, one completed action.
733
- spend_action_receipt "plot-deliver.sh"
734
-
735
- exit 0
13
+ exec node "$bundle" "$@"