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 },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue