pi-mega-compact 0.7.8 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +11 -12
  2. package/dist/extensions/dashboard-server/html.js +1023 -0
  3. package/dist/extensions/dashboard-server/html.test.js +41 -0
  4. package/dist/extensions/dashboard-server/index-reader.js +133 -0
  5. package/dist/extensions/dashboard-server/server.js +530 -0
  6. package/dist/extensions/dashboard-server/server.test.js +120 -0
  7. package/dist/extensions/dashboard-server/snapshot.js +43 -0
  8. package/dist/extensions/dashboard-server/state.js +30 -0
  9. package/dist/extensions/dashboard-server/types.js +5 -0
  10. package/dist/extensions/dashboard-server-s32.test.js +181 -0
  11. package/dist/extensions/dashboard-server.js +7 -1315
  12. package/dist/extensions/mega-commands.js +162 -134
  13. package/dist/extensions/mega-compact.js +3 -0
  14. package/dist/extensions/mega-compact.test.js +90 -21
  15. package/dist/extensions/mega-conflict-cmds.js +5 -1
  16. package/dist/extensions/mega-dashboard-cmds.js +29 -22
  17. package/dist/extensions/mega-db-cmds.js +11 -2
  18. package/dist/extensions/mega-events/agent-handlers.js +222 -0
  19. package/dist/extensions/mega-events/compact-handlers.js +162 -0
  20. package/dist/extensions/mega-events/context-handler.js +249 -0
  21. package/dist/extensions/mega-events/register.js +21 -0
  22. package/dist/extensions/mega-events/session-handlers.js +142 -0
  23. package/dist/extensions/mega-events.js +15 -699
  24. package/dist/extensions/mega-game-cmds.js +106 -0
  25. package/dist/extensions/mega-game-cmds.test.js +113 -0
  26. package/dist/extensions/mega-pipeline/compact.js +324 -0
  27. package/dist/extensions/mega-pipeline/memory-review.js +38 -0
  28. package/dist/extensions/mega-pipeline/recall.js +147 -0
  29. package/dist/extensions/mega-pipeline.js +9 -480
  30. package/dist/extensions/mega-runtime/helpers.js +40 -0
  31. package/dist/extensions/mega-runtime/query.js +29 -0
  32. package/dist/extensions/mega-runtime/state.js +877 -0
  33. package/dist/extensions/mega-runtime/state.test.js +171 -0
  34. package/dist/extensions/mega-runtime/widget.js +270 -0
  35. package/dist/extensions/mega-runtime/widget.test.js +160 -0
  36. package/dist/extensions/mega-runtime.js +15 -947
  37. package/dist/src/config/themes.js +84 -0
  38. package/dist/src/config/themes.test.js +94 -0
  39. package/dist/src/game/scoring.js +105 -0
  40. package/dist/src/game/scoring.test.js +98 -0
  41. package/dist/src/store/sqlite/checkpoints.js +145 -0
  42. package/dist/src/store/sqlite/dedup-mirror.js +64 -0
  43. package/dist/src/store/sqlite/foundation.js +38 -0
  44. package/dist/src/store/sqlite/game-achievements.js +111 -0
  45. package/dist/src/store/sqlite/game-achievements.test.js +67 -0
  46. package/dist/src/store/sqlite/game-scores.js +105 -0
  47. package/dist/src/store/sqlite/game-scores.test.js +106 -0
  48. package/dist/src/store/sqlite/game-state.js +54 -0
  49. package/dist/src/store/sqlite/game-state.test.js +76 -0
  50. package/dist/src/store/sqlite/global-index.js +224 -0
  51. package/dist/src/store/sqlite/maintenance.js +235 -0
  52. package/dist/src/store/sqlite/memories.js +164 -0
  53. package/dist/src/store/sqlite/meta.js +82 -0
  54. package/dist/src/store/sqlite/model-snapshots.js +47 -0
  55. package/dist/src/store/sqlite/raptor.js +57 -0
  56. package/dist/src/store/sqlite/raw-transcript.js +134 -0
  57. package/dist/src/store/sqlite/schema.js +294 -0
  58. package/dist/src/store/sqlite/session-state.js +28 -0
  59. package/dist/src/store/sqlite/stats.js +66 -0
  60. package/dist/src/store/sqlite/utils.js +120 -0
  61. package/dist/src/store/sqlite.js +23 -1607
  62. package/extensions/dashboard-server/html.test.ts +50 -0
  63. package/extensions/dashboard-server/html.ts +1026 -0
  64. package/extensions/dashboard-server/index-reader.ts +130 -0
  65. package/extensions/dashboard-server/server.test.ts +131 -0
  66. package/extensions/dashboard-server/server.ts +505 -0
  67. package/extensions/dashboard-server/snapshot.ts +44 -0
  68. package/extensions/dashboard-server/state.ts +33 -0
  69. package/extensions/dashboard-server/types.ts +134 -0
  70. package/extensions/dashboard-server-s32.test.ts +195 -0
  71. package/extensions/dashboard-server.ts +7 -1431
  72. package/extensions/mega-commands.ts +33 -10
  73. package/extensions/mega-compact.test.ts +198 -43
  74. package/extensions/mega-compact.ts +3 -0
  75. package/extensions/mega-conflict-cmds.ts +6 -2
  76. package/extensions/mega-dashboard-cmds.ts +30 -23
  77. package/extensions/mega-db-cmds.ts +11 -3
  78. package/extensions/mega-events/agent-handlers.ts +262 -0
  79. package/extensions/mega-events/compact-handlers.ts +192 -0
  80. package/extensions/mega-events/context-handler.ts +290 -0
  81. package/extensions/mega-events/register.ts +37 -0
  82. package/extensions/mega-events/session-handlers.ts +165 -0
  83. package/extensions/mega-events.ts +15 -780
  84. package/extensions/mega-game-cmds.test.ts +137 -0
  85. package/extensions/mega-game-cmds.ts +122 -0
  86. package/extensions/mega-pipeline/compact.ts +366 -0
  87. package/extensions/mega-pipeline/memory-review.ts +46 -0
  88. package/extensions/mega-pipeline/recall.ts +165 -0
  89. package/extensions/mega-pipeline.ts +9 -537
  90. package/extensions/mega-runtime/helpers.ts +68 -0
  91. package/extensions/mega-runtime/query.ts +29 -0
  92. package/extensions/mega-runtime/state.test.ts +171 -0
  93. package/extensions/mega-runtime/state.ts +967 -0
  94. package/extensions/mega-runtime/widget.test.ts +185 -0
  95. package/extensions/mega-runtime/widget.ts +359 -0
  96. package/extensions/mega-runtime.ts +15 -1093
  97. package/package.json +4 -3
  98. package/src/config/themes.test.ts +116 -0
  99. package/src/config/themes.ts +124 -0
  100. package/src/game/scoring.test.ts +103 -0
  101. package/src/game/scoring.ts +158 -0
  102. package/src/store/sqlite/checkpoints.ts +204 -0
  103. package/src/store/sqlite/dedup-mirror.ts +114 -0
  104. package/src/store/sqlite/foundation.ts +63 -0
  105. package/src/store/sqlite/game-achievements.test.ts +80 -0
  106. package/src/store/sqlite/game-achievements.ts +147 -0
  107. package/src/store/sqlite/game-scores.test.ts +132 -0
  108. package/src/store/sqlite/game-scores.ts +168 -0
  109. package/src/store/sqlite/game-state.test.ts +89 -0
  110. package/src/store/sqlite/game-state.ts +87 -0
  111. package/src/store/sqlite/global-index.ts +305 -0
  112. package/src/store/sqlite/maintenance.ts +294 -0
  113. package/src/store/sqlite/memories.ts +217 -0
  114. package/src/store/sqlite/meta.ts +108 -0
  115. package/src/store/sqlite/model-snapshots.ts +83 -0
  116. package/src/store/sqlite/raptor.ts +107 -0
  117. package/src/store/sqlite/raw-transcript.ts +221 -0
  118. package/src/store/sqlite/schema.ts +305 -0
  119. package/src/store/sqlite/session-state.ts +38 -0
  120. package/src/store/sqlite/stats.ts +127 -0
  121. package/src/store/sqlite/utils.ts +125 -0
  122. package/src/store/sqlite.ts +23 -2204
@@ -1,1097 +1,19 @@
1
1
  /**
2
- * mega-runtime.ts — the shared live state of the mega-compact extension.
2
+ * mega-runtime.ts — barrel file re-exporting the split submodules under
3
+ * extensions/mega-runtime/.
3
4
  *
4
- * The original mega-compact.ts was a single large closure over ~20 mutable
5
- * variables. This module lifts that state into a `MegaRuntime` class so the
6
- * event/command/pipeline modules can share it without re-declaring it. All
7
- * behavior (store/dashboard rebinding, dashboard snapshot shape, the
8
- * above-editor widget math, model capture) is preserved byte-for-byte from the
9
- * original closure.
5
+ * Originally a single ~1097-line monolith; split into focused submodules:
6
+ * - mega-runtime/widget.ts — above-editor widget helpers + WidgetData
7
+ * - mega-runtime/helpers.ts shared constants, SessionRuntime, ownVersion
8
+ * - mega-runtime/state.ts — the MegaRuntime class
9
+ * - mega-runtime/query.ts — recentUserQuery free function
10
+ *
11
+ * Every symbol previously exported from this file is re-exported here so
12
+ * consumers (mega-compact.ts, mega-commands.ts, mega-events.ts,
13
+ * mega-pipeline.ts, etc.) keep working with no import-path changes.
10
14
  */
11
15
 
12
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
13
- import { sessionEntryToContextMessages } from "@earendil-works/pi-coding-agent";
14
- import type { AgentMessage } from "@earendil-works/pi-agent-core";
15
- import { join, dirname } from "node:path";
16
- import { fileURLToPath } from "node:url";
17
- import { readFileSync, appendFileSync, mkdirSync } from "node:fs";
18
- import { VectorStore } from "../src/vectorStore.js";
19
- import { toEngineMessages } from "../src/adapt.js";
20
- import { normalizeSessionId } from "../src/store.js";
21
- import { Logger } from "../src/log.js";
22
- import {
23
- recordModelSnapshot,
24
- latestModelSnapshot,
25
- upsertRepoRegistry,
26
- recordRepoModel,
27
- getDedupStats,
28
- getCompactCount,
29
- getRecallInjected,
30
- getCacheHitTokensSaved,
31
- type ModelSnapshot,
32
- } from "../src/store/sqlite.js";
33
- import { detectCrossRepoDrift } from "../src/driftDetection.js";
34
- import {
35
- repoStateDir,
36
- resolveRepoRoot,
37
- pressureRatio,
38
- pressureFromPct,
39
- pressureBand,
40
- effectiveThresholdTokens,
41
- type MegaConfig,
42
- type PressureBand,
43
- } from "./mega-config.js";
44
- import { Dashboard, type DashboardSnapshot } from "./mega-dashboard.js";
45
-
46
- export const STATUS_KEY = "mega-compact";
47
- export const WIDGET_KEY = "mega-compact-stats";
48
- export const MARKER_TYPE = "mega-compact-marker";
49
-
50
- /** Cached npm version, read once from this extension's own package.json. */
51
- let CACHED_VERSION: string | null = null;
52
- function ownVersion(): string {
53
- if (CACHED_VERSION !== null) return CACHED_VERSION;
54
- let v = "?";
55
- try {
56
- const here = dirname(fileURLToPath(import.meta.url)); // .../extensions
57
- const pkg = JSON.parse(
58
- readFileSync(join(here, "..", "package.json"), "utf-8"),
59
- );
60
- v = pkg.version ?? "?";
61
- } catch {
62
- v = "?";
63
- }
64
- CACHED_VERSION = v;
65
- return v;
66
- }
67
-
68
- /** Per-session runtime state kept in the closure (mirrors neuralwatt-mcr). */
69
- interface SessionRuntime {
70
- sessionId: string;
71
- persistedThisSession: boolean;
72
- lastCheckpointId: string | undefined;
73
- lastCompactedFrom: number;
74
- lastCompactedTokens: number;
75
- dedupSkips: number; // compactions skipped because regionHash already stored
76
- dedupAttempts: number; // total compaction attempts (for hit-rate denominator)
77
- tokensSaved: number; // this session-instance only: reset on session_start
78
- lastCompactAt: number | null; // wall-clock ms of the last compaction this session
79
- lastNativeCompactAt: number | null; // COMPACT-DEDUP FIX: wall-clock ms of the last NATIVE pi compaction (session_compact event) — used by the agent_end/legacy race guard to skip a redundant ctx.compact() that would throw "Already compacted".
80
- // S25: live dashboard counters (reset on session_start, mirrored to SQLite).
81
- compactCount: number; // compactions performed this session-instance
82
- recallInjections: number; // recall blocks injected this session-instance
83
- cacheHitTokens: number; // tokens saved via cache hits (dedup + recall) this session
84
- lengthStopPending: boolean; // S28: set on turn_end when stopReason==='length'
85
- }
86
-
87
- /** ANSI palette for the toolbar. The pi TUI's Text component preserves ANSI
88
- * escape codes (see wrapTextWithAnsi), so raw escapes render as colors. No
89
- * chalk dependency needed — these are just strings. */
90
- export const C = {
91
- reset: "\x1b[0m",
92
- dim: "\x1b[2m",
93
- bold: "\x1b[1m",
94
- amber: "\x1b[38;5;214m", // tier / ready
95
- green: "\x1b[38;5;120m", // saved
96
- cyan: "\x1b[38;5;51m", // used / live activity
97
- teal: "\x1b[38;5;37m", // processing (compress/dedup)
98
- magenta: "\x1b[38;5;201m", // dedup rate
99
- blue: "\x1b[38;5;75m", // repo totals
100
- gray: "\x1b[38;5;245m", // labels
101
- red: "\x1b[38;5;203m", // pressure / overflow
102
- };
103
-
104
- const PULSE = ["◐", "◓", "◑", "◒"];
105
-
106
- // Rough tokens-processed-per-second heuristic for the dashboard's "time saved"
107
- // estimate. Throughput varies by model/hardware; this is order-of-magnitude so
108
- // the dashboard can show a human-readable figure, not a precise measurement.
109
- const TOKENS_PER_SEC_ESTIMATE = 2000;
110
-
111
- // ── Full-width widget panel helpers ────────────────────────────────────────
112
- // pi's above-editor widget renderer (a Container of Text lines) does NOT pass
113
- // a terminal width to setWidget(), so lines render left-aligned by default. To
114
- // make the widget read as a full-width status panel we pad each line to the
115
- // real terminal width with a background fill. NOTE: C.reset is a FULL SGR
116
- // reset, so we re-apply the panel bg after every reset to keep the background
117
- // continuous under colored text (and under pi's own trailing reset).
118
- const PANEL_BG = "\x1b[48;5;236m"; // dark slate panel background
119
- const PANEL_RST = "\x1b[0m" + PANEL_BG; // reset fg but retain panel bg
120
-
121
- /** Visible cell width of a string, ignoring ANSI SGR/OSC escapes. */
122
- function visibleWidth(s: string): number {
123
- const stripped = s
124
- .replace(/\x1b\[[0-9;?]*[A-Za-z]/g, "")
125
- .replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, "");
126
- let w = 0;
127
- for (const ch of stripped) {
128
- const cp = ch.codePointAt(0) ?? 0;
129
- const wide =
130
- cp >= 0x1100 &&
131
- (cp <= 0x115f ||
132
- (cp >= 0x2e80 && cp <= 0x303e) ||
133
- (cp >= 0x3041 && cp <= 0x33ff) ||
134
- (cp >= 0x3400 && cp <= 0x4dbf) ||
135
- (cp >= 0x4e00 && cp <= 0x9fff) ||
136
- (cp >= 0xa000 && cp <= 0xa4cf) ||
137
- (cp >= 0xac00 && cp <= 0xd7a3) ||
138
- (cp >= 0xf900 && cp <= 0xfaff) ||
139
- (cp >= 0xfe30 && cp <= 0xfe4f) ||
140
- (cp >= 0xff00 && cp <= 0xff60) ||
141
- (cp >= 0xffe0 && cp <= 0xffe6) ||
142
- (cp >= 0x1f300 && cp <= 0x1faff) ||
143
- (cp >= 0x20000 && cp <= 0x3fffd));
144
- w += wide ? 2 : 1;
145
- }
146
- return w;
147
- }
148
-
149
- /** Pad a content string (with ANSI colors) to `width` cells using panel bg. */
150
- /** Wrap a string (with ANSI codes) to fit within `maxWidth` visible chars.
151
- * Splits at │ separators or whitespace when possible. */
152
- function wrapLine(text: string, maxWidth: number): string[] {
153
- if (maxWidth <= 0) return [text];
154
- const result: string[] = [];
155
- let current = "";
156
- let currentW = 0;
157
- // Split at │ boundaries first
158
- const segments = text.split("│");
159
- for (let i = 0; i < segments.length; i++) {
160
- const seg = (i > 0 ? "│" : "") + segments[i];
161
- const segW = visibleWidth(PANEL_BG + seg.replace(/\x1b\[0m/g, PANEL_RST));
162
- if (currentW + segW <= maxWidth || currentW === 0) {
163
- current += seg;
164
- currentW += segW;
165
- } else {
166
- result.push(current);
167
- current = seg;
168
- currentW = segW;
169
- }
170
- }
171
- if (current) result.push(current);
172
- return result;
173
- }
174
-
175
- function panelLine(content: string, width: number): string {
176
- const withBg = PANEL_BG + content.replace(/\x1b\[0m/g, PANEL_RST);
177
- const pad = Math.max(0, width - visibleWidth(withBg));
178
- return withBg + " ".repeat(pad) + "\x1b[0m";
179
- }
180
-
181
- /** A full-width hairline bar (top/bottom border of the panel). */
182
- function panelBar(width: number, ch = "─"): string {
183
- return PANEL_BG + ch.repeat(Math.max(0, width)) + "\x1b[0m";
184
- }
185
-
186
- /** Token-count formatter: M at/above 1e6, k at/above 1e3, raw below.
187
- * 5,472,700 → "5.5mil", 24,100 → "24.1k", 142 → "142". */
188
- function fmtTokens(x: number): string {
189
- return x >= 1_000_000
190
- ? `${(x / 1_000_000).toFixed(1)}mil`
191
- : x >= 1000
192
- ? `${(x / 1000).toFixed(1)}k`
193
- : `${Math.round(x)}`;
194
- }
195
-
196
- /** Retro gradient bar — `w` cells shaded by fill position (green→amber→red).
197
- * Used for CONTEXT fill where low=green (room) and high=red (near the limit). */
198
- function ramp(pct: number, w = 12): string {
199
- const cells = ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"];
200
- const scaled = Math.max(0, Math.min(w, pct * w));
201
- const full = Math.floor(scaled);
202
- const frac = scaled - full;
203
- const fracCell = frac > 0 ? cells[Math.round(frac * (cells.length - 1))] : "";
204
- let out = "";
205
- for (let i = 0; i < full; i++)
206
- out += (i / w < 0.6 ? C.green : i / w < 0.85 ? C.amber : C.red) + "█";
207
- if (fracCell)
208
- out +=
209
- (full / w < 0.6 ? C.green : full / w < 0.85 ? C.amber : C.red) + fracCell;
210
- out +=
211
- C.dim + "░".repeat(Math.max(0, w - full - (fracCell ? 1 : 0))) + C.reset;
212
- return out;
213
- }
214
-
215
- /** Human "time since" string from a millisecond delta (or null → "never"). */
216
- function sinceCompactStr(ms: number | null): string {
217
- if (ms == null) return "never";
218
- const s = Math.floor(ms / 1000);
219
- if (s < 60) return `${s}s ago`;
220
- const m = Math.floor(s / 60);
221
- if (m < 60) return `${m}m ago`;
222
- const h = Math.floor(m / 60);
223
- if (h < 24) return `${h}h ago`;
224
- return `${Math.floor(h / 24)}d ago`;
225
- }
226
-
227
- interface TickerEntry {
228
- text: string;
229
- at: number;
230
- }
231
-
232
- /** Immutable snapshot of everything the above-editor widget needs to render.
233
- * Computed once per `snapshot()` (event-driven) and read by `buildWidgetLines`
234
- * on every TUI render frame, so frame rendering stays allocation-cheap and the
235
- * panel auto-fits whatever width pi passes to the setWidget factory. */
236
- interface WidgetData {
237
- version: string;
238
- tierLabel: string;
239
- triggerLabel: string;
240
- pctStr: string;
241
- tokStr: string;
242
- maxStr: string;
243
- ctxPct: number;
244
- chk: number;
245
- agentStr: string;
246
- turnStr: string;
247
- dedupStr: string;
248
- sessIn: number;
249
- sessKept: number;
250
- sTxt: string;
251
- repoIn: number;
252
- repoKept: number;
253
- rTxt: string;
254
- repoChk: number;
255
- repoSess: number;
256
- modelStr: string;
257
- sinceCompact: number | null;
258
- embedderName: string;
259
- compStr: string;
260
- driftStatus: "ok" | "warn";
261
- agentsActive: boolean;
262
- fresh: boolean;
263
- ticker: TickerEntry[];
264
- lastWhy: string | undefined;
265
- tierTrace: string | undefined;
266
- pulsing: boolean;
267
- }
268
-
269
- export class MegaRuntime {
270
- config: MegaConfig;
271
- // Store/dashboard/logger are rebound per-repo by bindRepo() so each git repo
272
- // gets its own isolated state dir. They start bound to the global default.
273
- store: VectorStore;
274
- logger: Logger;
275
- dashboard: Dashboard;
276
- activeRepoRoot: string | null = null;
277
- currentStateDir: string;
278
-
279
- // The only mutable per-session state. Reset on session_start / session_tree.
280
- rt: SessionRuntime = {
281
- sessionId: normalizeSessionId(undefined),
282
- persistedThisSession: false,
283
- lastCheckpointId: undefined,
284
- lastCompactedFrom: 0,
285
- lastCompactedTokens: 0,
286
- dedupSkips: 0,
287
- dedupAttempts: 0,
288
- tokensSaved: 0,
289
- lastCompactAt: null,
290
- lastNativeCompactAt: null,
291
- compactCount: 0,
292
- recallInjections: 0,
293
- cacheHitTokens: 0,
294
- lengthStopPending: false,
295
- };
296
- debounceUntil = 0;
297
- // S16: debounce for the agent_end resume nudge (avoid busy-loops).
298
- resumeNudgeUntil = 0;
299
- // Agent tracking for real-time widget updates
300
- activeAgents = 0;
301
- currentTurn = 0;
302
- // Recall block produced by auto-inline (resume/branch) that the next
303
- // before_agent_start should prepend to the system prompt. Unset after use.
304
- pendingRecallBlock: string | undefined;
305
- // S21: memory recall block, parallel to pendingRecallBlock. Same one-shot
306
- // semantics; composed with the checkpoint block in before_agent_start.
307
- pendingMemoryRecallBlock: string | undefined;
308
- statusKey: string | undefined; // current status text for dashboard
309
- // Active model/provider (for real cost estimation). Captured from ctx.model
310
- // on model_select + session_start; persisted to SQL so cost + the dashboard
311
- // can read it without a live ctx.
312
- currentModel: ModelSnapshot | undefined;
313
- // Live "what it's doing right now" timestamp, used for the fresh-window.
314
- lastActivityAt = 0;
315
- // Live per-tier dedup trace (Phase 1): e.g. "L0 ✓ → L1 ✓ → L2 0.91 → stored".
316
- // Built from the store's sync onTier callback during a compaction so the user
317
- // watches each tier evaluate in real time. Cleared once the outcome settles.
318
- tierTrace: string | undefined;
319
- // Phase 3 — standout toolbar state.
320
- // Recall/activity ticker: a small ring buffer (≤5) of recent compact/recall
321
- // events so the widget shows a live history instead of a single last action.
322
- ticker: TickerEntry[] = [];
323
- readonly TICKER_MAX = 5;
324
- // Pulsing status: set true while a compaction is in flight, cleared on result.
325
- pulsing = false;
326
- // S21.2: set by `applyMemoryOps` when a memory add/replace/remove lands in
327
- // the current compaction. The pipeline reads this after a successful compact
328
- // to decide whether to fire `consolidateMemories` (skip the work entirely
329
- // when no memory rows changed).
330
- memoriesTouchedThisCompaction = 0;
331
- // Rolling "saved" goal for the progress bar — grows as we save more, so the
332
- // bar always has a meaningful denominator (never sits at 100% forever).
333
- savedGoal = 50_000;
334
- // Last explain-why line (dedup reason / anchor-kept / superseded), surfaced
335
- // while fresh.
336
- lastWhy: string | undefined = undefined;
337
-
338
- // Context tracking for the dashboard (updated in the context handler).
339
- lastCtxTokens: number | null = null;
340
- lastCtxPercent: number | null = null;
341
- lastCtxWindow = 0;
342
-
343
- // Latest computed widget payload (recomputed per snapshot, rendered per frame).
344
- widgetData: WidgetData | null = null;
345
- // Cached cross-repo drift status (recomputed at most every 30s — it opens the
346
- // machine-wide registry DB, so we don't want to do it on every render frame).
347
- private driftCache: { at: number; status: "ok" | "warn" } | null = null;
348
-
349
- /**
350
- * DIAG counters for the "team run doesn't relieve context" investigation.
351
- * Plain integers, incremented at the three compaction decision points. They
352
- * let a headless test drive the real event handlers and assert the firing
353
- * cadence without scraping log files. Inert in production (the live-trim and
354
- * before-compact probes also emit logger.info, but these counters are always
355
- * updated and cost nothing).
356
- */
357
- diagLiveTrimFires = 0; // context handler returned a trimmed view
358
- diagBeforeCompactFires = 0; // session_before_compact handler entered
359
- diagBeforeCompactSupplied = 0; // session_before_compact supplied our trim
360
- diagAgentEndIdle = 0; // agent_end with activeAgents===0
361
- diagAgentEndDurable = 0; // agent_end fired ctx.compact() (mid-run durable trim)
362
- diagAgentEndDurableSkipRecent = 0; // agent_end skipped ctx.compact() — compaction in last 10s (race guard)
363
- // Per-skip-path counters for the team-run diagnosis.
364
- diagCtxFastGate = 0; // returned at token fast-gate (below threshold)
365
- diagCtxNoCompact = 0; // autoCompactCheck().shouldCompact === false
366
- diagCtxDebounce = 0; // debounceUntil not yet elapsed
367
- diagCtxRunSkipped = 0; // runCompact() returned skipped
368
- diagCtxCutNull = 0; // computeLiveTrimCut returned null (anchor/boundary)
369
- diagCtxThrown = 0; // live-trim try threw (caught)
370
-
371
- /**
372
- * S26 capture instrumentation: the "model_snapshots empty → $0.00 cost card"
373
- * bug was invisible because captureModel swallowed the DB write in a silent
374
- * `catch {}`. These always-updated counters (zero cost) let a headless test or
375
- * a live capture tell whether captureModel ran and whether the snapshot landed.
376
- */
377
- diagCaptureModelCalls = 0; // captureModel entered with a populated ctx.model
378
- diagCaptureModelFails = 0; // recordModelSnapshot threw → model_snapshots stays empty
379
-
380
- /**
381
- * Live 0–1 pressure — how full the context window is relative to the
382
- * compaction threshold.
383
- *
384
- * RECONCILE (BACKLOG dual-basis flicker): when the model context window is
385
- * known we base pressure consistently on the *percentage* basis
386
- * (`lastCtxPercent / (tierPct*100)`). This keeps the band stable whether the
387
- * latest context event carried a token count or only a percentage, so the
388
- * threshold comparison doesn't jump when a token-count event arrives vs a
389
- * percent-only event. We only fall back to the token-count basis
390
- * (`config.thresholdTokens`) when the window is unknown (e.g. before the first
391
- * context event, or a `custom` tier with no tierPct). Always finite + in [0,1].
392
- */
393
- get pressure(): number {
394
- if (
395
- this.lastCtxWindow > 0 &&
396
- this.config.tierPct != null &&
397
- this.lastCtxPercent != null
398
- ) {
399
- // pressureFromPct(x) = x/100, and x = lastCtxPercent/tierPct, so this is
400
- // exactly the intended lastCtxPercent/(tierPct*100) 0–1 ratio: at the
401
- // fire point (lastCtxPercent == tierPct*100) pressure == 1.0, matching the
402
- // token-based pressureRatio(currentTokens, effectiveThreshold) reading so
403
- // the band doesn't jump when a token-count vs percent-only event arrives.
404
- return pressureFromPct(this.lastCtxPercent / this.config.tierPct);
405
- }
406
- if (
407
- this.lastCtxTokens != null &&
408
- this.lastCtxTokens > 0 &&
409
- this.config.thresholdTokens > 0
410
- ) {
411
- return pressureRatio(this.lastCtxTokens, this.config.thresholdTokens);
412
- }
413
- return pressureFromPct(this.lastCtxPercent);
414
- }
415
-
416
- /**
417
- * The live compaction FIRE POINT in tokens: the effective threshold scaled by
418
- * the current model context window (`tierPct * window`) when known, else the
419
- * boot fallback `config.thresholdTokens`. This is what the FAST GATE /
420
- * `autoCompactCheck` / agent_end durable-trigger compare against, so
421
- * compaction fires at tier% of the window for ANY model size (200k or 1M),
422
- * always below pi's native auto-compaction (~80% of window).
423
- */
424
- get effectiveThreshold(): number {
425
- return effectiveThresholdTokens({
426
- tierPct: this.config.tierPct,
427
- fallbackThreshold: this.config.thresholdTokens,
428
- window: this.lastCtxWindow,
429
- });
430
- }
431
-
432
- /** Live discrete pressure band (low/medium/high/ultra/mega) over `pressure`. */
433
- get pressureBand(): PressureBand {
434
- return pressureBand(this.pressure);
435
- }
436
-
437
- constructor(config: MegaConfig) {
438
- this.config = config;
439
- this.store = new VectorStore({
440
- dedupSim: config.dedupSim,
441
- stateDir: config.stateDir,
442
- });
443
- this.logger = new Logger({
444
- enabled: config.debug,
445
- path: join(config.stateDir, "mega-compact.log"),
446
- });
447
- this.dashboard = new Dashboard(config.stateDir);
448
- this.currentStateDir = config.stateDir;
449
- }
450
-
451
- // ---- per-repo binding -----------------------------------------------------
452
-
453
- /**
454
- * Point store/dashboard/logger at the current repo's state dir. Rebuilds the
455
- * instances only when the repo root changes, so cross-repo dedup stats, db,
456
- * and events are fully isolated. Falls back to the global default outside git.
457
- */
458
- bindRepo(cwd: string | undefined): string {
459
- const dir = cwd
460
- ? repoStateDir(cwd, this.config.stateDir)
461
- : this.config.stateDir;
462
- const key = cwd ? (resolveRepoRoot(cwd) ?? dir) : dir;
463
- if (key === this.activeRepoRoot) return dir;
464
- this.activeRepoRoot = key;
465
- this.currentStateDir = dir;
466
- this.store = new VectorStore({
467
- dedupSim: this.config.dedupSim,
468
- stateDir: dir,
469
- });
470
- this.logger = new Logger({
471
- enabled: this.config.debug,
472
- path: join(dir, "mega-compact.log"),
473
- });
474
- this.dashboard = new Dashboard(dir);
475
- // Aggregate this repo into the machine-wide index so the multi-repo
476
- // dashboard (Summary / All-repos tabs) can show it alongside every other
477
- // repo. Best-effort + non-fatal: a read-only index dir or contention must
478
- // never break the per-repo compaction path. Runs only on repo-switch
479
- // (this branch), so it's infrequent — not per-context-event.
480
- try {
481
- const repo = this.store.repoStats();
482
- const di = this.store.dataInvariant();
483
- const root = key !== dir ? key : (resolveRepoRoot(cwd ?? dir) ?? dir);
484
- upsertRepoRegistry({
485
- repoRoot: root,
486
- displayName: root.split(/[\\/]/).filter(Boolean).pop() ?? root,
487
- stateDir: dir,
488
- checkpointCount: repo.checkpointCount,
489
- tokensSaved: repo.tokensSaved,
490
- compressedOriginalBytes: di.compressedOriginalBytes,
491
- });
492
- } catch {
493
- /* non-fatal: index aggregation must not block compaction */
494
- }
495
- return dir;
496
- }
497
-
498
- // ---- dashboard snapshot + widget ------------------------------------------
499
-
500
- /** Collect live state and write it to disk (+ paint the above-editor widget). */
501
- snapshot(ctx?: ExtensionContext): void {
502
- if (ctx) this.bindRepo(ctx.cwd);
503
- const st = this.store.stats(this.rt.sessionId);
504
- const repo = this.store.repoStats();
505
- const di = this.store.dataInvariant();
506
- // Live + store-wide cache-hit / compaction counters for the dashboard.
507
- const ds = getDedupStats(this.currentStateDir);
508
- const cacheHitsTotal = ds.deduped + getRecallInjected(this.currentStateDir);
509
- const cacheHitsTotalTokens = getCacheHitTokensSaved(this.currentStateDir);
510
- const cacheHitsSession = this.rt.dedupSkips + this.rt.recallInjections;
511
- const sec = (tok: number) => (tok || 0) / TOKENS_PER_SEC_ESTIMATE;
512
- // Active model/provider for the current-repo card + the multi-repo table.
513
- const modelSnap = latestModelSnapshot(this.currentStateDir);
514
- const model = modelSnap
515
- ? {
516
- name: modelSnap.modelName ?? modelSnap.modelId,
517
- provider: modelSnap.provider,
518
- providerName: modelSnap.providerName ?? "",
519
- inputRate: modelSnap.inputRate,
520
- outputRate: modelSnap.outputRate,
521
- }
522
- : undefined;
523
- // effectiveThresholdPct: the live fire point as a % of the window (null for
524
- // `custom`, which has no tierPct). S29: honors MEGACOMPACT_AUTO_PCT_TRIGGER
525
- // override so the dashboard's armed/ready match the context-handler gate
526
- // (which fires on this same %). Used by armed/ready + the dashboard.
527
- const effectiveThresholdPct =
528
- this.config.tierPct != null
529
- ? (this.config.autoPctTrigger ?? this.config.tierPct) * 100
530
- : null;
531
- // armed lights at/above the REAL fire point: max(effectiveThresholdPct,
532
- // fastGatePct). fastGatePct already equals tierPct*100 by default, but a
533
- // MEGACOMPACT_FAST_GATE_PCT override can raise it, so we take the max.
534
- const armed =
535
- this.lastCtxPercent != null &&
536
- this.lastCtxPercent >=
537
- Math.max(effectiveThresholdPct ?? 0, this.config.fastGatePct);
538
- // S29: ready mirrors the context-handler gate's basis — percent for tiered
539
- // (the gate fires on pct), tokens for custom (the gate fires on tokens).
540
- // Previously this always required tokens, so the dashboard could show
541
- // "armed" (percent high) but never "ready" when tokens were under-reported
542
- // — the same inconsistency the S29 gate fix removes.
543
- const ready =
544
- this.config.tierPct != null
545
- ? armed && (this.lastCtxPercent ?? 0) >= (effectiveThresholdPct ?? 0)
546
- : armed && (this.lastCtxTokens ?? 0) >= this.effectiveThreshold;
547
- this.dashboard.snapshot({
548
- version: 1,
549
- updatedAt: new Date().toISOString(),
550
- // S24: the headline tier is the LIVE pressure band; the env preset is kept
551
- // alongside as presetTier so the dashboard can show both.
552
- tier: this.pressureBand,
553
- presetTier: this.config.tier,
554
- pressure: this.pressure,
555
- config: {
556
- fastGatePct: this.config.fastGatePct,
557
- thresholdTokens: this.effectiveThreshold,
558
- tierPct: this.config.tierPct,
559
- effectiveThresholdPct,
560
- anchorUserMessages: this.config.anchorUserMessages,
561
- preserveRecent: this.config.preserveRecent,
562
- auto: this.config.auto,
563
- autoInline: this.config.autoInline,
564
- },
565
- session: {
566
- id: this.rt.sessionId,
567
- state: this.statusKey ?? "idle",
568
- persistedThisSession: this.rt.persistedThisSession,
569
- lastCheckpointId: this.rt.lastCheckpointId ?? null,
570
- lastCompactedFrom: this.rt.lastCompactedFrom,
571
- lastCompactedTokens: this.rt.lastCompactedTokens,
572
- dedupSkips: this.rt.dedupSkips,
573
- dedupAttempts: this.rt.dedupAttempts,
574
- },
575
- context: {
576
- tokens: this.lastCtxTokens,
577
- percent: this.lastCtxPercent,
578
- contextWindow: this.lastCtxWindow,
579
- },
580
- trigger: {
581
- armed,
582
- ready,
583
- currentTokens: this.lastCtxTokens,
584
- thresholdTokens: this.effectiveThreshold,
585
- fastGatePct: this.config.fastGatePct,
586
- tierPct: this.config.tierPct,
587
- effectiveThresholdPct,
588
- },
589
- crew: { activeAgents: this.activeAgents, currentTurn: this.currentTurn },
590
- store: {
591
- checkpointCount: st.checkpointCount,
592
- totalTokenEstimate: st.totalTokenEstimate,
593
- originalTokens: st.originalTokens,
594
- tokensSaved: this.rt.tokensSaved,
595
- injectedCount: st.injectedCount,
596
- dedupHitRate: st.dedupHitRate,
597
- storageDedupRate: st.storageDedupRate,
598
- dedupAttempts: st.dedupAttempts,
599
- dedupCollapsed: st.dedupCollapsed,
600
- },
601
- // Reconciled token accounting (single canonical formula, session + repo).
602
- // Freed = In − Out; In = Freed + Out. session.Freed = rt.tokensSaved (incl.
603
- // deduped-away originals); repo.Freed = repo.tokensSaved meta counter.
604
- compression: {
605
- session: {
606
- tokensIn: this.rt.tokensSaved + st.totalTokenEstimate,
607
- tokensOut: st.totalTokenEstimate,
608
- tokensFreed: this.rt.tokensSaved,
609
- compressionPct:
610
- this.rt.tokensSaved + st.totalTokenEstimate > 0
611
- ? this.rt.tokensSaved /
612
- (this.rt.tokensSaved + st.totalTokenEstimate)
613
- : 0,
614
- dedupPct: st.storageDedupRate,
615
- },
616
- repo: {
617
- tokensIn: repo.tokensSaved + repo.totalTokenEstimate,
618
- tokensOut: repo.totalTokenEstimate,
619
- tokensFreed: repo.tokensSaved,
620
- compressionPct:
621
- repo.tokensSaved + repo.totalTokenEstimate > 0
622
- ? repo.tokensSaved / (repo.tokensSaved + repo.totalTokenEstimate)
623
- : 0,
624
- dedupPct: repo.storageDedupRate,
625
- },
626
- },
627
- repo: {
628
- checkpointCount: repo.checkpointCount,
629
- totalTokenEstimate: repo.totalTokenEstimate,
630
- originalTokens: repo.originalTokens,
631
- tokensSaved: repo.tokensSaved,
632
- sessionCount: repo.sessionCount,
633
- dedupAttempts: repo.dedupAttempts,
634
- dedupCollapsed: repo.dedupCollapsed,
635
- storageDedupRate: repo.storageDedupRate,
636
- },
637
- integrity: {
638
- regionsRetained: di.regionsRetained,
639
- compressedOriginalBytes: di.compressedOriginalBytes,
640
- duplicatesCollapsed: di.duplicatesCollapsed,
641
- bytesPermanentlyDeleted: di.bytesPermanentlyDeleted,
642
- },
643
- cacheHits: {
644
- session: cacheHitsSession,
645
- total: cacheHitsTotal,
646
- sessionTokensSaved: this.rt.cacheHitTokens,
647
- totalTokensSaved: cacheHitsTotalTokens,
648
- },
649
- compacts: {
650
- session: this.rt.compactCount,
651
- total: getCompactCount(this.currentStateDir),
652
- },
653
- timeSaved: {
654
- compact: { sessionSec: sec(this.rt.tokensSaved), totalSec: sec(this.store.repoStats().tokensSaved) },
655
- cacheHit: { sessionSec: sec(this.rt.cacheHitTokens), totalSec: sec(cacheHitsTotalTokens) },
656
- },
657
- model,
658
- } as DashboardSnapshot);
659
-
660
- // Live stats widget above the editor
661
- if (ctx) {
662
- // ── gather widget data (computed per snapshot, rendered per frame) ────
663
- const tokStr =
664
- this.lastCtxTokens != null
665
- ? `${Math.round(this.lastCtxTokens / 1000)}k`
666
- : "?";
667
- const maxStr =
668
- this.lastCtxWindow > 0
669
- ? `${Math.round(this.lastCtxWindow / 1000)}k`
670
- : "?";
671
- const pctStr =
672
- this.lastCtxPercent != null
673
- ? this.lastCtxPercent > 100
674
- ? `>100%` // S29: overshoot warning, not a raw "250%" — the percent trigger now compacts before 100%, so this is the residual case where it can't keep up.
675
- : `${Math.round(this.lastCtxPercent * 10) / 10}%`
676
- : "?%";
677
- // S24: the tier label is the LIVE pressure band (low/medium/high/ultra/
678
- // mega), not the static env preset. It climbs as context fills.
679
- const liveBand = this.pressureBand;
680
- const tierLabel = `${C.bold}${liveBand}${C.reset}${C.gray}·${this.config.tier}${C.reset}`;
681
- const triggerLabel = ready
682
- ? `${C.green}● ready${C.reset}`
683
- : armed
684
- ? `${C.amber}◐ armed${C.reset}`
685
- : `${C.gray}○ idle${C.reset}`;
686
- // Storage dedup rate is cumulative (store-wide, per-repo) and survives
687
- // session resets. Always show a number (decimal for sub-10%).
688
- const storageRate = st.storageDedupRate; // 0..1
689
- const dedupStr =
690
- storageRate * 100 >= 10
691
- ? `${Math.round(storageRate * 100)}%`
692
- : `${(storageRate * 100).toFixed(1)}%`;
693
- // Agents view: count + status (S27 per-agent tokens are gated on P0).
694
- const agentLabel =
695
- this.activeAgents > 0
696
- ? `🤖 ${this.activeAgents} agent${this.activeAgents === 1 ? "" : "s"}`
697
- : `${C.dim}🤖 idle${C.reset}`;
698
- const agentStr = ` │ ${agentLabel}`;
699
- const turnStr = this.currentTurn > 0 ? ` │ turn ${this.currentTurn}` : "";
700
- // Reconciled in/out view (session + repo) — ONE canonical formula.
701
- const sessIn = this.rt.tokensSaved + st.totalTokenEstimate;
702
- const sessKept = st.totalTokenEstimate;
703
- const sessPct = sessIn > 0 ? this.rt.tokensSaved / sessIn : 0;
704
- const repoIn = repo.tokensSaved + repo.totalTokenEstimate;
705
- const repoKept = repo.totalTokenEstimate;
706
- const repoPct = repoIn > 0 ? repo.tokensSaved / repoIn : 0;
707
- const sTxt = (sessPct * 100).toFixed(sessPct * 100 >= 10 ? 0 : 1);
708
- const rTxt = (repoPct * 100).toFixed(repoPct * 100 >= 10 ? 0 : 1);
709
- const ctxPct =
710
- this.lastCtxPercent != null ? this.lastCtxPercent / 100 : 0;
711
- // Model + provider (S26 capture) for the header.
712
- const modelName = modelSnap?.modelName ?? modelSnap?.modelId ?? "?";
713
- const modelStr = modelSnap?.provider
714
- ? `${modelName}·${modelSnap.provider}`
715
- : modelName;
716
- // Since-last-compact (ms; null until first compaction this session).
717
- const sinceCompact =
718
- this.rt.lastCompactAt != null
719
- ? Date.now() - this.rt.lastCompactAt
720
- : null;
721
- // Memory store: embedder + compression ratio (original / stored).
722
- const embedderName = this.embedderName();
723
- const compRatio =
724
- st.originalTokens > 0 && st.totalTokenEstimate > 0
725
- ? st.originalTokens / st.totalTokenEstimate
726
- : st.originalTokens > 0
727
- ? 1
728
- : 0;
729
- const compStr = compRatio >= 1 ? `${compRatio.toFixed(1)}x` : "—";
730
- // Cross-repo drift status (cached, read-only).
731
- const driftStatus = this.driftStatus();
732
- const agentsActive = this.activeAgents > 0;
733
-
734
- this.widgetData = {
735
- version: ownVersion(),
736
- tierLabel,
737
- triggerLabel,
738
- pctStr,
739
- tokStr,
740
- maxStr,
741
- ctxPct,
742
- chk: st.checkpointCount,
743
- agentStr,
744
- turnStr,
745
- dedupStr,
746
- sessIn,
747
- sessKept,
748
- sTxt,
749
- repoIn,
750
- repoKept,
751
- rTxt,
752
- repoChk: repo.checkpointCount,
753
- repoSess: repo.sessionCount,
754
- modelStr,
755
- sinceCompact,
756
- embedderName,
757
- compStr,
758
- driftStatus,
759
- agentsActive,
760
- fresh: Date.now() - this.lastActivityAt < 4000,
761
- ticker: this.ticker,
762
- lastWhy: this.lastWhy,
763
- tierTrace: this.tierTrace,
764
- pulsing: this.pulsing,
765
- };
766
- // Auto-fit: register a factory so pi re-renders the panel at the REAL
767
- // terminal width every frame (tui.columns), instead of guessing with
768
- // process.stdout.columns. buildWidgetLines reads this.widgetData live.
769
- this.renderWidget(ctx);
770
- }
771
- }
772
-
773
- /** Register the above-editor widget as a width-aware factory so pi re-renders
774
- * it at the REAL terminal width every frame (auto-fit wide/narrow). The
775
- * factory returns a minimal Component whose render() reads this.widgetData.
776
- */
777
- private renderWidget(ctx: ExtensionContext): void {
778
- ctx.ui.setWidget(
779
- WIDGET_KEY,
780
- (_tui, _theme) => ({
781
- render: (width: number) =>
782
- this.buildWidgetLines(width > 0 ? width : 200),
783
- invalidate: () => {},
784
- }),
785
- { placement: "aboveEditor" },
786
- );
787
- }
788
-
789
- /** Build the full-width panel lines from the latest snapshot. Cheap: reads
790
- * only this.widgetData + a couple of live counters; no DB/IO. */
791
- private buildWidgetLines(width: number): string[] {
792
- const wd = this.widgetData;
793
- if (!wd) {
794
- return [
795
- panelBar(width, "─"),
796
- panelLine(" mega-compact: warming up…", width),
797
- panelBar(width, "─"),
798
- ];
799
- }
800
- const pulse = wd.pulsing
801
- ? `${C.cyan}${PULSE[Math.floor(Date.now() / 250) % PULSE.length]}${C.reset} `
802
- : "";
803
- const sep = ` ${C.dim}│${C.reset} `;
804
- // Build one long content line — let terminal wrap it naturally
805
- const content = [
806
- `${C.amber}⚡ ${wd.tierLabel}${C.reset} v${C.bold}${wd.version}${C.reset} ${ramp(wd.ctxPct, 20)} ${C.bold}${wd.pctStr}${C.reset} ${wd.tokStr}/${wd.maxStr}`,
807
- wd.triggerLabel,
808
- `${C.cyan}${wd.modelStr}${C.reset}`,
809
- `${wd.chk} chk${wd.agentStr}${wd.turnStr}`,
810
- `${C.magenta}dup ${wd.dedupStr}${C.reset}`,
811
- `${C.gray}sess${C.reset} ${fmtTokens(wd.sessIn)}→${fmtTokens(wd.sessKept)} kept ${C.green}(${wd.sTxt}% freed)${C.reset}`,
812
- `${C.gray}all-time${C.reset} ${fmtTokens(wd.repoIn)}→${fmtTokens(wd.repoKept)} kept ${C.blue}(${wd.rTxt}% freed)${C.reset}`,
813
- `${wd.repoChk} chk/${wd.repoSess} sess`,
814
- `${C.gray}mem${C.reset} ${wd.embedderName} · ${wd.chk} chunks · ${C.blue}comp ${wd.compStr}${C.reset}`,
815
- `${C.gray}drift${C.reset} ${wd.driftStatus === "ok" ? C.green : C.amber}${wd.driftStatus}${C.reset}`,
816
- `${C.gray}compact${C.reset} ${sinceCompactStr(wd.sinceCompact)}`,
817
- ].join(sep);
818
- // Wrap to terminal width and pad each line
819
- const wrapped = wrapLine(content, width - 2); // 2-char indent
820
- const lines: string[] = [
821
- panelBar(width, "─"),
822
- ...wrapped.map((l) => panelLine(l, width)),
823
- ];
824
- // L4 — agents block (S27, count + status; per-agent tokens gated on P0)
825
- if (wd.agentsActive) {
826
- lines.push(
827
- panelLine(
828
- ` ${C.cyan}🤖 ${this.activeAgents} active${wd.turnStr}${C.reset}`,
829
- width,
830
- ),
831
- );
832
- }
833
- // L5 — live ticker / activity (♻ deduped … why, or tier trace, or pulsing)
834
- if (wd.tierTrace && wd.fresh) {
835
- lines.push(panelLine(` ${pulse}${wd.tierTrace}`, width));
836
- } else if (wd.ticker.length > 0) {
837
- const step = Math.floor(Date.now() / 250);
838
- const idx = wd.ticker.length - 1 - (step % wd.ticker.length);
839
- const head = wd.ticker[idx].text;
840
- const why = wd.lastWhy ? ` ${C.gray}· ${wd.lastWhy}${C.reset}` : "";
841
- const more =
842
- wd.ticker.length > 1
843
- ? ` ${C.dim}(+${wd.ticker.length - 1} more)${C.reset}`
844
- : "";
845
- lines.push(
846
- panelLine(
847
- ` ${wd.fresh ? C.teal : C.dim}${head}${why}${more}${C.reset}`,
848
- width,
849
- ),
850
- );
851
- } else if (wd.pulsing) {
852
- lines.push(panelLine(` ${pulse}${C.teal}compacting…${C.reset}`, width));
853
- }
854
- // bottom border
855
- lines.push(panelBar(width, "─"));
856
- return lines;
857
- }
858
-
859
- /** Active embedder name for the memory-store line (Trigram default / MiniLM). */
860
- private embedderName(): string {
861
- // MINILM_EMBEDDER flag lives in src/config/dedup.ts; read the same env var
862
- // the embedder factory uses so the label matches what's actually running.
863
- return process.env.MEGACOMPACT_MINILM === "true" ||
864
- process.env.MEGACOMPACT_MINILM === "1"
865
- ? "MiniLM"
866
- : "Trigram";
867
- }
868
-
869
- /** Cross-repo drift status (ok | warn), cached for 30s (opens the registry DB). */
870
- private driftStatus(): "ok" | "warn" {
871
- const now = Date.now();
872
- if (this.driftCache && now - this.driftCache.at < 30_000)
873
- return this.driftCache.status;
874
- let status: "ok" | "warn" = "ok";
875
- try {
876
- const report = detectCrossRepoDrift();
877
- status = report.totals.warn > 0 ? "warn" : "ok";
878
- } catch {
879
- status = "ok";
880
- }
881
- this.driftCache = { at: now, status };
882
- return status;
883
- }
884
-
885
- setStatus(ctx: ExtensionContext, text: string | undefined): void {
886
- this.statusKey = text;
887
- ctx.ui.setStatus(STATUS_KEY, text);
888
- }
889
-
890
- resetRuntime(sessionId: string | undefined): void {
891
- const sid = normalizeSessionId(sessionId);
892
- if (this.rt.sessionId === sid && this.rt.persistedThisSession) return; // same session, keep checkpoint memory
893
- this.rt = {
894
- sessionId: sid,
895
- persistedThisSession: false,
896
- lastCheckpointId: undefined,
897
- lastCompactedFrom: 0,
898
- lastCompactedTokens: 0,
899
- dedupSkips: 0,
900
- dedupAttempts: 0,
901
- tokensSaved: 0,
902
- lastCompactAt: null,
903
- lastNativeCompactAt: null,
904
- compactCount: 0,
905
- recallInjections: 0,
906
- cacheHitTokens: 0,
907
- lengthStopPending: false,
908
- };
909
- this.statusKey = undefined;
910
- this.activeAgents = 0;
911
- this.currentTurn = 0;
912
- this.lastActivityAt = 0;
913
- this.tierTrace = undefined;
914
- this.ticker.length = 0;
915
- this.pulsing = false;
916
- this.savedGoal = 50_000;
917
- this.lastWhy = undefined;
918
- }
919
-
920
- /**
921
- * Capture the active model/provider from ctx.model and persist it so cost
922
- * estimation + the dashboard can read real pricing. Cheap + idempotent-ish:
923
- * only writes a new row when the model id changes (models change rarely).
924
- */
925
- captureModel(ctx: ExtensionContext): void {
926
- const m = ctx.model;
927
- if (!m) {
928
- this.appendEvent("captureModel:no-model", { cwd: ctx.cwd });
929
- return;
930
- }
931
- if (
932
- this.currentModel &&
933
- this.currentModel.modelId === m.id &&
934
- this.currentModel.provider === m.provider
935
- )
936
- return;
937
- let providerName: string | null = null;
938
- try {
939
- providerName =
940
- ctx.modelRegistry?.getProviderDisplayName(m.provider) ?? null;
941
- } catch {
942
- /* optional */
943
- }
944
- const snap: Omit<ModelSnapshot, "capturedAt"> = {
945
- provider: m.provider,
946
- providerName,
947
- modelId: m.id,
948
- modelName: m.name ?? null,
949
- inputRate: m.cost?.input ?? 0,
950
- outputRate: m.cost?.output ?? 0,
951
- contextWindow: m.contextWindow ?? 0,
952
- maxTokens: m.maxTokens ?? 0,
953
- reasoning: !!m.reasoning,
954
- };
955
- this.currentModel = { ...snap, capturedAt: Date.now() };
956
- this.diagCaptureModelCalls++;
957
- const repo = resolveRepoRoot(ctx.cwd) ?? this.currentStateDir;
958
- // S26: previously a single silent `catch {}` hid every capture failure, so
959
- // model_snapshots stayed empty and the cost card read $0.00 with zero signal.
960
- // Split per-write + append to events.log (always-on, dashboard live-streams
961
- // it) + bump a DIAG counter so a live capture surfaces the root cause.
962
- try {
963
- recordModelSnapshot(repo, snap, this.currentStateDir);
964
- this.appendEvent("captureModel:recorded", {
965
- repo,
966
- modelId: snap.modelId,
967
- provider: snap.provider,
968
- inputRate: snap.inputRate,
969
- outputRate: snap.outputRate,
970
- });
971
- } catch (e) {
972
- this.diagCaptureModelFails++;
973
- this.appendEvent("captureModel:record-failed", {
974
- repo,
975
- modelId: snap.modelId,
976
- error: e instanceof Error ? e.message : String(e),
977
- stack: e instanceof Error ? e.stack : undefined,
978
- });
979
- }
980
- try {
981
- // Denormalize the active model into the machine-wide index so the
982
- // All-repos dashboard table can show provider/model per repo without
983
- // opening every repo's DB. Best-effort + non-fatal.
984
- recordRepoModel(repo, {
985
- provider: snap.provider,
986
- providerName: snap.providerName,
987
- modelName: snap.modelName,
988
- inputRate: snap.inputRate,
989
- outputRate: snap.outputRate,
990
- stateDir: this.currentStateDir,
991
- displayName: repo.split(/[\\/]/).filter(Boolean).pop() ?? repo,
992
- });
993
- } catch (e) {
994
- this.appendEvent("captureModel:index-record-failed", {
995
- repo,
996
- modelId: snap.modelId,
997
- error: e instanceof Error ? e.message : String(e),
998
- });
999
- }
1000
- }
1001
-
1002
- /**
1003
- * Append a structured line to the repo's events.log — the always-on
1004
- * diagnostics sink the dashboard live-streams. Unlike this.logger (gated by
1005
- * config.debug), this fires in production, so capture failures surface during
1006
- * a real capture even with debugging off. Best-effort + non-fatal.
1007
- */
1008
- private appendEvent(event: string, fields: Record<string, unknown>): void {
1009
- try {
1010
- mkdirSync(this.currentStateDir, { recursive: true });
1011
- appendFileSync(
1012
- join(this.currentStateDir, "events.log"),
1013
- JSON.stringify({ ts: Date.now(), event, ...fields }) + "\n",
1014
- );
1015
- } catch {
1016
- /* non-fatal */
1017
- }
1018
- }
1019
-
1020
- /** S21: state dir of the currently bound repo (where memories live). */
1021
- getStateDir(): string {
1022
- return this.currentStateDir;
1023
- }
1024
-
1025
- /** Build the sync onTier callback that paints the live per-tier trace. */
1026
- makeTierCallback(
1027
- ctx: ExtensionContext,
1028
- ): (ev: {
1029
- tier: "L0" | "L1" | "L2" | "new";
1030
- status: "scanning" | "deduped" | "passed" | "stored";
1031
- detail?: string;
1032
- }) => void {
1033
- const order: Array<"L0" | "L1" | "L2" | "new"> = ["L0", "L1", "L2", "new"];
1034
- const seen = new Map<string, string>();
1035
- const glyph = (status: string) =>
1036
- status === "deduped"
1037
- ? `${C.green}✓${C.reset}`
1038
- : status === "passed"
1039
- ? `${C.dim}○${C.reset}`
1040
- : status === "scanning"
1041
- ? `${C.amber}…${C.reset}`
1042
- : `${C.cyan}●${C.reset}`;
1043
- return (ev) => {
1044
- const label =
1045
- ev.tier === "new"
1046
- ? `${C.cyan}stored${C.reset}`
1047
- : `${ev.tier} ${glyph(ev.status)}` +
1048
- (ev.detail ? ` ${C.gray}(${ev.detail})${C.reset}` : "");
1049
- // Show the most recent outcome per tier (collapses re-fires).
1050
- seen.set(ev.tier, label);
1051
- const show: string[] = [];
1052
- for (const t of order) if (seen.has(t)) show.push(seen.get(t)!);
1053
- this.tierTrace = `${C.teal}⚙${C.reset} ${show.join(` ${C.gray}→${C.reset} `)}`;
1054
- this.lastActivityAt = Date.now();
1055
- try {
1056
- this.snapshot(ctx);
1057
- } catch {
1058
- /* non-fatal */
1059
- }
1060
- };
1061
- }
1062
-
1063
- // Phase 3 — recall/activity ticker ring buffer.
1064
- pushTicker(text: string): void {
1065
- this.ticker.push({ text, at: Date.now() });
1066
- while (this.ticker.length > this.TICKER_MAX) this.ticker.shift();
1067
- this.lastActivityAt = Date.now();
1068
- }
1069
-
1070
- /** Convert the messages pi hands us in the `context` event into the engine view. */
1071
- engineView(messages: AgentMessage[]): ReturnType<typeof toEngineMessages> {
1072
- return toEngineMessages(messages);
1073
- }
1074
- }
1075
-
1076
- /**
1077
- * Latest user message text — used as the auto-inline recall query.
1078
- * Kept as a free function (not instance state) since it only reads ctx.
1079
- */
1080
- export function recentUserQuery(ctx: ExtensionContext): string {
1081
- try {
1082
- const entries = ctx.sessionManager.getEntries();
1083
- for (let i = entries.length - 1; i >= 0; i--) {
1084
- const msgs = sessionEntryToContextMessages(entries[i]);
1085
- for (let j = msgs.length - 1; j >= 0; j--) {
1086
- if (msgs[j].role === "user") {
1087
- const c = (msgs[j] as { content: unknown }).content;
1088
- if (typeof c === "string") return c;
1089
- if (Array.isArray(c)) return c.map((b: any) => b.text).join(" ");
1090
- }
1091
- }
1092
- }
1093
- } catch {
1094
- /* best-effort */
1095
- }
1096
- return "";
1097
- }
16
+ export * from "./mega-runtime/widget.js";
17
+ export * from "./mega-runtime/helpers.js";
18
+ export * from "./mega-runtime/state.js";
19
+ export * from "./mega-runtime/query.js";