Files
loki/scripts/test/experiment-litter-gate.sh
CatoandClaude Opus 5.5 5f1e283117 fix(scripts): grep -q is never the reader of a pipe under pipefail (#874)
`echo "$x" | grep -q PAT` under `set -o pipefail` can report "not found"
for text that IS there: grep -q exits on its first match and closes the
pipe, the writer takes SIGPIPE (141), and pipefail makes the pipeline
status 141. An `if` then takes the else branch on a successful match.

#587 found this in one file and noted it "only fires when the payload
exceeds the ~4KB pipe buffer". That is not so, and the belief is why
82 copies in 22 scripts survived. Measured today on retire-site's real
1,158-byte plan output with has()'s exact pattern, 3,000 calls each:

  'DRY RUN'        line 3 of 20    echo|grep -q   2/3000 false misses
  'apps.conf row'  line 11 of 20   echo|grep -q   2/3000 false misses
  last phrase      line 20 of 20   echo|grep -q   0/3000
  'DRY RUN'                        here-string    0/3000

Rare per call, but verify makes hundreds of these calls, and the
retire-site suite went red on those two exact phrases twice this week,
passing every rerun. It is worse in a NEGATIVE check - hasnt() read a
false miss as a PASS. And several copies are not tests: deploy-hetzner.sh
exits 1 on a false miss, and the sshd drift watch would raise a false
security alert.

All 82 rewritten to `grep -q PAT <<<"$x"` (a here-string is not a
pipeline, so there is no writer to signal) by a script that only touched
the simple quoted-variable form and reported anything else. Checked: all
22 files pass `bash -n`, every file is a 1:1 line swap (82 lines), and
the 20 line-continuation sites keep the here-string before the trailing
backslash (the first draft of the rewrite got those wrong; caught in a
dry-run diff before anything was written).

#587's comment is corrected so it stops teaching the wrong threshold.

Gate: scripts/test/no-grep-q-in-a-pipe.ts ratchets it repo-wide,
including forms the rewrite skipped (e.g. "$(cmd)" | grep -q), and
self-tests that its detector still fires. Proven by restoring the
original deploy-hetzner.sh line.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 02:57:19 -07:00

57 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# The litter gate is only worth having if it fires on the real shapes and stays
# quiet on real products. Both halves are tested, because a gate that cries wolf
# gets its pattern widened until it catches nothing.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CHECK="$HERE/../ci/check-no-experiment-litter.sh"
pass=0; fail=0
ok() { if [ "$1" = 0 ]; then pass=$((pass+1)); else fail=$((fail+1)); echo " ✗ $2"; fi }
TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT
REAL="$HERE/../hetzner/apps.conf"
BEFORE="$(md5sum "$REAL" | cut -d" " -f1)"
run() { REGISTER="$1" bash "$CHECK" 2>&1; }
header='# name|port|domains|repo_path|app_dir|db|owner|kind|status|plan|price|since'
row() { echo "$1|4099|$1.orangecat.ch|/home/ubuntu/dev/$1|.|-|bitbaum|$2|$3|-|-|2026-09-11"; }
echo "→ it fires on every shape a generated throwaway takes"
for name in dogfood-site-sep10-1201 coldstart-sep10-2339 velokiosk-sep10 \
factory-sep11-0040 probe-oct01-0930 e2e-probe-sep12 scratch-thing; do
{ echo "$header"; row "$name" client-site prospect; } > "$TMP/reg"
out=$(run "$TMP/reg" || true)
grep -q "$name" <<<"$out" && ok 0 "" || ok 1 "expected $name to be flagged"
done
echo "→ it stays quiet on the register we actually keep"
out=$(run "$REAL" || true)
grep -q "^✓" <<<"$out" && ok 0 "" || ok 1 "the real register must pass: $out"
echo "→ a real product is never flagged, whatever its kind or status"
# camille-boulangerie and sbb-fundbuero are demos that are real work. An earlier
# version of this gate judged by kind=demo and flagged both, which is exactly
# the false positive that gets a gate disabled.
{
echo "$header"
row camille-boulangerie demo demo
row sbb-fundbuero demo demo
row orangecat product live
row hirnli client-app live
} > "$TMP/reg"
out=$(run "$TMP/reg" || true)
grep -q "^✓" <<<"$out" && ok 0 "" || ok 1 "real products must not be flagged: $out"
echo "→ a missing register is a failure, not a pass"
out=$(REGISTER="$TMP/nope.conf" bash "$CHECK" 2>&1 || true)
grep -qi "missing" <<<"$out" && ok 0 "" || ok 1 "a missing register should fail loudly"
echo "→ it never writes to the register it reads"
AFTER="$(md5sum "$REAL" | cut -d" " -f1)"
[ "$BEFORE" = "$AFTER" ] && ok 0 "" || ok 1 "the real register was modified by its own gate"
echo
echo "experiment-litter: $pass passed, $fail failed"
[ "$fail" = 0 ]