@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.
@@ -1,429 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Plot helper: repair an ARTIFACT-ONLY merge conflict on one branch.
3
- # Usage: plot-resolve-artifact.sh [--dry-run] <branch>
4
- # --dry-run print the sequence and the worktree it would use; change nothing
5
- # <branch> the branch to repair — must already exist on origin
6
- # Output: one `step:` line per stage, then a machine-countable footer:
7
- # summary: branch=<name> outcome=pushed|abandoned|refused reason=<word>
8
- #
9
- # THE ONLY AUTOMATIC WRITE THIS SYSTEM GRANTS, and it is granted for three
10
- # verified reasons rather than for convenience:
11
- #
12
- # 1. `-merge` KEEPS THE FILE VALID. `.gitattributes` marks every bundle
13
- # `-merge`, so git keeps one side whole and writes NO conflict markers.
14
- # The artifact stays buildable JavaScript *through* a conflict — which is
15
- # why a script may touch it at all. `scripts/check-bundle-attributes.sh`
16
- # is the gate that keeps that true of every bundle rather than of one.
17
- # 2. THE REBUILD IS DETERMINISTIC. Measured: `build.mjs` embeds no timestamp
18
- # and no randomness, so the output does not depend on which side was kept.
19
- # 3. CI PROVES IT. The no-diff gate fails the build if the committed artifact
20
- # does not match a fresh rebuild.
21
- #
22
- # Together those make this the one repair whose correctness is checkable
23
- # WITHOUT JUDGEMENT. That is the whole licence. No other failure has these
24
- # three properties, and none may be added to this path — widening the entry
25
- # condition removes the argument that grants the permission, even if the code
26
- # looks correct.
27
- #
28
- # THIS IS A SCRIPT AND NOT AN AGENT, deliberately. Every step below is fixed
29
- # and nothing between them is a decision, which is *precisely* what licenses the
30
- # automation. Handing the sequence to an agent would introduce judgement exactly
31
- # where its absence is the permission. (Measured on 2026-08-17: this repo has no
32
- # `Worker command` configured either, so plot-dispatch.sh would report
33
- # `worker=unconfigured` and start nothing — but the shape is the reason, not
34
- # the measurement.)
35
- #
36
- # TESTS RUN BEFORE THE PUSH. The CI no-diff gate is what makes the repair
37
- # checkable, and CI runs only AFTER a push — so a resolver that pushed and
38
- # waited would manufacture exactly the state this exists to remove: a red PR in
39
- # the queue. The sequence therefore ends on `pnpm run test:board` green in the
40
- # branch's own worktree, and CI becomes confirmation rather than discovery.
41
- #
42
- # IF THE SUITE FAILS, NOTHING IS PUSHED. The repair stopped being mechanical the
43
- # moment its own gate said so; the branch is left exactly as it was, and the
44
- # board reports it as a conflict a human owns.
45
- #
46
- # IT MERGES ONLY IN A WORKTREE THAT IS IDLE. A worktree carrying modifications
47
- # belongs to whoever made them — measured on 2026-08-17, the resolver ran its
48
- # merge inside one an agent was actively editing. It refuses `worktree-busy`
49
- # rather than reaching in, which the plan names the honest minimum: a second
50
- # worktree on the same branch is not available to it anyway, since git refuses a
51
- # second checkout of one branch.
52
- #
53
- # AN EMPTY CONFLICT SET IS NOT A REFUSAL ABOUT FILES. Three cases, named apart,
54
- # because two of them were once one:
55
- #
56
- # bundles only → the licensed case → repair
57
- # other files present → needs judgement → not-artifact-only
58
- # empty, no merge ran → nothing was observed → not-observed
59
- #
60
- # The last is not a smaller version of the middle. `not-artifact-only` asserts
61
- # something about the files that conflicted, and a set of zero has none to
62
- # assert it about — saying it there sends a reader to look for files nobody ever
63
- # examined.
64
- #
65
- # WHICH SIDE IS TAKEN CANNOT MATTER, and the diff is never read. `--theirs` is
66
- # named here only because `git checkout` needs a word: the rebuild overwrites
67
- # whichever side was kept. Never phrase it as "take ours" — under `git merge`
68
- # *ours* is the branch being merged into, under `git rebase` it is the upstream,
69
- # and this repo rebases routinely.
70
- set -uo pipefail
71
-
72
- script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
73
- . "$script_dir/plot-tmp.sh"
74
-
75
- # THE FILES THIS SCRIPT MAY RESOLVE — a SET, and derived rather than listed.
76
- #
77
- # It was one hardcoded filename until 2026-09-06, and that cost a repair the
78
- # same day: PR #727 conflicted in `plot-registryd.mjs` — a `-merge` bundle with
79
- # a deterministic rebuild, exactly the licensed case — and this script refused
80
- # `not-artifact-only` against a list naming only `board-server.mjs`. The refusal
81
- # was correct behaviour against a stale list. Hours later the same branch
82
- # conflicted in `board-server.mjs` and was repaired automatically: same class of
83
- # conflict, opposite outcome, one filename apart.
84
- #
85
- # DERIVED FROM `build.mjs`'S OWN DECLARATIONS, by the same pipeline
86
- # `scripts/check-bundle-attributes.sh` uses, because a hand-written list here
87
- # would be a fourth place to drift — and drift is the defect this replaces. The
88
- # build declares each output as `const shippedX = path.join(here, '…')`, which
89
- # is what an author writes when adding a bundle; nothing else has to be
90
- # remembered. Nine bundles today; `plot-landed.mjs` arrived while the plan that
91
- # asked for this was still in draft, and the derivation found it.
92
- #
93
- # `plot-monitor.mjs` IS DELIBERATELY ABSENT. It is committed and documented, and
94
- # no `outfile` names it — nothing rebuilds it. Property 2 above is the whole
95
- # licence, so a file with no deterministic rebuild cannot be on this list. The
96
- # derivation reads the build, so it cannot ask for it.
97
- #
98
- # Still named in the board's contract as well, because the two run in different
99
- # languages and neither can import the other's constant. The pairing is asserted
100
- # by a test rather than trusted — and that test now asserts SET EQUALITY, since
101
- # a set that agrees on one member and differs on another is exactly the drift
102
- # this replaces.
103
- #
104
- # READ FROM THE REPOSITORY BEING REPAIRED, not from this script's own checkout.
105
- # The script is vendored into the published package, where `packages/` does not
106
- # exist — and it rebuilds with `pnpm build:board` inside the target repo, so the
107
- # build that defines the set is the one that will run. Resolved below, once
108
- # `repo_root` is known.
109
- bundle_set() { # $1=repo root → one path per line, sorted
110
- grep -aoE "shipped[A-Za-z]* = path\.join\([^)]*'[^']*'\)" "$1/packages/board/build.mjs" 2>/dev/null \
111
- | sed -E "s|.*'\.\./\.\./([^']*)'.*|\1|" \
112
- | sort -u
113
- }
114
-
115
- dry_run=0
116
- branch=""
117
- while [ $# -gt 0 ]; do
118
- case "$1" in
119
- --dry-run) dry_run=1 ;;
120
- -*) echo "plot-resolve-artifact: unknown option '$1'" >&2; exit 2 ;;
121
- *) branch="$1" ;;
122
- esac
123
- shift
124
- done
125
-
126
- # The footer travels on EVERY exit path, including the refusals. A run that
127
- # ends without one is indistinguishable from a crash, and a silent automatic
128
- # write is the failure mode this whole plan exists to remove.
129
- finish() { # $1=outcome $2=reason
130
- echo "summary: branch=$branch outcome=$1 reason=$2"
131
- case "$1" in
132
- pushed) exit 0 ;;
133
- *) exit 1 ;;
134
- esac
135
- }
136
-
137
- [ -n "$branch" ] || { echo "usage: plot-resolve-artifact.sh [--dry-run] <branch>" >&2; exit 2; }
138
-
139
- git rev-parse --git-dir >/dev/null 2>&1 || {
140
- echo "plot-resolve-artifact: not a git repository" >&2
141
- finish refused not-a-repo
142
- }
143
-
144
- MAIN=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null | sed 's#^origin/##')
145
- [ -n "$MAIN" ] || MAIN="main"
146
-
147
- repo_root=$(git rev-parse --show-toplevel)
148
-
149
- # THE SET, resolved against the repository this run will rebuild.
150
- ARTIFACT_PATHS=$(bundle_set "$repo_root")
151
-
152
- # AN EMPTY DERIVATION REFUSES, and it must: the guard below asks whether every
153
- # unmerged path is in this set, and against an empty set that question has no
154
- # true answer to give — but a guard written the other way round would have said
155
- # yes to everything. The build changing shape, or a checkout with no
156
- # `packages/`, is a reason to stop rather than a reason to repair blind.
157
- if [ -z "$ARTIFACT_PATHS" ]; then
158
- echo "step: no bundles derived from packages/board/build.mjs — refusing"
159
- finish refused no-bundle-set
160
- fi
161
-
162
- # WHICH WORKTREE HOLDS THIS BRANCH — ASK GIT, do not reconstruct the path from
163
- # the branch name.
164
- #
165
- # MEASURED, and recorded at length in plot-dispatch.sh's held_worktree: a
166
- # hand-made worktree is named for the branch with its TYPE dropped, so a gate
167
- # that guessed `plot-wt-<flattened>` missed a worktree with six modified files
168
- # in it. This site had the same shape — it composed `plot-wt-<flattened>` under
169
- # `repo_root/..` — and this change would make the guess worse, not better: the
170
- # new `Worktree root:` key introduces a SECOND naming convention, giving a
171
- # path guess a second way to be wrong. So the read asks git, and only the
172
- # CREATE-a-fresh-one fallback below composes a name (via dispatch's rule, so the
173
- # fresh worktree lands where dispatch would have put it).
174
- #
175
- # `git worktree list --porcelain` emits `worktree <path>` then `branch
176
- # refs/heads/<name>` per entry; the branch line is matched and the path taken
177
- # from the preceding one. A branch already dispatched is thus repaired in the
178
- # worktree it is already checked out in rather than in a second copy of itself —
179
- # git refuses a second checkout of one branch, and the two would fight over the
180
- # same index if it did not.
181
- wt=$(git worktree list --porcelain </dev/null 2>/dev/null | awk -v want="refs/heads/$branch" '
182
- /^worktree / { path = substr($0, 10) }
183
- /^branch / { if (substr($0, 8) == want) { print path; exit } }')
184
-
185
- # No existing worktree holds it — compose the path a fresh one will take, by the
186
- # same root+prefix rule plot-dispatch.sh uses. Under a `Worktree root:` key the
187
- # root moves and the `plot-wt-` prefix drops; absent it, today's behaviour.
188
- if [ -z "$wt" ]; then
189
- wt_root=$("$script_dir/plot-config.sh" get "Worktree root" "")
190
- if [ -z "$wt_root" ]; then
191
- wt_root=$(cd "$repo_root/.." && pwd)
192
- wt="$wt_root/plot-wt-$(printf '%s' "$branch" | tr '/' '-')"
193
- else
194
- case "$wt_root" in
195
- /*) : ;;
196
- *) wt_root="$repo_root/$wt_root" ;;
197
- esac
198
- wt="${wt_root%/}/$(printf '%s' "$branch" | tr '/' '-')"
199
- fi
200
- fi
201
-
202
- if [ "$dry_run" = 1 ]; then
203
- echo "step: would use worktree $wt"
204
- echo "step: would merge origin/$MAIN, take a side of each conflicted bundle, rebuild, test"
205
- printf 'step: bundle set (%s): %s\n' \
206
- "$(printf '%s\n' "$ARTIFACT_PATHS" | grep -c .)" \
207
- "$(printf '%s' "$ARTIFACT_PATHS" | tr '\n' ' ')"
208
- echo "step: would push only if pnpm run test:board passes"
209
- finish refused dry-run
210
- fi
211
-
212
- git fetch -q origin "$MAIN" "$branch" 2>/dev/null || true
213
-
214
- # ONE REPAIR AT A TIME, AND NEVER TWO ON ONE BRANCH.
215
- #
216
- # A second run while the first is working would fight over the same worktree:
217
- # the merge, the rebuild and the five-minute suite all write into it, and two of
218
- # them interleaved produce an artifact belonging to neither run. The lock is a
219
- # DIRECTORY rather than a file because `mkdir` is atomic on every filesystem
220
- # this runs on — two processes racing it, one wins, and the loser learns it lost
221
- # from the exit code rather than from a check-then-write that both pass.
222
- #
223
- # The board guards its own in-flight repairs too, in memory. Both are needed and
224
- # neither is redundant: the board's registry cannot see a repair started by a
225
- # second board or by a human at a shell, and this lock cannot stop the board
226
- # from spawning (it learns only after the spawn). The lock is the authority.
227
- lock="$repo_root/.plot/state/resolve-$(printf '%s' "$branch" | tr '/' '-').lock"
228
- mkdir -p "$(dirname "$lock")" 2>/dev/null || true
229
- if ! mkdir "$lock" 2>/dev/null; then
230
- echo "step: a repair is already in flight for $branch ($lock)"
231
- finish refused already-in-flight
232
- fi
233
- # Released on every exit, including a kill. A lock that outlives its process
234
- # would make one interrupted repair block the branch forever — and the repair is
235
- # idempotent, so there is nothing to protect after the process is gone. A TERM
236
- # or INT stops the repair (143 or 130) after the lock is released.
237
- plot_on_exit 'rmdir "$lock" 2>/dev/null || true'
238
-
239
- if [ -d "$wt" ] && git worktree list --porcelain | grep -qx "worktree $wt"; then
240
- # A REUSED WORKTREE MAY BELONG TO SOMEONE ELSE, and on 2026-08-17 one did: the
241
- # resolver ran `git merge` inside a worktree an agent was actively editing —
242
- # zero unmerged paths, three modified files, work in progress. It refused
243
- # before writing anything, but that was luck rather than design.
244
- #
245
- # Reuse is right when the worktree is IDLE; the name alone does not say so. A
246
- # worktree with modifications is one whose owner is mid-thought, and merging
247
- # into it would either fail on "local changes would be overwritten" or, worse,
248
- # succeed and fold a stranger's uncommitted work into a merge commit this
249
- # script then pushes.
250
- #
251
- # The honest minimum is to refuse, and the plan names it acceptable: creating a
252
- # scratch worktree is impossible anyway while git holds this branch checked out
253
- # here, since git refuses a second checkout of one branch.
254
- #
255
- # `--porcelain` rather than a parsed `git status`: it is the stable interface,
256
- # and an untracked file is deliberately NOT counted — a stray log or an
257
- # editor's scratch file is not work in progress, and `merge` does not touch it.
258
- echo "step: reusing worktree $wt"
259
- busy=$(git -C "$wt" status --porcelain --untracked-files=no 2>/dev/null)
260
- if [ -n "$busy" ]; then
261
- echo "step: worktree has modifications that are not this repair's — refusing"
262
- printf 'step: modified: %s\n' "$(printf '%s' "$busy" | sed 's/^...//' | tr '\n' ' ')"
263
- finish refused worktree-busy
264
- fi
265
- else
266
- if ! git worktree add -q "$wt" "$branch" 2>/dev/null; then
267
- if ! git worktree add -q -b "$branch" "$wt" "origin/$branch" 2>/dev/null; then
268
- echo "plot-resolve-artifact: cannot create a worktree for $branch at $wt" >&2
269
- finish refused no-worktree
270
- fi
271
- fi
272
- echo "step: worktree $wt"
273
- fi
274
-
275
- # THE FIXED SEQUENCE. Five steps, no decision between them.
276
-
277
- # 1. Merge. The conflict is EXPECTED — that is why we are here — so a non-zero
278
- # exit is not yet a failure. What decides is which paths came back
279
- # unmerged, checked next.
280
- git -C "$wt" merge --no-edit "origin/$MAIN" >/dev/null 2>&1
281
- merge_status=$?
282
-
283
- if [ "$merge_status" -eq 0 ]; then
284
- # Nothing conflicted after all — the prediction was made from refs that have
285
- # since moved, which is the direction this repo already knows they move in.
286
- # The merge stands; there is nothing to repair and nothing to prove, so this
287
- # pushes nothing rather than pushing a merge nobody asked for.
288
- git -C "$wt" merge --abort >/dev/null 2>&1 || true
289
- git -C "$wt" reset -q --hard "HEAD" >/dev/null 2>&1 || true
290
- echo "step: no conflict on merge — nothing to repair"
291
- finish refused no-conflict
292
- fi
293
-
294
- # 2. VERIFY THE SET, HERE, AGAINST THE REAL MERGE.
295
- #
296
- # The board classified from `merge-tree`, which predicts IN MEMORY from the refs
297
- # this machine holds. This is the merge itself, and it is the only place the set
298
- # is a fact rather than a forecast — a stale ref makes the prediction wrong in
299
- # the reassuring direction, so the entry condition is re-checked against reality
300
- # before anything is written.
301
- #
302
- # The set is read once and asked TWO questions, in order: was anything observed
303
- # at all, and — only then — was it exactly the artifact.
304
- unmerged=$(git -C "$wt" diff --name-only --diff-filter=U)
305
- n_unmerged=$(printf '%s\n' "$unmerged" | grep -c . || true)
306
-
307
- # AN EMPTY SET IS NOT A SMALL SET — it is the absence of a reading.
308
- #
309
- # The merge exited non-zero, so something went wrong; but a conflict is not the
310
- # only thing that ends a merge non-zero. A merge that never STARTED — refused
311
- # because the worktree was dirty, because a merge was already in progress, or
312
- # because the ref could not be resolved — exits non-zero too and leaves no
313
- # unmerged paths behind. Zero paths therefore answers a different question than
314
- # one or three do: those say WHICH files conflicted, zero says NOBODY LOOKED.
315
- #
316
- # Measured on 2026-08-17, and the defect this branch exists for: the resolver
317
- # reused a worktree in which no merge was running, read zero paths, compared
318
- # zero against one, and reported `not-artifact-only` — a name asserting
319
- # something about files it had never examined. The refusal was right; its reason
320
- # was wrong, and the wrong reason sent a reader looking for conflicts that did
321
- # not exist.
322
- #
323
- # So the two refusals are named apart. `not-artifact-only` is a claim about an
324
- # observed set and may only be said when there was one.
325
- if [ "$n_unmerged" = "0" ]; then
326
- git -C "$wt" merge --abort >/dev/null 2>&1 || true
327
- echo "step: the merge reported failure but left no unmerged paths — nothing was observed"
328
- finish refused not-observed
329
- fi
330
-
331
- # EVERY unmerged path is a bundle: the conflict set is a SUBSET of the bundle
332
- # set, and nothing else is in it. NOT "a bundle is among the conflicts" — an
333
- # implementation asking that passes every bundle-only case and silently repairs
334
- # merges that need judgement as a whole. The claim stayed exact when the list
335
- # grew from one file to nine; only the thing each path is checked against
336
- # changed. A merge conflicting in a bundle AND anything else still needs a
337
- # person, even though one of its files does not.
338
- #
339
- # WALKED PER PATH rather than compared as a whole, because the conflict set is
340
- # an arbitrary subset of nine and there is no single string to compare it to.
341
- # The direction is what keeps it exact: every element of the observed set must
342
- # appear in the licensed set, so an unlicensed path can only ever refuse.
343
- outside=""
344
- while IFS= read -r conflict; do
345
- [ -n "$conflict" ] || continue
346
- if ! printf '%s\n' "$ARTIFACT_PATHS" | grep -qxF -- "$conflict"; then
347
- outside="${outside}${outside:+ }$conflict"
348
- fi
349
- done <<EOF
350
- $unmerged
351
- EOF
352
-
353
- if [ -n "$outside" ]; then
354
- git -C "$wt" merge --abort >/dev/null 2>&1 || true
355
- echo "step: conflict set is not bundles only — refusing"
356
- printf 'step: unmerged: %s\n' "$(printf '%s' "$unmerged" | tr '\n' ' ')"
357
- printf 'step: outside the bundle set: %s\n' "$outside"
358
- finish refused not-artifact-only
359
- fi
360
-
361
- # 3. Take a side of EACH conflicted bundle. WHICH SIDE CANNOT MATTER — the
362
- # rebuild overwrites it — and the diff is never read. `--theirs` because the
363
- # command needs a word.
364
- #
365
- # Only the paths that actually conflicted, never the whole set: a bundle git
366
- # merged cleanly has no side to take, and `checkout --theirs` on an unmerged-
367
- # stage-free path errors rather than doing nothing useful.
368
- while IFS= read -r conflict; do
369
- [ -n "$conflict" ] || continue
370
- git -C "$wt" checkout --theirs -- "$conflict" 2>/dev/null \
371
- || git -C "$wt" checkout --ours -- "$conflict" 2>/dev/null \
372
- || true
373
- git -C "$wt" add -- "$conflict" 2>/dev/null || true
374
- echo "step: took a side of $conflict (either — the rebuild decides)"
375
- done <<EOF
376
- $unmerged
377
- EOF
378
-
379
- # 4. Rebuild, in the branch's OWN worktree. This is what makes the kept side
380
- # irrelevant, and it is the property CI's no-diff gate then re-checks.
381
- if ! (cd "$wt" && pnpm build:board >/dev/null 2>&1); then
382
- git -C "$wt" merge --abort >/dev/null 2>&1 || true
383
- echo "step: rebuild failed — pushing nothing"
384
- finish abandoned build-failed
385
- fi
386
- # EVERY bundle is staged after the rebuild, not just the ones that conflicted.
387
- # `pnpm build:board` regenerates all nine, and a rebuild triggered by one
388
- # conflict can legitimately move another — the merge brought in source changes
389
- # for the whole package. Staging only the conflicted paths would leave those
390
- # modifications unstaged, and CI's no-diff gate would then fail the push for a
391
- # file this run had already rebuilt correctly.
392
- while IFS= read -r bundle; do
393
- [ -n "$bundle" ] || continue
394
- git -C "$wt" add -- "$bundle" 2>/dev/null || true
395
- done <<EOF
396
- $ARTIFACT_PATHS
397
- EOF
398
- printf 'step: rebuilt and staged %s bundle(s)\n' "$(printf '%s\n' "$ARTIFACT_PATHS" | grep -c .)"
399
-
400
- # The merge commit exists only once the rebuild has produced the artifact it
401
- # will carry. Committing before the build would leave a commit holding a stale
402
- # artifact if the build then failed — exactly what CI's no-diff gate catches,
403
- # arriving as a push instead of as a refusal.
404
- if ! git -C "$wt" commit -q --no-edit 2>/dev/null; then
405
- echo "step: nothing to commit after the rebuild"
406
- finish abandoned nothing-to-commit
407
- fi
408
-
409
- # 5. THE GATE. Green in this worktree BEFORE the push, never CI after it.
410
- # A resolver that pushed and let CI decide passes every correctness check
411
- # above and manufactures a red PR in the queue — the exact stuck state this
412
- # plan exists to remove.
413
- echo "step: running pnpm run test:board"
414
- if ! (cd "$wt" && pnpm run test:board >/dev/null 2>&1); then
415
- # NOTHING IS PUSHED, and the merge is undone so the branch is left exactly as
416
- # it was found. A half-repaired branch would be a third state nobody named.
417
- git -C "$wt" reset -q --hard "HEAD~1" 2>/dev/null || true
418
- echo "step: test:board failed — pushing nothing, this is a conflict a human owns"
419
- finish abandoned tests-failed
420
- fi
421
- echo "step: test:board passed"
422
-
423
- if ! git -C "$wt" push -q origin "HEAD:$branch" 2>/dev/null; then
424
- echo "step: push rejected — the branch moved under us; leaving the repair local"
425
- finish abandoned push-failed
426
- fi
427
-
428
- echo "step: pushed $branch"
429
- finish pushed artifact-conflict-resolved