@tearleads/client-sdk 0.1.253 → 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/client/containerContents/documentDiscovery.js +0 -2
- package/client/containerContents/documentDiscovery.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/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/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/remoteHydration/hydrateRemoteContainers.js +4 -4
- package/workflows/container-contents/remoteHydration/hydrateRemoteContainers.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/remoteContainers/remoteContainerPageHydration.js +29 -27
- package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js.map +1 -1
- package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerState.js +17 -5
- 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 +3 -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/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
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { and, eq, inArray } from "drizzle-orm";
|
|
2
|
+
import { documentHistoryCheckpoints, documents, } from "../../sqlite/schema/documentRecordSchema.js";
|
|
3
|
+
import { containerRemovalBatches } from "./containerRemovalBatches.js";
|
|
4
|
+
import { readRemovalPlacementHeads } from "./containerRemovalPlacements.js";
|
|
5
|
+
import { CONTAINER_METADATA_APP_KIND } from "./dormantContainerMetadata.js";
|
|
6
|
+
/** A permission read may bypass an older entity clock. Its request-time
|
|
7
|
+
* metadata must still match after the deletion has acquired the write lock. */
|
|
8
|
+
export async function confirmedRemovalMetadataMatches(tx, guards) {
|
|
9
|
+
const confirmed = guards.filter((guard) => guard.confirmedAccessLoss && guard.expectedContainer);
|
|
10
|
+
for (const batch of containerRemovalBatches(confirmed)) {
|
|
11
|
+
const ids = batch.map((guard) => guard.containerId);
|
|
12
|
+
const placements = await readRemovalPlacementHeads(tx, batch.flatMap((guard) => guard.expectedContainer ? [guard.expectedContainer] : []));
|
|
13
|
+
const rows = await tx
|
|
14
|
+
.select({
|
|
15
|
+
id: documents.localId,
|
|
16
|
+
documentId: documents.documentId,
|
|
17
|
+
accessEpoch: documents.accessEpoch,
|
|
18
|
+
accessStateHash: documents.accessStateHash,
|
|
19
|
+
metadataUpdates: documentHistoryCheckpoints.snapshot,
|
|
20
|
+
})
|
|
21
|
+
.from(documents)
|
|
22
|
+
.leftJoin(documentHistoryCheckpoints, and(eq(documents.appKind, documentHistoryCheckpoints.appKind), eq(documents.localId, documentHistoryCheckpoints.localId)))
|
|
23
|
+
.where(and(eq(documents.appKind, CONTAINER_METADATA_APP_KIND), inArray(documents.localId, ids)));
|
|
24
|
+
const byId = new Map(rows.map((row) => [row.id, row]));
|
|
25
|
+
const matches = batch.every((guard) => {
|
|
26
|
+
if ((placements.get(guard.containerId) ?? null) !==
|
|
27
|
+
guard.confirmedAccessLoss?.placementHash)
|
|
28
|
+
return false;
|
|
29
|
+
const previous = guard.confirmedAccessLoss?.expectedMetadata;
|
|
30
|
+
const current = byId.get(guard.containerId);
|
|
31
|
+
// A purged metadata document can have an in-memory initial record with
|
|
32
|
+
// no signed head; it has no durable metadata to preserve.
|
|
33
|
+
if (!current)
|
|
34
|
+
return !previous?.accessStateHash;
|
|
35
|
+
if (!previous)
|
|
36
|
+
return false;
|
|
37
|
+
return (previous.documentId === current.documentId &&
|
|
38
|
+
previous.accessEpoch === current.accessEpoch &&
|
|
39
|
+
(previous.accessStateHash ?? null) === current.accessStateHash &&
|
|
40
|
+
previous.metadataUpdates === (current.metadataUpdates ?? ""));
|
|
41
|
+
});
|
|
42
|
+
if (!matches)
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=containerRemovalMetadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"containerRemovalMetadata.js","sourceRoot":"","sources":["containerRemovalMetadata.ts"],"names":[],"mappings":"AAAA,OAAO,EA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;QACJ,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,OAAO;YAAE,OAAO,KAAK,CAAC;IAC7B,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { and, eq, inArray } from \"drizzle-orm\";\nimport {\n documentHistoryCheckpoints,\n documents,\n} from \"../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport type { ContainerDeletionGuard } from \"./containerContentsPersistenceTypes\";\nimport { containerRemovalBatches } from \"./containerRemovalBatches\";\nimport { readRemovalPlacementHeads } from \"./containerRemovalPlacements\";\nimport { CONTAINER_METADATA_APP_KIND } from \"./dormantContainerMetadata\";\n\n/** A permission read may bypass an older entity clock. Its request-time\n * metadata must still match after the deletion has acquired the write lock. */\nexport async function confirmedRemovalMetadataMatches(\n tx: ClientSQLiteTransactionScope,\n guards: readonly ContainerDeletionGuard[],\n): Promise<boolean> {\n const confirmed = guards.filter(\n (guard) => guard.confirmedAccessLoss && guard.expectedContainer,\n );\n for (const batch of containerRemovalBatches(confirmed)) {\n const ids = batch.map((guard) => guard.containerId);\n const placements = await readRemovalPlacementHeads(\n tx,\n batch.flatMap((guard) =>\n guard.expectedContainer ? [guard.expectedContainer] : [],\n ),\n );\n const rows = await tx\n .select({\n id: documents.localId,\n documentId: documents.documentId,\n accessEpoch: documents.accessEpoch,\n accessStateHash: documents.accessStateHash,\n metadataUpdates: documentHistoryCheckpoints.snapshot,\n })\n .from(documents)\n .leftJoin(\n documentHistoryCheckpoints,\n and(\n eq(documents.appKind, documentHistoryCheckpoints.appKind),\n eq(documents.localId, documentHistoryCheckpoints.localId),\n ),\n )\n .where(\n and(\n eq(documents.appKind, CONTAINER_METADATA_APP_KIND),\n inArray(documents.localId, ids),\n ),\n );\n const byId = new Map(rows.map((row) => [row.id, row]));\n const matches = batch.every((guard) => {\n if (\n (placements.get(guard.containerId) ?? null) !==\n guard.confirmedAccessLoss?.placementHash\n )\n return false;\n const previous = guard.confirmedAccessLoss?.expectedMetadata;\n const current = byId.get(guard.containerId);\n // A purged metadata document can have an in-memory initial record with\n // no signed head; it has no durable metadata to preserve.\n if (!current) return !previous?.accessStateHash;\n if (!previous) return false;\n return (\n previous.documentId === current.documentId &&\n previous.accessEpoch === current.accessEpoch &&\n (previous.accessStateHash ?? null) === current.accessStateHash &&\n previous.metadataUpdates === (current.metadataUpdates ?? \"\")\n );\n });\n if (!matches) return false;\n }\n return true;\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { keyingCheckpointTables } from "../../sqlite/schema/keyingCheckpointSchema.js";
|
|
2
|
+
import { getClientSQLitePersistenceRuntime, } from "../../sqlite/sqlitePersistenceRuntime.js";
|
|
3
|
+
import { ensureSqlTables } from "../../sqlite/sqlSchema.js";
|
|
4
|
+
import { accessManifestObjectKey, loadAccessManifestCheckpointRows, } from "../keyingCheckpoints/keyingCheckpointPersistence.js";
|
|
5
|
+
/** A folder's placement checkpoint can advance independently of its metadata. */
|
|
6
|
+
export async function readRemovalPlacementHeads(db, folders) {
|
|
7
|
+
const identities = folders.map(({ id, organizationId }) => ({
|
|
8
|
+
objectId: id,
|
|
9
|
+
organizationId,
|
|
10
|
+
objectKind: "container",
|
|
11
|
+
}));
|
|
12
|
+
const stored = await loadAccessManifestCheckpointRows(db, identities);
|
|
13
|
+
return new Map(identities.map((identity) => [
|
|
14
|
+
identity.objectId,
|
|
15
|
+
stored.get(accessManifestObjectKey(identity))?.manifestHash ?? null,
|
|
16
|
+
]));
|
|
17
|
+
}
|
|
18
|
+
export async function captureRemovalPlacementHeads(execSql, folders) {
|
|
19
|
+
await ensureSqlTables(execSql, keyingCheckpointTables);
|
|
20
|
+
return readRemovalPlacementHeads(getClientSQLitePersistenceRuntime(execSql).db, folders);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=containerRemovalPlacements.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"containerRemovalPlacements.js","sourceRoot":"","sources":["containerRemovalPlacements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAC;AACpF,OAAO,EAEL,iCAAiC,GAClC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAgB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EACL,uBAAuB,EACvB,gCAAgC,GACjC,MAAM,kDAAkD,CAAC;AAK1D,iFAAiF;AACjF,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAC7C,EAAgC,EAChC,OAA6B;IAE7B,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,cAAc,EAAE,EAAE,EAAE,CAAC,CAAC;QAC1D,QAAQ,EAAE,EAAE;QACZ,cAAc;QACd,UAAU,EAAE,WAAoB;KACjC,CAAC,CAAC,CAAC;IACJ,MAAM,MAAM,GAAG,MAAM,gCAAgC,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;IACtE,OAAO,IAAI,GAAG,CACZ,UAAU,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC;QAC3B,QAAQ,CAAC,QAAQ;QACjB,MAAM,CAAC,GAAG,CAAC,uBAAuB,CAAC,QAAQ,CAAC,CAAC,EAAE,YAAY,IAAI,IAAI;KACpE,CAAC,CACH,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAChD,OAAgB,EAChB,OAA6B;IAE7B,MAAM,eAAe,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC;IACvD,OAAO,yBAAyB,CAC9B,iCAAiC,CAAC,OAAO,CAAC,CAAC,EAAE,EAC7C,OAAO,CACR,CAAC;AACJ,CAAC","sourcesContent":["import { keyingCheckpointTables } from \"../../sqlite/schema/keyingCheckpointSchema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../sqlite/sqlSchema\";\nimport {\n accessManifestObjectKey,\n loadAccessManifestCheckpointRows,\n} from \"../keyingCheckpoints/keyingCheckpointPersistence\";\nimport type { ContainerRecord } from \"./containerPersistence\";\n\ntype Placement = Pick<ContainerRecord, \"id\" | \"organizationId\">;\n\n/** A folder's placement checkpoint can advance independently of its metadata. */\nexport async function readRemovalPlacementHeads(\n db: ClientSQLiteTransactionScope,\n folders: readonly Placement[],\n) {\n const identities = folders.map(({ id, organizationId }) => ({\n objectId: id,\n organizationId,\n objectKind: \"container\" as const,\n }));\n const stored = await loadAccessManifestCheckpointRows(db, identities);\n return new Map(\n identities.map((identity) => [\n identity.objectId,\n stored.get(accessManifestObjectKey(identity))?.manifestHash ?? null,\n ]),\n );\n}\n\nexport async function captureRemovalPlacementHeads(\n execSql: ExecSql,\n folders: readonly Placement[],\n) {\n await ensureSqlTables(execSql, keyingCheckpointTables);\n return readRemovalPlacementHeads(\n getClientSQLitePersistenceRuntime(execSql).db,\n folders,\n );\n}\n"]}
|
|
@@ -2,12 +2,13 @@ import type { SyncWatermark } from "../../../internal/validators/response/index.
|
|
|
2
2
|
import { type ClientSQLiteDatabase, type ClientSQLiteTransactionScope } from "../../sqlite/sqlitePersistenceRuntime.js";
|
|
3
3
|
import { type ExecSql } from "../../sqlite/sqlSchema.js";
|
|
4
4
|
declare const CONTAINER_PARENT_LANE = "container_parent";
|
|
5
|
+
declare const CONTAINER_RESTORATION_LANE = "container_restoration";
|
|
5
6
|
declare const CONTAINER_DOCUMENTS_LANE = "container_documents";
|
|
6
7
|
export type ContainerSyncWatermarkLane = {
|
|
7
8
|
kind: typeof CONTAINER_PARENT_LANE;
|
|
8
9
|
parentId: string | null;
|
|
9
10
|
} | {
|
|
10
|
-
kind: typeof CONTAINER_DOCUMENTS_LANE;
|
|
11
|
+
kind: typeof CONTAINER_DOCUMENTS_LANE | typeof CONTAINER_RESTORATION_LANE;
|
|
11
12
|
containerId: string;
|
|
12
13
|
};
|
|
13
14
|
interface ContainerSyncWatermarkRecord {
|
|
@@ -3,6 +3,7 @@ import { containerSyncWatermarks, containerSyncWatermarkTables, } from "../../sq
|
|
|
3
3
|
import { getClientSQLitePersistenceRuntime, } from "../../sqlite/sqlitePersistenceRuntime.js";
|
|
4
4
|
import { ensureSqlTables } from "../../sqlite/sqlSchema.js";
|
|
5
5
|
const CONTAINER_PARENT_LANE = "container_parent";
|
|
6
|
+
const CONTAINER_RESTORATION_LANE = "container_restoration";
|
|
6
7
|
const CONTAINER_DOCUMENTS_LANE = "container_documents";
|
|
7
8
|
const ROOT_CONTAINER_PARENT_LANE_ID = "root";
|
|
8
9
|
const CONTAINER_PARENT_LANE_ID_PREFIX = "parent:";
|
|
@@ -24,6 +25,7 @@ export function containerSyncWatermarkLaneKey(lane) {
|
|
|
24
25
|
laneKind: lane.kind,
|
|
25
26
|
laneId: containerParentLaneId(lane.parentId),
|
|
26
27
|
};
|
|
28
|
+
case CONTAINER_RESTORATION_LANE:
|
|
27
29
|
case CONTAINER_DOCUMENTS_LANE:
|
|
28
30
|
return {
|
|
29
31
|
laneKind: lane.kind,
|
|
@@ -50,14 +52,18 @@ export async function deleteContainerWatermarksInTransaction(tx, containerIds) {
|
|
|
50
52
|
const documentLaneIds = uniqueContainerIds;
|
|
51
53
|
await tx
|
|
52
54
|
.delete(containerSyncWatermarks)
|
|
53
|
-
.where(or(and(eq(containerSyncWatermarks.laneKind, CONTAINER_PARENT_LANE), inArray(containerSyncWatermarks.laneId, parentLaneIds)), and(
|
|
55
|
+
.where(or(and(eq(containerSyncWatermarks.laneKind, CONTAINER_PARENT_LANE), inArray(containerSyncWatermarks.laneId, parentLaneIds)), and(inArray(containerSyncWatermarks.laneKind, [
|
|
56
|
+
CONTAINER_DOCUMENTS_LANE,
|
|
57
|
+
CONTAINER_RESTORATION_LANE,
|
|
58
|
+
]), inArray(containerSyncWatermarks.laneId, documentLaneIds))))
|
|
54
59
|
.run();
|
|
55
60
|
}
|
|
56
61
|
function mapSelectedWatermarkRecord(row) {
|
|
57
62
|
// A lane checked before any cursor was saved has no watermark yet.
|
|
58
63
|
if (row.watermarkId === null ||
|
|
59
64
|
row.watermarkUpdatedAt === null ||
|
|
60
|
-
row.updatedAt === null
|
|
65
|
+
row.updatedAt === null ||
|
|
66
|
+
(row.cursorFormat === 1 && row.watermarkId.includes("/"))) {
|
|
61
67
|
return null;
|
|
62
68
|
}
|
|
63
69
|
return {
|
|
@@ -114,6 +120,7 @@ async function selectWatermarkRows(execSql, lanes) {
|
|
|
114
120
|
laneId: containerSyncWatermarks.laneId,
|
|
115
121
|
laneKind: containerSyncWatermarks.laneKind,
|
|
116
122
|
updatedAt: containerSyncWatermarks.updatedAt,
|
|
123
|
+
cursorFormat: containerSyncWatermarks.cursorFormat,
|
|
117
124
|
watermarkId: containerSyncWatermarks.watermarkId,
|
|
118
125
|
watermarkUpdatedAt: containerSyncWatermarks.watermarkUpdatedAt,
|
|
119
126
|
})
|
|
@@ -192,6 +199,7 @@ export class ContainerSyncWatermarkRepository {
|
|
|
192
199
|
...(fetch.watermark
|
|
193
200
|
? {
|
|
194
201
|
updatedAt: now,
|
|
202
|
+
cursorFormat: 2,
|
|
195
203
|
watermarkId: fetch.watermark.id,
|
|
196
204
|
watermarkUpdatedAt: fetch.watermark.updatedAt,
|
|
197
205
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"containerSyncWatermarkPersistence.js","sourceRoot":"","sources":["containerSyncWatermarkPersistence.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,EAAY,MAAM,aAAa,CAAC;AACxE,OAAO,EACL,uBAAuB,EACvB,4BAA4B,GAC7B,MAAM,kDAAkD,CAAC;AAC1D,OAAO,EAGL,iCAAiC,GAClC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAgB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEv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YAClC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,CAAC,MAAM,OAAO,CAAC,kBAAkB,CAAC,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;aACpE,SAAS,CAAC;IACf,CAAC;CACF","sourcesContent":["import type { SyncWatermark } from \"@tearleads/validators/response\";\nimport { and, eq, inArray, isNotNull, or, type SQL } from \"drizzle-orm\";\nimport {\n containerSyncWatermarks,\n containerSyncWatermarkTables,\n} from \"../../sqlite/schema/containerSyncWatermarkSchema\";\nimport {\n type ClientSQLiteDatabase,\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../sqlite/sqlSchema\";\n\nconst CONTAINER_PARENT_LANE = \"container_parent\";\nconst CONTAINER_DOCUMENTS_LANE = \"container_documents\";\nconst ROOT_CONTAINER_PARENT_LANE_ID = \"root\";\nconst CONTAINER_PARENT_LANE_ID_PREFIX = \"parent:\";\n// Each lane predicate binds two values, so keep batches comfortably under\n// SQLite's variable limit when a caller reads many lanes at once.\nconst LANE_SELECT_BATCH_SIZE = 100;\n\nexport type ContainerSyncWatermarkLane =\n | {\n kind: typeof CONTAINER_PARENT_LANE;\n parentId: string | null;\n }\n | {\n kind: typeof CONTAINER_DOCUMENTS_LANE;\n containerId: string;\n };\n\ninterface SelectedContainerSyncWatermark {\n laneId: string;\n laneKind: string;\n updatedAt: string | null;\n watermarkId: string | null;\n watermarkUpdatedAt: string | null;\n}\n\ninterface ContainerSyncWatermarkRecord {\n laneId: string;\n laneKind: string;\n updatedAt: string;\n watermark: SyncWatermark;\n}\n\nexport interface ContainerSyncLaneCheckRecord {\n checkedAt: string;\n laneId: string;\n laneKind: string;\n}\n\ninterface ContainerSyncMarkerMutationOptions {\n readonly stillCurrent?: (() => boolean) | undefined;\n}\n\nexport const containerParentSyncLane = (\n parentId: string | null,\n): ContainerSyncWatermarkLane => ({\n kind: CONTAINER_PARENT_LANE,\n parentId,\n});\n\nexport const containerContentsSyncLane = (\n containerId: string,\n): ContainerSyncWatermarkLane => ({\n kind: CONTAINER_DOCUMENTS_LANE,\n containerId,\n});\n\nexport function containerSyncWatermarkLaneKey(\n lane: ContainerSyncWatermarkLane,\n): {\n laneId: string;\n laneKind: string;\n} {\n switch (lane.kind) {\n case CONTAINER_PARENT_LANE:\n return {\n laneKind: lane.kind,\n laneId: containerParentLaneId(lane.parentId),\n };\n case CONTAINER_DOCUMENTS_LANE:\n return {\n laneKind: lane.kind,\n laneId: lane.containerId,\n };\n }\n}\n\nfunction containerParentLaneId(parentId: string | null): string {\n return parentId === null\n ? ROOT_CONTAINER_PARENT_LANE_ID\n : `${CONTAINER_PARENT_LANE_ID_PREFIX}${parentId}`;\n}\n\n/**\n * The lane-state half of the container-delete cascade (cursor and freshness\n * marker share one row), statement-only so the caller can run it inside one\n * atomic transaction alongside the rest of the cascade.\n */\nexport async function deleteContainerWatermarksInTransaction(\n tx: ClientSQLiteDatabase | ClientSQLiteTransactionScope,\n containerIds: ReadonlyArray<string>,\n): Promise<void> {\n const uniqueContainerIds = Array.from(new Set(containerIds));\n if (uniqueContainerIds.length === 0) {\n return;\n }\n const parentLaneIds = uniqueContainerIds.map(containerParentLaneId);\n const documentLaneIds = uniqueContainerIds;\n await tx\n .delete(containerSyncWatermarks)\n .where(\n or(\n and(\n eq(containerSyncWatermarks.laneKind, CONTAINER_PARENT_LANE),\n inArray(containerSyncWatermarks.laneId, parentLaneIds),\n ),\n and(\n eq(containerSyncWatermarks.laneKind, CONTAINER_DOCUMENTS_LANE),\n inArray(containerSyncWatermarks.laneId, documentLaneIds),\n ),\n ),\n )\n .run();\n}\n\nfunction mapSelectedWatermarkRecord(\n row: SelectedContainerSyncWatermark,\n): ContainerSyncWatermarkRecord | null {\n // A lane checked before any cursor was saved has no watermark yet.\n if (\n row.watermarkId === null ||\n row.watermarkUpdatedAt === null ||\n row.updatedAt === null\n ) {\n return null;\n }\n return {\n laneId: row.laneId,\n laneKind: row.laneKind,\n updatedAt: row.updatedAt,\n watermark: {\n id: row.watermarkId,\n updatedAt: row.watermarkUpdatedAt,\n },\n };\n}\n\nfunction laneKeyString(input: { laneId: string; laneKind: string }): string {\n return `${input.laneKind}\\0${input.laneId}`;\n}\n\nfunction uniqueLaneKeys(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n): Array<{ laneId: string; laneKind: string }> {\n const seen = new Set<string>();\n const laneKeys: Array<{ laneId: string; laneKind: string }> = [];\n\n for (const lane of lanes) {\n const laneKey = containerSyncWatermarkLaneKey(lane);\n const key = laneKeyString(laneKey);\n if (seen.has(key)) {\n continue;\n }\n seen.add(key);\n laneKeys.push(laneKey);\n }\n\n return laneKeys;\n}\n\n// The ONE batched lane-select shape for this file: both row readers must\n// chunk, because a caller may pass unboundedly many lanes and each lane\n// predicate binds two variables.\nasync function selectLaneRowsInBatches<TRow>(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n buildLanePredicate: (laneKey: {\n laneId: string;\n laneKind: string;\n }) => SQL | undefined,\n selectBatch: (whereClause: SQL) => Promise<TRow[]>,\n): Promise<TRow[]> {\n const laneKeys = uniqueLaneKeys(lanes);\n const rows: TRow[] = [];\n\n for (\n let index = 0;\n index < laneKeys.length;\n index += LANE_SELECT_BATCH_SIZE\n ) {\n const lanePredicates = laneKeys\n .slice(index, index + LANE_SELECT_BATCH_SIZE)\n .map(buildLanePredicate)\n .filter((predicate): predicate is SQL => predicate !== undefined);\n const whereClause =\n lanePredicates.length === 1 ? lanePredicates[0] : or(...lanePredicates);\n if (!whereClause) {\n continue;\n }\n\n rows.push(...(await selectBatch(whereClause)));\n }\n\n return rows;\n}\n\nasync function selectWatermarkRows(\n execSql: ExecSql,\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n): Promise<SelectedContainerSyncWatermark[]> {\n await ensureSqlTables(execSql, containerSyncWatermarkTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n\n return selectLaneRowsInBatches(\n lanes,\n ({ laneId, laneKind }) =>\n and(\n eq(containerSyncWatermarks.laneKind, laneKind),\n eq(containerSyncWatermarks.laneId, laneId),\n ),\n (whereClause) =>\n db\n .select({\n laneId: containerSyncWatermarks.laneId,\n laneKind: containerSyncWatermarks.laneKind,\n updatedAt: containerSyncWatermarks.updatedAt,\n watermarkId: containerSyncWatermarks.watermarkId,\n watermarkUpdatedAt: containerSyncWatermarks.watermarkUpdatedAt,\n })\n .from(containerSyncWatermarks)\n .where(whereClause),\n );\n}\n\nasync function selectCheckRows(\n execSql: ExecSql,\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n): Promise<ContainerSyncLaneCheckRecord[]> {\n await ensureSqlTables(execSql, containerSyncWatermarkTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n\n return selectLaneRowsInBatches(\n lanes,\n ({ laneId, laneKind }) =>\n and(\n eq(containerSyncWatermarks.laneKind, laneKind),\n eq(containerSyncWatermarks.laneId, laneId),\n ),\n async (whereClause) =>\n (\n await db\n .select({\n checkedAt: containerSyncWatermarks.checkedAt,\n laneId: containerSyncWatermarks.laneId,\n laneKind: containerSyncWatermarks.laneKind,\n })\n .from(containerSyncWatermarks)\n .where(and(whereClause, isNotNull(containerSyncWatermarks.checkedAt)))\n ).flatMap(({ checkedAt, laneId, laneKind }) =>\n checkedAt === null ? [] : [{ checkedAt, laneId, laneKind }],\n ),\n );\n}\n\n/**\n * Per-lane container sync cursors and freshness markers, bound to one\n * executor and clock. Each method ensures its table and runs its own\n * mutation; a caller already inside a transaction composes\n * `deleteContainerWatermarksInTransaction` with the locked handle instead.\n */\nexport class ContainerSyncWatermarkRepository {\n private readonly execSql: ExecSql;\n private readonly now: () => Date;\n\n constructor(execSql: ExecSql, now: () => Date = () => new Date()) {\n this.execSql = execSql;\n this.now = now;\n }\n\n async loadWatermark(\n lane: ContainerSyncWatermarkLane,\n ): Promise<SyncWatermark | null> {\n const [record] = await this.loadWatermarkRecords([lane]);\n return record?.watermark ?? null;\n }\n\n async loadWatermarkRecords(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n ): Promise<Array<ContainerSyncWatermarkRecord | null>> {\n const rows = await selectWatermarkRows(this.execSql, lanes);\n const recordsByKey = new Map<string, ContainerSyncWatermarkRecord>();\n\n for (const row of rows) {\n const record = mapSelectedWatermarkRecord(row);\n if (record) recordsByKey.set(laneKeyString(record), record);\n }\n\n return lanes.map((lane) => {\n const laneKey = containerSyncWatermarkLaneKey(lane);\n return recordsByKey.get(laneKeyString(laneKey)) ?? null;\n });\n }\n\n async loadCheckRecords(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n ): Promise<Array<ContainerSyncLaneCheckRecord | null>> {\n const rows = await selectCheckRows(this.execSql, lanes);\n const recordsByKey = new Map<string, ContainerSyncLaneCheckRecord>();\n\n for (const row of rows) {\n recordsByKey.set(laneKeyString(row), row);\n }\n\n return lanes.map((lane) => {\n const laneKey = containerSyncWatermarkLaneKey(lane);\n return recordsByKey.get(laneKeyString(laneKey)) ?? null;\n });\n }\n\n markChecked(\n lane: ContainerSyncWatermarkLane,\n options?: ContainerSyncMarkerMutationOptions,\n ): Promise<boolean> {\n return this.recordLaneFetch(\n lane,\n { checked: true, watermark: null },\n options,\n );\n }\n\n saveWatermark(\n lane: ContainerSyncWatermarkLane,\n watermark: SyncWatermark,\n options?: ContainerSyncMarkerMutationOptions,\n ): Promise<boolean> {\n return this.recordLaneFetch(lane, { checked: false, watermark }, options);\n }\n\n /**\n * Upserts one lane row in a single statement: the cursor when `watermark`\n * is set and the freshness marker when `checked`; the other half is kept.\n */\n async recordLaneFetch(\n lane: ContainerSyncWatermarkLane,\n fetch: {\n readonly checked: boolean;\n readonly watermark: SyncWatermark | null;\n },\n options?: ContainerSyncMarkerMutationOptions,\n ): Promise<boolean> {\n if (!fetch.checked && !fetch.watermark) return true;\n await ensureSqlTables(this.execSql, containerSyncWatermarkTables);\n const now = this.now().toISOString();\n const changed = {\n ...(fetch.watermark\n ? {\n updatedAt: now,\n watermarkId: fetch.watermark.id,\n watermarkUpdatedAt: fetch.watermark.updatedAt,\n }\n : {}),\n ...(fetch.checked ? { checkedAt: now } : {}),\n };\n const record = async (\n db: ClientSQLiteDatabase | ClientSQLiteTransactionScope,\n ) => {\n await db\n .insert(containerSyncWatermarks)\n .values({ ...containerSyncWatermarkLaneKey(lane), ...changed })\n .onConflictDoUpdate({\n target: [\n containerSyncWatermarks.laneKind,\n containerSyncWatermarks.laneId,\n ],\n set: changed,\n })\n .run();\n };\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n if (!options?.stillCurrent) {\n await runtime.runMutation(record);\n return true;\n }\n return (await runtime.guardedTransaction(record, options.stillCurrent))\n .committed;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"containerSyncWatermarkPersistence.js","sourceRoot":"","sources":["containerSyncWatermarkPersistence.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,EAAY,MAAM,aAAa,CAAC;AACxE,OAAO,EACL,uBAAuB,EACvB,4BAA4B,GAC7B,MAAM,kDAAkD,CAAC;AAC1D,OAAO,EAGL,iCAAiC,GAClC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAgB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEvE,MAAM,qBAAqB,GAAG,kBAAkB,CAAC;AACjD,MAAM,0BAA0B,GAAG,uBAAuB,CAAC;AAC3D,MAAM,wBAAwB,GAAG,qBAAqB,CAAC;AACvD,MAAM,6BAA6B,GAAG,MAAM,CAAC;AAC7C,MAAM,+BAA+B,GAAG,SAAS,CAAC;AAClD,0EAA0E;AAC1E,kEAAkE;AAClE,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAsCnC,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,QAAuB,EACK,EAAE,CAAC,CAAC;IAChC,IAAI,EAAE,qBAAqB;IAC3B,QAAQ;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;aACpE,SAAS,CAAC;IACf,CAAC;CACF","sourcesContent":["import type { SyncWatermark } from \"@tearleads/validators/response\";\nimport { and, eq, inArray, isNotNull, or, type SQL } from \"drizzle-orm\";\nimport {\n containerSyncWatermarks,\n containerSyncWatermarkTables,\n} from \"../../sqlite/schema/containerSyncWatermarkSchema\";\nimport {\n type ClientSQLiteDatabase,\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../sqlite/sqlSchema\";\n\nconst CONTAINER_PARENT_LANE = \"container_parent\";\nconst CONTAINER_RESTORATION_LANE = \"container_restoration\";\nconst CONTAINER_DOCUMENTS_LANE = \"container_documents\";\nconst ROOT_CONTAINER_PARENT_LANE_ID = \"root\";\nconst CONTAINER_PARENT_LANE_ID_PREFIX = \"parent:\";\n// Each lane predicate binds two values, so keep batches comfortably under\n// SQLite's variable limit when a caller reads many lanes at once.\nconst LANE_SELECT_BATCH_SIZE = 100;\n\nexport type ContainerSyncWatermarkLane =\n | {\n kind: typeof CONTAINER_PARENT_LANE;\n parentId: string | null;\n }\n | {\n kind: typeof CONTAINER_DOCUMENTS_LANE | typeof CONTAINER_RESTORATION_LANE;\n containerId: string;\n };\n\ninterface SelectedContainerSyncWatermark {\n laneId: string;\n laneKind: string;\n updatedAt: string | null;\n cursorFormat: number;\n watermarkId: string | null;\n watermarkUpdatedAt: string | null;\n}\n\ninterface ContainerSyncWatermarkRecord {\n laneId: string;\n laneKind: string;\n updatedAt: string;\n watermark: SyncWatermark;\n}\n\nexport interface ContainerSyncLaneCheckRecord {\n checkedAt: string;\n laneId: string;\n laneKind: string;\n}\n\ninterface ContainerSyncMarkerMutationOptions {\n readonly stillCurrent?: (() => boolean) | undefined;\n}\n\nexport const containerParentSyncLane = (\n parentId: string | null,\n): ContainerSyncWatermarkLane => ({\n kind: CONTAINER_PARENT_LANE,\n parentId,\n});\n\nexport const containerContentsSyncLane = (\n containerId: string,\n): ContainerSyncWatermarkLane => ({\n kind: CONTAINER_DOCUMENTS_LANE,\n containerId,\n});\n\nexport function containerSyncWatermarkLaneKey(\n lane: ContainerSyncWatermarkLane,\n): {\n laneId: string;\n laneKind: string;\n} {\n switch (lane.kind) {\n case CONTAINER_PARENT_LANE:\n return {\n laneKind: lane.kind,\n laneId: containerParentLaneId(lane.parentId),\n };\n case CONTAINER_RESTORATION_LANE:\n case CONTAINER_DOCUMENTS_LANE:\n return {\n laneKind: lane.kind,\n laneId: lane.containerId,\n };\n }\n}\n\nfunction containerParentLaneId(parentId: string | null): string {\n return parentId === null\n ? ROOT_CONTAINER_PARENT_LANE_ID\n : `${CONTAINER_PARENT_LANE_ID_PREFIX}${parentId}`;\n}\n\n/**\n * The lane-state half of the container-delete cascade (cursor and freshness\n * marker share one row), statement-only so the caller can run it inside one\n * atomic transaction alongside the rest of the cascade.\n */\nexport async function deleteContainerWatermarksInTransaction(\n tx: ClientSQLiteDatabase | ClientSQLiteTransactionScope,\n containerIds: ReadonlyArray<string>,\n): Promise<void> {\n const uniqueContainerIds = Array.from(new Set(containerIds));\n if (uniqueContainerIds.length === 0) {\n return;\n }\n const parentLaneIds = uniqueContainerIds.map(containerParentLaneId);\n const documentLaneIds = uniqueContainerIds;\n await tx\n .delete(containerSyncWatermarks)\n .where(\n or(\n and(\n eq(containerSyncWatermarks.laneKind, CONTAINER_PARENT_LANE),\n inArray(containerSyncWatermarks.laneId, parentLaneIds),\n ),\n and(\n inArray(containerSyncWatermarks.laneKind, [\n CONTAINER_DOCUMENTS_LANE,\n CONTAINER_RESTORATION_LANE,\n ]),\n inArray(containerSyncWatermarks.laneId, documentLaneIds),\n ),\n ),\n )\n .run();\n}\n\nfunction mapSelectedWatermarkRecord(\n row: SelectedContainerSyncWatermark,\n): ContainerSyncWatermarkRecord | null {\n // A lane checked before any cursor was saved has no watermark yet.\n if (\n row.watermarkId === null ||\n row.watermarkUpdatedAt === null ||\n row.updatedAt === null ||\n (row.cursorFormat === 1 && row.watermarkId.includes(\"/\"))\n ) {\n return null;\n }\n return {\n laneId: row.laneId,\n laneKind: row.laneKind,\n updatedAt: row.updatedAt,\n watermark: {\n id: row.watermarkId,\n updatedAt: row.watermarkUpdatedAt,\n },\n };\n}\n\nfunction laneKeyString(input: { laneId: string; laneKind: string }): string {\n return `${input.laneKind}\\0${input.laneId}`;\n}\n\nfunction uniqueLaneKeys(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n): Array<{ laneId: string; laneKind: string }> {\n const seen = new Set<string>();\n const laneKeys: Array<{ laneId: string; laneKind: string }> = [];\n\n for (const lane of lanes) {\n const laneKey = containerSyncWatermarkLaneKey(lane);\n const key = laneKeyString(laneKey);\n if (seen.has(key)) {\n continue;\n }\n seen.add(key);\n laneKeys.push(laneKey);\n }\n\n return laneKeys;\n}\n\n// The ONE batched lane-select shape for this file: both row readers must\n// chunk, because a caller may pass unboundedly many lanes and each lane\n// predicate binds two variables.\nasync function selectLaneRowsInBatches<TRow>(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n buildLanePredicate: (laneKey: {\n laneId: string;\n laneKind: string;\n }) => SQL | undefined,\n selectBatch: (whereClause: SQL) => Promise<TRow[]>,\n): Promise<TRow[]> {\n const laneKeys = uniqueLaneKeys(lanes);\n const rows: TRow[] = [];\n\n for (\n let index = 0;\n index < laneKeys.length;\n index += LANE_SELECT_BATCH_SIZE\n ) {\n const lanePredicates = laneKeys\n .slice(index, index + LANE_SELECT_BATCH_SIZE)\n .map(buildLanePredicate)\n .filter((predicate): predicate is SQL => predicate !== undefined);\n const whereClause =\n lanePredicates.length === 1 ? lanePredicates[0] : or(...lanePredicates);\n if (!whereClause) {\n continue;\n }\n\n rows.push(...(await selectBatch(whereClause)));\n }\n\n return rows;\n}\n\nasync function selectWatermarkRows(\n execSql: ExecSql,\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n): Promise<SelectedContainerSyncWatermark[]> {\n await ensureSqlTables(execSql, containerSyncWatermarkTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n\n return selectLaneRowsInBatches(\n lanes,\n ({ laneId, laneKind }) =>\n and(\n eq(containerSyncWatermarks.laneKind, laneKind),\n eq(containerSyncWatermarks.laneId, laneId),\n ),\n (whereClause) =>\n db\n .select({\n laneId: containerSyncWatermarks.laneId,\n laneKind: containerSyncWatermarks.laneKind,\n updatedAt: containerSyncWatermarks.updatedAt,\n cursorFormat: containerSyncWatermarks.cursorFormat,\n watermarkId: containerSyncWatermarks.watermarkId,\n watermarkUpdatedAt: containerSyncWatermarks.watermarkUpdatedAt,\n })\n .from(containerSyncWatermarks)\n .where(whereClause),\n );\n}\n\nasync function selectCheckRows(\n execSql: ExecSql,\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n): Promise<ContainerSyncLaneCheckRecord[]> {\n await ensureSqlTables(execSql, containerSyncWatermarkTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n\n return selectLaneRowsInBatches(\n lanes,\n ({ laneId, laneKind }) =>\n and(\n eq(containerSyncWatermarks.laneKind, laneKind),\n eq(containerSyncWatermarks.laneId, laneId),\n ),\n async (whereClause) =>\n (\n await db\n .select({\n checkedAt: containerSyncWatermarks.checkedAt,\n laneId: containerSyncWatermarks.laneId,\n laneKind: containerSyncWatermarks.laneKind,\n })\n .from(containerSyncWatermarks)\n .where(and(whereClause, isNotNull(containerSyncWatermarks.checkedAt)))\n ).flatMap(({ checkedAt, laneId, laneKind }) =>\n checkedAt === null ? [] : [{ checkedAt, laneId, laneKind }],\n ),\n );\n}\n\n/**\n * Per-lane container sync cursors and freshness markers, bound to one\n * executor and clock. Each method ensures its table and runs its own\n * mutation; a caller already inside a transaction composes\n * `deleteContainerWatermarksInTransaction` with the locked handle instead.\n */\nexport class ContainerSyncWatermarkRepository {\n private readonly execSql: ExecSql;\n private readonly now: () => Date;\n\n constructor(execSql: ExecSql, now: () => Date = () => new Date()) {\n this.execSql = execSql;\n this.now = now;\n }\n\n async loadWatermark(\n lane: ContainerSyncWatermarkLane,\n ): Promise<SyncWatermark | null> {\n const [record] = await this.loadWatermarkRecords([lane]);\n return record?.watermark ?? null;\n }\n\n async loadWatermarkRecords(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n ): Promise<Array<ContainerSyncWatermarkRecord | null>> {\n const rows = await selectWatermarkRows(this.execSql, lanes);\n const recordsByKey = new Map<string, ContainerSyncWatermarkRecord>();\n\n for (const row of rows) {\n const record = mapSelectedWatermarkRecord(row);\n if (record) recordsByKey.set(laneKeyString(record), record);\n }\n\n return lanes.map((lane) => {\n const laneKey = containerSyncWatermarkLaneKey(lane);\n return recordsByKey.get(laneKeyString(laneKey)) ?? null;\n });\n }\n\n async loadCheckRecords(\n lanes: ReadonlyArray<ContainerSyncWatermarkLane>,\n ): Promise<Array<ContainerSyncLaneCheckRecord | null>> {\n const rows = await selectCheckRows(this.execSql, lanes);\n const recordsByKey = new Map<string, ContainerSyncLaneCheckRecord>();\n\n for (const row of rows) {\n recordsByKey.set(laneKeyString(row), row);\n }\n\n return lanes.map((lane) => {\n const laneKey = containerSyncWatermarkLaneKey(lane);\n return recordsByKey.get(laneKeyString(laneKey)) ?? null;\n });\n }\n\n markChecked(\n lane: ContainerSyncWatermarkLane,\n options?: ContainerSyncMarkerMutationOptions,\n ): Promise<boolean> {\n return this.recordLaneFetch(\n lane,\n { checked: true, watermark: null },\n options,\n );\n }\n\n saveWatermark(\n lane: ContainerSyncWatermarkLane,\n watermark: SyncWatermark,\n options?: ContainerSyncMarkerMutationOptions,\n ): Promise<boolean> {\n return this.recordLaneFetch(lane, { checked: false, watermark }, options);\n }\n\n /**\n * Upserts one lane row in a single statement: the cursor when `watermark`\n * is set and the freshness marker when `checked`; the other half is kept.\n */\n async recordLaneFetch(\n lane: ContainerSyncWatermarkLane,\n fetch: {\n readonly checked: boolean;\n readonly watermark: SyncWatermark | null;\n },\n options?: ContainerSyncMarkerMutationOptions,\n ): Promise<boolean> {\n if (!fetch.checked && !fetch.watermark) return true;\n await ensureSqlTables(this.execSql, containerSyncWatermarkTables);\n const now = this.now().toISOString();\n const changed = {\n ...(fetch.watermark\n ? {\n updatedAt: now,\n cursorFormat: 2,\n watermarkId: fetch.watermark.id,\n watermarkUpdatedAt: fetch.watermark.updatedAt,\n }\n : {}),\n ...(fetch.checked ? { checkedAt: now } : {}),\n };\n const record = async (\n db: ClientSQLiteDatabase | ClientSQLiteTransactionScope,\n ) => {\n await db\n .insert(containerSyncWatermarks)\n .values({ ...containerSyncWatermarkLaneKey(lane), ...changed })\n .onConflictDoUpdate({\n target: [\n containerSyncWatermarks.laneKind,\n containerSyncWatermarks.laneId,\n ],\n set: changed,\n })\n .run();\n };\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n if (!options?.stillCurrent) {\n await runtime.runMutation(record);\n return true;\n }\n return (await runtime.guardedTransaction(record, options.stillCurrent))\n .committed;\n }\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { DiscoveredDocumentInput } from "../../documents/documentSummary.js";
|
|
2
2
|
export interface DiscoveredDocumentCandidate extends DiscoveredDocumentInput {
|
|
3
|
+
readonly requiresCurrentHead?: true;
|
|
3
4
|
readonly listedContainerIds: readonly string[];
|
|
4
5
|
}
|
|
5
6
|
export declare function isDiscoveredDocumentCandidate(value: unknown): value is DiscoveredDocumentCandidate;
|
|
@@ -9,7 +9,9 @@ export function isDiscoveredDocumentCandidate(value) {
|
|
|
9
9
|
const level = readRecordValue(value, "effectiveAccessLevel");
|
|
10
10
|
const hash = readRecordValue(value, "accessStateHash");
|
|
11
11
|
const epoch = readRecordValue(value, "accessEpoch");
|
|
12
|
-
|
|
12
|
+
const fresh = readRecordValue(value, "requiresCurrentHead");
|
|
13
|
+
return ((fresh === undefined || fresh === true) &&
|
|
14
|
+
typeof readRecordValue(value, "documentId") === "string" &&
|
|
13
15
|
typeof readRecordValue(value, "containerId") === "string" &&
|
|
14
16
|
typeof readRecordValue(value, "createdAt") === "string" &&
|
|
15
17
|
typeof epoch === "number" &&
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentDiscoveryCandidate.js","sourceRoot":"","sources":["documentDiscoveryCandidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AAEpE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"documentDiscoveryCandidate.js","sourceRoot":"","sources":["documentDiscoveryCandidate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qCAAqC,CAAC;AAEpE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAO7D,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,CACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CACxE,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,6BAA6B,CAC3C,KAAc;IAEd,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACxC,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,EAAE,sBAAsB,CAAC,CAAC;IAC7D,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC;IACvD,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpD,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,EAAE,qBAAqB,CAAC,CAAC;IAC5D,OAAO,CACL,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,CAAC;QACvC,OAAO,eAAe,CAAC,KAAK,EAAE,YAAY,CAAC,KAAK,QAAQ;QACxD,OAAO,eAAe,CAAC,KAAK,EAAE,aAAa,CAAC,KAAK,QAAQ;QACzD,OAAO,eAAe,CAAC,KAAK,EAAE,WAAW,CAAC,KAAK,QAAQ;QACvD,OAAO,KAAK,KAAK,QAAQ;QACzB,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC;QAC3B,KAAK,GAAG,CAAC;QACT,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,CAAC;QACjE,CAAC,KAAK,KAAK,SAAS;YAClB,KAAK,KAAK,OAAO;YACjB,KAAK,KAAK,MAAM;YAChB,KAAK,KAAK,OAAO,CAAC;QACpB,aAAa,CAAC,eAAe,CAAC,KAAK,EAAE,oBAAoB,CAAC,CAAC;QAC3D,aAAa,CAAC,eAAe,CAAC,KAAK,EAAE,oBAAoB,CAAC,CAAC,CAC5D,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,4BAA4B,CAC1C,IAAY;IAEZ,MAAM,KAAK,GAAY,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACxC,IAAI,CAAC,6BAA6B,CAAC,KAAK,CAAC;QACvC,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IACpE,4EAA4E;IAC5E,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { isPlainObject } from \"@tearleads/validators/isPlainObject\";\nimport type { DiscoveredDocumentInput } from \"../../documents/documentSummary\";\nimport { readRecordValue } from \"../../shared/recordReaders\";\n\nexport interface DiscoveredDocumentCandidate extends DiscoveredDocumentInput {\n readonly requiresCurrentHead?: true;\n readonly listedContainerIds: readonly string[];\n}\n\nfunction isStringArray(value: unknown): value is string[] {\n return (\n Array.isArray(value) && value.every((item) => typeof item === \"string\")\n );\n}\n\nexport function isDiscoveredDocumentCandidate(\n value: unknown,\n): value is DiscoveredDocumentCandidate {\n if (!isPlainObject(value)) return false;\n const level = readRecordValue(value, \"effectiveAccessLevel\");\n const hash = readRecordValue(value, \"accessStateHash\");\n const epoch = readRecordValue(value, \"accessEpoch\");\n const fresh = readRecordValue(value, \"requiresCurrentHead\");\n return (\n (fresh === undefined || fresh === true) &&\n typeof readRecordValue(value, \"documentId\") === \"string\" &&\n typeof readRecordValue(value, \"containerId\") === \"string\" &&\n typeof readRecordValue(value, \"createdAt\") === \"string\" &&\n typeof epoch === \"number\" &&\n Number.isSafeInteger(epoch) &&\n epoch > 0 &&\n (hash === undefined || hash === null || typeof hash === \"string\") &&\n (level === undefined ||\n level === \"admin\" ||\n level === \"read\" ||\n level === \"write\") &&\n isStringArray(readRecordValue(value, \"linkedContainerIds\")) &&\n isStringArray(readRecordValue(value, \"listedContainerIds\"))\n );\n}\n\nexport function readStoredDiscoveryCandidate(\n json: string,\n): DiscoveredDocumentCandidate {\n const value: unknown = JSON.parse(json);\n if (!isDiscoveredDocumentCandidate(value))\n throw new Error(\"Stored document discovery candidate is invalid\");\n // Preserve property order: acknowledgements compare the exact stored input.\n return value;\n}\n"]}
|
|
@@ -25,7 +25,7 @@ export declare class DocumentDiscoveryEvidenceRepository {
|
|
|
25
25
|
private write;
|
|
26
26
|
begin(): Promise<number>;
|
|
27
27
|
isCurrent(generation: number): Promise<boolean>;
|
|
28
|
-
stage(inputs: readonly DiscoveredDocumentCandidate[], generation: number, removed?: readonly RemovedDiscoveryPlacement[]): Promise<boolean>;
|
|
28
|
+
stage(inputs: readonly DiscoveredDocumentCandidate[], generation: number, removed?: readonly RemovedDiscoveryPlacement[], conflictingDocumentIds?: ReadonlySet<string>): Promise<boolean>;
|
|
29
29
|
pending(containerIds: readonly string[], limit: number): Promise<readonly DiscoveredDocumentCandidate[]>;
|
|
30
30
|
defer(input: DiscoveredDocumentCandidate, generation: number): Promise<void>;
|
|
31
31
|
acknowledge(inputs: readonly DiscoveredDocumentCandidate[], generation: number): Promise<void>;
|
|
@@ -63,11 +63,11 @@ export class DocumentDiscoveryEvidenceRepository {
|
|
|
63
63
|
async isCurrent(generation) {
|
|
64
64
|
return isDocumentDiscoveryGenerationCurrent(await this.read(), generation);
|
|
65
65
|
}
|
|
66
|
-
stage(inputs, generation, removed = []) {
|
|
66
|
+
stage(inputs, generation, removed = [], conflictingDocumentIds = new Set()) {
|
|
67
67
|
return this.write(async (db) => {
|
|
68
68
|
if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))
|
|
69
69
|
return false;
|
|
70
|
-
if (inputs.length
|
|
70
|
+
if (!inputs.length && !removed.length && !conflictingDocumentIds.size)
|
|
71
71
|
return true;
|
|
72
72
|
await db.transaction(async (tx) => {
|
|
73
73
|
const rows = inputs.flatMap((input) => isDiscoveredDocumentCandidate(input)
|
|
@@ -87,7 +87,10 @@ export class DocumentDiscoveryEvidenceRepository {
|
|
|
87
87
|
// One multi-row upsert per chunk. SQLite applies a statement's rows
|
|
88
88
|
// in order, so a repeated placement updates the row the previous
|
|
89
89
|
// entry wrote, exactly as one statement per entry did.
|
|
90
|
-
|
|
90
|
+
// A later listing can replace a pending hint while its unlink is
|
|
91
|
+
// already behind the cursor. Keep the required current-head read.
|
|
92
|
+
const nextInput = sql `case when json_extract(${pending.inputJson}, '$.requiresCurrentHead') then json_set(excluded.input_json, '$.requiresCurrentHead', json('true')) else excluded.input_json end`;
|
|
93
|
+
const unchanged = sql `${pending.inputJson} = (${nextInput})`;
|
|
91
94
|
for (const chunk of chunked(rows, sqliteInsertBatchSize(pending))) {
|
|
92
95
|
await tx
|
|
93
96
|
.insert(pending)
|
|
@@ -97,7 +100,7 @@ export class DocumentDiscoveryEvidenceRepository {
|
|
|
97
100
|
set: {
|
|
98
101
|
accessEpoch: excludedValue(pending.accessEpoch),
|
|
99
102
|
generation: excludedValue(pending.generation),
|
|
100
|
-
inputJson:
|
|
103
|
+
inputJson: nextInput,
|
|
101
104
|
retryAt: sql `case when ${unchanged} then ${pending.retryAt} else 0 end`,
|
|
102
105
|
attempts: sql `case when ${unchanged} then ${pending.attempts} else 0 end`,
|
|
103
106
|
},
|
|
@@ -105,6 +108,15 @@ export class DocumentDiscoveryEvidenceRepository {
|
|
|
105
108
|
})
|
|
106
109
|
.run();
|
|
107
110
|
}
|
|
111
|
+
for (const ids of batches([...conflictingDocumentIds])) {
|
|
112
|
+
await tx
|
|
113
|
+
.update(pending)
|
|
114
|
+
.set({
|
|
115
|
+
inputJson: sql `json_set(${pending.inputJson}, '$.requiresCurrentHead', json('true'))`,
|
|
116
|
+
})
|
|
117
|
+
.where(and(inArray(pending.documentId, ids), lte(pending.generation, generation)))
|
|
118
|
+
.run();
|
|
119
|
+
}
|
|
108
120
|
// These are untrusted, unapplied hints only. Removing an existing local
|
|
109
121
|
// placement still requires the separate signed tombstone evidence gate.
|
|
110
122
|
for (const batch of remoteResetBatches(removed)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentDiscoveryEvidencePersistence.js","sourceRoot":"","sources":["documentDiscoveryEvidencePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,0BAA0B,GAC3B,MAAM,qDAAqD,CAAC;AAC7D,OAAO,EAAE,iCAAiC,EAAE,MAAM,8CAA8C,CAAC;AACjG,OAAO,EACL,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,iCAAiC,EAAE,MAAM,uCAAuC,CAAC;AAC1F,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAEL,eAAe,EACf,wBAAwB,GACzB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAC;AAC7F,OAAO,EAAE,yBAAyB,EAAE,MAAM,8CAA8C,CAAC;AACzF,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E,IAAI,CAAC,cAAc;YACnC,YAAY,EAAE,IAAI,CAAC,eAAe;YAClC,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;SACxE,CAAC;QACF,MAAM,EAAE;aACL,MAAM,CAAC,KAAK,CAAC;aACb,MAAM,CAAC,GAAG,CAAC;aACX,kBAAkB,CAAC;YAClB,MAAM,EAAE,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,cAAc,CAAC;YAChD,GAAG,EAAE,GAAG;YACR,QAAQ,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC;SACnD,CAAC;aACD,GAAG,EAAE,CAAC;IACX,CAAC;AACH,CAAC","sourcesContent":["import { and, asc, eq, inArray, lte, min, or, sql } from \"drizzle-orm\";\nimport { uniqueSortedStrings } from \"../../documents/shared/readers\";\nimport { readStringArray } from \"../../shared/recordReaders\";\nimport {\n documentDiscoveryHeads,\n documentDiscoverySequence,\n pendingDocumentDiscoveries,\n} from \"../../sqlite/schema/documentDiscoveryEvidenceSchema\";\nimport { documentContainerProjectionTables } from \"../../sqlite/schema/documentProjectionSchema\";\nimport {\n remoteResetBatches,\n sqliteInsertBatchSize,\n} from \"../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { excludedValue } from \"../../sqlite/sqliteUpsert\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../sqlite/sqlSchema\";\nimport { KeyingCheckpointRepository } from \"../keyingCheckpoints/keyingCheckpointRepository\";\nimport { heldTombstoneRetryDelayMs } from \"./containerDocumentTombstoneHoldsPersistence\";\nimport {\n type DiscoveredDocumentCandidate,\n isDiscoveredDocumentCandidate,\n readStoredDiscoveryCandidate,\n} from \"./documentDiscoveryCandidate\";\nimport { isDocumentDiscoveryGenerationCurrent } from \"./documentDiscoveryReset\";\nimport { placementPairsMatch } from \"./internal/containerDocumentTombstoneHolds\";\n\nexport type { DiscoveredDocumentCandidate } from \"./documentDiscoveryCandidate\";\nexport { clearDocumentDiscoveryEvidenceOnRemoteReset } from \"./documentDiscoveryReset\";\nexport interface CachedDiscoveryHead {\n readonly organizationId: string | null;\n readonly accessEpoch: number;\n readonly accessStateHash?: string;\n readonly linkedContainerIds: readonly string[];\n}\ninterface RemovedDiscoveryPlacement {\n readonly containerId: string;\n readonly documentId: string;\n}\n\nconst pending = pendingDocumentDiscoveries;\nconst heads = documentDiscoveryHeads;\nconst ID_BATCH_SIZE = 400;\nconst batches = (ids: readonly string[]) =>\n Array.from({ length: Math.ceil(ids.length / ID_BATCH_SIZE) }, (_, i) =>\n ids.slice(i * ID_BATCH_SIZE, (i + 1) * ID_BATCH_SIZE),\n );\nconst chunked = <T>(rows: readonly T[], size: number): T[][] =>\n Array.from({ length: Math.ceil(rows.length / size) }, (_, i) =>\n rows.slice(i * size, (i + 1) * size),\n );\nconst identity = (input: DiscoveredDocumentCandidate) =>\n and(\n eq(pending.containerId, input.containerId),\n eq(pending.documentId, input.documentId),\n eq(pending.inputJson, JSON.stringify(input)),\n );\n\n/**\n * Untrusted document discovery evidence bound to one executor and clock. The\n * clock (epoch milliseconds, like the `retryAt` column) schedules and reports\n * pending-discovery retries.\n */\nexport class DocumentDiscoveryEvidenceRepository {\n constructor(\n private readonly execSql: ExecSql,\n private readonly now: () => number = Date.now,\n ) {}\n\n private async read() {\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n return getClientSQLitePersistenceRuntime(this.execSql).db;\n }\n private write<T>(\n operation: (\n db: ReturnType<typeof getClientSQLitePersistenceRuntime>[\"db\"],\n ) => Promise<T>,\n ): Promise<T> {\n return runSerializedSqlMutation(this.execSql, async (locked) => {\n await ensureSqlTables(locked, documentContainerProjectionTables);\n return operation(getClientSQLitePersistenceRuntime(locked).db);\n });\n }\n async begin(): Promise<number> {\n let generation = 0;\n await this.write(async (db) => {\n const [row] = await db\n .insert(documentDiscoverySequence)\n .values({ id: \"requests\", generation: 1 })\n .onConflictDoUpdate({\n target: documentDiscoverySequence.id,\n set: {\n generation: sql`${documentDiscoverySequence.generation} + 1`,\n },\n })\n .returning({ generation: documentDiscoverySequence.generation });\n if (!row || !Number.isSafeInteger(row.generation))\n throw new Error(\"Document discovery generation is exhausted\");\n generation = row.generation;\n });\n return generation;\n }\n async isCurrent(generation: number): Promise<boolean> {\n return isDocumentDiscoveryGenerationCurrent(await this.read(), generation);\n }\n stage(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n removed: readonly RemovedDiscoveryPlacement[] = [],\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))\n return false;\n if (inputs.length === 0 && removed.length === 0) return true;\n await db.transaction(async (tx) => {\n const rows = inputs.flatMap((input) =>\n isDiscoveredDocumentCandidate(input)\n ? input.listedContainerIds.map((containerId) => ({\n containerId,\n documentId: input.documentId,\n accessEpoch: input.accessEpoch,\n generation,\n inputJson: JSON.stringify({\n ...input,\n containerId,\n listedContainerIds: [containerId],\n }),\n retryAt: 0,\n }))\n : [],\n );\n // One multi-row upsert per chunk. SQLite applies a statement's rows\n // in order, so a repeated placement updates the row the previous\n // entry wrote, exactly as one statement per entry did.\n const unchanged = sql`${pending.inputJson} = excluded.input_json`;\n for (const chunk of chunked(rows, sqliteInsertBatchSize(pending))) {\n await tx\n .insert(pending)\n .values(chunk)\n .onConflictDoUpdate({\n target: [pending.containerId, pending.documentId],\n set: {\n accessEpoch: excludedValue(pending.accessEpoch),\n generation: excludedValue(pending.generation),\n inputJson: excludedValue(pending.inputJson),\n retryAt: sql`case when ${unchanged} then ${pending.retryAt} else 0 end`,\n attempts: sql`case when ${unchanged} then ${pending.attempts} else 0 end`,\n },\n setWhere: lte(pending.generation, generation),\n })\n .run();\n }\n // These are untrusted, unapplied hints only. Removing an existing local\n // placement still requires the separate signed tombstone evidence gate.\n for (const batch of remoteResetBatches(removed)) {\n await tx\n .delete(pending)\n .where(\n and(\n placementPairsMatch(\n batch,\n pending.documentId,\n pending.containerId,\n ),\n lte(pending.generation, generation),\n ),\n )\n .run();\n }\n });\n return true;\n });\n }\n async pending(\n containerIds: readonly string[],\n limit: number,\n ): Promise<readonly DiscoveredDocumentCandidate[]> {\n const db = await this.read();\n const result: DiscoveredDocumentCandidate[] = [];\n for (const batch of batches(containerIds)) {\n if (result.length >= limit) break;\n const rows = await db\n .select({ inputJson: pending.inputJson })\n .from(pending)\n .where(\n and(\n inArray(pending.containerId, [...batch]),\n lte(pending.retryAt, this.now()),\n ),\n )\n .orderBy(asc(pending.retryAt), asc(pending.documentId))\n .limit(limit - result.length);\n result.push(\n ...rows.map((row) => readStoredDiscoveryCandidate(row.inputJson)),\n );\n }\n return result;\n }\n defer(input: DiscoveredDocumentCandidate, generation: number): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n const [row] = await db\n .select({ attempts: pending.attempts })\n .from(pending)\n .where(identity(input))\n .limit(1);\n if (!row) return;\n const attempts = Math.min(row.attempts + 1, 8);\n await db\n .update(pending)\n .set({\n attempts,\n retryAt: this.now() + heldTombstoneRetryDelayMs(attempts),\n })\n .where(identity(input))\n .run();\n });\n }\n acknowledge(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n ): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n await db.transaction(async (tx) => {\n for (const batch of remoteResetBatches(inputs))\n await tx\n .delete(pending)\n .where(or(...batch.map(identity)))\n .run();\n });\n });\n }\n async hasPending(containerIds: readonly string[]): Promise<boolean> {\n const db = await this.read();\n for (const batch of batches(containerIds)) {\n if (\n (\n await db\n .select({ documentId: pending.documentId })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]))\n .limit(1)\n ).length\n )\n return true;\n }\n return false;\n }\n async retryDelay(containerIds: readonly string[]): Promise<number | null> {\n const db = await this.read();\n let due: number | null = null;\n for (const batch of batches(containerIds)) {\n const [row] = await db\n .select({ due: min(pending.retryAt) })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]));\n if (row?.due !== null && row?.due !== undefined)\n due = Math.min(due ?? row.due, row.due);\n }\n return due === null ? null : Math.max(0, due - this.now());\n }\n async loadHead(\n documentId: string,\n manifestHash: string | null | undefined,\n ): Promise<CachedDiscoveryHead | null> {\n if (\n await new KeyingCheckpointRepository(\n this.execSql,\n ).loadDocumentPurgeCheckpoint(documentId)\n )\n return {\n organizationId: null,\n accessEpoch: Number.MAX_SAFE_INTEGER,\n linkedContainerIds: [],\n };\n if (!manifestHash) return null;\n const db = await this.read();\n const [row] = await db\n .select()\n .from(heads)\n .where(\n and(\n eq(heads.documentId, documentId),\n eq(heads.manifestHash, manifestHash),\n ),\n )\n .limit(1);\n if (!row) return null;\n // A different verifier (for example tombstone removal) can advance the\n // signed checkpoint without updating documents.accessEpoch or this cache.\n // Old-organization pins survive recovery; only the cached signed head's\n // organization can authorize reuse. Missing pins require full verification.\n const checkpoint = await new KeyingCheckpointRepository(\n this.execSql,\n ).loadAccessManifestCheckpoint(\"document\", row.organizationId, documentId);\n if (\n checkpoint?.manifestHash !== row.manifestHash ||\n checkpoint.epoch !== row.accessEpoch\n )\n return null;\n return {\n organizationId: row.organizationId,\n accessEpoch: row.accessEpoch,\n accessStateHash: row.manifestHash,\n linkedContainerIds: readStringArray(\n JSON.parse(row.linksJson),\n \"stored discovery links\",\n ),\n };\n }\n saveHead(\n documentId: string,\n head: CachedDiscoveryHead,\n generation: number,\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (\n !head.accessStateHash ||\n !head.organizationId ||\n !(await isDocumentDiscoveryGenerationCurrent(db, generation))\n )\n return false;\n const row = {\n documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(head.linkedContainerIds),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n return true;\n });\n }\n}\n\n/**\n * A document head this client signed, already checked against the server's\n * create acknowledgement and pinned as its access-manifest checkpoint in the\n * same transaction. It is the same signed head a discovery projection would\n * verify, so it seeds the exact-head cache; `loadHead` still reuses it only\n * while that checkpoint matches.\n */\nexport interface AuthoredDiscoveryHead {\n readonly documentId: string;\n readonly organizationId: string;\n readonly accessEpoch: number;\n readonly accessStateHash: string;\n readonly linkedContainerIds: readonly string[];\n}\n\n/** Writes authored heads inside the caller's serialized write transaction. */\nexport async function primeAuthoredDocumentDiscoveryHeads(\n execSql: ExecSql,\n authored: readonly AuthoredDiscoveryHead[],\n): Promise<void> {\n const db = getClientSQLitePersistenceRuntime(execSql).db;\n for (const head of authored) {\n const row = {\n documentId: head.documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(uniqueSortedStrings(head.linkedContainerIds)),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"documentDiscoveryEvidencePersistence.js","sourceRoot":"","sources":["documentDiscoveryEvidencePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,0B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AAM,CAAC,GAAG,CAAC;aACX,kBAAkB,CAAC;YAClB,MAAM,EAAE,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,cAAc,CAAC;YAChD,GAAG,EAAE,GAAG;YACR,QAAQ,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC;SACnD,CAAC;aACD,GAAG,EAAE,CAAC;IACX,CAAC;AACH,CAAC","sourcesContent":["import { and, asc, eq, inArray, lte, min, or, sql } from \"drizzle-orm\";\nimport { uniqueSortedStrings } from \"../../documents/shared/readers\";\nimport { readStringArray } from \"../../shared/recordReaders\";\nimport {\n documentDiscoveryHeads,\n documentDiscoverySequence,\n pendingDocumentDiscoveries,\n} from \"../../sqlite/schema/documentDiscoveryEvidenceSchema\";\nimport { documentContainerProjectionTables } from \"../../sqlite/schema/documentProjectionSchema\";\nimport {\n remoteResetBatches,\n sqliteInsertBatchSize,\n} from \"../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { excludedValue } from \"../../sqlite/sqliteUpsert\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../sqlite/sqlSchema\";\nimport { KeyingCheckpointRepository } from \"../keyingCheckpoints/keyingCheckpointRepository\";\nimport { heldTombstoneRetryDelayMs } from \"./containerDocumentTombstoneHoldsPersistence\";\nimport {\n type DiscoveredDocumentCandidate,\n isDiscoveredDocumentCandidate,\n readStoredDiscoveryCandidate,\n} from \"./documentDiscoveryCandidate\";\nimport { isDocumentDiscoveryGenerationCurrent } from \"./documentDiscoveryReset\";\nimport { placementPairsMatch } from \"./internal/containerDocumentTombstoneHolds\";\n\nexport type { DiscoveredDocumentCandidate } from \"./documentDiscoveryCandidate\";\nexport { clearDocumentDiscoveryEvidenceOnRemoteReset } from \"./documentDiscoveryReset\";\nexport interface CachedDiscoveryHead {\n readonly organizationId: string | null;\n readonly accessEpoch: number;\n readonly accessStateHash?: string;\n readonly linkedContainerIds: readonly string[];\n}\ninterface RemovedDiscoveryPlacement {\n readonly containerId: string;\n readonly documentId: string;\n}\n\nconst pending = pendingDocumentDiscoveries;\nconst heads = documentDiscoveryHeads;\nconst ID_BATCH_SIZE = 400;\nconst batches = (ids: readonly string[]) =>\n Array.from({ length: Math.ceil(ids.length / ID_BATCH_SIZE) }, (_, i) =>\n ids.slice(i * ID_BATCH_SIZE, (i + 1) * ID_BATCH_SIZE),\n );\nconst chunked = <T>(rows: readonly T[], size: number): T[][] =>\n Array.from({ length: Math.ceil(rows.length / size) }, (_, i) =>\n rows.slice(i * size, (i + 1) * size),\n );\nconst identity = (input: DiscoveredDocumentCandidate) =>\n and(\n eq(pending.containerId, input.containerId),\n eq(pending.documentId, input.documentId),\n eq(pending.inputJson, JSON.stringify(input)),\n );\n\n/**\n * Untrusted document discovery evidence bound to one executor and clock. The\n * clock (epoch milliseconds, like the `retryAt` column) schedules and reports\n * pending-discovery retries.\n */\nexport class DocumentDiscoveryEvidenceRepository {\n constructor(\n private readonly execSql: ExecSql,\n private readonly now: () => number = Date.now,\n ) {}\n\n private async read() {\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n return getClientSQLitePersistenceRuntime(this.execSql).db;\n }\n private write<T>(\n operation: (\n db: ReturnType<typeof getClientSQLitePersistenceRuntime>[\"db\"],\n ) => Promise<T>,\n ): Promise<T> {\n return runSerializedSqlMutation(this.execSql, async (locked) => {\n await ensureSqlTables(locked, documentContainerProjectionTables);\n return operation(getClientSQLitePersistenceRuntime(locked).db);\n });\n }\n async begin(): Promise<number> {\n let generation = 0;\n await this.write(async (db) => {\n const [row] = await db\n .insert(documentDiscoverySequence)\n .values({ id: \"requests\", generation: 1 })\n .onConflictDoUpdate({\n target: documentDiscoverySequence.id,\n set: {\n generation: sql`${documentDiscoverySequence.generation} + 1`,\n },\n })\n .returning({ generation: documentDiscoverySequence.generation });\n if (!row || !Number.isSafeInteger(row.generation))\n throw new Error(\"Document discovery generation is exhausted\");\n generation = row.generation;\n });\n return generation;\n }\n async isCurrent(generation: number): Promise<boolean> {\n return isDocumentDiscoveryGenerationCurrent(await this.read(), generation);\n }\n stage(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n removed: readonly RemovedDiscoveryPlacement[] = [],\n conflictingDocumentIds: ReadonlySet<string> = new Set(),\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))\n return false;\n if (!inputs.length && !removed.length && !conflictingDocumentIds.size)\n return true;\n await db.transaction(async (tx) => {\n const rows = inputs.flatMap((input) =>\n isDiscoveredDocumentCandidate(input)\n ? input.listedContainerIds.map((containerId) => ({\n containerId,\n documentId: input.documentId,\n accessEpoch: input.accessEpoch,\n generation,\n inputJson: JSON.stringify({\n ...input,\n containerId,\n listedContainerIds: [containerId],\n }),\n retryAt: 0,\n }))\n : [],\n );\n // One multi-row upsert per chunk. SQLite applies a statement's rows\n // in order, so a repeated placement updates the row the previous\n // entry wrote, exactly as one statement per entry did.\n // A later listing can replace a pending hint while its unlink is\n // already behind the cursor. Keep the required current-head read.\n const nextInput = sql`case when json_extract(${pending.inputJson}, '$.requiresCurrentHead') then json_set(excluded.input_json, '$.requiresCurrentHead', json('true')) else excluded.input_json end`;\n const unchanged = sql`${pending.inputJson} = (${nextInput})`;\n for (const chunk of chunked(rows, sqliteInsertBatchSize(pending))) {\n await tx\n .insert(pending)\n .values(chunk)\n .onConflictDoUpdate({\n target: [pending.containerId, pending.documentId],\n set: {\n accessEpoch: excludedValue(pending.accessEpoch),\n generation: excludedValue(pending.generation),\n inputJson: nextInput,\n retryAt: sql`case when ${unchanged} then ${pending.retryAt} else 0 end`,\n attempts: sql`case when ${unchanged} then ${pending.attempts} else 0 end`,\n },\n setWhere: lte(pending.generation, generation),\n })\n .run();\n }\n for (const ids of batches([...conflictingDocumentIds])) {\n await tx\n .update(pending)\n .set({\n inputJson: sql`json_set(${pending.inputJson}, '$.requiresCurrentHead', json('true'))`,\n })\n .where(\n and(\n inArray(pending.documentId, ids),\n lte(pending.generation, generation),\n ),\n )\n .run();\n }\n // These are untrusted, unapplied hints only. Removing an existing local\n // placement still requires the separate signed tombstone evidence gate.\n for (const batch of remoteResetBatches(removed)) {\n await tx\n .delete(pending)\n .where(\n and(\n placementPairsMatch(\n batch,\n pending.documentId,\n pending.containerId,\n ),\n lte(pending.generation, generation),\n ),\n )\n .run();\n }\n });\n return true;\n });\n }\n async pending(\n containerIds: readonly string[],\n limit: number,\n ): Promise<readonly DiscoveredDocumentCandidate[]> {\n const db = await this.read();\n const result: DiscoveredDocumentCandidate[] = [];\n for (const batch of batches(containerIds)) {\n if (result.length >= limit) break;\n const rows = await db\n .select({ inputJson: pending.inputJson })\n .from(pending)\n .where(\n and(\n inArray(pending.containerId, [...batch]),\n lte(pending.retryAt, this.now()),\n ),\n )\n .orderBy(asc(pending.retryAt), asc(pending.documentId))\n .limit(limit - result.length);\n result.push(\n ...rows.map((row) => readStoredDiscoveryCandidate(row.inputJson)),\n );\n }\n return result;\n }\n defer(input: DiscoveredDocumentCandidate, generation: number): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n const [row] = await db\n .select({ attempts: pending.attempts })\n .from(pending)\n .where(identity(input))\n .limit(1);\n if (!row) return;\n const attempts = Math.min(row.attempts + 1, 8);\n await db\n .update(pending)\n .set({\n attempts,\n retryAt: this.now() + heldTombstoneRetryDelayMs(attempts),\n })\n .where(identity(input))\n .run();\n });\n }\n acknowledge(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n ): Promise<void> {\n return this.write(async (db) => {\n if (!(await isDocumentDiscoveryGenerationCurrent(db, generation))) return;\n await db.transaction(async (tx) => {\n for (const batch of remoteResetBatches(inputs))\n await tx\n .delete(pending)\n .where(or(...batch.map(identity)))\n .run();\n });\n });\n }\n async hasPending(containerIds: readonly string[]): Promise<boolean> {\n const db = await this.read();\n for (const batch of batches(containerIds)) {\n if (\n (\n await db\n .select({ documentId: pending.documentId })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]))\n .limit(1)\n ).length\n )\n return true;\n }\n return false;\n }\n async retryDelay(containerIds: readonly string[]): Promise<number | null> {\n const db = await this.read();\n let due: number | null = null;\n for (const batch of batches(containerIds)) {\n const [row] = await db\n .select({ due: min(pending.retryAt) })\n .from(pending)\n .where(inArray(pending.containerId, [...batch]));\n if (row?.due !== null && row?.due !== undefined)\n due = Math.min(due ?? row.due, row.due);\n }\n return due === null ? null : Math.max(0, due - this.now());\n }\n async loadHead(\n documentId: string,\n manifestHash: string | null | undefined,\n ): Promise<CachedDiscoveryHead | null> {\n if (\n await new KeyingCheckpointRepository(\n this.execSql,\n ).loadDocumentPurgeCheckpoint(documentId)\n )\n return {\n organizationId: null,\n accessEpoch: Number.MAX_SAFE_INTEGER,\n linkedContainerIds: [],\n };\n if (!manifestHash) return null;\n const db = await this.read();\n const [row] = await db\n .select()\n .from(heads)\n .where(\n and(\n eq(heads.documentId, documentId),\n eq(heads.manifestHash, manifestHash),\n ),\n )\n .limit(1);\n if (!row) return null;\n // A different verifier (for example tombstone removal) can advance the\n // signed checkpoint without updating documents.accessEpoch or this cache.\n // Old-organization pins survive recovery; only the cached signed head's\n // organization can authorize reuse. Missing pins require full verification.\n const checkpoint = await new KeyingCheckpointRepository(\n this.execSql,\n ).loadAccessManifestCheckpoint(\"document\", row.organizationId, documentId);\n if (\n checkpoint?.manifestHash !== row.manifestHash ||\n checkpoint.epoch !== row.accessEpoch\n )\n return null;\n return {\n organizationId: row.organizationId,\n accessEpoch: row.accessEpoch,\n accessStateHash: row.manifestHash,\n linkedContainerIds: readStringArray(\n JSON.parse(row.linksJson),\n \"stored discovery links\",\n ),\n };\n }\n saveHead(\n documentId: string,\n head: CachedDiscoveryHead,\n generation: number,\n ): Promise<boolean> {\n return this.write(async (db) => {\n if (\n !head.accessStateHash ||\n !head.organizationId ||\n !(await isDocumentDiscoveryGenerationCurrent(db, generation))\n )\n return false;\n const row = {\n documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(head.linkedContainerIds),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n return true;\n });\n }\n}\n\n/**\n * A document head this client signed, already checked against the server's\n * create acknowledgement and pinned as its access-manifest checkpoint in the\n * same transaction. It is the same signed head a discovery projection would\n * verify, so it seeds the exact-head cache; `loadHead` still reuses it only\n * while that checkpoint matches.\n */\nexport interface AuthoredDiscoveryHead {\n readonly documentId: string;\n readonly organizationId: string;\n readonly accessEpoch: number;\n readonly accessStateHash: string;\n readonly linkedContainerIds: readonly string[];\n}\n\n/** Writes authored heads inside the caller's serialized write transaction. */\nexport async function primeAuthoredDocumentDiscoveryHeads(\n execSql: ExecSql,\n authored: readonly AuthoredDiscoveryHead[],\n): Promise<void> {\n const db = getClientSQLitePersistenceRuntime(execSql).db;\n for (const head of authored) {\n const row = {\n documentId: head.documentId,\n organizationId: head.organizationId,\n manifestHash: head.accessStateHash,\n accessEpoch: head.accessEpoch,\n linksJson: JSON.stringify(uniqueSortedStrings(head.linkedContainerIds)),\n };\n await db\n .insert(heads)\n .values(row)\n .onConflictDoUpdate({\n target: [heads.documentId, heads.organizationId],\n set: row,\n setWhere: lte(heads.accessEpoch, head.accessEpoch),\n })\n .run();\n }\n}\n"]}
|
|
@@ -41,6 +41,17 @@ export const localSchemaMigrations = [
|
|
|
41
41
|
},
|
|
42
42
|
version: 3,
|
|
43
43
|
},
|
|
44
|
+
{
|
|
45
|
+
name: "replay_commit_ordered_discovery",
|
|
46
|
+
up: async (execSql, context) => {
|
|
47
|
+
if (!(await context.hasTable("container_sync_watermarks")))
|
|
48
|
+
return;
|
|
49
|
+
// Older SDKs could checkpoint a publication before reconciling an
|
|
50
|
+
// unlink or a restored subtree. Legacy UUID cursors already replay.
|
|
51
|
+
await execSql("ALTER TABLE container_sync_watermarks ADD COLUMN cursor_format INTEGER NOT NULL DEFAULT 1");
|
|
52
|
+
},
|
|
53
|
+
version: 4,
|
|
54
|
+
},
|
|
44
55
|
];
|
|
45
56
|
const STEP_NAME_PATTERN = /^[a-z0-9_]+$/;
|
|
46
57
|
/** Rejects a registry whose versions are not consecutive from 2. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localSchemaMigrations.js","sourceRoot":"","sources":["localSchemaMigrations.ts"],"names":[],"mappings":"AA0BA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAoB;IACpD,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,CAAC;CACX,CAAC;AAEF,MAAM,qBAAqB,GAAG,yCAAyC,CAAC;AAExE;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAwC;IACxE;QACE,wEAAwE;QACxE,oEAAoE;QACpE,IAAI,EAAE,iCAAiC;QACvC,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,CAAC,MAAM,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC;gBAAE,OAAO;YAC7D,KAAK,MAAM,MAAM,IAAI,CAAC,oBAAoB,EAAE,0BAA0B,CAAC,EAAE,CAAC;gBACxE,MAAM,OAAO,CACX,gBAAgB,qBAAqB,kBAAkB,MAAM,GAAG,CACjE,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,EAAE,CAAC;KACX;IACD;QACE,wEAAwE;QACxE,sEAAsE;QACtE,0EAA0E;QAC1E,wCAAwC;QACxC,IAAI,EAAE,mCAAmC;QACzC,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;YACpB,MAAM,MAAM,GAAG,MAAM,OAAO,CAC1B,oFAAoF,CACrF,CAAC;YACF,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO;YAChC,MAAM,OAAO,CACX,2DAA2D,CAC5D,CAAC;QACJ,CAAC;QACD,OAAO,EAAE,CAAC;KACX;CACF,CAAC;AAEF,MAAM,iBAAiB,GAAG,cAAc,CAAC;AAEzC,oEAAoE;AACpE,MAAM,UAAU,2BAA2B,CACzC,UAA+C;IAE/C,KAAK,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,IAAI,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC;QACtD,MAAM,QAAQ,GAAG,qBAAqB,CAAC,OAAO,GAAG,KAAK,GAAG,CAAC,CAAC;QAC3D,IAAI,SAAS,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CACb,0BAA0B,SAAS,CAAC,IAAI,gBAAgB,SAAS,CAAC,OAAO,cAAc,QAAQ,EAAE,CAClG,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC;YAC5C,MAAM,IAAI,KAAK,CACb,0BAA0B,SAAS,CAAC,OAAO,0BAA0B,CACtE,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,gBAAgB,CAC9B,UAA+C;IAE/C,OAAO;QACL,qBAAqB;QACrB,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;KAC9D,CAAC;AACJ,CAAC","sourcesContent":["import type { ExecSql } from \"../sqlExec\";\n\nexport interface LocalSchemaMigrationContext {\n /**\n * Whether a table exists. Features create their tables lazily, so a\n * migration must skip a table this database never created: the table's\n * ensure creates it later in the current shape.\n */\n hasTable(name: string): Promise<boolean>;\n}\n\nexport interface LocalSchemaStep {\n readonly version: number;\n readonly name: string;\n}\n\nexport interface LocalSchemaMigration extends LocalSchemaStep {\n /**\n * Upgrades the local database from `version - 1` to `version`. It runs on\n * the locked executor inside the migration savepoint, so it must not BEGIN,\n * COMMIT, or change `PRAGMA foreign_keys`. Write it against the shape at\n * `version - 1`, never by importing current table definitions.\n */\n up(execSql: ExecSql, context: LocalSchemaMigrationContext): Promise<void>;\n}\n\n/**\n * Version 1 is the shape the SDK's table definitions created when local\n * migrations began. It has no steps: a database from before the ledger is\n * recorded at this version, and an older shape still fails its tables'\n * required-column check.\n */\nexport const LOCAL_SCHEMA_BASELINE: LocalSchemaStep = {\n name: \"baseline\",\n version: 1,\n};\n\nconst DIRECTORY_USERS_TABLE = \"organization_read_model_directory_users\";\n\n/**\n * Ordered and append-only: version 2 first, each one after the last. Never\n * edit or remove a shipped step; a correction is a new step.\n */\nexport const localSchemaMigrations: ReadonlyArray<LocalSchemaMigration> = [\n {\n // Directory rows carry key fingerprints only; public keys come from the\n // trusted identity store. Neither column is indexed or constrained.\n name: \"drop_directory_user_public_keys\",\n up: async (execSql, context) => {\n if (!(await context.hasTable(DIRECTORY_USERS_TABLE))) return;\n for (const column of [\"signing_public_key\", \"encapsulation_public_key\"]) {\n await execSql(\n `ALTER TABLE \"${DIRECTORY_USERS_TABLE}\" DROP COLUMN \"${column}\"`,\n );\n }\n },\n version: 2,\n },\n {\n // No reader ever used a recorded failure's HTTP status; the message and\n // attempt time explain a blocked document. The column is unindexed. A\n // documents table without it (never created, or not this SDK's table in a\n // restored backup) has nothing to drop.\n name: \"drop_document_sync_failure_status\",\n up: async (execSql) => {\n const column = await execSql(\n \"SELECT name FROM pragma_table_info('documents') WHERE name = 'sync_failure_status'\",\n );\n if (column.length === 0) return;\n await execSql(\n 'ALTER TABLE \"documents\" DROP COLUMN \"sync_failure_status\"',\n );\n },\n version: 3,\n },\n];\n\nconst STEP_NAME_PATTERN = /^[a-z0-9_]+$/;\n\n/** Rejects a registry whose versions are not consecutive from 2. */\nexport function assertLocalSchemaMigrations(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): void {\n for (const [index, migration] of migrations.entries()) {\n const expected = LOCAL_SCHEMA_BASELINE.version + index + 1;\n if (migration.version !== expected) {\n throw new Error(\n `Local schema migration ${migration.name} has version ${migration.version}; expected ${expected}`,\n );\n }\n if (!STEP_NAME_PATTERN.test(migration.name)) {\n throw new Error(\n `Local schema migration ${migration.version} needs a snake_case name`,\n );\n }\n }\n}\n\n/** The steps a database at the newest version has recorded, in order. */\nexport function localSchemaSteps(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): ReadonlyArray<LocalSchemaStep> {\n return [\n LOCAL_SCHEMA_BASELINE,\n ...migrations.map(({ name, version }) => ({ name, version })),\n ];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"localSchemaMigrations.js","sourceRoot":"","sources":["localSchemaMigrations.ts"],"names":[],"mappings":"AA0BA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAoB;IACpD,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,CAAC;CACX,CAAC;AAEF,MAAM,qBAAqB,GAAG,yCAAyC,CAAC;AAExE;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAwC;IACxE;QACE,wEAAwE;QACxE,oEAAoE;QACpE,IAAI,EAAE,iCAAiC;QACvC,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;YAC7B,IAAI,CAAC,CAAC,MAAM,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,CAAC;gBAAE,OAAO;YAC7D,KAAK,MAAM,MAAM,IAAI,CAAC,oBAAoB,EAAE,0BAA0B,CAAC,EAAE,CAAC;gBACxE,MAAM,OAAO,CACX,gBAAgB,qBAAqB,kBAAkB,MAAM,GAAG,CACjE,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO,EAAE,CAAC;KACX;IACD;QACE,wEAAwE;QACxE,sEAAsE;QACtE,0EAA0E;QAC1E,wCAAwC;QACxC,IAAI,EAAE,mCAAmC;QACzC,EAAE,EA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C,OAAO;QACL,qBAAqB;QACrB,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;KAC9D,CAAC;AACJ,CAAC","sourcesContent":["import type { ExecSql } from \"../sqlExec\";\n\nexport interface LocalSchemaMigrationContext {\n /**\n * Whether a table exists. Features create their tables lazily, so a\n * migration must skip a table this database never created: the table's\n * ensure creates it later in the current shape.\n */\n hasTable(name: string): Promise<boolean>;\n}\n\nexport interface LocalSchemaStep {\n readonly version: number;\n readonly name: string;\n}\n\nexport interface LocalSchemaMigration extends LocalSchemaStep {\n /**\n * Upgrades the local database from `version - 1` to `version`. It runs on\n * the locked executor inside the migration savepoint, so it must not BEGIN,\n * COMMIT, or change `PRAGMA foreign_keys`. Write it against the shape at\n * `version - 1`, never by importing current table definitions.\n */\n up(execSql: ExecSql, context: LocalSchemaMigrationContext): Promise<void>;\n}\n\n/**\n * Version 1 is the shape the SDK's table definitions created when local\n * migrations began. It has no steps: a database from before the ledger is\n * recorded at this version, and an older shape still fails its tables'\n * required-column check.\n */\nexport const LOCAL_SCHEMA_BASELINE: LocalSchemaStep = {\n name: \"baseline\",\n version: 1,\n};\n\nconst DIRECTORY_USERS_TABLE = \"organization_read_model_directory_users\";\n\n/**\n * Ordered and append-only: version 2 first, each one after the last. Never\n * edit or remove a shipped step; a correction is a new step.\n */\nexport const localSchemaMigrations: ReadonlyArray<LocalSchemaMigration> = [\n {\n // Directory rows carry key fingerprints only; public keys come from the\n // trusted identity store. Neither column is indexed or constrained.\n name: \"drop_directory_user_public_keys\",\n up: async (execSql, context) => {\n if (!(await context.hasTable(DIRECTORY_USERS_TABLE))) return;\n for (const column of [\"signing_public_key\", \"encapsulation_public_key\"]) {\n await execSql(\n `ALTER TABLE \"${DIRECTORY_USERS_TABLE}\" DROP COLUMN \"${column}\"`,\n );\n }\n },\n version: 2,\n },\n {\n // No reader ever used a recorded failure's HTTP status; the message and\n // attempt time explain a blocked document. The column is unindexed. A\n // documents table without it (never created, or not this SDK's table in a\n // restored backup) has nothing to drop.\n name: \"drop_document_sync_failure_status\",\n up: async (execSql) => {\n const column = await execSql(\n \"SELECT name FROM pragma_table_info('documents') WHERE name = 'sync_failure_status'\",\n );\n if (column.length === 0) return;\n await execSql(\n 'ALTER TABLE \"documents\" DROP COLUMN \"sync_failure_status\"',\n );\n },\n version: 3,\n },\n {\n name: \"replay_commit_ordered_discovery\",\n up: async (execSql, context) => {\n if (!(await context.hasTable(\"container_sync_watermarks\"))) return;\n // Older SDKs could checkpoint a publication before reconciling an\n // unlink or a restored subtree. Legacy UUID cursors already replay.\n await execSql(\n \"ALTER TABLE container_sync_watermarks ADD COLUMN cursor_format INTEGER NOT NULL DEFAULT 1\",\n );\n },\n version: 4,\n },\n];\n\nconst STEP_NAME_PATTERN = /^[a-z0-9_]+$/;\n\n/** Rejects a registry whose versions are not consecutive from 2. */\nexport function assertLocalSchemaMigrations(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): void {\n for (const [index, migration] of migrations.entries()) {\n const expected = LOCAL_SCHEMA_BASELINE.version + index + 1;\n if (migration.version !== expected) {\n throw new Error(\n `Local schema migration ${migration.name} has version ${migration.version}; expected ${expected}`,\n );\n }\n if (!STEP_NAME_PATTERN.test(migration.name)) {\n throw new Error(\n `Local schema migration ${migration.version} needs a snake_case name`,\n );\n }\n }\n}\n\n/** The steps a database at the newest version has recorded, in order. */\nexport function localSchemaSteps(\n migrations: ReadonlyArray<LocalSchemaMigration>,\n): ReadonlyArray<LocalSchemaStep> {\n return [\n LOCAL_SCHEMA_BASELINE,\n ...migrations.map(({ name, version }) => ({ name, version })),\n ];\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { primaryKey, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
1
|
+
import { integer, primaryKey, sqliteTable, text, } from "drizzle-orm/sqlite-core";
|
|
2
2
|
import { defineSqlTableSchema } from "../sqlTableSchema.js";
|
|
3
3
|
/**
|
|
4
4
|
* Per-lane sync state for container data: the server cursor and the local
|
|
@@ -13,13 +13,15 @@ import { defineSqlTableSchema } from "../sqlTableSchema.js";
|
|
|
13
13
|
*
|
|
14
14
|
* Columns:
|
|
15
15
|
* - `laneKind`: Sync lane category, such as `container_parent` or
|
|
16
|
-
* `container_documents`.
|
|
16
|
+
* `container_documents`. `container_restoration` stores the last reconciled
|
|
17
|
+
* access loss for a folder.
|
|
17
18
|
* - `laneId`: Lane identity. Root parent sync uses `root`, child sync uses a
|
|
18
19
|
* parent-prefixed container id, and document lanes use the container id.
|
|
19
20
|
* - `watermarkUpdatedAt`: Server `updatedAt` value for the stored watermark;
|
|
20
21
|
* null until a watermark is saved.
|
|
21
22
|
* - `watermarkId`: Server id tie-breaker for the stored watermark; null
|
|
22
23
|
* exactly when `watermarkUpdatedAt` is.
|
|
24
|
+
* - `cursorFormat`: SDK format that applied the saved watermark.
|
|
23
25
|
* - `updatedAt`: Local timestamp for when the cursor was saved.
|
|
24
26
|
* - `checkedAt`: Local timestamp for the most recent fully drained lane check;
|
|
25
27
|
* null until one completes.
|
|
@@ -32,13 +34,14 @@ export const containerSyncWatermarks = sqliteTable("container_sync_watermarks",
|
|
|
32
34
|
laneId: text("lane_id").notNull(),
|
|
33
35
|
watermarkUpdatedAt: text("watermark_updated_at"),
|
|
34
36
|
watermarkId: text("watermark_id"),
|
|
37
|
+
cursorFormat: integer("cursor_format").notNull().default(1),
|
|
35
38
|
updatedAt: text("updated_at"),
|
|
36
39
|
checkedAt: text("checked_at"),
|
|
37
40
|
}, (table) => [primaryKey({ columns: [table.laneKind, table.laneId] })]);
|
|
38
41
|
export const containerSyncWatermarkTables = [
|
|
39
42
|
{
|
|
40
43
|
...defineSqlTableSchema(containerSyncWatermarks),
|
|
41
|
-
requiredColumns: ["checked_at"],
|
|
44
|
+
requiredColumns: ["checked_at", "cursor_format"],
|
|
42
45
|
},
|
|
43
46
|
];
|
|
44
47
|
//# sourceMappingURL=containerSyncWatermarkSchema.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"containerSyncWatermarkSchema.js","sourceRoot":"","sources":["containerSyncWatermarkSchema.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"containerSyncWatermarkSchema.js","sourceRoot":"","sources":["containerSyncWatermarkSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,GACL,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,oBAAoB,EAAuB,MAAM,mBAAmB,CAAC;AAE9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,WAAW,CAChD,2BAA2B,EAC3B;IACE,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE;IACrC,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE;IACjC,kBAAkB,EAAE,IAAI,CAAC,sBAAsB,CAAC;IAChD,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC;IACjC,YAAY,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAC3D,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC;IAC7B,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC;CAC9B,EACD,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CACrE,CAAC;AAEF,MAAM,CAAC,MAAM,4BAA4B,GAAkC;IACzE;QACE,GAAG,oBAAoB,CAAC,uBAAuB,CAAC;QAChD,eAAe,EAAE,CAAC,YAAY,EAAE,eAAe,CAAC;KACjD;CACF,CAAC","sourcesContent":["import {\n integer,\n primaryKey,\n sqliteTable,\n text,\n} from \"drizzle-orm/sqlite-core\";\nimport { defineSqlTableSchema, type SqlTableSchema } from \"../sqlTableSchema\";\n\n/**\n * Per-lane sync state for container data: the server cursor and the local\n * freshness marker share one row per lane.\n *\n * Container sync is split into lanes, such as a parent-container child lane or\n * a container-document membership lane. The cursor lets the next request\n * resume without replaying the entire tree. The freshness marker records the\n * last fully drained check independently of the cursor: empty server\n * responses do not always carry a new watermark, but startup still needs to\n * know that it recently checked the lane before polling again.\n *\n * Columns:\n * - `laneKind`: Sync lane category, such as `container_parent` or\n * `container_documents`. `container_restoration` stores the last reconciled\n * access loss for a folder.\n * - `laneId`: Lane identity. Root parent sync uses `root`, child sync uses a\n * parent-prefixed container id, and document lanes use the container id.\n * - `watermarkUpdatedAt`: Server `updatedAt` value for the stored watermark;\n * null until a watermark is saved.\n * - `watermarkId`: Server id tie-breaker for the stored watermark; null\n * exactly when `watermarkUpdatedAt` is.\n * - `cursorFormat`: SDK format that applied the saved watermark.\n * - `updatedAt`: Local timestamp for when the cursor was saved.\n * - `checkedAt`: Local timestamp for the most recent fully drained lane check;\n * null until one completes.\n *\n * Indexes:\n * - `(laneKind, laneId)` is the primary key and stores one row per lane.\n */\nexport const containerSyncWatermarks = sqliteTable(\n \"container_sync_watermarks\",\n {\n laneKind: text(\"lane_kind\").notNull(),\n laneId: text(\"lane_id\").notNull(),\n watermarkUpdatedAt: text(\"watermark_updated_at\"),\n watermarkId: text(\"watermark_id\"),\n cursorFormat: integer(\"cursor_format\").notNull().default(1),\n updatedAt: text(\"updated_at\"),\n checkedAt: text(\"checked_at\"),\n },\n (table) => [primaryKey({ columns: [table.laneKind, table.laneId] })],\n);\n\nexport const containerSyncWatermarkTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(containerSyncWatermarks),\n requiredColumns: [\"checked_at\", \"cursor_format\"],\n },\n];\n"]}
|