#!/usr/bin/env node // Backfill **Category:** on the reference files that carry NO category label (R20). // // Deterministic folder→category rule (operator-approved taxonomy 2026-07-06): insert-only // via the tested `insertMetaField` primitive, with a hard per-file invariant asserted BEFORE // any write — exactly one line is inserted, it is the Category line, and the body is // byte-identical. If any target falls outside the approved folder rule, or any file would // change by more than that single line, the run aborts and writes nothing. Idempotent: // files that already carry **Category:**/**Kategori:** are skipped, so a re-run is a no-op. // // Usage: node scripts/kb-update/backfill-category.mjs [--dry] import { readFileSync, writeFileSync } from 'node:fs'; import { execSync } from 'node:child_process'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { insertMetaField } from './lib/transform.mjs'; const __dirname = dirname(fileURLToPath(import.meta.url)); const PLUGIN_ROOT = join(__dirname, '..', '..'); const dry = process.argv.includes('--dry'); // Immediate-parent-directory basename → Category. Operator-approved 2026-07-06. const FOLDER_CATEGORY = { architecture: 'Solution Architecture & Advisory', platforms: 'Microsoft AI Platforms', development: 'Microsoft AI Platforms', 'responsible-ai': 'Responsible AI & Governance', 'mlops-genaiops': 'MLOps & GenAIOps', }; const RE_CAT = /^\s*\*\*(Category|Kategori):\*\*/im; const files = execSync("find skills/*/references -name '*.md'", { cwd: PLUGIN_ROOT, encoding: 'utf8' }) .trim() .split('\n'); const targets = files.filter((rel) => !RE_CAT.test(readFileSync(join(PLUGIN_ROOT, rel), 'utf8'))); const planned = []; const unmatched = []; for (const rel of targets) { const folder = rel.split('/').slice(-2, -1)[0]; const cat = FOLDER_CATEGORY[folder]; if (!cat) { unmatched.push(`${rel} (folder=${folder})`); continue; } const old = readFileSync(join(PLUGIN_ROOT, rel), 'utf8'); const out = insertMetaField(old, 'Category', cat); // Hard invariant: exactly one line inserted (the Category line), body byte-identical. const o = old.split('\n'); const n = out.split('\n'); if (n.length !== o.length + 1) throw new Error(`ABORT ${rel}: not exactly one line inserted`); let d = 0; while (d < o.length && o[d] === n[d]) d++; if (n[d] !== `**Category:** ${cat}`) { throw new Error(`ABORT ${rel}: inserted line mismatch → ${JSON.stringify(n[d])}`); } if (o.slice(d).join('\n') !== n.slice(d + 1).join('\n')) { throw new Error(`ABORT ${rel}: body not byte-identical below the insertion`); } planned.push({ rel, cat, out }); } if (unmatched.length) { console.error(`ABORT — ${unmatched.length} target(s) outside the approved folder rule:`); unmatched.forEach((u) => console.error(' ' + u)); process.exit(1); } const byCat = {}; for (const p of planned) byCat[p.cat] = (byCat[p.cat] || 0) + 1; console.log(`Reference files: ${files.length} | without a category: ${targets.length} | planned: ${planned.length}`); console.log('By category:'); for (const [c, n] of Object.entries(byCat).sort()) console.log(` ${n} ${c}`); if (dry) { console.log('\n(dry run — no writes)'); process.exit(0); } for (const p of planned) writeFileSync(join(PLUGIN_ROOT, p.rel), p.out); console.log(`\nWrote ${planned.length} files.`);