@bastani/atomic 0.10.4 → 0.10.5
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 +122 -0
- package/dist/_shared/compile-cache-warmup-host-RU5L7I7A.js +1 -1
- package/dist/_shared/dist-KOELCESV.js +48 -3
- package/dist/_shared/fork-row-index-UAUAQRD5.js +47 -2
- package/dist/_shared/http-YFAPTIZD.js +16 -4
- package/dist/_shared/http-YFAPTIZD.js.map +1 -1
- package/dist/_shared/lib-4PES5P4V.js +59 -14
- package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js +1706 -258
- package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js.map +1 -1
- package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +317 -64
- package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js.map +1 -1
- package/dist/_shared/{mode-shared-HP7GUA7J.js → mode-shared-2QJSCUM6.js} +432 -81
- package/dist/_shared/mode-shared-2QJSCUM6.js.map +1 -0
- package/dist/_shared/p0-cli-BB45ZQY6.js +3482 -0
- package/dist/_shared/p0-cli-BB45ZQY6.js.map +1 -0
- package/dist/_shared/p0-cli-GUFK7TPN.js +2 -2838
- package/dist/_shared/p0-cli-GUFK7TPN.js.map +1 -1
- package/dist/_shared/{p1a-prewarm-YK3KDTLN.js → p1a-prewarm-UQY3X2UK.js} +270 -73
- package/dist/_shared/p1a-prewarm-UQY3X2UK.js.map +1 -0
- package/dist/_shared/{p1b-elig-PXQA2FYP.js → p1b-elig-ENCUPKKJ.js} +190 -121
- package/dist/_shared/p1b-elig-ENCUPKKJ.js.map +1 -0
- package/dist/_shared/p2-common-YLSYXMDQ.js +1 -3
- package/dist/_shared/p2-common-YLSYXMDQ.js.map +1 -1
- package/dist/_shared/p2-shell-TJBYQMCJ.js +1 -1
- package/dist/_shared/p2-shell-main-B2WIALFD.js +500 -127
- package/dist/_shared/p2-shell-main-B2WIALFD.js.map +1 -1
- package/dist/_shared/runtime-O4KCSG4C.js +4 -4
- package/dist/_shared/sdk-WQIOHVKO.js +56 -11
- package/dist/_shared/session-data-AHTU7IS6.js +48 -3
- package/dist/_shared/session-manager-list-2BLEEZVG.js +2 -2
- package/dist/_shared/settings-startup-65YEWRV2.js.map +1 -1
- package/dist/_shared/sql-session-store-5FD3WBMZ.js +57 -12
- package/dist/_shared/{startup-0-0-X4NF3GKM.js → startup-0-0-TYUFUOJZ.js} +519 -316
- package/dist/_shared/startup-0-0-TYUFUOJZ.js.map +1 -0
- package/dist/_shared/{startup-0-1-7QTG6VJZ.js → startup-0-1-D4LGGINF.js} +284 -81
- package/dist/_shared/startup-0-1-D4LGGINF.js.map +1 -0
- package/dist/_shared/{startup-0-2-KFDYPDJK.js → startup-0-2-SZGQIWHI.js} +483 -154
- package/dist/_shared/startup-0-2-SZGQIWHI.js.map +1 -0
- package/dist/_shared/startup-0-3-T4DVFYWX.js +340 -110
- package/dist/_shared/startup-0-3-T4DVFYWX.js.map +1 -1
- package/dist/_shared/startup-0-4-HN5EB4K7.js +261 -59
- package/dist/_shared/startup-0-4-HN5EB4K7.js.map +1 -1
- package/dist/_shared/startup-0-5-OHJNE6JH.js +421 -148
- package/dist/_shared/startup-0-5-OHJNE6JH.js.map +1 -1
- package/dist/_shared/startup-10-0-TDQNBDDL.js +62 -11
- package/dist/_shared/startup-10-0-TDQNBDDL.js.map +1 -1
- package/dist/_shared/startup-12-0-55AAXBJZ.js +3 -3
- package/dist/_shared/startup-14-0-MLX36RBG.js +12 -11
- package/dist/_shared/startup-14-0-MLX36RBG.js.map +1 -1
- package/dist/_shared/startup-15-0-YUKGAC63.js +53 -158
- package/dist/_shared/startup-15-0-YUKGAC63.js.map +1 -1
- package/dist/_shared/startup-15-1-4S7FLMNG.js +231 -46
- package/dist/_shared/startup-15-1-4S7FLMNG.js.map +1 -1
- package/dist/_shared/startup-8-0-34CBHPZH.js +115 -35
- package/dist/_shared/startup-8-0-34CBHPZH.js.map +1 -1
- package/dist/_shared/{startup-8-1-KQJBDKRU.js → startup-8-1-SJW2GMAM.js} +203 -62
- package/dist/_shared/startup-8-1-SJW2GMAM.js.map +1 -0
- package/dist/_shared/storage-command-7MGXLJGZ.js +55 -10
- package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +47 -2
- package/dist/_shared/tree-outline-MUEPK5B7.js +48 -3
- package/dist/builtin/intercom/CHANGELOG.md +42 -0
- package/dist/builtin/intercom/README.md +0 -1
- package/dist/builtin/intercom/broker/broker-instance.ts +13 -57
- package/dist/builtin/intercom/broker/broker-process.ts +30 -10
- package/dist/builtin/intercom/broker/broker-routing.ts +12 -2
- package/dist/builtin/intercom/broker/broker.bundle.mjs +2557 -498
- package/dist/builtin/intercom/broker/broker.ts +164 -55
- package/dist/builtin/intercom/broker/client.ts +36 -16
- package/dist/builtin/intercom/broker/control.ts +83 -20
- package/dist/builtin/intercom/broker/durable-ended-queue.ts +65 -19
- package/dist/builtin/intercom/broker/host-command-environment.ts +7 -2
- package/dist/builtin/intercom/broker/multinode-router.ts +1675 -387
- package/dist/builtin/intercom/broker/multinode-state-store.ts +10 -0
- package/dist/builtin/intercom/broker/paths.ts +68 -6
- package/dist/builtin/intercom/broker/pending-send-registry.ts +5 -0
- package/dist/builtin/intercom/broker/registration-bounds.ts +38 -0
- package/dist/builtin/intercom/broker/retiring-broker-check.ts +178 -0
- package/dist/builtin/intercom/broker/return-paths.ts +268 -7
- package/dist/builtin/intercom/broker/send-handler.ts +253 -28
- package/dist/builtin/intercom/broker/send-order-gate.ts +410 -0
- package/dist/builtin/intercom/broker/spawn.ts +950 -202
- package/dist/builtin/intercom/broker/sql-lease-store.ts +37 -0
- package/dist/builtin/intercom/broker/sql-outbox-store.ts +7 -0
- package/dist/builtin/intercom/broker/windows-cwd.ts +546 -0
- package/dist/builtin/intercom/index.bundle.mjs +3506 -2328
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/intercom/retry-policy.ts +4 -0
- package/dist/builtin/intercom/types.ts +2 -0
- package/dist/builtin/mcp/index.bundle.mjs +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/CHANGELOG.md +12 -0
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/src/extension/index.bundle.mjs +336 -86
- package/dist/builtin/web-access/index.bundle.mjs +23 -38
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/bun/cli.js +1 -1
- package/dist/bun/cli.js.map +1 -1
- package/dist/bun/internal-intercom-broker.js +119 -4
- package/dist/bun/internal-intercom-broker.js.map +1 -1
- package/dist/bun/register-theme-validator.js +6 -0
- package/dist/bun/register-theme-validator.js.map +1 -0
- package/dist/cli/args.js +2 -2
- package/dist/cli/auth-cli.code.js +1102 -0
- package/dist/cli/auth-cli.code.js.map +1 -0
- package/dist/cli/auth-cli.impl.js +30 -1096
- package/dist/cli/auth-cli.impl.js.map +1 -1
- package/dist/cli/auth-cli.js +11 -7
- package/dist/cli/blueprint-command.code.js +906 -0
- package/dist/cli/blueprint-command.code.js.map +1 -0
- package/dist/cli/blueprint-command.impl.js +30 -900
- package/dist/cli/blueprint-command.impl.js.map +1 -1
- package/dist/cli/blueprint-command.js +11 -7
- package/dist/cli/intercom-command.code.js +153 -0
- package/dist/cli/intercom-command.code.js.map +1 -0
- package/dist/cli/intercom-command.impl.js +33 -105
- package/dist/cli/intercom-command.impl.js.map +1 -1
- package/dist/cli/intercom-command.js +11 -7
- package/dist/cli/list-models.code.js +60 -0
- package/dist/cli/list-models.code.js.map +1 -0
- package/dist/cli/list-models.impl.js +33 -57
- package/dist/cli/list-models.impl.js.map +1 -1
- package/dist/cli/list-models.js +11 -7
- package/dist/cli/session-picker.code.js +55 -0
- package/dist/cli/session-picker.code.js.map +1 -0
- package/dist/cli/session-picker.impl.js +33 -52
- package/dist/cli/session-picker.impl.js.map +1 -1
- package/dist/cli/session-picker.js +11 -7
- package/dist/cli/startup-ui.code.js +5 -0
- package/dist/cli/startup-ui.code.js.map +1 -0
- package/dist/cli/startup-ui.impl.js +35 -4
- package/dist/cli/startup-ui.js +11 -7
- package/dist/cli/storage-command.code.js +3 -0
- package/dist/cli/storage-command.code.js.map +1 -0
- package/dist/cli/storage-command.impl.js +35 -2
- package/dist/cli/storage-command.js +11 -7
- package/dist/cli/storage-search-session.code.js +189 -0
- package/dist/cli/storage-search-session.code.js.map +1 -0
- package/dist/cli/storage-search-session.impl.js +33 -186
- package/dist/cli/storage-search-session.impl.js.map +1 -1
- package/dist/cli/storage-search-session.js +11 -7
- package/dist/cli/team-command.code.js +217 -0
- package/dist/cli/team-command.code.js.map +1 -0
- package/dist/cli/team-command.impl.js +32 -213
- package/dist/cli/team-command.impl.js.map +1 -1
- package/dist/cli/team-command.js +11 -7
- package/dist/cli.js +60 -11
- package/dist/cli.js.map +1 -1
- package/dist/config.js +1 -1
- package/dist/core/agent-session-methods.d.ts +4 -3
- package/dist/core/agent-session-methods.js.map +1 -1
- package/dist/core/agent-session-types.d.ts +5 -0
- package/dist/core/compaction/compaction.d.ts +6 -0
- package/dist/core/compaction/model-resolver.d.ts +1 -1
- package/dist/core/debug.code.js +3 -0
- package/dist/core/debug.code.js.map +1 -0
- package/dist/core/debug.impl.js +35 -2
- package/dist/core/debug.js +11 -7
- package/dist/core/default-model-api-prewarm.d.ts +5 -0
- package/dist/core/export-html/card-projection.code.js +257 -0
- package/dist/core/export-html/card-projection.code.js.map +1 -0
- package/dist/core/export-html/card-projection.impl.js +34 -255
- package/dist/core/export-html/card-projection.impl.js.map +1 -1
- package/dist/core/export-html/card-projection.js +11 -7
- package/dist/core/export-html/index.code.js +655 -0
- package/dist/core/export-html/index.code.js.map +1 -0
- package/dist/core/export-html/index.impl.js +30 -604
- package/dist/core/export-html/index.impl.js.map +1 -1
- package/dist/core/export-html/index.js +11 -7
- package/dist/core/export-html/json-spans.code.js +3 -0
- package/dist/core/export-html/json-spans.code.js.map +1 -0
- package/dist/core/export-html/json-spans.impl.js +35 -2
- package/dist/core/export-html/json-spans.js +11 -7
- package/dist/core/export-html/oversized-render-inputs.code.js +125 -0
- package/dist/core/export-html/oversized-render-inputs.code.js.map +1 -0
- package/dist/core/export-html/oversized-render-inputs.impl.js +31 -120
- package/dist/core/export-html/oversized-render-inputs.impl.js.map +1 -1
- package/dist/core/export-html/oversized-render-inputs.js +11 -7
- package/dist/core/export-html/tool-renderer.code.js +350 -0
- package/dist/core/export-html/tool-renderer.code.js.map +1 -0
- package/dist/core/export-html/tool-renderer.impl.js +33 -347
- package/dist/core/export-html/tool-renderer.impl.js.map +1 -1
- package/dist/core/export-html/tool-renderer.js +11 -7
- package/dist/core/extensions/jiti-loader.code.js +3 -0
- package/dist/core/extensions/jiti-loader.code.js.map +1 -0
- package/dist/core/extensions/jiti-loader.impl.js +35 -2
- package/dist/core/extensions/jiti-loader.js +11 -7
- package/dist/core/extensions/jiti-static-loader.code.js +3 -0
- package/dist/core/extensions/jiti-static-loader.code.js.map +1 -0
- package/dist/core/extensions/jiti-static-loader.impl.js +35 -2
- package/dist/core/extensions/jiti-static-loader.js +11 -7
- package/dist/core/extensions/loader-host-modules.code.js +3 -0
- package/dist/core/extensions/loader-host-modules.code.js.map +1 -0
- package/dist/core/extensions/loader-host-modules.impl.js +35 -2
- package/dist/core/extensions/loader-host-modules.js +11 -7
- package/dist/core/extensions/loader-virtual-modules.d.ts +1 -6
- package/dist/core/extensions/loader-virtual-modules.js +1 -1
- package/dist/core/extensions/runner-context.js +3 -3
- package/dist/core/extensions/session-events.d.ts +4 -0
- package/dist/core/extensions/session-events.js.map +1 -1
- package/dist/core/intercom-broker-control-host.code.js +5 -0
- package/dist/core/intercom-broker-control-host.code.js.map +1 -0
- package/dist/core/intercom-broker-control-host.impl.js +35 -4
- package/dist/core/intercom-broker-control-host.js +11 -7
- package/dist/core/intercom-broker-control.code.js +63 -0
- package/dist/core/intercom-broker-control.code.js.map +1 -0
- package/dist/core/intercom-broker-control.d.ts +21 -0
- package/dist/core/intercom-broker-control.impl.js +35 -17
- package/dist/core/intercom-broker-control.impl.js.map +1 -1
- package/dist/core/intercom-broker-control.js +11 -7
- package/dist/core/package-manager-storage-adapters.code.js +3 -0
- package/dist/core/package-manager-storage-adapters.code.js.map +1 -0
- package/dist/core/package-manager-storage-adapters.impl.js +35 -2
- package/dist/core/package-manager-storage-adapters.js +11 -7
- package/dist/core/process-start-token.d.ts +8 -2
- package/dist/core/process-tree-containment.d.ts +27 -0
- package/dist/core/queued-message-delivery.d.ts +5 -2
- package/dist/core/resume/blueprint/process.d.ts +3 -1
- package/dist/core/sdk.code.js +9 -0
- package/dist/core/sdk.code.js.map +1 -0
- package/dist/core/sdk.impl.js +35 -8
- package/dist/core/sdk.js +11 -7
- package/dist/core/session-manager-core.d.ts +15 -5
- package/dist/core/session-manager-core.js +195 -38
- package/dist/core/session-manager-core.js.map +1 -1
- package/dist/core/session-manager-list.code.js +3 -0
- package/dist/core/session-manager-list.code.js.map +1 -0
- package/dist/core/session-manager-list.impl.js +35 -2
- package/dist/core/session-manager-list.js +11 -7
- package/dist/core/session-manager-types.d.ts +20 -0
- package/dist/core/session-manager.code.js +8 -0
- package/dist/core/session-manager.code.js.map +1 -0
- package/dist/core/session-manager.d.ts +1 -1
- package/dist/core/session-manager.impl.js +35 -7
- package/dist/core/session-manager.js +11 -7
- package/dist/core/session-storage.code.js +3 -0
- package/dist/core/session-storage.code.js.map +1 -0
- package/dist/core/session-storage.impl.js +35 -2
- package/dist/core/session-storage.js +11 -7
- package/dist/core/storage/file-identity.d.ts +3 -0
- package/dist/core/storage/file-identity.js +296 -86
- package/dist/core/storage/file-identity.js.map +1 -1
- package/dist/core/storage/index.d.ts +1 -1
- package/dist/core/storage/index.js +52 -7
- package/dist/core/storage/index.js.map +1 -1
- package/dist/core/storage/node-sqlite-thread.js +47 -2
- package/dist/core/storage/node-sqlite.js +49 -4
- package/dist/core/storage/node-sqlite.js.map +1 -1
- package/dist/core/storage/offline-snapshot-file.code.js +37 -0
- package/dist/core/storage/offline-snapshot-file.code.js.map +1 -0
- package/dist/core/storage/offline-snapshot-file.impl.js +33 -34
- package/dist/core/storage/offline-snapshot-file.impl.js.map +1 -1
- package/dist/core/storage/offline-snapshot-file.js +11 -7
- package/dist/core/storage/provider.js +52 -7
- package/dist/core/storage/selection.js +49 -4
- package/dist/core/storage/session/branch-fork-source.code.js +146 -0
- package/dist/core/storage/session/branch-fork-source.code.js.map +1 -0
- package/dist/core/storage/session/branch-fork-source.impl.js +32 -97
- package/dist/core/storage/session/branch-fork-source.impl.js.map +1 -1
- package/dist/core/storage/session/branch-fork-source.js +11 -7
- package/dist/core/storage/session/card-view.code.js +60 -0
- package/dist/core/storage/session/card-view.code.js.map +1 -0
- package/dist/core/storage/session/card-view.impl.js +34 -58
- package/dist/core/storage/session/card-view.impl.js.map +1 -1
- package/dist/core/storage/session/card-view.js +11 -7
- package/dist/core/storage/session/child-reader.code.js +139 -0
- package/dist/core/storage/session/child-reader.code.js.map +1 -0
- package/dist/core/storage/session/child-reader.d.ts +1 -0
- package/dist/core/storage/session/child-reader.impl.js +33 -91
- package/dist/core/storage/session/child-reader.impl.js.map +1 -1
- package/dist/core/storage/session/child-reader.js +11 -7
- package/dist/core/storage/session/chunked-resume-projection.code.js +3 -0
- package/dist/core/storage/session/chunked-resume-projection.code.js.map +1 -0
- package/dist/core/storage/session/chunked-resume-projection.impl.js +35 -2
- package/dist/core/storage/session/chunked-resume-projection.js +11 -7
- package/dist/core/storage/session/expanded-child-details.code.js +684 -0
- package/dist/core/storage/session/expanded-child-details.code.js.map +1 -0
- package/dist/core/storage/session/expanded-child-details.impl.js +31 -634
- package/dist/core/storage/session/expanded-child-details.impl.js.map +1 -1
- package/dist/core/storage/session/expanded-child-details.js +11 -7
- package/dist/core/storage/session/fork-row-bytes.code.js +3 -0
- package/dist/core/storage/session/fork-row-bytes.code.js.map +1 -0
- package/dist/core/storage/session/fork-row-bytes.impl.js +35 -2
- package/dist/core/storage/session/fork-row-bytes.js +11 -7
- package/dist/core/storage/session/fork-row-index.code.js +3 -0
- package/dist/core/storage/session/fork-row-index.code.js.map +1 -0
- package/dist/core/storage/session/fork-row-index.impl.js +35 -2
- package/dist/core/storage/session/fork-row-index.js +11 -7
- package/dist/core/storage/session/fork-staging.code.js +580 -0
- package/dist/core/storage/session/fork-staging.code.js.map +1 -0
- package/dist/core/storage/session/fork-staging.impl.js +30 -574
- package/dist/core/storage/session/fork-staging.impl.js.map +1 -1
- package/dist/core/storage/session/fork-staging.js +11 -7
- package/dist/core/storage/session/fork-window.code.js +522 -0
- package/dist/core/storage/session/fork-window.code.js.map +1 -0
- package/dist/core/storage/session/fork-window.impl.js +30 -471
- package/dist/core/storage/session/fork-window.impl.js.map +1 -1
- package/dist/core/storage/session/fork-window.js +11 -7
- package/dist/core/storage/session/history-pages.code.js +3 -0
- package/dist/core/storage/session/history-pages.code.js.map +1 -0
- package/dist/core/storage/session/history-pages.impl.js +35 -2
- package/dist/core/storage/session/history-pages.js +11 -7
- package/dist/core/storage/session/jsonl-entry-projection.code.js +3 -0
- package/dist/core/storage/session/jsonl-entry-projection.code.js.map +1 -0
- package/dist/core/storage/session/jsonl-entry-projection.impl.js +35 -2
- package/dist/core/storage/session/jsonl-entry-projection.js +11 -7
- package/dist/core/storage/session/jsonl-session-store.code.js +3 -0
- package/dist/core/storage/session/jsonl-session-store.code.js.map +1 -0
- package/dist/core/storage/session/jsonl-session-store.impl.js +35 -2
- package/dist/core/storage/session/jsonl-session-store.js +11 -7
- package/dist/core/storage/session/memory-session-store.code.js +3 -0
- package/dist/core/storage/session/memory-session-store.code.js.map +1 -0
- package/dist/core/storage/session/memory-session-store.impl.js +35 -2
- package/dist/core/storage/session/memory-session-store.js +11 -7
- package/dist/core/storage/session/pinned-branch-resume.code.js +117 -0
- package/dist/core/storage/session/pinned-branch-resume.code.js.map +1 -0
- package/dist/core/storage/session/pinned-branch-resume.impl.js +33 -69
- package/dist/core/storage/session/pinned-branch-resume.impl.js.map +1 -1
- package/dist/core/storage/session/pinned-branch-resume.js +11 -7
- package/dist/core/storage/session/released-history-files.code.js +465 -0
- package/dist/core/storage/session/released-history-files.code.js.map +1 -0
- package/dist/core/storage/session/released-history-files.impl.js +32 -461
- package/dist/core/storage/session/released-history-files.impl.js.map +1 -1
- package/dist/core/storage/session/released-history-files.js +11 -7
- package/dist/core/storage/session/resident-fork-rows.code.js +57 -0
- package/dist/core/storage/session/resident-fork-rows.code.js.map +1 -0
- package/dist/core/storage/session/resident-fork-rows.impl.js +32 -53
- package/dist/core/storage/session/resident-fork-rows.impl.js.map +1 -1
- package/dist/core/storage/session/resident-fork-rows.js +11 -7
- package/dist/core/storage/session/session-resume.code.js +3 -0
- package/dist/core/storage/session/session-resume.code.js.map +1 -0
- package/dist/core/storage/session/session-resume.impl.js +35 -2
- package/dist/core/storage/session/session-resume.js +11 -7
- package/dist/core/storage/session/snapshot-cut.code.js +3 -0
- package/dist/core/storage/session/snapshot-cut.code.js.map +1 -0
- package/dist/core/storage/session/snapshot-cut.impl.js +35 -2
- package/dist/core/storage/session/snapshot-cut.js +11 -7
- package/dist/core/storage/session/snapshot-history.code.js +107 -0
- package/dist/core/storage/session/snapshot-history.code.js.map +1 -0
- package/dist/core/storage/session/snapshot-history.impl.js +34 -60
- package/dist/core/storage/session/snapshot-history.impl.js.map +1 -1
- package/dist/core/storage/session/snapshot-history.js +11 -7
- package/dist/core/storage/session/sql-session-store.code.js +3 -0
- package/dist/core/storage/session/sql-session-store.code.js.map +1 -0
- package/dist/core/storage/session/sql-session-store.impl.js +35 -2
- package/dist/core/storage/session/sql-session-store.js +11 -7
- package/dist/core/storage/session/tree-outline.code.js +3 -0
- package/dist/core/storage/session/tree-outline.code.js.map +1 -0
- package/dist/core/storage/session/tree-outline.impl.js +35 -2
- package/dist/core/storage/session/tree-outline.js +11 -7
- package/dist/core/storage/session/tree-presentation.code.js +342 -0
- package/dist/core/storage/session/tree-presentation.code.js.map +1 -0
- package/dist/core/storage/session/tree-presentation.impl.js +31 -337
- package/dist/core/storage/session/tree-presentation.impl.js.map +1 -1
- package/dist/core/storage/session/tree-presentation.js +11 -7
- package/dist/core/storage/session/types.d.ts +26 -0
- package/dist/core/storage/storage-status.code.js +3 -0
- package/dist/core/storage/storage-status.code.js.map +1 -0
- package/dist/core/storage/storage-status.impl.js +35 -2
- package/dist/core/storage/storage-status.js +11 -7
- package/dist/core/storage/team.code.js +3 -0
- package/dist/core/storage/team.code.js.map +1 -0
- package/dist/core/storage/team.impl.js +35 -2
- package/dist/core/storage/team.js +11 -7
- package/dist/core/structured-output/index.code.js +3 -0
- package/dist/core/structured-output/index.code.js.map +1 -0
- package/dist/core/structured-output/index.impl.js +35 -2
- package/dist/core/structured-output/index.js +11 -7
- package/dist/core/tasks/command-output.d.ts +4 -1
- package/dist/core/tasks/supervisor.d.ts +2 -0
- package/dist/core/tasks/task-completion-entry.code.js +254 -0
- package/dist/core/tasks/task-completion-entry.code.js.map +1 -0
- package/dist/core/tasks/task-completion-entry.impl.js +33 -251
- package/dist/core/tasks/task-completion-entry.impl.js.map +1 -1
- package/dist/core/tasks/task-completion-entry.js +11 -7
- package/dist/core/tasks/task-file-history.code.js +424 -0
- package/dist/core/tasks/task-file-history.code.js.map +1 -0
- package/dist/core/tasks/task-file-history.impl.js +32 -375
- package/dist/core/tasks/task-file-history.impl.js.map +1 -1
- package/dist/core/tasks/task-file-history.js +11 -7
- package/dist/core/tasks/{task-reference-archive.impl.js → task-reference-archive.code.js} +51 -6
- package/dist/core/tasks/{task-reference-archive.impl.js.map → task-reference-archive.code.js.map} +1 -1
- package/dist/core/tasks/task-reference-archive.js +11 -7
- package/dist/core/tasks/{task-reference-identity.impl.js → task-reference-identity.code.js} +1 -1
- package/dist/core/tasks/{task-reference-identity.impl.js.map → task-reference-identity.code.js.map} +1 -1
- package/dist/core/tasks/task-reference-identity.js +11 -7
- package/dist/core/tasks/task-reference-stream.code.js +3 -0
- package/dist/core/tasks/task-reference-stream.code.js.map +1 -0
- package/dist/core/tasks/task-reference-stream.impl.js +35 -2
- package/dist/core/tasks/task-reference-stream.js +11 -7
- package/dist/core/tasks/task-unsaved-answer.code.js +3 -0
- package/dist/core/tasks/task-unsaved-answer.code.js.map +1 -0
- package/dist/core/tasks/task-unsaved-answer.impl.js +35 -2
- package/dist/core/tasks/task-unsaved-answer.js +11 -7
- package/dist/core/tasks/task-view-source.code.js +3 -0
- package/dist/core/tasks/task-view-source.code.js.map +1 -0
- package/dist/core/tasks/task-view-source.d.ts +9 -0
- package/dist/core/tasks/task-view-source.impl.js +35 -2
- package/dist/core/tasks/task-view-source.js +12 -8
- package/dist/core/tools/read-url.code.js +1530 -0
- package/dist/core/tools/read-url.code.js.map +1 -0
- package/dist/core/tools/read-url.impl.js +30 -1524
- package/dist/core/tools/read-url.impl.js.map +1 -1
- package/dist/core/tools/read-url.js +11 -7
- package/dist/core/worktree-snapshot/blob-store.js +50 -5
- package/dist/core/worktree-snapshot/errors.code.js +6 -0
- package/dist/core/worktree-snapshot/errors.code.js.map +1 -0
- package/dist/core/worktree-snapshot/errors.impl.js +35 -5
- package/dist/core/worktree-snapshot/errors.js +11 -7
- package/dist/core/worktree-snapshot/session-index-reset.code.js +136 -0
- package/dist/core/worktree-snapshot/session-index-reset.code.js.map +1 -0
- package/dist/core/worktree-snapshot/session-index-reset.impl.js +34 -134
- package/dist/core/worktree-snapshot/session-index-reset.impl.js.map +1 -1
- package/dist/core/worktree-snapshot/session-index-reset.js +11 -7
- package/dist/extensions/codemode/execute.code.js +618 -0
- package/dist/extensions/codemode/execute.code.js.map +1 -0
- package/dist/extensions/codemode/execute.impl.js +30 -612
- package/dist/extensions/codemode/execute.impl.js.map +1 -1
- package/dist/extensions/codemode/execute.js +11 -7
- package/dist/extensions/codemode/renderer.code.js +110 -0
- package/dist/extensions/codemode/renderer.code.js.map +1 -0
- package/dist/extensions/codemode/renderer.impl.js +33 -107
- package/dist/extensions/codemode/renderer.impl.js.map +1 -1
- package/dist/extensions/codemode/renderer.js +11 -7
- package/dist/extensions/mcp/cli.code.js +565 -0
- package/dist/extensions/mcp/cli.code.js.map +1 -0
- package/dist/extensions/mcp/cli.impl.js +32 -561
- package/dist/extensions/mcp/cli.impl.js.map +1 -1
- package/dist/extensions/mcp/cli.js +11 -7
- package/dist/extensions/mcp/cli.lazy.code.js +52 -0
- package/dist/extensions/mcp/cli.lazy.code.js.map +1 -0
- package/dist/extensions/mcp/cli.lazy.impl.js +36 -7
- package/dist/extensions/mcp/cli.lazy.impl.js.map +1 -1
- package/dist/extensions/mcp/cli.lazy.js +11 -7
- package/dist/extensions/mcp/client/transports/stdio.d.ts +2 -0
- package/dist/extensions/mcp/runtime.code.js +5 -0
- package/dist/extensions/mcp/runtime.code.js.map +1 -0
- package/dist/extensions/mcp/runtime.impl.js +35 -4
- package/dist/extensions/mcp/runtime.js +11 -7
- package/dist/index.d.ts +1 -1
- package/dist/index.js +8 -8
- package/dist/main-first-time-setup.code.js +12 -0
- package/dist/main-first-time-setup.code.js.map +1 -0
- package/dist/main-first-time-setup.impl.js +35 -11
- package/dist/main-first-time-setup.impl.js.map +1 -1
- package/dist/main-first-time-setup.js +11 -7
- package/dist/main.d.ts +1 -1
- package/dist/main.js +2 -2
- package/dist/modes/interactive/agent-view/agent-view-switcher.d.ts +6 -1
- package/dist/modes/interactive/agent-view/contracts.d.ts +4 -0
- package/dist/modes/interactive/chat-reader-records.d.ts +1 -1
- package/dist/modes/interactive/components/stored-tree-view.code.js +338 -0
- package/dist/modes/interactive/components/stored-tree-view.code.js.map +1 -0
- package/dist/modes/interactive/components/stored-tree-view.impl.js +33 -290
- package/dist/modes/interactive/components/stored-tree-view.impl.js.map +1 -1
- package/dist/modes/interactive/components/stored-tree-view.js +11 -7
- package/dist/modes/interactive/components/task-selection-state.d.ts +8 -3
- package/dist/modes/interactive/components/task-transcript-page.d.ts +1 -0
- package/dist/modes/interactive/components/user-message-selector-archive.code.js +191 -0
- package/dist/modes/interactive/components/user-message-selector-archive.code.js.map +1 -0
- package/dist/modes/interactive/components/user-message-selector-archive.impl.js +34 -189
- package/dist/modes/interactive/components/user-message-selector-archive.impl.js.map +1 -1
- package/dist/modes/interactive/components/user-message-selector-archive.js +11 -7
- package/dist/modes/interactive/interactive-mode-base.d.ts +6 -0
- package/dist/modes/interactive/interactive-mode.js +1 -1
- package/dist/modes/interactive/interactive-queueing.d.ts +4 -0
- package/dist/modes/interactive/interactive-render-chat.d.ts +22 -0
- package/dist/modes/interactive/interactive-startup.d.ts +2 -0
- package/dist/modes/interactive/program-status-reporter.d.ts +2 -1
- package/dist/modes/interactive/startup-shell-eligibility.js +1 -1
- package/dist/modes/interactive/steering-queue-controller.d.ts +4 -0
- package/dist/modes/interactive/theme/theme-schema-factory.d.ts +26 -0
- package/dist/modes/interactive/theme/theme-schema.d.ts +4 -5
- package/dist/modes/interactive-engine/engine-prewarm.d.ts +30 -5
- package/dist/modes/interactive-engine/engine-prewarm.js +1 -1
- package/dist/modes/interactive-engine/isolated-runtime.d.ts +7 -0
- package/dist/modes/interactive-engine/protocol.d.ts +32 -1
- package/dist/modes/rpc/agent-view-rpc-types.d.ts +12 -1
- package/dist/modes/rpc/agent-view-rpc-types.js.map +1 -1
- package/dist/modes/rpc/rpc-client-api.d.ts +3 -1
- package/dist/modes/rpc/rpc-types.d.ts +12 -0
- package/dist/package-manager-cli.code.js +1140 -0
- package/dist/package-manager-cli.code.js.map +1 -0
- package/dist/package-manager-cli.impl.js +36 -1067
- package/dist/package-manager-cli.impl.js.map +1 -1
- package/dist/package-manager-cli.js +11 -7
- package/dist/rpc-entry.js +2 -2
- 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/13.js +1 -0
- package/dist/startup-stages/2.js +1 -7
- package/dist/startup-stages/3.js +6 -2
- package/dist/startup-stages/4.js +2 -1
- package/dist/startup-stages/5.js +1 -7
- package/dist/startup-stages/6.js +7 -10
- package/dist/startup-stages/7.js +10 -3
- package/dist/startup-stages/8.js +3 -6
- package/dist/startup-stages/9.js +6 -1
- package/dist/startup-stages/stages.json +1 -1
- package/dist/utils/canonical-path.js +2 -0
- package/dist/utils/canonical-path.js.map +1 -1
- package/dist/utils/child-process.d.ts +41 -1
- package/dist/utils/compile-cache-warmup-host.js +63 -18
- package/dist/utils/compile-cache-warmup-host.js.map +1 -1
- package/dist/utils/credential-redaction.d.ts +50 -1
- package/dist/utils/interactive-engine-bootstrap.d.ts +2 -0
- package/dist/utils/interactive-engine-env.d.ts +2 -0
- package/dist/utils/memoize-async.d.ts +32 -0
- package/dist/utils/memoize-async.js +91 -0
- package/dist/utils/memoize-async.js.map +1 -0
- package/dist/utils/photon.js +19 -29
- package/dist/utils/photon.js.map +1 -1
- package/dist/utils/shell.d.ts +7 -4
- package/dist/utils/startup-database-spare.code.js +108 -0
- package/dist/utils/startup-database-spare.code.js.map +1 -0
- package/dist/utils/startup-database-spare.impl.js +33 -105
- package/dist/utils/startup-database-spare.impl.js.map +1 -1
- package/dist/utils/startup-database-spare.js +11 -7
- package/dist/utils/tools-manager.code.js +3 -0
- package/dist/utils/tools-manager.code.js.map +1 -0
- package/dist/utils/tools-manager.impl.js +35 -2
- package/dist/utils/tools-manager.js +11 -7
- package/dist/utils/windows-cwd.js +585 -0
- package/dist/utils/windows-cwd.js.map +1 -0
- package/docs/extensions.md +1 -1
- package/docs/getting-started/installation.md +2 -0
- package/docs/intercom/operations.md +1 -0
- package/docs/reference/cli.md +1 -1
- package/package.json +24 -24
- package/dist/_shared/mode-shared-HP7GUA7J.js.map +0 -1
- package/dist/_shared/p1a-prewarm-YK3KDTLN.js.map +0 -1
- package/dist/_shared/p1b-elig-PXQA2FYP.js.map +0 -1
- package/dist/_shared/startup-0-0-X4NF3GKM.js.map +0 -1
- package/dist/_shared/startup-0-1-7QTG6VJZ.js.map +0 -1
- package/dist/_shared/startup-0-2-KFDYPDJK.js.map +0 -1
- package/dist/_shared/startup-8-1-KQJBDKRU.js.map +0 -1
|
@@ -5,8 +5,11 @@ import { normalizeGroup } from "../group.js";
|
|
|
5
5
|
import { isAgentRecipient, NON_AGENT_RECIPIENT_REFUSAL, TERMINAL_CHILD_ASK_REFUSAL } from "../recipient-purpose.js";
|
|
6
6
|
import {
|
|
7
7
|
DELIVERED_MESSAGE_TTL_MS,
|
|
8
|
+
HELD_SEND_GRACE_MS,
|
|
8
9
|
RECIPIENT_RECEIPT_TIMEOUT_MS,
|
|
9
10
|
RECIPIENT_SAVE_MAX_WAIT_MS,
|
|
11
|
+
RECIPIENT_SAVING_REPORT_INTERVAL_MS,
|
|
12
|
+
SEND_RESPONSE_TIMEOUT_MS,
|
|
10
13
|
} from "../retry-policy.js";
|
|
11
14
|
import {
|
|
12
15
|
resolveSessionTarget,
|
|
@@ -17,7 +20,7 @@ import {
|
|
|
17
20
|
import type { DeliveryFailureReasonCode, Message, SessionInfo } from "../types.js";
|
|
18
21
|
import { ASK_FAILURE_QUEUED, crashPoint } from "./crash-points.js";
|
|
19
22
|
import type { DeliveredMessageCache } from "./delivered-message-cache.js";
|
|
20
|
-
import { sameGroup } from "./group-isolation.js";
|
|
23
|
+
import { isVerticalBypass, sameGroup } from "./group-isolation.js";
|
|
21
24
|
import {
|
|
22
25
|
reachesGroups,
|
|
23
26
|
reachesSession,
|
|
@@ -40,6 +43,8 @@ import {
|
|
|
40
43
|
type KnownSessionOwnership,
|
|
41
44
|
type KnownSessionState,
|
|
42
45
|
type Lease,
|
|
46
|
+
type LeaseRenewalRequest,
|
|
47
|
+
type LeaseRenewResult,
|
|
43
48
|
type LeaseState,
|
|
44
49
|
OUTBOX_BACKUP_POLL_INTERVAL_MS,
|
|
45
50
|
OUTBOX_CLAIM_STALE_AFTER_MS,
|
|
@@ -59,6 +64,13 @@ import {
|
|
|
59
64
|
type SentMessageReservation,
|
|
60
65
|
type SessionIdReclaimResult,
|
|
61
66
|
} from "./multinode-state-store.js";
|
|
67
|
+
import {
|
|
68
|
+
BROKER_CONNECT_TIMEOUT_MS,
|
|
69
|
+
PRESENCE_REGISTRATION_BOUND_MS,
|
|
70
|
+
REGISTRATION_IPC_MARGIN_MS,
|
|
71
|
+
SESSION_ID_CLAIM_BOUND_MS,
|
|
72
|
+
SESSION_ID_CLAIM_PROGRESS_CAP_MS,
|
|
73
|
+
} from "./registration-bounds.js";
|
|
62
74
|
import type { PendingQuestion, PendingQuestionIndex } from "./pending-question-index.js";
|
|
63
75
|
import { ROUTE_UNAVAILABLE_REASON, UNAUTHORIZED_REPLY_REASON } from "./delivery-failure-reason.js";
|
|
64
76
|
import { UNCERTAIN_RECEIPT_REASON } from "./recipient-receipts.js";
|
|
@@ -84,6 +96,12 @@ import {
|
|
|
84
96
|
restoringRecipientRetryReason,
|
|
85
97
|
unrestoredReasonCode,
|
|
86
98
|
} from "./restoring-recipients.js";
|
|
99
|
+
import {
|
|
100
|
+
SendOrderGate,
|
|
101
|
+
type SendOrderTicket,
|
|
102
|
+
type TargetMatchInfo,
|
|
103
|
+
type WaitForEarlierOptions,
|
|
104
|
+
} from "./send-order-gate.js";
|
|
87
105
|
import {
|
|
88
106
|
answerAs,
|
|
89
107
|
type BrokerConnectedSession,
|
|
@@ -96,7 +114,13 @@ import {
|
|
|
96
114
|
type SendClientMessage,
|
|
97
115
|
senderGroupIdentity,
|
|
98
116
|
} from "./send-handler.js";
|
|
99
|
-
import {
|
|
117
|
+
import {
|
|
118
|
+
RETURN_PATH_MAX_ENTRIES,
|
|
119
|
+
RETURN_PATH_OPENING_WAIT_MS,
|
|
120
|
+
type ReturnPathOpeningWaitOutcome,
|
|
121
|
+
type ReturnPathParty,
|
|
122
|
+
returnPathClosedReason,
|
|
123
|
+
} from "./return-paths.js";
|
|
100
124
|
import { deliverHeldToAsker } from "./ask-failures.js";
|
|
101
125
|
import { brokerHostIdentity, isProcessAlive, processStartedAt } from "./broker-instance.js";
|
|
102
126
|
import { type LostRecipientBounds, lostRecipientBounds, lostRecipientReason } from "./lost-recipients.js";
|
|
@@ -133,6 +157,11 @@ const MAX_HELD_SENDS = 256;
|
|
|
133
157
|
* `RECIPIENT_RECEIPT_TIMEOUT_MS`).
|
|
134
158
|
*/
|
|
135
159
|
const HOLD_CHECK_GRACE_MS = 500;
|
|
160
|
+
/**
|
|
161
|
+
* Minimum client-deadline margin past a receipt wait so `expireWaiter` and its provisional hold still land strictly
|
|
162
|
+
* before the sender's client times out (`SEND_RESPONSE_TIMEOUT_MS`).
|
|
163
|
+
*/
|
|
164
|
+
const MIN_WAIT_CLIENT_MARGIN_MS = 2 * HOLD_CHECK_GRACE_MS;
|
|
136
165
|
/** A session that moved here may find its presence lease still held by the broker it just left. */
|
|
137
166
|
const PRESENCE_HANDOVER_ATTEMPTS = 4;
|
|
138
167
|
const PRESENCE_HANDOVER_RETRY_MS = 50;
|
|
@@ -141,30 +170,19 @@ const PRESENCE_HANDOVER_RETRY_MS = 50;
|
|
|
141
170
|
* session's asks) are tried, once when due and then at each renewal, before a failure that keeps recurring is given up.
|
|
142
171
|
*/
|
|
143
172
|
export const REGISTRATION_STEP_ATTEMPTS = 30;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* The longest a registration that asks for its id back waits for the shared database to bind it, before the presence
|
|
151
|
-
* wait: both together stay within the client's 10 s. Past it the session gets a fresh id, never an unchecked one.
|
|
152
|
-
*/
|
|
153
|
-
export const SESSION_ID_CLAIM_BOUND_MS = 2_500;
|
|
154
|
-
/**
|
|
155
|
-
* The longest a claim waits in all once it found the id owned by a broker of this host that has exited: each step that
|
|
156
|
-
* gives that broker's ids up and binds the id again has `SESSION_ID_CLAIM_BOUND_MS` of its own, as a shared database
|
|
157
|
-
* that answers slowly takes one round trip for each, up to this. The registration's presence wait is shortened by the
|
|
158
|
-
* extra time (`presenceBoundAfterClaim`), so both together stay within the client's 10 s.
|
|
159
|
-
*/
|
|
160
|
-
export const SESSION_ID_CLAIM_PROGRESS_CAP_MS = 2 * SESSION_ID_CLAIM_BOUND_MS;
|
|
173
|
+
export {
|
|
174
|
+
PRESENCE_REGISTRATION_BOUND_MS,
|
|
175
|
+
SESSION_ID_CLAIM_BOUND_MS,
|
|
176
|
+
SESSION_ID_CLAIM_PROGRESS_CAP_MS,
|
|
177
|
+
};
|
|
161
178
|
|
|
162
179
|
/**
|
|
163
180
|
* What a claim reports to its registration: how much longer than `SESSION_ID_CLAIM_BOUND_MS` it gave itself to take the
|
|
164
|
-
* id from a broker of this host that has exited (0 when it did not).
|
|
181
|
+
* id from a broker of this host that has exited (0 when it did not), and how long the claim actually took overall.
|
|
165
182
|
*/
|
|
166
183
|
export interface SessionIdClaimProgress {
|
|
167
184
|
extendedMs: number;
|
|
185
|
+
elapsedMs?: number;
|
|
168
186
|
}
|
|
169
187
|
|
|
170
188
|
/**
|
|
@@ -176,6 +194,28 @@ export function presenceBoundAfterClaim(extendedMs: number): number {
|
|
|
176
194
|
const most = SESSION_ID_CLAIM_PROGRESS_CAP_MS - SESSION_ID_CLAIM_BOUND_MS;
|
|
177
195
|
return PRESENCE_REGISTRATION_BOUND_MS - Math.min(most, Math.max(0, extendedMs));
|
|
178
196
|
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* How long a registration waits for its binding write, known-session record, and presence lease after its id claim
|
|
200
|
+
* finishes, derived from the client's real remaining deadline.
|
|
201
|
+
* - Never exceeds `presenceBoundAfterClaim(progress.extendedMs)`, preserving exact healthy (5 s) and exited-broker
|
|
202
|
+
* takeover bounds.
|
|
203
|
+
* - Reserves `min(REGISTRATION_IPC_MARGIN_MS, floor(remainingMs / 2))` below the client's remaining deadline so the
|
|
204
|
+
* broker answers before the client times out without collapsing to 0 while time remains on the client's clock
|
|
205
|
+
* (whether default 10 s or a custom `connectTimeoutMs`).
|
|
206
|
+
*/
|
|
207
|
+
export function registrationBoundAfterClaim(progress: SessionIdClaimProgress, clientRemainingMs?: number): number {
|
|
208
|
+
const base = presenceBoundAfterClaim(progress.extendedMs);
|
|
209
|
+
const remainingMs =
|
|
210
|
+
clientRemainingMs !== undefined && Number.isFinite(clientRemainingMs)
|
|
211
|
+
? Math.max(0, clientRemainingMs)
|
|
212
|
+
: progress.elapsedMs !== undefined && Number.isFinite(progress.elapsedMs)
|
|
213
|
+
? Math.max(0, BROKER_CONNECT_TIMEOUT_MS - Math.max(0, progress.elapsedMs))
|
|
214
|
+
: undefined;
|
|
215
|
+
if (remainingMs === undefined) return base;
|
|
216
|
+
const margin = Math.min(REGISTRATION_IPC_MARGIN_MS, Math.floor(remainingMs / 2));
|
|
217
|
+
return Math.min(base, remainingMs - margin);
|
|
218
|
+
}
|
|
179
219
|
/**
|
|
180
220
|
* #5607: waits between a returning registration's attempts at an id that another registration of the same session
|
|
181
221
|
* still owns, as when its broker is still releasing it after that session moved here. Tried while within the claim's
|
|
@@ -481,6 +521,10 @@ interface Inflight {
|
|
|
481
521
|
readonly released?: ReleasedReplyHandoff;
|
|
482
522
|
/** When the frame was handed to the recipient: its answers from then on count toward the return path it opens. */
|
|
483
523
|
readonly handedOverAt?: number;
|
|
524
|
+
/** Revocation mark taken at hand-over so a permanently ended responder or sender gets no late path. */
|
|
525
|
+
readonly handedOverMark?: number;
|
|
526
|
+
/** Ends the pending return-path opening hold when this delivery settles or is abandoned. */
|
|
527
|
+
readonly finishOpening?: () => void;
|
|
484
528
|
}
|
|
485
529
|
|
|
486
530
|
interface ReleasedReplyHandoff {
|
|
@@ -494,6 +538,8 @@ interface Waiter {
|
|
|
494
538
|
readonly socket: BrokerConnectedSession["socket"];
|
|
495
539
|
readonly attemptId?: string;
|
|
496
540
|
readonly senderSessionId: string;
|
|
541
|
+
/** Canonical sender identity for per-(sender, target) outbox-before-local ordering. */
|
|
542
|
+
readonly senderKey: string;
|
|
497
543
|
/** The sender's groups: the released child's reservation it may read when its send is queued (#4512). */
|
|
498
544
|
readonly sender: ReservationSender;
|
|
499
545
|
readonly expectsReply: boolean;
|
|
@@ -505,6 +551,8 @@ interface Waiter {
|
|
|
505
551
|
timer: ReturnType<typeof setTimeout> | undefined;
|
|
506
552
|
/** When, by `performance.now()`, the sender started waiting; a recipient's save reports never keep it waiting past the save cap (#3983). */
|
|
507
553
|
readonly waitingSince: number;
|
|
554
|
+
/** Monotonic time when the sender's client would time out unless extended with `send_held`. */
|
|
555
|
+
clientDeadlineAt: number;
|
|
508
556
|
/** Ends the wait as uncertain at the save cap, whatever the store does (#3983). */
|
|
509
557
|
capTimer?: ReturnType<typeof setTimeout>;
|
|
510
558
|
/** The hold check whose outbox read is in flight; a wait that ends meanwhile reuses it (#3983). */
|
|
@@ -517,6 +565,21 @@ interface Waiter {
|
|
|
517
565
|
missedSavingReport?: boolean;
|
|
518
566
|
}
|
|
519
567
|
|
|
568
|
+
interface SendProgress {
|
|
569
|
+
readonly startedAt: number;
|
|
570
|
+
clientDeadlineAt: number;
|
|
571
|
+
hadProgress: boolean;
|
|
572
|
+
lastProgressAt: number;
|
|
573
|
+
reportingEnabled: boolean;
|
|
574
|
+
readonly socket: BrokerConnectedSession["socket"];
|
|
575
|
+
readonly messageId: string;
|
|
576
|
+
readonly attemptId?: string;
|
|
577
|
+
readonly ticket?: SendOrderTicket;
|
|
578
|
+
waitingOnReplyKey?: string;
|
|
579
|
+
scheduledFireAt?: number;
|
|
580
|
+
stopReporting?: () => void;
|
|
581
|
+
}
|
|
582
|
+
|
|
520
583
|
interface TrackedHold {
|
|
521
584
|
readonly messageId: string;
|
|
522
585
|
readonly reservationKey: string;
|
|
@@ -530,6 +593,8 @@ interface TrackedHold {
|
|
|
530
593
|
}
|
|
531
594
|
|
|
532
595
|
interface PresenceEntry {
|
|
596
|
+
/** Monotonic registration epoch on this broker; used to skip unrelated earlier remote sends in O(1). */
|
|
597
|
+
readonly registeredSeq: number;
|
|
533
598
|
lease?: Lease;
|
|
534
599
|
/** `Date.now()` when this broker last wrote the session's known-session record. */
|
|
535
600
|
knownAt?: number;
|
|
@@ -1024,6 +1089,19 @@ export class MultiNodeRouter {
|
|
|
1024
1089
|
private readonly presence = new Map<BrokerConnectedSession, PresenceEntry>();
|
|
1025
1090
|
private readonly inflight = new Map<string, Inflight>();
|
|
1026
1091
|
private readonly waiters = new Map<string, Waiter>();
|
|
1092
|
+
/** Monotonic start, store-progress, and client-deadline state for each in-flight send routed through this router. */
|
|
1093
|
+
private readonly sendProgress = new WeakMap<SendClientMessage, SendProgress>();
|
|
1094
|
+
/** Associates each `SendOrderTicket` with its `SendProgress` so queued sends can track their blocker's progress. */
|
|
1095
|
+
private readonly ticketProgress = new WeakMap<SendOrderTicket, SendProgress>();
|
|
1096
|
+
/** Active and most-recent reply progress per sender reply lane (`serial("reply:...")`). */
|
|
1097
|
+
private readonly replyLanes = new Map<
|
|
1098
|
+
string,
|
|
1099
|
+
{ active?: SendOrderTicket; lastProgress?: SendProgress; queued: number }
|
|
1100
|
+
>();
|
|
1101
|
+
/** Sends currently waiting on an earlier send in `SendOrderGate` or `serial("reply:...")`, by `senderKey`. */
|
|
1102
|
+
private readonly queuedProgress = new Map<string, Set<SendProgress>>();
|
|
1103
|
+
/** Active progress-reporting timer disposers, cancelled in `close()`. */
|
|
1104
|
+
private readonly progressStops = new Set<() => void>();
|
|
1027
1105
|
private readonly holds = new Map<string, TrackedHold>();
|
|
1028
1106
|
private readonly mirrored = new Map<string, MirroredReservation>();
|
|
1029
1107
|
/** Reservations with a backup-poll wake check queued or running: one each while the store stalls, not a backlog. */
|
|
@@ -1032,9 +1110,20 @@ export class MultiNodeRouter {
|
|
|
1032
1110
|
private readonly unpublished = new Map<string, UnpublishedReservation>();
|
|
1033
1111
|
private readonly pendingPublishRetries = new Set<string>();
|
|
1034
1112
|
private readonly heldIdentities = new Set<string>();
|
|
1035
|
-
private readonly scanning = new Map<string, { rescan: boolean }>();
|
|
1113
|
+
private readonly scanning = new Map<string, { rescan: boolean; done: Promise<void> }>();
|
|
1114
|
+
private readonly initialScans = new Map<string, { readonly promise: Promise<void>; readonly resolve: () => void }>();
|
|
1115
|
+
/** Refcount of unsettled outbox waiters per `(senderKey, targetIdentity)` pair, maintained by `setWaiter`/`dropWaiter`. */
|
|
1116
|
+
private readonly unsettledOutboxPairs = new Map<string, number>();
|
|
1117
|
+
private readonly sendOrder = new SendOrderGate();
|
|
1118
|
+
private activeLocalCommit:
|
|
1119
|
+
| { readonly clientMessage: SendClientMessage; reentrant?: RemoteSendRequest }
|
|
1120
|
+
| undefined;
|
|
1121
|
+
private registrationSeq = 0;
|
|
1122
|
+
private checkingHolds: { again: boolean } | undefined;
|
|
1036
1123
|
private readonly serials = new Map<string, Promise<void>>();
|
|
1037
1124
|
private readonly settling = new Set<Promise<void>>();
|
|
1125
|
+
/** Every active pending return-path opening owned by this router (`inflight` and `settleDelivered`), by release handle. */
|
|
1126
|
+
private readonly activeOpenings = new Map<() => void, BrokerConnectedSession>();
|
|
1038
1127
|
/**
|
|
1039
1128
|
* When rows whose settle failed were first handed over, by message id: the recipient may have read such a row, and
|
|
1040
1129
|
* answered, before it is handed over again, so the path it opens counts answers from its first hand-over on.
|
|
@@ -1062,10 +1151,18 @@ export class MultiNodeRouter {
|
|
|
1062
1151
|
private readonly claimingHandoffs = new ReportingMap<string, ReservedId>();
|
|
1063
1152
|
/** #5607: the identities whose kept hand-over is to be written at the end of this turn (`saveHandoffsSoon`). */
|
|
1064
1153
|
private readonly unsavedHandoffs = new Set<string>();
|
|
1065
|
-
/**
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1154
|
+
/**
|
|
1155
|
+
* #5607: tokens of held ids that a registration coming back is taking, until it is registered here, by claim: two
|
|
1156
|
+
* registrations of a session can take the same hold, and each stops taking it on its own.
|
|
1157
|
+
*/
|
|
1158
|
+
private readonly consumingClaims = new Map<string, string>();
|
|
1159
|
+
/** #5607: tokens of unanswered claims whose registration's binding write has not decided yet, by claim. */
|
|
1160
|
+
private readonly decidingClaims = new Map<string, string>();
|
|
1161
|
+
/**
|
|
1162
|
+
* #5607: the unanswered claims whose binding write may still bind their id, by claim, each told whether its
|
|
1163
|
+
* registration uses that id (`claimLanding`).
|
|
1164
|
+
*/
|
|
1165
|
+
private readonly unansweredClaims = new Map<string, PromiseWithResolvers<"kept" | "unused">>();
|
|
1069
1166
|
/** #5607: when the shared store last answered one of this broker's statements. */
|
|
1070
1167
|
private storeAnsweredAt = Number.NEGATIVE_INFINITY;
|
|
1071
1168
|
/** #5607: what the instance key of every broker on this host starts with, when this host can be told apart. */
|
|
@@ -1288,7 +1385,7 @@ export class MultiNodeRouter {
|
|
|
1288
1385
|
};
|
|
1289
1386
|
const owner = this.newOwner(reserved?.token);
|
|
1290
1387
|
if (reserved !== undefined) {
|
|
1291
|
-
this.
|
|
1388
|
+
this.consumingClaims.set(claim, reserved.token);
|
|
1292
1389
|
// Saved as held until the claim decides: a broker that stops meanwhile leaves it for the next one.
|
|
1293
1390
|
if (identity !== undefined) this.claimingHandoffs.set(identity, reserved);
|
|
1294
1391
|
}
|
|
@@ -1390,6 +1487,8 @@ export class MultiNodeRouter {
|
|
|
1390
1487
|
if (claimed.status === "unanswered") {
|
|
1391
1488
|
expired = true;
|
|
1392
1489
|
attempts.catch(() => {});
|
|
1490
|
+
// #5607: an id the attempt binds after this is given back once the registration does not use it (`claimLanding`).
|
|
1491
|
+
this.claimLanding({ intercomSessionId: sessionId, token: owner.token }, claim, attempts);
|
|
1393
1492
|
// #5607: held for the session until that write decides: kept, it is the registration's; not kept, the session
|
|
1394
1493
|
// awaits it with the hand-over's recovery, and is handed it again (`claimAwaitedIds`).
|
|
1395
1494
|
if (reserved !== undefined) this.holdAgain(identity, reserved);
|
|
@@ -1413,13 +1512,16 @@ export class MultiNodeRouter {
|
|
|
1413
1512
|
} finally {
|
|
1414
1513
|
clearTimeout(bound);
|
|
1415
1514
|
clearTimeout(cap);
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1515
|
+
if (progress !== undefined) {
|
|
1516
|
+
progress.elapsedMs = Math.max(0, performance.now() - started.monotonic);
|
|
1517
|
+
// What the claim took past its own bound because it gave an exited broker's ids up, and only that: a claim answered
|
|
1518
|
+
// within its bound, or late for any other reason (a stalled broker), takes nothing off `extendedMs`.
|
|
1519
|
+
if (extended)
|
|
1520
|
+
progress.extendedMs = Math.max(0, (gaveUpAt ?? Math.min(deadline, elapsed())) - retryDeadline);
|
|
1521
|
+
}
|
|
1420
1522
|
// A held token stays in use until the registration that took it is registered here, which its broker does as
|
|
1421
1523
|
// it reads this answer.
|
|
1422
|
-
if (reserved !== undefined) setImmediate(() => this.
|
|
1524
|
+
if (reserved !== undefined) setImmediate(() => this.consumingClaims.delete(claim));
|
|
1423
1525
|
if (identity !== undefined && this.claimingHandoffs.get(identity) === reserved)
|
|
1424
1526
|
this.claimingHandoffs.delete(identity);
|
|
1425
1527
|
}
|
|
@@ -1454,10 +1556,131 @@ export class MultiNodeRouter {
|
|
|
1454
1556
|
* #5607: give an owned id back; a store without owners has nothing to give back. A failure is reported: the id is free
|
|
1455
1557
|
* again once its owner is no longer renewed.
|
|
1456
1558
|
*/
|
|
1457
|
-
async releaseOwner(ownership: KnownSessionOwnership): Promise<void> {
|
|
1559
|
+
async releaseOwner(ownership: KnownSessionOwnership, claim?: string): Promise<void> {
|
|
1560
|
+
// #5607: given back by the registration of the unanswered `claim` that took it (`claimLanding`).
|
|
1561
|
+
if (claim !== undefined) this.claimDecided(claim, "unused");
|
|
1458
1562
|
await this.state.knownSessions.owners?.release(ownership).catch((error: Error) => this.report(error));
|
|
1459
1563
|
}
|
|
1460
1564
|
|
|
1565
|
+
/**
|
|
1566
|
+
* #5607: the binding write of a claim that gave up before it answered may still bind the id with the claim's token, in
|
|
1567
|
+
* the database, after the registration decided it does not use that id: a pooled database may run it after the
|
|
1568
|
+
* registration's own writes. Once both the write has bound the id and the registration has decided so
|
|
1569
|
+
* (`claimDecided`), whichever comes first, the id is given back, unless something here still holds it with that token:
|
|
1570
|
+
* a hand-over's claim writes with its hold's token, which another registration of the session may be taking the id
|
|
1571
|
+
* with, and that hold gives the id back as it ends (`lapse`).
|
|
1572
|
+
*/
|
|
1573
|
+
private claimLanding(ownership: KnownSessionOwnership, claim: string, attempts: Promise<SessionIdClaim>): void {
|
|
1574
|
+
const use = Promise.withResolvers<"kept" | "unused">();
|
|
1575
|
+
this.unansweredClaims.set(claim, use);
|
|
1576
|
+
void attempts
|
|
1577
|
+
.then(
|
|
1578
|
+
async (landed) => {
|
|
1579
|
+
if (landed.status !== "granted" || (await use.promise) !== "unused") return;
|
|
1580
|
+
const { token } = ownership;
|
|
1581
|
+
if (this.tokenClaimedOrDeciding(token) || this.holdsPair(ownership)) return;
|
|
1582
|
+
await this.state.knownSessions.owners?.release(ownership).catch((error: Error) => this.report(error));
|
|
1583
|
+
},
|
|
1584
|
+
() => {},
|
|
1585
|
+
)
|
|
1586
|
+
.finally(() => {
|
|
1587
|
+
if (this.unansweredClaims.get(claim) === use) this.unansweredClaims.delete(claim);
|
|
1588
|
+
});
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
/** #5607: the registration of the unanswered `claim` uses its id, or does not (`claimLanding`). */
|
|
1592
|
+
private claimDecided(claim: string, use: "kept" | "unused"): void {
|
|
1593
|
+
const pending = this.unansweredClaims.get(claim);
|
|
1594
|
+
if (pending === undefined) return;
|
|
1595
|
+
this.unansweredClaims.delete(claim);
|
|
1596
|
+
pending.resolve(use);
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/**
|
|
1600
|
+
* #5607: the binding write of a registration and what it decides for the session: false when it takes nothing more
|
|
1601
|
+
* (its id is another session's across nodes, or another live registration of it owns it).
|
|
1602
|
+
*/
|
|
1603
|
+
private async bindRegistration(
|
|
1604
|
+
session: BrokerConnectedSession,
|
|
1605
|
+
entry: PresenceEntry,
|
|
1606
|
+
owner: KnownSessionOwner,
|
|
1607
|
+
reclaim: SessionIdReclaimRequest | undefined,
|
|
1608
|
+
grant: SessionIdGrant | undefined,
|
|
1609
|
+
): Promise<boolean> {
|
|
1610
|
+
// An id bound to another session's return address is not this session's across nodes (see `claimSessionId`).
|
|
1611
|
+
const confirmed = clockMark();
|
|
1612
|
+
let binding: KnownSessionBindResult;
|
|
1613
|
+
if (reclaim !== undefined) this.decidingClaims.set(reclaim.claim, reclaim.token);
|
|
1614
|
+
try {
|
|
1615
|
+
binding =
|
|
1616
|
+
reclaim === undefined
|
|
1617
|
+
? await this.state.knownSessions.bind(
|
|
1618
|
+
session.info.id,
|
|
1619
|
+
session.registrationReturnAddress ?? "",
|
|
1620
|
+
KNOWN_SESSION_RETENTION_MS,
|
|
1621
|
+
undefined,
|
|
1622
|
+
owner,
|
|
1623
|
+
)
|
|
1624
|
+
: await this.reclaimOrBind(session, reclaim, owner);
|
|
1625
|
+
} catch (error) {
|
|
1626
|
+
// #5607: the id this session was given (and answered under) is not confirmed its own: it takes it at the
|
|
1627
|
+
// next check, as a session registered while routing was opening does.
|
|
1628
|
+
if (this.state.knownSessions.owners !== undefined && this.presence.get(session) === entry) {
|
|
1629
|
+
entry.adoption = adoption(owner.token);
|
|
1630
|
+
if (grant?.alias !== undefined)
|
|
1631
|
+
entry.pendingAlias = {
|
|
1632
|
+
alias: grant.alias,
|
|
1633
|
+
...(grant.aliasToken === undefined ? {} : { aliasToken: grant.aliasToken }),
|
|
1634
|
+
...(grant.recovery === undefined ? {} : { recovery: grant.recovery }),
|
|
1635
|
+
};
|
|
1636
|
+
// A lapsed hand-over whose id its binding write did not keep leaves it awaiting that id all the same.
|
|
1637
|
+
if (reclaim?.recovery !== undefined && session.info.id !== reclaim.sessionId)
|
|
1638
|
+
entry.awaits = { id: reclaim.sessionId, token: randomUUID(), recovery: reclaim.recovery };
|
|
1639
|
+
}
|
|
1640
|
+
throw error;
|
|
1641
|
+
} finally {
|
|
1642
|
+
if (reclaim !== undefined) this.decidingClaims.delete(reclaim.claim);
|
|
1643
|
+
}
|
|
1644
|
+
if (binding.status === "refused") {
|
|
1645
|
+
entry.unbound = true;
|
|
1646
|
+
return false;
|
|
1647
|
+
}
|
|
1648
|
+
// #5607: another live registration of the same session owns the id (it took it once this registration's
|
|
1649
|
+
// went unrenewed): that registration keeps it, and this one reconnects for a fresh id.
|
|
1650
|
+
if (binding.status === "held") {
|
|
1651
|
+
this.lostSessionId(session, entry);
|
|
1652
|
+
return false;
|
|
1653
|
+
}
|
|
1654
|
+
if (binding.status === "bound" && this.state.knownSessions.owners !== undefined) {
|
|
1655
|
+
entry.owner = {
|
|
1656
|
+
intercomSessionId: session.info.id,
|
|
1657
|
+
token: owner.token,
|
|
1658
|
+
confirmed: laterMark(entry.owner?.confirmed, confirmed),
|
|
1659
|
+
};
|
|
1660
|
+
// #5607: the id it had while it awaited this one stays an alias of it, owned the same way.
|
|
1661
|
+
const alias = grant?.alias ?? reclaim?.alias;
|
|
1662
|
+
const aliasToken = grant?.alias === undefined ? reclaim?.aliasToken : grant.aliasToken;
|
|
1663
|
+
if (alias !== undefined && (reclaim === undefined || session.info.id === reclaim.sessionId)) {
|
|
1664
|
+
const recovery = grant?.alias === undefined ? reclaim?.recovery : grant.recovery;
|
|
1665
|
+
entry.pendingAlias = {
|
|
1666
|
+
alias,
|
|
1667
|
+
...(aliasToken === undefined ? {} : { aliasToken }),
|
|
1668
|
+
...(recovery === undefined ? {} : { recovery }),
|
|
1669
|
+
};
|
|
1670
|
+
await this.takePendingAlias(session, entry);
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
// #5607: a hand-over whose id its binding write did not keep gives its alias back too, unless a lapsed
|
|
1674
|
+
// hand-over still keeps it: the session then awaits the id with it, as one whose claim was held does.
|
|
1675
|
+
if (reclaim !== undefined && session.info.id !== reclaim.sessionId) {
|
|
1676
|
+
if (reclaim.recovery !== undefined && this.state.knownSessions.owners !== undefined)
|
|
1677
|
+
entry.awaits = { id: reclaim.sessionId, token: randomUUID(), recovery: reclaim.recovery };
|
|
1678
|
+
else if (reclaim.alias !== undefined && reclaim.aliasToken !== undefined)
|
|
1679
|
+
await this.releaseOwner({ intercomSessionId: reclaim.alias, token: reclaim.aliasToken });
|
|
1680
|
+
}
|
|
1681
|
+
return true;
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1461
1684
|
/**
|
|
1462
1685
|
* #5607: the binding write of a registration whose claim of its preferred id went unanswered, in the slot today's
|
|
1463
1686
|
* fresh-id binding takes. One write keeps the preferred id when the binding is still the one that claim wrote, no
|
|
@@ -1474,8 +1697,14 @@ export class MultiNodeRouter {
|
|
|
1474
1697
|
const { knownSessions } = this.state;
|
|
1475
1698
|
const returnAddress = session.registrationReturnAddress ?? "";
|
|
1476
1699
|
const freshId = session.info.id;
|
|
1700
|
+
// #5607: the claim's id is not this registration's: its token is no longer deciding it (`claimLanding`).
|
|
1701
|
+
const unused = () => {
|
|
1702
|
+
this.decidingClaims.delete(reclaim.claim);
|
|
1703
|
+
this.claimDecided(reclaim.claim, "unused");
|
|
1704
|
+
};
|
|
1477
1705
|
const bindFresh = async () => {
|
|
1478
1706
|
reclaim.settle(false);
|
|
1707
|
+
unused();
|
|
1479
1708
|
// A grant its claim's own write made, which this registration does not use, is given back. A hand-over's hold
|
|
1480
1709
|
// that loses its id this way lapses at the next renewal, keeping its alias (`renewOwners`).
|
|
1481
1710
|
await this.releaseOwner({ intercomSessionId: reclaim.sessionId, token: owner.token });
|
|
@@ -1496,13 +1725,18 @@ export class MultiNodeRouter {
|
|
|
1496
1725
|
});
|
|
1497
1726
|
} catch (error) {
|
|
1498
1727
|
reclaim.settle(false);
|
|
1728
|
+
unused();
|
|
1499
1729
|
throw error;
|
|
1500
1730
|
}
|
|
1501
1731
|
if (reclaimed.status === "fresh") {
|
|
1502
1732
|
reclaim.settle(false);
|
|
1733
|
+
unused();
|
|
1503
1734
|
return reclaimed.binding;
|
|
1504
1735
|
}
|
|
1505
|
-
if (reclaimed.status === "kept" && reclaim.settle(true))
|
|
1736
|
+
if (reclaimed.status === "kept" && reclaim.settle(true)) {
|
|
1737
|
+
this.claimDecided(reclaim.claim, "kept");
|
|
1738
|
+
return { status: "bound" };
|
|
1739
|
+
}
|
|
1506
1740
|
return bindFresh();
|
|
1507
1741
|
}
|
|
1508
1742
|
|
|
@@ -1947,7 +2181,10 @@ export class MultiNodeRouter {
|
|
|
1947
2181
|
* that presents the hand-over, and the move back of a session that awaits the id with its `recovery` carries it).
|
|
1948
2182
|
*/
|
|
1949
2183
|
private lapse(identity: string, reserved: ReservedId): void {
|
|
1950
|
-
|
|
2184
|
+
// The id only: the token may own the fresh id of a registration that did not keep this one.
|
|
2185
|
+
const held = { intercomSessionId: reserved.id, token: reserved.token };
|
|
2186
|
+
if (!this.tokenClaimedOrDeciding(held.token) && !this.holdsPair(held))
|
|
2187
|
+
void this.releaseOwner(held);
|
|
1951
2188
|
const earlier = this.lapsedHandoffs.get(identity);
|
|
1952
2189
|
if (reserved.alias === reserved.id) {
|
|
1953
2190
|
if (reserved.aliasToken !== undefined && !this.tokenInUse(reserved.aliasToken))
|
|
@@ -2010,13 +2247,20 @@ export class MultiNodeRouter {
|
|
|
2010
2247
|
}
|
|
2011
2248
|
}
|
|
2012
2249
|
|
|
2250
|
+
/** #5607: whether a claim here is taking a hold with `token`, or a registration's binding write deciding with it. */
|
|
2251
|
+
private tokenClaimedOrDeciding(token: string): boolean {
|
|
2252
|
+
for (const claimed of this.consumingClaims.values()) if (claimed === token) return true;
|
|
2253
|
+
for (const deciding of this.decidingClaims.values()) if (deciding === token) return true;
|
|
2254
|
+
return false;
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2013
2257
|
/**
|
|
2014
2258
|
* #5607: whether an owner token still serves a registration here: one held for a session that awaited its id, one a
|
|
2015
2259
|
* registration that comes back is taking, or one a live registration owns its id or an alias with. A late answer of an
|
|
2016
2260
|
* earlier write never gives such a token's id back.
|
|
2017
2261
|
*/
|
|
2018
2262
|
private tokenInUse(token: string): boolean {
|
|
2019
|
-
if (this.
|
|
2263
|
+
if (this.tokenClaimedOrDeciding(token)) return true;
|
|
2020
2264
|
for (const reserved of this.reservedIds.values())
|
|
2021
2265
|
if (reserved.token === token || reserved.aliasToken === token) return true;
|
|
2022
2266
|
for (const lapsed of this.lapsedHandoffs.values()) if (lapsed.aliasToken === token) return true;
|
|
@@ -2031,6 +2275,27 @@ export class MultiNodeRouter {
|
|
|
2031
2275
|
return false;
|
|
2032
2276
|
}
|
|
2033
2277
|
|
|
2278
|
+
/**
|
|
2279
|
+
* #5607: whether something here holds `intercomSessionId` with `token`: a hold or a claim taking one, a lapsed
|
|
2280
|
+
* hand-over's alias, or a registration that owns it, holds it as an alias, is taking it as one or awaits it. Unlike
|
|
2281
|
+
* `tokenInUse`, the token holding another id does not count.
|
|
2282
|
+
*/
|
|
2283
|
+
private holdsPair({ intercomSessionId, token }: KnownSessionOwnership): boolean {
|
|
2284
|
+
const same = (id: string | undefined, held: string | undefined) => id === intercomSessionId && held === token;
|
|
2285
|
+
for (const reserved of [...this.reservedIds.values(), ...this.claimingHandoffs.values()])
|
|
2286
|
+
if (same(reserved.id, reserved.token) || same(reserved.alias, reserved.aliasToken)) return true;
|
|
2287
|
+
for (const lapsed of this.lapsedHandoffs.values()) if (same(lapsed.alias, lapsed.aliasToken)) return true;
|
|
2288
|
+
for (const entry of this.presence.values())
|
|
2289
|
+
if (
|
|
2290
|
+
same(entry.owner?.intercomSessionId, entry.owner?.token) ||
|
|
2291
|
+
same(entry.alias?.intercomSessionId, entry.alias?.token) ||
|
|
2292
|
+
same(entry.awaits?.id, entry.awaits?.token) ||
|
|
2293
|
+
same(entry.pendingAlias?.alias, entry.pendingAlias?.aliasToken ?? entry.owner?.token)
|
|
2294
|
+
)
|
|
2295
|
+
return true;
|
|
2296
|
+
return false;
|
|
2297
|
+
}
|
|
2298
|
+
|
|
2034
2299
|
/** #5607: `session` takes `alias` too, in one conditional write; an alias it cannot take is left alone. */
|
|
2035
2300
|
private async takeAlias(
|
|
2036
2301
|
session: BrokerConnectedSession,
|
|
@@ -2223,7 +2488,7 @@ export class MultiNodeRouter {
|
|
|
2223
2488
|
boundMs: number = PRESENCE_REGISTRATION_BOUND_MS,
|
|
2224
2489
|
): Promise<void> {
|
|
2225
2490
|
if (this.closed || this.presence.has(session)) return Promise.resolve();
|
|
2226
|
-
const entry: PresenceEntry = {};
|
|
2491
|
+
const entry: PresenceEntry = { registeredSeq: ++this.registrationSeq };
|
|
2227
2492
|
const presented = grant?.recovery ?? reclaim?.recovery ?? recovery;
|
|
2228
2493
|
if (presented !== undefined) entry.presented = presented;
|
|
2229
2494
|
// The registration of a released subagent its owner authorized has the asks parked for it moved to it (`rebindParked`).
|
|
@@ -2247,100 +2512,68 @@ export class MultiNodeRouter {
|
|
|
2247
2512
|
entry.adoption = adoption(owner.token);
|
|
2248
2513
|
this.presence.set(session, entry);
|
|
2249
2514
|
const identity = deliveryTargetIdentity(session);
|
|
2515
|
+
// #5607: an unanswered claim's binding write decides the session's id as the registration begins, not behind the
|
|
2516
|
+
// departure of the session's earlier connection here, which may outlast the presence wait: what it decides from
|
|
2517
|
+
// (its claim's own write, the presence of the session on other brokers, the id's owner) is none of that departure's
|
|
2518
|
+
// to change. What the registration records of the session, and its presence, still follow the departure (below).
|
|
2519
|
+
const decided =
|
|
2520
|
+
reclaim === undefined || entry.adoption !== undefined
|
|
2521
|
+
? undefined
|
|
2522
|
+
: this.bindRegistration(session, entry, owner, reclaim, grant);
|
|
2523
|
+
decided?.catch(() => {});
|
|
2250
2524
|
// #4488: read as the registration begins, so a reservation a release of the child makes from here on is newer.
|
|
2251
2525
|
const insertedBy = consumed?.childName === undefined ? undefined : this.state.reservations.insertionMark();
|
|
2252
2526
|
insertedBy?.catch(() => {});
|
|
2527
|
+
let resolveInitialScan: () => void = () => {};
|
|
2528
|
+
const initialScanPromise = new Promise<void>((res) => {
|
|
2529
|
+
resolveInitialScan = res;
|
|
2530
|
+
});
|
|
2531
|
+
const initialScanEntry = { promise: initialScanPromise, resolve: resolveInitialScan };
|
|
2532
|
+
this.initialScans.get(identity)?.resolve();
|
|
2533
|
+
this.initialScans.set(identity, initialScanEntry);
|
|
2534
|
+
const startedMono = performance.now();
|
|
2253
2535
|
return new Promise<void>((resolve) => {
|
|
2254
|
-
|
|
2536
|
+
let resolved = false;
|
|
2537
|
+
const finish = (): void => {
|
|
2538
|
+
if (resolved) return;
|
|
2539
|
+
resolved = true;
|
|
2540
|
+
clearTimeout(bound);
|
|
2541
|
+
resolve();
|
|
2542
|
+
};
|
|
2543
|
+
// If `boundMs` already elapsed monotonically during an earlier step (e.g., synchronous SQLite I/O or an
|
|
2544
|
+
// event-loop stall), resolve and yield once before starting the next store write so `registered` flushes to
|
|
2545
|
+
// the client before background persistence continues. Zero overhead while still within `boundMs`.
|
|
2546
|
+
const yieldIfOverdue = async (): Promise<void> => {
|
|
2547
|
+
if (resolved || performance.now() - startedMono < boundMs) return;
|
|
2548
|
+
finish();
|
|
2549
|
+
await new Promise<void>((resume) => setImmediate(resume));
|
|
2550
|
+
};
|
|
2551
|
+
const bound = setTimeout(finish, boundMs);
|
|
2255
2552
|
bound.unref?.();
|
|
2256
2553
|
this.serial(identity, async () => {
|
|
2554
|
+
let shouldScan = false;
|
|
2257
2555
|
try {
|
|
2258
2556
|
// #5607: a session registered while routing was opening takes its id now, or reconnects for a fresh one. A
|
|
2259
2557
|
// write that fails leaves it to take the id at the next check (`renewOwners`).
|
|
2260
|
-
if (entry.adoption !== undefined) {
|
|
2558
|
+
if (decided === undefined && entry.adoption !== undefined) {
|
|
2261
2559
|
// Decided by whichever write answers first with the id taken or lost; the first write failing leaves it to
|
|
2262
2560
|
// the next renewal.
|
|
2263
2561
|
const pending = entry.adoption;
|
|
2264
2562
|
const first = this.adoptionCheck(session, entry).answered();
|
|
2265
2563
|
if ((await Promise.race([pending.decided.promise, first])) !== "own") return;
|
|
2266
2564
|
} else {
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
if (reclaim !== undefined) this.decidingTokens.add(reclaim.token);
|
|
2271
|
-
try {
|
|
2272
|
-
binding =
|
|
2273
|
-
reclaim === undefined
|
|
2274
|
-
? await this.state.knownSessions.bind(
|
|
2275
|
-
session.info.id,
|
|
2276
|
-
session.registrationReturnAddress ?? "",
|
|
2277
|
-
KNOWN_SESSION_RETENTION_MS,
|
|
2278
|
-
undefined,
|
|
2279
|
-
owner,
|
|
2280
|
-
)
|
|
2281
|
-
: await this.reclaimOrBind(session, reclaim, owner);
|
|
2282
|
-
} catch (error) {
|
|
2283
|
-
// #5607: the id this session was given (and answered under) is not confirmed its own: it takes it at the
|
|
2284
|
-
// next check, as a session registered while routing was opening does.
|
|
2285
|
-
if (this.state.knownSessions.owners !== undefined && this.presence.get(session) === entry) {
|
|
2286
|
-
entry.adoption = adoption(owner.token);
|
|
2287
|
-
if (grant?.alias !== undefined)
|
|
2288
|
-
entry.pendingAlias = {
|
|
2289
|
-
alias: grant.alias,
|
|
2290
|
-
...(grant.aliasToken === undefined ? {} : { aliasToken: grant.aliasToken }),
|
|
2291
|
-
...(grant.recovery === undefined ? {} : { recovery: grant.recovery }),
|
|
2292
|
-
};
|
|
2293
|
-
// A lapsed hand-over whose id its binding write did not keep leaves it awaiting that id all the same.
|
|
2294
|
-
if (reclaim?.recovery !== undefined && session.info.id !== reclaim.sessionId)
|
|
2295
|
-
entry.awaits = { id: reclaim.sessionId, token: randomUUID(), recovery: reclaim.recovery };
|
|
2296
|
-
}
|
|
2297
|
-
throw error;
|
|
2298
|
-
} finally {
|
|
2299
|
-
if (reclaim !== undefined) this.decidingTokens.delete(reclaim.token);
|
|
2300
|
-
}
|
|
2301
|
-
if (binding.status === "refused") {
|
|
2302
|
-
entry.unbound = true;
|
|
2303
|
-
return;
|
|
2304
|
-
}
|
|
2305
|
-
// #5607: another live registration of the same session owns the id (it took it once this registration's
|
|
2306
|
-
// went unrenewed): that registration keeps it, and this one reconnects for a fresh id.
|
|
2307
|
-
if (binding.status === "held") {
|
|
2308
|
-
this.lostSessionId(session, entry);
|
|
2309
|
-
return;
|
|
2310
|
-
}
|
|
2311
|
-
if (binding.status === "bound" && this.state.knownSessions.owners !== undefined) {
|
|
2312
|
-
entry.owner = {
|
|
2313
|
-
intercomSessionId: session.info.id,
|
|
2314
|
-
token: owner.token,
|
|
2315
|
-
confirmed: laterMark(entry.owner?.confirmed, confirmed),
|
|
2316
|
-
};
|
|
2317
|
-
// #5607: the id it had while it awaited this one stays an alias of it, owned the same way.
|
|
2318
|
-
const alias = grant?.alias ?? reclaim?.alias;
|
|
2319
|
-
const aliasToken = grant?.alias === undefined ? reclaim?.aliasToken : grant.aliasToken;
|
|
2320
|
-
if (alias !== undefined && (reclaim === undefined || session.info.id === reclaim.sessionId)) {
|
|
2321
|
-
const recovery = grant?.alias === undefined ? reclaim?.recovery : grant.recovery;
|
|
2322
|
-
entry.pendingAlias = {
|
|
2323
|
-
alias,
|
|
2324
|
-
...(aliasToken === undefined ? {} : { aliasToken }),
|
|
2325
|
-
...(recovery === undefined ? {} : { recovery }),
|
|
2326
|
-
};
|
|
2327
|
-
await this.takePendingAlias(session, entry);
|
|
2328
|
-
}
|
|
2329
|
-
}
|
|
2330
|
-
// #5607: a hand-over whose id its binding write did not keep gives its alias back too, unless a lapsed
|
|
2331
|
-
// hand-over still keeps it: the session then awaits the id with it, as one whose claim was held does.
|
|
2332
|
-
if (reclaim !== undefined && session.info.id !== reclaim.sessionId) {
|
|
2333
|
-
if (reclaim.recovery !== undefined && this.state.knownSessions.owners !== undefined)
|
|
2334
|
-
entry.awaits = { id: reclaim.sessionId, token: randomUUID(), recovery: reclaim.recovery };
|
|
2335
|
-
else if (reclaim.alias !== undefined && reclaim.aliasToken !== undefined)
|
|
2336
|
-
await this.releaseOwner({ intercomSessionId: reclaim.alias, token: reclaim.aliasToken });
|
|
2337
|
-
}
|
|
2565
|
+
if (!(await (decided ?? this.bindRegistration(session, entry, owner, reclaim, grant)))) return;
|
|
2566
|
+
// A registration whose id was decided early, and that lost it while it waited here, takes nothing more.
|
|
2567
|
+
if (decided !== undefined && entry.fenced === true) return;
|
|
2338
2568
|
}
|
|
2569
|
+
await yieldIfOverdue();
|
|
2339
2570
|
await this.recordKnown(session, entry);
|
|
2571
|
+
await yieldIfOverdue();
|
|
2340
2572
|
for (let attempt = 0; ; attempt++) {
|
|
2341
2573
|
await this.acquirePresence(session, entry);
|
|
2342
2574
|
if (entry.lease !== undefined || attempt >= PRESENCE_HANDOVER_ATTEMPTS || this.presence.get(session) !== entry)
|
|
2343
2575
|
break;
|
|
2576
|
+
await yieldIfOverdue();
|
|
2344
2577
|
await new Promise<void>((wait) => setTimeout(wait, PRESENCE_HANDOVER_RETRY_MS * 2 ** attempt).unref());
|
|
2345
2578
|
}
|
|
2346
2579
|
if (consumed !== undefined) {
|
|
@@ -2355,13 +2588,22 @@ export class MultiNodeRouter {
|
|
|
2355
2588
|
rebind: registration,
|
|
2356
2589
|
attempts: 0,
|
|
2357
2590
|
};
|
|
2591
|
+
await yieldIfOverdue();
|
|
2358
2592
|
await this.finishRegistration(session, entry);
|
|
2359
2593
|
}
|
|
2594
|
+
shouldScan = true;
|
|
2360
2595
|
} finally {
|
|
2361
|
-
|
|
2362
|
-
|
|
2596
|
+
finish();
|
|
2597
|
+
const finishInitial = () => {
|
|
2598
|
+
if (this.initialScans.get(identity) === initialScanEntry) this.initialScans.delete(identity);
|
|
2599
|
+
resolveInitialScan();
|
|
2600
|
+
};
|
|
2601
|
+
if (shouldScan && !this.closed) {
|
|
2602
|
+
void this.scanIdentity(identity).finally(finishInitial);
|
|
2603
|
+
} else {
|
|
2604
|
+
finishInitial();
|
|
2605
|
+
}
|
|
2363
2606
|
}
|
|
2364
|
-
this.scanIdentity(identity);
|
|
2365
2607
|
});
|
|
2366
2608
|
});
|
|
2367
2609
|
}
|
|
@@ -2761,8 +3003,21 @@ export class MultiNodeRouter {
|
|
|
2761
3003
|
// Its id names another session across nodes: that session's asks and records are not this one's to end.
|
|
2762
3004
|
const unbound = entry.unbound === true;
|
|
2763
3005
|
const identity = deliveryTargetIdentity(session);
|
|
3006
|
+
if (![...this.presence.keys()].some((other) => deliveryTargetIdentity(other) === identity)) {
|
|
3007
|
+
const initial = this.initialScans.get(identity);
|
|
3008
|
+
if (initial !== undefined) {
|
|
3009
|
+
this.initialScans.delete(identity);
|
|
3010
|
+
initial.resolve();
|
|
3011
|
+
}
|
|
3012
|
+
}
|
|
2764
3013
|
const claims = [...this.inflight.values()].filter((inflight) => inflight.session === session);
|
|
2765
|
-
for (const inflight of claims)
|
|
3014
|
+
for (const inflight of claims) {
|
|
3015
|
+
this.inflight.delete(inflight.row.messageId);
|
|
3016
|
+
inflight.finishOpening?.();
|
|
3017
|
+
}
|
|
3018
|
+
for (const [release, owner] of [...this.activeOpenings]) {
|
|
3019
|
+
if (owner === session) release();
|
|
3020
|
+
}
|
|
2766
3021
|
for (const waiter of [...this.waiters.values()]) {
|
|
2767
3022
|
// #4642: a hosted session shares its host's socket; only its own sends end with it.
|
|
2768
3023
|
const sentBySession =
|
|
@@ -3327,23 +3582,531 @@ export class MultiNodeRouter {
|
|
|
3327
3582
|
|
|
3328
3583
|
/** Answer a send no local session resolved: another node's session, a shared hold, an offline session, or none. */
|
|
3329
3584
|
routeSend(request: RemoteSendRequest): void {
|
|
3585
|
+
if (this.activeLocalCommit !== undefined && this.activeLocalCommit.clientMessage === request.clientMessage) {
|
|
3586
|
+
this.activeLocalCommit.reentrant = request;
|
|
3587
|
+
return;
|
|
3588
|
+
}
|
|
3589
|
+
const startedAt = performance.now();
|
|
3590
|
+
const senderKey = deliveryTargetIdentity(request.from);
|
|
3591
|
+
let pendingLocalTargetIdentities: Set<string> | undefined;
|
|
3592
|
+
if (
|
|
3593
|
+
request.localTarget === undefined &&
|
|
3594
|
+
!request.requirePendingReply &&
|
|
3595
|
+
request.returnPathArrivalSeq !== undefined &&
|
|
3596
|
+
mayUseReturnPath(request.message, request.supervisorSend, request.from)
|
|
3597
|
+
) {
|
|
3598
|
+
const senders = this.host.returnPaths?.pendingSendersForTarget(
|
|
3599
|
+
request.from,
|
|
3600
|
+
request.target,
|
|
3601
|
+
request.returnPathArrivalSeq,
|
|
3602
|
+
);
|
|
3603
|
+
if (senders !== undefined) {
|
|
3604
|
+
for (const identity of senders) {
|
|
3605
|
+
if (this.localSessionFor(identity) !== undefined) {
|
|
3606
|
+
pendingLocalTargetIdentities ??= new Set();
|
|
3607
|
+
pendingLocalTargetIdentities.add(identity);
|
|
3608
|
+
}
|
|
3609
|
+
}
|
|
3610
|
+
}
|
|
3611
|
+
}
|
|
3612
|
+
const ticket = this.sendOrder.begin(
|
|
3613
|
+
request.localTarget === undefined
|
|
3614
|
+
? {
|
|
3615
|
+
senderKey,
|
|
3616
|
+
rawTarget: request.target,
|
|
3617
|
+
arrivedRegSeq: this.registrationSeq,
|
|
3618
|
+
isLocal: false,
|
|
3619
|
+
...(pendingLocalTargetIdentities === undefined ? {} : { pendingLocalTargetIdentities }),
|
|
3620
|
+
}
|
|
3621
|
+
: {
|
|
3622
|
+
senderKey,
|
|
3623
|
+
rawTarget: request.target,
|
|
3624
|
+
arrivedRegSeq: this.registrationSeq,
|
|
3625
|
+
isLocal: true,
|
|
3626
|
+
targetIdentity: deliveryTargetIdentity(request.localTarget),
|
|
3627
|
+
},
|
|
3628
|
+
);
|
|
3629
|
+
const existingProgress = this.sendProgress.get(request.clientMessage);
|
|
3630
|
+
const progress: SendProgress = {
|
|
3631
|
+
startedAt: existingProgress?.startedAt ?? startedAt,
|
|
3632
|
+
clientDeadlineAt: existingProgress?.clientDeadlineAt ?? startedAt + SEND_RESPONSE_TIMEOUT_MS,
|
|
3633
|
+
hadProgress: false,
|
|
3634
|
+
lastProgressAt: startedAt,
|
|
3635
|
+
reportingEnabled: request.localTarget === undefined,
|
|
3636
|
+
socket: request.socket,
|
|
3637
|
+
messageId: request.message.id,
|
|
3638
|
+
...(request.attemptId === undefined ? {} : { attemptId: request.attemptId }),
|
|
3639
|
+
ticket,
|
|
3640
|
+
};
|
|
3641
|
+
this.sendProgress.set(request.clientMessage, progress);
|
|
3642
|
+
this.ticketProgress.set(ticket, progress);
|
|
3330
3643
|
// #4386: one sender's replies are routed in the order it sent them, so they reach an owner in that order.
|
|
3331
3644
|
// #4992: a departing replier waits for this queue, so its asks fail only after the replies it sent are queued.
|
|
3332
|
-
if (isReleasedReplyCandidate(request))
|
|
3333
|
-
|
|
3645
|
+
if (isReleasedReplyCandidate(request)) {
|
|
3646
|
+
const replyKey = `reply:${request.from.info.id}`;
|
|
3647
|
+
let lane = this.replyLanes.get(replyKey);
|
|
3648
|
+
if (lane === undefined) {
|
|
3649
|
+
lane = { queued: 0 };
|
|
3650
|
+
this.replyLanes.set(replyKey, lane);
|
|
3651
|
+
}
|
|
3652
|
+
lane.queued += 1;
|
|
3653
|
+
if (request.localTarget !== undefined) this.sendOrder.markUnbounded(ticket);
|
|
3654
|
+
const queuedBehind = lane.queued > 1;
|
|
3655
|
+
if (queuedBehind) {
|
|
3656
|
+
progress.waitingOnReplyKey = replyKey;
|
|
3657
|
+
progress.reportingEnabled = true;
|
|
3658
|
+
this.trackQueuedProgress(senderKey, progress);
|
|
3659
|
+
this.syncProgressFromBlocker(progress, this.resolveBlockerProgress(progress, ticket));
|
|
3660
|
+
this.scheduleProgressHold(progress, startedAt);
|
|
3661
|
+
}
|
|
3662
|
+
this.serial(replyKey, async () => {
|
|
3663
|
+
if (queuedBehind) this.untrackQueuedProgress(senderKey, progress);
|
|
3664
|
+
this.syncProgressFromBlocker(progress, lane.lastProgress);
|
|
3665
|
+
progress.waitingOnReplyKey = undefined;
|
|
3666
|
+
if (request.localTarget !== undefined) {
|
|
3667
|
+
this.stopProgress(progress);
|
|
3668
|
+
} else if (progress.hadProgress) {
|
|
3669
|
+
this.scheduleProgressHold(progress);
|
|
3670
|
+
}
|
|
3671
|
+
lane.active = ticket;
|
|
3672
|
+
lane.lastProgress = progress;
|
|
3673
|
+
try {
|
|
3674
|
+
await this.routeSendSafely(request, ticket, true);
|
|
3675
|
+
} finally {
|
|
3676
|
+
if (lane.active === ticket) lane.active = undefined;
|
|
3677
|
+
lane.queued -= 1;
|
|
3678
|
+
if (lane.queued <= 0 && this.replyLanes.get(replyKey) === lane) this.replyLanes.delete(replyKey);
|
|
3679
|
+
}
|
|
3680
|
+
});
|
|
3681
|
+
} else {
|
|
3682
|
+
void this.routeSendSafely(request, ticket, true);
|
|
3683
|
+
}
|
|
3334
3684
|
}
|
|
3335
3685
|
|
|
3336
|
-
private async routeSendSafely(request: RemoteSendRequest): Promise<void> {
|
|
3686
|
+
private async routeSendSafely(request: RemoteSendRequest, ticket: SendOrderTicket, ownsTicket: boolean): Promise<void> {
|
|
3687
|
+
const startedAt = performance.now();
|
|
3688
|
+
const openingDeadlineAt = this.returnPathOpeningDeadlineAt(request, startedAt);
|
|
3337
3689
|
// #5607: a sender or local recipient whose id may have passed to another registration is checked first.
|
|
3338
3690
|
const owners = this.sendOwnerChecks(request);
|
|
3339
3691
|
try {
|
|
3340
|
-
if (
|
|
3341
|
-
|
|
3692
|
+
if (this.closed) return;
|
|
3693
|
+
if (request.localTarget !== undefined)
|
|
3694
|
+
await this.answerLocalRetry(request, request.localTarget, owners, ticket, openingDeadlineAt);
|
|
3695
|
+
else if (owners === undefined) await this.route(request, ticket, openingDeadlineAt);
|
|
3696
|
+
else if (await this.senderOwns(request, owners)) {
|
|
3697
|
+
this.markRoutingProgress(request);
|
|
3698
|
+
await this.route(request, ticket, openingDeadlineAt);
|
|
3699
|
+
}
|
|
3342
3700
|
} catch (error) {
|
|
3343
3701
|
this.report(error);
|
|
3702
|
+
if (this.closed) return;
|
|
3344
3703
|
// Local delivery does not depend on the shared database (spec §8).
|
|
3345
|
-
if (request.localTarget !== undefined)
|
|
3704
|
+
if (request.localTarget !== undefined) {
|
|
3705
|
+
const remainingMs = Math.max(0, startedAt + LOCAL_RETRY_LOOKUP_BOUND_MS - performance.now());
|
|
3706
|
+
await this.deliverLocally(request, request.localTarget, ticket, remainingMs, openingDeadlineAt);
|
|
3707
|
+
return;
|
|
3708
|
+
}
|
|
3346
3709
|
this.fail(request, SHARED_STORAGE_UNAVAILABLE_REASON, request.requirePendingReply ? "session_not_found" : undefined);
|
|
3710
|
+
} finally {
|
|
3711
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
3712
|
+
if (progress !== undefined) this.stopProgress(progress);
|
|
3713
|
+
if (ownsTicket) this.sendOrder.finish(ticket);
|
|
3714
|
+
}
|
|
3715
|
+
}
|
|
3716
|
+
|
|
3717
|
+
private returnPathOpeningDeadlineAt(request: RemoteSendRequest, startedAt: number): number {
|
|
3718
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
3719
|
+
const clientDeadline = progress?.clientDeadlineAt ?? startedAt + SEND_RESPONSE_TIMEOUT_MS;
|
|
3720
|
+
const capDeadline = (progress?.startedAt ?? startedAt) + RECIPIENT_SAVE_MAX_WAIT_MS;
|
|
3721
|
+
const defaultDeadline = this.host.returnPaths?.openingWaitDeadline() ?? startedAt + RETURN_PATH_OPENING_WAIT_MS;
|
|
3722
|
+
const base = request.returnPathDeadlineAt ?? defaultDeadline;
|
|
3723
|
+
return Math.min(base, clientDeadline, capDeadline);
|
|
3724
|
+
}
|
|
3725
|
+
|
|
3726
|
+
private trackOpening(session: BrokerConnectedSession, finish: (() => void) | undefined): (() => void) | undefined {
|
|
3727
|
+
if (finish === undefined) return undefined;
|
|
3728
|
+
let released = false;
|
|
3729
|
+
const release = (): void => {
|
|
3730
|
+
if (released) return;
|
|
3731
|
+
released = true;
|
|
3732
|
+
this.activeOpenings.delete(release);
|
|
3733
|
+
finish();
|
|
3734
|
+
};
|
|
3735
|
+
this.activeOpenings.set(release, session);
|
|
3736
|
+
return release;
|
|
3737
|
+
}
|
|
3738
|
+
|
|
3739
|
+
private stopProgress(progress: SendProgress): void {
|
|
3740
|
+
progress.reportingEnabled = false;
|
|
3741
|
+
progress.stopReporting?.();
|
|
3742
|
+
}
|
|
3743
|
+
|
|
3744
|
+
private trackQueuedProgress(senderKey: string, progress: SendProgress): void {
|
|
3745
|
+
let set = this.queuedProgress.get(senderKey);
|
|
3746
|
+
if (set === undefined) {
|
|
3747
|
+
set = new Set();
|
|
3748
|
+
this.queuedProgress.set(senderKey, set);
|
|
3749
|
+
}
|
|
3750
|
+
set.add(progress);
|
|
3751
|
+
}
|
|
3752
|
+
|
|
3753
|
+
private untrackQueuedProgress(senderKey: string, progress: SendProgress): void {
|
|
3754
|
+
const set = this.queuedProgress.get(senderKey);
|
|
3755
|
+
if (set === undefined) return;
|
|
3756
|
+
set.delete(progress);
|
|
3757
|
+
if (set.size === 0) this.queuedProgress.delete(senderKey);
|
|
3758
|
+
}
|
|
3759
|
+
|
|
3760
|
+
/**
|
|
3761
|
+
* Event-driven horizon progress reporter: never arms a timer when there is no store progress or while
|
|
3762
|
+
* `desired = min(startedAt + RECIPIENT_SAVE_MAX_WAIT_MS, lastProgressAt + RECIPIENT_RECEIPT_TIMEOUT_MS)` still
|
|
3763
|
+
* fits inside `clientDeadlineAt - MIN_WAIT_CLIENT_MARGIN_MS`. When an actual store answer advances `desired`
|
|
3764
|
+
* past that margin, schedules at most one timer (bounded by `RECIPIENT_SAVING_REPORT_INTERVAL_MS` and
|
|
3765
|
+
* `clientDeadlineAt - MIN_WAIT_CLIENT_MARGIN_MS`) to extend the sender without ever shrinking an existing hold.
|
|
3766
|
+
*/
|
|
3767
|
+
private scheduleProgressHold(progress: SendProgress, now = performance.now()): void {
|
|
3768
|
+
if (!progress.reportingEnabled || !progress.hadProgress || this.closed) return;
|
|
3769
|
+
if (progress.socket.destroyed === true || progress.socket.writableEnded === true) {
|
|
3770
|
+
this.stopProgress(progress);
|
|
3771
|
+
return;
|
|
3772
|
+
}
|
|
3773
|
+
const capAt = progress.startedAt + RECIPIENT_SAVE_MAX_WAIT_MS;
|
|
3774
|
+
if (now >= capAt || now >= progress.clientDeadlineAt) {
|
|
3775
|
+
this.stopProgress(progress);
|
|
3776
|
+
return;
|
|
3777
|
+
}
|
|
3778
|
+
const desired = Math.min(capAt, progress.lastProgressAt + RECIPIENT_RECEIPT_TIMEOUT_MS);
|
|
3779
|
+
if (now >= desired || desired + MIN_WAIT_CLIENT_MARGIN_MS <= progress.clientDeadlineAt) return;
|
|
3780
|
+
const guardAt = progress.clientDeadlineAt - MIN_WAIT_CLIENT_MARGIN_MS;
|
|
3781
|
+
if (now >= guardAt) {
|
|
3782
|
+
const timeoutMs = Math.min(RECIPIENT_RECEIPT_TIMEOUT_MS, desired - now);
|
|
3783
|
+
if (timeoutMs > 0) {
|
|
3784
|
+
progress.stopReporting?.();
|
|
3785
|
+
this.sendHoldFrame(progress.socket, progress.messageId, progress.attemptId, timeoutMs, progress);
|
|
3786
|
+
}
|
|
3787
|
+
return;
|
|
3788
|
+
}
|
|
3789
|
+
const delayMs = Math.min(RECIPIENT_SAVING_REPORT_INTERVAL_MS, guardAt - now);
|
|
3790
|
+
const fireAt = now + delayMs;
|
|
3791
|
+
if (progress.scheduledFireAt !== undefined && progress.scheduledFireAt <= fireAt) return;
|
|
3792
|
+
progress.stopReporting?.();
|
|
3793
|
+
let active = true;
|
|
3794
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
3795
|
+
const stop = (): void => {
|
|
3796
|
+
active = false;
|
|
3797
|
+
if (timer !== undefined) {
|
|
3798
|
+
clearTimeout(timer);
|
|
3799
|
+
timer = undefined;
|
|
3800
|
+
}
|
|
3801
|
+
if (progress.stopReporting === stop) {
|
|
3802
|
+
progress.stopReporting = undefined;
|
|
3803
|
+
progress.scheduledFireAt = undefined;
|
|
3804
|
+
}
|
|
3805
|
+
this.progressStops.delete(stop);
|
|
3806
|
+
};
|
|
3807
|
+
progress.stopReporting = stop;
|
|
3808
|
+
progress.scheduledFireAt = fireAt;
|
|
3809
|
+
this.progressStops.add(stop);
|
|
3810
|
+
timer = setTimeout(() => {
|
|
3811
|
+
if (!active || !progress.reportingEnabled || this.closed) {
|
|
3812
|
+
stop();
|
|
3813
|
+
return;
|
|
3814
|
+
}
|
|
3815
|
+
stop();
|
|
3816
|
+
if (progress.socket.destroyed === true || progress.socket.writableEnded === true) {
|
|
3817
|
+
progress.reportingEnabled = false;
|
|
3818
|
+
return;
|
|
3819
|
+
}
|
|
3820
|
+
const tickNow = performance.now();
|
|
3821
|
+
const tickCapAt = progress.startedAt + RECIPIENT_SAVE_MAX_WAIT_MS;
|
|
3822
|
+
if (tickNow >= tickCapAt || tickNow >= progress.clientDeadlineAt) {
|
|
3823
|
+
progress.reportingEnabled = false;
|
|
3824
|
+
return;
|
|
3825
|
+
}
|
|
3826
|
+
const tickDesired = Math.min(tickCapAt, progress.lastProgressAt + RECIPIENT_RECEIPT_TIMEOUT_MS);
|
|
3827
|
+
if (tickNow >= tickDesired || tickDesired + MIN_WAIT_CLIENT_MARGIN_MS <= progress.clientDeadlineAt) return;
|
|
3828
|
+
const timeoutMs = Math.min(RECIPIENT_RECEIPT_TIMEOUT_MS, tickDesired - tickNow);
|
|
3829
|
+
if (timeoutMs > 0) {
|
|
3830
|
+
this.sendHoldFrame(progress.socket, progress.messageId, progress.attemptId, timeoutMs, progress);
|
|
3831
|
+
}
|
|
3832
|
+
}, delayMs);
|
|
3833
|
+
timer.unref?.();
|
|
3834
|
+
}
|
|
3835
|
+
|
|
3836
|
+
private resolveBlockerProgress(progress: SendProgress, ticket?: SendOrderTicket): SendProgress | undefined {
|
|
3837
|
+
let curProgress: SendProgress | undefined = progress;
|
|
3838
|
+
let curTicket: SendOrderTicket | undefined = ticket;
|
|
3839
|
+
let blocker: SendProgress | undefined;
|
|
3840
|
+
for (let hops = 0; hops < 64 && curProgress !== undefined; hops++) {
|
|
3841
|
+
let nextTicket: SendOrderTicket | undefined;
|
|
3842
|
+
if (curProgress.waitingOnReplyKey !== undefined) {
|
|
3843
|
+
const lane = this.replyLanes.get(curProgress.waitingOnReplyKey);
|
|
3844
|
+
if (lane?.active !== undefined) {
|
|
3845
|
+
nextTicket = lane.active;
|
|
3846
|
+
} else {
|
|
3847
|
+
return lane?.lastProgress ?? blocker;
|
|
3848
|
+
}
|
|
3849
|
+
} else if (curTicket !== undefined) {
|
|
3850
|
+
nextTicket = this.sendOrder.blockingTicket(curTicket);
|
|
3851
|
+
}
|
|
3852
|
+
if (nextTicket === undefined || (curTicket !== undefined && nextTicket.seq >= curTicket.seq)) {
|
|
3853
|
+
return blocker;
|
|
3854
|
+
}
|
|
3855
|
+
curTicket = nextTicket;
|
|
3856
|
+
curProgress = this.ticketProgress.get(nextTicket);
|
|
3857
|
+
if (curProgress !== undefined) blocker = curProgress;
|
|
3858
|
+
}
|
|
3859
|
+
return blocker;
|
|
3860
|
+
}
|
|
3861
|
+
|
|
3862
|
+
private syncProgressFromBlocker(progress: SendProgress, blocker: SendProgress | undefined): void {
|
|
3863
|
+
if (blocker === undefined || !blocker.hadProgress) return;
|
|
3864
|
+
progress.hadProgress = true;
|
|
3865
|
+
if (blocker.lastProgressAt > progress.lastProgressAt) {
|
|
3866
|
+
progress.lastProgressAt = blocker.lastProgressAt;
|
|
3867
|
+
}
|
|
3868
|
+
}
|
|
3869
|
+
|
|
3870
|
+
private notifyQueuedProgress(senderKey: string, source: SendProgress, now: number): void {
|
|
3871
|
+
const queued = this.queuedProgress.get(senderKey);
|
|
3872
|
+
if (queued === undefined) return;
|
|
3873
|
+
for (const waiting of queued) {
|
|
3874
|
+
if (!waiting.reportingEnabled) continue;
|
|
3875
|
+
const blocker = this.resolveBlockerProgress(waiting, waiting.ticket);
|
|
3876
|
+
if (blocker !== source) continue;
|
|
3877
|
+
this.syncProgressFromBlocker(waiting, source);
|
|
3878
|
+
this.scheduleProgressHold(waiting, now);
|
|
3879
|
+
}
|
|
3880
|
+
}
|
|
3881
|
+
|
|
3882
|
+
private markRoutingProgress(request: RemoteSendRequest): void {
|
|
3883
|
+
this.storeAnswered();
|
|
3884
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
3885
|
+
if (progress !== undefined) {
|
|
3886
|
+
const now = performance.now();
|
|
3887
|
+
progress.hadProgress = true;
|
|
3888
|
+
if (now > progress.lastProgressAt) progress.lastProgressAt = now;
|
|
3889
|
+
this.scheduleProgressHold(progress, now);
|
|
3890
|
+
this.notifyQueuedProgress(deliveryTargetIdentity(request.from), progress, now);
|
|
3891
|
+
}
|
|
3892
|
+
}
|
|
3893
|
+
|
|
3894
|
+
private sendHoldFrame(
|
|
3895
|
+
socket: BrokerConnectedSession["socket"],
|
|
3896
|
+
messageId: string,
|
|
3897
|
+
attemptId: string | undefined,
|
|
3898
|
+
timeoutMs: number,
|
|
3899
|
+
deadline: { clientDeadlineAt: number },
|
|
3900
|
+
): void {
|
|
3901
|
+
const nextDeadline = performance.now() + timeoutMs + HELD_SEND_GRACE_MS;
|
|
3902
|
+
if (nextDeadline > deadline.clientDeadlineAt) deadline.clientDeadlineAt = nextDeadline;
|
|
3903
|
+
this.host.write(socket, {
|
|
3904
|
+
type: "send_held",
|
|
3905
|
+
messageId,
|
|
3906
|
+
...(attemptId === undefined ? {} : { attemptId }),
|
|
3907
|
+
timeoutMs,
|
|
3908
|
+
});
|
|
3909
|
+
}
|
|
3910
|
+
|
|
3911
|
+
private extendBeforeLocalReceiptWait(request: RemoteSendRequest, target: BrokerConnectedSession): void {
|
|
3912
|
+
if (target.acknowledgesReceipt !== true || !this.sameLocalTarget(target)) return;
|
|
3913
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
3914
|
+
if (progress === undefined) return;
|
|
3915
|
+
const now = performance.now();
|
|
3916
|
+
const remaining = Math.max(0, progress.startedAt + RECIPIENT_SAVE_MAX_WAIT_MS - now);
|
|
3917
|
+
const waitMs = Math.min(RECIPIENT_RECEIPT_TIMEOUT_MS, remaining);
|
|
3918
|
+
if (waitMs > 0 && progress.clientDeadlineAt - (now + waitMs) < MIN_WAIT_CLIENT_MARGIN_MS) {
|
|
3919
|
+
this.sendHoldFrame(request.socket, request.message.id, request.attemptId, waitMs, progress);
|
|
3920
|
+
}
|
|
3921
|
+
}
|
|
3922
|
+
|
|
3923
|
+
/** Wait for strictly earlier remote/local sends to `options.targetIdentity`, inheriting blocker store progress. */
|
|
3924
|
+
private async waitForEarlierRemote(
|
|
3925
|
+
request: RemoteSendRequest,
|
|
3926
|
+
ticket: SendOrderTicket,
|
|
3927
|
+
options: WaitForEarlierOptions,
|
|
3928
|
+
): Promise<void> {
|
|
3929
|
+
const wait = this.sendOrder.waitForEarlier(ticket, options);
|
|
3930
|
+
if (wait === undefined) return;
|
|
3931
|
+
const senderKey = ticket.senderKey;
|
|
3932
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
3933
|
+
if (progress !== undefined) {
|
|
3934
|
+
this.trackQueuedProgress(senderKey, progress);
|
|
3935
|
+
this.syncProgressFromBlocker(progress, this.resolveBlockerProgress(progress, ticket));
|
|
3936
|
+
this.scheduleProgressHold(progress);
|
|
3937
|
+
}
|
|
3938
|
+
try {
|
|
3939
|
+
await wait;
|
|
3940
|
+
} finally {
|
|
3941
|
+
if (progress !== undefined) this.untrackQueuedProgress(senderKey, progress);
|
|
3942
|
+
}
|
|
3943
|
+
const lastBlocker = this.sendOrder.lastBlockingTicket(ticket);
|
|
3944
|
+
if (progress !== undefined && lastBlocker !== undefined) {
|
|
3945
|
+
this.syncProgressFromBlocker(progress, this.ticketProgress.get(lastBlocker));
|
|
3946
|
+
this.scheduleProgressHold(progress);
|
|
3947
|
+
}
|
|
3948
|
+
}
|
|
3949
|
+
|
|
3950
|
+
/** Latest monotonic store-progress timestamp for `earlier` or its upstream blocker chain, if any. */
|
|
3951
|
+
private blockerProgressAt(earlier: SendOrderTicket): number | undefined {
|
|
3952
|
+
const own = this.ticketProgress.get(earlier);
|
|
3953
|
+
if (own === undefined) return undefined;
|
|
3954
|
+
let latest = own.hadProgress ? own.lastProgressAt : undefined;
|
|
3955
|
+
const upstream = this.resolveBlockerProgress(own, earlier);
|
|
3956
|
+
if (upstream !== undefined && upstream.hadProgress) {
|
|
3957
|
+
latest = latest === undefined ? upstream.lastProgressAt : Math.max(latest, upstream.lastProgressAt);
|
|
3958
|
+
}
|
|
3959
|
+
return latest;
|
|
3960
|
+
}
|
|
3961
|
+
|
|
3962
|
+
/** Wait for strictly earlier sends from the same sender to `target` before performing a local commit. */
|
|
3963
|
+
private async waitForEarlierLocal(
|
|
3964
|
+
ticket: SendOrderTicket,
|
|
3965
|
+
target: BrokerConnectedSession,
|
|
3966
|
+
localBound?: Promise<"expired">,
|
|
3967
|
+
localBoundExpired?: boolean,
|
|
3968
|
+
): Promise<void> {
|
|
3969
|
+
const entry = this.presence.get(target);
|
|
3970
|
+
const names: string[] = [];
|
|
3971
|
+
if (target.info.name !== undefined) names.push(target.info.name);
|
|
3972
|
+
if (target.registrationName !== undefined && target.registrationName !== target.info.name)
|
|
3973
|
+
names.push(target.registrationName);
|
|
3974
|
+
const aliases: string[] = [];
|
|
3975
|
+
if (entry?.awaits !== undefined) aliases.push(entry.awaits.id);
|
|
3976
|
+
if (entry?.alias !== undefined) aliases.push(entry.alias.intercomSessionId);
|
|
3977
|
+
const targetMatch: TargetMatchInfo = {
|
|
3978
|
+
sessionId: target.info.id,
|
|
3979
|
+
...(names.length === 0 ? {} : { names }),
|
|
3980
|
+
...(target.registrationReturnAddress === undefined
|
|
3981
|
+
? {}
|
|
3982
|
+
: { atomicSessionId: target.registrationReturnAddress }),
|
|
3983
|
+
...(aliases.length === 0 ? {} : { aliases }),
|
|
3984
|
+
};
|
|
3985
|
+
const hasBound = localBound !== undefined || localBoundExpired === true;
|
|
3986
|
+
const ownProgress = hasBound ? this.ticketProgress.get(ticket) : undefined;
|
|
3987
|
+
const wait = this.sendOrder.waitForEarlier(ticket, {
|
|
3988
|
+
targetIdentity: deliveryTargetIdentity(target),
|
|
3989
|
+
...(entry === undefined ? {} : { targetRegSeq: entry.registeredSeq }),
|
|
3990
|
+
...(entry?.awaits === undefined ? {} : { awaitedId: entry.awaits.id }),
|
|
3991
|
+
...(entry?.alias === undefined ? {} : { aliasId: entry.alias.intercomSessionId }),
|
|
3992
|
+
targetMatch,
|
|
3993
|
+
...(localBound === undefined ? {} : { localBound }),
|
|
3994
|
+
...(localBoundExpired === true ? { localBoundExpired: true } : {}),
|
|
3995
|
+
...(hasBound
|
|
3996
|
+
? {
|
|
3997
|
+
blockerProgressAt: (earlier) => this.blockerProgressAt(earlier),
|
|
3998
|
+
progressSilenceMs: LOCAL_RETRY_LOOKUP_BOUND_MS,
|
|
3999
|
+
maxLocalWaitUntil: (ownProgress?.startedAt ?? performance.now()) + PRESENCE_REGISTRATION_BOUND_MS,
|
|
4000
|
+
}
|
|
4001
|
+
: {}),
|
|
4002
|
+
});
|
|
4003
|
+
if (wait !== undefined) await wait;
|
|
4004
|
+
}
|
|
4005
|
+
|
|
4006
|
+
/**
|
|
4007
|
+
* Wait for any initial or active outbox scan of `identity` on this broker, bounded by `expired`, only when
|
|
4008
|
+
* `senderKey` actually has an unsettled outbox waiter for `identity`. Unrelated senders and targets with no
|
|
4009
|
+
* unsettled outbox rows from this sender never wait behind slow presence writes or other senders' claims.
|
|
4010
|
+
*/
|
|
4011
|
+
private async waitForPendingOutboxScan(
|
|
4012
|
+
senderKey: string,
|
|
4013
|
+
identity: string,
|
|
4014
|
+
expired: Promise<"expired">,
|
|
4015
|
+
): Promise<void> {
|
|
4016
|
+
if (!this.unsettledOutboxPairs.has(outboxPairKey(senderKey, identity))) return;
|
|
4017
|
+
const initial = this.initialScans.get(identity)?.promise;
|
|
4018
|
+
if (initial !== undefined) await Promise.race([initial, expired]);
|
|
4019
|
+
if (this.closed) return;
|
|
4020
|
+
const active = this.scanning.get(identity)?.done;
|
|
4021
|
+
if (active !== undefined) await Promise.race([active, expired]);
|
|
4022
|
+
}
|
|
4023
|
+
|
|
4024
|
+
/**
|
|
4025
|
+
* Hand a local send back to `handleBrokerSend` while capturing any synchronous re-entrant fallback into
|
|
4026
|
+
* `routeSend` (e.g. if the local target disconnected, was fenced, or lost an alias mid-lookup) so the fallback
|
|
4027
|
+
* runs in place on the same order ticket without self-deadlock or priority inversion.
|
|
4028
|
+
*/
|
|
4029
|
+
private async commitLocalNotFound(
|
|
4030
|
+
request: RemoteSendRequest,
|
|
4031
|
+
ticket: SendOrderTicket,
|
|
4032
|
+
openingDeadlineAt?: number,
|
|
4033
|
+
): Promise<void> {
|
|
4034
|
+
if (this.closed) return;
|
|
4035
|
+
if (request.localTarget !== undefined && openingDeadlineAt !== undefined) {
|
|
4036
|
+
await this.awaitLocalReturnPathIfRefused(request, request.localTarget, ticket, openingDeadlineAt);
|
|
4037
|
+
if (this.closed) return;
|
|
4038
|
+
this.extendBeforeLocalReceiptWait(request, request.localTarget);
|
|
4039
|
+
}
|
|
4040
|
+
const frame: { readonly clientMessage: SendClientMessage; reentrant?: RemoteSendRequest } = {
|
|
4041
|
+
clientMessage: request.clientMessage,
|
|
4042
|
+
};
|
|
4043
|
+
const previous = this.activeLocalCommit;
|
|
4044
|
+
this.activeLocalCommit = frame;
|
|
4045
|
+
try {
|
|
4046
|
+
request.notFound();
|
|
4047
|
+
} finally {
|
|
4048
|
+
this.activeLocalCommit = previous;
|
|
4049
|
+
}
|
|
4050
|
+
if (frame.reentrant !== undefined && !this.closed) {
|
|
4051
|
+
this.sendOrder.reenterRemote(ticket);
|
|
4052
|
+
const progress = this.sendProgress.get(frame.reentrant.clientMessage);
|
|
4053
|
+
if (progress !== undefined && frame.reentrant.localTarget === undefined) {
|
|
4054
|
+
progress.reportingEnabled = true;
|
|
4055
|
+
if (progress.hadProgress) this.scheduleProgressHold(progress);
|
|
4056
|
+
}
|
|
4057
|
+
await this.routeSendSafely(frame.reentrant, ticket, false);
|
|
4058
|
+
}
|
|
4059
|
+
}
|
|
4060
|
+
|
|
4061
|
+
/**
|
|
4062
|
+
* Keep `ticket` in place while a local cross-group send waits for a matching in-flight return-path opening, so the
|
|
4063
|
+
* send never releases its SendOrderGate slot early or re-enters `routeSend` for a second outbox lookup.
|
|
4064
|
+
*/
|
|
4065
|
+
private async awaitLocalReturnPathIfRefused(
|
|
4066
|
+
request: RemoteSendRequest,
|
|
4067
|
+
target: BrokerConnectedSession,
|
|
4068
|
+
ticket: SendOrderTicket,
|
|
4069
|
+
openingDeadlineAt: number,
|
|
4070
|
+
): Promise<void> {
|
|
4071
|
+
const returnPaths = this.host.returnPaths;
|
|
4072
|
+
if (
|
|
4073
|
+
returnPaths === undefined ||
|
|
4074
|
+
request.requirePendingReply ||
|
|
4075
|
+
request.supervisorSend ||
|
|
4076
|
+
request.returnPathArrivalSeq === undefined ||
|
|
4077
|
+
!mayUseReturnPath(request.message, request.supervisorSend, request.from)
|
|
4078
|
+
)
|
|
4079
|
+
return;
|
|
4080
|
+
const isCrossGroupRefusal = (): boolean => {
|
|
4081
|
+
if (reachesSession(request.from, target.info)) return false;
|
|
4082
|
+
if (
|
|
4083
|
+
isVerticalBypass({
|
|
4084
|
+
replyTo: request.message.replyTo,
|
|
4085
|
+
sender: request.from.info,
|
|
4086
|
+
target: target.info,
|
|
4087
|
+
supervisorCache: this.host.supervisorCache,
|
|
4088
|
+
})
|
|
4089
|
+
)
|
|
4090
|
+
return false;
|
|
4091
|
+
if (
|
|
4092
|
+
request.message.replyTo !== undefined &&
|
|
4093
|
+
this.host.pendingQuestions.matchesReply(request.from.info.id, target.info.id, request.message.replyTo)
|
|
4094
|
+
)
|
|
4095
|
+
return false;
|
|
4096
|
+
return !returnPaths.allows(request.from, returnPathParty(target), request.message.id);
|
|
4097
|
+
};
|
|
4098
|
+
const targetParty = returnPathParty(target);
|
|
4099
|
+
if (!isCrossGroupRefusal() || !returnPaths.hasPendingOpening(request.from, targetParty, request.returnPathArrivalSeq))
|
|
4100
|
+
return;
|
|
4101
|
+
this.sendOrder.markUnbounded(ticket);
|
|
4102
|
+
while (!this.closed && isCrossGroupRefusal()) {
|
|
4103
|
+
const outcome = await this.awaitReturnPathOpening(
|
|
4104
|
+
request.from,
|
|
4105
|
+
targetParty,
|
|
4106
|
+
request.returnPathArrivalSeq,
|
|
4107
|
+
openingDeadlineAt,
|
|
4108
|
+
);
|
|
4109
|
+
if (this.closed || outcome !== "settled") return;
|
|
3347
4110
|
}
|
|
3348
4111
|
}
|
|
3349
4112
|
|
|
@@ -3352,19 +4115,50 @@ export class MultiNodeRouter {
|
|
|
3352
4115
|
* broker's own routes say answers an ask to its sender's alias goes out as from that alias while the alias is
|
|
3353
4116
|
* confirmed its own, and is refused as retryable while it is not: the database that could confirm it failed.
|
|
3354
4117
|
*/
|
|
3355
|
-
private deliverLocally(
|
|
4118
|
+
private async deliverLocally(
|
|
4119
|
+
request: RemoteSendRequest,
|
|
4120
|
+
target: BrokerConnectedSession,
|
|
4121
|
+
ticket: SendOrderTicket,
|
|
4122
|
+
remainingBoundMs: number,
|
|
4123
|
+
openingDeadlineAt: number,
|
|
4124
|
+
): Promise<void> {
|
|
4125
|
+
let bound: ReturnType<typeof setTimeout> | undefined;
|
|
4126
|
+
let boundExpired = remainingBoundMs <= 0;
|
|
4127
|
+
const expired =
|
|
4128
|
+
boundExpired
|
|
4129
|
+
? Promise.resolve("expired" as const)
|
|
4130
|
+
: new Promise<"expired">((resolve) => {
|
|
4131
|
+
bound = setTimeout(() => {
|
|
4132
|
+
boundExpired = true;
|
|
4133
|
+
resolve("expired");
|
|
4134
|
+
}, remainingBoundMs);
|
|
4135
|
+
bound.unref?.();
|
|
4136
|
+
});
|
|
4137
|
+
try {
|
|
4138
|
+
await this.waitForEarlierLocal(ticket, target, expired, boundExpired);
|
|
4139
|
+
} finally {
|
|
4140
|
+
clearTimeout(bound);
|
|
4141
|
+
}
|
|
4142
|
+
if (this.closed) return;
|
|
3356
4143
|
const replyTo = request.message.replyTo;
|
|
3357
4144
|
const alias = this.replyAlias(request);
|
|
3358
4145
|
const pending = this.host.pendingQuestions;
|
|
3359
|
-
if (replyTo === undefined || alias === undefined || pending.matchesReply(request.from.info.id, target.info.id, replyTo))
|
|
3360
|
-
|
|
4146
|
+
if (replyTo === undefined || alias === undefined || pending.matchesReply(request.from.info.id, target.info.id, replyTo)) {
|
|
4147
|
+
this.extendBeforeLocalReceiptWait(request, target);
|
|
4148
|
+
return this.commitLocalNotFound(request, ticket, openingDeadlineAt);
|
|
4149
|
+
}
|
|
3361
4150
|
// Only the database that failed could show an ask to the alias that no route here names: a reply that must answer
|
|
3362
4151
|
// an open ask is refused as retryable, not for good.
|
|
3363
|
-
if (!pending.matchesReply(alias.id, target.info.id, replyTo))
|
|
3364
|
-
|
|
4152
|
+
if (!pending.matchesReply(alias.id, target.info.id, replyTo)) {
|
|
4153
|
+
if (request.requirePendingReply) return this.refuseRouteUnavailable(request);
|
|
4154
|
+
this.extendBeforeLocalReceiptWait(request, target);
|
|
4155
|
+
return this.commitLocalNotFound(request, ticket, openingDeadlineAt);
|
|
4156
|
+
}
|
|
3365
4157
|
const sending = this.answeringAsHeld(request, alias);
|
|
3366
4158
|
if (sending === UNCERTAIN_ALIAS) return this.refuseRouteUnavailable(request);
|
|
3367
|
-
|
|
4159
|
+
const chosen = sending ?? request;
|
|
4160
|
+
this.extendBeforeLocalReceiptWait(chosen, target);
|
|
4161
|
+
await this.commitLocalNotFound(chosen, ticket, openingDeadlineAt);
|
|
3368
4162
|
}
|
|
3369
4163
|
|
|
3370
4164
|
/** Whether this broker's own routes authorize `request`'s reply to `target`. */
|
|
@@ -3388,127 +4182,256 @@ export class MultiNodeRouter {
|
|
|
3388
4182
|
return false;
|
|
3389
4183
|
}
|
|
3390
4184
|
|
|
3391
|
-
private
|
|
3392
|
-
|
|
3393
|
-
|
|
4185
|
+
private awaitReturnPathOpeningForTarget(
|
|
4186
|
+
from: BrokerConnectedSession,
|
|
4187
|
+
target: string,
|
|
4188
|
+
beforeSeq: number | undefined,
|
|
4189
|
+
deadlineAt: number,
|
|
4190
|
+
): Promise<ReturnPathOpeningWaitOutcome | "none"> {
|
|
4191
|
+
if (this.closed || beforeSeq === undefined || this.host.returnPaths === undefined) return Promise.resolve("none");
|
|
4192
|
+
return new Promise<ReturnPathOpeningWaitOutcome | "none">((resolve) => {
|
|
4193
|
+
const waited = this.host.returnPaths!.whenOpeningForTarget(from, target, beforeSeq, deadlineAt, resolve);
|
|
4194
|
+
if (!waited) resolve("none");
|
|
4195
|
+
});
|
|
4196
|
+
}
|
|
4197
|
+
|
|
4198
|
+
private awaitReturnPathOpening(
|
|
4199
|
+
from: BrokerConnectedSession,
|
|
4200
|
+
target: ReturnPathParty,
|
|
4201
|
+
beforeSeq: number | undefined,
|
|
4202
|
+
deadlineAt: number,
|
|
4203
|
+
): Promise<ReturnPathOpeningWaitOutcome | "none"> {
|
|
4204
|
+
if (this.closed || beforeSeq === undefined || this.host.returnPaths === undefined) return Promise.resolve("none");
|
|
4205
|
+
return new Promise<ReturnPathOpeningWaitOutcome | "none">((resolve) => {
|
|
4206
|
+
const waited = this.host.returnPaths!.whenOpening(from, target, beforeSeq, deadlineAt, resolve);
|
|
4207
|
+
if (!waited) resolve("none");
|
|
4208
|
+
});
|
|
4209
|
+
}
|
|
4210
|
+
|
|
4211
|
+
private async route(request: RemoteSendRequest, ticket: SendOrderTicket, openingDeadlineAt: number): Promise<void> {
|
|
4212
|
+
if (request.message.replyTo !== undefined) {
|
|
4213
|
+
const settling = [...this.settling];
|
|
4214
|
+
if (settling.length > 0) {
|
|
4215
|
+
const answeredBefore = this.storeAnsweredAt;
|
|
4216
|
+
await Promise.all(settling);
|
|
4217
|
+
if (this.storeAnsweredAt > answeredBefore) this.markRoutingProgress(request);
|
|
4218
|
+
}
|
|
4219
|
+
}
|
|
4220
|
+
if (this.closed) return;
|
|
4221
|
+
if (await this.routeReleasedReply(request, undefined, ticket)) return;
|
|
4222
|
+
if (this.closed) return;
|
|
3394
4223
|
const from = request.from;
|
|
3395
|
-
|
|
3396
|
-
(
|
|
3397
|
-
lease.subject.kind === "presence" &&
|
|
3398
|
-
lease.holderId !== this.brokerId &&
|
|
3399
|
-
!this.host.sessions.has(lease.subject.session.id),
|
|
3400
|
-
);
|
|
3401
|
-
const presenceOf = (lease: LeaseState) => lease.subject as RecipientPresenceSubject;
|
|
3402
|
-
// #5607: a plain send to an id a session holds as an alias reaches that session.
|
|
3403
|
-
const plain = isPlainSend(request);
|
|
3404
|
-
const exact =
|
|
3405
|
-
live.find((lease) => presenceOf(lease).session.id === request.target) ??
|
|
3406
|
-
(plain ? await this.aliasHolder(live, request.target) : undefined);
|
|
3407
|
-
let resolved: LeaseState | undefined = exact;
|
|
3408
|
-
if (resolved === undefined) {
|
|
3409
|
-
const replyAuthorized = await this.replyAuthorizedTargets(
|
|
3410
|
-
request,
|
|
3411
|
-
live.map((lease) => presenceOf(lease).session.id),
|
|
3412
|
-
);
|
|
3413
|
-
const visible = live.filter((lease) => {
|
|
3414
|
-
const info = presenceOf(lease).session;
|
|
3415
|
-
return (
|
|
3416
|
-
reachesSession(from, info) ||
|
|
3417
|
-
(request.supervisorSend && info.id === from.supervisorId) ||
|
|
3418
|
-
replyAuthorized.has(info.id)
|
|
3419
|
-
);
|
|
3420
|
-
});
|
|
3421
|
-
const nonAgentNamed = visible.some(
|
|
4224
|
+
for (;;) {
|
|
4225
|
+
const live = (await this.state.leases.listLive("presence", BROKER_LEASE_STALE_AFTER_MS)).filter(
|
|
3422
4226
|
(lease) =>
|
|
3423
|
-
|
|
3424
|
-
|
|
4227
|
+
lease.subject.kind === "presence" &&
|
|
4228
|
+
lease.holderId !== this.brokerId &&
|
|
4229
|
+
!this.host.sessions.has(lease.subject.session.id),
|
|
3425
4230
|
);
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
|
|
3432
|
-
|
|
4231
|
+
if (this.closed) return;
|
|
4232
|
+
this.markRoutingProgress(request);
|
|
4233
|
+
const presenceOf = (lease: LeaseState) => lease.subject as RecipientPresenceSubject;
|
|
4234
|
+
// #5607: a plain send to an id a session holds as an alias reaches that session.
|
|
4235
|
+
const plain = isPlainSend(request);
|
|
4236
|
+
const exact =
|
|
4237
|
+
live.find((lease) => presenceOf(lease).session.id === request.target) ??
|
|
4238
|
+
(plain ? await this.aliasHolder(live, request.target) : undefined);
|
|
4239
|
+
if (this.closed) return;
|
|
4240
|
+
let resolved: LeaseState | undefined = exact;
|
|
4241
|
+
if (resolved === undefined) {
|
|
4242
|
+
const replyAuthorized = await this.replyAuthorizedTargets(
|
|
4243
|
+
request,
|
|
4244
|
+
live.map((lease) => presenceOf(lease).session.id),
|
|
4245
|
+
);
|
|
4246
|
+
if (this.closed) return;
|
|
4247
|
+
const visible = live.filter((lease) => {
|
|
4248
|
+
const info = presenceOf(lease).session;
|
|
4249
|
+
return (
|
|
4250
|
+
reachesSession(from, info) ||
|
|
4251
|
+
(request.supervisorSend && info.id === from.supervisorId) ||
|
|
4252
|
+
replyAuthorized.has(info.id)
|
|
4253
|
+
);
|
|
3433
4254
|
});
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
resolution
|
|
3451
|
-
|
|
4255
|
+
const nonAgentNamed = visible.some(
|
|
4256
|
+
(lease) =>
|
|
4257
|
+
!isAgentRecipient(presenceOf(lease).session) &&
|
|
4258
|
+
presenceOf(lease).session.name?.toLowerCase() === request.target.toLowerCase(),
|
|
4259
|
+
);
|
|
4260
|
+
const agents = visible.filter((lease) => isAgentRecipient(presenceOf(lease).session));
|
|
4261
|
+
const infos = agents.map((lease) => presenceOf(lease).session);
|
|
4262
|
+
let resolution: SessionTargetResolution = resolveSessionTarget(infos, request.target);
|
|
4263
|
+
if (resolution.kind === "not_found") {
|
|
4264
|
+
resolution = resolveSessionTargetByHostSessionId(infos, request.target, (info) => {
|
|
4265
|
+
const lease = agents.find((candidate) => presenceOf(candidate).session === info);
|
|
4266
|
+
return lease === undefined ? undefined : returnAddressOf(presenceOf(lease).targetIdentity);
|
|
4267
|
+
});
|
|
4268
|
+
}
|
|
4269
|
+
// A sender this one holds a return path to answers to its name only when nothing visible does.
|
|
4270
|
+
const answerable =
|
|
4271
|
+
resolution.kind === "not_found"
|
|
4272
|
+
? live.filter(
|
|
4273
|
+
(lease) =>
|
|
4274
|
+
isAgentRecipient(presenceOf(lease).session) &&
|
|
4275
|
+
!agents.includes(lease) &&
|
|
4276
|
+
this.holdsReturnPath(request, {
|
|
4277
|
+
sessionId: presenceOf(lease).session.id,
|
|
4278
|
+
identity: stableIdentity(presenceOf(lease).targetIdentity),
|
|
4279
|
+
}),
|
|
4280
|
+
)
|
|
4281
|
+
: [];
|
|
4282
|
+
if (answerable.length > 0) {
|
|
4283
|
+
const answered = resolveSessionTarget(
|
|
4284
|
+
answerable.map((lease) => presenceOf(lease).session),
|
|
4285
|
+
request.target,
|
|
4286
|
+
);
|
|
4287
|
+
if (answered.kind !== "not_found") {
|
|
4288
|
+
resolution = answered;
|
|
4289
|
+
agents.push(...answerable);
|
|
4290
|
+
}
|
|
4291
|
+
}
|
|
4292
|
+
if (resolution.kind === "resolved") {
|
|
4293
|
+
const session = resolution.session;
|
|
4294
|
+
resolved = agents.find((lease) => presenceOf(lease).session === session);
|
|
4295
|
+
} else if (resolution.kind !== "not_found") {
|
|
4296
|
+
this.fail(request, sessionTargetFailureReason(request.clientMessage.to, resolution));
|
|
4297
|
+
return;
|
|
4298
|
+
} else if (nonAgentNamed) {
|
|
4299
|
+
this.fail(request, NON_AGENT_RECIPIENT_REFUSAL);
|
|
4300
|
+
return;
|
|
3452
4301
|
}
|
|
3453
4302
|
}
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
4303
|
+
// A presence whose id is bound to another session's return address (from a broker that grants ids without
|
|
4304
|
+
// binding them) is not that session: what is addressed to the id waits for the session it is bound to.
|
|
4305
|
+
if (resolved !== undefined) {
|
|
4306
|
+
const holds = await this.holdsSessionId(
|
|
4307
|
+
presenceOf(resolved).session.id,
|
|
4308
|
+
returnAddressOf(presenceOf(resolved).targetIdentity),
|
|
4309
|
+
);
|
|
4310
|
+
if (this.closed) return;
|
|
4311
|
+
this.markRoutingProgress(request);
|
|
4312
|
+
if (!holds) resolved = undefined;
|
|
4313
|
+
}
|
|
4314
|
+
if (resolved !== undefined) {
|
|
4315
|
+
const subject = presenceOf(resolved);
|
|
4316
|
+
await this.deliverThroughOutbox(
|
|
4317
|
+
request,
|
|
4318
|
+
{
|
|
4319
|
+
identity: subject.targetIdentity,
|
|
4320
|
+
sessionId: subject.session.id,
|
|
4321
|
+
groups: sessionGroups(subject.session),
|
|
4322
|
+
gates: subject.session,
|
|
4323
|
+
hostBrokerId: resolved.holderId,
|
|
4324
|
+
},
|
|
4325
|
+
ticket,
|
|
4326
|
+
openingDeadlineAt,
|
|
4327
|
+
{
|
|
4328
|
+
sessionId: subject.session.id,
|
|
4329
|
+
...(subject.session.name === undefined ? {} : { names: [subject.session.name] }),
|
|
4330
|
+
...(returnAddressOf(subject.targetIdentity) === undefined
|
|
4331
|
+
? {}
|
|
4332
|
+
: { atomicSessionId: returnAddressOf(subject.targetIdentity) }),
|
|
4333
|
+
...(subject.aliases === undefined || subject.aliases.length === 0
|
|
4334
|
+
? {}
|
|
4335
|
+
: { aliases: subject.aliases.map((alias) => alias.id) }),
|
|
4336
|
+
},
|
|
4337
|
+
);
|
|
3459
4338
|
return;
|
|
3460
|
-
}
|
|
3461
|
-
|
|
4339
|
+
}
|
|
4340
|
+
if (!request.supervisorSend && !request.requirePendingReply && request.expectedRecipientId === undefined) {
|
|
4341
|
+
if (await this.holdForRemoteReservation(request, ticket)) return;
|
|
4342
|
+
if (this.closed) return;
|
|
4343
|
+
this.markRoutingProgress(request);
|
|
4344
|
+
}
|
|
4345
|
+
const known = await this.resolveKnownSession(request);
|
|
4346
|
+
if (this.closed) return;
|
|
4347
|
+
this.markRoutingProgress(request);
|
|
4348
|
+
if (known === "answered") return;
|
|
4349
|
+
if (known !== undefined) {
|
|
4350
|
+
await this.deliverThroughOutbox(
|
|
4351
|
+
request,
|
|
4352
|
+
{
|
|
4353
|
+
identity: known.targetIdentity,
|
|
4354
|
+
sessionId: known.intercomSessionId,
|
|
4355
|
+
groups: new Set(known.groups),
|
|
4356
|
+
gates: known,
|
|
4357
|
+
},
|
|
4358
|
+
ticket,
|
|
4359
|
+
openingDeadlineAt,
|
|
4360
|
+
{
|
|
4361
|
+
sessionId: known.intercomSessionId,
|
|
4362
|
+
...(known.name === undefined ? {} : { names: [known.name] }),
|
|
4363
|
+
...(known.atomicSessionId === undefined ? {} : { atomicSessionId: known.atomicSessionId }),
|
|
4364
|
+
...(known.aliasSessionId === undefined ? {} : { aliases: [known.aliasSessionId] }),
|
|
4365
|
+
},
|
|
4366
|
+
);
|
|
3462
4367
|
return;
|
|
3463
4368
|
}
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
4369
|
+
// Refusal path only: after all visible/remote/known resolution found nothing, wait up to the per-send bound if
|
|
4370
|
+
// a matching cross-group opening handed over before this send arrived is still settling.
|
|
4371
|
+
if (
|
|
4372
|
+
!request.requirePendingReply &&
|
|
4373
|
+
mayUseReturnPath(request.message, request.supervisorSend, from) &&
|
|
4374
|
+
request.returnPathArrivalSeq !== undefined
|
|
4375
|
+
) {
|
|
4376
|
+
const outcome = await this.awaitReturnPathOpeningForTarget(
|
|
4377
|
+
from,
|
|
4378
|
+
request.target,
|
|
4379
|
+
request.returnPathArrivalSeq,
|
|
4380
|
+
openingDeadlineAt,
|
|
4381
|
+
);
|
|
4382
|
+
if (this.closed) return;
|
|
4383
|
+
if (outcome === "settled") {
|
|
4384
|
+
// If the settled opening was from a local session on this broker, deliver locally while keeping `ticket`.
|
|
4385
|
+
const localAnswerable = (this.host.returnPaths?.answerable(from) ?? []).filter((session) =>
|
|
4386
|
+
isAgentRecipient(session.info),
|
|
4387
|
+
);
|
|
4388
|
+
const localMatch = resolveSessionTarget(
|
|
4389
|
+
localAnswerable.map((session) => session.info),
|
|
4390
|
+
request.target,
|
|
4391
|
+
);
|
|
4392
|
+
if (localMatch.kind === "resolved") {
|
|
4393
|
+
const localSession = this.host.sessions.get(localMatch.session.id);
|
|
4394
|
+
if (localSession !== undefined) {
|
|
4395
|
+
await this.waitForEarlierLocal(ticket, localSession);
|
|
4396
|
+
if (this.closed) return;
|
|
4397
|
+
this.extendBeforeLocalReceiptWait(request, localSession);
|
|
4398
|
+
request.notFound();
|
|
4399
|
+
return;
|
|
4400
|
+
}
|
|
4401
|
+
}
|
|
4402
|
+
continue;
|
|
4403
|
+
}
|
|
4404
|
+
}
|
|
4405
|
+
if (this.closed) return;
|
|
4406
|
+
request.notFound();
|
|
3496
4407
|
return;
|
|
3497
4408
|
}
|
|
3498
|
-
request.notFound();
|
|
3499
4409
|
}
|
|
3500
4410
|
|
|
3501
4411
|
/** #4386: a reply to a released asker's question goes to the asker's owner, once; true when it was one. */
|
|
3502
|
-
private async routeReleasedReply(
|
|
4412
|
+
private async routeReleasedReply(
|
|
4413
|
+
request: RemoteSendRequest,
|
|
4414
|
+
answered?: ReleasedAskRecord,
|
|
4415
|
+
ticket?: SendOrderTicket,
|
|
4416
|
+
): Promise<boolean> {
|
|
3503
4417
|
const decision = await decideReleasedAskerReply(this.state, request, answered);
|
|
3504
4418
|
if (decision === undefined) return false;
|
|
3505
4419
|
switch (decision.kind) {
|
|
3506
4420
|
case "refused":
|
|
3507
4421
|
this.failReleasedReply(request, decision.error);
|
|
3508
4422
|
return true;
|
|
3509
|
-
case "deliver":
|
|
4423
|
+
case "deliver": {
|
|
4424
|
+
// Released-child replies are already strictly ordered per sender by `serial("reply:...")` (#4386),
|
|
4425
|
+
// and non-reply sends never commit to a `released-reply:*` target: resolve the ticket target so
|
|
4426
|
+
// unrelated later sends do not wait, and never stall behind unrelated still-resolving plain sends.
|
|
4427
|
+
if (ticket !== undefined) {
|
|
4428
|
+
this.sendOrder.markUnbounded(ticket);
|
|
4429
|
+
this.sendOrder.resolveTarget(ticket, `released-reply:${decision.owner.identity}`);
|
|
4430
|
+
if (this.closed) return true;
|
|
4431
|
+
}
|
|
3510
4432
|
await this.sendReleasedReply(request, decision);
|
|
3511
4433
|
return true;
|
|
4434
|
+
}
|
|
3512
4435
|
}
|
|
3513
4436
|
}
|
|
3514
4437
|
|
|
@@ -3524,6 +4447,11 @@ export class MultiNodeRouter {
|
|
|
3524
4447
|
* began (`adoptSharedRouteWithin`); both lookups together end within `PRESENCE_REGISTRATION_BOUND_MS` of it, so the
|
|
3525
4448
|
* reply, and the next reply its sender queued behind it, are answered before the sender stops waiting.
|
|
3526
4449
|
*/
|
|
4450
|
+
private sameLocalTarget(target: BrokerConnectedSession): boolean {
|
|
4451
|
+
const current = this.host.sessions.get(target.info.id);
|
|
4452
|
+
return current !== undefined && deliveryTargetIdentity(current) === deliveryTargetIdentity(target);
|
|
4453
|
+
}
|
|
4454
|
+
|
|
3527
4455
|
private async routeUnreachable(
|
|
3528
4456
|
request: RemoteSendRequest,
|
|
3529
4457
|
target: BrokerConnectedSession,
|
|
@@ -3531,6 +4459,7 @@ export class MultiNodeRouter {
|
|
|
3531
4459
|
): Promise<boolean> {
|
|
3532
4460
|
const replyTo = request.message.replyTo;
|
|
3533
4461
|
if (!request.requirePendingReply || replyTo === undefined) return false;
|
|
4462
|
+
if (!this.sameLocalTarget(target)) return false;
|
|
3534
4463
|
if (this.host.pendingQuestions.matchesReply(request.from.info.id, target.info.id, replyTo)) return false;
|
|
3535
4464
|
// A retry of a reply already delivered here is answered by the local path.
|
|
3536
4465
|
if (this.host.deliveredMessages.lookupForTarget(request.message.id, request.signature, deliveryTargetIdentity(target)) !== "miss")
|
|
@@ -3597,7 +4526,7 @@ export class MultiNodeRouter {
|
|
|
3597
4526
|
late: { readonly abandoned: boolean },
|
|
3598
4527
|
): Promise<RemoteSendRequest | typeof UNCERTAIN_ALIAS> {
|
|
3599
4528
|
const replyTo = request.message.replyTo;
|
|
3600
|
-
if (replyTo === undefined) return request;
|
|
4529
|
+
if (replyTo === undefined || !this.sameLocalTarget(target)) return request;
|
|
3601
4530
|
// #5607: an ask to the id its sender holds as an alias, received under that id, is answered as that id.
|
|
3602
4531
|
const alias = this.replyAlias(request);
|
|
3603
4532
|
const repliers = alias === undefined ? [request.from.info.id] : [request.from.info.id, alias.id];
|
|
@@ -3611,15 +4540,22 @@ export class MultiNodeRouter {
|
|
|
3611
4540
|
if (here !== undefined && alias !== undefined && here === alias.id) await this.confirmAlias(request, alias);
|
|
3612
4541
|
if (here !== undefined) return answering(here);
|
|
3613
4542
|
// An ask whose route is still moving there from this broker is in the shared state once that move is done.
|
|
3614
|
-
|
|
4543
|
+
const settling = [...this.settling];
|
|
4544
|
+
if (settling.length > 0) {
|
|
4545
|
+
const answeredBefore = this.storeAnsweredAt;
|
|
4546
|
+
await Promise.all(settling);
|
|
4547
|
+
if (this.storeAnsweredAt > answeredBefore) this.markRoutingProgress(request);
|
|
4548
|
+
}
|
|
3615
4549
|
const moved = routedHere();
|
|
3616
4550
|
if (moved !== undefined && alias !== undefined && moved === alias.id) await this.confirmAlias(request, alias);
|
|
3617
4551
|
if (moved !== undefined) return answering(moved);
|
|
3618
4552
|
for (const replySenderSessionId of repliers) {
|
|
3619
4553
|
const route = { replySenderSessionId, replyTargetSessionId: target.info.id, replyTo };
|
|
3620
|
-
|
|
4554
|
+
const matched = await this.state.pendingAsks.matchesReply(route);
|
|
4555
|
+
this.markRoutingProgress(request);
|
|
4556
|
+
if (matched.status !== "match") continue;
|
|
3621
4557
|
if (alias !== undefined && replySenderSessionId === alias.id) await this.confirmAlias(request, alias);
|
|
3622
|
-
if (late.abandoned) return request;
|
|
4558
|
+
if (late.abandoned || !this.sameLocalTarget(target)) return request;
|
|
3623
4559
|
const sending = answering(replySenderSessionId);
|
|
3624
4560
|
// Its alias lost meanwhile authorizes nothing; one held unconfirmed leaves the shared route where it is.
|
|
3625
4561
|
if (sending === UNCERTAIN_ALIAS || (sending === request && replySenderSessionId !== request.from.info.id))
|
|
@@ -3630,6 +4566,7 @@ export class MultiNodeRouter {
|
|
|
3630
4566
|
targetSessionId: replySenderSessionId,
|
|
3631
4567
|
messageId: replyTo,
|
|
3632
4568
|
});
|
|
4569
|
+
this.markRoutingProgress(request);
|
|
3633
4570
|
return sending;
|
|
3634
4571
|
}
|
|
3635
4572
|
return request;
|
|
@@ -3724,6 +4661,7 @@ export class MultiNodeRouter {
|
|
|
3724
4661
|
messageId: question.messageId,
|
|
3725
4662
|
};
|
|
3726
4663
|
const recorded = await this.state.pendingAsks.record(ask);
|
|
4664
|
+
this.storeAnswered();
|
|
3727
4665
|
if (recorded.status === "invalid") continue;
|
|
3728
4666
|
// #4997: answered or failed on another broker while this one kept its copy: the copy goes.
|
|
3729
4667
|
if (recorded.status === "settled") {
|
|
@@ -3774,7 +4712,9 @@ export class MultiNodeRouter {
|
|
|
3774
4712
|
replyTargetSessionId: question.senderSessionId,
|
|
3775
4713
|
replyTo: question.messageId,
|
|
3776
4714
|
})
|
|
3777
|
-
.then(() =>
|
|
4715
|
+
.then(() => {
|
|
4716
|
+
this.storeAnswered();
|
|
4717
|
+
})
|
|
3778
4718
|
.catch((error: Error) => this.report(error));
|
|
3779
4719
|
this.settling.add(settling);
|
|
3780
4720
|
void settling.finally(() => this.settling.delete(settling));
|
|
@@ -3827,6 +4767,12 @@ export class MultiNodeRouter {
|
|
|
3827
4767
|
|
|
3828
4768
|
/** Enqueue the owner's frame; a same-id retry is answered from the row it already has, exactly as any send. */
|
|
3829
4769
|
private async sendReleasedReply(request: RemoteSendRequest, decision: ReleasedReplyDelivery): Promise<void> {
|
|
4770
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
4771
|
+
if (progress !== undefined && performance.now() >= progress.startedAt + RECIPIENT_SAVE_MAX_WAIT_MS) {
|
|
4772
|
+
await this.reopenQuestion(decision);
|
|
4773
|
+
this.fail(request, SHARED_STORAGE_UNAVAILABLE_REASON, request.requirePendingReply ? "session_not_found" : undefined);
|
|
4774
|
+
return;
|
|
4775
|
+
}
|
|
3830
4776
|
const { owner, frame } = decision;
|
|
3831
4777
|
const digest = crossNodeDigest(request.signature);
|
|
3832
4778
|
const enqueued = await this.state.outbox
|
|
@@ -3838,6 +4784,7 @@ export class MultiNodeRouter {
|
|
|
3838
4784
|
...offlineHost(owner.hostBrokerId),
|
|
3839
4785
|
})
|
|
3840
4786
|
.catch((error: Error) => this.reopenAfter(decision, error));
|
|
4787
|
+
this.markRoutingProgress(request);
|
|
3841
4788
|
if (enqueued.status === "conflict") {
|
|
3842
4789
|
await this.reopenQuestion(decision);
|
|
3843
4790
|
this.failConflict(request);
|
|
@@ -3912,8 +4859,11 @@ export class MultiNodeRouter {
|
|
|
3912
4859
|
private async answerLocalRetry(
|
|
3913
4860
|
request: RemoteSendRequest,
|
|
3914
4861
|
target: BrokerConnectedSession,
|
|
3915
|
-
owners
|
|
4862
|
+
owners: SendOwnerChecks | undefined,
|
|
4863
|
+
ticket: SendOrderTicket,
|
|
4864
|
+
openingDeadlineAt: number,
|
|
3916
4865
|
): Promise<void> {
|
|
4866
|
+
const targetIdentity = deliveryTargetIdentity(target);
|
|
3917
4867
|
const lookup = this.state.outbox.read(request.message.id);
|
|
3918
4868
|
// #5607: a lookup that fails while an owner check is still answering is reported when it is awaited below, not
|
|
3919
4869
|
// left unobserved meanwhile. The checks are waited for within the lookup's own bound, counted from now.
|
|
@@ -3924,8 +4874,12 @@ export class MultiNodeRouter {
|
|
|
3924
4874
|
return undefined;
|
|
3925
4875
|
});
|
|
3926
4876
|
let bound: ReturnType<typeof setTimeout> | undefined;
|
|
4877
|
+
let boundExpired = false;
|
|
3927
4878
|
const expired = new Promise<"expired">((resolve) => {
|
|
3928
|
-
bound = setTimeout(() =>
|
|
4879
|
+
bound = setTimeout(() => {
|
|
4880
|
+
boundExpired = true;
|
|
4881
|
+
resolve("expired");
|
|
4882
|
+
}, LOCAL_RETRY_LOOKUP_BOUND_MS);
|
|
3929
4883
|
bound.unref?.();
|
|
3930
4884
|
});
|
|
3931
4885
|
try {
|
|
@@ -3937,35 +4891,74 @@ export class MultiNodeRouter {
|
|
|
3937
4891
|
lookup.catch((error: Error) => this.report(error));
|
|
3938
4892
|
// A sender disconnected here has nothing to be answered; a recipient disconnected here, or an alias it
|
|
3939
4893
|
// no longer holds, is looked for wherever that id lives now.
|
|
3940
|
-
if (found.sender !== "lost")
|
|
4894
|
+
if (found.sender !== "lost") {
|
|
4895
|
+
await this.waitForEarlierLocal(ticket, target, expired, boundExpired);
|
|
4896
|
+
await this.commitLocalNotFound(request, ticket, openingDeadlineAt);
|
|
4897
|
+
}
|
|
3941
4898
|
return;
|
|
3942
4899
|
}
|
|
3943
4900
|
}
|
|
3944
4901
|
const existing = await Promise.race([lookup, expired]);
|
|
3945
4902
|
if (existing === "expired") {
|
|
3946
4903
|
lookup.catch((error: Error) => this.report(error));
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
4904
|
+
let sending = request;
|
|
4905
|
+
if (request.message.replyTo !== undefined) {
|
|
4906
|
+
if (!this.routedHere(request, target)) this.sendOrder.markUnbounded(ticket);
|
|
4907
|
+
const decisionStartedAt = performance.now();
|
|
4908
|
+
const adopted = await this.adoptSharedRouteWithin(request, target);
|
|
4909
|
+
if (adopted === UNCERTAIN_ALIAS) return this.refuseRouteUnavailable(request);
|
|
4910
|
+
// #4997: a reply whose ask's route may be on a broker that is down is refused as retryable here too, not by
|
|
4911
|
+
// the local path's final refusal just because the outbox lookup was slow.
|
|
4912
|
+
if (await this.routeUnreachable(adopted, target, decisionStartedAt)) return;
|
|
4913
|
+
sending = adopted;
|
|
4914
|
+
}
|
|
4915
|
+
await this.waitForEarlierLocal(ticket, target, expired, boundExpired);
|
|
4916
|
+
await this.waitForPendingOutboxScan(ticket.senderKey, targetIdentity, expired);
|
|
4917
|
+
this.extendBeforeLocalReceiptWait(sending, target);
|
|
4918
|
+
await this.commitLocalNotFound(sending, ticket, openingDeadlineAt);
|
|
3954
4919
|
return;
|
|
3955
4920
|
}
|
|
4921
|
+
this.markRoutingProgress(request);
|
|
3956
4922
|
if (existing.status === "found") {
|
|
3957
4923
|
const digest = crossNodeDigest(request.signature);
|
|
3958
4924
|
const recipient = await this.rowRecipient(existing.row, target, expired);
|
|
4925
|
+
await this.waitForEarlierLocal(ticket, target, expired, boundExpired);
|
|
4926
|
+
if (this.closed) return;
|
|
4927
|
+
if (existing.row.state === "delivered" || existing.row.state === "failed") {
|
|
4928
|
+
// Already settled earlier: this retry writes no frame to target.socket, so release its order ticket
|
|
4929
|
+
// before post-settlement store writes (`sentMessages.accept`) so later local sends never wait on them.
|
|
4930
|
+
this.sendOrder.finish(ticket);
|
|
4931
|
+
await this.answerExistingRow(request, recipient, digest, existing.row);
|
|
4932
|
+
return;
|
|
4933
|
+
}
|
|
4934
|
+
this.sendOrder.markUnbounded(ticket);
|
|
3959
4935
|
await this.answerExistingRow(request, recipient, digest, existing.row);
|
|
4936
|
+
if (recipient.hostBrokerId === this.brokerId) {
|
|
4937
|
+
await Promise.race([this.scanIdentity(recipient.identity), expired]);
|
|
4938
|
+
}
|
|
3960
4939
|
return;
|
|
3961
4940
|
}
|
|
3962
4941
|
const moved = await Promise.race([released, expired]);
|
|
3963
|
-
if (moved !== "expired" &&
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
if (
|
|
3967
|
-
|
|
3968
|
-
|
|
4942
|
+
if (moved !== "expired" && isReleasedReplyCandidate(request) && !this.routedHere(request, target)) {
|
|
4943
|
+
this.markRoutingProgress(request);
|
|
4944
|
+
}
|
|
4945
|
+
if (moved !== "expired" && moved !== undefined) {
|
|
4946
|
+
this.sendOrder.markUnbounded(ticket);
|
|
4947
|
+
if (await this.routeReleasedReply(request, moved, ticket)) return;
|
|
4948
|
+
}
|
|
4949
|
+
let sending = request;
|
|
4950
|
+
if (request.message.replyTo !== undefined) {
|
|
4951
|
+
if (!this.routedHere(request, target)) this.sendOrder.markUnbounded(ticket);
|
|
4952
|
+
const decisionStartedAt = performance.now();
|
|
4953
|
+
const adopted = await this.adoptSharedRouteWithin(request, target);
|
|
4954
|
+
if (adopted === UNCERTAIN_ALIAS) return this.refuseRouteUnavailable(request);
|
|
4955
|
+
if (await this.routeUnreachable(adopted, target, decisionStartedAt)) return;
|
|
4956
|
+
sending = adopted;
|
|
4957
|
+
}
|
|
4958
|
+
await this.waitForEarlierLocal(ticket, target, expired, boundExpired);
|
|
4959
|
+
await this.waitForPendingOutboxScan(ticket.senderKey, targetIdentity, expired);
|
|
4960
|
+
this.extendBeforeLocalReceiptWait(sending, target);
|
|
4961
|
+
await this.commitLocalNotFound(sending, ticket, openingDeadlineAt);
|
|
3969
4962
|
} finally {
|
|
3970
4963
|
clearTimeout(bound);
|
|
3971
4964
|
}
|
|
@@ -4031,7 +5024,7 @@ export class MultiNodeRouter {
|
|
|
4031
5024
|
(await this.holdsSessionId(request.target, byAlias.atomicSessionId)) &&
|
|
4032
5025
|
(await this.aliasStillAnswers(byAlias, request.target))
|
|
4033
5026
|
)
|
|
4034
|
-
return { ...byAlias, intercomSessionId: request.target };
|
|
5027
|
+
return { ...byAlias, intercomSessionId: request.target, aliasSessionId: byAlias.intercomSessionId };
|
|
4035
5028
|
const replyAuthorized = await this.replyAuthorizedTargets(
|
|
4036
5029
|
request,
|
|
4037
5030
|
sessions.map((session) => session.intercomSessionId),
|
|
@@ -4077,7 +5070,14 @@ export class MultiNodeRouter {
|
|
|
4077
5070
|
}
|
|
4078
5071
|
|
|
4079
5072
|
/** Run the origin's routing decisions on the shared state, then enqueue the exact local frame. */
|
|
4080
|
-
private async deliverThroughOutbox(
|
|
5073
|
+
private async deliverThroughOutbox(
|
|
5074
|
+
request: RemoteSendRequest,
|
|
5075
|
+
target: RemoteTarget,
|
|
5076
|
+
ticket: SendOrderTicket,
|
|
5077
|
+
openingDeadlineAt: number,
|
|
5078
|
+
targetMatch?: TargetMatchInfo,
|
|
5079
|
+
): Promise<void> {
|
|
5080
|
+
this.sendOrder.resolveTarget(ticket, target.identity);
|
|
4081
5081
|
const { message } = request;
|
|
4082
5082
|
if (!isAgentRecipient(target.gates)) {
|
|
4083
5083
|
this.fail(request, NON_AGENT_RECIPIENT_REFUSAL);
|
|
@@ -4089,15 +5089,44 @@ export class MultiNodeRouter {
|
|
|
4089
5089
|
}
|
|
4090
5090
|
const digest = crossNodeDigest(request.signature);
|
|
4091
5091
|
const existing = await this.state.outbox.read(message.id);
|
|
5092
|
+
this.markRoutingProgress(request);
|
|
4092
5093
|
if (existing.status === "found") {
|
|
5094
|
+
await this.waitForEarlierRemote(request, ticket, {
|
|
5095
|
+
targetIdentity: target.identity,
|
|
5096
|
+
...(targetMatch === undefined ? {} : { targetMatch }),
|
|
5097
|
+
});
|
|
5098
|
+
if (this.closed) return;
|
|
5099
|
+
if (existing.row.state === "delivered" || existing.row.state === "failed") {
|
|
5100
|
+
// Already settled earlier: release its order ticket before post-settlement store writes
|
|
5101
|
+
// (`sentMessages.accept`) so later sends to this target never wait on them.
|
|
5102
|
+
this.sendOrder.finish(ticket);
|
|
5103
|
+
}
|
|
4093
5104
|
await this.answerExistingRow(request, target, digest, existing.row);
|
|
4094
5105
|
return;
|
|
4095
5106
|
}
|
|
4096
5107
|
if (this.answerLocalDelivery(request, target)) return;
|
|
4097
5108
|
const answering = await this.correlatedReply(request, target.sessionId);
|
|
5109
|
+
if (message.replyTo !== undefined) this.markRoutingProgress(request);
|
|
4098
5110
|
if (answering === UNCERTAIN_ALIAS) return this.refuseRouteUnavailable(request);
|
|
5111
|
+
await this.waitForEarlierRemote(request, ticket, {
|
|
5112
|
+
targetIdentity: target.identity,
|
|
5113
|
+
...(targetMatch === undefined ? {} : { targetMatch }),
|
|
5114
|
+
});
|
|
5115
|
+
if (this.closed) return;
|
|
4099
5116
|
// #5607: a reply to an ask its sender's alias received goes out as from that alias.
|
|
4100
|
-
await this.enqueueThroughOutbox(answering ?? request, target, digest, answering !== undefined);
|
|
5117
|
+
await this.enqueueThroughOutbox(answering ?? request, target, digest, answering !== undefined, openingDeadlineAt);
|
|
5118
|
+
if (this.localSessionFor(target.identity) !== undefined && !this.closed) {
|
|
5119
|
+
let bound: ReturnType<typeof setTimeout> | undefined;
|
|
5120
|
+
const expired = new Promise<"expired">((resolve) => {
|
|
5121
|
+
bound = setTimeout(() => resolve("expired"), LOCAL_RETRY_LOOKUP_BOUND_MS);
|
|
5122
|
+
bound.unref?.();
|
|
5123
|
+
});
|
|
5124
|
+
try {
|
|
5125
|
+
await Promise.race([this.scanIdentity(target.identity), expired]);
|
|
5126
|
+
} finally {
|
|
5127
|
+
clearTimeout(bound);
|
|
5128
|
+
}
|
|
5129
|
+
}
|
|
4101
5130
|
}
|
|
4102
5131
|
|
|
4103
5132
|
/** `request`, checked by `deliverThroughOutbox`, written to the shared outbox; `correlatedReply`: an open ask's reply. */
|
|
@@ -4106,7 +5135,13 @@ export class MultiNodeRouter {
|
|
|
4106
5135
|
target: RemoteTarget,
|
|
4107
5136
|
digest: string,
|
|
4108
5137
|
correlatedReply: boolean,
|
|
5138
|
+
openingDeadlineAt: number,
|
|
4109
5139
|
): Promise<void> {
|
|
5140
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
5141
|
+
if (progress !== undefined && performance.now() >= progress.startedAt + RECIPIENT_SAVE_MAX_WAIT_MS) {
|
|
5142
|
+
this.fail(request, SHARED_STORAGE_UNAVAILABLE_REASON, request.requirePendingReply ? "session_not_found" : undefined);
|
|
5143
|
+
return;
|
|
5144
|
+
}
|
|
4110
5145
|
const { message, from } = request;
|
|
4111
5146
|
if (request.requirePendingReply && !correlatedReply) {
|
|
4112
5147
|
await this.refuseUnauthorizedReply(request, { sessionId: target.sessionId, identity: target.identity });
|
|
@@ -4116,12 +5151,46 @@ export class MultiNodeRouter {
|
|
|
4116
5151
|
message.replyTo !== undefined &&
|
|
4117
5152
|
this.host.supervisorCache.matchReply(message.replyTo, from.info.id, target.sessionId);
|
|
4118
5153
|
// A session answers a sender outside its reach whose message reached it here, within the return path's bounds.
|
|
4119
|
-
|
|
5154
|
+
let crossesGroups =
|
|
5155
|
+
!request.supervisorSend && !verticalReply && !correlatedReply && !reachesGroups(from, target.groups);
|
|
4120
5156
|
const answered: ReturnPathParty | undefined =
|
|
4121
5157
|
crossesGroups && mayUseReturnPath(message, request.supervisorSend, from)
|
|
4122
5158
|
? { sessionId: target.sessionId, identity: stableIdentity(target.identity) }
|
|
4123
5159
|
: undefined;
|
|
4124
|
-
|
|
5160
|
+
let returnPath =
|
|
5161
|
+
answered !== undefined &&
|
|
5162
|
+
this.host.returnPaths?.use(from, answered, message.id, request.returnPathArrivalSeq) === true;
|
|
5163
|
+
while (
|
|
5164
|
+
!this.closed &&
|
|
5165
|
+
crossesGroups &&
|
|
5166
|
+
!returnPath &&
|
|
5167
|
+
answered !== undefined &&
|
|
5168
|
+
!request.requirePendingReply
|
|
5169
|
+
) {
|
|
5170
|
+
const outcome = await this.awaitReturnPathOpening(
|
|
5171
|
+
from,
|
|
5172
|
+
answered,
|
|
5173
|
+
request.returnPathArrivalSeq,
|
|
5174
|
+
openingDeadlineAt,
|
|
5175
|
+
);
|
|
5176
|
+
if (this.closed) return;
|
|
5177
|
+
if (outcome !== "settled") break;
|
|
5178
|
+
// The sender may have joined the target's group while waiting, so `delivered` skipped opening a return path.
|
|
5179
|
+
crossesGroups =
|
|
5180
|
+
!request.supervisorSend && !verticalReply && !correlatedReply && !reachesGroups(from, target.groups);
|
|
5181
|
+
if (!crossesGroups) break;
|
|
5182
|
+
if (progress !== undefined && performance.now() >= progress.startedAt + RECIPIENT_SAVE_MAX_WAIT_MS) {
|
|
5183
|
+
this.fail(request, SHARED_STORAGE_UNAVAILABLE_REASON, request.requirePendingReply ? "session_not_found" : undefined);
|
|
5184
|
+
return;
|
|
5185
|
+
}
|
|
5186
|
+
returnPath =
|
|
5187
|
+
this.host.returnPaths?.use(from, answered, message.id, request.returnPathArrivalSeq) === true;
|
|
5188
|
+
}
|
|
5189
|
+
if (this.closed) return;
|
|
5190
|
+
if (progress !== undefined && performance.now() >= progress.startedAt + RECIPIENT_SAVE_MAX_WAIT_MS) {
|
|
5191
|
+
this.fail(request, SHARED_STORAGE_UNAVAILABLE_REASON, request.requirePendingReply ? "session_not_found" : undefined);
|
|
5192
|
+
return;
|
|
5193
|
+
}
|
|
4125
5194
|
if (crossesGroups && !returnPath) {
|
|
4126
5195
|
this.fail(
|
|
4127
5196
|
request,
|
|
@@ -4160,6 +5229,7 @@ export class MultiNodeRouter {
|
|
|
4160
5229
|
},
|
|
4161
5230
|
{ targetIdentity: target.identity, originBrokerId: this.brokerId, digest, frame, ...offlineHost(target.hostBrokerId) },
|
|
4162
5231
|
);
|
|
5232
|
+
this.markRoutingProgress(request);
|
|
4163
5233
|
if (enqueued.status === "conflict") {
|
|
4164
5234
|
this.failConflict(request);
|
|
4165
5235
|
return;
|
|
@@ -4179,6 +5249,7 @@ export class MultiNodeRouter {
|
|
|
4179
5249
|
replyTargetSessionId: target.sessionId,
|
|
4180
5250
|
replyTo: message.replyTo!,
|
|
4181
5251
|
});
|
|
5252
|
+
this.markRoutingProgress(request);
|
|
4182
5253
|
}
|
|
4183
5254
|
await this.answerExistingRow(request, target, digest, enqueued.row);
|
|
4184
5255
|
}
|
|
@@ -4277,6 +5348,8 @@ export class MultiNodeRouter {
|
|
|
4277
5348
|
row: OutboxRow,
|
|
4278
5349
|
): Promise<void> {
|
|
4279
5350
|
const { message } = request;
|
|
5351
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
5352
|
+
if (progress !== undefined) this.stopProgress(progress);
|
|
4280
5353
|
if (row.digest !== digest || row.targetIdentity !== target.identity) {
|
|
4281
5354
|
this.failConflict(request);
|
|
4282
5355
|
return;
|
|
@@ -4287,7 +5360,23 @@ export class MultiNodeRouter {
|
|
|
4287
5360
|
}
|
|
4288
5361
|
const asksTarget = message.expectsReply === true && request.resolveReplyTarget && request.attemptId !== undefined;
|
|
4289
5362
|
if (row.state === "delivered") {
|
|
4290
|
-
|
|
5363
|
+
const now = performance.now();
|
|
5364
|
+
const acceptance: SendProgress = {
|
|
5365
|
+
startedAt: progress?.startedAt ?? now,
|
|
5366
|
+
clientDeadlineAt: progress?.clientDeadlineAt ?? now + SEND_RESPONSE_TIMEOUT_MS,
|
|
5367
|
+
hadProgress: true,
|
|
5368
|
+
lastProgressAt: now,
|
|
5369
|
+
reportingEnabled: true,
|
|
5370
|
+
socket: request.socket,
|
|
5371
|
+
messageId: message.id,
|
|
5372
|
+
...(request.attemptId === undefined ? {} : { attemptId: request.attemptId }),
|
|
5373
|
+
};
|
|
5374
|
+
this.scheduleProgressHold(acceptance, now);
|
|
5375
|
+
try {
|
|
5376
|
+
await this.state.sentMessages.accept({ messageId: message.id, digest });
|
|
5377
|
+
} finally {
|
|
5378
|
+
this.stopProgress(acceptance);
|
|
5379
|
+
}
|
|
4291
5380
|
if (asksTarget) await this.writeQuestionTarget(request.socket, message.id, request.attemptId, target);
|
|
4292
5381
|
this.host.write(request.socket, { type: "delivered", messageId: message.id, attemptId: request.attemptId });
|
|
4293
5382
|
return;
|
|
@@ -4295,12 +5384,16 @@ export class MultiNodeRouter {
|
|
|
4295
5384
|
const offline = target.hostBrokerId === undefined;
|
|
4296
5385
|
const previous = this.waiters.get(message.id);
|
|
4297
5386
|
if (previous !== undefined) this.dropWaiter(previous);
|
|
5387
|
+
const now = performance.now();
|
|
5388
|
+
const waitingSince = progress?.startedAt ?? now;
|
|
5389
|
+
const clientDeadlineAt = progress?.clientDeadlineAt ?? now + SEND_RESPONSE_TIMEOUT_MS;
|
|
4298
5390
|
const waiter: Waiter = {
|
|
4299
5391
|
messageId: message.id,
|
|
4300
5392
|
digest,
|
|
4301
5393
|
socket: request.socket,
|
|
4302
5394
|
...(request.attemptId === undefined ? {} : { attemptId: request.attemptId }),
|
|
4303
5395
|
senderSessionId: request.from.info.id,
|
|
5396
|
+
senderKey: deliveryTargetIdentity(request.from),
|
|
4304
5397
|
sender: reservationSender(request.from),
|
|
4305
5398
|
expectsReply: message.expectsReply === true,
|
|
4306
5399
|
resolveReplyTarget: request.resolveReplyTarget,
|
|
@@ -4308,14 +5401,27 @@ export class MultiNodeRouter {
|
|
|
4308
5401
|
targetIdentity: target.identity,
|
|
4309
5402
|
queued: offline,
|
|
4310
5403
|
timer: undefined,
|
|
4311
|
-
waitingSince
|
|
5404
|
+
waitingSince,
|
|
5405
|
+
clientDeadlineAt,
|
|
4312
5406
|
};
|
|
4313
|
-
this.armWaiter(waiter, offline ? DELIVERED_MESSAGE_TTL_MS : RECIPIENT_RECEIPT_TIMEOUT_MS);
|
|
4314
|
-
this.waiters.set(message.id, waiter);
|
|
4315
5407
|
if (offline) {
|
|
5408
|
+
this.armWaiter(waiter, DELIVERED_MESSAGE_TTL_MS);
|
|
5409
|
+
this.setWaiter(waiter);
|
|
4316
5410
|
await this.writeQueued(request.socket, message.id, request.attemptId, target.identity);
|
|
4317
5411
|
return;
|
|
4318
5412
|
}
|
|
5413
|
+
const remaining = this.capRemaining(waiter);
|
|
5414
|
+
if (remaining === 0) {
|
|
5415
|
+
this.setWaiter(waiter);
|
|
5416
|
+
this.expireUncertain(waiter);
|
|
5417
|
+
return;
|
|
5418
|
+
}
|
|
5419
|
+
const receiptWaitMs = Math.min(RECIPIENT_RECEIPT_TIMEOUT_MS, remaining);
|
|
5420
|
+
this.armWaiter(waiter, receiptWaitMs);
|
|
5421
|
+
this.setWaiter(waiter);
|
|
5422
|
+
if (waiter.clientDeadlineAt - (now + receiptWaitMs) < MIN_WAIT_CLIENT_MARGIN_MS) {
|
|
5423
|
+
this.holdSender(waiter, receiptWaitMs);
|
|
5424
|
+
}
|
|
4319
5425
|
if (asksTarget) await this.writeQuestionTarget(request.socket, message.id, request.attemptId, target);
|
|
4320
5426
|
await this.bus.publish({ brokerId: target.hostBrokerId!, reason: "outbox", version: row.sequence });
|
|
4321
5427
|
// A clean quit recorded before this send's row was written is seen here; one recorded after it wakes this
|
|
@@ -4361,7 +5467,7 @@ export class MultiNodeRouter {
|
|
|
4361
5467
|
this.dropWaiter(waiter);
|
|
4362
5468
|
const queued: Waiter = { ...waiter, queued: true, timer: undefined, capTimer: undefined, holdCheck: undefined };
|
|
4363
5469
|
this.armWaiter(queued, DELIVERED_MESSAGE_TTL_MS);
|
|
4364
|
-
this.
|
|
5470
|
+
this.setWaiter(queued);
|
|
4365
5471
|
this.host.write(queued.socket, queuedFrame(queued.messageId, queued.attemptId, position));
|
|
4366
5472
|
void this.wakeReleasedRecipient(queued).catch((error: Error) => this.report(error));
|
|
4367
5473
|
return true;
|
|
@@ -4422,7 +5528,7 @@ export class MultiNodeRouter {
|
|
|
4422
5528
|
}
|
|
4423
5529
|
|
|
4424
5530
|
/** §5.7: a reservation shared by another broker holds this send here, at its origin. */
|
|
4425
|
-
private async holdForRemoteReservation(request: RemoteSendRequest): Promise<boolean> {
|
|
5531
|
+
private async holdForRemoteReservation(request: RemoteSendRequest, ticket: SendOrderTicket): Promise<boolean> {
|
|
4426
5532
|
const { from, message } = request;
|
|
4427
5533
|
const lookup = await this.state.reservations.lookup(
|
|
4428
5534
|
normalizeTarget(request.target),
|
|
@@ -4435,7 +5541,16 @@ export class MultiNodeRouter {
|
|
|
4435
5541
|
this.fail(request, reservation.gone ?? restoringRecipientRetryReason(reservation.childName));
|
|
4436
5542
|
return true;
|
|
4437
5543
|
}
|
|
5544
|
+
this.markRoutingProgress(request);
|
|
5545
|
+
const reservationTarget = `reservation:${reservation.reservationKey}`;
|
|
5546
|
+
const reservationNames = [reservation.childName, ...reservation.targets];
|
|
5547
|
+
await this.waitForEarlierRemote(request, ticket, {
|
|
5548
|
+
targetIdentity: reservationTarget,
|
|
5549
|
+
targetMatch: { names: reservationNames },
|
|
5550
|
+
});
|
|
5551
|
+
if (this.closed) return true;
|
|
4438
5552
|
const listed = await this.state.heldSends.list(reservation.reservationKey);
|
|
5553
|
+
this.markRoutingProgress(request);
|
|
4439
5554
|
const count = listed.status === "listed" ? listed.sends.length : 0;
|
|
4440
5555
|
const reasonCode = unrestoredReasonCode(reservation.reservationKey, reservation.wake);
|
|
4441
5556
|
if (!request.awaitRestoringRecipient || count >= MAX_HELD_SENDS) {
|
|
@@ -4452,12 +5567,14 @@ export class MultiNodeRouter {
|
|
|
4452
5567
|
if (result.status === "duplicate" && (await this.state.heldSends.drop(message.id)).status === "dropped") {
|
|
4453
5568
|
result = await this.state.heldSends.hold(held);
|
|
4454
5569
|
}
|
|
5570
|
+
this.markRoutingProgress(request);
|
|
4455
5571
|
if (result.status !== "held") return true;
|
|
4456
5572
|
// #4488: a released child is parked while its owner releases it, then made wakeable, and its owner's broker looks
|
|
4457
5573
|
// for held sends as it makes it wakeable. When that look came before this send was written and this send found it
|
|
4458
5574
|
// still parked, only a read made now that the send is written sees it wakeable: one of the two asks for the wake.
|
|
4459
5575
|
const current =
|
|
4460
5576
|
reservation.wake || count > 0 ? reservation : await this.activeReservation(reservation.reservationKey);
|
|
5577
|
+
if (!reservation.wake && count === 0) this.markRoutingProgress(request);
|
|
4461
5578
|
const wake = current?.wake === true;
|
|
4462
5579
|
const previous = this.holds.get(message.id);
|
|
4463
5580
|
if (previous !== undefined) clearTimeout(previous.timer);
|
|
@@ -4474,6 +5591,13 @@ export class MultiNodeRouter {
|
|
|
4474
5591
|
reasonCode: wake ? unrestoredReasonCode(reservation.reservationKey, true) : reasonCode,
|
|
4475
5592
|
timer,
|
|
4476
5593
|
});
|
|
5594
|
+
// Stop routing progress reporting before writing the 20 s hold so no 8 s routing hold can shrink it during `requestWake`.
|
|
5595
|
+
const progress = this.sendProgress.get(request.clientMessage);
|
|
5596
|
+
if (progress !== undefined) {
|
|
5597
|
+
this.stopProgress(progress);
|
|
5598
|
+
const holdDeadline = performance.now() + RESTORING_RECIPIENT_HOLD_MS + HELD_SEND_GRACE_MS;
|
|
5599
|
+
if (holdDeadline > progress.clientDeadlineAt) progress.clientDeadlineAt = holdDeadline;
|
|
5600
|
+
}
|
|
4477
5601
|
this.host.write(request.socket, {
|
|
4478
5602
|
type: "send_held",
|
|
4479
5603
|
messageId: message.id,
|
|
@@ -4515,51 +5639,70 @@ export class MultiNodeRouter {
|
|
|
4515
5639
|
|
|
4516
5640
|
/** Read the reservation of every send held here and dispatch or refuse the ones whose hold ended (§5.7). */
|
|
4517
5641
|
private async checkHolds(): Promise<void> {
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4521
|
-
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
key =
|
|
4529
|
-
|
|
4530
|
-
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
5642
|
+
if (this.checkingHolds !== undefined) {
|
|
5643
|
+
this.checkingHolds.again = true;
|
|
5644
|
+
return;
|
|
5645
|
+
}
|
|
5646
|
+
const state = { again: false };
|
|
5647
|
+
this.checkingHolds = state;
|
|
5648
|
+
try {
|
|
5649
|
+
do {
|
|
5650
|
+
state.again = false;
|
|
5651
|
+
for (const hold of [...this.holds.values()]) {
|
|
5652
|
+
let key = hold.reservationKey;
|
|
5653
|
+
let decision:
|
|
5654
|
+
| { readonly kind: "keep" }
|
|
5655
|
+
| { readonly kind: "refuse"; readonly reason: string }
|
|
5656
|
+
| { readonly kind: "dispatch" } = {
|
|
5657
|
+
kind: "keep",
|
|
5658
|
+
};
|
|
5659
|
+
for (let hop = 0; hop < MAX_SUPERSEDED_HOPS; hop++) {
|
|
5660
|
+
const read = await this.state.reservations.read(key);
|
|
5661
|
+
if (read.status === "active") {
|
|
5662
|
+
decision =
|
|
5663
|
+
read.reservation.gone === undefined ? { kind: "keep" } : { kind: "refuse", reason: read.reservation.gone };
|
|
5664
|
+
} else if (read.status === "ended" && read.end.outcome === "superseded") {
|
|
5665
|
+
key = read.end.by;
|
|
5666
|
+
continue;
|
|
5667
|
+
} else if (read.status === "ended" && read.end.outcome === "released" && read.end.refusal !== undefined) {
|
|
5668
|
+
decision = { kind: "refuse", reason: read.end.refusal };
|
|
5669
|
+
} else if (read.status !== "invalid") {
|
|
5670
|
+
decision = { kind: "dispatch" };
|
|
5671
|
+
}
|
|
5672
|
+
break;
|
|
5673
|
+
}
|
|
5674
|
+
if (decision.kind === "keep" || this.holds.get(hold.messageId) !== hold) continue;
|
|
5675
|
+
this.holds.delete(hold.messageId);
|
|
5676
|
+
clearTimeout(hold.timer);
|
|
5677
|
+
if ((await this.state.heldSends.drop(hold.messageId)).status !== "dropped") continue;
|
|
5678
|
+
if (decision.kind === "refuse") {
|
|
5679
|
+
this.writeHoldFailure(hold, decision.reason);
|
|
5680
|
+
continue;
|
|
5681
|
+
}
|
|
5682
|
+
const sender = this.host.sessions.get(hold.senderId);
|
|
5683
|
+
if (sender?.socket === hold.socket) this.host.dispatch(hold.socket, hold.clientMessage, hold.senderId);
|
|
4534
5684
|
}
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
|
|
4540
|
-
if ((await this.state.heldSends.drop(hold.messageId)).status !== "dropped") continue;
|
|
4541
|
-
if (decision.kind === "refuse") {
|
|
4542
|
-
this.writeHoldFailure(hold, decision.reason);
|
|
4543
|
-
continue;
|
|
4544
|
-
}
|
|
4545
|
-
const sender = this.host.sessions.get(hold.senderId);
|
|
4546
|
-
if (sender?.socket === hold.socket) this.host.dispatch(hold.socket, hold.clientMessage, hold.senderId);
|
|
5685
|
+
} while (state.again && !this.closed);
|
|
5686
|
+
} catch (error) {
|
|
5687
|
+
this.report(error);
|
|
5688
|
+
} finally {
|
|
5689
|
+
this.checkingHolds = undefined;
|
|
4547
5690
|
}
|
|
4548
5691
|
}
|
|
4549
5692
|
|
|
4550
5693
|
// ---------------------------------------------------------------------------------------------------------
|
|
4551
5694
|
// Delivering the shared outbox to this broker's sessions
|
|
4552
5695
|
|
|
4553
|
-
private scanIdentity(identity: string): void {
|
|
4554
|
-
if (this.closed) return;
|
|
5696
|
+
private scanIdentity(identity: string): Promise<void> {
|
|
5697
|
+
if (this.closed) return Promise.resolve();
|
|
4555
5698
|
const running = this.scanning.get(identity);
|
|
4556
5699
|
if (running !== undefined) {
|
|
4557
5700
|
running.rescan = true;
|
|
4558
|
-
return;
|
|
5701
|
+
return running.done;
|
|
4559
5702
|
}
|
|
4560
|
-
const state = { rescan: false };
|
|
5703
|
+
const state: { rescan: boolean; done: Promise<void> } = { rescan: false, done: Promise.resolve() };
|
|
4561
5704
|
this.scanning.set(identity, state);
|
|
4562
|
-
|
|
5705
|
+
const done = (async () => {
|
|
4563
5706
|
try {
|
|
4564
5707
|
do {
|
|
4565
5708
|
state.rescan = false;
|
|
@@ -4568,9 +5711,11 @@ export class MultiNodeRouter {
|
|
|
4568
5711
|
} catch (error) {
|
|
4569
5712
|
this.report(error);
|
|
4570
5713
|
} finally {
|
|
4571
|
-
this.scanning.delete(identity);
|
|
5714
|
+
if (this.scanning.get(identity) === state) this.scanning.delete(identity);
|
|
4572
5715
|
}
|
|
4573
5716
|
})();
|
|
5717
|
+
state.done = done;
|
|
5718
|
+
return done;
|
|
4574
5719
|
}
|
|
4575
5720
|
|
|
4576
5721
|
private scanAll(): void {
|
|
@@ -4604,15 +5749,20 @@ export class MultiNodeRouter {
|
|
|
4604
5749
|
}
|
|
4605
5750
|
this.heldIdentities.delete(identity);
|
|
4606
5751
|
if (claimed.status !== "claimed") return;
|
|
4607
|
-
for (
|
|
5752
|
+
for (let index = 0; index < claimed.rows.length; index++) {
|
|
5753
|
+
const { row, claim } = claimed.rows[index]!;
|
|
4608
5754
|
const session = this.localSessionFor(identity);
|
|
4609
|
-
if (session === undefined) {
|
|
4610
|
-
// The recipient left
|
|
4611
|
-
//
|
|
4612
|
-
//
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
5755
|
+
if (session === undefined || session !== recipient) {
|
|
5756
|
+
// The recipient left or was replaced while this batch was being delivered: release this row and every
|
|
5757
|
+
// later row in the batch unwritten so a reconnected session never receives a later row ahead of an earlier
|
|
5758
|
+
// released row.
|
|
5759
|
+
for (let remaining = index; remaining < claimed.rows.length; remaining++) {
|
|
5760
|
+
const pending = claimed.rows[remaining]!;
|
|
5761
|
+
const released = await this.state.outbox.releaseClaim(pending.claim, { unwritten: true });
|
|
5762
|
+
if (released.status === "released") await this.wakeOrigin(pending.row);
|
|
5763
|
+
}
|
|
5764
|
+
if (this.localSessionFor(identity) !== undefined) void this.scanIdentity(identity);
|
|
5765
|
+
return;
|
|
4616
5766
|
}
|
|
4617
5767
|
if (row.frame.type === "released_child_reply") {
|
|
4618
5768
|
const handoffRefusal = await releasedReplyHandoffRefusal(this.state, row.frame);
|
|
@@ -4631,20 +5781,40 @@ export class MultiNodeRouter {
|
|
|
4631
5781
|
if (failed.status === "failed") await this.wakeOrigin(row);
|
|
4632
5782
|
continue;
|
|
4633
5783
|
}
|
|
5784
|
+
const handedOverMark = this.host.returnPaths?.mark();
|
|
4634
5785
|
const handedOverAt = this.firstHandedOverAt.get(row.messageId) ?? this.host.returnPaths?.handOverTime();
|
|
5786
|
+
const finishOpening =
|
|
5787
|
+
session.acknowledgesReceipt === true || handedOverAt === undefined
|
|
5788
|
+
? undefined
|
|
5789
|
+
: this.trackOpening(
|
|
5790
|
+
session,
|
|
5791
|
+
this.host.returnPaths?.beginOpening(
|
|
5792
|
+
session,
|
|
5793
|
+
{ sessionId: frame.from.id, identity: frame.senderIdentity },
|
|
5794
|
+
frame.from,
|
|
5795
|
+
handedOverAt,
|
|
5796
|
+
),
|
|
5797
|
+
);
|
|
4635
5798
|
const inflight: Inflight = {
|
|
4636
5799
|
claim,
|
|
4637
5800
|
row,
|
|
4638
5801
|
session,
|
|
4639
5802
|
identity,
|
|
4640
5803
|
...(handedOverAt === undefined ? {} : { handedOverAt }),
|
|
5804
|
+
...(handedOverMark === undefined ? {} : { handedOverMark }),
|
|
5805
|
+
...(finishOpening === undefined ? {} : { finishOpening }),
|
|
4641
5806
|
};
|
|
5807
|
+
this.inflight.get(row.messageId)?.finishOpening?.();
|
|
4642
5808
|
this.inflight.set(row.messageId, inflight);
|
|
4643
5809
|
const delivery = messageFrameFor(session, frame.from, frame.message, frame.channel);
|
|
4644
5810
|
this.host.writeConfirmed(session.socket, delivery, (written) => {
|
|
4645
|
-
if (this.inflight.get(row.messageId) !== inflight)
|
|
5811
|
+
if (this.inflight.get(row.messageId) !== inflight) {
|
|
5812
|
+
inflight.finishOpening?.();
|
|
5813
|
+
return;
|
|
5814
|
+
}
|
|
4646
5815
|
if (!written) {
|
|
4647
5816
|
this.inflight.delete(row.messageId);
|
|
5817
|
+
inflight.finishOpening?.();
|
|
4648
5818
|
void this.state.outbox.releaseClaim(claim).catch((error: Error) => this.report(error));
|
|
4649
5819
|
return;
|
|
4650
5820
|
}
|
|
@@ -4706,46 +5876,65 @@ export class MultiNodeRouter {
|
|
|
4706
5876
|
private settleDelivered(inflight: Inflight, recipient: BrokerConnectedSession, frame: IntercomDeliveryFrame): void {
|
|
4707
5877
|
const { messageId } = inflight.row;
|
|
4708
5878
|
this.inflight.delete(messageId);
|
|
5879
|
+
const finishOpening =
|
|
5880
|
+
inflight.finishOpening ??
|
|
5881
|
+
(inflight.handedOverAt === undefined
|
|
5882
|
+
? undefined
|
|
5883
|
+
: this.trackOpening(
|
|
5884
|
+
recipient,
|
|
5885
|
+
this.host.returnPaths?.beginOpening(
|
|
5886
|
+
recipient,
|
|
5887
|
+
{ sessionId: frame.from.id, identity: frame.senderIdentity },
|
|
5888
|
+
frame.from,
|
|
5889
|
+
inflight.handedOverAt,
|
|
5890
|
+
),
|
|
5891
|
+
));
|
|
4709
5892
|
let marked = false;
|
|
4710
5893
|
const settling = (async () => {
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4716
|
-
|
|
4717
|
-
|
|
4718
|
-
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
5894
|
+
try {
|
|
5895
|
+
// Route first (spec §5.4): a settled row is never claimed again, so a route written after `markDelivered`
|
|
5896
|
+
// would be lost if this broker stopped between the writes. Redelivery repeats the route write safely.
|
|
5897
|
+
if (frame.message.expectsReply === true) {
|
|
5898
|
+
const asker = this.host.sessions.get(frame.from.id);
|
|
5899
|
+
// The asker is here too: the reply takes the local path, which answers only local routes.
|
|
5900
|
+
if (asker !== undefined) this.host.pendingQuestions.record(frame.from.id, recipient.info.id, frame.message.id);
|
|
5901
|
+
else {
|
|
5902
|
+
await this.state.pendingAsks.record({
|
|
5903
|
+
senderSessionId: frame.from.id,
|
|
5904
|
+
targetSessionId: recipient.info.id,
|
|
5905
|
+
messageId: frame.message.id,
|
|
5906
|
+
});
|
|
5907
|
+
}
|
|
5908
|
+
}
|
|
5909
|
+
const delivered = await this.state.outbox.markDelivered(inflight.claim);
|
|
5910
|
+
this.storeAnswered();
|
|
5911
|
+
marked = true;
|
|
5912
|
+
this.firstHandedOverAt.delete(messageId);
|
|
5913
|
+
if (delivered.status !== "delivered") return;
|
|
5914
|
+
if (frame.message.replyTo !== undefined) {
|
|
5915
|
+
await this.state.pendingAsks.clearReply({
|
|
5916
|
+
replySenderSessionId: frame.from.id,
|
|
5917
|
+
replyTargetSessionId: recipient.info.id,
|
|
5918
|
+
replyTo: frame.message.replyTo,
|
|
4722
5919
|
});
|
|
4723
5920
|
}
|
|
5921
|
+
if (frame.channel === "supervisor") {
|
|
5922
|
+
this.host.supervisorCache.record(frame.message.id, frame.from.id, recipient.info.id);
|
|
5923
|
+
}
|
|
5924
|
+
// The sender's identity is the one its origin broker wrote; a frame from an older broker carries none.
|
|
5925
|
+
this.host.returnPaths?.delivered(
|
|
5926
|
+
recipient,
|
|
5927
|
+
{ sessionId: frame.from.id, identity: frame.senderIdentity },
|
|
5928
|
+
frame.from,
|
|
5929
|
+
frame.message.id,
|
|
5930
|
+
inflight.handedOverMark,
|
|
5931
|
+
inflight.handedOverAt,
|
|
5932
|
+
);
|
|
5933
|
+
finishOpening?.();
|
|
5934
|
+
await this.wakeOrigin(inflight.row);
|
|
5935
|
+
} finally {
|
|
5936
|
+
finishOpening?.();
|
|
4724
5937
|
}
|
|
4725
|
-
const delivered = await this.state.outbox.markDelivered(inflight.claim);
|
|
4726
|
-
marked = true;
|
|
4727
|
-
this.firstHandedOverAt.delete(messageId);
|
|
4728
|
-
if (delivered.status !== "delivered") return;
|
|
4729
|
-
if (frame.message.replyTo !== undefined) {
|
|
4730
|
-
await this.state.pendingAsks.clearReply({
|
|
4731
|
-
replySenderSessionId: frame.from.id,
|
|
4732
|
-
replyTargetSessionId: recipient.info.id,
|
|
4733
|
-
replyTo: frame.message.replyTo,
|
|
4734
|
-
});
|
|
4735
|
-
}
|
|
4736
|
-
if (frame.channel === "supervisor") {
|
|
4737
|
-
this.host.supervisorCache.record(frame.message.id, frame.from.id, recipient.info.id);
|
|
4738
|
-
}
|
|
4739
|
-
// The sender's identity is the one its origin broker wrote; a frame from an older broker carries none.
|
|
4740
|
-
this.host.returnPaths?.delivered(
|
|
4741
|
-
recipient,
|
|
4742
|
-
{ sessionId: frame.from.id, identity: frame.senderIdentity },
|
|
4743
|
-
frame.from,
|
|
4744
|
-
frame.message.id,
|
|
4745
|
-
undefined,
|
|
4746
|
-
inflight.handedOverAt,
|
|
4747
|
-
);
|
|
4748
|
-
await this.wakeOrigin(inflight.row);
|
|
4749
5938
|
})().catch((error: Error) => {
|
|
4750
5939
|
this.report(error);
|
|
4751
5940
|
// Not settled: hand the row back so it is delivered again (the recipient deduplicates) and settled then, with
|
|
@@ -4816,19 +6005,41 @@ export class MultiNodeRouter {
|
|
|
4816
6005
|
|
|
4817
6006
|
private async finishDelivered(waiter: Waiter): Promise<void> {
|
|
4818
6007
|
this.dropWaiter(waiter);
|
|
4819
|
-
|
|
4820
|
-
|
|
4821
|
-
|
|
4822
|
-
|
|
4823
|
-
|
|
4824
|
-
|
|
4825
|
-
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
|
|
6008
|
+
const now = performance.now();
|
|
6009
|
+
const acceptance: SendProgress = {
|
|
6010
|
+
startedAt: waiter.waitingSince,
|
|
6011
|
+
clientDeadlineAt: waiter.clientDeadlineAt,
|
|
6012
|
+
hadProgress: true,
|
|
6013
|
+
lastProgressAt: now,
|
|
6014
|
+
reportingEnabled: true,
|
|
6015
|
+
socket: waiter.socket,
|
|
6016
|
+
messageId: waiter.messageId,
|
|
6017
|
+
...(waiter.attemptId === undefined ? {} : { attemptId: waiter.attemptId }),
|
|
6018
|
+
};
|
|
6019
|
+
this.scheduleProgressHold(acceptance, now);
|
|
6020
|
+
try {
|
|
6021
|
+
await this.state.sentMessages.accept({ messageId: waiter.messageId, digest: waiter.digest });
|
|
6022
|
+
const afterAccept = performance.now();
|
|
6023
|
+
acceptance.lastProgressAt = afterAccept;
|
|
6024
|
+
this.scheduleProgressHold(acceptance, afterAccept);
|
|
6025
|
+
if (waiter.expectsReply && waiter.resolveReplyTarget && waiter.attemptId !== undefined) {
|
|
6026
|
+
// #3777: a recipient that reconnected before confirming answers under its new session id.
|
|
6027
|
+
const current = await this.state.leases.read({ kind: "presence", targetIdentity: waiter.targetIdentity });
|
|
6028
|
+
const afterLease = performance.now();
|
|
6029
|
+
acceptance.lastProgressAt = afterLease;
|
|
6030
|
+
this.scheduleProgressHold(acceptance, afterLease);
|
|
6031
|
+
if (
|
|
6032
|
+
current.status === "found" &&
|
|
6033
|
+
current.lease.subject.kind === "presence" &&
|
|
6034
|
+
current.lease.subject.session.id !== waiter.targetSessionId
|
|
6035
|
+
) {
|
|
6036
|
+
await this.writeQuestionTarget(waiter.socket, waiter.messageId, waiter.attemptId, {
|
|
6037
|
+
sessionId: current.lease.subject.session.id,
|
|
6038
|
+
});
|
|
6039
|
+
}
|
|
4831
6040
|
}
|
|
6041
|
+
} finally {
|
|
6042
|
+
this.stopProgress(acceptance);
|
|
4832
6043
|
}
|
|
4833
6044
|
this.host.write(waiter.socket, { type: "delivered", messageId: waiter.messageId, attemptId: waiter.attemptId });
|
|
4834
6045
|
}
|
|
@@ -4934,12 +6145,7 @@ export class MultiNodeRouter {
|
|
|
4934
6145
|
|
|
4935
6146
|
private holdSender(waiter: Waiter, timeoutMs: number): void {
|
|
4936
6147
|
if (this.waiters.get(waiter.messageId) !== waiter) return;
|
|
4937
|
-
this.
|
|
4938
|
-
type: "send_held",
|
|
4939
|
-
messageId: waiter.messageId,
|
|
4940
|
-
...(waiter.attemptId === undefined ? {} : { attemptId: waiter.attemptId }),
|
|
4941
|
-
timeoutMs,
|
|
4942
|
-
});
|
|
6148
|
+
this.sendHoldFrame(waiter.socket, waiter.messageId, waiter.attemptId, timeoutMs, waiter);
|
|
4943
6149
|
}
|
|
4944
6150
|
|
|
4945
6151
|
private expireUncertain(waiter: Waiter): void {
|
|
@@ -4953,8 +6159,22 @@ export class MultiNodeRouter {
|
|
|
4953
6159
|
});
|
|
4954
6160
|
}
|
|
4955
6161
|
|
|
6162
|
+
private setWaiter(waiter: Waiter): void {
|
|
6163
|
+
const previous = this.waiters.get(waiter.messageId);
|
|
6164
|
+
if (previous !== undefined) this.dropWaiter(previous);
|
|
6165
|
+
this.waiters.set(waiter.messageId, waiter);
|
|
6166
|
+
const pair = outboxPairKey(waiter.senderKey, waiter.targetIdentity);
|
|
6167
|
+
this.unsettledOutboxPairs.set(pair, (this.unsettledOutboxPairs.get(pair) ?? 0) + 1);
|
|
6168
|
+
}
|
|
6169
|
+
|
|
4956
6170
|
private dropWaiter(waiter: Waiter): void {
|
|
4957
|
-
if (this.waiters.get(waiter.messageId) === waiter)
|
|
6171
|
+
if (this.waiters.get(waiter.messageId) === waiter) {
|
|
6172
|
+
this.waiters.delete(waiter.messageId);
|
|
6173
|
+
const pair = outboxPairKey(waiter.senderKey, waiter.targetIdentity);
|
|
6174
|
+
const next = (this.unsettledOutboxPairs.get(pair) ?? 0) - 1;
|
|
6175
|
+
if (next <= 0) this.unsettledOutboxPairs.delete(pair);
|
|
6176
|
+
else this.unsettledOutboxPairs.set(pair, next);
|
|
6177
|
+
}
|
|
4958
6178
|
if (waiter.timer !== undefined) clearTimeout(waiter.timer);
|
|
4959
6179
|
if (waiter.capTimer !== undefined) clearTimeout(waiter.capTimer);
|
|
4960
6180
|
}
|
|
@@ -5032,12 +6252,73 @@ export class MultiNodeRouter {
|
|
|
5032
6252
|
}
|
|
5033
6253
|
|
|
5034
6254
|
private async renew(): Promise<void> {
|
|
5035
|
-
|
|
5036
|
-
|
|
5037
|
-
|
|
5038
|
-
|
|
6255
|
+
// Batch membership and all live presence lease renewals into one shared-store transaction when `renewMany` is
|
|
6256
|
+
// available (1 fsync instead of N+1 sequential fsyncs on SQLite), falling back to concurrent `allSettled` renew
|
|
6257
|
+
// calls on custom stores so a single presence rejection never discards fulfilled renewals or membership
|
|
6258
|
+
// reacquisition. Each callback guards against a newer lease replacing the captured one while renewal is in flight
|
|
6259
|
+
// and preserves per-lease `entry.unfinished` retry parity even when a presence lease returns stale.
|
|
6260
|
+
const membership = this.membership;
|
|
6261
|
+
const batch: LeaseRenewalRequest[] = [];
|
|
6262
|
+
const handlers: Array<(renewed: LeaseRenewResult) => void> = [];
|
|
6263
|
+
const hasMembership = membership !== undefined;
|
|
6264
|
+
if (membership !== undefined) {
|
|
6265
|
+
batch.push({ lease: membership });
|
|
6266
|
+
handlers.push((renewed) => {
|
|
6267
|
+
this.storeAnswered();
|
|
6268
|
+
if (this.membership === membership && renewed.status !== "renewed") this.membership = undefined;
|
|
6269
|
+
});
|
|
6270
|
+
}
|
|
6271
|
+
const missingPresence: Array<readonly [BrokerConnectedSession, PresenceEntry]> = [];
|
|
6272
|
+
for (const [session, entry] of this.presence) {
|
|
6273
|
+
const lease = entry.lease;
|
|
6274
|
+
if (lease !== undefined) {
|
|
6275
|
+
batch.push({ lease, subject: this.presenceSubject(session) });
|
|
6276
|
+
handlers.push((renewed) => {
|
|
6277
|
+
if (this.presence.get(session) !== entry || entry.lease !== lease) return;
|
|
6278
|
+
if (renewed.status !== "renewed") {
|
|
6279
|
+
entry.lease = undefined;
|
|
6280
|
+
} else if (entry.knownAt === undefined || Date.now() - entry.knownAt >= KNOWN_SESSION_REFRESH_INTERVAL_MS) {
|
|
6281
|
+
// A live session stays known: retention counts from its last renewal, not from its registration.
|
|
6282
|
+
this.serial(deliveryTargetIdentity(session), () => this.recordKnown(session, entry));
|
|
6283
|
+
}
|
|
6284
|
+
if (entry.unfinished !== undefined) {
|
|
6285
|
+
this.serial(deliveryTargetIdentity(session), async () => {
|
|
6286
|
+
if (this.presence.get(session) === entry) await this.finishRegistration(session, entry);
|
|
6287
|
+
});
|
|
6288
|
+
}
|
|
6289
|
+
});
|
|
6290
|
+
} else {
|
|
6291
|
+
missingPresence.push([session, entry]);
|
|
6292
|
+
}
|
|
6293
|
+
}
|
|
6294
|
+
let hasError = false;
|
|
6295
|
+
let firstError: unknown;
|
|
6296
|
+
let membershipRejected = false;
|
|
6297
|
+
if (batch.length > 0) {
|
|
6298
|
+
if (this.state.leases.renewMany !== undefined) {
|
|
6299
|
+
const results = await this.state.leases.renewMany(batch);
|
|
6300
|
+
for (let i = 0; i < handlers.length; i++) {
|
|
6301
|
+
const result = results[i];
|
|
6302
|
+
if (result !== undefined) handlers[i]!(result);
|
|
6303
|
+
}
|
|
6304
|
+
} else {
|
|
6305
|
+
const settled = await Promise.allSettled(
|
|
6306
|
+
batch.map((request) => this.state.leases.renew(request.lease, request.subject)),
|
|
6307
|
+
);
|
|
6308
|
+
for (let i = 0; i < handlers.length; i++) {
|
|
6309
|
+
const outcome = settled[i]!;
|
|
6310
|
+
if (outcome.status === "fulfilled") handlers[i]!(outcome.value);
|
|
6311
|
+
else {
|
|
6312
|
+
if (hasMembership && i === 0) membershipRejected = true;
|
|
6313
|
+
if (!hasError) {
|
|
6314
|
+
hasError = true;
|
|
6315
|
+
firstError = outcome.reason;
|
|
6316
|
+
}
|
|
6317
|
+
}
|
|
6318
|
+
}
|
|
6319
|
+
}
|
|
5039
6320
|
}
|
|
5040
|
-
if (this.membership === undefined) {
|
|
6321
|
+
if (this.membership === undefined && !membershipRejected) {
|
|
5041
6322
|
const acquired = await this.state.leases.acquire(
|
|
5042
6323
|
this.membershipSubject(),
|
|
5043
6324
|
this.brokerId,
|
|
@@ -5045,42 +6326,30 @@ export class MultiNodeRouter {
|
|
|
5045
6326
|
);
|
|
5046
6327
|
if (acquired.status === "acquired") this.membership = acquired.lease;
|
|
5047
6328
|
}
|
|
5048
|
-
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5052
|
-
|
|
5053
|
-
|
|
5054
|
-
|
|
6329
|
+
if (hasError) throw firstError;
|
|
6330
|
+
for (const [session, entry] of missingPresence) {
|
|
6331
|
+
this.serial(deliveryTargetIdentity(session), async () => {
|
|
6332
|
+
// #5607: a session still to take its id is not presented across nodes before it has.
|
|
6333
|
+
if (this.presence.get(session) !== entry || entry.unbound === true || entry.adoption !== undefined) return;
|
|
6334
|
+
await this.acquirePresence(session, entry);
|
|
6335
|
+
const { rebind } = entry;
|
|
6336
|
+
if (entry.lease !== undefined && rebind !== undefined) {
|
|
6337
|
+
// #5138: kept as an unfinished step until it succeeds, so a store error does not lose it.
|
|
6338
|
+
delete entry.rebind;
|
|
6339
|
+
entry.unfinished = { ...(entry.unfinished ?? { attempts: 0 }), rebind };
|
|
5055
6340
|
}
|
|
5056
|
-
|
|
5057
|
-
this.
|
|
5058
|
-
|
|
5059
|
-
|
|
6341
|
+
try {
|
|
6342
|
+
await this.finishRegistration(session, entry);
|
|
6343
|
+
} finally {
|
|
6344
|
+
if (entry.lease !== undefined) this.scanIdentity(deliveryTargetIdentity(session));
|
|
5060
6345
|
}
|
|
5061
|
-
}
|
|
5062
|
-
this.serial(deliveryTargetIdentity(session), async () => {
|
|
5063
|
-
// #5607: a session still to take its id is not presented across nodes before it has.
|
|
5064
|
-
if (this.presence.get(session) !== entry || entry.unbound === true || entry.adoption !== undefined) return;
|
|
5065
|
-
await this.acquirePresence(session, entry);
|
|
5066
|
-
const { rebind } = entry;
|
|
5067
|
-
if (entry.lease !== undefined && rebind !== undefined) {
|
|
5068
|
-
// #5138: kept as an unfinished step until it succeeds, so a store error does not lose it.
|
|
5069
|
-
delete entry.rebind;
|
|
5070
|
-
entry.unfinished = { ...(entry.unfinished ?? { attempts: 0 }), rebind };
|
|
5071
|
-
}
|
|
5072
|
-
try {
|
|
5073
|
-
await this.finishRegistration(session, entry);
|
|
5074
|
-
} finally {
|
|
5075
|
-
if (entry.lease !== undefined) this.scanIdentity(deliveryTargetIdentity(session));
|
|
5076
|
-
}
|
|
5077
|
-
});
|
|
5078
|
-
}
|
|
6346
|
+
});
|
|
5079
6347
|
}
|
|
5080
6348
|
for (const inflight of [...this.inflight.values()]) {
|
|
5081
6349
|
const renewed = await this.state.outbox.renewClaim(inflight.claim);
|
|
5082
6350
|
if (renewed.status !== "renewed" && this.inflight.get(inflight.row.messageId) === inflight) {
|
|
5083
6351
|
this.inflight.delete(inflight.row.messageId);
|
|
6352
|
+
inflight.finishOpening?.();
|
|
5084
6353
|
}
|
|
5085
6354
|
}
|
|
5086
6355
|
for (const [key, mirror] of this.mirrored) {
|
|
@@ -5232,8 +6501,23 @@ export class MultiNodeRouter {
|
|
|
5232
6501
|
async close(): Promise<void> {
|
|
5233
6502
|
if (this.closed) return;
|
|
5234
6503
|
this.closed = true;
|
|
6504
|
+
for (const stop of [...this.progressStops]) stop();
|
|
6505
|
+
this.progressStops.clear();
|
|
6506
|
+
this.queuedProgress.clear();
|
|
6507
|
+
this.replyLanes.clear();
|
|
6508
|
+
this.sendOrder.close();
|
|
6509
|
+
// Release every pending return-path opening (both `inflight` and `settleDelivered`) before awaiting `serials`,
|
|
6510
|
+
// so a serialized reply waiting on a pending opening wakes immediately and never deadlocks shutdown.
|
|
6511
|
+
for (const release of [...this.activeOpenings.keys()]) release();
|
|
6512
|
+
this.activeOpenings.clear();
|
|
6513
|
+
for (const initial of this.initialScans.values()) initial.resolve();
|
|
6514
|
+
this.initialScans.clear();
|
|
5235
6515
|
for (const timer of this.timers) clearInterval(timer);
|
|
6516
|
+
// #5607: the ids an unanswered claim may still bind are given up with all of this broker's (`retireBroker`).
|
|
6517
|
+
for (const use of this.unansweredClaims.values()) use.resolve("kept");
|
|
6518
|
+
this.unansweredClaims.clear();
|
|
5236
6519
|
for (const waiter of [...this.waiters.values()]) this.dropWaiter(waiter);
|
|
6520
|
+
this.unsettledOutboxPairs.clear();
|
|
5237
6521
|
for (const hold of this.holds.values()) clearTimeout(hold.timer);
|
|
5238
6522
|
this.holds.clear();
|
|
5239
6523
|
await Promise.all([...this.serials.values()]);
|
|
@@ -5260,3 +6544,7 @@ export class MultiNodeRouter {
|
|
|
5260
6544
|
function stableIdentity(targetIdentity: string): string | undefined {
|
|
5261
6545
|
return targetIdentity.startsWith('["return",') ? targetIdentity : undefined;
|
|
5262
6546
|
}
|
|
6547
|
+
|
|
6548
|
+
function outboxPairKey(senderKey: string, targetIdentity: string): string {
|
|
6549
|
+
return `${senderKey}\0${targetIdentity}`;
|
|
6550
|
+
}
|