@tearleads/client-sdk 0.1.252 → 0.1.254

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 (155) hide show
  1. package/Tearleads.js +3 -3
  2. package/Tearleads.js.map +1 -1
  3. package/client/containerContents/documentDiscovery.js +0 -2
  4. package/client/containerContents/documentDiscovery.js.map +1 -1
  5. package/client/containerContents/index.d.ts +2 -0
  6. package/client/containerContents/index.js +4 -9
  7. package/client/containerContents/index.js.map +1 -1
  8. package/client/containerContents/storeRuntime.d.ts +17 -0
  9. package/client/containerContents/storeRuntime.js +31 -0
  10. package/client/containerContents/storeRuntime.js.map +1 -0
  11. package/client/deviceFirst/index.d.ts +2 -3
  12. package/client/deviceFirst/index.js +44 -22
  13. package/client/deviceFirst/index.js.map +1 -1
  14. package/client/runtime/workflowRuntime.d.ts +2 -0
  15. package/client/runtime/workflowRuntime.js +13 -6
  16. package/client/runtime/workflowRuntime.js.map +1 -1
  17. package/data/persistence/container-contents/containerContentsPersistenceTypes.d.ts +7 -10
  18. package/data/persistence/container-contents/containerContentsPersistenceTypes.js.map +1 -1
  19. package/data/persistence/container-contents/containerDeletionPersistence.js +5 -1
  20. package/data/persistence/container-contents/containerDeletionPersistence.js.map +1 -1
  21. package/data/persistence/container-contents/containerHydrationPersistence.js +1 -1
  22. package/data/persistence/container-contents/containerHydrationPersistence.js.map +1 -1
  23. package/data/persistence/container-contents/containerMetadataMutationPersistence.js +11 -4
  24. package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
  25. package/data/persistence/container-contents/containerRemovalMetadata.js +47 -0
  26. package/data/persistence/container-contents/containerRemovalMetadata.js.map +1 -0
  27. package/data/persistence/container-contents/containerRemovalPlacements.js +22 -0
  28. package/data/persistence/container-contents/containerRemovalPlacements.js.map +1 -0
  29. package/data/persistence/container-contents/containerSyncWatermarkPersistence.d.ts +2 -1
  30. package/data/persistence/container-contents/containerSyncWatermarkPersistence.js +10 -2
  31. package/data/persistence/container-contents/containerSyncWatermarkPersistence.js.map +1 -1
  32. package/data/persistence/documents/documentDiscoveryCandidate.d.ts +1 -0
  33. package/data/persistence/documents/documentDiscoveryCandidate.js +3 -1
  34. package/data/persistence/documents/documentDiscoveryCandidate.js.map +1 -1
  35. package/data/persistence/documents/documentDiscoveryEvidencePersistence.d.ts +1 -1
  36. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js +16 -4
  37. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js.map +1 -1
  38. package/data/sqlite/migrations/localSchemaMigrations.js +11 -0
  39. package/data/sqlite/migrations/localSchemaMigrations.js.map +1 -1
  40. package/data/sqlite/schema/containerSyncWatermarkSchema.js +6 -3
  41. package/data/sqlite/schema/containerSyncWatermarkSchema.js.map +1 -1
  42. package/data/sqlite/schema/index.d.ts +17 -0
  43. package/data/sync/syncCoordinator.js +17 -10
  44. package/data/sync/syncCoordinator.js.map +1 -1
  45. package/internal/api-client/ApiClient.d.ts +1 -0
  46. package/internal/api-client/client/apiClientContext.d.ts +1 -0
  47. package/internal/api-client/client/containerReads.d.ts +1 -0
  48. package/internal/api-client/requestInternals.js +14 -6
  49. package/internal/api-client/requestInternals.js.map +1 -1
  50. package/internal/validators/operation/containerReads.d.ts +29 -2
  51. package/internal/validators/operation/containerReads.js +2 -1
  52. package/internal/validators/operation/containerReads.js.map +1 -1
  53. package/internal/validators/request/containerDocumentLanes.d.ts +7 -0
  54. package/internal/validators/request/containerDocumentLanes.js +6 -0
  55. package/internal/validators/request/containerDocumentLanes.js.map +1 -1
  56. package/internal/validators/response/container.d.ts +8 -0
  57. package/internal/validators/response/container.js +1 -0
  58. package/internal/validators/response/container.js.map +1 -1
  59. package/internal/validators/response/containerParentLanes.d.ts +2 -0
  60. package/package.json +1 -1
  61. package/stores/container-contents/containerContentsStore.d.ts +3 -9
  62. package/stores/container-contents/containerContentsStore.js +24 -21
  63. package/stores/container-contents/containerContentsStore.js.map +1 -1
  64. package/stores/container-contents/containerState/storeScope.js +6 -0
  65. package/stores/container-contents/containerState/storeScope.js.map +1 -0
  66. package/stores/container-contents/hydration/backgroundHydration.js +2 -1
  67. package/stores/container-contents/hydration/backgroundHydration.js.map +1 -1
  68. package/stores/container-contents/hydration/reconnectRefresh.d.ts +10 -0
  69. package/stores/container-contents/hydration/reconnectRefresh.js +68 -0
  70. package/stores/container-contents/hydration/reconnectRefresh.js.map +1 -0
  71. package/stores/container-contents/hydration/reconnectRefreshMachine.js +49 -0
  72. package/stores/container-contents/hydration/reconnectRefreshMachine.js.map +1 -0
  73. package/stores/container-contents/hydration/remoteHydrationRefresh.js +4 -1
  74. package/stores/container-contents/hydration/remoteHydrationRefresh.js.map +1 -1
  75. package/stores/container-contents/hydration/remoteHydrationRequest.js +79 -10
  76. package/stores/container-contents/hydration/remoteHydrationRequest.js.map +1 -1
  77. package/stores/container-contents/hydration/startupHydration.js +2 -9
  78. package/stores/container-contents/hydration/startupHydration.js.map +1 -1
  79. package/stores/container-contents/hydration/treeResetMachine.js +61 -0
  80. package/stores/container-contents/hydration/treeResetMachine.js.map +1 -0
  81. package/stores/container-contents/localRefresh.js +28 -12
  82. package/stores/container-contents/localRefresh.js.map +1 -1
  83. package/stores/container-contents/remoteEvents/remoteContainerIngestion.js +4 -1
  84. package/stores/container-contents/remoteEvents/remoteContainerIngestion.js.map +1 -1
  85. package/stores/container-contents/remoteWrites/writeGeneration.js +5 -2
  86. package/stores/container-contents/remoteWrites/writeGeneration.js.map +1 -1
  87. package/stores/container-contents/state.d.ts +3 -7
  88. package/stores/container-contents/state.js +27 -16
  89. package/stores/container-contents/state.js.map +1 -1
  90. package/stores/container-contents/syncAgent/index.d.ts +7 -2
  91. package/stores/container-contents/syncAgent/index.js +77 -49
  92. package/stores/container-contents/syncAgent/index.js.map +1 -1
  93. package/stores/container-contents/syncAgent/syncAgentTypes.d.ts +5 -2
  94. package/stores/container-contents/syncAgent/syncAgentTypes.js.map +1 -1
  95. package/stores/container-contents/syncAgent/syncLaneIteration.js +6 -3
  96. package/stores/container-contents/syncAgent/syncLaneIteration.js.map +1 -1
  97. package/stores/local-projection/localProjectionStore.js +24 -15
  98. package/stores/local-projection/localProjectionStore.js.map +1 -1
  99. package/stores/local-projection/registry.js +2 -2
  100. package/stores/local-projection/registry.js.map +1 -1
  101. package/workflows/container-contents/container-state/create/createChild.js +3 -15
  102. package/workflows/container-contents/container-state/create/createChild.js.map +1 -1
  103. package/workflows/container-contents/containerPersistence.d.ts +4 -8
  104. package/workflows/container-contents/containerPersistence.js +22 -12
  105. package/workflows/container-contents/containerPersistence.js.map +1 -1
  106. package/workflows/container-contents/documentDiscovery/containerDocumentListing.js +52 -4
  107. package/workflows/container-contents/documentDiscovery/containerDocumentListing.js.map +1 -1
  108. package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js +4 -2
  109. package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js.map +1 -1
  110. package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js +17 -12
  111. package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js.map +1 -1
  112. package/workflows/container-contents/documentDiscovery/documentDiscoveryTypes.js.map +1 -1
  113. package/workflows/container-contents/documentDiscovery/publicationWatermark.js +15 -0
  114. package/workflows/container-contents/documentDiscovery/publicationWatermark.js.map +1 -0
  115. package/workflows/container-contents/localState.js +3 -0
  116. package/workflows/container-contents/localState.js.map +1 -1
  117. package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.d.ts +12 -0
  118. package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js +93 -0
  119. package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js.map +1 -0
  120. package/workflows/container-contents/remoteHydration/index.d.ts +3 -11
  121. package/workflows/container-contents/remoteHydration/index.js +17 -66
  122. package/workflows/container-contents/remoteHydration/index.js.map +1 -1
  123. package/workflows/container-contents/remoteHydration/lanes/parentLaneFetch.js +3 -2
  124. package/workflows/container-contents/remoteHydration/lanes/parentLaneFetch.js.map +1 -1
  125. package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js +25 -0
  126. package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js.map +1 -0
  127. package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js +107 -0
  128. package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js.map +1 -0
  129. package/workflows/container-contents/remoteHydration/principalPolicyCache.js +57 -5
  130. package/workflows/container-contents/remoteHydration/principalPolicyCache.js.map +1 -1
  131. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js +129 -84
  132. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js.map +1 -1
  133. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js +32 -10
  134. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js.map +1 -1
  135. package/workflows/container-contents/remoteHydration/tombstoneApplication.js +67 -24
  136. package/workflows/container-contents/remoteHydration/tombstoneApplication.js.map +1 -1
  137. package/workflows/container-contents/remoteHydration/types.d.ts +16 -1
  138. package/workflows/container-contents/remoteHydration/types.js.map +1 -1
  139. package/workflows/container-contents/remoteReset/remoteResetCursorScope.js +3 -3
  140. package/workflows/container-contents/remoteReset/remoteResetCursorScope.js.map +1 -1
  141. package/workflows/container-contents/runtime.d.ts +3 -0
  142. package/workflows/container-contents/runtime.js.map +1 -1
  143. package/workflows/container-contents/storedContainerState.js +17 -7
  144. package/workflows/container-contents/storedContainerState.js.map +1 -1
  145. package/workflows/container-contents/syncLane.d.ts +1 -0
  146. package/workflows/container-contents/syncLane.js +9 -1
  147. package/workflows/container-contents/syncLane.js.map +1 -1
  148. package/workflows/container-contents/syncPause/discoveryGuard.js +6 -2
  149. package/workflows/container-contents/syncPause/discoveryGuard.js.map +1 -1
  150. package/workflows/container-contents/tombstones/documentTombstoneGate.js +6 -3
  151. package/workflows/container-contents/tombstones/documentTombstoneGate.js.map +1 -1
  152. package/workflows/shared/runtimeInput.d.ts +2 -0
  153. package/workflows/shared/runtimeInput.js.map +1 -1
  154. package/workflows/container-contents/remoteHydration/completeHydration.js +0 -25
  155. package/workflows/container-contents/remoteHydration/completeHydration.js.map +0 -1
@@ -63,11 +63,11 @@ export class DocumentDiscoveryEvidenceRepository {
63
63
  async isCurrent(generation) {
64
64
  return isDocumentDiscoveryGenerationCurrent(await this.read(), generation);
65
65
  }
66
- stage(inputs, generation, removed = []) {
66
+ stage(inputs, generation, removed = [], conflictingDocumentIds = new Set()) {
67
67
  return this.write(async (db) => {
68
68
  if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))
69
69
  return false;
70
- if (inputs.length === 0 && removed.length === 0)
70
+ if (!inputs.length && !removed.length && !conflictingDocumentIds.size)
71
71
  return true;
72
72
  await db.transaction(async (tx) => {
73
73
  const rows = inputs.flatMap((input) => isDiscoveredDocumentCandidate(input)
@@ -87,7 +87,10 @@ export class DocumentDiscoveryEvidenceRepository {
87
87
  // One multi-row upsert per chunk. SQLite applies a statement's rows
88
88
  // in order, so a repeated placement updates the row the previous
89
89
  // entry wrote, exactly as one statement per entry did.
90
- const unchanged = sql `${pending.inputJson} = excluded.input_json`;
90
+ // A later listing can replace a pending hint while its unlink is
91
+ // already behind the cursor. Keep the required current-head read.
92
+ const nextInput = sql `case when json_extract(${pending.inputJson}, '$.requiresCurrentHead') then json_set(excluded.input_json, '$.requiresCurrentHead', json('true')) else excluded.input_json end`;
93
+ const unchanged = sql `${pending.inputJson} = (${nextInput})`;
91
94
  for (const chunk of chunked(rows, sqliteInsertBatchSize(pending))) {
92
95
  await tx
93
96
  .insert(pending)
@@ -97,7 +100,7 @@ export class DocumentDiscoveryEvidenceRepository {
97
100
  set: {
98
101
  accessEpoch: excludedValue(pending.accessEpoch),
99
102
  generation: excludedValue(pending.generation),
100
- inputJson: excludedValue(pending.inputJson),
103
+ inputJson: nextInput,
101
104
  retryAt: sql `case when ${unchanged} then ${pending.retryAt} else 0 end`,
102
105
  attempts: sql `case when ${unchanged} then ${pending.attempts} else 0 end`,
103
106
  },
@@ -105,6 +108,15 @@ export class DocumentDiscoveryEvidenceRepository {
105
108
  })
106
109
  .run();
107
110
  }
111
+ for (const ids of batches([...conflictingDocumentIds])) {
112
+ await tx
113
+ .update(pending)
114
+ .set({
115
+ inputJson: sql `json_set(${pending.inputJson}, '$.requiresCurrentHead', json('true'))`,
116
+ })
117
+ .where(and(inArray(pending.documentId, ids), lte(pending.generation, generation)))
118
+ .run();
119
+ }
108
120
  // These are untrusted, unapplied hints only. Removing an existing local
109
121
  // placement still requires the separate signed tombstone evidence gate.
110
122
  for (const batch of remoteResetBatches(removed)) {
@@ -1 +1 @@
1
- {"version":3,"file":"documentDiscoveryEvidencePersistence.js","sourceRoot":"","sources":["documentDiscoveryEvidencePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,0BAA0B,GAC3B,MAAM,qDAAqD,CAAC;AAC7D,OAAO,EAAE,iCAAiC,EAAE,MAAM,8CAA8C,CAAC;AACjG,OAAO,EACL,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,iCAAiC,EAAE,MAAM,uCAAuC,CAAC;AAC1F,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAEL,eAAe,EACf,wBAAwB,GACzB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAC;AAC7F,OAAO,EAAE,yBAAyB,EAAE,MAAM,8CAA8C,CAAC;AACzF,OAAO,EAEL,6BAA6B,EAC7B,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AAY,EAAE,IAAI,CAAC,eAAe;YAClC,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;SACxE,CAAC;QACF,MAAM,EAAE;aACL,MAAM,CAAC,KAAK,CAAC;aACb,MAAM,CAAC,GAAG,CAAC;aACX,kBAAkB,CAAC;YAClB,MAAM,EAAE,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,cAAc,CAAC;YAChD,GAAG,EAAE,GAAG;YACR,QAAQ,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC;SACnD,CAAC;aACD,GAAG,EAAE,CAAC;IACX,CAAC;AACH,CAAC","sourcesContent":["import { and, asc, eq, inArray, lte, min, or, sql } from \"drizzle-orm\";\nimport { uniqueSortedStrings } from \"../../documents/shared/readers\";\nimport { readStringArray } from \"../../shared/recordReaders\";\nimport {\n documentDiscoveryHeads,\n documentDiscoverySequence,\n pendingDocumentDiscoveries,\n} from \"../../sqlite/schema/documentDiscoveryEvidenceSchema\";\nimport { documentContainerProjectionTables } from \"../../sqlite/schema/documentProjectionSchema\";\nimport {\n remoteResetBatches,\n sqliteInsertBatchSize,\n} from \"../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { excludedValue } from \"../../sqlite/sqliteUpsert\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../sqlite/sqlSchema\";\nimport { KeyingCheckpointRepository } from \"../keyingCheckpoints/keyingCheckpointRepository\";\nimport { heldTombstoneRetryDelayMs } from \"./containerDocumentTombstoneHoldsPersistence\";\nimport {\n type DiscoveredDocumentCandidate,\n isDiscoveredDocumentCandidate,\n readStoredDiscoveryCandidate,\n} from \"./documentDiscoveryCandidate\";\nimport { isDocumentDiscoveryGenerationCurrent } from \"./documentDiscoveryReset\";\nimport { placementPairsMatch } from \"./internal/containerDocumentTombstoneHolds\";\n\nexport type { DiscoveredDocumentCandidate } from \"./documentDiscoveryCandidate\";\nexport { clearDocumentDiscoveryEvidenceOnRemoteReset } from \"./documentDiscoveryReset\";\nexport interface CachedDiscoveryHead {\n readonly organizationId: string | null;\n readonly accessEpoch: number;\n readonly accessStateHash?: string;\n readonly linkedContainerIds: readonly string[];\n}\ninterface RemovedDiscoveryPlacement {\n readonly containerId: string;\n readonly documentId: string;\n}\n\nconst pending = pendingDocumentDiscoveries;\nconst heads = documentDiscoveryHeads;\nconst ID_BATCH_SIZE = 400;\nconst batches = (ids: readonly string[]) =>\n Array.from({ length: Math.ceil(ids.length / ID_BATCH_SIZE) }, (_, i) =>\n ids.slice(i * ID_BATCH_SIZE, (i + 1) * ID_BATCH_SIZE),\n );\nconst chunked = <T>(rows: readonly T[], size: number): T[][] =>\n Array.from({ length: Math.ceil(rows.length / size) }, (_, i) =>\n rows.slice(i * size, (i + 1) * size),\n );\nconst identity = (input: DiscoveredDocumentCandidate) =>\n and(\n eq(pending.containerId, input.containerId),\n eq(pending.documentId, input.documentId),\n eq(pending.inputJson, JSON.stringify(input)),\n );\n\n/**\n * Untrusted document discovery evidence bound to one executor and clock. The\n * clock (epoch milliseconds, like the `retryAt` column) schedules and reports\n * pending-discovery retries.\n */\nexport class DocumentDiscoveryEvidenceRepository {\n constructor(\n private readonly execSql: ExecSql,\n private readonly now: () => number = Date.now,\n ) {}\n\n private async read() {\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n return getClientSQLitePersistenceRuntime(this.execSql).db;\n }\n private write<T>(\n operation: (\n db: ReturnType<typeof getClientSQLitePersistenceRuntime>[\"db\"],\n ) => Promise<T>,\n ): Promise<T> {\n return runSerializedSqlMutation(this.execSql, async (locked) => {\n await ensureSqlTables(locked, documentContainerProjectionTables);\n return operation(getClientSQLitePersistenceRuntime(locked).db);\n });\n }\n async begin(): Promise<number> {\n let generation = 0;\n await this.write(async (db) => {\n const [row] = await db\n .insert(documentDiscoverySequence)\n .values({ id: \"requests\", generation: 1 })\n .onConflictDoUpdate({\n target: documentDiscoverySequence.id,\n set: {\n generation: sql`${documentDiscoverySequence.generation} + 1`,\n },\n })\n .returning({ generation: documentDiscoverySequence.generation });\n if (!row || !Number.isSafeInteger(row.generation))\n throw new Error(\"Document discovery generation is exhausted\");\n generation = row.generation;\n });\n return generation;\n }\n async isCurrent(generation: number): Promise<boolean> {\n return isDocumentDiscoveryGenerationCurrent(await this.read(), generation);\n }\n stage(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n removed: readonly RemovedDiscoveryPlacement[] = [],\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))\n return false;\n if (inputs.length === 0 && removed.length === 0) return true;\n await db.transaction(async (tx) => {\n const rows = inputs.flatMap((input) =>\n isDiscoveredDocumentCandidate(input)\n ? input.listedContainerIds.map((containerId) => ({\n containerId,\n documentId: input.documentId,\n accessEpoch: input.accessEpoch,\n generation,\n inputJson: JSON.stringify({\n ...input,\n containerId,\n listedContainerIds: [containerId],\n }),\n retryAt: 0,\n }))\n : [],\n );\n // One multi-row upsert per chunk. SQLite applies a statement's rows\n // in order, so a repeated placement updates the row the previous\n // entry wrote, exactly as one statement per entry did.\n const unchanged = sql`${pending.inputJson} = excluded.input_json`;\n for (const chunk of chunked(rows, sqliteInsertBatchSize(pending))) {\n await tx\n .insert(pending)\n .values(chunk)\n .onConflictDoUpdate({\n target: [pending.containerId, pending.documentId],\n set: {\n accessEpoch: excludedValue(pending.accessEpoch),\n generation: excludedValue(pending.generation),\n inputJson: excludedValue(pending.inputJson),\n retryAt: sql`case when ${unchanged} then ${pending.retryAt} else 0 end`,\n attempts: sql`case when ${unchanged} then ${pending.attempts} else 0 end`,\n },\n setWhere: lte(pending.generation, generation),\n })\n .run();\n }\n // These are untrusted, unapplied hints only. Removing an existing local\n // placement still requires the separate signed tombstone evidence gate.\n for (const batch of remoteResetBatches(removed)) {\n await tx\n .delete(pending)\n .where(\n and(\n placementPairsMatch(\n batch,\n pending.documentId,\n pending.containerId,\n ),\n lte(pending.generation, generation),\n ),\n )\n .run();\n }\n });\n return true;\n });\n }\n async pending(\n containerIds: readonly string[],\n limit: number,\n ): Promise<readonly DiscoveredDocumentCandidate[]> {\n const db = await this.read();\n const result: DiscoveredDocumentCandidate[] = [];\n for (const batch of batches(containerIds)) {\n if (result.length >= limit) break;\n const rows = await db\n .select({ inputJson: pending.inputJson })\n .from(pending)\n .where(\n and(\n inArray(pending.containerId, [...batch]),\n lte(pending.retryAt, this.now()),\n ),\n )\n .orderBy(asc(pending.retryAt), asc(pending.documentId))\n .limit(limit - result.length);\n result.push(\n ...rows.map((row) => readStoredDiscoveryCandidate(row.inputJson)),\n );\n }\n return result;\n }\n defer(input: DiscoveredDocumentCandidate, generation: number): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n const [row] = await db\n .select({ attempts: pending.attempts })\n .from(pending)\n .where(identity(input))\n .limit(1);\n if (!row) return;\n const attempts = Math.min(row.attempts + 1, 8);\n await db\n .update(pending)\n .set({\n attempts,\n retryAt: this.now() + heldTombstoneRetryDelayMs(attempts),\n })\n .where(identity(input))\n .run();\n });\n }\n acknowledge(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n ): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n await db.transaction(async (tx) => {\n for (const batch of remoteResetBatches(inputs))\n await tx\n .delete(pending)\n .where(or(...batch.map(identity)))\n .run();\n });\n });\n }\n async hasPending(containerIds: readonly string[]): Promise<boolean> {\n const db = await this.read();\n for (const batch of batches(containerIds)) {\n if (\n (\n await db\n .select({ documentId: pending.documentId })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]))\n .limit(1)\n ).length\n )\n return true;\n }\n return false;\n }\n async retryDelay(containerIds: readonly string[]): Promise<number | null> {\n const db = await this.read();\n let due: number | null = null;\n for (const batch of batches(containerIds)) {\n const [row] = await db\n .select({ due: min(pending.retryAt) })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]));\n if (row?.due !== null && row?.due !== undefined)\n due = Math.min(due ?? row.due, row.due);\n }\n return due === null ? null : Math.max(0, due - this.now());\n }\n async loadHead(\n documentId: string,\n manifestHash: string | null | undefined,\n ): Promise<CachedDiscoveryHead | null> {\n if (\n await new KeyingCheckpointRepository(\n this.execSql,\n ).loadDocumentPurgeCheckpoint(documentId)\n )\n return {\n organizationId: null,\n accessEpoch: Number.MAX_SAFE_INTEGER,\n linkedContainerIds: [],\n };\n if (!manifestHash) return null;\n const db = await this.read();\n const [row] = await db\n .select()\n .from(heads)\n .where(\n and(\n eq(heads.documentId, documentId),\n eq(heads.manifestHash, manifestHash),\n ),\n )\n .limit(1);\n if (!row) return null;\n // A different verifier (for example tombstone removal) can advance the\n // signed checkpoint without updating documents.accessEpoch or this cache.\n // Old-organization pins survive recovery; only the cached signed head's\n // organization can authorize reuse. Missing pins require full verification.\n const checkpoint = await new KeyingCheckpointRepository(\n this.execSql,\n ).loadAccessManifestCheckpoint(\"document\", row.organizationId, documentId);\n if (\n checkpoint?.manifestHash !== row.manifestHash ||\n checkpoint.epoch !== row.accessEpoch\n )\n return null;\n return {\n organizationId: row.organizationId,\n accessEpoch: row.accessEpoch,\n accessStateHash: row.manifestHash,\n linkedContainerIds: readStringArray(\n JSON.parse(row.linksJson),\n \"stored discovery links\",\n ),\n };\n }\n saveHead(\n documentId: string,\n head: CachedDiscoveryHead,\n generation: number,\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (\n !head.accessStateHash ||\n !head.organizationId ||\n !(await isDocumentDiscoveryGenerationCurrent(db, generation))\n )\n return false;\n const row = {\n documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(head.linkedContainerIds),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n return true;\n });\n }\n}\n\n/**\n * A document head this client signed, already checked against the server's\n * create acknowledgement and pinned as its access-manifest checkpoint in the\n * same transaction. It is the same signed head a discovery projection would\n * verify, so it seeds the exact-head cache; `loadHead` still reuses it only\n * while that checkpoint matches.\n */\nexport interface AuthoredDiscoveryHead {\n readonly documentId: string;\n readonly organizationId: string;\n readonly accessEpoch: number;\n readonly accessStateHash: string;\n readonly linkedContainerIds: readonly string[];\n}\n\n/** Writes authored heads inside the caller's serialized write transaction. */\nexport async function primeAuthoredDocumentDiscoveryHeads(\n execSql: ExecSql,\n authored: readonly AuthoredDiscoveryHead[],\n): Promise<void> {\n const db = getClientSQLitePersistenceRuntime(execSql).db;\n for (const head of authored) {\n const row = {\n documentId: head.documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(uniqueSortedStrings(head.linkedContainerIds)),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n }\n}\n"]}
1
+ {"version":3,"file":"documentDiscoveryEvidencePersistence.js","sourceRoot":"","sources":["documentDiscoveryEvidencePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,0BAA0B,GAC3B,MAAM,qDAAqD,CAAC;A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,CAAC;YAClB,MAAM,EAAE,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,cAAc,CAAC;YAChD,GAAG,EAAE,GAAG;YACR,QAAQ,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC;SACnD,CAAC;aACD,GAAG,EAAE,CAAC;IACX,CAAC;AACH,CAAC","sourcesContent":["import { and, asc, eq, inArray, lte, min, or, sql } from \"drizzle-orm\";\nimport { uniqueSortedStrings } from \"../../documents/shared/readers\";\nimport { readStringArray } from \"../../shared/recordReaders\";\nimport {\n documentDiscoveryHeads,\n documentDiscoverySequence,\n pendingDocumentDiscoveries,\n} from \"../../sqlite/schema/documentDiscoveryEvidenceSchema\";\nimport { documentContainerProjectionTables } from \"../../sqlite/schema/documentProjectionSchema\";\nimport {\n remoteResetBatches,\n sqliteInsertBatchSize,\n} from \"../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { excludedValue } from \"../../sqlite/sqliteUpsert\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../sqlite/sqlSchema\";\nimport { KeyingCheckpointRepository } from \"../keyingCheckpoints/keyingCheckpointRepository\";\nimport { heldTombstoneRetryDelayMs } from \"./containerDocumentTombstoneHoldsPersistence\";\nimport {\n type DiscoveredDocumentCandidate,\n isDiscoveredDocumentCandidate,\n readStoredDiscoveryCandidate,\n} from \"./documentDiscoveryCandidate\";\nimport { isDocumentDiscoveryGenerationCurrent } from \"./documentDiscoveryReset\";\nimport { placementPairsMatch } from \"./internal/containerDocumentTombstoneHolds\";\n\nexport type { DiscoveredDocumentCandidate } from \"./documentDiscoveryCandidate\";\nexport { clearDocumentDiscoveryEvidenceOnRemoteReset } from \"./documentDiscoveryReset\";\nexport interface CachedDiscoveryHead {\n readonly organizationId: string | null;\n readonly accessEpoch: number;\n readonly accessStateHash?: string;\n readonly linkedContainerIds: readonly string[];\n}\ninterface RemovedDiscoveryPlacement {\n readonly containerId: string;\n readonly documentId: string;\n}\n\nconst pending = pendingDocumentDiscoveries;\nconst heads = documentDiscoveryHeads;\nconst ID_BATCH_SIZE = 400;\nconst batches = (ids: readonly string[]) =>\n Array.from({ length: Math.ceil(ids.length / ID_BATCH_SIZE) }, (_, i) =>\n ids.slice(i * ID_BATCH_SIZE, (i + 1) * ID_BATCH_SIZE),\n );\nconst chunked = <T>(rows: readonly T[], size: number): T[][] =>\n Array.from({ length: Math.ceil(rows.length / size) }, (_, i) =>\n rows.slice(i * size, (i + 1) * size),\n );\nconst identity = (input: DiscoveredDocumentCandidate) =>\n and(\n eq(pending.containerId, input.containerId),\n eq(pending.documentId, input.documentId),\n eq(pending.inputJson, JSON.stringify(input)),\n );\n\n/**\n * Untrusted document discovery evidence bound to one executor and clock. The\n * clock (epoch milliseconds, like the `retryAt` column) schedules and reports\n * pending-discovery retries.\n */\nexport class DocumentDiscoveryEvidenceRepository {\n constructor(\n private readonly execSql: ExecSql,\n private readonly now: () => number = Date.now,\n ) {}\n\n private async read() {\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n return getClientSQLitePersistenceRuntime(this.execSql).db;\n }\n private write<T>(\n operation: (\n db: ReturnType<typeof getClientSQLitePersistenceRuntime>[\"db\"],\n ) => Promise<T>,\n ): Promise<T> {\n return runSerializedSqlMutation(this.execSql, async (locked) => {\n await ensureSqlTables(locked, documentContainerProjectionTables);\n return operation(getClientSQLitePersistenceRuntime(locked).db);\n });\n }\n async begin(): Promise<number> {\n let generation = 0;\n await this.write(async (db) => {\n const [row] = await db\n .insert(documentDiscoverySequence)\n .values({ id: \"requests\", generation: 1 })\n .onConflictDoUpdate({\n target: documentDiscoverySequence.id,\n set: {\n generation: sql`${documentDiscoverySequence.generation} + 1`,\n },\n })\n .returning({ generation: documentDiscoverySequence.generation });\n if (!row || !Number.isSafeInteger(row.generation))\n throw new Error(\"Document discovery generation is exhausted\");\n generation = row.generation;\n });\n return generation;\n }\n async isCurrent(generation: number): Promise<boolean> {\n return isDocumentDiscoveryGenerationCurrent(await this.read(), generation);\n }\n stage(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n removed: readonly RemovedDiscoveryPlacement[] = [],\n conflictingDocumentIds: ReadonlySet<string> = new Set(),\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))\n return false;\n if (!inputs.length && !removed.length && !conflictingDocumentIds.size)\n return true;\n await db.transaction(async (tx) => {\n const rows = inputs.flatMap((input) =>\n isDiscoveredDocumentCandidate(input)\n ? input.listedContainerIds.map((containerId) => ({\n containerId,\n documentId: input.documentId,\n accessEpoch: input.accessEpoch,\n generation,\n inputJson: JSON.stringify({\n ...input,\n containerId,\n listedContainerIds: [containerId],\n }),\n retryAt: 0,\n }))\n : [],\n );\n // One multi-row upsert per chunk. SQLite applies a statement's rows\n // in order, so a repeated placement updates the row the previous\n // entry wrote, exactly as one statement per entry did.\n // A later listing can replace a pending hint while its unlink is\n // already behind the cursor. Keep the required current-head read.\n const nextInput = sql`case when json_extract(${pending.inputJson}, '$.requiresCurrentHead') then json_set(excluded.input_json, '$.requiresCurrentHead', json('true')) else excluded.input_json end`;\n const unchanged = sql`${pending.inputJson} = (${nextInput})`;\n for (const chunk of chunked(rows, sqliteInsertBatchSize(pending))) {\n await tx\n .insert(pending)\n .values(chunk)\n .onConflictDoUpdate({\n target: [pending.containerId, pending.documentId],\n set: {\n accessEpoch: excludedValue(pending.accessEpoch),\n generation: excludedValue(pending.generation),\n inputJson: nextInput,\n retryAt: sql`case when ${unchanged} then ${pending.retryAt} else 0 end`,\n attempts: sql`case when ${unchanged} then ${pending.attempts} else 0 end`,\n },\n setWhere: lte(pending.generation, generation),\n })\n .run();\n }\n for (const ids of batches([...conflictingDocumentIds])) {\n await tx\n .update(pending)\n .set({\n inputJson: sql`json_set(${pending.inputJson}, '$.requiresCurrentHead', json('true'))`,\n })\n .where(\n and(\n inArray(pending.documentId, ids),\n lte(pending.generation, generation),\n ),\n )\n .run();\n }\n // These are untrusted, unapplied hints only. Removing an existing local\n // placement still requires the separate signed tombstone evidence gate.\n for (const batch of remoteResetBatches(removed)) {\n await tx\n .delete(pending)\n .where(\n and(\n placementPairsMatch(\n batch,\n pending.documentId,\n pending.containerId,\n ),\n lte(pending.generation, generation),\n ),\n )\n .run();\n }\n });\n return true;\n });\n }\n async pending(\n containerIds: readonly string[],\n limit: number,\n ): Promise<readonly DiscoveredDocumentCandidate[]> {\n const db = await this.read();\n const result: DiscoveredDocumentCandidate[] = [];\n for (const batch of batches(containerIds)) {\n if (result.length >= limit) break;\n const rows = await db\n .select({ inputJson: pending.inputJson })\n .from(pending)\n .where(\n and(\n inArray(pending.containerId, [...batch]),\n lte(pending.retryAt, this.now()),\n ),\n )\n .orderBy(asc(pending.retryAt), asc(pending.documentId))\n .limit(limit - result.length);\n result.push(\n ...rows.map((row) => readStoredDiscoveryCandidate(row.inputJson)),\n );\n }\n return result;\n }\n defer(input: DiscoveredDocumentCandidate, generation: number): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n const [row] = await db\n .select({ attempts: pending.attempts })\n .from(pending)\n .where(identity(input))\n .limit(1);\n if (!row) return;\n const attempts = Math.min(row.attempts + 1, 8);\n await db\n .update(pending)\n .set({\n attempts,\n retryAt: this.now() + heldTombstoneRetryDelayMs(attempts),\n })\n .where(identity(input))\n .run();\n });\n }\n acknowledge(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n ): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n await db.transaction(async (tx) => {\n for (const batch of remoteResetBatches(inputs))\n await tx\n .delete(pending)\n .where(or(...batch.map(identity)))\n .run();\n });\n });\n }\n async hasPending(containerIds: readonly string[]): Promise<boolean> {\n const db = await this.read();\n for (const batch of batches(containerIds)) {\n if (\n (\n await db\n .select({ documentId: pending.documentId })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]))\n .limit(1)\n ).length\n )\n return true;\n }\n return false;\n }\n async retryDelay(containerIds: readonly string[]): Promise<number | null> {\n const db = await this.read();\n let due: number | null = null;\n for (const batch of batches(containerIds)) {\n const [row] = await db\n .select({ due: min(pending.retryAt) })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]));\n if (row?.due !== null && row?.due !== undefined)\n due = Math.min(due ?? row.due, row.due);\n }\n return due === null ? null : Math.max(0, due - this.now());\n }\n async loadHead(\n documentId: string,\n manifestHash: string | null | undefined,\n ): Promise<CachedDiscoveryHead | null> {\n if (\n await new KeyingCheckpointRepository(\n this.execSql,\n ).loadDocumentPurgeCheckpoint(documentId)\n )\n return {\n organizationId: null,\n accessEpoch: Number.MAX_SAFE_INTEGER,\n linkedContainerIds: [],\n };\n if (!manifestHash) return null;\n const db = await this.read();\n const [row] = await db\n .select()\n .from(heads)\n .where(\n and(\n eq(heads.documentId, documentId),\n eq(heads.manifestHash, manifestHash),\n ),\n )\n .limit(1);\n if (!row) return null;\n // A different verifier (for example tombstone removal) can advance the\n // signed checkpoint without updating documents.accessEpoch or this cache.\n // Old-organization pins survive recovery; only the cached signed head's\n // organization can authorize reuse. Missing pins require full verification.\n const checkpoint = await new KeyingCheckpointRepository(\n this.execSql,\n ).loadAccessManifestCheckpoint(\"document\", row.organizationId, documentId);\n if (\n checkpoint?.manifestHash !== row.manifestHash ||\n checkpoint.epoch !== row.accessEpoch\n )\n return null;\n return {\n organizationId: row.organizationId,\n accessEpoch: row.accessEpoch,\n accessStateHash: row.manifestHash,\n linkedContainerIds: readStringArray(\n JSON.parse(row.linksJson),\n \"stored discovery links\",\n ),\n };\n }\n saveHead(\n documentId: string,\n head: CachedDiscoveryHead,\n generation: number,\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (\n !head.accessStateHash ||\n !head.organizationId ||\n !(await isDocumentDiscoveryGenerationCurrent(db, generation))\n )\n return false;\n const row = {\n documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(head.linkedContainerIds),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n return true;\n });\n }\n}\n\n/**\n * A document head this client signed, already checked against the server's\n * create acknowledgement and pinned as its access-manifest checkpoint in the\n * same transaction. It is the same signed head a discovery projection would\n * verify, so it seeds the exact-head cache; `loadHead` still reuses it only\n * while that checkpoint matches.\n */\nexport interface AuthoredDiscoveryHead {\n readonly documentId: string;\n readonly organizationId: string;\n readonly accessEpoch: number;\n readonly accessStateHash: string;\n readonly linkedContainerIds: readonly string[];\n}\n\n/** Writes authored heads inside the caller's serialized write transaction. */\nexport async function primeAuthoredDocumentDiscoveryHeads(\n execSql: ExecSql,\n authored: readonly AuthoredDiscoveryHead[],\n): Promise<void> {\n const db = getClientSQLitePersistenceRuntime(execSql).db;\n for (const head of authored) {\n const row = {\n documentId: head.documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(uniqueSortedStrings(head.linkedContainerIds)),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n }\n}\n"]}
@@ -41,6 +41,17 @@ export const localSchemaMigrations = [
41
41
  },
42
42
  version: 3,
43
43
  },
44
+ {
45
+ name: "replay_commit_ordered_discovery",
46
+ up: async (execSql, context) => {
47
+ if (!(await context.hasTable("container_sync_watermarks")))
48
+ return;
49
+ // Older SDKs could checkpoint a publication before reconciling an
50
+ // unlink or a restored subtree. Legacy UUID cursors already replay.
51
+ await execSql("ALTER TABLE container_sync_watermarks ADD COLUMN cursor_format INTEGER NOT NULL DEFAULT 1");
52
+ },
53
+ version: 4,
54
+ },
44
55
  ];
45
56
  const STEP_NAME_PATTERN = /^[a-z0-9_]+$/;
46
57
  /** Rejects a registry whose versions are not consecutive from 2. */
@@ -1 +1 @@
1
- {"version":3,"file":"localSchemaMigrations.js","sourceRoot":"","sources":["localSchemaMigrations.ts"],"names":[],"mappings":"AA0BA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAoB;IACpD,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,CAAC;CACX,CAAC;AAEF,MAAM,qBAAqB,GAAG,yCAAyC,CAAC;AAExE;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAwC;IACxE;QACE,wEAAwE;QACxE,oEAAoE;QACpE,IAAI,EAAE,iCAAiC;QACvC,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,CAAC,MAAM,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC;gBAAE,OAAO;YAC7D,KAAK,MAAM,MAAM,IAAI,CAAC,oBAAoB,EAAE,0BAA0B,CAAC,EAAE,CAAC;gBACxE,MAAM,OAAO,CACX,gBAAgB,qBAAqB,kBAAkB,MAAM,GAAG,CACjE,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,EAAE,CAAC;KACX;IACD;QACE,wEAAwE;QACxE,sEAAsE;QACtE,0EAA0E;QAC1E,wCAAwC;QACxC,IAAI,EAAE,mCAAmC;QACzC,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;YACpB,MAAM,MAAM,GAAG,MAAM,OAAO,CAC1B,oFAAoF,CACrF,CAAC;YACF,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO;YAChC,MAAM,OAAO,CACX,2DAA2D,CAC5D,CAAC;QACJ,CAAC;QACD,OAAO,EAAE,CAAC;KACX;CACF,CAAC;AAEF,MAAM,iBAAiB,GAAG,cAAc,CAAC;AAEzC,oEAAoE;AACpE,MAAM,UAAU,2BAA2B,CACzC,UAA+C;IAE/C,KAAK,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC;QACtD,MAAM,QAAQ,GAAG,qBAAqB,CAAC,OAAO,GAAG,KAAK,GAAG,CAAC,CAAC;QAC3D,IAAI,SAAS,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CACb,0BAA0B,SAAS,CAAC,IAAI,gBAAgB,SAAS,CAAC,OAAO,cAAc,QAAQ,EAAE,CAClG,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5C,MAAM,IAAI,KAAK,CACb,0BAA0B,SAAS,CAAC,OAAO,0BAA0B,CACtE,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,gBAAgB,CAC9B,UAA+C;IAE/C,OAAO;QACL,qBAAqB;QACrB,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;KAC9D,CAAC;AACJ,CAAC","sourcesContent":["import type { ExecSql } from \"../sqlExec\";\n\nexport interface LocalSchemaMigrationContext {\n /**\n * Whether a table exists. Features create their tables lazily, so a\n * migration must skip a table this database never created: the table's\n * ensure creates it later in the current shape.\n */\n hasTable(name: string): Promise<boolean>;\n}\n\nexport interface LocalSchemaStep {\n readonly version: number;\n readonly name: string;\n}\n\nexport interface LocalSchemaMigration extends LocalSchemaStep {\n /**\n * Upgrades the local database from `version - 1` to `version`. It runs on\n * the locked executor inside the migration savepoint, so it must not BEGIN,\n * COMMIT, or change `PRAGMA foreign_keys`. Write it against the shape at\n * `version - 1`, never by importing current table definitions.\n */\n up(execSql: ExecSql, context: LocalSchemaMigrationContext): Promise<void>;\n}\n\n/**\n * Version 1 is the shape the SDK's table definitions created when local\n * migrations began. It has no steps: a database from before the ledger is\n * recorded at this version, and an older shape still fails its tables'\n * required-column check.\n */\nexport const LOCAL_SCHEMA_BASELINE: LocalSchemaStep = {\n name: \"baseline\",\n version: 1,\n};\n\nconst DIRECTORY_USERS_TABLE = \"organization_read_model_directory_users\";\n\n/**\n * Ordered and append-only: version 2 first, each one after the last. Never\n * edit or remove a shipped step; a correction is a new step.\n */\nexport const localSchemaMigrations: ReadonlyArray<LocalSchemaMigration> = [\n {\n // Directory rows carry key fingerprints only; public keys come from the\n // trusted identity store. Neither column is indexed or constrained.\n name: \"drop_directory_user_public_keys\",\n up: async (execSql, context) => {\n if (!(await context.hasTable(DIRECTORY_USERS_TABLE))) return;\n for (const column of [\"signing_public_key\", \"encapsulation_public_key\"]) {\n await execSql(\n `ALTER TABLE \"${DIRECTORY_USERS_TABLE}\" DROP COLUMN \"${column}\"`,\n );\n }\n },\n version: 2,\n },\n {\n // No reader ever used a recorded failure's HTTP status; the message and\n // attempt time explain a blocked document. The column is unindexed. A\n // documents table without it (never created, or not this SDK's table in a\n // restored backup) has nothing to drop.\n name: \"drop_document_sync_failure_status\",\n up: async (execSql) => {\n const column = await execSql(\n \"SELECT name FROM pragma_table_info('documents') WHERE name = 'sync_failure_status'\",\n );\n if (column.length === 0) return;\n await execSql(\n 'ALTER TABLE \"documents\" DROP COLUMN \"sync_failure_status\"',\n );\n },\n version: 3,\n },\n];\n\nconst STEP_NAME_PATTERN = /^[a-z0-9_]+$/;\n\n/** Rejects a registry whose versions are not consecutive from 2. */\nexport function assertLocalSchemaMigrations(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): void {\n for (const [index, migration] of migrations.entries()) {\n const expected = LOCAL_SCHEMA_BASELINE.version + index + 1;\n if (migration.version !== expected) {\n throw new Error(\n `Local schema migration ${migration.name} has version ${migration.version}; expected ${expected}`,\n );\n }\n if (!STEP_NAME_PATTERN.test(migration.name)) {\n throw new Error(\n `Local schema migration ${migration.version} needs a snake_case name`,\n );\n }\n }\n}\n\n/** The steps a database at the newest version has recorded, in order. */\nexport function localSchemaSteps(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): ReadonlyArray<LocalSchemaStep> {\n return [\n LOCAL_SCHEMA_BASELINE,\n ...migrations.map(({ name, version }) => ({ name, version })),\n ];\n}\n"]}
1
+ {"version":3,"file":"localSchemaMigrations.js","sourceRoot":"","sources":["localSchemaMigrations.ts"],"names":[],"mappings":"AA0BA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAoB;IACpD,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,CAAC;CACX,CAAC;AAEF,MAAM,qBAAqB,GAAG,yCAAyC,CAAC;AAExE;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAwC;IACxE;QACE,wEAAwE;QACxE,oEAAoE;QACpE,IAAI,EAAE,iCAAiC;QACvC,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,CAAC,MAAM,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC;gBAAE,OAAO;YAC7D,KAAK,MAAM,MAAM,IAAI,CAAC,oBAAoB,EAAE,0BAA0B,CAAC,EAAE,CAAC;gBACxE,MAAM,OAAO,CACX,gBAAgB,qBAAqB,kBAAkB,MAAM,GAAG,CACjE,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,EAAE,CAAC;KACX;IACD;QACE,wEAAwE;QACxE,sEAAsE;QACtE,0EAA0E;QAC1E,wCAAwC;QACxC,IAAI,EAAE,mCAAmC;QACzC,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;YACpB,MAAM,MAAM,GAAG,MAAM,OAAO,CAC1B,oFAAoF,CACrF,CAAC;YACF,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO;YAChC,MAAM,OAAO,CACX,2DAA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CAAC","sourcesContent":["import type { ExecSql } from \"../sqlExec\";\n\nexport interface LocalSchemaMigrationContext {\n /**\n * Whether a table exists. Features create their tables lazily, so a\n * migration must skip a table this database never created: the table's\n * ensure creates it later in the current shape.\n */\n hasTable(name: string): Promise<boolean>;\n}\n\nexport interface LocalSchemaStep {\n readonly version: number;\n readonly name: string;\n}\n\nexport interface LocalSchemaMigration extends LocalSchemaStep {\n /**\n * Upgrades the local database from `version - 1` to `version`. It runs on\n * the locked executor inside the migration savepoint, so it must not BEGIN,\n * COMMIT, or change `PRAGMA foreign_keys`. Write it against the shape at\n * `version - 1`, never by importing current table definitions.\n */\n up(execSql: ExecSql, context: LocalSchemaMigrationContext): Promise<void>;\n}\n\n/**\n * Version 1 is the shape the SDK's table definitions created when local\n * migrations began. It has no steps: a database from before the ledger is\n * recorded at this version, and an older shape still fails its tables'\n * required-column check.\n */\nexport const LOCAL_SCHEMA_BASELINE: LocalSchemaStep = {\n name: \"baseline\",\n version: 1,\n};\n\nconst DIRECTORY_USERS_TABLE = \"organization_read_model_directory_users\";\n\n/**\n * Ordered and append-only: version 2 first, each one after the last. Never\n * edit or remove a shipped step; a correction is a new step.\n */\nexport const localSchemaMigrations: ReadonlyArray<LocalSchemaMigration> = [\n {\n // Directory rows carry key fingerprints only; public keys come from the\n // trusted identity store. Neither column is indexed or constrained.\n name: \"drop_directory_user_public_keys\",\n up: async (execSql, context) => {\n if (!(await context.hasTable(DIRECTORY_USERS_TABLE))) return;\n for (const column of [\"signing_public_key\", \"encapsulation_public_key\"]) {\n await execSql(\n `ALTER TABLE \"${DIRECTORY_USERS_TABLE}\" DROP COLUMN \"${column}\"`,\n );\n }\n },\n version: 2,\n },\n {\n // No reader ever used a recorded failure's HTTP status; the message and\n // attempt time explain a blocked document. The column is unindexed. A\n // documents table without it (never created, or not this SDK's table in a\n // restored backup) has nothing to drop.\n name: \"drop_document_sync_failure_status\",\n up: async (execSql) => {\n const column = await execSql(\n \"SELECT name FROM pragma_table_info('documents') WHERE name = 'sync_failure_status'\",\n );\n if (column.length === 0) return;\n await execSql(\n 'ALTER TABLE \"documents\" DROP COLUMN \"sync_failure_status\"',\n );\n },\n version: 3,\n },\n {\n name: \"replay_commit_ordered_discovery\",\n up: async (execSql, context) => {\n if (!(await context.hasTable(\"container_sync_watermarks\"))) return;\n // Older SDKs could checkpoint a publication before reconciling an\n // unlink or a restored subtree. Legacy UUID cursors already replay.\n await execSql(\n \"ALTER TABLE container_sync_watermarks ADD COLUMN cursor_format INTEGER NOT NULL DEFAULT 1\",\n );\n },\n version: 4,\n },\n];\n\nconst STEP_NAME_PATTERN = /^[a-z0-9_]+$/;\n\n/** Rejects a registry whose versions are not consecutive from 2. */\nexport function assertLocalSchemaMigrations(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): void {\n for (const [index, migration] of migrations.entries()) {\n const expected = LOCAL_SCHEMA_BASELINE.version + index + 1;\n if (migration.version !== expected) {\n throw new Error(\n `Local schema migration ${migration.name} has version ${migration.version}; expected ${expected}`,\n );\n }\n if (!STEP_NAME_PATTERN.test(migration.name)) {\n throw new Error(\n `Local schema migration ${migration.version} needs a snake_case name`,\n );\n }\n }\n}\n\n/** The steps a database at the newest version has recorded, in order. */\nexport function localSchemaSteps(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): ReadonlyArray<LocalSchemaStep> {\n return [\n LOCAL_SCHEMA_BASELINE,\n ...migrations.map(({ name, version }) => ({ name, version })),\n ];\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { primaryKey, sqliteTable, text } from "drizzle-orm/sqlite-core";
1
+ import { integer, primaryKey, sqliteTable, text, } from "drizzle-orm/sqlite-core";
2
2
  import { defineSqlTableSchema } from "../sqlTableSchema.js";
3
3
  /**
4
4
  * Per-lane sync state for container data: the server cursor and the local
@@ -13,13 +13,15 @@ import { defineSqlTableSchema } from "../sqlTableSchema.js";
13
13
  *
14
14
  * Columns:
15
15
  * - `laneKind`: Sync lane category, such as `container_parent` or
16
- * `container_documents`.
16
+ * `container_documents`. `container_restoration` stores the last reconciled
17
+ * access loss for a folder.
17
18
  * - `laneId`: Lane identity. Root parent sync uses `root`, child sync uses a
18
19
  * parent-prefixed container id, and document lanes use the container id.
19
20
  * - `watermarkUpdatedAt`: Server `updatedAt` value for the stored watermark;
20
21
  * null until a watermark is saved.
21
22
  * - `watermarkId`: Server id tie-breaker for the stored watermark; null
22
23
  * exactly when `watermarkUpdatedAt` is.
24
+ * - `cursorFormat`: SDK format that applied the saved watermark.
23
25
  * - `updatedAt`: Local timestamp for when the cursor was saved.
24
26
  * - `checkedAt`: Local timestamp for the most recent fully drained lane check;
25
27
  * null until one completes.
@@ -32,13 +34,14 @@ export const containerSyncWatermarks = sqliteTable("container_sync_watermarks",
32
34
  laneId: text("lane_id").notNull(),
33
35
  watermarkUpdatedAt: text("watermark_updated_at"),
34
36
  watermarkId: text("watermark_id"),
37
+ cursorFormat: integer("cursor_format").notNull().default(1),
35
38
  updatedAt: text("updated_at"),
36
39
  checkedAt: text("checked_at"),
37
40
  }, (table) => [primaryKey({ columns: [table.laneKind, table.laneId] })]);
38
41
  export const containerSyncWatermarkTables = [
39
42
  {
40
43
  ...defineSqlTableSchema(containerSyncWatermarks),
41
- requiredColumns: ["checked_at"],
44
+ requiredColumns: ["checked_at", "cursor_format"],
42
45
  },
43
46
  ];
44
47
  //# sourceMappingURL=containerSyncWatermarkSchema.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"containerSyncWatermarkSchema.js","sourceRoot":"","sources":["containerSyncWatermarkSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAuB,MAAM,mBAAmB,CAAC;AAE9E;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,WAAW,CAChD,2BAA2B,EAC3B;IACE,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE;IACrC,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE;IACjC,kBAAkB,EAAE,IAAI,CAAC,sBAAsB,CAAC;IAChD,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC;IACjC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC;IAC7B,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC;CAC9B,EACD,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CACrE,CAAC;AAEF,MAAM,CAAC,MAAM,4BAA4B,GAAkC;IACzE;QACE,GAAG,oBAAoB,CAAC,uBAAuB,CAAC;QAChD,eAAe,EAAE,CAAC,YAAY,CAAC;KAChC;CACF,CAAC","sourcesContent":["import { primaryKey, sqliteTable, text } from \"drizzle-orm/sqlite-core\";\nimport { defineSqlTableSchema, type SqlTableSchema } from \"../sqlTableSchema\";\n\n/**\n * Per-lane sync state for container data: the server cursor and the local\n * freshness marker share one row per lane.\n *\n * Container sync is split into lanes, such as a parent-container child lane or\n * a container-document membership lane. The cursor lets the next request\n * resume without replaying the entire tree. The freshness marker records the\n * last fully drained check independently of the cursor: empty server\n * responses do not always carry a new watermark, but startup still needs to\n * know that it recently checked the lane before polling again.\n *\n * Columns:\n * - `laneKind`: Sync lane category, such as `container_parent` or\n * `container_documents`.\n * - `laneId`: Lane identity. Root parent sync uses `root`, child sync uses a\n * parent-prefixed container id, and document lanes use the container id.\n * - `watermarkUpdatedAt`: Server `updatedAt` value for the stored watermark;\n * null until a watermark is saved.\n * - `watermarkId`: Server id tie-breaker for the stored watermark; null\n * exactly when `watermarkUpdatedAt` is.\n * - `updatedAt`: Local timestamp for when the cursor was saved.\n * - `checkedAt`: Local timestamp for the most recent fully drained lane check;\n * null until one completes.\n *\n * Indexes:\n * - `(laneKind, laneId)` is the primary key and stores one row per lane.\n */\nexport const containerSyncWatermarks = sqliteTable(\n \"container_sync_watermarks\",\n {\n laneKind: text(\"lane_kind\").notNull(),\n laneId: text(\"lane_id\").notNull(),\n watermarkUpdatedAt: text(\"watermark_updated_at\"),\n watermarkId: text(\"watermark_id\"),\n updatedAt: text(\"updated_at\"),\n checkedAt: text(\"checked_at\"),\n },\n (table) => [primaryKey({ columns: [table.laneKind, table.laneId] })],\n);\n\nexport const containerSyncWatermarkTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(containerSyncWatermarks),\n requiredColumns: [\"checked_at\"],\n },\n];\n"]}
1
+ {"version":3,"file":"containerSyncWatermarkSchema.js","sourceRoot":"","sources":["containerSyncWatermarkSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,GACL,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,oBAAoB,EAAuB,MAAM,mBAAmB,CAAC;AAE9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,WAAW,CAChD,2BAA2B,EAC3B;IACE,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE;IACrC,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE;IACjC,kBAAkB,EAAE,IAAI,CAAC,sBAAsB,CAAC;IAChD,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC;IACjC,YAAY,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAC3D,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC;IAC7B,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC;CAC9B,EACD,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CACrE,CAAC;AAEF,MAAM,CAAC,MAAM,4BAA4B,GAAkC;IACzE;QACE,GAAG,oBAAoB,CAAC,uBAAuB,CAAC;QAChD,eAAe,EAAE,CAAC,YAAY,EAAE,eAAe,CAAC;KACjD;CACF,CAAC","sourcesContent":["import {\n integer,\n primaryKey,\n sqliteTable,\n text,\n} from \"drizzle-orm/sqlite-core\";\nimport { defineSqlTableSchema, type SqlTableSchema } from \"../sqlTableSchema\";\n\n/**\n * Per-lane sync state for container data: the server cursor and the local\n * freshness marker share one row per lane.\n *\n * Container sync is split into lanes, such as a parent-container child lane or\n * a container-document membership lane. The cursor lets the next request\n * resume without replaying the entire tree. The freshness marker records the\n * last fully drained check independently of the cursor: empty server\n * responses do not always carry a new watermark, but startup still needs to\n * know that it recently checked the lane before polling again.\n *\n * Columns:\n * - `laneKind`: Sync lane category, such as `container_parent` or\n * `container_documents`. `container_restoration` stores the last reconciled\n * access loss for a folder.\n * - `laneId`: Lane identity. Root parent sync uses `root`, child sync uses a\n * parent-prefixed container id, and document lanes use the container id.\n * - `watermarkUpdatedAt`: Server `updatedAt` value for the stored watermark;\n * null until a watermark is saved.\n * - `watermarkId`: Server id tie-breaker for the stored watermark; null\n * exactly when `watermarkUpdatedAt` is.\n * - `cursorFormat`: SDK format that applied the saved watermark.\n * - `updatedAt`: Local timestamp for when the cursor was saved.\n * - `checkedAt`: Local timestamp for the most recent fully drained lane check;\n * null until one completes.\n *\n * Indexes:\n * - `(laneKind, laneId)` is the primary key and stores one row per lane.\n */\nexport const containerSyncWatermarks = sqliteTable(\n \"container_sync_watermarks\",\n {\n laneKind: text(\"lane_kind\").notNull(),\n laneId: text(\"lane_id\").notNull(),\n watermarkUpdatedAt: text(\"watermark_updated_at\"),\n watermarkId: text(\"watermark_id\"),\n cursorFormat: integer(\"cursor_format\").notNull().default(1),\n updatedAt: text(\"updated_at\"),\n checkedAt: text(\"checked_at\"),\n },\n (table) => [primaryKey({ columns: [table.laneKind, table.laneId] })],\n);\n\nexport const containerSyncWatermarkTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(containerSyncWatermarks),\n requiredColumns: [\"checked_at\", \"cursor_format\"],\n },\n];\n"]}
@@ -2870,6 +2870,23 @@ export declare const clientSQLiteSchema: {
2870
2870
  }, {}, {
2871
2871
  length: number | undefined;
2872
2872
  }>;
2873
+ cursorFormat: import("drizzle-orm/sqlite-core").SQLiteColumn<{
2874
+ name: "cursor_format";
2875
+ tableName: "container_sync_watermarks";
2876
+ dataType: "number";
2877
+ columnType: "SQLiteInteger";
2878
+ data: number;
2879
+ driverParam: number;
2880
+ notNull: true;
2881
+ hasDefault: true;
2882
+ isPrimaryKey: false;
2883
+ isAutoincrement: false;
2884
+ hasRuntimeDefault: false;
2885
+ enumValues: undefined;
2886
+ baseColumn: never;
2887
+ identity: undefined;
2888
+ generated: undefined;
2889
+ }, {}, {}>;
2873
2890
  updatedAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
2874
2891
  name: "updated_at";
2875
2892
  tableName: "container_sync_watermarks";
@@ -1,3 +1,4 @@
1
+ import { createListenerSet } from "../shared/listenerSet.js";
1
2
  import { requestAllPumpDrivenLanes, requestLaneSync, requestLaneSyncAfter, waitForIdleLanes, } from "./coordinatorPump.js";
2
3
  import { hasPendingLaneWork, INITIAL_SYNC_LANE_RUN_STATE, publishSyncCoordinatorSnapshot, } from "./coordinatorState.js";
3
4
  import { transitionSyncLaneRequest } from "./syncLaneMachine.js";
@@ -9,6 +10,8 @@ import { beginUploadLane } from "./uploadLane.js";
9
10
  export { isDatabaseUnavailableError } from "../sqlite/availability/databaseUnavailable.js";
10
11
  export { didRegainSyncPrerequisites, } from "./syncPrerequisites.js";
11
12
  const coordinatorsByScope = new WeakMap();
13
+ // A host watching a domain follows its replacement coordinator until it unsubscribes.
14
+ const observersByScope = new WeakMap();
12
15
  function createSyncLaneHandle(coordinatorState, lane) {
13
16
  return {
14
17
  isDisposed: () => coordinatorState.disposed,
@@ -16,11 +19,11 @@ function createSyncLaneHandle(coordinatorState, lane) {
16
19
  requestSyncAfter: (delayMs) => requestLaneSyncAfter(coordinatorState, lane, delayMs),
17
20
  };
18
21
  }
19
- function createDomainSyncCoordinator() {
22
+ function createDomainSyncCoordinator(domainScope) {
20
23
  const coordinatorState = {
21
24
  disposed: false,
22
25
  lanes: new Map(),
23
- listeners: new Set(),
26
+ listeners: new Set([() => observersByScope.get(domainScope)?.notify()]),
24
27
  nextRegistrationIndex: 0,
25
28
  nextRunOrder: 0,
26
29
  wakePump: null,
@@ -36,7 +39,11 @@ function createDomainSyncCoordinator() {
36
39
  return beginUploadLane(coordinatorState, key, options);
37
40
  },
38
41
  dispose() {
42
+ if (coordinatorState.disposed)
43
+ return;
39
44
  coordinatorState.disposed = true;
45
+ // A disposal listener can synchronously reopen this scope.
46
+ coordinatorsByScope.delete(domainScope);
40
47
  // Drop queued work so waitForIdle reports settled immediately and a
41
48
  // lane left re-requested by an in-flight run does not survive teardown.
42
49
  for (const lane of coordinatorState.lanes.values()) {
@@ -118,7 +125,7 @@ export function getOrCreateDomainSyncCoordinator(domainScope) {
118
125
  if (existingCoordinator) {
119
126
  return existingCoordinator;
120
127
  }
121
- const nextCoordinator = createDomainSyncCoordinator();
128
+ const nextCoordinator = createDomainSyncCoordinator(domainScope);
122
129
  coordinatorsByScope.set(domainScope, nextCoordinator);
123
130
  return nextCoordinator;
124
131
  }
@@ -138,18 +145,18 @@ export function requestDomainSyncLane(domainScope, key) {
138
145
  // Stop admitting work and remove this scope's coordinator. Active callbacks
139
146
  // drain under their existing watchdogs; getOrCreate builds a fresh coordinator.
140
147
  export function disposeDomainSyncCoordinator(domainScope) {
141
- const coordinator = coordinatorsByScope.get(domainScope);
142
- if (!coordinator) {
143
- return;
144
- }
145
- coordinator.dispose();
146
- coordinatorsByScope.delete(domainScope);
148
+ coordinatorsByScope.get(domainScope)?.dispose();
147
149
  }
148
150
  export function getDomainSyncCoordinatorSnapshot(domainScope) {
149
151
  return getOrCreateDomainSyncCoordinator(domainScope).getSnapshot();
150
152
  }
151
153
  export function subscribeToDomainSyncCoordinator(domainScope, listener) {
152
- return getOrCreateDomainSyncCoordinator(domainScope).subscribe(listener);
154
+ let observers = observersByScope.get(domainScope);
155
+ if (!observers) {
156
+ observers = createListenerSet();
157
+ observersByScope.set(domainScope, observers);
158
+ }
159
+ return observers.subscribe(listener);
153
160
  }
154
161
  export function beginDomainSyncUploadLane(domainScope, key, options) {
155
162
  return getOrCreateDomainSyncCoordinator(domainScope).beginUploadLane(key, options);
@@ -1 +1 @@
1
- {"version":3,"file":"syncCoordinator.js","sourceRoot":"","sources":["syncCoordinator.ts"],"names":[],"mappings":"AACA,OAAO,EACL,yBAAyB,EACzB,eAAe,EACf,oBAAoB,EAEpB,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAK3B,OAAO,EACL,kBAAkB,EAClB,2BAA2B,EAC3B,8BAA8B,GAC/B,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,wBAAwB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEhF,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,+EAA+E;AAC/E,6EAA6E;AAC7E,kCAAkC;AAClC,OAAO,EAAE,0BAA0B,EAAE,MAAM,4CAA4C,CAAC;AAGxF,OAAO,EACL,0BAA0B,GAE3B,MAAM,qBAAqB,CAAC;AAgC7B,MAAM,mBAAmB,GAAG,IAAI,OAAO,EAAsC,CAAC;AAE9E,SAAS,oBAAoB,CAC3B,gBAA4C,EAC5C,IAAmB;IAEnB,OAAO;QACL,UAAU,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,QAAQ;QAC3C,WAAW,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,gBAAgB,EAAE,IAAI,CAAC;QAC1D,gBAAgB,EAAE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AM,UAAU,qBAAqB,CACnC,WAAwB,EACxB,GAAW;IAEX,mBAAmB,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;AACzD,CAAC;AAED,4EAA4E;AAC5E,gFAAgF;AAChF,MAAM,UAAU,4BAA4B,CAAC,WAAwB;IACnE,MAAM,WAAW,GAAG,mBAAmB,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACzD,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO;IACT,CAAC;IAED,WAAW,CAAC,OAAO,EAAE,CAAC;IACtB,mBAAmB,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAC1C,CAAC;AAED,MAAM,UAAU,gCAAgC,CAC9C,WAAwB;IAExB,OAAO,gCAAgC,CAAC,WAAW,CAAC,CAAC,WAAW,EAAE,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,gCAAgC,CAC9C,WAAwB,EACxB,QAAoB;IAEpB,OAAO,gCAAgC,CAAC,WAAW,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;AAC3E,CAAC;AAED,MAAM,UAAU,yBAAyB,CACvC,WAAwB,EACxB,GAAW,EACX,OAA+B;IAE/B,OAAO,gCAAgC,CAAC,WAAW,CAAC,CAAC,eAAe,CAClE,GAAG,EACH,OAAO,CACR,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,oCAAoC,CAClD,WAAwB,EACxB,OAAyB;IAEzB,OAAO,CACL,mBAAmB,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC;QAC1D,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CACtB,CAAC;AACJ,CAAC","sourcesContent":["import type { DomainScope } from \"../shared/domainScope\";\nimport {\n requestAllPumpDrivenLanes,\n requestLaneSync,\n requestLaneSyncAfter,\n type SyncIdleOptions,\n waitForIdleLanes,\n} from \"./coordinatorPump\";\nimport type {\n DomainSyncCoordinatorState,\n SyncLaneState,\n} from \"./coordinatorState\";\nimport {\n hasPendingLaneWork,\n INITIAL_SYNC_LANE_RUN_STATE,\n publishSyncCoordinatorSnapshot,\n} from \"./coordinatorState\";\nimport type { SyncLane, SyncLaneConfig } from \"./syncLaneConfig\";\nimport { transitionSyncLaneRequest } from \"./syncLaneMachine\";\nimport type { DomainSyncSnapshot } from \"./syncTelemetry\";\nimport { createDomainSyncSnapshot, createSyncTimestamp } from \"./syncTelemetry\";\nimport type { UploadSyncLane, UploadSyncLaneOptions } from \"./uploadLane\";\nimport { beginUploadLane } from \"./uploadLane\";\n\n// Re-exported here because lanes reach it as a shouldIgnoreError alongside the\n// rest of the coordinator surface; it lives beside this coordinator with the\n// error type it discriminates on.\nexport { isDatabaseUnavailableError } from \"../sqlite/availability/databaseUnavailable\";\nexport type { SyncIdleOptions } from \"./coordinatorPump\";\nexport type { SyncLane, SyncLaneConfig } from \"./syncLaneConfig\";\nexport {\n didRegainSyncPrerequisites,\n type SyncRuntimeStatus,\n} from \"./syncPrerequisites\";\nexport type {\n DomainSyncSnapshot,\n SyncLaneLastAction,\n SyncLanePhase,\n SyncLaneProgress,\n SyncLaneSnapshot,\n SyncLaneStatus,\n} from \"./syncTelemetry\";\nexport type { UploadSyncLane, UploadSyncLaneOptions } from \"./uploadLane\";\n\nexport interface DomainSyncCoordinator {\n beginUploadLane: (\n key: string,\n options?: UploadSyncLaneOptions,\n ) => UploadSyncLane;\n // Drop queued work and refuse new runs; active callbacks retain ownership.\n dispose: () => void;\n getSnapshot: () => DomainSyncSnapshot;\n registerLane: (key: string, config: SyncLaneConfig) => SyncLane;\n // Re-request every pump-driven lane — a manual \"sync now\" that re-drives\n // durable owners stranded by a transient failure. Observational upload rows\n // are updated only by those owners' real attachment upload attempts.\n requestAllLanes: () => void;\n // Request one registered pump lane without re-driving unrelated owners.\n requestLane: (key: string) => void;\n hasPendingWork: () => boolean;\n isDisposed: () => boolean;\n subscribe: (listener: () => void) => () => void;\n waitForIdle: (options?: SyncIdleOptions) => Promise<boolean>;\n}\n\nconst coordinatorsByScope = new WeakMap<DomainScope, DomainSyncCoordinator>();\n\nfunction createSyncLaneHandle(\n coordinatorState: DomainSyncCoordinatorState,\n lane: SyncLaneState,\n): SyncLane {\n return {\n isDisposed: () => coordinatorState.disposed,\n requestSync: () => requestLaneSync(coordinatorState, lane),\n requestSyncAfter: (delayMs) =>\n requestLaneSyncAfter(coordinatorState, lane, delayMs),\n };\n}\n\nfunction createDomainSyncCoordinator(): DomainSyncCoordinator {\n const coordinatorState: DomainSyncCoordinatorState = {\n disposed: false,\n lanes: new Map<string, SyncLaneState>(),\n listeners: new Set(),\n nextRegistrationIndex: 0,\n nextRunOrder: 0,\n wakePump: null,\n pump: null,\n snapshot: createDomainSyncSnapshot({\n hasPendingWork: false,\n lanes: [],\n pumpActive: false,\n }),\n };\n\n return {\n beginUploadLane(key: string, options: UploadSyncLaneOptions = {}) {\n return beginUploadLane(coordinatorState, key, options);\n },\n dispose() {\n coordinatorState.disposed = true;\n // Drop queued work so waitForIdle reports settled immediately and a\n // lane left re-requested by an in-flight run does not survive teardown.\n for (const lane of coordinatorState.lanes.values()) {\n lane.request = transitionSyncLaneRequest(lane.request, {\n type: \"clear\",\n });\n }\n // Publish the final settled snapshot, then release listener closures:\n // the coordinator is dropped from the registry and a remount subscribes\n // to a fresh one.\n publishSyncCoordinatorSnapshot(coordinatorState);\n coordinatorState.listeners.clear();\n },\n getSnapshot() {\n return coordinatorState.snapshot;\n },\n hasPendingWork() {\n return (\n !!coordinatorState.pump ||\n hasPendingLaneWork(coordinatorState.lanes.values())\n );\n },\n isDisposed() {\n return coordinatorState.disposed;\n },\n registerLane(key: string, config: SyncLaneConfig): SyncLane {\n const existingLane = coordinatorState.lanes.get(key);\n if (existingLane) {\n existingLane.config = config;\n existingLane.blobStorageKey = null;\n existingLane.pumpDriven = true;\n publishSyncCoordinatorSnapshot(coordinatorState);\n return createSyncLaneHandle(coordinatorState, existingLane);\n }\n\n const registeredAt = createSyncTimestamp();\n const nextLane: SyncLaneState = {\n ...INITIAL_SYNC_LANE_RUN_STATE,\n blobStorageKey: null,\n config,\n errorCount: 0,\n key,\n lastAction: \"registered\",\n lastActionAt: registeredAt,\n lastCompletedAt: null,\n lastError: null,\n lastFailedAt: null,\n lastRequestedAt: null,\n lastStartedAt: null,\n progress: null,\n pumpDriven: true,\n registrationIndex: coordinatorState.nextRegistrationIndex,\n requestCount: 0,\n };\n coordinatorState.nextRegistrationIndex += 1;\n coordinatorState.lanes.set(key, nextLane);\n publishSyncCoordinatorSnapshot(coordinatorState);\n return createSyncLaneHandle(coordinatorState, nextLane);\n },\n requestAllLanes() {\n requestAllPumpDrivenLanes(coordinatorState);\n },\n requestLane(key: string) {\n const lane = coordinatorState.lanes.get(key);\n if (lane?.pumpDriven) {\n requestLaneSync(coordinatorState, lane);\n }\n },\n subscribe(listener: () => void) {\n coordinatorState.listeners.add(listener);\n return () => {\n coordinatorState.listeners.delete(listener);\n };\n },\n waitForIdle(options?: SyncIdleOptions) {\n return waitForIdleLanes(coordinatorState, options);\n },\n };\n}\n\nexport function getOrCreateDomainSyncCoordinator(\n domainScope: DomainScope,\n): DomainSyncCoordinator {\n const existingCoordinator = coordinatorsByScope.get(domainScope);\n if (existingCoordinator) {\n return existingCoordinator;\n }\n\n const nextCoordinator = createDomainSyncCoordinator();\n coordinatorsByScope.set(domainScope, nextCoordinator);\n return nextCoordinator;\n}\n\nexport function hasDomainSyncCoordinatorPendingWork(\n domainScope: DomainScope,\n): boolean {\n return coordinatorsByScope.get(domainScope)?.hasPendingWork() ?? false;\n}\n\n// Re-request every pump-driven lane for a scope: a user-driven \"sync now\" that\n// retries durable work stranded by a transient failure. Observational upload\n// telemetry is never directly pumped. A no-op when no coordinator exists yet\n// (nothing has been synced) or it has been disposed.\nexport function requestAllDomainSyncLanes(domainScope: DomainScope): void {\n coordinatorsByScope.get(domainScope)?.requestAllLanes();\n}\n\nexport function requestDomainSyncLane(\n domainScope: DomainScope,\n key: string,\n): void {\n coordinatorsByScope.get(domainScope)?.requestLane(key);\n}\n\n// Stop admitting work and remove this scope's coordinator. Active callbacks\n// drain under their existing watchdogs; getOrCreate builds a fresh coordinator.\nexport function disposeDomainSyncCoordinator(domainScope: DomainScope): void {\n const coordinator = coordinatorsByScope.get(domainScope);\n if (!coordinator) {\n return;\n }\n\n coordinator.dispose();\n coordinatorsByScope.delete(domainScope);\n}\n\nexport function getDomainSyncCoordinatorSnapshot(\n domainScope: DomainScope,\n): DomainSyncSnapshot {\n return getOrCreateDomainSyncCoordinator(domainScope).getSnapshot();\n}\n\nexport function subscribeToDomainSyncCoordinator(\n domainScope: DomainScope,\n listener: () => void,\n): () => void {\n return getOrCreateDomainSyncCoordinator(domainScope).subscribe(listener);\n}\n\nexport function beginDomainSyncUploadLane(\n domainScope: DomainScope,\n key: string,\n options?: UploadSyncLaneOptions,\n): UploadSyncLane {\n return getOrCreateDomainSyncCoordinator(domainScope).beginUploadLane(\n key,\n options,\n );\n}\n\nexport function waitForDomainSyncCoordinatorToSettle(\n domainScope: DomainScope,\n options?: SyncIdleOptions,\n): Promise<boolean> {\n return (\n coordinatorsByScope.get(domainScope)?.waitForIdle(options) ??\n Promise.resolve(true)\n );\n}\n"]}
1
+ {"version":3,"file":"syncCoordinator.js","sourceRoot":"","sources":["syncCoordinator.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EACL,yBAAyB,EACzB,eAAe,EACf,oBAAoB,EAEpB,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAK3B,OAAO,EACL,kBAAkB,EAClB,2BAA2B,EAC3B,8BAA8B,GAC/B,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,wBAAwB,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEhF,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC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AW,CAAC,CAAC;IAClD,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,SAAS,GAAG,iBAAiB,EAAE,CAAC;QAChC,gBAAgB,CAAC,GAAG,CAAC,WAAW,EAAE,SAAS,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,SAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,yBAAyB,CACvC,WAAwB,EACxB,GAAW,EACX,OAA+B;IAE/B,OAAO,gCAAgC,CAAC,WAAW,CAAC,CAAC,eAAe,CAClE,GAAG,EACH,OAAO,CACR,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,oCAAoC,CAClD,WAAwB,EACxB,OAAyB;IAEzB,OAAO,CACL,mBAAmB,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC;QAC1D,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CACtB,CAAC;AACJ,CAAC","sourcesContent":["import type { DomainScope } from \"../shared/domainScope\";\nimport { createListenerSet } from \"../shared/listenerSet\";\nimport {\n requestAllPumpDrivenLanes,\n requestLaneSync,\n requestLaneSyncAfter,\n type SyncIdleOptions,\n waitForIdleLanes,\n} from \"./coordinatorPump\";\nimport type {\n DomainSyncCoordinatorState,\n SyncLaneState,\n} from \"./coordinatorState\";\nimport {\n hasPendingLaneWork,\n INITIAL_SYNC_LANE_RUN_STATE,\n publishSyncCoordinatorSnapshot,\n} from \"./coordinatorState\";\nimport type { SyncLane, SyncLaneConfig } from \"./syncLaneConfig\";\nimport { transitionSyncLaneRequest } from \"./syncLaneMachine\";\nimport type { DomainSyncSnapshot } from \"./syncTelemetry\";\nimport { createDomainSyncSnapshot, createSyncTimestamp } from \"./syncTelemetry\";\nimport type { UploadSyncLane, UploadSyncLaneOptions } from \"./uploadLane\";\nimport { beginUploadLane } from \"./uploadLane\";\n\n// Re-exported here because lanes reach it as a shouldIgnoreError alongside the\n// rest of the coordinator surface; it lives beside this coordinator with the\n// error type it discriminates on.\nexport { isDatabaseUnavailableError } from \"../sqlite/availability/databaseUnavailable\";\nexport type { SyncIdleOptions } from \"./coordinatorPump\";\nexport type { SyncLane, SyncLaneConfig } from \"./syncLaneConfig\";\nexport {\n didRegainSyncPrerequisites,\n type SyncRuntimeStatus,\n} from \"./syncPrerequisites\";\nexport type {\n DomainSyncSnapshot,\n SyncLaneLastAction,\n SyncLanePhase,\n SyncLaneProgress,\n SyncLaneSnapshot,\n SyncLaneStatus,\n} from \"./syncTelemetry\";\nexport type { UploadSyncLane, UploadSyncLaneOptions } from \"./uploadLane\";\n\nexport interface DomainSyncCoordinator {\n beginUploadLane: (\n key: string,\n options?: UploadSyncLaneOptions,\n ) => UploadSyncLane;\n // Drop queued work and refuse new runs; active callbacks retain ownership.\n dispose: () => void;\n getSnapshot: () => DomainSyncSnapshot;\n registerLane: (key: string, config: SyncLaneConfig) => SyncLane;\n // Re-request every pump-driven lane — a manual \"sync now\" that re-drives\n // durable owners stranded by a transient failure. Observational upload rows\n // are updated only by those owners' real attachment upload attempts.\n requestAllLanes: () => void;\n // Request one registered pump lane without re-driving unrelated owners.\n requestLane: (key: string) => void;\n hasPendingWork: () => boolean;\n isDisposed: () => boolean;\n subscribe: (listener: () => void) => () => void;\n waitForIdle: (options?: SyncIdleOptions) => Promise<boolean>;\n}\n\nconst coordinatorsByScope = new WeakMap<DomainScope, DomainSyncCoordinator>();\n\n// A host watching a domain follows its replacement coordinator until it unsubscribes.\nconst observersByScope = new WeakMap<\n DomainScope,\n ReturnType<typeof createListenerSet<[]>>\n>();\n\nfunction createSyncLaneHandle(\n coordinatorState: DomainSyncCoordinatorState,\n lane: SyncLaneState,\n): SyncLane {\n return {\n isDisposed: () => coordinatorState.disposed,\n requestSync: () => requestLaneSync(coordinatorState, lane),\n requestSyncAfter: (delayMs) =>\n requestLaneSyncAfter(coordinatorState, lane, delayMs),\n };\n}\n\nfunction createDomainSyncCoordinator(\n domainScope: DomainScope,\n): DomainSyncCoordinator {\n const coordinatorState: DomainSyncCoordinatorState = {\n disposed: false,\n lanes: new Map<string, SyncLaneState>(),\n listeners: new Set([() => observersByScope.get(domainScope)?.notify()]),\n nextRegistrationIndex: 0,\n nextRunOrder: 0,\n wakePump: null,\n pump: null,\n snapshot: createDomainSyncSnapshot({\n hasPendingWork: false,\n lanes: [],\n pumpActive: false,\n }),\n };\n\n return {\n beginUploadLane(key: string, options: UploadSyncLaneOptions = {}) {\n return beginUploadLane(coordinatorState, key, options);\n },\n dispose() {\n if (coordinatorState.disposed) return;\n coordinatorState.disposed = true;\n // A disposal listener can synchronously reopen this scope.\n coordinatorsByScope.delete(domainScope);\n // Drop queued work so waitForIdle reports settled immediately and a\n // lane left re-requested by an in-flight run does not survive teardown.\n for (const lane of coordinatorState.lanes.values()) {\n lane.request = transitionSyncLaneRequest(lane.request, {\n type: \"clear\",\n });\n }\n // Publish the final settled snapshot, then release listener closures:\n // the coordinator is dropped from the registry and a remount subscribes\n // to a fresh one.\n publishSyncCoordinatorSnapshot(coordinatorState);\n coordinatorState.listeners.clear();\n },\n getSnapshot() {\n return coordinatorState.snapshot;\n },\n hasPendingWork() {\n return (\n !!coordinatorState.pump ||\n hasPendingLaneWork(coordinatorState.lanes.values())\n );\n },\n isDisposed() {\n return coordinatorState.disposed;\n },\n registerLane(key: string, config: SyncLaneConfig): SyncLane {\n const existingLane = coordinatorState.lanes.get(key);\n if (existingLane) {\n existingLane.config = config;\n existingLane.blobStorageKey = null;\n existingLane.pumpDriven = true;\n publishSyncCoordinatorSnapshot(coordinatorState);\n return createSyncLaneHandle(coordinatorState, existingLane);\n }\n\n const registeredAt = createSyncTimestamp();\n const nextLane: SyncLaneState = {\n ...INITIAL_SYNC_LANE_RUN_STATE,\n blobStorageKey: null,\n config,\n errorCount: 0,\n key,\n lastAction: \"registered\",\n lastActionAt: registeredAt,\n lastCompletedAt: null,\n lastError: null,\n lastFailedAt: null,\n lastRequestedAt: null,\n lastStartedAt: null,\n progress: null,\n pumpDriven: true,\n registrationIndex: coordinatorState.nextRegistrationIndex,\n requestCount: 0,\n };\n coordinatorState.nextRegistrationIndex += 1;\n coordinatorState.lanes.set(key, nextLane);\n publishSyncCoordinatorSnapshot(coordinatorState);\n return createSyncLaneHandle(coordinatorState, nextLane);\n },\n requestAllLanes() {\n requestAllPumpDrivenLanes(coordinatorState);\n },\n requestLane(key: string) {\n const lane = coordinatorState.lanes.get(key);\n if (lane?.pumpDriven) {\n requestLaneSync(coordinatorState, lane);\n }\n },\n subscribe(listener: () => void) {\n coordinatorState.listeners.add(listener);\n return () => {\n coordinatorState.listeners.delete(listener);\n };\n },\n waitForIdle(options?: SyncIdleOptions) {\n return waitForIdleLanes(coordinatorState, options);\n },\n };\n}\n\nexport function getOrCreateDomainSyncCoordinator(\n domainScope: DomainScope,\n): DomainSyncCoordinator {\n const existingCoordinator = coordinatorsByScope.get(domainScope);\n if (existingCoordinator) {\n return existingCoordinator;\n }\n\n const nextCoordinator = createDomainSyncCoordinator(domainScope);\n coordinatorsByScope.set(domainScope, nextCoordinator);\n return nextCoordinator;\n}\n\nexport function hasDomainSyncCoordinatorPendingWork(\n domainScope: DomainScope,\n): boolean {\n return coordinatorsByScope.get(domainScope)?.hasPendingWork() ?? false;\n}\n\n// Re-request every pump-driven lane for a scope: a user-driven \"sync now\" that\n// retries durable work stranded by a transient failure. Observational upload\n// telemetry is never directly pumped. A no-op when no coordinator exists yet\n// (nothing has been synced) or it has been disposed.\nexport function requestAllDomainSyncLanes(domainScope: DomainScope): void {\n coordinatorsByScope.get(domainScope)?.requestAllLanes();\n}\n\nexport function requestDomainSyncLane(\n domainScope: DomainScope,\n key: string,\n): void {\n coordinatorsByScope.get(domainScope)?.requestLane(key);\n}\n\n// Stop admitting work and remove this scope's coordinator. Active callbacks\n// drain under their existing watchdogs; getOrCreate builds a fresh coordinator.\nexport function disposeDomainSyncCoordinator(domainScope: DomainScope): void {\n coordinatorsByScope.get(domainScope)?.dispose();\n}\n\nexport function getDomainSyncCoordinatorSnapshot(\n domainScope: DomainScope,\n): DomainSyncSnapshot {\n return getOrCreateDomainSyncCoordinator(domainScope).getSnapshot();\n}\n\nexport function subscribeToDomainSyncCoordinator(\n domainScope: DomainScope,\n listener: () => void,\n): () => void {\n let observers = observersByScope.get(domainScope);\n if (!observers) {\n observers = createListenerSet();\n observersByScope.set(domainScope, observers);\n }\n return observers.subscribe(listener);\n}\n\nexport function beginDomainSyncUploadLane(\n domainScope: DomainScope,\n key: string,\n options?: UploadSyncLaneOptions,\n): UploadSyncLane {\n return getOrCreateDomainSyncCoordinator(domainScope).beginUploadLane(\n key,\n options,\n );\n}\n\nexport function waitForDomainSyncCoordinatorToSettle(\n domainScope: DomainScope,\n options?: SyncIdleOptions,\n): Promise<boolean> {\n return (\n coordinatorsByScope.get(domainScope)?.waitForIdle(options) ??\n Promise.resolve(true)\n );\n}\n"]}
@@ -2492,6 +2492,7 @@ export declare class ApiClient extends ApiClientAreas {
2492
2492
  organizationId: string;
2493
2493
  parentId: string | null;
2494
2494
  updatedAt: string;
2495
+ restorationId?: string | undefined;
2495
2496
  systemSlot?: string | null | undefined;
2496
2497
  }[];
2497
2498
  nextWatermark: {
@@ -96,6 +96,7 @@ export declare class ApiClientContext {
96
96
  organizationId: string;
97
97
  parentId: string | null;
98
98
  updatedAt: string;
99
+ restorationId?: string | undefined;
99
100
  systemSlot?: string | null | undefined;
100
101
  }[];
101
102
  nextWatermark: {
@@ -75,6 +75,7 @@ export declare class ContainerReadArea {
75
75
  organizationId: string;
76
76
  parentId: string | null;
77
77
  updatedAt: string;
78
+ restorationId?: string | undefined;
78
79
  systemSlot?: string | null | undefined;
79
80
  }[];
80
81
  nextWatermark: {
@@ -163,12 +163,20 @@ export function listContainerParentLanesRequestKey(input) {
163
163
  })));
164
164
  }
165
165
  export function listContainerDocumentLanesRequestKey(input) {
166
- return JSON.stringify(input.lanes.map((lane) => ({
167
- containerId: lane.containerId,
168
- laneId: lane.laneId,
169
- limit: lane.limit ?? null,
170
- watermark: syncWatermarkRequestKey(lane.watermark),
171
- })));
166
+ return JSON.stringify({
167
+ afterParentPage: input.afterParentPage
168
+ ? [
169
+ input.afterParentPage.parentId,
170
+ syncWatermarkRequestKey(input.afterParentPage.watermark),
171
+ ]
172
+ : null,
173
+ lanes: input.lanes.map((lane) => ({
174
+ containerId: lane.containerId,
175
+ laneId: lane.laneId,
176
+ limit: lane.limit ?? null,
177
+ watermark: syncWatermarkRequestKey(lane.watermark),
178
+ })),
179
+ });
172
180
  }
173
181
  export function cachedRequest(cache, cacheKey, request) {
174
182
  const cached = cache.get(cacheKey);