pi-mega-compact 0.6.1 → 0.6.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 +39 -23
- package/dist/extensions/conflict-scan.test.js +115 -0
- package/dist/extensions/mega-conflict-cmds.js +17 -0
- package/dist/extensions/mega-events.js +102 -6
- package/dist/extensions/mega-runtime.js +20 -0
- package/dist/extensions/mega-teamrun.test.js +143 -0
- package/dist/extensions/mega-trim.js +33 -2
- package/dist/src/memoryOps.js +42 -2
- package/dist/src/memoryRecall.js +48 -0
- package/dist/src/memoryRecall.test.js +52 -0
- package/dist/src/recall.js +34 -8
- package/dist/src/store/memoryIndex.js +230 -0
- package/dist/src/store/memoryIndex.test.js +51 -0
- package/dist/src/store/vectorIndex.js +32 -7
- package/extensions/mega-conflict-cmds.ts +15 -0
- package/extensions/mega-events.ts +96 -6
- package/extensions/mega-runtime.ts +21 -0
- package/extensions/mega-teamrun.test.ts +164 -0
- package/extensions/mega-trim.ts +28 -1
- package/package.json +1 -1
- package/src/memoryOps.ts +42 -2
- package/src/memoryRecall.test.ts +58 -0
- package/src/memoryRecall.ts +52 -0
- package/src/recall.ts +35 -10
- package/src/store/memoryIndex.test.ts +61 -0
- package/src/store/memoryIndex.ts +269 -0
- package/src/store/vectorIndex.ts +40 -6
- package/dist/extensions/openclaw-mega-compact.js +0 -291
- package/dist/src/minilm.js +0 -92
- package/dist/src/wordpiece.js +0 -129
|
@@ -11,6 +11,8 @@ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-a
|
|
|
11
11
|
import { detectConflicts, type ConflictReport } from "./conflict-scan.js";
|
|
12
12
|
import { addMemory, listMemories, searchMemories, recallMemory, type MemoryRecord } from "../src/store/sqlite.js";
|
|
13
13
|
import { resolveRepoRoot } from "./mega-config.js";
|
|
14
|
+
import { defaultEmbedder } from "../src/embedder.js";
|
|
15
|
+
import { upsertMemoryEmbedding } from "../src/store/memoryIndex.js";
|
|
14
16
|
import { MegaRuntime } from "./mega-runtime.js";
|
|
15
17
|
|
|
16
18
|
/** Run the conflict scan and format a human-readable report. */
|
|
@@ -82,6 +84,13 @@ export function registerConflictCommands(pi: ExtensionAPI, runtime: MegaRuntime)
|
|
|
82
84
|
const tagMatches = [...text.matchAll(/#([\w-]+)/g)].map((m) => m[1]);
|
|
83
85
|
const content = text.replace(/#[\w-]+/g, "").trim();
|
|
84
86
|
const id = addMemory({ content, tags: tagMatches }, repo, runtime.currentStateDir);
|
|
87
|
+
// S24: mirror into the cross-repo memory index (fire-and-forget).
|
|
88
|
+
try {
|
|
89
|
+
const vec = defaultEmbedder().embed(content);
|
|
90
|
+
void upsertMemoryEmbedding(repo, id, content, vec);
|
|
91
|
+
} catch {
|
|
92
|
+
/* non-fatal */
|
|
93
|
+
}
|
|
85
94
|
ctx.ui.notify(`[mega-memory] saved #${id} to ${repo.split(/[\\/]/).pop()}`);
|
|
86
95
|
return;
|
|
87
96
|
}
|
|
@@ -151,6 +160,12 @@ export function registerConflictCommands(pi: ExtensionAPI, runtime: MegaRuntime)
|
|
|
151
160
|
const tagMatches = [...text.matchAll(/#([\w-]+)/g)].map((m) => m[1]);
|
|
152
161
|
const content = text.replace(/#[\w-]+/g, "").trim();
|
|
153
162
|
const id = addMemory({ content, tags: tagMatches }, repo, runtime.currentStateDir);
|
|
163
|
+
try {
|
|
164
|
+
const vec = defaultEmbedder().embed(content);
|
|
165
|
+
void upsertMemoryEmbedding(repo, id, content, vec);
|
|
166
|
+
} catch {
|
|
167
|
+
/* non-fatal */
|
|
168
|
+
}
|
|
154
169
|
ctx.ui.notify(`[/m] saved #${id} to ${repo.split(/[\\/]/).pop()}`);
|
|
155
170
|
return;
|
|
156
171
|
}
|
|
@@ -19,8 +19,18 @@ import { driveNativeCompaction } from "./mega-compact-driver.js";
|
|
|
19
19
|
import { computeLiveTrimCut, liveTrimSummaryMessage } from "./mega-trim.js";
|
|
20
20
|
import { pressureFromPct, memoryReviewCadence, type MegaConfig } from "./mega-config.js";
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* DIAG accessor for the headless test harness: the most recently constructed
|
|
24
|
+
* MegaRuntime, so a test that loads the compiled extension via its default
|
|
25
|
+
* export can read diag counters (diagLiveTrimFires / diagBeforeCompactFires /
|
|
26
|
+
* diagBeforeCompactSupplied / diagAgentEndIdle) after firing synthetic events.
|
|
27
|
+
* No-op in production — nothing reads this outside tests.
|
|
28
|
+
*/
|
|
29
|
+
export let lastRuntime: MegaRuntime | undefined;
|
|
30
|
+
|
|
22
31
|
/** Register all pi lifecycle event handlers. */
|
|
23
32
|
export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, config: MegaConfig): void {
|
|
33
|
+
lastRuntime = runtime;
|
|
24
34
|
// ---- Session lifecycle (state reset points) -------------------------------
|
|
25
35
|
// Capture model/provider whenever it changes (drives real cost estimation).
|
|
26
36
|
pi.on("model_select", async (_event, ctx) => {
|
|
@@ -61,6 +71,8 @@ export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, co
|
|
|
61
71
|
try {
|
|
62
72
|
const mr = await recallMemoriesAndInline({
|
|
63
73
|
query, stateDir: runtime.getStateDir(), limit: 5,
|
|
74
|
+
crossRepo: config.crossRepoEnabled,
|
|
75
|
+
crossRepoCosine: config.crossRepoCosine,
|
|
64
76
|
});
|
|
65
77
|
if (!mr.empty) runtime.pendingMemoryRecallBlock = mr.block;
|
|
66
78
|
} catch (err) {
|
|
@@ -86,7 +98,7 @@ export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, co
|
|
|
86
98
|
}
|
|
87
99
|
// S21: parallel memory recall. Trigram embedder is sub-ms; await is fine.
|
|
88
100
|
try {
|
|
89
|
-
const mr = await recallMemoriesAndInline({ query, stateDir: runtime.getStateDir(), limit: 5 });
|
|
101
|
+
const mr = await recallMemoriesAndInline({ query, stateDir: runtime.getStateDir(), limit: 5, crossRepo: config.crossRepoEnabled, crossRepoCosine: config.crossRepoCosine });
|
|
90
102
|
if (!mr.empty) runtime.pendingMemoryRecallBlock = mr.block;
|
|
91
103
|
} catch (err) {
|
|
92
104
|
runtime.logger.warn("memory-recall skipped", { err: String(err) });
|
|
@@ -143,6 +155,46 @@ export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, co
|
|
|
143
155
|
const idle = ctx.isIdle?.() ?? true;
|
|
144
156
|
const queued = ctx.hasPendingMessages?.() ?? false;
|
|
145
157
|
const now = Date.now();
|
|
158
|
+
// DIAG (team-run relief): surface whether the agent is idle + over
|
|
159
|
+
// threshold at agent_end so we can see if a mid-run durable-trim trigger
|
|
160
|
+
// *should* have fired but didn't.
|
|
161
|
+
const overThreshold = (runtime.lastCtxTokens ?? 0) >= config.thresholdTokens;
|
|
162
|
+
runtime.diagAgentEndIdle++;
|
|
163
|
+
runtime.logger.info("agent-end-idle", {
|
|
164
|
+
sessionId: runtime.rt.sessionId,
|
|
165
|
+
idle,
|
|
166
|
+
queued,
|
|
167
|
+
overThreshold,
|
|
168
|
+
ctxPct: runtime.lastCtxPercent,
|
|
169
|
+
ctxTokens: runtime.lastCtxTokens,
|
|
170
|
+
thresholdTokens: config.thresholdTokens,
|
|
171
|
+
wouldNudge: idle && queued && now >= runtime.resumeNudgeUntil,
|
|
172
|
+
});
|
|
173
|
+
// S16+S24: MID-RUN DURABLE TRIM. During a long team run (sub-agents),
|
|
174
|
+
// pi's native durable compaction only fires from _checkCompaction at
|
|
175
|
+
// PARENT settle (agent-session.js:760/844), so the on-disk transcript +
|
|
176
|
+
// context meter balloon to ~150k and never relieve until the very end
|
|
177
|
+
// ("compacts but doesn't resume"). agent_end with activeAgents===0 is a
|
|
178
|
+
// SAFE, settled point: calling ctx.compact() here does NOT abort an
|
|
179
|
+
// in-flight turn (the S16 danger is only mid-turn). ctx.compact() runs
|
|
180
|
+
// pi's flow, which fires our session_before_compact handler to supply
|
|
181
|
+
// the durable trim (truncates the transcript from firstKeptEntryId).
|
|
182
|
+
// Guarded three ways: only when truly idle + over threshold, only when
|
|
183
|
+
// pi would actually compact (piCompactWouldNoop skips the user-facing
|
|
184
|
+
// no-op throw), and debounced (one durable trim per 2s) to avoid
|
|
185
|
+
// thrashing the transcript while sub-agents keep settling.
|
|
186
|
+
if (idle && overThreshold && now >= runtime.debounceUntil) {
|
|
187
|
+
if (!piCompactWouldNoop(ctx)) {
|
|
188
|
+
runtime.debounceUntil = now + 2000;
|
|
189
|
+
runtime.diagAgentEndDurable++;
|
|
190
|
+
runtime.logger.info("agent-end-durable-trigger", {
|
|
191
|
+
sessionId: runtime.rt.sessionId,
|
|
192
|
+
ctxTokens: runtime.lastCtxTokens,
|
|
193
|
+
thresholdTokens: config.thresholdTokens,
|
|
194
|
+
});
|
|
195
|
+
ctx.compact({ customInstructions: undefined }); // guardrails-allow PREVENT-PI-004: local ctx.compact() — no network; agent settled so no in-flight abort
|
|
196
|
+
}
|
|
197
|
+
}
|
|
146
198
|
if (idle && queued && now >= runtime.resumeNudgeUntil) {
|
|
147
199
|
runtime.resumeNudgeUntil = now + 30_000;
|
|
148
200
|
pi.sendUserMessage("[mega-compact] continue from the compacted context above.");
|
|
@@ -215,21 +267,21 @@ export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, co
|
|
|
215
267
|
Math.round((pct / 100) * (usage?.contextWindow ?? 0));
|
|
216
268
|
|
|
217
269
|
// FAST GATE: token-based (tier threshold), not percentage-based.
|
|
218
|
-
if (currentTokens < config.thresholdTokens) return;
|
|
270
|
+
if (currentTokens < config.thresholdTokens) { runtime.diagCtxFastGate++; return; }
|
|
219
271
|
|
|
220
272
|
const check = autoCompactCheck(currentTokens, config.thresholdTokens); // SERVER-STYLE CONFIRM (local)
|
|
221
|
-
if (!check.shouldCompact) return;
|
|
273
|
+
if (!check.shouldCompact) { runtime.diagCtxNoCompact++; return; }
|
|
222
274
|
|
|
223
275
|
// Debounce so we don't fire on every context event past threshold.
|
|
224
276
|
const now = Date.now();
|
|
225
|
-
if (now < runtime.debounceUntil) return;
|
|
277
|
+
if (now < runtime.debounceUntil) { runtime.diagCtxDebounce++; return; }
|
|
226
278
|
runtime.debounceUntil = now + 2000;
|
|
227
279
|
|
|
228
280
|
// Adaptive compression (Fix E): scale compression strength + keepFrom depth
|
|
229
281
|
// with how close we are to the model context limit.
|
|
230
282
|
const pressure = pressureFromPct(pct);
|
|
231
283
|
const ran = runCompact(pi, runtime, config, ctx, messages, { compressionPressure: pressure });
|
|
232
|
-
if (ran.skipped) return;
|
|
284
|
+
if (ran.skipped) { runtime.diagCtxRunSkipped++; return; }
|
|
233
285
|
|
|
234
286
|
// LEGACY path (rollback): v0.4.28 ctx.compact() + the no-op gate. The
|
|
235
287
|
// manual compact path aborts the in-flight turn — only used behind the flag.
|
|
@@ -262,7 +314,16 @@ export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, co
|
|
|
262
314
|
summary: ran.result.summary,
|
|
263
315
|
anchorUserMessages,
|
|
264
316
|
});
|
|
265
|
-
if (cut === null)
|
|
317
|
+
if (cut === null) {
|
|
318
|
+
runtime.diagCtxCutNull++;
|
|
319
|
+
runtime.logger.info("live-trim-skip", {
|
|
320
|
+
sessionId: runtime.rt.sessionId,
|
|
321
|
+
compactedFrom: ran.result.compactedFrom,
|
|
322
|
+
viewLen: view.length,
|
|
323
|
+
anchorUserMessages,
|
|
324
|
+
});
|
|
325
|
+
return; // unsafe / below anchor floor — no trim this call
|
|
326
|
+
}
|
|
266
327
|
const summaryMsg = liveTrimSummaryMessage({
|
|
267
328
|
compactedFrom: ran.result.compactedFrom,
|
|
268
329
|
summary: ran.result.summary,
|
|
@@ -276,8 +337,22 @@ export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, co
|
|
|
276
337
|
} as unknown as AgentMessage;
|
|
277
338
|
const recent = messages.slice(cut); // guardrails-allow PREVENT-PI-002: `cut` is the pre-sanitized `compactedFrom` produced by src/boundary.ts computeDropRange, so the preserved run begins on a toolPair-safe index.
|
|
278
339
|
runtime.snapshot(ctx);
|
|
340
|
+
// DIAG (team-run relief): confirm the live trim actually fires + how big
|
|
341
|
+
// the window still is. The return is non-durable (per-LLM-call only), so
|
|
342
|
+
// this is the signal that the model is being fed a compacted view while
|
|
343
|
+
// the on-disk transcript + context meter keep growing.
|
|
344
|
+
runtime.diagLiveTrimFires++;
|
|
345
|
+
runtime.logger.info("live-trim", {
|
|
346
|
+
sessionId: runtime.rt.sessionId,
|
|
347
|
+
inputMsgs: messages.length,
|
|
348
|
+
outputMsgs: recent.length + 1,
|
|
349
|
+
compactedFrom: cut,
|
|
350
|
+
ctxPct: pct,
|
|
351
|
+
ctxTokens: usage?.tokens ?? null,
|
|
352
|
+
});
|
|
279
353
|
return { messages: [summaryAgentMsg, ...recent] };
|
|
280
354
|
} catch {
|
|
355
|
+
runtime.diagCtxThrown++;
|
|
281
356
|
return; // non-fatal: no trim this call; the next context event retries
|
|
282
357
|
}
|
|
283
358
|
});
|
|
@@ -290,10 +365,25 @@ export function registerEventHandlers(pi: ExtensionAPI, runtime: MegaRuntime, co
|
|
|
290
365
|
// there is no full-reload + additive recall inflation.
|
|
291
366
|
pi.on("session_before_compact", async (event: SessionBeforeCompactEvent, ctx: ExtensionContext) => {
|
|
292
367
|
runtime.resetRuntime(ctx.sessionManager.getSessionId());
|
|
368
|
+
// DIAG (team-run relief): this is the ONLY durable-trim entry point. Log
|
|
369
|
+
// every fire + whether we supplied a compaction (truncates transcript) or
|
|
370
|
+
// fell through to {} (pi runs its own). If this is sparse during a team
|
|
371
|
+
// run, the durable trim is firing too late (only at parent settle).
|
|
372
|
+
const prep = event.preparation;
|
|
373
|
+
runtime.diagBeforeCompactFires++;
|
|
374
|
+
runtime.logger.info("before-compact-entry", {
|
|
375
|
+
sessionId: runtime.rt.sessionId,
|
|
376
|
+
reason: event.reason,
|
|
377
|
+
hasPrep: !!prep,
|
|
378
|
+
msgsToSummarize: prep?.messagesToSummarize?.length ?? 0,
|
|
379
|
+
firstKeptEntryId: prep?.firstKeptEntryId ?? null,
|
|
380
|
+
activeAgents: runtime.activeAgents,
|
|
381
|
+
});
|
|
293
382
|
if (!config.auto) return {}; // let pi run its own native compaction
|
|
294
383
|
try {
|
|
295
384
|
const result = driveNativeCompaction(event, runtime, config);
|
|
296
385
|
if (result) {
|
|
386
|
+
runtime.diagBeforeCompactSupplied++;
|
|
297
387
|
runtime.logger.info("native-compact", {
|
|
298
388
|
sessionId: runtime.rt.sessionId,
|
|
299
389
|
firstKeptEntryId: result.compaction.firstKeptEntryId,
|
|
@@ -143,6 +143,27 @@ export class MegaRuntime {
|
|
|
143
143
|
lastCtxPercent: number | null = null;
|
|
144
144
|
lastCtxWindow = 0;
|
|
145
145
|
|
|
146
|
+
/**
|
|
147
|
+
* DIAG counters for the "team run doesn't relieve context" investigation.
|
|
148
|
+
* Plain integers, incremented at the three compaction decision points. They
|
|
149
|
+
* let a headless test drive the real event handlers and assert the firing
|
|
150
|
+
* cadence without scraping log files. Inert in production (the live-trim and
|
|
151
|
+
* before-compact probes also emit logger.info, but these counters are always
|
|
152
|
+
* updated and cost nothing).
|
|
153
|
+
*/
|
|
154
|
+
diagLiveTrimFires = 0; // context handler returned a trimmed view
|
|
155
|
+
diagBeforeCompactFires = 0; // session_before_compact handler entered
|
|
156
|
+
diagBeforeCompactSupplied = 0; // session_before_compact supplied our trim
|
|
157
|
+
diagAgentEndIdle = 0; // agent_end with activeAgents===0
|
|
158
|
+
diagAgentEndDurable = 0; // agent_end fired ctx.compact() (mid-run durable trim)
|
|
159
|
+
// Per-skip-path counters for the team-run diagnosis.
|
|
160
|
+
diagCtxFastGate = 0; // returned at token fast-gate (below threshold)
|
|
161
|
+
diagCtxNoCompact = 0; // autoCompactCheck().shouldCompact === false
|
|
162
|
+
diagCtxDebounce = 0; // debounceUntil not yet elapsed
|
|
163
|
+
diagCtxRunSkipped = 0; // runCompact() returned skipped
|
|
164
|
+
diagCtxCutNull = 0; // computeLiveTrimCut returned null (anchor/boundary)
|
|
165
|
+
diagCtxThrown = 0; // live-trim try threw (caught)
|
|
166
|
+
|
|
146
167
|
/**
|
|
147
168
|
* Live 0–1 pressure: how full the context window is relative to the compaction
|
|
148
169
|
* threshold. Computed from the most recent context event the runtime already
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mega-teamrun.test.ts — regression test for the "auto-compact runs but context
|
|
3
|
+
* never relieves during a team run (sub-agents)" bug.
|
|
4
|
+
*
|
|
5
|
+
* Loads the REAL compiled extension (extensions/mega-compact.js) through a
|
|
6
|
+
* faithful mock pi (mirrors mega-compact.test.ts's harness) and drives the
|
|
7
|
+
* exact event sequence a long team run produces:
|
|
8
|
+
*
|
|
9
|
+
* agent_start -> context (over threshold) xN -> agent_end (repeat x3)
|
|
10
|
+
*
|
|
11
|
+
* Asserts the TWO fixes:
|
|
12
|
+
* 1. live trim FIRES per-call (computeLiveTrimCut no longer returns null on
|
|
13
|
+
* the anchor floor — was `cutNull`, liveTrimFires===0 before the fix).
|
|
14
|
+
* 2. the DURABLE trim fires at agent_end while idle + over threshold
|
|
15
|
+
* (mid-run durable trigger), not only at parent settle.
|
|
16
|
+
*
|
|
17
|
+
* The mock ctx.compact() drives session_before_compact so we observe the
|
|
18
|
+
* durable truncation. Counters come from MegaRuntime.diag* (set behind the
|
|
19
|
+
* real handler code, inert in production).
|
|
20
|
+
*
|
|
21
|
+
* MEGACOMPACT_PGLITE_DISABLED keeps the run fast (no WASM index init).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { test } from "node:test";
|
|
25
|
+
import assert from "node:assert/strict";
|
|
26
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
27
|
+
import { tmpdir } from "node:os";
|
|
28
|
+
import { join } from "node:path";
|
|
29
|
+
import { createRequire } from "node:module";
|
|
30
|
+
import { closeVectorIndex } from "../src/store/vectorIndex.js";
|
|
31
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
32
|
+
|
|
33
|
+
const require = createRequire(import.meta.url);
|
|
34
|
+
const baseTmp = mkdtempSync(join(tmpdir(), "mc-team-"));
|
|
35
|
+
process.env.MEGACOMPACT_INDEX_DIR = join(baseTmp, "index");
|
|
36
|
+
process.env.MEGACOMPACT_PGLITE_DISABLED = "true"; // fast: skip WASM index
|
|
37
|
+
let counter = 0;
|
|
38
|
+
|
|
39
|
+
function harness() {
|
|
40
|
+
const stateDir = join(baseTmp, `run-${counter++}`);
|
|
41
|
+
process.env.MEGACOMPACT_STATE_DIR = stateDir;
|
|
42
|
+
process.env.MEGACOMPACT_DEBUG = "true";
|
|
43
|
+
process.env.MEGACOMPACT_THRESHOLD_TOKENS = "50";
|
|
44
|
+
process.env.MEGACOMPACT_FAST_GATE_PCT = "1";
|
|
45
|
+
process.env.MEGACOMPACT_ANCHOR_USER_MESSAGES = "1";
|
|
46
|
+
process.env.MEGACOMPACT_DURABLE_TRIM_FLOOR = "0"; // piCompactWouldNoop must not skip
|
|
47
|
+
process.env.MEGACOMPACT_MEMORY_AUTO_REVIEW = "false";
|
|
48
|
+
process.env.MEGACOMPACT_RAPTOR_ENABLED = "false";
|
|
49
|
+
delete process.env.MEGACOMPACT_LEGACY_DURABLE_TRIM;
|
|
50
|
+
|
|
51
|
+
const handlers: Record<string, Function> = {};
|
|
52
|
+
const compactCalls: any[] = [];
|
|
53
|
+
|
|
54
|
+
function msg(role: string, text: string, toolName?: string): AgentMessage {
|
|
55
|
+
if (role === "assistant" && toolName) {
|
|
56
|
+
return { role: "assistant", content: [{ type: "toolCall", name: toolName, id: "c1", arguments: {} }], api: "anthropic-messages", provider: "anthropic", model: "m", usage: { inputTokens: 1, outputTokens: 1, cacheReadTokens: 0, cacheWriteTokens: 0 }, stopReason: "tool_use", timestamp: 0 } as unknown as AgentMessage;
|
|
57
|
+
}
|
|
58
|
+
if (role === "toolResult" && toolName) {
|
|
59
|
+
return { role: "toolResult", content: [{ type: "text", text }], toolCallId: "c1", toolName, isError: false, timestamp: 0 } as unknown as AgentMessage;
|
|
60
|
+
}
|
|
61
|
+
return { role: "user", content: text, timestamp: 0 } as unknown as AgentMessage;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const session: AgentMessage[] = [];
|
|
65
|
+
for (let i = 0; i < 14; i++) {
|
|
66
|
+
session.push(msg("user", `actually we decided to use approach ${i} for module ${i}`));
|
|
67
|
+
session.push(msg("assistant", `edited module ${i}`, "Edit"));
|
|
68
|
+
session.push(msg("toolResult", `edited module ${i}`, "Edit"));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const toEntry = (m: AgentMessage, i: number): any => ({ type: "message", id: `e${i}`, parentId: null, timestamp: String(i), message: m });
|
|
72
|
+
const sessionManager = {
|
|
73
|
+
getSessionId: () => "sess_team_001",
|
|
74
|
+
getEntries: () => session.map(toEntry),
|
|
75
|
+
getBranch: () => session.map(toEntry),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
function makeCtx(over: Partial<any> = {}) {
|
|
79
|
+
return {
|
|
80
|
+
ui: { setStatus: () => {}, notify: () => {}, select: () => {}, confirm: async () => true, input: async () => "", setWidget: () => {} },
|
|
81
|
+
mode: "tui" as any, hasUI: true, cwd: stateDir, sessionManager,
|
|
82
|
+
modelRegistry: {} as any, model: undefined, isIdle: () => true, isProjectTrusted: () => true,
|
|
83
|
+
signal: undefined, abort: () => {}, hasPendingMessages: () => false, shutdown: () => {},
|
|
84
|
+
getContextUsage: () => ({ tokens: 200000, contextWindow: 200000, percent: 100 }),
|
|
85
|
+
// Mock ctx.compact() runs pi's flow and fires session_before_compact.
|
|
86
|
+
compact: (opts?: any) => {
|
|
87
|
+
compactCalls.push(opts);
|
|
88
|
+
if (handlers["session_before_compact"]) {
|
|
89
|
+
return handlers["session_before_compact"](
|
|
90
|
+
{ type: "session_before_compact", reason: "threshold", willRetry: false, signal: undefined, preparation: { firstKeptEntryId: "e2", messagesToSummarize: session.slice(0, 2), tokensBefore: 500 } } as any,
|
|
91
|
+
makeCtx(),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
},
|
|
96
|
+
getSystemPrompt: () => "system base",
|
|
97
|
+
...over,
|
|
98
|
+
} as any;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const pi = {
|
|
102
|
+
on: (ev: string, h: Function) => { handlers[ev] = h; },
|
|
103
|
+
registerCommand: () => {}, registerTool: () => {}, registerShortcut: () => {},
|
|
104
|
+
registerFlag: () => {}, getFlag: () => undefined, registerMessageRenderer: () => {},
|
|
105
|
+
registerEntryRenderer: () => {}, sendMessage: () => {}, sendUserMessage: () => {},
|
|
106
|
+
appendEntry: () => {}, setSessionName: () => {}, getSessionName: () => undefined,
|
|
107
|
+
setLabel: () => {}, exec: async () => ({ stdout: "", stderr: "", code: 0 }),
|
|
108
|
+
getActiveTools: () => [], getAllTools: () => [], setActiveTools: () => {},
|
|
109
|
+
getCommands: () => [], setModel: async () => false, getThinkingLevel: () => "off" as any,
|
|
110
|
+
setThinkingLevel: () => {},
|
|
111
|
+
} as any;
|
|
112
|
+
|
|
113
|
+
const mod = require("./mega-compact.js") as { default: (p: any) => void };
|
|
114
|
+
mod.default(pi);
|
|
115
|
+
const { lastRuntime } = require("./mega-events.js") as { lastRuntime: any };
|
|
116
|
+
|
|
117
|
+
const fire = (ev: string, event: any, ctx: any) => handlers[ev](event, ctx);
|
|
118
|
+
return {
|
|
119
|
+
stateDir, handlers, compactCalls, fire, ctx: makeCtx, session,
|
|
120
|
+
runtime: lastRuntime, // MegaRuntime with diag* counters
|
|
121
|
+
// Advance the debounce so agent_end (same instant) can trigger durable trim.
|
|
122
|
+
clearDebounce: () => { if (lastRuntime) lastRuntime.debounceUntil = 0; },
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
test("team run: live trim fires AND durable trim fires per sub-agent (relieves context)", async () => {
|
|
127
|
+
const h = harness();
|
|
128
|
+
const ctx = h.ctx();
|
|
129
|
+
for (let a = 0; a < 3; a++) {
|
|
130
|
+
await h.fire("agent_start", { type: "agent_start", messages: [] }, ctx);
|
|
131
|
+
for (let i = 0; i < 4; i++) {
|
|
132
|
+
await h.fire("context", { type: "context", messages: h.session }, ctx);
|
|
133
|
+
}
|
|
134
|
+
// Real team runs settle seconds after the last context event; mimic that
|
|
135
|
+
// so the 2s debounce has elapsed and the durable trigger can fire.
|
|
136
|
+
await new Promise((r) => setTimeout(r, 2100));
|
|
137
|
+
h.clearDebounce();
|
|
138
|
+
await h.fire("agent_end", { type: "agent_end", messages: [] }, ctx);
|
|
139
|
+
}
|
|
140
|
+
const rt = h.runtime;
|
|
141
|
+
// FIX 1: live trim must actually fire (was 0 — computeLiveTrimCut returned null).
|
|
142
|
+
assert.ok(rt.diagLiveTrimFires > 0, "live trim fires during the team run (anchor-floor fix)");
|
|
143
|
+
assert.equal(rt.diagCtxCutNull, 0, "no live-trim cut skipped on anchor floor");
|
|
144
|
+
// FIX 2: durable trim must fire at each agent_end (was 0 — only at parent settle).
|
|
145
|
+
assert.equal(rt.diagAgentEndDurable, 3, "mid-run durable trigger fired at each agent_end");
|
|
146
|
+
assert.equal(rt.diagBeforeCompactSupplied, 3, "our durable trim supplied 3x (context relieved)");
|
|
147
|
+
assert.ok(h.compactCalls.length >= 3, "ctx.compact() invoked for durable trim between sub-agents");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("control: session_before_compact supplies a durable compaction (parent settles)", async () => {
|
|
151
|
+
const h = harness();
|
|
152
|
+
const res = await h.fire(
|
|
153
|
+
"session_before_compact",
|
|
154
|
+
{ type: "session_before_compact", reason: "threshold", willRetry: false, signal: undefined, preparation: { firstKeptEntryId: "e2", messagesToSummarize: h.session.slice(0, 4), tokensBefore: 500 } } as any,
|
|
155
|
+
h.ctx(),
|
|
156
|
+
);
|
|
157
|
+
assert.ok(res?.compaction, "compaction result returned to pi");
|
|
158
|
+
assert.equal(res.compaction.firstKeptEntryId, "e2", "reuses pi's boundary (PREVENT-PI-002)");
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("cleanup", async () => {
|
|
162
|
+
await closeVectorIndex();
|
|
163
|
+
rmSync(baseTmp, { recursive: true, force: true });
|
|
164
|
+
});
|
package/extensions/mega-trim.ts
CHANGED
|
@@ -46,7 +46,34 @@ export function computeLiveTrimCut(view: EngineMessage[], opts: BuildLiveTrimVie
|
|
|
46
46
|
if (cut <= 0) return null; // nothing safe to cut — keep everything this call
|
|
47
47
|
const recent = view.slice(cut);
|
|
48
48
|
const userCount = recent.filter((m) => m.role === "user").length;
|
|
49
|
-
|
|
49
|
+
// ANCHOR FLOOR (PREVENT-PI-001): the recent window must keep at least
|
|
50
|
+
// `anchorUserMessages` user messages. The original compactedFrom can land on a
|
|
51
|
+
// run that starts with fewer than that (e.g. the preserved region begins on a
|
|
52
|
+
// tool pair, or the session's tail is tool-heavy). Instead of bailing out and
|
|
53
|
+
// skipping the live trim entirely this call (which left the model fed a
|
|
54
|
+
// 150k-context window during long team runs), walk `cut` backward until the
|
|
55
|
+
// preserved run contains enough user messages — bounded by the boundary-safe
|
|
56
|
+
// constraint so we never split a tool pair. Falls back to null only when the
|
|
57
|
+
// whole view can't satisfy the floor (tiny sessions) — the next context event
|
|
58
|
+
// retries.
|
|
59
|
+
if (userCount < opts.anchorUserMessages) {
|
|
60
|
+
let c = cut;
|
|
61
|
+
while (c > 1) {
|
|
62
|
+
c--;
|
|
63
|
+
if (!isBoundarySafe(view, c)) continue;
|
|
64
|
+
const recentNow = view.slice(c);
|
|
65
|
+
const usersNow = recentNow.filter((m) => m.role === "user").length;
|
|
66
|
+
if (usersNow >= opts.anchorUserMessages) { cut = c; break; }
|
|
67
|
+
}
|
|
68
|
+
if (cut > 1) {
|
|
69
|
+
const finalRecent = view.slice(cut);
|
|
70
|
+
if (finalRecent.filter((m) => m.role === "user").length < opts.anchorUserMessages) {
|
|
71
|
+
return null; // cannot satisfy the floor without dropping too much — retry next call
|
|
72
|
+
}
|
|
73
|
+
} else {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
50
77
|
return cut;
|
|
51
78
|
}
|
|
52
79
|
|
package/package.json
CHANGED
package/src/memoryOps.ts
CHANGED
|
@@ -12,6 +12,24 @@ import {
|
|
|
12
12
|
removeMemory,
|
|
13
13
|
type MemoryRecord,
|
|
14
14
|
} from "./store/sqlite.js";
|
|
15
|
+
import { defaultEmbedder } from "./embedder.js";
|
|
16
|
+
import { upsertMemoryEmbedding } from "./store/memoryIndex.js";
|
|
17
|
+
import { execSync } from "node:child_process"; // guardrails-allow PREVENT-PI-004: read-only `git rev-parse` to scope the memory index per-repo
|
|
18
|
+
|
|
19
|
+
/** Resolve the current repo's git root (mirrors extensions/mega-config.ts but
|
|
20
|
+
* kept local so src/ stays pi-agnostic — no extension-layer import). */
|
|
21
|
+
function resolveRepoRootLocal(cwd: string): string | undefined {
|
|
22
|
+
try {
|
|
23
|
+
const out = execSync("git rev-parse --show-toplevel", {
|
|
24
|
+
cwd,
|
|
25
|
+
encoding: "utf-8",
|
|
26
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
27
|
+
}).trim();
|
|
28
|
+
return out || undefined;
|
|
29
|
+
} catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
15
33
|
|
|
16
34
|
/** Find a memory row whose content exactly matches (case-insensitive). */
|
|
17
35
|
function findByContent(memories: MemoryRecord[], content: string): MemoryRecord | undefined {
|
|
@@ -19,6 +37,23 @@ function findByContent(memories: MemoryRecord[], content: string): MemoryRecord
|
|
|
19
37
|
return memories.find((m) => m.content.trim().toLowerCase() === norm);
|
|
20
38
|
}
|
|
21
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Fire-and-forget mirror of a memory write into the cross-repo PGlite index
|
|
42
|
+
* (S24 optional memory-RAG mirror). Best-effort + non-fatal: never blocks the
|
|
43
|
+
* SQLite write and degrades to the same-repo scan if the index is disabled or
|
|
44
|
+
* fails. `repoId` is the resolved git root so the memory is findable from other
|
|
45
|
+
* repos; falls back to the state dir when outside git.
|
|
46
|
+
*/
|
|
47
|
+
function indexMemoryWrite(stateDir: string, memoryId: number, content: string): void {
|
|
48
|
+
const repoId = resolveRepoRootLocal(stateDir) ?? stateDir;
|
|
49
|
+
try {
|
|
50
|
+
const vec = defaultEmbedder().embed(content);
|
|
51
|
+
void upsertMemoryEmbedding(repoId, memoryId, content, vec);
|
|
52
|
+
} catch {
|
|
53
|
+
/* non-fatal — embedding/index failure must never break the SQLite write */
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
22
57
|
/**
|
|
23
58
|
* Apply add/replace/remove ops to the memories table. Replaces are matched by
|
|
24
59
|
* existing content; removes by content. Idempotent: an add that already exists
|
|
@@ -32,7 +67,7 @@ export async function applyMemoryOps(ops: MemoryOp[], stateDir: string): Promise
|
|
|
32
67
|
if (op.op === "add") {
|
|
33
68
|
// Skip if an identical memory already exists.
|
|
34
69
|
if (findByContent(existing, op.memory.content)) continue;
|
|
35
|
-
addMemory(
|
|
70
|
+
const id = addMemory(
|
|
36
71
|
{
|
|
37
72
|
kind: op.memory.category,
|
|
38
73
|
content: op.memory.content,
|
|
@@ -44,6 +79,8 @@ export async function applyMemoryOps(ops: MemoryOp[], stateDir: string): Promise
|
|
|
44
79
|
repo,
|
|
45
80
|
stateDir,
|
|
46
81
|
);
|
|
82
|
+
// S24: mirror into the cross-repo index (fire-and-forget; non-fatal).
|
|
83
|
+
indexMemoryWrite(stateDir, id, op.memory.content);
|
|
47
84
|
} else if (op.op === "replace") {
|
|
48
85
|
const match = findByContent(existing, op.targetContent);
|
|
49
86
|
if (match) {
|
|
@@ -53,9 +90,11 @@ export async function applyMemoryOps(ops: MemoryOp[], stateDir: string): Promise
|
|
|
53
90
|
category: op.memory.category,
|
|
54
91
|
sourceTurn: op.memory.sourceTurn,
|
|
55
92
|
}, stateDir);
|
|
93
|
+
// S24: re-mirror under the same memory id (fire-and-forget; non-fatal).
|
|
94
|
+
indexMemoryWrite(stateDir, match.id, op.memory.content);
|
|
56
95
|
} else {
|
|
57
96
|
// Target missing (e.g. earlier in-conversation contradiction) → add.
|
|
58
|
-
addMemory(
|
|
97
|
+
const id = addMemory(
|
|
59
98
|
{
|
|
60
99
|
kind: op.memory.category,
|
|
61
100
|
content: op.memory.content,
|
|
@@ -66,6 +105,7 @@ export async function applyMemoryOps(ops: MemoryOp[], stateDir: string): Promise
|
|
|
66
105
|
repo,
|
|
67
106
|
stateDir,
|
|
68
107
|
);
|
|
108
|
+
indexMemoryWrite(stateDir, id, op.memory.content);
|
|
69
109
|
}
|
|
70
110
|
} else {
|
|
71
111
|
const match = findByContent(existing, op.content);
|
package/src/memoryRecall.test.ts
CHANGED
|
@@ -98,3 +98,61 @@ test("recallMemories: fresher reference beats older at equal similarity", async
|
|
|
98
98
|
test("cleanup memrec", () => {
|
|
99
99
|
rmSync(baseTmp, { recursive: true, force: true });
|
|
100
100
|
});
|
|
101
|
+
|
|
102
|
+
// ---- S24: cross-repo memory recall (PGlite mirror) ---------------------------
|
|
103
|
+
test("recallMemoriesAndInline: surfaces a memory saved in ANOTHER repo via cross-repo index", async () => {
|
|
104
|
+
// Isolate the global PGlite index to a temp dir shared by both "repos".
|
|
105
|
+
process.env.MEGACOMPACT_INDEX_DIR = join(baseTmp, "xrepo-index");
|
|
106
|
+
const repoA = join(baseTmp, "repo-a");
|
|
107
|
+
const repoB = join(baseTmp, "repo-b");
|
|
108
|
+
try {
|
|
109
|
+
// repoA owns a decision about the store backend.
|
|
110
|
+
const { applyMemoryOps } = await import("./memoryOps.js");
|
|
111
|
+
await applyMemoryOps(
|
|
112
|
+
[{ op: "add", memory: { content: "we standardized on node:sqlite for the store backend", category: "decision", sourceTurn: 0 } }],
|
|
113
|
+
repoA,
|
|
114
|
+
);
|
|
115
|
+
// repoB is a fresh session with NO local memory about the store backend.
|
|
116
|
+
const { recallMemoriesAndInline } = await import("./recall.js");
|
|
117
|
+
const res = await recallMemoriesAndInline({
|
|
118
|
+
query: "what store backend do we use?",
|
|
119
|
+
stateDir: repoB,
|
|
120
|
+
limit: 5,
|
|
121
|
+
crossRepo: true,
|
|
122
|
+
crossRepoCosine: 0.3,
|
|
123
|
+
});
|
|
124
|
+
assert.ok(!res.empty, "cross-repo recall found the other repo's memory");
|
|
125
|
+
assert.ok(/node:sqlite/.test(res.block), "the node:sqlite decision was recalled from repo A");
|
|
126
|
+
assert.ok(res.report.some((r) => /from /.test(r)), "report labels the memory as cross-repo");
|
|
127
|
+
} finally {
|
|
128
|
+
const { closeMemoryIndex } = await import("./store/memoryIndex.js");
|
|
129
|
+
await closeMemoryIndex();
|
|
130
|
+
delete process.env.MEGACOMPACT_INDEX_DIR;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("recallMemoriesAndInline: cross-repo disabled when MEGACOMPACT_PGLITE_DISABLED", async () => {
|
|
135
|
+
process.env.MEGACOMPACT_INDEX_DIR = join(baseTmp, "xrepo-index-off");
|
|
136
|
+
process.env.MEGACOMPACT_PGLITE_DISABLED = "true";
|
|
137
|
+
const repoA = join(baseTmp, "repo-a2");
|
|
138
|
+
const repoB = join(baseTmp, "repo-b2");
|
|
139
|
+
try {
|
|
140
|
+
const { applyMemoryOps } = await import("./memoryOps.js");
|
|
141
|
+
await applyMemoryOps(
|
|
142
|
+
[{ op: "add", memory: { content: "we standardized on node:sqlite for the store backend", category: "decision", sourceTurn: 0 } }],
|
|
143
|
+
repoA,
|
|
144
|
+
);
|
|
145
|
+
const { recallMemoriesAndInline } = await import("./recall.js");
|
|
146
|
+
const res = await recallMemoriesAndInline({
|
|
147
|
+
query: "what store backend do we use?",
|
|
148
|
+
stateDir: repoB,
|
|
149
|
+
limit: 5,
|
|
150
|
+
crossRepo: true,
|
|
151
|
+
});
|
|
152
|
+
// Index disabled → no cross-repo hit; repoB has no local memory → empty.
|
|
153
|
+
assert.equal(res.empty, true, "cross-repo recall degrades to empty when disabled");
|
|
154
|
+
} finally {
|
|
155
|
+
delete process.env.MEGACOMPACT_PGLITE_DISABLED;
|
|
156
|
+
delete process.env.MEGACOMPACT_INDEX_DIR;
|
|
157
|
+
}
|
|
158
|
+
});
|
package/src/memoryRecall.ts
CHANGED
|
@@ -81,3 +81,55 @@ export async function recallMemories(
|
|
|
81
81
|
}
|
|
82
82
|
return top;
|
|
83
83
|
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Cross-repo memory recall (S24): augments the same-repo `recallMemories` with
|
|
87
|
+
* HNSW NN over the global PGlite `memory_index` (other repos' memories). Content
|
|
88
|
+
* is read inline from the index hit (the recall process can't open other repos'
|
|
89
|
+
* SQLite dirs), so no other-repo db access is required. Returns hits sorted by
|
|
90
|
+
* descending cosine, above `crossRepoCosine`. De-duped by content against
|
|
91
|
+
* `sameRepoContent` so we never surface a memory the same-repo scan already has.
|
|
92
|
+
* Non-fatal: any index failure returns []. Best-effort + PREVENT-PI-004 (local
|
|
93
|
+
* WASM only).
|
|
94
|
+
*/
|
|
95
|
+
export async function recallMemoriesCrossRepo(
|
|
96
|
+
query: string,
|
|
97
|
+
stateDir: string,
|
|
98
|
+
opts: RecallMemoriesOptions & { crossRepoCosine?: number; limit?: number } = {},
|
|
99
|
+
): Promise<Array<{ memory: MemoryRecord; score: number; repoId: string }>> {
|
|
100
|
+
const embedder = opts.embedder ?? defaultEmbedder();
|
|
101
|
+
const queryVec = embedder.embed(query);
|
|
102
|
+
const { searchMemoriesAsync } = await import("./store/memoryIndex.js");
|
|
103
|
+
const k = opts.limit ?? 5;
|
|
104
|
+
const floor = opts.crossRepoCosine ?? 0.3;
|
|
105
|
+
const hits = await searchMemoriesAsync(queryVec, { k });
|
|
106
|
+
if (!hits.length) return [];
|
|
107
|
+
// Mark same-repo content as already-covered so we don't duplicate it.
|
|
108
|
+
const sameRepo = new Set(
|
|
109
|
+
listMemories(opts.repo ?? null, 1000, stateDir).map((m) => m.content.trim().toLowerCase()),
|
|
110
|
+
);
|
|
111
|
+
const out: Array<{ memory: MemoryRecord; score: number; repoId: string }> = [];
|
|
112
|
+
for (const h of hits) {
|
|
113
|
+
if (h.score < floor) continue;
|
|
114
|
+
if (sameRepo.has(h.content.trim().toLowerCase())) continue;
|
|
115
|
+
out.push({
|
|
116
|
+
memory: {
|
|
117
|
+
id: h.memoryId,
|
|
118
|
+
repo: h.repoId,
|
|
119
|
+
kind: "note",
|
|
120
|
+
content: h.content,
|
|
121
|
+
tags: [],
|
|
122
|
+
createdAt: 0,
|
|
123
|
+
lastRecalledAt: null,
|
|
124
|
+
category: null,
|
|
125
|
+
target: null,
|
|
126
|
+
lastReferenced: null,
|
|
127
|
+
sourceTurn: null,
|
|
128
|
+
} as MemoryRecord,
|
|
129
|
+
score: h.score,
|
|
130
|
+
repoId: h.repoId,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
out.sort((a, b) => b.score - a.score);
|
|
134
|
+
return out;
|
|
135
|
+
}
|