pi-mega-compact 0.11.4 → 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 +2 -0
  14. package/dist/extensions/mega-events/agent-handlers.js +19 -4
  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 +16 -5
  19. package/dist/extensions/mega-events/mirror-append.js +107 -0
  20. package/dist/extensions/mega-events/outage-advisor.js +7 -1
  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 +5 -0
  25. package/dist/extensions/mega-runtime/runtime.js +6 -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 +10 -0
  98. package/extensions/mega-events/agent-handlers.ts +20 -4
  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 +15 -4
  103. package/extensions/mega-events/mirror-append.ts +155 -0
  104. package/extensions/mega-events/outage-advisor.ts +11 -5
  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 +12 -0
  109. package/extensions/mega-runtime/reset-runtime.ts +5 -0
  110. package/extensions/mega-runtime/runtime.ts +6 -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 -1383
  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 -1433
  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
@@ -13,10 +13,8 @@ import type {
13
13
  import type { AgentMessage } from "@earendil-works/pi-agent-core";
14
14
  import {
15
15
  openStore,
16
- appendRawTranscript,
17
16
  writeCheckpointEpoch,
18
17
  type CheckpointEpoch,
19
- type RawTranscriptRow,
20
18
  } from "../../src/store/sqlite.js";
21
19
  import { epochIdFor } from "../../src/mirror/epoch.js";
22
20
  import { autoCompactCheck } from "../../src/compact.js";
@@ -37,84 +35,8 @@ import {
37
35
  pressureRatio,
38
36
  type MegaConfig,
39
37
  } from "../mega-config.js";
40
- import { computeContentDigest } from "../../src/dedup/digest.js";
41
-
42
- /**
43
- * Recursively canonicalize a value for deterministic JSON serialization:
44
- * - Objects: keys sorted alphabetically, values recursively canonicalized
45
- * - Arrays: elements recursively canonicalized (array ORDER is preserved)
46
- * - Primitives: returned as-is
47
- *
48
- * F5 fix: shallow-sorted JSON.stringify(content, Object.keys(content).sort())
49
- * omitted nested keys not in the top-level keys array and only sorted one level.
50
- * This recursively sorts every object at every depth so semantically-equal
51
- * differently-ordered content hashes identically.
52
- */
53
- function canonicalize(value: unknown): unknown {
54
- if (value === null || value === undefined) return value;
55
- if (typeof value !== "object") return value;
56
- if (Array.isArray(value)) return value.map(canonicalize);
57
- // Plain object: sort keys, recursively canonicalize values.
58
- const sorted = Object.keys(value as Record<string, unknown>).sort();
59
- const out: Record<string, unknown> = {};
60
- for (const k of sorted) {
61
- out[k] = canonicalize((value as Record<string, unknown>)[k]);
62
- }
63
- return out;
64
- }
65
-
66
- /**
67
- * Convert a pi AgentMessage to a RawTranscriptRow for the DB mirror.
68
- * content_bytes is canonical JSON (sorted keys, recursive) for deterministic
69
- * storage; contentHash is the canonical digest (normalize + hash) that matches
70
- * what dedupTranscript computes so the two pipelines use the same linkage key.
71
- *
72
- * F4 fix: the hash key MUST match on both sides of the append/dedup split.
73
- * Using computeContentDigest here (normalize → hash) ensures content_ref set
74
- * by dedupTranscript always resolves to an existing dedup_mirror row even for
75
- * whitespace/case-variant content. F5 fix: recursive canonicalize replaces the
76
- * broken shallow-sorted JSON.stringify replacer.
77
- */
78
- function toRawTranscriptRow(
79
- msg: AgentMessage,
80
- sessionId: string,
81
- epochId: string,
82
- currentTurn?: number,
83
- ): RawTranscriptRow | null {
84
- // Narrow to Message union (has content + timestamp).
85
- const m = msg as {
86
- role?: string;
87
- content?: unknown;
88
- timestamp?: number;
89
- toolName?: string;
90
- };
91
- const content = m.content;
92
- if (content == null || content === "") return null;
93
- // Canonical bytes: string content → normalize for consistent byte content;
94
- // object content → recursive canonicalize then JSON (no replacer).
95
- const contentBytes =
96
- typeof content === "string"
97
- ? content // F5: strings are primitives; their byte content is fixed
98
- : JSON.stringify(canonicalize(content));
99
- // F4 fix: use the same digest as dedupTranscript so contentHash is consistent
100
- // on both sides of the append/dedup split. computeContentDigest normalizes
101
- // (strip ANSI, NFC, case-fold, collapse whitespace) then hashes → the same
102
- // key is stored in raw_transcript.content_hash AND used as the dedup link.
103
- const { contentHash } = computeContentDigest(contentBytes);
104
- return {
105
- contentHash,
106
- sessionId,
107
- seq: 0, // assigned by appendRawTranscript (COALESCE(MAX(seq),0)+1)
108
- role: m.role ?? "unknown",
109
- contentBytes,
110
- toolName: m.toolName ?? null,
111
- messageTimestamp: m.timestamp ?? null,
112
- checkpointEpoch: epochId,
113
- // S50: label the row with the turn that produced it (per-turn dedup /
114
- // compression-by-turn metrics). Null when the writer omits it (back-compat).
115
- turnIndex: currentTurn ?? null,
116
- };
117
- }
38
+ import { appendMirrorMessages } from "./mirror-append.js";
39
+ import { stagedForTail, withRecallTail } from "./recall-tail.js";
118
40
 
119
41
  /** Register the context event handler (live-trim auto-trigger). */
120
42
  export function registerContextHandler(
@@ -140,6 +62,16 @@ export function registerContextHandler(
140
62
  const usage = ctx.getContextUsage();
141
63
  const pct = usage?.percent;
142
64
  const messages = event.messages;
65
+ // S53: helper to inject the staged recall/memory block as a user-role
66
+ // tail message at any view-return point. Returns undefined when nothing
67
+ // is staged (or the flag is OFF) so the caller falls through to its
68
+ // normal return. The F3 mirror append (above the gates) runs on the
69
+ // REAL transcript before any view is built, so the injected tail never
70
+ // reaches raw_transcript (PREVENT-PI: append-only, view-only).
71
+ const tailResult = (msgs?: readonly AgentMessage[]) => {
72
+ if (!stagedForTail(runtime, config)) return undefined;
73
+ return { messages: withRecallTail(msgs ?? messages, runtime, config) };
74
+ };
143
75
  // Always track context for the dashboard/widget, even when auto is off.
144
76
  // (v0.8 regression: !config.auto gate sat above this, leaving ctx stats
145
77
  // null -> widget '?% / ?/?' when auto disabled. Track first, THEN gate.)
@@ -162,30 +94,35 @@ export function registerContextHandler(
162
94
  runtime.lastCtxPercent = pct ?? null;
163
95
  runtime.lastCtxWindow = usage?.contextWindow ?? 0;
164
96
  runtime.snapshot(ctx);
165
- if (!config.auto) return;
97
+ if (!config.auto) {
98
+ const tailed = tailResult();
99
+ if (tailed) return tailed;
100
+ return;
101
+ }
166
102
 
167
103
  const view = viewForFallback ?? runtime.engineView(messages);
168
104
 
169
- // S27 DB-mirror: append ALL incoming messages to raw_transcript.
170
- // Runs BEFORE fast-gate so every message is captured, even if we
171
- // don't compact this turn. Append is idempotent (content_hash PK).
172
- if (config.dbMirror) {
173
- try {
174
- const db = openStore(runtime.currentStateDir);
175
- const epochId = epochIdFor(runtime.rt.sessionId);
176
- for (const msg of messages) {
177
- const raw = toRawTranscriptRow(
178
- msg,
105
+ // S27 DB-mirror: append incoming messages to raw_transcript.
106
+ // Runs BEFORE fast-gate so every message is captured, even if we
107
+ // don't compact this turn. Append is idempotent (content_hash PK).
108
+ // F3: high-water mark (mirror-append.ts) skips already-processed
109
+ // messages on subsequent events. On fork/rewind (shorter list or
110
+ // boundary hash mismatch) the mark is dropped, falling back to a
111
+ // full reprocess.
112
+ if (config.dbMirror) {
113
+ try {
114
+ const db = openStore(runtime.currentStateDir);
115
+ appendMirrorMessages(
116
+ db,
117
+ messages,
179
118
  runtime.rt.sessionId,
180
- epochId,
119
+ epochIdFor(runtime.rt.sessionId),
181
120
  runtime.currentTurn,
182
121
  );
183
- if (raw) appendRawTranscript(db, raw);
122
+ } catch (e) {
123
+ runtime.logger.warn("db-mirror-append-fail", { error: String(e) });
184
124
  }
185
- } catch (e) {
186
- runtime.logger.warn("db-mirror-append-fail", { error: String(e) });
187
125
  }
188
- }
189
126
 
190
127
  // S29 FAST GATE: drive the auto-trigger off the context % (the number the
191
128
  // menu bar shows), NOT the token count — the model under-reports tokens,
@@ -205,18 +142,18 @@ export function registerContextHandler(
205
142
  // custom tier OR tiered-but-pct-unavailable → token gate (S27 fallback).
206
143
  if (currentTokens < runtime.effectiveThreshold) {
207
144
  runtime.diagCtxFastGate++;
208
- return;
145
+ return tailResult() ?? undefined;
209
146
  }
210
147
  const check = autoCompactCheck(currentTokens, runtime.effectiveThreshold); // SERVER-STYLE CONFIRM (local)
211
148
  if (!check.shouldCompact) {
212
149
  runtime.diagCtxNoCompact++;
213
- return;
150
+ return tailResult() ?? undefined;
214
151
  }
215
152
  gatePassed = true;
216
153
  }
217
154
  if (!gatePassed) {
218
155
  runtime.diagCtxFastGate++;
219
- return;
156
+ return tailResult() ?? undefined;
220
157
  }
221
158
 
222
159
  // D.2: Replay MUST be exempt from debounce — replay is free (no compute,
@@ -249,9 +186,8 @@ export function registerContextHandler(
249
186
  runtime.snapshot(ctx);
250
187
  // v0.8.7: shallow-copy the cached summary so pi's transformContext can't
251
188
  // mutate the shared reference across replays (audit P3).
252
- return {
253
- messages: [{ ...runtime.trimCache.summaryAgentMsg }, ...recent],
254
- };
189
+ const replayView = [{ ...runtime.trimCache.summaryAgentMsg }, ...recent];
190
+ return tailResult(replayView) ?? { messages: replayView };
255
191
  }
256
192
  // else: context grew enough → fall through to re-compact (cache is stale)
257
193
  }
@@ -261,7 +197,7 @@ export function registerContextHandler(
261
197
  const now = Date.now();
262
198
  if (now < runtime.debounceUntil) {
263
199
  runtime.diagCtxDebounce++;
264
- return;
200
+ return tailResult() ?? undefined;
265
201
  }
266
202
  runtime.debounceUntil = now + 2000;
267
203
 
@@ -290,11 +226,10 @@ export function registerContextHandler(
290
226
  runtime.diagLiveTrimFires++;
291
227
  runtime.diagLiveTrimReplays++;
292
228
  runtime.snapshot(ctx);
293
- return {
294
- messages: [{ ...runtime.trimCache.summaryAgentMsg }, ...recent],
295
- };
229
+ const skipView = [{ ...runtime.trimCache.summaryAgentMsg }, ...recent];
230
+ return tailResult(skipView) ?? { messages: skipView };
296
231
  }
297
- return;
232
+ return tailResult() ?? undefined;
298
233
  }
299
234
 
300
235
  // S27 DB-mirror: write checkpoint_epoch with deterministic nonce.
@@ -460,7 +395,7 @@ export function registerContextHandler(
460
395
  viewLen: view.length,
461
396
  anchorUserMessages,
462
397
  });
463
- return; // unsafe / below anchor floor — no trim this call
398
+ return tailResult() ?? undefined; // unsafe / below anchor floor — no trim this call
464
399
  }
465
400
  const summaryMsg = liveTrimSummaryMessage({
466
401
  compactedFrom: ran.result.compactedFrom,
@@ -515,10 +450,10 @@ export function registerContextHandler(
515
450
  ctxPct: pct,
516
451
  ctxTokens: usage?.tokens ?? null,
517
452
  });
518
- return { messages: [summaryAgentMsg, ...recent] };
453
+ return tailResult([summaryAgentMsg, ...recent]) ?? { messages: [summaryAgentMsg, ...recent] };
519
454
  } catch {
520
455
  runtime.diagCtxThrown++;
521
- return; // non-fatal: no trim this call; the next context event retries
456
+ return tailResult() ?? undefined; // non-fatal: no trim this call; the next context event retries
522
457
  }
523
458
  });
524
459
  }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * mega-events/context-hwm.ts — high-water mark for DB-mirror raw_transcript
3
+ * incremental append (F3 sprint).
4
+ *
5
+ * Stores the last-processed seq + content_hash per session so the context
6
+ * event only appends the tail on subsequent events. On fork/rewind (shorter
7
+ * message list or hash mismatch at the boundary), the mark is dropped so the
8
+ * next event falls back to a full reprocess.
9
+ *
10
+ * Non-fatal: every function logs and swallows errors; the agent loop is never
11
+ * broken (repo invariant).
12
+ *
13
+ * All queries parameterized (PREVENT-002); no `any` (PREVENT-011).
14
+ */
15
+ import type { DatabaseSync } from "node:sqlite";
16
+
17
+ export interface ContextHwm {
18
+ sessionId: string;
19
+ lastSeq: number;
20
+ lastContentHash: string;
21
+ }
22
+
23
+ const TABLE_DDL = `CREATE TABLE IF NOT EXISTS context_hwm (
24
+ session_id TEXT PRIMARY KEY,
25
+ last_seq INTEGER NOT NULL,
26
+ last_content_hash TEXT NOT NULL
27
+ )`;
28
+
29
+ /** Ensure the context_hwm table exists. Idempotent. */
30
+ export function ensureHwmTable(db: DatabaseSync): void {
31
+ try {
32
+ db.exec(TABLE_DDL);
33
+ } catch {
34
+ // non-fatal
35
+ }
36
+ }
37
+
38
+ /** Read the high-water mark for a session. Returns undefined if none. */
39
+ export function readHwm(
40
+ db: DatabaseSync,
41
+ sessionId: string,
42
+ ): ContextHwm | undefined {
43
+ try {
44
+ const row = db
45
+ .prepare(
46
+ "SELECT session_id, last_seq, last_content_hash FROM context_hwm WHERE session_id = ?",
47
+ )
48
+ .get(sessionId) as
49
+ | { session_id: string; last_seq: number; last_content_hash: string }
50
+ | undefined;
51
+ if (!row) return undefined;
52
+ return {
53
+ sessionId: row.session_id,
54
+ lastSeq: Number(row.last_seq),
55
+ lastContentHash: row.last_content_hash,
56
+ };
57
+ } catch {
58
+ return undefined;
59
+ }
60
+ }
61
+
62
+ /** Write (INSERT OR REPLACE) the high-water mark for a session. */
63
+ export function writeHwm(
64
+ db: DatabaseSync,
65
+ hwm: ContextHwm,
66
+ ): void {
67
+ try {
68
+ db.prepare(
69
+ "INSERT OR REPLACE INTO context_hwm (session_id, last_seq, last_content_hash) VALUES (?, ?, ?)",
70
+ ).run(hwm.sessionId, hwm.lastSeq, hwm.lastContentHash);
71
+ } catch {
72
+ // non-fatal
73
+ }
74
+ }
75
+
76
+ /** Drop the high-water mark for a session (invalidation). */
77
+ export function dropHwm(db: DatabaseSync, sessionId: string): void {
78
+ try {
79
+ db.prepare("DELETE FROM context_hwm WHERE session_id = ?").run(sessionId);
80
+ } catch {
81
+ // non-fatal
82
+ }
83
+ }
84
+
85
+ export const TEST_ONLY = { TABLE_DDL };
@@ -102,7 +102,7 @@ export function classifyErrorDetailed(message: unknown): ClassifyErrorDetail {
102
102
  const sr = typeof m.stopReason === 'string' ? m.stopReason : '';
103
103
  if (sr === 'length') return { category: null, signal: 'length-guard' };
104
104
  if (sr === 'aborted') return { category: 'cancelled', signal: 'cancelled' };
105
- if (sr === 'stop' || sr === 'toolUse' || sr === 'tool_use') return { category: null, signal: 'success' };
105
+ if (/^(stop|tool_?use|end_?turn|max_?tokens|complete|finished|done)$/i.test(sr)) return { category: null, signal: 'success' };
106
106
  const usage = m.usage;
107
107
  usagePresent = usage != null && typeof usage === 'object';
108
108
  if (usagePresent) {
@@ -133,9 +133,20 @@ export function classifyErrorDetailed(message: unknown): ClassifyErrorDetail {
133
133
  }
134
134
  }
135
135
  if (!sr) {
136
- const r: ClassifyErrorDetail = { category: 'transient', signal: 'stream-death-no-stopreason' };
137
- if (httpStatus !== undefined) r.httpStatus = httpStatus;
138
- return r;
136
+ // A missing stopReason alone is not sufficient evidence of stream death.
137
+ // Require at least one corroborating signal: an error field, or 0 total tokens
138
+ // (indicating the stream died before producing anything). A normal completion
139
+ // from a runtime that omits stopReason has content and tokens — not an error.
140
+ const hasError = (m.error != null && m.error !== undefined && m.error !== '') ||
141
+ (typeof (m as { errorMessage?: unknown }).errorMessage === 'string' && (m as { errorMessage?: string }).errorMessage !== '');
142
+ if (hasError || (usagePresent && totalTokens === 0)) {
143
+ const r: ClassifyErrorDetail = { category: 'transient', signal: 'stream-death-no-stopreason' };
144
+ if (httpStatus !== undefined) r.httpStatus = httpStatus;
145
+ return r;
146
+ }
147
+ // No stopReason, no error field, tokens present — likely a normal completion
148
+ // from a runtime that omits stopReason (pi-crew, etc.). Not an error.
149
+ return { category: null, signal: 'success' };
139
150
  }
140
151
  text = parts.join(' ');
141
152
  }
@@ -0,0 +1,155 @@
1
+ /**
2
+ * mirror-append.ts — extracted DB-mirror append logic for context-handler.ts
3
+ * (F3 sprint: high-water mark incremental append).
4
+ *
5
+ * Follows the repo's delegate-shell pattern: the context handler shell delegates
6
+ * to this impl module for the mirror-append concern. Returns `rowsProcessed`
7
+ * so callers (including tests) can observe how many new rows were written
8
+ * without adding test-only hooks.
9
+ */
10
+ import type { AgentMessage } from "@earendil-works/pi-agent-core";
11
+ import type { DatabaseSync } from "node:sqlite";
12
+ import { appendRawTranscript, type RawTranscriptRow } from "../../src/store/sqlite.js";
13
+ import { computeContentDigest } from "../../src/dedup/digest.js";
14
+ import {
15
+ ensureHwmTable,
16
+ readHwm,
17
+ writeHwm,
18
+ dropHwm,
19
+ } from "./context-hwm.js";
20
+
21
+ export interface MirrorAppendResult {
22
+ rowsProcessed: number;
23
+ }
24
+
25
+ /**
26
+ * Append incoming messages to raw_transcript with HWM incremental support.
27
+ *
28
+ * On fork/rewind (shorter message list or boundary hash mismatch), the HWM
29
+ * is dropped and all messages are reprocessed.
30
+ */
31
+ export function appendMirrorMessages(
32
+ db: DatabaseSync,
33
+ messages: AgentMessage[],
34
+ sessionId: string,
35
+ epochId: string,
36
+ currentTurn: number | undefined,
37
+ ): MirrorAppendResult {
38
+ ensureHwmTable(db);
39
+ const hwm = readHwm(db, sessionId);
40
+
41
+ let tailStart = 0;
42
+ let needDrop = false;
43
+
44
+ if (hwm) {
45
+ if (messages.length <= hwm.lastSeq) {
46
+ // Session was shortened (fork/rewind/clear) — full reprocess.
47
+ needDrop = true;
48
+ } else {
49
+ // Verify boundary integrity: hash of message at hwm.lastSeq-1
50
+ // must match the stored hash.
51
+ const boundaryMsg = messages[hwm.lastSeq - 1];
52
+ const boundaryHash = contentHashOf(
53
+ (boundaryMsg as { content?: unknown }).content,
54
+ );
55
+ if (boundaryHash === hwm.lastContentHash) {
56
+ tailStart = hwm.lastSeq;
57
+ } else {
58
+ needDrop = true;
59
+ }
60
+ }
61
+ }
62
+
63
+ if (needDrop) {
64
+ dropHwm(db, sessionId);
65
+ }
66
+
67
+ // Process tail (or all if tailStart === 0).
68
+ let rowsProcessed = 0;
69
+ for (let i = tailStart; i < messages.length; i++) {
70
+ const raw = toRawRow(messages[i], sessionId, epochId, currentTurn);
71
+ if (raw) {
72
+ appendRawTranscript(db, raw);
73
+ rowsProcessed++;
74
+ }
75
+ }
76
+
77
+ // Write new HWM.
78
+ if (messages.length > 0) {
79
+ const lastMsg = messages[messages.length - 1];
80
+ writeHwm(db, {
81
+ sessionId,
82
+ lastSeq: messages.length,
83
+ lastContentHash: contentHashOf(
84
+ (lastMsg as { content?: unknown }).content,
85
+ ),
86
+ });
87
+ }
88
+
89
+ return { rowsProcessed };
90
+ }
91
+
92
+ /** Single rule for hashing message content on BOTH sides of the HWM
93
+ * (write + boundary check) — a mismatch here silently defeats the
94
+ * incremental path (every event would full-reprocess). null/undefined → "". */
95
+ function contentHashOf(content: unknown): string {
96
+ if (content == null) return "";
97
+ const bytes =
98
+ typeof content === "string"
99
+ ? content
100
+ : JSON.stringify(canonicalize(content));
101
+ return computeContentDigest(bytes).contentHash;
102
+ }
103
+
104
+ /**
105
+ * Canonicalize a value for deterministic JSON serialization (keys sorted).
106
+ * This is the same logic as the inline function in context-handler.ts.
107
+ */
108
+ function canonicalize(value: unknown): unknown {
109
+ if (value === null || value === undefined) return value;
110
+ if (typeof value !== "object") return value;
111
+ if (Array.isArray(value)) return value.map(canonicalize);
112
+ const sorted = Object.keys(value as Record<string, unknown>).sort();
113
+ const out: Record<string, unknown> = {};
114
+ for (const k of sorted) {
115
+ (out as Record<string, unknown>)[k] = canonicalize(
116
+ (value as Record<string, unknown>)[k],
117
+ );
118
+ }
119
+ return out;
120
+ }
121
+
122
+ /** Convert an AgentMessage to a RawTranscriptRow (same logic as toRawTranscriptRow in context-handler.ts). */
123
+ function toRawRow(
124
+ msg: AgentMessage,
125
+ sessionId: string,
126
+ epochId: string,
127
+ currentTurn: number | undefined,
128
+ ): RawTranscriptRow | null {
129
+ const m = msg as {
130
+ role?: string;
131
+ content?: unknown;
132
+ timestamp?: number;
133
+ toolName?: string;
134
+ };
135
+ const content = m.content;
136
+ if (content == null || content === "") return null;
137
+ const contentBytes =
138
+ typeof content === "string"
139
+ ? content
140
+ : JSON.stringify(canonicalize(content));
141
+ return {
142
+ contentHash: computeContentDigest(contentBytes).contentHash,
143
+ sessionId,
144
+ seq: 0, // assigned by appendRawTranscript
145
+ role: m.role ?? "unknown",
146
+ contentBytes,
147
+ toolName: m.toolName ?? null,
148
+ messageTimestamp: m.timestamp ?? null,
149
+ checkpointEpoch: epochId,
150
+ turnIndex: currentTurn ?? null,
151
+ };
152
+ }
153
+
154
+ // Re-export for convenience when the handler only needs the table ensure.
155
+ export { ensureHwmTable, readHwm, writeHwm, dropHwm };
@@ -39,7 +39,7 @@ export async function maybeSendProviderOutageAdvisory(
39
39
  effectiveCategory: string,
40
40
  runtime: MegaRuntime,
41
41
  pi: ExtensionAPI,
42
- config: { providerOutageAdviseThreshold: number },
42
+ config: { providerOutageAdviseThreshold: number; advisoryChannel?: boolean },
43
43
  detail?: OutageDetail,
44
44
  ): Promise<void> {
45
45
  if (effectiveCategory !== "transient") return;
@@ -66,8 +66,14 @@ export async function maybeSendProviderOutageAdvisory(
66
66
  ...(detail?.rawText !== undefined ? { rawText: detail.rawText } : {}),
67
67
  });
68
68
 
69
- await safeSendUserMessage(
70
- pi,
71
- `[mega-compact] the provider is having issues (${runtime.rt.consecutiveErrors} consecutive failures — timeouts/5xx/rate-limits). Retries are bounded and continue automatically; your context is fine — do NOT clear or reset it. Work resumes as soon as the provider recovers.`,
72
- );
69
+ // Advisory is dashboard-only (events tab + log). No user-visible message —
70
+ // injecting into the conversation scares users and triggers false-positive
71
+ // re-classification of the resulting turn (2026-07-31 incident).
72
+ if (!config.advisoryChannel) {
73
+ // Legacy path (flag OFF): user-role message injection.
74
+ await safeSendUserMessage(
75
+ pi,
76
+ `[mega-compact] the provider is having issues (${runtime.rt.consecutiveErrors} consecutive failures — timeouts/5xx/rate-limits). Retries are bounded and continue automatically; your context is fine — do NOT clear or reset it. Work resumes as soon as the provider recovers.`,
77
+ );
78
+ }
73
79
  }
@@ -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
+ }