feat(ms-ai-architect): Sesjon 17 — B3 sanitize_skill + retire_skill dry-run-planners [skip-docs]
Speiler S16 merge-mønsteret: rene planners (leser ingen disk, anvender
ingenting) + impurt skall med gated --write. 0 skriving til skills/.
Intern kb-eval maintenance-tooling — ingen brukervendt /architect-kommando.
- planSanitizeSkill: fjerner KUN dødt innhold (kb-integrity orphan/dead-path).
Guardrail refuserer fjerning av kuratert/manglende innhold (illegalRemovals/
missingRemovals MÅ være tom). Dateless = kuratert, behandles ikke her.
- planRetireSkill: hel-skill m/ obligatorisk arkivering; guardrail refuserer
hard delete (--hard -> archiveDir=null, ok=false). taxonomyOrphaned flagget.
- loadSkillOrphans (read-only) + runSanitizePlan/runRetirePlan (gated).
- CLI plan-skill-op.mjs utvidet: {sanitize|retire} <skill> [--hard].
Ekte dry-runs: sanitize ms-ai-security 62/33 orphans OK · retire
ms-ai-infrastructure 35 arkivert OK · --hard FAILED (blokkert). git clean.
TDD: test-skill-ops-sanitize-retire.test.mjs (16). kb-eval 50->66.
Baseline uendret: validate 239 · kb-update 132 · kb-integrity 192/192.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f03a0e08b2
commit
f7b622b318
3 changed files with 564 additions and 25 deletions
|
|
@ -12,8 +12,8 @@
|
|||
// 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 { readdirSync, readFileSync, existsSync, statSync } from 'node:fs';
|
||||
import { join, relative, basename, sep } from 'node:path';
|
||||
import {
|
||||
loadDecisions,
|
||||
saveDecisions,
|
||||
|
|
@ -21,6 +21,8 @@ import {
|
|||
} from '../../kb-update/lib/decisions-io.mjs';
|
||||
|
||||
export const MERGE_OPERATION = 'merge_skills';
|
||||
export const SANITIZE_OPERATION = 'sanitize_skill';
|
||||
export const RETIRE_OPERATION = 'retire_skill';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pure core — planMergeSkills
|
||||
|
|
@ -113,6 +115,162 @@ export function planMergeSkills(absorber, absorbed, ctx = {}) {
|
|||
return { entry, diff, guardrail };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pure core — planSanitizeSkill (remove ONLY dead content)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Plan a skill sanitize: remove dead reference files (kb-integrity orphan /
|
||||
* dead-path) and NOTHING else. Pure; mutates nothing; reads no disk. The
|
||||
* binding guardrail (mirror of merge's collision rule) is that every removal
|
||||
* must be a flagged dead file — a request to remove a CURATED ref fails ok.
|
||||
* Dateless-but-curated files are NOT dead and are never removed here.
|
||||
*
|
||||
* @param {string} skill skill whose dead content is pruned
|
||||
* @param {{ refs?: string[], orphans?: string[], removals?: string[],
|
||||
* decided_at?: string|null, note?: string }} [ctx]
|
||||
* refs = all references-relative .md paths under the skill
|
||||
* orphans = references-relative paths kb-integrity flags as orphan/dead
|
||||
* removals = optional explicit removal list (default = all dead-in-skill)
|
||||
* @returns {{ entry: object, diff: object, guardrail: object }}
|
||||
*/
|
||||
export function planSanitizeSkill(skill, ctx = {}) {
|
||||
if (!skill) throw new Error('planSanitizeSkill: skill is required');
|
||||
const refs = ctx.refs ?? [];
|
||||
const orphans = ctx.orphans ?? [];
|
||||
const refSet = new Set(refs);
|
||||
const orphanSet = new Set(orphans);
|
||||
|
||||
// Removable universe = dead files that actually exist under this skill.
|
||||
// Default removal = sanitize ALL dead content surfaced by kb-integrity.
|
||||
const deadInSkill = [...new Set(orphans.filter((r) => refSet.has(r)))].sort();
|
||||
const removals = [...new Set(ctx.removals ?? deadInSkill)].sort();
|
||||
|
||||
// BINDING GUARDRAIL: a removal must be a kb-integrity-flagged dead file AND a
|
||||
// real file under the skill. Anything else would delete curated content.
|
||||
const illegalRemovals = removals.filter((r) => !orphanSet.has(r)).sort();
|
||||
const missingRemovals = removals.filter((r) => !refSet.has(r)).sort();
|
||||
|
||||
const preCount = refs.length;
|
||||
const removalCount = removals.length;
|
||||
const curatedCount = refs.filter((r) => !orphanSet.has(r)).length;
|
||||
const postCount = preCount - removalCount;
|
||||
|
||||
const guardrail = {
|
||||
method:
|
||||
'only kb-integrity-flagged orphan/dead-path files may be removed; curated refs are never touched. ' +
|
||||
'illegalRemovals = requested removals NOT in the dead-set (curated content) — must be empty. ' +
|
||||
'missingRemovals = removals absent from disk — must be empty. postCount = preCount - removals.',
|
||||
preCount,
|
||||
deadCount: deadInSkill.length,
|
||||
removalCount,
|
||||
curatedCount,
|
||||
postCount,
|
||||
illegalRemovals,
|
||||
missingRemovals,
|
||||
ok: illegalRemovals.length === 0 && missingRemovals.length === 0,
|
||||
};
|
||||
|
||||
const diff = {
|
||||
removals: removals.map((r) => ({
|
||||
path: `skills/${skill}/references/${r}`,
|
||||
reason: 'orphan/dead-path: basename unreferenced by any agent or SKILL.md (kb-integrity)',
|
||||
})),
|
||||
retainedCount: curatedCount,
|
||||
note: 'Sanitize removes only dead content surfaced by kb-integrity. Curated references (incl. dateless) are preserved untouched.',
|
||||
};
|
||||
|
||||
const entry = {
|
||||
operation_type: SANITIZE_OPERATION,
|
||||
status: 'pending',
|
||||
decided_at: ctx.decided_at ?? null, // never Date.now() in a pure lib
|
||||
targets: { skill },
|
||||
guardrail,
|
||||
note:
|
||||
ctx.note ??
|
||||
`Dry-run: sanitize ${skill} — remove ${removalCount} dead file(s) (${guardrail.ok ? 'guardrail OK' : 'GUARDRAIL FAILED'}). Applies nothing.`,
|
||||
};
|
||||
|
||||
return { entry, diff, guardrail };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pure core — planRetireSkill (whole-skill retire WITH mandatory archival)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Plan a whole-skill retire: archive every reference file + SKILL.md, then
|
||||
* remove the skills/ directory. Pure; mutates nothing; reads no disk. Archival
|
||||
* is MANDATORY — the binding guardrail refuses a hard-delete request (ok=false)
|
||||
* so curated content is never destroyed without a recoverable copy.
|
||||
*
|
||||
* @param {string} skill skill to retire
|
||||
* @param {{ refs?: string[], categorySkill?: Record<string,string>,
|
||||
* archiveDir?: string, hardDelete?: boolean,
|
||||
* decided_at?: string|null, note?: string }} [ctx]
|
||||
* @returns {{ entry: object, diff: object, guardrail: object }}
|
||||
*/
|
||||
export function planRetireSkill(skill, ctx = {}) {
|
||||
if (!skill) throw new Error('planRetireSkill: skill is required');
|
||||
const refs = ctx.refs ?? [];
|
||||
const categorySkill = ctx.categorySkill ?? {};
|
||||
const hardDelete = ctx.hardDelete === true;
|
||||
// Hard delete is refused -> no archive destination; otherwise default to a
|
||||
// recoverable archive path under archive/skills/<skill>.
|
||||
const archiveDir = hardDelete ? null : (ctx.archiveDir ?? `archive/skills/${skill}`);
|
||||
|
||||
const archiveMoves = archiveDir
|
||||
? [
|
||||
{ from: `skills/${skill}/SKILL.md`, to: `${archiveDir}/SKILL.md` },
|
||||
...refs.map((r) => ({
|
||||
from: `skills/${skill}/references/${r}`,
|
||||
to: `${archiveDir}/references/${r}`,
|
||||
})),
|
||||
]
|
||||
: [];
|
||||
const expectedArchiveCount = refs.length + 1; // refs + SKILL.md
|
||||
const archivedCount = archiveMoves.length;
|
||||
|
||||
// Categories this skill owns lose their owner on retire — manual reassignment.
|
||||
const taxonomyOrphaned = Object.keys(categorySkill)
|
||||
.filter((c) => categorySkill[c] === skill)
|
||||
.sort()
|
||||
.map((c) => ({ category: c, wasOwner: skill, note: 'owner removed — reassign manually' }));
|
||||
|
||||
const guardrail = {
|
||||
method:
|
||||
'whole-skill retire requires archival — every reference file + SKILL.md is MOVED to archive/, never hard-deleted. ' +
|
||||
'ok iff an archive destination is set AND archivedCount === refCount + 1 (SKILL.md). A hard-delete request is refused.',
|
||||
refCount: refs.length,
|
||||
expectedArchiveCount,
|
||||
archivedCount,
|
||||
archiveDir,
|
||||
hardDeleteRequested: hardDelete,
|
||||
ok: Boolean(archiveDir) && archivedCount === expectedArchiveCount && !hardDelete,
|
||||
};
|
||||
|
||||
const diff = {
|
||||
archiveMoves,
|
||||
removeSkillDir: `skills/${skill}`,
|
||||
taxonomyOrphaned,
|
||||
note: 'Retire archives the entire skill, then removes its skills/ directory. Categories it owned need manual reassignment.',
|
||||
};
|
||||
|
||||
const entry = {
|
||||
operation_type: RETIRE_OPERATION,
|
||||
status: 'pending',
|
||||
decided_at: ctx.decided_at ?? null, // never Date.now() in a pure lib
|
||||
targets: { skill },
|
||||
guardrail,
|
||||
note:
|
||||
ctx.note ??
|
||||
`Dry-run: retire ${skill} — archive ${expectedArchiveCount} file(s) to ${archiveDir ?? '(none)'} ` +
|
||||
`(${guardrail.ok ? 'guardrail OK' : 'GUARDRAIL FAILED — hard delete refused'}). Applies nothing.`,
|
||||
};
|
||||
|
||||
return { entry, diff, guardrail };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Impure shell — read-only disk load + gated ledger write
|
||||
// ---------------------------------------------------------------------------
|
||||
|
|
@ -168,3 +326,70 @@ export function runMergePlan(absorber, absorbed, opts) {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read-only: { skill -> [references-relative dead paths] }. A reference is
|
||||
* "dead" iff its basename appears in NO agent file and NO SKILL.md — exactly
|
||||
* kb-integrity's orphan semantics (grep -l basename over agents/ + SKILL.md).
|
||||
* @param {string} skillsDir absolute path to the skills/ root
|
||||
* @param {string} [agentsDir] absolute path to the agents/ dir (optional)
|
||||
* @returns {Record<string,string[]>}
|
||||
*/
|
||||
export function loadSkillOrphans(skillsDir, agentsDir) {
|
||||
const skillRefs = loadSkillRefs(skillsDir);
|
||||
// Build the reference haystack: every agent file + every SKILL.md body.
|
||||
let haystack = '';
|
||||
if (agentsDir && existsSync(agentsDir)) {
|
||||
for (const e of readdirSync(agentsDir, { withFileTypes: true })) {
|
||||
if (e.isFile() && e.name.endsWith('.md')) haystack += readFileSync(join(agentsDir, e.name), 'utf8') + '\n';
|
||||
}
|
||||
}
|
||||
for (const skill of Object.keys(skillRefs)) {
|
||||
const md = join(skillsDir, skill, 'SKILL.md');
|
||||
if (existsSync(md)) haystack += readFileSync(md, 'utf8') + '\n';
|
||||
}
|
||||
const out = {};
|
||||
for (const [skill, refs] of Object.entries(skillRefs)) {
|
||||
out[skill] = refs.filter((r) => !haystack.includes(basename(r)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drive a sanitize dry-run against real disk: load refs + orphans read-only,
|
||||
* plan the removal, and (only with write:true) record the PENDING entry in
|
||||
* decisions.json. Writes NOTHING under skills/.
|
||||
* @param {string} skill
|
||||
* @param {{ skillsDir: string, agentsDir?: string, dataDir: string, write?: boolean,
|
||||
* decided_at?: string|null, removals?: string[] }} opts
|
||||
* @returns {{ entry: object, diff: object, guardrail: object }}
|
||||
*/
|
||||
export function runSanitizePlan(skill, opts) {
|
||||
const { skillsDir, agentsDir, dataDir, write = false, decided_at = null, removals } = opts;
|
||||
const refs = loadSkillRefs(skillsDir)[skill] ?? [];
|
||||
const orphans = loadSkillOrphans(skillsDir, agentsDir)[skill] ?? [];
|
||||
const result = planSanitizeSkill(skill, { refs, orphans, removals, decided_at });
|
||||
if (write) {
|
||||
saveDecisions(recordAction(loadDecisions(dataDir), result.entry), dataDir);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drive a retire dry-run against real disk: load refs read-only, plan the
|
||||
* whole-skill archive+removal, and (only with write:true) record the PENDING
|
||||
* entry in decisions.json. Writes NOTHING under skills/.
|
||||
* @param {string} skill
|
||||
* @param {{ skillsDir: string, dataDir: string, write?: boolean, decided_at?: string|null,
|
||||
* archiveDir?: string, hardDelete?: boolean, categorySkill?: Record<string,string> }} opts
|
||||
* @returns {{ entry: object, diff: object, guardrail: object }}
|
||||
*/
|
||||
export function runRetirePlan(skill, opts) {
|
||||
const { skillsDir, dataDir, write = false, decided_at = null, archiveDir, hardDelete, categorySkill } = opts;
|
||||
const refs = loadSkillRefs(skillsDir)[skill] ?? [];
|
||||
const result = planRetireSkill(skill, { refs, categorySkill, archiveDir, hardDelete, decided_at });
|
||||
if (write) {
|
||||
saveDecisions(recordAction(loadDecisions(dataDir), result.entry), dataDir);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,28 @@
|
|||
#!/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.
|
||||
// Ships merge_skills (S16) + sanitize_skill + retire_skill (S17). Each dry-run
|
||||
// loads refs/orphans/taxonomy READ-ONLY, plans the op, 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/` 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]
|
||||
// node scripts/kb-eval/plan-skill-op.mjs merge <absorber> <absorbed> [--json] [--write [--date YYYY-MM-DD]]
|
||||
// node scripts/kb-eval/plan-skill-op.mjs sanitize <skill> [--json] [--write [--date YYYY-MM-DD]]
|
||||
// node scripts/kb-eval/plan-skill-op.mjs retire <skill> [--hard] [--json] [--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';
|
||||
import { runMergePlan, runSanitizePlan, runRetirePlan } 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 AGENTS_DIR = join(PLUGIN_ROOT, 'agents');
|
||||
const LEDGER_DATA_DIR = join(__dirname, '..', 'kb-update', 'data');
|
||||
const TAX_DATA_DIR = LEDGER_DATA_DIR;
|
||||
|
||||
|
|
@ -57,36 +58,101 @@ function printPlan({ entry, diff, guardrail }) {
|
|||
console.log(`Status: ${entry.status} (forslag — anvender INGENTING; krever operatør-godkjenning + apply-sesjon).\n`);
|
||||
}
|
||||
|
||||
function printSanitizePlan({ entry, diff, guardrail }) {
|
||||
const g = guardrail;
|
||||
console.log(`\nsanitize_skill — DRY-RUN: ${entry.targets.skill}\n`);
|
||||
console.log(`Guardrail (kun dødt innhold; aldri kuratert):`);
|
||||
console.log(` refs=${g.preCount} dead(orphan)=${g.deadCount} fjernes=${g.removalCount} kuratert-beholdt=${g.curatedCount} post=${g.postCount}`);
|
||||
console.log(` ulovlige-fjerninger(kuratert)=${g.illegalRemovals.length ? g.illegalRemovals.join(', ') : '0'} manglende=${g.missingRemovals.length ? g.missingRemovals.join(', ') : '0'}`);
|
||||
console.log(` => ${g.ok ? 'OK (trygt å sanere)' : 'FAILED (ville rørt kuratert/manglende innhold — blokkert)'}\n`);
|
||||
console.log(`Diff — fjernes (${diff.removals.length}):`);
|
||||
for (const r of diff.removals.slice(0, 20)) console.log(` • ${r.path}`);
|
||||
if (diff.removals.length > 20) console.log(` … +${diff.removals.length - 20} til`);
|
||||
console.log(` beholdt (kuratert): ${diff.retainedCount}\n`);
|
||||
console.log(`Status: ${entry.status} (forslag — anvender INGENTING; krever operatør-godkjenning + apply-sesjon).\n`);
|
||||
}
|
||||
|
||||
function printRetirePlan({ entry, diff, guardrail }) {
|
||||
const g = guardrail;
|
||||
console.log(`\nretire_skill — DRY-RUN: ${entry.targets.skill}\n`);
|
||||
console.log(`Guardrail (obligatorisk arkivering; aldri hard delete):`);
|
||||
console.log(` refs=${g.refCount} forventet-arkivert=${g.expectedArchiveCount} (refs + SKILL.md) faktisk-arkivert=${g.archivedCount}`);
|
||||
console.log(` arkiv-mål=${g.archiveDir ?? '(ingen)'} hard-delete-bedt=${g.hardDeleteRequested}`);
|
||||
console.log(` => ${g.ok ? 'OK (arkivert — trygt å pensjonere)' : 'FAILED (hard delete uten arkiv — blokkert)'}\n`);
|
||||
console.log(`Diff:`);
|
||||
console.log(` arkiv-flytt: ${diff.archiveMoves.length} (alle skills/${entry.targets.skill}/* -> ${g.archiveDir}/*)`);
|
||||
console.log(` fjern katalog: ${diff.removeSkillDir}`);
|
||||
if (diff.taxonomyOrphaned.length) {
|
||||
console.log(` taksonomi forelder-løs (manuell reassign):`);
|
||||
for (const t of diff.taxonomyOrphaned) console.log(` • ${t.category} (var eid av ${t.wasOwner})`);
|
||||
}
|
||||
console.log(`\nStatus: ${entry.status} (forslag — anvender INGENTING; krever operatør-godkjenning + apply-sesjon).\n`);
|
||||
}
|
||||
|
||||
const USAGE =
|
||||
'Usage:\n' +
|
||||
' node scripts/kb-eval/plan-skill-op.mjs merge <absorber> <absorbed> [--json] [--write [--date YYYY-MM-DD]]\n' +
|
||||
' node scripts/kb-eval/plan-skill-op.mjs sanitize <skill> [--json] [--write [--date YYYY-MM-DD]]\n' +
|
||||
' node scripts/kb-eval/plan-skill-op.mjs retire <skill> [--hard] [--json] [--write [--date YYYY-MM-DD]]';
|
||||
|
||||
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 hardDelete = args.includes('--hard');
|
||||
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]]');
|
||||
let result;
|
||||
let printer;
|
||||
let label;
|
||||
|
||||
if (op === 'merge' && positional.length >= 3) {
|
||||
const [, absorber, absorbed] = positional;
|
||||
result = runMergePlan(absorber, absorbed, {
|
||||
skillsDir: SKILLS_DIR,
|
||||
dataDir: LEDGER_DATA_DIR,
|
||||
write: doWrite,
|
||||
decided_at,
|
||||
descriptions: loadDescriptions(),
|
||||
categorySkill: loadTaxonomy(TAX_DATA_DIR).category_skill,
|
||||
});
|
||||
printer = printPlan;
|
||||
label = 'merge_skills';
|
||||
} else if (op === 'sanitize' && positional.length >= 2) {
|
||||
result = runSanitizePlan(positional[1], {
|
||||
skillsDir: SKILLS_DIR,
|
||||
agentsDir: AGENTS_DIR,
|
||||
dataDir: LEDGER_DATA_DIR,
|
||||
write: doWrite,
|
||||
decided_at,
|
||||
});
|
||||
printer = printSanitizePlan;
|
||||
label = 'sanitize_skill';
|
||||
} else if (op === 'retire' && positional.length >= 2) {
|
||||
result = runRetirePlan(positional[1], {
|
||||
skillsDir: SKILLS_DIR,
|
||||
dataDir: LEDGER_DATA_DIR,
|
||||
write: doWrite,
|
||||
decided_at,
|
||||
hardDelete,
|
||||
categorySkill: loadTaxonomy(TAX_DATA_DIR).category_skill,
|
||||
});
|
||||
printer = printRetirePlan;
|
||||
label = 'retire_skill';
|
||||
} else {
|
||||
console.error(USAGE);
|
||||
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`);
|
||||
printer(result);
|
||||
if (doWrite) console.log(`(Pending ${label}-entry skrevet til decisions.json — 0 skriving til skills/.)\n`);
|
||||
}
|
||||
|
||||
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
||||
|
|
|
|||
248
tests/kb-eval/test-skill-ops-sanitize-retire.test.mjs
Normal file
248
tests/kb-eval/test-skill-ops-sanitize-retire.test.mjs
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
// 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');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue