hippo-memory 1.44.0 → 1.45.0

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 (528) hide show
  1. package/README.md +12 -10
  2. package/bin/hippo.js +3 -1
  3. package/dist/api.d.ts +21 -21
  4. package/dist/api.js +69 -42
  5. package/dist/audit.d.ts +1 -1
  6. package/dist/audit.js +9 -0
  7. package/dist/autolearn.d.ts +1 -1
  8. package/dist/autolearn.js +2 -1
  9. package/dist/cli.d.ts +3 -0
  10. package/dist/cli.js +151 -72
  11. package/dist/client.d.ts +4 -21
  12. package/dist/client.js +3 -112
  13. package/dist/config.js +2 -1
  14. package/dist/consolidate.d.ts +3 -0
  15. package/dist/consolidate.js +44 -42
  16. package/dist/dag.d.ts +1 -0
  17. package/dist/dag.js +10 -5
  18. package/dist/dashboard.js +7 -3
  19. package/dist/db.js +12 -23
  20. package/dist/dedupe.d.ts +1 -0
  21. package/dist/dedupe.js +4 -7
  22. package/dist/embedding-provider.js +2 -1
  23. package/dist/embeddings.js +81 -8
  24. package/dist/extract.d.ts +2 -0
  25. package/dist/extract.js +9 -4
  26. package/dist/mcp/server.js +21 -36
  27. package/dist/memory.d.ts +3 -0
  28. package/dist/memory.js +7 -1
  29. package/dist/refine-llm.js +3 -2
  30. package/dist/rerankers/jev.js +3 -2
  31. package/dist/rerankers/llm.js +3 -2
  32. package/dist/search.js +3 -3
  33. package/dist/server.d.ts +5 -0
  34. package/dist/server.js +39 -29
  35. package/dist/shared.js +1 -1
  36. package/dist/store.d.ts +18 -11
  37. package/dist/store.js +144 -63
  38. package/dist/version.d.ts +4 -6
  39. package/dist/version.js +4 -7
  40. package/extensions/openclaw-plugin/openclaw.plugin.json +1 -1
  41. package/extensions/openclaw-plugin/package.json +1 -1
  42. package/openclaw.plugin.json +1 -1
  43. package/package.json +6 -3
  44. package/dist/ablation.d.ts.map +0 -1
  45. package/dist/ablation.js.map +0 -1
  46. package/dist/ambient.d.ts.map +0 -1
  47. package/dist/ambient.js.map +0 -1
  48. package/dist/api.d.ts.map +0 -1
  49. package/dist/api.js.map +0 -1
  50. package/dist/audit-prune.d.ts.map +0 -1
  51. package/dist/audit-prune.js.map +0 -1
  52. package/dist/audit.d.ts.map +0 -1
  53. package/dist/audit.js.map +0 -1
  54. package/dist/auth.d.ts.map +0 -1
  55. package/dist/auth.js.map +0 -1
  56. package/dist/autolearn.d.ts.map +0 -1
  57. package/dist/autolearn.js.map +0 -1
  58. package/dist/availability.d.ts.map +0 -1
  59. package/dist/availability.js.map +0 -1
  60. package/dist/benchmarks/e1.3/incident-recall-eval.js +0 -78
  61. package/dist/benchmarks/e1.3/incident-recall-eval.js.map +0 -1
  62. package/dist/benchmarks/e1.3/slack-1000-event-smoke.js +0 -103
  63. package/dist/benchmarks/e1.3/slack-1000-event-smoke.js.map +0 -1
  64. package/dist/capture.d.ts.map +0 -1
  65. package/dist/capture.js.map +0 -1
  66. package/dist/card-detail.d.ts.map +0 -1
  67. package/dist/card-detail.js.map +0 -1
  68. package/dist/card.d.ts.map +0 -1
  69. package/dist/card.js.map +0 -1
  70. package/dist/cli.d.ts.map +0 -1
  71. package/dist/cli.js.map +0 -1
  72. package/dist/client.d.ts.map +0 -1
  73. package/dist/client.js.map +0 -1
  74. package/dist/compare.d.ts.map +0 -1
  75. package/dist/compare.js.map +0 -1
  76. package/dist/config.d.ts.map +0 -1
  77. package/dist/config.js.map +0 -1
  78. package/dist/connectors/github/backfill.d.ts.map +0 -1
  79. package/dist/connectors/github/backfill.js.map +0 -1
  80. package/dist/connectors/github/cli-impl.d.ts.map +0 -1
  81. package/dist/connectors/github/cli-impl.js.map +0 -1
  82. package/dist/connectors/github/deletion.d.ts.map +0 -1
  83. package/dist/connectors/github/deletion.js.map +0 -1
  84. package/dist/connectors/github/dlq.d.ts.map +0 -1
  85. package/dist/connectors/github/dlq.js.map +0 -1
  86. package/dist/connectors/github/idempotency.d.ts.map +0 -1
  87. package/dist/connectors/github/idempotency.js.map +0 -1
  88. package/dist/connectors/github/ingest.d.ts.map +0 -1
  89. package/dist/connectors/github/ingest.js.map +0 -1
  90. package/dist/connectors/github/octokit-client.d.ts.map +0 -1
  91. package/dist/connectors/github/octokit-client.js.map +0 -1
  92. package/dist/connectors/github/ratelimit.d.ts.map +0 -1
  93. package/dist/connectors/github/ratelimit.js.map +0 -1
  94. package/dist/connectors/github/scope.d.ts.map +0 -1
  95. package/dist/connectors/github/scope.js.map +0 -1
  96. package/dist/connectors/github/signature.d.ts.map +0 -1
  97. package/dist/connectors/github/signature.js.map +0 -1
  98. package/dist/connectors/github/tenant-routing.d.ts.map +0 -1
  99. package/dist/connectors/github/tenant-routing.js.map +0 -1
  100. package/dist/connectors/github/transform.d.ts.map +0 -1
  101. package/dist/connectors/github/transform.js.map +0 -1
  102. package/dist/connectors/github/types.d.ts.map +0 -1
  103. package/dist/connectors/github/types.js.map +0 -1
  104. package/dist/connectors/slack/backfill.d.ts.map +0 -1
  105. package/dist/connectors/slack/backfill.js.map +0 -1
  106. package/dist/connectors/slack/deletion.d.ts.map +0 -1
  107. package/dist/connectors/slack/deletion.js.map +0 -1
  108. package/dist/connectors/slack/dlq.d.ts.map +0 -1
  109. package/dist/connectors/slack/dlq.js.map +0 -1
  110. package/dist/connectors/slack/idempotency.d.ts.map +0 -1
  111. package/dist/connectors/slack/idempotency.js.map +0 -1
  112. package/dist/connectors/slack/ingest.d.ts.map +0 -1
  113. package/dist/connectors/slack/ingest.js.map +0 -1
  114. package/dist/connectors/slack/ratelimit.d.ts.map +0 -1
  115. package/dist/connectors/slack/ratelimit.js.map +0 -1
  116. package/dist/connectors/slack/scope.d.ts.map +0 -1
  117. package/dist/connectors/slack/scope.js.map +0 -1
  118. package/dist/connectors/slack/signature.d.ts.map +0 -1
  119. package/dist/connectors/slack/signature.js.map +0 -1
  120. package/dist/connectors/slack/tenant-routing.d.ts.map +0 -1
  121. package/dist/connectors/slack/tenant-routing.js.map +0 -1
  122. package/dist/connectors/slack/transform.d.ts.map +0 -1
  123. package/dist/connectors/slack/transform.js.map +0 -1
  124. package/dist/connectors/slack/types.d.ts.map +0 -1
  125. package/dist/connectors/slack/types.js.map +0 -1
  126. package/dist/connectors/slack/web-client.d.ts.map +0 -1
  127. package/dist/connectors/slack/web-client.js.map +0 -1
  128. package/dist/connectors/slack/workspaces.d.ts.map +0 -1
  129. package/dist/connectors/slack/workspaces.js.map +0 -1
  130. package/dist/consolidate.d.ts.map +0 -1
  131. package/dist/consolidate.js.map +0 -1
  132. package/dist/correction-latency.d.ts.map +0 -1
  133. package/dist/correction-latency.js.map +0 -1
  134. package/dist/customer-notes.d.ts.map +0 -1
  135. package/dist/customer-notes.js.map +0 -1
  136. package/dist/dag.d.ts.map +0 -1
  137. package/dist/dag.js.map +0 -1
  138. package/dist/dashboard.d.ts.map +0 -1
  139. package/dist/dashboard.js.map +0 -1
  140. package/dist/db.d.ts.map +0 -1
  141. package/dist/db.js.map +0 -1
  142. package/dist/decisions.d.ts.map +0 -1
  143. package/dist/decisions.js.map +0 -1
  144. package/dist/dedupe.d.ts.map +0 -1
  145. package/dist/dedupe.js.map +0 -1
  146. package/dist/embedding-provider.d.ts.map +0 -1
  147. package/dist/embedding-provider.js.map +0 -1
  148. package/dist/embeddings.d.ts.map +0 -1
  149. package/dist/embeddings.js.map +0 -1
  150. package/dist/eval-suite.d.ts.map +0 -1
  151. package/dist/eval-suite.js.map +0 -1
  152. package/dist/eval.d.ts.map +0 -1
  153. package/dist/eval.js.map +0 -1
  154. package/dist/extensions/openclaw-plugin/index.js.map +0 -1
  155. package/dist/extract.d.ts.map +0 -1
  156. package/dist/extract.js.map +0 -1
  157. package/dist/forward-claim-detector.d.ts.map +0 -1
  158. package/dist/forward-claim-detector.js.map +0 -1
  159. package/dist/goals.d.ts.map +0 -1
  160. package/dist/goals.js.map +0 -1
  161. package/dist/graph-extract.d.ts.map +0 -1
  162. package/dist/graph-extract.js.map +0 -1
  163. package/dist/graph-recall.d.ts.map +0 -1
  164. package/dist/graph-recall.js.map +0 -1
  165. package/dist/graph-stream.d.ts.map +0 -1
  166. package/dist/graph-stream.js.map +0 -1
  167. package/dist/graph-view.d.ts.map +0 -1
  168. package/dist/graph-view.js.map +0 -1
  169. package/dist/graph.d.ts.map +0 -1
  170. package/dist/graph.js.map +0 -1
  171. package/dist/handoff.d.ts.map +0 -1
  172. package/dist/handoff.js.map +0 -1
  173. package/dist/hooks.d.ts.map +0 -1
  174. package/dist/hooks.js.map +0 -1
  175. package/dist/importers.d.ts.map +0 -1
  176. package/dist/importers.js.map +0 -1
  177. package/dist/incidents.d.ts.map +0 -1
  178. package/dist/incidents.js.map +0 -1
  179. package/dist/index.d.ts.map +0 -1
  180. package/dist/index.js.map +0 -1
  181. package/dist/invalidation.d.ts.map +0 -1
  182. package/dist/invalidation.js.map +0 -1
  183. package/dist/mcp/framing.d.ts.map +0 -1
  184. package/dist/mcp/framing.js.map +0 -1
  185. package/dist/mcp/server.d.ts.map +0 -1
  186. package/dist/mcp/server.js.map +0 -1
  187. package/dist/memory-value-weights.d.ts.map +0 -1
  188. package/dist/memory-value-weights.js.map +0 -1
  189. package/dist/memory-value.d.ts.map +0 -1
  190. package/dist/memory-value.js.map +0 -1
  191. package/dist/memory.d.ts.map +0 -1
  192. package/dist/memory.js.map +0 -1
  193. package/dist/multihop.d.ts.map +0 -1
  194. package/dist/multihop.js.map +0 -1
  195. package/dist/owner-validation.d.ts.map +0 -1
  196. package/dist/owner-validation.js.map +0 -1
  197. package/dist/path-context.d.ts.map +0 -1
  198. package/dist/path-context.js.map +0 -1
  199. package/dist/physics-config.d.ts.map +0 -1
  200. package/dist/physics-config.js.map +0 -1
  201. package/dist/physics-state.d.ts.map +0 -1
  202. package/dist/physics-state.js.map +0 -1
  203. package/dist/physics.d.ts.map +0 -1
  204. package/dist/physics.js.map +0 -1
  205. package/dist/policies.d.ts.map +0 -1
  206. package/dist/policies.js.map +0 -1
  207. package/dist/postinstall.d.ts.map +0 -1
  208. package/dist/postinstall.js.map +0 -1
  209. package/dist/predictions.d.ts.map +0 -1
  210. package/dist/predictions.js.map +0 -1
  211. package/dist/processes.d.ts.map +0 -1
  212. package/dist/processes.js.map +0 -1
  213. package/dist/project-briefs.d.ts.map +0 -1
  214. package/dist/project-briefs.js.map +0 -1
  215. package/dist/project-identity.d.ts.map +0 -1
  216. package/dist/project-identity.js.map +0 -1
  217. package/dist/provenance-coverage.d.ts.map +0 -1
  218. package/dist/provenance-coverage.js.map +0 -1
  219. package/dist/rate-limit.d.ts.map +0 -1
  220. package/dist/rate-limit.js.map +0 -1
  221. package/dist/raw-archive-mirror-cleanup.d.ts.map +0 -1
  222. package/dist/raw-archive-mirror-cleanup.js.map +0 -1
  223. package/dist/raw-archive.d.ts.map +0 -1
  224. package/dist/raw-archive.js.map +0 -1
  225. package/dist/recall-history.d.ts.map +0 -1
  226. package/dist/recall-history.js.map +0 -1
  227. package/dist/recall-scope.d.ts.map +0 -1
  228. package/dist/recall-scope.js.map +0 -1
  229. package/dist/recall-trace.d.ts.map +0 -1
  230. package/dist/recall-trace.js.map +0 -1
  231. package/dist/refine-llm.d.ts.map +0 -1
  232. package/dist/refine-llm.js.map +0 -1
  233. package/dist/reject-flow.d.ts.map +0 -1
  234. package/dist/reject-flow.js.map +0 -1
  235. package/dist/rejection.d.ts.map +0 -1
  236. package/dist/rejection.js.map +0 -1
  237. package/dist/replay.d.ts.map +0 -1
  238. package/dist/replay.js.map +0 -1
  239. package/dist/rerankers/cross-encoder.d.ts.map +0 -1
  240. package/dist/rerankers/cross-encoder.js.map +0 -1
  241. package/dist/rerankers/index.d.ts.map +0 -1
  242. package/dist/rerankers/index.js.map +0 -1
  243. package/dist/rerankers/jev.d.ts.map +0 -1
  244. package/dist/rerankers/jev.js.map +0 -1
  245. package/dist/rerankers/llm.d.ts.map +0 -1
  246. package/dist/rerankers/llm.js.map +0 -1
  247. package/dist/rerankers/types.d.ts.map +0 -1
  248. package/dist/rerankers/types.js.map +0 -1
  249. package/dist/rrf.d.ts.map +0 -1
  250. package/dist/rrf.js.map +0 -1
  251. package/dist/salience.d.ts.map +0 -1
  252. package/dist/salience.js.map +0 -1
  253. package/dist/scheduler.d.ts.map +0 -1
  254. package/dist/scheduler.js.map +0 -1
  255. package/dist/scope.d.ts.map +0 -1
  256. package/dist/scope.js.map +0 -1
  257. package/dist/search.d.ts.map +0 -1
  258. package/dist/search.js.map +0 -1
  259. package/dist/secret-detect.d.ts.map +0 -1
  260. package/dist/secret-detect.js.map +0 -1
  261. package/dist/server-detect.d.ts.map +0 -1
  262. package/dist/server-detect.js.map +0 -1
  263. package/dist/server.d.ts.map +0 -1
  264. package/dist/server.js.map +0 -1
  265. package/dist/shared.d.ts.map +0 -1
  266. package/dist/shared.js.map +0 -1
  267. package/dist/skills.d.ts.map +0 -1
  268. package/dist/skills.js.map +0 -1
  269. package/dist/sleep-redact.d.ts +0 -58
  270. package/dist/sleep-redact.d.ts.map +0 -1
  271. package/dist/sleep-redact.js +0 -80
  272. package/dist/sleep-redact.js.map +0 -1
  273. package/dist/src/ablation.js +0 -138
  274. package/dist/src/ablation.js.map +0 -1
  275. package/dist/src/ambient.js +0 -148
  276. package/dist/src/ambient.js.map +0 -1
  277. package/dist/src/api.js +0 -2109
  278. package/dist/src/api.js.map +0 -1
  279. package/dist/src/audit-prune.js +0 -109
  280. package/dist/src/audit-prune.js.map +0 -1
  281. package/dist/src/audit.js +0 -213
  282. package/dist/src/audit.js.map +0 -1
  283. package/dist/src/auth.js +0 -97
  284. package/dist/src/auth.js.map +0 -1
  285. package/dist/src/autolearn.js +0 -174
  286. package/dist/src/autolearn.js.map +0 -1
  287. package/dist/src/availability.js +0 -94
  288. package/dist/src/availability.js.map +0 -1
  289. package/dist/src/capture.js +0 -1346
  290. package/dist/src/capture.js.map +0 -1
  291. package/dist/src/card-detail.js +0 -15
  292. package/dist/src/card-detail.js.map +0 -1
  293. package/dist/src/card.js +0 -19
  294. package/dist/src/card.js.map +0 -1
  295. package/dist/src/cli.js +0 -9387
  296. package/dist/src/cli.js.map +0 -1
  297. package/dist/src/client.js +0 -237
  298. package/dist/src/client.js.map +0 -1
  299. package/dist/src/compare.js +0 -122
  300. package/dist/src/compare.js.map +0 -1
  301. package/dist/src/config.js +0 -135
  302. package/dist/src/config.js.map +0 -1
  303. package/dist/src/connectors/github/backfill.js +0 -281
  304. package/dist/src/connectors/github/backfill.js.map +0 -1
  305. package/dist/src/connectors/github/cli-impl.js +0 -234
  306. package/dist/src/connectors/github/cli-impl.js.map +0 -1
  307. package/dist/src/connectors/github/deletion.js +0 -85
  308. package/dist/src/connectors/github/deletion.js.map +0 -1
  309. package/dist/src/connectors/github/dlq.js +0 -190
  310. package/dist/src/connectors/github/dlq.js.map +0 -1
  311. package/dist/src/connectors/github/idempotency.js +0 -27
  312. package/dist/src/connectors/github/idempotency.js.map +0 -1
  313. package/dist/src/connectors/github/ingest.js +0 -156
  314. package/dist/src/connectors/github/ingest.js.map +0 -1
  315. package/dist/src/connectors/github/octokit-client.js +0 -70
  316. package/dist/src/connectors/github/octokit-client.js.map +0 -1
  317. package/dist/src/connectors/github/ratelimit.js +0 -31
  318. package/dist/src/connectors/github/ratelimit.js.map +0 -1
  319. package/dist/src/connectors/github/scope.js +0 -13
  320. package/dist/src/connectors/github/scope.js.map +0 -1
  321. package/dist/src/connectors/github/signature.js +0 -81
  322. package/dist/src/connectors/github/signature.js.map +0 -1
  323. package/dist/src/connectors/github/tenant-routing.js +0 -69
  324. package/dist/src/connectors/github/tenant-routing.js.map +0 -1
  325. package/dist/src/connectors/github/transform.js +0 -103
  326. package/dist/src/connectors/github/transform.js.map +0 -1
  327. package/dist/src/connectors/github/types.js +0 -100
  328. package/dist/src/connectors/github/types.js.map +0 -1
  329. package/dist/src/connectors/slack/backfill.js +0 -78
  330. package/dist/src/connectors/slack/backfill.js.map +0 -1
  331. package/dist/src/connectors/slack/deletion.js +0 -59
  332. package/dist/src/connectors/slack/deletion.js.map +0 -1
  333. package/dist/src/connectors/slack/dlq.js +0 -224
  334. package/dist/src/connectors/slack/dlq.js.map +0 -1
  335. package/dist/src/connectors/slack/idempotency.js +0 -31
  336. package/dist/src/connectors/slack/idempotency.js.map +0 -1
  337. package/dist/src/connectors/slack/ingest.js +0 -109
  338. package/dist/src/connectors/slack/ingest.js.map +0 -1
  339. package/dist/src/connectors/slack/ratelimit.js +0 -18
  340. package/dist/src/connectors/slack/ratelimit.js.map +0 -1
  341. package/dist/src/connectors/slack/scope.js +0 -13
  342. package/dist/src/connectors/slack/scope.js.map +0 -1
  343. package/dist/src/connectors/slack/signature.js +0 -27
  344. package/dist/src/connectors/slack/signature.js.map +0 -1
  345. package/dist/src/connectors/slack/tenant-routing.js +0 -41
  346. package/dist/src/connectors/slack/tenant-routing.js.map +0 -1
  347. package/dist/src/connectors/slack/transform.js +0 -47
  348. package/dist/src/connectors/slack/transform.js.map +0 -1
  349. package/dist/src/connectors/slack/types.js +0 -35
  350. package/dist/src/connectors/slack/types.js.map +0 -1
  351. package/dist/src/connectors/slack/web-client.js +0 -43
  352. package/dist/src/connectors/slack/web-client.js.map +0 -1
  353. package/dist/src/connectors/slack/workspaces.js +0 -62
  354. package/dist/src/connectors/slack/workspaces.js.map +0 -1
  355. package/dist/src/consolidate.js +0 -978
  356. package/dist/src/consolidate.js.map +0 -1
  357. package/dist/src/correction-latency.js +0 -74
  358. package/dist/src/correction-latency.js.map +0 -1
  359. package/dist/src/customer-notes.js +0 -325
  360. package/dist/src/customer-notes.js.map +0 -1
  361. package/dist/src/dag.js +0 -352
  362. package/dist/src/dag.js.map +0 -1
  363. package/dist/src/dashboard.js +0 -258
  364. package/dist/src/dashboard.js.map +0 -1
  365. package/dist/src/db.js +0 -2731
  366. package/dist/src/db.js.map +0 -1
  367. package/dist/src/decisions.js +0 -304
  368. package/dist/src/decisions.js.map +0 -1
  369. package/dist/src/dedupe.js +0 -141
  370. package/dist/src/dedupe.js.map +0 -1
  371. package/dist/src/embedding-provider.js +0 -314
  372. package/dist/src/embedding-provider.js.map +0 -1
  373. package/dist/src/embeddings.js +0 -543
  374. package/dist/src/embeddings.js.map +0 -1
  375. package/dist/src/eval-suite.js +0 -294
  376. package/dist/src/eval-suite.js.map +0 -1
  377. package/dist/src/eval.js +0 -187
  378. package/dist/src/eval.js.map +0 -1
  379. package/dist/src/extract.js +0 -117
  380. package/dist/src/extract.js.map +0 -1
  381. package/dist/src/forward-claim-detector.js +0 -117
  382. package/dist/src/forward-claim-detector.js.map +0 -1
  383. package/dist/src/goals.js +0 -399
  384. package/dist/src/goals.js.map +0 -1
  385. package/dist/src/graph-extract.js +0 -314
  386. package/dist/src/graph-extract.js.map +0 -1
  387. package/dist/src/graph-recall.js +0 -277
  388. package/dist/src/graph-recall.js.map +0 -1
  389. package/dist/src/graph-stream.js +0 -176
  390. package/dist/src/graph-stream.js.map +0 -1
  391. package/dist/src/graph-view.js +0 -310
  392. package/dist/src/graph-view.js.map +0 -1
  393. package/dist/src/graph.js +0 -762
  394. package/dist/src/graph.js.map +0 -1
  395. package/dist/src/handoff.js +0 -65
  396. package/dist/src/handoff.js.map +0 -1
  397. package/dist/src/hooks.js +0 -926
  398. package/dist/src/hooks.js.map +0 -1
  399. package/dist/src/importers.js +0 -881
  400. package/dist/src/importers.js.map +0 -1
  401. package/dist/src/incidents.js +0 -336
  402. package/dist/src/incidents.js.map +0 -1
  403. package/dist/src/index.js +0 -32
  404. package/dist/src/index.js.map +0 -1
  405. package/dist/src/invalidation.js +0 -129
  406. package/dist/src/invalidation.js.map +0 -1
  407. package/dist/src/mcp/framing.js +0 -45
  408. package/dist/src/mcp/framing.js.map +0 -1
  409. package/dist/src/mcp/server.js +0 -1298
  410. package/dist/src/mcp/server.js.map +0 -1
  411. package/dist/src/memory-value-weights.js +0 -31
  412. package/dist/src/memory-value-weights.js.map +0 -1
  413. package/dist/src/memory-value.js +0 -261
  414. package/dist/src/memory-value.js.map +0 -1
  415. package/dist/src/memory.js +0 -425
  416. package/dist/src/memory.js.map +0 -1
  417. package/dist/src/multihop.js +0 -35
  418. package/dist/src/multihop.js.map +0 -1
  419. package/dist/src/owner-validation.js +0 -56
  420. package/dist/src/owner-validation.js.map +0 -1
  421. package/dist/src/path-context.js +0 -48
  422. package/dist/src/path-context.js.map +0 -1
  423. package/dist/src/physics-config.js +0 -26
  424. package/dist/src/physics-config.js.map +0 -1
  425. package/dist/src/physics-state.js +0 -172
  426. package/dist/src/physics-state.js.map +0 -1
  427. package/dist/src/physics.js +0 -378
  428. package/dist/src/physics.js.map +0 -1
  429. package/dist/src/policies.js +0 -414
  430. package/dist/src/policies.js.map +0 -1
  431. package/dist/src/postinstall.js +0 -101
  432. package/dist/src/postinstall.js.map +0 -1
  433. package/dist/src/predictions.js +0 -629
  434. package/dist/src/predictions.js.map +0 -1
  435. package/dist/src/processes.js +0 -349
  436. package/dist/src/processes.js.map +0 -1
  437. package/dist/src/project-briefs.js +0 -492
  438. package/dist/src/project-briefs.js.map +0 -1
  439. package/dist/src/project-identity.js +0 -200
  440. package/dist/src/project-identity.js.map +0 -1
  441. package/dist/src/provenance-coverage.js +0 -23
  442. package/dist/src/provenance-coverage.js.map +0 -1
  443. package/dist/src/rate-limit.js +0 -60
  444. package/dist/src/rate-limit.js.map +0 -1
  445. package/dist/src/raw-archive-mirror-cleanup.js +0 -55
  446. package/dist/src/raw-archive-mirror-cleanup.js.map +0 -1
  447. package/dist/src/raw-archive.js +0 -91
  448. package/dist/src/raw-archive.js.map +0 -1
  449. package/dist/src/recall-history.js +0 -235
  450. package/dist/src/recall-history.js.map +0 -1
  451. package/dist/src/recall-scope.js +0 -89
  452. package/dist/src/recall-scope.js.map +0 -1
  453. package/dist/src/recall-trace.js +0 -186
  454. package/dist/src/recall-trace.js.map +0 -1
  455. package/dist/src/refine-llm.js +0 -156
  456. package/dist/src/refine-llm.js.map +0 -1
  457. package/dist/src/reject-flow.js +0 -209
  458. package/dist/src/reject-flow.js.map +0 -1
  459. package/dist/src/rejection.js +0 -160
  460. package/dist/src/rejection.js.map +0 -1
  461. package/dist/src/replay.js +0 -122
  462. package/dist/src/replay.js.map +0 -1
  463. package/dist/src/rerankers/cross-encoder.js +0 -137
  464. package/dist/src/rerankers/cross-encoder.js.map +0 -1
  465. package/dist/src/rerankers/index.js +0 -20
  466. package/dist/src/rerankers/index.js.map +0 -1
  467. package/dist/src/rerankers/jev.js +0 -119
  468. package/dist/src/rerankers/jev.js.map +0 -1
  469. package/dist/src/rerankers/llm.js +0 -80
  470. package/dist/src/rerankers/llm.js.map +0 -1
  471. package/dist/src/rerankers/types.js +0 -2
  472. package/dist/src/rerankers/types.js.map +0 -1
  473. package/dist/src/rrf.js +0 -61
  474. package/dist/src/rrf.js.map +0 -1
  475. package/dist/src/salience.js +0 -74
  476. package/dist/src/salience.js.map +0 -1
  477. package/dist/src/scheduler.js +0 -77
  478. package/dist/src/scheduler.js.map +0 -1
  479. package/dist/src/scope.js +0 -35
  480. package/dist/src/scope.js.map +0 -1
  481. package/dist/src/search.js +0 -1003
  482. package/dist/src/search.js.map +0 -1
  483. package/dist/src/secret-detect.js +0 -95
  484. package/dist/src/secret-detect.js.map +0 -1
  485. package/dist/src/server-detect.js +0 -231
  486. package/dist/src/server-detect.js.map +0 -1
  487. package/dist/src/server.js +0 -3255
  488. package/dist/src/server.js.map +0 -1
  489. package/dist/src/shared.js +0 -502
  490. package/dist/src/shared.js.map +0 -1
  491. package/dist/src/skills.js +0 -346
  492. package/dist/src/skills.js.map +0 -1
  493. package/dist/src/sleep-redact.js +0 -80
  494. package/dist/src/sleep-redact.js.map +0 -1
  495. package/dist/src/sso.js +0 -22
  496. package/dist/src/sso.js.map +0 -1
  497. package/dist/src/stdin.js +0 -41
  498. package/dist/src/stdin.js.map +0 -1
  499. package/dist/src/store.js +0 -3749
  500. package/dist/src/store.js.map +0 -1
  501. package/dist/src/tenant.js +0 -17
  502. package/dist/src/tenant.js.map +0 -1
  503. package/dist/src/trace.js +0 -72
  504. package/dist/src/trace.js.map +0 -1
  505. package/dist/src/version.js +0 -40
  506. package/dist/src/version.js.map +0 -1
  507. package/dist/src/working-memory.js +0 -157
  508. package/dist/src/working-memory.js.map +0 -1
  509. package/dist/src/yaml.js +0 -107
  510. package/dist/src/yaml.js.map +0 -1
  511. package/dist/sso.d.ts +0 -13
  512. package/dist/sso.d.ts.map +0 -1
  513. package/dist/sso.js +0 -22
  514. package/dist/sso.js.map +0 -1
  515. package/dist/stdin.d.ts.map +0 -1
  516. package/dist/stdin.js.map +0 -1
  517. package/dist/store.d.ts.map +0 -1
  518. package/dist/store.js.map +0 -1
  519. package/dist/tenant.d.ts.map +0 -1
  520. package/dist/tenant.js.map +0 -1
  521. package/dist/trace.d.ts.map +0 -1
  522. package/dist/trace.js.map +0 -1
  523. package/dist/version.d.ts.map +0 -1
  524. package/dist/version.js.map +0 -1
  525. package/dist/working-memory.d.ts.map +0 -1
  526. package/dist/working-memory.js.map +0 -1
  527. package/dist/yaml.d.ts.map +0 -1
  528. package/dist/yaml.js.map +0 -1
package/dist/src/store.js DELETED
@@ -1,3749 +0,0 @@
1
- /**
2
- * Storage layer for Hippo.
3
- *
4
- * SQLite is the source of truth.
5
- * Markdown + JSON files remain as human-readable compatibility mirrors.
6
- */
7
- import * as fs from 'fs';
8
- import * as path from 'path';
9
- import { Layer, generateId } from './memory.js';
10
- import { dumpFrontmatter, parseFrontmatter } from './yaml.js';
11
- import { openHippoDb, closeHippoDb, getMeta, setMeta, isFtsAvailable, pruneConsolidationRuns, getHippoDbPath, } from './db.js';
12
- import { rowToSessionHandoff, isHandoffOutcome } from './handoff.js';
13
- import { CARD_TRANSITIONS, CARD_LEASE_MS } from './card.js';
14
- import { tokenize } from './search.js';
15
- import { appendAuditEvent } from './audit.js';
16
- import { resolveTenantId } from './tenant.js';
17
- import { deriveOriginProject, originFromSource, findHippoStoreDir, realpathOrResolve } from './project-identity.js';
18
- import { checkRejectionGuard, RejectedValueError, rejectionDigest, normalizeValueForRejection, insertRejectedValue, findRejectedValue, } from './rejection.js';
19
- // AT1 (plan §5): resolveConflict's kind-aware loser removal needs
20
- // archiveRawMemory for kind='raw' losers. raw-archive.ts imports
21
- // markSummaryDirtyInTx from this module — both imports are used only
22
- // inside function bodies (never at module-evaluation time), so the cycle
23
- // is the standard safe mutual-function-reference shape under NodeNext ESM.
24
- import { archiveRawMemory } from './raw-archive.js';
25
- /**
26
- * Emit an audit event for a mutation against `db`. Wrapped so a broken audit
27
- * log can never crash the surrounding mutation — the SQLite store is still the
28
- * source of truth and audit failures are diagnosable from the missing rows.
29
- */
30
- function audit(db, op, targetId, metadata, actor = 'cli', tenantId) {
31
- try {
32
- appendAuditEvent(db, {
33
- tenantId: tenantId ?? resolveTenantId({}),
34
- actor,
35
- op,
36
- targetId,
37
- metadata,
38
- });
39
- }
40
- catch {
41
- // Audit must never crash a mutation. Failures here mean the audit_log
42
- // table is broken; the mutation has already succeeded.
43
- }
44
- }
45
- /**
46
- * Refusal audit for the AT1 rejection guard (plan §3). Written by the
47
- * transaction OWNER post-rollback — writeEntry's catch (no outer tx exists
48
- * there, so this lands in a fresh implicit transaction) and api.supersede's
49
- * catch (after its own ROLLBACK) — never inside a scope the caller's own
50
- * rollback could claw back. Best-effort `audit()` semantics: never throws.
51
- */
52
- export function auditRejectionRefusal(db, err, actor) {
53
- audit(db, 'reject_refusal', err.entryId, { digest: err.digest, reason: err.reason }, actor, err.tenantId);
54
- }
55
- const INDEX_VERSION = 3;
56
- const MEMORY_SELECT_COLUMNS = `id, created, last_retrieved, retrieval_count, strength, half_life_days, layer, tags_json, emotional_valence, schema_fit, source, outcome_score, outcome_positive, outcome_negative, conflicts_with_json, pinned, confidence, content, parents_json, starred, trace_outcome, source_session_id, valid_from, superseded_by, extracted_from, dag_level, dag_parent_id, kind, scope, owner, artifact_ref, tenant_id, origin_project, descendant_count, earliest_at, latest_at, summary_dirty, last_rebuilt_at, rebuild_count, dag_level_3_built_at`;
57
- // F1 (v1.7.0): qualified-and-aliased columns for the FTS join in
58
- // loadSearchRows. Every column is `m.<col> AS <col>` so rowToEntry's
59
- // unqualified field reads keep working unchanged. The trailing
60
- // bm25(memories_fts) AS bm25_score adds the FTS rank as a result column.
61
- // Only used inside the FTS path; non-FTS paths keep MEMORY_SELECT_COLUMNS.
62
- const MEMORY_SEARCH_COLUMNS = `m.id AS id, m.created AS created, m.last_retrieved AS last_retrieved, m.retrieval_count AS retrieval_count, m.strength AS strength, m.half_life_days AS half_life_days, m.layer AS layer, m.tags_json AS tags_json, m.emotional_valence AS emotional_valence, m.schema_fit AS schema_fit, m.source AS source, m.outcome_score AS outcome_score, m.outcome_positive AS outcome_positive, m.outcome_negative AS outcome_negative, m.conflicts_with_json AS conflicts_with_json, m.pinned AS pinned, m.confidence AS confidence, m.content AS content, m.parents_json AS parents_json, m.starred AS starred, m.trace_outcome AS trace_outcome, m.source_session_id AS source_session_id, m.valid_from AS valid_from, m.superseded_by AS superseded_by, m.extracted_from AS extracted_from, m.dag_level AS dag_level, m.dag_parent_id AS dag_parent_id, m.kind AS kind, m.scope AS scope, m.owner AS owner, m.artifact_ref AS artifact_ref, m.tenant_id AS tenant_id, m.origin_project AS origin_project, m.descendant_count AS descendant_count, m.earliest_at AS earliest_at, m.latest_at AS latest_at, m.summary_dirty AS summary_dirty, m.last_rebuilt_at AS last_rebuilt_at, m.rebuild_count AS rebuild_count, m.dag_level_3_built_at AS dag_level_3_built_at, bm25(memories_fts) AS bm25_score`;
63
- /**
64
- * Default candidate-pool size for `loadSearchEntries` when called with
65
- * `limit === undefined`. Single source of truth; `api.recall` imports
66
- * this for `RecallResult.windowSize` reporting so the two cannot drift.
67
- */
68
- export const DEFAULT_SEARCH_CANDIDATE_LIMIT = 200;
69
- /**
70
- * v1.7.2 — literal scopes excluded from recall by default-deny when the
71
- * caller passes no `scope`. The SQL clause in `loadSearchRows` and the JS
72
- * helper `passesScopeFilterForRecall` (src/api.ts) both read from this
73
- * constant. Adding a deny scope is a one-place change.
74
- *
75
- * Regex-based denies (e.g. `<source>:private:*`) stay in
76
- * `passesScopeFilterForRecall` as a separate JS step — they don't translate
77
- * cleanly to SQL.
78
- *
79
- * Invariant: never empty. An empty array would silently allow quarantine
80
- * scopes through both paths (SQL clause omitted, JS check vacuous). The
81
- * module-load assertion below pins this loudly.
82
- */
83
- export const RECALL_DEFAULT_DENY_SCOPES = ['unknown:legacy'];
84
- /**
85
- * @internal v1.7.3 — runtime guard against a future maintainer blanking a
86
- * load-bearing literal array. Extracted from the inline guard so the throw
87
- * path is directly testable. `as const` arrays widen via `readonly T[]` at
88
- * the call site so the empty case is reachable at runtime.
89
- */
90
- export function assertNonEmpty(arr, name) {
91
- if (arr.length === 0) {
92
- throw new Error(`${name} cannot be empty — would silently allow quarantine scopes`);
93
- }
94
- }
95
- assertNonEmpty(RECALL_DEFAULT_DENY_SCOPES, 'RECALL_DEFAULT_DENY_SCOPES');
96
- function layerDir(root, layer) {
97
- return path.join(root, layer);
98
- }
99
- /** Nearest ancestor store like git; the strict join is the fallback so `hippo init` still creates `<cwd>/.hippo`. */
100
- export function getHippoRoot(cwd = process.cwd(), opts) {
101
- return findHippoStoreDir(cwd, opts) ?? path.join(realpathOrResolve(cwd), '.hippo');
102
- }
103
- export function isInitialized(hippoRoot) {
104
- // A bare .hippo directory is not enough — autoInstallHooks /
105
- // setupDailySchedule can create it without ever calling initStore,
106
- // leaving a partial directory (integrations/, logs/, runs/) with no
107
- // hippo.db. Returning true in that state caused `hippo init` to skip
108
- // initStore and `hippo recall` to silently fall back to an empty store
109
- // (incident 2026-04-26: ingest_direct.py against a bare .hippo).
110
- // Treat the store as initialized only if hippo.db actually exists.
111
- return fs.existsSync(path.join(hippoRoot, 'hippo.db'));
112
- }
113
- export function initStore(hippoRoot) {
114
- ensureMirrorDirectories(hippoRoot);
115
- const db = openHippoDb(hippoRoot);
116
- try {
117
- const bootstrapped = bootstrapLegacyStore(db, hippoRoot);
118
- if (bootstrapped) {
119
- syncMirrorFiles(hippoRoot, db);
120
- }
121
- }
122
- finally {
123
- closeHippoDb(db);
124
- }
125
- }
126
- function ensureMirrorDirectories(hippoRoot) {
127
- const dirs = [
128
- hippoRoot,
129
- path.join(hippoRoot, 'buffer'),
130
- path.join(hippoRoot, 'episodic'),
131
- path.join(hippoRoot, 'semantic'),
132
- path.join(hippoRoot, 'conflicts'),
133
- ];
134
- for (const dir of dirs) {
135
- fs.mkdirSync(dir, { recursive: true });
136
- }
137
- }
138
- /**
139
- * Serialize a MemoryEntry to markdown with YAML frontmatter.
140
- */
141
- export function serializeEntry(entry) {
142
- const frontmatter = {
143
- id: entry.id,
144
- created: entry.created,
145
- last_retrieved: entry.last_retrieved,
146
- retrieval_count: entry.retrieval_count,
147
- strength: Math.round(entry.strength * 10000) / 10000,
148
- half_life_days: entry.half_life_days,
149
- layer: entry.layer,
150
- tags: entry.tags,
151
- emotional_valence: entry.emotional_valence,
152
- schema_fit: entry.schema_fit,
153
- source: entry.source,
154
- outcome_score: entry.outcome_score,
155
- outcome_positive: entry.outcome_positive,
156
- outcome_negative: entry.outcome_negative,
157
- conflicts_with: entry.conflicts_with,
158
- pinned: entry.pinned,
159
- confidence: entry.confidence ?? 'observed',
160
- parents: entry.parents ?? [],
161
- starred: entry.starred ?? false,
162
- trace_outcome: entry.trace_outcome ?? null,
163
- source_session_id: entry.source_session_id ?? null,
164
- kind: entry.kind ?? 'distilled',
165
- scope: entry.scope ?? null,
166
- owner: entry.owner ?? null,
167
- artifact_ref: entry.artifact_ref ?? null,
168
- };
169
- // Emit tenant_id only when not 'default' to keep diffs clean for the dominant
170
- // single-tenant case (mirrors the plan's task 7 guidance).
171
- const tenantId = entry.tenantId ?? 'default';
172
- if (tenantId !== 'default') {
173
- frontmatter['tenant_id'] = tenantId;
174
- }
175
- // v39: origin_project '' (user-global) is meaningful and must round-trip;
176
- // only undefined/null (legacy/unstamped) is omitted.
177
- if (entry.origin_project !== undefined && entry.origin_project !== null) {
178
- frontmatter['origin_project'] = entry.origin_project;
179
- }
180
- // Spread into a fresh object literal: dumpFrontmatter's Record<string,
181
- // YamlValue> parameter needs an index signature, which a named interface
182
- // reference (EntryFrontmatterFields) doesn't structurally provide even
183
- // though every property's value type already matches.
184
- const fm = dumpFrontmatter({ ...frontmatter });
185
- return `${fm}\n\n${entry.content}\n`;
186
- }
187
- /**
188
- * Deserialize a markdown file to a MemoryEntry.
189
- */
190
- export function deserializeEntry(raw) {
191
- const { data, content } = parseFrontmatter(raw);
192
- if (!data['id'] || !data['layer'])
193
- return null;
194
- // SAFETY: every `as X` below narrows a raw YAML frontmatter field to an
195
- // enum/union member of MemoryEntry; frontmatter is only ever written by
196
- // serializeEntry (whose own fields are typed), so out-of-range values here
197
- // would indicate hand-edited files, which this parser is not required to
198
- // reject — matches the pre-existing permissive-parse behavior.
199
- return {
200
- id: String(data['id']),
201
- created: String(data['created'] ?? new Date().toISOString()),
202
- last_retrieved: String(data['last_retrieved'] ?? new Date().toISOString()),
203
- retrieval_count: Number(data['retrieval_count'] ?? 0),
204
- strength: Number(data['strength'] ?? 1.0),
205
- half_life_days: Number(data['half_life_days'] ?? 7),
206
- layer: data['layer'],
207
- tags: normalizeStringArray(data['tags']),
208
- emotional_valence: data['emotional_valence'] ?? 'neutral',
209
- schema_fit: Number(data['schema_fit'] ?? 0.5),
210
- source: String(data['source'] ?? 'cli'),
211
- outcome_score: data['outcome_score'] === null || data['outcome_score'] === undefined ? null : Number(data['outcome_score']),
212
- outcome_positive: Number(data['outcome_positive'] ?? 0),
213
- outcome_negative: Number(data['outcome_negative'] ?? 0),
214
- conflicts_with: normalizeStringArray(data['conflicts_with']),
215
- pinned: Boolean(data['pinned'] ?? false),
216
- confidence: data['confidence'] ?? 'observed',
217
- content: content.trim(),
218
- parents: normalizeStringArray(data['parents']),
219
- starred: Boolean(data['starred'] ?? false),
220
- trace_outcome: data['trace_outcome'] ?? null,
221
- source_session_id: data['source_session_id'] === null || data['source_session_id'] === undefined
222
- ? null
223
- : String(data['source_session_id']),
224
- valid_from: data['valid_from'] ? String(data['valid_from']) : String(data['created'] ?? new Date().toISOString()),
225
- superseded_by: data['superseded_by'] === null || data['superseded_by'] === undefined
226
- ? null
227
- : String(data['superseded_by']),
228
- extracted_from: data['extracted_from'] ?? null,
229
- dag_level: Number(data['dag_level'] ?? 0),
230
- dag_parent_id: data['dag_parent_id'] ?? null,
231
- kind: (data['kind'] ?? 'distilled'),
232
- scope: data['scope'] === null || data['scope'] === undefined ? null : String(data['scope']),
233
- owner: data['owner'] === null || data['owner'] === undefined ? null : String(data['owner']),
234
- artifact_ref: data['artifact_ref'] === null || data['artifact_ref'] === undefined ? null : String(data['artifact_ref']),
235
- tenantId: data['tenant_id'] === null || data['tenant_id'] === undefined ? 'default' : String(data['tenant_id']),
236
- origin_project: data['origin_project'] === null || data['origin_project'] === undefined ? null : String(data['origin_project']),
237
- };
238
- }
239
- function normalizeStringArray(value) {
240
- if (!Array.isArray(value))
241
- return [];
242
- return value.map((item) => String(item));
243
- }
244
- function rowToEntry(row) {
245
- // SAFETY: every `as X` below narrows a SQLite column value to an
246
- // enum/union member of MemoryEntry; `row` comes from MEMORY_SELECT_COLUMNS
247
- // / MEMORY_SEARCH_COLUMNS, which are the only queries producing MemoryRow,
248
- // and the DB layer only ever writes these columns from the same enums.
249
- const entry = {
250
- id: row.id,
251
- created: row.created,
252
- last_retrieved: row.last_retrieved,
253
- retrieval_count: Number(row.retrieval_count ?? 0),
254
- strength: Number(row.strength ?? 1),
255
- half_life_days: Number(row.half_life_days ?? 7),
256
- layer: row.layer,
257
- tags: parseJsonArray(row.tags_json),
258
- emotional_valence: row.emotional_valence ?? 'neutral',
259
- schema_fit: Number(row.schema_fit ?? 0.5),
260
- source: row.source ?? 'cli',
261
- outcome_score: row.outcome_score === null || row.outcome_score === undefined ? null : Number(row.outcome_score),
262
- outcome_positive: Number(row.outcome_positive ?? 0),
263
- outcome_negative: Number(row.outcome_negative ?? 0),
264
- conflicts_with: parseJsonArray(row.conflicts_with_json),
265
- pinned: Boolean(row.pinned),
266
- confidence: row.confidence ?? 'observed',
267
- content: row.content,
268
- parents: parseJsonArray(row.parents_json),
269
- starred: Boolean(row.starred),
270
- trace_outcome: row.trace_outcome ?? null,
271
- source_session_id: row.source_session_id ?? null,
272
- valid_from: row.valid_from ?? row.created,
273
- superseded_by: row.superseded_by ?? null,
274
- extracted_from: row.extracted_from ?? null,
275
- dag_level: Number(row.dag_level ?? 0),
276
- dag_parent_id: row.dag_parent_id ?? null,
277
- kind: (row.kind ?? 'distilled'),
278
- scope: row.scope ?? null,
279
- owner: row.owner ?? null,
280
- artifact_ref: row.artifact_ref ?? null,
281
- tenantId: row.tenant_id ?? 'default',
282
- origin_project: row.origin_project ?? null,
283
- descendant_count: Number(row.descendant_count ?? 0),
284
- earliest_at: row.earliest_at ?? null,
285
- latest_at: row.latest_at ?? null,
286
- // v0.30 / E1 of DAG live-coupling (schema v28). Symmetric with v25 cache.
287
- summary_dirty: (Number(row.summary_dirty ?? 0) === 1 ? 1 : 0),
288
- last_rebuilt_at: row.last_rebuilt_at ?? null,
289
- rebuild_count: Number(row.rebuild_count ?? 0),
290
- dag_level_3_built_at: row.dag_level_3_built_at ?? null,
291
- };
292
- // F1 (v1.7.0): preserve bm25_score from the FTS path. `'bm25_score' in row`
293
- // distinguishes "absent column" (non-FTS path) from "column present but
294
- // value 0" — though FTS5 bm25() never returns 0, this is defensive.
295
- if ('bm25_score' in row && row.bm25_score !== undefined && row.bm25_score !== null) {
296
- entry.bm25_score = Number(row.bm25_score);
297
- }
298
- return entry;
299
- }
300
- function parseJsonArray(raw) {
301
- if (!raw)
302
- return [];
303
- try {
304
- const parsed = JSON.parse(raw);
305
- return Array.isArray(parsed) ? parsed.map((item) => String(item)) : [];
306
- }
307
- catch {
308
- return [];
309
- }
310
- }
311
- /**
312
- * Strict parse for the `last_trace_id` meta value (LC1 F1(d) structural
313
- * fix). A bare Number(raw) would turn '', whitespace, or garbage into a
314
- * usable-looking 0/NaN — a consumer INSERTing recall_trace_outcomes with
315
- * trace_id=0 would hit a masked FK violation (row id 0 never exists).
316
- * Require a clean positive integer string; anything else is treated as
317
- * unset. This is the ONE place that decides "clean" — every consumer of
318
- * `HippoIndex.last_trace_id` (outcomeForLastRecall, tests) reads the
319
- * already-validated value out of `buildIndexFromDb`'s result and never
320
- * re-parses the raw meta string itself.
321
- */
322
- function parseLastTraceId(raw) {
323
- const trimmed = (raw ?? '').trim();
324
- if (!/^\d+$/.test(trimmed) || Number(trimmed) <= 0)
325
- return null;
326
- return trimmed;
327
- }
328
- function isPlainJsonObject(x) {
329
- return x !== null && typeof x === 'object' && !Array.isArray(x);
330
- }
331
- function parseJsonObject(raw) {
332
- if (!raw)
333
- return {};
334
- try {
335
- const parsed = JSON.parse(raw);
336
- if (isPlainJsonObject(parsed)) {
337
- return parsed;
338
- }
339
- return {};
340
- }
341
- catch {
342
- return {};
343
- }
344
- }
345
- function rowToTaskSnapshot(row) {
346
- return {
347
- id: Number(row.id),
348
- task: row.task,
349
- summary: row.summary,
350
- next_step: row.next_step,
351
- status: row.status,
352
- source: row.source,
353
- session_id: row.session_id ?? null,
354
- scope: row.scope ?? null,
355
- created_at: row.created_at,
356
- updated_at: row.updated_at,
357
- };
358
- }
359
- function rowToMemoryConflict(row) {
360
- return {
361
- id: Number(row.id),
362
- memory_a_id: row.memory_a_id,
363
- memory_b_id: row.memory_b_id,
364
- reason: row.reason,
365
- score: Number(row.score ?? 0),
366
- status: row.status,
367
- detected_at: row.detected_at,
368
- updated_at: row.updated_at,
369
- };
370
- }
371
- function rowToSessionEvent(row) {
372
- return {
373
- id: Number(row.id),
374
- session_id: row.session_id,
375
- task: row.task ?? null,
376
- event_type: row.event_type,
377
- content: row.content,
378
- source: row.source,
379
- scope: row.scope ?? null,
380
- metadata: parseJsonObject(row.metadata_json),
381
- created_at: row.created_at,
382
- };
383
- }
384
- // Tenant-scoped mirror file paths. The single-tenant 'default' deployment
385
- // keeps the original `active-task.md` / `recent-session.md` filenames for
386
- // on-disk back-compat; multi-tenant deployments get a `.<tenantId>` suffix
387
- // so tenant B saving cannot overwrite tenant A's mirror file.
388
- function activeTaskMirrorPath(hippoRoot, tenantId) {
389
- const file = tenantId === 'default' ? 'active-task.md' : `active-task.${tenantId}.md`;
390
- return path.join(hippoRoot, 'buffer', file);
391
- }
392
- function recentSessionMirrorPath(hippoRoot, tenantId) {
393
- const file = tenantId === 'default' ? 'recent-session.md' : `recent-session.${tenantId}.md`;
394
- return path.join(hippoRoot, 'buffer', file);
395
- }
396
- function writeActiveTaskMirror(hippoRoot, tenantId, snapshot) {
397
- const filePath = activeTaskMirrorPath(hippoRoot, tenantId);
398
- const fm = dumpFrontmatter({
399
- id: snapshot.id,
400
- task: snapshot.task,
401
- status: snapshot.status,
402
- source: snapshot.source,
403
- session_id: snapshot.session_id,
404
- created_at: snapshot.created_at,
405
- updated_at: snapshot.updated_at,
406
- next_step: snapshot.next_step,
407
- });
408
- const body = [
409
- `# Active Task Snapshot`,
410
- '',
411
- `## Summary`,
412
- snapshot.summary,
413
- '',
414
- `## Next step`,
415
- snapshot.next_step,
416
- '',
417
- `## Task`,
418
- snapshot.task,
419
- '',
420
- ];
421
- if (snapshot.session_id) {
422
- body.push(`## Session`, snapshot.session_id, '');
423
- }
424
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
425
- fs.writeFileSync(filePath, `${fm}\n\n${body.join('\n')}`, 'utf8');
426
- }
427
- function removeActiveTaskMirror(hippoRoot, tenantId) {
428
- const filePath = activeTaskMirrorPath(hippoRoot, tenantId);
429
- if (fs.existsSync(filePath)) {
430
- fs.unlinkSync(filePath);
431
- }
432
- }
433
- function writeRecentSessionMirror(hippoRoot, tenantId, events) {
434
- const filePath = recentSessionMirrorPath(hippoRoot, tenantId);
435
- if (events.length === 0) {
436
- if (fs.existsSync(filePath)) {
437
- fs.unlinkSync(filePath);
438
- }
439
- return;
440
- }
441
- const latest = events[events.length - 1];
442
- const fm = dumpFrontmatter({
443
- session_id: latest.session_id,
444
- task: latest.task,
445
- event_count: events.length,
446
- updated_at: latest.created_at,
447
- });
448
- const lines = [
449
- '# Recent Session Trail',
450
- '',
451
- `- Session: ${latest.session_id}`,
452
- `- Task: ${latest.task ?? 'n/a'}`,
453
- `- Updated: ${latest.created_at}`,
454
- '',
455
- '## Events',
456
- '',
457
- ];
458
- for (const event of events) {
459
- lines.push(`- [${event.created_at}] (${event.event_type}) ${event.content}`);
460
- }
461
- lines.push('');
462
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
463
- fs.writeFileSync(filePath, `${fm}\n\n${lines.join('\n')}`, 'utf8');
464
- }
465
- function writeConflictMirrors(hippoRoot, conflicts) {
466
- const conflictDir = path.join(hippoRoot, 'conflicts');
467
- fs.mkdirSync(conflictDir, { recursive: true });
468
- const keep = new Set();
469
- for (const conflict of conflicts) {
470
- const filename = `conflict_${conflict.id}.md`;
471
- keep.add(filename);
472
- const fm = dumpFrontmatter({
473
- id: conflict.id,
474
- memory_a_id: conflict.memory_a_id,
475
- memory_b_id: conflict.memory_b_id,
476
- reason: conflict.reason,
477
- score: Math.round(conflict.score * 10000) / 10000,
478
- status: conflict.status,
479
- detected_at: conflict.detected_at,
480
- updated_at: conflict.updated_at,
481
- });
482
- const body = [
483
- '# Memory Conflict',
484
- '',
485
- `- Memory A: ${conflict.memory_a_id}`,
486
- `- Memory B: ${conflict.memory_b_id}`,
487
- `- Reason: ${conflict.reason}`,
488
- `- Score: ${conflict.score.toFixed(3)}`,
489
- `- Status: ${conflict.status}`,
490
- '',
491
- ].join('\n');
492
- fs.writeFileSync(path.join(conflictDir, filename), `${fm}\n\n${body}`, 'utf8');
493
- }
494
- for (const existing of fs.readdirSync(conflictDir)) {
495
- if (existing === '.gitkeep')
496
- continue;
497
- if (!keep.has(existing)) {
498
- fs.unlinkSync(path.join(conflictDir, existing));
499
- }
500
- }
501
- }
502
- function canonicalConflictPair(aId, bId) {
503
- return aId < bId
504
- ? { memory_a_id: aId, memory_b_id: bId }
505
- : { memory_a_id: bId, memory_b_id: aId };
506
- }
507
- function loadSearchRows(db, query, limit, tenantId, scopeFilter) {
508
- // tenantId undefined = no tenant filter (legacy callers / cross-deployment
509
- // helpers). tenantId set = strict tenant isolation, leveraging the composite
510
- // idx_memories_tenant_created (leading column tenant_id, O(log n) lookup).
511
- const tenantPredicate = tenantId !== undefined ? ` AND m.tenant_id = ?` : '';
512
- const tenantPredicateNoAlias = tenantId !== undefined ? ` AND tenant_id = ?` : '';
513
- const tenantOnlyPredicate = tenantId !== undefined ? ` WHERE tenant_id = ?` : '';
514
- const tenantParams = tenantId !== undefined ? [tenantId] : [];
515
- // v1.12.6 — belt-and-suspenders against `kind='archived'` leaking into recall.
516
- // `kind='archived'` is a transient sentinel inside `archiveRawMemory`'s
517
- // SAVEPOINT (src/raw-archive.ts:56): UPDATE kind = 'archived' immediately
518
- // followed by DELETE, both inside one savepoint that commits or rolls back
519
- // atomically. SQLite atomicity guarantees no concurrent reader sees the
520
- // intermediate state. This filter is defensive-only against:
521
- // (a) future bugs that drop the SAVEPOINT,
522
- // (b) future bugs that introduce kind='archived' as a persisted state,
523
- // (c) external direct-SQL writes that bypass archiveRawMemory.
524
- // tenantOnlyPredicate starts with " WHERE tenant_id = ?" when tenant is set;
525
- // when unset, we have no WHERE yet, so the archived clause needs both AND
526
- // and WHERE forms. The "tenant-only" path always has WHERE (from tenant or
527
- // we synthesize one).
528
- const archivedClauseAlias = ` AND m.kind != 'archived'`;
529
- const archivedClauseNoAlias = ` AND kind != 'archived'`;
530
- // For the "tenant-only" path: if no tenant set, tenantOnlyPredicate is '',
531
- // so prepend WHERE; if tenant set, append AND. handled in each call site
532
- // by always joining `tenantOnlyPredicate + archivedClauseTenantOnly` where
533
- // the latter switches between " AND" and " WHERE" based on caller context.
534
- const archivedClauseTenantOnly = tenantId !== undefined ? ` AND kind != 'archived'` : ` WHERE kind != 'archived'`;
535
- // v1.7.1 — recall-mode scope predicate (root-cause fix for the
536
- // `unknown:legacy` leak codex flagged on the v1.6.5 review). Forms:
537
- // undefined → no scope filter (background pipelines)
538
- // { mode: 'default-deny' } → exclude unknown:legacy + ':private:'
539
- // { mode: 'exact' } → m.scope = 'X'
540
- // { mode: 'default-deny-or-exact' } → default set OR m.scope = 'X'
541
- // v1.25.0: the private-scope exclusion now ALSO runs here pre-window as a
542
- // conservative LIKE approximation (see the deny-mode comment below); the
543
- // exact anchored regex stays the authoritative JS post-filter in the
544
- // recall consumers.
545
- //
546
- // **Cross-reference:** `passesScopeFilterForRecall` in src/api.ts encodes
547
- // the same default-deny rule. If the deny list grows (e.g. add
548
- // `unknown:purged`), update BOTH this SQL clause AND that helper AND the
549
- // continuity inline closure. v1.7.2 will consolidate them.
550
- let scopeClauseAlias = '';
551
- let scopeClauseNoAlias = '';
552
- let scopeClauseTenantOnly = '';
553
- const scopeParams = [];
554
- if (scopeFilter !== undefined) {
555
- if (scopeFilter.mode === 'default-deny') {
556
- // T2: bind from RECALL_DEFAULT_DENY_SCOPES so SQL and JS share one
557
- // source of truth. Module-load assertion at the top of this file
558
- // guarantees length > 0, so NOT IN () (a SQL parse error) is impossible.
559
- // NULL handling: m.scope NOT IN (?, ?) returns NULL on m.scope = NULL
560
- // (three-valued logic). The `m.scope IS NULL OR ...` disjunct admits
561
- // NULL rows.
562
- // v1.25.0 (codex review-stage P2): the private-scope exclusion must run
563
- // BEFORE the LIMIT, or a store where >window matching rows are
564
- // `<source>:private:*` (heavy private-channel ingestion) starves every
565
- // admitted row out of the candidate window and recall returns
566
- // empty/incomplete. SQL uses a deliberately CONSERVATIVE approximation
567
- // of the exact JS regex (`NOT LIKE '%:private:%'`, ASCII
568
- // case-insensitive): it denies a strict superset (any scope containing
569
- // ':private:' anywhere, any case) — fail-closed for a security filter.
570
- // The exact anchored regex (`passesScopeFilterForRecall` /
571
- // `isPrivateScope`) remains the authoritative JS post-filter.
572
- const placeholders = RECALL_DEFAULT_DENY_SCOPES.map(() => '?').join(', ');
573
- scopeClauseAlias = ` AND (m.scope IS NULL OR (m.scope NOT IN (${placeholders}) AND m.scope NOT LIKE '%:private:%'))`;
574
- scopeClauseNoAlias = ` AND (scope IS NULL OR (scope NOT IN (${placeholders}) AND scope NOT LIKE '%:private:%'))`;
575
- scopeClauseTenantOnly = scopeClauseNoAlias;
576
- scopeParams.push(...RECALL_DEFAULT_DENY_SCOPES);
577
- }
578
- else if (scopeFilter.mode === 'default-deny-or-exact') {
579
- // v1.25.0 CLI semantics: default-admitted set PLUS the explicitly
580
- // requested scope (see the RecallScopeFilter doc above). Same NULL
581
- // three-valued-logic handling and same pre-window private exclusion as
582
- // 'default-deny' (codex P2, comment above); the trailing `OR scope = ?`
583
- // arm keeps the explicitly requested scope loadable, INCLUDING a
584
- // requested private or quarantine scope (deliberate owner access, same
585
- // as api.recall's exact-match for the same input).
586
- const placeholders = RECALL_DEFAULT_DENY_SCOPES.map(() => '?').join(', ');
587
- scopeClauseAlias = ` AND (m.scope IS NULL OR (m.scope NOT IN (${placeholders}) AND m.scope NOT LIKE '%:private:%') OR m.scope = ?)`;
588
- scopeClauseNoAlias = ` AND (scope IS NULL OR (scope NOT IN (${placeholders}) AND scope NOT LIKE '%:private:%') OR scope = ?)`;
589
- scopeClauseTenantOnly = scopeClauseNoAlias;
590
- scopeParams.push(...RECALL_DEFAULT_DENY_SCOPES, scopeFilter.value);
591
- }
592
- else {
593
- // mode === 'exact'
594
- scopeClauseAlias = ` AND m.scope = ?`;
595
- scopeClauseNoAlias = ` AND scope = ?`;
596
- scopeClauseTenantOnly = scopeClauseNoAlias;
597
- scopeParams.push(scopeFilter.value);
598
- }
599
- }
600
- const terms = Array.from(new Set(tokenize(query)));
601
- if (terms.length === 0) {
602
- // F3 (v1.7.0) self-review: empty-query path is the second uncapped
603
- // path (codex diff-pass caught the full-store fallback at the bottom;
604
- // this no-terms path had the same shape). Apply LIMIT so all four
605
- // candidate paths honour the caller's cap when set.
606
- const sql = `SELECT ${MEMORY_SELECT_COLUMNS} FROM memories${tenantOnlyPredicate}${archivedClauseTenantOnly}${scopeClauseTenantOnly} ORDER BY created ASC, id ASC LIMIT ?`;
607
- // SAFETY: sql selects exactly MEMORY_SELECT_COLUMNS, whose column list
608
- // matches MemoryRow's field set.
609
- return db.prepare(sql).all(...tenantParams, ...scopeParams, limit);
610
- }
611
- // v1.7.1 — test/diagnostic hook: `HIPPO_FORCE_LIKE_PATH=1` forces the
612
- // LIKE-fallback path here only. Gated at the read-call site so writes
613
- // (`syncFtsRow`, `deleteFtsRow`, `raw-archive.ts::archiveRaw`) keep using
614
- // `isFtsAvailable` honestly and never silently skip FTS index sync.
615
- // Lets tests exercise the LIKE branch deterministically without
616
- // poisoning the on-disk FTS state.
617
- const forceLikePath = process.env.HIPPO_FORCE_LIKE_PATH === '1';
618
- if (!forceLikePath && isFtsAvailable(db)) {
619
- try {
620
- const ftsQuery = terms.map((t) => `"${t.replace(/"/g, '""')}"`).join(' OR ');
621
- // memories_fts virtual table has no tenant_id column; filter via the
622
- // joined memories row (cheap with idx_memories_tenant_created leading
623
- // on tenant_id).
624
- // F1 (v1.7.0): MEMORY_SEARCH_COLUMNS adds bm25_score as the trailing
625
- // result column. Every other column is m.<col> AS <col> so rowToEntry
626
- // sees the same shape it always has.
627
- // SAFETY: MEMORY_SEARCH_COLUMNS aliases every column to the same name
628
- // MEMORY_SELECT_COLUMNS uses (plus bm25_score), matching MemoryRow.
629
- const rows = db.prepare(`
630
- SELECT ${MEMORY_SEARCH_COLUMNS}
631
- FROM memories m
632
- JOIN memories_fts f ON f.id = m.id
633
- WHERE memories_fts MATCH ?${tenantPredicate}${archivedClauseAlias}${scopeClauseAlias}
634
- ORDER BY bm25(memories_fts), m.updated_at DESC, m.content ASC, m.id ASC
635
- LIMIT ?
636
- `).all(ftsQuery, ...tenantParams, ...scopeParams, limit);
637
- if (rows.length > 0)
638
- return rows;
639
- }
640
- catch {
641
- // Fall back to LIKE matching below.
642
- }
643
- }
644
- const escapeLike = (term) => term.replace(/[%_\\]/g, '\\$&');
645
- const where = terms.map(() => `(LOWER(content) LIKE ? ESCAPE '\\' OR LOWER(tags_json) LIKE ? ESCAPE '\\')`).join(' OR ');
646
- const params = terms.flatMap((term) => {
647
- const like = `%${escapeLike(term)}%`;
648
- return [like, like];
649
- });
650
- // SAFETY: this query selects exactly MEMORY_SELECT_COLUMNS, matching
651
- // MemoryRow's field set.
652
- const rows = db.prepare(`
653
- SELECT ${MEMORY_SELECT_COLUMNS}
654
- FROM memories
655
- WHERE (${where})${tenantPredicateNoAlias}${archivedClauseNoAlias}${scopeClauseNoAlias}
656
- ORDER BY updated_at DESC, created DESC, content ASC, id ASC
657
- LIMIT ?
658
- `).all(...params, ...tenantParams, ...scopeParams, limit);
659
- if (rows.length > 0)
660
- return rows;
661
- // F3 (v1.7.0) codex P1: pre-v1.7.0 the full-store fallback ignored
662
- // `limit` and could return the whole tenant store. With scorerWindow
663
- // now reported on RecallResult, an unbounded fallback would lie about
664
- // candidate-pool size. Apply LIMIT here so all four paths honour the
665
- // caller's cap.
666
- const fallback = `SELECT ${MEMORY_SELECT_COLUMNS} FROM memories${tenantOnlyPredicate}${archivedClauseTenantOnly}${scopeClauseTenantOnly} ORDER BY created ASC, id ASC LIMIT ?`;
667
- // SAFETY: fallback selects exactly MEMORY_SELECT_COLUMNS, matching
668
- // MemoryRow's field set.
669
- return db.prepare(fallback).all(...tenantParams, ...scopeParams, limit);
670
- }
671
- function writeMarkdownMirror(hippoRoot, entry) {
672
- removeEntryMirrors(hippoRoot, entry.id);
673
- const dir = layerDir(hippoRoot, entry.layer);
674
- fs.mkdirSync(dir, { recursive: true });
675
- fs.writeFileSync(path.join(dir, `${entry.id}.md`), serializeEntry(entry), 'utf8');
676
- }
677
- // AT1 P1 fix (codex): `writeMarkdownMirror` writes ANY layer's mirror,
678
- // including `trace/<id>.md` for Layer.Trace rows (auto-promoted traces,
679
- // consolidate.ts) — but this enumeration only walked
680
- // Buffer/Episodic/Semantic. A rejected/forgotten trace row's markdown
681
- // content survived on disk while the purge (and `hippo reject`/plain
682
- // `forget`) reported success, and a stale trace mirror is exactly the
683
- // resurrection channel bootstrapLegacyStore/rebuildIndex guard against.
684
- // Fixes BOTH the AT1 reject-flow purge and the pre-existing plain-`forget`
685
- // gap for trace rows (deleteEntry has always called this same function).
686
- export function removeEntryMirrors(hippoRoot, id) {
687
- for (const layer of [Layer.Buffer, Layer.Episodic, Layer.Semantic, Layer.Trace]) {
688
- const file = path.join(layerDir(hippoRoot, layer), `${id}.md`);
689
- if (fs.existsSync(file)) {
690
- fs.unlinkSync(file);
691
- }
692
- }
693
- }
694
- /**
695
- * AT1 mirror-purge honesty fix (docs/plans/2026-08-15-at1-rejected-value-tombstone.md):
696
- * the candidate markdown mirror paths still on disk for `id`, computed the
697
- * same way `removeEntryMirrors` walks them (one per layer: buffer/episodic/
698
- * semantic), filtered to the ones that still `fs.existsSync`. Used to report
699
- * an EXPLICIT path when a best-effort purge fails and no reaper exists to
700
- * retry it — plain `removeEntryMirrors` returns void, giving no way to name
701
- * which file is stuck.
702
- */
703
- export function getExistingEntryMirrorPaths(hippoRoot, id) {
704
- // AT1 P1 fix (codex): same missing Layer.Trace as removeEntryMirrors above
705
- // — kept in lockstep with it since this function's whole purpose is
706
- // walking the mirror paths "the same way removeEntryMirrors walks them"
707
- // (see its own doc comment).
708
- return [Layer.Buffer, Layer.Episodic, Layer.Semantic, Layer.Trace]
709
- .map((layer) => path.join(layerDir(hippoRoot, layer), `${id}.md`))
710
- .filter((file) => fs.existsSync(file));
711
- }
712
- /**
713
- * AT1 fix: best-effort markdown-mirror purge shared by `reject-flow.ts`'s
714
- * `rejectValue` and `resolveConflict`'s post-commit purge. Both used to log
715
- * "will retry via reaper on next open" for EVERY failure, but the reaper
716
- * (`cleanupArchivedMirrors`, raw-archive-mirror-cleanup.ts) only scans
717
- * `raw_archive` — that message was false for a non-raw id, which has no
718
- * reaper at all.
719
- *
720
- * Retries the unlink once synchronously (the common real-world failure is a
721
- * transient lock/AV-scanner false positive, not a permanent one). On a
722
- * second failure: raw ids still get the honest reaper message (true); non-raw
723
- * ids get the EXPLICIT leftover file path(s) and a manual-delete instruction,
724
- * since nothing will ever retry them automatically.
725
- *
726
- * Returns true if the mirror ended up purged (first or second attempt).
727
- */
728
- export function purgeMirrorBestEffort(hippoRoot, id, isRaw, logPrefix) {
729
- try {
730
- removeEntryMirrors(hippoRoot, id);
731
- return true;
732
- }
733
- catch {
734
- try {
735
- removeEntryMirrors(hippoRoot, id);
736
- return true;
737
- }
738
- catch (secondErr) {
739
- const msg = secondErr instanceof Error ? secondErr.message : String(secondErr);
740
- if (isRaw) {
741
- console.error(`${logPrefix}: mirror cleanup failed for ${id} (will retry via reaper on next open): ${msg}`);
742
- }
743
- else {
744
- const leftover = getExistingEntryMirrorPaths(hippoRoot, id);
745
- const pathsNote = leftover.length > 0 ? leftover.join(', ') : `${id}.md (path unresolved)`;
746
- console.error(`${logPrefix}: mirror cleanup failed for ${id} - no automatic retry exists for this file, ` +
747
- `delete it manually: ${pathsNote} (${msg})`);
748
- }
749
- return false;
750
- }
751
- }
752
- }
753
- function bootstrapLegacyStore(db, hippoRoot) {
754
- // SAFETY: countRow's shape matches the single `COUNT(*) AS count` column
755
- // selected above; `.get()` returns undefined only when no row exists.
756
- const countRow = db.prepare(`SELECT COUNT(*) AS count FROM memories`).get();
757
- const memoryCount = Number(countRow?.count ?? 0);
758
- if (memoryCount > 0)
759
- return false;
760
- // AT1 P2 fix: memoryCount alone is not a reliable "already bootstrapped"
761
- // signal once the rejection guard exists. If EVERY legacy mirror row is
762
- // rejected, memories stays at 0 rows even after a successful bootstrap
763
- // pass, so the memoryCount>0 gate above never trips — every subsequent
764
- // initStore() call would re-run this whole function: re-scan the legacy
765
- // mirrors, re-attempt (and re-refuse, re-auditing) every row, and
766
- // re-INSERT the legacy consolidation_runs rows with no dedup, duplicating
767
- // them on each open. A dedicated meta flag marks bootstrap as
768
- // attempted-and-settled regardless of how many rows actually landed.
769
- if (getMeta(db, 'legacy_bootstrap_completed', '0') === '1')
770
- return false;
771
- const legacyEntries = loadLegacyEntriesFromMarkdown(hippoRoot);
772
- if (legacyEntries.length === 0)
773
- return false;
774
- db.exec('BEGIN');
775
- try {
776
- // AT1 (plan §3, round-3 redesign): run the guard LIVE per row rather
777
- // than bypassing it. bootstrapLegacyStore is exactly the channel through
778
- // which a stale/never-purged markdown mirror could resurrect a rejected
779
- // value; a skip-and-count here closes that structurally, independent of
780
- // mirror state. The refusal audit is written INLINE inside this
781
- // still-open loop transaction (plain audit() — nothing is rolled back
782
- // on a per-row skip, so the post-rollback auditRejectionRefusal helper
783
- // is the wrong tool here).
784
- let rejectedCount = 0;
785
- for (const entry of legacyEntries) {
786
- // v39: legacy markdown carries no origin_project; stamp from the store
787
- // location so bootstrapped rows stay visible to ambient context.
788
- const stamped = stampOriginProjectForImport(hippoRoot, entry);
789
- try {
790
- upsertEntryRow(db, stamped);
791
- }
792
- catch (err) {
793
- if (err instanceof RejectedValueError) {
794
- rejectedCount++;
795
- audit(db, 'reject_refusal', err.entryId, { digest: err.digest, reason: err.reason }, 'cli', err.tenantId);
796
- continue;
797
- }
798
- throw err;
799
- }
800
- }
801
- if (rejectedCount > 0) {
802
- console.error(`bootstrapLegacyStore: skipped ${rejectedCount} rejected value(s) found in legacy mirrors`);
803
- }
804
- const legacyIndex = loadLegacyIndexFile(hippoRoot);
805
- setMeta(db, 'last_retrieval_ids', JSON.stringify(legacyIndex.last_retrieval_ids ?? []));
806
- // LC1: legacy index.json predates last_trace_id, so this is '' for every
807
- // pre-v40 store — harmless, matches the ensureMetaDefaults default.
808
- // Coerce like its neighbors below coerce theirs (independent-review-critic
809
- // LOW finding): accept only a clean digit string, else fall back to ''
810
- // rather than trusting whatever a hand-edited/corrupt index.json carries.
811
- const legacyTraceId = String(legacyIndex.last_trace_id ?? '');
812
- setMeta(db, 'last_trace_id', /^\d+$/.test(legacyTraceId) ? legacyTraceId : '');
813
- const legacyStats = loadLegacyStatsFile(hippoRoot);
814
- setMeta(db, 'total_remembered', String(Number(legacyStats.total_remembered ?? 0)));
815
- setMeta(db, 'total_recalled', String(Number(legacyStats.total_recalled ?? 0)));
816
- setMeta(db, 'total_forgotten', String(Number(legacyStats.total_forgotten ?? 0)));
817
- const runs = Array.isArray(legacyStats.consolidation_runs) ? legacyStats.consolidation_runs : [];
818
- const insertRun = db.prepare(`INSERT INTO consolidation_runs(timestamp, decayed, merged, removed) VALUES (?, ?, ?, ?)`);
819
- for (const run of runs) {
820
- if (!isPlainJsonObject(run))
821
- continue;
822
- const row = run;
823
- insertRun.run(String(row.timestamp ?? new Date().toISOString()), Number(row.decayed ?? 0), Number(row.merged ?? 0), Number(row.removed ?? 0));
824
- }
825
- // AT1 P2 fix: stamp completion regardless of how many rows actually
826
- // landed (all-rejected included) — see the gate comment above.
827
- setMeta(db, 'legacy_bootstrap_completed', '1');
828
- db.exec('COMMIT');
829
- }
830
- catch (error) {
831
- try {
832
- db.exec('ROLLBACK');
833
- }
834
- catch { /* already rolled back; keep the original error */ }
835
- throw error;
836
- }
837
- return true;
838
- }
839
- function loadLegacyEntriesFromMarkdown(hippoRoot) {
840
- const entries = [];
841
- for (const layer of [Layer.Buffer, Layer.Episodic, Layer.Semantic]) {
842
- const dir = layerDir(hippoRoot, layer);
843
- if (!fs.existsSync(dir))
844
- continue;
845
- for (const file of fs.readdirSync(dir)) {
846
- if (!file.endsWith('.md'))
847
- continue;
848
- const raw = fs.readFileSync(path.join(dir, file), 'utf8');
849
- const entry = deserializeEntry(raw);
850
- if (entry)
851
- entries.push(entry);
852
- }
853
- }
854
- return entries;
855
- }
856
- function loadLegacyIndexFile(hippoRoot) {
857
- const indexPath = path.join(hippoRoot, 'index.json');
858
- if (!fs.existsSync(indexPath)) {
859
- return { version: 1, entries: {}, last_retrieval_ids: [], last_trace_id: null };
860
- }
861
- try {
862
- // SAFETY: index.json is only ever written by writeIndexMirror below,
863
- // which always serializes a HippoIndex; a hand-edited or corrupted file
864
- // that violates the shape falls through to the catch block's fallback.
865
- return JSON.parse(fs.readFileSync(indexPath, 'utf8'));
866
- }
867
- catch {
868
- return { version: 1, entries: {}, last_retrieval_ids: [], last_trace_id: null };
869
- }
870
- }
871
- function loadLegacyStatsFile(hippoRoot) {
872
- const statsPath = path.join(hippoRoot, 'stats.json');
873
- if (!fs.existsSync(statsPath)) {
874
- return {
875
- total_remembered: 0,
876
- total_recalled: 0,
877
- total_forgotten: 0,
878
- consolidation_runs: [],
879
- };
880
- }
881
- try {
882
- // SAFETY: stats.json is only ever written by writeStatsMirror below,
883
- // which always emits exactly these four fields; callers additionally
884
- // guard every read with `?? 0` / `Array.isArray`, tolerating a
885
- // hand-edited or corrupted file even if this optimistic cast is wrong.
886
- return JSON.parse(fs.readFileSync(statsPath, 'utf8'));
887
- }
888
- catch {
889
- return {
890
- total_remembered: 0,
891
- total_recalled: 0,
892
- total_forgotten: 0,
893
- consolidation_runs: [],
894
- };
895
- }
896
- }
897
- /**
898
- * `bypassRejectionGuard` (AT1, plan §3): ONLY `batchWriteAndDelete`'s call
899
- * site passes `true`. Consolidation merges are DETERMINISTIC CONCATENATION
900
- * (mergeContents, consolidate.ts:736-751), not LLM paraphrase — the bypass
901
- * is safe because the producer (consolidate.ts's merge pass) now checks the
902
- * merged content's rejection digest against the tenant's tombstones BEFORE
903
- * ever assembling a batch to write, and skips the merge entirely on a hit.
904
- * Every other caller (writeEntryDbOnly, bootstrapLegacyStore, rebuildIndex)
905
- * leaves this false and the guard runs live.
906
- *
907
- * AT1 P1 fix (codex, batch-transaction rejection race): the producer check
908
- * above runs on a DIFFERENT connection BEFORE this transaction opens — a
909
- * `hippo reject X` that commits in that window is invisible to it. This
910
- * parameter's contract is UNCHANGED (still the sole bypass, still trusted
911
- * by the producer-side check for the common case); what changed is that
912
- * `batchWriteAndDelete` no longer trusts it BLINDLY. It now runs its own
913
- * in-transaction point-probe (same connection, same digest lookup this
914
- * function's guard would have done) immediately before each upsert and
915
- * skips — rather than writes — any entry whose content matches a tombstone
916
- * that landed after the producer's check. See batchWriteAndDelete for the
917
- * skip logic.
918
- */
919
- function upsertEntryRow(db, entry, bypassRejectionGuard = false) {
920
- if (!bypassRejectionGuard) {
921
- checkRejectionGuard(db, entry.tenantId ?? 'default', entry.id, entry.content);
922
- }
923
- db.prepare(`
924
- INSERT INTO memories(
925
- id, created, last_retrieved, retrieval_count, strength, half_life_days, layer,
926
- tags_json, emotional_valence, schema_fit, source, outcome_score,
927
- outcome_positive, outcome_negative,
928
- conflicts_with_json, pinned, confidence, content,
929
- parents_json, starred,
930
- trace_outcome, source_session_id,
931
- valid_from, superseded_by,
932
- extracted_from,
933
- dag_level, dag_parent_id,
934
- kind, scope, owner, artifact_ref,
935
- tenant_id, origin_project,
936
- descendant_count, earliest_at, latest_at,
937
- dag_level_3_built_at,
938
- updated_at
939
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
940
- ON CONFLICT(id) DO UPDATE SET
941
- created = excluded.created,
942
- last_retrieved = excluded.last_retrieved,
943
- retrieval_count = excluded.retrieval_count,
944
- strength = excluded.strength,
945
- half_life_days = excluded.half_life_days,
946
- layer = excluded.layer,
947
- tags_json = excluded.tags_json,
948
- emotional_valence = excluded.emotional_valence,
949
- schema_fit = excluded.schema_fit,
950
- source = excluded.source,
951
- outcome_score = excluded.outcome_score,
952
- outcome_positive = excluded.outcome_positive,
953
- outcome_negative = excluded.outcome_negative,
954
- conflicts_with_json = excluded.conflicts_with_json,
955
- pinned = excluded.pinned,
956
- confidence = excluded.confidence,
957
- content = excluded.content,
958
- parents_json = excluded.parents_json,
959
- starred = excluded.starred,
960
- trace_outcome = excluded.trace_outcome,
961
- source_session_id = excluded.source_session_id,
962
- valid_from = excluded.valid_from,
963
- superseded_by = excluded.superseded_by,
964
- extracted_from = excluded.extracted_from,
965
- dag_level = excluded.dag_level,
966
- dag_parent_id = excluded.dag_parent_id,
967
- kind = excluded.kind,
968
- scope = excluded.scope,
969
- owner = excluded.owner,
970
- artifact_ref = excluded.artifact_ref,
971
- tenant_id = excluded.tenant_id,
972
- origin_project = excluded.origin_project,
973
- descendant_count = excluded.descendant_count,
974
- earliest_at = excluded.earliest_at,
975
- latest_at = excluded.latest_at,
976
- dag_level_3_built_at = excluded.dag_level_3_built_at,
977
- updated_at = datetime('now')
978
- `).run(entry.id, entry.created, entry.last_retrieved, entry.retrieval_count, entry.strength, entry.half_life_days, entry.layer, JSON.stringify(entry.tags ?? []), entry.emotional_valence, entry.schema_fit, entry.source, entry.outcome_score, entry.outcome_positive ?? 0, entry.outcome_negative ?? 0, JSON.stringify(entry.conflicts_with ?? []), entry.pinned ? 1 : 0, entry.confidence, entry.content, JSON.stringify(entry.parents ?? []), entry.starred ? 1 : 0, entry.trace_outcome ?? null, entry.source_session_id ?? null, entry.valid_from ?? entry.created, entry.superseded_by ?? null, entry.extracted_from ?? null, entry.dag_level ?? 0, entry.dag_parent_id ?? null, entry.kind ?? 'distilled', entry.scope ?? null, entry.owner ?? null, entry.artifact_ref ?? null, entry.tenantId ?? 'default', entry.origin_project ?? null, entry.descendant_count ?? 0, entry.earliest_at ?? null, entry.latest_at ?? null, entry.dag_level_3_built_at ?? null);
979
- syncFtsRow(db, entry);
980
- }
981
- function syncFtsRow(db, entry) {
982
- if (!isFtsAvailable(db))
983
- return;
984
- try {
985
- db.prepare(`DELETE FROM memories_fts WHERE id = ?`).run(entry.id);
986
- db.prepare(`INSERT INTO memories_fts(id, content, tags) VALUES (?, ?, ?)`).run(entry.id, entry.content, entry.tags.join(' '));
987
- }
988
- catch {
989
- // Best effort only. SQLite store is still authoritative even if FTS is unavailable.
990
- }
991
- }
992
- function deleteFtsRow(db, id) {
993
- if (!isFtsAvailable(db))
994
- return;
995
- try {
996
- db.prepare(`DELETE FROM memories_fts WHERE id = ?`).run(id);
997
- }
998
- catch {
999
- // Best effort.
1000
- }
1001
- }
1002
- /**
1003
- * Derive the current `HippoIndex` (entries + last-retrieval/trace lockstep
1004
- * meta) from SQLite, the source of truth. Exported (AT1) for the same
1005
- * reason as `writeIndexMirror` below: `src/reject-flow.ts` needs to rebuild
1006
- * the index mirror post-commit after a (possibly multi-row) reject removal,
1007
- * without duplicating this query.
1008
- */
1009
- export function buildIndexFromDb(db) {
1010
- // SAFETY: rows' shape matches the seven columns named in the SELECT below.
1011
- const rows = db.prepare(`SELECT id, created, last_retrieved, strength, layer, tags_json, pinned FROM memories ORDER BY created ASC, id ASC`).all();
1012
- const entries = {};
1013
- for (const row of rows) {
1014
- // SAFETY: layer is only ever written from the Layer enum by this
1015
- // module's own INSERT/UPDATE paths.
1016
- const layer = row.layer;
1017
- entries[row.id] = {
1018
- id: row.id,
1019
- file: path.join(layer, `${row.id}.md`),
1020
- layer,
1021
- strength: Number(row.strength ?? 0),
1022
- tags: parseJsonArray(row.tags_json),
1023
- created: row.created,
1024
- last_retrieved: row.last_retrieved,
1025
- pinned: Boolean(row.pinned),
1026
- };
1027
- }
1028
- // LC1 codex round-2 med: the two lockstep keys must be read in ONE
1029
- // statement. Two autocommit SELECTs leave a window where a concurrent
1030
- // saveIndex (which commits both keys in one transaction) lands between
1031
- // them, handing the reader mismatched last_retrieval_ids / last_trace_id
1032
- // and re-opening the mislinkage hole saveIndex's BEGIN/COMMIT closed on
1033
- // the write side. One SELECT = one SQLite read snapshot.
1034
- // SAFETY: lockstepRows' shape matches the key/value columns named above.
1035
- const lockstepRows = db.prepare(`SELECT key, value FROM meta WHERE key IN ('last_retrieval_ids', 'last_trace_id')`).all();
1036
- const lockstep = new Map(lockstepRows.map((r) => [r.key, r.value]));
1037
- return {
1038
- version: INDEX_VERSION,
1039
- entries,
1040
- last_retrieval_ids: parseJsonArray(lockstep.get('last_retrieval_ids') ?? '[]'),
1041
- last_trace_id: parseLastTraceId(lockstep.get('last_trace_id') ?? ''),
1042
- };
1043
- }
1044
- function buildStatsFromDb(db) {
1045
- // SAFETY: runs' shape matches the four columns named in the SELECT above.
1046
- const runs = db.prepare(`SELECT timestamp, decayed, merged, removed FROM consolidation_runs ORDER BY timestamp ASC, id ASC`).all();
1047
- return {
1048
- total_remembered: Number(getMeta(db, 'total_remembered', '0')),
1049
- total_recalled: Number(getMeta(db, 'total_recalled', '0')),
1050
- total_forgotten: Number(getMeta(db, 'total_forgotten', '0')),
1051
- consolidation_runs: runs.map((run) => ({
1052
- timestamp: run.timestamp,
1053
- decayed: run.decayed,
1054
- merged: run.merged,
1055
- removed: run.removed,
1056
- })),
1057
- };
1058
- }
1059
- /**
1060
- * Write the `index.json` mirror file for a given (already-derived) index.
1061
- * Exported (AT1) so `src/reject-flow.ts` can replicate `deleteEntry`'s exact
1062
- * post-commit "removeEntryMirrors then rewrite the index once" sequence for
1063
- * the reject verb's (possibly multi-row) removal, without duplicating
1064
- * `buildIndexFromDb`'s query.
1065
- */
1066
- export function writeIndexMirror(hippoRoot, index) {
1067
- fs.writeFileSync(path.join(hippoRoot, 'index.json'), JSON.stringify(index, null, 2), 'utf8');
1068
- }
1069
- function writeStatsMirror(hippoRoot, stats) {
1070
- fs.writeFileSync(path.join(hippoRoot, 'stats.json'), JSON.stringify(stats, null, 2), 'utf8');
1071
- }
1072
- function syncMirrorFiles(hippoRoot, db) {
1073
- // SAFETY: this query selects exactly MEMORY_SELECT_COLUMNS, matching
1074
- // MemoryRow's field set.
1075
- const entries = db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories ORDER BY created ASC, id ASC`).all();
1076
- for (const entry of entries.map(rowToEntry)) {
1077
- writeMarkdownMirror(hippoRoot, entry);
1078
- }
1079
- // SAFETY: conflicts' shape matches the eight columns named in the SELECT
1080
- // above.
1081
- const conflicts = db.prepare(`
1082
- SELECT id, memory_a_id, memory_b_id, reason, score, status, detected_at, updated_at
1083
- FROM memory_conflicts
1084
- WHERE status = 'open'
1085
- ORDER BY updated_at DESC, id DESC
1086
- `).all();
1087
- writeConflictMirrors(hippoRoot, conflicts.map(rowToMemoryConflict));
1088
- writeIndexMirror(hippoRoot, buildIndexFromDb(db));
1089
- writeStatsMirror(hippoRoot, buildStatsFromDb(db));
1090
- }
1091
- /**
1092
- * Load the current derived index from SQLite and refresh the mirror file.
1093
- */
1094
- export function loadIndex(hippoRoot) {
1095
- initStore(hippoRoot);
1096
- const db = openHippoDb(hippoRoot);
1097
- try {
1098
- const index = buildIndexFromDb(db);
1099
- writeIndexMirror(hippoRoot, index);
1100
- return index;
1101
- }
1102
- finally {
1103
- closeHippoDb(db);
1104
- }
1105
- }
1106
- /**
1107
- * Persist mutable index metadata. Entry rows themselves are derived from SQLite.
1108
- *
1109
- * LC1 F1(c) structural fix: `last_retrieval_ids` and `last_trace_id` must
1110
- * land atomically — callers (getContext, cmdRecall) fold a freshly-written
1111
- * trace id into `index.last_trace_id` before calling this, relying on BOTH
1112
- * meta keys committing together. Wrapped in BEGIN/COMMIT so a crash or a
1113
- * mid-write failure can never advance one key without the other. The
1114
- * filesystem mirror write stays AFTER commit — the DB is the source of
1115
- * truth, the mirror is best-effort (matches every other per-call-handle
1116
- * site's convention).
1117
- */
1118
- export function saveIndex(hippoRoot, index) {
1119
- initStore(hippoRoot);
1120
- const db = openHippoDb(hippoRoot);
1121
- try {
1122
- db.exec('BEGIN');
1123
- try {
1124
- setMeta(db, 'last_retrieval_ids', JSON.stringify(index.last_retrieval_ids ?? []));
1125
- setMeta(db, 'last_trace_id', index.last_trace_id ?? '');
1126
- db.exec('COMMIT');
1127
- }
1128
- catch (error) {
1129
- try {
1130
- db.exec('ROLLBACK');
1131
- }
1132
- catch { /* already rolled back; keep the original error */ }
1133
- throw error;
1134
- }
1135
- writeIndexMirror(hippoRoot, buildIndexFromDb(db));
1136
- }
1137
- finally {
1138
- closeHippoDb(db);
1139
- }
1140
- }
1141
- /**
1142
- * Write a memory entry to SQLite and refresh compatibility mirrors.
1143
- *
1144
- * `opts.actor` defaults to 'cli' so unauthenticated direct-CLI callers still
1145
- * get the right audit attribution. The HTTP server (A1) and api.* layer pass
1146
- * the resolved actor (`api_key:<key_id>` / `localhost:cli`) so audit events
1147
- * land with one row per write, no double-emit.
1148
- *
1149
- * `opts.afterWrite` is invoked inside the same SAVEPOINT as the memories
1150
- * INSERT (mirrors archiveRawMemory's shape in raw-archive.ts). On callback
1151
- * throw, the SAVEPOINT rolls back — the memory row never lands, and the
1152
- * filesystem mirrors / audit emit never run. Used by E1.3+ connectors to
1153
- * stamp idempotency rows atomically with the memory write.
1154
- */
1155
- /**
1156
- * Stamp origin_project from the store's own location when the entry has
1157
- * never been stamped (v39 memory scope isolation). The store dir is
1158
- * `<project>/.hippo`, so its parent resolves to the owning project; the
1159
- * home/global store resolves to '' (user-global). Callers that know a better
1160
- * origin (shareMemory, syncGlobalToLocal) set entry.origin_project before
1161
- * writing and this is a no-op. Returns a stamped copy; never mutates.
1162
- *
1163
- * NULL is deliberately PRESERVED, not re-stamped: null means "legacy row the
1164
- * v39 migration found no evidence for" and is deny-by-default in ambient
1165
- * context. A writeback (e.g. markRetrieved on a crossProject-included row)
1166
- * must not launder it into an injectable origin - the migration is the only
1167
- * evidence-based NULL converter (codex gating round 2 P1).
1168
- */
1169
- export function stampOriginProject(hippoRoot, entry) {
1170
- if (entry.origin_project !== undefined)
1171
- return entry;
1172
- return { ...entry, origin_project: deriveOriginProject(path.dirname(hippoRoot)) };
1173
- }
1174
- /**
1175
- * Import-time variant that ALSO stamps null: used only where evidence exists
1176
- * for rows that predate the origin column - the legacy-markdown bootstrap and
1177
- * rebuildIndex import, which are the markdown-store equivalent of the v39 SQL
1178
- * backfill. Same evidence order as the migration: the provenance source
1179
- * (`shared:<project>:` / `promoted:<localRoot>`) wins over the destination
1180
- * store's location, so a shared row imported into the global store keeps its
1181
- * owning project instead of becoming user-global (codex gating round 3 P1).
1182
- */
1183
- function stampOriginProjectForImport(hippoRoot, entry) {
1184
- if (entry.origin_project !== undefined && entry.origin_project !== null)
1185
- return entry;
1186
- const fromSource = originFromSource(entry.source);
1187
- return {
1188
- ...entry,
1189
- origin_project: fromSource ?? deriveOriginProject(path.dirname(hippoRoot)),
1190
- };
1191
- }
1192
- export function writeEntry(hippoRoot, entry, opts) {
1193
- initStore(hippoRoot);
1194
- const stamped = stampOriginProject(hippoRoot, entry);
1195
- const db = openHippoDb(hippoRoot);
1196
- try {
1197
- writeEntryDbOnly(db, stamped, opts);
1198
- opts?.afterCommit?.();
1199
- writeEntryMirrors(hippoRoot, db, stamped);
1200
- }
1201
- catch (error) {
1202
- // AT1 (plan §3): writeEntryDbOnly's own SAVEPOINT has already unwound by
1203
- // the time this catch runs, so the refusal audit lands post-rollback in
1204
- // a fresh implicit transaction — then rethrow so the caller sees the
1205
- // refusal.
1206
- if (error instanceof RejectedValueError) {
1207
- auditRejectionRefusal(db, error, opts?.actor ?? 'cli');
1208
- }
1209
- throw error;
1210
- }
1211
- finally {
1212
- closeHippoDb(db);
1213
- }
1214
- }
1215
- /**
1216
- * DB-only write path. Caller owns the open `db` handle. Runs SAVEPOINT +
1217
- * upsert + afterWrite hook + audit row inside the SAVEPOINT scope. Caller
1218
- * is responsible for opening `db`, optionally wrapping in a larger BEGIN/
1219
- * COMMIT (e.g. supersede's BEGIN IMMEDIATE), closing `db`, AND calling
1220
- * `writeEntryMirrors` after the larger tx commits — mirrors must run
1221
- * post-commit so a rolled-back tx never leaves orphan markdown.
1222
- *
1223
- * Audit-order note: the audit row is emitted INSIDE the SAVEPOINT, so audit
1224
- * commits atomically with the row INSERT. A subsequent mirror failure cannot
1225
- * leave a recorded audit entry without its corresponding DB row. This is a
1226
- * documented hardening over the prior writeEntry-as-monolith ordering.
1227
- */
1228
- export function writeEntryDbOnly(db, entry, opts) {
1229
- // SAVEPOINT (not BEGIN) so this nests safely inside any outer transaction
1230
- // a caller might hold (e.g. supersede's BEGIN IMMEDIATE). SQLite refuses
1231
- // BEGIN within a transaction; SAVEPOINT is the only way to scope rollback
1232
- // without disturbing outers.
1233
- db.exec('SAVEPOINT write_entry');
1234
- try {
1235
- upsertEntryRow(db, entry);
1236
- if (opts?.afterWrite) {
1237
- opts.afterWrite(db, entry.id);
1238
- }
1239
- audit(db, 'remember', entry.id, {
1240
- kind: entry.kind ?? 'distilled',
1241
- scope: entry.scope ?? null,
1242
- }, opts?.actor ?? 'cli', entry.tenantId);
1243
- // v0.30 / E2 — DAG live-coupling: child write under a level-2 summary
1244
- // marks the parent dirty for E3 sleep-cycle rebuild. Early-exit on
1245
- // null dag_parent_id (vast majority of writes); cost is one null check
1246
- // on the hot path.
1247
- if (entry.dag_parent_id) {
1248
- markSummaryDirtyInTx(db, entry.dag_parent_id, entry.tenantId, opts?.actor ?? 'cli');
1249
- }
1250
- db.exec('RELEASE SAVEPOINT write_entry');
1251
- }
1252
- catch (e) {
1253
- try {
1254
- db.exec('ROLLBACK TO SAVEPOINT write_entry');
1255
- db.exec('RELEASE SAVEPOINT write_entry');
1256
- }
1257
- catch {
1258
- // Ignore rollback failures — the throw below is what matters.
1259
- }
1260
- throw e;
1261
- }
1262
- }
1263
- /**
1264
- * Filesystem mirrors path. Caller passes `hippoRoot` + an open `db` handle
1265
- * (used by `buildIndexFromDb` to derive the index from the source of truth).
1266
- * MUST be invoked AFTER the outer transaction commits — a mirror write
1267
- * during a tx that subsequently rolls back would leave orphan markdown.
1268
- */
1269
- export function writeEntryMirrors(hippoRoot, db, entry) {
1270
- writeMarkdownMirror(hippoRoot, entry);
1271
- writeIndexMirror(hippoRoot, buildIndexFromDb(db));
1272
- }
1273
- /**
1274
- * Read a memory entry by ID.
1275
- *
1276
- * When `tenantId` is provided, the read is scoped to that tenant (cross-tenant
1277
- * lookups return null). When omitted, no tenant filter is applied — preserves
1278
- * legacy single-tenant callers and the writeEntry/readEntry round-trip.
1279
- */
1280
- export function readEntry(hippoRoot, id, tenantId) {
1281
- initStore(hippoRoot);
1282
- const db = openHippoDb(hippoRoot);
1283
- try {
1284
- // SAFETY: both branches select exactly MEMORY_SELECT_COLUMNS, matching
1285
- // MemoryRow's field set.
1286
- const row = tenantId !== undefined
1287
- ? db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE id = ? AND tenant_id = ?`).get(id, tenantId)
1288
- : db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE id = ?`).get(id);
1289
- return row ? rowToEntry(row) : null;
1290
- }
1291
- finally {
1292
- closeHippoDb(db);
1293
- }
1294
- }
1295
- /**
1296
- * Batched lookup. Caps at 500 ids per call to keep the IN(?,?,...) clause
1297
- * within SQLite limits. Tenant filter is enforced when `tenantId` is passed.
1298
- * Used by DAG-aware recall (docs/plans/2026-05-05-dag-recall.md Task 1.5)
1299
- * to fetch parent summaries for a set of overflowed leaves.
1300
- */
1301
- export function loadEntriesByIds(hippoRoot, ids, tenantId) {
1302
- if (ids.length === 0)
1303
- return [];
1304
- const capped = ids.slice(0, 500);
1305
- initStore(hippoRoot);
1306
- const db = openHippoDb(hippoRoot);
1307
- try {
1308
- const placeholders = capped.map(() => '?').join(',');
1309
- // T2: no ORDER BY meant row order followed SQLite's IN(...) scan order
1310
- // (undefined w.r.t. the caller's `ids` order). created ASC, id ASC
1311
- // makes it deterministic.
1312
- // SAFETY: both branches select exactly MEMORY_SELECT_COLUMNS, matching
1313
- // MemoryRow's field set.
1314
- const rows = tenantId !== undefined
1315
- ? db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE id IN (${placeholders}) AND tenant_id = ? ORDER BY created ASC, content ASC, id ASC`).all(...capped, tenantId)
1316
- : db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE id IN (${placeholders}) ORDER BY created ASC, content ASC, id ASC`).all(...capped);
1317
- return rows.map(rowToEntry);
1318
- }
1319
- finally {
1320
- closeHippoDb(db);
1321
- }
1322
- }
1323
- /**
1324
- * All `kind='raw'` rows for a given session, tenant-scoped, returned
1325
- * oldest-first. Used by `api.assemble` to walk a session's chronological
1326
- * context. Excludes superseded rows.
1327
- *
1328
- * Cap semantics (v1.6.2 codex fix): when `cap` is provided, the NEWEST
1329
- * `cap` rows are loaded — `ORDER BY created DESC LIMIT cap` server-side,
1330
- * reversed to oldest-first client-side. Pre-v1.6.2 ordered ASC + LIMIT,
1331
- * which silently dropped the newest rows and broke fresh-tail in assemble.
1332
- *
1333
- * Returns `[]` for an empty sessionId. Final order: `created ASC, id ASC`.
1334
- */
1335
- export function loadSessionRawMemories(hippoRoot, sessionId, tenantId, cap) {
1336
- if (!sessionId)
1337
- return [];
1338
- initStore(hippoRoot);
1339
- const db = openHippoDb(hippoRoot);
1340
- try {
1341
- const params = [];
1342
- let sql = `SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE kind = 'raw' AND source_session_id = ? AND superseded_by IS NULL`;
1343
- params.push(sessionId);
1344
- if (tenantId !== undefined) {
1345
- sql += ' AND tenant_id = ?';
1346
- params.push(tenantId);
1347
- }
1348
- if (cap !== undefined && cap > 0) {
1349
- sql += ' ORDER BY created DESC, id DESC LIMIT ?';
1350
- params.push(cap);
1351
- // SAFETY: sql starts from MEMORY_SELECT_COLUMNS, matching MemoryRow.
1352
- const rows = db.prepare(sql).all(...params);
1353
- return rows.reverse().map(rowToEntry);
1354
- }
1355
- sql += ' ORDER BY created ASC, id ASC';
1356
- // SAFETY: sql starts from MEMORY_SELECT_COLUMNS, matching MemoryRow.
1357
- const rows = db.prepare(sql).all(...params);
1358
- return rows.map(rowToEntry);
1359
- }
1360
- finally {
1361
- closeHippoDb(db);
1362
- }
1363
- }
1364
- /**
1365
- * Pre-cap, scope-aware row count for a session. Lets `assemble` report
1366
- * the full session size even when `rowCap` truncates the loaded window,
1367
- * WITHOUT leaking rows the caller wouldn't have been allowed to load.
1368
- *
1369
- * v1.6.3 codex P1 / senior P0: an earlier draft of this helper ran an
1370
- * unscoped COUNT, which let a no-scope caller infer the existence of
1371
- * private rows by comparing `totalRaw` against `items.length`. This
1372
- * version SQL-encodes the same default-deny rule `passesScopeFilterForRecall`
1373
- * applies in TS:
1374
- * - explicit scope passed: exact-match
1375
- * - no scope: rows where scope IS NULL, or scope is NOT a `<source>:private:*`
1376
- * pattern AND not the `unknown:legacy` quarantine bucket.
1377
- *
1378
- * `tenantId` is optional for back-compat. Pass `undefined` only when
1379
- * intentionally counting cross-tenant; `assemble()` passes `ctx.tenantId`.
1380
- */
1381
- export function countSessionRawMemories(hippoRoot, sessionId, tenantId, scope) {
1382
- if (!sessionId)
1383
- return 0;
1384
- initStore(hippoRoot);
1385
- const db = openHippoDb(hippoRoot);
1386
- try {
1387
- const params = [];
1388
- let sql = `SELECT COUNT(*) AS c FROM memories WHERE kind = 'raw' AND source_session_id = ? AND superseded_by IS NULL`;
1389
- params.push(sessionId);
1390
- if (tenantId !== undefined) {
1391
- sql += ' AND tenant_id = ?';
1392
- params.push(tenantId);
1393
- }
1394
- if (scope !== undefined && scope !== '') {
1395
- sql += ' AND scope = ?';
1396
- params.push(scope);
1397
- }
1398
- else {
1399
- // SQL-ify the TS default-deny: scope IS NULL OR (NOT LIKE '%:private:%'
1400
- // AND != 'unknown:legacy'). Mirrors api.passesScopeFilterForRecall.
1401
- sql += ` AND (scope IS NULL OR (scope NOT LIKE '%:private:%' AND scope != 'unknown:legacy'))`;
1402
- }
1403
- // SAFETY: row's shape matches the single `COUNT(*) AS c` column above.
1404
- const row = db.prepare(sql).get(...params);
1405
- return Number(row?.c ?? 0);
1406
- }
1407
- finally {
1408
- closeHippoDb(db);
1409
- }
1410
- }
1411
- /**
1412
- * Last N kind='raw' memories by `created` desc. Tenant scoped. When
1413
- * `sessionId` is supplied, also constrains to a specific session — that
1414
- * is the correct shape for "what did I just see in THIS session."
1415
- *
1416
- * v1.6.2 codex review fix: pre-v1.6.2 was tenant-wide only. With multiple
1417
- * concurrent sessions in a tenant, fresh-tail recall surfaced unrelated
1418
- * rows from other sessions and stamped them `isFreshTail=true`. Callers
1419
- * that want session-scoped fresh-tail now pass `sessionId`. The
1420
- * tenant-wide form (no sessionId) still exists for "anything new across
1421
- * the whole tenant" — pass undefined to opt in.
1422
- *
1423
- * Bounded count cap at 200 — beyond that the caller should filter via
1424
- * tags/scope rather than time-windowed recall.
1425
- *
1426
- * Deprecation note (v1.6.5) — the **tenant-wide call shape** (omitting
1427
- * `sessionId`) is rarely the right shape for "what did I just see in this
1428
- * conversation". `api.recall` enforces session scoping when
1429
- * `HIPPO_REQUIRE_SESSION_SCOPED_FRESH_TAIL=1` is set, throwing
1430
- * `RecallContractError` instead. Tenant-wide remains the back-compat default
1431
- * but is discouraged for new callers. Passing `sessionId` is fully supported
1432
- * and recommended; this function is NOT deprecated as a whole.
1433
- */
1434
- export function loadFreshRawMemories(hippoRoot, count, tenantId, sessionId) {
1435
- if (count <= 0)
1436
- return [];
1437
- const capped = Math.min(count, 200);
1438
- initStore(hippoRoot);
1439
- const db = openHippoDb(hippoRoot);
1440
- try {
1441
- const params = [];
1442
- let sql = `SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE kind = 'raw' AND superseded_by IS NULL`;
1443
- if (tenantId !== undefined) {
1444
- sql += ' AND tenant_id = ?';
1445
- params.push(tenantId);
1446
- }
1447
- if (sessionId !== undefined && sessionId !== '') {
1448
- sql += ' AND source_session_id = ?';
1449
- params.push(sessionId);
1450
- }
1451
- // T2: tie tail keeps the LIMIT window keyed on `created` while making
1452
- // same-`created` rows deterministic. `content` before `id` (codex
1453
- // review): ids are random UUIDs, so an id-only tail would pick WHICH
1454
- // same-created rows make the window per-instance; content is
1455
- // cross-ingest-stable.
1456
- sql += ' ORDER BY created DESC, content ASC, id ASC LIMIT ?';
1457
- params.push(capped);
1458
- // SAFETY: sql starts from MEMORY_SELECT_COLUMNS, matching MemoryRow.
1459
- const rows = db.prepare(sql).all(...params);
1460
- return rows.map(rowToEntry);
1461
- }
1462
- finally {
1463
- closeHippoDb(db);
1464
- }
1465
- }
1466
- /**
1467
- * Direct DAG children of a parent summary. Tenant scoped. Returns only rows
1468
- * whose `dag_parent_id` matches `parentId`; does NOT walk recursively.
1469
- * Used by `drillDown` (Task 3).
1470
- */
1471
- export function loadChildrenOf(hippoRoot, parentId, tenantId) {
1472
- initStore(hippoRoot);
1473
- const db = openHippoDb(hippoRoot);
1474
- try {
1475
- // SAFETY: both branches select exactly MEMORY_SELECT_COLUMNS, matching
1476
- // MemoryRow's field set.
1477
- const rows = tenantId !== undefined
1478
- ? db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE dag_parent_id = ? AND tenant_id = ? ORDER BY created ASC, id ASC`).all(parentId, tenantId)
1479
- : db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE dag_parent_id = ? ORDER BY created ASC, id ASC`).all(parentId);
1480
- return rows.map(rowToEntry);
1481
- }
1482
- finally {
1483
- closeHippoDb(db);
1484
- }
1485
- }
1486
- /**
1487
- * AT1 (plan §4, round-2 fix, designed from source): db-scoped delete core.
1488
- * `deleteEntry` used to open+close its OWN connection, which meant it could
1489
- * never compose inside a caller's transaction (unlike writeEntry/
1490
- * writeEntryDbOnly, which already split this way). Split identically: row-
1491
- * meta SELECT, `DELETE FROM memories`, FTS delete, `forget` audit, DAG
1492
- * dirty-mark. NO filesystem I/O — the caller's own transaction may still be
1493
- * rolled back, and mirror writes must only happen post-commit.
1494
- *
1495
- * `opts.suppressForgetAudit` (default false, off): two AT1 callers set this
1496
- * so a removed non-raw row does NOT ALSO emit a `forget` row, because each
1497
- * already writes its own aggregate audit trail — `src/reject-flow.ts`'s
1498
- * `rejectValue` (single `reject_value` row covering every same-digest row
1499
- * removed) and `resolveConflict` (`conflict_resolve` row per resolution).
1500
- * Default keeps `deleteEntry` byte-identical to its pre-split behavior.
1501
- *
1502
- * Returns `{tenantId, dagParentId}` for the removed row, or `null` if no row
1503
- * with `id` existed.
1504
- */
1505
- export function deleteEntryCore(db, id, opts) {
1506
- // SAFETY: row's shape matches the three columns named in the SELECT above.
1507
- const row = db
1508
- .prepare(`SELECT id, tenant_id, dag_parent_id FROM memories WHERE id = ?`)
1509
- .get(id);
1510
- if (!row?.id)
1511
- return null;
1512
- db.prepare(`DELETE FROM memories WHERE id = ?`).run(id);
1513
- deleteFtsRow(db, id);
1514
- if (!opts?.suppressForgetAudit) {
1515
- audit(db, 'forget', id, undefined, opts?.actor ?? 'cli', row.tenant_id);
1516
- }
1517
- // v0.30 / E2 — DAG live-coupling: forget of a child under a level-2
1518
- // summary marks parent dirty. Non-atomic with the DELETE (no SAVEPOINT
1519
- // wrapper here, same as pre-split deleteEntry); markSummaryDirtyInTx is
1520
- // idempotent so any future child mutation re-marks parent if this fails.
1521
- // Acceptable degradation, mirrors the pre-split audit best-effort posture.
1522
- if (row.dag_parent_id) {
1523
- markSummaryDirtyInTx(db, row.dag_parent_id, row.tenant_id ?? 'default', opts?.actor ?? 'cli');
1524
- }
1525
- return { tenantId: row.tenant_id ?? 'default', dagParentId: row.dag_parent_id ?? null };
1526
- }
1527
- /**
1528
- * Delete an entry from SQLite and mirrors.
1529
- *
1530
- * `opts.actor` defaults to 'cli'. The api.* layer threads `ctx.actor` so HTTP
1531
- * callers land with `api_key:<key_id>` in the audit log without a duplicate
1532
- * emit from the api wrapper.
1533
- *
1534
- * Thin wrapper over `deleteEntryCore` (open → core → mirrors → close);
1535
- * behavior is byte-identical to the pre-split implementation for every
1536
- * existing caller.
1537
- */
1538
- export function deleteEntry(hippoRoot, id, opts) {
1539
- initStore(hippoRoot);
1540
- const db = openHippoDb(hippoRoot);
1541
- try {
1542
- const result = deleteEntryCore(db, id, opts);
1543
- if (!result)
1544
- return false;
1545
- removeEntryMirrors(hippoRoot, id);
1546
- writeIndexMirror(hippoRoot, buildIndexFromDb(db));
1547
- return true;
1548
- }
1549
- finally {
1550
- closeHippoDb(db);
1551
- }
1552
- }
1553
- /**
1554
- * Batch-write and batch-delete entries in a single transaction.
1555
- * Used by consolidation to avoid N open/close cycles.
1556
- */
1557
- export function batchWriteAndDelete(hippoRoot, toWrite, toDeleteIds) {
1558
- if (toWrite.length === 0 && toDeleteIds.length === 0)
1559
- return;
1560
- initStore(hippoRoot);
1561
- const db = openHippoDb(hippoRoot);
1562
- try {
1563
- // BEGIN IMMEDIATE (codex delta-review P2): the AT1 tombstone probes below
1564
- // READ before the first write. Under a deferred BEGIN, that read pins a
1565
- // WAL snapshot; a concurrent writer (e.g. `hippo reject`) committing
1566
- // between probe and first upsert would make the later write-lock upgrade
1567
- // fail with SQLITE_BUSY and roll back the ENTIRE batch — the exact race
1568
- // the probe exists to contain. Taking the write lock up front serializes
1569
- // the probe and the writes on one consistent snapshot.
1570
- db.exec('BEGIN IMMEDIATE');
1571
- // v0.30 / E2 — DAG live-coupling: BEFORE deletes, snapshot dag_parent_id
1572
- // for every doomed row so we can mark parents dirty post-COMMIT. Done
1573
- // inside the same BEGIN so the SELECT sees pre-delete state.
1574
- // independent-review-critic R1 HIGH: consolidate.ts/sleep flushes through
1575
- // this path every cycle; without these hooks parents NEVER get marked
1576
- // dirty for the dominant mutation source (decay, merge, garbage-collect).
1577
- const dirtyParents = new Set();
1578
- const tenantById = new Map();
1579
- if (toDeleteIds.length > 0) {
1580
- const placeholders = toDeleteIds.map(() => '?').join(',');
1581
- // SAFETY: rows' shape matches the two columns named in the SELECT
1582
- // above.
1583
- const rows = db.prepare(`SELECT dag_parent_id, tenant_id FROM memories WHERE id IN (${placeholders})`).all(...toDeleteIds);
1584
- for (const row of rows) {
1585
- if (row.dag_parent_id) {
1586
- dirtyParents.add(row.dag_parent_id);
1587
- tenantById.set(row.dag_parent_id, row.tenant_id ?? 'default');
1588
- }
1589
- }
1590
- }
1591
- // v39: consolidate's new semantic rows (and any other batch writer)
1592
- // bypass writeEntry, so stamp store-derived origins here too - a NULL
1593
- // origin would make freshly consolidated memories vanish from ambient
1594
- // context (codex gating review P1).
1595
- const stampedWrites = toWrite.map((e) => stampOriginProject(hippoRoot, e));
1596
- // AT1 P1 fix (codex, batch-transaction rejection race): the producer-side
1597
- // check (e.g. consolidate.ts's merge pass) runs BEFORE this transaction,
1598
- // on a different connection. A `hippo reject X` that commits in that
1599
- // window is invisible to it — a queued same-id write of X already
1600
- // sitting in `toWrite` (decay/replay re-persist, or a merge built before
1601
- // the reject) would silently re-INSERT the just-rejected row via the
1602
- // blind bypass. Fix: one indexed point probe per batch entry, on THIS
1603
- // connection, INSIDE this transaction — closes the race regardless of
1604
- // which write class hits it. N is small per sleep, so the extra query
1605
- // per entry is cheap.
1606
- //
1607
- // Skip, don't throw: the batch must still complete for every OTHER
1608
- // entry. Skipping is correct for every write class here — a merge
1609
- // summary skip just means that rollup is absent this cycle (its source
1610
- // facts stay merely demoted, recoverable next sleep); a skipped
1611
- // demotion/replay re-persist of a rejected-removed row means it stays
1612
- // gone, which is the entire point of the tombstone.
1613
- let batchRejectedSkips = 0;
1614
- const skippedWriteIds = new Set();
1615
- for (const entry of stampedWrites) {
1616
- const entryTenantId = entry.tenantId ?? 'default';
1617
- // Codex delta-review P2 fix: reuse checkRejectionGuard rather than a
1618
- // bare tombstone probe — the guard's content-INTRODUCTION
1619
- // classification must apply here too. A tombstone can legitimately
1620
- // coexist with a live same-content row (resolveConflict deliberately
1621
- // excludes keepId from its sweep; unreject-then-re-reject windows), and
1622
- // an unconditional skip would starve that row of decay/replay metadata
1623
- // updates forever. The guard throws only when the write is new-row or
1624
- // changes content TO the rejected value; unchanged same-id re-persists
1625
- // pass through, exactly as on the writeEntry path.
1626
- try {
1627
- checkRejectionGuard(db, entryTenantId, entry.id, entry.content);
1628
- }
1629
- catch (err) {
1630
- if (err instanceof RejectedValueError) {
1631
- batchRejectedSkips++;
1632
- skippedWriteIds.add(entry.id);
1633
- audit(db, 'reject_refusal', entry.id, { digest: err.digest, reason: err.reason }, 'sleep-batch', entryTenantId);
1634
- continue;
1635
- }
1636
- throw err;
1637
- }
1638
- // AT1 (plan §3, corrected): bypass the rejection guard here.
1639
- // Consolidation merges are DETERMINISTIC CONCATENATION (mergeContents,
1640
- // consolidate.ts:736-751) of already-guarded leaf facts, not an LLM
1641
- // paraphrase — refusing mid-batch would abort the whole consolidation
1642
- // transaction. The bypass is safe because consolidate.ts's merge pass
1643
- // now checks the merged content's rejection digest against the
1644
- // tenant's tombstones BEFORE ever pushing a merge into pendingWrites,
1645
- // skipping that merge entirely on a hit, AND because the point-probe
1646
- // immediately above closes the race window between that producer
1647
- // check and this COMMIT. The guard itself still belongs on leaf
1648
- // inserts, which write through writeEntry / writeEntryDbOnly and stay
1649
- // guarded (bypassRejectionGuard defaults false).
1650
- upsertEntryRow(db, entry, true);
1651
- // Hook for writes: child upserted under a level-2 summary marks parent dirty.
1652
- if (entry.dag_parent_id) {
1653
- dirtyParents.add(entry.dag_parent_id);
1654
- tenantById.set(entry.dag_parent_id, entry.tenantId);
1655
- }
1656
- }
1657
- for (const id of toDeleteIds) {
1658
- db.prepare('DELETE FROM memories WHERE id = ?').run(id);
1659
- deleteFtsRow(db, id);
1660
- }
1661
- // Fire dirty-mark for every collected parent INSIDE the BEGIN, so the
1662
- // dirty flag commits atomically with the writes + deletes.
1663
- for (const parentId of dirtyParents) {
1664
- markSummaryDirtyInTx(db, parentId, tenantById.get(parentId) ?? 'default', 'batch');
1665
- }
1666
- db.exec('COMMIT');
1667
- if (batchRejectedSkips > 0) {
1668
- console.error(`batchWriteAndDelete: skipped ${batchRejectedSkips} write(s) whose content matches a rejected value (tombstone hit during the batch transaction)`);
1669
- }
1670
- // Sync mirrors once after all DB writes. Entries skipped above were
1671
- // never inserted — writing their markdown mirror would resurrect the
1672
- // exact content the skip just kept out of the DB.
1673
- for (const entry of stampedWrites) {
1674
- if (skippedWriteIds.has(entry.id))
1675
- continue;
1676
- writeMarkdownMirror(hippoRoot, entry);
1677
- }
1678
- for (const id of toDeleteIds) {
1679
- removeEntryMirrors(hippoRoot, id);
1680
- }
1681
- writeIndexMirror(hippoRoot, buildIndexFromDb(db));
1682
- }
1683
- catch (error) {
1684
- try {
1685
- db.exec('ROLLBACK');
1686
- }
1687
- catch { /* ignore */ }
1688
- throw error;
1689
- }
1690
- finally {
1691
- closeHippoDb(db);
1692
- }
1693
- }
1694
- /**
1695
- * Load all entries from SQLite.
1696
- *
1697
- * When `tenantId` is provided, results are scoped to that tenant. Omitting it
1698
- * yields all rows (legacy behavior used by consolidate/autolearn etc.). Recall
1699
- * paths that surface results to a user MUST pass a resolved tenant.
1700
- */
1701
- export function loadAllEntries(hippoRoot, tenantId) {
1702
- initStore(hippoRoot);
1703
- const db = openHippoDb(hippoRoot);
1704
- try {
1705
- // SAFETY: both branches select exactly MEMORY_SELECT_COLUMNS, matching
1706
- // MemoryRow's field set.
1707
- const rows = tenantId !== undefined
1708
- ? db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE tenant_id = ? ORDER BY created ASC, id ASC`).all(tenantId)
1709
- : db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories ORDER BY created ASC, id ASC`).all();
1710
- return rows.map(rowToEntry);
1711
- }
1712
- finally {
1713
- closeHippoDb(db);
1714
- }
1715
- }
1716
- // The pins plus the `recentNeeded` newest rows that pass `admit`, for ambient
1717
- // injection. One connection: opening one costs ~5.8ms on a warm 1896-row store,
1718
- // so a second handle loses more than the narrower scan saves.
1719
- export function loadAmbientCandidates(hippoRoot, tenantId, recentNeeded, admit) {
1720
- initStore(hippoRoot);
1721
- // A SQL LIMIT takes an integer; the Array.slice this replaced truncated one,
1722
- // and include_recent is any non-negative finite number at the HTTP edge.
1723
- const needed = Math.trunc(recentNeeded);
1724
- const db = openHippoDb(hippoRoot);
1725
- try {
1726
- // SAFETY: every `where` below starts from MEMORY_SELECT_COLUMNS' table.
1727
- const run = (where, params) => db.prepare(`SELECT ${MEMORY_SELECT_COLUMNS} FROM memories WHERE ${where}`).all(...params).map(rowToEntry);
1728
- const byId = new Map();
1729
- const scoped = 'superseded_by IS NULL AND tenant_id = ?';
1730
- for (const e of run(`pinned = 1 AND ${scoped} ORDER BY created ASC, id ASC`, [tenantId])) {
1731
- if (admit(e))
1732
- byId.set(e.id, e);
1733
- }
1734
- if (needed > 0) {
1735
- // Text order is chronological only for canonical UTC ISO (memory.ts).
1736
- const drifted = db.prepare(`SELECT 1 FROM memories WHERE ${scoped} AND (length(created) <> 24 OR created NOT LIKE '%Z') LIMIT 1`).get(tenantId) !== undefined;
1737
- // `id DESC` mirrors getContext's comparator, not loadFreshRawMemories'
1738
- // cross-ingest-stable order: that would change what the hook injects.
1739
- const window = Math.max(needed * 4, 32);
1740
- const windowed = drifted
1741
- ? []
1742
- : run(`${scoped} ORDER BY created DESC, id DESC LIMIT ?`, [tenantId, window]);
1743
- let kept = windowed.filter(admit);
1744
- if (drifted || (kept.length < needed && windowed.length === window)) {
1745
- kept = run(`${scoped} ORDER BY created DESC, id DESC`, [tenantId]).filter(admit);
1746
- }
1747
- for (const e of kept)
1748
- byId.set(e.id, e);
1749
- }
1750
- // loadAllEntries' order: rankedPinned's comparator can tie and Array.sort
1751
- // is stable, so input order is load-bearing downstream.
1752
- return [...byId.values()].sort((a, b) => {
1753
- const byCreated = a.created.localeCompare(b.created);
1754
- return byCreated !== 0 ? byCreated : a.id.localeCompare(b.id);
1755
- });
1756
- }
1757
- finally {
1758
- closeHippoDb(db);
1759
- }
1760
- }
1761
- /**
1762
- * Load likely search candidates directly from SQLite.
1763
- * Uses FTS5 when available, falls back to LIKE matching, then full-store fallback.
1764
- *
1765
- * When `tenantId` is provided, every SELECT (FTS join, LIKE, fallback) filters
1766
- * by tenant_id. Cross-tenant memories never surface. Omitted = no filter.
1767
- */
1768
- export function loadSearchEntries(hippoRoot, query, limit = DEFAULT_SEARCH_CANDIDATE_LIMIT, tenantId) {
1769
- initStore(hippoRoot);
1770
- const db = openHippoDb(hippoRoot);
1771
- try {
1772
- return loadSearchRows(db, query, limit, tenantId).map(rowToEntry);
1773
- }
1774
- finally {
1775
- closeHippoDb(db);
1776
- }
1777
- }
1778
- /**
1779
- * v1.7.1 — recall-mode loader. Pushes the recall-side scope predicate into
1780
- * SQL so `unknown:legacy` cannot leak via any consumer that hasn't remembered
1781
- * to re-filter (root-cause-over-patches: codex flagged this on v1.6.5 review).
1782
- *
1783
- * - `requestedScope` undefined / '': default-deny on `unknown:legacy`.
1784
- * - `requestedScope` non-empty string: exact match on `m.scope = requestedScope`.
1785
- *
1786
- * Private-scope (`<source>:private:*`) exclusion: SQL applies a conservative
1787
- * pre-window approximation (`NOT LIKE '%:private:%'`, v1.25.0 — codex P2:
1788
- * post-window-only filtering let private rows starve admitted candidates out
1789
- * of the LIMIT window); the exact anchored regex
1790
- * (`passesScopeFilterForRecall`) remains the authoritative JS post-filter in
1791
- * the recall consumers.
1792
- *
1793
- * Consumers: `api.recall` (v1.7.1+), `cmdRecall`/`cmdExplain` direct CLI paths
1794
- * and `searchBothHybrid` recall mode (v1.25.0). Background pipelines
1795
- * (`consolidate`, `embeddings`, `refine-llm`, ...) keep using
1796
- * `loadSearchEntries` so they can see quarantined rows when needed.
1797
- *
1798
- * `tenantId` widened to optional in v1.25.0 for the searchBothHybrid recall
1799
- * mode (its `tenantId` option is optional); `loadSearchRows` already treats
1800
- * undefined as "no tenant filter" for legacy callers.
1801
- */
1802
- export function loadRecallSearchEntries(hippoRoot, query, limit = DEFAULT_SEARCH_CANDIDATE_LIMIT, tenantId, requestedScope, explicitScopeMode = 'exact') {
1803
- initStore(hippoRoot);
1804
- const db = openHippoDb(hippoRoot);
1805
- try {
1806
- // explicitScopeMode only matters when requestedScope is set:
1807
- // 'exact' — api.recall semantics: narrow to m.scope = requested.
1808
- // 'additive' — CLI --scope semantics (v1.25.0): default-admitted set
1809
- // PLUS the requested scope; see RecallScopeFilter docs.
1810
- const scopeFilter = requestedScope && requestedScope !== ''
1811
- ? explicitScopeMode === 'additive'
1812
- ? { mode: 'default-deny-or-exact', value: requestedScope }
1813
- : { mode: 'exact', value: requestedScope }
1814
- : { mode: 'default-deny' };
1815
- return loadSearchRows(db, query, limit, tenantId, scopeFilter).map(rowToEntry);
1816
- }
1817
- finally {
1818
- closeHippoDb(db);
1819
- }
1820
- }
1821
- /**
1822
- * Rebuild mirrors from SQLite, importing any legacy markdown files not already present.
1823
- */
1824
- export function rebuildIndex(hippoRoot) {
1825
- initStore(hippoRoot);
1826
- const db = openHippoDb(hippoRoot);
1827
- try {
1828
- // SAFETY: rows' shape matches the single `id` column selected above.
1829
- const existingIds = new Set(db.prepare(`SELECT id FROM memories`).all().map((row) => row.id));
1830
- const legacyEntries = loadLegacyEntriesFromMarkdown(hippoRoot).filter((entry) => !existingIds.has(entry.id));
1831
- if (legacyEntries.length > 0) {
1832
- db.exec('BEGIN');
1833
- try {
1834
- // AT1 (plan §3, round-3 redesign): same guard-with-per-row-skip as
1835
- // bootstrapLegacyStore — rebuildIndex is the other channel through
1836
- // which a stale markdown mirror could resurrect a rejected value.
1837
- // Refusal audit written INLINE (nothing rolls back on a skip).
1838
- let rejectedCount = 0;
1839
- for (const entry of legacyEntries) {
1840
- // v39: same store-derived origin stamp as bootstrapLegacyStore.
1841
- const stamped = stampOriginProjectForImport(hippoRoot, entry);
1842
- try {
1843
- upsertEntryRow(db, stamped);
1844
- }
1845
- catch (err) {
1846
- if (err instanceof RejectedValueError) {
1847
- rejectedCount++;
1848
- audit(db, 'reject_refusal', err.entryId, { digest: err.digest, reason: err.reason }, 'cli', err.tenantId);
1849
- continue;
1850
- }
1851
- throw err;
1852
- }
1853
- }
1854
- if (rejectedCount > 0) {
1855
- console.error(`rebuildIndex: skipped ${rejectedCount} rejected value(s) found in legacy mirrors`);
1856
- }
1857
- db.exec('COMMIT');
1858
- }
1859
- catch (err) {
1860
- try {
1861
- db.exec('ROLLBACK');
1862
- }
1863
- catch { /* ignore if no active txn */ }
1864
- throw err;
1865
- }
1866
- }
1867
- syncMirrorFiles(hippoRoot, db);
1868
- return buildIndexFromDb(db);
1869
- }
1870
- finally {
1871
- closeHippoDb(db);
1872
- }
1873
- }
1874
- export function updateStats(hippoRoot, delta) {
1875
- initStore(hippoRoot);
1876
- const db = openHippoDb(hippoRoot);
1877
- try {
1878
- // One atomic statement per counter, and only for counters the caller
1879
- // named: the read-modify-write this replaces both lost increments to a
1880
- // concurrent writer and stamped stale values over the untouched two.
1881
- const increments = [
1882
- ['total_remembered', delta.remembered ?? 0],
1883
- ['total_recalled', delta.recalled ?? 0],
1884
- ['total_forgotten', delta.forgotten ?? 0],
1885
- ];
1886
- for (const [key, amount] of increments) {
1887
- if (amount === 0)
1888
- continue;
1889
- // Both binds are the same string: node:sqlite binds a JS number as REAL,
1890
- // which would store "1.0" into this TEXT column instead of "1".
1891
- db.prepare(`
1892
- INSERT INTO meta(key, value) VALUES(?, ?)
1893
- ON CONFLICT(key) DO UPDATE SET value = CAST(meta.value AS INTEGER) + CAST(? AS INTEGER)
1894
- `).run(key, String(amount), String(amount));
1895
- }
1896
- writeStatsMirror(hippoRoot, buildStatsFromDb(db));
1897
- }
1898
- finally {
1899
- closeHippoDb(db);
1900
- }
1901
- }
1902
- export function loadStats(hippoRoot) {
1903
- initStore(hippoRoot);
1904
- const db = openHippoDb(hippoRoot);
1905
- try {
1906
- const stats = buildStatsFromDb(db);
1907
- writeStatsMirror(hippoRoot, stats);
1908
- return stats;
1909
- }
1910
- finally {
1911
- closeHippoDb(db);
1912
- }
1913
- }
1914
- export function appendConsolidationRun(hippoRoot, run) {
1915
- initStore(hippoRoot);
1916
- const db = openHippoDb(hippoRoot);
1917
- try {
1918
- db.prepare(`INSERT INTO consolidation_runs(timestamp, decayed, merged, removed) VALUES (?, ?, ?, ?)`).run(run.timestamp, run.decayed, run.merged, run.removed);
1919
- pruneConsolidationRuns(db, 50);
1920
- writeStatsMirror(hippoRoot, buildStatsFromDb(db));
1921
- }
1922
- finally {
1923
- closeHippoDb(db);
1924
- }
1925
- }
1926
- /**
1927
- * Load the session decay context from the store.
1928
- * Uses consolidation_runs timestamps to compute session intervals.
1929
- */
1930
- export function loadSessionDecayContext(hippoRoot) {
1931
- initStore(hippoRoot);
1932
- const db = openHippoDb(hippoRoot);
1933
- try {
1934
- // Get recent consolidation timestamps (last 20)
1935
- // SAFETY: rows' shape matches the single `timestamp` column above.
1936
- const rows = db.prepare(`SELECT timestamp FROM consolidation_runs ORDER BY timestamp DESC, id DESC LIMIT 20`).all();
1937
- const sleepCount = Number(getMeta(db, 'sleep_count', '0')) || rows.length;
1938
- if (rows.length < 2) {
1939
- return { sleepCount, avgSessionIntervalDays: 0 };
1940
- }
1941
- // Compute average interval between consecutive sessions
1942
- const timestamps = rows.map((r) => new Date(r.timestamp).getTime()).reverse();
1943
- let totalInterval = 0;
1944
- for (let i = 1; i < timestamps.length; i++) {
1945
- totalInterval += timestamps[i] - timestamps[i - 1];
1946
- }
1947
- const avgMs = totalInterval / (timestamps.length - 1);
1948
- const avgDays = avgMs / (1000 * 60 * 60 * 24);
1949
- return { sleepCount, avgSessionIntervalDays: Math.max(0, avgDays) };
1950
- }
1951
- finally {
1952
- closeHippoDb(db);
1953
- }
1954
- }
1955
- /**
1956
- * Increment the sleep counter. Called after each consolidation run.
1957
- */
1958
- export function incrementSleepCount(hippoRoot) {
1959
- initStore(hippoRoot);
1960
- const db = openHippoDb(hippoRoot);
1961
- try {
1962
- const current = Number(getMeta(db, 'sleep_count', '0')) || 0;
1963
- setMeta(db, 'sleep_count', String(current + 1));
1964
- }
1965
- finally {
1966
- closeHippoDb(db);
1967
- }
1968
- }
1969
- /**
1970
- * Defensive runtime guard for tenant id arguments.
1971
- *
1972
- * The continuity helpers (saveActiveTaskSnapshot, listSessionEvents, etc.)
1973
- * gained a required `tenantId` parameter in v0.41 / schema v22 to close a
1974
- * cross-tenant data leak. TypeScript catches misbinding at compile time, but
1975
- * JavaScript callers from older versions can silently pass a `sessionId`
1976
- * where `tenantId` is now expected, e.g.
1977
- * loadLatestHandoff(root, 'sess-abc') // WRONG: 'sess-abc' becomes the tenant
1978
- * which would silently filter to a non-existent tenant and return null with
1979
- * no error. This guard rejects the most common shape of that mistake (any
1980
- * value beginning with the conventional `sess-` / `sess_` session prefix).
1981
- *
1982
- * False-positive cost: a tenant literally named `sess-...` will be rejected.
1983
- * Acceptable tradeoff for catching the silent-leak class.
1984
- */
1985
- export function assertTenantId(fnName, value) {
1986
- if (typeof value !== 'string' || value.length === 0) {
1987
- throw new Error(`${fnName}: tenantId is required (got ${typeof value})`);
1988
- }
1989
- if (/^sess[-_]/i.test(value)) {
1990
- throw new Error(`${fnName}: tenantId looks like a session id ('${value}'). ` +
1991
- `In v0.41+ these helpers take (hippoRoot, tenantId, ...). ` +
1992
- `Pass the tenant id (e.g. 'default') and the session id separately.`);
1993
- }
1994
- }
1995
- export function saveActiveTaskSnapshot(hippoRoot, tenantId, snapshot) {
1996
- assertTenantId('saveActiveTaskSnapshot', tenantId);
1997
- initStore(hippoRoot);
1998
- const db = openHippoDb(hippoRoot);
1999
- const now = new Date().toISOString();
2000
- try {
2001
- db.exec('BEGIN');
2002
- db.prepare(`UPDATE task_snapshots SET status = 'superseded', updated_at = ? WHERE status = 'active' AND tenant_id = ?`).run(now, tenantId);
2003
- const result = db.prepare(`
2004
- INSERT INTO task_snapshots(task, summary, next_step, status, source, session_id, scope, tenant_id, created_at, updated_at)
2005
- VALUES (?, ?, ?, 'active', ?, ?, ?, ?, ?, ?)
2006
- `).run(snapshot.task, snapshot.summary, snapshot.next_step, snapshot.source ?? 'cli', snapshot.session_id ?? null, snapshot.scope ?? null, tenantId, now, now);
2007
- db.exec('COMMIT');
2008
- const id = Number(result.lastInsertRowid ?? 0);
2009
- // SAFETY: row's shape matches the ten columns named in the SELECT above.
2010
- const row = db.prepare(`
2011
- SELECT id, task, summary, next_step, status, source, session_id, scope, created_at, updated_at
2012
- FROM task_snapshots
2013
- WHERE id = ?
2014
- `).get(id);
2015
- if (!row) {
2016
- throw new Error('Failed to reload saved active task snapshot');
2017
- }
2018
- const loaded = rowToTaskSnapshot(row);
2019
- writeActiveTaskMirror(hippoRoot, tenantId, loaded);
2020
- return loaded;
2021
- }
2022
- catch (error) {
2023
- try {
2024
- db.exec('ROLLBACK');
2025
- }
2026
- catch {
2027
- // Ignore nested rollback failures.
2028
- }
2029
- throw error;
2030
- }
2031
- finally {
2032
- closeHippoDb(db);
2033
- }
2034
- }
2035
- export function loadActiveTaskSnapshot(hippoRoot, tenantId) {
2036
- assertTenantId('loadActiveTaskSnapshot', tenantId);
2037
- initStore(hippoRoot);
2038
- const db = openHippoDb(hippoRoot);
2039
- try {
2040
- // SAFETY: row's shape matches the ten columns named in the SELECT above.
2041
- const row = db.prepare(`
2042
- SELECT id, task, summary, next_step, status, source, session_id, scope, created_at, updated_at
2043
- FROM task_snapshots
2044
- WHERE status = 'active' AND tenant_id = ?
2045
- ORDER BY updated_at DESC, id DESC
2046
- LIMIT 1
2047
- `).get(tenantId);
2048
- if (!row) {
2049
- removeActiveTaskMirror(hippoRoot, tenantId);
2050
- return null;
2051
- }
2052
- const loaded = rowToTaskSnapshot(row);
2053
- writeActiveTaskMirror(hippoRoot, tenantId, loaded);
2054
- return loaded;
2055
- }
2056
- finally {
2057
- closeHippoDb(db);
2058
- }
2059
- }
2060
- /**
2061
- * Default freshness bound for AMBIENT active-task-snapshot reads (DF1,
2062
- * docs/plans/2026-08-23-df1-snapshot-lifecycle.md): 72h, chosen over 48h so
2063
- * a Friday-evening orphan still offers continuity on Monday morning.
2064
- * Exported so callers can override via `loadFreshActiveTaskSnapshot`'s
2065
- * `opts.maxAgeMs`; deliberately no env knob (Simplicity First).
2066
- */
2067
- export const SNAPSHOT_AMBIENT_MAX_AGE_MS = 72 * 60 * 60 * 1000;
2068
- /** A usable session id: non-null, non-empty string. Named predicate (not an
2069
- * inline `typeof` check) so the owner-match rule in
2070
- * `loadFreshActiveTaskSnapshot` states its contract once. */
2071
- function isNonEmptySessionId(value) {
2072
- return typeof value === 'string' && value.length > 0;
2073
- }
2074
- /**
2075
- * Bounded read for AMBIENT active-task-snapshot surfaces (UserPromptSubmit
2076
- * hook context, MCP recall block) — the never-expires fix for DF1. A
2077
- * snapshot written by `hippo pre-compact` has no death path tied to the
2078
- * session that owns it, so an orphaned row would otherwise inject into
2079
- * every prompt of every later session forever. Wraps `loadActiveTaskSnapshot`
2080
- * (unchanged, still the source of truth for explicit continuity surfaces),
2081
- * then applies, in order:
2082
- *
2083
- * 1. Owner match — ONLY when both `opts.sessionId` and the snapshot's
2084
- * `session_id` are non-null, non-empty strings and strictly equal
2085
- * (`===`). Owner reads are unbounded: the session that owns the snapshot
2086
- * can always see its own working state, regardless of age.
2087
- * 2. Age check — everything else, including absent-vs-absent ids. A
2088
- * null/undefined/empty id on EITHER side never counts as an owner match;
2089
- * it falls through here instead. (`runPreCompact` can legitimately save a
2090
- * snapshot with `session_id = null`; a null-equals-null "match" would
2091
- * reopen indefinite ambient injection for exactly those rows.) Returns
2092
- * the snapshot only when `age(updated_at) <= maxAgeMs` (default
2093
- * `SNAPSHOT_AMBIENT_MAX_AGE_MS`); otherwise null.
2094
- *
2095
- * No SQL change — age derives from the existing `updated_at` column.
2096
- */
2097
- export function loadFreshActiveTaskSnapshot(hippoRoot, tenantId, opts = {}) {
2098
- const snapshot = loadActiveTaskSnapshot(hippoRoot, tenantId);
2099
- if (!snapshot)
2100
- return null;
2101
- const callerSessionId = opts.sessionId;
2102
- const isOwnerMatch = isNonEmptySessionId(callerSessionId) &&
2103
- isNonEmptySessionId(snapshot.session_id) &&
2104
- callerSessionId === snapshot.session_id;
2105
- if (isOwnerMatch)
2106
- return snapshot;
2107
- const maxAgeMs = opts.maxAgeMs ?? SNAPSHOT_AMBIENT_MAX_AGE_MS;
2108
- const ageMs = Date.now() - Date.parse(snapshot.updated_at);
2109
- return ageMs <= maxAgeMs ? snapshot : null;
2110
- }
2111
- export function clearActiveTaskSnapshot(hippoRoot, tenantId, clearedStatus = 'cleared') {
2112
- assertTenantId('clearActiveTaskSnapshot', tenantId);
2113
- initStore(hippoRoot);
2114
- const db = openHippoDb(hippoRoot);
2115
- const now = new Date().toISOString();
2116
- try {
2117
- // SAFETY: active's shape matches the single `id` column selected above.
2118
- const active = db.prepare(`SELECT id FROM task_snapshots WHERE status = 'active' AND tenant_id = ? ORDER BY updated_at DESC, id DESC LIMIT 1`).get(tenantId);
2119
- if (!active?.id) {
2120
- removeActiveTaskMirror(hippoRoot, tenantId);
2121
- return false;
2122
- }
2123
- db.prepare(`UPDATE task_snapshots SET status = ?, updated_at = ? WHERE id = ? AND tenant_id = ?`).run(clearedStatus, now, active.id, tenantId);
2124
- removeActiveTaskMirror(hippoRoot, tenantId);
2125
- return true;
2126
- }
2127
- finally {
2128
- closeHippoDb(db);
2129
- }
2130
- }
2131
- /**
2132
- * Close the `active` task snapshot(s) owned by `sessionId`, for the T3
2133
- * session-end death path (DF1, docs/plans/2026-08-23-df1-snapshot-lifecycle.md).
2134
- * Only one `active` row exists per tenant in practice (supersession happens
2135
- * at save), but the WHERE clause scopes on `session_id` too — not just
2136
- * `status='active' AND tenant_id=?` — so an ending session can never close a
2137
- * different, newer session's active snapshot. Returns the number of rows
2138
- * closed (0 when no active row is owned by `sessionId`).
2139
- */
2140
- export function closeTaskSnapshotsForSession(hippoRoot, tenantId, sessionId, status = 'session-ended') {
2141
- assertTenantId('closeTaskSnapshotsForSession', tenantId);
2142
- initStore(hippoRoot);
2143
- const db = openHippoDb(hippoRoot);
2144
- const now = new Date().toISOString();
2145
- try {
2146
- const result = db.prepare(`UPDATE task_snapshots SET status = ?, updated_at = ? WHERE status = 'active' AND tenant_id = ? AND session_id = ?`).run(status, now, tenantId, sessionId);
2147
- return Number(result.changes ?? 0);
2148
- }
2149
- finally {
2150
- closeHippoDb(db);
2151
- }
2152
- }
2153
- export function appendSessionEvent(hippoRoot, tenantId, event) {
2154
- assertTenantId('appendSessionEvent', tenantId);
2155
- initStore(hippoRoot);
2156
- const db = openHippoDb(hippoRoot);
2157
- const now = new Date().toISOString();
2158
- // v1.2: scope is wired through. Default-deny in api.recall + cmdRecall
2159
- // continuity reads applies to slack:private:* and 'unknown:legacy' rows.
2160
- try {
2161
- const result = db.prepare(`
2162
- INSERT INTO session_events(session_id, task, event_type, content, source, scope, metadata_json, tenant_id, created_at)
2163
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
2164
- `).run(event.session_id, event.task ?? null, event.event_type, event.content, event.source ?? 'cli', event.scope ?? null, JSON.stringify(event.metadata ?? {}), tenantId, now);
2165
- const id = Number(result.lastInsertRowid ?? 0);
2166
- // SAFETY: row's shape matches the nine columns named in the SELECT
2167
- // above.
2168
- const row = db.prepare(`
2169
- SELECT id, session_id, task, event_type, content, source, scope, metadata_json, created_at
2170
- FROM session_events
2171
- WHERE id = ?
2172
- `).get(id);
2173
- if (!row) {
2174
- throw new Error('Failed to reload saved session event');
2175
- }
2176
- const loaded = rowToSessionEvent(row);
2177
- // SAFETY: recentRows' shape matches the nine columns named in the
2178
- // SELECT above.
2179
- const recentRows = db.prepare(`
2180
- SELECT id, session_id, task, event_type, content, source, scope, metadata_json, created_at
2181
- FROM session_events
2182
- WHERE session_id = ? AND tenant_id = ?
2183
- ORDER BY created_at DESC, id DESC
2184
- LIMIT ?
2185
- `).all(loaded.session_id, tenantId, 20);
2186
- const recent = recentRows.map(rowToSessionEvent).reverse();
2187
- writeRecentSessionMirror(hippoRoot, tenantId, recent);
2188
- return loaded;
2189
- }
2190
- finally {
2191
- closeHippoDb(db);
2192
- }
2193
- }
2194
- export function listSessionEvents(hippoRoot, tenantId, options = {}) {
2195
- assertTenantId('listSessionEvents', tenantId);
2196
- initStore(hippoRoot);
2197
- const db = openHippoDb(hippoRoot);
2198
- try {
2199
- const clauses = ['tenant_id = ?'];
2200
- const params = [tenantId];
2201
- if (options.session_id) {
2202
- clauses.push('session_id = ?');
2203
- params.push(options.session_id);
2204
- }
2205
- if (options.task) {
2206
- clauses.push('task = ?');
2207
- params.push(options.task);
2208
- }
2209
- const limit = Math.max(1, Math.trunc(options.limit ?? 8));
2210
- params.push(limit);
2211
- const where = `WHERE ${clauses.join(' AND ')}`;
2212
- // SAFETY: rows' shape matches the nine columns named in the SELECT
2213
- // above.
2214
- const rows = db.prepare(`
2215
- SELECT id, session_id, task, event_type, content, source, scope, metadata_json, created_at
2216
- FROM session_events
2217
- ${where}
2218
- ORDER BY created_at DESC, id DESC
2219
- LIMIT ?
2220
- `).all(...params);
2221
- return rows.map(rowToSessionEvent).reverse();
2222
- }
2223
- finally {
2224
- closeHippoDb(db);
2225
- }
2226
- }
2227
- /**
2228
- * Return session_ids with a `session_complete` event newer than `sinceMs`.
2229
- * Used by the sleep auto-promotion pass to bound scanning to a fixed window.
2230
- */
2231
- export function findPromotableSessions(hippoRoot, tenantId, sinceMs) {
2232
- assertTenantId('findPromotableSessions', tenantId);
2233
- initStore(hippoRoot);
2234
- const db = openHippoDb(hippoRoot);
2235
- try {
2236
- // SAFETY: rows' shape matches the single `session_id` column selected
2237
- // above.
2238
- const rows = db.prepare(`
2239
- SELECT DISTINCT session_id FROM session_events
2240
- WHERE event_type = 'session_complete' AND created_at >= ? AND tenant_id = ?
2241
- `).all(new Date(sinceMs).toISOString(), tenantId);
2242
- return rows;
2243
- }
2244
- finally {
2245
- closeHippoDb(db);
2246
- }
2247
- }
2248
- /**
2249
- * Idempotency guard — true if a trace-layer memory with this source_session_id
2250
- * already exists.
2251
- */
2252
- export function traceExistsForSession(hippoRoot, tenantId, session_id) {
2253
- assertTenantId('traceExistsForSession', tenantId);
2254
- initStore(hippoRoot);
2255
- const db = openHippoDb(hippoRoot);
2256
- try {
2257
- const row = db.prepare(`
2258
- SELECT 1 FROM memories
2259
- WHERE source_session_id = ? AND layer = 'trace' AND tenant_id = ?
2260
- LIMIT 1
2261
- `).get(session_id, tenantId);
2262
- return !!row;
2263
- }
2264
- finally {
2265
- closeHippoDb(db);
2266
- }
2267
- }
2268
- export function listMemoryConflicts(hippoRoot, status = 'open', tenantId) {
2269
- initStore(hippoRoot);
2270
- const db = openHippoDb(hippoRoot);
2271
- try {
2272
- // v0.28 — '*' is a sentinel meaning "no status filter, return all rows".
2273
- // Pre-v0.28 callers (cli/mcp/dashboard) always passed 'open' or default,
2274
- // so this sentinel is purely additive. The 4 SQL branches below cover
2275
- // {tenanted | unscoped} × {all-statuses | specific-status}.
2276
- const allStatuses = status === '*';
2277
- let rows;
2278
- if (tenantId !== undefined) {
2279
- // Tenanted query — JOIN to memories on both conflict members and require
2280
- // each in-tenant, so neither a normal cross-tenant pair nor a stale
2281
- // pre-fix row surfaces (consistent with resolveConflict).
2282
- // SAFETY: both branches select the same eight mc.* columns (aliased
2283
- // to MemoryConflictRow's field names) from memory_conflicts.
2284
- rows = allStatuses
2285
- ? db.prepare(`
2286
- SELECT mc.id, mc.memory_a_id, mc.memory_b_id, mc.reason, mc.score,
2287
- mc.status, mc.detected_at, mc.updated_at
2288
- FROM memory_conflicts mc
2289
- JOIN memories ma ON ma.id = mc.memory_a_id
2290
- JOIN memories mb ON mb.id = mc.memory_b_id
2291
- WHERE ma.tenant_id = ? AND mb.tenant_id = ?
2292
- ORDER BY mc.updated_at DESC, mc.id DESC
2293
- `).all(tenantId, tenantId)
2294
- : db.prepare(`
2295
- SELECT mc.id, mc.memory_a_id, mc.memory_b_id, mc.reason, mc.score,
2296
- mc.status, mc.detected_at, mc.updated_at
2297
- FROM memory_conflicts mc
2298
- JOIN memories ma ON ma.id = mc.memory_a_id
2299
- JOIN memories mb ON mb.id = mc.memory_b_id
2300
- WHERE mc.status = ? AND ma.tenant_id = ? AND mb.tenant_id = ?
2301
- ORDER BY mc.updated_at DESC, mc.id DESC
2302
- `).all(status, tenantId, tenantId);
2303
- }
2304
- else {
2305
- // Unscoped query — legacy direct-mode (CLI, tests, consolidate).
2306
- // SAFETY: both branches select the same eight columns matching
2307
- // MemoryConflictRow's field set.
2308
- rows = allStatuses
2309
- ? db.prepare(`
2310
- SELECT id, memory_a_id, memory_b_id, reason, score, status, detected_at, updated_at
2311
- FROM memory_conflicts
2312
- ORDER BY updated_at DESC, id DESC
2313
- `).all()
2314
- : db.prepare(`
2315
- SELECT id, memory_a_id, memory_b_id, reason, score, status, detected_at, updated_at
2316
- FROM memory_conflicts
2317
- WHERE status = ?
2318
- ORDER BY updated_at DESC, id DESC
2319
- `).all(status);
2320
- }
2321
- return rows.map(rowToMemoryConflict);
2322
- }
2323
- finally {
2324
- closeHippoDb(db);
2325
- }
2326
- }
2327
- export function replaceDetectedConflicts(hippoRoot, detected, detectedAt = new Date().toISOString()) {
2328
- initStore(hippoRoot);
2329
- const db = openHippoDb(hippoRoot);
2330
- try {
2331
- db.exec('BEGIN');
2332
- // Tenant guard (E2): a conflict is meaningful only within one tenant.
2333
- // Build id -> tenant_id once and skip cross-tenant pairs both when
2334
- // inserting rows and when rebuilding conflicts_with_json, so a stale
2335
- // cross-tenant row can neither persist nor leak a foreign id.
2336
- const tenantById = new Map();
2337
- // SAFETY: rows' shape matches the two columns named in the SELECT below.
2338
- for (const r of db.prepare(`SELECT id, tenant_id FROM memories`).all()) {
2339
- tenantById.set(r.id, r.tenant_id);
2340
- }
2341
- const sameTenant = (a, b) => {
2342
- const ta = tenantById.get(a);
2343
- const tb = tenantById.get(b);
2344
- return ta !== undefined && tb !== undefined && ta === tb;
2345
- };
2346
- const canonicalDetected = detected.map((conflict) => ({
2347
- ...canonicalConflictPair(conflict.memory_a_id, conflict.memory_b_id),
2348
- reason: conflict.reason,
2349
- score: conflict.score,
2350
- }));
2351
- const detectedKeys = new Set(canonicalDetected.map((conflict) => `${conflict.memory_a_id}::${conflict.memory_b_id}`));
2352
- // SAFETY: openRows' shape matches the eight columns named in the SELECT
2353
- // above.
2354
- const openRows = db.prepare(`
2355
- SELECT id, memory_a_id, memory_b_id, reason, score, status, detected_at, updated_at
2356
- FROM memory_conflicts
2357
- WHERE status = 'open'
2358
- `).all();
2359
- for (const row of openRows) {
2360
- const key = `${row.memory_a_id}::${row.memory_b_id}`;
2361
- const stale = !detectedKeys.has(key);
2362
- // v1.11.0 residue: auto-resolve any open cross-tenant row. The insert
2363
- // loop below (line 2089) and the refMap rebuild (line 2117) skip
2364
- // cross-tenant pairs, but the resolve-stale loop previously left
2365
- // re-detected cross-tenant rows lingering status='open'. The
2366
- // sameTenant() helper is already built one block up; no extra query.
2367
- const crossTenant = !sameTenant(row.memory_a_id, row.memory_b_id);
2368
- if (stale || crossTenant) {
2369
- db.prepare(`UPDATE memory_conflicts SET status = 'resolved', updated_at = ? WHERE id = ?`).run(detectedAt, row.id);
2370
- }
2371
- }
2372
- for (const conflict of canonicalDetected) {
2373
- // Skip cross-tenant pairs — never persist a conflict spanning tenants.
2374
- if (!sameTenant(conflict.memory_a_id, conflict.memory_b_id))
2375
- continue;
2376
- db.prepare(`
2377
- INSERT INTO memory_conflicts(memory_a_id, memory_b_id, reason, score, status, detected_at, updated_at)
2378
- VALUES (?, ?, ?, ?, 'open', ?, ?)
2379
- ON CONFLICT(memory_a_id, memory_b_id) DO UPDATE SET
2380
- reason = excluded.reason,
2381
- score = excluded.score,
2382
- status = 'open',
2383
- updated_at = excluded.updated_at
2384
- `).run(conflict.memory_a_id, conflict.memory_b_id, conflict.reason, conflict.score, detectedAt, detectedAt);
2385
- }
2386
- // SAFETY: openConflicts' shape matches the two columns named above.
2387
- const openConflicts = db.prepare(`
2388
- SELECT memory_a_id, memory_b_id
2389
- FROM memory_conflicts
2390
- WHERE status = 'open'
2391
- `).all();
2392
- const refMap = new Map();
2393
- for (const row of openConflicts) {
2394
- // Skip cross-tenant pairs so a stale row never seeds a foreign id
2395
- // into conflicts_with_json.
2396
- if (!sameTenant(row.memory_a_id, row.memory_b_id))
2397
- continue;
2398
- if (!refMap.has(row.memory_a_id))
2399
- refMap.set(row.memory_a_id, new Set());
2400
- if (!refMap.has(row.memory_b_id))
2401
- refMap.set(row.memory_b_id, new Set());
2402
- refMap.get(row.memory_a_id).add(row.memory_b_id);
2403
- refMap.get(row.memory_b_id).add(row.memory_a_id);
2404
- }
2405
- // SAFETY: memoryRows' shape matches the single `id` column selected
2406
- // above.
2407
- const memoryRows = db.prepare(`SELECT id FROM memories`).all();
2408
- for (const memory of memoryRows) {
2409
- const refs = Array.from(refMap.get(memory.id) ?? []).sort();
2410
- db.prepare(`UPDATE memories SET conflicts_with_json = ?, updated_at = datetime('now') WHERE id = ?`).run(JSON.stringify(refs), memory.id);
2411
- }
2412
- db.exec('COMMIT');
2413
- syncMirrorFiles(hippoRoot, db);
2414
- }
2415
- catch (error) {
2416
- try {
2417
- db.exec('ROLLBACK');
2418
- }
2419
- catch {
2420
- // Ignore nested rollback failures.
2421
- }
2422
- throw error;
2423
- }
2424
- finally {
2425
- closeHippoDb(db);
2426
- }
2427
- }
2428
- /**
2429
- * Resolve a conflict by keeping one memory and weakening the other.
2430
- * Sets conflict status to 'resolved' and halves the loser's half-life.
2431
- * If --forget is used, the loser is removed entirely (kind-aware: raw rows
2432
- * are archived via archiveRawMemory, others deleted via deleteEntryCore —
2433
- * AT1 fix for the pre-existing crash where a raw loser aborted the whole
2434
- * resolve transaction against the append-only trigger). `opts.rejectLoserValue`
2435
- * additionally tombstones the loser's normalized digest so it cannot be
2436
- * re-asserted later.
2437
- *
2438
- * Every resolution path (weaken / forget / reject) emits a `conflict_resolve`
2439
- * audit row (AT1 — previously resolveConflict wrote zero audit rows on any path).
2440
- *
2441
- * Returns the resolved conflict, or null if not found.
2442
- */
2443
- export function resolveConflict(hippoRoot, conflictId, keepId, forgetLoser = false, tenantId, opts) {
2444
- initStore(hippoRoot);
2445
- const db = openHippoDb(hippoRoot);
2446
- // When tenantId is set, the conflict lookup requires BOTH members in-tenant
2447
- // and every memories mutation carries AND tenant_id = ?. A cross-tenant probe
2448
- // then returns null, indistinguishable from a bad id. Omitted tenantId =
2449
- // legacy unscoped behaviour (CLI direct mode, tests, consolidate.ts).
2450
- const memScope = tenantId !== undefined ? ' AND tenant_id = ?' : '';
2451
- const memArgs = tenantId !== undefined ? [tenantId] : [];
2452
- try {
2453
- // SAFETY: both branches select the same eight columns (aliased in the
2454
- // tenanted branch) matching MemoryConflictRow's field set.
2455
- const row = (tenantId !== undefined
2456
- ? db.prepare(`
2457
- SELECT mc.id, mc.memory_a_id, mc.memory_b_id, mc.reason, mc.score,
2458
- mc.status, mc.detected_at, mc.updated_at
2459
- FROM memory_conflicts mc
2460
- JOIN memories ma ON ma.id = mc.memory_a_id
2461
- JOIN memories mb ON mb.id = mc.memory_b_id
2462
- WHERE mc.id = ? AND ma.tenant_id = ? AND mb.tenant_id = ?
2463
- `).get(conflictId, tenantId, tenantId)
2464
- : db.prepare(`
2465
- SELECT id, memory_a_id, memory_b_id, reason, score, status, detected_at, updated_at
2466
- FROM memory_conflicts WHERE id = ?
2467
- `).get(conflictId));
2468
- if (!row)
2469
- return null;
2470
- const conflict = rowToMemoryConflict(row);
2471
- if (conflict.status !== 'open')
2472
- return null;
2473
- const loserId = keepId === conflict.memory_a_id
2474
- ? conflict.memory_b_id
2475
- : keepId === conflict.memory_b_id
2476
- ? conflict.memory_a_id
2477
- : null;
2478
- if (!loserId)
2479
- return null;
2480
- db.exec('BEGIN');
2481
- // Mark conflict as resolved
2482
- db.prepare(`UPDATE memory_conflicts SET status = 'resolved', updated_at = datetime('now') WHERE id = ?`)
2483
- .run(conflictId);
2484
- // AT1 (plan §5): removal (forgetLoser OR rejectLoserValue — a tombstoned
2485
- // value cannot be left live) is now kind-aware. The old bare
2486
- // `DELETE FROM memories WHERE id = ?` aborted the whole transaction when
2487
- // the loser was kind='raw' (append-only trigger fires); route through
2488
- // the same helpers the reject verb uses (both db-scoped, both compose
2489
- // inside this BEGIN/COMMIT). loserRemoved / loserWasRaw drive both the
2490
- // conflicts_with_json skip below and the post-commit mirror purge.
2491
- let loserRemoved = false;
2492
- let loserWasRaw = false;
2493
- let rejectedDigest;
2494
- // AT1 P1 fix (codex): same-tenant duplicates of the loser's content that
2495
- // rejectLoserValue also removes (see below) — separate from loserId so
2496
- // the audit + post-commit mirror purge can cover ALL of them, not just
2497
- // loserId.
2498
- const extraRemovedIds = [];
2499
- const extraRemovedRawIds = [];
2500
- const removeLoser = forgetLoser || opts?.rejectLoserValue === true;
2501
- if (removeLoser) {
2502
- // SAFETY: loserRow's shape matches the three columns named in the
2503
- // SELECT above.
2504
- const loserRow = db
2505
- .prepare(`SELECT kind, content, tenant_id FROM memories WHERE id = ?${memScope}`)
2506
- .get(loserId, ...memArgs);
2507
- if (loserRow) {
2508
- const actor = opts?.rejectedBy ?? 'cli';
2509
- const reason = opts?.reason ?? `resolveConflict ${conflictId}: kept ${keepId}`;
2510
- if (opts?.rejectLoserValue) {
2511
- rejectedDigest = rejectionDigest(loserRow.content);
2512
- insertRejectedValue(db, {
2513
- tenantId: loserRow.tenant_id ?? 'default',
2514
- digest: rejectedDigest,
2515
- reason,
2516
- rejectedBy: actor,
2517
- rejectedAt: new Date().toISOString(),
2518
- sourceMemoryId: loserId,
2519
- normalizedChars: normalizeValueForRejection(loserRow.content).length,
2520
- });
2521
- // AT1 P1 fix (codex): reject-flow.ts's `rejectValue` removes ALL
2522
- // live same-tenant rows whose normalized digest matches, not just
2523
- // the one id passed — but this branch only ever removed loserId,
2524
- // leaving same-TENANT duplicates live while their shared content
2525
- // was tombstoned. Same O(N) scan pattern as reject-flow.ts (human-
2526
- // triggered command, tenant's row count is human-scale). CRITICAL
2527
- // BOUNDARY: tenant-scoped ONLY — tombstones are tenant-scoped by
2528
- // design, so a same-content row in ANOTHER tenant is legitimately
2529
- // live and must NOT be touched here. `keepId` is excluded even if
2530
- // its content coincidentally matches: the human explicitly chose
2531
- // to keep it in this same resolution, and this branch must not
2532
- // undo that choice in the same transaction.
2533
- const loserTenantId = loserRow.tenant_id ?? 'default';
2534
- // SAFETY: dupRows' shape matches the three columns named in the
2535
- // SELECT above.
2536
- const dupRows = db
2537
- .prepare(`SELECT id, kind, content FROM memories WHERE tenant_id = ? AND id != ? AND id != ?`)
2538
- .all(loserTenantId, loserId, keepId);
2539
- for (const dup of dupRows) {
2540
- if (rejectionDigest(dup.content) !== rejectedDigest)
2541
- continue;
2542
- if (dup.kind === 'raw') {
2543
- archiveRawMemory(db, dup.id, { reason, who: actor });
2544
- extraRemovedRawIds.push(dup.id);
2545
- }
2546
- else {
2547
- deleteEntryCore(db, dup.id, { actor, suppressForgetAudit: true });
2548
- }
2549
- extraRemovedIds.push(dup.id);
2550
- }
2551
- }
2552
- if (loserRow.kind === 'raw') {
2553
- archiveRawMemory(db, loserId, { reason, who: actor });
2554
- loserWasRaw = true;
2555
- }
2556
- else {
2557
- deleteEntryCore(db, loserId, { actor, suppressForgetAudit: true });
2558
- }
2559
- loserRemoved = true;
2560
- }
2561
- // loserRow undefined = tenant-scope mismatch (or already gone); matches
2562
- // the old tenant-scoped DELETE's silent 0-rows-affected behavior.
2563
- }
2564
- else {
2565
- // Halve the loser's half-life (weakens it over time)
2566
- db.prepare(`UPDATE memories SET half_life_days = MAX(1, half_life_days / 2), updated_at = datetime('now') WHERE id = ?${memScope}`)
2567
- .run(loserId, ...memArgs);
2568
- }
2569
- // Clean up conflicts_with references
2570
- // SAFETY: keepRow's shape matches the single `conflicts_with_json`
2571
- // column selected above.
2572
- const keepRow = db.prepare(`SELECT conflicts_with_json FROM memories WHERE id = ?${memScope}`).get(keepId, ...memArgs);
2573
- if (keepRow) {
2574
- const refs = JSON.parse(keepRow.conflicts_with_json || '[]');
2575
- const cleaned = refs.filter((r) => r !== loserId);
2576
- db.prepare(`UPDATE memories SET conflicts_with_json = ?, updated_at = datetime('now') WHERE id = ?${memScope}`)
2577
- .run(JSON.stringify(cleaned), keepId, ...memArgs);
2578
- }
2579
- if (!loserRemoved) {
2580
- // SAFETY: loserRow's shape matches the single `conflicts_with_json`
2581
- // column named in the SELECT below.
2582
- const loserRow = db.prepare(`SELECT conflicts_with_json FROM memories WHERE id = ?${memScope}`).get(loserId, ...memArgs);
2583
- if (loserRow) {
2584
- const refs = JSON.parse(loserRow.conflicts_with_json || '[]');
2585
- const cleaned = refs.filter((r) => r !== keepId);
2586
- db.prepare(`UPDATE memories SET conflicts_with_json = ?, updated_at = datetime('now') WHERE id = ?${memScope}`)
2587
- .run(JSON.stringify(cleaned), loserId, ...memArgs);
2588
- }
2589
- }
2590
- // AT1: the missing audit (plan §5 — resolveConflict wrote ZERO audit_log
2591
- // rows on any path before this). Every path — weaken, forget, reject —
2592
- // lands exactly one conflict_resolve row.
2593
- const conflictResolveMeta = {
2594
- conflictId,
2595
- keepId,
2596
- loserId,
2597
- disposition: loserRemoved ? (loserWasRaw ? 'archived_raw' : 'deleted') : 'weakened',
2598
- rejected: Boolean(opts?.rejectLoserValue),
2599
- // AT1 P1 fix: every row this call removed, not just loserId — the
2600
- // same-tenant duplicate sweep above (extraRemovedIds) needs an
2601
- // audit trail too.
2602
- removedIds: loserRemoved ? [loserId, ...extraRemovedIds] : [],
2603
- };
2604
- // Assigned only when present so the serialized audit payload keeps
2605
- // omitting the key, exactly as the pre-migration object literal did.
2606
- if (rejectedDigest !== undefined)
2607
- conflictResolveMeta.rejectedDigest = rejectedDigest;
2608
- // Fresh spread literal: ConflictResolveMeta is a closed interface (no
2609
- // index signature) and isn't directly assignable to audit()'s
2610
- // Record<string, JsonValue> metadata param; a spread into a fresh
2611
- // object literal satisfies it without widening the declared type above.
2612
- audit(db, 'conflict_resolve', keepId, { ...conflictResolveMeta }, opts?.rejectedBy ?? 'cli', tenantId);
2613
- db.exec('COMMIT');
2614
- syncMirrorFiles(hippoRoot, db);
2615
- // AT1 P1b fix: mirror purge + reaper stamp for EVERY removed loser, not
2616
- // just the rejectLoserValue path. Pre-AT1, the plain forgetLoser path on
2617
- // a raw loser crashed outright (bare DELETE FROM memories hit the
2618
- // append-only trigger) — there is no legacy "successful forget, no
2619
- // purge" behavior to preserve for that case. Post-AT1's kind-aware
2620
- // removal (archiveRawMemory / deleteEntryCore above) makes plain
2621
- // --forget succeed on every kind, but until this fix the mirror was
2622
- // only purged when rejectLoserValue was ALSO set: a plain raw --forget
2623
- // left its markdown mirror orphaned (the reaper still catches it
2624
- // eventually, since archiveRawMemory's own raw_archive insert leaves
2625
- // mirror_cleaned_at NULL) and a plain non-raw --forget left its mirror
2626
- // orphaned FOREVER (no reaper exists for non-raw rows). Same post-commit
2627
- // purge+reaper pattern as the reject verb (src/reject-flow.ts) and
2628
- // api.archiveRaw — reusing removeEntryMirrors + raw_archive bookkeeping.
2629
- if (loserRemoved) {
2630
- // AT1 P1 fix: loop over loserId AND every same-tenant duplicate the
2631
- // rejectLoserValue sweep above removed (extraRemovedIds) — previously
2632
- // only loserId's mirror was purged, leaving duplicate mirrors orphaned
2633
- // despite their rows being gone.
2634
- for (const removedId of [loserId, ...extraRemovedIds]) {
2635
- const isRaw = removedId === loserId ? loserWasRaw : extraRemovedRawIds.includes(removedId);
2636
- // AT1 fix: purgeMirrorBestEffort retries once, then — for non-raw ids,
2637
- // which cleanupArchivedMirrors' reaper never scans — reports the
2638
- // EXPLICIT leftover path(s) instead of the false "will retry via
2639
- // reaper" claim. See its own doc comment (store.ts, near
2640
- // removeEntryMirrors) for the full rationale.
2641
- const mirrorOk = purgeMirrorBestEffort(hippoRoot, removedId, isRaw, 'resolveConflict');
2642
- if (mirrorOk && isRaw) {
2643
- db.prepare(`UPDATE raw_archive SET mirror_cleaned_at = ? WHERE memory_id = ?`).run(new Date().toISOString(), removedId);
2644
- }
2645
- }
2646
- }
2647
- return { conflict: { ...conflict, status: 'resolved' }, loserId };
2648
- }
2649
- catch (error) {
2650
- try {
2651
- db.exec('ROLLBACK');
2652
- }
2653
- catch { /* ignore */ }
2654
- throw error;
2655
- }
2656
- finally {
2657
- closeHippoDb(db);
2658
- }
2659
- }
2660
- // W1: the nine-column SELECT was cloned four times (plan rule 8); one
2661
- // definition so a sixth caller can't drift from the other five.
2662
- const HANDOFF_COLUMNS = 'id, session_id, repo_root, task_id, summary, next_action, artifacts_json, scope, created_at, constraints_json, evidence_json, outcome, target_runtime, card_id';
2663
- /**
2664
- * Save a session handoff record. Returns the persisted handoff.
2665
- */
2666
- export function saveSessionHandoff(hippoRoot, tenantId, handoff) {
2667
- assertTenantId('saveSessionHandoff', tenantId);
2668
- initStore(hippoRoot);
2669
- const db = openHippoDb(hippoRoot);
2670
- const now = new Date().toISOString();
2671
- // v1.2: scope is wired through. Read-side default-deny in api.recall +
2672
- // cmdRecall continuity excludes slack:private:* and 'unknown:legacy'.
2673
- try {
2674
- const result = db.prepare(`
2675
- INSERT INTO session_handoffs(session_id, repo_root, task_id, summary, next_action, artifacts_json, scope, tenant_id, created_at, constraints_json, evidence_json, outcome, target_runtime, card_id)
2676
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2677
- `).run(handoff.sessionId, handoff.repoRoot ?? null, handoff.taskId ?? null, handoff.summary, handoff.nextAction ?? null, JSON.stringify(handoff.artifacts ?? []), handoff.scope ?? null, tenantId, now, JSON.stringify(handoff.constraints ?? []), handoff.evidence ? JSON.stringify(handoff.evidence) : null, handoff.outcome ?? null, handoff.targetRuntime ?? null, handoff.cardId ?? null);
2678
- const id = Number(result.lastInsertRowid ?? 0);
2679
- // SAFETY: row's shape matches HANDOFF_COLUMNS.
2680
- const row = db.prepare(`
2681
- SELECT ${HANDOFF_COLUMNS}
2682
- FROM session_handoffs
2683
- WHERE id = ?
2684
- `).get(id);
2685
- if (!row) {
2686
- throw new Error('Failed to reload saved session handoff');
2687
- }
2688
- return rowToSessionHandoff(row);
2689
- }
2690
- finally {
2691
- closeHippoDb(db);
2692
- }
2693
- }
2694
- /** Load the most recent handoff, optionally filtered by session ID. */
2695
- export function loadLatestHandoff(hippoRoot, tenantId, sessionId, opts = {}) {
2696
- assertTenantId('loadLatestHandoff', tenantId);
2697
- initStore(hippoRoot);
2698
- const db = openHippoDb(hippoRoot);
2699
- try {
2700
- const conditions = ['tenant_id = ?'];
2701
- const params = [tenantId];
2702
- if (sessionId) {
2703
- conditions.push('session_id = ?');
2704
- params.push(sessionId);
2705
- }
2706
- if (opts.unfinishedOnly) {
2707
- // codex P2: restrict to each session's newest revision first — stampHandoffOutcome
2708
- // only stamps the newest row, so an older null-outcome revision must not resurrect.
2709
- conditions.push(`id IN (SELECT MAX(id) FROM session_handoffs WHERE tenant_id = ? GROUP BY session_id)`);
2710
- params.push(tenantId);
2711
- conditions.push(`(outcome IS NULL OR outcome IN ('partial','failure'))`);
2712
- }
2713
- if (opts.maxAgeMs != null) {
2714
- conditions.push('created_at >= ?');
2715
- params.push(new Date(Date.now() - opts.maxAgeMs).toISOString());
2716
- }
2717
- if (opts.scopeFilter === 'default-deny') {
2718
- // codex P2: admit scope before LIMIT 1, else a newer denied row hides an older eligible one.
2719
- const placeholders = RECALL_DEFAULT_DENY_SCOPES.map(() => '?').join(', ');
2720
- conditions.push(`(scope IS NULL OR (scope NOT IN (${placeholders}) AND scope NOT LIKE '%:private:%'))`);
2721
- params.push(...RECALL_DEFAULT_DENY_SCOPES);
2722
- }
2723
- // SAFETY: row's shape matches HANDOFF_COLUMNS.
2724
- const row = db.prepare(`
2725
- SELECT ${HANDOFF_COLUMNS}
2726
- FROM session_handoffs
2727
- WHERE ${conditions.join(' AND ')}
2728
- ORDER BY created_at DESC, id DESC
2729
- LIMIT 1
2730
- `).get(...params);
2731
- return row ? rowToSessionHandoff(row) : null;
2732
- }
2733
- finally {
2734
- closeHippoDb(db);
2735
- }
2736
- }
2737
- /**
2738
- * Load a specific handoff by its row ID.
2739
- */
2740
- export function loadHandoffById(hippoRoot, tenantId, id) {
2741
- assertTenantId('loadHandoffById', tenantId);
2742
- initStore(hippoRoot);
2743
- const db = openHippoDb(hippoRoot);
2744
- try {
2745
- // SAFETY: row's shape matches HANDOFF_COLUMNS.
2746
- const row = db.prepare(`
2747
- SELECT ${HANDOFF_COLUMNS}
2748
- FROM session_handoffs
2749
- WHERE id = ? AND tenant_id = ?
2750
- `).get(id, tenantId);
2751
- return row ? rowToSessionHandoff(row) : null;
2752
- }
2753
- finally {
2754
- closeHippoDb(db);
2755
- }
2756
- }
2757
- /** Stamp the outcome on a session's newest handoff, only if it has none yet. Returns rows changed. */
2758
- export function stampHandoffOutcome(hippoRoot, tenantId, sessionId, outcome) {
2759
- assertTenantId('stampHandoffOutcome', tenantId);
2760
- initStore(hippoRoot);
2761
- const db = openHippoDb(hippoRoot);
2762
- try {
2763
- const result = db.prepare(`
2764
- UPDATE session_handoffs SET outcome = ?
2765
- WHERE tenant_id = ? AND session_id = ? AND outcome IS NULL
2766
- AND id = (
2767
- SELECT id FROM session_handoffs
2768
- WHERE tenant_id = ? AND session_id = ?
2769
- ORDER BY created_at DESC, id DESC LIMIT 1
2770
- )
2771
- `).run(outcome, tenantId, sessionId, tenantId, sessionId);
2772
- return Number(result.changes ?? 0);
2773
- }
2774
- finally {
2775
- closeHippoDb(db);
2776
- }
2777
- }
2778
- /**
2779
- * Auto-write a handoff at session-end from the session's active snapshot (DF1 T3).
2780
- * @param evidence best-effort git state; outcome comes from the newest session_complete event.
2781
- * @returns null unless the snapshot belongs to sessionId and no newer handoff already covers it.
2782
- */
2783
- export function writeSessionEndHandoff(hippoRoot, tenantId, sessionId, evidence) {
2784
- assertTenantId('writeSessionEndHandoff', tenantId);
2785
- const snapshot = loadActiveTaskSnapshot(hippoRoot, tenantId);
2786
- if (!snapshot || snapshot.session_id !== sessionId)
2787
- return null;
2788
- const existing = loadLatestHandoff(hippoRoot, tenantId, sessionId);
2789
- // Strict '>': a same-millisecond tie must not swallow the session's only write (test 6e).
2790
- if (existing && existing.updatedAt > snapshot.updated_at)
2791
- return null;
2792
- const db = openHippoDb(hippoRoot);
2793
- let outcome = null;
2794
- try {
2795
- // SAFETY: row's shape matches the single `content` column below.
2796
- const completeEvent = db.prepare(`
2797
- SELECT content FROM session_events
2798
- WHERE tenant_id = ? AND session_id = ? AND event_type = 'session_complete'
2799
- ORDER BY created_at DESC, id DESC LIMIT 1
2800
- `).get(tenantId, sessionId);
2801
- if (isHandoffOutcome(completeEvent?.content))
2802
- outcome = completeEvent.content;
2803
- }
2804
- finally {
2805
- closeHippoDb(db);
2806
- }
2807
- // codex P2: same-task refresh carries forward envelope fields nobody cleared,
2808
- // rather than dropping them when the snapshot rewrite has no opinion on them.
2809
- // codex P1: a scope mismatch must not leak private metadata into an unscoped envelope.
2810
- const carryForward = existing != null && existing.taskId === snapshot.task
2811
- && (existing.scope ?? null) === (snapshot.scope ?? null);
2812
- return saveSessionHandoff(hippoRoot, tenantId, {
2813
- version: 1,
2814
- sessionId,
2815
- repoRoot: carryForward ? existing.repoRoot : undefined,
2816
- taskId: snapshot.task,
2817
- summary: snapshot.summary,
2818
- nextAction: snapshot.next_step,
2819
- artifacts: carryForward ? existing.artifacts : [],
2820
- scope: snapshot.scope,
2821
- evidence,
2822
- outcome,
2823
- constraints: carryForward ? existing.constraints : undefined,
2824
- targetRuntime: carryForward ? existing.targetRuntime : undefined,
2825
- cardId: carryForward ? existing.cardId : undefined,
2826
- });
2827
- }
2828
- const CARD_COLUMNS = 'id, title, status, assignee_runtime, repo, contract, budget, lease_until, heartbeat_at, created_at, updated_at, tenant_id, scope';
2829
- function rowToCard(row) {
2830
- return {
2831
- id: row.id,
2832
- title: row.title,
2833
- status: row.status,
2834
- assigneeRuntime: row.assignee_runtime,
2835
- repo: row.repo,
2836
- contract: row.contract,
2837
- budget: row.budget,
2838
- leaseUntil: row.lease_until,
2839
- heartbeatAt: row.heartbeat_at,
2840
- createdAt: row.created_at,
2841
- updatedAt: row.updated_at,
2842
- tenantId: row.tenant_id,
2843
- scope: row.scope,
2844
- };
2845
- }
2846
- function loadCardRow(db, tenantId, id) {
2847
- // SAFETY: row's shape matches CARD_COLUMNS; status only ever holds a CardStatus value.
2848
- const row = db.prepare(`SELECT ${CARD_COLUMNS} FROM cards WHERE id = ? AND tenant_id = ?`).get(id, tenantId);
2849
- return row ? rowToCard(row) : null;
2850
- }
2851
- function rowToCardRun(row) {
2852
- return {
2853
- id: row.id,
2854
- card: row.card,
2855
- runtime: row.runtime,
2856
- sessionId: row.session_id,
2857
- started: row.started,
2858
- ended: row.ended,
2859
- outcome: row.outcome,
2860
- };
2861
- }
2862
- function rowToCardComment(row) {
2863
- return { id: row.id, cardId: row.card_id, author: row.author, body: row.body, createdAt: row.created_at };
2864
- }
2865
- function insertCardComment(db, tenantId, cardId, author, body) {
2866
- const now = new Date().toISOString();
2867
- const result = db.prepare(`
2868
- INSERT INTO card_comments (card_id, author, body, created_at, tenant_id)
2869
- SELECT ?, ?, ?, ?, ? WHERE EXISTS (SELECT 1 FROM cards WHERE id = ? AND tenant_id = ?)
2870
- `).run(cardId, author, body, now, tenantId, cardId, tenantId);
2871
- if (Number(result.changes ?? 0) === 0) {
2872
- throw new Error(`unknown card id: ${cardId}`);
2873
- }
2874
- const id = Number(result.lastInsertRowid ?? 0);
2875
- return { id, cardId, author, body, createdAt: now };
2876
- }
2877
- function leaseUntilFrom(now) {
2878
- return new Date(Date.parse(now) + CARD_LEASE_MS).toISOString();
2879
- }
2880
- function assertRunId(runId) {
2881
- if (!Number.isSafeInteger(runId) || runId <= 0) {
2882
- throw new Error(`Invalid run id: ${runId} (expected a positive integer)`);
2883
- }
2884
- }
2885
- // A second run that has not ended means a corrupt store; throw rather than guess which run the caller means.
2886
- function isLiveRun(db, tenantId, cardId, runId) {
2887
- // SAFETY: rows' shape matches the single `id` column named in the SELECT below.
2888
- const rows = db.prepare(`SELECT id FROM card_runs WHERE tenant_id = ? AND card = ? AND ended IS NULL`).all(tenantId, cardId);
2889
- if (rows.length > 1) {
2890
- throw new Error(`card ${cardId} has ${rows.length} runs that have not ended`);
2891
- }
2892
- return rows[0]?.id === runId;
2893
- }
2894
- function closeLiveRun(db, tenantId, cardId, outcome, now) {
2895
- db.prepare(`
2896
- UPDATE card_runs SET ended = ?, outcome = ?, updated_at = ?
2897
- WHERE card = ? AND tenant_id = ? AND ended IS NULL
2898
- `).run(now, outcome, now, cardId, tenantId);
2899
- }
2900
- // The single status-mutating seam (rule 15): CARD_TRANSITIONS is the one
2901
- // runtime authority, so a hand-copied wrong `from` list fails fast here.
2902
- export function transitionCard(db, tenantId, cardId, from, to, extra) {
2903
- for (const status of from) {
2904
- if (!CARD_TRANSITIONS[status].includes(to)) {
2905
- throw new Error(`illegal card transition: ${status} -> ${to}`);
2906
- }
2907
- }
2908
- const now = new Date().toISOString();
2909
- // Lease columns follow status: set on the move to running, cleared on every other move (rule 15).
2910
- const lease = to === 'running' ? [leaseUntilFrom(now), now] : [null, null];
2911
- const fromPlaceholders = from.map(() => '?').join(', ');
2912
- const sql = `
2913
- UPDATE cards SET status = ?, updated_at = ?, lease_until = ?, heartbeat_at = ?${extra?.setSql ? `, ${extra.setSql}` : ''}
2914
- WHERE id = ? AND tenant_id = ? AND status IN (${fromPlaceholders})${extra?.whereSql ? ` AND ${extra.whereSql}` : ''}
2915
- `;
2916
- const params = [to, now, ...lease, ...(extra?.params ?? []), cardId, tenantId, ...from];
2917
- const result = db.prepare(sql).run(...params);
2918
- return Number(result.changes ?? 0);
2919
- }
2920
- /** Creates a card; status is ready with no deps or once every dependsOn id is done, else backlog. An unknown dependsOn id throws and commits nothing. A repeated dependsOn id is recorded once. */
2921
- export function createCard(hippoRoot, tenantId, input) {
2922
- assertTenantId('createCard', tenantId);
2923
- if (input.title.trim() === '') {
2924
- throw new Error('title must not be empty');
2925
- }
2926
- if (input.budget !== undefined && !(Number.isSafeInteger(input.budget) && input.budget > 0)) {
2927
- throw new Error(`Invalid budget: ${input.budget} (expected a positive integer)`);
2928
- }
2929
- initStore(hippoRoot);
2930
- const db = openHippoDb(hippoRoot);
2931
- try {
2932
- const dependsOn = [...new Set(input.dependsOn ?? [])];
2933
- let id = '';
2934
- db.exec('BEGIN IMMEDIATE');
2935
- try {
2936
- // Probe runs inside the transaction (mirrors batchWriteAndDelete): a parent
2937
- // completing between an outside-the-lock read and the INSERT would strand the child.
2938
- let allParentsDone = true;
2939
- if (dependsOn.length > 0) {
2940
- const placeholders = dependsOn.map(() => '?').join(', ');
2941
- // SAFETY: rows' shape matches the two columns named in the SELECT below.
2942
- const rows = db.prepare(`SELECT id, status FROM cards WHERE tenant_id = ? AND id IN (${placeholders})`).all(tenantId, ...dependsOn);
2943
- const found = new Map(rows.map((r) => [r.id, r.status]));
2944
- // Pre-check before any write: a typo'd --depends-on can never commit a card row.
2945
- for (const parentId of dependsOn) {
2946
- if (!found.has(parentId)) {
2947
- throw new Error(`unknown parent card id: ${parentId}`);
2948
- }
2949
- }
2950
- allParentsDone = dependsOn.every((pid) => found.get(pid) === 'done');
2951
- }
2952
- id = generateId('card');
2953
- const now = new Date().toISOString();
2954
- const status = dependsOn.length === 0 || allParentsDone ? 'ready' : 'backlog';
2955
- db.prepare(`
2956
- INSERT INTO cards (id, title, status, repo, contract, budget, created_at, updated_at, tenant_id)
2957
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
2958
- `).run(id, input.title, status, input.repo ?? null, input.contract ?? null, input.budget ?? null, now, now, tenantId);
2959
- for (const parentId of dependsOn) {
2960
- db.prepare(`
2961
- INSERT INTO card_deps (parent, child, tenant_id, created_at) VALUES (?, ?, ?, ?)
2962
- `).run(parentId, id, tenantId, now);
2963
- }
2964
- db.exec('COMMIT');
2965
- }
2966
- catch (error) {
2967
- try {
2968
- db.exec('ROLLBACK');
2969
- }
2970
- catch { /* commit may have already rolled back */ }
2971
- throw error;
2972
- }
2973
- return loadCardRow(db, tenantId, id);
2974
- }
2975
- finally {
2976
- closeHippoDb(db);
2977
- }
2978
- }
2979
- /** Returns the card row for id, or null if it does not exist under this tenant. */
2980
- export function loadCard(hippoRoot, tenantId, id) {
2981
- assertTenantId('loadCard', tenantId);
2982
- initStore(hippoRoot);
2983
- const db = openHippoDb(hippoRoot);
2984
- try {
2985
- return loadCardRow(db, tenantId, id);
2986
- }
2987
- finally {
2988
- closeHippoDb(db);
2989
- }
2990
- }
2991
- /** Lists cards for this tenant, optionally filtered to one status, newest-updated first. */
2992
- export function listCards(hippoRoot, tenantId, opts = {}) {
2993
- assertTenantId('listCards', tenantId);
2994
- initStore(hippoRoot);
2995
- const db = openHippoDb(hippoRoot);
2996
- try {
2997
- const conditions = ['tenant_id = ?'];
2998
- const params = [tenantId];
2999
- if (opts.status) {
3000
- conditions.push('status = ?');
3001
- params.push(opts.status);
3002
- }
3003
- // SAFETY: rows' shape matches CARD_COLUMNS; status only ever holds a CardStatus value.
3004
- const rows = db.prepare(`
3005
- SELECT ${CARD_COLUMNS} FROM cards WHERE ${conditions.join(' AND ')} ORDER BY updated_at DESC, id DESC
3006
- `).all(...params);
3007
- return rows.map(rowToCard);
3008
- }
3009
- finally {
3010
- closeHippoDb(db);
3011
- }
3012
- }
3013
- /** Returns this card's parent and child ids from card_deps. */
3014
- export function loadCardDeps(hippoRoot, tenantId, id) {
3015
- assertTenantId('loadCardDeps', tenantId);
3016
- initStore(hippoRoot);
3017
- const db = openHippoDb(hippoRoot);
3018
- try {
3019
- // SAFETY: rows' shape matches the single `parent` column named in the SELECT below.
3020
- const parents = db.prepare(`SELECT parent FROM card_deps WHERE tenant_id = ? AND child = ?`).all(tenantId, id).map((r) => r.parent);
3021
- // SAFETY: rows' shape matches the single `child` column named in the SELECT below.
3022
- const children = db.prepare(`SELECT child FROM card_deps WHERE tenant_id = ? AND parent = ?`).all(tenantId, id).map((r) => r.child);
3023
- return { parents, children };
3024
- }
3025
- finally {
3026
- closeHippoDb(db);
3027
- }
3028
- }
3029
- /** Returns this card's run history, most recent first. */
3030
- export function loadCardRuns(hippoRoot, tenantId, id) {
3031
- assertTenantId('loadCardRuns', tenantId);
3032
- initStore(hippoRoot);
3033
- const db = openHippoDb(hippoRoot);
3034
- try {
3035
- // SAFETY: rows' shape matches CardRunRow.
3036
- const rows = db.prepare(`
3037
- SELECT id, card, runtime, session_id, started, ended, outcome
3038
- FROM card_runs WHERE tenant_id = ? AND card = ? ORDER BY started DESC, id DESC
3039
- `).all(tenantId, id);
3040
- return rows.map(rowToCardRun);
3041
- }
3042
- finally {
3043
- closeHippoDb(db);
3044
- }
3045
- }
3046
- /** Returns this card's comments, most recent first. */
3047
- export function loadCardComments(hippoRoot, tenantId, id) {
3048
- assertTenantId('loadCardComments', tenantId);
3049
- initStore(hippoRoot);
3050
- const db = openHippoDb(hippoRoot);
3051
- try {
3052
- // SAFETY: rows' shape matches CardCommentRow.
3053
- const rows = db.prepare(`
3054
- SELECT id, card_id, author, body, created_at
3055
- FROM card_comments WHERE tenant_id = ? AND card_id = ? ORDER BY created_at DESC, id DESC
3056
- `).all(tenantId, id);
3057
- return rows.map(rowToCardComment);
3058
- }
3059
- finally {
3060
- closeHippoDb(db);
3061
- }
3062
- }
3063
- /** Read side of the card <-> handoff round trip: the newest handoff filed against this card. */
3064
- export function loadLatestHandoffForCard(hippoRoot, tenantId, cardId) {
3065
- assertTenantId('loadLatestHandoffForCard', tenantId);
3066
- initStore(hippoRoot);
3067
- const db = openHippoDb(hippoRoot);
3068
- try {
3069
- // SAFETY: row's shape matches HANDOFF_COLUMNS.
3070
- const row = db.prepare(`
3071
- SELECT ${HANDOFF_COLUMNS} FROM session_handoffs
3072
- WHERE tenant_id = ? AND card_id = ? ORDER BY created_at DESC, id DESC LIMIT 1
3073
- `).get(tenantId, cardId);
3074
- return row ? rowToSessionHandoff(row) : null;
3075
- }
3076
- finally {
3077
- closeHippoDb(db);
3078
- }
3079
- }
3080
- /** Atomic claim: WHERE status IN (ready, blocked) AND assignee_runtime IS NULL decides the race. Throws on an unknown card id; returns null for a card not ready/blocked or already claimed. Sets a CARD_LEASE_MS lease and returns the new run's id as runId. */
3081
- export function claimCard(hippoRoot, tenantId, id, runtime, sessionId) {
3082
- assertTenantId('claimCard', tenantId);
3083
- if (runtime.trim() === '') {
3084
- throw new Error('runtime must not be empty');
3085
- }
3086
- initStore(hippoRoot);
3087
- const db = openHippoDb(hippoRoot);
3088
- try {
3089
- db.exec('BEGIN IMMEDIATE');
3090
- let runId = 0;
3091
- try {
3092
- const changes = transitionCard(db, tenantId, id, ['ready', 'blocked'], 'running', {
3093
- setSql: 'assignee_runtime = ?',
3094
- whereSql: 'assignee_runtime IS NULL',
3095
- params: [runtime],
3096
- });
3097
- if (changes === 0) {
3098
- if (!loadCardRow(db, tenantId, id)) {
3099
- throw new Error(`unknown card id: ${id}`);
3100
- }
3101
- db.exec('ROLLBACK');
3102
- return null;
3103
- }
3104
- const now = new Date().toISOString();
3105
- const insert = db.prepare(`
3106
- INSERT INTO card_runs (card, runtime, session_id, started, created_at, updated_at, tenant_id)
3107
- VALUES (?, ?, ?, ?, ?, ?, ?)
3108
- `).run(id, runtime, sessionId ?? null, now, now, now, tenantId);
3109
- runId = Number(insert.lastInsertRowid ?? 0);
3110
- db.exec('COMMIT');
3111
- }
3112
- catch (error) {
3113
- try {
3114
- db.exec('ROLLBACK');
3115
- }
3116
- catch { /* commit may have already rolled back */ }
3117
- throw error;
3118
- }
3119
- return { ...loadCardRow(db, tenantId, id), runId };
3120
- }
3121
- finally {
3122
- closeHippoDb(db);
3123
- }
3124
- }
3125
- /** Moves a running card's lease to CARD_LEASE_MS from now and records the heartbeat; updated_at is left alone. Throws on an unknown card id or a run id that is not a positive integer; returns null unless the card is running and runId is its live run. */
3126
- export function heartbeatCard(hippoRoot, tenantId, id, runId) {
3127
- assertTenantId('heartbeatCard', tenantId);
3128
- assertRunId(runId);
3129
- initStore(hippoRoot);
3130
- const db = openHippoDb(hippoRoot);
3131
- try {
3132
- db.exec('BEGIN IMMEDIATE');
3133
- try {
3134
- const card = loadCardRow(db, tenantId, id);
3135
- if (!card) {
3136
- throw new Error(`unknown card id: ${id}`);
3137
- }
3138
- if (card.status !== 'running' || !isLiveRun(db, tenantId, id, runId)) {
3139
- db.exec('ROLLBACK');
3140
- return null;
3141
- }
3142
- const now = new Date().toISOString();
3143
- db.prepare(`UPDATE cards SET lease_until = ?, heartbeat_at = ? WHERE id = ? AND tenant_id = ?`)
3144
- .run(leaseUntilFrom(now), now, id, tenantId);
3145
- db.exec('COMMIT');
3146
- }
3147
- catch (error) {
3148
- try {
3149
- db.exec('ROLLBACK');
3150
- }
3151
- catch { /* commit may have already rolled back */ }
3152
- throw error;
3153
- }
3154
- return loadCardRow(db, tenantId, id);
3155
- }
3156
- finally {
3157
- closeHippoDb(db);
3158
- }
3159
- }
3160
- /** Requires the card be running; closes the live run as blocked and files reason as a comment. Throws on an unknown card id; returns null for a card not running. When runId is given, returns null unless it is the card's live run. */
3161
- export function blockCard(hippoRoot, tenantId, id, reason, runId) {
3162
- assertTenantId('blockCard', tenantId);
3163
- if (reason.trim() === '') {
3164
- throw new Error('reason must not be empty');
3165
- }
3166
- if (runId !== undefined)
3167
- assertRunId(runId);
3168
- initStore(hippoRoot);
3169
- const db = openHippoDb(hippoRoot);
3170
- try {
3171
- db.exec('BEGIN IMMEDIATE');
3172
- try {
3173
- const allowed = runId === undefined || isLiveRun(db, tenantId, id, runId);
3174
- const changes = allowed ? transitionCard(db, tenantId, id, ['running'], 'blocked', { setSql: 'assignee_runtime = NULL' }) : 0;
3175
- if (changes === 0) {
3176
- if (!loadCardRow(db, tenantId, id)) {
3177
- throw new Error(`unknown card id: ${id}`);
3178
- }
3179
- db.exec('ROLLBACK');
3180
- return null;
3181
- }
3182
- const now = new Date().toISOString();
3183
- // Close the interrupted run here so completeCard's ended IS NULL scope only ever matches the live run.
3184
- closeLiveRun(db, tenantId, id, 'blocked', now);
3185
- insertCardComment(db, tenantId, id, 'system', reason);
3186
- db.exec('COMMIT');
3187
- }
3188
- catch (error) {
3189
- try {
3190
- db.exec('ROLLBACK');
3191
- }
3192
- catch { /* commit may have already rolled back */ }
3193
- throw error;
3194
- }
3195
- return loadCardRow(db, tenantId, id);
3196
- }
3197
- finally {
3198
- closeHippoDb(db);
3199
- }
3200
- }
3201
- /** Requires the card be running; moves it to review, clearing its lease and heartbeat and keeping its live run. When runId is given, returns null unless it is the card's live run. Throws on an unknown card id; returns null for a card not running. */
3202
- export function reviewCard(hippoRoot, tenantId, id, runId) {
3203
- assertTenantId('reviewCard', tenantId);
3204
- if (runId !== undefined)
3205
- assertRunId(runId);
3206
- initStore(hippoRoot);
3207
- const db = openHippoDb(hippoRoot);
3208
- try {
3209
- db.exec('BEGIN IMMEDIATE');
3210
- try {
3211
- const allowed = runId === undefined || isLiveRun(db, tenantId, id, runId);
3212
- const changes = allowed ? transitionCard(db, tenantId, id, ['running'], 'review') : 0;
3213
- if (changes === 0) {
3214
- if (!loadCardRow(db, tenantId, id)) {
3215
- throw new Error(`unknown card id: ${id}`);
3216
- }
3217
- db.exec('ROLLBACK');
3218
- return null;
3219
- }
3220
- db.exec('COMMIT');
3221
- }
3222
- catch (error) {
3223
- try {
3224
- db.exec('ROLLBACK');
3225
- }
3226
- catch { /* commit may have already rolled back */ }
3227
- throw error;
3228
- }
3229
- return loadCardRow(db, tenantId, id);
3230
- }
3231
- finally {
3232
- closeHippoDb(db);
3233
- }
3234
- }
3235
- /** Requires the card be in review; closes the live run with outcome. Outcome 'success' moves the card to done and, in the same transaction, promotes any child whose parents are now all done; 'failure' or 'partial' moves it to shelved and promotes nothing. Throws on an unknown card id; returns null for a card not in review. When runId is given, returns null unless it is the card's live run. */
3236
- export function completeCard(hippoRoot, tenantId, id, outcome, runId) {
3237
- assertTenantId('completeCard', tenantId);
3238
- if (!isHandoffOutcome(outcome)) {
3239
- throw new Error(`invalid card outcome: ${String(outcome)}`);
3240
- }
3241
- if (runId !== undefined)
3242
- assertRunId(runId);
3243
- initStore(hippoRoot);
3244
- const db = openHippoDb(hippoRoot);
3245
- try {
3246
- db.exec('BEGIN IMMEDIATE');
3247
- let promotedChildren = [];
3248
- try {
3249
- const target = outcome === 'success' ? 'done' : 'shelved';
3250
- const allowed = runId === undefined || isLiveRun(db, tenantId, id, runId);
3251
- const changes = allowed ? transitionCard(db, tenantId, id, ['review'], target) : 0;
3252
- if (changes === 0) {
3253
- if (!loadCardRow(db, tenantId, id)) {
3254
- throw new Error(`unknown card id: ${id}`);
3255
- }
3256
- db.exec('ROLLBACK');
3257
- return null;
3258
- }
3259
- const now = new Date().toISOString();
3260
- closeLiveRun(db, tenantId, id, outcome, now);
3261
- // Not best-effort (rule 12): promotion runs in this same transaction, so a
3262
- // card can never be `done` with an un-evaluated child.
3263
- if (target === 'done') {
3264
- // SAFETY: rows' shape matches the single `child` column named in the SELECT below.
3265
- const children = db.prepare(`SELECT child FROM card_deps WHERE tenant_id = ? AND parent = ?`).all(tenantId, id).map((r) => r.child);
3266
- for (const childId of children) {
3267
- // SAFETY: row's shape matches the single `status` column named in the SELECT below.
3268
- const child = db.prepare(`SELECT status FROM cards WHERE tenant_id = ? AND id = ?`).get(tenantId, childId);
3269
- if (!child || child.status !== 'backlog')
3270
- continue;
3271
- // SAFETY: rows' shape matches the single `parent` column named in the SELECT below.
3272
- const parents = db.prepare(`SELECT parent FROM card_deps WHERE tenant_id = ? AND child = ?`).all(tenantId, childId).map((r) => r.parent);
3273
- const placeholders = parents.map(() => '?').join(', ');
3274
- // SAFETY: row's shape matches the single `c` column named in the SELECT below.
3275
- const doneCount = db.prepare(`SELECT COUNT(*) as c FROM cards WHERE tenant_id = ? AND id IN (${placeholders}) AND status = 'done'`).get(tenantId, ...parents).c;
3276
- if (doneCount === parents.length) {
3277
- transitionCard(db, tenantId, childId, ['backlog'], 'ready');
3278
- promotedChildren.push(childId);
3279
- }
3280
- }
3281
- }
3282
- db.exec('COMMIT');
3283
- }
3284
- catch (error) {
3285
- try {
3286
- db.exec('ROLLBACK');
3287
- }
3288
- catch { /* commit may have already rolled back */ }
3289
- throw error;
3290
- }
3291
- return { card: loadCardRow(db, tenantId, id), promotedChildren };
3292
- }
3293
- finally {
3294
- closeHippoDb(db);
3295
- }
3296
- }
3297
- /** Returns to ready every running card of the tenant whose lease has expired or is missing: clears its assignee, closes its live run as 'reclaimed' and leaves its handoffs alone, all in one write transaction. Returns the reclaimed card ids in id order. */
3298
- export function reclaimExpiredCards(hippoRoot, tenantId) {
3299
- assertTenantId('reclaimExpiredCards', tenantId);
3300
- initStore(hippoRoot);
3301
- const db = openHippoDb(hippoRoot);
3302
- try {
3303
- db.exec('BEGIN IMMEDIATE');
3304
- try {
3305
- // Read lease times under the write lock, so a heartbeat that committed while we waited wins.
3306
- const now = new Date().toISOString();
3307
- // SAFETY: rows' shape matches the single `id` column named in the SELECT below.
3308
- const ids = db.prepare(`
3309
- SELECT id FROM cards
3310
- WHERE tenant_id = ? AND status = 'running' AND (lease_until IS NULL OR lease_until < ?)
3311
- ORDER BY id
3312
- `).all(tenantId, now).map((r) => r.id);
3313
- for (const id of ids) {
3314
- transitionCard(db, tenantId, id, ['running'], 'ready', { setSql: 'assignee_runtime = NULL' });
3315
- closeLiveRun(db, tenantId, id, 'reclaimed', now);
3316
- }
3317
- db.exec('COMMIT');
3318
- return ids;
3319
- }
3320
- catch (error) {
3321
- try {
3322
- db.exec('ROLLBACK');
3323
- }
3324
- catch { /* commit may have already rolled back */ }
3325
- throw error;
3326
- }
3327
- }
3328
- finally {
3329
- closeHippoDb(db);
3330
- }
3331
- }
3332
- /** Appends a comment to cardId in any card status; throws if cardId is not a card of this tenant. */
3333
- export function addCardComment(hippoRoot, tenantId, cardId, author, body) {
3334
- assertTenantId('addCardComment', tenantId);
3335
- if (body.trim() === '') {
3336
- throw new Error('body must not be empty');
3337
- }
3338
- initStore(hippoRoot);
3339
- const db = openHippoDb(hippoRoot);
3340
- try {
3341
- return insertCardComment(db, tenantId, cardId, author, body);
3342
- }
3343
- finally {
3344
- closeHippoDb(db);
3345
- }
3346
- }
3347
- // ---------------------------------------------------------------------------
3348
- // v0.30 / E1 of DAG live-coupling — dirty-flag helpers for the existing
3349
- // DAG layer's level-2 summaries.
3350
- //
3351
- // Used by E2 (child-write propagation in invalidation.ts / writeEntry /
3352
- // forgetMemory / archiveRawMemory) to mark a summary dirty when one of its
3353
- // children changes, and by E3's sleep-cycle rebuildDirtySummaries phase to
3354
- // enumerate candidates without scanning every memory row.
3355
- // ---------------------------------------------------------------------------
3356
- /**
3357
- * Load summaries flagged dirty for the given tenant. Sorted by latest_at
3358
- * DESC (NULLS LAST) so E3's rebuild cap (HIPPO_DAG_REBUILD_CAP, default 20)
3359
- * takes the most-recently-changed summaries first.
3360
- *
3361
- * Returns full MemoryEntry shape via MEMORY_SELECT_COLUMNS + rowToEntry
3362
- * (v28 fields are part of the standard read path).
3363
- */
3364
- export function loadDirtySummaries(hippoRoot, tenantId) {
3365
- assertTenantId('loadDirtySummaries', tenantId);
3366
- initStore(hippoRoot);
3367
- const db = openHippoDb(hippoRoot);
3368
- try {
3369
- // SAFETY: this query selects exactly MEMORY_SELECT_COLUMNS, matching
3370
- // MemoryRow's field set.
3371
- const rows = db.prepare(`
3372
- SELECT ${MEMORY_SELECT_COLUMNS}
3373
- FROM memories
3374
- WHERE summary_dirty = 1
3375
- AND tenant_id = ?
3376
- AND kind != 'archived'
3377
- ORDER BY latest_at DESC NULLS LAST, id ASC
3378
- `).all(tenantId);
3379
- return rows.map(rowToEntry);
3380
- }
3381
- finally {
3382
- closeHippoDb(db);
3383
- }
3384
- }
3385
- /**
3386
- * v0.30 / E2 — in-transaction variant of markSummaryDirty. Takes an open
3387
- * db (caller is responsible for any SAVEPOINT/BEGIN). Used by E2's hook
3388
- * sites: writeEntryDbOnly, api.supersede CAS, deleteEntry, archiveRawMemory,
3389
- * batchWriteAndDelete. Each child mutation's dirty-mark is atomic with the
3390
- * mutation itself (where the mutation IS in a SAVEPOINT/BEGIN — deleteEntry
3391
- * is the exception, acceptably non-atomic by design).
3392
- *
3393
- * EXPORTED (required for cross-module use by api.ts + raw-archive.ts).
3394
- * Risk of misuse (caller without open tx) is mitigated by the InTx
3395
- * suffix + the DatabaseSyncLike typed param. Public surface for end users
3396
- * stays at the markSummaryDirty (own-connection) variant.
3397
- *
3398
- * Same idempotency contract: 0->1 transition only, audit row only on
3399
- * transition, no-op on non-summary / archived / unknown id / cross-tenant.
3400
- */
3401
- export function markSummaryDirtyInTx(db, summaryId, tenantId, actor) {
3402
- // v0.30 / E5: widened dag_level=2 -> IN (2, 3). RETURNING dag_level reads
3403
- // actual level in same round trip so audit metadata stays accurate without
3404
- // a SELECT-before-UPDATE extra DB op on this hot path (5 caller sites).
3405
- // SAFETY: result's shape matches the single `dag_level` column returned
3406
- // above.
3407
- const result = db.prepare(`
3408
- UPDATE memories
3409
- SET summary_dirty = 1
3410
- WHERE id = ?
3411
- AND tenant_id = ?
3412
- AND dag_level IN (2, 3)
3413
- AND summary_dirty = 0
3414
- AND kind != 'archived'
3415
- RETURNING dag_level
3416
- `).get(summaryId, tenantId);
3417
- if (result) {
3418
- audit(db, 'summary_marked_dirty', summaryId, { dag_level: result.dag_level, source: 'E2' }, actor, tenantId);
3419
- }
3420
- }
3421
- /**
3422
- * Mark a summary as dirty. Idempotent (re-marking dirty is a no-op + no
3423
- * second audit row). Tenant-scoped to prevent cross-tenant writes via
3424
- * parent-lookup. Called by E2 from invalidation.ts / writeEntry /
3425
- * forgetMemory / archiveRawMemory whenever a child is invalidated,
3426
- * superseded, forgotten, or archived.
3427
- *
3428
- * Quietly no-ops if the target row doesn't exist or isn't a level-2
3429
- * summary (E5 will widen the dag_level guard to IN (2, 3) when level-3
3430
- * build path lands). Emits a 'summary_marked_dirty' audit row on actual
3431
- * state transitions (0 -> 1) via the audit() helper, which try/catches
3432
- * for missing audit_log (the v27 self-heal scenario).
3433
- */
3434
- export function markSummaryDirty(hippoRoot, summaryId, tenantId, actor = 'cli') {
3435
- assertTenantId('markSummaryDirty', tenantId);
3436
- initStore(hippoRoot);
3437
- const db = openHippoDb(hippoRoot);
3438
- try {
3439
- // v0.30 / E5: widened dag_level=2 -> IN (2, 3). RETURNING dag_level reads
3440
- // actual level in same round trip.
3441
- // SAFETY: result's shape matches the single `dag_level` column returned
3442
- // above.
3443
- const result = db.prepare(`
3444
- UPDATE memories
3445
- SET summary_dirty = 1
3446
- WHERE id = ?
3447
- AND tenant_id = ?
3448
- AND dag_level IN (2, 3)
3449
- AND summary_dirty = 0
3450
- AND kind != 'archived'
3451
- RETURNING dag_level
3452
- `).get(summaryId, tenantId);
3453
- if (result) {
3454
- // audit() wraps appendAuditEvent in try/catch (v27 heal scenario).
3455
- // metadata.source=E1 leaves a breadcrumb so E2-E5 debugging can
3456
- // distinguish dirty-marks across the arc's wiring layers.
3457
- audit(db, 'summary_marked_dirty', summaryId, { dag_level: result.dag_level, source: 'E1' }, actor, tenantId);
3458
- }
3459
- }
3460
- finally {
3461
- closeHippoDb(db);
3462
- }
3463
- }
3464
- // ---------------------------------------------------------------------------
3465
- // v0.30 / E3 of DAG live-coupling — sleep-cycle rebuild surface.
3466
- //
3467
- // loadAllDirtySummaries / loadChildrenOfSummary / applyRebuildResult /
3468
- // clearSummaryDirtyAfterBuild live HERE (not in dag.ts) because they need
3469
- // module-private MEMORY_SELECT_COLUMNS, MemoryRow, rowToEntry, audit,
3470
- // syncFtsRow, assertTenantId. dag.ts owns only the thin orchestrator
3471
- // rebuildDirtySummaries() that calls into these.
3472
- // ---------------------------------------------------------------------------
3473
- /**
3474
- * v0.30 / E5 — host-wide loader for L2 topic summaries without an L3 parent.
3475
- * Used by consolidate phase 1.9 (buildEntityProfiles) to cluster L2s into
3476
- * L3 entity profiles. Mirrors loadAllDirtySummaries pattern (E3): SQL-level
3477
- * filter is cheaper than reusing in-memory `survivors` (which doesn't
3478
- * contain L2s freshly created by phase 1.7 buildDag).
3479
- *
3480
- * Returns entries with tenantId attached so per-cluster writes stay
3481
- * tenant-scoped via summary.tenantId.
3482
- */
3483
- export function loadAllL2Summaries(hippoRoot) {
3484
- initStore(hippoRoot);
3485
- const db = openHippoDb(hippoRoot);
3486
- try {
3487
- // SAFETY: this query selects exactly MEMORY_SELECT_COLUMNS, matching
3488
- // MemoryRow's field set.
3489
- const rows = db.prepare(`
3490
- SELECT ${MEMORY_SELECT_COLUMNS}
3491
- FROM memories
3492
- WHERE dag_level = 2
3493
- AND dag_parent_id IS NULL
3494
- AND kind != 'archived'
3495
- ORDER BY created ASC, id ASC
3496
- `).all();
3497
- return rows.map(rowToEntry);
3498
- }
3499
- finally {
3500
- closeHippoDb(db);
3501
- }
3502
- }
3503
- /**
3504
- * v0.30 / E3 — host-wide variant of loadDirtySummaries. Iterates all tenants
3505
- * in one query so consolidate.ts (host-wide per L106-109) does not need a
3506
- * per-tenant loop. Each returned MemoryEntry carries its own tenantId (via
3507
- * rowToEntry), so per-summary children + rebuild UPDATE stay tenant-scoped.
3508
- *
3509
- * Sort: latest_at DESC NULLS LAST, id ASC — same as per-tenant variant so
3510
- * HIPPO_DAG_REBUILD_CAP takes most-recently-changed summaries first.
3511
- */
3512
- export function loadAllDirtySummaries(hippoRoot) {
3513
- initStore(hippoRoot);
3514
- const db = openHippoDb(hippoRoot);
3515
- try {
3516
- // SAFETY: this query selects exactly MEMORY_SELECT_COLUMNS, matching
3517
- // MemoryRow's field set.
3518
- const rows = db.prepare(`
3519
- SELECT ${MEMORY_SELECT_COLUMNS}
3520
- FROM memories
3521
- WHERE summary_dirty = 1
3522
- AND kind != 'archived'
3523
- ORDER BY latest_at DESC NULLS LAST, id ASC
3524
- `).all();
3525
- return rows.map(rowToEntry);
3526
- }
3527
- finally {
3528
- closeHippoDb(db);
3529
- }
3530
- }
3531
- /**
3532
- * v0.30 / E3 — load live children of a DAG summary. Used by
3533
- * rebuildDirtySummaries to regenerate content from the CURRENT child set
3534
- * (not the children at create-time). Skips archived. Tenant-scoped
3535
- * (defence in depth — dag_parent_id is unique-ish but tenant guard is
3536
- * cheap). created column is TEXT NOT NULL since db.ts schema v1.
3537
- */
3538
- export function loadChildrenOfSummary(hippoRoot, summaryId, tenantId) {
3539
- assertTenantId('loadChildrenOfSummary', tenantId);
3540
- initStore(hippoRoot);
3541
- const db = openHippoDb(hippoRoot);
3542
- try {
3543
- // SAFETY: this query selects exactly MEMORY_SELECT_COLUMNS, matching
3544
- // MemoryRow's field set.
3545
- const rows = db.prepare(`
3546
- SELECT ${MEMORY_SELECT_COLUMNS}
3547
- FROM memories
3548
- WHERE dag_parent_id = ?
3549
- AND tenant_id = ?
3550
- AND kind != 'archived'
3551
- ORDER BY created ASC
3552
- `).all(summaryId, tenantId);
3553
- return rows.map(rowToEntry);
3554
- }
3555
- finally {
3556
- closeHippoDb(db);
3557
- }
3558
- }
3559
- /**
3560
- * v0.30 / E3 — apply a rebuild result to a dirty summary. Atomic: one
3561
- * prepared UPDATE statement plus syncFtsRow inside one SAVEPOINT.
3562
- * WHERE includes `AND summary_dirty = 1` so concurrent sleep's race-loser
3563
- * becomes a no-op (no rebuild_count bump, no audit row).
3564
- *
3565
- * Returns `{ changed, refused }`. `changed` is true when this call's UPDATE
3566
- * (content or metadata-only) affected a row; false on race-loss / unknown id
3567
- * / archived / wrong dag_level. `refused` is true only when a tombstone hit
3568
- * suppressed the content write AND the metadata UPDATE still landed — see
3569
- * the return-semantics comment below for the full contract.
3570
- */
3571
- export function applyRebuildResult(hippoRoot, summary, patch) {
3572
- assertTenantId('applyRebuildResult', summary.tenantId);
3573
- initStore(hippoRoot);
3574
- const db = openHippoDb(hippoRoot);
3575
- try {
3576
- db.exec('SAVEPOINT rebuild_summary');
3577
- try {
3578
- const nowIso = new Date().toISOString();
3579
- // AT1 P1a fix (docs/plans/2026-08-15-at1-rejected-value-tombstone.md):
3580
- // applyRebuildResult's bumpRebuildCount branch wrote patch.content via
3581
- // a direct UPDATE, bypassing the rejection guard entirely (the guard
3582
- // lives in upsertEntryRow's INSERT path, which this function never
3583
- // calls). A rebuild that regenerates byte-identical content to an
3584
- // already-rejected value (e.g. deterministic summarization of an
3585
- // unchanged child set) would silently re-assert it every sleep cycle.
3586
- // Check BEFORE choosing which UPDATE to run — only the
3587
- // bumpRebuildCount branch ever writes content, so a miss or a
3588
- // zero-child call is a no-op here (one indexed point query, guarded
3589
- // path only).
3590
- const tombstone = patch.bumpRebuildCount
3591
- ? findRejectedValue(db, summary.tenantId, rejectionDigest(patch.content))
3592
- : null;
3593
- // On a hit: do NOT write the new content. Fall through to the SAME
3594
- // metadata-only behavior the zero-child branch already has —
3595
- // descendant_count/earliest_at/latest_at update + summary_dirty
3596
- // cleared, no content write, no rebuild_count bump. Clearing dirty
3597
- // (rather than leaving it set) is deliberate: leaving it dirty would
3598
- // make every following sleep cycle re-attempt and re-refuse the
3599
- // identical rebuild forever (the DAG-loop this fix closes).
3600
- const applyContentWrite = patch.bumpRebuildCount && !tombstone;
3601
- // ONE prepared UPDATE per branch. Test #8 inspects the SQL string.
3602
- // v0.30 / E5: widened dag_level=2 -> IN (2, 3) on both branches.
3603
- const sql = applyContentWrite
3604
- ? `UPDATE memories
3605
- SET content = ?,
3606
- descendant_count = ?,
3607
- earliest_at = ?,
3608
- latest_at = ?,
3609
- last_rebuilt_at = ?,
3610
- rebuild_count = COALESCE(rebuild_count, 0) + 1,
3611
- summary_dirty = 0
3612
- WHERE id = ?
3613
- AND tenant_id = ?
3614
- AND dag_level IN (2, 3)
3615
- AND summary_dirty = 1
3616
- AND kind != 'archived'`
3617
- : `UPDATE memories
3618
- SET descendant_count = ?,
3619
- earliest_at = ?,
3620
- latest_at = ?,
3621
- summary_dirty = 0
3622
- WHERE id = ?
3623
- AND tenant_id = ?
3624
- AND dag_level IN (2, 3)
3625
- AND summary_dirty = 1
3626
- AND kind != 'archived'`;
3627
- const result = applyContentWrite
3628
- ? db.prepare(sql).run(patch.content, patch.descendant_count, patch.earliest_at, patch.latest_at, nowIso, summary.id, summary.tenantId)
3629
- : db.prepare(sql).run(patch.descendant_count, patch.earliest_at, patch.latest_at, summary.id, summary.tenantId);
3630
- // Return-value semantics (v0.30/T4 split): `changed` reflects whether
3631
- // THIS call's UPDATE (content or metadata-only) affected a row — NOT
3632
- // whether patch.content specifically landed. On a refusal, metadata
3633
- // still applies, so changed=true even though content did not change.
3634
- // This preserves the pre-T4 no-infinite-retry choice: the caller
3635
- // (dag.ts rebuildDirtySummaries) treats changed=false as "race lost,
3636
- // silently retry next cycle" — returning false on a refusal would
3637
- // retry the same doomed LLM rebuild forever, so changed=true settles
3638
- // this cycle (dirty cleared) regardless of refusal.
3639
- // `refused` is the T4 addition: true only when a tombstone hit AND
3640
- // the metadata UPDATE landed (changed=true) — a refusal that loses
3641
- // the race to a concurrent writer reports refused=false too, since
3642
- // nothing from this call took effect. Before T4, a refusal also
3643
- // counted toward the caller's `rebuilt` stat because `changed` alone
3644
- // could not distinguish it; the caller now increments `refused`
3645
- // instead of `rebuilt` when this is true, so the stat reflects what
3646
- // happened without changing dirty-clearing or retry behavior.
3647
- const changed = (result.changes ?? 0) > 0;
3648
- const refused = Boolean(tombstone) && changed;
3649
- if (tombstone && changed) {
3650
- // refused === true here (same condition, narrowed for the tombstone.*
3651
- // access below). Best-effort refusal audit, written INLINE inside
3652
- // this still-open SAVEPOINT — nothing here rolls back on a refusal
3653
- // (the metadata UPDATE above already committed to this savepoint), so the
3654
- // post-rollback auditRejectionRefusal helper (writeEntry/supersede's
3655
- // tool) is the wrong one here; a direct audit() call is correct and
3656
- // commits with the rest of this savepoint.
3657
- audit(db, 'reject_refusal', summary.id, { digest: tombstone.digest, reason: tombstone.reason }, patch.actor, summary.tenantId);
3658
- console.error(`applyRebuildResult: refused rebuild content for ${summary.id} — matches a rejected value ` +
3659
- `(digest ${tombstone.digest.slice(0, 12)}...); metadata updated, content unchanged`);
3660
- }
3661
- if (changed) {
3662
- // FTS sync — bare UPDATE on memories does NOT update memories_fts.
3663
- // R1 HIGH must-fix from plan-eng-r1. Construct the patched entry in
3664
- // memory and reuse the existing syncFtsRow helper (delete-then-insert).
3665
- // earliest_at/latest_at preserve null semantics (R2 must-fix).
3666
- // AT1: content stays summary.content (unchanged) when the write was
3667
- // refused — applyContentWrite is false, so patch.content was never
3668
- // written to the row FTS must mirror.
3669
- const patchedEntry = {
3670
- ...summary,
3671
- content: applyContentWrite ? patch.content : summary.content,
3672
- descendant_count: patch.descendant_count,
3673
- earliest_at: patch.earliest_at,
3674
- latest_at: patch.latest_at,
3675
- summary_dirty: 0,
3676
- last_rebuilt_at: applyContentWrite ? nowIso : summary.last_rebuilt_at,
3677
- rebuild_count: applyContentWrite
3678
- ? (summary.rebuild_count ?? 0) + 1
3679
- : summary.rebuild_count,
3680
- };
3681
- syncFtsRow(db, patchedEntry);
3682
- audit(db, 'summary_rebuilt', summary.id, {
3683
- // v0.30 / E5: read actual level from the summary in scope
3684
- // (NOT hardcoded 2). L2 -> 2, L3 -> 3.
3685
- dag_level: summary.dag_level,
3686
- source: 'E3-rebuild',
3687
- zero_children: patch.zeroChildren,
3688
- descendant_count: patch.descendant_count,
3689
- }, patch.actor, summary.tenantId);
3690
- }
3691
- db.exec('RELEASE SAVEPOINT rebuild_summary');
3692
- return { changed, refused };
3693
- }
3694
- catch (e) {
3695
- try {
3696
- db.exec('ROLLBACK TO SAVEPOINT rebuild_summary');
3697
- db.exec('RELEASE SAVEPOINT rebuild_summary');
3698
- }
3699
- catch {
3700
- // Ignore rollback failures — throw below is what matters.
3701
- }
3702
- throw e;
3703
- }
3704
- }
3705
- finally {
3706
- closeHippoDb(db);
3707
- }
3708
- }
3709
- /**
3710
- * v0.30 / E3 — clear summary_dirty on a freshly-built summary. Called by
3711
- * buildDag immediately after the child-link loop finishes. Without this,
3712
- * each member's writeEntry call fires markSummaryDirtyInTx on the just-
3713
- * created parent (E2 hook at store.ts:1214), and the same sleep cycle's
3714
- * E3 rebuild phase would re-rebuild every new summary (2x LLM cost).
3715
- *
3716
- * Idempotent: no-op + no audit if summary isn't dirty. Audit
3717
- * source='buildDag-clean' distinguishes from E3-rebuild source.
3718
- */
3719
- export function clearSummaryDirtyAfterBuild(hippoRoot, summaryId, tenantId, actor = 'cli', source = 'buildDag-clean') {
3720
- assertTenantId('clearSummaryDirtyAfterBuild', tenantId);
3721
- initStore(hippoRoot);
3722
- const db = openHippoDb(hippoRoot);
3723
- try {
3724
- // v0.30 / E5: widened dag_level=2 -> IN (2, 3). RETURNING dag_level reads
3725
- // actual level so audit metadata stays accurate without an extra SELECT.
3726
- // SAFETY: result's shape matches the single `dag_level` column returned
3727
- // below.
3728
- const result = db.prepare(`
3729
- UPDATE memories
3730
- SET summary_dirty = 0
3731
- WHERE id = ?
3732
- AND tenant_id = ?
3733
- AND dag_level IN (2, 3)
3734
- AND summary_dirty = 1
3735
- AND kind != 'archived'
3736
- RETURNING dag_level
3737
- `).get(summaryId, tenantId);
3738
- if (result) {
3739
- // v0.30 / E5: source param distinguishes buildDag-clean (L2) from
3740
- // buildEntityProfiles-clean (L3) and any future build path.
3741
- audit(db, 'summary_marked_clean', summaryId, { dag_level: result.dag_level, source }, actor, tenantId);
3742
- }
3743
- }
3744
- finally {
3745
- closeHippoDb(db);
3746
- }
3747
- }
3748
- export { getHippoDbPath };
3749
- //# sourceMappingURL=store.js.map