import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { buildTaskContext, root, verifyTaskIndex } from "./task-context-lib.mjs"; import { validateCatalogRecords, validateContextBundle } from "./context-governance.mjs"; const requested = process.argv.indexOf("--task"); const task = requested >= 0 ? process.argv[requested + 1] : undefined; const bundle = buildTaskContext(task); const current = task === undefined; const { report, violations } = validateContextBundle(bundle, { current }); const index = verifyTaskIndex(); if (index) { const catalogPath = path.join(root, index.catalog.path); violations.push(...validateCatalogRecords(catalogPath, index)); const catalogHandle = fs.openSync(catalogPath, "r"); let active; try { active = Object.entries(index.entries).filter(([id]) => { const metadata = index.entries[id]; const buffer = Buffer.alloc(metadata.length); fs.readSync(catalogHandle, buffer, 0, metadata.length, metadata.offset); return JSON.parse(buffer.toString("utf8")).state === "active"; }); } finally { fs.closeSync(catalogHandle); } if (active.length !== 1) violations.push({ code: "INDEX_ACTIVE_TASK_COUNT", detail: `count=${active.length}` }); if (current && active[0]?.[0] !== bundle.context.task) violations.push({ code: "INDEX_ACTIVE_TASK_MISMATCH", detail: `${active[0]?.[0] ?? "NONE"}!=${bundle.context.task}` }); } assert.equal(violations.length, 0, `context governance violations: ${JSON.stringify(violations)}`); process.stdout.write(`context-governance-ok task=${bundle.context.task} totalTokens=${report.totalTokens} inputs=${bundle.context.inputPaths.length} commands=${bundle.context.commands.length}\n`);