@tearleads/client-sdk 0.1.253 → 0.1.255

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 (108) hide show
  1. package/client/containerContents/documentDiscovery.js +0 -2
  2. package/client/containerContents/documentDiscovery.js.map +1 -1
  3. package/data/persistence/container-contents/containerContentsPersistenceTypes.d.ts +7 -10
  4. package/data/persistence/container-contents/containerContentsPersistenceTypes.js.map +1 -1
  5. package/data/persistence/container-contents/containerDeletionPersistence.js +5 -1
  6. package/data/persistence/container-contents/containerDeletionPersistence.js.map +1 -1
  7. package/data/persistence/container-contents/containerHydrationPersistence.js +1 -1
  8. package/data/persistence/container-contents/containerHydrationPersistence.js.map +1 -1
  9. package/data/persistence/container-contents/containerMetadataMutationPersistence.js +11 -4
  10. package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
  11. package/data/persistence/container-contents/containerRemovalMetadata.js +47 -0
  12. package/data/persistence/container-contents/containerRemovalMetadata.js.map +1 -0
  13. package/data/persistence/container-contents/containerRemovalPlacements.js +22 -0
  14. package/data/persistence/container-contents/containerRemovalPlacements.js.map +1 -0
  15. package/data/persistence/container-contents/containerSyncWatermarkPersistence.d.ts +2 -1
  16. package/data/persistence/container-contents/containerSyncWatermarkPersistence.js +10 -2
  17. package/data/persistence/container-contents/containerSyncWatermarkPersistence.js.map +1 -1
  18. package/data/persistence/documents/documentDiscoveryCandidate.d.ts +1 -0
  19. package/data/persistence/documents/documentDiscoveryCandidate.js +3 -1
  20. package/data/persistence/documents/documentDiscoveryCandidate.js.map +1 -1
  21. package/data/persistence/documents/documentDiscoveryEvidencePersistence.d.ts +1 -1
  22. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js +16 -4
  23. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js.map +1 -1
  24. package/data/sqlite/migrations/localSchemaMigrations.js +11 -0
  25. package/data/sqlite/migrations/localSchemaMigrations.js.map +1 -1
  26. package/data/sqlite/schema/containerSyncWatermarkSchema.js +6 -3
  27. package/data/sqlite/schema/containerSyncWatermarkSchema.js.map +1 -1
  28. package/data/sqlite/schema/index.d.ts +17 -0
  29. package/index.d.ts +1 -1
  30. package/index.js +1 -1
  31. package/index.js.map +1 -1
  32. package/internal/api-client/ApiClient.d.ts +1 -0
  33. package/internal/api-client/client/apiClientContext.d.ts +1 -0
  34. package/internal/api-client/client/containerReads.d.ts +1 -0
  35. package/internal/api-client/requestInternals.js +14 -6
  36. package/internal/api-client/requestInternals.js.map +1 -1
  37. package/internal/validators/containerSystemSlot.d.ts +6 -0
  38. package/internal/validators/containerSystemSlot.js +14 -6
  39. package/internal/validators/containerSystemSlot.js.map +1 -1
  40. package/internal/validators/operation/containerReads.d.ts +29 -2
  41. package/internal/validators/operation/containerReads.js +2 -1
  42. package/internal/validators/operation/containerReads.js.map +1 -1
  43. package/internal/validators/request/containerDocumentLanes.d.ts +7 -0
  44. package/internal/validators/request/containerDocumentLanes.js +6 -0
  45. package/internal/validators/request/containerDocumentLanes.js.map +1 -1
  46. package/internal/validators/response/container.d.ts +8 -0
  47. package/internal/validators/response/container.js +1 -0
  48. package/internal/validators/response/container.js.map +1 -1
  49. package/internal/validators/response/containerParentLanes.d.ts +2 -0
  50. package/package.json +1 -1
  51. package/stores/container-contents/operations/index.js +4 -0
  52. package/stores/container-contents/operations/index.js.map +1 -1
  53. package/workflows/container-contents/container-state/create/createChild.d.ts +30 -0
  54. package/workflows/container-contents/container-state/move/moveIntentSync.js +1 -0
  55. package/workflows/container-contents/container-state/move/moveIntentSync.js.map +1 -1
  56. package/workflows/container-contents/container-state/remote.js +1 -1
  57. package/workflows/container-contents/container-state/remote.js.map +1 -1
  58. package/workflows/container-contents/container-state/types.d.ts +141 -0
  59. package/workflows/container-contents/documentDiscovery/containerDocumentListing.js +52 -4
  60. package/workflows/container-contents/documentDiscovery/containerDocumentListing.js.map +1 -1
  61. package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js +4 -2
  62. package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js.map +1 -1
  63. package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js +17 -12
  64. package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js.map +1 -1
  65. package/workflows/container-contents/documentDiscovery/documentDiscoveryTypes.js.map +1 -1
  66. package/workflows/container-contents/documentDiscovery/publicationWatermark.js +15 -0
  67. package/workflows/container-contents/documentDiscovery/publicationWatermark.js.map +1 -0
  68. package/workflows/container-contents/index.d.ts +1 -0
  69. package/workflows/container-contents/index.js +1 -0
  70. package/workflows/container-contents/index.js.map +1 -1
  71. package/workflows/container-contents/remoteHydration/destination/destinationSystemTopology.js +16 -0
  72. package/workflows/container-contents/remoteHydration/destination/destinationSystemTopology.js.map +1 -0
  73. package/workflows/container-contents/remoteHydration/destination/verifiedDestination.js +8 -9
  74. package/workflows/container-contents/remoteHydration/destination/verifiedDestination.js.map +1 -1
  75. package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js +4 -4
  76. package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js.map +1 -1
  77. package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js +25 -0
  78. package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js.map +1 -0
  79. package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js +107 -0
  80. package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js.map +1 -0
  81. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js +29 -27
  82. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js.map +1 -1
  83. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js +17 -5
  84. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js.map +1 -1
  85. package/workflows/container-contents/remoteHydration/tombstoneApplication.js +67 -24
  86. package/workflows/container-contents/remoteHydration/tombstoneApplication.js.map +1 -1
  87. package/workflows/container-contents/remoteHydration/types.d.ts +3 -1
  88. package/workflows/container-contents/remoteHydration/types.js.map +1 -1
  89. package/workflows/container-contents/remoteReset/remoteResetCursorScope.js +3 -3
  90. package/workflows/container-contents/remoteReset/remoteResetCursorScope.js.map +1 -1
  91. package/workflows/container-contents/syncPause/discoveryGuard.js +6 -2
  92. package/workflows/container-contents/syncPause/discoveryGuard.js.map +1 -1
  93. package/workflows/container-contents/tombstones/documentTombstoneGate.js +6 -3
  94. package/workflows/container-contents/tombstones/documentTombstoneGate.js.map +1 -1
  95. package/workflows/registration/bootstrapRootContainer.d.ts +1 -0
  96. package/workflows/registration/bootstrapRootContainer.js +9 -0
  97. package/workflows/registration/bootstrapRootContainer.js.map +1 -1
  98. package/workflows/registration/index.d.ts +1 -1
  99. package/workflows/registration/index.js +1 -1
  100. package/workflows/registration/index.js.map +1 -1
  101. package/workflows/registration/organizationProvisioning.d.ts +3 -2
  102. package/workflows/registration/organizationProvisioning.js +1 -2
  103. package/workflows/registration/organizationProvisioning.js.map +1 -1
  104. package/workflows/registration/provisioningBootstrapBuilders.d.ts +3 -5
  105. package/workflows/registration/provisioningBootstrapBuilders.js +11 -5
  106. package/workflows/registration/provisioningBootstrapBuilders.js.map +1 -1
  107. package/workflows/registration/registerIdentity.js +2 -0
  108. package/workflows/registration/registerIdentity.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"remote.js","sourceRoot":"","sources":["remote.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAMrD,OAAO,EAAE,0BAA0B,EAAE,MAAM,kDAAkD,CAAC;AAE9F,OAAO,EAAE,oCAAoC,EAAE,MAAM,4CAA4C,CAAC;AAClG,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACjE,OAAO,EAGL,wBAAwB,EACxB,iCAAiC,EACjC,uCAAuC,GACxC,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AAE9D,OAAO,EAEL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M,cAAc,GAAG,MAAM,qBAAqB,CAAC;QACjD,WAAW,EAAE,KAAK,CAAC,OAAO,CAAC,WAAW;QACtC,cAAc,EAAE,KAAK,CAAC,OAAO,CAAC,cAAc;QAC5C,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,OAAO;KAC7B,CAAC,CAAC,IAAI,CACL,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,gCAAgC,CAAC,EAChE,CAAC,KAAc,EAAE,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC,CACxC,CAAC;IACF,IAAI,KAAK,CAAC,SAAS,EAAE,IAAI,cAAc,KAAK,IAAI,EAAE,CAAC;QACjD,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAC1B,mEAAmE,KAAK,CAAC,OAAO,CAAC,WAAW,wBAAwB,cAAc,EAAE,CACrI,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import type { ContainerAccessManifestState } from \"@tearleads/crypto\";\nimport { emptyVersionVector } from \"@tearleads/loro\";\nimport type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport type {\n ContainerMutationResponse,\n ContainerWriterProjectionResponse,\n} from \"@tearleads/validators/response\";\nimport { isContainerNotFoundFailure } from \"../../../data/containers/shared/mutationFailures\";\nimport type { ProjectionUserKeyResolver } from \"../../../data/keyingProjectionVerification\";\nimport { ProjectionDependencyUnavailableError } from \"../../../data/shared/dependencyUnavailable\";\nimport { errorMessage } from \"../../../data/shared/errorMessage\";\nimport {\n type AppliedContainerMove,\n type ContainerMutationCollaborators,\n ContainerMutationService,\n signedContainerMetadataDocumentId,\n signedContainerReferencedPrincipalHeads,\n} from \"../../containers\";\nimport { resolveDocumentCreateAuthor } from \"../../documents\";\n\nimport {\n type ContainerAlreadyCommitted,\n createRemoteContainerWithMetadataDocument,\n} from \"./create/createWithMetadata\";\nimport type {\n ContainerCreateIntentSyncState,\n ContainerWorkflowRuntime,\n CreatedRemoteContainerState,\n RemoteContainer,\n SharedRemoteContainerState,\n} from \"./types\";\n\n/**\n * Resolve the author and the runtime snapshot's container mutation\n * collaborators, with a service bound to them, logging a skip for the given\n * operation when the writer context is unavailable.\n */\nfunction resolveContainerWriterContext(\n runtime: ContainerWorkflowRuntime,\n operation:\n | \"container create\"\n | \"container group share\"\n | \"container move\"\n | \"container share\",\n) {\n const author = resolveDocumentCreateAuthor(runtime);\n const secretKey = runtime.crypto.encapsulationKeyPair?.secretKey;\n if (!author || !secretKey) {\n runtime.util.log(\n `Container contents: skipped ${operation} because the writer context is unavailable.`,\n );\n return null;\n }\n\n const collaborators: ContainerMutationCollaborators<\n ContainerWorkflowRuntime[\"apiClient\"]\n > = {\n api: runtime.apiClient,\n encapsulationSecretKey: secretKey,\n execSql: runtime.infra.execSql,\n reportSecurityIncident: runtime.util.reportSecurityIncident,\n resolveTrustedUserIdentity: runtime.resolveTrustedUserIdentity,\n };\n return {\n author,\n collaborators,\n containers: new ContainerMutationService(collaborators),\n };\n}\n\nexport async function createRemoteContainer(input: {\n systemSlot?: ContainerSystemSlot | null | undefined;\n containerId: string;\n metadataSeedUpdate?: Parameters<\n typeof createRemoteContainerWithMetadataDocument\n >[0][\"metadataSeedUpdate\"];\n parentContainerId: string;\n parentProjection?: ContainerWriterProjectionResponse | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<CreatedRemoteContainerState | ContainerAlreadyCommitted | null> {\n return createRemoteContainerWithMetadataDocument(input);\n}\n\n/** An acknowledged mutation: the server's commitments and the signed plan. */\ninterface AcknowledgedContainerMutation {\n readonly plan: { readonly state: ContainerAccessManifestState };\n readonly response: ContainerMutationResponse;\n}\n\nfunction sharedRemoteContainerStateFromMutation(\n previousProjection: ContainerWriterProjectionResponse,\n { plan, response }: AcknowledgedContainerMutation,\n): SharedRemoteContainerState {\n if (\n response.containerId !== previousProjection.containerId ||\n response.organizationId !== previousProjection.organizationId\n )\n throw new Error(\"Container mutation response projection mismatch\");\n return {\n accessManifestHash: response.manifestHead.manifestHash,\n accessEpoch: response.manifestHead.epoch,\n createdAt: response.createdAt,\n metadataDocumentId: signedContainerMetadataDocumentId(plan),\n referencedPrincipalHeads: signedContainerReferencedPrincipalHeads(plan),\n updatedAt: response.updatedAt,\n // The next read obtains the post-commit proof. A failed read cannot undo\n // acknowledgement of this successful mutation.\n writerProjection: null,\n };\n}\n\nexport async function shareRemoteContainer(input: {\n accessLevel: \"read\" | \"write\" | \"admin\";\n containerId: string;\n previousProjection: ContainerWriterProjectionResponse;\n recipientUserId: string;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<SharedRemoteContainerState | null> {\n const writer = resolveContainerWriterContext(\n input.runtime,\n \"container share\",\n );\n if (!writer) {\n return null;\n }\n const shared = await writer.containers.shareRemoteContainer({\n accessLevel: input.accessLevel,\n author: writer.author,\n committedReadModel: input.runtime.organizations.committedReadModel({\n domainScope: input.runtime.state.domainScope,\n execSql: input.runtime.infra.execSql,\n organizationId: writer.author.organizationId,\n userId: writer.author.signerUserId,\n }),\n containerId: input.containerId,\n previousProjection: input.previousProjection,\n recipientUserId: input.recipientUserId,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n stillCurrent: input.stillCurrent,\n warmReferencedPrincipalPolicies:\n input.runtime.principalPolicies.createWarmer(),\n });\n if (!shared) {\n return null;\n }\n\n return sharedRemoteContainerStateFromMutation(\n input.previousProjection,\n shared,\n );\n}\n\nexport async function shareRemoteContainerWithGroup(input: {\n accessLevel: \"read\" | \"write\" | \"admin\";\n containerId: string;\n expectedGroupName?: string | undefined;\n knownContainerKeks?: ReadonlyMap<string, Uint8Array> | undefined;\n previousProjection: ContainerWriterProjectionResponse;\n recipientGroupId: string;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<SharedRemoteContainerState | null> {\n const writer = resolveContainerWriterContext(\n input.runtime,\n \"container group share\",\n );\n if (!writer) {\n return null;\n }\n const { author, collaborators } = writer;\n\n const shareWithGroup = input.runtime.organizations.createCurrentGroupShare(\n input.runtime,\n );\n if (!shareWithGroup)\n throw new ProjectionDependencyUnavailableError(\n \"Group sharing requires private paged recovery\",\n );\n const shared = await shareWithGroup({\n reportSecurityIncident: collaborators.reportSecurityIncident,\n accessLevel: input.accessLevel,\n apiClient: collaborators.api,\n author,\n containerId: input.containerId,\n execSql: collaborators.execSql,\n expectedGroupName: input.expectedGroupName ?? null,\n readEncryptedName: (bundle) =>\n input.runtime.organizations.groupMetadataNameReader(\n input.runtime,\n author.organizationId,\n input.stillCurrent,\n )(bundle),\n knownContainerKeks: input.knownContainerKeks,\n previousProjection: input.previousProjection,\n recipientGroupId: input.recipientGroupId,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n resolveTrustedUserIdentity: collaborators.resolveTrustedUserIdentity,\n signingKeyPair: input.runtime.crypto.signingKeyPair,\n stillCurrent: input.stillCurrent,\n targetSecretKey: collaborators.encapsulationSecretKey,\n warmReferencedPrincipalPolicies:\n input.runtime.principalPolicies.createWarmer(),\n });\n if (!shared) {\n return null;\n }\n\n return sharedRemoteContainerStateFromMutation(\n input.previousProjection,\n shared,\n );\n}\n\n/**\n * Replays a queued move. `applied` carries the verified head when the server\n * already holds the container under the destination, so nothing is posted.\n */\nexport async function moveRemoteContainer(input: {\n containerId: string;\n onAccepted?: (() => void) | undefined;\n parentContainerId: string;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<\n { applied: AppliedContainerMove } | { moved: RemoteContainer } | null\n> {\n const writer = resolveContainerWriterContext(input.runtime, \"container move\");\n if (!writer) {\n return null;\n }\n const replayed = await writer.containers.replayRemoteContainerMove({\n author: writer.author,\n containerId: input.containerId,\n destinationParentContainerId: input.parentContainerId,\n onAccepted: input.onAccepted,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n stillCurrent: input.stillCurrent,\n warmReferencedPrincipalPolicies:\n input.runtime.principalPolicies.createWarmer(),\n });\n if (!replayed || \"applied\" in replayed) {\n return replayed;\n }\n\n const { plan, response } = replayed;\n return {\n moved: {\n createdAt: response.createdAt,\n effectiveAccessLevel: \"admin\",\n id: response.containerId,\n organizationId: response.organizationId,\n parentId: response.parentId,\n metadataDocumentId: signedContainerMetadataDocumentId(plan),\n metadataAccessEpoch: response.manifestHead.epoch,\n metadataAccessStateHash: response.manifestHead.manifestHash,\n metadataReferencedPrincipals:\n signedContainerReferencedPrincipalHeads(plan),\n // A move says nothing about the metadata document's version.\n metadataVersionVector: emptyVersionVector(),\n updatedAt: response.updatedAt,\n },\n };\n}\n\nexport async function deleteRemoteContainer(input: {\n containerId: string;\n organizationId: string;\n requiredAncestorId?: string | undefined;\n runtime: ContainerWorkflowRuntime;\n}): Promise<{ deletedAt: string } | null> {\n const deleteResult = await input.runtime.apiClient.deleteContainerResult(\n input.containerId,\n {\n expectedPaymentRequiredOrganizationId: input.organizationId,\n reportErrors: false,\n requiredAncestorId: input.requiredAncestorId,\n },\n );\n if (!deleteResult.ok && !isContainerNotFoundFailure(deleteResult)) {\n deleteResult.report();\n return null;\n }\n\n if (!deleteResult.ok) {\n // A 404 proves this immutable id is permanently absent, but it does not\n // carry the server deletion time. A maximal fence prevents an older\n // in-flight listing page from resurrecting it.\n return { deletedAt: \"9999-12-31T23:59:59.999Z\" };\n }\n\n return { deletedAt: deleteResult.data.deletedAt };\n}\n\n/** Best-effort delete of a remote container whose local folder is gone. */\nexport async function discardOrphanedRemoteContainer(input: {\n created: CreatedRemoteContainerState;\n isCurrent: () => boolean;\n state: Pick<ContainerCreateIntentSyncState, \"runtime\">;\n}): Promise<void> {\n if (!input.isCurrent()) {\n return;\n }\n const discardFailure = await deleteRemoteContainer({\n containerId: input.created.containerId,\n organizationId: input.created.organizationId,\n runtime: input.state.runtime,\n }).then(\n (deleted) => (deleted ? null : \"the server rejected the delete\"),\n (error: unknown) => errorMessage(error),\n );\n if (input.isCurrent() && discardFailure !== null) {\n input.state.runtime.util.log(\n `Container contents: failed to discard orphaned remote container ${input.created.containerId} after local delete: ${discardFailure}`,\n );\n }\n}\n"]}
1
+ {"version":3,"file":"remote.js","sourceRoot":"","sources":["remote.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAMrD,OAAO,EAAE,0BAA0B,EAAE,MAAM,kDAAkD,CAAC;AAE9F,OAAO,EAAE,oCAAoC,EAAE,MAAM,4CAA4C,CAAC;AAClG,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AACjE,OAAO,EAGL,wBAAwB,EACxB,iCAAiC,EACjC,uCAAuC,GACxC,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,2BAA2B,EAAE,MAAM,iBAAiB,CAAC;AAE9D,OAAO,EAEL,yCAAyC,GAC1C,MAAM,6BAA6B,CAAC;AASrC;;;;GAIG;AACH,SAAS,6BAA6B,CACpC,OAAiC,EACjC,SAIqB;IAErB,MAAM,MAAM,GAAG,2BAA2B,CAAC,OAAO,CAAC,CAAC;IACpD,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,oBAAoB,EAAE,SAAS,CAAC;IACjE,IAAI,CAAC,MAAM,IAAI,CAAC,SAAS,EAAE,CAAC;QAC1B,OAAO,CAAC,IAAI,CAAC,GAAG,CACd,+BAA+B,SAAS,6CAA6C,CACtF,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,aAAa,GAEf;QACF,GAAG,EAAE,OAAO,CAAC,SAAS;QACtB,sBAAsB,EAAE,SAAS;QACjC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO;QAC9B,sBAAsB,EAAE,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IAAI,CAAC,GAAG,CAC1B,mEAAmE,KAAK,CAAC,OAAO,CAAC,WAAW,wBAAwB,cAAc,EAAE,CACrI,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import type { ContainerAccessManifestState } from \"@tearleads/crypto\";\nimport { emptyVersionVector } from \"@tearleads/loro\";\nimport type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport type {\n ContainerMutationResponse,\n ContainerWriterProjectionResponse,\n} from \"@tearleads/validators/response\";\nimport { isContainerNotFoundFailure } from \"../../../data/containers/shared/mutationFailures\";\nimport type { ProjectionUserKeyResolver } from \"../../../data/keyingProjectionVerification\";\nimport { ProjectionDependencyUnavailableError } from \"../../../data/shared/dependencyUnavailable\";\nimport { errorMessage } from \"../../../data/shared/errorMessage\";\nimport {\n type AppliedContainerMove,\n type ContainerMutationCollaborators,\n ContainerMutationService,\n signedContainerMetadataDocumentId,\n signedContainerReferencedPrincipalHeads,\n} from \"../../containers\";\nimport { resolveDocumentCreateAuthor } from \"../../documents\";\n\nimport {\n type ContainerAlreadyCommitted,\n createRemoteContainerWithMetadataDocument,\n} from \"./create/createWithMetadata\";\nimport type {\n ContainerCreateIntentSyncState,\n ContainerWorkflowRuntime,\n CreatedRemoteContainerState,\n RemoteContainer,\n SharedRemoteContainerState,\n} from \"./types\";\n\n/**\n * Resolve the author and the runtime snapshot's container mutation\n * collaborators, with a service bound to them, logging a skip for the given\n * operation when the writer context is unavailable.\n */\nfunction resolveContainerWriterContext(\n runtime: ContainerWorkflowRuntime,\n operation:\n | \"container create\"\n | \"container group share\"\n | \"container move\"\n | \"container share\",\n) {\n const author = resolveDocumentCreateAuthor(runtime);\n const secretKey = runtime.crypto.encapsulationKeyPair?.secretKey;\n if (!author || !secretKey) {\n runtime.util.log(\n `Container contents: skipped ${operation} because the writer context is unavailable.`,\n );\n return null;\n }\n\n const collaborators: ContainerMutationCollaborators<\n ContainerWorkflowRuntime[\"apiClient\"]\n > = {\n api: runtime.apiClient,\n encapsulationSecretKey: secretKey,\n execSql: runtime.infra.execSql,\n reportSecurityIncident: runtime.util.reportSecurityIncident,\n resolveTrustedUserIdentity: runtime.resolveTrustedUserIdentity,\n };\n return {\n author,\n collaborators,\n containers: new ContainerMutationService(collaborators),\n };\n}\n\nexport async function createRemoteContainer(input: {\n systemSlot?: ContainerSystemSlot | null | undefined;\n containerId: string;\n metadataSeedUpdate?: Parameters<\n typeof createRemoteContainerWithMetadataDocument\n >[0][\"metadataSeedUpdate\"];\n parentContainerId: string;\n parentProjection?: ContainerWriterProjectionResponse | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<CreatedRemoteContainerState | ContainerAlreadyCommitted | null> {\n return createRemoteContainerWithMetadataDocument(input);\n}\n\n/** An acknowledged mutation: the server's commitments and the signed plan. */\ninterface AcknowledgedContainerMutation {\n readonly plan: { readonly state: ContainerAccessManifestState };\n readonly response: ContainerMutationResponse;\n}\n\nfunction sharedRemoteContainerStateFromMutation(\n previousProjection: ContainerWriterProjectionResponse,\n { plan, response }: AcknowledgedContainerMutation,\n): SharedRemoteContainerState {\n if (\n response.containerId !== previousProjection.containerId ||\n response.organizationId !== previousProjection.organizationId\n )\n throw new Error(\"Container mutation response projection mismatch\");\n return {\n accessManifestHash: response.manifestHead.manifestHash,\n accessEpoch: response.manifestHead.epoch,\n createdAt: response.createdAt,\n metadataDocumentId: signedContainerMetadataDocumentId(plan),\n referencedPrincipalHeads: signedContainerReferencedPrincipalHeads(plan),\n updatedAt: response.updatedAt,\n // The next read obtains the post-commit proof. A failed read cannot undo\n // acknowledgement of this successful mutation.\n writerProjection: null,\n };\n}\n\nexport async function shareRemoteContainer(input: {\n accessLevel: \"read\" | \"write\" | \"admin\";\n containerId: string;\n previousProjection: ContainerWriterProjectionResponse;\n recipientUserId: string;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<SharedRemoteContainerState | null> {\n const writer = resolveContainerWriterContext(\n input.runtime,\n \"container share\",\n );\n if (!writer) {\n return null;\n }\n const shared = await writer.containers.shareRemoteContainer({\n accessLevel: input.accessLevel,\n author: writer.author,\n committedReadModel: input.runtime.organizations.committedReadModel({\n domainScope: input.runtime.state.domainScope,\n execSql: input.runtime.infra.execSql,\n organizationId: writer.author.organizationId,\n userId: writer.author.signerUserId,\n }),\n containerId: input.containerId,\n previousProjection: input.previousProjection,\n recipientUserId: input.recipientUserId,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n stillCurrent: input.stillCurrent,\n warmReferencedPrincipalPolicies:\n input.runtime.principalPolicies.createWarmer(),\n });\n if (!shared) {\n return null;\n }\n\n return sharedRemoteContainerStateFromMutation(\n input.previousProjection,\n shared,\n );\n}\n\nexport async function shareRemoteContainerWithGroup(input: {\n accessLevel: \"read\" | \"write\" | \"admin\";\n containerId: string;\n expectedGroupName?: string | undefined;\n knownContainerKeks?: ReadonlyMap<string, Uint8Array> | undefined;\n previousProjection: ContainerWriterProjectionResponse;\n recipientGroupId: string;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<SharedRemoteContainerState | null> {\n const writer = resolveContainerWriterContext(\n input.runtime,\n \"container group share\",\n );\n if (!writer) {\n return null;\n }\n const { author, collaborators } = writer;\n\n const shareWithGroup = input.runtime.organizations.createCurrentGroupShare(\n input.runtime,\n );\n if (!shareWithGroup)\n throw new ProjectionDependencyUnavailableError(\n \"Group sharing requires private paged recovery\",\n );\n const shared = await shareWithGroup({\n reportSecurityIncident: collaborators.reportSecurityIncident,\n accessLevel: input.accessLevel,\n apiClient: collaborators.api,\n author,\n containerId: input.containerId,\n execSql: collaborators.execSql,\n expectedGroupName: input.expectedGroupName ?? null,\n readEncryptedName: (bundle) =>\n input.runtime.organizations.groupMetadataNameReader(\n input.runtime,\n author.organizationId,\n input.stillCurrent,\n )(bundle),\n knownContainerKeks: input.knownContainerKeks,\n previousProjection: input.previousProjection,\n recipientGroupId: input.recipientGroupId,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n resolveTrustedUserIdentity: collaborators.resolveTrustedUserIdentity,\n signingKeyPair: input.runtime.crypto.signingKeyPair,\n stillCurrent: input.stillCurrent,\n targetSecretKey: collaborators.encapsulationSecretKey,\n warmReferencedPrincipalPolicies:\n input.runtime.principalPolicies.createWarmer(),\n });\n if (!shared) {\n return null;\n }\n\n return sharedRemoteContainerStateFromMutation(\n input.previousProjection,\n shared,\n );\n}\n\n/**\n * Replays a queued move. `applied` carries the verified head when the server\n * already holds the container under the destination, so nothing is posted.\n */\nexport async function moveRemoteContainer(input: {\n containerId: string;\n organizationId: string;\n onAccepted?: (() => void) | undefined;\n parentContainerId: string;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<\n { applied: AppliedContainerMove } | { moved: RemoteContainer } | null\n> {\n const writer = resolveContainerWriterContext(input.runtime, \"container move\");\n if (!writer) {\n return null;\n }\n const replayed = await writer.containers.replayRemoteContainerMove({\n author: { ...writer.author, organizationId: input.organizationId },\n containerId: input.containerId,\n destinationParentContainerId: input.parentContainerId,\n onAccepted: input.onAccepted,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n stillCurrent: input.stillCurrent,\n warmReferencedPrincipalPolicies:\n input.runtime.principalPolicies.createWarmer(),\n });\n if (!replayed || \"applied\" in replayed) {\n return replayed;\n }\n\n const { plan, response } = replayed;\n return {\n moved: {\n createdAt: response.createdAt,\n effectiveAccessLevel: \"admin\",\n id: response.containerId,\n organizationId: response.organizationId,\n parentId: response.parentId,\n metadataDocumentId: signedContainerMetadataDocumentId(plan),\n metadataAccessEpoch: response.manifestHead.epoch,\n metadataAccessStateHash: response.manifestHead.manifestHash,\n metadataReferencedPrincipals:\n signedContainerReferencedPrincipalHeads(plan),\n // A move says nothing about the metadata document's version.\n metadataVersionVector: emptyVersionVector(),\n updatedAt: response.updatedAt,\n },\n };\n}\n\nexport async function deleteRemoteContainer(input: {\n containerId: string;\n organizationId: string;\n requiredAncestorId?: string | undefined;\n runtime: ContainerWorkflowRuntime;\n}): Promise<{ deletedAt: string } | null> {\n const deleteResult = await input.runtime.apiClient.deleteContainerResult(\n input.containerId,\n {\n expectedPaymentRequiredOrganizationId: input.organizationId,\n reportErrors: false,\n requiredAncestorId: input.requiredAncestorId,\n },\n );\n if (!deleteResult.ok && !isContainerNotFoundFailure(deleteResult)) {\n deleteResult.report();\n return null;\n }\n\n if (!deleteResult.ok) {\n // A 404 proves this immutable id is permanently absent, but it does not\n // carry the server deletion time. A maximal fence prevents an older\n // in-flight listing page from resurrecting it.\n return { deletedAt: \"9999-12-31T23:59:59.999Z\" };\n }\n\n return { deletedAt: deleteResult.data.deletedAt };\n}\n\n/** Best-effort delete of a remote container whose local folder is gone. */\nexport async function discardOrphanedRemoteContainer(input: {\n created: CreatedRemoteContainerState;\n isCurrent: () => boolean;\n state: Pick<ContainerCreateIntentSyncState, \"runtime\">;\n}): Promise<void> {\n if (!input.isCurrent()) {\n return;\n }\n const discardFailure = await deleteRemoteContainer({\n containerId: input.created.containerId,\n organizationId: input.created.organizationId,\n runtime: input.state.runtime,\n }).then(\n (deleted) => (deleted ? null : \"the server rejected the delete\"),\n (error: unknown) => errorMessage(error),\n );\n if (input.isCurrent() && discardFailure !== null) {\n input.state.runtime.util.log(\n `Container contents: failed to discard orphaned remote container ${input.created.containerId} after local delete: ${discardFailure}`,\n );\n }\n}\n"]}
@@ -0,0 +1,141 @@
1
+ import type { ContainerSystemSlot } from "../../../internal/validators/containerSystemSlot.js";
2
+ import type { ContainerDeleteResponse, ContainerWriterProjectionResponse, ReferencedPrincipalStateResponse } from "../../../internal/validators/response/index.js";
3
+ import type { DocumentCreateApi } from "../../../data/documents/shared/types.js";
4
+ import type { ProjectionUserKeyResolver } from "../../../data/keyingProjectionVerification/index.js";
5
+ import type { DocumentRecord } from "../../../data/persistence/documents/records/index.js";
6
+ import type { ContainerMutationServiceApi } from "../../containers/index.js";
7
+ import type { shareRemoteContainerWithGroup } from "../../organizations/groupShare/index.js";
8
+ import type { CurrentPrincipalPolicyReader } from "../../principals/currentPrincipalPolicyReader.js";
9
+ import type { PrincipalPolicyHistoryReader } from "../../principals/principalPolicyHistoryReader.js";
10
+ import type { ContainerContentsPersistence, ContainerCreateIntentRecord, ContainerMoveIntentRecord } from "../containerPersistence.js";
11
+ import type { createInitializedContainerMetadataDocument } from "../metadata/containerMetadataDocument.js";
12
+ import type { ContainerState, RemoteContainer as HydratedRemoteContainer, RemoteContainerHydrationHost } from "../remoteHydration/types.js";
13
+ import type { ContainerContentsWorkflowRuntime } from "../runtime.js";
14
+ export type ContainerWorkflowApi = ContainerMutationServiceApi & Parameters<typeof shareRemoteContainerWithGroup>[0]["apiClient"] & DocumentCreateApi & Partial<PrincipalPolicyHistoryReader> & CurrentPrincipalPolicyReader & {
15
+ deleteContainerResult(containerId: string, options?: {
16
+ expectedPaymentRequiredOrganizationId?: string;
17
+ reportErrors?: boolean;
18
+ requiredAncestorId?: string | undefined;
19
+ }): Promise<{
20
+ data: ContainerDeleteResponse;
21
+ ok: true;
22
+ } | {
23
+ ok: false;
24
+ report: () => void;
25
+ status: number | null;
26
+ }>;
27
+ primeContainerWriterProjection(containerId: string, projection: ContainerWriterProjectionResponse): void;
28
+ };
29
+ export interface ContainerWorkflowRuntime extends Pick<ContainerContentsWorkflowRuntime, "auth" | "crypto" | "infra" | "organizations" | "principalPolicies" | "resolveTrustedUserIdentity" | "state" | "util"> {
30
+ readonly apiClient: ContainerWorkflowApi;
31
+ }
32
+ export interface CreatedRemoteContainerState {
33
+ accessManifestHash: string;
34
+ /**
35
+ * The metadata seed the create committed, settled by the caller instead of
36
+ * a first metadata sync. Absent when the create carried no seed or fell back
37
+ * to a plain create.
38
+ */
39
+ metadataSeed?: {
40
+ readonly commitLsn: string | null;
41
+ readonly updateId: string;
42
+ } | undefined;
43
+ /** The slot this client signed into the create; never the server's echo. */
44
+ systemSlot: ContainerSystemSlot | null;
45
+ containerId: string;
46
+ createdAt: string;
47
+ metadataDocumentId: string;
48
+ organizationId: string;
49
+ /** A created container is a child; the acknowledgement echoed this parent. */
50
+ parentId: string;
51
+ persistedMetadataState: Pick<DocumentRecord, "documentId" | "contentKeyBundle" | "documentKekTargets" | "documentManifestBundle">;
52
+ updatedAt: string;
53
+ }
54
+ export interface SharedRemoteContainerState {
55
+ accessEpoch: number;
56
+ accessManifestHash: string;
57
+ createdAt: string;
58
+ metadataDocumentId: string;
59
+ referencedPrincipalHeads: ReferencedPrincipalStateResponse[];
60
+ updatedAt: string;
61
+ writerProjection: ContainerWriterProjectionResponse | null;
62
+ }
63
+ export interface SharedContainerState {
64
+ container: ContainerState["container"];
65
+ record: ContainerState["record"];
66
+ }
67
+ export type SharedContainerStateResult = {
68
+ status: "missing";
69
+ } | {
70
+ status: "confirmed";
71
+ } | (SharedContainerState & {
72
+ status: "identity-superseded" | "persisted";
73
+ });
74
+ export type RemoteContainer = HydratedRemoteContainer;
75
+ export type ContainerMetadataDocumentState = Awaited<ReturnType<typeof createInitializedContainerMetadataDocument>>["doc"];
76
+ export interface CreatedChildContainerState {
77
+ containerState: ContainerState;
78
+ initialUpdate: Uint8Array;
79
+ shouldRequestSync: boolean;
80
+ }
81
+ export interface ContainerIntentSyncState {
82
+ containersById: ReadonlyMap<string, ContainerState>;
83
+ /**
84
+ * Metadata update ids this device sent, so their own update echoes are
85
+ * classified as self-authored (shared with the metadata sync).
86
+ */
87
+ locallyAcceptedMetadataUpdateIds?: Set<string> | undefined;
88
+ /** Metadata document ids of folder creates in flight (shared with hints). */
89
+ plannedMetadataDocumentIds?: Set<string> | undefined;
90
+ persistence: ContainerContentsPersistence;
91
+ resolveProjectionUserKey: ProjectionUserKeyResolver;
92
+ runtime: ContainerWorkflowRuntime;
93
+ /**
94
+ * Verifies that a listed container is this device's pending create before it
95
+ * is adopted and returns where it sits; `assertContainerCreateAdoptable`
96
+ * unless a test replaces it.
97
+ */
98
+ verifyCreateAdoption?: (input: {
99
+ readonly containerId: string;
100
+ readonly expectedOrganizationId: string;
101
+ }) => Promise<AdoptedContainerPlacement>;
102
+ }
103
+ /** Where a verified listed create committed, and where its container is now. */
104
+ export interface AdoptedContainerPlacement {
105
+ /** The parent its signed epoch-1 `container.create` named. */
106
+ readonly createdParentId: string;
107
+ /** The verified head's parent; another writer may have moved it since. */
108
+ readonly currentParentId: string;
109
+ }
110
+ export type ContainerCreateIntentSyncState = ContainerIntentSyncState;
111
+ export type ContainerCreateIntentSyncHost = Pick<RemoteContainerHydrationHost, "persistContainerState">;
112
+ export interface ContainerCreateIntentSyncInput {
113
+ host: ContainerCreateIntentSyncHost;
114
+ /** A parent whose own create has not settled takes no child create. */
115
+ isCreatePending: (containerId: string) => boolean;
116
+ isCurrent: () => boolean;
117
+ isRemoteSyncBlocked: (organizationId: string) => boolean;
118
+ intent: ContainerCreateIntentRecord;
119
+ requestRemoteReconciliation: (parentContainerId: string | null) => void;
120
+ state: ContainerCreateIntentSyncState;
121
+ }
122
+ export type ContainerMoveIntentSyncState = ContainerIntentSyncState;
123
+ export type ContainerMoveIntentSyncHost = Pick<RemoteContainerHydrationHost, "persistContainerState" | "updateSnapshot">;
124
+ /** Later passes for moves the server accepted but no pass settled. */
125
+ export interface AcceptedMoveRetry {
126
+ /** The server holds the move: schedule a replay to settle it. */
127
+ arm: (intentId: string) => void;
128
+ /** A replay failed; continue an accepted move's retries, if it has some. */
129
+ resume: (intentId: string) => void;
130
+ }
131
+ export interface ContainerMoveIntentSyncInput {
132
+ host: ContainerMoveIntentSyncHost;
133
+ isCurrent: () => boolean;
134
+ /** A folder whose create has not settled takes no structural writes. */
135
+ isCreatePending: (containerId: string) => boolean;
136
+ isRemoteSyncBlocked: (organizationId: string) => boolean;
137
+ intent: ContainerMoveIntentRecord;
138
+ requestRemoteReconciliation: (parentContainerId: string | null) => void;
139
+ acceptedMoveRetry: AcceptedMoveRetry;
140
+ state: ContainerMoveIntentSyncState;
141
+ }
@@ -1,4 +1,5 @@
1
1
  import { DEFAULT_CONTAINER_DOCUMENT_LANE_PAGE_LIMIT, MAX_CONTAINER_DOCUMENT_LANE_PAGE_TOTAL, MAX_CONTAINER_DOCUMENT_LANES, } from "../../../internal/validators/request/index.js";
2
+ import { changesDiscoveryOrder, isPublicationWatermark, } from "./publicationWatermark.js";
2
3
  /**
3
4
  * Rows each lane may return so a full batch stays within the single-page
4
5
  * maximum; a lone lane keeps the ordinary page size.
@@ -28,6 +29,45 @@ export async function listContainerDocumentLanesFromApi(apiClient, lanes, report
28
29
  return result && "page" in result ? result.page : null;
29
30
  });
30
31
  }
32
+ function removedDocumentPlacements(listed) {
33
+ const removals = new Map();
34
+ for (const lane of listed.values()) {
35
+ if (!lane ||
36
+ lane === "unavailable" ||
37
+ !isPublicationWatermark(lane.nextWatermark))
38
+ continue;
39
+ for (const row of lane.tombstones) {
40
+ const containers = removals.get(row.documentId) ?? new Set();
41
+ containers.add(row.containerId);
42
+ removals.set(row.documentId, containers);
43
+ }
44
+ }
45
+ return removals;
46
+ }
47
+ // Publication-ordered lanes exclude historical unlinks for live placements.
48
+ // Check the whole fetched batch before applying a lane: a linked document
49
+ // carries its placements in other containers as well as the queried one.
50
+ function discardConflictingDocumentPages(listed) {
51
+ const removals = removedDocumentPlacements(listed);
52
+ if (!removals.size)
53
+ return;
54
+ const conflicted = new Set();
55
+ for (const [id, lane] of listed) {
56
+ if (!lane || lane === "unavailable")
57
+ continue;
58
+ for (const item of lane.items) {
59
+ const removed = removals.get(item.id);
60
+ if (!removed)
61
+ continue;
62
+ for (const linked of item.linkedContainerIds.filter((container) => removed.has(container))) {
63
+ conflicted.add(id);
64
+ conflicted.add(linked);
65
+ }
66
+ }
67
+ }
68
+ for (const id of conflicted)
69
+ listed.set(id, "unavailable");
70
+ }
31
71
  /**
32
72
  * Pages every lane to exhaustion, batching first pages and continuations
33
73
  * alike. Each container keeps its own watermark, so one lane's failure or
@@ -45,6 +85,7 @@ export async function listAllContainerDocumentLanes(input) {
45
85
  pending.push({
46
86
  containerId,
47
87
  isFullListing: watermark === null,
88
+ isContinuation: false,
48
89
  items: [],
49
90
  tombstones: [],
50
91
  watermark,
@@ -53,16 +94,21 @@ export async function listAllContainerDocumentLanes(input) {
53
94
  while (pending.length > 0) {
54
95
  const batch = pending.splice(0, MAX_CONTAINER_DOCUMENT_LANES);
55
96
  const pages = await input.listContainerDocumentLanes(batch.map(({ containerId, watermark }) => ({ containerId, watermark })));
56
- batch.forEach((lane, index) => {
57
- if (!pages) {
97
+ if (!pages) {
98
+ for (const lane of batch)
58
99
  listed.set(lane.containerId, "unavailable");
59
- return;
60
- }
100
+ continue;
101
+ }
102
+ batch.forEach((lane, index) => {
61
103
  const page = pages[index] ?? null;
62
104
  if (!page) {
63
105
  listed.set(lane.containerId, null);
64
106
  return;
65
107
  }
108
+ if (changesDiscoveryOrder(lane.watermark, page.nextWatermark, lane.isContinuation)) {
109
+ listed.set(lane.containerId, "unavailable");
110
+ return;
111
+ }
66
112
  lane.items.push(...page.items);
67
113
  lane.tombstones.push(...page.tombstones);
68
114
  lane.watermark = page.nextWatermark;
@@ -75,6 +121,7 @@ export async function listAllContainerDocumentLanes(input) {
75
121
  });
76
122
  }
77
123
  else if (lane.watermark) {
124
+ lane.isContinuation = true;
78
125
  pending.push(lane);
79
126
  }
80
127
  else {
@@ -82,6 +129,7 @@ export async function listAllContainerDocumentLanes(input) {
82
129
  }
83
130
  });
84
131
  }
132
+ discardConflictingDocumentPages(listed);
85
133
  return listed;
86
134
  }
87
135
  /** Lists the authoritative current ids by deliberately starting at no watermark. */
@@ -1 +1 @@
1
- {"version":3,"file":"containerDocumentListing.js","sourceRoot":"","sources":["containerDocumentListing.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0CAA0C,EAC1C,sCAAsC,EACtC,4BA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G,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IACzB,OAAO,MAAM,IAAI,MAAM,KAAK,aAAa;QACvC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;QAClE,CAAC,CAAC,IAAI,CAAC;AACX,CAAC;AAED,+EAA+E;AAC/E,MAAM,CAAC,KAAK,UAAU,oCAAoC,CAAC,KAM1D;IACC,MAAM,KAAK,GACT,EAAE,CAAC;IACL,KAAK,MAAM,WAAW,IAAI,IAAI,GAAG,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;QACtD,KAAK,CAAC,IAAI,CAAC;YACT,WAAW;YACX,SAAS,EAAE,MAAM,KAAK,CAAC,8BAA8B,CAAC,WAAW,CAAC;SACnE,CAAC,CAAC;IACL,CAAC;IACD,OAAO,6BAA6B,CAAC;QACnC,KAAK;QACL,0BAA0B,EAAE,KAAK,CAAC,0BAA0B;KAC7D,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n DEFAULT_CONTAINER_DOCUMENT_LANE_PAGE_LIMIT,\n MAX_CONTAINER_DOCUMENT_LANE_PAGE_TOTAL,\n MAX_CONTAINER_DOCUMENT_LANES,\n} from \"@tearleads/validators/request\";\nimport type { SyncWatermark } from \"@tearleads/validators/response\";\nimport type {\n ContainerDocumentDiscoveryApi,\n ContainerDocumentLaneLister,\n ContainerDocumentLaneListing,\n ContainerDocumentTombstone,\n ListedContainerDocument,\n} from \"./documentDiscoveryTypes\";\n\ntype ContainerDocumentLaneApi = Pick<\n ContainerDocumentDiscoveryApi,\n \"listContainerDocumentLanes\"\n>;\n\n/**\n * Rows each lane may return so a full batch stays within the single-page\n * maximum; a lone lane keeps the ordinary page size.\n */\nfunction laneLimit(laneCount: number): number {\n return Math.max(\n 1,\n Math.min(\n DEFAULT_CONTAINER_DOCUMENT_LANE_PAGE_LIMIT,\n Math.floor(MAX_CONTAINER_DOCUMENT_LANE_PAGE_TOTAL / laneCount),\n ),\n );\n}\n\n/**\n * Lists one page per requested lane in a single batched request. A lane the\n * server rejected yields `null` for that lane only; a failed request yields\n * `null` for the batch.\n */\nexport async function listContainerDocumentLanesFromApi(\n apiClient: ContainerDocumentLaneApi,\n lanes: Parameters<ContainerDocumentLaneLister>[0],\n reportErrors = true,\n): ReturnType<ContainerDocumentLaneLister> {\n const limit = laneLimit(lanes.length);\n const requested = lanes.map((lane, index) => ({\n containerId: lane.containerId,\n laneId: `lane-${index}`,\n limit,\n watermark: lane.watermark,\n }));\n const response = await apiClient.listContainerDocumentLanes(\n { lanes: requested },\n { reportErrors },\n );\n if (!response) return null;\n const resultsByLaneId = new Map(\n response.results.map((result) => [result.laneId, result]),\n );\n return requested.map(({ laneId }) => {\n const result = resultsByLaneId.get(laneId);\n return result && \"page\" in result ? result.page : null;\n });\n}\n\ninterface PendingContainerDocumentLane {\n readonly containerId: string;\n readonly isFullListing: boolean;\n readonly items: ListedContainerDocument[];\n readonly tombstones: ContainerDocumentTombstone[];\n watermark: SyncWatermark | null;\n}\n\n/**\n * Pages every lane to exhaustion, batching first pages and continuations\n * alike. Each container keeps its own watermark, so one lane's failure or\n * continuation never changes another lane's result. A failed request marks\n * only the lanes it carried unavailable; the other batches still list.\n */\nexport async function listAllContainerDocumentLanes(input: {\n readonly lanes: ReadonlyArray<{\n readonly containerId: string;\n readonly watermark: SyncWatermark | null;\n }>;\n readonly listContainerDocumentLanes: ContainerDocumentLaneLister;\n}): Promise<ReadonlyMap<string, ContainerDocumentLaneListing>> {\n const listed = new Map<string, ContainerDocumentLaneListing>();\n const pending: PendingContainerDocumentLane[] = [];\n const queued = new Set<string>();\n for (const { containerId, watermark } of input.lanes) {\n if (queued.has(containerId)) continue;\n queued.add(containerId);\n pending.push({\n containerId,\n isFullListing: watermark === null,\n items: [],\n tombstones: [],\n watermark,\n });\n }\n\n while (pending.length > 0) {\n const batch = pending.splice(0, MAX_CONTAINER_DOCUMENT_LANES);\n const pages = await input.listContainerDocumentLanes(\n batch.map(({ containerId, watermark }) => ({ containerId, watermark })),\n );\n batch.forEach((lane, index) => {\n if (!pages) {\n listed.set(lane.containerId, \"unavailable\");\n return;\n }\n const page = pages[index] ?? null;\n if (!page) {\n listed.set(lane.containerId, null);\n return;\n }\n lane.items.push(...page.items);\n lane.tombstones.push(...page.tombstones);\n lane.watermark = page.nextWatermark;\n if (!page.hasMore) {\n listed.set(lane.containerId, {\n isFullListing: lane.isFullListing,\n items: lane.items,\n nextWatermark: lane.watermark,\n tombstones: lane.tombstones,\n });\n } else if (lane.watermark) {\n pending.push(lane);\n } else {\n listed.set(lane.containerId, null);\n }\n });\n }\n return listed;\n}\n\n/** Lists the authoritative current ids by deliberately starting at no watermark. */\nexport async function listAllContainerDocumentIdsFromApi(input: {\n readonly apiClient: ContainerDocumentLaneApi;\n readonly containerId: string;\n readonly reportErrors?: boolean;\n}): Promise<ReadonlyArray<string> | null> {\n const listed = (\n await listAllContainerDocumentLanes({\n lanes: [{ containerId: input.containerId, watermark: null }],\n listContainerDocumentLanes: (lanes) =>\n listContainerDocumentLanesFromApi(\n input.apiClient,\n lanes,\n input.reportErrors,\n ),\n })\n ).get(input.containerId);\n return listed && listed !== \"unavailable\"\n ? Array.from(new Set(listed.items.map((document) => document.id)))\n : null;\n}\n\n/** Loads each lane's persisted watermark, then lists every lane in batches. */\nexport async function listContainerDocumentsFromWatermarks(input: {\n readonly containerIds: ReadonlyArray<string>;\n readonly listContainerDocumentLanes: ContainerDocumentLaneLister;\n readonly loadContainerDocumentWatermark: (\n containerId: string,\n ) => Promise<SyncWatermark | null>;\n}): Promise<ReadonlyMap<string, ContainerDocumentLaneListing>> {\n const lanes: Array<{ containerId: string; watermark: SyncWatermark | null }> =\n [];\n for (const containerId of new Set(input.containerIds)) {\n lanes.push({\n containerId,\n watermark: await input.loadContainerDocumentWatermark(containerId),\n });\n }\n return listAllContainerDocumentLanes({\n lanes,\n listContainerDocumentLanes: input.listContainerDocumentLanes,\n });\n}\n"]}
1
+ {"version":3,"file":"containerDocumentListing.js","sourceRoot":"","sources":["containerDocumentListing.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0CAA0C,EAC1C,sCAAsC,EACtC,4BAA4B,GAC7B,MAAM,+BAA+B,CAAC;AASvC,OAAO,EACL,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAOhC;;;GAGG;AACH,SAAS,SAAS,CAAC,SAAiB;IAClC,OAAO,IAAI,CAAC,GAAG,CACb,CAAC,EACD,IAAI,CAAC,GAAG,CACN,0CAA0C,EAC1C,IAAI,CAAC,KAAK,CAAC,sCAAsC,GAAG,SAAS,CAAC,CAC/D,CACF,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,iCAAiC,CACrD,SAAmC,EACnC,KAAiD,EACjD,YAAY,GAAG,IAAI;IAEnB,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACtC,MAAM,SAAS,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;QAC5C,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,MAAM,EAAE,QAAQ,KAAK,EAAE;QACvB,KAAK;QACL,SAAS,EAAE,IAAI,CAAC,SAAS;KAC1B,CAAC,CAAC,CAAC;IACJ,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,0BAA0B,CACzD,EAAE,KAAK,EAAE,SAAS,EAAE,EACpB,EAAE,YAAY,EAAE,CACjB,CAAC;IACF,IAAI,CAAC,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC3B,MAAM,eAAe,GAAG,IAAI,GAAG,CAC7B,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM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A+E;AAC/E,MAAM,CAAC,KAAK,UAAU,oCAAoC,CAAC,KAM1D;IACC,MAAM,KAAK,GACT,EAAE,CAAC;IACL,KAAK,MAAM,WAAW,IAAI,IAAI,GAAG,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;QACtD,KAAK,CAAC,IAAI,CAAC;YACT,WAAW;YACX,SAAS,EAAE,MAAM,KAAK,CAAC,8BAA8B,CAAC,WAAW,CAAC;SACnE,CAAC,CAAC;IACL,CAAC;IACD,OAAO,6BAA6B,CAAC;QACnC,KAAK;QACL,0BAA0B,EAAE,KAAK,CAAC,0BAA0B;KAC7D,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n DEFAULT_CONTAINER_DOCUMENT_LANE_PAGE_LIMIT,\n MAX_CONTAINER_DOCUMENT_LANE_PAGE_TOTAL,\n MAX_CONTAINER_DOCUMENT_LANES,\n} from \"@tearleads/validators/request\";\nimport type { SyncWatermark } from \"@tearleads/validators/response\";\nimport type {\n ContainerDocumentDiscoveryApi,\n ContainerDocumentLaneLister,\n ContainerDocumentLaneListing,\n ContainerDocumentTombstone,\n ListedContainerDocument,\n} from \"./documentDiscoveryTypes\";\nimport {\n changesDiscoveryOrder,\n isPublicationWatermark,\n} from \"./publicationWatermark\";\n\ntype ContainerDocumentLaneApi = Pick<\n ContainerDocumentDiscoveryApi,\n \"listContainerDocumentLanes\"\n>;\n\n/**\n * Rows each lane may return so a full batch stays within the single-page\n * maximum; a lone lane keeps the ordinary page size.\n */\nfunction laneLimit(laneCount: number): number {\n return Math.max(\n 1,\n Math.min(\n DEFAULT_CONTAINER_DOCUMENT_LANE_PAGE_LIMIT,\n Math.floor(MAX_CONTAINER_DOCUMENT_LANE_PAGE_TOTAL / laneCount),\n ),\n );\n}\n\n/**\n * Lists one page per requested lane in a single batched request. A lane the\n * server rejected yields `null` for that lane only; a failed request yields\n * `null` for the batch.\n */\nexport async function listContainerDocumentLanesFromApi(\n apiClient: ContainerDocumentLaneApi,\n lanes: Parameters<ContainerDocumentLaneLister>[0],\n reportErrors = true,\n): ReturnType<ContainerDocumentLaneLister> {\n const limit = laneLimit(lanes.length);\n const requested = lanes.map((lane, index) => ({\n containerId: lane.containerId,\n laneId: `lane-${index}`,\n limit,\n watermark: lane.watermark,\n }));\n const response = await apiClient.listContainerDocumentLanes(\n { lanes: requested },\n { reportErrors },\n );\n if (!response) return null;\n const resultsByLaneId = new Map(\n response.results.map((result) => [result.laneId, result]),\n );\n return requested.map(({ laneId }) => {\n const result = resultsByLaneId.get(laneId);\n return result && \"page\" in result ? result.page : null;\n });\n}\n\ninterface PendingContainerDocumentLane {\n readonly containerId: string;\n readonly isFullListing: boolean;\n readonly items: ListedContainerDocument[];\n readonly tombstones: ContainerDocumentTombstone[];\n watermark: SyncWatermark | null;\n isContinuation: boolean;\n}\n\nfunction removedDocumentPlacements(\n listed: ReadonlyMap<string, ContainerDocumentLaneListing>,\n) {\n const removals = new Map<string, Set<string>>();\n for (const lane of listed.values()) {\n if (\n !lane ||\n lane === \"unavailable\" ||\n !isPublicationWatermark(lane.nextWatermark)\n )\n continue;\n for (const row of lane.tombstones) {\n const containers = removals.get(row.documentId) ?? new Set<string>();\n containers.add(row.containerId);\n removals.set(row.documentId, containers);\n }\n }\n return removals;\n}\n\n// Publication-ordered lanes exclude historical unlinks for live placements.\n// Check the whole fetched batch before applying a lane: a linked document\n// carries its placements in other containers as well as the queried one.\nfunction discardConflictingDocumentPages(\n listed: Map<string, ContainerDocumentLaneListing>,\n) {\n const removals = removedDocumentPlacements(listed);\n if (!removals.size) return;\n const conflicted = new Set<string>();\n for (const [id, lane] of listed) {\n if (!lane || lane === \"unavailable\") continue;\n for (const item of lane.items) {\n const removed = removals.get(item.id);\n if (!removed) continue;\n for (const linked of item.linkedContainerIds.filter((container) =>\n removed.has(container),\n )) {\n conflicted.add(id);\n conflicted.add(linked);\n }\n }\n }\n for (const id of conflicted) listed.set(id, \"unavailable\");\n}\n\n/**\n * Pages every lane to exhaustion, batching first pages and continuations\n * alike. Each container keeps its own watermark, so one lane's failure or\n * continuation never changes another lane's result. A failed request marks\n * only the lanes it carried unavailable; the other batches still list.\n */\nexport async function listAllContainerDocumentLanes(input: {\n readonly lanes: ReadonlyArray<{\n readonly containerId: string;\n readonly watermark: SyncWatermark | null;\n }>;\n readonly listContainerDocumentLanes: ContainerDocumentLaneLister;\n}): Promise<ReadonlyMap<string, ContainerDocumentLaneListing>> {\n const listed = new Map<string, ContainerDocumentLaneListing>();\n const pending: PendingContainerDocumentLane[] = [];\n const queued = new Set<string>();\n for (const { containerId, watermark } of input.lanes) {\n if (queued.has(containerId)) continue;\n queued.add(containerId);\n pending.push({\n containerId,\n isFullListing: watermark === null,\n isContinuation: false,\n items: [],\n tombstones: [],\n watermark,\n });\n }\n\n while (pending.length > 0) {\n const batch = pending.splice(0, MAX_CONTAINER_DOCUMENT_LANES);\n const pages = await input.listContainerDocumentLanes(\n batch.map(({ containerId, watermark }) => ({ containerId, watermark })),\n );\n if (!pages) {\n for (const lane of batch) listed.set(lane.containerId, \"unavailable\");\n continue;\n }\n batch.forEach((lane, index) => {\n const page = pages[index] ?? null;\n if (!page) {\n listed.set(lane.containerId, null);\n return;\n }\n if (\n changesDiscoveryOrder(\n lane.watermark,\n page.nextWatermark,\n lane.isContinuation,\n )\n ) {\n listed.set(lane.containerId, \"unavailable\");\n return;\n }\n lane.items.push(...page.items);\n lane.tombstones.push(...page.tombstones);\n lane.watermark = page.nextWatermark;\n if (!page.hasMore) {\n listed.set(lane.containerId, {\n isFullListing: lane.isFullListing,\n items: lane.items,\n nextWatermark: lane.watermark,\n tombstones: lane.tombstones,\n });\n } else if (lane.watermark) {\n lane.isContinuation = true;\n pending.push(lane);\n } else {\n listed.set(lane.containerId, null);\n }\n });\n }\n discardConflictingDocumentPages(listed);\n return listed;\n}\n\n/** Lists the authoritative current ids by deliberately starting at no watermark. */\nexport async function listAllContainerDocumentIdsFromApi(input: {\n readonly apiClient: ContainerDocumentLaneApi;\n readonly containerId: string;\n readonly reportErrors?: boolean;\n}): Promise<ReadonlyArray<string> | null> {\n const listed = (\n await listAllContainerDocumentLanes({\n lanes: [{ containerId: input.containerId, watermark: null }],\n listContainerDocumentLanes: (lanes) =>\n listContainerDocumentLanesFromApi(\n input.apiClient,\n lanes,\n input.reportErrors,\n ),\n })\n ).get(input.containerId);\n return listed && listed !== \"unavailable\"\n ? Array.from(new Set(listed.items.map((document) => document.id)))\n : null;\n}\n\n/** Loads each lane's persisted watermark, then lists every lane in batches. */\nexport async function listContainerDocumentsFromWatermarks(input: {\n readonly containerIds: ReadonlyArray<string>;\n readonly listContainerDocumentLanes: ContainerDocumentLaneLister;\n readonly loadContainerDocumentWatermark: (\n containerId: string,\n ) => Promise<SyncWatermark | null>;\n}): Promise<ReadonlyMap<string, ContainerDocumentLaneListing>> {\n const lanes: Array<{ containerId: string; watermark: SyncWatermark | null }> =\n [];\n for (const containerId of new Set(input.containerIds)) {\n lanes.push({\n containerId,\n watermark: await input.loadContainerDocumentWatermark(containerId),\n });\n }\n return listAllContainerDocumentLanes({\n lanes,\n listContainerDocumentLanes: input.listContainerDocumentLanes,\n });\n}\n"]}
@@ -17,6 +17,7 @@ async function saveAppliedContainerDocumentWatermark(input) {
17
17
  export async function applyListedContainerDocuments(options, generation, listedDocuments) {
18
18
  const { containerId, upsertDiscoveredDocuments } = options;
19
19
  await options.cacheReferencedPrincipalPolicies?.(dedupeReferencedPrincipalStates(listedDocuments.items.flatMap((document) => document.referencedPrincipals)));
20
+ const tombstones = getApplicableDocumentTombstones(listedDocuments);
20
21
  const verification = await options.verifyDiscoveredDocuments(listedDocuments.items.map((document) => ({
21
22
  accessEpoch: document.currentAccessEpoch,
22
23
  accessStateHash: document.currentAccessStateHash,
@@ -26,7 +27,7 @@ export async function applyListedContainerDocuments(options, generation, listedD
26
27
  documentId: document.id,
27
28
  effectiveAccessLevel: document.effectiveAccessLevel,
28
29
  linkedContainerIds: document.linkedContainerIds,
29
- })), [containerId], generation, getApplicableDocumentTombstones(listedDocuments));
30
+ })), [containerId], generation, tombstones, new Set(tombstones.map((row) => row.documentId)));
30
31
  if (!(await verification.isCurrent()))
31
32
  return null;
32
33
  const verifiedInputs = verification.inputs;
@@ -42,7 +43,8 @@ export async function applyListedContainerDocuments(options, generation, listedD
42
43
  const tombstoneDocumentSummaries = await settleContainerDocumentTombstones({
43
44
  containerIds: [containerId],
44
45
  store: options,
45
- tombstones: getApplicableDocumentTombstones(listedDocuments),
46
+ isCurrent: verification.isCurrent,
47
+ tombstones,
46
48
  });
47
49
  const complete = await verification.commit();
48
50
  if (!(await verification.isCurrent()))
@@ -1 +1 @@
1
- {"version":3,"file":"documentDiscoveryApply.js","sourceRoot":"","sources":["documentDiscoveryApply.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,+BAA+B,EAAE,MAAM,iFAAiF,CAAC;AAClI,OAAO,EAAE,4BAA4B,EAAE,MAAM,wCAAwC,CAAC;AACtF,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC;AAc5E,KAAK,UAAU,qCAAqC,CAAC,KAKpD;IACC,MAAM,EAAE,WAAW,EAAE,eAAe,EAAE,8BAA8B,EAAE,GACpE,KAAK,CAAC;IACR,IAAI,CAAC,eAAe,CAAC,aAAa,EAAE,CAAC;QACnC,OAAO;IACT,CAAC;IAED,MAAM,8BAA8B,CAClC,WAAW,EACX,eAAe,CAAC,aAAa,EAC7B,KAAK,CAAC,YAAY,CACnB,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,OAAuC,EACvC,UAAkB,EAClB,eAAyC;IAEzC,MAAM,EAAE,WAAW,EAAE,yBAAyB,EAAE,GAAG,OAAO,CAAC;IAC3D,MAAM,OAAO,CAAC,gCAAgC,EAAE,CAC9C,+BAA+B,CAC7B,eAAe,CAAC,KAAK,CAAC,OAAO,CAC3B,CAAC,QA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MAAM,iCAAiC,CAAC;QACzE,YAAY,EAAE,CAAC,WAAW,CAAC;QAC3B,KAAK,EAAE,OAAO;QACd,UAAU,EAAE,+BAA+B,CAAC,eAAe,CAAC;KAC7D,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,MAAM,YAAY,CAAC,MAAM,EAAE,CAAC;IAC7C,IAAI,CAAC,CAAC,MAAM,YAAY,CAAC,SAAS,EAAE,CAAC;QAAE,OAAO,IAAI,CAAC;IACnD,MAAM,qCAAqC,CAAC;QAC1C,WAAW;QACX,eAAe;QACf,8BAA8B,EAAE,OAAO,CAAC,8BAA8B;QACtE,YAAY,EAAE,YAAY,CAAC,SAAS;KACrC,CAAC,CAAC;IACH,IAAI,CAAC,CAAC,MAAM,YAAY,CAAC,SAAS,EAAE,CAAC;QAAE,OAAO,IAAI,CAAC;IAEnD,IAAI,eAAe,CAAC,aAAa,EAAE,CAAC;QAClC,4EAA4E;QAC5E,OAAO,CAAC,aAAa,EAAE,CACrB,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CACrD,CAAC;IACJ,CAAC;IAED,OAAO,CAAC,QAAQ;QACd,mBAAmB,CAAC,MAAM,KAAK,CAAC;QAChC,0BAA0B,CAAC,MAAM,KAAK,CAAC;QACvC,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,CAAC,GAAG,mBAAmB,EAAE,GAAG,0BAA0B,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import type { DocumentSummary } from \"../../../data/documents/documentSummary\";\nimport { dedupeReferencedPrincipalStates } from \"../../../data/keyingProjectionVerification/principalPolicy/principalPolicyCache\";\nimport { withoutDeferredDocumentLinks } from \"../documentLinks/deferredDocumentLinks\";\nimport { settleContainerDocumentTombstones } from \"../tombstones/documentTombstoneGate\";\nimport { getApplicableDocumentTombstones } from \"./documentDiscoveryInputs\";\nimport type {\n DiscoverContainerDocumentsOptions,\n ListedContainerDocuments,\n} from \"./documentDiscoveryTypes\";\n\n/** A discovery lane's local stores; the listing itself is supplied by the caller. */\nexport type ContainerDocumentDiscoveryLane = Omit<\n DiscoverContainerDocumentsOptions,\n | \"beginDocumentDiscovery\"\n | \"listContainerDocumentLanes\"\n | \"loadContainerDocumentWatermark\"\n>;\n\nasync function saveAppliedContainerDocumentWatermark(input: {\n containerId: string;\n listedDocuments: ListedContainerDocuments;\n saveContainerDocumentWatermark: DiscoverContainerDocumentsOptions[\"saveContainerDocumentWatermark\"];\n stillCurrent: () => Promise<boolean>;\n}) {\n const { containerId, listedDocuments, saveContainerDocumentWatermark } =\n input;\n if (!listedDocuments.nextWatermark) {\n return;\n }\n\n await saveContainerDocumentWatermark(\n containerId,\n listedDocuments.nextWatermark,\n input.stillCurrent,\n );\n}\n\n/**\n * Verifies and applies one container's completed listing under a discovery\n * generation captured before the listing request, then advances its\n * watermark. Returns `null` when nothing was applied.\n */\nexport async function applyListedContainerDocuments(\n options: ContainerDocumentDiscoveryLane,\n generation: number,\n listedDocuments: ListedContainerDocuments,\n): Promise<ReadonlyArray<DocumentSummary> | null> {\n const { containerId, upsertDiscoveredDocuments } = options;\n await options.cacheReferencedPrincipalPolicies?.(\n dedupeReferencedPrincipalStates(\n listedDocuments.items.flatMap(\n (document) => document.referencedPrincipals,\n ),\n ),\n );\n\n const verification = await options.verifyDiscoveredDocuments(\n listedDocuments.items.map((document) => ({\n accessEpoch: document.currentAccessEpoch,\n accessStateHash: document.currentAccessStateHash,\n containerId,\n listedContainerIds: [containerId],\n createdAt: document.createdAt,\n documentId: document.id,\n effectiveAccessLevel: document.effectiveAccessLevel,\n linkedContainerIds: document.linkedContainerIds,\n })),\n [containerId],\n generation,\n getApplicableDocumentTombstones(listedDocuments),\n );\n if (!(await verification.isCurrent())) return null;\n const verifiedInputs = verification.inputs;\n const discoveredDocuments = verifiedInputs.length\n ? await upsertDiscoveredDocuments(verifiedInputs, verification.isCurrent)\n : [];\n\n if (verifiedInputs.length)\n await options.replaceDocumentLinksBatch(\n await withoutDeferredDocumentLinks(\n verifiedInputs.map((document) => ({\n documentId: document.documentId,\n accessEpoch: document.accessEpoch,\n containerIds: document.linkedContainerIds,\n })),\n discoveredDocuments,\n ),\n verification.isCurrent,\n );\n\n const tombstoneDocumentSummaries = await settleContainerDocumentTombstones({\n containerIds: [containerId],\n store: options,\n tombstones: getApplicableDocumentTombstones(listedDocuments),\n });\n\n const complete = await verification.commit();\n if (!(await verification.isCurrent())) return null;\n await saveAppliedContainerDocumentWatermark({\n containerId,\n listedDocuments,\n saveContainerDocumentWatermark: options.saveContainerDocumentWatermark,\n stillCurrent: verification.isCurrent,\n });\n if (!(await verification.isCurrent())) return null;\n\n if (listedDocuments.isFullListing) {\n // Initial probes consume ids as discovery hints and verify their own heads.\n options.onFullListing?.(\n listedDocuments.items.map((document) => document.id),\n );\n }\n\n return !complete &&\n discoveredDocuments.length === 0 &&\n tombstoneDocumentSummaries.length === 0\n ? null\n : [...discoveredDocuments, ...tombstoneDocumentSummaries];\n}\n"]}
1
+ {"version":3,"file":"documentDiscoveryApply.js","sourceRoot":"","sources":["documentDiscoveryApply.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,+BAA+B,EAAE,MAAM,iFAAiF,CAAC;AAClI,OAAO,EAAE,4BAA4B,EAAE,MAAM,wCAAwC,CAAC;AACtF,OAAO,EAAE,iCAAiC,EAAE,MAAM,qCAAqC,CAAC;AACxF,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC;AAc5E,KAAK,UAAU,qCAAqC,CAAC,KAKpD;IACC,MAAM,EAAE,WAAW,EAAE,eAAe,EAAE,8BAA8B,EAAE,GACpE,KAAK,CAAC;IACR,IAAI,CAAC,eAAe,CAAC,aAAa,EAAE,CAAC;QACnC,OAAO;IACT,CAAC;IAED,MAAM,8BAA8B,CAClC,WAAW,EACX,eAAe,CAAC,aAAa,EAC7B,KAAK,CAAC,YAAY,CACnB,CAAC;AACJ,CAAC;AAED;;;;G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C,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,CAAC,GAAG,mBAAmB,EAAE,GAAG,0BAA0B,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import type { DocumentSummary } from \"../../../data/documents/documentSummary\";\nimport { dedupeReferencedPrincipalStates } from \"../../../data/keyingProjectionVerification/principalPolicy/principalPolicyCache\";\nimport { withoutDeferredDocumentLinks } from \"../documentLinks/deferredDocumentLinks\";\nimport { settleContainerDocumentTombstones } from \"../tombstones/documentTombstoneGate\";\nimport { getApplicableDocumentTombstones } from \"./documentDiscoveryInputs\";\nimport type {\n DiscoverContainerDocumentsOptions,\n ListedContainerDocuments,\n} from \"./documentDiscoveryTypes\";\n\n/** A discovery lane's local stores; the listing itself is supplied by the caller. */\nexport type ContainerDocumentDiscoveryLane = Omit<\n DiscoverContainerDocumentsOptions,\n | \"beginDocumentDiscovery\"\n | \"listContainerDocumentLanes\"\n | \"loadContainerDocumentWatermark\"\n>;\n\nasync function saveAppliedContainerDocumentWatermark(input: {\n containerId: string;\n listedDocuments: ListedContainerDocuments;\n saveContainerDocumentWatermark: DiscoverContainerDocumentsOptions[\"saveContainerDocumentWatermark\"];\n stillCurrent: () => Promise<boolean>;\n}) {\n const { containerId, listedDocuments, saveContainerDocumentWatermark } =\n input;\n if (!listedDocuments.nextWatermark) {\n return;\n }\n\n await saveContainerDocumentWatermark(\n containerId,\n listedDocuments.nextWatermark,\n input.stillCurrent,\n );\n}\n\n/**\n * Verifies and applies one container's completed listing under a discovery\n * generation captured before the listing request, then advances its\n * watermark. Returns `null` when nothing was applied.\n */\nexport async function applyListedContainerDocuments(\n options: ContainerDocumentDiscoveryLane,\n generation: number,\n listedDocuments: ListedContainerDocuments,\n): Promise<ReadonlyArray<DocumentSummary> | null> {\n const { containerId, upsertDiscoveredDocuments } = options;\n await options.cacheReferencedPrincipalPolicies?.(\n dedupeReferencedPrincipalStates(\n listedDocuments.items.flatMap(\n (document) => document.referencedPrincipals,\n ),\n ),\n );\n\n const tombstones = getApplicableDocumentTombstones(listedDocuments);\n const verification = await options.verifyDiscoveredDocuments(\n listedDocuments.items.map((document) => ({\n accessEpoch: document.currentAccessEpoch,\n accessStateHash: document.currentAccessStateHash,\n containerId,\n listedContainerIds: [containerId],\n createdAt: document.createdAt,\n documentId: document.id,\n effectiveAccessLevel: document.effectiveAccessLevel,\n linkedContainerIds: document.linkedContainerIds,\n })),\n [containerId],\n generation,\n tombstones,\n new Set(tombstones.map((row) => row.documentId)),\n );\n if (!(await verification.isCurrent())) return null;\n const verifiedInputs = verification.inputs;\n const discoveredDocuments = verifiedInputs.length\n ? await upsertDiscoveredDocuments(verifiedInputs, verification.isCurrent)\n : [];\n\n if (verifiedInputs.length)\n await options.replaceDocumentLinksBatch(\n await withoutDeferredDocumentLinks(\n verifiedInputs.map((document) => ({\n documentId: document.documentId,\n accessEpoch: document.accessEpoch,\n containerIds: document.linkedContainerIds,\n })),\n discoveredDocuments,\n ),\n verification.isCurrent,\n );\n\n const tombstoneDocumentSummaries = await settleContainerDocumentTombstones({\n containerIds: [containerId],\n store: options,\n isCurrent: verification.isCurrent,\n tombstones,\n });\n\n const complete = await verification.commit();\n if (!(await verification.isCurrent())) return null;\n await saveAppliedContainerDocumentWatermark({\n containerId,\n listedDocuments,\n saveContainerDocumentWatermark: options.saveContainerDocumentWatermark,\n stillCurrent: verification.isCurrent,\n });\n if (!(await verification.isCurrent())) return null;\n\n if (listedDocuments.isFullListing) {\n // Initial probes consume ids as discovery hints and verify their own heads.\n options.onFullListing?.(\n listedDocuments.items.map((document) => document.id),\n );\n }\n\n return !complete &&\n discoveredDocuments.length === 0 &&\n tombstoneDocumentSummaries.length === 0\n ? null\n : [...discoveredDocuments, ...tombstoneDocumentSummaries];\n}\n"]}
@@ -10,15 +10,20 @@ function unlistedHeadVerdict(head, input) {
10
10
  ? null
11
11
  : "unavailable";
12
12
  }
13
- async function verifyInput(input, loadHead, loadLocalEpoch, store, generation, organizationId) {
14
- const localEpoch = await loadLocalEpoch(input.documentId).catch(() => Number.MAX_SAFE_INTEGER);
15
- const cached = await store.loadHead(input.documentId, input.accessStateHash);
13
+ async function readDiscoveryHead(input, loadHead, store, localEpoch, requiresCurrentHead) {
14
+ const cached = requiresCurrentHead
15
+ ? null
16
+ : await store.loadHead(input.documentId, input.accessStateHash);
16
17
  const minimumEpoch = Math.max(localEpoch, input.accessEpoch);
17
- const head = cached && cached.accessEpoch >= minimumEpoch
18
- ? cached
19
- : await loadHead(input.documentId, input.accessEpoch >= localEpoch
18
+ const usableCached = cached && cached.accessEpoch >= minimumEpoch ? cached : null;
19
+ return (usableCached ??
20
+ (await loadHead(input.documentId, !requiresCurrentHead && input.accessEpoch >= localEpoch
20
21
  ? (input.accessStateHash ?? undefined)
21
- : undefined);
22
+ : undefined)));
23
+ }
24
+ async function verifyInput(input, loadHead, loadLocalEpoch, store, generation, organizationId, requiresCurrentHead) {
25
+ const localEpoch = await loadLocalEpoch(input.documentId).catch(() => Number.MAX_SAFE_INTEGER);
26
+ const head = await readDiscoveryHead(input, loadHead, store, localEpoch, requiresCurrentHead);
22
27
  if (head === "not-found")
23
28
  return null;
24
29
  if (!head || head.accessEpoch < Math.max(localEpoch, input.accessEpoch))
@@ -33,7 +38,7 @@ async function verifyInput(input, loadHead, loadLocalEpoch, store, generation, o
33
38
  return "unavailable";
34
39
  if (!(await store.saveHead(input.documentId, head, generation)))
35
40
  return "unavailable";
36
- const { listedContainerIds: _listedContainerIds, ...document } = input;
41
+ const { listedContainerIds: _listedContainerIds, requiresCurrentHead: _requiresCurrentHead, ...document } = input;
37
42
  return {
38
43
  ...document,
39
44
  containerId,
@@ -49,11 +54,11 @@ async function verifyInput(input, loadHead, loadLocalEpoch, store, generation, o
49
54
  * a crash cannot lose a candidate behind an already advanced listing watermark.
50
55
  */
51
56
  export function createDiscoveredDocumentVerifier(organizationId, loadHead, loadLocalEpoch, store, onPendingDiscovery) {
52
- return async (inputs, containerIds, generation, tombstones = []) => {
57
+ return async (inputs, containerIds, generation, tombstones, conflictingDocumentIds) => {
53
58
  const isCurrent = () => store.isCurrent(generation);
54
59
  const lanes = new Set(containerIds);
55
- const removed = tombstones.filter((row) => lanes.has(row.containerId));
56
- if (!(await store.stage(inputs, generation, removed)))
60
+ const removed = (tombstones ?? []).filter((row) => lanes.has(row.containerId));
61
+ if (!(await store.stage(inputs, generation, removed, conflictingDocumentIds)))
57
62
  return { inputs: [], isCurrent, commit: async () => true };
58
63
  const candidates = groupPendingDocumentDiscoveries(await store.pending(containerIds, HEAD_LINK_SET_LOADS_PER_RUN));
59
64
  const verified = new Map();
@@ -65,7 +70,7 @@ export function createDiscoveredDocumentVerifier(organizationId, loadHead, loadL
65
70
  if (!group)
66
71
  break;
67
72
  const { input, rows } = group;
68
- const result = await verifyInput(input, loadHead, loadLocalEpoch, store, generation, organizationId);
73
+ const result = await verifyInput(input, loadHead, loadLocalEpoch, store, generation, organizationId, rows.some((row) => row.requiresCurrentHead === true));
69
74
  if (result === "unavailable") {
70
75
  await Promise.all(rows.map((row) => store.defer(row, generation)));
71
76
  continue;
@@ -1 +1 @@
1
- {"version":3,"file":"documentDiscoveryEvidence.js","sourceRoot":"","sources":["documentDiscoveryEvidence.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,GAC5B,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,+BAA+B,EAAE,MAAM,+BAA+B,CAAC;AAEhF,OAAO,EAAE,+BAA+B,EAAE,MAAM,qBAAqB,CAAC;AAEtE,wEAAwE;AACxE,yEAAyE;AACzE,SAAS,mBAAmB,CAC1B,IAAiC,EACjC,KAAkC;IAElC,OAAO,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC,WAAW;QACzC,IAAI,CAAC,eAAe,KAAK,KAAK,CAAC,eAAe;QAC9C,CAAC,CAAC,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC,WAAW,KAAK,MAAM,CAAC,gBAAgB,CAAC;QACvE,CAAC,CAAC,IAAI;QACN,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ACT,MAAM,EAAE,KAAK,IAAI,EAAE;gBACjB,MAAM,KAAK,CAAC,WAAW,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;gBAC7C,MAAM,UAAU,GAAG,MAAM,KAAK,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;gBACxD,IAAI,UAAU,KAAK,IAAI;oBAAE,kBAAkB,EAAE,CAAC,UAAU,CAAC,CAAC;gBAC1D,OAAO,UAAU,KAAK,IAAI,CAAC;YAC7B,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { DiscoveredDocumentInput } from \"../../../data/documents/documentSummary\";\nimport type {\n DiscoveredDocumentCandidate,\n DocumentDiscoveryEvidenceRepository,\n} from \"../../../data/persistence/documents/documentDiscoveryEvidencePersistence\";\nimport type {\n DocumentHeadLinkSetLoader,\n LocalDocumentAccessEpochLoader,\n VerifiedDocumentHeadLinkSet,\n} from \"../tombstones/documentTombstoneEvidence\";\nimport {\n HEAD_LINK_SET_LOAD_CONCURRENCY,\n HEAD_LINK_SET_LOADS_PER_RUN,\n} from \"../tombstones/documentTombstoneEvidence\";\nimport { groupPendingDocumentDiscoveries } from \"./documentDiscoveryCandidates\";\nimport type { DiscoverContainerDocumentsOptions } from \"./documentDiscoveryTypes\";\nimport { documentHeadMatchesOrganization } from \"./documentHeadScope\";\n\n// A same-epoch head can predate a link addition. Only the named head or\n// terminal signed purge evidence can settle a candidate without a retry.\nfunction unlistedHeadVerdict(\n head: VerifiedDocumentHeadLinkSet,\n input: DiscoveredDocumentCandidate,\n): null | \"unavailable\" {\n return head.accessEpoch > input.accessEpoch ||\n head.accessStateHash === input.accessStateHash ||\n (!head.accessStateHash && head.accessEpoch === Number.MAX_SAFE_INTEGER)\n ? null\n : \"unavailable\";\n}\n\nasync function verifyInput(\n input: DiscoveredDocumentCandidate,\n loadHead: DocumentHeadLinkSetLoader,\n loadLocalEpoch: LocalDocumentAccessEpochLoader,\n store: DocumentDiscoveryEvidenceRepository,\n generation: number,\n organizationId: string | undefined,\n): Promise<DiscoveredDocumentInput | null | \"unavailable\"> {\n const localEpoch = await loadLocalEpoch(input.documentId).catch(\n () => Number.MAX_SAFE_INTEGER,\n );\n const cached = await store.loadHead(input.documentId, input.accessStateHash);\n const minimumEpoch = Math.max(localEpoch, input.accessEpoch);\n const head =\n cached && cached.accessEpoch >= minimumEpoch\n ? cached\n : await loadHead(\n input.documentId,\n input.accessEpoch >= localEpoch\n ? (input.accessStateHash ?? undefined)\n : undefined,\n );\n if (head === \"not-found\") return null;\n if (!head || head.accessEpoch < Math.max(localEpoch, input.accessEpoch))\n return \"unavailable\";\n if (!documentHeadMatchesOrganization(head, organizationId))\n return \"unavailable\";\n // A stale first lane must not hide a document present in another listed lane.\n const containerId = input.listedContainerIds.find((id) =>\n head.linkedContainerIds.includes(id),\n );\n if (!containerId) return unlistedHeadVerdict(head, input);\n if (!head.accessStateHash) return \"unavailable\";\n if (!(await store.saveHead(input.documentId, head, generation)))\n return \"unavailable\";\n const { listedContainerIds: _listedContainerIds, ...document } = input;\n return {\n ...document,\n containerId,\n accessEpoch: head.accessEpoch,\n accessStateHash: head.accessStateHash,\n linkedContainerIds: head.linkedContainerIds,\n };\n}\n\n/**\n * Persist untrusted candidates before advancing listing watermarks. Each pass\n * checks a bounded batch; unavailable heads back off independently while verified\n * siblings and tombstones still settle. Acknowledgement follows local apply so\n * a crash cannot lose a candidate behind an already advanced listing watermark.\n */\nexport function createDiscoveredDocumentVerifier(\n organizationId: string | undefined,\n loadHead: DocumentHeadLinkSetLoader,\n loadLocalEpoch: LocalDocumentAccessEpochLoader,\n store: DocumentDiscoveryEvidenceRepository,\n onPendingDiscovery?: ((delayMs: number) => void) | undefined,\n): DiscoverContainerDocumentsOptions[\"verifyDiscoveredDocuments\"] {\n return async (inputs, containerIds, generation, tombstones = []) => {\n const isCurrent = () => store.isCurrent(generation);\n const lanes = new Set(containerIds);\n const removed = tombstones.filter((row) => lanes.has(row.containerId));\n if (!(await store.stage(inputs, generation, removed)))\n return { inputs: [], isCurrent, commit: async () => true };\n const candidates = groupPendingDocumentDiscoveries(\n await store.pending(containerIds, HEAD_LINK_SET_LOADS_PER_RUN),\n );\n const verified = new Map<string, DiscoveredDocumentInput>();\n const settled: DiscoveredDocumentCandidate[] = [];\n let next = 0;\n const worker = async () => {\n while (next < candidates.length) {\n const group = candidates[next++];\n if (!group) break;\n const { input, rows } = group;\n const result = await verifyInput(\n input,\n loadHead,\n loadLocalEpoch,\n store,\n generation,\n organizationId,\n );\n if (result === \"unavailable\") {\n await Promise.all(rows.map((row) => store.defer(row, generation)));\n continue;\n }\n if (result) verified.set(result.documentId, result);\n settled.push(...rows);\n }\n };\n await Promise.all(\n Array.from(\n { length: Math.min(HEAD_LINK_SET_LOAD_CONCURRENCY, candidates.length) },\n worker,\n ),\n );\n return {\n inputs: [...verified.values()],\n isCurrent,\n commit: async () => {\n await store.acknowledge(settled, generation);\n const retryDelay = await store.retryDelay(containerIds);\n if (retryDelay !== null) onPendingDiscovery?.(retryDelay);\n return retryDelay === null;\n },\n };\n };\n}\n"]}
1
+ {"version":3,"file":"documentDiscoveryEvidence.js","sourceRoot":"","sources":["documentDiscoveryEvidence.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,GAC5B,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,+BAA+B,EAAE,MAAM,+BAA+B,CAAC;AAEhF,OAAO,EAAE,+BAA+B,EAAE,MAAM,qBAAqB,CAAC;AAEtE,wEAAwE;AACxE,yEAAyE;AACzE,SAAS,mBAAmB,CAC1B,IAAiC,EACjC,KAAkC;IAElC,OAAO,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC,WAAW;QACzC,IAAI,CAAC,eAAe,KAAK,KAAK,CAAC,eAAe;QAC9C,CAAC,CAAC,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC,WAAW,KAAK,MAAM,CAAC,gBAAgB,CAAC;QACvE,CAAC,CAAC,IAA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AC,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;oBACnE,SAAS;gBACX,CAAC;gBACD,IAAI,MAAM;oBAAE,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;gBACpD,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;YACxB,CAAC;QACH,CAAC,CAAC;QACF,MAAM,OAAO,CAAC,GAAG,CACf,KAAK,CAAC,IAAI,CACR,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,8BAA8B,EAAE,UAAU,CAAC,MAAM,CAAC,EAAE,EACvE,MAAM,CACP,CACF,CAAC;QACF,OAAO;YACL,MAAM,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;YAC9B,SAAS;YACT,MAAM,EAAE,KAAK,IAAI,EAAE;gBACjB,MAAM,KAAK,CAAC,WAAW,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;gBAC7C,MAAM,UAAU,GAAG,MAAM,KAAK,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;gBACxD,IAAI,UAAU,KAAK,IAAI;oBAAE,kBAAkB,EAAE,CAAC,UAAU,CAAC,CAAC;gBAC1D,OAAO,UAAU,KAAK,IAAI,CAAC;YAC7B,CAAC;SACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import type { DiscoveredDocumentInput } from \"../../../data/documents/documentSummary\";\nimport type {\n DiscoveredDocumentCandidate,\n DocumentDiscoveryEvidenceRepository,\n} from \"../../../data/persistence/documents/documentDiscoveryEvidencePersistence\";\nimport type {\n DocumentHeadLinkSetLoader,\n LocalDocumentAccessEpochLoader,\n VerifiedDocumentHeadLinkSet,\n} from \"../tombstones/documentTombstoneEvidence\";\nimport {\n HEAD_LINK_SET_LOAD_CONCURRENCY,\n HEAD_LINK_SET_LOADS_PER_RUN,\n} from \"../tombstones/documentTombstoneEvidence\";\nimport { groupPendingDocumentDiscoveries } from \"./documentDiscoveryCandidates\";\nimport type { DiscoverContainerDocumentsOptions } from \"./documentDiscoveryTypes\";\nimport { documentHeadMatchesOrganization } from \"./documentHeadScope\";\n\n// A same-epoch head can predate a link addition. Only the named head or\n// terminal signed purge evidence can settle a candidate without a retry.\nfunction unlistedHeadVerdict(\n head: VerifiedDocumentHeadLinkSet,\n input: DiscoveredDocumentCandidate,\n): null | \"unavailable\" {\n return head.accessEpoch > input.accessEpoch ||\n head.accessStateHash === input.accessStateHash ||\n (!head.accessStateHash && head.accessEpoch === Number.MAX_SAFE_INTEGER)\n ? null\n : \"unavailable\";\n}\n\nasync function readDiscoveryHead(\n input: DiscoveredDocumentCandidate,\n loadHead: DocumentHeadLinkSetLoader,\n store: DocumentDiscoveryEvidenceRepository,\n localEpoch: number,\n requiresCurrentHead: boolean,\n) {\n const cached = requiresCurrentHead\n ? null\n : await store.loadHead(input.documentId, input.accessStateHash);\n const minimumEpoch = Math.max(localEpoch, input.accessEpoch);\n const usableCached =\n cached && cached.accessEpoch >= minimumEpoch ? cached : null;\n return (\n usableCached ??\n (await loadHead(\n input.documentId,\n !requiresCurrentHead && input.accessEpoch >= localEpoch\n ? (input.accessStateHash ?? undefined)\n : undefined,\n ))\n );\n}\n\nasync function verifyInput(\n input: DiscoveredDocumentCandidate,\n loadHead: DocumentHeadLinkSetLoader,\n loadLocalEpoch: LocalDocumentAccessEpochLoader,\n store: DocumentDiscoveryEvidenceRepository,\n generation: number,\n organizationId: string | undefined,\n requiresCurrentHead: boolean,\n): Promise<DiscoveredDocumentInput | null | \"unavailable\"> {\n const localEpoch = await loadLocalEpoch(input.documentId).catch(\n () => Number.MAX_SAFE_INTEGER,\n );\n const head = await readDiscoveryHead(\n input,\n loadHead,\n store,\n localEpoch,\n requiresCurrentHead,\n );\n if (head === \"not-found\") return null;\n if (!head || head.accessEpoch < Math.max(localEpoch, input.accessEpoch))\n return \"unavailable\";\n if (!documentHeadMatchesOrganization(head, organizationId))\n return \"unavailable\";\n // A stale first lane must not hide a document present in another listed lane.\n const containerId = input.listedContainerIds.find((id) =>\n head.linkedContainerIds.includes(id),\n );\n if (!containerId) return unlistedHeadVerdict(head, input);\n if (!head.accessStateHash) return \"unavailable\";\n if (!(await store.saveHead(input.documentId, head, generation)))\n return \"unavailable\";\n const {\n listedContainerIds: _listedContainerIds,\n requiresCurrentHead: _requiresCurrentHead,\n ...document\n } = input;\n return {\n ...document,\n containerId,\n accessEpoch: head.accessEpoch,\n accessStateHash: head.accessStateHash,\n linkedContainerIds: head.linkedContainerIds,\n };\n}\n\n/**\n * Persist untrusted candidates before advancing listing watermarks. Each pass\n * checks a bounded batch; unavailable heads back off independently while verified\n * siblings and tombstones still settle. Acknowledgement follows local apply so\n * a crash cannot lose a candidate behind an already advanced listing watermark.\n */\nexport function createDiscoveredDocumentVerifier(\n organizationId: string | undefined,\n loadHead: DocumentHeadLinkSetLoader,\n loadLocalEpoch: LocalDocumentAccessEpochLoader,\n store: DocumentDiscoveryEvidenceRepository,\n onPendingDiscovery?: ((delayMs: number) => void) | undefined,\n): DiscoverContainerDocumentsOptions[\"verifyDiscoveredDocuments\"] {\n return async (\n inputs,\n containerIds,\n generation,\n tombstones,\n conflictingDocumentIds,\n ) => {\n const isCurrent = () => store.isCurrent(generation);\n const lanes = new Set(containerIds);\n const removed = (tombstones ?? []).filter((row) =>\n lanes.has(row.containerId),\n );\n if (\n !(await store.stage(inputs, generation, removed, conflictingDocumentIds))\n )\n return { inputs: [], isCurrent, commit: async () => true };\n const candidates = groupPendingDocumentDiscoveries(\n await store.pending(containerIds, HEAD_LINK_SET_LOADS_PER_RUN),\n );\n const verified = new Map<string, DiscoveredDocumentInput>();\n const settled: DiscoveredDocumentCandidate[] = [];\n let next = 0;\n const worker = async () => {\n while (next < candidates.length) {\n const group = candidates[next++];\n if (!group) break;\n const { input, rows } = group;\n const result = await verifyInput(\n input,\n loadHead,\n loadLocalEpoch,\n store,\n generation,\n organizationId,\n rows.some((row) => row.requiresCurrentHead === true),\n );\n if (result === \"unavailable\") {\n await Promise.all(rows.map((row) => store.defer(row, generation)));\n continue;\n }\n if (result) verified.set(result.documentId, result);\n settled.push(...rows);\n }\n };\n await Promise.all(\n Array.from(\n { length: Math.min(HEAD_LINK_SET_LOAD_CONCURRENCY, candidates.length) },\n worker,\n ),\n );\n return {\n inputs: [...verified.values()],\n isCurrent,\n commit: async () => {\n await store.acknowledge(settled, generation);\n const retryDelay = await store.retryDelay(containerIds);\n if (retryDelay !== null) onPendingDiscovery?.(retryDelay);\n return retryDelay === null;\n },\n };\n };\n}\n"]}
@@ -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"]}