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