@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
@@ -1 +1 @@
1
- {"version":3,"file":"documentDiscoveryTypes.js","sourceRoot":"","sources":["documentDiscoveryTypes.ts"],"names":[],"mappings":"AAoOA;;;;GAIG;AACH,MAAM,OAAO,wCAAyC,SAAQ,KAAK;IAC5C;IAArB,YAAqB,WAAmB;QACtC,KAAK,CAAC,kCAAkC,WAAW,iBAAiB,CAAC,CAAC;QADnD,gBAAW,GAAX,WAAW,CAAQ;QAEtC,IAAI,CAAC,IAAI,GAAG,0CAA0C,CAAC;IACzD,CAAC;CACF","sourcesContent":["import type {\n ListContainerDocumentLanesRequest,\n ListContainerParentLanesRequest,\n} from \"@tearleads/validators/request\";\nimport type {\n EffectiveAccessLevel,\n ReferencedPrincipalStateResponse,\n SyncWatermark,\n} from \"@tearleads/validators/response\";\nimport type {\n DiscoveredDocumentInput,\n DocumentSummary,\n} from \"../../../data/documents/documentSummary\";\nimport type { DiscoveredDocumentCandidate } from \"../../../data/persistence/documents/documentDiscoveryEvidencePersistence\";\n\nexport interface ListedContainerDocument {\n createdAt: string;\n currentAccessEpoch: number;\n currentAccessStateHash: string;\n effectiveAccessLevel?: EffectiveAccessLevel | undefined;\n id: string;\n linkedContainerIds: string[];\n referencedPrincipals: ReferencedPrincipalStateResponse[];\n updatedAt: string;\n}\n\nexport interface ListContainerDocumentsResponse {\n hasMore: boolean;\n items: ListedContainerDocument[];\n nextWatermark: SyncWatermark | null;\n tombstones: ReadonlyArray<{\n containerId: string;\n documentId: string;\n updatedAt: string;\n }>;\n}\n\nexport interface ListedContainer {\n id: string;\n}\n\nexport interface ListContainersResponse {\n hasMore: boolean;\n items: ListedContainer[];\n nextWatermark: SyncWatermark | null;\n}\n\nexport interface ListContainerParentLanesResponse {\n results: ReadonlyArray<{\n laneId: string;\n page: ListContainersResponse;\n }>;\n}\n\nexport type ContainerDocumentLaneResult =\n | { readonly laneId: string; readonly page: ListContainerDocumentsResponse }\n | {\n readonly failure: { readonly code?: string | undefined };\n readonly laneId: string;\n };\n\nexport interface ContainerDocumentDiscoveryApi {\n readonly listContainerDocumentLanes: (\n input: ListContainerDocumentLanesRequest,\n options?: { readonly reportErrors?: boolean | undefined },\n ) => Promise<{\n readonly results: ReadonlyArray<ContainerDocumentLaneResult>;\n } | null>;\n readonly listContainerParentLanes: (\n input: ListContainerParentLanesRequest,\n ) => Promise<ListContainerParentLanesResponse | null>;\n}\n\n/**\n * One page per lane, aligned with the requested lanes: `null` for a lane the\n * server rejected, or `null` overall when the request itself failed.\n */\nexport type ContainerDocumentLaneLister = (\n lanes: ReadonlyArray<{\n readonly containerId: string;\n readonly watermark: SyncWatermark | null;\n }>,\n) => Promise<ReadonlyArray<ListContainerDocumentsResponse | null> | null>;\n\nexport interface DocumentLinkInput {\n accessEpoch?: number | undefined;\n containerIds: ReadonlyArray<string>;\n documentId: string;\n}\n\nexport type ContainerDocumentTombstone =\n ListContainerDocumentsResponse[\"tombstones\"][number];\n\nexport interface VerifiedContainerDocumentTombstone\n extends ContainerDocumentTombstone {\n /** The verified head's link-set epoch, not below the local document epoch. */\n readonly accessEpoch: number;\n /** The verified head link set; it omits `containerId`. */\n readonly linkedContainerIds: ReadonlyArray<string>;\n}\n\n/**\n * What the document's signed head says about a listing tombstone.\n * - `verified`: the verified head link set omits the container; apply.\n * - `refuted`: the verified head still links the container; keep visible and retry.\n * - `unverified`: no verified head is available; hold, hide, retry later.\n * - `deferred`: not attempted this run (the per-run head-load cap); hold\n * without counting an attempt, so the retry backoff does not grow.\n */\nexport type ContainerDocumentTombstoneVerdict =\n | {\n readonly kind: \"verified\";\n readonly tombstone: VerifiedContainerDocumentTombstone;\n }\n | {\n readonly kind: \"refuted\";\n /** The verified head link set, which contains the container. */\n readonly linkedContainerIds: ReadonlyArray<string>;\n readonly tombstone: ContainerDocumentTombstone;\n }\n | {\n readonly kind: \"unverified\";\n readonly tombstone: ContainerDocumentTombstone;\n }\n | {\n readonly kind: \"deferred\";\n readonly tombstone: ContainerDocumentTombstone;\n };\n\nexport interface HeldContainerDocumentTombstoneInput\n extends ContainerDocumentTombstone {\n /** Held without a verification attempt; the backoff does not advance. */\n readonly deferred?: boolean | undefined;\n /** A verified head still links it; retry the tombstone while keeping it visible. */\n readonly refuted?: boolean | undefined;\n}\n\nexport type ContainerDocumentTombstoneVerifier = (\n tombstones: ReadonlyArray<ContainerDocumentTombstone>,\n) => Promise<ReadonlyArray<ContainerDocumentTombstoneVerdict>>;\n\nexport type ContainerDocumentPlacement = Pick<\n ContainerDocumentTombstone,\n \"containerId\" | \"documentId\"\n>;\n\nexport interface ContainerDocumentTombstoneHoldStore {\n holdContainerDocumentTombstones: (\n tombstones: ReadonlyArray<HeldContainerDocumentTombstoneInput>,\n ) => Promise<void>;\n /** Held tombstones on these containers that are due for another attempt. */\n listHeldContainerDocumentTombstones: (\n containerIds: ReadonlyArray<string>,\n ) => Promise<ReadonlyArray<ContainerDocumentTombstone>>;\n /** The subset of placements that exist locally (link row or primary). */\n listKnownContainerDocumentPlacements: (\n placements: ReadonlyArray<ContainerDocumentPlacement>,\n ) => Promise<ReadonlyArray<ContainerDocumentPlacement>>;\n refuteContainerDocumentTombstoneHolds: (\n placements: ReadonlyArray<ContainerDocumentPlacement>,\n ) => Promise<void>;\n}\n\nexport interface DiscoverContainerDocumentsOptions\n extends ContainerDocumentTombstoneHoldStore {\n applyContainerDocumentTombstones: (\n tombstones: ReadonlyArray<VerifiedContainerDocumentTombstone>,\n ) => Promise<ReadonlyArray<DocumentSummary>>;\n verifyContainerDocumentTombstones: ContainerDocumentTombstoneVerifier;\n /** Allocate durable local order before fetching a listing. */\n beginDocumentDiscovery: () => Promise<number>;\n /** Persist pending candidates and return only signed placements. */\n verifyDiscoveredDocuments: (\n inputs: ReadonlyArray<DiscoveredDocumentCandidate>,\n containerIds: ReadonlyArray<string>,\n generation: number,\n tombstones?: ReadonlyArray<ContainerDocumentTombstone>,\n ) => Promise<{\n inputs: ReadonlyArray<DiscoveredDocumentInput>;\n /** False once a remote trust reset cancels this listing pass. */\n isCurrent: () => Promise<boolean>;\n /** Acknowledge only after local apply; true means no pending candidates remain. */\n commit: () => Promise<boolean>;\n }>;\n cacheReferencedPrincipalPolicies?: (\n references: ReadonlyArray<ReferencedPrincipalStateResponse>,\n ) => Promise<void>;\n containerId: string;\n /** Completed unwatermarked listing, published only after local apply succeeds. */\n onFullListing?: ((documentIds: ReadonlyArray<string>) => void) | undefined;\n loadContainerDocumentWatermark: (\n containerId: string,\n ) => Promise<SyncWatermark | null>;\n listContainerDocumentLanes: ContainerDocumentLaneLister;\n /** Recheck stillCurrent under the mutation lock before persisting a listing. */\n replaceDocumentLinksBatch: (\n inputs: ReadonlyArray<DocumentLinkInput>,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<void>;\n saveContainerDocumentWatermark: (\n containerId: string,\n watermark: SyncWatermark,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<void>;\n upsertDiscoveredDocuments: (\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<ReadonlyArray<DocumentSummary>>;\n}\n\nexport interface ListedContainerDocuments {\n isFullListing: boolean;\n items: ListedContainerDocument[];\n nextWatermark: SyncWatermark | null;\n tombstones: ContainerDocumentTombstone[];\n}\n\n/**\n * One lane's listing: complete; `null` when the server refused the lane or a\n * continuation came back without a watermark; or `\"unavailable\"` when the\n * batched request carrying it failed or timed out, which says nothing about\n * the lane and is worth retrying.\n */\nexport type ContainerDocumentLaneListing =\n | ListedContainerDocuments\n | \"unavailable\"\n | null;\n\n/**\n * Discovery could not list the container because its listing request failed\n * or timed out. Unlike a refused lane, a later attempt may succeed, so the\n * reconciler treats it as a failed reconciliation, not a skipped one.\n */\nexport class ContainerDocumentListingUnavailableError extends Error {\n constructor(readonly containerId: string) {\n super(`Document listing for container ${containerId} is unavailable`);\n this.name = \"ContainerDocumentListingUnavailableError\";\n }\n}\n"]}
1
+ {"version":3,"file":"documentDiscoveryTypes.js","sourceRoot":"","sources":["documentDiscoveryTypes.ts"],"names":[],"mappings":"AAwOA;;;;GAIG;AACH,MAAM,OAAO,wCAAyC,SAAQ,KAAK;IAC5C;IAArB,YAAqB,WAAmB;QACtC,KAAK,CAAC,kCAAkC,WAAW,iBAAiB,CAAC,CAAC;QADnD,gBAAW,GAAX,WAAW,CAAQ;QAEtC,IAAI,CAAC,IAAI,GAAG,0CAA0C,CAAC;IACzD,CAAC;CACF","sourcesContent":["import type {\n ListContainerDocumentLanesRequest,\n ListContainerParentLanesRequest,\n} from \"@tearleads/validators/request\";\nimport type {\n EffectiveAccessLevel,\n ReferencedPrincipalStateResponse,\n SyncWatermark,\n} from \"@tearleads/validators/response\";\nimport type {\n DiscoveredDocumentInput,\n DocumentSummary,\n} from \"../../../data/documents/documentSummary\";\nimport type { DiscoveredDocumentCandidate } from \"../../../data/persistence/documents/documentDiscoveryEvidencePersistence\";\n\nexport interface ListedContainerDocument {\n createdAt: string;\n currentAccessEpoch: number;\n currentAccessStateHash: string;\n effectiveAccessLevel?: EffectiveAccessLevel | undefined;\n id: string;\n linkedContainerIds: string[];\n referencedPrincipals: ReferencedPrincipalStateResponse[];\n updatedAt: string;\n}\n\nexport interface ListContainerDocumentsResponse {\n hasMore: boolean;\n items: ListedContainerDocument[];\n nextWatermark: SyncWatermark | null;\n tombstones: ReadonlyArray<{\n containerId: string;\n documentId: string;\n updatedAt: string;\n }>;\n}\n\nexport interface ListedContainer {\n id: string;\n}\n\nexport interface ListContainersResponse {\n hasMore: boolean;\n items: ListedContainer[];\n nextWatermark: SyncWatermark | null;\n}\n\nexport interface ListContainerParentLanesResponse {\n results: ReadonlyArray<{\n laneId: string;\n page: ListContainersResponse;\n }>;\n}\n\nexport type ContainerDocumentLaneResult =\n | { readonly laneId: string; readonly page: ListContainerDocumentsResponse }\n | {\n readonly failure: { readonly code?: string | undefined };\n readonly laneId: string;\n };\n\nexport interface ContainerDocumentDiscoveryApi {\n readonly listContainerDocumentLanes: (\n input: ListContainerDocumentLanesRequest,\n options?: { readonly reportErrors?: boolean | undefined },\n ) => Promise<{\n readonly results: ReadonlyArray<ContainerDocumentLaneResult>;\n } | null>;\n readonly listContainerParentLanes: (\n input: ListContainerParentLanesRequest,\n ) => Promise<ListContainerParentLanesResponse | null>;\n}\n\n/**\n * One page per lane, aligned with the requested lanes: `null` for a lane the\n * server rejected, or `null` overall when the request itself failed.\n */\nexport type ContainerDocumentLaneLister = (\n lanes: ReadonlyArray<{\n readonly containerId: string;\n readonly watermark: SyncWatermark | null;\n }>,\n) => Promise<ReadonlyArray<ListContainerDocumentsResponse | null> | null>;\n\nexport interface DocumentLinkInput {\n accessEpoch?: number | undefined;\n containerIds: ReadonlyArray<string>;\n documentId: string;\n}\n\nexport type ContainerDocumentTombstone =\n ListContainerDocumentsResponse[\"tombstones\"][number];\n\nexport interface VerifiedContainerDocumentTombstone\n extends ContainerDocumentTombstone {\n /** The verified head's link-set epoch, not below the local document epoch. */\n readonly accessEpoch: number;\n /** The verified head link set; it omits `containerId`. */\n readonly linkedContainerIds: ReadonlyArray<string>;\n}\n\n/**\n * What the document's signed head says about a listing tombstone.\n * - `verified`: the verified head link set omits the container; apply.\n * - `refuted`: the verified head still links the container; keep visible and retry.\n * - `unverified`: no verified head is available; hold, hide, retry later.\n * - `deferred`: not attempted this run (the per-run head-load cap); hold\n * without counting an attempt, so the retry backoff does not grow.\n */\nexport type ContainerDocumentTombstoneVerdict =\n | {\n readonly kind: \"verified\";\n readonly tombstone: VerifiedContainerDocumentTombstone;\n }\n | {\n readonly kind: \"refuted\";\n /** The verified head link set, which contains the container. */\n readonly linkedContainerIds: ReadonlyArray<string>;\n readonly tombstone: ContainerDocumentTombstone;\n }\n | {\n readonly kind: \"unverified\";\n readonly tombstone: ContainerDocumentTombstone;\n }\n | {\n readonly kind: \"deferred\";\n readonly tombstone: ContainerDocumentTombstone;\n };\n\nexport interface HeldContainerDocumentTombstoneInput\n extends ContainerDocumentTombstone {\n /** Held without a verification attempt; the backoff does not advance. */\n readonly deferred?: boolean | undefined;\n /** A verified head still links it; retry the tombstone while keeping it visible. */\n readonly refuted?: boolean | undefined;\n}\n\nexport type ContainerDocumentTombstoneVerifier = (\n tombstones: ReadonlyArray<ContainerDocumentTombstone>,\n) => Promise<ReadonlyArray<ContainerDocumentTombstoneVerdict>>;\n\nexport type ContainerDocumentPlacement = Pick<\n ContainerDocumentTombstone,\n \"containerId\" | \"documentId\"\n>;\n\nexport interface ContainerDocumentTombstoneHoldStore {\n holdContainerDocumentTombstones: (\n tombstones: ReadonlyArray<HeldContainerDocumentTombstoneInput>,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<void>;\n /** Held tombstones on these containers that are due for another attempt. */\n listHeldContainerDocumentTombstones: (\n containerIds: ReadonlyArray<string>,\n ) => Promise<ReadonlyArray<ContainerDocumentTombstone>>;\n /** The subset of placements that exist locally (link row or primary). */\n listKnownContainerDocumentPlacements: (\n placements: ReadonlyArray<ContainerDocumentPlacement>,\n ) => Promise<ReadonlyArray<ContainerDocumentPlacement>>;\n refuteContainerDocumentTombstoneHolds: (\n placements: ReadonlyArray<ContainerDocumentPlacement>,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<void>;\n}\n\nexport interface DiscoverContainerDocumentsOptions\n extends ContainerDocumentTombstoneHoldStore {\n applyContainerDocumentTombstones: (\n tombstones: ReadonlyArray<VerifiedContainerDocumentTombstone>,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<ReadonlyArray<DocumentSummary>>;\n verifyContainerDocumentTombstones: ContainerDocumentTombstoneVerifier;\n /** Allocate durable local order before fetching a listing. */\n beginDocumentDiscovery: () => Promise<number>;\n /** Persist pending candidates and return only signed placements. */\n verifyDiscoveredDocuments: (\n inputs: ReadonlyArray<DiscoveredDocumentCandidate>,\n containerIds: ReadonlyArray<string>,\n generation: number,\n tombstones?: ReadonlyArray<ContainerDocumentTombstone>,\n conflictingDocumentIds?: ReadonlySet<string>,\n ) => Promise<{\n inputs: ReadonlyArray<DiscoveredDocumentInput>;\n /** False once a remote trust reset cancels this listing pass. */\n isCurrent: () => Promise<boolean>;\n /** Acknowledge only after local apply; true means no pending candidates remain. */\n commit: () => Promise<boolean>;\n }>;\n cacheReferencedPrincipalPolicies?: (\n references: ReadonlyArray<ReferencedPrincipalStateResponse>,\n ) => Promise<void>;\n containerId: string;\n /** Completed unwatermarked listing, published only after local apply succeeds. */\n onFullListing?: ((documentIds: ReadonlyArray<string>) => void) | undefined;\n loadContainerDocumentWatermark: (\n containerId: string,\n ) => Promise<SyncWatermark | null>;\n listContainerDocumentLanes: ContainerDocumentLaneLister;\n /** Recheck stillCurrent under the mutation lock before persisting a listing. */\n replaceDocumentLinksBatch: (\n inputs: ReadonlyArray<DocumentLinkInput>,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<void>;\n saveContainerDocumentWatermark: (\n containerId: string,\n watermark: SyncWatermark,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<void>;\n upsertDiscoveredDocuments: (\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n stillCurrent: () => Promise<boolean>,\n ) => Promise<ReadonlyArray<DocumentSummary>>;\n}\n\nexport interface ListedContainerDocuments {\n isFullListing: boolean;\n items: ListedContainerDocument[];\n nextWatermark: SyncWatermark | null;\n tombstones: ContainerDocumentTombstone[];\n}\n\n/**\n * One lane's listing: complete; `null` when the server refused the lane or a\n * continuation came back without a watermark; or `\"unavailable\"` when the\n * batched request carrying it failed or timed out, which says nothing about\n * the lane and is worth retrying.\n */\nexport type ContainerDocumentLaneListing =\n | ListedContainerDocuments\n | \"unavailable\"\n | null;\n\n/**\n * Discovery could not list the container because its listing request failed\n * or timed out. Unlike a refused lane, a later attempt may succeed, so the\n * reconciler treats it as a failed reconciliation, not a skipped one.\n */\nexport class ContainerDocumentListingUnavailableError extends Error {\n constructor(readonly containerId: string) {\n super(`Document listing for container ${containerId} is unavailable`);\n this.name = \"ContainerDocumentListingUnavailableError\";\n }\n}\n"]}
@@ -0,0 +1,15 @@
1
+ /** Select the server's publication-based recovery contract during rollout.
2
+ * Continuation positions inside the token remain opaque to the device. */
3
+ export function isPublicationWatermark(watermark) {
4
+ return watermark?.id.startsWith("d1/") === true;
5
+ }
6
+ /** An initial legacy cursor can replay on the upgraded API. Once a page has
7
+ * been read, both sides of the continuation must use the same ordering. */
8
+ export function changesDiscoveryOrder(previous, next, isContinuation) {
9
+ if (!previous || !next)
10
+ return false;
11
+ return isPublicationWatermark(previous)
12
+ ? !isPublicationWatermark(next)
13
+ : isContinuation && isPublicationWatermark(next);
14
+ }
15
+ //# sourceMappingURL=publicationWatermark.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publicationWatermark.js","sourceRoot":"","sources":["publicationWatermark.ts"],"names":[],"mappings":"AAEA;0EAC0E;AAC1E,MAAM,UAAU,sBAAsB,CACpC,SAA2C;IAE3C,OAAO,SAAS,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC;AAClD,CAAC;AAED;2EAC2E;AAC3E,MAAM,UAAU,qBAAqB,CACnC,QAA0C,EAC1C,IAA0B,EAC1B,cAAuB;IAEvB,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI;QAAE,OAAO,KAAK,CAAC;IACrC,OAAO,sBAAsB,CAAC,QAAQ,CAAC;QACrC,CAAC,CAAC,CAAC,sBAAsB,CAAC,IAAI,CAAC;QAC/B,CAAC,CAAC,cAAc,IAAI,sBAAsB,CAAC,IAAI,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type { SyncWatermark } from \"@tearleads/validators/response\";\n\n/** Select the server's publication-based recovery contract during rollout.\n * Continuation positions inside the token remain opaque to the device. */\nexport function isPublicationWatermark(\n watermark: SyncWatermark | null | undefined,\n): boolean {\n return watermark?.id.startsWith(\"d1/\") === true;\n}\n\n/** An initial legacy cursor can replay on the upgraded API. Once a page has\n * been read, both sides of the continuation must use the same ordering. */\nexport function changesDiscoveryOrder(\n previous: SyncWatermark | null | undefined,\n next: SyncWatermark | null,\n isContinuation: boolean,\n): boolean {\n if (!previous || !next) return false;\n return isPublicationWatermark(previous)\n ? !isPublicationWatermark(next)\n : isContinuation && isPublicationWatermark(next);\n}\n"]}
@@ -6,8 +6,11 @@ export async function loadLocalContainerStates(input) {
6
6
  const storedContainers = await persistence.loadContainers(execSql);
7
7
  const containerStates = [];
8
8
  for (const storedContainer of storedContainers) {
9
+ if (input.isCurrent?.() === false)
10
+ break;
9
11
  containerStates.push(await hydrateStoredContainerState({
10
12
  execSql,
13
+ isCurrent: input.isCurrent,
11
14
  persistence,
12
15
  storedContainer,
13
16
  }));
@@ -1 +1 @@
1
- {"version":3,"file":"localState.js","sourceRoot":"","sources":["localState.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AAIrE,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,KAG9C;IACC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;IACtC,MAAM,WAAW,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IACxC,MAAM,gBAAgB,GAAG,MAAM,WAAW,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAEnE,MAAM,eAAe,GAAqB,EAAE,CAAC;IAC7C,KAAK,MAAM,eAAe,IAAI,gBAAgB,EAAE,CAAC;QAC/C,eAAe,CAAC,IAAI,CAClB,MAAM,2BAA2B,CAAC;YAChC,OAAO;YACP,WAAW;YACX,eAAe;SAChB,CAAC,CACH,CAAC;IACJ,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC","sourcesContent":["import type { ContainerContentsPersistence } from \"./containerPersistence\";\nimport type { ContainerState } from \"./remoteHydration\";\nimport type { ContainerContentsWorkflowSqlRuntime } from \"./runtime\";\nimport { hydrateStoredContainerState } from \"./storedContainerState\";\n\ntype LocalContainerStateRuntime = ContainerContentsWorkflowSqlRuntime;\n\nexport async function loadLocalContainerStates(input: {\n persistence: ContainerContentsPersistence;\n runtime: LocalContainerStateRuntime;\n}): Promise<ReadonlyArray<ContainerState>> {\n const { persistence, runtime } = input;\n const execSql = runtime.infra.execSql;\n await persistence.ensureSchema(execSql);\n const storedContainers = await persistence.loadContainers(execSql);\n\n const containerStates: ContainerState[] = [];\n for (const storedContainer of storedContainers) {\n containerStates.push(\n await hydrateStoredContainerState({\n execSql,\n persistence,\n storedContainer,\n }),\n );\n }\n\n return containerStates;\n}\n"]}
1
+ {"version":3,"file":"localState.js","sourceRoot":"","sources":["localState.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAC;AAIrE,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,KAI9C;IACC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC;IACvC,MAAM,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;IACtC,MAAM,WAAW,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;IACxC,MAAM,gBAAgB,GAAG,MAAM,WAAW,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAEnE,MAAM,eAAe,GAAqB,EAAE,CAAC;IAC7C,KAAK,MAAM,eAAe,IAAI,gBAAgB,EAAE,CAAC;QAC/C,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE,KAAK,KAAK;YAAE,MAAM;QACzC,eAAe,CAAC,IAAI,CAClB,MAAM,2BAA2B,CAAC;YAChC,OAAO;YACP,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,WAAW;YACX,eAAe;SAChB,CAAC,CACH,CAAC;IACJ,CAAC;IAED,OAAO,eAAe,CAAC;AACzB,CAAC","sourcesContent":["import type { ContainerContentsPersistence } from \"./containerPersistence\";\nimport type { ContainerState } from \"./remoteHydration\";\nimport type { ContainerContentsWorkflowSqlRuntime } from \"./runtime\";\nimport { hydrateStoredContainerState } from \"./storedContainerState\";\n\ntype LocalContainerStateRuntime = ContainerContentsWorkflowSqlRuntime;\n\nexport async function loadLocalContainerStates(input: {\n isCurrent?: (() => boolean) | undefined;\n persistence: ContainerContentsPersistence;\n runtime: LocalContainerStateRuntime;\n}): Promise<ReadonlyArray<ContainerState>> {\n const { persistence, runtime } = input;\n const execSql = runtime.infra.execSql;\n await persistence.ensureSchema(execSql);\n const storedContainers = await persistence.loadContainers(execSql);\n\n const containerStates: ContainerState[] = [];\n for (const storedContainer of storedContainers) {\n if (input.isCurrent?.() === false) break;\n containerStates.push(\n await hydrateStoredContainerState({\n execSql,\n isCurrent: input.isCurrent,\n persistence,\n storedContainer,\n }),\n );\n }\n\n return containerStates;\n}\n"]}
@@ -0,0 +1,12 @@
1
+ import type { ReconnectCrawl, RemoteContainerHydrationHost, RemoteContainerHydrationState } from "./types.js";
2
+ export declare function hydrateRemoteContainers(input: {
3
+ followDiscoveredParentLanes?: boolean | undefined;
4
+ host: RemoteContainerHydrationHost;
5
+ isCurrent?: (() => boolean) | undefined;
6
+ reconnect?: ReconnectCrawl | undefined;
7
+ onFullyHydrated?: (() => Promise<void> | void) | undefined;
8
+ parentIds?: ReadonlyArray<string | null> | undefined;
9
+ resetAllLaneWatermarks?: boolean | undefined;
10
+ resetRootLaneWatermark?: boolean | undefined;
11
+ state: RemoteContainerHydrationState;
12
+ }): Promise<number>;
@@ -0,0 +1,93 @@
1
+ import { createContainerChildIndex } from "./lanes/childIndex.js";
2
+ import { createContainerParentHydrationQueue } from "./lanes/parentLaneQueue.js";
3
+ import { canHydrateRemoteContainers, hydrateContainerParentLanes, } from "./remoteContainers/remoteContainerPageHydration.js";
4
+ import { describeRemoteContainerHydration } from "./remoteHydrationLog.js";
5
+ async function finishRemoteHydration(input) {
6
+ if (input.isCurrent?.() === false) {
7
+ return;
8
+ }
9
+ if (input.changedCount > 0) {
10
+ input.host.updateSnapshot();
11
+ let insertedCount = 0;
12
+ for (const containerId of input.seenContainerObservations.keys()) {
13
+ if (!input.containerIdsBeforeHydration.has(containerId)) {
14
+ insertedCount++;
15
+ }
16
+ }
17
+ input.state.runtime.util.log(describeRemoteContainerHydration({
18
+ changedCount: input.changedCount,
19
+ insertedCount,
20
+ }));
21
+ }
22
+ if (input.complete &&
23
+ input.isCurrent?.() !== false &&
24
+ input.onFullyHydrated) {
25
+ await input.onFullyHydrated();
26
+ }
27
+ }
28
+ // Distinct correlation IDs prevent an acknowledged crawl joining an older in-flight HTTP listing.
29
+ let reconnectSequence = 0;
30
+ export async function hydrateRemoteContainers(input) {
31
+ const { host, state } = input;
32
+ if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {
33
+ return 0;
34
+ }
35
+ const seenContainerObservations = new Map();
36
+ const containerIdsBeforeHydration = new Set(state.containersById.keys());
37
+ const childIdsByParentId = createContainerChildIndex(state.containersById);
38
+ const { lanes, queueParentLane } = createContainerParentHydrationQueue({
39
+ containerIds: state.containersById.keys(),
40
+ parentIds: input.parentIds,
41
+ resetAllLaneWatermarks: input.resetAllLaneWatermarks,
42
+ resetRootLaneWatermark: input.resetRootLaneWatermark,
43
+ });
44
+ // Follow child lanes during an explicit recursive crawl or for containers first
45
+ // discovered in this pass. The latter lets a newly authorized root populate its
46
+ // descendants immediately, while routine ticks keep known subtrees cache-first.
47
+ const queueDiscoveredParentLane = (containerId) => {
48
+ if (input.followDiscoveredParentLanes ||
49
+ (containerId !== null && !containerIdsBeforeHydration.has(containerId))) {
50
+ queueParentLane(containerId);
51
+ }
52
+ };
53
+ let manualError;
54
+ const result = await hydrateContainerParentLanes({
55
+ reconnect: input.reconnect && {
56
+ laneIdPrefix: `r${++reconnectSequence}`,
57
+ recordManualError: (error) => {
58
+ manualError ??= error;
59
+ },
60
+ },
61
+ childIdsByParentId,
62
+ host,
63
+ isCurrent: input.isCurrent,
64
+ lanes,
65
+ queueParentLane: queueDiscoveredParentLane,
66
+ seenContainerObservations,
67
+ state,
68
+ });
69
+ const { changedCount } = result;
70
+ await finishRemoteHydration({
71
+ changedCount,
72
+ complete: result.outcome === "complete" &&
73
+ canHydrateRemoteContainers(state) &&
74
+ input.isCurrent?.() !== false,
75
+ containerIdsBeforeHydration,
76
+ host,
77
+ isCurrent: input.isCurrent,
78
+ onFullyHydrated: input.onFullyHydrated,
79
+ seenContainerObservations,
80
+ state,
81
+ });
82
+ if (input.reconnect?.isCurrent() && canHydrateRemoteContainers(state)) {
83
+ const manualRejection = input.resetAllLaneWatermarks
84
+ ? manualError
85
+ : undefined;
86
+ input.reconnect.record(result.outcome, manualRejection);
87
+ // A manual refresh sharing this crawl observes the error it would receive on its own.
88
+ if (manualRejection)
89
+ throw manualRejection;
90
+ }
91
+ return changedCount;
92
+ }
93
+ //# sourceMappingURL=hydrateRemoteContainers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hydrateRemoteContainers.js","sourceRoot":"","sources":["hydrateRemoteContainers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAC/D,OAAO,EAAE,mCAAmC,EAAE,MAAM,yBAAyB,CAAC;AAC9E,OAAO,EACL,0BAA0B,EAC1B,2BAA2B,GAC5B,MAAM,iDAAiD,CAAC;AACzD,OAAO,EAAE,gCAAgC,EAAE,MAAM,sBAAsB,CAAC;AAQxE,KAAK,UAAU,qBAAqB,CAAC,KASpC;IACC,IAAI,KAAK,CAAC,SAAS,EAAE,EAAE,KAAK,KAAK,EAAE,CAAC;QAClC,OAAO;IACT,CAAC;IACD,IAAI,KAAK,CAAC,YAAY,GAAG,CAAC,EAAE,CAAC;QAC3B,KAAK,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;QAC5B,IAAI,aAAa,GAAG,CAAC,CAAC;QACtB,KAAK,MAAM,WAAW,IAAI,KAAK,CAAC,yBAAyB,CAAC,IAAI,EAAE,EAAE,CAAC;YACjE,IAAI,CA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C;QACd,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;QACxD,sFAAsF;QACtF,IAAI,eAAe;YAAE,MAAM,eAAe,CAAC;IAC7C,CAAC;IACD,OAAO,YAAY,CAAC;AACtB,CAAC","sourcesContent":["import { createContainerChildIndex } from \"./lanes/childIndex\";\nimport { createContainerParentHydrationQueue } from \"./lanes/parentLaneQueue\";\nimport {\n canHydrateRemoteContainers,\n hydrateContainerParentLanes,\n} from \"./remoteContainers/remoteContainerPageHydration\";\nimport { describeRemoteContainerHydration } from \"./remoteHydrationLog\";\nimport type {\n QueueContainerParentLane,\n ReconnectCrawl,\n RemoteContainerHydrationHost,\n RemoteContainerHydrationState,\n} from \"./types\";\n\nasync function finishRemoteHydration(input: {\n changedCount: number;\n complete: boolean;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n onFullyHydrated?: (() => Promise<void> | void) | undefined;\n seenContainerObservations: ReadonlyMap<string, string>;\n containerIdsBeforeHydration: ReadonlySet<string>;\n state: RemoteContainerHydrationState;\n}): Promise<void> {\n if (input.isCurrent?.() === false) {\n return;\n }\n if (input.changedCount > 0) {\n input.host.updateSnapshot();\n let insertedCount = 0;\n for (const containerId of input.seenContainerObservations.keys()) {\n if (!input.containerIdsBeforeHydration.has(containerId)) {\n insertedCount++;\n }\n }\n input.state.runtime.util.log(\n describeRemoteContainerHydration({\n changedCount: input.changedCount,\n insertedCount,\n }),\n );\n }\n if (\n input.complete &&\n input.isCurrent?.() !== false &&\n input.onFullyHydrated\n ) {\n await input.onFullyHydrated();\n }\n}\n\n// Distinct correlation IDs prevent an acknowledged crawl joining an older in-flight HTTP listing.\nlet reconnectSequence = 0;\n\nexport async function hydrateRemoteContainers(input: {\n followDiscoveredParentLanes?: boolean | undefined;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n reconnect?: ReconnectCrawl | undefined;\n onFullyHydrated?: (() => Promise<void> | void) | undefined;\n parentIds?: ReadonlyArray<string | null> | undefined;\n resetAllLaneWatermarks?: boolean | undefined;\n resetRootLaneWatermark?: boolean | undefined;\n state: RemoteContainerHydrationState;\n}): Promise<number> {\n const { host, state } = input;\n if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {\n return 0;\n }\n\n const seenContainerObservations = new Map<string, string>();\n const containerIdsBeforeHydration = new Set(state.containersById.keys());\n const childIdsByParentId = createContainerChildIndex(state.containersById);\n const { lanes, queueParentLane } = createContainerParentHydrationQueue({\n containerIds: state.containersById.keys(),\n parentIds: input.parentIds,\n resetAllLaneWatermarks: input.resetAllLaneWatermarks,\n resetRootLaneWatermark: input.resetRootLaneWatermark,\n });\n // Follow child lanes during an explicit recursive crawl or for containers first\n // discovered in this pass. The latter lets a newly authorized root populate its\n // descendants immediately, while routine ticks keep known subtrees cache-first.\n const queueDiscoveredParentLane: QueueContainerParentLane = (containerId) => {\n if (\n input.followDiscoveredParentLanes ||\n (containerId !== null && !containerIdsBeforeHydration.has(containerId))\n ) {\n queueParentLane(containerId);\n }\n };\n let manualError: Error | undefined;\n const result = await hydrateContainerParentLanes({\n reconnect: input.reconnect && {\n laneIdPrefix: `r${++reconnectSequence}`,\n recordManualError: (error) => {\n manualError ??= error;\n },\n },\n childIdsByParentId,\n host,\n isCurrent: input.isCurrent,\n lanes,\n queueParentLane: queueDiscoveredParentLane,\n seenContainerObservations,\n state,\n });\n const { changedCount } = result;\n await finishRemoteHydration({\n changedCount,\n complete:\n result.outcome === \"complete\" &&\n canHydrateRemoteContainers(state) &&\n input.isCurrent?.() !== false,\n containerIdsBeforeHydration,\n host,\n isCurrent: input.isCurrent,\n onFullyHydrated: input.onFullyHydrated,\n seenContainerObservations,\n state,\n });\n if (input.reconnect?.isCurrent() && canHydrateRemoteContainers(state)) {\n const manualRejection = input.resetAllLaneWatermarks\n ? manualError\n : undefined;\n input.reconnect.record(result.outcome, manualRejection);\n // A manual refresh sharing this crawl observes the error it would receive on its own.\n if (manualRejection) throw manualRejection;\n }\n return changedCount;\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  import type { RemoteContainer, RemoteContainerHydrationHost, RemoteContainerHydrationState } from "./types.js";
2
- export type { ContainerState, PersistContainerStateResult, RemoteContainer, RemoteContainerHydrationHost, } from "./types.js";
2
+ export { hydrateRemoteContainers } from "./hydrateRemoteContainers.js";
3
+ export type { ContainerState, HydrationOutcome, PersistContainerStateResult, ReconnectCrawl, RemoteContainer, RemoteContainerHydrationHost, } from "./types.js";
3
4
  export declare class StaleRemoteHydrationError extends Error {
4
5
  }
5
6
  export declare function createGenerationGuardedHydrationHost(input: {
@@ -12,17 +13,8 @@ interface RemoteContainerIngestor {
12
13
  resume: () => Promise<void>;
13
14
  }
14
15
  export declare function createRemoteContainerIngestor(input: {
16
+ ownsScope?: () => boolean;
15
17
  getSerializationBarrier?: (() => Promise<void> | null) | undefined;
16
18
  host: RemoteContainerHydrationHost;
17
19
  state: RemoteContainerHydrationState;
18
20
  }): RemoteContainerIngestor;
19
- export declare function hydrateRemoteContainers(input: {
20
- followDiscoveredParentLanes?: boolean | undefined;
21
- host: RemoteContainerHydrationHost;
22
- isCurrent?: (() => boolean) | undefined;
23
- onFullyHydrated?: (() => Promise<void> | void) | undefined;
24
- parentIds?: ReadonlyArray<string | null> | undefined;
25
- resetAllLaneWatermarks?: boolean | undefined;
26
- resetRootLaneWatermark?: boolean | undefined;
27
- state: RemoteContainerHydrationState;
28
- }): Promise<number>;
@@ -1,10 +1,7 @@
1
- import { finishRemoteHydration } from "./completeHydration.js";
2
- import { createContainerChildIndex } from "./lanes/childIndex.js";
3
- import { createContainerParentHydrationQueue } from "./lanes/parentLaneQueue.js";
4
1
  import { cacheRemoteContainerPrincipalPolicies } from "./principalPolicyCache.js";
5
2
  import { loadPausedContainerFilter } from "./remoteContainers/organizationSyncPauseFilter.js";
6
- import { canHydrateRemoteContainers, hydrateContainerParentLanes, } from "./remoteContainers/remoteContainerPageHydration.js";
7
3
  import { listRemoteContainerIdsWithPendingMetadataUpdates, listRemoteContainerIdsWithPendingStructuralIntents, upsertIsolatedRemoteContainerState, } from "./remoteContainers/remoteContainerState.js";
4
+ export { hydrateRemoteContainers } from "./hydrateRemoteContainers.js";
8
5
  export class StaleRemoteHydrationError extends Error {
9
6
  }
10
7
  export function createGenerationGuardedHydrationHost(input) {
@@ -192,7 +189,18 @@ export function createRemoteContainerIngestor(input) {
192
189
  const pendingRemoteContainersById = new Map();
193
190
  const generationByContainerId = new Map();
194
191
  let ingestRemoteContainersPromise = null;
192
+ const settleStale = (generation) => settleStaleRemoteContainerIngestGeneration({
193
+ generation,
194
+ generationByContainerId,
195
+ queue: pendingRemoteContainersById,
196
+ state,
197
+ });
195
198
  const runNextGeneration = () => {
199
+ if (input.ownsScope?.() === false) {
200
+ pendingRemoteContainersById.clear();
201
+ generationByContainerId.clear();
202
+ return Promise.resolve();
203
+ }
196
204
  if (ingestRemoteContainersPromise) {
197
205
  return ingestRemoteContainersPromise.then(runNextGeneration, runNextGeneration);
198
206
  }
@@ -213,7 +221,8 @@ export function createRemoteContainerIngestor(input) {
213
221
  pendingRemoteContainersById.delete(nextContainerId);
214
222
  return runNextGeneration();
215
223
  }
216
- const isCurrent = () => isRemoteContainerIngestGenerationCurrent(state, generation);
224
+ const isCurrent = () => input.ownsScope?.() !== false &&
225
+ isRemoteContainerIngestGenerationCurrent(state, generation);
217
226
  const guardedHost = createGenerationGuardedHydrationHost({
218
227
  host,
219
228
  isCurrent,
@@ -228,23 +237,12 @@ export function createRemoteContainerIngestor(input) {
228
237
  state,
229
238
  }))
230
239
  .then(() => {
231
- if (!isCurrent()) {
232
- settleStaleRemoteContainerIngestGeneration({
233
- generation,
234
- generationByContainerId,
235
- queue: pendingRemoteContainersById,
236
- state,
237
- });
238
- }
240
+ if (!isCurrent())
241
+ settleStale(generation);
239
242
  })
240
243
  .catch((error) => {
241
244
  if (!isCurrent() || error instanceof StaleRemoteHydrationError) {
242
- settleStaleRemoteContainerIngestGeneration({
243
- generation,
244
- generationByContainerId,
245
- queue: pendingRemoteContainersById,
246
- state,
247
- });
245
+ settleStale(generation);
248
246
  return;
249
247
  }
250
248
  throw error;
@@ -266,51 +264,4 @@ export function createRemoteContainerIngestor(input) {
266
264
  ingestRemoteContainer.resume = runNextGeneration;
267
265
  return ingestRemoteContainer;
268
266
  }
269
- export async function hydrateRemoteContainers(input) {
270
- const { host, state } = input;
271
- if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {
272
- return 0;
273
- }
274
- const seenContainerIds = new Set();
275
- const containerIdsBeforeHydration = new Set(state.containersById.keys());
276
- const childIdsByParentId = createContainerChildIndex(state.containersById);
277
- const { lanes, queueParentLane } = createContainerParentHydrationQueue({
278
- containerIds: state.containersById.keys(),
279
- parentIds: input.parentIds,
280
- resetAllLaneWatermarks: input.resetAllLaneWatermarks,
281
- resetRootLaneWatermark: input.resetRootLaneWatermark,
282
- });
283
- // Follow child lanes during an explicit recursive crawl or for containers first
284
- // discovered in this pass. The latter lets a newly authorized root populate its
285
- // descendants immediately, while routine ticks keep known subtrees cache-first.
286
- const queueDiscoveredParentLane = (containerId) => {
287
- if (input.followDiscoveredParentLanes ||
288
- (containerId !== null && !containerIdsBeforeHydration.has(containerId))) {
289
- queueParentLane(containerId);
290
- }
291
- };
292
- const result = await hydrateContainerParentLanes({
293
- childIdsByParentId,
294
- host,
295
- isCurrent: input.isCurrent,
296
- lanes,
297
- queueParentLane: queueDiscoveredParentLane,
298
- seenContainerIds,
299
- state,
300
- });
301
- const { changedCount } = result;
302
- await finishRemoteHydration({
303
- changedCount,
304
- complete: result.complete &&
305
- canHydrateRemoteContainers(state) &&
306
- input.isCurrent?.() !== false,
307
- containerIdsBeforeHydration,
308
- host,
309
- isCurrent: input.isCurrent,
310
- onFullyHydrated: input.onFullyHydrated,
311
- seenContainerIds,
312
- state,
313
- });
314
- return changedCount;
315
- }
316
267
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"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AA0B,CAAC,KAAK,CAAC;YACjC,KAAK,CAAC,SAAS,EAAE,EAAE,KAAK,KAAK;QAC/B,2BAA2B;QAC3B,IAAI;QACJ,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,eAAe,EAAE,KAAK,CAAC,eAAe;QACtC,gBAAgB;QAChB,KAAK;KACN,CAAC,CAAC;IACH,OAAO,YAAY,CAAC;AACtB,CAAC","sourcesContent":["import { finishRemoteHydration } from \"./completeHydration\";\nimport { createContainerChildIndex } from \"./lanes/childIndex\";\nimport { createContainerParentHydrationQueue } from \"./lanes/parentLaneQueue\";\nimport { cacheRemoteContainerPrincipalPolicies } from \"./principalPolicyCache\";\nimport { loadPausedContainerFilter } from \"./remoteContainers/organizationSyncPauseFilter\";\nimport {\n canHydrateRemoteContainers,\n hydrateContainerParentLanes,\n} from \"./remoteContainers/remoteContainerPageHydration\";\nimport {\n listRemoteContainerIdsWithPendingMetadataUpdates,\n listRemoteContainerIdsWithPendingStructuralIntents,\n upsertIsolatedRemoteContainerState,\n} from \"./remoteContainers/remoteContainerState\";\nimport type {\n QueueContainerParentLane,\n RemoteContainer,\n RemoteContainerHydrationHost,\n RemoteContainerHydrationState,\n RemoteContainerIngestQueue,\n} from \"./types\";\n\nexport type {\n ContainerState,\n PersistContainerStateResult,\n RemoteContainer,\n RemoteContainerHydrationHost,\n} from \"./types\";\n\nexport class StaleRemoteHydrationError extends Error {}\n\nexport function createGenerationGuardedHydrationHost(input: {\n host: RemoteContainerHydrationHost;\n isCurrent: () => boolean;\n}): RemoteContainerHydrationHost {\n const assertCurrent = () => {\n if (!input.isCurrent()) {\n throw new StaleRemoteHydrationError();\n }\n };\n\n return {\n persistContainerState: async (\n containerState,\n patch,\n updateView,\n saveOptions,\n mutationOptions,\n ) => {\n const callerIsCurrent = mutationOptions?.isCurrent;\n const isCurrent = callerIsCurrent\n ? () => input.isCurrent() && callerIsCurrent()\n : input.isCurrent;\n assertCurrent();\n const record = await input.host.persistContainerState(\n containerState,\n patch,\n updateView,\n saveOptions,\n { ...mutationOptions, isCurrent },\n );\n assertCurrent();\n return record;\n },\n ...(input.host.requestDocumentPriming\n ? {\n requestDocumentPriming: () => {\n if (input.isCurrent()) {\n input.host.requestDocumentPriming?.();\n }\n },\n }\n : {}),\n ...(input.host.requestMetadataRevalidation\n ? {\n requestMetadataRevalidation: async (request) => {\n assertCurrent();\n await input.host.requestMetadataRevalidation?.(request);\n assertCurrent();\n },\n }\n : {}),\n updateSnapshot: () => {\n if (input.isCurrent()) {\n input.host.updateSnapshot();\n }\n },\n };\n}\n\ninterface RemoteContainerIngestGeneration {\n readonly lifecycle: number | undefined;\n readonly structural: number | undefined;\n}\ntype RemoteContainerIngestGenerationMap = Map<\n string,\n RemoteContainerIngestGeneration\n>;\n\nfunction remoteContainerIngestGeneration(\n state: RemoteContainerHydrationState,\n): RemoteContainerIngestGeneration {\n return {\n lifecycle: state.lifecycleGeneration,\n structural: state.structuralGeneration,\n };\n}\n\nfunction remoteContainerIngestGenerationsMatch(\n left: RemoteContainerIngestGeneration | undefined,\n right: RemoteContainerIngestGeneration,\n): boolean {\n return (\n left !== undefined &&\n left.lifecycle === right.lifecycle &&\n left.structural === right.structural\n );\n}\n\nfunction isCurrentQueuedRemoteContainer(\n generation: RemoteContainerIngestGeneration,\n generationByContainerId: RemoteContainerIngestGenerationMap,\n queue: RemoteContainerIngestQueue,\n remoteContainer: RemoteContainer,\n): boolean {\n return (\n queue.get(remoteContainer.id) === remoteContainer &&\n remoteContainerIngestGenerationsMatch(\n generationByContainerId.get(remoteContainer.id),\n generation,\n )\n );\n}\n\nasync function upsertQueuedRemoteContainer(input: {\n containerIdsWithPendingMetadataUpdates: ReadonlySet<string>;\n containerIdsWithPendingStructuralIntents: ReadonlySet<string>;\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n host: RemoteContainerHydrationHost;\n isCurrent: () => boolean;\n queue: RemoteContainerIngestQueue;\n queuedRemoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}): Promise<boolean> {\n const {\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n generation,\n generationByContainerId,\n host,\n queue,\n queuedRemoteContainer,\n state,\n } = input;\n if (\n !input.isCurrent() ||\n !isCurrentQueuedRemoteContainer(\n generation,\n generationByContainerId,\n queue,\n queuedRemoteContainer,\n )\n ) {\n return false;\n }\n\n // A paused organization's container is acknowledged without being applied,\n // including when the pause lands while the commit is in flight.\n const isPaused = await loadPausedContainerFilter(state);\n if (isPaused(queuedRemoteContainer)) return true;\n const tombstones = await state.persistence.loadContainerHydrationTombstones(\n state.runtime.infra.execSql,\n );\n // A refused folder is reported and acknowledged; the batch continues.\n const upserted = await upsertIsolatedRemoteContainerState({\n expectedHydrationTombstone:\n tombstones.find((row) => row.containerId === queuedRemoteContainer.id) ??\n null,\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n host,\n isCurrent: () => input.isCurrent() && !isPaused(queuedRemoteContainer),\n remoteContainer: queuedRemoteContainer,\n state,\n });\n if (!upserted) {\n return false;\n }\n return true;\n}\n\nfunction acknowledgeRemoteContainerIngestBatch(input: {\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n isCurrent: () => boolean;\n queue: RemoteContainerIngestQueue;\n queuedRemoteContainers: ReadonlyArray<RemoteContainer>;\n}): void {\n if (!input.isCurrent()) {\n return;\n }\n for (const queuedRemoteContainer of input.queuedRemoteContainers) {\n if (\n isCurrentQueuedRemoteContainer(\n input.generation,\n input.generationByContainerId,\n input.queue,\n queuedRemoteContainer,\n )\n ) {\n input.queue.delete(queuedRemoteContainer.id);\n input.generationByContainerId.delete(queuedRemoteContainer.id);\n }\n }\n}\n\nasync function drainRemoteContainerIngestQueue(input: {\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n host: RemoteContainerHydrationHost;\n isCurrent: () => boolean;\n queue: RemoteContainerIngestQueue;\n state: RemoteContainerHydrationState;\n}) {\n const { generation, generationByContainerId, host, queue, state } = input;\n let shouldUpdateSnapshot = false;\n\n try {\n while (input.isCurrent()) {\n const queuedRemoteContainers = Array.from(queue.values()).filter(\n (remoteContainer) =>\n remoteContainerIngestGenerationsMatch(\n generationByContainerId.get(remoteContainer.id),\n generation,\n ),\n );\n if (queuedRemoteContainers.length === 0) {\n return;\n }\n await cacheRemoteContainerPrincipalPolicies({\n cacheReferencedPrincipalPolicies:\n state.runtime.principalPolicies.createWarmer(),\n remoteContainers: queuedRemoteContainers,\n stillCurrent: input.isCurrent,\n });\n if (!input.isCurrent()) {\n return;\n }\n const [\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n ] = await Promise.all([\n listRemoteContainerIdsWithPendingMetadataUpdates({\n remoteContainers: queuedRemoteContainers,\n state,\n }),\n listRemoteContainerIdsWithPendingStructuralIntents({\n remoteContainers: queuedRemoteContainers,\n state,\n }),\n ]);\n if (!input.isCurrent()) {\n return;\n }\n\n for (const queuedRemoteContainer of queuedRemoteContainers) {\n shouldUpdateSnapshot =\n (await upsertQueuedRemoteContainer({\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n generation,\n generationByContainerId,\n host,\n isCurrent: input.isCurrent,\n queue,\n queuedRemoteContainer,\n state,\n })) || shouldUpdateSnapshot;\n }\n acknowledgeRemoteContainerIngestBatch({\n generation,\n generationByContainerId,\n isCurrent: input.isCurrent,\n queue,\n queuedRemoteContainers,\n });\n\n if (shouldUpdateSnapshot) {\n host.updateSnapshot();\n shouldUpdateSnapshot = false;\n }\n }\n } catch (error) {\n if (shouldUpdateSnapshot) {\n host.updateSnapshot();\n }\n throw error;\n }\n}\n\nfunction isRemoteContainerIngestGenerationCurrent(\n state: RemoteContainerHydrationState,\n generation: RemoteContainerIngestGeneration,\n): boolean {\n return remoteContainerIngestGenerationsMatch(\n remoteContainerIngestGeneration(state),\n generation,\n );\n}\n\nfunction settleStaleRemoteContainerIngestGeneration(input: {\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n queue: RemoteContainerIngestQueue;\n state: RemoteContainerHydrationState;\n}): void {\n const currentGeneration = remoteContainerIngestGeneration(input.state);\n const shouldReplay =\n currentGeneration.lifecycle !== input.generation.lifecycle &&\n currentGeneration.structural === input.generation.structural;\n for (const [containerId, generation] of input.generationByContainerId) {\n if (!remoteContainerIngestGenerationsMatch(generation, input.generation)) {\n continue;\n }\n if (shouldReplay) {\n input.generationByContainerId.set(containerId, currentGeneration);\n } else {\n input.generationByContainerId.delete(containerId);\n input.queue.delete(containerId);\n }\n }\n}\n\ninterface RemoteContainerIngestor {\n (remoteContainer: RemoteContainer): Promise<void>;\n hasPending: () => boolean;\n resume: () => Promise<void>;\n}\n\nexport function createRemoteContainerIngestor(input: {\n getSerializationBarrier?: (() => Promise<void> | null) | undefined;\n host: RemoteContainerHydrationHost;\n state: RemoteContainerHydrationState;\n}): RemoteContainerIngestor {\n const { host, state } = input;\n const pendingRemoteContainersById: RemoteContainerIngestQueue = new Map();\n const generationByContainerId: RemoteContainerIngestGenerationMap = new Map();\n let ingestRemoteContainersPromise: Promise<void> | null = null;\n\n const runNextGeneration = (): Promise<void> => {\n if (ingestRemoteContainersPromise) {\n return ingestRemoteContainersPromise.then(\n runNextGeneration,\n runNextGeneration,\n );\n }\n if (\n state.runtime.infra.dbStatus !== undefined &&\n state.runtime.infra.dbStatus !== \"ready\"\n ) {\n return Promise.resolve();\n }\n const serializationBarrier = input.getSerializationBarrier?.();\n if (serializationBarrier) {\n return serializationBarrier.then(runNextGeneration);\n }\n const nextContainerId = pendingRemoteContainersById.keys().next().value;\n if (typeof nextContainerId !== \"string\") {\n return Promise.resolve();\n }\n const generation = generationByContainerId.get(nextContainerId);\n if (!generation) {\n pendingRemoteContainersById.delete(nextContainerId);\n return runNextGeneration();\n }\n const isCurrent = () =>\n isRemoteContainerIngestGenerationCurrent(state, generation);\n const guardedHost = createGenerationGuardedHydrationHost({\n host,\n isCurrent,\n });\n const nextPromise = Promise.resolve()\n .then(() =>\n drainRemoteContainerIngestQueue({\n generation,\n generationByContainerId,\n host: guardedHost,\n isCurrent,\n queue: pendingRemoteContainersById,\n state,\n }),\n )\n .then(() => {\n if (!isCurrent()) {\n settleStaleRemoteContainerIngestGeneration({\n generation,\n generationByContainerId,\n queue: pendingRemoteContainersById,\n state,\n });\n }\n })\n .catch((error: unknown) => {\n if (!isCurrent() || error instanceof StaleRemoteHydrationError) {\n settleStaleRemoteContainerIngestGeneration({\n generation,\n generationByContainerId,\n queue: pendingRemoteContainersById,\n state,\n });\n return;\n }\n throw error;\n })\n .finally(() => {\n if (ingestRemoteContainersPromise === nextPromise) {\n ingestRemoteContainersPromise = null;\n }\n });\n ingestRemoteContainersPromise = nextPromise;\n return nextPromise.then(() =>\n pendingRemoteContainersById.size > 0 ? runNextGeneration() : undefined,\n );\n };\n\n const ingestRemoteContainer = (remoteContainer: RemoteContainer) => {\n pendingRemoteContainersById.set(remoteContainer.id, remoteContainer);\n generationByContainerId.set(\n remoteContainer.id,\n remoteContainerIngestGeneration(state),\n );\n return runNextGeneration();\n };\n ingestRemoteContainer.hasPending = () => pendingRemoteContainersById.size > 0;\n ingestRemoteContainer.resume = runNextGeneration;\n return ingestRemoteContainer;\n}\n\nexport async function hydrateRemoteContainers(input: {\n followDiscoveredParentLanes?: boolean | undefined;\n host: RemoteContainerHydrationHost;\n isCurrent?: (() => boolean) | undefined;\n onFullyHydrated?: (() => Promise<void> | void) | undefined;\n parentIds?: ReadonlyArray<string | null> | undefined;\n resetAllLaneWatermarks?: boolean | undefined;\n resetRootLaneWatermark?: boolean | undefined;\n state: RemoteContainerHydrationState;\n}): Promise<number> {\n const { host, state } = input;\n if (!canHydrateRemoteContainers(state) || input.isCurrent?.() === false) {\n return 0;\n }\n\n const seenContainerIds = new Set<string>();\n const containerIdsBeforeHydration = new Set(state.containersById.keys());\n const childIdsByParentId = createContainerChildIndex(state.containersById);\n const { lanes, queueParentLane } = createContainerParentHydrationQueue({\n containerIds: state.containersById.keys(),\n parentIds: input.parentIds,\n resetAllLaneWatermarks: input.resetAllLaneWatermarks,\n resetRootLaneWatermark: input.resetRootLaneWatermark,\n });\n // Follow child lanes during an explicit recursive crawl or for containers first\n // discovered in this pass. The latter lets a newly authorized root populate its\n // descendants immediately, while routine ticks keep known subtrees cache-first.\n const queueDiscoveredParentLane: QueueContainerParentLane = (containerId) => {\n if (\n input.followDiscoveredParentLanes ||\n (containerId !== null && !containerIdsBeforeHydration.has(containerId))\n ) {\n queueParentLane(containerId);\n }\n };\n const result = await hydrateContainerParentLanes({\n childIdsByParentId,\n host,\n isCurrent: input.isCurrent,\n lanes,\n queueParentLane: queueDiscoveredParentLane,\n seenContainerIds,\n state,\n });\n const { changedCount } = result;\n await finishRemoteHydration({\n changedCount,\n complete:\n result.complete &&\n canHydrateRemoteContainers(state) &&\n input.isCurrent?.() !== false,\n containerIdsBeforeHydration,\n host,\n isCurrent: input.isCurrent,\n onFullyHydrated: input.onFullyHydrated,\n seenContainerIds,\n state,\n });\n return changedCount;\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qCAAqC,EAAE,MAAM,wBAAwB,CAAC;AAC/E,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAC;AAC3F,OAAO,EACL,gDAAgD,EAChD,kDAAkD,EAClD,kCAAkC,GACnC,MAAM,yCAAyC,CAAC;AAQjD,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAUpE,MAAM,OAAO,yBAA0B,SAAQ,KAAK;CAAG;AAEvD,MAAM,UAAU,oCAAoC,CAAC,KAGpD;IACC,MAAM,aAAa,GAAG,GAAG,EAAE;QACzB,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,CAAC;YACvB,MAAM,IAAI,yBAAyB,EAAE,CAAC;QACx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,CAAC;QACH,CAAC,CAAC,CAAC;QACL,6BAA6B,GAAG,WAAW,CAAC;QAC5C,OAAO,WAAW,CAAC,IAAI,CAAC,GAAG,EAAE,CAC3B,2BAA2B,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,iBAAiB,EAAE,CAAC,CAAC,CAAC,SAAS,CACvE,CAAC;IACJ,CAAC,CAAC;IAEF,MAAM,qBAAqB,GAAG,CAAC,eAAgC,EAAE,EAAE;QACjE,2BAA2B,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,EAAE,eAAe,CAAC,CAAC;QACrE,uBAAuB,CAAC,GAAG,CACzB,eAAe,CAAC,EAAE,EAClB,+BAA+B,CAAC,KAAK,CAAC,CACvC,CAAC;QACF,OAAO,iBAAiB,EAAE,CAAC;IAC7B,CAAC,CAAC;IACF,qBAAqB,CAAC,UAAU,GAAG,GAAG,EAAE,CAAC,2BAA2B,CAAC,IAAI,GAAG,CAAC,CAAC;IAC9E,qBAAqB,CAAC,MAAM,GAAG,iBAAiB,CAAC;IACjD,OAAO,qBAAqB,CAAC;AAC/B,CAAC","sourcesContent":["import { cacheRemoteContainerPrincipalPolicies } from \"./principalPolicyCache\";\nimport { loadPausedContainerFilter } from \"./remoteContainers/organizationSyncPauseFilter\";\nimport {\n listRemoteContainerIdsWithPendingMetadataUpdates,\n listRemoteContainerIdsWithPendingStructuralIntents,\n upsertIsolatedRemoteContainerState,\n} from \"./remoteContainers/remoteContainerState\";\nimport type {\n RemoteContainer,\n RemoteContainerHydrationHost,\n RemoteContainerHydrationState,\n RemoteContainerIngestQueue,\n} from \"./types\";\n\nexport { hydrateRemoteContainers } from \"./hydrateRemoteContainers\";\nexport type {\n ContainerState,\n HydrationOutcome,\n PersistContainerStateResult,\n ReconnectCrawl,\n RemoteContainer,\n RemoteContainerHydrationHost,\n} from \"./types\";\n\nexport class StaleRemoteHydrationError extends Error {}\n\nexport function createGenerationGuardedHydrationHost(input: {\n host: RemoteContainerHydrationHost;\n isCurrent: () => boolean;\n}): RemoteContainerHydrationHost {\n const assertCurrent = () => {\n if (!input.isCurrent()) {\n throw new StaleRemoteHydrationError();\n }\n };\n\n return {\n persistContainerState: async (\n containerState,\n patch,\n updateView,\n saveOptions,\n mutationOptions,\n ) => {\n const callerIsCurrent = mutationOptions?.isCurrent;\n const isCurrent = callerIsCurrent\n ? () => input.isCurrent() && callerIsCurrent()\n : input.isCurrent;\n assertCurrent();\n const record = await input.host.persistContainerState(\n containerState,\n patch,\n updateView,\n saveOptions,\n { ...mutationOptions, isCurrent },\n );\n assertCurrent();\n return record;\n },\n ...(input.host.requestDocumentPriming\n ? {\n requestDocumentPriming: () => {\n if (input.isCurrent()) {\n input.host.requestDocumentPriming?.();\n }\n },\n }\n : {}),\n ...(input.host.requestMetadataRevalidation\n ? {\n requestMetadataRevalidation: async (request) => {\n assertCurrent();\n await input.host.requestMetadataRevalidation?.(request);\n assertCurrent();\n },\n }\n : {}),\n updateSnapshot: () => {\n if (input.isCurrent()) {\n input.host.updateSnapshot();\n }\n },\n };\n}\n\ninterface RemoteContainerIngestGeneration {\n readonly lifecycle: number | undefined;\n readonly structural: number | undefined;\n}\ntype RemoteContainerIngestGenerationMap = Map<\n string,\n RemoteContainerIngestGeneration\n>;\n\nfunction remoteContainerIngestGeneration(\n state: RemoteContainerHydrationState,\n): RemoteContainerIngestGeneration {\n return {\n lifecycle: state.lifecycleGeneration,\n structural: state.structuralGeneration,\n };\n}\n\nfunction remoteContainerIngestGenerationsMatch(\n left: RemoteContainerIngestGeneration | undefined,\n right: RemoteContainerIngestGeneration,\n): boolean {\n return (\n left !== undefined &&\n left.lifecycle === right.lifecycle &&\n left.structural === right.structural\n );\n}\n\nfunction isCurrentQueuedRemoteContainer(\n generation: RemoteContainerIngestGeneration,\n generationByContainerId: RemoteContainerIngestGenerationMap,\n queue: RemoteContainerIngestQueue,\n remoteContainer: RemoteContainer,\n): boolean {\n return (\n queue.get(remoteContainer.id) === remoteContainer &&\n remoteContainerIngestGenerationsMatch(\n generationByContainerId.get(remoteContainer.id),\n generation,\n )\n );\n}\n\nasync function upsertQueuedRemoteContainer(input: {\n containerIdsWithPendingMetadataUpdates: ReadonlySet<string>;\n containerIdsWithPendingStructuralIntents: ReadonlySet<string>;\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n host: RemoteContainerHydrationHost;\n isCurrent: () => boolean;\n queue: RemoteContainerIngestQueue;\n queuedRemoteContainer: RemoteContainer;\n state: RemoteContainerHydrationState;\n}): Promise<boolean> {\n const {\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n generation,\n generationByContainerId,\n host,\n queue,\n queuedRemoteContainer,\n state,\n } = input;\n if (\n !input.isCurrent() ||\n !isCurrentQueuedRemoteContainer(\n generation,\n generationByContainerId,\n queue,\n queuedRemoteContainer,\n )\n ) {\n return false;\n }\n\n // A paused organization's container is acknowledged without being applied,\n // including when the pause lands while the commit is in flight.\n const isPaused = await loadPausedContainerFilter(state);\n if (isPaused(queuedRemoteContainer)) return true;\n const tombstones = await state.persistence.loadContainerHydrationTombstones(\n state.runtime.infra.execSql,\n );\n // A refused folder is reported and acknowledged; the batch continues.\n const upserted = await upsertIsolatedRemoteContainerState({\n expectedHydrationTombstone:\n tombstones.find((row) => row.containerId === queuedRemoteContainer.id) ??\n null,\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n host,\n isCurrent: () => input.isCurrent() && !isPaused(queuedRemoteContainer),\n remoteContainer: queuedRemoteContainer,\n state,\n });\n if (!upserted) {\n return false;\n }\n return true;\n}\n\nfunction acknowledgeRemoteContainerIngestBatch(input: {\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n isCurrent: () => boolean;\n queue: RemoteContainerIngestQueue;\n queuedRemoteContainers: ReadonlyArray<RemoteContainer>;\n}): void {\n if (!input.isCurrent()) {\n return;\n }\n for (const queuedRemoteContainer of input.queuedRemoteContainers) {\n if (\n isCurrentQueuedRemoteContainer(\n input.generation,\n input.generationByContainerId,\n input.queue,\n queuedRemoteContainer,\n )\n ) {\n input.queue.delete(queuedRemoteContainer.id);\n input.generationByContainerId.delete(queuedRemoteContainer.id);\n }\n }\n}\n\nasync function drainRemoteContainerIngestQueue(input: {\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n host: RemoteContainerHydrationHost;\n isCurrent: () => boolean;\n queue: RemoteContainerIngestQueue;\n state: RemoteContainerHydrationState;\n}) {\n const { generation, generationByContainerId, host, queue, state } = input;\n let shouldUpdateSnapshot = false;\n\n try {\n while (input.isCurrent()) {\n const queuedRemoteContainers = Array.from(queue.values()).filter(\n (remoteContainer) =>\n remoteContainerIngestGenerationsMatch(\n generationByContainerId.get(remoteContainer.id),\n generation,\n ),\n );\n if (queuedRemoteContainers.length === 0) {\n return;\n }\n await cacheRemoteContainerPrincipalPolicies({\n cacheReferencedPrincipalPolicies:\n state.runtime.principalPolicies.createWarmer(),\n remoteContainers: queuedRemoteContainers,\n stillCurrent: input.isCurrent,\n });\n if (!input.isCurrent()) {\n return;\n }\n const [\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n ] = await Promise.all([\n listRemoteContainerIdsWithPendingMetadataUpdates({\n remoteContainers: queuedRemoteContainers,\n state,\n }),\n listRemoteContainerIdsWithPendingStructuralIntents({\n remoteContainers: queuedRemoteContainers,\n state,\n }),\n ]);\n if (!input.isCurrent()) {\n return;\n }\n\n for (const queuedRemoteContainer of queuedRemoteContainers) {\n shouldUpdateSnapshot =\n (await upsertQueuedRemoteContainer({\n containerIdsWithPendingMetadataUpdates,\n containerIdsWithPendingStructuralIntents,\n generation,\n generationByContainerId,\n host,\n isCurrent: input.isCurrent,\n queue,\n queuedRemoteContainer,\n state,\n })) || shouldUpdateSnapshot;\n }\n acknowledgeRemoteContainerIngestBatch({\n generation,\n generationByContainerId,\n isCurrent: input.isCurrent,\n queue,\n queuedRemoteContainers,\n });\n\n if (shouldUpdateSnapshot) {\n host.updateSnapshot();\n shouldUpdateSnapshot = false;\n }\n }\n } catch (error) {\n if (shouldUpdateSnapshot) {\n host.updateSnapshot();\n }\n throw error;\n }\n}\n\nfunction isRemoteContainerIngestGenerationCurrent(\n state: RemoteContainerHydrationState,\n generation: RemoteContainerIngestGeneration,\n): boolean {\n return remoteContainerIngestGenerationsMatch(\n remoteContainerIngestGeneration(state),\n generation,\n );\n}\n\nfunction settleStaleRemoteContainerIngestGeneration(input: {\n generation: RemoteContainerIngestGeneration;\n generationByContainerId: RemoteContainerIngestGenerationMap;\n queue: RemoteContainerIngestQueue;\n state: RemoteContainerHydrationState;\n}): void {\n const currentGeneration = remoteContainerIngestGeneration(input.state);\n const shouldReplay =\n currentGeneration.lifecycle !== input.generation.lifecycle &&\n currentGeneration.structural === input.generation.structural;\n for (const [containerId, generation] of input.generationByContainerId) {\n if (!remoteContainerIngestGenerationsMatch(generation, input.generation)) {\n continue;\n }\n if (shouldReplay) {\n input.generationByContainerId.set(containerId, currentGeneration);\n } else {\n input.generationByContainerId.delete(containerId);\n input.queue.delete(containerId);\n }\n }\n}\n\ninterface RemoteContainerIngestor {\n (remoteContainer: RemoteContainer): Promise<void>;\n hasPending: () => boolean;\n resume: () => Promise<void>;\n}\n\nexport function createRemoteContainerIngestor(input: {\n ownsScope?: () => boolean;\n getSerializationBarrier?: (() => Promise<void> | null) | undefined;\n host: RemoteContainerHydrationHost;\n state: RemoteContainerHydrationState;\n}): RemoteContainerIngestor {\n const { host, state } = input;\n const pendingRemoteContainersById: RemoteContainerIngestQueue = new Map();\n const generationByContainerId: RemoteContainerIngestGenerationMap = new Map();\n let ingestRemoteContainersPromise: Promise<void> | null = null;\n\n const settleStale = (generation: RemoteContainerIngestGeneration) =>\n settleStaleRemoteContainerIngestGeneration({\n generation,\n generationByContainerId,\n queue: pendingRemoteContainersById,\n state,\n });\n\n const runNextGeneration = (): Promise<void> => {\n if (input.ownsScope?.() === false) {\n pendingRemoteContainersById.clear();\n generationByContainerId.clear();\n return Promise.resolve();\n }\n if (ingestRemoteContainersPromise) {\n return ingestRemoteContainersPromise.then(\n runNextGeneration,\n runNextGeneration,\n );\n }\n if (\n state.runtime.infra.dbStatus !== undefined &&\n state.runtime.infra.dbStatus !== \"ready\"\n ) {\n return Promise.resolve();\n }\n const serializationBarrier = input.getSerializationBarrier?.();\n if (serializationBarrier) {\n return serializationBarrier.then(runNextGeneration);\n }\n const nextContainerId = pendingRemoteContainersById.keys().next().value;\n if (typeof nextContainerId !== \"string\") {\n return Promise.resolve();\n }\n const generation = generationByContainerId.get(nextContainerId);\n if (!generation) {\n pendingRemoteContainersById.delete(nextContainerId);\n return runNextGeneration();\n }\n const isCurrent = () =>\n input.ownsScope?.() !== false &&\n isRemoteContainerIngestGenerationCurrent(state, generation);\n const guardedHost = createGenerationGuardedHydrationHost({\n host,\n isCurrent,\n });\n const nextPromise = Promise.resolve()\n .then(() =>\n drainRemoteContainerIngestQueue({\n generation,\n generationByContainerId,\n host: guardedHost,\n isCurrent,\n queue: pendingRemoteContainersById,\n state,\n }),\n )\n .then(() => {\n if (!isCurrent()) settleStale(generation);\n })\n .catch((error: unknown) => {\n if (!isCurrent() || error instanceof StaleRemoteHydrationError) {\n settleStale(generation);\n return;\n }\n throw error;\n })\n .finally(() => {\n if (ingestRemoteContainersPromise === nextPromise) {\n ingestRemoteContainersPromise = null;\n }\n });\n ingestRemoteContainersPromise = nextPromise;\n return nextPromise.then(() =>\n pendingRemoteContainersById.size > 0 ? runNextGeneration() : undefined,\n );\n };\n\n const ingestRemoteContainer = (remoteContainer: RemoteContainer) => {\n pendingRemoteContainersById.set(remoteContainer.id, remoteContainer);\n generationByContainerId.set(\n remoteContainer.id,\n remoteContainerIngestGeneration(state),\n );\n return runNextGeneration();\n };\n ingestRemoteContainer.hasPending = () => pendingRemoteContainersById.size > 0;\n ingestRemoteContainer.resume = runNextGeneration;\n return ingestRemoteContainer;\n}\n"]}
@@ -8,13 +8,14 @@ async function prepareContainerParentLanePage(input) {
8
8
  ? await new ContainerSyncWatermarkRepository(state.runtime.infra.execSql).loadWatermark(syncLane)
9
9
  : lane.watermark;
10
10
  return {
11
- lane,
11
+ lane: { ...lane, watermark },
12
12
  request: { laneId, parentId: lane.parentId, watermark },
13
13
  syncLane,
14
14
  };
15
15
  }
16
16
  export async function fetchContainerParentLaneBatch(input) {
17
17
  const { batch, state } = input;
18
+ const prefix = input.laneIdPrefix ?? state.parentLaneRequestPrefix ?? "lane";
18
19
  const expectedContainerStates = new Map(Array.from(state.containersById, ([containerId, containerState]) => [
19
20
  containerId,
20
21
  {
@@ -24,7 +25,7 @@ export async function fetchContainerParentLaneBatch(input) {
24
25
  ]));
25
26
  const preparedPages = await Promise.all(batch.map((lane, index) => prepareContainerParentLanePage({
26
27
  lane,
27
- laneId: `lane-${index}`,
28
+ laneId: `${prefix}-${index}`,
28
29
  state,
29
30
  })));
30
31
  if (input.isCurrent?.() === false) {
@@ -1 +1 @@
1
- {"version":3,"file":"parentLaneFetch.js","sourceRoot":"","sources":["parentLaneFetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gCAAgC,EAAE,MAAM,mFAAmF,CAAC;AACrI,OAAO,EAAE,6BAA6B,EAAE,MAAM,4BAA4B,CAAC;AAC3E,OAAO,EAAE,+BAA+B,EAAE,MAAM,+CAA+C,CAAC;AAOhG,KAAK,UAAU,8BAA8B,CAAC,KAI7C;IACC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;IACtC,MAAM,QAAQ,GAAG,6BAA6B,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9D,MAAM,SAAS,GACb,IAAI,CAAC,SAAS,KAAK,SAAS;QAC1B,CAAC,CAAC,MAAM,IAAI,gCAAgC,CACxC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAC5B,CAAC,aAAa,CAAC,QAAQ,CAAC;QAC3B,C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AAQ;SAChC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { ContainerSyncWatermarkRepository } from \"../../../../data/persistence/container-contents/containerSyncWatermarkPersistence\";\nimport { createContainerParentSyncLane } from \"../../containerPersistence\";\nimport { createContainerStateFingerprint } from \"../remoteContainers/containerStateFingerprint\";\nimport type {\n ContainerParentHydrationLane,\n FetchedContainerParentLanePage,\n RemoteContainerHydrationState,\n} from \"../types\";\n\nasync function prepareContainerParentLanePage(input: {\n lane: ContainerParentHydrationLane;\n laneId: string;\n state: RemoteContainerHydrationState;\n}) {\n const { lane, laneId, state } = input;\n const syncLane = createContainerParentSyncLane(lane.parentId);\n const watermark =\n lane.watermark === undefined\n ? await new ContainerSyncWatermarkRepository(\n state.runtime.infra.execSql,\n ).loadWatermark(syncLane)\n : lane.watermark;\n return {\n lane,\n request: { laneId, parentId: lane.parentId, watermark },\n syncLane,\n };\n}\n\nexport async function fetchContainerParentLaneBatch(input: {\n batch: ReadonlyArray<ContainerParentHydrationLane>;\n isCurrent?: (() => boolean) | undefined;\n state: RemoteContainerHydrationState;\n}): Promise<ReadonlyArray<FetchedContainerParentLanePage> | null> {\n const { batch, state } = input;\n const expectedContainerStates = new Map(\n Array.from(state.containersById, ([containerId, containerState]) => [\n containerId,\n {\n container: { ...containerState.container },\n fingerprint: createContainerStateFingerprint(containerState),\n },\n ]),\n );\n const preparedPages = await Promise.all(\n batch.map((lane, index) =>\n prepareContainerParentLanePage({\n lane,\n laneId: `lane-${index}`,\n state,\n }),\n ),\n );\n if (input.isCurrent?.() === false) {\n return null;\n }\n const expectedHydrationTombstones = new Map(\n (\n await state.persistence.loadContainerHydrationTombstones(\n state.runtime.infra.execSql,\n )\n ).map((tombstone) => [tombstone.containerId, tombstone]),\n );\n if (input.isCurrent?.() === false) {\n return null;\n }\n const response = await state.runtime.apiClient.listContainerParentLanes({\n lanes: preparedPages.map((page) => page.request),\n });\n if (!response) {\n // A superseded crawl's failure says nothing about the current tree.\n if (input.isCurrent?.() !== false) {\n state.containerListingFailures =\n (state.containerListingFailures ?? 0) + 1;\n }\n return null;\n }\n\n const pagesByLaneId = new Map(\n response.results.map((result) => [result.laneId, result.page]),\n );\n if (\n pagesByLaneId.size !== preparedPages.length ||\n response.results.length !== preparedPages.length\n ) {\n throw new Error(\"Container parent lane batch response mismatch\");\n }\n\n return preparedPages.map((preparedPage) => {\n const page = pagesByLaneId.get(preparedPage.request.laneId);\n if (!page) {\n throw new Error(\"Container parent lane batch response mismatch\");\n }\n return {\n expectedContainerStates,\n expectedHydrationTombstones,\n lane: preparedPage.lane,\n response: page,\n syncLane: preparedPage.syncLane,\n };\n });\n}\n"]}
1
+ {"version":3,"file":"parentLaneFetch.js","sourceRoot":"","sources":["parentLaneFetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gCAAgC,EAAE,MAAM,mFAAmF,CAAC;AACrI,OAAO,EAAE,6BAA6B,EAAE,MAAM,4BAA4B,CAAC;AAC3E,OAAO,EAAE,+BAA+B,EAAE,MAAM,+CAA+C,CAAC;AAOhG,KAAK,UAAU,8BAA8B,CAAC,KAI7C;IACC,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;IACtC,MAAM,QAAQ,GAAG,6BAA6B,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9D,MAAM,SAAS,GACb,IAAI,CAAC,SAAS,KAAK,SAAS;QAC1B,CAAC,CAAC,MAAM,IAAI,gCAAgC,CACxC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAC5B,CAAC,aAAa,CAAC,QAAQ,CAAC;QAC3B,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC;IACrB,OAAO;QACL,IAAI,EAAE,EAAE,GAAG,IAAI,EAAE,SAAS,EAAE;QAC5B,OAAO,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,SAAS,EAAE;QACvD,QAAQ;KACT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":["import { ContainerSyncWatermarkRepository } from \"../../../../data/persistence/container-contents/containerSyncWatermarkPersistence\";\nimport { createContainerParentSyncLane } from \"../../containerPersistence\";\nimport { createContainerStateFingerprint } from \"../remoteContainers/containerStateFingerprint\";\nimport type {\n ContainerParentHydrationLane,\n FetchedContainerParentLanePage,\n RemoteContainerHydrationState,\n} from \"../types\";\n\nasync function prepareContainerParentLanePage(input: {\n lane: ContainerParentHydrationLane;\n laneId: string;\n state: RemoteContainerHydrationState;\n}) {\n const { lane, laneId, state } = input;\n const syncLane = createContainerParentSyncLane(lane.parentId);\n const watermark =\n lane.watermark === undefined\n ? await new ContainerSyncWatermarkRepository(\n state.runtime.infra.execSql,\n ).loadWatermark(syncLane)\n : lane.watermark;\n return {\n lane: { ...lane, watermark },\n request: { laneId, parentId: lane.parentId, watermark },\n syncLane,\n };\n}\n\nexport async function fetchContainerParentLaneBatch(input: {\n batch: ReadonlyArray<ContainerParentHydrationLane>;\n laneIdPrefix?: string | undefined;\n isCurrent?: (() => boolean) | undefined;\n state: RemoteContainerHydrationState;\n}): Promise<ReadonlyArray<FetchedContainerParentLanePage> | null> {\n const { batch, state } = input;\n const prefix = input.laneIdPrefix ?? state.parentLaneRequestPrefix ?? \"lane\";\n const expectedContainerStates = new Map(\n Array.from(state.containersById, ([containerId, containerState]) => [\n containerId,\n {\n container: { ...containerState.container },\n fingerprint: createContainerStateFingerprint(containerState),\n },\n ]),\n );\n const preparedPages = await Promise.all(\n batch.map((lane, index) =>\n prepareContainerParentLanePage({\n lane,\n laneId: `${prefix}-${index}`,\n state,\n }),\n ),\n );\n if (input.isCurrent?.() === false) {\n return null;\n }\n const expectedHydrationTombstones = new Map(\n (\n await state.persistence.loadContainerHydrationTombstones(\n state.runtime.infra.execSql,\n )\n ).map((tombstone) => [tombstone.containerId, tombstone]),\n );\n if (input.isCurrent?.() === false) {\n return null;\n }\n const response = await state.runtime.apiClient.listContainerParentLanes({\n lanes: preparedPages.map((page) => page.request),\n });\n if (!response) {\n // A superseded crawl's failure says nothing about the current tree.\n if (input.isCurrent?.() !== false) {\n state.containerListingFailures =\n (state.containerListingFailures ?? 0) + 1;\n }\n return null;\n }\n\n const pagesByLaneId = new Map(\n response.results.map((result) => [result.laneId, result.page]),\n );\n if (\n pagesByLaneId.size !== preparedPages.length ||\n response.results.length !== preparedPages.length\n ) {\n throw new Error(\"Container parent lane batch response mismatch\");\n }\n\n return preparedPages.map((preparedPage) => {\n const page = pagesByLaneId.get(preparedPage.request.laneId);\n if (!page) {\n throw new Error(\"Container parent lane batch response mismatch\");\n }\n return {\n expectedContainerStates,\n expectedHydrationTombstones,\n lane: preparedPage.lane,\n response: page,\n syncLane: preparedPage.syncLane,\n };\n });\n}\n"]}
@@ -0,0 +1,25 @@
1
+ import { ContainerSyncWatermarkRepository, } from "../../../../data/persistence/container-contents/containerSyncWatermarkPersistence.js";
2
+ const lane = (containerId) => ({
3
+ kind: "container_restoration",
4
+ containerId,
5
+ });
6
+ /** The same access loss can appear on later folder publications. Read all
7
+ * receipts for this page together; ordinary pages need no SQLite lookup. */
8
+ export async function pendingFolderRestorations(input) {
9
+ const candidates = input.items.filter((item) => !!item.restorationId &&
10
+ !input.isPaused?.(item) &&
11
+ !!input.childIdsByParentId.get(item.id)?.size);
12
+ if (!candidates.length)
13
+ return [];
14
+ const receipts = await new ContainerSyncWatermarkRepository(input.execSql).loadWatermarkRecords(candidates.map((item) => lane(item.id)));
15
+ return candidates.filter((item, index) => receipts[index]?.watermark.id !== item.restorationId);
16
+ }
17
+ export async function recordFolderRestoration(execSql, item, settled, stillCurrent) {
18
+ if (!item || !settled)
19
+ return settled;
20
+ return new ContainerSyncWatermarkRepository(execSql).saveWatermark(lane(item.id), {
21
+ id: item.restorationId,
22
+ updatedAt: item.updatedAt,
23
+ }, { stillCurrent });
24
+ }
25
+ //# sourceMappingURL=restorationReceipt.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"restorationReceipt.js","sourceRoot":"","sources":["restorationReceipt.ts"],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ACb;QACE,EAAE,EAAE,IAAI,CAAC,aAAa;QACtB,SAAS,EAAE,IAAI,CAAC,SAAS;KAC1B,EACD,EAAE,YAAY,EAAE,CACjB,CAAC;AACJ,CAAC","sourcesContent":["import {\n type ContainerSyncWatermarkLane,\n ContainerSyncWatermarkRepository,\n} from \"../../../../data/persistence/container-contents/containerSyncWatermarkPersistence\";\nimport type { ExecSql } from \"../../../../data/sqlite/sqlSchema\";\nimport type {\n ContainerChildIndex,\n ListedRemoteContainerPageItem,\n} from \"../types\";\n\ntype RestoredContainer = ListedRemoteContainerPageItem & {\n restorationId: string;\n};\n\nconst lane = (containerId: string): ContainerSyncWatermarkLane => ({\n kind: \"container_restoration\",\n containerId,\n});\n\n/** The same access loss can appear on later folder publications. Read all\n * receipts for this page together; ordinary pages need no SQLite lookup. */\nexport async function pendingFolderRestorations(input: {\n childIdsByParentId: ContainerChildIndex;\n execSql: ExecSql;\n isPaused?: ((item: ListedRemoteContainerPageItem) => boolean) | undefined;\n items: ReadonlyArray<ListedRemoteContainerPageItem>;\n}): Promise<RestoredContainer[]> {\n const candidates = input.items.filter(\n (item): item is RestoredContainer =>\n !!item.restorationId &&\n !input.isPaused?.(item) &&\n !!input.childIdsByParentId.get(item.id)?.size,\n );\n if (!candidates.length) return [];\n const receipts = await new ContainerSyncWatermarkRepository(\n input.execSql,\n ).loadWatermarkRecords(candidates.map((item) => lane(item.id)));\n return candidates.filter(\n (item, index) => receipts[index]?.watermark.id !== item.restorationId,\n );\n}\n\nexport async function recordFolderRestoration(\n execSql: ExecSql,\n item: RestoredContainer | undefined,\n settled: boolean,\n stillCurrent: () => boolean,\n) {\n if (!item || !settled) return settled;\n return new ContainerSyncWatermarkRepository(execSql).saveWatermark(\n lane(item.id),\n {\n id: item.restorationId,\n updatedAt: item.updatedAt,\n },\n { stillCurrent },\n );\n}\n"]}