feat(ms-ai-architect): Sesjon 16 — B3 ledger action-lag + merge_skills dry-run-planner

Spor B / B3 start. Operatør-gated dry-run for skill-livssyklus, merge først.

- decisions-io.mjs: additivt skill-nøklet `actions`-lag ved siden av urørt
  URL-nøklet `decisions` (14 URL-tester urørt = regresjonsbevis). version=1,
  loadDecisions backfiller manglende actions-nøkkel. Nye rene fn: actionKey
  (merge symmetrisk på paret), isActionDecided, recordAction (no-mutate),
  listActions. Policy A gjelder også actions.
- scripts/kb-eval/lib/skill-ops.mjs: ren planMergeSkills → {entry,diff,guardrail},
  leser ingen disk, anvender INGENTING. Guardrail = ingen kuratert verdi tapt
  (count-invariant + set-equality + kollisjons-deteksjon; ok=false ved klobring).
  Impur skall loadSkillRefs/runMergePlan + CLI plan-skill-op.mjs (gated --write).
- Ekte eng↔infra dry-run: guardrail OK (34+153=187, 0 kollisjoner), 153 fil-flytt
  + 7 taksonomi-reassign + retire eng; 0 skriving til skills/, ledger uendret
  (retning er operatør/judge-valg, ikke pushet gjennom gaten).
- TDD: test-decisions-actions (10) + test-skill-ops-merge (10, inkl. byte-for-byte
  skills/-snapshot som no-write-bevis). kb-eval 40→50, kb-update 122→132.
  Regresjon grønn: validate 239, kb-integrity 192/192.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-06-20 17:10:33 +02:00
commit f03a0e08b2
5 changed files with 672 additions and 4 deletions

View file

@ -0,0 +1,170 @@
// skill-ops.mjs — Spor B / B3: skill-lifecycle operation PLANNERS (lag-4-analog
// at skill granularity). The pure core APPLIES NOTHING and reads no disk: it
// returns { entry, diff, guardrail }. The operator gate (decisions.json) records
// the PENDING entry; a later approved apply-session performs any skills/ mutation.
//
// Sesjon 16 ships merge_skills only (operator priority). sanitize/retire/create
// follow in S17+ as further planners that emit the same kind of gated entry.
//
// Inherited arkitektur-invariant: detection/verification/transformation never
// write to skills/ directly — only via the ledger after the operator gate.
// Destructive ops carry mandatory guardrails that PROVE curated value is
// preserved (ref-file count-invariant + set-equality) and refuse (ok=false)
// when two reference files would clobber on the same target path.
import { readdirSync, existsSync, statSync } from 'node:fs';
import { join, relative, sep } from 'node:path';
import {
loadDecisions,
saveDecisions,
recordAction,
} from '../../kb-update/lib/decisions-io.mjs';
export const MERGE_OPERATION = 'merge_skills';
// ---------------------------------------------------------------------------
// Pure core — planMergeSkills
// ---------------------------------------------------------------------------
/**
* Plan a skill merge: `absorbed` is retired into `absorber`. Pure; mutates
* nothing; reads no disk. Reference identity is the references/-relative path
* (e.g. "rag-architecture/x.md"): two files sharing that identity across the
* two skills would clobber on merge the guardrail catches that and fails.
*
* @param {string} absorber skill that survives and receives all references
* @param {string} absorbed skill that is retired; its refs move under absorber
* @param {{ skillRefs?: Record<string,string[]>, descriptions?: Record<string,string>,
* categorySkill?: Record<string,string>, decided_at?: string|null,
* note?: string }} [ctx]
* @returns {{ entry: object, diff: object, guardrail: object }}
*/
export function planMergeSkills(absorber, absorbed, ctx = {}) {
if (!absorber || !absorbed || absorber === absorbed) {
throw new Error(`planMergeSkills: cannot merge a skill into itself (${absorber} / ${absorbed})`);
}
const skillRefs = ctx.skillRefs ?? {};
const descriptions = ctx.descriptions ?? {};
const categorySkill = ctx.categorySkill ?? {};
const aRefs = skillRefs[absorber] ?? [];
const bRefs = skillRefs[absorbed] ?? [];
const aSet = new Set(aRefs);
// Collision = a references-relative path present under BOTH skills. On merge
// the absorbed copy would overwrite the absorber copy (or vice versa): a
// silent loss of curated content. The guardrail must surface and reject it.
const collisions = [...new Set(bRefs.filter((r) => aSet.has(r)))].sort();
// Post-merge identity set = union of both ref sets by references-relative path.
const postSet = new Set([...aRefs, ...bRefs]);
const expectedPostCount = aRefs.length + bRefs.length; // no-loss target
const postCount = postSet.size;
const countInvariant = postCount === expectedPostCount; // holds iff no collision
const setEquality = [...aSet, ...new Set(bRefs)].every((r) => postSet.has(r)) && collisions.length === 0;
const guardrail = {
method:
'no curated value lost: postSet = union(absorber refs, absorbed refs) by references-relative path. ' +
'count-invariant = postCount === absorber+absorbed (fails on collision); ' +
'set-equality = every source ref survives AND no two map to the same target.',
preCountAbsorber: aRefs.length,
preCountAbsorbed: bRefs.length,
expectedPostCount,
postCount,
countInvariant,
setEquality,
collisions,
ok: countInvariant && setEquality && collisions.length === 0,
};
const fileMoves = bRefs.map((r) => ({
from: `skills/${absorbed}/references/${r}`,
to: `skills/${absorber}/references/${r}`,
}));
const taxonomyReassignments = Object.keys(categorySkill)
.filter((category) => categorySkill[category] === absorbed)
.sort()
.map((category) => ({ category, from: absorbed, to: absorber }));
const diff = {
fileMoves,
taxonomyReassignments,
descriptionReconciliation: {
absorber: descriptions[absorber] ?? null,
absorbed: descriptions[absorbed] ?? null,
note: 'Descriptions must be reconciled manually / by judge — the planner does not auto-merge semantic scope.',
},
retire: absorbed,
};
const entry = {
operation_type: MERGE_OPERATION,
status: 'pending',
decided_at: ctx.decided_at ?? null, // never Date.now() in a pure lib
targets: { absorber, absorbed },
guardrail,
note:
ctx.note ??
`Dry-run: merge ${absorbed} -> ${absorber} (${guardrail.ok ? 'guardrail OK' : 'GUARDRAIL FAILED'}). Applies nothing.`,
};
return { entry, diff, guardrail };
}
// ---------------------------------------------------------------------------
// Impure shell — read-only disk load + gated ledger write
// ---------------------------------------------------------------------------
/** Recursively list references/-relative .md paths (POSIX separators) under one skill. */
function listSkillRefs(refDir) {
const out = [];
if (!existsSync(refDir)) return out;
const walk = (dir) => {
for (const e of readdirSync(dir, { withFileTypes: true })) {
const p = join(dir, e.name);
if (e.isDirectory()) walk(p);
else if (e.isFile() && e.name.endsWith('.md')) out.push(relative(refDir, p).split(sep).join('/'));
}
};
walk(refDir);
return out.sort();
}
/**
* Read-only: { skill -> [references-relative .md paths] } for every skill dir
* that has a references/ folder.
* @param {string} skillsDir absolute path to the skills/ root
* @returns {Record<string,string[]>}
*/
export function loadSkillRefs(skillsDir) {
const out = {};
for (const e of readdirSync(skillsDir, { withFileTypes: true })) {
if (!e.isDirectory()) continue;
const refDir = join(skillsDir, e.name, 'references');
if (existsSync(refDir) && statSync(refDir).isDirectory()) out[e.name] = listSkillRefs(refDir);
}
return out;
}
/**
* Drive a merge dry-run against real disk: load refs read-only, plan the merge,
* and (only with write:true) record the PENDING entry in decisions.json. Writes
* NOTHING under skills/ the destructive apply is a separate approved step.
* @param {string} absorber
* @param {string} absorbed
* @param {{ skillsDir: string, dataDir: string, write?: boolean, decided_at?: string|null,
* descriptions?: Record<string,string>, categorySkill?: Record<string,string> }} opts
* @returns {{ entry: object, diff: object, guardrail: object }}
*/
export function runMergePlan(absorber, absorbed, opts) {
const { skillsDir, dataDir, write = false, decided_at = null, descriptions, categorySkill } = opts;
const skillRefs = loadSkillRefs(skillsDir);
const result = planMergeSkills(absorber, absorbed, { skillRefs, descriptions, categorySkill, decided_at });
if (write) {
const ledger = recordAction(loadDecisions(dataDir), result.entry);
saveDecisions(ledger, dataDir);
}
return result;
}

View file

@ -0,0 +1,94 @@
#!/usr/bin/env node
// plan-skill-op.mjs — Spor B / B3 CLI: GATED DRY-RUN for skill-lifecycle ops.
//
// Sesjon 16 ships merge_skills. The dry-run loads refs/descriptions/taxonomy
// READ-ONLY, plans the merge, prints the full diff + guardrail, and with
// --write records a PENDING entry in decisions.json (the operator gate's queue).
// It NEVER writes under skills/ — the destructive apply is a separate approved
// step. Verify after a run: `git status skills/` must be clean.
//
// Usage:
// node scripts/kb-eval/plan-skill-op.mjs merge <absorber> <absorbed>
// node scripts/kb-eval/plan-skill-op.mjs merge <absorber> <absorbed> --json
// node scripts/kb-eval/plan-skill-op.mjs merge <absorber> <absorbed> --write [--date YYYY-MM-DD]
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { splitFrontmatter, extractDescription } from './eval.mjs';
import { loadTaxonomy } from '../kb-update/lib/taxonomy.mjs';
import { runMergePlan } from './lib/skill-ops.mjs';
const __dirname = dirname(fileURLToPath(import.meta.url));
const PLUGIN_ROOT = join(__dirname, '..', '..');
const SKILLS_DIR = join(PLUGIN_ROOT, 'skills');
const LEDGER_DATA_DIR = join(__dirname, '..', 'kb-update', 'data');
const TAX_DATA_DIR = LEDGER_DATA_DIR;
/** Read the SKILL.md descriptions from disk (read-only). */
function loadDescriptions() {
const out = {};
for (const e of readdirSync(SKILLS_DIR, { withFileTypes: true })) {
if (!e.isDirectory()) continue;
const md = join(SKILLS_DIR, e.name, 'SKILL.md');
if (!existsSync(md)) continue;
out[e.name] = extractDescription(splitFrontmatter(readFileSync(md, 'utf8')).frontmatter);
}
return out;
}
function printPlan({ entry, diff, guardrail }) {
const g = guardrail;
console.log(`\nmerge_skills — DRY-RUN: ${entry.targets.absorbed} -> ${entry.targets.absorber}\n`);
console.log(`Guardrail (ingen kuratert verdi tapt):`);
console.log(` absorber-refs=${g.preCountAbsorber} absorbed-refs=${g.preCountAbsorbed} forventet post=${g.expectedPostCount} faktisk post=${g.postCount}`);
console.log(` count-invariant=${g.countInvariant} set-equality=${g.setEquality} kollisjoner=${g.collisions.length ? g.collisions.join(', ') : '0'}`);
console.log(` => ${g.ok ? 'OK (trygt å merge)' : 'FAILED (ville tapt/klobret innhold — blokkert)'}\n`);
console.log(`Diff:`);
console.log(` fil-flytt: ${diff.fileMoves.length} (alle ${entry.targets.absorbed}/references/* -> ${entry.targets.absorber}/references/*)`);
if (diff.taxonomyReassignments.length) {
console.log(` taksonomi-reassign:`);
for (const t of diff.taxonomyReassignments) console.log(`${t.category}: ${t.from} -> ${t.to}`);
}
console.log(` retire: ${diff.retire}`);
console.log(` description-reconciliation: MANUELL (planner auto-merger aldri semantisk scope)`);
console.log(` ${entry.targets.absorber}: ${diff.descriptionReconciliation.absorber?.slice(0, 80) ?? '(mangler)'}`);
console.log(` ${entry.targets.absorbed}: ${diff.descriptionReconciliation.absorbed?.slice(0, 80) ?? '(mangler)'}\n`);
console.log(`Status: ${entry.status} (forslag — anvender INGENTING; krever operatør-godkjenning + apply-sesjon).\n`);
}
function main() {
const args = process.argv.slice(2);
const op = args[0];
const positional = args.filter((a) => !a.startsWith('--'));
const jsonOut = args.includes('--json');
const doWrite = args.includes('--write');
const dateIdx = args.indexOf('--date');
const decided_at = doWrite ? (dateIdx >= 0 ? args[dateIdx + 1] : new Date().toISOString().slice(0, 10)) : null;
if (op !== 'merge' || positional.length < 3) {
console.error('Usage: node scripts/kb-eval/plan-skill-op.mjs merge <absorber> <absorbed> [--json] [--write [--date YYYY-MM-DD]]');
process.exit(2);
}
const [, absorber, absorbed] = positional;
const result = runMergePlan(absorber, absorbed, {
skillsDir: SKILLS_DIR,
dataDir: LEDGER_DATA_DIR,
write: doWrite,
decided_at,
descriptions: loadDescriptions(),
categorySkill: loadTaxonomy(TAX_DATA_DIR).category_skill,
});
if (jsonOut) {
process.stdout.write(JSON.stringify(result) + '\n');
return;
}
printPlan(result);
if (doWrite) console.log(`(Pending merge_skills-entry skrevet til decisions.json — 0 skriving til skills/.)\n`);
}
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
main();
}

View file

@ -17,21 +17,32 @@ const DEFAULT_DATA_DIR = join(__dirname, '..', 'data');
/**
* Empty ledger scaffold.
* @returns {{version: number, updated_at: string|null, decisions: object}}
*
* Two parallel collections, both written ONLY through the operator gate:
* - decisions URL-keyed (Spor A): which Microsoft Learn page belongs where.
* - actions skill-keyed (Spor B / B3): skill-lifecycle ops (merge_skills,
* sanitize_skill, retire_skill, create_skill). Additive: version
* stays 1, and a pre-action ledger on disk normalizes cleanly
* (loadDecisions backfills the missing actions key).
* @returns {{version: number, updated_at: string|null, decisions: object, actions: object}}
*/
export function createLedger() {
return { version: 1, updated_at: null, decisions: {} };
return { version: 1, updated_at: null, decisions: {}, actions: {} };
}
/**
* Load the decision ledger from disk.
* Load the decision ledger from disk. Backward-compatible: a ledger written
* before the action layer existed (no `actions` key) is normalized to `{}` so
* callers never have to guard against undefined.
* @param {string} [dataDir] defaults to ../data/ relative to lib/
* @returns {object} parsed ledger or empty scaffold
*/
export function loadDecisions(dataDir = DEFAULT_DATA_DIR) {
const path = join(dataDir, 'decisions.json');
if (!existsSync(path)) return createLedger();
return JSON.parse(readFileSync(path, 'utf8'));
const led = JSON.parse(readFileSync(path, 'utf8'));
if (!led.actions) led.actions = {};
return led;
}
/**
@ -84,3 +95,66 @@ export function recordDecision(ledger, url, decision) {
export function filterUndecided(ledger, candidates) {
return candidates.filter((c) => !isDecided(ledger, c.url));
}
// ===========================================================================
// Skill-level action layer (Spor B / B3) — additive, mirrors the URL helpers.
// Detection/transformation produce a PENDING action entry; only the gate writes
// it. recordAction never mutates skills/ — it records a proposal in the ledger.
// ===========================================================================
/**
* Deterministic dedup key for a skill-lifecycle action. merge_skills is keyed
* symmetrically on the pair (merging A<-B and B<-A dedup to the same key);
* single-skill ops (sanitize/retire/create) are keyed by their one target.
* @param {{operation_type: string, targets?: object}} entry
* @returns {string}
*/
export function actionKey(entry) {
const op = entry.operation_type;
const t = entry.targets ?? {};
if (op === 'merge_skills') {
const pair = [t.absorber, t.absorbed].filter(Boolean).sort();
return `merge_skills:${pair.join('+')}`;
}
const single = t.skill ?? t.name ?? t.absorber ?? '?';
return `${op}:${single}`;
}
/**
* Has the operator already decided on this action? Policy A: any entry
* (pending | approved | rejected) counts mirrors isDecided for URLs.
* @param {object} ledger
* @param {string} key from actionKey()
* @returns {boolean}
*/
export function isActionDecided(ledger, key) {
return Boolean(ledger.actions && ledger.actions[key]);
}
/**
* Record a skill-lifecycle action. Pure returns a new ledger, does not mutate.
* Keyed by actionKey(entry). Leaves the URL-keyed decisions map untouched.
* @param {object} ledger
* @param {{operation_type: string, status: string, decided_at?: string|null,
* targets?: object, guardrail?: object, note?: string}} entry
* @returns {object} new ledger
*/
export function recordAction(ledger, entry) {
const key = actionKey(entry);
return {
...ledger,
updated_at: entry.decided_at ?? ledger.updated_at,
actions: { ...(ledger.actions ?? {}), [key]: { ...entry } },
};
}
/**
* List recorded actions, optionally filtered by status.
* @param {object} ledger
* @param {string|null} [status] when given, keep only entries with this status
* @returns {Array<object>}
*/
export function listActions(ledger, status = null) {
const all = Object.values(ledger.actions ?? {});
return status ? all.filter((a) => a.status === status) : all;
}

View file

@ -0,0 +1,174 @@
// tests/kb-eval/test-skill-ops-merge.test.mjs
// Spor B / B3 (Sesjon 16): merge_skills as a GATED DRY-RUN PLANNER.
//
// Two layers:
// (A) pure core — planMergeSkills(absorber, absorbed, ctx): mutates nothing,
// reads no disk, returns { entry, diff, guardrail }. The guardrail proves
// no curated value is lost (count-invariant + set-equality) and refuses
// (ok=false) when two ref files would clobber on the same target path.
// (B) impure shell — runMergePlan(...) + loadSkillRefs(...): read-only disk
// load, optional gated `--write` of a PENDING entry to decisions.json.
// The destructive proof: running the plan makes ZERO changes under skills/.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync, existsSync, readdirSync, statSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { planMergeSkills, loadSkillRefs, runMergePlan, MERGE_OPERATION } from '../../scripts/kb-eval/lib/skill-ops.mjs';
import { loadDecisions, isActionDecided, actionKey } from '../../scripts/kb-update/lib/decisions-io.mjs';
// ===========================================================================
// (A) Pure core — planMergeSkills
// ===========================================================================
const CTX = {
skillRefs: {
'skill-a': ['cat1/a1.md', 'cat1/a2.md', 'cat2/a3.md'], // absorber, 3
'skill-b': ['cat3/b1.md', 'cat3/b2.md'], // absorbed, 2
},
descriptions: { 'skill-a': 'desc A', 'skill-b': 'desc B' },
categorySkill: { cat1: 'skill-a', cat2: 'skill-a', cat3: 'skill-b' },
decided_at: '2026-06-20',
};
test('planMergeSkills — guardrail OK for a clean (no-collision) union', () => {
const { guardrail } = planMergeSkills('skill-a', 'skill-b', CTX);
assert.equal(guardrail.ok, true);
assert.equal(guardrail.preCountAbsorber, 3);
assert.equal(guardrail.preCountAbsorbed, 2);
assert.equal(guardrail.expectedPostCount, 5, 'no loss: union of both ref sets');
assert.equal(guardrail.postCount, 5);
assert.equal(guardrail.countInvariant, true);
assert.equal(guardrail.setEquality, true);
assert.deepEqual(guardrail.collisions, []);
});
test('planMergeSkills — set-equality: EVERY ref from both skills survives in the plan', () => {
const { diff, guardrail } = planMergeSkills('skill-a', 'skill-b', CTX);
// absorbed files appear as moves; absorber files stay in place. Their union of
// references-relative identities must equal the pre-merge union with no loss.
const movedIdentities = diff.fileMoves.map((m) => m.to.replace('skills/skill-a/references/', ''));
const survivingIdentities = new Set([...CTX.skillRefs['skill-a'], ...movedIdentities]);
for (const r of [...CTX.skillRefs['skill-a'], ...CTX.skillRefs['skill-b']]) {
assert.ok(survivingIdentities.has(r), `${r} must survive the merge`);
}
assert.equal(survivingIdentities.size, guardrail.expectedPostCount);
});
test('planMergeSkills — guardrail FAILS (ok=false) when ref paths would collide/clobber', () => {
const colliding = {
...CTX,
skillRefs: { 'skill-a': ['cat1/shared.md', 'cat1/a1.md'], 'skill-b': ['cat1/shared.md'] },
};
const { guardrail } = planMergeSkills('skill-a', 'skill-b', colliding);
assert.equal(guardrail.ok, false, 'a clobber must not be silently approved');
assert.deepEqual(guardrail.collisions, ['cat1/shared.md']);
assert.equal(guardrail.countInvariant, false, 'postCount < expected -> loss detected');
});
test('planMergeSkills — entry is a PENDING merge_skills action with both targets', () => {
const { entry } = planMergeSkills('skill-a', 'skill-b', CTX);
assert.equal(entry.operation_type, MERGE_OPERATION);
assert.equal(entry.status, 'pending', 'planner only proposes — never approved');
assert.deepEqual(entry.targets, { absorber: 'skill-a', absorbed: 'skill-b' });
assert.equal(entry.decided_at, '2026-06-20');
assert.equal(entry.guardrail.ok, true);
});
test('planMergeSkills — diff carries moves, taxonomy reassignment, retire, and manual description reconciliation', () => {
const { diff } = planMergeSkills('skill-a', 'skill-b', CTX);
assert.deepEqual(diff.fileMoves, [
{ from: 'skills/skill-b/references/cat3/b1.md', to: 'skills/skill-a/references/cat3/b1.md' },
{ from: 'skills/skill-b/references/cat3/b2.md', to: 'skills/skill-a/references/cat3/b2.md' },
]);
assert.deepEqual(diff.taxonomyReassignments, [{ category: 'cat3', from: 'skill-b', to: 'skill-a' }]);
assert.equal(diff.retire, 'skill-b');
assert.equal(diff.descriptionReconciliation.absorber, 'desc A');
assert.match(diff.descriptionReconciliation.note, /reconcil/i, 'semantic merge flagged as manual, not auto-done');
});
test('planMergeSkills — throws on self-merge', () => {
assert.throws(() => planMergeSkills('skill-a', 'skill-a', CTX), /self|same/i);
});
test('planMergeSkills — missing ref entry defaults to empty (no throw)', () => {
const { guardrail } = planMergeSkills('skill-a', 'ghost', { skillRefs: { 'skill-a': ['c/x.md'] } });
assert.equal(guardrail.preCountAbsorbed, 0);
assert.equal(guardrail.expectedPostCount, 1);
assert.equal(guardrail.ok, true);
});
// ===========================================================================
// (B) Impure shell — loadSkillRefs + runMergePlan (read-only + gated ledger write)
// ===========================================================================
// Build a throwaway skills/ fixture: two skills with a few non-colliding refs.
function withSkillsFixture(fn) {
const root = mkdtempSync(join(tmpdir(), 'skillops-'));
const mk = (skill, cat, file, body) => {
const dir = join(root, 'skills', skill, 'references', cat);
mkdirSync(dir, { recursive: true });
writeFileSync(join(dir, file), body);
};
mk('skill-a', 'cat1', 'a1.md', '# A1\n');
mk('skill-a', 'cat1', 'a2.md', '# A2\n');
mk('skill-b', 'cat3', 'b1.md', '# B1\n');
const dataDir = join(root, 'data');
mkdirSync(dataDir, { recursive: true });
try {
return fn({ skillsDir: join(root, 'skills'), dataDir });
} finally {
rmSync(root, { recursive: true, force: true });
}
}
// Snapshot every file under a dir as path -> mtimeMs+size, for change detection.
function snapshot(dir) {
const out = {};
const walk = (d) => {
for (const e of readdirSync(d, { withFileTypes: true })) {
const p = join(d, e.name);
if (e.isDirectory()) walk(p);
else { const s = statSync(p); out[p] = `${s.size}:${s.mtimeMs}`; }
}
};
walk(dir);
return out;
}
test('loadSkillRefs — read-only, returns references-relative paths per skill', () => {
withSkillsFixture(({ skillsDir }) => {
const refs = loadSkillRefs(skillsDir);
assert.deepEqual(refs['skill-a'].sort(), ['cat1/a1.md', 'cat1/a2.md']);
assert.deepEqual(refs['skill-b'], ['cat3/b1.md']);
});
});
test('runMergePlan --write — records a PENDING entry in the ledger and touches NOTHING under skills/', () => {
withSkillsFixture(({ skillsDir, dataDir }) => {
const before = snapshot(skillsDir);
const { entry, guardrail } = runMergePlan('skill-a', 'skill-b', { skillsDir, dataDir, write: true, decided_at: '2026-06-20' });
// proposal is correct
assert.equal(entry.status, 'pending');
assert.equal(guardrail.ok, true);
assert.equal(guardrail.expectedPostCount, 3);
// ledger received the pending action (the gate's only write path)
const led = loadDecisions(dataDir);
assert.equal(isActionDecided(led, actionKey(entry)), true);
assert.equal(led.actions[actionKey(entry)].status, 'pending');
// THE DESTRUCTIVE INVARIANT: skills/ is byte-for-byte unchanged
assert.deepEqual(snapshot(skillsDir), before, 'dry-run must not write under skills/');
});
});
test('runMergePlan without --write — pure preview, no ledger file created', () => {
withSkillsFixture(({ skillsDir, dataDir }) => {
const { entry } = runMergePlan('skill-a', 'skill-b', { skillsDir, dataDir, write: false });
assert.equal(entry.status, 'pending');
assert.equal(existsSync(join(dataDir, 'decisions.json')), false, 'no write without --write');
});
});

View file

@ -0,0 +1,156 @@
// tests/kb-update/test-decisions-actions.test.mjs
// Spor B / B3 (Sesjon 16): the ADDITIVE skill-level action layer on the lag-2
// decision ledger. The URL-keyed `decisions` map (Spor A) is untouched — its
// contract is covered by test-decisions-io.test.mjs, which stays green
// unmodified, proving the extension is non-regressive. Here we cover only the
// new `actions` collection: actionKey dedup, recordAction (pure), isActionDecided,
// listActions, and backward-compatible normalization of pre-action ledgers.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
createLedger,
loadDecisions,
saveDecisions,
recordDecision,
isDecided,
actionKey,
isActionDecided,
recordAction,
listActions,
} from '../../scripts/kb-update/lib/decisions-io.mjs';
function withTmp(fn) {
const dir = mkdtempSync(join(tmpdir(), 'dec-act-test-'));
try {
return fn(dir);
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
const MERGE = {
operation_type: 'merge_skills',
status: 'pending',
decided_at: '2026-06-20',
targets: { absorber: 'ms-ai-infrastructure', absorbed: 'ms-ai-engineering' },
guardrail: { ok: true },
note: 'dry-run',
};
// --- scaffold now carries an empty actions collection (additive) ---
test('createLedger — additive actions:{} alongside untouched decisions:{}', () => {
const led = createLedger();
assert.equal(led.version, 1, 'version stays 1 — no migration needed');
assert.deepEqual(led.decisions, {}, 'URL-keyed map unchanged');
assert.deepEqual(led.actions, {}, 'new skill-op collection present and empty');
});
// --- actionKey — deterministic dedup key ---
test('actionKey — merge_skills key is symmetric on the pair (direction-agnostic dedup)', () => {
const k1 = actionKey(MERGE);
const k2 = actionKey({
...MERGE,
targets: { absorber: 'ms-ai-engineering', absorbed: 'ms-ai-infrastructure' },
});
assert.equal(k1, k2, 'merging A<-B and B<-A must dedup to the same key');
assert.equal(k1, 'merge_skills:ms-ai-engineering+ms-ai-infrastructure');
});
test('actionKey — single-skill ops keyed by their one target', () => {
assert.equal(
actionKey({ operation_type: 'sanitize_skill', targets: { skill: 'ms-ai-governance' } }),
'sanitize_skill:ms-ai-governance',
);
assert.equal(
actionKey({ operation_type: 'create_skill', targets: { name: 'ms-power-platform' } }),
'create_skill:ms-power-platform',
);
});
// --- recordAction — pure, mirrors recordDecision ---
test('recordAction — records under actionKey, returns new ledger, does not mutate input', () => {
const led = createLedger();
const next = recordAction(led, MERGE);
assert.deepEqual(led.actions, {}, 'input untouched');
assert.notEqual(next, led);
assert.equal(next.actions['merge_skills:ms-ai-engineering+ms-ai-infrastructure'].status, 'pending');
});
test('recordAction — advances updated_at to the action date', () => {
const next = recordAction(createLedger(), MERGE);
assert.equal(next.updated_at, '2026-06-20');
});
test('recordAction — does NOT touch the URL-keyed decisions map', () => {
let led = recordDecision(createLedger(), 'https://learn.microsoft.com/x', {
status: 'approved',
decided_at: '2026-06-19',
});
const next = recordAction(led, MERGE);
assert.equal(isDecided(next, 'https://learn.microsoft.com/x'), true, 'URL decision survives');
assert.equal(isActionDecided(next, actionKey(MERGE)), true, 'action recorded alongside');
});
// --- isActionDecided — dedup gate (policy A: any status counts) ---
test('isActionDecided — true for ANY recorded status, false when absent', () => {
let led = createLedger();
assert.equal(isActionDecided(led, actionKey(MERGE)), false);
led = recordAction(led, { ...MERGE, status: 'rejected' });
assert.equal(isActionDecided(led, actionKey(MERGE)), true, 'rejected merge still dedups (policy A)');
});
// --- listActions — flat list, optional status filter ---
test('listActions — returns all entries, filters by status when given', () => {
let led = createLedger();
led = recordAction(led, MERGE); // pending
led = recordAction(led, {
operation_type: 'sanitize_skill',
status: 'approved',
decided_at: '2026-06-20',
targets: { skill: 'ms-ai-governance' },
});
assert.equal(listActions(led).length, 2);
assert.deepEqual(
listActions(led, 'pending').map((a) => a.operation_type),
['merge_skills'],
);
assert.deepEqual(
listActions(led, 'approved').map((a) => a.operation_type),
['sanitize_skill'],
);
});
// --- backward compatibility: a pre-action ledger on disk loads cleanly ---
test('loadDecisions — normalizes a pre-action (no actions key) ledger to actions:{}', () => {
withTmp((dir) => {
// Simulate an old on-disk ledger written before the action layer existed.
writeFileSync(
join(dir, 'decisions.json'),
JSON.stringify({ version: 1, updated_at: null, decisions: {} }, null, 2),
);
const led = loadDecisions(dir);
assert.deepEqual(led.actions, {}, 'missing actions key is backfilled, not undefined');
// and recording an action on the loaded ledger works
const next = recordAction(led, MERGE);
assert.equal(isActionDecided(next, actionKey(MERGE)), true);
});
});
test('saveDecisions + loadDecisions — actions round-trip through disk', () => {
withTmp((dir) => {
const led = recordAction(createLedger(), MERGE);
saveDecisions(led, dir);
const back = loadDecisions(dir);
assert.deepEqual(back, led);
});
});