@remnic/core 9.62.0 → 9.62.2

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 (512) hide show
  1. package/dist/access-admin-ops-surface.d.ts +11 -11
  2. package/dist/access-admin-ops-surface.js +28 -28
  3. package/dist/access-audit.js +2 -2
  4. package/dist/access-authorization-probe.d.ts +11 -11
  5. package/dist/access-authorization-probe.js +29 -29
  6. package/dist/access-boundary.d.ts +11 -11
  7. package/dist/access-boundary.js +28 -28
  8. package/dist/access-cli.js +93 -91
  9. package/dist/access-cli.js.map +1 -1
  10. package/dist/access-coding-context-resolution.d.ts +1 -1
  11. package/dist/access-extraction-force-flush.d.ts +11 -11
  12. package/dist/access-extraction-force-flush.js +28 -28
  13. package/dist/access-health-types.d.ts +1 -1
  14. package/dist/access-http-lcm-compaction.d.ts +11 -11
  15. package/dist/access-http-lifecycle-flush.d.ts +11 -11
  16. package/dist/access-http-offline-stream.d.ts +11 -11
  17. package/dist/access-http-query.js +28 -28
  18. package/dist/access-http.d.ts +12 -12
  19. package/dist/access-http.js +38 -38
  20. package/dist/access-identity-continuity-surface.d.ts +9 -9
  21. package/dist/access-identity-continuity-surface.js +28 -28
  22. package/dist/access-lcm-surface.d.ts +11 -11
  23. package/dist/access-lcm-surface.js +28 -28
  24. package/dist/access-mcp.d.ts +11 -11
  25. package/dist/access-mcp.js +33 -33
  26. package/dist/access-memory-search-fanout.d.ts +2 -2
  27. package/dist/{access-namespace-preflight-EMvicnrb.d.ts → access-namespace-preflight-yO4LMGFH.d.ts} +1 -1
  28. package/dist/access-namespace-preflight.d.ts +3 -3
  29. package/dist/access-namespace-preflight.js +28 -28
  30. package/dist/access-observe-write-surface.d.ts +11 -11
  31. package/dist/access-observe-write-surface.js +28 -28
  32. package/dist/access-offline-manifest.d.ts +9 -9
  33. package/dist/access-offline-manifest.js +2 -2
  34. package/dist/access-operations-batch.js +29 -29
  35. package/dist/access-operations.d.ts +11 -11
  36. package/dist/access-operations.js +32 -32
  37. package/dist/access-recall-concurrency.d.ts +11 -11
  38. package/dist/access-recall-concurrency.js +28 -28
  39. package/dist/access-recall-response.d.ts +11 -11
  40. package/dist/access-recall-response.js +28 -28
  41. package/dist/access-recall-surface.d.ts +11 -11
  42. package/dist/access-recall-surface.js +28 -28
  43. package/dist/{access-service-BeDYQQ62.d.ts → access-service-M4LgO141.d.ts} +7 -7
  44. package/dist/access-service-helpers.d.ts +11 -11
  45. package/dist/access-service.d.ts +11 -11
  46. package/dist/access-service.js +28 -28
  47. package/dist/access-surface-catalog.d.ts +11 -11
  48. package/dist/access-surface-catalog.js +30 -30
  49. package/dist/access-wearables-meetings-surface.d.ts +3 -3
  50. package/dist/action-confidence.d.ts +1 -1
  51. package/dist/active-memory-bridge.d.ts +1 -1
  52. package/dist/active-memory-bridge.js +2 -2
  53. package/dist/active-recall.d.ts +1 -1
  54. package/dist/active-recall.js +3 -3
  55. package/dist/ambient-provenance.d.ts +1 -1
  56. package/dist/artifact-search.d.ts +1 -1
  57. package/dist/{auto-sync-FMWQQB3J.js → auto-sync-Z52VWMNC.js} +3 -3
  58. package/dist/behavior-learner.d.ts +1 -1
  59. package/dist/behavior-learner.js +2 -2
  60. package/dist/behavior-signals.d.ts +1 -1
  61. package/dist/bootstrap.d.ts +9 -9
  62. package/dist/briefing-window.d.ts +2 -2
  63. package/dist/briefing.d.ts +2 -2
  64. package/dist/briefing.js +13 -12
  65. package/dist/buffer-surprise-report.d.ts +1 -1
  66. package/dist/buffer-turn-helpers.d.ts +1 -1
  67. package/dist/buffer.d.ts +2 -2
  68. package/dist/bulk-import/index.d.ts +3 -3
  69. package/dist/calibration.d.ts +1 -1
  70. package/dist/capabilities.d.ts +1 -1
  71. package/dist/{catalog-B2SxZ6LU.d.ts → catalog-CyYTf_ok.d.ts} +1 -1
  72. package/dist/causal-behavior.d.ts +1 -1
  73. package/dist/causal-behavior.js +3 -3
  74. package/dist/causal-chain.js +3 -3
  75. package/dist/causal-consolidation.d.ts +1 -1
  76. package/dist/causal-consolidation.js +16 -16
  77. package/dist/causal-retrieval.js +3 -3
  78. package/dist/causal-trajectory-graph.d.ts +1 -1
  79. package/dist/causal-trajectory.js +2 -2
  80. package/dist/{chunk-B2QHVLJO.js → chunk-256FBCVX.js} +22 -10
  81. package/dist/chunk-256FBCVX.js.map +1 -0
  82. package/dist/{chunk-QP2IZTUR.js → chunk-2TYIAKU5.js} +2 -2
  83. package/dist/{chunk-HQBVQICL.js → chunk-347ZR6D4.js} +2 -2
  84. package/dist/{chunk-76YNCJ5H.js → chunk-3F3CVVDS.js} +2 -2
  85. package/dist/{chunk-TE4MKFT2.js → chunk-3MA7JVXN.js} +4 -4
  86. package/dist/{chunk-KHQ57QLJ.js → chunk-46WM6JX2.js} +7 -3
  87. package/dist/chunk-46WM6JX2.js.map +1 -0
  88. package/dist/chunk-47DU4SGJ.js +92 -0
  89. package/dist/chunk-47DU4SGJ.js.map +1 -0
  90. package/dist/{chunk-AWRJBAIH.js → chunk-4FFROSFG.js} +37 -14
  91. package/dist/{chunk-AWRJBAIH.js.map → chunk-4FFROSFG.js.map} +1 -1
  92. package/dist/{chunk-N4OYPGZS.js → chunk-4HO3542O.js} +2 -2
  93. package/dist/{chunk-HEEEMJ26.js → chunk-4IZ2IT7D.js} +3 -3
  94. package/dist/{chunk-4XAVQLBR.js → chunk-4Y2RHVCV.js} +2 -2
  95. package/dist/{chunk-33JBK2XP.js → chunk-5F6HMNMZ.js} +2 -2
  96. package/dist/{chunk-5WQMORU5.js → chunk-5PFGMO33.js} +2 -2
  97. package/dist/{chunk-6HEM6HTQ.js → chunk-6GGTATZF.js} +2 -2
  98. package/dist/{chunk-W3Y6FYRK.js → chunk-7BJYTX6Q.js} +2 -2
  99. package/dist/{chunk-2GRLJMKJ.js → chunk-7DRSWPUV.js} +3 -3
  100. package/dist/{chunk-TVVEYCNW.js → chunk-7K5Q6COX.js} +4 -4
  101. package/dist/{chunk-HXXGZ4KV.js → chunk-ALNZEWGN.js} +2 -2
  102. package/dist/{chunk-5WVUO6QT.js → chunk-AR253JBM.js} +1 -1
  103. package/dist/chunk-AR253JBM.js.map +1 -0
  104. package/dist/{chunk-S3B3UIQ3.js → chunk-AYIHDC5S.js} +2 -2
  105. package/dist/{chunk-2RIDW23Y.js → chunk-BBHFAVMP.js} +8 -8
  106. package/dist/{chunk-MJ4UGUR6.js → chunk-BHO6RHE5.js} +3 -3
  107. package/dist/{chunk-URN7XWGC.js → chunk-BP6WMYLP.js} +3 -3
  108. package/dist/{chunk-YUIBXMZY.js → chunk-CALURYWR.js} +284 -249
  109. package/dist/chunk-CALURYWR.js.map +1 -0
  110. package/dist/{chunk-D5HDJWNN.js → chunk-CH627RRV.js} +4 -4
  111. package/dist/{chunk-C3R732UY.js → chunk-CLJ5QS6J.js} +257 -60
  112. package/dist/chunk-CLJ5QS6J.js.map +1 -0
  113. package/dist/{chunk-YLHM5BQQ.js → chunk-DCUMFQYM.js} +171 -8
  114. package/dist/chunk-DCUMFQYM.js.map +1 -0
  115. package/dist/{chunk-ZBJMUXZH.js → chunk-DJ7HMNSL.js} +15 -8
  116. package/dist/chunk-DJ7HMNSL.js.map +1 -0
  117. package/dist/{chunk-XDK6ZFLL.js → chunk-DROAPP6N.js} +3 -3
  118. package/dist/{chunk-MU3TPZOV.js → chunk-DTLF2SLZ.js} +2 -2
  119. package/dist/{chunk-I67HKRBF.js → chunk-EFWHCTYJ.js} +2 -2
  120. package/dist/{chunk-XD4VGLDL.js → chunk-FF6E6HOT.js} +56 -52
  121. package/dist/chunk-FF6E6HOT.js.map +1 -0
  122. package/dist/{chunk-NP7FLVZW.js → chunk-G2FCSILA.js} +2 -2
  123. package/dist/{chunk-3GPBFDXH.js → chunk-H23GIXJC.js} +2 -2
  124. package/dist/{chunk-WT6UL3UA.js → chunk-H7PRHUBD.js} +2 -2
  125. package/dist/{chunk-4JGSG7QN.js → chunk-HSBQJRV2.js} +2 -2
  126. package/dist/{chunk-L33N3BCA.js → chunk-HVKMJAEF.js} +2 -2
  127. package/dist/{chunk-CSOYVTRX.js → chunk-IIPJKCZB.js} +3 -3
  128. package/dist/{chunk-E57E5SCS.js → chunk-IRLH4NM2.js} +3 -3
  129. package/dist/{chunk-H32ANIFP.js → chunk-IZRNVNPV.js} +2 -2
  130. package/dist/{chunk-XBR5BAKW.js → chunk-JCJGYNIS.js} +3 -3
  131. package/dist/{chunk-EIQFIJDM.js → chunk-JDI7G6EA.js} +2 -2
  132. package/dist/{chunk-QWPAQLYV.js → chunk-JFLKSD5V.js} +2 -2
  133. package/dist/{chunk-KQS72GCR.js → chunk-LJOIZPJH.js} +2 -2
  134. package/dist/{chunk-N4EPIKSY.js → chunk-LLGDVG52.js} +3 -3
  135. package/dist/{chunk-7N7DB4L5.js → chunk-LTJTP5YQ.js} +10 -9
  136. package/dist/chunk-LTJTP5YQ.js.map +1 -0
  137. package/dist/{chunk-VOELBUS4.js → chunk-NXH34IPL.js} +2 -2
  138. package/dist/{chunk-XL3TATMJ.js → chunk-OQIRBGUU.js} +21 -12
  139. package/dist/chunk-OQIRBGUU.js.map +1 -0
  140. package/dist/chunk-PJKTFXE2.js +119 -0
  141. package/dist/chunk-PJKTFXE2.js.map +1 -0
  142. package/dist/{chunk-JGW2EUCQ.js → chunk-PMRUMGK6.js} +2 -2
  143. package/dist/{chunk-KAB7TZN3.js → chunk-PSNN25DW.js} +2 -2
  144. package/dist/{chunk-LRGSLV7M.js → chunk-PYHOAXTI.js} +4 -4
  145. package/dist/{chunk-PA5OPZX2.js → chunk-QCI73Z7C.js} +2 -2
  146. package/dist/{chunk-EHMMRZOZ.js → chunk-QICOU4RY.js} +2 -2
  147. package/dist/{chunk-FW73HN33.js → chunk-RFH7U5LT.js} +2 -2
  148. package/dist/{chunk-4SGDYYWV.js → chunk-RPKJHYVE.js} +2 -2
  149. package/dist/{chunk-DSA47WBN.js → chunk-RYST72N5.js} +13 -2
  150. package/dist/chunk-RYST72N5.js.map +1 -0
  151. package/dist/{chunk-L4DLLKPN.js → chunk-SZ4HFZKB.js} +2 -2
  152. package/dist/chunk-T3GIETKV.js +152 -0
  153. package/dist/chunk-T3GIETKV.js.map +1 -0
  154. package/dist/{chunk-MYZ4NA43.js → chunk-TGSMU3F2.js} +3 -3
  155. package/dist/{chunk-CM4SZZBN.js → chunk-TIRJV2LW.js} +2 -2
  156. package/dist/{chunk-EGW4O32Z.js → chunk-UDTME32W.js} +7 -4
  157. package/dist/chunk-UDTME32W.js.map +1 -0
  158. package/dist/{chunk-CIA6ZUBA.js → chunk-WMOJIOIG.js} +2 -2
  159. package/dist/{chunk-W4Q54HM3.js → chunk-WR2YKUIW.js} +2 -2
  160. package/dist/{chunk-VWDTV6SH.js → chunk-X6E7TT3A.js} +2 -2
  161. package/dist/{chunk-D4OCJSLV.js → chunk-YDPAE2RD.js} +2 -2
  162. package/dist/{chunk-G3YBWFUC.js → chunk-YIIC5IDU.js} +35 -18
  163. package/dist/chunk-YIIC5IDU.js.map +1 -0
  164. package/dist/{chunk-QRKYABS6.js → chunk-YSR3DXGC.js} +2 -2
  165. package/dist/{chunk-LBJBNWS2.js → chunk-YTHQHH5U.js} +2 -2
  166. package/dist/{chunk-GN7HO2LA.js → chunk-ZPGHA7EO.js} +12 -12
  167. package/dist/{chunk-VUDALPI5.js → chunk-ZQKXRKMR.js} +19 -5
  168. package/dist/chunk-ZQKXRKMR.js.map +1 -0
  169. package/dist/{chunk-FQSDU634.js → chunk-ZS4JADYY.js} +3 -3
  170. package/dist/{cli-DB_uakTc.d.ts → cli-Bw9z-KqX.d.ts} +5 -5
  171. package/dist/cli.d.ts +13 -13
  172. package/dist/cli.js +62 -62
  173. package/dist/coding/pre-action-gate.d.ts +1 -1
  174. package/dist/coding/pre-action-gate.js +3 -3
  175. package/dist/compounding/engine.d.ts +2 -2
  176. package/dist/compounding/engine.js +13 -12
  177. package/dist/compounding/preference-consolidator.d.ts +1 -1
  178. package/dist/compression-optimizer.d.ts +1 -1
  179. package/dist/config.d.ts +1 -1
  180. package/dist/config.js +3 -3
  181. package/dist/connectors/codex-materialize-runner.d.ts +1 -1
  182. package/dist/connectors/codex-materialize-runner.js +13 -12
  183. package/dist/connectors/codex-materialize.d.ts +1 -1
  184. package/dist/connectors/index.d.ts +1 -1
  185. package/dist/connectors/index.js +13 -12
  186. package/dist/consolidation-provenance-check.d.ts +2 -2
  187. package/dist/consolidation-undo.d.ts +2 -2
  188. package/dist/contradiction/index.d.ts +2 -2
  189. package/dist/converge-config.d.ts +1 -1
  190. package/dist/convergence-refresh.d.ts +2 -2
  191. package/dist/convergence-refresh.js +15 -14
  192. package/dist/conversation-index/backend.d.ts +1 -1
  193. package/dist/conversation-index/chunker.d.ts +1 -1
  194. package/dist/conversation-index/faiss-adapter.d.ts +1 -1
  195. package/dist/conversation-index/indexer.d.ts +1 -1
  196. package/dist/conversation-index/search.d.ts +1 -1
  197. package/dist/corpus-watermark.d.ts +1 -1
  198. package/dist/corpus-watermark.js +15 -14
  199. package/dist/day-summary.d.ts +1 -1
  200. package/dist/delinearize.d.ts +1 -1
  201. package/dist/dependency-propagation-config.d.ts +1 -1
  202. package/dist/{dependency-propagation-delivery-DcOVpK3y.d.ts → dependency-propagation-delivery-BxhvAppj.d.ts} +1 -1
  203. package/dist/direct-answer-wiring.d.ts +1 -1
  204. package/dist/direct-answer-wiring.js +3 -3
  205. package/dist/direct-answer.d.ts +1 -1
  206. package/dist/direct-answer.js +2 -2
  207. package/dist/embedding-fallback.d.ts +1 -1
  208. package/dist/enrichment/index.d.ts +1 -1
  209. package/dist/entity-origin-fields.d.ts +1 -1
  210. package/dist/entity-retrieval.d.ts +2 -2
  211. package/dist/entity-retrieval.js +13 -12
  212. package/dist/entity-schema.d.ts +1 -1
  213. package/dist/episodic-context.d.ts +92 -0
  214. package/dist/episodic-context.js +16 -0
  215. package/dist/episodic-context.js.map +1 -0
  216. package/dist/explicit-capture.d.ts +9 -9
  217. package/dist/external-wiki-access.d.ts +11 -11
  218. package/dist/external-wiki-access.js +29 -29
  219. package/dist/external-wiki-collection-registration.d.ts +1 -1
  220. package/dist/external-wiki-collection.d.ts +1 -1
  221. package/dist/external-wiki-mcp-tools.d.ts +11 -11
  222. package/dist/extraction-error-classification.d.ts +1 -1
  223. package/dist/extraction-faithfulness.d.ts +1 -1
  224. package/dist/extraction-judge-telemetry.d.ts +1 -1
  225. package/dist/extraction-judge-training.d.ts +1 -1
  226. package/dist/extraction-judge.d.ts +1 -1
  227. package/dist/extraction-liveness.d.ts +1 -1
  228. package/dist/extraction-normalization.d.ts +1 -1
  229. package/dist/extraction-normalization.js +2 -2
  230. package/dist/extraction-prompt.d.ts +1 -1
  231. package/dist/extraction-source-grounding-rules.d.ts +1 -1
  232. package/dist/extraction-source-grounding.d.ts +1 -1
  233. package/dist/extraction-source-grounding.js +3 -3
  234. package/dist/extraction.d.ts +1 -1
  235. package/dist/extraction.js +18 -18
  236. package/dist/fallback-llm.d.ts +1 -1
  237. package/dist/{forget-5TWK7CZT.js → forget-SZ2BXDGF.js} +2 -2
  238. package/dist/graph-dashboard-diff.d.ts +1 -1
  239. package/dist/graph-dashboard-key.d.ts +1 -1
  240. package/dist/graph-dashboard-parser.d.ts +1 -1
  241. package/dist/graph-edge-reinforcement.d.ts +1 -1
  242. package/dist/graph-path-reconstruction.d.ts +1 -1
  243. package/dist/graph-path-scoring.d.ts +1 -1
  244. package/dist/graph-snapshot.d.ts +1 -1
  245. package/dist/graph.d.ts +1 -1
  246. package/dist/harmonic-construction.js +2 -2
  247. package/dist/harmonic-retrieval.js +2 -2
  248. package/dist/importance.d.ts +1 -1
  249. package/dist/importers/index.d.ts +1 -1
  250. package/dist/in-flight-reads.d.ts +1 -1
  251. package/dist/index.d.ts +22 -21
  252. package/dist/index.js +118 -103
  253. package/dist/intent.d.ts +1 -1
  254. package/dist/lcm/engine.d.ts +8 -2
  255. package/dist/lcm/engine.js +4 -4
  256. package/dist/lcm/index.d.ts +1 -1
  257. package/dist/lcm/index.js +9 -9
  258. package/dist/lcm/tools.d.ts +1 -1
  259. package/dist/lifecycle.d.ts +1 -1
  260. package/dist/live-connectors-runner.d.ts +1 -1
  261. package/dist/local-llm.d.ts +1 -1
  262. package/dist/local-model-endpoint.d.ts +1 -1
  263. package/dist/maintenance/memory-governance.d.ts +1 -1
  264. package/dist/maintenance/memory-governance.js +13 -12
  265. package/dist/maintenance/rebuild-memory-lifecycle-ledger.d.ts +2 -2
  266. package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +13 -12
  267. package/dist/maintenance/rebuild-memory-projection.d.ts +2 -2
  268. package/dist/maintenance/rebuild-memory-projection.js +14 -13
  269. package/dist/{maintenance-5c1olVtB.d.ts → maintenance-uJb74j64.d.ts} +3 -3
  270. package/dist/mcp-memory-inspector-app.d.ts +11 -11
  271. package/dist/memory-action-policy.d.ts +1 -1
  272. package/dist/memory-cache.d.ts +1 -1
  273. package/dist/memory-lifecycle-ledger-utils.d.ts +1 -1
  274. package/dist/memory-projection-store.d.ts +1 -1
  275. package/dist/memory-provenance.d.ts +1 -1
  276. package/dist/memory-snapshot.d.ts +1 -1
  277. package/dist/memory-worth-outcomes.d.ts +2 -2
  278. package/dist/models-json.d.ts +1 -1
  279. package/dist/namespaces/migrate.d.ts +3 -3
  280. package/dist/namespaces/migrate.js +14 -13
  281. package/dist/namespaces/principal.d.ts +1 -1
  282. package/dist/namespaces/search.d.ts +1 -1
  283. package/dist/namespaces/search.js +6 -5
  284. package/dist/namespaces/storage.d.ts +3 -3
  285. package/dist/namespaces/storage.js +13 -12
  286. package/dist/native-knowledge.d.ts +1 -1
  287. package/dist/offline-sync-impression-drain.d.ts +1 -1
  288. package/dist/operator-doctor-corpus.d.ts +1 -1
  289. package/dist/operator-doctor-corpus.js +16 -15
  290. package/dist/operator-doctor-replica.d.ts +1 -1
  291. package/dist/operator-toolkit.d.ts +3 -3
  292. package/dist/operator-toolkit.js +21 -20
  293. package/dist/orchestration/compression-guideline-coordinator.d.ts +2 -2
  294. package/dist/orchestration/maintenance.d.ts +4 -4
  295. package/dist/orchestration/maintenance.js +18 -17
  296. package/dist/{orchestrator-HPuY6I4i.d.ts → orchestrator-CFi-5JSR.d.ts} +23 -8
  297. package/dist/orchestrator.d.ts +9 -9
  298. package/dist/orchestrator.js +93 -91
  299. package/dist/patterns-cli.d.ts +1 -1
  300. package/dist/{pipeline-CvVe1Q2_.d.ts → pipeline-BdF3ferY.d.ts} +1 -1
  301. package/dist/policy-runtime.d.ts +1 -1
  302. package/dist/policy-runtime.js +3 -3
  303. package/dist/proactive-contention.d.ts +1 -1
  304. package/dist/provenance.d.ts +1 -1
  305. package/dist/provenance.js +2 -2
  306. package/dist/{public-http-D0JKFkyf.d.ts → public-http-DK-QwaBu.d.ts} +1 -1
  307. package/dist/{qmd-Bd0Tp76_.d.ts → qmd-D_62bYNl.d.ts} +1 -1
  308. package/dist/qmd-preflight.d.ts +2 -2
  309. package/dist/qmd-recall-cache.d.ts +1 -1
  310. package/dist/qmd.d.ts +2 -2
  311. package/dist/recall-concurrency-config.d.ts +1 -1
  312. package/dist/recall-disclosure-escalation.d.ts +1 -1
  313. package/dist/recall-explain-renderer.d.ts +1 -1
  314. package/dist/recall-explain-renderer.js +3 -3
  315. package/dist/recall-memory-map.d.ts +1 -1
  316. package/dist/recall-planner-llm.d.ts +1 -1
  317. package/dist/recall-query-policy.js +2 -2
  318. package/dist/recall-state.d.ts +1 -1
  319. package/dist/recall-tag-filter.d.ts +1 -1
  320. package/dist/recall-timings.d.ts +1 -1
  321. package/dist/recall-tokenization.d.ts +3 -1
  322. package/dist/recall-tokenization.js +5 -1
  323. package/dist/recall-xray-cli.d.ts +1 -1
  324. package/dist/recall-xray-cli.js +4 -4
  325. package/dist/recall-xray-renderer.d.ts +1 -1
  326. package/dist/recall-xray-renderer.js +3 -3
  327. package/dist/recall-xray.d.ts +1 -1
  328. package/dist/recall-xray.js +2 -2
  329. package/dist/reconcile/cursor.d.ts +1 -1
  330. package/dist/reconcile/manifest.d.ts +10 -1
  331. package/dist/reconcile/manifest.js +1 -1
  332. package/dist/reconcile/plan.d.ts +1 -1
  333. package/dist/replay/normalizers/chatgpt.d.ts +1 -1
  334. package/dist/replay/normalizers/claude.d.ts +1 -1
  335. package/dist/replay/normalizers/openclaw.d.ts +1 -1
  336. package/dist/replay/normalizers/shared.d.ts +1 -1
  337. package/dist/replay/runner.d.ts +1 -1
  338. package/dist/replay/types.d.ts +1 -1
  339. package/dist/replica-divergence.d.ts +1 -1
  340. package/dist/replica-peers-config.d.ts +1 -1
  341. package/dist/resolve-auth-token.d.ts +1 -1
  342. package/dist/resume-bundles.js +6 -6
  343. package/dist/retrieval-agents.d.ts +2 -2
  344. package/dist/retrieval-tiers.d.ts +1 -1
  345. package/dist/routing/engine.d.ts +1 -1
  346. package/dist/routing/store.d.ts +1 -1
  347. package/dist/salvage-envelope.d.ts +1 -1
  348. package/dist/{scope-profiles-ClLtm9gK.d.ts → scope-profiles-CeMv7pP0.d.ts} +1 -1
  349. package/dist/search/embed-helper.d.ts +1 -1
  350. package/dist/search/factory.d.ts +1 -1
  351. package/dist/search/factory.js +5 -4
  352. package/dist/search/index.d.ts +1 -1
  353. package/dist/search/index.js +9 -8
  354. package/dist/search/lancedb-backend.d.ts +14 -1
  355. package/dist/search/lancedb-backend.js +1 -1
  356. package/dist/search/meilisearch-backend.d.ts +1 -1
  357. package/dist/search/noop-backend.d.ts +1 -1
  358. package/dist/search/orama-backend.d.ts +22 -1
  359. package/dist/search/orama-backend.js +2 -1
  360. package/dist/search/port.d.ts +1 -1
  361. package/dist/search/remote-backend.d.ts +1 -1
  362. package/dist/{semantic-consolidation-B1EkeEDj.d.ts → semantic-consolidation-B-85brYj.d.ts} +1 -1
  363. package/dist/semantic-consolidation.d.ts +2 -2
  364. package/dist/semantic-consolidation.js +14 -14
  365. package/dist/semantic-rule-promotion.js +13 -12
  366. package/dist/semantic-rule-verifier.d.ts +1 -1
  367. package/dist/semantic-rule-verifier.js +13 -13
  368. package/dist/{service-Cf3ANQj9.d.ts → service-CoO0HA2E.d.ts} +2 -2
  369. package/dist/session-observer-bands.d.ts +1 -1
  370. package/dist/session-observer-state.d.ts +1 -1
  371. package/dist/shared-context/manager.d.ts +1 -1
  372. package/dist/signal.d.ts +1 -1
  373. package/dist/source-agent-qualifier.d.ts +1 -1
  374. package/dist/source-agent-qualifier.js +15 -14
  375. package/dist/{storage-DS2O_6Qt.d.ts → storage-BVAVitkO.d.ts} +1 -1
  376. package/dist/storage.d.ts +2 -2
  377. package/dist/storage.js +12 -11
  378. package/dist/summarizer.d.ts +1 -1
  379. package/dist/summary-snapshot.d.ts +1 -1
  380. package/dist/support-passport/index.d.ts +14 -14
  381. package/dist/support-passport/index.js +31 -31
  382. package/dist/temporal-supersession.d.ts +2 -2
  383. package/dist/temporal-timeline-recall.d.ts +1 -1
  384. package/dist/temporal-validity.d.ts +1 -1
  385. package/dist/threading.d.ts +1 -1
  386. package/dist/tier-migration.d.ts +2 -2
  387. package/dist/tier-routing.d.ts +1 -1
  388. package/dist/topics.d.ts +1 -1
  389. package/dist/transcript.d.ts +1 -1
  390. package/dist/transfer/import-sqlite.js +2 -2
  391. package/dist/trust-score-stage.d.ts +1 -1
  392. package/dist/trust-score.d.ts +1 -1
  393. package/dist/trust-zones.js +2 -2
  394. package/dist/{types-BOANGhCJ.d.ts → types-DWHOxlZP.d.ts} +3 -0
  395. package/dist/types.d.ts +1 -1
  396. package/dist/types.js +1 -1
  397. package/dist/user-message-cleaning.d.ts +23 -0
  398. package/dist/user-message-cleaning.js +10 -0
  399. package/dist/user-message-cleaning.js.map +1 -0
  400. package/dist/utility-runtime.d.ts +1 -1
  401. package/dist/verified-recall.js +13 -13
  402. package/dist/whitespace.d.ts +26 -1
  403. package/dist/whitespace.js +11 -3
  404. package/dist/work-product-ledger.js +2 -2
  405. package/dist/write-envelope.d.ts +1 -1
  406. package/package.json +12 -2
  407. package/src/access-service-offline-file-content.test.ts +1 -0
  408. package/src/cli.ts +14 -13
  409. package/src/config.ts +11 -0
  410. package/src/entity-retrieval.ts +4 -7
  411. package/src/episodic-context-recall.integration.test.ts +169 -0
  412. package/src/episodic-context.test.ts +266 -0
  413. package/src/episodic-context.ts +274 -0
  414. package/src/harmonic-construction.test.ts +187 -2
  415. package/src/harmonic-retrieval.ts +54 -32
  416. package/src/index.ts +3 -4
  417. package/src/lcm/engine.ts +18 -1
  418. package/src/lifecycle/tombstones.test.ts +134 -0
  419. package/src/lifecycle/tombstones.ts +29 -2
  420. package/src/orchestration/episodic-context-section.ts +255 -0
  421. package/src/orchestration/recall-internal-deps.ts +2 -0
  422. package/src/orchestration/recall-internal.ts +40 -25
  423. package/src/orchestration/recall-result-formatter.ts +37 -0
  424. package/src/orchestration/recall-section-coordinator.ts +20 -14
  425. package/src/recall-tokenization.ts +14 -8
  426. package/src/reconcile/manifest.test.ts +133 -4
  427. package/src/reconcile/manifest.ts +43 -7
  428. package/src/search/factory.ts +1 -0
  429. package/src/search/lancedb-backend.ts +24 -16
  430. package/src/search/orama-backend.test.ts +208 -1
  431. package/src/search/orama-backend.ts +133 -18
  432. package/src/search/orama-cjk-tokenizer.test.ts +108 -0
  433. package/src/search/orama-cjk-tokenizer.ts +143 -0
  434. package/src/storage/tombstone-migration-sources.test.ts +116 -0
  435. package/src/storage/tombstone-migration-sources.ts +90 -0
  436. package/src/storage.ts +13 -34
  437. package/src/types.ts +3 -3
  438. package/src/user-message-cleaning.ts +139 -0
  439. package/src/whitespace.test.ts +178 -0
  440. package/src/whitespace.ts +119 -0
  441. package/dist/chunk-5WVUO6QT.js.map +0 -1
  442. package/dist/chunk-6MKAMLQL.js +0 -16
  443. package/dist/chunk-6MKAMLQL.js.map +0 -1
  444. package/dist/chunk-7N7DB4L5.js.map +0 -1
  445. package/dist/chunk-B2QHVLJO.js.map +0 -1
  446. package/dist/chunk-C3R732UY.js.map +0 -1
  447. package/dist/chunk-DSA47WBN.js.map +0 -1
  448. package/dist/chunk-EGW4O32Z.js.map +0 -1
  449. package/dist/chunk-G3YBWFUC.js.map +0 -1
  450. package/dist/chunk-KHQ57QLJ.js.map +0 -1
  451. package/dist/chunk-VUDALPI5.js.map +0 -1
  452. package/dist/chunk-XD4VGLDL.js.map +0 -1
  453. package/dist/chunk-XL3TATMJ.js.map +0 -1
  454. package/dist/chunk-YLHM5BQQ.js.map +0 -1
  455. package/dist/chunk-YUIBXMZY.js.map +0 -1
  456. package/dist/chunk-ZBJMUXZH.js.map +0 -1
  457. /package/dist/{auto-sync-FMWQQB3J.js.map → auto-sync-Z52VWMNC.js.map} +0 -0
  458. /package/dist/{chunk-QP2IZTUR.js.map → chunk-2TYIAKU5.js.map} +0 -0
  459. /package/dist/{chunk-HQBVQICL.js.map → chunk-347ZR6D4.js.map} +0 -0
  460. /package/dist/{chunk-76YNCJ5H.js.map → chunk-3F3CVVDS.js.map} +0 -0
  461. /package/dist/{chunk-TE4MKFT2.js.map → chunk-3MA7JVXN.js.map} +0 -0
  462. /package/dist/{chunk-N4OYPGZS.js.map → chunk-4HO3542O.js.map} +0 -0
  463. /package/dist/{chunk-HEEEMJ26.js.map → chunk-4IZ2IT7D.js.map} +0 -0
  464. /package/dist/{chunk-4XAVQLBR.js.map → chunk-4Y2RHVCV.js.map} +0 -0
  465. /package/dist/{chunk-33JBK2XP.js.map → chunk-5F6HMNMZ.js.map} +0 -0
  466. /package/dist/{chunk-5WQMORU5.js.map → chunk-5PFGMO33.js.map} +0 -0
  467. /package/dist/{chunk-6HEM6HTQ.js.map → chunk-6GGTATZF.js.map} +0 -0
  468. /package/dist/{chunk-W3Y6FYRK.js.map → chunk-7BJYTX6Q.js.map} +0 -0
  469. /package/dist/{chunk-2GRLJMKJ.js.map → chunk-7DRSWPUV.js.map} +0 -0
  470. /package/dist/{chunk-TVVEYCNW.js.map → chunk-7K5Q6COX.js.map} +0 -0
  471. /package/dist/{chunk-HXXGZ4KV.js.map → chunk-ALNZEWGN.js.map} +0 -0
  472. /package/dist/{chunk-S3B3UIQ3.js.map → chunk-AYIHDC5S.js.map} +0 -0
  473. /package/dist/{chunk-2RIDW23Y.js.map → chunk-BBHFAVMP.js.map} +0 -0
  474. /package/dist/{chunk-MJ4UGUR6.js.map → chunk-BHO6RHE5.js.map} +0 -0
  475. /package/dist/{chunk-URN7XWGC.js.map → chunk-BP6WMYLP.js.map} +0 -0
  476. /package/dist/{chunk-D5HDJWNN.js.map → chunk-CH627RRV.js.map} +0 -0
  477. /package/dist/{chunk-XDK6ZFLL.js.map → chunk-DROAPP6N.js.map} +0 -0
  478. /package/dist/{chunk-MU3TPZOV.js.map → chunk-DTLF2SLZ.js.map} +0 -0
  479. /package/dist/{chunk-I67HKRBF.js.map → chunk-EFWHCTYJ.js.map} +0 -0
  480. /package/dist/{chunk-NP7FLVZW.js.map → chunk-G2FCSILA.js.map} +0 -0
  481. /package/dist/{chunk-3GPBFDXH.js.map → chunk-H23GIXJC.js.map} +0 -0
  482. /package/dist/{chunk-WT6UL3UA.js.map → chunk-H7PRHUBD.js.map} +0 -0
  483. /package/dist/{chunk-4JGSG7QN.js.map → chunk-HSBQJRV2.js.map} +0 -0
  484. /package/dist/{chunk-L33N3BCA.js.map → chunk-HVKMJAEF.js.map} +0 -0
  485. /package/dist/{chunk-CSOYVTRX.js.map → chunk-IIPJKCZB.js.map} +0 -0
  486. /package/dist/{chunk-E57E5SCS.js.map → chunk-IRLH4NM2.js.map} +0 -0
  487. /package/dist/{chunk-H32ANIFP.js.map → chunk-IZRNVNPV.js.map} +0 -0
  488. /package/dist/{chunk-XBR5BAKW.js.map → chunk-JCJGYNIS.js.map} +0 -0
  489. /package/dist/{chunk-EIQFIJDM.js.map → chunk-JDI7G6EA.js.map} +0 -0
  490. /package/dist/{chunk-QWPAQLYV.js.map → chunk-JFLKSD5V.js.map} +0 -0
  491. /package/dist/{chunk-KQS72GCR.js.map → chunk-LJOIZPJH.js.map} +0 -0
  492. /package/dist/{chunk-N4EPIKSY.js.map → chunk-LLGDVG52.js.map} +0 -0
  493. /package/dist/{chunk-VOELBUS4.js.map → chunk-NXH34IPL.js.map} +0 -0
  494. /package/dist/{chunk-JGW2EUCQ.js.map → chunk-PMRUMGK6.js.map} +0 -0
  495. /package/dist/{chunk-KAB7TZN3.js.map → chunk-PSNN25DW.js.map} +0 -0
  496. /package/dist/{chunk-LRGSLV7M.js.map → chunk-PYHOAXTI.js.map} +0 -0
  497. /package/dist/{chunk-PA5OPZX2.js.map → chunk-QCI73Z7C.js.map} +0 -0
  498. /package/dist/{chunk-EHMMRZOZ.js.map → chunk-QICOU4RY.js.map} +0 -0
  499. /package/dist/{chunk-FW73HN33.js.map → chunk-RFH7U5LT.js.map} +0 -0
  500. /package/dist/{chunk-4SGDYYWV.js.map → chunk-RPKJHYVE.js.map} +0 -0
  501. /package/dist/{chunk-L4DLLKPN.js.map → chunk-SZ4HFZKB.js.map} +0 -0
  502. /package/dist/{chunk-MYZ4NA43.js.map → chunk-TGSMU3F2.js.map} +0 -0
  503. /package/dist/{chunk-CM4SZZBN.js.map → chunk-TIRJV2LW.js.map} +0 -0
  504. /package/dist/{chunk-CIA6ZUBA.js.map → chunk-WMOJIOIG.js.map} +0 -0
  505. /package/dist/{chunk-W4Q54HM3.js.map → chunk-WR2YKUIW.js.map} +0 -0
  506. /package/dist/{chunk-VWDTV6SH.js.map → chunk-X6E7TT3A.js.map} +0 -0
  507. /package/dist/{chunk-D4OCJSLV.js.map → chunk-YDPAE2RD.js.map} +0 -0
  508. /package/dist/{chunk-QRKYABS6.js.map → chunk-YSR3DXGC.js.map} +0 -0
  509. /package/dist/{chunk-LBJBNWS2.js.map → chunk-YTHQHH5U.js.map} +0 -0
  510. /package/dist/{chunk-GN7HO2LA.js.map → chunk-ZPGHA7EO.js.map} +0 -0
  511. /package/dist/{chunk-FQSDU634.js.map → chunk-ZS4JADYY.js.map} +0 -0
  512. /package/dist/{forget-5TWK7CZT.js.map → forget-SZ2BXDGF.js.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lifecycle/tombstones.ts"],"sourcesContent":["// ---------------------------------------------------------------------------\n// lifecycle/tombstones.ts — Tombstone store + non-resurrection invariant\n// (issue #1579)\n//\n// A \"tombstone\" records that a fact has been retired (corrected, superseded,\n// or retracted) so that the SAME fact cannot silently come back to life through\n// any of the five resurrection paths:\n//\n// 1. Re-extraction (session replay, re-observation)\n// 2. Importers (capsule / import-* payloads)\n// 3. Consolidation merges\n// 4. Dreams (REM re-derivation)\n// 5. Pattern reinforcement (duplicate promotion)\n//\n// The invariant is enforced at the SINGLE storage persist path\n// (`StorageManager.writeMemory` — the same chokepoint that records catalog\n// writes, issue #1522). Every write path funnels through it, so paths (a)–(e)\n// are blocked WITHOUT per-path code.\n//\n// File-first / rebuildable (the repo's storage philosophy): the JSONL at\n// `<stateDir>/tombstones.jsonl` is a cache of truth. The authoritative sources\n// are the memory files themselves (status: superseded/retracted) and the\n// `corrections/` records. `rebuildTombstonesFromStorage` reconstructs the\n// JSONL from those sources.\n//\n// Design rules honored (issue #1579 design section):\n// - Hash `rawContent` exactly as the dedup index does — one helper\n// (`ContentHashIndex.computeHash` / `normalizeContent`), never a second\n// (checklist §13; rule 23).\n// - Append-only log — never rewrite history; a later `kind: \"revocation\"`\n// entry re-allows (rule 25).\n// - Instance-scoped, never module-level (rule 11).\n// - Namespace-scoped — a tombstone in namespace A never blocks namespace B\n// (rule 42).\n// - Serialized appends with rejection recovery (rule 40).\n// - Never silent drop — a blocked write lands as pending_review + blockedBy\n// (rule 34).\n// - Do NOT register a blocked fact as an active dedup/index entry (rule 44).\n// - Off = pre-feature behavior for rollback safety (rule 30).\n// - Semantic tier (4) ships dark, off by default (rule 48).\n// ---------------------------------------------------------------------------\n\nimport { serializeMutations, withHeldFileLock } from \"../utils/serialize-mutations.js\";\nimport {\n computeLegacyContentHash,\n isUnambiguousLegacyContentHash,\n normalizeLegacyContent,\n} from \"../content-hash.js\";\n\n/** Why a tombstone was emitted. */\nexport type TombstoneReason =\n | \"correction\"\n | \"supersession\"\n | \"retraction\"\n | \"contradiction_resolution\";\n\n/** Who emitted the tombstone. */\nexport type TombstoneCreatedBy =\n | \"user_correction\"\n | \"contradiction_resolution\"\n | \"supersession\"\n | \"chat\";\n\n/** Tier that matched on lookup. */\nexport type TombstoneMatchTier = \"exact\" | \"normalized\" | \"keyed\" | \"semantic\";\n\n/**\n * A single append-only tombstone log entry.\n *\n * `kind: \"tombstone\"` blocks; `kind: \"revocation\"` re-allows. The log is\n * never rewritten — the NEWEST matching entry wins at lookup (rule 25).\n */\nexport interface TombstoneEntry {\n /** Stable tombstone id (`tomb-<ts>-<rand>`). */\n id: string;\n kind: \"tombstone\" | \"revocation\";\n reason: TombstoneReason;\n /** The memory that was retired. */\n sourceMemoryId: string;\n /** sha256 of the retired identity; explicit contentHashSource may define it. */\n contentHash: string;\n /** Current body hash retained alongside an ambiguous or explicit primary hash. */\n currentContentHashAlias?: string;\n /** Current normalized body used by the normalized lookup tier. */\n normalizedText: string;\n /** Version of the current body identity fields. */\n normalizerVersion?: number;\n entityRef?: string;\n /** Structured-attribute supersession key when one existed. */\n supersessionKey?: string;\n /** Stable operation identity for crash-recovery replay. */\n operationKey?: string;\n /** Namespace scope (rule 42). */\n namespace: string;\n createdAt: string;\n createdBy: TombstoneCreatedBy;\n /** For `kind: \"revocation\"`: the tombstone id being revoked. */\n revokes?: string;\n}\n\n/** A positive block decision returned by `TombstoneStore.lookup`. */\nexport interface TombstoneMatch {\n tombstoneId: string;\n matchedTier: TombstoneMatchTier;\n reason: TombstoneReason;\n}\n\n/** Inputs to a lookup. At least one discriminator must be present.\n *\n * Issue #1579 thread Ociag/Oci-W: `supersessionKeys` (plural) lets the write\n * chokepoint check EVERY derived key, not just the first. Emitters register\n * one tombstone per matched key (temporal-supersession, rebuild), so a block\n * can live on any later key; querying only `supersessionKeys[0]` missed it\n * and the retired fact resurrected as active. `supersessionKey` (singular)\n * remains for direct/unit callers; `lookup` checks the union of both. */\nexport interface TombstoneLookupQuery {\n contentHash?: string;\n normalizedText?: string;\n entityRef?: string;\n /** Single supersession key (direct/unit callers). */\n supersessionKey?: string;\n /** All derived supersession keys (write chokepoint). The keyed tier is\n * checked for each; the first active match wins. */\n supersessionKeys?: string[];\n namespace: string;\n}\n\n/** Configuration for a tombstone store instance. */\nexport interface TombstoneStoreOptions {\n enabled: boolean;\n semanticMatch: boolean;\n semanticThreshold: number;\n /** sha256 of raw content — wired to `ContentHashIndex.computeHash`. */\n hashContent: (raw: string) => string;\n /** Normalize raw content — wired to `ContentHashIndex.normalizeContent`. */\n normalizeText: (raw: string) => string;\n /**\n * Optional cosine similarity in [0, 1] for the semantic tier. When\n * undefined or when `semanticMatch` is false, the semantic tier is\n * skipped entirely.\n */\n semanticSimilarity?: (a: string, b: string) => number;\n /**\n * Resolve source identity candidates for bounded legacy sourceMemoryIds.\n * StorageManager reads the corpus before the tombstone write lock.\n */\n readonly sourceContentsForMemoryIds?: (\n sourceMemoryIds: readonly string[],\n ) => Promise<ReadonlyMap<string, string | readonly string[]>>;\n /** Maximum number of legacy entries considered during one load. */\n readonly legacyMigrationLimit?: number;\n /**\n * Cross-process write-lock timings for the tombstone JSONL mutation lock\n * (issue #1639). The secure-store append is a read-merge-write (read\n * encrypted → decrypt → concat → re-encrypt → atomic rename), so two\n * Remnic processes appending concurrently can each read the same contents\n * and the last writer drops the other's entry — breaking the\n * non-resurrection invariant. The lock serializes the mutation across\n * processes. When omitted, defaults that fit the short tombstone critical\n * section (mirroring the summary-snapshot lock): staleMs 30s, maxWaitMs 5s,\n * heartbeatMs floor(staleMs/3).\n */\n readonly lockStaleMs?: number;\n readonly lockMaxWaitMs?: number;\n readonly lockHeartbeatMs?: number;\n}\n\n/** Injected file I/O — the StorageManager wires its secure-store-aware\n * implementations so tombstones are encrypted at rest alongside other state.\n * `stat` is optional; when provided the store tracks the file mtime itself and\n * runs a cross-process staleness probe on each access (#1579). */\nexport interface TombstoneFileIo {\n read: (filePath: string) => Promise<string>;\n append: (filePath: string, content: string) => Promise<void>;\n write: (filePath: string, content: string) => Promise<void>;\n stat?: (filePath: string) => { mtimeMs: number };\n}\n\n/** Aggregate stats for `remnic doctor`. */\nexport interface TombstoneStats {\n count: number;\n revoked: number;\n lastAppendAt: string | null;\n corruptedLines: number;\n /** Whether the in-memory index matches the on-disk file (rebuild check). */\n loaded: boolean;\n}\n\nconst TOMBSTONE_NORMALIZER_VERSION = 2;\nconst DEFAULT_LEGACY_MIGRATION_LIMIT = 10_000;\n\nconst TOMBSTONE_PREFIX = \"tomb\";\n\nfunction newTombstoneId(): string {\n return `${TOMBSTONE_PREFIX}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;\n}\n\n/** Deterministic key for the keyed tier (namespace + entityRef + supersessionKey).\n *\n * The namespace is part of the discriminator so two namespace-scoped stores\n * that share the same backing `tombstones.jsonl` (namespaces disabled, or the\n * same directory used with different namespace configs) cannot overwrite each\n * other's index entries — a later tombstone for namespace B with the same\n * entity/key no longer evicts namespace A's entry (issue #1579 thread Ocs-O). */\nfunction keyedTierKey(namespace: string, entityRef: string, supersessionKey: string): string {\n return `${namespace}\\0${entityRef}\\0${supersessionKey}`;\n}\n\n/**\n * Parse one JSONL line into a TombstoneEntry, validating the discriminated\n * union. Returns `null` for malformed lines (rule 34 — skip with a counter,\n * never crash).\n */\nexport function parseTombstoneLine(line: string): TombstoneEntry | null {\n const trimmed = line.trim();\n if (trimmed.length === 0) return null;\n let parsed: unknown;\n try {\n parsed = JSON.parse(trimmed);\n } catch {\n return null;\n }\n if (!parsed || typeof parsed !== \"object\") return null;\n const e = parsed as Record<string, unknown>;\n if (typeof e.id !== \"string\" || e.id.length === 0) return null;\n if (e.kind !== \"tombstone\" && e.kind !== \"revocation\") return null;\n if (\n e.reason !== \"correction\" &&\n e.reason !== \"supersession\" &&\n e.reason !== \"retraction\" &&\n e.reason !== \"contradiction_resolution\"\n ) {\n return null;\n }\n if (typeof e.sourceMemoryId !== \"string\") return null;\n if (typeof e.contentHash !== \"string\") return null;\n if (typeof e.normalizedText !== \"string\") return null;\n if (typeof e.namespace !== \"string\") return null;\n if (typeof e.createdAt !== \"string\") return null;\n if (\n e.createdBy !== \"user_correction\" &&\n e.createdBy !== \"contradiction_resolution\" &&\n e.createdBy !== \"supersession\" &&\n e.createdBy !== \"chat\"\n ) {\n return null;\n }\n const out: TombstoneEntry = {\n id: e.id,\n kind: e.kind,\n reason: e.reason,\n sourceMemoryId: e.sourceMemoryId,\n contentHash: e.contentHash,\n normalizedText: e.normalizedText,\n namespace: e.namespace,\n createdAt: e.createdAt,\n createdBy: e.createdBy,\n };\n if (typeof e.currentContentHashAlias === \"string\") {\n out.currentContentHashAlias = e.currentContentHashAlias;\n }\n if (typeof e.normalizerVersion === \"number\" && Number.isInteger(e.normalizerVersion)) {\n out.normalizerVersion = e.normalizerVersion;\n }\n if (typeof e.operationKey === \"string\") out.operationKey = e.operationKey;\n if (typeof e.entityRef === \"string\") out.entityRef = e.entityRef;\n if (typeof e.supersessionKey === \"string\") out.supersessionKey = e.supersessionKey;\n if (typeof e.revokes === \"string\") out.revokes = e.revokes;\n return out;\n}\n\n/**\n * The instance-scoped tombstone store. One per StorageManager (rule 11),\n * namespace-scoped via `namespace` + the on-disk path (rule 42).\n *\n * The in-memory index is built lazily on first access and invalidated on\n * append + by the StorageManager's cache-invalidation chokepoint (rule 25).\n */\nexport class TombstoneStore {\n private entries: TombstoneEntry[] = [];\n /** contentHash → tombstone id (newest) — exact tier. */\n private readonly byHash = new Map<string, string>();\n /** normalizedText → tombstone id (newest) — normalized tier. */\n private readonly byNormalized = new Map<string, string>();\n /** entityRef\\0supersessionKey → tombstone id (newest) — keyed tier. */\n private readonly byKey = new Map<string, string>();\n /** Tombstone ids that have a later revocation entry. */\n private readonly revokedIds = new Set<string>();\n /** id → entry index (newest only, for revocation lookups). */\n private readonly byId = new Map<string, TombstoneEntry>();\n private loaded = false;\n private loadPromise: Promise<void> | null = null;\n private corruptedLines = 0;\n private lastAppendAt: string | null = null;\n /**\n * Last-seen mtime of the JSONL file (cross-process staleness probe, #1579).\n * Tracked inside the store so the StorageManager wiring stays thin. When the\n * file's mtime advances past this value between accesses, the in-memory index\n * is stale (a peer process appended) and is reloaded before the next lookup.\n */\n private fileMtimeMs = 0;\n\n constructor(\n private readonly filePath: string,\n private readonly namespace: string,\n private readonly options: TombstoneStoreOptions,\n private readonly io: TombstoneFileIo,\n ) {\n const limit = this.options.legacyMigrationLimit;\n if (limit !== undefined && (!Number.isFinite(limit) || !Number.isInteger(limit) || limit < 0)) {\n throw new Error(\"legacyMigrationLimit must be a finite non-negative integer\");\n }\n this.lockStaleMs = this.options.lockStaleMs ?? 30_000;\n this.lockMaxWaitMs = this.options.lockMaxWaitMs ?? 5_000;\n this.lockHeartbeatMs =\n this.options.lockHeartbeatMs ?? Math.max(100, Math.floor(this.lockStaleMs / 3));\n }\n\n /**\n * Resolved cross-process write-lock timings (issue #1639). Defaults mirror\n * the summary-snapshot lock: a tombstone append critical section is\n * sub-second, so a stale lock older than 30s is a crashed holder; bounded\n * acquisition gives up after 5s and strict-fails rather than clobbering a\n * concurrent writer (which would reintroduce the lost-write race this lock\n * closes).\n */\n private readonly lockStaleMs: number;\n private readonly lockMaxWaitMs: number;\n private readonly lockHeartbeatMs: number;\n\n /**\n * Sibling advisory lockfile: `<tombstones>.jsonl` → `<tombstones>.lock`.\n * Shared by append, revoke, and rebuild so every JSONL mutation serializes\n * against the others across processes (issue #1639: \"shared with rebuilds\").\n */\n private get lockPath(): string {\n return this.filePath.replace(/\\.jsonl$/i, \"\") + \".lock\";\n }\n\n /** Lazy load + build the in-memory index. Idempotent. */\n async load(): Promise<void> {\n if (this.loaded) return;\n if (this.loadPromise) return this.loadPromise;\n this.loadPromise = this.loadInternal();\n try {\n await this.loadPromise;\n } finally {\n this.loadPromise = null;\n }\n }\n\n private async loadInternal(migrateLegacy = true): Promise<void> {\n // Record the file mtime before reading so an ENOENT still records 0 and\n // a successful load does not immediately trigger a staleness reload.\n this.recordFileMtime();\n let raw: string;\n try {\n raw = await this.io.read(this.filePath);\n } catch (err) {\n const code = (err as NodeJS.ErrnoException)?.code;\n if (code !== \"ENOENT\") throw err;\n this.loaded = true;\n return;\n }\n const initial = this.parseEntries(raw);\n const migrated = migrateLegacy\n ? await this.migrateLegacyEntries(initial)\n : initial;\n this.resetIndex();\n for (const entry of migrated.entries) this.indexEntry(entry);\n this.corruptedLines = migrated.corruptedLines;\n this.loaded = true;\n }\n\n private parseEntries(raw: string): {\n entries: TombstoneEntry[];\n corruptedLines: number;\n } {\n const entries: TombstoneEntry[] = [];\n let corruptedLines = 0;\n for (const line of raw.split(\"\\n\")) {\n const entry = parseTombstoneLine(line);\n if (!entry) {\n if (line.trim().length > 0) corruptedLines += 1;\n continue;\n }\n entries.push(entry);\n }\n return { entries, corruptedLines };\n }\n\n /**\n * Re-index pre-Unicode records from retired source content.\n * Source reads stay bounded, but every batch finishes before the store\n * publishes its in-memory index. Missing sources remain eligible on restart.\n */\n private async migrateLegacyEntries(initial: {\n entries: TombstoneEntry[];\n corruptedLines: number;\n }): Promise<{ entries: TombstoneEntry[]; corruptedLines: number }> {\n const limit = this.options.legacyMigrationLimit ?? DEFAULT_LEGACY_MIGRATION_LIMIT;\n if (limit === 0 || !this.options.sourceContentsForMemoryIds) return initial;\n const sourceMemoryIds: string[] = [];\n const requested = new Set<string>();\n for (const entry of initial.entries) {\n if (\n entry.kind !== \"tombstone\" ||\n entry.normalizerVersion === TOMBSTONE_NORMALIZER_VERSION\n ) {\n continue;\n }\n if (requested.has(entry.sourceMemoryId)) continue;\n requested.add(entry.sourceMemoryId);\n sourceMemoryIds.push(entry.sourceMemoryId);\n }\n if (sourceMemoryIds.length === 0) return initial;\n const sourceContents = new Map<string, string | readonly string[]>();\n for (let offset = 0; offset < sourceMemoryIds.length; offset += limit) {\n const batch = sourceMemoryIds.slice(offset, offset + limit);\n const fetched = await this.options.sourceContentsForMemoryIds(batch);\n for (const [sourceMemoryId, content] of fetched) sourceContents.set(sourceMemoryId, content);\n }\n return await serializeMutations(`tombstone:${this.filePath}`, () =>\n this.withWriteLock(async () => {\n const latestRaw = await this.io.read(this.filePath);\n const latest = this.parseEntries(latestRaw);\n let changed = false;\n const migrateEntry = (entry: TombstoneEntry): TombstoneEntry => {\n if (\n entry.kind !== \"tombstone\" ||\n entry.normalizerVersion === TOMBSTONE_NORMALIZER_VERSION ||\n !requested.has(entry.sourceMemoryId)\n ) {\n return entry;\n }\n const resolved = sourceContents.get(entry.sourceMemoryId);\n if (resolved === undefined) return entry;\n const candidates = typeof resolved === \"string\" ? [resolved] : resolved;\n const matches = candidates.filter((source) =>\n entry.contentHash === this.options.hashContent(source) ||\n entry.contentHash === computeLegacyContentHash(source)\n );\n const explicitPrimary =\n matches.length === 0 &&\n entry.contentHash !== this.options.hashContent(entry.normalizedText);\n const source = matches[0] ?? (explicitPrimary ? candidates[0] : undefined);\n if (source === undefined) return entry;\n const currentHash = this.options.hashContent(source);\n const alias = explicitPrimary\n ? currentHash\n : matches\n .map((candidate) => this.options.hashContent(candidate))\n .find((hash) => hash !== currentHash);\n changed = true;\n return {\n ...entry,\n contentHash: explicitPrimary ? entry.contentHash : currentHash,\n ...(alias ? { currentContentHashAlias: alias } : {}),\n normalizedText: this.options.normalizeText(source),\n normalizerVersion: TOMBSTONE_NORMALIZER_VERSION,\n };\n };\n const serialized = latestRaw\n .split(\"\\n\")\n .map((line) => {\n const entry = parseTombstoneLine(line);\n if (!entry) return line;\n const migrated = migrateEntry(entry);\n return migrated === entry ? line : JSON.stringify(migrated);\n })\n .join(\"\\n\");\n if (!changed) return latest;\n await this.io.write(this.filePath, serialized);\n this.markWritten();\n return this.parseEntries(serialized);\n }),\n );\n }\n\n private resetIndex(): void {\n this.entries = [];\n this.byHash.clear();\n this.byNormalized.clear();\n this.byKey.clear();\n this.revokedIds.clear();\n this.byId.clear();\n }\n\n /**\n * Index an entry. Later entries (higher array index = newer createdAt)\n * OVERRIDE earlier ones at each tier key — the newest matching entry wins\n * (rule 25). Revocation entries mark their target as revoked.\n */\n private indexEntry(entry: TombstoneEntry): void {\n this.entries.push(entry);\n this.byId.set(entry.id, entry);\n if (entry.createdAt > (this.lastAppendAt ?? \"\")) {\n this.lastAppendAt = entry.createdAt;\n }\n if (entry.kind === \"revocation\") {\n if (entry.revokes) this.revokedIds.add(entry.revokes);\n return;\n }\n // Tombstone entries populate the lookup maps. The namespace is part of\n // every discriminator key (issue #1579 thread Ocs-O): when two\n // namespace-scoped stores share the same backing file, a later tombstone\n // for namespace B with identical content must NOT evict namespace A's map\n // entry — otherwise A's lookup finds B's id, rejects it on namespace\n // mismatch, and misses its own still-active tombstone (resurrection).\n const ns = entry.namespace;\n if (entry.normalizerVersion === TOMBSTONE_NORMALIZER_VERSION) {\n if (entry.contentHash) this.byHash.set(`${ns}\\0${entry.contentHash}`, entry.id);\n if (entry.currentContentHashAlias) {\n this.byHash.set(`${ns}\\0${entry.currentContentHashAlias}`, entry.id);\n }\n if (entry.normalizedText) this.byNormalized.set(`${ns}\\0${entry.normalizedText}`, entry.id);\n }\n if (entry.entityRef && entry.supersessionKey) {\n this.byKey.set(keyedTierKey(ns, entry.entityRef, entry.supersessionKey), entry.id);\n }\n }\n\n /** Invalidate the in-memory cache (rule 25). The next access reloads. */\n invalidate(): void {\n this.loaded = false;\n this.loadPromise = null;\n this.resetIndex();\n }\n\n /**\n * Cross-process staleness probe (#1579). If the file's mtime advanced since\n * the last load/own-write, a peer process appended and our in-memory index\n * is stale — invalidate and reload before the next access. Mutation callers\n * pass `migrate: false` to avoid nested serializer/lock acquisition.\n */\n async ensureFreshAgainstDisk(options: { migrate?: boolean } = {}): Promise<void> {\n if (!this.io.stat) return;\n let mtimeMs: number;\n try {\n mtimeMs = Math.floor(this.io.stat(this.filePath).mtimeMs);\n } catch {\n return; // ENOENT / permission — keep the loaded index (fail-open).\n }\n if (mtimeMs === this.fileMtimeMs && this.loaded) return;\n this.fileMtimeMs = mtimeMs;\n this.invalidate();\n if (options.migrate === false) {\n await this.loadInternal(false);\n } else {\n await this.load();\n }\n }\n\n /**\n * Record the file mtime after THIS process writes (append / revoke / rebuild)\n * so `ensureFreshAgainstDisk` does not treat our own write as a peer append\n * and throw away the just-updated in-memory index (#1579 — avoids a needless\n * invalidate+reload in the hot write path).\n */\n private markWritten(): void {\n this.recordFileMtime();\n }\n\n private recordFileMtime(): void {\n if (!this.io.stat) return;\n try {\n this.fileMtimeMs = Math.floor(this.io.stat(this.filePath).mtimeMs);\n } catch {\n // ENOENT — fresh store with no file yet; mtime stays at its current value.\n }\n }\n\n /**\n * Append a tombstone entry. Serialized via `serializeMutations` keyed by\n * file path so concurrent appends do not interleave, with rejection recovery\n * (rule 40 — a single failed append never poisons the chain). The in-memory\n * index is updated AFTER the durable append succeeds.\n *\n * Returns the new tombstone id.\n */\n async appendTombstone(input: {\n reason: TombstoneReason;\n createdBy: TombstoneCreatedBy;\n sourceMemoryId: string;\n rawContent: string;\n entityRef?: string;\n supersessionKey?: string;\n operationKey?: string;\n createdAt?: string;\n /**\n * Pre-computed canonical contentHash from the retired memory's frontmatter.\n * When provided, used directly so the tombstone's exact tier matches the\n * hash writeMemory computes on re-extraction (issue #1579 review: cited\n * facts must not slip past the chokepoint because the emitter hashed the\n * citation-annotated body instead of the canonical source).\n */\n contentHash?: string;\n }): Promise<string> {\n await this.load();\n const createdAt = input.createdAt ?? new Date().toISOString();\n const id = newTombstoneId();\n const currentHash = this.options.hashContent(input.rawContent);\n const currentNormalizedText = this.options.normalizeText(input.rawContent);\n const entry: TombstoneEntry = {\n id,\n ...(input.contentHash && input.contentHash !== currentHash\n ? { currentContentHashAlias: currentHash }\n : {}),\n kind: \"tombstone\",\n reason: input.reason,\n sourceMemoryId: input.sourceMemoryId,\n contentHash: input.contentHash ?? currentHash,\n normalizedText: currentNormalizedText,\n normalizerVersion: TOMBSTONE_NORMALIZER_VERSION,\n ...(input.entityRef ? { entityRef: input.entityRef } : {}),\n ...(input.supersessionKey ? { supersessionKey: input.supersessionKey } : {}),\n ...(input.operationKey ? { operationKey: input.operationKey } : {}),\n namespace: this.namespace,\n createdAt,\n createdBy: input.createdBy,\n };\n await this.serializeAppend(entry);\n this.indexEntry(entry);\n this.markWritten();\n await this.migrateLoadedLegacyEntries();\n return id;\n }\n\n /**\n * Append a revocation entry referencing `tombstoneId`. The log is\n * append-only; lookup treats a revoked tombstone as re-allowed (rule 25).\n */\n async revoke(tombstoneId: string, createdBy: TombstoneCreatedBy): Promise<string> {\n await this.load();\n const createdAt = new Date().toISOString();\n const id = newTombstoneId();\n const entry: TombstoneEntry = {\n id,\n kind: \"revocation\",\n reason: \"correction\",\n sourceMemoryId: \"\",\n contentHash: \"\",\n normalizedText: \"\",\n namespace: this.namespace,\n createdAt,\n createdBy,\n revokes: tombstoneId,\n };\n await this.serializeAppend(entry);\n this.indexEntry(entry);\n this.markWritten();\n await this.migrateLoadedLegacyEntries();\n return id;\n }\n\n private serializeAppend(entry: TombstoneEntry): Promise<void> {\n const line = JSON.stringify(entry) + \"\\n\";\n // serializeMutations (in-process, rule 40 rejection recovery) wraps the\n // cross-process lock: within one process appends are queued, and across\n // processes the advisory lockfile serializes the read-merge-write so two\n // Remnic processes appending concurrently cannot drop each other's entry\n // (issue #1639). Under the lock we re-sync the in-memory index against\n // disk first, so indexEntry builds on the peer's just-appended entries.\n return serializeMutations(`tombstone:${this.filePath}`, () =>\n this.withWriteLock(async () => {\n await this.ensureFreshAgainstDisk({ migrate: false });\n await this.io.append(this.filePath, line);\n }),\n );\n }\n\n private async migrateLoadedLegacyEntries(): Promise<void> {\n const initial = {\n entries: [...this.entries],\n corruptedLines: this.corruptedLines,\n };\n if (!initial.entries.some(\n (entry) => entry.kind === \"tombstone\" && entry.normalizerVersion !== TOMBSTONE_NORMALIZER_VERSION,\n )) {\n return;\n }\n try {\n const migrated = await this.migrateLegacyEntries(initial);\n this.resetIndex();\n for (const entry of migrated.entries) this.indexEntry(entry);\n this.corruptedLines = migrated.corruptedLines;\n this.loaded = true;\n } catch {\n // The durable mutation already succeeded; defer migration to next load.\n }\n }\n\n /**\n * Acquire the cross-process tombstone write lock and run `task` under it\n * (issue #1639). Strict-fail on acquisition timeout: the secure-store append\n * is a read-merge-write, so a best-effort unlocked run would clobber a\n * concurrent writer — reintroducing the lost-write race this lock closes.\n * The thrown error surfaces to the caller (rule 34: never silently drop);\n * rebuild recovers the tombstone on the next maintenance cycle.\n *\n * In-process callers are already serialized by serializeMutations (one key\n * for append/revoke/rebuild), so this lock only contends across processes —\n * the single-process daemon (default deployment) acquires it immediately.\n */\n private withWriteLock<T>(task: () => Promise<T>): Promise<T> {\n return withHeldFileLock(\n this.lockPath,\n {\n staleMs: this.lockStaleMs,\n maxWaitMs: this.lockMaxWaitMs,\n heartbeatMs: this.lockHeartbeatMs,\n },\n async (acquired) => {\n if (!acquired) {\n throw new Error(\n \"could not acquire tombstone write lock (contention timeout or filesystem error)\",\n );\n }\n return task();\n },\n );\n }\n\n /**\n * Look up whether `query` is blocked by an active (non-revoked) tombstone.\n * Tiers are checked in order: exact (contentHash) → normalized → keyed →\n * semantic (off by default). Returns the first active match, or `null`.\n *\n * Namespace isolation (rule 42): only entries with `namespace === query.namespace`\n * match. The store is per-namespace, so this is belt-and-suspenders.\n */\n lookup(query: TombstoneLookupQuery): TombstoneMatch | null {\n // We intentionally do NOT await load() here — lookup is called from the\n // hot write path. Callers MUST ensure the store is loaded before lookup\n // (StorageManager does this in ensureTombstoneStoreLoaded). If not loaded,\n // lookup returns null (fail-open: a missing tombstone check is preferable\n // to crashing every write — see rule 34 / the \"degrade gracefully\" note).\n if (!this.options.enabled) return null;\n if (!this.loaded) return null;\n\n // Tier 1: exact contentHash. Namespace is part of the map key (thread\n // Ocs-O), so the lookup finds only this namespace's tombstone even when\n // the backing file is shared. The namespace equality check below is kept\n // as defense-in-depth.\n const ns = query.namespace;\n if (query.contentHash) {\n const id = this.byHash.get(`${ns}\\0${query.contentHash}`);\n if (id && !this.revokedIds.has(id)) {\n const entry = this.byId.get(id);\n if (entry && entry.namespace === ns) {\n return { tombstoneId: id, matchedTier: \"exact\", reason: entry.reason };\n }\n }\n }\n // Tier 2: normalized text.\n if (query.normalizedText) {\n const id = this.byNormalized.get(`${ns}\\0${query.normalizedText}`);\n if (id && !this.revokedIds.has(id)) {\n const entry = this.byId.get(id);\n if (entry && entry.namespace === ns) {\n return { tombstoneId: id, matchedTier: \"normalized\", reason: entry.reason };\n }\n }\n }\n // Tier 3: keyed (entityRef + supersessionKey). Issue #1579 thread\n // Ociag/Oci-W: check EVERY supplied key, not just the first — emitters\n // append one tombstone per matched supersession key, so the active block\n // can live on any later key. The union of `supersessionKey` (singular,\n // direct callers) and `supersessionKeys` (array, write chokepoint) is\n // checked; the first active match wins (tiers are equality-based, so\n // ordering across keys does not affect correctness).\n if (query.entityRef) {\n const keysToCheck: string[] = [];\n if (query.supersessionKey) keysToCheck.push(query.supersessionKey);\n if (query.supersessionKeys) {\n for (const k of query.supersessionKeys) {\n if (!keysToCheck.includes(k)) keysToCheck.push(k);\n }\n }\n for (const key of keysToCheck) {\n const id = this.byKey.get(keyedTierKey(ns, query.entityRef, key));\n if (id && !this.revokedIds.has(id)) {\n const entry = this.byId.get(id);\n if (entry && entry.namespace === ns) {\n return { tombstoneId: id, matchedTier: \"keyed\", reason: entry.reason };\n }\n }\n }\n }\n // Tier 4: semantic (off by default, rule 48).\n if (this.options.semanticMatch && this.options.semanticSimilarity && query.normalizedText) {\n const threshold = this.options.semanticThreshold;\n let best: { id: string; reason: TombstoneReason; score: number } | null = null;\n for (const entry of this.entries) {\n if (entry.kind !== \"tombstone\") continue;\n if (entry.normalizerVersion !== TOMBSTONE_NORMALIZER_VERSION) continue;\n if (entry.namespace !== query.namespace) continue;\n if (this.revokedIds.has(entry.id)) continue;\n if (!entry.normalizedText) continue;\n const score = this.options.semanticSimilarity(query.normalizedText, entry.normalizedText);\n if (score >= threshold && (!best || score > best.score)) {\n best = { id: entry.id, reason: entry.reason, score };\n }\n }\n if (best) {\n return { tombstoneId: best.id, matchedTier: \"semantic\", reason: best.reason };\n }\n }\n return null;\n }\n async hasExactEntry(input: {\n sourceMemoryId: string;\n contentHash?: string;\n entityRef?: string;\n supersessionKey?: string;\n createdAt?: string;\n operationKey?: string;\n }): Promise<boolean> {\n if (!this.options.enabled) return false;\n await this.ensureFreshAgainstDisk();\n await this.load();\n return this.entries.some(\n (entry) =>\n entry.kind === \"tombstone\" &&\n !this.revokedIds.has(entry.id) &&\n entry.namespace === this.namespace &&\n entry.sourceMemoryId === input.sourceMemoryId &&\n (input.contentHash === undefined || entry.contentHash === input.contentHash) &&\n (input.entityRef === undefined || entry.entityRef === input.entityRef) &&\n (input.supersessionKey === undefined || entry.supersessionKey === input.supersessionKey) &&\n (input.createdAt === undefined || entry.createdAt === input.createdAt) &&\n (input.operationKey === undefined || entry.operationKey === input.operationKey),\n );\n }\n\n\n /** Aggregate stats for the doctor / x-ray surfaces. */\n stats(): TombstoneStats {\n let active = 0;\n for (const entry of this.entries) {\n if (entry.kind === \"tombstone\" && !this.revokedIds.has(entry.id)) active += 1;\n }\n return {\n count: active,\n revoked: this.revokedIds.size,\n lastAppendAt: this.lastAppendAt,\n corruptedLines: this.corruptedLines,\n loaded: this.loaded,\n };\n }\n\n /** Read-only snapshot of all entries (for rebuild + tests). */\n snapshot(): readonly TombstoneEntry[] {\n return this.entries;\n }\n\n /**\n * Rebuild the in-memory index + JSONL from the supplied retired-memory\n * records + existing entries (preserving revocations). Byte-stable: entries\n * are sorted by (createdAt, id) before write (rule 38).\n *\n * Returns the count of tombstone entries written.\n */\n async rebuild(retiredMemories: ReadonlyArray<RetiredMemoryRecord>): Promise<number> {\n // The full read-merge-write runs UNDER the cross-process lock (issue #1639,\n // cursor/codex review): we ensureFreshAgainstDisk() first so the in-memory\n // index reflects any peer append/revoke that landed while we waited for the\n // lock, THEN compute the payload. Rebuild rewrites the entire JSONL, so a\n // payload built from a stale this.entries would overwrite a peer's just-\n // written entry and resurrect a retired fact — the exact invariant this\n // store exists to enforce. serializeMutations (in-process) wraps the\n // cross-process lock; the key is unified with append/revoke so they cannot\n // interleave in-process either.\n const rebuiltCount = await serializeMutations(`tombstone:${this.filePath}`, () =>\n this.withWriteLock(async () => {\n await this.ensureFreshAgainstDisk({ migrate: false });\n // Preserve existing revocations (all namespaces — ids are globally\n // unique) so a rebuild does not silently un-revoke.\n const existingRevocations = this.entries.filter((e) => e.kind === \"revocation\");\n // Preserve tombstone entries from OTHER namespaces when the backing\n // file is shared (issue #1579 thread Oc2MJ). rebuild rewrites the\n // entire file; without preserving foreign entries, rebuilding\n // namespace A would silently delete namespace B's tombstones, allowing\n // resurrection in B.\n const foreignTombstones = this.entries.filter(\n (e) => e.kind === \"tombstone\" && e.namespace !== this.namespace,\n );\n // Reuse existing tombstone ids for source-equivalent entries so a prior\n // revocation (which references the tombstone id) survives rebuild —\n // minting fresh ids would orphan the revocation and silently un-revoke.\n // Issue #1579 thread Oci-T: key the reuse map by (sourceMemoryId,\n // supersessionKey). See the append path above for the full rationale.\n const existingBySource = new Map<string, string>();\n for (const e of this.entries) {\n if (e.kind === \"tombstone\") {\n existingBySource.set(`${e.sourceMemoryId}\\u{0000}${e.supersessionKey ?? \"\"}`, e.id);\n }\n }\n const rebuilt: TombstoneEntry[] = retiredMemories.map((m) => {\n const currentHash = this.options.hashContent(m.rawContent);\n const currentNormalizedText = this.options.normalizeText(m.rawContent);\n const persistedHash = m.contentHash;\n // A persisted hash equal to the body's LEGACY hash only proves the\n // record predates the Unicode normalizer when that legacy hash is\n // unambiguous. When the legacy normalizer is lossy for this body\n // (any body whose ASCII skeleton collides with a distinct\n // contentHashSource), the equality cannot distinguish \"old body\n // hash\" from \"explicit source identity\" — replacing the persisted\n // hash would displace a live identity (issue #2367). Preserving\n // keeps BOTH keys: contentHash stays the persisted identity and\n // currentContentHashAlias still blocks the body.\n\n // Repair only when the persisted hash provably identifies THIS\n // body's stale legacy hash: either the unambiguous case (legacy and\n // current normalizations agree), or the empty-skeleton case — every\n // non-ASCII-only body normalizes to the empty legacy skeleton, so a\n // persisted hash equal to hash(\"\") is the shared non-identifying\n // legacy body hash and publishing it on the exact tier would\n // collide unrelated tombstones. Any OTHER persisted hash on an\n // empty-skeleton body is a distinct explicit identity (e.g.\n // contentHashSource) and must be preserved.\n const legacyBodyHash = computeLegacyContentHash(m.rawContent);\n const repairEmptySkeleton =\n normalizeLegacyContent(m.rawContent).length === 0 && persistedHash === legacyBodyHash;\n const preserveOverride =\n persistedHash !== undefined &&\n persistedHash !== currentHash &&\n !repairEmptySkeleton &&\n !isUnambiguousLegacyContentHash(m.rawContent, persistedHash, currentNormalizedText);\n return {\n id:\n existingBySource.get(`${m.memoryId}\\u{0000}${m.supersessionKey ?? \"\"}`) ??\n newTombstoneId(),\n ...(preserveOverride ? { currentContentHashAlias: currentHash } : {}),\n kind: \"tombstone\" as const,\n reason: m.reason,\n sourceMemoryId: m.memoryId,\n contentHash: preserveOverride ? persistedHash : currentHash,\n normalizedText: currentNormalizedText,\n normalizerVersion: TOMBSTONE_NORMALIZER_VERSION,\n ...(m.entityRef ? { entityRef: m.entityRef } : {}),\n ...(m.supersessionKey ? { supersessionKey: m.supersessionKey } : {}),\n namespace: this.namespace,\n createdAt: m.createdAt,\n createdBy: m.createdBy,\n };\n });\n // Sort deterministically (rule 38): createdAt, then id for stability.\n rebuilt.sort((a, b) =>\n a.createdAt === b.createdAt\n ? a.id < b.id ? -1 : a.id > b.id ? 1 : 0\n : a.createdAt < b.createdAt ? -1 : 1,\n );\n const all = [...rebuilt, ...existingRevocations, ...foreignTombstones].sort((a, b) =>\n a.createdAt === b.createdAt\n ? a.id < b.id ? -1 : a.id > b.id ? 1 : 0\n : a.createdAt < b.createdAt ? -1 : 1,\n );\n const serialized =\n all.map((e) => JSON.stringify(e)).join(\"\\n\") + (all.length > 0 ? \"\\n\" : \"\");\n await this.io.write(this.filePath, serialized);\n // Update the in-memory index under the same lock so it stays consistent\n // with the file we just wrote (no window for a lookup to see a stale\n // index that misses a just-rebuilt entry).\n this.resetIndex();\n for (const entry of all) this.indexEntry(entry);\n this.corruptedLines = 0;\n this.loaded = true;\n this.markWritten();\n return rebuilt.length;\n }),\n );\n await this.migrateLoadedLegacyEntries();\n return rebuiltCount;\n }\n}\n\n/** A retired memory projected into the shape `TombstoneStore.rebuild` consumes. */\nexport interface RetiredMemoryRecord {\n memoryId: string;\n rawContent: string;\n entityRef?: string;\n supersessionKey?: string;\n reason: TombstoneReason;\n createdBy: TombstoneCreatedBy;\n createdAt: string;\n /** Canonical contentHash from the retired memory's frontmatter (#1579). */\n contentHash?: string;\n}\n\n/**\n * Project a corpus of memories into the retired-memory records `rebuild`\n * consumes. Pure (no I/O) so the StorageManager wiring stays thin (#1579,\n * #1520 god-file ratchet). Only superseded / forgotten / rejected FACTS\n * participate — entities, questions, and artifacts have their own lifecycle\n * (issue pitfall). The supersession key is derived from structured attributes\n * via the injected helper so there is one keyed-tier definition (rule 23).\n */\nexport function collectRetiredMemoriesForRebuild(\n memories: ReadonlyArray<{\n frontmatter: {\n id: string;\n status?: string;\n category?: string;\n contentHash?: string;\n entityRef?: string;\n structuredAttributes?: Record<string, string>;\n updated?: string;\n created?: string;\n };\n content: string;\n }>,\n deps: {\n /** Strip citation annotations from the body before hashing/normalizing. */\n stripCitation: (text: string) => string;\n /** Derive the keyed-tier supersession key (one helper, rule 23). */\n supersessionKeysForFact: (spec: {\n entityRef?: string;\n structuredAttributes?: Record<string, string>;\n }) => string[];\n },\n): RetiredMemoryRecord[] {\n const retired: RetiredMemoryRecord[] = [];\n for (const m of memories) {\n const status = m.frontmatter.status;\n if (status !== \"superseded\" && status !== \"rejected\" && status !== \"forgotten\") continue;\n if (m.frontmatter.category !== \"fact\") continue;\n const reason: TombstoneReason =\n status === \"superseded\" ? \"supersession\" : status === \"forgotten\" ? \"retraction\" : \"correction\";\n const createdBy: TombstoneCreatedBy =\n reason === \"supersession\" ? \"supersession\" : \"user_correction\";\n const entityRef = m.frontmatter.entityRef;\n const keys =\n entityRef && m.frontmatter.structuredAttributes\n ? deps.supersessionKeysForFact({\n entityRef,\n structuredAttributes: m.frontmatter.structuredAttributes,\n })\n : [];\n // Issue #1579 thread Ocgjz: emit one record per matched supersession key\n // (not just keys[0]) so rebuild reproduces the same keyed tombstones as\n // live temporal-supersession (which now appends one per key — thread ObteS).\n // Without this, a rebuild would under-rebuild the JSONL and keyed-tier\n // blocks would disappear until rediscovered.\n const keysToEmit = keys.length > 0 ? keys : [undefined];\n for (const key of keysToEmit) {\n retired.push({\n memoryId: m.frontmatter.id,\n rawContent: deps.stripCitation(m.content),\n contentHash: m.frontmatter.contentHash,\n ...(entityRef ? { entityRef } : {}),\n ...(key ? { supersessionKey: key } : {}),\n reason,\n createdBy,\n createdAt: m.frontmatter.updated || m.frontmatter.created || new Date().toISOString(),\n });\n }\n }\n return retired;\n}\n\n/**\n * One-shot write-time resurrection gate (issue #1579 / #2213). Looks up the\n * fact's identity in the tombstone store and, on a match, downgrades the\n * frontmatter to `pending_review` + `blockedBy` IN PLACE. ADD-ONLY: an\n * already-blocked or terminal-status record is left untouched. Pure over its\n * inputs (no I/O beyond the injected store) so both `writeMemory`'s gate and\n * the chunk-repair re-gate share one lookup + apply.\n */\nexport function applyTombstoneResurrectionGate(\n store: Pick<TombstoneStore, \"lookup\">,\n fm: {\n id: string;\n status?: string;\n blockedBy?: string;\n tombstoneBlockTier?: TombstoneMatchTier;\n entityRef?: string;\n contentHash?: string;\n },\n query: {\n normalizedText: string;\n supersessionKeys: string[];\n namespace: string;\n },\n): TombstoneMatch | null {\n if (fm.status === \"pending_review\" && fm.blockedBy) return null;\n if (!(fm.status === undefined || fm.status === \"active\" || fm.status === \"pending_review\")) return null;\n const match = store.lookup({\n contentHash: fm.contentHash,\n normalizedText: query.normalizedText,\n ...(fm.entityRef ? { entityRef: fm.entityRef } : {}),\n ...(query.supersessionKeys.length > 0 ? { supersessionKeys: query.supersessionKeys } : {}),\n namespace: query.namespace,\n });\n if (!match) return null;\n fm.status = \"pending_review\";\n fm.blockedBy = match.tombstoneId;\n fm.tombstoneBlockTier = match.matchedTier;\n return match;\n}\n\n/**\n * Build the live-emission tombstone inputs for a single retired FACT — one\n * input per derived supersession key (or a single keyless record when no\n * structured attributes are present). Pure (no I/O) so the emitters in\n * `StorageManager.supersedeMemory` (contradiction) and `forgetMemory`\n * (retraction) stay thin (#1579, #1520 god-file ratchet). Mirrors the\n * temporal-supersession emitter and `collectRetiredMemoriesForRebuild` so\n * every retire path emits the same keyed-tombstone shape (issue #1579\n * threads Oci-Y / OchiF: without per-key tombstones, a paraphrased\n * re-observation missed the keyed tier and the fact resurrected active).\n */\nexport function buildRetiredFactTombstoneInputs(\n memory: {\n id: string;\n content: string;\n contentHash?: string;\n entityRef?: string;\n structuredAttributes?: Record<string, string>;\n },\n opts: {\n reason: TombstoneReason;\n createdBy: TombstoneCreatedBy;\n createdAt: string;\n supersessionKeysForFact: (spec: {\n entityRef?: string;\n structuredAttributes?: Record<string, string>;\n }) => string[];\n },\n): Array<{\n reason: TombstoneReason;\n createdBy: TombstoneCreatedBy;\n sourceMemoryId: string;\n rawContent: string;\n contentHash?: string;\n entityRef?: string;\n supersessionKey?: string;\n createdAt: string;\n}> {\n const keys =\n memory.entityRef && memory.structuredAttributes\n ? opts.supersessionKeysForFact({\n entityRef: memory.entityRef,\n structuredAttributes: memory.structuredAttributes,\n })\n : [];\n const keysToEmit = keys.length > 0 ? keys : [undefined];\n return keysToEmit.map((key) => ({\n reason: opts.reason,\n createdBy: opts.createdBy,\n sourceMemoryId: memory.id,\n rawContent: memory.content,\n ...(memory.contentHash ? { contentHash: memory.contentHash } : {}),\n ...(memory.entityRef ? { entityRef: memory.entityRef } : {}),\n ...(key ? { supersessionKey: key } : {}),\n createdAt: opts.createdAt,\n }));\n}\n"],"mappings":";;;;;;;;;;;AA4LA,IAAM,+BAA+B;AACrC,IAAM,iCAAiC;AAEvC,IAAM,mBAAmB;AAEzB,SAAS,iBAAyB;AAChC,SAAO,GAAG,gBAAgB,IAAI,KAAK,IAAI,EAAE,SAAS,EAAE,CAAC,IAAI,KAA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AM,OACJ,OAAO,aAAa,OAAO,uBACvB,KAAK,wBAAwB;AAAA,IAC3B,WAAW,OAAO;AAAA,IAClB,sBAAsB,OAAO;AAAA,EAC/B,CAAC,IACD,CAAC;AACP,QAAM,aAAa,KAAK,SAAS,IAAI,OAAO,CAAC,MAAS;AACtD,SAAO,WAAW,IAAI,CAAC,SAAS;AAAA,IAC9B,QAAQ,KAAK;AAAA,IACb,WAAW,KAAK;AAAA,IAChB,gBAAgB,OAAO;AAAA,IACvB,YAAY,OAAO;AAAA,IACnB,GAAI,OAAO,cAAc,EAAE,aAAa,OAAO,YAAY,IAAI,CAAC;AAAA,IAChE,GAAI,OAAO,YAAY,EAAE,WAAW,OAAO,UAAU,IAAI,CAAC;AAAA,IAC1D,GAAI,MAAM,EAAE,iBAAiB,IAAI,IAAI,CAAC;AAAA,IACtC,WAAW,KAAK;AAAA,EAClB,EAAE;AACJ;","names":["source"]}
@@ -0,0 +1,92 @@
1
+ // src/user-message-cleaning.ts
2
+ var NO_PLATFORM_HEADER = /$a/;
3
+ var WRAPPER_OPEN = "<supermemory-context";
4
+ var WRAPPER_CLOSE = "</supermemory-context>";
5
+ var WRAPPER_MAX_ATTRIBUTE_CHARS = 256;
6
+ function isWhitespaceCode(code) {
7
+ return code === 32 || code >= 9 && code <= 13;
8
+ }
9
+ function stripMemoryWrappers(content) {
10
+ let out = "";
11
+ let cursor = 0;
12
+ for (; ; ) {
13
+ const open = content.indexOf(WRAPPER_OPEN, cursor);
14
+ if (open === -1) return out + content.slice(cursor);
15
+ const headEnd = content.indexOf(">", open);
16
+ if (headEnd === -1) return out + content.slice(cursor);
17
+ const head = content.slice(open + WRAPPER_OPEN.length, headEnd);
18
+ const headIsPlain = head.length > 0 && (head.length > WRAPPER_MAX_ATTRIBUTE_CHARS + 1 || headIsNotAttributeRun(head));
19
+ if (head !== "" && headIsPlain) {
20
+ out += content.slice(cursor, open + WRAPPER_OPEN.length);
21
+ cursor = open + WRAPPER_OPEN.length;
22
+ continue;
23
+ }
24
+ const close = content.indexOf(WRAPPER_CLOSE, headEnd);
25
+ if (close === -1) return out + content.slice(cursor);
26
+ let after = close + WRAPPER_CLOSE.length;
27
+ while (after < content.length && isWhitespaceCode(content.charCodeAt(after))) {
28
+ after += 1;
29
+ }
30
+ out += content.slice(cursor, open);
31
+ cursor = after;
32
+ }
33
+ }
34
+ function stripTrailingMessageId(content) {
35
+ const marker = "[message_id:";
36
+ const start = content.lastIndexOf(marker);
37
+ if (start === -1) return null;
38
+ let index = start + marker.length;
39
+ while (index < content.length && isWhitespaceCode(content.charCodeAt(index))) {
40
+ index += 1;
41
+ }
42
+ const idStart = index;
43
+ while (index < content.length && content.charCodeAt(index) !== 93) {
44
+ index += 1;
45
+ }
46
+ if (index === idStart || index === content.length) return null;
47
+ index += 1;
48
+ while (index < content.length && isWhitespaceCode(content.charCodeAt(index))) {
49
+ index += 1;
50
+ }
51
+ if (index !== content.length) return null;
52
+ let boundary = start;
53
+ while (boundary > 0 && isWhitespaceCode(content.charCodeAt(boundary - 1))) {
54
+ boundary -= 1;
55
+ }
56
+ return content.slice(0, boundary);
57
+ }
58
+ function headIsNotAttributeRun(head) {
59
+ if (head.charCodeAt(0) !== 32 && !(head.charCodeAt(0) >= 9 && head.charCodeAt(0) <= 13)) {
60
+ return true;
61
+ }
62
+ for (let index = 0; index < head.length; index += 1) {
63
+ const code = head.charCodeAt(index);
64
+ if (code === 60 || code === 62) return true;
65
+ }
66
+ return false;
67
+ }
68
+ function cleanUserMessageWithPattern(content, platformHeaderPattern) {
69
+ let cleaned = stripMemoryWrappers(content);
70
+ const platformHeader = cleaned.match(platformHeaderPattern);
71
+ const hasPlatformHeader = platformHeader !== null;
72
+ if (platformHeader) {
73
+ cleaned = cleaned.slice(platformHeader[0].length);
74
+ }
75
+ cleaned = cleaned.replace(
76
+ /^\s*## Memory Context \((?:Engram|Remnic)\)[\s\S]*?(?=\n## |\n$)/i,
77
+ ""
78
+ );
79
+ if (hasPlatformHeader) {
80
+ cleaned = stripTrailingMessageId(cleaned) ?? cleaned;
81
+ }
82
+ return cleaned.trim();
83
+ }
84
+ function cleanArchivedUserMessage(content) {
85
+ return cleanUserMessageWithPattern(content, NO_PLATFORM_HEADER);
86
+ }
87
+
88
+ export {
89
+ cleanUserMessageWithPattern,
90
+ cleanArchivedUserMessage
91
+ };
92
+ //# sourceMappingURL=chunk-47DU4SGJ.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/user-message-cleaning.ts"],"sourcesContent":["/**\n * Host-agnostic user-message cleaning (issue #2331).\n *\n * Owns the generic part of the cleaner the OpenClaw wiring has always used\n * (`src/user-message-cleaning.ts` in the root host bundle): stripping the\n * structured host-injected memory wrappers and the leading memory-context\n * preamble from a user message. The host wrapper keeps only the\n * platform-header pattern (channel envelopes are an OpenClaw concept) and\n * delegates here, so core consumers — the episodic-context recall section\n * re-rendering archived user turns — share ONE implementation instead of a\n * second cleaner drifting beside it.\n */\n\n/** Never-matching header pattern for callers with no platform envelope. */\nconst NO_PLATFORM_HEADER = /$a/;\n\nconst WRAPPER_OPEN = \"<supermemory-context\";\nconst WRAPPER_CLOSE = \"</supermemory-context>\";\nconst WRAPPER_MAX_ATTRIBUTE_CHARS = 256;\n\nfunction isWhitespaceCode(code: number): boolean {\n return code === 32 || (code >= 9 && code <= 13);\n}\n\n/**\n * Remove `<supermemory-context …>…</supermemory-context>` wrappers. A\n * single left-to-right scan with string searches — no regex on uncontrolled\n * input — so adversarial repeats of the tag literal stay linear (CodeQL\n * redos rule, issue #2331 review). Malformed or oversized tag heads are\n * treated as plain text, and an unterminated wrapper preserves the rest.\n */\nfunction stripMemoryWrappers(content: string): string {\n let out = \"\";\n let cursor = 0;\n for (;;) {\n const open = content.indexOf(WRAPPER_OPEN, cursor);\n if (open === -1) return out + content.slice(cursor);\n const headEnd = content.indexOf(\">\", open);\n if (headEnd === -1) return out + content.slice(cursor);\n const head = content.slice(open + WRAPPER_OPEN.length, headEnd);\n const headIsPlain =\n head.length > 0 &&\n (head.length > WRAPPER_MAX_ATTRIBUTE_CHARS + 1 || headIsNotAttributeRun(head));\n if (head !== \"\" && headIsPlain) {\n // Not a wrapper open tag: keep the literal and continue after it.\n out += content.slice(cursor, open + WRAPPER_OPEN.length);\n cursor = open + WRAPPER_OPEN.length;\n continue;\n }\n const close = content.indexOf(WRAPPER_CLOSE, headEnd);\n if (close === -1) return out + content.slice(cursor);\n let after = close + WRAPPER_CLOSE.length;\n while (after < content.length && isWhitespaceCode(content.charCodeAt(after))) {\n after += 1;\n }\n // Keep everything before the wrapper; only the wrapper (plus its\n // trailing whitespace) is removed.\n out += content.slice(cursor, open);\n cursor = after;\n }\n}\n\n/**\n * Remove a trailing `[message_id: …]` marker (with surrounding whitespace)\n * using a single right-anchored string scan — no regex on uncontrolled\n * input (CodeQL redos rule). Returns null when no marker terminates the\n * content, leaving the caller's string untouched.\n */\nfunction stripTrailingMessageId(content: string): string | null {\n const marker = \"[message_id:\";\n const start = content.lastIndexOf(marker);\n if (start === -1) return null;\n let index = start + marker.length;\n while (index < content.length && isWhitespaceCode(content.charCodeAt(index))) {\n index += 1;\n }\n const idStart = index;\n while (index < content.length && content.charCodeAt(index) !== 93 /* ] */) {\n index += 1;\n }\n if (index === idStart || index === content.length) return null;\n index += 1; // past the closing bracket\n while (index < content.length && isWhitespaceCode(content.charCodeAt(index))) {\n index += 1;\n }\n if (index !== content.length) return null;\n let boundary = start;\n while (boundary > 0 && isWhitespaceCode(content.charCodeAt(boundary - 1))) {\n boundary -= 1;\n }\n return content.slice(0, boundary);\n}\n\n/** True when the tag-head text cannot be a whitespace-led attribute run. */\nfunction headIsNotAttributeRun(head: string): boolean {\n if (head.charCodeAt(0) !== 32 && !(head.charCodeAt(0) >= 9 && head.charCodeAt(0) <= 13)) {\n return true;\n }\n for (let index = 0; index < head.length; index += 1) {\n const code = head.charCodeAt(index);\n if (code === 60 || code === 62) return true;\n }\n return false;\n}\n\nexport function cleanUserMessageWithPattern(\n content: string,\n platformHeaderPattern: RegExp,\n): string {\n let cleaned = stripMemoryWrappers(content);\n const platformHeader = cleaned.match(platformHeaderPattern);\n const hasPlatformHeader = platformHeader !== null;\n if (platformHeader) {\n cleaned = cleaned.slice(platformHeader[0].length);\n }\n\n // Remove markdown memory context only when it is a leading preamble. If a\n // user writes a section with this title later in their message, preserve it.\n cleaned = cleaned.replace(\n /^\\s*## Memory Context \\((?:Engram|Remnic)\\)[\\s\\S]*?(?=\\n## |\\n$)/i,\n \"\",\n );\n\n if (hasPlatformHeader) {\n cleaned = stripTrailingMessageId(cleaned) ?? cleaned;\n }\n return cleaned.trim();\n}\n\n/**\n * Clean an archived user turn for re-injection (issue #2331): strips the\n * memory wrappers this system itself injects, so an archived echo of a\n * previous recall output cannot re-enter the context through the\n * episodic-context section. No platform envelope stripping — core has no\n * host channel-envelope concept.\n */\nexport function cleanArchivedUserMessage(content: string): string {\n return cleanUserMessageWithPattern(content, NO_PLATFORM_HEADER);\n}\n"],"mappings":";AAcA,IAAM,qBAAqB;AAE3B,IAAM,eAAe;AACrB,IAAM,gBAAgB;AACtB,IAAM,8BAA8B;AAEpC,SAAS,iBAAiB,MAAuB;AAC/C,SAAO,SAAS,MAAO,QAAQ,KAAK,QAAQ;AAC9C;AASA,SAAS,oBAAoB,SAAyB;AACpD,MAAI,MAAM;AACV,MAAI,SAAS;AACb,aAAS;AACP,UAAM,OAAO,QAAQ,QAAQ,cAAc,MAAM;AACjD,QAAI,SAAS,GAAI,QAAO,MAAM,QAAQ,MAAM,MAAM;AAClD,UAAM,UAAU,QAAQ,QAAQ,KAAK,IAAI;AACzC,QAAI,YAAY,GAAI,QAAO,MAAM,QAAQ,MAAM,MAAM;AACrD,UAAM,OAAO,QAAQ,MAAM,OAAO,aAAa,QAAQ,OAAO;AAC9D,UAAM,cACJ,KAAK,SAAS,MACb,KAAK,SAAS,8BAA8B,KAAK,sBAAsB,IAAI;AAC9E,QAAI,SAAS,MAAM,aAAa;AAE9B,aAAO,QAAQ,MAAM,QAA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GAAG,QAAQ,KAAK,QAAQ,SAAS,GAAG;AACnD,UAAM,OAAO,KAAK,WAAW,KAAK;AAClC,QAAI,SAAS,MAAM,SAAS,GAAI,QAAO;AAAA,EACzC;AACA,SAAO;AACT;AAEO,SAAS,4BACd,SACA,uBACQ;AACR,MAAI,UAAU,oBAAoB,OAAO;AACzC,QAAM,iBAAiB,QAAQ,MAAM,qBAAqB;AAC1D,QAAM,oBAAoB,mBAAmB;AAC7C,MAAI,gBAAgB;AAClB,cAAU,QAAQ,MAAM,eAAe,CAAC,EAAE,MAAM;AAAA,EAClD;AAIA,YAAU,QAAQ;AAAA,IAChB;AAAA,IACA;AAAA,EACF;AAEA,MAAI,mBAAmB;AACrB,cAAU,uBAAuB,OAAO,KAAK;AAAA,EAC/C;AACA,SAAO,QAAQ,KAAK;AACtB;AASO,SAAS,yBAAyB,SAAyB;AAChE,SAAO,4BAA4B,SAAS,kBAAkB;AAChE;","names":[]}
@@ -26,7 +26,7 @@ import {
26
26
  scoreTrustZoneProvenance,
27
27
  seedTrustZoneDemoDataset,
28
28
  summarizeTrustZonePromotionReadiness
29
- } from "./chunk-KQS72GCR.js";
29
+ } from "./chunk-LJOIZPJH.js";
30
30
  import {
31
31
  planSessionTranscriptMigration
32
32
  } from "./chunk-P5FPO2BR.js";
@@ -35,13 +35,13 @@ import {
35
35
  } from "./chunk-H7XKCNR6.js";
36
36
  import {
37
37
  toRecallExplainJson
38
- } from "./chunk-76YNCJ5H.js";
38
+ } from "./chunk-3F3CVVDS.js";
39
39
  import {
40
40
  estimateRecallTokens
41
- } from "./chunk-EHMMRZOZ.js";
41
+ } from "./chunk-QICOU4RY.js";
42
42
  import {
43
43
  readAggregateExtractionWatermark
44
- } from "./chunk-CIA6ZUBA.js";
44
+ } from "./chunk-WMOJIOIG.js";
45
45
  import {
46
46
  ReplicaDivergenceMonitor
47
47
  } from "./chunk-CVILPI77.js";
@@ -99,11 +99,11 @@ import {
99
99
  listMemoryGovernanceRuns,
100
100
  readMemoryGovernanceRunArtifact,
101
101
  runMemoryGovernance
102
- } from "./chunk-FW73HN33.js";
102
+ } from "./chunk-RFH7U5LT.js";
103
103
  import {
104
104
  CorpusWatermarkCache,
105
105
  computeServiceCorpusCensus
106
- } from "./chunk-I67HKRBF.js";
106
+ } from "./chunk-EFWHCTYJ.js";
107
107
  import {
108
108
  CrossNamespaceBudget,
109
109
  toBudgetWarning
@@ -120,28 +120,28 @@ import {
120
120
  import {
121
121
  filterTrajectoriesByLookbackDays,
122
122
  readCausalTrajectoryRecords
123
- } from "./chunk-HXXGZ4KV.js";
123
+ } from "./chunk-ALNZEWGN.js";
124
124
  import {
125
125
  FileCalendarSource,
126
126
  buildBriefing,
127
127
  enqueuePassiveCorrectionNotification,
128
128
  parseBriefingFocus,
129
129
  parseBriefingWindow
130
- } from "./chunk-W3Y6FYRK.js";
130
+ } from "./chunk-7BJYTX6Q.js";
131
131
  import {
132
132
  parseEntityFile,
133
133
  parseFrontmatter,
134
134
  qmdCollectionPathParts,
135
135
  qmdResultPathCandidates,
136
136
  selfDeps
137
- } from "./chunk-YUIBXMZY.js";
137
+ } from "./chunk-CALURYWR.js";
138
138
  import {
139
139
  namespaceCollectionName
140
- } from "./chunk-QWPAQLYV.js";
140
+ } from "./chunk-JFLKSD5V.js";
141
141
  import {
142
142
  DEFAULT_RECALL_DISCLOSURE,
143
143
  isRecallDisclosure
144
- } from "./chunk-5WVUO6QT.js";
144
+ } from "./chunk-AR253JBM.js";
145
145
  import {
146
146
  normalizeProjectionPreview,
147
147
  normalizeProjectionTags
@@ -208,7 +208,7 @@ import {
208
208
  } from "./chunk-LNEWDF2A.js";
209
209
  import {
210
210
  createOfflineSyncManifestStream
211
- } from "./chunk-4SGDYYWV.js";
211
+ } from "./chunk-RPKJHYVE.js";
212
212
  import {
213
213
  offlineSyncStorageForSnapshot
214
214
  } from "./chunk-HZNYJN77.js";
@@ -359,7 +359,7 @@ import {
359
359
  } from "./chunk-BTTPKDIL.js";
360
360
  import {
361
361
  AccessAuditAdapter
362
- } from "./chunk-TVVEYCNW.js";
362
+ } from "./chunk-7K5Q6COX.js";
363
363
  import {
364
364
  decodeCitationNamespace
365
365
  } from "./chunk-JIYDRBVZ.js";
@@ -8922,6 +8922,29 @@ ${details.join("\n")}`;
8922
8922
  return `## Harmonic Retrieval
8923
8923
 
8924
8924
  ${lines.join("\n\n")}`;
8925
+ }
8926
+ /**
8927
+ * Render grouped raw-turn episodes for the top recalled facts (issue #2331).
8928
+ * Returns `null` for zero episodes so the section is omitted entirely —
8929
+ * never an empty header.
8930
+ */
8931
+ formatEpisodicContext(episodes) {
8932
+ if (episodes.length === 0) return null;
8933
+ const blocks = episodes.map((episode) => {
8934
+ const shortKey = episode.sessionKey.length > 8 ? `${episode.sessionKey.slice(0, 8)}\u2026` : episode.sessionKey;
8935
+ const header = `### Episode: ${shortKey} turns ${episode.fromTurn}-${episode.toTurn - 1} (supports [${episode.memoryIds.join(", ")}])`;
8936
+ const lines = episode.turns.map((turn) => {
8937
+ const content = renderAuthorityBoundContent(turn.content, void 0, {
8938
+ enabled: this.originAuthorityEnabled,
8939
+ untrustedOrigins: this.config.untrustedOrigins
8940
+ });
8941
+ return `${turn.role}: ${content}`;
8942
+ });
8943
+ return [header, ...lines].join("\n");
8944
+ });
8945
+ return `## Source Episodes
8946
+
8947
+ ${blocks.join("\n\n")}`;
8925
8948
  }
8926
8949
  formatWorkProductResults(results) {
8927
8950
  const lines = results.map(({ entry, matchedFields }, index) => {
@@ -17672,4 +17695,4 @@ export {
17672
17695
  EngramAccessService,
17673
17696
  AccessAdminOpsSurface
17674
17697
  };
17675
- //# sourceMappingURL=chunk-AWRJBAIH.js.map
17698
+ //# sourceMappingURL=chunk-4FFROSFG.js.map