@bastani/atomic 0.10.10 → 0.10.11-alpha.1

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 (380) hide show
  1. package/CHANGELOG.md +83 -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 +8 -10
  7. package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js.map +1 -1
  8. package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +6 -6
  9. package/dist/_shared/mode-shared-2QJSCUM6.js +52 -15
  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 +3 -3
  13. package/dist/_shared/{sdk-RRLVN7ER.js → sdk-AVFII7NK.js} +757 -233
  14. package/dist/_shared/sdk-AVFII7NK.js.map +1 -0
  15. package/dist/_shared/session-data-AHTU7IS6.js +3 -3
  16. package/dist/_shared/session-manager-list-2BLEEZVG.js +1 -1
  17. package/dist/_shared/{startup-0-0-4VG2G44V.js → startup-0-0-57KF6LZH.js} +94 -42
  18. package/dist/_shared/startup-0-0-57KF6LZH.js.map +1 -0
  19. package/dist/_shared/{startup-0-1-XTIL65D4.js → startup-0-1-GIY75K46.js} +75 -13
  20. package/dist/_shared/startup-0-1-GIY75K46.js.map +1 -0
  21. package/dist/_shared/startup-0-2-27OWFHV6.js +72 -7
  22. package/dist/_shared/startup-0-2-27OWFHV6.js.map +1 -1
  23. package/dist/_shared/startup-0-3-T4DVFYWX.js +4 -4
  24. package/dist/_shared/startup-0-4-ZMJV547D.js +4 -4
  25. package/dist/_shared/{startup-0-5-FD2P735I.js → startup-0-5-YDW47QX6.js} +27 -69
  26. package/dist/_shared/startup-0-5-YDW47QX6.js.map +1 -0
  27. package/dist/_shared/startup-10-0-TDQNBDDL.js +6 -6
  28. package/dist/_shared/startup-12-0-55AAXBJZ.js +3 -3
  29. package/dist/_shared/startup-14-0-MLX36RBG.js +4 -4
  30. package/dist/_shared/startup-15-0-YUKGAC63.js +31 -7
  31. package/dist/_shared/startup-15-0-YUKGAC63.js.map +1 -1
  32. package/dist/_shared/startup-15-1-4S7FLMNG.js +7 -6
  33. package/dist/_shared/startup-15-1-4S7FLMNG.js.map +1 -1
  34. package/dist/_shared/startup-8-0-34CBHPZH.js +4 -4
  35. package/dist/_shared/{startup-8-1-4KANEHRA.js → startup-8-1-X32CYSXB.js} +1219 -714
  36. package/dist/_shared/startup-8-1-X32CYSXB.js.map +1 -0
  37. package/dist/_shared/storage-command-7MGXLJGZ.js +14 -8
  38. package/dist/_shared/storage-command-7MGXLJGZ.js.map +1 -1
  39. package/dist/_shared/task-reference-stream-JWMUZSJX.js +1 -1
  40. package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +2 -2
  41. package/dist/_shared/tree-outline-MUEPK5B7.js +2 -2
  42. package/dist/builtin/intercom/CHANGELOG.md +18 -0
  43. package/dist/builtin/intercom/broker/broker.bundle.mjs +993 -362
  44. package/dist/builtin/intercom/broker/database-wake-up-bus.ts +103 -24
  45. package/dist/builtin/intercom/broker/multinode-router.ts +42 -21
  46. package/dist/builtin/intercom/broker/multinode-state-store.ts +22 -1
  47. package/dist/builtin/intercom/broker/open-broker-state.ts +82 -31
  48. package/dist/builtin/intercom/broker/paths.ts +8 -4
  49. package/dist/builtin/intercom/broker/released-asker-remote-replies.ts +18 -6
  50. package/dist/builtin/intercom/broker/sql-add-column.ts +4 -1
  51. package/dist/builtin/intercom/broker/sql-ended-child-queue-store.ts +65 -44
  52. package/dist/builtin/intercom/broker/sql-held-send-store.ts +11 -3
  53. package/dist/builtin/intercom/broker/sql-known-session-store.ts +24 -12
  54. package/dist/builtin/intercom/broker/sql-lease-store.ts +375 -65
  55. package/dist/builtin/intercom/broker/sql-outbox-store.ts +270 -101
  56. package/dist/builtin/intercom/broker/sql-pending-ask-store.ts +20 -9
  57. package/dist/builtin/intercom/broker/sql-released-ask-store.ts +14 -6
  58. package/dist/builtin/intercom/broker/sql-restoring-reservation-store.ts +27 -11
  59. package/dist/builtin/intercom/broker/sql-schema.ts +139 -1
  60. package/dist/builtin/intercom/broker/sql-sent-message-authority.ts +97 -34
  61. package/dist/builtin/intercom/index.bundle.mjs +11 -8
  62. package/dist/builtin/intercom/package.json +1 -1
  63. package/dist/builtin/mcp/index.bundle.mjs +1 -1
  64. package/dist/builtin/mcp/package.json +1 -1
  65. package/dist/builtin/subagents/CHANGELOG.md +12 -0
  66. package/dist/builtin/subagents/package.json +1 -1
  67. package/dist/builtin/subagents/skills/skill-creator/SKILL.md +1 -1
  68. package/dist/builtin/subagents/src/extension/index.bundle.mjs +62 -23
  69. package/dist/builtin/web-access/index.bundle.mjs +1 -1
  70. package/dist/builtin/web-access/package.json +1 -1
  71. package/dist/cli/args.js +2 -2
  72. package/dist/cli/auth-cli.code.js +4 -4
  73. package/dist/cli/auth-cli.impl.js +3 -3
  74. package/dist/cli/auth-cli.js +3 -3
  75. package/dist/cli/blueprint-command.code.js +3 -3
  76. package/dist/cli/blueprint-command.impl.js +3 -3
  77. package/dist/cli/blueprint-command.js +3 -3
  78. package/dist/cli/intercom-command.code.js +2 -2
  79. package/dist/cli/intercom-command.impl.js +3 -3
  80. package/dist/cli/intercom-command.js +3 -3
  81. package/dist/cli/list-models.code.js +2 -2
  82. package/dist/cli/list-models.impl.js +3 -3
  83. package/dist/cli/list-models.js +3 -3
  84. package/dist/cli/session-picker.code.js +1 -1
  85. package/dist/cli/session-picker.impl.js +3 -3
  86. package/dist/cli/session-picker.js +3 -3
  87. package/dist/cli/startup-ui.code.js +1 -1
  88. package/dist/cli/startup-ui.impl.js +3 -3
  89. package/dist/cli/startup-ui.js +3 -3
  90. package/dist/cli/storage-command.impl.js +3 -3
  91. package/dist/cli/storage-command.js +3 -3
  92. package/dist/cli/storage-search-session.code.js +3 -3
  93. package/dist/cli/storage-search-session.impl.js +3 -3
  94. package/dist/cli/storage-search-session.js +3 -3
  95. package/dist/cli/team-command.code.js +2 -2
  96. package/dist/cli/team-command.impl.js +3 -3
  97. package/dist/cli/team-command.js +3 -3
  98. package/dist/cli.js +2 -2
  99. package/dist/core/agent-session-methods.d.ts +3 -0
  100. package/dist/core/agent-session-methods.js.map +1 -1
  101. package/dist/core/agent-session.d.ts +1 -0
  102. package/dist/core/compaction/compactor.d.ts +2 -0
  103. package/dist/core/debug.impl.js +3 -3
  104. package/dist/core/debug.js +3 -3
  105. package/dist/core/export-html/card-projection.code.js +1 -1
  106. package/dist/core/export-html/card-projection.impl.js +3 -3
  107. package/dist/core/export-html/card-projection.js +3 -3
  108. package/dist/core/export-html/index.code.js +2 -2
  109. package/dist/core/export-html/index.impl.js +3 -3
  110. package/dist/core/export-html/index.js +3 -3
  111. package/dist/core/export-html/json-spans.impl.js +3 -3
  112. package/dist/core/export-html/json-spans.js +3 -3
  113. package/dist/core/export-html/oversized-render-inputs.code.js +1 -1
  114. package/dist/core/export-html/oversized-render-inputs.impl.js +3 -3
  115. package/dist/core/export-html/oversized-render-inputs.js +3 -3
  116. package/dist/core/export-html/tool-renderer.impl.js +3 -3
  117. package/dist/core/export-html/tool-renderer.js +3 -3
  118. package/dist/core/extensions/jiti-loader.impl.js +3 -3
  119. package/dist/core/extensions/jiti-loader.js +3 -3
  120. package/dist/core/extensions/jiti-static-loader.impl.js +3 -3
  121. package/dist/core/extensions/jiti-static-loader.js +3 -3
  122. package/dist/core/extensions/loader-host-modules.code.js +1 -1
  123. package/dist/core/extensions/loader-host-modules.impl.js +3 -3
  124. package/dist/core/extensions/loader-host-modules.js +3 -3
  125. package/dist/core/extensions/loader-virtual-modules.js +2 -2
  126. package/dist/core/extensions/runner-context.js +3 -3
  127. package/dist/core/extensions/runner-context.js.map +1 -1
  128. package/dist/core/intercom-broker-control-host.code.js +1 -1
  129. package/dist/core/intercom-broker-control-host.impl.js +3 -3
  130. package/dist/core/intercom-broker-control-host.js +3 -3
  131. package/dist/core/intercom-broker-control.code.js +3 -3
  132. package/dist/core/intercom-broker-control.impl.js +3 -3
  133. package/dist/core/intercom-broker-control.js +3 -3
  134. package/dist/core/model-registry.js +1 -1
  135. package/dist/core/model-resolver-defaults.d.ts +3 -2
  136. package/dist/core/package-manager-storage-adapters.code.js +1 -1
  137. package/dist/core/package-manager-storage-adapters.impl.js +3 -3
  138. package/dist/core/package-manager-storage-adapters.js +3 -3
  139. package/dist/core/sdk.code.js +3 -4
  140. package/dist/core/sdk.impl.js +3 -3
  141. package/dist/core/sdk.js +3 -3
  142. package/dist/core/session-manager-core.d.ts +5 -2
  143. package/dist/core/session-manager-core.js +17 -13
  144. package/dist/core/session-manager-core.js.map +1 -1
  145. package/dist/core/session-manager-list.impl.js +3 -3
  146. package/dist/core/session-manager-list.js +3 -3
  147. package/dist/core/session-manager.code.js +1 -1
  148. package/dist/core/session-manager.impl.js +3 -3
  149. package/dist/core/session-manager.js +3 -3
  150. package/dist/core/session-storage.code.js +1 -1
  151. package/dist/core/session-storage.impl.js +3 -3
  152. package/dist/core/session-storage.js +3 -3
  153. package/dist/core/storage/added-exports.d.ts +6 -0
  154. package/dist/core/storage/added-exports.js +16 -0
  155. package/dist/core/storage/added-exports.js.map +1 -0
  156. package/dist/core/storage/database.d.ts +16 -0
  157. package/dist/core/storage/database.js.map +1 -1
  158. package/dist/core/storage/fenced-paths.d.ts +47 -0
  159. package/dist/core/storage/fenced-paths.js +53 -0
  160. package/dist/core/storage/fenced-paths.js.map +1 -1
  161. package/dist/core/storage/file-identity.js +31 -2
  162. package/dist/core/storage/file-identity.js.map +1 -1
  163. package/dist/core/storage/index.d.ts +3 -0
  164. package/dist/core/storage/index.js +5 -2
  165. package/dist/core/storage/index.js.map +1 -1
  166. package/dist/core/storage/migrations.d.ts +22 -0
  167. package/dist/core/storage/migrations.js +55 -0
  168. package/dist/core/storage/migrations.js.map +1 -1
  169. package/dist/core/storage/node-sqlite-thread.js +19 -6
  170. package/dist/core/storage/node-sqlite-thread.js.map +1 -1
  171. package/dist/core/storage/node-sqlite.js +53 -4
  172. package/dist/core/storage/node-sqlite.js.map +1 -1
  173. package/dist/core/storage/offline-snapshot-file.impl.js +3 -3
  174. package/dist/core/storage/offline-snapshot-file.js +3 -3
  175. package/dist/core/storage/prefix.d.ts +3 -2
  176. package/dist/core/storage/prefix.js +18 -2
  177. package/dist/core/storage/prefix.js.map +1 -1
  178. package/dist/core/storage/provider.js +2 -2
  179. package/dist/core/storage/schema-catalog.d.ts +42 -0
  180. package/dist/core/storage/schema-catalog.js +141 -0
  181. package/dist/core/storage/schema-catalog.js.map +1 -0
  182. package/dist/core/storage/selection.js +2 -2
  183. package/dist/core/storage/session/branch-fork-source.code.js +41 -20
  184. package/dist/core/storage/session/branch-fork-source.code.js.map +1 -1
  185. package/dist/core/storage/session/branch-fork-source.impl.js +3 -3
  186. package/dist/core/storage/session/branch-fork-source.js +3 -3
  187. package/dist/core/storage/session/card-view.impl.js +3 -3
  188. package/dist/core/storage/session/card-view.js +3 -3
  189. package/dist/core/storage/session/child-reader.code.js +3 -3
  190. package/dist/core/storage/session/child-reader.impl.js +3 -3
  191. package/dist/core/storage/session/child-reader.js +3 -3
  192. package/dist/core/storage/session/chunked-resume-projection.code.js +1 -1
  193. package/dist/core/storage/session/chunked-resume-projection.impl.js +3 -3
  194. package/dist/core/storage/session/chunked-resume-projection.js +3 -3
  195. package/dist/core/storage/session/expanded-child-details.code.js +3 -3
  196. package/dist/core/storage/session/expanded-child-details.impl.js +3 -3
  197. package/dist/core/storage/session/expanded-child-details.js +3 -3
  198. package/dist/core/storage/session/fork-row-bytes.impl.js +3 -3
  199. package/dist/core/storage/session/fork-row-bytes.js +3 -3
  200. package/dist/core/storage/session/fork-row-index.impl.js +3 -3
  201. package/dist/core/storage/session/fork-row-index.js +3 -3
  202. package/dist/core/storage/session/fork-staging.code.js +176 -36
  203. package/dist/core/storage/session/fork-staging.code.js.map +1 -1
  204. package/dist/core/storage/session/fork-staging.impl.js +3 -3
  205. package/dist/core/storage/session/fork-staging.js +3 -3
  206. package/dist/core/storage/session/fork-window.code.js +3 -3
  207. package/dist/core/storage/session/fork-window.impl.js +3 -3
  208. package/dist/core/storage/session/fork-window.js +3 -3
  209. package/dist/core/storage/session/history-pages.impl.js +3 -3
  210. package/dist/core/storage/session/history-pages.js +3 -3
  211. package/dist/core/storage/session/jsonl-entry-projection.impl.js +3 -3
  212. package/dist/core/storage/session/jsonl-entry-projection.js +3 -3
  213. package/dist/core/storage/session/jsonl-session-store.impl.js +3 -3
  214. package/dist/core/storage/session/jsonl-session-store.js +3 -3
  215. package/dist/core/storage/session/memory-session-store.impl.js +3 -3
  216. package/dist/core/storage/session/memory-session-store.js +3 -3
  217. package/dist/core/storage/session/pinned-branch-resume.code.js +3 -3
  218. package/dist/core/storage/session/pinned-branch-resume.impl.js +3 -3
  219. package/dist/core/storage/session/pinned-branch-resume.js +3 -3
  220. package/dist/core/storage/session/released-history-files.code.js +2 -2
  221. package/dist/core/storage/session/released-history-files.impl.js +3 -3
  222. package/dist/core/storage/session/released-history-files.js +3 -3
  223. package/dist/core/storage/session/resident-fork-rows.impl.js +3 -3
  224. package/dist/core/storage/session/resident-fork-rows.js +3 -3
  225. package/dist/core/storage/session/session-resume.impl.js +3 -3
  226. package/dist/core/storage/session/session-resume.js +3 -3
  227. package/dist/core/storage/session/session-search-index.d.ts +5 -0
  228. package/dist/core/storage/session/snapshot-cut.impl.js +3 -3
  229. package/dist/core/storage/session/snapshot-cut.js +3 -3
  230. package/dist/core/storage/session/snapshot-history.code.js +3 -3
  231. package/dist/core/storage/session/snapshot-history.impl.js +3 -3
  232. package/dist/core/storage/session/snapshot-history.js +3 -3
  233. package/dist/core/storage/session/sql-chat-records.d.ts +15 -0
  234. package/dist/core/storage/session/sql-session-store.code.js +1 -1
  235. package/dist/core/storage/session/sql-session-store.d.ts +102 -5
  236. package/dist/core/storage/session/sql-session-store.impl.js +4 -4
  237. package/dist/core/storage/session/sql-session-store.js +4 -4
  238. package/dist/core/storage/session/tree-outline.impl.js +3 -3
  239. package/dist/core/storage/session/tree-outline.js +3 -3
  240. package/dist/core/storage/session/tree-presentation.code.js +1 -1
  241. package/dist/core/storage/session/tree-presentation.impl.js +3 -3
  242. package/dist/core/storage/session/tree-presentation.js +3 -3
  243. package/dist/core/storage/session/types.d.ts +14 -0
  244. package/dist/core/storage/sqlite-facade.d.ts +37 -5
  245. package/dist/core/storage/sqlite-facade.js +351 -18
  246. package/dist/core/storage/sqlite-facade.js.map +1 -1
  247. package/dist/core/storage/statement-sequence.d.ts +83 -0
  248. package/dist/core/storage/statement-sequence.js +21 -0
  249. package/dist/core/storage/statement-sequence.js.map +1 -1
  250. package/dist/core/storage/storage-status.code.js +1 -1
  251. package/dist/core/storage/storage-status.impl.js +3 -3
  252. package/dist/core/storage/storage-status.js +3 -3
  253. package/dist/core/storage/team.code.js +1 -1
  254. package/dist/core/storage/team.impl.js +3 -3
  255. package/dist/core/storage/team.js +3 -3
  256. package/dist/core/structured-output/index.code.js +1 -1
  257. package/dist/core/structured-output/index.impl.js +3 -3
  258. package/dist/core/structured-output/index.js +3 -3
  259. package/dist/core/tasks/task-completion-entry.code.js +1 -1
  260. package/dist/core/tasks/task-completion-entry.impl.js +3 -3
  261. package/dist/core/tasks/task-completion-entry.js +3 -3
  262. package/dist/core/tasks/task-file-history.code.js +3 -3
  263. package/dist/core/tasks/task-file-history.impl.js +3 -3
  264. package/dist/core/tasks/task-file-history.js +3 -3
  265. package/dist/core/tasks/task-reference-archive.code.js +4 -4
  266. package/dist/core/tasks/task-reference-archive.js +3 -3
  267. package/dist/core/tasks/task-reference-identity.js +3 -3
  268. package/dist/core/tasks/task-reference-stream.impl.js +3 -3
  269. package/dist/core/tasks/task-reference-stream.js +3 -3
  270. package/dist/core/tasks/task-unsaved-answer.impl.js +3 -3
  271. package/dist/core/tasks/task-unsaved-answer.js +3 -3
  272. package/dist/core/tasks/task-view-source.code.js +1 -1
  273. package/dist/core/tasks/task-view-source.impl.js +3 -3
  274. package/dist/core/tasks/task-view-source.js +3 -3
  275. package/dist/core/tools/bash.d.ts +8 -4
  276. package/dist/core/tools/history-session.d.ts +5 -0
  277. package/dist/core/tools/history-tasks.d.ts +123 -0
  278. package/dist/core/tools/kill.d.ts +5 -2
  279. package/dist/core/tools/powershell.d.ts +2 -2
  280. package/dist/core/tools/read-url.code.js +3 -3
  281. package/dist/core/tools/read-url.impl.js +3 -3
  282. package/dist/core/tools/read-url.js +3 -3
  283. package/dist/core/tools/read.d.ts +12 -2
  284. package/dist/core/tools/resource-selectors.d.ts +2 -0
  285. package/dist/core/tools/search.d.ts +12 -2
  286. package/dist/core/worktree-snapshot/blob-store.d.ts +14 -2
  287. package/dist/core/worktree-snapshot/blob-store.js +69 -16
  288. package/dist/core/worktree-snapshot/blob-store.js.map +1 -1
  289. package/dist/core/worktree-snapshot/errors.code.js +2 -2
  290. package/dist/core/worktree-snapshot/errors.impl.js +3 -3
  291. package/dist/core/worktree-snapshot/errors.js +3 -3
  292. package/dist/core/worktree-snapshot/session-index-reset.impl.js +3 -3
  293. package/dist/core/worktree-snapshot/session-index-reset.js +3 -3
  294. package/dist/extensions/codemode/execute.code.js +2 -2
  295. package/dist/extensions/codemode/execute.impl.js +3 -3
  296. package/dist/extensions/codemode/execute.js +3 -3
  297. package/dist/extensions/codemode/renderer.code.js +2 -2
  298. package/dist/extensions/codemode/renderer.impl.js +3 -3
  299. package/dist/extensions/codemode/renderer.js +3 -3
  300. package/dist/extensions/mcp/cli.code.js +3 -3
  301. package/dist/extensions/mcp/cli.impl.js +3 -3
  302. package/dist/extensions/mcp/cli.js +3 -3
  303. package/dist/extensions/mcp/cli.lazy.code.js +2 -2
  304. package/dist/extensions/mcp/cli.lazy.impl.js +3 -3
  305. package/dist/extensions/mcp/cli.lazy.js +3 -3
  306. package/dist/extensions/mcp/runtime.code.js +1 -1
  307. package/dist/extensions/mcp/runtime.impl.js +3 -3
  308. package/dist/extensions/mcp/runtime.js +3 -3
  309. package/dist/index.js +5 -5
  310. package/dist/main-first-time-setup.code.js +1 -1
  311. package/dist/main-first-time-setup.impl.js +3 -3
  312. package/dist/main-first-time-setup.js +3 -3
  313. package/dist/main.js +1 -1
  314. package/dist/modes/interactive/components/agent-panel.d.ts +1 -0
  315. package/dist/modes/interactive/components/stored-tree-view.code.js +2 -2
  316. package/dist/modes/interactive/components/stored-tree-view.impl.js +3 -3
  317. package/dist/modes/interactive/components/stored-tree-view.js +3 -3
  318. package/dist/modes/interactive/components/task-inspector.d.ts +2 -0
  319. package/dist/modes/interactive/components/user-message-selector-archive.code.js +1 -1
  320. package/dist/modes/interactive/components/user-message-selector-archive.impl.js +3 -3
  321. package/dist/modes/interactive/components/user-message-selector-archive.js +3 -3
  322. package/dist/modes/interactive/interactive-mode-helpers.d.ts +2 -2
  323. package/dist/modes/print-mode.js +1 -1
  324. package/dist/modes/rpc/agent-panel-bridge.d.ts +5 -0
  325. package/dist/package-manager-cli.code.js +6 -6
  326. package/dist/package-manager-cli.impl.js +3 -3
  327. package/dist/package-manager-cli.js +3 -3
  328. package/dist/rpc-entry.js +1 -1
  329. package/dist/startup-files.json +1 -1
  330. package/dist/startup-stages/0.js +1 -1
  331. package/dist/startup-stages/1.js +1 -1
  332. package/dist/startup-stages/10.js +1 -1
  333. package/dist/startup-stages/12.js +1 -1
  334. package/dist/startup-stages/2.js +8 -1
  335. package/dist/startup-stages/3.js +1 -6
  336. package/dist/startup-stages/5.js +1 -1
  337. package/dist/startup-stages/7.js +1 -5
  338. package/dist/utils/startup-database-spare.code.js +2 -2
  339. package/dist/utils/startup-database-spare.impl.js +3 -3
  340. package/dist/utils/startup-database-spare.js +3 -3
  341. package/dist/utils/tools-manager.code.js +1 -1
  342. package/dist/utils/tools-manager.impl.js +3 -3
  343. package/dist/utils/tools-manager.js +3 -3
  344. package/docs/background-tasks.md +123 -23
  345. package/docs/compaction.md +18 -0
  346. package/docs/docs.json +2 -0
  347. package/docs/environment-variables.md +1 -1
  348. package/docs/getting-started/installation.md +9 -2
  349. package/docs/guides/postgres-pgbouncer.md +339 -0
  350. package/docs/guides/postgres-tuning.md +311 -0
  351. package/docs/guides/resume-on-any-machine.md +2 -2
  352. package/docs/guides.md +2 -0
  353. package/docs/index.md +1 -1
  354. package/docs/libraries/agent-core.md +1 -1
  355. package/docs/libraries/ai.md +2 -2
  356. package/docs/libraries/codemode.md +1 -1
  357. package/docs/libraries/storage-libsql.md +6 -1
  358. package/docs/libraries/storage-postgres.md +11 -6
  359. package/docs/libraries/tui.md +1 -1
  360. package/docs/libraries.md +2 -2
  361. package/docs/programmatic.md +1 -1
  362. package/docs/quickstart.md +1 -1
  363. package/docs/sdk/storage.md +1 -1
  364. package/docs/sdk/web-server.md +1 -3
  365. package/docs/sdk.md +1 -7
  366. package/docs/sessions.md +58 -2
  367. package/docs/settings.md +20 -1
  368. package/docs/skills/authoring.md +1 -1
  369. package/docs/skills.md +79 -6
  370. package/docs/storage.md +262 -12
  371. package/docs/subagents/reference.md +2 -2
  372. package/docs/subagents.md +4 -3
  373. package/docs/tools.md +44 -10
  374. package/docs/windows.md +1 -1
  375. package/package.json +9 -9
  376. package/dist/_shared/sdk-RRLVN7ER.js.map +0 -1
  377. package/dist/_shared/startup-0-0-4VG2G44V.js.map +0 -1
  378. package/dist/_shared/startup-0-1-XTIL65D4.js.map +0 -1
  379. package/dist/_shared/startup-0-5-FD2P735I.js.map +0 -1
  380. package/dist/_shared/startup-8-1-4KANEHRA.js.map +0 -1
@@ -0,0 +1,339 @@
1
+ ---
2
+ title: "Run many agents on PostgreSQL with PgBouncer"
3
+ sidebarTitle: "PostgreSQL with PgBouncer"
4
+ description: "Put PgBouncer between Atomic and PostgreSQL when your agents need more connections than the server allows, with a complete configuration, checks, and fixes for common errors."
5
+ ---
6
+
7
+ PgBouncer is a small server that sits between Atomic and PostgreSQL. Thousands of connections from Atomic go to PgBouncer, and PgBouncer runs their work over a few dozen connections to PostgreSQL. Use it when your agents need more connections than PostgreSQL allows.
8
+
9
+ This guide assumes you already set up the database and roles as in [PostgreSQL storage](/storage#postgresql). It uses these names, which you replace with your own:
10
+
11
+ | Name | Meaning |
12
+ | --- | --- |
13
+ | `db.internal` | The PostgreSQL server's host name |
14
+ | `pgbouncer.internal` | The PgBouncer server's host name |
15
+ | `atomic` | The database Atomic uses |
16
+ | `atomic_runtime` | The role Atomic logs in as |
17
+ | `pgbouncer_auth` | A role PgBouncer uses only to look up passwords |
18
+
19
+ ## When you need PgBouncer
20
+
21
+ Every connection an Atomic process opens counts against PostgreSQL's `max_connections`. [Count the connections Atomic needs](/storage#count-the-connections-atomic-needs) with this rule:
22
+
23
+ - An agent process uses up to `maxConnections` plus `readerMaxConnections`.
24
+ - An Intercom broker uses the same, plus 1 for wake-ups.
25
+
26
+ Add these up over every machine. If the total is more than the server has to share, even with `maxConnections` at 2, you need PgBouncer.
27
+
28
+ For example, 200 CI runners each run one agent process and one Intercom broker. That is 400 processes, of which 200 are brokers. The server has `max_connections = 300`, 3 slots reserved for administrators, and 10 connections used by other clients, so 287 are left for Atomic. With the smallest settings, `maxConnections: 1` and `readerMaxConnections: 0`, the processes need 400 × 1 + 200 = 600 connections. That is twice what the server has.
29
+
30
+ With PgBouncer, those 400 processes connect to PgBouncer instead. PgBouncer shares 80 connections to PostgreSQL between them, and the 200 brokers keep one direct connection each for wake-ups. That fits in 287.
31
+
32
+ ## How Atomic works with PgBouncer
33
+
34
+ **Transaction mode.** Run PgBouncer in transaction mode. In this mode, PgBouncer lends a connection to PostgreSQL to a client for one transaction, then gives it to the next client. Atomic works this way:
35
+
36
+ - Settings that Atomic changes, such as the schema it uses and its time limits, last only for the transaction that set them.
37
+ - Locks that Atomic takes are released when the transaction ends.
38
+ - A statement outside a transaction is a transaction of its own.
39
+
40
+ So nothing Atomic does depends on keeping the same connection to PostgreSQL between transactions. Do not use statement mode, which breaks transactions.
41
+
42
+ **Wake-ups need a direct connection.** An Intercom broker learns about new messages through PostgreSQL's `LISTEN` command. `LISTEN` only works on a connection that stays with the broker, and transaction mode does not keep one. So point the `listenUrl` setting straight at PostgreSQL. Each broker then holds one direct connection for wake-ups, and all other work goes through PgBouncer.
43
+
44
+ If you leave out `listenUrl`, Atomic notices that it is behind a pooler and stops waiting for wake-ups. Brokers then check the database for new messages every half second instead of every 5 seconds. Messages still arrive, a little later, and the database gets more queries.
45
+
46
+ **Prepared statements.** A prepared statement is a query that PostgreSQL parses and plans once, then runs many times. Atomic can keep its frequent queries prepared, which saves the server work, but only if the pooler keeps track of them. The Atomic setting `maxPreparedStatements` controls this:
47
+
48
+ - By default, Atomic turns prepared statements off when `listenUrl` is set or the URL's port is 6432 or 6543. Most setups need nothing more.
49
+ - To use them through PgBouncer, you need PgBouncer 1.21 or later with `max_prepared_statements` above `0`. PgBouncer 1.24 and later set it to 200 by default. Then set `maxPreparedStatements` in Atomic to `256`.
50
+
51
+ ```text
52
+ agent processes ──> PgBouncer :6432 ──> PostgreSQL :5432
53
+ Intercom brokers ──> PgBouncer :6432 ──> PostgreSQL :5432
54
+ └── wake-ups (listenUrl) ─────> PostgreSQL :5432
55
+ ```
56
+
57
+ ## Set up PostgreSQL
58
+
59
+ ### Connection limits
60
+
61
+ PostgreSQL needs room for:
62
+
63
+ - PgBouncer's connections: its pool size plus its reserve pool, `default_pool_size` plus `reserve_pool_size` in the configuration below.
64
+ - One wake-up connection per Intercom broker.
65
+ - Administrators, monitoring, and other applications.
66
+
67
+ Set `max_connections` to at least their sum. Changing it needs a restart of PostgreSQL. On a managed service, change the server parameter in the service's console.
68
+
69
+ To stop Atomic's direct wake-up connections from taking slots that PgBouncer needs, you can cap the runtime role. Set the cap to the number of brokers plus a margin:
70
+
71
+ ```sql
72
+ ALTER ROLE atomic_runtime CONNECTION LIMIT 220;
73
+ ```
74
+
75
+ The cap counts every connection the role opens, including PgBouncer's connections for that role. Raise it if PgBouncer logs `too many connections for role "atomic_runtime"`.
76
+
77
+ ### A role for password lookups
78
+
79
+ PgBouncer checks each client's password against the one PostgreSQL stores, so you do not keep a second copy of every password. It runs a lookup query as a role that can do nothing else. As a superuser, connected to the `atomic` database, run:
80
+
81
+ ```sql
82
+ CREATE ROLE pgbouncer_auth LOGIN PASSWORD '<auth role password>';
83
+ CREATE SCHEMA pgbouncer;
84
+ GRANT USAGE ON SCHEMA pgbouncer TO pgbouncer_auth;
85
+ CREATE FUNCTION pgbouncer.get_auth(p_usename text)
86
+ RETURNS TABLE(usename text, passwd text)
87
+ LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog
88
+ AS $$
89
+ SELECT rolname::text, rolpassword FROM pg_catalog.pg_authid
90
+ WHERE rolname = p_usename
91
+ AND rolcanlogin AND NOT rolsuper
92
+ AND rolname = ANY (ARRAY['atomic_runtime', 'atomic_owner'])
93
+ AND (rolvaliduntil IS NULL OR rolvaliduntil > now())
94
+ $$;
95
+ REVOKE ALL ON FUNCTION pgbouncer.get_auth(text) FROM PUBLIC;
96
+ GRANT EXECUTE ON FUNCTION pgbouncer.get_auth(text) TO pgbouncer_auth;
97
+ ```
98
+
99
+ The function runs with the rights of the superuser who created it, so `pgbouncer_auth` can read one password at a time without reading the password table itself. It returns only the roles you list, never a superuser, so no one can log in through PgBouncer as any other role. List every role your Atomic machines log in as. Declare the result with `RETURNS TABLE`, as above: PgBouncer refuses a lookup that returns a row of empty values, and logs `auth_query response contained null user name`.
100
+
101
+ When you change a role's password with `ALTER ROLE`, PgBouncer uses the new one at once. You do not need to reload it.
102
+
103
+ Passwords must use SCRAM, PostgreSQL's default since version 14. Check with `SHOW password_encoding;`, which should say `scram-sha-256`.
104
+
105
+ ### Who may connect
106
+
107
+ PostgreSQL's `pg_hba.conf` file decides which hosts may log in. Allow PgBouncer's address, and the machines that run Intercom brokers for their wake-up connections, over TLS:
108
+
109
+ ```text
110
+ # TYPE DATABASE USER ADDRESS METHOD
111
+ hostssl atomic atomic_runtime 10.0.1.5/32 scram-sha-256
112
+ hostssl atomic pgbouncer_auth 10.0.1.5/32 scram-sha-256
113
+ hostssl atomic atomic_runtime 10.0.2.0/24 scram-sha-256
114
+ ```
115
+
116
+ Here `10.0.1.5` is PgBouncer and `10.0.2.0/24` is the network of the agent machines. Reload PostgreSQL after editing the file, for example with `SELECT pg_reload_conf();`.
117
+
118
+ ## Configure PgBouncer
119
+
120
+ This configuration was tested with PgBouncer 1.26.1 in Docker, and a configuration like it with PgBouncer 1.25.1 on Ubuntu in front of PostgreSQL 18. Save it as `/etc/pgbouncer/pgbouncer.ini`:
121
+
122
+ ```ini
123
+ ;; Which databases PgBouncer offers, and where each one really is.
124
+ ;; Atomic connects to "atomic" on PgBouncer, which forwards to PostgreSQL.
125
+ [databases]
126
+ atomic = host=db.internal port=5432 dbname=atomic
127
+
128
+ [pgbouncer]
129
+ listen_addr = 0.0.0.0
130
+ listen_port = 6432
131
+
132
+ ;; Lend a PostgreSQL connection to a client for one transaction at a time.
133
+ pool_mode = transaction
134
+
135
+ ;; Clients log in with their own PostgreSQL role and password, checked with SCRAM.
136
+ ;; PgBouncer looks up the stored password through pgbouncer.get_auth, logged in as pgbouncer_auth.
137
+ auth_type = scram-sha-256
138
+ auth_user = pgbouncer_auth
139
+ auth_query = SELECT usename, passwd FROM pgbouncer.get_auth($1)
140
+ ;; Holds only pgbouncer_auth's own password.
141
+ auth_file = /etc/pgbouncer/userlist.txt
142
+
143
+ ;; TLS between Atomic and PgBouncer. PgBouncer presents this certificate.
144
+ client_tls_sslmode = require
145
+ client_tls_cert_file = /etc/pgbouncer/tls/pgbouncer.crt
146
+ client_tls_key_file = /etc/pgbouncer/tls/pgbouncer.key
147
+
148
+ ;; TLS between PgBouncer and PostgreSQL. verify-full checks the server's certificate and host name.
149
+ server_tls_sslmode = verify-full
150
+ server_tls_ca_file = /etc/pgbouncer/tls/root.crt
151
+
152
+ ;; How many clients may connect to PgBouncer. Count every Atomic process's pools:
153
+ ;; processes x (maxConnections + readerMaxConnections), with room to spare.
154
+ max_client_conn = 5000
155
+
156
+ ;; How many connections to PostgreSQL each database and role pair may use.
157
+ default_pool_size = 80
158
+ ;; Extra connections lent when a client has waited reserve_pool_timeout seconds.
159
+ reserve_pool_size = 5
160
+ reserve_pool_timeout = 3
161
+ ;; A hard cap on all connections to the atomic database.
162
+ max_db_connections = 85
163
+
164
+ ;; Remember up to this many prepared statements per PostgreSQL connection.
165
+ ;; Atomic uses them only when its maxPreparedStatements setting is above 0.
166
+ max_prepared_statements = 200
167
+
168
+ ;; Close a PostgreSQL connection unused for 10 minutes, and replace any connection after 1 hour.
169
+ server_idle_timeout = 600
170
+ server_lifetime = 3600
171
+
172
+ ;; Give up on a client that waited 2 minutes for a PostgreSQL connection.
173
+ query_wait_timeout = 120
174
+
175
+ ;; Roles that may run SHOW commands on PgBouncer's console.
176
+ stats_users = pgbouncer_auth
177
+ ```
178
+
179
+ Save the lookup role's password as `/etc/pgbouncer/userlist.txt`, readable only by the user PgBouncer runs as:
180
+
181
+ ```text
182
+ "pgbouncer_auth" "<auth role password>"
183
+ ```
184
+
185
+ PgBouncer needs this password in readable form, because it logs in to PostgreSQL with it. Every other role's password stays only in PostgreSQL.
186
+
187
+ ### Choose the pool sizes
188
+
189
+ - **`default_pool_size`**: how many connections to PostgreSQL PgBouncer opens for Atomic's role. Start from what PostgreSQL can spare: `max_connections`, minus the reserved slots, minus one per Intercom broker for wake-ups, minus other clients, minus `reserve_pool_size`. More is not always faster. The [PostgreSQL wiki](https://wiki.postgresql.org/wiki/Number_Of_Database_Connections) suggests about twice the number of CPU cores as a starting point, so about 32 on a 16-core server. Raise it while `cl_waiting` in `SHOW POOLS` stays above `0` and the server's CPU has room.
190
+ - **`reserve_pool_size`**: a few extra connections for bursts. PgBouncer lends them only after a client has waited `reserve_pool_timeout` seconds.
191
+ - **`max_db_connections`**: a cap on all of PgBouncer's connections to the database. Set it to `default_pool_size` plus `reserve_pool_size`.
192
+ - **`max_client_conn`**: how many connections PgBouncer accepts from clients. Connections to PgBouncer are cheap, so set it well above the number of Atomic processes times their pools. Each client connection uses a file descriptor, so raise the PgBouncer server's open-file limit (`ulimit -n`, or `LimitNOFILE` in a systemd unit) above `max_client_conn`.
193
+
194
+ - **Lock waits hold connections.** In transaction mode, a transaction that waits for a lock keeps its PostgreSQL connection while it waits. When many agents send Intercom messages at once over a slow link, those waits can fill the pool, and every other client queues behind them. If `cl_waiting` climbs while `sv_active` sits at the cap, raise `default_pool_size` and `max_db_connections`, as far as `max_connections` allows.
195
+
196
+ With PgBouncer in front, keep Atomic's `maxConnections` at its default of 8 or higher. Atomic's connections only reach PgBouncer, which queues them for its pool.
197
+
198
+ ### Start PgBouncer
199
+
200
+ Start PgBouncer with the configuration file, for example `pgbouncer -d /etc/pgbouncer/pgbouncer.ini`, or through your distribution's service, `systemctl restart pgbouncer`. After a change to the file, reload it from PgBouncer's console with `RELOAD;`.
201
+
202
+ The Ubuntu and Debian service starts PgBouncer with a limit of 1024 open files, which is too low for `max_client_conn = 5000`. PgBouncer logs `kernel file descriptor limit: 1024` at start. Raise the limit with a systemd drop-in, then restart:
203
+
204
+ ```ini
205
+ # /etc/systemd/system/pgbouncer.service.d/limits.conf
206
+ [Service]
207
+ LimitNOFILE=16384
208
+ ```
209
+
210
+ Run `systemctl daemon-reload` and `systemctl restart pgbouncer` after creating the file.
211
+
212
+ If PgBouncer runs on the database server and connects to PostgreSQL as `127.0.0.1`, the server's certificate does not name that address. Use `server_tls_sslmode = verify-ca` there, which checks the certificate authority without the host name, or connect by the name the certificate carries.
213
+
214
+ ## Configure Atomic
215
+
216
+ Point `url` at PgBouncer and `listenUrl` at PostgreSQL. Both use the runtime role and the same password. In `~/.atomic/agent/settings.json`:
217
+
218
+ ```json
219
+ {
220
+ "storage": {
221
+ "adapter": "@bastani/atomic-storage-postgres",
222
+ "options": {
223
+ "url": "postgresql://atomic_runtime@pgbouncer.internal:6432/atomic",
224
+ "listenUrl": "postgresql://atomic_runtime@db.internal:5432/atomic",
225
+ "maxConnections": 8
226
+ },
227
+ "secrets": { "password": { "file": "agent/storage-token.3f2a" } }
228
+ }
229
+ }
230
+ ```
231
+
232
+ The password file is the one `atomic storage join` writes. To create the block, join with the PgBouncer URL, then add `listenUrl` and restart Intercom:
233
+
234
+ ```bash
235
+ atomic storage join 'postgresql://atomic_runtime@pgbouncer.internal:6432/atomic' --password-file ~/atomic-db-password
236
+ atomic intercom restart
237
+ ```
238
+
239
+ Atomic checks the certificate and host name of both PgBouncer and PostgreSQL. If PgBouncer's certificate comes from a private certificate authority, add `?sslrootcert=/path/to/root.crt` to `url`. See [TLS](/storage#tls).
240
+
241
+ The same options in an SDK app:
242
+
243
+ ```typescript
244
+ import { openPostgresDatabase } from "@bastani/atomic-storage-postgres";
245
+
246
+ const database = await openPostgresDatabase({
247
+ url: "postgresql://atomic_runtime@pgbouncer.internal:6432/atomic",
248
+ listenUrl: "postgresql://atomic_runtime@db.internal:5432/atomic",
249
+ password: process.env.ATOMIC_DB_PASSWORD,
250
+ maxConnections: 8,
251
+ });
252
+ ```
253
+
254
+ ## Check that it works
255
+
256
+ **Atomic.** Run `atomic storage status` on an agent machine. It should report `wake-ups: LISTEN/NOTIFY`. If it reports `wake-ups: polling only, notifications do not pass the URL's transaction pooler`, `listenUrl` is missing or points at PgBouncer.
257
+
258
+ **PgBouncer.** Connect to PgBouncer's console, a built-in database named `pgbouncer`, as a role listed in `stats_users`:
259
+
260
+ ```bash
261
+ psql -h pgbouncer.internal -p 6432 -U pgbouncer_auth pgbouncer -c 'SHOW POOLS'
262
+ ```
263
+
264
+ `SHOW POOLS` prints one row per database and role. In the row for `atomic` and `atomic_runtime`:
265
+
266
+ | Column | Meaning | What to look for |
267
+ | --- | --- | --- |
268
+ | `cl_active` | Clients connected to PgBouncer | Roughly the number of Atomic connections. |
269
+ | `cl_waiting` | Clients waiting for a PostgreSQL connection | `0` most of the time. A number that stays above `0` means the pool is too small or the server too slow. |
270
+ | `sv_active` | PostgreSQL connections running a transaction | At most `default_pool_size` plus `reserve_pool_size`. |
271
+ | `sv_idle` | PostgreSQL connections ready for the next transaction | Some idle connections are normal. |
272
+ | `maxwait` | How many seconds the oldest waiting client has waited | Below a second. Higher values mean clients queue for the pool. |
273
+ | `pool_mode` | The pool mode | `transaction` |
274
+
275
+ `SHOW CLIENTS` lists each client connection, and `SHOW STATS` shows queries and wait times per database.
276
+
277
+ **PostgreSQL.** As an administrator, count the connections by application and state. PgBouncer's connections report Atomic's application name `atomic`, and each broker's wake-up connection reports `atomic-listen`:
278
+
279
+ ```sql
280
+ SELECT application_name, state, count(*)
281
+ FROM pg_stat_activity
282
+ WHERE datname = 'atomic'
283
+ GROUP BY 1, 2
284
+ ORDER BY 3 DESC;
285
+ ```
286
+
287
+ The `atomic` connections should stay at or below PgBouncer's pool size, and there should be one `atomic-listen` connection per broker.
288
+
289
+ ## Results from a 200-agent test
290
+
291
+ We ran 200 simulated agent runners through PgBouncer 1.25.1 to PostgreSQL 18, with a configuration like the one above: `default_pool_size = 80`, `max_db_connections = 90`, and `max_client_conn = 2000`. Each runner opened two Atomic database handles and one Intercom broker, 10 runners to a process.
292
+
293
+ | Round trip to the server | Pool | Server connections | Waiting clients | Save entries to a session, p50 / p95 | Errors |
294
+ | --- | --- | --- | --- | --- | --- |
295
+ | About 10 ms | 80 | 216 to 218: 200 for wake-ups, 13 to 18 pooled | 0 | 23 ms / 32 ms | 0 of 14,870 calls |
296
+ | About 90 ms | 80 | Pool full at 90 | Up to 661, waiting up to 14 seconds | 8.9 s / 18 s | |
297
+ | About 90 ms | 170 | Up to 170 pooled | Up to 10, no wait | 188 ms / 203 ms | 112 of 10,981 calls, all lock timeouts (`55P03`) on Intercom send, lease, claim and delivery calls |
298
+
299
+ What this shows:
300
+
301
+ - With the database close to the agents, 80 pooled connections serve 200 agents with room to spare. Only 13 to 18 were in use at a time.
302
+ - With the database far away, transactions last longer and lock waits hold connections, so the same pool fills up. A larger pool removed the queue. The remaining errors were lock timeouts between agents sending, claiming and delivering Intercom messages at once, which PgBouncer does not cause.
303
+ - Turning on prepared statements (`maxPreparedStatements: 256`) worked through PgBouncer but did not change these times. Leave it unset unless your own measurements show a gain.
304
+
305
+ ## Common errors
306
+
307
+ Atomic reports these as `StorageUnavailableError` or `StorageTransientError`. The message below is the error's cause, which Atomic prints with the error. The first four were reproduced through PgBouncer 1.26.1 with the configuration above, and seen again with PgBouncer 1.25.1 on a production server. The two PostgreSQL messages were reproduced against PostgreSQL 16. The `bouncer config error` and file-limit rows were seen on that production server.
308
+
309
+ | Message | Cause | Fix |
310
+ | --- | --- | --- |
311
+ | `no more connections allowed (max_client_conn)` | More clients connected to PgBouncer than `max_client_conn` allows. | Raise `max_client_conn`, and the open-file limit with it. |
312
+ | `query_wait_timeout` | A client waited `query_wait_timeout` seconds for a PostgreSQL connection. | Check `cl_waiting` and `maxwait` in `SHOW POOLS`. Raise `default_pool_size` if PostgreSQL has room, or look for long transactions with the queries in [Tuning](/storage#check-the-effect). |
313
+ | `SSL required` | Atomic connected without TLS. Atomic leaves TLS off for addresses on the same machine, such as `localhost`. | Add `?sslmode=verify-full&sslrootcert=/path/to/root.crt` to `url`. |
314
+ | `SASL authentication failed` | The password is wrong, the role does not exist, or the database is not listed in `[databases]`. | Check the password file, the role, and the database name. Check that the lookup function exists in that database. |
315
+ | `wake-ups: polling only, notifications do not pass the URL's transaction pooler` in `atomic storage status` | `listenUrl` is missing or points at PgBouncer. | Set `listenUrl` to PostgreSQL's own address. |
316
+ | `too many connections for role "atomic_runtime"` | The role's `CONNECTION LIMIT` is lower than PgBouncer's pool plus the brokers' wake-up connections. | Raise the limit, or lower `default_pool_size`. |
317
+ | `sorry, too many clients already` | PostgreSQL's `max_connections` is lower than PgBouncer's pool plus the wake-up and other connections. | Raise `max_connections`, or lower `default_pool_size` and `max_db_connections`. |
318
+ | `prepared statement "atomic_…" does not exist` | Atomic's `maxPreparedStatements` is above `0`, but PgBouncer does not keep prepared statements: it is older than 1.21, or `max_prepared_statements` is `0`. | Set `max_prepared_statements` above `0` in PgBouncer, or set `maxPreparedStatements` to `0` in Atomic. |
319
+ | `unsupported startup parameter: <name>` | A client sent a connection setting that PgBouncer does not forward. Atomic sends only `application_name`, which PgBouncer forwards. | Add the name to `ignore_startup_parameters` for other clients that need it, such as `extra_float_digits` for Java clients. Never add `application_name`. |
320
+ | `bouncer config error`, with `auth_query response contained null user name` in PgBouncer's log | The lookup function returned a row of empty values for an unknown role. | Declare the function with `RETURNS TABLE`, as in [A role for password lookups](#a-role-for-password-lookups). |
321
+ | `kernel file descriptor limit: 1024` in PgBouncer's log | The service's open-file limit is below `max_client_conn`. | Raise `LimitNOFILE`, as in [Start PgBouncer](#start-pgbouncer). |
322
+
323
+ ## Managed services
324
+
325
+ These services include a pooler of their own. We have not tested Atomic through them. The notes come from each service's documentation.
326
+
327
+ - **Supabase.** The shared pooler, Supavisor, runs transaction mode on port 6543. Point `url` at port 6543 and `listenUrl` at the direct connection on port 5432. Supavisor does not keep prepared statements, so leave `maxPreparedStatements` at its default, which is `0` on port 6543.
328
+ - **Neon.** The pooled host name ends in `-pooler` and runs PgBouncer in transaction mode. Point `url` at the pooled host and `listenUrl` at the same host name without `-pooler`.
329
+ - **Azure Database for PostgreSQL Flexible Server.** Turn on the built-in PgBouncer with the `pgbouncer.enabled` server parameter, on General Purpose and Memory Optimized tiers. It listens on port 6432 of the same host. Point `listenUrl` at port 5432.
330
+ - **Amazon RDS Proxy.** RDS Proxy does not pass `LISTEN`, so point `listenUrl` at the database instance's own endpoint. RDS Proxy may keep a client on one database connection ("pinning") when it sees session settings. We have not checked whether Atomic's statements cause pinning. Watch the proxy's pinning metrics in CloudWatch.
331
+
332
+ On a managed service where you cannot create a superuser function, use the service's pooler instead of `auth_query`, or list each role's password in PgBouncer's `auth_file`.
333
+
334
+ ## Related pages
335
+
336
+ - [Tuning for many concurrent agents](/storage#tuning-for-many-concurrent-agents): count connections and size `maxConnections`.
337
+ - [Tune PostgreSQL for many agents](/guides/postgres-tuning): the server-side settings that go with PgBouncer.
338
+ - [Connection poolers](/storage#connection-poolers) and [Wake-ups](/storage#wake-ups) in the storage guide.
339
+ - [PgBouncer's configuration reference](https://www.pgbouncer.org/config.html).
@@ -0,0 +1,311 @@
1
+ ---
2
+ title: "Tune PostgreSQL for many agents"
3
+ sidebarTitle: "Tune PostgreSQL"
4
+ description: "Server-side tuning for a PostgreSQL database shared by many Atomic agents: where the time goes, distance, connections, memory, monitoring, busy tables, and what to leave alone."
5
+ ---
6
+
7
+ This guide is for the person who runs the PostgreSQL server that Atomic's agents share. It follows what we did on a production server and measured there. Use it after you set up the database as in [PostgreSQL storage](/storage#postgresql).
8
+
9
+ **The server we tuned:** PostgreSQL 18 on a machine with 8 CPU cores, 30 GB of memory and an NVMe disk. It served up to 200 agents about 80 ms away over the network. Each section says what we measured on that server, what is general advice, and what we have not tried on managed services such as Amazon RDS, Azure or Google Cloud SQL.
10
+
11
+ The table names below assume Atomic's default schema, `atomic`, and no `prefix`. With a prefix, each table name starts with it, for example `atomic.team_session_sessions`.
12
+
13
+ ## Where the time goes
14
+
15
+ For Atomic, a database server is rarely short of CPU or disk. On our server the load average stayed below 1.7 while 200 agents worked, and every query that did real work took under 60 seconds in total over a 13-minute test. The limits are:
16
+
17
+ - **Round trips.** Each save waits for answers from the server. The farther away the server, the longer each wait.
18
+ - **Connections.** Each server has a fixed number. When agents need more than it has, they get errors.
19
+ - **Locks.** Agents that write the same data wait for each other. Before Atomic's own locking was fixed, waiting for locks was 99 to 100% of the time the server spent on Atomic's queries.
20
+
21
+ To tell which one you are hitting, run these as an administrator. Replace `atomic` with your database's name.
22
+
23
+ How many connections are in use, and what they are doing. `active` means running a query, and `idle` means open but unused:
24
+
25
+ ```sql
26
+ SELECT state, count(*)
27
+ FROM pg_stat_activity
28
+ WHERE datname = 'atomic'
29
+ GROUP BY state
30
+ ORDER BY 2 DESC;
31
+ ```
32
+
33
+ What the busy connections are waiting for. A large count with `wait_event_type` `Lock` means lock waits. `Client` with `ClientRead` means the server waits for the agent, which usually means distance:
34
+
35
+ ```sql
36
+ SELECT wait_event_type, wait_event, count(*)
37
+ FROM pg_stat_activity
38
+ WHERE datname = 'atomic' AND state <> 'idle'
39
+ GROUP BY 1, 2
40
+ ORDER BY 3 DESC;
41
+ ```
42
+
43
+ How close the server is to its connection limit:
44
+
45
+ ```sql
46
+ SELECT count(*) AS used, current_setting('max_connections')::int AS max
47
+ FROM pg_stat_activity
48
+ WHERE backend_type = 'client backend';
49
+ ```
50
+
51
+ On the machine itself, `uptime` shows the load average. If it stays well below the number of CPU cores while agents are slow, the server is waiting, not working.
52
+
53
+ ## Put the database close to the agents
54
+
55
+ Saving new entries to a session takes 2 round trips. A round trip is the time a message takes to reach the server and come back, which `ping` shows. So every save takes at least twice that:
56
+
57
+ | Distance to the server | One round trip | Each save, at least |
58
+ | --- | --- | --- |
59
+ | Same data center | about 1 ms | about 2 ms |
60
+ | Same region, different zone | about 2 ms | about 4 ms |
61
+ | Across a continent | about 80 ms | about 160 ms |
62
+
63
+ We measured this on our server. At 80 ms, a save took 164 to 166 ms in the middle of the range (p50) with 25 to 200 agents. At about 10 ms through PgBouncer, a save took 23 ms. Distance also makes every lock wait longer, because a transaction holds its locks for its round trips.
64
+
65
+ Run the server in the same region as the agents, and ideally the same zone. No server setting makes up for distance.
66
+
67
+ ## Connections
68
+
69
+ Each Atomic process opens up to `maxConnections` plus `readerMaxConnections` connections to the server, 10 by default. An Intercom broker process opens one more, for wake-ups. [Count the connections Atomic needs](/storage#count-the-connections-atomic-needs) explains the arithmetic, and [Tuning for many concurrent agents](/storage#tuning-for-many-concurrent-agents) shows how to size `maxConnections`.
70
+
71
+ On the server:
72
+
73
+ - **`max_connections`** is the total the server accepts. Each connection uses memory for its own work, so the server cannot accept unlimited connections. On our server we raised it from 300 to 400, which needs a restart. That leaves room for PgBouncer's pool, one wake-up connection per broker, and administrators.
74
+ - **Per-role limits.** `ALTER ROLE atomic_runtime CONNECTION LIMIT 300;` caps one role, so a runaway fleet cannot take every slot. We raised Atomic's role from 150 to 300, because every broker holds a direct wake-up connection.
75
+ - **PgBouncer.** When agents need more connections than the server can give, put [PgBouncer](/guides/postgres-pgbouncer) in front. With 200 agents and a 300-connection server, we had to.
76
+
77
+ ```sql
78
+ SHOW max_connections;
79
+ ALTER ROLE atomic_runtime CONNECTION LIMIT 300;
80
+ ```
81
+
82
+ ## Memory
83
+
84
+ ### Shared buffers
85
+
86
+ `shared_buffers` is the memory PostgreSQL keeps for table and index pages. A common starting point is a quarter of the machine's memory. Our server has 30 GB and uses `shared_buffers = 8GB`. Atomic's data was small enough that 99.999% of page reads came from memory, so a larger value would not have helped. Changing it needs a restart.
87
+
88
+ ### Huge pages
89
+
90
+ The operating system tracks memory in pages, usually 4 KB each. Every server process that touches shared memory keeps its own table of those pages. With 8 GB of shared buffers and hundreds of connections, those tables alone can take gigabytes. Huge pages are 2 MB each on Linux, so the tables are 512 times smaller.
91
+
92
+ PostgreSQL uses huge pages when `huge_pages` is `try` (the default) or `on`, and the operating system has enough of them reserved. Our server had `huge_pages = try` but none reserved, so `huge_pages_status` was `off`. To fix that on Linux:
93
+
94
+ 1. **Find out how many pages PostgreSQL needs.** Stop the server first: this setting can only be read while it is not running. Run, as the `postgres` user, with your data directory:
95
+
96
+ ```bash
97
+ postgres -D /var/lib/postgresql/18/main -C shared_memory_size_in_huge_pages
98
+ ```
99
+
100
+ On Debian and Ubuntu, `postgres` is in `/usr/lib/postgresql/18/bin/`. Add `-c config_file=/etc/postgresql/18/main/postgresql.conf` so it reads your settings. On our server it printed `4259`.
101
+
102
+ 2. **Reserve a little more than that**, rounded up, and keep it across reboots. We reserved 4350, which leaves about 180 MB spare:
103
+
104
+ ```bash
105
+ echo 'vm.nr_hugepages = 4350' | sudo tee /etc/sysctl.d/60-postgresql-hugepages.conf
106
+ sudo sysctl -p /etc/sysctl.d/60-postgresql-hugepages.conf
107
+ grep HugePages_Total /proc/meminfo
108
+ ```
109
+
110
+ 3. **Start the server and check** that it uses them:
111
+
112
+ ```sql
113
+ SHOW huge_pages_status;
114
+ ```
115
+
116
+ It should say `on`. `huge_pages_status` exists in PostgreSQL 17 and later. On any version, `HugePages_Rsvd` in `/proc/meminfo` should be close to the number PostgreSQL needed.
117
+
118
+ Reserved huge pages are taken out of the memory other programs can use. On our server, available memory went from 30.0 GB to 21.7 GB, because 8.5 GB was now reserved as huge pages for PostgreSQL's shared memory. We did not measure how much page-table memory this saved. To undo it, set `vm.nr_hugepages` back to `0` after stopping the server.
119
+
120
+ Managed services decide huge pages for you. We have not checked how each one sets them.
121
+
122
+ ## See what is slow
123
+
124
+ `pg_stat_statements` is an extension that records how often each query runs and how long it takes in total. It is the only way to see what hundreds of agents actually ask of the server.
125
+
126
+ 1. **Load it at server start.** Add this line to `postgresql.conf`, or to a file in its `conf.d` directory, and restart:
127
+
128
+ ```ini
129
+ shared_preload_libraries = 'pg_stat_statements'
130
+ ```
131
+
132
+ If the line already lists other libraries, add it to the list, separated by a comma.
133
+
134
+ 2. **Create the extension** in Atomic's database, as a superuser. Atomic's owner role cannot: it gets `permission denied to create extension "pg_stat_statements"` with the hint `Must be superuser to create this extension`.
135
+
136
+ ```sql
137
+ CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
138
+ ```
139
+
140
+ 3. **Let your monitoring role see every query.** Without this, a role sees the text only of its own queries:
141
+
142
+ ```sql
143
+ GRANT pg_read_all_stats TO atomic_owner;
144
+ ```
145
+
146
+ Queries to run, all in the `atomic` database:
147
+
148
+ The queries that take the most total time. This is where to look first:
149
+
150
+ ```sql
151
+ SELECT calls, round(total_exec_time) AS total_ms, round(mean_exec_time::numeric, 2) AS mean_ms,
152
+ left(query, 100) AS query
153
+ FROM pg_stat_statements
154
+ ORDER BY total_exec_time DESC
155
+ LIMIT 10;
156
+ ```
157
+
158
+ The slowest queries on average:
159
+
160
+ ```sql
161
+ SELECT calls, round(mean_exec_time::numeric, 2) AS mean_ms, round(max_exec_time::numeric, 2) AS max_ms,
162
+ left(query, 100) AS query
163
+ FROM pg_stat_statements
164
+ WHERE calls > 50
165
+ ORDER BY mean_exec_time DESC
166
+ LIMIT 10;
167
+ ```
168
+
169
+ How much of the time is lock waits. Atomic takes its locks with `pg_advisory_xact_lock`, so a large share here means agents wait for each other, not for the server:
170
+
171
+ ```sql
172
+ SELECT round((100 * sum(total_exec_time) FILTER (WHERE query LIKE '%pg_advisory%')
173
+ / nullif(sum(total_exec_time), 0))::numeric, 1) AS lock_share_percent
174
+ FROM pg_stat_statements;
175
+ ```
176
+
177
+ To start a new measurement, clear the counters:
178
+
179
+ ```sql
180
+ SELECT pg_stat_statements_reset();
181
+ ```
182
+
183
+ ## Busy small tables
184
+
185
+ A few of Atomic's tables are small but change constantly:
186
+
187
+ | Table | What changes it |
188
+ | --- | --- |
189
+ | `atomic.session_sessions` | One row per session, updated on every save |
190
+ | `atomic.intercom_leases` | Leases that Intercom brokers renew every few seconds |
191
+ | `atomic.snapshot_session_links` | Links between sessions and worktree snapshots, updated when sessions change |
192
+
193
+ ### Clean them up sooner
194
+
195
+ PostgreSQL never changes a row in place. An update writes a new version of the row and leaves the old one behind as a dead row. A background process, autovacuum, later frees the dead rows. By default it waits until a fifth of a table is dead, which on a small table that changes constantly can mean many dead rows between cleanups, and queries that read past them.
196
+
197
+ Tell autovacuum to clean these tables after a fixed number of dead rows instead. This takes effect at once, with no restart. These are the settings we use:
198
+
199
+ ```sql
200
+ ALTER TABLE atomic.session_sessions SET (
201
+ autovacuum_vacuum_scale_factor = 0, autovacuum_vacuum_threshold = 500,
202
+ autovacuum_analyze_scale_factor = 0, autovacuum_analyze_threshold = 500);
203
+ ALTER TABLE atomic.intercom_leases SET (
204
+ autovacuum_vacuum_scale_factor = 0, autovacuum_vacuum_threshold = 500,
205
+ autovacuum_analyze_scale_factor = 0, autovacuum_analyze_threshold = 500);
206
+ ALTER TABLE atomic.snapshot_session_links SET (
207
+ autovacuum_vacuum_scale_factor = 0, autovacuum_vacuum_threshold = 1000,
208
+ autovacuum_analyze_scale_factor = 0, autovacuum_analyze_threshold = 1000);
209
+ ```
210
+
211
+ On our server, `snapshot_session_links` had 13,444 dead rows before the change. Autovacuum cleaned it 21 seconds later, leaving none. Check dead rows and the last cleanup with:
212
+
213
+ ```sql
214
+ SELECT relname, n_live_tup, n_dead_tup, last_autovacuum
215
+ FROM pg_stat_user_tables
216
+ WHERE relname IN ('session_sessions', 'intercom_leases', 'snapshot_session_links');
217
+ ```
218
+
219
+ To undo the settings, run `ALTER TABLE <table> RESET (autovacuum_vacuum_scale_factor, autovacuum_vacuum_threshold, autovacuum_analyze_scale_factor, autovacuum_analyze_threshold);`.
220
+
221
+ ### Updates that stay on their page
222
+
223
+ When PostgreSQL writes a row's new version on the same page as the old one, and the update changes no indexed column, it can skip updating the table's indexes. This is called a HOT update, for heap-only tuple. It is faster and leaves less to clean up. It only works when the page has free space.
224
+
225
+ `fillfactor` leaves part of every page free for this. Atomic sets `fillfactor = 80` on `session_sessions` and `intercom_leases`, leaving a fifth of each page free, when you run `atomic storage migrate`. You do not set it yourself.
226
+
227
+ | Table | Share of updates that were HOT before | After `fillfactor = 80` | Where we measured |
228
+ | --- | --- | --- | --- |
229
+ | `session_sessions` | 92% | 94.5% | 300 sessions, in a test database |
230
+ | `intercom_leases` | 70 to 76% | about 90%, and the table half the size | 600 leases, in a test database |
231
+ | `session_sessions` | 71% on our server, misses on full pages | not yet measured there | production server |
232
+
233
+ `fillfactor` applies to pages written after the change. To rewrite existing pages, run `VACUUM FULL` on the table, which locks it while it runs, or use `pg_repack`. This is optional.
234
+
235
+ `snapshot_session_links` gets no HOT updates at all: 0 of 5,785 on our server. Its updates change a column that one of its indexes depends on, so free space does not help. The autovacuum settings above keep it small.
236
+
237
+ Check the share of HOT updates per table with:
238
+
239
+ ```sql
240
+ SELECT relname, n_tup_upd, n_tup_hot_upd,
241
+ round(100.0 * n_tup_hot_upd / nullif(n_tup_upd, 0), 1) AS hot_percent
242
+ FROM pg_stat_user_tables
243
+ WHERE relname IN ('session_sessions', 'intercom_leases', 'snapshot_session_links');
244
+ ```
245
+
246
+ ### The search index (optional)
247
+
248
+ Atomic indexes session text for search with a trigram index, `atomic.session_search_trgm`. This kind of index (GIN) collects new entries in a pending list and adds them to the index in one go when the list fills up. That flush happens inside one agent's save, so that save is slow now and then.
249
+
250
+ A smaller pending list makes the flushes smaller. In a benchmark of 460 inserts on PostgreSQL 16, run once per setting on a busy test machine, not on our server:
251
+
252
+ | Index setting | Insert p50 | Insert p99 | Slowest insert | Total insert time | Search |
253
+ | --- | --- | --- | --- | --- | --- |
254
+ | Default, 4 MB pending list | 1.96 ms | 22.9 ms | 26.5 ms | 1,122 ms | 0.83 ms |
255
+ | `gin_pending_list_limit = 1024` (1 MB) | 1.92 ms | 13.7 ms | 15.5 ms | 1,225 ms | 0.81 ms |
256
+ | `gin_pending_list_limit = 256` | 2.08 ms | 11.6 ms | 17.3 ms | 1,713 ms | 0.83 ms |
257
+ | `fastupdate = off` | 10.6 ms | 32.6 ms | 45.1 ms | 5,114 ms | 0.47 ms |
258
+
259
+ A 1 MB list halved the slowest inserts for 9% more total insert time. It does not make Atomic faster overall. If occasional slow saves matter to you, apply it:
260
+
261
+ ```sql
262
+ ALTER INDEX atomic.session_search_trgm SET (gin_pending_list_limit = 1024);
263
+ ```
264
+
265
+ ## What we checked and left alone
266
+
267
+ These settings come up in tuning advice. We measured each on our server and kept the default or the existing value:
268
+
269
+ - **`synchronous_commit = on`.** Turning it off makes commits return before they reach the disk, so a crash can lose the last commits. On our server's NVMe disk, writing the write-ahead log to disk (fsync) took 0.37 ms on average: 7,394 of them took 2.7 seconds in total. That is small next to a round trip, so there is nothing to gain.
270
+ - **`commit_delay`.** It makes commits wait a little so several can share one fsync. With 0.37 ms fsyncs, there is nothing to share.
271
+ - **`wal_compression`.** We kept `zstd`, which was already set. It makes the write-ahead log smaller at a small CPU cost, and the CPU was idle.
272
+ - **`io_method`.** PostgreSQL 18 can read from disk with `worker` (the default) or `io_uring`. With 99.999% of reads served from memory, the disk is rarely read, so we kept `worker`.
273
+ - **The search index's pending list.** See [The search index](#the-search-index-optional). It trades total insert time for fewer slow inserts, and does not raise throughput.
274
+ - **`deadlock_timeout`.** Atomic's lock waits are expected and end at its own lock timeout, so lowering it only adds work. Keep `log_lock_waits = on` to see long waits in the log.
275
+
276
+ We did not change `effective_cache_size`, `random_page_cost`, `max_wal_size` or `idle_in_transaction_session_timeout`. Their existing values worked.
277
+
278
+ ## After you upgrade Atomic
279
+
280
+ After every Atomic upgrade, run `atomic storage migrate` once, as the owner role, before you start many agents:
281
+
282
+ ```bash
283
+ atomic storage migrate --db 'postgresql://atomic_owner@db.example.com:5432/atomic' --password-file ~/atomic-owner-password
284
+ ```
285
+
286
+ Why it matters for performance:
287
+
288
+ - **Fast starts.** An agent opens its storage quickly, in a few round trips and without taking locks, only when the database is already up to date. If it is not, each agent starts by checking and updating it, and many agents starting at once queue behind each other.
289
+ - **New steps.** `atomic storage migrate` also applies changes a runtime role without DDL rights cannot make: the sequence Intercom uses to order new leases, and `fillfactor` on the busy tables. Until it runs, agents keep working, but without those gains.
290
+
291
+ ## Checklist
292
+
293
+ | Step | Restart needed | Measured on our server |
294
+ | --- | --- | --- |
295
+ | Run the database in the same region as the agents | — | Yes: 2 round trips per save |
296
+ | Size `max_connections` and per-role limits from the connection count, or put PgBouncer in front | `max_connections` only | Yes: raised 300 to 400, role 150 to 300 |
297
+ | Set `shared_buffers` to about a quarter of memory | Yes | Kept 8 GB of 30 GB |
298
+ | Reserve huge pages with `vm.nr_hugepages` and check `huge_pages_status` | Yes | Yes: `on`, 4259 pages needed, 4350 reserved |
299
+ | Preload `pg_stat_statements`, create it as a superuser, grant `pg_read_all_stats` | Yes | Yes |
300
+ | Set per-table autovacuum on `session_sessions`, `intercom_leases` and `snapshot_session_links` | No | Yes |
301
+ | Run `atomic storage migrate` after each Atomic upgrade | No | Yes |
302
+ | Optionally, shrink the search index's pending list | No | In a benchmark only |
303
+ | Leave `synchronous_commit`, `commit_delay`, `wal_compression` and `io_method` as they are | — | Yes |
304
+
305
+ On a managed service, the server settings above may be fixed or set through the service's own parameters. We have not tested this guide on Amazon RDS, Azure Database for PostgreSQL or Google Cloud SQL. The SQL statements work the same way there, except that creating `pg_stat_statements` needs the service's administrator role.
306
+
307
+ ## Related pages
308
+
309
+ - [PostgreSQL storage](/storage#postgresql): set up the database and roles.
310
+ - [Tuning for many concurrent agents](/storage#tuning-for-many-concurrent-agents): size Atomic's connection settings.
311
+ - [Run many agents on PostgreSQL with PgBouncer](/guides/postgres-pgbouncer).