@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.
Files changed (107) hide show
  1. package/client/containerContents/documentDiscovery.js +2 -2
  2. package/client/containerContents/documentDiscovery.js.map +1 -1
  3. package/client/organizations/organizationReadModels.js +3 -3
  4. package/client/organizations/organizationReadModels.js.map +1 -1
  5. package/data/persistence/container-contents/containerContentsPersistence.js +2 -2
  6. package/data/persistence/container-contents/containerContentsPersistence.js.map +1 -1
  7. package/data/persistence/container-contents/containerMetadataMutationPersistence.js +2 -2
  8. package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
  9. package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js +2 -2
  10. package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js.map +1 -1
  11. package/data/persistence/container-contents/containerPendingUpdatePersistence.js +6 -6
  12. package/data/persistence/container-contents/containerPendingUpdatePersistence.js.map +1 -1
  13. package/data/persistence/documents/documentDiscoveryEvidencePersistence.d.ts +11 -2
  14. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js +89 -77
  15. package/data/persistence/documents/documentDiscoveryEvidencePersistence.js.map +1 -1
  16. package/data/persistence/documents/internal/discardDocument.js +4 -4
  17. package/data/persistence/documents/internal/discardDocument.js.map +1 -1
  18. package/data/persistence/documents/internal/discoveredDocumentUpsert.js +3 -3
  19. package/data/persistence/documents/internal/discoveredDocumentUpsert.js.map +1 -1
  20. package/data/persistence/documents/internal/documentDeletionPersistence.js +8 -8
  21. package/data/persistence/documents/internal/documentDeletionPersistence.js.map +1 -1
  22. package/data/persistence/documents/internal/documentMutationPersistence.js +4 -3
  23. package/data/persistence/documents/internal/documentMutationPersistence.js.map +1 -1
  24. package/data/persistence/documents/internal/documentPullContinuationPersistence.js +2 -2
  25. package/data/persistence/documents/internal/documentPullContinuationPersistence.js.map +1 -1
  26. package/data/persistence/documents/internal/rowQueryPersistence.js +3 -3
  27. package/data/persistence/documents/internal/rowQueryPersistence.js.map +1 -1
  28. package/data/persistence/documents/internal/syncQueuePersistence.js +12 -13
  29. package/data/persistence/documents/internal/syncQueuePersistence.js.map +1 -1
  30. package/data/persistence/documents/records/documentHistoryPersistence.d.ts +24 -20
  31. package/data/persistence/documents/records/documentHistoryPersistence.js +59 -90
  32. package/data/persistence/documents/records/documentHistoryPersistence.js.map +1 -1
  33. package/data/persistence/documents/records/documentPendingUpdatePersistence.d.ts +10 -25
  34. package/data/persistence/documents/records/documentPendingUpdatePersistence.js +19 -79
  35. package/data/persistence/documents/records/documentPendingUpdatePersistence.js.map +1 -1
  36. package/data/persistence/documents/records/documentQueuedUpdateReplacement.d.ts +4 -14
  37. package/data/persistence/documents/records/documentQueuedUpdateReplacement.js +27 -42
  38. package/data/persistence/documents/records/documentQueuedUpdateReplacement.js.map +1 -1
  39. package/data/persistence/documents/records/documentRecordPersistence.d.ts +5 -8
  40. package/data/persistence/documents/records/documentRecordPersistence.js +2 -55
  41. package/data/persistence/documents/records/documentRecordPersistence.js.map +1 -1
  42. package/data/persistence/documents/records/documentRecordRepository.d.ts +133 -0
  43. package/data/persistence/documents/records/documentRecordRepository.js +286 -0
  44. package/data/persistence/documents/records/documentRecordRepository.js.map +1 -0
  45. package/data/persistence/documents/records/documentSyncFailurePersistence.d.ts +9 -12
  46. package/data/persistence/documents/records/documentSyncFailurePersistence.js +19 -34
  47. package/data/persistence/documents/records/documentSyncFailurePersistence.js.map +1 -1
  48. package/data/persistence/documents/records/index.d.ts +4 -19
  49. package/data/persistence/documents/records/index.js +5 -29
  50. package/data/persistence/documents/records/index.js.map +1 -1
  51. package/data/persistence/organizations/organizationPresentationDenialPersistence.js +0 -43
  52. package/data/persistence/organizations/organizationPresentationDenialPersistence.js.map +1 -1
  53. package/data/persistence/organizations/organizationReadModelMemberLoad.d.ts +7 -2
  54. package/data/persistence/organizations/organizationReadModelMemberLoad.js +22 -27
  55. package/data/persistence/organizations/organizationReadModelMemberLoad.js.map +1 -1
  56. package/data/persistence/organizations/organizationReadModelPersistence.js +38 -81
  57. package/data/persistence/organizations/organizationReadModelPersistence.js.map +1 -1
  58. package/data/persistence/organizations/organizationReadModelPurge.js +0 -38
  59. package/data/persistence/organizations/organizationReadModelPurge.js.map +1 -1
  60. package/data/persistence/organizations/organizationReadModelRepository.js +264 -0
  61. package/data/persistence/organizations/organizationReadModelRepository.js.map +1 -0
  62. package/package.json +1 -1
  63. package/stores/documents/documentStore/sync/attachmentUploadFailure.js +2 -2
  64. package/stores/documents/documentStore/sync/attachmentUploadFailure.js.map +1 -1
  65. package/stores/documents/documentStore/sync/syncAttachments.js +2 -2
  66. package/stores/documents/documentStore/sync/syncAttachments.js.map +1 -1
  67. package/stores/documents/documentStore/sync/syncShared.js +2 -2
  68. package/stores/documents/documentStore/sync/syncShared.js.map +1 -1
  69. package/workflows/container-contents/documentDiscovery/documentDiscoveryEvidence.js.map +1 -1
  70. package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js +2 -2
  71. package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js.map +1 -1
  72. package/workflows/container-contents/pendingWrites/index.js +4 -3
  73. package/workflows/container-contents/pendingWrites/index.js.map +1 -1
  74. package/workflows/container-contents/recovery/accessRestoration.js +2 -2
  75. package/workflows/container-contents/recovery/accessRestoration.js.map +1 -1
  76. package/workflows/documents/index.d.ts +1 -1
  77. package/workflows/documents/index.js +1 -1
  78. package/workflows/documents/index.js.map +1 -1
  79. package/workflows/documents/syncRekeys/syncRecoveryRekey.js +3 -2
  80. package/workflows/documents/syncRekeys/syncRecoveryRekey.js.map +1 -1
  81. package/workflows/organizations/groupNames/currentOrganizationGroupNames.js +6 -2
  82. package/workflows/organizations/groupNames/currentOrganizationGroupNames.js.map +1 -1
  83. package/workflows/organizations/index.d.ts +0 -1
  84. package/workflows/organizations/index.js +0 -1
  85. package/workflows/organizations/index.js.map +1 -1
  86. package/workflows/organizations/policyHistory/policyHistoryAccessDenial.js +7 -4
  87. package/workflows/organizations/policyHistory/policyHistoryAccessDenial.js.map +1 -1
  88. package/workflows/organizations/readModel/dataUsageReadModel.js +2 -2
  89. package/workflows/organizations/readModel/dataUsageReadModel.js.map +1 -1
  90. package/workflows/organizations/readModel/listLocalOrganizations.js +6 -4
  91. package/workflows/organizations/readModel/listLocalOrganizations.js.map +1 -1
  92. package/workflows/organizations/readModel/localReadModelDetails.js +8 -9
  93. package/workflows/organizations/readModel/localReadModelDetails.js.map +1 -1
  94. package/workflows/organizations/readModel/organizationPresentationAccessState.js +2 -2
  95. package/workflows/organizations/readModel/organizationPresentationAccessState.js.map +1 -1
  96. package/workflows/organizations/readModel/readModelProjection.d.ts +1 -1
  97. package/workflows/organizations/readModel/readModelProjection.js +12 -13
  98. package/workflows/organizations/readModel/readModelProjection.js.map +1 -1
  99. package/data/persistence/organizations/organizationAccessRevocationPersistence.js +0 -49
  100. package/data/persistence/organizations/organizationAccessRevocationPersistence.js.map +0 -1
  101. package/data/persistence/organizations/organizationGroupNamePersistence.js +0 -33
  102. package/data/persistence/organizations/organizationGroupNamePersistence.js.map +0 -1
  103. package/data/persistence/organizations/organizationProfileDocumentPointers.js +0 -32
  104. package/data/persistence/organizations/organizationProfileDocumentPointers.js.map +0 -1
  105. package/data/persistence/organizations/organizationReadModelCursorPersistence.d.ts +0 -15
  106. package/data/persistence/organizations/organizationReadModelCursorPersistence.js +0 -25
  107. package/data/persistence/organizations/organizationReadModelCursorPersistence.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { type ExecSql } from "../../../sqlite/sqlSchema.js";
1
+ import type { ClientSQLiteDatabase, ClientSQLiteTransactionScope } from "../../../sqlite/sqlitePersistenceRuntime.js";
2
2
  import type { DocumentScope } from "./documentPersistenceTypes.js";
3
3
  /**
4
4
  * Compaction thresholds: once the tail carries this many rows or bytes, the
@@ -14,16 +14,22 @@ export declare const DOCUMENT_HISTORY_COMPACTION_MAX_BYTES: number;
14
14
  * decrypted pulled updates. Restores advance the outgoing-delta marker across
15
15
  * remote rows (already server-side); local rows never advance it.
16
16
  */
17
- type DocumentHistoryUpdateOrigin = "local" | "remote";
17
+ export type DocumentHistoryUpdateOrigin = "local" | "remote";
18
+ type HistoryRead = Pick<ClientSQLiteTransactionScope, "select">;
18
19
  interface DocumentHistoryTailUpdate {
19
20
  readonly origin: DocumentHistoryUpdateOrigin;
20
21
  readonly updateData: string;
21
22
  }
22
- interface DocumentHistoryRestoreState {
23
+ export interface DocumentHistoryRestoreState {
23
24
  readonly snapshot: string;
24
25
  readonly tailUpdates: readonly DocumentHistoryTailUpdate[];
25
26
  }
26
- export declare function appendDocumentHistoryUpdates(execSql: ExecSql, scope: DocumentScope, updates: readonly string[], origin: DocumentHistoryUpdateOrigin): Promise<void>;
27
+ export declare function insertDocumentHistoryUpdatesInTransaction(tx: Pick<ClientSQLiteTransactionScope, "insert">, input: {
28
+ createdAt: string;
29
+ origin: DocumentHistoryUpdateOrigin;
30
+ scope: DocumentScope;
31
+ updates: readonly string[];
32
+ }): Promise<void>;
27
33
  /**
28
34
  * The checkpoint + ordered tail for a scope, or null when NEITHER exists (a
29
35
  * discovered shell that has not hydrated content yet; the caller starts the
@@ -33,18 +39,19 @@ export declare function appendDocumentHistoryUpdates(execSql: ExecSql, scope: Do
33
39
  * rows can be the only durable copy of a local edit, so they must never be
34
40
  * silently ignored.
35
41
  */
36
- export declare function loadDocumentHistoryRestoreState(execSql: ExecSql, scope: DocumentScope): Promise<DocumentHistoryRestoreState | null>;
42
+ export declare function selectDocumentHistoryRestoreState(db: HistoryRead, scope: DocumentScope): Promise<DocumentHistoryRestoreState | null>;
37
43
  /**
38
44
  * Size of the tail compaction can shrink. Queued rows are excluded: they stay
39
45
  * until settlement, so counting them would re-export full history on every
40
46
  * persist while offline edits wait to sync.
41
47
  */
42
- export declare function readDocumentHistoryTailSize(execSql: ExecSql, scope: DocumentScope): Promise<{
48
+ export declare function selectDocumentHistoryTailSize(db: HistoryRead, scope: DocumentScope): Promise<DocumentHistoryTailSize>;
49
+ export interface DocumentHistoryTailSize {
43
50
  byteLength: number;
44
51
  hasCheckpoint: boolean;
45
52
  rowCount: number;
46
- }>;
47
- interface DocumentHistoryTailEntry {
53
+ }
54
+ export interface DocumentHistoryTailEntry {
48
55
  readonly id: string;
49
56
  readonly updateData: string;
50
57
  }
@@ -54,16 +61,8 @@ interface DocumentHistoryTailEntry {
54
61
  * (another pane's append may carry ops this pane's document has not merged
55
62
  * yet). Queued rows are never candidates.
56
63
  */
57
- export declare function listDocumentHistoryTailEntries(execSql: ExecSql, scope: DocumentScope): Promise<DocumentHistoryTailEntry[]>;
58
- /**
59
- * Replace the scope's checkpoint with a fresh full-history snapshot and
60
- * delete ONLY the tail rows the caller captured before exporting it. Every
61
- * append path writes its tail row after the ops are already in the live
62
- * document, so any row captured before the export is provably covered by
63
- * the snapshot — while a row appended concurrently (between export and this
64
- * delete) survives for the next compaction instead of being lost.
65
- */
66
- export declare function replaceDocumentHistoryCheckpoint(execSql: ExecSql, scope: DocumentScope, input: {
64
+ export declare function selectDocumentHistoryTailEntries(db: HistoryRead, scope: DocumentScope): Promise<DocumentHistoryTailEntry[]>;
65
+ export interface DocumentHistoryCheckpointReplacement {
67
66
  coveredTailIds: readonly string[];
68
67
  endVersionVector: string;
69
68
  /**
@@ -81,6 +80,11 @@ export declare function replaceDocumentHistoryCheckpoint(execSql: ExecSql, scope
81
80
  * could install the OLD document's checkpoint over the new identity's.
82
81
  */
83
82
  stillCurrent?: () => boolean;
84
- }): Promise<boolean>;
85
- export declare function deleteDocumentHistory(execSql: ExecSql, scope: DocumentScope): Promise<void>;
83
+ }
84
+ /**
85
+ * The statements of `replaceDocumentHistoryCheckpoint` on the caller's locked
86
+ * handle. They run sequentially and open no transaction of their own.
87
+ */
88
+ export declare function replaceDocumentHistoryCheckpointInTransaction(tx: ClientSQLiteDatabase, scope: DocumentScope, input: DocumentHistoryCheckpointReplacement, now: () => Date): Promise<boolean>;
89
+ export declare function deleteDocumentHistoryInTransaction(tx: Pick<ClientSQLiteTransactionScope, "delete">, scope: DocumentScope): Promise<void>;
86
90
  export {};
@@ -1,10 +1,8 @@
1
1
  import { base64ToBytes } from "../../../../internal/encoding/index.js";
2
2
  import { createDocument, exportFullHistoryIdentity, importSnapshot, satisfiesVersionVector, } from "../../../../internal/loro/index.js";
3
3
  import { and, eq, exists, inArray, isNull, ne, sql } from "drizzle-orm";
4
- import { documentHistoryCheckpoints, documentHistoryUpdates, documentTables, } from "../../../sqlite/schema/documentRecordSchema.js";
4
+ import { documentHistoryCheckpoints, documentHistoryUpdates, } from "../../../sqlite/schema/documentRecordSchema.js";
5
5
  import { sqliteInsertBatchSize } from "../../../sqlite/sqliteBindBudget.js";
6
- import { getClientSQLitePersistenceRuntime } from "../../../sqlite/sqlitePersistenceRuntime.js";
7
- import { ensureSqlTables } from "../../../sqlite/sqlSchema.js";
8
6
  const HISTORY_INSERT_BATCH_SIZE = sqliteInsertBatchSize(documentHistoryUpdates);
9
7
  /**
10
8
  * Compaction thresholds: once the tail carries this many rows or bytes, the
@@ -30,28 +28,22 @@ function tailInScope(scope) {
30
28
  function compactableTailInScope(scope) {
31
29
  return and(tailInScope(scope), isNull(documentHistoryUpdates.updateId));
32
30
  }
33
- export async function appendDocumentHistoryUpdates(execSql, scope, updates, origin) {
34
- if (updates.length === 0) {
35
- return;
31
+ export async function insertDocumentHistoryUpdatesInTransaction(tx, input) {
32
+ const { createdAt, origin, scope, updates } = input;
33
+ // A pull page carries at most 64 updates, which fits one statement; the
34
+ // chunking keeps any longer append within SQLite's bind budget.
35
+ for (let index = 0; index < updates.length; index += HISTORY_INSERT_BATCH_SIZE) {
36
+ await tx.insert(documentHistoryUpdates).values(updates
37
+ .slice(index, index + HISTORY_INSERT_BATCH_SIZE)
38
+ .map((updateData) => ({
39
+ id: crypto.randomUUID(),
40
+ appKind: scope.appKind,
41
+ localId: scope.localId,
42
+ updateData,
43
+ origin,
44
+ createdAt,
45
+ })));
36
46
  }
37
- await ensureSqlTables(execSql, documentTables);
38
- const createdAt = new Date().toISOString();
39
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {
40
- // A pull page carries at most 64 updates, which fits one statement; the
41
- // chunking keeps any longer append within SQLite's bind budget.
42
- for (let index = 0; index < updates.length; index += HISTORY_INSERT_BATCH_SIZE) {
43
- await db.insert(documentHistoryUpdates).values(updates
44
- .slice(index, index + HISTORY_INSERT_BATCH_SIZE)
45
- .map((updateData) => ({
46
- id: crypto.randomUUID(),
47
- appKind: scope.appKind,
48
- localId: scope.localId,
49
- updateData,
50
- origin,
51
- createdAt,
52
- })));
53
- }
54
- });
55
47
  }
56
48
  /**
57
49
  * The checkpoint + ordered tail for a scope, or null when NEITHER exists (a
@@ -62,9 +54,7 @@ export async function appendDocumentHistoryUpdates(execSql, scope, updates, orig
62
54
  * rows can be the only durable copy of a local edit, so they must never be
63
55
  * silently ignored.
64
56
  */
65
- export async function loadDocumentHistoryRestoreState(execSql, scope) {
66
- await ensureSqlTables(execSql, documentTables);
67
- const { db } = getClientSQLitePersistenceRuntime(execSql);
57
+ export async function selectDocumentHistoryRestoreState(db, scope) {
68
58
  // Read the TAIL before the (full) checkpoint: if another pane compacts
69
59
  // between the two reads, this order yields old-tail + new-checkpoint — a
70
60
  // safe superset (the new checkpoint subsumes the old tail, and replay is
@@ -107,9 +97,7 @@ export async function loadDocumentHistoryRestoreState(execSql, scope) {
107
97
  * until settlement, so counting them would re-export full history on every
108
98
  * persist while offline edits wait to sync.
109
99
  */
110
- export async function readDocumentHistoryTailSize(execSql, scope) {
111
- await ensureSqlTables(execSql, documentTables);
112
- const { db } = getClientSQLitePersistenceRuntime(execSql);
100
+ export async function selectDocumentHistoryTailSize(db, scope) {
113
101
  // One statement: an aggregate without GROUP BY always returns one row, and
114
102
  // the checkpoint probe rides along as an EXISTS column.
115
103
  const [totals] = await db
@@ -135,9 +123,7 @@ export async function readDocumentHistoryTailSize(execSql, scope) {
135
123
  * (another pane's append may carry ops this pane's document has not merged
136
124
  * yet). Queued rows are never candidates.
137
125
  */
138
- export async function listDocumentHistoryTailEntries(execSql, scope) {
139
- await ensureSqlTables(execSql, documentTables);
140
- const { db } = getClientSQLitePersistenceRuntime(execSql);
126
+ export async function selectDocumentHistoryTailEntries(db, scope) {
141
127
  const rows = await db
142
128
  .select({
143
129
  id: documentHistoryUpdates.id,
@@ -150,55 +136,41 @@ export async function listDocumentHistoryTailEntries(execSql, scope) {
150
136
  /** Covered-tail ids per delete; the scope adds only a few binds. */
151
137
  const HISTORY_ROW_CHUNK = 400;
152
138
  /**
153
- * Replace the scope's checkpoint with a fresh full-history snapshot and
154
- * delete ONLY the tail rows the caller captured before exporting it. Every
155
- * append path writes its tail row after the ops are already in the live
156
- * document, so any row captured before the export is provably covered by
157
- * the snapshot — while a row appended concurrently (between export and this
158
- * delete) survives for the next compaction instead of being lost.
139
+ * The statements of `replaceDocumentHistoryCheckpoint` on the caller's locked
140
+ * handle. They run sequentially and open no transaction of their own.
159
141
  */
160
- export async function replaceDocumentHistoryCheckpoint(execSql, scope, input) {
161
- await ensureSqlTables(execSql, documentTables);
162
- // Sequential (non-transactional) so callers already inside a transaction
163
- // can nest this. Crash between the statements leaves the new checkpoint
164
- // plus covered tail rows — a safe superset, since tail replay is
165
- // idempotent by op identity. The replacement itself is a compare-and-swap
166
- // on the stored revision, so the version gate cannot be bypassed by a
167
- // concurrent compactor between the read and the write: whoever loses the
168
- // CAS re-reads and re-evaluates the gate against the winner's checkpoint.
169
- return getClientSQLitePersistenceRuntime(execSql).runMutation(async (tx) => {
170
- if (input.stillCurrent?.() === false) {
142
+ export async function replaceDocumentHistoryCheckpointInTransaction(tx, scope, input, now) {
143
+ if (input.stillCurrent?.() === false) {
144
+ return false;
145
+ }
146
+ for (let attempt = 0; attempt < CHECKPOINT_CAS_ATTEMPTS; attempt += 1) {
147
+ let replaced;
148
+ try {
149
+ replaced = await tryReplaceCheckpointOnce(tx, scope, input, now);
150
+ }
151
+ catch (error) {
152
+ rethrowUnlessGateRejected(error);
153
+ // A fresher checkpoint dominates this candidate: no write, and
154
+ // the caller's covered rows must stay for the covering pane.
171
155
  return false;
172
156
  }
173
- for (let attempt = 0; attempt < CHECKPOINT_CAS_ATTEMPTS; attempt += 1) {
174
- let replaced;
175
- try {
176
- replaced = await tryReplaceCheckpointOnce(tx, scope, input);
177
- }
178
- catch (error) {
179
- rethrowUnlessGateRejected(error);
180
- // A fresher checkpoint dominates this candidate: no write, and
181
- // the caller's covered rows must stay for the covering pane.
182
- return false;
183
- }
184
- if (!replaced) {
185
- // CAS lost to a concurrent writer — re-read and re-evaluate the
186
- // gate against the winner's checkpoint.
187
- continue;
188
- }
189
- for (let index = 0; index < input.coveredTailIds.length; index += HISTORY_ROW_CHUNK) {
190
- // Never a queued row, even if a caller names one: its operations may
191
- // be in the checkpoint, but it is still the unsent update itself.
192
- await tx
193
- .delete(documentHistoryUpdates)
194
- .where(and(compactableTailInScope(scope), inArray(documentHistoryUpdates.id, input.coveredTailIds.slice(index, index + HISTORY_ROW_CHUNK))));
195
- }
196
- return true;
157
+ if (!replaced) {
158
+ // CAS lost to a concurrent writer — re-read and re-evaluate the
159
+ // gate against the winner's checkpoint.
160
+ continue;
161
+ }
162
+ for (let index = 0; index < input.coveredTailIds.length; index += HISTORY_ROW_CHUNK) {
163
+ // Never a queued row, even if a caller names one: its operations may
164
+ // be in the checkpoint, but it is still the unsent update itself.
165
+ await tx
166
+ .delete(documentHistoryUpdates)
167
+ .where(and(compactableTailInScope(scope), inArray(documentHistoryUpdates.id, input.coveredTailIds.slice(index, index + HISTORY_ROW_CHUNK))));
197
168
  }
198
- // Exhaustion must not read as durable success: creation and rebuild
199
- // callers persist record rows assuming the checkpoint landed.
200
- throw new Error("Document history checkpoint replacement lost every CAS attempt");
201
- });
169
+ return true;
170
+ }
171
+ // Exhaustion must not read as durable success: creation and rebuild
172
+ // callers persist record rows assuming the checkpoint landed.
173
+ throw new Error("Document history checkpoint replacement lost every CAS attempt");
202
174
  }
203
175
  const CHECKPOINT_CAS_ATTEMPTS = 16;
204
176
  /**
@@ -207,8 +179,8 @@ const CHECKPOINT_CAS_ATTEMPTS = 16;
207
179
  * caller re-reads and retries), and throws CheckpointGateRejected when a
208
180
  * fresher stored checkpoint dominates the candidate.
209
181
  */
210
- async function tryReplaceCheckpointOnce(tx, scope, input) {
211
- const updatedAt = new Date().toISOString();
182
+ async function tryReplaceCheckpointOnce(tx, scope, input, now) {
183
+ const updatedAt = now().toISOString();
212
184
  const revision = crypto.randomUUID();
213
185
  const [stored] = await tx
214
186
  .select({
@@ -298,15 +270,12 @@ function rethrowUnlessGateRejected(error) {
298
270
  if (!(error instanceof CheckpointGateRejected))
299
271
  throw error;
300
272
  }
301
- export async function deleteDocumentHistory(execSql, scope) {
302
- await ensureSqlTables(execSql, documentTables);
303
- await getClientSQLitePersistenceRuntime(execSql).runMutation(async (tx) => {
304
- await tx
305
- .delete(documentHistoryCheckpoints)
306
- .where(and(eq(documentHistoryCheckpoints.appKind, scope.appKind), eq(documentHistoryCheckpoints.localId, scope.localId)));
307
- await tx
308
- .delete(documentHistoryUpdates)
309
- .where(and(eq(documentHistoryUpdates.appKind, scope.appKind), eq(documentHistoryUpdates.localId, scope.localId)));
310
- });
273
+ export async function deleteDocumentHistoryInTransaction(tx, scope) {
274
+ await tx
275
+ .delete(documentHistoryCheckpoints)
276
+ .where(and(eq(documentHistoryCheckpoints.appKind, scope.appKind), eq(documentHistoryCheckpoints.localId, scope.localId)));
277
+ await tx
278
+ .delete(documentHistoryUpdates)
279
+ .where(and(eq(documentHistoryUpdates.appKind, scope.appKind), eq(documentHistoryUpdates.localId, scope.localId)));
311
280
  }
312
281
  //# sourceMappingURL=documentHistoryPersistence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"documentHistoryPersistence.js","sourceRoot":"","sources":["documentHistoryPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EACL,cAAc,EACd,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AAQ,KAAK;IACxC;QACE,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AAED,SAAS,yBAAyB,CAAC,KAAc;IAC/C,IAAI,CAAC,CAAC,KAAK,YAAY,sBAAsB,CAAC;QAAE,MAAM,KAAK,CAAC;AAC9D,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,OAAgB,EAChB,KAAoB;IAEpB,MAAM,eAAe,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;IAC/C,MAAM,iCAAiC,CAAC,OAAO,CAAC,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QACxE,MAAM,EAAE;aACL,MAAM,CAAC,0BAA0B,CAAC;aAClC,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,0BAA0B,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACrD,EAAE,CAAC,0BAA0B,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CACtD,CACF,CAAC;QACJ,MAAM,EAAE;aACL,MAAM,CAAC,sBAAsB,CAAC;aAC9B,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAClD,CACF,CAAC;IACN,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { base64ToBytes } from \"@tearleads/encoding\";\nimport {\n createDocument,\n exportFullHistoryIdentity,\n importSnapshot,\n satisfiesVersionVector,\n} from \"@tearleads/loro\";\nimport { and, eq, exists, inArray, isNull, ne, sql } from \"drizzle-orm\";\nimport {\n documentHistoryCheckpoints,\n documentHistoryUpdates,\n documentTables,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport { sqliteInsertBatchSize } from \"../../../sqlite/sqliteBindBudget\";\nimport { getClientSQLitePersistenceRuntime } from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../../sqlite/sqlSchema\";\nimport type { DocumentScope } from \"./documentPersistenceTypes\";\n\nconst HISTORY_INSERT_BATCH_SIZE = sqliteInsertBatchSize(documentHistoryUpdates);\n\n/**\n * Compaction thresholds: once the tail carries this many rows or bytes, the\n * next persist of a full-history-capable document exports a fresh checkpoint\n * and clears the tail. Small enough to keep restore replay short, large\n * enough that a full-history export (O(history)) happens once per burst of\n * edits instead of per keystroke.\n */\nexport const DOCUMENT_HISTORY_COMPACTION_MAX_ROWS = 200;\nexport const DOCUMENT_HISTORY_COMPACTION_MAX_BYTES = 512 * 1024;\n\n/**\n * Row provenance: \"local\" for updates authored on this device, \"remote\" for\n * decrypted pulled updates. Restores advance the outgoing-delta marker across\n * remote rows (already server-side); local rows never advance it.\n */\ntype DocumentHistoryUpdateOrigin = \"local\" | \"remote\";\n\n/**\n * Rows of the update log that are history tail: every row except an \"outbox\"\n * row, which only waits to be sent (see `documentHistoryUpdates`).\n */\nfunction tailInScope(scope: DocumentScope) {\n return and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n ne(documentHistoryUpdates.origin, \"outbox\"),\n );\n}\n\n/**\n * Tail rows compaction may fold into a checkpoint and delete: a queued row is\n * the only durable copy of an unsent update, so it waits for settlement even\n * when a checkpoint already covers its operations.\n */\nfunction compactableTailInScope(scope: DocumentScope) {\n return and(tailInScope(scope), isNull(documentHistoryUpdates.updateId));\n}\n\ninterface DocumentHistoryTailUpdate {\n readonly origin: DocumentHistoryUpdateOrigin;\n readonly updateData: string;\n}\n\ninterface DocumentHistoryRestoreState {\n readonly snapshot: string;\n readonly tailUpdates: readonly DocumentHistoryTailUpdate[];\n}\n\nexport async function appendDocumentHistoryUpdates(\n execSql: ExecSql,\n scope: DocumentScope,\n updates: readonly string[],\n origin: DocumentHistoryUpdateOrigin,\n): Promise<void> {\n if (updates.length === 0) {\n return;\n }\n await ensureSqlTables(execSql, documentTables);\n const createdAt = new Date().toISOString();\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (db) => {\n // A pull page carries at most 64 updates, which fits one statement; the\n // chunking keeps any longer append within SQLite's bind budget.\n for (\n let index = 0;\n index < updates.length;\n index += HISTORY_INSERT_BATCH_SIZE\n ) {\n await db.insert(documentHistoryUpdates).values(\n updates\n .slice(index, index + HISTORY_INSERT_BATCH_SIZE)\n .map((updateData) => ({\n id: crypto.randomUUID(),\n appKind: scope.appKind,\n localId: scope.localId,\n updateData,\n origin,\n createdAt,\n })),\n );\n }\n });\n}\n\n/**\n * The checkpoint + ordered tail for a scope, or null when NEITHER exists (a\n * discovered shell that has not hydrated content yet; the caller starts the\n * document empty and the remote pull supplies the content). A tail WITHOUT a\n * checkpoint — a crash between the first enqueued edit's tail append and the\n * birth-checkpoint seed — restores as tail-only (`snapshot: \"\"`): those tail\n * rows can be the only durable copy of a local edit, so they must never be\n * silently ignored.\n */\nexport async function loadDocumentHistoryRestoreState(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<DocumentHistoryRestoreState | null> {\n await ensureSqlTables(execSql, documentTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n // Read the TAIL before the (full) checkpoint: if another pane compacts\n // between the two reads, this order yields old-tail + new-checkpoint — a\n // safe superset (the new checkpoint subsumes the old tail, and replay is\n // idempotent by op identity). The reverse order could yield old-checkpoint\n // + already-emptied tail, a torn read that silently drops the compacted\n // ops for the whole session.\n const tail = await db\n .select({\n id: documentHistoryUpdates.id,\n origin: documentHistoryUpdates.origin,\n updateData: documentHistoryUpdates.updateData,\n })\n .from(documentHistoryUpdates)\n .where(tailInScope(scope))\n // Insertion order: same-millisecond appends make createdAt tie and a\n // random-uuid tiebreak would replay out of order. importUpdates tolerates\n // out-of-order batches, but replay should still be deterministic.\n .orderBy(sql`rowid`);\n\n const [checkpoint] = await db\n .select({ snapshot: documentHistoryCheckpoints.snapshot })\n .from(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n )\n .limit(1);\n if (!checkpoint && tail.length === 0) {\n return null;\n }\n\n return {\n snapshot: checkpoint?.snapshot ?? \"\",\n tailUpdates: tail.map((row) => ({\n // Fail toward re-sending: an unrecognized origin never advances the\n // marker, which is the recoverable direction (a redundant re-submit\n // is idempotent; a skipped local op would be orphaned from sync).\n origin:\n row.origin === \"remote\" ? (\"remote\" as const) : (\"local\" as const),\n updateData: row.updateData,\n })),\n };\n}\n\n/**\n * Size of the tail compaction can shrink. Queued rows are excluded: they stay\n * until settlement, so counting them would re-export full history on every\n * persist while offline edits wait to sync.\n */\nexport async function readDocumentHistoryTailSize(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<{ byteLength: number; hasCheckpoint: boolean; rowCount: number }> {\n await ensureSqlTables(execSql, documentTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n // One statement: an aggregate without GROUP BY always returns one row, and\n // the checkpoint probe rides along as an EXISTS column.\n const [totals] = await db\n .select({\n byteLength: sql<number>`coalesce(sum(length(${documentHistoryUpdates.updateData})), 0)`,\n hasCheckpoint: sql<number>`${exists(\n db\n .select({ localId: documentHistoryCheckpoints.localId })\n .from(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n ),\n )}`,\n rowCount: sql<number>`count(*)`,\n })\n .from(documentHistoryUpdates)\n .where(compactableTailInScope(scope));\n return {\n byteLength: Number(totals?.byteLength ?? 0),\n hasCheckpoint: Number(totals?.hasCheckpoint ?? 0) !== 0,\n rowCount: Number(totals?.rowCount ?? 0),\n };\n}\n\ninterface DocumentHistoryTailEntry {\n readonly id: string;\n readonly updateData: string;\n}\n\n/**\n * The current compactable tail rows WITH their payloads, so a compactor can\n * prove each row's ops are covered by the snapshot it is about to install\n * (another pane's append may carry ops this pane's document has not merged\n * yet). Queued rows are never candidates.\n */\nexport async function listDocumentHistoryTailEntries(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<DocumentHistoryTailEntry[]> {\n await ensureSqlTables(execSql, documentTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select({\n id: documentHistoryUpdates.id,\n updateData: documentHistoryUpdates.updateData,\n })\n .from(documentHistoryUpdates)\n .where(compactableTailInScope(scope));\n return rows.flatMap((row) =>\n row.id === null ? [] : [{ id: row.id, updateData: row.updateData }],\n );\n}\n\n/** Covered-tail ids per delete; the scope adds only a few binds. */\nconst HISTORY_ROW_CHUNK = 400;\n\n/**\n * Replace the scope's checkpoint with a fresh full-history snapshot and\n * delete ONLY the tail rows the caller captured before exporting it. Every\n * append path writes its tail row after the ops are already in the live\n * document, so any row captured before the export is provably covered by\n * the snapshot — while a row appended concurrently (between export and this\n * delete) survives for the next compaction instead of being lost.\n */\nexport async function replaceDocumentHistoryCheckpoint(\n execSql: ExecSql,\n scope: DocumentScope,\n input: {\n coveredTailIds: readonly string[];\n endVersionVector: string;\n /**\n * Skip the monotonic version gate. Only for document CREATION, where any\n * existing checkpoint under this localId is the orphan of a failed prior\n * attempt (checkpoint written, record row never committed) — a retry's\n * fresh document need not dominate that orphan to replace it.\n */\n force?: boolean;\n snapshot: string;\n /**\n * Re-checked after acquiring the serialized mutation slot (like the\n * record-save path): a store reset/recreation that happened while this\n * call waited in the queue must abort the write, or a stale compactor\n * could install the OLD document's checkpoint over the new identity's.\n */\n stillCurrent?: () => boolean;\n },\n): Promise<boolean> {\n await ensureSqlTables(execSql, documentTables);\n // Sequential (non-transactional) so callers already inside a transaction\n // can nest this. Crash between the statements leaves the new checkpoint\n // plus covered tail rows — a safe superset, since tail replay is\n // idempotent by op identity. The replacement itself is a compare-and-swap\n // on the stored revision, so the version gate cannot be bypassed by a\n // concurrent compactor between the read and the write: whoever loses the\n // CAS re-reads and re-evaluates the gate against the winner's checkpoint.\n return getClientSQLitePersistenceRuntime(execSql).runMutation(async (tx) => {\n if (input.stillCurrent?.() === false) {\n return false;\n }\n for (let attempt = 0; attempt < CHECKPOINT_CAS_ATTEMPTS; attempt += 1) {\n let replaced: boolean;\n try {\n replaced = await tryReplaceCheckpointOnce(tx, scope, input);\n } catch (error) {\n rethrowUnlessGateRejected(error);\n // A fresher checkpoint dominates this candidate: no write, and\n // the caller's covered rows must stay for the covering pane.\n return false;\n }\n if (!replaced) {\n // CAS lost to a concurrent writer — re-read and re-evaluate the\n // gate against the winner's checkpoint.\n continue;\n }\n for (\n let index = 0;\n index < input.coveredTailIds.length;\n index += HISTORY_ROW_CHUNK\n ) {\n // Never a queued row, even if a caller names one: its operations may\n // be in the checkpoint, but it is still the unsent update itself.\n await tx\n .delete(documentHistoryUpdates)\n .where(\n and(\n compactableTailInScope(scope),\n inArray(\n documentHistoryUpdates.id,\n input.coveredTailIds.slice(index, index + HISTORY_ROW_CHUNK),\n ),\n ),\n );\n }\n return true;\n }\n // Exhaustion must not read as durable success: creation and rebuild\n // callers persist record rows assuming the checkpoint landed.\n throw new Error(\n \"Document history checkpoint replacement lost every CAS attempt\",\n );\n });\n}\n\nconst CHECKPOINT_CAS_ATTEMPTS = 16;\n\ntype MutationDb = Parameters<\n Parameters<\n ReturnType<typeof getClientSQLitePersistenceRuntime>[\"runMutation\"]\n >[0]\n>[0];\n\n/**\n * One compare-and-swap attempt. Returns true when this caller's checkpoint\n * became the stored one, false when a concurrent writer won the CAS (the\n * caller re-reads and retries), and throws CheckpointGateRejected when a\n * fresher stored checkpoint dominates the candidate.\n */\nasync function tryReplaceCheckpointOnce(\n tx: MutationDb,\n scope: DocumentScope,\n input: {\n endVersionVector: string;\n force?: boolean;\n snapshot: string;\n },\n): Promise<boolean> {\n const updatedAt = new Date().toISOString();\n const revision = crypto.randomUUID();\n const [stored] = await tx\n .select({\n endVersionVector: documentHistoryCheckpoints.endVersionVector,\n revision: documentHistoryCheckpoints.revision,\n snapshot: documentHistoryCheckpoints.snapshot,\n })\n .from(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n )\n .limit(1);\n\n if (!stored) {\n const inserted = await tx\n .insert(documentHistoryCheckpoints)\n .values({\n appKind: scope.appKind,\n localId: scope.localId,\n snapshot: input.snapshot,\n endVersionVector: input.endVersionVector,\n revision,\n updatedAt,\n })\n .onConflictDoNothing({\n target: [\n documentHistoryCheckpoints.appKind,\n documentHistoryCheckpoints.localId,\n ],\n })\n .returning({ localId: documentHistoryCheckpoints.localId });\n // Conflict means a concurrent writer inserted first — retry the gate.\n return inserted.length > 0;\n }\n\n // A stale compactor (a pane whose document has not merged another pane's\n // ops) must never replace a checkpoint it does not cover: the covered\n // pane may already have deleted its tail rows, so regressing the\n // checkpoint would leave those ops with no durable copy at all. Vector\n // dominance proves counter coverage; the retained-prefix identity also\n // proves the candidate carries the exact stored operations at those\n // counters rather than a same-frontier fork.\n if (input.force !== true) {\n if (\n !satisfiesVersionVector(input.endVersionVector, stored.endVersionVector)\n ) {\n throw new CheckpointGateRejected();\n }\n if (\n input.snapshot !== stored.snapshot &&\n !(await candidateContainsStoredCheckpointHistory({\n candidateSnapshot: input.snapshot,\n storedEndVersion: stored.endVersionVector,\n storedSnapshot: stored.snapshot,\n }))\n ) {\n throw new CheckpointGateRejected();\n }\n }\n\n const replaced = await tx\n .update(documentHistoryCheckpoints)\n .set({\n snapshot: input.snapshot,\n endVersionVector: input.endVersionVector,\n revision,\n updatedAt,\n })\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n eq(documentHistoryCheckpoints.revision, stored.revision),\n ),\n )\n .returning({ localId: documentHistoryCheckpoints.localId });\n return replaced.length > 0;\n}\n\nasync function candidateContainsStoredCheckpointHistory(input: {\n candidateSnapshot: string;\n storedEndVersion: string;\n storedSnapshot: string;\n}): Promise<boolean> {\n const [candidateDocument, storedDocument] = await Promise.all([\n createDocument(\"checkpoint-candidate-history-gate\"),\n createDocument(\"checkpoint-stored-history-gate\"),\n ]);\n try {\n importSnapshot(candidateDocument, base64ToBytes(input.candidateSnapshot));\n importSnapshot(storedDocument, base64ToBytes(input.storedSnapshot));\n return (\n exportFullHistoryIdentity(candidateDocument, input.storedEndVersion) ===\n exportFullHistoryIdentity(storedDocument)\n );\n } finally {\n candidateDocument.free();\n storedDocument.free();\n }\n}\n\nclass CheckpointGateRejected extends Error {\n constructor() {\n super(\"checkpoint gate rejected\");\n this.name = \"CheckpointGateRejected\";\n }\n}\n\nfunction rethrowUnlessGateRejected(error: unknown): void {\n if (!(error instanceof CheckpointGateRejected)) throw error;\n}\n\nexport async function deleteDocumentHistory(\n execSql: ExecSql,\n scope: DocumentScope,\n): Promise<void> {\n await ensureSqlTables(execSql, documentTables);\n await getClientSQLitePersistenceRuntime(execSql).runMutation(async (tx) => {\n await tx\n .delete(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n );\n await tx\n .delete(documentHistoryUpdates)\n .where(\n and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n ),\n );\n });\n}\n"]}
1
+ {"version":3,"file":"documentHistoryPersistence.js","sourceRoot":"","sources":["documentHistoryPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EACL,cAAc,EACd,yBAAyB,EACzB,cAAc,EACd,sBAAsB,GACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EACL,0BAA0B,EAC1B,sBAAsB,GAC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AK;IACxC;QACE,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AAED,SAAS,yBAAyB,CAAC,KAAc;IAC/C,IAAI,CAAC,CAAC,KAAK,YAAY,sBAAsB,CAAC;QAAE,MAAM,KAAK,CAAC;AAC9D,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,kCAAkC,CACtD,EAAgD,EAChD,KAAoB;IAEpB,MAAM,EAAE;SACL,MAAM,CAAC,0BAA0B,CAAC;SAClC,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,0BAA0B,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACrD,EAAE,CAAC,0BAA0B,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CACtD,CACF,CAAC;IACJ,MAAM,EAAE;SACL,MAAM,CAAC,sBAAsB,CAAC;SAC9B,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACjD,EAAE,CAAC,sBAAsB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,CAClD,CACF,CAAC;AACN,CAAC","sourcesContent":["import { base64ToBytes } from \"@tearleads/encoding\";\nimport {\n createDocument,\n exportFullHistoryIdentity,\n importSnapshot,\n satisfiesVersionVector,\n} from \"@tearleads/loro\";\nimport { and, eq, exists, inArray, isNull, ne, sql } from \"drizzle-orm\";\nimport {\n documentHistoryCheckpoints,\n documentHistoryUpdates,\n} from \"../../../sqlite/schema/documentRecordSchema\";\nimport { sqliteInsertBatchSize } from \"../../../sqlite/sqliteBindBudget\";\nimport type {\n ClientSQLiteDatabase,\n ClientSQLiteTransactionScope,\n} from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { DocumentScope } from \"./documentPersistenceTypes\";\n\nconst HISTORY_INSERT_BATCH_SIZE = sqliteInsertBatchSize(documentHistoryUpdates);\n\n/**\n * Compaction thresholds: once the tail carries this many rows or bytes, the\n * next persist of a full-history-capable document exports a fresh checkpoint\n * and clears the tail. Small enough to keep restore replay short, large\n * enough that a full-history export (O(history)) happens once per burst of\n * edits instead of per keystroke.\n */\nexport const DOCUMENT_HISTORY_COMPACTION_MAX_ROWS = 200;\nexport const DOCUMENT_HISTORY_COMPACTION_MAX_BYTES = 512 * 1024;\n\n/**\n * Row provenance: \"local\" for updates authored on this device, \"remote\" for\n * decrypted pulled updates. Restores advance the outgoing-delta marker across\n * remote rows (already server-side); local rows never advance it.\n */\nexport type DocumentHistoryUpdateOrigin = \"local\" | \"remote\";\n\n/**\n * Rows of the update log that are history tail: every row except an \"outbox\"\n * row, which only waits to be sent (see `documentHistoryUpdates`).\n */\nfunction tailInScope(scope: DocumentScope) {\n return and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n ne(documentHistoryUpdates.origin, \"outbox\"),\n );\n}\n\n/**\n * Tail rows compaction may fold into a checkpoint and delete: a queued row is\n * the only durable copy of an unsent update, so it waits for settlement even\n * when a checkpoint already covers its operations.\n */\nfunction compactableTailInScope(scope: DocumentScope) {\n return and(tailInScope(scope), isNull(documentHistoryUpdates.updateId));\n}\n\ntype HistoryRead = Pick<ClientSQLiteTransactionScope, \"select\">;\n\ninterface DocumentHistoryTailUpdate {\n readonly origin: DocumentHistoryUpdateOrigin;\n readonly updateData: string;\n}\n\nexport interface DocumentHistoryRestoreState {\n readonly snapshot: string;\n readonly tailUpdates: readonly DocumentHistoryTailUpdate[];\n}\n\nexport async function insertDocumentHistoryUpdatesInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"insert\">,\n input: {\n createdAt: string;\n origin: DocumentHistoryUpdateOrigin;\n scope: DocumentScope;\n updates: readonly string[];\n },\n): Promise<void> {\n const { createdAt, origin, scope, updates } = input;\n // A pull page carries at most 64 updates, which fits one statement; the\n // chunking keeps any longer append within SQLite's bind budget.\n for (\n let index = 0;\n index < updates.length;\n index += HISTORY_INSERT_BATCH_SIZE\n ) {\n await tx.insert(documentHistoryUpdates).values(\n updates\n .slice(index, index + HISTORY_INSERT_BATCH_SIZE)\n .map((updateData) => ({\n id: crypto.randomUUID(),\n appKind: scope.appKind,\n localId: scope.localId,\n updateData,\n origin,\n createdAt,\n })),\n );\n }\n}\n\n/**\n * The checkpoint + ordered tail for a scope, or null when NEITHER exists (a\n * discovered shell that has not hydrated content yet; the caller starts the\n * document empty and the remote pull supplies the content). A tail WITHOUT a\n * checkpoint — a crash between the first enqueued edit's tail append and the\n * birth-checkpoint seed — restores as tail-only (`snapshot: \"\"`): those tail\n * rows can be the only durable copy of a local edit, so they must never be\n * silently ignored.\n */\nexport async function selectDocumentHistoryRestoreState(\n db: HistoryRead,\n scope: DocumentScope,\n): Promise<DocumentHistoryRestoreState | null> {\n // Read the TAIL before the (full) checkpoint: if another pane compacts\n // between the two reads, this order yields old-tail + new-checkpoint — a\n // safe superset (the new checkpoint subsumes the old tail, and replay is\n // idempotent by op identity). The reverse order could yield old-checkpoint\n // + already-emptied tail, a torn read that silently drops the compacted\n // ops for the whole session.\n const tail = await db\n .select({\n id: documentHistoryUpdates.id,\n origin: documentHistoryUpdates.origin,\n updateData: documentHistoryUpdates.updateData,\n })\n .from(documentHistoryUpdates)\n .where(tailInScope(scope))\n // Insertion order: same-millisecond appends make createdAt tie and a\n // random-uuid tiebreak would replay out of order. importUpdates tolerates\n // out-of-order batches, but replay should still be deterministic.\n .orderBy(sql`rowid`);\n\n const [checkpoint] = await db\n .select({ snapshot: documentHistoryCheckpoints.snapshot })\n .from(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n )\n .limit(1);\n if (!checkpoint && tail.length === 0) {\n return null;\n }\n\n return {\n snapshot: checkpoint?.snapshot ?? \"\",\n tailUpdates: tail.map((row) => ({\n // Fail toward re-sending: an unrecognized origin never advances the\n // marker, which is the recoverable direction (a redundant re-submit\n // is idempotent; a skipped local op would be orphaned from sync).\n origin:\n row.origin === \"remote\" ? (\"remote\" as const) : (\"local\" as const),\n updateData: row.updateData,\n })),\n };\n}\n\n/**\n * Size of the tail compaction can shrink. Queued rows are excluded: they stay\n * until settlement, so counting them would re-export full history on every\n * persist while offline edits wait to sync.\n */\nexport async function selectDocumentHistoryTailSize(\n db: HistoryRead,\n scope: DocumentScope,\n): Promise<DocumentHistoryTailSize> {\n // One statement: an aggregate without GROUP BY always returns one row, and\n // the checkpoint probe rides along as an EXISTS column.\n const [totals] = await db\n .select({\n byteLength: sql<number>`coalesce(sum(length(${documentHistoryUpdates.updateData})), 0)`,\n hasCheckpoint: sql<number>`${exists(\n db\n .select({ localId: documentHistoryCheckpoints.localId })\n .from(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n ),\n )}`,\n rowCount: sql<number>`count(*)`,\n })\n .from(documentHistoryUpdates)\n .where(compactableTailInScope(scope));\n return {\n byteLength: Number(totals?.byteLength ?? 0),\n hasCheckpoint: Number(totals?.hasCheckpoint ?? 0) !== 0,\n rowCount: Number(totals?.rowCount ?? 0),\n };\n}\n\nexport interface DocumentHistoryTailSize {\n byteLength: number;\n hasCheckpoint: boolean;\n rowCount: number;\n}\n\nexport interface DocumentHistoryTailEntry {\n readonly id: string;\n readonly updateData: string;\n}\n\n/**\n * The current compactable tail rows WITH their payloads, so a compactor can\n * prove each row's ops are covered by the snapshot it is about to install\n * (another pane's append may carry ops this pane's document has not merged\n * yet). Queued rows are never candidates.\n */\nexport async function selectDocumentHistoryTailEntries(\n db: HistoryRead,\n scope: DocumentScope,\n): Promise<DocumentHistoryTailEntry[]> {\n const rows = await db\n .select({\n id: documentHistoryUpdates.id,\n updateData: documentHistoryUpdates.updateData,\n })\n .from(documentHistoryUpdates)\n .where(compactableTailInScope(scope));\n return rows.flatMap((row) =>\n row.id === null ? [] : [{ id: row.id, updateData: row.updateData }],\n );\n}\n\n/** Covered-tail ids per delete; the scope adds only a few binds. */\nconst HISTORY_ROW_CHUNK = 400;\n\nexport interface DocumentHistoryCheckpointReplacement {\n coveredTailIds: readonly string[];\n endVersionVector: string;\n /**\n * Skip the monotonic version gate. Only for document CREATION, where any\n * existing checkpoint under this localId is the orphan of a failed prior\n * attempt (checkpoint written, record row never committed) — a retry's\n * fresh document need not dominate that orphan to replace it.\n */\n force?: boolean;\n snapshot: string;\n /**\n * Re-checked after acquiring the serialized mutation slot (like the\n * record-save path): a store reset/recreation that happened while this\n * call waited in the queue must abort the write, or a stale compactor\n * could install the OLD document's checkpoint over the new identity's.\n */\n stillCurrent?: () => boolean;\n}\n\n/**\n * The statements of `replaceDocumentHistoryCheckpoint` on the caller's locked\n * handle. They run sequentially and open no transaction of their own.\n */\nexport async function replaceDocumentHistoryCheckpointInTransaction(\n tx: ClientSQLiteDatabase,\n scope: DocumentScope,\n input: DocumentHistoryCheckpointReplacement,\n now: () => Date,\n): Promise<boolean> {\n if (input.stillCurrent?.() === false) {\n return false;\n }\n for (let attempt = 0; attempt < CHECKPOINT_CAS_ATTEMPTS; attempt += 1) {\n let replaced: boolean;\n try {\n replaced = await tryReplaceCheckpointOnce(tx, scope, input, now);\n } catch (error) {\n rethrowUnlessGateRejected(error);\n // A fresher checkpoint dominates this candidate: no write, and\n // the caller's covered rows must stay for the covering pane.\n return false;\n }\n if (!replaced) {\n // CAS lost to a concurrent writer — re-read and re-evaluate the\n // gate against the winner's checkpoint.\n continue;\n }\n for (\n let index = 0;\n index < input.coveredTailIds.length;\n index += HISTORY_ROW_CHUNK\n ) {\n // Never a queued row, even if a caller names one: its operations may\n // be in the checkpoint, but it is still the unsent update itself.\n await tx\n .delete(documentHistoryUpdates)\n .where(\n and(\n compactableTailInScope(scope),\n inArray(\n documentHistoryUpdates.id,\n input.coveredTailIds.slice(index, index + HISTORY_ROW_CHUNK),\n ),\n ),\n );\n }\n return true;\n }\n // Exhaustion must not read as durable success: creation and rebuild\n // callers persist record rows assuming the checkpoint landed.\n throw new Error(\n \"Document history checkpoint replacement lost every CAS attempt\",\n );\n}\n\nconst CHECKPOINT_CAS_ATTEMPTS = 16;\n\n/**\n * One compare-and-swap attempt. Returns true when this caller's checkpoint\n * became the stored one, false when a concurrent writer won the CAS (the\n * caller re-reads and retries), and throws CheckpointGateRejected when a\n * fresher stored checkpoint dominates the candidate.\n */\nasync function tryReplaceCheckpointOnce(\n tx: ClientSQLiteDatabase,\n scope: DocumentScope,\n input: {\n endVersionVector: string;\n force?: boolean;\n snapshot: string;\n },\n now: () => Date,\n): Promise<boolean> {\n const updatedAt = now().toISOString();\n const revision = crypto.randomUUID();\n const [stored] = await tx\n .select({\n endVersionVector: documentHistoryCheckpoints.endVersionVector,\n revision: documentHistoryCheckpoints.revision,\n snapshot: documentHistoryCheckpoints.snapshot,\n })\n .from(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n )\n .limit(1);\n\n if (!stored) {\n const inserted = await tx\n .insert(documentHistoryCheckpoints)\n .values({\n appKind: scope.appKind,\n localId: scope.localId,\n snapshot: input.snapshot,\n endVersionVector: input.endVersionVector,\n revision,\n updatedAt,\n })\n .onConflictDoNothing({\n target: [\n documentHistoryCheckpoints.appKind,\n documentHistoryCheckpoints.localId,\n ],\n })\n .returning({ localId: documentHistoryCheckpoints.localId });\n // Conflict means a concurrent writer inserted first — retry the gate.\n return inserted.length > 0;\n }\n\n // A stale compactor (a pane whose document has not merged another pane's\n // ops) must never replace a checkpoint it does not cover: the covered\n // pane may already have deleted its tail rows, so regressing the\n // checkpoint would leave those ops with no durable copy at all. Vector\n // dominance proves counter coverage; the retained-prefix identity also\n // proves the candidate carries the exact stored operations at those\n // counters rather than a same-frontier fork.\n if (input.force !== true) {\n if (\n !satisfiesVersionVector(input.endVersionVector, stored.endVersionVector)\n ) {\n throw new CheckpointGateRejected();\n }\n if (\n input.snapshot !== stored.snapshot &&\n !(await candidateContainsStoredCheckpointHistory({\n candidateSnapshot: input.snapshot,\n storedEndVersion: stored.endVersionVector,\n storedSnapshot: stored.snapshot,\n }))\n ) {\n throw new CheckpointGateRejected();\n }\n }\n\n const replaced = await tx\n .update(documentHistoryCheckpoints)\n .set({\n snapshot: input.snapshot,\n endVersionVector: input.endVersionVector,\n revision,\n updatedAt,\n })\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n eq(documentHistoryCheckpoints.revision, stored.revision),\n ),\n )\n .returning({ localId: documentHistoryCheckpoints.localId });\n return replaced.length > 0;\n}\n\nasync function candidateContainsStoredCheckpointHistory(input: {\n candidateSnapshot: string;\n storedEndVersion: string;\n storedSnapshot: string;\n}): Promise<boolean> {\n const [candidateDocument, storedDocument] = await Promise.all([\n createDocument(\"checkpoint-candidate-history-gate\"),\n createDocument(\"checkpoint-stored-history-gate\"),\n ]);\n try {\n importSnapshot(candidateDocument, base64ToBytes(input.candidateSnapshot));\n importSnapshot(storedDocument, base64ToBytes(input.storedSnapshot));\n return (\n exportFullHistoryIdentity(candidateDocument, input.storedEndVersion) ===\n exportFullHistoryIdentity(storedDocument)\n );\n } finally {\n candidateDocument.free();\n storedDocument.free();\n }\n}\n\nclass CheckpointGateRejected extends Error {\n constructor() {\n super(\"checkpoint gate rejected\");\n this.name = \"CheckpointGateRejected\";\n }\n}\n\nfunction rethrowUnlessGateRejected(error: unknown): void {\n if (!(error instanceof CheckpointGateRejected)) throw error;\n}\n\nexport async function deleteDocumentHistoryInTransaction(\n tx: Pick<ClientSQLiteTransactionScope, \"delete\">,\n scope: DocumentScope,\n): Promise<void> {\n await tx\n .delete(documentHistoryCheckpoints)\n .where(\n and(\n eq(documentHistoryCheckpoints.appKind, scope.appKind),\n eq(documentHistoryCheckpoints.localId, scope.localId),\n ),\n );\n await tx\n .delete(documentHistoryUpdates)\n .where(\n and(\n eq(documentHistoryUpdates.appKind, scope.appKind),\n eq(documentHistoryUpdates.localId, scope.localId),\n ),\n );\n}\n"]}
@@ -1,6 +1,5 @@
1
1
  import { type SQL } from "drizzle-orm";
2
2
  import type { ClientSQLiteTransactionScope } from "../../../sqlite/sqlitePersistenceRuntime.js";
3
- import type { ExecSql } from "../../../sqlite/sqlSchema.js";
4
3
  import type { DocumentScope, PendingUpdateFields, PendingUpdateOrigin, QueuedPendingUpdateRecord } from "./documentPersistenceTypes.js";
5
4
  type QueueStatement = Pick<ClientSQLiteTransactionScope, "delete" | "update">;
6
5
  type QueueRead = Pick<ClientSQLiteTransactionScope, "selectDistinct">;
@@ -8,7 +7,7 @@ export declare function queuedInScope(scope: DocumentScope): SQL<unknown> | unde
8
7
  /** The ids among `localIds` (one app kind) with at least one queued update. */
9
8
  export declare function listLocalIdsWithQueuedUpdates(db: QueueRead, appKind: string, localIds: readonly string[]): Promise<string[]>;
10
9
  export declare function hasQueuedDocumentUpdates(db: QueueRead, scope: DocumentScope): Promise<boolean>;
11
- export declare function listDocumentPendingUpdates(execSql: ExecSql, scope: DocumentScope): Promise<QueuedPendingUpdateRecord[]>;
10
+ export declare function selectDocumentPendingUpdates(db: Pick<ClientSQLiteTransactionScope, "select">, scope: DocumentScope): Promise<QueuedPendingUpdateRecord[]>;
12
11
  /** Queue order: insertion time, then insertion order within one instant. */
13
12
  export declare function queueOrder(): readonly [SQL<unknown>, SQL<unknown>];
14
13
  /**
@@ -42,17 +41,12 @@ export declare function queuedUpdateRow(input: {
42
41
  * Queue an update that has no history-tail role: container metadata (its
43
42
  * content is its checkpoint) and full-state re-uploads after a remote reset.
44
43
  */
45
- export declare function enqueueDocumentPendingUpdate(execSql: ExecSql, scope: DocumentScope, pendingUpdate: PendingUpdateFields): Promise<string>;
46
- /**
47
- * Append a locally authored update to the history tail AND queue it, as one
48
- * row in one transaction. A crash can therefore never leave the op restorable
49
- * from history while missing from the outgoing queue, where a null-marker
50
- * restore would classify it as covered and never send it.
51
- */
52
- export declare function enqueueDocumentPendingUpdateWithHistory(execSql: ExecSql, scope: DocumentScope, pendingUpdate: PendingUpdateFields, options?: {
53
- expectedDocumentId: string | null;
54
- expectedRecoveryGeneration: number;
55
- }): Promise<string | null>;
44
+ export declare function insertDocumentPendingUpdateInTransaction(tx: Pick<ClientSQLiteTransactionScope, "insert">, input: {
45
+ createdAt: string;
46
+ pendingUpdate: PendingUpdateFields;
47
+ scope: DocumentScope;
48
+ updateId: string;
49
+ }): Promise<void>;
56
50
  export declare function insertDocumentPendingUpdateWithHistoryInTransaction(input: {
57
51
  createdAt: string;
58
52
  pendingUpdate: PendingUpdateFields;
@@ -81,28 +75,19 @@ export declare function insertDocumentPendingUpdateWithHistoryInTransaction(inpu
81
75
  * passes per launch.
82
76
  */
83
77
  export declare const MAX_PENDING_UPDATE_REKEYS = 5;
84
- /**
85
- * Reset the durable re-key budget for every queued update in a scope. The
86
- * cap exists to stop a server that keeps conflicting fresh ids from driving
87
- * a network-speed loop — but a budget burned during an unrelated outage
88
- * (e.g. yesterday's stale-bundle deadlock 409ing every submit) should not be
89
- * terminal forever. An explicit manual retry is the deliberate, rate-limited
90
- * signal that conditions changed, so it gets a fresh budget.
91
- */
92
- export declare function resetDocumentPendingUpdateRekeyBudget(execSql: ExecSql, scope: DocumentScope): Promise<void>;
93
- export declare function rekeyDocumentPendingUpdate(execSql: ExecSql, id: string): Promise<string | null>;
78
+ /** Re-key one queued row below the bound; true when the UPDATE matched it. */
79
+ export declare function rekeyDocumentPendingUpdateInTransaction(tx: Pick<ClientSQLiteTransactionScope, "update">, id: string, nextId: string): Promise<boolean>;
94
80
  /**
95
81
  * Settle queued updates: an outbox row is deleted, and a tail row stops being
96
82
  * queued but stays as history until compaction folds it into a checkpoint.
97
83
  */
98
84
  export declare function settleQueuedDocumentUpdatesInTransaction(tx: QueueStatement, scope: DocumentScope, updateIds: readonly string[]): Promise<void>;
99
85
  /** Settle one queued update by id, whatever its scope. */
100
- export declare function settleDocumentPendingUpdate(execSql: ExecSql, id: string): Promise<void>;
86
+ export declare function settleDocumentPendingUpdateInTransaction(tx: QueueStatement, id: string): Promise<void>;
101
87
  /**
102
88
  * Stop queueing every update in the given scopes: outbox rows are dropped,
103
89
  * and local tail rows keep their content as history (it is already in the
104
90
  * document).
105
91
  */
106
92
  export declare function unqueueDocumentScopesInTransaction(tx: QueueStatement, scopes: readonly DocumentScope[]): Promise<void>;
107
- export declare function unqueueDocumentPendingUpdates(execSql: ExecSql, scope: DocumentScope): Promise<void>;
108
93
  export {};
@@ -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