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
@@ -15,7 +15,7 @@ import { VectorStore } from "../../src/vectorStore.js";
15
15
  import { toEngineMessages } from "../../src/adapt.js";
16
16
  import { normalizeSessionId } from "../../src/store.js";
17
17
  import { Logger } from "../../src/log.js";
18
- import { recordModelSnapshot, latestModelSnapshot, upsertRepoRegistry, recordRepoModel, getDedupStats, getCompactCount, getRecallInjected, getCacheHitTokensSaved, getGameState, } from "../../src/store/sqlite.js";
18
+ import { recordModelSnapshot, latestModelSnapshot, upsertRepoRegistry, recordRepoModel, getDedupStats, getCompactCount, getRecallInjected, getCacheHitTokensSaved, getGameState, recordPerfSample, } from "../../src/store/sqlite.js";
19
19
  import { detectCrossRepoDrift } from "../../src/driftDetection.js";
20
20
  import { repoStateDir, resolveRepoRoot, pressureRatio, pressureFromPct, pressureBand, effectiveThresholdTokens, } from "../mega-config.js";
21
21
  import { Dashboard } from "../mega-dashboard.js";
@@ -121,6 +121,12 @@ export class MegaRuntime {
121
121
  // Last explain-why line (dedup reason / anchor-kept / superseded), surfaced
122
122
  // while fresh.
123
123
  lastWhy = undefined;
124
+ // v0.8.8 Perf dashboard instrumentation: turn/provider start timestamps +
125
+ // the 5s cpu/mem interval handle (one per MegaRuntime, cleared in dispose()).
126
+ perfTurnStart = 0;
127
+ perfProviderStart = 0;
128
+ perfCpuInterval;
129
+ perfCpuBaseline;
124
130
  // Context tracking for the dashboard (updated in the context handler).
125
131
  lastCtxTokens = null;
126
132
  lastCtxPercent = null;
@@ -329,6 +335,7 @@ export class MegaRuntime {
329
335
  this.renderWidget(ctx);
330
336
  return;
331
337
  }
338
+ const perfT0 = performance.now();
332
339
  const st = this.store.stats(this.rt.sessionId);
333
340
  const repo = this.store.repoStats();
334
341
  const di = this.store.dataInvariant();
@@ -479,7 +486,13 @@ export class MegaRuntime {
479
486
  cacheHit: { sessionSec: sec(this.rt.cacheHitTokens), totalSec: sec(cacheHitsTotalTokens) },
480
487
  },
481
488
  model,
489
+ diag: {
490
+ ctxFastGate: this.diagCtxFastGate,
491
+ liveTrimFires: this.diagLiveTrimFires,
492
+ liveTrimReplays: this.diagLiveTrimReplays,
493
+ },
482
494
  });
495
+ const perfDiskMs = this.dashboard.lastWriteMs;
483
496
  // Live stats widget above the editor
484
497
  if (ctx) {
485
498
  // ── gather widget data (computed per snapshot, rendered per frame) ────
@@ -634,6 +647,13 @@ export class MegaRuntime {
634
647
  }
635
648
  // v0.8.5: record the material-change signature computed at the top so the
636
649
  // next snapshot() can skip this whole body when nothing material changed.
650
+ try {
651
+ recordPerfSample(this.currentStateDir, "db_recompute_ms", performance.now() - perfT0);
652
+ recordPerfSample(this.currentStateDir, "disk_write_ms", perfDiskMs);
653
+ }
654
+ catch {
655
+ /* non-fatal: perf instrumentation never blocks the agent */
656
+ }
637
657
  this.lastSnapshotSig = sig;
638
658
  }
639
659
  /** Register the above-editor widget as a width-aware factory so pi re-renders
@@ -892,6 +912,44 @@ export class MegaRuntime {
892
912
  this.gameStateWatcher = undefined;
893
913
  this.gameStateWatchDir = undefined;
894
914
  }
915
+ // v0.8.8: stop the cpu/mem sampling interval on teardown. Re-armed lazily
916
+ // by ensurePerfInterval() on the next turn_start.
917
+ if (this.perfCpuInterval) {
918
+ clearInterval(this.perfCpuInterval);
919
+ this.perfCpuInterval = undefined;
920
+ this.perfCpuBaseline = undefined;
921
+ }
922
+ }
923
+ /** v0.8.8: (re)start the 5s cpu/mem sampling interval (idempotent). One per
924
+ * MegaRuntime; cleared in dispose(). Samples process.cpuUsage() (user/sys
925
+ * delta vs the last tick → ms) + process.memoryUsage() (rss/heap → MB) and
926
+ * records them as perf_samples. unref'd so it never keeps the process alive
927
+ * on its own. Non-fatal: any failure is swallowed (instrumentation never
928
+ * blocks the agent). PREVENT-PI-004: local process stats + SQLite only. */
929
+ ensurePerfInterval() {
930
+ if (this.perfCpuInterval)
931
+ return;
932
+ this.perfCpuBaseline = undefined; // first tick sets the baseline (no delta)
933
+ this.perfCpuInterval = setInterval(() => {
934
+ try {
935
+ const dir = this.currentStateDir;
936
+ const cpu = process.cpuUsage();
937
+ const mem = process.memoryUsage();
938
+ if (this.perfCpuBaseline) {
939
+ const du = (cpu.user - this.perfCpuBaseline.user) / 1000; // μs → ms
940
+ const ds = (cpu.system - this.perfCpuBaseline.sys) / 1000;
941
+ recordPerfSample(dir, "cpu_user_ms", Math.max(0, du));
942
+ recordPerfSample(dir, "cpu_sys_ms", Math.max(0, ds));
943
+ }
944
+ this.perfCpuBaseline = { user: cpu.user, sys: cpu.system };
945
+ recordPerfSample(dir, "rss_mb", mem.rss / 1_000_000);
946
+ recordPerfSample(dir, "heap_mb", mem.heapUsed / 1_000_000);
947
+ }
948
+ catch {
949
+ /* non-fatal */
950
+ }
951
+ }, 5000);
952
+ this.perfCpuInterval.unref?.();
895
953
  }
896
954
  /** S31: the cached game-mode state (game_mode_on/theme/tui_display_mode).
897
955
  * Lazily read from the game_state SQLite row on the first call, then
@@ -0,0 +1,81 @@
1
+ /**
2
+ * perf-samples.ts — `perf_samples` table accessors (v0.8.8 Perf dashboard).
3
+ *
4
+ * Append-only local instrumentation store for the dashboard's Perf tab: model
5
+ * endpoint latency, TPS, cache hit %, CPU/mem, and the snapshot() recompute /
6
+ * disk-write cost. One row per sample; the dashboard server reads a rolling
7
+ * window and derives p50/p95 + latest values.
8
+ *
9
+ * PREVENT-PI-004: local SQLite only, zero network.
10
+ * PREVENT-002: all SQL parameterized (? placeholders). The optional `kind`
11
+ * filter is bound as a parameter (never string-concatenated); the only
12
+ * interpolated fragment is the code-controlled `AND kind = ?` clause toggle,
13
+ * never external input.
14
+ * Pi-agnostic: no pi runtime types (mirrors game-scores.ts / meta.ts).
15
+ */
16
+ import { getStateDir } from "../../store.js";
17
+ import { openStore } from "./utils.js";
18
+ /** Allow-list of valid perf sample kinds (mirrors the table's domain). */
19
+ export const PERF_KINDS = [
20
+ "turn_latency_ms",
21
+ "provider_latency_ms",
22
+ "tps",
23
+ "cache_hit_pct",
24
+ "rss_mb",
25
+ "heap_mb",
26
+ "cpu_user_ms",
27
+ "cpu_sys_ms",
28
+ "db_recompute_ms",
29
+ "disk_write_ms",
30
+ ];
31
+ function isPerfKind(k) {
32
+ return PERF_KINDS.includes(k);
33
+ }
34
+ /**
35
+ * Record one perf sample. `ts` is set to Date.now(). SQL is fully parameterized
36
+ * (PREVENT-002); the kind is validated against the fixed allow-list. Pi-agnostic.
37
+ * Never throws on an unknown kind or non-finite value (silently ignored) so
38
+ * instrumentation can never block the agent; a known kind + finite value always
39
+ * writes.
40
+ */
41
+ export function recordPerfSample(stateDir = getStateDir(), kind, value, meta) {
42
+ if (!isPerfKind(kind))
43
+ return;
44
+ if (!Number.isFinite(value))
45
+ return;
46
+ const db = openStore(stateDir);
47
+ db.prepare(`INSERT INTO perf_samples (ts, kind, value, meta)
48
+ VALUES (?, ?, ?, ?)`).run(Date.now(), kind, value, meta != null ? JSON.stringify(meta) : null);
49
+ }
50
+ /**
51
+ * Read perf samples since `sinceTs` (epoch ms), optionally filtered by kind.
52
+ * Returns rows ascending by ts. The optional kind filter is bound as a
53
+ * parameter (PREVENT-002). Pi-agnostic. `meta` is parsed defensively (null-safe:
54
+ * PREVENT-001 — assigned to a variable before any property access).
55
+ */
56
+ export function readPerfSamples(stateDir = getStateDir(), sinceTs = 0, kind) {
57
+ const db = openStore(stateDir);
58
+ const sql = kind
59
+ ? `SELECT id, ts, kind, value, meta FROM perf_samples
60
+ WHERE ts >= ? AND kind = ? ORDER BY ts ASC`
61
+ : `SELECT id, ts, kind, value, meta FROM perf_samples
62
+ WHERE ts >= ? ORDER BY ts ASC`;
63
+ const params = kind ? [sinceTs, kind] : [sinceTs];
64
+ const rows = db.prepare(sql).all(...params);
65
+ const out = [];
66
+ for (const r of rows) {
67
+ if (!isPerfKind(r.kind))
68
+ continue; // defensive: unknown kind row skipped
69
+ let meta = null;
70
+ if (r.meta != null) {
71
+ try {
72
+ meta = JSON.parse(r.meta);
73
+ }
74
+ catch {
75
+ meta = null;
76
+ }
77
+ }
78
+ out.push({ id: r.id, ts: r.ts, kind: r.kind, value: r.value, meta });
79
+ }
80
+ return out;
81
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * perf-samples.test.ts — v0.8.8 perf_samples table round-trip + filtering.
3
+ * Pi-agnostic. Uses an isolated state dir (never the real user dir — G7).
4
+ */
5
+ import { describe, it, before, after } from "node:test";
6
+ import assert from "node:assert/strict";
7
+ import { tmpdir } from "node:os";
8
+ import { join } from "node:path";
9
+ import { mkdtempSync, rmSync } from "node:fs";
10
+ import { closeStore } from "./utils.js";
11
+ import { recordPerfSample, readPerfSamples, PERF_KINDS, } from "./perf-samples.js";
12
+ describe("perf-samples (v0.8.8)", () => {
13
+ let dir;
14
+ before(() => {
15
+ dir = mkdtempSync(join(tmpdir(), "mc-perfsamples-"));
16
+ process.env.MEGACOMPACT_STATE_DIR = dir;
17
+ });
18
+ after(() => {
19
+ closeStore(dir);
20
+ delete process.env.MEGACOMPACT_STATE_DIR;
21
+ rmSync(dir, { recursive: true, force: true });
22
+ });
23
+ it("records + reads back a turn_latency_ms sample with parsed meta", () => {
24
+ recordPerfSample(dir, "turn_latency_ms", 123.4, { turnIndex: 2 });
25
+ const rows = readPerfSamples(dir, 0);
26
+ assert.equal(rows.length, 1);
27
+ assert.equal(rows[0].kind, "turn_latency_ms");
28
+ assert.equal(rows[0].value, 123.4);
29
+ assert.deepEqual(rows[0].meta, { turnIndex: 2 });
30
+ });
31
+ it("filters by kind and by sinceTs", () => {
32
+ recordPerfSample(dir, "tps", 50);
33
+ recordPerfSample(dir, "rss_mb", 256);
34
+ const tps = readPerfSamples(dir, 0, "tps");
35
+ assert.equal(tps.length, 1);
36
+ assert.equal(tps[0].kind, "tps");
37
+ assert.equal(tps[0].value, 50);
38
+ const future = readPerfSamples(dir, Date.now() + 10000, "tps");
39
+ assert.equal(future.length, 0);
40
+ });
41
+ it("ignores non-finite values + unknown kinds (never throws, nothing added)", () => {
42
+ const before = readPerfSamples(dir, 0).length;
43
+ recordPerfSample(dir, "tps", Number.NaN);
44
+ recordPerfSample(dir, "tps", Infinity);
45
+ assert.doesNotThrow(() => recordPerfSample(dir, "bogus", 1));
46
+ const after = readPerfSamples(dir, 0).length;
47
+ assert.equal(after, before);
48
+ });
49
+ it("PERF_KINDS lists the 10 instrumentation kinds", () => {
50
+ assert.equal(PERF_KINDS.length, 10);
51
+ assert.ok(PERF_KINDS.includes("db_recompute_ms"));
52
+ assert.ok(PERF_KINDS.includes("cache_hit_pct"));
53
+ });
54
+ });
@@ -261,6 +261,20 @@ export function initSchema(db) {
261
261
  icon TEXT,
262
262
  unlocked_at INTEGER NULL
263
263
  ) WITHOUT ROWID;
264
+
265
+ -- v0.8.8 Perf dashboard: append-only local instrumentation samples (one row
266
+ -- per turn / provider round-trip / 5s cpu-mem tick / snapshot-recompute).
267
+ -- Drives the dashboard Perf tab. Local SQLite (PREVENT-PI-004); parameterized
268
+ -- accessors in perf-samples.ts (PREVENT-002).
269
+ CREATE TABLE IF NOT EXISTS perf_samples (
270
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
271
+ ts INTEGER NOT NULL,
272
+ kind TEXT NOT NULL,
273
+ value REAL NOT NULL,
274
+ meta TEXT
275
+ );
276
+ CREATE INDEX IF NOT EXISTS idx_perf_samples_ts ON perf_samples(ts);
277
+ CREATE INDEX IF NOT EXISTS idx_perf_samples_kind_ts ON perf_samples(kind, ts);
264
278
  `);
265
279
  // Idempotent column migrations. `CREATE TABLE IF NOT EXISTS` is a no-op on a
266
280
  // pre-existing table, so new columns added to context_chunks after a store was
@@ -23,3 +23,4 @@ export * from "./sqlite/maintenance.js";
23
23
  export * from "./sqlite/game-state.js";
24
24
  export * from "./sqlite/game-scores.js";
25
25
  export * from "./sqlite/game-achievements.js";
26
+ export * from "./sqlite/perf-samples.js";
@@ -189,6 +189,7 @@ export function dashboardHtml(tierName: string): string {
189
189
  <button class="tab" data-tab="active">Active Repos</button>
190
190
  <button class="tab" data-tab="summary">Summary</button>
191
191
  <button class="tab" data-tab="game">Game Mode</button>
192
+ <button class="tab" data-tab="perf">Perf</button>
192
193
  </nav>
193
194
 
194
195
  <!-- Current repo (existing single-repo view) -->
@@ -443,6 +444,18 @@ export function dashboardHtml(tierName: string): string {
443
444
  <div id="game-empty">No scores yet — run a session with game mode on.</div>
444
445
  </div>
445
446
 
447
+ <!-- Perf (v0.8.8) — live local instrumentation -->
448
+ <div class="tab-panel" id="panel-perf">
449
+ <div class="grid">
450
+ <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>
451
+ <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>
452
+ <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>
453
+ <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>
454
+ <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>
455
+ </div>
456
+ <div class="updated" id="perf-updated">waiting for data</div>
457
+ </div>
458
+
446
459
  <script>
447
460
  (function() {
448
461
  var evBox = document.getElementById('events');
@@ -1000,9 +1013,40 @@ export function dashboardHtml(tierName: string): string {
1000
1013
  }).catch(function() {});
1001
1014
  }
1002
1015
 
1016
+ // --- Perf tab (v0.8.8) — live local instrumentation ----------------------
1017
+ var perfPollTimer = null;
1018
+ function pollPerf() {
1019
+ 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)
1020
+ if (!d) return;
1021
+ var el = document.getElementById('perf-updated');
1022
+ if (el) el.textContent = d.sampleCount + ' samples \u00b7 updated ' + (d.updatedAt || '');
1023
+ function setText(id, txt) { var e = document.getElementById(id); if (e) e.textContent = txt; }
1024
+ function fmtMs(v) { return v == null ? '\u2014' : (v >= 100 ? Math.round(v) + 'ms' : v.toFixed(1) + 'ms'); }
1025
+ function fmtNum(v, dec) { return v == null ? '\u2014' : (typeof v === 'number' ? v.toFixed(dec) : '\u2014'); }
1026
+ setText('pf-turn-p50', fmtMs(d.turn_latency_ms && d.turn_latency_ms.p50));
1027
+ setText('pf-turn-p95', fmtMs(d.turn_latency_ms && d.turn_latency_ms.p95));
1028
+ setText('pf-prov-p50', fmtMs(d.provider_latency_ms && d.provider_latency_ms.p50));
1029
+ setText('pf-prov-p95', fmtMs(d.provider_latency_ms && d.provider_latency_ms.p95));
1030
+ setText('pf-tps', fmtNum(d.tps && d.tps.avg, 1));
1031
+ setText('pf-cache', (d.cache_hit_pct && typeof d.cache_hit_pct.avg === 'number') ? fmtNum(d.cache_hit_pct.avg, 1) + '%' : '\u2014');
1032
+ setText('pf-rss', (d.rss_mb && typeof d.rss_mb.latest === 'number') ? fmtNum(d.rss_mb.latest, 1) + ' MB' : '\u2014');
1033
+ setText('pf-heap', (d.heap_mb && typeof d.heap_mb.latest === 'number') ? fmtNum(d.heap_mb.latest, 1) + ' MB' : '\u2014');
1034
+ 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');
1035
+ setText('pf-db-p50', fmtMs(d.db_recompute_ms && d.db_recompute_ms.p50));
1036
+ setText('pf-db-p95', fmtMs(d.db_recompute_ms && d.db_recompute_ms.p95));
1037
+ setText('pf-disk', fmtMs(d.disk_write_ms && d.disk_write_ms.p50));
1038
+ var diag = d.diag || {};
1039
+ setText('pf-recompute', diag.liveTrimFires != null ? String(diag.liveTrimFires) : '\u2014');
1040
+ setText('pf-replays', diag.liveTrimReplays != null ? String(diag.liveTrimReplays) : '\u2014');
1041
+ setText('pf-skips', diag.ctxFastGate != null ? String(diag.ctxFastGate) : '\u2014');
1042
+ }).catch(function() {});
1043
+ }
1044
+ function startPerfPoll() { if (perfPollTimer) return; pollPerf(); perfPollTimer = setInterval(pollPerf, 2000); }
1045
+ function stopPerfPoll() { if (perfPollTimer) { clearInterval(perfPollTimer); perfPollTimer = null; } }
1046
+
1003
1047
  // --- Tab switching ------------------------------------------------------
1004
1048
  var tabs = document.querySelectorAll('.tab');
1005
- var panels = { current: 'panel-current', all: 'panel-all', active: 'panel-active', summary: 'panel-summary', game: 'panel-game' };
1049
+ var panels = { current: 'panel-current', all: 'panel-all', active: 'panel-active', summary: 'panel-summary', game: 'panel-game', perf: 'panel-perf' };
1006
1050
  for (var i = 0; i < tabs.length; i++) {
1007
1051
  tabs[i].addEventListener('click', function() {
1008
1052
  var name = this.getAttribute('data-tab');
@@ -1017,6 +1061,7 @@ export function dashboardHtml(tierName: string): string {
1017
1061
  if (name === 'all' || name === 'summary') pollIndex();
1018
1062
  if (name === 'active') pollServers();
1019
1063
  if (name === 'game') { renderGameScores(); renderAchievements(); }
1064
+ if (name === 'perf') startPerfPoll(); else stopPerfPoll();
1020
1065
  });
1021
1066
  }
1022
1067
  })();
@@ -0,0 +1,101 @@
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
+
14
+ const SERVER_ENTRY = new URL("./server.js", import.meta.url).pathname;
15
+
16
+ function waitFor(cond: () => boolean | Promise<boolean>, timeoutMs = 6000): Promise<void> {
17
+ const start = Date.now();
18
+ return new Promise((resolve, reject) => {
19
+ const tick = async () => {
20
+ if (await cond()) return resolve();
21
+ if (Date.now() - start > timeoutMs) return reject(new Error("timeout"));
22
+ setTimeout(tick, 50);
23
+ };
24
+ tick();
25
+ });
26
+ }
27
+
28
+ function freshDir(prefix: string): string {
29
+ return mkdtempSync(join(tmpdir(), prefix));
30
+ }
31
+
32
+ async function withServer<T>(
33
+ port: string,
34
+ dir: string,
35
+ fn: (port: number) => Promise<T>,
36
+ ): Promise<T> {
37
+ process.env.MEGACOMPACT_DASHBOARD_PORT = port;
38
+ const child = spawn(process.execPath, [SERVER_ENTRY, dir], { stdio: "ignore" });
39
+ try {
40
+ await waitFor(async () => {
41
+ try {
42
+ const raw = JSON.parse(readFileSync(join(dir, "port.pid"), "utf-8"));
43
+ const res = await fetch(`http://localhost:${raw.port}/api/version`);
44
+ return res.ok;
45
+ } catch {
46
+ return false;
47
+ }
48
+ });
49
+ const raw = JSON.parse(readFileSync(join(dir, "port.pid"), "utf-8"));
50
+ return await fn(raw.port);
51
+ } finally {
52
+ child.kill("SIGTERM");
53
+ delete process.env.MEGACOMPACT_DASHBOARD_PORT;
54
+ rmSync(dir, { recursive: true, force: true });
55
+ }
56
+ }
57
+
58
+ interface PerfResp {
59
+ sampleCount: number;
60
+ turn_latency_ms: { p50: number; p95: number; n: number };
61
+ provider_latency_ms: { p50: number; p95: number; n: number };
62
+ tps: { avg: number; n: number };
63
+ cache_hit_pct: { avg: number; latest: number; n: number };
64
+ db_recompute_ms: { p50: number; p95: number; n: number };
65
+ disk_write_ms: { p50: number; p95: number; n: number };
66
+ rss_mb: { latest: number; n: number };
67
+ heap_mb: { latest: number; n: number };
68
+ cpu_user_ms: { latest: number; n: number };
69
+ cpu_sys_ms: { latest: number; n: number };
70
+ diag: { ctxFastGate: number; liveTrimFires: number; liveTrimReplays: number } | null;
71
+ }
72
+
73
+ describe("v0.8.8 /api/perf", () => {
74
+ test("GET returns aggregates over recorded perf_samples", async () => {
75
+ const dir = freshDir("dash-perf-agg-");
76
+ recordPerfSample(dir, "turn_latency_ms", 100, { turnIndex: 1 });
77
+ recordPerfSample(dir, "turn_latency_ms", 200);
78
+ recordPerfSample(dir, "tps", 50);
79
+ recordPerfSample(dir, "rss_mb", 256);
80
+ await withServer("19440", dir, async (port) => {
81
+ const res = await fetch(`http://localhost:${port}/api/perf?minutes=30`);
82
+ assert.equal(res.status, 200);
83
+ const d = (await res.json()) as PerfResp;
84
+ assert.equal(d.sampleCount, 4);
85
+ assert.equal(d.turn_latency_ms.n, 2);
86
+ assert.equal(d.turn_latency_ms.p50, 100); // nearest-rank p50 of [100,200]
87
+ assert.equal(d.turn_latency_ms.p95, 200); // nearest-rank p95 of [100,200]
88
+ assert.equal(d.tps.avg, 50);
89
+ assert.equal(d.rss_mb.latest, 256);
90
+ assert.equal(d.diag, null); // no runtime wrote dashboard.json in this dir
91
+ });
92
+ });
93
+
94
+ test("non-GET (POST) -> 405", async () => {
95
+ const dir = freshDir("dash-perf-meth-");
96
+ await withServer("19441", dir, async (port) => {
97
+ const res = await fetch(`http://localhost:${port}/api/perf`, { method: "POST" });
98
+ assert.equal(res.status, 405);
99
+ });
100
+ });
101
+ });
@@ -415,6 +415,85 @@ export async function launchDashboardServer(stateDir: string): Promise<{ port: n
415
415
  return;
416
416
  }
417
417
 
418
+ // /api/perf — v0.8.8 Perf dashboard tab. GET returns rolling-window
419
+ // aggregates over perf_samples: per-kind p50/p95 (turn/provider latency,
420
+ // tps avg, db recompute, disk write), latest rss/heap, cpu user/sys delta,
421
+ // cache hit %, plus the diag recompute/skip/replay counts (read from
422
+ // dashboard.json snapshot if available). The dashboard server is a detached
423
+ // child with no MegaRuntime ref, so it reads perf_samples via a require()'d
424
+ // sqlite helper (same pattern as /api/game-scores). Unknown/invalid params
425
+ // are clamped (never throw). Non-GET -> 405. PREVENT-PI-004: loopback.
426
+ if (req.url?.startsWith("/api/perf")) {
427
+ const pfReq = createRequire(import.meta.url);
428
+ const { readPerfSamples } = pfReq("../../src/store/sqlite.js") as typeof import("../../src/store/sqlite.js");
429
+ if (req.method !== "GET") {
430
+ res.writeHead(405, { "Content-Type": "application/json" }); // guardrails-allow PREVENT-PI-004: loopback dashboard response (local)
431
+ res.end(JSON.stringify({ error: "method_not_allowed" }));
432
+ return;
433
+ }
434
+ try {
435
+ const url = new URL(req.url, "http://x"); // guardrails-allow PREVENT-PI-004: localhost dashboard URL base (loopback-only)
436
+ let minutes = Number(url.searchParams.get("minutes") ?? "30");
437
+ if (!Number.isFinite(minutes) || minutes <= 0) minutes = 30;
438
+ minutes = Math.min(minutes, 1440); // cap at 24h
439
+ const sinceTs = Date.now() - minutes * 60_000;
440
+ const rows = readPerfSamples(stateDir, sinceTs); // guardrails-allow PREVENT-PI-004: local SQLite read (loopback dashboard)
441
+ const byKind = new Map<string, number[]>();
442
+ for (const r of rows) {
443
+ let arr = byKind.get(r.kind);
444
+ if (!arr) { arr = []; byKind.set(r.kind, arr); }
445
+ arr.push(r.value);
446
+ }
447
+ // Nearest-rank percentile (ceil(p/100*n)-1, clamped). Code-controlled,
448
+ // never user input (PREVENT-002 safe).
449
+ function pct(arr: number[], p: number): number {
450
+ if (!arr.length) return 0;
451
+ const s = [...arr].sort((a, b) => a - b);
452
+ const idx = Math.min(s.length - 1, Math.max(0, Math.ceil((p / 100) * s.length) - 1));
453
+ return s[idx];
454
+ }
455
+ function avg(arr: number[]): number {
456
+ if (!arr.length) return 0;
457
+ return arr.reduce((a, b) => a + b, 0) / arr.length;
458
+ }
459
+ // rows are ASC by ts, so the last pushed value is the most recent.
460
+ function latest(arr: number[]): number {
461
+ return arr.length ? arr[arr.length - 1] : 0;
462
+ }
463
+ const get = (k: string): number[] => byKind.get(k) ?? [];
464
+ // diag counters live in the runtime-written dashboard.json (the server is
465
+ // a detached child with no MegaRuntime ref). Read defensively — absent
466
+ // until the first snapshot() write (PREVENT-001: assign before access).
467
+ let diag: { ctxFastGate: number; liveTrimFires: number; liveTrimReplays: number } | null = null;
468
+ try {
469
+ const raw = readFileSync(snapshotPath, "utf-8");
470
+ const parsed = JSON.parse(raw) as { diag?: { ctxFastGate: number; liveTrimFires: number; liveTrimReplays: number } };
471
+ if (parsed && typeof parsed === "object" && parsed.diag) diag = parsed.diag;
472
+ } catch { /* dashboard.json not written yet */ }
473
+ res.writeHead(200, { "Content-Type": "application/json" });
474
+ res.end(JSON.stringify({
475
+ updatedAt: new Date().toISOString(),
476
+ windowMinutes: minutes,
477
+ sampleCount: rows.length,
478
+ turn_latency_ms: { p50: pct(get("turn_latency_ms"), 50), p95: pct(get("turn_latency_ms"), 95), n: get("turn_latency_ms").length },
479
+ provider_latency_ms: { p50: pct(get("provider_latency_ms"), 50), p95: pct(get("provider_latency_ms"), 95), n: get("provider_latency_ms").length },
480
+ tps: { avg: avg(get("tps")), n: get("tps").length },
481
+ cache_hit_pct: { avg: avg(get("cache_hit_pct")), latest: latest(get("cache_hit_pct")), n: get("cache_hit_pct").length },
482
+ db_recompute_ms: { p50: pct(get("db_recompute_ms"), 50), p95: pct(get("db_recompute_ms"), 95), n: get("db_recompute_ms").length },
483
+ disk_write_ms: { p50: pct(get("disk_write_ms"), 50), p95: pct(get("disk_write_ms"), 95), n: get("disk_write_ms").length },
484
+ rss_mb: { latest: latest(get("rss_mb")), n: get("rss_mb").length },
485
+ heap_mb: { latest: latest(get("heap_mb")), n: get("heap_mb").length },
486
+ cpu_user_ms: { latest: latest(get("cpu_user_ms")), n: get("cpu_user_ms").length },
487
+ cpu_sys_ms: { latest: latest(get("cpu_sys_ms")), n: get("cpu_sys_ms").length },
488
+ diag,
489
+ }));
490
+ } catch (e) {
491
+ res.writeHead(500, { "Content-Type": "application/json" });
492
+ res.end(JSON.stringify({ error: "perf_unavailable", detail: String(e) }));
493
+ }
494
+ return;
495
+ }
496
+
418
497
  // /api/achievements — S35 achievement tiles. GET returns the 9 seeded rows
419
498
  // {id,title,description,icon,hidden,unlocked_at}. The dashboard server is a
420
499
  // detached child with no MegaRuntime ref, so it reads game_achievements via