@bastani/atomic 0.10.10 → 0.10.11-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (402) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/README.md +1 -1
  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 +11 -13
  7. package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js.map +1 -1
  8. package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +9 -9
  9. package/dist/_shared/mode-shared-2QJSCUM6.js +55 -18
  10. package/dist/_shared/mode-shared-2QJSCUM6.js.map +1 -1
  11. package/dist/_shared/p1a-prewarm-XIU57JHF.js +3 -3
  12. package/dist/_shared/runtime-O4KCSG4C.js +5 -5
  13. package/dist/_shared/{sdk-RRLVN7ER.js → sdk-IQMULKFU.js} +2962 -470
  14. package/dist/_shared/sdk-IQMULKFU.js.map +1 -0
  15. package/dist/_shared/session-data-AHTU7IS6.js +4 -4
  16. package/dist/_shared/session-manager-list-2BLEEZVG.js +1 -1
  17. package/dist/_shared/settings-startup-8-DNZ2RIHM.js +1 -1
  18. package/dist/_shared/{settings-startup-4QCVCI53.js → settings-startup-HXQLSUWM.js} +8 -2
  19. package/dist/_shared/settings-startup-HXQLSUWM.js.map +1 -0
  20. package/dist/_shared/{startup-0-0-4VG2G44V.js → startup-0-0-3IXNMFM2.js} +482 -53
  21. package/dist/_shared/startup-0-0-3IXNMFM2.js.map +1 -0
  22. package/dist/_shared/{startup-0-1-XTIL65D4.js → startup-0-1-7T2IAE7J.js} +121 -251
  23. package/dist/_shared/startup-0-1-7T2IAE7J.js.map +1 -0
  24. package/dist/_shared/{startup-0-2-27OWFHV6.js → startup-0-2-5QHPCEON.js} +293 -10
  25. package/dist/_shared/startup-0-2-5QHPCEON.js.map +1 -0
  26. package/dist/_shared/{startup-0-3-T4DVFYWX.js → startup-0-3-4ZYBYQNM.js} +273 -14
  27. package/dist/_shared/startup-0-3-4ZYBYQNM.js.map +1 -0
  28. package/dist/_shared/startup-0-4-ZMJV547D.js +6 -6
  29. package/dist/_shared/{startup-0-5-FD2P735I.js → startup-0-5-YDW47QX6.js} +29 -71
  30. package/dist/_shared/startup-0-5-YDW47QX6.js.map +1 -0
  31. package/dist/_shared/startup-10-0-TDQNBDDL.js +9 -9
  32. package/dist/_shared/startup-12-0-55AAXBJZ.js +4 -4
  33. package/dist/_shared/startup-14-0-MLX36RBG.js +6 -6
  34. package/dist/_shared/startup-15-0-YUKGAC63.js +34 -10
  35. package/dist/_shared/startup-15-0-YUKGAC63.js.map +1 -1
  36. package/dist/_shared/startup-15-1-4S7FLMNG.js +10 -9
  37. package/dist/_shared/startup-15-1-4S7FLMNG.js.map +1 -1
  38. package/dist/_shared/startup-8-0-34CBHPZH.js +421 -84
  39. package/dist/_shared/startup-8-0-34CBHPZH.js.map +1 -1
  40. package/dist/_shared/{startup-8-1-4KANEHRA.js → startup-8-1-X32CYSXB.js} +1291 -722
  41. package/dist/_shared/startup-8-1-X32CYSXB.js.map +1 -0
  42. package/dist/_shared/storage-command-7MGXLJGZ.js +17 -9
  43. package/dist/_shared/storage-command-7MGXLJGZ.js.map +1 -1
  44. package/dist/_shared/task-reference-stream-JWMUZSJX.js +1 -1
  45. package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +2 -2
  46. package/dist/_shared/tree-outline-MUEPK5B7.js +3 -3
  47. package/dist/builtin/intercom/CHANGELOG.md +29 -0
  48. package/dist/builtin/intercom/broker/broker.bundle.mjs +1138 -384
  49. package/dist/builtin/intercom/broker/database-wake-up-bus.ts +103 -24
  50. package/dist/builtin/intercom/broker/multinode-router.ts +42 -21
  51. package/dist/builtin/intercom/broker/multinode-state-store.ts +22 -1
  52. package/dist/builtin/intercom/broker/open-broker-state.ts +82 -31
  53. package/dist/builtin/intercom/broker/paths.ts +8 -4
  54. package/dist/builtin/intercom/broker/released-asker-remote-replies.ts +18 -6
  55. package/dist/builtin/intercom/broker/sql-add-column.ts +4 -1
  56. package/dist/builtin/intercom/broker/sql-ended-child-queue-store.ts +65 -44
  57. package/dist/builtin/intercom/broker/sql-held-send-store.ts +11 -3
  58. package/dist/builtin/intercom/broker/sql-known-session-store.ts +24 -12
  59. package/dist/builtin/intercom/broker/sql-lease-store.ts +386 -65
  60. package/dist/builtin/intercom/broker/sql-outbox-store.ts +462 -130
  61. package/dist/builtin/intercom/broker/sql-pending-ask-store.ts +20 -9
  62. package/dist/builtin/intercom/broker/sql-released-ask-store.ts +14 -6
  63. package/dist/builtin/intercom/broker/sql-restoring-reservation-store.ts +27 -11
  64. package/dist/builtin/intercom/broker/sql-schema.ts +198 -5
  65. package/dist/builtin/intercom/broker/sql-sent-message-authority.ts +97 -34
  66. package/dist/builtin/intercom/index.bundle.mjs +11 -8
  67. package/dist/builtin/intercom/package.json +1 -1
  68. package/dist/builtin/mcp/index.bundle.mjs +1 -1
  69. package/dist/builtin/mcp/package.json +1 -1
  70. package/dist/builtin/subagents/CHANGELOG.md +18 -0
  71. package/dist/builtin/subagents/package.json +1 -1
  72. package/dist/builtin/subagents/skills/skill-creator/SKILL.md +1 -1
  73. package/dist/builtin/subagents/src/extension/index.bundle.mjs +62 -23
  74. package/dist/builtin/web-access/index.bundle.mjs +1 -1
  75. package/dist/builtin/web-access/package.json +1 -1
  76. package/dist/cli/args.js +2 -2
  77. package/dist/cli/auth-cli.code.js +6 -6
  78. package/dist/cli/auth-cli.impl.js +3 -3
  79. package/dist/cli/auth-cli.js +3 -3
  80. package/dist/cli/blueprint-command.code.js +4 -4
  81. package/dist/cli/blueprint-command.impl.js +3 -3
  82. package/dist/cli/blueprint-command.js +3 -3
  83. package/dist/cli/intercom-command.code.js +3 -3
  84. package/dist/cli/intercom-command.impl.js +3 -3
  85. package/dist/cli/intercom-command.js +3 -3
  86. package/dist/cli/list-models.code.js +2 -2
  87. package/dist/cli/list-models.impl.js +3 -3
  88. package/dist/cli/list-models.js +3 -3
  89. package/dist/cli/session-picker.code.js +2 -2
  90. package/dist/cli/session-picker.impl.js +3 -3
  91. package/dist/cli/session-picker.js +3 -3
  92. package/dist/cli/startup-ui.code.js +2 -2
  93. package/dist/cli/startup-ui.impl.js +3 -3
  94. package/dist/cli/startup-ui.js +3 -3
  95. package/dist/cli/storage-command.impl.js +3 -3
  96. package/dist/cli/storage-command.js +3 -3
  97. package/dist/cli/storage-search-session.code.js +3 -3
  98. package/dist/cli/storage-search-session.impl.js +3 -3
  99. package/dist/cli/storage-search-session.js +3 -3
  100. package/dist/cli/team-command.code.js +3 -3
  101. package/dist/cli/team-command.impl.js +3 -3
  102. package/dist/cli/team-command.js +3 -3
  103. package/dist/cli.js +2 -2
  104. package/dist/core/agent-session-methods.d.ts +3 -0
  105. package/dist/core/agent-session-methods.js.map +1 -1
  106. package/dist/core/agent-session.d.ts +1 -0
  107. package/dist/core/compaction/compactor.d.ts +2 -0
  108. package/dist/core/debug.impl.js +3 -3
  109. package/dist/core/debug.js +3 -3
  110. package/dist/core/export-html/card-projection.code.js +1 -1
  111. package/dist/core/export-html/card-projection.impl.js +3 -3
  112. package/dist/core/export-html/card-projection.js +3 -3
  113. package/dist/core/export-html/index.code.js +2 -2
  114. package/dist/core/export-html/index.impl.js +3 -3
  115. package/dist/core/export-html/index.js +3 -3
  116. package/dist/core/export-html/json-spans.impl.js +3 -3
  117. package/dist/core/export-html/json-spans.js +3 -3
  118. package/dist/core/export-html/oversized-render-inputs.code.js +2 -2
  119. package/dist/core/export-html/oversized-render-inputs.impl.js +3 -3
  120. package/dist/core/export-html/oversized-render-inputs.js +3 -3
  121. package/dist/core/export-html/tool-renderer.impl.js +3 -3
  122. package/dist/core/export-html/tool-renderer.js +3 -3
  123. package/dist/core/extensions/jiti-loader.impl.js +3 -3
  124. package/dist/core/extensions/jiti-loader.js +3 -3
  125. package/dist/core/extensions/jiti-static-loader.impl.js +3 -3
  126. package/dist/core/extensions/jiti-static-loader.js +3 -3
  127. package/dist/core/extensions/loader-host-modules.code.js +1 -1
  128. package/dist/core/extensions/loader-host-modules.impl.js +3 -3
  129. package/dist/core/extensions/loader-host-modules.js +3 -3
  130. package/dist/core/extensions/loader-virtual-modules.js +2 -2
  131. package/dist/core/extensions/runner-context.js +4 -4
  132. package/dist/core/extensions/runner-context.js.map +1 -1
  133. package/dist/core/intercom-broker-control-host.code.js +1 -1
  134. package/dist/core/intercom-broker-control-host.impl.js +3 -3
  135. package/dist/core/intercom-broker-control-host.js +3 -3
  136. package/dist/core/intercom-broker-control.code.js +3 -3
  137. package/dist/core/intercom-broker-control.impl.js +3 -3
  138. package/dist/core/intercom-broker-control.js +3 -3
  139. package/dist/core/model-registry.js +1 -1
  140. package/dist/core/model-resolver-defaults.d.ts +3 -2
  141. package/dist/core/package-manager-storage-adapters.code.js +1 -1
  142. package/dist/core/package-manager-storage-adapters.impl.js +3 -3
  143. package/dist/core/package-manager-storage-adapters.js +3 -3
  144. package/dist/core/sdk.code.js +4 -5
  145. package/dist/core/sdk.impl.js +3 -3
  146. package/dist/core/sdk.js +3 -3
  147. package/dist/core/session-manager-core.d.ts +5 -2
  148. package/dist/core/session-manager-core.js +39 -23
  149. package/dist/core/session-manager-core.js.map +1 -1
  150. package/dist/core/session-manager-list.impl.js +3 -3
  151. package/dist/core/session-manager-list.js +3 -3
  152. package/dist/core/session-manager.code.js +3 -3
  153. package/dist/core/session-manager.impl.js +3 -3
  154. package/dist/core/session-manager.js +3 -3
  155. package/dist/core/session-open-markers.js +3 -0
  156. package/dist/core/session-open-markers.js.map +1 -0
  157. package/dist/core/session-storage.code.js +1 -1
  158. package/dist/core/session-storage.impl.js +3 -3
  159. package/dist/core/session-storage.js +3 -3
  160. package/dist/core/storage/added-exports.d.ts +6 -0
  161. package/dist/core/storage/added-exports.js +19 -0
  162. package/dist/core/storage/added-exports.js.map +1 -0
  163. package/dist/core/storage/database.d.ts +53 -0
  164. package/dist/core/storage/database.js.map +1 -1
  165. package/dist/core/storage/fenced-paths.d.ts +47 -0
  166. package/dist/core/storage/fenced-paths.js +53 -0
  167. package/dist/core/storage/fenced-paths.js.map +1 -1
  168. package/dist/core/storage/file-identity.js +31 -2
  169. package/dist/core/storage/file-identity.js.map +1 -1
  170. package/dist/core/storage/index.d.ts +4 -1
  171. package/dist/core/storage/index.js +5 -2
  172. package/dist/core/storage/index.js.map +1 -1
  173. package/dist/core/storage/migrations.d.ts +33 -2
  174. package/dist/core/storage/migrations.js +73 -5
  175. package/dist/core/storage/migrations.js.map +1 -1
  176. package/dist/core/storage/node-sqlite-thread.js +48 -6
  177. package/dist/core/storage/node-sqlite-thread.js.map +1 -1
  178. package/dist/core/storage/node-sqlite.js +137 -4
  179. package/dist/core/storage/node-sqlite.js.map +1 -1
  180. package/dist/core/storage/offline-snapshot-file.impl.js +3 -3
  181. package/dist/core/storage/offline-snapshot-file.js +3 -3
  182. package/dist/core/storage/prefix.d.ts +3 -2
  183. package/dist/core/storage/prefix.js +23 -2
  184. package/dist/core/storage/prefix.js.map +1 -1
  185. package/dist/core/storage/provider.js +2 -2
  186. package/dist/core/storage/schema-catalog.d.ts +120 -0
  187. package/dist/core/storage/schema-catalog.js +351 -0
  188. package/dist/core/storage/schema-catalog.js.map +1 -0
  189. package/dist/core/storage/selection.js +2 -2
  190. package/dist/core/storage/session/branch-fork-source.code.js +47 -25
  191. package/dist/core/storage/session/branch-fork-source.code.js.map +1 -1
  192. package/dist/core/storage/session/branch-fork-source.impl.js +3 -3
  193. package/dist/core/storage/session/branch-fork-source.js +3 -3
  194. package/dist/core/storage/session/card-view.impl.js +3 -3
  195. package/dist/core/storage/session/card-view.js +3 -3
  196. package/dist/core/storage/session/child-reader.code.js +4 -4
  197. package/dist/core/storage/session/child-reader.impl.js +3 -3
  198. package/dist/core/storage/session/child-reader.js +3 -3
  199. package/dist/core/storage/session/chunked-resume-projection.code.js +1 -1
  200. package/dist/core/storage/session/chunked-resume-projection.impl.js +3 -3
  201. package/dist/core/storage/session/chunked-resume-projection.js +3 -3
  202. package/dist/core/storage/session/errors.js +1 -1
  203. package/dist/core/storage/session/expanded-child-details.code.js +4 -4
  204. package/dist/core/storage/session/expanded-child-details.impl.js +3 -3
  205. package/dist/core/storage/session/expanded-child-details.js +3 -3
  206. package/dist/core/storage/session/fork-detach.d.ts +118 -0
  207. package/dist/core/storage/session/fork-row-bytes.impl.js +3 -3
  208. package/dist/core/storage/session/fork-row-bytes.js +3 -3
  209. package/dist/core/storage/session/fork-row-index.impl.js +3 -3
  210. package/dist/core/storage/session/fork-row-index.js +3 -3
  211. package/dist/core/storage/session/fork-schema.js +53 -0
  212. package/dist/core/storage/session/fork-schema.js.map +1 -0
  213. package/dist/core/storage/session/fork-segments.d.ts +58 -0
  214. package/dist/core/storage/session/fork-staging.code.js +176 -36
  215. package/dist/core/storage/session/fork-staging.code.js.map +1 -1
  216. package/dist/core/storage/session/fork-staging.impl.js +3 -3
  217. package/dist/core/storage/session/fork-staging.js +3 -3
  218. package/dist/core/storage/session/fork-sweep.js +61 -0
  219. package/dist/core/storage/session/fork-sweep.js.map +1 -0
  220. package/dist/core/storage/session/fork-window.code.js +4 -4
  221. package/dist/core/storage/session/fork-window.impl.js +3 -3
  222. package/dist/core/storage/session/fork-window.js +3 -3
  223. package/dist/core/storage/session/history-pages.impl.js +3 -3
  224. package/dist/core/storage/session/history-pages.js +3 -3
  225. package/dist/core/storage/session/jsonl-entry-projection.impl.js +3 -3
  226. package/dist/core/storage/session/jsonl-entry-projection.js +3 -3
  227. package/dist/core/storage/session/jsonl-session-store.d.ts +37 -1
  228. package/dist/core/storage/session/jsonl-session-store.impl.js +3 -3
  229. package/dist/core/storage/session/jsonl-session-store.js +3 -3
  230. package/dist/core/storage/session/memory-session-store.impl.js +3 -3
  231. package/dist/core/storage/session/memory-session-store.js +3 -3
  232. package/dist/core/storage/session/pinned-branch-resume.code.js +3 -3
  233. package/dist/core/storage/session/pinned-branch-resume.impl.js +3 -3
  234. package/dist/core/storage/session/pinned-branch-resume.js +3 -3
  235. package/dist/core/storage/session/released-history-files.code.js +3 -3
  236. package/dist/core/storage/session/released-history-files.impl.js +3 -3
  237. package/dist/core/storage/session/released-history-files.js +3 -3
  238. package/dist/core/storage/session/resident-fork-rows.code.js +1 -1
  239. package/dist/core/storage/session/resident-fork-rows.impl.js +3 -3
  240. package/dist/core/storage/session/resident-fork-rows.js +3 -3
  241. package/dist/core/storage/session/session-resume.impl.js +3 -3
  242. package/dist/core/storage/session/session-resume.js +3 -3
  243. package/dist/core/storage/session/session-search-index.d.ts +5 -0
  244. package/dist/core/storage/session/snapshot-cut.impl.js +3 -3
  245. package/dist/core/storage/session/snapshot-cut.js +3 -3
  246. package/dist/core/storage/session/snapshot-history.code.js +4 -4
  247. package/dist/core/storage/session/snapshot-history.impl.js +3 -3
  248. package/dist/core/storage/session/snapshot-history.js +3 -3
  249. package/dist/core/storage/session/sql-chat-records.d.ts +15 -0
  250. package/dist/core/storage/session/sql-session-store.code.js +1 -1
  251. package/dist/core/storage/session/sql-session-store.d.ts +445 -11
  252. package/dist/core/storage/session/sql-session-store.impl.js +4 -4
  253. package/dist/core/storage/session/sql-session-store.js +4 -4
  254. package/dist/core/storage/session/tree-outline.impl.js +3 -3
  255. package/dist/core/storage/session/tree-outline.js +3 -3
  256. package/dist/core/storage/session/tree-presentation.code.js +1 -1
  257. package/dist/core/storage/session/tree-presentation.impl.js +3 -3
  258. package/dist/core/storage/session/tree-presentation.js +3 -3
  259. package/dist/core/storage/session/types.d.ts +36 -0
  260. package/dist/core/storage/sqlite-facade.d.ts +54 -6
  261. package/dist/core/storage/sqlite-facade.js +384 -18
  262. package/dist/core/storage/sqlite-facade.js.map +1 -1
  263. package/dist/core/storage/statement-sequence.d.ts +83 -0
  264. package/dist/core/storage/statement-sequence.js +21 -0
  265. package/dist/core/storage/statement-sequence.js.map +1 -1
  266. package/dist/core/storage/storage-status.code.js +1 -1
  267. package/dist/core/storage/storage-status.impl.js +3 -3
  268. package/dist/core/storage/storage-status.js +3 -3
  269. package/dist/core/storage/team.code.js +1 -1
  270. package/dist/core/storage/team.impl.js +3 -3
  271. package/dist/core/storage/team.js +3 -3
  272. package/dist/core/structured-output/index.code.js +1 -1
  273. package/dist/core/structured-output/index.impl.js +3 -3
  274. package/dist/core/structured-output/index.js +3 -3
  275. package/dist/core/tasks/task-completion-entry.code.js +1 -1
  276. package/dist/core/tasks/task-completion-entry.impl.js +3 -3
  277. package/dist/core/tasks/task-completion-entry.js +3 -3
  278. package/dist/core/tasks/task-file-history.code.js +4 -4
  279. package/dist/core/tasks/task-file-history.impl.js +3 -3
  280. package/dist/core/tasks/task-file-history.js +3 -3
  281. package/dist/core/tasks/task-reference-archive.code.js +5 -5
  282. package/dist/core/tasks/task-reference-archive.js +3 -3
  283. package/dist/core/tasks/task-reference-identity.js +3 -3
  284. package/dist/core/tasks/task-reference-stream.impl.js +3 -3
  285. package/dist/core/tasks/task-reference-stream.js +3 -3
  286. package/dist/core/tasks/task-unsaved-answer.impl.js +3 -3
  287. package/dist/core/tasks/task-unsaved-answer.js +3 -3
  288. package/dist/core/tasks/task-view-source.code.js +1 -1
  289. package/dist/core/tasks/task-view-source.impl.js +3 -3
  290. package/dist/core/tasks/task-view-source.js +3 -3
  291. package/dist/core/tools/bash.d.ts +8 -4
  292. package/dist/core/tools/history-session.d.ts +7 -0
  293. package/dist/core/tools/history-tasks.d.ts +123 -0
  294. package/dist/core/tools/kill.d.ts +5 -2
  295. package/dist/core/tools/powershell.d.ts +2 -2
  296. package/dist/core/tools/read-url.code.js +3 -3
  297. package/dist/core/tools/read-url.impl.js +3 -3
  298. package/dist/core/tools/read-url.js +3 -3
  299. package/dist/core/tools/read.d.ts +12 -2
  300. package/dist/core/tools/resource-selectors.d.ts +2 -0
  301. package/dist/core/tools/search.d.ts +12 -2
  302. package/dist/core/worktree-snapshot/blob-store.d.ts +45 -2
  303. package/dist/core/worktree-snapshot/blob-store.js +228 -60
  304. package/dist/core/worktree-snapshot/blob-store.js.map +1 -1
  305. package/dist/core/worktree-snapshot/errors.code.js +3 -3
  306. package/dist/core/worktree-snapshot/errors.impl.js +3 -3
  307. package/dist/core/worktree-snapshot/errors.js +3 -3
  308. package/dist/core/worktree-snapshot/fork-snapshot-index.js +121 -0
  309. package/dist/core/worktree-snapshot/fork-snapshot-index.js.map +1 -0
  310. package/dist/core/worktree-snapshot/session-index-reset.impl.js +3 -3
  311. package/dist/core/worktree-snapshot/session-index-reset.js +3 -3
  312. package/dist/extensions/codemode/execute.code.js +3 -3
  313. package/dist/extensions/codemode/execute.impl.js +3 -3
  314. package/dist/extensions/codemode/execute.js +3 -3
  315. package/dist/extensions/codemode/renderer.code.js +2 -2
  316. package/dist/extensions/codemode/renderer.impl.js +3 -3
  317. package/dist/extensions/codemode/renderer.js +3 -3
  318. package/dist/extensions/mcp/cli.code.js +3 -3
  319. package/dist/extensions/mcp/cli.impl.js +3 -3
  320. package/dist/extensions/mcp/cli.js +3 -3
  321. package/dist/extensions/mcp/cli.lazy.code.js +2 -2
  322. package/dist/extensions/mcp/cli.lazy.impl.js +3 -3
  323. package/dist/extensions/mcp/cli.lazy.js +3 -3
  324. package/dist/extensions/mcp/runtime.code.js +1 -1
  325. package/dist/extensions/mcp/runtime.impl.js +3 -3
  326. package/dist/extensions/mcp/runtime.js +3 -3
  327. package/dist/index.js +8 -8
  328. package/dist/main-first-time-setup.code.js +2 -2
  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/agent-panel.d.ts +1 -0
  333. package/dist/modes/interactive/components/stored-tree-view.code.js +3 -3
  334. package/dist/modes/interactive/components/stored-tree-view.impl.js +3 -3
  335. package/dist/modes/interactive/components/stored-tree-view.js +3 -3
  336. package/dist/modes/interactive/components/task-inspector.d.ts +2 -0
  337. package/dist/modes/interactive/components/user-message-selector-archive.code.js +1 -1
  338. package/dist/modes/interactive/components/user-message-selector-archive.impl.js +3 -3
  339. package/dist/modes/interactive/components/user-message-selector-archive.js +3 -3
  340. package/dist/modes/interactive/interactive-mode-helpers.d.ts +2 -2
  341. package/dist/modes/print-mode.js +1 -1
  342. package/dist/modes/rpc/agent-panel-bridge.d.ts +5 -0
  343. package/dist/package-manager-cli.code.js +7 -7
  344. package/dist/package-manager-cli.impl.js +3 -3
  345. package/dist/package-manager-cli.js +3 -3
  346. package/dist/rpc-entry.js +1 -1
  347. package/dist/startup-files.json +1 -1
  348. package/dist/startup-stages/0.js +1 -1
  349. package/dist/startup-stages/1.js +1 -1
  350. package/dist/startup-stages/10.js +1 -1
  351. package/dist/startup-stages/12.js +1 -1
  352. package/dist/startup-stages/2.js +9 -1
  353. package/dist/startup-stages/3.js +1 -6
  354. package/dist/startup-stages/5.js +1 -1
  355. package/dist/startup-stages/7.js +1 -1
  356. package/dist/startup-stages/8.js +2 -2
  357. package/dist/utils/startup-database-spare.code.js +3 -3
  358. package/dist/utils/startup-database-spare.impl.js +3 -3
  359. package/dist/utils/startup-database-spare.js +3 -3
  360. package/dist/utils/tools-manager.code.js +1 -1
  361. package/dist/utils/tools-manager.impl.js +3 -3
  362. package/dist/utils/tools-manager.js +3 -3
  363. package/docs/background-tasks.md +123 -23
  364. package/docs/compaction.md +18 -0
  365. package/docs/docs.json +2 -0
  366. package/docs/environment-variables.md +1 -1
  367. package/docs/getting-started/installation.md +9 -2
  368. package/docs/guides/postgres-pgbouncer.md +346 -0
  369. package/docs/guides/postgres-tuning.md +311 -0
  370. package/docs/guides/resume-on-any-machine.md +2 -2
  371. package/docs/guides.md +2 -0
  372. package/docs/index.md +1 -1
  373. package/docs/libraries/agent-core.md +1 -1
  374. package/docs/libraries/ai.md +2 -2
  375. package/docs/libraries/codemode.md +1 -1
  376. package/docs/libraries/storage-libsql.md +6 -1
  377. package/docs/libraries/storage-postgres.md +11 -6
  378. package/docs/libraries/tui.md +1 -1
  379. package/docs/libraries.md +2 -2
  380. package/docs/programmatic.md +1 -1
  381. package/docs/quickstart.md +1 -1
  382. package/docs/sdk/storage.md +1 -1
  383. package/docs/sdk/web-server.md +1 -3
  384. package/docs/sdk.md +1 -7
  385. package/docs/sessions.md +67 -2
  386. package/docs/settings.md +20 -1
  387. package/docs/skills/authoring.md +1 -1
  388. package/docs/skills.md +79 -6
  389. package/docs/storage.md +305 -14
  390. package/docs/subagents/reference.md +2 -2
  391. package/docs/subagents.md +4 -3
  392. package/docs/tools.md +44 -10
  393. package/docs/windows.md +1 -1
  394. package/package.json +9 -9
  395. package/dist/_shared/sdk-RRLVN7ER.js.map +0 -1
  396. package/dist/_shared/settings-startup-4QCVCI53.js.map +0 -1
  397. package/dist/_shared/startup-0-0-4VG2G44V.js.map +0 -1
  398. package/dist/_shared/startup-0-1-XTIL65D4.js.map +0 -1
  399. package/dist/_shared/startup-0-2-27OWFHV6.js.map +0 -1
  400. package/dist/_shared/startup-0-3-T4DVFYWX.js.map +0 -1
  401. package/dist/_shared/startup-0-5-FD2P735I.js.map +0 -1
  402. package/dist/_shared/startup-8-1-4KANEHRA.js.map +0 -1
@@ -19,7 +19,7 @@ import type {
19
19
  } from "./multinode-state-store.js";
20
20
  import { SETTLED_ASK_RETENTION_MS } from "./multinode-state-store.js";
21
21
  import { addMissingColumns } from "./sql-add-column.js";
22
- import { ensureBrokerSchema } from "./sql-schema.js";
22
+ import { ensureBrokerSchema, withBrokerSchemaCatalog } from "./sql-schema.js";
23
23
 
24
24
  type AskRow = SqliteRow & { sender_session_id: string; target_session_id: string; message_id: string };
25
25
 
@@ -49,12 +49,23 @@ function invalidRoute(route: ReplyRoute): InvalidStateInput | undefined {
49
49
  }
50
50
 
51
51
  /** S3: `PendingAskStore` on the shared storage adapter's database, table `<prefix>intercom_pending_asks`. */
52
- export async function openSqlPendingAskStore({
52
+ export function openSqlPendingAskStore(options: IntercomBrokerStateOptions): Promise<PendingAskStore> {
53
+ // Its schema checks answer from one read of the catalog, shared with an open around it: a current schema costs
54
+ // no transaction and no lock, also when the store is opened on its own (#6560).
55
+ return withBrokerSchemaCatalog(options.database, options.prefix, () => openPendingAskStoreInCatalog(options));
56
+ }
57
+
58
+ async function openPendingAskStoreInCatalog({
53
59
  database,
54
60
  prefix,
55
61
  }: IntercomBrokerStateOptions): Promise<PendingAskStore> {
56
62
  const table = `${validateStoragePrefix(prefix, dialectOf(database).name)}intercom_pending_asks`;
57
63
  const { types, nowMs, greatest } = dialectOf(database);
64
+ /**
65
+ * Its transactions read and write this table and the settled asks alone, which no other store touches, so they
66
+ * exclude one another, not every writer of the database (#6560).
67
+ */
68
+ const serial = { concurrency: { kind: "serial", domain: table } } as const;
58
69
  await ensureBrokerSchema(database, prefix, "pending-asks", (tx) =>
59
70
  tx.exec(`
60
71
  CREATE TABLE IF NOT EXISTS ${table}(
@@ -112,7 +123,7 @@ export async function openSqlPendingAskStore({
112
123
  ...params,
113
124
  );
114
125
  return changes === 0 ? ({ status: "duplicate" } as const) : ({ status: "recorded" } as const);
115
- });
126
+ }, serial);
116
127
  },
117
128
  async matchesReply(route): Promise<PendingAskMatchResult> {
118
129
  const bad = invalidRoute(route);
@@ -132,7 +143,7 @@ export async function openSqlPendingAskStore({
132
143
  );
133
144
  await tx.run(`DELETE FROM ${settled} WHERE settled_at < ${nowMs} - ?`, SETTLED_ASK_RETENTION_MS);
134
145
  return changes === 0 ? ({ status: "no-match" } as const) : ({ status: "cleared" } as const);
135
- });
146
+ }, serial);
136
147
  },
137
148
  async isSettled(route): Promise<boolean> {
138
149
  if (invalidRoute(route) !== undefined) return false;
@@ -177,7 +188,7 @@ export async function openSqlPendingAskStore({
177
188
  );
178
189
  await tx.run(`DELETE FROM ${table} WHERE target_session_id = ?`, targetSessionId);
179
190
  return taken;
180
- });
191
+ }, serial);
181
192
  return { status: "taken", asks: rows.map(toAsk) };
182
193
  },
183
194
  async listForTarget(targetSessionId): Promise<PendingAskListResult> {
@@ -220,7 +231,7 @@ export async function openSqlPendingAskStore({
220
231
  ...parked.params,
221
232
  );
222
233
  return found;
223
- });
234
+ }, serial);
224
235
  return {
225
236
  status: "rebound",
226
237
  asks: rows.map((row) => ({
@@ -241,7 +252,7 @@ export async function openSqlPendingAskStore({
241
252
  );
242
253
  await tx.run(`DELETE FROM ${table} WHERE ${parked.where}`, ...parked.params);
243
254
  return taken;
244
- });
255
+ }, serial);
245
256
  return { status: "taken", asks: rows.map(toAsk) };
246
257
  },
247
258
  async listParked(ownerSessionId, reservationKey): Promise<PendingAskListResult> {
@@ -292,7 +303,7 @@ export async function openSqlPendingAskStore({
292
303
  if (Number(changes) === 1) marked.push(row);
293
304
  }
294
305
  return marked;
295
- });
306
+ }, serial);
296
307
  return { status: "listed", asks: rows.map(toAsk) };
297
308
  },
298
309
  async unmarkEnded(route): Promise<PendingAskMarkResult> {
@@ -318,7 +329,7 @@ export async function openSqlPendingAskStore({
318
329
  WHERE ${where} AND replied IS NULL AND ended_at IS NOT NULL ORDER BY "order"`,
319
330
  ...params,
320
331
  );
321
- });
332
+ }, serial);
322
333
  return { status: "listed", asks: rows.map(toAsk) };
323
334
  }
324
335
  }
@@ -17,7 +17,7 @@ import type {
17
17
  ReleasedAskSupersede,
18
18
  StateStoreInvalidReason,
19
19
  } from "./multinode-state-store.js";
20
- import { ensureBrokerSchema } from "./sql-schema.js";
20
+ import { ensureBrokerSchema, withBrokerSchemaCatalog } from "./sql-schema.js";
21
21
 
22
22
  interface ReleasedAskRow extends SqliteRow {
23
23
  readonly question_id: string;
@@ -69,9 +69,17 @@ function toRecord(row: ReleasedAskRow): ReleasedAskRecord {
69
69
  }
70
70
 
71
71
  /** S3 (#4386): `ReleasedAskStore` on the shared storage adapter's database, table `<prefix>intercom_released_asks`. */
72
- export async function openSqlReleasedAskStore({ database, prefix }: IntercomBrokerStateOptions): Promise<ReleasedAskStore> {
72
+ export function openSqlReleasedAskStore(options: IntercomBrokerStateOptions): Promise<ReleasedAskStore> {
73
+ // Its schema checks answer from one read of the catalog, shared with an open around it: a current schema costs
74
+ // no transaction and no lock, also when the store is opened on its own (#6560).
75
+ return withBrokerSchemaCatalog(options.database, options.prefix, () => openReleasedAskStoreInCatalog(options));
76
+ }
77
+
78
+ async function openReleasedAskStoreInCatalog({ database, prefix }: IntercomBrokerStateOptions): Promise<ReleasedAskStore> {
73
79
  const table = `${validateStoragePrefix(prefix, dialectOf(database).name)}intercom_released_asks`;
74
80
  const { types, nowMs, greatest } = dialectOf(database);
81
+ /** Its transactions read and write this table alone, so they exclude one another, not every writer (#6560). */
82
+ const serial = { concurrency: { kind: "serial", domain: table } } as const;
75
83
  await ensureBrokerSchema(database, prefix, "released-asks", (tx) =>
76
84
  tx.exec(`
77
85
  CREATE TABLE IF NOT EXISTS ${table}(
@@ -124,7 +132,7 @@ export async function openSqlReleasedAskStore({ database, prefix }: IntercomBrok
124
132
  inserted += changes;
125
133
  }
126
134
  return inserted;
127
- });
135
+ }, serial);
128
136
  return { status: "kept", kept };
129
137
  },
130
138
  async lookup(key) {
@@ -153,7 +161,7 @@ export async function openSqlReleasedAskStore({ database, prefix }: IntercomBrok
153
161
  return { status: row.reply_message_id === claim.replyMessageId ? "claimed" : "taken", ask: toRecord(row) } as const;
154
162
  }
155
163
  return claimFor(tx, claim, row);
156
- });
164
+ }, serial);
157
165
  },
158
166
  async supersede(claim) {
159
167
  const bad = invalidReleasedAskSupersede(claim);
@@ -169,7 +177,7 @@ export async function openSqlReleasedAskStore({ database, prefix }: IntercomBrok
169
177
  return { status: row.reply_message_id === claim.replyMessageId ? "claimed" : "taken", ask: toRecord(row) } as const;
170
178
  }
171
179
  return claimFor(tx, claim, row);
172
- });
180
+ }, serial);
173
181
  },
174
182
  async answer(claim) {
175
183
  const bad = invalidReleasedAskClaim(claim);
@@ -216,6 +224,6 @@ export async function openSqlReleasedAskStore({ database, prefix }: IntercomBrok
216
224
  claim.replierSessionId,
217
225
  );
218
226
  return { status: "settled" } as const;
219
- });
227
+ }, serial);
220
228
  }
221
229
  }
@@ -27,7 +27,7 @@ import type {
27
27
  import { reservationAdmitsSender } from "./group-membership.js";
28
28
  import { isReleasedReservationKey } from "./released-children.js";
29
29
  import { addMissingColumns } from "./sql-add-column.js";
30
- import { ensureBrokerSchema } from "./sql-schema.js";
30
+ import { ensureBrokerSchema, openCatalogOf, withBrokerSchemaCatalog } from "./sql-schema.js";
31
31
 
32
32
  type ReservationRow = SqliteRow & {
33
33
  reservation_key: string;
@@ -123,7 +123,13 @@ async function renameWakeIndex(tx: SqliteExecutor, dialect: StorageDialect, tabl
123
123
  * `<prefix>intercom_restoring_reservations`. Insertion order and fencing tokens come from
124
124
  * `<prefix>intercom_restoring_reservation_counters`, so both keep increasing after `prune` deletes rows.
125
125
  */
126
- export async function openSqlRestoringReservationStore({
126
+ export function openSqlRestoringReservationStore(options: IntercomBrokerStateOptions): Promise<RestoringReservationStore> {
127
+ // Its schema checks answer from one read of the catalog, shared with an open around it: a current schema costs
128
+ // no transaction and no lock, also when the store is opened on its own (#6560).
129
+ return withBrokerSchemaCatalog(options.database, options.prefix, () => openRestoringReservationStoreInCatalog(options));
130
+ }
131
+
132
+ async function openRestoringReservationStoreInCatalog({
127
133
  database,
128
134
  prefix,
129
135
  }: IntercomBrokerStateOptions): Promise<RestoringReservationStore> {
@@ -132,6 +138,11 @@ export async function openSqlRestoringReservationStore({
132
138
  const counters = `${validPrefix}intercom_restoring_reservation_counters`;
133
139
  const wakeClaims = `${validPrefix}intercom_restoring_wake_claims`;
134
140
  const { types, nowMs, greatest } = dialectOf(database);
141
+ /**
142
+ * Its transactions read and write this table, its counters and its wake claims alone, which no other store
143
+ * touches, so they exclude one another, not every writer of the database (#6560).
144
+ */
145
+ const serial = { concurrency: { kind: "serial", domain: table } } as const;
135
146
  await ensureBrokerSchema(database, prefix, "restoring-reservations", (tx) =>
136
147
  tx.exec(`
137
148
  CREATE TABLE IF NOT EXISTS ${table}(
@@ -172,8 +183,13 @@ export async function openSqlRestoringReservationStore({
172
183
  await ensureBrokerSchema(database, prefix, "restoring-reservation-indexes", (tx) =>
173
184
  tx.exec(`CREATE INDEX IF NOT EXISTS ${table}_child ON ${table}(child_target) WHERE end_json IS NULL`),
174
185
  );
175
- // Probed every time, as an earlier broker may recreate its index meanwhile; a current one runs no DDL (#5218).
176
- await database.transaction((tx) => renameWakeIndex(tx, dialectOf(database), table));
186
+ // Probed every time, as an earlier broker may recreate its index meanwhile; a current one runs no DDL (#5218). The
187
+ // catalog an open read answers that probe, so a current index costs no transaction (#6560).
188
+ const catalog = await openCatalogOf(database);
189
+ const wakeIndexCurrent =
190
+ catalog?.hasObject(`${table}_wake`) === true &&
191
+ !catalog.hasObject(`${table}_wake_requested`.slice(0, dialectOf(database).name === "postgres" ? 63 : undefined));
192
+ if (!wakeIndexCurrent) await database.transaction((tx) => renameWakeIndex(tx, dialectOf(database), table));
177
193
  const columns = `reservation_key, owner_session_id, owner_broker_id, child_name AS child_name_json, child_target,
178
194
  targets_json, "group" AS reservation_group, dormant, wake, wake_generation, fencing_token, "order" AS reservation_order,
179
195
  gone AS gone_json, end_json,
@@ -316,7 +332,7 @@ export async function openSqlRestoringReservationStore({
316
332
  },
317
333
  superseded,
318
334
  };
319
- });
335
+ }, serial);
320
336
  },
321
337
  async lookup(target, sender, staleAfterMs): Promise<RestoringReservationLookupResult> {
322
338
  if (target.length === 0) return invalid("empty-target");
@@ -353,7 +369,7 @@ export async function openSqlRestoringReservationStore({
353
369
  reservationKey,
354
370
  );
355
371
  return { status: changes > 0 ? "requested" : "not-wakeable" };
356
- });
372
+ }, serial);
357
373
  },
358
374
  async takeWakeRequests(ownerBrokerId): Promise<RestoringReservationTakeWakeResult> {
359
375
  if (ownerBrokerId.length === 0) return invalid("empty-broker-id");
@@ -374,7 +390,7 @@ export async function openSqlRestoringReservationStore({
374
390
  childName: JSON.parse(row.child_name_json) as string,
375
391
  })),
376
392
  };
377
- });
393
+ }, serial);
378
394
  },
379
395
  async renew(lease): Promise<RestoringReservationRenewResult> {
380
396
  const bad = invalidLease(lease);
@@ -387,7 +403,7 @@ export async function openSqlRestoringReservationStore({
387
403
  lease.reservationKey,
388
404
  );
389
405
  return { status: "renewed" };
390
- });
406
+ }, serial);
391
407
  },
392
408
  async release(lease, outcome): Promise<RestoringReservationReleaseResult> {
393
409
  const bad = invalidLease(lease);
@@ -411,7 +427,7 @@ export async function openSqlRestoringReservationStore({
411
427
  : { outcome: "released", refusal: outcome.refusal },
412
428
  );
413
429
  return { status: "released" };
414
- });
430
+ }, serial);
415
431
  },
416
432
  async consume(reservationKey, registered): Promise<RestoringReservationConsumeResult> {
417
433
  if (reservationKey.length === 0) return invalid("empty-reservation-key");
@@ -435,7 +451,7 @@ export async function openSqlRestoringReservationStore({
435
451
  ended.push(row.reservation_key);
436
452
  }
437
453
  return { status: "consumed", reservationKeys: ended };
438
- });
454
+ }, serial);
439
455
  },
440
456
  async insertionMark(): Promise<number> {
441
457
  const row = await database.get<SqliteRow & { value: number }>(
@@ -458,7 +474,7 @@ export async function openSqlRestoringReservationStore({
458
474
  );
459
475
  await tx.run(`DELETE FROM ${wakeClaims} WHERE reservation_key NOT IN (SELECT reservation_key FROM ${table})`);
460
476
  return changes;
461
- });
477
+ }, serial);
462
478
  },
463
479
  };
464
480
  }
@@ -1,9 +1,44 @@
1
+ import * as storage from "@bastani/atomic/storage";
1
2
  import { dialectOf, ensureStorageSchema, type SqliteDatabase, type SqliteExecutor, storageMigrationTable } from "@bastani/atomic/storage";
2
3
 
4
+ /**
5
+ * `storageSchemaCurrent`, `schemaCatalogOf` and `withStorageSchemaCatalog` of the storage API, which an Atomic
6
+ * installation older than #6560 lacks: a named import would fail to link there, so they are taken from the module
7
+ * namespace, and without them every open checks its schema in a transaction, as before.
8
+ */
9
+ const catalogApi: Partial<
10
+ Pick<
11
+ typeof storage,
12
+ "storageSchemaCurrent" | "schemaCatalogOf" | "withStorageSchemaCatalog" | "restampStorageSchemaAtOpenEnd"
13
+ >
14
+ > = storage;
15
+
16
+ /**
17
+ * Runs `open`, a broker store's open on `database`, with one read of the prefix's schema catalog, which its schema
18
+ * checks answer from: a store whose schema is current opens with no schema transaction and no lock, whether the broker
19
+ * opens it with the others (`openIntercomBrokerState`, whose read it shares), on its own, or later, as the ended-child
20
+ * queue of a new home (#6560). `views` are other handles on the same connection whose checks answer from the read.
21
+ */
22
+ export async function withBrokerSchemaCatalog<T>(
23
+ database: SqliteDatabase,
24
+ prefix: string | undefined,
25
+ open: () => Promise<T>,
26
+ views: readonly SqliteDatabase[] = [],
27
+ ): Promise<T> {
28
+ const withCatalog = catalogApi.withStorageSchemaCatalog;
29
+ return withCatalog === undefined ? open() : withCatalog(database, storageMigrationTable(prefix), open, views);
30
+ }
31
+
3
32
  /**
4
33
  * Runs `setup`, a broker store's idempotent schema statements, in a transaction of its own, unless the prefix's ledger
5
34
  * records `revision` for `store` already (#5218): on PostgreSQL a current schema runs no DDL, so a role with only DML
6
- * privileges opens the store. Raise `revision` whenever `setup` creates or alters anything new.
35
+ * privileges opens the store. Raise `revision` whenever `setup` creates or alters anything new. Inside an open that read
36
+ * the catalog (`withStorageSchemaCatalog`), a revision it found recorded skips the transaction too (#6560). On SQLite and
37
+ * libSQL the store's stamp records it. A setup that runs stamps nothing: later setups of the same open would leave its
38
+ * stamp behind, and stamping costs a new database's first open statements and a transaction. The next open finds the
39
+ * store in the catalog instead; there a setup whose objects the catalog holds is checked again, and a counter it seeds
40
+ * seeded, in the one transaction the open's end stamps every such store in, so later opens of a current schema take no
41
+ * write lock and answer every store from its stamp.
7
42
  */
8
43
  export async function ensureBrokerSchema(
9
44
  database: SqliteDatabase,
@@ -13,9 +48,167 @@ export async function ensureBrokerSchema(
13
48
  revision = 1,
14
49
  ): Promise<void> {
15
50
  const schema = { ledger: storageMigrationTable(prefix), domain: `intercom-${store}`, revision };
16
- await database.transaction((tx) =>
17
- ensureStorageSchema(tx, dialectOf(database), schema, async () => {
51
+ if (await catalogApi.storageSchemaCurrent?.(database, schema)) return;
52
+ const found = await foundInCatalog(database, setup);
53
+ if (found?.present === true) {
54
+ const options = { verify: found.verify, deferred: found.seeds };
55
+ if (catalogApi.restampStorageSchemaAtOpenEnd?.(database, schema, setup, options) === true) return;
56
+ if (!found.seeds) return;
57
+ }
58
+ const dialect = dialectOf(database);
59
+ await database.transaction(async (tx) => {
60
+ if (!dialect.versionedMigrations) {
18
61
  await setup(tx);
19
- }),
20
- );
62
+ return;
63
+ }
64
+ await ensureStorageSchema(tx, dialect, schema, async () => {
65
+ await setup(tx);
66
+ });
67
+ });
68
+ // Stamped where the end of the open takes a transaction for other stores anyway, as after a repair or an upgrade.
69
+ if (!dialect.versionedMigrations)
70
+ catalogApi.restampStorageSchemaAtOpenEnd?.(database, schema, setup, { verify: found?.verify, alongside: true });
71
+ }
72
+
73
+ /**
74
+ * Asks the open of `database` in progress to stamp `store`'s `setup` at its end, on SQLite and libSQL, where the open
75
+ * found the store current in the catalog, `verify` says, rather than by its stamp, which a schema change since left
76
+ * behind: the next open then answers from the stamp (#6560). Does nothing where the stamp is current, elsewhere, and
77
+ * outside an open.
78
+ */
79
+ export async function stampBrokerSchemaAtOpenEnd(
80
+ database: SqliteDatabase,
81
+ prefix: string | undefined,
82
+ store: string,
83
+ setup: (tx: SqliteExecutor) => Promise<unknown>,
84
+ verify: (catalog: storage.StorageSchemaCatalog) => boolean,
85
+ revision = 1,
86
+ ): Promise<void> {
87
+ const schema = { ledger: storageMigrationTable(prefix), domain: `intercom-${store}`, revision };
88
+ if (await catalogApi.storageSchemaCurrent?.(database, schema)) return;
89
+ catalogApi.restampStorageSchemaAtOpenEnd?.(database, schema, setup, { verify });
90
+ }
91
+
92
+ /**
93
+ * What `setup` creates on a SQLite or libSQL database, where it only runs `CREATE TABLE` and `CREATE INDEX` statements
94
+ * with `IF NOT EXISTS` and seeds rows with `INSERT … ON CONFLICT … DO NOTHING`: `present`, whether the catalog an open
95
+ * in progress read found each object, as a table or an index as the statement names it; `verify`, the same check
96
+ * against another catalog; and `seeds`, whether it seeds rows, which still have to be written. SQLite records no
97
+ * versions, so such a setup ran at every open in a write transaction of its own, which took the database's write lock
98
+ * and, on a server, three round trips (#6560); statements on an existing object of that kind do nothing. The setup runs
99
+ * against a handle that only collects the SQL it would run. Undefined for any other call or statement, outside such an
100
+ * open, and on PostgreSQL, whose ledger decides.
101
+ */
102
+ async function foundInCatalog(
103
+ database: SqliteDatabase,
104
+ setup: (tx: SqliteExecutor) => Promise<unknown>,
105
+ ): Promise<
106
+ | {
107
+ readonly verify: (catalog: storage.StorageSchemaCatalog) => boolean;
108
+ readonly seeds: boolean;
109
+ readonly present: boolean;
110
+ }
111
+ | undefined
112
+ > {
113
+ if (dialectOf(database).versionedMigrations) return undefined;
114
+ const catalog = await openCatalogOf(database);
115
+ if (catalog?.sqliteObjectType === undefined) return undefined;
116
+ const scripts: string[] = [];
117
+ let other = false;
118
+ const refuse = () => {
119
+ other = true;
120
+ return Promise.reject(new Error("not a schema statement"));
121
+ };
122
+ const collecting: SqliteExecutor = {
123
+ exec: (sql) => {
124
+ scripts.push(sql);
125
+ return Promise.resolve();
126
+ },
127
+ run: refuse,
128
+ get: refuse,
129
+ all: refuse,
130
+ };
131
+ try {
132
+ await setup(collecting);
133
+ } catch {
134
+ return undefined;
135
+ }
136
+ if (other) return undefined;
137
+ const created: { readonly name: string; readonly type: string }[] = [];
138
+ let seeds = false;
139
+ for (const script of scripts) {
140
+ const statements = splitStatements(script);
141
+ if (statements === undefined) return undefined;
142
+ for (const statement of statements) {
143
+ const create = CREATE_IF_NOT_EXISTS.exec(statement);
144
+ if (create !== null) created.push({ name: create[3] ?? create[4]!, type: create[2]!.toLowerCase() });
145
+ else if (SEED.test(statement)) seeds = true;
146
+ else return undefined;
147
+ }
148
+ }
149
+ const verify = (found: storage.StorageSchemaCatalog) =>
150
+ created.every(({ name, type }) => found.sqliteObjectType?.(name) === type);
151
+ return { verify, seeds, present: verify(catalog) };
152
+ }
153
+
154
+ /** `INSERT INTO … ON CONFLICT … DO NOTHING`: rows a setup seeds once, which writing again leaves as they are. */
155
+ const SEED = /^INSERT\s+INTO\s[\s\S]*\sON\s+CONFLICT\b[\s\S]*\sDO\s+NOTHING$/i;
156
+
157
+ /** `CREATE [UNIQUE] TABLE|INDEX IF NOT EXISTS <name>`, the name plain or double-quoted and never schema-qualified. */
158
+ const CREATE_IF_NOT_EXISTS =
159
+ /^CREATE\s+(UNIQUE\s+)?(TABLE|INDEX)\s+IF\s+NOT\s+EXISTS\s+(?:"([^"]+)"|([A-Za-z_][A-Za-z0-9_]*))(?![A-Za-z0-9_$."])/i;
160
+
161
+ /**
162
+ * The statements of `script`, each trimmed, split at the semicolons outside quotes, brackets, parentheses and comments;
163
+ * undefined when a quote or comment does not end, or a statement starts with a comment.
164
+ */
165
+ function splitStatements(script: string): string[] | undefined {
166
+ const statements: string[] = [];
167
+ let start = 0;
168
+ let depth = 0;
169
+ for (let at = 0; at < script.length; at++) {
170
+ const char = script[at]!;
171
+ const close = char === "'" || char === '"' || char === "`" ? char : char === "[" ? "]" : undefined;
172
+ if (close !== undefined) {
173
+ const end = script.indexOf(close, at + 1);
174
+ if (end < 0) return undefined;
175
+ at = end;
176
+ } else if (char === "-" && script[at + 1] === "-") {
177
+ const end = script.indexOf("\n", at);
178
+ at = end < 0 ? script.length : end;
179
+ } else if (char === "/" && script[at + 1] === "*") {
180
+ const end = script.indexOf("*/", at + 2);
181
+ if (end < 0) return undefined;
182
+ at = end + 1;
183
+ } else if (char === "(") depth++;
184
+ else if (char === ")") depth--;
185
+ else if (char === ";" && depth === 0) {
186
+ statements.push(script.slice(start, at));
187
+ start = at + 1;
188
+ }
189
+ }
190
+ statements.push(script.slice(start));
191
+ const trimmed = statements.map((statement) => statement.trim()).filter((statement) => statement.length > 0);
192
+ return trimmed.some((statement) => statement.startsWith("--") || statement.startsWith("/*")) ? undefined : trimmed;
193
+ }
194
+
195
+ /**
196
+ * Whether the catalog an open in progress read found `table` with every one of `columns`, so adding them would change
197
+ * nothing (#6560). False outside such an open.
198
+ */
199
+ export async function catalogHasColumns(
200
+ database: SqliteDatabase,
201
+ table: string,
202
+ columns: readonly string[],
203
+ ): Promise<boolean> {
204
+ const catalog = await openCatalogOf(database);
205
+ return catalog !== undefined && columns.every((column) => catalog.hasColumn(table, column));
206
+ }
207
+
208
+ /**
209
+ * The schema catalog an open of `database` in progress read (`withStorageSchemaCatalog`), undefined outside one. Its
210
+ * answers describe the schema when the open began.
211
+ */
212
+ export async function openCatalogOf(database: SqliteDatabase): Promise<storage.StorageSchemaCatalog | undefined> {
213
+ return catalogApi.schemaCatalogOf?.(database);
21
214
  }