pi-mega-compact 0.4.14 → 0.4.16

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.
@@ -12,1081 +12,27 @@
12
12
  * - Message drops must preserve an anchor floor (PREVENT-PI-001) and never
13
13
  * split a toolCall/toolResult pair (PREVENT-PI-002).
14
14
  *
15
- * Sprint 3 wires: config, session state reset, the auto-trigger pipeline
16
- * (fast-gate auto_compact_check Trident+persist → context drop),
17
- * session_before_compact cancellation, the compact-marker sentinel, and the
18
- * /megacompact + /megacompact-status commands.
15
+ * The extension is split into focused modules under extensions/mega-*.ts:
16
+ * - mega-config.ts tiers, env helpers, loadConfig, per-repo scoping
17
+ * - mega-dashboard.ts live snapshot writer (dashboard.json / events.log)
18
+ * - mega-runtime.ts shared live state (MegaRuntime) + widget + model capture
19
+ * - mega-pipeline.ts runCompact (Trident+persist) + doRecall (Layer 5)
20
+ * - mega-commands.ts data/inspection slash commands
21
+ * - mega-dashboard-cmds.ts localhost dashboard server lifecycle commands
22
+ * - mega-events.ts pi lifecycle event handlers
19
23
  *
20
- * Sprint 4 wires the unified recall layer (Layer 5): recallAndInline() is the
21
- * ONLY code path that injects compacted context. It serves three entry points —
22
- * auto-inline on resume/branch (before_agent_start), on-demand /recall-context,
23
- * and the dedup sentinel — all through one dedup engine, injected via the
24
- * before_agent_start systemPrompt prepend (PREVENT-PI-003).
24
+ * This file is the thin wiring layer: it owns the default export, constructs
25
+ * the runtime, and registers handlers/commands. Behavior is unchanged.
25
26
  */
26
- import { sessionEntryToContextMessages } from "@earendil-works/pi-coding-agent";
27
- import { join, dirname, sep } from "node:path";
28
- import { fileURLToPath } from "node:url";
29
- import { STATE_DIR_DEFAULT } from "../src/config.js";
30
- import { VectorStore } from "../src/vectorStore.js";
31
- import { toEngineMessages, dropCompactedRange } from "../src/adapt.js";
32
- import { compactSession } from "../src/engine.js";
33
- import { recallAndInline } from "../src/recall.js";
34
- import { autoCompactCheck } from "../src/compact.js";
35
- import { estimateSessionTokens } from "../src/tokens.js";
36
- import { normalizeSessionId } from "../src/store.js";
37
- import { touchSession, logDaily, listCheckpoints } from "../src/store/sqlite.js";
38
- import { decompressSmart } from "../src/store/compression.js";
39
- import { loadMetrics, fpRate, p95 } from "../src/monitoring.js";
40
- import { Logger } from "../src/log.js";
41
- import { writeFileSync, appendFileSync, readFileSync } from "node:fs";
42
- import { existsSync, mkdirSync, unlinkSync } from "node:fs";
43
- import { spawn } from "node:child_process"; // guardrails-allow PREVENT-PI-004: spawns the optional, user-triggered localhost dashboard server only
44
- import { execSync } from "node:child_process"; // guardrails-allow PREVENT-PI-004: read-only `git rev-parse` to scope the store per-repo
45
- const STATUS_KEY = "mega-compact";
46
- const WIDGET_KEY = "mega-compact-stats";
47
- const MARKER_TYPE = "mega-compact-marker";
48
- function envFlag(name, fallback) {
49
- const v = process.env[name];
50
- if (v == null || v === "")
51
- return fallback;
52
- const n = Number(v);
53
- return Number.isFinite(n) ? n : fallback;
54
- }
55
- function envBool(name, fallback) {
56
- const v = process.env[name];
57
- if (v == null || v === "")
58
- return fallback;
59
- return v === "true" || v === "1";
60
- }
61
- /**
62
- * Named compaction tiers. A tier sets the token threshold at which the
63
- * auto-trigger persists a checkpoint; pick by how aggressively you want the
64
- * session trimmed. Explicit MEGACOMPACT_THRESHOLD_TOKENS always wins.
65
- */
66
- const COMPACT_TIERS = {
67
- low: 50_000,
68
- medium: 100_000,
69
- high: 200_000,
70
- ultra: 1_000_000,
71
- mega: 10_000_000,
72
- };
73
- /** Resolve the effective token threshold from TIER (or explicit) env vars. */
74
- function resolveThreshold() {
75
- const explicit = process.env.MEGACOMPACT_THRESHOLD_TOKENS;
76
- if (explicit != null && explicit !== "") {
77
- const n = Number(explicit);
78
- if (Number.isFinite(n))
79
- return { tier: "custom", thresholdTokens: n };
80
- }
81
- const raw = (process.env.MEGACOMPACT_TIER ?? "low").toLowerCase();
82
- const tier = (raw in COMPACT_TIERS ? raw : "low");
83
- return { tier, thresholdTokens: COMPACT_TIERS[tier] };
84
- }
85
- function loadConfig() {
86
- const { tier, thresholdTokens } = resolveThreshold();
87
- return {
88
- tier,
89
- // Global default; the live store/dashboard are rebound per-repo at runtime
90
- // via bindRepo() so each git repo gets its own isolated state dir.
91
- stateDir: process.env.MEGACOMPACT_STATE_DIR ?? STATE_DIR_DEFAULT,
92
- fastGatePct: envFlag("MEGACOMPACT_FAST_GATE_PCT", 70),
93
- thresholdTokens,
94
- anchorUserMessages: envFlag("MEGACOMPACT_ANCHOR_USER_MESSAGES", 3),
95
- preserveRecent: envFlag("MEGACOMPACT_PRESERVE_RECENT", 4),
96
- auto: envBool("MEGACOMPACT_AUTO", true),
97
- autoInline: envBool("MEGACOMPACT_AUTO_INLINE", true),
98
- autoInlineK: envFlag("MEGACOMPACT_AUTO_INLINE_K", 3),
99
- dedupSim: Number(process.env.MEGACOMPACT_DEDUP_SIM ?? "0.9"),
100
- debug: envBool("MEGACOMPACT_DEBUG", false),
101
- };
102
- }
103
- /**
104
- * Resolve the current repo's git root from a cwd. Returns undefined for a
105
- * non-git directory (caller falls back to a global state dir).
106
- */
107
- function resolveRepoRoot(cwd) {
108
- try {
109
- const out = execSync("git rev-parse --show-toplevel", {
110
- cwd,
111
- encoding: "utf-8",
112
- stdio: ["ignore", "pipe", "ignore"],
113
- }).trim();
114
- return out || undefined;
115
- }
116
- catch {
117
- return undefined;
118
- }
119
- }
120
- /**
121
- * Per-repo state dir: <repo>/.pi/mega-compact (tracked, so it travels with the
122
- * repo across devices — not gitignored). Falls back to `fallback` for non-git
123
- * cwds (the explicit MEGACOMPACT_STATE_DIR override, if set).
124
- */
125
- function repoStateDir(cwd, fallback) {
126
- const root = resolveRepoRoot(cwd);
127
- if (!root)
128
- return fallback;
129
- return join(root, ".pi", "mega-compact");
130
- }
131
- class Dashboard {
132
- snapshotPath;
133
- eventsPath;
134
- constructor(stateDir) {
135
- if (!existsSync(stateDir))
136
- mkdirSync(stateDir, { recursive: true });
137
- this.snapshotPath = join(stateDir, "dashboard.json");
138
- this.eventsPath = join(stateDir, "events.log");
139
- }
140
- /** Write a full state snapshot (atomically replaces previous). */
141
- snapshot(data) {
142
- writeFileSync(this.snapshotPath, JSON.stringify(data, null, 2) + "\n");
143
- }
144
- /** Append a timestamped JSONL event line. */
145
- event(type, data) {
146
- const line = JSON.stringify({ ts: new Date().toISOString(), type, ...data });
147
- appendFileSync(this.eventsPath, line + "\n");
148
- }
149
- }
150
- /** Convert the messages pi hands us in the `context` event into the engine view. */
151
- function engineView(messages) {
152
- return toEngineMessages(messages);
153
- }
27
+ import { loadConfig } from "./mega-config.js";
28
+ import { MegaRuntime } from "./mega-runtime.js";
29
+ import { registerEventHandlers } from "./mega-events.js";
30
+ import { registerCommands } from "./mega-commands.js";
31
+ import { registerDashboardCommands } from "./mega-dashboard-cmds.js";
154
32
  export default function (pi) {
155
33
  const config = loadConfig();
156
- // Store/dashboard/logger are rebound per-repo by bindRepo() (below) so each
157
- // git repo gets its own isolated state dir. They start bound to the global
158
- // default until the first handler resolves a cwd.
159
- let store = new VectorStore({ dedupSim: config.dedupSim, stateDir: config.stateDir });
160
- let logger = new Logger({ enabled: config.debug, path: join(config.stateDir, "mega-compact.log") });
161
- let dashboard = new Dashboard(config.stateDir);
162
- let activeRepoRoot = null;
163
- let currentStateDir = config.stateDir;
164
- /**
165
- * Point store/dashboard/logger at the current repo's state dir. Rebuilds the
166
- * instances only when the repo root changes, so cross-repo dedup stats, db,
167
- * and events are fully isolated. Falls back to the global default outside git.
168
- */
169
- function bindRepo(cwd) {
170
- const dir = cwd ? repoStateDir(cwd, config.stateDir) : config.stateDir;
171
- const key = cwd ? resolveRepoRoot(cwd) ?? dir : dir;
172
- if (key === activeRepoRoot)
173
- return dir;
174
- activeRepoRoot = key;
175
- currentStateDir = dir;
176
- store = new VectorStore({ dedupSim: config.dedupSim, stateDir: dir });
177
- logger = new Logger({ enabled: config.debug, path: join(dir, "mega-compact.log") });
178
- dashboard = new Dashboard(dir);
179
- return dir;
180
- }
181
- // --- snapshot() helper: collect live state and write it to disk ---
182
- let lastCtxTokens = null;
183
- let lastCtxPercent = null;
184
- let lastCtxWindow = 0;
185
- function snapshot(ctx) {
186
- if (ctx)
187
- bindRepo(ctx.cwd);
188
- const st = store.stats(rt.sessionId);
189
- const repo = store.repoStats();
190
- const di = store.dataInvariant();
191
- const armed = lastCtxPercent != null && lastCtxPercent >= config.fastGatePct;
192
- const ready = armed && (lastCtxTokens ?? 0) >= config.thresholdTokens;
193
- dashboard.snapshot({
194
- version: 1,
195
- updatedAt: new Date().toISOString(),
196
- tier: config.tier,
197
- config: {
198
- fastGatePct: config.fastGatePct,
199
- thresholdTokens: config.thresholdTokens,
200
- anchorUserMessages: config.anchorUserMessages,
201
- preserveRecent: config.preserveRecent,
202
- auto: config.auto,
203
- autoInline: config.autoInline,
204
- },
205
- session: {
206
- id: rt.sessionId,
207
- state: statusKey ?? "idle",
208
- persistedThisSession: rt.persistedThisSession,
209
- lastCheckpointId: rt.lastCheckpointId ?? null,
210
- lastCompactedFrom: rt.lastCompactedFrom,
211
- lastCompactedTokens: rt.lastCompactedTokens,
212
- dedupSkips: rt.dedupSkips,
213
- dedupAttempts: rt.dedupAttempts,
214
- },
215
- context: { tokens: lastCtxTokens, percent: lastCtxPercent, contextWindow: lastCtxWindow },
216
- trigger: { armed, ready, currentTokens: lastCtxTokens, thresholdTokens: config.thresholdTokens, fastGatePct: config.fastGatePct },
217
- crew: { activeAgents, currentTurn },
218
- store: { checkpointCount: st.checkpointCount, totalTokenEstimate: st.totalTokenEstimate, originalTokens: st.originalTokens, tokensSaved: rt.tokensSaved, injectedCount: st.injectedCount, dedupHitRate: st.dedupHitRate, storageDedupRate: st.storageDedupRate, dedupAttempts: st.dedupAttempts, dedupCollapsed: st.dedupCollapsed },
219
- repo: {
220
- checkpointCount: repo.checkpointCount,
221
- totalTokenEstimate: repo.totalTokenEstimate,
222
- originalTokens: repo.originalTokens,
223
- tokensSaved: repo.tokensSaved,
224
- sessionCount: repo.sessionCount,
225
- dedupAttempts: repo.dedupAttempts,
226
- dedupCollapsed: repo.dedupCollapsed,
227
- storageDedupRate: repo.storageDedupRate,
228
- },
229
- integrity: {
230
- regionsRetained: di.regionsRetained,
231
- compressedOriginalBytes: di.compressedOriginalBytes,
232
- duplicatesCollapsed: di.duplicatesCollapsed,
233
- bytesPermanentlyDeleted: di.bytesPermanentlyDeleted,
234
- },
235
- });
236
- // Live stats widget above the editor
237
- if (ctx) {
238
- const tokStr = lastCtxTokens != null ? `${Math.round(lastCtxTokens / 1000)}k` : "?";
239
- const maxStr = lastCtxWindow > 0 ? `${Math.round(lastCtxWindow / 1000)}k` : "?";
240
- const pctStr = lastCtxPercent != null ? `${Math.round(lastCtxPercent * 10) / 10}%` : "?%";
241
- const triggerLabel = ready ? `${C.green}● ready${C.reset}` : armed ? `${C.amber}◐ armed${C.reset}` : `${C.gray}○ idle${C.reset}`;
242
- // Storage dedup rate is cumulative (store-wide, per-repo) and survives
243
- // session resets. Always show a number: 0% before any compaction, a
244
- // decimal for sub-10% rates so small-but-real dedup isn't rounded away.
245
- const storageRate = st.storageDedupRate; // 0..1
246
- const dedupStr = storageRate * 100 >= 10
247
- ? `${Math.round(storageRate * 100)}%`
248
- : `${(storageRate * 100).toFixed(1)}%`;
249
- // saved = tokens removed from context (cumulative original − stored).
250
- // Show BOTH this-session (rt.tokensSaved) and repo-wide-total
251
- // (repo.tokensSaved) so the user sees per-session progress vs the running
252
- // repo total. "used" = stored checkpoint tokens (repo.totalTokenEstimate
253
- // vs st.totalTokenEstimate). Use "k" only at/above 1000 so small-but-real
254
- // numbers stay visible (previously Math.round(x/1000) zeroed <1000).
255
- const fmt = (x) => (x >= 1000 ? `${(x / 1000).toFixed(1)}k` : `${x}`);
256
- const savedStr = `${C.green}${fmt(rt.tokensSaved)} sess${C.reset} / ${C.blue}${fmt(repo.tokensSaved)} repo${C.reset}`;
257
- const usedStr = `${C.cyan}${fmt(st.totalTokenEstimate)} sess${C.reset} / ${C.blue}${fmt(repo.totalTokenEstimate)} repo${C.reset}`;
258
- const agentStr = activeAgents > 0 ? ` │ 🤖 ${activeAgents} agent${activeAgents === 1 ? "" : "s"}` : "";
259
- const turnStr = currentTurn > 0 ? ` │ turn ${currentTurn}` : "";
260
- // Phase 3 — pulsing status glyph while a compaction is in flight.
261
- const pulse = pulsing ? `${C.cyan}${PULSE[Math.floor(Date.now() / 250) % PULSE.length]}${C.reset} ` : "";
262
- const lines = [
263
- ` ${C.amber}⚡ ${config.tier}${C.reset} │ ${tokStr}/${maxStr} tokens (${C.bold}${pctStr}${C.reset}) │ ${st.checkpointCount} chkpt${agentStr}${turnStr}`,
264
- ` ${triggerLabel} │ ${C.magenta}dedup: ${dedupStr}${C.reset} │ ${C.gray}used:${C.reset} ${usedStr} │ ${C.gray}saved:${C.reset} ${savedStr}`,
265
- ];
266
- // Phase 3 — compact progress bar: session tokens saved toward the rolling goal.
267
- if (rt.tokensSaved > 0) {
268
- const goal = Math.max(savedGoal, 1);
269
- const pct = Math.min(100, Math.round((rt.tokensSaved / goal) * 100));
270
- const filled = Math.round((pct / 100) * 10);
271
- const bar = "▓".repeat(filled) + "░".repeat(10 - filled);
272
- lines.push(` ${C.green}saved ${fmt(rt.tokensSaved)} ${bar}${C.reset} ${pct}% of ${fmt(goal)}`);
273
- }
274
- // Live "now processing" line — teal while fresh (≤4s), then the last-seen
275
- // action keeps the widget lively. Cleared on session reset.
276
- const fresh = Date.now() - lastActivityAt < 4000;
277
- if (tierTrace && fresh) {
278
- lines.push(` ${pulse}${tierTrace}`);
279
- }
280
- else if (currentActivity) {
281
- lines.push(` ${fresh ? C.teal : C.dim}${currentActivity}${C.reset}`);
282
- }
283
- else if (pulsing) {
284
- lines.push(` ${pulse}${C.teal}compacting…${C.reset}`);
285
- }
286
- // Phase 3 — explain-why line (fresh only).
287
- if (lastWhy && fresh)
288
- lines.push(` ${C.gray}${lastWhy}${C.reset}`);
289
- // Phase 3 — recall/activity ticker (most-recent first), fresh only.
290
- if (fresh) {
291
- for (let i = ticker.length - 1; i >= 0; i--) {
292
- if (lines.length >= 10)
293
- break; // MAX_WIDGET_LINES guard
294
- lines.push(` ${i === ticker.length - 1 ? "" : C.dim}${ticker[i].text}${C.reset}`);
295
- }
296
- }
297
- ctx.ui.setWidget(WIDGET_KEY, lines, { placement: "aboveEditor" });
298
- }
299
- }
300
- // The only mutable per-session state. Reset on session_start / session_tree.
301
- let rt = {
302
- sessionId: normalizeSessionId(undefined),
303
- persistedThisSession: false,
304
- lastCheckpointId: undefined,
305
- lastCompactedFrom: 0,
306
- lastCompactedTokens: 0,
307
- dedupSkips: 0,
308
- dedupAttempts: 0,
309
- tokensSaved: 0,
310
- };
311
- let debounceUntil = 0;
312
- // Agent tracking for real-time widget updates
313
- let activeAgents = 0;
314
- let currentTurn = 0;
315
- // Recall block produced by auto-inline (resume/branch) that the next
316
- // before_agent_start should prepend to the system prompt. Unset after use.
317
- let pendingRecallBlock;
318
- let statusKey; // current status text for dashboard
319
- // Live "what it's doing right now" line for the toolbar. Set on each
320
- // compaction; shown in teal while recent, then kept as the last-seen action so
321
- // the widget is never blank. Cleared on session reset.
322
- let currentActivity;
323
- let lastActivityAt = 0;
324
- // Live per-tier dedup trace (Phase 1): e.g. "L0 ✓ → L1 ✓ → L2 0.91 → stored".
325
- // Built from the store's sync onTier callback during a compaction so the user
326
- // watches each tier evaluate in real time. Cleared once the outcome settles.
327
- let tierTrace;
328
- const ticker = [];
329
- const TICKER_MAX = 5;
330
- function pushTicker(text) {
331
- ticker.push({ text, at: Date.now() });
332
- while (ticker.length > TICKER_MAX)
333
- ticker.shift();
334
- lastActivityAt = Date.now();
335
- }
336
- // Pulsing status: set true while a compaction is in flight, cleared on result.
337
- let pulsing = false;
338
- // Rolling "saved" goal for the progress bar — grows as we save more, so the
339
- // bar always has a meaningful denominator (never sits at 100% forever).
340
- let savedGoal = 50_000;
341
- // Last explain-why line (dedup reason / anchor-kept / superseded), surfaced
342
- // while fresh.
343
- let lastWhy = undefined;
344
- // Cycling glyph phases for the pulsing status.
345
- const PULSE = ["◐", "◓", "◑", "◒"];
346
- // ANSI palette for the toolbar. The pi TUI's Text component preserves ANSI
347
- // escape codes (see wrapTextWithAnsi), so raw escapes render as colors. No
348
- // chalk dependency needed — these are just strings.
349
- const C = {
350
- reset: "\x1b[0m",
351
- dim: "\x1b[2m",
352
- bold: "\x1b[1m",
353
- amber: "\x1b[38;5;214m", // tier / ready
354
- green: "\x1b[38;5;120m", // saved
355
- cyan: "\x1b[38;5;51m", // used / live activity
356
- teal: "\x1b[38;5;37m", // processing (compress/dedup)
357
- magenta: "\x1b[38;5;201m", // dedup rate
358
- blue: "\x1b[38;5;75m", // repo totals
359
- gray: "\x1b[38;5;245m", // labels
360
- };
361
- function setStatus(ctx, text) {
362
- statusKey = text;
363
- ctx.ui.setStatus(STATUS_KEY, text);
364
- }
365
- function resetRuntime(sessionId) {
366
- const sid = normalizeSessionId(sessionId);
367
- if (rt.sessionId === sid && rt.persistedThisSession)
368
- return; // same session, keep checkpoint memory
369
- rt = {
370
- sessionId: sid,
371
- persistedThisSession: false,
372
- lastCheckpointId: undefined,
373
- lastCompactedFrom: 0,
374
- lastCompactedTokens: 0,
375
- dedupSkips: 0,
376
- dedupAttempts: 0,
377
- tokensSaved: 0,
378
- };
379
- statusKey = undefined;
380
- activeAgents = 0;
381
- currentTurn = 0;
382
- currentActivity = undefined;
383
- lastActivityAt = 0;
384
- tierTrace = undefined;
385
- ticker.length = 0;
386
- pulsing = false;
387
- savedGoal = 50_000;
388
- lastWhy = undefined;
389
- }
390
- /** Build the sync onTier callback that paints the live per-tier trace. */
391
- function makeTierCallback(ctx) {
392
- const order = ["L0", "L1", "L2", "new"];
393
- const seen = new Map();
394
- const glyph = (status) => status === "deduped" ? `${C.green}✓${C.reset}` :
395
- status === "passed" ? `${C.dim}○${C.reset}` :
396
- status === "scanning" ? `${C.amber}…${C.reset}` :
397
- `${C.cyan}●${C.reset}`;
398
- return (ev) => {
399
- const label = ev.tier === "new"
400
- ? `${C.cyan}stored${C.reset}`
401
- : `${ev.tier} ${glyph(ev.status)}` +
402
- (ev.detail ? ` ${C.gray}(${ev.detail})${C.reset}` : "");
403
- // Show the most recent outcome per tier (collapses re-fires).
404
- seen.set(ev.tier, label);
405
- const show = [];
406
- for (const t of order)
407
- if (seen.has(t))
408
- show.push(seen.get(t));
409
- tierTrace = `${C.teal}⚙${C.reset} ${show.join(` ${C.gray}→${C.reset} `)}`;
410
- lastActivityAt = Date.now();
411
- try {
412
- snapshot(ctx);
413
- }
414
- catch { /* non-fatal */ }
415
- };
416
- }
417
- /** Run the full compaction pipeline and persist a checkpoint. Returns the result. */
418
- function runCompact(ctx, messages, opts = {}) {
419
- bindRepo(ctx.cwd);
420
- const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
421
- resetRuntime(sid);
422
- rt.sessionId = sid;
423
- const view = engineView(messages);
424
- const keepFrom = opts.keepFrom ?? Math.max(0, view.length - config.preserveRecent);
425
- if (keepFrom <= 0)
426
- return { skipped: true };
427
- pulsing = true; // animate the status line while the (sync) pipeline runs
428
- const result = compactSession({
429
- sessionId: sid,
430
- messages: view,
431
- keepFrom,
432
- summary: opts.summary,
433
- timestamp: Date.now(),
434
- onTier: makeTierCallback(ctx),
435
- }, store);
436
- pulsing = false;
437
- if (result.skipped)
438
- return { skipped: true };
439
- if (!result.deduped) {
440
- rt.persistedThisSession = true;
441
- rt.lastCheckpointId = result.checkpointId;
442
- }
443
- rt.lastCompactedFrom = result.compactedFrom;
444
- rt.lastCompactedTokens = result.tokenEstimate;
445
- rt.dedupAttempts++;
446
- // Honest "tokens saved" for this session-instance only:
447
- // new checkpoint → original − stored
448
- // deduped onto existing → whole original region (nothing new stored)
449
- // Resets to 0 on session_start (rt is rebuilt) — so a fresh session shows 0
450
- // while the repo's cumulative saved (SQLite meta) keeps the running total.
451
- const saved = result.deduped
452
- ? result.originalTokenEstimate
453
- : Math.max(0, result.originalTokenEstimate - result.tokenEstimate);
454
- rt.tokensSaved += saved;
455
- if (result.deduped)
456
- rt.dedupSkips++;
457
- // Grow the rolling "saved" goal so the progress bar always has a fresh
458
- // denominator (we don't want it pinned at 100% once we pass an old target).
459
- if (rt.tokensSaved > savedGoal)
460
- savedGoal = Math.ceil((rt.tokensSaved * 1.25) / 10_000) * 10_000;
461
- // Live toolbar "now processing" line: what file/region just got compacted or
462
- // deduped. Reset to the last-seen action after a few seconds (see snapshot).
463
- const files = result.filesModified ?? [];
464
- const fileLabel = files.length
465
- ? files.map((f) => f.split("/").pop() ?? f).slice(0, 2).join(", ")
466
- : result.regionHash.slice(0, 8);
467
- currentActivity = result.deduped
468
- ? `♻ deduped ${fileLabel}`
469
- : `🗜 compacted ${result.checkpointId} · ${fileLabel}`;
470
- lastActivityAt = Date.now();
471
- // Explain-why line: surfaced while fresh. Pulls the dedup reason (which for
472
- // L2 includes the cosine sim) so the user sees WHY a region was kept/dropped.
473
- lastWhy = result.deduped
474
- ? `why: deduped@${result.dedupReason ?? "tier"}`
475
- : `why: compacted → ${result.checkpointId}`;
476
- // Recall/activity ticker: record this event in the ring buffer.
477
- const savedK = (saved / 1000).toFixed(1);
478
- pushTicker(result.deduped
479
- ? `${C.green}♻${C.reset} deduped ${fileLabel} · ${savedK}k saved`
480
- : `${C.cyan}🗜${C.reset} ${result.checkpointId} · +${savedK}k · ${fileLabel}`);
481
- // The per-tier trace has settled into the final outcome — fold it back into
482
- // the activity line and stop showing the live trace.
483
- tierTrace = undefined;
484
- // Record session activity + a daily-log entry in the per-repo SQLite store
485
- // (foundation for resume-sessions / daily-log features). Best-effort — never
486
- // block a compaction on bookkeeping.
487
- try {
488
- const repo = resolveRepoRoot(ctx.cwd);
489
- touchSession(sid, repo, currentStateDir);
490
- logDaily(sid, "compact", result.checkpointId, saved, currentStateDir);
491
- }
492
- catch {
493
- /* non-fatal: stats bookkeeping only */
494
- }
495
- // Sentinel marker: a non-LLM bookkeeping entry so subsequent triggers can
496
- // skip re-vectorizing an already-compacted region (zero token cost).
497
- pi.appendEntry(MARKER_TYPE, {
498
- checkpointId: result.checkpointId,
499
- regionHash: result.regionHash,
500
- tokenEstimate: result.tokenEstimate,
501
- deduped: result.deduped,
502
- });
503
- setStatus(ctx, rt.persistedThisSession
504
- ? `mega-compact: ${result.checkpointId} · ${saved} tok saved`
505
- : `mega-compact: ready`);
506
- logger.info("compact", {
507
- sessionId: sid,
508
- checkpointId: result.checkpointId ?? "(deduped)",
509
- deduped: result.deduped,
510
- tokenEstimate: saved,
511
- compactedFrom: result.compactedFrom,
512
- });
513
- dashboard.event("compact", {
514
- sessionId: sid,
515
- checkpointId: result.checkpointId ?? "(deduped)",
516
- deduped: result.deduped,
517
- tokenEstimate: saved,
518
- compactedFrom: result.compactedFrom,
519
- });
520
- snapshot(ctx);
521
- return { skipped: false, result, keepFrom, saved };
522
- }
523
- /**
524
- * Unified recall (Layer 5). The ONE path that injects. Returns the recall
525
- * result; callers decide whether to stage it for before_agent_start (resume)
526
- * or report it (command).
527
- */
528
- function doRecall(ctx, query, source) {
529
- bindRepo(ctx.cwd);
530
- const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
531
- const result = recallAndInline({ sessionId: sid, query, limit: config.autoInlineK, source, skipInjected: true }, store);
532
- dashboard.event("recall", { source, query: query.slice(0, 120), injected: result.toInject.length, empty: result.empty });
533
- if (!result.empty && result.toInject.length > 0) {
534
- const top = result.toInject[0];
535
- const scorePct = Math.round((top.score ?? 0) * 100);
536
- const files = top.checkpoint.filesModified ?? [];
537
- const label = files.length ? files.map((f) => f.split("/").pop() ?? f).slice(0, 2).join(", ") : top.checkpoint.checkpointId;
538
- pushTicker(`${C.amber}↩${C.reset} recalled ${top.checkpoint.checkpointId} · ${scorePct}% · ${label}`);
539
- lastWhy = `why: recalled@${scorePct}% (${result.toInject.length} chkpt)`;
540
- }
541
- return result;
542
- }
543
- // ---- Session lifecycle (state reset points) -------------------------------
544
- pi.on("session_start", async (event, ctx) => {
545
- resetRuntime(ctx.sessionManager.getSessionId());
546
- setStatus(ctx, config.auto ? "mega-compact: ready" : "mega-compact: manual only");
547
- // Auto-inline on resume/fork/continue: stage the most relevant checkpoints
548
- // so the next before_agent_start prepends them to the system prompt.
549
- // Triggered whenever this session already has persisted checkpoints AND a
550
- // usable query — that covers reason "resume"/"fork" (explicit) and
551
- // reason "startup" (e.g. `pi --continue`s an existing session, which still
552
- // emits "startup" but with a populated message window). A brand-new empty
553
- // session has no checkpoints, so it's naturally excluded.
554
- if (config.autoInline) {
555
- const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
556
- const query = recentUserQuery(ctx);
557
- if (query && store.stats(sid).checkpointCount > 0) {
558
- const r = doRecall(ctx, query, "resume");
559
- if (!r.empty) {
560
- pendingRecallBlock = r.block;
561
- setStatus(ctx, `mega-compact: recalled ${r.toInject.length} chkpt`);
562
- logger.info("auto-inline", { reason: event.reason, query, injected: r.toInject.map((h) => h.checkpoint.checkpointId) });
563
- }
564
- }
565
- }
566
- dashboard.event("session_start", { reason: event.reason, sessionId: rt.sessionId });
567
- snapshot(ctx);
568
- });
569
- pi.on("session_tree", async (_event, ctx) => {
570
- // Branch navigation invalidates region indexes — reset checkpoint memory but
571
- // keep the on-disk store (markers replayed from entries below if needed).
572
- resetRuntime(ctx.sessionManager.getSessionId());
573
- setStatus(ctx, "mega-compact: ready (branch)");
574
- if (config.autoInline) {
575
- const query = recentUserQuery(ctx);
576
- if (query) {
577
- const r = doRecall(ctx, query, "resume");
578
- if (!r.empty) {
579
- pendingRecallBlock = r.block;
580
- logger.info("auto-inline", { reason: "session_tree", query, injected: r.toInject.map((h) => h.checkpoint.checkpointId) });
581
- }
582
- }
583
- }
584
- dashboard.event("session_tree", { sessionId: rt.sessionId });
585
- snapshot(ctx);
586
- });
587
- // ---- Auto-inline injection point: prepend staged recall to systemPrompt ----
588
- pi.on("before_agent_start", async (event, _ctx) => {
589
- if (!pendingRecallBlock)
590
- return;
591
- const block = pendingRecallBlock;
592
- pendingRecallBlock = undefined; // one-shot: consume so we never double-inject
593
- return { systemPrompt: `${event.systemPrompt}\n\n${block}` };
594
- });
595
- pi.on("session_shutdown", async (_event, ctx) => {
596
- setStatus(ctx, undefined);
597
- activeAgents = 0;
598
- currentTurn = 0;
599
- ctx.ui.setWidget(WIDGET_KEY, [], { placement: "aboveEditor" });
600
- });
601
- // ---- Agent tracking for real-time widget + status-line updates ---------
602
- pi.on("agent_start", async (_event, ctx) => {
603
- activeAgents++;
604
- dashboard.event("agent_start", { activeAgents });
605
- // Surface live agent activity on the status line (toolbar), not just the
606
- // above-editor widget — otherwise concurrent agents look frozen.
607
- setStatus(ctx, `mega-compact: ▶ ${activeAgents} agent${activeAgents === 1 ? "" : "s"}`);
608
- snapshot(ctx);
609
- });
610
- pi.on("agent_end", async (_event, ctx) => {
611
- activeAgents = Math.max(0, activeAgents - 1);
612
- dashboard.event("agent_end", { activeAgents });
613
- if (activeAgents > 0) {
614
- setStatus(ctx, `mega-compact: ▶ ${activeAgents} agent${activeAgents === 1 ? "" : "s"}`);
615
- }
616
- else {
617
- setStatus(ctx, config.auto ? "mega-compact: ready" : "mega-compact: manual only");
618
- }
619
- snapshot(ctx);
620
- });
621
- pi.on("turn_start", async (event, ctx) => {
622
- currentTurn = event.turnIndex;
623
- dashboard.event("turn_start", { turnIndex: event.turnIndex });
624
- snapshot(ctx);
625
- });
626
- pi.on("turn_end", async (event, ctx) => {
627
- dashboard.event("turn_end", { turnIndex: event.turnIndex });
628
- snapshot(ctx);
629
- });
630
- // ---- Auto-trigger: fast-gate → confirm → Trident+persist → drop --------
631
- pi.on("context", async (event, ctx) => {
632
- if (!config.auto)
633
- return;
634
- const usage = ctx.getContextUsage();
635
- const pct = usage?.percent;
636
- // Always track context for the dashboard, even if we return early below.
637
- lastCtxTokens = usage?.tokens ?? null;
638
- lastCtxPercent = pct ?? null;
639
- lastCtxWindow = usage?.contextWindow ?? 0;
640
- snapshot(ctx);
641
- if (pct == null)
642
- return;
643
- const messages = event.messages;
644
- const view = engineView(messages);
645
- // Prefer the runtime's real token estimate; fall back to our heuristic
646
- // (and to a percent-of-window proxy when tokens is unknown).
647
- const currentTokens = usage?.tokens ?? estimateSessionTokens(view) ??
648
- Math.round((pct / 100) * (usage?.contextWindow ?? 0));
649
- // FAST GATE: token-based (tier threshold), not percentage-based.
650
- // A 20% gate on a 2M window = 400k, which is way above the 50k low-tier
651
- // threshold. Gate on the actual token count instead.
652
- if (currentTokens < config.thresholdTokens)
653
- return;
654
- const check = autoCompactCheck(currentTokens, config.thresholdTokens); // SERVER-STYLE CONFIRM (local)
655
- if (!check.shouldCompact)
656
- return;
657
- // Debounce so we don't fire on every context event past threshold.
658
- const now = Date.now();
659
- if (now < debounceUntil)
660
- return;
661
- debounceUntil = now + 2000;
662
- const ran = runCompact(ctx, messages);
663
- if (ran.skipped)
664
- return;
665
- // DROP the compacted range from the outgoing context, honoring the anchor
666
- // floor + tool-pair boundary guards (PREVENT-PI-001/002).
667
- const kept = dropCompactedRange(messages, ran.keepFrom, config.anchorUserMessages);
668
- if (kept.length < messages.length) {
669
- return { messages: kept };
670
- }
671
- });
672
- // ---- Cancel native compaction once we've persisted our own -------------
673
- pi.on("session_before_compact", async (_event, ctx) => {
674
- resetRuntime(ctx.sessionManager.getSessionId());
675
- if (rt.persistedThisSession) {
676
- // We already persisted a checkpoint for this session (via the context
677
- // hook drop) — cancel pi's own compaction to avoid double-compacting.
678
- // Our context-hook drop already trimmed the window.
679
- return { cancel: true };
680
- }
681
- // We haven't persisted yet this session: let pi run its native compaction.
682
- // (Our auto-trigger only fires again past the threshold, and will then
683
- // capture a checkpoint next time around.)
684
- return {};
685
- });
686
- // ---- Commands ----------------------------------------------------------
687
- pi.registerCommand("mega-compact", {
688
- description: "Compress current session context into the local vector store.",
689
- handler: async (args, ctx) => {
690
- const sessionEntries = ctx.sessionManager.getEntries();
691
- // Project entries (branch-aware) into the message view.
692
- const messages = sessionEntries.flatMap((e) => sessionEntryToContextMessages(e));
693
- const summaryArg = args.trim();
694
- const ran = runCompact(ctx, messages, summaryArg ? { summary: summaryArg } : {});
695
- if ("skipped" in ran && ran.skipped) {
696
- ctx.ui.notify("[mega-compact] Nothing to compact (session too small).");
697
- return;
698
- }
699
- const r = ran.result;
700
- ctx.ui.notify(`[mega-compact] ${r.deduped ? "region already compacted (deduped)" : `persisted ${r.checkpointId}`} · ` +
701
- `${r.tokenEstimate} tok · ${currentStateDir}`);
702
- },
703
- });
704
- pi.registerCommand("mega-recall", {
705
- description: "Recall relevant compacted context from the vector store and inline it.",
706
- handler: async (args, ctx) => {
707
- const query = args.trim() || recentUserQuery(ctx);
708
- if (!query) {
709
- ctx.ui.notify("[mega-compact] /mega-recall needs a query or a prior user message.");
710
- return;
711
- }
712
- const r = doRecall(ctx, query, "command");
713
- if (r.empty) {
714
- logger.info("recall-empty", { query });
715
- ctx.ui.notify(`[mega-compact] recall found nothing new for "${query}".`);
716
- return;
717
- }
718
- // Stage the block so the next before_agent_start prepends it (actual
719
- // injection). Report what was selected now for immediate feedback.
720
- pendingRecallBlock = r.block;
721
- const list = r.report.map((l) => l).join("\n");
722
- logger.info("recall", { query, injected: r.toInject.map((h) => h.checkpoint.checkpointId) });
723
- setStatus(ctx, `mega-compact: recalled ${r.toInject.length} chkpt`);
724
- ctx.ui.notify(`[mega-compact] recall staged ${r.toInject.length} checkpoint(s) for "${query}":\n${list}\n` +
725
- `(injected at the next turn via system prompt)`);
726
- },
727
- });
728
- pi.registerCommand("mega-status", {
729
- description: "Show mega-compact config, context usage, and the data-safety invariant.",
730
- handler: async (_args, ctx) => {
731
- bindRepo(ctx.cwd);
732
- const usage = ctx.getContextUsage();
733
- const pct = usage?.percent != null ? `${usage.percent}%` : "n/a";
734
- const tokens = usage?.tokens != null ? `${usage.tokens} tok` : "n/a";
735
- const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
736
- const st = store.stats(sid);
737
- const repo = store.repoStats();
738
- const di = store.dataInvariant();
739
- const fmtB = (b) => b >= 1_048_576 ? `${(b / 1_048_576).toFixed(1)} MiB` :
740
- b >= 1024 ? `${(b / 1024).toFixed(1)} KiB` : `${b} B`;
741
- // Tangible cost: turn "tokens saved" into a dollar figure + context-days
742
- // extended, so the counter is concrete rather than opaque. ~$3 / 1M tok
743
- // (rough blended rate); contextWindow ÷ savedRate = days of context bought.
744
- const usd = (repo.tokensSaved / 1_000_000 * 3).toFixed(2);
745
- const ctxWindow = usage?.contextWindow ?? 0;
746
- const daysExtended = ctxWindow > 0 && repo.tokensSaved > 0
747
- ? (repo.tokensSaved / ctxWindow).toFixed(1)
748
- : "0";
749
- const costStr = `≈ $${usd} saved · ${daysExtended} context-windows extended`;
750
- // Recall-quality badge (Phase 4): trust score from monitoring metrics.
751
- const m = loadMetrics(currentStateDir);
752
- const fp = fpRate(m, "L2");
753
- const p95L2 = p95(m.latency.L2 ?? []);
754
- const relPct = (st.dedupHitRate * 100).toFixed(0);
755
- const qualityStr = `recall ${relPct}% relevant · FP ${(fp * 100).toFixed(1)}% · L2 p95 ${p95L2.toFixed(0)}ms`;
756
- ctx.ui.notify(`[mega-compact] pct=${pct} tokens=${tokens} tier=${config.tier} fastGate=${config.fastGatePct}% ` +
757
- `threshold=${config.thresholdTokens} auto=${config.auto} autoInline=${config.autoInline}\n` +
758
- `[mega-compact] store: ${st.checkpointCount} chkpt · ` +
759
- `${st.totalTokenEstimate} tok · last=${st.lastCheckpointId ?? "—"} · ` +
760
- `injected=${st.injectedCount} · dedup=${(st.dedupHitRate * 100).toFixed(0)}%\n` +
761
- `[mega-compact] anchor=${config.anchorUserMessages} preserveRecent=${config.preserveRecent} ` +
762
- `autoInlineK=${config.autoInlineK} dedupSim=${config.dedupSim} debug=${config.debug}\n` +
763
- `[mega-compact] 🛡 data-safe: ${di.regionsRetained} regions retained ` +
764
- `(${fmtB(di.compressedOriginalBytes)} compressed-original) · ` +
765
- `${di.duplicatesCollapsed} dedup-duplicates collapsed · ` +
766
- `${C.green}0 bytes permanently deleted${C.reset}\n` +
767
- `[mega-compact] 💰 ${costStr}\n` +
768
- `[mega-compact] 🎯 ${qualityStr}\n` +
769
- `[mega-compact] stateDir=${currentStateDir}`);
770
- },
771
- });
772
- // ---- Phase 4: cheap standout commands (data is already persisted) -------
773
- /** Resolve a checkpoint by id (or "recent"/"last") from this session's store. */
774
- function findCheckpoint(sid, ref) {
775
- const all = listCheckpoints(sid, currentStateDir);
776
- if (all.length === 0)
777
- return undefined;
778
- if (!ref || ref === "recent" || ref === "last")
779
- return all[all.length - 1];
780
- return all.find((c) => c.checkpointId === ref) ?? all.find((c) => c.checkpointId.endsWith(ref));
781
- }
782
- pi.registerCommand("mega-restore", {
783
- description: "Re-inject a checkpoint's verbatim original region into context. Usage: /mega-restore <chkpt|recent>",
784
- handler: async (args, ctx) => {
785
- bindRepo(ctx.cwd);
786
- const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
787
- const cp = findCheckpoint(sid, args.trim());
788
- if (!cp) {
789
- ctx.ui.notify(`[mega-compact] no checkpoint found${args.trim() ? ` for "${args.trim()}"` : ""} in this session. Try /mega-history.`);
790
- return;
791
- }
792
- if (!cp.compressedOriginal) {
793
- ctx.ui.notify(`[mega-compact] ${cp.checkpointId} has no recoverable original (pre-blob or direct add). Cannot restore verbatim.`);
794
- return;
795
- }
796
- const original = decompressSmart(cp.compressedOriginal).toString("utf-8");
797
- // Re-inject verbatim via before_agent_start (PREVENT-PI-003) — never
798
- // touches live messages, only prepends the restored region to systemPrompt.
799
- pendingRecallBlock = `The following compacted context was RESTORED from checkpoint ${cp.checkpointId} (verbatim original region):\n\n${original}`;
800
- const files = cp.filesModified?.length ? cp.filesModified.join(", ") : "(no files captured)";
801
- ctx.ui.notify(`[mega-compact] ♻ restored ${cp.checkpointId} — ${original.length} chars re-injected on next turn.\n` +
802
- `[mega-compact] files: ${files}`);
803
- dashboard.event("restore", { checkpointId: cp.checkpointId, chars: original.length });
804
- },
805
- });
806
- pi.registerCommand("mega-history", {
807
- description: "List this session's checkpoints (id, date, files, tokens). Usage: /mega-history",
808
- handler: async (_args, ctx) => {
809
- bindRepo(ctx.cwd);
810
- const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
811
- const all = listCheckpoints(sid, currentStateDir);
812
- if (all.length === 0) {
813
- ctx.ui.notify("[mega-compact] no checkpoints in this session yet.");
814
- return;
815
- }
816
- const rows = all.map((c) => {
817
- const when = c.timestamp ? new Date(c.timestamp).toISOString().slice(0, 16).replace("T", " ") : "—";
818
- const files = c.filesModified?.length ? c.filesModified.map((f) => f.split("/").pop()).join(", ") : "—";
819
- const orig = c.originalTokenEstimate ?? 0;
820
- const stored = c.tokenEstimate ?? 0;
821
- const saved = Math.max(0, orig - stored);
822
- return ` ${c.checkpointId} ${when} ${C.cyan}${saved}t saved${C.reset} ${files}`;
823
- });
824
- ctx.ui.notify(`[mega-compact] ${all.length} checkpoint(s) in this session:\n` + rows.join("\n") +
825
- `\n[mega-compact] /mega-view <chkpt> to see the original region · /mega-restore <chkpt> to re-inject it`);
826
- },
827
- });
828
- pi.registerCommand("mega-view", {
829
- description: "Show a checkpoint's verbatim original region. Usage: /mega-view <chkpt|recent>",
830
- handler: async (args, ctx) => {
831
- bindRepo(ctx.cwd);
832
- const sid = normalizeSessionId(ctx.sessionManager.getSessionId());
833
- const cp = findCheckpoint(sid, args.trim());
834
- if (!cp) {
835
- ctx.ui.notify(`[mega-compact] no checkpoint found${args.trim() ? ` for "${args.trim()}"` : ""}. Try /mega-history.`);
836
- return;
837
- }
838
- if (!cp.compressedOriginal) {
839
- ctx.ui.notify(`[mega-compact] ${cp.checkpointId} summary:\n${cp.summary.slice(0, 500)}${cp.summary.length > 500 ? "…" : ""}\n(no verbatim original stored)`);
840
- return;
841
- }
842
- const original = decompressSmart(cp.compressedOriginal).toString("utf-8");
843
- ctx.ui.notify(`[mega-compact] ${cp.checkpointId} — original region (${original.length} chars):\n` +
844
- `${original.slice(0, 1500)}${original.length > 1500 ? "\n…(truncated)" : ""}`);
845
- },
846
- });
847
- pi.registerCommand("mega-tier", {
848
- description: "Show or change the compaction tier at runtime. Usage: /mega-tier [low|medium|high|ultra|mega]",
849
- handler: async (args, ctx) => {
850
- const arg = args.trim().toLowerCase();
851
- if (!arg) {
852
- // Show current tier and available options.
853
- ctx.ui.notify(`[mega-compact] current tier: ${config.tier} (${config.thresholdTokens} tok)\n` +
854
- `[mega-compact] available tiers: ${Object.entries(COMPACT_TIERS).map(([k, v]) => `${k}=${v}`).join(", ")}`);
855
- return;
856
- }
857
- if (!(arg in COMPACT_TIERS)) {
858
- ctx.ui.notify(`[mega-compact] unknown tier "${arg}". Available: ${Object.keys(COMPACT_TIERS).join(", ")}`);
859
- return;
860
- }
861
- const newTier = arg;
862
- config.tier = newTier;
863
- config.thresholdTokens = COMPACT_TIERS[newTier];
864
- setStatus(ctx, `mega-compact: tier → ${newTier} (${config.thresholdTokens} tok)`);
865
- ctx.ui.notify(`[mega-compact] tier changed to ${newTier} (threshold: ${config.thresholdTokens} tokens)`);
866
- snapshot(ctx);
867
- },
868
- });
869
- // ---- Dashboard server commands ----------------------------------------
870
- const portFile = join(currentStateDir, "port.pid");
871
- const runnerFile = join(currentStateDir, "_dashboard-runner.mjs");
872
- const launchLog = join(currentStateDir, "_dashboard-launch.log");
873
- // Whether the runner must be spawned with --experimental-strip-types (true only
874
- // when we fall back to the .ts source outside node_modules; false when using
875
- // the shipped compiled dist/extensions/dashboard-server.js).
876
- let dashboardNeedsStrip = false;
877
- // The dashboard server binds 9320–9329 (TARGET_PORT..TARGET_PORT+PORT_RANGE-1
878
- // in dashboard-server.js). Probe each for a live /api/snapshot so we can detect
879
- // readiness even when port.pid landed in a different state dir than we poll.
880
- async function findLivePort() {
881
- for (let port = 9320; port <= 9329; port++) {
882
- try {
883
- const res = await fetch(`http://localhost:${port}/api/snapshot`, { signal: AbortSignal.timeout(800) }); // guardrails-allow PREVENT-PI-004: localhost liveness probe of the dashboard server this extension spawned
884
- if (res.ok)
885
- return port;
886
- }
887
- catch { /* not on this port — try next */ }
888
- }
889
- return null;
890
- }
891
- /** Try to reach a running dashboard server. Returns { port, url } or null. */
892
- async function isServerRunning() {
893
- const port = await findLivePort();
894
- if (!port) {
895
- // Stale marker with no live server behind it — clean up.
896
- if (existsSync(portFile)) {
897
- try {
898
- unlinkSync(portFile);
899
- }
900
- catch { /* ignore */ }
901
- }
902
- return null;
903
- }
904
- return { port, url: `http://localhost:${port}` }; // guardrails-allow PREVENT-PI-004: localhost URL of the dashboard server this extension spawned
905
- }
906
- /**
907
- * Resolve the launchable dashboard-server module.
908
- *
909
- * CRITICAL: Node's `--experimental-strip-types` REFUSES to strip .ts files that
910
- * live under `node_modules` (ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING). Since
911
- * the published package installs under node_modules, importing the .ts source
912
- * fails in every real install (it only worked from a source checkout). So we
913
- * prefer the COMPILED dist/extensions/dashboard-server.js (which the package
914
- * ships from v0.4.6 — it imports only Node built-ins, so it runs standalone),
915
- * and only fall back to the .ts source (with strip-types) when the compiled
916
- * file is absent AND we're not under node_modules (dev checkout without a build).
917
- *
918
- * Returns { entry, needsStripTypes }.
919
- */
920
- function resolveDashboardEntry() {
921
- const here = dirname(fileURLToPath(import.meta.url)); // .../extensions
922
- const candidates = [
923
- // 1. Compiled sibling when running from dist/ (import.meta is dist/extensions/…js)
924
- { entry: join(here, "dashboard-server.js"), strip: false },
925
- // 2. Compiled under the package's dist/ when running from source extensions/…ts
926
- { entry: join(here, "..", "dist", "extensions", "dashboard-server.js"), strip: false },
927
- // 3. Last resort: the .ts source (only strippable OUTSIDE node_modules)
928
- { entry: join(here, "dashboard-server.ts"), strip: true },
929
- ];
930
- for (const c of candidates) {
931
- if (!existsSync(c.entry))
932
- continue;
933
- if (c.strip && c.entry.includes(`${sep}node_modules${sep}`))
934
- continue; // unstrippable
935
- return { entry: c.entry, needsStripTypes: c.strip };
936
- }
937
- return null;
938
- }
939
- /** Write a small ESM runner script that imports and launches the dashboard server. */
940
- function writeRunnerScript() {
941
- const resolved = resolveDashboardEntry();
942
- if (!resolved)
943
- return false;
944
- dashboardNeedsStrip = resolved.needsStripTypes;
945
- const script = [
946
- `import { appendFileSync } from "node:fs";`,
947
- `const __log = ${JSON.stringify(launchLog)};`,
948
- `function __fail(err) {`,
949
- ` const msg = "[mega-compact] dashboard failed: " + (err && err.stack ? err.stack : String(err));`,
950
- ` try { appendFileSync(__log, msg + "\\n"); } catch { /* ignore */ }`,
951
- ` console.error(msg);`,
952
- ` process.exit(1);`,
953
- `}`,
954
- `import { launchDashboardServer } from ${JSON.stringify(resolved.entry)};`,
955
- `launchDashboardServer(${JSON.stringify(currentStateDir)}).catch(__fail);`,
956
- ].join("\n");
957
- writeFileSync(runnerFile, script);
958
- return true;
959
- }
960
- /** Open a URL in the default browser. Platform-aware. Uses spawn (not exec) to avoid shell injection. */
961
- function openBrowser(url) {
962
- const cmd = process.platform === "darwin" ? "open" :
963
- process.platform === "win32" ? "start" :
964
- "xdg-open";
965
- try {
966
- spawn(cmd, [url], { detached: true, stdio: "ignore" }).unref();
967
- }
968
- catch {
969
- /* non-fatal — user can open manually */
970
- }
971
- }
972
- pi.registerCommand("mega-dashboard", {
973
- description: "Start the local web dashboard and optionally open it in the default browser.",
974
- handler: async (_args, ctx) => {
975
- bindRepo(ctx.cwd);
976
- let info = await isServerRunning();
977
- if (info) {
978
- ctx.ui.notify(`[mega-compact] dashboard already running at ${info.url}`);
979
- const open = await ctx.ui.confirm("mega-compact dashboard", `Open ${info.url} in browser?`);
980
- if (open)
981
- openBrowser(info.url);
982
- return;
983
- }
984
- // Start the server
985
- ctx.ui.notify("[mega-compact] starting dashboard server…");
986
- if (!writeRunnerScript()) {
987
- ctx.ui.notify("[mega-compact] dashboard entry not found — check logs.");
988
- return;
989
- }
990
- const args = dashboardNeedsStrip ? ["--experimental-strip-types", runnerFile] : [runnerFile];
991
- const child = spawn(process.execPath, args, {
992
- detached: true,
993
- stdio: "ignore",
994
- });
995
- child.unref();
996
- // Poll for a live server (port 9320–9329) instead of relying solely on the
997
- // port.pid marker, which can land in a different state dir than the one we
998
- // poll when a prior compact left currentStateDir pointing elsewhere.
999
- const deadline = Date.now() + 6_000;
1000
- let port = null;
1001
- while (Date.now() < deadline) {
1002
- await new Promise((resolve) => setTimeout(resolve, 300));
1003
- port = await findLivePort();
1004
- if (port)
1005
- break;
1006
- }
1007
- if (!port) {
1008
- let detail = "";
1009
- try {
1010
- const log = readFileSync(launchLog, "utf-8").trim();
1011
- if (log)
1012
- detail = ` — ${log.split("\n").slice(-3).join("; ")}`;
1013
- }
1014
- catch { /* no log yet */ }
1015
- ctx.ui.notify(`[mega-compact] dashboard server failed to start${detail}. See ${launchLog}`);
1016
- return;
1017
- }
1018
- const url = `http://localhost:${port}`; // guardrails-allow PREVENT-PI-004: localhost URL of the dashboard server this extension spawned
1019
- ctx.ui.notify(`[mega-compact] dashboard running at ${url}`);
1020
- const open = await ctx.ui.confirm("mega-compact dashboard", `Open ${url} in browser?`);
1021
- if (open)
1022
- openBrowser(url);
1023
- },
1024
- });
1025
- pi.registerCommand("mega-dashboard-stop", {
1026
- description: "Stop the local dashboard server.",
1027
- handler: async (_args, ctx) => {
1028
- if (!existsSync(portFile)) {
1029
- ctx.ui.notify("[mega-compact] no dashboard server running.");
1030
- return;
1031
- }
1032
- try {
1033
- const info = JSON.parse(readFileSync(portFile, "utf-8"));
1034
- // Verify the server is actually ours by probing the port before killing
1035
- try {
1036
- await fetch(`http://localhost:${info.port}/api/snapshot`, { signal: AbortSignal.timeout(1000) }); // guardrails-allow PREVENT-PI-004: localhost probe to verify the dashboard server is ours before stopping it
1037
- }
1038
- catch {
1039
- // Not responding — just clean up stale pid file
1040
- try {
1041
- unlinkSync(portFile);
1042
- }
1043
- catch { /* ok */ }
1044
- ctx.ui.notify("[mega-compact] dashboard was not running (stale pid file cleaned up).");
1045
- return;
1046
- }
1047
- if (info?.pid)
1048
- process.kill(info.pid, "SIGTERM");
1049
- }
1050
- catch { /* already dead */ }
1051
- try {
1052
- unlinkSync(portFile);
1053
- }
1054
- catch { /* ok */ }
1055
- ctx.ui.notify("[mega-compact] dashboard stopped.");
1056
- },
1057
- });
1058
- pi.registerCommand("mega-dashboard-status", {
1059
- description: "Check if the dashboard server is running.",
1060
- handler: async (_args, ctx) => {
1061
- const info = await isServerRunning();
1062
- if (info) {
1063
- ctx.ui.notify(`[mega-compact] dashboard running at ${info.url}`);
1064
- }
1065
- else {
1066
- ctx.ui.notify("[mega-compact] dashboard is not running. Use /dashboard to start it.");
1067
- }
1068
- },
1069
- });
1070
- }
1071
- /** Latest user message text — used as the auto-inline recall query. */
1072
- function recentUserQuery(ctx) {
1073
- try {
1074
- const entries = ctx.sessionManager.getEntries();
1075
- for (let i = entries.length - 1; i >= 0; i--) {
1076
- const msgs = sessionEntryToContextMessages(entries[i]);
1077
- for (let j = msgs.length - 1; j >= 0; j--) {
1078
- if (msgs[j].role === "user") {
1079
- const c = msgs[j].content;
1080
- if (typeof c === "string")
1081
- return c;
1082
- if (Array.isArray(c))
1083
- return c.map((b) => b.text).join(" ");
1084
- }
1085
- }
1086
- }
1087
- }
1088
- catch {
1089
- /* best-effort */
1090
- }
1091
- return "";
34
+ const runtime = new MegaRuntime(config);
35
+ registerEventHandlers(pi, runtime, config);
36
+ registerCommands(pi, runtime, config);
37
+ registerDashboardCommands(pi, runtime);
1092
38
  }