pi-mega-compact 0.7.8 → 0.7.9

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 (112) hide show
  1. package/README.md +11 -12
  2. package/dist/extensions/dashboard-server/helpers.js +37 -0
  3. package/dist/extensions/dashboard-server/html/all-repos-tab.js +26 -0
  4. package/dist/extensions/dashboard-server/html/body-open.js +23 -0
  5. package/dist/extensions/dashboard-server/html/current-repo-tab.js +130 -0
  6. package/dist/extensions/dashboard-server/html/head-open.js +16 -0
  7. package/dist/extensions/dashboard-server/html/high-score-tab.js +25 -0
  8. package/dist/extensions/dashboard-server/html/repo-detail-modal.js +26 -0
  9. package/dist/extensions/dashboard-server/html/script.js +259 -0
  10. package/dist/extensions/dashboard-server/html/styles.js +103 -0
  11. package/dist/extensions/dashboard-server/html/summary-tab.js +19 -0
  12. package/dist/extensions/dashboard-server/html-template.js +41 -0
  13. package/dist/extensions/dashboard-server/html.js +756 -0
  14. package/dist/extensions/dashboard-server/index-reader.js +133 -0
  15. package/dist/extensions/dashboard-server/server.js +370 -0
  16. package/dist/extensions/dashboard-server/snapshot.js +43 -0
  17. package/dist/extensions/dashboard-server/state.js +30 -0
  18. package/dist/extensions/dashboard-server/types.js +5 -0
  19. package/dist/extensions/dashboard-server.js +7 -1315
  20. package/dist/extensions/mega-commands.js +162 -134
  21. package/dist/extensions/mega-compact.test.js +90 -21
  22. package/dist/extensions/mega-conflict-cmds.js +5 -1
  23. package/dist/extensions/mega-dashboard-cmds.js +29 -22
  24. package/dist/extensions/mega-db-cmds.js +11 -2
  25. package/dist/extensions/mega-events/agent-handlers.js +173 -0
  26. package/dist/extensions/mega-events/compact-handlers.js +133 -0
  27. package/dist/extensions/mega-events/context-handler.js +249 -0
  28. package/dist/extensions/mega-events/register.js +21 -0
  29. package/dist/extensions/mega-events/session-handlers.js +142 -0
  30. package/dist/extensions/mega-events.js +15 -699
  31. package/dist/extensions/mega-pipeline/compact.js +324 -0
  32. package/dist/extensions/mega-pipeline/memory-review.js +38 -0
  33. package/dist/extensions/mega-pipeline/recall.js +147 -0
  34. package/dist/extensions/mega-pipeline.js +9 -480
  35. package/dist/extensions/mega-runtime/helpers.js +40 -0
  36. package/dist/extensions/mega-runtime/query.js +29 -0
  37. package/dist/extensions/mega-runtime/state.js +711 -0
  38. package/dist/extensions/mega-runtime/widget.js +197 -0
  39. package/dist/extensions/mega-runtime.js +15 -947
  40. package/dist/src/store/sqlite/checkpoints.js +145 -0
  41. package/dist/src/store/sqlite/connection.js +35 -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/global-index.js +224 -0
  45. package/dist/src/store/sqlite/index-store.js +167 -0
  46. package/dist/src/store/sqlite/maintenance.js +235 -0
  47. package/dist/src/store/sqlite/memories.js +164 -0
  48. package/dist/src/store/sqlite/memory.js +54 -0
  49. package/dist/src/store/sqlite/meta.js +82 -0
  50. package/dist/src/store/sqlite/minhash-lsh.js +47 -0
  51. package/dist/src/store/sqlite/model-snapshots.js +47 -0
  52. package/dist/src/store/sqlite/raptor.js +57 -0
  53. package/dist/src/store/sqlite/raw-transcript.js +134 -0
  54. package/dist/src/store/sqlite/schema.js +250 -0
  55. package/dist/src/store/sqlite/session-state.js +28 -0
  56. package/dist/src/store/sqlite/sessions.js +39 -0
  57. package/dist/src/store/sqlite/stats.js +66 -0
  58. package/dist/src/store/sqlite/transaction.js +19 -0
  59. package/dist/src/store/sqlite/utils.js +120 -0
  60. package/dist/src/store/sqlite.js +20 -1607
  61. package/dist/src/vectorStore/add.js +260 -0
  62. package/dist/src/vectorStore/dedup.js +52 -0
  63. package/dist/src/vectorStore/index.js +10 -0
  64. package/dist/src/vectorStore/queries.js +83 -0
  65. package/dist/src/vectorStore/search.js +95 -0
  66. package/dist/src/vectorStore/session.js +19 -0
  67. package/dist/src/vectorStore/store.js +105 -0
  68. package/dist/src/vectorStore/types.js +6 -0
  69. package/dist/src/vectorStore/utils.js +23 -0
  70. package/extensions/dashboard-server/html.ts +758 -0
  71. package/extensions/dashboard-server/index-reader.ts +130 -0
  72. package/extensions/dashboard-server/server.ts +358 -0
  73. package/extensions/dashboard-server/snapshot.ts +44 -0
  74. package/extensions/dashboard-server/state.ts +33 -0
  75. package/extensions/dashboard-server/types.ts +134 -0
  76. package/extensions/dashboard-server.ts +7 -1431
  77. package/extensions/mega-commands.ts +33 -10
  78. package/extensions/mega-compact.test.ts +198 -43
  79. package/extensions/mega-conflict-cmds.ts +6 -2
  80. package/extensions/mega-dashboard-cmds.ts +30 -23
  81. package/extensions/mega-db-cmds.ts +11 -3
  82. package/extensions/mega-events/agent-handlers.ts +214 -0
  83. package/extensions/mega-events/compact-handlers.ts +164 -0
  84. package/extensions/mega-events/context-handler.ts +290 -0
  85. package/extensions/mega-events/register.ts +37 -0
  86. package/extensions/mega-events/session-handlers.ts +165 -0
  87. package/extensions/mega-events.ts +15 -780
  88. package/extensions/mega-pipeline/compact.ts +366 -0
  89. package/extensions/mega-pipeline/memory-review.ts +46 -0
  90. package/extensions/mega-pipeline/recall.ts +165 -0
  91. package/extensions/mega-pipeline.ts +9 -537
  92. package/extensions/mega-runtime/helpers.ts +68 -0
  93. package/extensions/mega-runtime/query.ts +29 -0
  94. package/extensions/mega-runtime/state.ts +797 -0
  95. package/extensions/mega-runtime/widget.ts +258 -0
  96. package/extensions/mega-runtime.ts +15 -1093
  97. package/package.json +4 -3
  98. package/src/store/sqlite/checkpoints.ts +204 -0
  99. package/src/store/sqlite/dedup-mirror.ts +114 -0
  100. package/src/store/sqlite/foundation.ts +63 -0
  101. package/src/store/sqlite/global-index.ts +305 -0
  102. package/src/store/sqlite/maintenance.ts +294 -0
  103. package/src/store/sqlite/memories.ts +217 -0
  104. package/src/store/sqlite/meta.ts +108 -0
  105. package/src/store/sqlite/model-snapshots.ts +83 -0
  106. package/src/store/sqlite/raptor.ts +107 -0
  107. package/src/store/sqlite/raw-transcript.ts +221 -0
  108. package/src/store/sqlite/schema.ts +258 -0
  109. package/src/store/sqlite/session-state.ts +38 -0
  110. package/src/store/sqlite/stats.ts +127 -0
  111. package/src/store/sqlite/utils.ts +125 -0
  112. package/src/store/sqlite.ts +20 -2204
@@ -0,0 +1,258 @@
1
+ /**
2
+ * widget.ts — above-editor widget rendering helpers (free functions, consts,
3
+ * and interfaces) extracted from the original mega-runtime.ts monolith.
4
+ *
5
+ * These are pure rendering primitives with zero runtime-state dependencies;
6
+ * the `MegaRuntime` class (state.ts) imports them to paint the panel.
7
+ */
8
+
9
+ // pi-tui's OWN width measurers — the same functions pi-tui uses to enforce its
10
+ // render-width check ("visibleWidth(line) > width" → crash). Measuring with
11
+ // these guarantees we never disagree on a grapheme's width (e.g. RGI emoji
12
+ // like ⚡ which pi-tui counts as 2 but a naive regex counts as 1), and
13
+ // truncateToWidth both pads AND hard-clips to exactly `width` cells, so no
14
+ // off-by-one can trip the strict `> width` guard. (Fix for the
15
+ // `Rendered line N exceeds terminal width (W > W-1)` crash.)
16
+ import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
17
+
18
+ // ── ANSI palette ───────────────────────────────────────────────────────────
19
+ // The pi TUI's Text component preserves ANSI escape codes (see wrapTextWithAnsi),
20
+ // so raw escapes render as colors. No chalk dependency needed — these are just
21
+ // strings. Exported because mega-pipeline.ts and mega-commands.ts import `C`
22
+ // from the mega-runtime barrel.
23
+ export const C = {
24
+ reset: "\x1b[0m",
25
+ dim: "\x1b[2m",
26
+ bold: "\x1b[1m",
27
+ amber: "\x1b[38;5;214m", // tier / ready
28
+ green: "\x1b[38;5;120m", // saved
29
+ cyan: "\x1b[38;5;51m", // used / live activity
30
+ teal: "\x1b[38;5;37m", // processing (compress/dedup)
31
+ magenta: "\x1b[38;5;201m", // dedup rate
32
+ blue: "\x1b[38;5;75m", // repo totals
33
+ gray: "\x1b[38;5;245m", // labels
34
+ red: "\x1b[38;5;203m", // pressure / overflow
35
+ };
36
+
37
+ const PULSE = ["◐", "◓", "◑", "◒"];
38
+
39
+ // ── Full-width widget panel helpers ────────────────────────────────────────
40
+ // pi's above-editor widget renderer (a Container of Text lines) does NOT pass
41
+ // a terminal width to setWidget(), so lines render left-aligned by default. To
42
+ // make the widget read as a full-width status panel we pad each line to the
43
+ // real terminal width with a background fill. NOTE: C.reset is a FULL SGR
44
+ // reset, so we re-apply the panel bg after every reset to keep the background
45
+ // continuous under colored text (and under pi's own trailing reset).
46
+ const PANEL_BG = "\x1b[48;5;236m"; // dark slate panel background
47
+ const PANEL_RST = "\x1b[0m" + PANEL_BG; // reset fg but retain panel bg
48
+
49
+ // (Visible-width measurement is delegated to pi-tui's `visibleWidth` — imported
50
+ // above — so our width math can never diverge from pi-tui's render-width check.)
51
+
52
+ /** Wrap a string (with ANSI codes) to fit within `maxWidth` visible chars.
53
+ * Splits at │ separators or whitespace when possible. */
54
+ function wrapLine(text: string, maxWidth: number): string[] {
55
+ if (maxWidth <= 0) return [text];
56
+ const result: string[] = [];
57
+ let current = "";
58
+ let currentW = 0;
59
+ // Split at │ boundaries first
60
+ const segments = text.split("│");
61
+ for (let i = 0; i < segments.length; i++) {
62
+ const seg = (i > 0 ? "│" : "") + segments[i];
63
+ const segW = visibleWidth(PANEL_BG + seg.replace(/\x1b\[0m/g, PANEL_RST));
64
+ if (currentW + segW <= maxWidth || currentW === 0) {
65
+ current += seg;
66
+ currentW += segW;
67
+ } else {
68
+ result.push(current);
69
+ current = seg;
70
+ currentW = segW;
71
+ }
72
+ }
73
+ if (current) result.push(current);
74
+ return result;
75
+ }
76
+
77
+ function panelLine(content: string, width: number): string {
78
+ if (width <= 0) return "";
79
+ // Apply the panel background; swap every inner full-reset for a reset that
80
+ // re-applies the bg so the fill stays continuous under colored text.
81
+ const withBg = PANEL_BG + content.replace(/\x1b\[0m/g, PANEL_RST);
82
+ // truncateToWidth(line, width, "", true) returns EXACTLY `width` visible cells
83
+ // (by pi-tui's measure), ANSI-preserved, space-padded. It hard-clips overflow
84
+ // — so even a segment wider than `width` (or a width-rule mismatch) can never
85
+ // produce a line that trips pi-tui's strict `visibleWidth(line) > width` check.
86
+ return truncateToWidth(withBg, width, "", true) + "\x1b[0m";
87
+ }
88
+
89
+ /** A full-width hairline bar (top/bottom border of the panel). */
90
+ function panelBar(width: number, ch = "─"): string {
91
+ // `─` (U+2500) is narrow (1 cell) in both our measure and pi-tui's, so a
92
+ // `ch.repeat(width)` bar is exactly `width` cells and already passes the
93
+ // `> width` guard. truncateToWidth is belt-and-suspenders in case `ch` is
94
+ // ever swapped for a wide/fullwidth character.
95
+ return truncateToWidth(PANEL_BG + ch.repeat(Math.max(0, width)), width, "", false) + "\x1b[0m";
96
+ }
97
+
98
+ /** Token-count formatter: M at/above 1e6, k at/above 1e3, raw below.
99
+ * 5,472,700 → "5.5mil", 24,100 → "24.1k", 142 → "142". */
100
+ function fmtTokens(x: number): string {
101
+ return x >= 1_000_000
102
+ ? `${(x / 1_000_000).toFixed(1)}mil`
103
+ : x >= 1000
104
+ ? `${(x / 1000).toFixed(1)}k`
105
+ : `${Math.round(x)}`;
106
+ }
107
+
108
+ /** Retro gradient bar — `w` cells shaded by fill position (green→amber→red).
109
+ * Used for CONTEXT fill where low=green (room) and high=red (near the limit). */
110
+ function ramp(pct: number, w = 12): string {
111
+ const cells = ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"];
112
+ const scaled = Math.max(0, Math.min(w, pct * w));
113
+ const full = Math.floor(scaled);
114
+ const frac = scaled - full;
115
+ const fracCell = frac > 0 ? cells[Math.round(frac * (cells.length - 1))] : "";
116
+ let out = "";
117
+ for (let i = 0; i < full; i++)
118
+ out += (i / w < 0.6 ? C.green : i / w < 0.85 ? C.amber : C.red) + "█";
119
+ if (fracCell)
120
+ out +=
121
+ (full / w < 0.6 ? C.green : full / w < 0.85 ? C.amber : C.red) + fracCell;
122
+ out +=
123
+ C.dim + "░".repeat(Math.max(0, w - full - (fracCell ? 1 : 0))) + C.reset;
124
+ return out;
125
+ }
126
+
127
+ /** Human "time since" string from a millisecond delta (or null → "never"). */
128
+ function sinceCompactStr(ms: number | null): string {
129
+ if (ms == null) return "never";
130
+ const s = Math.floor(ms / 1000);
131
+ if (s < 60) return `${s}s ago`;
132
+ const m = Math.floor(s / 60);
133
+ if (m < 60) return `${m}m ago`;
134
+ const h = Math.floor(m / 60);
135
+ if (h < 24) return `${h}h ago`;
136
+ return `${Math.floor(h / 24)}d ago`;
137
+ }
138
+
139
+ /** Ticker ring-buffer entry (recall/activity history for the widget footer). */
140
+ export interface TickerEntry {
141
+ text: string;
142
+ at: number;
143
+ }
144
+
145
+ /** Immutable snapshot of everything the above-editor widget needs to render.
146
+ * Computed once per `snapshot()` (event-driven) and read by `buildWidgetLines`
147
+ * on every TUI render frame, so frame rendering stays allocation-cheap and the
148
+ * panel auto-fits whatever width pi passes to the setWidget factory. */
149
+ export interface WidgetData {
150
+ version: string;
151
+ tierLabel: string;
152
+ triggerLabel: string;
153
+ pctStr: string;
154
+ tokStr: string;
155
+ maxStr: string;
156
+ ctxPct: number;
157
+ chk: number;
158
+ agentStr: string;
159
+ turnStr: string;
160
+ dedupStr: string;
161
+ sessIn: number;
162
+ sessKept: number;
163
+ sTxt: string;
164
+ repoIn: number;
165
+ repoKept: number;
166
+ rTxt: string;
167
+ repoChk: number;
168
+ repoSess: number;
169
+ modelStr: string;
170
+ sinceCompact: number | null;
171
+ embedderName: string;
172
+ compStr: string;
173
+ driftStatus: "ok" | "warn";
174
+ agentsActive: boolean;
175
+ fresh: boolean;
176
+ ticker: TickerEntry[];
177
+ lastWhy: string | undefined;
178
+ tierTrace: string | undefined;
179
+ pulsing: boolean;
180
+ }
181
+
182
+ // ── buildWidgetLines ───────────────────────────────────────────────────────
183
+ // Kept as a free function (not a MegaRuntime method) so state.ts stays
184
+ // focused on state management. It reads the WidgetData snapshot + the live
185
+ // activeAgents counter (passed in) and returns the panel lines.
186
+
187
+ /** Build the full-width panel lines from the latest snapshot. Cheap: reads
188
+ * only the WidgetData + a couple of live counters; no DB/IO. */
189
+ export function buildWidgetLines(
190
+ wd: WidgetData | null,
191
+ width: number,
192
+ activeAgents: number,
193
+ ): string[] {
194
+ if (!wd) {
195
+ return [
196
+ panelBar(width, "─"),
197
+ panelLine(" mega-compact: warming up…", width),
198
+ panelBar(width, "─"),
199
+ ];
200
+ }
201
+ const pulse = wd.pulsing
202
+ ? `${C.cyan}${PULSE[Math.floor(Date.now() / 250) % PULSE.length]}${C.reset} `
203
+ : "";
204
+ const sep = ` ${C.dim}│${C.reset} `;
205
+ // Build one long content line — let terminal wrap it naturally
206
+ const content = [
207
+ `${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}`,
208
+ wd.triggerLabel,
209
+ `${C.cyan}${wd.modelStr}${C.reset}`,
210
+ `${wd.chk} chk${wd.agentStr}${wd.turnStr}`,
211
+ `${C.magenta}dup ${wd.dedupStr}${C.reset}`,
212
+ `${C.gray}sess${C.reset} ${fmtTokens(wd.sessIn)}→${fmtTokens(wd.sessKept)} kept ${C.green}(${wd.sTxt}% freed)${C.reset}`,
213
+ `${C.gray}all-time${C.reset} ${fmtTokens(wd.repoIn)}→${fmtTokens(wd.repoKept)} kept ${C.blue}(${wd.rTxt}% freed)${C.reset}`,
214
+ `${wd.repoChk} chk/${wd.repoSess} sess`,
215
+ `${C.gray}mem${C.reset} ${wd.embedderName} · ${wd.chk} chunks · ${C.blue}comp ${wd.compStr}${C.reset}`,
216
+ `${C.gray}drift${C.reset} ${wd.driftStatus === "ok" ? C.green : C.amber}${wd.driftStatus}${C.reset}`,
217
+ `${C.gray}compact${C.reset} ${sinceCompactStr(wd.sinceCompact)}`,
218
+ ].join(sep);
219
+ // Wrap to terminal width and pad each line
220
+ const wrapped = wrapLine(content, width - 2); // 2-char indent
221
+ const lines: string[] = [
222
+ panelBar(width, "─"),
223
+ ...wrapped.map((l) => panelLine(l, width)),
224
+ ];
225
+ // L4 — agents block (S27, count + status; per-agent tokens gated on P0)
226
+ if (wd.agentsActive) {
227
+ lines.push(
228
+ panelLine(
229
+ ` ${C.cyan}🤖 ${activeAgents} active${wd.turnStr}${C.reset}`,
230
+ width,
231
+ ),
232
+ );
233
+ }
234
+ // L5 — live ticker / activity (♻ deduped … why, or tier trace, or pulsing)
235
+ if (wd.tierTrace && wd.fresh) {
236
+ lines.push(panelLine(` ${pulse}${wd.tierTrace}`, width));
237
+ } else if (wd.ticker.length > 0) {
238
+ const step = Math.floor(Date.now() / 250);
239
+ const idx = wd.ticker.length - 1 - (step % wd.ticker.length);
240
+ const head = wd.ticker[idx].text;
241
+ const why = wd.lastWhy ? ` ${C.gray}· ${wd.lastWhy}${C.reset}` : "";
242
+ const more =
243
+ wd.ticker.length > 1
244
+ ? ` ${C.dim}(+${wd.ticker.length - 1} more)${C.reset}`
245
+ : "";
246
+ lines.push(
247
+ panelLine(
248
+ ` ${wd.fresh ? C.teal : C.dim}${head}${why}${more}${C.reset}`,
249
+ width,
250
+ ),
251
+ );
252
+ } else if (wd.pulsing) {
253
+ lines.push(panelLine(` ${pulse}${C.teal}compacting…${C.reset}`, width));
254
+ }
255
+ // bottom border
256
+ lines.push(panelBar(width, "─"));
257
+ return lines;
258
+ }