#!/usr/bin/env node // Backfill **Last updated:** on the ONE advisor reference file that carries no date label at // all — decision-trees.md (Enhet 4, ⊥ R7). Ground truth (2026-07-16): it is the only // skills/**/*.md whose header lacks an English **Last updated:**. Sister driver to // backfill-status.mjs / backfill-category.mjs — same manifest-driven shape over the tested // insertMetaField primitive. // // Value derivation — git "content-commit" heuristic (NEVER "today"): // The date is the authored date of the last commit that changed decision-trees.md's BODY, // EXCLUDING the R20/R21 metadata passes (which inserted only a **Category:** / **Status:** // header line — no body change). Measured 2026-07-16 against `git log -- `: // 5a0e8d7 2026-07-06 R21 Status-backfill — header-only insert → metadata → EXCLUDED // a583599 2026-07-06 R20 Category-backfill — header-only insert → metadata → EXCLUDED // 03d596e 2026-06-23 KB-refresh Foundry namesweep — 3 BODY table rows changed → CONTENT ✓ // baa2d02 2026-04-08 file birth (plugin add) // ⇒ last content commit = 03d596e (2026-06-23). Full YYYY-MM-DD dialect (dominant among the // architecture/ siblings). The date is FROZEN below with this provenance; re-measure against // git before re-running on a mutated history. "today" (MEASURED_TODAY) is asserted-against so // a naive last-commit / metadata-pass date can never leak in. // // This applier writes ONLY the derived bold **Last updated:** line — reuses insertMetaField, // with a hard per-file invariant asserted BEFORE any write (exactly one line inserted, it is the // Last updated line with the frozen date byte-exact, body byte-identical). Idempotent: a file // already carrying **Last updated:** in its header window is skipped. atomicWriteSync // (crash-safe: tmp+rename — a reader sees the old file or the new one, never a partial; an // interrupted run recovers by re-running). Aborts and writes nothing on any drift or breach. // // Usage: node scripts/kb-update/backfill-last-updated.mjs [--dry] import { readFileSync, existsSync, realpathSync } from 'node:fs'; import { join, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { insertMetaField } from './lib/transform.mjs'; import { atomicWriteSync } from './lib/atomic-write.mjs'; const __dirname = dirname(fileURLToPath(import.meta.url)); const PLUGIN_ROOT = join(__dirname, '..', '..'); // The session date the manifest was measured on. A manifest date equal to this means the // heuristic fell back to a metadata-pass / naive last-commit date — the never-today guard rejects it. export const MEASURED_TODAY = '2026-07-16'; // Frozen manifest — the single dateless advisor file + its git-content-commit date (03d596e). // Value is a YYYY-MM-DD string; provenance in the header comment above. export const MANIFEST = [ { path: 'skills/ms-ai-advisor/references/architecture/decision-trees.md', lastUpdated: '2026-06-23' }, ]; const RE_ISO_DATE = /^\d{4}-\d{2}-\d{2}$/; // Presence of a dated **Last updated:** in the header window — mirrors audit-corpus-headers.mjs // RE_LAST_UPDATED (the guard the header audit reads), used here for idempotency. const RE_LAST_UPDATED = /\*\*Last updated:\*\*\s*[\d-]+/i; function run({ dry }) { const planned = []; const skipped = []; const missing = []; for (const { path: rel, lastUpdated } of MANIFEST) { if (!RE_ISO_DATE.test(lastUpdated)) throw new Error(`ABORT ${rel}: date '${lastUpdated}' not YYYY-MM-DD`); if (lastUpdated === MEASURED_TODAY) throw new Error(`ABORT ${rel}: date equals measured 'today' — never-today guard`); const abs = join(PLUGIN_ROOT, rel); if (!existsSync(abs)) { missing.push(rel); continue; } const old = readFileSync(abs, 'utf8'); if (RE_LAST_UPDATED.test(old.slice(0, 500))) { skipped.push(rel); continue; } // idempotent const out = insertMetaField(old, 'Last updated', lastUpdated); // Hard invariant: exactly one line inserted (the Last updated 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] !== `**Last updated:** ${lastUpdated}`) 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, lastUpdated, 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 insert: ${planned.length} | already present (skipped): ${skipped.length}`); if (dry) { console.log('\n(dry run — no writes)'); for (const p of planned) console.log(` + ${p.rel} → **Last updated:** ${p.lastUpdated}`); 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') });