@bastani/atomic 0.10.2 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (499) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/dist/builtin/intercom/CHANGELOG.md +22 -0
  3. package/dist/builtin/intercom/broker/broker-instance.ts +2 -13
  4. package/dist/builtin/intercom/broker/broker.ts +158 -73
  5. package/dist/builtin/intercom/broker/durable-ended-queue.ts +14 -2
  6. package/dist/builtin/intercom/broker/host-command-environment.ts +13 -0
  7. package/dist/builtin/intercom/broker/hosted-session-errors.ts +1 -0
  8. package/dist/builtin/intercom/broker/multinode-router.ts +111 -16
  9. package/dist/builtin/intercom/broker/multinode-state-store.ts +50 -4
  10. package/dist/builtin/intercom/broker/open-broker-state.ts +5 -4
  11. package/dist/builtin/intercom/broker/spawn.ts +144 -25
  12. package/dist/builtin/intercom/broker/sql-add-column.ts +33 -0
  13. package/dist/builtin/intercom/broker/sql-known-session-store.ts +120 -36
  14. package/dist/builtin/intercom/broker/sql-outbox-store.ts +75 -13
  15. package/dist/builtin/intercom/broker/start-announcement.ts +55 -10
  16. package/dist/builtin/intercom/broker/watchdog.ts +10 -7
  17. package/dist/builtin/intercom/index.bundle.mjs +111 -27
  18. package/dist/builtin/intercom/package.json +1 -1
  19. package/dist/builtin/mcp/package.json +1 -1
  20. package/dist/builtin/subagents/CHANGELOG.md +26 -0
  21. package/dist/builtin/subagents/package.json +1 -1
  22. package/dist/builtin/subagents/skills/skill-creator/SKILL.md +30 -52
  23. package/dist/builtin/subagents/skills/skill-creator/eval-viewer/generate_review.py +34 -40
  24. package/dist/builtin/subagents/skills/skill-creator/eval-viewer/viewer.html +2 -2
  25. package/dist/builtin/subagents/skills/skill-creator/scripts/atomic_cli.py +731 -0
  26. package/dist/builtin/subagents/skills/skill-creator/scripts/generate_report.py +3 -3
  27. package/dist/builtin/subagents/skills/skill-creator/scripts/improve_description.py +41 -38
  28. package/dist/builtin/subagents/skills/skill-creator/scripts/run_eval.py +276 -176
  29. package/dist/builtin/subagents/skills/skill-creator/scripts/run_loop.py +20 -11
  30. package/dist/builtin/subagents/skills/skill-creator/scripts/utils.py +1 -1
  31. package/dist/builtin/subagents/src/extension/index.bundle.mjs +163 -97
  32. package/dist/builtin/web-access/CHANGELOG.md +14 -0
  33. package/dist/builtin/web-access/index.bundle.mjs +5 -4
  34. package/dist/builtin/web-access/package.json +1 -1
  35. package/dist/core/agent-session-auto-compaction.d.ts +10 -1
  36. package/dist/core/agent-session-auto-compaction.d.ts.map +1 -1
  37. package/dist/core/agent-session-auto-compaction.js +54 -2
  38. package/dist/core/agent-session-auto-compaction.js.map +1 -1
  39. package/dist/core/agent-session-compaction.d.ts +4 -0
  40. package/dist/core/agent-session-compaction.d.ts.map +1 -1
  41. package/dist/core/agent-session-compaction.js +17 -2
  42. package/dist/core/agent-session-compaction.js.map +1 -1
  43. package/dist/core/agent-session-custom-message-commit.d.ts.map +1 -1
  44. package/dist/core/agent-session-custom-message-commit.js +3 -1
  45. package/dist/core/agent-session-custom-message-commit.js.map +1 -1
  46. package/dist/core/agent-session-events.d.ts +2 -0
  47. package/dist/core/agent-session-events.d.ts.map +1 -1
  48. package/dist/core/agent-session-events.js +34 -13
  49. package/dist/core/agent-session-events.js.map +1 -1
  50. package/dist/core/agent-session-export.d.ts.map +1 -1
  51. package/dist/core/agent-session-export.js +3 -0
  52. package/dist/core/agent-session-export.js.map +1 -1
  53. package/dist/core/agent-session-message-queue.d.ts +27 -0
  54. package/dist/core/agent-session-message-queue.d.ts.map +1 -1
  55. package/dist/core/agent-session-message-queue.js +213 -30
  56. package/dist/core/agent-session-message-queue.js.map +1 -1
  57. package/dist/core/agent-session-methods.d.ts +6 -1
  58. package/dist/core/agent-session-methods.d.ts.map +1 -1
  59. package/dist/core/agent-session-methods.js.map +1 -1
  60. package/dist/core/agent-session-prompt.d.ts.map +1 -1
  61. package/dist/core/agent-session-prompt.js +35 -3
  62. package/dist/core/agent-session-prompt.js.map +1 -1
  63. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  64. package/dist/core/agent-session-runtime.js +2 -2
  65. package/dist/core/agent-session-runtime.js.map +1 -1
  66. package/dist/core/agent-session-state.d.ts.map +1 -1
  67. package/dist/core/agent-session-state.js +6 -1
  68. package/dist/core/agent-session-state.js.map +1 -1
  69. package/dist/core/agent-session-tool-hooks.d.ts.map +1 -1
  70. package/dist/core/agent-session-tool-hooks.js +15 -2
  71. package/dist/core/agent-session-tool-hooks.js.map +1 -1
  72. package/dist/core/agent-session-typed-queue.d.ts +14 -1
  73. package/dist/core/agent-session-typed-queue.d.ts.map +1 -1
  74. package/dist/core/agent-session-typed-queue.js +26 -1
  75. package/dist/core/agent-session-typed-queue.js.map +1 -1
  76. package/dist/core/agent-session-types.d.ts +5 -0
  77. package/dist/core/agent-session-types.d.ts.map +1 -1
  78. package/dist/core/agent-session-types.js.map +1 -1
  79. package/dist/core/agent-session.d.ts.map +1 -1
  80. package/dist/core/agent-session.js +2 -1
  81. package/dist/core/agent-session.js.map +1 -1
  82. package/dist/core/async-scope.d.ts +9 -4
  83. package/dist/core/async-scope.d.ts.map +1 -1
  84. package/dist/core/async-scope.js +13 -5
  85. package/dist/core/async-scope.js.map +1 -1
  86. package/dist/core/builtin-diagnostics.js +2 -2
  87. package/dist/core/builtin-diagnostics.js.map +1 -1
  88. package/dist/core/chat-admission-order.d.ts.map +1 -1
  89. package/dist/core/chat-admission-order.js +2 -1
  90. package/dist/core/chat-admission-order.js.map +1 -1
  91. package/dist/core/chat-record-saves.d.ts +4 -0
  92. package/dist/core/chat-record-saves.d.ts.map +1 -0
  93. package/dist/core/chat-record-saves.js +25 -0
  94. package/dist/core/chat-record-saves.js.map +1 -0
  95. package/dist/core/command-deliveries.d.ts +62 -0
  96. package/dist/core/command-deliveries.d.ts.map +1 -0
  97. package/dist/core/command-deliveries.js +139 -0
  98. package/dist/core/command-deliveries.js.map +1 -0
  99. package/dist/core/diagnostic-writer.d.ts.map +1 -1
  100. package/dist/core/diagnostic-writer.js +5 -4
  101. package/dist/core/diagnostic-writer.js.map +1 -1
  102. package/dist/core/extension-startup-close.d.ts +1 -2
  103. package/dist/core/extension-startup-close.d.ts.map +1 -1
  104. package/dist/core/extension-startup-close.js +2 -2
  105. package/dist/core/extension-startup-close.js.map +1 -1
  106. package/dist/core/extensions/jiti-aliases.d.ts +11 -0
  107. package/dist/core/extensions/jiti-aliases.d.ts.map +1 -0
  108. package/dist/core/extensions/jiti-aliases.js +28 -0
  109. package/dist/core/extensions/jiti-aliases.js.map +1 -0
  110. package/dist/core/extensions/loader-api.js +3 -3
  111. package/dist/core/extensions/loader-api.js.map +1 -1
  112. package/dist/core/extensions/loader-bindings.js +2 -2
  113. package/dist/core/extensions/loader-bindings.js.map +1 -1
  114. package/dist/core/extensions/loader-rollback.d.ts +1 -2
  115. package/dist/core/extensions/loader-rollback.d.ts.map +1 -1
  116. package/dist/core/extensions/loader-rollback.js +2 -2
  117. package/dist/core/extensions/loader-rollback.js.map +1 -1
  118. package/dist/core/extensions/loader-virtual-modules.d.ts.map +1 -1
  119. package/dist/core/extensions/loader-virtual-modules.js +53 -9
  120. package/dist/core/extensions/loader-virtual-modules.js.map +1 -1
  121. package/dist/core/extensions/native-builtin-entries.d.ts +5 -0
  122. package/dist/core/extensions/native-builtin-entries.d.ts.map +1 -1
  123. package/dist/core/extensions/native-builtin-entries.js +8 -1
  124. package/dist/core/extensions/native-builtin-entries.js.map +1 -1
  125. package/dist/core/extensions/reloadable-scripts.d.ts +21 -2
  126. package/dist/core/extensions/reloadable-scripts.d.ts.map +1 -1
  127. package/dist/core/extensions/reloadable-scripts.js +11 -3
  128. package/dist/core/extensions/reloadable-scripts.js.map +1 -1
  129. package/dist/core/extensions/source-builtin-imports.d.ts +40 -0
  130. package/dist/core/extensions/source-builtin-imports.d.ts.map +1 -0
  131. package/dist/core/extensions/source-builtin-imports.js +142 -0
  132. package/dist/core/extensions/source-builtin-imports.js.map +1 -0
  133. package/dist/core/extensions/transform-graph.d.ts +15 -0
  134. package/dist/core/extensions/transform-graph.d.ts.map +1 -0
  135. package/dist/core/extensions/transform-graph.js +85 -0
  136. package/dist/core/extensions/transform-graph.js.map +1 -0
  137. package/dist/core/extensions/ts-source-specifiers.d.ts +2 -0
  138. package/dist/core/extensions/ts-source-specifiers.d.ts.map +1 -1
  139. package/dist/core/extensions/ts-source-specifiers.js +7 -0
  140. package/dist/core/extensions/ts-source-specifiers.js.map +1 -1
  141. package/dist/core/extensions/ts-transform-worker.d.ts +2 -0
  142. package/dist/core/extensions/ts-transform-worker.d.ts.map +1 -0
  143. package/dist/core/extensions/ts-transform-worker.js +43 -0
  144. package/dist/core/extensions/ts-transform-worker.js.map +1 -0
  145. package/dist/core/message-admission.d.ts.map +1 -1
  146. package/dist/core/message-admission.js +4 -4
  147. package/dist/core/message-admission.js.map +1 -1
  148. package/dist/core/messages.d.ts +5 -0
  149. package/dist/core/messages.d.ts.map +1 -1
  150. package/dist/core/messages.js +35 -0
  151. package/dist/core/messages.js.map +1 -1
  152. package/dist/core/model-fallback-failures.d.ts +1 -1
  153. package/dist/core/model-fallback-failures.d.ts.map +1 -1
  154. package/dist/core/model-fallback-failures.js +18 -1
  155. package/dist/core/model-fallback-failures.js.map +1 -1
  156. package/dist/core/process-start-token.d.ts +7 -0
  157. package/dist/core/process-start-token.d.ts.map +1 -1
  158. package/dist/core/process-start-token.js +14 -0
  159. package/dist/core/process-start-token.js.map +1 -1
  160. package/dist/core/project-trust-electorate.d.ts +24 -0
  161. package/dist/core/project-trust-electorate.d.ts.map +1 -0
  162. package/dist/core/project-trust-electorate.js +52 -0
  163. package/dist/core/project-trust-electorate.js.map +1 -0
  164. package/dist/core/project-trust-state.d.ts +15 -0
  165. package/dist/core/project-trust-state.d.ts.map +1 -1
  166. package/dist/core/project-trust-state.js +15 -0
  167. package/dist/core/project-trust-state.js.map +1 -1
  168. package/dist/core/provider-request-admission.d.ts.map +1 -1
  169. package/dist/core/provider-request-admission.js +2 -2
  170. package/dist/core/provider-request-admission.js.map +1 -1
  171. package/dist/core/queued-input-admission.js +2 -2
  172. package/dist/core/queued-input-admission.js.map +1 -1
  173. package/dist/core/queued-message-delivery.d.ts +2 -2
  174. package/dist/core/queued-message-delivery.d.ts.map +1 -1
  175. package/dist/core/queued-message-delivery.js +6 -3
  176. package/dist/core/queued-message-delivery.js.map +1 -1
  177. package/dist/core/resource-admission-scope.js +2 -2
  178. package/dist/core/resource-admission-scope.js.map +1 -1
  179. package/dist/core/resume/blueprint/process.d.ts +3 -4
  180. package/dist/core/resume/blueprint/process.d.ts.map +1 -1
  181. package/dist/core/resume/blueprint/process.js +33 -17
  182. package/dist/core/resume/blueprint/process.js.map +1 -1
  183. package/dist/core/resume/path-mapping.js +4 -4
  184. package/dist/core/resume/path-mapping.js.map +1 -1
  185. package/dist/core/resume/session-resume.d.ts +9 -0
  186. package/dist/core/resume/session-resume.d.ts.map +1 -1
  187. package/dist/core/resume/session-resume.js +27 -2
  188. package/dist/core/resume/session-resume.js.map +1 -1
  189. package/dist/core/resume-cache-checkpoint.d.ts +27 -1
  190. package/dist/core/resume-cache-checkpoint.d.ts.map +1 -1
  191. package/dist/core/resume-cache-checkpoint.js +45 -0
  192. package/dist/core/resume-cache-checkpoint.js.map +1 -1
  193. package/dist/core/sdk.d.ts.map +1 -1
  194. package/dist/core/sdk.js +7 -31
  195. package/dist/core/sdk.js.map +1 -1
  196. package/dist/core/session-lifecycle-scope.d.ts +1 -2
  197. package/dist/core/session-lifecycle-scope.d.ts.map +1 -1
  198. package/dist/core/session-lifecycle-scope.js +2 -2
  199. package/dist/core/session-lifecycle-scope.js.map +1 -1
  200. package/dist/core/session-lifecycle-work.js +2 -2
  201. package/dist/core/session-lifecycle-work.js.map +1 -1
  202. package/dist/core/session-manager-core.d.ts.map +1 -1
  203. package/dist/core/session-manager-core.js +3 -0
  204. package/dist/core/session-manager-core.js.map +1 -1
  205. package/dist/core/session-storage.d.ts +12 -0
  206. package/dist/core/session-storage.d.ts.map +1 -1
  207. package/dist/core/session-storage.js +68 -1
  208. package/dist/core/session-storage.js.map +1 -1
  209. package/dist/core/settings-lock-directory.d.ts.map +1 -1
  210. package/dist/core/settings-lock-directory.js +4 -3
  211. package/dist/core/settings-lock-directory.js.map +1 -1
  212. package/dist/core/settings-write-ownership.d.ts +1 -2
  213. package/dist/core/settings-write-ownership.d.ts.map +1 -1
  214. package/dist/core/settings-write-ownership.js +2 -2
  215. package/dist/core/settings-write-ownership.js.map +1 -1
  216. package/dist/core/storage/node-sqlite-thread.d.ts.map +1 -1
  217. package/dist/core/storage/node-sqlite-thread.js +6 -4
  218. package/dist/core/storage/node-sqlite-thread.js.map +1 -1
  219. package/dist/core/storage/node-sqlite.d.ts.map +1 -1
  220. package/dist/core/storage/node-sqlite.js +22 -8
  221. package/dist/core/storage/node-sqlite.js.map +1 -1
  222. package/dist/core/system-prompt.d.ts +16 -2
  223. package/dist/core/system-prompt.d.ts.map +1 -1
  224. package/dist/core/system-prompt.js +33 -6
  225. package/dist/core/system-prompt.js.map +1 -1
  226. package/dist/core/tasks/agent-adapter.d.ts.map +1 -1
  227. package/dist/core/tasks/agent-adapter.js +2 -1
  228. package/dist/core/tasks/agent-adapter.js.map +1 -1
  229. package/dist/core/tasks/completion.d.ts +3 -2
  230. package/dist/core/tasks/completion.d.ts.map +1 -1
  231. package/dist/core/tasks/completion.js.map +1 -1
  232. package/dist/core/tasks/execution-scope.js +2 -2
  233. package/dist/core/tasks/execution-scope.js.map +1 -1
  234. package/dist/core/tools/bash.d.ts.map +1 -1
  235. package/dist/core/tools/bash.js +2 -1
  236. package/dist/core/tools/bash.js.map +1 -1
  237. package/dist/core/tools/history-regex-worker.d.ts.map +1 -1
  238. package/dist/core/tools/history-regex-worker.js +3 -2
  239. package/dist/core/tools/history-regex-worker.js.map +1 -1
  240. package/dist/core/tools/shell-task-render.d.ts +3 -2
  241. package/dist/core/tools/shell-task-render.d.ts.map +1 -1
  242. package/dist/core/tools/shell-task-render.js +7 -5
  243. package/dist/core/tools/shell-task-render.js.map +1 -1
  244. package/dist/core/tools/shell-task-status.d.ts +6 -2
  245. package/dist/core/tools/shell-task-status.d.ts.map +1 -1
  246. package/dist/core/tools/shell-task-status.js +13 -8
  247. package/dist/core/tools/shell-task-status.js.map +1 -1
  248. package/dist/core/tools/sqlite-selector-connection.d.ts.map +1 -1
  249. package/dist/core/tools/sqlite-selector-connection.js +4 -3
  250. package/dist/core/tools/sqlite-selector-connection.js.map +1 -1
  251. package/dist/core/tools/windows-directory-security.d.ts.map +1 -1
  252. package/dist/core/tools/windows-directory-security.js +2 -1
  253. package/dist/core/tools/windows-directory-security.js.map +1 -1
  254. package/dist/core/worktree-snapshot/git.js +2 -2
  255. package/dist/core/worktree-snapshot/git.js.map +1 -1
  256. package/dist/extensions/mcp/index.d.ts +6 -1
  257. package/dist/extensions/mcp/index.d.ts.map +1 -1
  258. package/dist/extensions/mcp/index.js +150 -18
  259. package/dist/extensions/mcp/index.js.map +1 -1
  260. package/dist/extensions/mcp/tool-records.d.ts +44 -9
  261. package/dist/extensions/mcp/tool-records.d.ts.map +1 -1
  262. package/dist/extensions/mcp/tool-records.js +58 -16
  263. package/dist/extensions/mcp/tool-records.js.map +1 -1
  264. package/dist/index.d.ts +2 -0
  265. package/dist/index.d.ts.map +1 -1
  266. package/dist/index.js +4 -0
  267. package/dist/index.js.map +1 -1
  268. package/dist/main-session.d.ts +3 -1
  269. package/dist/main-session.d.ts.map +1 -1
  270. package/dist/main-session.js +1 -0
  271. package/dist/main-session.js.map +1 -1
  272. package/dist/main.d.ts.map +1 -1
  273. package/dist/main.js +230 -96
  274. package/dist/main.js.map +1 -1
  275. package/dist/modes/interactive/agent-view/agent-view-bridge.d.ts +5 -0
  276. package/dist/modes/interactive/agent-view/agent-view-bridge.d.ts.map +1 -1
  277. package/dist/modes/interactive/agent-view/agent-view-bridge.js.map +1 -1
  278. package/dist/modes/interactive/agent-view/agent-view-switcher.d.ts +5 -2
  279. package/dist/modes/interactive/agent-view/agent-view-switcher.d.ts.map +1 -1
  280. package/dist/modes/interactive/agent-view/agent-view-switcher.js +3 -3
  281. package/dist/modes/interactive/agent-view/agent-view-switcher.js.map +1 -1
  282. package/dist/modes/interactive/agent-view/contracts.d.ts +6 -0
  283. package/dist/modes/interactive/agent-view/contracts.d.ts.map +1 -1
  284. package/dist/modes/interactive/agent-view/contracts.js.map +1 -1
  285. package/dist/modes/interactive/agent-view/install.d.ts.map +1 -1
  286. package/dist/modes/interactive/agent-view/install.js +2 -1
  287. package/dist/modes/interactive/agent-view/install.js.map +1 -1
  288. package/dist/modes/interactive/agent-view/open-port.d.ts +4 -2
  289. package/dist/modes/interactive/agent-view/open-port.d.ts.map +1 -1
  290. package/dist/modes/interactive/agent-view/open-port.js +4 -3
  291. package/dist/modes/interactive/agent-view/open-port.js.map +1 -1
  292. package/dist/modes/interactive/agent-view/prompt-retarget.d.ts.map +1 -1
  293. package/dist/modes/interactive/agent-view/prompt-retarget.js +18 -5
  294. package/dist/modes/interactive/agent-view/prompt-retarget.js.map +1 -1
  295. package/dist/modes/interactive/agent-view/remote-port.d.ts +10 -1
  296. package/dist/modes/interactive/agent-view/remote-port.d.ts.map +1 -1
  297. package/dist/modes/interactive/agent-view/remote-port.js +36 -4
  298. package/dist/modes/interactive/agent-view/remote-port.js.map +1 -1
  299. package/dist/modes/interactive/agent-view/viewed-keys.d.ts +56 -0
  300. package/dist/modes/interactive/agent-view/viewed-keys.d.ts.map +1 -0
  301. package/dist/modes/interactive/agent-view/viewed-keys.js +63 -0
  302. package/dist/modes/interactive/agent-view/viewed-keys.js.map +1 -0
  303. package/dist/modes/interactive/agent-view/viewed-submit.d.ts +14 -4
  304. package/dist/modes/interactive/agent-view/viewed-submit.d.ts.map +1 -1
  305. package/dist/modes/interactive/agent-view/viewed-submit.js +51 -17
  306. package/dist/modes/interactive/agent-view/viewed-submit.js.map +1 -1
  307. package/dist/modes/interactive/components/agent-panel.d.ts.map +1 -1
  308. package/dist/modes/interactive/components/agent-panel.js +8 -21
  309. package/dist/modes/interactive/components/agent-panel.js.map +1 -1
  310. package/dist/modes/interactive/components/chat-session-host-actions.d.ts.map +1 -1
  311. package/dist/modes/interactive/components/chat-session-host-actions.js +18 -5
  312. package/dist/modes/interactive/components/chat-session-host-actions.js.map +1 -1
  313. package/dist/modes/interactive/components/chat-session-host-rendering.d.ts.map +1 -1
  314. package/dist/modes/interactive/components/chat-session-host-rendering.js +6 -0
  315. package/dist/modes/interactive/components/chat-session-host-rendering.js.map +1 -1
  316. package/dist/modes/interactive/components/chat-session-host-state.d.ts +1 -0
  317. package/dist/modes/interactive/components/chat-session-host-state.d.ts.map +1 -1
  318. package/dist/modes/interactive/components/chat-session-host-state.js +2 -0
  319. package/dist/modes/interactive/components/chat-session-host-state.js.map +1 -1
  320. package/dist/modes/interactive/components/chat-session-host-types.d.ts +5 -0
  321. package/dist/modes/interactive/components/chat-session-host-types.d.ts.map +1 -1
  322. package/dist/modes/interactive/components/chat-session-host-types.js.map +1 -1
  323. package/dist/modes/interactive/components/custom-editor.d.ts +33 -2
  324. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
  325. package/dist/modes/interactive/components/custom-editor.js +58 -2
  326. package/dist/modes/interactive/components/custom-editor.js.map +1 -1
  327. package/dist/modes/interactive/components/host-session-picker.js +2 -2
  328. package/dist/modes/interactive/components/host-session-picker.js.map +1 -1
  329. package/dist/modes/interactive/components/main-composer-keys.d.ts +16 -5
  330. package/dist/modes/interactive/components/main-composer-keys.d.ts.map +1 -1
  331. package/dist/modes/interactive/components/main-composer-keys.js +27 -8
  332. package/dist/modes/interactive/components/main-composer-keys.js.map +1 -1
  333. package/dist/modes/interactive/components/notice-line.d.ts +8 -25
  334. package/dist/modes/interactive/components/notice-line.d.ts.map +1 -1
  335. package/dist/modes/interactive/components/notice-line.js +18 -17
  336. package/dist/modes/interactive/components/notice-line.js.map +1 -1
  337. package/dist/modes/interactive/components/prompt-slot.d.ts +20 -3
  338. package/dist/modes/interactive/components/prompt-slot.d.ts.map +1 -1
  339. package/dist/modes/interactive/components/prompt-slot.js +82 -32
  340. package/dist/modes/interactive/components/prompt-slot.js.map +1 -1
  341. package/dist/modes/interactive/components/queued-messages-view.d.ts +5 -2
  342. package/dist/modes/interactive/components/queued-messages-view.d.ts.map +1 -1
  343. package/dist/modes/interactive/components/queued-messages-view.js +12 -5
  344. package/dist/modes/interactive/components/queued-messages-view.js.map +1 -1
  345. package/dist/modes/interactive/components/task-board.d.ts +5 -8
  346. package/dist/modes/interactive/components/task-board.d.ts.map +1 -1
  347. package/dist/modes/interactive/components/task-board.js +5 -49
  348. package/dist/modes/interactive/components/task-board.js.map +1 -1
  349. package/dist/modes/interactive/components/task-completion-message.d.ts.map +1 -1
  350. package/dist/modes/interactive/components/task-completion-message.js +9 -2
  351. package/dist/modes/interactive/components/task-completion-message.js.map +1 -1
  352. package/dist/modes/interactive/components/task-marks.d.ts +19 -0
  353. package/dist/modes/interactive/components/task-marks.d.ts.map +1 -0
  354. package/dist/modes/interactive/components/task-marks.js +25 -0
  355. package/dist/modes/interactive/components/task-marks.js.map +1 -0
  356. package/dist/modes/interactive/components/task-row.d.ts +10 -2
  357. package/dist/modes/interactive/components/task-row.d.ts.map +1 -1
  358. package/dist/modes/interactive/components/task-row.js +39 -32
  359. package/dist/modes/interactive/components/task-row.js.map +1 -1
  360. package/dist/modes/interactive/components/task-transcript-page.d.ts +7 -0
  361. package/dist/modes/interactive/components/task-transcript-page.d.ts.map +1 -1
  362. package/dist/modes/interactive/components/task-transcript-page.js +106 -10
  363. package/dist/modes/interactive/components/task-transcript-page.js.map +1 -1
  364. package/dist/modes/interactive/components/tool-block-families.d.ts.map +1 -1
  365. package/dist/modes/interactive/components/tool-block-families.js +8 -9
  366. package/dist/modes/interactive/components/tool-block-families.js.map +1 -1
  367. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  368. package/dist/modes/interactive/components/tool-execution.js +5 -1
  369. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  370. package/dist/modes/interactive/extension-command-hold.d.ts.map +1 -1
  371. package/dist/modes/interactive/extension-command-hold.js +5 -7
  372. package/dist/modes/interactive/extension-command-hold.js.map +1 -1
  373. package/dist/modes/interactive/interactive-extension-dialogs.js +6 -1
  374. package/dist/modes/interactive/interactive-extension-dialogs.js.map +1 -1
  375. package/dist/modes/interactive/interactive-extension-runtime.d.ts.map +1 -1
  376. package/dist/modes/interactive/interactive-extension-runtime.js +21 -7
  377. package/dist/modes/interactive/interactive-extension-runtime.js.map +1 -1
  378. package/dist/modes/interactive/interactive-initial-session-binding.d.ts +1 -2
  379. package/dist/modes/interactive/interactive-initial-session-binding.d.ts.map +1 -1
  380. package/dist/modes/interactive/interactive-initial-session-binding.js +2 -2
  381. package/dist/modes/interactive/interactive-initial-session-binding.js.map +1 -1
  382. package/dist/modes/interactive/interactive-input-handling.d.ts.map +1 -1
  383. package/dist/modes/interactive/interactive-input-handling.js +13 -4
  384. package/dist/modes/interactive/interactive-input-handling.js.map +1 -1
  385. package/dist/modes/interactive/interactive-mode-base.d.ts +2 -1
  386. package/dist/modes/interactive/interactive-mode-base.d.ts.map +1 -1
  387. package/dist/modes/interactive/interactive-mode-base.js +6 -1
  388. package/dist/modes/interactive/interactive-mode-base.js.map +1 -1
  389. package/dist/modes/interactive/interactive-mode-helpers.d.ts +4 -2
  390. package/dist/modes/interactive/interactive-mode-helpers.d.ts.map +1 -1
  391. package/dist/modes/interactive/interactive-mode-helpers.js +6 -0
  392. package/dist/modes/interactive/interactive-mode-helpers.js.map +1 -1
  393. package/dist/modes/interactive/interactive-mode-surface.d.ts +5 -0
  394. package/dist/modes/interactive/interactive-mode-surface.d.ts.map +1 -1
  395. package/dist/modes/interactive/interactive-mode-surface.js.map +1 -1
  396. package/dist/modes/interactive/interactive-process-lifecycle.js +96 -17
  397. package/dist/modes/interactive/interactive-process-lifecycle.js.map +1 -1
  398. package/dist/modes/interactive/interactive-queueing.d.ts +5 -0
  399. package/dist/modes/interactive/interactive-queueing.d.ts.map +1 -1
  400. package/dist/modes/interactive/interactive-queueing.js +37 -5
  401. package/dist/modes/interactive/interactive-queueing.js.map +1 -1
  402. package/dist/modes/interactive/interactive-render-chat.d.ts.map +1 -1
  403. package/dist/modes/interactive/interactive-render-chat.js +45 -4
  404. package/dist/modes/interactive/interactive-render-chat.js.map +1 -1
  405. package/dist/modes/interactive/interactive-resource-disclosure.d.ts +4 -1
  406. package/dist/modes/interactive/interactive-resource-disclosure.d.ts.map +1 -1
  407. package/dist/modes/interactive/interactive-resource-disclosure.js +20 -1
  408. package/dist/modes/interactive/interactive-resource-disclosure.js.map +1 -1
  409. package/dist/modes/interactive/interactive-resource-rendering.js +3 -0
  410. package/dist/modes/interactive/interactive-resource-rendering.js.map +1 -1
  411. package/dist/modes/interactive/interactive-startup.d.ts.map +1 -1
  412. package/dist/modes/interactive/interactive-startup.js +43 -5
  413. package/dist/modes/interactive/interactive-startup.js.map +1 -1
  414. package/dist/modes/interactive-engine/activity-watchdog.d.ts +6 -3
  415. package/dist/modes/interactive-engine/activity-watchdog.d.ts.map +1 -1
  416. package/dist/modes/interactive-engine/activity-watchdog.js +3 -2
  417. package/dist/modes/interactive-engine/activity-watchdog.js.map +1 -1
  418. package/dist/modes/interactive-engine/engine-health.d.ts +2 -0
  419. package/dist/modes/interactive-engine/engine-health.d.ts.map +1 -1
  420. package/dist/modes/interactive-engine/engine-health.js.map +1 -1
  421. package/dist/modes/interactive-engine/engine-quit-intent.d.ts +10 -0
  422. package/dist/modes/interactive-engine/engine-quit-intent.d.ts.map +1 -1
  423. package/dist/modes/interactive-engine/engine-quit-intent.js +33 -0
  424. package/dist/modes/interactive-engine/engine-quit-intent.js.map +1 -1
  425. package/dist/modes/interactive-engine/extension-ui-bridge.d.ts +7 -1
  426. package/dist/modes/interactive-engine/extension-ui-bridge.d.ts.map +1 -1
  427. package/dist/modes/interactive-engine/extension-ui-bridge.js +15 -2
  428. package/dist/modes/interactive-engine/extension-ui-bridge.js.map +1 -1
  429. package/dist/modes/interactive-engine/host-listener-failures.d.ts +24 -0
  430. package/dist/modes/interactive-engine/host-listener-failures.d.ts.map +1 -0
  431. package/dist/modes/interactive-engine/host-listener-failures.js +98 -0
  432. package/dist/modes/interactive-engine/host-listener-failures.js.map +1 -0
  433. package/dist/modes/interactive-engine/isolated-runtime.d.ts +16 -0
  434. package/dist/modes/interactive-engine/isolated-runtime.d.ts.map +1 -1
  435. package/dist/modes/interactive-engine/isolated-runtime.js +74 -7
  436. package/dist/modes/interactive-engine/isolated-runtime.js.map +1 -1
  437. package/dist/modes/interactive-engine/protocol.d.ts +5 -0
  438. package/dist/modes/interactive-engine/protocol.d.ts.map +1 -1
  439. package/dist/modes/interactive-engine/protocol.js +8 -2
  440. package/dist/modes/interactive-engine/protocol.js.map +1 -1
  441. package/dist/modes/interactive-engine/remote-component.d.ts +13 -0
  442. package/dist/modes/interactive-engine/remote-component.d.ts.map +1 -1
  443. package/dist/modes/interactive-engine/remote-component.js +69 -4
  444. package/dist/modes/interactive-engine/remote-component.js.map +1 -1
  445. package/dist/modes/print-mode.d.ts.map +1 -1
  446. package/dist/modes/print-mode.js +34 -6
  447. package/dist/modes/print-mode.js.map +1 -1
  448. package/dist/modes/rpc/agent-panel-bridge.d.ts +7 -0
  449. package/dist/modes/rpc/agent-panel-bridge.d.ts.map +1 -1
  450. package/dist/modes/rpc/agent-panel-bridge.js +11 -1
  451. package/dist/modes/rpc/agent-panel-bridge.js.map +1 -1
  452. package/dist/modes/rpc/agent-view-rpc-panel.d.ts.map +1 -1
  453. package/dist/modes/rpc/agent-view-rpc-panel.js +11 -5
  454. package/dist/modes/rpc/agent-view-rpc-panel.js.map +1 -1
  455. package/dist/modes/rpc/rpc-client-process.d.ts +8 -1
  456. package/dist/modes/rpc/rpc-client-process.d.ts.map +1 -1
  457. package/dist/modes/rpc/rpc-client-process.js +89 -65
  458. package/dist/modes/rpc/rpc-client-process.js.map +1 -1
  459. package/dist/modes/rpc/rpc-client.d.ts +21 -0
  460. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  461. package/dist/modes/rpc/rpc-client.js +77 -33
  462. package/dist/modes/rpc/rpc-client.js.map +1 -1
  463. package/dist/modes/rpc/rpc-event-buffer.d.ts +5 -0
  464. package/dist/modes/rpc/rpc-event-buffer.d.ts.map +1 -1
  465. package/dist/modes/rpc/rpc-event-buffer.js +8 -2
  466. package/dist/modes/rpc/rpc-event-buffer.js.map +1 -1
  467. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  468. package/dist/modes/rpc/rpc-mode.js +9 -2
  469. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  470. package/dist/utils/shell.d.ts +5 -1
  471. package/dist/utils/shell.d.ts.map +1 -1
  472. package/dist/utils/shell.js +14 -7
  473. package/dist/utils/shell.js.map +1 -1
  474. package/dist/utils/worker-threads.d.ts +10 -4
  475. package/dist/utils/worker-threads.d.ts.map +1 -1
  476. package/dist/utils/worker-threads.js +10 -4
  477. package/dist/utils/worker-threads.js.map +1 -1
  478. package/docs/background-tasks.md +10 -10
  479. package/docs/compaction.md +1 -0
  480. package/docs/development.md +1 -1
  481. package/docs/environment-variables.md +4 -0
  482. package/docs/extensions/api-reference.md +2 -0
  483. package/docs/extensions/events.md +1 -1
  484. package/docs/getting-started/installation.md +1 -0
  485. package/docs/guides/non-interactive.md +2 -0
  486. package/docs/guides/resume-on-any-machine.md +3 -1
  487. package/docs/intercom/reference.md +1 -1
  488. package/docs/keybindings.md +6 -5
  489. package/docs/libraries/ai.md +2 -1
  490. package/docs/mcp-servers.md +1 -1
  491. package/docs/sdk/reference.md +1 -1
  492. package/docs/sdk.md +6 -0
  493. package/docs/security.md +4 -2
  494. package/docs/settings.md +2 -1
  495. package/docs/storage.md +2 -0
  496. package/docs/subagents.md +2 -2
  497. package/docs/tui/reference.md +3 -1
  498. package/npm-shrinkwrap.json +49 -49
  499. package/package.json +6 -6
@@ -1,5 +1,5 @@
1
- import { dialectOf, type SqliteRow, validateStoragePrefix } from "@bastani/atomic/storage";
2
- import { addMissingColumns } from "./sql-add-column.js";
1
+ import { dialectOf, type SqliteExecutor, type SqliteRow, validateStoragePrefix } from "@bastani/atomic/storage";
2
+ import { addMissingColumns, addOptionalColumn } from "./sql-add-column.js";
3
3
  import type {
4
4
  IntercomBrokerStateOptions,
5
5
  KnownSessionBindingResult,
@@ -8,11 +8,21 @@ import type {
8
8
  KnownSessionRecordResult,
9
9
  KnownSessionState,
10
10
  KnownSessionStore,
11
+ SessionIdReclaim,
12
+ SessionIdReclaimResult,
11
13
  } from "./multinode-state-store.js";
12
14
  import { ensureBrokerSchema } from "./sql-schema.js";
13
15
 
14
16
  type BindingRow = SqliteRow & { return_address: string };
15
17
 
18
+ /**
19
+ * The start every delivery identity of a session with `returnAddress` shares, whatever its group (`deliveryTargetIdentity`
20
+ * writes `["return", returnAddress, group]`).
21
+ */
22
+ function returnIdentityPrefix(returnAddress: string): string {
23
+ return `${JSON.stringify(["return", returnAddress]).slice(0, -1)},`;
24
+ }
25
+
16
26
  type KnownRow = SqliteRow & {
17
27
  target_identity: string;
18
28
  intercom_session_id: string;
@@ -55,6 +65,8 @@ export async function openSqlKnownSessionStore({
55
65
  }: IntercomBrokerStateOptions): Promise<KnownSessionStore> {
56
66
  const table = `${validateStoragePrefix(prefix, dialectOf(database).name)}intercom_known_sessions`;
57
67
  const bindings = `${validateStoragePrefix(prefix, dialectOf(database).name)}intercom_session_bindings`;
68
+ // The lease store's table, which the broker opens first: `reclaim` reads its presence leases.
69
+ const leases = `${validateStoragePrefix(prefix, dialectOf(database).name)}intercom_leases`;
58
70
  const { types, nowMs, greatest } = dialectOf(database);
59
71
  const age = greatest("0", `${nowMs} - recorded_at`);
60
72
  /** `age` at the statement's one reading of the clock, from `WITH clock`. */
@@ -89,10 +101,112 @@ export async function openSqlKnownSessionStore({
89
101
  // #5154: added later: set, by store clock, once the session's broker was retired before it recorded the session
90
102
  // leaving. An older broker ignores it, and its `record` leaves it set.
91
103
  await addMissingColumns(database, table, { lost_at: types.int });
104
+ // #5607: added later: the claim of the registration that last bound the id, written by `bind` when it is given one.
105
+ // An older broker's `bind` leaves it as it is. Without it (a role that may not add it) there is no `reclaim`.
106
+ const claims = await addOptionalColumn(database, bindings, "latest_claim", "TEXT");
92
107
  const columns = `target_identity, intercom_session_id, name_json, atomic_session_id, group_name, groups_json,
93
108
  recipient_purpose, reply_capability, ${age} AS age_ms`;
94
109
 
110
+ /** `bind`'s statements, in the caller's serial transaction on `bindings`. */
111
+ const bindIn = async (
112
+ tx: SqliteExecutor,
113
+ intercomSessionId: string,
114
+ returnAddress: string,
115
+ retentionMs: number,
116
+ claim: string | undefined,
117
+ ): Promise<KnownSessionBindResult> => {
118
+ await tx.run(
119
+ `DELETE FROM ${bindings} WHERE intercom_session_id = ? AND ${nowMs} - recorded_at > ?`,
120
+ intercomSessionId,
121
+ retentionMs,
122
+ );
123
+ // A known-session record kept from before bindings existed holds the id for its return address.
124
+ await tx.run(
125
+ `INSERT INTO ${bindings}(intercom_session_id, return_address, recorded_at)
126
+ SELECT intercom_session_id, COALESCE(atomic_session_id, ''), ${nowMs} FROM ${table}
127
+ WHERE intercom_session_id = ? AND ${age} <= ?
128
+ ORDER BY record_order LIMIT 1
129
+ ON CONFLICT(intercom_session_id) DO NOTHING`,
130
+ intercomSessionId,
131
+ retentionMs,
132
+ );
133
+ // One conditional write: a binding held by another return address is left as it is.
134
+ if (claims) {
135
+ await tx.run(
136
+ `INSERT INTO ${bindings}(intercom_session_id, return_address, recorded_at, latest_claim) VALUES (?, ?, ${nowMs}, ?)
137
+ ON CONFLICT(intercom_session_id) DO UPDATE SET recorded_at = excluded.recorded_at, latest_claim = excluded.latest_claim
138
+ WHERE ${bindings}.return_address = excluded.return_address`,
139
+ intercomSessionId,
140
+ returnAddress,
141
+ claim ?? null,
142
+ );
143
+ } else {
144
+ await tx.run(
145
+ `INSERT INTO ${bindings}(intercom_session_id, return_address, recorded_at) VALUES (?, ?, ${nowMs})
146
+ ON CONFLICT(intercom_session_id) DO UPDATE SET recorded_at = excluded.recorded_at
147
+ WHERE ${bindings}.return_address = excluded.return_address`,
148
+ intercomSessionId,
149
+ returnAddress,
150
+ );
151
+ }
152
+ const bound = await tx.get<BindingRow>(
153
+ `SELECT return_address FROM ${bindings} WHERE intercom_session_id = ?`,
154
+ intercomSessionId,
155
+ );
156
+ if (bound === undefined || bound.return_address === returnAddress) return { status: "bound" };
157
+ return { status: "refused", returnAddress: bound.return_address };
158
+ };
159
+
160
+ const reclaim = async (request: SessionIdReclaim): Promise<SessionIdReclaimResult> => {
161
+ if (request.intercomSessionId.length === 0 || request.freshId.length === 0)
162
+ return { status: "invalid", reason: "empty-session-id" };
163
+ return database.transaction(
164
+ async (tx) => {
165
+ // A session of this return address that another broker presents, in any group, holds the id there.
166
+ const identities = returnIdentityPrefix(request.returnAddress);
167
+ const { changes } = await tx.run(
168
+ `UPDATE ${bindings} SET recorded_at = ${nowMs}
169
+ WHERE intercom_session_id = ? AND return_address = ? AND latest_claim = ? AND ${nowMs} - recorded_at <= ?
170
+ AND NOT EXISTS (
171
+ SELECT 1 FROM ${leases} presence
172
+ WHERE presence.kind = 'presence' AND substr(presence.key, 1, length(?)) = ? AND presence.holder_id IS NOT NULL
173
+ AND presence.holder_id <> ? AND ${greatest("0", `${nowMs} - presence.renewed_at`)} <= ?
174
+ )`,
175
+ request.intercomSessionId,
176
+ request.returnAddress,
177
+ request.claim,
178
+ request.retentionMs,
179
+ // The database measures the prefix itself, in its own characters.
180
+ identities,
181
+ identities,
182
+ request.brokerId,
183
+ request.presenceStaleAfterMs,
184
+ );
185
+ if (changes === 1) return { status: "kept" } as const;
186
+ // A fresh id, made at random for this registration, has no binding or record yet: one write binds it, and only an
187
+ // id already there, bound or recorded, takes `bind`'s whole way.
188
+ const inserted = await tx.run(
189
+ `INSERT INTO ${bindings}(intercom_session_id, return_address, recorded_at)
190
+ SELECT ?, ?, ${nowMs}
191
+ WHERE NOT EXISTS (SELECT 1 FROM ${table} WHERE intercom_session_id = ? AND ${age} <= ?)
192
+ ON CONFLICT(intercom_session_id) DO NOTHING`,
193
+ request.freshId,
194
+ request.returnAddress,
195
+ request.freshId,
196
+ request.retentionMs,
197
+ );
198
+ const binding =
199
+ inserted.changes === 1
200
+ ? ({ status: "bound" } as const)
201
+ : await bindIn(tx, request.freshId, request.returnAddress, request.retentionMs, undefined);
202
+ return { status: "fresh", binding } as const;
203
+ },
204
+ { concurrency: { kind: "serial", domain: bindings } },
205
+ );
206
+ };
207
+
95
208
  return {
209
+ ...(claims ? { reclaim } : {}),
96
210
  async record(session): Promise<KnownSessionRecordResult> {
97
211
  if (session.targetIdentity.length === 0) return { status: "invalid", reason: "empty-target-identity" };
98
212
  if (session.intercomSessionId.length === 0) return { status: "invalid", reason: "empty-session-id" };
@@ -142,41 +256,11 @@ export async function openSqlKnownSessionStore({
142
256
  );
143
257
  return { status: "found", sessions: rows.map(stateFromRow) };
144
258
  },
145
- async bind(intercomSessionId, returnAddress, retentionMs): Promise<KnownSessionBindResult> {
259
+ async bind(intercomSessionId, returnAddress, retentionMs, claim): Promise<KnownSessionBindResult> {
146
260
  if (intercomSessionId.length === 0) return { status: "invalid", reason: "empty-session-id" };
147
- const bound = await database.transaction(
148
- async (tx) => {
149
- await tx.run(
150
- `DELETE FROM ${bindings} WHERE intercom_session_id = ? AND ${nowMs} - recorded_at > ?`,
151
- intercomSessionId,
152
- retentionMs,
153
- );
154
- // A known-session record kept from before bindings existed holds the id for its return address.
155
- await tx.run(
156
- `INSERT INTO ${bindings}(intercom_session_id, return_address, recorded_at)
157
- SELECT intercom_session_id, COALESCE(atomic_session_id, ''), ${nowMs} FROM ${table}
158
- WHERE intercom_session_id = ? AND ${age} <= ?
159
- ORDER BY record_order LIMIT 1
160
- ON CONFLICT(intercom_session_id) DO NOTHING`,
161
- intercomSessionId,
162
- retentionMs,
163
- );
164
- await tx.run(
165
- `INSERT INTO ${bindings}(intercom_session_id, return_address, recorded_at) VALUES (?, ?, ${nowMs})
166
- ON CONFLICT(intercom_session_id) DO UPDATE SET recorded_at = excluded.recorded_at
167
- WHERE ${bindings}.return_address = excluded.return_address`,
168
- intercomSessionId,
169
- returnAddress,
170
- );
171
- return tx.get<BindingRow>(
172
- `SELECT return_address FROM ${bindings} WHERE intercom_session_id = ?`,
173
- intercomSessionId,
174
- );
175
- },
176
- { concurrency: { kind: "serial", domain: bindings } },
177
- );
178
- if (bound === undefined || bound.return_address === returnAddress) return { status: "bound" };
179
- return { status: "refused", returnAddress: bound.return_address };
261
+ return database.transaction((tx) => bindIn(tx, intercomSessionId, returnAddress, retentionMs, claim), {
262
+ concurrency: { kind: "serial", domain: bindings },
263
+ });
180
264
  },
181
265
  async binding(intercomSessionId, retentionMs): Promise<KnownSessionBindingResult> {
182
266
  if (intercomSessionId.length === 0) return { status: "invalid", reason: "empty-session-id" };
@@ -69,12 +69,42 @@ function toRow(record: OutboxRecord): OutboxRow {
69
69
 
70
70
  const isSettled = (record: OutboxRecord): boolean => record.state === "delivered" || record.state === "failed";
71
71
 
72
+ /**
73
+ * #5607: the wake-up hint written with an offline recipient's row failed, so neither was written. The hint is only a
74
+ * hint: the caller writes the row again without it (`withoutFailedWake`).
75
+ */
76
+ export class OutboxWakeFailedError extends Error {
77
+ constructor(cause: unknown) {
78
+ super("The wake-up hint for a queued message could not be written", { cause });
79
+ this.name = "OutboxWakeFailedError";
80
+ }
81
+ }
82
+
72
83
  /** The SQL outbox, which can also enqueue inside a transaction the caller holds. */
73
84
  export interface SqlOutboxStore extends OutboxStore {
74
- /** `enqueue`, as part of `executor`'s transaction: the row is written only when that transaction commits. */
85
+ /**
86
+ * `enqueue`, as part of `executor`'s transaction: the row is written only when that transaction commits. It throws
87
+ * `OutboxWakeFailedError` when only the optional wake-up hint failed; run the transaction again without it then.
88
+ */
75
89
  enqueueIn(executor: SqliteExecutor, request: OutboxEnqueueRequest): Promise<OutboxEnqueueResult>;
76
90
  }
77
91
 
92
+ /**
93
+ * #5607: runs `write` for `request`, and once more for it without its wake-up hint when only that hint failed it.
94
+ */
95
+ export async function withoutFailedWake<T>(
96
+ request: OutboxEnqueueRequest,
97
+ write: (request: OutboxEnqueueRequest) => Promise<T>,
98
+ ): Promise<T> {
99
+ try {
100
+ return await write(request);
101
+ } catch (error) {
102
+ if (!(error instanceof OutboxWakeFailedError)) throw error;
103
+ const { wakeHostWithinMs: _hint, ...withoutHint } = request;
104
+ return write(withoutHint);
105
+ }
106
+ }
107
+
78
108
  /**
79
109
  * S2 outbox on the shared storage adapter's database, in `<prefix>intercom_outbox`. Every read-modify-write runs in
80
110
  * one `BEGIN IMMEDIATE` transaction, and every age is measured on the database clock.
@@ -82,7 +112,7 @@ export interface SqlOutboxStore extends OutboxStore {
82
112
  export async function openSqlOutboxStore(options: IntercomBrokerStateOptions): Promise<SqlOutboxStore> {
83
113
  const { database } = options;
84
114
  const table = `${validateStoragePrefix(options.prefix, dialectOf(database).name)}intercom_outbox`;
85
- const { types, nowMs, greatest } = dialectOf(database);
115
+ const { types, nowMs, greatest, name: dialect } = dialectOf(database);
86
116
  const ageSince = (column: string) => greatest("0", `${nowMs} - ${column}`);
87
117
  // In a serial transaction, so brokers opening a new store at once take turns creating it (#5054).
88
118
  const schema = `
@@ -120,6 +150,10 @@ export async function openSqlOutboxStore(options: IntercomBrokerStateOptions): P
120
150
  CASE WHEN saving_at IS NULL THEN NULL ELSE ${ageSince("saving_at")} END AS saving_age`;
121
151
  const select = (executor: SqliteExecutor, messageId: string) =>
122
152
  executor.get<OutboxRecord>(`SELECT ${columns} FROM ${table} WHERE message_id = ?`, messageId);
153
+ // #5607: the lease store's table, whose presence leases name the broker hosting a recipient, and the wake-up bus's
154
+ // hint log. The broker opens both before it enqueues anything.
155
+ const leases = `${validateStoragePrefix(options.prefix, dialectOf(database).name)}intercom_leases`;
156
+ const wakeups = `${validateStoragePrefix(options.prefix, dialectOf(database).name)}intercom_wakeups`;
123
157
  const undelivered = (executor: SqliteExecutor, targetIdentity: string, limit?: number) =>
124
158
  limit === undefined
125
159
  ? executor.all<OutboxRecord>(
@@ -212,18 +246,44 @@ export async function openSqlOutboxStore(options: IntercomBrokerStateOptions): P
212
246
  ) {
213
247
  return { status: "full", limit: OUTBOX_TARGET_MAX_ROWS, byteLimit: OUTBOX_TARGET_MAX_BYTES } as const;
214
248
  }
215
- await executor.run(
216
- `INSERT INTO ${table}(message_id, target_identity, origin_broker_id, digest, frame_json, state, queued_at,
249
+ const insert = `INSERT INTO ${table}(message_id, target_identity, origin_broker_id, digest, frame_json, state, queued_at,
217
250
  frame_bytes)
218
- VALUES (?, ?, ?, ?, ?, 'queued', ${nowMs}, ?)`,
219
- messageId,
251
+ VALUES (?, ?, ?, ?, ?, 'queued', ${nowMs}, ?)`;
252
+ const values = [messageId, request.targetIdentity, request.originBrokerId, request.digest, frameJson, frameBytes];
253
+ if (request.wakeHostWithinMs === undefined) {
254
+ await executor.run(insert, ...values);
255
+ return { status: "inserted", row: toRow((await select(executor, messageId))!) } as const;
256
+ }
257
+ // #5607: the row comes back from its insert, with the broker holding the recipient's live presence, read in this
258
+ // transaction: a presence lease committed before it is seen here; one committed after it was written after this row,
259
+ // and the registration that wrote it looks for the row next. Only when there is one does a second statement write
260
+ // its wake-up hint, in this transaction too.
261
+ const inserted = (await executor.get<OutboxRecord & { readonly presence_holder: string | null }>(
262
+ `${insert} RETURNING ${columns}, (
263
+ SELECT presence.holder_id FROM ${leases} presence
264
+ WHERE presence.kind = 'presence' AND presence.key = ? AND presence.holder_id IS NOT NULL
265
+ AND ${greatest("0", `${nowMs} - presence.renewed_at`)} <= ?
266
+ ) AS presence_holder`,
267
+ ...values,
220
268
  request.targetIdentity,
221
- request.originBrokerId,
222
- request.digest,
223
- frameJson,
224
- frameBytes,
225
- );
226
- return { status: "inserted", row: toRow((await select(executor, messageId))!) } as const;
269
+ request.wakeHostWithinMs,
270
+ ))!;
271
+ const host = inserted.presence_holder;
272
+ if (host !== null) {
273
+ const hint = async () => {
274
+ // PostgreSQL: never wait behind another session's lock on the hint log; such a hint goes without.
275
+ if (dialect === "postgres") await executor.run(`LOCK TABLE ${wakeups} IN ROW EXCLUSIVE MODE NOWAIT`);
276
+ await executor.run(
277
+ `INSERT INTO ${wakeups}(broker_id, reason, version, created_at) VALUES (?, 'outbox', ?, ${nowMs})`,
278
+ host,
279
+ Number(inserted.sequence),
280
+ );
281
+ };
282
+ await hint().catch((cause: unknown) => {
283
+ throw new OutboxWakeFailedError(cause);
284
+ });
285
+ }
286
+ return { status: "inserted", row: toRow(inserted), ...(host === null ? {} : { woke: host }) } as const;
227
287
  };
228
288
 
229
289
  return {
@@ -232,7 +292,9 @@ export async function openSqlOutboxStore(options: IntercomBrokerStateOptions): P
232
292
  if (request.targetIdentity.length === 0) return invalid("empty-target-identity");
233
293
  if (request.originBrokerId.length === 0) return invalid("empty-broker-id");
234
294
  if (request.frame.message.id.length === 0) return invalid("empty-message-id");
235
- return database.transaction((executor) => enqueueIn(executor, request));
295
+ return withoutFailedWake(request, (attempt) =>
296
+ database.transaction((executor) => enqueueIn(executor, attempt)),
297
+ );
236
298
  },
237
299
 
238
300
  async claim(request) {
@@ -1,5 +1,6 @@
1
1
  import { readFileSync, unlinkSync, writeFileSync } from "node:fs";
2
2
  import { readFile } from "node:fs/promises";
3
+ import { createRequire } from "node:module";
3
4
 
4
5
  /**
5
6
  * A spawned broker announces its real pid, so a parent that cannot watch the process it spawned can tell a broker that
@@ -16,9 +17,14 @@ import { readFile } from "node:fs/promises";
16
17
  * The parent hands the broker the file and token through the environment, so a broker started any other way (a test,
17
18
  * an older parent) announces nothing. The token ties a file to the spawn that asked for it: a broker that is retiring
18
19
  * and a replacement that is starting never read each other's announcement as their own.
20
+ *
21
+ * #6133: a parent that asks for it (`BROKER_ANNOUNCE_START_ENV`) also gets when the broker process was created, as
22
+ * Windows records it, so that it can tell the broker apart from a later process Windows gives the same pid to. Without
23
+ * the request the file holds exactly what earlier versions wrote, so an older parent still reads it.
19
24
  */
20
25
  export const BROKER_ANNOUNCE_PATH_ENV = "ATOMIC_INTERCOM_BROKER_ANNOUNCE_PATH";
21
26
  export const BROKER_ANNOUNCE_TOKEN_ENV = "ATOMIC_INTERCOM_BROKER_ANNOUNCE_TOKEN";
27
+ export const BROKER_ANNOUNCE_START_ENV = "ATOMIC_INTERCOM_BROKER_ANNOUNCE_START";
22
28
 
23
29
  /** The file and token one spawn asks its broker to announce itself with. */
24
30
  export interface BrokerStartAnnouncement {
@@ -26,8 +32,16 @@ export interface BrokerStartAnnouncement {
26
32
  readonly token: string;
27
33
  }
28
34
 
35
+ /** What a broker announced: its pid, and when it was created where it could read that and was asked to. */
36
+ export interface AnnouncedBroker {
37
+ readonly pid: number;
38
+ /** The process's creation time, as the decimal count of 100-nanosecond intervals since 1601-01-01 UTC. */
39
+ readonly started?: string;
40
+ }
41
+
29
42
  /** Tokens go in the file and the environment unquoted, so only plain characters are accepted. */
30
43
  const TOKEN_PATTERN = /^[A-Za-z0-9-]+$/u;
44
+ const CREATION_TIME_PATTERN = /^[1-9]\d*$/u;
31
45
 
32
46
  /** The announcement the environment asks this process for, or undefined when it asks for none. */
33
47
  function requested(env: NodeJS.ProcessEnv): BrokerStartAnnouncement | undefined {
@@ -37,12 +51,38 @@ function requested(env: NodeJS.ProcessEnv): BrokerStartAnnouncement | undefined
37
51
  return { path, token };
38
52
  }
39
53
 
40
- /** Broker side: write `<token> <pid>` where the parent asked. A failure to write must never keep the broker from starting. */
41
- export function announceBrokerStart(env: NodeJS.ProcessEnv = process.env, pid: number = process.pid): void {
54
+ /**
55
+ * When this process was created, as Windows records it, read by Atomic's native module; undefined elsewhere, or where
56
+ * that module cannot be loaded. Read synchronously: the broker announces itself before it loads anything else.
57
+ */
58
+ function ownCreationTime(): string | undefined {
59
+ if (process.platform !== "win32") return undefined;
60
+ try {
61
+ const native = createRequire(import.meta.url)("@bastani/atomic-natives") as {
62
+ processCreationTime?: (pid: number) => string | null;
63
+ };
64
+ const created = native.processCreationTime?.(process.pid);
65
+ return typeof created === "string" && CREATION_TIME_PATTERN.test(created) ? created : undefined;
66
+ } catch {
67
+ return undefined;
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Broker side: write `<token> <pid>` where the parent asked, followed by ` <creation time>` when the parent asked for it
73
+ * too and it can be read. A failure to write must never keep the broker from starting.
74
+ */
75
+ export function announceBrokerStart(
76
+ env: NodeJS.ProcessEnv = process.env,
77
+ pid: number = process.pid,
78
+ readStart: () => string | undefined = ownCreationTime,
79
+ ): void {
42
80
  const announcement = requested(env);
43
81
  if (announcement === undefined) return;
82
+ const started = env[BROKER_ANNOUNCE_START_ENV] === "1" ? readStart() : undefined;
83
+ const witness = started !== undefined && CREATION_TIME_PATTERN.test(started) ? ` ${started}` : "";
44
84
  try {
45
- writeFileSync(announcement.path, `${announcement.token} ${pid}\n`, { mode: 0o600 });
85
+ writeFileSync(announcement.path, `${announcement.token} ${pid}${witness}\n`, { mode: 0o600 });
46
86
  } catch {
47
87
  // The parent then waits out its start-up budget, as it did before broker announcements existed.
48
88
  }
@@ -53,7 +93,7 @@ export function retractBrokerStartAnnouncement(env: NodeJS.ProcessEnv = process.
53
93
  const announcement = requested(env);
54
94
  if (announcement === undefined) return;
55
95
  // Only the broker's own announcement: a stale file from another broker is the parent's to replace.
56
- if (readBrokerStartAnnouncement(announcement) !== pid) return;
96
+ if (readBrokerStartAnnouncement(announcement)?.pid !== pid) return;
57
97
  try {
58
98
  unlinkSync(announcement.path);
59
99
  } catch {
@@ -61,17 +101,22 @@ export function retractBrokerStartAnnouncement(env: NodeJS.ProcessEnv = process.
61
101
  }
62
102
  }
63
103
 
64
- function parseBrokerStartAnnouncement(text: string, announcement: BrokerStartAnnouncement): number | undefined {
104
+ function parseBrokerStartAnnouncement(
105
+ text: string,
106
+ announcement: BrokerStartAnnouncement,
107
+ ): AnnouncedBroker | undefined {
65
108
  // A complete line only: a write still in progress is read as nothing yet.
66
109
  if (!text.endsWith("\n")) return undefined;
67
- const [token, pid, ...rest] = text.trim().split(/\s+/u);
110
+ const [token, pid, started, ...rest] = text.trim().split(/\s+/u);
68
111
  if (token !== announcement.token || rest.length > 0) return undefined;
112
+ if (started !== undefined && !CREATION_TIME_PATTERN.test(started)) return undefined;
69
113
  const parsed = Number(pid);
70
- return Number.isInteger(parsed) && parsed > 0 ? parsed : undefined;
114
+ if (!Number.isInteger(parsed) || parsed <= 0) return undefined;
115
+ return started === undefined ? { pid: parsed } : { pid: parsed, started };
71
116
  }
72
117
 
73
- /** Parent side: the pid this spawn's broker announced, or undefined until it has, or when the file is another spawn's. */
74
- export function readBrokerStartAnnouncement(announcement: BrokerStartAnnouncement): number | undefined {
118
+ /** Parent side: what this spawn's broker announced, or undefined until it has, or when the file is another spawn's. */
119
+ export function readBrokerStartAnnouncement(announcement: BrokerStartAnnouncement): AnnouncedBroker | undefined {
75
120
  let text: string;
76
121
  try {
77
122
  text = readFileSync(announcement.path, "utf-8");
@@ -83,7 +128,7 @@ export function readBrokerStartAnnouncement(announcement: BrokerStartAnnouncemen
83
128
 
84
129
  export async function readBrokerStartAnnouncementAsync(
85
130
  announcement: BrokerStartAnnouncement,
86
- ): Promise<number | undefined> {
131
+ ): Promise<AnnouncedBroker | undefined> {
87
132
  let text: string;
88
133
  try {
89
134
  text = await readFile(announcement.path, "utf-8");
@@ -4,11 +4,14 @@ export const BROKER_WATCHDOG_INTERVAL_MS = 2_000;
4
4
  export const BROKER_WATCHDOG_PROBE_TIMEOUT_MS = 10_000;
5
5
  export const BROKER_WATCHDOG_SILENCE_MS = 60_000;
6
6
 
7
+ // Node evaluates this source as an ES module, which has no require, when --input-type=module reaches the worker (from
8
+ // NODE_OPTIONS, or the process's flags under the permission model) (#6110); `builtin` loads Node's modules either way.
7
9
  const WATCHDOG_SOURCE = `
8
- const { workerData } = require("node:worker_threads");
9
- const { connect } = require("node:net");
10
- const { randomUUID } = require("node:crypto");
11
- const { writeFileSync } = require("node:fs");
10
+ const builtin = typeof require === "function" ? require : (id) => process.getBuiltinModule(id);
11
+ const { workerData } = builtin("node:worker_threads");
12
+ const { connect } = builtin("node:net");
13
+ const { randomUUID } = builtin("node:crypto");
14
+ const { writeFileSync } = builtin("node:fs");
12
15
  let lastHealthyAt = performance.now();
13
16
  let consecutiveFailures = 0;
14
17
 
@@ -87,9 +90,9 @@ export function startBrokerWatchdog(socketPath: string, instanceId: string, logP
87
90
  try {
88
91
  const worker = new Worker(WATCHDOG_SOURCE, {
89
92
  eval: true,
90
- // None of the process's flags: an `--input-type=module` would make this CommonJS source a module, and the
91
- // process's preloads would run again in the watchdog. Under Node's permission model a worker is restricted
92
- // only by the flags it starts with, so there it keeps them.
93
+ // None of the process's flags: the process's preloads would run again in the watchdog, and its
94
+ // `--input-type=module` would make the source a module. Under Node's permission model a worker is
95
+ // restricted only by the flags it starts with, so there it keeps them.
93
96
  execArgv: process.permission === undefined ? [] : undefined,
94
97
  workerData: {
95
98
  socketPath,