@plot-pm/board 0.16.1 → 0.16.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/plot-reap.sh CHANGED
@@ -44,12 +44,11 @@
44
44
  # written. The scan reports; nothing reaped. Measured 2026-08-25 on this
45
45
  # estate: 56 worktrees, 42 of them dispatch trees, of which 29 were finished.
46
46
  #
47
- # WHY A SCRIPT RATHER THAN AN AGENT (Manifesto Principle 3, and the licence
48
- # `plot-resolve-artifact.sh` states for the one other automatic write): every
49
- # refusal below is a MEASUREMENT, not a judgement. Is a process alive; is the
50
- # tree dirty; did the host merge the PR. An agent asked "is this safe to
51
- # delete?" can talk itself past any of the three. A script cannot, and
52
- # judgement's absence is exactly what licenses the delete.
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.
53
52
  #
54
53
  # AND THE DECIDING IS NOT HERE. This script GATHERS the readings, asks
55
54
  # `packages/domain/src/rules/reapable.ts`, and ACTS on the answer; it holds no
@@ -399,86 +398,43 @@ if [ -r "$CONFIG" ]; then
399
398
  fi
400
399
  case "$MANIFEST_DIR" in /*) ;; *) MANIFEST_DIR="$ROOT/$MANIFEST_DIR" ;; esac
401
400
 
402
- # Where the agent logs live, resolved through `plot-config.sh` from the SAME
403
- # `Worktree root` key `resolve_wt_root()` and `agentLogDir` read. Three readers
404
- # of one key, and none of them may invent a second: a reaper sweeping a
405
- # directory the board never writes to reports success over a file that is still
406
- # there, which is the failure this slice exists to stop.
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.
407
406
  #
408
- # THE FALLBACK IS THE PARENT DIRECTORY, NOT AN ERROR — `agentLogDir`'s rule,
409
- # stated the same way here. A repository with no key has no `.worktrees/`, and
410
- # the logs it wrote are beside it; a reaper that refused to look there would
411
- # clean nothing on exactly the repositories that never migrated.
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.
412
413
  #
413
- # The case split is `resolve_wt_root()`'s: absolute taken as given, relative
414
- # joined onto the repo root, trailing slash trimmed as pure string work because
415
- # the directory need not exist. A second convention for resolving a configured
416
- # directory is a second way to be wrong.
417
- LOG_DIR="$(cd "$ROOT/.." && pwd)"
418
- if [ -r "$CONFIG" ]; then
419
- d=$(bash "$CONFIG" get "Worktree root" "" 2>/dev/null) || d=""
420
- if [ -n "$d" ]; then
421
- case "$d" in
422
- /*) LOG_DIR="$d" ;;
423
- *) LOG_DIR="$ROOT/$d" ;;
424
- esac
425
- LOG_DIR="${LOG_DIR%/}"
426
- fi
427
- fi
428
-
429
- # Where the DESKS live, which is the same key resolved for a different question.
430
- #
431
- # `LOG_DIR` and this answer two things: `LOG_DIR` is where a finished branch's
432
- # log files are swept from, and this is the directory a tree must sit under to
433
- # be a candidate desk at all. A repository with NO configured root has no
434
- # `.worktrees/` and its desks are named `plot-wt-*` beside the repo — so this
435
- # stays EMPTY there rather than defaulting to the parent, because the parent
436
- # holds every sibling checkout a person ever made and calling those candidate
437
- # desks is the over-broad reading this slice must not introduce.
438
- #
439
- # IT IS RESOLVED AGAINST THE MAIN CHECKOUT, NOT `$ROOT`, and that is the one
440
- # place in this script where the two differ on purpose. `git rev-parse
414
+ # IT IS RESOLVED AGAINST THE MAIN CHECKOUT, NOT `$ROOT`. `git rev-parse
441
415
  # --show-toplevel` answers *this* worktree, so a reaper run from inside a desk
442
- # resolves `.worktrees` to a directory beneath that desk — which does not
416
+ # would resolve `.worktrees` to a directory beneath that desk — which does not
443
417
  # exist, so every tree reads as unplaceable-but-elsewhere and the reading is
444
- # silently empty. Measured 2026-09-10 from this worktree: `$ROOT/.worktrees`
418
+ # silently empty. Measured 2026-09-10 from inside a desk: `$ROOT/.worktrees`
445
419
  # named `.../plot-wt-feature-a-finished-desk-is-a-finding/.worktrees` while
446
420
  # every desk sits under `.../plot/.worktrees`. A dispatched agent runs the
447
421
  # reaper from exactly there, so the wrong answer would be the usual one.
448
422
  #
449
- # `--git-common-dir` is shared by every worktree of one repository, and its
450
- # parent is the primary checkout — which is what `plot-dispatch.sh` composes
451
- # desk paths from when it creates them. Same directory, same key, so a
452
- # creation and this reading cannot disagree.
453
- #
454
- # `LOG_DIR` above keeps `$ROOT` untouched: it is this script's established
455
- # behaviour with its own callers, and changing where a sweep DELETES from is a
456
- # blast radius rather than a reading. Named here rather than fixed silently.
457
- WT_ROOT=""
458
- if [ -r "$CONFIG" ]; then
459
- d=$(bash "$CONFIG" get "Worktree root" "" 2>/dev/null) || d=""
460
- if [ -n "$d" ]; then
461
- case "$d" in
462
- /*) WT_ROOT="$d" ;;
463
- *)
464
- # Falls back to `$ROOT` where git cannot answer, which is the same
465
- # directory on a single-checkout repository and the only one available.
466
- main_checkout=$(dirname "$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)") \
467
- || main_checkout="$ROOT"
468
- [ -d "$main_checkout" ] || main_checkout="$ROOT"
469
- WT_ROOT="$main_checkout/$d"
470
- ;;
471
- esac
472
- WT_ROOT="${WT_ROOT%/}"
473
- fi
474
- fi
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"
475
430
 
476
431
  # The files ONE branch's agent run leaves beside its worktree, removed with it.
477
432
  #
478
433
  # WHICH LOG THIS IS, since the plan says "the dispatcher log" and the estate
479
434
  # holds two shapes of one. `plot-resolve-<branch>` is keyed by BRANCH with its
480
- # slashes flattened (`repairLogPath`), so it maps one-to-one onto the worktree
481
- # this loop is removing. `plot-dispatch-<slug>` is keyed by PLAN and opened for
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
482
438
  # APPEND across every dispatch of that plan — `dispatch.ts:150` states it: "a
483
439
  # dispatcher log belongs to a plan, a worker log to a branch". Reaping one
484
440
  # branch of a five-branch plan must not delete the record the other four are
@@ -700,8 +656,25 @@ while IFS=$'\037' read -r wt br prunable; do
700
656
  # not about whether a worktree may go. The filter runs before `head`, so a
701
657
  # correction beside a real file names the real file.
702
658
  marker=false
703
- ls "$wt"/PLOT-BLOCKED* >/dev/null 2>&1 && marker=true
704
- dirty=$(desk_dirt "$wt" | head -1)
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
705
678
 
706
679
  # The host: whether ANY PR for this branch merged.
707
680
  #
@@ -770,6 +743,7 @@ while IFS=$'\037' read -r wt br prunable; do
770
743
  verdict=$(PLOT_BRANCH="$short" PLOT_DEFAULT="$DEFAULT" PLOT_PID="$pid" \
771
744
  PLOT_DIRTY="$dirty" PLOT_MARKER="$marker" PLOT_MERGE="$merge" \
772
745
  PLOT_UNPUSHED="$unpushed" PLOT_RULE="$RULE" \
746
+ PLOT_MARKER_RECORDS_WORK="$marker_records_work" \
773
747
  node --input-type=module - <<'NODE_EOF' 2>/dev/null
774
748
  // Imported from an ABSOLUTE path derived from this script, never from the
775
749
  // cwd. The reaper runs with its cwd wherever the operator invoked it and the
@@ -790,6 +764,7 @@ const problem = firstReapRefusal({
790
764
  workerPid: process.env.PLOT_PID === "" ? null : process.env.PLOT_PID,
791
765
  dirtyPath: process.env.PLOT_DIRTY,
792
766
  blockedMarker: process.env.PLOT_MARKER === "true",
767
+ markerRecordsWork: process.env.PLOT_MARKER_RECORDS_WORK === "true",
793
768
  merge: process.env.PLOT_MERGE,
794
769
  unpushed: process.env.PLOT_UNPUSHED === "unknown"
795
770
  ? "unknown"
@@ -836,6 +811,27 @@ NODE_EOF
836
811
  # a missing log is the desired state, not an event.
837
812
  logs=$(present_logs "$short")
838
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
+
839
835
  reap=$((reap+1))
840
836
  if [ "$DRY" -eq 1 ]; then
841
837
  printf '%-8s %-52s %s\n' "would" "$label" "$why${logs:+, log $logs}"
@@ -1079,25 +1075,18 @@ fi
1079
1075
  case "$ACTIVE_DIR_SWEEP" in /*) ;; *) ACTIVE_DIR_SWEEP="$ROOT/$ACTIVE_DIR_SWEEP" ;; esac
1080
1076
 
1081
1077
  # Does this branch carry ONLY empty claim commits? The scan's definition,
1082
- # applied to a LOCAL ref — this sweep runs over local branches, where the scan
1083
- # reads `origin/`.
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
+
1084
1088
  sweep_is_empty_claim() { # $1=branch
1085
- local br="$1" ahead c subj real=0
1086
- ahead=$(git rev-list --count "origin/$DEFAULT..$br" 2>/dev/null || echo 0)
1087
- [ "${ahead:-0}" -gt 0 ] || return 1 # nothing of its own → merged work, not a claim
1088
- for c in $(git rev-list "origin/$DEFAULT..$br" </dev/null 2>/dev/null); do
1089
- subj=$(git log -1 --format=%s "$c" </dev/null 2>/dev/null)
1090
- case "$subj" in
1091
- "plot: claim "*)
1092
- # Titled AND empty. Both, or it counts as real work.
1093
- if [ "$(git rev-parse "$c^{tree}" </dev/null 2>/dev/null)" \
1094
- = "$(git rev-parse "$c^^{tree}" </dev/null 2>/dev/null)" ]; then
1095
- continue
1096
- fi ;;
1097
- esac
1098
- real=$((real+1))
1099
- done
1100
- [ "$real" = "0" ]
1089
+ printf '%s\n' "$EMPTY_CLAIMS" | grep -qxF "$1 0"
1101
1090
  }
1102
1091
 
1103
1092
  # How the plan annotation classified this claim — the scan's `claim_disposition`,
@@ -165,11 +165,17 @@ _action_receipt_dir() {
165
165
  # adapter — a script name in a controller is a boundary crossing no spawn
166
166
  # counter can see. So the shared filename is the action word, and mapping a
167
167
  # script back to it happens HERE, on the side that reads command lines.
168
+ #
169
+ # `plot-deliver.sh` maps to `deliver` here, same as always — `release` is a
170
+ # SEPARATE action sharing that script, and only `plot-controller-gate.sh` can
171
+ # tell the two calls apart (by whether `--release` rode along), so it passes
172
+ # the action word directly rather than the script name for that one case.
168
173
  _action_of() { # $1=script name or action word → the action, or nothing
169
174
  case "${1##*/}" in
170
175
  plot-dispatch.sh|dispatch) printf 'dispatch\n' ;;
171
176
  plot-approve.sh|approve) printf 'approve\n' ;;
172
177
  plot-deliver.sh|deliver) printf 'deliver\n' ;;
178
+ release) printf 'release\n' ;;
173
179
  esac
174
180
  }
175
181
 
@@ -231,7 +237,7 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
231
237
  action_reason="$*"
232
238
  if [ -z "$action_script" ] || [ -z "$action_subject" ] || [ -z "$action_reason" ]; then
233
239
  echo "plot-state-receipt: --unowned-action needs an action, a subject and a reason." >&2
234
- echo " usage: plot-state-receipt.sh --unowned-action <dispatch|approve|deliver> <slug> <reason>" >&2
240
+ echo " usage: plot-state-receipt.sh --unowned-action <dispatch|approve|deliver|release> <slug> <reason>" >&2
235
241
  echo "" >&2
236
242
  echo " The reason is required because each use names a routing gap, and a gap" >&2
237
243
  echo " nobody wrote down is one nobody closes. Where the board IS running, the" >&2
@@ -240,13 +246,17 @@ if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
240
246
  fi
241
247
  # The action word, or the script name — an operator reaching for this has
242
248
  # just read a refusal naming a script, so both spellings are accepted.
249
+ # `release` has no script of its own (`plot-deliver.sh --release` shares
250
+ # `plot-deliver.sh`, which already names `deliver`), so only the bare word
251
+ # is accepted for it.
243
252
  case "$action_script" in
244
253
  dispatch|plot-dispatch.sh) action_script="dispatch" ;;
245
254
  approve|plot-approve.sh) action_script="approve" ;;
246
255
  deliver|plot-deliver.sh) action_script="deliver" ;;
256
+ release) action_script="release" ;;
247
257
  *)
248
258
  echo "plot-state-receipt: '$action_script' is not a controller-owned action." >&2
249
- echo " The three are: dispatch, approve, deliver." >&2
259
+ echo " The four are: dispatch, approve, deliver, release." >&2
250
260
  exit 2
251
261
  ;;
252
262
  esac
package/plot-tmp.sh CHANGED
@@ -1,5 +1,5 @@
1
1
  # plot-tmp.sh — every temp path a Plot script creates, and the process's only
2
- # EXIT/INT/TERM traps. SOURCED, not run.
2
+ # EXIT/INT/TERM/PIPE traps. SOURCED, not run.
3
3
  #
4
4
  # . "$SCRIPT_DIR/plot-tmp.sh"
5
5
  # plot_tmpdir work fleet-ref # $work = $TMPDIR/plot-fleet-ref.XXXXXX (a directory)
@@ -27,8 +27,11 @@
27
27
  # first call would be installed inside that call's substitution and remove the
28
28
  # path when the substitution closed. `PLOT_TMP_LOADED` makes a second source in
29
29
  # the same process a no-op, because a re-run setup would truncate the live
30
- # registry. INT and TERM run the cleanup, clear their own trap and re-raise, so
31
- # the script stops with 130 or 143 rather than running on to exit 0.
30
+ # registry. INT, TERM and PIPE run the cleanup, clear their own trap and
31
+ # re-raise, so the script stops with 130, 143 or 141 rather than running on to
32
+ # exit 0. PIPE is among them because a board server stopped with SIGTERM leaves
33
+ # its running scripts writing to closed pipes, and SIGPIPE's default action
34
+ # skips the EXIT trap.
32
35
  #
33
36
  # A script that sources this must not install its own EXIT, INT or TERM trap:
34
37
  # the last `trap` wins, and that replacement is the defect this file fixes
@@ -42,29 +45,79 @@ PLOT_TMP_LOADED=$$
42
45
  # Fixed at first source: a later `TMPDIR` change in the script does not move it.
43
46
  # A file already at this path belongs to a dead process that had the same pid,
44
47
  # and its `c:` commands are not this process's, so it is replaced, never read.
48
+ # The file itself is created at the end of this file, after the traps.
45
49
  PLOT_TMP_REGISTRY="${TMPDIR:-/tmp}/plot-reg.$$"
46
- rm -f -- "$PLOT_TMP_REGISTRY" 2>/dev/null
47
- : > "$PLOT_TMP_REGISTRY" 2>/dev/null || true
48
50
 
49
51
  # One line per entry, in registration order: `p:<path>` or `c:<command>`.
50
52
  _plot_tmp_register() {
51
53
  printf '%s:%s\n' "$1" "$2" >> "$PLOT_TMP_REGISTRY" 2>/dev/null || true
52
54
  }
53
55
 
56
+ # A SIGNAL INSIDE A CREATION WAITS FOR THE REGISTRATION. bash runs a pending
57
+ # trap as soon as `$(mktemp …)` returns, before the next line registers the
58
+ # path, so a cleanup run there would remove the registry and leave the new path
59
+ # unlisted. While `__plot_tmp_busy` is set, the signal handler records the
60
+ # signal in `__plot_tmp_pending` and returns; `_plot_tmp_create` handles it once
61
+ # the path is registered.
62
+ #
63
+ # A SUBSHELL HOLDS THE SIGNAL ITSELF. bash resets caught traps in `$(…)` and
64
+ # `( … )`, so there the handler above is not installed and a TERM between
65
+ # `mktemp` and the registration ends the subshell with the path unlisted. The
66
+ # owner's cleanup then runs and cannot remove it. `plot-host.sh`'s
67
+ # `pr_list_call` creates `plot-host-prlist-err.*` this way, inside
68
+ # `_raw="$(pr_list_call …)"`, and CI run 37052327801 left one when the board
69
+ # test teardown sent its process group TERM. Inside a subshell
70
+ # (`BASH_SUBSHELL` > 0; bash 3.2 has no `BASHPID`) the creation installs
71
+ # handlers that only record the signal, and after the registration it restores
72
+ # the defaults and exits with 128 + the signal's number. It does not run the
73
+ # cleanup: the registry belongs to the owner, which received the same group
74
+ # signal and removes the path when the subshell has ended.
75
+ __plot_tmp_busy=''
76
+ __plot_tmp_pending=''
77
+
78
+ # _plot_tmp_create VAR prefix [-d] — create, register, assign to VAR.
79
+ _plot_tmp_create() {
80
+ local __plot_tmp_new __plot_tmp_rc __plot_tmp_sub=''
81
+ __plot_tmp_busy=1
82
+ if [ "${BASH_SUBSHELL:-0}" -gt 0 ]; then
83
+ __plot_tmp_sub=1
84
+ trap '__plot_tmp_pending="INT 2"' INT
85
+ trap '__plot_tmp_pending="TERM 15"' TERM
86
+ trap '__plot_tmp_pending="PIPE 13"' PIPE
87
+ fi
88
+ if [ "${3:-}" = -d ]; then
89
+ __plot_tmp_new=$(mktemp -d "${TMPDIR:-/tmp}/plot-$2.XXXXXX")
90
+ else
91
+ __plot_tmp_new=$(mktemp "${TMPDIR:-/tmp}/plot-$2.XXXXXX")
92
+ fi
93
+ __plot_tmp_rc=$?
94
+ # THE PATH IS REGISTERED WHEN IT EXISTS, whatever the status says. bash 3.2
95
+ # reports 143 for `$(mktemp …)` when a trap ran during it, although `mktemp`
96
+ # created the path and printed it; a status-only test leaves that path
97
+ # unlisted.
98
+ if [ -n "$__plot_tmp_new" ] && { [ -e "$__plot_tmp_new" ] || [ "$__plot_tmp_rc" -eq 0 ]; }; then
99
+ _plot_tmp_register p "$__plot_tmp_new"
100
+ fi
101
+ __plot_tmp_busy=''
102
+ if [ -n "$__plot_tmp_sub" ]; then
103
+ trap - INT TERM PIPE
104
+ [ -z "$__plot_tmp_pending" ] || exit $((128 + ${__plot_tmp_pending#* }))
105
+ elif [ -n "$__plot_tmp_pending" ]; then
106
+ # shellcheck disable=SC2086 # "<name> <number>", split on purpose
107
+ _plot_tmp_on_signal $__plot_tmp_pending
108
+ fi
109
+ [ "$__plot_tmp_rc" -eq 0 ] || return 1
110
+ printf -v "$1" '%s' "$__plot_tmp_new"
111
+ }
112
+
54
113
  # plot_tmpdir VAR prefix — create a directory, register it, assign it to VAR.
55
114
  plot_tmpdir() {
56
- local __plot_tmp_new
57
- __plot_tmp_new=$(mktemp -d "${TMPDIR:-/tmp}/plot-$2.XXXXXX") || return 1
58
- _plot_tmp_register p "$__plot_tmp_new"
59
- printf -v "$1" '%s' "$__plot_tmp_new"
115
+ _plot_tmp_create "$1" "$2" -d
60
116
  }
61
117
 
62
118
  # plot_tmpfile VAR prefix — create a file, register it, assign it to VAR.
63
119
  plot_tmpfile() {
64
- local __plot_tmp_new
65
- __plot_tmp_new=$(mktemp "${TMPDIR:-/tmp}/plot-$2.XXXXXX") || return 1
66
- _plot_tmp_register p "$__plot_tmp_new"
67
- printf -v "$1" '%s' "$__plot_tmp_new"
120
+ _plot_tmp_create "$1" "$2"
68
121
  }
69
122
 
70
123
  # plot_on_exit command — run `command` (one line) when the process ends.
@@ -75,9 +128,13 @@ plot_on_exit() {
75
128
  # Runs every registered command and removes every registered path, in
76
129
  # registration order, then removes the registry. Runs once: the registry is
77
130
  # gone afterwards, so a second call finds nothing.
131
+ #
132
+ # PIPE is ignored for the cleanup: the process is ending, and a registered
133
+ # command that writes to a closed stdout or stderr must not stop the removals.
78
134
  _plot_tmp_cleanup() {
79
135
  local __plot_tmp_line
80
- [ -f "$PLOT_TMP_REGISTRY" ] || return 0
136
+ [ -n "${__plot_tmp_owned:-}" ] && [ -f "$PLOT_TMP_REGISTRY" ] || return 0
137
+ trap '' PIPE
81
138
  while IFS= read -r __plot_tmp_line; do
82
139
  case $__plot_tmp_line in
83
140
  c:*) eval "${__plot_tmp_line#c:}" ;;
@@ -97,6 +154,10 @@ _plot_tmp_on_exit() {
97
154
  # $1 is the signal name, $2 its number. `kill` re-raises it with the default
98
155
  # disposition; the `exit` is reached only if the shell defers the delivery.
99
156
  _plot_tmp_on_signal() {
157
+ if [ -n "$__plot_tmp_busy" ]; then
158
+ __plot_tmp_pending="$1 $2"
159
+ return 0
160
+ fi
100
161
  trap - EXIT "$1"
101
162
  _plot_tmp_cleanup
102
163
  kill -"$1" "$$"
@@ -106,3 +167,18 @@ _plot_tmp_on_signal() {
106
167
  trap _plot_tmp_on_exit EXIT
107
168
  trap '_plot_tmp_on_signal INT 2' INT
108
169
  trap '_plot_tmp_on_signal TERM 15' TERM
170
+ trap '_plot_tmp_on_signal PIPE 13' PIPE
171
+
172
+ # THE REGISTRY IS CREATED AFTER THE TRAPS, so no signal lands between the file
173
+ # and the handler that removes it. It is replaced under the creation deferral:
174
+ # until `__plot_tmp_owned` is set, a file at this path is a dead process's and
175
+ # the cleanup leaves it unread.
176
+ __plot_tmp_busy=1
177
+ rm -f -- "$PLOT_TMP_REGISTRY" 2>/dev/null
178
+ : > "$PLOT_TMP_REGISTRY" 2>/dev/null || true
179
+ __plot_tmp_owned=1
180
+ __plot_tmp_busy=''
181
+ if [ -n "$__plot_tmp_pending" ]; then
182
+ # shellcheck disable=SC2086 # "<name> <number>", split on purpose
183
+ _plot_tmp_on_signal $__plot_tmp_pending
184
+ fi
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env bash
2
2
  # The ONE answer to "how long has this worktree's agent been quiet?" — sourced,
3
- # not run, by `plot-worker-monitor.sh` and `plot-worker-loop.sh`.
3
+ # not run, by `plot-worker-loop.sh` for its own watcher (`plot-worker-state.sh`'s
4
+ # `plot_worker_idle_watch_pass`) and for its own ending message. Sourced by the
5
+ # now-deleted `plot-worker-monitor.sh` until `bug/the-loop-reports-idle`.
4
6
  #
5
7
  # It reads the AGENT rather than the machine. A `claude -p` session appends a
6
8
  # timestamped line to its transcript for every model turn, tool call and tool