// tests/kb-eval/test-skill-ops-sanitize-retire.test.mjs // Spor B / B3 (Sesjon 17): sanitize_skill + retire_skill as GATED DRY-RUN PLANNERS. // Mirrors the merge_skills planner (S16): a pure core that mutates nothing and // reads no disk, plus an impure shell that loads read-only and (with write:true) // records a PENDING entry to decisions.json — NEVER touching skills/. // // sanitize_skill — removes ONLY dead content (kb-integrity orphan/dead-path). // Guardrail: every removal must be a flagged dead file; curated refs are // never touched. A request to remove a curated ref FAILS the guardrail. // retire_skill — retires a WHOLE skill with MANDATORY archival (never a hard // delete). Guardrail: every reference file + SKILL.md is archived; a // hard-delete request is refused (ok=false). import { test } from 'node:test'; import assert from 'node:assert/strict'; import { mkdtempSync, rmSync, mkdirSync, writeFileSync, existsSync, readdirSync, statSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { planSanitizeSkill, planRetireSkill, loadSkillOrphans, runSanitizePlan, runRetirePlan, SANITIZE_OPERATION, RETIRE_OPERATION, } from '../../scripts/kb-eval/lib/skill-ops.mjs'; import { loadDecisions, isActionDecided, actionKey } from '../../scripts/kb-update/lib/decisions-io.mjs'; // =========================================================================== // (A) Pure core — planSanitizeSkill // =========================================================================== const S_CTX = { // skill-x has 4 refs; two are dead (orphan/dead-path), two are curated. refs: ['cat1/keep1.md', 'cat1/keep2.md', 'cat2/dead1.md', 'cat2/dead2.md'], orphans: ['cat2/dead1.md', 'cat2/dead2.md'], decided_at: '2026-06-20', }; test('planSanitizeSkill — guardrail OK: default removes all dead-in-skill, curated untouched', () => { const { guardrail } = planSanitizeSkill('skill-x', S_CTX); assert.equal(guardrail.ok, true); assert.equal(guardrail.preCount, 4); assert.equal(guardrail.deadCount, 2); assert.equal(guardrail.removalCount, 2); assert.equal(guardrail.curatedCount, 2, 'two curated refs preserved'); assert.equal(guardrail.postCount, 2, 'postCount = preCount - removals'); assert.deepEqual(guardrail.illegalRemovals, []); }); test('planSanitizeSkill — diff lists removals with reason and retains curated count', () => { const { diff } = planSanitizeSkill('skill-x', S_CTX); assert.deepEqual( diff.removals.map((r) => r.path), ['skills/skill-x/references/cat2/dead1.md', 'skills/skill-x/references/cat2/dead2.md'], ); for (const r of diff.removals) assert.match(r.reason, /orphan|dead/i); assert.equal(diff.retainedCount, 2); }); test('planSanitizeSkill — guardrail FAILS when a removal targets a CURATED (non-orphan) ref', () => { const { guardrail } = planSanitizeSkill('skill-x', { ...S_CTX, removals: ['cat1/keep1.md'] }); assert.equal(guardrail.ok, false, 'deleting curated content must never be silently approved'); assert.deepEqual(guardrail.illegalRemovals, ['cat1/keep1.md']); }); test('planSanitizeSkill — guardrail FAILS when a removal points at a non-existent file', () => { const { guardrail } = planSanitizeSkill('skill-x', { ...S_CTX, removals: ['cat9/ghost.md'] }); assert.equal(guardrail.ok, false); assert.deepEqual(guardrail.missingRemovals, ['cat9/ghost.md']); }); test('planSanitizeSkill — no orphans -> empty removal, ok=true, postCount=preCount (no-op safe)', () => { const { guardrail, diff } = planSanitizeSkill('skill-x', { refs: ['c/a.md', 'c/b.md'], orphans: [] }); assert.equal(guardrail.ok, true); assert.equal(guardrail.removalCount, 0); assert.equal(guardrail.postCount, 2); assert.deepEqual(diff.removals, []); }); test('planSanitizeSkill — entry is a PENDING sanitize_skill action with the target skill', () => { const { entry } = planSanitizeSkill('skill-x', S_CTX); assert.equal(entry.operation_type, SANITIZE_OPERATION); assert.equal(entry.status, 'pending'); assert.deepEqual(entry.targets, { skill: 'skill-x' }); assert.equal(entry.decided_at, '2026-06-20'); assert.equal(actionKey(entry), 'sanitize_skill:skill-x'); }); test('planSanitizeSkill — throws on missing skill', () => { assert.throws(() => planSanitizeSkill('', S_CTX), /skill/i); }); // =========================================================================== // (B) Pure core — planRetireSkill // =========================================================================== const R_CTX = { refs: ['cat1/a.md', 'cat1/b.md', 'cat2/c.md'], categorySkill: { cat1: 'doomed', cat2: 'doomed', cat3: 'survivor' }, decided_at: '2026-06-20', }; test('planRetireSkill — guardrail OK: archives every ref + SKILL.md (count = refs + 1)', () => { const { guardrail } = planRetireSkill('doomed', R_CTX); assert.equal(guardrail.ok, true); assert.equal(guardrail.refCount, 3); assert.equal(guardrail.expectedArchiveCount, 4, 'refs + SKILL.md'); assert.equal(guardrail.archivedCount, 4); assert.equal(guardrail.hardDeleteRequested, false); assert.match(guardrail.archiveDir, /archive\/skills\/doomed/); }); test('planRetireSkill — guardrail FAILS on a hard-delete request (no archival)', () => { const { guardrail } = planRetireSkill('doomed', { ...R_CTX, hardDelete: true }); assert.equal(guardrail.ok, false, 'hard delete without archive is forbidden'); assert.equal(guardrail.hardDeleteRequested, true); assert.equal(guardrail.archiveDir, null); }); test('planRetireSkill — diff carries archive moves (incl SKILL.md), dir removal, and orphaned taxonomy', () => { const { diff } = planRetireSkill('doomed', R_CTX); assert.ok( diff.archiveMoves.some((m) => m.from === 'skills/doomed/SKILL.md'), 'SKILL.md must be archived', ); assert.equal(diff.archiveMoves.length, 4, '3 refs + SKILL.md'); assert.equal(diff.removeSkillDir, 'skills/doomed'); assert.deepEqual( diff.taxonomyOrphaned.map((t) => t.category).sort(), ['cat1', 'cat2'], 'categories the retired skill owned need manual reassignment', ); }); test('planRetireSkill — entry is a PENDING retire_skill action with the target skill', () => { const { entry } = planRetireSkill('doomed', R_CTX); assert.equal(entry.operation_type, RETIRE_OPERATION); assert.equal(entry.status, 'pending'); assert.deepEqual(entry.targets, { skill: 'doomed' }); assert.equal(actionKey(entry), 'retire_skill:doomed'); }); test('planRetireSkill — throws on missing skill', () => { assert.throws(() => planRetireSkill('', R_CTX), /skill/i); }); // =========================================================================== // (C) Impure shell — loadSkillOrphans + runSanitizePlan / runRetirePlan // =========================================================================== // Fixture: a skills/ tree + an agents/ dir. A ref is "dead" iff its basename // appears in NO agent file and NO SKILL.md (kb-integrity orphan semantics). function withFixture(fn) { const root = mkdtempSync(join(tmpdir(), 'skillops-sr-')); const skillsDir = join(root, 'skills'); const agentsDir = join(root, 'agents'); const dataDir = join(root, 'data'); const mkRef = (skill, cat, file, body) => { const dir = join(skillsDir, skill, 'references', cat); mkdirSync(dir, { recursive: true }); writeFileSync(join(dir, file), body); }; const mkSkillMd = (skill, body) => { mkdirSync(join(skillsDir, skill), { recursive: true }); writeFileSync(join(skillsDir, skill, 'SKILL.md'), body); }; // skill-a: live.md referenced by SKILL.md + agent; dead.md referenced nowhere. mkRef('skill-a', 'cat1', 'live.md', '# Live\n'); mkRef('skill-a', 'cat1', 'dead.md', '# Dead\n'); mkSkillMd('skill-a', '# skill-a\nSee references/cat1/live.md for details.\n'); // skill-b: a small skill we will retire. mkRef('skill-b', 'cat3', 'b1.md', '# B1\n'); mkSkillMd('skill-b', '# skill-b\nSee references/cat3/b1.md.\n'); mkdirSync(agentsDir, { recursive: true }); writeFileSync(join(agentsDir, 'agent.md'), 'Reads references/cat1/live.md and references/cat3/b1.md\n'); mkdirSync(dataDir, { recursive: true }); try { return fn({ skillsDir, agentsDir, dataDir }); } finally { rmSync(root, { recursive: true, force: true }); } } // Snapshot every file under a dir as path -> size:mtimeMs, 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('loadSkillOrphans — read-only, flags refs whose basename is unreferenced by agents/SKILL.md', () => { withFixture(({ skillsDir, agentsDir }) => { const orphans = loadSkillOrphans(skillsDir, agentsDir); assert.deepEqual(orphans['skill-a'], ['cat1/dead.md'], 'only dead.md is orphaned'); assert.deepEqual(orphans['skill-b'], [], 'b1.md is referenced -> not orphaned'); }); }); test('runSanitizePlan --write — records a PENDING entry and touches NOTHING under skills/', () => { withFixture(({ skillsDir, agentsDir, dataDir }) => { const before = snapshot(skillsDir); const { entry, guardrail } = runSanitizePlan('skill-a', { skillsDir, agentsDir, dataDir, write: true, decided_at: '2026-06-20' }); assert.equal(entry.status, 'pending'); assert.equal(guardrail.ok, true); assert.equal(guardrail.removalCount, 1, 'only the dead orphan is removed'); assert.equal(guardrail.curatedCount, 1, 'live.md preserved'); const led = loadDecisions(dataDir); assert.equal(isActionDecided(led, actionKey(entry)), true); assert.equal(led.actions[actionKey(entry)].status, 'pending'); assert.deepEqual(snapshot(skillsDir), before, 'sanitize dry-run must not write under skills/'); }); }); test('runRetirePlan --write — records a PENDING entry and touches NOTHING under skills/', () => { withFixture(({ skillsDir, dataDir }) => { const before = snapshot(skillsDir); const { entry, guardrail } = runRetirePlan('skill-b', { skillsDir, dataDir, write: true, decided_at: '2026-06-20' }); assert.equal(entry.status, 'pending'); assert.equal(guardrail.ok, true); assert.equal(guardrail.expectedArchiveCount, 2, 'b1.md + SKILL.md'); const led = loadDecisions(dataDir); assert.equal(isActionDecided(led, actionKey(entry)), true); assert.deepEqual(snapshot(skillsDir), before, 'retire dry-run must not write under skills/'); }); }); test('runSanitizePlan without --write — pure preview, no ledger file created', () => { withFixture(({ skillsDir, agentsDir, dataDir }) => { const { entry } = runSanitizePlan('skill-a', { skillsDir, agentsDir, dataDir, write: false }); assert.equal(entry.status, 'pending'); assert.equal(existsSync(join(dataDir, 'decisions.json')), false, 'no write without --write'); }); });