@opendatalabs/personal-server-ts-lite 1.37.0 → 1.38.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/account-mode-store.d.ts +11 -0
  2. package/dist/account-mode-store.d.ts.map +1 -0
  3. package/dist/account-mode-store.js +68 -0
  4. package/dist/account-mode-store.js.map +1 -0
  5. package/dist/additions-worker.bundle.js +135 -37
  6. package/dist/browser-runtime.d.ts +88 -1
  7. package/dist/browser-runtime.d.ts.map +1 -1
  8. package/dist/browser-runtime.js +135 -12
  9. package/dist/browser-runtime.js.map +1 -1
  10. package/dist/browser-tls-rustls/browser_tls_rustls_bg.wasm +0 -0
  11. package/dist/client.d.ts +42 -3
  12. package/dist/client.d.ts.map +1 -1
  13. package/dist/client.js +130 -115
  14. package/dist/client.js.map +1 -1
  15. package/dist/history-probe-worker.bundle.js +66 -29
  16. package/dist/history-probe.worker.d.ts.map +1 -1
  17. package/dist/history-probe.worker.js +4 -3
  18. package/dist/history-probe.worker.js.map +1 -1
  19. package/dist/index.d.ts +6 -3
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +2 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/localdb-port.d.ts +28 -0
  24. package/dist/localdb-port.d.ts.map +1 -0
  25. package/dist/localdb-port.js +30 -0
  26. package/dist/localdb-port.js.map +1 -0
  27. package/dist/mcp-store.d.ts.map +1 -1
  28. package/dist/mcp-store.js +6 -2
  29. package/dist/mcp-store.js.map +1 -1
  30. package/dist/owner-binding.d.ts +5 -0
  31. package/dist/owner-binding.d.ts.map +1 -1
  32. package/dist/owner-binding.js +4 -8
  33. package/dist/owner-binding.js.map +1 -1
  34. package/dist/owner-request.d.ts +16 -0
  35. package/dist/owner-request.d.ts.map +1 -0
  36. package/dist/owner-request.js +37 -0
  37. package/dist/owner-request.js.map +1 -0
  38. package/dist/pdpp-records.d.ts +18 -0
  39. package/dist/pdpp-records.d.ts.map +1 -0
  40. package/dist/pdpp-records.js +105 -0
  41. package/dist/pdpp-records.js.map +1 -0
  42. package/dist/persistence.d.ts +1 -1
  43. package/dist/persistence.d.ts.map +1 -1
  44. package/dist/persistence.js +2 -2
  45. package/dist/persistence.js.map +1 -1
  46. package/dist/personal-server-client.d.ts +32 -0
  47. package/dist/personal-server-client.d.ts.map +1 -0
  48. package/dist/personal-server-client.js +57 -0
  49. package/dist/personal-server-client.js.map +1 -0
  50. package/dist/pull-signals.d.ts +52 -0
  51. package/dist/pull-signals.d.ts.map +1 -0
  52. package/dist/pull-signals.js +142 -0
  53. package/dist/pull-signals.js.map +1 -0
  54. package/dist/relay.d.ts.map +1 -1
  55. package/dist/relay.js +4 -1
  56. package/dist/relay.js.map +1 -1
  57. package/dist/runtime.d.ts +38 -2
  58. package/dist/runtime.d.ts.map +1 -1
  59. package/dist/runtime.js +327 -16
  60. package/dist/runtime.js.map +1 -1
  61. package/dist/seed-staging.d.ts +8 -0
  62. package/dist/seed-staging.d.ts.map +1 -0
  63. package/dist/seed-staging.js +106 -0
  64. package/dist/seed-staging.js.map +1 -0
  65. package/dist/state.d.ts +9 -1
  66. package/dist/state.d.ts.map +1 -1
  67. package/dist/state.js +10 -4
  68. package/dist/state.js.map +1 -1
  69. package/dist/storage-utils.d.ts.map +1 -1
  70. package/dist/storage-utils.js +1 -1
  71. package/dist/storage-utils.js.map +1 -1
  72. package/dist/storage.d.ts +26 -1
  73. package/dist/storage.d.ts.map +1 -1
  74. package/dist/storage.js +226 -109
  75. package/dist/storage.js.map +1 -1
  76. package/dist/stream-sync-budget.d.ts +3 -0
  77. package/dist/stream-sync-budget.d.ts.map +1 -0
  78. package/dist/stream-sync-budget.js +3 -0
  79. package/dist/stream-sync-budget.js.map +1 -0
  80. package/dist/sync.d.ts +82 -2
  81. package/dist/sync.d.ts.map +1 -1
  82. package/dist/sync.js +295 -57
  83. package/dist/sync.js.map +1 -1
  84. package/dist/wasm-stream-crypto.d.ts +20 -0
  85. package/dist/wasm-stream-crypto.d.ts.map +1 -0
  86. package/dist/wasm-stream-crypto.js +53 -0
  87. package/dist/wasm-stream-crypto.js.map +1 -0
  88. package/package.json +10 -3
package/dist/storage.d.ts CHANGED
@@ -2,6 +2,7 @@ import type { DataStoragePort } from "@opendatalabs/personal-server-ts-core/port
2
2
  import type { DataBlockManifest, DataScopeBlock } from "@opendatalabs/personal-server-ts-core/storage/blocks";
3
3
  import type { DataFileEnvelope } from "@opendatalabs/vana-sdk/browser";
4
4
  import type { IndexEntry } from "@opendatalabs/personal-server-ts-core/storage/index";
5
+ import { type LegacyWriteSeal } from "@opendatalabs/personal-server-ts-core/storage/legacy-write-seal";
5
6
  import type { PsLiteStorageAdapter } from "./runtime.js";
6
7
  export interface PsLitePersistedStorageState {
7
8
  version: 1;
@@ -67,6 +68,12 @@ export interface PsLiteStorageCapabilities {
67
68
  /** Persistent Lite storage with its public backend capability report. */
68
69
  export interface PsLiteStoragePort extends DataStoragePort {
69
70
  readonly capabilities: PsLiteStorageCapabilities;
71
+ /**
72
+ * The seal every legacy write of this storage runs inside, and every
73
+ * removal under its shared lock. Given at construction; see
74
+ * `core/storage/legacy-write-seal.ts`.
75
+ */
76
+ readonly legacyWriteSeal: LegacyWriteSeal;
70
77
  }
71
78
  export interface PsLitePersistenceAdapter {
72
79
  read(): Promise<PsLitePersistedStorageState | null>;
@@ -90,5 +97,23 @@ export declare function createIndexedDbFallbackDataFileStore(envelopes: Map<stri
90
97
  export declare function isOpfsAvailable(): Promise<boolean>;
91
98
  export declare function createOpfsPsLiteDataFileStore(): Promise<PsLiteDataFileStore>;
92
99
  export declare function createIndexedDbPsLitePersistence(options?: IndexedDbPsLitePersistenceOptions): PsLitePersistenceAdapter;
93
- export declare function createPersistentPsLiteStorage(adapter: PsLiteStorageAdapter, persistence?: PsLitePersistenceAdapter, dataFileStore?: PsLiteDataFileStore): Promise<PsLiteStoragePort>;
100
+ /**
101
+ * Persistent Lite storage. `legacyWriteSeal` is required: the storage makes a
102
+ * legacy version durable only inside `legacyWriteSeal.commit`, and removes
103
+ * data only inside `legacyWriteSeal.remove` (or under the `hold` of a caller
104
+ * that is already inside `commit`).
105
+ *
106
+ * Lock order, for every sealed method: the seal first, then the path lock,
107
+ * then the scope lock, then the index queue. A method takes the seal once, at
108
+ * its outermost call, and hands the hold to the helpers it calls; the seal is
109
+ * never taken from inside a path lock, the queue or a scope lock, so a write
110
+ * that waits behind a queued On cannot be waited on by a call that holds the
111
+ * lock the On is waiting for.
112
+ *
113
+ * Not sealed, because they add no visible version: `updateFileId`,
114
+ * `updateDataPointId`, `updateEntryVersion` (they rewrite a row that is
115
+ * already visible), and the sidecars (block manifests and payloads, first-seen
116
+ * ledgers).
117
+ */
118
+ export declare function createPersistentPsLiteStorage(adapter: PsLiteStorageAdapter, persistenceAdapter: PsLitePersistenceAdapter | undefined, dataFileStore: PsLiteDataFileStore | undefined, legacyWriteSeal: LegacyWriteSeal): Promise<PsLiteStoragePort>;
94
119
  //# sourceMappingURL=storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EAEhB,MAAM,6CAA6C,CAAC;AACrD,OAAO,KAAK,EACV,iBAAiB,EACjB,cAAc,EACf,MAAM,sDAAsD,CAAC;AAQ9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAEvE,OAAO,KAAK,EACV,UAAU,EAEX,MAAM,qDAAqD,CAAC;AAM7D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAOzD,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,CAAC,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,UAAU,EAAE,CAAC;IACtB,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAC9B,sEAAsE;IACtE,mBAAmB,CAAC,EAAE,MAAM,EAAE,C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O,GAAG,IAAI,CAAC,CAAC;IAChD,QAAQ,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACvD,SAAS,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACzC;AAED,MAAM,WAAW,iCAAiC;IAChD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAkED,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACxC,cAAc,GAAE,GAAG,CAAC,MAAM,EAAE,iBAAiB,CAAa,EAC1D,aAAa,GAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAa,GACrD,mBAAmB,CAmCrB;AA8CD,wBAAsB,eAAe,IAAI,OAAO,CAAC,OAAO,CAAC,CAKxD;AAED,wBAAsB,6BAA6B,IAAI,OAAO,CAAC,mBAAmB,CAAC,CA2GlF;AAqHD,wBAAgB,gCAAgC,CAC9C,OAAO,GAAE,iCAAsC,GAC9C,wBAAwB,CA4C1B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAsB,6BAA6B,CACjD,OAAO,EAAE,oBAAoB,EAC7B,kBAAkB,EAAE,wBAAwB,GAAG,SAAS,EACxD,aAAa,EAAE,mBAAmB,GAAG,SAAS,EAC9C,eAAe,EAAE,eAAe,GAC/B,OAAO,CAAC,iBAAiB,CAAC,CAw8B5B"}
package/dist/storage.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { buildDataBlocksAsync } from "@opendatalabs/personal-server-ts-core/storage/blocks";
2
2
  import { DataBlockStorageError, encodeDataBlockCursor, selectScopeBlocksByIds, validateDataBlockCursor, } from "@opendatalabs/personal-server-ts-core/storage/blocks";
3
3
  import { withScopeLock } from "@opendatalabs/personal-server-ts-core/additions";
4
+ import { LegacyWriteRefusedError, } from "@opendatalabs/personal-server-ts-core/storage/legacy-write-seal";
4
5
  import { IngestPersistedError, VersionSupersededError, } from "@opendatalabs/personal-server-ts-core/contracts";
5
6
  import { createStorageReadMethods, sortEntries } from "./storage-utils.js";
6
7
  import { previewEnvelopeValue, previewJsonEnvelopePrefix, } from "@opendatalabs/personal-server-ts-core/storage/preview";
@@ -289,17 +290,37 @@ function openIndexedDb(dbName, storeName) {
289
290
  request.onerror = () => reject(request.error);
290
291
  });
291
292
  }
293
+ /**
294
+ * Run one request in its own transaction. Resolves with the request's result
295
+ * only when the transaction has committed: a request's `success` event comes
296
+ * before the commit, and the transaction can still abort after it (quota, a
297
+ * closed tab, an explicit `abort()`). Rejects on a request error and on abort.
298
+ */
292
299
  function runIndexedDbTransaction(options, mode, callback) {
293
300
  return openIndexedDb(options.dbName, options.storeName).then((db) => new Promise((resolve, reject) => {
294
- const transaction = db.transaction(options.storeName, mode);
295
- const store = transaction.objectStore(options.storeName);
296
- const request = callback(store);
297
- request.onsuccess = () => resolve(request.result);
298
- request.onerror = () => reject(request.error);
299
- transaction.oncomplete = () => db.close();
301
+ let transaction;
302
+ let request;
303
+ try {
304
+ transaction = db.transaction(options.storeName, mode);
305
+ request = callback(transaction.objectStore(options.storeName));
306
+ }
307
+ catch (err) {
308
+ db.close();
309
+ reject(err);
310
+ return;
311
+ }
312
+ transaction.oncomplete = () => {
313
+ db.close();
314
+ resolve(request.result);
315
+ };
300
316
  transaction.onerror = () => {
301
317
  db.close();
302
- reject(transaction.error);
318
+ reject(transaction.error ?? request.error);
319
+ };
320
+ transaction.onabort = () => {
321
+ db.close();
322
+ reject(transaction.error ??
323
+ new DOMException("The transaction was aborted", "AbortError"));
303
324
  };
304
325
  }));
305
326
  }
@@ -331,7 +352,26 @@ export function createIndexedDbPsLitePersistence(options = {}) {
331
352
  },
332
353
  };
333
354
  }
334
- export async function createPersistentPsLiteStorage(adapter, persistence = createIndexedDbPsLitePersistence(), dataFileStore) {
355
+ /**
356
+ * Persistent Lite storage. `legacyWriteSeal` is required: the storage makes a
357
+ * legacy version durable only inside `legacyWriteSeal.commit`, and removes
358
+ * data only inside `legacyWriteSeal.remove` (or under the `hold` of a caller
359
+ * that is already inside `commit`).
360
+ *
361
+ * Lock order, for every sealed method: the seal first, then the path lock,
362
+ * then the scope lock, then the index queue. A method takes the seal once, at
363
+ * its outermost call, and hands the hold to the helpers it calls; the seal is
364
+ * never taken from inside a path lock, the queue or a scope lock, so a write
365
+ * that waits behind a queued On cannot be waited on by a call that holds the
366
+ * lock the On is waiting for.
367
+ *
368
+ * Not sealed, because they add no visible version: `updateFileId`,
369
+ * `updateDataPointId`, `updateEntryVersion` (they rewrite a row that is
370
+ * already visible), and the sidecars (block manifests and payloads, first-seen
371
+ * ledgers).
372
+ */
373
+ export async function createPersistentPsLiteStorage(adapter, persistenceAdapter, dataFileStore, legacyWriteSeal) {
374
+ const persistence = persistenceAdapter ?? createIndexedDbPsLitePersistence();
335
375
  let state = normalizeState(await persistence.read());
336
376
  const fallbackEnvelopes = new Map(state.envelopes.map((envelope) => [
337
377
  envelopePath(envelope.scope, envelope.collectedAt),
@@ -368,6 +408,25 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
368
408
  persistQueue = run.then(() => undefined, () => undefined);
369
409
  return run;
370
410
  }
411
+ /**
412
+ * Write a version's blob. Caller holds the path lock and is inside
413
+ * `legacyWriteSeal.commit`. Returns the size in bytes.
414
+ */
415
+ async function storeEnvelopeBlob(_hold, path, envelope) {
416
+ const size = await fileStore.writeEnvelope(path, envelope);
417
+ if (fileStore.kind !== "indexeddb") {
418
+ await fallbackStore.deleteEnvelope(path);
419
+ }
420
+ return size;
421
+ }
422
+ /**
423
+ * Run a pure removal. Inside a `commit` (the caller passes its `hold`) it
424
+ * runs as is, because the lock is already held and is not re-entrant;
425
+ * otherwise it takes the seal's shared lock, which is open in every mode.
426
+ */
427
+ function removal(hold, work) {
428
+ return hold ? work() : legacyWriteSeal.remove(work);
429
+ }
371
430
  function snapshotOf(source) {
372
431
  return {
373
432
  ...source,
@@ -407,9 +466,10 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
407
466
  return true;
408
467
  });
409
468
  }
410
- function updateEntryField(path, patch) {
469
+ function updateEntryField(path, patch, expectedId) {
411
470
  return updateIndex((current) => {
412
- if (!current.entries.some((entry) => entry.path === path))
471
+ const row = current.entries.find((entry) => entry.path === path);
472
+ if (!row || (expectedId !== undefined && row.id !== expectedId))
413
473
  return null;
414
474
  return {
415
475
  ...current,
@@ -433,12 +493,13 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
433
493
  }).catch(() => undefined);
434
494
  }
435
495
  /**
436
- * Install a row at its path, replacing any row there. Caller holds the path
437
- * lock. With `onlyIfNewest`, the row is refused when the scope already has a
438
- * later `collectedAt`; that check runs in the index queue, so it is atomic
439
- * with the publication.
496
+ * Install a row at its path, replacing any row there. A row is a visible
497
+ * legacy version, so the caller must be inside `legacyWriteSeal.commit` (it
498
+ * holds the proof) and holds the path lock. With `onlyIfNewest`, the row is
499
+ * refused when the scope already has a later `collectedAt`; that check runs
500
+ * in the index queue, so it is atomic with the publication.
440
501
  */
441
- async function installEntry(entry, options = {}) {
502
+ async function installEntry(_hold, entry, options = {}) {
442
503
  let indexed;
443
504
  await updateIndex((current) => {
444
505
  if (options.onlyIfNewest &&
@@ -468,12 +529,28 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
468
529
  });
469
530
  return indexed;
470
531
  }
532
+ // Rows grouped by scope, rebuilt once per index state (state is replaced,
533
+ // never edited in place), so a lookup is O(scope rows) instead of O(all
534
+ // rows): the upload worker looks up every coalesced scope it plans.
535
+ let scopeGroups;
471
536
  function entriesForScope(scope) {
472
- return sortEntries(state.entries.filter((entry) => entry.scope === scope));
537
+ if (scopeGroups?.source !== state.entries) {
538
+ const groups = new Map();
539
+ for (const entry of state.entries) {
540
+ const group = groups.get(entry.scope);
541
+ if (group)
542
+ group.push(entry);
543
+ else
544
+ groups.set(entry.scope, [entry]);
545
+ }
546
+ scopeGroups = { source: state.entries, groups };
547
+ }
548
+ return sortEntries(scopeGroups.groups.get(scope) ?? []);
473
549
  }
474
550
  const storagePort = {
475
551
  kind: adapter.kind === "custom" ? "custom" : "browser-indexeddb-opfs",
476
552
  capabilities,
553
+ legacyWriteSeal,
477
554
  ...createStorageReadMethods(() => state.entries, entriesForScope),
478
555
  findByFileId(fileId) {
479
556
  return state.entries.find((entry) => entry.fileId === fileId);
@@ -496,6 +573,30 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
496
573
  ? entries
497
574
  : entries.slice(0, options.limit);
498
575
  },
576
+ findUnsyncedPage({ afterId, limit }) {
577
+ const page = [];
578
+ for (const entry of state.entries) {
579
+ if (entry.dataPointId === null && entry.id > afterId)
580
+ page.push(entry);
581
+ }
582
+ return page.sort((a, b) => a.id - b.id).slice(0, limit);
583
+ },
584
+ countUnsynced() {
585
+ let count = 0;
586
+ for (const entry of state.entries) {
587
+ if (entry.dataPointId === null)
588
+ count += 1;
589
+ }
590
+ return count;
591
+ },
592
+ maxUnsyncedId() {
593
+ let max = 0;
594
+ for (const entry of state.entries) {
595
+ if (entry.dataPointId === null && entry.id > max)
596
+ max = entry.id;
597
+ }
598
+ return max;
599
+ },
499
600
  async readEnvelope(scope, collectedAt) {
500
601
  const path = envelopePath(scope, collectedAt);
501
602
  const envelope = (await fileStore.readEnvelope(path)) ??
@@ -505,33 +606,32 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
505
606
  }
506
607
  return envelope;
507
608
  },
508
- async writeEnvelope(envelope) {
609
+ // A blob with no row is invisible, but it is the bytes of a legacy
610
+ // version: a refused write must not write them. The whole write is sealed,
611
+ // so a refusal happens before any file is touched and there is nothing to
612
+ // clean up.
613
+ writeEnvelope(envelope) {
509
614
  const path = envelopePath(envelope.scope, envelope.collectedAt);
510
- const sizeBytes = await withPathLock(path, async () => {
511
- const size = await fileStore.writeEnvelope(path, envelope);
512
- if (fileStore.kind !== "indexeddb") {
513
- await fallbackStore.deleteEnvelope(path);
514
- }
515
- return size;
615
+ return legacyWriteSeal.commit(async (blobHold) => {
616
+ const sizeBytes = await withPathLock(path, () => storeEnvelopeBlob(blobHold, path, envelope));
617
+ await persist();
618
+ return {
619
+ path,
620
+ relativePath: path,
621
+ sizeBytes,
622
+ };
516
623
  });
517
- await persist();
518
- return {
519
- path,
520
- relativePath: path,
521
- sizeBytes,
522
- };
523
624
  },
524
- async writeEnvelopeAndIndex(envelope, entry) {
625
+ writeEnvelopeAndIndex(envelope, entry) {
525
626
  const path = envelopePath(envelope.scope, envelope.collectedAt);
526
- // The blob and its row are one step under the path lock, so no erase or
627
+ // Sealed as one step, blob and row: a refusal comes before the blob is
628
+ // written, so a refused write leaves neither. Under the seal, the blob
629
+ // and its row are one step under the path lock, so no erase or
527
630
  // replacement of this path runs between them.
528
- return withPathLock(path, async () => {
529
- const size = await fileStore.writeEnvelope(path, envelope);
530
- if (fileStore.kind !== "indexeddb") {
531
- await fallbackStore.deleteEnvelope(path);
532
- }
631
+ return legacyWriteSeal.commit((blobAndRowHold) => withPathLock(path, async () => {
632
+ const size = await storeEnvelopeBlob(blobAndRowHold, path, envelope);
533
633
  try {
534
- return await installEntry({
634
+ return await installEntry(blobAndRowHold, {
535
635
  ...entry,
536
636
  scope: envelope.scope,
537
637
  collectedAt: envelope.collectedAt,
@@ -543,7 +643,7 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
543
643
  // The bytes are durable here; the caller's recovery depends on that.
544
644
  throw new IngestPersistedError(path, error);
545
645
  }
546
- });
646
+ }));
547
647
  },
548
648
  ...(fileStore.openEnvelopeWriter
549
649
  ? {
@@ -554,29 +654,40 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
554
654
  write: (text) => file.write(text),
555
655
  abort: () => file.abort(),
556
656
  async commit(entry, options) {
557
- // The rename and the index install are one step under the path
558
- // lock, so no erase of this path runs between them.
559
- return withPathLock(path, async () => {
560
- const sizeBytes = await file.commit();
561
- try {
562
- // The index entry is what makes the version visible.
563
- await installEntry({
564
- ...entry,
565
- scope,
566
- collectedAt,
567
- path,
568
- sizeBytes,
569
- }, options);
570
- }
571
- catch (error) {
572
- await dropUnindexedEnvelope(path);
573
- throw error;
657
+ // The bytes were streamed to a staged file before this call
658
+ // and are invisible there. The seal covers the rename and the
659
+ // index install, which are one step under the path lock, so
660
+ // no erase of this path runs between them. A refused commit
661
+ // discards the staged file.
662
+ try {
663
+ return await legacyWriteSeal.commit((renameHold) => withPathLock(path, async () => {
664
+ const sizeBytes = await file.commit();
665
+ try {
666
+ // The index entry is what makes the version visible.
667
+ await installEntry(renameHold, {
668
+ ...entry,
669
+ scope,
670
+ collectedAt,
671
+ path,
672
+ sizeBytes,
673
+ }, options);
674
+ }
675
+ catch (error) {
676
+ await dropUnindexedEnvelope(path);
677
+ throw error;
678
+ }
679
+ if (fileStore.kind !== "indexeddb") {
680
+ await fallbackStore.deleteEnvelope(path);
681
+ }
682
+ return { path, relativePath: path, sizeBytes };
683
+ }));
684
+ }
685
+ catch (error) {
686
+ if (error instanceof LegacyWriteRefusedError) {
687
+ await file.abort();
574
688
  }
575
- if (fileStore.kind !== "indexeddb") {
576
- await fallbackStore.deleteEnvelope(path);
577
- }
578
- return { path, relativePath: path, sizeBytes };
579
- });
689
+ throw error;
690
+ }
580
691
  },
581
692
  };
582
693
  },
@@ -767,7 +878,7 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
767
878
  await persist();
768
879
  },
769
880
  insertEntry(entry) {
770
- return withPathLock(entry.path, () => installEntry(entry));
881
+ return legacyWriteSeal.commit((rowHold) => withPathLock(entry.path, () => installEntry(rowHold, entry)));
771
882
  },
772
883
  updateFileId(path, fileId) {
773
884
  return updateEntryField(path, { fileId });
@@ -775,11 +886,11 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
775
886
  findLatestVersionByScope(scope) {
776
887
  return state.entries.reduce((max, e) => (e.scope === scope && e.version > max ? e.version : max), 0);
777
888
  },
778
- updateDataPointId(path, dataPointId) {
779
- return updateEntryField(path, { dataPointId });
889
+ updateDataPointId(path, dataPointId, expectedId) {
890
+ return updateEntryField(path, { dataPointId }, expectedId);
780
891
  },
781
- updateEntryVersion(path, version) {
782
- return updateEntryField(path, { version });
892
+ updateEntryVersion(path, version, expectedId) {
893
+ return updateEntryField(path, { version }, expectedId);
783
894
  },
784
895
  async readFirstSeenLedger(scope) {
785
896
  if (!firstSeenLedgers.has(scope)) {
@@ -797,50 +908,53 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
797
908
  firstSeenLedgers.delete(scope);
798
909
  await persistence.deleteAux?.(ledgerAux(scope));
799
910
  },
800
- // The ledger barrier runs under the scope lock (a rebuild in flight cannot
801
- // write it back). Each row is then erased under its own path lock, the
802
- // same owner an ordinary write takes, so no write to a path interleaves
803
- // with the erase of that path. The scope lock is never held across those
804
- // path locks: lock order is path, scope, queue.
805
- async deleteScope(scope) {
806
- await withScopeLock(scope, async () => {
807
- await dropLedger(scope);
808
- for (const entry of entriesForScope(scope)) {
809
- readyBlockManifests.delete(blockManifestPath(scope, entry.collectedAt));
810
- }
811
- // Repeat the barrier on retry, even if a failed write cleared the set.
812
- await persist();
911
+ // The whole removal runs under the seal's shared lock (or the caller's
912
+ // hold). The ledger barrier runs under the scope lock (a rebuild in flight
913
+ // cannot write it back). Each row is then erased under its own path lock,
914
+ // the same owner an ordinary write takes, so no write to a path
915
+ // interleaves with the erase of that path. The scope lock is never held
916
+ // across those path locks: lock order is seal, path, scope, queue.
917
+ deleteScope(scope, hold) {
918
+ return removal(hold, async () => {
919
+ await withScopeLock(scope, async () => {
920
+ await dropLedger(scope);
921
+ for (const entry of entriesForScope(scope)) {
922
+ readyBlockManifests.delete(blockManifestPath(scope, entry.collectedAt));
923
+ }
924
+ // Repeat the barrier on retry, even if a failed write cleared the set.
925
+ await persist();
926
+ });
927
+ // A row a later write adds is not in this snapshot; it is not deleted.
928
+ const paths = await enqueue(async () => Array.from(new Set(state.entries
929
+ .filter((entry) => entry.scope === scope)
930
+ .map((entry) => entry.path))));
931
+ // Each path settles on its own, so one failed blob delete does not stop
932
+ // the others. Rows whose delete failed stay, for the next retry.
933
+ const outcomes = await Promise.allSettled(paths.map((path) => withPathLock(path, () => eraseUnderPathLock(path, (e) => e.scope === scope && e.path === path))));
934
+ const failed = outcomes.find((outcome) => outcome.status === "rejected");
935
+ if (failed)
936
+ throw failed.reason;
937
+ return outcomes.filter((outcome) => outcome.status === "fulfilled" && outcome.value === true).length;
813
938
  });
814
- // A row a later write adds is not in this snapshot; it is not deleted.
815
- const paths = await enqueue(async () => Array.from(new Set(state.entries
816
- .filter((entry) => entry.scope === scope)
817
- .map((entry) => entry.path))));
818
- // Each path settles on its own, so one failed blob delete does not stop
819
- // the others. Rows whose delete failed stay, for the next retry.
820
- const outcomes = await Promise.allSettled(paths.map((path) => withPathLock(path, () => eraseUnderPathLock(path, (e) => e.scope === scope && e.path === path))));
821
- const failed = outcomes.find((outcome) => outcome.status === "rejected");
822
- if (failed)
823
- throw failed.reason;
824
- return outcomes.filter((outcome) => outcome.status === "fulfilled" && outcome.value === true).length;
825
939
  },
826
940
  // Unfenced: by (scope, collectedAt) alone. Fenced: only the row the caller
827
941
  // decided on, by its id and path.
828
- deleteVersion(scope, collectedAt, fence) {
942
+ deleteVersion(scope, collectedAt, fence, hold) {
829
943
  // The path is fixed by (scope, collectedAt), so the lock is known now.
830
944
  const path = envelopePath(scope, collectedAt);
831
- return withPathLock(path, () => eraseUnderPathLock(path, (e) => e.scope === scope &&
945
+ return removal(hold, () => withPathLock(path, () => eraseUnderPathLock(path, (e) => e.scope === scope &&
832
946
  e.collectedAt === collectedAt &&
833
947
  e.path === path &&
834
- (fence === undefined || e.id === fence.id))).then((outcome) => outcome === true);
948
+ (fence === undefined || e.id === fence.id)))).then((outcome) => outcome === true);
835
949
  },
836
950
  // Only while the envelope is absent: the check and the erase both run
837
951
  // under the path lock that every write of this path takes. A store that
838
952
  // throws on read is a fault, not absence, and the row stays.
839
- deleteVersionIfEnvelopeMissing(scope, collectedAt, fence) {
953
+ deleteVersionIfEnvelopeMissing(scope, collectedAt, fence, hold) {
840
954
  const path = envelopePath(scope, collectedAt);
841
955
  if (fence.path !== path)
842
956
  return Promise.resolve(false);
843
- return withPathLock(path, async () => {
957
+ return removal(hold, () => withPathLock(path, async () => {
844
958
  const present = (await fileStore.readEnvelope(path)) ??
845
959
  (await fallbackStore.readEnvelope(path));
846
960
  if (present)
@@ -850,19 +964,21 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
850
964
  e.path === path &&
851
965
  e.id === fence.id);
852
966
  return outcome === true;
853
- });
967
+ }));
854
968
  },
855
- async deleteByFileId(fileId) {
856
- // The fileId only names the path whose lock to take. The row is
857
- // re-resolved under that lock, and looked again if it moved.
858
- for (;;) {
859
- const path = await enqueue(async () => state.entries.find((e) => e.fileId === fileId)?.path ?? null);
860
- if (path === null)
861
- return false;
862
- const outcome = await withPathLock(path, () => eraseUnderPathLock(path, (e) => e.fileId === fileId));
863
- if (outcome !== "moved")
864
- return outcome;
865
- }
969
+ deleteByFileId(fileId, hold) {
970
+ return removal(hold, async () => {
971
+ // The fileId only names the path whose lock to take. The row is
972
+ // re-resolved under that lock, and looked again if it moved.
973
+ for (;;) {
974
+ const path = await enqueue(async () => state.entries.find((e) => e.fileId === fileId)?.path ?? null);
975
+ if (path === null)
976
+ return false;
977
+ const outcome = await withPathLock(path, () => eraseUnderPathLock(path, (e) => e.fileId === fileId));
978
+ if (outcome !== "moved")
979
+ return outcome;
980
+ }
981
+ });
866
982
  },
867
983
  };
868
984
  /**
@@ -902,9 +1018,10 @@ export async function createPersistentPsLiteStorage(adapter, persistence = creat
902
1018
  * interleave. The slot is taken synchronously at call time, so calls run in
903
1019
  * call order.
904
1020
  *
905
- * Lock order, never reversed: path lock, then scope lock, then the index
906
- * queue. Never take a path lock while holding the scope lock or inside the
907
- * queue (`enqueue`), or two operations can wait on each other.
1021
+ * Lock order, never reversed: the legacy-write seal, then the path lock,
1022
+ * then the scope lock, then the index queue. Never take a path lock while
1023
+ * holding the scope lock or inside the queue (`enqueue`), and never take the
1024
+ * seal inside any of them, or two operations can wait on each other.
908
1025
  */
909
1026
  const pathTails = new Map();
910
1027
  function withPathLock(path, fn) {