@bastani/atomic 0.10.11-alpha.3 → 0.10.11-alpha.4
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 +51 -0
- package/dist/_shared/dist-KOELCESV.js +2 -2
- package/dist/_shared/fork-row-index-UAUAQRD5.js +2 -2
- package/dist/_shared/lib-4PES5P4V.js +2 -2
- package/dist/_shared/mode-interactive-sdk-7KXUFI2F.js +4 -4
- package/dist/_shared/mode-rpc-sdk-IOPGDYR4.js +4 -4
- package/dist/_shared/mode-shared-2QJSCUM6.js +4 -4
- package/dist/_shared/p1a-prewarm-XIU57JHF.js +3 -3
- package/dist/_shared/p1b-elig-TVB5CBLL.js +22 -6
- package/dist/_shared/p1b-elig-TVB5CBLL.js.map +1 -1
- package/dist/_shared/p2-shell-main-B2WIALFD.js +6 -3
- package/dist/_shared/p2-shell-main-B2WIALFD.js.map +1 -1
- package/dist/_shared/runtime-O4KCSG4C.js +2 -2
- package/dist/_shared/sdk-B5LEWGGI.js +899 -515
- package/dist/_shared/sdk-B5LEWGGI.js.map +1 -1
- package/dist/_shared/session-data-AHTU7IS6.js +2 -2
- package/dist/_shared/session-manager-list-2BLEEZVG.js +1 -1
- package/dist/_shared/{startup-0-0-EZUKEP4Q.js → startup-0-0-XJJDOIEC.js} +6 -12
- package/dist/_shared/{startup-0-0-EZUKEP4Q.js.map → startup-0-0-XJJDOIEC.js.map} +1 -1
- package/dist/_shared/{startup-0-1-RTOHJFDJ.js → startup-0-1-M6OWAYHH.js} +354 -103
- package/dist/_shared/startup-0-1-M6OWAYHH.js.map +1 -0
- package/dist/_shared/startup-0-2-5QHPCEON.js +18 -9
- package/dist/_shared/startup-0-2-5QHPCEON.js.map +1 -1
- package/dist/_shared/startup-0-3-4ZYBYQNM.js +4 -4
- package/dist/_shared/startup-0-4-ZMJV547D.js +4 -4
- package/dist/_shared/startup-0-5-YDW47QX6.js +4 -4
- package/dist/_shared/startup-10-0-TDQNBDDL.js +4 -4
- package/dist/_shared/startup-12-0-55AAXBJZ.js +1 -1
- package/dist/_shared/startup-14-0-MLX36RBG.js +2 -2
- package/dist/_shared/startup-15-0-YUKGAC63.js +2 -2
- package/dist/_shared/startup-15-1-4S7FLMNG.js +4 -4
- package/dist/_shared/startup-8-0-34CBHPZH.js +2114 -235
- package/dist/_shared/startup-8-0-34CBHPZH.js.map +1 -1
- package/dist/_shared/startup-8-1-LZFXSD3Z.js +12 -12
- package/dist/_shared/storage-command-7MGXLJGZ.js +6 -6
- package/dist/_shared/storage-command-7MGXLJGZ.js.map +1 -1
- package/dist/_shared/storage-status-K7PX2RXA.js +2 -2
- package/dist/_shared/task-unsaved-answer-VJ2KTB4V.js +2 -2
- package/dist/_shared/team-DOQKICG2.js +1 -1
- package/dist/_shared/tree-outline-MUEPK5B7.js +2 -2
- package/dist/builtin/intercom/CHANGELOG.md +19 -0
- package/dist/builtin/intercom/broker/broker.bundle.mjs +1020 -108
- package/dist/builtin/intercom/broker/client.ts +8 -0
- package/dist/builtin/intercom/broker/database-wake-up-bus.ts +91 -14
- package/dist/builtin/intercom/broker/multinode-router.ts +102 -17
- package/dist/builtin/intercom/broker/multinode-state-store.ts +41 -2
- package/dist/builtin/intercom/broker/send-order-gate.ts +5 -2
- package/dist/builtin/intercom/broker/sql-add-column.ts +22 -1
- package/dist/builtin/intercom/broker/sql-lease-store.ts +304 -48
- package/dist/builtin/intercom/broker/sql-outbox-store.ts +541 -50
- package/dist/builtin/intercom/broker/sql-restoring-reservation-store.ts +239 -8
- package/dist/builtin/intercom/broker/sql-retention.ts +11 -2
- package/dist/builtin/intercom/broker/sql-schema.ts +42 -8
- package/dist/builtin/intercom/index.bundle.mjs +13 -2
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/index.bundle.mjs +1 -1
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/subagents/CHANGELOG.md +6 -0
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/skills/skill-creator/eval-viewer/generate_review.py +8 -1
- package/dist/builtin/subagents/src/extension/index.bundle.mjs +1 -1
- package/dist/builtin/web-access/index.bundle.mjs +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/cli/args.js +2 -2
- package/dist/cli/auth-cli.code.js +2 -2
- package/dist/cli/auth-cli.impl.js +3 -3
- package/dist/cli/auth-cli.js +3 -3
- package/dist/cli/blueprint-command.code.js +1 -1
- package/dist/cli/blueprint-command.impl.js +3 -3
- package/dist/cli/blueprint-command.js +3 -3
- package/dist/cli/intercom-command.code.js +2 -2
- package/dist/cli/intercom-command.impl.js +3 -3
- package/dist/cli/intercom-command.js +3 -3
- package/dist/cli/list-models.code.js +2 -2
- package/dist/cli/list-models.impl.js +3 -3
- package/dist/cli/list-models.js +3 -3
- package/dist/cli/session-picker.code.js +1 -1
- package/dist/cli/session-picker.impl.js +3 -3
- package/dist/cli/session-picker.js +3 -3
- package/dist/cli/startup-ui.code.js +1 -1
- package/dist/cli/startup-ui.impl.js +3 -3
- package/dist/cli/startup-ui.js +3 -3
- package/dist/cli/storage-command.impl.js +3 -3
- package/dist/cli/storage-command.js +3 -3
- package/dist/cli/storage-search-session.code.js +1 -1
- package/dist/cli/storage-search-session.impl.js +3 -3
- package/dist/cli/storage-search-session.js +3 -3
- package/dist/cli/team-command.code.js +1 -1
- package/dist/cli/team-command.impl.js +3 -3
- package/dist/cli/team-command.js +3 -3
- package/dist/cli.js +2 -2
- package/dist/core/debug.impl.js +3 -3
- package/dist/core/debug.js +3 -3
- package/dist/core/export-html/card-projection.impl.js +3 -3
- package/dist/core/export-html/card-projection.js +3 -3
- package/dist/core/export-html/index.code.js +4 -4
- package/dist/core/export-html/index.code.js.map +1 -1
- package/dist/core/export-html/index.impl.js +3 -3
- package/dist/core/export-html/index.js +3 -3
- package/dist/core/export-html/json-spans.impl.js +3 -3
- package/dist/core/export-html/json-spans.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.impl.js +3 -3
- package/dist/core/export-html/oversized-render-inputs.js +3 -3
- package/dist/core/export-html/tool-renderer.impl.js +3 -3
- package/dist/core/export-html/tool-renderer.js +3 -3
- package/dist/core/extensions/jiti-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-loader.js +3 -3
- package/dist/core/extensions/jiti-static-loader.impl.js +3 -3
- package/dist/core/extensions/jiti-static-loader.js +3 -3
- package/dist/core/extensions/loader-host-modules.code.js +1 -1
- package/dist/core/extensions/loader-host-modules.impl.js +3 -3
- package/dist/core/extensions/loader-host-modules.js +3 -3
- package/dist/core/extensions/loader-virtual-modules.js +1 -1
- package/dist/core/extensions/runner-context.js +1 -1
- package/dist/core/intercom-broker-control-host.impl.js +3 -3
- package/dist/core/intercom-broker-control-host.js +3 -3
- package/dist/core/intercom-broker-control.code.js +3 -3
- package/dist/core/intercom-broker-control.impl.js +3 -3
- package/dist/core/intercom-broker-control.js +3 -3
- package/dist/core/package-manager-storage-adapters.impl.js +3 -3
- package/dist/core/package-manager-storage-adapters.js +3 -3
- package/dist/core/process-start-token.d.ts +5 -4
- package/dist/core/sdk.code.js +2 -2
- package/dist/core/sdk.impl.js +3 -3
- package/dist/core/sdk.js +3 -3
- package/dist/core/session-manager-core.js +4 -4
- package/dist/core/session-manager-list.impl.js +3 -3
- package/dist/core/session-manager-list.js +3 -3
- package/dist/core/session-manager.code.js +1 -1
- package/dist/core/session-manager.impl.js +3 -3
- package/dist/core/session-manager.js +3 -3
- package/dist/core/session-storage.impl.js +3 -3
- package/dist/core/session-storage.js +3 -3
- package/dist/core/storage/database.d.ts +6 -0
- package/dist/core/storage/database.js.map +1 -1
- package/dist/core/storage/fenced-paths.d.ts +7 -6
- package/dist/core/storage/fenced-paths.js +7 -6
- package/dist/core/storage/fenced-paths.js.map +1 -1
- package/dist/core/storage/index.js +2 -2
- package/dist/core/storage/node-sqlite-thread.js +209 -19
- package/dist/core/storage/node-sqlite-thread.js.map +1 -1
- package/dist/core/storage/node-sqlite.js +435 -71
- package/dist/core/storage/node-sqlite.js.map +1 -1
- package/dist/core/storage/offline-snapshot-file.impl.js +3 -3
- package/dist/core/storage/offline-snapshot-file.js +3 -3
- package/dist/core/storage/provider.js +2 -2
- package/dist/core/storage/schema-catalog.d.ts +69 -7
- package/dist/core/storage/schema-catalog.js +352 -61
- package/dist/core/storage/schema-catalog.js.map +1 -1
- package/dist/core/storage/selection.js +2 -2
- package/dist/core/storage/session/branch-fork-source.code.js +2 -2
- package/dist/core/storage/session/branch-fork-source.impl.js +3 -3
- package/dist/core/storage/session/branch-fork-source.js +3 -3
- package/dist/core/storage/session/card-view.impl.js +3 -3
- package/dist/core/storage/session/card-view.js +3 -3
- package/dist/core/storage/session/child-reader.code.js +3 -3
- package/dist/core/storage/session/child-reader.impl.js +3 -3
- package/dist/core/storage/session/child-reader.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.impl.js +3 -3
- package/dist/core/storage/session/chunked-resume-projection.js +3 -3
- package/dist/core/storage/session/expanded-child-details.code.js +2 -2
- package/dist/core/storage/session/expanded-child-details.impl.js +3 -3
- package/dist/core/storage/session/expanded-child-details.js +3 -3
- package/dist/core/storage/session/fork-detach.d.ts +11 -1
- package/dist/core/storage/session/fork-row-bytes.impl.js +3 -3
- package/dist/core/storage/session/fork-row-bytes.js +3 -3
- package/dist/core/storage/session/fork-row-index.impl.js +3 -3
- package/dist/core/storage/session/fork-row-index.js +3 -3
- package/dist/core/storage/session/fork-staging.code.js +1 -1
- package/dist/core/storage/session/fork-staging.impl.js +3 -3
- package/dist/core/storage/session/fork-staging.js +3 -3
- package/dist/core/storage/session/fork-sweep.js +31 -7
- package/dist/core/storage/session/fork-sweep.js.map +1 -1
- package/dist/core/storage/session/fork-window.code.js +3 -3
- package/dist/core/storage/session/fork-window.impl.js +3 -3
- package/dist/core/storage/session/fork-window.js +3 -3
- package/dist/core/storage/session/history-pages.impl.js +3 -3
- package/dist/core/storage/session/history-pages.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.impl.js +3 -3
- package/dist/core/storage/session/jsonl-entry-projection.js +3 -3
- package/dist/core/storage/session/jsonl-session-lock.d.ts +100 -0
- package/dist/core/storage/session/jsonl-session-store.d.ts +41 -23
- package/dist/core/storage/session/jsonl-session-store.impl.js +3 -3
- package/dist/core/storage/session/jsonl-session-store.js +3 -3
- package/dist/core/storage/session/memory-session-store.impl.js +3 -3
- package/dist/core/storage/session/memory-session-store.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.code.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.impl.js +3 -3
- package/dist/core/storage/session/pinned-branch-resume.js +3 -3
- package/dist/core/storage/session/released-history-files.code.js +8 -8
- package/dist/core/storage/session/released-history-files.code.js.map +1 -1
- package/dist/core/storage/session/released-history-files.impl.js +3 -3
- package/dist/core/storage/session/released-history-files.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.impl.js +3 -3
- package/dist/core/storage/session/resident-fork-rows.js +3 -3
- package/dist/core/storage/session/session-resume.impl.js +3 -3
- package/dist/core/storage/session/session-resume.js +3 -3
- package/dist/core/storage/session/session-search-index.d.ts +9 -1
- package/dist/core/storage/session/snapshot-cut.impl.js +3 -3
- package/dist/core/storage/session/snapshot-cut.js +3 -3
- package/dist/core/storage/session/snapshot-history.code.js +3 -3
- package/dist/core/storage/session/snapshot-history.impl.js +3 -3
- package/dist/core/storage/session/snapshot-history.js +3 -3
- package/dist/core/storage/session/sql-session-store.d.ts +51 -7
- package/dist/core/storage/session/sql-session-store.impl.js +3 -3
- package/dist/core/storage/session/sql-session-store.js +3 -3
- package/dist/core/storage/session/tree-outline.impl.js +3 -3
- package/dist/core/storage/session/tree-outline.js +3 -3
- package/dist/core/storage/session/tree-presentation.impl.js +3 -3
- package/dist/core/storage/session/tree-presentation.js +3 -3
- package/dist/core/storage/sqlite-facade.d.ts +6 -0
- package/dist/core/storage/sqlite-facade.js +309 -33
- package/dist/core/storage/sqlite-facade.js.map +1 -1
- package/dist/core/storage/sqlite-open-batch.js +10 -0
- package/dist/core/storage/sqlite-open-batch.js.map +1 -0
- package/dist/core/storage/storage-status.impl.js +3 -3
- package/dist/core/storage/storage-status.js +3 -3
- package/dist/core/storage/team.impl.js +3 -3
- package/dist/core/storage/team.js +3 -3
- package/dist/core/structured-output/index.impl.js +3 -3
- package/dist/core/structured-output/index.js +3 -3
- package/dist/core/tasks/task-completion-entry.impl.js +3 -3
- package/dist/core/tasks/task-completion-entry.js +3 -3
- package/dist/core/tasks/task-file-history.code.js +3 -3
- package/dist/core/tasks/task-file-history.impl.js +3 -3
- package/dist/core/tasks/task-file-history.js +3 -3
- package/dist/core/tasks/task-reference-archive.code.js +3 -3
- package/dist/core/tasks/task-reference-archive.js +3 -3
- package/dist/core/tasks/task-reference-identity.js +3 -3
- package/dist/core/tasks/task-reference-stream.impl.js +3 -3
- package/dist/core/tasks/task-reference-stream.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.impl.js +3 -3
- package/dist/core/tasks/task-unsaved-answer.js +3 -3
- package/dist/core/tasks/task-view-source.impl.js +3 -3
- package/dist/core/tasks/task-view-source.js +3 -3
- package/dist/core/tools/read-url.code.js +2 -2
- package/dist/core/tools/read-url.impl.js +3 -3
- package/dist/core/tools/read-url.js +3 -3
- package/dist/core/worktree-snapshot/blob-store.d.ts +5 -3
- package/dist/core/worktree-snapshot/blob-store.js +151 -21
- package/dist/core/worktree-snapshot/blob-store.js.map +1 -1
- package/dist/core/worktree-snapshot/errors.code.js +2 -2
- package/dist/core/worktree-snapshot/errors.impl.js +3 -3
- package/dist/core/worktree-snapshot/errors.js +3 -3
- package/dist/core/worktree-snapshot/fork-snapshot-index.js +158 -41
- package/dist/core/worktree-snapshot/fork-snapshot-index.js.map +1 -1
- package/dist/core/worktree-snapshot/session-index-reset.impl.js +3 -3
- package/dist/core/worktree-snapshot/session-index-reset.js +3 -3
- package/dist/extensions/codemode/execute.code.js +1 -1
- package/dist/extensions/codemode/execute.impl.js +3 -3
- package/dist/extensions/codemode/execute.js +3 -3
- package/dist/extensions/codemode/renderer.code.js +1 -1
- package/dist/extensions/codemode/renderer.impl.js +3 -3
- package/dist/extensions/codemode/renderer.js +3 -3
- package/dist/extensions/mcp/cli.code.js +1 -1
- package/dist/extensions/mcp/cli.impl.js +3 -3
- package/dist/extensions/mcp/cli.js +3 -3
- package/dist/extensions/mcp/cli.lazy.code.js +2 -2
- package/dist/extensions/mcp/cli.lazy.impl.js +3 -3
- package/dist/extensions/mcp/cli.lazy.js +3 -3
- package/dist/extensions/mcp/runtime.impl.js +3 -3
- package/dist/extensions/mcp/runtime.js +3 -3
- package/dist/index.js +2 -2
- package/dist/main-first-time-setup.code.js +1 -1
- package/dist/main-first-time-setup.impl.js +3 -3
- package/dist/main-first-time-setup.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.code.js +2 -2
- package/dist/modes/interactive/components/stored-tree-view.impl.js +3 -3
- package/dist/modes/interactive/components/stored-tree-view.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.impl.js +3 -3
- package/dist/modes/interactive/components/user-message-selector-archive.js +3 -3
- package/dist/package-manager-cli.code.js +4 -4
- package/dist/package-manager-cli.impl.js +3 -3
- package/dist/package-manager-cli.js +3 -3
- package/dist/rpc-entry.js +1 -1
- package/dist/startup-files.json +1 -1
- package/dist/startup-stages/3.js +1 -1
- package/dist/startup-stages/5.js +1 -1
- package/dist/utils/startup-database-spare.code.js +2 -2
- package/dist/utils/startup-database-spare.impl.js +3 -3
- package/dist/utils/startup-database-spare.js +3 -3
- package/dist/utils/tools-manager.code.js +1 -1
- package/dist/utils/tools-manager.impl.js +3 -3
- package/dist/utils/tools-manager.js +3 -3
- package/docs/changelog.mdx +49 -0
- package/docs/storage/jsonl.md +3 -3
- package/package.json +8 -8
- package/dist/_shared/startup-0-1-RTOHJFDJ.js.map +0 -1
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
type SqliteDatabase,
|
|
6
6
|
type SqliteExecutor,
|
|
7
7
|
type SqliteRow,
|
|
8
|
-
type SqliteRunResult,
|
|
9
8
|
type SqliteStatement,
|
|
10
9
|
type StorageDialect,
|
|
11
10
|
storageMigrationTable,
|
|
@@ -26,8 +25,10 @@ import type {
|
|
|
26
25
|
import { isMissingPrivilege } from "./sql-add-column.js";
|
|
27
26
|
import { recipientTransaction } from "./sql-outbox-store.js";
|
|
28
27
|
import {
|
|
28
|
+
brokerSchemaCurrent,
|
|
29
29
|
ensureBrokerSchema,
|
|
30
30
|
openCatalogOf,
|
|
31
|
+
queueBrokerSqliteSetup,
|
|
31
32
|
RETRY_UNAPPLIED,
|
|
32
33
|
stampBrokerSchemaAtOpenEnd,
|
|
33
34
|
withBrokerSchemaCatalog,
|
|
@@ -57,6 +58,8 @@ const LEASE_LOCK_WAIT_MS = 2_000;
|
|
|
57
58
|
|
|
58
59
|
/** The statement of a presence acquisition's wait for the sends to its recipient under way: it only takes the lock. */
|
|
59
60
|
const SENDS_PASSED = { sql: "SELECT 1 AS passed", params: [] };
|
|
61
|
+
/** The setting a PostgreSQL batched lease renewal sets when any lease missed, so its follow-up read runs. */
|
|
62
|
+
const LEASE_RENEW_MISSES = "atomic.intercom_lease_renew_misses";
|
|
60
63
|
|
|
61
64
|
/**
|
|
62
65
|
* The kind of the row whose `last_token` counts acquisitions on PostgreSQL where the order sequence is missing: see
|
|
@@ -215,21 +218,41 @@ async function ensureAddressColumns(database: SqliteDatabase, prefix: string | u
|
|
|
215
218
|
);
|
|
216
219
|
await tx.exec(indexes);
|
|
217
220
|
};
|
|
218
|
-
// On SQLite, which records no step, the catalog an open read
|
|
219
|
-
//
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
ADDRESS_COLUMNS.every(
|
|
224
|
-
(column) => catalog.hasColumn(table, column) && catalog.sqliteObjectType?.(`${table}_${column}`) === "index",
|
|
225
|
-
)
|
|
226
|
-
) {
|
|
227
|
-
await stampBrokerSchemaAtOpenEnd(database, prefix, "lease-addresses", setup, (found) =>
|
|
221
|
+
// On SQLite, which records no step, the catalog or stamp an open read decides: a current table costs no write
|
|
222
|
+
// transaction (#6560), and missing columns/indexes queue into the open's single write transaction (#6709).
|
|
223
|
+
if (!dialect.versionedMigrations) {
|
|
224
|
+
if (await brokerSchemaCurrent(database, prefix, "lease-addresses")) return true;
|
|
225
|
+
const verify = (found: storage.StorageSchemaCatalog) =>
|
|
228
226
|
ADDRESS_COLUMNS.every(
|
|
229
227
|
(column) => found.hasColumn(table, column) && found.sqliteObjectType?.(`${table}_${column}`) === "index",
|
|
230
|
-
)
|
|
231
|
-
);
|
|
232
|
-
|
|
228
|
+
);
|
|
229
|
+
const schema = { ledger: storageMigrationTable(prefix), domain: "intercom-lease-addresses", revision: 1 };
|
|
230
|
+
if (
|
|
231
|
+
queueBrokerSqliteSetup(database, prefix ?? "", {
|
|
232
|
+
stamp: schema,
|
|
233
|
+
verify,
|
|
234
|
+
run(ctx) {
|
|
235
|
+
if (ctx.catalog !== undefined && verify(ctx.catalog)) return;
|
|
236
|
+
for (const column of ADDRESS_COLUMNS) {
|
|
237
|
+
if (ctx.hasColumn(table, column)) continue;
|
|
238
|
+
ctx.enqueue(
|
|
239
|
+
`ALTER TABLE ${table} ADD COLUMN ${column} TEXT GENERATED ALWAYS AS (${expressions[column]}) VIRTUAL`,
|
|
240
|
+
);
|
|
241
|
+
ctx.noteColumnAdded(table, column);
|
|
242
|
+
}
|
|
243
|
+
for (const column of ADDRESS_COLUMNS) {
|
|
244
|
+
if (ctx.catalog?.sqliteObjectType?.(`${table}_${column}`) === "index") continue;
|
|
245
|
+
ctx.enqueue(`CREATE INDEX IF NOT EXISTS ${table}_${column} ON ${table}(${column})`);
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
})
|
|
249
|
+
)
|
|
250
|
+
return true;
|
|
251
|
+
const catalog = await openCatalogOf(database);
|
|
252
|
+
if (catalog?.sqliteObjectType !== undefined && verify(catalog)) {
|
|
253
|
+
await stampBrokerSchemaAtOpenEnd(database, prefix, "lease-addresses", setup, verify);
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
233
256
|
}
|
|
234
257
|
try {
|
|
235
258
|
await ensureBrokerSchema(database, prefix, "lease-addresses", setup);
|
|
@@ -269,6 +292,20 @@ type HolderRow = SqliteRow & {
|
|
|
269
292
|
age_ms: number | bigint | null;
|
|
270
293
|
};
|
|
271
294
|
|
|
295
|
+
type RenewedLeaseRow = SqliteRow & {
|
|
296
|
+
kind: string | null;
|
|
297
|
+
key: string | null;
|
|
298
|
+
holder_id: string | null;
|
|
299
|
+
fencing_token: number | bigint | null;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
type MissedHolderRow = SqliteRow & {
|
|
303
|
+
kind: string;
|
|
304
|
+
key: string;
|
|
305
|
+
holder_id: string | null;
|
|
306
|
+
fencing_token: number | bigint | null;
|
|
307
|
+
};
|
|
308
|
+
|
|
272
309
|
type StateRow = SqliteRow & {
|
|
273
310
|
kind: string;
|
|
274
311
|
key: string;
|
|
@@ -406,7 +443,7 @@ async function openBrokerLeaseStoreInCatalog(options: IntercomBrokerStateOptions
|
|
|
406
443
|
const { sql, params } = holderStatement(key);
|
|
407
444
|
return executor.get<HolderRow>(sql, ...params);
|
|
408
445
|
};
|
|
409
|
-
const stale = (row: HolderRow | undefined) =>
|
|
446
|
+
const stale = (row: Pick<HolderRow, "holder_id" | "fencing_token"> | undefined) =>
|
|
410
447
|
row?.holder_id === null || row?.holder_id === undefined
|
|
411
448
|
? ({ status: "stale" } as const)
|
|
412
449
|
: ({ status: "stale", currentFencingToken: Number(row.fencing_token) } as const);
|
|
@@ -575,53 +612,272 @@ async function openBrokerLeaseStoreInCatalog(options: IntercomBrokerStateOptions
|
|
|
575
612
|
},
|
|
576
613
|
async renewMany(requests) {
|
|
577
614
|
if (requests.length === 0) return [];
|
|
578
|
-
const
|
|
615
|
+
const invalidResults: Array<InvalidStateInput | undefined> = new Array(requests.length);
|
|
579
616
|
let validCount = 0;
|
|
580
617
|
for (let i = 0; i < requests.length; i++) {
|
|
581
618
|
const { lease, subject } = requests[i]!;
|
|
582
619
|
const bad = validLease(lease);
|
|
583
620
|
if (bad !== undefined) {
|
|
584
|
-
|
|
621
|
+
invalidResults[i] = bad;
|
|
585
622
|
} else if (
|
|
586
623
|
subject !== undefined &&
|
|
587
624
|
(subject.kind !== lease.key.kind || keyId(keyOf(subject)) !== keyId(lease.key))
|
|
588
625
|
) {
|
|
589
|
-
|
|
626
|
+
invalidResults[i] = invalid("subject-key-mismatch");
|
|
590
627
|
} else {
|
|
591
628
|
validCount++;
|
|
592
629
|
}
|
|
593
630
|
}
|
|
594
|
-
if (validCount === 0) return
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
631
|
+
if (validCount === 0) return invalidResults as LeaseRenewResult[];
|
|
632
|
+
const tupleKey = (kind: string, id: string, holderId: string, fencingToken: number) =>
|
|
633
|
+
JSON.stringify([kind, id.toWellFormed(), holderId.toWellFormed(), fencingToken]);
|
|
634
|
+
const pairKey = (kind: string, id: string) => JSON.stringify([kind, id.toWellFormed()]);
|
|
635
|
+
// Deduplicate exact (kind, key, holder, fencing_token) tuples, keeping the last subject in input order so the
|
|
636
|
+
// single UPDATE matches each target row at most once and matches serial renewal semantics (#6714).
|
|
637
|
+
const byTuple = new Map<
|
|
638
|
+
string,
|
|
639
|
+
{
|
|
640
|
+
readonly kind: LeaseKind;
|
|
641
|
+
readonly id: string;
|
|
642
|
+
readonly holderId: string;
|
|
643
|
+
readonly fencingToken: number;
|
|
644
|
+
subjectJson: string | null;
|
|
645
|
+
}
|
|
646
|
+
>();
|
|
647
|
+
const validPairs = new Map<string, readonly [LeaseKind, string]>();
|
|
648
|
+
let hasNonSafeIntToken = false;
|
|
649
|
+
for (let i = 0; i < requests.length; i++) {
|
|
650
|
+
if (invalidResults[i] !== undefined) continue;
|
|
651
|
+
const { lease, subject } = requests[i]!;
|
|
652
|
+
const id = keyId(lease.key);
|
|
653
|
+
const pKey = pairKey(lease.key.kind, id);
|
|
654
|
+
if (!validPairs.has(pKey)) validPairs.set(pKey, [lease.key.kind, id]);
|
|
655
|
+
if (!Number.isSafeInteger(lease.fencingToken)) {
|
|
656
|
+
hasNonSafeIntToken = true;
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
const tKey = tupleKey(lease.key.kind, id, lease.holderId, lease.fencingToken);
|
|
660
|
+
const nextSubject = subject === undefined ? undefined : subjectJson(subject);
|
|
661
|
+
const existing = byTuple.get(tKey);
|
|
662
|
+
if (existing === undefined) {
|
|
663
|
+
byTuple.set(tKey, {
|
|
664
|
+
kind: lease.key.kind,
|
|
665
|
+
id,
|
|
666
|
+
holderId: lease.holderId,
|
|
667
|
+
fencingToken: lease.fencingToken,
|
|
668
|
+
subjectJson: nextSubject ?? null,
|
|
669
|
+
});
|
|
670
|
+
} else if (nextSubject !== undefined) {
|
|
671
|
+
existing.subjectJson = nextSubject;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
const entries = [...byTuple.values()].sort((a, b) => compare(a.kind, b.kind) || compare(a.id, b.id));
|
|
675
|
+
const answer = (
|
|
676
|
+
renewed: ReadonlySet<string>,
|
|
677
|
+
found: ReadonlyMap<string, MissedHolderRow>,
|
|
678
|
+
): LeaseRenewResult[] =>
|
|
679
|
+
requests.map(({ lease }, index): LeaseRenewResult => {
|
|
680
|
+
const bad = invalidResults[index];
|
|
681
|
+
if (bad !== undefined) return bad;
|
|
682
|
+
const id = keyId(lease.key);
|
|
683
|
+
if (
|
|
684
|
+
Number.isSafeInteger(lease.fencingToken) &&
|
|
685
|
+
renewed.has(tupleKey(lease.key.kind, id, lease.holderId, lease.fencingToken))
|
|
686
|
+
)
|
|
687
|
+
return { status: "renewed" } as const;
|
|
688
|
+
return stale(found.get(pairKey(lease.key.kind, id)));
|
|
613
689
|
});
|
|
614
|
-
|
|
615
|
-
const
|
|
616
|
-
if (
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
690
|
+
if (dialect.name === "postgres") {
|
|
691
|
+
const pairsParam = JSON.stringify([...validPairs.values()], wellFormed);
|
|
692
|
+
if (entries.length === 0) {
|
|
693
|
+
const readAll: SqliteStatement = {
|
|
694
|
+
sql: `WITH missed AS MATERIALIZED (
|
|
695
|
+
SELECT entry ->> 0 AS kind, entry ->> 1 AS key
|
|
696
|
+
FROM jsonb_array_elements((?)::jsonb) AS entry
|
|
697
|
+
)
|
|
698
|
+
SELECT t.kind, t.key, t.holder_id, t.fencing_token
|
|
699
|
+
FROM ${table} AS t
|
|
700
|
+
JOIN missed ON t.kind = missed.kind AND t.key = missed.key
|
|
701
|
+
WHERE t.kind = ANY(ARRAY(SELECT DISTINCT kind FROM missed))
|
|
702
|
+
AND t.key = ANY(ARRAY(SELECT DISTINCT key FROM missed))`,
|
|
703
|
+
params: [pairsParam],
|
|
704
|
+
};
|
|
705
|
+
return database.transaction(
|
|
706
|
+
async (tx) => {
|
|
707
|
+
const found = new Map<string, MissedHolderRow>();
|
|
708
|
+
for (const row of await tx.all<MissedHolderRow>(readAll.sql, ...readAll.params))
|
|
709
|
+
found.set(pairKey(row.kind, row.key), row);
|
|
710
|
+
return answer(new Set(), found);
|
|
711
|
+
},
|
|
712
|
+
{ ...renewal, leading: [readAll], leadingOnly: true },
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
const write: SqliteStatement = {
|
|
716
|
+
sql: `WITH requested AS MATERIALIZED (
|
|
717
|
+
SELECT
|
|
718
|
+
entry ->> 0 AS kind,
|
|
719
|
+
entry ->> 1 AS key,
|
|
720
|
+
entry ->> 2 AS holder_id,
|
|
721
|
+
(entry ->> 3)::bigint AS fencing_token,
|
|
722
|
+
entry ->> 4 AS subject_json
|
|
723
|
+
FROM jsonb_array_elements((?)::jsonb) AS entry
|
|
724
|
+
),
|
|
725
|
+
locked AS MATERIALIZED (
|
|
726
|
+
SELECT t.kind, t.key, requested.holder_id, requested.fencing_token, requested.subject_json
|
|
727
|
+
FROM ${table} AS t
|
|
728
|
+
JOIN requested
|
|
729
|
+
ON t.kind = requested.kind
|
|
730
|
+
AND t.key = requested.key
|
|
731
|
+
AND t.holder_id = requested.holder_id
|
|
732
|
+
AND t.fencing_token = requested.fencing_token
|
|
733
|
+
WHERE t.kind = ANY(ARRAY(SELECT DISTINCT kind FROM requested))
|
|
734
|
+
AND t.key = ANY(ARRAY(SELECT DISTINCT key FROM requested))
|
|
735
|
+
ORDER BY t.kind, t.key
|
|
736
|
+
FOR UPDATE OF t
|
|
737
|
+
),
|
|
738
|
+
renewed AS (
|
|
739
|
+
UPDATE ${table} AS t
|
|
740
|
+
SET renewed_at = ${nowMs}, subject_json = COALESCE(locked.subject_json, t.subject_json)
|
|
741
|
+
FROM locked
|
|
742
|
+
WHERE t.kind = locked.kind
|
|
743
|
+
AND t.key = locked.key
|
|
744
|
+
AND t.holder_id = locked.holder_id
|
|
745
|
+
AND t.fencing_token = locked.fencing_token
|
|
746
|
+
RETURNING t.kind, t.key, t.holder_id, t.fencing_token
|
|
747
|
+
),
|
|
748
|
+
gate AS MATERIALIZED (
|
|
749
|
+
SELECT set_config('${LEASE_RENEW_MISSES}',
|
|
750
|
+
CASE WHEN (SELECT COUNT(*) FROM renewed) < CAST(? AS ${types.int}) THEN '1' ELSE '' END,
|
|
751
|
+
true) AS misses
|
|
752
|
+
)
|
|
753
|
+
SELECT renewed.kind, renewed.key, renewed.holder_id, renewed.fencing_token
|
|
754
|
+
FROM gate LEFT JOIN renewed ON TRUE`,
|
|
755
|
+
params: [
|
|
756
|
+
JSON.stringify(
|
|
757
|
+
entries.map((entry) => [
|
|
758
|
+
entry.kind,
|
|
759
|
+
entry.id,
|
|
760
|
+
entry.holderId,
|
|
761
|
+
entry.fencingToken,
|
|
762
|
+
entry.subjectJson,
|
|
763
|
+
]),
|
|
764
|
+
wellFormed,
|
|
765
|
+
),
|
|
766
|
+
entries.length + (hasNonSafeIntToken ? 1 : 0),
|
|
767
|
+
],
|
|
768
|
+
};
|
|
769
|
+
const readMisses: SqliteStatement = {
|
|
770
|
+
sql: `WITH missed AS MATERIALIZED (
|
|
771
|
+
SELECT entry ->> 0 AS kind, entry ->> 1 AS key
|
|
772
|
+
FROM jsonb_array_elements((?)::jsonb) AS entry
|
|
773
|
+
WHERE current_setting('${LEASE_RENEW_MISSES}', true) = '1'
|
|
774
|
+
)
|
|
775
|
+
SELECT t.kind, t.key, t.holder_id, t.fencing_token
|
|
776
|
+
FROM ${table} AS t
|
|
777
|
+
JOIN missed ON t.kind = missed.kind AND t.key = missed.key
|
|
778
|
+
WHERE current_setting('${LEASE_RENEW_MISSES}', true) = '1'
|
|
779
|
+
AND t.kind = ANY(ARRAY(SELECT DISTINCT kind FROM missed))
|
|
780
|
+
AND t.key = ANY(ARRAY(SELECT DISTINCT key FROM missed))`,
|
|
781
|
+
params: [pairsParam],
|
|
782
|
+
};
|
|
783
|
+
return database.transaction(
|
|
784
|
+
async (tx) => {
|
|
785
|
+
const renewed = new Set<string>();
|
|
786
|
+
for (const row of await tx.all<RenewedLeaseRow>(write.sql, ...write.params))
|
|
787
|
+
if (
|
|
788
|
+
row.kind !== null &&
|
|
789
|
+
row.key !== null &&
|
|
790
|
+
row.holder_id !== null &&
|
|
791
|
+
row.fencing_token !== null
|
|
792
|
+
)
|
|
793
|
+
renewed.add(tupleKey(row.kind, row.key, row.holder_id, Number(row.fencing_token)));
|
|
794
|
+
const found = new Map<string, MissedHolderRow>();
|
|
795
|
+
for (const row of await tx.all<MissedHolderRow>(readMisses.sql, ...readMisses.params))
|
|
796
|
+
found.set(pairKey(row.kind, row.key), row);
|
|
797
|
+
return answer(renewed, found);
|
|
798
|
+
},
|
|
799
|
+
{ ...renewal, leading: [write, readMisses], leadingOnly: true },
|
|
800
|
+
);
|
|
801
|
+
}
|
|
802
|
+
const write: SqliteStatement | undefined =
|
|
803
|
+
entries.length === 0
|
|
804
|
+
? undefined
|
|
805
|
+
: {
|
|
806
|
+
sql: `WITH requested AS (
|
|
807
|
+
SELECT
|
|
808
|
+
json_extract(value, '$[0]') AS req_kind,
|
|
809
|
+
json_extract(value, '$[1]') AS req_key,
|
|
810
|
+
json_extract(value, '$[2]') AS req_holder_id,
|
|
811
|
+
json_extract(value, '$[3]') AS req_fencing_token,
|
|
812
|
+
json_extract(value, '$[4]') AS req_subject_json
|
|
813
|
+
FROM json_each(?)
|
|
814
|
+
)
|
|
815
|
+
UPDATE ${table}
|
|
816
|
+
SET renewed_at = ${nowMs}, subject_json = COALESCE(requested.req_subject_json, ${table}.subject_json)
|
|
817
|
+
FROM requested
|
|
818
|
+
WHERE ${table}.kind = requested.req_kind
|
|
819
|
+
AND ${table}.key = requested.req_key
|
|
820
|
+
AND ${table}.holder_id = requested.req_holder_id
|
|
821
|
+
AND ${table}.fencing_token = requested.req_fencing_token
|
|
822
|
+
RETURNING kind, key, holder_id, fencing_token`,
|
|
823
|
+
params: [
|
|
824
|
+
JSON.stringify(
|
|
825
|
+
entries.map((entry) => [
|
|
826
|
+
entry.kind,
|
|
827
|
+
entry.id,
|
|
828
|
+
entry.holderId,
|
|
829
|
+
entry.fencingToken,
|
|
830
|
+
entry.subjectJson,
|
|
831
|
+
]),
|
|
832
|
+
wellFormed,
|
|
833
|
+
),
|
|
834
|
+
],
|
|
835
|
+
};
|
|
836
|
+
const readMissedSql = `SELECT t.kind, t.key, t.holder_id, t.fencing_token
|
|
837
|
+
FROM ${table} AS t
|
|
838
|
+
JOIN (
|
|
839
|
+
SELECT json_extract(value, '$[0]') AS kind, json_extract(value, '$[1]') AS key
|
|
840
|
+
FROM json_each(?)
|
|
841
|
+
) AS missed
|
|
842
|
+
ON t.kind = missed.kind AND t.key = missed.key`;
|
|
843
|
+
return database.transaction(
|
|
844
|
+
async (tx) => {
|
|
845
|
+
const renewed = new Set<string>();
|
|
846
|
+
if (write !== undefined) {
|
|
847
|
+
for (const row of await tx.all<RenewedLeaseRow>(write.sql, ...write.params))
|
|
848
|
+
if (
|
|
849
|
+
row.kind !== null &&
|
|
850
|
+
row.key !== null &&
|
|
851
|
+
row.holder_id !== null &&
|
|
852
|
+
row.fencing_token !== null
|
|
853
|
+
)
|
|
854
|
+
renewed.add(tupleKey(row.kind, row.key, row.holder_id, Number(row.fencing_token)));
|
|
855
|
+
}
|
|
856
|
+
const missedByPair = new Map<string, readonly [LeaseKind, string]>();
|
|
857
|
+
for (let i = 0; i < requests.length; i++) {
|
|
858
|
+
if (invalidResults[i] !== undefined) continue;
|
|
859
|
+
const { lease } = requests[i]!;
|
|
860
|
+
const id = keyId(lease.key);
|
|
861
|
+
if (
|
|
862
|
+
Number.isSafeInteger(lease.fencingToken) &&
|
|
863
|
+
renewed.has(tupleKey(lease.key.kind, id, lease.holderId, lease.fencingToken))
|
|
864
|
+
)
|
|
865
|
+
continue;
|
|
866
|
+
const pKey = pairKey(lease.key.kind, id);
|
|
867
|
+
if (!missedByPair.has(pKey)) missedByPair.set(pKey, [lease.key.kind, id]);
|
|
868
|
+
}
|
|
869
|
+
const found = new Map<string, MissedHolderRow>();
|
|
870
|
+
if (missedByPair.size > 0) {
|
|
871
|
+
const rows = await tx.all<MissedHolderRow>(
|
|
872
|
+
readMissedSql,
|
|
873
|
+
JSON.stringify([...missedByPair.values()], wellFormed),
|
|
874
|
+
);
|
|
875
|
+
for (const row of rows) found.set(pairKey(row.kind, row.key), row);
|
|
876
|
+
}
|
|
877
|
+
return answer(renewed, found);
|
|
878
|
+
},
|
|
879
|
+
renewal,
|
|
880
|
+
);
|
|
625
881
|
},
|
|
626
882
|
async release(lease) {
|
|
627
883
|
const bad = validLease(lease);
|