@feigi/fleet-ctl 3.25.0 → 3.27.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.
@@ -290,252 +290,26 @@ git rev-parse --verify "$base_rev" >/dev/null || die "$base does not resolve"
290
290
  # script stats. So establish the listing is COMPLETE before trusting an absence
291
291
  # it reports — not by guarding its exit status, which stays 0 throughout.
292
292
  #
293
- # Do not try to predict which reads git needs: stat'ing each entry directory is
294
- # one level too shallow, because naming an entry needs read+execute on the
295
- # PARENT only. `chmod 000` on the `gitdir` FILE inside an entry passes every
296
- # permission test this script could make on the entry itself and still drops
297
- # the worktree from the listing — measured. Count
298
- # instead: one registry entry on disk per linked worktree, against what git
299
- # reported. A mismatch is a silent drop, whichever file inside was unreadable.
300
- #
301
- # Absent entirely is fine and answers nothing here: a repo where a worktree
302
- # was removed and pruned (or never had one) has no `worktrees` dir at all, and
303
- # that emptiness is real, not a permission problem — zero entries against the
304
- # main worktree alone is a match, so the release goes through.
305
- common=$(git rev-parse --path-format=absolute --git-common-dir) ||
306
- die "cannot resolve the git common directory"
307
- wtroot="$common/worktrees"
308
-
309
- # A function, not inline, because the count is taken more than once: here, and
310
- # again by the recount loop below, up to `recount_tries` times, while the
311
- # cross-check disagrees with git's own listing. It mirrors inflight.sh's own
312
- # count_registry, adapted for the difference that matters: that copy returns a
313
- # status into an accumulate-and-continue probe; this one dies, so an unreadable
314
- # registry still just dies here rather than returning past it.
315
- count_registry() {
316
- registered=0
317
- [ -e "$wtroot" ] || return 0
318
- # The parent needs its own check even so, and this is not redundant with the
319
- # count: unreadable, the glob below expands to nothing, and zero-on-disk would
320
- # AGREE with the empty listing git returns for the same reason.
321
- [ -r "$wtroot" ] && [ -x "$wtroot" ] ||
322
- die "worktree registry $wtroot could not be read — whether #$issue has a worktree is unknown"
323
- # A registry entry is a directory; anything else in here is not git's and must
324
- # not be counted as a worktree git failed to report.
325
- for entry in "$wtroot"/*; do
326
- [ -d "$entry" ] || continue
327
- # Skip only an EMPTY directory. That is an operator's stray `mkdir`, which
328
- # git ignores — and counting one refuses EVERY release of every ticket in
329
- # the repo, forever, over something git is right to ignore (measured, git
330
- # 2.50.1: git lists 2 worktrees where a bare `-d` count said 2 registered
331
- # against 1 linked). A stray FILE was already skipped by the `-d` above; a
332
- # stray directory was not, which is why the file case shipped green.
333
- #
334
- # Emptiness, NOT the absence of a `gitdir` file, and the difference is a
335
- # wrong "free": git drops an entry whose `gitdir` was deleted, so keying the
336
- # skip on that file lets the entry through as "not git's", the count agrees,
337
- # no refusal fires, and the release proceeds while the checkout may still be
338
- # on disk (measured: listed 1 → linked 0, and a gitdir-keyed count returns 0
339
- # to match). A corrupt entry still holds git's own files — commondir, HEAD,
340
- # index, logs, refs — so emptiness separates it from a stray where the
341
- # missing `gitdir` does not.
342
- #
343
- # `ls`'s STATUS, not just its output, and that is the whole point: an entry
344
- # we could not LIST is not an empty one, and `2>/dev/null` hides the
345
- # difference. A stray `mkdir` lists empty at rc 0; an entry chmod'd 000
346
- # (unsearchable) OR 0111 (searchable, so an `-x` test passes it, but not
347
- # readable) fails EACCES and prints nothing just the same. Reading only the
348
- # output skips that entry as "not git's" — and git drops it too, so the
349
- # counts AGREE, no refusal fires, and the claim is freed with the member's
350
- # uncommitted work still on disk (measured: exit 0, `released:true`,
351
- # `blockers:[]`, branch deleted, in-progress label dropped, checkout
352
- # standing). Could not read it, so we cannot tell → count it and let the
353
- # mismatch below fire.
354
- if contents=$(ls -A "$entry" 2>/dev/null) && [ -z "$contents" ]; then continue; fi
355
- registered=$((registered + 1))
356
- done
357
- return 0
358
- }
359
- count_registry
360
-
361
- # The path is the whole rest of the line, never $2: `worktree list --porcelain`
362
- # prints it raw, so any checkout living under a directory with a space in it —
363
- # ordinary on macOS — would otherwise be truncated at the first one.
364
- #
365
- # A newline in that path was the same truncation one byte further out: the plain
366
- # porcelain ends every attribute with one, so the record split and every
367
- # `substr($0,10)` below stopped at the newline. `wt_listing` reads `-z` and
368
- # swaps the separators, so a record ends where git says it ends.
369
- #
370
- # `|| die`, where this was a bare assignment: left bare the read died on `set -e`
371
- # under git's own diagnostic, with no line carrying the `release-ticket:` prefix
372
- # a caller greps stderr for — the very reason every lookup OVER this listing is
373
- # already guarded that way.
374
- #
375
- # A function, not inline, because the recount below needs this whole
376
- # pair — git's own read and the count derived from it — re-taken TOGETHER, not
377
- # just re-assigned in isolation. `linked` and `wt_list`/`wt_err` are this
378
- # function's OUTPUT, exactly as `registered` is `count_registry`'s.
379
- count_linked() {
380
- wt_listing || die "could not read the worktree list for #$issue: $wt_err"
381
- # The main worktree is always listed first and has no registry entry of its own,
382
- # hence the -1.
383
- #
384
- # awk, not `grep -c … || true`. `grep -c` exits 1 on zero matches — legitimate,
385
- # and `set -e` would read it as fatal — so a `|| true` has to absorb it, and
386
- # that same `|| true` absorbs a grep that could not RUN AT ALL just as happily.
387
- # The count is then the empty string, `$((listed - 1))` is -1 (measured), and
388
- # the mismatch report below blames `git worktree list` for a count no listing
389
- # can produce — sending the operator after git when the fault was a fork
390
- # failure. awk needs no such case separated out: the program contains no `exit`,
391
- # so it returns 0 whether or not anything matched, and every non-zero status is
392
- # a real failure. This script already removed exactly this shape elsewhere.
393
- #
394
- # What this count does NOT cover, stated because it reads as though it might: a
395
- # path with a newline in it never moved this number. The orphaned continuation
396
- # line the plain porcelain produced did not begin `worktree `, so `listed - 1`
397
- # still equalled `registered` and the cross-check AGREED with a read that had
398
- # truncated the path. Re-measured on a real linked worktree added at
399
- # `…/wt/fix-33<LF>slug`, git 2.50.1 (Apple Git-155):
400
- #
401
- # git worktree add -b fix/33-slug "../wt/$(printf 'fix-33\nslug')"
402
- # git worktree list --porcelain | awk '/^worktree /{n++} END{print n+0}'
403
- # ls .git/worktrees | wc -l
404
- #
405
- # `listed - 1` equals `registered` while the same listing's `substr($0,10)` hands
406
- # back `…/wt/fix-33`, a path `[ -d ]` says is not there. The blindness is the
407
- # point, not the arithmetic: the orphan line adds no `worktree ` line, so no
408
- # count over this listing can see the truncation. It is a count of records against
409
- # registry entries and catches an entry git DROPPED; the path inside a record it
410
- # does keep is `nl_path`'s to refuse, below. Under `-z` the count is now right by
411
- # construction — one `worktree ` line per record, whatever the path holds.
412
- listed=$(printf '%s\n' "$wt_list" | LC_ALL=C awk '/^worktree /{c++} END{print c+0}') ||
413
- die "could not count the worktrees git listed for #$issue"
414
- # The `|| die` above closes only the route where awk could not RUN. An empty but
415
- # SUCCESSFUL listing reaches the same -1: awk exits 0 printing `0`, the guard
416
- # cannot fire, and the mismatch report below blames `git worktree list` for the
417
- # very count the guard above exists to keep out of an operator's face. One
418
- # comparison closes it for every branch below at once. `-ge 1`, not `-gt 1`:
419
- # `listed=1` is the main checkout alone, `linked=0`, the ordinary repo with no
420
- # linked worktree at all — a guard that refused that would refuse most releases
421
- # in this repo. Real `git worktree list --porcelain` always prints the main
422
- # worktree, so reaching this needs a broken or shimmed git; the refusal
423
- # direction was already right, only the number was nonsense.
424
- [ "$listed" -ge 1 ] ||
425
- die "git listed no worktrees at all for #$issue — not even the main checkout, so the listing cannot be trusted"
426
- linked=$((listed - 1))
427
- }
428
- count_linked
429
- # Name the direction actually observed. The two disagreements have opposite
430
- # causes and send the reader to opposite places, so one message cannot serve
431
- # both: FEWER listed than registered is git silently dropping an entry it could
432
- # not read, which is the fault this whole check exists to catch. MORE listed
433
- # than registered is the reverse — the on-disk count is the stale read, a
434
- # sibling agent's `git worktree add` having landed between the two, which in a
435
- # parallel fleet is routine rather than exotic. Calling that "the listing is
436
- # incomplete" sends an operator hunting a permissions fault that is not there.
437
- # Recount before refusing. The registry scan above and git's listing
438
- # just taken are two reads at two different instants, not one atomic read, and
439
- # a sibling agent's `git worktree add` or `remove` landing in the gap makes the
440
- # two counts disagree with nothing actually wrong — measured against this
441
- # script directly: 3/100 dry-run releases aborted on this cross-check under a
442
- # throttled churner, 48-66/80 unthrottled, all with zero real faults among them.
443
- #
444
- # inflight.sh already recounts here into its own accumulate-and-continue
445
- # probe; this script's whole contract is `die` on any unmet precondition
446
- # instead, so the port keeps that shape rather than inheriting the accumulator.
447
- # Both copies recount BOTH `registered` and `linked`, which closes the same
448
- # window in each.
449
- # Re-taking `registered` alone leaves `linked` pinned to the FIRST `wt_listing`
450
- # call: a second sibling mutation landing after that call returns but before
451
- # the lone recount re-scans the registry inflates `registered` without touching
452
- # `linked`, which can flip which branch fires below and name the wrong cause —
453
- # reporting "the listing is incomplete" (implying a fault) for what is,
454
- # underneath, the same benign race the elif below already names correctly.
455
- # Verified against this script directly with a shim landing a second mutation
456
- # in exactly that window.
457
- #
458
- # A mutation landing between the FIRST count and git's listing is already
459
- # reflected in that listing, so the second count agrees with it — that is the
460
- # invariant the ORIGINAL pair above relies on. Recounting `registered` and
461
- # `linked` together, in the same order (`count_registry` first, `count_linked`
462
- # second, mirroring lines 364-434 above), gives the RECOUNT pair that same
463
- # invariant: escaping it needs a mutation inside the narrower window these two
464
- # calls open between themselves, not the whole span back to the first
465
- # `wt_listing` (measured on inflight.sh's copy while it was still
466
- # registered-only, the same shape of window: 1.99% -> 0.00% at 2 mutations/s,
467
- # 56.6% -> 1.29% saturated). A genuinely dropped entry is a standing state, not
468
- # a moment, so it survives every recount below and still refuses.
469
- #
470
- # The order is kept the same as the original pair for THIS argument's sake —
471
- # it lets the invariant above carry over instead of being re-derived for a
472
- # different order — not because a different order would be wrong. Verified
473
- # directly: swapping the recount to `count_linked` first then
474
- # `count_registry` still passes all four tests below (the two single-mutation
475
- # absorptions and the two regression cases) unchanged. Both functions
476
- # only read — neither writes anything the other observes — so whichever runs
477
- # first merely trades which of the two symmetric windows (before the first
478
- # call, between the two, after the second) a same-shaped mutation would need
479
- # to land in; it does not change whether the pair converges. Nothing below
480
- # depends on this order for correctness; it is kept fixed only so this
481
- # comment's own reasoning stays about one order instead of two.
482
- #
483
- # The unreadable-registry case above is unaffected: `count_registry`'s own
484
- # `[ -r ] && [ -x ]` guard on $wtroot dies the same way on every call, before
485
- # any recount is ever reached. The cannot-read-inside case is NOT unaffected
486
- # the same way — an entry inside $wtroot that `ls -A` cannot read is counted as
487
- # registered rather than dying (above), so a stale one survives every recount
488
- # too, but what fires on it is the mismatch below, not `count_registry` itself:
489
- # a different guard, a different exit code, under "the listing is incomplete" —
490
- # a cause that is not actually what happened.
491
- #
492
- # The recount pair above is itself two reads at two different instants, not
493
- # one atomic read: a THIRD sibling mutation landing between the recount's own
494
- # `count_registry` and `count_linked` calls escapes the single-recount
495
- # invariant the same way a second mutation once escaped the original pair,
496
- # and mislabels the same benign race under one of the two `die`s below.
497
- # Verified directly with a shim landing a
498
- # first mutation ahead of the initial listing and a second squarely inside the
499
- # recount's own gap: the wrong-direction message reproduced against this
500
- # script before the loop below existed.
501
- #
502
- # Closing that for good needs a single atomic snapshot of the registry and
503
- # git's listing together, and this repo has no primitive for one: git exposes
504
- # no combined read, and building one would mean a lock held not just here but
505
- # in every other script that mutates the registry (claim-ticket.sh, reap.sh,
506
- # and any other `git worktree add`/`remove` caller in the fleet) — a
507
- # repo-wide locking protocol this script has no authority to impose on
508
- # siblings it does not control, for a window that already needs three
509
- # precisely-timed mutations landing in gaps with no subprocess call in them at
510
- # all. Disproportionate, so not attempted.
511
- #
512
- # What IS cheap: recounting more than once. Each additional pass demands one
513
- # MORE precisely-timed sibling mutation to defeat, compounding against the
514
- # same mutation being both willing and able to land in an ever-later gap
515
- # within this one invocation — while a genuine corruption is a standing
516
- # state, not a moment, and still survives every pass and still refuses once
517
- # the loop below runs out. `recount_tries` bounds it at one more attempt than
518
- # the single recount already took (closing the specific residual without turning
519
- # this into an open-ended chase) rather than a
520
- # measured constant — sustained real churn settling in two extra reads is not
521
- # something this fixture can measure a percentage for, unlike the numbers
522
- # above.
523
- recount_tries=2
524
- while [ "$linked" -ne "$registered" ] && [ "$recount_tries" -gt 0 ]; do
525
- count_registry
526
- count_linked
527
- recount_tries=$((recount_tries - 1))
528
- done
529
- if [ "$linked" -lt "$registered" ]; then
530
- die "git listed $linked worktrees for $registered registry entries in $wtroot — the listing is incomplete, so no absence it reports can be trusted"
531
- elif [ "$linked" -gt "$registered" ]; then
532
- die "git listed $linked worktrees but only $registered registry entries were counted in $wtroot — the registry read missed entries git can see, so no absence it reports can be trusted"
293
+ # worktree.sh's `wt_counts` establishes it: it counts the registry entries on
294
+ # disk against the linked worktrees git listed, recounts before refusing, and
295
+ # names the direction a standing disagreement runs. Each refusal is this
296
+ # script's precondition `die`; a listing git could not produce at all names
297
+ # git's own cause. Absent entirely is fine and answers nothing: zero registry
298
+ # entries against the main worktree alone is a match, so the release goes
299
+ # through. The listing it validated is the `$wt_list` every lookup below scans.
300
+ if wt_counts; then :; else
301
+ case $? in
302
+ 2) die "could not read the worktree list for #$issue: $wt_why" ;;
303
+ *) die "$wt_why" ;;
304
+ esac
533
305
  fi
534
306
 
535
307
  # Every lookup ASSIGNED from the listing is guarded, for the reason the worktree
536
308
  # counter above is: left bare, an awk that could not answer ends the run on its
537
309
  # own diagnostic, with no line carrying the `release-ticket:` prefix a caller
538
- # greps stderr for. The lookups OVER it are guarded too, but never this way —
310
+ # greps stderr for. The branch lookup is worktree.sh's `wt_find_branch`, which
311
+ # refuses rather than answering "no worktree" for a scan it could not finish.
312
+ # The lookups OVER it are guarded too, but never this way —
539
313
  # `locked` and `unresolved_head` below read the same `$wt_list` through awk and
540
314
  # answer THROUGH its exit status, where telling "could not run" from "no match"
541
315
  # needs more than a `|| die`. Each captures that status itself and refuses above
@@ -556,11 +330,23 @@ fi
556
330
  # absent worktree stays the answer the checks below expect. The predicates are
557
331
  # the opposite case — their `exit` is how they answer at all — which is why the
558
332
  # same sentence cannot be written about them and why their guard is not this one.
559
- wt=$(printf '%s\n' "$wt_list" |
560
- awk -v b="refs/heads/$branch" '/^worktree /{w=substr($0,10);n++} /^branch /&&$2==b&&n>1{print w}') ||
561
- die "could not read the worktree git listed for #$issue"
562
333
  main_branch=$(printf '%s\n' "$wt_list" | awk '/^worktree /{n++} n==1&&/^branch /{print $2; exit}') ||
563
334
  die "could not read the main checkout's branch from git's listing for #$issue"
335
+ # Only a LINKED worktree is ours, and `wt_find_branch` answers for every record
336
+ # the main checkout included, one path per line in listing order. The main
337
+ # checkout is always the first record, so when it holds the branch its path is
338
+ # the first line, and it is dropped here; the main-checkout blocker below
339
+ # refuses that shape on its own.
340
+ wt_find_branch "$branch" || die "$wt_why"
341
+ wt=$wt_path
342
+ if [ "$main_branch" = "refs/heads/$branch" ]; then
343
+ case $wt in
344
+ *"
345
+ "*) wt=${wt#*"
346
+ "} ;;
347
+ *) wt= ;;
348
+ esac
349
+ fi
564
350
  # The main checkout's path, and the anchor the orphan probe below reconstructs a
565
351
  # claim's directory from. Off git's own listing rather than $PWD: this script is
566
352
  # routinely run from inside a member's worktree, where a cwd-relative
@@ -651,7 +437,7 @@ branch_j=$(jstr "$branch") && branch_rw=$(jrewritten "$branch") \
651
437
  # fails (measured, git 2.50.1). Its exit code therefore answers for neither, and
652
438
  # `nothing landed` over a cleared registration was a positive assertion that was
653
439
  # false. So `wt_outcome` carries a measured release outcome — CONTEXT.md's four
654
- # states — taken by `release_outcome` after the call rather than read off its rc.
440
+ # states — taken by worktree.sh's `wt_outcome` after the call rather than read off its rc.
655
441
  #
656
442
  # `Unreleased` to start because nothing has been attempted yet, and the line is
657
443
  # not printed at all when this claim has no worktree of ours: a state whose
@@ -807,129 +593,16 @@ unresolved_head() {
807
593
  return 1
808
594
  }
809
595
 
810
- # Is anything at all occupying $1? Not the same question as `gone`, which asks
811
- # whether an absence is established; this one asks whether the path is taken.
812
- # `-e` alone FOLLOWS symlinks, so a DANGLING one reads as absent while it still
813
- # occupies the path and still fails the next `git worktree add` (`fatal: '...'
814
- # already exists`) — and that is residue this very script can leave: where a
815
- # symlink POINTS AT the registered worktree directory, `git worktree remove`
816
- # deletes that directory and returns 0, leaving the link behind and now dangling.
817
- # NOT the symlink standing in for the directory: that is the rc-255 row of the
818
- # table below, and there the target survives emptied, so `-e` alone already sees
819
- # it. Both measured, git 2.50.1. Same reason the non-directory precondition
820
- # below tests -L separately.
821
- #
822
- # Neither shape impeaches the rc-0 fast path at the removal, so nothing there
823
- # needs re-measuring: no shape measured returns 0 with the path git was asked to
824
- # delete still occupied — the dangling residue sits on the LINK, which is a
825
- # different path and reaches this predicate through the orphan probe.
826
- occupied() { [ -e "$1" ] || [ -L "$1" ]; }
827
-
828
- # The registration PROBE at the heart of the arm below: is $1 still listed in
829
- # $now, the fresh post-removal listing? `locked` and `unresolved_head`
830
- # read their own awk's exit status STRAIGHT as the answer — 0 true, 1 false —
831
- # so an awk that could not run (rc >= 2: killed, OOM, a broken interpreter) and
832
- # an awk that ran fine and found no match (rc 1, the genuine "not registered")
833
- # both land on the false side of the same elif, and it was measured what that
834
- # fold did here: with nothing left to distinguish them, the run fell through to
835
- # `elif occupied "$1"; then wt_outcome=Deregistered` and wrote a false
836
- # Deregistered into the JSON receipt for a registration nothing established was
837
- # actually cleared.
838
- #
839
- # `locked` and `unresolved_head`'s own fix is not enough copied
840
- # verbatim: both `die` on an awk that could not run, which is right for a
841
- # precondition gate — abort before any mutation is attempted — and wrong here.
842
- # `release_outcome` runs AFTER `git worktree remove` has already run, is called
843
- # as a bare statement rather than inside a `$( )`, and its caller reads
844
- # `$wt_outcome` right after the call returns; a `die` here would abort the
845
- # function with nothing set — worse than the defect it replaces. Nor can the
846
- # status be captured with a bare pipeline followed by a separate `reg=$?` line:
847
- # measured, that does not survive this script's `set -eu` either — `set -e`
848
- # kills the script on the pipeline itself before the assignment is ever
849
- # reached. So this stays a boolean predicate in the `locked`/`unresolved_head`
850
- # shape — a non-final AND-OR element (`… && return 0`), never a bare pipeline
851
- # whose status a later line reads — but the "could not run" case is carried out
852
- # through `$wt_why` instead of `die`, for `release_outcome` to route to
853
- # Indeterminate exactly as it already does for a listing git could not
854
- # produce, one arm up. 0 registered, 1 genuinely not registered (`$wt_why`
855
- # stays empty, already cleared by the caller before this runs), anything else
856
- # could not be told apart from "not registered" by rc alone (`$wt_why` is set
857
- # to say so, straight into the field the caller already reserves for it).
858
- ro_registered() {
859
- printf '%s\n' "$now" |
860
- P="$1" awk '/^worktree /{if (substr($0,10)==ENVIRON["P"]) f=1} END{exit !f}' && return 0
861
- [ $? = 1 ] || wt_why="could not tell whether $1 is still registered for #$issue"
862
- return 1
863
- }
864
-
865
- # Which release outcome does $1 hold after a `git worktree remove` that refused?
866
- # Measured, never inferred from the rc — recording the rc instead of what landed is the defect this exists to remove.
867
- # git drops the registration BEFORE the directory and does not restore it when
868
- # the directory delete fails, so one call has three landing shapes and the exit
869
- # code separates none of them. All measured on git 2.50.1:
870
- #
871
- # dirty worktree rc 128, registration and directory kept
872
- # symlink standing in for the dir rc 255, registration CLEARED, path kept
873
- # unwritable .git/worktrees rc 255, registration and directory gone
874
- #
875
- # A FRESH listing, never `wt_list`: that one was captured before any mutation,
876
- # so answering from it would re-read the state this function exists to
877
- # re-measure. Read with the same ENVIRON-keyed awk `locked` uses, and for the
878
- # same reason — a `-v` assignment mangles a backslash in the path, and the
879
- # comparison then falls to the permissive answer.
880
- #
881
- # The tri-valued directory probe is `occupied` composed with `gone`, exactly the
882
- # pairing `gone`'s own contract prescribes for a caller that needs present and
883
- # cannot-stat apart. No second predicate and no third value inside `gone`: it is
884
- # now a single definition in worktree.sh that every caller in the fleet shares,
885
- # so a third value added for this one caller lands in all of them — and this file
886
- # already carries two answers on unreadable worktrees from a concept that
887
- # got duplicated.
888
- #
889
- # Only the four named states, so the two cells CONTEXT.md has no name for are
890
- # not asserted: a registration that survived a directory that did not is
891
- # reported Indeterminate rather than squeezed into Unreleased, whose definition
892
- # is that BOTH are still present.
893
- # Writes `$wt_outcome`, and `$wt_why` whenever the answer is Indeterminate for a
894
- # reason git named. It does NOT echo the state: a `$( )` around the call would
895
- # put the whole body in a subshell, and the cause — which `wt_listing` leaves in
896
- # `$wt_err` — would die with it. That is not hypothetical, it is what this
897
- # function did: the one case where the headline degrades to "what landed could
898
- # not be measured" was the one case whose cause never reached the operator, on
899
- # stderr or in the receipt. Assigning globals is what carries it out.
900
- release_outcome() {
901
- # `wt_listing` writes the shared `$wt_list`, and this function must not disturb
902
- # the pre-mutation capture the guards above read off it. Saved and put back in
903
- # the same breath — and now load-bearing rather than defensive, because without
904
- # the subshell there is nothing else keeping the two reads apart.
905
- ro_prior=$wt_list
906
- ro_read=true
907
- wt_listing || ro_read=false
908
- ro_err=$wt_err
909
- now=$wt_list
910
- wt_list=$ro_prior
911
- wt_why=
912
- if [ "$ro_read" = false ]; then
913
- # The listing is how the registration is read, so a listing git could not
914
- # produce leaves the registration unknown — not absent. `$ro_err` is git's
915
- # own prose for why it could not, flattened to one line for the receipt.
916
- wt_outcome=Indeterminate
917
- wt_why=$(printf '%s' "$ro_err" | tr '\n' ' ')
918
- elif ro_registered "$1"; then
919
- if occupied "$1"; then wt_outcome=Unreleased; else wt_outcome=Indeterminate; fi
920
- elif [ -n "$wt_why" ]; then
921
- # awk could not run at all (rc >= 2), which is a different fact from
922
- # "ran fine and found nothing" — the latter alone means Deregistered is on
923
- # the table below.
924
- wt_outcome=Indeterminate
925
- elif occupied "$1"; then
926
- wt_outcome=Deregistered
927
- elif gone "$1"; then
928
- wt_outcome=Released
929
- else
930
- wt_outcome=Indeterminate
931
- fi
932
- }
596
+ # Whether a path is taken at all (`wt_occupied`, which a dangling link still
597
+ # answers true for) and which release outcome a refused `git worktree remove`
598
+ # left behind (`wt_outcome`) are worktree.sh's, with the measurements behind
599
+ # both. The symlink standing in for a worktree directory is the rc-255 row of
600
+ # `wt_outcome`'s table, and its target survives emptied, so `-e` alone already
601
+ # sees it; the non-directory precondition below tests `-L` for the same reason
602
+ # `wt_occupied` does. No shape measured returns 0 from the removal with the
603
+ # path git was asked to delete still occupied — the dangling residue sits on
604
+ # the LINK, which is a different path and reaches `wt_occupied` through the
605
+ # orphan probe — so the rc-0 fast path records `Released` without re-measuring.
933
606
 
934
607
  # A path holding a newline, refused rather than acted on. `wt_listing` delivers
935
608
  # it whole with the newline substituted, and the substituted byte is one no
@@ -965,7 +638,7 @@ fi
965
638
  # and rewrites the entry's `gitdir` file to follow it, but never renames the
966
639
  # ENTRY (both measured, git 2.50.1) — so a moved claim whose HEAD git cannot
967
640
  # resolve, invisible to `wt` and to the suffix key alike, is still reachable
968
- # through `$wtroot/$issue-$slug/gitdir`. That file holds `<worktree path>/.git`,
641
+ # through `$wt_root/$issue-$slug/gitdir`. That file holds `<worktree path>/.git`,
969
642
  # and the path git prints in the porcelain is its content with `/.git` stripped,
970
643
  # echoed VERBATIM with no canonicalisation of its own (measured: a hand-written
971
644
  # `gitdir` naming a path through a symlink is printed back through the symlink),
@@ -1073,22 +746,22 @@ shquote() {
1073
746
  # pasted into a shell and a path is not shell text: under double quotes a `$`
1074
747
  # in the path expands again when the operator runs it and the search comes back
1075
748
  # EMPTY at rc 1 — no entry reported while the entry is right there, this arm's
1076
- # own defect one step further out — and an unquoted `$wtroot/*/gitdir` splits
749
+ # own defect one step further out — and an unquoted `$wt_root/*/gitdir` splits
1077
750
  # on a space. Through `shquote`, because quoting a path is not the same as
1078
751
  # wrapping it in quotes: the one character that defeats a quoted string is the
1079
752
  # quote, and git accepts one in a worktree directory name.
1080
753
  #
1081
- # Empty on the entry key, which read `$wtroot/$issue-$slug/gitdir` itself and
754
+ # Empty on the entry key, which read `$wt_root/$issue-$slug/gitdir` itself and
1082
755
  # puts that name in `$stray_own`: searching for what it already measured would
1083
756
  # be the step backwards. Assigned before that key can answer, so `$stray` here
1084
757
  # is the suffix key's or nothing.
1085
758
  shquote "$stray/.git"
1086
759
  stray_needle=$shq
1087
- shquote "$wtroot"
760
+ shquote "$wt_root"
1088
761
  stray_find="; find that entry with: grep -Fxl $stray_needle $shq/*/gitdir"
1089
- if [ -z "$wt" ] && [ -z "$stray" ] && [ -r "$wtroot/$issue-$slug/gitdir" ]; then
1090
- entry_wt=$(cat "$wtroot/$issue-$slug/gitdir") ||
1091
- die "could not read the worktree registry entry $wtroot/$issue-$slug/gitdir for #$issue"
762
+ if [ -z "$wt" ] && [ -z "$stray" ] && [ -r "$wt_root/$issue-$slug/gitdir" ]; then
763
+ entry_wt=$(cat "$wt_root/$issue-$slug/gitdir") ||
764
+ die "could not read the worktree registry entry $wt_root/$issue-$slug/gitdir for #$issue"
1092
765
  entry_wt=${entry_wt%/.git}
1093
766
  entry_stray=$(printf '%s\n' "$wt_list" |
1094
767
  E="$entry_wt" awk '/^worktree /{n++; p=substr($0,10)
@@ -1174,7 +847,7 @@ if [ -z "$wt" ] && [ -n "$stray" ]; then
1174
847
  elif gone "$stray"; then
1175
848
  block "worktree $stray $stray_own and its directory is gone — git worktree prune to clear the registration"
1176
849
  elif unresolved_head "$stray"; then
1177
- block "worktree $stray $stray_own but git could not read its HEAD, so its branch is unknown — inspect its entry's HEAD file under $wtroot by hand$stray_find"
850
+ block "worktree $stray $stray_own but git could not read its HEAD, so its branch is unknown — inspect its entry's HEAD file under $wt_root by hand$stray_find"
1178
851
  else
1179
852
  block "worktree $stray $stray_own but is not on $branch — release it by hand"
1180
853
  fi
@@ -1210,12 +883,12 @@ fi
1210
883
  # Nor an automatic delete: a refusal is a finding to report, and nothing here
1211
884
  # has inspected what is in that directory.
1212
885
  #
1213
- # Absence is ESTABLISHED by `gone`, never inferred from `occupied` alone, for
886
+ # Absence is ESTABLISHED by `gone`, never inferred from `wt_occupied` alone, for
1214
887
  # the reason the dirty check gives: read as "no orphan", a `.worktrees` this
1215
888
  # script may not search would release the claim over one.
1216
889
  if [ -z "$wt" ] && [ -z "$stray" ]; then
1217
890
  orphan="$main_wt/.worktrees/$issue-$slug"
1218
- if occupied "$orphan"; then
891
+ if wt_occupied "$orphan"; then
1219
892
  block "worktree directory $orphan is this claim's and has no registration — inspect it and remove the directory by hand"
1220
893
  elif ! gone "$orphan"; then
1221
894
  block "cannot tell whether an orphaned worktree directory is at $orphan, so whether #$issue can be released is unknown"
@@ -1237,7 +910,7 @@ if [ -z "$wt" ] && [ -z "$stray" ]; then
1237
910
  #
1238
911
  # The `-z "$blockers"` conjunct is load-bearing, not dead weight (a prior
1239
912
  # round of this comment argued the opposite and was wrong): `$blockers` can
1240
- # be non-empty here even though this arm's own occupied/gone siblings above
913
+ # be non-empty here even though this arm's own wt_occupied/gone siblings above
1241
914
  # did not fire. `git checkout --orphan release/5-foo` on the MAIN checkout
1242
915
  # points HEAD at refs/heads/release/5-foo before any commit exists, so
1243
916
  # `main_branch` (read off `git worktree list --porcelain`) already equals
@@ -1329,7 +1002,7 @@ fi
1329
1002
  # branch deleted, label dropped, exit 0, `"blockers":[]` — with the member's
1330
1003
  # uncommitted work still on disk and now orphaned.
1331
1004
  #
1332
- # `occupied`, where this read `[ ! -e "$wt" ]`: `gone` reports a path that EXISTS
1005
+ # `wt_occupied`, where this read `[ ! -e "$wt" ]`: `gone` reports a path that EXISTS
1333
1006
  # as not-established-absent, which is the same answer it gives for one it cannot
1334
1007
  # stat, and only the second is unknown — so the existence test has to be the one
1335
1008
  # that agrees with `gone` about what "exists" means. It did not. Every `test`
@@ -1339,18 +1012,18 @@ fi
1339
1012
  # exactly, and which the stand-in blocker below already answers. Measured on
1340
1013
  # the release-ticket tests' own dangling-link fixture, which is what caught it.
1341
1014
  #
1342
- # `! occupied && ! gone` is the tri-valued pairing `gone`'s contract prescribes,
1343
- # and the same one `release_outcome` composes: present or link-present is not
1015
+ # `! wt_occupied && ! gone` is the tri-valued pairing `gone`'s contract prescribes,
1016
+ # and the same one `wt_outcome` composes: present or link-present is not
1344
1017
  # unknown, established-absent is not unknown, and what is left over is.
1345
1018
  #
1346
- # `! nl_path` for the same reason the dangling link needed `occupied`, and it is
1019
+ # `! nl_path` for the same reason the dangling link needed `wt_occupied`, and it is
1347
1020
  # the same trap a second time: `gone` now refuses a substituted path too,
1348
1021
  # so without this exemption the pairing reads it as "cannot tell" and dies — for
1349
1022
  # a path the newline blocker above has already answered in the operator's own
1350
1023
  # terms, downgrading a receipt-carrying `NOT released` verdict to an exit-2 die
1351
1024
  # that says nothing the receipt did not. Measured on a worktree registered at a
1352
1025
  # path holding a newline.
1353
- if [ -n "$wt" ] && ! nl_path "$wt" && ! occupied "$wt" && ! gone "$wt"; then
1026
+ if [ -n "$wt" ] && ! nl_path "$wt" && ! wt_occupied "$wt" && ! gone "$wt"; then
1354
1027
  die "cannot tell whether $wt exists, so whether it holds uncommitted work is unknown"
1355
1028
  fi
1356
1029
 
@@ -1457,52 +1130,25 @@ if [ -n "$wt" ] && [ -d "$wt" ]; then
1457
1130
  # tampering, `git worktree` never writes one, and a stricter check here was
1458
1131
  # ruled not worth its code.
1459
1132
  #
1460
- # Compared as a DIRECTORY (`-ef`, same device and inode), never as a string,
1461
- # or this false-refuses a HEALTHY worktree: `$wt` is the path `worktree list
1462
- # --porcelain` echoes and `--show-toplevel` is git's own resolved spelling,
1463
- # and the two legitimately differ for one and the same directory. A parent
1464
- # that was a plain dir at `worktree add` time and is a symlink now leaves the
1465
- # listed path non-canonical while git's answer is resolved (measured). And a
1466
- # path holding a Unicode NFD-composed name (`cafe` + U+0301) is listed
1467
- # PRECOMPOSED by the `core.precomposeunicode` git writes into every new repo
1468
- # on macOS, while `--show-toplevel` answers the on-disk NFD bytes — visually
1469
- # identical, byte-different, one directory (measured, git 2.50.1, Apple
1470
- # Git-155; reap.sh's copy of this compare compares as a directory too). Under bash —
1471
- # macOS's own `/bin/sh` — `cd "$wt" && pwd -P` canonicalised only the
1472
- # first: it echoes the spelling it was given, so a byte compare against it
1473
- # refused every healthy NFD worktree at exit 2 there; dash's own `pwd -P`
1474
- # resolves through to the on-disk NFD bytes instead, so that shell's old
1475
- # byte compare would not have refused the same worktree. `-ef`
1476
- # answers "same directory" for both and for any other spelling the
1477
- # filesystem aliases, and still refuses every redirect above — each names a
1478
- # different directory. POSIX.1-2017's `test` does not define `-ef`; it is a
1479
- # ksh-derived extension bash, dash and BSD sh share, and POSIX.1-2024 adds it.
1480
- # A `[` that cannot evaluate it fails, and so does a `--show-toplevel` naming
1481
- # a path that does not exist: both are the refusing direction.
1482
- #
1483
- # `&& echo x` inside the substitution, then `%?x`: `$(...)` strips EVERY
1484
- # trailing newline, so a `core.worktree` naming a sibling directory called
1485
- # `$wt` plus a newline byte — git accepts one as an ordinary path character —
1486
- # would otherwise name `$wt` itself and pass the redirect, the dirty check
1487
- # then reading that sibling's clean copy over $wt's work (measured;
1488
- # the shape no-undo-audit.sh closed the same way, and reap.sh's copy
1489
- # of this compare). The sentinel leaves `$(...)` only git's own
1490
- # terminating newline to strip.
1133
+ # worktree.sh's `wt_linkage` asks it, as a DIRECTORY compare (`-ef`) — a
1134
+ # string compare false-refuses a HEALTHY worktree whose parent became a
1135
+ # symlink or whose name is NFD — with a sentinel against a newline-suffixed
1136
+ # sibling; its comment carries the measurements. Under bash — macOS's own
1137
+ # `/bin/sh` — `cd "$wt" && pwd -P` canonicalised only the symlinked parent:
1138
+ # it echoes the spelling it was given, so a byte compare against it refused
1139
+ # every healthy NFD worktree at exit 2 there; dash's own `pwd -P` resolves
1140
+ # through to the on-disk NFD bytes instead.
1491
1141
  #
1492
1142
  # A $wt whose own leaf was replaced by a symlink — never `git worktree add`'s
1493
- # own shape, hand tampering only — still reaches this compare, because the
1143
+ # own shape, hand tampering only — still reaches this check, because the
1494
1144
  # `-L` guard just above `block`s rather than `die`s (see its own comment).
1495
- # `-ef` follows the link, and `--show-toplevel` resolves through it to the
1496
- # same real, possibly newline-suffixed, directory, so the two compare equal
1497
- # and that case stays exactly as blocked — "not a directory" at exit 1 — never
1498
- # a fabricated "does not point at itself" mismatch at exit 2.
1499
- if [ -f "$wt/.git" ]; then
1500
- toplevel=$(git -C "$wt" rev-parse --show-toplevel && echo x) ||
1501
- die "cannot read the git repository at $wt (its .git file or the gitdir it names), so whether it holds uncommitted work is unknown"
1502
- toplevel=${toplevel%?x}
1503
- # shellcheck disable=SC3013 # -ef is a ksh-derived extension bash/dash/BSD sh share, base in POSIX.1-2024; this file targets dash too and -ef is verified there
1504
- [ "$toplevel" -ef "$wt" ] ||
1505
- die "$wt's .git does not point at $wt — it resolves to $toplevel — so whether it holds uncommitted work is unknown"
1145
+ # A link standing in for this claim's own renamed directory reaches this
1146
+ # claim's admin dir, whose back-pointer names $wt, and `-ef` resolves both
1147
+ # sides to the same real directory — so that case stays exactly as blocked,
1148
+ # "not a directory" at exit 1, never a fabricated mismatch at exit 2. A link
1149
+ # to ANOTHER worktree reaches that worktree's admin dir and is refused here.
1150
+ if [ -f "$wt/.git" ] && ! wt_linkage "$wt"; then
1151
+ die "$wt_why, so whether it holds uncommitted work is unknown"
1506
1152
  fi
1507
1153
 
1508
1154
  # Same reason: folded-in stderr would be counted as uncommitted changes.
@@ -1531,6 +1177,24 @@ if [ -n "$wt" ] && [ -d "$wt" ]; then
1531
1177
  [ "$n" -eq 0 ] || block "worktree $wt has $n uncommitted change(s)"
1532
1178
  fi
1533
1179
 
1180
+ # The release removes `$wt`, and a run standing inside it — the member whose
1181
+ # claim this is, releasing from its own shell — would have its cwd deleted
1182
+ # under it, after which every git call dies with `fatal: Unable to read current
1183
+ # working directory` (measured on reap.sh, whose sweeps refuse the same path).
1184
+ # Asked of the directory, not the string, by worktree.sh's `wt_holds_cwd`,
1185
+ # which also answers true when `[` cannot evaluate the compare. `pwd -P`, not
1186
+ # `$PWD`: an inherited `PWD` naming some other directory is one POSIX leaves
1187
+ # the shell free to keep. The sentinel keeps a trailing newline in the path.
1188
+ # A blocker, not a die: what stands in the way is known, and one `cd` away.
1189
+ if [ -n "$wt" ]; then
1190
+ rt_cwd=$(pwd -P && echo x) ||
1191
+ die "cannot read the working directory this run was started in, so whether releasing $wt would delete it is unknown"
1192
+ rt_cwd=${rt_cwd%?x}
1193
+ if wt_holds_cwd "$wt" "$rt_cwd"; then
1194
+ block "worktree $wt holds the working directory this run was started in — removing it would delete the cwd every git call after it needs; rerun from outside it"
1195
+ fi
1196
+ fi
1197
+
1534
1198
  # Report the blockers before asking GitHub anything. The answer cannot change —
1535
1199
  # every artefact stays put either way — and reaching for the tracker first turns
1536
1200
  # an offline blocked claim into an unanswerable one, burying the finding the
@@ -1597,7 +1261,7 @@ else
1597
1261
  # failure. Re-measuring here would also make the happy path answerable by a
1598
1262
  # probe that can return Indeterminate, refusing releases that plainly worked.
1599
1263
  if ! err=$(git worktree remove "$wt" 2>&1); then
1600
- release_outcome "$wt"
1264
+ wt_outcome "$wt"
1601
1265
  halt "git worktree remove refused $wt: $(printf '%s' "$err" | tr '\n' ' ')"
1602
1266
  fi
1603
1267
  wt_outcome=Released