@makaio/storage-pg 1.0.0-dev-1786014320559 → 1.0.0-dev-1788511348215

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/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export { postgresStorageEngine } from './engine.js';
15
15
  export { isPostgresDuplicateObjectError, isPostgresUniqueViolationError } from './errors.js';
16
16
  export { postgresFtsSearchStrategy } from './fts-strategy.js';
17
17
  export { buildPostgresLedgerDdl, migrationAdvisoryLockKey, POSTGRES_MIGRATION_BEGIN } from './migrations.js';
18
+ export { postgresTransactionLockExpressions, postgresTransactionLockKey, postgresTransactionLocks, } from './transaction-locks.js';
18
19
  /**
19
20
  * Well-known engine export consumed by host URL auto-resolve: runtime hosts
20
21
  * that recognize a Postgres database URL import this package and register
package/dist/index.mjs CHANGED
@@ -455,6 +455,61 @@ async function acquirePostgresMigrationLock(session, ledgerTableName) {
455
455
  await session.run(sql`SELECT pg_advisory_xact_lock(CAST(${lockKey} AS bigint))`);
456
456
  }
457
457
 
458
+ //#endregion
459
+ //#region src/transaction-locks.ts
460
+ /**
461
+ * Postgres implementation of stable transaction-key locks.
462
+ * @packageDocumentation
463
+ */
464
+ const FNV_64_OFFSET_BASIS = 14695981039346656037n;
465
+ const FNV_64_PRIME = 1099511628211n;
466
+ const UINT_64_MASK = 18446744073709551615n;
467
+ const SIGNED_64_BOUNDARY = 9223372036854775808n;
468
+ const UINT_64_RANGE = 18446744073709551616n;
469
+ const encoder = new TextEncoder();
470
+ /**
471
+ * Write one UTF-8 string in an unambiguous length-prefixed representation.
472
+ * @param value - String to frame.
473
+ * @returns Big-endian length followed by the UTF-8 bytes.
474
+ */
475
+ function frame(value) {
476
+ const bytes = encoder.encode(value);
477
+ const framed = new Uint8Array(4 + bytes.length);
478
+ new DataView(framed.buffer).setUint32(0, bytes.length, false);
479
+ framed.set(bytes, 4);
480
+ return framed;
481
+ }
482
+ /**
483
+ * Derive a signed Postgres advisory key from a stable namespace and identity.
484
+ *
485
+ * Both values are UTF-8 length-framed before FNV-1a is applied, so distinct
486
+ * `(namespace, identity)` pairs cannot collide through concatenation ambiguity.
487
+ * @param lock - Stable lock identity supplied by a storage caller.
488
+ * @returns Signed 64-bit key accepted by `pg_advisory_xact_lock`.
489
+ */
490
+ function postgresTransactionLockKey(lock) {
491
+ let hash = FNV_64_OFFSET_BASIS;
492
+ for (const value of [lock.namespace, lock.identity]) for (const byte of frame(value)) {
493
+ hash ^= BigInt(byte);
494
+ hash = hash * FNV_64_PRIME & UINT_64_MASK;
495
+ }
496
+ return hash >= SIGNED_64_BOUNDARY ? hash - UINT_64_RANGE : hash;
497
+ }
498
+ /**
499
+ * Build ordered advisory-lock expressions for a stable-key set.
500
+ *
501
+ * Postgres advisory locks are keyed by signed 64-bit integers. Deduplicating
502
+ * and sorting those actual integers, rather than their input strings, gives
503
+ * every transaction that needs the same set one database-level lock order.
504
+ * @param locks - Stable keys the transaction will mutate.
505
+ * @returns One transaction-scoped advisory-lock expression per distinct key.
506
+ */
507
+ function postgresTransactionLockExpressions(locks) {
508
+ return [...new Set(locks.map(postgresTransactionLockKey))].sort((left, right) => left < right ? -1 : left > right ? 1 : 0).map((key) => sql`pg_advisory_xact_lock(CAST(${key.toString()} AS bigint))`);
509
+ }
510
+ /** Postgres strategy for the generic transaction-lock acquisition seam. */
511
+ const postgresTransactionLocks = { lockExpressions: postgresTransactionLockExpressions };
512
+
458
513
  //#endregion
459
514
  //#region src/engine.ts
460
515
  /**
@@ -511,6 +566,7 @@ const postgresStorageEngine = {
511
566
  acquireTransactionLock: acquirePostgresMigrationLock,
512
567
  extensionLedgerName: (sourceHash) => `__makaio_migrations_${sourceHash}`
513
568
  },
569
+ transactionLocks: postgresTransactionLocks,
514
570
  fts: postgresFtsSearchStrategy
515
571
  };
516
572
 
@@ -525,4 +581,4 @@ const postgresStorageEngine = {
525
581
  const storageEngine = postgresStorageEngine;
526
582
 
527
583
  //#endregion
528
- export { POSTGRES_MIGRATION_BEGIN, buildPostgresLedgerDdl, isPostgresDuplicateObjectError, isPostgresUniqueViolationError, migrationAdvisoryLockKey, postgresFtsSearchStrategy, postgresStorageEngine, storageEngine };
584
+ export { POSTGRES_MIGRATION_BEGIN, buildPostgresLedgerDdl, isPostgresDuplicateObjectError, isPostgresUniqueViolationError, migrationAdvisoryLockKey, postgresFtsSearchStrategy, postgresStorageEngine, postgresTransactionLockExpressions, postgresTransactionLockKey, postgresTransactionLocks, storageEngine };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Postgres implementation of stable transaction-key locks.
3
+ * @packageDocumentation
4
+ */
5
+ import { type SQL } from 'drizzle-orm';
6
+ import type { StorageEngineTransactionLocks, TransactionLock } from '@makaio/framework/storage/drizzle';
7
+ /**
8
+ * Derive a signed Postgres advisory key from a stable namespace and identity.
9
+ *
10
+ * Both values are UTF-8 length-framed before FNV-1a is applied, so distinct
11
+ * `(namespace, identity)` pairs cannot collide through concatenation ambiguity.
12
+ * @param lock - Stable lock identity supplied by a storage caller.
13
+ * @returns Signed 64-bit key accepted by `pg_advisory_xact_lock`.
14
+ */
15
+ export declare function postgresTransactionLockKey(lock: TransactionLock): bigint;
16
+ /**
17
+ * Build ordered advisory-lock expressions for a stable-key set.
18
+ *
19
+ * Postgres advisory locks are keyed by signed 64-bit integers. Deduplicating
20
+ * and sorting those actual integers, rather than their input strings, gives
21
+ * every transaction that needs the same set one database-level lock order.
22
+ * @param locks - Stable keys the transaction will mutate.
23
+ * @returns One transaction-scoped advisory-lock expression per distinct key.
24
+ */
25
+ export declare function postgresTransactionLockExpressions(locks: readonly TransactionLock[]): readonly SQL[];
26
+ /** Postgres strategy for the generic transaction-lock acquisition seam. */
27
+ export declare const postgresTransactionLocks: StorageEngineTransactionLocks;
@@ -0,0 +1,12 @@
1
+ CREATE TABLE "runtime_instances" (
2
+ "instance_id" text NOT NULL,
3
+ "machine_id" text NOT NULL,
4
+ "incarnation" integer NOT NULL,
5
+ "started_at" bigint NOT NULL,
6
+ "retired_at" bigint,
7
+ CONSTRAINT "runtime_instances_instance_id_machine_id_pk" PRIMARY KEY("instance_id","machine_id")
8
+ );
9
+ --> statement-breakpoint
10
+ ALTER TABLE "adapter_session_claims" ADD COLUMN "owner_instance_id" text;--> statement-breakpoint
11
+ CREATE UNIQUE INDEX "uniq_runtime_instances_incarnation" ON "runtime_instances" USING btree ("machine_id","incarnation");--> statement-breakpoint
12
+ ALTER TABLE "adapter_session_claims" ADD CONSTRAINT "adapter_session_claims_owner_instance_id_machine_id_runtime_instances_instance_id_machine_id_fk" FOREIGN KEY ("owner_instance_id","machine_id") REFERENCES "runtime_instances"("instance_id","machine_id") ON DELETE restrict ON UPDATE no action;
@@ -0,0 +1,3 @@
1
+ ALTER TABLE "agents" ADD COLUMN "owner_machine_id" text;--> statement-breakpoint
2
+ ALTER TABLE "agents" ADD COLUMN "owner_instance_id" text;--> statement-breakpoint
3
+ ALTER TABLE "agents" ADD CONSTRAINT "agents_runtime_owner_pair_check" CHECK (("agents"."owner_machine_id" IS NULL) = ("agents"."owner_instance_id" IS NULL));
@@ -0,0 +1 @@
1
+ ALTER TABLE "agents" ADD COLUMN "recovery_attempt_id" text;
@@ -0,0 +1,6 @@
1
+ -- Pre-release clean cut: reset existing development databases before applying
2
+ -- this chain. The private allocator intentionally does not backfill prior rows.
3
+ CREATE TABLE "runtime_instance_incarnation_counters" (
4
+ "machine_id" text PRIMARY KEY NOT NULL,
5
+ "last_allocated_incarnation" integer NOT NULL
6
+ );
@@ -169,6 +169,34 @@
169
169
  "when": 1785874382691,
170
170
  "tag": "0023_m_msf3hu2s",
171
171
  "breakpoints": true
172
+ },
173
+ {
174
+ "idx": 24,
175
+ "version": "7",
176
+ "when": 1786006291205,
177
+ "tag": "0024_m_msha1388",
178
+ "breakpoints": true
179
+ },
180
+ {
181
+ "idx": 25,
182
+ "version": "7",
183
+ "when": 1786017213179,
184
+ "tag": "0025_m_mshgj6x7",
185
+ "breakpoints": true
186
+ },
187
+ {
188
+ "idx": 26,
189
+ "version": "7",
190
+ "when": 1786103485863,
191
+ "tag": "0026_m_msivwbgm",
192
+ "breakpoints": true
193
+ },
194
+ {
195
+ "idx": 27,
196
+ "version": "7",
197
+ "when": 1786383667306,
198
+ "tag": "0027_runtime_instance_incarnation_counter",
199
+ "breakpoints": true
172
200
  }
173
201
  ]
174
202
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@makaio/storage-pg",
3
3
  "description": "Postgres storage engine for the Makaio framework: driver glue, error classification, and migration behavior registered through the storage engine seam.",
4
- "version": "1.0.0-dev-1786014320559",
4
+ "version": "1.0.0-dev-1788511348215",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",