feat(ms-ai-architect): surgical insertVerifiedFields stamp (byte-identisk body, NFR non-destruktivt, TDD) [skip-docs]

This commit is contained in:
Kjell Tore Guttormsen 2026-07-04 22:58:10 +02:00
commit b5c44e6c6e
2 changed files with 161 additions and 0 deletions

View file

@ -25,6 +25,7 @@ import {
composeKbFile,
insertToc,
insertHeaderFields,
insertVerifiedFields,
normalizeStaleVerified,
stampVerifiedMeta,
} from '../../scripts/kb-update/lib/transform.mjs';
@ -704,6 +705,80 @@ test('insertToc(insertHeaderFields(large)) → Type+Source+TOC, passes real chec
assert.equal(tail(out), tail(HDR_LARGE));
});
// --- insertVerifiedFields: the R7R10 surgical stamp primitive ---------------------
// Insert-or-update ONLY the **Verified:** / **Verified by:** header lines in place, body
// byte-identical, NEVER a TOC (contrast composeKbFile, which rebuilds the header + may add
// ## Innhold → body diff). THROWS when the stamp would land past the 500-byte window so a
// judge-cleared file is never given a silent, unparseable stamp.
// A migrated reference carrying Type+Source but NOT yet Verified — the R7 stamp target.
const HDR_UNVERIFIED =
'# Azure AI Foundry\n\n' +
'**Last updated:** 2026-06\n' +
'**Status:** GA\n' +
'**Category:** AI Services\n' +
'**Type:** reference\n' +
'**Source:** https://learn.microsoft.com/azure/ai-foundry/x\n\n' +
'---\n\n' +
'## Introduksjon\n\nBrødtekst.\n';
// Same, but large (>100 lines) — proves stamping never triggers a TOC insertion.
const HDR_LARGE_UNVERIFIED =
'# Big Ref\n\n' +
'**Last updated:** 2026-06\n' +
'**Status:** GA\n' +
'**Category:** X\n' +
'**Type:** reference\n' +
'**Source:** ' + MS_URL + '\n\n' +
'---\n\n' +
LARGE_BODY;
// A header whose title line alone exceeds the 500-byte window: any stamp inserted after it
// lands past the window → invisible to parseVerified*Header → insertVerifiedFields must THROW.
const HDR_NO_ROOM =
'# ' + 'A'.repeat(520) + '\n\n' +
'**Type:** reference\n**Source:** ' + MS_URL + '\n\n---\n\n## Introduksjon\n\ntekst.\n';
test('insertVerifiedFields stamps Verified + Verified by, body byte-identical, no TOC', () => {
const out = insertVerifiedFields(HDR_UNVERIFIED, { verified: '2026-07-04', verified_by: 'judge-v3.1' });
assert.equal(parseVerifiedHeader(out), '2026-07-04');
assert.equal(parseVerifiedByHeader(out), 'judge-v3.1');
assert.doesNotMatch(out, /## Innhold/); // never a TOC (contrast composeKbFile)
const tail = (s) => s.slice(s.indexOf('## Introduksjon'));
assert.equal(tail(out), tail(HDR_UNVERIFIED)); // body byte-identical
});
test('insertVerifiedFields updates an existing stamp in place (idempotent + re-stampable)', () => {
const once = insertVerifiedFields(HDR_UNVERIFIED, { verified: '2026-07-04', verified_by: 'judge-v3.1' });
const twice = insertVerifiedFields(once, { verified: '2026-07-04', verified_by: 'judge-v3.1' });
assert.equal(twice, once, 'same values → byte-identical (idempotent)');
const updated = insertVerifiedFields(once, { verified: '2026-08-01', verified_by: 'human' });
assert.equal(parseVerifiedHeader(updated), '2026-08-01');
assert.equal(parseVerifiedByHeader(updated), 'human');
assert.equal((updated.match(/\*\*Verified:\*\*/g) || []).length, 1, 'not duplicated');
assert.equal((updated.match(/\*\*Verified by:\*\*/g) || []).length, 1, 'not duplicated');
});
test('insertVerifiedFields never inserts a TOC into a large file (contrast composeKbFile)', () => {
const out = insertVerifiedFields(HDR_LARGE_UNVERIFIED, { verified: '2026-07-04', verified_by: 'judge-v3.1' });
assert.equal(parseVerifiedHeader(out), '2026-07-04');
assert.doesNotMatch(out, /## Innhold/);
const tail = (s) => s.slice(s.indexOf('## Introduksjon'));
assert.equal(tail(out), tail(HDR_LARGE_UNVERIFIED));
});
test('insertVerifiedFields throws when the stamp would land past the 500-byte window', () => {
assert.throws(
() => insertVerifiedFields(HDR_NO_ROOM, { verified: '2026-07-04', verified_by: 'judge-v3.1' }),
/500|window|vindu/i,
);
});
test('insertVerifiedFields requires both verified and verified_by', () => {
assert.throws(() => insertVerifiedFields(HDR_UNVERIFIED, { verified: '2026-07-04' }), /verified_by|required/i);
assert.throws(() => insertVerifiedFields(HDR_UNVERIFIED, { verified_by: 'judge-v3.1' }), /verified|required/i);
});
// --- normalizeStaleVerified: surgical carve-out for the 14 `**Verified:** MCP` files ---
// Acts on EVERY stale (non-date) **Verified:** that falls inside the 500-byte scan window —
// exactly what parseVerifiedHeader reads as the file's verified value, whether it sits in