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