pi-mega-compact 0.8.23 → 0.8.25

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 (157) hide show
  1. package/README.md +26 -0
  2. package/dist/extensions/dashboard-server/api-contracts/endpoints.js +8 -0
  3. package/dist/extensions/dashboard-server/api-contracts/game-types.js +7 -0
  4. package/dist/extensions/mega-compact-s38.test.js +263 -14
  5. package/dist/extensions/mega-compact.js +15 -0
  6. package/dist/extensions/mega-config.js +3 -0
  7. package/dist/extensions/mega-events/agent-handlers.js +211 -26
  8. package/dist/extensions/mega-events/context-handler.js +45 -7
  9. package/dist/extensions/mega-events/error-classifier.js +125 -18
  10. package/dist/extensions/mega-pipeline/compact.js +24 -13
  11. package/dist/extensions/mega-pipeline/recall.js +31 -2
  12. package/dist/extensions/mega-runtime/append-event.js +24 -0
  13. package/dist/extensions/mega-runtime/bind-repo.js +65 -0
  14. package/dist/extensions/mega-runtime/capture-model.js +87 -0
  15. package/dist/extensions/mega-runtime/dashboard-snapshot.js +122 -0
  16. package/dist/extensions/mega-runtime/effects.js +86 -0
  17. package/dist/extensions/mega-runtime/engine-view.js +11 -0
  18. package/dist/extensions/mega-runtime/game-state.js +116 -0
  19. package/dist/extensions/mega-runtime/get-state-dir.js +10 -0
  20. package/dist/extensions/mega-runtime/perf.js +49 -0
  21. package/dist/extensions/mega-runtime/pressure-getters.js +64 -0
  22. package/dist/extensions/mega-runtime/render-widget.js +17 -0
  23. package/dist/extensions/mega-runtime/reset-runtime.js +50 -0
  24. package/dist/extensions/mega-runtime/runtime-helpers.js +73 -0
  25. package/dist/extensions/mega-runtime/runtime-snapshot.js +208 -0
  26. package/dist/extensions/mega-runtime/runtime.js +405 -0
  27. package/dist/extensions/mega-runtime/snapshot.js +142 -0
  28. package/dist/extensions/mega-runtime/state.js +5 -1151
  29. package/dist/extensions/mega-runtime/status.js +11 -0
  30. package/dist/extensions/mega-runtime/widget-ansi.js +207 -0
  31. package/dist/extensions/mega-runtime/widget-types.js +8 -0
  32. package/dist/extensions/mega-runtime/widget.js +15 -204
  33. package/dist/extensions/openclaw-mega-compact.js +291 -0
  34. package/dist/src/boundary.js +79 -43
  35. package/dist/src/boundary.test.js +119 -2
  36. package/dist/src/canary.js +10 -0
  37. package/dist/src/config/dedup.js +14 -0
  38. package/dist/src/config.js +3 -1
  39. package/dist/src/dedup/raptor/buildHistory.js +164 -0
  40. package/dist/src/dedup/raptor/buildHistory.test.js +292 -0
  41. package/dist/src/dedup/raptor/index.js +38 -0
  42. package/dist/src/dedup/raptor/multilevel-serve.test.js +229 -0
  43. package/dist/src/dedup/raptor/multilevel.js +17 -5
  44. package/dist/src/dedup/raptor/multilevel.test.js +36 -1
  45. package/dist/src/dedup/raptor/raptor.test.js +43 -0
  46. package/dist/src/dedup/raptor/retrieval.js +14 -2
  47. package/dist/src/dedup/raptor/retrieval.test.js +95 -0
  48. package/dist/src/dedup/raptor/serve-gate.test.js +298 -0
  49. package/dist/src/dedup/raptor/summarizer.js +1 -0
  50. package/dist/src/dedup/raptor/tree.js +16 -2
  51. package/dist/src/engine.js +18 -2
  52. package/dist/src/httpEmbedder.js +96 -6
  53. package/dist/src/httpEmbedder.test.js +277 -0
  54. package/dist/src/mechanical-fix.test.js +65 -0
  55. package/dist/src/minilm.js +92 -0
  56. package/dist/src/raptor-inject-summaries.test.js +155 -0
  57. package/dist/src/recall.js +135 -21
  58. package/dist/src/recall.test.js +179 -4
  59. package/dist/src/store/sqlite/dedup-mirror.js +32 -15
  60. package/dist/src/store/sqlite/maintenance.js +2 -2
  61. package/dist/src/store/sqlite/mechanical-fix.test.js +146 -0
  62. package/dist/src/store/sqlite/memories.js +5 -5
  63. package/dist/src/store/sqlite/meta.js +1 -1
  64. package/dist/src/store/sqlite/raptor.js +56 -17
  65. package/dist/src/store/sqlite/raptor.test.js +106 -0
  66. package/dist/src/store/sqlite/schema.js +90 -1
  67. package/dist/src/store/sqlite/session-state.js +9 -3
  68. package/dist/src/store/sqlite/stats.js +9 -5
  69. package/dist/src/store/sqlite/turns.js +181 -0
  70. package/dist/src/store/sqlite/turns.test.js +183 -0
  71. package/dist/src/store/sqlite/utils.js +15 -4
  72. package/dist/src/store/sqlite.js +1 -0
  73. package/dist/src/store.js +2 -2
  74. package/dist/src/vector-search-cache.test.js +157 -0
  75. package/dist/src/vector-search.js +107 -15
  76. package/dist/src/vectorStore.js +36 -8
  77. package/dist/src/wordpiece.js +129 -0
  78. package/extensions/dashboard-client/dist/assets/index-D_WtU2TV.js.map +1 -1
  79. package/extensions/dashboard-server/api-contracts/endpoints.ts +30 -155
  80. package/extensions/dashboard-server/api-contracts/game-types.ts +172 -0
  81. package/extensions/mega-compact-s38.test.ts +259 -14
  82. package/extensions/mega-compact.ts +15 -0
  83. package/extensions/mega-config.ts +18 -0
  84. package/extensions/mega-dashboard.ts +10 -1
  85. package/extensions/mega-events/agent-handlers.ts +211 -26
  86. package/extensions/mega-events/context-handler.ts +43 -7
  87. package/extensions/mega-events/error-classifier.ts +125 -17
  88. package/extensions/mega-pipeline/compact.ts +28 -16
  89. package/extensions/mega-pipeline/recall.ts +34 -2
  90. package/extensions/mega-runtime/DECOMPOSITION.md +180 -0
  91. package/extensions/mega-runtime/README.md +38 -0
  92. package/extensions/mega-runtime/append-event.ts +40 -0
  93. package/extensions/mega-runtime/bind-repo.ts +81 -0
  94. package/extensions/mega-runtime/capture-model.ts +101 -0
  95. package/extensions/mega-runtime/dashboard-snapshot.ts +181 -0
  96. package/extensions/mega-runtime/effects.ts +129 -0
  97. package/extensions/mega-runtime/engine-view.ts +17 -0
  98. package/extensions/mega-runtime/game-state.ts +149 -0
  99. package/extensions/mega-runtime/get-state-dir.ts +19 -0
  100. package/extensions/mega-runtime/helpers.ts +25 -1
  101. package/extensions/mega-runtime/perf.ts +60 -0
  102. package/extensions/mega-runtime/pressure-getters.ts +96 -0
  103. package/extensions/mega-runtime/render-widget.ts +41 -0
  104. package/extensions/mega-runtime/runtime-helpers.ts +119 -0
  105. package/extensions/mega-runtime/runtime-snapshot.ts +293 -0
  106. package/extensions/mega-runtime/runtime.ts +483 -0
  107. package/extensions/mega-runtime/snapshot.ts +230 -0
  108. package/extensions/mega-runtime/state.ts +5 -1268
  109. package/extensions/mega-runtime/status.ts +26 -0
  110. package/extensions/mega-runtime/widget-ansi.ts +217 -0
  111. package/extensions/mega-runtime/widget-types.ts +80 -0
  112. package/extensions/mega-runtime/widget.ts +34 -285
  113. package/package.json +1 -1
  114. package/src/boundary.test.ts +128 -2
  115. package/src/boundary.ts +75 -39
  116. package/src/canary.ts +10 -0
  117. package/src/config/dedup.ts +25 -0
  118. package/src/config.ts +3 -1
  119. package/src/dedup/raptor/buildHistory.test.ts +353 -0
  120. package/src/dedup/raptor/buildHistory.ts +259 -0
  121. package/src/dedup/raptor/index.ts +38 -0
  122. package/src/dedup/raptor/multilevel-serve.test.ts +273 -0
  123. package/src/dedup/raptor/multilevel.test.ts +47 -0
  124. package/src/dedup/raptor/multilevel.ts +18 -8
  125. package/src/dedup/raptor/raptor.test.ts +59 -0
  126. package/src/dedup/raptor/retrieval.test.ts +118 -0
  127. package/src/dedup/raptor/retrieval.ts +14 -2
  128. package/src/dedup/raptor/serve-gate.test.ts +348 -0
  129. package/src/dedup/raptor/summarizer.ts +1 -0
  130. package/src/dedup/raptor/tree.ts +17 -2
  131. package/src/engine.ts +32 -3
  132. package/src/httpEmbedder.test.ts +286 -0
  133. package/src/httpEmbedder.ts +98 -8
  134. package/src/mechanical-fix.test.ts +70 -0
  135. package/src/raptor-inject-summaries.test.ts +212 -0
  136. package/src/recall.test.ts +220 -4
  137. package/src/recall.ts +151 -22
  138. package/src/store/sqlite/dedup-mirror.ts +35 -18
  139. package/src/store/sqlite/maintenance.ts +2 -2
  140. package/src/store/sqlite/mechanical-fix.test.ts +162 -0
  141. package/src/store/sqlite/memories.ts +5 -5
  142. package/src/store/sqlite/meta.ts +1 -1
  143. package/src/store/sqlite/raptor.test.ts +139 -0
  144. package/src/store/sqlite/raptor.ts +135 -81
  145. package/src/store/sqlite/schema.ts +90 -1
  146. package/src/store/sqlite/session-state.ts +9 -3
  147. package/src/store/sqlite/stats.ts +10 -8
  148. package/src/store/sqlite/turns.test.ts +218 -0
  149. package/src/store/sqlite/turns.ts +292 -0
  150. package/src/store/sqlite/utils.ts +14 -4
  151. package/src/store/sqlite.ts +1 -0
  152. package/src/store.ts +9 -2
  153. package/src/vector-search-cache.test.ts +190 -0
  154. package/src/vector-search.ts +273 -156
  155. package/src/vectorStore.ts +443 -382
  156. package/dist/extensions/dashboard-client/src/hooks/useApi.js +0 -51
  157. package/dist/extensions/dashboard-client/src/hooks/useSSE.js +0 -63
@@ -0,0 +1,230 @@
1
+ /**
2
+ * snapshot.ts — pure computation of the live stats widget data.
3
+ *
4
+ * Extracted from `MegaRuntime.snapshot()` in state.ts so the computation is
5
+ * a pure function with no side effects beyond returning data. The caller
6
+ * (state.ts) handles side-effects (level-up flare, flare consumption, render).
7
+ */
8
+
9
+ import { C } from "./widget.js";
10
+ import { ownVersion } from "./helpers.js";
11
+ import { getTheme } from "../../src/config/themes.js";
12
+ import type { VectorStats } from "../../src/vector-read.js";
13
+ import type { RepoStats } from "../../src/store/sqlite/stats.js";
14
+ import type { GameState } from "../../src/store/sqlite/game-state.js";
15
+ import type { ModelSnapshot } from "../../src/store/sqlite/model-snapshots.js";
16
+ import type { WidgetData, TickerEntry } from "./widget.js";
17
+
18
+ // ------------------------------------------------------------------ helpers
19
+
20
+ function dedupStr(storageRate: number): string {
21
+ // Storage dedup rate is cumulative (store-wide, per-repo) and survives
22
+ // session resets. Always show a number (decimal for sub-10%).
23
+ return storageRate * 100 >= 10
24
+ ? `${Math.round(storageRate * 100)}%`
25
+ : `${(storageRate * 100).toFixed(1)}%`;
26
+ }
27
+
28
+ function pctLabel(lastCtxPercent: number | null): string {
29
+ if (lastCtxPercent == null) return "?%";
30
+ if (lastCtxPercent > 100) return `>100%`; // S29: overshoot warning
31
+ return `${Math.round(lastCtxPercent * 10) / 10}%`;
32
+ }
33
+
34
+ function tokLabel(v: number | null): string {
35
+ return v != null ? `${Math.round(v / 1000)}k` : "?";
36
+ }
37
+
38
+ function maxLabel(v: number): string {
39
+ return v > 0 ? `${Math.round(v / 1000)}k` : "?";
40
+ }
41
+
42
+ function agentStr(activeAgents: number): string {
43
+ const agentLabel =
44
+ activeAgents > 0
45
+ ? `\u{1F916} ${activeAgents} agent${activeAgents === 1 ? "" : "s"}`
46
+ : `${C.dim}\u{1F916} idle${C.reset}`;
47
+ return ` │ ${agentLabel}`;
48
+ }
49
+
50
+ // ----------------------------------------------------------------- types
51
+
52
+ export interface SnapshotInput {
53
+ // Context-window counters
54
+ lastCtxTokens: number | null;
55
+ lastCtxWindow: number;
56
+ lastCtxPercent: number | null;
57
+ activeAgents: number;
58
+ currentTurn: number;
59
+ statusKey: string | undefined;
60
+
61
+ // Store stats (pre-computed outside this function)
62
+ st: VectorStats;
63
+ repo: RepoStats;
64
+
65
+ // Session counters
66
+ rtTokensSaved: number;
67
+ lastCompactAt: number | null;
68
+
69
+ // Widget display fields
70
+ ticker: TickerEntry[];
71
+ lastWhy: string | undefined;
72
+ tierTrace: string | undefined;
73
+ pulsing: boolean;
74
+
75
+ // Pressure / trigger state
76
+ pressureBand: "low" | "medium" | "high" | "ultra" | "mega";
77
+ configTier: string;
78
+ ready: boolean;
79
+ armed: boolean;
80
+
81
+ // Model snapshot (pre-fetched)
82
+ modelSnap: ModelSnapshot | undefined;
83
+
84
+ // Game-mode state (callers that need real-time values pass lambdas)
85
+ getCachedGameState: () => GameState;
86
+ getTurnLevel: () => number;
87
+ embedderName: () => string;
88
+ driftStatus: () => "ok" | "warn";
89
+
90
+ // Flare flags (one-shot per render cycle)
91
+ megaCacheFlare: boolean;
92
+ megaCacheFlarePct: number;
93
+ levelUpFlare: boolean;
94
+ achievementFlare: boolean;
95
+ achievementFlareTitles: string[];
96
+
97
+ // v0.8.3: ambient border effect
98
+ activeEffect: {
99
+ type: "pulse" | "flash";
100
+ role: "accent" | "mega" | "red";
101
+ startedAt: number;
102
+ durationMs: number;
103
+ } | null;
104
+
105
+ lastActivityAt: number;
106
+ }
107
+
108
+ export interface SnapshotResult {
109
+ widgetData: WidgetData;
110
+ curLevel: number;
111
+ }
112
+
113
+ // ---------------------------------------------------------- main computation
114
+
115
+ /**
116
+ * Pure computation of the live stats widget data.
117
+ *
118
+ * Takes a snapshot of the current MegaRuntime state and returns a fully
119
+ * populated `WidgetData` object plus the computed turn level. No side effects
120
+ * — callers are responsible for flare consumption, level-up checks, and render.
121
+ */
122
+ export function computeMegaSnapshot(p: SnapshotInput): SnapshotResult {
123
+ const st = p.st;
124
+ const repo = p.repo;
125
+ const liveBand = p.pressureBand;
126
+
127
+ // ── header strings ────────────────────────────────────────────────────
128
+ const tierLabel = `${C.bold}${liveBand}${C.reset}${C.gray}·${p.configTier}${C.reset}`;
129
+ const triggerLabel = p.ready
130
+ ? `${C.green}● ready${C.reset}`
131
+ : p.armed
132
+ ? `${C.amber}◐ armed${C.reset}`
133
+ : `${C.gray}○ idle${C.reset}`;
134
+ const pctStr = pctLabel(p.lastCtxPercent);
135
+ const tokStr = tokLabel(p.lastCtxTokens);
136
+ const maxStr = maxLabel(p.lastCtxWindow);
137
+ const dedupStr_ = dedupStr(st.storageDedupRate);
138
+ const agentStr_ = agentStr(p.activeAgents);
139
+ const turnStr = p.currentTurn > 0 ? ` │ turn ${p.currentTurn}` : "";
140
+
141
+ // ── reconciled in/out view (session + repo) ───────────────────────────
142
+ const sessIn = p.rtTokensSaved + st.totalTokenEstimate;
143
+ const sessKept = st.totalTokenEstimate;
144
+ const sessPct = sessIn > 0 ? p.rtTokensSaved / sessIn : 0;
145
+ const repoIn = repo.tokensSaved + repo.totalTokenEstimate;
146
+ const repoKept = repo.totalTokenEstimate;
147
+ const repoPct = repoIn > 0 ? repo.tokensSaved / repoIn : 0;
148
+ const sTxt = (sessPct * 100).toFixed(sessPct * 100 >= 10 ? 0 : 1);
149
+ const rTxt = (repoPct * 100).toFixed(repoPct * 100 >= 10 ? 0 : 1);
150
+ const ctxPct = p.lastCtxPercent != null ? p.lastCtxPercent / 100 : 0;
151
+
152
+ // ── model + provider (S26 capture) for the header ─────────────────────
153
+ const modelName =
154
+ p.modelSnap?.modelName ?? p.modelSnap?.modelId ?? "?";
155
+ const modelStr = p.modelSnap?.provider
156
+ ? `${modelName}·${p.modelSnap.provider}`
157
+ : modelName;
158
+
159
+ // ── since-last-compact (ms; null until first compaction this session) ──
160
+ const sinceCompact =
161
+ p.lastCompactAt != null ? Date.now() - p.lastCompactAt : null;
162
+
163
+ // ── memory store: embedder + compression ratio ────────────────────────
164
+ const embedderName_ = p.embedderName();
165
+ const compRatio =
166
+ st.originalTokens > 0 && st.totalTokenEstimate > 0
167
+ ? st.originalTokens / st.totalTokenEstimate
168
+ : st.originalTokens > 0
169
+ ? 1
170
+ : 0;
171
+ const compStr = compRatio >= 1 ? `${compRatio.toFixed(1)}x` : "—";
172
+
173
+ // ── cross-repo drift status ──────────────────────────────────────────
174
+ const driftStatus_: "ok" | "warn" = p.driftStatus();
175
+ const agentsActive = p.activeAgents > 0;
176
+
177
+ // ── S31: game-mode state ──────────────────────────────────────────────
178
+ const gs = p.getCachedGameState();
179
+ const curLevel = p.getTurnLevel();
180
+ const cachePct = st.dedupHitRate * 100;
181
+
182
+ const widgetData: WidgetData = {
183
+ version: ownVersion(),
184
+ tierLabel,
185
+ triggerLabel,
186
+ pctStr,
187
+ tokStr,
188
+ maxStr,
189
+ ctxPct,
190
+ chk: st.checkpointCount,
191
+ agentStr: agentStr_,
192
+ turnStr,
193
+ dedupStr: dedupStr_,
194
+ sessIn,
195
+ sessKept,
196
+ sTxt,
197
+ repoIn,
198
+ repoKept,
199
+ rTxt,
200
+ repoChk: repo.checkpointCount,
201
+ repoSess: repo.sessionCount,
202
+ modelStr,
203
+ sinceCompact,
204
+ embedderName: embedderName_,
205
+ compStr,
206
+ driftStatus: driftStatus_,
207
+ agentsActive,
208
+ fresh: Date.now() - p.lastActivityAt < 4000,
209
+ ticker: p.ticker,
210
+ lastWhy: p.lastWhy,
211
+ tierTrace: p.tierTrace,
212
+ pulsing: p.pulsing,
213
+ // S31 game-mode fields:
214
+ gameMode: gs.game_mode_on,
215
+ theme: getTheme(gs.theme) ? gs.theme : "transparent",
216
+ tuiMode: gs.tui_display_mode,
217
+ level: curLevel,
218
+ cachePct,
219
+ megaCacheFlare: p.megaCacheFlare,
220
+ megaCacheFlarePct: p.megaCacheFlarePct,
221
+ levelUpFlare: p.levelUpFlare,
222
+ achievementFlare: p.achievementFlare,
223
+ achievementFlareTitles: p.achievementFlareTitles,
224
+ // v0.8.3: ambient border effect — threaded live so the widget can
225
+ // compute the per-frame phase and render animated borders.
226
+ activeEffect: p.activeEffect,
227
+ };
228
+
229
+ return { widgetData, curLevel };
230
+ }