pi-mega-compact 0.21.2 → 0.21.4

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 (141) hide show
  1. package/dist/extensions/dashboard-server/routes-rag-settings-helpers.js +1 -0
  2. package/dist/extensions/mega-compact-child.js +126 -0
  3. package/dist/extensions/mega-config.js +5 -0
  4. package/dist/extensions/mega-events/agent-handlers/turnEndHandler/recordTurnRow.js +12 -2
  5. package/dist/extensions/mega-events/context-handler/afterCompact.js +11 -0
  6. package/dist/extensions/mega-events/context-handler/dbMirrorAppend.js +8 -0
  7. package/dist/extensions/mega-events/health-handler.js +12 -2
  8. package/dist/extensions/mega-events/session-handlers.js +1 -0
  9. package/dist/extensions/mega-pipeline/compact/run.js +11 -0
  10. package/dist/extensions/mega-runtime/helpers.js +45 -0
  11. package/dist/extensions/mega-runtime/internal-errors.js +14 -0
  12. package/dist/extensions/mega-runtime/runtime-instrumentation.js +166 -0
  13. package/dist/extensions/mega-runtime/runtime.js +27 -193
  14. package/dist/extensions/mega-runtime/state.js +1 -1
  15. package/dist/extensions/mega-runtime/widget.js +1 -1
  16. package/dist/extensions/mega-turn-store.js +5 -0
  17. package/dist/src/bridge/factory.js +195 -0
  18. package/dist/src/bridge/types.js +10 -0
  19. package/dist/src/bridge.js +1 -0
  20. package/dist/src/contextHealth.js +13 -5
  21. package/dist/src/metrics/turns.js +6 -1
  22. package/dist/src/store/sqlite/context-health-cache-poison.js +60 -0
  23. package/dist/src/store/sqlite/context-health.js +6 -48
  24. package/dist/src/store/sqlite/embedding-cache.js +4 -0
  25. package/dist/src/store/sqlite/schema.js +4 -0
  26. package/dist/src/store/turns/memory-store/admin.js +119 -0
  27. package/dist/src/store/turns/memory-store/ctx.js +28 -0
  28. package/dist/src/store/turns/memory-store/read.js +110 -0
  29. package/dist/src/store/turns/memory-store/write.js +88 -0
  30. package/dist/src/store/turns/memory-store.js +44 -273
  31. package/dist/src/store/turns/migrations.js +4 -0
  32. package/dist/src/store/turns/schema.js +1 -0
  33. package/dist/src/store/turns/sqlite-store/read.js +13 -0
  34. package/dist/src/store/turns/sqlite-store/rows.js +4 -0
  35. package/dist/src/store/turns/sqlite-store/write.js +3 -3
  36. package/dist/src/store/turns/sqlite-store.js +4 -0
  37. package/extensions/dashboard-client/dist/assets/{AdminTab-BsA8-_bX.js → AdminTab-C4Y0_UVx.js} +2 -2
  38. package/extensions/dashboard-client/dist/assets/{AdminTab-BsA8-_bX.js.map → AdminTab-C4Y0_UVx.js.map} +1 -1
  39. package/extensions/dashboard-client/dist/assets/{AreaChart-BRHEfWhc.js → AreaChart-MCWK9rn6.js} +2 -2
  40. package/extensions/dashboard-client/dist/assets/{AreaChart-BRHEfWhc.js.map → AreaChart-MCWK9rn6.js.map} +1 -1
  41. package/extensions/dashboard-client/dist/assets/{BarChart-C-M0LaFc.js → BarChart-CvSGAiWV.js} +2 -2
  42. package/extensions/dashboard-client/dist/assets/{BarChart-C-M0LaFc.js.map → BarChart-CvSGAiWV.js.map} +1 -1
  43. package/extensions/dashboard-client/dist/assets/{CacheTab-Cj3n59U3.js → CacheTab-DnMEVRro.js} +2 -2
  44. package/extensions/dashboard-client/dist/assets/{CacheTab-Cj3n59U3.js.map → CacheTab-DnMEVRro.js.map} +1 -1
  45. package/extensions/dashboard-client/dist/assets/{ConfigTab-C016XTNC.js → ConfigTab-BExuZYy2.js} +2 -2
  46. package/extensions/dashboard-client/dist/assets/{ConfigTab-C016XTNC.js.map → ConfigTab-BExuZYy2.js.map} +1 -1
  47. package/extensions/dashboard-client/dist/assets/{EventsTab-DE29fuqe.js → EventsTab-CspsEslY.js} +2 -2
  48. package/extensions/dashboard-client/dist/assets/{EventsTab-DE29fuqe.js.map → EventsTab-CspsEslY.js.map} +1 -1
  49. package/extensions/dashboard-client/dist/assets/HealthTab-DiWzo2ML.js +2 -0
  50. package/extensions/dashboard-client/dist/assets/HealthTab-DiWzo2ML.js.map +1 -0
  51. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-4fJ7M4S4.js → MaintenanceTab-9NJuv3dB.js} +2 -2
  52. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-4fJ7M4S4.js.map → MaintenanceTab-9NJuv3dB.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-CDbcK9e4.js → MemoryMapTab-YpJK1h17.js} +2 -2
  54. package/extensions/dashboard-client/dist/assets/{MemoryMapTab-CDbcK9e4.js.map → MemoryMapTab-YpJK1h17.js.map} +1 -1
  55. package/extensions/dashboard-client/dist/assets/{MetricsCards-VUEcgGzo.js → MetricsCards-D2Mom1M7.js} +2 -2
  56. package/extensions/dashboard-client/dist/assets/{MetricsCards-VUEcgGzo.js.map → MetricsCards-D2Mom1M7.js.map} +1 -1
  57. package/extensions/dashboard-client/dist/assets/MetricsTab-DqWF7wud.js +2 -0
  58. package/extensions/dashboard-client/dist/assets/{MetricsTab-CKqJ0Zl-.js.map → MetricsTab-DqWF7wud.js.map} +1 -1
  59. package/extensions/dashboard-client/dist/assets/{OverviewTab-9Kxo79mi.js → OverviewTab-YuRxjV9U.js} +2 -2
  60. package/extensions/dashboard-client/dist/assets/{OverviewTab-9Kxo79mi.js.map → OverviewTab-YuRxjV9U.js.map} +1 -1
  61. package/extensions/dashboard-client/dist/assets/{ReposTab-C4iveqrP.js → ReposTab-sXMrR7je.js} +2 -2
  62. package/extensions/dashboard-client/dist/assets/{ReposTab-C4iveqrP.js.map → ReposTab-sXMrR7je.js.map} +1 -1
  63. package/extensions/dashboard-client/dist/assets/{SessionsTab-DSEwkMQm.js → SessionsTab-4YCXeGNR.js} +2 -2
  64. package/extensions/dashboard-client/dist/assets/{SessionsTab-DSEwkMQm.js.map → SessionsTab-4YCXeGNR.js.map} +1 -1
  65. package/extensions/dashboard-client/dist/assets/{SetupTab-CCyWgy4O.js → SetupTab-67pZcM_W.js} +2 -2
  66. package/extensions/dashboard-client/dist/assets/{SetupTab-CCyWgy4O.js.map → SetupTab-67pZcM_W.js.map} +1 -1
  67. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CyD8e_e_.js → TimeSavedCard-DInjNETP.js} +2 -2
  68. package/extensions/dashboard-client/dist/assets/{TimeSavedCard-CyD8e_e_.js.map → TimeSavedCard-DInjNETP.js.map} +1 -1
  69. package/extensions/dashboard-client/dist/assets/{TurnMemoryView-5tLbWkRv.js → TurnMemoryView-C3Bif8ix.js} +2 -2
  70. package/extensions/dashboard-client/dist/assets/{TurnMemoryView-5tLbWkRv.js.map → TurnMemoryView-C3Bif8ix.js.map} +1 -1
  71. package/extensions/dashboard-client/dist/assets/TurnsTab-BUGfiANp.js +2 -0
  72. package/extensions/dashboard-client/dist/assets/{TurnsTab-C7pm92Hg.js.map → TurnsTab-BUGfiANp.js.map} +1 -1
  73. package/extensions/dashboard-client/dist/assets/{VcStatusBadge-ObhcIyfN.js → VcStatusBadge-DKU7qz9f.js} +2 -2
  74. package/extensions/dashboard-client/dist/assets/{VcStatusBadge-ObhcIyfN.js.map → VcStatusBadge-DKU7qz9f.js.map} +1 -1
  75. package/extensions/dashboard-client/dist/assets/{VectorCortexTab-BtHZb7o7.js → VectorCortexTab-BsVSf4xm.js} +2 -2
  76. package/extensions/dashboard-client/dist/assets/{VectorCortexTab-BtHZb7o7.js.map → VectorCortexTab-BsVSf4xm.js.map} +1 -1
  77. package/extensions/dashboard-client/dist/assets/{WikiTab-lQ4l3QIK.js → WikiTab-DZw9_Fwd.js} +2 -2
  78. package/extensions/dashboard-client/dist/assets/{WikiTab-lQ4l3QIK.js.map → WikiTab-DZw9_Fwd.js.map} +1 -1
  79. package/extensions/dashboard-client/dist/assets/{button-DhQQAJGm.js → button-BLRPN489.js} +2 -2
  80. package/extensions/dashboard-client/dist/assets/{button-DhQQAJGm.js.map → button-BLRPN489.js.map} +1 -1
  81. package/extensions/dashboard-client/dist/assets/{card-Cfhz9X31.js → card-BGkkcM4Q.js} +2 -2
  82. package/extensions/dashboard-client/dist/assets/{card-Cfhz9X31.js.map → card-BGkkcM4Q.js.map} +1 -1
  83. package/extensions/dashboard-client/dist/assets/{client-extra-CKNOGEz_.js → client-extra-0OJClxHD.js} +2 -2
  84. package/extensions/dashboard-client/dist/assets/{client-extra-CKNOGEz_.js.map → client-extra-0OJClxHD.js.map} +1 -1
  85. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-JKjK9_cB.js → generateCategoricalChart-Dl5lmrHF.js} +2 -2
  86. package/extensions/dashboard-client/dist/assets/{generateCategoricalChart-JKjK9_cB.js.map → generateCategoricalChart-Dl5lmrHF.js.map} +1 -1
  87. package/extensions/dashboard-client/dist/assets/health-lSeDFTz7.js.map +1 -1
  88. package/extensions/dashboard-client/dist/assets/{index-iuxgtzEW.js → index-CH_AD-S_.js} +3 -3
  89. package/extensions/dashboard-client/dist/assets/{index-iuxgtzEW.js.map → index-CH_AD-S_.js.map} +1 -1
  90. package/extensions/dashboard-client/dist/assets/{switch-7EHEQMSP.js → switch-BPP2-d12.js} +2 -2
  91. package/extensions/dashboard-client/dist/assets/{switch-7EHEQMSP.js.map → switch-BPP2-d12.js.map} +1 -1
  92. package/extensions/dashboard-client/dist/assets/{toggle-z1lWY4UM.js → toggle-Dez0JaMV.js} +2 -2
  93. package/extensions/dashboard-client/dist/assets/{toggle-z1lWY4UM.js.map → toggle-Dez0JaMV.js.map} +1 -1
  94. package/extensions/dashboard-client/dist/assets/{useSSE-sctOZuRM.js → useSSE-Cx2W7BTo.js} +2 -2
  95. package/extensions/dashboard-client/dist/assets/{useSSE-sctOZuRM.js.map → useSSE-Cx2W7BTo.js.map} +1 -1
  96. package/extensions/dashboard-client/dist/index.html +1 -1
  97. package/extensions/dashboard-client/src/api/health.ts +1 -0
  98. package/extensions/dashboard-client/src/tabs/HealthTab.tsx +1 -0
  99. package/extensions/dashboard-server/routes-rag-settings-helpers.ts +6 -0
  100. package/extensions/mega-compact-child.ts +129 -0
  101. package/extensions/mega-config-types.ts +5 -0
  102. package/extensions/mega-config.ts +5 -0
  103. package/extensions/mega-events/agent-handlers/turnEndHandler/recordTurnRow.ts +12 -2
  104. package/extensions/mega-events/context-handler/afterCompact.ts +11 -0
  105. package/extensions/mega-events/context-handler/dbMirrorAppend.ts +8 -0
  106. package/extensions/mega-events/health-handler.ts +13 -3
  107. package/extensions/mega-events/session-handlers.ts +1 -0
  108. package/extensions/mega-pipeline/compact/run.ts +11 -0
  109. package/extensions/mega-runtime/helpers.ts +46 -0
  110. package/extensions/mega-runtime/internal-errors.ts +30 -0
  111. package/extensions/mega-runtime/runtime-instrumentation.ts +195 -0
  112. package/extensions/mega-runtime/runtime.ts +29 -209
  113. package/extensions/mega-runtime/state.ts +1 -1
  114. package/extensions/mega-runtime/widget.ts +1 -1
  115. package/extensions/mega-turn-store.ts +9 -0
  116. package/package.json +2 -2
  117. package/src/bridge/factory.ts +245 -0
  118. package/src/bridge/types.ts +140 -0
  119. package/src/bridge.ts +24 -0
  120. package/src/contextHealth.ts +26 -6
  121. package/src/metrics/turns.ts +6 -1
  122. package/src/store/sqlite/context-health-cache-poison.ts +97 -0
  123. package/src/store/sqlite/context-health.ts +12 -84
  124. package/src/store/sqlite/embedding-cache.ts +4 -0
  125. package/src/store/sqlite/schema.ts +4 -0
  126. package/src/store/turns/memory-store/admin.ts +171 -0
  127. package/src/store/turns/memory-store/ctx.ts +62 -0
  128. package/src/store/turns/memory-store/read.ts +172 -0
  129. package/src/store/turns/memory-store/write.ts +126 -0
  130. package/src/store/turns/memory-store.ts +51 -330
  131. package/src/store/turns/migrations.ts +4 -0
  132. package/src/store/turns/schema.ts +1 -0
  133. package/src/store/turns/sqlite-store/read.ts +19 -0
  134. package/src/store/turns/sqlite-store/rows.ts +5 -0
  135. package/src/store/turns/sqlite-store/write.ts +3 -2
  136. package/src/store/turns/sqlite-store.ts +5 -0
  137. package/src/store/turns/types.ts +15 -0
  138. package/extensions/dashboard-client/dist/assets/HealthTab-D4eVI8ar.js +0 -2
  139. package/extensions/dashboard-client/dist/assets/HealthTab-D4eVI8ar.js.map +0 -1
  140. package/extensions/dashboard-client/dist/assets/MetricsTab-CKqJ0Zl-.js +0 -2
  141. package/extensions/dashboard-client/dist/assets/TurnsTab-C7pm92Hg.js +0 -2
@@ -15,13 +15,12 @@
15
15
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
16
16
  import type { AgentMessage } from "@earendil-works/pi-agent-core";
17
17
  import { join } from "node:path";
18
- import type { FSWatcher } from "node:fs";
19
18
  import { VectorStore } from "../../src/vectorStore.js";
20
19
  import type { toEngineMessages } from "../../src/adapt.js";
21
- import { normalizeSessionId } from "../../src/store.js";
22
20
  import { Logger } from "../../src/log.js";
21
+
22
+ export { RING_MAX } from "./internal-errors.js";
23
23
  import type {
24
- ModelSnapshot,
25
24
  GameState,
26
25
  } from "../../src/store/sqlite.js";
27
26
  import type {
@@ -32,10 +31,7 @@ import { Dashboard } from "../mega-dashboard.js";
32
31
  import type {
33
32
  SessionRuntime,
34
33
  } from "./helpers.js";
35
- import type {
36
- TickerEntry,
37
- WidgetData,
38
- } from "./widget.js";
34
+ import { createSessionRuntime } from "./helpers.js";
39
35
  import {
40
36
  ensureGameStateWatcherImpl,
41
37
  getCachedGameStateImpl,
@@ -61,12 +57,14 @@ import {
61
57
  } from "./pressure-getters.js";
62
58
  import { resetRuntimeImpl } from "./reset-runtime.js";
63
59
  import { appendEventImpl } from "./append-event.js";
60
+ import { recordInternalErrorImpl } from "./internal-errors.js";
61
+ import { RuntimeInstrumentation } from "./runtime-instrumentation.js";
64
62
  import { getStateDirImpl } from "./get-state-dir.js";
65
63
  import { renderWidgetImpl } from "./render-widget.js";
66
64
  import { setStatusImpl } from "./status.js";
67
65
  import { engineViewImpl } from "./engine-view.js";
68
66
 
69
- export class MegaRuntime {
67
+ export class MegaRuntime extends RuntimeInstrumentation {
70
68
  config: MegaConfig;
71
69
  // Store/dashboard/logger are rebound per-repo by bindRepo() so each git repo
72
70
  // gets its own isolated state dir. They start bound to the global default.
@@ -77,208 +75,14 @@ export class MegaRuntime {
77
75
  currentStateDir: string;
78
76
 
79
77
  // The only mutable per-session state. Reset on session_start / session_tree.
80
- rt: SessionRuntime = {
81
- sessionId: normalizeSessionId(undefined),
82
- persistedThisSession: false,
83
- lastCheckpointId: undefined,
84
- lastCompactedFrom: 0,
85
- lastCompactedTokens: 0,
86
- dedupSkips: 0,
87
- dedupAttempts: 0,
88
- tokensSaved: 0,
89
- lastCompactAt: null,
90
- lastRecallAt: null,
91
- lastInjectAt: null,
92
- _prevCacheHitPct: null,
93
- _lastCacheHealthScore: undefined,
94
- lastNativeCompactAt: null,
95
- compactCount: 0,
96
- recallInjections: 0,
97
- cacheHitTokens: 0,
98
- lengthStopPending: false,
99
- errorRetryCount: 0,
100
- errorRetryUntil: 0,
101
- consecutiveErrors: 0,
102
- lastErrorRetryAt: 0,
103
- retryNudgePending: false,
104
- errorRetrySessionCount: 0,
105
- lastErrorText: undefined,
106
- errorTextRepeatCount: 0,
107
- poisonedAdviseSent: false,
108
- providerOutageAdvised: false,
109
- poisonedCompactSignatures: new Set(),
110
- // S53: tracks whether withRecallTail injected this turn (consumed at turn_end).
111
- recallInjectedThisTurn: false,
112
- poisonedCount: 0,
113
- extensionInitiatedTurn: false,
114
- };
115
- // v0.8.6 cache-stability: the cached live-trim view for the current
116
- // compaction epoch. Set after a fresh runCompact + computeLiveTrimCut, and
117
- // replayed verbatim on subsequent gated context events in the SAME epoch
118
- // (same checkpointId) so the provider KV-cache prefix stays stable instead
119
- // of being invalidated by a freshly regenerated summary + sentinel every
120
- // fire. Invalidated on session restart (resetRuntime) and on any native
121
- // durable compaction (session_compact) that truncates the transcript.
122
- trimCache: {
123
- checkpointId: string;
124
- cut: number;
125
- summaryAgentMsg: AgentMessage;
126
- ctxPct: number | null;
127
- ctxTokens: number | null;
128
- } | null = null;
129
- debounceUntil = 0;
130
- // S16: debounce for the agent_end resume nudge (avoid busy-loops).
131
- resumeNudgeUntil = 0;
132
- // Agent tracking for real-time widget updates
133
- activeAgents = 0;
134
- currentTurn = 0;
135
- // S33: transient MEGA CACHE flare flag (armed by the turn_end scoring hook
136
- // when cachePct > 100). Copied into widgetData.megaCacheFlare on the next
137
- // snapshot() so the widget renders the oopsie gag, then reset (one cycle).
138
- megaCacheFlare = false;
139
- /** v0.8.3: ambient effect state for animated panel borders keyed off
140
- * status transitions (level-up, mega-cache overshoot, achievement unlock,
141
- * compaction start). Threaded into widgetData as `activeEffect`; the widget
142
- * computes the per-frame phase from startedAt vs Date.now() (non-expired).
143
- * Null when idle/expired. */
144
- activeEffect: { type: "pulse" | "flash"; role: "accent" | "mega" | "red"; startedAt: number; durationMs: number } | null = null;
145
- megaCacheFlarePct = 0;
146
- levelUpFlare = false;
147
- lastLevel = 0;
148
- // S35: transient achievement-unlock flare (armed by the scoring hooks after
149
- // evaluateAndUnlockAchievements returns newly-unlocked titles). Copied into
150
- // widgetData.achievementFlare on the next snapshot() so the widget renders the
151
- // unlock toast, then reset (one cycle — mirrors megaCacheFlare/levelUpFlare).
152
- achievementFlare = false;
153
- achievementFlareTitles: string[] = [];
154
- // S33: last cumulative dedup-collapsed count seen by the session_compact
155
- // hook, so we only record the DELTA as the dedupe score (leaderboard sums).
156
- lastDedupCollapsed = 0;
157
- // Recall block produced by auto-inline (resume/branch) that the next
158
- // before_agent_start should prepend to the system prompt. Unset after use.
159
- pendingRecallBlock: string | undefined;
160
- // S21: memory recall block, parallel to pendingRecallBlock. Same one-shot
161
- // semantics; composed with the checkpoint block in before_agent_start.
162
- pendingMemoryRecallBlock: string | undefined;
163
- statusKey: string | undefined; // current status text for dashboard
164
- // Active model/provider (for real cost estimation). Captured from ctx.model
165
- // on model_select + session_start; persisted to SQL so cost + the dashboard
166
- // can read it without a live ctx.
167
- currentModel: ModelSnapshot | undefined;
168
- // Live "what it's doing right now" timestamp, used for the fresh-window.
169
- lastActivityAt = 0;
170
- // Live per-tier dedup trace (Phase 1): e.g. "L0 ✓ → L1 ✓ → L2 0.91 → stored".
171
- // Built from the store's sync onTier callback during a compaction so the user
172
- // watches each tier evaluate in real time. Cleared once the outcome settles.
173
- tierTrace: string | undefined;
174
- // Phase 3 — standout toolbar state.
175
- // Recall/activity ticker: a small ring buffer (≤5) of recent compact/recall
176
- // events so the widget shows a live history instead of a single last action.
177
- ticker: TickerEntry[] = [];
178
- readonly TICKER_MAX = 5;
179
- // Pulsing status: set true while a compaction is in flight, cleared on result.
180
- pulsing = false;
181
- // S21.2: set by `applyMemoryOps` when a memory add/replace/remove lands in
182
- // the current compaction. The pipeline reads this after a successful compact
183
- // to decide whether to fire `consolidateMemories` (skip the work entirely
184
- // when no memory rows changed).
185
- memoriesTouchedThisCompaction = 0;
186
- // Rolling "saved" goal for the progress bar — grows as we save more, so the
187
- // bar always has a meaningful denominator (never sits at 100% forever).
188
- savedGoal = 50_000;
189
- // Last explain-why line (dedup reason / anchor-kept / superseded), surfaced
190
- // while fresh.
191
- lastWhy: string | undefined = undefined;
192
- // v0.8.8 Perf dashboard instrumentation: turn/provider start timestamps +
193
- // the 5s cpu/mem interval handle (one per MegaRuntime, cleared in dispose()).
194
- perfTurnStart = 0;
195
- perfProviderStart = 0;
196
- perfCpuInterval: ReturnType<typeof setInterval> | null = null;
197
- perfCpuBaseline: { user: number; sys: number } | undefined;
198
-
199
- // Context tracking for the dashboard (updated in the context handler).
200
- lastCtxTokens: number | null = null;
201
- lastCtxPercent: number | null = null;
202
- lastCtxWindow = 0;
203
-
204
- // Latest computed widget payload (recomputed per snapshot, rendered per frame).
205
- widgetData: WidgetData | null = null;
206
- // v0.8.5: material-change signature from the last full snapshot() body. When
207
- // the next snapshot()'s signature matches, the expensive recompute (6 sync
208
- // SQLite opens) + writeFileSync(dashboard.json) are skipped — only the
209
- // (already-registered) widget factory is refreshed. Kills the per-event
210
- // main-thread block during typing/idle streaming with no material change.
211
- lastSnapshotSig: string | null = null;
212
- lastHeartbeatAt: number = 0;
213
- // v0.8.5: bumped whenever the cached game-state memo is evicted (bumpGameState
214
- // for in-process /mega-game writes, the fs.watch callback for cross-process
215
- // dashboard-server writes, and bindRepo on repo switch) so the snapshot gate
216
- // invalidates and the widget re-reads theme/mode after the change.
217
- gameStateBump = 0;
218
- // Cached cross-repo drift status (recomputed at most every 30s — it opens the
219
- // machine-wide registry DB, so we don't want to do it on every render frame).
220
- driftCache: { at: number; status: "ok" | "warn" } | null = null;
221
- // S31: cached game-mode state (game_mode_on/theme/tui_display_mode). Lazily
222
- // read from the game_state SQLite row on the first widget render, then
223
- // memoized until bumpGameState() evicts it (called by /mega-game after a
224
- // write) so the widget picks up theme/mode/level changes live without
225
- // re-querying the DB on every render frame.
226
- cachedGameState: GameState | undefined;
227
- // S32: fs.watch on the current repo's sqlite.db so cross-process writes
228
- // (e.g. the dashboard server's PUT /api/game-state, which runs as a detached
229
- // child with no MegaRuntime ref) evict the cached game-state memo. Without
230
- // this, /mega-game's in-process bumpGameState() is the only eviction trigger
231
- // and the widget would keep showing stale theme/mode/toggle after a dashboard
232
- // edit until a restart. The watcher tracks currentStateDir — closed + re-opened
233
- // by ensureGameStateWatcher() on every bindRepo repo switch. Non-fatal: any
234
- // fs.watch failure (missing file / platform issue) is swallowed; the next
235
- // getCachedGameState() snapshot re-queries the DB anyway.
236
- gameStateWatcher?: FSWatcher;
237
- gameStateWatchDir?: string;
238
- // P2: the last ExtensionContext handed to snapshot()/renderWidget(), stashed
239
- // so the fs.watch game-state callback can force a widget re-render without
240
- // a context event (cross-process dashboard edits while pi is idle). Cleared
241
- // implicitly on construction (undefined → watcher skips until first snap).
242
- lastWidgetCtx?: ExtensionContext;
243
-
244
- /**
245
- * DIAG counters for the "team run doesn't relieve context" investigation.
246
- * Plain integers, incremented at the three compaction decision points. They
247
- * let a headless test drive the real event handlers and assert the firing
248
- * cadence without scraping log files. Inert in production (the live-trim and
249
- * before-compact probes also emit logger.info, but these counters are always
250
- * updated and cost nothing).
251
- */
252
- diagLiveTrimFires = 0; // context handler returned a trimmed view
253
- diagLiveTrimReplays = 0; // v0.8.6: trim view returned via cached replay (skipped re-compact)
254
- diagBeforeCompactFires = 0; // session_before_compact handler entered
255
- diagBeforeCompactSupplied = 0; // session_before_compact supplied our trim
256
- diagAgentEndIdle = 0; // agent_end with activeAgents===0
257
- diagAgentEndDurable = 0; // agent_end fired ctx.compact() (mid-run durable trim)
258
- diagAgentEndDurableSkipRecent = 0; // agent_end skipped ctx.compact() — compaction in last 10s (race guard)
259
- // Per-skip-path counters for the team-run diagnosis.
260
- diagCtxFastGate = 0; // returned at token fast-gate (below threshold)
261
- diagCtxNoCompact = 0; // autoCompactCheck().shouldCompact === false
262
- diagCtxDebounce = 0; // debounceUntil not yet elapsed
263
- diagCtxRunSkipped = 0; // runCompact() returned skipped
264
- diagCtxCutNull = 0; // computeLiveTrimCut returned null (anchor/boundary)
265
- diagCtxThrown = 0; // live-trim try threw (caught)
266
-
267
- // Context health instrumentation (v0.12): rolling ring buffers for
268
- // drift detection + cache poison Layer 1 hash baseline.
269
- recentTurnEmbeddings: number[][] = [];
270
- recentErrorCategories: (string | null)[] = [];
271
- lastPrefixHash: string | null = null;
272
- lastErrorCategory: string | null = null;
78
+ // Initialized via createSessionRuntime() (helpers.ts delegate-shell split).
79
+ rt: SessionRuntime = createSessionRuntime();
273
80
 
274
- /**
275
- * S26 capture instrumentation: the "model_snapshots empty $0.00 cost card"
276
- * bug was invisible because captureModel swallowed the DB write in a silent
277
- * `catch {}`. These always-updated counters (zero cost) let a headless test or
278
- * a live capture tell whether captureModel ran and whether the snapshot landed.
279
- */
280
- diagCaptureModelCalls = 0; // captureModel entered with a populated ctx.model
281
- diagCaptureModelFails = 0; // recordModelSnapshot threw → model_snapshots stays empty
81
+ // All other field declarations (instrumentation counters, context-health
82
+ // rings, the Sprint-H internal-error ring, live display / cache-stability
83
+ // state) live on the RuntimeInstrumentation base class see
84
+ // runtime-instrumentation.ts. Field names are unchanged (this.diagXxx,
85
+ // this.trimCache, this.recentInternalErrors, etc).
282
86
 
283
87
  // ---- pressure accessors (bodies in pressure-getters.ts) -------------------
284
88
 
@@ -301,6 +105,7 @@ export class MegaRuntime {
301
105
  }
302
106
 
303
107
  constructor(config: MegaConfig) {
108
+ super();
304
109
  this.config = config;
305
110
  this.store = new VectorStore({
306
111
  dedupSim: config.dedupSim,
@@ -362,6 +167,21 @@ export class MegaRuntime {
362
167
  appendEventImpl(this, event, fields);
363
168
  }
364
169
 
170
+ /**
171
+ * Sprint H (Finding 3 / Option A): record an internal store/service-write
172
+ * failure category into the `recentInternalErrors` ring. Called AT each
173
+ * failure emit site (see the §2.3a audit in
174
+ * docs/specs/c2-resume-and-health-fixes.md) — never by a central log filter.
175
+ * Mirrors `recentErrorCategories` (cap = RING_MAX, shift when over).
176
+ *
177
+ * NOTE (process boundary): the ring is per-runtime / in-memory. A child
178
+ * subprocess's failures do NOT reach the parent's ring; the parent-side
179
+ * dashboard already aggregates events.log cross-process, so nothing is lost.
180
+ */
181
+ recordInternalError(category: string): void {
182
+ recordInternalErrorImpl(this, category);
183
+ }
184
+
365
185
  /** S21: state dir of the currently bound repo (where memories live) — thin
366
186
  * delegate to `getStateDirImpl` (get-state-dir.ts). */
367
187
  getStateDir(): string {
@@ -5,4 +5,4 @@
5
5
  * every existing `import { MegaRuntime } from "./state.js"` continues to
6
6
  * resolve without changes.
7
7
  */
8
- export { MegaRuntime } from "./runtime.js";
8
+ export { MegaRuntime, RING_MAX } from "./runtime.js";
@@ -129,7 +129,7 @@ export function buildWidgetLines(
129
129
  `${C.gray}all-time${C.reset} ${fmtTokens(wd.repoIn)}→${fmtTokens(wd.repoKept)} kept ${C.blue}(${wd.rTxt}% freed)${C.reset}`,
130
130
  `${wd.repoChk} chk/${wd.repoSess} sess`,
131
131
  `${C.gray}mem${C.reset} ${wd.embedderName} · ${wd.chk} chunks · ${C.blue}comp ${wd.compStr}${C.reset}`,
132
- `${C.gray}drift${C.reset} ${wd.driftStatus === "ok" ? C.green : C.amber}${wd.driftStatus}${C.reset}`,
132
+ `${C.gray}comp lag${C.reset} ${wd.driftStatus === "ok" ? C.green : C.amber}${wd.driftStatus}${C.reset}`,
133
133
  `${C.gray}compact${C.reset} ${sinceCompactStr(wd.sinceCompact)}`,
134
134
  ...(wd.perTurnCacheHitPct != null
135
135
  ? [
@@ -12,6 +12,7 @@
12
12
  import {
13
13
  createTurnStore,
14
14
  type TurnStore,
15
+ type TurnReader,
15
16
  type TurnEntry,
16
17
  type TurnRecallEntry,
17
18
  type TurnHydeTelemetry,
@@ -40,6 +41,11 @@ function storeFor(stateDir: string): TurnStore {
40
41
  return s;
41
42
  }
42
43
 
44
+ /** Read-only view of the turn store for a state dir (e.g. nextTurnIndexFor). */
45
+ export function turnReaderFor(stateDir: string): TurnReader {
46
+ return storeFor(stateDir).asReader();
47
+ }
48
+
43
49
  /** Map a legacy RecallSource onto the contract `TurnRecallEntry.source` enum. */
44
50
  function mapSource(s: string): TurnRecallEntry["source"] {
45
51
  switch (s) {
@@ -77,6 +83,8 @@ export function recordTurnWrite(
77
83
  conversationId: string;
78
84
  sessionId: string;
79
85
  turnIndex: number;
86
+ /** S49R: pi's per-session counter, carried for the raw_transcript join. */
87
+ sessionTurnIndex?: number;
80
88
  role: string;
81
89
  startedAt?: number;
82
90
  endedAt?: number;
@@ -100,6 +108,7 @@ export function recordTurnWrite(
100
108
  conversationId: input.conversationId,
101
109
  sessionId: input.sessionId,
102
110
  turnIndex: input.turnIndex,
111
+ sessionTurnIndex: input.sessionTurnIndex,
103
112
  role: input.role as TurnEntry["role"],
104
113
  endedAt: input.endedAt ?? input.startedAt ?? Date.now(),
105
114
  ctxTokens: input.ctxTokens,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.21.2",
3
+ "version": "0.21.4",
4
4
  "description": "Layered, local, vector-backed context compressor for pi — supersede/collapse/cluster compaction with deduped inline recall.",
5
5
  "type": "module",
6
6
  "license": "BSD-3-Clause",
@@ -51,7 +51,7 @@
51
51
  "postbuild": "node scripts/vector-cortex-publish-acceptance.mjs",
52
52
  "build:dashboard": "cd extensions/dashboard-client && npx vite build",
53
53
  "test:dashboard-smoke": "node scripts/dashboard-tab-smoke.mjs",
54
- "lint": "tsc --noEmit && node scripts/guardrails-scan.mjs && node scripts/semantic-scan.mjs && node scripts/stub-scan.mjs --fail-on-unregistered && node scripts/mock-scan.mjs --fail-on-unregistered",
54
+ "lint": "tsc --noEmit && node scripts/guardrails-scan.mjs && node scripts/semantic-scan.mjs && node scripts/stub-scan.mjs --fail-on-unregistered && node scripts/mock-scan.mjs --fail-on-unregistered && node scripts/internal-err-scan.mjs --fail-on-unregistered",
55
55
  "test": "npm run build && node scripts/run-tests.mjs",
56
56
  "guardrails": "python3 scripts/regression_check.py --all || node scripts/guardrails-scan.mjs || node scripts/semantic-scan.mjs",
57
57
  "precommit": "bash .claude/hooks/pre-commit.sh",
@@ -0,0 +1,245 @@
1
+ /**
2
+ * bridge/factory.ts — `createMegaBridge(opts)` implementation.
3
+ *
4
+ * A thin, pi-agnostic wrapper over the engine's compaction / recall / memory /
5
+ * fork / vector APIs. Stores are constructed lazily on first use (a consumer
6
+ * that only calls recallMemories pays no VectorStore cost). Exceptions
7
+ * propagate from every method except `fork` (catches ForkError by design) and
8
+ * `close` (swallows best-effort cleanup), so failures surface in tests.
9
+ */
10
+ import { compactSession } from "../engine.js";
11
+ import {
12
+ recallAndInline,
13
+ recallAndInlineAsync,
14
+ recallMemoriesAndInline,
15
+ } from "../recall.js";
16
+ import type {
17
+ RecallInjectOptions,
18
+ RecallInjectResult,
19
+ MemoryRecallInjectOptions,
20
+ } from "../recall/types.js";
21
+ import { forkFromConversation, ForkError } from "../fork.js";
22
+ import { createTurnStore } from "../store/turns/index.js";
23
+ import type { TurnStore, TurnEntry } from "../store/turns/types.js";
24
+ import { addMemory } from "../store/sqlite/memories.js";
25
+ import { VectorStore, vectorSearch } from "../vectorStore.js";
26
+ import type { SearchHit } from "../vectorStore.js";
27
+ import { repoKey } from "../store/repoKey.js";
28
+ import type {
29
+ BridgeOptions,
30
+ BridgeCompactInput,
31
+ BridgeCompactResult,
32
+ BridgeRecallOptions,
33
+ BridgeRecallResult,
34
+ BridgeMemoryRecallOptions,
35
+ BridgeMemoryRecallResult,
36
+ BridgeForkOptions,
37
+ BridgeForkResult,
38
+ BridgeCortexOptions,
39
+ BridgeCortexResult,
40
+ BridgeAddMemoryInput,
41
+ BridgeRecordTurnInput,
42
+ MegaBridge,
43
+ } from "./types.js";
44
+
45
+ /** Map a RecallInjectResult to the bridge's slimmer result contract. */
46
+ function mapRecallResult(r: RecallInjectResult): BridgeRecallResult {
47
+ return {
48
+ block: r.block,
49
+ report: r.report,
50
+ hitCount: r.toInject.length,
51
+ empty: r.empty,
52
+ };
53
+ }
54
+
55
+ /** Map the memoryRecallAndInline tuple result to the bridge contract. */
56
+ function mapMemoryResult(
57
+ r: { empty: boolean; block: string; report: string[] },
58
+ ): BridgeMemoryRecallResult {
59
+ return {
60
+ block: r.block,
61
+ report: r.report,
62
+ hitCount: r.report.length,
63
+ empty: r.empty,
64
+ };
65
+ }
66
+
67
+ /** Map vectorSearch hits to the cortex result contract. */
68
+ function mapCortexHits(hits: SearchHit[], limit: number): BridgeCortexResult {
69
+ const top = hits.slice(0, limit);
70
+ return {
71
+ results: top.map((h) => ({
72
+ checkpointId: h.checkpoint.checkpointId,
73
+ score: h.score,
74
+ summary: h.checkpoint.summary,
75
+ })),
76
+ hitCount: top.length,
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Create a MegaBridge over a single stateDir.
82
+ *
83
+ * The VectorStore and TurnStore are lazy: constructed on first use and cached
84
+ * in closures. The stateDir is retained for memory recall, which needs it
85
+ * directly.
86
+ */
87
+ export function createMegaBridge(opts: BridgeOptions): MegaBridge {
88
+ const stateDir = opts.stateDir;
89
+ let vectorStore: VectorStore | undefined;
90
+ let turnStore: TurnStore | undefined;
91
+
92
+ const getVectorStore = (): VectorStore => {
93
+ if (!vectorStore) vectorStore = new VectorStore({ stateDir });
94
+ return vectorStore;
95
+ };
96
+ const getTurnStore = (): TurnStore => {
97
+ if (!turnStore) turnStore = createTurnStore({ stateDir });
98
+ return turnStore;
99
+ };
100
+
101
+ return {
102
+ compact(input: BridgeCompactInput): BridgeCompactResult {
103
+ const result = compactSession(
104
+ {
105
+ sessionId: input.sessionId,
106
+ messages: input.messages,
107
+ keepFrom: input.keepFrom,
108
+ summary: input.summary,
109
+ keyDecisions: input.keyDecisions,
110
+ nextSteps: input.nextSteps,
111
+ filesModified: input.filesModified,
112
+ compressionPressure: input.compressionPressure,
113
+ },
114
+ getVectorStore(),
115
+ );
116
+ return {
117
+ skipped: result.skipped,
118
+ deduped: result.deduped,
119
+ summary: result.summary,
120
+ checkpointId: result.checkpointId,
121
+ tokenEstimate: result.tokenEstimate,
122
+ originalTokenEstimate: result.originalTokenEstimate,
123
+ compactedFrom: result.compactedFrom,
124
+ };
125
+ },
126
+
127
+ recallCheckpoints(opts: BridgeRecallOptions): BridgeRecallResult {
128
+ const recallOpts: RecallInjectOptions = {
129
+ sessionId: opts.sessionId,
130
+ query: opts.query,
131
+ limit: opts.limit ?? 3,
132
+ source: "command",
133
+ skipInjected: opts.skipInjected,
134
+ recallMaxTokens: opts.recallMaxTokens,
135
+ };
136
+ return mapRecallResult(recallAndInline(recallOpts, getVectorStore()));
137
+ },
138
+
139
+ async recallMemories(opts: BridgeMemoryRecallOptions): Promise<BridgeMemoryRecallResult> {
140
+ const memOpts: MemoryRecallInjectOptions = {
141
+ query: opts.query,
142
+ stateDir,
143
+ limit: opts.limit,
144
+ minSimilarity: opts.minSimilarity,
145
+ crossRepo: opts.crossRepo,
146
+ crossRepoCosine: opts.crossRepoCosine,
147
+ recallMaxTokens: opts.recallMaxTokens,
148
+ };
149
+ const r = await recallMemoriesAndInline(memOpts);
150
+ return mapMemoryResult(r);
151
+ },
152
+
153
+ async recallAndInlineAsync(opts: BridgeRecallOptions): Promise<BridgeRecallResult> {
154
+ const recallOpts: RecallInjectOptions = {
155
+ sessionId: opts.sessionId,
156
+ query: opts.query,
157
+ limit: opts.limit ?? 3,
158
+ source: "command",
159
+ skipInjected: opts.skipInjected,
160
+ recallMaxTokens: opts.recallMaxTokens,
161
+ };
162
+ const r = await recallAndInlineAsync(recallOpts, getVectorStore());
163
+ return mapRecallResult(r);
164
+ },
165
+
166
+ fork(opts: BridgeForkOptions): BridgeForkResult {
167
+ try {
168
+ const outcome = forkFromConversation(
169
+ getTurnStore(),
170
+ opts.parentConversationId,
171
+ opts.turnIndex,
172
+ );
173
+ return {
174
+ childConversationId: outcome.childConversationId,
175
+ checkpointIds: outcome.checkpointIds,
176
+ forkTurnIndex: opts.turnIndex,
177
+ };
178
+ } catch (e) {
179
+ if (e instanceof ForkError) {
180
+ return { error: e.code };
181
+ }
182
+ // The bridge is pi-agnostic (no runtime handle) — the host-side adapter
183
+ // (ithacus) owns the internal-error ring for fork failures, so this
184
+ // re-thrown non-ForkError is recorded there, not here.
185
+ // guardrails-allow INTERNAL-ERR: bridge is pi-agnostic; no runtime handle — host adapter records fork failures
186
+ throw e;
187
+ }
188
+ },
189
+
190
+ cortexQuery(opts: BridgeCortexOptions): BridgeCortexResult {
191
+ const limit = opts.limit ?? 3;
192
+ const scope = opts.repo ?? repoKey(stateDir);
193
+ const hits = vectorSearch(getVectorStore(), scope, opts.query, limit);
194
+ return mapCortexHits(hits, limit);
195
+ },
196
+
197
+ addMemory(input: BridgeAddMemoryInput): number | void {
198
+ // repo === null ⇒ stateDir-scoped durable memory (matches recallMemories).
199
+ return addMemory(
200
+ {
201
+ kind: input.kind,
202
+ content: input.content,
203
+ tags: input.tags,
204
+ category: input.category,
205
+ },
206
+ null,
207
+ stateDir,
208
+ );
209
+ },
210
+
211
+ recordTurn(input: BridgeRecordTurnInput): void {
212
+ // S49R: child turns get the conversation-monotonic index (MAX+1) so a
213
+ // re-dispatched child (same conversation, restarted session counter) never
214
+ // collides with UNIQUE(conversation_id, turn_index). Carry the child's
215
+ // session counter as sessionTurnIndex for the raw_transcript join.
216
+ const turnIndex = getTurnStore().asReader().nextTurnIndexFor(input.conversationId);
217
+ const turn: TurnEntry = {
218
+ conversationId: input.conversationId,
219
+ sessionId: input.sessionId,
220
+ turnIndex,
221
+ sessionTurnIndex: input.turnIndex,
222
+ role: (input.role as TurnEntry["role"]) ?? "assistant",
223
+ endedAt: input.endedAt ?? Date.now(),
224
+ ctxTokens: input.ctxTokens,
225
+ ctxPercent: input.ctxPercent,
226
+ model: input.model,
227
+ };
228
+ // The bridge is pi-agnostic (no runtime handle); the host-side adapter
229
+ // (ithacus) owns the internal-error ring for appendTurn failures. The
230
+ // recordTurn child-path twin of Finding 2 is recorded there, not here.
231
+ // guardrails-allow INTERNAL-ERR: bridge is pi-agnostic; no runtime handle — host adapter records appendTurn failures
232
+ getTurnStore().asWriter().appendTurn(turn);
233
+ },
234
+
235
+ close(): void {
236
+ if (turnStore) {
237
+ try {
238
+ turnStore.close();
239
+ } catch {
240
+ /* best-effort */
241
+ }
242
+ }
243
+ },
244
+ };
245
+ }