@bastani/atomic 0.10.11-alpha.1 → 0.10.11-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +155 -0
- package/README.md +5 -8
- package/dist/_shared/dist-KOELCESV.js +2 -2
- package/dist/_shared/fork-row-index-UAUAQRD5.js +2 -2
- package/dist/_shared/lib-4PES5P4V.js +2 -2
- package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js +9 -9
- package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +8 -8
- package/dist/_shared/mode-shared-2QJSCUM6.js +9 -9
- package/dist/_shared/p0-cli-BB45ZQY6.js +46 -67
- package/dist/_shared/p0-cli-BB45ZQY6.js.map +1 -1
- package/dist/_shared/p1a-prewarm-XIU57JHF.js +7 -8
- package/dist/_shared/p1a-prewarm-XIU57JHF.js.map +1 -1
- package/dist/_shared/{p1b-elig-ENCUPKKJ.js → p1b-elig-TVB5CBLL.js} +14 -2
- package/dist/_shared/{p1b-elig-ENCUPKKJ.js.map → p1b-elig-TVB5CBLL.js.map} +1 -1
- package/dist/_shared/runtime-O4KCSG4C.js +5 -5
- package/dist/_shared/{sdk-AVFII7NK.js → sdk-B5LEWGGI.js} +2633 -488
- package/dist/_shared/sdk-B5LEWGGI.js.map +1 -0
- package/dist/_shared/session-data-AHTU7IS6.js +3 -3
- package/dist/_shared/session-manager-list-2BLEEZVG.js +1 -1
- package/dist/_shared/settings-startup-8-DNZ2RIHM.js +1 -1
- package/dist/_shared/{settings-startup-4QCVCI53.js → settings-startup-HXQLSUWM.js} +8 -2
- package/dist/_shared/settings-startup-HXQLSUWM.js.map +1 -0
- package/dist/_shared/{startup-0-0-57KF6LZH.js → startup-0-0-EZUKEP4Q.js} +436 -51
- package/dist/_shared/startup-0-0-EZUKEP4Q.js.map +1 -0
- package/dist/_shared/{startup-0-1-GIY75K46.js → startup-0-1-RTOHJFDJ.js} +240 -279
- package/dist/_shared/startup-0-1-RTOHJFDJ.js.map +1 -0
- package/dist/_shared/{startup-0-2-27OWFHV6.js → startup-0-2-5QHPCEON.js} +254 -46
- package/dist/_shared/startup-0-2-5QHPCEON.js.map +1 -0
- package/dist/_shared/{startup-0-3-T4DVFYWX.js → startup-0-3-4ZYBYQNM.js} +273 -14
- package/dist/_shared/startup-0-3-4ZYBYQNM.js.map +1 -0
- package/dist/_shared/startup-0-4-ZMJV547D.js +14 -9
- package/dist/_shared/startup-0-4-ZMJV547D.js.map +1 -1
- package/dist/_shared/startup-0-5-YDW47QX6.js +10 -10
- package/dist/_shared/startup-0-5-YDW47QX6.js.map +1 -1
- package/dist/_shared/startup-10-0-TDQNBDDL.js +9 -9
- package/dist/_shared/startup-12-0-55AAXBJZ.js +3 -3
- package/dist/_shared/startup-14-0-MLX36RBG.js +6 -7
- package/dist/_shared/startup-14-0-MLX36RBG.js.map +1 -1
- package/dist/_shared/startup-15-0-YUKGAC63.js +7 -7
- package/dist/_shared/startup-15-1-4S7FLMNG.js +22 -30
- package/dist/_shared/startup-15-1-4S7FLMNG.js.map +1 -1
- package/dist/_shared/startup-8-0-34CBHPZH.js +455 -102
- package/dist/_shared/startup-8-0-34CBHPZH.js.map +1 -1
- package/dist/_shared/{startup-8-1-X32CYSXB.js → startup-8-1-LZFXSD3Z.js} +162 -1184
- package/dist/_shared/startup-8-1-LZFXSD3Z.js.map +1 -0
- package/dist/_shared/storage-command-7MGXLJGZ.js +189 -36
- package/dist/_shared/storage-command-7MGXLJGZ.js.map +1 -1
- package/dist/_shared/storage-status-K7PX2RXA.js +557 -0
- package/dist/_shared/storage-status-K7PX2RXA.js.map +1 -0
- package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +2 -2
- package/dist/_shared/team-DOQKICG2.js +376 -0
- package/dist/_shared/team-DOQKICG2.js.map +1 -0
- package/dist/_shared/tree-outline-MUEPK5B7.js +3 -3
- package/dist/builtin/intercom/CHANGELOG.md +34 -0
- package/dist/builtin/intercom/README.md +2 -2
- package/dist/builtin/intercom/broker/broker.bundle.mjs +1091 -344
- package/dist/builtin/intercom/broker/database-wake-up-bus.ts +243 -64
- package/dist/builtin/intercom/broker/multinode-router.ts +154 -22
- package/dist/builtin/intercom/broker/multinode-state-store.ts +30 -3
- package/dist/builtin/intercom/broker/open-broker-state.ts +14 -0
- package/dist/builtin/intercom/broker/sql-add-column.ts +29 -5
- package/dist/builtin/intercom/broker/sql-ended-child-queue-store.ts +3 -2
- package/dist/builtin/intercom/broker/sql-held-send-store.ts +2 -2
- package/dist/builtin/intercom/broker/sql-known-session-store.ts +43 -19
- package/dist/builtin/intercom/broker/sql-lease-store.ts +208 -101
- package/dist/builtin/intercom/broker/sql-outbox-store.ts +516 -102
- package/dist/builtin/intercom/broker/sql-pending-ask-store.ts +8 -5
- package/dist/builtin/intercom/broker/sql-released-ask-store.ts +12 -5
- package/dist/builtin/intercom/broker/sql-restoring-reservation-store.ts +24 -7
- package/dist/builtin/intercom/broker/sql-retention.ts +141 -0
- package/dist/builtin/intercom/broker/sql-schema.ts +199 -27
- package/dist/builtin/intercom/broker/sql-sent-message-authority.ts +74 -19
- package/dist/builtin/intercom/broker/wake-up-bus.ts +6 -0
- package/dist/builtin/intercom/index.bundle.mjs +2 -3
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/index.bundle.mjs +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/CHANGELOG.md +6 -0
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/src/extension/index.bundle.mjs +1 -1
- package/dist/builtin/web-access/index.bundle.mjs +2 -10
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/bun/bundled-sqlite.js +41 -0
- package/dist/bun/bundled-sqlite.js.map +1 -0
- package/dist/cli/args.js +2 -2
- package/dist/cli/auth-cli.code.js +6 -6
- package/dist/cli/auth-cli.impl.js +3 -3
- package/dist/cli/auth-cli.js +3 -3
- package/dist/cli/blueprint-command.code.js +4 -4
- package/dist/cli/blueprint-command.impl.js +3 -3
- package/dist/cli/blueprint-command.js +3 -3
- package/dist/cli/intercom-command.code.js +4 -4
- package/dist/cli/intercom-command.code.js.map +1 -1
- package/dist/cli/intercom-command.impl.js +3 -3
- package/dist/cli/intercom-command.js +3 -3
- package/dist/cli/list-models.code.js +2 -2
- package/dist/cli/list-models.impl.js +3 -3
- package/dist/cli/list-models.js +3 -3
- package/dist/cli/session-picker.code.js +2 -2
- package/dist/cli/session-picker.impl.js +3 -3
- package/dist/cli/session-picker.js +3 -3
- package/dist/cli/startup-ui.code.js +2 -2
- package/dist/cli/startup-ui.impl.js +3 -3
- package/dist/cli/startup-ui.js +3 -3
- package/dist/cli/storage-command.impl.js +3 -3
- package/dist/cli/storage-command.js +3 -3
- package/dist/cli/storage-search-session.code.js +3 -3
- package/dist/cli/storage-search-session.impl.js +3 -3
- package/dist/cli/storage-search-session.js +3 -3
- package/dist/cli/team-command.code.js +20 -114
- package/dist/cli/team-command.code.js.map +1 -1
- package/dist/cli/team-command.impl.js +3 -3
- package/dist/cli/team-command.js +3 -3
- package/dist/cli.js +5 -18
- package/dist/cli.js.map +1 -1
- package/dist/core/agent-rehydrate-errors.d.ts +4 -9
- package/dist/core/compaction/compactor.d.ts +3 -3
- package/dist/core/debug.code.js +1 -1
- package/dist/core/debug.impl.js +3 -3
- package/dist/core/debug.js +3 -3
- package/dist/core/export-html/card-projection.impl.js +3 -3
- package/dist/core/export-html/card-projection.js +3 -3
- package/dist/core/export-html/index.code.js +2 -2
- package/dist/core/export-html/index.impl.js +3 -3
- package/dist/core/export-html/index.js +3 -3
- package/dist/core/export-html/json-spans.impl.js +3 -3
- package/dist/core/export-html/json-spans.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.code.js +1 -1
- package/dist/core/export-html/oversized-render-inputs.impl.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.js +3 -3
- package/dist/core/export-html/tool-renderer.impl.js +3 -3
- package/dist/core/export-html/tool-renderer.js +3 -3
- package/dist/core/extensions/jiti-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-loader.js +3 -3
- package/dist/core/extensions/jiti-static-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-static-loader.js +3 -3
- package/dist/core/extensions/loader-host-modules.code.js +1 -1
- package/dist/core/extensions/loader-host-modules.impl.js +3 -3
- package/dist/core/extensions/loader-host-modules.js +3 -3
- package/dist/core/extensions/loader-virtual-modules.js +1 -1
- package/dist/core/extensions/runner-context.js +3 -3
- package/dist/core/intercom-broker-control-host.code.js +2 -2
- package/dist/core/intercom-broker-control-host.impl.js +3 -3
- package/dist/core/intercom-broker-control-host.js +3 -3
- package/dist/core/intercom-broker-control.code.js +3 -3
- package/dist/core/intercom-broker-control.impl.js +3 -3
- package/dist/core/intercom-broker-control.js +3 -3
- package/dist/core/package-manager-storage-adapters.code.js +1 -1
- package/dist/core/package-manager-storage-adapters.impl.js +3 -3
- package/dist/core/package-manager-storage-adapters.js +3 -3
- package/dist/core/process-start-token.d.ts +8 -0
- package/dist/core/process-tree-containment.d.ts +16 -0
- package/dist/core/sdk-types.d.ts +1 -2
- package/dist/core/sdk-types.js.map +1 -1
- package/dist/core/sdk.code.js +4 -4
- package/dist/core/sdk.impl.js +3 -3
- package/dist/core/sdk.js +3 -3
- package/dist/core/session-manager-core.js +27 -15
- package/dist/core/session-manager-core.js.map +1 -1
- package/dist/core/session-manager-list.impl.js +3 -3
- package/dist/core/session-manager-list.js +3 -3
- package/dist/core/session-manager.code.js +3 -3
- package/dist/core/session-manager.impl.js +3 -3
- package/dist/core/session-manager.js +3 -3
- package/dist/core/session-open-markers.js +3 -0
- package/dist/core/session-open-markers.js.map +1 -0
- package/dist/core/session-storage.code.js +1 -1
- package/dist/core/session-storage.impl.js +3 -3
- package/dist/core/session-storage.js +3 -3
- package/dist/core/settings-schema.d.ts +0 -3
- package/dist/core/storage/added-exports-0-10-11.js +19 -0
- package/dist/core/storage/added-exports-0-10-11.js.map +1 -0
- package/dist/core/storage/added-exports.js +3 -0
- package/dist/core/storage/added-exports.js.map +1 -1
- package/dist/core/storage/creation-time.js +105 -0
- package/dist/core/storage/creation-time.js.map +1 -0
- package/dist/core/storage/database.d.ts +56 -5
- package/dist/core/storage/database.js.map +1 -1
- package/dist/core/storage/dialect.d.ts +1 -1
- package/dist/core/storage/dialect.js +1 -1
- package/dist/core/storage/dialect.js.map +1 -1
- package/dist/core/storage/errors.d.ts +13 -13
- package/dist/core/storage/errors.js +10 -16
- package/dist/core/storage/errors.js.map +1 -1
- package/dist/core/storage/fenced-paths.d.ts +11 -0
- package/dist/core/storage/fenced-paths.js +11 -0
- package/dist/core/storage/fenced-paths.js.map +1 -1
- package/dist/core/storage/file-identity.d.ts +2 -0
- package/dist/core/storage/file-identity.js +24 -8
- package/dist/core/storage/file-identity.js.map +1 -1
- package/dist/core/storage/index.d.ts +4 -3
- package/dist/core/storage/index.js +5 -3
- package/dist/core/storage/index.js.map +1 -1
- package/dist/core/storage/libsql-removed.d.ts +71 -0
- package/dist/core/storage/libsql-removed.js +123 -0
- package/dist/core/storage/libsql-removed.js.map +1 -0
- package/dist/core/storage/migrations.d.ts +81 -8
- package/dist/core/storage/migrations.js +386 -47
- package/dist/core/storage/migrations.js.map +1 -1
- package/dist/core/storage/node-sqlite-file-gate.js +0 -15
- package/dist/core/storage/node-sqlite-file-gate.js.map +1 -1
- package/dist/core/storage/node-sqlite-thread.js +36 -12
- package/dist/core/storage/node-sqlite-thread.js.map +1 -1
- package/dist/core/storage/node-sqlite.js +135 -3
- package/dist/core/storage/node-sqlite.js.map +1 -1
- package/dist/core/storage/offline-snapshot-file.impl.js +3 -3
- package/dist/core/storage/offline-snapshot-file.js +3 -3
- package/dist/core/storage/prefix.d.ts +1 -1
- package/dist/core/storage/prefix.js +14 -2
- package/dist/core/storage/prefix.js.map +1 -1
- package/dist/core/storage/process-selection.d.ts +5 -5
- package/dist/core/storage/provider.js +2 -2
- package/dist/core/storage/schema-catalog.d.ts +82 -5
- package/dist/core/storage/schema-catalog.js +244 -34
- package/dist/core/storage/schema-catalog.js.map +1 -1
- package/dist/core/storage/selection.d.ts +7 -82
- package/dist/core/storage/selection.js +20 -235
- package/dist/core/storage/selection.js.map +1 -1
- package/dist/core/storage/session/branch-fork-source.code.js +9 -8
- package/dist/core/storage/session/branch-fork-source.code.js.map +1 -1
- package/dist/core/storage/session/branch-fork-source.impl.js +3 -3
- package/dist/core/storage/session/branch-fork-source.js +3 -3
- package/dist/core/storage/session/card-view.impl.js +3 -3
- package/dist/core/storage/session/card-view.js +3 -3
- package/dist/core/storage/session/child-reader.code.js +4 -4
- package/dist/core/storage/session/child-reader.impl.js +3 -3
- package/dist/core/storage/session/child-reader.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.impl.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.js +3 -3
- package/dist/core/storage/session/errors.js +1 -1
- package/dist/core/storage/session/expanded-child-details.code.js +3 -3
- package/dist/core/storage/session/expanded-child-details.impl.js +3 -3
- package/dist/core/storage/session/expanded-child-details.js +3 -3
- package/dist/core/storage/session/fork-detach.d.ts +118 -0
- package/dist/core/storage/session/fork-row-bytes.impl.js +3 -3
- package/dist/core/storage/session/fork-row-bytes.js +3 -3
- package/dist/core/storage/session/fork-row-index.impl.js +3 -3
- package/dist/core/storage/session/fork-row-index.js +3 -3
- package/dist/core/storage/session/fork-schema.js +53 -0
- package/dist/core/storage/session/fork-schema.js.map +1 -0
- package/dist/core/storage/session/fork-segments.d.ts +58 -0
- package/dist/core/storage/session/fork-staging.code.js +5 -8
- package/dist/core/storage/session/fork-staging.code.js.map +1 -1
- package/dist/core/storage/session/fork-staging.impl.js +3 -3
- package/dist/core/storage/session/fork-staging.js +3 -3
- package/dist/core/storage/session/fork-sweep.js +61 -0
- package/dist/core/storage/session/fork-sweep.js.map +1 -0
- package/dist/core/storage/session/fork-window.code.js +4 -4
- package/dist/core/storage/session/fork-window.impl.js +3 -3
- package/dist/core/storage/session/fork-window.js +3 -3
- package/dist/core/storage/session/history-pages.impl.js +3 -3
- package/dist/core/storage/session/history-pages.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.impl.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.js +3 -3
- package/dist/core/storage/session/jsonl-session-store.d.ts +42 -2
- package/dist/core/storage/session/jsonl-session-store.impl.js +3 -3
- package/dist/core/storage/session/jsonl-session-store.js +3 -3
- package/dist/core/storage/session/memory-session-store.impl.js +3 -3
- package/dist/core/storage/session/memory-session-store.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.code.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.impl.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.js +3 -3
- package/dist/core/storage/session/released-history-files.code.js +4 -4
- package/dist/core/storage/session/released-history-files.impl.js +3 -3
- package/dist/core/storage/session/released-history-files.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.code.js +1 -1
- package/dist/core/storage/session/resident-fork-rows.impl.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.js +3 -3
- package/dist/core/storage/session/session-resume.impl.js +3 -3
- package/dist/core/storage/session/session-resume.js +3 -3
- package/dist/core/storage/session/session-search-index.d.ts +43 -4
- package/dist/core/storage/session/snapshot-cut.impl.js +3 -3
- package/dist/core/storage/session/snapshot-cut.js +3 -3
- package/dist/core/storage/session/snapshot-history.code.js +4 -4
- package/dist/core/storage/session/snapshot-history.impl.js +3 -3
- package/dist/core/storage/session/snapshot-history.js +3 -3
- package/dist/core/storage/session/sql-session-store.code.js +1 -1
- package/dist/core/storage/session/sql-session-store.d.ts +394 -20
- package/dist/core/storage/session/sql-session-store.impl.js +4 -4
- package/dist/core/storage/session/sql-session-store.js +4 -4
- package/dist/core/storage/session/tree-outline.impl.js +3 -3
- package/dist/core/storage/session/tree-outline.js +3 -3
- package/dist/core/storage/session/tree-presentation.impl.js +3 -3
- package/dist/core/storage/session/tree-presentation.js +3 -3
- package/dist/core/storage/session/types.d.ts +22 -0
- package/dist/core/storage/sqlite-facade.d.ts +23 -2
- package/dist/core/storage/sqlite-facade.js +34 -0
- package/dist/core/storage/sqlite-facade.js.map +1 -1
- package/dist/core/storage/storage-status.code.js +1 -1
- package/dist/core/storage/storage-status.impl.js +3 -3
- package/dist/core/storage/storage-status.js +3 -3
- package/dist/core/storage/team.code.js +1 -1
- package/dist/core/storage/team.d.ts +26 -115
- package/dist/core/storage/team.impl.js +3 -3
- package/dist/core/storage/team.js +3 -3
- package/dist/core/structured-output/index.code.js +1 -1
- package/dist/core/structured-output/index.impl.js +3 -3
- package/dist/core/structured-output/index.js +3 -3
- package/dist/core/tasks/task-completion-entry.impl.js +3 -3
- package/dist/core/tasks/task-completion-entry.js +3 -3
- package/dist/core/tasks/task-file-history.code.js +4 -4
- package/dist/core/tasks/task-file-history.impl.js +3 -3
- package/dist/core/tasks/task-file-history.js +3 -3
- package/dist/core/tasks/task-reference-archive.code.js +5 -5
- package/dist/core/tasks/task-reference-archive.js +3 -3
- package/dist/core/tasks/task-reference-identity.js +3 -3
- package/dist/core/tasks/task-reference-stream.impl.js +3 -3
- package/dist/core/tasks/task-reference-stream.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.impl.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.js +3 -3
- package/dist/core/tasks/task-view-source.code.js +1 -1
- package/dist/core/tasks/task-view-source.impl.js +3 -3
- package/dist/core/tasks/task-view-source.js +3 -3
- package/dist/core/tools/history-session.d.ts +2 -0
- package/dist/core/tools/history-tasks.d.ts +3 -2
- package/dist/core/tools/read-url.code.js +4 -4
- package/dist/core/tools/read-url.impl.js +3 -3
- package/dist/core/tools/read-url.js +3 -3
- package/dist/core/worktree-snapshot/blob-store.d.ts +71 -3
- package/dist/core/worktree-snapshot/blob-store.js +575 -67
- package/dist/core/worktree-snapshot/blob-store.js.map +1 -1
- package/dist/core/worktree-snapshot/errors.code.js +3 -3
- package/dist/core/worktree-snapshot/errors.impl.js +3 -3
- package/dist/core/worktree-snapshot/errors.js +3 -3
- package/dist/core/worktree-snapshot/fork-blob-guard.js +81 -0
- package/dist/core/worktree-snapshot/fork-blob-guard.js.map +1 -0
- package/dist/core/worktree-snapshot/fork-snapshot-index.js +162 -0
- package/dist/core/worktree-snapshot/fork-snapshot-index.js.map +1 -0
- package/dist/core/worktree-snapshot/session-index-reset.code.js +4 -0
- package/dist/core/worktree-snapshot/session-index-reset.code.js.map +1 -1
- package/dist/core/worktree-snapshot/session-index-reset.impl.js +3 -3
- package/dist/core/worktree-snapshot/session-index-reset.js +3 -3
- package/dist/extensions/codemode/execute.code.js +2 -2
- package/dist/extensions/codemode/execute.impl.js +3 -3
- package/dist/extensions/codemode/execute.js +3 -3
- package/dist/extensions/codemode/renderer.code.js +2 -2
- package/dist/extensions/codemode/renderer.impl.js +3 -3
- package/dist/extensions/codemode/renderer.js +3 -3
- package/dist/extensions/mcp/cli.code.js +3 -3
- package/dist/extensions/mcp/cli.impl.js +3 -3
- package/dist/extensions/mcp/cli.js +3 -3
- package/dist/extensions/mcp/cli.lazy.code.js +2 -2
- package/dist/extensions/mcp/cli.lazy.impl.js +3 -3
- package/dist/extensions/mcp/cli.lazy.js +3 -3
- package/dist/extensions/mcp/runtime.code.js +1 -1
- package/dist/extensions/mcp/runtime.impl.js +3 -3
- package/dist/extensions/mcp/runtime.js +3 -3
- package/dist/index.d.ts +3 -1
- package/dist/index.js +8 -8
- package/dist/main-first-time-setup.code.js +2 -2
- package/dist/main-first-time-setup.impl.js +3 -3
- package/dist/main-first-time-setup.js +3 -3
- package/dist/main.js +1 -1
- package/dist/modes/interactive/components/stored-tree-view.code.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.impl.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.impl.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.js +3 -3
- package/dist/modes/interactive/startup-shell-eligibility.js +1 -1
- package/dist/modes/interactive-engine/engine-prewarm.d.ts +1 -1
- package/dist/modes/print-mode.js +1 -1
- package/dist/package-manager-cli.code.js +7 -7
- package/dist/package-manager-cli.impl.js +3 -3
- package/dist/package-manager-cli.js +3 -3
- package/dist/rpc-entry.js +1 -1
- package/dist/startup-files.json +1 -1
- package/dist/startup-stages/10.js +1 -1
- package/dist/startup-stages/11.js +1 -1
- package/dist/startup-stages/12.js +1 -1
- package/dist/startup-stages/2.js +2 -3
- package/dist/startup-stages/3.js +1 -3
- package/dist/startup-stages/4.js +1 -2
- package/dist/startup-stages/5.js +1 -1
- package/dist/startup-stages/6.js +5 -7
- package/dist/startup-stages/7.js +3 -6
- package/dist/startup-stages/8.js +7 -2
- package/dist/startup-stages/9.js +1 -7
- package/dist/startup-stages/stages.json +1 -1
- package/dist/utils/compile-cache-deferral.js +8 -13
- package/dist/utils/compile-cache-deferral.js.map +1 -1
- package/dist/utils/compile-cache-warmup-host.js +3 -4
- package/dist/utils/compile-cache-warmup-host.js.map +1 -1
- package/dist/utils/compile-cache.js +12 -100
- package/dist/utils/compile-cache.js.map +1 -1
- package/dist/utils/startup-database-spare.code.js +3 -3
- package/dist/utils/startup-database-spare.impl.js +3 -3
- package/dist/utils/startup-database-spare.js +3 -3
- package/dist/utils/tools-manager.code.js +1 -1
- package/dist/utils/tools-manager.impl.js +3 -3
- package/dist/utils/tools-manager.js +3 -3
- package/dist/utils/worker-threads.js +2 -15
- package/dist/utils/worker-threads.js.map +1 -1
- package/docs/build.md +3 -3
- package/docs/changelog.mdx +1421 -69
- package/docs/compaction/reference.md +5 -5
- package/docs/compaction.md +7 -9
- package/docs/containerization.md +2 -2
- package/docs/custom-provider/api-reference.md +1 -1
- package/docs/docs.json +37 -10
- package/docs/environment-variables.md +0 -2
- package/docs/extensions/api-reference.md +4 -4
- package/docs/getting-started/installation.md +3 -0
- package/docs/guides/configuration.md +1 -1
- package/docs/guides/non-interactive.md +1 -1
- package/docs/guides/resume-on-any-machine.md +14 -22
- package/docs/guides/team-setup.md +56 -83
- package/docs/guides.md +2 -4
- package/docs/herdr.md +1 -1
- package/docs/index.md +1 -1
- package/docs/intercom/operations.md +26 -21
- package/docs/intercom/reference.md +2 -2
- package/docs/intercom.md +1 -1
- package/docs/libraries/storage-postgres.md +9 -9
- package/docs/libraries.md +2 -3
- package/docs/models-configuration.md +2 -2
- package/docs/models.md +2 -3
- package/docs/packages/authoring.md +7 -7
- package/docs/packages.md +1 -1
- package/docs/programmatic.md +2 -2
- package/docs/quickstart.md +1 -1
- package/docs/reference/cli.md +6 -11
- package/docs/reference.md +2 -2
- package/docs/rpc/extension-ui.md +2 -2
- package/docs/sdk/reference.md +5 -4
- package/docs/sdk/storage.md +8 -92
- package/docs/sdk/structured-decisions.md +1 -1
- package/docs/sdk/web-server.md +3 -3
- package/docs/sdk.md +14 -4
- package/docs/security.md +1 -1
- package/docs/session-format.md +5 -5
- package/docs/sessions.md +31 -42
- package/docs/settings.md +7 -8
- package/docs/storage/custom-adapters.md +255 -0
- package/docs/storage/jsonl.md +117 -0
- package/docs/{guides/postgres-pgbouncer.md → storage/postgres/pgbouncer.md} +73 -68
- package/docs/storage/postgres/tuning.md +337 -0
- package/docs/storage/postgres.md +556 -0
- package/docs/storage/sqlite.md +99 -0
- package/docs/storage.md +333 -956
- package/docs/subagents/authoring.md +1 -1
- package/docs/subagents.md +2 -2
- package/docs/tools.md +1 -1
- package/docs/worktree-snapshots.md +2 -2
- package/package.json +9 -9
- package/schemas/models.schema.json +2 -2
- package/schemas/settings.schema.json +0 -14
- package/dist/_shared/sdk-AVFII7NK.js.map +0 -1
- package/dist/_shared/settings-startup-4QCVCI53.js.map +0 -1
- package/dist/_shared/startup-0-0-57KF6LZH.js.map +0 -1
- package/dist/_shared/startup-0-1-GIY75K46.js.map +0 -1
- package/dist/_shared/startup-0-2-27OWFHV6.js.map +0 -1
- package/dist/_shared/startup-0-3-T4DVFYWX.js.map +0 -1
- package/dist/_shared/startup-8-1-X32CYSXB.js.map +0 -1
- package/dist/core/intercom-broker-control.d.ts +0 -129
- package/dist/startup-stages/13.js +0 -1
- package/docs/guides/postgres-tuning.md +0 -311
- package/docs/libraries/storage-libsql.md +0 -97
|
@@ -16,11 +16,12 @@ import {
|
|
|
16
16
|
OUTBOX_EXPIRED_REASON,
|
|
17
17
|
OUTBOX_TARGET_MAX_BYTES,
|
|
18
18
|
OUTBOX_TARGET_MAX_ROWS,
|
|
19
|
-
type ClaimedOutboxRow,
|
|
20
19
|
type InvalidStateInput,
|
|
21
20
|
type IntercomBrokerStateOptions,
|
|
22
21
|
type OutboxClaim,
|
|
22
|
+
type OutboxClaimResult,
|
|
23
23
|
type OutboxClaimWriteRejection,
|
|
24
|
+
type OutboxDeliveredResult,
|
|
24
25
|
type OutboxEnqueueRequest,
|
|
25
26
|
type OutboxEnqueueResult,
|
|
26
27
|
type OutboxFrame,
|
|
@@ -29,10 +30,32 @@ import {
|
|
|
29
30
|
type OutboxStore,
|
|
30
31
|
type StateStoreInvalidReason,
|
|
31
32
|
} from "./multinode-state-store.js";
|
|
33
|
+
import { wakeUpNotices } from "./database-wake-up-bus.js";
|
|
32
34
|
import { addMissingColumns } from "./sql-add-column.js";
|
|
33
|
-
import {
|
|
35
|
+
import { deleteInBatches, SETTLED, storeNow, UNDELIVERED } from "./sql-retention.js";
|
|
36
|
+
import type { OutboxReservation } from "./sql-sent-message-authority.js";
|
|
37
|
+
import { ensureBrokerSchema, RETRY_UNAPPLIED, withBrokerSchemaCatalog } from "./sql-schema.js";
|
|
34
38
|
|
|
35
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The settings a PostgreSQL send that runs in one round trip (`enqueueAtOnce`) records its decisions in, for the
|
|
41
|
+
* statements after the one that made them: whether the recipient's queue has room for the row, and which reservation
|
|
42
|
+
* of the message the send deletes if its row is not queued.
|
|
43
|
+
*/
|
|
44
|
+
const SEND_ROOM = "atomic.intercom_send_room";
|
|
45
|
+
const SEND_FORGET = "atomic.intercom_send_forget";
|
|
46
|
+
/**
|
|
47
|
+
* The settings a one-round-trip send to a recipient its sender found offline records, for the statements after the row's
|
|
48
|
+
* insert: the broker holding the recipient's live presence, read with the insert, the row's sequence and recipient, and whether the
|
|
49
|
+
* hint log took the send's lock without waiting, so its wake-up hint was written.
|
|
50
|
+
*/
|
|
51
|
+
const SEND_WAKE = "atomic.intercom_send_wake";
|
|
52
|
+
const SEND_WAKE_VERSION = "atomic.intercom_send_wake_version";
|
|
53
|
+
const SEND_WAKE_TARGET = "atomic.intercom_send_wake_target";
|
|
54
|
+
const SEND_HINT = "atomic.intercom_send_hint";
|
|
55
|
+
/** The setting a PostgreSQL claim transaction records in that its recipient has undelivered rows, and its locks are held. */
|
|
56
|
+
const CLAIM_GATE = "atomic.intercom_outbox_claim";
|
|
57
|
+
/** The most `markDelivered` calls one transaction settles together (#6560). */
|
|
58
|
+
const MARK_DELIVERED_BATCH_MAX = 100;
|
|
36
59
|
|
|
37
60
|
interface OutboxRecord extends SqliteRow {
|
|
38
61
|
readonly sequence: SqliteValue;
|
|
@@ -112,6 +135,16 @@ export interface SqlOutboxStore extends OutboxStore {
|
|
|
112
135
|
* connection sends them with the transaction's start instead of a round trip each (#6560).
|
|
113
136
|
*/
|
|
114
137
|
enqueueLeading(request: OutboxEnqueueRequest): readonly SqliteStatement[];
|
|
138
|
+
/**
|
|
139
|
+
* PostgreSQL: `enqueue`, and with `reservation` a send that reserves its message with it as `enqueueIn` does under
|
|
140
|
+
* its guard, in one transaction of the recipient's domain whose every decision the database makes, so it is one round
|
|
141
|
+
* trip and holds the recipient's lock domain for no round trip at all, a wake-up hint for an offline recipient's host
|
|
142
|
+
* included. Undefined for a request that `enqueueIn` refuses, which takes `enqueueIn`'s way.
|
|
143
|
+
*/
|
|
144
|
+
enqueueAtOnce?(
|
|
145
|
+
request: OutboxEnqueueRequest,
|
|
146
|
+
reservation?: OutboxReservation,
|
|
147
|
+
): Promise<OutboxEnqueueResult> | undefined;
|
|
115
148
|
}
|
|
116
149
|
|
|
117
150
|
/**
|
|
@@ -152,7 +185,15 @@ export async function withoutFailedWake<T>(
|
|
|
152
185
|
* The lock domain of one recipient's sends, `<outbox>:<recipient>`: a send reserves its message and queues its row in
|
|
153
186
|
* one transaction of it (`reserveAndEnqueue`, `enqueue`), so sends to one recipient run one at a time, and commit their
|
|
154
187
|
* rows in the order of their sequence, and a send's checks of that recipient's queue (its size budget) hold until it
|
|
155
|
-
* commits. Sends to other recipients, claims and every write of one row run beside them (#6560).
|
|
188
|
+
* commits. Sends to other recipients, claims and every write of one row run beside them (#6560). A send that a lock
|
|
189
|
+
* timeout ended runs again before the next send to that recipient this process makes starts (`RETRY_UNAPPLIED`).
|
|
190
|
+
*
|
|
191
|
+
* Sends to one recipient deliberately exclude one another, though a send holds the lock only while the database runs its
|
|
192
|
+
* statements, never across a round trip. Letting them share it (with a claim excluding them instead) would let them run
|
|
193
|
+
* side by side, but rows would then commit out of sequence order: a broker of an earlier release, which claims without
|
|
194
|
+
* waiting for sends, could hand a recipient's later message out before an earlier one during a rolling upgrade, and two
|
|
195
|
+
* sends could both see room for the last message the recipient's queue accepts and exceed its size budget. The time it
|
|
196
|
+
* would save, a few milliseconds per send, is not worth either.
|
|
156
197
|
*/
|
|
157
198
|
export function recipientTransaction(
|
|
158
199
|
database: SqliteDatabase,
|
|
@@ -160,7 +201,7 @@ export function recipientTransaction(
|
|
|
160
201
|
targetIdentity: string,
|
|
161
202
|
): SqliteTransactionOptions {
|
|
162
203
|
const outbox = `${validateStoragePrefix(prefix, dialectOf(database).name)}intercom_outbox`;
|
|
163
|
-
return { concurrency: { kind: "serial", domain: `${outbox}:${targetIdentity}` } };
|
|
204
|
+
return { ...RETRY_UNAPPLIED, concurrency: { kind: "serial", domain: `${outbox}:${targetIdentity}` } };
|
|
164
205
|
}
|
|
165
206
|
|
|
166
207
|
/**
|
|
@@ -169,15 +210,18 @@ export function recipientTransaction(
|
|
|
169
210
|
* transactions an older broker runs on these tables (#6560).
|
|
170
211
|
*/
|
|
171
212
|
export function rowTransaction(): SqliteTransactionOptions {
|
|
172
|
-
return {
|
|
213
|
+
return {
|
|
214
|
+
...RETRY_UNAPPLIED,
|
|
215
|
+
concurrency: { kind: "fenced", audit: fencedPath("intercom-outbox-row"), excludesSerial: true },
|
|
216
|
+
};
|
|
173
217
|
}
|
|
174
218
|
|
|
175
219
|
/**
|
|
176
220
|
* S2 outbox on the shared storage adapter's database, in `<prefix>intercom_outbox`. Every read-modify-write runs in
|
|
177
221
|
* one `BEGIN IMMEDIATE` transaction, and every age is measured on the database clock. On a database whose transactions
|
|
178
|
-
* run concurrently (PostgreSQL), an enqueue holds its recipient's domain (`recipientTransaction`); a claim
|
|
179
|
-
* recipient's rows in sequence order and every write of a claimed row is one
|
|
180
|
-
* (`rowTransaction`), so no write waits for another recipient's (#6560).
|
|
222
|
+
* run concurrently (PostgreSQL), an enqueue holds its recipient's domain (`recipientTransaction`); a claim holds its
|
|
223
|
+
* recipient's claim domain and locks the recipient's rows in sequence order, and every write of a claimed row is one
|
|
224
|
+
* statement guarded by its claim token (`rowTransaction`), so no write waits for another recipient's (#6560).
|
|
181
225
|
*/
|
|
182
226
|
export function openSqlOutboxStore(options: IntercomBrokerStateOptions): Promise<SqlOutboxStore> {
|
|
183
227
|
// Its schema checks answer from one read of the catalog, shared with an open around it: a current schema costs
|
|
@@ -189,7 +233,7 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
189
233
|
const { database } = options;
|
|
190
234
|
const table = `${validateStoragePrefix(options.prefix, dialectOf(database).name)}intercom_outbox`;
|
|
191
235
|
const { types, nowMs, greatest, lockClause, name: dialect } = dialectOf(database);
|
|
192
|
-
/**
|
|
236
|
+
/** Claim writes and expiry: each decides on the rows it locks (#6560). */
|
|
193
237
|
const fenced = rowTransaction();
|
|
194
238
|
const ageSince = (column: string) => greatest("0", `${nowMs} - ${column}`);
|
|
195
239
|
// In a serial transaction, so brokers opening a new store at once take turns creating it (#5054).
|
|
@@ -209,18 +253,14 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
209
253
|
failure_reason TEXT,
|
|
210
254
|
settled_at ${types.int}
|
|
211
255
|
);
|
|
212
|
-
CREATE INDEX IF NOT EXISTS ${table}_target ON ${table}(target_identity, state, sequence);
|
|
213
256
|
`;
|
|
214
257
|
await ensureBrokerSchema(database, options.prefix, "outbox", (tx) => tx.exec(schema));
|
|
215
258
|
// #3983: added later; a table from an older broker gets the column, and an older broker ignores it.
|
|
216
259
|
await addMissingColumns(database, table, { saving_at: types.int });
|
|
217
260
|
// #4896: when the row was queued and the size of its frame; rows an older broker wrote have neither.
|
|
218
261
|
await addMissingColumns(database, table, { queued_at: types.int, frame_bytes: types.int });
|
|
219
|
-
//
|
|
220
|
-
//
|
|
221
|
-
await ensureBrokerSchema(database, options.prefix, "outbox-indexes", (tx) =>
|
|
222
|
-
tx.exec(`CREATE INDEX IF NOT EXISTS ${table}_target_bytes ON ${table}(target_identity, state, frame_bytes)`),
|
|
223
|
-
);
|
|
262
|
+
// Its partial indexes, and its fillfactor on PostgreSQL, are set at the end of a broker's open
|
|
263
|
+
// (`ensureRetentionIndexes`), where the other stores' tables are there too.
|
|
224
264
|
|
|
225
265
|
const columns = `sequence, message_id, target_identity, origin_broker_id, digest, frame_json, state,
|
|
226
266
|
claim_attempts, claimant_id, claim_token, failure_reason,
|
|
@@ -253,8 +293,6 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
253
293
|
limit,
|
|
254
294
|
);
|
|
255
295
|
};
|
|
256
|
-
const isLive = (record: OutboxRecord, staleAfterMs: number): boolean =>
|
|
257
|
-
record.claim_age !== null && Number(record.claim_age) <= staleAfterMs;
|
|
258
296
|
|
|
259
297
|
/**
|
|
260
298
|
* Applies `assignments` (with their parameters) to the row only while `claim` is its current claim: one UPDATE whose
|
|
@@ -267,6 +305,7 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
267
305
|
assignments: string,
|
|
268
306
|
params: readonly SqliteValue[],
|
|
269
307
|
answer: (written: OutboxRecord) => Result,
|
|
308
|
+
relaxedDurability = false,
|
|
270
309
|
): Promise<Result | OutboxClaimWriteRejection> => {
|
|
271
310
|
if (claim.messageId.length === 0) return Promise.resolve(invalid("empty-message-id"));
|
|
272
311
|
if (claim.claimantId.length === 0) return Promise.resolve(invalid("empty-claimant-id"));
|
|
@@ -285,7 +324,7 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
285
324
|
if (isSettled(record)) return { status: "settled", row: toRow(record) } as const;
|
|
286
325
|
return { status: "stale" } as const;
|
|
287
326
|
},
|
|
288
|
-
{ ...fenced, leading: [write] },
|
|
327
|
+
{ ...fenced, leading: [write], ...(relaxedDurability ? { relaxedDurability } : {}) },
|
|
289
328
|
);
|
|
290
329
|
};
|
|
291
330
|
|
|
@@ -293,7 +332,96 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
293
332
|
claimant_id = NULL, claim_token = NULL, claim_renewed_at = NULL, saving_at = NULL`;
|
|
294
333
|
|
|
295
334
|
/**
|
|
296
|
-
*
|
|
335
|
+
* Settles each of `claims`, of distinct rows, as delivered, in one transaction: one UPDATE, sent with its start, that
|
|
336
|
+
* locks their rows in sequence order, as a claim locks a recipient's rows, and changes those whose claim is still
|
|
337
|
+
* current. Only when it changed fewer rows than it was given are the others read, to report why (#6560).
|
|
338
|
+
*/
|
|
339
|
+
const markDeliveredTogether = (claims: readonly OutboxClaim[]): Promise<OutboxDeliveredResult[]> => {
|
|
340
|
+
// The claims go as one JSON parameter, so the statement's text is the same however many it settles: a server that
|
|
341
|
+
// keeps prepared statements, and pg_stat_statements, see one statement, not one per count (#6560). Each row is
|
|
342
|
+
// found by its key, then checked against its claim.
|
|
343
|
+
const current = "(message_id, claim_token, claimant_id) IN (SELECT message_id, claim_token, claimant_id FROM claims)";
|
|
344
|
+
const write: SqliteStatement = {
|
|
345
|
+
sql: `WITH claims AS (
|
|
346
|
+
SELECT c ->> 0 AS message_id, c ->> 1 AS claim_token, c ->> 2 AS claimant_id FROM jsonb_array_elements(?::jsonb) AS c
|
|
347
|
+
)
|
|
348
|
+
UPDATE ${table} SET state = 'delivered', ${settled}
|
|
349
|
+
WHERE message_id = ANY(ARRAY(
|
|
350
|
+
SELECT message_id FROM ${table} WHERE state = 'claimed'
|
|
351
|
+
AND message_id = ANY(ARRAY(SELECT message_id FROM claims)) AND ${current}
|
|
352
|
+
ORDER BY sequence${lockClause("update")}
|
|
353
|
+
)) AND state = 'claimed' AND ${current}
|
|
354
|
+
RETURNING ${columns}`,
|
|
355
|
+
params: [JSON.stringify(claims.map((claim) => [claim.messageId, claim.token, claim.claimantId])), null],
|
|
356
|
+
};
|
|
357
|
+
return database.transaction(
|
|
358
|
+
async (executor) => {
|
|
359
|
+
const written = new Map(
|
|
360
|
+
(await executor.all<OutboxRecord>(write.sql, ...write.params)).map((record) => [record.message_id, record]),
|
|
361
|
+
);
|
|
362
|
+
const others = claims.filter((claim) => !written.has(claim.messageId));
|
|
363
|
+
const found =
|
|
364
|
+
others.length === 0
|
|
365
|
+
? new Map<string, OutboxRecord>()
|
|
366
|
+
: new Map(
|
|
367
|
+
(
|
|
368
|
+
await executor.all<OutboxRecord>(
|
|
369
|
+
`SELECT ${columns} FROM ${table} WHERE message_id = ANY(ARRAY(SELECT jsonb_array_elements_text(?::jsonb)))`,
|
|
370
|
+
JSON.stringify(others.map((claim) => claim.messageId)),
|
|
371
|
+
)
|
|
372
|
+
).map((record) => [record.message_id, record]),
|
|
373
|
+
);
|
|
374
|
+
return claims.map((claim): OutboxDeliveredResult => {
|
|
375
|
+
const delivered = written.get(claim.messageId);
|
|
376
|
+
if (delivered !== undefined) return { status: "delivered", row: toRow(delivered) };
|
|
377
|
+
const record = found.get(claim.messageId);
|
|
378
|
+
if (record === undefined) return { status: "unknown" };
|
|
379
|
+
if (isSettled(record)) return { status: "settled", row: toRow(record) };
|
|
380
|
+
return { status: "stale" };
|
|
381
|
+
});
|
|
382
|
+
},
|
|
383
|
+
{ ...fenced, leading: [write] },
|
|
384
|
+
);
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* The `markDelivered` calls waiting for the transaction that settles them, in the order they were made. On PostgreSQL
|
|
389
|
+
* the calls made while one transaction settles others go together in the next, whose round trips they share, rather
|
|
390
|
+
* than two round trips each: one transaction at a time, so each commits after every call made before it (#6560).
|
|
391
|
+
*/
|
|
392
|
+
const deliveries: {
|
|
393
|
+
readonly claim: OutboxClaim;
|
|
394
|
+
readonly resolve: (result: OutboxDeliveredResult) => void;
|
|
395
|
+
readonly reject: (error: unknown) => void;
|
|
396
|
+
}[] = [];
|
|
397
|
+
let delivering = false;
|
|
398
|
+
const deliverQueued = async () => {
|
|
399
|
+
// Calls made in the same turn as the first go with it.
|
|
400
|
+
await undefined;
|
|
401
|
+
while (deliveries.length > 0) {
|
|
402
|
+
// The calls in order, up to one that names a row already in the batch: it goes in the next, so it finds the row as
|
|
403
|
+
// the call before it left it.
|
|
404
|
+
const rows = new Set<string>();
|
|
405
|
+
let count = 0;
|
|
406
|
+
while (
|
|
407
|
+
count < deliveries.length &&
|
|
408
|
+
count < MARK_DELIVERED_BATCH_MAX &&
|
|
409
|
+
!rows.has(deliveries[count]!.claim.messageId)
|
|
410
|
+
)
|
|
411
|
+
rows.add(deliveries[count++]!.claim.messageId);
|
|
412
|
+
const batch = deliveries.splice(0, count);
|
|
413
|
+
try {
|
|
414
|
+
const results = await markDeliveredTogether(batch.map(({ claim }) => claim));
|
|
415
|
+
batch.forEach(({ resolve }, index) => resolve(results[index]!));
|
|
416
|
+
} catch (error) {
|
|
417
|
+
for (const { reject } of batch) reject(error);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
delivering = false;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* #4896: the recipient's undelivered rows and their frame bytes, read from `<table>_undelivered` alone. Only rows an
|
|
297
425
|
* older broker wrote, without `frame_bytes`, are measured from their frames.
|
|
298
426
|
*/
|
|
299
427
|
const sizedTotalsStatement = (targetIdentity: string): SqliteStatement => ({
|
|
@@ -410,16 +538,23 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
410
538
|
const hint = async () => {
|
|
411
539
|
// PostgreSQL: never wait behind another session's lock on the hint log; such a hint goes without. The hint
|
|
412
540
|
// is a new row, so it needs no lock domain of its own (#6560): see `fencedPath("intercom-wakeup-hint")`.
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
541
|
+
// Notified once it commits, as the wake-up bus's own hints are, so the host does not wait for its backup
|
|
542
|
+
// poll (#6560); on Postgres the notifications go right behind the commit, outside it.
|
|
543
|
+
const notifications = database.notifications;
|
|
544
|
+
const statements: SqliteStatement[] = [
|
|
545
|
+
...(dialect === "postgres" ? [{ sql: `LOCK TABLE ${wakeups} IN ROW EXCLUSIVE MODE NOWAIT`, params: [] }] : []),
|
|
546
|
+
{
|
|
547
|
+
sql: `INSERT INTO ${wakeups}(broker_id, reason, version, created_at, target_identity)
|
|
548
|
+
VALUES (?, 'outbox', ?, ${nowMs}, ?)`,
|
|
549
|
+
params: [host, Number(inserted.sequence), request.targetIdentity],
|
|
550
|
+
},
|
|
551
|
+
...(notifications === undefined ? [] : wakeUpNotices(notifications, wakeups, leases, host)),
|
|
552
|
+
];
|
|
553
|
+
// Together, in one round trip, on a pipelined transaction's own handle, which sends calls in the order they
|
|
554
|
+
// are made: a lock refused aborts the transaction, so the insert after it fails too.
|
|
555
|
+
if (finisherOf?.(executor) !== undefined)
|
|
556
|
+
await Promise.all(statements.map(({ sql, params }) => executor.run(sql, ...params)));
|
|
557
|
+
else for (const { sql, params } of statements) await executor.run(sql, ...params);
|
|
423
558
|
};
|
|
424
559
|
await hint().catch((cause: unknown) => {
|
|
425
560
|
throw new OutboxWakeFailedError(cause);
|
|
@@ -433,15 +568,206 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
433
568
|
...(request.owed === true ? [] : [sizedTotalsStatement(request.targetIdentity)]),
|
|
434
569
|
];
|
|
435
570
|
|
|
571
|
+
/**
|
|
572
|
+
* PostgreSQL: a send in statements that the database decides alone, sent together with the transaction's start and
|
|
573
|
+
* commit. The recipient's lock domain, taken with the start, is held while the server runs them and never across a
|
|
574
|
+
* round trip to the broker, so sends to one busy recipient wait for each other only that long. Under the lock, as
|
|
575
|
+
* `enqueueIn` and the guard decide it across round trips:
|
|
576
|
+
* 1. `decide` reads the message's row and reservation and the recipient's queue, and records in the transaction whether
|
|
577
|
+
* the queue has room for the row, and whether the send's earlier reservation goes because it has not.
|
|
578
|
+
* 2. With a reservation, it is written only while no row of the message with another digest is queued, and either the
|
|
579
|
+
* queue has room or the send's own row is queued already; a reservation of the message already there is left as it is.
|
|
580
|
+
* 3. The row is inserted only while the queue has room and the send's digest holds the reservation. For a recipient its
|
|
581
|
+
* sender found offline (`wakeHostWithinMs`), the insert also reads, and locks shared, the recipient's live presence,
|
|
582
|
+
* as `enqueueIn` does, and records the broker holding it.
|
|
583
|
+
* 4. With such a broker, a block locks the hint log without waiting and writes the hint, naming the broker and the row's
|
|
584
|
+
* sequence, and records that it did: a hint log another session holds locked leaves the send without its hint, as
|
|
585
|
+
* in `enqueueIn`, and fails nothing.
|
|
586
|
+
* 5. A read of whether it did.
|
|
587
|
+
* 6. A reservation the send wrote is deleted again when its row is not queued, and an earlier one when the queue was full.
|
|
588
|
+
* 7. The message's row is read, to answer a send whose row was not inserted.
|
|
589
|
+
* Each statement sees what every transaction that committed before it wrote, as each of `enqueueIn`'s does. The hint's
|
|
590
|
+
* notification goes out after the commit, so the transaction ends without waiting for it.
|
|
591
|
+
*/
|
|
592
|
+
const enqueueAtOnce = (
|
|
593
|
+
request: OutboxEnqueueRequest,
|
|
594
|
+
reservation?: OutboxReservation,
|
|
595
|
+
): Promise<OutboxEnqueueResult> | undefined => {
|
|
596
|
+
const messageId = request.frame.message.id;
|
|
597
|
+
if (request.targetIdentity.length === 0 || request.originBrokerId.length === 0 || messageId.length === 0)
|
|
598
|
+
return undefined;
|
|
599
|
+
const frameJson = JSON.stringify(request.frame);
|
|
600
|
+
const frameBytes = Buffer.byteLength(frameJson);
|
|
601
|
+
const queuedTotals =
|
|
602
|
+
request.owed === true
|
|
603
|
+
? { sql: "(SELECT 1) AS totals", params: [] as SqliteValue[] }
|
|
604
|
+
: {
|
|
605
|
+
sql: `(SELECT COUNT(*) AS row_count, COALESCE(SUM(frame_bytes), 0) + COALESCE((
|
|
606
|
+
SELECT SUM(LENGTH(frame_json)) FROM ${table}
|
|
607
|
+
WHERE target_identity = ? AND ${UNDELIVERED} AND frame_bytes IS NULL
|
|
608
|
+
), 0) AS byte_count
|
|
609
|
+
FROM ${table} WHERE target_identity = ? AND ${UNDELIVERED}) AS totals`,
|
|
610
|
+
params: [request.targetIdentity, request.targetIdentity],
|
|
611
|
+
};
|
|
612
|
+
const room =
|
|
613
|
+
request.owed === true
|
|
614
|
+
? { sql: "1", params: [] as SqliteValue[] }
|
|
615
|
+
: {
|
|
616
|
+
sql: "CASE WHEN totals.row_count < ? AND totals.byte_count + ? <= ? THEN 1 ELSE 0 END",
|
|
617
|
+
params: [OUTBOX_TARGET_MAX_ROWS, frameBytes, OUTBOX_TARGET_MAX_BYTES],
|
|
618
|
+
};
|
|
619
|
+
const holder = reservation?.holder ?? { sql: "SELECT CAST(NULL AS TEXT)", params: [] };
|
|
620
|
+
const decide: SqliteStatement = {
|
|
621
|
+
sql: `SELECT decided.room, decided.queued_digest, decided.reserved_digest,
|
|
622
|
+
set_config('${SEND_ROOM}', CASE WHEN decided.room = 1 THEN 'room' ELSE 'full' END, true) AS room_gate,
|
|
623
|
+
set_config('${SEND_FORGET}', CASE WHEN decided.room = 0 AND decided.queued_digest IS NULL
|
|
624
|
+
AND decided.reserved_digest = ? THEN 'earlier' ELSE '' END, true) AS forget_gate
|
|
625
|
+
FROM (
|
|
626
|
+
SELECT ${room.sql} AS room, (SELECT digest FROM ${table} WHERE message_id = ?) AS queued_digest,
|
|
627
|
+
(${holder.sql}) AS reserved_digest
|
|
628
|
+
FROM ${queuedTotals.sql}
|
|
629
|
+
) AS decided`,
|
|
630
|
+
params: [reservation?.digest ?? null, ...room.params, messageId, ...holder.params, ...queuedTotals.params],
|
|
631
|
+
};
|
|
632
|
+
const hasRoom = `current_setting('${SEND_ROOM}', true) = 'room'`;
|
|
633
|
+
const reserve = reservation?.reserveWhere(
|
|
634
|
+
{
|
|
635
|
+
sql: `NOT EXISTS (SELECT 1 FROM ${table} WHERE message_id = ? AND digest <> ?)
|
|
636
|
+
AND (${hasRoom} OR EXISTS (SELECT 1 FROM ${table} WHERE message_id = ? AND digest = ?))`,
|
|
637
|
+
params: [messageId, request.digest, messageId, request.digest],
|
|
638
|
+
},
|
|
639
|
+
`set_config('${SEND_FORGET}', 'new', true) AS reserved`,
|
|
640
|
+
);
|
|
641
|
+
const held = reservation?.held ?? { sql: "1 = 1", params: [] };
|
|
642
|
+
const wakes = request.wakeHostWithinMs !== undefined;
|
|
643
|
+
// #5607: read in this transaction, under the row's insert, as `enqueueIn` reads it: see there.
|
|
644
|
+
const presence = wakes
|
|
645
|
+
? {
|
|
646
|
+
sql: `, set_config('${SEND_WAKE}', COALESCE((
|
|
647
|
+
SELECT presence.holder_id FROM ${leases} presence
|
|
648
|
+
WHERE presence.kind = 'presence' AND presence.key = ? AND presence.holder_id IS NOT NULL
|
|
649
|
+
AND ${greatest("0", `${nowMs} - presence.renewed_at`)} <= ?${lockClause("share")}
|
|
650
|
+
), ''), true) AS presence_holder, set_config('${SEND_WAKE_VERSION}', CAST(sequence AS TEXT), true) AS hint_version,
|
|
651
|
+
set_config('${SEND_WAKE_TARGET}', target_identity, true) AS hint_target`,
|
|
652
|
+
params: [request.targetIdentity, request.wakeHostWithinMs!] as SqliteValue[],
|
|
653
|
+
}
|
|
654
|
+
: { sql: "", params: [] as SqliteValue[] };
|
|
655
|
+
const insert: SqliteStatement = {
|
|
656
|
+
sql: `INSERT INTO ${table}(message_id, target_identity, origin_broker_id, digest, frame_json, state, queued_at,
|
|
657
|
+
frame_bytes)
|
|
658
|
+
SELECT ?, ?, ?, ?, ?, 'queued', ${nowMs}, CAST(? AS ${types.int})
|
|
659
|
+
WHERE ${hasRoom} AND ${held.sql}
|
|
660
|
+
ON CONFLICT(message_id) DO NOTHING
|
|
661
|
+
RETURNING ${columns}${presence.sql}`,
|
|
662
|
+
params: [
|
|
663
|
+
messageId,
|
|
664
|
+
request.targetIdentity,
|
|
665
|
+
request.originBrokerId,
|
|
666
|
+
request.digest,
|
|
667
|
+
frameJson,
|
|
668
|
+
frameBytes,
|
|
669
|
+
...held.params,
|
|
670
|
+
...presence.params,
|
|
671
|
+
],
|
|
672
|
+
};
|
|
673
|
+
const woken = `COALESCE(current_setting('${SEND_WAKE}', true), '')`;
|
|
674
|
+
// Locks the hint log and writes the hint only with such a broker, and never waits behind another session's lock on
|
|
675
|
+
// the log: such a send goes without its hint (#5607). A hint is a new row, so it needs no lock domain of its own
|
|
676
|
+
// (#6560): see `fencedPath("intercom-wakeup-hint")`. Even an insert of no row would wait for the log's lock, so the
|
|
677
|
+
// insert is made in the block, only once the lock is held.
|
|
678
|
+
const writeHint: SqliteStatement = {
|
|
679
|
+
sql: `DO $atomic_send$ BEGIN
|
|
680
|
+
IF ${woken} <> '' THEN
|
|
681
|
+
LOCK TABLE ${wakeups} IN ROW EXCLUSIVE MODE NOWAIT;
|
|
682
|
+
INSERT INTO ${wakeups}(broker_id, reason, version, created_at, target_identity)
|
|
683
|
+
VALUES (${woken}, 'outbox', CAST(current_setting('${SEND_WAKE_VERSION}') AS BIGINT), ${nowMs},
|
|
684
|
+
current_setting('${SEND_WAKE_TARGET}'));
|
|
685
|
+
PERFORM set_config('${SEND_HINT}', 'written', true);
|
|
686
|
+
END IF;
|
|
687
|
+
EXCEPTION WHEN lock_not_available THEN NULL;
|
|
688
|
+
END $atomic_send$`,
|
|
689
|
+
params: [],
|
|
690
|
+
};
|
|
691
|
+
const hint: SqliteStatement = {
|
|
692
|
+
sql: `SELECT ${woken} AS broker_id WHERE COALESCE(current_setting('${SEND_HINT}', true), '') = 'written'`,
|
|
693
|
+
params: [],
|
|
694
|
+
};
|
|
695
|
+
const forget = reservation?.forgetWhere({
|
|
696
|
+
sql: `current_setting('${SEND_FORGET}', true) = 'earlier' OR (current_setting('${SEND_FORGET}', true) = 'new'
|
|
697
|
+
AND NOT EXISTS (SELECT 1 FROM ${table} WHERE message_id = ? AND digest = ?))`,
|
|
698
|
+
params: [messageId, request.digest],
|
|
699
|
+
});
|
|
700
|
+
const queued = selectStatement(messageId);
|
|
701
|
+
const leading = [
|
|
702
|
+
decide,
|
|
703
|
+
...(reserve === undefined ? [] : [reserve]),
|
|
704
|
+
insert,
|
|
705
|
+
...(wakes ? [writeHint, hint] : []),
|
|
706
|
+
...(forget === undefined ? [] : [forget]),
|
|
707
|
+
queued,
|
|
708
|
+
];
|
|
709
|
+
const answer = async (executor: SqliteExecutor): Promise<OutboxEnqueueResult> => {
|
|
710
|
+
// In the order of `leading`, each answered with the transaction's start: no call waits for a round trip.
|
|
711
|
+
const decided = (await executor.get<SqliteRow & { room: SqliteValue; reserved_digest: string | null }>(
|
|
712
|
+
decide.sql,
|
|
713
|
+
...decide.params,
|
|
714
|
+
))!;
|
|
715
|
+
if (reserve !== undefined) await executor.all(reserve.sql, ...reserve.params);
|
|
716
|
+
const row = await executor.get<OutboxRecord>(insert.sql, ...insert.params);
|
|
717
|
+
let woke: string | undefined;
|
|
718
|
+
if (wakes) {
|
|
719
|
+
// A database that cannot run the block (no PL/pgSQL for this role) writes the send again without its hint.
|
|
720
|
+
await executor.run(writeHint.sql).catch((cause: unknown) => {
|
|
721
|
+
throw new OutboxWakeFailedError(cause);
|
|
722
|
+
});
|
|
723
|
+
woke = (await executor.get<SqliteRow & { broker_id: string }>(hint.sql, ...hint.params))?.broker_id;
|
|
724
|
+
}
|
|
725
|
+
if (forget !== undefined) await executor.run(forget.sql, ...forget.params);
|
|
726
|
+
const found = await executor.get<OutboxRecord>(queued.sql, ...queued.params);
|
|
727
|
+
if (row !== undefined)
|
|
728
|
+
return { status: "inserted", row: toRow(row), ...(woke === undefined ? {} : { woke }) } as const;
|
|
729
|
+
// Another digest held the message's reservation: the send is refused, as before it wrote anything.
|
|
730
|
+
if (decided.reserved_digest !== null && decided.reserved_digest !== reservation?.digest)
|
|
731
|
+
return { status: "conflict", messageId };
|
|
732
|
+
if (found !== undefined)
|
|
733
|
+
return found.digest === request.digest
|
|
734
|
+
? { status: "duplicate", row: toRow(found) }
|
|
735
|
+
: { status: "conflict", messageId };
|
|
736
|
+
if (Number(decided.room) === 0)
|
|
737
|
+
return { status: "full", limit: OUTBOX_TARGET_MAX_ROWS, byteLimit: OUTBOX_TARGET_MAX_BYTES };
|
|
738
|
+
return { status: "conflict", messageId };
|
|
739
|
+
};
|
|
740
|
+
return database
|
|
741
|
+
.transaction(answer, {
|
|
742
|
+
...recipientTransaction(database, options.prefix, request.targetIdentity),
|
|
743
|
+
leading,
|
|
744
|
+
leadingOnly: true,
|
|
745
|
+
})
|
|
746
|
+
.then((result) => {
|
|
747
|
+
// Notified once the hint committed, on the host's own channel (and the shared one only for a host of an
|
|
748
|
+
// earlier release), as the wake-up bus's own hints are, so the host does not wait for its backup poll
|
|
749
|
+
// (#6560); the hint is there for its poll if a notification is lost.
|
|
750
|
+
const notifications = database.notifications;
|
|
751
|
+
if (result.status === "inserted" && result.woke !== undefined && notifications !== undefined)
|
|
752
|
+
for (const { sql, params } of wakeUpNotices(notifications, wakeups, leases, result.woke))
|
|
753
|
+
void database.run(sql, ...params).catch(() => {});
|
|
754
|
+
return result;
|
|
755
|
+
});
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
const enqueueAtOnceIfPostgres = (request: OutboxEnqueueRequest, reservation?: OutboxReservation) =>
|
|
759
|
+
dialect === "postgres" ? enqueueAtOnce(request, reservation) : undefined;
|
|
760
|
+
|
|
436
761
|
return {
|
|
437
762
|
enqueueIn,
|
|
438
763
|
enqueueLeading,
|
|
764
|
+
...(dialect === "postgres" ? { enqueueAtOnce } : {}),
|
|
439
765
|
async enqueue(request) {
|
|
440
766
|
if (request.targetIdentity.length === 0) return invalid("empty-target-identity");
|
|
441
767
|
if (request.originBrokerId.length === 0) return invalid("empty-broker-id");
|
|
442
768
|
if (request.frame.message.id.length === 0) return invalid("empty-message-id");
|
|
443
769
|
return withoutFailedWake(request, (attempt) =>
|
|
444
|
-
database.transaction((executor) => enqueueIn(executor, attempt), {
|
|
770
|
+
enqueueAtOnceIfPostgres(attempt) ?? database.transaction((executor) => enqueueIn(executor, attempt), {
|
|
445
771
|
...recipientTransaction(database, options.prefix, attempt.targetIdentity),
|
|
446
772
|
leading: enqueueLeading(attempt),
|
|
447
773
|
}),
|
|
@@ -451,65 +777,139 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
451
777
|
async claim(request) {
|
|
452
778
|
if (request.claimantId.length === 0) return invalid("empty-claimant-id");
|
|
453
779
|
if (request.targetIdentity.length === 0) return invalid("empty-target-identity");
|
|
454
|
-
|
|
455
|
-
//
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
780
|
+
const { claimantId, targetIdentity, staleAfterMs } = request;
|
|
781
|
+
// Each row's token: this claim's, with the row's sequence, so every claimed row has a token of its own.
|
|
782
|
+
const token = randomUUID();
|
|
783
|
+
const claimed = (records: readonly OutboxRecord[]): OutboxClaimResult => ({
|
|
784
|
+
status: "claimed",
|
|
785
|
+
rows: [...records]
|
|
786
|
+
.sort((first, second) => Number(first.sequence) - Number(second.sequence))
|
|
787
|
+
.map((record) => {
|
|
788
|
+
const row = toRow(record);
|
|
789
|
+
const claim = { messageId: row.messageId, claimantId, token: String(record.claim_token), attempt: row.claimAttempts };
|
|
790
|
+
return { row, claim };
|
|
791
|
+
}),
|
|
792
|
+
});
|
|
793
|
+
const claiming = `state = 'claimed', claim_attempts = claim_attempts + 1, claimant_id = ?,
|
|
794
|
+
claim_token = ? || ':' || CAST(sequence AS TEXT), claim_renewed_at = ${nowMs}, saving_at = NULL`;
|
|
795
|
+
if (dialect === "postgres") {
|
|
796
|
+
// A read and one claim statement, sent with the transaction's start and its commit: one round trip whether or not
|
|
797
|
+
// there is anything to claim (#6560). The claim statement locks the recipient's undelivered rows in sequence order, as every write of several rows
|
|
798
|
+
// locks them, and reads them in their newest versions, so a renewal or settlement of one waits for this claim, or
|
|
799
|
+
// this claim for it. It does not skip a locked row (SKIP LOCKED): an earlier row passed over could be handed out
|
|
800
|
+
// after a later one, and another claimant's live claim would go unseen. Another claimant's live claim holds
|
|
801
|
+
// every row; otherwise each row not live is claimed, and the claimant's own live claims stay in flight.
|
|
802
|
+
// The recipient's claims take turns: the lock domain `<outbox>_claims:<recipient>` is taken before the statement
|
|
803
|
+
// takes its snapshot. The statement sees only rows its snapshot holds, so without it a claim made meanwhile of a
|
|
804
|
+
// row queued meanwhile went unseen: this claim could take over a claimant's older row it was late to renew while
|
|
805
|
+
// that claimant held a newer one, and two brokers would hand out one recipient's messages at once, the newer
|
|
806
|
+
// possibly first. Sends, settlements and other recipients' claims do not take it.
|
|
807
|
+
// Only a recipient with undelivered rows needs the lock. A read sent first, `waiting`, takes it, with the
|
|
808
|
+
// database's lock shared, only when it finds such a row, so the common empty poll takes no lock at all. The read
|
|
809
|
+
// records what it found in a setting of the transaction, and the claim statement locks and claims rows only when
|
|
810
|
+
// the read found some: every claim acts under the lock, with a snapshot taken after it. A row queued after the
|
|
811
|
+
// read waits for the next claim, which its send's wake-up hint brings. An older driver takes the locks with the
|
|
812
|
+
// transaction's start instead, which is just as correct.
|
|
813
|
+
// While it waits for the locks, `waiting` holds the table's share lock. A broker that adds a column to the table
|
|
814
|
+
// under the database's lock at that moment ends one of the two with a deadlock error; a claim that fails so is
|
|
815
|
+
// taken again at the next scan, as after any failed claim.
|
|
816
|
+
const waiting: SqliteStatement = {
|
|
817
|
+
sql: `SELECT set_config('${CLAIM_GATE}', 'locked', true) AS waiting
|
|
818
|
+
WHERE EXISTS (SELECT 1 FROM ${table} WHERE target_identity = ? AND ${UNDELIVERED})`,
|
|
819
|
+
params: [targetIdentity],
|
|
820
|
+
};
|
|
821
|
+
const statement: SqliteStatement = {
|
|
822
|
+
sql: `WITH locked AS MATERIALIZED (
|
|
823
|
+
SELECT message_id, sequence, state, claimant_id, claim_renewed_at FROM ${table}
|
|
824
|
+
WHERE target_identity = ? AND ${UNDELIVERED}
|
|
825
|
+
AND current_setting('${CLAIM_GATE}', true) = 'locked'
|
|
826
|
+
ORDER BY sequence${lockClause("update")}
|
|
827
|
+
), live AS MATERIALIZED (
|
|
828
|
+
SELECT message_id, sequence, claimant_id FROM locked
|
|
829
|
+
WHERE state = 'claimed' AND claim_renewed_at IS NOT NULL AND ${ageSince("claim_renewed_at")} <= ?
|
|
830
|
+
), holder AS MATERIALIZED (
|
|
831
|
+
SELECT claimant_id FROM live WHERE claimant_id <> ? ORDER BY sequence LIMIT 1
|
|
832
|
+
), claimed AS (
|
|
833
|
+
UPDATE ${table} SET ${claiming}
|
|
834
|
+
WHERE message_id IN (SELECT message_id FROM locked)
|
|
835
|
+
AND message_id NOT IN (SELECT message_id FROM live)
|
|
836
|
+
AND NOT EXISTS (SELECT 1 FROM holder)
|
|
837
|
+
RETURNING ${columns}
|
|
838
|
+
)
|
|
839
|
+
SELECT claimed.*, (SELECT claimant_id FROM holder) AS holder
|
|
840
|
+
FROM (SELECT 1) AS answer LEFT JOIN claimed ON TRUE`,
|
|
841
|
+
params: [targetIdentity, staleAfterMs, claimantId, claimantId, token],
|
|
842
|
+
};
|
|
843
|
+
return database.transaction(
|
|
844
|
+
async (executor) => {
|
|
845
|
+
await executor.all(waiting.sql, ...waiting.params);
|
|
846
|
+
const records = await executor.all<OutboxRecord & { readonly holder: string | null }>(
|
|
847
|
+
statement.sql,
|
|
848
|
+
...statement.params,
|
|
849
|
+
);
|
|
850
|
+
const holder = records[0]?.holder ?? null;
|
|
851
|
+
if (holder !== null) return { status: "held", claimantId: holder } as const;
|
|
852
|
+
return claimed(records.filter((record) => record.message_id !== null));
|
|
853
|
+
},
|
|
854
|
+
{
|
|
855
|
+
...RETRY_UNAPPLIED,
|
|
856
|
+
concurrency: { kind: "serial", domain: `${table}_claims:${targetIdentity}` },
|
|
857
|
+
leading: [waiting, statement],
|
|
858
|
+
leadingOnly: true,
|
|
859
|
+
lockIfLeadingFinds: true,
|
|
860
|
+
},
|
|
477
861
|
);
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
862
|
+
}
|
|
863
|
+
// SQLite: a write statement takes the database's write lock even when it changes nothing, so a read
|
|
864
|
+
// decides first, without it, whether there is anything to claim: empty scans (such as fan-out wake-up polls across
|
|
865
|
+
// idle sessions) cost one read. Linearizable: if a row commits after this snapshot, its enqueue writes a new
|
|
866
|
+
// wake-up hint. The same read answers a claim another claimant holds, or one of only the claimant's own rows in
|
|
867
|
+
// flight. Otherwise one statement claims the rows, deciding again on what it changes (#6560).
|
|
868
|
+
const live = (alias: string) =>
|
|
869
|
+
`${alias}.state = 'claimed' AND ${alias}.claim_renewed_at IS NOT NULL
|
|
870
|
+
AND ${ageSince(`${alias}.claim_renewed_at`)} <= ?`;
|
|
871
|
+
const heldBy = `SELECT live.claimant_id FROM ${table} live
|
|
872
|
+
WHERE live.target_identity = ? AND ${live("live")} AND live.claimant_id <> ?`;
|
|
873
|
+
const probe = () =>
|
|
874
|
+
database.get<{ holder: string | null; claimable: SqliteValue }>(
|
|
875
|
+
`SELECT (${heldBy} ORDER BY live.sequence LIMIT 1) AS holder, EXISTS (
|
|
876
|
+
SELECT 1 FROM ${table} pending WHERE pending.target_identity = ?
|
|
877
|
+
AND pending.${UNDELIVERED} AND NOT (${live("pending")})
|
|
878
|
+
) AS claimable`,
|
|
879
|
+
targetIdentity,
|
|
880
|
+
staleAfterMs,
|
|
881
|
+
claimantId,
|
|
882
|
+
targetIdentity,
|
|
883
|
+
staleAfterMs,
|
|
884
|
+
);
|
|
885
|
+
const found = await probe();
|
|
886
|
+
if (found !== undefined && found.holder !== null) return { status: "held", claimantId: found.holder } as const;
|
|
887
|
+
if (Number(found?.claimable ?? 0) === 0) return { status: "claimed", rows: [] } as const;
|
|
888
|
+
const records = await database.all<OutboxRecord>(
|
|
889
|
+
`UPDATE ${table} SET ${claiming}
|
|
890
|
+
WHERE target_identity = ? AND ${UNDELIVERED} AND NOT (${live(table)}) AND NOT EXISTS (${heldBy})
|
|
891
|
+
RETURNING ${columns}`,
|
|
892
|
+
claimantId,
|
|
893
|
+
token,
|
|
894
|
+
targetIdentity,
|
|
895
|
+
staleAfterMs,
|
|
896
|
+
targetIdentity,
|
|
897
|
+
staleAfterMs,
|
|
898
|
+
claimantId,
|
|
899
|
+
);
|
|
900
|
+
if (records.length > 0) return claimed(records);
|
|
901
|
+
// Another claimant took the rows since the read, or they were settled: only then is the holder read again.
|
|
902
|
+
const again = await probe();
|
|
903
|
+
return again !== undefined && again.holder !== null
|
|
904
|
+
? ({ status: "held", claimantId: again.holder } as const)
|
|
905
|
+
: ({ status: "claimed", rows: [] } as const);
|
|
509
906
|
},
|
|
510
907
|
|
|
511
908
|
renewClaim(claim) {
|
|
512
|
-
|
|
909
|
+
// Its claimant renews again within the renewal interval, far inside the claim's staleness limit: a renewal lost
|
|
910
|
+
// in a crash of the database server leaves the claim a renewal older and still current, so its commit does not
|
|
911
|
+
// wait for the disk.
|
|
912
|
+
return withClaim(claim, `claim_renewed_at = ${nowMs}`, [], () => ({ status: "renewed" }) as const, true);
|
|
513
913
|
},
|
|
514
914
|
|
|
515
915
|
reportSaving(claim) {
|
|
@@ -541,10 +941,17 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
541
941
|
},
|
|
542
942
|
|
|
543
943
|
markDelivered(claim) {
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
944
|
+
if (dialect !== "postgres" || claim.messageId.length === 0 || claim.claimantId.length === 0)
|
|
945
|
+
return withClaim(claim, `state = 'delivered', ${settled}`, [null], (written) => ({
|
|
946
|
+
status: "delivered" as const,
|
|
947
|
+
row: toRow(written),
|
|
948
|
+
}));
|
|
949
|
+
return new Promise<OutboxDeliveredResult>((resolve, reject) => {
|
|
950
|
+
deliveries.push({ claim, resolve, reject });
|
|
951
|
+
if (delivering) return;
|
|
952
|
+
delivering = true;
|
|
953
|
+
void deliverQueued();
|
|
954
|
+
});
|
|
548
955
|
},
|
|
549
956
|
|
|
550
957
|
markFailed(claim, reason) {
|
|
@@ -568,7 +975,7 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
568
975
|
async undeliveredPosition(targetIdentity, messageId) {
|
|
569
976
|
if (targetIdentity.length === 0) return invalid("empty-target-identity");
|
|
570
977
|
if (messageId.length === 0) return invalid("empty-message-id");
|
|
571
|
-
// Counted on
|
|
978
|
+
// Counted on `<outbox>_undelivered`: no row, and no frame, is read.
|
|
572
979
|
const counted = await database.get<{ position: SqliteValue }>(
|
|
573
980
|
`SELECT COUNT(*) AS position FROM ${table}
|
|
574
981
|
WHERE target_identity = ? AND ${UNDELIVERED} AND sequence <= (
|
|
@@ -583,11 +990,16 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
583
990
|
},
|
|
584
991
|
|
|
585
992
|
async prune(settledOlderThanMs, queuedOlderThanMs) {
|
|
993
|
+
// Each age compared as `column < now - age`, so an index on the column answers it.
|
|
994
|
+
const now = storeNow(database);
|
|
586
995
|
if (queuedOlderThanMs !== undefined) {
|
|
587
996
|
// Every recipient's rows, each locked in sequence order and decided on in its newest version, as a claim
|
|
588
|
-
// locks them: a renewal that commits first keeps its row, and expiry waits for no lock domain (#6560).
|
|
589
|
-
|
|
590
|
-
|
|
997
|
+
// locks them: a renewal that commits first keeps its row, and expiry waits for no lock domain (#6560). PostgreSQL
|
|
998
|
+
// takes the locked rows' ids as one array, so the update finds each by its key rather than joining the table.
|
|
999
|
+
const lockedWhere = (where: string) => {
|
|
1000
|
+
const locked = `SELECT message_id FROM ${table} WHERE ${where} ORDER BY sequence${lockClause("update")}`;
|
|
1001
|
+
return dialect === "postgres" ? `message_id = ANY(ARRAY(${locked}))` : `message_id IN (${locked})`;
|
|
1002
|
+
};
|
|
591
1003
|
await database.transaction(async (executor) => {
|
|
592
1004
|
// A row an older broker queued has no time: its retention starts now.
|
|
593
1005
|
await executor.run(
|
|
@@ -596,8 +1008,8 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
596
1008
|
await executor.run(
|
|
597
1009
|
`UPDATE ${table} SET state = 'failed', ${settled}
|
|
598
1010
|
WHERE ${lockedWhere(
|
|
599
|
-
`${UNDELIVERED} AND ${
|
|
600
|
-
AND (claim_renewed_at IS NULL OR ${
|
|
1011
|
+
`${UNDELIVERED} AND queued_at < ${now} - ?
|
|
1012
|
+
AND (claim_renewed_at IS NULL OR claim_renewed_at < ${now} - ?)`,
|
|
601
1013
|
)}`,
|
|
602
1014
|
JSON.stringify(OUTBOX_EXPIRED_REASON),
|
|
603
1015
|
queuedOlderThanMs,
|
|
@@ -605,12 +1017,14 @@ async function openOutboxStoreInCatalog(options: IntercomBrokerStateOptions): Pr
|
|
|
605
1017
|
);
|
|
606
1018
|
}, fenced);
|
|
607
1019
|
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
1020
|
+
// Oldest first, from `<outbox>_settled`, in batches.
|
|
1021
|
+
return deleteInBatches(database, {
|
|
1022
|
+
table,
|
|
1023
|
+
key: "sequence",
|
|
1024
|
+
where: `${SETTLED} AND settled_at < ${now} - ?`,
|
|
1025
|
+
params: [settledOlderThanMs],
|
|
1026
|
+
orderBy: "settled_at",
|
|
1027
|
+
});
|
|
614
1028
|
},
|
|
615
1029
|
};
|
|
616
1030
|
}
|