You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte-kit-vice/scripts/check-gate.ts

92 lines
3.2 KiB

/**
* check-gate — svelte-check with the debt ledger applied.
*
* Policy (P0 fase A, audit 2026-08-26):
* - Any ERROR under `src/` or `scripts/` fails the gate: framework source and
* the tooling that guards it owe zero (scripts/ is type-checked since the
* closure plan's F5; agent probes `scripts/__*` are excluded in tsconfig.json).
* - ERRORs elsewhere (the demo tree) are measured against the per-file
* ledger in `check-debt.ts`. A file above its ledgered count — or an
* erroring file the ledger does not name — fails. Counts only shrink:
* when a file drops below its entry the gate says so; lower the entry
* in the same change.
* - The run must end with svelte-check's `COMPLETED` marker and the
* parsed error count must match the marker's total — a gate that
* inspected nothing must fail, never pass.
*
* Usage: npm run check:gate
*/
import { spawnSync } from 'node:child_process';
import { CHECK_DEBT } from './check-debt';
const run = spawnSync('npm', ['run', 'check'], {
shell: true,
encoding: 'utf8',
maxBuffer: 64 * 1024 * 1024
});
const out = `${run.stdout ?? ''}\n${run.stderr ?? ''}`;
const completed = /^\d+\s+COMPLETED\s+\d+\s+FILES\s+(\d+)\s+ERRORS\b/m.exec(out);
if (!completed) {
console.error('check-gate: svelte-check produced no COMPLETED marker — treating as failure.');
console.error(out.slice(-2000));
process.exit(1);
}
const reportedErrors = Number(completed[1]);
const errorsByFile = new Map<string, number>();
let parsedErrors = 0;
for (const line of out.split('\n')) {
const m = /^\d+\s+ERROR\s+"(.+?)"\s+\d+:\d+\s+/.exec(line);
if (!m) continue;
const file = m[1].replace(/\\+/g, '/');
errorsByFile.set(file, (errorsByFile.get(file) ?? 0) + 1);
parsedErrors += 1;
}
if (parsedErrors !== reportedErrors) {
console.error(
`check-gate: parsed ${parsedErrors} ERROR lines but svelte-check reported ${reportedErrors} — output format drifted, refusing to judge.`
);
process.exit(1);
}
let failed = false;
const OWES_ZERO = ['src/', 'scripts/'];
const owesZero = (file: string) => OWES_ZERO.some((prefix) => file.startsWith(prefix));
const srcErrors = [...errorsByFile].filter(([file]) => owesZero(file));
if (srcErrors.length > 0) {
failed = true;
console.error(
'check-gate: framework source or tooling has type errors (src/ and scripts/ owe zero):'
);
for (const [file, count] of srcErrors) console.error(` ${file}: ${count}`);
}
for (const [file, count] of errorsByFile) {
if (owesZero(file)) continue;
const allowed = CHECK_DEBT[file];
if (allowed === undefined) {
failed = true;
console.error(`check-gate: NEW debt — ${file} has ${count} error(s) and no ledger entry.`);
} else if (count > allowed) {
failed = true;
console.error(
`check-gate: debt GREW — ${file} has ${count} error(s), ledger allows ${allowed}.`
);
} else if (count < allowed) {
console.log(
`check-gate: debt shrank — ${file} is at ${count} (ledger says ${allowed}); lower the entry.`
);
}
}
for (const file of Object.keys(CHECK_DEBT)) {
if (!errorsByFile.has(file)) {
console.log(`check-gate: ${file} is clean — remove its ledger entry.`);
}
}
if (!failed) console.log(`check-gate: OK — ${reportedErrors} error(s), all within the ledger.`);
process.exit(failed ? 1 : 0);

Powered by TurnKey Linux.