pi-mega-compact 0.11.3 → 0.11.5

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 (307) hide show
  1. package/dist/extensions/dashboard-server/api-contracts/endpoints.js +19 -0
  2. package/dist/extensions/dashboard-server/api-contracts/memory.js +8 -0
  3. package/dist/extensions/dashboard-server/api-contracts/provider-cache.js +1 -0
  4. package/dist/extensions/dashboard-server/api-contracts/setup.js +10 -0
  5. package/dist/extensions/dashboard-server/index-reader.js +6 -2
  6. package/dist/extensions/dashboard-server/routes-cache.js +115 -55
  7. package/dist/extensions/dashboard-server/routes-memory.js +51 -0
  8. package/dist/extensions/dashboard-server/routes-repo.js +2 -2
  9. package/dist/extensions/dashboard-server/routes-setup.js +162 -0
  10. package/dist/extensions/dashboard-server/routes.js +2 -0
  11. package/dist/extensions/dashboard-server/server.js +7 -1
  12. package/dist/extensions/mega-commands.js +189 -1
  13. package/dist/extensions/mega-config.js +3 -0
  14. package/dist/extensions/mega-events/agent-handlers.js +41 -6
  15. package/dist/extensions/mega-events/compact-handlers.js +1 -0
  16. package/dist/extensions/mega-events/context-handler.js +37 -92
  17. package/dist/extensions/mega-events/context-hwm.js +51 -0
  18. package/dist/extensions/mega-events/error-classifier.js +118 -128
  19. package/dist/extensions/mega-events/mirror-append.js +107 -0
  20. package/dist/extensions/mega-events/outage-advisor.js +59 -0
  21. package/dist/extensions/mega-events/perf-handler.js +51 -1
  22. package/dist/extensions/mega-events/recall-tail.js +59 -0
  23. package/dist/extensions/mega-events/session-handlers.js +15 -8
  24. package/dist/extensions/mega-runtime/reset-runtime.js +6 -0
  25. package/dist/extensions/mega-runtime/runtime.js +7 -0
  26. package/dist/src/embedder.js +41 -2
  27. package/dist/src/memoryStats.js +75 -0
  28. package/dist/src/prefix-break.js +43 -0
  29. package/dist/src/pricing.js +1 -1
  30. package/dist/src/store/sqlite/perf-samples.js +144 -16
  31. package/dist/src/store/turns/memory-store.js +8 -0
  32. package/dist/src/store/turns/sqlite-store.js +14 -3
  33. package/dist/src/store/turns/types.js +18 -1
  34. package/dist/src/store/vectorIndex.js +26 -5
  35. package/extensions/dashboard-client/dist/assets/{AchievementTiles-vEo2MtE2.js → AchievementTiles-BoL7qBOJ.js} +2 -2
  36. package/extensions/dashboard-client/dist/assets/{AchievementTiles-vEo2MtE2.js.map → AchievementTiles-BoL7qBOJ.js.map} +1 -1
  37. package/extensions/dashboard-client/dist/assets/AchievementsTab-BFDE3HYc.js +2 -0
  38. package/extensions/dashboard-client/dist/assets/{AchievementsTab-DjxfYrDc.js.map → AchievementsTab-BFDE3HYc.js.map} +1 -1
  39. package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js +2 -0
  40. package/extensions/dashboard-client/dist/assets/CacheTab-eKVc-NV1.js.map +1 -0
  41. package/extensions/dashboard-client/dist/assets/{ConfigTab-Cm6ymbmx.js → ConfigTab-DDFE-ckA.js} +2 -2
  42. package/extensions/dashboard-client/dist/assets/{ConfigTab-Cm6ymbmx.js.map → ConfigTab-DDFE-ckA.js.map} +1 -1
  43. package/extensions/dashboard-client/dist/assets/{EventsTab-DXWl_YzI.js → EventsTab-B6UxZ7PM.js} +2 -2
  44. package/extensions/dashboard-client/dist/assets/{EventsTab-DXWl_YzI.js.map → EventsTab-B6UxZ7PM.js.map} +1 -1
  45. package/extensions/dashboard-client/dist/assets/{GameTab-BLDudcO5.js → GameTab-DehUX-KE.js} +2 -2
  46. package/extensions/dashboard-client/dist/assets/{GameTab-BLDudcO5.js.map → GameTab-DehUX-KE.js.map} +1 -1
  47. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DLkKCztn.js → MaintenanceTab-0GWRt4wn.js} +2 -2
  48. package/extensions/dashboard-client/dist/assets/{MaintenanceTab-DLkKCztn.js.map → MaintenanceTab-0GWRt4wn.js.map} +1 -1
  49. package/extensions/dashboard-client/dist/assets/{MetricsTab-Cq4u-rZF.js → MetricsTab-C3rwMVsN.js} +2 -2
  50. package/extensions/dashboard-client/dist/assets/{MetricsTab-Cq4u-rZF.js.map → MetricsTab-C3rwMVsN.js.map} +1 -1
  51. package/extensions/dashboard-client/dist/assets/OverviewTab-39O9_x5j.js +2 -0
  52. package/extensions/dashboard-client/dist/assets/{OverviewTab-D4kGvsiQ.js.map → OverviewTab-39O9_x5j.js.map} +1 -1
  53. package/extensions/dashboard-client/dist/assets/ReposTab-Cfj6Q5sH.js +2 -0
  54. package/extensions/dashboard-client/dist/assets/ReposTab-Cfj6Q5sH.js.map +1 -0
  55. package/extensions/dashboard-client/dist/assets/{SessionsTab-DZC2-GCn.js → SessionsTab-DXDTCi3r.js} +2 -2
  56. package/extensions/dashboard-client/dist/assets/{SessionsTab-DZC2-GCn.js.map → SessionsTab-DXDTCi3r.js.map} +1 -1
  57. package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js +2 -0
  58. package/extensions/dashboard-client/dist/assets/SetupTab-mMlfunJa.js.map +1 -0
  59. package/extensions/dashboard-client/dist/assets/TimeSavedCard-Cw1dJ4e9.js +2 -0
  60. package/extensions/dashboard-client/dist/assets/TimeSavedCard-Cw1dJ4e9.js.map +1 -0
  61. package/extensions/dashboard-client/dist/assets/{TopicsTab-01G71hv1.js → TopicsTab-BAw972BV.js} +2 -2
  62. package/extensions/dashboard-client/dist/assets/{TopicsTab-01G71hv1.js.map → TopicsTab-BAw972BV.js.map} +1 -1
  63. package/extensions/dashboard-client/dist/assets/{TurnsTab-DLRg0LZT.js → TurnsTab-Cf5mGIdl.js} +2 -2
  64. package/extensions/dashboard-client/dist/assets/{TurnsTab-DLRg0LZT.js.map → TurnsTab-Cf5mGIdl.js.map} +1 -1
  65. package/extensions/dashboard-client/dist/assets/format-COjDIVGj.js +2 -0
  66. package/extensions/dashboard-client/dist/assets/format-COjDIVGj.js.map +1 -0
  67. package/extensions/dashboard-client/dist/assets/{index-BX0bcgLu.js → index-4gQ7auad.js} +10 -10
  68. package/extensions/dashboard-client/dist/assets/index-4gQ7auad.js.map +1 -0
  69. package/extensions/dashboard-client/dist/assets/pricing-CvE4NllQ.js +2 -0
  70. package/extensions/dashboard-client/dist/assets/pricing-CvE4NllQ.js.map +1 -0
  71. package/extensions/dashboard-client/dist/assets/{useSSE-BBYt5UPB.js → useSSE-DNgMuyP6.js} +2 -2
  72. package/extensions/dashboard-client/dist/assets/{useSSE-BBYt5UPB.js.map → useSSE-DNgMuyP6.js.map} +1 -1
  73. package/extensions/dashboard-client/dist/index.html +1 -1
  74. package/extensions/dashboard-client/src/App.tsx +4 -0
  75. package/extensions/dashboard-client/src/api/client.ts +14 -0
  76. package/extensions/dashboard-client/src/components/ActiveReposTable.tsx +21 -12
  77. package/extensions/dashboard-client/src/components/ProviderCacheCard.tsx +56 -0
  78. package/extensions/dashboard-client/src/components/SavingsByModelTable.tsx +38 -57
  79. package/extensions/dashboard-client/src/tabs/CacheTab.tsx +1 -0
  80. package/extensions/dashboard-client/src/tabs/SetupTab.tsx +355 -0
  81. package/extensions/dashboard-client/tsconfig.json +3 -2
  82. package/extensions/dashboard-client/vite.config.ts +2 -0
  83. package/extensions/dashboard-server/api-contracts/endpoints.ts +36 -3
  84. package/extensions/dashboard-server/api-contracts/index.ts +7 -0
  85. package/extensions/dashboard-server/api-contracts/memory.ts +27 -0
  86. package/extensions/dashboard-server/api-contracts/multi-repo.ts +231 -231
  87. package/extensions/dashboard-server/api-contracts/provider-cache.ts +37 -0
  88. package/extensions/dashboard-server/api-contracts/setup.ts +58 -0
  89. package/extensions/dashboard-server/index-reader.ts +138 -121
  90. package/extensions/dashboard-server/routes-cache.ts +161 -85
  91. package/extensions/dashboard-server/routes-memory.ts +63 -0
  92. package/extensions/dashboard-server/routes-repo.ts +36 -36
  93. package/extensions/dashboard-server/routes-setup.ts +181 -0
  94. package/extensions/dashboard-server/routes.ts +2 -0
  95. package/extensions/dashboard-server/server.ts +6 -0
  96. package/extensions/mega-commands.ts +211 -1
  97. package/extensions/mega-config.ts +18 -0
  98. package/extensions/mega-events/agent-handlers.ts +44 -5
  99. package/extensions/mega-events/compact-handlers.ts +1 -0
  100. package/extensions/mega-events/context-handler.ts +45 -110
  101. package/extensions/mega-events/context-hwm.ts +85 -0
  102. package/extensions/mega-events/error-classifier.ts +156 -124
  103. package/extensions/mega-events/mirror-append.ts +155 -0
  104. package/extensions/mega-events/outage-advisor.ts +79 -0
  105. package/extensions/mega-events/perf-handler.ts +79 -5
  106. package/extensions/mega-events/recall-tail.ts +88 -0
  107. package/extensions/mega-events/session-handlers.ts +14 -7
  108. package/extensions/mega-runtime/helpers.ts +14 -0
  109. package/extensions/mega-runtime/reset-runtime.ts +6 -0
  110. package/extensions/mega-runtime/runtime.ts +7 -0
  111. package/package.json +5 -2
  112. package/src/embedder.ts +42 -2
  113. package/src/memoryStats.ts +107 -0
  114. package/src/prefix-break.ts +83 -0
  115. package/src/pricing.ts +2 -2
  116. package/src/store/sqlite/perf-samples.ts +215 -22
  117. package/src/store/turns/memory-store.ts +10 -1
  118. package/src/store/turns/sqlite-store.ts +27 -15
  119. package/src/store/turns/types.ts +21 -0
  120. package/src/store/vectorIndex.ts +33 -4
  121. package/dist/extensions/conflict-scan.test.js +0 -115
  122. package/dist/extensions/dashboard-server/api-contracts.test.js +0 -884
  123. package/dist/extensions/dashboard-server/html.test.js +0 -52
  124. package/dist/extensions/dashboard-server/perf-server.test.js +0 -80
  125. package/dist/extensions/dashboard-server/routes-cache.test.js +0 -248
  126. package/dist/extensions/dashboard-server/server.test.js +0 -120
  127. package/dist/extensions/dashboard-server-s32.test.js +0 -181
  128. package/dist/extensions/dashboard-server.test.js +0 -339
  129. package/dist/extensions/mega-cache-replay.test.js +0 -400
  130. package/dist/extensions/mega-compact-s38.test.js +0 -820
  131. package/dist/extensions/mega-compact.test.js +0 -1112
  132. package/dist/extensions/mega-events.test.js +0 -47
  133. package/dist/extensions/mega-game-cmds.test.js +0 -163
  134. package/dist/extensions/mega-runtime/snapshot.test.js +0 -127
  135. package/dist/extensions/mega-runtime/state.test.js +0 -266
  136. package/dist/extensions/mega-runtime/widget.test.js +0 -343
  137. package/dist/extensions/mega-shutdown-widget.test.js +0 -121
  138. package/dist/extensions/mega-teamrun.test.js +0 -156
  139. package/dist/extensions/mega-trim.test.js +0 -58
  140. package/dist/extensions/mega-turn-store.test.js +0 -68
  141. package/dist/src/boundary.test.js +0 -170
  142. package/dist/src/compact.test.js +0 -78
  143. package/dist/src/config/themes.test.js +0 -94
  144. package/dist/src/dedup/dedup.test.js +0 -41
  145. package/dist/src/dedup/l1.test.js +0 -50
  146. package/dist/src/dedup/raptor/buildHistory.test.js +0 -292
  147. package/dist/src/dedup/raptor/multilevel-serve.test.js +0 -229
  148. package/dist/src/dedup/raptor/multilevel.test.js +0 -238
  149. package/dist/src/dedup/raptor/promote.test.js +0 -69
  150. package/dist/src/dedup/raptor/raptor.test.js +0 -248
  151. package/dist/src/dedup/raptor/retrieval.test.js +0 -95
  152. package/dist/src/dedup/raptor/serve-gate.test.js +0 -298
  153. package/dist/src/dedup/sprint12.test.js +0 -219
  154. package/dist/src/dedup-engine.test.js +0 -472
  155. package/dist/src/driftDetection.test.js +0 -87
  156. package/dist/src/e2e.test.js +0 -698
  157. package/dist/src/engine.test.js +0 -111
  158. package/dist/src/extractive.test.js +0 -147
  159. package/dist/src/fork.test.js +0 -81
  160. package/dist/src/game/scoring.test.js +0 -98
  161. package/dist/src/httpEmbedder.test.js +0 -277
  162. package/dist/src/importance.test.js +0 -372
  163. package/dist/src/intent.test.js +0 -78
  164. package/dist/src/log.test.js +0 -42
  165. package/dist/src/mechanical-fix.test.js +0 -65
  166. package/dist/src/memory.test.js +0 -70
  167. package/dist/src/memoryConsolidate.test.js +0 -38
  168. package/dist/src/memoryOps.test.js +0 -129
  169. package/dist/src/memoryRecall.test.js +0 -171
  170. package/dist/src/memoryRoundtrip.test.js +0 -137
  171. package/dist/src/metrics/turns.test.js +0 -152
  172. package/dist/src/mirror/mirror.test.js +0 -185
  173. package/dist/src/raptor-inject-summaries.test.js +0 -162
  174. package/dist/src/ratio.bench.test.js +0 -897
  175. package/dist/src/recall.integration.test.js +0 -77
  176. package/dist/src/recall.test.js +0 -379
  177. package/dist/src/sprint14.test.js +0 -219
  178. package/dist/src/sprint4x-rag-verification.test.js +0 -93
  179. package/dist/src/store/compression.test.js +0 -139
  180. package/dist/src/store/memoryIndex.test.js +0 -51
  181. package/dist/src/store/migrate.test.js +0 -139
  182. package/dist/src/store/pgOpenGuard.test.js +0 -74
  183. package/dist/src/store/phase01.test.js +0 -81
  184. package/dist/src/store/phase04.test.js +0 -54
  185. package/dist/src/store/sprint10.test.js +0 -186
  186. package/dist/src/store/sqlite/game-achievements.test.js +0 -67
  187. package/dist/src/store/sqlite/game-scores.test.js +0 -106
  188. package/dist/src/store/sqlite/game-state.test.js +0 -76
  189. package/dist/src/store/sqlite/global-index.test.js +0 -136
  190. package/dist/src/store/sqlite/mechanical-fix.test.js +0 -146
  191. package/dist/src/store/sqlite/perf-samples.test.js +0 -146
  192. package/dist/src/store/sqlite/raptor.test.js +0 -106
  193. package/dist/src/store/sqlite/turns.test.js +0 -183
  194. package/dist/src/store/sqlite.cachehit.test.js +0 -55
  195. package/dist/src/store/sqlite.dbmaint.test.js +0 -158
  196. package/dist/src/store/sqlite.dbmirror.test.js +0 -184
  197. package/dist/src/store/turns/contract-compliance.test.js +0 -428
  198. package/dist/src/store/turns/memory-store.test.js +0 -9
  199. package/dist/src/store/turns/migrations.test.js +0 -138
  200. package/dist/src/store/turns/sqlite-store.test.js +0 -144
  201. package/dist/src/store/vectorIndex.test.js +0 -123
  202. package/dist/src/store.test.js +0 -161
  203. package/dist/src/supersede.test.js +0 -36
  204. package/dist/src/topics/cluster.test.js +0 -150
  205. package/dist/src/topics/store.test.js +0 -117
  206. package/dist/src/types.test.js +0 -9
  207. package/dist/src/vector-search-cache.test.js +0 -157
  208. package/dist/src/vectorStore.test.js +0 -479
  209. package/dist/src/wiki.test.js +0 -171
  210. package/extensions/conflict-scan.test.ts +0 -129
  211. package/extensions/dashboard-client/dist/assets/AchievementsTab-DjxfYrDc.js +0 -2
  212. package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js +0 -2
  213. package/extensions/dashboard-client/dist/assets/CacheTab-Dnpv94wq.js.map +0 -1
  214. package/extensions/dashboard-client/dist/assets/OverviewTab-D4kGvsiQ.js +0 -2
  215. package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js +0 -2
  216. package/extensions/dashboard-client/dist/assets/ReposTab-C0Wnrf2u.js.map +0 -1
  217. package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js +0 -2
  218. package/extensions/dashboard-client/dist/assets/TimeSavedCard-UqljSyP-.js.map +0 -1
  219. package/extensions/dashboard-client/dist/assets/index-BX0bcgLu.js.map +0 -1
  220. package/extensions/dashboard-server/api-contracts.test.ts +0 -1065
  221. package/extensions/dashboard-server/html.test.ts +0 -62
  222. package/extensions/dashboard-server/perf-server.test.ts +0 -101
  223. package/extensions/dashboard-server/routes-cache.test.ts +0 -302
  224. package/extensions/dashboard-server/server.test.ts +0 -131
  225. package/extensions/dashboard-server-s32.test.ts +0 -195
  226. package/extensions/dashboard-server.test.ts +0 -382
  227. package/extensions/mega-cache-replay.test.ts +0 -526
  228. package/extensions/mega-compact-s38.test.ts +0 -872
  229. package/extensions/mega-compact.test.ts +0 -1509
  230. package/extensions/mega-events.test.ts +0 -55
  231. package/extensions/mega-game-cmds.test.ts +0 -197
  232. package/extensions/mega-runtime/snapshot.test.ts +0 -139
  233. package/extensions/mega-runtime/state.test.ts +0 -262
  234. package/extensions/mega-runtime/widget.test.ts +0 -468
  235. package/extensions/mega-shutdown-widget.test.ts +0 -141
  236. package/extensions/mega-teamrun.test.ts +0 -176
  237. package/extensions/mega-trim.test.ts +0 -64
  238. package/extensions/mega-turn-store.test.ts +0 -108
  239. package/src/boundary.test.ts +0 -187
  240. package/src/compact.test.ts +0 -99
  241. package/src/config/themes.test.ts +0 -116
  242. package/src/dedup/dedup.test.ts +0 -46
  243. package/src/dedup/l1.test.ts +0 -57
  244. package/src/dedup/raptor/buildHistory.test.ts +0 -353
  245. package/src/dedup/raptor/multilevel-serve.test.ts +0 -273
  246. package/src/dedup/raptor/multilevel.test.ts +0 -325
  247. package/src/dedup/raptor/promote.test.ts +0 -82
  248. package/src/dedup/raptor/raptor.test.ts +0 -297
  249. package/src/dedup/raptor/retrieval.test.ts +0 -118
  250. package/src/dedup/raptor/serve-gate.test.ts +0 -348
  251. package/src/dedup/sprint12.test.ts +0 -242
  252. package/src/dedup-engine.test.ts +0 -670
  253. package/src/driftDetection.test.ts +0 -100
  254. package/src/e2e.test.ts +0 -843
  255. package/src/engine.test.ts +0 -123
  256. package/src/extractive.test.ts +0 -174
  257. package/src/fork.test.ts +0 -94
  258. package/src/game/scoring.test.ts +0 -103
  259. package/src/httpEmbedder.test.ts +0 -286
  260. package/src/importance.test.ts +0 -538
  261. package/src/intent.test.ts +0 -87
  262. package/src/log.test.ts +0 -47
  263. package/src/mechanical-fix.test.ts +0 -70
  264. package/src/memory.test.ts +0 -92
  265. package/src/memoryConsolidate.test.ts +0 -47
  266. package/src/memoryOps.test.ts +0 -148
  267. package/src/memoryRecall.test.ts +0 -194
  268. package/src/memoryRoundtrip.test.ts +0 -155
  269. package/src/metrics/turns.test.ts +0 -165
  270. package/src/mirror/mirror.test.ts +0 -240
  271. package/src/raptor-inject-summaries.test.ts +0 -228
  272. package/src/ratio.bench.test.ts +0 -1316
  273. package/src/recall.integration.test.ts +0 -96
  274. package/src/recall.test.ts +0 -467
  275. package/src/sprint14.test.ts +0 -245
  276. package/src/sprint4x-rag-verification.test.ts +0 -119
  277. package/src/store/compression.test.ts +0 -168
  278. package/src/store/memoryIndex.test.ts +0 -61
  279. package/src/store/migrate.test.ts +0 -158
  280. package/src/store/pgOpenGuard.test.ts +0 -89
  281. package/src/store/phase01.test.ts +0 -88
  282. package/src/store/phase04.test.ts +0 -58
  283. package/src/store/sprint10.test.ts +0 -214
  284. package/src/store/sqlite/game-achievements.test.ts +0 -80
  285. package/src/store/sqlite/game-scores.test.ts +0 -132
  286. package/src/store/sqlite/game-state.test.ts +0 -89
  287. package/src/store/sqlite/global-index.test.ts +0 -222
  288. package/src/store/sqlite/mechanical-fix.test.ts +0 -162
  289. package/src/store/sqlite/perf-samples.test.ts +0 -169
  290. package/src/store/sqlite/raptor.test.ts +0 -139
  291. package/src/store/sqlite/turns.test.ts +0 -218
  292. package/src/store/sqlite.cachehit.test.ts +0 -68
  293. package/src/store/sqlite.dbmaint.test.ts +0 -193
  294. package/src/store/sqlite.dbmirror.test.ts +0 -229
  295. package/src/store/turns/contract-compliance.test.ts +0 -562
  296. package/src/store/turns/memory-store.test.ts +0 -15
  297. package/src/store/turns/migrations.test.ts +0 -161
  298. package/src/store/turns/sqlite-store.test.ts +0 -161
  299. package/src/store/vectorIndex.test.ts +0 -140
  300. package/src/store.test.ts +0 -191
  301. package/src/supersede.test.ts +0 -42
  302. package/src/topics/cluster.test.ts +0 -176
  303. package/src/topics/store.test.ts +0 -132
  304. package/src/types.test.ts +0 -10
  305. package/src/vector-search-cache.test.ts +0 -190
  306. package/src/vectorStore.test.ts +0 -533
  307. package/src/wiki.test.ts +0 -217
@@ -3,8 +3,11 @@
3
3
  *
4
4
  * Captures cheap, local-only telemetry into the `perf_samples` SQLite table for
5
5
  * the dashboard's Perf tab: turn + provider latency, TPS, cache hit %, and (via
6
- * MegaRuntime.ensurePerfInterval) a 5s cpu/mem interval. All capture is wrapped in
7
- * try/catch — instrumentation NEVER blocks the agent loop (non-fatal).
6
+ * MegaRuntime.ensurePerfInterval) a 5s cpu/mem interval. All capture is wrapped
7
+ * in try/catch — instrumentation NEVER blocks the agent loop (non-fatal).
8
+ *
9
+ * S53A: detects significant cache_hit_pct drops (delta > 20 pp) between
10
+ * consecutive turns and records a `prefix_break` sample with a classified cause.
8
11
  *
9
12
  * PREVENT-PI-004: Date.now / process.cpuUsage / process.memoryUsage + local
10
13
  * SQLite only, zero network.
@@ -13,6 +16,7 @@
13
16
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
14
17
  import { type MegaRuntime } from "../mega-runtime.js";
15
18
  import { recordPerfSample } from "../../src/store/sqlite.js";
19
+ import { classifyPrefixBreak, type EventTimestamps } from "../../src/prefix-break.js";
16
20
 
17
21
  /** Structural view of an AssistantMessage usage block (no pi-ai import). */
18
22
  interface UsageBlock {
@@ -30,6 +34,51 @@ function usageOf(
30
34
  return msg.usage;
31
35
  }
32
36
 
37
+ /**
38
+ * Attempt to record a prefix_break sample when a significant cache_hit_pct
39
+ * drop is detected between two consecutive turns.
40
+ *
41
+ * Drop threshold: 20 percentage points. When triggered, calls
42
+ * classifyPrefixBreak with the three candidate event timestamps from the
43
+ * runtime and records a `prefix_break` sample. Best-effort + non-fatal.
44
+ *
45
+ * PREVENT-PI-004: guardrails-allow PREVENT-PI-004: local SQLite write only.
46
+ * PREVENT-002: SQL is fully parameterized via recordPerfSample.
47
+ */
48
+ function tryRecordPrefixBreak(
49
+ stateDir: string,
50
+ prevHitPct: number,
51
+ currHitPct: number,
52
+ breakAt: number,
53
+ runtime: MegaRuntime,
54
+ ): void {
55
+ try {
56
+ if (prevHitPct - currHitPct < 20) return; // no significant drop
57
+
58
+ const events: EventTimestamps = {
59
+ lastRecallAt: runtime.rt.lastInjectAt, // S53: inject fires alongside recall
60
+ lastCompactAt: runtime.rt.lastCompactAt,
61
+ lastInjectAt: runtime.rt.lastInjectAt,
62
+ };
63
+ const { cause, confidence } = classifyPrefixBreak(breakAt, events);
64
+
65
+ recordPerfSample(
66
+ stateDir,
67
+ "prefix_break",
68
+ 0,
69
+ {
70
+ cause,
71
+ confidence,
72
+ prevHitPct,
73
+ currHitPct,
74
+ breakAt,
75
+ },
76
+ ); // guardrails-allow PREVENT-PI-004: local SQLite write (loopback perf instrumentation)
77
+ } catch {
78
+ /* non-fatal: prefix-break detection never blocks the agent loop */
79
+ }
80
+ }
81
+
33
82
  /** Register perf instrumentation handlers + start the 5s cpu/mem interval. */
34
83
  export function registerPerfHandler(
35
84
  pi: ExtensionAPI,
@@ -51,6 +100,7 @@ export function registerPerfHandler(
51
100
 
52
101
  // turn_end: compute turn latency + TPS + cache hit % from the assistant
53
102
  // message's usage block. One perf_samples row per metric per turn.
103
+ // S53A: tracks prevHitPct across turns to detect prefix breaks.
54
104
  pi.on("turn_end", async (event) => {
55
105
  try {
56
106
  if (runtime.perfTurnStart > 0) {
@@ -73,10 +123,34 @@ export function registerPerfHandler(
73
123
  runtime.currentStateDir,
74
124
  "cache_hit_pct",
75
125
  hitPct,
76
- { input: u.input, cacheRead: u.cacheRead, cacheWrite: u.cacheWrite },
126
+ {
127
+ input: u.input,
128
+ cacheRead: u.cacheRead,
129
+ cacheWrite: u.cacheWrite,
130
+ modelName: runtime.currentModel?.modelName,
131
+ modelId: runtime.currentModel?.modelId,
132
+ },
77
133
  );
134
+
135
+ // S53A: detect significant cache hit % drop → record prefix_break.
136
+ // Only trigger when we have a previous value AND the drop is >20pp.
137
+ if (
138
+ runtime.rt._prevCacheHitPct != null &&
139
+ runtime.rt._prevCacheHitPct - hitPct >= 20
140
+ ) {
141
+ const breakAt = runtime.perfTurnStart;
142
+ tryRecordPrefixBreak(
143
+ runtime.currentStateDir,
144
+ runtime.rt._prevCacheHitPct,
145
+ hitPct,
146
+ breakAt,
147
+ runtime,
148
+ );
149
+ }
150
+ // Stamp for the next turn's comparison (even when no break fires).
151
+ runtime.rt._prevCacheHitPct = hitPct;
152
+ }
78
153
  }
79
- }
80
154
  } catch {
81
155
  /* non-fatal: instrumentation must never break the agent loop */
82
156
  }
@@ -110,4 +184,4 @@ export function registerPerfHandler(
110
184
  // Start the 5s cpu/mem sampling interval (one per MegaRuntime; cleared in
111
185
  // runtime.dispose()). Idempotent — safe to call again after a dispose().
112
186
  runtime.ensurePerfInterval();
113
- }
187
+ }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * recall-tail.ts — S53 Recall Tail Injection: append staged recall/memory blocks
3
+ * as a single user-role message at the very tail of the context view, instead
4
+ * of prepending them as part of systemPrompt (the pre-S53 behavior).
5
+ *
6
+ * Key invariants (enforced / tested):
7
+ * - PREVENT-PI-001/002: append-only at the very tail; never splits a
8
+ * toolCall/toolResult pair.
9
+ * - PREVENT-PI-003: user-role message, never system role.
10
+ * - Turn-scoped pin: staged blocks inject on EVERY context event of the turn
11
+ * (tool loops); consumed at turn_end ONLY if actually injected.
12
+ * - Non-fatal: any failure returns the unmodified messages array.
13
+ * - Flag OFF: no-op (returns messages unchanged — byte-identical pre-sprint;
14
+ * session-handlers.ts keeps the legacy systemPrompt prepend path).
15
+ */
16
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
17
+ import type { MegaRuntime } from "../mega-runtime.js";
18
+ import type { MegaConfig } from "../mega-config.js";
19
+
20
+ /**
21
+ * True when recall blocks are staged AND the feature flag is ON. Called by the
22
+ * context handler to decide whether to call withRecallTail.
23
+ */
24
+ export function stagedForTail(
25
+ runtime: MegaRuntime,
26
+ config: MegaConfig,
27
+ ): boolean {
28
+ if (!config.recallTailInject) return false;
29
+ return (
30
+ runtime.pendingRecallBlock != null ||
31
+ runtime.pendingMemoryRecallBlock != null
32
+ );
33
+ }
34
+
35
+ /**
36
+ * Append staged recall/memory blocks as a single user-role message at the very
37
+ * tail of the messages array. Returns a NEW array (does not mutate input).
38
+ *
39
+ * Sets `runtime.recallInjectedThisTurn = true` when it actually appends, so the
40
+ * turn_end handler (agent-handlers.ts) consumes the staged blocks only if they
41
+ * reached a view this turn. If a turn ends with no context event, the flag
42
+ * stays false and the blocks remain staged for the next turn.
43
+ *
44
+ * Stable timestamp (runtime.perfTurnStart ?? Date.now()) so replays within the
45
+ * turn are byte-identical (mirrors the v0.8.6 summaryAgentMsg rationale).
46
+ *
47
+ * Non-fatal: on any failure, returns messages unmodified and does NOT set the
48
+ * injected flag. PREVENT-PI-003: the injected message has role "user", never
49
+ * "system".
50
+ */
51
+ export function withRecallTail(
52
+ messages: readonly AgentMessage[],
53
+ runtime: MegaRuntime,
54
+ config: MegaConfig,
55
+ ): AgentMessage[] {
56
+ if (!config.recallTailInject) return messages as AgentMessage[];
57
+ try {
58
+ const blocks: string[] = [];
59
+ if (runtime.pendingRecallBlock != null) {
60
+ blocks.push(runtime.pendingRecallBlock);
61
+ }
62
+ if (runtime.pendingMemoryRecallBlock != null) {
63
+ blocks.push(runtime.pendingMemoryRecallBlock);
64
+ }
65
+ if (blocks.length === 0) return messages as AgentMessage[];
66
+
67
+ const tailText = blocks.join("\n\n");
68
+ const timestamp =
69
+ runtime.perfTurnStart ?? // stable within a turn
70
+ Date.now();
71
+
72
+ const tailMsg = {
73
+ role: "user" as const,
74
+ content: tailText,
75
+ timestamp,
76
+ } as unknown as AgentMessage;
77
+
78
+ runtime.rt.recallInjectedThisTurn = true;
79
+ // Append at the very tail — never splits toolCall/toolResult.
80
+ // PREVENT-PI-001/002: a single message appended after a complete
81
+ // transcript prefix cannot split a toolCall/toolResult pair.
82
+ return [...messages, tailMsg];
83
+ } catch {
84
+ // Non-fatal: leave blocks staged (flag stays unset) and return the
85
+ // unmodified view. The context event still proceeds normally.
86
+ return messages as AgentMessage[];
87
+ }
88
+ }
@@ -169,13 +169,20 @@ export function registerSessionHandlers(
169
169
  } catch {
170
170
  /* non-fatal: intent polling never breaks the agent loop */
171
171
  }
172
- const cpBlock = runtime.pendingRecallBlock;
173
- const memBlock = runtime.pendingMemoryRecallBlock;
174
- if (!cpBlock && !memBlock) return;
175
- runtime.pendingRecallBlock = undefined;
176
- runtime.pendingMemoryRecallBlock = undefined;
177
- const composed = [cpBlock, memBlock].filter(Boolean).join("\n\n");
178
- return { systemPrompt: `${event.systemPrompt}\n\n${composed}` };
172
+ // S53: when recallTailInject is ON, the staged blocks are injected as a
173
+ // user-role tail message by the context handler (recall-tail.ts), NOT
174
+ // here. This prepend path is the legacy behavior (flag OFF = byte-
175
+ // identical pre-sprint). captureModel + rewind-intent consumption above
176
+ // stay unconditional regardless of the flag.
177
+ if (!config.recallTailInject) {
178
+ const cpBlock = runtime.pendingRecallBlock;
179
+ const memBlock = runtime.pendingMemoryRecallBlock;
180
+ if (!cpBlock && !memBlock) return;
181
+ runtime.pendingRecallBlock = undefined;
182
+ runtime.pendingMemoryRecallBlock = undefined;
183
+ const composed = [cpBlock, memBlock].filter(Boolean).join("\n\n");
184
+ return { systemPrompt: `${event.systemPrompt}\n\n${composed}` };
185
+ }
179
186
  });
180
187
 
181
188
  pi.on("session_shutdown", async (_event, ctx) => {
@@ -37,6 +37,11 @@ export interface SessionRuntime {
37
37
  dedupAttempts: number; // total compaction attempts (for hit-rate denominator)
38
38
  tokensSaved: number; // this session-instance only: reset on session_start
39
39
  lastCompactAt: number | null; // wall-clock ms of the last compaction this session
40
+ // S53A: prefix-break classification timestamps (wall-clock ms).
41
+ lastRecallAt: number | null; // last recall injection this session
42
+ lastInjectAt: number | null; // last tail injection this session
43
+ // S53A: previous cache_hit_pct for drop detection.
44
+ _prevCacheHitPct: number | null;
40
45
  lastNativeCompactAt: number | null; // COMPACT-DEDUP FIX: wall-clock ms of the last NATIVE pi compaction (session_compact event) — used by the agent_end/legacy race guard to skip a redundant ctx.compact() that would throw "Already compacted".
41
46
  // S25: live dashboard counters (reset on session_start, mirrored to SQLite).
42
47
  compactCount: number; // compactions performed this session-instance
@@ -67,11 +72,20 @@ export interface SessionRuntime {
67
72
  errorTextRepeatCount: number; // consecutive count of identical error signatures
68
73
  // R3b: one-per-session /clear advise message throttle.
69
74
  poisonedAdviseSent: boolean; // true after the advise message fires once
75
+ // R10: provider-outage advisory throttle (one per flapping episode).
76
+ providerOutageAdvised: boolean; // true after the outage advise message fires once
70
77
  // R3c: one guarded compact per error signature (avoids re-compacting the
71
78
  // same poisoned region repeatedly).
72
79
  poisonedCompactSignatures: Set<string>; // signatures already attempted a compact for
80
+ // S53: recall tail injection state — tracks whether a tail was injected this turn.
81
+ recallInjectedThisTurn: boolean;
73
82
  // R7: poisoned-context event counter for the dashboard.
74
83
  poisonedCount: number;
84
+ // R13: set when the extension sends a nudge (error-retry, resume, compact)
85
+ // that will trigger the next turn. The turn_end handler checks this flag and
86
+ // skips error classification for extension-initiated turns — prevents the
87
+ // feedback loop where our own nudges get misclassified as errors.
88
+ extensionInitiatedTurn: boolean;
75
89
  }
76
90
 
77
91
  // ── ownVersion ─────────────────────────────────────────────────────────────
@@ -54,6 +54,9 @@ export function resetRuntimeImpl(
54
54
  dedupAttempts: 0,
55
55
  tokensSaved: 0,
56
56
  lastCompactAt: null,
57
+ lastRecallAt: null,
58
+ lastInjectAt: null,
59
+ _prevCacheHitPct: null,
57
60
  lastNativeCompactAt: null,
58
61
  compactCount: 0,
59
62
  recallInjections: 0,
@@ -68,8 +71,11 @@ export function resetRuntimeImpl(
68
71
  lastErrorText: undefined,
69
72
  errorTextRepeatCount: 0,
70
73
  poisonedAdviseSent: false,
74
+ providerOutageAdvised: false,
71
75
  poisonedCompactSignatures: new Set(),
76
+ recallInjectedThisTurn: false,
72
77
  poisonedCount: 0,
78
+ extensionInitiatedTurn: false,
73
79
  };
74
80
  self.trimCache = null; // v0.8.6: never replay a stale trim into a new session
75
81
  self.statusKey = undefined;
@@ -87,6 +87,9 @@ export class MegaRuntime {
87
87
  dedupAttempts: 0,
88
88
  tokensSaved: 0,
89
89
  lastCompactAt: null,
90
+ lastRecallAt: null,
91
+ lastInjectAt: null,
92
+ _prevCacheHitPct: null,
90
93
  lastNativeCompactAt: null,
91
94
  compactCount: 0,
92
95
  recallInjections: 0,
@@ -101,8 +104,12 @@ export class MegaRuntime {
101
104
  lastErrorText: undefined,
102
105
  errorTextRepeatCount: 0,
103
106
  poisonedAdviseSent: false,
107
+ providerOutageAdvised: false,
104
108
  poisonedCompactSignatures: new Set(),
109
+ // S53: tracks whether withRecallTail injected this turn (consumed at turn_end).
110
+ recallInjectedThisTurn: false,
105
111
  poisonedCount: 0,
112
+ extensionInitiatedTurn: false,
106
113
  };
107
114
  // v0.8.6 cache-stability: the cached live-trim view for the current
108
115
  // compaction epoch. Set after a fresh runCompact + computeLiveTrimCut, and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mega-compact",
3
- "version": "0.11.3",
3
+ "version": "0.11.5",
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",
@@ -28,7 +28,10 @@
28
28
  "extensions/dashboard-client/dist",
29
29
  "src",
30
30
  "README.md",
31
- "LICENSE"
31
+ "LICENSE",
32
+ "!dist/**/*.test.js",
33
+ "!dist/**/*.test.js.map",
34
+ "!**/*.test.ts"
32
35
  ],
33
36
  "pi": {
34
37
  "extensions": [
package/src/embedder.ts CHANGED
@@ -59,21 +59,62 @@ function fnv1a(str: string): number {
59
59
  * Default embedder: character 3-gram bag-of-counts, hashed into a fixed-dim
60
60
  * vector, L2-normalized. Captures local lexical/structure overlap well enough
61
61
  * for checkpoint relevance ranking.
62
+ *
63
+ * S53B: includes a 256-entry (configurable) FIFO cache keyed by FNV-1a(text).
64
+ * Each cache entry holds a DEFENSIVE COPY of the embedding to prevent mutation
65
+ * from corrupting stored vectors. Set MEGACOMPACT_EMBED_CACHE=0 to disable.
62
66
  */
63
67
  export class TrigramEmbedder implements Embedder {
64
68
  readonly dim: number;
65
69
  private readonly seed: number;
70
+ /** @type {Map<string, Vector>} */
71
+ private readonly _cache: Map<string, Vector>;
72
+ private readonly _cacheSize: number;
73
+ private _hits = 0;
74
+ private _misses = 0;
66
75
 
67
76
  constructor(dim = 512, seed = 0x9e3779b9) {
68
77
  this.dim = dim;
69
78
  this.seed = seed >>> 0;
79
+ const cap = Number(process.env.MEGACOMPACT_EMBED_CACHE ?? "256");
80
+ this._cacheSize = cap > 0 ? Math.floor(cap) : 0;
81
+ this._cache = new Map();
82
+ }
83
+
84
+ /** Cache statistics — always { hits: 0, misses: 0 } when cache is disabled. */
85
+ getEmbedCacheStats(): { hits: number; misses: number } {
86
+ return { hits: this._hits, misses: this._misses };
70
87
  }
71
88
 
72
89
  embed(text: string): Vector {
90
+ if (this._cacheSize === 0) {
91
+ return this._embedRaw(text);
92
+ }
93
+ const key = fnv1a(text).toString(36);
94
+ const cached = this._cache.get(key);
95
+ if (cached !== undefined) {
96
+ this._hits++;
97
+ // Defensive copy: caller mutating the returned vector must not corrupt cache.
98
+ return cached.slice() as Vector;
99
+ }
100
+ this._misses++;
101
+ const vec = this._embedRaw(text);
102
+ // Store a defensive copy (slice) so caller mutation cannot corrupt stored value.
103
+ this._cache.set(key, vec.slice() as Vector);
104
+ // FIFO eviction: remove the oldest entry when at capacity.
105
+ if (this._cache.size > this._cacheSize) {
106
+ // Map insertion order is guaranteed — first key is the oldest.
107
+ const firstKey = this._cache.keys().next().value;
108
+ if (firstKey !== undefined) this._cache.delete(firstKey);
109
+ }
110
+ return vec;
111
+ }
112
+
113
+ /** Raw embed computation (no cache logic). Public so tests can bypass cache. */
114
+ _embedRaw(text: string): Vector {
73
115
  const vec = new Array<number>(this.dim).fill(0);
74
116
  const norm = text.toLowerCase().replace(/\s+/g, " ");
75
117
  if (norm.length === 0) return l2Normalize(vec);
76
- // Whole-string + word + char-trigram signals.
77
118
  vec[fnv1a(norm) % this.dim] += 1;
78
119
  for (const word of norm.split(" ")) {
79
120
  if (word.length === 0) continue;
@@ -84,7 +125,6 @@ export class TrigramEmbedder implements Embedder {
84
125
  vec[idx] += 1;
85
126
  }
86
127
  }
87
- // Edge: very short tokens still get a slot.
88
128
  if (norm.length < 3) vec[fnv1a(norm) % this.dim] += 1;
89
129
  return l2Normalize(vec);
90
130
  }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * memoryStats.ts — aggregate memory-store statistics (S53B).
3
+ *
4
+ * Reads from the durable SQLite memories table to produce:
5
+ * - totalMemories: all memories in the store
6
+ * - memoriesInLast30Days: memories created within the last 30 days
7
+ * - topStableMemories: top-N memories by recall count (desc)
8
+ * - avgRecallScore: mean recallCount across all memories
9
+ *
10
+ * recallCount = 1 if last_recalled_at IS NOT NULL, else 0.
11
+ * This is the durable signal from recallMemory() — a memory that was
12
+ * recalled at least once is "stable" (still relevant). The exact recall
13
+ * frequency is tracked via turn_recall rows (source='memory') for
14
+ * cross-session analysis, but the memories table provides a reliable
15
+ * on-store signal that is always present without a schema migration.
16
+ *
17
+ * Real-store only (PREVENT-PI-004). No mocks. No network.
18
+ */
19
+ import { openStore } from "./store/sqlite/utils.js";
20
+
21
+ export interface MemoryStatsResult {
22
+ totalMemories: number;
23
+ memoriesInLast30Days: number;
24
+ topStableMemories: Array<{
25
+ id: number;
26
+ text: string;
27
+ recallCount: number;
28
+ lastRecalledAt: number | null;
29
+ }>;
30
+ avgRecallScore: number;
31
+ }
32
+
33
+ export interface MemoryStatsOptions {
34
+ /** Max memories in topStableMemories (default 5). */
35
+ topN?: number;
36
+ }
37
+
38
+ /**
39
+ * Aggregate memory statistics from the SQLite store.
40
+ * Always returns a valid MemoryStatsResult; never throws.
41
+ */
42
+ export async function memoryStats(
43
+ stateDir: string,
44
+ opts: MemoryStatsOptions = {},
45
+ ): Promise<MemoryStatsResult> {
46
+ const topN = opts.topN ?? 5;
47
+ try {
48
+ const db = openStore(stateDir);
49
+ // ── totalMemories ──────────────────────────────────────────────────────
50
+ const totalRow = db
51
+ .prepare("SELECT COUNT(*) AS n FROM memories")
52
+ .get() as { n: number };
53
+ const totalMemories = totalRow.n;
54
+
55
+ // ── memoriesInLast30Days ───────────────────────────────────────────────
56
+ const thirtyDaysAgo = Math.floor(Date.now() / 1000) - 30 * 86_400;
57
+ const recentRow = db
58
+ .prepare(
59
+ "SELECT COUNT(*) AS n FROM memories WHERE created_at >= ?",
60
+ )
61
+ .get(thirtyDaysAgo) as { n: number };
62
+ const memoriesInLast30Days = recentRow.n;
63
+
64
+ // ── topStableMemories + avgRecallScore ─────────────────────────────────
65
+ // recallCount: 1 if last_recalled_at IS NOT NULL (recalled at least once),
66
+ // 0 otherwise. Stable memories score high.
67
+ const topRows = db
68
+ .prepare(
69
+ `SELECT
70
+ m.id,
71
+ m.content AS text,
72
+ m.last_recalled_at AS lastRecalledAt,
73
+ CASE WHEN m.last_recalled_at IS NOT NULL THEN 1 ELSE 0 END AS recallCount
74
+ FROM memories m
75
+ ORDER BY recallCount DESC, m.id ASC
76
+ LIMIT ?`,
77
+ )
78
+ .all(topN) as Array<Record<string, unknown>>;
79
+
80
+ // avgRecallScore = (count of recalled memories) / totalMemories.
81
+ // This represents the fraction of memories that are "stable" (recalled at least once).
82
+ const recalledRow = db
83
+ .prepare(
84
+ "SELECT COUNT(*) AS n FROM memories WHERE last_recalled_at IS NOT NULL",
85
+ )
86
+ .get() as { n: number };
87
+ const avgRecallScore =
88
+ totalMemories > 0 ? recalledRow.n / totalMemories : 0;
89
+
90
+ const topStableMemories = topRows.map((r) => ({
91
+ id: r.id as number,
92
+ text: (r.text as string) ?? "",
93
+ recallCount: (r.recallCount as number) ?? 0,
94
+ lastRecalledAt: (r.lastRecalledAt as number | null) ?? null,
95
+ }));
96
+
97
+ return { totalMemories, memoriesInLast30Days, topStableMemories, avgRecallScore };
98
+ } catch {
99
+ // Non-fatal: return zeros rather than propagating an error.
100
+ return {
101
+ totalMemories: 0,
102
+ memoriesInLast30Days: 0,
103
+ topStableMemories: [],
104
+ avgRecallScore: 0,
105
+ };
106
+ }
107
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * prefix-break.ts — S53A prefix-break classification.
3
+ *
4
+ * Classifies WHY the provider cache prefix broke on a given turn by comparing
5
+ * the break timestamp against the three candidate event timestamps with a
6
+ * configurable tolerance window.
7
+ *
8
+ * The classification is best-effort (non-fatal): null timestamps are absent,
9
+ * and a break outside all tolerance windows is classified as 'other'.
10
+ *
11
+ * Pi-agnostic: no pi runtime types, no network.
12
+ */
13
+
14
+ export interface PrefixBreakMeta {
15
+ /** Which event caused the break. */
16
+ cause: "recall" | "compaction" | "inject" | "other";
17
+ /** Confidence score 0.0–1.0 based on proximity to the matched event. */
18
+ confidence: number;
19
+ /** Cache hit % immediately before the break. */
20
+ prevHitPct: number;
21
+ /** Cache hit % of the first degraded sample. */
22
+ currHitPct: number;
23
+ /** Wall-clock ms of the break sample. */
24
+ breakAt: number;
25
+ }
26
+
27
+ export interface PrefixBreak {
28
+ id: number;
29
+ ts: number;
30
+ kind: "prefix_break";
31
+ value: number;
32
+ meta: PrefixBreakMeta;
33
+ }
34
+
35
+ /**
36
+ * Timestamps of recent events that can cause a prefix break.
37
+ * All values are wall-clock ms (Date.now()) or null if no event has occurred.
38
+ */
39
+ export interface EventTimestamps {
40
+ lastRecallAt: number | null;
41
+ lastCompactAt: number | null;
42
+ lastInjectAt: number | null;
43
+ }
44
+
45
+ /**
46
+ * Classify why the provider cache prefix broke at `breakTimestamp`.
47
+ *
48
+ * @param breakTimestamp Wall-clock ms of the cache hit % drop sample.
49
+ * @param events Recent event timestamps from the runtime.
50
+ * @param toleranceMs Max ms gap to consider a match (default 2000).
51
+ * @returns Classification result with cause + confidence.
52
+ *
53
+ * Priority: recall > compaction > inject. Confidence = 1 − (gap / (2·tolerance)),
54
+ * capped at [0.5, 1.0]. Returns 'other' when no event is within tolerance.
55
+ */
56
+ export function classifyPrefixBreak(
57
+ breakTimestamp: number,
58
+ events: EventTimestamps,
59
+ toleranceMs = 2000,
60
+ ): { cause: "recall" | "compaction" | "inject" | "other"; confidence: number } {
61
+ // Ordered by priority (recall wins if multiple match).
62
+ const candidates: Array<{
63
+ cause: "recall" | "compaction" | "inject";
64
+ ts: number | null;
65
+ }> = [
66
+ { cause: "recall", ts: events.lastRecallAt },
67
+ { cause: "compaction", ts: events.lastCompactAt },
68
+ { cause: "inject", ts: events.lastInjectAt },
69
+ ];
70
+
71
+ for (const { cause, ts } of candidates) {
72
+ if (ts == null) continue;
73
+ const gap = Math.abs(breakTimestamp - ts);
74
+ if (gap <= toleranceMs) {
75
+ // confidence = 1 − (gap / (2·tolerance)) ∈ [0.5, 1.0]
76
+ // exact match (gap=0) → 1.0; at boundary (gap=tolerance) → 0.5
77
+ const confidence = Math.max(0.5, 1 - gap / (2 * toleranceMs));
78
+ return { cause, confidence };
79
+ }
80
+ }
81
+
82
+ return { cause: "other", confidence: 0 };
83
+ }
package/src/pricing.ts CHANGED
@@ -39,7 +39,7 @@ export const MODEL_INPUT_RATES: Record<string, number> = {
39
39
  "gemini-2.0-flash": 0.1e-6,
40
40
  "deepseek-chat": 0.14e-6,
41
41
  "deepseek-reasoner": 0.55e-6,
42
- "codestral": 0.3e-6,
42
+ codestral: 0.3e-6,
43
43
  "mistral-large": 2e-6,
44
44
  "llama-3.1-405b": 1.33e-6,
45
45
  "llama-3.1-70b": 0.8e-6,
@@ -92,4 +92,4 @@ export function computeCacheSavings(
92
92
  cacheWriteCost,
93
93
  netSaved: cacheReadSaved - cacheWriteCost,
94
94
  };
95
- }
95
+ }