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