feat(ms-ai-architect): R20 — Category-backfill på 25 kategorisløse ref-filer via insertMetaField [skip-docs]

Ny testet primitiv transform.insertMetaField (anker + 500B-back-off som insertHeaderFields, idempotent på eksakt label, body byte-identisk; 6 tester) + backfill-category.mjs (deterministisk folder->category-regel, hard per-fil-invariant, insert-only, aborterer for skriving ved avvik).

Fordeling: 14 Solution Architecture & Advisory (architecture/), 6 Microsoft AI Platforms (platforms/+development/), 4 Responsible AI & Governance, 1 MLOps & GenAIOps. Label = engelsk Category (322 vs 42 Kategori). Eksisterende recommended-mcp-servers/rag-maturity-model urort.

Roadmap: R20 done + R21 (Status/Last-updated not-due) encoded. Verifisering: 0 kategorislose filer (var 25); diff +25/-0; idempotent re-run; suite 764/764 exit 0.
This commit is contained in:
Kjell Tore Guttormsen 2026-07-06 07:39:54 +02:00
commit a583599ab1
29 changed files with 262 additions and 1 deletions

View file

@ -359,6 +359,65 @@ export function insertHeaderFields(content, meta) {
return lines.join('\n');
}
/**
* Surgically insert ONE bold-label meta line `**<label>:** <value>` into an EXISTING KB
* file that lacks it: the R20/R21 base-field backfill primitive (**Category:** in R20;
* **Status:** / **Last updated:** in R21). Reuses insertHeaderFields' anchor + 500-byte
* back-off discipline the line lands immediately after the last line of the first
* contiguous bold-label meta run that still leaves room in the window, with a title fallback
* for the "None" dialect (no meta lines). Idempotent on the EXACT label: a file already
* carrying `**<label>:**` anywhere in the header region is returned unchanged. The body (from
* the first `## ` section) is byte-identical.
*
* Unlike insertHeaderFields (Port-1 Type/Source, with type validation) this is field-agnostic
* it keys on the label shape only, so it never re-derives a value or touches other lines.
*
* @param {string} content full existing file content
* @param {string} label the bold label WITHOUT the `**`/`:` decoration, e.g. 'Category'
* @param {string} value the field value
* @returns {string} content with the meta line inserted, or unchanged if the label is present
* @throws if label or value is blank
*/
export function insertMetaField(content, label, value) {
const s = String(content ?? '');
const lab = String(label ?? '').trim();
const val = String(value ?? '').trim();
if (lab === '') throw new Error('insertMetaField: label is required');
if (val === '') throw new Error('insertMetaField: value is required');
const lines = s.split('\n');
const esc = lab.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const labelRe = new RegExp('^\\s*\\*\\*' + esc + ':\\*\\*', 'i');
// Idempotent: if the exact label already appears in the header region, return unchanged.
for (const line of lines) {
if (/^##\s/.test(line) || /^---\s*$/.test(line)) break; // reached body / rule
if (labelRe.test(line)) return s;
}
// Anchor: last line of the first contiguous meta run whose insertion point still fits the
// 500-byte window; title fallback for the "None" dialect (mirrors insertHeaderFields).
let anchorIdx = -1;
let titleIdx = -1;
let cum = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (/^##\s/.test(line) || /^---\s*$/.test(line)) break; // reached body / rule
if (titleIdx === -1 && /^#\s+\S/.test(line)) titleIdx = i;
const nextCum = cum + line.length + 1; // byte offset just AFTER this line
if (RE_META_LINE.test(line)) {
if (nextCum > HEADER_REGION_BYTES) break; // back off — insertion point past the window
anchorIdx = i;
} else if (anchorIdx !== -1) {
break; // first non-meta line after the run — the contiguous run is over
}
cum = nextCum;
}
const at = (anchorIdx !== -1 ? anchorIdx : titleIdx) + 1; // -1 + 1 = 0 → very top if no title
lines.splice(at, 0, `**${lab}:** ${val}`);
return lines.join('\n');
}
/**
* Surgically insert-or-update ONLY the `**Verified:**` and `**Verified by:**` header lines
* on an EXISTING KB file the R7R10 born-verified STAMP primitive. Unlike composeKbFile