ms-ai-architect/scripts/kb-update/dedup-plain-header.mjs
Kjell Tore Guttormsen de0d94cbc1 feat(ms-ai-architect): R22 decision-b Enhet 3 — Status-backfill 25 none + ai-act dual-header-dedup 4 (Missing Status/Last-updated 29+4→0) [skip-docs]
To operasjoner, én økt (⊥ R7), begge ren metadata-normalisering (verdi aldri fabrikkert).

Premiss-korreksjon (ground truth 2026-07-07): roadmap sa «27 none + 4 ai-act».
Målt: 29 mangler bold **Status:** = 25 rene none + 4 ai-act (plain Status: GA).
De «27» inkluderte 2 for mye — 2 filer (custom-dashboards-ai-operations,
zero-trust-ai-services) har bold **Status:** KUN forbi byte 500 (present for
full-fil-audit, usynlig for 500B header-parser) → egen header-slanking-residual
(§8-register), utenfor Enhet 3.

Op A — Status-backfill 25 rene none: utvidet backfill-status.mjs MANIFEST 14→39
(samme statusForFile + insertMetaField + hard per-fil-invariant, idempotent skip
på de 14 R21-gjorte). Alle 25 → **Status:** Established Practice (ingen matcher
template|matrix|benchmarks|register). Diff +25/-0.

Op B — ai-act dual-header-dedup (4 filer): ny driver dedup-plain-header.mjs + 2
rene primitiver i transform.mjs — boldifyPlainField (plain→bold, verdi bevart
byte-eksakt, header-scoped, idempotent) + dropRedundantPlainField (sletter plain
KUN når bold m/ identisk verdi beviser redundans; kaster ved avvik/manglende bold).
Per fil: plain Last updated: + Status: GA → bold (2026-06-18/2026-02, GA bevart),
redundant plain Category: fjernet. Hard per-fil-invariant (net -1 linje, begge
felt bold m/ bevart verdi, ingen plain-header igjen, body byte-identisk). Diff -12/+8.

Verifisering: test-backfill-status 8/8 + test-dedup-plain-header 13/13; audit
Missing Status 29→0, Missing English Last updated 4→0; skills-diff 29 filer
+33/-12 (kun **Status:** + 8 bold-swaps), diff-kontekst inspisert per fil; begge
drivere idempotent (re-run 0 writes); suite 806/806 exit 0; none=8 uendret (Enhet 4).
2026-07-07 07:45:27 +02:00

139 lines
6.3 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.
//
// Usage: node scripts/kb-update/dedup-plain-header.mjs [--dry]
import { readFileSync, writeFileSync, existsSync, realpathSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { boldifyPlainField, dropRedundantPlainField } from './lib/transform.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) writeFileSync(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') });