Make the step-order gate report an unreadable jobs: block (#678) (#694)

`check-roborazzi-step-order.sh` exited 1 with ZERO bytes of output on a
workflow with no `jobs:` key, and on one whose `jobs:` is flow-style
(`jobs: {}`) so no job key matches the flat scan. Measured on both.

`grep` exits 1 when it matches nothing. Under `set -euo pipefail` that
status propagates out of the command substitution and kills the script --
after the value has been captured and before anything is printed. Lines
39-40 had that shape; the gate's own `index_of` already carries a `|| true`
and a comment about the same trap.

shellcheck cannot see this at any severity, including the
`check-extra-masked-returns,check-set-e-suppressed` pair the repo enables
and `-o all`: an assignment from a command substitution is the one place
the status is NOT masked, which is exactly the problem. The self-test is
the only mechanism that can hold it.

Behaviour chosen: FAIL with a named finding, not a silent pass and not a
diagnose-and-skip. These are the inputs the check exists to notice -- a
workflow that stopped having exactly one job, or stopped having a `jobs:`
key where this scan expects one -- and the script's `job_name_count -ne 1`
branch already intended to fail on them; it just died first. Passing
vacuously over zero steps would drop the ordering protection with nothing
said, which is #579's failure mode. The two shapes now get distinct
messages: "has no top-level 'jobs:' key" and "has a 'jobs:' key but no job
this scan can see".

`job_count` is dropped rather than fixed. It was assigned, `:`-ed to keep
SC2034 quiet, and never compared against anything; the real assertion is
`job_name_count` below it. Deleting it removes the defect site instead of
papering over it.

The self-test grows the two measured inputs, and `expect` grows an
assertion that a red case printed something at all. That is the half that
catches this: the status was ALREADY 1 while the bug was live, so a case
checking only the exit code passes against the broken script. Verified red
first -- both new cases failed on "exited 1 with ZERO bytes of output"
while passing the status check -- then green.

Gate suite: 82 -> 84 `ok:` lines. `check-shell-lint-test.sh` is red before
and after, identically, because actionlint is not installed locally.

Closes #678

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
monkopedia-coder 2026-09-03 00:03:17 -04:00 • committed by GitHub
parent 7b6fe5e0b5
commit ac1b028432
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 15 deletions

View file

@ -34,10 +34,32 @@ fi
# `android-ci.yml` has a single job, so every top-level step in the file belongs
# to it and document order is execution order. Asserted rather than assumed: if
# a second job is ever added, the flat scan below stops being meaningful and
# this must be revisited instead of quietly comparing steps across jobs.
job_count=$(grep -cE '^ [A-Za-z0-9_-]+:$' "$workflow" | tr -d ' ')
jobs_line=$(grep -n '^jobs:' "$workflow" | cut -d: -f1)
# a second job is ever added, or the jobs: block is reshaped so this scan can no
# longer read it, the flat scan below stops being meaningful and that must be
# revisited instead of quietly comparing steps across jobs -- or, worse, passing
# vacuously over no steps at all.
#
# The `|| true` is load-bearing rather than a route to green, for the same
# reason it is on `index_of` below: `grep` exits 1 when it matches nothing, and
# under `set -euo pipefail` that status propagates out of the assignment and
# kills the script. A workflow with no `jobs:` line is exactly the shape this
# block exists to report, so without the swallow the check died on its own
# subject matter -- exit 1 with NO output, which reads as a crash rather than as
# a finding (#678, the #628 shape). The captured value, not the status, is the
# answer; every way it can come back unusable takes a FAIL branch below.
jobs_line=$(grep -n '^jobs:' "$workflow" | cut -d: -f1 || true)
if [[ -z "$jobs_line" ]]; then
echo "FAIL: $workflow has no top-level 'jobs:' key"
echo " This check reads steps flat from the jobs: line down. With no such"
echo " line there is nothing to read, and a workflow that has stopped"
echo " having one is a shape change to look at, not a silent pass."
exit 1
fi
if [[ ! "$jobs_line" =~ ^[0-9]+$ ]]; then
echo "FAIL: could not locate a single 'jobs:' line in $workflow"
echo " (matched: ${jobs_line//$'\n'/ })"
exit 1
fi
job_names=$(awk -v start="$jobs_line" 'NR > start && /^ [A-Za-z0-9_-]+:$/ { gsub(/[ :]/, ""); print }' "$workflow")
# Hoisted out of the `[[ ]]`, and the `|| true` on it is load-bearing rather
# than a route to green: `grep -c` PRINTS 0 and EXITS 1 when it counts nothing,
@ -45,21 +67,24 @@ job_names=$(awk -v start="$jobs_line" 'NR > start && /^ [A-Za-z0-9_-]+:$/ { gsu
# swallow the assignment would kill the script here, on the exact input the
# check is for -- exit 1 with no output, the #628 shape. The printed count, not
# the status, is the answer; a value that is not 1, including one that is not a
# number, takes the FAIL branch.
#
# (The two `grep`s two lines up still have that shape and are deliberately left
# alone: that is #678's, measured and filed separately, and fixing it here would
# bury it.)
# number, takes a FAIL branch.
job_name_count=$(printf '%s\n' "$job_names" | grep -c . || true)
if [[ "$job_name_count" -ne 1 ]]; then
# Joined with bash parameter expansion rather than `| tr`: a failure message
# should not depend on a subprocess whose own status is then discarded, and
# there is no status left to discard once the subprocess is gone.
echo "FAIL: $workflow no longer has exactly one job (found: ${job_names//$'\n'/ })"
echo " This check scans steps flat, which is only valid for a single job."
if [[ "$job_name_count" -eq 0 ]]; then
echo "FAIL: $workflow has a 'jobs:' key but no job this scan can see"
echo " Job keys are matched as ' <name>:' alone on a line; a flow-style"
echo " 'jobs: {}', a reindented block or a reusable-workflow shape is"
echo " invisible to it. The scan is not valid for a shape it cannot"
echo " read, so this is red rather than a vacuous pass over zero steps."
else
# Joined with bash parameter expansion rather than `| tr`: a failure message
# should not depend on a subprocess whose own status is then discarded, and
# there is no status left to discard once the subprocess is gone.
echo "FAIL: $workflow no longer has exactly one job (found: ${job_names//$'\n'/ })"
echo " This check scans steps flat, which is only valid for a single job."
fi
exit 1
fi
: "$job_count"
# Ordered list of step names, one per line, in document = execution order.
# Steps declared as a bare `- uses:` carry no name and are absent from this

View file

@ -65,6 +65,18 @@ expect() {
failures=$((failures + 1))
return
fi
# The bug's signature is the ABSENCE of output, not the status: a gate that
# goes red with zero bytes reads as a crash rather than as the finding it is
# (#628, and #678 for this script's own instance of it). Asserted separately
# from the substring so that even a case written without a `must_say` cannot
# pass against that shape. (`$(...)` strips trailing newlines, so an empty
# `$out` means the gate printed nothing but newlines.)
if [ "$want" -ne 0 ] && [ -z "$out" ]; then
echo "FAIL: $name -- exited $status with ZERO bytes of output"
echo " A gate that fails silently reads as a crash, not as a finding."
failures=$((failures + 1))
return
fi
if [ -n "$must_say" ] && ! printf '%s\n' "$out" | grep -qF "$must_say"; then
echo "FAIL: $name -- exited $status but never said '$must_say'"
printf '%s\n' "$out" | sed 's/^/ /'
@ -164,6 +176,29 @@ write_workflow "$sandbox/wf.yml" "${good_order[@]}"
} >> "$sandbox/wf.yml"
expect 1 "a second job invalidates the flat scan" "no longer has exactly one job"
# The same precondition seen from the other end, and the two inputs #678
# measured. `grep` exits 1 when it matches nothing, so under `set -euo pipefail`
# the two scans that locate the jobs: key used to kill the script here -- exit 1
# with NO output, on precisely the inputs this check exists to notice. Both
# cases assert the MESSAGE, not just the status: the status was already 1 while
# the bug was live, and `expect` above independently rejects a silent red.
{
echo "name: Android CI"
echo "on: [push]"
} > "$sandbox/wf.yml"
expect 1 "a workflow with no jobs: key at all" "has no top-level 'jobs:' key"
# Flow-style `jobs: {}`: the jobs: key is there, but no job key is on its own
# line for the flat scan to see. Vacuously passing over zero steps would be the
# worst outcome -- the ordering would stop being protected with nothing said.
{
echo "name: Android CI"
echo "on: [push]"
echo "jobs: {}"
} > "$sandbox/wf.yml"
expect 1 "jobs: written flow-style, invisible to the flat scan" \
"no job this scan can see"
# A workflow that is not there at all.
rm -f "$sandbox/wf.yml"
expect 1 "the workflow file is missing" "no workflow at"