import { existsSync, readFileSync } from "node:fs"; import { relative, resolve } from "node:path"; const root = resolve(import.meta.dir, ".."); const bun = process.execPath; type GitState = { readonly status: string; readonly diff: string }; async function capture(executable: string, args: string[]): Promise { const child = Bun.spawn([executable, ...args], { cwd: root, stdout: "pipe", stderr: "pipe", }); const [stdout, stderr] = await Promise.all([ new Response(child.stdout).text(), new Response(child.stderr).text(), ]); const exitCode = await child.exited; if (exitCode !== 0) throw new Error(`${executable} ${args.join(" ")} failed with exit code ${exitCode}: ${stderr}`); return stdout; } async function gitState(): Promise { const [status, diff] = await Promise.all([ capture("git", ["status", "--short", "--untracked-files=all"]), capture("git", ["diff", "--binary", "--no-ext-diff"]), ]); return { status, diff }; } function assertNoGitStateChange(label: string, before: GitState, after: GitState): void { if (before.status !== after.status || before.diff !== after.diff) throw new Error(`${label} changed the tracked or untracked worktree state`); console.log(`✓ ${label}`); } async function run( label: string, executable: string, args: string[], environment: NodeJS.ProcessEnv = process.env, ): Promise { console.log(`\n▶ ${label}: ${[executable, ...args].join(" ")}`); const child = Bun.spawn([executable, ...args], { cwd: root, env: environment, stdout: "inherit", stderr: "inherit", }); const exitCode = await child.exited; if (exitCode !== 0) throw new Error(`${label} failed with exit code ${exitCode}`); console.log(`✓ ${label}`); } export function implementationSizeOffenders(root: string): string[] { const offenders: string[] = []; const directories = "apps examples integrations packages scripts templates".split(" "); for (const directory of directories) { const absolute = resolve(root, directory); if (!existsSync(absolute)) continue; for (const path of new Bun.Glob("**/*.{ts,tsx,mts,cts,js,jsx,mjs,cjs}").scanSync({ cwd: absolute, onlyFiles: true, })) { if (/(^|\/)(dist|node_modules|\.turbo|\.relkit)(\/|$)/.test(path)) continue; if (/(^|\/)[^/]+\.(?:test|spec)\.[^.]+$/.test(path)) continue; const file = resolve(absolute, path); const text = readFileSync(file, "utf8"); const lines = text.split(/\r?\n/).length - (text.endsWith("\n") || text.endsWith("\r") ? 1 : 0); if (lines > 250) offenders.push(`${relative(root, file)} (${lines} lines)`); } } return offenders.sort(); } function checkImplementationSize(): void { const offenders = implementationSizeOffenders(root); if (offenders.length > 0) throw new Error(`Implementation files exceed 250 lines:\n${offenders.join("\n")}`); console.log("✓ implementation-file limit (maximum 250 lines)"); } async function runStructuralAudit(): Promise { console.log("\n▶ configured structural audit: bun run konsistent -- check --format=json"); const child = Bun.spawn( [bun, "run", "konsistent", "--", "check", "--format=json", "--max-diagnostics=1000"], { cwd: root, stdout: "pipe", stderr: "pipe" }, ); const [stdout, stderr] = await Promise.all([ new Response(child.stdout).text(), new Response(child.stderr).text(), ]); const exitCode = await child.exited; if (stdout) process.stdout.write(stdout); if (stderr) process.stderr.write(stderr); if (exitCode > 1) throw new Error(`configured structural audit failed with exit code ${exitCode}`); console.log( exitCode === 1 ? "⚠ structural findings are advisory; configuration validation remains blocking" : "✓ configured structural audit", ); } async function main(): Promise { const installState = await gitState(); await run("frozen install", bun, ["install", "--frozen-lockfile"]); assertNoGitStateChange("frozen install no-diff", installState, await gitState()); await run("format check", bun, [ "x", "prettier", "--check", "AGENTS.md", "README.md", "CONTRIBUTING.md", "CODE_OF_CONDUCT.md", "SECURITY.md", "CHANGELOG.md", "RELEASE_NOTES.md", "package.json", "turbo.json", "tsconfig.base.json", "tsconfig.json", ".prettierrc.json", "eslint.config.mjs", "playwright.config.ts", ".changeset", ".github", "apps", "examples", "integrations", "packages", "scripts", "templates", "tests", ]); await run("lint", bun, ["run", "lint"]); await run("ESLint configuration check", bun, ["x", "eslint", "eslint.config.mjs"]); await run("boundaries and scope", bun, ["run", "check"]); await run("observability sink source scan", bun, ["run", "scripts/check-observability-sinks.ts"]); checkImplementationSize(); await run("Konsistent configuration validation", bun, ["run", "konsistent", "--", "validate"]); await runStructuralAudit(); const buildState = await gitState(); await run("build", bun, ["run", "build"]); assertNoGitStateChange("generated-file no-diff", buildState, await gitState()); await run("typecheck", bun, ["run", "typecheck"]); await run("execution package test types", bun, ["run", "test:execution:types"]); await run("type fixtures", bun, ["run", "test:types"]); await run("package tests", bun, ["run", "test:packages"]); await run("unit and schema tests", bun, ["run", "test:unit"]); await run("compiler and graph tests", bun, ["run", "test:compiler"]); await run("provider contracts", bun, ["run", "test:contracts"]); await run("integration tests", bun, ["run", "test:integration"]); await run("restart tests", bun, ["run", "test:restart"]); await run("inspector API tests", bun, ["run", "test:inspector"]); await run("MCP inspector tests", bun, ["run", "test:mcp:inspector"]); await run("generator tests", bun, ["run", "test:generator"]); await run("executable examples", bun, ["run", "test:examples"]); await run("documentation", bun, ["run", "test:docs"]); await run("jobs unit tests", bun, ["run", "test:jobs:unit"]); await run("jobs type fixtures", bun, ["run", "test:jobs:types"]); await run("jobs contract tests", bun, ["run", "test:jobs:contracts"]); await run("jobs restart tests", bun, ["run", "test:jobs:restart"]); await run("jobs client tests", bun, ["run", "test:jobs:client"]); await run("jobs validation/binding/watch quality gate", bun, ["run", "test:jobs:quality"]); await run("jobs Inspector tests", bun, ["run", "test:jobs:inspector"]); await run("jobs compatibility and capability matrix", bun, ["run", "test:jobs:matrix"]); await run("release readiness (account-free Docker variants)", bun, ["run", "release:check"], { ...process.env, RELKIT_TEST_DOCKER: "1", }); await run("recursive synthetic-secret artifact scan", bun, ["run", "scripts/secret-scan.ts"]); await run("whitespace check", "git", [ "diff", "--check", "--", ".", ":(exclude)**/__snapshots__/**", ]); await run("security and redaction tests", bun, ["run", "test:security"]); await run("public declaration leak scan", bun, ["run", "scripts/check-public-declarations.ts"]); await run("agent declaration/source/graph scans", bun, [ "test", "tests/agents/source-boundaries.test.ts", "tests/compiler/commerce-example.test.ts", ]); console.log("\nVerification passed in the fixed fail-fast order."); } if (import.meta.main) { try { await main(); } catch (error) { console.error(error instanceof Error ? error.message : error); process.exitCode = 1; } }