|
| 1 | +// #3896 follow-up — the empty-state gate (CLI entry). |
| 2 | +// |
| 3 | +// pnpm --filter @objectstack/spec check:empty-state |
| 4 | +// pnpm --filter @objectstack/spec check:empty-state -- --dump # inventory, seeding aid |
| 5 | +// |
| 6 | +// See empty-state.mts for what it enforces and why, and |
| 7 | +// packages/spec/liveness/README.md § "Empty-state semantics" for the author-facing |
| 8 | +// version. |
| 9 | + |
| 10 | +import { readdirSync, readFileSync, existsSync } from 'node:fs'; |
| 11 | +import { join, resolve, relative, dirname } from 'node:path'; |
| 12 | +import { fileURLToPath } from 'node:url'; |
| 13 | + |
| 14 | +import { EMPTY_STATE_REGISTRY } from './empty-state-registry.mts'; |
| 15 | +import { checkEmptyState } from './empty-state.mts'; |
| 16 | + |
| 17 | +const here = dirname(fileURLToPath(import.meta.url)); |
| 18 | +const specRoot = resolve(here, '../..'); // packages/spec |
| 19 | +const repoRoot = resolve(specRoot, '../..'); |
| 20 | +const srcRoot = join(specRoot, 'src'); |
| 21 | + |
| 22 | +/** The authorable spec surface: the schema files an author (or a model) reads. */ |
| 23 | +function collectSchemaFiles(dir: string, out: string[] = []): string[] { |
| 24 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 25 | + const full = join(dir, entry.name); |
| 26 | + if (entry.isDirectory()) { |
| 27 | + collectSchemaFiles(full, out); |
| 28 | + } else if (entry.name.endsWith('.zod.ts') && !entry.name.endsWith('.test.ts')) { |
| 29 | + out.push(full); |
| 30 | + } |
| 31 | + } |
| 32 | + return out; |
| 33 | +} |
| 34 | + |
| 35 | +const args = process.argv.slice(2); |
| 36 | +const asJson = args.includes('--json'); |
| 37 | +const dump = args.includes('--dump'); |
| 38 | + |
| 39 | +const files = collectSchemaFiles(srcRoot).sort(); |
| 40 | +const sources = new Map<string, string>( |
| 41 | + files.map((f) => [relative(repoRoot, f), readFileSync(f, 'utf8')]), |
| 42 | +); |
| 43 | + |
| 44 | +const { hits, findings, notes } = checkEmptyState({ |
| 45 | + sources, |
| 46 | + registry: EMPTY_STATE_REGISTRY, |
| 47 | + exists: (p) => existsSync(join(repoRoot, p)), |
| 48 | +}); |
| 49 | + |
| 50 | +if (dump) { |
| 51 | + console.log(`Permissive-empty statements across ${sources.size} schema files:\n`); |
| 52 | + for (const h of hits) { |
| 53 | + console.log(` ${h.file}:${h.line} [${h.property ?? '<unresolved>'}]`); |
| 54 | + console.log(` ${h.text}`); |
| 55 | + } |
| 56 | + process.exit(0); |
| 57 | +} |
| 58 | + |
| 59 | +if (asJson) { |
| 60 | + console.log(JSON.stringify({ scanned: sources.size, hits, findings, notes }, null, 2)); |
| 61 | + process.exit(findings.length > 0 ? 1 : 0); |
| 62 | +} |
| 63 | + |
| 64 | +console.log(`Empty-state gate — ${sources.size} schema files, ${hits.length} permissive-empty statements.`); |
| 65 | + |
| 66 | +const byKind = new Map<string, typeof findings>(); |
| 67 | +for (const f of findings) { |
| 68 | + const list = byKind.get(f.kind) ?? []; |
| 69 | + list.push(f); |
| 70 | + byKind.set(f.kind, list); |
| 71 | +} |
| 72 | + |
| 73 | +const HEADINGS: Record<string, string> = { |
| 74 | + unregistered: 'UNCLASSIFIED — a permissive empty state nobody signed off on', |
| 75 | + unresolved: 'UNRESOLVED — statement could not be tied to a property', |
| 76 | + stale: 'STALE — registered, but the statement is gone', |
| 77 | + 'missing-rationale': 'NO RATIONALE', |
| 78 | + 'missing-evidence': 'NO EVIDENCE — access gates must cite their enforcement site', |
| 79 | + 'rotted-evidence': 'ROTTED EVIDENCE', |
| 80 | +}; |
| 81 | + |
| 82 | +for (const [kind, list] of byKind) { |
| 83 | + console.log(`\n${HEADINGS[kind] ?? kind} (${list.length}):`); |
| 84 | + for (const f of list) { |
| 85 | + const where = f.line ? `${f.file}:${f.line}` : f.file; |
| 86 | + console.log(` ✗ ${where}${f.property ? ` — ${f.property}` : ''}`); |
| 87 | + console.log(` ${f.message}`); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +if (notes.length > 0) { |
| 92 | + console.log(`\nNotes — narrative, not enforced (${notes.length}):`); |
| 93 | + for (const n of notes) console.log(` · ${n.file}:${n.line} — ${n.message}`); |
| 94 | +} |
| 95 | + |
| 96 | +if (findings.length === 0) { |
| 97 | + const counts = EMPTY_STATE_REGISTRY.reduce<Record<string, number>>((acc, e) => { |
| 98 | + acc[e.semantics] = (acc[e.semantics] ?? 0) + 1; |
| 99 | + return acc; |
| 100 | + }, {}); |
| 101 | + const summary = Object.entries(counts) |
| 102 | + .sort(([a], [b]) => a.localeCompare(b)) |
| 103 | + .map(([k, v]) => `${v} ${k}`) |
| 104 | + .join(', '); |
| 105 | + console.log(`✓ all classified (${summary})`); |
| 106 | + process.exit(0); |
| 107 | +} |
| 108 | + |
| 109 | +console.log(`\n${findings.length} finding(s) — see packages/spec/scripts/liveness/empty-state-registry.mts`); |
| 110 | +process.exit(1); |
0 commit comments