#!/usr/bin/env bun
// Every tracked app's own `x verify`, built first, run as a BLOCKING check on this repo —
// `examples/dummy`, the deployed `dummy/social-media-clone` and `dummy/docs-site`. A ratchet rather
// than a pass/fail: every step passing today must keep passing, every step pinned in that app's
// `expectedRed` must still be failing, and the moment an app's `typecheck` comes off the pin it
// has to join the root `tsc -b` solution. Red never becomes the resting state.
//
// bun run scripts/reference-app-gate.ts [--app
] [--jobs N] [--json] # --app: one app, its own CI runner
// bun run scripts/reference-app-gate.ts --unpin :[,] # retained for future pins
//
// As of 2026-11, all three expectedRed tables are empty and every declared step is asserted;
// no tracked app currently has a pin to remove. New red steps remain regressions, not pins.
// The pins live in scripts/lib/gated-apps.ts; this file owns only what the ratchet does with them.
// `--app` is scripts/lib/app-select.ts and the `--unpin` edit is scripts/reference-app-unpin.ts.
// why: total RAM bounds how many apps run at once; Bun ships no memory primitive.
import { totalmem } from 'node:os';
// `join` builds the host-separator paths to each app root and the CLI entry point; Bun ships no
// equivalent, and both must be absolute so the subprocess's cwd cannot change what runs.
import { join } from 'node:path';
import type { Runner } from '@ultimat3/cli';
import {
exec,
normalizeReferencePath,
readVerifyFloor,
VERIFY_FLOOR_FILE,
VERIFY_STEP_NAMES,
} from '@ultimat3/cli';
import { appJobs, gateWidth, sharedServices } from './lib/app-gate-schedule';
import { selectApps } from './lib/app-select';
import { flagString, parseScriptArgs } from './lib/args';
import { leasePool, serviceKeys } from './lib/coverage-schedule';
import type { GatedApp } from './lib/gated-apps';
import { GATED_APPS, PINS_FILE } from './lib/gated-apps';
import type { Finding } from './lib/log';
import { renderFinding, report } from './lib/log';
import { buildApp } from './lib/reference-app-build';
import { repoRoot } from './lib/run';
import { badFlag, unpin } from './reference-app-unpin';
export const ROOT_TSCONFIG = 'tsconfig.json';
/** The command that performs the stale-pin edit, so the finding can hand over a runnable line. */
export const unpinCommand = (app: GatedApp, steps: readonly string[]): string =>
`bun run scripts/reference-app-gate.ts --unpin ${app.dir}:${steps.join(',')}`;
/** Runnable from the repo root, and the same gate this script runs — just rendered for a human. */
export const reproduce = (app: GatedApp): string => {
const up = '../'.repeat(app.dir.split('/').length);
return `cd ${app.dir} && bun run ${up}packages/cli/src/bin.ts verify`;
};
export interface GateStep {
readonly name: string;
readonly ok: boolean;
readonly skipped: boolean;
readonly findings: readonly Finding[];
/**
* A failed step's captured stdout — the failing test's NAME, its assertion diff and its
* `file:line`. `packages/cli/src/output.ts:199-207` already carries it through `x verify --json`
* for exactly this reason, with a comment saying the alternative is a log that reads "one or
* more unit tests failed" and nothing else. This file then dropped it on the floor, so that is
* precisely what CI showed: a shard's exit code, no test name, and nothing in the log to find.
*/
readonly output?: string;
/**
* The step's warnings, printed only when it is red: a `live` step whose database never came up
* fails as a vanished suite, and the reason (`X_TEST_SERVICES_UNAVAILABLE`) is a warning.
*/
readonly warnings?: readonly string[];
/** The step's wall time as the gate printed it: the CI log's only answer to where the minutes went. */
readonly durationMs?: number;
}
const asFinding = (value: unknown): Finding | undefined => {
if (typeof value !== 'object' || value === null) return undefined;
const { code, cause, fix, at } = value as Record;
if (typeof code !== 'string' || typeof cause !== 'string' || typeof fix !== 'string') {
return undefined;
}
return typeof at === 'string' ? { code, cause, fix, at } : { code, cause, fix };
};
const asStep = (value: unknown): GateStep | undefined => {
if (typeof value !== 'object' || value === null) return undefined;
const { name, ok, skipped, findings } = value as Record;
if (typeof name !== 'string' || typeof ok !== 'boolean') return undefined;
const { output, durationMs, warnings } = value as Record;
const said = Array.isArray(warnings)
? warnings.filter((line): line is string => typeof line === 'string')
: [];
return {
name,
ok,
skipped: skipped === true,
findings: (Array.isArray(findings) ? findings : [])
.map(asFinding)
.filter((f) => f !== undefined),
// Absent, never `''`: "captured nothing" and "output dropped" are different states.
...(typeof output === 'string' && output.length > 0 ? { output } : {}),
...(said.length > 0 ? { warnings: said } : {}),
...(typeof durationMs === 'number' && Number.isFinite(durationMs) && durationMs >= 0
? { durationMs }
: {}),
};
};
/**
* The step objects as the gate PRINTED them, before any field is dropped. Every consumer of a
* `verify --json` table goes through this one reader: `asStep` keeps the four fields the ratchet
* judges (and the time `stepLines` prints) and discards the rest, so a second consumer that wants the timings had re-implemented
* the line scan, the `JSON.parse` guard and the array check beside it — three copies of one
* decision about what "unreadable" means, free to disagree.
*
* `x verify --json` prints one object on its own line. The last `{`-line is taken rather than the
* whole stream so a stray write from a step's subprocess cannot make the gate unreadable — and
* `undefined` (no table at all) is treated as the worst outcome, not as "nothing failed".
*/
export const parseStepPayloads = (stdout: string): readonly unknown[] | undefined => {
const line = stdout
.split('\n')
.map((part) => part.trim())
.findLast((part) => part.startsWith('{'));
if (line === undefined) return undefined;
let payload: unknown;
try {
payload = JSON.parse(line);
} catch {
return undefined;
}
// Guarded the same way `asStep`/`asFinding` guard their own input: `payload` is `unknown` from
// here on, so a shape this parser cannot use — `null`, an array, a bare number — reads as "no
// table" rather than a thrown TypeError reading `.steps` off it.
const steps =
typeof payload === 'object' && payload !== null
? (payload as { readonly steps?: unknown }).steps
: undefined;
return Array.isArray(steps) ? steps : undefined;
};
/** The ratchet's view of that table: the four fields it judges, or `undefined` if any step is not one. */
export const parseSteps = (stdout: string): readonly GateStep[] | undefined => {
const payloads = parseStepPayloads(stdout);
if (payloads === undefined) return undefined;
const parsed = payloads.map(asStep);
return parsed.every((step) => step !== undefined) ? parsed : undefined;
};
export const redSteps = (steps: readonly GateStep[]): readonly string[] =>
steps.filter((step) => !(step.ok || step.skipped)).map((step) => step.name);
/**
* The red/stale-pin checks below only ever look at the steps the table actually contains — a step
* `verify --json` silently dropped (a crash mid-run, a future step wired into the CLI but never
* into its own JSON output) is invisible to both, and a passing gate would not mean "17 steps
* checked", it would mean "however many the process happened to print". Comparing the received
* names against the full declared set is what makes a missing step a finding instead of a step
* nobody was checking.
*/
export const declaredStepIssues = (
steps: readonly GateStep[],
declaredSteps: readonly string[],
): readonly string[] => {
const names = steps.map((step) => step.name);
const missing = declaredSteps.filter((name) => !names.includes(name));
const unknown = [...new Set(names.filter((name) => !declaredSteps.includes(name)))];
const seen = new Set();
const duplicate = new Set();
for (const name of names) {
if (seen.has(name)) duplicate.add(name);
seen.add(name);
}
return [
...missing.map((name) => `${name} is declared but missing from the step table`),
...unknown.map((name) => `${name} is in the step table but not a declared step`),
...[...duplicate].map((name) => `${name} appears more than once in the step table`),
];
};
export interface GateInput {
readonly app: GatedApp;
readonly steps: readonly GateStep[] | undefined;
readonly referenced: boolean;
/** The complete step set a real run reports against — `VERIFY_STEP_NAMES` at the real call
* site. A parameter, not a hardcoded import, so a test can pin a small closed world instead of
* asserting against every step this repo happens to declare today. */
readonly declaredSteps: readonly string[];
}
/**
* The whole decision for ONE app, separated from running anything so it can be tested both ways
* round. Every finding names the app: with more than one gated app, "typecheck regressed" with no
* directory is a finding the reader has to reproduce twice to place.
*/
export const gateFindings = (input: GateInput): readonly Finding[] => {
const { app, steps, referenced, declaredSteps } = input;
const expectedRed = app.expectedRed;
if (steps === undefined || steps.length === 0) {
return [
{
code: 'X_REFERENCE_APP_REGRESSED',
cause: `${app.dir} printed no step table, so not one step could be checked`,
fix: reproduce(app),
at: app.dir,
},
];
}
const findings: Finding[] = [];
// Before red/stale-pin even get a say: a step missing from the table entirely is neither red
// nor a stale pin, it is a step nobody checked — and a duplicate or unknown name means the table
// itself cannot be trusted to answer either question correctly.
const shapeIssues = declaredStepIssues(steps, declaredSteps);
if (shapeIssues.length > 0) {
findings.push({
code: 'X_REFERENCE_APP_REGRESSED',
cause: `${app.dir}'s step table does not match the declared steps: ${shapeIssues.join('; ')}`,
fix: reproduce(app),
at: app.dir,
});
}
const red = redSteps(steps);
const regressed = red.filter((name) => !(name in expectedRed));
if (regressed.length > 0) {
findings.push({
code: 'X_REFERENCE_APP_REGRESSED',
cause: `${regressed.join(', ')} passed for ${app.dir} and now ${regressed.length === 1 ? 'fails' : 'fail'}`,
fix: reproduce(app),
at: app.dir,
});
}
// A pinned step that came back SKIPPED is still pinned, never stale. "Not red" and "passes" were
// the same branch, so deleting the suite behind a pinned step reported it as repaired and handed
// over an `--unpin` line — after which the step is neither red nor pinned and the gate is green
// over a suite that no longer exists. `floorFindings` is the half that says so out loud.
const skipped = steps.filter((step) => step.skipped).map((step) => step.name);
const stale = Object.keys(expectedRed).filter(
(name) => !(red.includes(name) || skipped.includes(name)),
);
if (stale.length > 0) {
findings.push({
code: 'X_REFERENCE_APP_PIN_STALE',
cause: `${stale.join(', ')} now ${stale.length === 1 ? 'passes' : 'pass'} for ${app.dir} but ${stale.length === 1 ? 'is' : 'are'} still pinned as failing`,
// Runnable, not prose: this is the edit every landed fix makes, and `at` still names the
// file for a reader who would rather delete the lines themselves.
fix: unpinCommand(app, stale),
at: PINS_FILE,
});
}
// The build-graph half of the same promise: an app that compiles has no excuse for sitting
// outside `tsc -b`, and only the root solution proves the packages' emitted .d.ts are consumable
// by a real app rather than only their own sources.
if (!(red.includes('typecheck') || referenced)) {
findings.push({
code: 'X_REFERENCE_APP_UNREFERENCED',
cause: `${app.dir} typechecks but is not a project the root ${ROOT_TSCONFIG} references`,
fix: `{ "path": "${app.reference}" } // add to the "references" array in ${ROOT_TSCONFIG}`,
at: ROOT_TSCONFIG,
});
}
return findings;
};
export interface FloorInput {
readonly app: GatedApp;
readonly steps: readonly GateStep[];
/** The step names the app's committed `x.verify.json` holds; `undefined` when it commits none. */
readonly floor: readonly string[] | undefined;
}
/**
* The suite floor, judged from OUTSIDE the app. `x verify` already turns a floor-named step that
* finds nothing into a failure — but only for a floor that exists, and neither tracked app had one,
* so deleting a whole `contract`/`live`/`job`/`e2e` suite turned its step from red-and-pinned into
* skipped-and-green: not a regression, not a stale pin, and both gates stayed green over a suite
* that had ceased to exist. Two rules, mirror images, and the pair is what makes the floor a
* ratchet rather than a note:
*
* - a step this run PROVED applies that the floor does not name — the floor is behind, and every
* step it omits is one whose deletion nothing would catch;
* - a step the floor names that this run SKIPPED — the suite is gone, or the floor drops the line
* in the commit that says why.
*
* Pure, like `gateFindings`: the caller reads the file.
*/
export const floorFindings = (input: FloorInput): readonly Finding[] => {
const { app, steps, floor } = input;
if (steps.length === 0) return [];
const applied = steps.filter((step) => !step.skipped).map((step) => step.name);
const floorPath = `${app.dir}/${VERIFY_FLOOR_FILE}`;
if (floor === undefined || floor.length === 0) {
return [
{
code: 'X_REFERENCE_APP_NO_FLOOR',
cause:
`${app.dir} commits no usable ${VERIFY_FLOOR_FILE}, so a deleted suite turns its step ` +
'from red into skipped and neither this gate nor the app’s own reports it',
fix: `{"steps":${JSON.stringify(applied)}} // the whole of ${floorPath}`,
at: floorPath,
},
];
}
const findings: Finding[] = [];
const missing = applied.filter((name) => !floor.includes(name));
if (missing.length > 0) {
findings.push({
code: 'X_REFERENCE_APP_NO_FLOOR',
cause: `${app.dir} ran ${missing.join(', ')} and its ${VERIFY_FLOOR_FILE} does not name ${missing.length === 1 ? 'it' : 'them'}, so deleting ${missing.length === 1 ? 'that suite' : 'those suites'} would read as a skip`,
fix: `{"steps":${JSON.stringify([...floor, ...missing])}} // the whole of ${floorPath}: it adds ${missing.join(', ')}`,
at: floorPath,
});
}
const vanished = steps.filter((step) => step.skipped && floor.includes(step.name));
if (vanished.length > 0) {
const names = vanished.map((step) => step.name);
findings.push({
code: 'X_REFERENCE_APP_REGRESSED',
cause: `${names.join(', ')} ran for ${app.dir} when its ${VERIFY_FLOOR_FILE} was written and now ${names.length === 1 ? 'finds' : 'find'} nothing to check`,
fix: reproduce(app),
at: floorPath,
});
}
return findings;
};
/** Whether an app is in the root build graph. An unreadable root config counts as "not in it". */
export const referencesApp = async (root: string, entry: string): Promise => {
const parsed: unknown = await Bun.file(join(root, ROOT_TSCONFIG))
.json()
.catch(() => undefined);
const references = (parsed as { readonly references?: unknown } | undefined)?.references;
if (!Array.isArray(references)) return false;
// A malformed entry (`null`, a bare string, a number) is a non-reference, not a crash: reading
// `.path` off it without this guard throws before the real entries ever get a chance to match.
// Compared through the CLI's own spelling rule, so `./examples/dummy`, `examples/dummy` and
// `examples/dummy/` answer the same here as they do in `package-shape`'s build-graph check.
const wanted = normalizeReferencePath(entry);
return references.some((ref) => {
if (typeof ref !== 'object' || ref === null) return false;
const path = (ref as { readonly path?: unknown }).path;
return typeof path === 'string' && normalizeReferencePath(path) === wanted;
});
};
/** The step table, so a CI reader sees the whole gate and not only what this check rejected. */
export const stepLines = (
steps: readonly GateStep[],
expectedRed: Readonly>,
): readonly string[] => {
const lines: string[] = [];
for (const step of steps) {
const mark = step.skipped ? '-' : step.ok ? '✓' : '✗';
const pinned = step.name in expectedRed ? ' pinned' : '';
const time =
step.durationMs === undefined ? '' : ` ${(step.durationMs / 1000).toFixed(1).padStart(6)}s`;
lines.push(` ${mark} ${step.name.padEnd(14)}${time}${pinned}`);
for (const finding of step.findings) lines.push(renderFinding(finding, ' '));
// Only on a red step, and only when it is NOT pinned: success stays quiet (the pattern
// `packages/cli/src/output.ts:205-207` sets), and a pinned red is expected, so printing its
// output every run would bury the one unpinned failure a reader is here for.
if (!step.ok && !step.skipped && pinned === '') {
for (const warning of step.warnings ?? []) lines.push(` ! ${warning}`);
for (const line of step.output?.split('\n') ?? []) lines.push(` | ${line}`);
}
}
return lines;
};
/** The red steps this app's table pins. The rest of `red` is a regression, never a held pin. */
export const pinnedRedSteps = (app: GatedApp, red: readonly string[]): readonly string[] =>
red.filter((name) => name in app.expectedRed);
/**
* One app's line in the summary. Red and pinned-red are separate numbers because they are separate
* facts: reporting every red step as "pinned red" described the failing run — the one this line
* exists to explain — as if the ratchet were holding.
*/
export const tally = (app: GatedApp, red: readonly string[], total: number): string =>
`${app.dir} ${total - red.length}/${total}, ${red.length} red (${pinnedRedSteps(app, red).length} pinned)`;
export const runAppGate = async (
root: string,
runner: Runner,
dir: string,
): Promise => {
const result = await runner(
['bun', 'run', join(root, 'packages/cli/src/bin.ts'), 'verify', '--json'],
{ cwd: join(root, dir) },
);
return parseSteps(result.stdout);
};
const SCRIPT = 'reference-app-gate';
if (import.meta.main) {
const args = parseScriptArgs(Bun.argv.slice(2));
const root = repoRoot();
const requested = flagString(args, 'unpin');
// The edit, not the gate: running both would report the ratchet as it was a moment ago.
if (requested !== undefined) report(await unpin(root, requested), args.json);
const apps = selectApps(args.flags.get('app'), GATED_APPS);
if ('code' in apps) report(badFlag(apps.cause, apps.fix), args.json);
const findings: Finding[] = [];
const lines: string[] = [];
const data: Record[] = [];
// Per-app, and in the summary rather than only in `lines`: `--json` drops `lines` entirely, and
// "the apps hold" without the counts tells a CI reader nothing about which way they are moving.
const tallies: string[] = [];
const jobs = appJobs(args.flags.get('jobs'), navigator.hardwareConcurrency, totalmem());
if (jobs === undefined) {
report(
badFlag(
`--jobs needs a positive integer, got ${String(args.flags.get('jobs'))}`,
'bun run scripts/reference-app-gate.ts --jobs 1',
),
args.json,
);
}
// Builds side by side: each writes only its own app's `.x/` and holds no service. Gates side by
// side only while no shared service URL is set (`app-gate-schedule.ts` says which collide), and
// every app's tests lease from the one machine slot pool either way. Results are collected in
// table order, so the report reads exactly as a serial run's does.
const shared = sharedServices(Bun.env, serviceKeys(root));
const width = gateWidth(jobs, shared);
const one = (): number => 1;
const builds = await leasePool(apps, jobs, one, (app) => buildApp(root, exec, app));
const gates = await leasePool(apps, width, one, async (app) => ({
steps: await runAppGate(root, exec, app.dir),
referenced: await referencesApp(root, app.reference),
// Read here rather than inside `gateFindings` for the same reason `referenced` is: the
// decision stays pure and testable, and the one thing that touches the disk is this pool.
floor: await readVerifyFloor(join(root, app.dir)),
}));
if (apps.length > 1) {
lines.push(
shared.length > 0
? `${apps.length} apps: built ${Math.min(jobs, apps.length)} at a time, gated one at a time — ${shared.join(', ')} is one server for every app`
: `${apps.length} apps: built and gated ${Math.min(jobs, apps.length)} at a time (--jobs)`,
);
}
apps.forEach((app, index) => {
const built = builds[index];
if (built !== undefined) findings.push(built);
const { steps, referenced, floor } = gates[index] as (typeof gates)[number];
findings.push(...gateFindings({ app, steps, referenced, declaredSteps: VERIFY_STEP_NAMES }));
findings.push(...floorFindings({ app, steps: steps ?? [], floor: floor?.steps }));
const red = steps === undefined ? [] : redSteps(steps);
const total = steps?.length ?? 0;
tallies.push(tally(app, red, total));
lines.push(`${app.dir}: ${total - red.length} of ${total} pass, ${red.length} red`);
lines.push(...(steps === undefined ? [] : stepLines(steps, app.expectedRed)));
data.push({
app: app.dir,
red,
pinnedRed: pinnedRedSteps(app, red),
pinned: Object.keys(app.expectedRed),
referenced,
});
});
report(
{
ok: findings.length === 0,
script: SCRIPT,
summary:
findings.length === 0
? `every pin holds — ${tallies.join('; ')}`
: `${findings.length} app finding(s) — ${tallies.join('; ')}`,
findings,
lines,
data: { apps: data },
},
args.json,
);
}