@brass-build/client 0.1.0 → 0.2.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 (193) hide show
  1. package/AGENTS.md +72 -63
  2. package/CHANGELOG.md +86 -0
  3. package/README.md +317 -231
  4. package/dist/auth-flow.d.ts +10 -1
  5. package/dist/auth-flow.d.ts.map +1 -1
  6. package/dist/auth-flow.js +64 -20
  7. package/dist/auth-flow.js.map +1 -1
  8. package/dist/client.d.ts +3 -0
  9. package/dist/client.d.ts.map +1 -1
  10. package/dist/client.js +217 -75
  11. package/dist/client.js.map +1 -1
  12. package/dist/document-engine.d.ts +4 -0
  13. package/dist/document-engine.d.ts.map +1 -0
  14. package/dist/document-engine.js +9 -0
  15. package/dist/document-engine.js.map +1 -0
  16. package/dist/document-streams.d.ts +8 -1
  17. package/dist/document-streams.d.ts.map +1 -1
  18. package/dist/document-streams.js +79 -8
  19. package/dist/document-streams.js.map +1 -1
  20. package/dist/documents-namespace.d.ts.map +1 -1
  21. package/dist/documents-namespace.js +19 -3
  22. package/dist/documents-namespace.js.map +1 -1
  23. package/dist/documents.d.ts +43 -14
  24. package/dist/documents.d.ts.map +1 -1
  25. package/dist/documents.js +871 -223
  26. package/dist/documents.js.map +1 -1
  27. package/dist/embed-protocol.d.ts +3 -0
  28. package/dist/embed-protocol.d.ts.map +1 -0
  29. package/dist/embed-protocol.js +26 -0
  30. package/dist/embed-protocol.js.map +1 -0
  31. package/dist/emitter.d.ts.map +1 -1
  32. package/dist/emitter.js +8 -1
  33. package/dist/emitter.js.map +1 -1
  34. package/dist/export-guest.d.ts.map +1 -1
  35. package/dist/export-guest.js +14 -6
  36. package/dist/export-guest.js.map +1 -1
  37. package/dist/fetch-deadline.d.ts +22 -0
  38. package/dist/fetch-deadline.d.ts.map +1 -0
  39. package/dist/fetch-deadline.js +96 -0
  40. package/dist/fetch-deadline.js.map +1 -0
  41. package/dist/fetch-retry.d.ts +1 -0
  42. package/dist/fetch-retry.d.ts.map +1 -1
  43. package/dist/fetch-retry.js +12 -5
  44. package/dist/fetch-retry.js.map +1 -1
  45. package/dist/handle-types.d.ts +150 -0
  46. package/dist/handle-types.d.ts.map +1 -0
  47. package/dist/handle-types.js +8 -0
  48. package/dist/handle-types.js.map +1 -0
  49. package/dist/handles.d.ts +48 -48
  50. package/dist/handles.d.ts.map +1 -1
  51. package/dist/handles.js +3122 -501
  52. package/dist/handles.js.map +1 -1
  53. package/dist/hosted-runtime.d.ts.map +1 -1
  54. package/dist/hosted-runtime.js +8 -12
  55. package/dist/hosted-runtime.js.map +1 -1
  56. package/dist/hosted-ui.d.ts +0 -1
  57. package/dist/hosted-ui.d.ts.map +1 -1
  58. package/dist/hosted-ui.js +29 -24
  59. package/dist/hosted-ui.js.map +1 -1
  60. package/dist/importer.d.ts.map +1 -1
  61. package/dist/importer.js +253 -8
  62. package/dist/importer.js.map +1 -1
  63. package/dist/index.d.ts +10 -10
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +11 -2
  66. package/dist/index.js.map +1 -1
  67. package/dist/internal.d.ts +1 -1
  68. package/dist/internal.d.ts.map +1 -1
  69. package/dist/internal.js.map +1 -1
  70. package/dist/join-coverage.d.ts +12 -0
  71. package/dist/join-coverage.d.ts.map +1 -0
  72. package/dist/join-coverage.js +37 -0
  73. package/dist/join-coverage.js.map +1 -0
  74. package/dist/jwt.d.ts.map +1 -1
  75. package/dist/jwt.js +5 -1
  76. package/dist/jwt.js.map +1 -1
  77. package/dist/launch-context.js +2 -2
  78. package/dist/launch-context.js.map +1 -1
  79. package/dist/mutate-batching.d.ts +25 -2
  80. package/dist/mutate-batching.d.ts.map +1 -1
  81. package/dist/mutate-batching.js +57 -10
  82. package/dist/mutate-batching.js.map +1 -1
  83. package/dist/open-document.d.ts +15 -1
  84. package/dist/open-document.d.ts.map +1 -1
  85. package/dist/open-document.js +1851 -321
  86. package/dist/open-document.js.map +1 -1
  87. package/dist/presence.d.ts +28 -0
  88. package/dist/presence.d.ts.map +1 -1
  89. package/dist/presence.js +149 -7
  90. package/dist/presence.js.map +1 -1
  91. package/dist/record-paging.d.ts +336 -71
  92. package/dist/record-paging.d.ts.map +1 -1
  93. package/dist/record-paging.js +591 -127
  94. package/dist/record-paging.js.map +1 -1
  95. package/dist/selection-persistence.d.ts.map +1 -1
  96. package/dist/selection-persistence.js +11 -1
  97. package/dist/selection-persistence.js.map +1 -1
  98. package/dist/serializer.d.ts +30 -1
  99. package/dist/serializer.d.ts.map +1 -1
  100. package/dist/session-persistence.d.ts.map +1 -1
  101. package/dist/session-persistence.js +9 -8
  102. package/dist/session-persistence.js.map +1 -1
  103. package/dist/session-toolbar.d.ts +0 -1
  104. package/dist/session-toolbar.d.ts.map +1 -1
  105. package/dist/session-toolbar.js +226 -75
  106. package/dist/session-toolbar.js.map +1 -1
  107. package/dist/session.d.ts.map +1 -1
  108. package/dist/session.js +87 -18
  109. package/dist/session.js.map +1 -1
  110. package/dist/tail-ops.d.ts +14 -4
  111. package/dist/tail-ops.d.ts.map +1 -1
  112. package/dist/tail-ops.js +75 -15
  113. package/dist/tail-ops.js.map +1 -1
  114. package/dist/topic-lifecycle.d.ts +4 -0
  115. package/dist/topic-lifecycle.d.ts.map +1 -1
  116. package/dist/topic-lifecycle.js +25 -1
  117. package/dist/topic-lifecycle.js.map +1 -1
  118. package/dist/types.d.ts +47 -1
  119. package/dist/types.d.ts.map +1 -1
  120. package/dist/undo.d.ts +2 -3
  121. package/dist/undo.d.ts.map +1 -1
  122. package/dist/undo.js +11 -0
  123. package/dist/undo.js.map +1 -1
  124. package/dist/unload-guard.d.ts +5 -0
  125. package/dist/unload-guard.d.ts.map +1 -0
  126. package/dist/unload-guard.js +88 -0
  127. package/dist/unload-guard.js.map +1 -0
  128. package/dist/version-bar.d.ts +4 -3
  129. package/dist/version-bar.d.ts.map +1 -1
  130. package/dist/version-bar.js +140 -19
  131. package/dist/version-bar.js.map +1 -1
  132. package/dist/version.d.ts +2 -2
  133. package/dist/version.js +1 -1
  134. package/dist/vite.d.ts +18 -2
  135. package/dist/vite.d.ts.map +1 -1
  136. package/dist/vite.js +133 -8
  137. package/dist/vite.js.map +1 -1
  138. package/dist/ws-client.d.ts +23 -3
  139. package/dist/ws-client.d.ts.map +1 -1
  140. package/dist/ws-client.js +339 -89
  141. package/dist/ws-client.js.map +1 -1
  142. package/dist/ws-frame-limit.d.ts.map +1 -1
  143. package/dist/ws-frame-limit.js +19 -12
  144. package/dist/ws-frame-limit.js.map +1 -1
  145. package/docs/app-manifest.md +7 -8
  146. package/docs/custom-chrome.md +17 -20
  147. package/docs/hosted-ui-advanced.md +14 -17
  148. package/docs/imported-documents.md +53 -197
  149. package/docs/presence.md +45 -21
  150. package/docs/schemas.md +12 -17
  151. package/docs/streams.md +55 -57
  152. package/package.json +4 -3
  153. package/src/auth-flow.ts +90 -23
  154. package/src/client.ts +225 -78
  155. package/src/document-engine.ts +15 -0
  156. package/src/document-signals.ts +7 -7
  157. package/src/document-streams.ts +95 -9
  158. package/src/documents-namespace.ts +18 -3
  159. package/src/documents.ts +983 -277
  160. package/src/embed-protocol.ts +25 -0
  161. package/src/emitter.ts +8 -1
  162. package/src/export-guest.ts +15 -9
  163. package/src/fetch-deadline.ts +119 -0
  164. package/src/fetch-retry.ts +14 -5
  165. package/src/handle-types.ts +303 -0
  166. package/src/handles.ts +3406 -683
  167. package/src/hosted-runtime.ts +8 -12
  168. package/src/hosted-ui.ts +28 -32
  169. package/src/importer.ts +261 -8
  170. package/src/index.ts +29 -18
  171. package/src/internal.ts +1 -0
  172. package/src/join-coverage.ts +42 -0
  173. package/src/jwt.ts +8 -1
  174. package/src/launch-context.ts +2 -2
  175. package/src/mutate-batching.ts +60 -11
  176. package/src/open-document.ts +2355 -515
  177. package/src/presence.ts +210 -11
  178. package/src/record-paging.ts +687 -137
  179. package/src/selection-persistence.ts +10 -1
  180. package/src/serializer.ts +33 -1
  181. package/src/session-persistence.ts +9 -8
  182. package/src/session-toolbar.ts +249 -80
  183. package/src/session.ts +91 -17
  184. package/src/tail-ops.ts +76 -10
  185. package/src/topic-lifecycle.ts +31 -1
  186. package/src/types.ts +56 -4
  187. package/src/undo.ts +13 -5
  188. package/src/unload-guard.ts +91 -0
  189. package/src/version-bar.ts +163 -25
  190. package/src/version.ts +1 -1
  191. package/src/vite.ts +175 -10
  192. package/src/ws-client.ts +392 -92
  193. package/src/ws-frame-limit.ts +19 -12
package/src/documents.ts CHANGED
@@ -17,11 +17,8 @@ import {
17
17
  decodeImportBlobMeta,
18
18
  } from 'loro-crdt';
19
19
 
20
- import { MutateBatcher } from './mutate-batching.js';
21
- import {
22
- UndoController,
23
- type UndoControllerOptions,
24
- } from './undo.js';
20
+ import { PAGING_COMMIT_ORIGIN, UndoController } from './undo.js';
21
+ import type { DocSyncDiagnostics, UndoControllerOptions } from './types.js';
25
22
  import {
26
23
  createOrderedMapHandle,
27
24
  createRootListHandle,
@@ -29,22 +26,24 @@ import {
29
26
  rebindRootListAnchor,
30
27
  rebindRootMapAnchor,
31
28
  resyncRootHandle,
29
+ snapshotRootMapKeys,
32
30
  teardownRootHandle,
33
- wrapLoroMapProxy,
34
- type ChangeSource,
35
- type HandleOwner,
36
- type ListHandle,
37
- type MapHandle,
38
- type OrderedMapHandle,
31
+ type StateVersion,
39
32
  } from './handles.js';
33
+ import type {
34
+ ChangeSource,
35
+ HandleOwner,
36
+ ListHandle,
37
+ MapHandle,
38
+ OrderedMapHandle,
39
+ } from './handle-types.js';
40
40
  import {
41
- CURRENT_PAGING_SCHEME,
42
- isPagedByPageCount,
41
+ flattenPagedLookup,
42
+ isPagedMarker,
43
43
  META_KEY,
44
- PAGE_COUNT_KEY,
45
- PAGING_SCHEME_KEY,
46
- RECORDS_KEY,
44
+ pagedRootKey,
47
45
  } from './record-paging.js';
46
+ import { MAX_OP_BLOB_CHARS } from './ws-frame-limit.js';
48
47
  import type {
49
48
  BranchSummary,
50
49
  DocumentCheckpoint,
@@ -83,6 +82,7 @@ import {
83
82
  BranchController,
84
83
  type BranchProvider,
85
84
  } from './document-branches.js';
85
+ import { releaseUnsavedWork, trackUnsavedWork } from './unload-guard.js';
86
86
  import {
87
87
  CommandMenu,
88
88
  type Command,
@@ -140,21 +140,23 @@ export interface DocumentMutator<E extends object = EntityProxy> {
140
140
  }
141
141
 
142
142
  // What the entity proxy presents by default: any nested plain object
143
- // shape backed by a LoroMap / LoroList container tree. `Record<string,
143
+ // shape backed by a LoroMap / LoroMovableList container tree. `Record<string,
144
144
  // unknown>` because Loro's value type is dynamic; typed apps pass their
145
145
  // shape to `DocumentMutator<E>` / `doc.mutate<E>` instead.
146
146
  export type EntityProxy = Record<string, unknown>;
147
147
 
148
- export type DocumentSubscriber = (event: unknown) => void;
148
+ export type DocumentSubscriber = (event: SyntheticSubscribeEvent) => void;
149
149
  export type DocumentUnsubscribe = () => void;
150
150
 
151
- // Discriminated event the SDK fires through `doc.subscribe` for
152
- // non-CRDT changes (checkout, return-to-head, saving count). Apps can
153
- // ignore the event arg and re-read in the handler, or narrow on
154
- // `event.kind`. CRDT-origin events keep their Loro shape (also
155
- // delivered through the same `subscribe`).
151
+ // The discriminated event `doc.subscribe` fires. Every event the document
152
+ // produces arrives in this shape, whether a content change or one of the
153
+ // SDK's own metadata ticks, so an app can ignore the argument and re-read in
154
+ // the handler, or narrow on `event.kind`.
156
155
  export type SyntheticSubscribeEvent =
157
- | { kind: 'checkout'; version: number }
156
+ // A content change, with what produced it. `source` is the same value the
157
+ // handle surface reports as `origin.source`.
158
+ | { kind: 'change'; source: ChangeSource }
159
+ | { kind: 'history'; version: number }
158
160
  | { kind: 'return-to-head' }
159
161
  // A bare re-read tick: the document's branch state settled or changed
160
162
  // (the list loaded on open, or a switch/create landed). Carries no
@@ -163,6 +165,12 @@ export type SyntheticSubscribeEvent =
163
165
  // branch model, management is hosted.
164
166
  | { kind: 'branches' }
165
167
  | { kind: 'saving-count' }
168
+ // The document's capacity verdict changed, so `doc.atCapacity` reads
169
+ // differently than it did. Fired on its own kind rather than folded into
170
+ // `saving-count` because an app acts on it differently: a saving count
171
+ // moves constantly and settles, while this one gates whether adding more
172
+ // is worth offering at all.
173
+ | { kind: 'capacity' }
166
174
  | { kind: 'commands' };
167
175
 
168
176
  // Snapshot of the Document's undo/redo state, pushed to
@@ -193,7 +201,6 @@ const REDO_COMMIT_ORIGIN = 'brass.redo';
193
201
  function changeSourceOf(event: unknown): ChangeSource {
194
202
  const by = (event as { by?: unknown }).by;
195
203
  if (by === 'import') return 'remote';
196
- if (by === 'checkout') return 'checkout';
197
204
  // by === 'local': disambiguate via the commit-origin tag.
198
205
  const origin = (event as { origin?: unknown }).origin;
199
206
  if (origin === UNDO_COMMIT_ORIGIN) return 'undo';
@@ -201,20 +208,53 @@ function changeSourceOf(event: unknown): ChangeSource {
201
208
  return 'local';
202
209
  }
203
210
 
204
- // Per-chunk cap on a resync-push blob (durability handshake). Each push
205
- // ships as one op frame, and the transport carries 128 KB; the blob is
206
- // base64-encoded (~1.33x) inside a JSON envelope, so the raw blob must
207
- // stay well under that. 64 KB base64s to ~85 KB. A larger missing slice
208
- // is split into multiple chunks (see `computeResyncPush`), each capped.
209
- const RESYNC_PUSH_MAX_BYTES = 64 * 1024;
210
-
211
- // Hard ceiling for a single irreducible op (one Loro op, unsplittable).
212
- // Sized so the base64 frame still clears the 128 KB limit (90 KB base64s
213
- // to ~120 KB plus envelope). An op this large cannot occur on a doc
214
- // whose normal edits send at all (same frame limit), so this only guards
215
- // a pathological op; it is skipped with a warning rather than retried
211
+ // Per-chunk cap on a blob this Document pushes as several ops: a durability
212
+ // resync's missing slice (`computeResyncPush`) and a local commit no write
213
+ // budget bounded (`splitLocalUpdate`).
214
+ //
215
+ // Each chunk goes out through the same local-op hook an ordinary commit
216
+ // uses, so the transport picks itself by size and a chunk this large takes
217
+ // the HTTP op write. Sizing the chunk to a WebSocket frame instead is what
218
+ // makes a heal the most expensive write the SDK performs: the slice a bulk
219
+ // write leaves behind is the whole document, and at frame size that is
220
+ // thousands of ops, each one landing on the document's single storage
221
+ // partition at exactly the size that stays inline on its row. Measured, a
222
+ // 40 MB divergence healed in 16 KB chunks throttled the partition and left
223
+ // the join a tail it refused to serve. The same divergence at this size is
224
+ // tens of ops whose bytes go to the bucket instead.
225
+ const RESYNC_PUSH_MAX_BYTES = 512 * 1024;
226
+
227
+ // Hard ceiling for a single irreducible op (one Loro op, unsplittable),
228
+ // beyond which the chunk is skipped with a warning rather than retried
216
229
  // forever, while the doc's other chunks still heal.
217
- const RESYNC_PUSH_HARD_MAX_BYTES = 90 * 1024;
230
+ //
231
+ // Skipping drops an edit the server never received, so this sits at exactly
232
+ // what the write path can carry rather than at what a frame holds. Base64
233
+ // turns B raw bytes into ceil(B/3)*4 characters, so the last raw size whose
234
+ // encoding still fits the durable cap is three quarters of it. Derived
235
+ // rather than written out, because the two have to move together and a
236
+ // literal here would let the ceiling drift above what the server accepts,
237
+ // which turns a heal into silent loss. A blob this large travels through
238
+ // the staged upload rather than a request body, a path the write already
239
+ // takes on its own size.
240
+ const RESYNC_PUSH_HARD_MAX_BYTES = Math.floor((MAX_OP_BLOB_CHARS * 3) / 4);
241
+
242
+ // What one run of the durability handshake settled. Only `covered` means the
243
+ // durable frontier holds everything local does; a caller driving the
244
+ // handshake to close a known divergence keeps asking on anything else.
245
+ //
246
+ // covered the server holds it all, so the handshake is finished
247
+ // pushed chunks are on the wire and their acks decide the outcome
248
+ // in-flight an earlier push has not settled, so this run stood down
249
+ // undecodable the frontier did not parse, so nothing was compared
250
+ // closed the doc was freed before the handshake ran
251
+ /** @internal */
252
+ export type ResyncPushOutcome =
253
+ | 'covered'
254
+ | 'pushed'
255
+ | 'in-flight'
256
+ | 'undecodable'
257
+ | 'closed';
218
258
 
219
259
  // Element-wise max of two Loro version vectors. `VersionVector` exposes
220
260
  // no public join, so round-trip through its `Map<PeerID, number>` JSON
@@ -233,64 +273,193 @@ function mergeVersionVectors(
233
273
  return VersionVector.parseJSON(merged);
234
274
  }
235
275
 
236
- // Split half-open per-peer counter ranges into two halves by total op
237
- // count, splitting at most one range across the boundary. `total` must
238
- // be >= 2 so both halves are non-empty (the caller handles total === 1
239
- // as the irreducible single-op case). Order is preserved.
240
- function splitRangesByLength(
241
- ranges: { peer: PeerID; from: number; to: number }[],
242
- total: number,
243
- ): [
244
- { peer: PeerID; from: number; to: number }[],
245
- { peer: PeerID; from: number; to: number }[],
246
- ] {
247
- const half = Math.floor(total / 2);
248
- const first: { peer: PeerID; from: number; to: number }[] = [];
249
- const second: { peer: PeerID; from: number; to: number }[] = [];
276
+ type CounterRange = { peer: PeerID; from: number; to: number };
277
+
278
+ function rangeOpCount(ranges: readonly CounterRange[]): number {
279
+ return ranges.reduce((n, r) => n + (r.to - r.from), 0);
280
+ }
281
+
282
+ // Split half-open per-peer counter ranges in two after `at` ops, splitting at
283
+ // most one range across the boundary. Order is preserved.
284
+ function splitRangesAt(
285
+ ranges: readonly CounterRange[],
286
+ at: number,
287
+ ): [CounterRange[], CounterRange[]] {
288
+ const first: CounterRange[] = [];
289
+ const second: CounterRange[] = [];
250
290
  let acc = 0;
251
291
  for (const r of ranges) {
252
292
  const len = r.to - r.from;
253
- if (acc >= half) {
293
+ if (acc >= at) {
254
294
  second.push(r);
255
- } else if (acc + len <= half) {
295
+ } else if (acc + len <= at) {
256
296
  first.push(r);
257
297
  acc += len;
258
298
  } else {
259
- const splitAt = r.from + (half - acc);
299
+ const splitAt = r.from + (at - acc);
260
300
  first.push({ peer: r.peer, from: r.from, to: splitAt });
261
301
  second.push({ peer: r.peer, from: splitAt, to: r.to });
262
- acc = half;
302
+ acc = at;
263
303
  }
264
304
  }
265
305
  return [first, second];
266
306
  }
267
307
 
268
- // The record store's page count off a doc's `meta` map, or 0 when
269
- // unpaged. `handles.ts` reads it to resolve which page a key hashes to
270
- // AND to decide whether the store is paged at all (on every root-map
271
- // read / edit). Read by PATH, not `getMap(META_KEY)`: `getMap` is
272
- // get-or-create and would materialize an empty `meta` root on every
273
- // unpaged document, polluting its wire shape; `getByPath` is a pure read
274
- // returning undefined when `meta` does not exist.
275
- function readStorePageCount(source: LoroDoc): number {
276
- const pc = source.getByPath(`${META_KEY}/${PAGE_COUNT_KEY}`);
277
- // Same "is paged" predicate as the formats/core readers, so the SDK
278
- // handle path and the store readers cannot disagree on a malformed
279
- // count (e.g. a non-finite value must read as unpaged everywhere).
280
- return isPagedByPageCount(pc) ? (pc as number) : 0;
308
+ // Split into two halves by op count. `total` must be >= 2 so both halves are
309
+ // non-empty (the caller handles total === 1 as the irreducible single-op case).
310
+ function splitRangesByLength(
311
+ ranges: readonly CounterRange[],
312
+ total: number,
313
+ ): [CounterRange[], CounterRange[]] {
314
+ return splitRangesAt(ranges, Math.floor(total / 2));
315
+ }
316
+
317
+ // Split into `parts` pieces of roughly equal op count, in order.
318
+ //
319
+ // A caller that already knows the whole range's byte size can name the number
320
+ // of pieces it wants instead of discovering it by halving, which costs one
321
+ // export per level and re-exports the same ops at every level: an 8 MB commit
322
+ // takes 31 exports to reach sixteen chunks by halving and 16 by naming them.
323
+ // The pieces are still measured afterwards, so an uneven commit that leaves a
324
+ // piece over the cap is split further from there.
325
+ function splitRangesEvenly(
326
+ ranges: readonly CounterRange[],
327
+ parts: number,
328
+ ): CounterRange[][] {
329
+ let rest = [...ranges];
330
+ let remaining = rangeOpCount(rest);
331
+ if (parts <= 1 || remaining <= parts) return [rest];
332
+ const out: CounterRange[][] = [];
333
+ for (let left = parts; left > 1; left -= 1) {
334
+ const at = Math.floor(remaining / left);
335
+ if (at < 1) break;
336
+ const [first, second] = splitRangesAt(rest, at);
337
+ out.push(first);
338
+ rest = second;
339
+ remaining -= at;
340
+ }
341
+ out.push(rest);
342
+ return out.filter((piece) => rangeOpCount(piece) > 0);
343
+ }
344
+
345
+ // The `meta` root a document's paged marker lives in, or `null` while the
346
+ // document has none.
347
+ //
348
+ // The marker is read on every root-map read, edit and keyed lookup, and a
349
+ // reader that answers from a copy taken earlier is what puts paged-shaped keys
350
+ // into a store whose marker says unpaged, or reads a root a peer paged as its
351
+ // raw page entries. Answering off the live container instead makes both
352
+ // impossible: a peer's write lands in that same container, so the next read
353
+ // sees it, with no invalidation to get right.
354
+ //
355
+ // What is remembered per document is the resolved CONTAINER, and only once the
356
+ // root exists. A root handle reads through to the document's current state, so
357
+ // holding one keeps the value live: a peer's import and a checkout both land
358
+ // on the same container the cell already points at.
359
+ //
360
+ // Holding it is also what keeps this off the leak. Every `getMap` mints a new
361
+ // wasm-bindgen handle that dropping does not reclaim (the same fact `pageValue`
362
+ // and `entryExists` free around), and this resolves on every root-map read,
363
+ // edit and keyed lookup, so minting per call retains ~145 bytes each for the
364
+ // length of the session. One handle per document is a bounded cost instead.
365
+ //
366
+ // Resolution goes through `getByPath` rather than `getMap`, which is
367
+ // get-or-create and would materialize an empty `meta` on a document that has
368
+ // none (in the wire shape of every unpaged document, and as a local write on a
369
+ // read-only open). Its cost tracks the document's container count, and it is
370
+ // paid once per document.
371
+ const metaRoots = new WeakMap<LoroDoc, { map: LoroMap | null }>();
372
+
373
+ function metaRoot(source: LoroDoc): LoroMap | null {
374
+ let cell = metaRoots.get(source);
375
+ if (cell === undefined) {
376
+ cell = { map: null };
377
+ metaRoots.set(source, cell);
378
+ }
379
+ if (cell.map === null) {
380
+ const found = source.getByPath(META_KEY);
381
+ if (!(found instanceof LoroMap)) return null;
382
+ cell.map = found;
383
+ }
384
+ return cell.map;
385
+ }
386
+
387
+ /** The `meta` root, creating it if `source` has none, and remembering it so
388
+ * later reads resolve without minting a second handle. */
389
+ function metaRootOrCreate(source: LoroDoc): LoroMap {
390
+ const existing = metaRoot(source);
391
+ if (existing !== null) return existing;
392
+ const created = source.getMap(META_KEY);
393
+ metaRoots.set(source, { map: created });
394
+ return created;
395
+ }
396
+
397
+ /** Whether one root map is paged. Same predicate the record-store readers
398
+ * use, so the SDK handle path and those readers cannot disagree. */
399
+ function readRootPaged(source: LoroDoc, rootKey: string): boolean {
400
+ return isPagedMarker(metaRoot(source)?.get(pagedRootKey(rootKey)));
401
+ }
402
+
403
+ /** Whether a root's JSON is the keyed lookup a record store flattens. A root
404
+ * that projects to a list or a scalar cannot hold pages whatever `meta`
405
+ * says, so it is handed back untouched. */
406
+ function isRootLookup(value: unknown): value is Record<string, unknown> {
407
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
281
408
  }
282
409
 
283
- // The paging scheme a paged store was written under, off the same `meta` map
284
- // as the page count (read by path, not `getMap`, for the same reason: a pure
285
- // read that never materializes an empty `meta` root). A paged store predating
286
- // the stamp reports the current scheme, so it stays editable; a store written
287
- // under a newer scheme reports that number, and the edit path refuses it
288
- // rather than hashing a key into the wrong page.
289
- function readStorePagingScheme(source: LoroDoc): number {
290
- const s = source.getByPath(`${META_KEY}/${PAGING_SCHEME_KEY}`);
291
- return typeof s === 'number' && s > 0 ? s : CURRENT_PAGING_SCHEME;
410
+ // Mark `rootKey` paged, under a `meta` key of its own. Written ONCE and never
411
+ // revised: pages are addressed by session and index, so nothing downstream
412
+ // needs to know how many there are or how much the store grows afterwards.
413
+ //
414
+ // A key per root is what lets two peers page two different roots at the same
415
+ // time. One entry naming every paged root would have to be read, extended and
416
+ // written back whole, and the losing side of that race takes its root's marker
417
+ // with it (see `PAGED_ROOT_PREFIX`); two keys merge.
418
+ //
419
+ // The marker commits on its own, under an origin the undo stack excludes, and
420
+ // `commitPending` lands whatever the running mutation has written so far
421
+ // first. Two properties ride on that. Peers and the server never see a page
422
+ // without the marker that says how to read it, because the marker's op
423
+ // precedes every op carrying a page. And nothing an app authored is taken out
424
+ // of the undo stack with it: a commit carrying both would exclude the records
425
+ // as well as the marker.
426
+ function pageRoot(
427
+ source: LoroDoc,
428
+ rootKey: string,
429
+ commitPending: () => void,
430
+ ): void {
431
+ if (readRootPaged(source, rootKey)) return;
432
+ commitPending();
433
+ metaRootOrCreate(source).set(pagedRootKey(rootKey), true);
434
+ source.setNextCommitOrigin(PAGING_COMMIT_ORIGIN);
435
+ source.commit();
292
436
  }
293
437
 
438
+ // Why a live Document refuses local writes. Each names a refusal the server
439
+ // would make, so `mutate` can say which one applies rather than answering
440
+ // every refusal with one message.
441
+ /** @internal */
442
+ export type ReadOnlyReason =
443
+ // Opened through a view-only share link.
444
+ | 'link'
445
+ // The reader's grant on this document does not carry write access.
446
+ | 'grant'
447
+ // The app acting on the reader's behalf may not edit this document.
448
+ | 'app'
449
+ // A change this copy holds was refused, so it shows something no other
450
+ // reader has. Editing on top of it builds more of the same, and `reload()`
451
+ // is what returns the copy to what the platform holds.
452
+ | 'unsaved';
453
+
454
+ const READ_ONLY_MESSAGES: Record<ReadOnlyReason, string> = {
455
+ link: 'Document is read-only: it was opened via a view-only share link.',
456
+ grant: 'Document is read-only: you have view-only access to it.',
457
+ app: 'Document is read-only: this app is not permitted to edit it.',
458
+ unsaved:
459
+ 'Document is read-only: a change could not be saved, so this copy holds ' +
460
+ 'work the platform does not. Brass is replacing it with the platform copy.',
461
+ };
462
+
294
463
  export interface DocumentOptions {
295
464
  // Root key the Document wraps. Defaults to `"entities"` to match the
296
465
  // wire protocol's top-level shape.
@@ -307,6 +476,10 @@ export interface DocumentOptions {
307
476
  // True when opened at a historical version. A read-only Document
308
477
  // throws from `mutate` and carries no live WS transport.
309
478
  readOnly?: boolean;
479
+ // Why `readOnly` was set, which picks the message `mutate` throws.
480
+ // Defaults to the view-only link.
481
+ /** @internal */
482
+ readOnlyReason?: ReadOnlyReason;
310
483
  // The historical version materialized at, when opened via `{ version
311
484
  // }`; `null` for a live (head) open. The server's *effective* version,
312
485
  // which may round down when compaction has TTL'd the exact op.
@@ -361,13 +534,15 @@ export class Document {
361
534
  // leaves this `false`.
362
535
  private readonly _isFrozen: boolean;
363
536
  // Forced read-only on a LIVE head open (distinct from `_isFrozen`, which
364
- // is a version-pinned open with no live channel). The anonymous
365
- // link-guest open sets this: the Document keeps a live WS subscription
366
- // and applies incoming remote ops, but local `mutate` is rejected, so a
367
- // guest sees edits stream in while being unable to write.
368
- private readonly _forcedReadOnly: boolean;
369
- private readonly _root: LoroMap;
370
- private readonly _entities: EntityProxy;
537
+ // is a version-pinned open with no live channel): the Document keeps a
538
+ // live WS subscription and applies incoming remote ops, but local
539
+ // `mutate` is rejected, so the reader sees edits stream in while being
540
+ // unable to write. Set at open from the join's `can_edit` (a viewer's
541
+ // grant, a view-only link), and latched on afterwards by
542
+ // `_markReadOnly` when the server refuses a write on the same grounds.
543
+ private _forcedReadOnly: boolean;
544
+ // Which refusal `_forcedReadOnly` stands for, so `mutate` names it.
545
+ private _readOnlyReason: ReadOnlyReason;
371
546
  // The UndoManager is constructed eagerly so the first call to
372
547
  // `doc.mutate(...)` lands in the undo stack. Lazy construction
373
548
  // (deferred to first `doc.undo()`) would miss every commit made
@@ -381,7 +556,9 @@ export class Document {
381
556
  // app reconciles by value and reads `origin.source` only when it has a
382
557
  // non-render reason to know the cause. A throwing subscriber is swallowed
383
558
  // so it cannot strand the others or the WS router.
384
- private readonly _subs = createEmitter<unknown>({ onError: () => {} });
559
+ private readonly _subs = createEmitter<SyntheticSubscribeEvent>({
560
+ onError: () => {},
561
+ });
385
562
  // Subscribers registered via `subscribeUndoState`. Fired after
386
563
  // every underlying Loro event when `canUndo()` or `canRedo()`
387
564
  // changed value vs the last fire, so UI bound to undo/redo
@@ -532,18 +709,32 @@ export class Document {
532
709
  // Live = neither closed nor parked. This is the public-surface gate;
533
710
  // internal ingress (remote ops, status emits) deliberately checks
534
711
  // `_closed` alone, because a parked doc keeps applying remote state.
535
- private get _isLive(): boolean {
712
+ // Readable from the SDK's own layers (the client's open-document set
713
+ // gates on it) rather than only from inside this class.
714
+ /** @internal */
715
+ get _isLive(): boolean {
536
716
  return !this._closed && !this._suspended;
537
717
  }
538
718
 
539
719
  private _assertOpen(): void {
540
720
  if (!this._isLive) throw new Error('Document is closed');
541
721
  }
722
+
723
+ // `close()` releases the CRDT state, so a read after it reaches released
724
+ // memory and fails with whatever the engine says about that. Reads are gated
725
+ // on this rather than on `_assertOpen` because a parked Document still holds
726
+ // its state and answers from it.
727
+ private _assertNotClosed(): void {
728
+ if (this._closed) throw new Error('Document is closed');
729
+ }
542
730
  // Server-pushed lifecycle signals: `_gone` is fire-once-then-cache
543
731
  // for `doc_gone` eviction; `_workStatus` is latest-retained,
544
732
  // re-emit-per-phase for `doc_status`.
545
733
  private readonly _gone = new GoneSignal();
546
734
  private readonly _workStatus = new WorkStatusSignal();
735
+ // Inbound work ended without delivering content, so this doc holds
736
+ // nothing and stays locked. See `_emitStatus` for how it is derived.
737
+ private _loadFailed = false;
547
738
  // Drive sync state, latest-retained. `null` until the open factory
548
739
  // seeds it (drive-linked docs only) and on docs with no drive link.
549
740
  private readonly _driveStatus = new DriveStatusSignal();
@@ -552,7 +743,11 @@ export class Document {
552
743
  // on, and on the offline / test path where there is no transport.
553
744
  presence: import('./presence.js').PresenceClient | null = null;
554
745
 
555
- constructor(loroDoc: LoroDoc, options?: DocumentOptions) {
746
+ // A Document owns the LoroDoc it wraps, so it is built through the factories
747
+ // below (or `documentFromEngine`, for in-repo tooling that brings its own).
748
+ // Owning construction is what lets this class declare an engine-neutral
749
+ // surface: app code holds a Document, never the doc underneath it.
750
+ private constructor(loroDoc: LoroDoc, options?: DocumentOptions) {
556
751
  this._doc = loroDoc;
557
752
  this._rootKey = options?.rootKey ?? 'entities';
558
753
  this._docId = options?.docId ?? null;
@@ -574,12 +769,15 @@ export class Document {
574
769
  this._isFrozen = false;
575
770
  this._checkout = null;
576
771
  }
577
- // `readOnly` on a live (non-version) open is a forced read-only: the
578
- // guest link view. A version open is already frozen, so it does not
579
- // also need the forced flag.
772
+ // `readOnly` on a live (non-version) open is a forced read-only: a
773
+ // reader the server will not accept writes from, whether because their
774
+ // grant is view-only or because the link they came through is. A
775
+ // version open is already frozen, so it does not also need the flag.
580
776
  this._forcedReadOnly = options?.readOnly === true && initialVersion === null;
581
- this._root = loroDoc.getMap(this._rootKey);
582
- this._entities = wrapLoroMapProxy(this._root) as EntityProxy;
777
+ this._readOnlyReason = options?.readOnlyReason ?? 'link';
778
+ // Materialize the root container, so a document nothing has written to
779
+ // still carries its root in the shape a reader opens it with.
780
+ loroDoc.getMap(this._rootKey);
583
781
  this._undo = new UndoController(this._doc, options?.undo);
584
782
  }
585
783
 
@@ -604,6 +802,15 @@ export class Document {
604
802
  | { kind: 'list'; key: string; handle: ListHandle<unknown> }
605
803
  >();
606
804
 
805
+ // Names the pages this Document's writes may occupy. A page is one value,
806
+ // so two writers appending to the same page container would resolve
807
+ // last-writer-wins and lose one writer's records; the token is what keeps
808
+ // their key spaces apart. Loro's peer id is already unique per open, which
809
+ // is exactly the scope wanted.
810
+ private get _pagingSessionToken(): string {
811
+ return this._doc.peerIdStr;
812
+ }
813
+
607
814
  // Get a handle for a top-level LoroMap at `key`. The container is
608
815
  // materialized in the underlying LoroDoc immediately (Loro's
609
816
  // `getMap` is "get or create"); mutations through the handle do
@@ -623,41 +830,32 @@ export class Document {
623
830
  // `instances` map present its page containers transparently as a
624
831
  // flat record map. It resolves the ACTIVE source at call time (not
625
832
  // the one captured at handle creation): a checkout rebinds the
626
- // handle's anchor to the preview doc, and the page count must come
627
- // from that same doc or keyed reads hash into the wrong page (and a
628
- // closure pinned to a preview doc would read a freed LoroDoc after
629
- // returnToHead).
833
+ // handle's anchor to the preview doc, and the page state must come
834
+ // from that same doc or a read takes the wrong path for the state it is
835
+ // looking at (and a closure pinned to a preview doc would read a freed
836
+ // LoroDoc after returnToHead).
630
837
  //
631
- // Only the record store's paged root (`instances`) may report a page
632
- // count. `readStorePageCount` reads the document-global
633
- // `meta.page_count`, so handing it to every root map would make the
634
- // sibling record-store roots (`meta`, `header`) and any app-owned
635
- // root map read as paged once `instances` pages, routing their
636
- // `get`/`has`/`mutate` through the page-hash path against a map that
637
- // holds no page containers (`get` returns undefined for present keys;
638
- // `mutate` writes a stray page container into a plain map). A non-
639
- // record-store key gets a null provider and stays unpaged.
640
- const handle = createRootMapHandle<unknown>(
641
- loroMap,
642
- this._handleOwner,
643
- key === RECORDS_KEY
644
- ? (): number =>
645
- readStorePageCount(
646
- this._checkout !== null ? this._checkout.previewDoc : this._doc,
647
- )
648
- : undefined,
649
- key === RECORDS_KEY
650
- ? (): number =>
651
- readStorePagingScheme(
652
- this._checkout !== null ? this._checkout.previewDoc : this._doc,
653
- )
654
- : undefined,
655
- );
838
+ // EVERY root map gets the providers, because a `meta` key of its own is
839
+ // what names a paged root: a root with no such key reads as unpaged, which
840
+ // is the answer `meta` and `header` want anyway. Naming one root here
841
+ // instead would make paging a property of a reserved key rather than of a
842
+ // collection, so a map an app owns could never be paged however large it
843
+ // grew.
844
+ const activeSource = (): LoroDoc =>
845
+ this._checkout !== null ? this._checkout.previewDoc : this._doc;
846
+ const handle = createRootMapHandle<unknown>(loroMap, this._handleOwner, {
847
+ isPaged: (): boolean => readRootPaged(activeSource(), key),
848
+ page: (): void => {
849
+ pageRoot(this._doc, key, this._handleOwner.commitMutate);
850
+ },
851
+ sessionToken: (): string => this._pagingSessionToken,
852
+ stateVersion: (): StateVersion => activeSource().frontiers(),
853
+ });
656
854
  this._rootHandles.set(cacheKey, { kind: 'map', key, handle });
657
855
  return handle as unknown as MapHandle<T>;
658
856
  }
659
857
 
660
- // Get a handle for a top-level LoroList at `key`.
858
+ // Get a handle for a top-level LoroMovableList at `key`.
661
859
  list<T = unknown>(key: string): ListHandle<T> {
662
860
  this._assertOpen();
663
861
  const cacheKey = `list:${key}`;
@@ -665,7 +863,7 @@ export class Document {
665
863
  if (existing) return existing.handle as unknown as ListHandle<T>;
666
864
  const source =
667
865
  this._checkout !== null ? this._checkout.previewDoc : this._doc;
668
- const loroList = source.getList(key);
866
+ const loroList = source.getMovableList(key);
669
867
  const handle = createRootListHandle<unknown>(loroList, this._handleOwner);
670
868
  this._rootHandles.set(cacheKey, { kind: 'list', key, handle });
671
869
  return handle as unknown as ListHandle<T>;
@@ -710,6 +908,9 @@ export class Document {
710
908
  private get _handleOwner(): HandleOwner {
711
909
  if (this._handleOwnerCache) return this._handleOwnerCache;
712
910
  this._handleOwnerCache = {
911
+ assertReadable: (): void => {
912
+ this._assertNotClosed();
913
+ },
713
914
  assertWritable: (): void => {
714
915
  this._assertOpen();
715
916
  if (this._checkout !== null) {
@@ -718,9 +919,19 @@ export class Document {
718
919
  'Call `doc.returnToHead()` to edit.',
719
920
  );
720
921
  }
922
+ if (this._versionBrowsing) {
923
+ throw new Error(
924
+ 'Document is read-only: the reader is browsing its versions. ' +
925
+ 'Close the version bar to edit.',
926
+ );
927
+ }
721
928
  if (this._forcedReadOnly) {
929
+ throw new Error(READ_ONLY_MESSAGES[this._readOnlyReason]);
930
+ }
931
+ if (this._loadFailed) {
722
932
  throw new Error(
723
- 'Document is read-only: it was opened via a view-only share link.',
933
+ 'Document is read-only: its content could not be prepared ' +
934
+ '(see `doc.status`). Reopen it to try again.',
724
935
  );
725
936
  }
726
937
  if (this._isLoadLocked()) {
@@ -760,11 +971,14 @@ export class Document {
760
971
  return this._branchId;
761
972
  }
762
973
 
763
- // True when the Document is currently showing a historical version
764
- // (a checkout): `mutate` throws and edit affordances should be
765
- // disabled. Reactive: flips on `_checkoutVersion(v)` /
766
- // `returnToHead()`. `subscribe` fires when this changes, so apps
767
- // bind it directly inside the subscribe handler.
974
+ // True when this reader cannot write to the document right now:
975
+ // `mutate` throws and edit affordances should be disabled. It covers a
976
+ // view-only grant, a view-only share link, a historical version showing
977
+ // (a checkout), and a document whose content is still loading or could
978
+ // not be loaded. Reactive in every one of those: `subscribe` fires when
979
+ // it changes, so an app binds it directly inside the subscribe handler
980
+ // and follows a grant the server withdraws mid-session as well as a
981
+ // checkout the reader entered.
768
982
  get readOnly(): boolean {
769
983
  // A doc that is still loading its first content (a drive-linked
770
984
  // stub hydrating on first open) is read-only until the bytes
@@ -774,23 +988,53 @@ export class Document {
774
988
  // `reloadAfterHydration`); until then mutation is rejected. This is
775
989
  // also what stops an app's "seed an empty doc" path from clobbering
776
990
  // a doc that only looks empty because it has not loaded yet.
991
+ // Browsing versions locks the document at the live head as well as at an
992
+ // earlier one. Stepping moves between the points history KEEPS, and an
993
+ // edit at the head makes no such point, so an edit made under the open
994
+ // bar puts the reader's own newest work somewhere the bar cannot travel
995
+ // to: Back then steps past it to the newest marker, and the way forward
996
+ // is a different number of presses from the way back. Reading history
997
+ // and writing it are separate modes, and the bar carries the Close that
998
+ // leaves this one.
777
999
  return (
778
- this._forcedReadOnly || this._checkout !== null || this._isLoadLocked()
1000
+ this._forcedReadOnly ||
1001
+ this._checkout !== null ||
1002
+ this._versionBrowsing ||
1003
+ this._isLoadLocked()
779
1004
  );
780
1005
  }
781
1006
 
782
1007
  // True when content-replacing work (hydration / extraction) is pending
783
1008
  // and editing must be locked so incoming bytes do not clobber local
784
- // ops. A pending PUSH is outbound (it writes the doc out to the drive,
785
- // nothing replaces local content), so the server marks it
1009
+ // ops, and true again once that work ends without content
1010
+ // (`_loadFailed`): a doc nothing could prepare is empty, and an edit
1011
+ // there writes ops into a document the app is about to be told could not
1012
+ // be opened. A pending PUSH is outbound (it writes the doc out to the
1013
+ // drive, nothing replaces local content), so the server marks it
786
1014
  // `blocking: false` and the doc stays editable while the importer
787
1015
  // runs. An older server omits `blocking`; absence is treated as
788
1016
  // blocking so hydration stays locked.
789
1017
  private _isLoadLocked(): boolean {
1018
+ if (this._loadFailed) return true;
790
1019
  const s = this._workStatus.status;
791
1020
  return s?.pending === true && s.blocking !== false;
792
1021
  }
793
1022
 
1023
+ /**
1024
+ * Whether inbound work on this document ended without content. The SDK's
1025
+ * session reads it to report the open as failed rather than binding an
1026
+ * empty document as ready.
1027
+ * @internal
1028
+ */
1029
+ get _contentLoadFailed(): boolean {
1030
+ return this._loadFailed;
1031
+ }
1032
+
1033
+ /** @internal The `doc_gone` reason this Document has seen, or `null`. */
1034
+ get _goneReason(): DocumentGoneReason | null {
1035
+ return this._gone.reason;
1036
+ }
1037
+
794
1038
  /**
795
1039
  * The historical version this Document shows, or `null` for a live
796
1040
  * (head) open. Reactive alongside `readOnly`. The number is the
@@ -857,6 +1101,16 @@ export class Document {
857
1101
  return new Document(new LoroDoc(), options);
858
1102
  }
859
1103
 
1104
+ /**
1105
+ * Wrap a LoroDoc the caller already holds. `documentFromEngine` is the typed
1106
+ * entry point; this seam stays `unknown` so `documents.ts` declares an
1107
+ * engine-neutral surface.
1108
+ * @internal
1109
+ */
1110
+ static _fromEngine(engine: unknown, options?: DocumentOptions): Document {
1111
+ return new Document(engine as LoroDoc, options);
1112
+ }
1113
+
860
1114
  // Apply a remote op blob produced by another peer (delivered via the
861
1115
  // WS `doc_op` server frame). Idempotent: Loro's import
862
1116
  // path dedupes by lamport, so re-applying a self-originated op is
@@ -883,8 +1137,8 @@ export class Document {
883
1137
 
884
1138
  // Apply a snapshot blob over the top of an existing Document.
885
1139
  // Internal: the SDK calls this when the server delivers a fresh
886
- // snapshot mid-session (e.g. after a compaction cycle). Persistence
887
- // is server-authoritative, so there is no app-facing snapshot import.
1140
+ // snapshot mid-session. Persistence is server-authoritative: the server
1141
+ // decides when a fresh snapshot supersedes the local history.
888
1142
  /** @internal */
889
1143
  _applySnapshot(snapshot: Uint8Array): void {
890
1144
  // See `applyRemoteOp`: refuse the import on a closed (freed) doc so a late
@@ -994,9 +1248,9 @@ export class Document {
994
1248
  // intentional escape hatch: `Brass.openDocumentById` wires the hook;
995
1249
  // `Document.create()` and direct construction stay
996
1250
  // hook-free so the class is usable without taking the WS dependency.
997
- // `E` defaults to `EntityProxy` (the untyped Record<string, unknown>)
998
- // for backwards-compat with existing callers. Apps with a typed
999
- // entity shape pass it as a type argument:
1251
+ // `E` defaults to `EntityProxy` (the untyped Record<string, unknown>) so an
1252
+ // app that has not described its entity shape still gets a working mutate.
1253
+ // Apps with a typed entity shape pass it as a type argument:
1000
1254
  //
1001
1255
  // doc.mutate<MyEntity>((d) => {
1002
1256
  // d.entities[id] = myTypedEntity;
@@ -1006,50 +1260,18 @@ export class Document {
1006
1260
  // regardless of `E`; the generic exists purely to widen the type at
1007
1261
  // the assignment site so closed interfaces don't need an
1008
1262
  // `as Record<string, unknown>` cast.
1263
+ //
1264
+ // `d.entities` is the root map's handle under a plain-object surface, so
1265
+ // the two reach one collection: a record a paged store holds reads and
1266
+ // edits here exactly as it does through `doc.map(rootKey)`, and a write
1267
+ // large enough to cross the paging threshold takes the paged shape from
1268
+ // either.
1009
1269
  mutate<E extends object = EntityProxy>(
1010
1270
  fn: (d: DocumentMutator<E>) => void,
1011
1271
  ): void {
1012
- this._assertOpen();
1013
- if (this._checkout !== null) {
1014
- throw new Error(
1015
- 'Document is read-only: it is showing a historical version. ' +
1016
- 'Call `doc.returnToHead()` to edit.',
1017
- );
1018
- }
1019
- // A view-only share-link (guest) open is a live head held read-only:
1020
- // remote ops still stream in, but a local edit must be refused. The
1021
- // `_checkout` guard above only covers the version-pinned case, so this
1022
- // is a separate branch.
1023
- if (this._forcedReadOnly) {
1024
- throw new Error(
1025
- 'Document is read-only: it was opened via a view-only share link.',
1026
- );
1027
- }
1028
- if (this._isLoadLocked()) {
1029
- throw new Error(
1030
- 'Document is still loading its content; wait for the load to ' +
1031
- 'finish before editing (see `doc.onStatus`).',
1032
- );
1033
- }
1034
- this._ensureLocalUpdatesSubscribed();
1035
- // A fresh proxy for the call, because the batcher rides it: the
1036
- // long-lived `this._entities` must stay batcher-free for the reads
1037
- // that go through it outside a mutation.
1038
- const batcher = new MutateBatcher(() => {
1039
- this._doc.setNextCommitOrigin(MUTATE_COMMIT_ORIGIN);
1040
- this._doc.commit();
1041
- });
1042
- fn({
1043
- entities: wrapLoroMapProxy(this._root, batcher) as unknown as Record<
1044
- string,
1045
- E
1046
- >,
1272
+ this.map<E>(this._rootKey).mutate((entities) => {
1273
+ fn({ entities });
1047
1274
  });
1048
- // Stamp the commit so `subscribe`'s default path can identify
1049
- // and suppress the local-mutate echo. `setNextCommitOrigin`
1050
- // applies to the very next commit, and `commit()` consumes it.
1051
- this._doc.setNextCommitOrigin(MUTATE_COMMIT_ORIGIN);
1052
- this._doc.commit();
1053
1275
  }
1054
1276
 
1055
1277
  // Install / replace the local-op send hook; `null` detaches it.
@@ -1112,6 +1334,13 @@ export class Document {
1112
1334
  return this._checkout !== null;
1113
1335
  }
1114
1336
 
1337
+ /** @internal Whether this Document is pinned to the version it was opened
1338
+ * at, with no live channel behind it. Stepping and returning to the head
1339
+ * both throw on that shape, so the SDK's version bar offers neither. */
1340
+ get _isVersionPinned(): boolean {
1341
+ return this._isFrozen;
1342
+ }
1343
+
1115
1344
  // Switch the Document to a historical version, in place. @internal
1116
1345
  // (the hosted `history` command drives this). After the promise
1117
1346
  // resolves, `doc.readOnly` is `true`,
@@ -1162,6 +1391,11 @@ export class Document {
1162
1391
  for (const blob of tailOpBlobs) previewDoc.import(blob);
1163
1392
  const previewRoot = previewDoc.getMap(this._rootKey);
1164
1393
  const old = this._checkout;
1394
+ // Snapshot the handles' keys before the active-source pointer moves (see
1395
+ // `_snapshotRootHandleKeys`). Nothing between here and `_rebindRootHandles`
1396
+ // may return early, or the snapshot would outlive its swap and be diffed
1397
+ // against an unrelated later one.
1398
+ this._snapshotRootHandleKeys();
1165
1399
  this._checkout = {
1166
1400
  previewDoc,
1167
1401
  previewRoot,
@@ -1187,30 +1421,38 @@ export class Document {
1187
1421
  // ignore; already freed
1188
1422
  }
1189
1423
  }
1190
- this._fireSubscribers({ kind: 'checkout', version: effectiveVersion });
1424
+ this._fireSubscribers({ kind: 'history', version: effectiveVersion });
1191
1425
  this._maybeFireUndoState();
1192
1426
  // Preview every opened stream at the same point in time, so a
1193
1427
  // partitioned document's history shows a consistent historical state
1194
1428
  // across streams rather than a historical default over live streams.
1195
- await this._checkoutOpenStreams();
1429
+ await this._checkoutOpenStreams(generation);
1196
1430
  }
1197
1431
 
1198
1432
  // Re-materialize each opened non-default stream at its seq recorded by the
1199
1433
  // active preview's checkpoint. A stream with no recorded seq at this
1200
1434
  // version (opened after the checkpoint, or before any checkpoint recorded
1201
- // one) returns to its own head, so it shows its earliest-known rather than
1202
- // a mismatched live state. Best-effort per stream: a stream whose inner
1435
+ // one) has no historical state it can show, so it is held read-only for the
1436
+ // duration rather than left live and writable under a historical default. Best-effort per stream: a stream whose inner
1203
1437
  // has no checkout fetcher (a guest line, which has no token to fetch
1204
1438
  // at-version) stays live. Failures are swallowed so one stream cannot
1205
1439
  // break the doc-level preview.
1206
- private async _checkoutOpenStreams(): Promise<void> {
1440
+ private async _checkoutOpenStreams(generation: number): Promise<void> {
1207
1441
  const seqs = this._checkoutStreamSeqs ?? {};
1208
1442
  for (const [name, openPromise] of this._streamOpens) {
1209
1443
  try {
1210
1444
  const stream = await openPromise;
1445
+ // Every stream costs an await, so the reader can have stepped
1446
+ // somewhere else, or back to the live head, since the previous one.
1447
+ // The document's own swap is guarded the same way, and this loop
1448
+ // running on past it is what leaves a partitioned document showing
1449
+ // two moments at once: the document at the version the reader asked
1450
+ // for, and whichever streams a superseded run still reached at its
1451
+ // own.
1452
+ if (generation !== this._checkoutGeneration) return;
1211
1453
  const lineId = this._streamManifest?.get(name)?.line_id;
1212
1454
  const seq = lineId !== undefined ? seqs[lineId] : undefined;
1213
- await stream._previewAt(seq);
1455
+ await stream._previewAt(seq, true);
1214
1456
  } catch {
1215
1457
  // A stream that failed to open or preview is left as-is.
1216
1458
  }
@@ -1240,9 +1482,12 @@ export class Document {
1240
1482
  // Bump first, unconditionally: a checkout fetch may be in flight with
1241
1483
  // `_checkout` still null, and this is the signal that supersedes it when
1242
1484
  // it resolves.
1243
- this._checkoutGeneration += 1;
1485
+ const generation = (this._checkoutGeneration += 1);
1244
1486
  if (this._checkout === null) return;
1245
1487
  const old = this._checkout;
1488
+ // Symmetric with `_checkoutVersion`: snapshot before the active-source
1489
+ // pointer moves, and return early nowhere between here and the rebind.
1490
+ this._snapshotRootHandleKeys();
1246
1491
  this._checkout = null;
1247
1492
  // Re-point the app's handles back at the live doc and redraw them,
1248
1493
  // symmetrically with the checkout path, so the model returns to the live
@@ -1266,13 +1511,33 @@ export class Document {
1266
1511
  this._checkoutStreamSeqs = null;
1267
1512
  for (const openPromise of this._streamOpens.values()) {
1268
1513
  void openPromise
1269
- .then((stream) => stream._previewAt(undefined))
1514
+ .then((stream) => {
1515
+ // Each stream is reached a turn or more later, so a checkout the
1516
+ // reader started in the meantime owns the document by now and this
1517
+ // return would drag its streams back to the live head under it.
1518
+ if (generation !== this._checkoutGeneration) return;
1519
+ return stream._previewAt(undefined);
1520
+ })
1270
1521
  .catch(() => {
1271
1522
  // A stream that failed to open has nothing to return.
1272
1523
  });
1273
1524
  }
1274
1525
  }
1275
1526
 
1527
+ // Capture every root map handle's key set for the resync's removed-key diff,
1528
+ // while the handles still describe the outgoing source. This is a phase of
1529
+ // its own, ahead of `_rebindRootHandles`, because a handle resolves its
1530
+ // container from its anchor but its paging state from this Document's active
1531
+ // source: both must still name the outgoing source when the keys are read,
1532
+ // and the caller moves the active-source pointer between the two calls.
1533
+ // Callers must not return early in that window; a snapshot left unconsumed
1534
+ // would be diffed against a later, unrelated swap.
1535
+ private _snapshotRootHandleKeys(): void {
1536
+ for (const entry of this._rootHandles.values()) {
1537
+ if (entry.kind === 'map') snapshotRootMapKeys(entry.handle);
1538
+ }
1539
+ }
1540
+
1276
1541
  // Re-point every cached root handle at the corresponding container in
1277
1542
  // `source` (the live doc or a checkout preview) and resync its subscribers.
1278
1543
  // The app holds these same handle objects, so this is what makes a version
@@ -1289,7 +1554,7 @@ export class Document {
1289
1554
  if (entry.kind === 'map') {
1290
1555
  rebindRootMapAnchor(entry.handle, source.getMap(entry.key));
1291
1556
  } else {
1292
- rebindRootListAnchor(entry.handle, source.getList(entry.key));
1557
+ rebindRootListAnchor(entry.handle, source.getMovableList(entry.key));
1293
1558
  }
1294
1559
  }
1295
1560
  for (const entry of this._rootHandles.values()) {
@@ -1419,11 +1684,8 @@ export class Document {
1419
1684
  this._sourceBlobFetcher = fetcher;
1420
1685
  }
1421
1686
 
1422
- // Publish this document's current state back to its drive source.
1423
- // Lets an app code one line on save / on a commit button to make its
1424
- // edits visible to people working outside Brass on the same source:
1425
- //
1426
- // await doc.pushToDrive({ message: 'Wall move' });
1687
+ // Publish this document's current state back to its drive source,
1688
+ // driving the hosted toolbar's publish control.
1427
1689
  //
1428
1690
  // The platform publishes the doc's current state, overwriting the
1429
1691
  // source (unknown drive fields preserved), and marks its own write so
@@ -1431,6 +1693,7 @@ export class Document {
1431
1693
  // concurrent drive-side edits. Throws on auth / link / write
1432
1694
  // failures; resolves with the new external ref and the published
1433
1695
  // entity count.
1696
+ /** @internal */
1434
1697
  async pushToDrive(
1435
1698
  input?: PushToDriveInput,
1436
1699
  ): Promise<PushToDriveResult> {
@@ -1447,10 +1710,9 @@ export class Document {
1447
1710
  }
1448
1711
 
1449
1712
  // Resolve a `behind` / `diverged` drive-linked doc by opening the
1450
- // hosted resolution surface, so an app can offer pull (take the source)
1451
- // / keep both alongside the publish-over that `pushToDrive` does:
1452
- //
1453
- // if (doc.driveStatus?.state === 'diverged') await doc.resolveDrive();
1713
+ // hosted resolution surface, which offers pull (take the source) / keep
1714
+ // both alongside the publish-over that `pushToDrive` does. Driven by the
1715
+ // hosted toolbar.
1454
1716
  //
1455
1717
  // Unlike `pushToDrive`, the resolution can mint a NEW mirror doc that
1456
1718
  // takes over the drive link (take-source trashes this doc, keep-both
@@ -1460,6 +1722,7 @@ export class Document {
1460
1722
  // returned `newDocId` lets a multi-doc app do its own switch. Resolves
1461
1723
  // with the outcome, or `null` when the user dismissed the dialog.
1462
1724
  // Throws when there is no doc identity or no resolver is attached.
1725
+ /** @internal */
1463
1726
  async resolveDrive(): Promise<DriveResolveResult | null> {
1464
1727
  this._assertOpen();
1465
1728
  if (this._docId === null) {
@@ -1517,6 +1780,9 @@ export class Document {
1517
1780
  private _streamRuntime: StreamRuntime | null = null;
1518
1781
  private _streamManifest: Map<string, DocumentStreamInfo> | null = null;
1519
1782
  private readonly _streamOpens = new Map<string, Promise<DocumentStream>>();
1783
+ // The subset of `_streamOpens` that has resolved, so a park reaches them
1784
+ // without waiting on a microtask. Kept in step with the map above.
1785
+ private readonly _openStreams = new Map<string, DocumentStream>();
1520
1786
  private _ownStream: DocumentStream | null = null;
1521
1787
  // Which stream this Document's own handles read. An open names a stream
1522
1788
  // and the usual one has a default, so `default` is a name here rather
@@ -1602,16 +1868,33 @@ export class Document {
1602
1868
  // the same point in time rather than showing it live under a
1603
1869
  // historical default.
1604
1870
  if (this._checkout !== null) {
1605
- await stream._previewAt(this._checkoutStreamSeqs?.[info.line_id]);
1871
+ await stream._previewAt(this._checkoutStreamSeqs?.[info.line_id], true);
1606
1872
  }
1607
1873
  return stream;
1608
1874
  })();
1609
1875
  this._streamOpens.set(name, open);
1610
- // A failed open (network, deleted stream) must not poison the cache:
1611
- // drop it so the next call retries.
1612
- open.catch(() => {
1613
- if (this._streamOpens.get(name) === open) this._streamOpens.delete(name);
1614
- });
1876
+ // Hold the RESOLVED stream too. A park has to reach the streams
1877
+ // synchronously (an app's `close()` returns before any microtask runs, and
1878
+ // a remote op landing in between would still call the app back), and a
1879
+ // promise cannot be read that way. An open still in flight when the park
1880
+ // happens resolves into a parked document, so it is parked on arrival.
1881
+ void open.then(
1882
+ (stream) => {
1883
+ if (this._streamOpens.get(name) !== open) return;
1884
+ this._openStreams.set(name, stream);
1885
+ // `savingCount` counts this stream from here on, so an indicator bound
1886
+ // to the document has to hear the stream's writes land.
1887
+ stream._inner._observeSavingCount(() => {
1888
+ this._fireSubscribers({ kind: 'saving-count' });
1889
+ });
1890
+ if (this._suspended) stream._suspend();
1891
+ },
1892
+ () => {
1893
+ // A failed open (network, deleted stream) must not poison the cache:
1894
+ // drop it so the next call retries.
1895
+ if (this._streamOpens.get(name) === open) this._streamOpens.delete(name);
1896
+ },
1897
+ );
1615
1898
  return open;
1616
1899
  }
1617
1900
 
@@ -1683,15 +1966,45 @@ export class Document {
1683
1966
  // Close every opened stream's own transport + state. Streams opened
1684
1967
  // while this Document was live do not outlive it.
1685
1968
  private _closeStreams(): void {
1969
+ // A stream that has already opened closes IN THIS CALL, for the reason the
1970
+ // park reaches them through this map: `close()` returns to the app before
1971
+ // any microtask runs, so a stream closed on a `.then` spends the rest of
1972
+ // that turn taking writes and calling the app back while `doc.mutate` on
1973
+ // the same document already refuses. A write in that window reaches a
1974
+ // transport about to be torn down and is never persisted.
1975
+ for (const stream of this._openStreams.values()) {
1976
+ stream._inner._observeSavingCount(null);
1977
+ stream._close();
1978
+ }
1979
+ // An open still in flight has no Document to close yet, so it closes on
1980
+ // arrival. A stream reached both ways is closed twice, which is a no-op.
1686
1981
  for (const open of this._streamOpens.values()) {
1687
1982
  void open.then((s) => s._close()).catch(() => {
1688
1983
  // A failed open has nothing to close.
1689
1984
  });
1690
1985
  }
1691
1986
  this._streamOpens.clear();
1987
+ this._openStreams.clear();
1692
1988
  this._streamManifest = null;
1693
1989
  }
1694
1990
 
1991
+ // Park / unpark the streams opened off this document, alongside the
1992
+ // document's own park.
1993
+ //
1994
+ // A non-default stream is its own Document, which nothing else suspends: it
1995
+ // keeps its transport, so an app holding `await doc.stream(name)` across
1996
+ // `doc.close()` would go on receiving remote-op callbacks and could still
1997
+ // write through the stream, while `doc.mutate` correctly throws. That is the
1998
+ // cross-session callback a park exists to stop, plus a write path around the
1999
+ // closed check. The default stream's inner IS this document, which owns its
2000
+ // own lifecycle, so `_ownsInner` gates this exactly as it gates the close.
2001
+ private _setStreamsSuspended(suspended: boolean): void {
2002
+ for (const stream of this._openStreams.values()) {
2003
+ if (suspended) stream._suspend();
2004
+ else stream._resume();
2005
+ }
2006
+ }
2007
+
1695
2008
  // Seal a deliberate checkpoint on this document's version-
1696
2009
  // history timeline. Compaction snapshots continue to appear as
1697
2010
  // no-action fallback markers, so an app that never calls this still
@@ -1767,6 +2080,14 @@ export class Document {
1767
2080
  else console.error('[Brass]', err);
1768
2081
  }
1769
2082
 
2083
+ /** @internal Report a failure from the SDK's own chrome (the version
2084
+ * bar's intents) through the same sink the built-in commands use, so a
2085
+ * refused checkout reaches the app's `onError` rather than being dropped
2086
+ * on the way back to a surface that only shows the reader a state. */
2087
+ _reportChromeError(err: Error): void {
2088
+ this._reportError(err);
2089
+ }
2090
+
1770
2091
  // Fire all `subscribe` listeners with a synthetic event. Used for
1771
2092
  // metadata-only changes (checkout, return-to-head, branch switch,
1772
2093
  // branches list update, saving count change, commands change)
@@ -1775,21 +2096,127 @@ export class Document {
1775
2096
  // call `toJSON()` in the handler; the discriminant is there for
1776
2097
  // the rare consumer that wants to branch on the reason.
1777
2098
  private _fireSubscribers(event: SyntheticSubscribeEvent): void {
2099
+ // Every change to the outstanding-op map reports itself here, which
2100
+ // makes this the one place the unload guard has to hear about un-acked
2101
+ // work (`close()` releases the doc outright).
2102
+ if (event.kind === 'saving-count') {
2103
+ // Own ops rather than `savingCount`: a stream's inner Document registers
2104
+ // itself, so the registry's total already carries one entry per stream.
2105
+ trackUnsavedWork(this, this._outstandingByClientId.size);
2106
+ this._savingCountObserver?.();
2107
+ }
1778
2108
  this._ensureLoroSubscribed();
1779
2109
  this._subs.emit(event);
1780
2110
  }
1781
2111
 
1782
- // Saving indicator: number of locally-issued ops not yet acked by
1783
- // the server. Reactive: `subscribe` fires when this count changes
1784
- // (an op is sent or an `op_ack` lands). Apps bind directly inside
1785
- // the subscribe handler:
2112
+ // Told when this Document's outstanding-op count moves, so a parent
2113
+ // Document's `savingCount` covers the streams it has open and its
2114
+ // subscribers fire when a stream's writes land. One observer, because a
2115
+ // stream's inner Document is opened by, and belongs to, exactly one parent.
2116
+ private _savingCountObserver: (() => void) | null = null;
2117
+
2118
+ /** @internal */
2119
+ _observeSavingCount(observer: (() => void) | null): void {
2120
+ this._savingCountObserver = observer;
2121
+ }
2122
+
2123
+ // Saving indicator: number of locally-issued ops not yet acked by the
2124
+ // server, across this document's own state and every stream it has open. A
2125
+ // partitioned document keeps its bulk on its streams, so a count that named
2126
+ // the default stream alone reports "All changes saved" over a stream still
2127
+ // writing. Reactive: `subscribe` fires when this count changes (an op is
2128
+ // sent or an `op_ack` lands, on any of them). Apps bind directly inside the
2129
+ // subscribe handler:
1786
2130
  //
1787
2131
  // doc.subscribe(() => {
1788
2132
  // savingEl.textContent =
1789
2133
  // doc.savingCount === 0 ? 'All changes saved'
1790
2134
  // : `Saving ${doc.savingCount} change${doc.savingCount === 1 ? '' : 's'}...`;
1791
2135
  // });
2136
+ //
2137
+ // Only this count aggregates. `readOnly` and `atCapacity` gate `doc.mutate`,
2138
+ // which writes this document's own state, so folding another stream's
2139
+ // refusal into them would close an editor over a stream taking writes;
2140
+ // `stream.readOnly` / `stream.atCapacity` answer for one stream.
2141
+ // Supplied by the doc transport, which holds the sync state this reports.
2142
+ // Absent until a transport is wired (a read-only or detached Document),
2143
+ // where the honest answer is that nothing is outstanding.
2144
+ private _diagnosticsProvider: (() => DocSyncDiagnostics) | null = null;
2145
+
2146
+ /** @internal */
2147
+ _setDiagnosticsProvider(provider: () => DocSyncDiagnostics): void {
2148
+ this._diagnosticsProvider = provider;
2149
+ }
2150
+
2151
+ /**
2152
+ * How this document's sync is going, as the client sees it: how much is
2153
+ * outstanding, how long it has been, over which transport, across how many
2154
+ * reconnects, and under which correlator, which is what separates a
2155
+ * connection that is answering nothing from one that is merely behind.
2156
+ *
2157
+ * @internal Telemetry for Brass's own surfaces. An app reads `savingCount`
2158
+ * to know a save is outstanding, and the SDK resolves a stuck one itself.
2159
+ */
2160
+ diagnostics(): DocSyncDiagnostics {
2161
+ const own: DocSyncDiagnostics = this._diagnosticsProvider?.() ?? {
2162
+ unackedOps: 0,
2163
+ oldestUnackedMs: 0,
2164
+ oldestClientOpId: null,
2165
+ reconnects: 0,
2166
+ lastAckAgeMs: null,
2167
+ inFlightBytes: 0,
2168
+ httpOpsInFlight: 0,
2169
+ healRounds: 0,
2170
+ hidden: false,
2171
+ };
2172
+ // Cover the streams this document opened, the way `savingCount` does: a
2173
+ // reader watching a save waits on the document, and which line an op was
2174
+ // written to is not something they can act on. The work counters add
2175
+ // up and the waiting ones take the longest wait, so the answer is about
2176
+ // the oldest edit anywhere in the document.
2177
+ //
2178
+ // The connection counters are the parent's alone rather than a fold,
2179
+ // because every line rides one socket, so reconnects and page
2180
+ // visibility are one fact about the client and not one per line.
2181
+ for (const stream of this._openStreams.values()) {
2182
+ const line = stream._inner.diagnostics();
2183
+ own.unackedOps += line.unackedOps;
2184
+ own.inFlightBytes += line.inFlightBytes;
2185
+ own.httpOpsInFlight += line.httpOpsInFlight;
2186
+ own.healRounds = Math.max(own.healRounds, line.healRounds);
2187
+ // Take the stream's op when it waited longer, and also when this
2188
+ // document has none of its own: a strict comparison against a zero
2189
+ // wait leaves the fold reporting outstanding work while naming no op
2190
+ // to go looking for, which is the field the rest hangs off.
2191
+ if (
2192
+ line.oldestClientOpId !== null &&
2193
+ (own.oldestClientOpId === null ||
2194
+ line.oldestUnackedMs > own.oldestUnackedMs)
2195
+ ) {
2196
+ own.oldestUnackedMs = line.oldestUnackedMs;
2197
+ own.oldestClientOpId = line.oldestClientOpId;
2198
+ }
2199
+ }
2200
+ return own;
2201
+ }
2202
+
1792
2203
  get savingCount(): number {
2204
+ let total = this._ownSavingCount;
2205
+ // `_openStreams` holds the streams that opened their own line. The stream
2206
+ // this Document IS wraps `this`, and is not in it, so nothing is counted
2207
+ // twice.
2208
+ for (const stream of this._openStreams.values()) {
2209
+ total += stream._inner.savingCount;
2210
+ }
2211
+ return total;
2212
+ }
2213
+
2214
+ // The ops issued against THIS line, which is what a stream reports for
2215
+ // itself: `doc.stream('default')` wraps this Document, so a default stream
2216
+ // reading the aggregate would report a sibling stream's writes as its own
2217
+ // and stand at "saving" while its own line has nothing outstanding.
2218
+ /** @internal */
2219
+ get _ownSavingCount(): number {
1793
2220
  return this._outstandingByClientId.size;
1794
2221
  }
1795
2222
 
@@ -1806,9 +2233,54 @@ export class Document {
1806
2233
  // once every chunk of the push has drained.
1807
2234
  this._resyncPushClientOpIds.delete(clientOpId);
1808
2235
  if (wasTracked) this._fireSubscribers({ kind: 'saving-count' });
2236
+ // An ack is proof the branch is taking writes again, which is the only
2237
+ // thing that can clear the capacity latch. A full branch refuses every
2238
+ // op, so no ack can arrive while the refusal stands; that is what makes
2239
+ // this evidence rather than a guess, and what keeps it from clearing a
2240
+ // state the next write would re-enter.
2241
+ if (this._atCapacity) {
2242
+ this._atCapacity = false;
2243
+ this._fireSubscribers({ kind: 'capacity' });
2244
+ }
1809
2245
  return wasTracked;
1810
2246
  }
1811
2247
 
2248
+ // Internal: turn this Document read-only because the server refused a
2249
+ // write on the reader's authority rather than on the op. It lands on
2250
+ // `readOnly` because that is the gate an app already renders, so a grant
2251
+ // withdrawn mid-session closes the editor with no new signal to learn.
2252
+ // One-way: the socket resolves write authority when it subscribes, so a
2253
+ // grant restored afterwards is refused by the server too until the
2254
+ // connection recycles, and a reopen is what picks the wider grant up.
2255
+ /** @internal */
2256
+ _markReadOnly(reason: ReadOnlyReason): void {
2257
+ if (this._forcedReadOnly) return;
2258
+ this._forcedReadOnly = true;
2259
+ this._readOnlyReason = reason;
2260
+ this._fireSubscribers({ kind: 'commands' });
2261
+ this._maybeFireUndoState();
2262
+ }
2263
+
2264
+ /**
2265
+ * Return this copy to what the platform holds, discarding anything it
2266
+ * refused. A refused change stays drawn on the copy that made it and
2267
+ * nowhere else, and no local operation removes it (a CRDT merges rather
2268
+ * than replaces, and an undo inverts an operation instead of discarding
2269
+ * it), so the only state holding exactly what the platform has is one
2270
+ * built from it.
2271
+ *
2272
+ * The session closes this document and opens the same id again, so a
2273
+ * `session.subscribe` handler rebinds the way it does on any document
2274
+ * switch.
2275
+ *
2276
+ * @internal The SDK drives this itself on the one refusal that reaches it,
2277
+ * telling the reader first, because an app that renders nothing for it is
2278
+ * exactly the case that must not leave someone on a diverged copy.
2279
+ */
2280
+ reload(): void {
2281
+ this._markGone('moved');
2282
+ }
2283
+
1812
2284
  // Internal: forget an outstanding op the server TERMINALLY rejected (an
1813
2285
  // `op_rejected` frame: the op can never be accepted, e.g. it exceeded the
1814
2286
  // server's op-blob cap), as opposed to `resolveOutstandingOp`'s success. The
@@ -1817,16 +2289,77 @@ export class Document {
1817
2289
  // app can react (the optimistic local commit stays applied but never
1818
2290
  // persists, so the app's cue is to reload). Returns true if it was tracked.
1819
2291
  /** @internal */
1820
- _failOutstandingOp(clientOpId: string, message: string): boolean {
2292
+ _failOutstandingOp(
2293
+ clientOpId: string,
2294
+ message: string,
2295
+ // Whether the app hears about this one. A refusal that describes the
2296
+ // DOCUMENT answers every op in flight at once, so reporting per op tells
2297
+ // the reader the same thing as many times as they happened to have
2298
+ // unacked writes, which on a bulk write is dozens. The caller reports it
2299
+ // once instead; the op still leaves the tracker either way, because
2300
+ // `savingCount` has to fall whether or not anyone is told.
2301
+ opts: { report?: boolean } = {},
2302
+ ): boolean {
1821
2303
  const wasTracked = this._outstandingByClientId.delete(clientOpId);
1822
2304
  this._resyncPushClientOpIds.delete(clientOpId);
1823
2305
  if (wasTracked) {
1824
2306
  this._fireSubscribers({ kind: 'saving-count' });
1825
- this._reportError(new Error(message));
2307
+ if (opts.report !== false) this._reportError(new Error(message));
1826
2308
  }
1827
2309
  return wasTracked;
1828
2310
  }
1829
2311
 
2312
+ // Internal: whether this op is one the durability handshake pushed, rather
2313
+ // than one the app wrote. The transport reads it to tell a refusal the heal
2314
+ // itself provoked from one that arrived on its own, which is what keeps a
2315
+ // refused heal chunk from re-arming the heal that sent it.
2316
+ /** @internal */
2317
+ _isResyncPushOp(clientOpId: string): boolean {
2318
+ return this._resyncPushClientOpIds.has(clientOpId);
2319
+ }
2320
+
2321
+ private _atCapacity = false;
2322
+
2323
+ /**
2324
+ * Whether the document has stopped accepting new changes because it has
2325
+ * reached the largest size the platform can keep.
2326
+ *
2327
+ * Read from the server's refusal of a write rather than predicted locally,
2328
+ * so it turns true at the first edit that does not land, and back to false
2329
+ * on the first edit that does. Both directions are evidence: a full branch
2330
+ * refuses every op, so an ack cannot arrive while the refusal stands.
2331
+ *
2332
+ * Edits keep applying to the local copy while it is true, which is what
2333
+ * lets the app show the reader what they did alongside the fact that it is
2334
+ * not saved; the copy is discarded on reload.
2335
+ *
2336
+ * An app reads this to stop offering work that would only be refused (a
2337
+ * bulk import, a generate pass) and to say so once, instead of surfacing one
2338
+ * error per rejected op.
2339
+ */
2340
+ get atCapacity(): boolean {
2341
+ return this._atCapacity;
2342
+ }
2343
+
2344
+ // Internal: record the capacity verdict from a server refusal. Idempotent.
2345
+ // Cleared again by `resolveOutstandingOp`, so a branch whose compaction
2346
+ // seals it back under the ceiling reaches the open reader through the next
2347
+ // write that lands rather than only through a reload.
2348
+ /**
2349
+ * Returns whether this call is the one that flipped it, which is what lets
2350
+ * the transport report the refusal exactly once. Every op in flight draws
2351
+ * the same refusal, and a bulk write has as many of those as it has unacked
2352
+ * writes, so the flip is the only moment worth telling the app about.
2353
+ *
2354
+ * @internal
2355
+ */
2356
+ _markAtCapacity(): boolean {
2357
+ if (this._atCapacity) return false;
2358
+ this._atCapacity = true;
2359
+ this._fireSubscribers({ kind: 'capacity' });
2360
+ return true;
2361
+ }
2362
+
1830
2363
  private _ensureLocalUpdatesSubscribed(): void {
1831
2364
  if (this._unsubLocal) return;
1832
2365
  if (!this._localOpHook) return;
@@ -1843,16 +2376,45 @@ export class Document {
1843
2376
  // actually flushes (the WS client may queue the frame).
1844
2377
  const copy = new Uint8Array(bytes.byteLength);
1845
2378
  copy.set(bytes);
1846
- const clientOpId = hook(copy);
1847
- this._outstandingByClientId.set(clientOpId, copy);
1848
- // The local mutate has already fired `subscribe` (the Loro
1849
- // event), but `savingCount` just incremented; fire one more
1850
- // synthetic event so a subscribe handler bound on `savingCount`
1851
- // sees the change.
1852
- this._fireSubscribers({ kind: 'saving-count' });
2379
+ const chunks = this.splitLocalUpdate(copy);
2380
+ if (chunks === null) {
2381
+ this._shipLocalOp(copy);
2382
+ return;
2383
+ }
2384
+ // In order: each chunk continues the one before it, so a peer applying
2385
+ // them out of sequence holds the tail unapplied until the gap closes.
2386
+ for (const chunk of chunks) this._shipLocalOp(chunk);
1853
2387
  });
1854
2388
  }
1855
2389
 
2390
+ // Hand one op blob to the transport and count it as unsaved, or keep it
2391
+ // local when the server has evicted this document.
2392
+ //
2393
+ // A gone document still takes edits by design: it stays open so the app can
2394
+ // render its own "no longer available" state, and the person may keep
2395
+ // interacting until they navigate. Those edits have nowhere to land. The
2396
+ // transport dropped its retry machinery on `doc_gone`, and the frame would
2397
+ // ship onto a subscription the server evicted and come back as a `forbidden`
2398
+ // that describes nothing the app did. Counting them is the part that shows:
2399
+ // each one adds to `savingCount` for a save that cannot happen, so the doc
2400
+ // reports "no longer available" and "Saving 3 changes..." together and
2401
+ // re-arms the leave-site prompt `_markGone` just released.
2402
+ //
2403
+ // Returns the minted `client_op_id`, or null when the op stayed local.
2404
+ private _shipLocalOp(copy: Uint8Array): string | null {
2405
+ const hook = this._localOpHook;
2406
+ if (!hook) return null;
2407
+ if (this._gone.reason !== null) return null;
2408
+ const clientOpId = hook(copy);
2409
+ this._outstandingByClientId.set(clientOpId, copy);
2410
+ // The local mutate has already fired `subscribe` (the Loro
2411
+ // event), but `savingCount` just incremented; fire one more
2412
+ // synthetic event so a subscribe handler bound on `savingCount`
2413
+ // sees the change.
2414
+ this._fireSubscribers({ kind: 'saving-count' });
2415
+ return clientOpId;
2416
+ }
2417
+
1856
2418
  // Lazy-attach the underlying LoroDoc subscriber. **Do not** wire this
1857
2419
  // in the constructor or before the initial snapshot import: with a
1858
2420
  // subscriber attached, snapshot-import of a 50k doc takes ~8.5 s
@@ -1873,7 +2435,17 @@ export class Document {
1873
2435
  // One channel: every event reaches every subscriber, whatever
1874
2436
  // produced it. The app reconciles by value and reads the cause off
1875
2437
  // the event only when it has a non-render reason to.
1876
- this._subs.emit(event);
2438
+ //
2439
+ // What reaches a subscriber is this SDK's own `{ kind, source }`, not
2440
+ // the engine's event object. An app that receives the engine's shape
2441
+ // can read its internals (container ids, peer ids, the raw diff) and
2442
+ // build on them, which pins the storage engine through a surface that
2443
+ // never promised it. The cause survives as `source`, the same
2444
+ // engine-neutral vocabulary the handle surface reports.
2445
+ this._subs.emit({
2446
+ kind: 'change',
2447
+ source: changeSourceOf(event),
2448
+ } satisfies SyntheticSubscribeEvent);
1877
2449
  const isLocalCommit = (event as { by?: unknown }).by === 'local';
1878
2450
  this._maybeFireUndoState();
1879
2451
  // A local commit (mutate / undo / redo) schedules a debounced
@@ -1897,12 +2469,12 @@ export class Document {
1897
2469
  // construction stays cheap.
1898
2470
  //
1899
2471
  // `cb` fires for EVERY change, whatever produced it: a local `mutate()`,
1900
- // a remote `import`, `undo`, `redo`, `checkout`, and the SDK's synthetic
1901
- // metadata events (saving-count, branches, commands). One channel; the
1902
- // app re-reads / reconciles in the handler and branches on the event only
1903
- // when it needs the cause. CRDT-origin events keep their Loro shape and
1904
- // carry `by` / `origin` so the cause is recoverable; the engine-neutral
1905
- // `origin.source` lives on the handle surface.
2472
+ // a remote op, `undo`, `redo`, a history move, and the SDK's own metadata
2473
+ // events (saving-count, branches, commands). One channel; the app re-reads
2474
+ // / reconciles in the handler and branches on `event.kind` only when it
2475
+ // needs the cause. A content change arrives as
2476
+ // `{ kind: 'change', source }`, carrying the same engine-neutral `source`
2477
+ // the handle surface reports.
1906
2478
  subscribe(cb: DocumentSubscriber): DocumentUnsubscribe {
1907
2479
  this._assertOpen();
1908
2480
  this._ensureLoroSubscribed();
@@ -1988,10 +2560,10 @@ export class Document {
1988
2560
  // The Document is NOT auto-closed; callers may want to render a
1989
2561
  // "the document was deleted" overlay before closing. Disable any
1990
2562
  // pending mutations in the listener and call `close()` when ready.
1991
- // Mutations after `_markGone` still mutate the local CRDT and call
1992
- // the `LocalOpHook`, but the server will reject those op frames
1993
- // (the SUB# row was evicted at fanout time); the SDK consumer
1994
- // sees the rejection via the underlying transport's error channel.
2563
+ // Mutations after `_markGone` still mutate the local CRDT, and stay
2564
+ // local: `_shipLocalOp` keeps them off the transport and out of
2565
+ // `savingCount`, so this one notice is what the app hears about the
2566
+ // eviction rather than a refusal per later edit.
1995
2567
  onGone(cb: DocumentGoneListener): DocumentGoneUnsubscribe {
1996
2568
  // Guard like `subscribe` / `subscribeUndoState`: `_suspend` clears these
1997
2569
  // signals' listeners so a stale handle from a parked session does not
@@ -2035,10 +2607,27 @@ export class Document {
2035
2607
  // false`) is retained so a late `onStatus` subscriber still sees the
2036
2608
  // final state.
2037
2609
  /** @internal */
2038
- _emitStatus(status: DocumentWorkStatus): void {
2610
+ _emitStatus(
2611
+ status: DocumentWorkStatus,
2612
+ opts?: {
2613
+ // The caller imported the document's content immediately before this
2614
+ // emit, so the work delivered what it promised whatever the terminal
2615
+ // frame says.
2616
+ contentLoaded?: boolean;
2617
+ },
2618
+ ): void {
2039
2619
  // A late reload/reconcile closure that resolved after `close()` must not
2040
2620
  // re-emit onto a torn-down doc (its undo stack is freed below in `close`).
2041
2621
  if (this._closed) return;
2622
+ // Terminal work that still reports itself as locking editing is inbound
2623
+ // work that ended without content: the platform clears the status when
2624
+ // hydration / extraction succeeds, so what it reports terminally is a
2625
+ // failure. Anything else means the doc is either loading or holds what
2626
+ // it has.
2627
+ this._loadFailed =
2628
+ opts?.contentLoaded !== true &&
2629
+ status.pending === false &&
2630
+ status.blocking === true;
2042
2631
  this._workStatus.emit(status);
2043
2632
  // A blocking work-status is one of the inputs to `canUndo` / `canRedo`
2044
2633
  // (via `_isLoadLocked`), so a status that engages or releases the load
@@ -2049,10 +2638,10 @@ export class Document {
2049
2638
  }
2050
2639
 
2051
2640
  // The current drive sync state of this document, or `null` when the
2052
- // doc is not linked to a drive source. The read-side companion to
2053
- // `doc.pushToDrive(...)`: an app reads `canPush` / `state` to decide
2054
- // whether to surface a "Publish" affordance, and renders the
2055
- // server-authored `label` / `detail` as the wording. Seeded on open
2641
+ // doc is not linked to a drive source. An app reads `state` / `canPush`
2642
+ // to tell the reader they have unpublished edits, and renders the
2643
+ // server-authored `label` / `detail` as the wording; publishing itself
2644
+ // is the hosted toolbar's control. Seeded on open
2056
2645
  // (for a drive-linked doc) and kept live as edits, refreshes, and
2057
2646
  // pushes move the state.
2058
2647
  get driveStatus(): DocumentDriveStatus | null {
@@ -2104,12 +2693,26 @@ export class Document {
2104
2693
 
2105
2694
  // Plain-JS projection of the current state.
2106
2695
  toJSON(): Record<string, unknown> {
2696
+ this._assertNotClosed();
2107
2697
  // When in checkout, read from the preview LoroDoc so the app
2108
2698
  // renders the historical state. Returning to head clears
2109
2699
  // `_checkout` and reads go back to the live `_doc`.
2110
2700
  const source =
2111
2701
  this._checkout !== null ? this._checkout.previewDoc : this._doc;
2112
- return source.toJSON() as Record<string, unknown>;
2702
+ const json = source.toJSON() as Record<string, unknown>;
2703
+ // A paged root holds its records inside page entries beside the store's
2704
+ // own edit bookkeeping, so flatten it to the same record lookup
2705
+ // `doc.map(root).read()` hands back: a serializer and a whole-document
2706
+ // renderer read this surface, and a store that crossed the paging
2707
+ // threshold has to reach them as records. Every root is asked, because a
2708
+ // root no handle was ever taken on is paged by the same rule as one that
2709
+ // was; a root the marker does not name comes back as loro produced it.
2710
+ for (const [rootKey, value] of Object.entries(json)) {
2711
+ if (!isRootLookup(value)) continue;
2712
+ if (!readRootPaged(source, rootKey)) continue;
2713
+ json[rootKey] = flattenPagedLookup(value, true);
2714
+ }
2715
+ return json;
2113
2716
  }
2114
2717
 
2115
2718
  /**
@@ -2128,29 +2731,36 @@ export class Document {
2128
2731
  // in either caller. No-op when the durable frontier already covers the
2129
2732
  // local doc, while a prior resync push is still unacked, or when the
2130
2733
  // missing slice is too large to ship as one op (see the cap below).
2734
+ //
2735
+ // The return value says whether the caller may stop asking. `covered`
2736
+ // means the durable frontier holds everything local does, so the
2737
+ // handshake is finished; every other outcome leaves the doc diverged and
2738
+ // is the caller's cue to run the handshake again once the push settles.
2131
2739
  /** @internal */
2132
2740
  maybeResyncPush(
2133
2741
  persistedSnapshotVersionVector: Uint8Array,
2134
2742
  durableTailBlobs: Uint8Array[],
2135
- ): void {
2743
+ ): ResyncPushOutcome {
2136
2744
  // `computeResyncPush` reads `this._doc.version()` / `export()`; refuse on a
2137
2745
  // closed (freed) doc so a late `doc_ops_dropped` / periodic-reverify
2138
2746
  // closure can't touch freed WASM.
2139
- if (this._closed) return;
2747
+ if (this._closed) return 'closed';
2140
2748
  // Loop guard: skip while any chunk of a prior push is still unacked,
2141
2749
  // so a flurry of reconnects cannot re-push the same missing slice.
2142
2750
  for (const id of this._resyncPushClientOpIds) {
2143
- if (this._outstandingByClientId.has(id)) return;
2751
+ if (this._outstandingByClientId.has(id)) return 'in-flight';
2144
2752
  }
2145
2753
  const chunks = this.computeResyncPush(
2146
2754
  persistedSnapshotVersionVector,
2147
2755
  durableTailBlobs,
2148
2756
  );
2149
- if (chunks === null) return;
2757
+ if (chunks === 'covered') return 'covered';
2758
+ if (chunks === 'undecodable') return 'undecodable';
2150
2759
  for (const chunk of chunks) {
2151
2760
  const id = this.injectLocalUpdate(chunk);
2152
2761
  if (id !== null) this._resyncPushClientOpIds.add(id);
2153
2762
  }
2763
+ return 'pushed';
2154
2764
  }
2155
2765
 
2156
2766
  // Given the server's persisted snapshot version vector and the op
@@ -2168,10 +2778,13 @@ export class Document {
2168
2778
  // case. `decodeImportBlobMeta` reads each blob's end vector from its
2169
2779
  // header without a full import, so this stays cheap.
2170
2780
  //
2171
- // Returns the chunks to resync-push, or null when the durable state
2172
- // already covers everything local holds (or a malformed vector / tail
2173
- // blob, which degrades the handshake to a no-op and warns rather than
2174
- // throwing out of the frame handler that drives the live transport).
2781
+ // Returns the chunks to resync-push, `'covered'` when the durable state
2782
+ // already holds everything local does, or `'undecodable'` for a malformed
2783
+ // vector / tail blob, which degrades the handshake to a no-op and warns
2784
+ // rather than throwing out of the frame handler that drives the live
2785
+ // transport. `'undecodable'` is distinct from `'covered'` because it says
2786
+ // nothing about whether the doc has diverged, so a caller healing a known
2787
+ // divergence must keep asking.
2175
2788
  //
2176
2789
  // The common case is one chunk: an `update` export from the durable
2177
2790
  // frontier carries only the missing ops plus their dependencies, never
@@ -2183,11 +2796,47 @@ export class Document {
2183
2796
  // large divergence heals instead of being skipped. Import is idempotent
2184
2797
  // and order-independent (Loro buffers a chunk whose dependency is in a
2185
2798
  // sibling chunk until it arrives), so the chunks need no ordering.
2799
+ /**
2800
+ * Whether the durable snapshot holds changes by an author this doc has never
2801
+ * heard from.
2802
+ *
2803
+ * Every author a client can hear from reaches it as ops, so a peer present
2804
+ * in the durable frontier and absent from the local doc after the tail has
2805
+ * applied is one whose changes exist only inside the snapshot. That is the
2806
+ * platform having rewritten how the document stores what it stores
2807
+ * (compaction's patch fold is the one thing that does this), which a client
2808
+ * cannot merge with and cannot undo across: its undo stack inverts the
2809
+ * operations it made, and the values those operations wrote now live
2810
+ * somewhere else.
2811
+ *
2812
+ * The answer drives a re-import, which is the existing recovery for exactly
2813
+ * this shape (`_applySnapshot` drops both stacks, so undo reports itself
2814
+ * unavailable instead of silently reverting nothing).
2815
+ *
2816
+ * @internal
2817
+ */
2818
+ _snapshotHasUnseenAuthor(persistedSnapshotVersionVector: Uint8Array): boolean {
2819
+ if (this._closed) return false;
2820
+ let durable: VersionVector;
2821
+ try {
2822
+ durable = VersionVector.decode(persistedSnapshotVersionVector);
2823
+ } catch {
2824
+ // Undecodable says nothing, and the durability handshake already warns
2825
+ // about it on the same frame.
2826
+ return false;
2827
+ }
2828
+ const local = this._doc.version().toJSON();
2829
+ for (const [peer, counter] of durable.toJSON()) {
2830
+ if (counter > 0 && !local.has(peer)) return true;
2831
+ }
2832
+ return false;
2833
+ }
2834
+
2186
2835
  /** @internal */
2187
2836
  computeResyncPush(
2188
2837
  persistedSnapshotVersionVector: Uint8Array,
2189
2838
  durableTailBlobs: Uint8Array[],
2190
- ): Uint8Array[] | null {
2839
+ ): Uint8Array[] | 'covered' | 'undecodable' {
2191
2840
  let durable: VersionVector;
2192
2841
  try {
2193
2842
  durable = VersionVector.decode(persistedSnapshotVersionVector);
@@ -2200,7 +2849,7 @@ export class Document {
2200
2849
  '[Brass] durability handshake skipped: could not decode the persisted version vector or a tail op blob',
2201
2850
  err,
2202
2851
  );
2203
- return null;
2852
+ return 'undecodable';
2204
2853
  }
2205
2854
  const local = this._doc.version();
2206
2855
  const cmp = local.compare(durable);
@@ -2209,7 +2858,7 @@ export class Document {
2209
2858
  // undefined when the two are concurrent (local holds ops the server
2210
2859
  // lacks AND vice versa). Push in the ahead and concurrent cases; the
2211
2860
  // export from `durable` carries only the local-only ops either way.
2212
- if (cmp !== undefined && cmp <= 0) return null;
2861
+ if (cmp !== undefined && cmp <= 0) return 'covered';
2213
2862
  // Common case: the whole missing slice fits in one frame. Keep this
2214
2863
  // path byte-identical to a plain `update` export so the no-chunking
2215
2864
  // case is unchanged.
@@ -2237,39 +2886,98 @@ export class Document {
2237
2886
  }
2238
2887
 
2239
2888
  // Recursively export the given ranges as one `updates-in-range` blob,
2240
- // splitting by op count until each blob is under the per-frame cap. An
2241
- // irreducible single op (one that alone exceeds the cap) is emitted up
2242
- // to the hard ceiling, or skipped with a warning beyond it so the loop
2243
- // never wedges on an un-sendable frame.
2889
+ // splitting by op count until each blob is under the per-chunk cap.
2890
+ //
2891
+ // `dropOversizedSingle` decides what happens to an irreducible op (one that
2892
+ // alone exceeds the cap and the hard ceiling). A resync drops it with a
2893
+ // warning so the loop never wedges on an un-sendable frame while the rest
2894
+ // of the slice heals. A local commit keeps it: it is an edit the app just
2895
+ // made and nothing else will carry it, so it goes to the write path, which
2896
+ // reports the refusal to the app rather than losing it silently.
2897
+ private chunkRanges(
2898
+ ranges: { peer: PeerID; from: number; to: number }[],
2899
+ dropOversizedSingle: boolean,
2900
+ ): Uint8Array[] {
2901
+ const total = ranges.reduce((n, r) => n + (r.to - r.from), 0);
2902
+ if (total === 0) return [];
2903
+ const blob = this._doc.export({
2904
+ mode: 'updates-in-range',
2905
+ spans: ranges.map((r) => ({
2906
+ id: { peer: r.peer, counter: r.from },
2907
+ len: r.to - r.from,
2908
+ })),
2909
+ });
2910
+ if (blob.byteLength <= RESYNC_PUSH_MAX_BYTES) return [blob];
2911
+ if (total === 1) {
2912
+ // A single op that cannot be split further.
2913
+ if (blob.byteLength <= RESYNC_PUSH_HARD_MAX_BYTES) return [blob];
2914
+ if (!dropOversizedSingle) return [blob];
2915
+ console.warn(
2916
+ `[Brass] durability resync skipped one op of ${blob.byteLength} bytes, over the ${RESYNC_PUSH_HARD_MAX_BYTES}-byte single-frame ceiling. The rest of the slice still heals; this op heals on a future snapshot-based recovery.`,
2917
+ );
2918
+ return [];
2919
+ }
2920
+ const [first, second] = splitRangesByLength(ranges, total);
2921
+ return [
2922
+ ...this.chunkRanges(first, dropOversizedSingle),
2923
+ ...this.chunkRanges(second, dropOversizedSingle),
2924
+ ];
2925
+ }
2926
+
2244
2927
  private chunkMissingSlice(
2245
2928
  local: VersionVector,
2246
2929
  durable: VersionVector,
2247
2930
  ): Uint8Array[] {
2248
- const pack = (
2249
- ranges: { peer: PeerID; from: number; to: number }[],
2250
- ): Uint8Array[] => {
2251
- const total = ranges.reduce((n, r) => n + (r.to - r.from), 0);
2252
- if (total === 0) return [];
2253
- const blob = this._doc.export({
2254
- mode: 'updates-in-range',
2255
- spans: ranges.map((r) => ({
2256
- id: { peer: r.peer, counter: r.from },
2257
- len: r.to - r.from,
2258
- })),
2259
- });
2260
- if (blob.byteLength <= RESYNC_PUSH_MAX_BYTES) return [blob];
2261
- if (total === 1) {
2262
- // A single op that cannot be split further.
2263
- if (blob.byteLength <= RESYNC_PUSH_HARD_MAX_BYTES) return [blob];
2264
- console.warn(
2265
- `[Brass] durability resync skipped one op of ${blob.byteLength} bytes, over the ${RESYNC_PUSH_HARD_MAX_BYTES}-byte single-frame ceiling. The rest of the slice still heals; this op heals on a future snapshot-based recovery.`,
2266
- );
2267
- return [];
2931
+ return this.chunkRanges(this.missingRanges(local, durable), true);
2932
+ }
2933
+
2934
+ // A local commit re-exported as several op blobs, or null to ship it whole.
2935
+ //
2936
+ // `mutate` bounds what one commit carries by committing as it goes, but a
2937
+ // commit Loro makes for itself takes no such budget: `redo` replays a whole
2938
+ // undo step, so a bulk write that correctly split into many ops comes back
2939
+ // as one op the size of the entire write. Past `MAX_OP_BLOB_CHARS` that op
2940
+ // is refused outright and the redone edit never persists, and below it, it
2941
+ // still lands as a single large row on the document's one storage partition.
2942
+ //
2943
+ // The ops already exist, so the only lever left is which spans they ship in.
2944
+ // The blob names the counter range it covers, and re-exporting a sub-range
2945
+ // is what the durability resync already does, so the chunks are ordinary ops
2946
+ // a peer applies in sequence.
2947
+ //
2948
+ // Splitting is abandoned unless EVERY chunk comes in under the cap. A commit
2949
+ // holding one value nothing can split (a mesh, a large embedded body) would
2950
+ // otherwise ship as a small chunk that lands and an irreducible one that the
2951
+ // write path refuses, which puts a record on the server missing the field
2952
+ // that made it large and leaves the two copies permanently apart. Whole, its
2953
+ // refusal covers the whole edit and the app is told once.
2954
+ private splitLocalUpdate(blob: Uint8Array): Uint8Array[] | null {
2955
+ if (blob.byteLength <= RESYNC_PUSH_MAX_BYTES) return null;
2956
+ let ranges: { peer: PeerID; from: number; to: number }[];
2957
+ try {
2958
+ const meta = decodeImportBlobMeta(blob, false);
2959
+ const start = meta.partialStartVersionVector.toJSON();
2960
+ ranges = [];
2961
+ for (const [peer, to] of meta.partialEndVersionVector.toJSON()) {
2962
+ const from = start.get(peer) ?? 0;
2963
+ if (to > from) ranges.push({ peer, from, to });
2268
2964
  }
2269
- const [first, second] = splitRangesByLength(ranges, total);
2270
- return [...pack(first), ...pack(second)];
2271
- };
2272
- return pack(this.missingRanges(local, durable));
2965
+ } catch {
2966
+ // An undecodable blob is one this cannot describe in spans. Shipping it
2967
+ // whole is what happens without this path at all.
2968
+ return null;
2969
+ }
2970
+ const total = rangeOpCount(ranges);
2971
+ if (total < 2) return null;
2972
+ // The blob's own size names how many pieces to ask for, so the split does
2973
+ // not rediscover that by halving.
2974
+ const parts = Math.min(total, Math.ceil(blob.byteLength / RESYNC_PUSH_MAX_BYTES));
2975
+ const chunks = splitRangesEvenly(ranges, parts).flatMap((piece) =>
2976
+ this.chunkRanges(piece, false),
2977
+ );
2978
+ if (chunks.length < 2) return null;
2979
+ if (chunks.some((c) => c.byteLength > RESYNC_PUSH_MAX_BYTES)) return null;
2980
+ return chunks;
2273
2981
  }
2274
2982
 
2275
2983
  // Feed an op blob into the local-op send path as if it were a fresh
@@ -2283,14 +2991,9 @@ export class Document {
2283
2991
  /** @internal */
2284
2992
  injectLocalUpdate(blob: Uint8Array): string | null {
2285
2993
  this._ensureLocalUpdatesSubscribed();
2286
- const hook = this._localOpHook;
2287
- if (!hook) return null;
2288
2994
  const copy = new Uint8Array(blob.byteLength);
2289
2995
  copy.set(blob);
2290
- const clientOpId = hook(copy);
2291
- this._outstandingByClientId.set(clientOpId, copy);
2292
- this._fireSubscribers({ kind: 'saving-count' });
2293
- return clientOpId;
2996
+ return this._shipLocalOp(copy);
2294
2997
  }
2295
2998
 
2296
2999
  // Internal: park the Document in the SDK cache. Clears app-side
@@ -2305,6 +3008,7 @@ export class Document {
2305
3008
  _suspend(): void {
2306
3009
  if (this._closed || this._suspended) return;
2307
3010
  this._suspended = true;
3011
+ this._setStreamsSuspended(true);
2308
3012
  // A pending auto-checkpoint would otherwise fire against a parked
2309
3013
  // Document and silently no-op via the scheduler's guard.
2310
3014
  // Cancelling here keeps the timer count clean across long-lived
@@ -2347,6 +3051,7 @@ export class Document {
2347
3051
  /** @internal */
2348
3052
  _resume(): void {
2349
3053
  this._suspended = false;
3054
+ this._setStreamsSuspended(false);
2350
3055
  // Pair of the `_suspend` presence gate: reopen the presence controller's
2351
3056
  // outbound path and re-advertise this client's cursor on the resumed open.
2352
3057
  // `_suspend` only closed the gate; without this the parked doc's presence
@@ -2387,6 +3092,7 @@ export class Document {
2387
3092
  this._currentSeqProvider = null;
2388
3093
  this._outstandingByClientId.clear();
2389
3094
  this._resyncPushClientOpIds.clear();
3095
+ releaseUnsavedWork(this);
2390
3096
  this._subs.clear();
2391
3097
  this._undoStateSubs.clear();
2392
3098
  // Drop the signal listeners too, mirroring `_suspend()`. Their frames