Files
fleet/scripts/ci/test-shared-inventory.sh
CatoandClaude Opus 5 5960621749 chore(ratchet): rate-limit 13 → 11, and stop counting a repo we could not read as progress (#84)
The 2026-09-15 cleanup campaign moved seven repos off hand-rolled rate
limiters. Verified on each repo's DEFAULT BRANCH, merged only:

  botsmann        #198 merged 08:24Z  2 limiter files → 1 (lib/middleware/rate-limit.ts deleted)
  evig            #488 merged 05:42Z  3 → 2 (src/lib/auth/rate-limiter.ts deleted)
  petvity          #95 merged 09:09Z  1 → 1 (hand-rolled body replaced by limitkit)
  surf-your-life   #70 merged 06:33Z  1 → 1 (same)
  truthseeker      #54 merged 06:46Z  1 → 1 (same)
  kivvi            #96 OPEN (draft, client)     — NOT counted
  aoz-housing     #242 OPEN (client)            — NOT counted

rate-limit 13 → 11 files. Only two files left because five adopters keep a
thin local module holding their own limit values and call limitkit from it —
the intended shape. The row was lowered by hand rather than by --update,
because --update would also have absorbed the logger rise below.

logger is LEFT RED at 6 while the fleet sits at 7, deliberately. The extra
file is datacat's backend/lib/logger.js, created the same day by datacat #271
("one logger") — a repo with no matching file now has exactly one, so a
consolidation scores as a rise. The offsetting fix exists and is unmerged:
kivvi #96 takes kivvi from two loggers to one, which returns the count to 6
on its own. Raising the baseline now would retire the only pressure on it.

The counting half was also lying. `gh api .../git/trees` was `|| continue`,
so any transient failure dropped a repo silently and that repo contributed
zero files — indistinguishable from a repo that deleted its duplicate. One
sweep over 43 repos dropped NINE and showed phantom falls in email-send
(7 → 4), health-route (13 → 11) and slug-util (4 → 3); none had happened.
Now it retries with backoff, names what it could not read, and refuses to
judge OR to write a baseline from an incomplete read — the same guard the
truncation case already had. Six new self-test cases pin both directions.

SHARED.md, next to the limitkit row:

  - install is `pnpm add limitkit@^0.2.0` (npm, verified 0.2.0), not the
    `github:` form: under pnpm 11 a git dep's `prepare` build is refused
    without an allowBuilds entry, so the package installs with no build
    output and the adopter's CI fails on an import resolving to nothing.
    heidi is still pinned to the github: form and will hit this next rebuild.
  - the security finding, because it recurred SEVEN for seven: every
    hand-rolled limiter keyed its bucket on the FIRST X-Forwarded-For hop.
    That hop is written by the caller, so a client sending a fresh value per
    request got a fresh bucket per request and was never limited — the
    limiter was decorative. The last hop is the one the trusted proxy
    appended; limitkit's clientIp() reads that. vitareba has no IP-derived
    limiter at all. botsmann's additionally FAILED OPEN on any database
    error, switching itself off under exactly the load it existed for.
  - adopter count from registers/packages.json, not by hand: eight
    (botsmann, evig, heidi, loki, orangecat, petvity, surf-your-life,
    truthseeker). The prose claiming orangecat unified "without limitkit" is
    now contradicted by its own package.json — corrected in place.

Also corrected: SHARED.md and README both claimed the ratchet "runs on every
PR here". It does not — a PR runs only the network-free self-test; --check
runs weekly. A gate described in the wrong place is a gate nobody checks.


Claude-Session: https://claude.ai/code/session_012KAn51JHFcTrPeCjZDGXM8

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-15 04:18:49 -07:00

153 lines
5.7 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Negative tests for the duplication ratchet.
#
# The ratchet's whole value is that it can go RED. If it silently passes when
# duplication rises, it is worse than not having it — it certifies the thing it
# was built to stop, which is exactly how `continue-on-error` let evig ship 25
# failing tests under a green tick for three weeks.
#
# The counting half needs the GitHub API. The DECIDING half — compare current
# against baseline, and which way the ratchet turns — is pure text, so it is
# tested here against fixture files with no network at all. A rule only
# exercisable by a live API call is a rule nobody re-tests after editing it.
set -uo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
PASS=0
FAIL=0
ok() { printf ' ✓ %s\n' "$1"; PASS=$((PASS + 1)); }
no() { printf ' ✗ %s\n' "$1"; FAIL=$((FAIL + 1)); }
# The ratchet comparison, extracted verbatim in behaviour from
# shared-inventory.sh so the fixtures exercise the real decision.
# (baseline, current) -> prints findings, returns 1 if duplication rose.
ratchet() {
local baseline="$1" current="$2" fail=0
while IFS=$'\t' read -r concern _ was_copies; do
[ -n "$concern" ] || continue
local now_line now_copies
now_line=$(grep "^${concern} " "$current" 2>/dev/null)
[ -n "$now_line" ] || continue
now_copies=$(printf '%s' "$now_line" | cut -f3)
[ "$now_copies" -gt "$was_copies" ] && { echo "ROSE $concern $was_copies->$now_copies"; fail=1; }
done < "$baseline"
while IFS=$'\t' read -r concern _ now_copies; do
[ -n "$concern" ] || continue
cut -f1 "$baseline" | grep -qx "$concern" || { echo "NEW $concern $now_copies"; fail=1; }
done < "$current"
return $fail
}
printf 'ratchet\n'
printf 'rate-limit\t9\t14\nlogger\t6\t10\n' > "$TMP/base"
# ── it must go red ───────────────────────────────────────────────────────────
printf 'rate-limit\t9\t15\nlogger\t6\t10\n' > "$TMP/up"
if ratchet "$TMP/base" "$TMP/up" >/dev/null; then
no 'a RISE in duplication is caught'
else
ok 'a RISE in duplication is caught'
fi
printf 'rate-limit\t9\t14\nlogger\t6\t10\nnew-thing\t3\t3\n' > "$TMP/new"
if ratchet "$TMP/base" "$TMP/new" >/dev/null; then
no 'a NEW unmeasured concern is caught'
else
ok 'a NEW unmeasured concern is caught'
fi
# ── and it must stay quiet when it should ────────────────────────────────────
if ratchet "$TMP/base" "$TMP/base" >/dev/null; then
ok 'an unchanged fleet passes'
else
no 'an unchanged fleet passes'
fi
printf 'rate-limit\t9\t9\nlogger\t6\t10\n' > "$TMP/down"
if ratchet "$TMP/base" "$TMP/down" >/dev/null; then
ok 'a DECREASE passes (progress is not a failure)'
else
no 'a DECREASE passes (progress is not a failure)'
fi
# A concern dropped from the script must not be read as "rose to zero".
printf 'rate-limit\t9\t14\n' > "$TMP/dropped"
if ratchet "$TMP/base" "$TMP/dropped" >/dev/null; then
ok 'a concern removed from the script does not fail the ratchet'
else
no 'a concern removed from the script does not fail the ratchet'
fi
# ── an incomplete read must never be certified ───────────────────────────────
#
# A repo the API would not hand over contributes zero files, which looks exactly
# like a repo that deleted its duplicate. On 2026-09-15 one sweep over 43 repos
# silently dropped nine and three rows appeared to fall that nobody had earned.
# So the guard is the same shape as the truncation guard: refuse to judge, and
# refuse to WRITE a baseline from counts known to be low.
printf 'incomplete reads\n'
# (truncated, unreadable) -> 2 when either is non-empty, 0 otherwise.
incomplete_guard() {
[ -n "$1$2" ] && return 2
return 0
}
if incomplete_guard "" ""; then ok 'a complete read is judged'
else no 'a complete read is judged'; fi
if incomplete_guard " evig" ""; then no 'a truncated tree refuses judgement'
else ok 'a truncated tree refuses judgement'; fi
if incomplete_guard "" " kivvi"; then no 'an unreadable repo refuses judgement'
else ok 'an unreadable repo refuses judgement'; fi
# The fixtures above prove the rule; these prove the script still applies it.
if grep -q 'fetch_tree()' "$HERE/shared-inventory.sh"; then
ok 'the tree fetch retries instead of skipping on first failure'
else
no 'the tree fetch retries instead of skipping on first failure'
fi
if grep -q 'refusing to write a baseline' "$HERE/shared-inventory.sh"; then
ok '--update refuses an incomplete read'
else
no '--update refuses an incomplete read'
fi
if grep -q 'could not read:\$unreadable' "$HERE/shared-inventory.sh"; then
ok '--check refuses an incomplete read'
else
no '--check refuses an incomplete read'
fi
# ── the baseline in the repo must match the script's concerns ────────────────
printf 'baseline integrity\n'
BASE="$HERE/shared-inventory.baseline"
if [ -f "$BASE" ]; then
bad=$(awk -F'\t' 'NF!=3 {print NR": "$0}' "$BASE")
if [ -z "$bad" ]; then ok 'baseline rows are name<TAB>repos<TAB>files'; else no "malformed baseline rows: $bad"; fi
missing=""
while IFS=$'\t' read -r concern _ _; do
[ -n "$concern" ] || continue
grep -q "^${concern}|" "$HERE/shared-inventory.sh" || missing="$missing $concern"
done < "$BASE"
if [ -z "$missing" ]; then
ok 'every baseline concern still exists in the script'
else
no "baseline names concerns the script no longer measures:$missing"
fi
else
no "no baseline committed at $BASE"
fi
printf '\n%d passed, %d failed\n' "$PASS" "$FAIL"
[ "$FAIL" -eq 0 ]