akm-cli 0.9.0-beta.3 → 0.9.0-beta.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +600 -0
- package/dist/assets/prompts/consolidate-system.md +23 -0
- package/dist/assets/prompts/contradiction-judge.md +33 -0
- package/dist/assets/prompts/distill-knowledge-system.md +22 -0
- package/dist/assets/prompts/distill-lesson-system.md +36 -0
- package/dist/assets/prompts/extract-session.md +5 -1
- package/dist/assets/prompts/graph-extract-system.md +1 -0
- package/dist/assets/prompts/memory-infer-system.md +1 -0
- package/dist/assets/prompts/memory-infer-user.md +5 -0
- package/dist/assets/prompts/metadata-enhance-system.md +1 -0
- package/dist/assets/prompts/procedural-system.md +44 -0
- package/dist/assets/prompts/recombine-system.md +40 -0
- package/dist/assets/prompts/staleness-detect-system.md +6 -0
- package/dist/assets/prompts/validate-summary-judge.md +1 -0
- package/dist/assets/templates/html/health.html +281 -111
- package/dist/cli.js +14 -3
- package/dist/commands/agent/contribute-cli.js +16 -3
- package/dist/commands/feedback-cli.js +15 -6
- package/dist/commands/graph/graph.js +75 -71
- package/dist/commands/health/checks.js +48 -0
- package/dist/commands/health/html-report.js +422 -80
- package/dist/commands/health.js +381 -9
- package/dist/commands/improve/calibration.js +161 -0
- package/dist/commands/improve/consolidate.js +634 -111
- package/dist/commands/improve/dedup.js +482 -0
- package/dist/commands/improve/distill.js +145 -69
- package/dist/commands/improve/encoding-salience.js +205 -0
- package/dist/commands/improve/extract-cli.js +115 -1
- package/dist/commands/improve/extract-prompt.js +33 -2
- package/dist/commands/improve/extract-watch.js +140 -0
- package/dist/commands/improve/extract.js +244 -35
- package/dist/commands/improve/feedback-valence.js +54 -0
- package/dist/commands/improve/homeostatic.js +467 -0
- package/dist/commands/improve/improve-auto-accept.js +113 -6
- package/dist/commands/improve/improve-profiles.js +12 -0
- package/dist/commands/improve/improve.js +1974 -614
- package/dist/commands/improve/memory/memory-contradiction-detect.js +23 -28
- package/dist/commands/improve/outcome-loop.js +256 -0
- package/dist/commands/improve/proactive-maintenance.js +87 -0
- package/dist/commands/improve/procedural.js +409 -0
- package/dist/commands/improve/recombine.js +528 -0
- package/dist/commands/improve/reflect.js +26 -1
- package/dist/commands/improve/related-sessions.js +120 -0
- package/dist/commands/improve/salience.js +386 -0
- package/dist/commands/improve/triage.js +95 -0
- package/dist/commands/lint/agent-linter.js +19 -24
- package/dist/commands/lint/base-linter.js +173 -60
- package/dist/commands/lint/command-linter.js +19 -24
- package/dist/commands/lint/env-key-rules.js +34 -1
- package/dist/commands/lint/fact-linter.js +39 -0
- package/dist/commands/lint/index.js +31 -13
- package/dist/commands/lint/memory-linter.js +1 -1
- package/dist/commands/lint/registry.js +7 -2
- package/dist/commands/lint/task-linter.js +3 -3
- package/dist/commands/lint/workflow-linter.js +26 -1
- package/dist/commands/proposal/proposal.js +5 -0
- package/dist/commands/proposal/validators/proposals.js +71 -54
- package/dist/commands/read/curate.js +344 -80
- package/dist/commands/read/search-cli.js +7 -0
- package/dist/commands/read/search.js +1 -0
- package/dist/commands/read/show.js +67 -2
- package/dist/commands/sources/installed-stashes.js +5 -1
- package/dist/commands/sources/stash-cli.js +10 -2
- package/dist/core/asset/asset-registry.js +2 -0
- package/dist/core/asset/asset-spec.js +14 -0
- package/dist/core/asset/frontmatter.js +166 -167
- package/dist/core/asset/markdown.js +8 -0
- package/dist/core/config/config-schema.js +259 -2
- package/dist/core/config/config.js +2 -2
- package/dist/core/logs-db.js +4 -3
- package/dist/core/paths.js +3 -0
- package/dist/core/state-db.js +649 -30
- package/dist/indexer/db/db.js +364 -38
- package/dist/indexer/db/graph-db.js +129 -86
- package/dist/indexer/ensure-index.js +152 -17
- package/dist/indexer/graph/graph-boost.js +51 -41
- package/dist/indexer/graph/graph-extraction.js +203 -3
- package/dist/indexer/index-writer-lock.js +99 -0
- package/dist/indexer/indexer.js +114 -111
- package/dist/indexer/passes/memory-inference.js +10 -3
- package/dist/indexer/passes/staleness-detect.js +2 -5
- package/dist/indexer/search/db-search.js +15 -4
- package/dist/indexer/search/ranking-contributors.js +22 -0
- package/dist/indexer/search/ranking.js +4 -0
- package/dist/indexer/walk/matchers.js +9 -0
- package/dist/integrations/agent/prompts.js +1 -0
- package/dist/integrations/harnesses/claude/session-log.js +11 -1
- package/dist/integrations/harnesses/opencode/session-log.js +9 -0
- package/dist/integrations/session-logs/index.js +16 -0
- package/dist/llm/client.js +23 -4
- package/dist/llm/embedder.js +27 -3
- package/dist/llm/embedders/local.js +66 -2
- package/dist/llm/graph-extract.js +2 -1
- package/dist/llm/memory-infer.js +4 -8
- package/dist/llm/metadata-enhance.js +9 -1
- package/dist/output/renderers.js +73 -1
- package/dist/output/shapes/curate.js +14 -2
- package/dist/output/text/helpers.js +9 -0
- package/dist/runtime.js +25 -1
- package/dist/scripts/migrate-storage.js +1242 -594
- package/dist/scripts/migrations/import-fs-improve-runs-to-db.js +473 -270
- package/dist/sources/providers/tar-utils.js +16 -8
- package/dist/storage/sqlite-pragmas.js +146 -0
- package/dist/workflows/db.js +3 -4
- package/dist/workflows/validate-summary.js +2 -7
- package/docs/data-and-telemetry.md +1 -0
- package/package.json +9 -6
|
@@ -52,22 +52,31 @@
|
|
|
52
52
|
*/
|
|
53
53
|
import fs from "node:fs";
|
|
54
54
|
import path from "node:path";
|
|
55
|
+
import distillKnowledgeSystemPrompt from "../../assets/prompts/distill-knowledge-system.md" with { type: "text" };
|
|
56
|
+
import distillLessonSystemPrompt from "../../assets/prompts/distill-lesson-system.md" with { type: "text" };
|
|
55
57
|
import { parseAssetRef } from "../../core/asset/asset-ref.js";
|
|
56
58
|
import { assembleAssetFromString } from "../../core/asset/asset-serialize.js";
|
|
57
|
-
import { parseFrontmatter } from "../../core/asset/frontmatter.js";
|
|
59
|
+
import { parseFrontmatter, writeSalienceToFrontmatter } from "../../core/asset/frontmatter.js";
|
|
58
60
|
import { stripMarkdownFences } from "../../core/asset/markdown.js";
|
|
59
61
|
import { resolveStashDir, timestampForFilename } from "../../core/common.js";
|
|
60
62
|
import { getDefaultLlmConfig, loadConfig } from "../../core/config/config.js";
|
|
61
63
|
import { ConfigError, UsageError } from "../../core/errors.js";
|
|
62
64
|
import { appendEvent, readEvents } from "../../core/events.js";
|
|
63
65
|
import { lintLessonContent } from "../../core/lesson-lint.js";
|
|
66
|
+
import { getDbPath } from "../../core/paths.js";
|
|
67
|
+
import { openStateDatabase } from "../../core/state-db.js";
|
|
64
68
|
import { warnVerbose } from "../../core/warn.js";
|
|
69
|
+
import { closeDatabase, getAllEntries, openDatabase } from "../../indexer/db/db.js";
|
|
65
70
|
import { resolveAssetPath } from "../../indexer/walk/path-resolver.js";
|
|
66
71
|
import { chatCompletion, parseEmbeddedJsonResponse } from "../../llm/client.js";
|
|
67
72
|
import { isLlmFeatureEnabled, tryLlmFeature } from "../../llm/feature-gate.js";
|
|
68
73
|
import { createProposal, isProposalSkipped, listProposals, } from "../proposal/validators/proposals.js";
|
|
69
74
|
import { akmSearch } from "../read/search.js";
|
|
75
|
+
import { stripFrontmatterBody as stripBodyForFidelity } from "./dedup.js";
|
|
70
76
|
import { assessMemoryKnowledgePromotionCandidate, deriveKnowledgeRef } from "./distill-promotion-policy.js";
|
|
77
|
+
import { buildRefVocabulary, scoreEncodingSalience } from "./encoding-salience.js";
|
|
78
|
+
import { buildClsContext, checkDistillFidelity } from "./homeostatic.js";
|
|
79
|
+
import { computeSalience, upsertAssetSalience } from "./salience.js";
|
|
71
80
|
/**
|
|
72
81
|
* Asset-ref types that `akm distill` structurally refuses as inputs.
|
|
73
82
|
*
|
|
@@ -122,68 +131,8 @@ import { repairTruncatedDescription } from "../../core/text-truncation.js";
|
|
|
122
131
|
import { detectDoubleFrontmatter, isValidDescription, isValidWhenToUse, } from "../proposal/validators/proposal-quality-validators.js";
|
|
123
132
|
export { detectDoubleFrontmatter, isValidDescription, isValidWhenToUse };
|
|
124
133
|
// ── Prompt assembly ─────────────────────────────────────────────────────────
|
|
125
|
-
const LESSON_SYSTEM_PROMPT =
|
|
126
|
-
|
|
127
|
-
"Given an asset and recent feedback events about it, produce a single",
|
|
128
|
-
"concise *lesson* an agent should remember next time it works on this",
|
|
129
|
-
"asset's domain.",
|
|
130
|
-
"",
|
|
131
|
-
"YOUR RESPONSE MUST START EXACTLY WITH `---` ON THE VERY FIRST LINE.",
|
|
132
|
-
"DO NOT output any prose, explanation, or code fences before or after.",
|
|
133
|
-
"",
|
|
134
|
-
"Required output format — copy this structure exactly:",
|
|
135
|
-
"---",
|
|
136
|
-
"description: <one complete sentence (ending with `.`) summarising what the lesson teaches>",
|
|
137
|
-
"when_to_use: <one complete sentence describing the concrete trigger condition>",
|
|
138
|
-
"---",
|
|
139
|
-
"",
|
|
140
|
-
"<lesson body — plain markdown, 1–3 short paragraphs of practical guidance>",
|
|
141
|
-
"",
|
|
142
|
-
"## description field (MANDATORY)",
|
|
143
|
-
"- A single complete sentence in present tense, 80-200 chars, NO markdown.",
|
|
144
|
-
"- Self-contained: a reviewer must understand the lesson from this field alone.",
|
|
145
|
-
'- DO NOT start with "When ", "If ", or a connector word — that belongs in when_to_use.',
|
|
146
|
-
'- DO NOT copy a section heading ("Key takeaways", "For example", "Key pitfalls").',
|
|
147
|
-
"- DO NOT begin with a numbered list marker, code fence, or markdown heading.",
|
|
148
|
-
"",
|
|
149
|
-
'GOOD: "Always validate ref existence before promoting a memory to knowledge; missing refs surface as silent 404s during accept."',
|
|
150
|
-
'BAD: "Key pitfalls"',
|
|
151
|
-
'BAD: "When working with the akm CLI"',
|
|
152
|
-
'BAD: "For example, you might..."',
|
|
153
|
-
'BAD: "1. Check the file"',
|
|
154
|
-
"",
|
|
155
|
-
"RULES:",
|
|
156
|
-
"- `when_to_use` MUST be a complete sentence describing a concrete trigger. Never write `When working with <asset-name>` — that is circular and useless.",
|
|
157
|
-
"- `description` and `when_to_use` MUST differ from each other.",
|
|
158
|
-
"- The lesson body MUST be non-empty markdown prose. Do NOT restate `description:` or `when_to_use:` inside the body (no `**description:** ...` or `**when_to_use:** ...` lines — the frontmatter is the only place those keys belong).",
|
|
159
|
-
"- Do NOT emit a second `---` fence after the opening frontmatter — there are exactly two `---` lines in the output, both belonging to the single frontmatter block at the top.",
|
|
160
|
-
"- Do NOT reproduce the source asset verbatim — distil what a caller needs to know.",
|
|
161
|
-
"- Output ONLY the lesson file. No preamble, no code fences, no trailing prose.",
|
|
162
|
-
].join("\n");
|
|
163
|
-
const KNOWLEDGE_SYSTEM_PROMPT = [
|
|
164
|
-
"You are the akm `distill` distiller.",
|
|
165
|
-
"Given an asset and recent feedback events about it, produce a concise",
|
|
166
|
-
"*knowledge* markdown document capturing the durable, reusable facts.",
|
|
167
|
-
"Prefer stable guidance over narrative recap.",
|
|
168
|
-
"",
|
|
169
|
-
"YOUR RESPONSE MUST START EXACTLY WITH `---` ON THE VERY FIRST LINE.",
|
|
170
|
-
"DO NOT output any prose, explanation, or code fences before or after.",
|
|
171
|
-
"",
|
|
172
|
-
"Required output format:",
|
|
173
|
-
"---",
|
|
174
|
-
"description: <one-line summary of the knowledge asset>",
|
|
175
|
-
"tags: [<tag1>, <tag2>]",
|
|
176
|
-
"---",
|
|
177
|
-
"",
|
|
178
|
-
"# <Title>",
|
|
179
|
-
"",
|
|
180
|
-
"<body — structured markdown, durable facts only>",
|
|
181
|
-
"",
|
|
182
|
-
"RULES:",
|
|
183
|
-
"- `description` MUST be a non-empty single-line string.",
|
|
184
|
-
"- Include a meaningful markdown body with a `# Title` heading.",
|
|
185
|
-
"- Output ONLY the knowledge file. No preamble, no code fences, no trailing prose.",
|
|
186
|
-
].join("\n");
|
|
134
|
+
const LESSON_SYSTEM_PROMPT = distillLessonSystemPrompt;
|
|
135
|
+
const KNOWLEDGE_SYSTEM_PROMPT = distillKnowledgeSystemPrompt;
|
|
187
136
|
// ── Structured-output schemas (responseSchema lift) ─────────────────────────
|
|
188
137
|
//
|
|
189
138
|
// PR 1 of the asset-writers decision (see knowledge:projects/akm/
|
|
@@ -586,7 +535,7 @@ similarLessons) {
|
|
|
586
535
|
* @param reason - Human-readable rejection reason.
|
|
587
536
|
* @param extraMeta - Optional additional metadata for the event.
|
|
588
537
|
*/
|
|
589
|
-
function writeQualityRejection(stash, inputRef, lessonRef, content, score, reason, extraMeta = {}) {
|
|
538
|
+
function writeQualityRejection(stash, inputRef, lessonRef, content, score, reason, extraMeta = {}, eligibilitySource) {
|
|
590
539
|
// D-5 / #388: reviewNeeded flag selects "review_needed" vs "quality_rejected" outcome.
|
|
591
540
|
const outcome = extraMeta.reviewNeeded ? "review_needed" : "quality_rejected";
|
|
592
541
|
const rejectDir = path.join(stash, ".akm", "distill-rejected");
|
|
@@ -602,6 +551,9 @@ function writeQualityRejection(stash, inputRef, lessonRef, content, score, reaso
|
|
|
602
551
|
score,
|
|
603
552
|
reason,
|
|
604
553
|
...extraMeta,
|
|
554
|
+
// Attribution tagging: stamp the eligibility lane so distill_invoked can be
|
|
555
|
+
// sliced by lane downstream. See EligibilitySource.
|
|
556
|
+
...(eligibilitySource ? { eligibilitySource } : {}),
|
|
605
557
|
},
|
|
606
558
|
});
|
|
607
559
|
return {
|
|
@@ -629,6 +581,12 @@ export async function akmDistill(options) {
|
|
|
629
581
|
// Validate the ref shape up front so a typo never reaches the LLM.
|
|
630
582
|
const parsedInputRef = parseAssetRef(inputRef);
|
|
631
583
|
const targetKind = options.proposalKind ?? "lesson";
|
|
584
|
+
// Attribution tagging: spread into every distill_invoked event's metadata so
|
|
585
|
+
// the lane that selected this asset is recorded uniformly across all outcome
|
|
586
|
+
// branches. Empty object when no lane was supplied (direct `akm distill`).
|
|
587
|
+
const eligMeta = options.eligibilitySource
|
|
588
|
+
? { eligibilitySource: options.eligibilitySource }
|
|
589
|
+
: {};
|
|
632
590
|
// Recursive-distillation guard. Distill produces *lessons* from non-lesson
|
|
633
591
|
// sources (memory, skill, knowledge, etc.). Calling distill on an existing
|
|
634
592
|
// lesson would derive `lesson:lesson-<name>-lesson-lesson` (double `-lesson`
|
|
@@ -650,6 +608,7 @@ export async function akmDistill(options) {
|
|
|
650
608
|
lessonRef: skippedRef,
|
|
651
609
|
message: "distill refuses lesson inputs — lessons are the distilled form, not a source",
|
|
652
610
|
skipReason: "recursive_lesson_input",
|
|
611
|
+
...eligMeta,
|
|
653
612
|
},
|
|
654
613
|
});
|
|
655
614
|
return {
|
|
@@ -671,15 +630,78 @@ export async function akmDistill(options) {
|
|
|
671
630
|
// Best-effort load: when the asset is not yet indexed we still proceed —
|
|
672
631
|
// the LLM is asked to distil from "available signal" (feedback alone).
|
|
673
632
|
let assetContent = null;
|
|
633
|
+
let assetFilePath = null;
|
|
674
634
|
try {
|
|
675
635
|
const filePath = await lookup(inputRef);
|
|
676
636
|
if (filePath && fs.existsSync(filePath)) {
|
|
637
|
+
assetFilePath = filePath;
|
|
677
638
|
assetContent = fs.readFileSync(filePath, "utf8");
|
|
678
639
|
}
|
|
679
640
|
}
|
|
680
641
|
catch {
|
|
681
642
|
assetContent = null;
|
|
682
643
|
}
|
|
644
|
+
// ── #608: Encoding-time salience scoring ────────────────────────────────
|
|
645
|
+
// Score the source asset with the three-signal model (novelty × 0.40 +
|
|
646
|
+
// magnitude × 0.35 + predictionError × 0.25) and persist the result to:
|
|
647
|
+
// 1. The asset's frontmatter (human-readable mirror; idempotent delta gate).
|
|
648
|
+
// 2. state.db :: asset_salience (canonical; feeds improve's high-salience gate).
|
|
649
|
+
// Both writes are best-effort — a DB error never blocks distillation.
|
|
650
|
+
if (assetContent && assetFilePath) {
|
|
651
|
+
try {
|
|
652
|
+
const parsedRef = parseAssetRef(inputRef);
|
|
653
|
+
// Build bigram vocabulary from currently-indexed refs for novelty signal.
|
|
654
|
+
let existingRefVocabulary = new Set();
|
|
655
|
+
try {
|
|
656
|
+
const embCfg = config?.embedding;
|
|
657
|
+
const indexDb = openDatabase(getDbPath(), embCfg?.dimension ? { embeddingDim: embCfg.dimension } : undefined);
|
|
658
|
+
try {
|
|
659
|
+
const allRefs = getAllEntries(indexDb).map((e) => e.entryKey);
|
|
660
|
+
existingRefVocabulary = buildRefVocabulary(allRefs);
|
|
661
|
+
}
|
|
662
|
+
finally {
|
|
663
|
+
closeDatabase(indexDb);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
catch {
|
|
667
|
+
// Index not available — novelty defaults to type-floor.
|
|
668
|
+
}
|
|
669
|
+
const salienceResult = scoreEncodingSalience({
|
|
670
|
+
body: assetContent,
|
|
671
|
+
type: parsedRef.type,
|
|
672
|
+
existingRefVocabulary,
|
|
673
|
+
revisionCount: 0,
|
|
674
|
+
});
|
|
675
|
+
// 1. Write salience to the source asset frontmatter (idempotent).
|
|
676
|
+
const updatedContent = writeSalienceToFrontmatter(assetContent, salienceResult.score, salienceResult);
|
|
677
|
+
if (updatedContent !== assetContent) {
|
|
678
|
+
fs.writeFileSync(assetFilePath, updatedContent, "utf8");
|
|
679
|
+
assetContent = updatedContent;
|
|
680
|
+
}
|
|
681
|
+
// 2. Persist encoding_salience to state.db.
|
|
682
|
+
try {
|
|
683
|
+
const stateDb = openStateDatabase();
|
|
684
|
+
try {
|
|
685
|
+
const vector = computeSalience({
|
|
686
|
+
ref: inputRef,
|
|
687
|
+
type: parsedRef.type,
|
|
688
|
+
retrievalFreq: 0,
|
|
689
|
+
encodingSalience: salienceResult.score,
|
|
690
|
+
});
|
|
691
|
+
upsertAssetSalience(stateDb, inputRef, vector);
|
|
692
|
+
}
|
|
693
|
+
finally {
|
|
694
|
+
stateDb.close();
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
catch {
|
|
698
|
+
// State DB unavailable — frontmatter mirror is the only persistence.
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
catch {
|
|
702
|
+
// Scoring errors never block distillation.
|
|
703
|
+
}
|
|
704
|
+
}
|
|
683
705
|
const { events } = readEventsImpl({
|
|
684
706
|
ref: inputRef,
|
|
685
707
|
type: "feedback",
|
|
@@ -766,6 +788,7 @@ export async function akmDistill(options) {
|
|
|
766
788
|
outcome: "skipped",
|
|
767
789
|
lessonRef: promotion.knowledgeRef,
|
|
768
790
|
message: "D-1: LLM resolved destination conflict as NOOP — existing content kept",
|
|
791
|
+
...eligMeta,
|
|
769
792
|
},
|
|
770
793
|
});
|
|
771
794
|
return {
|
|
@@ -814,9 +837,9 @@ export async function akmDistill(options) {
|
|
|
814
837
|
if (!judgeResult.pass) {
|
|
815
838
|
if (judgeResult.reviewNeeded) {
|
|
816
839
|
// Uncertainty band (2.5–3.5): queue as review_needed instead of rejecting.
|
|
817
|
-
return writeQualityRejection(stash, inputRef, promotion.knowledgeRef, resolvedPromotionContent, judgeResult.score, judgeResult.reason, { reviewNeeded: true });
|
|
840
|
+
return writeQualityRejection(stash, inputRef, promotion.knowledgeRef, resolvedPromotionContent, judgeResult.score, judgeResult.reason, { reviewNeeded: true }, options.eligibilitySource);
|
|
818
841
|
}
|
|
819
|
-
return writeQualityRejection(stash, inputRef, promotion.knowledgeRef, resolvedPromotionContent, judgeResult.score, judgeResult.reason);
|
|
842
|
+
return writeQualityRejection(stash, inputRef, promotion.knowledgeRef, resolvedPromotionContent, judgeResult.score, judgeResult.reason, {}, options.eligibilitySource);
|
|
820
843
|
}
|
|
821
844
|
// Normalize 1-5 judge score to [0, 1]. Score of -1 means pass-through
|
|
822
845
|
// (no LLM / timeout / parse failure) — leave confidence undefined so
|
|
@@ -834,6 +857,8 @@ export async function akmDistill(options) {
|
|
|
834
857
|
...(Object.keys(knowledgeParsed.data).length > 0 ? { frontmatter: knowledgeParsed.data } : {}),
|
|
835
858
|
},
|
|
836
859
|
...(knowledgeJudgeConfidence !== undefined ? { confidence: knowledgeJudgeConfidence } : {}),
|
|
860
|
+
// Attribution tagging: persist the eligibility lane on the proposal.
|
|
861
|
+
...(options.eligibilitySource ? { eligibilitySource: options.eligibilitySource } : {}),
|
|
837
862
|
}, options.ctx);
|
|
838
863
|
if (isProposalSkipped(proposalResult)) {
|
|
839
864
|
appendEvent({
|
|
@@ -844,6 +869,7 @@ export async function akmDistill(options) {
|
|
|
844
869
|
lessonRef: promotion.knowledgeRef,
|
|
845
870
|
message: proposalResult.message,
|
|
846
871
|
skipReason: proposalResult.reason,
|
|
872
|
+
...eligMeta,
|
|
847
873
|
},
|
|
848
874
|
});
|
|
849
875
|
return {
|
|
@@ -867,6 +893,7 @@ export async function akmDistill(options) {
|
|
|
867
893
|
proposalId: proposal.id,
|
|
868
894
|
...(options.sourceRun !== undefined ? { sourceRun: options.sourceRun } : {}),
|
|
869
895
|
...(exclusionSet.size > 0 ? { filteredFeedbackCount } : {}),
|
|
896
|
+
...eligMeta,
|
|
870
897
|
},
|
|
871
898
|
});
|
|
872
899
|
return {
|
|
@@ -894,13 +921,32 @@ export async function akmDistill(options) {
|
|
|
894
921
|
reason: p.review?.reason ?? "no reason given",
|
|
895
922
|
contentPreview: p.payload.content.slice(0, 500),
|
|
896
923
|
}));
|
|
897
|
-
|
|
924
|
+
// WS-3b CLS interleaving (step 9).
|
|
925
|
+
// When cls.enabled, inject embedding-retrieved adjacent lessons/knowledge
|
|
926
|
+
// into the distill prompt so the LLM avoids overwriting prior generalizations
|
|
927
|
+
// (catastrophic interference). DEFAULT OFF.
|
|
928
|
+
const clsConfig = config.profiles?.improve?.default?.processes?.distill?.cls ?? {};
|
|
929
|
+
let clsContext = "";
|
|
930
|
+
if (clsConfig.enabled) {
|
|
931
|
+
try {
|
|
932
|
+
const adjacentCount = clsConfig.adjacentCount ?? 3;
|
|
933
|
+
// Use the asset content or input ref as the query for adjacent retrieval.
|
|
934
|
+
const clsQuery = assetContent ? assetContent.slice(0, 500) : inputRef;
|
|
935
|
+
const adjacentItems = await fetchSimilarLessonsFn(clsQuery, adjacentCount);
|
|
936
|
+
clsContext = buildClsContext(adjacentItems, clsConfig);
|
|
937
|
+
}
|
|
938
|
+
catch {
|
|
939
|
+
// Fail open — CLS is supplemental, never required.
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
const baseUserPrompt = buildDistillPrompt({
|
|
898
943
|
inputRef,
|
|
899
944
|
assetContent,
|
|
900
945
|
feedback,
|
|
901
946
|
proposalKind: effectiveProposalKind,
|
|
902
947
|
...(rejectedForRef.length > 0 ? { rejectedProposals: rejectedForRef } : {}),
|
|
903
948
|
});
|
|
949
|
+
const userPrompt = clsContext ? `${baseUserPrompt}${clsContext}` : baseUserPrompt;
|
|
904
950
|
const messages = [
|
|
905
951
|
{ role: "system", content: effectiveProposalKind === "knowledge" ? KNOWLEDGE_SYSTEM_PROMPT : LESSON_SYSTEM_PROMPT },
|
|
906
952
|
{ role: "user", content: userPrompt },
|
|
@@ -979,6 +1025,7 @@ export async function akmDistill(options) {
|
|
|
979
1025
|
lessonRef: effectiveLessonRef,
|
|
980
1026
|
proposalKind: effectiveProposalKind,
|
|
981
1027
|
...(exclusionSet.size > 0 ? { filteredFeedbackCount } : {}),
|
|
1028
|
+
...eligMeta,
|
|
982
1029
|
},
|
|
983
1030
|
});
|
|
984
1031
|
return {
|
|
@@ -1203,6 +1250,7 @@ export async function akmDistill(options) {
|
|
|
1203
1250
|
proposalKind: effectiveProposalKind,
|
|
1204
1251
|
findingKinds: findings.map((f) => f.kind),
|
|
1205
1252
|
...(exclusionSet.size > 0 ? { filteredFeedbackCount } : {}),
|
|
1253
|
+
...eligMeta,
|
|
1206
1254
|
},
|
|
1207
1255
|
});
|
|
1208
1256
|
const message = findings.map((f) => f.message).join("\n");
|
|
@@ -1224,9 +1272,9 @@ export async function akmDistill(options) {
|
|
|
1224
1272
|
return writeQualityRejection(stash, inputRef, effectiveLessonRef, content, judgeResult.score, judgeResult.reason, {
|
|
1225
1273
|
reviewNeeded: true,
|
|
1226
1274
|
...(exclusionSet.size > 0 ? { filteredFeedbackCount, feedbackFullyFiltered } : {}),
|
|
1227
|
-
});
|
|
1275
|
+
}, options.eligibilitySource);
|
|
1228
1276
|
}
|
|
1229
|
-
return writeQualityRejection(stash, inputRef, effectiveLessonRef, content, judgeResult.score, judgeResult.reason, exclusionSet.size > 0 ? { filteredFeedbackCount, feedbackFullyFiltered } : {});
|
|
1277
|
+
return writeQualityRejection(stash, inputRef, effectiveLessonRef, content, judgeResult.score, judgeResult.reason, exclusionSet.size > 0 ? { filteredFeedbackCount, feedbackFullyFiltered } : {}, options.eligibilitySource);
|
|
1230
1278
|
}
|
|
1231
1279
|
// Normalize 1-5 judge score to [0, 1]. Score of -1 means pass-through
|
|
1232
1280
|
// (no LLM / timeout / parse failure) — leave confidence undefined so
|
|
@@ -1234,6 +1282,30 @@ export async function akmDistill(options) {
|
|
|
1234
1282
|
if (judgeResult.score > 0)
|
|
1235
1283
|
lessonJudgeConfidence = judgeResult.score / 5;
|
|
1236
1284
|
}
|
|
1285
|
+
// WS-3b: Distill→source fidelity check (step 10).
|
|
1286
|
+
// When fidelityCheck.enabled, check the distill proposal against its cited
|
|
1287
|
+
// source memories. A contradiction flag routes to human review (not auto-accept).
|
|
1288
|
+
// DEFAULT OFF. Fail-open: any error is treated as no-contradiction.
|
|
1289
|
+
const fidelityConfig = config.profiles?.improve?.default?.processes?.distill?.fidelityCheck ?? {};
|
|
1290
|
+
if (fidelityConfig.enabled && assetContent) {
|
|
1291
|
+
try {
|
|
1292
|
+
const proposalBody = stripBodyForFidelity(content);
|
|
1293
|
+
const sourceBodies = [stripBodyForFidelity(assetContent)];
|
|
1294
|
+
const fidelityResult = checkDistillFidelity(proposalBody, sourceBodies, fidelityConfig);
|
|
1295
|
+
if (fidelityResult.contradictionDetected) {
|
|
1296
|
+
// Route to human review by writing a quality rejection with reviewNeeded=true.
|
|
1297
|
+
return writeQualityRejection(stash, inputRef, effectiveLessonRef, content, 2.0, // below auto-accept threshold, signals review needed
|
|
1298
|
+
fidelityResult.reason ?? "Proposal may contradict cited source memories.", {
|
|
1299
|
+
reviewNeeded: true,
|
|
1300
|
+
fidelityContradiction: true,
|
|
1301
|
+
...(exclusionSet.size > 0 ? { filteredFeedbackCount, feedbackFullyFiltered } : {}),
|
|
1302
|
+
}, options.eligibilitySource);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
catch {
|
|
1306
|
+
// Fail open — fidelity check is supplemental.
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1237
1309
|
// Round-trip the parsed frontmatter so the proposal carries it as a
|
|
1238
1310
|
// structured payload alongside the raw content (matches the shape used by
|
|
1239
1311
|
// other proposal sources).
|
|
@@ -1256,6 +1328,8 @@ export async function akmDistill(options) {
|
|
|
1256
1328
|
frontmatter: frontmatterWithSources,
|
|
1257
1329
|
},
|
|
1258
1330
|
...(lessonJudgeConfidence !== undefined ? { confidence: lessonJudgeConfidence } : {}),
|
|
1331
|
+
// Attribution tagging: persist the eligibility lane on the proposal.
|
|
1332
|
+
...(options.eligibilitySource ? { eligibilitySource: options.eligibilitySource } : {}),
|
|
1259
1333
|
}, options.ctx);
|
|
1260
1334
|
if (isProposalSkipped(proposalResult2)) {
|
|
1261
1335
|
appendEvent({
|
|
@@ -1266,6 +1340,7 @@ export async function akmDistill(options) {
|
|
|
1266
1340
|
lessonRef: effectiveLessonRef,
|
|
1267
1341
|
message: proposalResult2.message,
|
|
1268
1342
|
skipReason: proposalResult2.reason,
|
|
1343
|
+
...eligMeta,
|
|
1269
1344
|
},
|
|
1270
1345
|
});
|
|
1271
1346
|
return {
|
|
@@ -1290,6 +1365,7 @@ export async function akmDistill(options) {
|
|
|
1290
1365
|
...(options.sourceRun !== undefined ? { sourceRun: options.sourceRun } : {}),
|
|
1291
1366
|
...(exclusionSet.size > 0 ? { filteredFeedbackCount } : {}),
|
|
1292
1367
|
...(descriptionSwapped > 0 ? { descriptionSwapped } : {}),
|
|
1368
|
+
...eligMeta,
|
|
1293
1369
|
},
|
|
1294
1370
|
});
|
|
1295
1371
|
return {
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
4
|
+
/**
|
|
5
|
+
* Encoding-time salience scoring for issue #608.
|
|
6
|
+
*
|
|
7
|
+
* Pure module — no I/O. All inputs are pre-fetched by the caller.
|
|
8
|
+
* Implements the three-signal model: novelty × 0.40 + magnitude × 0.35 +
|
|
9
|
+
* predictionError × 0.25, clamped to [0, 1].
|
|
10
|
+
*/
|
|
11
|
+
// ── Weights ────────────────────────────────────────────────────────────────────
|
|
12
|
+
const W_NOVELTY = 0.4;
|
|
13
|
+
const W_MAGNITUDE = 0.35;
|
|
14
|
+
const W_PREDICTION_ERROR = 0.25;
|
|
15
|
+
// Guard: weights must sum to 1.0 (consistent with the pattern in salience.ts).
|
|
16
|
+
// This check fires at module load and catches any future re-tuning mistakes.
|
|
17
|
+
if (Math.abs(W_NOVELTY + W_MAGNITUDE + W_PREDICTION_ERROR - 1.0) > 1e-9) {
|
|
18
|
+
throw new Error(`encoding-salience.ts: sub-signal weights must sum to 1.0 (got ${W_NOVELTY + W_MAGNITUDE + W_PREDICTION_ERROR})`);
|
|
19
|
+
}
|
|
20
|
+
// ── Novelty type floors ────────────────────────────────────────────────────────
|
|
21
|
+
const TYPE_NOVELTY_FLOOR = Object.freeze({
|
|
22
|
+
skill: 0.8,
|
|
23
|
+
agent: 0.8,
|
|
24
|
+
memory: 0.4,
|
|
25
|
+
});
|
|
26
|
+
const DEFAULT_NOVELTY_FLOOR = 0.5;
|
|
27
|
+
// ── Magnitude keyword sets ──────────────────────────────────────────────────────
|
|
28
|
+
const SEVERITY_BUCKET = new Set(["error", "critical", "warning", "incident", "regression"]);
|
|
29
|
+
// Note: "fails" and "urgent" are from the spec's initial keyword list and are included
|
|
30
|
+
// here as strong constraint signals, even though the spec's final bucket table omits them.
|
|
31
|
+
const CONSTRAINT_BUCKET = new Set(["must", "never", "always", "blocked", "breaking", "deprecated", "fails", "urgent"]);
|
|
32
|
+
const ALL_MAGNITUDE_KEYWORDS = new Set([...SEVERITY_BUCKET, ...CONSTRAINT_BUCKET]);
|
|
33
|
+
// Number of distinct keyword matches to reach full magnitude score.
|
|
34
|
+
const MAGNITUDE_FULL_SCORE_THRESHOLD = 4;
|
|
35
|
+
// Single-bucket magnitude cap: when only one semantic bucket matches.
|
|
36
|
+
const MAGNITUDE_SINGLE_BUCKET_CAP = 0.5;
|
|
37
|
+
// ── Stop-words (excluded from bigram tokenization) ────────────────────────────
|
|
38
|
+
const STOP_WORDS = new Set([
|
|
39
|
+
"a",
|
|
40
|
+
"an",
|
|
41
|
+
"the",
|
|
42
|
+
"and",
|
|
43
|
+
"or",
|
|
44
|
+
"but",
|
|
45
|
+
"in",
|
|
46
|
+
"on",
|
|
47
|
+
"at",
|
|
48
|
+
"to",
|
|
49
|
+
"for",
|
|
50
|
+
"of",
|
|
51
|
+
"with",
|
|
52
|
+
"by",
|
|
53
|
+
"from",
|
|
54
|
+
"as",
|
|
55
|
+
"is",
|
|
56
|
+
"it",
|
|
57
|
+
"its",
|
|
58
|
+
"be",
|
|
59
|
+
"are",
|
|
60
|
+
"was",
|
|
61
|
+
"were",
|
|
62
|
+
"has",
|
|
63
|
+
"have",
|
|
64
|
+
"had",
|
|
65
|
+
"do",
|
|
66
|
+
"does",
|
|
67
|
+
"did",
|
|
68
|
+
"not",
|
|
69
|
+
"this",
|
|
70
|
+
"that",
|
|
71
|
+
"these",
|
|
72
|
+
"those",
|
|
73
|
+
"i",
|
|
74
|
+
"we",
|
|
75
|
+
"you",
|
|
76
|
+
"he",
|
|
77
|
+
"she",
|
|
78
|
+
"they",
|
|
79
|
+
"my",
|
|
80
|
+
"our",
|
|
81
|
+
"your",
|
|
82
|
+
"his",
|
|
83
|
+
"her",
|
|
84
|
+
"their",
|
|
85
|
+
"can",
|
|
86
|
+
"will",
|
|
87
|
+
"would",
|
|
88
|
+
"should",
|
|
89
|
+
"could",
|
|
90
|
+
"may",
|
|
91
|
+
"might",
|
|
92
|
+
"shall",
|
|
93
|
+
"so",
|
|
94
|
+
"if",
|
|
95
|
+
"then",
|
|
96
|
+
"than",
|
|
97
|
+
"when",
|
|
98
|
+
"while",
|
|
99
|
+
"what",
|
|
100
|
+
"which",
|
|
101
|
+
"who",
|
|
102
|
+
"how",
|
|
103
|
+
"no",
|
|
104
|
+
"any",
|
|
105
|
+
"all",
|
|
106
|
+
"more",
|
|
107
|
+
"also",
|
|
108
|
+
]);
|
|
109
|
+
// ── Tokenization helpers ──────────────────────────────────────────────────────
|
|
110
|
+
/**
|
|
111
|
+
* Tokenize text into lowercase words, splitting on whitespace and punctuation,
|
|
112
|
+
* dropping stop-words. Used for body bigram extraction (novelty signal).
|
|
113
|
+
*/
|
|
114
|
+
function tokenizeBody(text) {
|
|
115
|
+
return text
|
|
116
|
+
.toLowerCase()
|
|
117
|
+
.split(/[\s\p{P}\p{S}\-_/]+/u)
|
|
118
|
+
.filter((t) => t.length > 1 && !STOP_WORDS.has(t));
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Tokenize a ref name or tag into lowercase words without stop-word filtering.
|
|
122
|
+
* Ref names are identifiers, not prose — all tokens are significant.
|
|
123
|
+
*/
|
|
124
|
+
function tokenizeRef(ref) {
|
|
125
|
+
return ref
|
|
126
|
+
.toLowerCase()
|
|
127
|
+
.split(/[\s\p{P}\p{S}\-_/]+/u)
|
|
128
|
+
.filter((t) => t.length > 0);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Generate all consecutive bigrams from an array of tokens.
|
|
132
|
+
* Returns them as "token1 token2" strings.
|
|
133
|
+
*/
|
|
134
|
+
function bigrams(tokens) {
|
|
135
|
+
const result = [];
|
|
136
|
+
for (let i = 0; i < tokens.length - 1; i++) {
|
|
137
|
+
result.push(`${tokens[i]} ${tokens[i + 1]}`);
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Build the existing-ref vocabulary: a Set of bigrams derived from tokenizing
|
|
143
|
+
* the provided ref names (and tags). Pass this to `scoreEncodingSalience` as
|
|
144
|
+
* `existingRefVocabulary` so the novelty signal can measure how much of the
|
|
145
|
+
* asset body is already represented in the stash vocabulary.
|
|
146
|
+
*/
|
|
147
|
+
export function buildRefVocabulary(refs) {
|
|
148
|
+
const vocab = new Set();
|
|
149
|
+
for (const ref of refs) {
|
|
150
|
+
const tokens = tokenizeRef(ref);
|
|
151
|
+
for (const bg of bigrams(tokens)) {
|
|
152
|
+
vocab.add(bg);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return vocab;
|
|
156
|
+
}
|
|
157
|
+
// ── Sub-signal computations ────────────────────────────────────────────────────
|
|
158
|
+
function computeNovelty(body, type, vocab) {
|
|
159
|
+
const tokens = tokenizeBody(body);
|
|
160
|
+
const bgs = bigrams(tokens);
|
|
161
|
+
const floor = TYPE_NOVELTY_FLOOR[type] ?? DEFAULT_NOVELTY_FLOOR;
|
|
162
|
+
if (bgs.length === 0)
|
|
163
|
+
return floor;
|
|
164
|
+
const novelCount = bgs.filter((bg) => !vocab.has(bg)).length;
|
|
165
|
+
const bigramNoveltyFraction = novelCount / bgs.length;
|
|
166
|
+
return Math.max(floor, bigramNoveltyFraction);
|
|
167
|
+
}
|
|
168
|
+
function computeMagnitude(body) {
|
|
169
|
+
const lowerBody = body.toLowerCase();
|
|
170
|
+
// Split on word boundaries to avoid partial matches (e.g. "errors" matching "error").
|
|
171
|
+
const words = new Set(lowerBody.split(/\W+/).filter((w) => w.length > 0));
|
|
172
|
+
const matched = [...ALL_MAGNITUDE_KEYWORDS].filter((kw) => words.has(kw));
|
|
173
|
+
if (matched.length === 0)
|
|
174
|
+
return 0;
|
|
175
|
+
const distinctCount = matched.length;
|
|
176
|
+
const rawMagnitude = Math.min(1.0, distinctCount / MAGNITUDE_FULL_SCORE_THRESHOLD);
|
|
177
|
+
// Require at least one match from each bucket to lift the single-bucket cap.
|
|
178
|
+
const hasSeverity = matched.some((kw) => SEVERITY_BUCKET.has(kw));
|
|
179
|
+
const hasConstraint = matched.some((kw) => CONSTRAINT_BUCKET.has(kw));
|
|
180
|
+
if (hasSeverity && hasConstraint)
|
|
181
|
+
return rawMagnitude;
|
|
182
|
+
return Math.min(rawMagnitude, MAGNITUDE_SINGLE_BUCKET_CAP);
|
|
183
|
+
}
|
|
184
|
+
function computePredictionError(revisionCount) {
|
|
185
|
+
if (revisionCount === 0)
|
|
186
|
+
return 1.0;
|
|
187
|
+
return 1 / (1 + Math.log(1 + revisionCount));
|
|
188
|
+
}
|
|
189
|
+
// ── Main export ────────────────────────────────────────────────────────────────
|
|
190
|
+
/**
|
|
191
|
+
* Compute the encoding-time salience score for an asset.
|
|
192
|
+
*
|
|
193
|
+
* Three sub-signals:
|
|
194
|
+
* novelty (0.40) — how much of the body is absent from the stash vocabulary.
|
|
195
|
+
* magnitude (0.35) — presence of severity/constraint keywords.
|
|
196
|
+
* predictionError (0.25) — surprise: high for new assets, decays with revisions.
|
|
197
|
+
*/
|
|
198
|
+
export function scoreEncodingSalience(inputs) {
|
|
199
|
+
const novelty = computeNovelty(inputs.body, inputs.type, inputs.existingRefVocabulary);
|
|
200
|
+
const magnitude = computeMagnitude(inputs.body);
|
|
201
|
+
const predictionError = computePredictionError(inputs.revisionCount);
|
|
202
|
+
const raw = W_NOVELTY * novelty + W_MAGNITUDE * magnitude + W_PREDICTION_ERROR * predictionError;
|
|
203
|
+
const score = Math.min(1, Math.max(0, raw));
|
|
204
|
+
return { score, novelty, magnitude, predictionError };
|
|
205
|
+
}
|