- backup.mjs restore(relPaths): SCOPED per-fil-rollback erstatter hel-tre rmSync(srcDir)+cpSync. Ruller kun tilbake kjøringens egne skriv; parallelle økters skriv til søsken-filer overlever; intet destruktivt rm→cp-vindu. Nyskapte filer slettes; prior bytes gjenopprettes atomisk. - migrate-corpus.mjs: returnert restore() er nå en null-arg closure bundet til kjøringens skrive- liste (public API uendret) → scoped. detectStaleRollback aborterer ved forrige krasj-sentinel; cleanupOldBackups pruner backups forbi retention etter vellykket batch. - 5 drivere (backfill-status/-category, dedup-plain-header, relabel-dato/-dialect): writeFileSync → atomicWriteSync (crash-safe tmp+rename) + recovery-kontrakt i header. - backfill-category.mjs: refaktorert importerbar (isMain-guard + run()/planCategoryBackfill/ categoryForFile/FOLDER_CATEGORY-eksporter, parameterisert root) → testbar uten scan-side-effekt. - Tester (+16): 6 scoped-restore (parallel-preservering, ny-fil-sletting, throws-guard) erstatter 2 hel-tre; test-backfill-category (frosset taxonomi + hermetisk plan); test-driver-atomic-writes (5 drivere); migrate stale-abort + cleanup-wiring. Suite 859→875 exit 0. validate-plugin 250/0.
143 lines
6.6 KiB
JavaScript
143 lines
6.6 KiB
JavaScript
#!/usr/bin/env node
|
|
// dedup-plain-header.mjs — Enhet 3 Op B (2026-07-07, ⊥ R7). Normalize the dual-header block on
|
|
// the 4 ms-ai-governance/responsible-ai/ai-act-* files.
|
|
//
|
|
// Those files carry, below the bold header, an authored PLAIN-TEXT block:
|
|
// Last updated: <date>
|
|
// Status: GA
|
|
// Category: <value> ← redundant with a bold **Category:** <same value> (R20)
|
|
// This driver, per file:
|
|
// 1. boldifyPlainField('Last updated') — plain → **Last updated:**, date PRESERVED byte-exact
|
|
// 2. boldifyPlainField('Status') — plain → **Status:** GA, GA PRESERVED (authored, not
|
|
// the derived {Reference|Established Practice} vocab)
|
|
// 3. dropRedundantPlainField('Category')— delete the plain Category iff a bold **Category:**
|
|
// with the IDENTICAL value proves it redundant
|
|
// It NEVER derives a value and NEVER deletes a field it cannot prove redundant (the primitive
|
|
// throws on a value mismatch or a missing bold field). A hard per-file invariant is asserted
|
|
// BEFORE any write: exactly one line removed net, both target fields now bold with the authored
|
|
// value, no plain header field remains, and the body (from the first `## `/`---`) is byte-
|
|
// identical. Idempotent: a re-run finds the plain fields gone and is a no-op. Aborts, writing
|
|
// nothing, on any drift or invariant breach.
|
|
//
|
|
// Closes the last 4 of the corpus's Missing-Status and all 4 Missing-English-Last-updated.
|
|
//
|
|
// Recovery contract: writes are crash-safe (atomicWriteSync tmp+rename — a reader sees the old
|
|
// file or the new one, never a partial); an interrupted run is recovered by re-running (idempotent).
|
|
//
|
|
// Usage: node scripts/kb-update/dedup-plain-header.mjs [--dry]
|
|
import { readFileSync, existsSync, realpathSync } from 'node:fs';
|
|
import { join, dirname } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { boldifyPlainField, dropRedundantPlainField } from './lib/transform.mjs';
|
|
import { atomicWriteSync } from './lib/atomic-write.mjs';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const PLUGIN_ROOT = join(__dirname, '..', '..');
|
|
|
|
// Frozen manifest — the 4 dual-header ai-act-* files (ground truth 2026-07-07: the only
|
|
// non-advisor files carrying a plain-text Status/Last updated block).
|
|
export const MANIFEST = [
|
|
'skills/ms-ai-governance/references/responsible-ai/ai-act-classification-methodology.md',
|
|
'skills/ms-ai-governance/references/responsible-ai/ai-act-deployer-obligations.md',
|
|
'skills/ms-ai-governance/references/responsible-ai/ai-act-fria-template.md',
|
|
'skills/ms-ai-governance/references/responsible-ai/ai-act-provider-obligations.md',
|
|
];
|
|
|
|
/**
|
|
* The pure Op-B composition: boldify the plain Last updated + Status, then drop the redundant
|
|
* plain Category. Order-independent for these files (distinct labels). Pure — no I/O.
|
|
* @param {string} content
|
|
* @returns {string}
|
|
*/
|
|
export function dedupPlainHeader(content) {
|
|
let out = boldifyPlainField(content, 'Last updated');
|
|
out = boldifyPlainField(out, 'Status');
|
|
out = dropRedundantPlainField(out, 'Category');
|
|
return out;
|
|
}
|
|
|
|
// Header region = lines above the first `## `/`---` (mirrors transform.mjs headerEndIndex).
|
|
function splitHeaderBody(content) {
|
|
const lines = content.split('\n');
|
|
let i = 0;
|
|
for (; i < lines.length; i++) {
|
|
if (/^##\s/.test(lines[i]) || /^---\s*$/.test(lines[i])) break;
|
|
}
|
|
return { header: lines.slice(0, i).join('\n'), body: lines.slice(i).join('\n') };
|
|
}
|
|
|
|
const RE_PLAIN_META = /^(Last updated|Status|Category):\s*\S/im;
|
|
|
|
// Assert the hard per-file invariant. Throws (aborting the run) on any breach.
|
|
function assertInvariant(rel, oldC, newC) {
|
|
const o = oldC.split('\n');
|
|
const n = newC.split('\n');
|
|
if (n.length !== o.length - 1) {
|
|
throw new Error(`ABORT ${rel}: expected exactly one line removed net (was ${o.length}, now ${n.length})`);
|
|
}
|
|
const oldH = splitHeaderBody(oldC);
|
|
const newH = splitHeaderBody(newC);
|
|
if (oldH.body !== newH.body) throw new Error(`ABORT ${rel}: body not byte-identical`);
|
|
if (RE_PLAIN_META.test(newH.header)) throw new Error(`ABORT ${rel}: a plain header field remains`);
|
|
if (!/\*\*Last updated:\*\*\s*\S/.test(newH.header)) throw new Error(`ABORT ${rel}: **Last updated:** missing after dedup`);
|
|
if (!/\*\*Status:\*\*\s*\S/.test(newH.header)) throw new Error(`ABORT ${rel}: **Status:** missing after dedup`);
|
|
if ((newH.header.match(/\*\*Category:\*\*/g) || []).length !== 1) {
|
|
throw new Error(`ABORT ${rel}: expected exactly one bold **Category:** after dedup`);
|
|
}
|
|
// Value preservation: the authored plain values must survive verbatim in the bold lines.
|
|
const oldPlain = (label) => {
|
|
const m = new RegExp('^' + label.replace(/ /g, '\\ ') + ':\\s*(\\S.*?)\\s*$', 'im').exec(oldH.header);
|
|
return m ? m[1] : null;
|
|
};
|
|
for (const label of ['Last updated', 'Status']) {
|
|
const want = oldPlain(label);
|
|
const boldRe = new RegExp('\\*\\*' + label + ':\\*\\*\\s*(\\S.*?)\\s*$', 'im');
|
|
const got = boldRe.exec(newH.header);
|
|
if (!want || !got || got[1] !== want) {
|
|
throw new Error(`ABORT ${rel}: value for '${label}' not preserved (want ${JSON.stringify(want)}, got ${JSON.stringify(got && got[1])})`);
|
|
}
|
|
}
|
|
}
|
|
|
|
function run({ dry }) {
|
|
const planned = [];
|
|
const skipped = [];
|
|
const missing = [];
|
|
|
|
for (const rel of MANIFEST) {
|
|
const abs = join(PLUGIN_ROOT, rel);
|
|
if (!existsSync(abs)) { missing.push(rel); continue; }
|
|
const old = readFileSync(abs, 'utf8');
|
|
const out = dedupPlainHeader(old);
|
|
if (out === old) { skipped.push(rel); continue; } // idempotent: already deduped
|
|
assertInvariant(rel, old, out);
|
|
planned.push({ rel, out });
|
|
}
|
|
|
|
if (missing.length) {
|
|
console.error(`ABORT — ${missing.length} manifest target(s) not found (corpus drift):`);
|
|
missing.forEach((m) => console.error(' ' + m));
|
|
process.exit(1);
|
|
}
|
|
if (planned.length + skipped.length !== MANIFEST.length) {
|
|
throw new Error(`ABORT — accounted ${planned.length + skipped.length} ≠ manifest ${MANIFEST.length}`);
|
|
}
|
|
|
|
console.log(`Manifest: ${MANIFEST.length} | to dedup: ${planned.length} | already deduped (skipped): ${skipped.length}`);
|
|
if (dry) {
|
|
console.log('\n(dry run — no writes)');
|
|
for (const p of planned) console.log(` ~ ${p.rel}`);
|
|
return;
|
|
}
|
|
for (const p of planned) atomicWriteSync(join(PLUGIN_ROOT, p.rel), p.out);
|
|
console.log(`\nWrote ${planned.length} files.`);
|
|
}
|
|
|
|
const isMain = (() => {
|
|
try {
|
|
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
} catch {
|
|
return false;
|
|
}
|
|
})();
|
|
if (isMain) run({ dry: process.argv.includes('--dry') });
|