@tearleads/client-sdk 0.1.252 → 0.1.254
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Tearleads.js +3 -3
- package/Tearleads.js.map +1 -1
- package/client/containerContents/documentDiscovery.js +0 -2
- package/client/containerContents/documentDiscovery.js.map +1 -1
- package/client/containerContents/index.d.ts +2 -0
- package/client/containerContents/index.js +4 -9
- package/client/containerContents/index.js.map +1 -1
- package/client/containerContents/storeRuntime.d.ts +17 -0
- package/client/containerContents/storeRuntime.js +31 -0
- package/client/containerContents/storeRuntime.js.map +1 -0
- package/client/deviceFirst/index.d.ts +2 -3
- package/client/deviceFirst/index.js +44 -22
- package/client/deviceFirst/index.js.map +1 -1
- package/client/runtime/workflowRuntime.d.ts +2 -0
- package/client/runtime/workflowRuntime.js +13 -6
- package/client/runtime/workflowRuntime.js.map +1 -1
- package/data/persistence/container-contents/containerContentsPersistenceTypes.d.ts +7 -10
- package/data/persistence/container-contents/containerContentsPersistenceTypes.js.map +1 -1
- package/data/persistence/container-contents/containerDeletionPersistence.js +5 -1
- package/data/persistence/container-contents/containerDeletionPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerHydrationPersistence.js +1 -1
- package/data/persistence/container-contents/containerHydrationPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js +11 -4
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerRemovalMetadata.js +47 -0
- package/data/persistence/container-contents/containerRemovalMetadata.js.map +1 -0
- package/data/persistence/container-contents/containerRemovalPlacements.js +22 -0
- package/data/persistence/container-contents/containerRemovalPlacements.js.map +1 -0
- package/data/persistence/container-contents/containerSyncWatermarkPersistence.d.ts +2 -1
- package/data/persistence/container-contents/containerSyncWatermarkPersistence.js +10 -2
- package/data/persistence/container-contents/containerSyncWatermarkPersistence.js.map +1 -1
- package/data/persistence/documents/documentDiscoveryCandidate.d.ts +1 -0
- package/data/persistence/documents/documentDiscoveryCandidate.js +3 -1
- package/data/persistence/documents/documentDiscoveryCandidate.js.map +1 -1
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.d.ts +1 -1
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.js +16 -4
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.js.map +1 -1
- package/data/sqlite/migrations/localSchemaMigrations.js +11 -0
- package/data/sqlite/migrations/localSchemaMigrations.js.map +1 -1
- package/data/sqlite/schema/containerSyncWatermarkSchema.js +6 -3
- package/data/sqlite/schema/containerSyncWatermarkSchema.js.map +1 -1
- package/data/sqlite/schema/index.d.ts +17 -0
- package/data/sync/syncCoordinator.js +17 -10
- package/data/sync/syncCoordinator.js.map +1 -1
- package/internal/api-client/ApiClient.d.ts +1 -0
- package/internal/api-client/client/apiClientContext.d.ts +1 -0
- package/internal/api-client/client/containerReads.d.ts +1 -0
- package/internal/api-client/requestInternals.js +14 -6
- package/internal/api-client/requestInternals.js.map +1 -1
- package/internal/validators/operation/containerReads.d.ts +29 -2
- package/internal/validators/operation/containerReads.js +2 -1
- package/internal/validators/operation/containerReads.js.map +1 -1
- package/internal/validators/request/containerDocumentLanes.d.ts +7 -0
- package/internal/validators/request/containerDocumentLanes.js +6 -0
- package/internal/validators/request/containerDocumentLanes.js.map +1 -1
- package/internal/validators/response/container.d.ts +8 -0
- package/internal/validators/response/container.js +1 -0
- package/internal/validators/response/container.js.map +1 -1
- package/internal/validators/response/containerParentLanes.d.ts +2 -0
- package/package.json +1 -1
- package/stores/container-contents/containerContentsStore.d.ts +3 -9
- package/stores/container-contents/containerContentsStore.js +24 -21
- package/stores/container-contents/containerContentsStore.js.map +1 -1
- package/stores/container-contents/containerState/storeScope.js +6 -0
- package/stores/container-contents/containerState/storeScope.js.map +1 -0
- package/stores/container-contents/hydration/backgroundHydration.js +2 -1
- package/stores/container-contents/hydration/backgroundHydration.js.map +1 -1
- package/stores/container-contents/hydration/reconnectRefresh.d.ts +10 -0
- package/stores/container-contents/hydration/reconnectRefresh.js +68 -0
- package/stores/container-contents/hydration/reconnectRefresh.js.map +1 -0
- package/stores/container-contents/hydration/reconnectRefreshMachine.js +49 -0
- package/stores/container-contents/hydration/reconnectRefreshMachine.js.map +1 -0
- package/stores/container-contents/hydration/remoteHydrationRefresh.js +4 -1
- package/stores/container-contents/hydration/remoteHydrationRefresh.js.map +1 -1
- package/stores/container-contents/hydration/remoteHydrationRequest.js +79 -10
- package/stores/container-contents/hydration/remoteHydrationRequest.js.map +1 -1
- package/stores/container-contents/hydration/startupHydration.js +2 -9
- package/stores/container-contents/hydration/startupHydration.js.map +1 -1
- package/stores/container-contents/hydration/treeResetMachine.js +61 -0
- package/stores/container-contents/hydration/treeResetMachine.js.map +1 -0
- package/stores/container-contents/localRefresh.js +28 -12
- package/stores/container-contents/localRefresh.js.map +1 -1
- package/stores/container-contents/remoteEvents/remoteContainerIngestion.js +4 -1
- package/stores/container-contents/remoteEvents/remoteContainerIngestion.js.map +1 -1
- package/stores/container-contents/remoteWrites/writeGeneration.js +5 -2
- package/stores/container-contents/remoteWrites/writeGeneration.js.map +1 -1
- package/stores/container-contents/state.d.ts +3 -7
- package/stores/container-contents/state.js +27 -16
- package/stores/container-contents/state.js.map +1 -1
- package/stores/container-contents/syncAgent/index.d.ts +7 -2
- package/stores/container-contents/syncAgent/index.js +77 -49
- package/stores/container-contents/syncAgent/index.js.map +1 -1
- package/stores/container-contents/syncAgent/syncAgentTypes.d.ts +5 -2
- package/stores/container-contents/syncAgent/syncAgentTypes.js.map +1 -1
- package/stores/container-contents/syncAgent/syncLaneIteration.js +6 -3
- package/stores/container-contents/syncAgent/syncLaneIteration.js.map +1 -1
- package/stores/local-projection/localProjectionStore.js +24 -15
- package/stores/local-projection/localProjectionStore.js.map +1 -1
- package/stores/local-projection/registry.js +2 -2
- package/stores/local-projection/registry.js.map +1 -1
- package/workflows/container-contents/container-state/create/createChild.js +3 -15
- package/workflows/container-contents/container-state/create/createChild.js.map +1 -1
- package/workflows/container-contents/containerPersistence.d.ts +4 -8
- package/workflows/container-contents/containerPersistence.js +22 -12
- package/workflows/container-contents/containerPersistence.js.map +1 -1
- package/workflows/container-contents/documentDiscovery/containerDocumentListing.js +52 -4
- package/workflows/container-contents/documentDiscovery/containerDocumentListing.js.map +1 -1
- package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js +4 -2
- package/workflows/container-contents/documentDiscovery/documentDiscoveryApply.js.map +1 -1
- package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js +17 -12
- package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js.map +1 -1
- package/workflows/container-contents/documentDiscovery/documentDiscoveryTypes.js.map +1 -1
- package/workflows/container-contents/documentDiscovery/publicationWatermark.js +15 -0
- package/workflows/container-contents/documentDiscovery/publicationWatermark.js.map +1 -0
- package/workflows/container-contents/localState.js +3 -0
- package/workflows/container-contents/localState.js.map +1 -1
- package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.d.ts +12 -0
- package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js +93 -0
- package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.js.map +1 -0
- package/workflows/container-contents/remoteHydration/index.d.ts +3 -11
- package/workflows/container-contents/remoteHydration/index.js +17 -66
- package/workflows/container-contents/remoteHydration/index.js.map +1 -1
- package/workflows/container-contents/remoteHydration/lanes/parentLaneFetch.js +3 -2
- package/workflows/container-contents/remoteHydration/lanes/parentLaneFetch.js.map +1 -1
- package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js +25 -0
- package/workflows/container-contents/remoteHydration/lanes/restorationReceipt.js.map +1 -0
- package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js +107 -0
- package/workflows/container-contents/remoteHydration/lanes/tombstoneAccess.js.map +1 -0
- package/workflows/container-contents/remoteHydration/principalPolicyCache.js +57 -5
- package/workflows/container-contents/remoteHydration/principalPolicyCache.js.map +1 -1
- package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js +129 -84
- package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js.map +1 -1
- package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js +32 -10
- package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js.map +1 -1
- package/workflows/container-contents/remoteHydration/tombstoneApplication.js +67 -24
- package/workflows/container-contents/remoteHydration/tombstoneApplication.js.map +1 -1
- package/workflows/container-contents/remoteHydration/types.d.ts +16 -1
- package/workflows/container-contents/remoteHydration/types.js.map +1 -1
- package/workflows/container-contents/remoteReset/remoteResetCursorScope.js +3 -3
- package/workflows/container-contents/remoteReset/remoteResetCursorScope.js.map +1 -1
- package/workflows/container-contents/runtime.d.ts +3 -0
- package/workflows/container-contents/runtime.js.map +1 -1
- package/workflows/container-contents/storedContainerState.js +17 -7
- package/workflows/container-contents/storedContainerState.js.map +1 -1
- package/workflows/container-contents/syncLane.d.ts +1 -0
- package/workflows/container-contents/syncLane.js +9 -1
- package/workflows/container-contents/syncLane.js.map +1 -1
- package/workflows/container-contents/syncPause/discoveryGuard.js +6 -2
- package/workflows/container-contents/syncPause/discoveryGuard.js.map +1 -1
- package/workflows/container-contents/tombstones/documentTombstoneGate.js +6 -3
- package/workflows/container-contents/tombstones/documentTombstoneGate.js.map +1 -1
- package/workflows/shared/runtimeInput.d.ts +2 -0
- package/workflows/shared/runtimeInput.js.map +1 -1
- package/workflows/container-contents/remoteHydration/completeHydration.js +0 -25
- package/workflows/container-contents/remoteHydration/completeHydration.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { bytesToBase64 } from "../../../../internal/encoding/index.js";
|
|
2
2
|
import { createPendingUpdateFields } from "../../../../data/documents/documentSync.js";
|
|
3
3
|
import { deriveStableUuidV4Shaped } from "../../../../data/keying/stableUuid.js";
|
|
4
|
-
import {
|
|
4
|
+
import { resolveAtomicContainerSave } from "../../containerPersistence.js";
|
|
5
5
|
import { createInitializedContainerMetadataDocument } from "../../metadata/containerMetadataDocument.js";
|
|
6
6
|
import { loadContainerWriterProjectionForState } from "../projectionCache.js";
|
|
7
7
|
import { createRemoteContainer } from "../remote.js";
|
|
@@ -122,18 +122,6 @@ function createInitialChildContainerDocumentRecord(input) {
|
|
|
122
122
|
snapshotEndVersion: "",
|
|
123
123
|
};
|
|
124
124
|
}
|
|
125
|
-
function resolveAtomicChildContainerSave(persistence) {
|
|
126
|
-
const saveContainerWithPendingUpdate = persistence.saveContainerWithPendingUpdate;
|
|
127
|
-
if (!saveContainerWithPendingUpdate)
|
|
128
|
-
return null;
|
|
129
|
-
const inheritedSqlAtomicSave = saveContainerWithPendingUpdate ===
|
|
130
|
-
defaultContainerContentsPersistence.saveContainerWithPendingUpdate &&
|
|
131
|
-
(persistence.saveContainer !==
|
|
132
|
-
defaultContainerContentsPersistence.saveContainer ||
|
|
133
|
-
persistence.enqueuePendingUpdate !==
|
|
134
|
-
defaultContainerContentsPersistence.enqueuePendingUpdate);
|
|
135
|
-
return inheritedSqlAtomicSave ? null : saveContainerWithPendingUpdate;
|
|
136
|
-
}
|
|
137
125
|
function childContainerSaveOptions(input) {
|
|
138
126
|
const { createIntent, localUpdatedAt, stillCurrent } = input;
|
|
139
127
|
return createIntent || localUpdatedAt || stillCurrent
|
|
@@ -164,7 +152,7 @@ async function persistCreatedChildContainer(input) {
|
|
|
164
152
|
? createPendingUpdateFields(input.initialUpdate)
|
|
165
153
|
: null;
|
|
166
154
|
if (pendingUpdate) {
|
|
167
|
-
const saveContainerWithPendingUpdate =
|
|
155
|
+
const saveContainerWithPendingUpdate = resolveAtomicContainerSave(persistence);
|
|
168
156
|
if (!saveContainerWithPendingUpdate)
|
|
169
157
|
return null;
|
|
170
158
|
containerState.container = await saveContainerWithPendingUpdate(runtime.infra.execSql, containerState.container, containerState.record, { ...saveOptions, pendingUpdate });
|
|
@@ -196,7 +184,7 @@ export async function createChildContainerState(input) {
|
|
|
196
184
|
// Refuse before any remote create when an adapter cannot provide
|
|
197
185
|
// that atomic operation; a SQLite transaction cannot roll back adapter-owned
|
|
198
186
|
// side effects.
|
|
199
|
-
if (queueRemoteSync && !
|
|
187
|
+
if (queueRemoteSync && !resolveAtomicContainerSave(persistence)) {
|
|
200
188
|
return null;
|
|
201
189
|
}
|
|
202
190
|
const remoteChild = createRemote
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createChild.js","sourceRoot":"","sources":["createChild.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AACpF,OAAO,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAM9E,OAAO,EAAE,mCAAmC,EAAE,MAAM,4BAA4B,CAAC;AACjF,OAAO,EAAE,0CAA0C,EAAE,MAAM,0CAA0C,CAAC;AAEtG,OAAO,EAAE,qCAAqC,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAMlD,OAAO,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AAEnE,MAAM,mCAAmC,GAAG,IAAI,UAAU,CAAC;IACzD,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IAC5E,IAAI,EAAE,IAAI,EAAE,IAAI;CACjB,CAAC,CAAC;AAEH;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAGvC;IACC,OAAO,wBAAwB,CAC7B,mCAAmC,EACnC,GAAG,KAAK,CAAC,iBAAiB,SAAS,KAAK,CAAC,UAAU,EAAE,CACtD,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,sBAAsB,CACnC,iBA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AC,WAAW,CAAC,EAAE,CAAC;QACrE,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,WAAW,GAAG,YAAY;QAC9B,CAAC,CAAC,MAAM,+CAA+C,CAAC;YACpD,OAAO;YACP,IAAI;YACJ,UAAU;YACV,GAAG;YACH,aAAa;YACb,aAAa,EAAE,eAAe,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI;YACrD,gCAAgC,EAC9B,KAAK,CAAC,gCAAgC;YACxC,WAAW;YACX,wBAAwB;YACxB,OAAO;YACP,YAAY,EAAE,KAAK,CAAC,YAAY;YAChC,WAAW;SACZ,CAAC;QACJ,CAAC,CAAC,IAAI,CAAC;IACT,MAAM,cAAc,GAClB,WAAW,EAAE,cAAc;QAC3B,8CAA8C,CAAC;YAC7C,OAAO;YACP,IAAI;YACJ,UAAU;YACV,GAAG;YACH,aAAa;YACb,WAAW;YACX,WAAW;SACZ,CAAC,CAAC;IACL,MAAM,iBAAiB,GAAG,MAAM,4BAA4B,CAAC;QAC3D,cAAc;QACd,aAAa;QACb,qEAAqE;QACrE,sCAAsC;QACtC,cAAc,EAAE,WAAW,EAAE,eAAe,IAAI,IAAI;QACpD,WAAW;QACX,eAAe,EAAE,eAAe,IAAI,CAAC,WAAW,EAAE,eAAe;QACjE,OAAO;QACP,YAAY,EAAE,KAAK,CAAC,YAAY;KACjC,CAAC,CAAC;IACH,IAAI,iBAAiB,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAE5C,OAAO;QACL,cAAc;QACd,aAAa;QACb,iBAAiB;KAClB,CAAC;AACJ,CAAC","sourcesContent":["import { bytesToBase64 } from \"@tearleads/encoding\";\nimport type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport { createPendingUpdateFields } from \"../../../../data/documents/documentSync\";\nimport { deriveStableUuidV4Shaped } from \"../../../../data/keying/stableUuid\";\nimport type { ProjectionUserKeyResolver } from \"../../../../data/keyingProjectionVerification\";\nimport type {\n ContainerContentsPersistence,\n ContainerDocumentRecord as DocumentRecord,\n} from \"../../containerPersistence\";\nimport { defaultContainerContentsPersistence } from \"../../containerPersistence\";\nimport { createInitializedContainerMetadataDocument } from \"../../metadata/containerMetadataDocument\";\nimport type { ContainerState } from \"../../remoteHydration\";\nimport { loadContainerWriterProjectionForState } from \"../projectionCache\";\nimport { createRemoteContainer } from \"../remote\";\nimport type {\n ContainerMetadataDocumentState,\n ContainerWorkflowRuntime,\n CreatedChildContainerState,\n} from \"../types\";\nimport { withRegisteredMetadataSeed } from \"./createIntentSeed\";\nimport { CONTAINER_ALREADY_COMMITTED } from \"./createWithMetadata\";\n\nconst SYSTEM_CONTAINER_ID_NAMESPACE_BYTES = new Uint8Array([\n 0xc5, 0x5e, 0x8f, 0x2e, 0x3d, 0x82, 0x4b, 0x4d, 0x8d, 0xbd, 0x0d, 0xd2, 0xd3,\n 0x60, 0xb3, 0x6d,\n]);\n\n/**\n * Derives a stable, UUID-shaped container id for a built-in system slot.\n *\n * The namespace digest makes each slot idempotent across stale store snapshots.\n * The UUID version and variant bits are normalized to a v4 shape because remote\n * container APIs currently validate container ids with the same v4 UUID contract\n * they use for random user-created containers.\n */\nexport function deriveSystemContainerId(input: {\n parentContainerId: string;\n systemSlot: ContainerSystemSlot;\n}): Promise<string> {\n return deriveStableUuidV4Shaped(\n SYSTEM_CONTAINER_ID_NAMESPACE_BYTES,\n `${input.parentContainerId}\\u0000${input.systemSlot}`,\n );\n}\n\nasync function createChildContainerId(\n parentContainerId: string,\n systemSlot: ContainerSystemSlot | null | undefined,\n): Promise<string> {\n return systemSlot\n ? deriveSystemContainerId({ parentContainerId, systemSlot })\n : crypto.randomUUID();\n}\n\nasync function buildRemoteContainerContentsChildContainerState(input: {\n childId: string;\n icon: string | null;\n systemSlot?: ContainerSystemSlot | null | undefined;\n doc: ContainerMetadataDocumentState;\n initialRecord: DocumentRecord;\n /** Present when the first metadata update should commit with the create. */\n initialUpdate: Uint8Array | null;\n locallyAcceptedMetadataUpdateIds?: Set<string> | undefined;\n parentState: ContainerState;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n trimmedName: string;\n}): Promise<{\n readonly containerState: ContainerState;\n /** The acknowledged create time, when the create committed the seed. */\n readonly seedCommittedAt: string | null;\n} | null> {\n const {\n childId,\n icon,\n systemSlot,\n doc,\n initialRecord,\n parentState,\n resolveProjectionUserKey,\n runtime,\n stillCurrent,\n trimmedName,\n } = input;\n const parentProjection = await loadContainerWriterProjectionForState({\n containerState: parentState,\n runtime,\n });\n if (!parentProjection) {\n return null;\n }\n\n // The create carries the whole first metadata update, as a replayed create\n // intent does, so no metadata sync follows an accepted seed.\n const seedFields = input.initialUpdate\n ? createPendingUpdateFields(input.initialUpdate)\n : null;\n const metadataSeedUpdate = seedFields\n ? { ...seedFields, id: crypto.randomUUID() }\n : null;\n const created = await withRegisteredMetadataSeed(\n input.locallyAcceptedMetadataUpdateIds,\n metadataSeedUpdate,\n () =>\n createRemoteContainer({\n systemSlot,\n containerId: childId,\n ...(metadataSeedUpdate ? { metadataSeedUpdate } : {}),\n parentContainerId: parentState.container.id,\n parentProjection,\n resolveProjectionUserKey,\n runtime,\n stillCurrent,\n }),\n );\n\n if (!created || created === CONTAINER_ALREADY_COMMITTED) {\n // A benign manifest-exists conflict means the container is already committed\n // remotely (a lost create response). This eager path cannot rebuild its\n // committed metadata state, so leave the create intent to reconcile it via\n // hydration rather than fabricating a local record from a missing response.\n return null;\n }\n\n return {\n containerState: {\n container: {\n id: created.containerId,\n // The creator owns a newly-created container until the server reconciles.\n effectiveAccessLevel: \"admin\",\n organizationId: created.organizationId,\n parentId: created.parentId,\n metadataDocumentId: created.metadataDocumentId,\n systemSlot: created.systemSlot,\n name: trimmedName,\n icon,\n createdAt: created.createdAt,\n serverCreatedAt: created.createdAt,\n serverUpdatedAt: created.updatedAt,\n updatedAt: created.updatedAt,\n },\n doc,\n record: {\n ...initialRecord,\n accessEpoch: 1,\n accessStateHash: created.accessManifestHash,\n // A committed seed stands in for the first metadata sync.\n lastCommitLsn: created.metadataSeed?.commitLsn ?? null,\n ...created.persistedMetadataState,\n },\n },\n seedCommittedAt: created.metadataSeed ? created.updatedAt : null,\n };\n}\n\nfunction buildLocalContainerContentsChildContainerState(input: {\n childId: string;\n icon: string | null;\n systemSlot?: ContainerSystemSlot | null | undefined;\n doc: ContainerMetadataDocumentState;\n initialRecord: DocumentRecord;\n parentState: ContainerState;\n trimmedName: string;\n}): ContainerState {\n const {\n systemSlot,\n childId,\n doc,\n icon,\n initialRecord,\n parentState,\n trimmedName,\n } = input;\n\n return {\n container: {\n id: childId,\n effectiveAccessLevel: \"admin\",\n organizationId: parentState.container.organizationId,\n parentId: parentState.container.id,\n metadataDocumentId: null,\n systemSlot: systemSlot ?? null,\n name: trimmedName,\n icon,\n },\n doc,\n record: initialRecord,\n };\n}\n\nfunction createInitialChildContainerDocumentRecord(input: {\n childId: string;\n initialUpdate: Uint8Array;\n}): DocumentRecord {\n return {\n accessEpoch: 1,\n accessStateHash: null,\n contentKeyBundle: null,\n documentId: null,\n documentKekTargets: null,\n documentManifestBundle: null,\n id: input.childId,\n lastCommitLsn: null,\n metadataUpdates: bytesToBase64(input.initialUpdate),\n snapshotEndVersion: \"\",\n };\n}\n\nfunction resolveAtomicChildContainerSave(\n persistence: ContainerContentsPersistence,\n): NonNullable<\n ContainerContentsPersistence[\"saveContainerWithPendingUpdate\"]\n> | null {\n const saveContainerWithPendingUpdate =\n persistence.saveContainerWithPendingUpdate;\n if (!saveContainerWithPendingUpdate) return null;\n const inheritedSqlAtomicSave =\n saveContainerWithPendingUpdate ===\n defaultContainerContentsPersistence.saveContainerWithPendingUpdate &&\n (persistence.saveContainer !==\n defaultContainerContentsPersistence.saveContainer ||\n persistence.enqueuePendingUpdate !==\n defaultContainerContentsPersistence.enqueuePendingUpdate);\n return inheritedSqlAtomicSave ? null : saveContainerWithPendingUpdate;\n}\n\nfunction childContainerSaveOptions(input: {\n createIntent: { parentContainerId: string } | undefined;\n localUpdatedAt: string | null;\n stillCurrent?: (() => boolean) | undefined;\n}) {\n const { createIntent, localUpdatedAt, stillCurrent } = input;\n return createIntent || localUpdatedAt || stillCurrent\n ? {\n ...(createIntent ? { createIntent } : {}),\n ...(localUpdatedAt ? { localUpdatedAt } : {}),\n stillCurrent,\n }\n : undefined;\n}\n\nasync function persistCreatedChildContainer(input: {\n containerState: ContainerState;\n initialUpdate: Uint8Array;\n /** Set once the create committed the initial update as its seed. */\n localUpdatedAt: string | null;\n persistence: ContainerContentsPersistence;\n queueRemoteSync: boolean;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<boolean | null> {\n const { containerState, localUpdatedAt, persistence, queueRemoteSync } =\n input;\n const { runtime } = input;\n const createIntent =\n queueRemoteSync &&\n !containerState.record.documentId &&\n containerState.container.parentId\n ? { parentContainerId: containerState.container.parentId }\n : undefined;\n const shouldRequestSync =\n queueRemoteSync &&\n (!containerState.record.documentId ||\n Boolean(containerState.record.contentKeyBundle));\n const saveOptions = childContainerSaveOptions({\n createIntent,\n localUpdatedAt,\n stillCurrent: input.stillCurrent,\n });\n const pendingUpdate = shouldRequestSync\n ? createPendingUpdateFields(input.initialUpdate)\n : null;\n if (pendingUpdate) {\n const saveContainerWithPendingUpdate =\n resolveAtomicChildContainerSave(persistence);\n if (!saveContainerWithPendingUpdate) return null;\n containerState.container = await saveContainerWithPendingUpdate(\n runtime.infra.execSql,\n containerState.container,\n containerState.record,\n { ...saveOptions, pendingUpdate },\n );\n } else {\n containerState.container = await persistence.saveContainer(\n runtime.infra.execSql,\n containerState.container,\n containerState.record,\n saveOptions,\n );\n }\n return input.stillCurrent?.() === false ? null : shouldRequestSync;\n}\n\nexport async function createChildContainerState(input: {\n systemSlot?: ContainerSystemSlot | null | undefined;\n createRemote: boolean;\n icon?: string | null | undefined;\n locallyAcceptedMetadataUpdateIds?: Set<string> | undefined;\n name: string;\n parentState: ContainerState;\n persistence: ContainerContentsPersistence;\n queueRemoteSync?: boolean | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<CreatedChildContainerState | null> {\n const {\n createRemote,\n systemSlot,\n name,\n parentState,\n persistence,\n queueRemoteSync = true,\n resolveProjectionUserKey,\n runtime,\n } = input;\n const trimmedName = name.trim();\n if (!trimmedName) {\n return null;\n }\n\n const icon = input.icon?.trim() || null;\n const childId = await createChildContainerId(\n parentState.container.id,\n systemSlot,\n );\n const { doc, initialUpdate } =\n await createInitializedContainerMetadataDocument(childId, {\n icon,\n name: trimmedName,\n });\n const initialRecord = createInitialChildContainerDocumentRecord({\n childId,\n initialUpdate,\n });\n if (input.stillCurrent?.() === false) return null;\n // An initial metadata update and its container identity are one durable unit.\n // Refuse before any remote create when an adapter cannot provide\n // that atomic operation; a SQLite transaction cannot roll back adapter-owned\n // side effects.\n if (queueRemoteSync && !resolveAtomicChildContainerSave(persistence)) {\n return null;\n }\n\n const remoteChild = createRemote\n ? await buildRemoteContainerContentsChildContainerState({\n childId,\n icon,\n systemSlot,\n doc,\n initialRecord,\n initialUpdate: queueRemoteSync ? initialUpdate : null,\n locallyAcceptedMetadataUpdateIds:\n input.locallyAcceptedMetadataUpdateIds,\n parentState,\n resolveProjectionUserKey,\n runtime,\n stillCurrent: input.stillCurrent,\n trimmedName,\n })\n : null;\n const containerState =\n remoteChild?.containerState ??\n buildLocalContainerContentsChildContainerState({\n childId,\n icon,\n systemSlot,\n doc,\n initialRecord,\n parentState,\n trimmedName,\n });\n const shouldRequestSync = await persistCreatedChildContainer({\n containerState,\n initialUpdate,\n // A committed seed leaves nothing to queue or sync: nothing local is\n // newer than the acknowledged create.\n localUpdatedAt: remoteChild?.seedCommittedAt ?? null,\n persistence,\n queueRemoteSync: queueRemoteSync && !remoteChild?.seedCommittedAt,\n runtime,\n stillCurrent: input.stillCurrent,\n });\n if (shouldRequestSync === null) return null;\n\n return {\n containerState,\n initialUpdate,\n shouldRequestSync,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"createChild.js","sourceRoot":"","sources":["createChild.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AACpF,OAAO,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAM9E,OAAO,EAAE,0BAA0B,EAAE,MAAM,4BAA4B,CAAC;AACxE,OAAO,EAAE,0CAA0C,EAAE,MAAM,0CAA0C,CAAC;AAEtG,OAAO,EAAE,qCAAqC,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAMlD,OAAO,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AAEnE,MAAM,mCAAmC,GAAG,IAAI,UAAU,CAAC;IACzD,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IAC5E,IAAI,EAAE,IAAI,EAAE,IAAI;CACjB,CAAC,CAAC;AAEH;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CAAC,KAGvC;IACC,OAAO,wBAAwB,CAC7B,mCAAmC,EACnC,GAAG,KAAK,CAAC,iBAAiB,SAAS,KAAK,CAAC,UAAU,EAAE,CACtD,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,sBAAsB,CACnC,iBAAyB,EACzB,UAAkD;IAElD,OAAO,UAAU;QACf,CAAC,CAAC,uBAAuB,CAAC,EAAE,iBAAiB,EAAE,UAAU,EAAE,CAAC;QAC5D,CAAC,CAAC,MAAM,CAAC,UAA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X,WAAW;SACZ,CAAC,CAAC;IACL,MAAM,iBAAiB,GAAG,MAAM,4BAA4B,CAAC;QAC3D,cAAc;QACd,aAAa;QACb,qEAAqE;QACrE,sCAAsC;QACtC,cAAc,EAAE,WAAW,EAAE,eAAe,IAAI,IAAI;QACpD,WAAW;QACX,eAAe,EAAE,eAAe,IAAI,CAAC,WAAW,EAAE,eAAe;QACjE,OAAO;QACP,YAAY,EAAE,KAAK,CAAC,YAAY;KACjC,CAAC,CAAC;IACH,IAAI,iBAAiB,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAE5C,OAAO;QACL,cAAc;QACd,aAAa;QACb,iBAAiB;KAClB,CAAC;AACJ,CAAC","sourcesContent":["import { bytesToBase64 } from \"@tearleads/encoding\";\nimport type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport { createPendingUpdateFields } from \"../../../../data/documents/documentSync\";\nimport { deriveStableUuidV4Shaped } from \"../../../../data/keying/stableUuid\";\nimport type { ProjectionUserKeyResolver } from \"../../../../data/keyingProjectionVerification\";\nimport type {\n ContainerContentsPersistence,\n ContainerDocumentRecord as DocumentRecord,\n} from \"../../containerPersistence\";\nimport { resolveAtomicContainerSave } from \"../../containerPersistence\";\nimport { createInitializedContainerMetadataDocument } from \"../../metadata/containerMetadataDocument\";\nimport type { ContainerState } from \"../../remoteHydration\";\nimport { loadContainerWriterProjectionForState } from \"../projectionCache\";\nimport { createRemoteContainer } from \"../remote\";\nimport type {\n ContainerMetadataDocumentState,\n ContainerWorkflowRuntime,\n CreatedChildContainerState,\n} from \"../types\";\nimport { withRegisteredMetadataSeed } from \"./createIntentSeed\";\nimport { CONTAINER_ALREADY_COMMITTED } from \"./createWithMetadata\";\n\nconst SYSTEM_CONTAINER_ID_NAMESPACE_BYTES = new Uint8Array([\n 0xc5, 0x5e, 0x8f, 0x2e, 0x3d, 0x82, 0x4b, 0x4d, 0x8d, 0xbd, 0x0d, 0xd2, 0xd3,\n 0x60, 0xb3, 0x6d,\n]);\n\n/**\n * Derives a stable, UUID-shaped container id for a built-in system slot.\n *\n * The namespace digest makes each slot idempotent across stale store snapshots.\n * The UUID version and variant bits are normalized to a v4 shape because remote\n * container APIs currently validate container ids with the same v4 UUID contract\n * they use for random user-created containers.\n */\nexport function deriveSystemContainerId(input: {\n parentContainerId: string;\n systemSlot: ContainerSystemSlot;\n}): Promise<string> {\n return deriveStableUuidV4Shaped(\n SYSTEM_CONTAINER_ID_NAMESPACE_BYTES,\n `${input.parentContainerId}\\u0000${input.systemSlot}`,\n );\n}\n\nasync function createChildContainerId(\n parentContainerId: string,\n systemSlot: ContainerSystemSlot | null | undefined,\n): Promise<string> {\n return systemSlot\n ? deriveSystemContainerId({ parentContainerId, systemSlot })\n : crypto.randomUUID();\n}\n\nasync function buildRemoteContainerContentsChildContainerState(input: {\n childId: string;\n icon: string | null;\n systemSlot?: ContainerSystemSlot | null | undefined;\n doc: ContainerMetadataDocumentState;\n initialRecord: DocumentRecord;\n /** Present when the first metadata update should commit with the create. */\n initialUpdate: Uint8Array | null;\n locallyAcceptedMetadataUpdateIds?: Set<string> | undefined;\n parentState: ContainerState;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n trimmedName: string;\n}): Promise<{\n readonly containerState: ContainerState;\n /** The acknowledged create time, when the create committed the seed. */\n readonly seedCommittedAt: string | null;\n} | null> {\n const {\n childId,\n icon,\n systemSlot,\n doc,\n initialRecord,\n parentState,\n resolveProjectionUserKey,\n runtime,\n stillCurrent,\n trimmedName,\n } = input;\n const parentProjection = await loadContainerWriterProjectionForState({\n containerState: parentState,\n runtime,\n });\n if (!parentProjection) {\n return null;\n }\n\n // The create carries the whole first metadata update, as a replayed create\n // intent does, so no metadata sync follows an accepted seed.\n const seedFields = input.initialUpdate\n ? createPendingUpdateFields(input.initialUpdate)\n : null;\n const metadataSeedUpdate = seedFields\n ? { ...seedFields, id: crypto.randomUUID() }\n : null;\n const created = await withRegisteredMetadataSeed(\n input.locallyAcceptedMetadataUpdateIds,\n metadataSeedUpdate,\n () =>\n createRemoteContainer({\n systemSlot,\n containerId: childId,\n ...(metadataSeedUpdate ? { metadataSeedUpdate } : {}),\n parentContainerId: parentState.container.id,\n parentProjection,\n resolveProjectionUserKey,\n runtime,\n stillCurrent,\n }),\n );\n\n if (!created || created === CONTAINER_ALREADY_COMMITTED) {\n // A benign manifest-exists conflict means the container is already committed\n // remotely (a lost create response). This eager path cannot rebuild its\n // committed metadata state, so leave the create intent to reconcile it via\n // hydration rather than fabricating a local record from a missing response.\n return null;\n }\n\n return {\n containerState: {\n container: {\n id: created.containerId,\n // The creator owns a newly-created container until the server reconciles.\n effectiveAccessLevel: \"admin\",\n organizationId: created.organizationId,\n parentId: created.parentId,\n metadataDocumentId: created.metadataDocumentId,\n systemSlot: created.systemSlot,\n name: trimmedName,\n icon,\n createdAt: created.createdAt,\n serverCreatedAt: created.createdAt,\n serverUpdatedAt: created.updatedAt,\n updatedAt: created.updatedAt,\n },\n doc,\n record: {\n ...initialRecord,\n accessEpoch: 1,\n accessStateHash: created.accessManifestHash,\n // A committed seed stands in for the first metadata sync.\n lastCommitLsn: created.metadataSeed?.commitLsn ?? null,\n ...created.persistedMetadataState,\n },\n },\n seedCommittedAt: created.metadataSeed ? created.updatedAt : null,\n };\n}\n\nfunction buildLocalContainerContentsChildContainerState(input: {\n childId: string;\n icon: string | null;\n systemSlot?: ContainerSystemSlot | null | undefined;\n doc: ContainerMetadataDocumentState;\n initialRecord: DocumentRecord;\n parentState: ContainerState;\n trimmedName: string;\n}): ContainerState {\n const {\n systemSlot,\n childId,\n doc,\n icon,\n initialRecord,\n parentState,\n trimmedName,\n } = input;\n\n return {\n container: {\n id: childId,\n effectiveAccessLevel: \"admin\",\n organizationId: parentState.container.organizationId,\n parentId: parentState.container.id,\n metadataDocumentId: null,\n systemSlot: systemSlot ?? null,\n name: trimmedName,\n icon,\n },\n doc,\n record: initialRecord,\n };\n}\n\nfunction createInitialChildContainerDocumentRecord(input: {\n childId: string;\n initialUpdate: Uint8Array;\n}): DocumentRecord {\n return {\n accessEpoch: 1,\n accessStateHash: null,\n contentKeyBundle: null,\n documentId: null,\n documentKekTargets: null,\n documentManifestBundle: null,\n id: input.childId,\n lastCommitLsn: null,\n metadataUpdates: bytesToBase64(input.initialUpdate),\n snapshotEndVersion: \"\",\n };\n}\n\nfunction childContainerSaveOptions(input: {\n createIntent: { parentContainerId: string } | undefined;\n localUpdatedAt: string | null;\n stillCurrent?: (() => boolean) | undefined;\n}) {\n const { createIntent, localUpdatedAt, stillCurrent } = input;\n return createIntent || localUpdatedAt || stillCurrent\n ? {\n ...(createIntent ? { createIntent } : {}),\n ...(localUpdatedAt ? { localUpdatedAt } : {}),\n stillCurrent,\n }\n : undefined;\n}\n\nasync function persistCreatedChildContainer(input: {\n containerState: ContainerState;\n initialUpdate: Uint8Array;\n /** Set once the create committed the initial update as its seed. */\n localUpdatedAt: string | null;\n persistence: ContainerContentsPersistence;\n queueRemoteSync: boolean;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<boolean | null> {\n const { containerState, localUpdatedAt, persistence, queueRemoteSync } =\n input;\n const { runtime } = input;\n const createIntent =\n queueRemoteSync &&\n !containerState.record.documentId &&\n containerState.container.parentId\n ? { parentContainerId: containerState.container.parentId }\n : undefined;\n const shouldRequestSync =\n queueRemoteSync &&\n (!containerState.record.documentId ||\n Boolean(containerState.record.contentKeyBundle));\n const saveOptions = childContainerSaveOptions({\n createIntent,\n localUpdatedAt,\n stillCurrent: input.stillCurrent,\n });\n const pendingUpdate = shouldRequestSync\n ? createPendingUpdateFields(input.initialUpdate)\n : null;\n if (pendingUpdate) {\n const saveContainerWithPendingUpdate =\n resolveAtomicContainerSave(persistence);\n if (!saveContainerWithPendingUpdate) return null;\n containerState.container = await saveContainerWithPendingUpdate(\n runtime.infra.execSql,\n containerState.container,\n containerState.record,\n { ...saveOptions, pendingUpdate },\n );\n } else {\n containerState.container = await persistence.saveContainer(\n runtime.infra.execSql,\n containerState.container,\n containerState.record,\n saveOptions,\n );\n }\n return input.stillCurrent?.() === false ? null : shouldRequestSync;\n}\n\nexport async function createChildContainerState(input: {\n systemSlot?: ContainerSystemSlot | null | undefined;\n createRemote: boolean;\n icon?: string | null | undefined;\n locallyAcceptedMetadataUpdateIds?: Set<string> | undefined;\n name: string;\n parentState: ContainerState;\n persistence: ContainerContentsPersistence;\n queueRemoteSync?: boolean | undefined;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n stillCurrent?: (() => boolean) | undefined;\n}): Promise<CreatedChildContainerState | null> {\n const {\n createRemote,\n systemSlot,\n name,\n parentState,\n persistence,\n queueRemoteSync = true,\n resolveProjectionUserKey,\n runtime,\n } = input;\n const trimmedName = name.trim();\n if (!trimmedName) {\n return null;\n }\n\n const icon = input.icon?.trim() || null;\n const childId = await createChildContainerId(\n parentState.container.id,\n systemSlot,\n );\n const { doc, initialUpdate } =\n await createInitializedContainerMetadataDocument(childId, {\n icon,\n name: trimmedName,\n });\n const initialRecord = createInitialChildContainerDocumentRecord({\n childId,\n initialUpdate,\n });\n if (input.stillCurrent?.() === false) return null;\n // An initial metadata update and its container identity are one durable unit.\n // Refuse before any remote create when an adapter cannot provide\n // that atomic operation; a SQLite transaction cannot roll back adapter-owned\n // side effects.\n if (queueRemoteSync && !resolveAtomicContainerSave(persistence)) {\n return null;\n }\n\n const remoteChild = createRemote\n ? await buildRemoteContainerContentsChildContainerState({\n childId,\n icon,\n systemSlot,\n doc,\n initialRecord,\n initialUpdate: queueRemoteSync ? initialUpdate : null,\n locallyAcceptedMetadataUpdateIds:\n input.locallyAcceptedMetadataUpdateIds,\n parentState,\n resolveProjectionUserKey,\n runtime,\n stillCurrent: input.stillCurrent,\n trimmedName,\n })\n : null;\n const containerState =\n remoteChild?.containerState ??\n buildLocalContainerContentsChildContainerState({\n childId,\n icon,\n systemSlot,\n doc,\n initialRecord,\n parentState,\n trimmedName,\n });\n const shouldRequestSync = await persistCreatedChildContainer({\n containerState,\n initialUpdate,\n // A committed seed leaves nothing to queue or sync: nothing local is\n // newer than the acknowledged create.\n localUpdatedAt: remoteChild?.seedCommittedAt ?? null,\n persistence,\n queueRemoteSync: queueRemoteSync && !remoteChild?.seedCommittedAt,\n runtime,\n stillCurrent: input.stillCurrent,\n });\n if (shouldRequestSync === null) return null;\n\n return {\n containerState,\n initialUpdate,\n shouldRequestSync,\n };\n}\n"]}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ContainerContentsPersistence, sqlContainerContentsPersistence as defaultContainerContentsPersistence } from "../../data/persistence/container-contents/containerContentsPersistence.js";
|
|
2
2
|
import { type ContainerSyncWatermarkLane } from "../../data/persistence/container-contents/containerSyncWatermarkPersistence.js";
|
|
3
|
-
import type { ExecSql } from "../../data/sqlite/sqlSchema.js";
|
|
4
3
|
export type { ContainerContentsPersistence, ContainerCreateIntentRecord, ContainerHydrationTombstone, ContainerMetadataRecord, ContainerMetadataRecord as ContainerDocumentRecord, ContainerMoveIntentRecord, LocalRootDescendantReparentInput, StoredContainerState, } from "../../data/persistence/container-contents/containerContentsPersistence.js";
|
|
5
|
-
export { sqlContainerContentsPersistence as defaultContainerContentsPersistence } from "../../data/persistence/container-contents/containerContentsPersistence.js";
|
|
6
4
|
export type { ContainerMetadataRevalidation } from "../../data/persistence/container-contents/containerMetadataRevalidations.js";
|
|
7
5
|
export type { ContainerRecord } from "../../data/persistence/container-contents/containerPersistence.js";
|
|
8
6
|
export { type ContainerSyncWatermarkLane, ContainerSyncWatermarkRepository, } from "../../data/persistence/container-contents/containerSyncWatermarkPersistence.js";
|
|
9
7
|
export type { ExecSql } from "../../data/sqlite/sqlSchema.js";
|
|
10
|
-
export
|
|
11
|
-
containerId: string;
|
|
12
|
-
sourceVersionVector?: string | null | undefined;
|
|
13
|
-
update: Uint8Array;
|
|
14
|
-
}): Promise<string | null>;
|
|
8
|
+
export { defaultContainerContentsPersistence };
|
|
15
9
|
export declare function createContainerParentSyncLane(parentId: string | null): ContainerSyncWatermarkLane;
|
|
16
10
|
export declare function createContainerContentsSyncLane(containerId: string): ContainerSyncWatermarkLane;
|
|
11
|
+
export declare function resolveAtomicContainerSave(persistence: ContainerContentsPersistence): NonNullable<ContainerContentsPersistence["saveContainerWithPendingUpdate"]> | null;
|
|
12
|
+
export declare function requireAtomicContainerSave(persistence: ContainerContentsPersistence): (execSql: import("./containerPersistence.js").ExecSql, container: import("./containerPersistence.js").ContainerRecord, record: import("./containerPersistence.js").ContainerMetadataRecord, options: import("../../data/persistence/container-contents/containerContentsPersistenceTypes.js").SaveContainerWithPendingUpdateOptions) => Promise<import("./containerPersistence.js").ContainerRecord>;
|
|
@@ -1,21 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { sqlContainerContentsPersistence as defaultContainerContentsPersistence, } from "../../data/persistence/container-contents/containerContentsPersistence.js";
|
|
2
2
|
import { containerContentsSyncLane, containerParentSyncLane, } from "../../data/persistence/container-contents/containerSyncWatermarkPersistence.js";
|
|
3
|
-
export { sqlContainerContentsPersistence as defaultContainerContentsPersistence } from "../../data/persistence/container-contents/containerContentsPersistence.js";
|
|
4
3
|
export { ContainerSyncWatermarkRepository, } from "../../data/persistence/container-contents/containerSyncWatermarkPersistence.js";
|
|
5
|
-
export
|
|
6
|
-
const pendingUpdateFields = createPendingUpdateFields(params.update, params.sourceVersionVector);
|
|
7
|
-
if (!pendingUpdateFields) {
|
|
8
|
-
return null;
|
|
9
|
-
}
|
|
10
|
-
return persistence.enqueuePendingUpdate(execSql, {
|
|
11
|
-
containerId: params.containerId,
|
|
12
|
-
...pendingUpdateFields,
|
|
13
|
-
});
|
|
14
|
-
}
|
|
4
|
+
export { defaultContainerContentsPersistence };
|
|
15
5
|
export function createContainerParentSyncLane(parentId) {
|
|
16
6
|
return containerParentSyncLane(parentId);
|
|
17
7
|
}
|
|
18
8
|
export function createContainerContentsSyncLane(containerId) {
|
|
19
9
|
return containerContentsSyncLane(containerId);
|
|
20
10
|
}
|
|
11
|
+
export function resolveAtomicContainerSave(persistence) {
|
|
12
|
+
const saveContainerWithPendingUpdate = persistence.saveContainerWithPendingUpdate;
|
|
13
|
+
if (!saveContainerWithPendingUpdate)
|
|
14
|
+
return null;
|
|
15
|
+
const inheritedSqlAtomicSave = saveContainerWithPendingUpdate ===
|
|
16
|
+
defaultContainerContentsPersistence.saveContainerWithPendingUpdate &&
|
|
17
|
+
(persistence.saveContainer !==
|
|
18
|
+
defaultContainerContentsPersistence.saveContainer ||
|
|
19
|
+
persistence.enqueuePendingUpdate !==
|
|
20
|
+
defaultContainerContentsPersistence.enqueuePendingUpdate);
|
|
21
|
+
return inheritedSqlAtomicSave
|
|
22
|
+
? null
|
|
23
|
+
: saveContainerWithPendingUpdate.bind(persistence);
|
|
24
|
+
}
|
|
25
|
+
export function requireAtomicContainerSave(persistence) {
|
|
26
|
+
const save = resolveAtomicContainerSave(persistence);
|
|
27
|
+
if (!save)
|
|
28
|
+
throw new Error("Container persistence must implement saveContainerWithPendingUpdate to repair pending metadata");
|
|
29
|
+
return save;
|
|
30
|
+
}
|
|
21
31
|
//# sourceMappingURL=containerPersistence.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"containerPersistence.js","sourceRoot":"","sources":["containerPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"containerPersistence.js","sourceRoot":"","sources":["containerPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,+BAA+B,IAAI,mCAAmC,GACvE,MAAM,wEAAwE,CAAC;AAChF,OAAO,EAEL,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,6EAA6E,CAAC;AAcrF,OAAO,EAEL,gCAAgC,GACjC,MAAM,6EAA6E,CAAC;AAErF,OAAO,EAAE,mCAAmC,EAAE,CAAC;AAE/C,MAAM,UAAU,6BAA6B,CAC3C,QAAuB;IAEvB,OAAO,uBAAuB,CAAC,QAAQ,CAAC,CAAC;AAC3C,CAAC;AAED,MAAM,UAAU,+BAA+B,CAC7C,WAAmB;IAEnB,OAAO,yBAAyB,CAAC,WAAW,CAAC,CAAC;AAChD,CAAC;AAED,MAAM,UAAU,0BAA0B,CACxC,WAAyC;IAIzC,MAAM,8BAA8B,GAClC,WAAW,CAAC,8BAA8B,CAAC;IAC7C,IAAI,CAAC,8BAA8B;QAAE,OAAO,IAAI,CAAC;IACjD,MAAM,sBAAsB,GAC1B,8BAA8B;QAC5B,mCAAmC,CAAC,8BAA8B;QACpE,CAAC,WAAW,CAAC,aAAa;YACxB,mCAAmC,CAAC,aAAa;YACjD,WAAW,CAAC,oBAAoB;gBAC9B,mCAAmC,CAAC,oBAAoB,CAAC,CAAC;IAChE,OAAO,sBAAsB;QAC3B,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,8BAA8B,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,0BAA0B,CACxC,WAAyC;IAEzC,MAAM,IAAI,GAAG,0BAA0B,CAAC,WAAW,CAAC,CAAC;IACrD,IAAI,CAAC,IAAI;QACP,MAAM,IAAI,KAAK,CACb,gGAAgG,CACjG,CAAC;IACJ,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import {\n type ContainerContentsPersistence,\n sqlContainerContentsPersistence as defaultContainerContentsPersistence,\n} from \"../../data/persistence/container-contents/containerContentsPersistence\";\nimport {\n type ContainerSyncWatermarkLane,\n containerContentsSyncLane,\n containerParentSyncLane,\n} from \"../../data/persistence/container-contents/containerSyncWatermarkPersistence\";\n\nexport type {\n ContainerContentsPersistence,\n ContainerCreateIntentRecord,\n ContainerHydrationTombstone,\n ContainerMetadataRecord,\n ContainerMetadataRecord as ContainerDocumentRecord,\n ContainerMoveIntentRecord,\n LocalRootDescendantReparentInput,\n StoredContainerState,\n} from \"../../data/persistence/container-contents/containerContentsPersistence\";\nexport type { ContainerMetadataRevalidation } from \"../../data/persistence/container-contents/containerMetadataRevalidations\";\nexport type { ContainerRecord } from \"../../data/persistence/container-contents/containerPersistence\";\nexport {\n type ContainerSyncWatermarkLane,\n ContainerSyncWatermarkRepository,\n} from \"../../data/persistence/container-contents/containerSyncWatermarkPersistence\";\nexport type { ExecSql } from \"../../data/sqlite/sqlSchema\";\nexport { defaultContainerContentsPersistence };\n\nexport function createContainerParentSyncLane(\n parentId: string | null,\n): ContainerSyncWatermarkLane {\n return containerParentSyncLane(parentId);\n}\n\nexport function createContainerContentsSyncLane(\n containerId: string,\n): ContainerSyncWatermarkLane {\n return containerContentsSyncLane(containerId);\n}\n\nexport function resolveAtomicContainerSave(\n persistence: ContainerContentsPersistence,\n): NonNullable<\n ContainerContentsPersistence[\"saveContainerWithPendingUpdate\"]\n> | null {\n const saveContainerWithPendingUpdate =\n persistence.saveContainerWithPendingUpdate;\n if (!saveContainerWithPendingUpdate) return null;\n const inheritedSqlAtomicSave =\n saveContainerWithPendingUpdate ===\n defaultContainerContentsPersistence.saveContainerWithPendingUpdate &&\n (persistence.saveContainer !==\n defaultContainerContentsPersistence.saveContainer ||\n persistence.enqueuePendingUpdate !==\n defaultContainerContentsPersistence.enqueuePendingUpdate);\n return inheritedSqlAtomicSave\n ? null\n : saveContainerWithPendingUpdate.bind(persistence);\n}\n\nexport function requireAtomicContainerSave(\n persistence: ContainerContentsPersistence,\n) {\n const save = resolveAtomicContainerSave(persistence);\n if (!save)\n throw new Error(\n \"Container persistence must implement saveContainerWithPendingUpdate to repair pending metadata\",\n );\n return save;\n}\n"]}
|
|
@@ -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
|
-
|
|
57
|
-
|
|
97
|
+
if (!pages) {
|
|
98
|
+
for (const lane of batch)
|
|
58
99
|
listed.set(lane.containerId, "unavailable");
|
|
59
|
-
|
|
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,4BAA4B,GAC7B,MAAM,+BAA+B,CAAC;AAevC;;;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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KAAK,UAAU,kCAAkC,CAAC,KAIxD;IACC,MAAM,MAAM,GAAG,CACb,MAAM,6BAA6B,CAAC;QAClC,KAAK,EAAE,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,WAAW,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;QAC5D,0BAA0B,EAAE,CAAC,KAAK,EAAE,EAAE,CACpC,iCAAiC,CAC/B,KAAK,CAAC,SAAS,EACf,KAAK,EACL,KAAK,CAAC,YAAY,CACnB;KACJ,CAAC,CACH,CAAC,GAAG,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,S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AC,YAAY,CACnB;KACJ,CAAC,CACH,CAAC,GAAG,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\";\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,
|
|
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
|
-
|
|
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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,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 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
|
|
14
|
-
const
|
|
15
|
-
|
|
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
|
|
18
|
-
|
|
19
|
-
|
|
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)));
|
|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M,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,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 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,IAAI;QACN,CAAC,CAAC,aAAa,CAAC;AACpB,CAAC;AAED,KAAK,UAAU,iBAAiB,CAC9B,KAAkC,EAClC,QAAmC,EACnC,KAA0C,EAC1C,UAAkB,EAClB,mBAA4B;IAE5B,MAAM,MAAM,GAAG,mBAAmB;QAChC,CAAC,CAAC,IAAI;QACN,CAAC,CAAC,MAAM,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,eAAe,CAAC,CAAC;IAClE,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;IAC7D,MAAM,YAAY,GAChB,MAAM,IAAI,MAAM,CAAC,WAAW,IAAI,YAAY,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;IAC/D,OAAO,CACL,YAAY;QACZ,CAAC,MAAM,QAAQ,CACb,KAAK,CAAC,UAAU,EAChB,CAAC,mBAAmB,IAAI,KAAK,CAAC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AC,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"]}
|