fix(scripts): row 211 R4 busy check ancestor reads, denied lookups, sourced tests (#1709)

From the GPT review of R3 (row211-r3-gpt.md, R1 to R3 and Check 5):

- slot-busy.sh: every ancestor status read must give a numeric PPid. A
  failed ancestor or cmdline read counts as gone only when a listing of
  the proc root succeeds and lacks the pid; anything else is exit 2 with
  no CLEAR. The [ -d ] and [ -e ] tests, which can't tell a denied lookup
  from a missing one, are gone.
- slot-busy.sh: a shell-snapshot wrapper whose text sources (source or .)
  a gate, test or mutant script is BUSY; one that only runs children is
  still skipped.
- install-git-hooks.sh: the hook is staged in a mktemp file in the hooks
  directory, not a pid-named path.
- mutants.sh: exit 1 on a survivor, NO MATCH, COPY FAILED or a timeout;
  nine new mutants, 30 of 30 killed.

test-git-guard.sh: RED 90/14, GREEN 105/0. BUILD.md R4 has the details,
including a correction to the R3 bind-mount note.
This commit is contained in:
jason.woltje committed 2026-11-02 14:11:47 -06:00
1 parent 7d03f212db
commit fd1aa8cc81
8 files changed
+517 -35

No files matched your search

+12 -5
View File
@@ -11,8 +11,10 @@
# resolves anywhere but the common directory's hooks, and when a different
# pre-push is installed, unless --force-install is given. It never edits an
# installed hook in place:
# a new hook is linked in, a replacement is renamed over the old one. A canary
# then runs the installed hook through git and needs a delete refused.
# a new hook is linked in, a replacement is renamed over the old one. The
# bytes go first into a new file that mktemp makes in the hooks directory, so
# nothing planted there in advance is written through. A canary then runs the
# installed hook through git and needs a delete refused.
#
# Exit codes: 0 installed (or already the same bytes); 1 failed; 2 refused;
# 4 usage. Installing in the canonical checkout is its own step with the lead.
@@ -71,7 +73,12 @@ own_hooks
if [ -L "$HOOK" ]; then die 2 "refused: $HOOK is a symlink; remove it by hand if it is not wanted"; fi
mkdir -p "$HOOKS" || die 1 "cannot create $HOOKS"
own_hooks
tmp="$HOOKS/.pre-push.install-git-hooks.$$"
# The hook's bytes in a new file that mktemp creates (O_EXCL, a random name)
# in the checked hooks directory, made only once a copy is due.
stage() {
tmp=$(mktemp "$HOOKS/.pre-push.install-git-hooks.XXXXXX") || die 1 "cannot create a temporary file in $HOOKS"
cp -- "$SRC" "$tmp" && chmod 0755 "$tmp" || { rm -f -- "$tmp"; die 1 "cannot write $tmp"; }
}
if [ -e "$HOOK" ]; then
[ -f "$HOOK" ] || die 2 "refused: $HOOK is not a regular file"
if cmp -s "$SRC" "$HOOK"; then
@@ -79,13 +86,13 @@ if [ -e "$HOOK" ]; then
note="the same bytes were already there"
else
[ "$FORCE" = 1 ] || die 2 "refused: a different pre-push hook is installed at $HOOK; compare it with $SRC, and pass --force-install to replace it"
cp -- "$SRC" "$tmp" && chmod 0755 "$tmp" || { rm -f -- "$tmp"; die 1 "cannot write $tmp"; }
stage
# rename() swaps the whole file; the old hook's bytes are never edited.
mv -f -- "$tmp" "$HOOK" || { rm -f -- "$tmp"; die 1 "cannot replace $HOOK"; }
note="replaced a different hook (--force-install)"
fi
else
cp -- "$SRC" "$tmp" && chmod 0755 "$tmp" || { rm -f -- "$tmp"; die 1 "cannot write $tmp"; }
stage
# link() never replaces, so a hook that appeared meanwhile is kept.
if ! ln -- "$tmp" "$HOOK" 2>/dev/null; then rm -f -- "$tmp"; die 2 "refused: a pre-push hook appeared at $HOOK while installing"; fi
rm -f -- "$tmp"
+52 -17
View File
@@ -6,9 +6,10 @@
# scripts/slot-busy.sh [--proc-root DIR]
#
# It prints one BUSY line per matching process, or one CLEAR line. Exit codes:
# 0 CLEAR, 1 BUSY, 2 its own error, which includes a process it can't read
# that hasn't exited (it still prints any BUSY lines, never CLEAR). Chain it
# on the exit code, never on a filter of its output:
# 0 CLEAR, 1 BUSY, 2 its own error, which includes a process or an ancestor
# it can't read that isn't confirmed gone (it still prints any BUSY lines,
# never CLEAR). Gone means a listing of the proc root that succeeds and lacks
# the pid. Chain it on the exit code, never on a filter of its output:
# bash scripts/slot-busy.sh > LOG && gate (set -o pipefail if piped)
# never `bash scripts/slot-busy.sh | tail -1 && gate`, which runs while BUSY.
#
@@ -18,9 +19,12 @@
# agent harness's shell-snapshot wrapper (`bash -c 'source .../shell-snapshots/
# snapshot-*.sh ...'`), whose command line is the agent's command text; that
# shell's real children are still matched, and so is any other command line
# that only names a shell-snapshots path. It reads only /proc/<pid>/cmdline,
# the cwd link and its own and its ancestors' /proc/<pid>/status, never a
# file under a seat's directory.
# that only names a shell-snapshots path. A wrapper whose text after the
# snapshot source also sources (`source` or `.`) a gate, test or mutant
# script runs it in its own shell, so that wrapper is BUSY. It reads only
# /proc/<pid>/cmdline, the cwd link, its own and its ancestors'
# /proc/<pid>/status and, after a failed read, the proc root's listing, never
# a file under a seat's directory.
# --proc-root names a stand-in for /proc in the selftests.
set -uo pipefail
@@ -34,16 +38,29 @@ while [ $# -gt 0 ]; do
done
[ -d "$PROC" ] || { echo "slot-busy: error: $PROC is not a directory" >&2; exit 2; }
# This process and its ancestors, from the real /proc. Its own status must
# read and name a parent; a failed read further up only ends the walk, which
# can give a false BUSY but never a false CLEAR.
# gone ROOT PID: true only when ROOT lists and PID isn't in the listing. A
# failed or denied listing is false: a lookup that fails can't tell an exited
# process from one it may not see. `ls -f` reads the names only, no stat.
gone() {
local names
names=$(ls -f -- "$1" 2>/dev/null) || return 1
[[ $'\n'"$names"$'\n' != *$'\n'"$2"$'\n'* ]]
}
# This process and its ancestors, from the real /proc. Every status read must
# give a numeric PPid. An ancestor that exited (confirmed gone) ends the walk;
# any other failed read is an error, never a shorter walk.
declare -A SKIP=()
p=$(awk '/^PPid:/{print $2; exit}' "/proc/$$/status" 2>/dev/null) && [[ "$p" =~ ^[0-9]+$ ]] \
|| { echo "slot-busy: error: cannot read its own process" >&2; exit 2; }
SKIP[$$]=1
while [ -n "$p" ] && [ "$p" != 0 ]; do
while [ "$p" != 0 ]; do
SKIP[$p]=1
p=$(awk '/^PPid:/{print $2; exit}' "/proc/$p/status" 2>/dev/null) || p=""
if ! q=$(awk '/^PPid:/{print $2; exit}' "/proc/$p/status" 2>/dev/null) || ! [[ "$q" =~ ^[0-9]+$ ]]; then
gone /proc "$p" && break
echo "slot-busy: error: cannot read the status of ancestor $p" >&2; exit 2
fi
p=$q
done
RE_NODE='[ /]node[^ ]* (.* )?--test '
@@ -53,9 +70,25 @@ RE_COMPOSE='[ /]docker( compose|-compose) '
# The wrapper's shape: a shell, its options with -c, then `source` (or `.`)
# of a shell-snapshots/snapshot-*.sh file as the first command.
RE_SNAPSHOT='^ (/[^ ]*/)?(ba|z)?sh( -[a-zA-Z]+)* -[a-zA-Z]*c[a-zA-Z]*( -[a-zA-Z]+)* (source|\.) [^ ]*/shell-snapshots/snapshot-[^ /]*\.sh '
# A sourced file in the wrapper's text: `source` or `.` after the start, a
# blank, a shell operator or a quote, then blanks, an optional `--`, an
# optional opening quote and the path, up to a blank, operator or quote.
RE_SOURCED="(^|[[:space:];&|(){}\`'\"])(source|\\.)[[:space:]]+(--[[:space:]]+)?['\"]?([^[:space:];&|<>(){}\`'\"]+)"
sourced() { # $1: the wrapper's text after the snapshot source. Prints the rule.
local rest=$1 m f
while [[ "$rest" =~ $RE_SOURCED ]]; do
# Saved first: the rule tests below reset BASH_REMATCH.
m=${BASH_REMATCH[0]} f=" ${BASH_REMATCH[4]} "
if [[ "$f" =~ $RE_GATE ]]; then echo "sourced gate"; return 0; fi
if [[ "$f" == *mutant* ]]; then echo "sourced mutant"; return 0; fi
if [[ "$f" =~ $RE_TEST ]]; then echo "sourced test script"; return 0; fi
rest=${rest#*"$m"}
done
return 1
}
match() { # $1: the command line, NULs as spaces. Prints the rule that matched.
local c=" $1 "
if [[ "$c" =~ $RE_SNAPSHOT ]]; then return 1; fi
if [[ "$c" =~ $RE_SNAPSHOT ]]; then sourced "${c:${#BASH_REMATCH[0]}}"; return; fi
if [[ "$c" =~ $RE_NODE ]]; then echo "node --test"; return 0; fi
if [[ "$c" =~ $RE_GATE ]]; then echo "gate"; return 0; fi
if [[ "$c" == *mutant* ]]; then echo "mutant"; return 0; fi
@@ -67,17 +100,19 @@ match() { # $1: the command line, NULs as spaces. Prints the rule that matched.
seen=0
busy=0
errors=0
# No directory test before the read: `[ -d ]` is false for a denied lookup
# too, so every listed pid goes through the read and the gone check.
for d in "$PROC"/[0-9]*; do
[ -d "$d" ] || continue
pid=${d##*/}
[[ "$pid" =~ ^[0-9]+$ ]] || continue
seen=$((seen + 1))
[ -z "${SKIP[$pid]:-}" ] || continue
# A process can exit between the listing and the read; that's not an error.
# A failed read while its directory is still there is: the scan can't say
# what that process runs.
if ! cmd=$(tr '\0' ' ' < "$d/cmdline" 2>/dev/null); then
[ -e "$d" ] || continue
# A failed read is, unless a fresh listing confirms the pid is gone: the
# scan can't say what that process runs.
# The braces send the shell's own message for a failed `<` to /dev/null too.
if ! cmd=$( { tr '\0' ' ' < "$d/cmdline"; } 2>/dev/null); then
gone "$PROC" "$pid" && continue
echo "slot-busy: error: cannot read $d/cmdline" >&2
errors=$((errors + 1))
continue
+147
View File
@@ -14,8 +14,12 @@ unset GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR GIT_INDEX_FILE GIT_OBJECT_DIRECTORY G
SANDBOX="$(cd "$(mktemp -d)" && pwd -P)"
STANDIN=""
KIDS="" # wrapper shells and their children that the suite starts
LOCKED="" # directories a test makes unreadable; cleanup gives modes back
cleanup() {
if [ -n "$STANDIN" ]; then kill "$STANDIN" 2>/dev/null; wait "$STANDIN" 2>/dev/null; fi
for k in $KIDS; do kill "$k" 2>/dev/null; wait "$k" 2>/dev/null; done
for l in $LOCKED; do chmod 0755 "$l" 2>/dev/null; done
rm -rf "$SANDBOX"
}
trap cleanup EXIT
@@ -208,6 +212,17 @@ out=$(bash "$INSTALL" --repo "$SANDBOX/sl" --force-install 2>&1); rc=$?
check "it refuses it with --force-install too" "$([ "$rc" = 2 ] && has "$out" "/.git/hooks is a symlink"; echo $?)"
check "nothing was written in the directory the symlink names" "$([ -z "$(ls -A "$SANDBOX/shared-hooks")" ]; echo $?)"
# The temporary copy's name isn't predictable from the installer's pid. The
# subshell plants a link at the pid-named path, then execs the installer, so
# the installer's $$ is that pid.
fresh "$SANDBOX/pt"
printf 'victim\n' > "$SANDBOX/pt-victim"
out=$(pid=$BASHPID; ln -s "$SANDBOX/pt-victim" "$SANDBOX/pt/.git/hooks/.pre-push.install-git-hooks.$pid" && exec bash "$INSTALL" --repo "$SANDBOX/pt" 2>&1); rc=$?
check "the install writes nothing through a link planted at a pid-named temporary path" \
"$([ "$rc" = 0 ] && [ "$(cat "$SANDBOX/pt-victim")" = victim ]; echo $?)"
check "the installed hook is a regular file with the repository's bytes" \
"$([ -f "$SANDBOX/pt/.git/hooks/pre-push" ] && [ ! -L "$SANDBOX/pt/.git/hooks/pre-push" ] && cmp -s "$HOOK_SRC" "$SANDBOX/pt/.git/hooks/pre-push"; echo $?)"
fresh "$SANDBOX/wd"
git -C "$SANDBOX/wd" worktree add -q -b seat/x "$SANDBOX/wd-wt" origin/main
out=$(bash "$INSTALL" --repo "$SANDBOX/wd-wt" 2>&1); rc=$?
@@ -236,10 +251,14 @@ fakeproc 4194402 /srv grep -r gate .
# Both wrappers' text would match a rule without the snapshot exclusion.
fakeproc 4194403 /srv /bin/bash -c "source /home/u/.claude/shell-snapshots/snapshot-bash-1.sh && node --test x.test.mjs"
fakeproc 4194405 /srv /usr/bin/bash -c "source /home/u/.claude/shell-snapshots/snapshot-bash-2-ab.sh 2>/dev/null || true && eval 'bash scripts/test-x.sh > x.log' && pwd -P >| /home/u/cwd"
# A wrapper that sources files that aren't gates, tests or mutants, then
# runs a gate as a child: the child is matched, the wrapper isn't.
fakeproc 4194406 /srv /bin/bash -c "source /home/u/.claude/shell-snapshots/snapshot-bash-1.sh && source .venv/bin/activate && . ./env.sh && ./gate.sh 253"
mkdir -p "$P/4194404"; : > "$P/4194404/cmdline"
out=$(busyrun); rc=$?
check "no matching process: exit 0 and CLEAR" "$([ "$rc" = 0 ] && [ "$out" = CLEAR ]; echo $?)"
check "an agent's shell-snapshot wrapper isn't a match" "$(! has "$out" "4194403" && ! has "$out" "4194405"; echo $?)"
check "a wrapper that sources other files and runs a gate as a child isn't a match" "$(! has "$out" "4194406"; echo $?)"
fakeproc 4194410 /seat/a node --test packages/x/tests/a.test.mjs
out=$(busyrun); rc=$?
@@ -268,6 +287,16 @@ rule "docker compose" /usr/bin/docker-compose up
rule "node --test" /bin/bash -c "node --test x.test.mjs"
rule "node --test" node --test /seat/tests/shell-snapshots/snapshot-guard.test.mjs
rule "gate" bash /seat/shell-snapshots/snapshot-gate.sh
# A wrapper that sources a gate, test or mutant script runs it in its own
# shell, with no child to match, so the wrapper itself is BUSY.
SNAP="source /home/u/.claude/shell-snapshots/snapshot-bash-1.sh"
rule "sourced test script" /bin/bash -c "$SNAP && source /seat/scripts/test-x.sh"
rule "sourced test script" /bin/bash -c "$SNAP && . /seat/scripts/test-x.sh"
rule "sourced test script" /bin/bash -c "$SNAP && source \"scripts/test-x.sh\" arg"
# A sourced file that isn't one comes first; the one after it still counts.
rule "sourced test script" /bin/bash -c "$SNAP && source .venv/bin/activate && . scripts/test-x.sh"
rule "sourced gate" /usr/bin/bash -c "source /home/u/.claude/shell-snapshots/snapshot-bash-2-ab.sh 2>/dev/null || true && eval '. ./gate.sh 253' && pwd -P >| /home/u/cwd"
rule "sourced mutant" /bin/bash -c "$SNAP && cd /seat && eval 'source agents/x/work/queue-1/mutants.sh M3'"
rm -rf "${P:?}/4194420"
echo "== slot-busy, read errors =="
@@ -315,6 +344,77 @@ out=$(PATH="$SANDBOX/failawk:$PATH" bash "$BUSY" --proc-root "$P" 2>&1); rc=$?
check "its own process unreadable (awk exits 1): exit 2, no CLEAR" \
"$([ "$rc" = 2 ] && has "$out" "cannot read its own process" && ! has "$out" CLEAR; echo $?)"
# Every ancestor's status read counts, not only the first. The awk below is
# the real awk for call 1 (the scan's own status) and then fails or prints
# nothing; a third one prints a pid that isn't in /proc on call 2, so the
# walk meets an ancestor that has exited.
mkdir -p "$SANDBOX/awkn"
cat > "$SANDBOX/awkn/awk" <<EOF
#!/bin/sh
n=\$(cat "$SANDBOX/awkn/n" 2>/dev/null || echo 0); n=\$((n + 1)); echo "\$n" > "$SANDBOX/awkn/n"
if [ "\$n" -ge 2 ]; then
case \$(cat "$SANDBOX/awkn/mode") in
fail) exit 1 ;;
empty) exit 0 ;;
exited) if [ "\$n" = 2 ]; then echo 4194999; exit 0; fi ;;
esac
fi
exec $(command -v awk) "\$@"
EOF
chmod +x "$SANDBOX/awkn/awk"
awkn() { # $1: fail, empty or exited. Runs the check on the idle fake /proc.
echo "$1" > "$SANDBOX/awkn/mode"; rm -f "$SANDBOX/awkn/n"
out=$(PATH="$SANDBOX/awkn:$PATH" bash "$BUSY" --proc-root "$P" 2>&1); rc=$?
calls=$(cat "$SANDBOX/awkn/n" 2>/dev/null || echo 0)
}
awkn fail
check "an ancestor's status read that fails after its own read passed: exit 2, no CLEAR" \
"$([ "$calls" -ge 2 ] && [ "$rc" = 2 ] && has "$out" "cannot read the status of ancestor" && ! has "$out" CLEAR; echo $?)"
awkn empty
check "an ancestor's status with no PPid: exit 2, no CLEAR" \
"$([ "$calls" -ge 2 ] && [ "$rc" = 2 ] && has "$out" "cannot read the status of ancestor" && ! has "$out" CLEAR; echo $?)"
awkn exited
check "an ancestor that has exited (its pid gone from /proc) ends the walk: exit 0 and CLEAR" \
"$([ "$calls" -ge 3 ] && [ "$rc" = 0 ] && [ "$out" = CLEAR ]; echo $?)"
# A lookup that is denied isn't a process that vanished. This tr makes the
# proc root unreadable once the pid directory is listed, then fails the read;
# the pid directory and its node --test cmdline stay where they are.
DP="$SANDBOX/dproc"
mkdir -p "$SANDBOX/denytr" "$DP/4194450"
printf '%s\0' node --test d.test.mjs > "$DP/4194450/cmdline"
cat > "$SANDBOX/denytr/tr" <<EOF
#!/bin/sh
f=\$(readlink "/proc/\$\$/fd/0")
case "\$f" in */4194450/cmdline) chmod 000 "$DP"; exit 1 ;; esac
exec $(command -v tr) "\$@"
EOF
chmod +x "$SANDBOX/denytr/tr"
LOCKED="$LOCKED $DP"
out=$(PATH="$SANDBOX/denytr:$PATH" bash "$BUSY" --proc-root "$DP" 2>&1); rc=$?
denied=$([ ! -r "$DP" ] && echo yes); chmod 0755 "$DP"
check "a lookup denied after the listing (proc root mode 000): exit 2, no CLEAR" \
"$([ "$denied" = yes ] && [ "$rc" = 2 ] && has "$out" "cannot read $DP/4194450/cmdline" && ! has "$out" CLEAR; echo $?)"
check "the denied pid directory and its node --test cmdline were still there" \
"$([ "$(tr '\0' ' ' < "$DP/4194450/cmdline")" = "node --test d.test.mjs " ]; echo $?)"
# The root turns unreadable after the first pid's read went through: the next
# pid, a node --test, is a doubt, not a skip.
rm -rf "${DP:?}/4194450"
mkdir -p "$SANDBOX/locktr" "$DP/4194451" "$DP/4194452"
printf '%s\0' sleep 60 > "$DP/4194451/cmdline"
printf '%s\0' node --test l.test.mjs > "$DP/4194452/cmdline"
cat > "$SANDBOX/locktr/tr" <<EOF
#!/bin/sh
f=\$(readlink "/proc/\$\$/fd/0")
case "\$f" in */4194451/cmdline) chmod 000 "$DP" ;; esac
exec $(command -v tr) "\$@"
EOF
chmod +x "$SANDBOX/locktr/tr"
out=$(PATH="$SANDBOX/locktr:$PATH" bash "$BUSY" --proc-root "$DP" 2>&1); rc=$?
denied=$([ ! -r "$DP" ] && echo yes); chmod 0755 "$DP"
check "a proc root that turns unreadable after one read hides no later process: exit 2, no CLEAR" \
"$([ "$denied" = yes ] && [ "$rc" = 2 ] && has "$out" "cannot read $DP/4194452/cmdline" && ! has "$out" CLEAR; echo $?)"
out=$(bash "$BUSY" --proc-root "$SANDBOX/no-such-proc" 2>&1); rc=$?
check "a missing proc root: exit 2" "$([ "$rc" = 2 ]; echo $?)"
mkdir -p "$SANDBOX/empty-proc"
@@ -350,6 +450,53 @@ out=$(bash "$BUSY" 2>&1); rc=$?
check "after the stand-in exits, it has no BUSY line" "$(! printf '%s\n' "$out" | grep -q "^BUSY $gone "; echo $?)"
check "the real-/proc run exits 0 or 1, never 2" "$([ "$rc" = 0 ] || [ "$rc" = 1 ]; echo $?)"
echo "== slot-busy, real snapshot wrappers =="
# Live wrappers, each shown to the check through a proc root that links only
# its own real /proc directories, so other jobs on the host don't count.
mkdir -p "$SANDBOX/shell-snapshots" "$SANDBOX/scripts" "$SANDBOX/wrap"
: > "$SANDBOX/shell-snapshots/snapshot-x.sh"
mkfifo "$SANDBOX/wrap/fifo"
# Sourced: it says it's ready, then blocks in the read builtin, no child.
cat > "$SANDBOX/scripts/test-x.sh" <<EOF
exec 3<>"$SANDBOX/wrap/fifo"
: > "$SANDBOX/wrap/ready"
read -t 60 -u 3 _
EOF
(cd "$SANDBOX/wrap" && exec bash -c "source $SANDBOX/shell-snapshots/snapshot-x.sh && source $SANDBOX/scripts/test-x.sh") &
WRAP=$!; KIDS="$KIDS $WRAP"
for _ in $(seq 50); do [ -e "$SANDBOX/wrap/ready" ] && break; sleep 0.1; done
kids=$(cat "/proc/$WRAP/task/$WRAP/children") && kids="none$kids"
LP="$SANDBOX/lproc1"; mkdir -p "$LP"; ln -s "/proc/$WRAP" "$LP/$WRAP"
out=$(bash "$BUSY" --proc-root "$LP" 2>&1); rc=$?
check "a live wrapper that sourced a test script, blocked in a builtin with no child: exit 1" \
"$([ -e "$SANDBOX/wrap/ready" ] && [ "$kids" = none ] && [ "$rc" = 1 ] && ! has "$out" CLEAR; echo $?)"
check "its BUSY line names the wrapper's pid, the sourced rule and its cwd" \
"$(printf '%s\n' "$out" | grep -q "^BUSY $WRAP sourced test script cwd=$SANDBOX/wrap "; echo $?)"
out=$(bash "$BUSY" 2>&1); rc=$?
check "the real /proc has a BUSY line for that wrapper too" \
"$([ "$rc" != 0 ] && printf '%s\n' "$out" | grep -q "^BUSY $WRAP sourced test script "; echo $?)"
kill "$WRAP" 2>/dev/null; wait "$WRAP" 2>/dev/null; KIDS=""
# External: the wrapper runs node --test as a child (`; :` keeps bash from
# exec'ing it). The wrapper is skipped and its child is BUSY.
(cd "$SANDBOX/wrap" && exec bash -c "source $SANDBOX/shell-snapshots/snapshot-x.sh && $SANDBOX/bin/node --test active.test.mjs; :") &
WRAP=$!; KIDS="$KIDS $WRAP"; kid=""
for _ in $(seq 50); do
kid=$(cat "/proc/$WRAP/task/$WRAP/children" 2>/dev/null); kid=${kid%% *}
[ -n "$kid" ] && has "$(tr '\0' ' ' < "/proc/$kid/cmdline" 2>/dev/null)" "--test active" && break
sleep 0.1
done
KIDS="$KIDS $kid"
LP="$SANDBOX/lproc2"; mkdir -p "$LP"; ln -s "/proc/$WRAP" "$LP/$WRAP"; [ -n "$kid" ] && ln -s "/proc/$kid" "$LP/$kid"
out=$(bash "$BUSY" --proc-root "$LP" 2>&1); rc=$?
check "a live wrapper that runs node --test as a child: exit 1 on the child's BUSY line" \
"$([ -n "$kid" ] && [ "$rc" = 1 ] && printf '%s\n' "$out" | grep -q "^BUSY $kid node --test "; echo $?)"
check "the wrapper itself has no BUSY line" "$(! printf '%s\n' "$out" | grep -q "^BUSY $WRAP "; echo $?)"
# The wrapper exits by itself once its child has; only then are both gone.
[ -n "$kid" ] && kill "$kid" 2>/dev/null
for _ in $(seq 50); do kill -0 "$WRAP" 2>/dev/null || break; sleep 0.1; done
if ! kill -0 "$WRAP" 2>/dev/null; then wait "$WRAP" 2>/dev/null; KIDS=""; fi
echo "---"
echo "git-guard selftests: $PASS passed, $FAIL failed"
[ "$FAIL" -eq 0 ]