@bastani/atomic 0.10.11-alpha.2 → 0.10.11-alpha.3

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 (432) hide show
  1. package/CHANGELOG.md +99 -0
  2. package/README.md +5 -8
  3. package/dist/_shared/dist-KOELCESV.js +2 -2
  4. package/dist/_shared/fork-row-index-UAUAQRD5.js +2 -2
  5. package/dist/_shared/lib-4PES5P4V.js +2 -2
  6. package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js +6 -6
  7. package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +5 -5
  8. package/dist/_shared/mode-shared-2QJSCUM6.js +6 -6
  9. package/dist/_shared/p0-cli-BB45ZQY6.js +46 -67
  10. package/dist/_shared/p0-cli-BB45ZQY6.js.map +1 -1
  11. package/dist/_shared/p1a-prewarm-XIU57JHF.js +7 -8
  12. package/dist/_shared/p1a-prewarm-XIU57JHF.js.map +1 -1
  13. package/dist/_shared/{p1b-elig-ENCUPKKJ.js → p1b-elig-TVB5CBLL.js} +14 -2
  14. package/dist/_shared/{p1b-elig-ENCUPKKJ.js.map → p1b-elig-TVB5CBLL.js.map} +1 -1
  15. package/dist/_shared/runtime-O4KCSG4C.js +3 -3
  16. package/dist/_shared/{sdk-IQMULKFU.js → sdk-B5LEWGGI.js} +340 -163
  17. package/dist/_shared/sdk-B5LEWGGI.js.map +1 -0
  18. package/dist/_shared/session-data-AHTU7IS6.js +2 -2
  19. package/dist/_shared/session-manager-list-2BLEEZVG.js +1 -1
  20. package/dist/_shared/{startup-0-0-3IXNMFM2.js → startup-0-0-EZUKEP4Q.js} +48 -40
  21. package/dist/_shared/startup-0-0-EZUKEP4Q.js.map +1 -0
  22. package/dist/_shared/{startup-0-1-7T2IAE7J.js → startup-0-1-RTOHJFDJ.js} +195 -42
  23. package/dist/_shared/startup-0-1-RTOHJFDJ.js.map +1 -0
  24. package/dist/_shared/startup-0-2-5QHPCEON.js +33 -43
  25. package/dist/_shared/startup-0-2-5QHPCEON.js.map +1 -1
  26. package/dist/_shared/startup-0-3-4ZYBYQNM.js +5 -5
  27. package/dist/_shared/startup-0-4-ZMJV547D.js +12 -7
  28. package/dist/_shared/startup-0-4-ZMJV547D.js.map +1 -1
  29. package/dist/_shared/startup-0-5-YDW47QX6.js +7 -7
  30. package/dist/_shared/startup-0-5-YDW47QX6.js.map +1 -1
  31. package/dist/_shared/startup-10-0-TDQNBDDL.js +6 -6
  32. package/dist/_shared/startup-12-0-55AAXBJZ.js +2 -2
  33. package/dist/_shared/startup-14-0-MLX36RBG.js +4 -5
  34. package/dist/_shared/startup-14-0-MLX36RBG.js.map +1 -1
  35. package/dist/_shared/startup-15-0-YUKGAC63.js +4 -4
  36. package/dist/_shared/startup-15-1-4S7FLMNG.js +19 -27
  37. package/dist/_shared/startup-15-1-4S7FLMNG.js.map +1 -1
  38. package/dist/_shared/startup-8-0-34CBHPZH.js +39 -23
  39. package/dist/_shared/startup-8-0-34CBHPZH.js.map +1 -1
  40. package/dist/_shared/{startup-8-1-X32CYSXB.js → startup-8-1-LZFXSD3Z.js} +90 -1176
  41. package/dist/_shared/startup-8-1-LZFXSD3Z.js.map +1 -0
  42. package/dist/_shared/storage-command-7MGXLJGZ.js +186 -35
  43. package/dist/_shared/storage-command-7MGXLJGZ.js.map +1 -1
  44. package/dist/_shared/storage-status-K7PX2RXA.js +557 -0
  45. package/dist/_shared/storage-status-K7PX2RXA.js.map +1 -0
  46. package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +2 -2
  47. package/dist/_shared/team-DOQKICG2.js +376 -0
  48. package/dist/_shared/team-DOQKICG2.js.map +1 -0
  49. package/dist/_shared/tree-outline-MUEPK5B7.js +2 -2
  50. package/dist/builtin/intercom/CHANGELOG.md +23 -0
  51. package/dist/builtin/intercom/README.md +2 -2
  52. package/dist/builtin/intercom/broker/broker.bundle.mjs +913 -289
  53. package/dist/builtin/intercom/broker/database-wake-up-bus.ts +243 -64
  54. package/dist/builtin/intercom/broker/multinode-router.ts +154 -22
  55. package/dist/builtin/intercom/broker/multinode-state-store.ts +30 -3
  56. package/dist/builtin/intercom/broker/open-broker-state.ts +14 -0
  57. package/dist/builtin/intercom/broker/sql-add-column.ts +29 -5
  58. package/dist/builtin/intercom/broker/sql-ended-child-queue-store.ts +3 -2
  59. package/dist/builtin/intercom/broker/sql-held-send-store.ts +2 -2
  60. package/dist/builtin/intercom/broker/sql-known-session-store.ts +43 -19
  61. package/dist/builtin/intercom/broker/sql-lease-store.ts +170 -74
  62. package/dist/builtin/intercom/broker/sql-outbox-store.ts +298 -47
  63. package/dist/builtin/intercom/broker/sql-pending-ask-store.ts +8 -5
  64. package/dist/builtin/intercom/broker/sql-released-ask-store.ts +12 -5
  65. package/dist/builtin/intercom/broker/sql-restoring-reservation-store.ts +24 -7
  66. package/dist/builtin/intercom/broker/sql-retention.ts +141 -0
  67. package/dist/builtin/intercom/broker/sql-schema.ts +136 -19
  68. package/dist/builtin/intercom/broker/sql-sent-message-authority.ts +74 -19
  69. package/dist/builtin/intercom/broker/wake-up-bus.ts +6 -0
  70. package/dist/builtin/intercom/index.bundle.mjs +2 -3
  71. package/dist/builtin/intercom/package.json +1 -1
  72. package/dist/builtin/mcp/index.bundle.mjs +1 -1
  73. package/dist/builtin/mcp/package.json +1 -1
  74. package/dist/builtin/subagents/package.json +1 -1
  75. package/dist/builtin/subagents/src/extension/index.bundle.mjs +1 -1
  76. package/dist/builtin/web-access/index.bundle.mjs +2 -10
  77. package/dist/builtin/web-access/package.json +1 -1
  78. package/dist/bun/bundled-sqlite.js +41 -0
  79. package/dist/bun/bundled-sqlite.js.map +1 -0
  80. package/dist/cli/args.js +2 -2
  81. package/dist/cli/auth-cli.code.js +4 -4
  82. package/dist/cli/auth-cli.impl.js +3 -3
  83. package/dist/cli/auth-cli.js +3 -3
  84. package/dist/cli/blueprint-command.code.js +3 -3
  85. package/dist/cli/blueprint-command.impl.js +3 -3
  86. package/dist/cli/blueprint-command.js +3 -3
  87. package/dist/cli/intercom-command.code.js +3 -3
  88. package/dist/cli/intercom-command.code.js.map +1 -1
  89. package/dist/cli/intercom-command.impl.js +3 -3
  90. package/dist/cli/intercom-command.js +3 -3
  91. package/dist/cli/list-models.code.js +2 -2
  92. package/dist/cli/list-models.impl.js +3 -3
  93. package/dist/cli/list-models.js +3 -3
  94. package/dist/cli/session-picker.code.js +1 -1
  95. package/dist/cli/session-picker.impl.js +3 -3
  96. package/dist/cli/session-picker.js +3 -3
  97. package/dist/cli/startup-ui.code.js +1 -1
  98. package/dist/cli/startup-ui.impl.js +3 -3
  99. package/dist/cli/startup-ui.js +3 -3
  100. package/dist/cli/storage-command.impl.js +3 -3
  101. package/dist/cli/storage-command.js +3 -3
  102. package/dist/cli/storage-search-session.code.js +3 -3
  103. package/dist/cli/storage-search-session.impl.js +3 -3
  104. package/dist/cli/storage-search-session.js +3 -3
  105. package/dist/cli/team-command.code.js +19 -113
  106. package/dist/cli/team-command.code.js.map +1 -1
  107. package/dist/cli/team-command.impl.js +3 -3
  108. package/dist/cli/team-command.js +3 -3
  109. package/dist/cli.js +5 -18
  110. package/dist/cli.js.map +1 -1
  111. package/dist/core/agent-rehydrate-errors.d.ts +4 -9
  112. package/dist/core/compaction/compactor.d.ts +3 -3
  113. package/dist/core/debug.code.js +1 -1
  114. package/dist/core/debug.impl.js +3 -3
  115. package/dist/core/debug.js +3 -3
  116. package/dist/core/export-html/card-projection.impl.js +3 -3
  117. package/dist/core/export-html/card-projection.js +3 -3
  118. package/dist/core/export-html/index.code.js +2 -2
  119. package/dist/core/export-html/index.impl.js +3 -3
  120. package/dist/core/export-html/index.js +3 -3
  121. package/dist/core/export-html/json-spans.impl.js +3 -3
  122. package/dist/core/export-html/json-spans.js +3 -3
  123. package/dist/core/export-html/oversized-render-inputs.impl.js +3 -3
  124. package/dist/core/export-html/oversized-render-inputs.js +3 -3
  125. package/dist/core/export-html/tool-renderer.impl.js +3 -3
  126. package/dist/core/export-html/tool-renderer.js +3 -3
  127. package/dist/core/extensions/jiti-loader.impl.js +3 -3
  128. package/dist/core/extensions/jiti-loader.js +3 -3
  129. package/dist/core/extensions/jiti-static-loader.impl.js +3 -3
  130. package/dist/core/extensions/jiti-static-loader.js +3 -3
  131. package/dist/core/extensions/loader-host-modules.code.js +1 -1
  132. package/dist/core/extensions/loader-host-modules.impl.js +3 -3
  133. package/dist/core/extensions/loader-host-modules.js +3 -3
  134. package/dist/core/extensions/loader-virtual-modules.js +1 -1
  135. package/dist/core/extensions/runner-context.js +2 -2
  136. package/dist/core/intercom-broker-control-host.code.js +2 -2
  137. package/dist/core/intercom-broker-control-host.impl.js +3 -3
  138. package/dist/core/intercom-broker-control-host.js +3 -3
  139. package/dist/core/intercom-broker-control.code.js +3 -3
  140. package/dist/core/intercom-broker-control.impl.js +3 -3
  141. package/dist/core/intercom-broker-control.js +3 -3
  142. package/dist/core/package-manager-storage-adapters.code.js +1 -1
  143. package/dist/core/package-manager-storage-adapters.impl.js +3 -3
  144. package/dist/core/package-manager-storage-adapters.js +3 -3
  145. package/dist/core/process-start-token.d.ts +8 -0
  146. package/dist/core/process-tree-containment.d.ts +16 -0
  147. package/dist/core/sdk-types.d.ts +1 -2
  148. package/dist/core/sdk-types.js.map +1 -1
  149. package/dist/core/sdk.code.js +3 -3
  150. package/dist/core/sdk.impl.js +3 -3
  151. package/dist/core/sdk.js +3 -3
  152. package/dist/core/session-manager-core.js +5 -5
  153. package/dist/core/session-manager-list.impl.js +3 -3
  154. package/dist/core/session-manager-list.js +3 -3
  155. package/dist/core/session-manager.code.js +1 -1
  156. package/dist/core/session-manager.impl.js +3 -3
  157. package/dist/core/session-manager.js +3 -3
  158. package/dist/core/session-storage.code.js +1 -1
  159. package/dist/core/session-storage.impl.js +3 -3
  160. package/dist/core/session-storage.js +3 -3
  161. package/dist/core/settings-schema.d.ts +0 -3
  162. package/dist/core/storage/added-exports-0-10-11.js +19 -0
  163. package/dist/core/storage/added-exports-0-10-11.js.map +1 -0
  164. package/dist/core/storage/creation-time.js +105 -0
  165. package/dist/core/storage/creation-time.js.map +1 -0
  166. package/dist/core/storage/database.d.ts +19 -5
  167. package/dist/core/storage/database.js.map +1 -1
  168. package/dist/core/storage/dialect.d.ts +1 -1
  169. package/dist/core/storage/dialect.js +1 -1
  170. package/dist/core/storage/dialect.js.map +1 -1
  171. package/dist/core/storage/errors.d.ts +13 -13
  172. package/dist/core/storage/errors.js +10 -16
  173. package/dist/core/storage/errors.js.map +1 -1
  174. package/dist/core/storage/fenced-paths.d.ts +11 -0
  175. package/dist/core/storage/fenced-paths.js +11 -0
  176. package/dist/core/storage/fenced-paths.js.map +1 -1
  177. package/dist/core/storage/file-identity.d.ts +2 -0
  178. package/dist/core/storage/file-identity.js +24 -8
  179. package/dist/core/storage/file-identity.js.map +1 -1
  180. package/dist/core/storage/index.d.ts +3 -2
  181. package/dist/core/storage/index.js +5 -3
  182. package/dist/core/storage/index.js.map +1 -1
  183. package/dist/core/storage/libsql-removed.d.ts +71 -0
  184. package/dist/core/storage/libsql-removed.js +123 -0
  185. package/dist/core/storage/libsql-removed.js.map +1 -0
  186. package/dist/core/storage/migrations.d.ts +78 -14
  187. package/dist/core/storage/migrations.js +375 -49
  188. package/dist/core/storage/migrations.js.map +1 -1
  189. package/dist/core/storage/node-sqlite-file-gate.js +0 -15
  190. package/dist/core/storage/node-sqlite-file-gate.js.map +1 -1
  191. package/dist/core/storage/node-sqlite-thread.js +8 -13
  192. package/dist/core/storage/node-sqlite-thread.js.map +1 -1
  193. package/dist/core/storage/node-sqlite.js +51 -3
  194. package/dist/core/storage/node-sqlite.js.map +1 -1
  195. package/dist/core/storage/offline-snapshot-file.impl.js +3 -3
  196. package/dist/core/storage/offline-snapshot-file.js +3 -3
  197. package/dist/core/storage/prefix.d.ts +1 -1
  198. package/dist/core/storage/prefix.js +9 -2
  199. package/dist/core/storage/prefix.js.map +1 -1
  200. package/dist/core/storage/process-selection.d.ts +5 -5
  201. package/dist/core/storage/provider.js +2 -2
  202. package/dist/core/storage/schema-catalog.d.ts +7 -8
  203. package/dist/core/storage/schema-catalog.js +8 -8
  204. package/dist/core/storage/schema-catalog.js.map +1 -1
  205. package/dist/core/storage/selection.d.ts +7 -82
  206. package/dist/core/storage/selection.js +20 -235
  207. package/dist/core/storage/selection.js.map +1 -1
  208. package/dist/core/storage/session/branch-fork-source.code.js +2 -2
  209. package/dist/core/storage/session/branch-fork-source.impl.js +3 -3
  210. package/dist/core/storage/session/branch-fork-source.js +3 -3
  211. package/dist/core/storage/session/card-view.impl.js +3 -3
  212. package/dist/core/storage/session/card-view.js +3 -3
  213. package/dist/core/storage/session/child-reader.code.js +3 -3
  214. package/dist/core/storage/session/child-reader.impl.js +3 -3
  215. package/dist/core/storage/session/child-reader.js +3 -3
  216. package/dist/core/storage/session/chunked-resume-projection.impl.js +3 -3
  217. package/dist/core/storage/session/chunked-resume-projection.js +3 -3
  218. package/dist/core/storage/session/expanded-child-details.code.js +2 -2
  219. package/dist/core/storage/session/expanded-child-details.impl.js +3 -3
  220. package/dist/core/storage/session/expanded-child-details.js +3 -3
  221. package/dist/core/storage/session/fork-row-bytes.impl.js +3 -3
  222. package/dist/core/storage/session/fork-row-bytes.js +3 -3
  223. package/dist/core/storage/session/fork-row-index.impl.js +3 -3
  224. package/dist/core/storage/session/fork-row-index.js +3 -3
  225. package/dist/core/storage/session/fork-staging.code.js +4 -7
  226. package/dist/core/storage/session/fork-staging.code.js.map +1 -1
  227. package/dist/core/storage/session/fork-staging.impl.js +3 -3
  228. package/dist/core/storage/session/fork-staging.js +3 -3
  229. package/dist/core/storage/session/fork-window.code.js +3 -3
  230. package/dist/core/storage/session/fork-window.impl.js +3 -3
  231. package/dist/core/storage/session/fork-window.js +3 -3
  232. package/dist/core/storage/session/history-pages.impl.js +3 -3
  233. package/dist/core/storage/session/history-pages.js +3 -3
  234. package/dist/core/storage/session/jsonl-entry-projection.impl.js +3 -3
  235. package/dist/core/storage/session/jsonl-entry-projection.js +3 -3
  236. package/dist/core/storage/session/jsonl-session-store.d.ts +5 -1
  237. package/dist/core/storage/session/jsonl-session-store.impl.js +3 -3
  238. package/dist/core/storage/session/jsonl-session-store.js +3 -3
  239. package/dist/core/storage/session/memory-session-store.impl.js +3 -3
  240. package/dist/core/storage/session/memory-session-store.js +3 -3
  241. package/dist/core/storage/session/pinned-branch-resume.code.js +3 -3
  242. package/dist/core/storage/session/pinned-branch-resume.impl.js +3 -3
  243. package/dist/core/storage/session/pinned-branch-resume.js +3 -3
  244. package/dist/core/storage/session/released-history-files.code.js +3 -3
  245. package/dist/core/storage/session/released-history-files.impl.js +3 -3
  246. package/dist/core/storage/session/released-history-files.js +3 -3
  247. package/dist/core/storage/session/resident-fork-rows.impl.js +3 -3
  248. package/dist/core/storage/session/resident-fork-rows.js +3 -3
  249. package/dist/core/storage/session/session-resume.impl.js +3 -3
  250. package/dist/core/storage/session/session-resume.js +3 -3
  251. package/dist/core/storage/session/session-search-index.d.ts +43 -4
  252. package/dist/core/storage/session/snapshot-cut.impl.js +3 -3
  253. package/dist/core/storage/session/snapshot-cut.js +3 -3
  254. package/dist/core/storage/session/snapshot-history.code.js +3 -3
  255. package/dist/core/storage/session/snapshot-history.impl.js +3 -3
  256. package/dist/core/storage/session/snapshot-history.js +3 -3
  257. package/dist/core/storage/session/sql-session-store.code.js +1 -1
  258. package/dist/core/storage/session/sql-session-store.d.ts +48 -11
  259. package/dist/core/storage/session/sql-session-store.impl.js +3 -3
  260. package/dist/core/storage/session/sql-session-store.js +3 -3
  261. package/dist/core/storage/session/tree-outline.impl.js +3 -3
  262. package/dist/core/storage/session/tree-outline.js +3 -3
  263. package/dist/core/storage/session/tree-presentation.impl.js +3 -3
  264. package/dist/core/storage/session/tree-presentation.js +3 -3
  265. package/dist/core/storage/sqlite-facade.d.ts +6 -1
  266. package/dist/core/storage/sqlite-facade.js +1 -0
  267. package/dist/core/storage/sqlite-facade.js.map +1 -1
  268. package/dist/core/storage/storage-status.code.js +1 -1
  269. package/dist/core/storage/storage-status.impl.js +3 -3
  270. package/dist/core/storage/storage-status.js +3 -3
  271. package/dist/core/storage/team.code.js +1 -1
  272. package/dist/core/storage/team.d.ts +26 -115
  273. package/dist/core/storage/team.impl.js +3 -3
  274. package/dist/core/storage/team.js +3 -3
  275. package/dist/core/structured-output/index.code.js +1 -1
  276. package/dist/core/structured-output/index.impl.js +3 -3
  277. package/dist/core/structured-output/index.js +3 -3
  278. package/dist/core/tasks/task-completion-entry.impl.js +3 -3
  279. package/dist/core/tasks/task-completion-entry.js +3 -3
  280. package/dist/core/tasks/task-file-history.code.js +3 -3
  281. package/dist/core/tasks/task-file-history.impl.js +3 -3
  282. package/dist/core/tasks/task-file-history.js +3 -3
  283. package/dist/core/tasks/task-reference-archive.code.js +4 -4
  284. package/dist/core/tasks/task-reference-archive.js +3 -3
  285. package/dist/core/tasks/task-reference-identity.js +3 -3
  286. package/dist/core/tasks/task-reference-stream.impl.js +3 -3
  287. package/dist/core/tasks/task-reference-stream.js +3 -3
  288. package/dist/core/tasks/task-unsaved-answer.impl.js +3 -3
  289. package/dist/core/tasks/task-unsaved-answer.js +3 -3
  290. package/dist/core/tasks/task-view-source.code.js +1 -1
  291. package/dist/core/tasks/task-view-source.impl.js +3 -3
  292. package/dist/core/tasks/task-view-source.js +3 -3
  293. package/dist/core/tools/history-tasks.d.ts +3 -2
  294. package/dist/core/tools/read-url.code.js +4 -4
  295. package/dist/core/tools/read-url.impl.js +3 -3
  296. package/dist/core/tools/read-url.js +3 -3
  297. package/dist/core/worktree-snapshot/blob-store.d.ts +40 -3
  298. package/dist/core/worktree-snapshot/blob-store.js +414 -21
  299. package/dist/core/worktree-snapshot/blob-store.js.map +1 -1
  300. package/dist/core/worktree-snapshot/errors.code.js +2 -2
  301. package/dist/core/worktree-snapshot/errors.impl.js +3 -3
  302. package/dist/core/worktree-snapshot/errors.js +3 -3
  303. package/dist/core/worktree-snapshot/fork-blob-guard.js +81 -0
  304. package/dist/core/worktree-snapshot/fork-blob-guard.js.map +1 -0
  305. package/dist/core/worktree-snapshot/fork-snapshot-index.js +79 -38
  306. package/dist/core/worktree-snapshot/fork-snapshot-index.js.map +1 -1
  307. package/dist/core/worktree-snapshot/session-index-reset.code.js +4 -0
  308. package/dist/core/worktree-snapshot/session-index-reset.code.js.map +1 -1
  309. package/dist/core/worktree-snapshot/session-index-reset.impl.js +3 -3
  310. package/dist/core/worktree-snapshot/session-index-reset.js +3 -3
  311. package/dist/extensions/codemode/execute.code.js +1 -1
  312. package/dist/extensions/codemode/execute.impl.js +3 -3
  313. package/dist/extensions/codemode/execute.js +3 -3
  314. package/dist/extensions/codemode/renderer.code.js +2 -2
  315. package/dist/extensions/codemode/renderer.impl.js +3 -3
  316. package/dist/extensions/codemode/renderer.js +3 -3
  317. package/dist/extensions/mcp/cli.code.js +3 -3
  318. package/dist/extensions/mcp/cli.impl.js +3 -3
  319. package/dist/extensions/mcp/cli.js +3 -3
  320. package/dist/extensions/mcp/cli.lazy.code.js +2 -2
  321. package/dist/extensions/mcp/cli.lazy.impl.js +3 -3
  322. package/dist/extensions/mcp/cli.lazy.js +3 -3
  323. package/dist/extensions/mcp/runtime.code.js +1 -1
  324. package/dist/extensions/mcp/runtime.impl.js +3 -3
  325. package/dist/extensions/mcp/runtime.js +3 -3
  326. package/dist/index.d.ts +3 -1
  327. package/dist/index.js +5 -5
  328. package/dist/main-first-time-setup.code.js +1 -1
  329. package/dist/main-first-time-setup.impl.js +3 -3
  330. package/dist/main-first-time-setup.js +3 -3
  331. package/dist/main.js +1 -1
  332. package/dist/modes/interactive/components/stored-tree-view.code.js +2 -2
  333. package/dist/modes/interactive/components/stored-tree-view.impl.js +3 -3
  334. package/dist/modes/interactive/components/stored-tree-view.js +3 -3
  335. package/dist/modes/interactive/components/user-message-selector-archive.impl.js +3 -3
  336. package/dist/modes/interactive/components/user-message-selector-archive.js +3 -3
  337. package/dist/modes/interactive/startup-shell-eligibility.js +1 -1
  338. package/dist/modes/interactive-engine/engine-prewarm.d.ts +1 -1
  339. package/dist/modes/print-mode.js +1 -1
  340. package/dist/package-manager-cli.code.js +6 -6
  341. package/dist/package-manager-cli.impl.js +3 -3
  342. package/dist/package-manager-cli.js +3 -3
  343. package/dist/rpc-entry.js +1 -1
  344. package/dist/startup-files.json +1 -1
  345. package/dist/startup-stages/10.js +1 -1
  346. package/dist/startup-stages/11.js +1 -1
  347. package/dist/startup-stages/12.js +1 -1
  348. package/dist/startup-stages/2.js +2 -4
  349. package/dist/startup-stages/3.js +1 -3
  350. package/dist/startup-stages/4.js +1 -2
  351. package/dist/startup-stages/5.js +1 -1
  352. package/dist/startup-stages/6.js +5 -7
  353. package/dist/startup-stages/7.js +3 -10
  354. package/dist/startup-stages/8.js +7 -2
  355. package/dist/startup-stages/9.js +1 -7
  356. package/dist/startup-stages/stages.json +1 -1
  357. package/dist/utils/compile-cache-deferral.js +8 -13
  358. package/dist/utils/compile-cache-deferral.js.map +1 -1
  359. package/dist/utils/compile-cache-warmup-host.js +3 -4
  360. package/dist/utils/compile-cache-warmup-host.js.map +1 -1
  361. package/dist/utils/compile-cache.js +12 -100
  362. package/dist/utils/compile-cache.js.map +1 -1
  363. package/dist/utils/startup-database-spare.code.js +2 -2
  364. package/dist/utils/startup-database-spare.impl.js +3 -3
  365. package/dist/utils/startup-database-spare.js +3 -3
  366. package/dist/utils/tools-manager.code.js +1 -1
  367. package/dist/utils/tools-manager.impl.js +3 -3
  368. package/dist/utils/tools-manager.js +3 -3
  369. package/dist/utils/worker-threads.js +2 -15
  370. package/dist/utils/worker-threads.js.map +1 -1
  371. package/docs/build.md +3 -3
  372. package/docs/changelog.mdx +1421 -69
  373. package/docs/compaction/reference.md +5 -5
  374. package/docs/compaction.md +7 -9
  375. package/docs/containerization.md +2 -2
  376. package/docs/custom-provider/api-reference.md +1 -1
  377. package/docs/docs.json +37 -10
  378. package/docs/environment-variables.md +0 -2
  379. package/docs/extensions/api-reference.md +4 -4
  380. package/docs/getting-started/installation.md +3 -0
  381. package/docs/guides/configuration.md +1 -1
  382. package/docs/guides/non-interactive.md +1 -1
  383. package/docs/guides/resume-on-any-machine.md +14 -22
  384. package/docs/guides/team-setup.md +56 -83
  385. package/docs/guides.md +2 -4
  386. package/docs/herdr.md +1 -1
  387. package/docs/index.md +1 -1
  388. package/docs/intercom/operations.md +26 -21
  389. package/docs/intercom/reference.md +2 -2
  390. package/docs/intercom.md +1 -1
  391. package/docs/libraries/storage-postgres.md +9 -9
  392. package/docs/libraries.md +2 -3
  393. package/docs/models-configuration.md +2 -2
  394. package/docs/models.md +2 -3
  395. package/docs/packages/authoring.md +7 -7
  396. package/docs/packages.md +1 -1
  397. package/docs/programmatic.md +2 -2
  398. package/docs/quickstart.md +1 -1
  399. package/docs/reference/cli.md +6 -11
  400. package/docs/reference.md +2 -2
  401. package/docs/rpc/extension-ui.md +2 -2
  402. package/docs/sdk/reference.md +5 -4
  403. package/docs/sdk/storage.md +8 -92
  404. package/docs/sdk/structured-decisions.md +1 -1
  405. package/docs/sdk/web-server.md +3 -3
  406. package/docs/sdk.md +14 -4
  407. package/docs/security.md +1 -1
  408. package/docs/session-format.md +5 -5
  409. package/docs/sessions.md +29 -49
  410. package/docs/settings.md +7 -8
  411. package/docs/storage/custom-adapters.md +255 -0
  412. package/docs/storage/jsonl.md +117 -0
  413. package/docs/{guides/postgres-pgbouncer.md → storage/postgres/pgbouncer.md} +66 -68
  414. package/docs/storage/postgres/tuning.md +337 -0
  415. package/docs/storage/postgres.md +556 -0
  416. package/docs/storage/sqlite.md +99 -0
  417. package/docs/storage.md +326 -990
  418. package/docs/subagents/authoring.md +1 -1
  419. package/docs/subagents.md +2 -2
  420. package/docs/tools.md +1 -1
  421. package/docs/worktree-snapshots.md +2 -2
  422. package/package.json +9 -9
  423. package/schemas/models.schema.json +2 -2
  424. package/schemas/settings.schema.json +0 -14
  425. package/dist/_shared/sdk-IQMULKFU.js.map +0 -1
  426. package/dist/_shared/startup-0-0-3IXNMFM2.js.map +0 -1
  427. package/dist/_shared/startup-0-1-7T2IAE7J.js.map +0 -1
  428. package/dist/_shared/startup-8-1-X32CYSXB.js.map +0 -1
  429. package/dist/core/intercom-broker-control.d.ts +0 -129
  430. package/dist/startup-stages/13.js +0 -1
  431. package/docs/guides/postgres-tuning.md +0 -311
  432. package/docs/libraries/storage-libsql.md +0 -97
@@ -1,10 +1,17 @@
1
+ import { createHash } from "node:crypto";
2
+ import * as storage from "@bastani/atomic/storage";
1
3
  import {
2
4
  dialectOf,
5
+ ensureStorageSchema,
3
6
  fencedPath,
4
7
  type SqliteDatabase,
5
8
  type SqliteExecutor,
6
9
  type SqliteRow,
10
+ type SqliteStatement,
11
+ type StorageNotificationEvent,
12
+ type StorageNotifications,
7
13
  type StorageNotificationSubscription,
14
+ storageMigrationTable,
8
15
  validateStoragePrefix,
9
16
  } from "@bastani/atomic/storage";
10
17
  import { OUTBOX_BACKUP_POLL_INTERVAL_MS } from "./multinode-state-store.js";
@@ -17,7 +24,8 @@ import type {
17
24
  WakeUpSubscribeResult,
18
25
  WakeUpSubscription,
19
26
  } from "./wake-up-bus.js";
20
- import { ensureBrokerSchema, withBrokerSchemaCatalog } from "./sql-schema.js";
27
+ import { addMissingColumns } from "./sql-add-column.js";
28
+ import { ensureBrokerSchema, openCatalogOf, RETRY_UNAPPLIED, withBrokerSchemaCatalog } from "./sql-schema.js";
21
29
 
22
30
  /**
23
31
  * S4 wake-up bus (specs/2026-10-19-intercom-multi-node.md §5.6): a hint log in the shared storage database. Each
@@ -26,12 +34,15 @@ import { ensureBrokerSchema, withBrokerSchemaCatalog } from "./sql-schema.js";
26
34
  * hints written meanwhile may have been pruned or missed. While the database stays unavailable, reopening backs off
27
35
  * exponentially from the poll interval up to `WAKE_UP_RECONNECT_MAX_DELAY_MS`.
28
36
  *
29
- * On a database that delivers notifications (Postgres), a publish also notifies the channel named like the table, in
30
- * the transaction that writes the hint, and a subscribed connection polls as soon as a notification for one of its
31
- * brokers arrives (#5103). The interval poll stays: notifications are lost while the listening connection is down, and
32
- * none arrive through a transaction pooler. While the database reports its listening connection live, the poll only
33
- * backs the notifications up and runs at `WAKE_UP_LISTENING_POLL_INTERVAL_MS`; the listener's `listening` event, sent
34
- * when it listens again after an outage, polls at once (#6560).
37
+ * On a database that delivers notifications (Postgres), a publish also notifies the hint's broker once the hint
38
+ * commits, and a subscribed connection polls as soon as a notification for one of its brokers arrives (#5103). Each
39
+ * broker has a channel of its own (`brokerWakeUpChannel`), so a notification reaches only the connection that hosts it;
40
+ * brokers of earlier releases listen on the channel named like the table instead, which is notified too while the
41
+ * broker's membership does not say it listens on its own (`wakeUpNotices`). A connection listens on both. The interval
42
+ * poll stays: notifications are lost while the listening connection is down, and none arrive through a transaction
43
+ * pooler. While the database reports its listening connection live, the poll only backs the notifications up and runs
44
+ * at `WAKE_UP_LISTENING_POLL_INTERVAL_MS`; the listener's `listening` event, sent when it listens again after an
45
+ * outage, polls at once (#6560).
35
46
  */
36
47
 
37
48
  /** How often a connection with subscriptions reads its hints when no notification would announce them. */
@@ -41,21 +52,62 @@ export const WAKE_UP_POLL_INTERVAL_MS = 500;
41
52
  * in an outage the listener has not noticed yet waits for this poll.
42
53
  */
43
54
  export const WAKE_UP_LISTENING_POLL_INTERVAL_MS = 5_000;
44
- /** Hints nobody consumed (their broker exited) are deleted after this age; the backup poll has covered them. */
55
+ /**
56
+ * The longest a hint nobody consumed (its broker exited) stays in the table, while brokers publish: by then the backup
57
+ * poll has covered it. The prune runs at most every {@link WAKE_UP_PRUNE_INTERVAL_MS}, so it deletes the hints older
58
+ * than the retention less that interval: one that just missed a prune is gone by the next, at most the retention old.
59
+ */
45
60
  export const WAKE_UP_HINT_RETENTION_MS = OUTBOX_BACKUP_POLL_INTERVAL_MS;
46
61
  /**
47
- * How often at most one connection deletes the hints past the retention, at a publish (#6560): a hint nobody consumed
48
- * then stays at most this much longer than the retention.
62
+ * How often at most one connection deletes old hints, at a publish (#6560): each prune is a write every broker would
63
+ * otherwise send at every publish.
49
64
  */
50
65
  export const WAKE_UP_PRUNE_INTERVAL_MS = 10_000;
66
+ /** The age past which a prune deletes a hint, so that no hint outlives {@link WAKE_UP_HINT_RETENTION_MS}. */
67
+ const WAKE_UP_PRUNE_AGE_MS = WAKE_UP_HINT_RETENTION_MS - WAKE_UP_PRUNE_INTERVAL_MS;
51
68
  /**
52
- * Hints read and deleted per statement; a poll repeats until a batch comes back short. Each batch's delete binds one
53
- * parameter per hint, so a backlog never exceeds SQLite's bound-parameter limit (999 on old builds).
69
+ * Hints read and deleted per statement; a poll repeats until a batch comes back short, so a backlog is taken in
70
+ * statements of bounded size.
54
71
  */
55
72
  export const WAKE_UP_POLL_BATCH_SIZE = 500;
56
73
  /** Longest wait between attempts to reopen an unavailable database; the backup poll covers the gap. */
57
74
  export const WAKE_UP_RECONNECT_MAX_DELAY_MS = OUTBOX_BACKUP_POLL_INTERVAL_MS;
58
75
 
76
+ /**
77
+ * What a broker's membership subject holds, as the lease table stores it, when the broker listens on its own wake-up
78
+ * channel (`BrokerMembershipSubject.ownWakeUpChannel`). A JSON string escapes its quotes, so only that field matches.
79
+ */
80
+ export const OWN_WAKE_UP_CHANNEL_MARK = '"ownWakeUpChannel":true';
81
+
82
+ /**
83
+ * The channel only `brokerId`'s connections listen on: the hint table's name and 64 bits of a hash of the broker id, so
84
+ * it stays a short identifier whatever the id, as Que's `que_listener_<pid>`. A notification on it reaches that broker's
85
+ * connection alone, where one on the table's channel reaches every broker's.
86
+ */
87
+ export function brokerWakeUpChannel(table: string, brokerId: string): string {
88
+ return `${table}_${createHash("sha256").update(brokerId).digest("hex").slice(0, 16)}`;
89
+ }
90
+
91
+ /**
92
+ * The notifications that wake `brokerId` once the transaction that runs them commits: one on its own channel, and one on
93
+ * the table's channel, where brokers of earlier releases listen, unless `brokerId`'s membership lease, in `leases`, says
94
+ * it listens on its own. Both carry the broker id, which a listener checks.
95
+ */
96
+ export function wakeUpNotices(
97
+ notifications: StorageNotifications,
98
+ table: string,
99
+ leases: string,
100
+ brokerId: string,
101
+ ): SqliteStatement[] {
102
+ return [
103
+ notifications.notify(brokerWakeUpChannel(table, brokerId), brokerId),
104
+ notifications.notify(table, brokerId, {
105
+ sql: `SELECT 1 FROM ${leases} WHERE kind = 'broker' AND key = ? AND strpos(subject_json, ?) > 0`,
106
+ params: [brokerId, OWN_WAKE_UP_CHANNEL_MARK],
107
+ }),
108
+ ];
109
+ }
110
+
59
111
  /** Runs `callback` once after `delayMs` and returns a function that cancels it. */
60
112
  export type WakeUpPollScheduler = (callback: () => void, delayMs: number) => () => void;
61
113
 
@@ -69,9 +121,9 @@ export interface DatabaseWakeUpBusOptions {
69
121
  readonly schedule?: WakeUpPollScheduler;
70
122
  /**
71
123
  * A separate handle on the same database that the poll reads its hints through, so a poll never queues behind the
72
- * database's writes (#6560); the bus does not close it. On PostgreSQL the poll takes and deletes its hints through
73
- * it in one statement (`DELETE … RETURNING`), so it must accept writes. Absent: the poll runs on the database it
74
- * opened.
124
+ * database's writes (#6560); the bus does not close it, and writes nothing through it. On PostgreSQL the poll reads
125
+ * nothing through it: it takes and deletes its hints in one statement (`DELETE … RETURNING`) on the database it
126
+ * opened. Absent: the poll runs on the database it opened.
75
127
  */
76
128
  readonly reader?: SqliteExecutor;
77
129
  }
@@ -81,6 +133,15 @@ interface HintRow extends SqliteRow {
81
133
  readonly broker_id: string;
82
134
  readonly reason: string;
83
135
  readonly version: number;
136
+ readonly target_identity: string | null;
137
+ }
138
+
139
+ /** The channels a connection listens on, each with its subscription, and the database it listens through. */
140
+ interface Listening {
141
+ readonly database: SqliteDatabase;
142
+ readonly channels: Map<string, StorageNotificationSubscription>;
143
+ /** The database reported its listening connection live, as of the latest check. */
144
+ live: boolean;
84
145
  }
85
146
 
86
147
  interface DatabaseSubscription {
@@ -95,19 +156,94 @@ const scheduleTimeout: WakeUpPollScheduler = (callback, delayMs) => {
95
156
  return () => clearTimeout(timer);
96
157
  };
97
158
 
159
+ /**
160
+ * `storageSchemaCurrent` of the storage API, which an Atomic installation older than #6560 lacks: a named import would
161
+ * fail to link there, so it is taken from the module namespace, and without it the hint log's persistence is read from
162
+ * the catalog at every open.
163
+ */
164
+ const schemaApi: Partial<Pick<typeof storage, "storageSchemaCurrent" | "ensureStorageSchemaOnce">> = storage;
165
+
166
+ /** The ledger step, `schema:intercom-wakeups-unlogged`, that makes a hint log created as a logged table unlogged. */
167
+ const UNLOGGED_HINTS_DOMAIN = "intercom-wakeups-unlogged";
168
+
98
169
  async function createSchema(database: SqliteDatabase, table: string, prefix: string | undefined): Promise<void> {
99
- const { types } = dialectOf(database);
100
- // In a serial transaction, so brokers opening a new bus at once take turns creating it (#5054).
101
- const schema = `CREATE TABLE IF NOT EXISTS ${table} (
170
+ const { types, name } = dialectOf(database);
171
+ // On PostgreSQL the hint log is unlogged: its rows skip the write-ahead log, so a hint costs no log write and its
172
+ // commit no flush to disk. A crash of the server empties it, which loses only hints, and the backup poll finds
173
+ // every message they announced. In a serial transaction, so brokers opening a new bus at once take turns creating
174
+ // it (#5054).
175
+ const schema = `CREATE ${name === "postgres" ? "UNLOGGED " : ""}TABLE IF NOT EXISTS ${table} (
102
176
  id ${types.serialKey},
103
177
  broker_id TEXT NOT NULL,
104
178
  reason TEXT NOT NULL CHECK (reason IN ('outbox', 'outcome')),
105
179
  version ${types.int} NOT NULL,
106
- created_at ${types.int} NOT NULL
180
+ created_at ${types.int} NOT NULL,
181
+ target_identity TEXT
107
182
  );
108
183
  CREATE INDEX IF NOT EXISTS ${table}_broker ON ${table} (broker_id, id);
109
184
  CREATE INDEX IF NOT EXISTS ${table}_created ON ${table} (created_at);`;
185
+ // `target_identity`, the recipient a hint is about (`WakeUpHint.targetIdentity`), came later: a table an earlier
186
+ // release created gets it here, and an earlier broker's hints leave it null. A table this open creates has it, so a
187
+ // new database's first open spends nothing on it.
188
+ const catalog = await openCatalogOf(database);
189
+ const existed = catalog === undefined || catalog.hasTable(table);
110
190
  await ensureBrokerSchema(database, prefix, "wakeups", (tx) => tx.exec(schema));
191
+ if (existed) await addMissingColumns(database, table, { target_identity: "TEXT" });
192
+ await ensureUnloggedHints(database, table, prefix);
193
+ }
194
+
195
+ /**
196
+ * Makes a hint log that an earlier Atomic created as a logged table unlogged on PostgreSQL, once per database, recorded
197
+ * in the ledger as `schema:intercom-wakeups-unlogged`. `ALTER TABLE … SET UNLOGGED` needs the table's owner, so a
198
+ * runtime role without DDL leaves it logged, as before, until `atomic storage migrate` runs as the owner. Earlier
199
+ * Atomic releases use the unlogged table unchanged. The rewrite locks the table, which holds only hints younger than
200
+ * the retention: when another broker holds it longer than the schema lock wait, the next open tries again. A table
201
+ * that is unlogged already, as every table this build creates, needs only the step recorded: the statement that reads
202
+ * the table's persistence records it too, so a new database's open pays one round trip for it and no transaction
203
+ * (#6560).
204
+ */
205
+ async function ensureUnloggedHints(database: SqliteDatabase, table: string, prefix: string | undefined): Promise<void> {
206
+ const dialect = dialectOf(database);
207
+ if (dialect.name !== "postgres" || database.readOnly === true) return;
208
+ const schema = { ledger: storageMigrationTable(prefix), domain: UNLOGGED_HINTS_DOMAIN, revision: 1 };
209
+ if (await schemaApi.storageSchemaCurrent?.(database, schema)) return;
210
+ const yes = (value: boolean | number | undefined) => value === true || Number(value) === 1;
211
+ let found: (SqliteRow & { owned: boolean | number; unlogged: boolean | number }) | undefined;
212
+ try {
213
+ // The ledger exists: the hint log's own setup recorded itself there. Recording a step that holds, whoever else
214
+ // records it at once, needs no lock.
215
+ found = await database.get(
216
+ `WITH found AS (
217
+ SELECT pg_catalog.pg_has_role(current_user, c.relowner, 'USAGE') AS owned, c.relpersistence = 'u' AS unlogged
218
+ FROM pg_catalog.pg_class c WHERE c.relnamespace = current_schema()::regnamespace AND c.relname = ?
219
+ ), recorded AS (
220
+ INSERT INTO ${schema.ledger} (domain, version, applied_at)
221
+ SELECT ?, ?, ${dialect.nowMs} FROM found WHERE found.unlogged ON CONFLICT DO NOTHING RETURNING 1
222
+ )
223
+ SELECT owned, unlogged FROM found`,
224
+ dialect.identifier(table),
225
+ `schema:${schema.domain}`,
226
+ schema.revision,
227
+ );
228
+ } catch {
229
+ // A role that may not write the ledger leaves the step to `atomic storage migrate`.
230
+ return;
231
+ }
232
+ if (found === undefined || yes(found.unlogged) || !yes(found.owned)) return;
233
+ const setup = async (tx: SqliteExecutor) => {
234
+ await tx.exec(`ALTER TABLE ${table} SET UNLOGGED`);
235
+ };
236
+ try {
237
+ // Brokers that open the database at once make it unlogged once: the others wait for it without its lock (#6560).
238
+ const ensureOnce = schemaApi.ensureStorageSchemaOnce;
239
+ if (ensureOnce !== undefined) await ensureOnce(database, schema, setup);
240
+ else
241
+ await database.transaction((tx) => ensureStorageSchema(tx, dialect, schema, () => setup(tx)), {
242
+ concurrency: { kind: "serial", domain: schema.ledger },
243
+ });
244
+ } catch {
245
+ // The table stays as it was: hints work the same in a logged table.
246
+ }
111
247
  }
112
248
 
113
249
  /**
@@ -137,6 +273,8 @@ class DatabaseWakeUpBus implements WakeUpBus {
137
273
  private readonly subscriptions = new Set<DatabaseSubscription>();
138
274
  private readonly options: DatabaseWakeUpBusOptions;
139
275
  private readonly table: string;
276
+ /** The lease table, whose membership leases say which brokers listen on their own channels. */
277
+ private readonly leases: string;
140
278
  private readonly pollIntervalMs: number;
141
279
  private readonly listeningPollIntervalMs: number;
142
280
  private readonly schedule: WakeUpPollScheduler;
@@ -149,15 +287,11 @@ class DatabaseWakeUpBus implements WakeUpBus {
149
287
  /** Consecutive failed attempts to reopen the database; each doubles the wait before the next. */
150
288
  private reconnectFailures = 0;
151
289
  private reconnectNotBefore = 0;
152
- /** The notification channel this connection listens on, and the database it listens through. */
153
- private listening:
154
- | {
155
- readonly database: SqliteDatabase;
156
- readonly subscription: StorageNotificationSubscription;
157
- /** The database reported its listening connection live, as of the latest check. */
158
- live: boolean;
159
- }
160
- | undefined;
290
+ /**
291
+ * The notification channels this connection listens on, each with its subscription: the table's and each subscribed
292
+ * broker's own (`brokerWakeUpChannel`). And the database it listens through.
293
+ */
294
+ private listening: Listening | undefined;
161
295
  /** A notification arrived during a poll: the next poll runs as soon as that one ends. */
162
296
  private pollAgain = false;
163
297
  /** When a publish last deleted the hints past the retention, by this process's clock. */
@@ -166,6 +300,7 @@ class DatabaseWakeUpBus implements WakeUpBus {
166
300
  constructor(options: DatabaseWakeUpBusOptions, table: string, database: SqliteDatabase) {
167
301
  this.options = options;
168
302
  this.table = table;
303
+ this.leases = `${validateStoragePrefix(options.prefix)}intercom_leases`;
169
304
  this.database = database;
170
305
  this.pollIntervalMs = options.pollIntervalMs ?? WAKE_UP_POLL_INTERVAL_MS;
171
306
  this.listeningPollIntervalMs = options.listeningPollIntervalMs ?? WAKE_UP_LISTENING_POLL_INTERVAL_MS;
@@ -177,29 +312,37 @@ class DatabaseWakeUpBus implements WakeUpBus {
177
312
  const database = await this.connection(false);
178
313
  if (database === undefined) return { status: "unavailable" };
179
314
  const { nowMs } = dialectOf(database);
180
- const insert = `INSERT INTO ${this.table} (broker_id, reason, version, created_at) VALUES (?, ?, ?, ${nowMs})`;
315
+ const insert: SqliteStatement = {
316
+ sql: `INSERT INTO ${this.table} (broker_id, reason, version, created_at, target_identity) VALUES (?, ?, ?, ${nowMs}, ?)`,
317
+ params: [hint.brokerId, hint.reason, hint.version, hint.targetIdentity ?? null],
318
+ };
181
319
  const notifications = database.notifications;
182
320
  try {
183
- if (notifications === undefined) await database.run(insert, hint.brokerId, hint.reason, hint.version);
321
+ if (notifications === undefined) await database.run(insert.sql, ...insert.params);
184
322
  else {
185
- // The notification goes out when the hint commits, so the poll it starts finds the hint. A hint is a new
186
- // row that no write checks first, so its writers take no lock domain (#6560): see
323
+ // The notifications go out once the hint commits, so the poll they start finds the hint: on Postgres right
324
+ // behind its commit, in the same round trip, and the insert goes with the transaction's start (#6560). A hint
325
+ // is a new row that no write checks first, so its writers take no lock domain: see
187
326
  // `fencedPath("intercom-wakeup-hint")`.
188
- const notify = notifications.notify(this.table, hint.brokerId);
327
+ const notices = wakeUpNotices(notifications, this.table, this.leases, hint.brokerId);
189
328
  await database.transaction(
190
329
  async (tx) => {
191
- await tx.run(insert, hint.brokerId, hint.reason, hint.version);
192
- await tx.run(notify.sql, ...notify.params);
330
+ await Promise.all([insert, ...notices].map(({ sql, params }) => tx.run(sql, ...params)));
331
+ },
332
+ {
333
+ ...RETRY_UNAPPLIED,
334
+ concurrency: { kind: "fenced", audit: fencedPath("intercom-wakeup-hint") },
335
+ leading: [insert],
193
336
  },
194
- { concurrency: { kind: "fenced", audit: fencedPath("intercom-wakeup-hint") } },
195
337
  );
196
338
  }
197
339
  // #6560: at most once per interval, not at every publish: each prune is a write every broker would send.
198
340
  if (this.prunedAt === undefined || Date.now() - this.prunedAt >= WAKE_UP_PRUNE_INTERVAL_MS) {
199
341
  this.prunedAt = Date.now();
342
+ // The clock is read once, in a subquery: PostgreSQL's is volatile, and a volatile bound uses no index.
200
343
  await database.run(
201
- `DELETE FROM ${this.table} WHERE created_at < ${nowMs} - ?`,
202
- WAKE_UP_HINT_RETENTION_MS,
344
+ `DELETE FROM ${this.table} WHERE created_at < (SELECT ${nowMs}) - ?`,
345
+ WAKE_UP_PRUNE_AGE_MS,
203
346
  );
204
347
  }
205
348
  } catch {
@@ -222,6 +365,7 @@ class DatabaseWakeUpBus implements WakeUpBus {
222
365
  close: async () => {
223
366
  subscription.open = false;
224
367
  this.subscriptions.delete(subscription);
368
+ this.listen();
225
369
  },
226
370
  };
227
371
  return { status: "subscribed", subscription: handle };
@@ -236,8 +380,7 @@ class DatabaseWakeUpBus implements WakeUpBus {
236
380
  this.closed = true;
237
381
  this.cancelPoll?.();
238
382
  this.cancelPoll = undefined;
239
- this.listening?.subscription.close();
240
- this.listening = undefined;
383
+ this.stopListening();
241
384
  for (const subscription of this.subscriptions) subscription.open = false;
242
385
  this.subscriptions.clear();
243
386
  await this.polling;
@@ -253,22 +396,45 @@ class DatabaseWakeUpBus implements WakeUpBus {
253
396
  });
254
397
  }
255
398
 
256
- /** Listens for notifications through the current database, when it delivers them. */
399
+ /**
400
+ * Listens for notifications through the current database, when it delivers them: on each subscribed broker's own
401
+ * channel, and on the table's, where brokers of earlier releases send them. A channel no subscription needs any more
402
+ * is left.
403
+ */
257
404
  private listen(): void {
258
405
  const database = this.database;
259
- if (this.closed || database === undefined || this.listening?.database === database) return;
260
- this.listening?.subscription.close();
261
- this.listening = undefined;
406
+ if (this.closed || database === undefined) return;
407
+ if (this.listening !== undefined && this.listening.database !== database) this.stopListening();
262
408
  const notifications = database.notifications;
263
409
  if (notifications === undefined) return;
264
- const subscription = notifications.listen(this.table, (event) => {
265
- if (event.kind === "listening") {
266
- if (this.listening?.subscription === subscription) this.listening.live = true;
267
- this.pollNow();
268
- } else if ([...this.subscriptions].some((open) => open.brokerId === event.payload)) this.pollNow();
269
- });
270
- this.listening = { database, subscription, live: false };
271
- void this.checkListening();
410
+ const fresh = this.listening === undefined;
411
+ const listening = (this.listening ??= { database, channels: new Map(), live: false });
412
+ const wanted = new Set([
413
+ this.table,
414
+ ...[...this.subscriptions].map((subscription) => brokerWakeUpChannel(this.table, subscription.brokerId)),
415
+ ]);
416
+ for (const [channel, subscription] of listening.channels) {
417
+ if (wanted.has(channel)) continue;
418
+ subscription.close();
419
+ listening.channels.delete(channel);
420
+ }
421
+ for (const channel of wanted) {
422
+ if (!listening.channels.has(channel))
423
+ listening.channels.set(channel, notifications.listen(channel, (event) => this.notified(listening, event)));
424
+ }
425
+ if (fresh) void this.checkListening();
426
+ }
427
+
428
+ private notified(listening: Listening, event: StorageNotificationEvent): void {
429
+ if (event.kind === "listening") {
430
+ if (this.listening === listening) listening.live = true;
431
+ this.pollNow();
432
+ } else if ([...this.subscriptions].some((open) => open.brokerId === event.payload)) this.pollNow();
433
+ }
434
+
435
+ private stopListening(): void {
436
+ for (const subscription of this.listening?.channels.values() ?? []) subscription.close();
437
+ this.listening = undefined;
272
438
  }
273
439
 
274
440
  /** Asks the database whether its listening connection is live, for the interval of the next poll. */
@@ -321,26 +487,38 @@ class DatabaseWakeUpBus implements WakeUpBus {
321
487
  const database = await this.connection(true);
322
488
  const brokerIds = [...new Set([...this.subscriptions].map((subscription) => subscription.brokerId))];
323
489
  if (database === undefined || brokerIds.length === 0) return;
324
- const placeholders = brokerIds.map(() => "?").join(", ");
325
- const batch = `FROM ${this.table} WHERE broker_id IN (${placeholders}) ORDER BY id LIMIT ${WAKE_UP_POLL_BATCH_SIZE}`;
490
+ const dialect = dialectOf(database);
491
+ // One statement text whatever the number of brokers, so the server plans it once: their ids go as one JSON array.
492
+ const batch = `FROM ${this.table} WHERE broker_id IN (SELECT value FROM ${dialect.jsonValues("?")}) ORDER BY id LIMIT ${WAKE_UP_POLL_BATCH_SIZE}`;
493
+ const ids = JSON.stringify(brokerIds);
326
494
  const reader = this.options.reader ?? database;
327
495
  for (;;) {
328
496
  let rows: HintRow[];
329
497
  try {
330
- if (dialectOf(database).name === "postgres") {
331
- // #6560: one statement takes the batch and deletes it, through the reader, whose calls never queue
332
- // behind this database's writes. It returns the hints in no particular order.
333
- rows = await reader.all<HintRow>(
334
- `DELETE FROM ${this.table} WHERE id IN (SELECT id ${batch}) RETURNING id, broker_id, reason, version`,
335
- ...brokerIds,
336
- );
498
+ if (dialect.name === "postgres") {
499
+ // #6560: one statement takes the batch and deletes it. It is a write, so it goes through the database,
500
+ // never the reader, as a statement of its own in the wake-up table's domain: one round trip, queued
501
+ // behind neither other domains' writes nor read-only transactions. It returns the hints in no
502
+ // particular order.
503
+ const take = {
504
+ sql: `DELETE FROM ${this.table} WHERE id IN (SELECT id ${batch}) RETURNING id, broker_id, reason, version, target_identity`,
505
+ params: [ids],
506
+ };
507
+ rows = (
508
+ database.autocommit === undefined
509
+ ? await database.all(take.sql, ...take.params)
510
+ : await database.autocommit(take, { kind: "serial", domain: this.table })
511
+ ) as HintRow[];
337
512
  rows.sort((left, right) => Number(left.id) - Number(right.id));
338
513
  } else {
339
- rows = await reader.all<HintRow>(`SELECT id, broker_id, reason, version ${batch}`, ...brokerIds);
514
+ rows = await reader.all<HintRow>(
515
+ `SELECT id, broker_id, reason, version, target_identity ${batch}`,
516
+ ids,
517
+ );
340
518
  if (rows.length > 0) {
341
519
  await database.run(
342
- `DELETE FROM ${this.table} WHERE id IN (${rows.map(() => "?").join(", ")})`,
343
- ...rows.map((row) => row.id),
520
+ `DELETE FROM ${this.table} WHERE id IN (SELECT value FROM ${dialect.jsonValues("?")})`,
521
+ JSON.stringify(rows.map((row) => Number(row.id))),
344
522
  );
345
523
  }
346
524
  }
@@ -353,6 +531,7 @@ class DatabaseWakeUpBus implements WakeUpBus {
353
531
  brokerId: row.broker_id,
354
532
  reason: row.reason === "outcome" ? "outcome" : "outbox",
355
533
  version: row.version,
534
+ ...(row.target_identity === null ? {} : { targetIdentity: row.target_identity }),
356
535
  };
357
536
  for (const subscription of this.subscriptions) {
358
537
  if (subscription.brokerId === hint.brokerId) this.deliver(subscription, { kind: "hint", hint });