@terreno/api 0.30.0 → 56.0.0-beta.0

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 (226) hide show
  1. package/dist/api.d.ts +10 -0
  2. package/dist/api.js +110 -361
  3. package/dist/auth.d.ts +26 -0
  4. package/dist/auth.js +100 -1
  5. package/dist/betterAuth.d.ts +5 -0
  6. package/dist/betterAuthSetup.js +23 -4
  7. package/dist/docLoader.js +12 -9
  8. package/dist/errors.d.ts +13 -0
  9. package/dist/errors.js +29 -1
  10. package/dist/index.d.ts +17 -1
  11. package/dist/index.js +29 -1
  12. package/dist/middleware.d.ts +7 -0
  13. package/dist/middleware.js +16 -1
  14. package/dist/openApiCompat.d.ts +10 -1
  15. package/dist/openApiCompat.js +115 -47
  16. package/dist/permissions.d.ts +27 -0
  17. package/dist/permissions.js +42 -1
  18. package/dist/plugins.d.ts +13 -0
  19. package/dist/plugins.js +35 -1
  20. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  21. package/dist/realtime/changeStreamWatcher.js +542 -137
  22. package/dist/realtime/realtimeApp.d.ts +2 -0
  23. package/dist/realtime/realtimeApp.js +73 -21
  24. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  25. package/dist/realtime/sessionRevalidation.js +438 -0
  26. package/dist/realtime/socketAuth.d.ts +97 -0
  27. package/dist/realtime/socketAuth.js +272 -0
  28. package/dist/realtime/socketUser.d.ts +54 -0
  29. package/dist/realtime/socketUser.js +75 -5
  30. package/dist/realtime/types.d.ts +40 -0
  31. package/dist/secretProviders.d.ts +23 -0
  32. package/dist/secretProviders.js +9 -3
  33. package/dist/sync/executors.d.ts +198 -0
  34. package/dist/sync/executors.js +670 -0
  35. package/dist/sync/loadHarness.js +807 -0
  36. package/dist/sync/models.d.ts +216 -0
  37. package/dist/sync/models.js +644 -0
  38. package/dist/sync/mutationHandler.d.ts +97 -0
  39. package/dist/sync/mutationHandler.js +886 -0
  40. package/dist/sync/registry.d.ts +73 -0
  41. package/dist/sync/registry.js +223 -0
  42. package/dist/sync/routes.d.ts +62 -0
  43. package/dist/sync/routes.js +936 -0
  44. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  45. package/dist/sync/scripts/compactTombstones.js +270 -0
  46. package/dist/sync/serialize.d.ts +20 -0
  47. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  48. package/dist/sync/socketHandlers.d.ts +54 -0
  49. package/dist/sync/socketHandlers.js +470 -0
  50. package/dist/sync/streams.d.ts +85 -0
  51. package/dist/sync/streams.js +221 -0
  52. package/dist/sync/syncApp.d.ts +29 -0
  53. package/dist/sync/syncApp.js +44 -0
  54. package/dist/sync/syncBatch.d.ts +71 -0
  55. package/dist/sync/syncBatch.js +252 -0
  56. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  57. package/dist/sync/syncSeqPlugin.js +903 -0
  58. package/dist/sync/types.d.ts +254 -0
  59. package/dist/sync/types.js +9 -0
  60. package/dist/terrenoApp.js +90 -27
  61. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  62. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  63. package/package.json +7 -6
  64. package/src/__tests__/openApiCompat.test.ts +20 -0
  65. package/src/actions.openApi.test.ts +7 -10
  66. package/src/api.test.ts +12 -0
  67. package/src/api.ts +87 -295
  68. package/src/auth.test.ts +128 -176
  69. package/src/auth.ts +109 -0
  70. package/src/betterAuth.ts +6 -0
  71. package/src/betterAuthSetup.test.ts +34 -0
  72. package/src/betterAuthSetup.ts +20 -1
  73. package/src/docLoader.ts +3 -1
  74. package/src/errors.ts +33 -0
  75. package/src/expressServer.test.ts +7 -1
  76. package/src/index.ts +42 -0
  77. package/src/middleware.test.ts +40 -1
  78. package/src/middleware.ts +15 -0
  79. package/src/openApiCompat.ts +99 -45
  80. package/src/permissions.test.ts +75 -1
  81. package/src/permissions.ts +49 -0
  82. package/src/plugins.test.ts +54 -0
  83. package/src/plugins.ts +43 -0
  84. package/src/realtime/changeStreamWatcher.ts +552 -114
  85. package/src/realtime/realtime.test.ts +157 -0
  86. package/src/realtime/realtimeApp.ts +75 -11
  87. package/src/realtime/sessionRevalidation.test.ts +423 -0
  88. package/src/realtime/sessionRevalidation.ts +273 -0
  89. package/src/realtime/socketAuth.test.ts +355 -0
  90. package/src/realtime/socketAuth.ts +228 -0
  91. package/src/realtime/socketUser.ts +69 -0
  92. package/src/realtime/types.ts +40 -0
  93. package/src/secretProviders.test.ts +7 -1
  94. package/src/secretProviders.ts +15 -3
  95. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  96. package/src/sync/executors.test.ts +882 -0
  97. package/src/sync/executors.ts +703 -0
  98. package/src/sync/integration.test.ts +1160 -0
  99. package/src/sync/loadHarness.ts +618 -0
  100. package/src/sync/models.ts +640 -0
  101. package/src/sync/mutationHandler.test.ts +1252 -0
  102. package/src/sync/mutationHandler.ts +850 -0
  103. package/src/sync/registry.ts +232 -0
  104. package/src/sync/routes.ts +812 -0
  105. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  106. package/src/sync/scripts/compactTombstones.ts +145 -0
  107. package/src/sync/serialize.ts +48 -0
  108. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  109. package/src/sync/socketHandlers.ts +320 -0
  110. package/src/sync/streams.ts +188 -0
  111. package/src/sync/sync.test.ts +582 -0
  112. package/src/sync/syncApp.ts +44 -0
  113. package/src/sync/syncBatch.ts +219 -0
  114. package/src/sync/syncFrontier.test.ts +494 -0
  115. package/src/sync/syncPhaseC.test.ts +718 -0
  116. package/src/sync/syncRoutes.test.ts +865 -0
  117. package/src/sync/syncSeqPlugin.ts +738 -0
  118. package/src/sync/syncSocket.test.ts +1440 -0
  119. package/src/sync/types.ts +270 -0
  120. package/src/terrenoApp.ts +37 -17
  121. package/src/tests/bunSetup.ts +8 -0
  122. package/tsconfig.json +4 -4
  123. package/tsconfig.typedoc.json +7 -1
  124. package/dist/__tests__/openApiCompat.test.js +0 -317
  125. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  126. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  127. package/dist/actions.openApi.test.d.ts +0 -1
  128. package/dist/actions.openApi.test.js +0 -255
  129. package/dist/actions.test.d.ts +0 -1
  130. package/dist/actions.test.js +0 -1089
  131. package/dist/api.arrayOperations.test.d.ts +0 -1
  132. package/dist/api.arrayOperations.test.js +0 -878
  133. package/dist/api.asyncHandler.test.d.ts +0 -1
  134. package/dist/api.asyncHandler.test.js +0 -236
  135. package/dist/api.coverage.test.d.ts +0 -1
  136. package/dist/api.coverage.test.js +0 -194
  137. package/dist/api.errorPaths.test.d.ts +0 -1
  138. package/dist/api.errorPaths.test.js +0 -801
  139. package/dist/api.errors.test.d.ts +0 -1
  140. package/dist/api.errors.test.js +0 -185
  141. package/dist/api.hooks.test.d.ts +0 -1
  142. package/dist/api.hooks.test.js +0 -950
  143. package/dist/api.query.test.d.ts +0 -1
  144. package/dist/api.query.test.js +0 -948
  145. package/dist/api.test.d.ts +0 -1
  146. package/dist/api.test.js +0 -2820
  147. package/dist/auth.compile.test.d.ts +0 -1
  148. package/dist/auth.compile.test.js +0 -201
  149. package/dist/auth.coverage.test.d.ts +0 -1
  150. package/dist/auth.coverage.test.js +0 -202
  151. package/dist/auth.test.d.ts +0 -1
  152. package/dist/auth.test.js +0 -2286
  153. package/dist/betterAuth.test.d.ts +0 -1
  154. package/dist/betterAuth.test.js +0 -181
  155. package/dist/betterAuthApp.test.d.ts +0 -1
  156. package/dist/betterAuthApp.test.js +0 -242
  157. package/dist/betterAuthSetup.test.d.ts +0 -1
  158. package/dist/betterAuthSetup.test.js +0 -762
  159. package/dist/config.test.d.ts +0 -1
  160. package/dist/config.test.js +0 -353
  161. package/dist/configuration.test.d.ts +0 -1
  162. package/dist/configuration.test.js +0 -980
  163. package/dist/configurationPlugin.test.d.ts +0 -1
  164. package/dist/configurationPlugin.test.js +0 -827
  165. package/dist/consentApp.test.d.ts +0 -1
  166. package/dist/consentApp.test.js +0 -1415
  167. package/dist/docLoader.test.d.ts +0 -1
  168. package/dist/docLoader.test.js +0 -137
  169. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  170. package/dist/envConfigurationPlugin.test.js +0 -369
  171. package/dist/errors.test.d.ts +0 -1
  172. package/dist/errors.test.js +0 -607
  173. package/dist/expressServer.test.d.ts +0 -1
  174. package/dist/expressServer.test.js +0 -1249
  175. package/dist/githubAuth.test.d.ts +0 -1
  176. package/dist/githubAuth.test.js +0 -1142
  177. package/dist/httpClient.test.d.ts +0 -1
  178. package/dist/httpClient.test.js +0 -1471
  179. package/dist/logger.test.d.ts +0 -1
  180. package/dist/logger.test.js +0 -685
  181. package/dist/middleware.test.d.ts +0 -1
  182. package/dist/middleware.test.js +0 -241
  183. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  184. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  185. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  186. package/dist/notifiers/slackNotifier.test.js +0 -297
  187. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  188. package/dist/notifiers/zoomNotifier.test.js +0 -373
  189. package/dist/openApi.test.d.ts +0 -1
  190. package/dist/openApi.test.js +0 -620
  191. package/dist/openApiBuilder.test.d.ts +0 -1
  192. package/dist/openApiBuilder.test.js +0 -601
  193. package/dist/openApiEtag.test.d.ts +0 -1
  194. package/dist/openApiEtag.test.js +0 -86
  195. package/dist/openApiValidator.test.d.ts +0 -1
  196. package/dist/openApiValidator.test.js +0 -761
  197. package/dist/permissions.middleware.test.d.ts +0 -1
  198. package/dist/permissions.middleware.test.js +0 -308
  199. package/dist/permissions.test.d.ts +0 -1
  200. package/dist/permissions.test.js +0 -439
  201. package/dist/plugins.test.d.ts +0 -1
  202. package/dist/plugins.test.js +0 -890
  203. package/dist/populate.test.d.ts +0 -1
  204. package/dist/populate.test.js +0 -373
  205. package/dist/realtime/realtime.test.d.ts +0 -10
  206. package/dist/realtime/realtime.test.js +0 -3982
  207. package/dist/requestContext.test.d.ts +0 -1
  208. package/dist/requestContext.test.js +0 -412
  209. package/dist/scriptRunner.test.d.ts +0 -1
  210. package/dist/scriptRunner.test.js +0 -386
  211. package/dist/secretProviders.test.d.ts +0 -1
  212. package/dist/secretProviders.test.js +0 -726
  213. package/dist/syncConsents.test.d.ts +0 -1
  214. package/dist/syncConsents.test.js +0 -522
  215. package/dist/terrenoApp.test.d.ts +0 -1
  216. package/dist/terrenoApp.test.js +0 -489
  217. package/dist/tests/bunSetup.d.ts +0 -1
  218. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  219. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  220. package/dist/tests/testEnv.d.ts +0 -0
  221. package/dist/tests/testEnv.js +0 -6
  222. package/dist/transformers.test.d.ts +0 -1
  223. package/dist/transformers.test.js +0 -423
  224. package/dist/utils.test.d.ts +0 -1
  225. package/dist/utils.test.js +0 -242
  226. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -0,0 +1,738 @@
1
+ // noExplicitAny: Schema/Query generics must be loose to accept arbitrary consumer schemas
2
+ // biome-ignore-all lint/suspicious/noExplicitAny: Schema/Query generics must be loose to accept arbitrary consumer schemas
3
+ import type {
4
+ CallbackWithoutResultAndOptionalError,
5
+ ClientSession,
6
+ Model,
7
+ Query,
8
+ Schema,
9
+ } from "mongoose";
10
+ import {logger} from "../logger";
11
+ import {isVersionError} from "./executors";
12
+ import {claimSyncSeqs, confirmSyncSeqs, releaseSyncSeqs, SyncScopeMove} from "./models";
13
+ import {findSyncEntryByModelName, type SyncRegistryEntry} from "./registry";
14
+ import {
15
+ assertWritableStream,
16
+ getScopeField,
17
+ resolveStreamForDoc,
18
+ streamForScopeValue,
19
+ } from "./streams";
20
+
21
+ /**
22
+ * Schema plugin for sync-enabled models. Stamps a monotonic per-stream `_syncSeq` on
23
+ * every single-document write and records `_syncPrevStream` when a write moves the
24
+ * document between scopes (owner/tenant change).
25
+ *
26
+ * Apply to the schema alongside `isDeletedPlugin`; activation is keyed off the sync
27
+ * registry, so models that are not registered via modelRouter's `sync` option no-op.
28
+ *
29
+ * Sequencing guarantees (C1 — stable frontier):
30
+ * - Validation failures never consume a seq: Mongoose runs validation before user
31
+ * pre('save') hooks, so the claim happens post-validation.
32
+ * - Each claim registers the claimed seq on the counter's in-flight `pending` registry;
33
+ * the matching post-write hook (`confirmSyncSeqs`) clears it. Until confirmed, the
34
+ * stream's stable frontier holds below the claimed seq, so a snapshot/delta cursor
35
+ * never advances past a seq whose owning write has not yet committed. A crash between
36
+ * claim and confirm leaves a stale pending entry that ages out via the lease.
37
+ * - The claim joins the caller's session when one is present; that path gets true
38
+ * counter+write atomicity and skips the pending registry entirely (no confirm due).
39
+ * - Every write path claims in its pre hook and confirms in its POST hook — single saves,
40
+ * `insertMany` batches, and query-writes alike. Confirming before the documents commit
41
+ * would let a cursor advance past seqs that have not landed, permanently stranding those
42
+ * documents below every catch-up cursor.
43
+ *
44
+ * Scope moves (C4): when a write moves the document between streams, a durable
45
+ * `SyncScopeMove` marker is written AFTER the move commits, carrying a seq claimed from
46
+ * the OLD stream's counter. The change-stream watcher and old-stream snapshot catch-up
47
+ * tombstone the document from the marker (not the racy `_syncPrevStream` post-image),
48
+ * so a racing second write can no longer erase the tombstone. `_syncPrevStream` is still
49
+ * stamped (harmless), but the marker is the source of truth. Writing the marker
50
+ * post-commit keeps a failed write from leaving a phantom tombstone; the insert is
51
+ * retried ({@link SCOPE_MOVE_MARKER_ATTEMPTS}) because a committed move whose marker is
52
+ * lost can never be tombstoned on the old stream.
53
+ *
54
+ * Lost-update protection (Task 9.13): the plugin turns on Mongoose
55
+ * `optimisticConcurrency`, so `doc.save()` on an existing document filters by the loaded
56
+ * `__v` and throws a `VersionError` when another write landed in between. This closes the
57
+ * read-compare-save TOCTOU window in `executeUpdate`'s `baseVersion` check — two clients
58
+ * sending the same `baseVersion` concurrently now produce exactly one ack and one conflict
59
+ * instead of two acks with one edit silently lost. The loser's already-claimed seq is
60
+ * released (never stamped on anything), so the stream's frontier does not stall for the
61
+ * pending-claim lease.
62
+ *
63
+ * Write restrictions:
64
+ * - `updateMany`, `deleteMany`, hard deletes, and `bulkWrite` are unsupported on synced
65
+ * models and all throw. The first three are guarded here; `bulkWrite` bypasses
66
+ * middleware entirely, so `registerSync` replaces that static on the model instead.
67
+ * - Query-writes MUST target a single document by `_id` (m9): a non-`_id` filter could
68
+ * match a different document than intended and stamp the wrong stream's seq.
69
+ * - `upsert: true` is rejected on query-writes (m8/C6): an upsert can create a document
70
+ * the pre-write lookup never saw, escaping seq stamping.
71
+ */
72
+
73
+ const INITIAL_STREAM_KEY = "_syncInitialStream";
74
+
75
+ /**
76
+ * Resolve the stream for a plain object under an entry's scope. Total by design: this also
77
+ * runs on documents already in the database (hydration, post-commit seq confirmation, and
78
+ * the previous-stream lookup for a scope move), where a legacy row with no scope value
79
+ * must still be nameable rather than unreadable.
80
+ */
81
+ const streamForObject = (entry: SyncRegistryEntry, obj: Record<string, unknown>): string =>
82
+ resolveStreamForDoc({collectionTag: entry.collectionTag, doc: obj, scope: entry.config.scope});
83
+
84
+ /**
85
+ * Same, for the document a write is about to persist: refuses a tenant-scoped write with
86
+ * no scope value rather than filing it under an unsubscribable `tenant:undefined` stream
87
+ * (Task 9.21). Only the seq-stamping write paths use this — reads stay total.
88
+ */
89
+ const writableStreamForObject = (
90
+ entry: SyncRegistryEntry,
91
+ obj: Record<string, unknown>
92
+ ): string => {
93
+ assertWritableStream({collectionTag: entry.collectionTag, doc: obj, scope: entry.config.scope});
94
+ return streamForObject(entry, obj);
95
+ };
96
+
97
+ const unsupportedWrite = (modelName: string, operation: string): Error =>
98
+ new Error(
99
+ `${operation} is not supported on sync-enabled model ${modelName}: per-document seq ` +
100
+ "stamping requires single-document writes (and deletes must be soft). " +
101
+ "Loop per document instead."
102
+ );
103
+
104
+ /**
105
+ * m9: a query-write on a synced model must target exactly one document by `_id`, so the
106
+ * pre-write lookup resolves the stream of the SAME document the update mutates. A non-`_id`
107
+ * filter can match a different document and stamp the wrong stream's seq (duplicate seqs
108
+ * within a stream). Accept `{_id: value}` and `{_id: {$eq: value}}`.
109
+ */
110
+ const filterTargetsSingleId = (filter: Record<string, unknown>): boolean => {
111
+ const idClause = filter._id;
112
+ if (idClause === undefined || idClause === null) {
113
+ return false;
114
+ }
115
+ // A plain string/number id, or an ObjectId (or any BSON value — has a `_bsontype`),
116
+ // is a direct single-document match.
117
+ if (typeof idClause !== "object" || (idClause as {_bsontype?: unknown})._bsontype) {
118
+ return true;
119
+ }
120
+ // A query-operator object is only single-document when it is exactly `{$eq: value}`.
121
+ const keys = Object.keys(idClause as Record<string, unknown>);
122
+ return keys.length === 1 && keys[0] === "$eq";
123
+ };
124
+
125
+ const nonIdFilterError = (modelName: string, operation: string): Error =>
126
+ new Error(
127
+ `${operation} on sync-enabled model ${modelName} must target a single document by _id ` +
128
+ "(use findByIdAndUpdate or loop per document): a non-_id filter can stamp the wrong " +
129
+ "stream's seq."
130
+ );
131
+
132
+ const upsertError = (modelName: string, operation: string): Error =>
133
+ new Error(
134
+ `${operation} with upsert:true is not supported on sync-enabled model ${modelName}: an ` +
135
+ "upsert can create a document the pre-write lookup never saw, escaping seq stamping."
136
+ );
137
+
138
+ /**
139
+ * C4: how many times a scope-move marker insert is attempted before giving up. Losing the
140
+ * marker loses the old stream's tombstone forever (the exact race C4 exists to eliminate),
141
+ * so a transient failure (replica step-down, transient network error) is retried.
142
+ */
143
+ export const SCOPE_MOVE_MARKER_ATTEMPTS = 3;
144
+
145
+ /** Backoff between marker-write attempts. */
146
+ const SCOPE_MOVE_MARKER_RETRY_MS = 25;
147
+
148
+ const delay = (ms: number): Promise<void> =>
149
+ new Promise((resolve) => {
150
+ setTimeout(resolve, ms);
151
+ });
152
+
153
+ /**
154
+ * C4: write the scope-move marker (claim a seq from the OLD stream so it orders in that
155
+ * stream's frontier). Runs AFTER the move commits, so it never leaves a phantom tombstone
156
+ * for a write that failed; the insert is retried up to {@link SCOPE_MOVE_MARKER_ATTEMPTS}
157
+ * times because the already-committed move cannot be undone if the marker is lost.
158
+ *
159
+ * Never throws: the owning write has already committed, so a marker failure is logged
160
+ * rather than surfaced to the caller.
161
+ */
162
+ const writeScopeMoveMarker = async ({
163
+ entry,
164
+ entityId,
165
+ fromStream,
166
+ toStream,
167
+ session,
168
+ }: {
169
+ entry: SyncRegistryEntry;
170
+ entityId: string;
171
+ fromStream: string;
172
+ toStream: string;
173
+ session: ClientSession | null;
174
+ }): Promise<void> => {
175
+ let claim: Awaited<ReturnType<typeof claimSyncSeqs>>;
176
+ try {
177
+ claim = await claimSyncSeqs({session, stream: fromStream});
178
+ } catch (error: unknown) {
179
+ logger.error("[sync] Failed to claim scope-move marker seq", {
180
+ entityId,
181
+ error: String(error),
182
+ fromStream,
183
+ });
184
+ return;
185
+ }
186
+ // Retry the insert with the SAME claimed seq so a retry cannot mint a second marker
187
+ // ordered at a different point in the old stream.
188
+ for (let attempt = 1; attempt <= SCOPE_MOVE_MARKER_ATTEMPTS; attempt++) {
189
+ try {
190
+ await SyncScopeMove.create(
191
+ [
192
+ {
193
+ collectionTag: entry.collectionTag,
194
+ entityId,
195
+ fromStream,
196
+ seq: claim.lastSeq,
197
+ toStream,
198
+ },
199
+ ],
200
+ session ? {session} : {}
201
+ );
202
+ // The marker's own seq on the old stream is confirmed once the insert lands: that
203
+ // insert is the committing write for the claim.
204
+ if (claim.registered) {
205
+ await confirmSyncSeqs({seqs: claim.seqs, stream: fromStream}).catch((error: unknown) => {
206
+ logger.error("[sync] Failed to confirm scope-move marker seq", {
207
+ error: String(error),
208
+ stream: fromStream,
209
+ });
210
+ });
211
+ }
212
+ return;
213
+ } catch (error: unknown) {
214
+ const exhausted = attempt === SCOPE_MOVE_MARKER_ATTEMPTS;
215
+ logger.error("[sync] Failed to write scope-move marker", {
216
+ attempt,
217
+ entityId,
218
+ error: String(error),
219
+ exhausted,
220
+ fromStream,
221
+ });
222
+ if (exhausted) {
223
+ // The claim is left pending: the old stream's frontier holds below the missing
224
+ // tombstone until the lease ages it out, rather than skipping it silently.
225
+ return;
226
+ }
227
+ await delay(SCOPE_MOVE_MARKER_RETRY_MS);
228
+ }
229
+ }
230
+ };
231
+
232
+ /**
233
+ * Task 9.17: the confirm found no pending entry, meaning this writer's claim was reaped as
234
+ * abandoned (it stalled past {@link PENDING_CLAIM_LEASE_MS} without crashing) and the
235
+ * frontier has already advanced past `reapedSeq`. The document just committed BELOW every
236
+ * catch-up cursor, so re-stamp it with a freshly claimed seq: the raw-collection write
237
+ * bypasses this plugin's hooks (no recursion, no second claim) while still producing a
238
+ * change event, so online clients get a delta and offline ones see it on catch-up.
239
+ *
240
+ * Returns the new seq, or undefined when re-stamping failed (logged; the document stays
241
+ * below the frontier and only a full re-bootstrap recovers it).
242
+ */
243
+ const restampReapedSeq = async ({
244
+ model,
245
+ id,
246
+ stream,
247
+ reapedSeq,
248
+ }: {
249
+ model: Model<any>;
250
+ /** The document's RAW `_id` — the native driver does no casting, so a stringified
251
+ * ObjectId would match nothing and silently leave the doc below the frontier. */
252
+ id: unknown;
253
+ stream: string;
254
+ reapedSeq: number;
255
+ }): Promise<number | undefined> => {
256
+ logger.error(
257
+ "[sync] Seq claim was reaped before its write committed; re-stamping the document above " +
258
+ "the frontier",
259
+ {entityId: String(id), model: model.modelName, reapedSeq, stream}
260
+ );
261
+ try {
262
+ const claim = await claimSyncSeqs({stream});
263
+ const result = await model.collection.updateOne(
264
+ {_id: id as any},
265
+ {$set: {_syncSeq: claim.lastSeq}}
266
+ );
267
+ if ((result.matchedCount ?? 0) === 0) {
268
+ logger.error("[sync] Re-stamp matched no document; releasing the re-stamp claim", {
269
+ entityId: String(id),
270
+ seq: claim.lastSeq,
271
+ stream,
272
+ });
273
+ await releaseSyncSeqs({seqs: claim.seqs, stream});
274
+ return undefined;
275
+ }
276
+ const confirmed = await confirmSyncSeqs({seqs: claim.seqs, stream});
277
+ if (!confirmed.cleared) {
278
+ // Two reaps in a row means the process is pathologically slow; stop recursing.
279
+ logger.error("[sync] Re-stamped seq was itself reaped; document remains below the frontier", {
280
+ entityId: String(id),
281
+ seq: claim.lastSeq,
282
+ stream,
283
+ });
284
+ }
285
+ return claim.lastSeq;
286
+ } catch (error: unknown) {
287
+ logger.error("[sync] Failed to re-stamp a reaped seq", {
288
+ entityId: String(id),
289
+ error: String(error),
290
+ reapedSeq,
291
+ stream,
292
+ });
293
+ return undefined;
294
+ }
295
+ };
296
+
297
+ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
298
+ // Task 9.13: `doc.save()` must be conditional on the version the document was loaded at,
299
+ // so the `baseVersion` comparison in executeUpdate cannot be defeated by a concurrent
300
+ // save landing between the read and the write.
301
+ if (!schema.options.versionKey) {
302
+ throw new Error(
303
+ "syncPlugin requires a versionKey on the schema: sync relies on Mongoose " +
304
+ "optimisticConcurrency to make the baseVersion conflict check atomic. " +
305
+ "Remove `versionKey: false` from the schema options."
306
+ );
307
+ }
308
+ schema.set("optimisticConcurrency", true);
309
+
310
+ schema.add({
311
+ _syncPrevStream: {
312
+ default: null,
313
+ description:
314
+ "The document's previous sync stream, set when a write moved it between scopes; " +
315
+ "null when the last write did not move it",
316
+ type: String,
317
+ },
318
+ _syncSeq: {
319
+ description: "Monotonic per-stream sequence stamped on every synced write",
320
+ index: true,
321
+ type: Number,
322
+ },
323
+ });
324
+
325
+ // Capture the stream the document belonged to when it was loaded, so scope moves can
326
+ // be detected at save time without re-querying.
327
+ schema.post("init", function () {
328
+ const entry = findSyncEntryByModelName((this.constructor as Model<any>).modelName);
329
+ if (!entry) {
330
+ return;
331
+ }
332
+ this.$locals[INITIAL_STREAM_KEY] = streamForObject(entry, this.toObject());
333
+ });
334
+
335
+ const PENDING_COMMIT_KEY = "_syncPendingCommit";
336
+
337
+ /** What `post("save")` must finish once the document write commits. */
338
+ interface PendingSaveCommit {
339
+ claim: {registered: boolean; seqs: number[]};
340
+ currentStream: string;
341
+ entityId: string;
342
+ entry: SyncRegistryEntry;
343
+ prevStream: string | null;
344
+ session: ClientSession | null;
345
+ }
346
+
347
+ schema.pre("save", async function () {
348
+ const entry = findSyncEntryByModelName((this.constructor as Model<any>).modelName);
349
+ if (!entry) {
350
+ return;
351
+ }
352
+ // A previous save on this instance that failed after this hook leaves its pending work
353
+ // behind; drop it so this save's post hook never acts on the earlier attempt's claim.
354
+ this.$locals[PENDING_COMMIT_KEY] = undefined;
355
+ // m10: a save that changes nothing meaningful must not burn a seq or emit a delta.
356
+ // Excluded from the "meaningful" set: the sync stamps this hook writes (`_syncSeq`,
357
+ // `_syncPrevStream`) and auto-managed timestamp metadata (`updated`, which
358
+ // createdUpdatedPlugin bumps on EVERY save — otherwise no save would ever be a
359
+ // no-op). A save whose only modified paths are these is a no-op: skip the claim.
360
+ if (!this.isNew) {
361
+ const ignored = new Set(["_syncSeq", "_syncPrevStream", "updated"]);
362
+ const meaningful = this.modifiedPaths().filter((p) => !ignored.has(p));
363
+ if (meaningful.length === 0) {
364
+ return;
365
+ }
366
+ }
367
+ const session = this.$session() ?? null;
368
+ const currentStream = writableStreamForObject(entry, this.toObject());
369
+ let prevStream: string | null = null;
370
+ if (!this.isNew) {
371
+ const initialStream = this.$locals[INITIAL_STREAM_KEY] as string | undefined;
372
+ if (initialStream && initialStream !== currentStream) {
373
+ prevStream = initialStream;
374
+ }
375
+ }
376
+ const claim = await claimSyncSeqs({session, stream: currentStream});
377
+ this.set({_syncPrevStream: prevStream, _syncSeq: claim.lastSeq});
378
+ // Stash everything post-commit work needs: the seq confirm (C1) and the scope-move
379
+ // marker (C4). Both MUST wait for the commit — a save that fails after this hook
380
+ // (E11000, VersionError, a later user hook) must leave neither a confirmed seq nor a
381
+ // phantom tombstone that old-stream clients would apply to a still-live document.
382
+ const pending: PendingSaveCommit = {
383
+ claim,
384
+ currentStream,
385
+ entityId: String(this._id),
386
+ entry,
387
+ prevStream,
388
+ session,
389
+ };
390
+ this.$locals[PENDING_COMMIT_KEY] = pending;
391
+ });
392
+
393
+ // C1/C4: once the document write commits, confirm the claimed seq (so the stable
394
+ // frontier can advance past it) and record the durable scope-move marker. Document
395
+ // post('save') `this` is the saved document.
396
+ schema.post("save", async function () {
397
+ const pending = this.$locals[PENDING_COMMIT_KEY] as PendingSaveCommit | undefined;
398
+ if (!pending) {
399
+ return;
400
+ }
401
+ this.$locals[PENDING_COMMIT_KEY] = undefined;
402
+ if (pending.claim.registered) {
403
+ const confirmed = await confirmSyncSeqs({
404
+ seqs: pending.claim.seqs,
405
+ stream: pending.currentStream,
406
+ }).catch((error: unknown) => {
407
+ // Never fail the user write for a confirm error: the entry ages out via the lease.
408
+ logger.error("[sync] Failed to confirm seq after save", {
409
+ error: String(error),
410
+ stream: pending.currentStream,
411
+ });
412
+ return {cleared: true};
413
+ });
414
+ if (!confirmed.cleared) {
415
+ // Task 9.17: this writer's claim was reaped as abandoned while it was still
416
+ // running, so the frontier already passed the seq we just committed.
417
+ const restamped = await restampReapedSeq({
418
+ id: this._id,
419
+ model: this.constructor as Model<any>,
420
+ reapedSeq: pending.claim.seqs[pending.claim.seqs.length - 1],
421
+ stream: pending.currentStream,
422
+ });
423
+ if (restamped !== undefined) {
424
+ // Reflect the durable seq on the in-memory document so the caller's ack (and
425
+ // any serialization of this instance) reports the seq clients will actually see.
426
+ this.$set("_syncSeq", restamped);
427
+ this.unmarkModified("_syncSeq");
428
+ }
429
+ }
430
+ }
431
+ if (pending.prevStream) {
432
+ await writeScopeMoveMarker({
433
+ entityId: pending.entityId,
434
+ entry: pending.entry,
435
+ fromStream: pending.prevStream,
436
+ session: pending.session,
437
+ toStream: pending.currentStream,
438
+ });
439
+ }
440
+ // The committed stream becomes the baseline for the next save on this instance; a
441
+ // FAILED save keeps the old baseline so a retry still detects the move.
442
+ this.$locals[INITIAL_STREAM_KEY] = pending.currentStream;
443
+ });
444
+
445
+ // Task 9.13: an optimistic-concurrency loss (`VersionError`) matched no document, so the
446
+ // seq claimed in pre('save') will never be stamped on anything. Cancel it instead of
447
+ // letting it hold the stable frontier down for the full pending-claim lease — a lost
448
+ // update race is now an EXPECTED outcome, not a crash, and must not stall catch-up for
449
+ // every other client on the stream.
450
+ schema.post(
451
+ "save",
452
+ async function (error: any, doc: any, next: (err?: Error) => void): Promise<void> {
453
+ const locals = (this?.$locals ?? doc?.$locals) as Record<string, unknown> | undefined;
454
+ const pending = locals?.[PENDING_COMMIT_KEY] as PendingSaveCommit | undefined;
455
+ if (locals) {
456
+ locals[PENDING_COMMIT_KEY] = undefined;
457
+ }
458
+ if (pending?.claim.registered && isVersionError(error)) {
459
+ await releaseSyncSeqs({seqs: pending.claim.seqs, stream: pending.currentStream}).catch(
460
+ (releaseError: unknown) => {
461
+ logger.error("[sync] Failed to release the seq claim of a version-conflicted save", {
462
+ error: String(releaseError),
463
+ stream: pending.currentStream,
464
+ });
465
+ }
466
+ );
467
+ }
468
+ next(error);
469
+ }
470
+ );
471
+
472
+ schema.pre(
473
+ "insertMany",
474
+ async function (
475
+ next: CallbackWithoutResultAndOptionalError,
476
+ docs: Record<string, unknown>[],
477
+ options?: {session?: ClientSession | null}
478
+ ) {
479
+ try {
480
+ const model = this as unknown as Model<any>;
481
+ const entry = findSyncEntryByModelName(model.modelName);
482
+ if (!entry || !Array.isArray(docs) || docs.length === 0) {
483
+ return next();
484
+ }
485
+ const session = options?.session ?? null;
486
+ const byStream = new Map<string, Record<string, unknown>[]>();
487
+ for (const doc of docs) {
488
+ const stream = writableStreamForObject(entry, doc);
489
+ const group = byStream.get(stream) ?? [];
490
+ group.push(doc);
491
+ byStream.set(stream, group);
492
+ }
493
+ for (const [stream, group] of byStream) {
494
+ // The claim joins the caller's session so a transactional insertMany gets true
495
+ // counter+write atomicity (and registers no pending entry to confirm).
496
+ const claim = await claimSyncSeqs({count: group.length, session, stream});
497
+ group.forEach((doc, index) => {
498
+ doc._syncPrevStream = null;
499
+ doc._syncSeq = claim.seqs[index];
500
+ });
501
+ }
502
+ // C1: the confirm belongs in post('insertMany') — confirming here would let the
503
+ // stable frontier (and therefore a snapshot cursor) advance past seqs whose
504
+ // documents have not committed yet, stranding them below every catch-up cursor.
505
+ return next();
506
+ } catch (error: unknown) {
507
+ return next(error as Error);
508
+ }
509
+ }
510
+ );
511
+
512
+ // C1: the batch has committed — confirm the claimed seqs so the frontier advances past
513
+ // them. Post insertMany middleware receives the inserted (hydrated) documents, which
514
+ // carry the seqs stamped in the pre hook.
515
+ schema.post("insertMany", async function (docs: unknown) {
516
+ if (!Array.isArray(docs) || docs.length === 0) {
517
+ return;
518
+ }
519
+ const modelName = ((docs[0] as {constructor?: {modelName?: string}})?.constructor?.modelName ??
520
+ (this as unknown as Model<any>).modelName) as string | undefined;
521
+ const entry = modelName ? findSyncEntryByModelName(modelName) : undefined;
522
+ if (!entry) {
523
+ return;
524
+ }
525
+ const seqsByStream = new Map<string, number[]>();
526
+ for (const doc of docs as Record<string, any>[]) {
527
+ // A session-backed claim registered nothing to confirm, and a `$pull` issued outside
528
+ // the caller's still-open transaction would conflict with its counter write.
529
+ if (typeof doc?.$session === "function" && doc.$session()) {
530
+ continue;
531
+ }
532
+ const obj = (typeof doc?.toObject === "function" ? doc.toObject() : doc) as Record<
533
+ string,
534
+ unknown
535
+ >;
536
+ const seq = obj._syncSeq;
537
+ if (typeof seq !== "number") {
538
+ continue;
539
+ }
540
+ const stream = streamForObject(entry, obj);
541
+ const seqs = seqsByStream.get(stream) ?? [];
542
+ seqs.push(seq);
543
+ seqsByStream.set(stream, seqs);
544
+ }
545
+ for (const [stream, seqs] of seqsByStream) {
546
+ await confirmSyncSeqs({seqs, stream}).catch((error: unknown) => {
547
+ // Never fail the user write for a confirm error: the entries age out via the lease.
548
+ logger.error("[sync] Failed to confirm insertMany seqs", {
549
+ error: String(error),
550
+ stream,
551
+ });
552
+ });
553
+ }
554
+ });
555
+
556
+ // Single-document query writes: fetch the target to resolve its stream (and detect
557
+ // scope moves), claim a seq, and merge the stamp into the update.
558
+ const preQueryWrite = async function (this: Query<any, any>): Promise<void> {
559
+ const model = this.model as Model<any>;
560
+ const entry = findSyncEntryByModelName(model.modelName);
561
+ if (!entry) {
562
+ return;
563
+ }
564
+ const operation = (this as unknown as {op?: string}).op ?? "query-write";
565
+ // m8/C6: reject upserts — an upsert can create a doc the lookup never saw.
566
+ if (this.getOptions().upsert) {
567
+ throw upsertError(model.modelName, operation);
568
+ }
569
+ const rawFilter = this.getFilter();
570
+ // m9: refuse a non-single-_id filter — it could match a different doc than intended.
571
+ if (!filterTargetsSingleId(rawFilter as Record<string, unknown>)) {
572
+ throw nonIdFilterError(model.modelName, operation);
573
+ }
574
+ const session = this.getOptions().session ?? null;
575
+ // Mirror update semantics: query updates are NOT auto-filtered by isDeletedPlugin
576
+ // (it only hooks find/findOne), so the lookup must see tombstones too.
577
+ const filter: Record<string, unknown> = {...rawFilter};
578
+ if (filter.deleted === undefined) {
579
+ filter.deleted = {$in: [true, false]};
580
+ }
581
+ const matches = await model
582
+ .find(filter)
583
+ .setOptions(session ? {session} : {})
584
+ .limit(1);
585
+ const target = matches[0];
586
+ if (!target) {
587
+ // The update matches nothing; let it proceed as a no-op.
588
+ return;
589
+ }
590
+ const targetObj = target.toObject() as Record<string, unknown>;
591
+ const previousStream = streamForObject(entry, targetObj);
592
+
593
+ const rawUpdate = (this.getUpdate() ?? {}) as Record<string, any>;
594
+ // Only replaceOne/findOneAndReplace replace the document. A plain object passed to
595
+ // updateOne/findOneAndUpdate is an IMPLICIT $set in Mongoose — treating it as a
596
+ // replacement would resolve the scope value as undefined and claim the wrong stream.
597
+ const op = (this as unknown as {op?: string}).op ?? "";
598
+ const isTrueReplacement = op === "replaceOne" || op === "findOneAndReplace";
599
+ const hasOperators = Object.keys(rawUpdate).some((key) => key.startsWith("$"));
600
+ // The fields the update effectively $sets (empty for true replacements).
601
+ const setFields: Record<string, unknown> = isTrueReplacement
602
+ ? {}
603
+ : hasOperators
604
+ ? ((rawUpdate.$set as Record<string, unknown>) ?? {})
605
+ : rawUpdate;
606
+ const scopeField = getScopeField(entry.config.scope);
607
+
608
+ let currentStream: string;
609
+ if (typeof entry.config.scope === "function") {
610
+ const effectiveDoc = isTrueReplacement ? rawUpdate : {...targetObj, ...setFields};
611
+ currentStream = streamForObject(entry, effectiveDoc);
612
+ } else if (scopeField) {
613
+ // Distinguish "the update clears the scope field" from "the update leaves it
614
+ // alone": `??` cannot, because it treats an explicit `$set: {field: null}` as
615
+ // absent and falls back to the document's current value — which both hides the
616
+ // clear from the guard below and stamps the OLD stream's seq onto a document that
617
+ // no longer belongs to it.
618
+ const unsetFields: Record<string, unknown> = hasOperators
619
+ ? ((rawUpdate.$unset as Record<string, unknown>) ?? {})
620
+ : {};
621
+ let newScopeValue: unknown;
622
+ if (isTrueReplacement) {
623
+ newScopeValue = rawUpdate[scopeField];
624
+ } else if (scopeField in unsetFields) {
625
+ newScopeValue = undefined;
626
+ } else if (scopeField in setFields) {
627
+ newScopeValue = setFields[scopeField];
628
+ } else {
629
+ newScopeValue = targetObj[scopeField];
630
+ }
631
+ // A replacement, a `$set` to null, or an `$unset` can strip the tenant field off an
632
+ // existing document, which would move it into an unsubscribable stream just as
633
+ // surely as creating it without one.
634
+ assertWritableStream({
635
+ collectionTag: entry.collectionTag,
636
+ doc: {[scopeField]: newScopeValue},
637
+ scope: entry.config.scope,
638
+ });
639
+ currentStream = streamForScopeValue({
640
+ collectionTag: entry.collectionTag,
641
+ scope: entry.config.scope,
642
+ scopeValue: newScopeValue,
643
+ });
644
+ } else {
645
+ currentStream = previousStream;
646
+ }
647
+
648
+ const prevStream = previousStream !== currentStream ? previousStream : null;
649
+ const claim = await claimSyncSeqs({session, stream: currentStream});
650
+
651
+ if (isTrueReplacement || !hasOperators) {
652
+ // True replacements and implicit-$set plain objects both take plain keys.
653
+ rawUpdate._syncPrevStream = prevStream;
654
+ rawUpdate._syncSeq = claim.lastSeq;
655
+ } else {
656
+ rawUpdate.$set = {
657
+ ...(rawUpdate.$set ?? {}),
658
+ _syncPrevStream: prevStream,
659
+ _syncSeq: claim.lastSeq,
660
+ };
661
+ }
662
+ this.setUpdate(rawUpdate);
663
+ // Stash the claim + move info so the query post hook confirms/records after commit.
664
+ (this as unknown as {_syncPendingConfirm?: unknown})._syncPendingConfirm = {
665
+ claim,
666
+ currentStream,
667
+ entityId: String(target._id),
668
+ entry,
669
+ prevStream,
670
+ session,
671
+ };
672
+ };
673
+
674
+ // C1/C4: after the query write commits, confirm the claimed seq and record the
675
+ // scope-move marker. Query post middleware `this` is the Query (not a document).
676
+ const postQueryWrite = async function (this: Query<any, any>): Promise<void> {
677
+ const pending = (this as unknown as {_syncPendingConfirm?: any})._syncPendingConfirm;
678
+ if (!pending) {
679
+ return;
680
+ }
681
+ (this as unknown as {_syncPendingConfirm?: unknown})._syncPendingConfirm = undefined;
682
+ const {claim, currentStream, entityId, entry, prevStream, session} = pending as {
683
+ claim: {registered: boolean; seqs: number[]};
684
+ currentStream: string;
685
+ entityId: string;
686
+ entry: SyncRegistryEntry;
687
+ prevStream: string | null;
688
+ session: ClientSession | null;
689
+ };
690
+ if (claim.registered) {
691
+ await confirmSyncSeqs({seqs: claim.seqs, stream: currentStream}).catch((error: unknown) => {
692
+ logger.error("[sync] Failed to confirm seq after query write", {
693
+ error: String(error),
694
+ stream: currentStream,
695
+ });
696
+ });
697
+ }
698
+ if (prevStream) {
699
+ // Retries internally and never throws: the move already committed, so the marker
700
+ // must not be dropped on the first transient failure nor fail the user's write.
701
+ await writeScopeMoveMarker({
702
+ entityId,
703
+ entry,
704
+ fromStream: prevStream,
705
+ session,
706
+ toStream: currentStream,
707
+ });
708
+ }
709
+ };
710
+
711
+ schema.pre("updateOne", {document: false, query: true}, preQueryWrite);
712
+ schema.pre("findOneAndUpdate", preQueryWrite);
713
+ schema.pre("replaceOne", preQueryWrite);
714
+ schema.pre("findOneAndReplace", preQueryWrite);
715
+ schema.post("updateOne", {document: false, query: true}, postQueryWrite);
716
+ schema.post("findOneAndUpdate", postQueryWrite);
717
+ schema.post("replaceOne", postQueryWrite);
718
+ schema.post("findOneAndReplace", postQueryWrite);
719
+
720
+ // Unsupported multi-document / hard-delete paths throw for registered models.
721
+ const guardQuery = (operation: string) =>
722
+ function (this: Query<any, any>): void {
723
+ const model = this.model as Model<any>;
724
+ if (findSyncEntryByModelName(model.modelName)) {
725
+ throw unsupportedWrite(model.modelName, operation);
726
+ }
727
+ };
728
+ schema.pre("updateMany", guardQuery("updateMany"));
729
+ schema.pre("deleteMany", guardQuery("deleteMany"));
730
+ schema.pre("deleteOne", {document: false, query: true}, guardQuery("deleteOne"));
731
+ schema.pre("findOneAndDelete", guardQuery("findOneAndDelete"));
732
+ schema.pre("deleteOne", {document: true, query: false}, function () {
733
+ const entry = findSyncEntryByModelName((this.constructor as Model<any>).modelName);
734
+ if (entry) {
735
+ throw unsupportedWrite(entry.modelName, "document deleteOne (hard delete)");
736
+ }
737
+ });
738
+ };