#!/usr/bin/env node /** * CI: cross-check versioned contracts appear consistently across source + key docs. * Complements verify-compat-matrix.mjs (matrix table) with syscall example + JSON Schema. */ import fs from 'node:fs' import path from 'node:path' import process from 'node:process' import { fileURLToPath } from 'node:url' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') function read(rel) { return fs.readFileSync(path.join(root, rel), 'utf8') } function must(cond, msg) { if (!cond) { console.error('verify-doc-contracts:', msg) process.exit(1) } } function main() { const ctxSrc = read('packages/bare-os-booter/lib/ctx/bare-os-ctx-api.js') const ctxM = ctxSrc.match(/export const BARE_OS_CTX_API_VERSION = '([^']+)'/) must(ctxM, 'parse BARE_OS_CTX_API_VERSION failed') const ctxVer = ctxM[1] const compat = read('docs/reference/compatibility-matrix.md') must( compat.includes('`' + ctxVer + '`'), 'compatibility-matrix.md must list bareOsCtxApiVersion ' + ctxVer ) const posixSrc = read('packages/bare-os-protocol/lib/bare-os-posix-profile.js') const posixM = posixSrc.match( /export const BARE_OS_POSIX_PROFILE_VERSION = '([^']+)'/ ) must(posixM, 'parse BARE_OS_POSIX_PROFILE_VERSION failed') const posixVer = posixM[1] must( compat.includes('`' + posixVer + '`'), 'compatibility-matrix.md must list BARE_OS_POSIX_PROFILE_VERSION ' + posixVer ) const declared = read('docs/architecture/POSIX_DECLARED_PROFILE.md') must( declared.includes('`' + posixVer + '`'), 'POSIX_DECLARED_PROFILE.md must list profile version ' + posixVer ) const procJsonSrc = read( 'packages/bare-os-booter/lib/posix/bare-os-syscalls-proc-json.js' ) const schM = procJsonSrc.match(/schemaVersion:\s*(\d+)/) must(schM, 'parse syscalls proc schemaVersion failed') const syscallSchema = schM[1] const exRaw = read('kernel/etc/bare-os/syscalls.example.json') const exM = exRaw.match(/"schemaVersion":\s*(\d+)/) must(exM, 'parse syscalls.example.json schemaVersion failed') must( exM[1] === syscallSchema, `syscalls.example.json schemaVersion ${exM[1]} !== proc builder ${syscallSchema}` ) const schemaPath = 'docs/schemas/bare-os-syscalls.schema.json' const schemaRaw = read(schemaPath) const jsonSchemaM = schemaRaw.match( /"schemaVersion"\s*:\s*\{\s*"type"\s*:\s*"integer"\s*,\s*"const"\s*:\s*(\d+)/ ) must(jsonSchemaM, 'parse bare-os-syscalls.schema.json schemaVersion const failed') must( jsonSchemaM[1] === syscallSchema, `${schemaPath} const ${jsonSchemaM[1]} !== proc builder ${syscallSchema}` ) must( compat.includes('Schema **' + syscallSchema + '**'), 'compatibility-matrix.md must document syscalls.json schema ' + syscallSchema ) const genPath = 'docs/reference/ctx-client-helper.generated.ts' if (fs.existsSync(path.join(root, genPath))) { const genRaw = read(genPath) const genM = genRaw.match( /BARE_OS_CTX_API_CLIENT_VERSION = '([^']+)'/ ) must(genM, 'parse BARE_OS_CTX_API_CLIENT_VERSION from ' + genPath + ' failed') must( genM[1] === ctxVer, `${genPath} BARE_OS_CTX_API_CLIENT_VERSION ${genM[1]} !== bare-os-ctx-api.js ${ctxVer} (run node scripts/gen-ctx-client-helper.mjs)` ) } const posixMatrixRaw = read('docs/reference/posix-compliance-matrix.json') const posixMatrix = JSON.parse(posixMatrixRaw) const procTableSchema = Number(posixMatrix?.synthetic_proc?.process_table_schema) const syscallsSchemaFromMatrix = Number( posixMatrix?.synthetic_proc?.syscalls_json_schema ) must( Number.isFinite(procTableSchema) && procTableSchema > 0, 'posix-compliance-matrix.json missing synthetic_proc.process_table_schema' ) must( Number.isFinite(syscallsSchemaFromMatrix) && syscallsSchemaFromMatrix > 0, 'posix-compliance-matrix.json missing synthetic_proc.syscalls_json_schema' ) must( String(syscallsSchemaFromMatrix) === syscallSchema, `posix-compliance-matrix syscalls_json_schema ${syscallsSchemaFromMatrix} !== proc builder ${syscallSchema}` ) const handbookPosix = read('handbook/09-posix-utilities-shell-and-vfs.md') must( handbookPosix.includes( '`/proc/bare_os/syscalls.json`** (schema **' + syscallsSchemaFromMatrix + '**' ), `handbook/09-posix-utilities-shell-and-vfs.md must mention /proc/bare_os/syscalls.json schema ${syscallsSchemaFromMatrix}` ) must( handbookPosix.includes( '`/proc/bare_os/process_table.json`** (**schema ' + procTableSchema + '**' ), `handbook/09-posix-utilities-shell-and-vfs.md must mention /proc/bare_os/process_table.json schema ${procTableSchema}` ) console.log('verify-doc-contracts: OK') } main()