pi-mega-compact 0.8.6 → 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,189 @@
1
+ /**
2
+ * mega-cache-replay.test.ts — locks the v0.8.7 cache-stability fix.
3
+ *
4
+ * Two tests (reuses the mega-teamrun.test.ts harness shape: mock pi + the REAL
5
+ * compiled extension at extensions/mega-compact.js):
6
+ * a. REPLAY: drive >=2 gated context events past the debounce within ONE epoch
7
+ * (same lastCheckpointId) and assert diagLiveTrimReplays > 0 AND the returned
8
+ * messages array is byte-identical (deepEqual) across replays (stable prefix).
9
+ * b. DEDUP-ON-DIFFERENT-CHECKPOINT: after a fresh trim, simulate a re-compact
10
+ * (context grew on the token basis) that DEDUPS onto a DIFFERENT existing
11
+ * checkpoint id (L0 contentHash match against an OLDER checkpoint, so
12
+ * result.checkpointId != rt.lastCheckpointId), then assert the NEXT gated
13
+ * event STILL replays (diagLiveTrimReplays increments) — i.e. the cache key
14
+ * trimCache.checkpointId === rt.lastCheckpointId holds. This is the P2 gap the
15
+ * v0.8.6 audit found: keying on the dedup-volatile result.checkpointId
16
+ * disabled replay for the rest of the epoch after such a dedup fire.
17
+ *
18
+ * MEGACOMPACT_PGLITE_DISABLED keeps the run fast (no WASM index init).
19
+ */
20
+ import { test } from "node:test";
21
+ import assert from "node:assert/strict";
22
+ import { mkdtempSync, rmSync } from "node:fs";
23
+ import { tmpdir } from "node:os";
24
+ import { join } from "node:path";
25
+ import { createRequire } from "node:module";
26
+ import { closeVectorIndex } from "../src/store/vectorIndex.js";
27
+ const require = createRequire(import.meta.url);
28
+ const baseTmp = mkdtempSync(join(tmpdir(), "mc-cache-"));
29
+ process.env.MEGACOMPACT_INDEX_DIR = join(baseTmp, "index");
30
+ process.env.MEGACOMPACT_PGLITE_DISABLED = "true"; // fast: skip WASM index
31
+ let counter = 0;
32
+ function harness() {
33
+ const stateDir = join(baseTmp, `run-${counter++}`);
34
+ process.env.MEGACOMPACT_STATE_DIR = stateDir;
35
+ process.env.MEGACOMPACT_DEBUG = "true";
36
+ process.env.MEGACOMPACT_THRESHOLD_TOKENS = "50";
37
+ process.env.MEGACOMPACT_FAST_GATE_PCT = "1";
38
+ process.env.MEGACOMPACT_ANCHOR_USER_MESSAGES = "1";
39
+ process.env.MEGACOMPACT_DURABLE_TRIM_FLOOR = "0";
40
+ process.env.MEGACOMPACT_MEMORY_AUTO_REVIEW = "false";
41
+ process.env.MEGACOMPACT_RAPTOR_ENABLED = "false";
42
+ // Disable the FUZZY dedup tiers (L1 MinHash/LSH + L2 cosine) so the DEDUP test
43
+ // is controlled by L0 contentHash only: setB (different vocabulary) then
44
+ // creates a genuinely NEW checkpoint instead of fuzzy-matching setA's, and
45
+ // setA-re-again still L0-contentHash-dedups onto the first setA checkpoint.
46
+ // The TrigramEmbedder otherwise matches on shared structural trigrams
47
+ // ("— step N" / "Edit"), collapsing setB onto setA. Harmless for the REPLAY
48
+ // test (pure replay, no dedup reliance).
49
+ process.env.MEGACOMPACT_L1_ENABLED = "false";
50
+ process.env.MEGACOMPACT_L2_ENABLED = "false";
51
+ delete process.env.MEGACOMPACT_LEGACY_DURABLE_TRIM;
52
+ // Mutable context-usage so tests can drive re-compact on the TOKEN basis by
53
+ // raising `tokens` while keeping `percent` null (→ token gate + token
54
+ // grewEnough path in context-handler.ts). The REPLAY test keeps percent=100 so
55
+ // the percent-basis grewEnough (>=10) never trips (no re-compact → pure replay).
56
+ const usage = { tokens: 200000, contextWindow: 200000, percent: 100 };
57
+ const handlers = {};
58
+ const compactCalls = [];
59
+ function msg(role, text, toolName) {
60
+ if (role === "assistant" && toolName) {
61
+ 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 };
62
+ }
63
+ if (role === "toolResult" && toolName) {
64
+ return { role: "toolResult", content: [{ type: "text", text }], toolCallId: "c1", toolName, isError: false, timestamp: 0 };
65
+ }
66
+ return { role: "user", content: text, timestamp: 0 };
67
+ }
68
+ // Build a session of `n` tool-call triples tagged `tag`. Set A and B differ in
69
+ // content (so B never dedups against A) but A === A reproduces the same
70
+ // regionText → same L0 contentHash → dedup onto the first A checkpoint.
71
+ function buildSession(tag, n) {
72
+ const s = [];
73
+ for (let i = 0; i < n; i++) {
74
+ s.push(msg("user", `[${tag}] we decided to use approach ${i} for module ${i}`));
75
+ s.push(msg("assistant", `[${tag}] edited module ${i}`, "Edit"));
76
+ s.push(msg("toolResult", `[${tag}] edited module ${i}`, "Edit"));
77
+ }
78
+ return s;
79
+ }
80
+ const toEntry = (m, i) => ({ type: "message", id: `e${i}`, parentId: null, timestamp: String(i), message: m });
81
+ const sessionManager = {
82
+ getSessionId: () => "sess_cache_001",
83
+ getEntries: () => buildSession("A", 14).map(toEntry),
84
+ getBranch: () => buildSession("A", 14).map(toEntry),
85
+ };
86
+ function makeCtx(over = {}) {
87
+ return {
88
+ ui: { setStatus: () => { }, notify: () => { }, select: () => { }, confirm: async () => true, input: async () => "", setWidget: () => { } },
89
+ mode: "tui", hasUI: true, cwd: stateDir, sessionManager,
90
+ modelRegistry: {}, model: undefined, isIdle: () => true, isProjectTrusted: () => true,
91
+ signal: undefined, abort: () => { }, hasPendingMessages: () => false, shutdown: () => { },
92
+ getContextUsage: () => ({ ...usage }),
93
+ compact: (opts) => { compactCalls.push(opts); return undefined; },
94
+ getSystemPrompt: () => "system base",
95
+ ...over,
96
+ };
97
+ }
98
+ const pi = {
99
+ on: (ev, h) => { handlers[ev] = h; },
100
+ registerCommand: () => { }, registerTool: () => { }, registerShortcut: () => { },
101
+ registerFlag: () => { }, getFlag: () => undefined, registerMessageRenderer: () => { },
102
+ registerEntryRenderer: () => { }, sendMessage: () => { }, sendUserMessage: () => { },
103
+ appendEntry: () => { }, setSessionName: () => { }, getSessionName: () => undefined,
104
+ setLabel: () => { }, exec: async () => ({ stdout: "", stderr: "", code: 0 }),
105
+ getActiveTools: () => [], getAllTools: () => [], setActiveTools: () => { },
106
+ getCommands: () => [], setModel: async () => false, getThinkingLevel: () => "off",
107
+ setThinkingLevel: () => { },
108
+ };
109
+ const mod = require("./mega-compact.js");
110
+ mod.default(pi);
111
+ const { lastRuntime } = require("./mega-events.js");
112
+ const fire = (ev, event, ctx) => handlers[ev](event, ctx);
113
+ return {
114
+ stateDir, handlers, compactCalls, fire, ctx: makeCtx, usage, buildSession,
115
+ runtime: lastRuntime, // MegaRuntime with diag* counters + rt + trimCache
116
+ // Bypass the 2s debounce so each fire proceeds without real waiting.
117
+ clearDebounce: () => { if (lastRuntime)
118
+ lastRuntime.debounceUntil = 0; },
119
+ };
120
+ }
121
+ test("REPLAY: >=2 gated context events within one epoch replay verbatim (byte-identical)", async () => {
122
+ const h = harness();
123
+ const ctx = h.ctx();
124
+ const session = h.buildSession("A", 14);
125
+ // Fire 3 gated context events; clearDebounce between so each passes the gate.
126
+ // percent stays 100 → percent-basis grewEnough (>=10) never trips → pure replay.
127
+ h.clearDebounce();
128
+ const r1 = await h.fire("context", { type: "context", messages: session }, ctx);
129
+ h.clearDebounce();
130
+ const r2 = await h.fire("context", { type: "context", messages: session }, ctx);
131
+ h.clearDebounce();
132
+ const r3 = await h.fire("context", { type: "context", messages: session }, ctx);
133
+ const rt = h.runtime;
134
+ assert.ok(rt.diagLiveTrimFires >= 1, "fresh trim fired on first event");
135
+ assert.ok(rt.diagLiveTrimReplays >= 2, `replay fired >=2 (got ${rt.diagLiveTrimReplays})`);
136
+ // byte-identical (stable KV-cache prefix) across replays
137
+ assert.deepEqual(r2?.messages, r3?.messages, "replay messages byte-identical across replays");
138
+ // replay matches the fresh-trim view (shallow-copy preserves content)
139
+ assert.deepEqual(r1?.messages, r2?.messages, "replay matches fresh-trim view (stable prefix)");
140
+ });
141
+ test("DEDUP: re-compact that dedups onto a DIFFERENT checkpoint still replays next (P2 fix)", async () => {
142
+ const h = harness();
143
+ // Token-basis growth path: percent null → token gate + token grewEnough
144
+ // (currentTokens - trimCache.ctxTokens >= effectiveThreshold * 0.5 = 25).
145
+ h.usage.percent = null;
146
+ h.usage.tokens = 200000;
147
+ const ctx = h.ctx();
148
+ const setA = h.buildSession("A", 14);
149
+ const setB = h.buildSession("B", 14); // different content, same length
150
+ const rt = h.runtime;
151
+ // 1) Fresh trim on setA → genuinely new checkpoint cp_A. lastCheckpointId = cp_A.
152
+ h.clearDebounce();
153
+ await h.fire("context", { type: "context", messages: setA }, ctx);
154
+ const cpA = rt.rt.lastCheckpointId;
155
+ assert.ok(cpA, "cp_A created on fresh trim");
156
+ assert.equal(rt.diagLiveTrimFires, 1, "first fire was a fresh trim");
157
+ // 2) Re-compact on setB (grew tokens) → genuinely new checkpoint cp_B (not deduped).
158
+ h.usage.tokens = 200100; // grew 100 >= 25
159
+ h.clearDebounce();
160
+ await h.fire("context", { type: "context", messages: setB }, ctx);
161
+ const cpB = rt.rt.lastCheckpointId;
162
+ assert.notEqual(cpB, cpA, "cp_B is a different, genuinely new checkpoint");
163
+ assert.equal(rt.rt.dedupSkips, 0, "setB did not dedup (different vocabulary, fuzzy tiers off)");
164
+ // 3) Re-compact on setA AGAIN (grew tokens) → L0 contentHash dedup onto cp_A.
165
+ // result.checkpointId = cp_A (!= lastCheckpointId cp_B); lastCheckpointId is
166
+ // NOT updated on a dedup (compact.ts:100-104), so it stays cp_B. With the
167
+ // fix, trimCache.checkpointId is keyed on lastCheckpointId (cp_B), NOT the
168
+ // dedup-volatile result.checkpointId (cp_A).
169
+ h.usage.tokens = 200200; // grew 100 >= 25
170
+ h.clearDebounce();
171
+ await h.fire("context", { type: "context", messages: setA }, ctx);
172
+ assert.equal(rt.rt.lastCheckpointId, cpB, "dedup did NOT bump lastCheckpointId (still cp_B)");
173
+ assert.ok(rt.rt.dedupSkips >= 1, "setA re-compact deduped onto an existing checkpoint");
174
+ // The P2 invariant: the cache key must equal the stable epoch signal.
175
+ assert.equal(rt.trimCache?.checkpointId, rt.rt.lastCheckpointId, "trimCache.checkpointId keyed on lastCheckpointId (P2 fix), not dedup-volatile result.checkpointId");
176
+ // 4) Next gated event (no growth) MUST replay instead of re-running runCompact.
177
+ // Without the fix, trimCache.checkpointId (cp_A) != lastCheckpointId (cp_B)
178
+ // → the replay condition is false → runCompact re-runs every fire → the
179
+ // thrash silently persists in that path (the audit's finding).
180
+ const replaysBefore = rt.diagLiveTrimReplays;
181
+ h.usage.tokens = 200200; // no growth → replay
182
+ h.clearDebounce();
183
+ await h.fire("context", { type: "context", messages: setA }, ctx);
184
+ assert.ok(rt.diagLiveTrimReplays > replaysBefore, `replay fired after dedup-onto-different-checkpoint (got ${rt.diagLiveTrimReplays}, was ${replaysBefore})`);
185
+ });
186
+ test("cleanup", async () => {
187
+ await closeVectorIndex();
188
+ rmSync(baseTmp, { recursive: true, force: true });
189
+ });
@@ -146,7 +146,9 @@ export function registerContextHandler(pi, runtime, config) {
146
146
  runtime.diagLiveTrimFires++; // trim view returned this call (replay counts as a fire)
147
147
  runtime.diagLiveTrimReplays++;
148
148
  runtime.snapshot(ctx);
149
- return { messages: [runtime.trimCache.summaryAgentMsg, ...recent] };
149
+ // v0.8.7: shallow-copy the cached summary so pi's transformContext can't
150
+ // mutate the shared reference across replays (audit P3).
151
+ return { messages: [{ ...runtime.trimCache.summaryAgentMsg }, ...recent] };
150
152
  }
151
153
  // else: context grew enough → fall through to re-compact (cache is stale)
152
154
  }
@@ -262,7 +264,20 @@ export function registerContextHandler(pi, runtime, config) {
262
264
  // replay it verbatim (stabilizing the KV-cache prefix) instead of
263
265
  // regenerating a fresh summary + sentinel every fire.
264
266
  runtime.trimCache = {
265
- checkpointId: ran.result.checkpointId ?? `epoch-${runtime.rt.lastCompactAt ?? Date.now()}`,
267
+ // v0.8.7: key the replay cache on the STABLE epoch signal
268
+ // (rt.lastCheckpointId) instead of ran.result.checkpointId, which is
269
+ // dedup-volatile: on a re-compact that dedups onto a DIFFERENT existing
270
+ // checkpoint, result.checkpointId is the matched id (engine.ts:188) while
271
+ // lastCheckpointId is only updated on a genuinely new checkpoint
272
+ // (compact.ts:100-104). Keying on result.checkpointId would make
273
+ // trimCache.checkpointId != rt.lastCheckpointId forever after that
274
+ // dedup fire, disabling replay for the rest of the epoch (the
275
+ // alternating cache-miss that 0.8.6 meant to fix). Prefer the stable
276
+ // signal; fall back to result.checkpointId then the epoch timestamp
277
+ // only for the no-checkpoint edge case.
278
+ checkpointId: runtime.rt.lastCheckpointId ??
279
+ ran.result.checkpointId ??
280
+ `epoch-${runtime.rt.lastCompactAt ?? Date.now()}`,
266
281
  cut,
267
282
  summaryAgentMsg,
268
283
  ctxPct: pct ?? null,
@@ -0,0 +1,211 @@
1
+ /**
2
+ * mega-cache-replay.test.ts — locks the v0.8.7 cache-stability fix.
3
+ *
4
+ * Two tests (reuses the mega-teamrun.test.ts harness shape: mock pi + the REAL
5
+ * compiled extension at extensions/mega-compact.js):
6
+ * a. REPLAY: drive >=2 gated context events past the debounce within ONE epoch
7
+ * (same lastCheckpointId) and assert diagLiveTrimReplays > 0 AND the returned
8
+ * messages array is byte-identical (deepEqual) across replays (stable prefix).
9
+ * b. DEDUP-ON-DIFFERENT-CHECKPOINT: after a fresh trim, simulate a re-compact
10
+ * (context grew on the token basis) that DEDUPS onto a DIFFERENT existing
11
+ * checkpoint id (L0 contentHash match against an OLDER checkpoint, so
12
+ * result.checkpointId != rt.lastCheckpointId), then assert the NEXT gated
13
+ * event STILL replays (diagLiveTrimReplays increments) — i.e. the cache key
14
+ * trimCache.checkpointId === rt.lastCheckpointId holds. This is the P2 gap the
15
+ * v0.8.6 audit found: keying on the dedup-volatile result.checkpointId
16
+ * disabled replay for the rest of the epoch after such a dedup fire.
17
+ *
18
+ * MEGACOMPACT_PGLITE_DISABLED keeps the run fast (no WASM index init).
19
+ */
20
+ import { test } from "node:test";
21
+ import assert from "node:assert/strict";
22
+ import { mkdtempSync, rmSync } from "node:fs";
23
+ import { tmpdir } from "node:os";
24
+ import { join } from "node:path";
25
+ import { createRequire } from "node:module";
26
+ import { closeVectorIndex } from "../src/store/vectorIndex.js";
27
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
28
+
29
+ const require = createRequire(import.meta.url);
30
+ const baseTmp = mkdtempSync(join(tmpdir(), "mc-cache-"));
31
+ process.env.MEGACOMPACT_INDEX_DIR = join(baseTmp, "index");
32
+ process.env.MEGACOMPACT_PGLITE_DISABLED = "true"; // fast: skip WASM index
33
+ let counter = 0;
34
+
35
+ function harness() {
36
+ const stateDir = join(baseTmp, `run-${counter++}`);
37
+ process.env.MEGACOMPACT_STATE_DIR = stateDir;
38
+ process.env.MEGACOMPACT_DEBUG = "true";
39
+ process.env.MEGACOMPACT_THRESHOLD_TOKENS = "50";
40
+ process.env.MEGACOMPACT_FAST_GATE_PCT = "1";
41
+ process.env.MEGACOMPACT_ANCHOR_USER_MESSAGES = "1";
42
+ process.env.MEGACOMPACT_DURABLE_TRIM_FLOOR = "0";
43
+ process.env.MEGACOMPACT_MEMORY_AUTO_REVIEW = "false";
44
+ process.env.MEGACOMPACT_RAPTOR_ENABLED = "false";
45
+ // Disable the FUZZY dedup tiers (L1 MinHash/LSH + L2 cosine) so the DEDUP test
46
+ // is controlled by L0 contentHash only: setB (different vocabulary) then
47
+ // creates a genuinely NEW checkpoint instead of fuzzy-matching setA's, and
48
+ // setA-re-again still L0-contentHash-dedups onto the first setA checkpoint.
49
+ // The TrigramEmbedder otherwise matches on shared structural trigrams
50
+ // ("— step N" / "Edit"), collapsing setB onto setA. Harmless for the REPLAY
51
+ // test (pure replay, no dedup reliance).
52
+ process.env.MEGACOMPACT_L1_ENABLED = "false";
53
+ process.env.MEGACOMPACT_L2_ENABLED = "false";
54
+ delete process.env.MEGACOMPACT_LEGACY_DURABLE_TRIM;
55
+
56
+ // Mutable context-usage so tests can drive re-compact on the TOKEN basis by
57
+ // raising `tokens` while keeping `percent` null (→ token gate + token
58
+ // grewEnough path in context-handler.ts). The REPLAY test keeps percent=100 so
59
+ // the percent-basis grewEnough (>=10) never trips (no re-compact → pure replay).
60
+ const usage = { tokens: 200000, contextWindow: 200000, percent: 100 as number | null };
61
+
62
+ const handlers: Record<string, Function> = {};
63
+ const compactCalls: any[] = [];
64
+
65
+ function msg(role: string, text: string, toolName?: string): AgentMessage {
66
+ if (role === "assistant" && toolName) {
67
+ 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;
68
+ }
69
+ if (role === "toolResult" && toolName) {
70
+ return { role: "toolResult", content: [{ type: "text", text }], toolCallId: "c1", toolName, isError: false, timestamp: 0 } as unknown as AgentMessage;
71
+ }
72
+ return { role: "user", content: text, timestamp: 0 } as unknown as AgentMessage;
73
+ }
74
+
75
+ // Build a session of `n` tool-call triples tagged `tag`. Set A and B differ in
76
+ // content (so B never dedups against A) but A === A reproduces the same
77
+ // regionText → same L0 contentHash → dedup onto the first A checkpoint.
78
+ function buildSession(tag: string, n: number): AgentMessage[] {
79
+ const s: AgentMessage[] = [];
80
+ for (let i = 0; i < n; i++) {
81
+ s.push(msg("user", `[${tag}] we decided to use approach ${i} for module ${i}`));
82
+ s.push(msg("assistant", `[${tag}] edited module ${i}`, "Edit"));
83
+ s.push(msg("toolResult", `[${tag}] edited module ${i}`, "Edit"));
84
+ }
85
+ return s;
86
+ }
87
+
88
+ const toEntry = (m: AgentMessage, i: number): any => ({ type: "message", id: `e${i}`, parentId: null, timestamp: String(i), message: m });
89
+ const sessionManager = {
90
+ getSessionId: () => "sess_cache_001",
91
+ getEntries: () => buildSession("A", 14).map(toEntry),
92
+ getBranch: () => buildSession("A", 14).map(toEntry),
93
+ };
94
+
95
+ function makeCtx(over: Partial<any> = {}) {
96
+ return {
97
+ ui: { setStatus: () => {}, notify: () => {}, select: () => {}, confirm: async () => true, input: async () => "", setWidget: () => {} },
98
+ mode: "tui" as any, hasUI: true, cwd: stateDir, sessionManager,
99
+ modelRegistry: {} as any, model: undefined, isIdle: () => true, isProjectTrusted: () => true,
100
+ signal: undefined, abort: () => {}, hasPendingMessages: () => false, shutdown: () => {},
101
+ getContextUsage: () => ({ ...usage }),
102
+ compact: (opts?: any) => { compactCalls.push(opts); return undefined; },
103
+ getSystemPrompt: () => "system base",
104
+ ...over,
105
+ } as any;
106
+ }
107
+
108
+ const pi = {
109
+ on: (ev: string, h: Function) => { handlers[ev] = h; },
110
+ registerCommand: () => {}, registerTool: () => {}, registerShortcut: () => {},
111
+ registerFlag: () => {}, getFlag: () => undefined, registerMessageRenderer: () => {},
112
+ registerEntryRenderer: () => {}, sendMessage: () => {}, sendUserMessage: () => {},
113
+ appendEntry: () => {}, setSessionName: () => {}, getSessionName: () => undefined,
114
+ setLabel: () => {}, exec: async () => ({ stdout: "", stderr: "", code: 0 }),
115
+ getActiveTools: () => [], getAllTools: () => [], setActiveTools: () => {},
116
+ getCommands: () => [], setModel: async () => false, getThinkingLevel: () => "off" as any,
117
+ setThinkingLevel: () => {},
118
+ } as any;
119
+
120
+ const mod = require("./mega-compact.js") as { default: (p: any) => void };
121
+ mod.default(pi);
122
+ const { lastRuntime } = require("./mega-events.js") as { lastRuntime: any };
123
+
124
+ const fire = (ev: string, event: any, ctx: any) => handlers[ev](event, ctx);
125
+ return {
126
+ stateDir, handlers, compactCalls, fire, ctx: makeCtx, usage, buildSession,
127
+ runtime: lastRuntime, // MegaRuntime with diag* counters + rt + trimCache
128
+ // Bypass the 2s debounce so each fire proceeds without real waiting.
129
+ clearDebounce: () => { if (lastRuntime) lastRuntime.debounceUntil = 0; },
130
+ };
131
+ }
132
+
133
+ test("REPLAY: >=2 gated context events within one epoch replay verbatim (byte-identical)", async () => {
134
+ const h = harness();
135
+ const ctx = h.ctx();
136
+ const session = h.buildSession("A", 14);
137
+
138
+ // Fire 3 gated context events; clearDebounce between so each passes the gate.
139
+ // percent stays 100 → percent-basis grewEnough (>=10) never trips → pure replay.
140
+ h.clearDebounce();
141
+ const r1 = await h.fire("context", { type: "context", messages: session }, ctx);
142
+ h.clearDebounce();
143
+ const r2 = await h.fire("context", { type: "context", messages: session }, ctx);
144
+ h.clearDebounce();
145
+ const r3 = await h.fire("context", { type: "context", messages: session }, ctx);
146
+
147
+ const rt = h.runtime;
148
+ assert.ok(rt.diagLiveTrimFires >= 1, "fresh trim fired on first event");
149
+ assert.ok(rt.diagLiveTrimReplays >= 2, `replay fired >=2 (got ${rt.diagLiveTrimReplays})`);
150
+ // byte-identical (stable KV-cache prefix) across replays
151
+ assert.deepEqual(r2?.messages, r3?.messages, "replay messages byte-identical across replays");
152
+ // replay matches the fresh-trim view (shallow-copy preserves content)
153
+ assert.deepEqual(r1?.messages, r2?.messages, "replay matches fresh-trim view (stable prefix)");
154
+ });
155
+
156
+ test("DEDUP: re-compact that dedups onto a DIFFERENT checkpoint still replays next (P2 fix)", async () => {
157
+ const h = harness();
158
+ // Token-basis growth path: percent null → token gate + token grewEnough
159
+ // (currentTokens - trimCache.ctxTokens >= effectiveThreshold * 0.5 = 25).
160
+ h.usage.percent = null;
161
+ h.usage.tokens = 200000;
162
+ const ctx = h.ctx();
163
+ const setA = h.buildSession("A", 14);
164
+ const setB = h.buildSession("B", 14); // different content, same length
165
+ const rt = h.runtime;
166
+
167
+ // 1) Fresh trim on setA → genuinely new checkpoint cp_A. lastCheckpointId = cp_A.
168
+ h.clearDebounce();
169
+ await h.fire("context", { type: "context", messages: setA }, ctx);
170
+ const cpA = rt.rt.lastCheckpointId;
171
+ assert.ok(cpA, "cp_A created on fresh trim");
172
+ assert.equal(rt.diagLiveTrimFires, 1, "first fire was a fresh trim");
173
+
174
+ // 2) Re-compact on setB (grew tokens) → genuinely new checkpoint cp_B (not deduped).
175
+ h.usage.tokens = 200100; // grew 100 >= 25
176
+ h.clearDebounce();
177
+ await h.fire("context", { type: "context", messages: setB }, ctx);
178
+ const cpB = rt.rt.lastCheckpointId;
179
+ assert.notEqual(cpB, cpA, "cp_B is a different, genuinely new checkpoint");
180
+ assert.equal(rt.rt.dedupSkips, 0, "setB did not dedup (different vocabulary, fuzzy tiers off)");
181
+
182
+ // 3) Re-compact on setA AGAIN (grew tokens) → L0 contentHash dedup onto cp_A.
183
+ // result.checkpointId = cp_A (!= lastCheckpointId cp_B); lastCheckpointId is
184
+ // NOT updated on a dedup (compact.ts:100-104), so it stays cp_B. With the
185
+ // fix, trimCache.checkpointId is keyed on lastCheckpointId (cp_B), NOT the
186
+ // dedup-volatile result.checkpointId (cp_A).
187
+ h.usage.tokens = 200200; // grew 100 >= 25
188
+ h.clearDebounce();
189
+ await h.fire("context", { type: "context", messages: setA }, ctx);
190
+ assert.equal(rt.rt.lastCheckpointId, cpB, "dedup did NOT bump lastCheckpointId (still cp_B)");
191
+ assert.ok(rt.rt.dedupSkips >= 1, "setA re-compact deduped onto an existing checkpoint");
192
+ // The P2 invariant: the cache key must equal the stable epoch signal.
193
+ assert.equal(rt.trimCache?.checkpointId, rt.rt.lastCheckpointId,
194
+ "trimCache.checkpointId keyed on lastCheckpointId (P2 fix), not dedup-volatile result.checkpointId");
195
+
196
+ // 4) Next gated event (no growth) MUST replay instead of re-running runCompact.
197
+ // Without the fix, trimCache.checkpointId (cp_A) != lastCheckpointId (cp_B)
198
+ // → the replay condition is false → runCompact re-runs every fire → the
199
+ // thrash silently persists in that path (the audit's finding).
200
+ const replaysBefore = rt.diagLiveTrimReplays;
201
+ h.usage.tokens = 200200; // no growth → replay
202
+ h.clearDebounce();
203
+ await h.fire("context", { type: "context", messages: setA }, ctx);
204
+ assert.ok(rt.diagLiveTrimReplays > replaysBefore,
205
+ `replay fired after dedup-onto-different-checkpoint (got ${rt.diagLiveTrimReplays}, was ${replaysBefore})`);
206
+ });
207
+
208
+ test("cleanup", async () => {
209
+ await closeVectorIndex();
210
+ rmSync(baseTmp, { recursive: true, force: true });
211
+ });
@@ -184,7 +184,9 @@ export function registerContextHandler(
184
184
  runtime.diagLiveTrimFires++; // trim view returned this call (replay counts as a fire)
185
185
  runtime.diagLiveTrimReplays++;
186
186
  runtime.snapshot(ctx);
187
- return { messages: [runtime.trimCache.summaryAgentMsg, ...recent] };
187
+ // v0.8.7: shallow-copy the cached summary so pi's transformContext can't
188
+ // mutate the shared reference across replays (audit P3).
189
+ return { messages: [{ ...runtime.trimCache.summaryAgentMsg }, ...recent] };
188
190
  }
189
191
  // else: context grew enough → fall through to re-compact (cache is stale)
190
192
  }
@@ -308,7 +310,21 @@ export function registerContextHandler(
308
310
  // replay it verbatim (stabilizing the KV-cache prefix) instead of
309
311
  // regenerating a fresh summary + sentinel every fire.
310
312
  runtime.trimCache = {
311
- checkpointId: ran.result.checkpointId ?? `epoch-${runtime.rt.lastCompactAt ?? Date.now()}`,
313
+ // v0.8.7: key the replay cache on the STABLE epoch signal
314
+ // (rt.lastCheckpointId) instead of ran.result.checkpointId, which is
315
+ // dedup-volatile: on a re-compact that dedups onto a DIFFERENT existing
316
+ // checkpoint, result.checkpointId is the matched id (engine.ts:188) while
317
+ // lastCheckpointId is only updated on a genuinely new checkpoint
318
+ // (compact.ts:100-104). Keying on result.checkpointId would make
319
+ // trimCache.checkpointId != rt.lastCheckpointId forever after that
320
+ // dedup fire, disabling replay for the rest of the epoch (the
321
+ // alternating cache-miss that 0.8.6 meant to fix). Prefer the stable
322
+ // signal; fall back to result.checkpointId then the epoch timestamp
323
+ // only for the no-checkpoint edge case.
324
+ checkpointId:
325
+ runtime.rt.lastCheckpointId ??
326
+ ran.result.checkpointId ??
327
+ `epoch-${runtime.rt.lastCompactAt ?? Date.now()}`,
312
328
  cut,
313
329
  summaryAgentMsg,
314
330
  ctxPct: pct ?? null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "description": "Layered, local, vector-backed context compressor for pi — supersede/collapse/cluster compaction with deduped inline recall.",
5
5
  "type": "module",
6
6
  "license": "BSD-2-Clause",