| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * check-facts.mjs — CI drift gate for website facts. |
| 4 | * |
| 5 | * Re-derives mechanical facts from the current workspace (using the same |
| 6 | * logic as derive-facts.mjs / facts-lib.mjs) and compares them against the |
| 7 | * committed web/lib/facts.generated.ts. Exits non-zero when the committed |
| 8 | * file is stale so the mismatch is caught before deploy. |
| 9 | * |
| 10 | * Usage: |
| 11 | * cd web && npm run check:facts |
| 12 | * |
| 13 | * Checked fields: |
| 14 | * version, providers, crates, sandboxBackends, defaultModel, nodeEngines, |
| 15 | * toolCount, contributorCount, license, latestPublishedRelease. |
| 16 | * |
| 17 | * Fields NOT checked (by design): |
| 18 | * generatedAt — always different |
| 19 | * sourceRevision/sourceCommittedAt — injected from the exact build checkout |
| 20 | */ |
| 21 | import { readFileSync, existsSync } from "node:fs"; |
| 22 | import { resolve, dirname } from "node:path"; |
| 23 | import { fileURLToPath } from "node:url"; |
| 24 | import { buildFacts, deriveFeatures, REPO_ROOT } from "./facts-lib.mjs"; |
| 25 | import { deriveAgentDocs } from "./docs-sources-lib.mjs"; |
| 26 | import { checkReleaseBaseline, deriveReleaseBaseline, readCommittedBaseline, tagResolves } from "./release-baseline-lib.mjs"; |
| 27 | |
| 28 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 29 | const GENERATED_PATH = resolve(__dirname, "..", "lib", "facts.generated.ts"); |
| 30 | |
| 31 | // --- Helpers --------------------------------------------------------- |
| 32 | |
| 33 | /** |
| 34 | * Parse the committed `facts.generated.ts` into a plain object. |
| 35 | * We don't `import()` the TS file (which would need ts-node); instead we |
| 36 | * extract the JSON object literal from the export declaration. |
| 37 | */ |
| 38 | function parseCommittedFacts() { |
| 39 | if (!existsSync(GENERATED_PATH)) { |
| 40 | return { error: `not found: ${GENERATED_PATH}` }; |
| 41 | } |
| 42 | const src = readFileSync(GENERATED_PATH, "utf-8"); |
| 43 | |
| 44 | // Extract the object literal between "export const FACTS: RepoFacts = " and |
| 45 | // the closing ";" (possibly preceded by "as const"). |
| 46 | const m = src.match(/export const FACTS\s*:\s*\w+\s*=\s*([\s\S]*?);?\s*$/); |
| 47 | if (!m) { |
| 48 | return { error: `could not parse FACTS export from ${GENERATED_PATH}` }; |
| 49 | } |
| 50 | try { |
| 51 | const obj = JSON.parse(m[1]); |
| 52 | return { facts: obj }; |
| 53 | } catch (e) { |
| 54 | return { error: `invalid JSON in ${GENERATED_PATH}: ${e.message}` }; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Compare two facts objects and return a list of field-level diffs. |
| 60 | */ |
| 61 | function diffFacts(committed, fresh) { |
| 62 | // Fields checked for drift. Skip generatedAt and exact-build provenance. |
| 63 | const checkFields = [ |
| 64 | "version", |
| 65 | "crates", |
| 66 | "sandboxBackends", |
| 67 | "providers", |
| 68 | "models", |
| 69 | "defaultModel", |
| 70 | "nodeEngines", |
| 71 | "toolCount", |
| 72 | "contributorCount", |
| 73 | "license", |
| 74 | "latestPublishedRelease", |
| 75 | ]; |
| 76 | |
| 77 | const diffs = []; |
| 78 | for (const field of checkFields) { |
| 79 | const a = JSON.stringify(committed[field] ?? null); |
| 80 | const b = JSON.stringify(fresh[field] ?? null); |
| 81 | if (a !== b) { |
| 82 | diffs.push({ field, committed: committed[field], fresh: fresh[field] }); |
| 83 | } |
| 84 | } |
| 85 | return diffs; |
| 86 | } |
| 87 | |
| 88 | // --- Main ------------------------------------------------------------- |
| 89 | |
| 90 | const committed = parseCommittedFacts(); |
| 91 | if (committed.error) { |
| 92 | console.error(`[check-facts] ERROR: ${committed.error}`); |
| 93 | process.exit(1); |
| 94 | } |
| 95 | |
| 96 | // The shared parser refuses incomplete or malformed provider projections. |
| 97 | const fresh = buildFacts(); |
| 98 | try { |
| 99 | deriveAgentDocs(resolve(__dirname, ".."), REPO_ROOT); |
| 100 | } catch (error) { |
| 101 | console.error(`[check-facts] FAIL — agent docs cannot be derived from lib/docs-map.ts: ${error.message}`); |
| 102 | process.exit(1); |
| 103 | } |
| 104 | const featuresPath = resolve(__dirname, "..", "data", "features.generated.json"); |
| 105 | if (!existsSync(featuresPath) || JSON.stringify(JSON.parse(readFileSync(featuresPath, "utf8"))) !== JSON.stringify(deriveFeatures())) { |
| 106 | console.error("[check-facts] FAIL — generated feature registry is stale; run npm run prebuild"); |
| 107 | process.exit(1); |
| 108 | } |
| 109 | |
| 110 | const allowShallow = process.argv.includes("--allow-shallow") || process.env.CODEWHALE_ALLOW_SHALLOW_CHECKOUT === "1"; |
| 111 | const committedBaseline = readCommittedBaseline(); |
| 112 | const tagAvailable = Boolean(committedBaseline && tagResolves(committedBaseline.tag)); |
| 113 | const derivedBaseline = |
| 114 | tagAvailable && fresh.latestPublishedRelease |
| 115 | ? deriveReleaseBaseline(fresh.latestPublishedRelease) |
| 116 | : null; |
| 117 | if (committedBaseline && fresh.latestPublishedRelease && !tagAvailable && allowShallow) { |
| 118 | console.warn(`[check-facts] warning: tag ${committedBaseline.tag} is not in this checkout, so the release baseline was not recomputed (--allow-shallow)`); |
| 119 | } |
| 120 | const baselineProblems = checkReleaseBaseline( |
| 121 | committedBaseline, |
| 122 | fresh.latestPublishedRelease, |
| 123 | derivedBaseline, |
| 124 | { allowShallow: allowShallow && !tagAvailable }, |
| 125 | ); |
| 126 | if (baselineProblems.length > 0) { |
| 127 | for (const problem of baselineProblems) console.error(`[check-facts] FAIL — ${problem}`); |
| 128 | process.exit(1); |
| 129 | } |
| 130 | |
| 131 | // Quick sanity: critical source facts must never degrade to matching nulls. |
| 132 | const criticalGaps = []; |
| 133 | if (!fresh.version) criticalGaps.push("version"); |
| 134 | if (fresh.providers.length === 0) criticalGaps.push("providers"); |
| 135 | if (!fresh.latestPublishedRelease) criticalGaps.push("latestPublishedRelease"); |
| 136 | if (criticalGaps.length > 0) { |
| 137 | console.error( |
| 138 | `[check-facts] FAIL — fresh derivation returned empty/missing: ${criticalGaps.join(", ")}`, |
| 139 | ); |
| 140 | process.exit(1); |
| 141 | } |
| 142 | |
| 143 | const diffs = diffFacts(committed.facts, fresh); |
| 144 | |
| 145 | if (diffs.length === 0) { |
| 146 | console.log("[check-facts] OK — committed facts.generated.ts matches workspace"); |
| 147 | process.exit(0); |
| 148 | } |
| 149 | |
| 150 | console.error("[check-facts] FAIL — committed facts.generated.ts is stale"); |
| 151 | for (const d of diffs) { |
| 152 | console.error(` ${d.field}:`); |
| 153 | console.error(` committed: ${JSON.stringify(d.committed)}`); |
| 154 | console.error(` fresh: ${JSON.stringify(d.fresh)}`); |
| 155 | } |
| 156 | |
| 157 | console.error( |
| 158 | "\nRun `cd web && npm run prebuild` to regenerate facts.generated.ts, then commit the result.", |
| 159 | ); |
| 160 | process.exit(1); |
| 161 |