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
@@ -16,6 +16,7 @@ import { consolidateMemories } from "../../src/memory.js";
16
16
  import { C, MARKER_TYPE, } from "../mega-runtime.js";
17
17
  import { resolveRepoRoot, preserveRecentForPressure } from "../mega-config.js";
18
18
  import { runRaptor } from "../../src/dedup/raptor/index.js";
19
+ import { isRaptorTreeFresh } from "../../src/dedup/raptor/buildHistory.js";
19
20
  import { loadDedupConfig } from "../../src/config/dedup.js";
20
21
  import { upsertEmbedding as indexUpsertEmbedding } from "../../src/store/vectorIndex.js";
21
22
  import { runMemoryReview } from "./memory-review.js";
@@ -180,19 +181,29 @@ function doCompact(view, keepFrom, opts, sid, config, pi, ctx, runtime) {
180
181
  embedding: cp.embedding,
181
182
  }));
182
183
  if (leaves.length >= 2) {
183
- // S25: stamp the tree with the newest checkpoint epoch so the
184
- // freshness guard in raptorSearchHits can reject stale trees after a
185
- // later compaction adds newer checkpoints.
186
- const builtAt = all.length > 0 ? Math.max(...all.map((c) => c.timestamp)) : Date.now();
187
- runRaptor(leaves, {
188
- stateDir: runtime.currentStateDir,
189
- sessionId: sid,
190
- budgetMs: dd.RAPTOR_BUDGET_MS,
191
- clustersPerLevel: dd.RAPTOR_CLUSTERS_PER_LEVEL,
192
- consistencyThreshold: dd.RAPTOR_CONSISTENCY,
193
- logger: runtime.logger,
194
- builtAt: Number.isFinite(builtAt) ? builtAt : Date.now(),
195
- });
184
+ // S42D: skip the rebuild when the last build is fresh (within
185
+ // RAPTOR_FRESHNESS_HOURS) and the checkpoint count hasn't drifted by
186
+ // more than 20%. avoids re-clustering on every compaction when the
187
+ // tree is still representative. 0 disables (always rebuild).
188
+ if (dd.RAPTOR_FRESHNESS_HOURS > 0 &&
189
+ isRaptorTreeFresh(sid, runtime.currentStateDir, dd.RAPTOR_FRESHNESS_HOURS, all.length)) {
190
+ runtime.logger?.info("raptor_skip_fresh", { sessionId: sid });
191
+ }
192
+ else {
193
+ // S25: stamp the tree with the newest checkpoint epoch so the
194
+ // freshness guard in raptorSearchHits can reject stale trees after a
195
+ // later compaction adds newer checkpoints.
196
+ const builtAt = all.length > 0 ? Math.max(...all.map((c) => c.timestamp)) : Date.now();
197
+ runRaptor(leaves, {
198
+ stateDir: runtime.currentStateDir,
199
+ sessionId: sid,
200
+ budgetMs: dd.RAPTOR_BUDGET_MS,
201
+ clustersPerLevel: dd.RAPTOR_CLUSTERS_PER_LEVEL,
202
+ consistencyThreshold: dd.RAPTOR_CONSISTENCY,
203
+ logger: runtime.logger,
204
+ builtAt: Number.isFinite(builtAt) ? builtAt : Date.now(),
205
+ });
206
+ }
196
207
  }
197
208
  }
198
209
  catch {
@@ -8,7 +8,8 @@
8
8
  import { sessionEntryToContextMessages } from "@earendil-works/pi-coding-agent";
9
9
  import { recallAndInline, recallAndInlineAsync, formatRecallBlock } from "../../src/recall.js";
10
10
  import { normalizeSessionId } from "../../src/store.js";
11
- import { incRecallInjected, incCacheHitTokens } from "../../src/store/sqlite.js";
11
+ import { incRecallInjected, incCacheHitTokens, getIndexDir } from "../../src/store/sqlite.js";
12
+ import { ensureConversationId, recordTurn, recordTurnRecall } from "../../src/store/sqlite/turns.js";
12
13
  import { C, } from "../mega-runtime.js";
13
14
  /**
14
15
  * Unified recall (Layer 5). The ONE path that injects. Returns the recall
@@ -50,6 +51,27 @@ export function doRecall(runtime, config, ctx, query, source) {
50
51
  runtime.rt.cacheHitTokens += sumTokens;
51
52
  incRecallInjected(result.toInject.length, runtime.currentStateDir);
52
53
  incCacheHitTokens(sumTokens, runtime.currentStateDir);
54
+ // S43: record recall provenance — which checkpoints/summaries served this
55
+ // turn, their score + source path. Linked to the turn row written at
56
+ // turn_end via the conversation+turnIndex. Best-effort + non-fatal.
57
+ try {
58
+ const convId = ensureConversationId(sid, runtime.currentStateDir);
59
+ const turnId = recordTurn({
60
+ conversationId: convId,
61
+ sessionId: sid,
62
+ turnIndex: runtime.currentTurn,
63
+ startedAt: Date.now(),
64
+ }, runtime.currentStateDir);
65
+ recordTurnRecall(turnId, result.toInject.map((h) => ({
66
+ checkpointId: h.checkpoint.checkpointId,
67
+ score: h.score,
68
+ source: (h.raptorLevel !== undefined ? "raptor" : h.repoId ? "cross-repo" : "flat"),
69
+ raptorLevel: h.raptorLevel,
70
+ })), runtime.currentStateDir);
71
+ }
72
+ catch {
73
+ /* non-fatal: recall provenance never breaks the recall path */
74
+ }
53
75
  }
54
76
  return result;
55
77
  }
@@ -84,7 +106,14 @@ export async function doRecallAsync(runtime, config, ctx, query, source, opts =
84
106
  sessionId: sid, query, limit: config.autoInlineK, source, skipInjected: true,
85
107
  recallMaxTokens: config.recallMaxTokens, windowDedupe: config.windowDedupe,
86
108
  liveWindow, dedupSim: config.crossRepoCosine, crossRepo: true,
87
- globalIndexDir: process.env.MEGACOMPACT_INDEX_DIR,
109
+ // F2: resolve the machine-wide index dir via the shared resolver so the
110
+ // cross-repo injected-set dedup works even when MEGACOMPACT_INDEX_DIR is
111
+ // unset. The env var still wins when set (getIndexDir checks it first);
112
+ // the default (~/.mega-compact-index) is the same DB mega-commands and the
113
+ // dashboard read, so injection counts stay consistent. Without this, a
114
+ // bare `process.env` read returns undefined → cross-repo hits re-inject in
115
+ // every new session (the global injected-set is never consulted).
116
+ globalIndexDir: getIndexDir(),
88
117
  }, runtime.store);
89
118
  runtime.dashboard.event("recall-crossrepo", {
90
119
  source, query: query.slice(0, 120), injected: x.toInject.length,
@@ -0,0 +1,24 @@
1
+ /**
2
+ * append-event.ts — extracted `MegaRuntime.appendEvent()`: the structured
3
+ * events.log diagnostics sink. Same context-interface + free-function +
4
+ * thin-delegate pattern as runtime-helpers.ts / effects.ts / game-state.ts.
5
+ */
6
+ import { join } from "node:path";
7
+ import { appendFileSync, mkdirSync } from "node:fs";
8
+ // ---------------------------------------------------------------- appendEvent
9
+ /**
10
+ * Append a structured line to the repo's events.log — the always-on
11
+ * diagnostics sink the dashboard live-streams. Unlike the runtime logger
12
+ * (gated by config.debug), this fires in production, so capture failures
13
+ * surface during a real capture even with debugging off. Best-effort +
14
+ * non-fatal.
15
+ */
16
+ export function appendEventImpl(self, event, fields) {
17
+ try {
18
+ mkdirSync(self.currentStateDir, { recursive: true });
19
+ appendFileSync(join(self.currentStateDir, "events.log"), JSON.stringify({ ts: Date.now(), event, ...fields }) + "\n");
20
+ }
21
+ catch {
22
+ /* non-fatal */
23
+ }
24
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * bind-repo.ts — extracted per-repo binding for MegaRuntime.
3
+ *
4
+ * Point store/dashboard/logger at the current repo's state dir. Rebuilds the
5
+ * instances only when the repo root changes, so cross-repo dedup stats, db,
6
+ * and events are fully isolated. Falls back to the global default outside git.
7
+ */
8
+ import { join } from "node:path";
9
+ import { VectorStore, vectorRepoStats, vectorDataInvariant } from "../../src/vectorStore.js";
10
+ import { repoStateDir, resolveRepoRoot } from "../mega-config.js";
11
+ import { upsertRepoRegistry } from "../../src/store/sqlite.js";
12
+ import { Logger } from "../../src/log.js";
13
+ import { Dashboard } from "../mega-dashboard.js";
14
+ // ------------------------------------------------------------------ bindRepo
15
+ export function bindRepoImpl(ctx, cwd) {
16
+ const dir = cwd
17
+ ? repoStateDir(cwd, ctx.config.stateDir)
18
+ : ctx.config.stateDir;
19
+ const key = cwd ? (resolveRepoRoot(cwd) ?? dir) : dir;
20
+ if (key === ctx.activeRepoRoot)
21
+ return dir;
22
+ ctx.activeRepoRoot = key;
23
+ ctx.currentStateDir = dir;
24
+ // S31 audit P2: bindRepo switched currentStateDir but left cachedGameState
25
+ // memoized -> the widget kept showing the previous repo's theme/mode/toggle
26
+ // until /mega-game or a restart. The game_state row is per-repo (per
27
+ // stateDir), so evict the memo on every repo switch; the next widget render
28
+ // re-queries lazily via getCachedGameState().
29
+ ctx.cachedGameState = undefined;
30
+ ctx.gameStateBump++;
31
+ // S32: re-target the fs.watch cache-eviction watcher at the NEW stateDir's
32
+ // sqlite.db so cross-process writes (dashboard server) still evict the memo.
33
+ ctx.ensureGameStateWatcher();
34
+ ctx.store = new VectorStore({
35
+ dedupSim: ctx.config.dedupSim,
36
+ stateDir: dir,
37
+ });
38
+ ctx.logger = new Logger({
39
+ enabled: ctx.config.debug,
40
+ path: join(dir, "mega-compact.log"),
41
+ });
42
+ ctx.dashboard = new Dashboard(dir);
43
+ // Aggregate this repo into the machine-wide index so the multi-repo
44
+ // dashboard (Summary / All-repos tabs) can show it alongside every other
45
+ // repo. Best-effort + non-fatal: a read-only index dir or contention must
46
+ // never break the per-repo compaction path. Runs only on repo-switch
47
+ // (this branch), so it's infrequent — not per-context-event.
48
+ try {
49
+ const repo = vectorRepoStats(ctx.store);
50
+ const di = vectorDataInvariant(ctx.store);
51
+ const root = key !== dir ? key : (resolveRepoRoot(cwd ?? dir) ?? dir);
52
+ upsertRepoRegistry({
53
+ repoRoot: root,
54
+ displayName: root.split(/[\\/]/).filter(Boolean).pop() ?? root,
55
+ stateDir: dir,
56
+ checkpointCount: repo.checkpointCount,
57
+ tokensSaved: repo.tokensSaved,
58
+ compressedOriginalBytes: di.compressedOriginalBytes,
59
+ });
60
+ }
61
+ catch {
62
+ /* non-fatal: index aggregation must not block compaction */
63
+ }
64
+ return dir;
65
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * capture-model.ts — extracted model-capture logic for MegaRuntime.
3
+ *
4
+ * Captures the active model/provider from ctx.model and persists it so cost
5
+ * estimation + the dashboard can read real pricing. Cheap + idempotent-ish:
6
+ * only writes a new row when the model id changes (models change rarely).
7
+ */
8
+ import { resolveRepoRoot } from "../mega-config.js";
9
+ import { recordModelSnapshot, recordRepoModel } from "../../src/store/sqlite.js";
10
+ // -------------------------------------------------------------- captureModel
11
+ export function captureModelImpl(ctx, ectx) {
12
+ const m = ectx.model;
13
+ if (!m) {
14
+ ctx.appendEvent("captureModel:no-model", { cwd: ectx.cwd });
15
+ return;
16
+ }
17
+ if (ctx.currentModel &&
18
+ ctx.currentModel.modelId === m.id &&
19
+ ctx.currentModel.provider === m.provider)
20
+ return;
21
+ let providerName = null;
22
+ try {
23
+ providerName =
24
+ ectx.modelRegistry?.getProviderDisplayName(m.provider) ?? null;
25
+ }
26
+ catch {
27
+ /* optional */
28
+ }
29
+ const snap = {
30
+ provider: m.provider,
31
+ providerName,
32
+ modelId: m.id,
33
+ modelName: m.name ?? null,
34
+ inputRate: m.cost?.input ?? 0,
35
+ outputRate: m.cost?.output ?? 0,
36
+ contextWindow: m.contextWindow ?? 0,
37
+ maxTokens: m.maxTokens ?? 0,
38
+ reasoning: !!m.reasoning,
39
+ };
40
+ ctx.currentModel = { ...snap, capturedAt: Date.now() };
41
+ ctx.diagCaptureModelCalls++;
42
+ const repo = resolveRepoRoot(ectx.cwd) ?? ctx.currentStateDir;
43
+ // S26: previously a single silent `catch {}` hid every capture failure, so
44
+ // model_snapshots stayed empty and the cost card read $0.00 with zero signal.
45
+ // Split per-write + append to events.log (always-on, dashboard live-streams
46
+ // it) + bump a DIAG counter so a live capture surfaces the root cause.
47
+ try {
48
+ recordModelSnapshot(repo, snap, ctx.currentStateDir);
49
+ ctx.appendEvent("captureModel:recorded", {
50
+ repo,
51
+ modelId: snap.modelId,
52
+ provider: snap.provider,
53
+ inputRate: snap.inputRate,
54
+ outputRate: snap.outputRate,
55
+ });
56
+ }
57
+ catch (e) {
58
+ ctx.diagCaptureModelFails++;
59
+ ctx.appendEvent("captureModel:record-failed", {
60
+ repo,
61
+ modelId: snap.modelId,
62
+ error: e instanceof Error ? e.message : String(e),
63
+ stack: e instanceof Error ? e.stack : undefined,
64
+ });
65
+ }
66
+ try {
67
+ // Denormalize the active model into the machine-wide index so the
68
+ // All-repos dashboard table can show provider/model per repo without
69
+ // opening every repo's DB. Best-effort + non-fatal.
70
+ recordRepoModel(repo, {
71
+ provider: snap.provider,
72
+ providerName: snap.providerName,
73
+ modelName: snap.modelName,
74
+ inputRate: snap.inputRate,
75
+ outputRate: snap.outputRate,
76
+ stateDir: ctx.currentStateDir,
77
+ displayName: repo.split(/[\\/]/).filter(Boolean).pop() ?? repo,
78
+ });
79
+ }
80
+ catch (e) {
81
+ ctx.appendEvent("captureModel:index-record-failed", {
82
+ repo,
83
+ modelId: snap.modelId,
84
+ error: e instanceof Error ? e.message : String(e),
85
+ });
86
+ }
87
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * dashboard-snapshot.ts — extracted DashboardSnapshot builder for MegaRuntime.
3
+ *
4
+ * Builds the ~130-line DashboardSnapshot data object that was previously
5
+ * inlined inside MegaRuntime.snapshot(). Pure data-gathering — no I/O, no
6
+ * side effects.
7
+ */
8
+ // ---------------------------------------------------------- buildDashboardSnapshot
9
+ /** Build the DashboardSnapshot object from precomputed store/live metrics.
10
+ * Pure — no I/O, no side effects. */
11
+ export function buildDashboardSnapshot(ctx) {
12
+ const armed = (ctx.lastCtxTokens ?? 0) >= ctx.effectiveThreshold * ctx.config.fastGatePct;
13
+ const ready = armed && (ctx.lastCtxTokens ?? 0) >= ctx.effectiveThreshold;
14
+ return {
15
+ version: 1,
16
+ updatedAt: new Date().toISOString(),
17
+ tier: ctx.pressureBand,
18
+ presetTier: ctx.config.tier,
19
+ pressure: ctx.pressure,
20
+ config: {
21
+ fastGatePct: ctx.config.fastGatePct,
22
+ thresholdTokens: ctx.effectiveThreshold,
23
+ tierPct: ctx.config.tierPct,
24
+ effectiveThresholdPct: ctx.config.tierPct != null ? ctx.config.tierPct * 100 : null,
25
+ anchorUserMessages: ctx.config.anchorUserMessages,
26
+ preserveRecent: ctx.config.preserveRecent,
27
+ auto: ctx.config.auto,
28
+ autoInline: ctx.config.autoInline,
29
+ },
30
+ session: {
31
+ id: ctx.rt.sessionId,
32
+ state: ctx.statusKey ?? "idle",
33
+ persistedThisSession: ctx.rt.persistedThisSession,
34
+ lastCheckpointId: ctx.rt.lastCheckpointId ?? null,
35
+ lastCompactedFrom: ctx.rt.lastCompactedFrom,
36
+ lastCompactedTokens: ctx.rt.lastCompactedTokens,
37
+ dedupSkips: ctx.rt.dedupSkips,
38
+ dedupAttempts: ctx.rt.dedupAttempts,
39
+ },
40
+ context: {
41
+ tokens: ctx.lastCtxTokens,
42
+ percent: ctx.lastCtxPercent,
43
+ contextWindow: ctx.lastCtxWindow,
44
+ },
45
+ trigger: {
46
+ armed,
47
+ ready,
48
+ currentTokens: ctx.lastCtxTokens,
49
+ thresholdTokens: ctx.effectiveThreshold,
50
+ fastGatePct: ctx.config.fastGatePct,
51
+ tierPct: ctx.config.tierPct,
52
+ effectiveThresholdPct: ctx.config.tierPct != null ? ctx.config.tierPct * 100 : null,
53
+ },
54
+ store: ctx.st,
55
+ crew: {
56
+ activeAgents: ctx.activeAgents,
57
+ currentTurn: ctx.currentTurn,
58
+ },
59
+ repo: ctx.repo,
60
+ compression: {
61
+ session: {
62
+ tokensIn: ctx.rt.tokensSaved + (ctx.st.totalTokenEstimate - ctx.st.originalTokens),
63
+ tokensOut: ctx.st.totalTokenEstimate,
64
+ tokensFreed: ctx.rt.tokensSaved,
65
+ compressionPct: ctx.rt.tokensSaved / Math.max(1, ctx.rt.tokensSaved + (ctx.st.totalTokenEstimate - ctx.st.originalTokens)),
66
+ dedupPct: ctx.rt.dedupAttempts > 0 ? ctx.rt.dedupSkips / ctx.rt.dedupAttempts : 0,
67
+ },
68
+ repo: {
69
+ tokensIn: ctx.repo.tokensSaved + (ctx.repo.totalTokenEstimate - ctx.repo.originalTokens),
70
+ tokensOut: ctx.repo.totalTokenEstimate,
71
+ tokensFreed: ctx.repo.tokensSaved,
72
+ compressionPct: ctx.repo.tokensSaved / Math.max(1, ctx.repo.tokensSaved + (ctx.repo.totalTokenEstimate - ctx.repo.originalTokens)),
73
+ dedupPct: ctx.repo.dedupAttempts > 0 ? ctx.repo.dedupCollapsed / ctx.repo.dedupAttempts : 0,
74
+ },
75
+ },
76
+ integrity: ctx.di,
77
+ cacheHits: {
78
+ session: ctx.rt.dedupSkips + ctx.rt.recallInjections,
79
+ total: ctx.st.dedupCollapsed + ctx.st.injectedCount,
80
+ sessionTokensSaved: ctx.rt.cacheHitTokens,
81
+ totalTokensSaved: ctx.st.dedupCollapsed > 0 ? ctx.st.dedupCollapsed * 100 : 0,
82
+ },
83
+ compacts: {
84
+ session: ctx.rt.compactCount,
85
+ total: ctx.st.checkpointCount,
86
+ },
87
+ timeSaved: {
88
+ compact: {
89
+ sessionSec: ctx.rt.tokensSaved / 1000,
90
+ totalSec: ctx.repo.tokensSaved / 1000,
91
+ },
92
+ cacheHit: {
93
+ sessionSec: ctx.rt.cacheHitTokens / 1000,
94
+ totalSec: (ctx.st.dedupCollapsed * 100) / 1000,
95
+ },
96
+ },
97
+ model: ctx.currentModel
98
+ ? {
99
+ name: ctx.currentModel.modelId,
100
+ provider: ctx.currentModel.provider,
101
+ providerName: ctx.currentModel.providerName ?? ctx.currentModel.provider,
102
+ inputRate: ctx.currentModel.inputRate,
103
+ outputRate: ctx.currentModel.outputRate,
104
+ }
105
+ : undefined,
106
+ diag: {
107
+ ctxFastGate: ctx.diagCtxFastGate,
108
+ liveTrimFires: ctx.diagLiveTrimFires,
109
+ liveTrimReplays: ctx.diagLiveTrimReplays,
110
+ },
111
+ retries: {
112
+ errorRetryCount: ctx.errorRetryCount,
113
+ consecutiveErrors: ctx.consecutiveErrors,
114
+ maxConsecutiveErrors: ctx.ERROR_RETRY_MAX_CONSECUTIVE,
115
+ errorRetryHardStop: ctx.errorRetryHardStop,
116
+ // R7 (retry redesign): additive session-cap + poisoned-context counters.
117
+ sessionRetryCount: ctx.sessionRetryCount,
118
+ sessionMax: ctx.sessionRetryMax,
119
+ poisonedCount: ctx.poisonedCount,
120
+ },
121
+ };
122
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * effects.ts — extracted effect/flare/ticker logic for MegaRuntime.
3
+ *
4
+ * Pure implementations of ambient effects, flare arming, tier-trace callback,
5
+ * and the recall/activity ticker. Each function takes a typed context object so
6
+ * the class methods in state.ts become thin one-line delegates.
7
+ */
8
+ import { C } from "./widget.js";
9
+ // --------------------------------------------------------------- setEffect
10
+ /** v0.8.3: arm an ambient border effect (animated pulse/flash on the panel
11
+ * borders). Replaces any in-flight effect (last call wins). The widget reads
12
+ * activeEffect each frame and computes the per-frame phase from startedAt vs
13
+ * Date.now(); it renders '' once the window elapses. */
14
+ export function setEffectImpl(ctx, type, role, durationMs) {
15
+ ctx.activeEffect = { type, role, startedAt: Date.now(), durationMs };
16
+ }
17
+ // -------------------------------------------------------- armMegaCacheFlare
18
+ /** S33: arm the transient MEGA CACHE flare so the next snapshot() copies it
19
+ * into widgetData and the widget renders the oopsie gag for one cycle.
20
+ * v0.8.3: also arm a 'flash' ambient effect on the panel borders (mega
21
+ * color) for 1.2s. */
22
+ export function armMegaCacheFlareImpl(ctx, peakPct) {
23
+ ctx.megaCacheFlare = true;
24
+ ctx.megaCacheFlarePct = peakPct;
25
+ setEffectImpl(ctx, "flash", "mega", 1200);
26
+ }
27
+ // ------------------------------------------------------ armAchievementFlare
28
+ /** S35: arm the transient achievement-unlock flare with the newly-unlocked
29
+ * titles so the next snapshot() copies them into widgetData and the widget
30
+ * renders the one-time unlock toast for one render cycle.
31
+ * v0.8.3: also arm a 'pulse' ambient effect on the panel borders (accent
32
+ * color) for 2s to celebrate the unlock. */
33
+ export function armAchievementFlareImpl(ctx, titles) {
34
+ ctx.achievementFlare = true;
35
+ ctx.achievementFlareTitles = titles;
36
+ setEffectImpl(ctx, "pulse", "accent", 2000);
37
+ }
38
+ // -------------------------------------------------------- makeTierCallback
39
+ /** Build the sync onTier callback that paints the live per-tier trace. */
40
+ export function makeTierCallbackImpl(ctx, ectx) {
41
+ const order = ["L0", "L1", "L2", "new"];
42
+ const seen = new Map();
43
+ const glyph = (status) => status === "deduped"
44
+ ? `${C.green}✓${C.reset}`
45
+ : status === "passed"
46
+ ? `${C.dim}○${C.reset}`
47
+ : status === "scanning"
48
+ ? `${C.amber}…${C.reset}`
49
+ : `${C.cyan}●${C.reset}`;
50
+ return (ev) => {
51
+ const label = ev.tier === "new"
52
+ ? `${C.cyan}stored${C.reset}`
53
+ : `${ev.tier} ${glyph(ev.status)}` +
54
+ (ev.detail ? ` ${C.gray}(${ev.detail})${C.reset}` : "");
55
+ // Show the most recent outcome per tier (collapses re-fires).
56
+ seen.set(ev.tier, label);
57
+ const show = [];
58
+ for (const t of order)
59
+ if (seen.has(t))
60
+ show.push(seen.get(t));
61
+ ctx.tierTrace = `${C.teal}⚙${C.reset} ${show.join(` ${C.gray}→${C.reset} `)}`;
62
+ ctx.lastActivityAt = Date.now();
63
+ try {
64
+ ctx.snapshot(ectx);
65
+ }
66
+ catch {
67
+ /* non-fatal */
68
+ }
69
+ };
70
+ }
71
+ // -------------------------------------------------------------- pushTicker
72
+ /** Phase 3 — recall/activity ticker ring buffer.
73
+ * Dedupe consecutive identical entries — skip the append when the last
74
+ * entry's text matches, so a re-fired compact/recall/dedup event doesn't
75
+ * flood the ring (keeps it at TICKER_MAX for real variety). `at` is NOT
76
+ * refreshed on a skip (the original event time stands). */
77
+ export function pushTickerImpl(ctx, text) {
78
+ if (ctx.ticker[ctx.ticker.length - 1]?.text === text) {
79
+ ctx.lastActivityAt = Date.now();
80
+ return;
81
+ }
82
+ ctx.ticker.push({ text, at: Date.now() });
83
+ while (ctx.ticker.length > ctx.TICKER_MAX)
84
+ ctx.ticker.shift();
85
+ ctx.lastActivityAt = Date.now();
86
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * engine-view.ts — extracted `MegaRuntime.engineView()`: the pi→engine message
3
+ * adapter passthrough. A one-liner in its own module per the maximal-split
4
+ * convention.
5
+ */
6
+ import { toEngineMessages } from "../../src/adapt.js";
7
+ // ------------------------------------------------------------------ engineView
8
+ /** Convert the messages pi hands us in the `context` event into the engine view. */
9
+ export function engineViewImpl(messages) {
10
+ return toEngineMessages(messages);
11
+ }
@@ -0,0 +1,116 @@
1
+ /**
2
+ * game-state.ts — extracted game-state management for MegaRuntime.
3
+ *
4
+ * This module keeps the game-state logic cohesive while leaving the class fields
5
+ * and public method signatures in state.ts. The original methods become thin
6
+ * delegates so runtime behavior stays byte-for-byte identical.
7
+ */
8
+ import { watch } from "node:fs";
9
+ import { getGameState } from "../../src/store/sqlite.js";
10
+ import { getTheme } from "../../src/config/themes.js";
11
+ import { disposePerf } from "./perf.js";
12
+ export function getCachedGameStateImpl(self) {
13
+ if (!self.cachedGameState) {
14
+ try {
15
+ self.cachedGameState = getGameState(self.currentStateDir);
16
+ }
17
+ catch {
18
+ self.cachedGameState = {
19
+ game_mode_on: false,
20
+ theme: "transparent",
21
+ tui_display_mode: "full",
22
+ };
23
+ }
24
+ }
25
+ return self.cachedGameState;
26
+ }
27
+ export function refreshWidgetGameStateImpl(self, view, ctx) {
28
+ if (!self.widgetData || !ctx)
29
+ return;
30
+ const gs = getCachedGameStateImpl(self);
31
+ self.widgetData.gameMode = gs.game_mode_on;
32
+ self.widgetData.theme = getTheme(gs.theme) ? gs.theme : "transparent";
33
+ self.widgetData.tuiMode = gs.tui_display_mode;
34
+ view.renderWidget(ctx);
35
+ }
36
+ export function bumpGameStateImpl(self) {
37
+ self.cachedGameState = undefined;
38
+ self.gameStateBump++;
39
+ }
40
+ /** S32: release the fs.watch game-state watcher AND stop the v0.8.8 perf
41
+ * sampling interval. Called when the runtime is torn down (no existing
42
+ * dispose path — the process exit reclaims the fd, but explicit close is
43
+ * correct for any in-process reload / test reuse). Extracted from
44
+ * MegaRuntime.dispose(); the class keeps a thin delegate. */
45
+ export function disposeRuntimeImpl(self) {
46
+ if (self.gameStateWatcher) {
47
+ try {
48
+ self.gameStateWatcher.close();
49
+ }
50
+ catch { /* non-fatal */ }
51
+ self.gameStateWatcher = undefined;
52
+ self.gameStateWatchDir = undefined;
53
+ }
54
+ disposePerf(self);
55
+ }
56
+ export function ensureGameStateWatcherImpl(self, view) {
57
+ if (self.gameStateWatcher && self.gameStateWatchDir === self.currentStateDir) {
58
+ return;
59
+ }
60
+ if (self.gameStateWatcher) {
61
+ try {
62
+ self.gameStateWatcher.close();
63
+ }
64
+ catch {
65
+ /* non-fatal */
66
+ }
67
+ self.gameStateWatcher = undefined;
68
+ self.gameStateWatchDir = undefined;
69
+ }
70
+ try {
71
+ // Watch the state DIR (not just sqlite.db) and filter by filename.
72
+ // Why: the store is WAL-mode (openStore sets PRAGMA journal_mode=WAL).
73
+ // Cross-process writes (dashboard server child) append to sqlite.db-wal
74
+ // and do NOT modify sqlite.db until a checkpoint — and a long-lived
75
+ // parent connection (VectorStore + dashboard readers) keeps the WAL
76
+ // uncheckpointed, so a watcher on sqlite.db alone never fires and
77
+ // cachedGameState stays stale (theme stuck after a dashboard edit).
78
+ // Watching the dir + matching sqlite.db* catches the main db, the -wal
79
+ // sidecar, and -shm, so the memo evicts on any cross-process write. The
80
+ // filter also excludes events.log / *.log noise in the same dir.
81
+ self.gameStateWatcher = watch(self.currentStateDir, (_eventType, filename) => {
82
+ if (typeof filename === "string" && filename.startsWith("sqlite.db")) {
83
+ self.cachedGameState = undefined;
84
+ self.gameStateBump++;
85
+ // P2: force a widget re-render so a dashboard-made theme/toggle/
86
+ // tui-mode change reflects in the live TUI immediately, even when
87
+ // pi is idle (no context event to drive snapshot()). Use the
88
+ // LIGHTWEIGHT refreshWidgetGameState() — NOT the full snapshot():
89
+ // snapshot() recomputes 6 sync SQLite opens + writes dashboard.json
90
+ // + writes to the store, and those store writes RETRIGGER this
91
+ // same fs.watch callback (it fires on every sqlite.db* write) →
92
+ // re-entrant thrash → 190s test timeout under mega-compact.test.js
93
+ // / mega-teamrun.test.js. The lightweight path re-reads ONLY the
94
+ // game_state row and patches the three game-mode fields on the
95
+ // existing widgetData, then re-registers the factory via
96
+ // renderWidget() — it writes nothing to the store or
97
+ // dashboard.json, so it cannot retrigger itself. Guard: skip until
98
+ // the first snapshot stashed a ctx (no widget registered yet →
99
+ // nothing to refresh). Non-fatal: next context event re-snapshots.
100
+ const ctx = self.lastWidgetCtx;
101
+ if (ctx) {
102
+ try {
103
+ refreshWidgetGameStateImpl(self, view, ctx);
104
+ }
105
+ catch {
106
+ /* non-fatal */
107
+ }
108
+ }
109
+ }
110
+ });
111
+ self.gameStateWatchDir = self.currentStateDir;
112
+ }
113
+ catch {
114
+ /* non-fatal: missing dir / platform issue — next snapshot re-queries */
115
+ }
116
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * get-state-dir.ts — extracted `MegaRuntime.getStateDir()` (S21). A one-liner
3
+ * in its own module per the maximal-split convention: no method bodies left in
4
+ * runtime.ts.
5
+ */
6
+ // --------------------------------------------------------------- getStateDir
7
+ /** S21: state dir of the currently bound repo (where memories live). */
8
+ export function getStateDirImpl(self) {
9
+ return self.currentStateDir;
10
+ }