mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-27 22:15:33 +00:00
6ae94301a6
* v0.41.26.1 fix: lock-renewal cathedral — closes ~39 worker crashes/day (supersedes #1567)
Production worker daemons against Supabase / PgBouncer were crashing
~39 times/day with `unhandledRejection at renewLock`. PR #1567
proposed the right try/catch shape; this wave incorporates it and
closes the entire bug class (4 inside-review + 8 outside-voice
findings absorbed via 9 locked design decisions).
What's fixed:
- `setInterval(async () => await renewLock(...))` replaced with a
sync wrapper around the new pure `runLockRenewalTick` function.
No more unhandled rejections escaping the timer callback.
- Second crash vector closed: `.catch()` on the stored
`executeJob(...).finally(...)` promise so failJob/completeJob
throws during the same outage can't propagate to
`process.on('unhandledRejection')`.
- Per-call `Promise.race` timeout (default `lockDuration/3`) bounds
hung renewLock calls so the re-entrancy guard can't wedge
indefinitely.
- Time-based abort (NOT count-based) so the worker releases its
lock BEFORE another worker can reclaim. With the prior 3-strike
count + 30s lockDuration, a 15s window let other workers race.
- Infrastructure aborts (`lock-renewal-failed`, `lock-lost`) don't
burn job attempts — `executeJob`'s catch consults the exported
`INFRASTRUCTURE_ABORT_REASONS` set and skips `failJob` so the
stall detector reclaims cleanly.
- Universal grace-eviction: 30s force-evict safety net now fires
for ANY abort reason, not just `job.timeout_ms`.
What's added:
- `src/core/minions/lock-renewal-tick.ts` (NEW): pure extracted
state-machine function + env-knob resolver. Three operator-tunable
knobs via env (max-failures-for-audit, call-timeout-ms,
safety-margin-ms) with stderr-warn-once on bad input + default
fallback.
- `src/core/audit/lock-renewal-audit.ts` (NEW): sibling of
`batch-retry-audit.ts`. Four outcomes: failure /
success_after_failure / gave_up / executeJob_rejected. JSONL at
`~/.gbrain/audit/lock-renewal-YYYY-Www.jsonl`.
- `src/core/audit/redact-connection-info.ts` (NEW): shared privacy
helper. Strips Postgres URLs, host=, user=, password=, IPv4 from
error messages before they hit audit JSONL. Wired into BOTH the
new lock-renewal audit AND the existing batch-retry audit
(privacy backfill — same risk class).
- `scripts/check-worker-lock-renewal-shape.sh` (NEW): CI guard
wired into `bun run verify`. Asserts the v0.41.22.1 bug pattern
(`lockTimer = setInterval(async ...)`) stays absent AND the pure
function call site survives refactors. Bug-pattern-specific so it
doesn't fight legitimate refactors (codex C12).
Tests: 64 new cases across 5 new test files. 182 existing minion +
worker tests still pass. All hermetic — no PGLite, no real network,
no `mock.module`.
Plan + 9 decisions + codex outside-voice review at
~/.claude/plans/system-instruction-you-are-working-humming-nygaard.md
Closes #1567 (incorporates the contributor's try/catch shape; closes
the bug class structurally).
Co-Authored-By: @garrytan-agents <noreply@github.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* test: fill A-H gaps for v0.41.26.1 lock-renewal cathedral
The original v0.41.26.1 wave shipped 64 hermetic unit tests on the
pure tick function, audit primitives, and redactor. Post-ship audit
flagged 8 wiring gaps the pure tests can't see — A (launchJob
wiring), B (executeJob skip-failJob), C (.catch on stored promise),
D (INFRASTRUCTURE_ABORT_REASONS export), E (universal grace-evict),
F (executeJob_rejected end-to-end), G (re-entrancy guard at worker
layer), H (gold-standard E2E regression).
Now closed:
- **test/worker-lock-renewal-e2e.serial.test.ts** (1 test, gap H):
the headline gold-standard regression. Real PGLite + real
MinionWorker + executeRaw wrap that injects renewLock failures on
demand. Pins that the worker process DOES NOT crash via
unhandledRejection under sustained renewLock throws, the handler
observes abort.signal.aborted = true with reason
'lock-renewal-failed', and the audit JSONL contains both `failure`
and `gave_up` events. The exact v0.41.22.1 production bug class.
Quarantined to its own file because bun:test serial + PGLite has an
unresolved interaction with multiple MinionWorker-driven tests in
the same file (second test's queue.add hangs indefinitely).
- **test/worker-lock-renewal-shape.test.ts** (18 tests, gaps A-G):
source-shape behavioral pins. Greps worker.ts function bodies for
the patterns the locked decisions promised: launchJob calls
runLockRenewalTick + resolveLockRenewalKnobs + uses
lockRenewalAudit; tickInFlight declared and gated correctly; stored
executeJob promise has .catch with logExecuteJobRejected + console
stderr; abort.signal.addEventListener fires for any abort (not just
timeout_ms); INFRASTRUCTURE_ABORT_REASONS used inside executeJob's
catch with return-early shape. Bug-pattern-specific so a refactor
that genuinely improves the shape passes; a refactor that
accidentally strips a guarantee fails loud.
All 83 lock-renewal wave tests pass in 5.2s. 205 existing minion +
worker tests still green. No production code changes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix: typecheck errors in worker-lock-renewal-e2e.serial.test.ts
CI verify failed on the new E2E gap-fill test (commit a8b282d4) due
to two TS errors that bun's runtime accepts but tsc rejects:
1. line 92: `originalExecuteRaw(sql, ...args)` with `args: unknown[]`
— TS can't prove args has <=2 elements, so the call site looks
like 1+1+N args against a function that accepts 1-3. Fixed by
destructuring the wrap params explicitly: `(sql, params?, opts?)`
matching the executeRaw signature, then calling
`originalExecuteRaw(sql, params, opts)` with named args.
2. line 145: `expect(abortReason).toBe('lock-renewal-failed')` where
`abortReason: string | null = null`. TS narrows the variable to
`null` because the closure assignment in worker.register isn't
observable to the inferrer. bun:test's `.toBe` overload then
picks the null variant and rejects the string literal. Fixed by
`as unknown as string` cast — the preceding `handlerAbortObserved`
assertion guarantees we entered the branch where abortReason was
assigned. Documented inline.
Local verify (29 checks) now green; E2E test still passes in 5.0s.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: @garrytan-agents <noreply@github.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
202 lines
7.5 KiB
Bash
Executable File
202 lines
7.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# scripts/run-verify-parallel.sh — parallel verify dispatcher.
|
|
#
|
|
# Runs the 19+ verify checks (privacy, jsonb, source-id, … + typecheck +
|
|
# admin-build) as background jobs, waits for all, aggregates exit codes,
|
|
# surfaces failed-check name + tail of its log to stderr.
|
|
#
|
|
# Replaces the sequential `&&`-chain in package.json's `verify` script.
|
|
# Wallclock: 19 sequential checks (~15-25s on CI) → parallel (~3-5s).
|
|
#
|
|
# Usage:
|
|
# bash scripts/run-verify-parallel.sh # run every CHECK below
|
|
# bash scripts/run-verify-parallel.sh --dry-list # print check list, exit
|
|
#
|
|
# Env overrides:
|
|
# GBRAIN_VERIFY_TIMEOUT per-check wallclock cap, seconds (default 120)
|
|
# GBRAIN_VERIFY_LOG_DIR where to write per-check logs (default tempdir)
|
|
#
|
|
# Exit codes:
|
|
# 0 all checks passed
|
|
# 1 one or more checks failed (full details in stderr)
|
|
# 2 usage error / no checks defined
|
|
|
|
set -uo pipefail
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
# ──────────────────────────────────────────────────────────────────────────
|
|
# Checks to run. Order is irrelevant (parallel), but keep stable for log
|
|
# determinism + grep-ability. Each entry is a bun-script name (the
|
|
# `package.json` "scripts" key), invoked as `bun run <name>`.
|
|
#
|
|
# To add a check: append to this array. To skip in CI temporarily, comment
|
|
# the line — the parallel runner doesn't care about count.
|
|
# ──────────────────────────────────────────────────────────────────────────
|
|
CHECKS=(
|
|
"check:privacy"
|
|
"check:proposal-pii"
|
|
"check:test-names"
|
|
"check:jsonb"
|
|
"check:source-id-projection"
|
|
"check:source-config-leak"
|
|
"check:progress"
|
|
"check:test-isolation"
|
|
"check:wasm"
|
|
"check:admin-build"
|
|
"check:admin-scope-drift"
|
|
"check:cli-exec"
|
|
"check:system-of-record"
|
|
"check:eval-glossary"
|
|
"check:no-pii-agent-voice"
|
|
"check:synthetic-corpus-privacy"
|
|
"check:skill-brain-first"
|
|
"check:fuzz-purity"
|
|
"check:operations-filter-bypass"
|
|
"check:gateway-routed"
|
|
"check:worker-pool-atomicity"
|
|
"check:fixture-privacy"
|
|
"check:conversation-parser"
|
|
"check:resolver"
|
|
"check:source-scope-onboard"
|
|
"check:no-double-retry"
|
|
"check:batch-audit-site"
|
|
"check:worker-lock-renewal-shape"
|
|
"typecheck"
|
|
)
|
|
|
|
if [ "${#CHECKS[@]}" -eq 0 ]; then
|
|
echo "ERROR: no checks defined in run-verify-parallel.sh" >&2
|
|
exit 2
|
|
fi
|
|
|
|
# Dry-run path: list checks, exit. Used by tests + ops debugging.
|
|
if [ "${1:-}" = "--dry-list" ]; then
|
|
printf '%s\n' "${CHECKS[@]}"
|
|
exit 0
|
|
fi
|
|
|
|
if [ "$#" -gt 0 ] && [ "${1:-}" != "" ]; then
|
|
echo "ERROR: unknown arg: $1" >&2
|
|
echo "usage: bash scripts/run-verify-parallel.sh [--dry-list]" >&2
|
|
exit 2
|
|
fi
|
|
|
|
TIMEOUT="${GBRAIN_VERIFY_TIMEOUT:-120}"
|
|
|
|
# Per-check temp dir. Each check gets its own subdir so writes can't race
|
|
# on shared scratch state (the checks themselves are read-only — they grep
|
|
# the working tree — but defense-in-depth.)
|
|
if [ -n "${GBRAIN_VERIFY_LOG_DIR:-}" ]; then
|
|
LOG_DIR="$GBRAIN_VERIFY_LOG_DIR"
|
|
mkdir -p "$LOG_DIR" || { echo "ERROR: cannot create $LOG_DIR" >&2; exit 2; }
|
|
else
|
|
LOG_DIR="$(mktemp -d /tmp/gbrain-verify-XXXXXX)"
|
|
trap 'rm -rf "$LOG_DIR"' EXIT
|
|
fi
|
|
|
|
# Resolve `timeout` for per-check wallclock cap. macOS doesn't ship one;
|
|
# brew coreutils provides `gtimeout`. If neither is available, fall back to
|
|
# bg-pid + sleep-cap (slightly less reliable but still bounded).
|
|
TIMEOUT_BIN=""
|
|
if command -v gtimeout >/dev/null 2>&1; then TIMEOUT_BIN="gtimeout"
|
|
elif command -v timeout >/dev/null 2>&1; then TIMEOUT_BIN="timeout"
|
|
fi
|
|
|
|
START_TS=$(date +%s)
|
|
echo "[verify-parallel] running ${#CHECKS[@]} checks in parallel (timeout=${TIMEOUT}s, logs=$LOG_DIR)" >&2
|
|
|
|
# ──────────────────────────────────────────────────────────────────────────
|
|
# Spawn one background process per check. Each child captures its own exit
|
|
# code into a sentinel file under $LOG_DIR/<safe-name>.exit; the parent
|
|
# never trusts `wait`'s aggregate value because that maps to last-spawned.
|
|
#
|
|
# safe_name: turn `check:privacy` into `check_privacy` so it fits a filename
|
|
# without escaping.
|
|
# ──────────────────────────────────────────────────────────────────────────
|
|
PIDS=()
|
|
SAFE_NAMES=()
|
|
for c in "${CHECKS[@]}"; do
|
|
safe="${c//:/_}"
|
|
SAFE_NAMES+=("$safe")
|
|
LOG_FILE="$LOG_DIR/$safe.log"
|
|
EXIT_FILE="$LOG_DIR/$safe.exit"
|
|
(
|
|
if [ -n "$TIMEOUT_BIN" ]; then
|
|
"$TIMEOUT_BIN" "${TIMEOUT}s" bun run "$c" > "$LOG_FILE" 2>&1
|
|
else
|
|
bun run "$c" > "$LOG_FILE" 2>&1 &
|
|
pid=$!
|
|
( sleep "$TIMEOUT" && kill -TERM "$pid" 2>/dev/null && \
|
|
sleep 5 && kill -KILL "$pid" 2>/dev/null ) &
|
|
cap_pid=$!
|
|
wait "$pid" 2>/dev/null
|
|
kill "$cap_pid" 2>/dev/null
|
|
wait "$cap_pid" 2>/dev/null
|
|
fi
|
|
rc=$?
|
|
echo "$rc" > "$EXIT_FILE"
|
|
) &
|
|
PIDS+=($!)
|
|
done
|
|
|
|
# Wait for every background job. Ignore wait's aggregate exit — exit codes
|
|
# live in the sentinel files.
|
|
for pid in "${PIDS[@]}"; do wait "$pid" 2>/dev/null || true; done
|
|
|
|
END_TS=$(date +%s)
|
|
ELAPSED=$((END_TS - START_TS))
|
|
|
|
# ──────────────────────────────────────────────────────────────────────────
|
|
# Aggregate. For each check, read its exit file; on failure, append a
|
|
# labeled block (check name + tail of log) to the failure report. Surface
|
|
# one final summary line and the report to stderr if anything failed.
|
|
# ──────────────────────────────────────────────────────────────────────────
|
|
PASS=0
|
|
FAIL=0
|
|
FAIL_NAMES=()
|
|
FAIL_REPORT=""
|
|
|
|
for i in "${!CHECKS[@]}"; do
|
|
c="${CHECKS[$i]}"
|
|
safe="${SAFE_NAMES[$i]}"
|
|
EXIT_FILE="$LOG_DIR/$safe.exit"
|
|
LOG_FILE="$LOG_DIR/$safe.log"
|
|
|
|
rc=1
|
|
[ -f "$EXIT_FILE" ] && rc=$(cat "$EXIT_FILE" 2>/dev/null || echo 1)
|
|
|
|
if [ "$rc" = "0" ]; then
|
|
PASS=$((PASS + 1))
|
|
else
|
|
FAIL=$((FAIL + 1))
|
|
FAIL_NAMES+=("$c")
|
|
if [ "$rc" = "124" ]; then
|
|
FAIL_REPORT+=$'\n--- '"$c"' (TIMED OUT after '"${TIMEOUT}"'s) ---\n'
|
|
else
|
|
FAIL_REPORT+=$'\n--- '"$c"' (rc='"$rc"') ---\n'
|
|
fi
|
|
if [ -f "$LOG_FILE" ]; then
|
|
FAIL_REPORT+="$(tail -30 "$LOG_FILE")"
|
|
FAIL_REPORT+=$'\n'
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [ "$FAIL" -gt 0 ]; then
|
|
{
|
|
echo ""
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
echo "❌ verify failed: $FAIL/${#CHECKS[@]} checks did not pass"
|
|
echo "Failed: ${FAIL_NAMES[*]}"
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
printf '%s' "$FAIL_REPORT"
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
echo "[verify-parallel] elapsed=${ELAPSED}s | pass=$PASS fail=$FAIL"
|
|
} >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "[verify-parallel] elapsed=${ELAPSED}s | pass=$PASS fail=0 | all checks green" >&2
|
|
exit 0
|