@plot-pm/board 0.17.0 → 0.19.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.
- package/dist/board-server.mjs +204 -187
- package/package.json +2 -1
- package/plot-agent-manifest.sh +9 -9
- package/plot-approve.sh +9 -978
- package/plot-config.sh +14 -4
- package/plot-default-branch.sh +15 -19
- package/plot-fleet-scan.sh +2 -2
- package/plot-host.sh +210 -113
- package/plot-reap.sh +12 -1250
package/plot-reap.sh
CHANGED
|
@@ -1,1251 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
# 4. DIRTY TREES NOBODY OWNS — named, never deleted
|
|
15
|
-
#
|
|
16
|
-
# Kinds 2-4 were added 2026-09-03. Measured on this estate the day before:
|
|
17
|
-
# 85 of 98 local branches already merged and nothing looked at them, claim refs
|
|
18
|
-
# whose agent never existed, and 2 dirty desks holding 52 and 1 files that
|
|
19
|
-
# every run refused and nothing ever resolved.
|
|
20
|
-
#
|
|
21
|
-
# AND ONE THING IT ONLY REPORTS: a worktree git lists whose directory is gone.
|
|
22
|
-
# `git worktree list --porcelain` calls it `prunable`, and it is NOT a sixth
|
|
23
|
-
# refusal. A refusal says *do not remove this* and sends an operator to look; a
|
|
24
|
-
# vanished entry says *there is nothing to remove and the entry is stale*, with
|
|
25
|
-
# `git worktree prune` as the repair. It is reported before the refusals are
|
|
26
|
-
# asked, because it is the prior question: four of the five measure something
|
|
27
|
-
# inside a tree that is not there. Measured 2026-09-06 on this estate, 3 of 20
|
|
28
|
-
# worktrees were prunable and this script named none of them.
|
|
29
|
-
#
|
|
30
|
-
# EVERY KIND KEEPS ONE SHAPE: `--dry-run` by default, acting on `--yes`,
|
|
31
|
-
# bounded by `--max N`. The bound is PER KIND, because the kinds are different
|
|
32
|
-
# acts on different populations — a run bounded to five worktrees has not
|
|
33
|
-
# thereby been asked to leave the 85th branch alone.
|
|
34
|
-
#
|
|
35
|
-
# THE ASYMMETRY BETWEEN KINDS IS DELIBERATE AND STAYS. A removed checkout comes
|
|
36
|
-
# back with `git worktree add` and a local branch is re-fetchable from origin,
|
|
37
|
-
# so both are swept estate-wide. A deleted REMOTE ref is not re-creatable at
|
|
38
|
-
# all, so `plot-release-refs.sh` deletes those under its own licence, its own
|
|
39
|
-
# five guards, and a blast radius bounded by one plan file.
|
|
40
|
-
#
|
|
41
|
-
# The gap this fills was named by a comment before it existed:
|
|
42
|
-
# `plot-reconcile-scan.sh:323` says "with a deferred: annotation the reaper
|
|
43
|
-
# would offer to DELETE real work" — describing a reaper that was never
|
|
44
|
-
# written. The scan reports; nothing reaped. Measured 2026-08-25 on this
|
|
45
|
-
# estate: 56 worktrees, 42 of them dispatch trees, of which 29 were finished.
|
|
46
|
-
#
|
|
47
|
-
# WHY A SCRIPT RATHER THAN AN AGENT (Manifesto Principle 3): every refusal
|
|
48
|
-
# below is a MEASUREMENT, not a judgement. Is a process alive; is the tree
|
|
49
|
-
# dirty; did the host merge the PR. An agent asked "is this safe to delete?"
|
|
50
|
-
# can talk itself past any of the three. A script cannot, and judgement's
|
|
51
|
-
# absence is exactly what licenses the delete.
|
|
52
|
-
#
|
|
53
|
-
# AND THE DECIDING IS NOT HERE. This script GATHERS the readings, asks
|
|
54
|
-
# `packages/domain/src/rules/reapable.ts`, and ACTS on the answer; it holds no
|
|
55
|
-
# `if` about whether a worktree may go. The five refusals are named values the
|
|
56
|
-
# rule returns, so each is triggerable against a fixture — including the
|
|
57
|
-
# combinations this estate will not produce on demand, a marker and a live pid
|
|
58
|
-
# at once and a host that cannot be asked at all. In shell they were five
|
|
59
|
-
# `if`s nothing could test.
|
|
60
|
-
#
|
|
61
|
-
# SO THIS SCRIPT NEEDS NODE, where its first version deliberately did not.
|
|
62
|
-
# That constraint is retired rather than quietly broken: the alternative is a
|
|
63
|
-
# second implementation of the five refusals, in shell, where nothing can test
|
|
64
|
-
# it — and a copy drifting toward permissive fails in the direction that
|
|
65
|
-
# deletes work. A rule that cannot be asked REFUSES, so a missing `node` keeps
|
|
66
|
-
# every tree and says so per tree rather than skipping them silently.
|
|
67
|
-
#
|
|
68
|
-
# DEFAULT IS --dry-run. Removal happens only under --yes.
|
|
69
|
-
#
|
|
70
|
-
# plot-reap.sh # report what WOULD be reaped
|
|
71
|
-
# plot-reap.sh --yes # actually remove them
|
|
72
|
-
# plot-reap.sh --yes --max 5 # bound it
|
|
73
|
-
#
|
|
74
|
-
# What is NEVER reaped, in the order the tests run:
|
|
75
|
-
# 1. a worktree with a LIVE worker process (a desk someone is at)
|
|
76
|
-
# 2. a worktree with uncommitted changes (work that exists nowhere else)
|
|
77
|
-
# 3. a worktree carrying a PLOT-BLOCKED* marker (a worker waiting on a person)
|
|
78
|
-
# 4. a branch NO PR of which merged (the host is the authority)
|
|
79
|
-
# 5. the main checkout, and any non-dispatch tree (not ours to remove)
|
|
80
|
-
#
|
|
81
|
-
# THOSE FIVE REFUSALS ARE UNCHANGED BY THE THREE NEW KINDS, in this file and in
|
|
82
|
-
# `packages/domain/src/rules/reapable.ts` alike. They were written for exactly
|
|
83
|
-
# the population they sweep, and a backstop that guesses is worse than none.
|
|
84
|
-
# Each new kind brings its own gate instead, in `rules/sweepable.ts`:
|
|
85
|
-
#
|
|
86
|
-
# local branch the host says merged, AND no worktree holds it. NEVER
|
|
87
|
-
# `git branch -d`, which refuses a squash-merged branch for the
|
|
88
|
-
# wrong reason and would have kept all 85 of them.
|
|
89
|
-
# claim ref only what `plot-reconcile-scan.sh` section 3 ALREADY calls
|
|
90
|
-
# reapable — a `deferred:`/`moved:` annotation. A bare
|
|
91
|
-
# `claimed:` is reported and left for a person.
|
|
92
|
-
# dirty tree nothing. There is no deletion path: where this guard is
|
|
93
|
-
# wrong, destruction cannot be undone.
|
|
94
|
-
#
|
|
95
|
-
# A dispatch tree is recognised by `.plot-worker.pid`, which the dispatcher
|
|
96
|
-
# writes at creation, OR by the legacy `plot-wt-` path. Both are supported
|
|
97
|
-
# permanently. Identifying one by its path ALONE was the defect fixed on
|
|
98
|
-
# 2026-08-30: `plot-wt-` is only used when `Worktree root` is absent, so on a
|
|
99
|
-
# repo that configures one the reaper matched nothing and reported
|
|
100
|
-
# `reapable=0 kept=0` over nine trees.
|
|
101
|
-
#
|
|
102
|
-
# A TREE NEITHER TEST PLACES IS REPORTED `unknown` AND STILL NOT TOUCHED.
|
|
103
|
-
# The recognition test above is unchanged in strictness — this adds no tree to
|
|
104
|
-
# the reapable population and offers no removal. What it removes is the
|
|
105
|
-
# SILENCE: until 2026-09-10 such a tree hit `continue`, so it was not reaped,
|
|
106
|
-
# not kept, not counted and not named, and that is how ten finished desks went
|
|
107
|
-
# unnoticed while the reaper reported three. Only a tree under the configured
|
|
108
|
-
# `Worktree root` qualifies; a hand-made checkout elsewhere stays silent,
|
|
109
|
-
# because a person's tree must never become an instruction to remove it.
|
|
110
|
-
#
|
|
111
|
-
# THE MANIFEST GOES WITH THE WORKTREE. `readAgentRegistry` renders one row per
|
|
112
|
-
# manifest, so a reap that removes only the checkout converts a finished agent
|
|
113
|
-
# into an `unknown` row naming a directory that no longer exists — measured
|
|
114
|
-
# 2026-08-26, twelve worktrees removed and seven such rows appearing at once.
|
|
115
|
-
# Nothing further needs deciding to remove it: an entry whose worktree the five
|
|
116
|
-
# tests above just cleared is covered by exactly those measurements.
|
|
117
|
-
#
|
|
118
|
-
# AND THE LOG GOES WITH THE WORKTREE TOO. Measured 2026-08-30: 190 log files,
|
|
119
|
-
# 2.6 MB beside the repository, the oldest from 2026-08-17, and NOT ONE
|
|
120
|
-
# belonging to live work. This script took the worktree and the manifest every
|
|
121
|
-
# time and left the log forever, so a finished agent's last act was to leave a
|
|
122
|
-
# file nobody would ever open again.
|
|
123
|
-
#
|
|
124
|
-
# It is the branch's own `plot-resolve-<branch>` run — log, `.state` and
|
|
125
|
-
# `.prompt.md` together, since a sweep that took the log alone would leave half
|
|
126
|
-
# a run behind. NOT the per-plan `plot-dispatch-<slug>.log`, which is appended
|
|
127
|
-
# to by every dispatch of a plan and outlives any one of its branches.
|
|
128
|
-
#
|
|
129
|
-
# ORDER: worktree FIRST, manifest second, log LAST. The first two are ordered
|
|
130
|
-
# because the reverse leaves a live worktree with no registration, which
|
|
131
|
-
# `readAgentRegistry` answers by SYNTHESIZING an `unknown` entry — the same bad
|
|
132
|
-
# row, earned a different way. A failure between them this way round leaves an
|
|
133
|
-
# orphaned manifest, which the sweep below clears on the next run.
|
|
134
|
-
#
|
|
135
|
-
# The log is last because it is the only one that is PURE CLEANUP: a missing
|
|
136
|
-
# manifest orphans an agent, a missing worktree loses a desk, and a missing log
|
|
137
|
-
# costs a record of work the host already merged. So a failure before it has
|
|
138
|
-
# cost the least, and its own failure costs nothing.
|
|
139
|
-
#
|
|
140
|
-
# A MISSING LOG IS NOT A REFUSAL. The five refusals above are about work that
|
|
141
|
-
# might be lost; a log describes work that has already landed. `rm -f`
|
|
142
|
-
# semantics — if it is not there, that is the desired state.
|
|
143
|
-
#
|
|
144
|
-
# AND IT IS NOT THE TRANSCRIPT. `<worktree>/.plot-worker.log` is the agent's own
|
|
145
|
-
# words and lives INSIDE the tree, so it goes when the tree does and is not
|
|
146
|
-
# swept here. This is the dispatcher's record of what it started. Two files,
|
|
147
|
-
# two lifetimes, and CLAUDE.md already distinguishes them.
|
|
148
|
-
#
|
|
149
|
-
# `--sweep-temp` IS A SEPARATE MODE, and it runs INSTEAD of the four kinds. A
|
|
150
|
-
# trap does not run on SIGKILL — the board ends a scan at its timeout,
|
|
151
|
-
# `bounded.sh` escalates to SIGKILL, a person kills a hung script — so some temp
|
|
152
|
-
# paths outlive every trap. It removes two populations, each owned by this user
|
|
153
|
-
# and older than `Temp sweep after` hours (default 24), by the entry's own
|
|
154
|
-
# modification time:
|
|
155
|
-
#
|
|
156
|
-
# - `$TMPDIR/plot-?*` entries directly under `$TMPDIR` — `plot-` and at least
|
|
157
|
-
# one more character, any separator: `mkdtempSync` appends six characters
|
|
158
|
-
# with no dot, so `plot-host-pTFuyG` is the common shape. Never `plot`,
|
|
159
|
-
# `plotter-old` or any `tmp.*`: that is every template-less `mktemp` on the
|
|
160
|
-
# machine, and neither owner nor age separates Plot's from another
|
|
161
|
-
# program's. A `plot-reg.<pid>` exit registry is kept while its pid lives,
|
|
162
|
-
# because a worker loop registers its exit command and runs for days.
|
|
163
|
-
# - `$PLOT_BUDGET_HOME/memo/<pid>` directories (default `~/.plot/state/memo`)
|
|
164
|
-
# whose pid is not alive.
|
|
165
|
-
#
|
|
166
|
-
# It lists each candidate with `find` and removes it by the full path it
|
|
167
|
-
# listed; it never passes a glob to `rm`, and it never reads `/tmp` or
|
|
168
|
-
# `/var/folders` when `$TMPDIR` points elsewhere. The age bound is safe because
|
|
169
|
-
# every Plot temp path belongs to one script call, one scan or one board
|
|
170
|
-
# request, and 24 h is about 1,000 times the scan's 90 s timeout.
|
|
171
|
-
set -u
|
|
172
|
-
|
|
173
|
-
DRY=1; MAX=0; SWEEP_TEMP=0
|
|
174
|
-
while [ $# -gt 0 ]; do
|
|
175
|
-
case "$1" in
|
|
176
|
-
--yes) DRY=0 ;;
|
|
177
|
-
--sweep-temp) SWEEP_TEMP=1 ;;
|
|
178
|
-
--dry-run) DRY=1 ;;
|
|
179
|
-
--max) MAX="${2:-0}"; shift ;;
|
|
180
|
-
# The header, however long it has become. A hardcoded last line silently
|
|
181
|
-
# truncates help mid-sentence the first time the header grows — measured
|
|
182
|
-
# here, when it grew past 42.
|
|
183
|
-
-h|--help) sed -n '2,/^[^#]/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//'; exit 0 ;;
|
|
184
|
-
*) echo "plot-reap: unknown argument: $1" >&2; exit 2 ;;
|
|
185
|
-
esac
|
|
186
|
-
shift
|
|
187
|
-
done
|
|
188
|
-
|
|
189
|
-
# Is a pid alive? `ps -p` answers for another user's process too, where
|
|
190
|
-
# `kill -0` reports EPERM, so a reused pid always keeps its entry.
|
|
191
|
-
pid_alive() { ps -p "$1" >/dev/null 2>&1; }
|
|
192
|
-
|
|
193
|
-
# One entry: report it, and remove it by the exact path unless this is a dry run.
|
|
194
|
-
sweep_one() { # $1=path $2=why
|
|
195
|
-
temp_swept=$((temp_swept + 1))
|
|
196
|
-
if [ "$DRY" = 1 ]; then
|
|
197
|
-
echo "temp: would remove $1 ($2)"
|
|
198
|
-
elif rm -rf -- "$1" 2>/dev/null; then
|
|
199
|
-
echo "temp: removed $1 ($2)"
|
|
200
|
-
temp_removed=$((temp_removed + 1))
|
|
201
|
-
else
|
|
202
|
-
echo "temp: could not remove $1 ($2)"
|
|
203
|
-
fi
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
sweep_temp() {
|
|
207
|
-
local hours root me memo entry name
|
|
208
|
-
hours=$("$(dirname "${BASH_SOURCE[0]}")/plot-config.sh" get "Temp sweep after" 24 2>/dev/null) || hours=24
|
|
209
|
-
case "$hours" in
|
|
210
|
-
''|*[!0-9]*) echo "plot-reap: 'Temp sweep after' must be a whole number of hours, not '$hours'" >&2; return 2 ;;
|
|
211
|
-
esac
|
|
212
|
-
root="${TMPDIR:-/tmp}"; root="${root%/}"
|
|
213
|
-
me=$(id -un)
|
|
214
|
-
temp_swept=0; temp_removed=0; temp_live=0
|
|
215
|
-
while IFS= read -r entry; do
|
|
216
|
-
[ -n "$entry" ] || continue
|
|
217
|
-
[ "$MAX" -gt 0 ] && [ "$temp_swept" -ge "$MAX" ] && break
|
|
218
|
-
name=${entry##*/}
|
|
219
|
-
case "$name" in
|
|
220
|
-
plot-reg.*)
|
|
221
|
-
if pid_alive "${name#plot-reg.}"; then temp_live=$((temp_live + 1)); continue; fi ;;
|
|
222
|
-
esac
|
|
223
|
-
sweep_one "$entry" "older than ${hours}h"
|
|
224
|
-
done <<LIST
|
|
225
|
-
$(find "$root" -mindepth 1 -maxdepth 1 -user "$me" -name 'plot-?*' -mmin +$((hours * 60)) -print 2>/dev/null)
|
|
226
|
-
LIST
|
|
227
|
-
memo="${PLOT_BUDGET_HOME:-${HOME:-}/.plot/state}/memo"
|
|
228
|
-
if [ -d "$memo" ]; then
|
|
229
|
-
while IFS= read -r entry; do
|
|
230
|
-
[ -n "$entry" ] || continue
|
|
231
|
-
[ "$MAX" -gt 0 ] && [ "$temp_swept" -ge "$MAX" ] && break
|
|
232
|
-
name=${entry##*/}
|
|
233
|
-
case "$name" in ''|*[!0-9]*) continue ;; esac
|
|
234
|
-
if pid_alive "$name"; then temp_live=$((temp_live + 1)); continue; fi
|
|
235
|
-
sweep_one "$entry" "memo of dead pid $name, older than ${hours}h"
|
|
236
|
-
done <<LIST
|
|
237
|
-
$(find "$memo" -mindepth 1 -maxdepth 1 -type d -user "$me" -mmin +$((hours * 60)) -print 2>/dev/null)
|
|
238
|
-
LIST
|
|
239
|
-
fi
|
|
240
|
-
echo "temp-summary: swept=$temp_swept removed=$temp_removed kept_live=$temp_live bound_hours=$hours root=$root dry_run=$DRY"
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if [ "$SWEEP_TEMP" = 1 ]; then
|
|
244
|
-
sweep_temp
|
|
245
|
-
exit $?
|
|
246
|
-
fi
|
|
247
|
-
|
|
248
|
-
command -v git >/dev/null 2>&1 || { echo "plot-reap: git not found" >&2; exit 2; }
|
|
249
|
-
ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || {
|
|
250
|
-
echo "plot-reap: not a git repository" >&2; exit 2; }
|
|
251
|
-
|
|
252
|
-
# The default branch, via the host adapter when it can answer and `main`
|
|
253
|
-
# otherwise. A wrong answer here would only ever make the ancestry test MORE
|
|
254
|
-
# conservative, never less.
|
|
255
|
-
HOST="$(dirname "${BASH_SOURCE[0]}")/plot-host.sh"
|
|
256
|
-
DEFAULT=main
|
|
257
|
-
if [ -x "$HOST" ]; then
|
|
258
|
-
d=$("$HOST" default-branch 2>/dev/null) && [ -n "$d" ] && DEFAULT="$d"
|
|
259
|
-
fi
|
|
260
|
-
git fetch origin "$DEFAULT" --quiet 2>/dev/null || true
|
|
261
|
-
|
|
262
|
-
# Does the host say ANY PR for this branch merged?
|
|
263
|
-
#
|
|
264
|
-
# SOURCED from `plot-pr-merged.sh` rather than defined here, since 2026-08-28.
|
|
265
|
-
# It lived in this file until `plot-release-refs.sh` needed the SAME gate: both
|
|
266
|
-
# scripts ask "has this branch's work landed", and a second implementation that
|
|
267
|
-
# drifted toward permissive would delete a ref that cannot be restored. The
|
|
268
|
-
# helper carries the reasoning — `mergedAt` never `state`, ANY PR never the
|
|
269
|
-
# newest — and defines `pr_merged` and nothing else on load.
|
|
270
|
-
. "$(dirname "${BASH_SOURCE[0]}")/plot-pr-merged.sh"
|
|
271
|
-
|
|
272
|
-
# Which uncommitted paths count as unlanded work. SOURCED, because the reap
|
|
273
|
-
# reading, the dirty sweep below and `plot-reconcile-scan.sh` section 21 must
|
|
274
|
-
# read one tree the same way; the helper names each excused path.
|
|
275
|
-
. "$(dirname "${BASH_SOURCE[0]}")/plot-desk-dirt.sh"
|
|
276
|
-
|
|
277
|
-
# Is an AGENT still working at a desk? SOURCED from `plot-worker-state.sh`,
|
|
278
|
-
# the ONE classifier `plot-dispatch.sh --stop` also asks. Reading only whether
|
|
279
|
-
# the recorded pid answers `ps` asked about the wrapper shell, which outlives
|
|
280
|
-
# its agent: a desk whose agent had exited read `worker alive` here while
|
|
281
|
-
# `--stop` answered `finished` for the same tree, so it could be neither
|
|
282
|
-
# stopped nor reaped.
|
|
283
|
-
# shellcheck source=plot-worker-state.sh
|
|
284
|
-
. "$(dirname "${BASH_SOURCE[0]}")/plot-worker-state.sh"
|
|
285
|
-
|
|
286
|
-
# The live worker's pid at a desk, or empty. The FIVE process states map to
|
|
287
|
-
# one reading: `running` is live, and `finished`, `failed`, `ended` and `none`
|
|
288
|
-
# are not.
|
|
289
|
-
#
|
|
290
|
-
# `waiting` and `stalled` are DISCARDED BY NAME. They are desk facts — a marker
|
|
291
|
-
# for a person, work on the floor — which `plot_worker_state` reaches only once
|
|
292
|
-
# no agent runs, and they answer what the agent still OWES. That question is
|
|
293
|
-
# the blocked-marker, uncommitted and unpushed readings' below; only the
|
|
294
|
-
# process fact under the two words (no agent is running) crosses into the
|
|
295
|
-
# liveness reading.
|
|
296
|
-
#
|
|
297
|
-
# A word this mapping does not know keeps the desk: it reports the recorded
|
|
298
|
-
# pid, or `unknown`, rather than guessing the process is gone.
|
|
299
|
-
desk_worker_pid() { # $1=worktree → the live worker's pid, or empty
|
|
300
|
-
local row state spid
|
|
301
|
-
row=$(plot_worker_state "$1" "")
|
|
302
|
-
state=$(printf '%s' "$row" | cut -f1)
|
|
303
|
-
spid=$(printf '%s' "$row" | cut -f2)
|
|
304
|
-
case "$state" in
|
|
305
|
-
running) printf '%s' "${spid:-unknown}" ;;
|
|
306
|
-
finished|failed|ended|none) ;;
|
|
307
|
-
waiting|stalled) ;;
|
|
308
|
-
*) printf '%s' "${spid:-unknown}" ;;
|
|
309
|
-
esac
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
# The commits on a desk's HEAD that no remote holds, as short shas, one per
|
|
313
|
-
# line. Returns 1 when they cannot be counted, which the rule reads as
|
|
314
|
-
# `unknown` and refuses on.
|
|
315
|
-
#
|
|
316
|
-
# NOT `@{upstream}..HEAD`. The host deletes a branch when its PR merges, and
|
|
317
|
-
# the reaper only removes desks whose PR merged, so after `git fetch --prune`
|
|
318
|
-
# every desk it serves has lost its upstream — and an absent upstream counts as
|
|
319
|
-
# nothing. The reading is `HEAD --not --remotes` instead: what no remote-tracking
|
|
320
|
-
# ref holds. After a squash merge and a pruned ref, the branch's own commits
|
|
321
|
-
# are on no remote ref either, so the head the host MERGED is excluded too
|
|
322
|
-
# (`pr_merged_heads`): a commit beyond it was never pushed.
|
|
323
|
-
desk_unpushed() { # $1=worktree $2=branch $3=merge reading → short shas
|
|
324
|
-
local wt="$1" br="$2" merge="$3" list heads h
|
|
325
|
-
local -a excl=()
|
|
326
|
-
list=$(git -C "$wt" rev-list --abbrev-commit HEAD --not --remotes 2>/dev/null) || return 1
|
|
327
|
-
[ -n "$list" ] || return 0
|
|
328
|
-
if [ -n "$br" ] && [ "$merge" = merged ]; then
|
|
329
|
-
heads=$(pr_merged_heads "$br") || return 1
|
|
330
|
-
for h in $heads; do
|
|
331
|
-
git -C "$wt" cat-file -e "$h^{commit}" 2>/dev/null && excl+=("$h")
|
|
332
|
-
done
|
|
333
|
-
if [ "${#excl[@]}" -gt 0 ]; then
|
|
334
|
-
list=$(git -C "$wt" rev-list --abbrev-commit HEAD --not --remotes "${excl[@]}" 2>/dev/null) \
|
|
335
|
-
|| return 1
|
|
336
|
-
else
|
|
337
|
-
# NO MERGED HEAD THIS DESK CONTAINS, and the host still said merged. The
|
|
338
|
-
# subtraction above cannot run, and without it `--not --remotes` reports
|
|
339
|
-
# EVERY commit the branch ever had — so the desk would be held forever
|
|
340
|
-
# for having done the work that merged (#1033).
|
|
341
|
-
#
|
|
342
|
-
# Two ways to reach here, both normal. A SQUASH merge rewrites the
|
|
343
|
-
# commits, so the head the host names exists nowhere in this history —
|
|
344
|
-
# the same property that makes `plot-pr-merged.sh` read `mergedAt` and
|
|
345
|
-
# never ancestry, measured here as ancestry clearing 1 of 29 finished
|
|
346
|
-
# trees against the host's 28. And a host answer that carries no head at
|
|
347
|
-
# all leaves nothing to subtract.
|
|
348
|
-
#
|
|
349
|
-
# THE READING IS PATCH-ID: `git cherry` marks a commit `-` when its
|
|
350
|
-
# change is already upstream and `+` when it is not. A `+` commit that no
|
|
351
|
-
# remote holds is work the merge did not take — a commit made after the
|
|
352
|
-
# merge — and it holds the desk. No clock is consulted: a committer date
|
|
353
|
-
# holds a rebased merged desk forever, and an author date reaps an old
|
|
354
|
-
# patch committed today (#1038). The rule stays in shell because the
|
|
355
|
-
# logic is the prefix test and nothing else; a second conditional here is
|
|
356
|
-
# the signal to move it into the domain with a corpus entry.
|
|
357
|
-
#
|
|
358
|
-
# It fails toward keeping: an unreadable base or a failing `git cherry`
|
|
359
|
-
# returns 1, which the rule reads as `unknown` and refuses on.
|
|
360
|
-
local full cherry
|
|
361
|
-
full=$(git -C "$wt" rev-list HEAD --not --remotes 2>/dev/null) || return 1
|
|
362
|
-
cherry=$(git -C "$wt" cherry "origin/$DEFAULT" HEAD 2>/dev/null) || return 1
|
|
363
|
-
list=""
|
|
364
|
-
for h in $(printf '%s\n' "$cherry" | sed -n 's/^+ //p'); do
|
|
365
|
-
case $'\n'"$full"$'\n' in
|
|
366
|
-
*$'\n'"$h"$'\n'*) list+="$(git -C "$wt" rev-parse --short "$h")"$'\n' ;;
|
|
367
|
-
esac
|
|
368
|
-
done
|
|
369
|
-
list=${list%$'\n'}
|
|
370
|
-
fi
|
|
371
|
-
fi
|
|
372
|
-
printf '%s\n' "$list"
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
# Where the registry lives, resolved through `plot-config.sh` — the SAME key and
|
|
376
|
-
# default the board's reader uses (`resolveManifestDir` in `registry.ts` shells
|
|
377
|
-
# out to exactly this). Two implementations of "where is the registry" is how
|
|
378
|
-
# they drift, so this asks the config rather than hard-coding `.plot/agents`: a
|
|
379
|
-
# project whose board is served from another checkout points the key elsewhere,
|
|
380
|
-
# and a reaper writing to the wrong directory would report success over a
|
|
381
|
-
# manifest the board still renders.
|
|
382
|
-
# Tested with -r, not -x: the helper is invoked through `bash "$CONFIG"`, which
|
|
383
|
-
# needs the file READABLE and not executable. `-x` would silently fall back to
|
|
384
|
-
# the default on a checkout whose exec bits did not survive — and a reaper
|
|
385
|
-
# reading the wrong directory reports success over a manifest the board still
|
|
386
|
-
# renders, which is exactly the failure #420 fixed on the board's own side.
|
|
387
|
-
# The rule that decides whether a worktree may go. Resolved from THIS script's
|
|
388
|
-
# location rather than the cwd, and to a `file://` URL because `import()` needs
|
|
389
|
-
# one for an absolute path. Missing or unreadable, the decision below reports
|
|
390
|
-
# "could not be asked" and keeps every tree.
|
|
391
|
-
RULE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." 2>/dev/null && pwd)/packages/domain/src/rules/reapable.ts"
|
|
392
|
-
RULE="file://$RULE_PATH"
|
|
393
|
-
|
|
394
|
-
CONFIG="$(dirname "${BASH_SOURCE[0]}")/plot-config.sh"
|
|
395
|
-
MANIFEST_DIR=".plot/agents"
|
|
396
|
-
if [ -r "$CONFIG" ]; then
|
|
397
|
-
d=$(bash "$CONFIG" get "Agent registry" ".plot/agents" 2>/dev/null) && [ -n "$d" ] && MANIFEST_DIR="$d"
|
|
398
|
-
fi
|
|
399
|
-
case "$MANIFEST_DIR" in /*) ;; *) MANIFEST_DIR="$ROOT/$MANIFEST_DIR" ;; esac
|
|
400
|
-
|
|
401
|
-
# Where the DESKS and the agent logs live: the desk root, `deskRoot`'s answer
|
|
402
|
-
# asked through `plot-desk-root.sh` — the rule `plot-dispatch.sh` creates desks
|
|
403
|
-
# under and `agentLogDir` writes logs into. One rule, so a reaper cannot sweep a
|
|
404
|
-
# directory the board never writes to and report success over a file that is
|
|
405
|
-
# still there.
|
|
406
|
-
#
|
|
407
|
-
# Two questions read the one answer. `LOG_DIR` is where a finished branch's log
|
|
408
|
-
# files are swept from; `WT_ROOT` is the directory a tree must sit under to be a
|
|
409
|
-
# candidate desk at all. The sweep's filename filter (`branch_log_files`) is
|
|
410
|
-
# unchanged, so what it may delete is still only `plot-<kind>-*` names. The
|
|
411
|
-
# default root is `<repo>/.worktrees`, which Plot owns, so a sibling checkout in
|
|
412
|
-
# the parent is never under it and stays silent.
|
|
413
|
-
#
|
|
414
|
-
# IT IS RESOLVED AGAINST THE MAIN CHECKOUT, NOT `$ROOT`. `git rev-parse
|
|
415
|
-
# --show-toplevel` answers *this* worktree, so a reaper run from inside a desk
|
|
416
|
-
# would resolve `.worktrees` to a directory beneath that desk — which does not
|
|
417
|
-
# exist, so every tree reads as unplaceable-but-elsewhere and the reading is
|
|
418
|
-
# silently empty. Measured 2026-09-10 from inside a desk: `$ROOT/.worktrees`
|
|
419
|
-
# named `.../plot-wt-feature-a-finished-desk-is-a-finding/.worktrees` while
|
|
420
|
-
# every desk sits under `.../plot/.worktrees`. A dispatched agent runs the
|
|
421
|
-
# reaper from exactly there, so the wrong answer would be the usual one.
|
|
422
|
-
#
|
|
423
|
-
# There is NO FALLBACK: a reaper that cannot ask the rule stops rather than
|
|
424
|
-
# compute a second default and delete from it.
|
|
425
|
-
# shellcheck source=plot-desk-root.sh
|
|
426
|
-
. "$(dirname "${BASH_SOURCE[0]}")/plot-desk-root.sh"
|
|
427
|
-
WT_ROOT=$(plot_desk_root "$(plot_repo_root)") || {
|
|
428
|
-
echo "plot-reap: cannot resolve the desk root — nothing was reaped." >&2; exit 2; }
|
|
429
|
-
LOG_DIR="$WT_ROOT"
|
|
430
|
-
|
|
431
|
-
# The files ONE branch's agent run leaves beside its worktree, removed with it.
|
|
432
|
-
#
|
|
433
|
-
# WHICH LOG THIS IS, since the plan says "the dispatcher log" and the estate
|
|
434
|
-
# holds two shapes of one. `plot-resolve-<branch>` is keyed by BRANCH with its
|
|
435
|
-
# slashes flattened (`agentLogPath`'s `resolve` kind), so it maps one-to-one
|
|
436
|
-
# onto the worktree this loop is removing. The artifact repair that wrote it
|
|
437
|
-
# was retired on 2026-10-03; the sweep removes the logs still on disk. `plot-dispatch-<slug>` is keyed by PLAN and opened for
|
|
438
|
-
# APPEND across every dispatch of that plan — `dispatch.ts:150` states it: "a
|
|
439
|
-
# dispatcher log belongs to a plan, a worker log to a branch". Reaping one
|
|
440
|
-
# branch of a five-branch plan must not delete the record the other four are
|
|
441
|
-
# still writing to, so the per-plan log is deliberately NOT swept here. It dies
|
|
442
|
-
# with its plan, which is a different lifetime and so a different question.
|
|
443
|
-
#
|
|
444
|
-
# All three extensions go together. `agent-log.ts` puts the `.state` and
|
|
445
|
-
# `.prompt.md` beside the log precisely so a sweep takes the whole run: one
|
|
446
|
-
# that knew about the log alone would leave half of it behind, which is the
|
|
447
|
-
# accumulation this plan measured rather than a smaller version of it.
|
|
448
|
-
#
|
|
449
|
-
# `rm -f` semantics, and A MISSING LOG IS NOT A REFUSAL. The five refusals
|
|
450
|
-
# guard work that might be lost; a log describes work the host already merged.
|
|
451
|
-
# Not being there is the desired state, so it is reported as nothing at all.
|
|
452
|
-
branch_log_files() { # $1=branch → the paths this branch's run may have left
|
|
453
|
-
local flat=${1//\//-}
|
|
454
|
-
printf '%s\n' \
|
|
455
|
-
"$LOG_DIR/plot-resolve-$flat.log" \
|
|
456
|
-
"$LOG_DIR/plot-resolve-$flat.state" \
|
|
457
|
-
"$LOG_DIR/plot-resolve-$flat.prompt.md"
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
# The ones that are actually there, as a comma-separated list of basenames for
|
|
461
|
-
# the report, or empty. Reading is separated from removing so `--dry-run` can
|
|
462
|
-
# NAME what a real run would take — the plan asks for that by name, and a
|
|
463
|
-
# preview that said "and its log" without checking would promise a file that is
|
|
464
|
-
# not there.
|
|
465
|
-
present_logs() { # $1=branch → "plot-resolve-x.log, plot-resolve-x.state" or ""
|
|
466
|
-
local f out=""
|
|
467
|
-
while IFS= read -r f; do
|
|
468
|
-
[ -f "$f" ] || continue
|
|
469
|
-
out="${out:+$out, }$(basename "$f")"
|
|
470
|
-
done < <(branch_log_files "$1")
|
|
471
|
-
printf '%s' "$out"
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
# The manifest naming a given worktree, or nothing.
|
|
475
|
-
#
|
|
476
|
-
# Manifests are keyed by SESSION id, not by branch, so the file cannot be
|
|
477
|
-
# derived from the worktree path — it is found by reading the `worktree` field
|
|
478
|
-
# out of each one. The match is on the exact recorded path: a prefix match would
|
|
479
|
-
# let `plot-wt-foo` claim `plot-wt-foo-bar`'s manifest.
|
|
480
|
-
#
|
|
481
|
-
# Parsed with `sed`, not a JSON reader, deliberately — this script must run
|
|
482
|
-
# where node does not, and the field it needs is one flat string written by the
|
|
483
|
-
# dispatcher. A manifest whose `worktree` cannot be read simply does not match,
|
|
484
|
-
# which keeps an unparseable file OUT of the removal set rather than in it.
|
|
485
|
-
# A path with its symlinks resolved, or the path unchanged when it does not
|
|
486
|
-
# exist (nothing to resolve, and the caller still needs a string to compare).
|
|
487
|
-
#
|
|
488
|
-
# NOT cosmetic. `git worktree list` reports RESOLVED paths, while a manifest
|
|
489
|
-
# records whatever the dispatcher was handed — and on macOS `/tmp`, `/var` and
|
|
490
|
-
# `/etc` are symlinks into `/private`, so the same directory arrives as two
|
|
491
|
-
# different strings. Measured while writing this: a worktree git called
|
|
492
|
-
# `/private/var/.../repo` against a manifest saying `/var/.../repo`, matching
|
|
493
|
-
# nothing and stranding the manifest the reap was supposed to take.
|
|
494
|
-
canonical() {
|
|
495
|
-
local p="$1"
|
|
496
|
-
[ -n "$p" ] || return 0
|
|
497
|
-
# Resolve through the filesystem while the directory is still there — the
|
|
498
|
-
# authoritative answer, and the only one that handles an arbitrary symlink.
|
|
499
|
-
if [ -d "$p" ]; then
|
|
500
|
-
p=$( (cd "$p" 2>/dev/null && pwd -P) || printf '%s' "$p" )
|
|
501
|
-
fi
|
|
502
|
-
# Then normalise the macOS `/private` prefix TEXTUALLY, because the manifest
|
|
503
|
-
# side is compared AFTER its directory has been removed and there is no
|
|
504
|
-
# longer anything to resolve. `/tmp`, `/var` and `/etc` are symlinks into
|
|
505
|
-
# `/private`, so git's `/private/var/...` and a manifest's `/var/...` name
|
|
506
|
-
# one directory; stripping the prefix from both makes them one string
|
|
507
|
-
# whether or not either still exists.
|
|
508
|
-
case "$p" in
|
|
509
|
-
/private/tmp/*|/private/var/*|/private/etc/*) p=${p#/private} ;;
|
|
510
|
-
esac
|
|
511
|
-
printf '%s\n' "$p"
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
manifest_for() {
|
|
515
|
-
local target="$1" f wt
|
|
516
|
-
[ -d "$MANIFEST_DIR" ] || return 1
|
|
517
|
-
target=$(canonical "$target")
|
|
518
|
-
for f in "$MANIFEST_DIR"/*.json; do
|
|
519
|
-
[ -f "$f" ] || continue
|
|
520
|
-
wt=$(sed -n 's/.*"worktree"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$f" | head -1)
|
|
521
|
-
[ -n "$wt" ] || continue
|
|
522
|
-
[ "$(canonical "$wt")" = "$target" ] && { printf '%s\n' "$f"; return 0; }
|
|
523
|
-
done
|
|
524
|
-
return 1
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
reap=0; kept=0; removed=0; cleared=0; vanished=0; unplaced=0
|
|
528
|
-
printf '%-8s %-52s %s\n' "verdict" "branch" "why"
|
|
529
|
-
|
|
530
|
-
while IFS=$'\037' read -r wt br prunable; do
|
|
531
|
-
[ -n "$wt" ] || continue
|
|
532
|
-
short=${br#refs/heads/}
|
|
533
|
-
[ "$wt" = "$ROOT" ] && continue
|
|
534
|
-
|
|
535
|
-
# WHAT THE REPORT CALLS THIS TREE. `short` is the branch and stays empty for
|
|
536
|
-
# a detached desk, which is the correct reading and a useless label — an
|
|
537
|
-
# operator reading a blank column cannot tell which of thirteen it names.
|
|
538
|
-
# The directory is what identifies such a tree, so the report says so and
|
|
539
|
-
# every refusal still measures `short`.
|
|
540
|
-
label=$short
|
|
541
|
-
[ -z "$label" ] && label="(detached) $(basename "$wt")"
|
|
542
|
-
|
|
543
|
-
# 4a. GIT'S OWN ANSWER THAT THE DIRECTORY IS GONE, and it is a REPORT rather
|
|
544
|
-
# than a sixth refusal. The five below each say *do not remove this* and
|
|
545
|
-
# send an operator to look; this says *there is nothing to remove and the
|
|
546
|
-
# entry is stale*, and names the repair. Blurring the two would tell
|
|
547
|
-
# somebody to go and inspect a directory that is not there.
|
|
548
|
-
#
|
|
549
|
-
# IT IS ASKED FIRST because it is the PRIOR question. Four of the five
|
|
550
|
-
# refusals measure something inside the tree — a pid file, a marker, the
|
|
551
|
-
# porcelain status, the checked-out branch — and every one of them is
|
|
552
|
-
# unanswerable here. `rules/reapable.ts` says the same thing in the
|
|
553
|
-
# domain: a `vanished` tree makes those four conditions `unknown`.
|
|
554
|
-
#
|
|
555
|
-
# AND IT IS ASKED BEFORE THE DISPATCH-TREE FILTER, which would otherwise
|
|
556
|
-
# hide exactly this population. That filter accepts a tree by its
|
|
557
|
-
# `.plot-worker.pid` file or its legacy `plot-wt-` path — and a vanished
|
|
558
|
-
# desk under `Worktree root` has neither, the pid file having gone with
|
|
559
|
-
# the directory. Measured 2026-09-06: 3 of 20 worktrees here were
|
|
560
|
-
# prunable and the reaper reported none of them.
|
|
561
|
-
#
|
|
562
|
-
# NOTHING IS PRUNED ON THIS PATH. The reading came from the listing the
|
|
563
|
-
# loop already makes, no `git` call was added for it, and whether to
|
|
564
|
-
# prune stays the operator's decision — the same discipline that makes
|
|
565
|
-
# every refusal a measurement rather than an act.
|
|
566
|
-
if [ "$prunable" = "yes" ]; then
|
|
567
|
-
printf '%-8s %-52s %s\n' "vanished" "$label" "directory gone — 'git worktree prune' clears the entry"
|
|
568
|
-
vanished=$((vanished+1)); continue
|
|
569
|
-
fi
|
|
570
|
-
|
|
571
|
-
# 5. Only dispatch trees. A hand-made worktree and the main checkout are not
|
|
572
|
-
# this script's to remove, whatever state they are in.
|
|
573
|
-
#
|
|
574
|
-
# ASKED OF THE DISK, NOT OF THE PATH. `.plot-worker.pid` is written by the
|
|
575
|
-
# dispatcher at creation, so it is a marker Plot itself left rather than a
|
|
576
|
-
# name Plot hopes was used. The path test alone recognised only the LEGACY
|
|
577
|
-
# `plot-wt-` layout, which `plot-dispatch.sh:129` uses when `Worktree root`
|
|
578
|
-
# is absent — so on this repo, which sets `Worktree root: .worktrees`,
|
|
579
|
-
# every tree is `.worktrees/<branch-with-dashes>` and matched nothing.
|
|
580
|
-
# Measured 2026-08-30: nine dispatch trees, `kept=0` rather than `kept=9`.
|
|
581
|
-
# A refusal counts and a skip does not, so `reapable=0` read as *nothing to
|
|
582
|
-
# clean* and meant *nothing was looked at*.
|
|
583
|
-
#
|
|
584
|
-
# Both signals are accepted and neither is in transition: `plot-wt-` is
|
|
585
|
-
# supported permanently, and a legacy tree predating the marker keeps
|
|
586
|
-
# being recognised by its name. A dispatch tree whose pid file was deleted
|
|
587
|
-
# and whose path does not match goes unrecognised — which fails by
|
|
588
|
-
# REFUSING, the same safe direction the path test failed in, and for one
|
|
589
|
-
# tree instead of all of them.
|
|
590
|
-
#
|
|
591
|
-
# THE REFUSAL IS NOW MEASURED RATHER THAN PERFORMED, and that is this
|
|
592
|
-
# slice's whole subject. The two tests below produce READINGS; nothing
|
|
593
|
-
# here decides what the readings mean. A `case` deciding it is where the
|
|
594
|
-
# 2026-08-30 defect lived — one line, in shell, that no test could reach,
|
|
595
|
-
# shadowing `ReapEvidence.isDispatchTree`, declared for the same question.
|
|
596
|
-
#
|
|
597
|
-
# `is_dispatch_tree` is UNCHANGED in strictness. Widening it would move
|
|
598
|
-
# trees into the reaper's population, which is a blast radius, not a
|
|
599
|
-
# report — and turning a person's checkout into a removal instruction is
|
|
600
|
-
# worse than the silence being fixed.
|
|
601
|
-
#
|
|
602
|
-
# `unclassified` is the reading that was missing: a tree sitting under
|
|
603
|
-
# the configured `Worktree root` that neither test placed. It looks like a
|
|
604
|
-
# desk by location and is recognised as one by nothing, so a person is the
|
|
605
|
-
# only thing that can say. Measured 2026-09-10 on this estate:
|
|
606
|
-
# `.worktrees/feature-one-monitor-watches-the-slice`, 5 unpushed commits,
|
|
607
|
-
# a PLOT-BLOCKED marker and no PR, reported by nothing.
|
|
608
|
-
is_dispatch_tree=false
|
|
609
|
-
if [ -f "$wt/.plot-worker.pid" ]; then
|
|
610
|
-
is_dispatch_tree=true
|
|
611
|
-
else
|
|
612
|
-
case "$wt" in *"/plot-wt-"*) is_dispatch_tree=true ;; esac
|
|
613
|
-
fi
|
|
614
|
-
|
|
615
|
-
unclassified=false
|
|
616
|
-
if [ "$is_dispatch_tree" = false ] && [ -n "$WT_ROOT" ]; then
|
|
617
|
-
case "$wt" in "$WT_ROOT"/*) unclassified=true ;; esac
|
|
618
|
-
fi
|
|
619
|
-
|
|
620
|
-
# A tree that is NEITHER is silent, and that silence is correct: it is a
|
|
621
|
-
# hand-made checkout outside the population, not a tree that failed a test.
|
|
622
|
-
# Measured here — four `/tmp` baseline and scratchpad checkouts, each of
|
|
623
|
-
# which carries a `.plot/` directory because the repo tracks one, which is
|
|
624
|
-
# why the presence of `.plot/` cannot be the test.
|
|
625
|
-
if [ "$is_dispatch_tree" = false ] && [ "$unclassified" = false ]; then
|
|
626
|
-
continue
|
|
627
|
-
fi
|
|
628
|
-
|
|
629
|
-
# Reported and NOT judged. Every reading below measures something inside a
|
|
630
|
-
# desk, and this is a tree nothing has established is one — so the loop says
|
|
631
|
-
# what it found and moves on, rather than asking the rule a question about a
|
|
632
|
-
# population the rule excludes. It counts, because a refusal that counts is
|
|
633
|
-
# the difference between *nothing to clean* and *nothing was looked at*.
|
|
634
|
-
if [ "$unclassified" = true ]; then
|
|
635
|
-
printf '%-8s %-52s %s\n' "unknown" "$label" \
|
|
636
|
-
"under $(basename "$WT_ROOT")/, no worker pid and no recognised name — needs a person"
|
|
637
|
-
unplaced=$((unplaced+1)); continue
|
|
638
|
-
fi
|
|
639
|
-
|
|
640
|
-
# THE READINGS. Everything from here to the rule call MEASURES; nothing
|
|
641
|
-
# decides. Each of the four sources that can answer is read once, into a
|
|
642
|
-
# variable named for what it holds rather than for the verdict it implies.
|
|
643
|
-
|
|
644
|
-
# The process table: the live worker's pid, or empty. Read but not judged —
|
|
645
|
-
# an empty pid file is not a live process, and which of those two it is is
|
|
646
|
-
# the rule's to say. "Live" means an agent runs under the recorded pid, the
|
|
647
|
-
# answer `--stop` reads; a wrapper whose agent exited is not live.
|
|
648
|
-
pid=$(desk_worker_pid "$wt")
|
|
649
|
-
|
|
650
|
-
# The tree: a PLOT-BLOCKED marker, and the first uncommitted path.
|
|
651
|
-
#
|
|
652
|
-
# `desk_dirt` excuses the paths the estate writes itself — the tiny-garden
|
|
653
|
-
# pulse and a root `PLOT-CORRECTION.md` — and reports every OTHER dirty path,
|
|
654
|
-
# which keeps this an exception rather than a hole. It is filtered HERE, in
|
|
655
|
-
# the reading, because those are facts about files this estate writes and
|
|
656
|
-
# not about whether a worktree may go. The filter runs before `head`, so a
|
|
657
|
-
# correction beside a real file names the real file.
|
|
658
|
-
marker=false
|
|
659
|
-
marker_file=$(ls "$wt"/PLOT-BLOCKED* 2>/dev/null | head -1)
|
|
660
|
-
[ -n "$marker_file" ] && marker=true
|
|
661
|
-
# The marker's own line is excluded from `dirty`, which otherwise answers
|
|
662
|
-
# `uncommitted-changes` for a desk this slice reads `refused-empty` instead:
|
|
663
|
-
# the marker is a question for a person, not unlanded work, and the two
|
|
664
|
-
# refusals must not both fire for the one line that names it.
|
|
665
|
-
dirty=$(desk_dirt "$wt" | grep -v 'PLOT-BLOCKED' | head -1)
|
|
666
|
-
|
|
667
|
-
# markerRecordsWork — does the desk hold anything besides its marker: a
|
|
668
|
-
# dirty path that is not the marker itself (`$dirty`, already filtered
|
|
669
|
-
# above), or a file-changing commit beyond the default branch. Read only
|
|
670
|
-
# when a marker is present — `firstReapRefusal` does not consult this field
|
|
671
|
-
# otherwise.
|
|
672
|
-
marker_records_work=false
|
|
673
|
-
if [ "$marker" = true ]; then
|
|
674
|
-
if [ -n "$dirty" ] || [ "$(real_commits "$wt" "origin/$DEFAULT..HEAD")" != 0 ]; then
|
|
675
|
-
marker_records_work=true
|
|
676
|
-
fi
|
|
677
|
-
fi
|
|
678
|
-
|
|
679
|
-
# The host: whether ANY PR for this branch merged.
|
|
680
|
-
#
|
|
681
|
-
# `merged` never `state` — a merged PR reports CLOSED — and ANY PR never the
|
|
682
|
-
# newest, both of which `plot-pr-merged.sh` carries. Ancestry is consulted
|
|
683
|
-
# FIRST only because it needs no network; it can only ever ADD a merged
|
|
684
|
-
# answer, never withhold one, since a squash-merge leaves the branch
|
|
685
|
-
# permanently ahead and falls through to the host.
|
|
686
|
-
#
|
|
687
|
-
# `unreachable` is not distinguished from `not-merged` here: `pr_merged`
|
|
688
|
-
# returns one exit code for both, deliberately, since both must keep the
|
|
689
|
-
# tree. The rule accepts the distinction so a fixture can trigger it; this
|
|
690
|
-
# reading simply cannot supply it, and reporting the stronger claim would be
|
|
691
|
-
# a lie about what was measured.
|
|
692
|
-
merge=not-merged; why=""
|
|
693
|
-
if [ -n "$short" ] && [ "$(git -C "$wt" rev-list --count "origin/$DEFAULT..$short" 2>/dev/null || echo 1)" = "0" ]; then
|
|
694
|
-
merge=merged; why="merged into $DEFAULT"
|
|
695
|
-
elif [ -n "$short" ] && pr_merged "$short"; then
|
|
696
|
-
merge=merged; why="PR merged (squash)"
|
|
697
|
-
elif [ -z "$short" ] \
|
|
698
|
-
&& [ "$(git -C "$wt" rev-list --count "origin/$DEFAULT..HEAD" 2>/dev/null || echo 1)" = "0" ]; then
|
|
699
|
-
# A DETACHED DESK HAS NOTHING TO LAND, and that is a measurement rather
|
|
700
|
-
# than a weakened refusal. `plot-dispatch.sh --start` cuts a free agent's
|
|
701
|
-
# desk detached at `origin/<main>` because a free agent holds no slice, so
|
|
702
|
-
# such a tree never had a branch and can never have a PR. Reading it
|
|
703
|
-
# through `no-merged-pr` would refuse every one of them forever — the same
|
|
704
|
-
# silence this slice removes, in the opposite direction.
|
|
705
|
-
#
|
|
706
|
-
# THE READING IS `HEAD` AGAINST THE DEFAULT BRANCH, never the desk's name.
|
|
707
|
-
# Measured 2026-09-22: three `free-*` desks hold a branch and two carry
|
|
708
|
-
# live workers (pids 27820, 6542), so a prefix-keyed test reaps a running
|
|
709
|
-
# agent. Detachment is the condition; the name is not.
|
|
710
|
-
#
|
|
711
|
-
# A detached desk carrying commits falls through to `not-merged` and is
|
|
712
|
-
# kept, which is correct: somebody committed there and nothing says the
|
|
713
|
-
# work landed. The live-pid, marker and dirty refusals are all asked
|
|
714
|
-
# before this and are unaffected.
|
|
715
|
-
merge=merged; why="detached, nothing to land"
|
|
716
|
-
fi
|
|
717
|
-
|
|
718
|
-
# The desk again: commits only this checkout holds. Taken AFTER the merge
|
|
719
|
-
# reading, because the head the host merged is what separates a pushed
|
|
720
|
-
# commit from an unpushed one once the remote ref is gone. Lines, or the
|
|
721
|
-
# word `unknown` when they could not be counted.
|
|
722
|
-
if unpushed=$(desk_unpushed "$wt" "$short" "$merge"); then :; else unpushed=unknown; fi
|
|
723
|
-
|
|
724
|
-
# THE DECISION. One call, and the script holds no `if` about whether a
|
|
725
|
-
# worktree may go — only about what to do with the answer.
|
|
726
|
-
#
|
|
727
|
-
# The rule is `packages/domain/src/rules/reapable.ts`, imported directly:
|
|
728
|
-
# node 24 strips the types, so there is no build step between this script and
|
|
729
|
-
# the decision it asks for. The same shape, and the same reason, as
|
|
730
|
-
# `scripts/check-changeset-packages.sh` — the JS arrives on STDIN from a
|
|
731
|
-
# QUOTED heredoc so the shell expands none of it.
|
|
732
|
-
#
|
|
733
|
-
# THIS SCRIPT NOW NEEDS NODE, and its header said it must run where node does
|
|
734
|
-
# not. That constraint is retired rather than quietly broken: the alternative
|
|
735
|
-
# is a second implementation of the five refusals living in shell where
|
|
736
|
-
# nothing can test it, and a copy that drifted toward permissive would delete
|
|
737
|
-
# work. `manifest_for` keeps its `sed` parser, which is a different question
|
|
738
|
-
# — reading one flat string out of a file Plot wrote, not deciding anything.
|
|
739
|
-
#
|
|
740
|
-
# A rule that cannot be asked REFUSES: `node` missing, the import failing,
|
|
741
|
-
# the module throwing all leave `verdict` empty, and an empty verdict keeps
|
|
742
|
-
# the tree and says why. Silence is never permission, on this path either.
|
|
743
|
-
verdict=$(PLOT_BRANCH="$short" PLOT_DEFAULT="$DEFAULT" PLOT_PID="$pid" \
|
|
744
|
-
PLOT_DIRTY="$dirty" PLOT_MARKER="$marker" PLOT_MERGE="$merge" \
|
|
745
|
-
PLOT_UNPUSHED="$unpushed" PLOT_RULE="$RULE" \
|
|
746
|
-
PLOT_MARKER_RECORDS_WORK="$marker_records_work" \
|
|
747
|
-
node --input-type=module - <<'NODE_EOF' 2>/dev/null
|
|
748
|
-
// Imported from an ABSOLUTE path derived from this script, never from the
|
|
749
|
-
// cwd. The reaper runs with its cwd wherever the operator invoked it and the
|
|
750
|
-
// reconcile suite runs it against sandbox repos in the temp directory, so a
|
|
751
|
-
// relative specifier resolves to a `packages/` that is not there — which the
|
|
752
|
-
// fail-safe turns into "rule could not be asked" and every tree kept. Correct
|
|
753
|
-
// direction, useless reaper. Same discipline as `plot-host.sh`,
|
|
754
|
-
// `plot-config.sh` and `plot-pr-merged.sh`, which are all found via
|
|
755
|
-
// `BASH_SOURCE`.
|
|
756
|
-
const { firstReapRefusal } = await import(process.env.PLOT_RULE);
|
|
757
|
-
|
|
758
|
-
const problem = firstReapRefusal({
|
|
759
|
-
branch: process.env.PLOT_BRANCH,
|
|
760
|
-
defaultBranch: process.env.PLOT_DEFAULT,
|
|
761
|
-
// The main checkout is excluded before the loop reaches here, so the only
|
|
762
|
-
// way this reading is true is the branch test the rule makes anyway.
|
|
763
|
-
isMain: false,
|
|
764
|
-
workerPid: process.env.PLOT_PID === "" ? null : process.env.PLOT_PID,
|
|
765
|
-
dirtyPath: process.env.PLOT_DIRTY,
|
|
766
|
-
blockedMarker: process.env.PLOT_MARKER === "true",
|
|
767
|
-
markerRecordsWork: process.env.PLOT_MARKER_RECORDS_WORK === "true",
|
|
768
|
-
merge: process.env.PLOT_MERGE,
|
|
769
|
-
unpushed: process.env.PLOT_UNPUSHED === "unknown"
|
|
770
|
-
? "unknown"
|
|
771
|
-
: process.env.PLOT_UNPUSHED.split("\n").filter((l) => l !== ""),
|
|
772
|
-
});
|
|
773
|
-
|
|
774
|
-
// `reap` when nothing refused; otherwise the refusal and its reading, which
|
|
775
|
-
// the shell renders into the prose an operator reads.
|
|
776
|
-
process.stdout.write(problem === null ? "reap\t" : `${problem.refusal}\t${problem.detail}`);
|
|
777
|
-
NODE_EOF
|
|
778
|
-
)
|
|
779
|
-
|
|
780
|
-
refusal=${verdict%%$'\t'*}
|
|
781
|
-
detail=${verdict#*$'\t'}
|
|
782
|
-
|
|
783
|
-
# RENDERING, not deciding. The rule named the measurement; this names what it
|
|
784
|
-
# means to someone reading the report, which is the caller's half because
|
|
785
|
-
# only the caller knows it is printing a table.
|
|
786
|
-
if [ "$refusal" != "reap" ]; then
|
|
787
|
-
case "$refusal" in
|
|
788
|
-
live-worker) reason="worker alive (pid $detail)" ;;
|
|
789
|
-
blocked-marker) reason="PLOT-BLOCKED marker — needs a person" ;;
|
|
790
|
-
uncommitted-changes) reason="uncommitted: ${detail:0:40}" ;;
|
|
791
|
-
unpushed-commits) reason="unpushed commits: ${detail:0:40}" ;;
|
|
792
|
-
on-default-branch) reason="on $DEFAULT — dispatched branch not checked out" ;;
|
|
793
|
-
no-merged-pr) reason="unlanded work — no merged PR" ;;
|
|
794
|
-
*) reason="rule could not be asked — keeping" ;;
|
|
795
|
-
esac
|
|
796
|
-
printf '%-8s %-52s %s\n' "keep" "$label" "$reason"; kept=$((kept+1)); continue
|
|
797
|
-
fi
|
|
798
|
-
|
|
799
|
-
if [ "$MAX" -gt 0 ] && [ "$reap" -ge "$MAX" ]; then
|
|
800
|
-
printf '%-8s %-52s %s\n' "keep" "$label" "--max $MAX reached"; kept=$((kept+1)); continue
|
|
801
|
-
fi
|
|
802
|
-
|
|
803
|
-
# Resolved BEFORE the removal, because `canonical` needs the directory to
|
|
804
|
-
# still exist to resolve it. After `git worktree remove` there is nothing to
|
|
805
|
-
# follow, and the manifest's spelling would never converge with git's.
|
|
806
|
-
wt_real=$(canonical "$wt")
|
|
807
|
-
|
|
808
|
-
# Read BEFORE the removal for the same reason `wt_real` is: the report names
|
|
809
|
-
# what was there when the run decided, and a dry run must name exactly what a
|
|
810
|
-
# real run would take. Empty when the branch left no log, which is silent —
|
|
811
|
-
# a missing log is the desired state, not an event.
|
|
812
|
-
logs=$(present_logs "$short")
|
|
813
|
-
|
|
814
|
-
# THE MARKER MOVES, IT IS NOT DROPPED. A tree that reached `reap` with
|
|
815
|
-
# `marker=true` passed `firstReapRefusal` only because `markerRecordsWork`
|
|
816
|
-
# was measured false — `rules/desk-lifecycle.ts`'s `refused-empty`. The
|
|
817
|
-
# marker's text is evidence of a question somebody asked even though the
|
|
818
|
-
# desk holding it is otherwise empty, so it is copied to REFUSALS_LOG before
|
|
819
|
-
# the worktree goes. A FAILED COPY KEEPS THE DESK: the marker is the only
|
|
820
|
-
# record of the question, and a reap that lost both would make the evidence
|
|
821
|
-
# exactly as unrecoverable as the desk it lived in.
|
|
822
|
-
if [ "$DRY" -eq 0 ] && [ "$marker" = true ] && [ -n "$marker_file" ]; then
|
|
823
|
-
refusals_log="$ROOT/.plot/state/refusals.tsv"
|
|
824
|
-
if mkdir -p "$(dirname "$refusals_log")" 2>/dev/null \
|
|
825
|
-
&& printf '%s\t%s\t%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$label" \
|
|
826
|
-
"$(tr '\n\t' ' ' < "$marker_file" 2>/dev/null)" >> "$refusals_log" 2>/dev/null; then
|
|
827
|
-
why="$why, marker saved to .plot/state/refusals.tsv"
|
|
828
|
-
else
|
|
829
|
-
printf '%-8s %-52s %s\n' "keep" "$label" \
|
|
830
|
-
"could not save PLOT-BLOCKED text to .plot/state/refusals.tsv — desk kept"
|
|
831
|
-
kept=$((kept+1)); continue
|
|
832
|
-
fi
|
|
833
|
-
fi
|
|
834
|
-
|
|
835
|
-
reap=$((reap+1))
|
|
836
|
-
if [ "$DRY" -eq 1 ]; then
|
|
837
|
-
printf '%-8s %-52s %s\n' "would" "$label" "$why${logs:+, log $logs}"
|
|
838
|
-
else
|
|
839
|
-
if git worktree remove --force "$wt" 2>/dev/null; then
|
|
840
|
-
# The worktree is gone; NOW the manifest may go. Inside the success arm
|
|
841
|
-
# and nowhere else — a manifest removed before a removal that then
|
|
842
|
-
# refuses leaves a live worktree unregistered, which the registry answers
|
|
843
|
-
# by synthesizing an `unknown` row. Failing this way round strands a
|
|
844
|
-
# manifest instead, which the sweep below clears.
|
|
845
|
-
if m=$(manifest_for "$wt_real"); then
|
|
846
|
-
rm -f "$m" && why="$why, manifest cleared"
|
|
847
|
-
fi
|
|
848
|
-
# AND THE LOG LAST, because it is the only one that is pure cleanup. A
|
|
849
|
-
# missing manifest orphans an agent and a missing worktree loses a desk;
|
|
850
|
-
# a missing log costs a record of work the host already merged. So it
|
|
851
|
-
# goes where a failure before it has cost the least, and its own failure
|
|
852
|
-
# costs nothing at all.
|
|
853
|
-
#
|
|
854
|
-
# Inside the success arm with the manifest: a log describes the worktree,
|
|
855
|
-
# so a removal that refused must keep it — an operator sent to look at a
|
|
856
|
-
# tree that survived needs the words explaining why it is there.
|
|
857
|
-
#
|
|
858
|
-
# `rm -f` and the result ignored. Not being there is the desired state,
|
|
859
|
-
# and a log that cannot be unlinked is not a reason to report a reap that
|
|
860
|
-
# happened as one that did not.
|
|
861
|
-
if [ -n "$logs" ]; then
|
|
862
|
-
while IFS= read -r f; do rm -f "$f" 2>/dev/null; done < <(branch_log_files "$short")
|
|
863
|
-
why="$why, log removed"
|
|
864
|
-
fi
|
|
865
|
-
printf '%-8s %-52s %s\n' "reaped" "$label" "$why"; removed=$((removed+1))
|
|
866
|
-
else
|
|
867
|
-
printf '%-8s %-52s %s\n' "FAILED" "$label" "git worktree remove refused"; kept=$((kept+1))
|
|
868
|
-
fi
|
|
869
|
-
fi
|
|
870
|
-
done < <(git worktree list --porcelain \
|
|
871
|
-
| awk -v OFS="\037" '/^worktree /{ if (p != "") print p, br, pr; p=$2; br=""; pr="no"; next }
|
|
872
|
-
/^branch / { br=$2; next }
|
|
873
|
-
/^prunable/ { pr="yes"; next }
|
|
874
|
-
END { if (p != "") print p, br, pr }')
|
|
875
|
-
|
|
876
|
-
[ "$DRY" -eq 0 ] && git worktree prune 2>/dev/null
|
|
877
|
-
|
|
878
|
-
# The manifests whose worktree is ALREADY gone.
|
|
879
|
-
#
|
|
880
|
-
# Every reap before this script learned about the registry left one, and the
|
|
881
|
-
# board renders each as an `unknown` row naming a directory that does not
|
|
882
|
-
# exist. They are the population this plan was written from — seven of them,
|
|
883
|
-
# measured 2026-08-26 — and a fix that only stops NEW ones leaves those on the
|
|
884
|
-
# board forever.
|
|
885
|
-
#
|
|
886
|
-
# The predicate is the same one the loop above satisfies by construction: the
|
|
887
|
-
# recorded worktree is not there. It needs no PR check and no liveness check —
|
|
888
|
-
# nothing runs in a directory that does not exist, which is the strongest
|
|
889
|
-
# evidence of "dead" available, not the weakest.
|
|
890
|
-
#
|
|
891
|
-
# A manifest recording NO worktree path is left alone: it names an agent
|
|
892
|
-
# between checkouts, and absence of a path is not absence of an agent.
|
|
893
|
-
if [ -d "$MANIFEST_DIR" ]; then
|
|
894
|
-
for m in "$MANIFEST_DIR"/*.json; do
|
|
895
|
-
[ -f "$m" ] || continue
|
|
896
|
-
mwt=$(sed -n 's/.*"worktree"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$m" | head -1)
|
|
897
|
-
[ -n "$mwt" ] || continue
|
|
898
|
-
[ -d "$mwt" ] && continue
|
|
899
|
-
cleared=$((cleared+1))
|
|
900
|
-
if [ "$DRY" -eq 1 ]; then
|
|
901
|
-
printf '%-8s %-52s %s\n' "would" "$(basename "${mwt}")" "orphaned manifest — worktree absent"
|
|
902
|
-
else
|
|
903
|
-
rm -f "$m"
|
|
904
|
-
printf '%-8s %-52s %s\n' "cleared" "$(basename "${mwt}")" "orphaned manifest — worktree absent"
|
|
905
|
-
fi
|
|
906
|
-
done
|
|
907
|
-
fi
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
# ===========================================================================
|
|
911
|
-
# THE OTHER THREE KINDS OF LEFTOVER.
|
|
912
|
-
#
|
|
913
|
-
# The sweep answers ONE question — is anything here that nobody is coming back
|
|
914
|
-
# for? — and it does not care whether the cause was a dead agent, an
|
|
915
|
-
# interrupted dispatch, a `--stop`, or a merge somebody did on the host.
|
|
916
|
-
# Everything above answers it about WORKTREES. Measured 2026-09-02 on this
|
|
917
|
-
# estate, three more populations answer it and nothing looks at them:
|
|
918
|
-
#
|
|
919
|
-
# local branches 85 of 98 already merged ← the largest, swept below
|
|
920
|
-
# orphaned claim refs a claim whose agent never existed
|
|
921
|
-
# dirty trees nobody owns 2 desks, 52 and 1 files ← reported, never deleted
|
|
922
|
-
#
|
|
923
|
-
# EVERY KIND KEEPS THE SHAPE THIS SCRIPT ALREADY HAS: `--dry-run` by default,
|
|
924
|
-
# acting on `--yes`, bounded by `--max N`. The bound is per kind, because the
|
|
925
|
-
# kinds are different acts on different populations — a run bounded to five
|
|
926
|
-
# worktrees has not therefore been asked to leave the 85th branch alone.
|
|
927
|
-
#
|
|
928
|
-
# AND THE DECIDING IS NOT HERE EITHER. Each kind gathers readings and asks
|
|
929
|
-
# `packages/domain/src/rules/sweepable.ts`, exactly as the worktree loop asks
|
|
930
|
-
# `reapable.ts`. `reapable.ts` is UNTOUCHED: its five refusals were written for
|
|
931
|
-
# the population it sweeps, and a backstop that guesses is worse than none.
|
|
932
|
-
# A rule that cannot be asked REFUSES, so a missing `node` sweeps nothing.
|
|
933
|
-
# ===========================================================================
|
|
934
|
-
|
|
935
|
-
SWEEP_RULE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." 2>/dev/null && pwd)/packages/domain/src/rules/sweepable.ts"
|
|
936
|
-
SWEEP_RULE="file://$SWEEP_RULE_PATH"
|
|
937
|
-
|
|
938
|
-
swept_branches=0; deleted_branches=0; kept_branches=0
|
|
939
|
-
swept_claims=0; deleted_claims=0; kept_claims=0
|
|
940
|
-
dirty_trees=0
|
|
941
|
-
|
|
942
|
-
# ---------------------------------------------------------------------------
|
|
943
|
-
# KIND 2: LOCAL BRANCHES.
|
|
944
|
-
#
|
|
945
|
-
# THE GATE IS THE REAPER'S, NOT GIT'S:
|
|
946
|
-
#
|
|
947
|
-
# the host says merged, AND no worktree holds it → delete
|
|
948
|
-
#
|
|
949
|
-
# `git branch -d` is NOT the gate. It refuses an unmerged branch, which sounds
|
|
950
|
-
# like the safety this needs — except squash-merge leaves a branch permanently
|
|
951
|
-
# ahead of main, the trap `plot-pr-merged.sh` exists for, so `-d` would refuse
|
|
952
|
-
# all 85 for the wrong reason. This uses `-D` and puts the safety in the two
|
|
953
|
-
# measurements above it, where a test can reach it.
|
|
954
|
-
#
|
|
955
|
-
# WHY A LOCAL BRANCH JOINS THE ESTATE-WIDE SIDE. The asymmetry between kinds is
|
|
956
|
-
# deliberate: a removed checkout comes back with `git worktree add`, a deleted
|
|
957
|
-
# REMOTE ref does not, so `plot-release-refs.sh` stays plan-scoped. A local
|
|
958
|
-
# branch whose PR merged is re-fetchable from origin — it is A copy, not THE
|
|
959
|
-
# copy — so the argument that protects remote refs does not transfer, and this
|
|
960
|
-
# sweeps estate-wide like the worktrees do.
|
|
961
|
-
#
|
|
962
|
-
# REPORT-ONLY WAS THE ALTERNATIVE, AND 85 ROWS IS THE ARGUMENT AGAINST IT. A
|
|
963
|
-
# sweep that reports and never acts becomes one more thing a person has to
|
|
964
|
-
# clear — the problem this plan exists to remove, reintroduced one level up.
|
|
965
|
-
# ---------------------------------------------------------------------------
|
|
966
|
-
|
|
967
|
-
# Every branch checked out ANYWHERE, for the second half of the gate.
|
|
968
|
-
#
|
|
969
|
-
# Read once before the loop, the same reading and the same reason as
|
|
970
|
-
# `plot-release-refs.sh:126` — that file's guard 4 collects exactly this, and
|
|
971
|
-
# the brief asks for it to be reused rather than written twice. A worktree
|
|
972
|
-
# created mid-run holds a branch this run has not yet reached, and the next run
|
|
973
|
-
# sees it.
|
|
974
|
-
sweep_checked_out=$(git worktree list --porcelain 2>/dev/null \
|
|
975
|
-
| sed -n 's|^branch refs/heads/||p')
|
|
976
|
-
|
|
977
|
-
sweep_is_checked_out() {
|
|
978
|
-
printf '%s\n' "$sweep_checked_out" | grep -qxF "$1"
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
# Ask the rule about one branch. Empty output means the rule could not be
|
|
982
|
-
# asked, which the caller renders as a refusal — silence is never permission.
|
|
983
|
-
sweep_branch_verdict() { # $1=branch $2=merged(true/false) $3=checked_out(true/false)
|
|
984
|
-
PLOT_BRANCH="$1" PLOT_DEFAULT="$DEFAULT" PLOT_MERGED="$2" PLOT_CHECKED="$3" \
|
|
985
|
-
PLOT_RULE="$SWEEP_RULE" \
|
|
986
|
-
node --input-type=module - <<'NODE_EOF' 2>/dev/null
|
|
987
|
-
const { firstBranchRefusal } = await import(process.env.PLOT_RULE);
|
|
988
|
-
const refusal = firstBranchRefusal({
|
|
989
|
-
branch: process.env.PLOT_BRANCH,
|
|
990
|
-
defaultBranch: process.env.PLOT_DEFAULT,
|
|
991
|
-
hasMergedPr: process.env.PLOT_MERGED === "true",
|
|
992
|
-
checkedOut: process.env.PLOT_CHECKED === "true",
|
|
993
|
-
});
|
|
994
|
-
process.stdout.write(refusal === null ? "sweep" : refusal);
|
|
995
|
-
NODE_EOF
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
echo
|
|
999
|
-
echo "-- local branches --"
|
|
1000
|
-
|
|
1001
|
-
while IFS= read -r br; do
|
|
1002
|
-
[ -n "$br" ] || continue
|
|
1003
|
-
|
|
1004
|
-
# The host, asked exactly as the worktree loop asks it. Ancestry FIRST only
|
|
1005
|
-
# because it needs no network; it can only ever ADD a merged answer, never
|
|
1006
|
-
# withhold one, since squash-merge leaves the branch permanently ahead and
|
|
1007
|
-
# falls through to `pr_merged`.
|
|
1008
|
-
merged=false
|
|
1009
|
-
if [ "$(git rev-list --count "origin/$DEFAULT..$br" 2>/dev/null || echo 1)" = "0" ]; then
|
|
1010
|
-
merged=true; bwhy="merged into $DEFAULT"
|
|
1011
|
-
elif pr_merged "$br"; then
|
|
1012
|
-
merged=true; bwhy="PR merged (squash)"
|
|
1013
|
-
fi
|
|
1014
|
-
|
|
1015
|
-
held=false
|
|
1016
|
-
sweep_is_checked_out "$br" && held=true
|
|
1017
|
-
|
|
1018
|
-
bverdict=$(sweep_branch_verdict "$br" "$merged" "$held")
|
|
1019
|
-
|
|
1020
|
-
if [ "$bverdict" != "sweep" ]; then
|
|
1021
|
-
case "$bverdict" in
|
|
1022
|
-
default-branch) breason="the default branch — never deleted" ;;
|
|
1023
|
-
no-merged-pr) breason="unlanded work — no merged PR" ;;
|
|
1024
|
-
checked-out) breason="checked out in a worktree — somebody is reading it" ;;
|
|
1025
|
-
*) breason="rule could not be asked — keeping" ;;
|
|
1026
|
-
esac
|
|
1027
|
-
printf '%-8s %-52s %s\n' "keep" "$br" "$breason"; kept_branches=$((kept_branches+1)); continue
|
|
1028
|
-
fi
|
|
1029
|
-
|
|
1030
|
-
if [ "$MAX" -gt 0 ] && [ "$swept_branches" -ge "$MAX" ]; then
|
|
1031
|
-
printf '%-8s %-52s %s\n' "keep" "$br" "--max $MAX reached"; kept_branches=$((kept_branches+1)); continue
|
|
1032
|
-
fi
|
|
1033
|
-
|
|
1034
|
-
swept_branches=$((swept_branches+1))
|
|
1035
|
-
if [ "$DRY" -eq 1 ]; then
|
|
1036
|
-
printf '%-8s %-52s %s\n' "would" "$br" "$bwhy, no worktree holds it"
|
|
1037
|
-
else
|
|
1038
|
-
# `-D`, not `-d`. The safety is the two measurements above, and `-d` would
|
|
1039
|
-
# veto every squash-merged branch — the whole population — for a reason
|
|
1040
|
-
# that has nothing to do with whether the work landed.
|
|
1041
|
-
if git branch -D "$br" >/dev/null 2>&1; then
|
|
1042
|
-
printf '%-8s %-52s %s\n' "deleted" "$br" "$bwhy, local ref deleted"; deleted_branches=$((deleted_branches+1))
|
|
1043
|
-
else
|
|
1044
|
-
printf '%-8s %-52s %s\n' "FAILED" "$br" "git branch -D refused"; kept_branches=$((kept_branches+1))
|
|
1045
|
-
fi
|
|
1046
|
-
fi
|
|
1047
|
-
done < <(git for-each-ref --format='%(refname:short)' refs/heads/ 2>/dev/null)
|
|
1048
|
-
|
|
1049
|
-
# ---------------------------------------------------------------------------
|
|
1050
|
-
# KIND 3: ORPHANED CLAIM REFS.
|
|
1051
|
-
#
|
|
1052
|
-
# A claim whose agent never existed. `plot-reconcile-scan.sh:423` defines the
|
|
1053
|
-
# marker precisely and this uses THE SAME definition: a claim commit is titled
|
|
1054
|
-
# `plot: claim ...` AND empty, its tree equal to its parent's. The subject
|
|
1055
|
-
# alone is not evidence — a human commit titled "plot: claim handling
|
|
1056
|
-
# refactor" carrying real files would otherwise read as an empty claim, and
|
|
1057
|
-
# the sweep would offer to delete real work.
|
|
1058
|
-
#
|
|
1059
|
-
# ONLY WHAT THE SCAN ALREADY CALLS REAPABLE IS SWEPT. Section 3 classifies
|
|
1060
|
-
# these: a `deferred:`/`moved:` annotation in the plan means reapable, a bare
|
|
1061
|
-
# `claimed:` needs judgment. The judgment cases are left for a person and keep
|
|
1062
|
-
# being reported — a slow worker and a dead one leave the identical empty
|
|
1063
|
-
# branch, and one of them is doing real work.
|
|
1064
|
-
#
|
|
1065
|
-
# Git cannot answer how a claim ended, so the plan annotation is the only
|
|
1066
|
-
# signal. Reading it here is the same deliberate exception the scan states: it
|
|
1067
|
-
# decides CLEANUP, not work, so a wrong annotation costs at most a missed
|
|
1068
|
-
# cleanup — never lost or duplicated work.
|
|
1069
|
-
# ---------------------------------------------------------------------------
|
|
1070
|
-
|
|
1071
|
-
ACTIVE_DIR_SWEEP="docs/plans/active/"
|
|
1072
|
-
if [ -r "$CONFIG" ]; then
|
|
1073
|
-
d=$(bash "$CONFIG" get "Active index" "docs/plans/active/" 2>/dev/null) && [ -n "$d" ] && ACTIVE_DIR_SWEEP="$d"
|
|
1074
|
-
fi
|
|
1075
|
-
case "$ACTIVE_DIR_SWEEP" in /*) ;; *) ACTIVE_DIR_SWEEP="$ROOT/$ACTIVE_DIR_SWEEP" ;; esac
|
|
1076
|
-
|
|
1077
|
-
# Does this branch carry ONLY empty claim commits? The scan's definition,
|
|
1078
|
-
# applied to LOCAL refs — this sweep runs over local branches, where the scan
|
|
1079
|
-
# reads `origin/`. ONE `board/plot-empty-claim.mjs` call answers every branch;
|
|
1080
|
-
# a branch it does not answer `<branch>\t0` for, a missing bundle included,
|
|
1081
|
-
# carries real work and stays.
|
|
1082
|
-
EMPTY_CLAIMS=$(git for-each-ref --format='%(refname:short)' refs/heads/ 2>/dev/null |
|
|
1083
|
-
while IFS= read -r br; do
|
|
1084
|
-
git log --boundary --format="${br//%/%%}%x09%m%x09%H%x09%T%x09%P%x09%s" \
|
|
1085
|
-
"origin/$DEFAULT..$br" -- </dev/null 2>/dev/null
|
|
1086
|
-
done | node "$(dirname "${BASH_SOURCE[0]}")/board/plot-empty-claim.mjs" 2>/dev/null) || EMPTY_CLAIMS=""
|
|
1087
|
-
|
|
1088
|
-
sweep_is_empty_claim() { # $1=branch
|
|
1089
|
-
printf '%s\n' "$EMPTY_CLAIMS" | grep -qxF "$1 0"
|
|
1090
|
-
}
|
|
1091
|
-
|
|
1092
|
-
# How the plan annotation classified this claim — the scan's `claim_disposition`,
|
|
1093
|
-
# same predicate, same directory.
|
|
1094
|
-
sweep_claim_disposition() { # $1=branch → abandoned | unresolved
|
|
1095
|
-
local br="$1" l line
|
|
1096
|
-
for l in "$ACTIVE_DIR_SWEEP"/*.md; do
|
|
1097
|
-
[ -e "$l" ] || continue
|
|
1098
|
-
line=$(grep -F -- "\`$br\`" "$l" 2>/dev/null | head -1)
|
|
1099
|
-
[ -n "$line" ] || continue
|
|
1100
|
-
case "$line" in
|
|
1101
|
-
*"<!-- deferred:"*|*"<!-- moved:"*) echo "abandoned"; return ;;
|
|
1102
|
-
esac
|
|
1103
|
-
done
|
|
1104
|
-
echo "unresolved"
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
sweep_claim_verdict() { # $1=branch $2=empty(true/false) $3=disposition
|
|
1108
|
-
PLOT_BRANCH="$1" PLOT_EMPTY="$2" PLOT_DISP="$3" PLOT_RULE="$SWEEP_RULE" \
|
|
1109
|
-
node --input-type=module - <<'NODE_EOF' 2>/dev/null
|
|
1110
|
-
const { firstClaimRefusal } = await import(process.env.PLOT_RULE);
|
|
1111
|
-
const refusal = firstClaimRefusal({
|
|
1112
|
-
branch: process.env.PLOT_BRANCH,
|
|
1113
|
-
isEmptyClaim: process.env.PLOT_EMPTY === "true",
|
|
1114
|
-
disposition: process.env.PLOT_DISP === "abandoned" ? "abandoned" : "unresolved",
|
|
1115
|
-
});
|
|
1116
|
-
process.stdout.write(refusal === null ? "sweep" : refusal);
|
|
1117
|
-
NODE_EOF
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
echo
|
|
1121
|
-
echo "-- orphaned claim refs --"
|
|
1122
|
-
|
|
1123
|
-
while IFS= read -r br; do
|
|
1124
|
-
[ -n "$br" ] || continue
|
|
1125
|
-
[ "$br" = "$DEFAULT" ] && continue
|
|
1126
|
-
|
|
1127
|
-
empty=false
|
|
1128
|
-
sweep_is_empty_claim "$br" && empty=true
|
|
1129
|
-
# Only branches that ARE empty claims belong to this kind at all. Anything
|
|
1130
|
-
# else is another kind's population or none, and reporting it here would say
|
|
1131
|
-
# "this is a claim we declined" about a branch carrying real work.
|
|
1132
|
-
[ "$empty" = "true" ] || continue
|
|
1133
|
-
|
|
1134
|
-
disp=$(sweep_claim_disposition "$br")
|
|
1135
|
-
cverdict=$(sweep_claim_verdict "$br" "$empty" "$disp")
|
|
1136
|
-
|
|
1137
|
-
if [ "$cverdict" != "sweep" ]; then
|
|
1138
|
-
case "$cverdict" in
|
|
1139
|
-
needs-judgment) creason="still claimed, no commits → needs judgment (worker thinking, or dead)" ;;
|
|
1140
|
-
not-an-empty-claim) creason="carries real work — not a claim" ;;
|
|
1141
|
-
*) creason="rule could not be asked — keeping" ;;
|
|
1142
|
-
esac
|
|
1143
|
-
printf '%-8s %-52s %s\n' "keep" "$br" "$creason"; kept_claims=$((kept_claims+1)); continue
|
|
1144
|
-
fi
|
|
1145
|
-
|
|
1146
|
-
if [ "$MAX" -gt 0 ] && [ "$swept_claims" -ge "$MAX" ]; then
|
|
1147
|
-
printf '%-8s %-52s %s\n' "keep" "$br" "--max $MAX reached"; kept_claims=$((kept_claims+1)); continue
|
|
1148
|
-
fi
|
|
1149
|
-
|
|
1150
|
-
swept_claims=$((swept_claims+1))
|
|
1151
|
-
if [ "$DRY" -eq 1 ]; then
|
|
1152
|
-
printf '%-8s %-52s %s\n' "would" "$br" "abandoned claim (plan says deferred/moved)"
|
|
1153
|
-
else
|
|
1154
|
-
if git branch -D "$br" >/dev/null 2>&1; then
|
|
1155
|
-
printf '%-8s %-52s %s\n' "deleted" "$br" "abandoned claim — local ref deleted"; deleted_claims=$((deleted_claims+1))
|
|
1156
|
-
else
|
|
1157
|
-
printf '%-8s %-52s %s\n' "FAILED" "$br" "git branch -D refused"; kept_claims=$((kept_claims+1))
|
|
1158
|
-
fi
|
|
1159
|
-
fi
|
|
1160
|
-
done < <(git for-each-ref --format='%(refname:short)' refs/heads/ 2>/dev/null)
|
|
1161
|
-
|
|
1162
|
-
# ---------------------------------------------------------------------------
|
|
1163
|
-
# KIND 4: DIRTY TREES NOBODY OWNS.
|
|
1164
|
-
#
|
|
1165
|
-
# REFUSED TODAY AND NEVER RESOLVED, and this KEEPS it refused. `uncommitted-
|
|
1166
|
-
# changes` is a refusal for the same reason the create-or-reset guard does not
|
|
1167
|
-
# `reset --hard`: the case where the guard is wrong is exactly the case where
|
|
1168
|
-
# destruction cannot be undone. A guard that misjudges should leave a desk the
|
|
1169
|
-
# sweep reports, not deleted work.
|
|
1170
|
-
#
|
|
1171
|
-
# SO THIS KIND HAS NO `--yes` PATH AT ALL, and that absence is the design. What
|
|
1172
|
-
# it adds is the NAME: the tree is reported as a leftover whose owner is
|
|
1173
|
-
# `nobody`, loudly enough that a person clears it. The population was refused
|
|
1174
|
-
# and never resolved precisely because nothing ever said whose it was.
|
|
1175
|
-
#
|
|
1176
|
-
# It runs over EVERY worktree, not only dispatch trees: a tree nobody owns is
|
|
1177
|
-
# by construction one that may carry no `.plot-worker.pid`, so the marker the
|
|
1178
|
-
# reap loop gates on is exactly what a leftover of this kind lacks.
|
|
1179
|
-
#
|
|
1180
|
-
# EXCEPT THE MAIN CHECKOUT, which is a person's desk by definition. Measured
|
|
1181
|
-
# while writing this: the operator's own checkout carried 2 uncommitted files
|
|
1182
|
-
# and no `.plot-worker.pid`, so it read as a leftover nobody owns — and it is
|
|
1183
|
-
# the one tree on the estate somebody is certainly at. `$ROOT` is not the test,
|
|
1184
|
-
# because this script runs from whichever worktree invoked it; the main
|
|
1185
|
-
# checkout is the PARENT of `--git-common-dir`, which every worktree agrees on.
|
|
1186
|
-
# ---------------------------------------------------------------------------
|
|
1187
|
-
|
|
1188
|
-
MAIN_CHECKOUT=$(cd "$(git rev-parse --git-common-dir 2>/dev/null)/.." 2>/dev/null && pwd -P) || MAIN_CHECKOUT=""
|
|
1189
|
-
|
|
1190
|
-
sweep_dirty_owner() { # $1=pid $2=manifest → owner word
|
|
1191
|
-
PLOT_PID="$1" PLOT_MANIFEST="$2" PLOT_RULE="$SWEEP_RULE" \
|
|
1192
|
-
node --input-type=module - <<'NODE_EOF' 2>/dev/null
|
|
1193
|
-
const { dirtyTreeOwner } = await import(process.env.PLOT_RULE);
|
|
1194
|
-
process.stdout.write(dirtyTreeOwner({
|
|
1195
|
-
path: "",
|
|
1196
|
-
branch: "",
|
|
1197
|
-
dirtyCount: 1,
|
|
1198
|
-
workerPid: process.env.PLOT_PID === "" ? null : process.env.PLOT_PID,
|
|
1199
|
-
manifest: process.env.PLOT_MANIFEST,
|
|
1200
|
-
}));
|
|
1201
|
-
NODE_EOF
|
|
1202
|
-
}
|
|
1203
|
-
|
|
1204
|
-
echo
|
|
1205
|
-
echo "-- dirty trees nobody owns --"
|
|
1206
|
-
|
|
1207
|
-
while IFS=$'\t' read -r wt br; do
|
|
1208
|
-
[ -n "$wt" ] || continue
|
|
1209
|
-
[ -d "$wt" ] || continue
|
|
1210
|
-
# The main checkout is a person's desk, and its dirt is a person's work in
|
|
1211
|
-
# progress. Compared canonically, since git reports resolved paths and macOS
|
|
1212
|
-
# spells `/tmp`, `/var` and `/etc` two ways.
|
|
1213
|
-
[ -n "$MAIN_CHECKOUT" ] && [ "$(canonical "$wt")" = "$(canonical "$MAIN_CHECKOUT")" ] && continue
|
|
1214
|
-
dshort=${br#refs/heads/}
|
|
1215
|
-
|
|
1216
|
-
dcount=$(desk_dirt "$wt" | wc -l | tr -d ' ')
|
|
1217
|
-
[ "${dcount:-0}" -gt 0 ] || continue
|
|
1218
|
-
|
|
1219
|
-
# The same liveness reading the reap loop takes, so a desk the reaper keeps
|
|
1220
|
-
# for a live worker and one the sweep names as owned are the same desks.
|
|
1221
|
-
dpid=$(desk_worker_pid "$wt")
|
|
1222
|
-
|
|
1223
|
-
dmanifest=""
|
|
1224
|
-
if m=$(manifest_for "$(canonical "$wt")"); then dmanifest=$(basename "$m"); fi
|
|
1225
|
-
|
|
1226
|
-
downer=$(sweep_dirty_owner "$dpid" "$dmanifest")
|
|
1227
|
-
# A rule that could not be asked answers nothing, and nothing is not
|
|
1228
|
-
# `nobody`. An unaskable rule must not be what promotes a tree to a finding.
|
|
1229
|
-
[ "$downer" = "nobody" ] || continue
|
|
1230
|
-
|
|
1231
|
-
dirty_trees=$((dirty_trees+1))
|
|
1232
|
-
# NEVER a `would` or a `deleted`: there is no act to preview. `LEFTOVER` is
|
|
1233
|
-
# the verdict, and the owner is the finding.
|
|
1234
|
-
printf '%-8s %-52s %s\n' "LEFTOVER" "${dshort:-(detached)}" \
|
|
1235
|
-
"$dcount uncommitted, owner: nobody — clear it by hand: $wt"
|
|
1236
|
-
done < <(git worktree list --porcelain \
|
|
1237
|
-
| awk '/^worktree /{p=$2} /^branch /{print p"\t"$2} /^detached/{print p"\t"}')
|
|
1238
|
-
|
|
1239
|
-
if [ "$dirty_trees" -gt 0 ]; then
|
|
1240
|
-
echo " ^ $dirty_trees dirty tree(s) nobody owns. Nothing was deleted from them,"
|
|
1241
|
-
echo " deliberately: where this guard is wrong, destruction cannot be undone."
|
|
1242
|
-
fi
|
|
1243
|
-
|
|
1244
|
-
# THE REMOTE refs are untouched, deliberately. This deletes LOCAL branches,
|
|
1245
|
-
# which are re-fetchable from origin — a copy, not the copy — so the act is
|
|
1246
|
-
# bounded to a local ref and a reflog. A remote ref is not re-creatable at all,
|
|
1247
|
-
# which is why `plot-release-refs.sh` deletes those, plan-scoped, under its own
|
|
1248
|
-
# licence and its own five guards. The asymmetry between the kinds is the whole
|
|
1249
|
-
# safety argument and it stays.
|
|
1250
|
-
echo "summary: reapable=$reap removed=$removed kept=$kept vanished=$vanished unplaced=$unplaced cleared=$cleared branches=$swept_branches branches_deleted=$deleted_branches branches_kept=$kept_branches claims=$swept_claims claims_deleted=$deleted_claims claims_kept=$kept_claims dirty_trees=$dirty_trees dry_run=$DRY"
|
|
1251
|
-
exit 0
|
|
2
|
+
# Launcher. The mechanical half of reaping worktrees is `board/plot-reap.mjs`,
|
|
3
|
+
# a JS entry point over the domain — see its header for what it does. This
|
|
4
|
+
# resolves it, passes arguments and the exit code through, and decides
|
|
5
|
+
# nothing.
|
|
6
|
+
set -uo pipefail
|
|
7
|
+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
8
|
+
bundle="$script_dir/board/plot-reap.mjs"
|
|
9
|
+
[ -f "$bundle" ] || {
|
|
10
|
+
echo "plot-reap: cannot find $bundle — update the plot plugin in this repository, or — in the plot repository itself — run: pnpm build:board" >&2
|
|
11
|
+
exit 2
|
|
12
|
+
}
|
|
13
|
+
exec node "$bundle" "$@"
|