UNPKG

akm-cli

Version:

akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur

66 lines (65 loc) • 3.05 kB
// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. /** * Distill guards: the related lessons, knowledge notes and skills shown to the * writer so it does not repeat or overwrite them (CLS context), and a cheap check * that a proposal does not contradict the memories it came from. */ export const DEFAULT_CLS_ADJACENT_COUNT = 3; /** The CLS prompt section (each entry capped at 600 chars); empty when disabled (on unless `enabled: false`) or nothing is related. */ export function buildClsContext(adjacentItems, config) { if (config.enabled === false || adjacentItems.length === 0) return ""; const lines = [ "", "## Related assets already in the library", "The library already holds these lessons, knowledge notes and skills near this memory. They may be about another subject.", "If one of them already states the rule the memory would give, answer NONE. Do not contradict or overwrite them.", "", ]; for (const item of adjacentItems) lines.push(`### ${item.ref}`, item.content.trim().slice(0, 600), ""); return lines.join("\n"); } /** * Flag a proposal whose "always/must X" (or "never/must not X") claim meets * the opposite claim about X in a source. Deliberately conservative: a flag * only costs a human review, while a model call per proposal is expensive. */ export function checkDistillFidelity(proposalBody, sourceBodies, config) { if (!config.enabled || sourceBodies.length === 0) return { contradictionDetected: false }; const strongClaims = extractStrongClaims(proposalBody.toLowerCase()); for (const sourceBody of sourceBodies) { const sourceLow = sourceBody.toLowerCase(); for (const { polarity, term } of strongClaims) { if (hasStrongClaim(sourceLow, term, polarity === "positive" ? "negative" : "positive")) { return { contradictionDetected: true, reason: `Proposal makes a ${polarity} strong claim about "${term}" that conflicts with an opposing claim in a cited source. Route to human review.`, }; } } } return { contradictionDetected: false }; } const CLAIM_PATTERNS = [ { polarity: "positive", re: /\b(?:always|must)\s+(\w+)/g }, { polarity: "negative", re: /\b(?:never|must\s+not|should\s+not)\s+(\w+)/g }, ]; function extractStrongClaims(text) { const claims = []; for (const { polarity, re } of CLAIM_PATTERNS) { for (const m of text.matchAll(re)) { const term = m[1]; if (term && term.length > 2) claims.push({ polarity, term }); } } return claims; } function hasStrongClaim(text, term, polarity) { const marker = polarity === "positive" ? /\b(?:always|must)\s/ : /\b(?:never|must\s+not|should\s+not)\s/; return marker.test(text) && text.includes(term); }