@joshuaswarren/openclaw-engram 9.0.92 → 9.0.93

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.
@@ -0,0 +1,236 @@
1
+ // openclaw-engram: Local-first memory plugin
2
+ import {
3
+ FallbackLlmClient
4
+ } from "./chunk-PW5V7OJH.js";
5
+ import {
6
+ listJsonFiles
7
+ } from "./chunk-DEIBZP3O.js";
8
+ import {
9
+ log
10
+ } from "./chunk-SSIIJJKA.js";
11
+
12
+ // src/calibration.ts
13
+ import { createHash } from "crypto";
14
+ import path from "path";
15
+ import { mkdir, readFile, writeFile } from "fs/promises";
16
+ function calibrationDir(memoryDir) {
17
+ return path.join(memoryDir, "state", "calibration");
18
+ }
19
+ function calibrationIndexPath(memoryDir) {
20
+ return path.join(calibrationDir(memoryDir), "calibration-index.json");
21
+ }
22
+ async function readCalibrationIndex(memoryDir) {
23
+ try {
24
+ const raw = JSON.parse(await readFile(calibrationIndexPath(memoryDir), "utf8"));
25
+ return {
26
+ rules: Array.isArray(raw.rules) ? raw.rules : [],
27
+ updatedAt: typeof raw.updatedAt === "string" ? raw.updatedAt : (/* @__PURE__ */ new Date()).toISOString(),
28
+ totalCorrectionsAnalyzed: typeof raw.totalCorrectionsAnalyzed === "number" ? raw.totalCorrectionsAnalyzed : 0
29
+ };
30
+ } catch {
31
+ return { rules: [], updatedAt: (/* @__PURE__ */ new Date()).toISOString(), totalCorrectionsAnalyzed: 0 };
32
+ }
33
+ }
34
+ async function writeCalibrationIndex(memoryDir, index) {
35
+ const dir = calibrationDir(memoryDir);
36
+ await mkdir(dir, { recursive: true });
37
+ index.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
38
+ await writeFile(calibrationIndexPath(memoryDir), JSON.stringify(index, null, 2), "utf8");
39
+ }
40
+ async function readCorrections(memoryDir) {
41
+ const correctionsDir = path.join(memoryDir, "corrections");
42
+ const files = await listJsonFiles(correctionsDir).catch(() => {
43
+ return [];
44
+ });
45
+ const factsDir = path.join(memoryDir, "facts");
46
+ try {
47
+ const { readdir } = await import("fs/promises");
48
+ const dayDirs = (await readdir(factsDir)).filter((d) => /^\d{4}-\d{2}-\d{2}$/.test(d));
49
+ for (const day of dayDirs) {
50
+ const dayPath = path.join(factsDir, day);
51
+ const dayFiles = (await readdir(dayPath)).filter((f) => f.startsWith("correction-") && f.endsWith(".md")).map((f) => path.join(dayPath, f));
52
+ files.push(...dayFiles);
53
+ }
54
+ } catch {
55
+ }
56
+ try {
57
+ const { readdir } = await import("fs/promises");
58
+ const corrFiles = (await readdir(correctionsDir)).filter((f) => f.endsWith(".md")).map((f) => path.join(correctionsDir, f));
59
+ files.push(...corrFiles);
60
+ } catch {
61
+ }
62
+ const corrections = [];
63
+ const seen = /* @__PURE__ */ new Set();
64
+ for (const filePath of files) {
65
+ try {
66
+ const raw = await readFile(filePath, "utf8");
67
+ const fmMatch = raw.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
68
+ if (!fmMatch) continue;
69
+ const content = fmMatch[2].trim();
70
+ if (!content || content.length < 10) continue;
71
+ const idMatch = fmMatch[1].match(/^id:\s*(.+)$/m);
72
+ const id = idMatch?.[1]?.trim() ?? path.basename(filePath, ".md");
73
+ if (seen.has(id)) continue;
74
+ seen.add(id);
75
+ const confMatch = fmMatch[1].match(/^confidence:\s*(.+)$/m);
76
+ const confidence = confMatch ? parseFloat(confMatch[1]) : 0.9;
77
+ const entityMatch = fmMatch[1].match(/^entityRef:\s*(.+)$/m);
78
+ const entityRefs = entityMatch ? [entityMatch[1].trim()] : [];
79
+ corrections.push({ id, content, created: "", confidence, entityRefs, tags: [] });
80
+ } catch {
81
+ }
82
+ }
83
+ return corrections;
84
+ }
85
+ var CLUSTER_PROMPT = `You are analyzing user corrections to an AI assistant. Each correction represents a moment where the assistant's prediction of what the user wanted was WRONG.
86
+
87
+ Your job: Group these corrections into clusters where the SAME TYPE of misunderstanding is happening. Then for each cluster, synthesize a CalibrationRule.
88
+
89
+ A CalibrationRule describes:
90
+ - condition: When does this type of mistake happen?
91
+ - modelTendency: What does the model tend to assume or do wrong?
92
+ - userExpectation: What does the user actually want instead?
93
+ - calibration: How should the model adjust its behavior?
94
+ - ruleType: One of "model_tendency", "user_expectation", "scope_boundary", "verification_required"
95
+
96
+ Focus on PATTERNS, not individual corrections. A cluster needs at least 2 corrections to be worth a rule.
97
+
98
+ Output valid JSON only:
99
+ {
100
+ "rules": [
101
+ {
102
+ "ruleType": "model_tendency",
103
+ "condition": "When discussing project scope or task boundaries",
104
+ "modelTendency": "The model tends to assume broader scope than the user intends",
105
+ "userExpectation": "The user prefers narrow, specific task definitions and wants to be asked before scope expansion",
106
+ "calibration": "When uncertain about scope, ask for clarification rather than assuming. Default to the narrower interpretation.",
107
+ "confidence": 0.85,
108
+ "evidenceIds": ["correction-id-1", "correction-id-2"]
109
+ }
110
+ ]
111
+ }`;
112
+ async function synthesizeCalibrationRules(corrections, llm, existingRules) {
113
+ if (corrections.length < 2) return [];
114
+ const correctionText = corrections.slice(0, 50).map((c, i) => `[${c.id}] ${c.content}`).join("\n\n");
115
+ const existingRulesText = existingRules.length > 0 ? `
116
+
117
+ Existing calibration rules (avoid duplicating these):
118
+ ${existingRules.map((r) => `- ${r.condition}: ${r.calibration}`).join("\n")}` : "";
119
+ const response = await llm.chatCompletion(
120
+ [
121
+ { role: "system", content: CLUSTER_PROMPT },
122
+ { role: "user", content: `Here are ${corrections.length} corrections from this user:
123
+
124
+ ${correctionText}${existingRulesText}` }
125
+ ],
126
+ { temperature: 0.3, maxTokens: 3e3 }
127
+ );
128
+ if (!response?.content) return [];
129
+ try {
130
+ let jsonStr = response.content.trim();
131
+ const fenceMatch = jsonStr.match(/```(?:json)?\s*\n?([\s\S]*?)\n?\s*```/);
132
+ if (fenceMatch) jsonStr = fenceMatch[1];
133
+ const parsed = JSON.parse(jsonStr);
134
+ if (!Array.isArray(parsed.rules)) return [];
135
+ const now = (/* @__PURE__ */ new Date()).toISOString();
136
+ return parsed.rules.filter((r) => r.condition && r.calibration && r.modelTendency).map((r) => ({
137
+ id: `cal-${createHash("sha256").update(r.condition + r.calibration).digest("hex").slice(0, 12)}`,
138
+ ruleType: r.ruleType ?? "model_tendency",
139
+ condition: String(r.condition),
140
+ modelTendency: String(r.modelTendency),
141
+ userExpectation: String(r.userExpectation ?? ""),
142
+ calibration: String(r.calibration),
143
+ confidence: typeof r.confidence === "number" ? r.confidence : 0.7,
144
+ evidenceCount: Array.isArray(r.evidenceIds) ? r.evidenceIds.length : 1,
145
+ evidenceCorrectionIds: Array.isArray(r.evidenceIds) ? r.evidenceIds : [],
146
+ createdAt: now,
147
+ lastReinforcedAt: now
148
+ }));
149
+ } catch {
150
+ log.warn("[calibration] failed to parse LLM response");
151
+ return [];
152
+ }
153
+ }
154
+ function buildCalibrationRecallSection(rules, query, maxChars = 1200) {
155
+ if (rules.length === 0) return null;
156
+ const lines = [
157
+ "## Model Calibration (learned from past corrections)",
158
+ "",
159
+ "Adjustments for this specific user, learned from patterns in their corrections:",
160
+ ""
161
+ ];
162
+ let totalChars = lines.join("\n").length;
163
+ for (const rule of rules) {
164
+ const line = `- **${rule.condition}**: ${rule.modelTendency} \u2192 Instead: ${rule.calibration}`;
165
+ if (totalChars + line.length + 1 > maxChars) break;
166
+ lines.push(line);
167
+ totalChars += line.length + 1;
168
+ }
169
+ if (lines.length <= 4) return null;
170
+ lines.push("");
171
+ return lines.join("\n");
172
+ }
173
+ async function runCalibrationConsolidation(options) {
174
+ try {
175
+ const llm = new FallbackLlmClient(options.gatewayConfig);
176
+ if (!llm.isAvailable()) {
177
+ log.debug("[calibration] no LLM available \u2014 skipping consolidation");
178
+ return [];
179
+ }
180
+ const corrections = await readCorrections(options.memoryDir);
181
+ if (corrections.length < 3) {
182
+ log.debug(`[calibration] only ${corrections.length} corrections \u2014 need at least 3`);
183
+ return [];
184
+ }
185
+ const existingIndex = await readCalibrationIndex(options.memoryDir);
186
+ const newRules = await synthesizeCalibrationRules(corrections, llm, existingIndex.rules);
187
+ if (newRules.length === 0) {
188
+ log.debug("[calibration] no new calibration rules synthesized");
189
+ return existingIndex.rules;
190
+ }
191
+ const ruleMap = new Map(existingIndex.rules.map((r) => [r.id, r]));
192
+ for (const rule of newRules) {
193
+ if (ruleMap.has(rule.id)) {
194
+ const existing = ruleMap.get(rule.id);
195
+ existing.lastReinforcedAt = (/* @__PURE__ */ new Date()).toISOString();
196
+ existing.evidenceCount += rule.evidenceCount;
197
+ existing.confidence = Math.min(1, existing.confidence + 0.05);
198
+ } else {
199
+ ruleMap.set(rule.id, rule);
200
+ }
201
+ }
202
+ const allRules = [...ruleMap.values()];
203
+ await writeCalibrationIndex(options.memoryDir, {
204
+ rules: allRules,
205
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
206
+ totalCorrectionsAnalyzed: corrections.length
207
+ });
208
+ log.debug(`[calibration] synthesized ${newRules.length} new rule(s), ${allRules.length} total`);
209
+ return allRules;
210
+ } catch (error) {
211
+ log.warn(`[calibration] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);
212
+ return [];
213
+ }
214
+ }
215
+ async function runCalibrationIfEnabled(options) {
216
+ if (!options.calibrationEnabled) {
217
+ return [];
218
+ }
219
+ return runCalibrationConsolidation({
220
+ memoryDir: options.memoryDir,
221
+ gatewayConfig: options.gatewayConfig
222
+ });
223
+ }
224
+ async function getCalibrationRulesForRecall(memoryDir) {
225
+ const index = await readCalibrationIndex(memoryDir);
226
+ return index.rules;
227
+ }
228
+ export {
229
+ buildCalibrationRecallSection,
230
+ getCalibrationRulesForRecall,
231
+ readCalibrationIndex,
232
+ runCalibrationConsolidation,
233
+ runCalibrationIfEnabled,
234
+ synthesizeCalibrationRules
235
+ };
236
+ //# sourceMappingURL=calibration-TLGCHI7D.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/calibration.ts"],"sourcesContent":["/**\n * calibration.ts — Prediction-Error-Driven Model-User Calibration\n *\n * Analyzes patterns in user corrections to identify systematic miscalibration\n * between the model's predictions and the user's actual expectations.\n * During consolidation, replays chains of similar corrections through an LLM\n * to synthesize CalibrationRules that adjust model behavior for this specific user.\n *\n * Inspired by:\n * - Cerebellar motor calibration (prediction errors drive lasting adjustments)\n * - Temporal difference learning (dopamine signals prediction error)\n * - Tesla FSD shadow mode (divergence between prediction and reality = training signal)\n */\n\nimport { createHash } from \"node:crypto\";\nimport path from \"node:path\";\nimport { mkdir, readFile, writeFile } from \"node:fs/promises\";\nimport { FallbackLlmClient } from \"./fallback-llm.js\";\nimport type { GatewayConfig, MemoryFile } from \"./types.js\";\nimport { listJsonFiles, readJsonFile } from \"./json-store.js\";\nimport { isRecord } from \"./store-contract.js\";\nimport { log } from \"./logger.js\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface CalibrationRule {\n id: string;\n ruleType: \"model_tendency\" | \"user_expectation\" | \"scope_boundary\" | \"verification_required\";\n condition: string;\n modelTendency: string;\n userExpectation: string;\n calibration: string;\n confidence: number;\n evidenceCount: number;\n evidenceCorrectionIds: string[];\n createdAt: string;\n lastReinforcedAt: string;\n}\n\nexport interface CalibrationIndex {\n rules: CalibrationRule[];\n updatedAt: string;\n totalCorrectionsAnalyzed: number;\n}\n\n// ─── Storage ─────────────────────────────────────────────────────────────────\n\nfunction calibrationDir(memoryDir: string): string {\n return path.join(memoryDir, \"state\", \"calibration\");\n}\n\nfunction calibrationIndexPath(memoryDir: string): string {\n return path.join(calibrationDir(memoryDir), \"calibration-index.json\");\n}\n\nexport async function readCalibrationIndex(memoryDir: string): Promise<CalibrationIndex> {\n try {\n const raw = JSON.parse(await readFile(calibrationIndexPath(memoryDir), \"utf8\"));\n return {\n rules: Array.isArray(raw.rules) ? raw.rules : [],\n updatedAt: typeof raw.updatedAt === \"string\" ? raw.updatedAt : new Date().toISOString(),\n totalCorrectionsAnalyzed: typeof raw.totalCorrectionsAnalyzed === \"number\" ? raw.totalCorrectionsAnalyzed : 0,\n };\n } catch {\n return { rules: [], updatedAt: new Date().toISOString(), totalCorrectionsAnalyzed: 0 };\n }\n}\n\nasync function writeCalibrationIndex(memoryDir: string, index: CalibrationIndex): Promise<void> {\n const dir = calibrationDir(memoryDir);\n await mkdir(dir, { recursive: true });\n index.updatedAt = new Date().toISOString();\n await writeFile(calibrationIndexPath(memoryDir), JSON.stringify(index, null, 2), \"utf8\");\n}\n\n// ─── Correction Reading ──────────────────────────────────────────────────────\n\ninterface CorrectionMemory {\n id: string;\n content: string;\n created: string;\n confidence: number;\n entityRefs: string[];\n tags: string[];\n}\n\nasync function readCorrections(memoryDir: string): Promise<CorrectionMemory[]> {\n const correctionsDir = path.join(memoryDir, \"corrections\");\n const files = await listJsonFiles(correctionsDir).catch(() => {\n // Corrections might be in facts/ directories too\n return [] as string[];\n });\n\n // Also scan facts directories for correction-category files\n const factsDir = path.join(memoryDir, \"facts\");\n try {\n const { readdir } = await import(\"node:fs/promises\");\n const dayDirs = (await readdir(factsDir)).filter((d: string) => /^\\d{4}-\\d{2}-\\d{2}$/.test(d));\n for (const day of dayDirs) {\n const dayPath = path.join(factsDir, day);\n const dayFiles = (await readdir(dayPath))\n .filter((f: string) => f.startsWith(\"correction-\") && f.endsWith(\".md\"))\n .map((f: string) => path.join(dayPath, f));\n files.push(...dayFiles);\n }\n } catch {\n // facts dir might not exist\n }\n\n // Also check the dedicated corrections directory\n try {\n const { readdir } = await import(\"node:fs/promises\");\n const corrFiles = (await readdir(correctionsDir))\n .filter((f: string) => f.endsWith(\".md\"))\n .map((f: string) => path.join(correctionsDir, f));\n files.push(...corrFiles);\n } catch {\n // corrections dir might not exist\n }\n\n const corrections: CorrectionMemory[] = [];\n const seen = new Set<string>();\n\n for (const filePath of files) {\n try {\n const raw = await readFile(filePath, \"utf8\");\n\n // Parse frontmatter\n const fmMatch = raw.match(/^---\\n([\\s\\S]*?)\\n---\\n([\\s\\S]*)$/);\n if (!fmMatch) continue;\n\n const content = fmMatch[2].trim();\n if (!content || content.length < 10) continue;\n\n // Extract id from frontmatter\n const idMatch = fmMatch[1].match(/^id:\\s*(.+)$/m);\n const id = idMatch?.[1]?.trim() ?? path.basename(filePath, \".md\");\n\n if (seen.has(id)) continue;\n seen.add(id);\n\n const confMatch = fmMatch[1].match(/^confidence:\\s*(.+)$/m);\n const confidence = confMatch ? parseFloat(confMatch[1]) : 0.9;\n\n const entityMatch = fmMatch[1].match(/^entityRef:\\s*(.+)$/m);\n const entityRefs = entityMatch ? [entityMatch[1].trim()] : [];\n\n corrections.push({ id, content, created: \"\", confidence, entityRefs, tags: [] });\n } catch {\n // skip unparseable files\n }\n }\n\n return corrections;\n}\n\n// ─── LLM-Assisted Clustering and Replay ──────────────────────────────────────\n\nconst CLUSTER_PROMPT = `You are analyzing user corrections to an AI assistant. Each correction represents a moment where the assistant's prediction of what the user wanted was WRONG.\n\nYour job: Group these corrections into clusters where the SAME TYPE of misunderstanding is happening. Then for each cluster, synthesize a CalibrationRule.\n\nA CalibrationRule describes:\n- condition: When does this type of mistake happen?\n- modelTendency: What does the model tend to assume or do wrong?\n- userExpectation: What does the user actually want instead?\n- calibration: How should the model adjust its behavior?\n- ruleType: One of \"model_tendency\", \"user_expectation\", \"scope_boundary\", \"verification_required\"\n\nFocus on PATTERNS, not individual corrections. A cluster needs at least 2 corrections to be worth a rule.\n\nOutput valid JSON only:\n{\n \"rules\": [\n {\n \"ruleType\": \"model_tendency\",\n \"condition\": \"When discussing project scope or task boundaries\",\n \"modelTendency\": \"The model tends to assume broader scope than the user intends\",\n \"userExpectation\": \"The user prefers narrow, specific task definitions and wants to be asked before scope expansion\",\n \"calibration\": \"When uncertain about scope, ask for clarification rather than assuming. Default to the narrower interpretation.\",\n \"confidence\": 0.85,\n \"evidenceIds\": [\"correction-id-1\", \"correction-id-2\"]\n }\n ]\n}`;\n\nexport async function synthesizeCalibrationRules(\n corrections: CorrectionMemory[],\n llm: FallbackLlmClient,\n existingRules: CalibrationRule[],\n): Promise<CalibrationRule[]> {\n if (corrections.length < 2) return [];\n\n // Format corrections for the LLM\n const correctionText = corrections\n .slice(0, 50) // limit to avoid huge prompts\n .map((c, i) => `[${c.id}] ${c.content}`)\n .join(\"\\n\\n\");\n\n const existingRulesText = existingRules.length > 0\n ? `\\n\\nExisting calibration rules (avoid duplicating these):\\n${existingRules.map((r) => `- ${r.condition}: ${r.calibration}`).join(\"\\n\")}`\n : \"\";\n\n const response = await llm.chatCompletion(\n [\n { role: \"system\", content: CLUSTER_PROMPT },\n { role: \"user\", content: `Here are ${corrections.length} corrections from this user:\\n\\n${correctionText}${existingRulesText}` },\n ],\n { temperature: 0.3, maxTokens: 3000 },\n );\n\n if (!response?.content) return [];\n\n try {\n let jsonStr = response.content.trim();\n const fenceMatch = jsonStr.match(/```(?:json)?\\s*\\n?([\\s\\S]*?)\\n?\\s*```/);\n if (fenceMatch) jsonStr = fenceMatch[1];\n\n const parsed = JSON.parse(jsonStr);\n if (!Array.isArray(parsed.rules)) return [];\n\n const now = new Date().toISOString();\n return parsed.rules\n .filter((r: any) => r.condition && r.calibration && r.modelTendency)\n .map((r: any) => ({\n id: `cal-${createHash(\"sha256\").update(r.condition + r.calibration).digest(\"hex\").slice(0, 12)}`,\n ruleType: r.ruleType ?? \"model_tendency\",\n condition: String(r.condition),\n modelTendency: String(r.modelTendency),\n userExpectation: String(r.userExpectation ?? \"\"),\n calibration: String(r.calibration),\n confidence: typeof r.confidence === \"number\" ? r.confidence : 0.7,\n evidenceCount: Array.isArray(r.evidenceIds) ? r.evidenceIds.length : 1,\n evidenceCorrectionIds: Array.isArray(r.evidenceIds) ? r.evidenceIds : [],\n createdAt: now,\n lastReinforcedAt: now,\n }));\n } catch {\n log.warn(\"[calibration] failed to parse LLM response\");\n return [];\n }\n}\n\n// ─── Recall Section ──────────────────────────────────────────────────────────\n\n/**\n * Build a recall section from calibration rules relevant to the current query.\n * Uses the LLM to select which rules apply to the current context.\n */\nexport function buildCalibrationRecallSection(\n rules: CalibrationRule[],\n query: string,\n maxChars: number = 1200,\n): string | null {\n if (rules.length === 0) return null;\n\n // Simple relevance: include all rules (they're already filtered to this user)\n // In production, could use embedding similarity to filter\n const lines: string[] = [\n \"## Model Calibration (learned from past corrections)\",\n \"\",\n \"Adjustments for this specific user, learned from patterns in their corrections:\",\n \"\",\n ];\n\n let totalChars = lines.join(\"\\n\").length;\n\n for (const rule of rules) {\n const line = `- **${rule.condition}**: ${rule.modelTendency} → Instead: ${rule.calibration}`;\n if (totalChars + line.length + 1 > maxChars) break;\n lines.push(line);\n totalChars += line.length + 1;\n }\n\n if (lines.length <= 4) return null;\n lines.push(\"\");\n return lines.join(\"\\n\");\n}\n\n// ─── Public API ──────────────────────────────────────────────────────────────\n\n/**\n * Run the full calibration pipeline:\n * 1. Read all corrections\n * 2. Send to LLM for clustering and rule synthesis\n * 3. Merge with existing rules\n * 4. Write updated index\n */\nexport async function runCalibrationConsolidation(options: {\n memoryDir: string;\n gatewayConfig?: GatewayConfig;\n}): Promise<CalibrationRule[]> {\n try {\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable()) {\n log.debug(\"[calibration] no LLM available — skipping consolidation\");\n return [];\n }\n\n const corrections = await readCorrections(options.memoryDir);\n if (corrections.length < 3) {\n log.debug(`[calibration] only ${corrections.length} corrections — need at least 3`);\n return [];\n }\n\n const existingIndex = await readCalibrationIndex(options.memoryDir);\n\n const newRules = await synthesizeCalibrationRules(corrections, llm, existingIndex.rules);\n if (newRules.length === 0) {\n log.debug(\"[calibration] no new calibration rules synthesized\");\n return existingIndex.rules;\n }\n\n // Merge: keep existing rules, add new ones (deduplicate by id)\n const ruleMap = new Map(existingIndex.rules.map((r) => [r.id, r]));\n for (const rule of newRules) {\n if (ruleMap.has(rule.id)) {\n // Reinforce existing rule\n const existing = ruleMap.get(rule.id)!;\n existing.lastReinforcedAt = new Date().toISOString();\n existing.evidenceCount += rule.evidenceCount;\n existing.confidence = Math.min(1, existing.confidence + 0.05);\n } else {\n ruleMap.set(rule.id, rule);\n }\n }\n\n const allRules = [...ruleMap.values()];\n await writeCalibrationIndex(options.memoryDir, {\n rules: allRules,\n updatedAt: new Date().toISOString(),\n totalCorrectionsAnalyzed: corrections.length,\n });\n\n log.debug(`[calibration] synthesized ${newRules.length} new rule(s), ${allRules.length} total`);\n return allRules;\n } catch (error) {\n log.warn(`[calibration] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return [];\n }\n}\n\n/**\n * Standalone entry point for calibration consolidation that can be called\n * independently of weekly compounding. The compounding engine's\n * `synthesizeWeekly()` is one trigger, but orchestrators or periodic\n * maintenance jobs should call this directly so calibration is not gated\n * on weekly compounding being enabled.\n */\nexport async function runCalibrationIfEnabled(options: {\n memoryDir: string;\n calibrationEnabled: boolean;\n gatewayConfig?: GatewayConfig;\n}): Promise<CalibrationRule[]> {\n if (!options.calibrationEnabled) {\n return [];\n }\n return runCalibrationConsolidation({\n memoryDir: options.memoryDir,\n gatewayConfig: options.gatewayConfig,\n });\n}\n\n/**\n * Get calibration rules for recall injection.\n * Reads the pre-computed calibration index.\n */\nexport async function getCalibrationRulesForRecall(\n memoryDir: string,\n): Promise<CalibrationRule[]> {\n const index = await readCalibrationIndex(memoryDir);\n return index.rules;\n}\n"],"mappings":";;;;;;;;;;;;AAcA,SAAS,kBAAkB;AAC3B,OAAO,UAAU;AACjB,SAAS,OAAO,UAAU,iBAAiB;AA+B3C,SAAS,eAAe,WAA2B;AACjD,SAAO,KAAK,KAAK,WAAW,SAAS,aAAa;AACpD;AAEA,SAAS,qBAAqB,WAA2B;AACvD,SAAO,KAAK,KAAK,eAAe,SAAS,GAAG,wBAAwB;AACtE;AAEA,eAAsB,qBAAqB,WAA8C;AACvF,MAAI;AACF,UAAM,MAAM,KAAK,MAAM,MAAM,SAAS,qBAAqB,SAAS,GAAG,MAAM,CAAC;AAC9E,WAAO;AAAA,MACL,OAAO,MAAM,QAAQ,IAAI,KAAK,IAAI,IAAI,QAAQ,CAAC;AAAA,MAC/C,WAAW,OAAO,IAAI,cAAc,WAAW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B;AAC5B,QAAM,QAAQ,MAAM,qBAAqB,SAAS;AAClD,SAAO,MAAM;AACf;","names":[]}
@@ -0,0 +1,23 @@
1
+ // openclaw-engram: Local-first memory plugin
2
+ import {
3
+ makeEdgeId,
4
+ readChainIndex,
5
+ resolveChainsDir,
6
+ scoreStitchCandidate,
7
+ stitchCausalChain,
8
+ validateCausalEdge,
9
+ writeChainIndex
10
+ } from "./chunk-3GARBXWR.js";
11
+ import "./chunk-AFMNQR7H.js";
12
+ import "./chunk-DEIBZP3O.js";
13
+ import "./chunk-SSIIJJKA.js";
14
+ export {
15
+ makeEdgeId,
16
+ readChainIndex,
17
+ resolveChainsDir,
18
+ scoreStitchCandidate,
19
+ stitchCausalChain,
20
+ validateCausalEdge,
21
+ writeChainIndex
22
+ };
23
+ //# sourceMappingURL=causal-chain-OJCLNNEH.js.map
@@ -0,0 +1,206 @@
1
+ // openclaw-engram: Local-first memory plugin
2
+ import {
3
+ readChainIndex,
4
+ resolveChainsDir
5
+ } from "./chunk-3GARBXWR.js";
6
+ import {
7
+ isRecord
8
+ } from "./chunk-AFMNQR7H.js";
9
+ import {
10
+ FallbackLlmClient
11
+ } from "./chunk-PW5V7OJH.js";
12
+ import {
13
+ listJsonFiles,
14
+ readJsonFile
15
+ } from "./chunk-DEIBZP3O.js";
16
+ import {
17
+ log
18
+ } from "./chunk-SSIIJJKA.js";
19
+
20
+ // src/causal-consolidation.ts
21
+ import { createHash } from "crypto";
22
+ import path from "path";
23
+ async function readAllTrajectories(memoryDir, causalTrajectoryStoreDir) {
24
+ const root = causalTrajectoryStoreDir ? path.join(memoryDir, causalTrajectoryStoreDir) : path.join(memoryDir, "state", "causal-trajectories");
25
+ const trajectoriesDir = path.join(root, "trajectories");
26
+ const files = await listJsonFiles(trajectoriesDir).catch(() => []);
27
+ const results = [];
28
+ for (const filePath of files) {
29
+ try {
30
+ const raw = await readJsonFile(filePath);
31
+ if (isRecord(raw) && typeof raw.trajectoryId === "string") {
32
+ results.push(raw);
33
+ }
34
+ } catch {
35
+ }
36
+ }
37
+ return results;
38
+ }
39
+ function formatCausalContext(trajectories, chainIndex, maxChars = 8e3) {
40
+ const bySession = /* @__PURE__ */ new Map();
41
+ for (const t of trajectories) {
42
+ const list = bySession.get(t.sessionKey) ?? [];
43
+ list.push(t);
44
+ bySession.set(t.sessionKey, list);
45
+ }
46
+ const lines = [];
47
+ lines.push(`## Causal Trajectories (${trajectories.length} across ${bySession.size} sessions)`);
48
+ lines.push("");
49
+ for (const [sessionKey, sessionTrajs] of bySession) {
50
+ lines.push(`### Session: ${sessionKey}`);
51
+ for (const t of sessionTrajs.slice(0, 5)) {
52
+ const outcome = t.outcomeKind === "success" ? "+" : t.outcomeKind === "failure" ? "-" : "~";
53
+ lines.push(`[${outcome}] Goal: ${t.goal}`);
54
+ lines.push(` Action: ${t.actionSummary}`);
55
+ lines.push(` Outcome: ${t.outcomeSummary}`);
56
+ if (t.followUpSummary) lines.push(` Follow-up: ${t.followUpSummary}`);
57
+ if (t.entityRefs?.length) lines.push(` Entities: ${t.entityRefs.join(", ")}`);
58
+ }
59
+ lines.push("");
60
+ }
61
+ const edgeCount = Object.keys(chainIndex.edges).length;
62
+ if (edgeCount > 0) {
63
+ lines.push(`## Cross-Session Causal Chains (${edgeCount} connections)`);
64
+ lines.push("");
65
+ const trajectoryMap = new Map(trajectories.map((t) => [t.trajectoryId, t]));
66
+ const shown = /* @__PURE__ */ new Set();
67
+ for (const [edgeId, edge] of Object.entries(chainIndex.edges)) {
68
+ if (shown.size >= 10) break;
69
+ const from = trajectoryMap.get(edge.fromTrajectoryId);
70
+ const to = trajectoryMap.get(edge.toTrajectoryId);
71
+ if (!from || !to) continue;
72
+ lines.push(`${edge.edgeType}: "${from.goal}" (${from.sessionKey}) \u2192 "${to.goal}" (${to.sessionKey})`);
73
+ shown.add(edgeId);
74
+ }
75
+ lines.push("");
76
+ }
77
+ const result = lines.join("\n");
78
+ return result.length > maxChars ? result.slice(0, maxChars) + "\n[truncated]" : result;
79
+ }
80
+ var CONSOLIDATION_PROMPT = `You are analyzing a user's causal trajectory history across multiple sessions. Trajectories record what the user tried to do (goal), what they did (action), and what happened (outcome).
81
+
82
+ Your job is to identify:
83
+ 1. BEHAVIORAL RULES: Recurring patterns where the same approach consistently succeeds or fails. These should be actionable guidance for future sessions.
84
+ 2. PREFERENCES: What the user cares about, prefers, or consistently chooses \u2014 even if never explicitly stated. Infer preferences from what they repeatedly do, retry until successful, or always include in their workflow.
85
+
86
+ IMPORTANT:
87
+ - Look for CROSS-SESSION patterns \u2014 things that repeat across different sessions are more significant than within-session patterns.
88
+ - A user who retries the same goal across sessions has a strong implicit preference for that outcome.
89
+ - Consistent action choices reveal preferences even when the user never says "I prefer X."
90
+ - Frame preferences as "The user would prefer responses that..." when applicable.
91
+
92
+ Output valid JSON only:
93
+ {
94
+ "rules": [
95
+ {"content": "actionable rule text", "category": "rule|principle", "confidence": 0.0-1.0, "evidence": ["trajectory IDs"]}
96
+ ],
97
+ "preferences": [
98
+ {"statement": "The user would prefer...", "confidence": 0.0-1.0, "evidence": ["trajectory IDs"]}
99
+ ]
100
+ }
101
+
102
+ If no clear patterns exist, return {"rules": [], "preferences": []}.`;
103
+ async function consolidateWithLlm(context, llm) {
104
+ const response = await llm.chatCompletion(
105
+ [
106
+ { role: "system", content: CONSOLIDATION_PROMPT },
107
+ { role: "user", content: context }
108
+ ],
109
+ { temperature: 0.2, maxTokens: 2e3 }
110
+ );
111
+ if (!response?.content) {
112
+ return { rules: [], preferences: [] };
113
+ }
114
+ try {
115
+ let jsonStr = response.content.trim();
116
+ const fenceMatch = jsonStr.match(/```(?:json)?\s*\n?([\s\S]*?)\n?\s*```/);
117
+ if (fenceMatch) jsonStr = fenceMatch[1];
118
+ const parsed = JSON.parse(jsonStr);
119
+ return {
120
+ rules: Array.isArray(parsed.rules) ? parsed.rules.filter(
121
+ (r) => typeof r.content === "string" && r.content.length > 5
122
+ ) : [],
123
+ preferences: Array.isArray(parsed.preferences) ? parsed.preferences.filter(
124
+ (p) => typeof p.statement === "string" && p.statement.length > 5
125
+ ) : []
126
+ };
127
+ } catch {
128
+ log.warn("[cmc] failed to parse LLM consolidation response");
129
+ return { rules: [], preferences: [] };
130
+ }
131
+ }
132
+ function stablePatternId(content) {
133
+ const digest = createHash("sha256").update(`causal-pattern\0${content}`).digest("hex").slice(0, 16);
134
+ return `causal-pattern:${digest}`;
135
+ }
136
+ function llmResultToCandidates(result) {
137
+ const candidates = [];
138
+ for (const rule of result.rules) {
139
+ const category = rule.category === "principle" ? "principle" : "rule";
140
+ candidates.push({
141
+ id: stablePatternId(rule.content),
142
+ sourceType: "causal-pattern",
143
+ subject: rule.content.slice(0, 80),
144
+ category,
145
+ content: rule.content,
146
+ score: Math.min(1, rule.confidence ?? 0.7),
147
+ rationale: "LLM-identified causal pattern from cross-session trajectory analysis",
148
+ outcome: null,
149
+ provenance: (rule.evidence ?? []).slice(0, 5),
150
+ agent: null,
151
+ workflow: null
152
+ });
153
+ }
154
+ return candidates;
155
+ }
156
+ async function deriveCausalPromotionCandidates(options) {
157
+ try {
158
+ const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);
159
+ if (trajectories.length < options.config.minRecurrence) return [];
160
+ const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);
161
+ const chainIndex = await readChainIndex(chainsDir);
162
+ const context = formatCausalContext(trajectories, chainIndex);
163
+ const llm = new FallbackLlmClient(options.gatewayConfig);
164
+ if (!llm.isAvailable()) {
165
+ log.debug("[cmc] no LLM available for consolidation \u2014 skipping");
166
+ return [];
167
+ }
168
+ const result = await consolidateWithLlm(context, llm);
169
+ const candidates = llmResultToCandidates(result);
170
+ log.debug(`[cmc] LLM consolidation produced ${candidates.length} rule(s) and ${result.preferences.length} preference(s)`);
171
+ return candidates;
172
+ } catch (error) {
173
+ log.warn(`[cmc] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);
174
+ return [];
175
+ }
176
+ }
177
+ async function synthesizeCausalPreferencesViaLlm(options) {
178
+ try {
179
+ const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);
180
+ if (trajectories.length < (options.minTrajectories ?? 2)) return null;
181
+ const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);
182
+ const chainIndex = await readChainIndex(chainsDir);
183
+ const context = formatCausalContext(trajectories, chainIndex);
184
+ const llm = new FallbackLlmClient(options.gatewayConfig);
185
+ if (!llm.isAvailable()) return null;
186
+ const result = await consolidateWithLlm(context, llm);
187
+ if (result.preferences.length === 0 && result.rules.length === 0) return null;
188
+ const lines = ["## Behavioral Insights (from Causal Chain Analysis)", ""];
189
+ for (const pref of result.preferences) {
190
+ lines.push(`- ${pref.statement}`);
191
+ }
192
+ for (const rule of result.rules) {
193
+ lines.push(`- ${rule.content}`);
194
+ }
195
+ lines.push("");
196
+ return lines.join("\n");
197
+ } catch (error) {
198
+ log.warn(`[cmc] preference synthesis failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);
199
+ return null;
200
+ }
201
+ }
202
+ export {
203
+ deriveCausalPromotionCandidates,
204
+ synthesizeCausalPreferencesViaLlm
205
+ };
206
+ //# sourceMappingURL=causal-consolidation-LN3WQ6OV.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/causal-consolidation.ts"],"sourcesContent":["/**\n * causal-consolidation.ts — CMC Phase 2: LLM-Assisted Causal Consolidation\n *\n * Uses an LLM to analyze causal trajectory patterns across sessions.\n * The LLM receives the causal chain graph as context — connected trajectories\n * from different sessions — and identifies recurring behavioral patterns,\n * preference signals, and actionable rules.\n *\n * This is the core CMC innovation: the LLM gets cross-session causal context\n * that no other memory system provides. It can see that a user investigated\n * a bug in session 1, attempted a fix in session 2, and succeeded in session 3 —\n * and synthesize a rule or preference from that chain.\n */\n\nimport { createHash } from \"node:crypto\";\nimport type { CausalTrajectoryRecord } from \"./causal-trajectory.js\";\nimport { readChainIndex, resolveChainsDir, type CausalChainIndex, type CausalEdge } from \"./causal-chain.js\";\nimport { listJsonFiles, readJsonFile } from \"./json-store.js\";\nimport { isRecord } from \"./store-contract.js\";\nimport { FallbackLlmClient } from \"./fallback-llm.js\";\nimport type { GatewayConfig } from \"./types.js\";\nimport path from \"node:path\";\nimport { log } from \"./logger.js\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface CausalPatternCandidate {\n id: string;\n sourceType: \"causal-pattern\";\n subject: string;\n category: \"principle\" | \"rule\";\n content: string;\n score: number;\n rationale: string;\n outcome: null;\n provenance: string[];\n agent: string | null;\n workflow: string | null;\n}\n\nexport interface ConsolidationConfig {\n minRecurrence: number;\n minSessions: number;\n successThreshold: number;\n}\n\nexport interface LlmConsolidationResult {\n rules: Array<{\n content: string;\n category: \"rule\" | \"principle\" | \"preference\";\n confidence: number;\n evidence: string[];\n }>;\n preferences: Array<{\n statement: string;\n confidence: number;\n evidence: string[];\n }>;\n}\n\n// ─── Trajectory Reading ──────────────────────────────────────────────────────\n\nasync function readAllTrajectories(\n memoryDir: string,\n causalTrajectoryStoreDir?: string,\n): Promise<CausalTrajectoryRecord[]> {\n const root = causalTrajectoryStoreDir\n ? path.join(memoryDir, causalTrajectoryStoreDir)\n : path.join(memoryDir, \"state\", \"causal-trajectories\");\n const trajectoriesDir = path.join(root, \"trajectories\");\n\n const files = await listJsonFiles(trajectoriesDir).catch(() => [] as string[]);\n const results: CausalTrajectoryRecord[] = [];\n\n for (const filePath of files) {\n try {\n const raw = await readJsonFile(filePath);\n if (isRecord(raw) && typeof raw.trajectoryId === \"string\") {\n results.push(raw as unknown as CausalTrajectoryRecord);\n }\n } catch {\n // skip invalid\n }\n }\n\n return results;\n}\n\n// ─── Context Formatting ──────────────────────────────────────────────────────\n\n/**\n * Format trajectories and their causal connections as a readable context\n * for the LLM. Groups by session and shows chain connections.\n */\nfunction formatCausalContext(\n trajectories: CausalTrajectoryRecord[],\n chainIndex: CausalChainIndex,\n maxChars: number = 8000,\n): string {\n // Group trajectories by session\n const bySession = new Map<string, CausalTrajectoryRecord[]>();\n for (const t of trajectories) {\n const list = bySession.get(t.sessionKey) ?? [];\n list.push(t);\n bySession.set(t.sessionKey, list);\n }\n\n const lines: string[] = [];\n lines.push(`## Causal Trajectories (${trajectories.length} across ${bySession.size} sessions)`);\n lines.push(\"\");\n\n // Format each session's trajectories\n for (const [sessionKey, sessionTrajs] of bySession) {\n lines.push(`### Session: ${sessionKey}`);\n for (const t of sessionTrajs.slice(0, 5)) {\n const outcome = t.outcomeKind === \"success\" ? \"+\" : t.outcomeKind === \"failure\" ? \"-\" : \"~\";\n lines.push(`[${outcome}] Goal: ${t.goal}`);\n lines.push(` Action: ${t.actionSummary}`);\n lines.push(` Outcome: ${t.outcomeSummary}`);\n if (t.followUpSummary) lines.push(` Follow-up: ${t.followUpSummary}`);\n if (t.entityRefs?.length) lines.push(` Entities: ${t.entityRefs.join(\", \")}`);\n }\n lines.push(\"\");\n }\n\n // Format causal chain connections\n const edgeCount = Object.keys(chainIndex.edges).length;\n if (edgeCount > 0) {\n lines.push(`## Cross-Session Causal Chains (${edgeCount} connections)`);\n lines.push(\"\");\n\n const trajectoryMap = new Map(trajectories.map((t) => [t.trajectoryId, t]));\n const shown = new Set<string>();\n\n for (const [edgeId, edge] of Object.entries(chainIndex.edges)) {\n if (shown.size >= 10) break; // limit output size\n const from = trajectoryMap.get(edge.fromTrajectoryId);\n const to = trajectoryMap.get(edge.toTrajectoryId);\n if (!from || !to) continue;\n\n lines.push(`${edge.edgeType}: \"${from.goal}\" (${from.sessionKey}) → \"${to.goal}\" (${to.sessionKey})`);\n shown.add(edgeId);\n }\n lines.push(\"\");\n }\n\n const result = lines.join(\"\\n\");\n return result.length > maxChars ? result.slice(0, maxChars) + \"\\n[truncated]\" : result;\n}\n\n// ─── LLM Consolidation ──────────────────────────────────────────────────────\n\nconst CONSOLIDATION_PROMPT = `You are analyzing a user's causal trajectory history across multiple sessions. Trajectories record what the user tried to do (goal), what they did (action), and what happened (outcome).\n\nYour job is to identify:\n1. BEHAVIORAL RULES: Recurring patterns where the same approach consistently succeeds or fails. These should be actionable guidance for future sessions.\n2. PREFERENCES: What the user cares about, prefers, or consistently chooses — even if never explicitly stated. Infer preferences from what they repeatedly do, retry until successful, or always include in their workflow.\n\nIMPORTANT:\n- Look for CROSS-SESSION patterns — things that repeat across different sessions are more significant than within-session patterns.\n- A user who retries the same goal across sessions has a strong implicit preference for that outcome.\n- Consistent action choices reveal preferences even when the user never says \"I prefer X.\"\n- Frame preferences as \"The user would prefer responses that...\" when applicable.\n\nOutput valid JSON only:\n{\n \"rules\": [\n {\"content\": \"actionable rule text\", \"category\": \"rule|principle\", \"confidence\": 0.0-1.0, \"evidence\": [\"trajectory IDs\"]}\n ],\n \"preferences\": [\n {\"statement\": \"The user would prefer...\", \"confidence\": 0.0-1.0, \"evidence\": [\"trajectory IDs\"]}\n ]\n}\n\nIf no clear patterns exist, return {\"rules\": [], \"preferences\": []}.`;\n\nasync function consolidateWithLlm(\n context: string,\n llm: FallbackLlmClient,\n): Promise<LlmConsolidationResult> {\n const response = await llm.chatCompletion(\n [\n { role: \"system\", content: CONSOLIDATION_PROMPT },\n { role: \"user\", content: context },\n ],\n { temperature: 0.2, maxTokens: 2000 },\n );\n\n if (!response?.content) {\n return { rules: [], preferences: [] };\n }\n\n try {\n // Extract JSON from response (may have markdown code fences)\n let jsonStr = response.content.trim();\n const fenceMatch = jsonStr.match(/```(?:json)?\\s*\\n?([\\s\\S]*?)\\n?\\s*```/);\n if (fenceMatch) jsonStr = fenceMatch[1];\n\n const parsed = JSON.parse(jsonStr);\n return {\n rules: Array.isArray(parsed.rules) ? parsed.rules.filter(\n (r: any) => typeof r.content === \"string\" && r.content.length > 5,\n ) : [],\n preferences: Array.isArray(parsed.preferences) ? parsed.preferences.filter(\n (p: any) => typeof p.statement === \"string\" && p.statement.length > 5,\n ) : [],\n };\n } catch {\n log.warn(\"[cmc] failed to parse LLM consolidation response\");\n return { rules: [], preferences: [] };\n }\n}\n\n// ─── Candidate Generation ────────────────────────────────────────────────────\n\nfunction stablePatternId(content: string): string {\n const digest = createHash(\"sha256\")\n .update(`causal-pattern\\0${content}`)\n .digest(\"hex\")\n .slice(0, 16);\n return `causal-pattern:${digest}`;\n}\n\nfunction llmResultToCandidates(result: LlmConsolidationResult): CausalPatternCandidate[] {\n const candidates: CausalPatternCandidate[] = [];\n\n for (const rule of result.rules) {\n const category = rule.category === \"principle\" ? \"principle\" : \"rule\";\n candidates.push({\n id: stablePatternId(rule.content),\n sourceType: \"causal-pattern\",\n subject: rule.content.slice(0, 80),\n category,\n content: rule.content,\n score: Math.min(1, rule.confidence ?? 0.7),\n rationale: \"LLM-identified causal pattern from cross-session trajectory analysis\",\n outcome: null,\n provenance: (rule.evidence ?? []).slice(0, 5),\n agent: null,\n workflow: null,\n });\n }\n\n return candidates;\n}\n\n// ─── Public API ──────────────────────────────────────────────────────────────\n\n/**\n * Run LLM-assisted consolidation: read trajectories, format causal context,\n * ask LLM to identify patterns and preferences.\n */\nexport async function deriveCausalPromotionCandidates(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n config: ConsolidationConfig;\n gatewayConfig?: GatewayConfig;\n}): Promise<CausalPatternCandidate[]> {\n try {\n const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);\n if (trajectories.length < options.config.minRecurrence) return [];\n\n const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const chainIndex = await readChainIndex(chainsDir);\n\n // Format the causal context for the LLM\n const context = formatCausalContext(trajectories, chainIndex);\n\n // If no LLM available, fall back to empty (no deterministic fallback)\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable()) {\n log.debug(\"[cmc] no LLM available for consolidation — skipping\");\n return [];\n }\n\n // Call LLM for pattern analysis\n const result = await consolidateWithLlm(context, llm);\n const candidates = llmResultToCandidates(result);\n\n log.debug(`[cmc] LLM consolidation produced ${candidates.length} rule(s) and ${result.preferences.length} preference(s)`);\n return candidates;\n } catch (error) {\n log.warn(`[cmc] consolidation failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return [];\n }\n}\n\n/**\n * Get LLM-synthesized preferences from causal trajectory analysis.\n * Returns formatted preference statements for recall injection.\n */\nexport async function synthesizeCausalPreferencesViaLlm(options: {\n memoryDir: string;\n causalTrajectoryStoreDir?: string;\n gatewayConfig?: GatewayConfig;\n minTrajectories?: number;\n}): Promise<string | null> {\n try {\n const trajectories = await readAllTrajectories(options.memoryDir, options.causalTrajectoryStoreDir);\n if (trajectories.length < (options.minTrajectories ?? 2)) return null;\n\n const chainsDir = resolveChainsDir(options.memoryDir, options.causalTrajectoryStoreDir);\n const chainIndex = await readChainIndex(chainsDir);\n const context = formatCausalContext(trajectories, chainIndex);\n\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable()) return null;\n\n const result = await consolidateWithLlm(context, llm);\n if (result.preferences.length === 0 && result.rules.length === 0) return null;\n\n const lines: string[] = [\"## Behavioral Insights (from Causal Chain Analysis)\", \"\"];\n\n for (const pref of result.preferences) {\n lines.push(`- ${pref.statement}`);\n }\n\n for (const rule of result.rules) {\n lines.push(`- ${rule.content}`);\n }\n\n lines.push(\"\");\n return lines.join(\"\\n\");\n } catch (error) {\n log.warn(`[cmc] preference synthesis failed (non-fatal): ${error instanceof Error ? error.message : String(error)}`);\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAcA,SAAS,kBAAkB;AAO3B,OAAO,UAAU;AAyCjB,eAAe,oBACb,WACA,0BACmC;AACnC,QAAM,OAAO,2BACT,KAAK,KAAK,WAAW,wBAAwB,IAC7C,KAAK,KAAK,WAAW,SAAS,qBAAqB;AACvD,QAAM,kBAAkB,KAAK,KAAK,MAAM,cAAc;AAEtD,QAAM,QAAQ,MAAM,cAAc,eAAe,EAAE,MAAM,MAAM,CAAC,CAAa;AAC7E,QAAM,UAAoC,CAAC;AAE3C,aAAW,YAAY,OAA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