@tearleads/client-sdk 0.1.202 → 0.1.204

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 (30) hide show
  1. package/client/containerContents/documentDiscovery.js +9 -7
  2. package/client/containerContents/documentDiscovery.js.map +1 -1
  3. package/data/persistence/documents/containerDocumentTombstoneHoldRepository.js +112 -0
  4. package/data/persistence/documents/containerDocumentTombstoneHoldRepository.js.map +1 -0
  5. package/data/persistence/documents/containerDocumentTombstoneHoldsPersistence.js +0 -96
  6. package/data/persistence/documents/containerDocumentTombstoneHoldsPersistence.js.map +1 -1
  7. package/package.json +1 -1
  8. package/workflows/container-contents/documentLinks/index.d.ts +3 -3
  9. package/workflows/container-contents/documentLinks/index.js +7 -5
  10. package/workflows/container-contents/documentLinks/index.js.map +1 -1
  11. package/workflows/container-contents/documentQueries/documentSubtreeQueries.js +3 -2
  12. package/workflows/container-contents/documentQueries/documentSubtreeQueries.js.map +1 -1
  13. package/workflows/container-contents/tombstones/documentPurge.d.ts +2 -2
  14. package/workflows/container-contents/tombstones/documentPurge.js +7 -4
  15. package/workflows/container-contents/tombstones/documentPurge.js.map +1 -1
  16. package/workflows/documents/index.d.ts +3 -2
  17. package/workflows/documents/index.js +2 -2
  18. package/workflows/documents/index.js.map +1 -1
  19. package/workflows/documents/linkSet/linkSetRemote.d.ts +12 -13
  20. package/workflows/documents/linkSet/linkSetRemote.js +10 -1
  21. package/workflows/documents/linkSet/linkSetRemote.js.map +1 -1
  22. package/workflows/documents/purge/index.d.ts +15 -26
  23. package/workflows/documents/purge/index.js +6 -1
  24. package/workflows/documents/purge/index.js.map +1 -1
  25. package/workflows/documents/remote/remoteDocumentCollaborators.d.ts +29 -0
  26. package/workflows/documents/remote/remoteDocumentCollaborators.js +2 -0
  27. package/workflows/documents/remote/remoteDocumentCollaborators.js.map +1 -0
  28. package/workflows/documents/remote/remoteDocumentService.d.ts +18 -0
  29. package/workflows/documents/remote/remoteDocumentService.js +22 -0
  30. package/workflows/documents/remote/remoteDocumentService.js.map +1 -0
@@ -1,5 +1,5 @@
1
1
  import { sqlContainerContentsPersistence } from "../../data/persistence/container-contents/containerContentsPersistence.js";
2
- import { holdContainerDocumentTombstones, listKnownContainerDocumentPlacements, listRetryableHeldContainerDocumentTombstones, loadLocalDocumentAccessEpoch, refuteContainerDocumentTombstoneHolds, } from "../../data/persistence/documents/containerDocumentTombstoneHoldsPersistence.js";
2
+ import { ContainerDocumentTombstoneHoldRepository } from "../../data/persistence/documents/containerDocumentTombstoneHoldRepository.js";
3
3
  import { createDocumentDiscoveryEvidenceStore } from "../../data/persistence/documents/documentDiscoveryEvidencePersistence.js";
4
4
  import { getOrCreateOrganizationSyncPauses } from "../../data/sync/organizationSyncPauses.js";
5
5
  import { listContainerDocumentLanesFromApi } from "../../workflows/container-contents/documentDiscovery/containerDocumentListing.js";
@@ -12,10 +12,10 @@ import { guardDocumentDiscoveryWrites } from "../../workflows/container-contents
12
12
  import { createContainerDocumentTombstoneVerifier, createDocumentHeadLinkSetLoader, } from "../../workflows/container-contents/tombstones/documentTombstoneEvidence.js";
13
13
  import { createPrincipalPolicyOperation, createRuntimePrincipalPolicyWarmer, } from "../../workflows/principals/runtimePolicyWarmer.js";
14
14
  function createDiscoveryLane(input) {
15
- const { containerOrganizationId, execSql, request, runtime } = input;
15
+ const { containerOrganizationId, execSql, holdRepository, request, runtime } = input;
16
16
  const warmReferencedPrincipalPolicies = createRuntimePrincipalPolicyWarmer(runtime, { operation: input.policyOperation });
17
17
  const loadHead = createDocumentHeadLinkSetLoader(runtime, undefined, input.policyOperation);
18
- const loadEpoch = (documentId) => loadLocalDocumentAccessEpoch(execSql, documentId);
18
+ const loadEpoch = (documentId) => holdRepository.loadLocalDocumentAccessEpoch(documentId);
19
19
  return {
20
20
  ...input.queries,
21
21
  cacheReferencedPrincipalPolicies: (references) => containerOrganizationId
@@ -25,11 +25,11 @@ function createDiscoveryLane(input) {
25
25
  })
26
26
  : Promise.resolve(),
27
27
  containerId: request.containerId,
28
- holdContainerDocumentTombstones: (tombstones) => holdContainerDocumentTombstones(execSql, tombstones),
29
- listHeldContainerDocumentTombstones: (containerIds) => listRetryableHeldContainerDocumentTombstones(execSql, containerIds),
30
- listKnownContainerDocumentPlacements: (placements) => listKnownContainerDocumentPlacements(execSql, placements),
28
+ holdContainerDocumentTombstones: (tombstones) => holdRepository.holdContainerDocumentTombstones(tombstones),
29
+ listHeldContainerDocumentTombstones: (containerIds) => holdRepository.listRetryableHeldContainerDocumentTombstones(containerIds),
30
+ listKnownContainerDocumentPlacements: (placements) => holdRepository.listKnownContainerDocumentPlacements(placements),
31
31
  onFullListing: request.onFullListing,
32
- refuteContainerDocumentTombstoneHolds: (placements) => refuteContainerDocumentTombstoneHolds(execSql, placements),
32
+ refuteContainerDocumentTombstoneHolds: (placements) => holdRepository.refuteContainerDocumentTombstoneHolds(placements),
33
33
  verifyContainerDocumentTombstones: createContainerDocumentTombstoneVerifier(containerOrganizationId, loadHead, loadEpoch),
34
34
  ...guardDocumentDiscoveryWrites({
35
35
  execSql,
@@ -56,6 +56,7 @@ export async function discoverContainerDocumentLanesForRuntime({ onOutcome, requ
56
56
  const runtime = createContainerContentsWorkflowRuntime(input);
57
57
  const execSql = input.infra.execSql;
58
58
  const evidenceStore = createDocumentDiscoveryEvidenceStore(execSql);
59
+ const holdRepository = new ContainerDocumentTombstoneHoldRepository(execSql);
59
60
  const queries = createContainerDocumentQueriesFromRuntime(runtime);
60
61
  // Capture the reset fence before reading any lane's scope, so a reset
61
62
  // between these reads and the listing cannot publish old-organization
@@ -78,6 +79,7 @@ export async function discoverContainerDocumentLanesForRuntime({ onOutcome, requ
78
79
  isContainerPaused,
79
80
  evidenceStore,
80
81
  execSql,
82
+ holdRepository,
81
83
  policyOperation,
82
84
  queries,
83
85
  request,
@@ -1 +1 @@
1
- {"version":3,"file":"documentDiscovery.js","sourceRoot":"","sources":["documentDiscovery.ts"],"names":[],"mappings":"AACA,OAAO,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S,CAAC,cAAc;gBACxD,iBAAiB;gBACjB,aAAa;gBACb,OAAO;gBACP,eAAe;gBACf,OAAO;gBACP,OAAO;gBACP,OAAO;aACR,CAAC,CACH,CAAC;IACN,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,8BAA8B,CAAC;QACpD,UAAU;QACV,KAAK;QACL,0BAA0B,EAAE,CAAC,MAAM,EAAE,EAAE,CACrC,iCAAiC,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC;QAC9D,8BAA8B,EAAE,OAAO,CAAC,8BAA8B;QACtE,SAAS;KACV,CAAC,CAAC;IACH,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAClC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAC1D,CAAC;IACF,OAAO,QAAQ,CAAC,GAAG,CACjB,CAAC,EAAE,WAAW,EAAE,EAAE,EAAE,CAClB,oBAAoB,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,QAAQ,CAAC,WAAW,CAAC,CACjE,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,oCAAoC,CAAC,EACzD,cAAc,EACd,GAAG,OAAO,EAGX;IACC,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,wCAAwC,CAAC;QAC/D,QAAQ,EAAE,CAAC,OAAO,CAAC;QACnB,cAAc;KACf,CAAC,CAAC;IACH,IAAI,OAAO,EAAE,MAAM,KAAK,UAAU,EAAE,CAAC;QACnC,IAAI,OAAO,CAAC,MAAM,YAAY,wCAAwC,EAAE,CAAC;YACvE,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,OAAO,CAAC,MAAM,CAAC;IACvB,CAAC;IACD,OAAO,OAAO,EAAE,KAAK,IAAI,IAAI,CAAC;AAChC,CAAC","sourcesContent":["import type { DocumentSummary } from \"../../data/documents/documentSummary\";\nimport { sqlContainerContentsPersistence } from \"../../data/persistence/container-contents/containerContentsPersistence\";\nimport {\n holdContainerDocumentTombstones,\n listKnownContainerDocumentPlacements,\n listRetryableHeldContainerDocumentTombstones,\n loadLocalDocumentAccessEpoch,\n refuteContainerDocumentTombstoneHolds,\n} from \"../../data/persistence/documents/containerDocumentTombstoneHoldsPersistence\";\nimport { createDocumentDiscoveryEvidenceStore } from \"../../data/persistence/documents/documentDiscoveryEvidencePersistence\";\nimport { getOrCreateOrganizationSyncPauses } from \"../../data/sync/organizationSyncPauses\";\nimport { listContainerDocumentLanesFromApi } from \"../../workflows/container-contents/documentDiscovery/containerDocumentListing\";\nimport { createDiscoveredDocumentVerifier } from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryEvidence\";\nimport {\n type ContainerDocumentDiscoveryLane,\n type ContainerDocumentDiscoveryOutcome,\n type ContainerDocumentDiscoveryOutcomeHandler,\n discoverContainerDocumentLanes,\n} from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryLanes\";\nimport { ContainerDocumentListingUnavailableError } from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryTypes\";\nimport { createContainerDocumentQueriesFromRuntime } from \"../../workflows/container-contents/documentQueries\";\nimport {\n type ContainerContentsWorkflowRuntime,\n createContainerContentsWorkflowRuntime,\n} from \"../../workflows/container-contents/runtime\";\nimport { guardDocumentDiscoveryWrites } from \"../../workflows/container-contents/syncPause/discoveryGuard\";\nimport {\n createContainerDocumentTombstoneVerifier,\n createDocumentHeadLinkSetLoader,\n} from \"../../workflows/container-contents/tombstones/documentTombstoneEvidence\";\nimport {\n createPrincipalPolicyOperation,\n createRuntimePrincipalPolicyWarmer,\n type PrincipalPolicyOperation,\n} from \"../../workflows/principals/runtimePolicyWarmer\";\nimport type { InternalRuntime } from \"../runtime/workflowRuntime\";\n\nexport type { ContainerDocumentDiscoveryOutcome } from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryLanes\";\n\n/** One container to discover, with the reconciler's per-lane callbacks. */\nexport interface ContainerDocumentDiscoveryRequest {\n readonly containerId: string;\n readonly onFullListing?:\n | ((documentIds: ReadonlyArray<string>) => void)\n | undefined;\n readonly onPendingDiscovery?: ((delayMs: number) => void) | undefined;\n}\n\ntype ExecSql = ReturnType<InternalRuntime[\"workflowInput\"]>[\"infra\"][\"execSql\"];\n\nfunction createDiscoveryLane(input: {\n readonly containerOrganizationId: string | undefined;\n readonly isContainerPaused: () => boolean;\n readonly evidenceStore: ReturnType<\n typeof createDocumentDiscoveryEvidenceStore\n >;\n readonly execSql: ExecSql;\n /** The discovery pass's token: its lanes share one directory discovery. */\n readonly policyOperation: PrincipalPolicyOperation;\n readonly queries: ReturnType<\n typeof createContainerDocumentQueriesFromRuntime\n >;\n readonly request: ContainerDocumentDiscoveryRequest;\n readonly runtime: ContainerContentsWorkflowRuntime;\n}): ContainerDocumentDiscoveryLane {\n const { containerOrganizationId, execSql, request, runtime } = input;\n const warmReferencedPrincipalPolicies = createRuntimePrincipalPolicyWarmer(\n runtime,\n { operation: input.policyOperation },\n );\n const loadHead = createDocumentHeadLinkSetLoader(\n runtime,\n undefined,\n input.policyOperation,\n );\n const loadEpoch = (documentId: string) =>\n loadLocalDocumentAccessEpoch(execSql, documentId);\n return {\n ...input.queries,\n cacheReferencedPrincipalPolicies: (references) =>\n containerOrganizationId\n ? warmReferencedPrincipalPolicies({\n organizationId: containerOrganizationId,\n references,\n })\n : Promise.resolve(),\n containerId: request.containerId,\n holdContainerDocumentTombstones: (tombstones) =>\n holdContainerDocumentTombstones(execSql, tombstones),\n listHeldContainerDocumentTombstones: (containerIds) =>\n listRetryableHeldContainerDocumentTombstones(execSql, containerIds),\n listKnownContainerDocumentPlacements: (placements) =>\n listKnownContainerDocumentPlacements(execSql, placements),\n onFullListing: request.onFullListing,\n refuteContainerDocumentTombstoneHolds: (placements) =>\n refuteContainerDocumentTombstoneHolds(execSql, placements),\n verifyContainerDocumentTombstones: createContainerDocumentTombstoneVerifier(\n containerOrganizationId,\n loadHead,\n loadEpoch,\n ),\n ...guardDocumentDiscoveryWrites({\n execSql,\n isPaused: input.isContainerPaused,\n verifyDiscoveredDocuments: createDiscoveredDocumentVerifier(\n containerOrganizationId,\n loadHead,\n loadEpoch,\n input.evidenceStore,\n request.onPendingDiscovery,\n ),\n }),\n };\n}\n\n/**\n * Internal adapter shared by explicit discovery and background reconciliation:\n * one batched listing for every requested container, then per-container\n * verification scoped to each container's durable organization.\n */\nexport async function discoverContainerDocumentLanesForRuntime({\n onOutcome,\n requests,\n runtimeService,\n}: {\n readonly onOutcome?: ContainerDocumentDiscoveryOutcomeHandler | undefined;\n readonly requests: ReadonlyArray<ContainerDocumentDiscoveryRequest>;\n readonly runtimeService: InternalRuntime;\n}): Promise<ReadonlyArray<ContainerDocumentDiscoveryOutcome>> {\n const unlisted = (\n containerId: string,\n ): ContainerDocumentDiscoveryOutcome => ({\n containerId,\n status: \"fulfilled\",\n value: null,\n });\n const input = runtimeService.workflowInput();\n if (input.infra.dbStatus !== \"ready\") {\n return requests.map(({ containerId }) => unlisted(containerId));\n }\n const runtime = createContainerContentsWorkflowRuntime(input);\n const execSql = input.infra.execSql;\n const evidenceStore = createDocumentDiscoveryEvidenceStore(execSql);\n const queries = createContainerDocumentQueriesFromRuntime(runtime);\n // Capture the reset fence before reading any lane's scope, so a reset\n // between these reads and the listing cannot publish old-organization\n // evidence into its replacement.\n const generation = await evidenceStore.begin();\n const syncPauses = getOrCreateOrganizationSyncPauses(input.state.domainScope);\n await syncPauses.load(execSql);\n const lanes: ContainerDocumentDiscoveryLane[] = [];\n const policyOperation = createPrincipalPolicyOperation();\n for (const request of requests) {\n const stored =\n await sqlContainerContentsPersistence.loadContainerMetadataState(\n execSql,\n request.containerId,\n );\n if (!stored) continue;\n const isContainerPaused = () =>\n syncPauses.isContainerPaused(\n stored.container,\n runtimeService.workflowInput().auth.organizationId ?? null,\n );\n // A paused organization's container lists like one that is not stored,\n // and a pause landing mid-listing stops its write.\n if (!isContainerPaused())\n lanes.push(\n createDiscoveryLane({\n containerOrganizationId: stored.container.organizationId,\n isContainerPaused,\n evidenceStore,\n execSql,\n policyOperation,\n queries,\n request,\n runtime,\n }),\n );\n }\n const outcomes = await discoverContainerDocumentLanes({\n generation,\n lanes,\n listContainerDocumentLanes: (listed) =>\n listContainerDocumentLanesFromApi(runtime.apiClient, listed),\n loadContainerDocumentWatermark: queries.loadContainerDocumentWatermark,\n onOutcome,\n });\n const outcomeByContainerId = new Map(\n outcomes.map((outcome) => [outcome.containerId, outcome]),\n );\n return requests.map(\n ({ containerId }) =>\n outcomeByContainerId.get(containerId) ?? unlisted(containerId),\n );\n}\n\n/**\n * Discovers one container; a rejected lane rethrows its failure, except an\n * unavailable listing, which this contract reports as `null`.\n */\nexport async function discoverContainerDocumentsForRuntime({\n runtimeService,\n ...request\n}: ContainerDocumentDiscoveryRequest & {\n readonly runtimeService: InternalRuntime;\n}): Promise<ReadonlyArray<DocumentSummary> | null> {\n const [outcome] = await discoverContainerDocumentLanesForRuntime({\n requests: [request],\n runtimeService,\n });\n if (outcome?.status === \"rejected\") {\n if (outcome.reason instanceof ContainerDocumentListingUnavailableError) {\n return null;\n }\n throw outcome.reason;\n }\n return outcome?.value ?? null;\n}\n"]}
1
+ {"version":3,"file":"documentDiscovery.js","sourceRoot":"","sources":["documentDiscovery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,+BAA+B,EAAE,MAAM,wEAAwE,CAAC;AACzH,OAAO,EAAE,wCAAwC,EAAE,MAAM,2EAA2E,CAAC;AACrI,OAAO,EAAE,oCAAoC,EAAE,MAAM,uEAAuE,CAAC;AAC7H,OAAO,EAAE,iCAAiC,EAAE,MAAM,wCAAwC,CAAC;AAC3F,OAAO,EAAE,iCAAiC,EAAE,MAAM,+EAA+E,CAAC;AAClI,OAAO,EAAE,gCAAgC,EAAE,MAAM,gFAAgF,CAAC;AAClI,OAAO,EAIL,8BAA8B,GAC/B,MAAM,6EAA6E,CAAC;AACrF,OAAO,EAAE,wCAAwC,EAAE,MAAM,6EA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ACH,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAClC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC,CAC1D,CAAC;IACF,OAAO,QAAQ,CAAC,GAAG,CACjB,CAAC,EAAE,WAAW,EAAE,EAAE,EAAE,CAClB,oBAAoB,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,QAAQ,CAAC,WAAW,CAAC,CACjE,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,oCAAoC,CAAC,EACzD,cAAc,EACd,GAAG,OAAO,EAGX;IACC,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,wCAAwC,CAAC;QAC/D,QAAQ,EAAE,CAAC,OAAO,CAAC;QACnB,cAAc;KACf,CAAC,CAAC;IACH,IAAI,OAAO,EAAE,MAAM,KAAK,UAAU,EAAE,CAAC;QACnC,IAAI,OAAO,CAAC,MAAM,YAAY,wCAAwC,EAAE,CAAC;YACvE,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,OAAO,CAAC,MAAM,CAAC;IACvB,CAAC;IACD,OAAO,OAAO,EAAE,KAAK,IAAI,IAAI,CAAC;AAChC,CAAC","sourcesContent":["import type { DocumentSummary } from \"../../data/documents/documentSummary\";\nimport { sqlContainerContentsPersistence } from \"../../data/persistence/container-contents/containerContentsPersistence\";\nimport { ContainerDocumentTombstoneHoldRepository } from \"../../data/persistence/documents/containerDocumentTombstoneHoldRepository\";\nimport { createDocumentDiscoveryEvidenceStore } from \"../../data/persistence/documents/documentDiscoveryEvidencePersistence\";\nimport { getOrCreateOrganizationSyncPauses } from \"../../data/sync/organizationSyncPauses\";\nimport { listContainerDocumentLanesFromApi } from \"../../workflows/container-contents/documentDiscovery/containerDocumentListing\";\nimport { createDiscoveredDocumentVerifier } from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryEvidence\";\nimport {\n type ContainerDocumentDiscoveryLane,\n type ContainerDocumentDiscoveryOutcome,\n type ContainerDocumentDiscoveryOutcomeHandler,\n discoverContainerDocumentLanes,\n} from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryLanes\";\nimport { ContainerDocumentListingUnavailableError } from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryTypes\";\nimport { createContainerDocumentQueriesFromRuntime } from \"../../workflows/container-contents/documentQueries\";\nimport {\n type ContainerContentsWorkflowRuntime,\n createContainerContentsWorkflowRuntime,\n} from \"../../workflows/container-contents/runtime\";\nimport { guardDocumentDiscoveryWrites } from \"../../workflows/container-contents/syncPause/discoveryGuard\";\nimport {\n createContainerDocumentTombstoneVerifier,\n createDocumentHeadLinkSetLoader,\n} from \"../../workflows/container-contents/tombstones/documentTombstoneEvidence\";\nimport {\n createPrincipalPolicyOperation,\n createRuntimePrincipalPolicyWarmer,\n type PrincipalPolicyOperation,\n} from \"../../workflows/principals/runtimePolicyWarmer\";\nimport type { InternalRuntime } from \"../runtime/workflowRuntime\";\n\nexport type { ContainerDocumentDiscoveryOutcome } from \"../../workflows/container-contents/documentDiscovery/documentDiscoveryLanes\";\n\n/** One container to discover, with the reconciler's per-lane callbacks. */\nexport interface ContainerDocumentDiscoveryRequest {\n readonly containerId: string;\n readonly onFullListing?:\n | ((documentIds: ReadonlyArray<string>) => void)\n | undefined;\n readonly onPendingDiscovery?: ((delayMs: number) => void) | undefined;\n}\n\ntype ExecSql = ReturnType<InternalRuntime[\"workflowInput\"]>[\"infra\"][\"execSql\"];\n\nfunction createDiscoveryLane(input: {\n readonly containerOrganizationId: string | undefined;\n readonly isContainerPaused: () => boolean;\n readonly evidenceStore: ReturnType<\n typeof createDocumentDiscoveryEvidenceStore\n >;\n readonly execSql: ExecSql;\n readonly holdRepository: ContainerDocumentTombstoneHoldRepository;\n /** The discovery pass's token: its lanes share one directory discovery. */\n readonly policyOperation: PrincipalPolicyOperation;\n readonly queries: ReturnType<\n typeof createContainerDocumentQueriesFromRuntime\n >;\n readonly request: ContainerDocumentDiscoveryRequest;\n readonly runtime: ContainerContentsWorkflowRuntime;\n}): ContainerDocumentDiscoveryLane {\n const { containerOrganizationId, execSql, holdRepository, request, runtime } =\n input;\n const warmReferencedPrincipalPolicies = createRuntimePrincipalPolicyWarmer(\n runtime,\n { operation: input.policyOperation },\n );\n const loadHead = createDocumentHeadLinkSetLoader(\n runtime,\n undefined,\n input.policyOperation,\n );\n const loadEpoch = (documentId: string) =>\n holdRepository.loadLocalDocumentAccessEpoch(documentId);\n return {\n ...input.queries,\n cacheReferencedPrincipalPolicies: (references) =>\n containerOrganizationId\n ? warmReferencedPrincipalPolicies({\n organizationId: containerOrganizationId,\n references,\n })\n : Promise.resolve(),\n containerId: request.containerId,\n holdContainerDocumentTombstones: (tombstones) =>\n holdRepository.holdContainerDocumentTombstones(tombstones),\n listHeldContainerDocumentTombstones: (containerIds) =>\n holdRepository.listRetryableHeldContainerDocumentTombstones(containerIds),\n listKnownContainerDocumentPlacements: (placements) =>\n holdRepository.listKnownContainerDocumentPlacements(placements),\n onFullListing: request.onFullListing,\n refuteContainerDocumentTombstoneHolds: (placements) =>\n holdRepository.refuteContainerDocumentTombstoneHolds(placements),\n verifyContainerDocumentTombstones: createContainerDocumentTombstoneVerifier(\n containerOrganizationId,\n loadHead,\n loadEpoch,\n ),\n ...guardDocumentDiscoveryWrites({\n execSql,\n isPaused: input.isContainerPaused,\n verifyDiscoveredDocuments: createDiscoveredDocumentVerifier(\n containerOrganizationId,\n loadHead,\n loadEpoch,\n input.evidenceStore,\n request.onPendingDiscovery,\n ),\n }),\n };\n}\n\n/**\n * Internal adapter shared by explicit discovery and background reconciliation:\n * one batched listing for every requested container, then per-container\n * verification scoped to each container's durable organization.\n */\nexport async function discoverContainerDocumentLanesForRuntime({\n onOutcome,\n requests,\n runtimeService,\n}: {\n readonly onOutcome?: ContainerDocumentDiscoveryOutcomeHandler | undefined;\n readonly requests: ReadonlyArray<ContainerDocumentDiscoveryRequest>;\n readonly runtimeService: InternalRuntime;\n}): Promise<ReadonlyArray<ContainerDocumentDiscoveryOutcome>> {\n const unlisted = (\n containerId: string,\n ): ContainerDocumentDiscoveryOutcome => ({\n containerId,\n status: \"fulfilled\",\n value: null,\n });\n const input = runtimeService.workflowInput();\n if (input.infra.dbStatus !== \"ready\") {\n return requests.map(({ containerId }) => unlisted(containerId));\n }\n const runtime = createContainerContentsWorkflowRuntime(input);\n const execSql = input.infra.execSql;\n const evidenceStore = createDocumentDiscoveryEvidenceStore(execSql);\n const holdRepository = new ContainerDocumentTombstoneHoldRepository(execSql);\n const queries = createContainerDocumentQueriesFromRuntime(runtime);\n // Capture the reset fence before reading any lane's scope, so a reset\n // between these reads and the listing cannot publish old-organization\n // evidence into its replacement.\n const generation = await evidenceStore.begin();\n const syncPauses = getOrCreateOrganizationSyncPauses(input.state.domainScope);\n await syncPauses.load(execSql);\n const lanes: ContainerDocumentDiscoveryLane[] = [];\n const policyOperation = createPrincipalPolicyOperation();\n for (const request of requests) {\n const stored =\n await sqlContainerContentsPersistence.loadContainerMetadataState(\n execSql,\n request.containerId,\n );\n if (!stored) continue;\n const isContainerPaused = () =>\n syncPauses.isContainerPaused(\n stored.container,\n runtimeService.workflowInput().auth.organizationId ?? null,\n );\n // A paused organization's container lists like one that is not stored,\n // and a pause landing mid-listing stops its write.\n if (!isContainerPaused())\n lanes.push(\n createDiscoveryLane({\n containerOrganizationId: stored.container.organizationId,\n isContainerPaused,\n evidenceStore,\n execSql,\n holdRepository,\n policyOperation,\n queries,\n request,\n runtime,\n }),\n );\n }\n const outcomes = await discoverContainerDocumentLanes({\n generation,\n lanes,\n listContainerDocumentLanes: (listed) =>\n listContainerDocumentLanesFromApi(runtime.apiClient, listed),\n loadContainerDocumentWatermark: queries.loadContainerDocumentWatermark,\n onOutcome,\n });\n const outcomeByContainerId = new Map(\n outcomes.map((outcome) => [outcome.containerId, outcome]),\n );\n return requests.map(\n ({ containerId }) =>\n outcomeByContainerId.get(containerId) ?? unlisted(containerId),\n );\n}\n\n/**\n * Discovers one container; a rejected lane rethrows its failure, except an\n * unavailable listing, which this contract reports as `null`.\n */\nexport async function discoverContainerDocumentsForRuntime({\n runtimeService,\n ...request\n}: ContainerDocumentDiscoveryRequest & {\n readonly runtimeService: InternalRuntime;\n}): Promise<ReadonlyArray<DocumentSummary> | null> {\n const [outcome] = await discoverContainerDocumentLanesForRuntime({\n requests: [request],\n runtimeService,\n });\n if (outcome?.status === \"rejected\") {\n if (outcome.reason instanceof ContainerDocumentListingUnavailableError) {\n return null;\n }\n throw outcome.reason;\n }\n return outcome?.value ?? null;\n}\n"]}
@@ -0,0 +1,112 @@
1
+ import { and, eq, max } from "drizzle-orm";
2
+ import { DOCUMENTS_APP_KIND } from "../../documents/documentConstants.js";
3
+ import { containerDocumentTombstoneHolds } from "../../sqlite/schema/index.js";
4
+ import { documentContainerProjectionTables } from "../../sqlite/schema/documentProjectionSchema.js";
5
+ import { documents, documentTables, } from "../../sqlite/schema/documentRecordSchema.js";
6
+ import { remoteResetBatches } from "../../sqlite/sqliteBindBudget.js";
7
+ import { getClientSQLitePersistenceRuntime } from "../../sqlite/sqlitePersistenceRuntime.js";
8
+ import { ensureSqlTables, runSerializedSqlMutation, } from "../../sqlite/sqlSchema.js";
9
+ import { heldTombstoneRetryDelayMs } from "./containerDocumentTombstoneHoldsPersistence.js";
10
+ import { deleteContainerDocumentTombstoneHoldRows, holdContainerDocumentTombstonesInTransaction, listContainerDocumentTombstoneHoldsForDocumentsInTransaction, listContainerDocumentTombstoneHoldsInTransaction, listKnownContainerDocumentPlacementsInTransaction, partitionContainerDocumentTombstoneHolds, placementPairsMatch, } from "./internal/containerDocumentTombstoneHolds.js";
11
+ function isHoldDue(hold, now) {
12
+ return (Date.parse(hold.updatedAt) + heldTombstoneRetryDelayMs(hold.attempts) <=
13
+ now.getTime());
14
+ }
15
+ /**
16
+ * Container document tombstone holds bound to one executor and clock. The
17
+ * clock stamps new holds and decides which held tombstones are due for retry.
18
+ */
19
+ export class ContainerDocumentTombstoneHoldRepository {
20
+ execSql;
21
+ now;
22
+ constructor(execSql, now = () => new Date()) {
23
+ this.execSql = execSql;
24
+ this.now = now;
25
+ }
26
+ async holdContainerDocumentTombstones(tombstones) {
27
+ if (tombstones.length === 0)
28
+ return;
29
+ const now = this.now().toISOString();
30
+ return runSerializedSqlMutation(this.execSql, async (lockedExecSql) => {
31
+ await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);
32
+ const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);
33
+ await db.transaction((tx) => holdContainerDocumentTombstonesInTransaction(tx, tombstones, now));
34
+ });
35
+ }
36
+ async refuteContainerDocumentTombstoneHolds(placements) {
37
+ if (placements.length === 0)
38
+ return;
39
+ return runSerializedSqlMutation(this.execSql, async (lockedExecSql) => {
40
+ await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);
41
+ const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);
42
+ await db.transaction(async (tx) => {
43
+ for (const batch of remoteResetBatches(placements)) {
44
+ await tx
45
+ .update(containerDocumentTombstoneHolds)
46
+ .set({ hidden: false })
47
+ .where(placementPairsMatch(batch, containerDocumentTombstoneHolds.documentId, containerDocumentTombstoneHolds.containerId))
48
+ .run();
49
+ }
50
+ });
51
+ });
52
+ }
53
+ async listContainerDocumentTombstoneHolds(containerIds) {
54
+ if (containerIds.length === 0)
55
+ return [];
56
+ await ensureSqlTables(this.execSql, documentContainerProjectionTables);
57
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
58
+ return listContainerDocumentTombstoneHoldsInTransaction(db, containerIds);
59
+ }
60
+ async listContainerDocumentTombstoneHoldsForDocuments(documentIds) {
61
+ if (documentIds.length === 0)
62
+ return [];
63
+ await ensureSqlTables(this.execSql, documentContainerProjectionTables);
64
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
65
+ return listContainerDocumentTombstoneHoldsForDocumentsInTransaction(db, documentIds);
66
+ }
67
+ async listKnownContainerDocumentPlacements(placements) {
68
+ if (placements.length === 0)
69
+ return [];
70
+ await ensureSqlTables(this.execSql, documentContainerProjectionTables);
71
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
72
+ return listKnownContainerDocumentPlacementsInTransaction(db, placements);
73
+ }
74
+ /**
75
+ * The held tombstones on the given containers that are due for another
76
+ * verification attempt. Holds whose placement is gone are pruned first; that
77
+ * is the only write, and it is taken under the mutation lock only when there
78
+ * is something to prune.
79
+ */
80
+ async listRetryableHeldContainerDocumentTombstones(containerIds) {
81
+ if (containerIds.length === 0)
82
+ return [];
83
+ const now = this.now();
84
+ await ensureSqlTables(this.execSql, documentContainerProjectionTables);
85
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
86
+ const { live, orphaned } = await partitionContainerDocumentTombstoneHolds(db, await listContainerDocumentTombstoneHoldsInTransaction(db, containerIds));
87
+ if (orphaned.length > 0) {
88
+ // Re-check under the lock: a hold re-created between the unlocked read
89
+ // and this write is hiding a placement again and must survive.
90
+ await runSerializedSqlMutation(this.execSql, (lockedExecSql) => getClientSQLitePersistenceRuntime(lockedExecSql).db.transaction(async (tx) => deleteContainerDocumentTombstoneHoldRows(tx, (await partitionContainerDocumentTombstoneHolds(tx, orphaned))
91
+ .orphaned)));
92
+ }
93
+ return live
94
+ .filter((hold) => isHoldDue(hold, now))
95
+ .map((hold) => ({
96
+ containerId: hold.containerId,
97
+ documentId: hold.documentId,
98
+ updatedAt: hold.tombstonedAt,
99
+ }));
100
+ }
101
+ /** Ordinary-document floors must exclude unsigned container-metadata epochs. */
102
+ async loadLocalDocumentAccessEpoch(documentId) {
103
+ await ensureSqlTables(this.execSql, documentTables);
104
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
105
+ const [row] = await db
106
+ .select({ accessEpoch: max(documents.accessEpoch) })
107
+ .from(documents)
108
+ .where(and(eq(documents.appKind, DOCUMENTS_APP_KIND), eq(documents.documentId, documentId)));
109
+ return row?.accessEpoch ?? 0;
110
+ }
111
+ }
112
+ //# sourceMappingURL=containerDocumentTombstoneHoldRepository.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"containerDocumentTombstoneHoldRepository.js","sourceRoot":"","sources":["containerDocumentTombstoneHoldRepository.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,+BAA+B,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,iCAAiC,EAAE,MAAM,8CAA8C,CAAC;AACjG,OAAO,EACL,SAAS,EACT,cAAc,GACf,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,iCAAiC,EAAE,MAAM,uCAAuC,CAAC;AAC1F,OAAO,EAEL,eAAe,EACf,wBAAwB,GACzB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,yBAAyB,EAAE,MAAM,8CAA8C,CAAC;AACzF,OAAO,EAGL,wCAAwC,EAExC,4CAA4C,EAC5C,4DAA4D,EAC5D,gDAAgD,EAChD,iDAAiD,EACjD,wCAAwC,EACxC,mBAAmB,GACpB,MAAM,4CAA4C,CAAC;AAEpD,SAAS,SAAS,CAChB,IAAuC,EACvC,GAAS;IAET,OAAO,CACL,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,yBAAyB,CAAC,IAAI,CAAC,QAAQ,CAAC;QACrE,GAAG,CAAC,OAAO,EAAE,CACd,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,OAAO,wCAAwC;IAClC,OAAO,CAAU;IACjB,GAAG,CAAa;IAEjC,YAAY,OAAgB,EAAE,MAAkB,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;QAC9D,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,GAAG,GAAG,GAA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,EAAE,WAAW,EAAE,GAAG,CAAC,SAAS,CAAC,WAAW,CAAC,EAAE,CAAC;aACnD,IAAI,CAAC,SAAS,CAAC;aACf,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,kBAAkB,CAAC,EACzC,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC,CACrC,CACF,CAAC;QACJ,OAAO,GAAG,EAAE,WAAW,IAAI,CAAC,CAAC;IAC/B,CAAC;CACF","sourcesContent":["import { and, eq, max } from \"drizzle-orm\";\nimport { DOCUMENTS_APP_KIND } from \"../../documents/documentConstants\";\nimport { containerDocumentTombstoneHolds } from \"../../sqlite/schema\";\nimport { documentContainerProjectionTables } from \"../../sqlite/schema/documentProjectionSchema\";\nimport {\n documents,\n documentTables,\n} from \"../../sqlite/schema/documentRecordSchema\";\nimport { remoteResetBatches } from \"../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../sqlite/sqlSchema\";\nimport { heldTombstoneRetryDelayMs } from \"./containerDocumentTombstoneHoldsPersistence\";\nimport {\n type ContainerDocumentPlacementKey,\n type ContainerDocumentTombstoneHoldRow,\n deleteContainerDocumentTombstoneHoldRows,\n type HeldContainerDocumentTombstone,\n holdContainerDocumentTombstonesInTransaction,\n listContainerDocumentTombstoneHoldsForDocumentsInTransaction,\n listContainerDocumentTombstoneHoldsInTransaction,\n listKnownContainerDocumentPlacementsInTransaction,\n partitionContainerDocumentTombstoneHolds,\n placementPairsMatch,\n} from \"./internal/containerDocumentTombstoneHolds\";\n\nfunction isHoldDue(\n hold: ContainerDocumentTombstoneHoldRow,\n now: Date,\n): boolean {\n return (\n Date.parse(hold.updatedAt) + heldTombstoneRetryDelayMs(hold.attempts) <=\n now.getTime()\n );\n}\n\n/**\n * Container document tombstone holds bound to one executor and clock. The\n * clock stamps new holds and decides which held tombstones are due for retry.\n */\nexport class ContainerDocumentTombstoneHoldRepository {\n private readonly execSql: ExecSql;\n private readonly now: () => Date;\n\n constructor(execSql: ExecSql, now: () => Date = () => new Date()) {\n this.execSql = execSql;\n this.now = now;\n }\n\n async holdContainerDocumentTombstones(\n tombstones: ReadonlyArray<HeldContainerDocumentTombstone>,\n ): Promise<void> {\n if (tombstones.length === 0) return;\n const now = this.now().toISOString();\n return runSerializedSqlMutation(this.execSql, async (lockedExecSql) => {\n await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);\n await db.transaction((tx) =>\n holdContainerDocumentTombstonesInTransaction(tx, tombstones, now),\n );\n });\n }\n\n async refuteContainerDocumentTombstoneHolds(\n placements: ReadonlyArray<ContainerDocumentPlacementKey>,\n ): Promise<void> {\n if (placements.length === 0) return;\n return runSerializedSqlMutation(this.execSql, async (lockedExecSql) => {\n await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);\n await db.transaction(async (tx) => {\n for (const batch of remoteResetBatches(placements)) {\n await tx\n .update(containerDocumentTombstoneHolds)\n .set({ hidden: false })\n .where(\n placementPairsMatch(\n batch,\n containerDocumentTombstoneHolds.documentId,\n containerDocumentTombstoneHolds.containerId,\n ),\n )\n .run();\n }\n });\n });\n }\n\n async listContainerDocumentTombstoneHolds(\n containerIds: ReadonlyArray<string>,\n ): Promise<ContainerDocumentTombstoneHoldRow[]> {\n if (containerIds.length === 0) return [];\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n return listContainerDocumentTombstoneHoldsInTransaction(db, containerIds);\n }\n\n async listContainerDocumentTombstoneHoldsForDocuments(\n documentIds: ReadonlyArray<string>,\n ): Promise<ContainerDocumentPlacementKey[]> {\n if (documentIds.length === 0) return [];\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n return listContainerDocumentTombstoneHoldsForDocumentsInTransaction(\n db,\n documentIds,\n );\n }\n\n async listKnownContainerDocumentPlacements(\n placements: ReadonlyArray<ContainerDocumentPlacementKey>,\n ): Promise<ContainerDocumentPlacementKey[]> {\n if (placements.length === 0) return [];\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n return listKnownContainerDocumentPlacementsInTransaction(db, placements);\n }\n\n /**\n * The held tombstones on the given containers that are due for another\n * verification attempt. Holds whose placement is gone are pruned first; that\n * is the only write, and it is taken under the mutation lock only when there\n * is something to prune.\n */\n async listRetryableHeldContainerDocumentTombstones(\n containerIds: ReadonlyArray<string>,\n ): Promise<HeldContainerDocumentTombstone[]> {\n if (containerIds.length === 0) return [];\n const now = this.now();\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n const { live, orphaned } = await partitionContainerDocumentTombstoneHolds(\n db,\n await listContainerDocumentTombstoneHoldsInTransaction(db, containerIds),\n );\n if (orphaned.length > 0) {\n // Re-check under the lock: a hold re-created between the unlocked read\n // and this write is hiding a placement again and must survive.\n await runSerializedSqlMutation(this.execSql, (lockedExecSql) =>\n getClientSQLitePersistenceRuntime(lockedExecSql).db.transaction(\n async (tx) =>\n deleteContainerDocumentTombstoneHoldRows(\n tx,\n (await partitionContainerDocumentTombstoneHolds(tx, orphaned))\n .orphaned,\n ),\n ),\n );\n }\n return live\n .filter((hold) => isHoldDue(hold, now))\n .map((hold) => ({\n containerId: hold.containerId,\n documentId: hold.documentId,\n updatedAt: hold.tombstonedAt,\n }));\n }\n\n /** Ordinary-document floors must exclude unsigned container-metadata epochs. */\n async loadLocalDocumentAccessEpoch(documentId: string): Promise<number> {\n await ensureSqlTables(this.execSql, documentTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n const [row] = await db\n .select({ accessEpoch: max(documents.accessEpoch) })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, DOCUMENTS_APP_KIND),\n eq(documents.documentId, documentId),\n ),\n );\n return row?.accessEpoch ?? 0;\n }\n}\n"]}
@@ -1,12 +1,3 @@
1
- import { and, eq, max } from "drizzle-orm";
2
- import { DOCUMENTS_APP_KIND } from "../../documents/documentConstants.js";
3
- import { containerDocumentTombstoneHolds } from "../../sqlite/schema/index.js";
4
- import { documentContainerProjectionTables } from "../../sqlite/schema/documentProjectionSchema.js";
5
- import { documents, documentTables, } from "../../sqlite/schema/documentRecordSchema.js";
6
- import { remoteResetBatches } from "../../sqlite/sqliteBindBudget.js";
7
- import { getClientSQLitePersistenceRuntime } from "../../sqlite/sqlitePersistenceRuntime.js";
8
- import { ensureSqlTables, runSerializedSqlMutation, } from "../../sqlite/sqlSchema.js";
9
- import { deleteContainerDocumentTombstoneHoldRows, holdContainerDocumentTombstonesInTransaction, listContainerDocumentTombstoneHoldsForDocumentsInTransaction, listContainerDocumentTombstoneHoldsInTransaction, listKnownContainerDocumentPlacementsInTransaction, partitionContainerDocumentTombstoneHolds, placementPairsMatch, } from "./internal/containerDocumentTombstoneHolds.js";
10
1
  export { containerDocumentPlacementKey, deleteContainerDocumentTombstoneHoldRowsForLinks, deleteContainerDocumentTombstoneHoldsForContainers, deleteContainerDocumentTombstoneHoldsForDocuments, placementPairsMatch, } from "./internal/containerDocumentTombstoneHolds.js";
11
2
  /**
12
3
  * Retry backoff for a held tombstone: fifteen minutes after the first failed
@@ -21,91 +12,4 @@ export function heldTombstoneRetryDelayMs(attempts) {
21
12
  const doublings = Math.min(Math.max(attempts, 1) - 1, HELD_TOMBSTONE_MAX_BACKOFF_DOUBLINGS);
22
13
  return HELD_TOMBSTONE_RETRY_INTERVAL_MS * 2 ** doublings;
23
14
  }
24
- function isHoldDue(hold, now) {
25
- return (Date.parse(hold.updatedAt) + heldTombstoneRetryDelayMs(hold.attempts) <=
26
- now.getTime());
27
- }
28
- export async function holdContainerDocumentTombstones(execSql, tombstones, now = new Date().toISOString()) {
29
- if (tombstones.length === 0)
30
- return;
31
- return runSerializedSqlMutation(execSql, async (lockedExecSql) => {
32
- await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);
33
- const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);
34
- await db.transaction((tx) => holdContainerDocumentTombstonesInTransaction(tx, tombstones, now));
35
- });
36
- }
37
- export async function refuteContainerDocumentTombstoneHolds(execSql, placements) {
38
- if (placements.length === 0)
39
- return;
40
- return runSerializedSqlMutation(execSql, async (lockedExecSql) => {
41
- await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);
42
- const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);
43
- await db.transaction(async (tx) => {
44
- for (const batch of remoteResetBatches(placements)) {
45
- await tx
46
- .update(containerDocumentTombstoneHolds)
47
- .set({ hidden: false })
48
- .where(placementPairsMatch(batch, containerDocumentTombstoneHolds.documentId, containerDocumentTombstoneHolds.containerId))
49
- .run();
50
- }
51
- });
52
- });
53
- }
54
- export async function listContainerDocumentTombstoneHolds(execSql, containerIds) {
55
- if (containerIds.length === 0)
56
- return [];
57
- await ensureSqlTables(execSql, documentContainerProjectionTables);
58
- const { db } = getClientSQLitePersistenceRuntime(execSql);
59
- return listContainerDocumentTombstoneHoldsInTransaction(db, containerIds);
60
- }
61
- export async function listContainerDocumentTombstoneHoldsForDocuments(execSql, documentIds) {
62
- if (documentIds.length === 0)
63
- return [];
64
- await ensureSqlTables(execSql, documentContainerProjectionTables);
65
- const { db } = getClientSQLitePersistenceRuntime(execSql);
66
- return listContainerDocumentTombstoneHoldsForDocumentsInTransaction(db, documentIds);
67
- }
68
- export async function listKnownContainerDocumentPlacements(execSql, placements) {
69
- if (placements.length === 0)
70
- return [];
71
- await ensureSqlTables(execSql, documentContainerProjectionTables);
72
- const { db } = getClientSQLitePersistenceRuntime(execSql);
73
- return listKnownContainerDocumentPlacementsInTransaction(db, placements);
74
- }
75
- /**
76
- * The held tombstones on the given containers that are due for another
77
- * verification attempt. Holds whose placement is gone are pruned first; that
78
- * is the only write, and it is taken under the mutation lock only when there
79
- * is something to prune.
80
- */
81
- export async function listRetryableHeldContainerDocumentTombstones(execSql, containerIds, now = new Date()) {
82
- if (containerIds.length === 0)
83
- return [];
84
- await ensureSqlTables(execSql, documentContainerProjectionTables);
85
- const { db } = getClientSQLitePersistenceRuntime(execSql);
86
- const { live, orphaned } = await partitionContainerDocumentTombstoneHolds(db, await listContainerDocumentTombstoneHoldsInTransaction(db, containerIds));
87
- if (orphaned.length > 0) {
88
- // Re-check under the lock: a hold re-created between the unlocked read
89
- // and this write is hiding a placement again and must survive.
90
- await runSerializedSqlMutation(execSql, (lockedExecSql) => getClientSQLitePersistenceRuntime(lockedExecSql).db.transaction(async (tx) => deleteContainerDocumentTombstoneHoldRows(tx, (await partitionContainerDocumentTombstoneHolds(tx, orphaned))
91
- .orphaned)));
92
- }
93
- return live
94
- .filter((hold) => isHoldDue(hold, now))
95
- .map((hold) => ({
96
- containerId: hold.containerId,
97
- documentId: hold.documentId,
98
- updatedAt: hold.tombstonedAt,
99
- }));
100
- }
101
- /** Ordinary-document floors must exclude unsigned container-metadata epochs. */
102
- export async function loadLocalDocumentAccessEpoch(execSql, documentId) {
103
- await ensureSqlTables(execSql, documentTables);
104
- const { db } = getClientSQLitePersistenceRuntime(execSql);
105
- const [row] = await db
106
- .select({ accessEpoch: max(documents.accessEpoch) })
107
- .from(documents)
108
- .where(and(eq(documents.appKind, DOCUMENTS_APP_KIND), eq(documents.documentId, documentId)));
109
- return row?.accessEpoch ?? 0;
110
- }
111
15
  //# sourceMappingURL=containerDocumentTombstoneHoldsPersistence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"containerDocumentTombstoneHoldsPersistence.js","sourceRoot":"","sources":["containerDocumentTombstoneHoldsPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,E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CAAC;SACnD,IAAI,CAAC,SAAS,CAAC;SACf,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,kBAAkB,CAAC,EACzC,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC,CACrC,CACF,CAAC;IACJ,OAAO,GAAG,EAAE,WAAW,IAAI,CAAC,CAAC;AAC/B,CAAC","sourcesContent":["import { and, eq, max } from \"drizzle-orm\";\nimport { DOCUMENTS_APP_KIND } from \"../../documents/documentConstants\";\nimport { containerDocumentTombstoneHolds } from \"../../sqlite/schema\";\nimport { documentContainerProjectionTables } from \"../../sqlite/schema/documentProjectionSchema\";\nimport {\n documents,\n documentTables,\n} from \"../../sqlite/schema/documentRecordSchema\";\nimport { remoteResetBatches } from \"../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../sqlite/sqlSchema\";\nimport {\n type ContainerDocumentPlacementKey,\n type ContainerDocumentTombstoneHoldRow,\n deleteContainerDocumentTombstoneHoldRows,\n type HeldContainerDocumentTombstone,\n holdContainerDocumentTombstonesInTransaction,\n listContainerDocumentTombstoneHoldsForDocumentsInTransaction,\n listContainerDocumentTombstoneHoldsInTransaction,\n listKnownContainerDocumentPlacementsInTransaction,\n partitionContainerDocumentTombstoneHolds,\n placementPairsMatch,\n} from \"./internal/containerDocumentTombstoneHolds\";\n\nexport {\n type ContainerDocumentPlacementKey,\n containerDocumentPlacementKey,\n deleteContainerDocumentTombstoneHoldRowsForLinks,\n deleteContainerDocumentTombstoneHoldsForContainers,\n deleteContainerDocumentTombstoneHoldsForDocuments,\n type HeldContainerDocumentTombstone,\n placementPairsMatch,\n} from \"./internal/containerDocumentTombstoneHolds\";\n\n/**\n * Retry backoff for a held tombstone: fifteen minutes after the first failed\n * verification, doubling on every further failure up to 32 hours. An\n * honest hold that can never verify (the requester lost read access to the\n * document, or it was purged elsewhere) would otherwise cost a head fetch on\n * every discovery of its container for as long as the container exists.\n */\nexport const HELD_TOMBSTONE_RETRY_INTERVAL_MS = 15 * 60 * 1000;\nconst HELD_TOMBSTONE_MAX_BACKOFF_DOUBLINGS = 7;\n\nexport function heldTombstoneRetryDelayMs(attempts: number): number {\n const doublings = Math.min(\n Math.max(attempts, 1) - 1,\n HELD_TOMBSTONE_MAX_BACKOFF_DOUBLINGS,\n );\n return HELD_TOMBSTONE_RETRY_INTERVAL_MS * 2 ** doublings;\n}\n\nfunction isHoldDue(\n hold: ContainerDocumentTombstoneHoldRow,\n now: Date,\n): boolean {\n return (\n Date.parse(hold.updatedAt) + heldTombstoneRetryDelayMs(hold.attempts) <=\n now.getTime()\n );\n}\n\nexport async function holdContainerDocumentTombstones(\n execSql: ExecSql,\n tombstones: ReadonlyArray<HeldContainerDocumentTombstone>,\n now = new Date().toISOString(),\n): Promise<void> {\n if (tombstones.length === 0) return;\n return runSerializedSqlMutation(execSql, async (lockedExecSql) => {\n await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);\n await db.transaction((tx) =>\n holdContainerDocumentTombstonesInTransaction(tx, tombstones, now),\n );\n });\n}\n\nexport async function refuteContainerDocumentTombstoneHolds(\n execSql: ExecSql,\n placements: ReadonlyArray<ContainerDocumentPlacementKey>,\n): Promise<void> {\n if (placements.length === 0) return;\n return runSerializedSqlMutation(execSql, async (lockedExecSql) => {\n await ensureSqlTables(lockedExecSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(lockedExecSql);\n await db.transaction(async (tx) => {\n for (const batch of remoteResetBatches(placements)) {\n await tx\n .update(containerDocumentTombstoneHolds)\n .set({ hidden: false })\n .where(\n placementPairsMatch(\n batch,\n containerDocumentTombstoneHolds.documentId,\n containerDocumentTombstoneHolds.containerId,\n ),\n )\n .run();\n }\n });\n });\n}\n\nexport async function listContainerDocumentTombstoneHolds(\n execSql: ExecSql,\n containerIds: ReadonlyArray<string>,\n): Promise<ContainerDocumentTombstoneHoldRow[]> {\n if (containerIds.length === 0) return [];\n await ensureSqlTables(execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n return listContainerDocumentTombstoneHoldsInTransaction(db, containerIds);\n}\n\nexport async function listContainerDocumentTombstoneHoldsForDocuments(\n execSql: ExecSql,\n documentIds: ReadonlyArray<string>,\n): Promise<ContainerDocumentPlacementKey[]> {\n if (documentIds.length === 0) return [];\n await ensureSqlTables(execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n return listContainerDocumentTombstoneHoldsForDocumentsInTransaction(\n db,\n documentIds,\n );\n}\n\nexport async function listKnownContainerDocumentPlacements(\n execSql: ExecSql,\n placements: ReadonlyArray<ContainerDocumentPlacementKey>,\n): Promise<ContainerDocumentPlacementKey[]> {\n if (placements.length === 0) return [];\n await ensureSqlTables(execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n return listKnownContainerDocumentPlacementsInTransaction(db, placements);\n}\n\n/**\n * The held tombstones on the given containers that are due for another\n * verification attempt. Holds whose placement is gone are pruned first; that\n * is the only write, and it is taken under the mutation lock only when there\n * is something to prune.\n */\nexport async function listRetryableHeldContainerDocumentTombstones(\n execSql: ExecSql,\n containerIds: ReadonlyArray<string>,\n now = new Date(),\n): Promise<HeldContainerDocumentTombstone[]> {\n if (containerIds.length === 0) return [];\n await ensureSqlTables(execSql, documentContainerProjectionTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const { live, orphaned } = await partitionContainerDocumentTombstoneHolds(\n db,\n await listContainerDocumentTombstoneHoldsInTransaction(db, containerIds),\n );\n if (orphaned.length > 0) {\n // Re-check under the lock: a hold re-created between the unlocked read\n // and this write is hiding a placement again and must survive.\n await runSerializedSqlMutation(execSql, (lockedExecSql) =>\n getClientSQLitePersistenceRuntime(lockedExecSql).db.transaction(\n async (tx) =>\n deleteContainerDocumentTombstoneHoldRows(\n tx,\n (await partitionContainerDocumentTombstoneHolds(tx, orphaned))\n .orphaned,\n ),\n ),\n );\n }\n return live\n .filter((hold) => isHoldDue(hold, now))\n .map((hold) => ({\n containerId: hold.containerId,\n documentId: hold.documentId,\n updatedAt: hold.tombstonedAt,\n }));\n}\n\n/** Ordinary-document floors must exclude unsigned container-metadata epochs. */\nexport async function loadLocalDocumentAccessEpoch(\n execSql: ExecSql,\n documentId: string,\n): Promise<number> {\n await ensureSqlTables(execSql, documentTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const [row] = await db\n .select({ accessEpoch: max(documents.accessEpoch) })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, DOCUMENTS_APP_KIND),\n eq(documents.documentId, documentId),\n ),\n );\n return row?.accessEpoch ?? 0;\n}\n"]}
1
+ {"version":3,"file":"containerDocumentTombstoneHoldsPersistence.js","sourceRoot":"","sources":["containerDocumentTombstoneHoldsPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,6BAA6B,EAC7B,gDAAgD,EAChD,kDAAkD,EAClD,iDAAiD,EAEjD,mBAAmB,GACpB,MAAM,4CAA4C,CAAC;AAEpD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAC/D,MAAM,oCAAoC,GAAG,CAAC,CAAC;AAE/C,MAAM,UAAU,yBAAyB,CAAC,QAAgB;IACxD,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CACxB,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,GAAG,CAAC,EACzB,oCAAoC,CACrC,CAAC;IACF,OAAO,gCAAgC,GAAG,CAAC,IAAI,SAAS,CAAC;AAC3D,CAAC","sourcesContent":["export {\n containerDocumentPlacementKey,\n deleteContainerDocumentTombstoneHoldRowsForLinks,\n deleteContainerDocumentTombstoneHoldsForContainers,\n deleteContainerDocumentTombstoneHoldsForDocuments,\n type HeldContainerDocumentTombstone,\n placementPairsMatch,\n} from \"./internal/containerDocumentTombstoneHolds\";\n\n/**\n * Retry backoff for a held tombstone: fifteen minutes after the first failed\n * verification, doubling on every further failure up to 32 hours. An\n * honest hold that can never verify (the requester lost read access to the\n * document, or it was purged elsewhere) would otherwise cost a head fetch on\n * every discovery of its container for as long as the container exists.\n */\nexport const HELD_TOMBSTONE_RETRY_INTERVAL_MS = 15 * 60 * 1000;\nconst HELD_TOMBSTONE_MAX_BACKOFF_DOUBLINGS = 7;\n\nexport function heldTombstoneRetryDelayMs(attempts: number): number {\n const doublings = Math.min(\n Math.max(attempts, 1) - 1,\n HELD_TOMBSTONE_MAX_BACKOFF_DOUBLINGS,\n );\n return HELD_TOMBSTONE_RETRY_INTERVAL_MS * 2 ** doublings;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tearleads/client-sdk",
3
- "version": "0.1.202",
3
+ "version": "0.1.204",
4
4
  "description": "React-free Tearleads client runtime: local SQLite persistence, keys, sync, encrypted blobs, and domain workflows.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -1,11 +1,11 @@
1
1
  import type { ProjectionUserKeyResolver } from "../../../data/keyingProjectionVerification/index.js";
2
2
  import type { ExecSql } from "../../../data/sqlite/sqlSchema.js";
3
- import { type DocumentLinkSetFailureHandler, type LinkSetAttachmentReuse, type RelinkRemoteDocumentResult, relinkRemoteDocument } from "../../documents/index.js";
3
+ import { type DocumentLinkSetFailureHandler, type LinkSetAttachmentReuse, type RelinkRemoteDocumentResult, RemoteDocumentService, type RemoteDocumentServiceApi } from "../../documents/index.js";
4
4
  import type { SubtreePurgeRoot } from "../../documents/purge/subtreePurgeScope.js";
5
5
  import { type PrincipalPolicyOperation } from "../../principals/runtimePolicyWarmer.js";
6
6
  import type { ContainerContentsWorkflowRuntime } from "../runtime.js";
7
- export type ContainerDocumentLinkApi = Parameters<typeof relinkRemoteDocument>[0]["apiClient"];
8
- type ContainerDocumentLinkOperation = Parameters<typeof relinkRemoteDocument>[0]["operation"];
7
+ export type ContainerDocumentLinkApi = RemoteDocumentServiceApi;
8
+ type ContainerDocumentLinkOperation = Parameters<RemoteDocumentService["relinkRemoteDocument"]>[0]["operation"];
9
9
  type RemoteDocumentPersistedState = RelinkRemoteDocumentResult["persistedState"];
10
10
  export interface ContainerDocumentLinkRuntime extends Pick<ContainerContentsWorkflowRuntime, "auth" | "crypto" | "infra" | "resolveTrustedUserIdentity" | "state" | "util"> {
11
11
  apiClient: ContainerDocumentLinkApi & Pick<ContainerContentsWorkflowRuntime["apiClient"], "getPrincipalPolicyPages" | "getProjectionPolicyHistoryPages">;
@@ -2,7 +2,7 @@ import { reportAndRethrowKeyingVerificationError } from "../../../data/keyingPro
2
2
  import { sqlDocumentContainerProjectionPersistence } from "../../../data/persistence/container-contents/documentContainerProjectionPersistence.js";
3
3
  import { errorMessage } from "../../../data/shared/errorMessage.js";
4
4
  import { isDatabaseUnavailableError } from "../../../data/sync/databaseUnavailable.js";
5
- import { relinkRemoteDocument, resolveDocumentCreateAuthor, } from "../../documents/index.js";
5
+ import { RemoteDocumentService, resolveDocumentCreateAuthor, } from "../../documents/index.js";
6
6
  import { createRuntimePrincipalPolicyWarmer, } from "../../principals/runtimePolicyWarmer.js";
7
7
  export async function initializeDocumentLinksSchema(execSql) {
8
8
  await sqlDocumentContainerProjectionPersistence.ensureSchema(execSql);
@@ -48,22 +48,24 @@ export async function relinkRemoteContainerDocument(input) {
48
48
  });
49
49
  return null;
50
50
  }
51
+ const remoteDocuments = new RemoteDocumentService({
52
+ api: runtime.apiClient,
53
+ encapsulationSecretKey: targetSecretKey,
54
+ execSql,
55
+ });
51
56
  try {
52
- const result = await relinkRemoteDocument({
57
+ const result = await remoteDocuments.relinkRemoteDocument({
53
58
  attachmentReuse: input.attachmentReuse,
54
59
  beforeSubmit: input.beforeSubmit,
55
- apiClient: runtime.apiClient,
56
60
  author,
57
61
  documentId,
58
62
  expectedSubtreeRoot: input.expectedSubtreeRoot,
59
- execSql,
60
63
  onFailure: input.onFailure,
61
64
  operation,
62
65
  resolveProjectionUserKey,
63
66
  rotationSnapshot: input.rotationSnapshot,
64
67
  stillCurrent: input.isCurrent,
65
68
  targetContainerId,
66
- targetSecretKey,
67
69
  warmReferencedPrincipalPolicies: createRuntimePrincipalPolicyWarmer(runtime, { operation: input.policyOperation }),
68
70
  });
69
71
  if (!result) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uCAAuC,EAAE,MAAM,kDAAkD,CAAC;AAC3G,OAAO,EAAE,yCAAyC,EAAE,MAAM,qFAAqF,CAAC;AAChJ,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAEjE,OAAO,EAAE,0BAA0B,EAAE,MAAM,wCAAwC,CAAC;AACpF,OAAO,EAIL,oBAAoB,EACpB,2BAA2B,GAC5B,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,kCAAkC,GAEnC,MAAM,sCAAsC,CAAC;AA+B9C,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,OAAgB;IAEhB,MAAM,yCAAyC,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;AACxE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,8BAA8B,CAClD,OAAgB,EAChB,UAAkB;IAElB,OAAO,yCAAyC,CAAC,sBAAsB,CACrE,OAAO,EACP,UAAU,CACX,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,OAAgB,EAChB,UAAkB,EAClB,YAAmC;IAEnC,MAAM,yCAAyC,CAAC,oBAAoB,CAClE,OAAO,EACP,UAAU,EACV,YAAY,CACb,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,iCAAiC,CACxC,IAA0C,EAC1C,KAAc;IAEd,uEAAuE;IACvE,8EAA8E;IAC9E,+EAA+E;I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OAAO,EAAE,CAC/I,CAAC;QACF,sEAAsE;QACtE,4EAA4E;QAC5E,oEAAoE;QACpE,iCAAiC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACvD,KAAK,CAAC,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAAC,KAanD;IACC,OAAO,6BAA6B,CAAC;QACnC,GAAG,KAAK;QACR,SAAS,EAAE,QAAQ;KACpB,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { ProjectionUserKeyResolver } from \"../../../data/keyingProjectionVerification\";\nimport { reportAndRethrowKeyingVerificationError } from \"../../../data/keyingProjectionVerification/error\";\nimport { sqlDocumentContainerProjectionPersistence } from \"../../../data/persistence/container-contents/documentContainerProjectionPersistence\";\nimport { errorMessage } from \"../../../data/shared/errorMessage\";\nimport type { ExecSql } from \"../../../data/sqlite/sqlSchema\";\nimport { isDatabaseUnavailableError } from \"../../../data/sync/databaseUnavailable\";\nimport {\n type DocumentLinkSetFailureHandler,\n type LinkSetAttachmentReuse,\n type RelinkRemoteDocumentResult,\n relinkRemoteDocument,\n resolveDocumentCreateAuthor,\n} from \"../../documents\";\nimport type { SubtreePurgeRoot } from \"../../documents/purge/subtreePurgeScope\";\nimport {\n createRuntimePrincipalPolicyWarmer,\n type PrincipalPolicyOperation,\n} from \"../../principals/runtimePolicyWarmer\";\nimport type { ContainerContentsWorkflowRuntime } from \"../runtime\";\n\nexport type ContainerDocumentLinkApi = Parameters<\n typeof relinkRemoteDocument\n>[0][\"apiClient\"];\ntype ContainerDocumentLinkOperation = Parameters<\n typeof relinkRemoteDocument\n>[0][\"operation\"];\ntype RemoteDocumentPersistedState =\n RelinkRemoteDocumentResult[\"persistedState\"];\n\nexport interface ContainerDocumentLinkRuntime\n extends Pick<\n ContainerContentsWorkflowRuntime,\n | \"auth\"\n | \"crypto\"\n | \"infra\"\n | \"resolveTrustedUserIdentity\"\n | \"state\"\n | \"util\"\n > {\n apiClient: ContainerDocumentLinkApi &\n Pick<\n ContainerContentsWorkflowRuntime[\"apiClient\"],\n \"getPrincipalPolicyPages\" | \"getProjectionPolicyHistoryPages\"\n >;\n}\n\nexport type { RemoteDocumentPersistedState };\n\nexport async function initializeDocumentLinksSchema(\n execSql: ExecSql,\n): Promise<void> {\n await sqlDocumentContainerProjectionPersistence.ensureSchema(execSql);\n}\n\nexport async function listDocumentLinkedContainerIds(\n execSql: ExecSql,\n documentId: string,\n): Promise<ReadonlyArray<string>> {\n return sqlDocumentContainerProjectionPersistence.listLinkedContainerIds(\n execSql,\n documentId,\n );\n}\n\nexport async function replaceDocumentLinks(\n execSql: ExecSql,\n documentId: string,\n containerIds: ReadonlyArray<string>,\n): Promise<void> {\n await sqlDocumentContainerProjectionPersistence.replaceDocumentLinks(\n execSql,\n documentId,\n containerIds,\n );\n}\n\n/**\n * Hand a link/unlink/move/purge failure to host diagnostics as a real `Error`.\n * The caller's string-only log line stays the fallback for a host without\n * `logError`, so this never logs a second copy of the same failure.\n */\nfunction reportDocumentLinkMutationFailure(\n util: ContainerDocumentLinkRuntime[\"util\"],\n error: unknown,\n): void {\n // `ApiClient` returns `RequestFailure` objects instead of throwing, so\n // offline and 403/404/409 already left through the `!result` branch: a status\n // filter here would be dead code. Only a database lost mid-flight stays quiet.\n if (!util.logError || isDatabaseUnavailableError(error)) return;\n try {\n // A host logger that throws or rejects must not replace the null result or\n // skip the failure handler below.\n void Promise.resolve(\n util.logError(\"Container contents: document link mutation failed\", error),\n ).catch(() => undefined);\n } catch {\n // Hosts may throw synchronously or return a rejected promise.\n }\n}\n\nexport async function relinkRemoteContainerDocument(input: {\n attachmentReuse?: LinkSetAttachmentReuse | undefined;\n beforeSubmit?: (() => Promise<boolean>) | undefined;\n documentId: string;\n expectedSubtreeRoot?: SubtreePurgeRoot | undefined;\n isCurrent?: (() => boolean) | undefined;\n noteId: string;\n onFailure?: DocumentLinkSetFailureHandler | undefined;\n operation: ContainerDocumentLinkOperation;\n /** A workflow root's token; each leg otherwise discovers on its own. */\n policyOperation?: PrincipalPolicyOperation | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n rotationSnapshot?: Uint8Array | undefined;\n runtime: ContainerDocumentLinkRuntime;\n targetContainerId: string;\n}): Promise<RelinkRemoteDocumentResult | null> {\n const {\n documentId,\n noteId,\n operation,\n resolveProjectionUserKey,\n runtime,\n targetContainerId,\n } = input;\n const author = resolveDocumentCreateAuthor(runtime);\n const targetSecretKey = runtime.crypto.encapsulationKeyPair?.secretKey;\n const execSql = runtime.infra.execSql;\n if (input.isCurrent?.() === false) return null;\n if (!author || !targetSecretKey) {\n runtime.util.log(\n \"Container contents: document mutation skipped because the local key context is unavailable\",\n );\n input.onFailure?.({\n message: \"the local key context is unavailable\",\n status: null,\n });\n return null;\n }\n\n try {\n const result = await relinkRemoteDocument({\n attachmentReuse: input.attachmentReuse,\n beforeSubmit: input.beforeSubmit,\n apiClient: runtime.apiClient,\n author,\n documentId,\n expectedSubtreeRoot: input.expectedSubtreeRoot,\n execSql,\n onFailure: input.onFailure,\n operation,\n resolveProjectionUserKey,\n rotationSnapshot: input.rotationSnapshot,\n stillCurrent: input.isCurrent,\n targetContainerId,\n targetSecretKey,\n warmReferencedPrincipalPolicies: createRuntimePrincipalPolicyWarmer(\n runtime,\n { operation: input.policyOperation },\n ),\n });\n if (!result) {\n runtime.util.log(\n `Container contents: failed to ${operation} note ${noteId} ${operation === \"link\" ? \"to\" : \"from\"} container ${targetContainerId}`,\n );\n return null;\n }\n if (input.isCurrent?.() === false) return null;\n\n await sqlDocumentContainerProjectionPersistence.replaceDocumentLinks(\n execSql,\n documentId,\n result.linkedContainerIds,\n { stillCurrent: input.isCurrent },\n );\n\n return result;\n } catch (error) {\n await reportAndRethrowKeyingVerificationError(\n error,\n runtime.util.reportSecurityIncident,\n {\n objectId: documentId,\n objectKind: \"document\",\n operation: `document.${operation}`,\n },\n );\n const message = errorMessage(error);\n runtime.util.log(\n `Container contents: failed to ${operation} note ${noteId} ${operation === \"link\" ? \"to\" : \"from\"} container ${targetContainerId}: ${message}`,\n );\n // After the keying rethrow above, so a verification failure keeps its\n // security incident instead of being reported twice, and before the failure\n // handler, so a throwing caller callback cannot swallow the report.\n reportDocumentLinkMutationFailure(runtime.util, error);\n input.onFailure?.({ message, status: null });\n return null;\n }\n}\n\nexport async function unlinkRemoteContainerDocument(input: {\n attachmentReuse?: LinkSetAttachmentReuse | undefined;\n beforeSubmit?: (() => Promise<boolean>) | undefined;\n documentId: string;\n expectedSubtreeRoot?: SubtreePurgeRoot | undefined;\n isCurrent?: (() => boolean) | undefined;\n noteId: string;\n onFailure?: DocumentLinkSetFailureHandler | undefined;\n policyOperation?: PrincipalPolicyOperation | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n rotationSnapshot: Uint8Array;\n runtime: ContainerDocumentLinkRuntime;\n targetContainerId: string;\n}): Promise<RelinkRemoteDocumentResult | null> {\n return relinkRemoteContainerDocument({\n ...input,\n operation: \"unlink\",\n });\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uCAAuC,EAAE,MAAM,kDAAkD,CAAC;AAC3G,OAAO,EAAE,yCAAyC,EAAE,MAAM,qFAAqF,CAAC;AAChJ,OAAO,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAEjE,OAAO,EAAE,0BAA0B,EAAE,MAAM,wCAAwC,CAAC;AACpF,OAAO,EAIL,qBAAqB,EAErB,2BAA2B,GAC5B,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACL,kCAAkC,GAEnC,MAAM,sCAAsC,CAAC;AA6B9C,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,OAAgB;IAEhB,MAAM,yCAAyC,CAAC,YAAY,CAAC,OAAO,CAAC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AC,kBAAkB,EACzB,EAAE,YAAY,EAAE,KAAK,CAAC,SAAS,EAAE,CAClC,CAAC;QAEF,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,uCAAuC,CAC3C,KAAK,EACL,OAAO,CAAC,IAAI,CAAC,sBAAsB,EACnC;YACE,QAAQ,EAAE,UAAU;YACpB,UAAU,EAAE,UAAU;YACtB,SAAS,EAAE,YAAY,SAAS,EAAE;SACnC,CACF,CAAC;QACF,MAAM,OAAO,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;QACpC,OAAO,CAAC,IAAI,CAAC,GAAG,CACd,iCAAiC,SAAS,SAAS,MAAM,IAAI,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,cAAc,iBAAiB,KAAK,OAAO,EAAE,CAC/I,CAAC;QACF,sEAAsE;QACtE,4EAA4E;QAC5E,oEAAoE;QACpE,iCAAiC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACvD,KAAK,CAAC,SAAS,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7C,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAAC,KAanD;IACC,OAAO,6BAA6B,CAAC;QACnC,GAAG,KAAK;QACR,SAAS,EAAE,QAAQ;KACpB,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { ProjectionUserKeyResolver } from \"../../../data/keyingProjectionVerification\";\nimport { reportAndRethrowKeyingVerificationError } from \"../../../data/keyingProjectionVerification/error\";\nimport { sqlDocumentContainerProjectionPersistence } from \"../../../data/persistence/container-contents/documentContainerProjectionPersistence\";\nimport { errorMessage } from \"../../../data/shared/errorMessage\";\nimport type { ExecSql } from \"../../../data/sqlite/sqlSchema\";\nimport { isDatabaseUnavailableError } from \"../../../data/sync/databaseUnavailable\";\nimport {\n type DocumentLinkSetFailureHandler,\n type LinkSetAttachmentReuse,\n type RelinkRemoteDocumentResult,\n RemoteDocumentService,\n type RemoteDocumentServiceApi,\n resolveDocumentCreateAuthor,\n} from \"../../documents\";\nimport type { SubtreePurgeRoot } from \"../../documents/purge/subtreePurgeScope\";\nimport {\n createRuntimePrincipalPolicyWarmer,\n type PrincipalPolicyOperation,\n} from \"../../principals/runtimePolicyWarmer\";\nimport type { ContainerContentsWorkflowRuntime } from \"../runtime\";\n\nexport type ContainerDocumentLinkApi = RemoteDocumentServiceApi;\ntype ContainerDocumentLinkOperation = Parameters<\n RemoteDocumentService[\"relinkRemoteDocument\"]\n>[0][\"operation\"];\ntype RemoteDocumentPersistedState =\n RelinkRemoteDocumentResult[\"persistedState\"];\n\nexport interface ContainerDocumentLinkRuntime\n extends Pick<\n ContainerContentsWorkflowRuntime,\n | \"auth\"\n | \"crypto\"\n | \"infra\"\n | \"resolveTrustedUserIdentity\"\n | \"state\"\n | \"util\"\n > {\n apiClient: ContainerDocumentLinkApi &\n Pick<\n ContainerContentsWorkflowRuntime[\"apiClient\"],\n \"getPrincipalPolicyPages\" | \"getProjectionPolicyHistoryPages\"\n >;\n}\n\nexport type { RemoteDocumentPersistedState };\n\nexport async function initializeDocumentLinksSchema(\n execSql: ExecSql,\n): Promise<void> {\n await sqlDocumentContainerProjectionPersistence.ensureSchema(execSql);\n}\n\nexport async function listDocumentLinkedContainerIds(\n execSql: ExecSql,\n documentId: string,\n): Promise<ReadonlyArray<string>> {\n return sqlDocumentContainerProjectionPersistence.listLinkedContainerIds(\n execSql,\n documentId,\n );\n}\n\nexport async function replaceDocumentLinks(\n execSql: ExecSql,\n documentId: string,\n containerIds: ReadonlyArray<string>,\n): Promise<void> {\n await sqlDocumentContainerProjectionPersistence.replaceDocumentLinks(\n execSql,\n documentId,\n containerIds,\n );\n}\n\n/**\n * Hand a link/unlink/move/purge failure to host diagnostics as a real `Error`.\n * The caller's string-only log line stays the fallback for a host without\n * `logError`, so this never logs a second copy of the same failure.\n */\nfunction reportDocumentLinkMutationFailure(\n util: ContainerDocumentLinkRuntime[\"util\"],\n error: unknown,\n): void {\n // `ApiClient` returns `RequestFailure` objects instead of throwing, so\n // offline and 403/404/409 already left through the `!result` branch: a status\n // filter here would be dead code. Only a database lost mid-flight stays quiet.\n if (!util.logError || isDatabaseUnavailableError(error)) return;\n try {\n // A host logger that throws or rejects must not replace the null result or\n // skip the failure handler below.\n void Promise.resolve(\n util.logError(\"Container contents: document link mutation failed\", error),\n ).catch(() => undefined);\n } catch {\n // Hosts may throw synchronously or return a rejected promise.\n }\n}\n\nexport async function relinkRemoteContainerDocument(input: {\n attachmentReuse?: LinkSetAttachmentReuse | undefined;\n beforeSubmit?: (() => Promise<boolean>) | undefined;\n documentId: string;\n expectedSubtreeRoot?: SubtreePurgeRoot | undefined;\n isCurrent?: (() => boolean) | undefined;\n noteId: string;\n onFailure?: DocumentLinkSetFailureHandler | undefined;\n operation: ContainerDocumentLinkOperation;\n /** A workflow root's token; each leg otherwise discovers on its own. */\n policyOperation?: PrincipalPolicyOperation | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n rotationSnapshot?: Uint8Array | undefined;\n runtime: ContainerDocumentLinkRuntime;\n targetContainerId: string;\n}): Promise<RelinkRemoteDocumentResult | null> {\n const {\n documentId,\n noteId,\n operation,\n resolveProjectionUserKey,\n runtime,\n targetContainerId,\n } = input;\n const author = resolveDocumentCreateAuthor(runtime);\n const targetSecretKey = runtime.crypto.encapsulationKeyPair?.secretKey;\n const execSql = runtime.infra.execSql;\n if (input.isCurrent?.() === false) return null;\n if (!author || !targetSecretKey) {\n runtime.util.log(\n \"Container contents: document mutation skipped because the local key context is unavailable\",\n );\n input.onFailure?.({\n message: \"the local key context is unavailable\",\n status: null,\n });\n return null;\n }\n const remoteDocuments = new RemoteDocumentService({\n api: runtime.apiClient,\n encapsulationSecretKey: targetSecretKey,\n execSql,\n });\n\n try {\n const result = await remoteDocuments.relinkRemoteDocument({\n attachmentReuse: input.attachmentReuse,\n beforeSubmit: input.beforeSubmit,\n author,\n documentId,\n expectedSubtreeRoot: input.expectedSubtreeRoot,\n onFailure: input.onFailure,\n operation,\n resolveProjectionUserKey,\n rotationSnapshot: input.rotationSnapshot,\n stillCurrent: input.isCurrent,\n targetContainerId,\n warmReferencedPrincipalPolicies: createRuntimePrincipalPolicyWarmer(\n runtime,\n { operation: input.policyOperation },\n ),\n });\n if (!result) {\n runtime.util.log(\n `Container contents: failed to ${operation} note ${noteId} ${operation === \"link\" ? \"to\" : \"from\"} container ${targetContainerId}`,\n );\n return null;\n }\n if (input.isCurrent?.() === false) return null;\n\n await sqlDocumentContainerProjectionPersistence.replaceDocumentLinks(\n execSql,\n documentId,\n result.linkedContainerIds,\n { stillCurrent: input.isCurrent },\n );\n\n return result;\n } catch (error) {\n await reportAndRethrowKeyingVerificationError(\n error,\n runtime.util.reportSecurityIncident,\n {\n objectId: documentId,\n objectKind: \"document\",\n operation: `document.${operation}`,\n },\n );\n const message = errorMessage(error);\n runtime.util.log(\n `Container contents: failed to ${operation} note ${noteId} ${operation === \"link\" ? \"to\" : \"from\"} container ${targetContainerId}: ${message}`,\n );\n // After the keying rethrow above, so a verification failure keeps its\n // security incident instead of being reported twice, and before the failure\n // handler, so a throwing caller callback cannot swallow the report.\n reportDocumentLinkMutationFailure(runtime.util, error);\n input.onFailure?.({ message, status: null });\n return null;\n }\n}\n\nexport async function unlinkRemoteContainerDocument(input: {\n attachmentReuse?: LinkSetAttachmentReuse | undefined;\n beforeSubmit?: (() => Promise<boolean>) | undefined;\n documentId: string;\n expectedSubtreeRoot?: SubtreePurgeRoot | undefined;\n isCurrent?: (() => boolean) | undefined;\n noteId: string;\n onFailure?: DocumentLinkSetFailureHandler | undefined;\n policyOperation?: PrincipalPolicyOperation | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n rotationSnapshot: Uint8Array;\n runtime: ContainerDocumentLinkRuntime;\n targetContainerId: string;\n}): Promise<RelinkRemoteDocumentResult | null> {\n return relinkRemoteContainerDocument({\n ...input,\n operation: \"unlink\",\n });\n}\n"]}
@@ -1,5 +1,6 @@
1
1
  import { sqlDocumentContainerProjectionPersistence } from "../../../data/persistence/container-contents/documentContainerProjectionPersistence.js";
2
- import { containerDocumentPlacementKey, listContainerDocumentTombstoneHoldsForDocuments, } from "../../../data/persistence/documents/containerDocumentTombstoneHoldsPersistence.js";
2
+ import { ContainerDocumentTombstoneHoldRepository } from "../../../data/persistence/documents/containerDocumentTombstoneHoldRepository.js";
3
+ import { containerDocumentPlacementKey } from "../../../data/persistence/documents/containerDocumentTombstoneHoldsPersistence.js";
3
4
  import { sqlDocumentsPersistence } from "../../../data/persistence/documents/documentsPersistence.js";
4
5
  import { compareContainerContentsDocumentSummaries } from "./rows.js";
5
6
  import { listContainerContentsSqlIdBatches } from "./sql.js";
@@ -70,7 +71,7 @@ export async function listContainerContentsDocumentsForContainers(execSql, conta
70
71
  sortDocumentSummaries: options.sortDocumentSummaries,
71
72
  });
72
73
  const documentIds = Array.from(new Set(documentSummaries.flatMap((documentSummary) => documentSummary.documentId ? [documentSummary.documentId] : [])));
73
- const linkedContainerIdsByDocumentId = withoutHeldPlacements(await sqlDocumentContainerProjectionPersistence.listLinkedContainerIdsByDocumentIds(execSql, documentIds), await listContainerDocumentTombstoneHoldsForDocuments(execSql, documentIds));
74
+ const linkedContainerIdsByDocumentId = withoutHeldPlacements(await sqlDocumentContainerProjectionPersistence.listLinkedContainerIdsByDocumentIds(execSql, documentIds), await new ContainerDocumentTombstoneHoldRepository(execSql).listContainerDocumentTombstoneHoldsForDocuments(documentIds));
74
75
  // An earlier link-id read can race a move. Recheck membership against the
75
76
  // summaries and final link read so a fresh trash summary cannot ride stale
76
77
  // root ids into the root view. Notification fences reject older summaries.