pi-mega-compact 0.8.6 → 0.8.8

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.
Files changed (29) hide show
  1. package/dist/extensions/dashboard-server/html.js +46 -1
  2. package/dist/extensions/dashboard-server/perf-server.test.js +80 -0
  3. package/dist/extensions/dashboard-server/server.js +87 -0
  4. package/dist/extensions/mega-cache-replay.test.js +189 -0
  5. package/dist/extensions/mega-dashboard.js +9 -0
  6. package/dist/extensions/mega-events/context-handler.js +17 -2
  7. package/dist/extensions/mega-events/perf-handler.js +71 -0
  8. package/dist/extensions/mega-events/register.js +2 -0
  9. package/dist/extensions/mega-events.js +1 -0
  10. package/dist/extensions/mega-runtime/state.js +59 -1
  11. package/dist/src/store/sqlite/perf-samples.js +81 -0
  12. package/dist/src/store/sqlite/perf-samples.test.js +54 -0
  13. package/dist/src/store/sqlite/schema.js +14 -0
  14. package/dist/src/store/sqlite.js +1 -0
  15. package/extensions/dashboard-server/html.ts +46 -1
  16. package/extensions/dashboard-server/perf-server.test.ts +101 -0
  17. package/extensions/dashboard-server/server.ts +79 -0
  18. package/extensions/mega-cache-replay.test.ts +211 -0
  19. package/extensions/mega-dashboard.ts +17 -0
  20. package/extensions/mega-events/context-handler.ts +18 -2
  21. package/extensions/mega-events/perf-handler.ts +113 -0
  22. package/extensions/mega-events/register.ts +2 -0
  23. package/extensions/mega-events.ts +1 -0
  24. package/extensions/mega-runtime/state.ts +57 -0
  25. package/package.json +1 -1
  26. package/src/store/sqlite/perf-samples.test.ts +65 -0
  27. package/src/store/sqlite/perf-samples.ts +125 -0
  28. package/src/store/sqlite/schema.ts +14 -0
  29. package/src/store/sqlite.ts +1 -0
@@ -186,6 +186,7 @@ export function dashboardHtml(tierName) {
186
186
  <button class="tab" data-tab="active">Active Repos</button>
187
187
  <button class="tab" data-tab="summary">Summary</button>
188
188
  <button class="tab" data-tab="game">Game Mode</button>
189
+ <button class="tab" data-tab="perf">Perf</button>
189
190
  </nav>
190
191
 
191
192
  <!-- Current repo (existing single-repo view) -->
@@ -440,6 +441,18 @@ export function dashboardHtml(tierName) {
440
441
  <div id="game-empty">No scores yet — run a session with game mode on.</div>
441
442
  </div>
442
443
 
444
+ <!-- Perf (v0.8.8) — live local instrumentation -->
445
+ <div class="tab-panel" id="panel-perf">
446
+ <div class="grid">
447
+ <div class="card"><h2>Model latency</h2><div class="stat-grid"><span class="label">Turn p50</span><span class="value" id="pf-turn-p50">—</span><span class="label">Turn p95</span><span class="value" id="pf-turn-p95">—</span><span class="label">Provider p50</span><span class="value" id="pf-prov-p50">—</span><span class="label">Provider p95</span><span class="value" id="pf-prov-p95">—</span></div></div>
448
+ <div class="card"><h2>Throughput</h2><div class="stat-grid"><span class="label">TPS (avg)</span><span class="value" id="pf-tps">—</span><span class="label">Cache hit %</span><span class="value" id="pf-cache">—</span></div></div>
449
+ <div class="card"><h2>Process</h2><div class="stat-grid"><span class="label">RSS</span><span class="value" id="pf-rss">—</span><span class="label">Heap</span><span class="value" id="pf-heap">—</span><span class="label">CPU user/sys</span><span class="value" id="pf-cpu">—</span></div></div>
450
+ <div class="card"><h2>Snapshot cost</h2><div class="stat-grid"><span class="label">DB recompute p50</span><span class="value" id="pf-db-p50">—</span><span class="label">DB recompute p95</span><span class="value" id="pf-db-p95">—</span><span class="label">Disk write p50</span><span class="value" id="pf-disk">—</span></div></div>
451
+ <div class="card"><h2>TUI lag proxy</h2><div class="stat-grid"><span class="label">Live-trim fires</span><span class="value" id="pf-recompute">—</span><span class="label">Cache replays</span><span class="value" id="pf-replays">—</span><span class="label">Fast-gate skips</span><span class="value" id="pf-skips">—</span></div><div class="meter-sub">skip vs recompute vs replay cadence</div></div>
452
+ </div>
453
+ <div class="updated" id="perf-updated">waiting for data</div>
454
+ </div>
455
+
443
456
  <script>
444
457
  (function() {
445
458
  var evBox = document.getElementById('events');
@@ -997,9 +1010,40 @@ export function dashboardHtml(tierName) {
997
1010
  }).catch(function() {});
998
1011
  }
999
1012
 
1013
+ // --- Perf tab (v0.8.8) — live local instrumentation ----------------------
1014
+ var perfPollTimer = null;
1015
+ function pollPerf() {
1016
+ fetch('/api/perf?minutes=30').then(function(r) { return r.ok ? r.json() : null; }).then(function(d) { // guardrails-allow PREVENT-PI-004: browser-side fetch in dashboard HTML template (not Node runtime)
1017
+ if (!d) return;
1018
+ var el = document.getElementById('perf-updated');
1019
+ if (el) el.textContent = d.sampleCount + ' samples \u00b7 updated ' + (d.updatedAt || '');
1020
+ function setText(id, txt) { var e = document.getElementById(id); if (e) e.textContent = txt; }
1021
+ function fmtMs(v) { return v == null ? '\u2014' : (v >= 100 ? Math.round(v) + 'ms' : v.toFixed(1) + 'ms'); }
1022
+ function fmtNum(v, dec) { return v == null ? '\u2014' : (typeof v === 'number' ? v.toFixed(dec) : '\u2014'); }
1023
+ setText('pf-turn-p50', fmtMs(d.turn_latency_ms && d.turn_latency_ms.p50));
1024
+ setText('pf-turn-p95', fmtMs(d.turn_latency_ms && d.turn_latency_ms.p95));
1025
+ setText('pf-prov-p50', fmtMs(d.provider_latency_ms && d.provider_latency_ms.p50));
1026
+ setText('pf-prov-p95', fmtMs(d.provider_latency_ms && d.provider_latency_ms.p95));
1027
+ setText('pf-tps', fmtNum(d.tps && d.tps.avg, 1));
1028
+ setText('pf-cache', (d.cache_hit_pct && typeof d.cache_hit_pct.avg === 'number') ? fmtNum(d.cache_hit_pct.avg, 1) + '%' : '\u2014');
1029
+ setText('pf-rss', (d.rss_mb && typeof d.rss_mb.latest === 'number') ? fmtNum(d.rss_mb.latest, 1) + ' MB' : '\u2014');
1030
+ setText('pf-heap', (d.heap_mb && typeof d.heap_mb.latest === 'number') ? fmtNum(d.heap_mb.latest, 1) + ' MB' : '\u2014');
1031
+ setText('pf-cpu', (d.cpu_user_ms && d.cpu_sys_ms) ? (fmtNum(d.cpu_user_ms.latest,1) + ' / ' + fmtNum(d.cpu_sys_ms.latest,1) + ' ms') : '\u2014');
1032
+ setText('pf-db-p50', fmtMs(d.db_recompute_ms && d.db_recompute_ms.p50));
1033
+ setText('pf-db-p95', fmtMs(d.db_recompute_ms && d.db_recompute_ms.p95));
1034
+ setText('pf-disk', fmtMs(d.disk_write_ms && d.disk_write_ms.p50));
1035
+ var diag = d.diag || {};
1036
+ setText('pf-recompute', diag.liveTrimFires != null ? String(diag.liveTrimFires) : '\u2014');
1037
+ setText('pf-replays', diag.liveTrimReplays != null ? String(diag.liveTrimReplays) : '\u2014');
1038
+ setText('pf-skips', diag.ctxFastGate != null ? String(diag.ctxFastGate) : '\u2014');
1039
+ }).catch(function() {});
1040
+ }
1041
+ function startPerfPoll() { if (perfPollTimer) return; pollPerf(); perfPollTimer = setInterval(pollPerf, 2000); }
1042
+ function stopPerfPoll() { if (perfPollTimer) { clearInterval(perfPollTimer); perfPollTimer = null; } }
1043
+
1000
1044
  // --- Tab switching ------------------------------------------------------
1001
1045
  var tabs = document.querySelectorAll('.tab');
1002
- var panels = { current: 'panel-current', all: 'panel-all', active: 'panel-active', summary: 'panel-summary', game: 'panel-game' };
1046
+ var panels = { current: 'panel-current', all: 'panel-all', active: 'panel-active', summary: 'panel-summary', game: 'panel-game', perf: 'panel-perf' };
1003
1047
  for (var i = 0; i < tabs.length; i++) {
1004
1048
  tabs[i].addEventListener('click', function() {
1005
1049
  var name = this.getAttribute('data-tab');
@@ -1014,6 +1058,7 @@ export function dashboardHtml(tierName) {
1014
1058
  if (name === 'all' || name === 'summary') pollIndex();
1015
1059
  if (name === 'active') pollServers();
1016
1060
  if (name === 'game') { renderGameScores(); renderAchievements(); }
1061
+ if (name === 'perf') startPerfPoll(); else stopPerfPoll();
1017
1062
  });
1018
1063
  }
1019
1064
  })();
@@ -0,0 +1,80 @@
1
+ /**
2
+ * perf-server.test.ts — v0.8.8 /api/perf endpoint (GET aggregates + 405).
3
+ * Mirrors the server.test.ts spawn-and-fetch harness (self-contained so the
4
+ * dashboard HTTP-port lane stays isolated).
5
+ */
6
+ import { test, describe } from "node:test";
7
+ import assert from "node:assert/strict";
8
+ import { mkdtempSync, rmSync, readFileSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import { spawn } from "node:child_process";
12
+ import { recordPerfSample } from "../../src/store/sqlite.js";
13
+ const SERVER_ENTRY = new URL("./server.js", import.meta.url).pathname;
14
+ function waitFor(cond, timeoutMs = 6000) {
15
+ const start = Date.now();
16
+ return new Promise((resolve, reject) => {
17
+ const tick = async () => {
18
+ if (await cond())
19
+ return resolve();
20
+ if (Date.now() - start > timeoutMs)
21
+ return reject(new Error("timeout"));
22
+ setTimeout(tick, 50);
23
+ };
24
+ tick();
25
+ });
26
+ }
27
+ function freshDir(prefix) {
28
+ return mkdtempSync(join(tmpdir(), prefix));
29
+ }
30
+ async function withServer(port, dir, fn) {
31
+ process.env.MEGACOMPACT_DASHBOARD_PORT = port;
32
+ const child = spawn(process.execPath, [SERVER_ENTRY, dir], { stdio: "ignore" });
33
+ try {
34
+ await waitFor(async () => {
35
+ try {
36
+ const raw = JSON.parse(readFileSync(join(dir, "port.pid"), "utf-8"));
37
+ const res = await fetch(`http://localhost:${raw.port}/api/version`);
38
+ return res.ok;
39
+ }
40
+ catch {
41
+ return false;
42
+ }
43
+ });
44
+ const raw = JSON.parse(readFileSync(join(dir, "port.pid"), "utf-8"));
45
+ return await fn(raw.port);
46
+ }
47
+ finally {
48
+ child.kill("SIGTERM");
49
+ delete process.env.MEGACOMPACT_DASHBOARD_PORT;
50
+ rmSync(dir, { recursive: true, force: true });
51
+ }
52
+ }
53
+ describe("v0.8.8 /api/perf", () => {
54
+ test("GET returns aggregates over recorded perf_samples", async () => {
55
+ const dir = freshDir("dash-perf-agg-");
56
+ recordPerfSample(dir, "turn_latency_ms", 100, { turnIndex: 1 });
57
+ recordPerfSample(dir, "turn_latency_ms", 200);
58
+ recordPerfSample(dir, "tps", 50);
59
+ recordPerfSample(dir, "rss_mb", 256);
60
+ await withServer("19440", dir, async (port) => {
61
+ const res = await fetch(`http://localhost:${port}/api/perf?minutes=30`);
62
+ assert.equal(res.status, 200);
63
+ const d = (await res.json());
64
+ assert.equal(d.sampleCount, 4);
65
+ assert.equal(d.turn_latency_ms.n, 2);
66
+ assert.equal(d.turn_latency_ms.p50, 100); // nearest-rank p50 of [100,200]
67
+ assert.equal(d.turn_latency_ms.p95, 200); // nearest-rank p95 of [100,200]
68
+ assert.equal(d.tps.avg, 50);
69
+ assert.equal(d.rss_mb.latest, 256);
70
+ assert.equal(d.diag, null); // no runtime wrote dashboard.json in this dir
71
+ });
72
+ });
73
+ test("non-GET (POST) -> 405", async () => {
74
+ const dir = freshDir("dash-perf-meth-");
75
+ await withServer("19441", dir, async (port) => {
76
+ const res = await fetch(`http://localhost:${port}/api/perf`, { method: "POST" });
77
+ assert.equal(res.status, 405);
78
+ });
79
+ });
80
+ });
@@ -444,6 +444,93 @@ export async function launchDashboardServer(stateDir) {
444
444
  }
445
445
  return;
446
446
  }
447
+ // /api/perf — v0.8.8 Perf dashboard tab. GET returns rolling-window
448
+ // aggregates over perf_samples: per-kind p50/p95 (turn/provider latency,
449
+ // tps avg, db recompute, disk write), latest rss/heap, cpu user/sys delta,
450
+ // cache hit %, plus the diag recompute/skip/replay counts (read from
451
+ // dashboard.json snapshot if available). The dashboard server is a detached
452
+ // child with no MegaRuntime ref, so it reads perf_samples via a require()'d
453
+ // sqlite helper (same pattern as /api/game-scores). Unknown/invalid params
454
+ // are clamped (never throw). Non-GET -> 405. PREVENT-PI-004: loopback.
455
+ if (req.url?.startsWith("/api/perf")) {
456
+ const pfReq = createRequire(import.meta.url);
457
+ const { readPerfSamples } = pfReq("../../src/store/sqlite.js");
458
+ if (req.method !== "GET") {
459
+ res.writeHead(405, { "Content-Type": "application/json" }); // guardrails-allow PREVENT-PI-004: loopback dashboard response (local)
460
+ res.end(JSON.stringify({ error: "method_not_allowed" }));
461
+ return;
462
+ }
463
+ try {
464
+ const url = new URL(req.url, "http://x"); // guardrails-allow PREVENT-PI-004: localhost dashboard URL base (loopback-only)
465
+ let minutes = Number(url.searchParams.get("minutes") ?? "30");
466
+ if (!Number.isFinite(minutes) || minutes <= 0)
467
+ minutes = 30;
468
+ minutes = Math.min(minutes, 1440); // cap at 24h
469
+ const sinceTs = Date.now() - minutes * 60_000;
470
+ const rows = readPerfSamples(stateDir, sinceTs); // guardrails-allow PREVENT-PI-004: local SQLite read (loopback dashboard)
471
+ const byKind = new Map();
472
+ for (const r of rows) {
473
+ let arr = byKind.get(r.kind);
474
+ if (!arr) {
475
+ arr = [];
476
+ byKind.set(r.kind, arr);
477
+ }
478
+ arr.push(r.value);
479
+ }
480
+ // Nearest-rank percentile (ceil(p/100*n)-1, clamped). Code-controlled,
481
+ // never user input (PREVENT-002 safe).
482
+ function pct(arr, p) {
483
+ if (!arr.length)
484
+ return 0;
485
+ const s = [...arr].sort((a, b) => a - b);
486
+ const idx = Math.min(s.length - 1, Math.max(0, Math.ceil((p / 100) * s.length) - 1));
487
+ return s[idx];
488
+ }
489
+ function avg(arr) {
490
+ if (!arr.length)
491
+ return 0;
492
+ return arr.reduce((a, b) => a + b, 0) / arr.length;
493
+ }
494
+ // rows are ASC by ts, so the last pushed value is the most recent.
495
+ function latest(arr) {
496
+ return arr.length ? arr[arr.length - 1] : 0;
497
+ }
498
+ const get = (k) => byKind.get(k) ?? [];
499
+ // diag counters live in the runtime-written dashboard.json (the server is
500
+ // a detached child with no MegaRuntime ref). Read defensively — absent
501
+ // until the first snapshot() write (PREVENT-001: assign before access).
502
+ let diag = null;
503
+ try {
504
+ const raw = readFileSync(snapshotPath, "utf-8");
505
+ const parsed = JSON.parse(raw);
506
+ if (parsed && typeof parsed === "object" && parsed.diag)
507
+ diag = parsed.diag;
508
+ }
509
+ catch { /* dashboard.json not written yet */ }
510
+ res.writeHead(200, { "Content-Type": "application/json" });
511
+ res.end(JSON.stringify({
512
+ updatedAt: new Date().toISOString(),
513
+ windowMinutes: minutes,
514
+ sampleCount: rows.length,
515
+ turn_latency_ms: { p50: pct(get("turn_latency_ms"), 50), p95: pct(get("turn_latency_ms"), 95), n: get("turn_latency_ms").length },
516
+ provider_latency_ms: { p50: pct(get("provider_latency_ms"), 50), p95: pct(get("provider_latency_ms"), 95), n: get("provider_latency_ms").length },
517
+ tps: { avg: avg(get("tps")), n: get("tps").length },
518
+ cache_hit_pct: { avg: avg(get("cache_hit_pct")), latest: latest(get("cache_hit_pct")), n: get("cache_hit_pct").length },
519
+ db_recompute_ms: { p50: pct(get("db_recompute_ms"), 50), p95: pct(get("db_recompute_ms"), 95), n: get("db_recompute_ms").length },
520
+ disk_write_ms: { p50: pct(get("disk_write_ms"), 50), p95: pct(get("disk_write_ms"), 95), n: get("disk_write_ms").length },
521
+ rss_mb: { latest: latest(get("rss_mb")), n: get("rss_mb").length },
522
+ heap_mb: { latest: latest(get("heap_mb")), n: get("heap_mb").length },
523
+ cpu_user_ms: { latest: latest(get("cpu_user_ms")), n: get("cpu_user_ms").length },
524
+ cpu_sys_ms: { latest: latest(get("cpu_sys_ms")), n: get("cpu_sys_ms").length },
525
+ diag,
526
+ }));
527
+ }
528
+ catch (e) {
529
+ res.writeHead(500, { "Content-Type": "application/json" });
530
+ res.end(JSON.stringify({ error: "perf_unavailable", detail: String(e) }));
531
+ }
532
+ return;
533
+ }
447
534
  // /api/achievements — S35 achievement tiles. GET returns the 9 seeded rows
448
535
  // {id,title,description,icon,hidden,unlocked_at}. The dashboard server is a
449
536
  // detached child with no MegaRuntime ref, so it reads game_achievements via
@@ -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
+ });
@@ -23,9 +23,18 @@ export class Dashboard {
23
23
  this.snapshotPath = join(stateDir, "dashboard.json");
24
24
  this.eventsPath = join(stateDir, "events.log");
25
25
  }
26
+ /** v0.8.8: duration (ms) of the last dashboard.json write — read by
27
+ * MegaRuntime.snapshot() to record a `disk_write_ms` perf sample without
28
+ * wrapping the giant snapshot object literal at the call site. */
29
+ _lastWriteMs = 0;
30
+ get lastWriteMs() {
31
+ return this._lastWriteMs;
32
+ }
26
33
  /** Write a full state snapshot (atomically replaces previous). */
27
34
  snapshot(data) {
35
+ const t = performance.now();
28
36
  writeFileSync(this.snapshotPath, JSON.stringify(data, null, 2) + "\n");
37
+ this._lastWriteMs = performance.now() - t;
29
38
  }
30
39
  /** Append a timestamped JSONL event line. */
31
40
  event(type, data) {
@@ -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,71 @@
1
+ import { recordPerfSample } from "../../src/store/sqlite.js";
2
+ /** Narrow a turn_end message to its usage block when it is an assistant msg. */
3
+ function usageOf(msg) {
4
+ if (msg.role !== "assistant" || !msg.usage)
5
+ return null;
6
+ return msg.usage;
7
+ }
8
+ /** Register perf instrumentation handlers + start the 5s cpu/mem interval. */
9
+ export function registerPerfHandler(pi, runtime) {
10
+ // turn_start: record the wall-clock start of the turn. Using Date.now() (not
11
+ // event.timestamp) so the turn_end duration is on ONE clock — mixing pi's
12
+ // timestamp with Date.now() would skew the delta. Also (re)arms the cpu/mem
13
+ // interval so a new session after a dispose() resumes sampling on its first
14
+ // turn (the interval is cleared in runtime.dispose()).
15
+ pi.on("turn_start", async () => {
16
+ try {
17
+ runtime.perfTurnStart = Date.now();
18
+ runtime.ensurePerfInterval();
19
+ }
20
+ catch {
21
+ /* non-fatal */
22
+ }
23
+ });
24
+ // turn_end: compute turn latency + TPS + cache hit % from the assistant
25
+ // message's usage block. One perf_samples row per metric per turn.
26
+ pi.on("turn_end", async (event) => {
27
+ try {
28
+ if (runtime.perfTurnStart > 0) {
29
+ const durMs = Date.now() - runtime.perfTurnStart;
30
+ recordPerfSample(runtime.currentStateDir, "turn_latency_ms", durMs, {
31
+ turnIndex: event.turnIndex,
32
+ });
33
+ const u = usageOf(event.message);
34
+ if (u) {
35
+ const durSec = Math.max(durMs / 1000, 0.001);
36
+ recordPerfSample(runtime.currentStateDir, "tps", u.output / durSec, { outputTokens: u.output });
37
+ const denom = u.cacheRead + u.input + u.cacheWrite;
38
+ const hitPct = denom > 0 ? (u.cacheRead / denom) * 100 : 0;
39
+ recordPerfSample(runtime.currentStateDir, "cache_hit_pct", hitPct, { input: u.input, cacheRead: u.cacheRead, cacheWrite: u.cacheWrite });
40
+ }
41
+ }
42
+ }
43
+ catch {
44
+ /* non-fatal: instrumentation must never break the agent loop */
45
+ }
46
+ });
47
+ // before_provider_request -> after_provider_response: raw round-trip latency
48
+ // to the model endpoint (HTTP status carried on the response event).
49
+ pi.on("before_provider_request", async () => {
50
+ try {
51
+ runtime.perfProviderStart = Date.now();
52
+ }
53
+ catch {
54
+ /* non-fatal */
55
+ }
56
+ });
57
+ pi.on("after_provider_response", async (event) => {
58
+ try {
59
+ if (runtime.perfProviderStart > 0) {
60
+ const lat = Date.now() - runtime.perfProviderStart;
61
+ recordPerfSample(runtime.currentStateDir, "provider_latency_ms", lat, { status: event.status });
62
+ }
63
+ }
64
+ catch {
65
+ /* non-fatal */
66
+ }
67
+ });
68
+ // Start the 5s cpu/mem sampling interval (one per MegaRuntime; cleared in
69
+ // runtime.dispose()). Idempotent — safe to call again after a dispose().
70
+ runtime.ensurePerfInterval();
71
+ }
@@ -2,6 +2,7 @@ import { registerSessionHandlers } from "./session-handlers.js";
2
2
  import { registerAgentHandlers } from "./agent-handlers.js";
3
3
  import { registerContextHandler } from "./context-handler.js";
4
4
  import { registerCompactHandlers } from "./compact-handlers.js";
5
+ import { registerPerfHandler } from "./perf-handler.js";
5
6
  /**
6
7
  * DIAG accessor for the headless test harness: the most recently constructed
7
8
  * MegaRuntime, so a test that loads the compiled extension via its default
@@ -18,4 +19,5 @@ export function registerEventHandlers(pi, runtime, config) {
18
19
  registerAgentHandlers(pi, runtime, config);
19
20
  registerContextHandler(pi, runtime, config);
20
21
  registerCompactHandlers(pi, runtime, config);
22
+ registerPerfHandler(pi, runtime);
21
23
  }
@@ -15,3 +15,4 @@ export * from "./mega-events/session-handlers.js";
15
15
  export * from "./mega-events/agent-handlers.js";
16
16
  export * from "./mega-events/context-handler.js";
17
17
  export * from "./mega-events/compact-handlers.js";
18
+ export * from "./mega-events/perf-handler.js";