pi-blackhole 0.2.2 → 0.2.3
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 +7 -1
- package/package.json +1 -1
- package/src/core/unified-config.ts +21 -2
- package/src/om/agents/dropper/agent.ts +128 -10
- package/src/om/agents/dropper/coverage.ts +128 -0
- package/src/om/agents/dropper/prompts.ts +12 -12
- package/src/om/agents/observer/prompts.ts +1 -1
- package/src/om/agents/reflector/prompts.ts +8 -4
- package/src/om/consolidation.ts +2 -47
- package/src/om/ledger/projection.ts +13 -1
- package/src/om/ledger/render-summary.ts +51 -0
package/README.md
CHANGED
|
@@ -5,8 +5,14 @@ Algorithmic compaction + session-aware observational memory for [Pi](https://git
|
|
|
5
5
|
Combines [pi-vcc](https://github.com/sting8k/pi-vcc) and [pi-observational-memory](https://github.com/elpapi42/pi-observational-memory) with unified configuration, per-worker model fallback chains, persisted cooldowns, and a manual flush mode.
|
|
6
6
|
|
|
7
7
|
> This is a frankenmerge. I liked both extensions but they were not compatible - observational memory hooked into Pi's default compaction and prevented pi-vcc from working. So I merged them, made them share the same hook and output, and added the things both were missing: fallback chains, cooldowns, and a toggle between them.
|
|
8
|
+
> Please also see the [`CHANGELOG.md`](CHANGELOG.md)
|
|
9
|
+
|
|
10
|
+
### Lockstep with upstreams
|
|
11
|
+
|
|
12
|
+
pi-blackhole tracks both upstream repositories via a [lockstep audit system](https://github.com/k0valik/pi-blackhole/tree/lockstep/2026-05-27/.pi/skills/lockstep) that classifies every new upstream commit as safe-to-port, modified (needs review), rewritten (skip), or orphan (needs mapping). The goal is to lift bugfixes, prompts improvements, and compatible features without breaking existing users or rolling back intentional divergences. Ported changes are reviewed per-commit with human approval — nothing is blindly merged. See [SKILL.md](.pi/skills/lockstep/SKILL.md) for the full workflow. An example execution (including rationale for skipped changes) is documented in [PR #8](https://github.com/k0valik/pi-blackhole/pull/8).
|
|
13
|
+
|
|
14
|
+
#### For easy setup pass the [`llms.txt`](llms.txt) llms.txt to your agent and it will guide you through the config without needing to read all the docs if you're as lazy as me.
|
|
8
15
|
|
|
9
|
-
#### For easy setup pass the [`llms.txt`](llms.txt) llms.txt to your agent and it will guide you through the config without needing to read all the docs if you're as lazy as me
|
|
10
16
|
|
|
11
17
|
# Demo
|
|
12
18
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-blackhole",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Unified compaction + observational memory extension for Pi — compresses conversation context while preserving durable observations and reflections",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "index.ts",
|
|
@@ -44,6 +44,16 @@ export interface UnifiedConfig {
|
|
|
44
44
|
compactAfterTokens: number;
|
|
45
45
|
/** Observation pool token pressure for full fold. */
|
|
46
46
|
observationsPoolMaxTokens: number;
|
|
47
|
+
/** Target token budget for the observation pool (dropper aims here).
|
|
48
|
+
* Optional; defaults to half of observationsPoolMaxTokens when unset.
|
|
49
|
+
* Must be less than observationsPoolMaxTokens.
|
|
50
|
+
*
|
|
51
|
+
* NOTE: Ported from upstream as forward-compat (no-op in our pool algorithm).
|
|
52
|
+
* Upstream renamed budgetTokens→targetTokens (52b5844) and uses this
|
|
53
|
+
* for their tokensOverTarget / avgTokensPerObservation drop calculation.
|
|
54
|
+
* We keep our ratio-based urgency algorithm; this knob exists so future
|
|
55
|
+
* lockstep iterations don't diverge on the config shape. */
|
|
56
|
+
observationsPoolTargetTokens: number;
|
|
47
57
|
/** Max prompt tokens for reflector model input (rolling window cap). */
|
|
48
58
|
reflectorInputMaxTokens: number;
|
|
49
59
|
/** Max prompt tokens for dropper model input (rolling window cap). */
|
|
@@ -96,6 +106,7 @@ export const DEFAULTS: UnifiedConfig = {
|
|
|
96
106
|
reflectAfterTokens: 25_000,
|
|
97
107
|
compactAfterTokens: 81_000,
|
|
98
108
|
observationsPoolMaxTokens: 20_000,
|
|
109
|
+
observationsPoolTargetTokens: 10_000,
|
|
99
110
|
reflectorInputMaxTokens: 80_000,
|
|
100
111
|
dropperInputMaxTokens: 80_000,
|
|
101
112
|
observerChunkMaxTokens: 40_000,
|
|
@@ -160,7 +171,7 @@ function parseConfig(raw: Record<string, unknown>): Partial<UnifiedConfig> {
|
|
|
160
171
|
if (typeof raw.debugLog === "boolean") c.debugLog = raw.debugLog;
|
|
161
172
|
|
|
162
173
|
// Positive integers
|
|
163
|
-
const numKeys = ["observeAfterTokens", "reflectAfterTokens", "compactAfterTokens", "observationsPoolMaxTokens", "reflectorInputMaxTokens", "dropperInputMaxTokens", "observerChunkMaxTokens", "observerPreambleMaxTokens", "agentMaxTurns"] as const;
|
|
174
|
+
const numKeys = ["observeAfterTokens", "reflectAfterTokens", "compactAfterTokens", "observationsPoolMaxTokens", "observationsPoolTargetTokens", "reflectorInputMaxTokens", "dropperInputMaxTokens", "observerChunkMaxTokens", "observerPreambleMaxTokens", "agentMaxTurns"] as const;
|
|
164
175
|
for (const k of numKeys) {
|
|
165
176
|
const v = positiveInt(raw[k]);
|
|
166
177
|
if (v !== undefined) (c as Record<string, unknown>)[k] = v;
|
|
@@ -238,7 +249,15 @@ export function loadUnifiedConfig(cwd: string): UnifiedConfig {
|
|
|
238
249
|
else if (["0", "false", "no", "off"].includes(v)) parsed.passive = false;
|
|
239
250
|
}
|
|
240
251
|
|
|
241
|
-
|
|
252
|
+
// Merge defaults then override
|
|
253
|
+
const merged = { ...DEFAULTS, ...parsed };
|
|
254
|
+
|
|
255
|
+
// Derive observationsPoolTargetTokens if unset or invalid (must be < max)
|
|
256
|
+
if (merged.observationsPoolTargetTokens >= merged.observationsPoolMaxTokens) {
|
|
257
|
+
merged.observationsPoolTargetTokens = Math.floor(merged.observationsPoolMaxTokens / 2);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return merged;
|
|
242
261
|
}
|
|
243
262
|
|
|
244
263
|
/**
|
|
@@ -9,9 +9,18 @@ import { agentLoop, type AgentContext, type AgentLoopConfig, type AgentTool } fr
|
|
|
9
9
|
import type { Message, Model, ModelThinkingLevel } from "@earendil-works/pi-ai";
|
|
10
10
|
import { Type } from "@earendil-works/pi-ai";
|
|
11
11
|
import type { Static } from "typebox";
|
|
12
|
+
import { debugLog } from "../../debug-log.js";
|
|
12
13
|
import { AGENT_LOOP_MAX_TOKENS, boundedMaxTokens } from "../../model-budget.js";
|
|
13
|
-
import {
|
|
14
|
+
import { reflectionToSummaryLine, type Observation, type Reflection } from "../../ledger/index.js";
|
|
14
15
|
import { DROPPER_SYSTEM } from "./prompts.js";
|
|
16
|
+
import {
|
|
17
|
+
REFLECTION_COVERAGE_DROP_RANK,
|
|
18
|
+
coverageTierForObservation,
|
|
19
|
+
observationToDropperLine,
|
|
20
|
+
reflectionCoverageMap,
|
|
21
|
+
summarizeCoverageByRelevance,
|
|
22
|
+
summarizeCoverageByRelevanceForIds,
|
|
23
|
+
} from "./coverage.js";
|
|
15
24
|
|
|
16
25
|
interface RunDropperArgs {
|
|
17
26
|
model: Model<any>;
|
|
@@ -81,6 +90,13 @@ export function maxDropCountForPool(observations: readonly Observation[], observ
|
|
|
81
90
|
return Math.max(1, Math.floor(droppableCount * dropRatio));
|
|
82
91
|
}
|
|
83
92
|
|
|
93
|
+
function relevanceCounts(observations: readonly Observation[]): Record<Observation["relevance"], number> {
|
|
94
|
+
return observations.reduce<Record<Observation["relevance"], number>>((counts, observation) => {
|
|
95
|
+
if (observation.relevance in counts) counts[observation.relevance]++;
|
|
96
|
+
return counts;
|
|
97
|
+
}, { low: 0, medium: 0, high: 0, critical: 0 });
|
|
98
|
+
}
|
|
99
|
+
|
|
84
100
|
export function normalizeDropObservationIds(
|
|
85
101
|
ids: readonly string[] | undefined,
|
|
86
102
|
observations: readonly Observation[],
|
|
@@ -92,7 +108,6 @@ export function normalizeDropObservationIds(
|
|
|
92
108
|
for (const id of ids) {
|
|
93
109
|
const observation = allowed.get(id);
|
|
94
110
|
if (!observation) continue;
|
|
95
|
-
if (observation.relevance === "critical") continue;
|
|
96
111
|
if (seen.has(id)) continue;
|
|
97
112
|
seen.add(id);
|
|
98
113
|
result.push(id);
|
|
@@ -100,14 +115,21 @@ export function normalizeDropObservationIds(
|
|
|
100
115
|
return result.length > 0 ? result : undefined;
|
|
101
116
|
}
|
|
102
117
|
|
|
118
|
+
function timestampRank(timestamp: string): number {
|
|
119
|
+
const parsed = Date.parse(timestamp);
|
|
120
|
+
return Number.isFinite(parsed) ? parsed : Number.POSITIVE_INFINITY;
|
|
121
|
+
}
|
|
122
|
+
|
|
103
123
|
export function selectDropCandidates(
|
|
104
124
|
ids: readonly string[],
|
|
105
125
|
observations: readonly Observation[],
|
|
106
126
|
maxDrops: number,
|
|
127
|
+
reflections: readonly Reflection[] = [],
|
|
107
128
|
): string[] {
|
|
108
129
|
if (maxDrops <= 0 || ids.length === 0) return [];
|
|
109
130
|
|
|
110
131
|
const byId = new Map(observations.map((observation) => [observation.id, observation]));
|
|
132
|
+
const coverageById = reflectionCoverageMap(observations, reflections);
|
|
111
133
|
const firstProposalIndex = new Map<string, number>();
|
|
112
134
|
for (let i = 0; i < ids.length; i++) {
|
|
113
135
|
const id = ids[i];
|
|
@@ -117,11 +139,16 @@ export function selectDropCandidates(
|
|
|
117
139
|
return Array.from(firstProposalIndex.entries())
|
|
118
140
|
.map(([id, index]) => ({ id, index, observation: byId.get(id) }))
|
|
119
141
|
.filter((candidate): candidate is { id: string; index: number; observation: Observation } =>
|
|
120
|
-
candidate.observation !== undefined
|
|
142
|
+
candidate.observation !== undefined
|
|
121
143
|
)
|
|
122
144
|
.sort((a, b) => {
|
|
145
|
+
const coverageDelta = REFLECTION_COVERAGE_DROP_RANK[coverageTierForObservation(a.observation, coverageById)]
|
|
146
|
+
- REFLECTION_COVERAGE_DROP_RANK[coverageTierForObservation(b.observation, coverageById)];
|
|
123
147
|
const relevanceDelta = RELEVANCE_DROP_RANK[a.observation.relevance] - RELEVANCE_DROP_RANK[b.observation.relevance];
|
|
124
|
-
|
|
148
|
+
const aAge = timestampRank(a.observation.timestamp);
|
|
149
|
+
const bAge = timestampRank(b.observation.timestamp);
|
|
150
|
+
const ageDelta = aAge === bAge ? 0 : aAge - bAge;
|
|
151
|
+
return coverageDelta || relevanceDelta || ageDelta || a.index - b.index;
|
|
125
152
|
})
|
|
126
153
|
.slice(0, maxDrops)
|
|
127
154
|
.map((candidate) => candidate.id);
|
|
@@ -135,10 +162,42 @@ export async function runDropper(args: RunDropperArgs): Promise<string[] | undef
|
|
|
135
162
|
const fullness = observationPoolFullness(observationTokens, budgetTokens);
|
|
136
163
|
const urgency = dropUrgencyForFullness(fullness);
|
|
137
164
|
const maxDropsAllowed = maxDropCountForPool(observations, observationTokens, budgetTokens);
|
|
138
|
-
|
|
165
|
+
const coverageById = reflectionCoverageMap(observations, reflections);
|
|
166
|
+
const coverageSummaryByRelevance = summarizeCoverageByRelevance(observations, coverageById);
|
|
167
|
+
debugLog("dropper.agent_start", {
|
|
168
|
+
activeObservationCount: observations.length,
|
|
169
|
+
reflectionCount: reflections.length,
|
|
170
|
+
observationTokens,
|
|
171
|
+
budgetTokens,
|
|
172
|
+
fullness,
|
|
173
|
+
urgency,
|
|
174
|
+
maxDropsAllowed,
|
|
175
|
+
relevanceCounts: relevanceCounts(observations),
|
|
176
|
+
coverageSummaryByRelevance,
|
|
177
|
+
});
|
|
178
|
+
if (maxDropsAllowed <= 0) {
|
|
179
|
+
debugLog("dropper.result", {
|
|
180
|
+
reason: "not_over_target",
|
|
181
|
+
toolCallCount: 0,
|
|
182
|
+
rawRequestedIdsCount: 0,
|
|
183
|
+
acceptedCandidateCount: 0,
|
|
184
|
+
selectedDropsCount: 0,
|
|
185
|
+
selectedDropTokens: 0,
|
|
186
|
+
selectedCoverageSummaryByRelevance: summarizeCoverageByRelevanceForIds([], observations, coverageById),
|
|
187
|
+
maxDropsAllowed,
|
|
188
|
+
});
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
139
191
|
|
|
140
192
|
const proposedDropIds: string[] = [];
|
|
141
193
|
const proposed = new Set<string>();
|
|
194
|
+
const allowed = new Map(observations.map((observation) => [observation.id, observation]));
|
|
195
|
+
let toolCallCount = 0;
|
|
196
|
+
let rawRequestedIdsCount = 0;
|
|
197
|
+
let missingIdsCount = 0;
|
|
198
|
+
let criticalCandidateIdsCount = 0;
|
|
199
|
+
let duplicateInRequestCount = 0;
|
|
200
|
+
let duplicateInRunCount = 0;
|
|
142
201
|
|
|
143
202
|
const dropObservations: AgentTool<typeof DropObservationsSchema> = {
|
|
144
203
|
name: "drop_observations",
|
|
@@ -146,14 +205,51 @@ export async function runDropper(args: RunDropperArgs): Promise<string[] | undef
|
|
|
146
205
|
description: "Propose active observation ids that are safe to remove from compacted memory.",
|
|
147
206
|
parameters: DropObservationsSchema,
|
|
148
207
|
execute: async (_id, params: DropObservationsArgs) => {
|
|
149
|
-
|
|
208
|
+
toolCallCount++;
|
|
209
|
+
rawRequestedIdsCount += params.ids.length;
|
|
210
|
+
const seenInRequest = new Set<string>();
|
|
150
211
|
let added = 0;
|
|
151
|
-
|
|
152
|
-
|
|
212
|
+
let requestMissingIds = 0;
|
|
213
|
+
let requestCriticalCandidateIds = 0;
|
|
214
|
+
let requestDuplicateIds = 0;
|
|
215
|
+
let requestDuplicateInRunIds = 0;
|
|
216
|
+
for (const id of params.ids) {
|
|
217
|
+
const observation = allowed.get(id);
|
|
218
|
+
if (!observation) {
|
|
219
|
+
missingIdsCount++;
|
|
220
|
+
requestMissingIds++;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (seenInRequest.has(id)) {
|
|
224
|
+
duplicateInRequestCount++;
|
|
225
|
+
requestDuplicateIds++;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
seenInRequest.add(id);
|
|
229
|
+
if (proposed.has(id)) {
|
|
230
|
+
duplicateInRunCount++;
|
|
231
|
+
requestDuplicateInRunIds++;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
153
234
|
proposed.add(id);
|
|
154
235
|
proposedDropIds.push(id);
|
|
236
|
+
if (observation.relevance === "critical") {
|
|
237
|
+
criticalCandidateIdsCount++;
|
|
238
|
+
requestCriticalCandidateIds++;
|
|
239
|
+
}
|
|
155
240
|
added++;
|
|
156
241
|
}
|
|
242
|
+
debugLog("dropper.tool_call", {
|
|
243
|
+
toolCallCount,
|
|
244
|
+
rawRequestedIdsCount: params.ids.length,
|
|
245
|
+
acceptedIdsCount: added,
|
|
246
|
+
missingIdsCount: requestMissingIds,
|
|
247
|
+
criticalCandidateIdsCount: requestCriticalCandidateIds,
|
|
248
|
+
duplicateInRequestCount: requestDuplicateIds,
|
|
249
|
+
duplicateInRunCount: requestDuplicateInRunIds,
|
|
250
|
+
totalCandidates: proposedDropIds.length,
|
|
251
|
+
maxDropsAllowed,
|
|
252
|
+
});
|
|
157
253
|
return {
|
|
158
254
|
content: [{ type: "text", text: `Queued ${added} drop candidate${added === 1 ? "" : "s"}. Candidates this run: ${proposedDropIds.length}. Maximum drops allowed: ${maxDropsAllowed}.` }],
|
|
159
255
|
details: { added, totalCandidates: proposedDropIds.length, maxDropsAllowed },
|
|
@@ -166,7 +262,7 @@ export async function runDropper(args: RunDropperArgs): Promise<string[] | undef
|
|
|
166
262
|
? `EXISTING ACTIVE OBSERVATIONS (for context only — these are NOT candidates for dropping):\n${args.existingObservationsSummary}\n\n`
|
|
167
263
|
: '';
|
|
168
264
|
|
|
169
|
-
const userText = `CURRENT REFLECTIONS:\n${joinOrEmpty(reflections.map(reflectionToSummaryLine))}\n\n${existingObservationsContext}NEW OBSERVATIONS TO EVALUATE FOR DROPPING:\n${joinOrEmpty(observations.map(
|
|
265
|
+
const userText = `CURRENT REFLECTIONS:\n${joinOrEmpty(reflections.map(reflectionToSummaryLine))}\n\n${existingObservationsContext}NEW OBSERVATIONS TO EVALUATE FOR DROPPING:\n${joinOrEmpty(observations.map((observation) => observationToDropperLine(observation, coverageTierForObservation(observation, coverageById))))}\n\nObservation pool pressure: ~${observationTokens.toLocaleString()} tokens; target budget: ~${budgetTokens.toLocaleString()} tokens; fullness: ~${fullnessPercent.toLocaleString()}%.\nDrop urgency: ${urgency}.\nMaximum drops allowed this run: ${maxDropsAllowed.toLocaleString()} observation${maxDropsAllowed === 1 ? "" : "s"}.\nThis maximum is a hard upper bound, not a target. Drop fewer or none if fewer observations are clearly safe.`;
|
|
170
266
|
const prompts: Message[] = [{ role: "user", content: [{ type: "text", text: userText }], timestamp: Date.now() }];
|
|
171
267
|
const context: AgentContext = { systemPrompt: DROPPER_SYSTEM, messages: [], tools: [dropObservations as AgentTool<any>] };
|
|
172
268
|
const reasoning = (model as { reasoning?: unknown }).reasoning;
|
|
@@ -199,6 +295,28 @@ export async function runDropper(args: RunDropperArgs): Promise<string[] | undef
|
|
|
199
295
|
}
|
|
200
296
|
await stream.result();
|
|
201
297
|
if (agentError && proposedDropIds.length === 0) throw new Error(`Dropper API error: ${agentError}`);
|
|
202
|
-
const droppedIds = selectDropCandidates(proposedDropIds, observations, maxDropsAllowed);
|
|
298
|
+
const droppedIds = selectDropCandidates(proposedDropIds, observations, maxDropsAllowed, reflections);
|
|
299
|
+
const reason = droppedIds.length > 0
|
|
300
|
+
? "selected_nonempty"
|
|
301
|
+
: toolCallCount === 0
|
|
302
|
+
? "no_tool_call"
|
|
303
|
+
: proposedDropIds.length === 0
|
|
304
|
+
? "all_filtered"
|
|
305
|
+
: "selected_empty";
|
|
306
|
+
const selectedDropTokens = droppedIds.reduce((sum, id) => sum + (allowed.get(id)?.tokenCount ?? 0), 0);
|
|
307
|
+
debugLog("dropper.result", {
|
|
308
|
+
reason,
|
|
309
|
+
toolCallCount,
|
|
310
|
+
rawRequestedIdsCount,
|
|
311
|
+
missingIdsCount,
|
|
312
|
+
criticalCandidateIdsCount,
|
|
313
|
+
duplicateInRequestCount,
|
|
314
|
+
duplicateInRunCount,
|
|
315
|
+
acceptedCandidateCount: proposedDropIds.length,
|
|
316
|
+
selectedDropsCount: droppedIds.length,
|
|
317
|
+
selectedDropTokens,
|
|
318
|
+
selectedCoverageSummaryByRelevance: summarizeCoverageByRelevanceForIds(droppedIds, observations, coverageById),
|
|
319
|
+
maxDropsAllowed,
|
|
320
|
+
});
|
|
203
321
|
return droppedIds.length > 0 ? droppedIds : undefined;
|
|
204
322
|
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { Observation, Reflection } from "../../ledger/index.js";
|
|
2
|
+
|
|
3
|
+
export const REFLECTION_COVERAGE_TIERS = ["none", "partial", "strong"] as const;
|
|
4
|
+
export type ReflectionCoverageTier = typeof REFLECTION_COVERAGE_TIERS[number];
|
|
5
|
+
|
|
6
|
+
type Relevance = Observation["relevance"];
|
|
7
|
+
|
|
8
|
+
type CoverageBucket = Record<ReflectionCoverageTier, { count: number; tokens: number }>;
|
|
9
|
+
export type CoverageSummaryByRelevance = Record<Relevance, CoverageBucket>;
|
|
10
|
+
export type CoverageTransitionSummaryByRelevance = Record<Relevance, Record<string, { count: number; tokens: number }>>;
|
|
11
|
+
|
|
12
|
+
export const REFLECTION_COVERAGE_DROP_RANK: Record<ReflectionCoverageTier, number> = {
|
|
13
|
+
strong: 0,
|
|
14
|
+
partial: 1,
|
|
15
|
+
none: 2,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function reflectionSupportCounts(reflections: readonly Reflection[]): Map<string, number> {
|
|
19
|
+
const counts = new Map<string, number>();
|
|
20
|
+
for (const reflection of reflections) {
|
|
21
|
+
const uniqueIds = new Set(reflection.supportingObservationIds);
|
|
22
|
+
for (const id of uniqueIds) counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
23
|
+
}
|
|
24
|
+
return counts;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function reflectionCoverageTierForCount(count: number): ReflectionCoverageTier {
|
|
28
|
+
if (count <= 0) return "none";
|
|
29
|
+
if (count === 1) return "partial";
|
|
30
|
+
return "strong";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function reflectionCoverageMap(
|
|
34
|
+
observations: readonly Observation[],
|
|
35
|
+
reflections: readonly Reflection[],
|
|
36
|
+
): Map<string, ReflectionCoverageTier> {
|
|
37
|
+
const counts = reflectionSupportCounts(reflections);
|
|
38
|
+
return new Map(observations.map((observation) => [
|
|
39
|
+
observation.id,
|
|
40
|
+
reflectionCoverageTierForCount(counts.get(observation.id) ?? 0),
|
|
41
|
+
]));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function emptyCoverageBucket(): CoverageBucket {
|
|
45
|
+
return {
|
|
46
|
+
none: { count: 0, tokens: 0 },
|
|
47
|
+
partial: { count: 0, tokens: 0 },
|
|
48
|
+
strong: { count: 0, tokens: 0 },
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function emptyCoverageSummaryByRelevance(): CoverageSummaryByRelevance {
|
|
53
|
+
return {
|
|
54
|
+
low: emptyCoverageBucket(),
|
|
55
|
+
medium: emptyCoverageBucket(),
|
|
56
|
+
high: emptyCoverageBucket(),
|
|
57
|
+
critical: emptyCoverageBucket(),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function summarizeCoverageByRelevance(
|
|
62
|
+
observations: readonly Observation[],
|
|
63
|
+
coverageById: ReadonlyMap<string, ReflectionCoverageTier>,
|
|
64
|
+
): CoverageSummaryByRelevance {
|
|
65
|
+
const summary = emptyCoverageSummaryByRelevance();
|
|
66
|
+
for (const observation of observations) {
|
|
67
|
+
const tier = coverageById.get(observation.id) ?? "none";
|
|
68
|
+
const bucket = summary[observation.relevance][tier];
|
|
69
|
+
bucket.count++;
|
|
70
|
+
bucket.tokens += observation.tokenCount;
|
|
71
|
+
}
|
|
72
|
+
return summary;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function summarizeCoverageByRelevanceForIds(
|
|
76
|
+
ids: readonly string[],
|
|
77
|
+
observations: readonly Observation[],
|
|
78
|
+
coverageById: ReadonlyMap<string, ReflectionCoverageTier>,
|
|
79
|
+
): CoverageSummaryByRelevance {
|
|
80
|
+
const byId = new Map(observations.map((observation) => [observation.id, observation]));
|
|
81
|
+
const selected = ids.flatMap((id) => {
|
|
82
|
+
const observation = byId.get(id);
|
|
83
|
+
return observation ? [observation] : [];
|
|
84
|
+
});
|
|
85
|
+
return summarizeCoverageByRelevance(selected, coverageById);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function emptyCoverageTransitionSummaryByRelevance(): CoverageTransitionSummaryByRelevance {
|
|
89
|
+
return {
|
|
90
|
+
low: {},
|
|
91
|
+
medium: {},
|
|
92
|
+
high: {},
|
|
93
|
+
critical: {},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function summarizeCoverageTransitionsByRelevance(
|
|
98
|
+
observations: readonly Observation[],
|
|
99
|
+
beforeCoverageById: ReadonlyMap<string, ReflectionCoverageTier>,
|
|
100
|
+
afterCoverageById: ReadonlyMap<string, ReflectionCoverageTier>,
|
|
101
|
+
): CoverageTransitionSummaryByRelevance {
|
|
102
|
+
const summary = emptyCoverageTransitionSummaryByRelevance();
|
|
103
|
+
for (const observation of observations) {
|
|
104
|
+
const before = beforeCoverageById.get(observation.id) ?? "none";
|
|
105
|
+
const after = afterCoverageById.get(observation.id) ?? "none";
|
|
106
|
+
if (before === after) continue;
|
|
107
|
+
const key = `${before}->${after}`;
|
|
108
|
+
const bucket = summary[observation.relevance][key] ?? { count: 0, tokens: 0 };
|
|
109
|
+
bucket.count++;
|
|
110
|
+
bucket.tokens += observation.tokenCount;
|
|
111
|
+
summary[observation.relevance][key] = bucket;
|
|
112
|
+
}
|
|
113
|
+
return summary;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function observationToDropperLine(
|
|
117
|
+
observation: Observation,
|
|
118
|
+
coverage: ReflectionCoverageTier,
|
|
119
|
+
): string {
|
|
120
|
+
return `[${observation.id}] ${observation.timestamp} [${observation.relevance}] [coverage: ${coverage}] ${observation.content}`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function coverageTierForObservation(
|
|
124
|
+
observation: Observation,
|
|
125
|
+
coverageById: ReadonlyMap<string, ReflectionCoverageTier>,
|
|
126
|
+
): ReflectionCoverageTier {
|
|
127
|
+
return coverageById.get(observation.id) ?? "none";
|
|
128
|
+
}
|
|
@@ -6,30 +6,30 @@ Your job is to identify only the safest active observations to remove from compa
|
|
|
6
6
|
|
|
7
7
|
Active-memory framing. Dropping an observation removes it from active compacted memory; it does not erase the ledger history or source evidence. Still, future compressed context will no longer show the observation, so only drop it when its durable meaning is safely captured elsewhere or it is genuinely low-signal and carries no unique future value.
|
|
8
8
|
|
|
9
|
-
The user message includes
|
|
10
|
-
|
|
11
|
-
Urgency guidance:
|
|
12
|
-
- low urgency: only propose trivially safe drops, usually low-signal observations with no unique detail.
|
|
13
|
-
- medium urgency: perform conservative cleanup; prefer low observations and clearly redundant medium observations.
|
|
14
|
-
- high urgency: cleanup is more useful, but preservation rules do not weaken and load-bearing memory must still be kept.
|
|
9
|
+
The user message includes the active observation pool target and "Maximum drops allowed this run". The maximum is a hard upper bound sized to move the pool toward the target if every proposed drop is clearly safe. It is not a target. Do not try to fill it. Drop fewer or none when fewer observations are safely removable. When the active pool is far over target, make a thorough pass over safe candidates rather than stopping after a few obvious examples.
|
|
15
10
|
|
|
16
11
|
What to drop, in priority order:
|
|
17
12
|
- Redundant observations whose durable meaning is already captured by current reflections with equivalent fidelity.
|
|
18
13
|
- Superseded observations where a later observation clearly replaces the older state.
|
|
19
14
|
- Repeated routine tool acknowledgements or low-signal progress updates that do not carry decisions, constraints, exact errors, or user-specific facts.
|
|
20
|
-
- Older
|
|
15
|
+
- Older observations that no longer carry working context and are covered by a reflection or a newer observation.
|
|
16
|
+
|
|
17
|
+
Age-gradient rule. Recent observations carry working context the assistant may still need; older observations have usually been summarized elsewhere or are no longer load-bearing. Prefer older safe drops before newer working context, but age alone is not enough to drop important or uniquely load-bearing observations.
|
|
21
18
|
|
|
22
|
-
|
|
19
|
+
Reflection coverage guidance. Each observation line includes [coverage: none|partial|strong]. Coverage is evidence, not an automatic decision:
|
|
20
|
+
- none: no current reflection cites this observation id. Be cautious, especially for high or critical observations.
|
|
21
|
+
- partial: one current reflection cites this observation id. Compare the observation to the reflection before dropping.
|
|
22
|
+
- strong: two or more current reflections cite this observation id. This is stronger evidence that the durable meaning is preserved, but you must still keep uniquely load-bearing or uncertain observations.
|
|
23
23
|
|
|
24
|
-
Relevance guidance:
|
|
24
|
+
Relevance guidance. Relevance is importance/resistance, not an absolute keep/drop lock:
|
|
25
25
|
- low: consider first, but drop only when it carries no unique detail, decision, state, error, identifier, or user-specific fact.
|
|
26
26
|
- medium: drop when redundant with reflections or other observations, or when the work state is clearly obsolete.
|
|
27
27
|
- high: drop only when clearly superseded or already captured by a reflection with equivalent fidelity.
|
|
28
|
-
- critical:
|
|
28
|
+
- critical: highest importance and strongest resistance. Do not drop fresh or uniquely load-bearing critical observations. Critical observations may be dropped only with strong semantic evidence such as age plus partial/strong reflection coverage, supersession by newer memory, redundancy, or clear obsolescence.
|
|
29
29
|
|
|
30
|
-
User assertions and concrete completions
|
|
30
|
+
User assertions and concrete completions must be preserved unless a current reflection or newer observation preserves the exact assertion/completion and its important details with equivalent fidelity.
|
|
31
31
|
|
|
32
|
-
Preservation floor. Regardless of relevance label,
|
|
32
|
+
Preservation floor. Regardless of relevance label, budget pressure, coverage, or age, do not drop observations that uniquely carry any of the following:
|
|
33
33
|
- User preferences, constraints, corrections, or identity/role facts.
|
|
34
34
|
- Concrete completions that future runs must not redo.
|
|
35
35
|
- Named identifiers, file paths, function names, package names, tickets, commit SHAs, handles, or exact commands.
|
|
@@ -101,7 +101,7 @@ If a detail is non-obvious from the code or git history, it belongs in the obser
|
|
|
101
101
|
|
|
102
102
|
Relevance levels (pick one per observation; this field drives future dropping):
|
|
103
103
|
|
|
104
|
-
- critical: user assertions about identity, role, or persistent preferences; explicit corrections ("no, don't do X"); concrete completions that future runs MUST NOT redo. These are load-bearing and
|
|
104
|
+
- critical: user assertions about identity, role, or persistent preferences; explicit corrections ("no, don't do X"); concrete completions that future runs MUST NOT redo. These are highest-resistance, load-bearing observations and require the strongest evidence before leaving active memory. Why this matters: if a "critical" item is lost, the assistant may redo finished work, contradict a correction, or misrepresent who the user is.
|
|
105
105
|
- high: non-trivial technical decisions, architectural direction, unresolved blockers, key constraints. Worth keeping across many compactions.
|
|
106
106
|
- medium: task-level context that helps within the current work but isn't durable. The default when you are unsure between medium and high.
|
|
107
107
|
- low: routine tool-call acks, repetitive status updates, content trivially re-derivable from recent messages. The dropper will drop these first.
|
|
@@ -6,7 +6,8 @@ Your task is different from the observer's: you are not recording events, you ar
|
|
|
6
6
|
|
|
7
7
|
You receive:
|
|
8
8
|
- Current reflections: durable facts already crystallized.
|
|
9
|
-
- Current observations: active timestamped evidence lines, each shown as "[id] YYYY-MM-DD HH:MM [relevance] content".
|
|
9
|
+
- Current observations: active timestamped evidence lines, each shown as "[id] YYYY-MM-DD HH:MM [relevance] [coverage: none|partial|strong] content".
|
|
10
|
+
- Coverage tiers are review context: none means no current reflection supports the observation id, partial means exactly one current reflection supports it, and strong means two or more current reflections support it. Coverage is not a quota, target, priority score, or instruction to emit reflections.
|
|
10
11
|
|
|
11
12
|
What to emit:
|
|
12
13
|
- Emit only new durable reflections not already present in current reflections.
|
|
@@ -39,13 +40,16 @@ Focus on:
|
|
|
39
40
|
- Completed outcomes future runs must not redo.
|
|
40
41
|
- Durable blockers, invariants, and open decisions that should survive compaction.
|
|
41
42
|
|
|
42
|
-
Support ids:
|
|
43
|
+
Support ids and coverage stewardship:
|
|
43
44
|
- Every reflection must include supportingObservationIds from the current observations list.
|
|
44
|
-
-
|
|
45
|
-
- supportingObservationIds are
|
|
45
|
+
- First decide whether the reflection content passes the durable-value bar. Then audit support ids for that already-worthy reflection.
|
|
46
|
+
- supportingObservationIds are a coverage/provenance set and downstream dropper coverage evidence: include all current observation ids whose durable meaning is preserved by the reflection with equivalent fidelity and can later be treated as redundant active-memory detail.
|
|
47
|
+
- supportingObservationIds are not a checklist to cover every observation. Do not add ids merely to improve coverage counts, maximize support ids, maximize strong coverage, or unlock the dropper.
|
|
48
|
+
- False or inflated support ids can cause unsafe downstream dropper pruning, including removal of high-resistance active observations whose meaning was not actually preserved.
|
|
46
49
|
- Include additional observation ids only when the reflection preserves their durable meaning with equivalent fidelity.
|
|
47
50
|
- Leave observations unsupported when their details are still active working state, too specific to compress safely, or not yet durable enough.
|
|
48
51
|
- Do not include observations whose unique exact detail, current task state, user correction, user constraint, or concrete completion is not captured by the reflection.
|
|
52
|
+
- If no candidate reflection passes the durable-value bar, emit zero reflections even when observations have coverage: none.
|
|
49
53
|
- Never invent observation ids. Proposals with missing, empty, or invalid supportingObservationIds are rejected.
|
|
50
54
|
|
|
51
55
|
User assertions are authoritative. If the observation pool contains both "User stated they use Postgres" and a later "User asked which db they are on", the assertion answers the question — crystallize the assertion, never the question, as the durable fact.
|
package/src/om/consolidation.ts
CHANGED
|
@@ -49,6 +49,8 @@ import {
|
|
|
49
49
|
rawTokensSinceReflectionCoverage,
|
|
50
50
|
reflectionToSummaryLine,
|
|
51
51
|
reflectionsCreatedAfterIndex,
|
|
52
|
+
scoreObservation,
|
|
53
|
+
selectPriorObservations,
|
|
52
54
|
type Entry,
|
|
53
55
|
type Observation,
|
|
54
56
|
type Reflection,
|
|
@@ -125,54 +127,7 @@ function mergeReflections(existing: Reflection[], additional: Reflection[]): Ref
|
|
|
125
127
|
return merged;
|
|
126
128
|
}
|
|
127
129
|
|
|
128
|
-
/** Score an observation for preamble cap selection.
|
|
129
|
-
* Relevance tier dominates: medium (5+) always outranks low (max 2).
|
|
130
|
-
* Recency is based on position in the flat-mapped array (0 = oldest, N-1 = newest),
|
|
131
|
-
* avoiding wall-clock dependency that punishes sessions spanning days or weeks. */
|
|
132
|
-
function scoreObservation(obs: Observation, index: number, total: number): number {
|
|
133
|
-
const base = obs.relevance === "high" || obs.relevance === "critical" ? 10
|
|
134
|
-
: obs.relevance === "medium" ? 5 : 1;
|
|
135
|
-
const recency = total > 1 ? index / (total - 1) : 1;
|
|
136
|
-
return base + recency;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** Select observations for the observer preamble, keeping all high-relevance items
|
|
140
|
-
* unconditionally and filling the remaining token budget with the best-scoring
|
|
141
|
-
* medium and low observations (relevance-tiered + recency).
|
|
142
|
-
*
|
|
143
|
-
* Reflections are never trimmed — they are inherently rare and always stay. */
|
|
144
|
-
function selectPriorObservations(observations: Observation[], maxTokens: number): Observation[] {
|
|
145
|
-
// Track original indices so we can restore chronological order after scoring
|
|
146
|
-
const indexed = observations.map((obs, i) => ({ obs, originalIndex: i }));
|
|
147
|
-
const high = indexed.filter(item => item.obs.relevance === "high" || item.obs.relevance === "critical");
|
|
148
|
-
const rest = indexed.filter(item => item.obs.relevance !== "high" && item.obs.relevance !== "critical");
|
|
149
|
-
|
|
150
|
-
// High always kept — consume budget first
|
|
151
|
-
let budget = maxTokens;
|
|
152
|
-
const selected = new Set<{ obs: Observation; originalIndex: number }>();
|
|
153
|
-
for (const item of high) {
|
|
154
|
-
const lineTokens = Math.ceil(observationToSummaryLine(item.obs).length / 4);
|
|
155
|
-
selected.add(item);
|
|
156
|
-
budget -= lineTokens;
|
|
157
|
-
}
|
|
158
130
|
|
|
159
|
-
// Score medium + low and select best within remaining budget
|
|
160
|
-
if (rest.length > 0 && budget > 0) {
|
|
161
|
-
const scored = rest.map((item, i) => ({ item, score: scoreObservation(item.obs, i, rest.length) }));
|
|
162
|
-
scored.sort((a, b) => b.score - a.score); // highest score first
|
|
163
|
-
for (const { item } of scored) {
|
|
164
|
-
const lineTokens = Math.ceil(observationToSummaryLine(item.obs).length / 4);
|
|
165
|
-
if (budget - lineTokens < 0) break;
|
|
166
|
-
selected.add(item);
|
|
167
|
-
budget -= lineTokens;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Restore original chronological order before returning
|
|
172
|
-
return Array.from(selected)
|
|
173
|
-
.sort((a, b) => a.originalIndex - b.originalIndex)
|
|
174
|
-
.map(item => item.obs);
|
|
175
|
-
}
|
|
176
131
|
|
|
177
132
|
/**
|
|
178
133
|
* Extract all pending observations from accumulated batches that were recorded
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Upstream: https://github.com/elpapi42/pi-observational-memory (src/session-ledger/projection.ts)
|
|
5
5
|
* Unmodified.
|
|
6
6
|
*/
|
|
7
|
+
import { selectPriorObservations } from "./render-summary.js";
|
|
7
8
|
import {
|
|
8
9
|
OM_FOLDED,
|
|
9
10
|
isMemoryDetails,
|
|
@@ -207,10 +208,21 @@ export function buildCompactionProjection(
|
|
|
207
208
|
0,
|
|
208
209
|
);
|
|
209
210
|
const fullFold = observationTokens >= config.observationsPoolMaxTokens;
|
|
210
|
-
|
|
211
|
+
let projection = fullFold
|
|
211
212
|
? fullProjection(entries, firstKeptEntryId)
|
|
212
213
|
: normalProjection;
|
|
213
214
|
|
|
215
|
+
// Cap observations to budget using relevance-tiered + recency scoring.
|
|
216
|
+
// Even if the dropper determined some old observations are worth keeping,
|
|
217
|
+
// this safety valve ensures the compaction output never exceeds the pool
|
|
218
|
+
// token budget. Observations survive in the branch regardless.
|
|
219
|
+
if (config.observationsPoolMaxTokens > 0 && observationTokens >= config.observationsPoolMaxTokens) {
|
|
220
|
+
projection = {
|
|
221
|
+
observations: selectPriorObservations(projection.observations, config.observationsPoolMaxTokens),
|
|
222
|
+
reflections: projection.reflections,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
214
226
|
const details: MemoryDetails = {
|
|
215
227
|
type: OM_FOLDED,
|
|
216
228
|
version: 1,
|
|
@@ -25,6 +25,57 @@ export function observationToSummaryLine(observation: Observation): string {
|
|
|
25
25
|
return `[${observation.id}] ${observation.timestamp} [${observation.relevance}] ${observation.content}`;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/** Score an observation for cap/trim selection.
|
|
29
|
+
* Relevance tier dominates: medium (5+) always outranks low (max 2).
|
|
30
|
+
* Recency is based on position in the flat-mapped array (0 = oldest, N-1 = newest),
|
|
31
|
+
* avoiding wall-clock dependency that punishes sessions spanning days or weeks. */
|
|
32
|
+
export function scoreObservation(obs: Observation, index: number, total: number): number {
|
|
33
|
+
const base = obs.relevance === "high" || obs.relevance === "critical" ? 10
|
|
34
|
+
: obs.relevance === "medium" ? 5 : 1;
|
|
35
|
+
const recency = total > 1 ? index / (total - 1) : 1;
|
|
36
|
+
return base + recency;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Select observations up to a token budget, keeping all high-relevance items
|
|
40
|
+
* unconditionally and filling the remaining budget with the best-scoring
|
|
41
|
+
* medium and low observations (relevance-tiered + recency).
|
|
42
|
+
*
|
|
43
|
+
* Reflections are never trimmed — they are inherently rare and always stay.
|
|
44
|
+
* Observations stay in the branch either way; this only caps what is rendered
|
|
45
|
+
* in the compaction summary output. */
|
|
46
|
+
export function selectPriorObservations(observations: Observation[], maxTokens: number): Observation[] {
|
|
47
|
+
// Track original indices so we can restore chronological order after scoring
|
|
48
|
+
const indexed = observations.map((obs, i) => ({ obs, originalIndex: i }));
|
|
49
|
+
const high = indexed.filter(item => item.obs.relevance === "high" || item.obs.relevance === "critical");
|
|
50
|
+
const rest = indexed.filter(item => item.obs.relevance !== "high" && item.obs.relevance !== "critical");
|
|
51
|
+
|
|
52
|
+
// High always kept — consume budget first
|
|
53
|
+
let budget = maxTokens;
|
|
54
|
+
const selected = new Set<{ obs: Observation; originalIndex: number }>();
|
|
55
|
+
for (const item of high) {
|
|
56
|
+
const lineTokens = Math.ceil(observationToSummaryLine(item.obs).length / 4);
|
|
57
|
+
selected.add(item);
|
|
58
|
+
budget -= lineTokens;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Score medium + low and select best within remaining budget
|
|
62
|
+
if (rest.length > 0 && budget > 0) {
|
|
63
|
+
const scored = rest.map((item, i) => ({ item, score: scoreObservation(item.obs, i, rest.length) }));
|
|
64
|
+
scored.sort((a, b) => b.score - a.score); // highest score first
|
|
65
|
+
for (const { item } of scored) {
|
|
66
|
+
const lineTokens = Math.ceil(observationToSummaryLine(item.obs).length / 4);
|
|
67
|
+
if (budget - lineTokens < 0) break;
|
|
68
|
+
selected.add(item);
|
|
69
|
+
budget -= lineTokens;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Restore original chronological order before returning
|
|
74
|
+
return Array.from(selected)
|
|
75
|
+
.sort((a, b) => a.originalIndex - b.originalIndex)
|
|
76
|
+
.map(item => item.obs);
|
|
77
|
+
}
|
|
78
|
+
|
|
28
79
|
export function reflectionToSummaryLine(reflection: Reflection): string {
|
|
29
80
|
return `[${reflection.id}] ${reflection.content}`;
|
|
30
81
|
}
|