@remnic/core 1.0.0 → 1.0.1
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/dist/access-cli.js +10 -9
- package/dist/access-cli.js.map +1 -1
- package/dist/access-http.d.ts +1 -1
- package/dist/access-mcp.d.ts +1 -1
- package/dist/access-schema.d.ts +40 -40
- package/dist/access-service.d.ts +1 -1
- package/dist/bootstrap.d.ts +1 -1
- package/dist/calibration.js +3 -2
- package/dist/calibration.js.map +1 -1
- package/dist/causal-consolidation.js +3 -2
- package/dist/causal-consolidation.js.map +1 -1
- package/dist/{chunk-QFQVZOGA.js → chunk-6UJQNRIO.js} +2 -2
- package/dist/{chunk-CDW777AI.js → chunk-LP47L3ZX.js} +2 -2
- package/dist/{chunk-LU3GQNDQ.js → chunk-M5ZBBBJI.js} +12 -1
- package/dist/chunk-M5ZBBBJI.js.map +1 -0
- package/dist/chunk-OOSWAUYB.js +47 -0
- package/dist/chunk-OOSWAUYB.js.map +1 -0
- package/dist/{chunk-YAPUAHAY.js → chunk-OTFNI3OO.js} +32 -32
- package/dist/{chunk-TKO4HZCK.js → chunk-UYSKNO6E.js} +1 -1
- package/dist/{chunk-KT4NEUNF.js → chunk-XUHI52HK.js} +80 -22
- package/dist/chunk-XUHI52HK.js.map +1 -0
- package/dist/cli.d.ts +2 -2
- package/dist/cli.js +1 -1
- package/dist/{engine-P26JFSVY.js → engine-2A6J4XEX.js} +2 -2
- package/dist/explicit-capture.d.ts +1 -1
- package/dist/extraction.js +4 -3
- package/dist/fallback-llm.d.ts +22 -3
- package/dist/fallback-llm.js +3 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +10 -9
- package/dist/index.js.map +1 -1
- package/dist/models-json.d.ts +17 -0
- package/dist/models-json.js +12 -0
- package/dist/models-json.js.map +1 -0
- package/dist/orchestrator.d.ts +1 -1
- package/dist/orchestrator.js +10 -9
- package/dist/resolve-provider-secret.d.ts +29 -1
- package/dist/resolve-provider-secret.js +3 -1
- package/dist/schemas.d.ts +42 -42
- package/dist/summarizer.js +4 -3
- package/package.json +2 -1
- package/dist/chunk-KT4NEUNF.js.map +0 -1
- package/dist/chunk-LU3GQNDQ.js.map +0 -1
- package/dist/{chunk-QFQVZOGA.js.map → chunk-6UJQNRIO.js.map} +0 -0
- package/dist/{chunk-CDW777AI.js.map → chunk-LP47L3ZX.js.map} +0 -0
- package/dist/{chunk-YAPUAHAY.js.map → chunk-OTFNI3OO.js.map} +0 -0
- package/dist/{chunk-TKO4HZCK.js.map → chunk-UYSKNO6E.js.map} +0 -0
- package/dist/{engine-P26JFSVY.js.map → engine-2A6J4XEX.js.map} +0 -0
- package/dist/{orchestrator-zTa-Qo-1.d.ts → orchestrator-CIvLFHx3.d.ts} +2 -2
package/dist/access-cli.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Orchestrator
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-OTFNI3OO.js";
|
|
4
|
+
import "./chunk-UYSKNO6E.js";
|
|
4
5
|
import "./chunk-IFFFR3MR.js";
|
|
5
|
-
import "./chunk-TKO4HZCK.js";
|
|
6
6
|
import "./chunk-Z5AAYHUC.js";
|
|
7
7
|
import "./chunk-4A24LIM2.js";
|
|
8
8
|
import "./chunk-TPB3I2AC.js";
|
|
9
9
|
import "./chunk-UHGBNIOS.js";
|
|
10
10
|
import "./chunk-ZKYI7UVO.js";
|
|
11
|
-
import "./chunk-
|
|
11
|
+
import "./chunk-LP47L3ZX.js";
|
|
12
12
|
import "./chunk-ETOW6ACV.js";
|
|
13
|
+
import "./chunk-GPGBSNKM.js";
|
|
14
|
+
import "./chunk-V3RXWQIE.js";
|
|
13
15
|
import "./chunk-G3AG3KZN.js";
|
|
14
16
|
import "./chunk-2CJCWDMR.js";
|
|
17
|
+
import "./chunk-HG2NKWR2.js";
|
|
15
18
|
import "./chunk-X7XN6YU4.js";
|
|
16
19
|
import "./chunk-BRK4ODMI.js";
|
|
17
20
|
import "./chunk-C7VW7C3F.js";
|
|
18
|
-
import "./chunk-GPGBSNKM.js";
|
|
19
|
-
import "./chunk-V3RXWQIE.js";
|
|
20
21
|
import "./chunk-YCN4BVDK.js";
|
|
21
22
|
import "./chunk-L5RPWGFK.js";
|
|
22
|
-
import "./chunk-HG2NKWR2.js";
|
|
23
23
|
import "./chunk-GJR6D6KC.js";
|
|
24
24
|
import "./chunk-H63EDPFJ.js";
|
|
25
25
|
import "./chunk-WWIQTB2Y.js";
|
|
26
|
-
import "./chunk-
|
|
26
|
+
import "./chunk-6UJQNRIO.js";
|
|
27
27
|
import "./chunk-MWGVGUIS.js";
|
|
28
28
|
import "./chunk-763GUIOU.js";
|
|
29
29
|
import "./chunk-ONRU4L2N.js";
|
|
@@ -65,8 +65,9 @@ import "./chunk-JWPLJLDU.js";
|
|
|
65
65
|
import "./chunk-YNI4S5WT.js";
|
|
66
66
|
import "./chunk-DORBM6OB.js";
|
|
67
67
|
import "./chunk-XYIK4LF6.js";
|
|
68
|
-
import "./chunk-
|
|
69
|
-
import "./chunk-
|
|
68
|
+
import "./chunk-XUHI52HK.js";
|
|
69
|
+
import "./chunk-M5ZBBBJI.js";
|
|
70
|
+
import "./chunk-OOSWAUYB.js";
|
|
70
71
|
import "./chunk-Y27UJK6V.js";
|
|
71
72
|
import "./chunk-UZB5KHKX.js";
|
|
72
73
|
import "./chunk-M5KEYE5E.js";
|
package/dist/access-cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/access-cli.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { parseConfig } from \"./config.js\";\nimport type { PluginConfig } from \"./types.js\";\nimport { Orchestrator } from \"./orchestrator.js\";\nimport { EngramAccessService } from \"./access-service.js\";\nimport { readEnvVar, resolveHomeDir } from \"./runtime/env.js\";\n\ntype CommandName = \"browse\" | \"store\";\n\ntype ParsedArgs = {\n command: CommandName;\n options: Record<string, string[]>;\n flags: Set<string>;\n};\n\ntype Runtime = {\n config: PluginConfig;\n service: EngramAccessService;\n};\n\ntype UsageErrorKind =\n | \"unsupported-command\"\n | \"unexpected-positional\"\n | \"missing-option\"\n | \"missing-content\"\n | \"invalid-integer\"\n | \"invalid-number\";\n\nclass UsageError extends Error {\n constructor(\n readonly kind: UsageErrorKind,\n readonly optionName?: string,\n ) {\n super(\"invalid access-cli arguments\");\n }\n}\n\nfunction formatUsageError(error: UsageError): string {\n switch (error.kind) {\n case \"unsupported-command\":\n return \"unsupported command\";\n case \"unexpected-positional\":\n return \"unexpected positional argument\";\n case \"missing-option\":\n return `missing required option: --${error.optionName ?? \"unknown\"}`;\n case \"missing-content\":\n return \"missing required option: --content or --content-file\";\n case \"invalid-integer\":\n return `invalid integer for --${error.optionName ?? \"unknown\"}`;\n case \"invalid-number\":\n return `invalid number for --${error.optionName ?? \"unknown\"}`;\n }\n}\n\nfunction writeCliOutput(text: string = \"\"): void {\n process.stdout.write(`${text}\\n`);\n}\n\nfunction usage(): string {\n return [\n \"Usage:\",\n \" engram-access browse [options]\",\n \" engram-access store [options]\",\n \"\",\n \"Browse options:\",\n \" --namespace <name>\",\n \" --query <text>\",\n \" --category <name>\",\n \" --status <name>\",\n \" --sort <updated_desc|updated_asc|created_desc|created_asc>\",\n \" --limit <n>\",\n \" --offset <n>\",\n \"\",\n \"Store options:\",\n \" --namespace <name>\",\n \" --session-key <key>\",\n \" --principal <principal>\",\n \" --content <text> | --content-file <path>\",\n \" --category <name>\",\n \" --confidence <0-1>\",\n \" --tag <tag> (repeatable)\",\n \" --entity-ref <ref>\",\n \" --ttl <duration>\",\n \" --source-reason <text>\",\n \" --idempotency-key <key>\",\n \" --dry-run\",\n ].join(\"\\n\");\n}\n\nfunction parseArgs(argv: string[]): ParsedArgs {\n const [commandRaw, ...rest] = argv;\n if (commandRaw !== \"browse\" && commandRaw !== \"store\") {\n throw new UsageError(\"unsupported-command\");\n }\n\n const options: Record<string, string[]> = {};\n const flags = new Set<string>();\n\n for (let i = 0; i < rest.length; i += 1) {\n const token = rest[i];\n if (!token.startsWith(\"--\")) {\n throw new UsageError(\"unexpected-positional\");\n }\n const key = token.slice(2);\n const next = rest[i + 1];\n if (!next || next.startsWith(\"--\")) {\n flags.add(key);\n continue;\n }\n if (!options[key]) {\n options[key] = [];\n }\n options[key].push(next);\n i += 1;\n }\n\n return {\n command: commandRaw,\n options,\n flags,\n };\n}\n\nfunction getLastOption(args: ParsedArgs, name: string): string | undefined {\n const values = args.options[name];\n if (!values || values.length === 0) return undefined;\n return values[values.length - 1];\n}\n\nfunction getAllOptions(args: ParsedArgs, name: string): string[] {\n return args.options[name] ?? [];\n}\n\nfunction requireOption(args: ParsedArgs, name: string): string {\n const value = getLastOption(args, name);\n if (!value || value.trim().length === 0) {\n throw new UsageError(\"missing-option\", name);\n }\n return value;\n}\n\nfunction parseIntegerOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = parseInt(raw, 10);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-integer\", name);\n }\n return value;\n}\n\nfunction parseFloatOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = Number.parseFloat(raw);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-number\", name);\n }\n return value;\n}\n\nfunction loadPluginConfig(): Record<string, unknown> {\n const configPath =\n readEnvVar(\"OPENCLAW_ENGRAM_CONFIG_PATH\") ||\n readEnvVar(\"OPENCLAW_CONFIG_PATH\") ||\n path.join(resolveHomeDir(), \".openclaw\", \"openclaw.json\");\n const raw = JSON.parse(fs.readFileSync(configPath, \"utf8\"));\n return raw?.plugins?.entries?.[\"openclaw-engram\"]?.config ?? {};\n}\n\nfunction buildRuntime(): Runtime {\n const config = parseConfig(loadPluginConfig());\n return {\n config,\n service: new EngramAccessService(new Orchestrator(config)),\n };\n}\n\nasync function runBrowse(args: ParsedArgs): Promise<void> {\n const { service } = buildRuntime();\n const result = await service.memoryBrowse({\n namespace: getLastOption(args, \"namespace\"),\n query: getLastOption(args, \"query\"),\n category: getLastOption(args, \"category\"),\n status: getLastOption(args, \"status\"),\n sort: getLastOption(args, \"sort\") as \"updated_desc\" | \"updated_asc\" | \"created_desc\" | \"created_asc\" | undefined,\n limit: parseIntegerOption(args, \"limit\"),\n offset: parseIntegerOption(args, \"offset\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nasync function runStore(args: ParsedArgs): Promise<void> {\n const { config, service } = buildRuntime();\n const contentFile = getLastOption(args, \"content-file\");\n const inlineContent = getLastOption(args, \"content\");\n const content = contentFile ? fs.readFileSync(contentFile, \"utf8\") : inlineContent;\n if (!content || content.trim().length === 0) {\n throw new UsageError(\"missing-content\");\n }\n\n const result = await service.memoryStore({\n namespace: getLastOption(args, \"namespace\"),\n sessionKey: getLastOption(args, \"session-key\"),\n authenticatedPrincipal: getLastOption(args, \"principal\") ?? config.agentAccessHttp.principal,\n content,\n category: requireOption(args, \"category\"),\n confidence: parseFloatOption(args, \"confidence\"),\n tags: getAllOptions(args, \"tag\"),\n entityRef: getLastOption(args, \"entity-ref\"),\n ttl: getLastOption(args, \"ttl\"),\n sourceReason: getLastOption(args, \"source-reason\"),\n idempotencyKey: getLastOption(args, \"idempotency-key\"),\n dryRun: args.flags.has(\"dry-run\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nexport async function main(argv: string[] = process.argv.slice(2)): Promise<void> {\n const args = parseArgs(argv);\n if (args.command === \"browse\") {\n await runBrowse(args);\n return;\n }\n await runStore(args);\n}\n\nexport function printUsage(): void {\n writeCliOutput(usage());\n}\n\nexport async function runCli(argv: string[] = process.argv.slice(2)): Promise<void> {\n try {\n await main(argv);\n } catch (error) {\n if (error instanceof UsageError) {\n writeCliOutput(formatUsageError(error));\n writeCliOutput();\n printUsage();\n process.exit(1);\n }\n\n console.error(\"access-cli failed\");\n process.exit(1);\n }\n}\n"],"mappings":";;;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AAEA,YAAQ,MAAM,mBAAmB;AACjC,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/access-cli.ts"],"sourcesContent":["import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { parseConfig } from \"./config.js\";\nimport type { PluginConfig } from \"./types.js\";\nimport { Orchestrator } from \"./orchestrator.js\";\nimport { EngramAccessService } from \"./access-service.js\";\nimport { readEnvVar, resolveHomeDir } from \"./runtime/env.js\";\n\ntype CommandName = \"browse\" | \"store\";\n\ntype ParsedArgs = {\n command: CommandName;\n options: Record<string, string[]>;\n flags: Set<string>;\n};\n\ntype Runtime = {\n config: PluginConfig;\n service: EngramAccessService;\n};\n\ntype UsageErrorKind =\n | \"unsupported-command\"\n | \"unexpected-positional\"\n | \"missing-option\"\n | \"missing-content\"\n | \"invalid-integer\"\n | \"invalid-number\";\n\nclass UsageError extends Error {\n constructor(\n readonly kind: UsageErrorKind,\n readonly optionName?: string,\n ) {\n super(\"invalid access-cli arguments\");\n }\n}\n\nfunction formatUsageError(error: UsageError): string {\n switch (error.kind) {\n case \"unsupported-command\":\n return \"unsupported command\";\n case \"unexpected-positional\":\n return \"unexpected positional argument\";\n case \"missing-option\":\n return `missing required option: --${error.optionName ?? \"unknown\"}`;\n case \"missing-content\":\n return \"missing required option: --content or --content-file\";\n case \"invalid-integer\":\n return `invalid integer for --${error.optionName ?? \"unknown\"}`;\n case \"invalid-number\":\n return `invalid number for --${error.optionName ?? \"unknown\"}`;\n }\n}\n\nfunction writeCliOutput(text: string = \"\"): void {\n process.stdout.write(`${text}\\n`);\n}\n\nfunction usage(): string {\n return [\n \"Usage:\",\n \" engram-access browse [options]\",\n \" engram-access store [options]\",\n \"\",\n \"Browse options:\",\n \" --namespace <name>\",\n \" --query <text>\",\n \" --category <name>\",\n \" --status <name>\",\n \" --sort <updated_desc|updated_asc|created_desc|created_asc>\",\n \" --limit <n>\",\n \" --offset <n>\",\n \"\",\n \"Store options:\",\n \" --namespace <name>\",\n \" --session-key <key>\",\n \" --principal <principal>\",\n \" --content <text> | --content-file <path>\",\n \" --category <name>\",\n \" --confidence <0-1>\",\n \" --tag <tag> (repeatable)\",\n \" --entity-ref <ref>\",\n \" --ttl <duration>\",\n \" --source-reason <text>\",\n \" --idempotency-key <key>\",\n \" --dry-run\",\n ].join(\"\\n\");\n}\n\nfunction parseArgs(argv: string[]): ParsedArgs {\n const [commandRaw, ...rest] = argv;\n if (commandRaw !== \"browse\" && commandRaw !== \"store\") {\n throw new UsageError(\"unsupported-command\");\n }\n\n const options: Record<string, string[]> = {};\n const flags = new Set<string>();\n\n for (let i = 0; i < rest.length; i += 1) {\n const token = rest[i];\n if (!token.startsWith(\"--\")) {\n throw new UsageError(\"unexpected-positional\");\n }\n const key = token.slice(2);\n const next = rest[i + 1];\n if (!next || next.startsWith(\"--\")) {\n flags.add(key);\n continue;\n }\n if (!options[key]) {\n options[key] = [];\n }\n options[key].push(next);\n i += 1;\n }\n\n return {\n command: commandRaw,\n options,\n flags,\n };\n}\n\nfunction getLastOption(args: ParsedArgs, name: string): string | undefined {\n const values = args.options[name];\n if (!values || values.length === 0) return undefined;\n return values[values.length - 1];\n}\n\nfunction getAllOptions(args: ParsedArgs, name: string): string[] {\n return args.options[name] ?? [];\n}\n\nfunction requireOption(args: ParsedArgs, name: string): string {\n const value = getLastOption(args, name);\n if (!value || value.trim().length === 0) {\n throw new UsageError(\"missing-option\", name);\n }\n return value;\n}\n\nfunction parseIntegerOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = parseInt(raw, 10);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-integer\", name);\n }\n return value;\n}\n\nfunction parseFloatOption(args: ParsedArgs, name: string): number | undefined {\n const raw = getLastOption(args, name);\n if (!raw) return undefined;\n const value = Number.parseFloat(raw);\n if (!Number.isFinite(value)) {\n throw new UsageError(\"invalid-number\", name);\n }\n return value;\n}\n\nfunction loadPluginConfig(): Record<string, unknown> {\n const configPath =\n readEnvVar(\"OPENCLAW_ENGRAM_CONFIG_PATH\") ||\n readEnvVar(\"OPENCLAW_CONFIG_PATH\") ||\n path.join(resolveHomeDir(), \".openclaw\", \"openclaw.json\");\n const raw = JSON.parse(fs.readFileSync(configPath, \"utf8\"));\n return raw?.plugins?.entries?.[\"openclaw-engram\"]?.config ?? {};\n}\n\nfunction buildRuntime(): Runtime {\n const config = parseConfig(loadPluginConfig());\n return {\n config,\n service: new EngramAccessService(new Orchestrator(config)),\n };\n}\n\nasync function runBrowse(args: ParsedArgs): Promise<void> {\n const { service } = buildRuntime();\n const result = await service.memoryBrowse({\n namespace: getLastOption(args, \"namespace\"),\n query: getLastOption(args, \"query\"),\n category: getLastOption(args, \"category\"),\n status: getLastOption(args, \"status\"),\n sort: getLastOption(args, \"sort\") as \"updated_desc\" | \"updated_asc\" | \"created_desc\" | \"created_asc\" | undefined,\n limit: parseIntegerOption(args, \"limit\"),\n offset: parseIntegerOption(args, \"offset\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nasync function runStore(args: ParsedArgs): Promise<void> {\n const { config, service } = buildRuntime();\n const contentFile = getLastOption(args, \"content-file\");\n const inlineContent = getLastOption(args, \"content\");\n const content = contentFile ? fs.readFileSync(contentFile, \"utf8\") : inlineContent;\n if (!content || content.trim().length === 0) {\n throw new UsageError(\"missing-content\");\n }\n\n const result = await service.memoryStore({\n namespace: getLastOption(args, \"namespace\"),\n sessionKey: getLastOption(args, \"session-key\"),\n authenticatedPrincipal: getLastOption(args, \"principal\") ?? config.agentAccessHttp.principal,\n content,\n category: requireOption(args, \"category\"),\n confidence: parseFloatOption(args, \"confidence\"),\n tags: getAllOptions(args, \"tag\"),\n entityRef: getLastOption(args, \"entity-ref\"),\n ttl: getLastOption(args, \"ttl\"),\n sourceReason: getLastOption(args, \"source-reason\"),\n idempotencyKey: getLastOption(args, \"idempotency-key\"),\n dryRun: args.flags.has(\"dry-run\"),\n });\n console.log(JSON.stringify(result, null, 2));\n}\n\nexport async function main(argv: string[] = process.argv.slice(2)): Promise<void> {\n const args = parseArgs(argv);\n if (args.command === \"browse\") {\n await runBrowse(args);\n return;\n }\n await runStore(args);\n}\n\nexport function printUsage(): void {\n writeCliOutput(usage());\n}\n\nexport async function runCli(argv: string[] = process.argv.slice(2)): Promise<void> {\n try {\n await main(argv);\n } catch (error) {\n if (error instanceof UsageError) {\n writeCliOutput(formatUsageError(error));\n writeCliOutput();\n printUsage();\n process.exit(1);\n }\n\n console.error(\"access-cli failed\");\n process.exit(1);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AA4BjB,IAAM,aAAN,cAAyB,MAAM;AAAA,EAC7B,YACW,MACA,YACT;AACA,UAAM,8BAA8B;AAH3B;AACA;AAAA,EAGX;AAAA,EAJW;AAAA,EACA;AAIb;AAEA,SAAS,iBAAiB,OAA2B;AACnD,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO,8BAA8B,MAAM,cAAc,SAAS;AAAA,IACpE,KAAK;AACH,aAAO;AAAA,IACT,KAAK;AACH,aAAO,yBAAyB,MAAM,cAAc,SAAS;AAAA,IAC/D,KAAK;AACH,aAAO,wBAAwB,MAAM,cAAc,SAAS;AAAA,EAChE;AACF;AAEA,SAAS,eAAe,OAAe,IAAU;AAC/C,UAAQ,OAAO,MAAM,GAAG,IAAI;AAAA,CAAI;AAClC;AAEA,SAAS,QAAgB;AACvB,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA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KAAK,YAAY,UAAU;AAC7B,UAAM,UAAU,IAAI;AACpB;AAAA,EACF;AACA,QAAM,SAAS,IAAI;AACrB;AAEO,SAAS,aAAmB;AACjC,iBAAe,MAAM,CAAC;AACxB;AAEA,eAAsB,OAAO,OAAiB,QAAQ,KAAK,MAAM,CAAC,GAAkB;AAClF,MAAI;AACF,UAAM,KAAK,IAAI;AAAA,EACjB,SAAS,OAAO;AACd,QAAI,iBAAiB,YAAY;AAC/B,qBAAe,iBAAiB,KAAK,CAAC;AACtC,qBAAe;AACf,iBAAW;AACX,cAAQ,KAAK,CAAC;AAAA,IAChB;AAEA,YAAQ,MAAM,mBAAmB;AACjC,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF;","names":[]}
|
package/dist/access-http.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { IncomingMessage } from 'node:http';
|
|
|
2
2
|
import { EngramAccessService } from './access-service.js';
|
|
3
3
|
import './types.js';
|
|
4
4
|
import './explicit-capture.js';
|
|
5
|
-
import './orchestrator-
|
|
5
|
+
import './orchestrator-CIvLFHx3.js';
|
|
6
6
|
import './buffer.js';
|
|
7
7
|
import './storage.js';
|
|
8
8
|
import './memory-projection-store-NxMkbocT.js';
|
package/dist/access-mcp.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Readable, Writable } from 'node:stream';
|
|
|
2
2
|
import { EngramAccessService } from './access-service.js';
|
|
3
3
|
import './types.js';
|
|
4
4
|
import './explicit-capture.js';
|
|
5
|
-
import './orchestrator-
|
|
5
|
+
import './orchestrator-CIvLFHx3.js';
|
|
6
6
|
import './buffer.js';
|
|
7
7
|
import './storage.js';
|
|
8
8
|
import './memory-projection-store-NxMkbocT.js';
|
package/dist/access-schema.d.ts
CHANGED
|
@@ -18,28 +18,28 @@ declare const recallRequestSchema: z.ZodObject<{
|
|
|
18
18
|
includeDebug: z.ZodOptional<z.ZodBoolean>;
|
|
19
19
|
}, "strip", z.ZodTypeAny, {
|
|
20
20
|
query: string;
|
|
21
|
+
sessionKey?: string | undefined;
|
|
21
22
|
namespace?: string | undefined;
|
|
22
23
|
topK?: number | undefined;
|
|
23
|
-
mode?: "
|
|
24
|
-
sessionKey?: string | undefined;
|
|
24
|
+
mode?: "auto" | "no_recall" | "minimal" | "full" | "graph_mode" | undefined;
|
|
25
25
|
includeDebug?: boolean | undefined;
|
|
26
26
|
}, {
|
|
27
27
|
query: string;
|
|
28
|
+
sessionKey?: string | undefined;
|
|
28
29
|
namespace?: string | undefined;
|
|
29
30
|
topK?: number | undefined;
|
|
30
|
-
mode?: "
|
|
31
|
-
sessionKey?: string | undefined;
|
|
31
|
+
mode?: "auto" | "no_recall" | "minimal" | "full" | "graph_mode" | undefined;
|
|
32
32
|
includeDebug?: boolean | undefined;
|
|
33
33
|
}>;
|
|
34
34
|
declare const recallExplainRequestSchema: z.ZodObject<{
|
|
35
35
|
sessionKey: z.ZodOptional<z.ZodString>;
|
|
36
36
|
namespace: z.ZodOptional<z.ZodString>;
|
|
37
37
|
}, "strip", z.ZodTypeAny, {
|
|
38
|
-
namespace?: string | undefined;
|
|
39
38
|
sessionKey?: string | undefined;
|
|
40
|
-
}, {
|
|
41
39
|
namespace?: string | undefined;
|
|
40
|
+
}, {
|
|
42
41
|
sessionKey?: string | undefined;
|
|
42
|
+
namespace?: string | undefined;
|
|
43
43
|
}>;
|
|
44
44
|
declare const observeRequestSchema: z.ZodObject<{
|
|
45
45
|
sessionKey: z.ZodString;
|
|
@@ -47,27 +47,27 @@ declare const observeRequestSchema: z.ZodObject<{
|
|
|
47
47
|
role: z.ZodEnum<["user", "assistant"]>;
|
|
48
48
|
content: z.ZodString;
|
|
49
49
|
}, "strip", z.ZodTypeAny, {
|
|
50
|
-
content: string;
|
|
51
50
|
role: "user" | "assistant";
|
|
52
|
-
}, {
|
|
53
51
|
content: string;
|
|
52
|
+
}, {
|
|
54
53
|
role: "user" | "assistant";
|
|
54
|
+
content: string;
|
|
55
55
|
}>, "many">;
|
|
56
56
|
namespace: z.ZodOptional<z.ZodString>;
|
|
57
57
|
skipExtraction: z.ZodOptional<z.ZodBoolean>;
|
|
58
58
|
}, "strip", z.ZodTypeAny, {
|
|
59
59
|
sessionKey: string;
|
|
60
60
|
messages: {
|
|
61
|
-
content: string;
|
|
62
61
|
role: "user" | "assistant";
|
|
62
|
+
content: string;
|
|
63
63
|
}[];
|
|
64
64
|
namespace?: string | undefined;
|
|
65
65
|
skipExtraction?: boolean | undefined;
|
|
66
66
|
}, {
|
|
67
67
|
sessionKey: string;
|
|
68
68
|
messages: {
|
|
69
|
-
content: string;
|
|
70
69
|
role: "user" | "assistant";
|
|
70
|
+
content: string;
|
|
71
71
|
}[];
|
|
72
72
|
namespace?: string | undefined;
|
|
73
73
|
skipExtraction?: boolean | undefined;
|
|
@@ -87,30 +87,30 @@ declare const memoryStoreRequestSchema: z.ZodObject<{
|
|
|
87
87
|
sourceReason: z.ZodOptional<z.ZodString>;
|
|
88
88
|
}, "strip", z.ZodTypeAny, {
|
|
89
89
|
content: string;
|
|
90
|
+
sessionKey?: string | undefined;
|
|
90
91
|
namespace?: string | undefined;
|
|
92
|
+
schemaVersion?: number | undefined;
|
|
93
|
+
idempotencyKey?: string | undefined;
|
|
94
|
+
dryRun?: boolean | undefined;
|
|
91
95
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
92
96
|
confidence?: number | undefined;
|
|
93
|
-
ttl?: string | undefined;
|
|
94
97
|
tags?: string[] | undefined;
|
|
95
98
|
entityRef?: string | undefined;
|
|
96
|
-
|
|
97
|
-
sessionKey?: string | undefined;
|
|
98
|
-
dryRun?: boolean | undefined;
|
|
99
|
+
ttl?: string | undefined;
|
|
99
100
|
sourceReason?: string | undefined;
|
|
100
|
-
idempotencyKey?: string | undefined;
|
|
101
101
|
}, {
|
|
102
102
|
content: string;
|
|
103
|
+
sessionKey?: string | undefined;
|
|
103
104
|
namespace?: string | undefined;
|
|
105
|
+
schemaVersion?: number | undefined;
|
|
106
|
+
idempotencyKey?: string | undefined;
|
|
107
|
+
dryRun?: boolean | undefined;
|
|
104
108
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
105
109
|
confidence?: number | undefined;
|
|
106
|
-
ttl?: string | undefined;
|
|
107
110
|
tags?: string[] | undefined;
|
|
108
111
|
entityRef?: string | undefined;
|
|
109
|
-
|
|
110
|
-
sessionKey?: string | undefined;
|
|
111
|
-
dryRun?: boolean | undefined;
|
|
112
|
+
ttl?: string | undefined;
|
|
112
113
|
sourceReason?: string | undefined;
|
|
113
|
-
idempotencyKey?: string | undefined;
|
|
114
114
|
}>;
|
|
115
115
|
declare const suggestionSubmitRequestSchema: z.ZodObject<{
|
|
116
116
|
schemaVersion: z.ZodOptional<z.ZodNumber>;
|
|
@@ -127,30 +127,30 @@ declare const suggestionSubmitRequestSchema: z.ZodObject<{
|
|
|
127
127
|
sourceReason: z.ZodOptional<z.ZodString>;
|
|
128
128
|
}, "strip", z.ZodTypeAny, {
|
|
129
129
|
content: string;
|
|
130
|
+
sessionKey?: string | undefined;
|
|
130
131
|
namespace?: string | undefined;
|
|
132
|
+
schemaVersion?: number | undefined;
|
|
133
|
+
idempotencyKey?: string | undefined;
|
|
134
|
+
dryRun?: boolean | undefined;
|
|
131
135
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
132
136
|
confidence?: number | undefined;
|
|
133
|
-
ttl?: string | undefined;
|
|
134
137
|
tags?: string[] | undefined;
|
|
135
138
|
entityRef?: string | undefined;
|
|
136
|
-
|
|
137
|
-
sessionKey?: string | undefined;
|
|
138
|
-
dryRun?: boolean | undefined;
|
|
139
|
+
ttl?: string | undefined;
|
|
139
140
|
sourceReason?: string | undefined;
|
|
140
|
-
idempotencyKey?: string | undefined;
|
|
141
141
|
}, {
|
|
142
142
|
content: string;
|
|
143
|
+
sessionKey?: string | undefined;
|
|
143
144
|
namespace?: string | undefined;
|
|
145
|
+
schemaVersion?: number | undefined;
|
|
146
|
+
idempotencyKey?: string | undefined;
|
|
147
|
+
dryRun?: boolean | undefined;
|
|
144
148
|
category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | undefined;
|
|
145
149
|
confidence?: number | undefined;
|
|
146
|
-
ttl?: string | undefined;
|
|
147
150
|
tags?: string[] | undefined;
|
|
148
151
|
entityRef?: string | undefined;
|
|
149
|
-
|
|
150
|
-
sessionKey?: string | undefined;
|
|
151
|
-
dryRun?: boolean | undefined;
|
|
152
|
+
ttl?: string | undefined;
|
|
152
153
|
sourceReason?: string | undefined;
|
|
153
|
-
idempotencyKey?: string | undefined;
|
|
154
154
|
}>;
|
|
155
155
|
declare const reviewDispositionRequestSchema: z.ZodObject<{
|
|
156
156
|
memoryId: z.ZodString;
|
|
@@ -158,14 +158,14 @@ declare const reviewDispositionRequestSchema: z.ZodObject<{
|
|
|
158
158
|
reasonCode: z.ZodString;
|
|
159
159
|
namespace: z.ZodOptional<z.ZodString>;
|
|
160
160
|
}, "strip", z.ZodTypeAny, {
|
|
161
|
-
status: "active" | "pending_review" | "
|
|
162
|
-
reasonCode: string;
|
|
161
|
+
status: "active" | "pending_review" | "quarantined" | "rejected" | "superseded" | "archived";
|
|
163
162
|
memoryId: string;
|
|
163
|
+
reasonCode: string;
|
|
164
164
|
namespace?: string | undefined;
|
|
165
165
|
}, {
|
|
166
|
-
status: "active" | "pending_review" | "
|
|
167
|
-
reasonCode: string;
|
|
166
|
+
status: "active" | "pending_review" | "quarantined" | "rejected" | "superseded" | "archived";
|
|
168
167
|
memoryId: string;
|
|
168
|
+
reasonCode: string;
|
|
169
169
|
namespace?: string | undefined;
|
|
170
170
|
}>;
|
|
171
171
|
declare const trustZonePromoteRequestSchema: z.ZodObject<{
|
|
@@ -181,17 +181,17 @@ declare const trustZonePromoteRequestSchema: z.ZodObject<{
|
|
|
181
181
|
targetZone: "working" | "trusted";
|
|
182
182
|
promotionReason: string;
|
|
183
183
|
namespace?: string | undefined;
|
|
184
|
-
summary?: string | undefined;
|
|
185
184
|
dryRun?: boolean | undefined;
|
|
186
185
|
recordedAt?: string | undefined;
|
|
186
|
+
summary?: string | undefined;
|
|
187
187
|
}, {
|
|
188
188
|
recordId: string;
|
|
189
189
|
targetZone: "working" | "trusted";
|
|
190
190
|
promotionReason: string;
|
|
191
191
|
namespace?: string | undefined;
|
|
192
|
-
summary?: string | undefined;
|
|
193
192
|
dryRun?: boolean | undefined;
|
|
194
193
|
recordedAt?: string | undefined;
|
|
194
|
+
summary?: string | undefined;
|
|
195
195
|
}>;
|
|
196
196
|
declare const trustZoneDemoSeedRequestSchema: z.ZodObject<{
|
|
197
197
|
scenario: z.ZodOptional<z.ZodString>;
|
|
@@ -216,27 +216,27 @@ declare const lcmSearchRequestSchema: z.ZodObject<{
|
|
|
216
216
|
limit: z.ZodOptional<z.ZodNumber>;
|
|
217
217
|
}, "strip", z.ZodTypeAny, {
|
|
218
218
|
query: string;
|
|
219
|
+
sessionKey?: string | undefined;
|
|
219
220
|
namespace?: string | undefined;
|
|
220
221
|
limit?: number | undefined;
|
|
221
|
-
sessionKey?: string | undefined;
|
|
222
222
|
}, {
|
|
223
223
|
query: string;
|
|
224
|
+
sessionKey?: string | undefined;
|
|
224
225
|
namespace?: string | undefined;
|
|
225
226
|
limit?: number | undefined;
|
|
226
|
-
sessionKey?: string | undefined;
|
|
227
227
|
}>;
|
|
228
228
|
declare const daySummaryRequestSchema: z.ZodObject<{
|
|
229
229
|
memories: z.ZodOptional<z.ZodString>;
|
|
230
230
|
sessionKey: z.ZodOptional<z.ZodString>;
|
|
231
231
|
namespace: z.ZodOptional<z.ZodString>;
|
|
232
232
|
}, "strip", z.ZodTypeAny, {
|
|
233
|
+
sessionKey?: string | undefined;
|
|
233
234
|
namespace?: string | undefined;
|
|
234
235
|
memories?: string | undefined;
|
|
235
|
-
sessionKey?: string | undefined;
|
|
236
236
|
}, {
|
|
237
|
+
sessionKey?: string | undefined;
|
|
237
238
|
namespace?: string | undefined;
|
|
238
239
|
memories?: string | undefined;
|
|
239
|
-
sessionKey?: string | undefined;
|
|
240
240
|
}>;
|
|
241
241
|
type RecallRequest = z.infer<typeof recallRequestSchema>;
|
|
242
242
|
type RecallExplainRequest = z.infer<typeof recallExplainRequestSchema>;
|
package/dist/access-service.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { DaySummaryResult, RecallPlanMode, MemoryFile, MemoryLifecycleEvent, Ent
|
|
|
2
2
|
import { ExplicitCaptureInput } from './explicit-capture.js';
|
|
3
3
|
import { r as readMemoryGovernanceRunArtifact } from './memory-projection-store-NxMkbocT.js';
|
|
4
4
|
import { LastRecallSnapshot } from './recall-state.js';
|
|
5
|
-
import { O as Orchestrator, I as IntentDebugSnapshot, G as GraphRecallSnapshot } from './orchestrator-
|
|
5
|
+
import { O as Orchestrator, I as IntentDebugSnapshot, G as GraphRecallSnapshot } from './orchestrator-CIvLFHx3.js';
|
|
6
6
|
import { TrustZoneStoreStatus, TrustZoneName, TrustZoneRecordKind, TrustZoneSourceClass, TrustZoneProvenanceScore, TrustZonePromotionResult, TrustZoneDemoSeedResult } from './trust-zones.js';
|
|
7
7
|
import 'better-sqlite3';
|
|
8
8
|
import './buffer.js';
|
package/dist/bootstrap.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PluginConfig } from './types.js';
|
|
2
|
-
import { O as Orchestrator } from './orchestrator-
|
|
2
|
+
import { O as Orchestrator } from './orchestrator-CIvLFHx3.js';
|
|
3
3
|
import './buffer.js';
|
|
4
4
|
import './storage.js';
|
|
5
5
|
import './memory-projection-store-NxMkbocT.js';
|
package/dist/calibration.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
FallbackLlmClient
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-XUHI52HK.js";
|
|
4
|
+
import "./chunk-M5ZBBBJI.js";
|
|
5
|
+
import "./chunk-OOSWAUYB.js";
|
|
5
6
|
import "./chunk-Y27UJK6V.js";
|
|
6
7
|
import "./chunk-UZB5KHKX.js";
|
|
7
8
|
import "./chunk-MARWOCVP.js";
|
package/dist/calibration.js.map
CHANGED
|
@@ -1 +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 agentId?: string,\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, agentId },\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 gatewayAgentId?: string;\n}): Promise<CalibrationRule[]> {\n try {\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) {\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, options.gatewayAgentId);\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,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AAE,YAAY;AAAA,MAClC,0BAA0B,YAAY;AAAA,IACxC,CAAC;AAED,QAAI,MAAM,6BAA6B,SAAS,MAAM,iBAAiB,SAAS,MAAM,QAAQ;AAC9F,WAAO;AAAA,EACT,SAAS,OAAO;AACd,QAAI,KAAK,mDAAmD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,EAAE;AACpH,WAAO,CAAC;AAAA,EACV;AACF;AASA,eAAsB,wBAAwB,SAIf;AAC7B,MAAI,CAAC,QAAQ,oBAAoB;AAC/B,WAAO,CAAC;AAAA,EACV;AACA,SAAO,4BAA4B;AAAA,IACjC,WAAW,QAAQ;AAAA,IACnB,eAAe,QAAQ;AAAA,EACzB,CAAC;AACH;AAMA,eAAsB,6BACpB,WAC4B;AAC5B,QAAM,QAAQ,MAAM,qBAAqB,SAAS;AAClD,SAAO,MAAM;AACf;","names":[]}
|
|
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 agentId?: string,\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, agentId },\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 gatewayAgentId?: string;\n}): Promise<CalibrationRule[]> {\n try {\n const llm = new FallbackLlmClient(options.gatewayConfig);\n if (!llm.isAvailable(options.gatewayAgentId)) {\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, options.gatewayAgentId);\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,IAAI,aAAY,oBAAI,KAAK,GAAE,YAAY;AAAA,MACtF,0BAA0B,OAAO,IAAI,6BAA6B,WAAW,IAAI,2BAA2B;AAAA,IAC9G;AAAA,EACF,QAAQ;AACN,WAAO,EAAE,OAAO,CAAC,GAAG,YAAW,oBAAI,KAAK,GAAE,YAAY,GAAG,0BAA0B,EAAE;AAAA,EACvF;AACF;AAEA,eAAe,sBAAsB,WAAmB,OAAwC;AAC9F,QAAM,MAAM,eAAe,SAAS;AACpC,QAAM,MAAM,KAAK,EAAE,WAAW,KAAK,CAAC;AACpC,QAAM,aAAY,oBAAI,KAAK,GAAE,YAAY;AACzC,QAAM,UAAU,qBAAqB,SAAS,GAAG,KAAK,UAAU,OAAO,MAAM,CAAC,GAAG,MAAM;AACzF;AAaA,eAAe,gBAAgB,WAA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,iBAAS,iBAAiB,KAAK;AAC/B,iBAAS,aAAa,KAAK,IAAI,GAAG,SAAS,aAAa,IAAI;AAAA,MAC9D,OAAO;AACL,gBAAQ,IAAI,KAAK,IAAI,IAAI;AAAA,MAC3B;AAAA,IACF;AAEA,UAAM,WAAW,CAAC,GAAG,QAAQ,OAAO,CAAC;AACrC,UAAM,sBAAsB,QAAQ,WAAW;AAAA,MAC7C,OAAO;AAAA,MACP,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,MAClC,0BAA0B,YAAY;AAAA,IACxC,CAAC;AAED,QAAI,MAAM,6BAA6B,SAAS,MAAM,iBAAiB,SAAS,MAAM,QAAQ;AAC9F,WAAO;AAAA,EACT,SAAS,OAAO;AACd,QAAI,KAAK,mDAAmD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,EAAE;AACpH,WAAO,CAAC;AAAA,EACV;AACF;AASA,eAAsB,wBAAwB,SAIf;AAC7B,MAAI,CAAC,QAAQ,oBAAoB;AAC/B,WAAO,CAAC;AAAA,EACV;AACA,SAAO,4BAA4B;AAAA,IACjC,WAAW,QAAQ;AAAA,IACnB,eAAe,QAAQ;AAAA,EACzB,CAAC;AACH;AAMA,eAAsB,6BACpB,WAC4B;AAC5B,QAAM,QAAQ,MAAM,qBAAqB,SAAS;AAClD,SAAO,MAAM;AACf;","names":[]}
|
|
@@ -4,8 +4,9 @@ import {
|
|
|
4
4
|
} from "./chunk-NTTLPF7F.js";
|
|
5
5
|
import {
|
|
6
6
|
FallbackLlmClient
|
|
7
|
-
} from "./chunk-
|
|
8
|
-
import "./chunk-
|
|
7
|
+
} from "./chunk-XUHI52HK.js";
|
|
8
|
+
import "./chunk-M5ZBBBJI.js";
|
|
9
|
+
import "./chunk-OOSWAUYB.js";
|
|
9
10
|
import "./chunk-Y27UJK6V.js";
|
|
10
11
|
import "./chunk-UZB5KHKX.js";
|
|
11
12
|
import "./chunk-M5KEYE5E.js";
|
|
@@ -1 +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 agentId?: string,\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, agentId },\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 gatewayAgentId?: string;\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(options.gatewayAgentId)) {\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, options.gatewayAgentId);\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 gatewayAgentId?: string;\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(options.gatewayAgentId)) return null;\n\n const result = await consolidateWithLlm(context, llm, options.gatewayAgentId);\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,OAAO;AAC5B,QAAI;AACF,YAAM,MAAM,MAAM,aAAa,QAAQ;AACvC,UAAI,SAAS,GAAG,KAAK,OAAO,IAAI,iBAAiB,UAAU;AACzD,gBAAQ,KAAK,GAAwC;AAAA,MACvD;AAAA,IACF,QAAQ;AAAA,IAER;AAAA,EACF;AAEA,SAAO;AACT;AAQA,SAAS,oBACP,cACA,YACA,WAAmB,KACX;AAER,QAAM,YAAY,oBAAI,IAAsC;AAC5D,aAAW,KAAK,cAAc;AAC5B,UAAM,OAAO,UAAU,IAAI,EAAE,UAAU,KAAK,CAAC;AAC7C,SAAK,KAAK,CAAC;AACX,cAAU,IAAI,EAAE,YAAY,IAAI;AAAA,EAClC;AAEA,QAAM,QAAkB,CAAC;AACzB,QAAM,KAAK,2BAA2B,aAAa,MAAM,WAAW,UAAU,IAAI,YAAY;AAC9F,QAAM,KAAK,EAAE;AAGb,aAAW,CAAC,YAAY,YAAY,KAAK,WAAW;AAClD,UAAM,KAAK,gBAAgB,UAAU,EAAE;AACvC,eAAW,KAAK,aAAa,MAAM,GAAG,CAAC,GAAG;AACxC,YAAM,U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AC;AAAA,EACV;AACF;AAMA,eAAsB,kCAAkC,SAM7B;AACzB,MAAI;AACF,UAAM,eAAe,MAAM,oBAAoB,QAAQ,WAAW,QAAQ,wBAAwB;AAClG,QAAI,aAAa,UAAU,QAAQ,mBAAmB,GAAI,QAAO;AAEjE,UAAM,YAAY,iBAAiB,QAAQ,WAAW,QAAQ,wBAAwB;AACtF,UAAM,aAAa,MAAM,eAAe,SAAS;AACjD,UAAM,UAAU,oBAAoB,cAAc,UAAU;AAE5D,UAAM,MAAM,IAAI,kBAAkB,QAAQ,aAAa;AACvD,QAAI,CAAC,IAAI,YAAY,QAAQ,cAAc,EAAG,QAAO;AAErD,UAAM,SAAS,MAAM,mBAAmB,SAAS,KAAK,QAAQ,cAAc;AAC5E,QAAI,OAAO,YAAY,WAAW,KAAK,OAAO,MAAM,WAAW,EAAG,QAAO;AAEzE,UAAM,QAAkB,CAAC,uDAAuD,EAAE;AAElF,eAAW,QAAQ,OAAO,aAAa;AACrC,YAAM,KAAK,KAAK,KAAK,SAAS,EAAE;AAAA,IAClC;AAEA,eAAW,QAAQ,OAAO,OAAO;AAC/B,YAAM,KAAK,KAAK,KAAK,OAAO,EAAE;AAAA,IAChC;AAEA,UAAM,KAAK,EAAE;AACb,WAAO,MAAM,KAAK,IAAI;AAAA,EACxB,SAAS,OAAO;AACd,QAAI,KAAK,kDAAkD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,EAAE;AACnH,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
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 agentId?: string,\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, agentId },\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 gatewayAgentId?: string;\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(options.gatewayAgentId)) {\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, options.gatewayAgentId);\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 gatewayAgentId?: string;\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(options.gatewayAgentId)) return null;\n\n const result = await consolidateWithLlm(context, llm, options.gatewayAgentId);\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,0BAC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AAc,UAAU;AAE5D,UAAM,MAAM,IAAI,kBAAkB,QAAQ,aAAa;AACvD,QAAI,CAAC,IAAI,YAAY,QAAQ,cAAc,EAAG,QAAO;AAErD,UAAM,SAAS,MAAM,mBAAmB,SAAS,KAAK,QAAQ,cAAc;AAC5E,QAAI,OAAO,YAAY,WAAW,KAAK,OAAO,MAAM,WAAW,EAAG,QAAO;AAEzE,UAAM,QAAkB,CAAC,uDAAuD,EAAE;AAElF,eAAW,QAAQ,OAAO,aAAa;AACrC,YAAM,KAAK,KAAK,KAAK,SAAS,EAAE;AAAA,IAClC;AAEA,eAAW,QAAQ,OAAO,OAAO;AAC/B,YAAM,KAAK,KAAK,KAAK,OAAO,EAAE;AAAA,IAChC;AAEA,UAAM,KAAK,EAAE;AACb,WAAO,MAAM,KAAK,IAAI;AAAA,EACxB,SAAS,OAAO;AACd,QAAI,KAAK,kDAAkD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC,EAAE;AACnH,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
} from "./chunk-VEWZZM3H.js";
|
|
26
26
|
import {
|
|
27
27
|
FallbackLlmClient
|
|
28
|
-
} from "./chunk-
|
|
28
|
+
} from "./chunk-XUHI52HK.js";
|
|
29
29
|
import {
|
|
30
30
|
buildChatCompletionTokenLimit,
|
|
31
31
|
shouldAssumeOpenAiChatCompletions
|
|
@@ -2165,4 +2165,4 @@ ${memoryList}` }
|
|
|
2165
2165
|
export {
|
|
2166
2166
|
ExtractionEngine
|
|
2167
2167
|
};
|
|
2168
|
-
//# sourceMappingURL=chunk-
|
|
2168
|
+
//# sourceMappingURL=chunk-6UJQNRIO.js.map
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from "./chunk-MDDAA2AO.js";
|
|
12
12
|
import {
|
|
13
13
|
FallbackLlmClient
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-XUHI52HK.js";
|
|
15
15
|
import {
|
|
16
16
|
extractJsonCandidates
|
|
17
17
|
} from "./chunk-UZB5KHKX.js";
|
|
@@ -618,4 +618,4 @@ ${truncatedConversation}`;
|
|
|
618
618
|
export {
|
|
619
619
|
HourlySummarizer
|
|
620
620
|
};
|
|
621
|
-
//# sourceMappingURL=chunk-
|
|
621
|
+
//# sourceMappingURL=chunk-LP47L3ZX.js.map
|