pi-mega-compact 0.8.22 → 0.8.24

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 (63) hide show
  1. package/LICENSE +6 -2
  2. package/README.md +1 -1
  3. package/dist/extensions/dashboard-server/api-contracts/endpoints.js +8 -0
  4. package/dist/extensions/dashboard-server/api-contracts/game-types.js +7 -0
  5. package/dist/extensions/mega-runtime/append-event.js +24 -0
  6. package/dist/extensions/mega-runtime/bind-repo.js +65 -0
  7. package/dist/extensions/mega-runtime/capture-model.js +87 -0
  8. package/dist/extensions/mega-runtime/dashboard-snapshot.js +118 -0
  9. package/dist/extensions/mega-runtime/effects.js +86 -0
  10. package/dist/extensions/mega-runtime/engine-view.js +11 -0
  11. package/dist/extensions/mega-runtime/game-state.js +116 -0
  12. package/dist/extensions/mega-runtime/get-state-dir.js +10 -0
  13. package/dist/extensions/mega-runtime/perf.js +49 -0
  14. package/dist/extensions/mega-runtime/pressure-getters.js +64 -0
  15. package/dist/extensions/mega-runtime/render-widget.js +17 -0
  16. package/dist/extensions/mega-runtime/reset-runtime.js +50 -0
  17. package/dist/extensions/mega-runtime/runtime-helpers.js +73 -0
  18. package/dist/extensions/mega-runtime/runtime-snapshot.js +204 -0
  19. package/dist/extensions/mega-runtime/runtime.js +352 -0
  20. package/dist/extensions/mega-runtime/snapshot.js +142 -0
  21. package/dist/extensions/mega-runtime/state.js +5 -1151
  22. package/dist/extensions/mega-runtime/status.js +11 -0
  23. package/dist/extensions/mega-runtime/widget-ansi.js +207 -0
  24. package/dist/extensions/mega-runtime/widget-types.js +8 -0
  25. package/dist/extensions/mega-runtime/widget.js +15 -204
  26. package/dist/extensions/openclaw-mega-compact.js +291 -0
  27. package/dist/src/dedup/raptor/multilevel.js +172 -0
  28. package/dist/src/dedup/raptor/multilevel.test.js +203 -0
  29. package/dist/src/dedup/raptor/retrieval.js +1 -1
  30. package/dist/src/minilm.js +92 -0
  31. package/dist/src/wordpiece.js +129 -0
  32. package/extensions/dashboard-client/dist/assets/index-D_WtU2TV.js.map +1 -1
  33. package/extensions/dashboard-server/api-contracts/endpoints.ts +30 -155
  34. package/extensions/dashboard-server/api-contracts/game-types.ts +172 -0
  35. package/extensions/mega-runtime/DECOMPOSITION.md +180 -0
  36. package/extensions/mega-runtime/README.md +38 -0
  37. package/extensions/mega-runtime/append-event.ts +40 -0
  38. package/extensions/mega-runtime/bind-repo.ts +81 -0
  39. package/extensions/mega-runtime/capture-model.ts +101 -0
  40. package/extensions/mega-runtime/dashboard-snapshot.ts +173 -0
  41. package/extensions/mega-runtime/effects.ts +129 -0
  42. package/extensions/mega-runtime/engine-view.ts +17 -0
  43. package/extensions/mega-runtime/game-state.ts +149 -0
  44. package/extensions/mega-runtime/get-state-dir.ts +19 -0
  45. package/extensions/mega-runtime/perf.ts +60 -0
  46. package/extensions/mega-runtime/pressure-getters.ts +96 -0
  47. package/extensions/mega-runtime/render-widget.ts +41 -0
  48. package/extensions/mega-runtime/reset-runtime.ts +80 -0
  49. package/extensions/mega-runtime/runtime-helpers.ts +119 -0
  50. package/extensions/mega-runtime/runtime-snapshot.ts +289 -0
  51. package/extensions/mega-runtime/runtime.ts +437 -0
  52. package/extensions/mega-runtime/snapshot.ts +230 -0
  53. package/extensions/mega-runtime/state.ts +5 -1268
  54. package/extensions/mega-runtime/status.ts +26 -0
  55. package/extensions/mega-runtime/widget-ansi.ts +217 -0
  56. package/extensions/mega-runtime/widget-types.ts +80 -0
  57. package/extensions/mega-runtime/widget.ts +34 -285
  58. package/package.json +2 -2
  59. package/src/dedup/raptor/multilevel.test.ts +278 -0
  60. package/src/dedup/raptor/multilevel.ts +246 -0
  61. package/src/dedup/raptor/retrieval.ts +1 -1
  62. package/dist/extensions/dashboard-client/src/hooks/useApi.js +0 -51
  63. package/dist/extensions/dashboard-client/src/hooks/useSSE.js +0 -63
@@ -1,1271 +1,8 @@
1
1
  /**
2
- * state.ts — the `MegaRuntime` class: shared live state of the mega-compact
3
- * extension.
2
+ * state.ts — backwards-compatible re-export of MegaRuntime.
4
3
  *
5
- * The original mega-compact.ts was a single large closure over ~20 mutable
6
- * variables. This module lifts that state into a `MegaRuntime` class so the
7
- * event/command/pipeline modules can share it without re-declaring it. All
8
- * behavior (store/dashboard rebinding, dashboard snapshot shape, the
9
- * above-editor widget math, model capture) is preserved byte-for-byte from the
10
- * original closure.
4
+ * The class implementation lives in runtime.ts. This file exists so that
5
+ * every existing `import { MegaRuntime } from "./state.js"` continues to
6
+ * resolve without changes.
11
7
  */
12
-
13
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
14
- import type { AgentMessage } from "@earendil-works/pi-agent-core";
15
- import { join } from "node:path";
16
- import { appendFileSync, mkdirSync } from "node:fs";
17
- import { VectorStore, vectorStats, vectorRepoStats, vectorDataInvariant } from "../../src/vectorStore.js";
18
- import { toEngineMessages } from "../../src/adapt.js";
19
- import { normalizeSessionId } from "../../src/store.js";
20
- import { Logger } from "../../src/log.js";
21
- import {
22
- recordModelSnapshot,
23
- latestModelSnapshot,
24
- upsertRepoRegistry,
25
- recordRepoModel,
26
- getDedupStats,
27
- getCompactCount,
28
- getRecallInjected,
29
- getCacheHitTokensSaved,
30
- getGameState,
31
- recordPerfSample,
32
- recordSessionHeartbeat,
33
- appendTokenSample,
34
- type ModelSnapshot,
35
- type GameState,
36
- } from "../../src/store/sqlite.js";
37
- import { detectCrossRepoDrift } from "../../src/driftDetection.js";
38
- import {
39
- repoStateDir,
40
- resolveRepoRoot,
41
- pressureRatio,
42
- pressureFromPct,
43
- pressureBand,
44
- effectiveThresholdTokens,
45
- type MegaConfig,
46
- type PressureBand,
47
- } from "../mega-config.js";
48
- import { Dashboard, type DashboardSnapshot } from "../mega-dashboard.js";
49
- import {
50
- STATUS_KEY,
51
- WIDGET_KEY,
52
- TOKENS_PER_SEC_ESTIMATE,
53
- ownVersion,
54
- type SessionRuntime,
55
- } from "./helpers.js";
56
- import {
57
- C,
58
- buildWidgetLines,
59
- type TickerEntry,
60
- type WidgetData,
61
- } from "./widget.js";
62
- import { getTheme } from "../../src/config/themes.js";
63
- import { watch, type FSWatcher } from "node:fs";
64
- import { turnLevel } from "../../src/game/scoring.js";
65
-
66
- export class MegaRuntime {
67
- config: MegaConfig;
68
- // Store/dashboard/logger are rebound per-repo by bindRepo() so each git repo
69
- // gets its own isolated state dir. They start bound to the global default.
70
- store: VectorStore;
71
- logger: Logger;
72
- dashboard: Dashboard;
73
- activeRepoRoot: string | null = null;
74
- currentStateDir: string;
75
-
76
- // The only mutable per-session state. Reset on session_start / session_tree.
77
- rt: SessionRuntime = {
78
- sessionId: normalizeSessionId(undefined),
79
- persistedThisSession: false,
80
- lastCheckpointId: undefined,
81
- lastCompactedFrom: 0,
82
- lastCompactedTokens: 0,
83
- dedupSkips: 0,
84
- dedupAttempts: 0,
85
- tokensSaved: 0,
86
- lastCompactAt: null,
87
- lastNativeCompactAt: null,
88
- compactCount: 0,
89
- recallInjections: 0,
90
- cacheHitTokens: 0,
91
- lengthStopPending: false,
92
- errorRetryCount: 0,
93
- errorRetryUntil: 0,
94
- consecutiveErrors: 0,
95
- };
96
- // v0.8.6 cache-stability: the cached live-trim view for the current
97
- // compaction epoch. Set after a fresh runCompact + computeLiveTrimCut, and
98
- // replayed verbatim on subsequent gated context events in the SAME epoch
99
- // (same checkpointId) so the provider KV-cache prefix stays stable instead
100
- // of being invalidated by a freshly regenerated summary + sentinel every
101
- // fire. Invalidated on session restart (resetRuntime) and on any native
102
- // durable compaction (session_compact) that truncates the transcript.
103
- trimCache: {
104
- checkpointId: string;
105
- cut: number;
106
- summaryAgentMsg: AgentMessage;
107
- ctxPct: number | null;
108
- ctxTokens: number | null;
109
- } | null = null;
110
- debounceUntil = 0;
111
- // S16: debounce for the agent_end resume nudge (avoid busy-loops).
112
- resumeNudgeUntil = 0;
113
- // Agent tracking for real-time widget updates
114
- activeAgents = 0;
115
- currentTurn = 0;
116
- // S33: transient MEGA CACHE flare flag (armed by the turn_end scoring hook
117
- // when cachePct > 100). Copied into widgetData.megaCacheFlare on the next
118
- // snapshot() so the widget renders the oopsie gag, then reset (one cycle).
119
- megaCacheFlare = false;
120
- /** v0.8.3: ambient effect state for animated panel borders keyed off
121
- * status transitions (level-up, mega-cache overshoot, achievement unlock,
122
- * compaction start). Threaded into widgetData as `activeEffect`; the widget
123
- * computes the per-frame phase from startedAt vs Date.now() (non-expired).
124
- * Null when idle/expired. */
125
- activeEffect: { type: "pulse" | "flash"; role: "accent" | "mega" | "red"; startedAt: number; durationMs: number } | null = null;
126
- megaCacheFlarePct = 0;
127
- levelUpFlare = false;
128
- lastLevel = 0;
129
- // S35: transient achievement-unlock flare (armed by the scoring hooks after
130
- // evaluateAndUnlockAchievements returns newly-unlocked titles). Copied into
131
- // widgetData.achievementFlare on the next snapshot() so the widget renders the
132
- // unlock toast, then reset (one cycle — mirrors megaCacheFlare/levelUpFlare).
133
- achievementFlare = false;
134
- achievementFlareTitles: string[] = [];
135
- // S33: last cumulative dedup-collapsed count seen by the session_compact
136
- // hook, so we only record the DELTA as the dedupe score (leaderboard sums).
137
- lastDedupCollapsed = 0;
138
- // Recall block produced by auto-inline (resume/branch) that the next
139
- // before_agent_start should prepend to the system prompt. Unset after use.
140
- pendingRecallBlock: string | undefined;
141
- // S21: memory recall block, parallel to pendingRecallBlock. Same one-shot
142
- // semantics; composed with the checkpoint block in before_agent_start.
143
- pendingMemoryRecallBlock: string | undefined;
144
- statusKey: string | undefined; // current status text for dashboard
145
- // Active model/provider (for real cost estimation). Captured from ctx.model
146
- // on model_select + session_start; persisted to SQL so cost + the dashboard
147
- // can read it without a live ctx.
148
- currentModel: ModelSnapshot | undefined;
149
- // Live "what it's doing right now" timestamp, used for the fresh-window.
150
- lastActivityAt = 0;
151
- // Live per-tier dedup trace (Phase 1): e.g. "L0 ✓ → L1 ✓ → L2 0.91 → stored".
152
- // Built from the store's sync onTier callback during a compaction so the user
153
- // watches each tier evaluate in real time. Cleared once the outcome settles.
154
- tierTrace: string | undefined;
155
- // Phase 3 — standout toolbar state.
156
- // Recall/activity ticker: a small ring buffer (≤5) of recent compact/recall
157
- // events so the widget shows a live history instead of a single last action.
158
- ticker: TickerEntry[] = [];
159
- readonly TICKER_MAX = 5;
160
- // Pulsing status: set true while a compaction is in flight, cleared on result.
161
- pulsing = false;
162
- // S21.2: set by `applyMemoryOps` when a memory add/replace/remove lands in
163
- // the current compaction. The pipeline reads this after a successful compact
164
- // to decide whether to fire `consolidateMemories` (skip the work entirely
165
- // when no memory rows changed).
166
- memoriesTouchedThisCompaction = 0;
167
- // Rolling "saved" goal for the progress bar — grows as we save more, so the
168
- // bar always has a meaningful denominator (never sits at 100% forever).
169
- savedGoal = 50_000;
170
- // Last explain-why line (dedup reason / anchor-kept / superseded), surfaced
171
- // while fresh.
172
- lastWhy: string | undefined = undefined;
173
- // v0.8.8 Perf dashboard instrumentation: turn/provider start timestamps +
174
- // the 5s cpu/mem interval handle (one per MegaRuntime, cleared in dispose()).
175
- perfTurnStart = 0;
176
- perfProviderStart = 0;
177
- perfCpuInterval: ReturnType<typeof setInterval> | undefined;
178
- private perfCpuBaseline: { user: number; sys: number } | undefined;
179
-
180
- // Context tracking for the dashboard (updated in the context handler).
181
- lastCtxTokens: number | null = null;
182
- lastCtxPercent: number | null = null;
183
- lastCtxWindow = 0;
184
-
185
- // Latest computed widget payload (recomputed per snapshot, rendered per frame).
186
- widgetData: WidgetData | null = null;
187
- // v0.8.5: material-change signature from the last full snapshot() body. When
188
- // the next snapshot()'s signature matches, the expensive recompute (6 sync
189
- // SQLite opens) + writeFileSync(dashboard.json) are skipped — only the
190
- // (already-registered) widget factory is refreshed. Kills the per-event
191
- // main-thread block during typing/idle streaming with no material change.
192
- private lastSnapshotSig: string | null = null;
193
- // v0.8.5: bumped whenever the cached game-state memo is evicted (bumpGameState
194
- // for in-process /mega-game writes, the fs.watch callback for cross-process
195
- // dashboard-server writes, and bindRepo on repo switch) so the snapshot gate
196
- // invalidates and the widget re-reads theme/mode after the change.
197
- private gameStateBump = 0;
198
- // Cached cross-repo drift status (recomputed at most every 30s — it opens the
199
- // machine-wide registry DB, so we don't want to do it on every render frame).
200
- private driftCache: { at: number; status: "ok" | "warn" } | null = null;
201
- // S31: cached game-mode state (game_mode_on/theme/tui_display_mode). Lazily
202
- // read from the game_state SQLite row on the first widget render, then
203
- // memoized until bumpGameState() evicts it (called by /mega-game after a
204
- // write) so the widget picks up theme/mode/level changes live without
205
- // re-querying the DB on every render frame.
206
- private cachedGameState: GameState | undefined;
207
- // S32: fs.watch on the current repo's sqlite.db so cross-process writes
208
- // (e.g. the dashboard server's PUT /api/game-state, which runs as a detached
209
- // child with no MegaRuntime ref) evict the cached game-state memo. Without
210
- // this, /mega-game's in-process bumpGameState() is the only eviction trigger
211
- // and the widget would keep showing stale theme/mode/toggle after a dashboard
212
- // edit until a restart. The watcher tracks currentStateDir — closed + re-opened
213
- // by ensureGameStateWatcher() on every bindRepo repo switch. Non-fatal: any
214
- // fs.watch failure (missing file / platform issue) is swallowed; the next
215
- // getCachedGameState() snapshot re-queries the DB anyway.
216
- private gameStateWatcher?: FSWatcher;
217
- private gameStateWatchDir?: string;
218
- // P2: the last ExtensionContext handed to snapshot()/renderWidget(), stashed
219
- // so the fs.watch game-state callback can force a widget re-render without
220
- // a context event (cross-process dashboard edits while pi is idle). Cleared
221
- // implicitly on construction (undefined → watcher skips until first snap).
222
- private lastWidgetCtx?: ExtensionContext;
223
-
224
- /**
225
- * DIAG counters for the "team run doesn't relieve context" investigation.
226
- * Plain integers, incremented at the three compaction decision points. They
227
- * let a headless test drive the real event handlers and assert the firing
228
- * cadence without scraping log files. Inert in production (the live-trim and
229
- * before-compact probes also emit logger.info, but these counters are always
230
- * updated and cost nothing).
231
- */
232
- diagLiveTrimFires = 0; // context handler returned a trimmed view
233
- diagLiveTrimReplays = 0; // v0.8.6: trim view returned via cached replay (skipped re-compact)
234
- diagBeforeCompactFires = 0; // session_before_compact handler entered
235
- diagBeforeCompactSupplied = 0; // session_before_compact supplied our trim
236
- diagAgentEndIdle = 0; // agent_end with activeAgents===0
237
- diagAgentEndDurable = 0; // agent_end fired ctx.compact() (mid-run durable trim)
238
- diagAgentEndDurableSkipRecent = 0; // agent_end skipped ctx.compact() — compaction in last 10s (race guard)
239
- // Per-skip-path counters for the team-run diagnosis.
240
- diagCtxFastGate = 0; // returned at token fast-gate (below threshold)
241
- diagCtxNoCompact = 0; // autoCompactCheck().shouldCompact === false
242
- diagCtxDebounce = 0; // debounceUntil not yet elapsed
243
- diagCtxRunSkipped = 0; // runCompact() returned skipped
244
- diagCtxCutNull = 0; // computeLiveTrimCut returned null (anchor/boundary)
245
- diagCtxThrown = 0; // live-trim try threw (caught)
246
-
247
- /**
248
- * S26 capture instrumentation: the "model_snapshots empty → $0.00 cost card"
249
- * bug was invisible because captureModel swallowed the DB write in a silent
250
- * `catch {}`. These always-updated counters (zero cost) let a headless test or
251
- * a live capture tell whether captureModel ran and whether the snapshot landed.
252
- */
253
- diagCaptureModelCalls = 0; // captureModel entered with a populated ctx.model
254
- diagCaptureModelFails = 0; // recordModelSnapshot threw → model_snapshots stays empty
255
-
256
- /**
257
- * Live 0–1 pressure — how full the context window is relative to the
258
- * compaction threshold.
259
- *
260
- * RECONCILE (BACKLOG dual-basis flicker): when the model context window is
261
- * known we base pressure consistently on the *percentage* basis
262
- * (`lastCtxPercent / (tierPct*100)`). This keeps the band stable whether the
263
- * latest context event carried a token count or only a percentage, so the
264
- * threshold comparison doesn't jump when a token-count event arrives vs a
265
- * percent-only event. We only fall back to the token-count basis
266
- * (`config.thresholdTokens`) when the window is unknown (e.g. before the first
267
- * context event, or a `custom` tier with no tierPct). Always finite + in [0,1].
268
- */
269
- get pressure(): number {
270
- if (
271
- this.lastCtxWindow > 0 &&
272
- this.config.tierPct != null &&
273
- this.lastCtxPercent != null
274
- ) {
275
- // pressureFromPct(x) = x/100, and x = lastCtxPercent/tierPct, so this is
276
- // exactly the intended lastCtxPercent/(tierPct*100) 0–1 ratio: at the
277
- // fire point (lastCtxPercent == tierPct*100) pressure == 1.0, matching the
278
- // token-based pressureRatio(currentTokens, effectiveThreshold) reading so
279
- // the band doesn't jump when a token-count vs percent-only event arrives.
280
- return pressureFromPct(this.lastCtxPercent / this.config.tierPct);
281
- }
282
- if (
283
- this.lastCtxTokens != null &&
284
- this.lastCtxTokens > 0 &&
285
- this.config.thresholdTokens > 0
286
- ) {
287
- return pressureRatio(this.lastCtxTokens, this.config.thresholdTokens);
288
- }
289
- return pressureFromPct(this.lastCtxPercent);
290
- }
291
-
292
- /**
293
- * The live compaction FIRE POINT in tokens: the effective threshold scaled by
294
- * the current model context window (`tierPct * window`) when known, else the
295
- * boot fallback `config.thresholdTokens`. This is what the FAST GATE /
296
- * `autoCompactCheck` / agent_end durable-trigger compare against, so
297
- * compaction fires at tier% of the window for ANY model size (200k or 1M),
298
- * always below pi's native auto-compaction (~80% of window).
299
- */
300
- get effectiveThreshold(): number {
301
- return effectiveThresholdTokens({
302
- tierPct: this.config.tierPct,
303
- fallbackThreshold: this.config.thresholdTokens,
304
- window: this.lastCtxWindow,
305
- });
306
- }
307
-
308
- /** Live discrete pressure band (low/medium/high/ultra/mega) over `pressure`. */
309
- get pressureBand(): PressureBand {
310
- return pressureBand(this.pressure);
311
- }
312
-
313
- constructor(config: MegaConfig) {
314
- this.config = config;
315
- this.store = new VectorStore({
316
- dedupSim: config.dedupSim,
317
- stateDir: config.stateDir,
318
- });
319
- this.logger = new Logger({
320
- enabled: config.debug,
321
- path: join(config.stateDir, "mega-compact.log"),
322
- });
323
- this.dashboard = new Dashboard(config.stateDir);
324
- this.currentStateDir = config.stateDir;
325
- this.ensureGameStateWatcher();
326
- }
327
-
328
- // ---- per-repo binding -----------------------------------------------------
329
-
330
- /**
331
- * Point store/dashboard/logger at the current repo's state dir. Rebuilds the
332
- * instances only when the repo root changes, so cross-repo dedup stats, db,
333
- * and events are fully isolated. Falls back to the global default outside git.
334
- */
335
- bindRepo(cwd: string | undefined): string {
336
- const dir = cwd
337
- ? repoStateDir(cwd, this.config.stateDir)
338
- : this.config.stateDir;
339
- const key = cwd ? (resolveRepoRoot(cwd) ?? dir) : dir;
340
- if (key === this.activeRepoRoot) return dir;
341
- this.activeRepoRoot = key;
342
- this.currentStateDir = dir;
343
- // S31 audit P2: bindRepo switched currentStateDir but left cachedGameState
344
- // memoized -> the widget kept showing the previous repo's theme/mode/toggle
345
- // until /mega-game or a restart. The game_state row is per-repo (per
346
- // stateDir), so evict the memo on every repo switch; the next widget render
347
- // re-queries lazily via getCachedGameState().
348
- this.cachedGameState = undefined;
349
- this.gameStateBump++;
350
- // S32: re-target the fs.watch cache-eviction watcher at the NEW stateDir's
351
- // sqlite.db so cross-process writes (dashboard server) still evict the memo.
352
- this.ensureGameStateWatcher();
353
- this.store = new VectorStore({
354
- dedupSim: this.config.dedupSim,
355
- stateDir: dir,
356
- });
357
- this.logger = new Logger({
358
- enabled: this.config.debug,
359
- path: join(dir, "mega-compact.log"),
360
- });
361
- this.dashboard = new Dashboard(dir);
362
- // Aggregate this repo into the machine-wide index so the multi-repo
363
- // dashboard (Summary / All-repos tabs) can show it alongside every other
364
- // repo. Best-effort + non-fatal: a read-only index dir or contention must
365
- // never break the per-repo compaction path. Runs only on repo-switch
366
- // (this branch), so it's infrequent — not per-context-event.
367
- try {
368
- const repo = vectorRepoStats(this.store);
369
- const di = vectorDataInvariant(this.store);
370
- const root = key !== dir ? key : (resolveRepoRoot(cwd ?? dir) ?? dir);
371
- upsertRepoRegistry({
372
- repoRoot: root,
373
- displayName: root.split(/[\\/]/).filter(Boolean).pop() ?? root,
374
- stateDir: dir,
375
- checkpointCount: repo.checkpointCount,
376
- tokensSaved: repo.tokensSaved,
377
- compressedOriginalBytes: di.compressedOriginalBytes,
378
- });
379
- } catch {
380
- /* non-fatal: index aggregation must not block compaction */
381
- }
382
- return dir;
383
- }
384
-
385
- // ---- dashboard snapshot + widget ------------------------------------------
386
-
387
- /** Collect live state and write it to disk (+ paint the above-editor widget). */
388
- snapshot(ctx?: ExtensionContext): void {
389
- if (ctx) this.lastWidgetCtx = ctx;
390
- if (ctx) this.bindRepo(ctx.cwd);
391
- // v0.8.5: gate the expensive body (6 sync SQLite opens +
392
- // writeFileSync(dashboard.json)) behind a cheap material-change signature.
393
- // During typing / idle / no-compaction streaming, the 'context' event
394
- // fires repeatedly with NO material change — skip the recompute + write and
395
- // just re-register the (live) widget factory, which reads the cached
396
- // widgetData every frame. This removes the per-event main-thread block
397
- // WITHOUT changing write timing, so tests that read dashboard.json
398
- // synchronously after a compaction still see it written (compaction changes
399
- // compactCount/tokensSaved → the signature changes → the full recompute +
400
- // write runs).
401
- const sig = this.materialSig();
402
- if (ctx && this.widgetData && this.lastSnapshotSig === sig) {
403
- this.renderWidget(ctx);
404
- return;
405
- }
406
- const perfT0 = performance.now();
407
- const st = vectorStats(this.store, this.rt.sessionId);
408
- const repo = vectorRepoStats(this.store);
409
- const di = vectorDataInvariant(this.store);
410
- // Live + store-wide cache-hit / compaction counters for the dashboard.
411
- const ds = getDedupStats(this.currentStateDir);
412
- const cacheHitsTotal = ds.deduped + getRecallInjected(this.currentStateDir);
413
- const cacheHitsTotalTokens = getCacheHitTokensSaved(this.currentStateDir);
414
- const cacheHitsSession = this.rt.dedupSkips + this.rt.recallInjections;
415
- const sec = (tok: number) => (tok || 0) / TOKENS_PER_SEC_ESTIMATE;
416
- // Active model/provider for the current-repo card + the multi-repo table.
417
- const modelSnap = latestModelSnapshot(this.currentStateDir);
418
- const model = modelSnap
419
- ? {
420
- name: modelSnap.modelName ?? modelSnap.modelId,
421
- provider: modelSnap.provider,
422
- providerName: modelSnap.providerName ?? "",
423
- inputRate: modelSnap.inputRate,
424
- outputRate: modelSnap.outputRate,
425
- }
426
- : undefined;
427
- // effectiveThresholdPct: the live fire point as a % of the window (null for
428
- // `custom`, which has no tierPct). S29: honors MEGACOMPACT_AUTO_PCT_TRIGGER
429
- // override so the dashboard's armed/ready match the context-handler gate
430
- // (which fires on this same %). Used by armed/ready + the dashboard.
431
- const effectiveThresholdPct =
432
- this.config.tierPct != null
433
- ? (this.config.autoPctTrigger ?? this.config.tierPct) * 100
434
- : null;
435
- // armed lights at/above the REAL fire point: max(effectiveThresholdPct,
436
- // fastGatePct). fastGatePct already equals tierPct*100 by default, but a
437
- // MEGACOMPACT_FAST_GATE_PCT override can raise it, so we take the max.
438
- const armed =
439
- this.lastCtxPercent != null &&
440
- this.lastCtxPercent >=
441
- Math.max(effectiveThresholdPct ?? 0, this.config.fastGatePct);
442
- // S29: ready mirrors the context-handler gate's basis — percent for tiered
443
- // (the gate fires on pct), tokens for custom (the gate fires on tokens).
444
- // Previously this always required tokens, so the dashboard could show
445
- // "armed" (percent high) but never "ready" when tokens were under-reported
446
- // — the same inconsistency the S29 gate fix removes.
447
- const ready =
448
- this.config.tierPct != null
449
- ? armed && (this.lastCtxPercent ?? 0) >= (effectiveThresholdPct ?? 0)
450
- : armed && (this.lastCtxTokens ?? 0) >= this.effectiveThreshold;
451
- this.dashboard.snapshot({
452
- version: 1,
453
- updatedAt: new Date().toISOString(),
454
- // S24: the headline tier is the LIVE pressure band; the env preset is kept
455
- // alongside as presetTier so the dashboard can show both.
456
- tier: this.pressureBand,
457
- presetTier: this.config.tier,
458
- pressure: this.pressure,
459
- config: {
460
- fastGatePct: this.config.fastGatePct,
461
- thresholdTokens: this.effectiveThreshold,
462
- tierPct: this.config.tierPct,
463
- effectiveThresholdPct,
464
- anchorUserMessages: this.config.anchorUserMessages,
465
- preserveRecent: this.config.preserveRecent,
466
- auto: this.config.auto,
467
- autoInline: this.config.autoInline,
468
- },
469
- session: {
470
- id: this.rt.sessionId,
471
- state: this.statusKey ?? "idle",
472
- persistedThisSession: this.rt.persistedThisSession,
473
- lastCheckpointId: this.rt.lastCheckpointId ?? null,
474
- lastCompactedFrom: this.rt.lastCompactedFrom,
475
- lastCompactedTokens: this.rt.lastCompactedTokens,
476
- dedupSkips: this.rt.dedupSkips,
477
- dedupAttempts: this.rt.dedupAttempts,
478
- },
479
- context: {
480
- tokens: this.lastCtxTokens,
481
- percent: this.lastCtxPercent,
482
- contextWindow: this.lastCtxWindow,
483
- },
484
- trigger: {
485
- armed,
486
- ready,
487
- currentTokens: this.lastCtxTokens,
488
- thresholdTokens: this.effectiveThreshold,
489
- fastGatePct: this.config.fastGatePct,
490
- tierPct: this.config.tierPct,
491
- effectiveThresholdPct,
492
- },
493
- crew: { activeAgents: this.activeAgents, currentTurn: this.currentTurn },
494
- store: {
495
- checkpointCount: st.checkpointCount,
496
- totalTokenEstimate: st.totalTokenEstimate,
497
- originalTokens: st.originalTokens,
498
- tokensSaved: this.rt.tokensSaved,
499
- injectedCount: st.injectedCount,
500
- dedupHitRate: st.dedupHitRate,
501
- storageDedupRate: st.storageDedupRate,
502
- dedupAttempts: st.dedupAttempts,
503
- dedupCollapsed: st.dedupCollapsed,
504
- },
505
- // Reconciled token accounting (single canonical formula, session + repo).
506
- // Freed = In − Out; In = Freed + Out. session.Freed = rt.tokensSaved (incl.
507
- // deduped-away originals); repo.Freed = repo.tokensSaved meta counter.
508
- compression: {
509
- session: {
510
- tokensIn: this.rt.tokensSaved + st.totalTokenEstimate,
511
- tokensOut: st.totalTokenEstimate,
512
- tokensFreed: this.rt.tokensSaved,
513
- compressionPct:
514
- this.rt.tokensSaved + st.totalTokenEstimate > 0
515
- ? this.rt.tokensSaved /
516
- (this.rt.tokensSaved + st.totalTokenEstimate)
517
- : 0,
518
- dedupPct: st.storageDedupRate,
519
- },
520
- repo: {
521
- tokensIn: repo.tokensSaved + repo.totalTokenEstimate,
522
- tokensOut: repo.totalTokenEstimate,
523
- tokensFreed: repo.tokensSaved,
524
- compressionPct:
525
- repo.tokensSaved + repo.totalTokenEstimate > 0
526
- ? repo.tokensSaved / (repo.tokensSaved + repo.totalTokenEstimate)
527
- : 0,
528
- dedupPct: repo.storageDedupRate,
529
- },
530
- },
531
- repo: {
532
- checkpointCount: repo.checkpointCount,
533
- totalTokenEstimate: repo.totalTokenEstimate,
534
- originalTokens: repo.originalTokens,
535
- tokensSaved: repo.tokensSaved,
536
- sessionCount: repo.sessionCount,
537
- dedupAttempts: repo.dedupAttempts,
538
- dedupCollapsed: repo.dedupCollapsed,
539
- storageDedupRate: repo.storageDedupRate,
540
- },
541
- integrity: {
542
- regionsRetained: di.regionsRetained,
543
- compressedOriginalBytes: di.compressedOriginalBytes,
544
- duplicatesCollapsed: di.duplicatesCollapsed,
545
- bytesPermanentlyDeleted: di.bytesPermanentlyDeleted,
546
- },
547
- cacheHits: {
548
- session: cacheHitsSession,
549
- total: cacheHitsTotal,
550
- sessionTokensSaved: this.rt.cacheHitTokens,
551
- totalTokensSaved: cacheHitsTotalTokens,
552
- },
553
- compacts: {
554
- session: this.rt.compactCount,
555
- total: getCompactCount(this.currentStateDir),
556
- },
557
- timeSaved: {
558
- compact: { sessionSec: sec(this.rt.tokensSaved), totalSec: sec(vectorRepoStats(this.store).tokensSaved) },
559
- cacheHit: { sessionSec: sec(this.rt.cacheHitTokens), totalSec: sec(cacheHitsTotalTokens) },
560
- },
561
- model,
562
- // S38.8: error-retry state for the dashboard "retries" tile. The field is
563
- // declared on DashboardSnapshot (mega-dashboard.ts) and surfaced here so the
564
- // dashboard can render live retry/circuit-breaker status alongside the event
565
- // stream (which already carries per-retry events).
566
- retries: {
567
- errorRetryCount: this.rt.errorRetryCount,
568
- consecutiveErrors: this.rt.consecutiveErrors,
569
- maxConsecutiveErrors: this.config.maxConsecutiveErrors,
570
- errorRetryHardStop: this.config.errorRetryHardStop,
571
- },
572
- diag: {
573
- ctxFastGate: this.diagCtxFastGate,
574
- liveTrimFires: this.diagLiveTrimFires,
575
- liveTrimReplays: this.diagLiveTrimReplays,
576
- },
577
- } as DashboardSnapshot);
578
- const perfDiskMs = this.dashboard.lastWriteMs;
579
-
580
- // S39: record a session heartbeat + token sample into the shared
581
- // machine-wide index.sqlite so the dashboard can show a real-time
582
- // stacked-memory graph across all active pi processes. Behind the
583
- // material-change gate (this code only runs when sig changed). Non-fatal
584
- // try/catch mirrors the recordPerfSample pattern below. Skip the token
585
- // sample when lastCtxTokens is null (no context data yet).
586
- try {
587
- const repo = resolveRepoRoot(ctx?.cwd ?? this.currentStateDir) ?? this.currentStateDir;
588
- recordSessionHeartbeat(
589
- process.pid,
590
- this.rt.sessionId,
591
- repo,
592
- this.currentStateDir,
593
- this.lastCtxWindow || 0,
594
- );
595
- if (this.lastCtxTokens != null) {
596
- appendTokenSample(
597
- this.rt.sessionId,
598
- repo,
599
- this.lastCtxTokens,
600
- this.lastCtxPercent ?? 0,
601
- this.lastCtxWindow || 0,
602
- join(this.currentStateDir, "events.log"),
603
- );
604
- }
605
- } catch {
606
- /* non-fatal: S39 monitoring must never block the snapshot path */
607
- }
608
-
609
- // Live stats widget above the editor
610
- if (ctx) {
611
- // ── gather widget data (computed per snapshot, rendered per frame) ────
612
- const tokStr =
613
- this.lastCtxTokens != null
614
- ? `${Math.round(this.lastCtxTokens / 1000)}k`
615
- : "?";
616
- const maxStr =
617
- this.lastCtxWindow > 0
618
- ? `${Math.round(this.lastCtxWindow / 1000)}k`
619
- : "?";
620
- const pctStr =
621
- this.lastCtxPercent != null
622
- ? this.lastCtxPercent > 100
623
- ? `>100%` // S29: overshoot warning, not a raw "250%" — the percent trigger now compacts before 100%, so this is the residual case where it can't keep up.
624
- : `${Math.round(this.lastCtxPercent * 10) / 10}%`
625
- : "?%";
626
- // S24: the tier label is the LIVE pressure band (low/medium/high/ultra/
627
- // mega), not the static env preset. It climbs as context fills.
628
- const liveBand = this.pressureBand;
629
- const tierLabel = `${C.bold}${liveBand}${C.reset}${C.gray}·${this.config.tier}${C.reset}`;
630
- const triggerLabel = ready
631
- ? `${C.green}● ready${C.reset}`
632
- : armed
633
- ? `${C.amber}◐ armed${C.reset}`
634
- : `${C.gray}○ idle${C.reset}`;
635
- // Storage dedup rate is cumulative (store-wide, per-repo) and survives
636
- // session resets. Always show a number (decimal for sub-10%).
637
- const storageRate = st.storageDedupRate; // 0..1
638
- const dedupStr =
639
- storageRate * 100 >= 10
640
- ? `${Math.round(storageRate * 100)}%`
641
- : `${(storageRate * 100).toFixed(1)}%`;
642
- // Agents view: count + status (S27 per-agent tokens are gated on P0).
643
- const agentLabel =
644
- this.activeAgents > 0
645
- ? `🤖 ${this.activeAgents} agent${this.activeAgents === 1 ? "" : "s"}`
646
- : `${C.dim}🤖 idle${C.reset}`;
647
- const agentStr = ` │ ${agentLabel}`;
648
- const turnStr = this.currentTurn > 0 ? ` │ turn ${this.currentTurn}` : "";
649
- // Reconciled in/out view (session + repo) — ONE canonical formula.
650
- const sessIn = this.rt.tokensSaved + st.totalTokenEstimate;
651
- const sessKept = st.totalTokenEstimate;
652
- const sessPct = sessIn > 0 ? this.rt.tokensSaved / sessIn : 0;
653
- const repoIn = repo.tokensSaved + repo.totalTokenEstimate;
654
- const repoKept = repo.totalTokenEstimate;
655
- const repoPct = repoIn > 0 ? repo.tokensSaved / repoIn : 0;
656
- const sTxt = (sessPct * 100).toFixed(sessPct * 100 >= 10 ? 0 : 1);
657
- const rTxt = (repoPct * 100).toFixed(repoPct * 100 >= 10 ? 0 : 1);
658
- const ctxPct =
659
- this.lastCtxPercent != null ? this.lastCtxPercent / 100 : 0;
660
- // Model + provider (S26 capture) for the header.
661
- const modelName = modelSnap?.modelName ?? modelSnap?.modelId ?? "?";
662
- const modelStr = modelSnap?.provider
663
- ? `${modelName}·${modelSnap.provider}`
664
- : modelName;
665
- // Since-last-compact (ms; null until first compaction this session).
666
- const sinceCompact =
667
- this.rt.lastCompactAt != null
668
- ? Date.now() - this.rt.lastCompactAt
669
- : null;
670
- // Memory store: embedder + compression ratio (original / stored).
671
- const embedderName = this.embedderName();
672
- const compRatio =
673
- st.originalTokens > 0 && st.totalTokenEstimate > 0
674
- ? st.originalTokens / st.totalTokenEstimate
675
- : st.originalTokens > 0
676
- ? 1
677
- : 0;
678
- const compStr = compRatio >= 1 ? `${compRatio.toFixed(1)}x` : "—";
679
- // Cross-repo drift status (cached, read-only).
680
- const driftStatus = this.driftStatus();
681
- const agentsActive = this.activeAgents > 0;
682
-
683
- // S31: game-mode state for the widget (theme/mode/level + MEGA CACHE).
684
- // Pulled from the cached game_state row; cachePct is the REAL dedup hit
685
- // rate (may exceed 100% — that's the MEGA CACHE trigger). megaCacheFlare
686
- // is false for now (S33.4 scoring hook arms it when cachePct > 100).
687
- const gs = this.getCachedGameState();
688
- // S34: derive the level-up flare from the turn count each snapshot.
689
- const curLevel = this.getTurnLevel();
690
- if (curLevel > this.lastLevel) {
691
- this.levelUpFlare = true;
692
- // v0.8.3: arm a pulse border effect to celebrate the level-up.
693
- this.setEffect("pulse", "accent", 1500);
694
- }
695
- const cachePct = st.dedupHitRate * 100;
696
- this.widgetData = {
697
- version: ownVersion(),
698
- tierLabel,
699
- triggerLabel,
700
- pctStr,
701
- tokStr,
702
- maxStr,
703
- ctxPct,
704
- chk: st.checkpointCount,
705
- agentStr,
706
- turnStr,
707
- dedupStr,
708
- sessIn,
709
- sessKept,
710
- sTxt,
711
- repoIn,
712
- repoKept,
713
- rTxt,
714
- repoChk: repo.checkpointCount,
715
- repoSess: repo.sessionCount,
716
- modelStr,
717
- sinceCompact,
718
- embedderName,
719
- compStr,
720
- driftStatus,
721
- agentsActive,
722
- fresh: Date.now() - this.lastActivityAt < 4000,
723
- ticker: this.ticker,
724
- lastWhy: this.lastWhy,
725
- tierTrace: this.tierTrace,
726
- pulsing: this.pulsing,
727
- // S31 game-mode fields:
728
- gameMode: gs.game_mode_on,
729
- theme: getTheme(gs.theme) ? gs.theme : "transparent",
730
- tuiMode: gs.tui_display_mode,
731
- level: this.getTurnLevel(),
732
- cachePct,
733
- megaCacheFlare: this.megaCacheFlare,
734
- megaCacheFlarePct: this.megaCacheFlarePct,
735
- levelUpFlare: this.levelUpFlare,
736
- achievementFlare: this.achievementFlare,
737
- achievementFlareTitles: this.achievementFlareTitles,
738
- // v0.8.3: ambient border effect — threaded live so the widget can
739
- // compute the per-frame phase and render animated borders.
740
- activeEffect: this.activeEffect,
741
- };
742
- // S33: consume the flare after copying it into widgetData so it fires
743
- // for exactly one render cycle (the gag flares once, then clears).
744
- this.megaCacheFlare = false;
745
- this.megaCacheFlarePct = 0;
746
-
747
- // S34: consume the level-up flare after one render cycle (mirrors the
748
- // megaCacheFlare one-shot semantics), and advance lastLevel.
749
- this.levelUpFlare = false;
750
- this.lastLevel = curLevel;
751
- // S35: consume the achievement-unlock flare after one render cycle
752
- // (mirrors the megaCacheFlare/levelUpFlare one-shot semantics).
753
- this.achievementFlare = false;
754
- this.achievementFlareTitles = [];
755
- // v0.8.3: expire the ambient border effect once its time window has
756
- // elapsed. SEPARATE from the one-shot flares above (those are per-cycle
757
- // consumes; activeEffect is time-windowed and cleared when Date.now()
758
- // crosses startedAt + durationMs). The widget also defends this per-frame
759
- // (effectBorderSgr returns '' once expired), so this is bookkeeping to
760
- // free the slot and prevent a stale effect lingering between snapshots.
761
- if (
762
- this.activeEffect &&
763
- Date.now() - this.activeEffect.startedAt >=
764
- this.activeEffect.durationMs
765
- ) {
766
- this.activeEffect = null;
767
- }
768
- // Auto-fit: register a factory so pi re-renders the panel at the REAL
769
- // terminal width every frame (tui.columns), instead of guessing with
770
- // process.stdout.columns. buildWidgetLines reads this.widgetData live.
771
- this.renderWidget(ctx);
772
- }
773
- // v0.8.5: record the material-change signature computed at the top so the
774
- // next snapshot() can skip this whole body when nothing material changed.
775
- try {
776
- recordPerfSample(this.currentStateDir, "db_recompute_ms", performance.now() - perfT0);
777
- recordPerfSample(this.currentStateDir, "disk_write_ms", perfDiskMs);
778
- } catch {
779
- /* non-fatal: perf instrumentation never blocks the agent */
780
- }
781
- this.lastSnapshotSig = sig;
782
- }
783
-
784
- /** Register the above-editor widget as a width-aware factory so pi re-renders
785
- * it at the REAL terminal width every frame (auto-fit wide/narrow). The
786
- * factory returns a minimal Component whose render() reads this.widgetData.
787
- */
788
- private renderWidget(ctx: ExtensionContext): void {
789
- ctx.ui.setWidget(
790
- WIDGET_KEY,
791
- (_tui, _theme) => ({
792
- render: (width: number) =>
793
- buildWidgetLines(
794
- this.widgetData,
795
- width > 0 ? width : 200,
796
- this.activeAgents,
797
- ),
798
- invalidate: () => {},
799
- }),
800
- { placement: "aboveEditor" },
801
- );
802
- }
803
-
804
- /** v0.8.5: cheap material-change signature over live runtime fields (no
805
- * SQLite). Two snapshots with the same signature produce identical
806
- * dashboard.json + widgetData, so the 6 synchronous SQLite opens + the
807
- * writeFileSync(dashboard.json) can be skipped. Built from in-memory state
808
- * only; gameStateBump covers cross-process game_state edits (fs.watch) +
809
- * in-process /mega-game writes (bumpGameState) + repo switches (bindRepo).
810
- * The transient flare flags are included so a one-shot flare forces the
811
- * recompute that renders (then clears) it for exactly one cycle. */
812
- private materialSig(): string {
813
- const rt = this.rt;
814
- const ae = this.activeEffect;
815
- return JSON.stringify([
816
- this.lastCtxTokens, this.lastCtxPercent, this.lastCtxWindow,
817
- this.activeAgents, this.currentTurn,
818
- rt.compactCount, rt.tokensSaved, rt.dedupSkips, rt.dedupAttempts,
819
- rt.recallInjections, rt.cacheHitTokens, rt.persistedThisSession,
820
- rt.lastCheckpointId ?? null, rt.lastCompactedFrom, rt.lastCompactedTokens,
821
- this.statusKey ?? null,
822
- this.currentModel?.modelId ?? null, this.currentModel?.provider ?? null,
823
- ae ? `${ae.type}:${ae.role}:${ae.startedAt}` : null,
824
- this.gameStateBump,
825
- this.megaCacheFlare, this.megaCacheFlarePct,
826
- this.levelUpFlare, this.achievementFlare,
827
- this.achievementFlareTitles.join("|"),
828
- this.tierTrace ?? null, this.lastWhy ?? null, this.pulsing,
829
- this.ticker.length,
830
- ]);
831
- }
832
-
833
- /** Active embedder name for the memory-store line (Trigram default / MiniLM). */
834
- private embedderName(): string {
835
- // MINILM_EMBEDDER flag lives in src/config/dedup.ts; read the same env var
836
- // the embedder factory uses so the label matches what's actually running.
837
- return process.env.MEGACOMPACT_MINILM === "true" ||
838
- process.env.MEGACOMPACT_MINILM === "1"
839
- ? "MiniLM"
840
- : "Trigram";
841
- }
842
-
843
- /** Cross-repo drift status (ok | warn), cached for 30s (opens the registry DB). */
844
- private driftStatus(): "ok" | "warn" {
845
- const now = Date.now();
846
- if (this.driftCache && now - this.driftCache.at < 30_000)
847
- return this.driftCache.status;
848
- let status: "ok" | "warn" = "ok";
849
- try {
850
- const report = detectCrossRepoDrift();
851
- status = report.totals.warn > 0 ? "warn" : "ok";
852
- } catch {
853
- status = "ok";
854
- }
855
- this.driftCache = { at: now, status };
856
- return status;
857
- }
858
-
859
- setStatus(ctx: ExtensionContext, text: string | undefined): void {
860
- this.statusKey = text;
861
- ctx.ui.setStatus(STATUS_KEY, text);
862
- }
863
-
864
- resetRuntime(sessionId: string | undefined): void {
865
- const sid = normalizeSessionId(sessionId);
866
- if (this.rt.sessionId === sid && this.rt.persistedThisSession) return; // same session, keep checkpoint memory
867
- this.rt = {
868
- sessionId: sid,
869
- persistedThisSession: false,
870
- lastCheckpointId: undefined,
871
- lastCompactedFrom: 0,
872
- lastCompactedTokens: 0,
873
- dedupSkips: 0,
874
- dedupAttempts: 0,
875
- tokensSaved: 0,
876
- lastCompactAt: null,
877
- lastNativeCompactAt: null,
878
- compactCount: 0,
879
- recallInjections: 0,
880
- cacheHitTokens: 0,
881
- lengthStopPending: false,
882
- errorRetryCount: 0,
883
- errorRetryUntil: 0,
884
- consecutiveErrors: 0,
885
- };
886
- this.trimCache = null; // v0.8.6: never replay a stale trim into a new session
887
- this.statusKey = undefined;
888
- this.activeAgents = 0;
889
- this.currentTurn = 0;
890
- this.lastActivityAt = 0;
891
- this.tierTrace = undefined;
892
- this.ticker.length = 0;
893
- this.pulsing = false;
894
- this.savedGoal = 50_000;
895
- this.lastWhy = undefined;
896
- // S31 audit P2: symmetry with bindRepo — a reset can coincide with a context
897
- // that re-binds the repo, so drop the memo too. Cheap; the next
898
- // getCachedGameState() re-queries lazily.
899
- this.cachedGameState = undefined;
900
- }
901
-
902
- /**
903
- * Capture the active model/provider from ctx.model and persist it so cost
904
- * estimation + the dashboard can read real pricing. Cheap + idempotent-ish:
905
- * only writes a new row when the model id changes (models change rarely).
906
- */
907
- captureModel(ctx: ExtensionContext): void {
908
- const m = ctx.model;
909
- if (!m) {
910
- this.appendEvent("captureModel:no-model", { cwd: ctx.cwd });
911
- return;
912
- }
913
- if (
914
- this.currentModel &&
915
- this.currentModel.modelId === m.id &&
916
- this.currentModel.provider === m.provider
917
- )
918
- return;
919
- let providerName: string | null = null;
920
- try {
921
- providerName =
922
- ctx.modelRegistry?.getProviderDisplayName(m.provider) ?? null;
923
- } catch {
924
- /* optional */
925
- }
926
- const snap: Omit<ModelSnapshot, "capturedAt"> = {
927
- provider: m.provider,
928
- providerName,
929
- modelId: m.id,
930
- modelName: m.name ?? null,
931
- inputRate: m.cost?.input ?? 0,
932
- outputRate: m.cost?.output ?? 0,
933
- contextWindow: m.contextWindow ?? 0,
934
- maxTokens: m.maxTokens ?? 0,
935
- reasoning: !!m.reasoning,
936
- };
937
- this.currentModel = { ...snap, capturedAt: Date.now() };
938
- this.diagCaptureModelCalls++;
939
- const repo = resolveRepoRoot(ctx.cwd) ?? this.currentStateDir;
940
- // S26: previously a single silent `catch {}` hid every capture failure, so
941
- // model_snapshots stayed empty and the cost card read $0.00 with zero signal.
942
- // Split per-write + append to events.log (always-on, dashboard live-streams
943
- // it) + bump a DIAG counter so a live capture surfaces the root cause.
944
- try {
945
- recordModelSnapshot(repo, snap, this.currentStateDir);
946
- this.appendEvent("captureModel:recorded", {
947
- repo,
948
- modelId: snap.modelId,
949
- provider: snap.provider,
950
- inputRate: snap.inputRate,
951
- outputRate: snap.outputRate,
952
- });
953
- } catch (e) {
954
- this.diagCaptureModelFails++;
955
- this.appendEvent("captureModel:record-failed", {
956
- repo,
957
- modelId: snap.modelId,
958
- error: e instanceof Error ? e.message : String(e),
959
- stack: e instanceof Error ? e.stack : undefined,
960
- });
961
- }
962
- try {
963
- // Denormalize the active model into the machine-wide index so the
964
- // All-repos dashboard table can show provider/model per repo without
965
- // opening every repo's DB. Best-effort + non-fatal.
966
- recordRepoModel(repo, {
967
- provider: snap.provider,
968
- providerName: snap.providerName,
969
- modelName: snap.modelName,
970
- inputRate: snap.inputRate,
971
- outputRate: snap.outputRate,
972
- stateDir: this.currentStateDir,
973
- displayName: repo.split(/[\\/]/).filter(Boolean).pop() ?? repo,
974
- });
975
- } catch (e) {
976
- this.appendEvent("captureModel:index-record-failed", {
977
- repo,
978
- modelId: snap.modelId,
979
- error: e instanceof Error ? e.message : String(e),
980
- });
981
- }
982
- }
983
-
984
- /**
985
- * Append a structured line to the repo's events.log — the always-on
986
- * diagnostics sink the dashboard live-streams. Unlike this.logger (gated by
987
- * config.debug), this fires in production, so capture failures surface during
988
- * a real capture even with debugging off. Best-effort + non-fatal.
989
- */
990
- private appendEvent(event: string, fields: Record<string, unknown>): void {
991
- try {
992
- mkdirSync(this.currentStateDir, { recursive: true });
993
- appendFileSync(
994
- join(this.currentStateDir, "events.log"),
995
- JSON.stringify({ ts: Date.now(), event, ...fields }) + "\n",
996
- );
997
- } catch {
998
- /* non-fatal */
999
- }
1000
- }
1001
-
1002
- /** S21: state dir of the currently bound repo (where memories live). */
1003
- getStateDir(): string {
1004
- return this.currentStateDir;
1005
- }
1006
-
1007
- /** S32: (re)target the fs.watch cache-eviction watcher at the current
1008
- * stateDir's sqlite.db. Called from the constructor + every bindRepo repo
1009
- * switch so the watcher always tracks the NEW repo's db file. If a watcher
1010
- * already exists for this dir, no-op; if the dir changed, close the old one
1011
- * first. fs.watch can throw on a missing file / platform issues — wrapped
1012
- * non-fatal; the next getCachedGameState() re-queries the DB anyway. */
1013
- private ensureGameStateWatcher(): void {
1014
- if (this.gameStateWatcher && this.gameStateWatchDir === this.currentStateDir) {
1015
- return;
1016
- }
1017
- if (this.gameStateWatcher) {
1018
- try { this.gameStateWatcher.close(); } catch { /* non-fatal */ }
1019
- this.gameStateWatcher = undefined;
1020
- this.gameStateWatchDir = undefined;
1021
- }
1022
- try {
1023
- // Watch the state DIR (not just sqlite.db) and filter by filename.
1024
- // Why: the store is WAL-mode (openStore sets PRAGMA journal_mode=WAL).
1025
- // Cross-process writes (dashboard server child) append to sqlite.db-wal
1026
- // and do NOT modify sqlite.db until a checkpoint — and a long-lived
1027
- // parent connection (VectorStore + dashboard readers) keeps the WAL
1028
- // uncheckpointed, so a watcher on sqlite.db alone never fires and
1029
- // cachedGameState stays stale (theme stuck after a dashboard edit).
1030
- // Watching the dir + matching sqlite.db* catches the main db, the -wal
1031
- // sidecar, and -shm, so the memo evicts on any cross-process write. The
1032
- // filter also excludes events.log / *.log noise in the same dir.
1033
- this.gameStateWatcher = watch(
1034
- this.currentStateDir,
1035
- (_eventType, filename) => {
1036
- if (typeof filename === "string" && filename.startsWith("sqlite.db")) {
1037
- this.cachedGameState = undefined;
1038
- this.gameStateBump++;
1039
- // P2: force a widget re-render so a dashboard-made theme/toggle/
1040
- // tui-mode change reflects in the live TUI immediately, even when
1041
- // pi is idle (no context event to drive snapshot()). Use the
1042
- // LIGHTWEIGHT refreshWidgetGameState() — NOT the full snapshot():
1043
- // snapshot() recomputes 6 sync SQLite opens + writes dashboard.json
1044
- // + writes to the store, and those store writes RETRIGGER this
1045
- // same fs.watch callback (it fires on every sqlite.db* write) →
1046
- // re-entrant thrash → 190s test timeout under mega-compact.test.js
1047
- // / mega-teamrun.test.js. The lightweight path re-reads ONLY the
1048
- // game_state row and patches the three game-mode fields on the
1049
- // existing widgetData, then re-registers the factory via
1050
- // renderWidget() — it writes nothing to the store or
1051
- // dashboard.json, so it cannot retrigger itself. Guard: skip until
1052
- // the first snapshot stashed a ctx (no widget registered yet →
1053
- // nothing to refresh). Non-fatal: next context event re-snapshots.
1054
- const ctx = this.lastWidgetCtx;
1055
- if (ctx) {
1056
- try {
1057
- this.refreshWidgetGameState(ctx);
1058
- } catch {
1059
- /* non-fatal */
1060
- }
1061
- }
1062
- }
1063
- },
1064
- );
1065
- this.gameStateWatchDir = this.currentStateDir;
1066
- } catch {
1067
- /* non-fatal: missing dir / platform issue — next snapshot re-queries */
1068
- }
1069
- }
1070
-
1071
- /** S32: release the fs.watch game-state watcher. Called when the runtime is
1072
- * torn down (no existing dispose path — the process exit reclaims the fd,
1073
- * but explicit close is correct for any in-process reload / test reuse). */
1074
- dispose(): void {
1075
- if (this.gameStateWatcher) {
1076
- try { this.gameStateWatcher.close(); } catch { /* non-fatal */ }
1077
- this.gameStateWatcher = undefined;
1078
- this.gameStateWatchDir = undefined;
1079
- }
1080
- // v0.8.8: stop the cpu/mem sampling interval on teardown. Re-armed lazily
1081
- // by ensurePerfInterval() on the next turn_start.
1082
- if (this.perfCpuInterval) {
1083
- clearInterval(this.perfCpuInterval);
1084
- this.perfCpuInterval = undefined;
1085
- this.perfCpuBaseline = undefined;
1086
- }
1087
- }
1088
-
1089
- /** v0.8.8: (re)start the 5s cpu/mem sampling interval (idempotent). One per
1090
- * MegaRuntime; cleared in dispose(). Samples process.cpuUsage() (user/sys
1091
- * delta vs the last tick → ms) + process.memoryUsage() (rss/heap → MB) and
1092
- * records them as perf_samples. unref'd so it never keeps the process alive
1093
- * on its own. Non-fatal: any failure is swallowed (instrumentation never
1094
- * blocks the agent). PREVENT-PI-004: local process stats + SQLite only. */
1095
- ensurePerfInterval(): void {
1096
- if (this.perfCpuInterval) return;
1097
- this.perfCpuBaseline = undefined; // first tick sets the baseline (no delta)
1098
- this.perfCpuInterval = setInterval(() => {
1099
- try {
1100
- const dir = this.currentStateDir;
1101
- const cpu = process.cpuUsage();
1102
- const mem = process.memoryUsage();
1103
- if (this.perfCpuBaseline) {
1104
- const du = (cpu.user - this.perfCpuBaseline.user) / 1000; // μs → ms
1105
- const ds = (cpu.system - this.perfCpuBaseline.sys) / 1000;
1106
- recordPerfSample(dir, "cpu_user_ms", Math.max(0, du));
1107
- recordPerfSample(dir, "cpu_sys_ms", Math.max(0, ds));
1108
- }
1109
- this.perfCpuBaseline = { user: cpu.user, sys: cpu.system };
1110
- recordPerfSample(dir, "rss_mb", mem.rss / 1_000_000);
1111
- recordPerfSample(dir, "heap_mb", mem.heapUsed / 1_000_000);
1112
- } catch {
1113
- /* non-fatal */
1114
- }
1115
- }, 5000);
1116
- this.perfCpuInterval.unref?.();
1117
- }
1118
-
1119
- /** S31: the cached game-mode state (game_mode_on/theme/tui_display_mode).
1120
- * Lazily read from the game_state SQLite row on the first call, then
1121
- * memoized until `bumpGameState()` evicts it. Reading is non-throwing
1122
- * (getGameState returns DEFAULT_GAME_STATE on any error), so the widget
1123
- * can call this on every render safely. */
1124
- getCachedGameState(): GameState {
1125
- if (!this.cachedGameState) {
1126
- try {
1127
- this.cachedGameState = getGameState(this.currentStateDir);
1128
- } catch {
1129
- this.cachedGameState = {
1130
- game_mode_on: false,
1131
- theme: "transparent",
1132
- tui_display_mode: "full",
1133
- };
1134
- }
1135
- }
1136
- return this.cachedGameState;
1137
- }
1138
-
1139
- /** P2 cross-process re-render: lightweight game-state refresh for the
1140
- * fs.watch callback. Eviction of cachedGameState + gameStateBump++ happens
1141
- * in the caller BEFORE this runs. Here we re-read ONLY the game_state row
1142
- * via getCachedGameState() (one SELECT; the cache is already evicted) and
1143
- * patch ONLY the three game-mode fields on the EXISTING widgetData, then
1144
- * re-register the widget factory via renderWidget() so pi redraws next
1145
- * frame.
1146
- *
1147
- * WHY a lightweight path: the full snapshot(ctx) recomputes 6 synchronous
1148
- * SQLite opens + writeFileSync(dashboard.json) + store writes, and those
1149
- * store writes RETRIGGER this same fs.watch callback → re-entrant thrash
1150
- * (the watcher fires on every sqlite.db* write, including context-event
1151
- * checkpoint writes) → 190s test timeout under mega-compact.test.js /
1152
- * mega-teamrun.test.js. This path writes NOTHING to the store or
1153
- * dashboard.json, so it cannot retrigger itself.
1154
- *
1155
- * Guard: no-op when widgetData is null (no snapshot has run yet → nothing
1156
- * to patch) or ctx is undefined. Field values mirror snapshot() exactly. */
1157
- refreshWidgetGameState(ctx: ExtensionContext): void {
1158
- if (!this.widgetData || !ctx) return;
1159
- const gs = this.getCachedGameState();
1160
- this.widgetData.gameMode = gs.game_mode_on;
1161
- this.widgetData.theme = getTheme(gs.theme) ? gs.theme : "transparent";
1162
- this.widgetData.tuiMode = gs.tui_display_mode;
1163
- this.renderWidget(ctx);
1164
- }
1165
-
1166
- /** S31: evict the cached game-mode state so the next widget render re-reads
1167
- * the game_state row. Called by /mega-game after every setGameState() so
1168
- * the panel picks up theme/mode/toggle changes live. */
1169
- bumpGameState(): void {
1170
- this.cachedGameState = undefined;
1171
- this.gameStateBump++;
1172
- }
1173
-
1174
- /** S33: player level for game mode — floor(log2(turns+1))+1 (gentle).
1175
- * Defensive: non-finite/negative collapses to 1 (never NaN). */
1176
- private getTurnLevel(): number {
1177
- return turnLevel(this.currentTurn);
1178
- }
1179
-
1180
- /** S33: arm the transient MEGA CACHE flare so the next snapshot() copies it
1181
- * into widgetData and the widget renders the oopsie gag for one cycle.
1182
- * v0.8.3: also arm a 'flash' ambient effect on the panel borders (mega
1183
- * color) for 1.2s. */
1184
- armMegaCacheFlare(peakPct: number): void {
1185
- this.megaCacheFlare = true;
1186
- this.megaCacheFlarePct = peakPct;
1187
- this.setEffect("flash", "mega", 1200);
1188
- }
1189
-
1190
- /** S35: arm the transient achievement-unlock flare with the newly-unlocked
1191
- * titles so the next snapshot() copies them into widgetData and the widget
1192
- * renders the one-time unlock toast for one render cycle.
1193
- * v0.8.3: also arm a 'pulse' ambient effect on the panel borders (accent
1194
- * color) for 2s to celebrate the unlock. */
1195
- armAchievementFlare(titles: string[]): void {
1196
- this.achievementFlare = true;
1197
- this.achievementFlareTitles = titles;
1198
- this.setEffect("pulse", "accent", 2000);
1199
- }
1200
-
1201
- /** v0.8.3: arm an ambient border effect (animated pulse/flash on the panel
1202
- * borders). Replaces any in-flight effect (last call wins — a later event
1203
- * like a level-up during an achievement pulse simply overrides). The widget
1204
- * reads activeEffect each frame and computes the per-frame phase from
1205
- * startedAt vs Date.now(); it renders '' once the window elapses. */
1206
- setEffect(
1207
- type: "pulse" | "flash",
1208
- role: "accent" | "mega" | "red",
1209
- durationMs: number,
1210
- ): void {
1211
- this.activeEffect = { type, role, startedAt: Date.now(), durationMs };
1212
- }
1213
-
1214
- /** Build the sync onTier callback that paints the live per-tier trace. */
1215
- makeTierCallback(
1216
- ctx: ExtensionContext,
1217
- ): (ev: {
1218
- tier: "L0" | "L1" | "L2" | "new";
1219
- status: "scanning" | "deduped" | "passed" | "stored";
1220
- detail?: string;
1221
- }) => void {
1222
- const order: Array<"L0" | "L1" | "L2" | "new"> = ["L0", "L1", "L2", "new"];
1223
- const seen = new Map<string, string>();
1224
- const glyph = (status: string) =>
1225
- status === "deduped"
1226
- ? `${C.green}✓${C.reset}`
1227
- : status === "passed"
1228
- ? `${C.dim}○${C.reset}`
1229
- : status === "scanning"
1230
- ? `${C.amber}…${C.reset}`
1231
- : `${C.cyan}●${C.reset}`;
1232
- return (ev) => {
1233
- const label =
1234
- ev.tier === "new"
1235
- ? `${C.cyan}stored${C.reset}`
1236
- : `${ev.tier} ${glyph(ev.status)}` +
1237
- (ev.detail ? ` ${C.gray}(${ev.detail})${C.reset}` : "");
1238
- // Show the most recent outcome per tier (collapses re-fires).
1239
- seen.set(ev.tier, label);
1240
- const show: string[] = [];
1241
- for (const t of order) if (seen.has(t)) show.push(seen.get(t)!);
1242
- this.tierTrace = `${C.teal}⚙${C.reset} ${show.join(` ${C.gray}→${C.reset} `)}`;
1243
- this.lastActivityAt = Date.now();
1244
- try {
1245
- this.snapshot(ctx);
1246
- } catch {
1247
- /* non-fatal */
1248
- }
1249
- };
1250
- }
1251
-
1252
- // Phase 3 — recall/activity ticker ring buffer.
1253
- pushTicker(text: string): void {
1254
- // P1: dedupe consecutive identical entries — skip the append when the
1255
- // last entry's text matches, so a re-fired compact/recall/dedup event
1256
- // doesn't flood the ring (keeps it at TICKER_MAX for real variety).
1257
- // `at` is NOT refreshed on a skip (the original event time stands).
1258
- if (this.ticker[this.ticker.length - 1]?.text === text) {
1259
- this.lastActivityAt = Date.now();
1260
- return;
1261
- }
1262
- this.ticker.push({ text, at: Date.now() });
1263
- while (this.ticker.length > this.TICKER_MAX) this.ticker.shift();
1264
- this.lastActivityAt = Date.now();
1265
- }
1266
-
1267
- /** Convert the messages pi hands us in the `context` event into the engine view. */
1268
- engineView(messages: AgentMessage[]): ReturnType<typeof toEngineMessages> {
1269
- return toEngineMessages(messages);
1270
- }
1271
- }
8
+ export { MegaRuntime } from "./runtime.js";