From fedb1d95ef0ec17ba6752c42c7b8cf758d1e4e4c Mon Sep 17 00:00:00 2001 From: Quino Terrasa <26749475+espetro@users.noreply.github.com> Date: Wed, 2 Dec 2026 12:37:43 -0800 Subject: [PATCH] ci: build and run the native ELF in CI before release MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 0.7.0 daemon crash shipped because CI never built the ELF: pnpm test runs node bin/coordinate.mjs (the loose dynamic path), release.yml only ran --help on the two Linux binaries, and the darwin binaries were only file-checked. An absent optional positional arrived as null in the compiled binary and scriptc's type claim rejected it at daemon start. - scripts/native-smoke.sh: the verb battery — every --help, root --help, no-args exit 1, unknown-verb did-you-mean, enqueue/status round trip, dash-leading command capture, jobs --json, events, lease acquire/ release/list/waits, cap show/set, dead retry --all, agent register/ heartbeat/unregister, inbox, daemon start-and-stay-up (the 0.7.0 killer), daemon restart handoff, daemon --help isolation. Validated both ways: passes on the fixed 0.7.0 binary, exits non-zero on the broken one at the daemon step. - ci.yml: native-smoke job builds the linux-x64 ELF on every PR and runs the battery. - release.yml: darwin-smoke job (macos-latest) builds the darwin-arm64 ELF, builds the SQLite shim archive, runs the battery; the release job now needs it. The Linux smoke step runs the full battery on both linux-gnu and linux-musl binaries instead of just --help. - scripts/scriptc-gate.mjs + scriptc-coverage-baseline.json: coverage ratchet — fails on static-statement drop, dynamic-statement growth, or new diagnostics in our code. ci.yml: scriptc-coverage job. Branch protection on main now requires all five CI contexts (ts tests, oxlint, tsc, native ELF smoke, scriptc coverage ratchet). --- .github/workflows/ci.yml | 52 ++++++++++++ .github/workflows/release.yml | 46 +++++++++- scripts/native-smoke.sh | 113 +++++++++++++++++++++++++ scripts/scriptc-coverage-baseline.json | 22 +++++ scripts/scriptc-gate.mjs | 113 +++++++++++++++++++++++++ 5 files changed, 343 insertions(+), 3 deletions(-) create mode 100755 scripts/native-smoke.sh create mode 100644 scripts/scriptc-coverage-baseline.json create mode 100644 scripts/scriptc-gate.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1adebe3..4853989 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -59,3 +59,55 @@ jobs: run: pnpm install --frozen-lockfile - name: Typecheck run: pnpm typecheck + + # The Node test suite never runs the compiled binary, but the native ELF is + # what ships: scriptc type claims reject values Node accepts (the 0.7.0 + # daemon died on an absent optional positional that arrives as null). This + # job builds the linux-x64 ELF and runs it through the verb paths the Node + # tests cannot reach. scripts/native-smoke.sh is the battery; it caught the + # 0.7.0 regression when replayed against that binary. + native-smoke: + name: native ELF smoke (linux-x64) + runs-on: ubuntu-24.04 + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 24 + - name: Install pnpm + run: corepack enable && corepack prepare pnpm@12.16.1 --activate + - name: Install scriptc 0.4.16 + run: npm install -g scriptc@0.4.16 + - name: Install dependencies + run: pnpm install --frozen-lockfile + - name: Build the linux-x64 ELF + run: | + scriptc build src/main-elf.ts \ + --npm-static effect,@cutils/sqlite,@cutils/cli \ + --ffi node_modules/@cutils/sqlite/ffi.json \ + -o coordinate-smoke + - name: Run the verb battery against the binary + run: bash scripts/native-smoke.sh ./coordinate-smoke + + # Ratchet the scriptc static tier: fail when the compiled program loses + # static coverage or gains new lowering diagnostics in our own code. The + # baseline is scripts/scriptc-coverage-baseline.json; refresh it with + # scripts/scriptc-gate.mjs --update when the change is deliberate. + scriptc-coverage: + name: scriptc coverage ratchet + runs-on: ubuntu-24.04 + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 24 + - name: Install pnpm + run: corepack enable && corepack prepare pnpm@12.16.1 --activate + - name: Install scriptc 0.4.16 + run: npm install -g scriptc@0.4.16 + - name: Install dependencies + run: pnpm install --frozen-lockfile + - name: Run the coverage gate + run: node scripts/scriptc-gate.mjs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 64696b5..ce62f0b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -94,8 +94,8 @@ jobs: - name: Smoke test Linux binaries run: | VERSION="${GITHUB_REF_NAME#v}" - "./coordinate-${VERSION}-linux-x64-gnu" --help - "./coordinate-${VERSION}-linux-x64-musl" --help + bash scripts/native-smoke.sh "./coordinate-${VERSION}-linux-x64-gnu" + bash scripts/native-smoke.sh "./coordinate-${VERSION}-linux-x64-musl" - name: Build darwin-arm64 (cross-compiled, not runnable here) run: | @@ -167,6 +167,44 @@ jobs: path: dist/ if-no-files-found: error + # The darwin binaries cross-compile on Linux, which proves the link but not + # the runtime: a Mach-O that crashes on launch still passes a file-type + # check. This job builds the darwin-arm64 binary natively on a macOS runner + # and runs the same verb battery as the Linux smoke, so a native-only + # regression (the 0.7.0 daemon null-arg failure) cannot ship. + darwin-smoke: + name: darwin-arm64 native smoke + needs: gate + runs-on: macos-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 24 + - name: Install pnpm + run: corepack enable && corepack prepare pnpm@12.16.1 --activate + - name: Install scriptc 0.4.16 + run: npm install -g scriptc@0.4.16 + - name: Install dependencies + run: pnpm install --frozen-lockfile + - name: Build the SQLite static archive for the ELF link + run: | + cd node_modules/@cutils/sqlite + mkdir -p build + cc -O2 -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_FTS5 -c vendor/shim/sqlite_shim.c -o build/sqlite_shim.o + cc -O2 -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_FTS5 -c vendor/sqlite-amalgamation/sqlite3.c -o build/sqlite3.o + ar rcs build/libsqlite3-shim.a build/sqlite_shim.o build/sqlite3.o + ranlib build/libsqlite3-shim.a + - name: Build the darwin-arm64 ELF natively + run: | + scriptc build src/main-elf.ts \ + --npm-static effect,@cutils/sqlite,@cutils/cli \ + --ffi node_modules/@cutils/sqlite/ffi.json \ + -o coordinate-smoke + - name: Run the verb battery against the binary + run: bash scripts/native-smoke.sh ./coordinate-smoke + # The windows spike verdict is conditional go: the exe builds via zig on # Linux but nothing ran it. This job is the smoke pass (enqueue, status, # daemon claim loop) on a real Windows runner. Windows is best-effort for @@ -211,9 +249,11 @@ jobs: "$EXE" events | grep -q '"done"' [ "$STATE" = done ] + # The darwin smoke gates the release: a Mach-O that passes a file-type + # check but dies on launch must not ship. release: name: attach to the GitHub Release - needs: [gate, binaries] + needs: [gate, binaries, darwin-smoke] runs-on: ubuntu-latest timeout-minutes: 15 steps: diff --git a/scripts/native-smoke.sh b/scripts/native-smoke.sh new file mode 100755 index 0000000..a7307d6 --- /dev/null +++ b/scripts/native-smoke.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +# Native smoke battery: run the compiled ELF through the verb paths that the +# Node test suite cannot reach. The 0.7.0 daemon died here (an absent optional +# positional arrives as null and the native type claim rejects it), so every +# optional-arg verb and the daemon start path are exercised on purpose. +# Usage: scripts/native-smoke.sh +set -euo pipefail + +BIN="${1:?usage: native-smoke.sh }" +TMP="$(mktemp -d)" +trap 'rm -rf "$TMP"' EXIT +export COORDINATE_DB="$TMP/queue.db" +export COORDINATE_CONFIG="$TMP/config.toml" +export COORDINATE_MACHINE="smoke" +export XDG_STATE_HOME="$TMP/state" +export XDG_CONFIG_HOME="$TMP/conf" +: > "$COORDINATE_CONFIG" + +step() { printf 'smoke: %s\n' "$1"; } + +# Every verb answers --help without touching the DB. +for verb in enqueue status jobs wait-on release cancel run lease send inbox agent logs why health stats cap events forward dead audit tui doctor daemon service; do + step "$verb --help" + "$BIN" "$verb" --help >/dev/null +done + +step "root --help" +"$BIN" --help >/dev/null + +step "no args exits 1" +if "$BIN" >/dev/null 2>&1; then + echo "smoke: no-args must exit 1" >&2 + exit 1 +fi + +step "unknown verb exits 1 with a suggestion" +if "$BIN" statu >/dev/null 2>&1; then + echo "smoke: unknown verb must exit 1" >&2 + exit 1 +fi + +step "enqueue -> status round trip" +JID="$("$BIN" enqueue --cwd "$TMP" -- true)" +"$BIN" status "$JID" | grep -q '"status": "queued"' + +step "enqueue without -- captures a dash-leading command" +"$BIN" enqueue --cwd "$TMP" ls -la >/dev/null + +step "status with no id lists rows (optional id arg)" +"$BIN" status >/dev/null + +step "jobs --json lists rows" +"$BIN" jobs --json >/dev/null + +step "events with no filter and with --job 0 (numeric option)" +"$BIN" events >/dev/null +"$BIN" events --job 0 >/dev/null + +step "lease acquire/release (required positional)" +GRANT="$("$BIN" lease acquire tool:smoke --cost 1 | python3 -c 'import json,sys; print(json.load(sys.stdin)["grant_id"])')" +"$BIN" lease release "$GRANT" >/dev/null + +step "lease list/waits (group verbs, no positionals)" +"$BIN" lease list >/dev/null +"$BIN" lease waits >/dev/null + +step "cap show/set (group verbs with options)" +"$BIN" cap show >/dev/null +"$BIN" cap set --machine-cap 8 >/dev/null + +step "dead retry --all (optional id + bool)" +"$BIN" dead retry --all >/dev/null + +step "agent register/heartbeat/unregister (group verbs)" +"$BIN" agent register smoke-agent >/dev/null +"$BIN" agent heartbeat smoke-agent >/dev/null +"$BIN" agent unregister smoke-agent >/dev/null + +step "inbox with no owner fails cleanly (optional positional)" +if "$BIN" inbox >/dev/null 2>&1; then + echo "smoke: inbox with no owner must exit non-zero" >&2 + exit 1 +fi +"$BIN" inbox "agent:smoke-agent" >/dev/null 2>&1 || true + +step "daemon starts and stays up (the 0.7.0 failure)" +"$BIN" daemon & +DAEMON_PID=$! +sleep 3 +if ! kill -0 "$DAEMON_PID" 2>/dev/null; then + echo "smoke: daemon exited within 3s of start" >&2 + wait "$DAEMON_PID" || true + exit 1 +fi +"$BIN" health | grep -q "daemon up" +kill "$DAEMON_PID" 2>/dev/null || true +wait "$DAEMON_PID" 2>/dev/null || true + +step "daemon restart hands off" +"$BIN" daemon >/dev/null 2>&1 & +DAEMON_PID=$! +sleep 2 +RESTART_JSON="$("$BIN" daemon restart)" +NEW_PID="$(printf '%s' "$RESTART_JSON" | python3 -c 'import json,sys; print(json.load(sys.stdin)["newPid"])')" +sleep 2 +"$BIN" health | grep -q "daemon up" +# The replacement daemon pid came from the restart record we just parsed. +kill "$NEW_PID" 2>/dev/null || true + +step "daemon --help does not touch the running daemon" +"$BIN" daemon --help >/dev/null + +printf 'smoke: all checks passed\n' diff --git a/scripts/scriptc-coverage-baseline.json b/scripts/scriptc-coverage-baseline.json new file mode 100644 index 0000000..a991553 --- /dev/null +++ b/scripts/scriptc-coverage-baseline.json @@ -0,0 +1,22 @@ +{ + "entry": "src/main-elf.ts", + "compilerVersion": "0.4.16", + "statementsTotal": 10493, + "statementsStatic": 10482, + "statementsDynamic": 4238, + "percentStatic": 99, + "ourDiagnostics": [ + { + "code": "SC6014", + "file": "src/main.ts", + "line": 647, + "message": "objects of this type enumerate their keys as 'id, owner_id, kind, sender, dedupe_key, deliver_at, state, created_at, delivered_at, payload' natively, but an object spread here copies keys from a value whose key order is not known; under Node each object lists its keys in the order it created them, and the program observes the key order of this type (JSON.stringify, Object.keys, for-in, console output or a value passed to untyped code)" + }, + { + "code": "SC6101", + "file": "src/daemon.ts", + "line": 616, + "message": "10 functions that likely run often use untyped values, which runs slower than typed code:" + } + ] +} diff --git a/scripts/scriptc-gate.mjs b/scripts/scriptc-gate.mjs new file mode 100644 index 0000000..39eca2e --- /dev/null +++ b/scripts/scriptc-gate.mjs @@ -0,0 +1,113 @@ +#!/usr/bin/env node +// Ratchet the scriptc static tier: fail when the compiled program loses static +// coverage or gains new lowering diagnostics in our own code. +// +// The native ELF is what ships, and a function that falls off the static tier +// loses scriptc's type claims: the 0.7.0 daemon died on a null that only the +// native type claim rejected. This gate is not the fix for that incident (the +// native ELF smoke is), it is the drift alarm that catches the class. +// +// The baseline is a checked-in file, so a legitimate change refreshes it with +// an explicit diff rather than silently. Compare: static statement count, +// dynamic statement count, and the set of diagnostics pointing at files in +// this repo (node_modules diagnostics are the dependencies' problem). +// +// Usage: scripts/scriptc-gate.mjs [--update] +import { execFileSync } from "node:child_process"; +import { readFileSync, writeFileSync, existsSync } from "node:fs"; +import path from "node:path"; + +const ROOT = path.resolve(import.meta.dirname, ".."); +const ENTRY = "src/main-elf.ts"; +const FFI = "node_modules/@cutils/sqlite/ffi.json"; +const BASELINE = path.join(ROOT, "scripts", "scriptc-coverage-baseline.json"); +const UPDATE = process.argv.includes("--update"); + +function runCoverage() { + const out = execFileSync( + "scriptc", + ["coverage", ENTRY, "--ffi", FFI, "--print=diagnostics"], + { cwd: ROOT, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }, + ); + return JSON.parse(out); +} + +function oursDiagnostics(report) { + const out = []; + for (const d of report.diagnostics ?? []) { + const file = d.file ?? ""; + if (file.includes("node_modules")) continue; + const rel = path.relative(ROOT, file); + out.push({ code: d.code, file: rel, line: d.line, message: (d.message ?? "").split("\n")[0] }); + } + out.sort((a, b) => a.code.localeCompare(b.code) || a.file.localeCompare(b.file) || a.line - b.line); + return out; +} + +function snapshot(report) { + return { + entry: ENTRY, + compilerVersion: report.compilerVersion ?? "", + statementsTotal: report.stats?.statementsTotal ?? 0, + statementsStatic: report.stats?.statementsStatic ?? 0, + statementsDynamic: report.stats?.statementsDynamic ?? 0, + percentStatic: report.stats?.percentStatic ?? 0, + ourDiagnostics: oursDiagnostics(report), + }; +} + +const report = runCoverage(); +const now = snapshot(report); + +if (UPDATE) { + writeFileSync(BASELINE, JSON.stringify(now, null, 2) + "\n"); + console.log("scriptc-gate: baseline updated"); + console.log(` static ${now.statementsStatic}/${now.statementsTotal} (${now.percentStatic}%)`); + console.log(` our diagnostics: ${now.ourDiagnostics.length}`); + process.exit(0); +} + +if (!existsSync(BASELINE)) { + console.error("scriptc-gate: no baseline at " + path.relative(ROOT, BASELINE)); + console.error("scriptc-gate: create it with scripts/scriptc-gate.mjs --update"); + process.exit(1); +} + +const before = JSON.parse(readFileSync(BASELINE, "utf8")); +let failed = false; + +if (now.statementsStatic < before.statementsStatic) { + console.error( + `scriptc-gate: static statements dropped ${before.statementsStatic} -> ${now.statementsStatic} ` + + `(-${before.statementsStatic - now.statementsStatic})`, + ); + failed = true; +} +if (now.statementsDynamic > before.statementsDynamic) { + console.error( + `scriptc-gate: dynamic statements grew ${before.statementsDynamic} -> ${now.statementsDynamic} ` + + `(+${now.statementsDynamic - before.statementsDynamic})`, + ); + failed = true; +} + +const beforeKeys = new Set(before.ourDiagnostics.map((d) => d.code + " " + d.file + ":" + d.line)); +const added = now.ourDiagnostics.filter((d) => !beforeKeys.has(d.code + " " + d.file + ":" + d.line)); +if (added.length > 0) { + console.error("scriptc-gate: new lowering diagnostics in our code:"); + for (const d of added) { + console.error(` ${d.code} ${d.file}:${d.line} ${d.message}`); + } + failed = true; +} + +if (failed) { + console.error("scriptc-gate: the static tier regressed. Fix the code, or refresh the baseline"); + console.error(" with scripts/scriptc-gate.mjs --update when the change is deliberate."); + process.exit(1); +} + +console.log( + `scriptc-gate: ok (static ${now.statementsStatic}/${now.statementsTotal} (${now.percentStatic}%), ` + + `our diagnostics ${now.ourDiagnostics.length})`, +);