@tearleads/client-sdk 0.1.210 → 0.1.212

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/client/organizations/index.js +5 -5
  2. package/client/organizations/index.js.map +1 -1
  3. package/client/organizations/principalMutationScope.js +7 -1
  4. package/client/organizations/principalMutationScope.js.map +1 -1
  5. package/client/organizations/principalMutations.d.ts +8 -6
  6. package/client/organizations/principalMutations.js +8 -8
  7. package/client/organizations/principalMutations.js.map +1 -1
  8. package/client/organizations/recipientAdmission.d.ts +2 -1
  9. package/client/organizations/recipientAdmission.js +2 -2
  10. package/client/organizations/recipientAdmission.js.map +1 -1
  11. package/client/runtime/workflowRuntime.d.ts +2 -1
  12. package/client/runtime/workflowRuntime.js +1 -1
  13. package/client/runtime/workflowRuntime.js.map +1 -1
  14. package/data/persistence/container-contents/containerContentsPersistence.js +2 -2
  15. package/data/persistence/container-contents/containerContentsPersistence.js.map +1 -1
  16. package/data/persistence/container-contents/containerMetadataMutationPersistence.js +2 -2
  17. package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
  18. package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js +2 -2
  19. package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js.map +1 -1
  20. package/data/persistence/container-contents/containerPendingUpdatePersistence.js +6 -6
  21. package/data/persistence/container-contents/containerPendingUpdatePersistence.js.map +1 -1
  22. package/data/persistence/documents/internal/discardDocument.js +4 -4
  23. package/data/persistence/documents/internal/discardDocument.js.map +1 -1
  24. package/data/persistence/documents/internal/discoveredDocumentUpsert.js +3 -3
  25. package/data/persistence/documents/internal/discoveredDocumentUpsert.js.map +1 -1
  26. package/data/persistence/documents/internal/documentDeletionPersistence.js +8 -8
  27. package/data/persistence/documents/internal/documentDeletionPersistence.js.map +1 -1
  28. package/data/persistence/documents/internal/documentMutationPersistence.js +4 -3
  29. package/data/persistence/documents/internal/documentMutationPersistence.js.map +1 -1
  30. package/data/persistence/documents/internal/documentPullContinuationPersistence.js +2 -2
  31. package/data/persistence/documents/internal/documentPullContinuationPersistence.js.map +1 -1
  32. package/data/persistence/documents/internal/rowQueryPersistence.js +3 -3
  33. package/data/persistence/documents/internal/rowQueryPersistence.js.map +1 -1
  34. package/data/persistence/documents/internal/syncQueuePersistence.js +12 -13
  35. package/data/persistence/documents/internal/syncQueuePersistence.js.map +1 -1
  36. package/data/persistence/documents/records/documentHistoryPersistence.d.ts +24 -20
  37. package/data/persistence/documents/records/documentHistoryPersistence.js +59 -90
  38. package/data/persistence/documents/records/documentHistoryPersistence.js.map +1 -1
  39. package/data/persistence/documents/records/documentPendingUpdatePersistence.d.ts +10 -25
  40. package/data/persistence/documents/records/documentPendingUpdatePersistence.js +19 -79
  41. package/data/persistence/documents/records/documentPendingUpdatePersistence.js.map +1 -1
  42. package/data/persistence/documents/records/documentQueuedUpdateReplacement.d.ts +4 -14
  43. package/data/persistence/documents/records/documentQueuedUpdateReplacement.js +27 -42
  44. package/data/persistence/documents/records/documentQueuedUpdateReplacement.js.map +1 -1
  45. package/data/persistence/documents/records/documentRecordPersistence.d.ts +5 -8
  46. package/data/persistence/documents/records/documentRecordPersistence.js +2 -55
  47. package/data/persistence/documents/records/documentRecordPersistence.js.map +1 -1
  48. package/data/persistence/documents/records/documentRecordRepository.d.ts +133 -0
  49. package/data/persistence/documents/records/documentRecordRepository.js +286 -0
  50. package/data/persistence/documents/records/documentRecordRepository.js.map +1 -0
  51. package/data/persistence/documents/records/documentSyncFailurePersistence.d.ts +9 -12
  52. package/data/persistence/documents/records/documentSyncFailurePersistence.js +19 -34
  53. package/data/persistence/documents/records/documentSyncFailurePersistence.js.map +1 -1
  54. package/data/persistence/documents/records/index.d.ts +4 -19
  55. package/data/persistence/documents/records/index.js +5 -29
  56. package/data/persistence/documents/records/index.js.map +1 -1
  57. package/package.json +1 -1
  58. package/stores/documents/documentStore/sync/attachmentUploadFailure.js +2 -2
  59. package/stores/documents/documentStore/sync/attachmentUploadFailure.js.map +1 -1
  60. package/stores/documents/documentStore/sync/syncAttachments.js +2 -2
  61. package/stores/documents/documentStore/sync/syncAttachments.js.map +1 -1
  62. package/stores/documents/documentStore/sync/syncShared.js +2 -2
  63. package/stores/documents/documentStore/sync/syncShared.js.map +1 -1
  64. package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js +2 -2
  65. package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js.map +1 -1
  66. package/workflows/container-contents/pendingWrites/index.js +4 -3
  67. package/workflows/container-contents/pendingWrites/index.js.map +1 -1
  68. package/workflows/container-contents/recovery/accessRestoration.js +2 -2
  69. package/workflows/container-contents/recovery/accessRestoration.js.map +1 -1
  70. package/workflows/documents/index.d.ts +1 -1
  71. package/workflows/documents/index.js +1 -1
  72. package/workflows/documents/index.js.map +1 -1
  73. package/workflows/documents/syncRekeys/syncRecoveryRekey.js +3 -2
  74. package/workflows/documents/syncRekeys/syncRecoveryRekey.js.map +1 -1
  75. package/workflows/organizations/readModel/listLocalOrganizations.js +2 -2
  76. package/workflows/organizations/readModel/listLocalOrganizations.js.map +1 -1
@@ -1,6 +1,5 @@
1
1
  import { and, asc, eq, inArray, isNotNull, lt, or, sql, } from "drizzle-orm";
2
2
  import { documentHistoryUpdates, documents, } from "../../../sqlite/schema/documentRecordSchema.js";
3
- import { getClientSQLitePersistenceRuntime } from "../../../sqlite/sqlitePersistenceRuntime.js";
4
3
  /**
5
4
  * Queued updates are the rows of the local update log that carry an
6
5
  * `updateId`. A tail row (origin "local") stays in the log after settlement as
@@ -49,8 +48,7 @@ export async function hasQueuedDocumentUpdates(db, scope) {
49
48
  ]);
50
49
  return ids.length > 0;
51
50
  }
52
- export async function listDocumentPendingUpdates(execSql, scope) {
53
- const { db } = getClientSQLitePersistenceRuntime(execSql);
51
+ export async function selectDocumentPendingUpdates(db, scope) {
54
52
  const rows = await db
55
53
  .select({
56
54
  id: documentHistoryUpdates.updateId,
@@ -101,42 +99,11 @@ export function queuedUpdateRow(input) {
101
99
  * Queue an update that has no history-tail role: container metadata (its
102
100
  * content is its checkpoint) and full-state re-uploads after a remote reset.
103
101
  */
104
- export async function enqueueDocumentPendingUpdate(execSql, scope, pendingUpdate) {
105
- const updateId = crypto.randomUUID();
106
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
107
- await db
108
- .insert(documentHistoryUpdates)
109
- .values(queuedUpdateRow({
110
- createdAt: new Date().toISOString(),
111
- origin: OUTBOX_ORIGIN,
112
- pendingUpdate,
113
- scope,
114
- updateId,
115
- }))
116
- .run();
117
- });
118
- return updateId;
119
- }
120
- /**
121
- * Append a locally authored update to the history tail AND queue it, as one
122
- * row in one transaction. A crash can therefore never leave the op restorable
123
- * from history while missing from the outgoing queue, where a null-marker
124
- * restore would classify it as covered and never send it.
125
- */
126
- export async function enqueueDocumentPendingUpdateWithHistory(execSql, scope, pendingUpdate, options) {
127
- const createdAt = new Date().toISOString();
128
- return getClientSQLitePersistenceRuntime(execSql).transaction(async (tx) => {
129
- if (options &&
130
- !(await documentWriteFenceMatchesInTransaction(tx, scope, options))) {
131
- return null;
132
- }
133
- return insertDocumentPendingUpdateWithHistoryInTransaction({
134
- createdAt,
135
- pendingUpdate,
136
- scope,
137
- tx,
138
- });
139
- }, { behavior: "immediate" });
102
+ export async function insertDocumentPendingUpdateInTransaction(tx, input) {
103
+ await tx
104
+ .insert(documentHistoryUpdates)
105
+ .values(queuedUpdateRow({ ...input, origin: OUTBOX_ORIGIN }))
106
+ .run();
140
107
  }
141
108
  export async function insertDocumentPendingUpdateWithHistoryInTransaction(input) {
142
109
  const updateId = crypto.randomUUID();
@@ -174,37 +141,17 @@ export async function insertDocumentPendingUpdateWithHistoryInTransaction(input)
174
141
  * passes per launch.
175
142
  */
176
143
  export const MAX_PENDING_UPDATE_REKEYS = 5;
177
- /**
178
- * Reset the durable re-key budget for every queued update in a scope. The
179
- * cap exists to stop a server that keeps conflicting fresh ids from driving
180
- * a network-speed loop — but a budget burned during an unrelated outage
181
- * (e.g. yesterday's stale-bundle deadlock 409ing every submit) should not be
182
- * terminal forever. An explicit manual retry is the deliberate, rate-limited
183
- * signal that conditions changed, so it gets a fresh budget.
184
- */
185
- export async function resetDocumentPendingUpdateRekeyBudget(execSql, scope) {
186
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
187
- await db
188
- .update(documentHistoryUpdates)
189
- .set({ rekeyCount: 0 })
190
- .where(queuedInScope(scope));
191
- });
192
- }
193
- export async function rekeyDocumentPendingUpdate(execSql, id) {
194
- const nextId = crypto.randomUUID();
195
- let rekeyed = false;
196
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
197
- const updated = await db
198
- .update(documentHistoryUpdates)
199
- .set({
200
- updateId: nextId,
201
- rekeyCount: sql `${documentHistoryUpdates.rekeyCount} + 1`,
202
- })
203
- .where(and(eq(documentHistoryUpdates.updateId, id), lt(documentHistoryUpdates.rekeyCount, MAX_PENDING_UPDATE_REKEYS)))
204
- .returning({ id: documentHistoryUpdates.updateId });
205
- rekeyed = updated.length > 0;
206
- });
207
- return rekeyed ? nextId : null;
144
+ /** Re-key one queued row below the bound; true when the UPDATE matched it. */
145
+ export async function rekeyDocumentPendingUpdateInTransaction(tx, id, nextId) {
146
+ const updated = await tx
147
+ .update(documentHistoryUpdates)
148
+ .set({
149
+ updateId: nextId,
150
+ rekeyCount: sql `${documentHistoryUpdates.rekeyCount} + 1`,
151
+ })
152
+ .where(and(eq(documentHistoryUpdates.updateId, id), lt(documentHistoryUpdates.rekeyCount, MAX_PENDING_UPDATE_REKEYS)))
153
+ .returning({ id: documentHistoryUpdates.updateId });
154
+ return updated.length > 0;
208
155
  }
209
156
  /**
210
157
  * Settle queued updates: an outbox row is deleted, and a tail row stops being
@@ -235,10 +182,8 @@ async function unqueueSelectedUpdates(tx, selected) {
235
182
  .run();
236
183
  }
237
184
  /** Settle one queued update by id, whatever its scope. */
238
- export async function settleDocumentPendingUpdate(execSql, id) {
239
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
240
- await unqueueSelectedUpdates(db, eq(documentHistoryUpdates.updateId, id));
241
- });
185
+ export async function settleDocumentPendingUpdateInTransaction(tx, id) {
186
+ await unqueueSelectedUpdates(tx, eq(documentHistoryUpdates.updateId, id));
242
187
  }
243
188
  /**
244
189
  * Stop queueing every update in the given scopes: outbox rows are dropped,
@@ -250,9 +195,4 @@ export async function unqueueDocumentScopesInTransaction(tx, scopes) {
250
195
  return;
251
196
  await unqueueSelectedUpdates(tx, or(...scopes.map(queuedInScope)));
252
197
  }
253
- export async function unqueueDocumentPendingUpdates(execSql, scope) {
254
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
255
- await unqueueDocumentScopesInTransaction(db, [scope]);
256
- });
257
- }
258
198
  //# sourceMappingURL=documentPendingUpdatePersistence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"documentPendingUpdatePersistence.js","sourceRoot":"","sources":["documentPendingUpdatePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,GAAG,EACH,EAAE,EACF,OAAO,EACP,SAAS,EACT,EAAE,EACF,EAAE,EAEF,GAAG,GACJ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,sBAAsB,EACtB,SAAS,GACV,MAAM,6CAA6C,CAAC;AAErD,OAAO,EAAE,iCAAiC,EAAE,MAAM,0CAA0C,CAAC;AAU7F;;;;;GAKG;AACH,MAAM,aAAa,GAAG,QAAQ,CAAC;AAK/B,MAAM,UAAU,aAAa,CAAC,KAAoB;IAChD,OAAO,GAAG,CACR,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,SAAS,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAC3C,CAAC;AACJ,CAAC;AAED,SAAS,wBAAwB,CAC/B,GAA6B;IAE7B,yEAA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AI;QACzB,UAAU,EAAE,CAAC;KACd,CAAC;SACD,KAAK,CAAC,QAAQ,CAAC;SACf,GAAG,EAAE,CAAC;AACX,CAAC;AAED,0DAA0D;AAC1D,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAC/C,OAAgB,EAChB,EAAU;IAEV,MAAM,iCAAiC,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QACxE,MAAM,sBAAsB,CAAC,EAAE,EAAE,EAAE,CAAC,sBAAsB,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,kCAAkC,CACtD,EAAkB,EAClB,MAAgC;IAEhC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IAChC,MAAM,sBAAsB,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;AACrE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,OAAgB,EAChB,KAAoB;IAEpB,MAAM,iCAAiC,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QACxE,MAAM,kCAAkC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n and,\n asc,\n eq,\n inArray,\n isNotNull,\n lt,\n or,\n type SQL,\n sql,\n} from \"drizzle-orm\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport { getClientSQLitePersistenceRuntime } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport type {\n DocumentScope,\n PendingUpdateFields,\n PendingUpdateOrigin,\n QueuedPendingUpdateRecord,\n SelectedPendingUpdateRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * Queued updates are the rows of the local update log that carry an\n * `updateId`. A tail row (origin \"local\") stays in the log after settlement as\n * history until compaction; an \"outbox\" row has no tail role and settlement\n * deletes it.\n */\nconst OUTBOX_ORIGIN = \"outbox\";\n\ntype QueueStatement = Pick<ClientSQLiteTransactionScope, \"delete\" | \"update\">;\ntype QueueRead = Pick<ClientSQLiteTransactionScope, \"selectDistinct\">;\n\nexport function queuedInScope(scope: DocumentScope) {\n return and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n isNotNull(documentHistoryUpdates.updateId),\n );\n}\n\nfunction mapSelectedPendingUpdate(\n row: SelectedPendingUpdateRow,\n): QueuedPendingUpdateRecord {\n // A queued row always carries its submission vectors; a row without them\n // cannot be submitted and must fail closed rather than send empty ranges.\n if (\n row.partialStartVersionVector === null ||\n row.partialEndVersionVector === null\n ) {\n throw new Error(\"Queued document update is missing its version vectors\");\n }\n if (row.origin !== \"local\" && row.origin !== OUTBOX_ORIGIN) {\n throw new Error(\"Queued document update has an unqueueable origin\");\n }\n return {\n id: String(row.id ?? \"\"),\n origin: row.origin,\n updateData: row.updateData,\n partialStartVersionVector: row.partialStartVersionVector,\n partialEndVersionVector: row.partialEndVersionVector,\n sourceVersionVector: row.sourceVersionVector,\n rekeyCount: row.rekeyCount,\n };\n}\n\n/** The ids among `localIds` (one app kind) with at least one queued update. */\nexport async function listLocalIdsWithQueuedUpdates(\n db: QueueRead,\n appKind: string,\n localIds: readonly string[],\n): Promise<string[]> {\n const uniqueIds = [...new Set(localIds)];\n if (uniqueIds.length === 0) return [];\n const rows = await db\n .selectDistinct({ localId: documentHistoryUpdates.localId })\n .from(documentHistoryUpdates)\n .where(\n and(\n eq(documentHistoryUpdates.appKind, appKind),\n inArray(documentHistoryUpdates.localId, uniqueIds),\n isNotNull(documentHistoryUpdates.updateId),\n ),\n )\n .orderBy(asc(documentHistoryUpdates.localId));\n return rows.map((row) => row.localId);\n}\n\nexport async function hasQueuedDocumentUpdates(\n db: QueueRead,\n scope: DocumentScope,\n): Promise<boolean> {\n const ids = await listLocalIdsWithQueuedUpdates(db, scope.appKind, [\n scope.localId,\n ]);\n return ids.length > 0;\n}\n\nexport async function listDocumentPendingUpdates(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<QueuedPendingUpdateRecord[]> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select({\n id: documentHistoryUpdates.updateId,\n origin: documentHistoryUpdates.origin,\n updateData: documentHistoryUpdates.updateData,\n partialStartVersionVector:\n documentHistoryUpdates.partialStartVersionVector,\n partialEndVersionVector: documentHistoryUpdates.partialEndVersionVector,\n sourceVersionVector: documentHistoryUpdates.sourceVersionVector,\n rekeyCount: documentHistoryUpdates.rekeyCount,\n })\n .from(documentHistoryUpdates)\n .where(queuedInScope(scope))\n .orderBy(...queueOrder());\n\n return rows.map(mapSelectedPendingUpdate);\n}\n\n/** Queue order: insertion time, then insertion order within one instant. */\nexport function queueOrder() {\n return [asc(documentHistoryUpdates.createdAt), asc(sql`rowid`)] as const;\n}\n\n/**\n * Write fence inside an open transaction: the scope's document still has the\n * captured server document id and recovery generation.\n */\nexport async function documentWriteFenceMatchesInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"select\">,\n scope: DocumentScope,\n fence: {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n },\n): Promise<boolean> {\n const matchingDocuments = await tx\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n sql`${documents.documentId} IS ${fence.expectedDocumentId}`,\n eq(documents.recoveryGeneration, fence.expectedRecoveryGeneration),\n ),\n )\n .limit(1);\n return matchingDocuments.length > 0;\n}\n\n/** One log row for a queued update; `origin` decides its tail role. */\nexport function queuedUpdateRow(input: {\n createdAt: string;\n origin: PendingUpdateOrigin;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n updateId: string;\n}) {\n return {\n id: crypto.randomUUID(),\n appKind: input.scope.appKind,\n localId: input.scope.localId,\n updateData: input.pendingUpdate.updateData,\n origin: input.origin,\n createdAt: input.createdAt,\n updateId: input.updateId,\n partialStartVersionVector: input.pendingUpdate.partialStartVersionVector,\n partialEndVersionVector: input.pendingUpdate.partialEndVersionVector,\n sourceVersionVector: input.pendingUpdate.sourceVersionVector ?? null,\n };\n}\n\n/**\n * Queue an update that has no history-tail role: container metadata (its\n * content is its checkpoint) and full-state re-uploads after a remote reset.\n */\nexport async function enqueueDocumentPendingUpdate(\n execSql: ExecSql,\n scope: DocumentScope,\n pendingUpdate: PendingUpdateFields,\n): Promise<string> {\n const updateId = crypto.randomUUID();\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await db\n .insert(documentHistoryUpdates)\n .values(\n queuedUpdateRow({\n createdAt: new Date().toISOString(),\n origin: OUTBOX_ORIGIN,\n pendingUpdate,\n scope,\n updateId,\n }),\n )\n .run();\n });\n return updateId;\n}\n\n/**\n * Append a locally authored update to the history tail AND queue it, as one\n * row in one transaction. A crash can therefore never leave the op restorable\n * from history while missing from the outgoing queue, where a null-marker\n * restore would classify it as covered and never send it.\n */\nexport async function enqueueDocumentPendingUpdateWithHistory(\n execSql: ExecSql,\n scope: DocumentScope,\n pendingUpdate: PendingUpdateFields,\n options?: {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n },\n): Promise<string | null> {\n const createdAt = new Date().toISOString();\n return getClientSQLitePersistenceRuntime(execSql).transaction(\n async (tx) => {\n if (\n options &&\n !(await documentWriteFenceMatchesInTransaction(tx, scope, options))\n ) {\n return null;\n }\n\n return insertDocumentPendingUpdateWithHistoryInTransaction({\n createdAt,\n pendingUpdate,\n scope,\n tx,\n });\n },\n { behavior: \"immediate\" },\n );\n}\n\nexport async function insertDocumentPendingUpdateWithHistoryInTransaction(input: {\n createdAt: string;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n tx: ClientSQLiteTransactionScope;\n}): Promise<string> {\n const updateId = crypto.randomUUID();\n await input.tx\n .insert(documentHistoryUpdates)\n .values(\n queuedUpdateRow({\n createdAt: input.createdAt,\n origin: \"local\",\n pendingUpdate: input.pendingUpdate,\n scope: input.scope,\n updateId,\n }),\n )\n .run();\n return updateId;\n}\n\n/**\n * Assign a fresh id to a queued update, keeping every other column. A lost\n * sync ack leaves the server holding the update under the original id with\n * ciphertext a rebuilt submission can never reproduce (each rebuild encrypts\n * with a fresh IV), so the id is permanently poisoned: every resubmission\n * 409s and the whole outgoing batch rolls back. Re-keying lets the next sync\n * pass submit the same ops under a conflict-free id; the server-side copy is\n * harmless because CRDT update import is idempotent.\n *\n * Returns null when no row carries the id, so callers never report progress\n * for a row that lives in some other queue (or nowhere). The UPDATE's own\n * RETURNING row is the progress signal — a pre-flight SELECT would race a\n * second tab sharing the SQLite file, which can settle the row between the\n * read and the write and turn \"rekeyed\" into a spurious re-arm.\n *\n * The persisted per-row bound is enforced here as well: a row already at\n * MAX_PENDING_UPDATE_REKEYS is left untouched and reported as no progress.\n * The in-memory lane counter bounds a busy-loop within one session; this\n * bound survives restarts, so a poisoned row cannot re-key forever at three\n * passes per launch.\n */\nexport const MAX_PENDING_UPDATE_REKEYS = 5;\n\n/**\n * Reset the durable re-key budget for every queued update in a scope. The\n * cap exists to stop a server that keeps conflicting fresh ids from driving\n * a network-speed loop — but a budget burned during an unrelated outage\n * (e.g. yesterday's stale-bundle deadlock 409ing every submit) should not be\n * terminal forever. An explicit manual retry is the deliberate, rate-limited\n * signal that conditions changed, so it gets a fresh budget.\n */\nexport async function resetDocumentPendingUpdateRekeyBudget(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await db\n .update(documentHistoryUpdates)\n .set({ rekeyCount: 0 })\n .where(queuedInScope(scope));\n });\n}\n\nexport async function rekeyDocumentPendingUpdate(\n execSql: ExecSql,\n id: string,\n): Promise<string | null> {\n const nextId = crypto.randomUUID();\n let rekeyed = false;\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n const updated = await db\n .update(documentHistoryUpdates)\n .set({\n updateId: nextId,\n rekeyCount: sql`${documentHistoryUpdates.rekeyCount} + 1`,\n })\n .where(\n and(\n eq(documentHistoryUpdates.updateId, id),\n lt(documentHistoryUpdates.rekeyCount, MAX_PENDING_UPDATE_REKEYS),\n ),\n )\n .returning({ id: documentHistoryUpdates.updateId });\n rekeyed = updated.length > 0;\n });\n return rekeyed ? nextId : null;\n}\n\n/**\n * Settle queued updates: an outbox row is deleted, and a tail row stops being\n * queued but stays as history until compaction folds it into a checkpoint.\n */\nexport async function settleQueuedDocumentUpdatesInTransaction(\n tx: QueueStatement,\n scope: DocumentScope,\n updateIds: readonly string[],\n): Promise<void> {\n const uniqueIds = [...new Set(updateIds)];\n if (uniqueIds.length === 0) return;\n const selected = and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n inArray(documentHistoryUpdates.updateId, uniqueIds),\n );\n await unqueueSelectedUpdates(tx, selected);\n}\n\nasync function unqueueSelectedUpdates(\n tx: QueueStatement,\n selected: SQL | undefined,\n): Promise<void> {\n await tx\n .delete(documentHistoryUpdates)\n .where(and(selected, eq(documentHistoryUpdates.origin, OUTBOX_ORIGIN)))\n .run();\n await tx\n .update(documentHistoryUpdates)\n .set({\n updateId: null,\n partialStartVersionVector: null,\n partialEndVersionVector: null,\n sourceVersionVector: null,\n rekeyCount: 0,\n })\n .where(selected)\n .run();\n}\n\n/** Settle one queued update by id, whatever its scope. */\nexport async function settleDocumentPendingUpdate(\n execSql: ExecSql,\n id: string,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await unqueueSelectedUpdates(db, eq(documentHistoryUpdates.updateId, id));\n });\n}\n\n/**\n * Stop queueing every update in the given scopes: outbox rows are dropped,\n * and local tail rows keep their content as history (it is already in the\n * document).\n */\nexport async function unqueueDocumentScopesInTransaction(\n tx: QueueStatement,\n scopes: readonly DocumentScope[],\n): Promise<void> {\n if (scopes.length === 0) return;\n await unqueueSelectedUpdates(tx, or(...scopes.map(queuedInScope)));\n}\n\nexport async function unqueueDocumentPendingUpdates(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await unqueueDocumentScopesInTransaction(db, [scope]);\n });\n}\n"]}
1
+ {"version":3,"file":"documentPendingUpdatePersistence.js","sourceRoot":"","sources":["documentPendingUpdatePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,GAAG,EACH,EAAE,EACF,OAAO,EACP,SAAS,EACT,EAAE,EACF,EAAE,EAEF,GAAG,GACJ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,sBAAsB,EACtB,SAAS,GACV,MAAM,6CAA6C,CAAC;AAUrD;;;;;GAKG;AACH,MAAM,aAAa,GAAG,QAAQ,CAAC;AAK/B,MAAM,UAAU,aAAa,CAAC,KAAoB;IAChD,OAAO,GAAG,CACR,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,SAAS,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAC3C,CAAC;AACJ,CAAC;AAED,SAAS,wBAAwB,CAC/B,GAA6B;IAE7B,yEAAyE;IACzE,0EAA0E;IAC1E,IACE,GAAG,CAAC,yBAAyB,KAAK,IAAI;QACtC,GAAG,CAAC,uBAAuB,KAAK,IAAI,EACpC,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC3E,CAAC;IACD,IAAI,GAAG,CAAC,MAAM,KA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AAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;AACrE,CAAC","sourcesContent":["import {\n and,\n asc,\n eq,\n inArray,\n isNotNull,\n lt,\n or,\n type SQL,\n sql,\n} from \"drizzle-orm\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type {\n DocumentScope,\n PendingUpdateFields,\n PendingUpdateOrigin,\n QueuedPendingUpdateRecord,\n SelectedPendingUpdateRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * Queued updates are the rows of the local update log that carry an\n * `updateId`. A tail row (origin \"local\") stays in the log after settlement as\n * history until compaction; an \"outbox\" row has no tail role and settlement\n * deletes it.\n */\nconst OUTBOX_ORIGIN = \"outbox\";\n\ntype QueueStatement = Pick<ClientSQLiteTransactionScope, \"delete\" | \"update\">;\ntype QueueRead = Pick<ClientSQLiteTransactionScope, \"selectDistinct\">;\n\nexport function queuedInScope(scope: DocumentScope) {\n return and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n isNotNull(documentHistoryUpdates.updateId),\n );\n}\n\nfunction mapSelectedPendingUpdate(\n row: SelectedPendingUpdateRow,\n): QueuedPendingUpdateRecord {\n // A queued row always carries its submission vectors; a row without them\n // cannot be submitted and must fail closed rather than send empty ranges.\n if (\n row.partialStartVersionVector === null ||\n row.partialEndVersionVector === null\n ) {\n throw new Error(\"Queued document update is missing its version vectors\");\n }\n if (row.origin !== \"local\" && row.origin !== OUTBOX_ORIGIN) {\n throw new Error(\"Queued document update has an unqueueable origin\");\n }\n return {\n id: String(row.id ?? \"\"),\n origin: row.origin,\n updateData: row.updateData,\n partialStartVersionVector: row.partialStartVersionVector,\n partialEndVersionVector: row.partialEndVersionVector,\n sourceVersionVector: row.sourceVersionVector,\n rekeyCount: row.rekeyCount,\n };\n}\n\n/** The ids among `localIds` (one app kind) with at least one queued update. */\nexport async function listLocalIdsWithQueuedUpdates(\n db: QueueRead,\n appKind: string,\n localIds: readonly string[],\n): Promise<string[]> {\n const uniqueIds = [...new Set(localIds)];\n if (uniqueIds.length === 0) return [];\n const rows = await db\n .selectDistinct({ localId: documentHistoryUpdates.localId })\n .from(documentHistoryUpdates)\n .where(\n and(\n eq(documentHistoryUpdates.appKind, appKind),\n inArray(documentHistoryUpdates.localId, uniqueIds),\n isNotNull(documentHistoryUpdates.updateId),\n ),\n )\n .orderBy(asc(documentHistoryUpdates.localId));\n return rows.map((row) => row.localId);\n}\n\nexport async function hasQueuedDocumentUpdates(\n db: QueueRead,\n scope: DocumentScope,\n): Promise<boolean> {\n const ids = await listLocalIdsWithQueuedUpdates(db, scope.appKind, [\n scope.localId,\n ]);\n return ids.length > 0;\n}\n\nexport async function selectDocumentPendingUpdates(\n db: Pick<ClientSQLiteTransactionScope, \"select\">,\n scope: DocumentScope,\n): Promise<QueuedPendingUpdateRecord[]> {\n const rows = await db\n .select({\n id: documentHistoryUpdates.updateId,\n origin: documentHistoryUpdates.origin,\n updateData: documentHistoryUpdates.updateData,\n partialStartVersionVector:\n documentHistoryUpdates.partialStartVersionVector,\n partialEndVersionVector: documentHistoryUpdates.partialEndVersionVector,\n sourceVersionVector: documentHistoryUpdates.sourceVersionVector,\n rekeyCount: documentHistoryUpdates.rekeyCount,\n })\n .from(documentHistoryUpdates)\n .where(queuedInScope(scope))\n .orderBy(...queueOrder());\n\n return rows.map(mapSelectedPendingUpdate);\n}\n\n/** Queue order: insertion time, then insertion order within one instant. */\nexport function queueOrder() {\n return [asc(documentHistoryUpdates.createdAt), asc(sql`rowid`)] as const;\n}\n\n/**\n * Write fence inside an open transaction: the scope's document still has the\n * captured server document id and recovery generation.\n */\nexport async function documentWriteFenceMatchesInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"select\">,\n scope: DocumentScope,\n fence: {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n },\n): Promise<boolean> {\n const matchingDocuments = await tx\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n sql`${documents.documentId} IS ${fence.expectedDocumentId}`,\n eq(documents.recoveryGeneration, fence.expectedRecoveryGeneration),\n ),\n )\n .limit(1);\n return matchingDocuments.length > 0;\n}\n\n/** One log row for a queued update; `origin` decides its tail role. */\nexport function queuedUpdateRow(input: {\n createdAt: string;\n origin: PendingUpdateOrigin;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n updateId: string;\n}) {\n return {\n id: crypto.randomUUID(),\n appKind: input.scope.appKind,\n localId: input.scope.localId,\n updateData: input.pendingUpdate.updateData,\n origin: input.origin,\n createdAt: input.createdAt,\n updateId: input.updateId,\n partialStartVersionVector: input.pendingUpdate.partialStartVersionVector,\n partialEndVersionVector: input.pendingUpdate.partialEndVersionVector,\n sourceVersionVector: input.pendingUpdate.sourceVersionVector ?? null,\n };\n}\n\n/**\n * Queue an update that has no history-tail role: container metadata (its\n * content is its checkpoint) and full-state re-uploads after a remote reset.\n */\nexport async function insertDocumentPendingUpdateInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"insert\">,\n input: {\n createdAt: string;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n updateId: string;\n },\n): Promise<void> {\n await tx\n .insert(documentHistoryUpdates)\n .values(queuedUpdateRow({ ...input, origin: OUTBOX_ORIGIN }))\n .run();\n}\n\nexport async function insertDocumentPendingUpdateWithHistoryInTransaction(input: {\n createdAt: string;\n pendingUpdate: PendingUpdateFields;\n scope: DocumentScope;\n tx: ClientSQLiteTransactionScope;\n}): Promise<string> {\n const updateId = crypto.randomUUID();\n await input.tx\n .insert(documentHistoryUpdates)\n .values(\n queuedUpdateRow({\n createdAt: input.createdAt,\n origin: \"local\",\n pendingUpdate: input.pendingUpdate,\n scope: input.scope,\n updateId,\n }),\n )\n .run();\n return updateId;\n}\n\n/**\n * Assign a fresh id to a queued update, keeping every other column. A lost\n * sync ack leaves the server holding the update under the original id with\n * ciphertext a rebuilt submission can never reproduce (each rebuild encrypts\n * with a fresh IV), so the id is permanently poisoned: every resubmission\n * 409s and the whole outgoing batch rolls back. Re-keying lets the next sync\n * pass submit the same ops under a conflict-free id; the server-side copy is\n * harmless because CRDT update import is idempotent.\n *\n * Returns null when no row carries the id, so callers never report progress\n * for a row that lives in some other queue (or nowhere). The UPDATE's own\n * RETURNING row is the progress signal — a pre-flight SELECT would race a\n * second tab sharing the SQLite file, which can settle the row between the\n * read and the write and turn \"rekeyed\" into a spurious re-arm.\n *\n * The persisted per-row bound is enforced here as well: a row already at\n * MAX_PENDING_UPDATE_REKEYS is left untouched and reported as no progress.\n * The in-memory lane counter bounds a busy-loop within one session; this\n * bound survives restarts, so a poisoned row cannot re-key forever at three\n * passes per launch.\n */\nexport const MAX_PENDING_UPDATE_REKEYS = 5;\n\n/** Re-key one queued row below the bound; true when the UPDATE matched it. */\nexport async function rekeyDocumentPendingUpdateInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"update\">,\n id: string,\n nextId: string,\n): Promise<boolean> {\n const updated = await tx\n .update(documentHistoryUpdates)\n .set({\n updateId: nextId,\n rekeyCount: sql`${documentHistoryUpdates.rekeyCount} + 1`,\n })\n .where(\n and(\n eq(documentHistoryUpdates.updateId, id),\n lt(documentHistoryUpdates.rekeyCount, MAX_PENDING_UPDATE_REKEYS),\n ),\n )\n .returning({ id: documentHistoryUpdates.updateId });\n return updated.length > 0;\n}\n\n/**\n * Settle queued updates: an outbox row is deleted, and a tail row stops being\n * queued but stays as history until compaction folds it into a checkpoint.\n */\nexport async function settleQueuedDocumentUpdatesInTransaction(\n tx: QueueStatement,\n scope: DocumentScope,\n updateIds: readonly string[],\n): Promise<void> {\n const uniqueIds = [...new Set(updateIds)];\n if (uniqueIds.length === 0) return;\n const selected = and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n inArray(documentHistoryUpdates.updateId, uniqueIds),\n );\n await unqueueSelectedUpdates(tx, selected);\n}\n\nasync function unqueueSelectedUpdates(\n tx: QueueStatement,\n selected: SQL | undefined,\n): Promise<void> {\n await tx\n .delete(documentHistoryUpdates)\n .where(and(selected, eq(documentHistoryUpdates.origin, OUTBOX_ORIGIN)))\n .run();\n await tx\n .update(documentHistoryUpdates)\n .set({\n updateId: null,\n partialStartVersionVector: null,\n partialEndVersionVector: null,\n sourceVersionVector: null,\n rekeyCount: 0,\n })\n .where(selected)\n .run();\n}\n\n/** Settle one queued update by id, whatever its scope. */\nexport async function settleDocumentPendingUpdateInTransaction(\n tx: QueueStatement,\n id: string,\n): Promise<void> {\n await unqueueSelectedUpdates(tx, eq(documentHistoryUpdates.updateId, id));\n}\n\n/**\n * Stop queueing every update in the given scopes: outbox rows are dropped,\n * and local tail rows keep their content as history (it is already in the\n * document).\n */\nexport async function unqueueDocumentScopesInTransaction(\n tx: QueueStatement,\n scopes: readonly DocumentScope[],\n): Promise<void> {\n if (scopes.length === 0) return;\n await unqueueSelectedUpdates(tx, or(...scopes.map(queuedInScope)));\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { ExecSql } from "../../../sqlite/sqlSchema.js";
1
+ import type { ClientSQLiteTransactionScope } from "../../../sqlite/sqlitePersistenceRuntime.js";
2
2
  import type { DocumentScope, PendingUpdateFields } from "./documentPersistenceTypes.js";
3
3
  /** Replace a run of queued local updates with one merged update. */
4
4
  export interface QueuedUpdateReplacement {
@@ -11,17 +11,7 @@ export interface QueuedUpdateReplacement {
11
11
  rekeyCount: number;
12
12
  }
13
13
  /**
14
- * Replace queued local updates with one merged update, all or nothing, in
15
- * one immediate transaction. Every check must hold or nothing changes and
16
- * the result is null: the document still has the captured document id and
17
- * recovery generation, and the members are still queued under their ids as
18
- * ordinary local rows (no outbox row, no rotation checkpoint) forming one
19
- * contiguous run in queue order that starts with the first listed member.
20
- *
21
- * The first member's row is updated in place, so it keeps its row id,
22
- * `created_at` and rowid (its queue and history-tail position), and receives
23
- * a fresh update id (an id is never reused for different content), the
24
- * merged data and vectors, and `rekeyCount`. The other members are deleted.
25
- * Resolves to the merged row's new update id.
14
+ * The checks and writes of `replaceDocumentQueuedUpdates`, inside the
15
+ * caller's transaction: null unless every check holds.
26
16
  */
27
- export declare function replaceDocumentQueuedUpdates(execSql: ExecSql, scope: DocumentScope, replacement: QueuedUpdateReplacement): Promise<string | null>;
17
+ export declare function replaceDocumentQueuedUpdatesInTransaction(tx: ClientSQLiteTransactionScope, scope: DocumentScope, replacement: QueuedUpdateReplacement): Promise<string | null>;
@@ -1,6 +1,5 @@
1
1
  import { eq, inArray } from "drizzle-orm";
2
2
  import { documentHistoryUpdates } from "../../../sqlite/schema/documentRecordSchema.js";
3
- import { getClientSQLitePersistenceRuntime } from "../../../sqlite/sqlitePersistenceRuntime.js";
4
3
  import { documentWriteFenceMatchesInTransaction, queuedInScope, queueOrder, } from "./documentPendingUpdatePersistence.js";
5
4
  /**
6
5
  * The row ids of the members when they are still exactly a contiguous run of
@@ -35,48 +34,34 @@ async function contiguousMemberRowIds(tx, scope, memberUpdateIds) {
35
34
  return rowIds.length === memberUpdateIds.length ? rowIds : null;
36
35
  }
37
36
  /**
38
- * Replace queued local updates with one merged update, all or nothing, in
39
- * one immediate transaction. Every check must hold or nothing changes and
40
- * the result is null: the document still has the captured document id and
41
- * recovery generation, and the members are still queued under their ids as
42
- * ordinary local rows (no outbox row, no rotation checkpoint) forming one
43
- * contiguous run in queue order that starts with the first listed member.
44
- *
45
- * The first member's row is updated in place, so it keeps its row id,
46
- * `created_at` and rowid (its queue and history-tail position), and receives
47
- * a fresh update id (an id is never reused for different content), the
48
- * merged data and vectors, and `rekeyCount`. The other members are deleted.
49
- * Resolves to the merged row's new update id.
37
+ * The checks and writes of `replaceDocumentQueuedUpdates`, inside the
38
+ * caller's transaction: null unless every check holds.
50
39
  */
51
- export async function replaceDocumentQueuedUpdates(execSql, scope, replacement) {
52
- if (replacement.memberUpdateIds.length < 2)
40
+ export async function replaceDocumentQueuedUpdatesInTransaction(tx, scope, replacement) {
41
+ if (!(await documentWriteFenceMatchesInTransaction(tx, scope, replacement))) {
53
42
  return null;
54
- return getClientSQLitePersistenceRuntime(execSql).transaction(async (tx) => {
55
- if (!(await documentWriteFenceMatchesInTransaction(tx, scope, replacement))) {
56
- return null;
57
- }
58
- const rowIds = await contiguousMemberRowIds(tx, scope, replacement.memberUpdateIds);
59
- const [firstRowId, ...mergedAwayRowIds] = rowIds ?? [];
60
- if (firstRowId === undefined)
61
- return null;
62
- const updateId = crypto.randomUUID();
63
- await tx
64
- .update(documentHistoryUpdates)
65
- .set({
66
- updateId,
67
- updateData: replacement.merged.updateData,
68
- partialStartVersionVector: replacement.merged.partialStartVersionVector,
69
- partialEndVersionVector: replacement.merged.partialEndVersionVector,
70
- sourceVersionVector: null,
71
- rekeyCount: replacement.rekeyCount,
72
- })
73
- .where(eq(documentHistoryUpdates.id, firstRowId))
74
- .run();
75
- await tx
76
- .delete(documentHistoryUpdates)
77
- .where(inArray(documentHistoryUpdates.id, mergedAwayRowIds))
78
- .run();
79
- return updateId;
80
- }, { behavior: "immediate" });
43
+ }
44
+ const rowIds = await contiguousMemberRowIds(tx, scope, replacement.memberUpdateIds);
45
+ const [firstRowId, ...mergedAwayRowIds] = rowIds ?? [];
46
+ if (firstRowId === undefined)
47
+ return null;
48
+ const updateId = crypto.randomUUID();
49
+ await tx
50
+ .update(documentHistoryUpdates)
51
+ .set({
52
+ updateId,
53
+ updateData: replacement.merged.updateData,
54
+ partialStartVersionVector: replacement.merged.partialStartVersionVector,
55
+ partialEndVersionVector: replacement.merged.partialEndVersionVector,
56
+ sourceVersionVector: null,
57
+ rekeyCount: replacement.rekeyCount,
58
+ })
59
+ .where(eq(documentHistoryUpdates.id, firstRowId))
60
+ .run();
61
+ await tx
62
+ .delete(documentHistoryUpdates)
63
+ .where(inArray(documentHistoryUpdates.id, mergedAwayRowIds))
64
+ .run();
65
+ return updateId;
81
66
  }
82
67
  //# sourceMappingURL=documentQueuedUpdateReplacement.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"documentQueuedUpdateReplacement.js","sourceRoot":"","sources":["documentQueuedUpdateReplacement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6CAA6C,CAAC;AAErF,OAAO,EAAE,iCAAiC,EAAE,MAAM,0CAA0C,CAAC;AAE7F,OAAO,EACL,sCAAsC,EACtC,aAAa,EACb,UAAU,GACX,MAAM,oCAAoC,CAAC;AAiB5C;;;;GAIG;AACH,KAAK,UAAU,sBAAsB,CACnC,EAAgC,EAChC,KAAoB,EACpB,eAAkC;IAElC,MAAM,MAAM,GAAG,MAAM,EAAE;SACpB,MAAM,CAAC;QACN,EAAE,EAAE,sBAAsB,CAAC,EAAE;QAC7B,MAAM,EAAE,sBAAsB,CAAC,MAAM;QACrC,mBAAmB,EAAE,sBAAsB,CAAC,mBAAmB;QAC/D,QAAQ,EAAE,sBAAsB,CAAC,QAAQ;KAC1C,CAAC;SACD,IAAI,CAAC,sBAAsB,CAAC;SAC5B,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;SAC3B,OAAO,CAAC,GAAG,UAAU,EAAE,CAAC,CAAC;IAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,KAAK,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7E,IAAI,KAAK,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IAC3B,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IAChE,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC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WAAW,CAAC,MAAM,CAAC,UAAU;YACzC,yBAAyB,EACvB,WAAW,CAAC,MAAM,CAAC,yBAAyB;YAC9C,uBAAuB,EAAE,WAAW,CAAC,MAAM,CAAC,uBAAuB;YACnE,mBAAmB,EAAE,IAAI;YACzB,UAAU,EAAE,WAAW,CAAC,UAAU;SACnC,CAAC;aACD,KAAK,CAAC,EAAE,CAAC,sBAAsB,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;aAChD,GAAG,EAAE,CAAC;QACT,MAAM,EAAE;aACL,MAAM,CAAC,sBAAsB,CAAC;aAC9B,KAAK,CAAC,OAAO,CAAC,sBAAsB,CAAC,EAAE,EAAE,gBAAgB,CAAC,CAAC;aAC3D,GAAG,EAAE,CAAC;QACT,OAAO,QAAQ,CAAC;IAClB,CAAC,EACD,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;AACJ,CAAC","sourcesContent":["import { eq, inArray } from \"drizzle-orm\";\nimport { documentHistoryUpdates } from \"../../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport { getClientSQLitePersistenceRuntime } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport {\n documentWriteFenceMatchesInTransaction,\n queuedInScope,\n queueOrder,\n} from \"./documentPendingUpdatePersistence\";\nimport type {\n DocumentScope,\n PendingUpdateFields,\n} from \"./documentPersistenceTypes\";\n\n/** Replace a run of queued local updates with one merged update. */\nexport interface QueuedUpdateReplacement {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n /** Members' current update ids, in queue order; at least two. */\n memberUpdateIds: readonly string[];\n /** The merged update. It is never a rotation checkpoint. */\n merged: Omit<PendingUpdateFields, \"sourceVersionVector\">;\n rekeyCount: number;\n}\n\n/**\n * The row ids of the members when they are still exactly a contiguous run of\n * ordinary local queued rows, starting with the first listed member;\n * otherwise null.\n */\nasync function contiguousMemberRowIds(\n tx: ClientSQLiteTransactionScope,\n scope: DocumentScope,\n memberUpdateIds: readonly string[],\n): Promise<string[] | null> {\n const queued = await tx\n .select({\n id: documentHistoryUpdates.id,\n origin: documentHistoryUpdates.origin,\n sourceVersionVector: documentHistoryUpdates.sourceVersionVector,\n updateId: documentHistoryUpdates.updateId,\n })\n .from(documentHistoryUpdates)\n .where(queuedInScope(scope))\n .orderBy(...queueOrder());\n const first = queued.findIndex((row) => row.updateId === memberUpdateIds[0]);\n if (first < 0) return null;\n const run = queued.slice(first, first + memberUpdateIds.length);\n const rowIds: string[] = [];\n for (const [index, row] of run.entries()) {\n if (\n row.id === null ||\n row.updateId !== memberUpdateIds[index] ||\n row.origin !== \"local\" ||\n row.sourceVersionVector !== null\n ) {\n return null;\n }\n rowIds.push(row.id);\n }\n return rowIds.length === memberUpdateIds.length ? rowIds : null;\n}\n\n/**\n * Replace queued local updates with one merged update, all or nothing, in\n * one immediate transaction. Every check must hold or nothing changes and\n * the result is null: the document still has the captured document id and\n * recovery generation, and the members are still queued under their ids as\n * ordinary local rows (no outbox row, no rotation checkpoint) forming one\n * contiguous run in queue order that starts with the first listed member.\n *\n * The first member's row is updated in place, so it keeps its row id,\n * `created_at` and rowid (its queue and history-tail position), and receives\n * a fresh update id (an id is never reused for different content), the\n * merged data and vectors, and `rekeyCount`. The other members are deleted.\n * Resolves to the merged row's new update id.\n */\nexport async function replaceDocumentQueuedUpdates(\n execSql: ExecSql,\n scope: DocumentScope,\n replacement: QueuedUpdateReplacement,\n): Promise<string | null> {\n if (replacement.memberUpdateIds.length < 2) return null;\n return getClientSQLitePersistenceRuntime(execSql).transaction(\n async (tx) => {\n if (\n !(await documentWriteFenceMatchesInTransaction(tx, scope, replacement))\n ) {\n return null;\n }\n const rowIds = await contiguousMemberRowIds(\n tx,\n scope,\n replacement.memberUpdateIds,\n );\n const [firstRowId, ...mergedAwayRowIds] = rowIds ?? [];\n if (firstRowId === undefined) return null;\n const updateId = crypto.randomUUID();\n await tx\n .update(documentHistoryUpdates)\n .set({\n updateId,\n updateData: replacement.merged.updateData,\n partialStartVersionVector:\n replacement.merged.partialStartVersionVector,\n partialEndVersionVector: replacement.merged.partialEndVersionVector,\n sourceVersionVector: null,\n rekeyCount: replacement.rekeyCount,\n })\n .where(eq(documentHistoryUpdates.id, firstRowId))\n .run();\n await tx\n .delete(documentHistoryUpdates)\n .where(inArray(documentHistoryUpdates.id, mergedAwayRowIds))\n .run();\n return updateId;\n },\n { behavior: \"immediate\" },\n );\n}\n"]}
1
+ {"version":3,"file":"documentQueuedUpdateReplacement.js","sourceRoot":"","sources":["documentQueuedUpdateReplacement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,sBAAsB,EAAE,MAAM,6CAA6C,CAAC;AAErF,OAAO,EACL,sCAAsC,EACtC,aAAa,EACb,UAAU,GACX,MAAM,oCAAoC,CAAC;AAiB5C;;;;GAIG;AACH,KAAK,UAAU,sBAAsB,CACnC,EAAgC,EAChC,KAAoB,EACpB,eAAkC;IAElC,MAAM,MAAM,GAAG,MAAM,EAAE;SACpB,MAAM,CAAC;QACN,EAAE,EAAE,sBAAsB,CAAC,EAAE;QAC7B,MAAM,EAAE,sBAAsB,CAAC,MAAM;QACrC,mBAAmB,EAAE,sBAAsB,CAAC,mBAAmB;QAC/D,QAAQ,EAAE,sBAAsB,CAAC,QAAQ;KAC1C,CAAC;SACD,IAAI,CAAC,sBAAsB,CAAC;SAC5B,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;SAC3B,OAAO,CAAC,GAAG,UAAU,EAAE,CAAC,CAAC;IAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,KAAK,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7E,IAAI,KAAK,GAAG,CAAC;QAAE,OAA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;QACH,QAAQ;QACR,UAAU,EAAE,WAAW,CAAC,MAAM,CAAC,UAAU;QACzC,yBAAyB,EAAE,WAAW,CAAC,MAAM,CAAC,yBAAyB;QACvE,uBAAuB,EAAE,WAAW,CAAC,MAAM,CAAC,uBAAuB;QACnE,mBAAmB,EAAE,IAAI;QACzB,UAAU,EAAE,WAAW,CAAC,UAAU;KACnC,CAAC;SACD,KAAK,CAAC,EAAE,CAAC,sBAAsB,CAAC,EAAE,EAAE,UAAU,CAAC,CAAC;SAChD,GAAG,EAAE,CAAC;IACT,MAAM,EAAE;SACL,MAAM,CAAC,sBAAsB,CAAC;SAC9B,KAAK,CAAC,OAAO,CAAC,sBAAsB,CAAC,EAAE,EAAE,gBAAgB,CAAC,CAAC;SAC3D,GAAG,EAAE,CAAC;IACT,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import { eq, inArray } from \"drizzle-orm\";\nimport { documentHistoryUpdates } from \"../../../sqlite/schema/documentRecordSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport {\n documentWriteFenceMatchesInTransaction,\n queuedInScope,\n queueOrder,\n} from \"./documentPendingUpdatePersistence\";\nimport type {\n DocumentScope,\n PendingUpdateFields,\n} from \"./documentPersistenceTypes\";\n\n/** Replace a run of queued local updates with one merged update. */\nexport interface QueuedUpdateReplacement {\n expectedDocumentId: string | null;\n expectedRecoveryGeneration: number;\n /** Members' current update ids, in queue order; at least two. */\n memberUpdateIds: readonly string[];\n /** The merged update. It is never a rotation checkpoint. */\n merged: Omit<PendingUpdateFields, \"sourceVersionVector\">;\n rekeyCount: number;\n}\n\n/**\n * The row ids of the members when they are still exactly a contiguous run of\n * ordinary local queued rows, starting with the first listed member;\n * otherwise null.\n */\nasync function contiguousMemberRowIds(\n tx: ClientSQLiteTransactionScope,\n scope: DocumentScope,\n memberUpdateIds: readonly string[],\n): Promise<string[] | null> {\n const queued = await tx\n .select({\n id: documentHistoryUpdates.id,\n origin: documentHistoryUpdates.origin,\n sourceVersionVector: documentHistoryUpdates.sourceVersionVector,\n updateId: documentHistoryUpdates.updateId,\n })\n .from(documentHistoryUpdates)\n .where(queuedInScope(scope))\n .orderBy(...queueOrder());\n const first = queued.findIndex((row) => row.updateId === memberUpdateIds[0]);\n if (first < 0) return null;\n const run = queued.slice(first, first + memberUpdateIds.length);\n const rowIds: string[] = [];\n for (const [index, row] of run.entries()) {\n if (\n row.id === null ||\n row.updateId !== memberUpdateIds[index] ||\n row.origin !== \"local\" ||\n row.sourceVersionVector !== null\n ) {\n return null;\n }\n rowIds.push(row.id);\n }\n return rowIds.length === memberUpdateIds.length ? rowIds : null;\n}\n\n/**\n * The checks and writes of `replaceDocumentQueuedUpdates`, inside the\n * caller's transaction: null unless every check holds.\n */\nexport async function replaceDocumentQueuedUpdatesInTransaction(\n tx: ClientSQLiteTransactionScope,\n scope: DocumentScope,\n replacement: QueuedUpdateReplacement,\n): Promise<string | null> {\n if (!(await documentWriteFenceMatchesInTransaction(tx, scope, replacement))) {\n return null;\n }\n const rowIds = await contiguousMemberRowIds(\n tx,\n scope,\n replacement.memberUpdateIds,\n );\n const [firstRowId, ...mergedAwayRowIds] = rowIds ?? [];\n if (firstRowId === undefined) return null;\n const updateId = crypto.randomUUID();\n await tx\n .update(documentHistoryUpdates)\n .set({\n updateId,\n updateData: replacement.merged.updateData,\n partialStartVersionVector: replacement.merged.partialStartVersionVector,\n partialEndVersionVector: replacement.merged.partialEndVersionVector,\n sourceVersionVector: null,\n rekeyCount: replacement.rekeyCount,\n })\n .where(eq(documentHistoryUpdates.id, firstRowId))\n .run();\n await tx\n .delete(documentHistoryUpdates)\n .where(inArray(documentHistoryUpdates.id, mergedAwayRowIds))\n .run();\n return updateId;\n}\n"]}
@@ -1,5 +1,4 @@
1
- import type { ExecSql } from "../../../sqlite/sqlSchema.js";
2
- import type { DocumentRecord, DocumentScope, SelectedDocumentRecordRow } from "./documentPersistenceTypes.js";
1
+ import type { DocumentRecord, SelectedDocumentRecordRow } from "./documentPersistenceTypes.js";
3
2
  /**
4
3
  * The ONE select shape for a full documents-row record: every reader that
5
4
  * feeds mapSelectedDocumentRecord spreads this, so a new record column cannot
@@ -270,11 +269,9 @@ export declare const documentRecordSelection: {
270
269
  }>;
271
270
  };
272
271
  export declare function mapSelectedDocumentRecord(row: SelectedDocumentRecordRow): DocumentRecord;
273
- export declare function loadDocumentRecord(execSql: ExecSql, scope: DocumentScope): Promise<DocumentRecord | null>;
274
- export declare function findLocalIdByDocumentId(execSql: ExecSql, appKind: string, documentId: string): Promise<string | null>;
275
272
  /**
276
- * `findLocalIdByDocumentId` for many document ids: one read per chunk, in the
277
- * same preference order, keeping the first row per document id.
273
+ * Duplicate remote identities can survive an interrupted local adoption, so
274
+ * the row to use for a document id is chosen by this order: queued or
275
+ * deferred edits first, then a stable fallback.
278
276
  */
279
- export declare function findLocalIdsByDocumentIds(execSql: ExecSql, appKind: string, documentIds: ReadonlyArray<string>): Promise<Map<string, string>>;
280
- export declare function deleteDocumentRecord(execSql: ExecSql, scope: DocumentScope): Promise<void>;
277
+ export declare function localIdPreferenceOrder(): import("drizzle-orm").SQL<unknown>[];
@@ -1,9 +1,7 @@
1
- import { and, desc, eq, inArray, sql } from "drizzle-orm";
1
+ import { desc, sql } from "drizzle-orm";
2
2
  import { deserializeDocumentSyncPullContinuation } from "../../../documents/shared/sync/pullContinuation.js";
3
3
  import { normalizeEffectiveAccessLevel } from "../../../keying/accessLevel.js";
4
4
  import { documentHistoryUpdates, documents, } from "../../../sqlite/schema/documentRecordSchema.js";
5
- import { sqliteRowsPerStatement } from "../../../sqlite/sqliteBindBudget.js";
6
- import { getClientSQLitePersistenceRuntime } from "../../../sqlite/sqlitePersistenceRuntime.js";
7
5
  /**
8
6
  * The ONE select shape for a full documents-row record: every reader that
9
7
  * feeds mapSelectedDocumentRecord spreads this, so a new record column cannot
@@ -58,21 +56,12 @@ export function mapSelectedDocumentRecord(row) {
58
56
  }
59
57
  return record;
60
58
  }
61
- export async function loadDocumentRecord(execSql, scope) {
62
- const { db } = getClientSQLitePersistenceRuntime(execSql);
63
- const rows = await db
64
- .select(documentRecordSelection)
65
- .from(documents)
66
- .where(and(eq(documents.appKind, scope.appKind), eq(documents.localId, scope.localId)))
67
- .limit(1);
68
- return rows[0] ? mapSelectedDocumentRecord(rows[0]) : null;
69
- }
70
59
  /**
71
60
  * Duplicate remote identities can survive an interrupted local adoption, so
72
61
  * the row to use for a document id is chosen by this order: queued or
73
62
  * deferred edits first, then a stable fallback.
74
63
  */
75
- function localIdPreferenceOrder() {
64
+ export function localIdPreferenceOrder() {
76
65
  const hasPendingUpdates = sql `EXISTS (
77
66
  SELECT 1
78
67
  FROM ${documentHistoryUpdates}
@@ -93,46 +82,4 @@ function localIdPreferenceOrder() {
93
82
  desc(documents.localId),
94
83
  ];
95
84
  }
96
- export async function findLocalIdByDocumentId(execSql, appKind, documentId) {
97
- const { db } = getClientSQLitePersistenceRuntime(execSql);
98
- const rows = await db
99
- .select({ localId: documents.localId })
100
- .from(documents)
101
- .where(and(eq(documents.appKind, appKind), eq(documents.documentId, documentId)))
102
- .orderBy(...localIdPreferenceOrder())
103
- .limit(1);
104
- return rows[0]?.localId ?? null;
105
- }
106
- /**
107
- * `findLocalIdByDocumentId` for many document ids: one read per chunk, in the
108
- * same preference order, keeping the first row per document id.
109
- */
110
- export async function findLocalIdsByDocumentIds(execSql, appKind, documentIds) {
111
- const { db } = getClientSQLitePersistenceRuntime(execSql);
112
- const localIds = new Map();
113
- const unique = Array.from(new Set(documentIds));
114
- // One bind for the app kind; the rest carry document ids.
115
- const chunkSize = sqliteRowsPerStatement(1, 1);
116
- for (let offset = 0; offset < unique.length; offset += chunkSize) {
117
- const rows = await db
118
- .select({ documentId: documents.documentId, localId: documents.localId })
119
- .from(documents)
120
- .where(and(eq(documents.appKind, appKind), inArray(documents.documentId, unique.slice(offset, offset + chunkSize))))
121
- .orderBy(...localIdPreferenceOrder());
122
- for (const row of rows) {
123
- if (row.documentId !== null && !localIds.has(row.documentId)) {
124
- localIds.set(row.documentId, row.localId);
125
- }
126
- }
127
- }
128
- return localIds;
129
- }
130
- export async function deleteDocumentRecord(execSql, scope) {
131
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
132
- await db
133
- .delete(documents)
134
- .where(and(eq(documents.appKind, scope.appKind), eq(documents.localId, scope.localId)))
135
- .run();
136
- });
137
- }
138
85
  //# sourceMappingURL=documentRecordPersistence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"documentRecordPersistence.js","sourceRoot":"","sources":["documentRecordPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,uCAAuC,EAAE,MAAM,iDAAiD,CAAC;AAC1G,OAAO,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EACL,sBAAsB,EACtB,SAAS,GACV,MAAM,6CAA6C,CAAC;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EAAE,iCAAiC,EAAE,MAAM,0CAA0C,CAAC;AAQ7F;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,EAAE,EAAE,SAAS,CAAC,OAAO;IACrB,UAAU,EAAE,SAAS,CAAC,UAAU;IAChC,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,WAAW,EAAE,SAAS,CAAC,WAAW;IAClC,eAAe,EAAE,SAAS,CAAC,eAAe;IAC1C,oBAAoB,EAAE,SAAS,CAAC,oBAAoB;IACpD,aAAa,EAAE,SAAS,CAAC,aAAa;IACtC,sBAAsB,EAAE,SAAS,CAAC,sBAAsB;IACxD,gBAAgB,EAAE,SAAS,CAA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xE,IAAI,CAAC,SAAS,CAAC;aACf,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,EAC9B,OAAO,CACL,SAAS,CAAC,UAAU,EACpB,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CACzC,CACF,CACF;aACA,OAAO,CAAC,GAAG,sBAAsB,EAAE,CAAC,CAAC;QACxC,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,IAAI,GAAG,CAAC,UAAU,KAAK,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;gBAC7D,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;YAC5C,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,OAAgB,EAChB,KAAoB;IAEpB,MAAM,iCAAiC,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QACxE,MAAM,EAAE;aACL,MAAM,CAAC,SAAS,CAAC;aACjB,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACpC,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CACrC,CACF;aACA,GAAG,EAAE,CAAC;IACX,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { and, desc, eq, inArray, sql } from \"drizzle-orm\";\nimport { deserializeDocumentSyncPullContinuation } from \"../../../documents/shared/sync/pullContinuation\";\nimport { normalizeEffectiveAccessLevel } from \"../../../keying/accessLevel\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport { sqliteRowsPerStatement } from \"../../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport type {\n DocumentRecord,\n DocumentScope,\n SelectedDocumentRecordRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * The ONE select shape for a full documents-row record: every reader that\n * feeds mapSelectedDocumentRecord spreads this, so a new record column cannot\n * silently miss one of them.\n */\nexport const documentRecordSelection = {\n id: documents.localId,\n documentId: documents.documentId,\n recoveryGeneration: documents.recoveryGeneration,\n recoveryDocumentId: documents.recoveryDocumentId,\n snapshotEndVersion: documents.snapshotEndVersion,\n accessEpoch: documents.accessEpoch,\n accessStateHash: documents.accessStateHash,\n effectiveAccessLevel: documents.effectiveAccessLevel,\n lastCommitLsn: documents.lastCommitLsn,\n documentManifestBundle: documents.documentManifestBundle,\n contentKeyBundle: documents.contentKeyBundle,\n documentKekTargets: documents.documentKekTargets,\n pendingBaseVersion: documents.pendingBaseVersion,\n pullContinuation: documents.pullContinuation,\n};\n\nexport function mapSelectedDocumentRecord(\n row: SelectedDocumentRecordRow,\n): DocumentRecord {\n const record: DocumentRecord = {\n id: row.id,\n documentId: row.documentId,\n recoveryGeneration: row.recoveryGeneration,\n snapshotEndVersion: row.snapshotEndVersion,\n accessEpoch: row.accessEpoch,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n row.effectiveAccessLevel,\n ),\n lastCommitLsn: row.lastCommitLsn,\n contentKeyBundle: row.contentKeyBundle,\n documentKekTargets: row.documentKekTargets,\n documentManifestBundle: row.documentManifestBundle,\n };\n\n if (row.accessStateHash !== null) {\n record.accessStateHash = row.accessStateHash;\n }\n\n if (row.recoveryDocumentId !== null) {\n record.recoveryDocumentId = row.recoveryDocumentId;\n }\n\n // Only surface the marker when one was persisted, so a row that never stored\n // one keeps its prior shape (and init falls through to seeding it).\n if (row.pendingBaseVersion !== null) {\n record.pendingBaseVersion = row.pendingBaseVersion;\n }\n\n const pullContinuation = deserializeDocumentSyncPullContinuation(\n row.pullContinuation,\n );\n if (pullContinuation !== null) {\n record.pullContinuation = pullContinuation;\n } else if (row.pullContinuation !== null) {\n record.pullContinuationRecoveryRequired = true;\n }\n\n return record;\n}\n\nexport async function loadDocumentRecord(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<DocumentRecord | null> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select(documentRecordSelection)\n .from(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n ),\n )\n .limit(1);\n\n return rows[0] ? mapSelectedDocumentRecord(rows[0]) : null;\n}\n\n/**\n * Duplicate remote identities can survive an interrupted local adoption, so\n * the row to use for a document id is chosen by this order: queued or\n * deferred edits first, then a stable fallback.\n */\nfunction localIdPreferenceOrder() {\n const hasPendingUpdates = sql<number>`EXISTS (\n SELECT 1\n FROM ${documentHistoryUpdates}\n WHERE ${documentHistoryUpdates.appKind} = ${documents.appKind}\n AND ${documentHistoryUpdates.localId} = ${documents.localId}\n AND ${documentHistoryUpdates.updateId} IS NOT NULL\n )`;\n const hasPendingLocalWork = sql<number>`(\n ${hasPendingUpdates}\n OR (\n ${documents.pendingBaseVersion} IS NOT NULL\n AND ${documents.pendingBaseVersion} <> ${documents.snapshotEndVersion}\n )\n )`;\n return [\n desc(hasPendingLocalWork),\n desc(documents.updatedAt),\n desc(documents.localId),\n ];\n}\n\nexport async function findLocalIdByDocumentId(\n execSql: ExecSql,\n appKind: string,\n documentId: string,\n): Promise<string | null> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(eq(documents.appKind, appKind), eq(documents.documentId, documentId)),\n )\n .orderBy(...localIdPreferenceOrder())\n .limit(1);\n\n return rows[0]?.localId ?? null;\n}\n\n/**\n * `findLocalIdByDocumentId` for many document ids: one read per chunk, in the\n * same preference order, keeping the first row per document id.\n */\nexport async function findLocalIdsByDocumentIds(\n execSql: ExecSql,\n appKind: string,\n documentIds: ReadonlyArray<string>,\n): Promise<Map<string, string>> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const localIds = new Map<string, string>();\n const unique = Array.from(new Set(documentIds));\n // One bind for the app kind; the rest carry document ids.\n const chunkSize = sqliteRowsPerStatement(1, 1);\n for (let offset = 0; offset < unique.length; offset += chunkSize) {\n const rows = await db\n .select({ documentId: documents.documentId, localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, appKind),\n inArray(\n documents.documentId,\n unique.slice(offset, offset + chunkSize),\n ),\n ),\n )\n .orderBy(...localIdPreferenceOrder());\n for (const row of rows) {\n if (row.documentId !== null && !localIds.has(row.documentId)) {\n localIds.set(row.documentId, row.localId);\n }\n }\n }\n return localIds;\n}\n\nexport async function deleteDocumentRecord(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<void> {\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n await db\n .delete(documents)\n .where(\n and(\n eq(documents.appKind, scope.appKind),\n eq(documents.localId, scope.localId),\n ),\n )\n .run();\n });\n}\n"]}
1
+ {"version":3,"file":"documentRecordPersistence.js","sourceRoot":"","sources":["documentRecordPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,uCAAuC,EAAE,MAAM,iDAAiD,CAAC;AAC1G,OAAO,EAAE,6BAA6B,EAAE,MAAM,6BAA6B,CAAC;AAC5E,OAAO,EACL,sBAAsB,EACtB,SAAS,GACV,MAAM,6CAA6C,CAAC;AAMrD;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACrC,EAAE,EAAE,SAAS,CAAC,OAAO;IACrB,UAAU,EAAE,SAAS,CAAC,UAAU;IAChC,kBAAkB,EAAE,SAAS,CAAC,kBAAkB;IAChD,kBAAkB,EAAE,SAAS,CAAC,k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EAAE,CAAC;QACpC,MAAM,CAAC,kBAAkB,GAAG,GAAG,CAAC,kBAAkB,CAAC;IACrD,CAAC;IAED,MAAM,gBAAgB,GAAG,uCAAuC,CAC9D,GAAG,CAAC,gBAAgB,CACrB,CAAC;IACF,IAAI,gBAAgB,KAAK,IAAI,EAAE,CAAC;QAC9B,MAAM,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC7C,CAAC;SAAM,IAAI,GAAG,CAAC,gBAAgB,KAAK,IAAI,EAAE,CAAC;QACzC,MAAM,CAAC,gCAAgC,GAAG,IAAI,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,sBAAsB;IACpC,MAAM,iBAAiB,GAAG,GAAG,CAAQ;;WAE5B,sBAAsB;YACrB,sBAAsB,CAAC,OAAO,MAAM,SAAS,CAAC,OAAO;YACrD,sBAAsB,CAAC,OAAO,MAAM,SAAS,CAAC,OAAO;YACrD,sBAAsB,CAAC,QAAQ;IACvC,CAAC;IACH,MAAM,mBAAmB,GAAG,GAAG,CAAQ;MACnC,iBAAiB;;QAEf,SAAS,CAAC,kBAAkB;YACxB,SAAS,CAAC,kBAAkB,OAAO,SAAS,CAAC,kBAAkB;;IAEvE,CAAC;IACH,OAAO;QACL,IAAI,CAAC,mBAAmB,CAAC;QACzB,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC;QACzB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;KACxB,CAAC;AACJ,CAAC","sourcesContent":["import { desc, sql } from \"drizzle-orm\";\nimport { deserializeDocumentSyncPullContinuation } from \"../../../documents/shared/sync/pullContinuation\";\nimport { normalizeEffectiveAccessLevel } from \"../../../keying/accessLevel\";\nimport {\n documentHistoryUpdates,\n documents,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport type {\n DocumentRecord,\n SelectedDocumentRecordRow,\n} from \"./documentPersistenceTypes\";\n\n/**\n * The ONE select shape for a full documents-row record: every reader that\n * feeds mapSelectedDocumentRecord spreads this, so a new record column cannot\n * silently miss one of them.\n */\nexport const documentRecordSelection = {\n id: documents.localId,\n documentId: documents.documentId,\n recoveryGeneration: documents.recoveryGeneration,\n recoveryDocumentId: documents.recoveryDocumentId,\n snapshotEndVersion: documents.snapshotEndVersion,\n accessEpoch: documents.accessEpoch,\n accessStateHash: documents.accessStateHash,\n effectiveAccessLevel: documents.effectiveAccessLevel,\n lastCommitLsn: documents.lastCommitLsn,\n documentManifestBundle: documents.documentManifestBundle,\n contentKeyBundle: documents.contentKeyBundle,\n documentKekTargets: documents.documentKekTargets,\n pendingBaseVersion: documents.pendingBaseVersion,\n pullContinuation: documents.pullContinuation,\n};\n\nexport function mapSelectedDocumentRecord(\n row: SelectedDocumentRecordRow,\n): DocumentRecord {\n const record: DocumentRecord = {\n id: row.id,\n documentId: row.documentId,\n recoveryGeneration: row.recoveryGeneration,\n snapshotEndVersion: row.snapshotEndVersion,\n accessEpoch: row.accessEpoch,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n row.effectiveAccessLevel,\n ),\n lastCommitLsn: row.lastCommitLsn,\n contentKeyBundle: row.contentKeyBundle,\n documentKekTargets: row.documentKekTargets,\n documentManifestBundle: row.documentManifestBundle,\n };\n\n if (row.accessStateHash !== null) {\n record.accessStateHash = row.accessStateHash;\n }\n\n if (row.recoveryDocumentId !== null) {\n record.recoveryDocumentId = row.recoveryDocumentId;\n }\n\n // Only surface the marker when one was persisted, so a row that never stored\n // one keeps its prior shape (and init falls through to seeding it).\n if (row.pendingBaseVersion !== null) {\n record.pendingBaseVersion = row.pendingBaseVersion;\n }\n\n const pullContinuation = deserializeDocumentSyncPullContinuation(\n row.pullContinuation,\n );\n if (pullContinuation !== null) {\n record.pullContinuation = pullContinuation;\n } else if (row.pullContinuation !== null) {\n record.pullContinuationRecoveryRequired = true;\n }\n\n return record;\n}\n\n/**\n * Duplicate remote identities can survive an interrupted local adoption, so\n * the row to use for a document id is chosen by this order: queued or\n * deferred edits first, then a stable fallback.\n */\nexport function localIdPreferenceOrder() {\n const hasPendingUpdates = sql<number>`EXISTS (\n SELECT 1\n FROM ${documentHistoryUpdates}\n WHERE ${documentHistoryUpdates.appKind} = ${documents.appKind}\n AND ${documentHistoryUpdates.localId} = ${documents.localId}\n AND ${documentHistoryUpdates.updateId} IS NOT NULL\n )`;\n const hasPendingLocalWork = sql<number>`(\n ${hasPendingUpdates}\n OR (\n ${documents.pendingBaseVersion} IS NOT NULL\n AND ${documents.pendingBaseVersion} <> ${documents.snapshotEndVersion}\n )\n )`;\n return [\n desc(hasPendingLocalWork),\n desc(documents.updatedAt),\n desc(documents.localId),\n ];\n}\n"]}