feat(ms-ai-architect): Sesjon 18 — B3 apply-path (sanitize+retire), merge→S19 [skip-docs]
Apply-path = den destruktive frontieren: eksekver en operatør-godkjent
(status:approved) ledger-entry → faktisk skills/-mutasjon. Staget per
operatør-beslutning til single-skill ops (sanitize+retire); merge-apply
isoleres til S19 (krever taksonomi-persistering + cross-skill flytt).
- revalidateApply (ren, skill-ops.mjs): idempotent revalidering mot fersk
re-plan — nekter ved status≠approved, op-mismatch, fersk guardrail≠ok,
eller drift (isDeepStrictEqual fresh vs approved guardrail-snapshot).
- applyApprovedAction (impur): arkiver-så-slett (rename skills/…→archive/…
atomisk; retire rmdir'er tom katalog sist) + flipp ledger approved→applied.
apply:false = preview (0 mutasjon). merge_skills → throw (S19).
- setActionStatus (ren, decisions-io.mjs): ledger status-transisjon, bevarer
targets+guardrail, audit-record beholdes.
- CLI apply-skill-op.mjs {list|sanitize|retire} — default PREVIEW, --apply =
dobbel-gate utover ledger-approved.
TDD, tmpdir-fixtures: 0 ekte skills/-mutasjon. Tester: kb-eval 66→78,
kb-update 132→137; validate 239 · kb-integrity 192/192 uendret.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f7b622b318
commit
4ac18a76c8
5 changed files with 603 additions and 2 deletions
123
scripts/kb-eval/apply-skill-op.mjs
Normal file
123
scripts/kb-eval/apply-skill-op.mjs
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
#!/usr/bin/env node
|
||||
// apply-skill-op.mjs — Spor B / B3 (Sesjon 18) CLI: the DESTRUCTIVE apply-path.
|
||||
//
|
||||
// Executes an operator-APPROVED (status:approved) skill-lifecycle entry from
|
||||
// decisions.json into a real skills/ mutation. Single-skill ops only this
|
||||
// session: sanitize_skill + retire_skill (merge_skills is staged to S19).
|
||||
//
|
||||
// Two gates, in order: (1) the entry must already be status:approved in the
|
||||
// ledger (the operator's sign-off on a prior `plan-skill-op … --write` dry-run);
|
||||
// (2) this CLI mutates skills/ ONLY with --apply. Without --apply it is a PREVIEW
|
||||
// that re-plans from fresh disk, revalidates (drift-safe), and mutates nothing.
|
||||
//
|
||||
// Apply archives every file under archive/ BEFORE removing it, then flips the
|
||||
// ledger entry approved -> applied. Verify before/after: `git status`.
|
||||
//
|
||||
// Usage:
|
||||
// node scripts/kb-eval/apply-skill-op.mjs list
|
||||
// node scripts/kb-eval/apply-skill-op.mjs sanitize <skill> [--apply] [--date YYYY-MM-DD]
|
||||
// node scripts/kb-eval/apply-skill-op.mjs retire <skill> [--apply] [--date YYYY-MM-DD]
|
||||
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { loadTaxonomy } from '../kb-update/lib/taxonomy.mjs';
|
||||
import { loadDecisions, listActions, actionKey } from '../kb-update/lib/decisions-io.mjs';
|
||||
import { applyApprovedAction, SANITIZE_OPERATION, RETIRE_OPERATION } 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 OP_BY_VERB = { sanitize: SANITIZE_OPERATION, retire: RETIRE_OPERATION };
|
||||
|
||||
const USAGE =
|
||||
'Usage:\n' +
|
||||
' node scripts/kb-eval/apply-skill-op.mjs list\n' +
|
||||
' node scripts/kb-eval/apply-skill-op.mjs sanitize <skill> [--apply] [--date YYYY-MM-DD]\n' +
|
||||
' node scripts/kb-eval/apply-skill-op.mjs retire <skill> [--apply] [--date YYYY-MM-DD]';
|
||||
|
||||
/** List approved actions awaiting apply (operator's apply queue). */
|
||||
function listApproved() {
|
||||
const led = loadDecisions(LEDGER_DATA_DIR);
|
||||
const approved = listActions(led, 'approved');
|
||||
const applied = listActions(led, 'applied');
|
||||
console.log(`\nGodkjente handlinger som venter på apply (${approved.length}):`);
|
||||
if (!approved.length) console.log(' (ingen — kjør plan-skill-op … --write og sett status:approved i decisions.json)');
|
||||
for (const a of approved) {
|
||||
const t = a.targets ?? {};
|
||||
const tgt = t.skill ?? `${t.absorbed} -> ${t.absorber}`;
|
||||
console.log(` • ${a.operation_type}: ${tgt} [guardrail ${a.guardrail?.ok ? 'OK' : 'FAILED'}]`);
|
||||
}
|
||||
if (applied.length) console.log(`\nAllerede anvendt (${applied.length}): ${applied.map((a) => actionKey(a)).join(', ')}`);
|
||||
console.log('');
|
||||
}
|
||||
|
||||
function printResult(res, verb, skill, apply) {
|
||||
const r = res.revalidate;
|
||||
console.log(`\n${verb}_skill — ${apply ? 'APPLY' : 'PREVIEW'}: ${skill}\n`);
|
||||
console.log(`Revalidering (mot fersk disk):`);
|
||||
console.log(` fersk-guardrail-ok=${r.freshOk} drift=${r.drift} => ${r.ok ? 'OK' : 'BLOKKERT'}`);
|
||||
if (r.reason) console.log(` grunn: ${r.reason}`);
|
||||
if (!res.applied) {
|
||||
if (res.preview && r.ok) {
|
||||
const d = res.plan?.diff ?? {};
|
||||
const n = verb === 'sanitize' ? (d.removals?.length ?? 0) : (d.archiveMoves?.length ?? 0);
|
||||
console.log(`\n Ville arkivert+fjernet ${n} fil(er). Kjør på nytt med --apply for å eksekvere.`);
|
||||
}
|
||||
console.log(`\nStatus: INGEN mutasjon utført.${res.preview && r.ok ? '' : ' (revalidering blokkerte eller ingen --apply.)'}\n`);
|
||||
return;
|
||||
}
|
||||
const rep = res.report;
|
||||
console.log(`\nUtført:`);
|
||||
console.log(` arkivert: ${rep.moves} fil(er) -> archive/`);
|
||||
if (rep.removedDir) console.log(` fjernet katalog: ${rep.removedDir}`);
|
||||
console.log(` ledger: ${actionKey({ operation_type: rep.op, targets: rep.target })} -> ${rep.ledgerStatus}`);
|
||||
console.log(`\nStatus: APPLIED. Verifiser med 'git status' og re-kjør validate/kb-integrity/kb-eval.\n`);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const args = process.argv.slice(2);
|
||||
const verb = args[0];
|
||||
if (verb === 'list') return listApproved();
|
||||
|
||||
const positional = args.filter((a) => !a.startsWith('--'));
|
||||
const apply = args.includes('--apply');
|
||||
const dateIdx = args.indexOf('--date');
|
||||
const decided_at = dateIdx >= 0 ? args[dateIdx + 1] : new Date().toISOString().slice(0, 10);
|
||||
|
||||
const op = OP_BY_VERB[verb];
|
||||
if (!op || positional.length < 2) {
|
||||
console.error(USAGE);
|
||||
process.exit(2);
|
||||
}
|
||||
const skill = positional[1];
|
||||
|
||||
const led = loadDecisions(LEDGER_DATA_DIR);
|
||||
const key = `${op}:${skill}`;
|
||||
const entry = led.actions?.[key];
|
||||
if (!entry) {
|
||||
console.error(`Fant ingen ledger-handling "${key}". Kjør 'apply-skill-op.mjs list' for å se godkjente.`);
|
||||
process.exit(1);
|
||||
}
|
||||
if (entry.status !== 'approved') {
|
||||
console.error(`Handling "${key}" har status "${entry.status}" (må være "approved"). Avbryter.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const res = applyApprovedAction(entry, {
|
||||
pluginRoot: PLUGIN_ROOT,
|
||||
skillsDir: SKILLS_DIR,
|
||||
agentsDir: AGENTS_DIR,
|
||||
dataDir: LEDGER_DATA_DIR,
|
||||
apply,
|
||||
decided_at: apply ? decided_at : null,
|
||||
categorySkill: op === RETIRE_OPERATION ? loadTaxonomy(LEDGER_DATA_DIR).category_skill : undefined,
|
||||
});
|
||||
printResult(res, verb, skill, apply);
|
||||
}
|
||||
|
||||
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
||||
main();
|
||||
}
|
||||
|
|
@ -12,12 +12,15 @@
|
|||
// preserved (ref-file count-invariant + set-equality) and refuse (ok=false)
|
||||
// when two reference files would clobber on the same target path.
|
||||
|
||||
import { readdirSync, readFileSync, existsSync, statSync } from 'node:fs';
|
||||
import { join, relative, basename, sep } from 'node:path';
|
||||
import { readdirSync, readFileSync, existsSync, statSync, mkdirSync, renameSync, rmSync } from 'node:fs';
|
||||
import { join, relative, basename, dirname, sep } from 'node:path';
|
||||
import { isDeepStrictEqual } from 'node:util';
|
||||
import {
|
||||
loadDecisions,
|
||||
saveDecisions,
|
||||
recordAction,
|
||||
actionKey,
|
||||
setActionStatus,
|
||||
} from '../../kb-update/lib/decisions-io.mjs';
|
||||
|
||||
export const MERGE_OPERATION = 'merge_skills';
|
||||
|
|
@ -393,3 +396,107 @@ export function runRetirePlan(skill, opts) {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// Apply-path (Sesjon 18) — the DESTRUCTIVE frontier. An operator-approved
|
||||
// ledger entry is executed into a real skills/ mutation. Single-skill ops only
|
||||
// (sanitize/retire); merge-apply is staged to S19 (needs taxonomy persistence).
|
||||
//
|
||||
// Invariants:
|
||||
// - Idempotent revalidation: re-plan from FRESH disk and refuse unless the
|
||||
// fresh guardrail is ok AND deep-equals the approved snapshot (no drift).
|
||||
// - Archive BEFORE delete: every removal is a rename skills/… -> archive/…,
|
||||
// which moves (archives) and removes atomically; retire then rmdir's the
|
||||
// emptied skill directory last.
|
||||
// ===========================================================================
|
||||
|
||||
/**
|
||||
* Pure: re-check an operator-approved entry against a FRESH re-plan. Refuses if
|
||||
* the entry is not approved, the op-type differs, the fresh guardrail is not ok,
|
||||
* or the fresh guardrail differs from the approved snapshot (disk drifted since
|
||||
* approval). Reads no disk — the caller supplies the fresh plan.
|
||||
* @param {object} approvedEntry the ledger entry (status must be 'approved')
|
||||
* @param {{ entry: object, guardrail: object }} freshResult output of a fresh re-plan
|
||||
* @returns {{ ok: boolean, drift: boolean, freshOk: boolean, reason: string|null }}
|
||||
*/
|
||||
export function revalidateApply(approvedEntry, freshResult) {
|
||||
const reasons = [];
|
||||
const status = approvedEntry?.status;
|
||||
if (status !== 'approved') reasons.push(`entry status "${status}" is not "approved"`);
|
||||
const op = approvedEntry?.operation_type;
|
||||
const freshOp = freshResult?.entry?.operation_type;
|
||||
if (op !== freshOp) reasons.push(`operation_type mismatch (approved=${op}, fresh=${freshOp})`);
|
||||
const freshOk = freshResult?.guardrail?.ok === true;
|
||||
if (!freshOk) reasons.push('fresh guardrail not ok — applying would lose or clobber content');
|
||||
const drift = !isDeepStrictEqual(approvedEntry?.guardrail, freshResult?.guardrail);
|
||||
if (drift) reasons.push('disk drifted since approval (fresh guardrail differs from approved snapshot)');
|
||||
return { ok: reasons.length === 0, drift, freshOk, reason: reasons.length ? reasons.join('; ') : null };
|
||||
}
|
||||
|
||||
/** Archive a single file: mkdir -p the destination dir, then rename (atomic move). */
|
||||
function archiveMove(absFrom, absTo) {
|
||||
mkdirSync(dirname(absTo), { recursive: true });
|
||||
renameSync(absFrom, absTo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an operator-approved single-skill op into a real skills/ mutation.
|
||||
* Re-plans from fresh disk, revalidates (drift-safe), and only with apply:true
|
||||
* performs the move(s) — archiving every file under archive/ BEFORE removing it,
|
||||
* then (retire) removing the emptied skill dir. On success the ledger entry is
|
||||
* flipped approved -> applied. apply:false is a preview that mutates nothing.
|
||||
*
|
||||
* @param {object} approvedEntry ledger entry with status:'approved'
|
||||
* @param {{ pluginRoot: string, skillsDir: string, agentsDir?: string, dataDir: string,
|
||||
* apply?: boolean, decided_at?: string|null, categorySkill?: Record<string,string> }} opts
|
||||
* @returns {{ applied: boolean, preview?: boolean, revalidate: object, plan?: object, report?: object }}
|
||||
*/
|
||||
export function applyApprovedAction(approvedEntry, opts) {
|
||||
const { pluginRoot, skillsDir, agentsDir, dataDir, apply = false, decided_at = null, categorySkill } = opts;
|
||||
const op = approvedEntry?.operation_type;
|
||||
const target = approvedEntry?.targets ?? {};
|
||||
|
||||
// 1. Re-plan from fresh disk (read-only) for the supported single-skill ops.
|
||||
let fresh;
|
||||
if (op === SANITIZE_OPERATION) {
|
||||
fresh = runSanitizePlan(target.skill, { skillsDir, agentsDir, dataDir, write: false });
|
||||
} else if (op === RETIRE_OPERATION) {
|
||||
fresh = runRetirePlan(target.skill, { skillsDir, dataDir, write: false, categorySkill });
|
||||
} else {
|
||||
throw new Error(`applyApprovedAction: unsupported operation_type "${op}" — S18 applies sanitize/retire only (merge staged to S19)`);
|
||||
}
|
||||
|
||||
// 2. Idempotent revalidation against fresh disk — refuse on any failure.
|
||||
const revalidate = revalidateApply(approvedEntry, fresh);
|
||||
if (!revalidate.ok || !apply) {
|
||||
return { applied: false, preview: !apply, revalidate, plan: fresh };
|
||||
}
|
||||
|
||||
// 3. Build the move list (archive destination = archive/ + repo-relative path).
|
||||
const moves =
|
||||
op === SANITIZE_OPERATION
|
||||
? fresh.diff.removals.map((r) => ({ from: r.path, to: join('archive', r.path) }))
|
||||
: fresh.diff.archiveMoves.map((m) => ({ from: m.from, to: m.to }));
|
||||
|
||||
// 4. Execute: archive (rename) every file FIRST, then (retire) drop the dir.
|
||||
const archived = [];
|
||||
for (const m of moves) {
|
||||
archiveMove(join(pluginRoot, m.from), join(pluginRoot, m.to));
|
||||
archived.push(m.to);
|
||||
}
|
||||
let removedDir = null;
|
||||
if (op === RETIRE_OPERATION) {
|
||||
removedDir = fresh.diff.removeSkillDir;
|
||||
rmSync(join(pluginRoot, removedDir), { recursive: true, force: true });
|
||||
}
|
||||
|
||||
// 5. Flip the ledger entry approved -> applied (gated write; audit record kept).
|
||||
const key = actionKey(approvedEntry);
|
||||
saveDecisions(setActionStatus(loadDecisions(dataDir), key, 'applied', decided_at), dataDir);
|
||||
|
||||
return {
|
||||
applied: true,
|
||||
revalidate,
|
||||
report: { op, target, moves: moves.length, archived, removedDir, ledgerStatus: 'applied' },
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -158,3 +158,29 @@ export function listActions(ledger, status = null) {
|
|||
const all = Object.values(ledger.actions ?? {});
|
||||
return status ? all.filter((a) => a.status === status) : all;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transition a recorded action to a new status. Pure — returns a new ledger,
|
||||
* does not mutate. Every other field (targets, guardrail, note) is preserved.
|
||||
* This is the apply-path's one write primitive (Sesjon 18): after a successful
|
||||
* skills/ mutation it flips the operator-approved entry approved -> applied,
|
||||
* keeping the entry as an audit record (policy A dedup still holds).
|
||||
* @param {object} ledger
|
||||
* @param {string} key — from actionKey()
|
||||
* @param {string} status — new status (e.g. 'approved', 'applied')
|
||||
* @param {string|null} [decided_at] — when given, advances the entry date + updated_at
|
||||
* @returns {object} new ledger
|
||||
* @throws if no action exists under `key`
|
||||
*/
|
||||
export function setActionStatus(ledger, key, status, decided_at = null) {
|
||||
const existing = ledger.actions?.[key];
|
||||
if (!existing) throw new Error(`setActionStatus: no action recorded under key "${key}"`);
|
||||
return {
|
||||
...ledger,
|
||||
updated_at: decided_at ?? ledger.updated_at,
|
||||
actions: {
|
||||
...ledger.actions,
|
||||
[key]: { ...existing, status, decided_at: decided_at ?? existing.decided_at },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
286
tests/kb-eval/test-skill-ops-apply.test.mjs
Normal file
286
tests/kb-eval/test-skill-ops-apply.test.mjs
Normal file
|
|
@ -0,0 +1,286 @@
|
|||
// tests/kb-eval/test-skill-ops-apply.test.mjs
|
||||
// Spor B / B3 (Sesjon 18): the APPLY-PATH — the destructive frontier. An
|
||||
// operator-approved (status:approved) ledger entry is executed into a real
|
||||
// skills/ mutation. Single-skill ops only this session (sanitize + retire);
|
||||
// merge-apply is staged to S19 (it needs taxonomy persistence + cross-skill
|
||||
// moves). create_skill comes last.
|
||||
//
|
||||
// Binding contract proven here:
|
||||
// 1. revalidateApply (pure) re-checks an approved entry against a FRESH plan:
|
||||
// status must be approved, op-type must match, fresh guardrail must be ok,
|
||||
// and the fresh guardrail must deep-equal the approved snapshot (no drift).
|
||||
// 2. applyApprovedAction archives BEFORE it deletes (rename is an atomic
|
||||
// archive+remove): dead content / whole skills land under archive/ first.
|
||||
// 3. Disk drift since approval (or a non-approved / guardrail-failing entry)
|
||||
// ABORTS with zero mutation and the ledger left untouched.
|
||||
// 4. A successful apply flips the ledger entry approved -> applied.
|
||||
//
|
||||
// Every scenario runs against a tmpdir fixture — NEVER the real skills/ tree.
|
||||
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, existsSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import {
|
||||
revalidateApply,
|
||||
applyApprovedAction,
|
||||
runSanitizePlan,
|
||||
runRetirePlan,
|
||||
planSanitizeSkill,
|
||||
SANITIZE_OPERATION,
|
||||
RETIRE_OPERATION,
|
||||
MERGE_OPERATION,
|
||||
} from '../../scripts/kb-eval/lib/skill-ops.mjs';
|
||||
import {
|
||||
loadDecisions,
|
||||
saveDecisions,
|
||||
recordAction,
|
||||
actionKey,
|
||||
} from '../../scripts/kb-update/lib/decisions-io.mjs';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Fixture: a skills/ tree + agents/ dir under one root, so archive/ lands at
|
||||
// the same root (rename stays on one filesystem). skill-a has a curated
|
||||
// (referenced) ref + a dead (orphan) ref; skill-b is a small skill to retire.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function withFixture(fn) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'skillops-apply-'));
|
||||
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);
|
||||
};
|
||||
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');
|
||||
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({ root, skillsDir, agentsDir, dataDir });
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// Seed an operator-approved entry by running the planner once (snapshotting its
|
||||
// guardrail) and recording it with status:approved — exactly what the operator
|
||||
// gate would have written after reviewing the dry-run.
|
||||
function approveSanitize(skill, { skillsDir, agentsDir, dataDir }) {
|
||||
const { entry } = runSanitizePlan(skill, { skillsDir, agentsDir, dataDir, write: false, decided_at: '2026-06-20' });
|
||||
const approved = { ...entry, status: 'approved' };
|
||||
saveDecisions(recordAction(loadDecisions(dataDir), approved), dataDir);
|
||||
return approved;
|
||||
}
|
||||
function approveRetire(skill, { skillsDir, dataDir }) {
|
||||
const { entry } = runRetirePlan(skill, { skillsDir, dataDir, write: false, decided_at: '2026-06-20' });
|
||||
const approved = { ...entry, status: 'approved' };
|
||||
saveDecisions(recordAction(loadDecisions(dataDir), approved), dataDir);
|
||||
return approved;
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// (A) Pure core — revalidateApply
|
||||
// ===========================================================================
|
||||
|
||||
// A self-consistent approved/fresh pair: same op, same guardrail snapshot, ok.
|
||||
function pair(overridesApproved = {}, overridesFresh = {}) {
|
||||
const guardrail = { method: 'm', preCount: 4, deadCount: 2, removalCount: 2, ok: true };
|
||||
const approved = {
|
||||
operation_type: SANITIZE_OPERATION,
|
||||
status: 'approved',
|
||||
targets: { skill: 'skill-x' },
|
||||
guardrail,
|
||||
...overridesApproved,
|
||||
};
|
||||
const fresh = {
|
||||
entry: { operation_type: SANITIZE_OPERATION, targets: { skill: 'skill-x' } },
|
||||
diff: {},
|
||||
guardrail: { ...guardrail },
|
||||
...overridesFresh,
|
||||
};
|
||||
return { approved, fresh };
|
||||
}
|
||||
|
||||
test('revalidateApply — OK: approved status, op match, fresh guardrail ok, no drift', () => {
|
||||
const { approved, fresh } = pair();
|
||||
const r = revalidateApply(approved, fresh);
|
||||
assert.equal(r.ok, true);
|
||||
assert.equal(r.drift, false);
|
||||
assert.equal(r.freshOk, true);
|
||||
assert.equal(r.reason, null);
|
||||
});
|
||||
|
||||
test('revalidateApply — refuses an entry whose status is not approved', () => {
|
||||
const { approved, fresh } = pair({ status: 'pending' });
|
||||
const r = revalidateApply(approved, fresh);
|
||||
assert.equal(r.ok, false);
|
||||
assert.match(r.reason, /approved/i);
|
||||
});
|
||||
|
||||
test('revalidateApply — refuses on operation_type mismatch', () => {
|
||||
const { approved, fresh } = pair({}, { entry: { operation_type: RETIRE_OPERATION, targets: {} } });
|
||||
const r = revalidateApply(approved, fresh);
|
||||
assert.equal(r.ok, false);
|
||||
assert.match(r.reason, /operation_type/i);
|
||||
});
|
||||
|
||||
test('revalidateApply — refuses when the FRESH guardrail is not ok', () => {
|
||||
const { approved, fresh } = pair({}, { guardrail: { method: 'm', preCount: 4, deadCount: 2, removalCount: 2, ok: false } });
|
||||
const r = revalidateApply(approved, fresh);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.freshOk, false);
|
||||
});
|
||||
|
||||
test('revalidateApply — detects DRIFT: fresh guardrail differs from approved snapshot', () => {
|
||||
// Disk changed since approval: one dead file already gone -> counts differ.
|
||||
const { approved, fresh } = pair({}, { guardrail: { method: 'm', preCount: 3, deadCount: 1, removalCount: 1, ok: true } });
|
||||
const r = revalidateApply(approved, fresh);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.drift, true);
|
||||
assert.match(r.reason, /drift/i);
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// (B) Impure executor — sanitize apply (archive-then-remove)
|
||||
// ===========================================================================
|
||||
|
||||
test('applyApprovedAction sanitize — archives the dead ref, removes it, leaves curated untouched', () => {
|
||||
withFixture(({ root, skillsDir, agentsDir, dataDir }) => {
|
||||
const approved = approveSanitize('skill-a', { skillsDir, agentsDir, dataDir });
|
||||
const res = applyApprovedAction(approved, { pluginRoot: root, skillsDir, agentsDir, dataDir, apply: true, decided_at: '2026-06-21' });
|
||||
|
||||
assert.equal(res.applied, true);
|
||||
assert.equal(res.revalidate.ok, true);
|
||||
|
||||
// Dead ref archived FIRST, then gone from skills/.
|
||||
assert.ok(existsSync(join(root, 'archive/skills/skill-a/references/cat1/dead.md')), 'dead ref archived');
|
||||
assert.equal(existsSync(join(skillsDir, 'skill-a/references/cat1/dead.md')), false, 'dead ref removed from skills/');
|
||||
|
||||
// Curated content untouched.
|
||||
assert.ok(existsSync(join(skillsDir, 'skill-a/references/cat1/live.md')), 'curated ref preserved');
|
||||
assert.ok(existsSync(join(skillsDir, 'skill-a/SKILL.md')), 'SKILL.md preserved');
|
||||
|
||||
// Ledger flipped approved -> applied.
|
||||
assert.equal(loadDecisions(dataDir).actions[actionKey(approved)].status, 'applied');
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// (C) Impure executor — retire apply (whole-skill archive, then dir removal)
|
||||
// ===========================================================================
|
||||
|
||||
test('applyApprovedAction retire — archives the whole skill, then removes its dir; siblings untouched', () => {
|
||||
withFixture(({ root, skillsDir, dataDir }) => {
|
||||
const approved = approveRetire('skill-b', { skillsDir, dataDir });
|
||||
const res = applyApprovedAction(approved, { pluginRoot: root, skillsDir, dataDir, apply: true, decided_at: '2026-06-21' });
|
||||
|
||||
assert.equal(res.applied, true);
|
||||
|
||||
// Entire skill archived (SKILL.md + ref) BEFORE the dir is removed.
|
||||
assert.ok(existsSync(join(root, 'archive/skills/skill-b/SKILL.md')), 'SKILL.md archived');
|
||||
assert.ok(existsSync(join(root, 'archive/skills/skill-b/references/cat3/b1.md')), 'ref archived');
|
||||
|
||||
// skills/skill-b is gone entirely.
|
||||
assert.equal(existsSync(join(skillsDir, 'skill-b')), false, 'retired skill dir removed');
|
||||
|
||||
// Sibling skill untouched.
|
||||
assert.ok(existsSync(join(skillsDir, 'skill-a/SKILL.md')), 'sibling skill survives');
|
||||
|
||||
assert.equal(loadDecisions(dataDir).actions[actionKey(approved)].status, 'applied');
|
||||
});
|
||||
});
|
||||
|
||||
// ===========================================================================
|
||||
// (D) Safety — drift, preview, non-approved, guardrail-fail, unsupported op
|
||||
// ===========================================================================
|
||||
|
||||
test('applyApprovedAction — DRIFT after approval aborts with ZERO mutation and an untouched ledger', () => {
|
||||
withFixture(({ root, skillsDir, agentsDir, dataDir }) => {
|
||||
const approved = approveSanitize('skill-a', { skillsDir, agentsDir, dataDir });
|
||||
// Operator approved when there was 1 dead file. Now the disk drifts: the
|
||||
// dead file is already gone -> a fresh plan finds 0 dead -> guardrail differs.
|
||||
rmSync(join(skillsDir, 'skill-a/references/cat1/dead.md'));
|
||||
|
||||
const res = applyApprovedAction(approved, { pluginRoot: root, skillsDir, agentsDir, dataDir, apply: true });
|
||||
|
||||
assert.equal(res.applied, false, 'drift must abort the apply');
|
||||
assert.equal(res.revalidate.drift, true);
|
||||
assert.equal(existsSync(join(root, 'archive')), false, 'nothing archived on a drift abort');
|
||||
assert.equal(loadDecisions(dataDir).actions[actionKey(approved)].status, 'approved', 'ledger left at approved');
|
||||
});
|
||||
});
|
||||
|
||||
test('applyApprovedAction — preview (apply:false) mutates nothing and leaves the ledger at approved', () => {
|
||||
withFixture(({ root, skillsDir, agentsDir, dataDir }) => {
|
||||
const approved = approveSanitize('skill-a', { skillsDir, agentsDir, dataDir });
|
||||
const res = applyApprovedAction(approved, { pluginRoot: root, skillsDir, agentsDir, dataDir, apply: false });
|
||||
|
||||
assert.equal(res.applied, false);
|
||||
assert.equal(res.preview, true);
|
||||
assert.equal(res.revalidate.ok, true, 'disk unchanged -> revalidation would pass');
|
||||
assert.ok(existsSync(join(skillsDir, 'skill-a/references/cat1/dead.md')), 'dead ref still present (preview only)');
|
||||
assert.equal(existsSync(join(root, 'archive')), false, 'preview archives nothing');
|
||||
assert.equal(loadDecisions(dataDir).actions[actionKey(approved)].status, 'approved');
|
||||
});
|
||||
});
|
||||
|
||||
test('applyApprovedAction — refuses a non-approved (pending) entry with zero mutation', () => {
|
||||
withFixture(({ root, skillsDir, agentsDir, dataDir }) => {
|
||||
const { entry } = runSanitizePlan('skill-a', { skillsDir, agentsDir, dataDir, write: false });
|
||||
// pending, not approved — operator never signed off.
|
||||
saveDecisions(recordAction(loadDecisions(dataDir), entry), dataDir);
|
||||
|
||||
const res = applyApprovedAction(entry, { pluginRoot: root, skillsDir, agentsDir, dataDir, apply: true });
|
||||
assert.equal(res.applied, false);
|
||||
assert.match(res.revalidate.reason, /approved/i);
|
||||
assert.ok(existsSync(join(skillsDir, 'skill-a/references/cat1/dead.md')), 'no mutation');
|
||||
assert.equal(existsSync(join(root, 'archive')), false);
|
||||
});
|
||||
});
|
||||
|
||||
test('applyApprovedAction — a guardrail-failing approved entry never applies', () => {
|
||||
withFixture(({ root, skillsDir, agentsDir, dataDir }) => {
|
||||
// Construct an approved entry whose guardrail is NOT ok (illegal removal of a
|
||||
// curated ref). A fresh plan with the same removals also fails the guardrail.
|
||||
const { entry } = planSanitizeSkill('skill-a', {
|
||||
refs: ['cat1/live.md', 'cat1/dead.md'],
|
||||
orphans: ['cat1/dead.md'],
|
||||
removals: ['cat1/live.md'], // curated -> illegal
|
||||
});
|
||||
assert.equal(entry.guardrail.ok, false, 'precondition: this dry-run already failed the guardrail');
|
||||
const approved = { ...entry, status: 'approved' };
|
||||
saveDecisions(recordAction(loadDecisions(dataDir), approved), dataDir);
|
||||
|
||||
const res = applyApprovedAction(approved, { pluginRoot: root, skillsDir, agentsDir, dataDir, apply: true });
|
||||
assert.equal(res.applied, false, 'a failing guardrail must never reach the filesystem');
|
||||
assert.equal(existsSync(join(root, 'archive')), false);
|
||||
assert.ok(existsSync(join(skillsDir, 'skill-a/references/cat1/live.md')), 'curated ref still safe');
|
||||
});
|
||||
});
|
||||
|
||||
test('applyApprovedAction — merge_skills is unsupported this session (staged to S19) and throws', () => {
|
||||
withFixture(({ root, skillsDir, agentsDir, dataDir }) => {
|
||||
const approved = {
|
||||
operation_type: MERGE_OPERATION,
|
||||
status: 'approved',
|
||||
targets: { absorber: 'skill-a', absorbed: 'skill-b' },
|
||||
guardrail: { ok: true },
|
||||
};
|
||||
assert.throws(
|
||||
() => applyApprovedAction(approved, { pluginRoot: root, skillsDir, agentsDir, dataDir, apply: true }),
|
||||
/merge|unsupported|S19/i,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
@ -21,6 +21,7 @@ import {
|
|||
isActionDecided,
|
||||
recordAction,
|
||||
listActions,
|
||||
setActionStatus,
|
||||
} from '../../scripts/kb-update/lib/decisions-io.mjs';
|
||||
|
||||
function withTmp(fn) {
|
||||
|
|
@ -154,3 +155,61 @@ test('saveDecisions + loadDecisions — actions round-trip through disk', () =>
|
|||
assert.deepEqual(back, led);
|
||||
});
|
||||
});
|
||||
|
||||
// --- setActionStatus — pure status transition (Sesjon 18: gates the apply-path) ---
|
||||
// The apply-path flips an operator-approved entry approved -> applied AFTER a
|
||||
// successful skills/ mutation. setActionStatus is the one pure primitive that
|
||||
// records that transition; it preserves every other field (targets, guardrail).
|
||||
|
||||
test('setActionStatus — transitions status, preserves targets+guardrail, is pure', () => {
|
||||
const led = recordAction(createLedger(), { ...MERGE, status: 'approved' });
|
||||
const key = actionKey(MERGE);
|
||||
const next = setActionStatus(led, key, 'applied', '2026-06-21');
|
||||
|
||||
assert.equal(led.actions[key].status, 'approved', 'input ledger untouched (pure)');
|
||||
assert.notEqual(next, led);
|
||||
assert.equal(next.actions[key].status, 'applied');
|
||||
assert.deepEqual(next.actions[key].targets, MERGE.targets, 'targets preserved');
|
||||
assert.deepEqual(next.actions[key].guardrail, MERGE.guardrail, 'guardrail preserved');
|
||||
});
|
||||
|
||||
test('setActionStatus — advances updated_at to the transition date', () => {
|
||||
const led = recordAction(createLedger(), { ...MERGE, status: 'approved' });
|
||||
const next = setActionStatus(led, actionKey(MERGE), 'applied', '2026-06-21');
|
||||
assert.equal(next.actions[actionKey(MERGE)].decided_at, '2026-06-21', 'entry date advances');
|
||||
assert.equal(next.updated_at, '2026-06-21');
|
||||
});
|
||||
|
||||
test('setActionStatus — without a date, keeps the entry decided_at and ledger updated_at', () => {
|
||||
let led = recordAction(createLedger(), { ...MERGE, status: 'approved' }); // decided_at 2026-06-20
|
||||
const next = setActionStatus(led, actionKey(MERGE), 'applied');
|
||||
assert.equal(next.actions[actionKey(MERGE)].decided_at, '2026-06-20', 'no date -> keep original');
|
||||
});
|
||||
|
||||
test('setActionStatus — throws on an unknown action key (cannot transition a non-entry)', () => {
|
||||
const led = recordAction(createLedger(), { ...MERGE, status: 'approved' });
|
||||
assert.throws(() => setActionStatus(led, 'sanitize_skill:ghost', 'applied'), /no action|ghost/i);
|
||||
});
|
||||
|
||||
test('setActionStatus — leaves the URL-keyed decisions map and sibling actions untouched', () => {
|
||||
let led = recordDecision(createLedger(), 'https://learn.microsoft.com/x', {
|
||||
status: 'approved',
|
||||
decided_at: '2026-06-19',
|
||||
});
|
||||
led = recordAction(led, { ...MERGE, status: 'approved' });
|
||||
led = recordAction(led, {
|
||||
operation_type: 'sanitize_skill',
|
||||
status: 'approved',
|
||||
decided_at: '2026-06-20',
|
||||
targets: { skill: 'ms-ai-governance' },
|
||||
guardrail: { ok: true },
|
||||
});
|
||||
const next = setActionStatus(led, actionKey(MERGE), 'applied', '2026-06-21');
|
||||
|
||||
assert.equal(isDecided(next, 'https://learn.microsoft.com/x'), true, 'URL decision survives');
|
||||
assert.equal(
|
||||
next.actions['sanitize_skill:ms-ai-governance'].status,
|
||||
'approved',
|
||||
'sibling action untouched',
|
||||
);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue