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.
- 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-cache-replay.test.js +189 -0
- package/dist/extensions/mega-dashboard.js +9 -0
- package/dist/extensions/mega-events/context-handler.js +17 -2
- 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-cache-replay.test.ts +211 -0
- package/extensions/mega-dashboard.ts +17 -0
- package/extensions/mega-events/context-handler.ts +18 -2
- 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
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mega-cache-replay.test.ts — locks the v0.8.7 cache-stability fix.
|
|
3
|
+
*
|
|
4
|
+
* Two tests (reuses the mega-teamrun.test.ts harness shape: mock pi + the REAL
|
|
5
|
+
* compiled extension at extensions/mega-compact.js):
|
|
6
|
+
* a. REPLAY: drive >=2 gated context events past the debounce within ONE epoch
|
|
7
|
+
* (same lastCheckpointId) and assert diagLiveTrimReplays > 0 AND the returned
|
|
8
|
+
* messages array is byte-identical (deepEqual) across replays (stable prefix).
|
|
9
|
+
* b. DEDUP-ON-DIFFERENT-CHECKPOINT: after a fresh trim, simulate a re-compact
|
|
10
|
+
* (context grew on the token basis) that DEDUPS onto a DIFFERENT existing
|
|
11
|
+
* checkpoint id (L0 contentHash match against an OLDER checkpoint, so
|
|
12
|
+
* result.checkpointId != rt.lastCheckpointId), then assert the NEXT gated
|
|
13
|
+
* event STILL replays (diagLiveTrimReplays increments) — i.e. the cache key
|
|
14
|
+
* trimCache.checkpointId === rt.lastCheckpointId holds. This is the P2 gap the
|
|
15
|
+
* v0.8.6 audit found: keying on the dedup-volatile result.checkpointId
|
|
16
|
+
* disabled replay for the rest of the epoch after such a dedup fire.
|
|
17
|
+
*
|
|
18
|
+
* MEGACOMPACT_PGLITE_DISABLED keeps the run fast (no WASM index init).
|
|
19
|
+
*/
|
|
20
|
+
import { test } from "node:test";
|
|
21
|
+
import assert from "node:assert/strict";
|
|
22
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
23
|
+
import { tmpdir } from "node:os";
|
|
24
|
+
import { join } from "node:path";
|
|
25
|
+
import { createRequire } from "node:module";
|
|
26
|
+
import { closeVectorIndex } from "../src/store/vectorIndex.js";
|
|
27
|
+
import type { AgentMessage } from "@earendil-works/pi-agent-core";
|
|
28
|
+
|
|
29
|
+
const require = createRequire(import.meta.url);
|
|
30
|
+
const baseTmp = mkdtempSync(join(tmpdir(), "mc-cache-"));
|
|
31
|
+
process.env.MEGACOMPACT_INDEX_DIR = join(baseTmp, "index");
|
|
32
|
+
process.env.MEGACOMPACT_PGLITE_DISABLED = "true"; // fast: skip WASM index
|
|
33
|
+
let counter = 0;
|
|
34
|
+
|
|
35
|
+
function harness() {
|
|
36
|
+
const stateDir = join(baseTmp, `run-${counter++}`);
|
|
37
|
+
process.env.MEGACOMPACT_STATE_DIR = stateDir;
|
|
38
|
+
process.env.MEGACOMPACT_DEBUG = "true";
|
|
39
|
+
process.env.MEGACOMPACT_THRESHOLD_TOKENS = "50";
|
|
40
|
+
process.env.MEGACOMPACT_FAST_GATE_PCT = "1";
|
|
41
|
+
process.env.MEGACOMPACT_ANCHOR_USER_MESSAGES = "1";
|
|
42
|
+
process.env.MEGACOMPACT_DURABLE_TRIM_FLOOR = "0";
|
|
43
|
+
process.env.MEGACOMPACT_MEMORY_AUTO_REVIEW = "false";
|
|
44
|
+
process.env.MEGACOMPACT_RAPTOR_ENABLED = "false";
|
|
45
|
+
// Disable the FUZZY dedup tiers (L1 MinHash/LSH + L2 cosine) so the DEDUP test
|
|
46
|
+
// is controlled by L0 contentHash only: setB (different vocabulary) then
|
|
47
|
+
// creates a genuinely NEW checkpoint instead of fuzzy-matching setA's, and
|
|
48
|
+
// setA-re-again still L0-contentHash-dedups onto the first setA checkpoint.
|
|
49
|
+
// The TrigramEmbedder otherwise matches on shared structural trigrams
|
|
50
|
+
// ("— step N" / "Edit"), collapsing setB onto setA. Harmless for the REPLAY
|
|
51
|
+
// test (pure replay, no dedup reliance).
|
|
52
|
+
process.env.MEGACOMPACT_L1_ENABLED = "false";
|
|
53
|
+
process.env.MEGACOMPACT_L2_ENABLED = "false";
|
|
54
|
+
delete process.env.MEGACOMPACT_LEGACY_DURABLE_TRIM;
|
|
55
|
+
|
|
56
|
+
// Mutable context-usage so tests can drive re-compact on the TOKEN basis by
|
|
57
|
+
// raising `tokens` while keeping `percent` null (→ token gate + token
|
|
58
|
+
// grewEnough path in context-handler.ts). The REPLAY test keeps percent=100 so
|
|
59
|
+
// the percent-basis grewEnough (>=10) never trips (no re-compact → pure replay).
|
|
60
|
+
const usage = { tokens: 200000, contextWindow: 200000, percent: 100 as number | null };
|
|
61
|
+
|
|
62
|
+
const handlers: Record<string, Function> = {};
|
|
63
|
+
const compactCalls: any[] = [];
|
|
64
|
+
|
|
65
|
+
function msg(role: string, text: string, toolName?: string): AgentMessage {
|
|
66
|
+
if (role === "assistant" && toolName) {
|
|
67
|
+
return { role: "assistant", content: [{ type: "toolCall", name: toolName, id: "c1", arguments: {} }], api: "anthropic-messages", provider: "anthropic", model: "m", usage: { inputTokens: 1, outputTokens: 1, cacheReadTokens: 0, cacheWriteTokens: 0 }, stopReason: "tool_use", timestamp: 0 } as unknown as AgentMessage;
|
|
68
|
+
}
|
|
69
|
+
if (role === "toolResult" && toolName) {
|
|
70
|
+
return { role: "toolResult", content: [{ type: "text", text }], toolCallId: "c1", toolName, isError: false, timestamp: 0 } as unknown as AgentMessage;
|
|
71
|
+
}
|
|
72
|
+
return { role: "user", content: text, timestamp: 0 } as unknown as AgentMessage;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Build a session of `n` tool-call triples tagged `tag`. Set A and B differ in
|
|
76
|
+
// content (so B never dedups against A) but A === A reproduces the same
|
|
77
|
+
// regionText → same L0 contentHash → dedup onto the first A checkpoint.
|
|
78
|
+
function buildSession(tag: string, n: number): AgentMessage[] {
|
|
79
|
+
const s: AgentMessage[] = [];
|
|
80
|
+
for (let i = 0; i < n; i++) {
|
|
81
|
+
s.push(msg("user", `[${tag}] we decided to use approach ${i} for module ${i}`));
|
|
82
|
+
s.push(msg("assistant", `[${tag}] edited module ${i}`, "Edit"));
|
|
83
|
+
s.push(msg("toolResult", `[${tag}] edited module ${i}`, "Edit"));
|
|
84
|
+
}
|
|
85
|
+
return s;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const toEntry = (m: AgentMessage, i: number): any => ({ type: "message", id: `e${i}`, parentId: null, timestamp: String(i), message: m });
|
|
89
|
+
const sessionManager = {
|
|
90
|
+
getSessionId: () => "sess_cache_001",
|
|
91
|
+
getEntries: () => buildSession("A", 14).map(toEntry),
|
|
92
|
+
getBranch: () => buildSession("A", 14).map(toEntry),
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
function makeCtx(over: Partial<any> = {}) {
|
|
96
|
+
return {
|
|
97
|
+
ui: { setStatus: () => {}, notify: () => {}, select: () => {}, confirm: async () => true, input: async () => "", setWidget: () => {} },
|
|
98
|
+
mode: "tui" as any, hasUI: true, cwd: stateDir, sessionManager,
|
|
99
|
+
modelRegistry: {} as any, model: undefined, isIdle: () => true, isProjectTrusted: () => true,
|
|
100
|
+
signal: undefined, abort: () => {}, hasPendingMessages: () => false, shutdown: () => {},
|
|
101
|
+
getContextUsage: () => ({ ...usage }),
|
|
102
|
+
compact: (opts?: any) => { compactCalls.push(opts); return undefined; },
|
|
103
|
+
getSystemPrompt: () => "system base",
|
|
104
|
+
...over,
|
|
105
|
+
} as any;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const pi = {
|
|
109
|
+
on: (ev: string, h: Function) => { handlers[ev] = h; },
|
|
110
|
+
registerCommand: () => {}, registerTool: () => {}, registerShortcut: () => {},
|
|
111
|
+
registerFlag: () => {}, getFlag: () => undefined, registerMessageRenderer: () => {},
|
|
112
|
+
registerEntryRenderer: () => {}, sendMessage: () => {}, sendUserMessage: () => {},
|
|
113
|
+
appendEntry: () => {}, setSessionName: () => {}, getSessionName: () => undefined,
|
|
114
|
+
setLabel: () => {}, exec: async () => ({ stdout: "", stderr: "", code: 0 }),
|
|
115
|
+
getActiveTools: () => [], getAllTools: () => [], setActiveTools: () => {},
|
|
116
|
+
getCommands: () => [], setModel: async () => false, getThinkingLevel: () => "off" as any,
|
|
117
|
+
setThinkingLevel: () => {},
|
|
118
|
+
} as any;
|
|
119
|
+
|
|
120
|
+
const mod = require("./mega-compact.js") as { default: (p: any) => void };
|
|
121
|
+
mod.default(pi);
|
|
122
|
+
const { lastRuntime } = require("./mega-events.js") as { lastRuntime: any };
|
|
123
|
+
|
|
124
|
+
const fire = (ev: string, event: any, ctx: any) => handlers[ev](event, ctx);
|
|
125
|
+
return {
|
|
126
|
+
stateDir, handlers, compactCalls, fire, ctx: makeCtx, usage, buildSession,
|
|
127
|
+
runtime: lastRuntime, // MegaRuntime with diag* counters + rt + trimCache
|
|
128
|
+
// Bypass the 2s debounce so each fire proceeds without real waiting.
|
|
129
|
+
clearDebounce: () => { if (lastRuntime) lastRuntime.debounceUntil = 0; },
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
test("REPLAY: >=2 gated context events within one epoch replay verbatim (byte-identical)", async () => {
|
|
134
|
+
const h = harness();
|
|
135
|
+
const ctx = h.ctx();
|
|
136
|
+
const session = h.buildSession("A", 14);
|
|
137
|
+
|
|
138
|
+
// Fire 3 gated context events; clearDebounce between so each passes the gate.
|
|
139
|
+
// percent stays 100 → percent-basis grewEnough (>=10) never trips → pure replay.
|
|
140
|
+
h.clearDebounce();
|
|
141
|
+
const r1 = await h.fire("context", { type: "context", messages: session }, ctx);
|
|
142
|
+
h.clearDebounce();
|
|
143
|
+
const r2 = await h.fire("context", { type: "context", messages: session }, ctx);
|
|
144
|
+
h.clearDebounce();
|
|
145
|
+
const r3 = await h.fire("context", { type: "context", messages: session }, ctx);
|
|
146
|
+
|
|
147
|
+
const rt = h.runtime;
|
|
148
|
+
assert.ok(rt.diagLiveTrimFires >= 1, "fresh trim fired on first event");
|
|
149
|
+
assert.ok(rt.diagLiveTrimReplays >= 2, `replay fired >=2 (got ${rt.diagLiveTrimReplays})`);
|
|
150
|
+
// byte-identical (stable KV-cache prefix) across replays
|
|
151
|
+
assert.deepEqual(r2?.messages, r3?.messages, "replay messages byte-identical across replays");
|
|
152
|
+
// replay matches the fresh-trim view (shallow-copy preserves content)
|
|
153
|
+
assert.deepEqual(r1?.messages, r2?.messages, "replay matches fresh-trim view (stable prefix)");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("DEDUP: re-compact that dedups onto a DIFFERENT checkpoint still replays next (P2 fix)", async () => {
|
|
157
|
+
const h = harness();
|
|
158
|
+
// Token-basis growth path: percent null → token gate + token grewEnough
|
|
159
|
+
// (currentTokens - trimCache.ctxTokens >= effectiveThreshold * 0.5 = 25).
|
|
160
|
+
h.usage.percent = null;
|
|
161
|
+
h.usage.tokens = 200000;
|
|
162
|
+
const ctx = h.ctx();
|
|
163
|
+
const setA = h.buildSession("A", 14);
|
|
164
|
+
const setB = h.buildSession("B", 14); // different content, same length
|
|
165
|
+
const rt = h.runtime;
|
|
166
|
+
|
|
167
|
+
// 1) Fresh trim on setA → genuinely new checkpoint cp_A. lastCheckpointId = cp_A.
|
|
168
|
+
h.clearDebounce();
|
|
169
|
+
await h.fire("context", { type: "context", messages: setA }, ctx);
|
|
170
|
+
const cpA = rt.rt.lastCheckpointId;
|
|
171
|
+
assert.ok(cpA, "cp_A created on fresh trim");
|
|
172
|
+
assert.equal(rt.diagLiveTrimFires, 1, "first fire was a fresh trim");
|
|
173
|
+
|
|
174
|
+
// 2) Re-compact on setB (grew tokens) → genuinely new checkpoint cp_B (not deduped).
|
|
175
|
+
h.usage.tokens = 200100; // grew 100 >= 25
|
|
176
|
+
h.clearDebounce();
|
|
177
|
+
await h.fire("context", { type: "context", messages: setB }, ctx);
|
|
178
|
+
const cpB = rt.rt.lastCheckpointId;
|
|
179
|
+
assert.notEqual(cpB, cpA, "cp_B is a different, genuinely new checkpoint");
|
|
180
|
+
assert.equal(rt.rt.dedupSkips, 0, "setB did not dedup (different vocabulary, fuzzy tiers off)");
|
|
181
|
+
|
|
182
|
+
// 3) Re-compact on setA AGAIN (grew tokens) → L0 contentHash dedup onto cp_A.
|
|
183
|
+
// result.checkpointId = cp_A (!= lastCheckpointId cp_B); lastCheckpointId is
|
|
184
|
+
// NOT updated on a dedup (compact.ts:100-104), so it stays cp_B. With the
|
|
185
|
+
// fix, trimCache.checkpointId is keyed on lastCheckpointId (cp_B), NOT the
|
|
186
|
+
// dedup-volatile result.checkpointId (cp_A).
|
|
187
|
+
h.usage.tokens = 200200; // grew 100 >= 25
|
|
188
|
+
h.clearDebounce();
|
|
189
|
+
await h.fire("context", { type: "context", messages: setA }, ctx);
|
|
190
|
+
assert.equal(rt.rt.lastCheckpointId, cpB, "dedup did NOT bump lastCheckpointId (still cp_B)");
|
|
191
|
+
assert.ok(rt.rt.dedupSkips >= 1, "setA re-compact deduped onto an existing checkpoint");
|
|
192
|
+
// The P2 invariant: the cache key must equal the stable epoch signal.
|
|
193
|
+
assert.equal(rt.trimCache?.checkpointId, rt.rt.lastCheckpointId,
|
|
194
|
+
"trimCache.checkpointId keyed on lastCheckpointId (P2 fix), not dedup-volatile result.checkpointId");
|
|
195
|
+
|
|
196
|
+
// 4) Next gated event (no growth) MUST replay instead of re-running runCompact.
|
|
197
|
+
// Without the fix, trimCache.checkpointId (cp_A) != lastCheckpointId (cp_B)
|
|
198
|
+
// → the replay condition is false → runCompact re-runs every fire → the
|
|
199
|
+
// thrash silently persists in that path (the audit's finding).
|
|
200
|
+
const replaysBefore = rt.diagLiveTrimReplays;
|
|
201
|
+
h.usage.tokens = 200200; // no growth → replay
|
|
202
|
+
h.clearDebounce();
|
|
203
|
+
await h.fire("context", { type: "context", messages: setA }, ctx);
|
|
204
|
+
assert.ok(rt.diagLiveTrimReplays > replaysBefore,
|
|
205
|
+
`replay fired after dedup-onto-different-checkpoint (got ${rt.diagLiveTrimReplays}, was ${replaysBefore})`);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("cleanup", async () => {
|
|
209
|
+
await closeVectorIndex();
|
|
210
|
+
rmSync(baseTmp, { recursive: true, force: true });
|
|
211
|
+
});
|
|
@@ -139,6 +139,13 @@ export interface DashboardSnapshot {
|
|
|
139
139
|
inputRate: number; // USD per input token (Model.cost)
|
|
140
140
|
outputRate: number; // USD per output token (Model.cost)
|
|
141
141
|
};
|
|
142
|
+
/** v0.8.8 Perf dashboard: live diag counters (skip vs recompute vs replay)
|
|
143
|
+
* for the Perf tab's "TUI lag proxy" cards. Optional for back-compat. */
|
|
144
|
+
diag?: {
|
|
145
|
+
ctxFastGate: number;
|
|
146
|
+
liveTrimFires: number;
|
|
147
|
+
liveTrimReplays: number;
|
|
148
|
+
};
|
|
142
149
|
}
|
|
143
150
|
|
|
144
151
|
export class Dashboard {
|
|
@@ -151,9 +158,19 @@ export class Dashboard {
|
|
|
151
158
|
this.eventsPath = join(stateDir, "events.log");
|
|
152
159
|
}
|
|
153
160
|
|
|
161
|
+
/** v0.8.8: duration (ms) of the last dashboard.json write — read by
|
|
162
|
+
* MegaRuntime.snapshot() to record a `disk_write_ms` perf sample without
|
|
163
|
+
* wrapping the giant snapshot object literal at the call site. */
|
|
164
|
+
private _lastWriteMs = 0;
|
|
165
|
+
get lastWriteMs(): number {
|
|
166
|
+
return this._lastWriteMs;
|
|
167
|
+
}
|
|
168
|
+
|
|
154
169
|
/** Write a full state snapshot (atomically replaces previous). */
|
|
155
170
|
snapshot(data: DashboardSnapshot): void {
|
|
171
|
+
const t = performance.now();
|
|
156
172
|
writeFileSync(this.snapshotPath, JSON.stringify(data, null, 2) + "\n");
|
|
173
|
+
this._lastWriteMs = performance.now() - t;
|
|
157
174
|
}
|
|
158
175
|
|
|
159
176
|
/** Append a timestamped JSONL event line. */
|
|
@@ -184,7 +184,9 @@ export function registerContextHandler(
|
|
|
184
184
|
runtime.diagLiveTrimFires++; // trim view returned this call (replay counts as a fire)
|
|
185
185
|
runtime.diagLiveTrimReplays++;
|
|
186
186
|
runtime.snapshot(ctx);
|
|
187
|
-
|
|
187
|
+
// v0.8.7: shallow-copy the cached summary so pi's transformContext can't
|
|
188
|
+
// mutate the shared reference across replays (audit P3).
|
|
189
|
+
return { messages: [{ ...runtime.trimCache.summaryAgentMsg }, ...recent] };
|
|
188
190
|
}
|
|
189
191
|
// else: context grew enough → fall through to re-compact (cache is stale)
|
|
190
192
|
}
|
|
@@ -308,7 +310,21 @@ export function registerContextHandler(
|
|
|
308
310
|
// replay it verbatim (stabilizing the KV-cache prefix) instead of
|
|
309
311
|
// regenerating a fresh summary + sentinel every fire.
|
|
310
312
|
runtime.trimCache = {
|
|
311
|
-
|
|
313
|
+
// v0.8.7: key the replay cache on the STABLE epoch signal
|
|
314
|
+
// (rt.lastCheckpointId) instead of ran.result.checkpointId, which is
|
|
315
|
+
// dedup-volatile: on a re-compact that dedups onto a DIFFERENT existing
|
|
316
|
+
// checkpoint, result.checkpointId is the matched id (engine.ts:188) while
|
|
317
|
+
// lastCheckpointId is only updated on a genuinely new checkpoint
|
|
318
|
+
// (compact.ts:100-104). Keying on result.checkpointId would make
|
|
319
|
+
// trimCache.checkpointId != rt.lastCheckpointId forever after that
|
|
320
|
+
// dedup fire, disabling replay for the rest of the epoch (the
|
|
321
|
+
// alternating cache-miss that 0.8.6 meant to fix). Prefer the stable
|
|
322
|
+
// signal; fall back to result.checkpointId then the epoch timestamp
|
|
323
|
+
// only for the no-checkpoint edge case.
|
|
324
|
+
checkpointId:
|
|
325
|
+
runtime.rt.lastCheckpointId ??
|
|
326
|
+
ran.result.checkpointId ??
|
|
327
|
+
`epoch-${runtime.rt.lastCompactAt ?? Date.now()}`,
|
|
312
328
|
cut,
|
|
313
329
|
summaryAgentMsg,
|
|
314
330
|
ctxPct: pct ?? null,
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mega-events/perf-handler.ts — local perf instrumentation handlers (v0.8.8).
|
|
3
|
+
*
|
|
4
|
+
* Captures cheap, local-only telemetry into the `perf_samples` SQLite table for
|
|
5
|
+
* the dashboard's Perf tab: turn + provider latency, TPS, cache hit %, and (via
|
|
6
|
+
* MegaRuntime.ensurePerfInterval) a 5s cpu/mem interval. All capture is wrapped in
|
|
7
|
+
* try/catch — instrumentation NEVER blocks the agent loop (non-fatal).
|
|
8
|
+
*
|
|
9
|
+
* PREVENT-PI-004: Date.now / process.cpuUsage / process.memoryUsage + local
|
|
10
|
+
* SQLite only, zero network.
|
|
11
|
+
* PREVENT-011: no `any` — the usage block is narrowed structurally.
|
|
12
|
+
*/
|
|
13
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
14
|
+
import { type MegaRuntime } from "../mega-runtime.js";
|
|
15
|
+
import { recordPerfSample } from "../../src/store/sqlite.js";
|
|
16
|
+
|
|
17
|
+
/** Structural view of an AssistantMessage usage block (no pi-ai import). */
|
|
18
|
+
interface UsageBlock {
|
|
19
|
+
input: number;
|
|
20
|
+
output: number;
|
|
21
|
+
cacheRead: number;
|
|
22
|
+
cacheWrite: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Narrow a turn_end message to its usage block when it is an assistant msg. */
|
|
26
|
+
function usageOf(
|
|
27
|
+
msg: { role?: string; usage?: UsageBlock },
|
|
28
|
+
): UsageBlock | null {
|
|
29
|
+
if (msg.role !== "assistant" || !msg.usage) return null;
|
|
30
|
+
return msg.usage;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Register perf instrumentation handlers + start the 5s cpu/mem interval. */
|
|
34
|
+
export function registerPerfHandler(
|
|
35
|
+
pi: ExtensionAPI,
|
|
36
|
+
runtime: MegaRuntime,
|
|
37
|
+
): void {
|
|
38
|
+
// turn_start: record the wall-clock start of the turn. Using Date.now() (not
|
|
39
|
+
// event.timestamp) so the turn_end duration is on ONE clock — mixing pi's
|
|
40
|
+
// timestamp with Date.now() would skew the delta. Also (re)arms the cpu/mem
|
|
41
|
+
// interval so a new session after a dispose() resumes sampling on its first
|
|
42
|
+
// turn (the interval is cleared in runtime.dispose()).
|
|
43
|
+
pi.on("turn_start", async () => {
|
|
44
|
+
try {
|
|
45
|
+
runtime.perfTurnStart = Date.now();
|
|
46
|
+
runtime.ensurePerfInterval();
|
|
47
|
+
} catch {
|
|
48
|
+
/* non-fatal */
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// turn_end: compute turn latency + TPS + cache hit % from the assistant
|
|
53
|
+
// message's usage block. One perf_samples row per metric per turn.
|
|
54
|
+
pi.on("turn_end", async (event) => {
|
|
55
|
+
try {
|
|
56
|
+
if (runtime.perfTurnStart > 0) {
|
|
57
|
+
const durMs = Date.now() - runtime.perfTurnStart;
|
|
58
|
+
recordPerfSample(runtime.currentStateDir, "turn_latency_ms", durMs, {
|
|
59
|
+
turnIndex: event.turnIndex,
|
|
60
|
+
});
|
|
61
|
+
const u = usageOf(event.message);
|
|
62
|
+
if (u) {
|
|
63
|
+
const durSec = Math.max(durMs / 1000, 0.001);
|
|
64
|
+
recordPerfSample(
|
|
65
|
+
runtime.currentStateDir,
|
|
66
|
+
"tps",
|
|
67
|
+
u.output / durSec,
|
|
68
|
+
{ outputTokens: u.output },
|
|
69
|
+
);
|
|
70
|
+
const denom = u.cacheRead + u.input + u.cacheWrite;
|
|
71
|
+
const hitPct = denom > 0 ? (u.cacheRead / denom) * 100 : 0;
|
|
72
|
+
recordPerfSample(
|
|
73
|
+
runtime.currentStateDir,
|
|
74
|
+
"cache_hit_pct",
|
|
75
|
+
hitPct,
|
|
76
|
+
{ input: u.input, cacheRead: u.cacheRead, cacheWrite: u.cacheWrite },
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
/* non-fatal: instrumentation must never break the agent loop */
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// before_provider_request -> after_provider_response: raw round-trip latency
|
|
86
|
+
// to the model endpoint (HTTP status carried on the response event).
|
|
87
|
+
pi.on("before_provider_request", async () => {
|
|
88
|
+
try {
|
|
89
|
+
runtime.perfProviderStart = Date.now();
|
|
90
|
+
} catch {
|
|
91
|
+
/* non-fatal */
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
pi.on("after_provider_response", async (event) => {
|
|
95
|
+
try {
|
|
96
|
+
if (runtime.perfProviderStart > 0) {
|
|
97
|
+
const lat = Date.now() - runtime.perfProviderStart;
|
|
98
|
+
recordPerfSample(
|
|
99
|
+
runtime.currentStateDir,
|
|
100
|
+
"provider_latency_ms",
|
|
101
|
+
lat,
|
|
102
|
+
{ status: event.status },
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
/* non-fatal */
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Start the 5s cpu/mem sampling interval (one per MegaRuntime; cleared in
|
|
111
|
+
// runtime.dispose()). Idempotent — safe to call again after a dispose().
|
|
112
|
+
runtime.ensurePerfInterval();
|
|
113
|
+
}
|
|
@@ -12,6 +12,7 @@ import { registerSessionHandlers } from "./session-handlers.js";
|
|
|
12
12
|
import { registerAgentHandlers } from "./agent-handlers.js";
|
|
13
13
|
import { registerContextHandler } from "./context-handler.js";
|
|
14
14
|
import { registerCompactHandlers } from "./compact-handlers.js";
|
|
15
|
+
import { registerPerfHandler } from "./perf-handler.js";
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* DIAG accessor for the headless test harness: the most recently constructed
|
|
@@ -34,4 +35,5 @@ export function registerEventHandlers(
|
|
|
34
35
|
registerAgentHandlers(pi, runtime, config);
|
|
35
36
|
registerContextHandler(pi, runtime, config);
|
|
36
37
|
registerCompactHandlers(pi, runtime, config);
|
|
38
|
+
registerPerfHandler(pi, runtime);
|
|
37
39
|
}
|
|
@@ -28,6 +28,7 @@ import {
|
|
|
28
28
|
getRecallInjected,
|
|
29
29
|
getCacheHitTokensSaved,
|
|
30
30
|
getGameState,
|
|
31
|
+
recordPerfSample,
|
|
31
32
|
type ModelSnapshot,
|
|
32
33
|
type GameState,
|
|
33
34
|
} from "../../src/store/sqlite.js";
|
|
@@ -164,6 +165,12 @@ export class MegaRuntime {
|
|
|
164
165
|
// Last explain-why line (dedup reason / anchor-kept / superseded), surfaced
|
|
165
166
|
// while fresh.
|
|
166
167
|
lastWhy: string | undefined = undefined;
|
|
168
|
+
// v0.8.8 Perf dashboard instrumentation: turn/provider start timestamps +
|
|
169
|
+
// the 5s cpu/mem interval handle (one per MegaRuntime, cleared in dispose()).
|
|
170
|
+
perfTurnStart = 0;
|
|
171
|
+
perfProviderStart = 0;
|
|
172
|
+
perfCpuInterval: ReturnType<typeof setInterval> | undefined;
|
|
173
|
+
private perfCpuBaseline: { user: number; sys: number } | undefined;
|
|
167
174
|
|
|
168
175
|
// Context tracking for the dashboard (updated in the context handler).
|
|
169
176
|
lastCtxTokens: number | null = null;
|
|
@@ -385,6 +392,7 @@ export class MegaRuntime {
|
|
|
385
392
|
this.renderWidget(ctx);
|
|
386
393
|
return;
|
|
387
394
|
}
|
|
395
|
+
const perfT0 = performance.now();
|
|
388
396
|
const st = this.store.stats(this.rt.sessionId);
|
|
389
397
|
const repo = this.store.repoStats();
|
|
390
398
|
const di = this.store.dataInvariant();
|
|
@@ -540,7 +548,13 @@ export class MegaRuntime {
|
|
|
540
548
|
cacheHit: { sessionSec: sec(this.rt.cacheHitTokens), totalSec: sec(cacheHitsTotalTokens) },
|
|
541
549
|
},
|
|
542
550
|
model,
|
|
551
|
+
diag: {
|
|
552
|
+
ctxFastGate: this.diagCtxFastGate,
|
|
553
|
+
liveTrimFires: this.diagLiveTrimFires,
|
|
554
|
+
liveTrimReplays: this.diagLiveTrimReplays,
|
|
555
|
+
},
|
|
543
556
|
} as DashboardSnapshot);
|
|
557
|
+
const perfDiskMs = this.dashboard.lastWriteMs;
|
|
544
558
|
|
|
545
559
|
// Live stats widget above the editor
|
|
546
560
|
if (ctx) {
|
|
@@ -708,6 +722,12 @@ export class MegaRuntime {
|
|
|
708
722
|
}
|
|
709
723
|
// v0.8.5: record the material-change signature computed at the top so the
|
|
710
724
|
// next snapshot() can skip this whole body when nothing material changed.
|
|
725
|
+
try {
|
|
726
|
+
recordPerfSample(this.currentStateDir, "db_recompute_ms", performance.now() - perfT0);
|
|
727
|
+
recordPerfSample(this.currentStateDir, "disk_write_ms", perfDiskMs);
|
|
728
|
+
} catch {
|
|
729
|
+
/* non-fatal: perf instrumentation never blocks the agent */
|
|
730
|
+
}
|
|
711
731
|
this.lastSnapshotSig = sig;
|
|
712
732
|
}
|
|
713
733
|
|
|
@@ -981,6 +1001,43 @@ export class MegaRuntime {
|
|
|
981
1001
|
this.gameStateWatcher = undefined;
|
|
982
1002
|
this.gameStateWatchDir = undefined;
|
|
983
1003
|
}
|
|
1004
|
+
// v0.8.8: stop the cpu/mem sampling interval on teardown. Re-armed lazily
|
|
1005
|
+
// by ensurePerfInterval() on the next turn_start.
|
|
1006
|
+
if (this.perfCpuInterval) {
|
|
1007
|
+
clearInterval(this.perfCpuInterval);
|
|
1008
|
+
this.perfCpuInterval = undefined;
|
|
1009
|
+
this.perfCpuBaseline = undefined;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/** v0.8.8: (re)start the 5s cpu/mem sampling interval (idempotent). One per
|
|
1014
|
+
* MegaRuntime; cleared in dispose(). Samples process.cpuUsage() (user/sys
|
|
1015
|
+
* delta vs the last tick → ms) + process.memoryUsage() (rss/heap → MB) and
|
|
1016
|
+
* records them as perf_samples. unref'd so it never keeps the process alive
|
|
1017
|
+
* on its own. Non-fatal: any failure is swallowed (instrumentation never
|
|
1018
|
+
* blocks the agent). PREVENT-PI-004: local process stats + SQLite only. */
|
|
1019
|
+
ensurePerfInterval(): void {
|
|
1020
|
+
if (this.perfCpuInterval) return;
|
|
1021
|
+
this.perfCpuBaseline = undefined; // first tick sets the baseline (no delta)
|
|
1022
|
+
this.perfCpuInterval = setInterval(() => {
|
|
1023
|
+
try {
|
|
1024
|
+
const dir = this.currentStateDir;
|
|
1025
|
+
const cpu = process.cpuUsage();
|
|
1026
|
+
const mem = process.memoryUsage();
|
|
1027
|
+
if (this.perfCpuBaseline) {
|
|
1028
|
+
const du = (cpu.user - this.perfCpuBaseline.user) / 1000; // μs → ms
|
|
1029
|
+
const ds = (cpu.system - this.perfCpuBaseline.sys) / 1000;
|
|
1030
|
+
recordPerfSample(dir, "cpu_user_ms", Math.max(0, du));
|
|
1031
|
+
recordPerfSample(dir, "cpu_sys_ms", Math.max(0, ds));
|
|
1032
|
+
}
|
|
1033
|
+
this.perfCpuBaseline = { user: cpu.user, sys: cpu.system };
|
|
1034
|
+
recordPerfSample(dir, "rss_mb", mem.rss / 1_000_000);
|
|
1035
|
+
recordPerfSample(dir, "heap_mb", mem.heapUsed / 1_000_000);
|
|
1036
|
+
} catch {
|
|
1037
|
+
/* non-fatal */
|
|
1038
|
+
}
|
|
1039
|
+
}, 5000);
|
|
1040
|
+
this.perfCpuInterval.unref?.();
|
|
984
1041
|
}
|
|
985
1042
|
|
|
986
1043
|
/** S31: the cached game-mode state (game_mode_on/theme/tui_display_mode).
|
package/package.json
CHANGED
|
@@ -0,0 +1,65 @@
|
|
|
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 {
|
|
12
|
+
recordPerfSample,
|
|
13
|
+
readPerfSamples,
|
|
14
|
+
PERF_KINDS,
|
|
15
|
+
} from "./perf-samples.js";
|
|
16
|
+
|
|
17
|
+
describe("perf-samples (v0.8.8)", () => {
|
|
18
|
+
let dir: string;
|
|
19
|
+
before(() => {
|
|
20
|
+
dir = mkdtempSync(join(tmpdir(), "mc-perfsamples-"));
|
|
21
|
+
process.env.MEGACOMPACT_STATE_DIR = dir;
|
|
22
|
+
});
|
|
23
|
+
after(() => {
|
|
24
|
+
closeStore(dir);
|
|
25
|
+
delete process.env.MEGACOMPACT_STATE_DIR;
|
|
26
|
+
rmSync(dir, { recursive: true, force: true });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("records + reads back a turn_latency_ms sample with parsed meta", () => {
|
|
30
|
+
recordPerfSample(dir, "turn_latency_ms", 123.4, { turnIndex: 2 });
|
|
31
|
+
const rows = readPerfSamples(dir, 0);
|
|
32
|
+
assert.equal(rows.length, 1);
|
|
33
|
+
assert.equal(rows[0].kind, "turn_latency_ms");
|
|
34
|
+
assert.equal(rows[0].value, 123.4);
|
|
35
|
+
assert.deepEqual(rows[0].meta, { turnIndex: 2 });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("filters by kind and by sinceTs", () => {
|
|
39
|
+
recordPerfSample(dir, "tps", 50);
|
|
40
|
+
recordPerfSample(dir, "rss_mb", 256);
|
|
41
|
+
const tps = readPerfSamples(dir, 0, "tps");
|
|
42
|
+
assert.equal(tps.length, 1);
|
|
43
|
+
assert.equal(tps[0].kind, "tps");
|
|
44
|
+
assert.equal(tps[0].value, 50);
|
|
45
|
+
const future = readPerfSamples(dir, Date.now() + 10000, "tps");
|
|
46
|
+
assert.equal(future.length, 0);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("ignores non-finite values + unknown kinds (never throws, nothing added)", () => {
|
|
50
|
+
const before = readPerfSamples(dir, 0).length;
|
|
51
|
+
recordPerfSample(dir, "tps", Number.NaN);
|
|
52
|
+
recordPerfSample(dir, "tps", Infinity);
|
|
53
|
+
assert.doesNotThrow(() =>
|
|
54
|
+
recordPerfSample(dir, "bogus" as never, 1),
|
|
55
|
+
);
|
|
56
|
+
const after = readPerfSamples(dir, 0).length;
|
|
57
|
+
assert.equal(after, before);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("PERF_KINDS lists the 10 instrumentation kinds", () => {
|
|
61
|
+
assert.equal(PERF_KINDS.length, 10);
|
|
62
|
+
assert.ok(PERF_KINDS.includes("db_recompute_ms"));
|
|
63
|
+
assert.ok(PERF_KINDS.includes("cache_hit_pct"));
|
|
64
|
+
});
|
|
65
|
+
});
|