pi-observational-memory 2.3.0 → 2.4.0
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/README.md +30 -22
- package/package.json +1 -1
- package/src/branch.ts +275 -6
- package/src/commands/status.ts +37 -11
- package/src/commands/view.ts +6 -5
- package/src/compaction.ts +330 -44
- package/src/config.ts +21 -2
- package/src/hooks/compaction-hook.ts +27 -22
- package/src/hooks/compaction-trigger.ts +31 -23
- package/src/hooks/observer-trigger.ts +13 -7
- package/src/prompts.ts +37 -13
- package/src/runtime.ts +6 -1
- package/src/tools/recall-observation.ts +315 -111
- package/src/types.ts +91 -9
|
@@ -5,17 +5,18 @@ import {
|
|
|
5
5
|
gapRawEntries,
|
|
6
6
|
getMemoryState,
|
|
7
7
|
} from "../branch.js";
|
|
8
|
-
import { renderSummary, runPruner, runReflector } from "../compaction.js";
|
|
8
|
+
import { migrateLegacyReflections, renderSummary, runPruner, runReflector } from "../compaction.js";
|
|
9
9
|
import { observationsToPromptLines, runObserver } from "../observer.js";
|
|
10
10
|
import type { Runtime } from "../runtime.js";
|
|
11
11
|
import { serializeSourceAddressedBranchEntries } from "../serialize.js";
|
|
12
12
|
import { estimateStringTokens } from "../tokens.js";
|
|
13
13
|
import {
|
|
14
14
|
OBSERVATION_CUSTOM_TYPE,
|
|
15
|
-
|
|
15
|
+
reflectionToPromptLine,
|
|
16
|
+
type MemoryDetailsV4,
|
|
17
|
+
type MemoryReflection,
|
|
16
18
|
type ObservationEntryData,
|
|
17
19
|
type ObservationRecord,
|
|
18
|
-
type Reflection,
|
|
19
20
|
} from "../types.js";
|
|
20
21
|
|
|
21
22
|
export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void {
|
|
@@ -33,9 +34,14 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
33
34
|
const { preparation, branchEntries, signal } = event;
|
|
34
35
|
const { firstKeptEntryId, tokensBefore } = preparation;
|
|
35
36
|
|
|
37
|
+
// Capture ctx properties synchronously — after multiple awaits below,
|
|
38
|
+
// the extension ctx may become stale (e.g. after session replacement/reload).
|
|
39
|
+
const hasUI = ctx.hasUI;
|
|
40
|
+
const ui = ctx.ui;
|
|
41
|
+
|
|
36
42
|
const resolved = await runtime.resolveModel(ctx as any);
|
|
37
43
|
if (!resolved.ok) {
|
|
38
|
-
if (
|
|
44
|
+
if (hasUI) ui?.notify(
|
|
39
45
|
`Observational memory: cannot compact — ${resolved.reason}. ` +
|
|
40
46
|
"Fix the model/API key and try /compact manually.",
|
|
41
47
|
"error",
|
|
@@ -67,7 +73,7 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
67
73
|
...memoryState.pendingObs,
|
|
68
74
|
]);
|
|
69
75
|
const gapTokenEstimate = estimateStringTokens(gapChunk);
|
|
70
|
-
if (
|
|
76
|
+
if (hasUI) ui?.notify(
|
|
71
77
|
`Observational memory: sync catch-up observer running on ~${gapTokenEstimate.toLocaleString()}-token gap`,
|
|
72
78
|
"info",
|
|
73
79
|
);
|
|
@@ -76,7 +82,7 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
76
82
|
model: resolved.model as any,
|
|
77
83
|
apiKey: resolved.apiKey,
|
|
78
84
|
headers: resolved.headers,
|
|
79
|
-
priorReflections: memoryState.reflections,
|
|
85
|
+
priorReflections: memoryState.reflections.map(reflectionToPromptLine),
|
|
80
86
|
priorObservations: priorObservationLines,
|
|
81
87
|
chunk: gapChunk,
|
|
82
88
|
allowedSourceEntryIds: sourceEntryIds,
|
|
@@ -95,19 +101,19 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
95
101
|
tokenCount: observationTokens,
|
|
96
102
|
};
|
|
97
103
|
pi.appendEntry(OBSERVATION_CUSTOM_TYPE, gapObservationData);
|
|
98
|
-
if (
|
|
104
|
+
if (hasUI && ui) ui.notify(
|
|
99
105
|
`Observational memory: sync catch-up recorded ${records.length} observation${records.length === 1 ? "" : "s"} (~${observationTokens.toLocaleString()} tokens)`,
|
|
100
106
|
"info",
|
|
101
107
|
);
|
|
102
|
-
} else if (
|
|
103
|
-
|
|
108
|
+
} else if (hasUI && ui) {
|
|
109
|
+
ui.notify(
|
|
104
110
|
"Observational memory: sync catch-up observer returned empty — proceeding with compaction",
|
|
105
111
|
"warning",
|
|
106
112
|
);
|
|
107
113
|
}
|
|
108
114
|
} catch (error) {
|
|
109
115
|
const msg = error instanceof Error ? error.message : String(error);
|
|
110
|
-
if (
|
|
116
|
+
if (hasUI && ui) ui.notify(
|
|
111
117
|
`Observational memory: sync catch-up observer failed: ${msg}. Cancelling compaction — ${gap.length} unobserved raw entries would be pruned without coverage. Try /compact again.`,
|
|
112
118
|
"warning",
|
|
113
119
|
);
|
|
@@ -125,11 +131,11 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
125
131
|
if (gapObservationData) deltaObservationData.push(gapObservationData);
|
|
126
132
|
|
|
127
133
|
if (deltaObservationData.length === 0) {
|
|
128
|
-
if (
|
|
134
|
+
if (hasUI) ui?.notify("Observational memory: nothing to compact yet", "warning");
|
|
129
135
|
return { cancel: true };
|
|
130
136
|
}
|
|
131
137
|
|
|
132
|
-
const workingReflections:
|
|
138
|
+
const workingReflections: MemoryReflection[] = migrateLegacyReflections(memoryState.reflections);
|
|
133
139
|
const workingObservations: ObservationRecord[] = [
|
|
134
140
|
...memoryState.committedObs,
|
|
135
141
|
...deltaObservationData.flatMap((d) => d.records),
|
|
@@ -141,14 +147,13 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
141
147
|
let finalObservations = workingObservations;
|
|
142
148
|
|
|
143
149
|
if (observationTokens >= runtime.config.reflectionThresholdTokens) {
|
|
144
|
-
if (
|
|
150
|
+
if (hasUI) ui?.notify("Observational memory: running reflector + pruner...", "info");
|
|
145
151
|
try {
|
|
146
|
-
|
|
152
|
+
finalReflections = await runReflector(
|
|
147
153
|
{ model: resolved.model as any, apiKey: resolved.apiKey, headers: resolved.headers, signal },
|
|
148
154
|
workingReflections,
|
|
149
155
|
workingObservations,
|
|
150
156
|
);
|
|
151
|
-
finalReflections = [...workingReflections, ...newReflections];
|
|
152
157
|
|
|
153
158
|
const prunerResult = await runPruner(
|
|
154
159
|
{ model: resolved.model as any, apiKey: resolved.apiKey, headers: resolved.headers, signal },
|
|
@@ -157,15 +162,15 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
157
162
|
runtime.config.reflectionThresholdTokens,
|
|
158
163
|
);
|
|
159
164
|
finalObservations = prunerResult.observations;
|
|
160
|
-
if (prunerResult.fellBack &&
|
|
161
|
-
|
|
165
|
+
if (prunerResult.fellBack && hasUI) {
|
|
166
|
+
ui?.notify(
|
|
162
167
|
"Observational memory: pruner run failed; kept observation set unchanged",
|
|
163
168
|
"warning",
|
|
164
169
|
);
|
|
165
170
|
}
|
|
166
171
|
} catch (error) {
|
|
167
172
|
const msg = error instanceof Error ? error.message : String(error);
|
|
168
|
-
if (
|
|
173
|
+
if (hasUI) ui?.notify(`Observational memory: reflect/prune failed: ${msg}`, "warning");
|
|
169
174
|
}
|
|
170
175
|
}
|
|
171
176
|
|
|
@@ -175,14 +180,14 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
175
180
|
throw new Error("invariant violated: finalObservations empty after delta guard");
|
|
176
181
|
}
|
|
177
182
|
|
|
178
|
-
const details:
|
|
183
|
+
const details: MemoryDetailsV4 = {
|
|
179
184
|
type: "observational-memory",
|
|
180
|
-
version:
|
|
185
|
+
version: 4,
|
|
181
186
|
observations: finalObservations,
|
|
182
187
|
reflections: finalReflections,
|
|
183
188
|
};
|
|
184
189
|
|
|
185
|
-
if (
|
|
190
|
+
if (hasUI) ui?.notify(
|
|
186
191
|
`Observational memory: compaction assembled — ${finalObservations.length} observation${finalObservations.length === 1 ? "" : "s"}, ${finalReflections.length} reflection${finalReflections.length === 1 ? "" : "s"}`,
|
|
187
192
|
"info",
|
|
188
193
|
);
|
|
@@ -199,4 +204,4 @@ export function registerCompactionHook(pi: ExtensionAPI, runtime: Runtime): void
|
|
|
199
204
|
runtime.compactHookInFlight = false;
|
|
200
205
|
}
|
|
201
206
|
});
|
|
202
|
-
}
|
|
207
|
+
}
|
|
@@ -5,13 +5,19 @@ import type { Runtime } from "../runtime.js";
|
|
|
5
5
|
export function registerCompactionTrigger(pi: ExtensionAPI, runtime: Runtime): void {
|
|
6
6
|
pi.on("agent_end", (_event, ctx) => {
|
|
7
7
|
runtime.ensureConfig(ctx.cwd);
|
|
8
|
+
if (runtime.config.passive === true) return;
|
|
8
9
|
if (runtime.compactInFlight) return;
|
|
9
10
|
|
|
10
11
|
const entries = ctx.sessionManager.getBranch() as Parameters<typeof rawTokensSinceLastCompaction>[0];
|
|
11
12
|
const tokens = rawTokensSinceLastCompaction(entries);
|
|
12
13
|
if (tokens < runtime.config.compactionThresholdTokens) return;
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
// Capture ctx properties synchronously — the setTimeout + async work below
|
|
16
|
+
// may outlive the extension ctx (stale after session replacement/reload).
|
|
17
|
+
const hasUI = ctx.hasUI;
|
|
18
|
+
const ui = ctx.ui;
|
|
19
|
+
|
|
20
|
+
if (hasUI) ui?.notify(
|
|
15
21
|
`Observational memory: compaction threshold reached (~${tokens.toLocaleString()} tokens); triggering compaction`,
|
|
16
22
|
"info",
|
|
17
23
|
);
|
|
@@ -25,29 +31,31 @@ export function registerCompactionTrigger(pi: ExtensionAPI, runtime: Runtime): v
|
|
|
25
31
|
// errors already surfaced via launchObserverTask
|
|
26
32
|
}
|
|
27
33
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (ctx.hasUI) ctx.ui.notify(
|
|
31
|
-
"Observational memory: compaction deferred — agent became busy after observer wait",
|
|
32
|
-
"info",
|
|
33
|
-
);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
const currentEntries = ctx.sessionManager.getBranch() as Parameters<typeof rawTokensSinceLastCompaction>[0];
|
|
37
|
-
const currentTokens = rawTokensSinceLastCompaction(currentEntries);
|
|
38
|
-
if (currentTokens < runtime.config.compactionThresholdTokens) {
|
|
39
|
-
runtime.compactInFlight = false;
|
|
40
|
-
if (ctx.hasUI) ctx.ui.notify(
|
|
41
|
-
"Observational memory: compaction skipped — another compaction already ran during observer wait",
|
|
42
|
-
"info",
|
|
43
|
-
);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
34
|
+
// After awaiting observerPromise, ctx may be stale.
|
|
35
|
+
// Use captured hasUI/ui for notification; wrap ctx access in try/catch.
|
|
46
36
|
try {
|
|
37
|
+
if (!ctx.isIdle()) {
|
|
38
|
+
runtime.compactInFlight = false;
|
|
39
|
+
if (hasUI) ui?.notify(
|
|
40
|
+
"Observational memory: compaction deferred — agent became busy after observer wait",
|
|
41
|
+
"info",
|
|
42
|
+
);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const currentEntries = ctx.sessionManager.getBranch() as Parameters<typeof rawTokensSinceLastCompaction>[0];
|
|
46
|
+
const currentTokens = rawTokensSinceLastCompaction(currentEntries);
|
|
47
|
+
if (currentTokens < runtime.config.compactionThresholdTokens) {
|
|
48
|
+
runtime.compactInFlight = false;
|
|
49
|
+
if (hasUI) ui?.notify(
|
|
50
|
+
"Observational memory: compaction skipped — another compaction already ran during observer wait",
|
|
51
|
+
"info",
|
|
52
|
+
);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
47
55
|
ctx.compact({
|
|
48
56
|
onComplete: () => {
|
|
49
57
|
runtime.compactInFlight = false;
|
|
50
|
-
if (
|
|
58
|
+
if (hasUI) ui?.notify("Observational memory: compaction complete", "info");
|
|
51
59
|
},
|
|
52
60
|
onError: (error) => {
|
|
53
61
|
runtime.compactInFlight = false;
|
|
@@ -55,14 +63,14 @@ export function registerCompactionTrigger(pi: ExtensionAPI, runtime: Runtime): v
|
|
|
55
63
|
// We already notified the user with the real reason before returning { cancel: true }.
|
|
56
64
|
return;
|
|
57
65
|
}
|
|
58
|
-
if (
|
|
66
|
+
if (hasUI) ui?.notify(`Observational memory: ${error.message}`, "error");
|
|
59
67
|
},
|
|
60
68
|
});
|
|
61
69
|
} catch (error) {
|
|
62
70
|
runtime.compactInFlight = false;
|
|
63
71
|
const msg = error instanceof Error ? error.message : String(error);
|
|
64
|
-
if (
|
|
72
|
+
if (hasUI) ui?.notify(`Observational memory: compact threw: ${msg}`, "error");
|
|
65
73
|
}
|
|
66
74
|
}, 0);
|
|
67
75
|
});
|
|
68
|
-
}
|
|
76
|
+
}
|
|
@@ -10,11 +10,12 @@ import { observationsToPromptLines, runObserver } from "../observer.js";
|
|
|
10
10
|
import type { Runtime } from "../runtime.js";
|
|
11
11
|
import { serializeSourceAddressedBranchEntries } from "../serialize.js";
|
|
12
12
|
import { estimateStringTokens } from "../tokens.js";
|
|
13
|
-
import { OBSERVATION_CUSTOM_TYPE, type ObservationEntryData } from "../types.js";
|
|
13
|
+
import { OBSERVATION_CUSTOM_TYPE, reflectionToPromptLine, type ObservationEntryData } from "../types.js";
|
|
14
14
|
|
|
15
15
|
export function registerObserverTrigger(pi: ExtensionAPI, runtime: Runtime): void {
|
|
16
16
|
pi.on("turn_end", (_event, ctx) => {
|
|
17
17
|
runtime.ensureConfig(ctx.cwd);
|
|
18
|
+
if (runtime.config.passive === true) return;
|
|
18
19
|
if (runtime.observerInFlight) return;
|
|
19
20
|
|
|
20
21
|
const entries = ctx.sessionManager.getBranch() as Parameters<typeof rawTokensSinceLastBound>[0];
|
|
@@ -42,11 +43,16 @@ export function registerObserverTrigger(pi: ExtensionAPI, runtime: Runtime): voi
|
|
|
42
43
|
"info",
|
|
43
44
|
);
|
|
44
45
|
|
|
46
|
+
// Capture ctx properties synchronously — the async work below may outlive
|
|
47
|
+
// the extension ctx (stale after session replacement/reload).
|
|
48
|
+
const hasUI = ctx.hasUI;
|
|
49
|
+
const ui = ctx.ui;
|
|
50
|
+
|
|
45
51
|
void runtime.launchObserverTask(ctx, "observer", async () => {
|
|
46
52
|
const resolved = await runtime.resolveModel(ctx as any);
|
|
47
53
|
if (!resolved.ok) {
|
|
48
|
-
if (!runtime.resolveFailureNotified &&
|
|
49
|
-
|
|
54
|
+
if (!runtime.resolveFailureNotified && hasUI && ui) {
|
|
55
|
+
ui.notify(
|
|
50
56
|
`Observational memory: observer skipped — ${resolved.reason}`,
|
|
51
57
|
"warning",
|
|
52
58
|
);
|
|
@@ -60,13 +66,13 @@ export function registerObserverTrigger(pi: ExtensionAPI, runtime: Runtime): voi
|
|
|
60
66
|
model: resolved.model as any,
|
|
61
67
|
apiKey: resolved.apiKey,
|
|
62
68
|
headers: resolved.headers,
|
|
63
|
-
priorReflections: reflections,
|
|
69
|
+
priorReflections: reflections.map(reflectionToPromptLine),
|
|
64
70
|
priorObservations: priorObservationLines,
|
|
65
71
|
chunk,
|
|
66
72
|
allowedSourceEntryIds: sourceEntryIds,
|
|
67
73
|
});
|
|
68
74
|
if (!records || records.length === 0) {
|
|
69
|
-
if (
|
|
75
|
+
if (hasUI && ui) ui.notify(
|
|
70
76
|
"Observational memory: observer returned no observations",
|
|
71
77
|
"warning",
|
|
72
78
|
);
|
|
@@ -81,10 +87,10 @@ export function registerObserverTrigger(pi: ExtensionAPI, runtime: Runtime): voi
|
|
|
81
87
|
tokenCount: observationTokens,
|
|
82
88
|
};
|
|
83
89
|
pi.appendEntry(OBSERVATION_CUSTOM_TYPE, data);
|
|
84
|
-
if (
|
|
90
|
+
if (hasUI && ui) ui.notify(
|
|
85
91
|
`Observational memory: ${records.length} observation${records.length === 1 ? "" : "s"} recorded (~${observationTokens.toLocaleString()} tokens)`,
|
|
86
92
|
"info",
|
|
87
93
|
);
|
|
88
94
|
});
|
|
89
95
|
});
|
|
90
|
-
}
|
|
96
|
+
}
|
package/src/prompts.ts
CHANGED
|
@@ -130,7 +130,7 @@ export const REFLECTOR_SYSTEM = `You are the reflection agent for a coding assis
|
|
|
130
130
|
|
|
131
131
|
${MEMORY_STAKES}
|
|
132
132
|
|
|
133
|
-
Your job is to crystallize stable, long-lived patterns from accumulated observations into
|
|
133
|
+
Your job is to crystallize stable, long-lived patterns from accumulated observations into reflections by calling the record_reflections tool. Reflections are the most durable layer of memory: once the pruner drops the observations behind them, the reflection is what remains.
|
|
134
134
|
|
|
135
135
|
You are operating on records produced by another part of the memory pipeline — the observer. To understand what you are reading and to produce reflections in the same voice, the observer was given these rules:
|
|
136
136
|
|
|
@@ -145,17 +145,23 @@ ${RELEVANCE_RUBRIC}
|
|
|
145
145
|
Your task is different from the observer's: you are not recording events, you are distilling stable patterns from them.
|
|
146
146
|
|
|
147
147
|
You receive:
|
|
148
|
-
- Current reflections (already-crystallized long-lived facts, one per line).
|
|
148
|
+
- Current reflections (already-crystallized long-lived facts, one per line). Newer reflections may begin with a bracketed id handle; treat that id as recall metadata, not as part of the reflection prose.
|
|
149
149
|
- Current observations (timestamped, relevance-tagged events accumulated over many turns). Each is shown as "[id] YYYY-MM-DD HH:MM [relevance] content".
|
|
150
150
|
|
|
151
151
|
How you work:
|
|
152
152
|
1. Read current reflections and observations to understand what is already crystallized and what new signal exists in the pool.
|
|
153
|
-
2. Identify new stable patterns worth crystallizing and call record_reflections with a batch of one or more new reflection
|
|
153
|
+
2. Identify new stable patterns worth crystallizing and call record_reflections with a batch of one or more new reflection proposals. Each proposal must include the reflection content and the exact supporting observation ids.
|
|
154
154
|
3. Read the receipt. If more reflections are warranted, call record_reflections again with another batch. You may call the tool many times.
|
|
155
155
|
4. When nothing more is stable enough to crystallize, STOP calling the tool and reply with a brief plain-text confirmation (one short sentence). That ends the run.
|
|
156
156
|
|
|
157
157
|
What to emit:
|
|
158
|
-
- Produce
|
|
158
|
+
- Produce new reflections when durable meaning is missing from the current reflections.
|
|
159
|
+
- To strengthen an existing reflection, emit the exact same reflection content with additional supportingObservationIds; the system will merge the supporting ids into the existing reflection.
|
|
160
|
+
- To promote a legacy/no-provenance reflection, emit the exact same reflection content with valid supportingObservationIds; the system will replace it with a provenance-backed reflection.
|
|
161
|
+
- When repeating exact existing content, emit only the reflection prose; omit any bracketed id handle.
|
|
162
|
+
- Do not lightly reword existing reflections. Rewording creates a separate reflection, so only use different wording when the durable meaning is materially different, more specific, or corrects/refines the existing reflection.
|
|
163
|
+
- For every reflection proposal, include supportingObservationIds: the smallest exact set of current observation ids that directly support the reflection.
|
|
164
|
+
- Never invent supporting observation ids. Use only ids printed in the current observations list. Reflection proposals with missing, empty, or invalid supportingObservationIds will be rejected and not recorded.
|
|
159
165
|
- Crystallize preferentially from "high" and "critical" observations; ignore "low" unless a pattern across many "low" observations is itself significant.
|
|
160
166
|
- Focus on:
|
|
161
167
|
- User identity, role, preferences, constraints.
|
|
@@ -196,10 +202,15 @@ ${RELEVANCE_RUBRIC}
|
|
|
196
202
|
</relevance-rubric>
|
|
197
203
|
|
|
198
204
|
You receive:
|
|
199
|
-
- Current reflections (long-lived facts; they survive regardless — treat them as already captured).
|
|
200
|
-
- Current observations (timestamped, relevance-tagged events to prune). Each is shown as "[id] YYYY-MM-DD HH:MM [relevance] content", where id is the 12-character hex handle you reference when dropping.
|
|
205
|
+
- Current reflections (long-lived facts; they survive regardless — treat them as already captured). Newer reflections may begin with a bracketed id handle; treat that id as recall metadata, not as part of the reflection prose.
|
|
206
|
+
- Current observations (timestamped, relevance-tagged events to prune). Each is shown as "[id] YYYY-MM-DD HH:MM [relevance] [coverage: tag] content", where id is the 12-character hex handle you reference when dropping.
|
|
201
207
|
- A pressure line stating pool size, target, tokens still to cut, and the current pass strategy.
|
|
202
208
|
|
|
209
|
+
Coverage tags are advisory pruning signals derived from current provenance-backed reflection support ids:
|
|
210
|
+
- [coverage: uncited] means no current provenance-backed reflection cites this observation. Prune cautiously, especially for medium/high/critical observations, because durable meaning may not be captured elsewhere.
|
|
211
|
+
- [coverage: cited] means 1-3 current provenance-backed reflections cite this observation. It is a better pruning candidate when the reflection preserves equivalent meaning, but it is not automatically safe to drop.
|
|
212
|
+
- [coverage: reinforced] means 4 or more current provenance-backed reflections cite this observation. Durable meaning is likely represented, but still preserve it if it carries exact errors, file paths, decisions, recent task state, user assertions, constraints, or nuance not captured with equivalent fidelity.
|
|
213
|
+
|
|
203
214
|
How you work:
|
|
204
215
|
1. Read reflections and the observation pool.
|
|
205
216
|
2. Identify ids that should be removed and call drop_observations with them. Pass multiple ids per call and call the tool multiple times as you work the pool down toward the target.
|
|
@@ -209,7 +220,7 @@ How you work:
|
|
|
209
220
|
This agent may be invoked again in a follow-up pass if the pool is still over budget — focus each run on your next-weakest drops rather than trying to do everything in one call.
|
|
210
221
|
|
|
211
222
|
What to drop (in priority order):
|
|
212
|
-
- Signal-captured: observations
|
|
223
|
+
- Signal-captured: observations tagged [coverage: cited] or [coverage: reinforced] whose durable meaning is captured by a reflection now in the reflections list. These are better pruning candidates, but still keep them when they contain exact details, user assertions, concrete completions, recent task state, or nuance not captured with equivalent fidelity.
|
|
213
224
|
- Superseded: directly contradicted or replaced by a newer observation.
|
|
214
225
|
- Redundant: near-duplicate of another observation (keep the higher-relevance or more recent one).
|
|
215
226
|
- Exhausted routine: tool-call acks, status updates, trivia that no longer affects the work.
|
|
@@ -243,7 +254,7 @@ If one of these categories is ALSO captured by an existing reflection with equiv
|
|
|
243
254
|
BAD: drop "[id] 2025-12-04 14:30 [medium] Build failed: TS2322 at src/auth.ts:47 — Type 'string | undefined' is not assignable to type 'string'" because it is only medium and the task moved on.
|
|
244
255
|
GOOD: keep that observation; it is a verbatim error the user hit, not captured in any reflection. Future debugging may need the exact code and location.
|
|
245
256
|
|
|
246
|
-
When in doubt,
|
|
257
|
+
When in doubt, prefer dropping cited or reinforced observations over uncited observations, but remember coverage tags are not commands. Reflections protect durable facts only when they preserve equivalent meaning. The only things you must preserve unconditionally are user assertions and concrete completions.
|
|
247
258
|
|
|
248
259
|
What you CANNOT do:
|
|
249
260
|
- You cannot merge observations. If two overlap, drop the weaker one.
|
|
@@ -254,12 +265,25 @@ It is valid to end a pass with zero drops if the pool genuinely has nothing more
|
|
|
254
265
|
|
|
255
266
|
Remember: every observation you drop is erased from the assistant's memory. A drop that looks reasonable at "low" becomes a mistake if the content was a user correction with a mis-labeled relevance. Read before you cut.`;
|
|
256
267
|
|
|
268
|
+
type ReflectorPassTier = 1 | 2 | 3;
|
|
269
|
+
|
|
270
|
+
const REFLECTOR_PASS_STRATEGIES: Record<ReflectorPassTier, string> = {
|
|
271
|
+
1: `Pass strategy — multi-observation synthesis. Find broad durable patterns, repeated preferences, recurring constraints, stable work style, and project-level themes supported by multiple observations. Every reflection recorded in this pass must cite at least 2 distinct supportingObservationIds. Do not create one-off event summaries; leave important single-observation facts for the atomic durable facts pass. If an existing reflection already captures the pattern, repeat the exact same content only when adding support ids materially strengthens it.`,
|
|
272
|
+
2: `Pass strategy — atomic durable facts. Capture important durable facts that may be supported by a single authoritative observation: explicit user preferences, hard constraints, corrections, decisions, completed milestones, project facts, release or rollback caveats, and other load-bearing facts future agents must not forget. Do not duplicate reflections created in earlier passes; repeat exact existing content only to add missing support or promote no-provenance legacy memory.`,
|
|
273
|
+
3: `Pass strategy — final safety review. Review the full observation pool against the current reflections, including reflections created in earlier passes, and catch durable information still missing. Look especially for high or critical observations, explicit user assertions, corrections, constraints, decisions, completed work, and important technical context. Do not create reflections just to increase coverage; only record a reflection if the durable meaning is not already captured with sufficient fidelity.`,
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
export function buildReflectorPassGuidance(pass: number, maxPasses: number): string {
|
|
277
|
+
const tier = (Math.min(3, Math.max(1, pass)) as ReflectorPassTier);
|
|
278
|
+
return `Pass ${pass} of up to ${maxPasses}. ${REFLECTOR_PASS_STRATEGIES[tier]}`;
|
|
279
|
+
}
|
|
280
|
+
|
|
257
281
|
type PrunerPassTier = 1 | 2 | 3;
|
|
258
282
|
|
|
259
283
|
const PRUNER_PASS_STRATEGIES: Record<PrunerPassTier, string> = {
|
|
260
|
-
1: `Pass strategy — clear-cut drops only.
|
|
261
|
-
2: `Pass strategy — topic compression. Drop "low" observations that cover the same territory as recent "medium" or "high" observations. Drop older "medium" observations whose substance is now covered by a reflection. Collapse sequences of repeated tool-call observations by keeping the one that captures the learning and dropping the rest.`,
|
|
262
|
-
3: `Pass strategy — aggressive age compression. In the older half of the pool, drop all but the outcome-bearing "low" and "medium" observations. Keep the most recent ~30% of the pool at higher detail. Drop "high" observations only when a reflection clearly captures the same fact. NEVER drop "critical" items, user assertions, or concrete completions regardless of age.`,
|
|
284
|
+
1: `Pass strategy — clear-cut drops only. Prefer old low-value [coverage: cited] or [coverage: reinforced] observations when their durable meaning is represented by current reflections. Also remove exact duplicates, near-duplicates (keep the higher-relevance or more recent version), observations directly superseded by a newer one, and routine "low" tool-call acks. Do not touch ambiguous [coverage: uncited] cases on this pass — a follow-up pass will handle them if still needed.`,
|
|
285
|
+
2: `Pass strategy — topic compression. Drop "low" observations that cover the same territory as recent "medium" or "high" observations, especially when tagged [coverage: cited] or [coverage: reinforced]. Drop older "medium" observations whose substance is now covered by a reflection. Collapse sequences of repeated tool-call observations by keeping the one that captures the learning and dropping the rest.`,
|
|
286
|
+
3: `Pass strategy — aggressive age compression. In the older half of the pool, drop all but the outcome-bearing "low" and "medium" observations, preferring [coverage: cited] and [coverage: reinforced] over [coverage: uncited]. Keep the most recent ~30% of the pool at higher detail. Drop "high" observations only when a reflection clearly captures the same fact. NEVER drop "critical" items, user assertions, or concrete completions regardless of age.`,
|
|
263
287
|
};
|
|
264
288
|
|
|
265
289
|
export function buildPrunerPassGuidance(pass: number, maxPasses: number): string {
|
|
@@ -269,9 +293,9 @@ export function buildPrunerPassGuidance(pass: number, maxPasses: number): string
|
|
|
269
293
|
|
|
270
294
|
export const CONTEXT_USAGE_INSTRUCTIONS = `These are condensed memories from earlier in this session.
|
|
271
295
|
|
|
272
|
-
- Reflections: stable, long-lived facts about the user, project, decisions, and constraints.
|
|
296
|
+
- Reflections: stable, long-lived facts about the user, project, decisions, and constraints. New reflection lines may include ids in brackets.
|
|
273
297
|
- Observations: timestamped events from the conversation history, in chronological order. Observation lines include ids in brackets.
|
|
274
298
|
|
|
275
299
|
Treat these as past records. When entries conflict, the most recent observation reflects the latest known state. Work that prior observations describe as completed should not be redone unless the user explicitly asks to revisit it.
|
|
276
300
|
|
|
277
|
-
When exact source context is needed for precision or traceability, use the recall tool with the relevant observation id. Do not use recall as broad search or inject raw source unless it is needed.`;
|
|
301
|
+
When exact source context is needed for precision or traceability, use the recall tool with the relevant observation or reflection id. This is especially useful when a reflection materially affects a decision or is too compressed to continue confidently. Do not use recall as broad search or inject raw source unless it is needed.`;
|
package/src/runtime.ts
CHANGED
|
@@ -58,13 +58,18 @@ export class Runtime {
|
|
|
58
58
|
|
|
59
59
|
launchObserverTask(ctx: LaunchCtx, label: string, work: () => Promise<void>): Promise<void> {
|
|
60
60
|
this.observerInFlight = true;
|
|
61
|
+
// Capture ctx properties synchronously — after `await work()` the extension ctx
|
|
62
|
+
// may be stale (e.g. after ctx.newSession/fork/switchSession/reload), and accessing
|
|
63
|
+
// ctx.hasUI or ctx.ui on a stale proxy throws.
|
|
64
|
+
const hasUI = ctx.hasUI;
|
|
65
|
+
const ui = ctx.ui;
|
|
61
66
|
let promise!: Promise<void>;
|
|
62
67
|
promise = (async () => {
|
|
63
68
|
try {
|
|
64
69
|
await work();
|
|
65
70
|
} catch (error) {
|
|
66
71
|
const msg = error instanceof Error ? error.message : String(error);
|
|
67
|
-
if (
|
|
72
|
+
if (hasUI && ui) ui.notify(`Observational memory: ${label} failed: ${msg}`, "warning");
|
|
68
73
|
} finally {
|
|
69
74
|
this.observerInFlight = false;
|
|
70
75
|
if (this.observerPromise === promise) this.observerPromise = null;
|