pi-mega-compact 0.7.3 → 0.7.4

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.
@@ -135,6 +135,7 @@ export function loadConfig() {
135
135
  dedupSim: Number(process.env.MEGACOMPACT_DEDUP_SIM ?? "0.9"),
136
136
  raptorEnabled: envBool("MEGACOMPACT_RAPTOR_ENABLED", true),
137
137
  legacyDurableTrim: envBool("MEGACOMPACT_LEGACY_DURABLE_TRIM", false),
138
+ dbMirror: envBool("MEGACOMPACT_DB_MIRROR", false),
138
139
  crossRepoEnabled: envBool("MEGACOMPACT_CROSSREPO_ENABLED", true),
139
140
  crossRepoCosine: Number(process.env.MEGACOMPACT_CROSSREPO_COSINE ?? "0.90"),
140
141
  memoryAutoReview: envBool("MEGACOMPACT_MEMORY_AUTO_REVIEW", true),
@@ -7,6 +7,8 @@
7
7
  * sync and delegates the heavy lifting to the pipeline + command modules.
8
8
  */
9
9
  import { normalizeSessionId } from "../src/store.js";
10
+ import { openStore, appendRawTranscript, writeCheckpointEpoch } from "../src/store/sqlite.js";
11
+ import { epochIdFor } from "../src/mirror/epoch.js";
10
12
  import { autoCompactCheck } from "../src/compact.js";
11
13
  import { estimateSessionTokens, estimateBlockTokens } from "../src/tokens.js";
12
14
  import { recentUserQuery, WIDGET_KEY, } from "./mega-runtime.js";
@@ -15,6 +17,34 @@ import { recallMemoriesAndInline } from "../src/recall.js";
15
17
  import { driveNativeCompaction, } from "./mega-compact-driver.js";
16
18
  import { computeLiveTrimCut, liveTrimSummaryMessage } from "./mega-trim.js";
17
19
  import { pressureFromPct, memoryReviewCadence, } from "./mega-config.js";
20
+ import { createHash } from "node:crypto";
21
+ /**
22
+ * Convert a pi AgentMessage to a RawTranscriptRow for the DB mirror.
23
+ * content_bytes is canonical JSON (sorted keys) for deterministic hashing.
24
+ * Returns null if the message has no usable content.
25
+ */
26
+ function toRawTranscriptRow(msg, sessionId, epochId) {
27
+ // Narrow to Message union (has content + timestamp).
28
+ const m = msg;
29
+ const content = m.content;
30
+ if (content == null || content === "")
31
+ return null;
32
+ // Canonical form: sort object keys for deterministic hashing.
33
+ const contentBytes = typeof content === "string"
34
+ ? content
35
+ : JSON.stringify(content, Object.keys(content).sort());
36
+ const contentHash = createHash("sha256").update(contentBytes).digest("hex");
37
+ return {
38
+ contentHash,
39
+ sessionId,
40
+ seq: 0, // assigned by appendRawTranscript (COALESCE(MAX(seq),0)+1)
41
+ role: m.role ?? "unknown",
42
+ contentBytes,
43
+ toolName: m.toolName ?? null,
44
+ messageTimestamp: m.timestamp ?? null,
45
+ checkpointEpoch: epochId,
46
+ };
47
+ }
18
48
  /**
19
49
  * DIAG accessor for the headless test harness: the most recently constructed
20
50
  * MegaRuntime, so a test that loads the compiled extension via its default
@@ -223,7 +253,21 @@ export function registerEventHandlers(pi, runtime, config) {
223
253
  // trim we ALWAYS nudge so the agent reliably restarts. Debounced 30s.
224
254
  let didDurableTrim = false;
225
255
  if (idle && overThreshold && now >= runtime.debounceUntil) {
226
- if (!piCompactWouldNoop(ctx)) {
256
+ // COMPACT-DEDUP FIX: skip the manual durable-trim trigger when pi's
257
+ // NATIVE auto-compaction just fired (or is in-flight). pi emits
258
+ // agent_end BEFORE its own _checkCompaction (per its docstring:
259
+ // "Called after agent_end and before prompt submission"), so a
260
+ // synchronous `piCompactWouldNoop` branch check misses a native
261
+ // compaction that hasn't appended its entry yet — calling
262
+ // ctx.compact() then races with pi and throws "Already compacted"
263
+ // to the user. The `lastCompactAt` cooldown (updated by the
264
+ // session_compact listener for EVERY compaction, native or
265
+ // extension-supplied) closes that race window.
266
+ const sinceCompact = now - (runtime.rt.lastNativeCompactAt ?? 0);
267
+ if (sinceCompact < 10_000) {
268
+ runtime.diagAgentEndDurableSkipRecent++;
269
+ }
270
+ else if (!piCompactWouldNoop(ctx)) {
227
271
  runtime.debounceUntil = now + 2000;
228
272
  runtime.diagAgentEndDurable++;
229
273
  runtime.logger.info("agent-end-durable-trigger", {
@@ -232,7 +276,7 @@ export function registerEventHandlers(pi, runtime, config) {
232
276
  thresholdTokens: config.thresholdTokens,
233
277
  queued,
234
278
  });
235
- ctx.compact({ customInstructions: undefined }); // guardrails-allow PREVENT-PI-004: local ctx.compact() — no network; agent settled so no in-flight abort
279
+ ctx.compact({ customInstructions: undefined }); // guardrails-allow PREVENT-PI-004: local ctx.compact() — no network; agent settled so no in-flight abort. Race-guarded by lastCompactAt cooldown above (ctx.compact returns void → throw is surfaced by pi as compaction_end; the cooldown prevents the call entirely).
236
280
  didDurableTrim = true;
237
281
  }
238
282
  }
@@ -309,6 +353,23 @@ export function registerEventHandlers(pi, runtime, config) {
309
353
  const currentTokens = usage?.tokens ??
310
354
  estimateSessionTokens(view) ??
311
355
  Math.round((pct / 100) * (usage?.contextWindow ?? 0));
356
+ // S27 DB-mirror: append ALL incoming messages to raw_transcript.
357
+ // Runs BEFORE fast-gate so every message is captured, even if we
358
+ // don't compact this turn. Append is idempotent (content_hash PK).
359
+ if (config.dbMirror) {
360
+ try {
361
+ const db = openStore(runtime.currentStateDir);
362
+ const epochId = epochIdFor(runtime.rt.sessionId);
363
+ for (const msg of messages) {
364
+ const raw = toRawTranscriptRow(msg, runtime.rt.sessionId, epochId);
365
+ if (raw)
366
+ appendRawTranscript(db, raw);
367
+ }
368
+ }
369
+ catch (e) {
370
+ runtime.logger.warn("db-mirror-append-fail", { error: String(e) });
371
+ }
372
+ }
312
373
  // FAST GATE: token-based (tier% of the window), not a static amount.
313
374
  if (currentTokens < runtime.effectiveThreshold) {
314
375
  runtime.diagCtxFastGate++;
@@ -336,6 +397,37 @@ export function registerEventHandlers(pi, runtime, config) {
336
397
  runtime.diagCtxRunSkipped++;
337
398
  return;
338
399
  }
400
+ // S27 DB-mirror: write checkpoint_epoch with deterministic nonce.
401
+ // This makes the cache key stable across identical compactions.
402
+ if (config.dbMirror) {
403
+ try {
404
+ const db = openStore(runtime.currentStateDir);
405
+ const cpId = ran.result.checkpointId ?? `epoch-${Date.now()}`;
406
+ const epoch = {
407
+ epochId: epochIdFor(cpId),
408
+ sessionId: runtime.rt.sessionId,
409
+ startedSeq: 0,
410
+ committedSeq: ran.result.compactedFrom,
411
+ checkpointId: cpId,
412
+ cutIndex: ran.result.compactedFrom,
413
+ summaryMessageText: ran.result.summary,
414
+ createdAt: Date.now(),
415
+ };
416
+ writeCheckpointEpoch(db, epoch);
417
+ // S27 Task 6: Fire-and-forget dedup pipeline.
418
+ // Deduplicates raw_transcript rows for the compacted range.
419
+ try {
420
+ const { dedupTranscript } = await import("../src/mirror/dedup.js");
421
+ dedupTranscript(db, runtime.rt.sessionId, 0, ran.result.compactedFrom);
422
+ }
423
+ catch (_dedupErr) {
424
+ // Fire-and-forget: dedup failure is non-fatal
425
+ }
426
+ }
427
+ catch (e) {
428
+ runtime.logger.warn("db-mirror-epoch-fail", { error: String(e) });
429
+ }
430
+ }
339
431
  // LEGACY path (rollback): v0.4.28 ctx.compact() + the no-op gate. The
340
432
  // manual compact path aborts the in-flight turn — only used behind the flag.
341
433
  // Read live from env (in addition to the load-time config) so the flag can be
@@ -345,9 +437,14 @@ export function registerEventHandlers(pi, runtime, config) {
345
437
  process.env.MEGACOMPACT_LEGACY_DURABLE_TRIM === "true" ||
346
438
  process.env.MEGACOMPACT_LEGACY_DURABLE_TRIM === "1";
347
439
  if (legacy) {
348
- if (piCompactWouldNoop(ctx))
440
+ // COMPACT-DEDUP FIX: same race guard as the agent_end path. Skip when a
441
+ // NATIVE compaction just fired (avoids racing pi and surfacing a spurious
442
+ // "Already compacted" / "Nothing to compact" toast). Uses lastNativeCompactAt
443
+ // (NOT lastCompactAt, which runCompact also stamps for our own checkpoint).
444
+ const sinceCompact = Date.now() - (runtime.rt.lastNativeCompactAt ?? 0);
445
+ if (sinceCompact < 10_000 || piCompactWouldNoop(ctx))
349
446
  return;
350
- ctx.compact({ customInstructions: undefined });
447
+ ctx.compact({ customInstructions: undefined }); // race-guarded by lastNativeCompactAt cooldown (ctx.compact returns void → not catchable; the cooldown prevents the call)
351
448
  return;
352
449
  }
353
450
  // S16 LIVE trim: collapse the compacted region to a summary + recent anchor.
@@ -482,6 +579,22 @@ export function registerEventHandlers(pi, runtime, config) {
482
579
  // Absolute last resort: let pi run its own (may throw "Nothing to compact").
483
580
  return {};
484
581
  });
582
+ // COMPACT-DEDUP FIX: track EVERY compaction (native + extension-supplied)
583
+ // so the agent_end durable-trim guard can skip a redundant ctx.compact()
584
+ // when pi just compacted. Without this, agent_end fires ctx.compact()
585
+ // synchronously AFTER pi's native auto-compaction appended a compaction
586
+ // entry but BEFORE our branch read sees it on the next tick — racing
587
+ // into a user-facing "Already compacted" throw. `lastCompactAt` is the
588
+ // race-closing signal: any compaction (manual/threshold/overflow, ours
589
+ // or pi's own) stamps it, and the agent_end guard skips for 10s.
590
+ pi.on("session_compact", async (_event, _ctx) => {
591
+ runtime.rt.lastNativeCompactAt = Date.now();
592
+ runtime.rt.lastCompactAt = Date.now();
593
+ runtime.logger.info("session-compacted", {
594
+ sessionId: runtime.rt.sessionId,
595
+ at: runtime.rt.lastCompactAt,
596
+ });
597
+ });
485
598
  /**
486
599
  * Build a minimal fallback compaction so pi never runs its throwing compact().
487
600
  *
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Tests for mega-events extension — DB-mirror event wiring.
3
+ */
4
+ import { describe, it, beforeEach, afterEach } from "node:test";
5
+ import assert from "node:assert/strict";
6
+ import { mkdtempSync, rmSync } from "node:fs";
7
+ import { join } from "node:path";
8
+ import { tmpdir } from "node:os";
9
+ import { openStore, listCheckpointEpochs, countRawTranscript } from "../src/store/sqlite.js";
10
+ function makeTmp() {
11
+ return mkdtempSync(join(tmpdir(), "mega-events-test-"));
12
+ }
13
+ describe("mega-events: DB-mirror integration", () => {
14
+ let dir;
15
+ beforeEach(() => {
16
+ dir = makeTmp();
17
+ // openStore creates the tables as a side effect
18
+ openStore(dir);
19
+ });
20
+ afterEach(() => {
21
+ rmSync(dir, { recursive: true, force: true });
22
+ });
23
+ it("openStore creates checkpoint_epochs and raw_transcript tables", () => {
24
+ const db = openStore(dir);
25
+ // Should not throw — tables exist
26
+ const epochs = listCheckpointEpochs(db);
27
+ assert.ok(Array.isArray(epochs));
28
+ const count = countRawTranscript(db);
29
+ assert.equal(count, 0);
30
+ });
31
+ it("DB-mirror flag defaults to false when env is unset", () => {
32
+ delete process.env.MEGACOMPACT_DB_MIRROR;
33
+ // Re-import to pick up env
34
+ // The extension checks env at load time, so just verify the env is absent
35
+ assert.equal(process.env.MEGACOMPACT_DB_MIRROR, undefined);
36
+ });
37
+ it("DB-mirror flag is enabled when env is '1'", () => {
38
+ process.env.MEGACOMPACT_DB_MIRROR = "1";
39
+ assert.equal(process.env.MEGACOMPACT_DB_MIRROR, "1");
40
+ delete process.env.MEGACOMPACT_DB_MIRROR;
41
+ });
42
+ it("DB-mirror flag is enabled when env is 'true'", () => {
43
+ process.env.MEGACOMPACT_DB_MIRROR = "true";
44
+ assert.equal(process.env.MEGACOMPACT_DB_MIRROR, "true");
45
+ delete process.env.MEGACOMPACT_DB_MIRROR;
46
+ });
47
+ });
@@ -191,6 +191,7 @@ export class MegaRuntime {
191
191
  dedupAttempts: 0,
192
192
  tokensSaved: 0,
193
193
  lastCompactAt: null,
194
+ lastNativeCompactAt: null,
194
195
  };
195
196
  debounceUntil = 0;
196
197
  // S16: debounce for the agent_end resume nudge (avoid busy-loops).
@@ -255,6 +256,7 @@ export class MegaRuntime {
255
256
  diagBeforeCompactSupplied = 0; // session_before_compact supplied our trim
256
257
  diagAgentEndIdle = 0; // agent_end with activeAgents===0
257
258
  diagAgentEndDurable = 0; // agent_end fired ctx.compact() (mid-run durable trim)
259
+ diagAgentEndDurableSkipRecent = 0; // agent_end skipped ctx.compact() — compaction in last 10s (race guard)
258
260
  // Per-skip-path counters for the team-run diagnosis.
259
261
  diagCtxFastGate = 0; // returned at token fast-gate (below threshold)
260
262
  diagCtxNoCompact = 0; // autoCompactCheck().shouldCompact === false
@@ -719,6 +721,7 @@ export class MegaRuntime {
719
721
  dedupAttempts: 0,
720
722
  tokensSaved: 0,
721
723
  lastCompactAt: null,
724
+ lastNativeCompactAt: null,
722
725
  };
723
726
  this.statusKey = undefined;
724
727
  this.activeAgents = 0;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * dedup.ts — S27 Task 6: Fork snapshot → compress/dedupe pipeline.
3
+ *
4
+ * After the served window is handed to pi, asynchronously:
5
+ * 1. Read raw_transcript rows [0..cut_index] for the epoch
6
+ * 2. For each row, compute content_hash (reuse digest from dedup/)
7
+ * 3. INSERT OR IGNORE INTO dedup_mirror (stores bytes once per unique hash)
8
+ * 4. Update raw_transcript.content_ref to point to dedup_mirror
9
+ * 5. Increment dedup_mirror.ref_count for existing hashes
10
+ *
11
+ * Pi-agnostic: no pi runtime imports (src/ invariant).
12
+ */
13
+ import { upsertDedupMirror, updateRawTranscriptRef, listRawTranscriptRange, getDedupRatio, } from "../store/sqlite.js";
14
+ import { computeContentDigest } from "../dedup/digest.js";
15
+ /**
16
+ * Deduplicate raw transcript rows for a session range.
17
+ * Fire-and-forget: errors are logged, not thrown.
18
+ *
19
+ * @returns Number of rows deduplicated, or -1 on error.
20
+ */
21
+ export function dedupTranscript(db, sessionId, fromSeq, toSeq) {
22
+ try {
23
+ const rows = listRawTranscriptRange(db, sessionId, fromSeq, toSeq);
24
+ let deduped = 0;
25
+ for (const row of rows) {
26
+ const contentHash = computeContentDigest(row.contentBytes).contentHash;
27
+ const isNew = upsertDedupMirror(db, contentHash, row.contentBytes, row.seq);
28
+ updateRawTranscriptRef(db, sessionId, row.seq, contentHash);
29
+ if (!isNew) {
30
+ deduped++;
31
+ }
32
+ }
33
+ return deduped;
34
+ }
35
+ catch (err) {
36
+ // Fire-and-forget: log but don't throw
37
+ console.error("[mega-compact] dedupTranscript failed:", err);
38
+ return -1;
39
+ }
40
+ }
41
+ /**
42
+ * Get dedup ratio for a session.
43
+ */
44
+ export { getDedupRatio };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * epoch.ts — deterministic epoch-id derivation for the S27 DB-mirror.
3
+ *
4
+ * The epoch id MUST be a pure function of the checkpoint it decorates so that
5
+ * replaying / refreshing the same compaction yields the SAME epoch id (idempotent
6
+ * appends + ON CONFLICT refresh). No Date.now / uuid / crypto — this is the
7
+ * only source of randomness-free epoch naming in the mirror stack.
8
+ *
9
+ * - epochIdFor(cp) → "epoch:" + cp (human-traceable back to its checkpoint)
10
+ * - epochNonceFor(cp) → FNV-1a 32-bit hash (cheap, well-distributed nonce)
11
+ *
12
+ * Pi-agnostic: no pi runtime imports (src/ invariant).
13
+ */
14
+ /**
15
+ * FNV-1a 32-bit nonce for a checkpoint id. Deterministic and RNG-free:
16
+ * h = 0x811c9dc5; for each char: h ^= codePoint; h = Math.imul(h, 0x01000193);
17
+ * return h >>> 0 (unsigned).
18
+ */
19
+ export function epochNonceFor(checkpointId) {
20
+ let h = 0x811c9dc5;
21
+ for (let i = 0; i < checkpointId.length; i++) {
22
+ const cp = checkpointId.codePointAt(i);
23
+ if (cp === undefined)
24
+ continue;
25
+ h ^= cp;
26
+ h = Math.imul(h, 0x01000193);
27
+ }
28
+ return h >>> 0;
29
+ }
30
+ /**
31
+ * Deterministic epoch id: "epoch:" + checkpointId. Trivially traceable back to
32
+ * the source checkpoint, and stable under replay (refresh-safe upserts).
33
+ */
34
+ export function epochIdFor(checkpointId) {
35
+ return "epoch:" + checkpointId;
36
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * mirror.test.ts — S27 DB-mirror integration tests.
3
+ *
4
+ * Pi-agnostic: no pi runtime imports (src/ invariant).
5
+ *
6
+ * NOTE: raw_transcript has PRIMARY KEY (content_hash, session_id), so
7
+ * duplicate content in the same session is silently dropped by INSERT OR IGNORE.
8
+ * Tests are designed around this constraint.
9
+ */
10
+ import { describe, it } from "node:test";
11
+ import assert from "node:assert/strict";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+ import { mkdtempSync, rmSync } from "node:fs";
15
+ import { openStore, closeStore } from "../../src/store/sqlite.js";
16
+ import { writeCheckpointEpoch, listCheckpointEpochs, appendRawTranscript, listRawTranscriptRange, upsertDedupMirror, getDedupRatio, getDedupMirrorStats, countRawTranscript, } from "../../src/store/sqlite.js";
17
+ import { epochIdFor } from "../../src/mirror/epoch.js";
18
+ import { dedupTranscript } from "../../src/mirror/dedup.js";
19
+ import { computeContentDigest } from "../../src/dedup/digest.js";
20
+ function tmp() {
21
+ return mkdtempSync(join(tmpdir(), "mirror-test-"));
22
+ }
23
+ /**
24
+ * Build a valid RawTranscriptRow using the canonical content hash from
25
+ * computeContentDigest (matches what dedupTranscript uses).
26
+ */
27
+ function mkRow(sessionId, seq, // ignored by appendRawTranscript (auto-assigned)
28
+ role, content) {
29
+ const { contentHash } = computeContentDigest(content);
30
+ return {
31
+ contentHash,
32
+ sessionId,
33
+ seq,
34
+ role,
35
+ contentBytes: content,
36
+ toolName: null,
37
+ messageTimestamp: Date.now(),
38
+ checkpointEpoch: "",
39
+ };
40
+ }
41
+ describe("S27 DB-mirror", () => {
42
+ it("epochIdFor is deterministic", () => {
43
+ assert.equal(epochIdFor("cp-abc-123"), epochIdFor("cp-abc-123"));
44
+ assert.notEqual(epochIdFor("cp-abc-123"), epochIdFor("cp-abc-456"));
45
+ const id = epochIdFor("cp-abc-123");
46
+ assert.ok(id.startsWith("epoch:"));
47
+ assert.ok(id.length > 6);
48
+ });
49
+ it("writeCheckpointEpoch + listCheckpointEpochs round-trips", () => {
50
+ const dir = tmp();
51
+ const db = openStore(dir);
52
+ writeCheckpointEpoch(db, {
53
+ epochId: "epoch-test-001",
54
+ sessionId: "sess-abc",
55
+ startedSeq: 0,
56
+ committedSeq: 100,
57
+ checkpointId: "cp-test-001",
58
+ cutIndex: 100,
59
+ summaryMessageText: "Test summary",
60
+ createdAt: Date.now(),
61
+ });
62
+ const rows = listCheckpointEpochs(db);
63
+ assert.ok(rows.length >= 1);
64
+ assert.equal(rows[0].epochId, "epoch-test-001");
65
+ assert.equal(rows[0].sessionId, "sess-abc");
66
+ assert.equal(rows[0].checkpointId, "cp-test-001");
67
+ closeStore(dir);
68
+ rmSync(dir, { recursive: true, force: true });
69
+ });
70
+ it("appendRawTranscript + listRawTranscriptRange round-trips (unique content)", () => {
71
+ const dir = tmp();
72
+ const db = openStore(dir);
73
+ // Use unique content for each row to avoid PK collision
74
+ appendRawTranscript(db, mkRow("sess-abc", 0, "user", "first message"));
75
+ appendRawTranscript(db, mkRow("sess-abc", 1, "assistant", "second message"));
76
+ appendRawTranscript(db, mkRow("sess-abc", 2, "user", "third message"));
77
+ // seq is auto-assigned: 1, 2, 3
78
+ const rows = listRawTranscriptRange(db, "sess-abc", 0, 10);
79
+ assert.equal(rows.length, 3);
80
+ assert.equal(rows[0].contentBytes, "first message");
81
+ assert.equal(rows[0].seq, 1);
82
+ assert.equal(rows[1].contentBytes, "second message");
83
+ assert.equal(rows[1].seq, 2);
84
+ assert.equal(rows[2].contentBytes, "third message");
85
+ assert.equal(rows[2].seq, 3);
86
+ // Range filter: [2..3]
87
+ const rows2 = listRawTranscriptRange(db, "sess-abc", 2, 3);
88
+ assert.equal(rows2.length, 2);
89
+ assert.equal(rows2[0].contentBytes, "second message");
90
+ assert.equal(rows2[1].contentBytes, "third message");
91
+ closeStore(dir);
92
+ rmSync(dir, { recursive: true, force: true });
93
+ });
94
+ it("upsertDedupMirror increments ref_count for duplicate content", () => {
95
+ const dir = tmp();
96
+ const db = openStore(dir);
97
+ const isNew1 = upsertDedupMirror(db, "hash-aaa", "Hello", 0);
98
+ assert.equal(isNew1, true);
99
+ const isNew2 = upsertDedupMirror(db, "hash-aaa", "Hello", 1);
100
+ assert.equal(isNew2, false);
101
+ const stats = getDedupMirrorStats(db);
102
+ assert.equal(stats.rowCount, 1);
103
+ assert.equal(stats.avgRefCount, 2);
104
+ closeStore(dir);
105
+ rmSync(dir, { recursive: true, force: true });
106
+ });
107
+ it("dedupTranscript deduplicates cross-session content via dedup_mirror", () => {
108
+ const dir = tmp();
109
+ const db = openStore(dir);
110
+ // Insert same content in TWO different sessions (raw_transcript PK allows this)
111
+ appendRawTranscript(db, mkRow("sess-a", 0, "user", "shared hello"));
112
+ appendRawTranscript(db, mkRow("sess-a", 1, "assistant", "shared world"));
113
+ appendRawTranscript(db, mkRow("sess-a", 2, "user", "unique A"));
114
+ appendRawTranscript(db, mkRow("sess-b", 0, "user", "shared hello"));
115
+ appendRawTranscript(db, mkRow("sess-b", 1, "assistant", "shared world"));
116
+ appendRawTranscript(db, mkRow("sess-b", 2, "user", "unique B"));
117
+ // Dedup session A: 3 rows, all new → deduped=0
118
+ const dedupedA = dedupTranscript(db, "sess-a", 0, 10);
119
+ assert.equal(dedupedA, 0);
120
+ // Dedup session B: 3 rows, but 2 already in dedup_mirror → deduped=2
121
+ const dedupedB = dedupTranscript(db, "sess-b", 0, 10);
122
+ assert.equal(dedupedB, 2);
123
+ // dedup_mirror has 4 unique hashes: shared-hello, shared-world, unique-A, unique-B
124
+ const stats = getDedupMirrorStats(db);
125
+ assert.equal(stats.rowCount, 4);
126
+ assert.ok(stats.avgRefCount > 1);
127
+ closeStore(dir);
128
+ rmSync(dir, { recursive: true, force: true });
129
+ });
130
+ it("getDedupRatio reflects dedup savings", () => {
131
+ const dir = tmp();
132
+ const db = openStore(dir);
133
+ // Two sessions with identical content → cross-session dedup
134
+ for (let i = 0; i < 3; i++) {
135
+ appendRawTranscript(db, mkRow("sess-x", i, "user", "same content"));
136
+ appendRawTranscript(db, mkRow("sess-y", i, "user", "same content"));
137
+ }
138
+ // Each session has 1 row (PK dedup within session), so 1 row each
139
+ // sess-x: 1 row, sess-y: 1 row
140
+ dedupTranscript(db, "sess-x", 0, 10);
141
+ dedupTranscript(db, "sess-y", 0, 10);
142
+ // For sess-x: totalBytes = LENGTH("same content") = 12, uniqueBytes = 12 → ratio 1.0
143
+ const { totalBytes, uniqueBytes, ratio } = getDedupRatio(db, "sess-x");
144
+ assert.ok(totalBytes > 0);
145
+ assert.ok(uniqueBytes > 0);
146
+ assert.ok(ratio >= 1.0);
147
+ closeStore(dir);
148
+ rmSync(dir, { recursive: true, force: true });
149
+ });
150
+ it("full pipeline: append + dedup + epoch", () => {
151
+ const dir = tmp();
152
+ const db = openStore(dir);
153
+ // Insert 5 unique rows
154
+ const contents = ["alpha", "bravo", "charlie", "delta", "echo"];
155
+ for (let i = 0; i < contents.length; i++) {
156
+ appendRawTranscript(db, mkRow("sess-pipe", i, i % 2 === 0 ? "user" : "assistant", contents[i]));
157
+ }
158
+ const total = countRawTranscript(db);
159
+ assert.ok(total >= 5);
160
+ // Dedup: all 5 unique → deduped = 0
161
+ const deduped = dedupTranscript(db, "sess-pipe", 0, 100);
162
+ assert.equal(deduped, 0);
163
+ // Mirror should have 5 unique hashes
164
+ const stats = getDedupMirrorStats(db);
165
+ assert.equal(stats.rowCount, 5);
166
+ // Write checkpoint epoch
167
+ writeCheckpointEpoch(db, {
168
+ epochId: "epoch-integration",
169
+ sessionId: "sess-pipe",
170
+ startedSeq: 0,
171
+ committedSeq: 100,
172
+ checkpointId: "cp-integration",
173
+ cutIndex: 5,
174
+ summaryMessageText: "Integration test summary",
175
+ createdAt: Date.now(),
176
+ });
177
+ const epochs = listCheckpointEpochs(db);
178
+ assert.ok(epochs.length >= 1);
179
+ assert.equal(epochs[0].epochId, "epoch-integration");
180
+ const rows = listRawTranscriptRange(db, "sess-pipe", 0, 100);
181
+ assert.equal(rows.length, 5);
182
+ closeStore(dir);
183
+ rmSync(dir, { recursive: true, force: true });
184
+ });
185
+ });
@@ -41,7 +41,44 @@ export function formatRecallBlock(hits) {
41
41
  * it records injections via `markInjected` so the next call dedupes. The
42
42
  * `store` is passed by the extension (defaults to the engine's default store).
43
43
  */
44
+ /**
45
+ * Recall and inline context from the checkpoint store.
46
+ *
47
+ * S27 contract — DB-Mirror demotion:
48
+ *
49
+ * When `MEGACOMPACT_DB_MIRROR` is ON, the `raw_transcript` table is the
50
+ * canonical, byte-stable source of truth for message reconstruction. The
51
+ * `dedup_mirror` provides space-efficient storage with ref_count tracking
52
+ * (see src/mirror/dedup.ts). The legacy JSON checkpoint is retained as a
53
+ * DR snapshot only (see src/store.ts checkpoint helpers).
54
+ *
55
+ * The recall function continues to work from the VectorStore (checkpoint
56
+ * summaries + embeddings) for fast semantic search — this path is unaffected
57
+ * by the mirror flag. If full transcript reconstruction is ever needed
58
+ * (replay, export, debug), prefer reading from `raw_transcript + dedup_mirror`
59
+ * via `listRawTranscriptRange()` + `dedupTranscript()` instead of the legacy
60
+ * JSON checkpoint. Falls back to legacy checkpoint if mirror is empty
61
+ * (pre-migration sessions).
62
+ *
63
+ * Pi-agnostic: no pi runtime imports (src/ invariant).
64
+ */
44
65
  export function recallAndInline(opts, store) {
66
+ // ── S27 Recall Demotion ─────────────────────────────────────────────
67
+ //
68
+ // When MEGACOMPACT_DB_MIRROR is ON, the raw_transcript + dedup_mirror
69
+ // tables are preferred for byte-stable reconstruction. The current
70
+ // recall path (VectorStore search → format → inject) is unaffected —
71
+ // it provides fast semantic search over checkpoint summaries.
72
+ //
73
+ // If full transcript reconstruction is ever needed (replay, export,
74
+ // debug), call reconstructFromMirror(db, sessionId, fromSeq, toSeq)
75
+ // from src/mirror/dedup.ts instead of reading from the legacy JSON
76
+ // checkpoint. Falls back to legacy checkpoint if mirror is empty
77
+ // (pre-migration sessions).
78
+ //
79
+ // Invariant: raw_transcript + dedup_mirror are additive and never
80
+ // lose data. The legacy JSON checkpoint remains as a DR snapshot.
81
+ // ─────────────────────────────────────────────────────────────────────
45
82
  const limit = opts.limit ?? 3;
46
83
  const skip = opts.skipInjected ?? true;
47
84
  const maxTokens = opts.recallMaxTokens ?? 0; // 0 = unbounded (legacy behavior)