pi-mega-compact 0.8.7 → 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.
- package/dist/extensions/dashboard-server/html.js +46 -1
- package/dist/extensions/dashboard-server/perf-server.test.js +80 -0
- package/dist/extensions/dashboard-server/server.js +87 -0
- package/dist/extensions/mega-dashboard.js +9 -0
- package/dist/extensions/mega-events/perf-handler.js +71 -0
- package/dist/extensions/mega-events/register.js +2 -0
- package/dist/extensions/mega-events.js +1 -0
- package/dist/extensions/mega-runtime/state.js +59 -1
- package/dist/src/store/sqlite/perf-samples.js +81 -0
- package/dist/src/store/sqlite/perf-samples.test.js +54 -0
- package/dist/src/store/sqlite/schema.js +14 -0
- package/dist/src/store/sqlite.js +1 -0
- package/extensions/dashboard-server/html.ts +46 -1
- package/extensions/dashboard-server/perf-server.test.ts +101 -0
- package/extensions/dashboard-server/server.ts +79 -0
- package/extensions/mega-dashboard.ts +17 -0
- package/extensions/mega-events/perf-handler.ts +113 -0
- package/extensions/mega-events/register.ts +2 -0
- package/extensions/mega-events.ts +1 -0
- package/extensions/mega-runtime/state.ts +57 -0
- package/package.json +1 -1
- package/src/store/sqlite/perf-samples.test.ts +65 -0
- package/src/store/sqlite/perf-samples.ts +125 -0
- package/src/store/sqlite/schema.ts +14 -0
- 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
|
|
@@ -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) {
|
|
@@ -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,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
|
package/dist/src/store/sqlite.js
CHANGED