@caupulican/pi-adaptative 0.80.58 → 0.80.59

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,57 @@
1
+ import type { Usage } from "@caupulican/pi-ai";
2
+ export type StopReason = "stop" | "toolUse" | "aborted" | "error" | string;
3
+ export interface IsolatedCompletionResult {
4
+ text: string;
5
+ usage: Usage;
6
+ stopReason: StopReason;
7
+ }
8
+ export type ReflectionTrigger = "complex" | "corrective" | "session-end" | "none";
9
+ export interface DemandSignals {
10
+ trigger: ReflectionTrigger;
11
+ toolCallCount: number;
12
+ hadCorrection: boolean;
13
+ contextHeadroomPct: number;
14
+ usefulLately: number;
15
+ }
16
+ export interface DemandPlan {
17
+ act: "skip" | "reflect";
18
+ reason: string;
19
+ tokenBudget: number;
20
+ }
21
+ /**
22
+ * Pure zero-I/O heuristic to decide whether the current turn justifies a reflection run
23
+ * and determine the token budget under the cheap-tool net-negative doctrine.
24
+ */
25
+ export declare function decideDemand(signals: DemandSignals): DemandPlan;
26
+ export interface ReflectionInput {
27
+ recentTurnText: string;
28
+ existingMemory: string;
29
+ plan: DemandPlan;
30
+ complete: (systemPrompt: string, userPrompt: string) => Promise<IsolatedCompletionResult>;
31
+ }
32
+ export type ReflectionWrite = {
33
+ kind: "memory_add";
34
+ section: "MEMORY" | "USER";
35
+ text: string;
36
+ } | {
37
+ kind: "memory_replace";
38
+ target: string;
39
+ text: string;
40
+ } | {
41
+ kind: "memory_remove";
42
+ target: string;
43
+ };
44
+ export interface ReflectionResult {
45
+ writes: ReflectionWrite[];
46
+ usage: Usage;
47
+ rationale: string;
48
+ }
49
+ export declare class ReflectionEngine {
50
+ /**
51
+ * Build the reflection prompt, call the injected isolated complete(),
52
+ * parse the response, confront existing memory, and return memory writes.
53
+ * Zero direct I/O.
54
+ */
55
+ reflect(input: ReflectionInput): Promise<ReflectionResult>;
56
+ }
57
+ //# sourceMappingURL=reflection-engine.d.ts.map
@@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AE,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACxD;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7C,MAAM,WAAW,gBAAgB;IAChC,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1B,KAAK,EAAE,KAAK,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,gBAAgB;IAC5B;;;;OAIG;IACG,OAAO,CAAC,KAAK,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAyF/D;CACD","sourcesContent":["import type { Usage } from \"@caupulican/pi-ai\";\n\nexport type StopReason = \"stop\" | \"toolUse\" | \"aborted\" | \"error\" | string;\n\nexport interface IsolatedCompletionResult {\n\ttext: string;\n\tusage: Usage;\n\tstopReason: StopReason;\n}\n\nexport type ReflectionTrigger = \"complex\" | \"corrective\" | \"session-end\" | \"none\";\n\nexport interface DemandSignals {\n\ttrigger: ReflectionTrigger;\n\ttoolCallCount: number;\n\thadCorrection: boolean;\n\tcontextHeadroomPct: number; // 0..100\n\tusefulLately: number; // 0..1 rolling score\n}\n\nexport interface DemandPlan {\n\tact: \"skip\" | \"reflect\";\n\treason: string;\n\ttokenBudget: number;\n}\n\n/**\n * Pure zero-I/O heuristic to decide whether the current turn justifies a reflection run\n * and determine the token budget under the cheap-tool net-negative doctrine.\n */\nexport function decideDemand(signals: DemandSignals): DemandPlan {\n\tif (signals.trigger === \"none\") {\n\t\treturn { act: \"skip\", reason: \"No trigger detected\", tokenBudget: 0 };\n\t}\n\tif (signals.contextHeadroomPct < 10) {\n\t\treturn { act: \"skip\", reason: \"Context headroom is critically low (< 10%)\", tokenBudget: 0 };\n\t}\n\n\t// Dynamic token budget based on headroom (keep reflection bounded between 500 and 1500 tokens)\n\tconst baseBudget = 1000;\n\tconst tokenBudget = Math.max(500, Math.min(1500, Math.round(baseBudget * (signals.contextHeadroomPct / 100))));\n\n\tif (signals.hadCorrection) {\n\t\treturn { act: \"reflect\", reason: \"Correction detected in the turn\", tokenBudget };\n\t}\n\tif (signals.trigger === \"session-end\") {\n\t\treturn { act: \"reflect\", reason: \"Session end reflection triggered\", tokenBudget };\n\t}\n\tif (signals.trigger === \"complex\") {\n\t\tif (signals.toolCallCount >= 3) {\n\t\t\treturn { act: \"reflect\", reason: `Complex turn with ${signals.toolCallCount} tool calls`, tokenBudget };\n\t\t}\n\t}\n\n\treturn { act: \"skip\", reason: \"Signals do not justify reflection overhead\", tokenBudget: 0 };\n}\n\nexport interface ReflectionInput {\n\trecentTurnText: string; // host serializes the just-finished turn\n\texistingMemory: string; // current MEMORY.md + USER.md snapshot\n\tplan: DemandPlan;\n\t// host-injected isolated completion function:\n\tcomplete: (systemPrompt: string, userPrompt: string) => Promise<IsolatedCompletionResult>;\n}\n\nexport type ReflectionWrite =\n\t| { kind: \"memory_add\"; section: \"MEMORY\" | \"USER\"; text: string }\n\t| { kind: \"memory_replace\"; target: string; text: string }\n\t| { kind: \"memory_remove\"; target: string };\n\nexport interface ReflectionResult {\n\twrites: ReflectionWrite[];\n\tusage: Usage;\n\trationale: string;\n}\n\nexport class ReflectionEngine {\n\t/**\n\t * Build the reflection prompt, call the injected isolated complete(),\n\t * parse the response, confront existing memory, and return memory writes.\n\t * Zero direct I/O.\n\t */\n\tasync reflect(input: ReflectionInput): Promise<ReflectionResult> {\n\t\tconst systemPrompt = `You are a reflection engine. Your job is to analyze the recent conversation turn, compare it against the agent's existing memory, and decide if any memory updates are needed.\n\nExisting Memory snapshot:\n${input.existingMemory}\n\nMemory guidelines:\n- \"MEMORY\" is for project facts, configuration, repeatable workflows, and coding findings.\n- \"USER\" is for user preferences, patterns, and style specifications.\n- Avoid duplicate facts. If the fact is already represented, do not add it.\n- CONFRONT existing memory: if the new turn contradicts or updates an existing fact, use \"memory_replace\" or \"memory_remove\" to supersede the old fact rather than blindly appending.\n- Keep memories short, factual, and direct. No fluff.\n\nYou must output your analysis and writes in the following JSON format inside a \\`\\`\\`json\\`\\`\\` code fence:\n{\n \"rationale\": \"Explanation of your reasoning\",\n \"writes\": [\n { \"kind\": \"memory_add\", \"section\": \"MEMORY\" | \"USER\", \"text\": \"New direct fact to append\" },\n { \"kind\": \"memory_replace\", \"target\": \"Exact text substring to replace\", \"text\": \"New replacement text\" },\n { \"kind\": \"memory_remove\", \"target\": \"Exact text substring to remove\" }\n ]\n}\n`;\n\n\t\tconst userPrompt = `Recent turn transcript:\n${input.recentTurnText}\n\nAnalyze this turn and output your memory updates.`;\n\n\t\ttry {\n\t\t\tconst compResult = await input.complete(systemPrompt, userPrompt);\n\t\t\tconst text = compResult.text;\n\n\t\t\tconst jsonMatch = text.match(/```json\\s*([\\s\\S]*?)\\s*```/) || text.match(/{[\\s\\S]*}/);\n\t\t\tif (!jsonMatch) {\n\t\t\t\treturn {\n\t\t\t\t\twrites: [],\n\t\t\t\t\tusage: compResult.usage,\n\t\t\t\t\trationale: `Failed to locate JSON response. Raw text:\\n${text}`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst parsed = JSON.parse(jsonMatch[1] || jsonMatch[0]);\n\t\t\tconst rationale = parsed.rationale || \"\";\n\t\t\tconst writes: ReflectionWrite[] = [];\n\n\t\t\tif (Array.isArray(parsed.writes)) {\n\t\t\t\tfor (const w of parsed.writes) {\n\t\t\t\t\tif (w && typeof w === \"object\") {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tw.kind === \"memory_add\" &&\n\t\t\t\t\t\t\t(w.section === \"MEMORY\" || w.section === \"USER\") &&\n\t\t\t\t\t\t\ttypeof w.text === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_add\", section: w.section, text: w.text });\n\t\t\t\t\t\t} else if (\n\t\t\t\t\t\t\tw.kind === \"memory_replace\" &&\n\t\t\t\t\t\t\ttypeof w.target === \"string\" &&\n\t\t\t\t\t\t\ttypeof w.text === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_replace\", target: w.target, text: w.text });\n\t\t\t\t\t\t} else if (w.kind === \"memory_remove\" && typeof w.target === \"string\") {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_remove\", target: w.target });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twrites,\n\t\t\t\tusage: compResult.usage,\n\t\t\t\trationale,\n\t\t\t};\n\t\t} catch (err) {\n\t\t\t// Zeroed/fallback usage representation\n\t\t\tconst emptyUsage: Usage = {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t};\n\t\t\treturn {\n\t\t\t\twrites: [],\n\t\t\t\tusage: emptyUsage,\n\t\t\t\trationale: `Error during reflection: ${String(err)}`,\n\t\t\t};\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Pure zero-I/O heuristic to decide whether the current turn justifies a reflection run
3
+ * and determine the token budget under the cheap-tool net-negative doctrine.
4
+ */
5
+ export function decideDemand(signals) {
6
+ if (signals.trigger === "none") {
7
+ return { act: "skip", reason: "No trigger detected", tokenBudget: 0 };
8
+ }
9
+ if (signals.contextHeadroomPct < 10) {
10
+ return { act: "skip", reason: "Context headroom is critically low (< 10%)", tokenBudget: 0 };
11
+ }
12
+ // Dynamic token budget based on headroom (keep reflection bounded between 500 and 1500 tokens)
13
+ const baseBudget = 1000;
14
+ const tokenBudget = Math.max(500, Math.min(1500, Math.round(baseBudget * (signals.contextHeadroomPct / 100))));
15
+ if (signals.hadCorrection) {
16
+ return { act: "reflect", reason: "Correction detected in the turn", tokenBudget };
17
+ }
18
+ if (signals.trigger === "session-end") {
19
+ return { act: "reflect", reason: "Session end reflection triggered", tokenBudget };
20
+ }
21
+ if (signals.trigger === "complex") {
22
+ if (signals.toolCallCount >= 3) {
23
+ return { act: "reflect", reason: `Complex turn with ${signals.toolCallCount} tool calls`, tokenBudget };
24
+ }
25
+ }
26
+ return { act: "skip", reason: "Signals do not justify reflection overhead", tokenBudget: 0 };
27
+ }
28
+ export class ReflectionEngine {
29
+ /**
30
+ * Build the reflection prompt, call the injected isolated complete(),
31
+ * parse the response, confront existing memory, and return memory writes.
32
+ * Zero direct I/O.
33
+ */
34
+ async reflect(input) {
35
+ const systemPrompt = `You are a reflection engine. Your job is to analyze the recent conversation turn, compare it against the agent's existing memory, and decide if any memory updates are needed.
36
+
37
+ Existing Memory snapshot:
38
+ ${input.existingMemory}
39
+
40
+ Memory guidelines:
41
+ - "MEMORY" is for project facts, configuration, repeatable workflows, and coding findings.
42
+ - "USER" is for user preferences, patterns, and style specifications.
43
+ - Avoid duplicate facts. If the fact is already represented, do not add it.
44
+ - CONFRONT existing memory: if the new turn contradicts or updates an existing fact, use "memory_replace" or "memory_remove" to supersede the old fact rather than blindly appending.
45
+ - Keep memories short, factual, and direct. No fluff.
46
+
47
+ You must output your analysis and writes in the following JSON format inside a \`\`\`json\`\`\` code fence:
48
+ {
49
+ "rationale": "Explanation of your reasoning",
50
+ "writes": [
51
+ { "kind": "memory_add", "section": "MEMORY" | "USER", "text": "New direct fact to append" },
52
+ { "kind": "memory_replace", "target": "Exact text substring to replace", "text": "New replacement text" },
53
+ { "kind": "memory_remove", "target": "Exact text substring to remove" }
54
+ ]
55
+ }
56
+ `;
57
+ const userPrompt = `Recent turn transcript:
58
+ ${input.recentTurnText}
59
+
60
+ Analyze this turn and output your memory updates.`;
61
+ try {
62
+ const compResult = await input.complete(systemPrompt, userPrompt);
63
+ const text = compResult.text;
64
+ const jsonMatch = text.match(/```json\s*([\s\S]*?)\s*```/) || text.match(/{[\s\S]*}/);
65
+ if (!jsonMatch) {
66
+ return {
67
+ writes: [],
68
+ usage: compResult.usage,
69
+ rationale: `Failed to locate JSON response. Raw text:\n${text}`,
70
+ };
71
+ }
72
+ const parsed = JSON.parse(jsonMatch[1] || jsonMatch[0]);
73
+ const rationale = parsed.rationale || "";
74
+ const writes = [];
75
+ if (Array.isArray(parsed.writes)) {
76
+ for (const w of parsed.writes) {
77
+ if (w && typeof w === "object") {
78
+ if (w.kind === "memory_add" &&
79
+ (w.section === "MEMORY" || w.section === "USER") &&
80
+ typeof w.text === "string") {
81
+ writes.push({ kind: "memory_add", section: w.section, text: w.text });
82
+ }
83
+ else if (w.kind === "memory_replace" &&
84
+ typeof w.target === "string" &&
85
+ typeof w.text === "string") {
86
+ writes.push({ kind: "memory_replace", target: w.target, text: w.text });
87
+ }
88
+ else if (w.kind === "memory_remove" && typeof w.target === "string") {
89
+ writes.push({ kind: "memory_remove", target: w.target });
90
+ }
91
+ }
92
+ }
93
+ }
94
+ return {
95
+ writes,
96
+ usage: compResult.usage,
97
+ rationale,
98
+ };
99
+ }
100
+ catch (err) {
101
+ // Zeroed/fallback usage representation
102
+ const emptyUsage = {
103
+ input: 0,
104
+ output: 0,
105
+ cacheRead: 0,
106
+ cacheWrite: 0,
107
+ totalTokens: 0,
108
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
109
+ };
110
+ return {
111
+ writes: [],
112
+ usage: emptyUsage,
113
+ rationale: `Error during reflection: ${String(err)}`,
114
+ };
115
+ }
116
+ }
117
+ }
118
+ //# sourceMappingURL=reflection-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reflection-engine.js","sourceRoot":"","sources":["../../../src/core/learning/reflection-engine.ts"],"names":[],"mappings":"AA0BA;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,OAAsB,EAAc;IAChE,IAAI,OAAO,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;QAChC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,qBAAqB,EAAE,WAAW,EAA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import type { Usage } from \"@caupulican/pi-ai\";\n\nexport type StopReason = \"stop\" | \"toolUse\" | \"aborted\" | \"error\" | string;\n\nexport interface IsolatedCompletionResult {\n\ttext: string;\n\tusage: Usage;\n\tstopReason: StopReason;\n}\n\nexport type ReflectionTrigger = \"complex\" | \"corrective\" | \"session-end\" | \"none\";\n\nexport interface DemandSignals {\n\ttrigger: ReflectionTrigger;\n\ttoolCallCount: number;\n\thadCorrection: boolean;\n\tcontextHeadroomPct: number; // 0..100\n\tusefulLately: number; // 0..1 rolling score\n}\n\nexport interface DemandPlan {\n\tact: \"skip\" | \"reflect\";\n\treason: string;\n\ttokenBudget: number;\n}\n\n/**\n * Pure zero-I/O heuristic to decide whether the current turn justifies a reflection run\n * and determine the token budget under the cheap-tool net-negative doctrine.\n */\nexport function decideDemand(signals: DemandSignals): DemandPlan {\n\tif (signals.trigger === \"none\") {\n\t\treturn { act: \"skip\", reason: \"No trigger detected\", tokenBudget: 0 };\n\t}\n\tif (signals.contextHeadroomPct < 10) {\n\t\treturn { act: \"skip\", reason: \"Context headroom is critically low (< 10%)\", tokenBudget: 0 };\n\t}\n\n\t// Dynamic token budget based on headroom (keep reflection bounded between 500 and 1500 tokens)\n\tconst baseBudget = 1000;\n\tconst tokenBudget = Math.max(500, Math.min(1500, Math.round(baseBudget * (signals.contextHeadroomPct / 100))));\n\n\tif (signals.hadCorrection) {\n\t\treturn { act: \"reflect\", reason: \"Correction detected in the turn\", tokenBudget };\n\t}\n\tif (signals.trigger === \"session-end\") {\n\t\treturn { act: \"reflect\", reason: \"Session end reflection triggered\", tokenBudget };\n\t}\n\tif (signals.trigger === \"complex\") {\n\t\tif (signals.toolCallCount >= 3) {\n\t\t\treturn { act: \"reflect\", reason: `Complex turn with ${signals.toolCallCount} tool calls`, tokenBudget };\n\t\t}\n\t}\n\n\treturn { act: \"skip\", reason: \"Signals do not justify reflection overhead\", tokenBudget: 0 };\n}\n\nexport interface ReflectionInput {\n\trecentTurnText: string; // host serializes the just-finished turn\n\texistingMemory: string; // current MEMORY.md + USER.md snapshot\n\tplan: DemandPlan;\n\t// host-injected isolated completion function:\n\tcomplete: (systemPrompt: string, userPrompt: string) => Promise<IsolatedCompletionResult>;\n}\n\nexport type ReflectionWrite =\n\t| { kind: \"memory_add\"; section: \"MEMORY\" | \"USER\"; text: string }\n\t| { kind: \"memory_replace\"; target: string; text: string }\n\t| { kind: \"memory_remove\"; target: string };\n\nexport interface ReflectionResult {\n\twrites: ReflectionWrite[];\n\tusage: Usage;\n\trationale: string;\n}\n\nexport class ReflectionEngine {\n\t/**\n\t * Build the reflection prompt, call the injected isolated complete(),\n\t * parse the response, confront existing memory, and return memory writes.\n\t * Zero direct I/O.\n\t */\n\tasync reflect(input: ReflectionInput): Promise<ReflectionResult> {\n\t\tconst systemPrompt = `You are a reflection engine. Your job is to analyze the recent conversation turn, compare it against the agent's existing memory, and decide if any memory updates are needed.\n\nExisting Memory snapshot:\n${input.existingMemory}\n\nMemory guidelines:\n- \"MEMORY\" is for project facts, configuration, repeatable workflows, and coding findings.\n- \"USER\" is for user preferences, patterns, and style specifications.\n- Avoid duplicate facts. If the fact is already represented, do not add it.\n- CONFRONT existing memory: if the new turn contradicts or updates an existing fact, use \"memory_replace\" or \"memory_remove\" to supersede the old fact rather than blindly appending.\n- Keep memories short, factual, and direct. No fluff.\n\nYou must output your analysis and writes in the following JSON format inside a \\`\\`\\`json\\`\\`\\` code fence:\n{\n \"rationale\": \"Explanation of your reasoning\",\n \"writes\": [\n { \"kind\": \"memory_add\", \"section\": \"MEMORY\" | \"USER\", \"text\": \"New direct fact to append\" },\n { \"kind\": \"memory_replace\", \"target\": \"Exact text substring to replace\", \"text\": \"New replacement text\" },\n { \"kind\": \"memory_remove\", \"target\": \"Exact text substring to remove\" }\n ]\n}\n`;\n\n\t\tconst userPrompt = `Recent turn transcript:\n${input.recentTurnText}\n\nAnalyze this turn and output your memory updates.`;\n\n\t\ttry {\n\t\t\tconst compResult = await input.complete(systemPrompt, userPrompt);\n\t\t\tconst text = compResult.text;\n\n\t\t\tconst jsonMatch = text.match(/```json\\s*([\\s\\S]*?)\\s*```/) || text.match(/{[\\s\\S]*}/);\n\t\t\tif (!jsonMatch) {\n\t\t\t\treturn {\n\t\t\t\t\twrites: [],\n\t\t\t\t\tusage: compResult.usage,\n\t\t\t\t\trationale: `Failed to locate JSON response. Raw text:\\n${text}`,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst parsed = JSON.parse(jsonMatch[1] || jsonMatch[0]);\n\t\t\tconst rationale = parsed.rationale || \"\";\n\t\t\tconst writes: ReflectionWrite[] = [];\n\n\t\t\tif (Array.isArray(parsed.writes)) {\n\t\t\t\tfor (const w of parsed.writes) {\n\t\t\t\t\tif (w && typeof w === \"object\") {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tw.kind === \"memory_add\" &&\n\t\t\t\t\t\t\t(w.section === \"MEMORY\" || w.section === \"USER\") &&\n\t\t\t\t\t\t\ttypeof w.text === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_add\", section: w.section, text: w.text });\n\t\t\t\t\t\t} else if (\n\t\t\t\t\t\t\tw.kind === \"memory_replace\" &&\n\t\t\t\t\t\t\ttypeof w.target === \"string\" &&\n\t\t\t\t\t\t\ttypeof w.text === \"string\"\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_replace\", target: w.target, text: w.text });\n\t\t\t\t\t\t} else if (w.kind === \"memory_remove\" && typeof w.target === \"string\") {\n\t\t\t\t\t\t\twrites.push({ kind: \"memory_remove\", target: w.target });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twrites,\n\t\t\t\tusage: compResult.usage,\n\t\t\t\trationale,\n\t\t\t};\n\t\t} catch (err) {\n\t\t\t// Zeroed/fallback usage representation\n\t\t\tconst emptyUsage: Usage = {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t};\n\t\t\treturn {\n\t\t\t\twrites: [],\n\t\t\t\tusage: emptyUsage,\n\t\t\t\trationale: `Error during reflection: ${String(err)}`,\n\t\t\t};\n\t\t}\n\t}\n}\n"]}
@@ -10,6 +10,14 @@ export declare class MemoryManager {
10
10
  registerProvider(p: MemoryProvider): void;
11
11
  initializeAll(sessionId: string, ctx: MemoryLifecycleContext): Promise<void>;
12
12
  buildSystemPromptBlock(): string;
13
+ /**
14
+ * Compose the memory block freshly from the providers, BYPASSING the frozen cache used by the
15
+ * system prompt. Used by end-of-loop reflection so its confront-before-write sees the live memory
16
+ * (including writes made earlier in the same session) without churning the prefix-cache-stable
17
+ * system prompt block.
18
+ */
19
+ buildSystemPromptBlockFresh(): string;
20
+ private _composeSystemPromptBlock;
13
21
  prefetch(query: string): Promise<string>;
14
22
  syncTurn(user: string, assistant: string): Promise<void>;
15
23
  onPreCompress(): Promise<string>;
@@ -1 +1 @@
1
- {"version":3,"file":"memory-manager.d.ts","sourceRoot":"","sources":["../../../src/core/memory/memory-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,KAAK,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEnF,qBAAa,aAAa;IACzB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAwB;IAClD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA0B;IAC1D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA0B;IAC9D,OAAO,CAAC,GAAG,CAAC,CAAyB;IACrC,OAAO,CAAC,sBAAsB,CAAC,CAAS;IAGxC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,wBAAwB,CAY7C;IAEI,gBAAgB,CAAC,CAAC,EAAE,cAAc,GAAG,IAAI,CA8B/C;IAEY,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkBxF;IAEM,sBAAsB,IAAI,MAAM,CAsBtC;IAEY,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAgBpD;IAEY,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAepE;IAEY,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC,CAgB5C;IAEY,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC,CAezC;IAEY,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAcxC;IAEM,kBAAkB,IAAI,cAAc,EAAE,CAa5C;IAEM,iBAAiB,IAAI,MAAM,EAAE,CAgBnC;IAEM,KAAK,IAAI,IAAI,CAMnB;CACD","sourcesContent":["import type { ToolDefinition } from \"../extensions/types.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"./memory-provider.ts\";\n\nexport class MemoryManager {\n\tprivate readonly providers: MemoryProvider[] = [];\n\tprivate readonly activeProviders: Set<string> = new Set();\n\tprivate readonly registeredToolNames: Set<string> = new Set();\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate systemPromptBlockCache?: string;\n\n\t// Core reserved tool names to prevent hijacking or schema corruption.\n\tprivate static readonly RESERVED_CORE_TOOL_NAMES = new Set([\n\t\t\"read\",\n\t\t\"write\",\n\t\t\"grep\",\n\t\t\"find\",\n\t\t\"ls\",\n\t\t\"bash\",\n\t\t\"ask_user\",\n\t\t\"skillify\",\n\t\t\"skill_audit\",\n\t\t\"skill_search\",\n\t\t\"skill_open\",\n\t]);\n\n\tpublic registerProvider(p: MemoryProvider): void {\n\t\tif (this.providers.some((prov) => prov.name === p.name)) {\n\t\t\tthrow new Error(`Memory provider ${p.name} is already registered.`);\n\t\t}\n\n\t\tif (p.getToolDefinitions) {\n\t\t\tlet tools: ToolDefinition[] = [];\n\t\t\ttry {\n\t\t\t\ttools = p.getToolDefinitions() ?? [];\n\t\t\t} catch (err) {\n\t\t\t\tthrow new Error(`Failed to get tool definitions from provider ${p.name}: ${String(err)}`);\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tif (MemoryManager.RESERVED_CORE_TOOL_NAMES.has(tool.name)) {\n\t\t\t\t\tthrow new Error(`Memory provider ${p.name} tried to register reserved core tool: ${tool.name}`);\n\t\t\t\t}\n\t\t\t\tif (this.registeredToolNames.has(tool.name)) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Memory provider tool name collision: ${tool.name} is already registered. First-registration-wins.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tthis.registeredToolNames.add(tool.name);\n\t\t\t}\n\t\t}\n\n\t\tthis.providers.push(p);\n\t}\n\n\tpublic async initializeAll(sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.activeProviders.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\n\t\tfor (const p of this.providers) {\n\t\t\ttry {\n\t\t\t\tconst available = await p.isAvailable();\n\t\t\t\tif (!available) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tawait p.initialize(sessionId, ctx);\n\t\t\t\tthis.activeProviders.add(p.name);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to initialize and was deactivated:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic buildSystemPromptBlock(): string {\n\t\tif (this.systemPromptBlockCache !== undefined) {\n\t\t\treturn this.systemPromptBlockCache;\n\t\t}\n\n\t\tconst blocks: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.systemPromptBlock) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst block = p.systemPromptBlock();\n\t\t\t\tif (block) {\n\t\t\t\t\tblocks.push(block);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to generate system prompt block:`, err);\n\t\t\t}\n\t\t}\n\n\t\tthis.systemPromptBlockCache = blocks.join(\"\\n\\n\");\n\t\treturn this.systemPromptBlockCache;\n\t}\n\n\tpublic async prefetch(query: string): Promise<string> {\n\t\tconst results: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.prefetch) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst text = await p.prefetch(query);\n\t\t\t\tif (text) {\n\t\t\t\t\tresults.push(text);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during prefetch:`, err);\n\t\t\t}\n\t\t}\n\t\treturn results.join(\"\\n\\n\");\n\t}\n\n\tpublic async syncTurn(user: string, assistant: string): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.syncTurn) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.syncTurn(user, assistant);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during syncTurn:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async onPreCompress(): Promise<string> {\n\t\tconst insights: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onPreCompress) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst insight = await p.onPreCompress();\n\t\t\t\tif (insight) {\n\t\t\t\t\tinsights.push(insight);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onPreCompress:`, err);\n\t\t\t}\n\t\t}\n\t\treturn insights.join(\"\\n\\n\");\n\t}\n\n\tpublic async onSessionEnd(): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onSessionEnd) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.onSessionEnd();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onSessionEnd:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async shutdownAll(): Promise<void> {\n\t\t// Shutdown in reverse registration order\n\t\tconst reversed = [...this.providers].reverse();\n\t\tfor (const p of reversed) {\n\t\t\tif (!this.activeProviders.has(p.name)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.shutdown();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to shutdown cleanly:`, err);\n\t\t\t}\n\t\t}\n\t\tthis.activeProviders.clear();\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\tconst tools: ToolDefinition[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getToolDefinitions) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\ttools.push(...p.getToolDefinitions());\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get tool definitions from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn tools;\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\tconst markers = new Set<string>();\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getContextMarkers) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst list = p.getContextMarkers() ?? [];\n\t\t\t\tfor (const m of list) {\n\t\t\t\t\tmarkers.add(m);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get context markers from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn [...markers];\n\t}\n\n\tpublic reset(): void {\n\t\tthis.providers.length = 0;\n\t\tthis.activeProviders.clear();\n\t\tthis.registeredToolNames.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\t\tthis.ctx = undefined;\n\t}\n}\n"]}
1
+ {"version":3,"file":"memory-manager.d.ts","sourceRoot":"","sources":["../../../src/core/memory/memory-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,KAAK,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEnF,qBAAa,aAAa;IACzB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAwB;IAClD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA0B;IAC1D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA0B;IAC9D,OAAO,CAAC,GAAG,CAAC,CAAyB;IACrC,OAAO,CAAC,sBAAsB,CAAC,CAAS;IAGxC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,wBAAwB,CAY7C;IAEI,gBAAgB,CAAC,CAAC,EAAE,cAAc,GAAG,IAAI,CA8B/C;IAEY,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC,CAkBxF;IAEM,sBAAsB,IAAI,MAAM,CAOtC;IAED;;;;;OAKG;IACI,2BAA2B,IAAI,MAAM,CAE3C;IAED,OAAO,CAAC,yBAAyB;IAkBpB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAgBpD;IAEY,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAepE;IAEY,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC,CAgB5C;IAEY,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC,CAezC;IAEY,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAcxC;IAEM,kBAAkB,IAAI,cAAc,EAAE,CAa5C;IAEM,iBAAiB,IAAI,MAAM,EAAE,CAgBnC;IAEM,KAAK,IAAI,IAAI,CAMnB;CACD","sourcesContent":["import type { ToolDefinition } from \"../extensions/types.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"./memory-provider.ts\";\n\nexport class MemoryManager {\n\tprivate readonly providers: MemoryProvider[] = [];\n\tprivate readonly activeProviders: Set<string> = new Set();\n\tprivate readonly registeredToolNames: Set<string> = new Set();\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate systemPromptBlockCache?: string;\n\n\t// Core reserved tool names to prevent hijacking or schema corruption.\n\tprivate static readonly RESERVED_CORE_TOOL_NAMES = new Set([\n\t\t\"read\",\n\t\t\"write\",\n\t\t\"grep\",\n\t\t\"find\",\n\t\t\"ls\",\n\t\t\"bash\",\n\t\t\"ask_user\",\n\t\t\"skillify\",\n\t\t\"skill_audit\",\n\t\t\"skill_search\",\n\t\t\"skill_open\",\n\t]);\n\n\tpublic registerProvider(p: MemoryProvider): void {\n\t\tif (this.providers.some((prov) => prov.name === p.name)) {\n\t\t\tthrow new Error(`Memory provider ${p.name} is already registered.`);\n\t\t}\n\n\t\tif (p.getToolDefinitions) {\n\t\t\tlet tools: ToolDefinition[] = [];\n\t\t\ttry {\n\t\t\t\ttools = p.getToolDefinitions() ?? [];\n\t\t\t} catch (err) {\n\t\t\t\tthrow new Error(`Failed to get tool definitions from provider ${p.name}: ${String(err)}`);\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tif (MemoryManager.RESERVED_CORE_TOOL_NAMES.has(tool.name)) {\n\t\t\t\t\tthrow new Error(`Memory provider ${p.name} tried to register reserved core tool: ${tool.name}`);\n\t\t\t\t}\n\t\t\t\tif (this.registeredToolNames.has(tool.name)) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Memory provider tool name collision: ${tool.name} is already registered. First-registration-wins.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tthis.registeredToolNames.add(tool.name);\n\t\t\t}\n\t\t}\n\n\t\tthis.providers.push(p);\n\t}\n\n\tpublic async initializeAll(sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.activeProviders.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\n\t\tfor (const p of this.providers) {\n\t\t\ttry {\n\t\t\t\tconst available = await p.isAvailable();\n\t\t\t\tif (!available) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tawait p.initialize(sessionId, ctx);\n\t\t\t\tthis.activeProviders.add(p.name);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to initialize and was deactivated:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic buildSystemPromptBlock(): string {\n\t\tif (this.systemPromptBlockCache !== undefined) {\n\t\t\treturn this.systemPromptBlockCache;\n\t\t}\n\n\t\tthis.systemPromptBlockCache = this._composeSystemPromptBlock();\n\t\treturn this.systemPromptBlockCache;\n\t}\n\n\t/**\n\t * Compose the memory block freshly from the providers, BYPASSING the frozen cache used by the\n\t * system prompt. Used by end-of-loop reflection so its confront-before-write sees the live memory\n\t * (including writes made earlier in the same session) without churning the prefix-cache-stable\n\t * system prompt block.\n\t */\n\tpublic buildSystemPromptBlockFresh(): string {\n\t\treturn this._composeSystemPromptBlock();\n\t}\n\n\tprivate _composeSystemPromptBlock(): string {\n\t\tconst blocks: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.systemPromptBlock) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst block = p.systemPromptBlock();\n\t\t\t\tif (block) {\n\t\t\t\t\tblocks.push(block);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to generate system prompt block:`, err);\n\t\t\t}\n\t\t}\n\t\treturn blocks.join(\"\\n\\n\");\n\t}\n\n\tpublic async prefetch(query: string): Promise<string> {\n\t\tconst results: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.prefetch) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst text = await p.prefetch(query);\n\t\t\t\tif (text) {\n\t\t\t\t\tresults.push(text);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during prefetch:`, err);\n\t\t\t}\n\t\t}\n\t\treturn results.join(\"\\n\\n\");\n\t}\n\n\tpublic async syncTurn(user: string, assistant: string): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.syncTurn) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.syncTurn(user, assistant);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during syncTurn:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async onPreCompress(): Promise<string> {\n\t\tconst insights: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onPreCompress) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst insight = await p.onPreCompress();\n\t\t\t\tif (insight) {\n\t\t\t\t\tinsights.push(insight);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onPreCompress:`, err);\n\t\t\t}\n\t\t}\n\t\treturn insights.join(\"\\n\\n\");\n\t}\n\n\tpublic async onSessionEnd(): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onSessionEnd) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.onSessionEnd();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onSessionEnd:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async shutdownAll(): Promise<void> {\n\t\t// Shutdown in reverse registration order\n\t\tconst reversed = [...this.providers].reverse();\n\t\tfor (const p of reversed) {\n\t\t\tif (!this.activeProviders.has(p.name)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.shutdown();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to shutdown cleanly:`, err);\n\t\t\t}\n\t\t}\n\t\tthis.activeProviders.clear();\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\tconst tools: ToolDefinition[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getToolDefinitions) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\ttools.push(...p.getToolDefinitions());\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get tool definitions from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn tools;\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\tconst markers = new Set<string>();\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getContextMarkers) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst list = p.getContextMarkers() ?? [];\n\t\t\t\tfor (const m of list) {\n\t\t\t\t\tmarkers.add(m);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get context markers from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn [...markers];\n\t}\n\n\tpublic reset(): void {\n\t\tthis.providers.length = 0;\n\t\tthis.activeProviders.clear();\n\t\tthis.registeredToolNames.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\t\tthis.ctx = undefined;\n\t}\n}\n"]}
@@ -66,6 +66,19 @@ export class MemoryManager {
66
66
  if (this.systemPromptBlockCache !== undefined) {
67
67
  return this.systemPromptBlockCache;
68
68
  }
69
+ this.systemPromptBlockCache = this._composeSystemPromptBlock();
70
+ return this.systemPromptBlockCache;
71
+ }
72
+ /**
73
+ * Compose the memory block freshly from the providers, BYPASSING the frozen cache used by the
74
+ * system prompt. Used by end-of-loop reflection so its confront-before-write sees the live memory
75
+ * (including writes made earlier in the same session) without churning the prefix-cache-stable
76
+ * system prompt block.
77
+ */
78
+ buildSystemPromptBlockFresh() {
79
+ return this._composeSystemPromptBlock();
80
+ }
81
+ _composeSystemPromptBlock() {
69
82
  const blocks = [];
70
83
  for (const p of this.providers) {
71
84
  if (!this.activeProviders.has(p.name) || !p.systemPromptBlock) {
@@ -81,8 +94,7 @@ export class MemoryManager {
81
94
  console.error(`Memory provider ${p.name} failed to generate system prompt block:`, err);
82
95
  }
83
96
  }
84
- this.systemPromptBlockCache = blocks.join("\n\n");
85
- return this.systemPromptBlockCache;
97
+ return blocks.join("\n\n");
86
98
  }
87
99
  async prefetch(query) {
88
100
  const results = [];
@@ -1 +1 @@
1
- {"version":3,"file":"memory-manager.js","sourceRoot":"","sources":["../../../src/core/memory/memory-manager.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,aAAa;IACR,SAAS,GAAqB,EAAE,CAAC;IACjC,eAAe,GAAgB,IAAI,GAAG,EAAE,C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AK,GAAS;QACpB,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;QAC1B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE,CAAC;QACjC,IAAI,CAAC,sBAAsB,GAAG,SAAS,CAAC;QACxC,IAAI,CAAC,GAAG,GAAG,SAAS,CAAC;IAAA,CACrB;CACD","sourcesContent":["import type { ToolDefinition } from \"../extensions/types.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"./memory-provider.ts\";\n\nexport class MemoryManager {\n\tprivate readonly providers: MemoryProvider[] = [];\n\tprivate readonly activeProviders: Set<string> = new Set();\n\tprivate readonly registeredToolNames: Set<string> = new Set();\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate systemPromptBlockCache?: string;\n\n\t// Core reserved tool names to prevent hijacking or schema corruption.\n\tprivate static readonly RESERVED_CORE_TOOL_NAMES = new Set([\n\t\t\"read\",\n\t\t\"write\",\n\t\t\"grep\",\n\t\t\"find\",\n\t\t\"ls\",\n\t\t\"bash\",\n\t\t\"ask_user\",\n\t\t\"skillify\",\n\t\t\"skill_audit\",\n\t\t\"skill_search\",\n\t\t\"skill_open\",\n\t]);\n\n\tpublic registerProvider(p: MemoryProvider): void {\n\t\tif (this.providers.some((prov) => prov.name === p.name)) {\n\t\t\tthrow new Error(`Memory provider ${p.name} is already registered.`);\n\t\t}\n\n\t\tif (p.getToolDefinitions) {\n\t\t\tlet tools: ToolDefinition[] = [];\n\t\t\ttry {\n\t\t\t\ttools = p.getToolDefinitions() ?? [];\n\t\t\t} catch (err) {\n\t\t\t\tthrow new Error(`Failed to get tool definitions from provider ${p.name}: ${String(err)}`);\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tif (MemoryManager.RESERVED_CORE_TOOL_NAMES.has(tool.name)) {\n\t\t\t\t\tthrow new Error(`Memory provider ${p.name} tried to register reserved core tool: ${tool.name}`);\n\t\t\t\t}\n\t\t\t\tif (this.registeredToolNames.has(tool.name)) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Memory provider tool name collision: ${tool.name} is already registered. First-registration-wins.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tthis.registeredToolNames.add(tool.name);\n\t\t\t}\n\t\t}\n\n\t\tthis.providers.push(p);\n\t}\n\n\tpublic async initializeAll(sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.activeProviders.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\n\t\tfor (const p of this.providers) {\n\t\t\ttry {\n\t\t\t\tconst available = await p.isAvailable();\n\t\t\t\tif (!available) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tawait p.initialize(sessionId, ctx);\n\t\t\t\tthis.activeProviders.add(p.name);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to initialize and was deactivated:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic buildSystemPromptBlock(): string {\n\t\tif (this.systemPromptBlockCache !== undefined) {\n\t\t\treturn this.systemPromptBlockCache;\n\t\t}\n\n\t\tconst blocks: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.systemPromptBlock) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst block = p.systemPromptBlock();\n\t\t\t\tif (block) {\n\t\t\t\t\tblocks.push(block);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to generate system prompt block:`, err);\n\t\t\t}\n\t\t}\n\n\t\tthis.systemPromptBlockCache = blocks.join(\"\\n\\n\");\n\t\treturn this.systemPromptBlockCache;\n\t}\n\n\tpublic async prefetch(query: string): Promise<string> {\n\t\tconst results: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.prefetch) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst text = await p.prefetch(query);\n\t\t\t\tif (text) {\n\t\t\t\t\tresults.push(text);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during prefetch:`, err);\n\t\t\t}\n\t\t}\n\t\treturn results.join(\"\\n\\n\");\n\t}\n\n\tpublic async syncTurn(user: string, assistant: string): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.syncTurn) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.syncTurn(user, assistant);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during syncTurn:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async onPreCompress(): Promise<string> {\n\t\tconst insights: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onPreCompress) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst insight = await p.onPreCompress();\n\t\t\t\tif (insight) {\n\t\t\t\t\tinsights.push(insight);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onPreCompress:`, err);\n\t\t\t}\n\t\t}\n\t\treturn insights.join(\"\\n\\n\");\n\t}\n\n\tpublic async onSessionEnd(): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onSessionEnd) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.onSessionEnd();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onSessionEnd:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async shutdownAll(): Promise<void> {\n\t\t// Shutdown in reverse registration order\n\t\tconst reversed = [...this.providers].reverse();\n\t\tfor (const p of reversed) {\n\t\t\tif (!this.activeProviders.has(p.name)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.shutdown();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to shutdown cleanly:`, err);\n\t\t\t}\n\t\t}\n\t\tthis.activeProviders.clear();\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\tconst tools: ToolDefinition[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getToolDefinitions) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\ttools.push(...p.getToolDefinitions());\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get tool definitions from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn tools;\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\tconst markers = new Set<string>();\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getContextMarkers) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst list = p.getContextMarkers() ?? [];\n\t\t\t\tfor (const m of list) {\n\t\t\t\t\tmarkers.add(m);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get context markers from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn [...markers];\n\t}\n\n\tpublic reset(): void {\n\t\tthis.providers.length = 0;\n\t\tthis.activeProviders.clear();\n\t\tthis.registeredToolNames.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\t\tthis.ctx = undefined;\n\t}\n}\n"]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AAE,IAAI,EAAE,CAAC;gBACzC,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;oBACtB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBAChB,CAAC;YACF,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,OAAO,CAAC,KAAK,CAAC,+CAA+C,CAAC,CAAC,IAAI,GAAG,EAAE,GAAG,CAAC,CAAC;YAC9E,CAAC;QACF,CAAC;QACD,OAAO,CAAC,GAAG,OAAO,CAAC,CAAC;IAAA,CACpB;IAEM,KAAK,GAAS;QACpB,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;QAC1B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,mBAAmB,CAAC,KAAK,EAAE,CAAC;QACjC,IAAI,CAAC,sBAAsB,GAAG,SAAS,CAAC;QACxC,IAAI,CAAC,GAAG,GAAG,SAAS,CAAC;IAAA,CACrB;CACD","sourcesContent":["import type { ToolDefinition } from \"../extensions/types.ts\";\nimport type { MemoryLifecycleContext, MemoryProvider } from \"./memory-provider.ts\";\n\nexport class MemoryManager {\n\tprivate readonly providers: MemoryProvider[] = [];\n\tprivate readonly activeProviders: Set<string> = new Set();\n\tprivate readonly registeredToolNames: Set<string> = new Set();\n\tprivate ctx?: MemoryLifecycleContext;\n\tprivate systemPromptBlockCache?: string;\n\n\t// Core reserved tool names to prevent hijacking or schema corruption.\n\tprivate static readonly RESERVED_CORE_TOOL_NAMES = new Set([\n\t\t\"read\",\n\t\t\"write\",\n\t\t\"grep\",\n\t\t\"find\",\n\t\t\"ls\",\n\t\t\"bash\",\n\t\t\"ask_user\",\n\t\t\"skillify\",\n\t\t\"skill_audit\",\n\t\t\"skill_search\",\n\t\t\"skill_open\",\n\t]);\n\n\tpublic registerProvider(p: MemoryProvider): void {\n\t\tif (this.providers.some((prov) => prov.name === p.name)) {\n\t\t\tthrow new Error(`Memory provider ${p.name} is already registered.`);\n\t\t}\n\n\t\tif (p.getToolDefinitions) {\n\t\t\tlet tools: ToolDefinition[] = [];\n\t\t\ttry {\n\t\t\t\ttools = p.getToolDefinitions() ?? [];\n\t\t\t} catch (err) {\n\t\t\t\tthrow new Error(`Failed to get tool definitions from provider ${p.name}: ${String(err)}`);\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tif (MemoryManager.RESERVED_CORE_TOOL_NAMES.has(tool.name)) {\n\t\t\t\t\tthrow new Error(`Memory provider ${p.name} tried to register reserved core tool: ${tool.name}`);\n\t\t\t\t}\n\t\t\t\tif (this.registeredToolNames.has(tool.name)) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Memory provider tool name collision: ${tool.name} is already registered. First-registration-wins.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (const tool of tools) {\n\t\t\t\tthis.registeredToolNames.add(tool.name);\n\t\t\t}\n\t\t}\n\n\t\tthis.providers.push(p);\n\t}\n\n\tpublic async initializeAll(sessionId: string, ctx: MemoryLifecycleContext): Promise<void> {\n\t\tthis.ctx = ctx;\n\t\tthis.activeProviders.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\n\t\tfor (const p of this.providers) {\n\t\t\ttry {\n\t\t\t\tconst available = await p.isAvailable();\n\t\t\t\tif (!available) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tawait p.initialize(sessionId, ctx);\n\t\t\t\tthis.activeProviders.add(p.name);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to initialize and was deactivated:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic buildSystemPromptBlock(): string {\n\t\tif (this.systemPromptBlockCache !== undefined) {\n\t\t\treturn this.systemPromptBlockCache;\n\t\t}\n\n\t\tthis.systemPromptBlockCache = this._composeSystemPromptBlock();\n\t\treturn this.systemPromptBlockCache;\n\t}\n\n\t/**\n\t * Compose the memory block freshly from the providers, BYPASSING the frozen cache used by the\n\t * system prompt. Used by end-of-loop reflection so its confront-before-write sees the live memory\n\t * (including writes made earlier in the same session) without churning the prefix-cache-stable\n\t * system prompt block.\n\t */\n\tpublic buildSystemPromptBlockFresh(): string {\n\t\treturn this._composeSystemPromptBlock();\n\t}\n\n\tprivate _composeSystemPromptBlock(): string {\n\t\tconst blocks: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.systemPromptBlock) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst block = p.systemPromptBlock();\n\t\t\t\tif (block) {\n\t\t\t\t\tblocks.push(block);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to generate system prompt block:`, err);\n\t\t\t}\n\t\t}\n\t\treturn blocks.join(\"\\n\\n\");\n\t}\n\n\tpublic async prefetch(query: string): Promise<string> {\n\t\tconst results: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.prefetch) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst text = await p.prefetch(query);\n\t\t\t\tif (text) {\n\t\t\t\t\tresults.push(text);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during prefetch:`, err);\n\t\t\t}\n\t\t}\n\t\treturn results.join(\"\\n\\n\");\n\t}\n\n\tpublic async syncTurn(user: string, assistant: string): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.syncTurn) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.syncTurn(user, assistant);\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during syncTurn:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async onPreCompress(): Promise<string> {\n\t\tconst insights: string[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onPreCompress) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst insight = await p.onPreCompress();\n\t\t\t\tif (insight) {\n\t\t\t\t\tinsights.push(insight);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onPreCompress:`, err);\n\t\t\t}\n\t\t}\n\t\treturn insights.join(\"\\n\\n\");\n\t}\n\n\tpublic async onSessionEnd(): Promise<void> {\n\t\tif (this.ctx?.isChildSession) {\n\t\t\treturn; // Write-gated: skip writes in child sessions\n\t\t}\n\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.onSessionEnd) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.onSessionEnd();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed during onSessionEnd:`, err);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async shutdownAll(): Promise<void> {\n\t\t// Shutdown in reverse registration order\n\t\tconst reversed = [...this.providers].reverse();\n\t\tfor (const p of reversed) {\n\t\t\tif (!this.activeProviders.has(p.name)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait p.shutdown();\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Memory provider ${p.name} failed to shutdown cleanly:`, err);\n\t\t\t}\n\t\t}\n\t\tthis.activeProviders.clear();\n\t}\n\n\tpublic getToolDefinitions(): ToolDefinition[] {\n\t\tconst tools: ToolDefinition[] = [];\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getToolDefinitions) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\ttools.push(...p.getToolDefinitions());\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get tool definitions from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn tools;\n\t}\n\n\tpublic getContextMarkers(): string[] {\n\t\tconst markers = new Set<string>();\n\t\tfor (const p of this.providers) {\n\t\t\tif (!this.activeProviders.has(p.name) || !p.getContextMarkers) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst list = p.getContextMarkers() ?? [];\n\t\t\t\tfor (const m of list) {\n\t\t\t\t\tmarkers.add(m);\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(`Failed to get context markers from provider ${p.name}:`, err);\n\t\t\t}\n\t\t}\n\t\treturn [...markers];\n\t}\n\n\tpublic reset(): void {\n\t\tthis.providers.length = 0;\n\t\tthis.activeProviders.clear();\n\t\tthis.registeredToolNames.clear();\n\t\tthis.systemPromptBlockCache = undefined;\n\t\tthis.ctx = undefined;\n\t}\n}\n"]}
@@ -433,6 +433,19 @@ export declare class InteractiveMode {
433
433
  private countAgentToolCalls;
434
434
  private buildAutonomyReviewDigest;
435
435
  private evaluateAutonomyReview;
436
+ /**
437
+ * Native reflection (R2) is the in-process replacement for the buggy `continuous-learning`
438
+ * subprocess. It runs when auto-learn is enabled and is not killed via `PI_NATIVE_REFLECTION=0`.
439
+ */
440
+ private isNativeReflectionEnabled;
441
+ /** Heuristic: does the user's turn text read like a correction/steer worth learning from? */
442
+ private hasCorrectionSignal;
443
+ /**
444
+ * End-of-loop native reflection: demand-gate the just-finished turn (zero-I/O) and, when
445
+ * warranted, run the in-process {@link AgentSession.runReflectionPass} as a fire-and-forget
446
+ * background microtask. No subprocess, no blocking of the UI.
447
+ */
448
+ private maybeRunNativeReflection;
436
449
  private maybeStartAutoLearn;
437
450
  private maybeStartAutonomyReview;
438
451
  private updateAutoLearnFooter;