@tearleads/client-sdk 0.1.211 → 0.1.214
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 +2 -2
- package/client/containerContents/documentDiscovery.js.map +1 -1
- package/client/organizations/organizationReadModels.js +3 -3
- package/client/organizations/organizationReadModels.js.map +1 -1
- package/data/persistence/container-contents/containerContentsPersistence.js +2 -2
- package/data/persistence/container-contents/containerContentsPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js +2 -2
- package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js +2 -2
- package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js.map +1 -1
- package/data/persistence/container-contents/containerPendingUpdatePersistence.js +6 -6
- package/data/persistence/container-contents/containerPendingUpdatePersistence.js.map +1 -1
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.d.ts +11 -2
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.js +89 -77
- package/data/persistence/documents/documentDiscoveryEvidencePersistence.js.map +1 -1
- package/data/persistence/documents/internal/discardDocument.js +4 -4
- package/data/persistence/documents/internal/discardDocument.js.map +1 -1
- package/data/persistence/documents/internal/discoveredDocumentUpsert.js +3 -3
- package/data/persistence/documents/internal/discoveredDocumentUpsert.js.map +1 -1
- package/data/persistence/documents/internal/documentDeletionPersistence.js +8 -8
- package/data/persistence/documents/internal/documentDeletionPersistence.js.map +1 -1
- package/data/persistence/documents/internal/documentMutationPersistence.js +4 -3
- package/data/persistence/documents/internal/documentMutationPersistence.js.map +1 -1
- package/data/persistence/documents/internal/documentPullContinuationPersistence.js +2 -2
- package/data/persistence/documents/internal/documentPullContinuationPersistence.js.map +1 -1
- package/data/persistence/documents/internal/rowQueryPersistence.js +3 -3
- package/data/persistence/documents/internal/rowQueryPersistence.js.map +1 -1
- package/data/persistence/documents/internal/syncQueuePersistence.js +12 -13
- package/data/persistence/documents/internal/syncQueuePersistence.js.map +1 -1
- package/data/persistence/documents/records/documentHistoryPersistence.d.ts +24 -20
- package/data/persistence/documents/records/documentHistoryPersistence.js +59 -90
- package/data/persistence/documents/records/documentHistoryPersistence.js.map +1 -1
- package/data/persistence/documents/records/documentPendingUpdatePersistence.d.ts +10 -25
- package/data/persistence/documents/records/documentPendingUpdatePersistence.js +19 -79
- package/data/persistence/documents/records/documentPendingUpdatePersistence.js.map +1 -1
- package/data/persistence/documents/records/documentQueuedUpdateReplacement.d.ts +4 -14
- package/data/persistence/documents/records/documentQueuedUpdateReplacement.js +27 -42
- package/data/persistence/documents/records/documentQueuedUpdateReplacement.js.map +1 -1
- package/data/persistence/documents/records/documentRecordPersistence.d.ts +5 -8
- package/data/persistence/documents/records/documentRecordPersistence.js +2 -55
- package/data/persistence/documents/records/documentRecordPersistence.js.map +1 -1
- package/data/persistence/documents/records/documentRecordRepository.d.ts +133 -0
- package/data/persistence/documents/records/documentRecordRepository.js +286 -0
- package/data/persistence/documents/records/documentRecordRepository.js.map +1 -0
- package/data/persistence/documents/records/documentSyncFailurePersistence.d.ts +9 -12
- package/data/persistence/documents/records/documentSyncFailurePersistence.js +19 -34
- package/data/persistence/documents/records/documentSyncFailurePersistence.js.map +1 -1
- package/data/persistence/documents/records/index.d.ts +4 -19
- package/data/persistence/documents/records/index.js +5 -29
- package/data/persistence/documents/records/index.js.map +1 -1
- package/data/persistence/organizations/organizationPresentationDenialPersistence.js +0 -43
- package/data/persistence/organizations/organizationPresentationDenialPersistence.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelMemberLoad.d.ts +7 -2
- package/data/persistence/organizations/organizationReadModelMemberLoad.js +22 -27
- package/data/persistence/organizations/organizationReadModelMemberLoad.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelPersistence.js +38 -81
- package/data/persistence/organizations/organizationReadModelPersistence.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelPurge.js +0 -38
- package/data/persistence/organizations/organizationReadModelPurge.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelRepository.js +264 -0
- package/data/persistence/organizations/organizationReadModelRepository.js.map +1 -0
- package/package.json +1 -1
- package/stores/documents/documentStore/sync/attachmentUploadFailure.js +2 -2
- package/stores/documents/documentStore/sync/attachmentUploadFailure.js.map +1 -1
- package/stores/documents/documentStore/sync/syncAttachments.js +2 -2
- package/stores/documents/documentStore/sync/syncAttachments.js.map +1 -1
- package/stores/documents/documentStore/sync/syncShared.js +2 -2
- package/stores/documents/documentStore/sync/syncShared.js.map +1 -1
- package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js.map +1 -1
- package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js +2 -2
- package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js.map +1 -1
- package/workflows/container-contents/pendingWrites/index.js +4 -3
- package/workflows/container-contents/pendingWrites/index.js.map +1 -1
- package/workflows/container-contents/recovery/accessRestoration.js +2 -2
- package/workflows/container-contents/recovery/accessRestoration.js.map +1 -1
- package/workflows/documents/index.d.ts +1 -1
- package/workflows/documents/index.js +1 -1
- package/workflows/documents/index.js.map +1 -1
- package/workflows/documents/syncRekeys/syncRecoveryRekey.js +3 -2
- package/workflows/documents/syncRekeys/syncRecoveryRekey.js.map +1 -1
- package/workflows/organizations/groupNames/currentOrganizationGroupNames.js +6 -2
- package/workflows/organizations/groupNames/currentOrganizationGroupNames.js.map +1 -1
- package/workflows/organizations/index.d.ts +0 -1
- package/workflows/organizations/index.js +0 -1
- package/workflows/organizations/index.js.map +1 -1
- package/workflows/organizations/policyHistory/policyHistoryAccessDenial.js +7 -4
- package/workflows/organizations/policyHistory/policyHistoryAccessDenial.js.map +1 -1
- package/workflows/organizations/readModel/dataUsageReadModel.js +2 -2
- package/workflows/organizations/readModel/dataUsageReadModel.js.map +1 -1
- package/workflows/organizations/readModel/listLocalOrganizations.js +6 -4
- package/workflows/organizations/readModel/listLocalOrganizations.js.map +1 -1
- package/workflows/organizations/readModel/localReadModelDetails.js +8 -9
- package/workflows/organizations/readModel/localReadModelDetails.js.map +1 -1
- package/workflows/organizations/readModel/organizationPresentationAccessState.js +2 -2
- package/workflows/organizations/readModel/organizationPresentationAccessState.js.map +1 -1
- package/workflows/organizations/readModel/readModelProjection.d.ts +1 -1
- package/workflows/organizations/readModel/readModelProjection.js +12 -13
- package/workflows/organizations/readModel/readModelProjection.js.map +1 -1
- package/data/persistence/organizations/organizationAccessRevocationPersistence.js +0 -49
- package/data/persistence/organizations/organizationAccessRevocationPersistence.js.map +0 -1
- package/data/persistence/organizations/organizationGroupNamePersistence.js +0 -33
- package/data/persistence/organizations/organizationGroupNamePersistence.js.map +0 -1
- package/data/persistence/organizations/organizationProfileDocumentPointers.js +0 -32
- package/data/persistence/organizations/organizationProfileDocumentPointers.js.map +0 -1
- package/data/persistence/organizations/organizationReadModelCursorPersistence.d.ts +0 -15
- package/data/persistence/organizations/organizationReadModelCursorPersistence.js +0 -25
- package/data/persistence/organizations/organizationReadModelCursorPersistence.js.map +0 -1
|
@@ -12,7 +12,17 @@ interface RemovedDiscoveryPlacement {
|
|
|
12
12
|
readonly containerId: string;
|
|
13
13
|
readonly documentId: string;
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Untrusted document discovery evidence bound to one executor and clock. The
|
|
17
|
+
* clock (epoch milliseconds, like the `retryAt` column) schedules and reports
|
|
18
|
+
* pending-discovery retries.
|
|
19
|
+
*/
|
|
20
|
+
export declare class DocumentDiscoveryEvidenceRepository {
|
|
21
|
+
private readonly execSql;
|
|
22
|
+
private readonly now;
|
|
23
|
+
constructor(execSql: ExecSql, now?: () => number);
|
|
24
|
+
private read;
|
|
25
|
+
private write;
|
|
16
26
|
begin(): Promise<number>;
|
|
17
27
|
isCurrent(generation: number): Promise<boolean>;
|
|
18
28
|
stage(inputs: readonly DiscoveredDocumentCandidate[], generation: number, removed?: readonly RemovedDiscoveryPlacement[]): Promise<boolean>;
|
|
@@ -40,4 +50,3 @@ export interface AuthoredDiscoveryHead {
|
|
|
40
50
|
}
|
|
41
51
|
/** Writes authored heads inside the caller's serialized write transaction. */
|
|
42
52
|
export declare function primeAuthoredDocumentDiscoveryHeads(execSql: ExecSql, authored: readonly AuthoredDiscoveryHead[]): Promise<void>;
|
|
43
|
-
export declare function createDocumentDiscoveryEvidenceStore(execSql: ExecSql, now?: () => number): DocumentDiscoveryEvidenceStore;
|
|
@@ -19,22 +19,29 @@ const ID_BATCH_SIZE = 400;
|
|
|
19
19
|
const batches = (ids) => Array.from({ length: Math.ceil(ids.length / ID_BATCH_SIZE) }, (_, i) => ids.slice(i * ID_BATCH_SIZE, (i + 1) * ID_BATCH_SIZE));
|
|
20
20
|
const chunked = (rows, size) => Array.from({ length: Math.ceil(rows.length / size) }, (_, i) => rows.slice(i * size, (i + 1) * size));
|
|
21
21
|
const identity = (input) => and(eq(pending.containerId, input.containerId), eq(pending.documentId, input.documentId), eq(pending.inputJson, JSON.stringify(input)));
|
|
22
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Untrusted document discovery evidence bound to one executor and clock. The
|
|
24
|
+
* clock (epoch milliseconds, like the `retryAt` column) schedules and reports
|
|
25
|
+
* pending-discovery retries.
|
|
26
|
+
*/
|
|
27
|
+
export class DocumentDiscoveryEvidenceRepository {
|
|
23
28
|
execSql;
|
|
24
29
|
now;
|
|
25
|
-
constructor(execSql, now) {
|
|
30
|
+
constructor(execSql, now = Date.now) {
|
|
26
31
|
this.execSql = execSql;
|
|
27
32
|
this.now = now;
|
|
28
33
|
}
|
|
29
|
-
|
|
34
|
+
async read() {
|
|
30
35
|
await ensureSqlTables(this.execSql, documentContainerProjectionTables);
|
|
31
36
|
return getClientSQLitePersistenceRuntime(this.execSql).db;
|
|
32
|
-
}
|
|
33
|
-
write
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
}
|
|
38
|
+
write(operation) {
|
|
39
|
+
return runSerializedSqlMutation(this.execSql, async (locked) => {
|
|
40
|
+
await ensureSqlTables(locked, documentContainerProjectionTables);
|
|
41
|
+
return operation(getClientSQLitePersistenceRuntime(locked).db);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async begin() {
|
|
38
45
|
let generation = 0;
|
|
39
46
|
await this.write(async (db) => {
|
|
40
47
|
const [row] = await db
|
|
@@ -52,9 +59,11 @@ class SqlDocumentDiscoveryEvidenceStore {
|
|
|
52
59
|
generation = row.generation;
|
|
53
60
|
});
|
|
54
61
|
return generation;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
}
|
|
63
|
+
async isCurrent(generation) {
|
|
64
|
+
return isDocumentDiscoveryGenerationCurrent(await this.read(), generation);
|
|
65
|
+
}
|
|
66
|
+
stage(inputs, generation, removed = []) {
|
|
58
67
|
return this.write(async (db) => {
|
|
59
68
|
if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))
|
|
60
69
|
return false;
|
|
@@ -107,8 +116,8 @@ class SqlDocumentDiscoveryEvidenceStore {
|
|
|
107
116
|
});
|
|
108
117
|
return true;
|
|
109
118
|
});
|
|
110
|
-
}
|
|
111
|
-
|
|
119
|
+
}
|
|
120
|
+
async pending(containerIds, limit) {
|
|
112
121
|
const db = await this.read();
|
|
113
122
|
const result = [];
|
|
114
123
|
for (const batch of batches(containerIds)) {
|
|
@@ -123,39 +132,43 @@ class SqlDocumentDiscoveryEvidenceStore {
|
|
|
123
132
|
result.push(...rows.map((row) => readStoredDiscoveryCandidate(row.inputJson)));
|
|
124
133
|
}
|
|
125
134
|
return result;
|
|
126
|
-
}
|
|
127
|
-
defer
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
135
|
+
}
|
|
136
|
+
defer(input, generation) {
|
|
137
|
+
return this.write(async (db) => {
|
|
138
|
+
if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))
|
|
139
|
+
return;
|
|
140
|
+
const [row] = await db
|
|
141
|
+
.select({ attempts: pending.attempts })
|
|
142
|
+
.from(pending)
|
|
143
|
+
.where(identity(input))
|
|
144
|
+
.limit(1);
|
|
145
|
+
if (!row)
|
|
146
|
+
return;
|
|
147
|
+
const attempts = Math.min(row.attempts + 1, 8);
|
|
148
|
+
await db
|
|
149
|
+
.update(pending)
|
|
150
|
+
.set({
|
|
151
|
+
attempts,
|
|
152
|
+
retryAt: this.now() + heldTombstoneRetryDelayMs(attempts),
|
|
153
|
+
})
|
|
154
|
+
.where(identity(input))
|
|
155
|
+
.run();
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
acknowledge(inputs, generation) {
|
|
159
|
+
return this.write(async (db) => {
|
|
160
|
+
if (!(await isDocumentDiscoveryGenerationCurrent(db, generation)))
|
|
161
|
+
return;
|
|
162
|
+
await db.transaction(async (tx) => {
|
|
163
|
+
for (const batch of remoteResetBatches(inputs))
|
|
164
|
+
await tx
|
|
165
|
+
.delete(pending)
|
|
166
|
+
.where(or(...batch.map(identity)))
|
|
167
|
+
.run();
|
|
168
|
+
});
|
|
156
169
|
});
|
|
157
|
-
}
|
|
158
|
-
|
|
170
|
+
}
|
|
171
|
+
async hasPending(containerIds) {
|
|
159
172
|
const db = await this.read();
|
|
160
173
|
for (const batch of batches(containerIds)) {
|
|
161
174
|
if ((await db
|
|
@@ -166,8 +179,8 @@ class SqlDocumentDiscoveryEvidenceStore {
|
|
|
166
179
|
return true;
|
|
167
180
|
}
|
|
168
181
|
return false;
|
|
169
|
-
}
|
|
170
|
-
|
|
182
|
+
}
|
|
183
|
+
async retryDelay(containerIds) {
|
|
171
184
|
const db = await this.read();
|
|
172
185
|
let due = null;
|
|
173
186
|
for (const batch of batches(containerIds)) {
|
|
@@ -179,8 +192,8 @@ class SqlDocumentDiscoveryEvidenceStore {
|
|
|
179
192
|
due = Math.min(due ?? row.due, row.due);
|
|
180
193
|
}
|
|
181
194
|
return due === null ? null : Math.max(0, due - this.now());
|
|
182
|
-
}
|
|
183
|
-
|
|
195
|
+
}
|
|
196
|
+
async loadHead(documentId, manifestHash) {
|
|
184
197
|
if (await new KeyingCheckpointRepository(this.execSql).loadDocumentPurgeCheckpoint(documentId))
|
|
185
198
|
return {
|
|
186
199
|
organizationId: null,
|
|
@@ -211,30 +224,32 @@ class SqlDocumentDiscoveryEvidenceStore {
|
|
|
211
224
|
accessStateHash: row.manifestHash,
|
|
212
225
|
linkedContainerIds: readStringArray(JSON.parse(row.linksJson), "stored discovery links"),
|
|
213
226
|
};
|
|
214
|
-
}
|
|
215
|
-
saveHead
|
|
216
|
-
|
|
217
|
-
!head.
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
227
|
+
}
|
|
228
|
+
saveHead(documentId, head, generation) {
|
|
229
|
+
return this.write(async (db) => {
|
|
230
|
+
if (!head.accessStateHash ||
|
|
231
|
+
!head.organizationId ||
|
|
232
|
+
!(await isDocumentDiscoveryGenerationCurrent(db, generation)))
|
|
233
|
+
return false;
|
|
234
|
+
const row = {
|
|
235
|
+
documentId,
|
|
236
|
+
organizationId: head.organizationId,
|
|
237
|
+
manifestHash: head.accessStateHash,
|
|
238
|
+
accessEpoch: head.accessEpoch,
|
|
239
|
+
linksJson: JSON.stringify(head.linkedContainerIds),
|
|
240
|
+
};
|
|
241
|
+
await db
|
|
242
|
+
.insert(heads)
|
|
243
|
+
.values(row)
|
|
244
|
+
.onConflictDoUpdate({
|
|
245
|
+
target: [heads.documentId, heads.organizationId],
|
|
246
|
+
set: row,
|
|
247
|
+
setWhere: lte(heads.accessEpoch, head.accessEpoch),
|
|
248
|
+
})
|
|
249
|
+
.run();
|
|
250
|
+
return true;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
238
253
|
}
|
|
239
254
|
/** Writes authored heads inside the caller's serialized write transaction. */
|
|
240
255
|
export async function primeAuthoredDocumentDiscoveryHeads(execSql, authored) {
|
|
@@ -258,7 +273,4 @@ export async function primeAuthoredDocumentDiscoveryHeads(execSql, authored) {
|
|
|
258
273
|
.run();
|
|
259
274
|
}
|
|
260
275
|
}
|
|
261
|
-
export function createDocumentDiscoveryEvidenceStore(execSql, now = Date.now) {
|
|
262
|
-
return new SqlDocumentDiscoveryEvidenceStore(execSql, now);
|
|
263
|
-
}
|
|
264
276
|
//# sourceMappingURL=documentDiscoveryEvidencePersistence.js.map
|
|
@@ -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,+BA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EAAE,CAAC;QACT,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;CACN;AAiBD,8EAA8E;AAC9E,MAAM,CAAC,KAAK,UAAU,mCAAmC,CACvD,OAAgB,EAChB,QAA0C;IAE1C,MAAM,EAAE,GAAG,iCAAiC,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;IACzD,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC5B,MAAM,GAAG,GAAG;YACV,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,cAAc,EAAE,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;AAED,MAAM,UAAU,oCAAoC,CAClD,OAAgB,EAChB,GAAG,GAAG,IAAI,CAAC,GAAG;IAEd,OAAO,IAAI,iCAAiC,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AAC7D,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}\nexport interface DocumentDiscoveryEvidenceStore {\n begin(): Promise<number>;\n isCurrent(generation: number): Promise<boolean>;\n stage(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n removed?: readonly RemovedDiscoveryPlacement[],\n ): Promise<boolean>;\n pending(\n containerIds: readonly string[],\n limit: number,\n ): Promise<readonly DiscoveredDocumentCandidate[]>;\n defer(input: DiscoveredDocumentCandidate, generation: number): Promise<void>;\n acknowledge(\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n ): Promise<void>;\n hasPending(containerIds: readonly string[]): Promise<boolean>;\n retryDelay(containerIds: readonly string[]): Promise<number | null>;\n loadHead(\n documentId: string,\n manifestHash: string | null | undefined,\n ): Promise<CachedDiscoveryHead | null>;\n saveHead(\n documentId: string,\n head: CachedDiscoveryHead,\n generation: number,\n ): Promise<boolean>;\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\nclass SqlDocumentDiscoveryEvidenceStore\n implements DocumentDiscoveryEvidenceStore\n{\n constructor(\n private readonly execSql: ExecSql,\n private readonly now: () => number,\n ) {}\n\n private read = async () => {\n await ensureSqlTables(this.execSql, documentContainerProjectionTables);\n return getClientSQLitePersistenceRuntime(this.execSql).db;\n };\n private write = async <T>(\n operation: (\n db: ReturnType<typeof getClientSQLitePersistenceRuntime>[\"db\"],\n ) => Promise<T>,\n ) =>\n runSerializedSqlMutation(this.execSql, async (locked) => {\n await ensureSqlTables(locked, documentContainerProjectionTables);\n return operation(getClientSQLitePersistenceRuntime(locked).db);\n });\n begin = async () => {\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 isCurrent = async (generation: number) =>\n isDocumentDiscoveryGenerationCurrent(await this.read(), generation);\n stage = (\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n removed: readonly RemovedDiscoveryPlacement[] = [],\n ) => {\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 pending = async (containerIds: readonly string[], limit: number) => {\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) =>\n 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 acknowledge = (\n inputs: readonly DiscoveredDocumentCandidate[],\n generation: number,\n ) =>\n 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 hasPending = async (containerIds: readonly string[]) => {\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 retryDelay = async (containerIds: readonly string[]) => {\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 loadHead = async (\n documentId: string,\n manifestHash: string | null | undefined,\n ) => {\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 ) =>\n 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 * 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\nexport function createDocumentDiscoveryEvidenceStore(\n execSql: ExecSql,\n now = Date.now,\n): DocumentDiscoveryEvidenceStore {\n return new SqlDocumentDiscoveryEvidenceStore(execSql, now);\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,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,wBA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CAAC,EAAE,CAAC;IACzD,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC5B,MAAM,GAAG,GAAG;YACV,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,cAAc,EAAE,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"]}
|
|
@@ -4,8 +4,7 @@ import { resolveDocumentProjectorRegistry, } from "../../../documents/documentKi
|
|
|
4
4
|
import { documentAttachmentBlobProjection, documentPendingAttachments, documentPlacementIntentTables, inLocalIntentLane, LOCAL_INTENT_LANES, localIntents, } from "../../../sqlite/schema/index.js";
|
|
5
5
|
import { getClientSQLitePersistenceRuntime, } from "../../../sqlite/sqlitePersistenceRuntime.js";
|
|
6
6
|
import { ensureSqlTables, runSerializedSqlMutation, } from "../../../sqlite/sqlSchema.js";
|
|
7
|
-
import {
|
|
8
|
-
import { deleteDocumentHistory } from "../records/documentHistoryPersistence.js";
|
|
7
|
+
import { DocumentRecordRepository } from "../records/index.js";
|
|
9
8
|
import { getDocumentScope, resolveDocumentSaveTimestamp, saveDocumentRows, } from "./documentRows.js";
|
|
10
9
|
import { queueDocumentAttachmentStorageKeys } from "./orphanSideRows.js";
|
|
11
10
|
function buildDiscardShellDocument(localId, existingDocument) {
|
|
@@ -78,8 +77,9 @@ async function clearDiscardedDocumentRows(input) {
|
|
|
78
77
|
.delete(documentAttachmentBlobProjection)
|
|
79
78
|
.where(and(eq(documentAttachmentBlobProjection.localId, localId), isNotNull(documentAttachmentBlobProjection.detachedAt)))
|
|
80
79
|
.run();
|
|
81
|
-
|
|
82
|
-
await
|
|
80
|
+
const records = new DocumentRecordRepository(lockedExecSql);
|
|
81
|
+
await records.deleteDocumentHistory(getDocumentScope(localId));
|
|
82
|
+
await records.clearDocumentSyncFailure(getDocumentScope(localId));
|
|
83
83
|
}
|
|
84
84
|
async function saveDiscardShell(input) {
|
|
85
85
|
const { candidate, documentProjectors, localId, lockedExecSql, tx } = input;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discardDocument.js","sourceRoot":"","sources":["discardDocument.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAGL,gCAAgC,GACjC,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACL,gCAAgC,EAChC,0BAA0B,EAC1B,6BAA6B,EAC7B,iBAAiB,EACjB,kBAAkB,EAClB,YAAY,GACb,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAEL,iCAAiC,GAClC,MAAM,0CAA0C,CAAC;AAClD,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AgB,CAAC,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAC;QACpD,uEAAuE;QACvE,qEAAqE;QACrE,oEAAoE;QACpE,MAAM,kCAAkC,CAAC,EAAE,EAAE;YAC3C,GAAG,IAAI,GAAG,CAAC;gBACT,GAAG,SAAS,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC;gBAC5D,GAAG,SAAS,CAAC,mBAAmB;aACjC,CAAC;SACH,CAAC,CAAC;QACH,OAAO;YACL,SAAS,EAAE,IAAI;YACf,YAAY,EACV,SAAS,CAAC,gBAAgB,CAAC,YAAY,IAAI,qBAAqB;SACnE,CAAC;IACJ,CAAC,EACD,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAAC,KASlD;IACC,MAAM,EAAE,kBAAkB,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,KAAK,CAAC;IAC3D,OAAO,wBAAwB,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,EAAE;QACrE,MAAM,eAAe,CAAC,aAAa,EAAE,6BAA6B,CAAC,CAAC;QACpE,OAAO,0BAA0B,CAAC;YAChC,kBAAkB,EAAE,gCAAgC,CAClD,KAAK,CAAC,kBAAkB,CACzB;YACD,kBAAkB;YAClB,OAAO;YACP,aAAa;YACb,WAAW;SACZ,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { and, eq, isNotNull } from \"drizzle-orm\";\nimport { DEFAULT_DOCUMENT_KIND } from \"../../../documents/documentConstants\";\nimport {\n type DocumentProjectorRegistry,\n type DocumentProjectorRegistryInput,\n resolveDocumentProjectorRegistry,\n} from \"../../../documents/documentKinds\";\nimport {\n documentAttachmentBlobProjection,\n documentPendingAttachments,\n documentPlacementIntentTables,\n inLocalIntentLane,\n LOCAL_INTENT_LANES,\n localIntents,\n} from \"../../../sqlite/schema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../../sqlite/sqlSchema\";\nimport { clearDocumentSyncFailure } from \"../records\";\nimport { deleteDocumentHistory } from \"../records/documentHistoryPersistence\";\nimport type {\n DiscardDocumentToShellResult,\n DocumentsPersistence,\n StoredDocumentRecord,\n} from \"../types\";\nimport {\n getDocumentScope,\n resolveDocumentSaveTimestamp,\n saveDocumentRows,\n} from \"./documentRows\";\nimport { queueDocumentAttachmentStorageKeys } from \"./orphanSideRows\";\n\nfunction buildDiscardShellDocument(\n localId: string,\n existingDocument: StoredDocumentRecord,\n): StoredDocumentRecord {\n return {\n id: localId,\n accessEpoch: existingDocument.accessEpoch,\n accessStateHash: existingDocument.accessStateHash ?? null,\n containerId: existingDocument.containerId,\n contentKeyBundle: null,\n documentId: existingDocument.documentId,\n documentKekTargets: null,\n documentManifestBundle: null,\n effectiveAccessLevel: existingDocument.effectiveAccessLevel ?? null,\n lastCommitLsn: null,\n pendingBaseVersion: null,\n pullContinuation: null,\n recoveryGeneration: existingDocument.recoveryGeneration ?? 0,\n snapshotEndVersion: \"\",\n text: \"\",\n ...(existingDocument.documentKind === undefined\n ? {}\n : { documentKind: existingDocument.documentKind }),\n ...(existingDocument.title === undefined\n ? {}\n : { title: existingDocument.title }),\n };\n}\n\ninterface DiscardCandidate {\n detachedStorageKeys: string[];\n existingDocument: StoredDocumentRecord;\n pendingAttachments: ReadonlyArray<{ slotId: string; storageKey: string }>;\n}\n\nasync function loadDiscardCandidate(input: {\n expectedDocumentId: string;\n localId: string;\n lockedExecSql: ExecSql;\n persistence: Pick<\n DocumentsPersistence,\n \"listPendingAttachments\" | \"loadDocument\"\n >;\n tx: ClientSQLiteTransactionScope;\n}): Promise<DiscardCandidate | null> {\n const { expectedDocumentId, localId, lockedExecSql, persistence, tx } = input;\n const existingDocument = await persistence.loadDocument(\n lockedExecSql,\n localId,\n );\n if (\n !existingDocument?.documentId ||\n existingDocument.documentId !== expectedDocumentId ||\n !existingDocument.containerId\n ) {\n return null;\n }\n const moveIntentRows = await tx\n .select({ id: localIntents.id })\n .from(localIntents)\n .where(\n and(\n inLocalIntentLane(LOCAL_INTENT_LANES.documentPlacement),\n eq(localIntents.localId, localId),\n ),\n )\n .limit(1);\n if (moveIntentRows.length > 0) return null;\n\n const pendingAttachments = await persistence.listPendingAttachments(\n lockedExecSql,\n localId,\n );\n const detachedRows = await tx\n .select({ storageKey: documentAttachmentBlobProjection.storageKey })\n .from(documentAttachmentBlobProjection)\n .where(\n and(\n eq(documentAttachmentBlobProjection.localId, localId),\n isNotNull(documentAttachmentBlobProjection.detachedAt),\n ),\n );\n return {\n detachedStorageKeys: detachedRows.map((row) => row.storageKey),\n existingDocument,\n pendingAttachments,\n };\n}\n\n// Staged-upload rows AND detached local-attachment markers both go: a marker\n// for a locally-discarded detach would otherwise keep filtering the slot out\n// after a re-pull restores it, leaving the attachment permanently invisible.\nasync function clearDiscardedDocumentRows(input: {\n candidate: DiscardCandidate;\n localId: string;\n lockedExecSql: ExecSql;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n const { candidate, localId, lockedExecSql, tx } = input;\n await tx\n .delete(documentPendingAttachments)\n .where(eq(documentPendingAttachments.localId, localId))\n .run();\n for (const pendingAttachment of candidate.pendingAttachments) {\n await tx\n .delete(documentAttachmentBlobProjection)\n .where(\n and(\n eq(documentAttachmentBlobProjection.localId, localId),\n eq(documentAttachmentBlobProjection.slotId, pendingAttachment.slotId),\n eq(\n documentAttachmentBlobProjection.storageKey,\n pendingAttachment.storageKey,\n ),\n ),\n )\n .run();\n }\n await tx\n .delete(documentAttachmentBlobProjection)\n .where(\n and(\n eq(documentAttachmentBlobProjection.localId, localId),\n isNotNull(documentAttachmentBlobProjection.detachedAt),\n ),\n )\n .run();\n await deleteDocumentHistory(lockedExecSql, getDocumentScope(localId));\n await clearDocumentSyncFailure(lockedExecSql, getDocumentScope(localId));\n}\n\nasync function saveDiscardShell(input: {\n candidate: DiscardCandidate;\n documentProjectors: DocumentProjectorRegistry;\n localId: string;\n lockedExecSql: ExecSql;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n const { candidate, documentProjectors, localId, lockedExecSql, tx } = input;\n const shellDocument = buildDiscardShellDocument(\n localId,\n candidate.existingDocument,\n );\n // The document-kind projection derives from discarded content, so clear it\n // inside the transaction and let the next pull rebuild it.\n await documentProjectors.deleteStoredDocumentClientProjection({\n documentKind: shellDocument.documentKind ?? DEFAULT_DOCUMENT_KIND,\n execSql: lockedExecSql,\n localId,\n });\n const updatedAt = await resolveDocumentSaveTimestamp({\n document: shellDocument,\n tx,\n });\n if (!(await saveDocumentRows({ document: shellDocument, tx, updatedAt }))) {\n throw new Error(\"Document recovery generation changed during discard\");\n }\n}\n\n// Eligibility, teardown, client-projection clearing, and shell replacement\n// share one immediate transaction, so a second connection cannot relink the\n// local id between identity validation and destructive writes.\nasync function discardDocumentRowsToShell(input: {\n documentProjectors: DocumentProjectorRegistry;\n expectedDocumentId: string;\n localId: string;\n lockedExecSql: ExecSql;\n persistence: Pick<\n DocumentsPersistence,\n \"listPendingAttachments\" | \"loadDocument\"\n >;\n}): Promise<DiscardDocumentToShellResult> {\n const clientProjectionTables =\n input.documentProjectors.getClientProjectionTables();\n if (clientProjectionTables.length > 0) {\n await ensureSqlTables(input.lockedExecSql, clientProjectionTables);\n }\n return getClientSQLitePersistenceRuntime(input.lockedExecSql).transaction(\n async (tx) => {\n const candidate = await loadDiscardCandidate({ ...input, tx });\n if (!candidate) return { discarded: false };\n await clearDiscardedDocumentRows({\n candidate,\n localId: input.localId,\n lockedExecSql: input.lockedExecSql,\n tx,\n });\n await saveDiscardShell({ ...input, candidate, tx });\n // The deleted rows were these bytes' only pointers from this document,\n // but a hydrated copy is shared by every slot holding the same blob.\n // Queue them for the reference-checked reclaim instead of deleting.\n await queueDocumentAttachmentStorageKeys(tx, [\n ...new Set([\n ...candidate.pendingAttachments.map((row) => row.storageKey),\n ...candidate.detachedStorageKeys,\n ]),\n ]);\n return {\n discarded: true,\n documentKind:\n candidate.existingDocument.documentKind ?? DEFAULT_DOCUMENT_KIND,\n };\n },\n { behavior: \"immediate\" },\n );\n}\n\n/**\n * Atomically convert a stuck document's local state to the\n * freshly-discovered-share shell: drop its queued updates, staged attachment\n * rows (and their settled local-attachment halves, which share the staged\n * storage keys the caller reclaims), detached attachment markers, durable\n * history, recorded sync failure, and document-kind client projection, then\n * overwrite the record with an empty snapshot that keeps its identity,\n * placement, title, and kind. Everything commits in ONE transaction.\n *\n * Refused under the serialized mutation when the document is local-only or\n * unlinked (its rows are the only copy), when its documentId no longer\n * matches the caller's expectation (a stale caller or racing relink must not\n * discard a different identity's edits), or when any move intent references\n * it (the local containerId is then the move's optimistic placement, and\n * reseeding it as server truth would silently commit the move locally while\n * discarding the intent that was meant to perform it).\n */\nexport async function discardStoredDocumentToShell(input: {\n documentProjectors: DocumentProjectorRegistryInput;\n execSql: ExecSql;\n expectedDocumentId: string;\n localId: string;\n persistence: Pick<\n DocumentsPersistence,\n \"listPendingAttachments\" | \"loadDocument\"\n >;\n}): Promise<DiscardDocumentToShellResult> {\n const { expectedDocumentId, localId, persistence } = input;\n return runSerializedSqlMutation(input.execSql, async (lockedExecSql) => {\n await ensureSqlTables(lockedExecSql, documentPlacementIntentTables);\n return discardDocumentRowsToShell({\n documentProjectors: resolveDocumentProjectorRegistry(\n input.documentProjectors,\n ),\n expectedDocumentId,\n localId,\n lockedExecSql,\n persistence,\n });\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"discardDocument.js","sourceRoot":"","sources":["discardDocument.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,aAAa,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;YAClB,OAAO;YACP,aAAa;YACb,WAAW;SACZ,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { and, eq, isNotNull } from \"drizzle-orm\";\nimport { DEFAULT_DOCUMENT_KIND } from \"../../../documents/documentConstants\";\nimport {\n type DocumentProjectorRegistry,\n type DocumentProjectorRegistryInput,\n resolveDocumentProjectorRegistry,\n} from \"../../../documents/documentKinds\";\nimport {\n documentAttachmentBlobProjection,\n documentPendingAttachments,\n documentPlacementIntentTables,\n inLocalIntentLane,\n LOCAL_INTENT_LANES,\n localIntents,\n} from \"../../../sqlite/schema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport {\n type ExecSql,\n ensureSqlTables,\n runSerializedSqlMutation,\n} from \"../../../sqlite/sqlSchema\";\nimport { DocumentRecordRepository } from \"../records\";\nimport type {\n DiscardDocumentToShellResult,\n DocumentsPersistence,\n StoredDocumentRecord,\n} from \"../types\";\nimport {\n getDocumentScope,\n resolveDocumentSaveTimestamp,\n saveDocumentRows,\n} from \"./documentRows\";\nimport { queueDocumentAttachmentStorageKeys } from \"./orphanSideRows\";\n\nfunction buildDiscardShellDocument(\n localId: string,\n existingDocument: StoredDocumentRecord,\n): StoredDocumentRecord {\n return {\n id: localId,\n accessEpoch: existingDocument.accessEpoch,\n accessStateHash: existingDocument.accessStateHash ?? null,\n containerId: existingDocument.containerId,\n contentKeyBundle: null,\n documentId: existingDocument.documentId,\n documentKekTargets: null,\n documentManifestBundle: null,\n effectiveAccessLevel: existingDocument.effectiveAccessLevel ?? null,\n lastCommitLsn: null,\n pendingBaseVersion: null,\n pullContinuation: null,\n recoveryGeneration: existingDocument.recoveryGeneration ?? 0,\n snapshotEndVersion: \"\",\n text: \"\",\n ...(existingDocument.documentKind === undefined\n ? {}\n : { documentKind: existingDocument.documentKind }),\n ...(existingDocument.title === undefined\n ? {}\n : { title: existingDocument.title }),\n };\n}\n\ninterface DiscardCandidate {\n detachedStorageKeys: string[];\n existingDocument: StoredDocumentRecord;\n pendingAttachments: ReadonlyArray<{ slotId: string; storageKey: string }>;\n}\n\nasync function loadDiscardCandidate(input: {\n expectedDocumentId: string;\n localId: string;\n lockedExecSql: ExecSql;\n persistence: Pick<\n DocumentsPersistence,\n \"listPendingAttachments\" | \"loadDocument\"\n >;\n tx: ClientSQLiteTransactionScope;\n}): Promise<DiscardCandidate | null> {\n const { expectedDocumentId, localId, lockedExecSql, persistence, tx } = input;\n const existingDocument = await persistence.loadDocument(\n lockedExecSql,\n localId,\n );\n if (\n !existingDocument?.documentId ||\n existingDocument.documentId !== expectedDocumentId ||\n !existingDocument.containerId\n ) {\n return null;\n }\n const moveIntentRows = await tx\n .select({ id: localIntents.id })\n .from(localIntents)\n .where(\n and(\n inLocalIntentLane(LOCAL_INTENT_LANES.documentPlacement),\n eq(localIntents.localId, localId),\n ),\n )\n .limit(1);\n if (moveIntentRows.length > 0) return null;\n\n const pendingAttachments = await persistence.listPendingAttachments(\n lockedExecSql,\n localId,\n );\n const detachedRows = await tx\n .select({ storageKey: documentAttachmentBlobProjection.storageKey })\n .from(documentAttachmentBlobProjection)\n .where(\n and(\n eq(documentAttachmentBlobProjection.localId, localId),\n isNotNull(documentAttachmentBlobProjection.detachedAt),\n ),\n );\n return {\n detachedStorageKeys: detachedRows.map((row) => row.storageKey),\n existingDocument,\n pendingAttachments,\n };\n}\n\n// Staged-upload rows AND detached local-attachment markers both go: a marker\n// for a locally-discarded detach would otherwise keep filtering the slot out\n// after a re-pull restores it, leaving the attachment permanently invisible.\nasync function clearDiscardedDocumentRows(input: {\n candidate: DiscardCandidate;\n localId: string;\n lockedExecSql: ExecSql;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n const { candidate, localId, lockedExecSql, tx } = input;\n await tx\n .delete(documentPendingAttachments)\n .where(eq(documentPendingAttachments.localId, localId))\n .run();\n for (const pendingAttachment of candidate.pendingAttachments) {\n await tx\n .delete(documentAttachmentBlobProjection)\n .where(\n and(\n eq(documentAttachmentBlobProjection.localId, localId),\n eq(documentAttachmentBlobProjection.slotId, pendingAttachment.slotId),\n eq(\n documentAttachmentBlobProjection.storageKey,\n pendingAttachment.storageKey,\n ),\n ),\n )\n .run();\n }\n await tx\n .delete(documentAttachmentBlobProjection)\n .where(\n and(\n eq(documentAttachmentBlobProjection.localId, localId),\n isNotNull(documentAttachmentBlobProjection.detachedAt),\n ),\n )\n .run();\n const records = new DocumentRecordRepository(lockedExecSql);\n await records.deleteDocumentHistory(getDocumentScope(localId));\n await records.clearDocumentSyncFailure(getDocumentScope(localId));\n}\n\nasync function saveDiscardShell(input: {\n candidate: DiscardCandidate;\n documentProjectors: DocumentProjectorRegistry;\n localId: string;\n lockedExecSql: ExecSql;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n const { candidate, documentProjectors, localId, lockedExecSql, tx } = input;\n const shellDocument = buildDiscardShellDocument(\n localId,\n candidate.existingDocument,\n );\n // The document-kind projection derives from discarded content, so clear it\n // inside the transaction and let the next pull rebuild it.\n await documentProjectors.deleteStoredDocumentClientProjection({\n documentKind: shellDocument.documentKind ?? DEFAULT_DOCUMENT_KIND,\n execSql: lockedExecSql,\n localId,\n });\n const updatedAt = await resolveDocumentSaveTimestamp({\n document: shellDocument,\n tx,\n });\n if (!(await saveDocumentRows({ document: shellDocument, tx, updatedAt }))) {\n throw new Error(\"Document recovery generation changed during discard\");\n }\n}\n\n// Eligibility, teardown, client-projection clearing, and shell replacement\n// share one immediate transaction, so a second connection cannot relink the\n// local id between identity validation and destructive writes.\nasync function discardDocumentRowsToShell(input: {\n documentProjectors: DocumentProjectorRegistry;\n expectedDocumentId: string;\n localId: string;\n lockedExecSql: ExecSql;\n persistence: Pick<\n DocumentsPersistence,\n \"listPendingAttachments\" | \"loadDocument\"\n >;\n}): Promise<DiscardDocumentToShellResult> {\n const clientProjectionTables =\n input.documentProjectors.getClientProjectionTables();\n if (clientProjectionTables.length > 0) {\n await ensureSqlTables(input.lockedExecSql, clientProjectionTables);\n }\n return getClientSQLitePersistenceRuntime(input.lockedExecSql).transaction(\n async (tx) => {\n const candidate = await loadDiscardCandidate({ ...input, tx });\n if (!candidate) return { discarded: false };\n await clearDiscardedDocumentRows({\n candidate,\n localId: input.localId,\n lockedExecSql: input.lockedExecSql,\n tx,\n });\n await saveDiscardShell({ ...input, candidate, tx });\n // The deleted rows were these bytes' only pointers from this document,\n // but a hydrated copy is shared by every slot holding the same blob.\n // Queue them for the reference-checked reclaim instead of deleting.\n await queueDocumentAttachmentStorageKeys(tx, [\n ...new Set([\n ...candidate.pendingAttachments.map((row) => row.storageKey),\n ...candidate.detachedStorageKeys,\n ]),\n ]);\n return {\n discarded: true,\n documentKind:\n candidate.existingDocument.documentKind ?? DEFAULT_DOCUMENT_KIND,\n };\n },\n { behavior: \"immediate\" },\n );\n}\n\n/**\n * Atomically convert a stuck document's local state to the\n * freshly-discovered-share shell: drop its queued updates, staged attachment\n * rows (and their settled local-attachment halves, which share the staged\n * storage keys the caller reclaims), detached attachment markers, durable\n * history, recorded sync failure, and document-kind client projection, then\n * overwrite the record with an empty snapshot that keeps its identity,\n * placement, title, and kind. Everything commits in ONE transaction.\n *\n * Refused under the serialized mutation when the document is local-only or\n * unlinked (its rows are the only copy), when its documentId no longer\n * matches the caller's expectation (a stale caller or racing relink must not\n * discard a different identity's edits), or when any move intent references\n * it (the local containerId is then the move's optimistic placement, and\n * reseeding it as server truth would silently commit the move locally while\n * discarding the intent that was meant to perform it).\n */\nexport async function discardStoredDocumentToShell(input: {\n documentProjectors: DocumentProjectorRegistryInput;\n execSql: ExecSql;\n expectedDocumentId: string;\n localId: string;\n persistence: Pick<\n DocumentsPersistence,\n \"listPendingAttachments\" | \"loadDocument\"\n >;\n}): Promise<DiscardDocumentToShellResult> {\n const { expectedDocumentId, localId, persistence } = input;\n return runSerializedSqlMutation(input.execSql, async (lockedExecSql) => {\n await ensureSqlTables(lockedExecSql, documentPlacementIntentTables);\n return discardDocumentRowsToShell({\n documentProjectors: resolveDocumentProjectorRegistry(\n input.documentProjectors,\n ),\n expectedDocumentId,\n localId,\n lockedExecSql,\n persistence,\n });\n });\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import { KeyingVerificationError } from "../../../../internal/crypto/index.js";
|
|
|
2
2
|
import { DEFAULT_DOCUMENT_KIND, DOCUMENTS_APP_KIND, } from "../../../documents/documentConstants.js";
|
|
3
3
|
import { loadDocumentMovePlacements, } from "../../container-contents/documentPlacement.js";
|
|
4
4
|
import { hasDocumentPurgeCheckpointInTransaction, listPurgedDocumentIdsInTransaction, } from "../../keyingCheckpoints/documentPurgeCheckpointPersistence.js";
|
|
5
|
-
import {
|
|
5
|
+
import { DocumentRecordRepository } from "../records/index.js";
|
|
6
6
|
import { discoveredDocumentPlacement, resolveDiscoveredDocumentPlacement, } from "./discoveredDocumentPlacement.js";
|
|
7
7
|
import { toDocumentSummary } from "./documentProjectionRows.js";
|
|
8
8
|
import { documentSaveTimestampFrom, resolveDocumentSaveTimestamp, saveDocumentRows, saveDocumentRowsBatch, } from "./documentRows.js";
|
|
@@ -40,7 +40,7 @@ async function upsertDiscoveredDocumentWithExec(execSql, input, pendingCreates,
|
|
|
40
40
|
if (await hasDocumentPurgeCheckpointInTransaction(tx, input.documentId)) {
|
|
41
41
|
throw purgedDocumentError();
|
|
42
42
|
}
|
|
43
|
-
const existingLocalId = await findLocalIdByDocumentId(
|
|
43
|
+
const existingLocalId = await new DocumentRecordRepository(execSql).findLocalIdByDocumentId(DOCUMENTS_APP_KIND, input.documentId);
|
|
44
44
|
const localId = existingLocalId ?? input.documentId;
|
|
45
45
|
const pendingLocalId = pendingCreates.get(input.documentId);
|
|
46
46
|
if (!existingLocalId && pendingLocalId) {
|
|
@@ -78,7 +78,7 @@ async function upsertDiscoveredDocumentsPerRow(execSql, inputs, pendingCreates,
|
|
|
78
78
|
async function resolveDiscoveryTargets(execSql, inputs, pendingCreates, tx) {
|
|
79
79
|
const documentIds = inputs.map((input) => input.documentId);
|
|
80
80
|
const purged = await listPurgedDocumentIdsInTransaction(tx, documentIds);
|
|
81
|
-
const existingLocalIds = await findLocalIdsByDocumentIds(
|
|
81
|
+
const existingLocalIds = await new DocumentRecordRepository(execSql).findLocalIdsByDocumentIds(DOCUMENTS_APP_KIND, documentIds);
|
|
82
82
|
const targets = [];
|
|
83
83
|
const pendingLocalIds = new Set();
|
|
84
84
|
for (const input of inputs) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discoveredDocumentUpsert.js","sourceRoot":"","sources":["discoveredDocumentUpsert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EACL,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,sCAAsC,CAAC;AAO9C,OAAO,EAEL,0BAA0B,GAC3B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACL,uCAAuC,EACvC,kCAAkC,GACnC,MAAM,4DAA4D,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEhF,OAAO,EACL,2BAA2B,EAC3B,kCAAkC,GACnC,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EACL,yBAAyB,EACzB,4BAA4B,EAC5B,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,oCAAoC,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AACnE,OAAO,EACL,uBAAuB,EACvB,wBAAwB,GAEzB,MAAM,uBAAuB,CAAC;AAE/B,MAAM,qCAAqC,GAAG,qBAAqB,CAAC;AAEpE,SAAS,mBAAmB;IAC1B,OAAO,IAAI,uBAAuB,CAChC,UAAU,EACV,0DAA0D,CAC3D,CA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CAAC;QACrD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QACpE,OAAO,iBAAiB,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport {\n DEFAULT_DOCUMENT_KIND,\n DOCUMENTS_APP_KIND,\n} from \"../../../documents/documentConstants\";\nimport type {\n DiscoveredDocumentInput,\n DocumentSummary,\n} from \"../../../documents/documentSummary\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport {\n type DocumentMovePlacement,\n loadDocumentMovePlacements,\n} from \"../../container-contents/documentPlacement\";\nimport {\n hasDocumentPurgeCheckpointInTransaction,\n listPurgedDocumentIdsInTransaction,\n} from \"../../keyingCheckpoints/documentPurgeCheckpointPersistence\";\nimport { findLocalIdByDocumentId, findLocalIdsByDocumentIds } from \"../records\";\nimport type { StoredDocumentRecord } from \"../types\";\nimport {\n discoveredDocumentPlacement,\n resolveDiscoveredDocumentPlacement,\n} from \"./discoveredDocumentPlacement\";\nimport { toDocumentSummary } from \"./documentProjectionRows\";\nimport {\n documentSaveTimestampFrom,\n resolveDocumentSaveTimestamp,\n saveDocumentRows,\n saveDocumentRowsBatch,\n} from \"./documentRows\";\nimport { resolvePersistedDocumentRuntimeState } from \"./documentRuntimeState\";\nimport { loadPendingCreateSummary } from \"./pendingCreateAdoption\";\nimport {\n loadStoredDocumentState,\n loadStoredDocumentStates,\n type StoredDocumentState,\n} from \"./rowQueryPersistence\";\n\nconst DISCOVERED_DOCUMENT_PLACEHOLDER_TITLE = \"Syncing document...\";\n\nfunction purgedDocumentError(): KeyingVerificationError {\n return new KeyingVerificationError(\n \"rollback\",\n \"Document discovery targets a permanently purged document\",\n );\n}\n\nfunction fenceSkippedError(): Error {\n return new Error(\"Document recovery generation changed during discovery\");\n}\n\nfunction nextDiscoveredDocument(\n localId: string,\n input: DiscoveredDocumentInput,\n existingDocument: StoredDocumentRecord | null,\n placement: ReturnType<typeof discoveredDocumentPlacement>,\n): StoredDocumentRecord {\n return {\n id: localId,\n documentId: input.documentId,\n documentKind: existingDocument?.documentKind ?? DEFAULT_DOCUMENT_KIND,\n text: existingDocument?.text ?? \"\",\n title: existingDocument?.title ?? DISCOVERED_DOCUMENT_PLACEHOLDER_TITLE,\n snapshotEndVersion: existingDocument?.snapshotEndVersion ?? \"\",\n recoveryGeneration: existingDocument?.recoveryGeneration ?? 0,\n ...placement,\n effectiveAccessLevel:\n input.effectiveAccessLevel ??\n existingDocument?.effectiveAccessLevel ??\n null,\n ...resolvePersistedDocumentRuntimeState(existingDocument, {\n accessEpoch: placement.accessEpoch,\n documentId: input.documentId,\n }),\n };\n}\n\n/** One discovered document, read and written on its own (the per-row path). */\nasync function upsertDiscoveredDocumentWithExec(\n execSql: ExecSql,\n input: DiscoveredDocumentInput,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DocumentSummary> {\n if (await hasDocumentPurgeCheckpointInTransaction(tx, input.documentId)) {\n throw purgedDocumentError();\n }\n const existingLocalId = await findLocalIdByDocumentId(\n execSql,\n DOCUMENTS_APP_KIND,\n input.documentId,\n );\n const localId = existingLocalId ?? input.documentId;\n const pendingLocalId = pendingCreates.get(input.documentId);\n if (!existingLocalId && pendingLocalId) {\n const pending = await loadPendingCreateSummary(execSql, pendingLocalId);\n if (!pending) throw new Error(\"Pending create projection is unavailable\");\n return pending;\n }\n const existingDocument =\n (await loadStoredDocumentState(execSql, localId))?.record ?? null;\n const placement = await resolveDiscoveredDocumentPlacement(\n tx,\n existingDocument,\n input,\n );\n const nextDocument = nextDiscoveredDocument(\n localId,\n input,\n existingDocument,\n placement,\n );\n const updatedAt = await resolveDocumentSaveTimestamp({\n document: nextDocument,\n options: existingDocument ? undefined : { updatedAt: input.createdAt },\n tx,\n });\n if (!(await saveDocumentRows({ document: nextDocument, tx, updatedAt }))) {\n throw fenceSkippedError();\n }\n return toDocumentSummary(nextDocument, updatedAt);\n}\n\nasync function upsertDiscoveredDocumentsPerRow(\n execSql: ExecSql,\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DocumentSummary[]> {\n const summaries: DocumentSummary[] = [];\n for (const input of inputs) {\n summaries.push(\n await upsertDiscoveredDocumentWithExec(\n execSql,\n input,\n pendingCreates,\n tx,\n ),\n );\n }\n return summaries;\n}\n\ntype DiscoveryTarget =\n | { kind: \"pending\"; summary: DocumentSummary }\n | { input: DiscoveredDocumentInput; kind: \"write\"; localId: string };\n\n/**\n * Resolves every input to its pending-create summary or its target local id,\n * in input order, so the first purged or unavailable input throws exactly as\n * the per-row walk would. Returns null when one input's write could change\n * another input's reads, which only the per-row walk handles.\n */\nasync function resolveDiscoveryTargets(\n execSql: ExecSql,\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DiscoveryTarget[] | null> {\n const documentIds = inputs.map((input) => input.documentId);\n const purged = await listPurgedDocumentIdsInTransaction(tx, documentIds);\n const existingLocalIds = await findLocalIdsByDocumentIds(\n execSql,\n DOCUMENTS_APP_KIND,\n documentIds,\n );\n const targets: DiscoveryTarget[] = [];\n const pendingLocalIds = new Set<string>();\n for (const input of inputs) {\n if (purged.has(input.documentId)) throw purgedDocumentError();\n const existingLocalId = existingLocalIds.get(input.documentId);\n const pendingLocalId = pendingCreates.get(input.documentId);\n if (!existingLocalId && pendingLocalId) {\n const pending = await loadPendingCreateSummary(execSql, pendingLocalId);\n if (!pending) throw new Error(\"Pending create projection is unavailable\");\n pendingLocalIds.add(pendingLocalId);\n targets.push({ kind: \"pending\", summary: pending });\n continue;\n }\n targets.push({\n input,\n kind: \"write\",\n localId: existingLocalId ?? input.documentId,\n });\n }\n const writeLocalIds = targets.flatMap((target) =>\n target.kind === \"write\" ? [target.localId] : [],\n );\n const independent =\n new Set(writeLocalIds).size === writeLocalIds.length &&\n !writeLocalIds.some((localId) => pendingLocalIds.has(localId));\n return independent ? targets : null;\n}\n\n/**\n * Whether writing each target leaves every other target's reads unchanged: a\n * stored row at a target local id that carries a different document id would\n * be re-pointed by its write, which another input's lookup could have read.\n */\nfunction targetsReadDisjointRows(\n targets: ReadonlyArray<DiscoveryTarget>,\n states: ReadonlyMap<string, StoredDocumentState>,\n): boolean {\n return targets.every(\n (target) =>\n target.kind !== \"write\" ||\n (states.get(target.localId)?.record.documentId ??\n target.input.documentId) === target.input.documentId,\n );\n}\n\nfunction plannedDiscoveryWrite(\n target: Extract<DiscoveryTarget, { kind: \"write\" }>,\n state: StoredDocumentState | undefined,\n intent: DocumentMovePlacement | undefined,\n) {\n const existingDocument = state?.record ?? null;\n const document = nextDiscoveredDocument(\n target.localId,\n target.input,\n existingDocument,\n discoveredDocumentPlacement(existingDocument, target.input, intent),\n );\n // The joined state read is the row and projection the per-row walk reads\n // again for its timestamp; nothing earlier in this transaction writes it.\n const updatedAt = state\n ? documentSaveTimestampFrom(\n {\n projection: state.projection,\n snapshotEndVersion: state.record.snapshotEndVersion,\n },\n document,\n )\n : target.input.createdAt;\n return { document, updatedAt };\n}\n\n/**\n * Discovery as set statements: one read per table for every input, then\n * multi-row upserts. Inputs whose rows could interact (a repeated document\n * id, a shared local id, or a row another input would re-point) keep the\n * per-row walk, which reads each input after the previous one's writes.\n */\nexport async function upsertDiscoveredDocumentsInTransaction(\n execSql: ExecSql,\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DocumentSummary[]> {\n const documentIds = inputs.map((input) => input.documentId);\n if (new Set(documentIds).size !== documentIds.length) {\n return upsertDiscoveredDocumentsPerRow(execSql, inputs, pendingCreates, tx);\n }\n const targets = await resolveDiscoveryTargets(\n execSql,\n inputs,\n pendingCreates,\n tx,\n );\n if (!targets) {\n return upsertDiscoveredDocumentsPerRow(execSql, inputs, pendingCreates, tx);\n }\n const writes = targets.flatMap((target) =>\n target.kind === \"write\" ? [target] : [],\n );\n if (writes.length === 0) {\n return targets.flatMap((target) =>\n target.kind === \"pending\" ? [target.summary] : [],\n );\n }\n const states = await loadStoredDocumentStates(\n execSql,\n writes.map((target) => target.localId),\n );\n if (!targetsReadDisjointRows(targets, states)) {\n return upsertDiscoveredDocumentsPerRow(execSql, inputs, pendingCreates, tx);\n }\n const intents = await loadDocumentMovePlacements(\n tx,\n writes.map((target) => target.input.documentId),\n );\n const planned = new Map(\n writes.map((target) => [\n target.localId,\n plannedDiscoveryWrite(\n target,\n states.get(target.localId),\n intents.get(target.input.documentId),\n ),\n ]),\n );\n const saved = await saveDocumentRowsBatch({\n entries: [...planned.values()],\n tx,\n });\n if (saved.size !== planned.size) throw fenceSkippedError();\n return targets.map((target) => {\n if (target.kind === \"pending\") return target.summary;\n const write = planned.get(target.localId);\n if (!write) throw new Error(\"Discovered document write is missing\");\n return toDocumentSummary(write.document, write.updatedAt);\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"discoveredDocumentUpsert.js","sourceRoot":"","sources":["discoveredDocumentUpsert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EACL,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,sCAAsC,CAAC;AAO9C,OAAO,EAEL,0BAA0B,GAC3B,MAAM,4CAA4C,CAAC;AACpD,OAAO,EACL,uCAAuC,EACvC,kCAAkC,GACnC,MAAM,4DAA4D,CAAC;AACpE,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,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,CAAC,MAAM,EAAE,EAAE,CAAC;QACrB,MAAM,CAAC,OAAO;QACd,qBAAqB,CACnB,MAAM,EACN,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,EAC1B,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CACrC;KACF,CAAC,CACH,CAAC;IACF,MAAM,KAAK,GAAG,MAAM,qBAAqB,CAAC;QACxC,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC9B,EAAE;KACH,CAAC,CAAC;IACH,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI;QAAE,MAAM,iBAAiB,EAAE,CAAC;IAC3D,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE;QAC5B,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS;YAAE,OAAO,MAAM,CAAC,OAAO,CAAC;QACrD,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1C,IAAI,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;QACpE,OAAO,iBAAiB,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport {\n DEFAULT_DOCUMENT_KIND,\n DOCUMENTS_APP_KIND,\n} from \"../../../documents/documentConstants\";\nimport type {\n DiscoveredDocumentInput,\n DocumentSummary,\n} from \"../../../documents/documentSummary\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport {\n type DocumentMovePlacement,\n loadDocumentMovePlacements,\n} from \"../../container-contents/documentPlacement\";\nimport {\n hasDocumentPurgeCheckpointInTransaction,\n listPurgedDocumentIdsInTransaction,\n} from \"../../keyingCheckpoints/documentPurgeCheckpointPersistence\";\nimport { DocumentRecordRepository } from \"../records\";\nimport type { StoredDocumentRecord } from \"../types\";\nimport {\n discoveredDocumentPlacement,\n resolveDiscoveredDocumentPlacement,\n} from \"./discoveredDocumentPlacement\";\nimport { toDocumentSummary } from \"./documentProjectionRows\";\nimport {\n documentSaveTimestampFrom,\n resolveDocumentSaveTimestamp,\n saveDocumentRows,\n saveDocumentRowsBatch,\n} from \"./documentRows\";\nimport { resolvePersistedDocumentRuntimeState } from \"./documentRuntimeState\";\nimport { loadPendingCreateSummary } from \"./pendingCreateAdoption\";\nimport {\n loadStoredDocumentState,\n loadStoredDocumentStates,\n type StoredDocumentState,\n} from \"./rowQueryPersistence\";\n\nconst DISCOVERED_DOCUMENT_PLACEHOLDER_TITLE = \"Syncing document...\";\n\nfunction purgedDocumentError(): KeyingVerificationError {\n return new KeyingVerificationError(\n \"rollback\",\n \"Document discovery targets a permanently purged document\",\n );\n}\n\nfunction fenceSkippedError(): Error {\n return new Error(\"Document recovery generation changed during discovery\");\n}\n\nfunction nextDiscoveredDocument(\n localId: string,\n input: DiscoveredDocumentInput,\n existingDocument: StoredDocumentRecord | null,\n placement: ReturnType<typeof discoveredDocumentPlacement>,\n): StoredDocumentRecord {\n return {\n id: localId,\n documentId: input.documentId,\n documentKind: existingDocument?.documentKind ?? DEFAULT_DOCUMENT_KIND,\n text: existingDocument?.text ?? \"\",\n title: existingDocument?.title ?? DISCOVERED_DOCUMENT_PLACEHOLDER_TITLE,\n snapshotEndVersion: existingDocument?.snapshotEndVersion ?? \"\",\n recoveryGeneration: existingDocument?.recoveryGeneration ?? 0,\n ...placement,\n effectiveAccessLevel:\n input.effectiveAccessLevel ??\n existingDocument?.effectiveAccessLevel ??\n null,\n ...resolvePersistedDocumentRuntimeState(existingDocument, {\n accessEpoch: placement.accessEpoch,\n documentId: input.documentId,\n }),\n };\n}\n\n/** One discovered document, read and written on its own (the per-row path). */\nasync function upsertDiscoveredDocumentWithExec(\n execSql: ExecSql,\n input: DiscoveredDocumentInput,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DocumentSummary> {\n if (await hasDocumentPurgeCheckpointInTransaction(tx, input.documentId)) {\n throw purgedDocumentError();\n }\n const existingLocalId = await new DocumentRecordRepository(\n execSql,\n ).findLocalIdByDocumentId(DOCUMENTS_APP_KIND, input.documentId);\n const localId = existingLocalId ?? input.documentId;\n const pendingLocalId = pendingCreates.get(input.documentId);\n if (!existingLocalId && pendingLocalId) {\n const pending = await loadPendingCreateSummary(execSql, pendingLocalId);\n if (!pending) throw new Error(\"Pending create projection is unavailable\");\n return pending;\n }\n const existingDocument =\n (await loadStoredDocumentState(execSql, localId))?.record ?? null;\n const placement = await resolveDiscoveredDocumentPlacement(\n tx,\n existingDocument,\n input,\n );\n const nextDocument = nextDiscoveredDocument(\n localId,\n input,\n existingDocument,\n placement,\n );\n const updatedAt = await resolveDocumentSaveTimestamp({\n document: nextDocument,\n options: existingDocument ? undefined : { updatedAt: input.createdAt },\n tx,\n });\n if (!(await saveDocumentRows({ document: nextDocument, tx, updatedAt }))) {\n throw fenceSkippedError();\n }\n return toDocumentSummary(nextDocument, updatedAt);\n}\n\nasync function upsertDiscoveredDocumentsPerRow(\n execSql: ExecSql,\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DocumentSummary[]> {\n const summaries: DocumentSummary[] = [];\n for (const input of inputs) {\n summaries.push(\n await upsertDiscoveredDocumentWithExec(\n execSql,\n input,\n pendingCreates,\n tx,\n ),\n );\n }\n return summaries;\n}\n\ntype DiscoveryTarget =\n | { kind: \"pending\"; summary: DocumentSummary }\n | { input: DiscoveredDocumentInput; kind: \"write\"; localId: string };\n\n/**\n * Resolves every input to its pending-create summary or its target local id,\n * in input order, so the first purged or unavailable input throws exactly as\n * the per-row walk would. Returns null when one input's write could change\n * another input's reads, which only the per-row walk handles.\n */\nasync function resolveDiscoveryTargets(\n execSql: ExecSql,\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DiscoveryTarget[] | null> {\n const documentIds = inputs.map((input) => input.documentId);\n const purged = await listPurgedDocumentIdsInTransaction(tx, documentIds);\n const existingLocalIds = await new DocumentRecordRepository(\n execSql,\n ).findLocalIdsByDocumentIds(DOCUMENTS_APP_KIND, documentIds);\n const targets: DiscoveryTarget[] = [];\n const pendingLocalIds = new Set<string>();\n for (const input of inputs) {\n if (purged.has(input.documentId)) throw purgedDocumentError();\n const existingLocalId = existingLocalIds.get(input.documentId);\n const pendingLocalId = pendingCreates.get(input.documentId);\n if (!existingLocalId && pendingLocalId) {\n const pending = await loadPendingCreateSummary(execSql, pendingLocalId);\n if (!pending) throw new Error(\"Pending create projection is unavailable\");\n pendingLocalIds.add(pendingLocalId);\n targets.push({ kind: \"pending\", summary: pending });\n continue;\n }\n targets.push({\n input,\n kind: \"write\",\n localId: existingLocalId ?? input.documentId,\n });\n }\n const writeLocalIds = targets.flatMap((target) =>\n target.kind === \"write\" ? [target.localId] : [],\n );\n const independent =\n new Set(writeLocalIds).size === writeLocalIds.length &&\n !writeLocalIds.some((localId) => pendingLocalIds.has(localId));\n return independent ? targets : null;\n}\n\n/**\n * Whether writing each target leaves every other target's reads unchanged: a\n * stored row at a target local id that carries a different document id would\n * be re-pointed by its write, which another input's lookup could have read.\n */\nfunction targetsReadDisjointRows(\n targets: ReadonlyArray<DiscoveryTarget>,\n states: ReadonlyMap<string, StoredDocumentState>,\n): boolean {\n return targets.every(\n (target) =>\n target.kind !== \"write\" ||\n (states.get(target.localId)?.record.documentId ??\n target.input.documentId) === target.input.documentId,\n );\n}\n\nfunction plannedDiscoveryWrite(\n target: Extract<DiscoveryTarget, { kind: \"write\" }>,\n state: StoredDocumentState | undefined,\n intent: DocumentMovePlacement | undefined,\n) {\n const existingDocument = state?.record ?? null;\n const document = nextDiscoveredDocument(\n target.localId,\n target.input,\n existingDocument,\n discoveredDocumentPlacement(existingDocument, target.input, intent),\n );\n // The joined state read is the row and projection the per-row walk reads\n // again for its timestamp; nothing earlier in this transaction writes it.\n const updatedAt = state\n ? documentSaveTimestampFrom(\n {\n projection: state.projection,\n snapshotEndVersion: state.record.snapshotEndVersion,\n },\n document,\n )\n : target.input.createdAt;\n return { document, updatedAt };\n}\n\n/**\n * Discovery as set statements: one read per table for every input, then\n * multi-row upserts. Inputs whose rows could interact (a repeated document\n * id, a shared local id, or a row another input would re-point) keep the\n * per-row walk, which reads each input after the previous one's writes.\n */\nexport async function upsertDiscoveredDocumentsInTransaction(\n execSql: ExecSql,\n inputs: ReadonlyArray<DiscoveredDocumentInput>,\n pendingCreates: ReadonlyMap<string, string>,\n tx: ClientSQLiteTransactionScope,\n): Promise<DocumentSummary[]> {\n const documentIds = inputs.map((input) => input.documentId);\n if (new Set(documentIds).size !== documentIds.length) {\n return upsertDiscoveredDocumentsPerRow(execSql, inputs, pendingCreates, tx);\n }\n const targets = await resolveDiscoveryTargets(\n execSql,\n inputs,\n pendingCreates,\n tx,\n );\n if (!targets) {\n return upsertDiscoveredDocumentsPerRow(execSql, inputs, pendingCreates, tx);\n }\n const writes = targets.flatMap((target) =>\n target.kind === \"write\" ? [target] : [],\n );\n if (writes.length === 0) {\n return targets.flatMap((target) =>\n target.kind === \"pending\" ? [target.summary] : [],\n );\n }\n const states = await loadStoredDocumentStates(\n execSql,\n writes.map((target) => target.localId),\n );\n if (!targetsReadDisjointRows(targets, states)) {\n return upsertDiscoveredDocumentsPerRow(execSql, inputs, pendingCreates, tx);\n }\n const intents = await loadDocumentMovePlacements(\n tx,\n writes.map((target) => target.input.documentId),\n );\n const planned = new Map(\n writes.map((target) => [\n target.localId,\n plannedDiscoveryWrite(\n target,\n states.get(target.localId),\n intents.get(target.input.documentId),\n ),\n ]),\n );\n const saved = await saveDocumentRowsBatch({\n entries: [...planned.values()],\n tx,\n });\n if (saved.size !== planned.size) throw fenceSkippedError();\n return targets.map((target) => {\n if (target.kind === \"pending\") return target.summary;\n const write = planned.get(target.localId);\n if (!write) throw new Error(\"Discovered document write is missing\");\n return toDocumentSummary(write.document, write.updatedAt);\n });\n}\n"]}
|