carlos / platform Public

test-gate: refuse bad shard counts, stop jobs on cancel (Astra review)

P1: TEST_SPLIT=./internal/console=0 took console out of the ordinary run
and launched none of its shards, so the gate went green without its
tests. Every count must now be a whole number of at least 1, and the
names launched across a package's shards must equal go test -list's.

P2: an interrupt or TERM removed the scratch directory and left go test
and its test binaries running. The gate now walks each job's process
tree (pgrep -P) and stops it first; set -m was tried and dash turns job
control off without a terminal. A job killed before writing its status
counts as failed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev b3ede368c8bfa93064fc67d708d389011a81f045 parent 43dd2e9
1 file changed, +57 −4
  • .amadan/test-gate.sh +57 −4

CI passed — run details

diff --git a/.amadan/test-gate.sh b/.amadan/test-gate.sh
index d6720758d..35fa13ee8 100755
--- a/.amadan/test-gate.sh
+++ b/.amadan/test-gate.sh
@@ -36,8 +36,53 @@ set -eu
: "${TEST_SPLIT=./internal/console=8 .=3}"
weights=$(pwd)/.amadan/test-weights.tsv
+# A shard count that is not a whole number of at least 1 would take a
+# package out of the ordinary run and then launch none of its shards: a
+# green gate that ran none of that package's tests. Refuse it up front.
+for spec in $TEST_SPLIT; do
+ count=${spec#*=}
+ case $spec:$count in
+ *=*:[0-9]*) ;;
+ *)
+ echo "TEST_SPLIT entry '$spec' is not dir=count" >&2
+ exit 2
+ ;;
+ esac
+ case $count in
+ *[!0-9]*)
+ echo "TEST_SPLIT entry '$spec' needs a whole number of shards" >&2
+ exit 2
+ ;;
+ esac
+ if [ "$count" -lt 1 ]; then
+ echo "TEST_SPLIT entry '$spec' needs at least one shard" >&2
+ exit 2
+ fi
+done
+
+# Stopping the gate stops its jobs: the go test processes and the test
+# binaries under them, not just the shells that started them. That covers
+# an interrupt, a TERM from the runner, and an early exit while jobs are
+# already running. Process groups (set -m) would be simpler, but dash
+# turns job control off without a terminal, which is how CI runs it, so
+# each job's tree is walked with pgrep -P instead.
out=$(mktemp -d "${TMPDIR:-/tmp}/carlos-gate-XXXXXX")
-trap 'rm -rf "$out"' EXIT INT TERM
+pids=""
+kill_tree() {
+ for child in $(pgrep -P "$1" 2>/dev/null); do
+ kill_tree "$child"
+ done
+ kill -TERM "$1" 2>/dev/null || true
+}
+stop_jobs() {
+ for pid in $pids; do
+ kill_tree "$pid"
+ done
+ wait 2>/dev/null || true
+ rm -rf "$out"
+}
+trap stop_jobs EXIT
+trap 'trap - EXIT; stop_jobs; exit 130' INT TERM
names=""
# A failure found before any test runs (a package that will not list, a
@@ -60,6 +105,7 @@ start() {
echo $? >"$out/$start_name.rc"
echo $(($(date +%s) - t0)) >"$out/$start_name.secs"
) &
+ pids="$pids $!"
}
start launchctx sh -c 'cd launchctx && go vet ./... && go test ./... -count=1'
@@ -124,22 +170,29 @@ for spec in $TEST_SPLIT; do
fi
i=0
+ launched=0
while [ "$i" -lt "$shards" ]; do
pattern=$(awk -v i="$i" -F '\t' '$1 == i { printf "%s%s", sep, $2; sep = "|" }' "$out/deal-$tag")
if [ -n "$pattern" ]; then
start "$tag@$i" go test -count=1 -timeout 30m -run "^($pattern)\$" "$dir"
+ launched=$((launched + $(awk -v i="$i" -F '\t' '$1 == i' "$out/deal-$tag" | wc -l)))
fi
i=$((i + 1))
done
+ if [ "$launched" -ne "$listed" ]; then
+ fail_job "$tag-deal" "launched $launched of $listed tests in $pkg"
+ fi
done
wait
+pids=""
failed=""
for name in $names; do
- rc=$(cat "$out/$name.rc")
- echo "=== $name ($(cat "$out/$name.secs") s)"
- cat "$out/$name.log"
+ # A job with no status file was killed before it could write one.
+ rc=$(cat "$out/$name.rc" 2>/dev/null || echo 1)
+ echo "=== $name ($(cat "$out/$name.secs" 2>/dev/null || echo '?') s)"
+ cat "$out/$name.log" 2>/dev/null || true
case $name in
*@*)
if grep -q 'no tests to run' "$out/$name.log"; then