js-bao 0.5.1 → 0.6.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.
package/dist/index.d.ts CHANGED
@@ -96,10 +96,28 @@ interface DatabaseConfig {
96
96
  }
97
97
 
98
98
  type DocumentPermissionHint = "read" | "read-write";
99
+ /**
100
+ * Per-document storage format (#2816). `1` (or absent) is the legacy "one
101
+ * Y.Doc" document; `2` is a large document whose Y.Doc holds only the current
102
+ * epoch's overlay and whose merged `records` table is the document.
103
+ */
104
+ type DocumentFormat = 1 | 2;
105
+ interface ConnectDocumentOptions {
106
+ documentFormat?: DocumentFormat;
107
+ /**
108
+ * The epoch a large document is on, as the caller understands it (#2816).
109
+ *
110
+ * The record store is created following epoch 0, which the open path reads
111
+ * as "this database holds no document" — so a bind without the mark makes a
112
+ * client that has the document look like one that never did.
113
+ */
114
+ epoch?: number;
115
+ }
99
116
  interface ConnectedDocument {
100
117
  docId: string;
101
118
  yDoc: Y.Doc;
102
119
  permissionHint: DocumentPermissionHint;
120
+ documentFormat?: DocumentFormat;
103
121
  }
104
122
  interface SaveOptions {
105
123
  targetDocument?: string;
@@ -107,7 +125,7 @@ interface SaveOptions {
107
125
  upsertOn?: string;
108
126
  }
109
127
  interface DocumentManager {
110
- connectDocument(docId: string, yDoc: Y.Doc, permissionHint: DocumentPermissionHint): Promise<void>;
128
+ connectDocument(docId: string, yDoc: Y.Doc, permissionHint: DocumentPermissionHint, options?: ConnectDocumentOptions): Promise<void>;
111
129
  disconnectDocument(docId: string): Promise<void>;
112
130
  getConnectedDocuments(): Map<string, ConnectedDocument>;
113
131
  isDocumentConnected(docId: string): boolean;
@@ -217,6 +235,549 @@ interface PaginationOptions {
217
235
  direction?: "forward" | "backward";
218
236
  }
219
237
 
238
+ /**
239
+ * Shared format-2 ("large document", #2816) primitives.
240
+ *
241
+ * A format-2 document is no longer "one Y.Doc": the authoritative document is
242
+ * a persisted SQLite `records` table on both ends, and the Y.Doc shrinks to a
243
+ * bounded per-epoch **state overlay** of the records/fields touched during the
244
+ * current epoch. This module owns the overlay's on-the-wire shape so the
245
+ * client write points, the client observer, the server projector, the
246
+ * materializer and the snapshot builder all encode and decode it identically.
247
+ *
248
+ * ## Encoding — flat field-level keys (sponsor amendment, 2026-08-21)
249
+ *
250
+ * Each model map holds FLAT entries, not one nested `Y.Map` per record:
251
+ *
252
+ * ```
253
+ * ⟨recordId⟩/⟨field⟩ → value ordinary field (`null` = unset)
254
+ * ⟨recordId⟩/⟨field⟩/⟨member⟩ → true|false stringset member / tombstone (D3)
255
+ * ⟨recordId⟩/_replace → true discard the base row
256
+ * ⟨recordId⟩/_deleted → true|false record tombstone
257
+ * ```
258
+ *
259
+ * The nested layout cannot work under epochs: two clients that both touch a
260
+ * record ABSENT from the current epoch each mint a fresh `Y.Map` at the same
261
+ * model-map key, and Yjs resolves that parent-key conflict as last-writer-wins,
262
+ * silently dropping the loser's fields. With flat keys, concurrency resolves
263
+ * per field key, so a concurrent create-vs-patch merges to the union of fields
264
+ * with LWW only on genuinely colliding fields.
265
+ *
266
+ * The record's LOGICAL overlay content is unchanged from the approved design —
267
+ * consumers group a record's keys back into a fields object plus markers via
268
+ * {@link groupOverlayEntries} and apply the record-level rules in
269
+ * {@link materializeOverlayRecord}.
270
+ */
271
+
272
+ /** Separator between the record-id, field and member segments of an overlay key. */
273
+ declare const OVERLAY_KEY_SEPARATOR = "/";
274
+ /** Marker key: the create discards the base row and writes exactly its fields. */
275
+ declare const OVERLAY_MARKER_REPLACE = "_replace";
276
+ /** Marker key: the record is deleted (a bare Yjs key delete is invisible). */
277
+ declare const OVERLAY_MARKER_DELETED = "_deleted";
278
+ /**
279
+ * Escape one overlay-key segment so the key stays unambiguous.
280
+ *
281
+ * Record ids are ULIDs and always safe, but field and member names are user
282
+ * data: they may contain the separator, the escape character, or start with the
283
+ * `_` that marks a marker key. Escaping is RFC 6901-flavored:
284
+ *
285
+ * - `~` → `~0`
286
+ * - `/` → `~1`
287
+ * - a LEADING `_` → `~2` (so a field literally named `_replace` can never be
288
+ * read back as the `_replace` marker, which is written unescaped)
289
+ */
290
+ declare function encodeOverlaySegment(segment: string): string;
291
+ /** Inverse of {@link encodeOverlaySegment}. */
292
+ declare function decodeOverlaySegment(segment: string): string;
293
+ /** Overlay key for an ordinary field of a record. */
294
+ declare function overlayFieldKey(recordId: string, field: string): string;
295
+ /** Overlay key for one stringset member of a record's field. */
296
+ declare function overlayMemberKey(recordId: string, field: string, member: string): string;
297
+ /** Overlay key for a record-level marker (written unescaped, by construction). */
298
+ declare function overlayMarkerKey(recordId: string, marker: string): string;
299
+ /** The parts an overlay key decodes to, or `null` when it is not an overlay key. */
300
+ interface ParsedOverlayKey {
301
+ recordId: string;
302
+ field?: string;
303
+ member?: string;
304
+ marker?: string;
305
+ }
306
+ /** Decode an overlay key back into its parts; `null` when it is malformed. */
307
+ declare function parseOverlayKey(key: string): ParsedOverlayKey | null;
308
+ /** One record's overlay content, regrouped from its flat keys. */
309
+ interface OverlayRecordEntry {
310
+ id: string;
311
+ /** Touched ordinary fields. A `null` value is an explicit unset (RFC 7396). */
312
+ fields: Record<string, unknown>;
313
+ /** Touched stringset members per field: `member → true` add, `false` tombstone. */
314
+ stringSets: Record<string, Record<string, boolean>>;
315
+ /** The create discarded the base row. */
316
+ replace: boolean;
317
+ /** The record is tombstoned. */
318
+ deleted: boolean;
319
+ }
320
+ /**
321
+ * Regroup a model map's flat overlay entries into one {@link OverlayRecordEntry}
322
+ * per record. Accepts anything iterable of `[key, value]` — a `Y.Map`'s
323
+ * `entries()`, an array of observed changes, or a decoded snapshot.
324
+ */
325
+ declare function groupOverlayEntries(entries: Iterable<readonly [string, unknown]>): Map<string, OverlayRecordEntry>;
326
+ /** Anything that can enumerate a model map's overlay keys. */
327
+ interface OverlayEntrySource {
328
+ entries(): Iterable<readonly [string, unknown]> | IterableIterator<any>;
329
+ }
330
+ /**
331
+ * Read ONE record's complete overlay entry out of a model map.
332
+ *
333
+ * A delta fold — projecting exactly the keys an update touched — is right for
334
+ * a patch, which merges over the stored row. It is wrong for `_replace`: that
335
+ * marker discards the row entirely, so the record has to be rebuilt from every
336
+ * overlay key it currently has, not just the ones the arriving update carried.
337
+ * Concurrency is where the two differ: when one client patches a record while
338
+ * another deletes and re-creates it, Yjs merges their keys per key, and the
339
+ * update carrying `_replace` names only its own — folding it alone would drop
340
+ * the concurrent patch that the overlay itself still holds.
341
+ *
342
+ * Costs a walk of the model map, which is bounded by the epoch rotation
343
+ * threshold. Use {@link completeOverlayEntries} when several records need
344
+ * rebuilding: it pays that walk ONCE for all of them.
345
+ */
346
+ declare function readOverlayRecord(modelMap: OverlayEntrySource, recordId: string): OverlayRecordEntry;
347
+ /**
348
+ * Complete the entries of a delta fold that need the record's whole overlay.
349
+ *
350
+ * Applied by both the server's projector and the client's observer, so the
351
+ * authoritative table and the local merged view fold an update identically.
352
+ *
353
+ * Every create carries `_replace`, so this sits on the hot write path: the
354
+ * model map is walked ONCE per call however many records were replaced. A walk
355
+ * per replaced record would make one bulk create quadratic in the number of
356
+ * records it writes.
357
+ */
358
+ declare function completeOverlayEntries(modelMap: OverlayEntrySource, entries: Map<string, OverlayRecordEntry>): Map<string, OverlayRecordEntry>;
359
+ /**
360
+ * Materialize one record: `base ⊕ overlay`, the operation that turns
361
+ * `S_E ⊕ D_E` into the authoritative `records` row.
362
+ *
363
+ * - a `_deleted` tombstone materializes to `null` (delete the row);
364
+ * - `_replace` discards `base` entirely and writes exactly the overlay's
365
+ * fields, so a delete-then-re-create cannot resurrect base fields the
366
+ * re-created record omitted;
367
+ * - an ordinary field merges RFC 7396-style, with an explicit `null` removing
368
+ * the key;
369
+ * - stringset member keys apply as add/remove deltas over the base members.
370
+ *
371
+ * Applying an overlay is idempotent and order-free at the record level, so a
372
+ * torn read during a snapshot build is corrected by the next overlay.
373
+ */
374
+ declare function materializeOverlayRecord(base: Record<string, unknown> | null | undefined, overlay: OverlayRecordEntry): Record<string, unknown> | null;
375
+ /** A record mutation expressed in overlay terms, before it is encoded to keys. */
376
+ interface OverlayMutation {
377
+ id: string;
378
+ /**
379
+ * `create` writes `_replace` (the id is absent from the merged view) and
380
+ * clears any prior `_deleted`; `patch` touches only the named fields;
381
+ * `delete` writes the `_deleted` tombstone.
382
+ */
383
+ kind: "create" | "patch" | "delete";
384
+ /** Ordinary field values; `null` is an explicit unset. */
385
+ fields?: Record<string, unknown>;
386
+ /** Per-field stringset deltas: `member → true` add, `false` remove (D3). */
387
+ stringSetDeltas?: Record<string, Record<string, boolean>>;
388
+ }
389
+ /**
390
+ * Encode a mutation into the flat overlay entries it writes. Returned as
391
+ * `[key, value]` pairs so callers can apply them to a `Y.Map`, ship them, or
392
+ * assert on them without a Y.Doc.
393
+ */
394
+ declare function encodeOverlayMutation(mutation: OverlayMutation): Array<[string, unknown]>;
395
+ /** Apply {@link encodeOverlayMutation}'s entries to a model map in one place. */
396
+ declare function applyOverlayMutation(modelMap: Y.Map<any>, mutation: OverlayMutation): void;
397
+ /**
398
+ * A composite (`Y.Map` / `Y.Array` / `Y.Text`) value written into a field slot
399
+ * of a format-2 document (D4). Format 2 rejects these instead of normalizing
400
+ * them, which is what lets the overlay encode field values as plain JSON.
401
+ */
402
+ declare class Format2CompositeFieldError extends Error {
403
+ readonly modelName: string;
404
+ readonly recordId: string;
405
+ readonly field: string;
406
+ constructor(modelName: string, recordId: string, field: string);
407
+ }
408
+ /**
409
+ * D4: reject a Yjs composite in a field slot on a format-2 write path. Plain
410
+ * values pass untouched, including stringset arrays and `{$add}`/`{$remove}`
411
+ * delta objects — only Yjs instances are rejected.
412
+ */
413
+ declare function assertNoFormat2CompositeFields(modelName: string, recordId: string, fields: Record<string, unknown> | undefined | null): void;
414
+ /**
415
+ * D3, format-scoped: the tombstone-aware stringset discriminator. A format-2
416
+ * stringset `Y.Map` may carry `false` for a removed member, which the legacy
417
+ * (#625) discriminator deliberately rejects — widening it globally would
418
+ * reclassify legacy all-boolean composites and break the "format 1 unchanged"
419
+ * contract, so legacy paths keep {@link isStringSetYMap} untouched.
420
+ *
421
+ * Safe here because D4 guarantees no composite field values exist on format-2
422
+ * documents.
423
+ */
424
+ declare function isFormat2StringSetYMap(value: unknown): value is Y.Map<any>;
425
+ /** Members of a tombstone-aware stringset map: the `true` entries only. */
426
+ declare function format2StringSetMembers(value: unknown): string[];
427
+
428
+ /**
429
+ * Fold a format-2 epoch overlay into a `records` table (#2816).
430
+ *
431
+ * Shared by BOTH ends: the Durable Object's authoritative table and the
432
+ * client's local merged view fold the overlay with this one implementation, so
433
+ * "what the server says the record is" and "what the client reads back" cannot
434
+ * drift.
435
+ *
436
+ * On a format-2 document the SQLite `records` table is the document — the
437
+ * Y.Doc holds only the current epoch's overlay. Every applied update therefore
438
+ * has to reach `records` in the same transaction boundary as the update-log
439
+ * row, for a document that may be 2GB. That rules out read-modify-write of the
440
+ * record: this module expresses each overlay entry as one merge-upsert whose
441
+ * RFC 7396 semantics SQLite's `json_patch` already implements (an explicit
442
+ * `null` deletes the key), plus incremental `stringset_index` maintenance for
443
+ * the member deltas.
444
+ *
445
+ * The semantics are pinned against the shared JS materializer
446
+ * (`materializeOverlayRecord`), which is what the client's merged view folds
447
+ * with — the two must never drift.
448
+ */
449
+
450
+ /**
451
+ * The slice of Cloudflare's `SqlStorage` this module needs. `exec` returns an
452
+ * iterable cursor of rows; a plain array satisfies it too, which is what the
453
+ * hermetic tests bind.
454
+ */
455
+ interface OverlaySql {
456
+ exec(query: string, ...bindings: unknown[]): Iterable<Record<string, any>>;
457
+ }
458
+ /**
459
+ * Which tables the fold writes to.
460
+ *
461
+ * The Durable Object hosts exactly one document, so it uses the default names.
462
+ * A CLIENT database can hold several connected documents at once, and the
463
+ * merged view of one must never be readable as another's — so the client
464
+ * passes per-document table names (see `format2TableNames`). The STATEMENTS
465
+ * stay this module's single implementation either way; only the table names
466
+ * differ, which is what keeps the two ends from drifting.
467
+ */
468
+ interface OverlayTables {
469
+ records: string;
470
+ stringSetIndex: string;
471
+ }
472
+ /** The Durable Object's tables: one document per DO, so no scoping needed. */
473
+ declare const DEFAULT_OVERLAY_TABLES: OverlayTables;
474
+ /**
475
+ * Project one record's overlay entry into `records` + `stringset_index`.
476
+ *
477
+ * Callers pass the entry for the keys the update TOUCHED, not the record's
478
+ * whole overlay: materialization is a fold, so `row ⊕ delta` over the stored
479
+ * row is the same record as replaying the epoch from its base.
480
+ */
481
+ declare function projectOverlayEntry(sql: OverlaySql, modelName: string, entry: OverlayRecordEntry, tables?: OverlayTables): void;
482
+
483
+ /**
484
+ * The client-side record store for format-2 (large) documents (#2816).
485
+ *
486
+ * A format-2 client no longer reads records out of Y.Maps: the epoch doc holds
487
+ * only what the current epoch touched, so the merged `records` view is the
488
+ * document. The existing `StorageProvider` cannot serve that — it is an async
489
+ * key-value API with no query and no multi-table transaction.
490
+ *
491
+ * This store defines the contract instead:
492
+ *
493
+ * - **reads** come from `records` (folded with the same `projectOverlayEntry`
494
+ * the Durable Object uses, so the two views cannot drift);
495
+ * - **writes** commit the merged row and the `_pending_ops` entry in ONE
496
+ * transaction — the commit barrier a `save()` resolves behind, which is what
497
+ * makes read-your-writes true and makes a crash immediately after the save
498
+ * non-lossy;
499
+ * - **`_pending_ops`** are pruned only at or below the server's `update.ack`
500
+ * high-water mark, never on `ws.send`;
501
+ * - `_epoch` and `_snapshot_load` carry the epoch the client is following and
502
+ * the chunks a cold load has already completed.
503
+ *
504
+ * It is deliberately synchronous over an injected SQL executor: better-sqlite3
505
+ * (Node) drives it directly, and the worker-hosted OPFS engine drives it inside
506
+ * its worker, where a single round-trip commits the whole transaction.
507
+ *
508
+ * ## Document scoping
509
+ *
510
+ * Unlike the Durable Object — one document per DO — a client database can hold
511
+ * several connected documents at once, so every table here is scoped to ONE
512
+ * document: `records`/`stringset_index` get per-document names, and the
513
+ * bookkeeping tables carry a `doc_id` column in their primary key. Without
514
+ * that, two open large documents would read each other's records and one
515
+ * document's `update.ack` would prune the other's unsynced writes.
516
+ */
517
+
518
+ /** A SQL executor that can also run a set of statements as one atomic commit. */
519
+ interface Format2StoreSql extends OverlaySql {
520
+ transaction<T>(fn: () => T): T;
521
+ }
522
+ /**
523
+ * Per-document names for the merged view's tables.
524
+ *
525
+ * The document id is hex-encoded rather than sanitized: a lossy mapping could
526
+ * land two documents on the same tables, which is exactly the mixing this
527
+ * scoping exists to prevent.
528
+ */
529
+ declare function format2TableNames(docId: string): OverlayTables;
530
+ /**
531
+ * The local schema one format-2 document owns.
532
+ *
533
+ * `records`/`stringset_index` keep the DO's column shape and primary keys —
534
+ * the shared fold in `overlaySql.ts` writes both ends — under per-document
535
+ * names. The bookkeeping tables are shared across documents and scoped by
536
+ * `doc_id` instead.
537
+ */
538
+ declare function format2ClientDDL(docId: string): string[];
539
+ /** One durable record of a local write, replayed if it is never acked. */
540
+ interface PendingOp {
541
+ seq: number;
542
+ model: string;
543
+ recordId: string;
544
+ op: "create" | "patch" | "delete";
545
+ /** Field names the op touched — the unit amended-D1 conflict detection works on. */
546
+ fields: string[];
547
+ /** Epoch the op was written against, for recency-aware replay. */
548
+ baseEpoch: number;
549
+ /** Server-offset-corrected wall clock at write time. */
550
+ ts: number;
551
+ /**
552
+ * The overlay mutation this op wrote, so the write can be reproduced from
553
+ * the durable log alone — see {@link Format2RecordStore.commitLocalWrite}.
554
+ * `null` only for a row written by a build that did not record it.
555
+ */
556
+ mutation: OverlayMutation | null;
557
+ }
558
+ type PendingOpInput = Omit<PendingOp, "seq" | "mutation"> & {
559
+ seq?: number;
560
+ };
561
+ /**
562
+ * A model this device did not hydrate, used as if it had (#2816,
563
+ * Precondition B's fallback).
564
+ *
565
+ * A capped load leaves a model out because the device has no room for it. The
566
+ * records it does hold of that model afterwards — whatever a later overlay
567
+ * happens to touch — are not the model, they are a handful of recently
568
+ * changed rows, and answering a query from them would be wrong in the way
569
+ * that is hardest to notice. So the store refuses instead: the model is
570
+ * UNAVAILABLE on this device, which is a thing an app can handle, rather than
571
+ * nearly empty, which is not.
572
+ */
573
+ declare class Format2ModelNotHydratedError extends Error {
574
+ readonly model: string;
575
+ /** The models this device did load. */
576
+ readonly hydrated: string[];
577
+ /** Stable across builds, so an app can branch on it rather than on text. */
578
+ readonly code = "FORMAT2_MODEL_NOT_HYDRATED";
579
+ constructor(model: string,
580
+ /** The models this device did load. */
581
+ hydrated: string[]);
582
+ }
583
+ declare class Format2RecordStore {
584
+ private readonly sql;
585
+ private readonly docId;
586
+ /** This document's merged-view tables (never another document's). */
587
+ private readonly tables;
588
+ /** Models this device holds, or `null` for the whole document. */
589
+ private scope;
590
+ /** Whether {@link scope} has been read from `_epoch` yet. */
591
+ private scopeLoaded;
592
+ constructor(sql: Format2StoreSql, docId: string);
593
+ /** Create the local schema. Safe to call on every open. */
594
+ init(): void;
595
+ /**
596
+ * Record which models this device holds (#2816, Precondition B's fallback).
597
+ *
598
+ * `null` — the normal case — is "the whole document". A list is what a
599
+ * capped cold load leaves behind: the plan
600
+ * {@link planSnapshotHydration} made, persisted so it outlives the load. It
601
+ * has to be durable, because a restart reconnects and starts folding the
602
+ * epoch overlay again, and without the scope that fold would quietly
603
+ * repopulate a skipped model with the handful of records the epoch touched.
604
+ */
605
+ setHydrationScope(models: readonly string[] | null): void;
606
+ /** The models this device holds, or `null` when it holds the document. */
607
+ hydrationScope(): string[] | null;
608
+ /** Whether this device holds `modelName` at all. */
609
+ isHydrated(modelName: string): boolean;
610
+ /** Refuse a model this device does not hold, rather than answering partly. */
611
+ private assertHydrated;
612
+ read(modelName: string, recordId: string): Record<string, any> | null;
613
+ readAll(modelName: string): Array<Record<string, any>>;
614
+ /**
615
+ * The id of the first record whose fields all match — the merged-view answer
616
+ * to `upsertOn` resolution and to an optimistic unique-constraint check. A
617
+ * base-only holder is found here and nowhere in the epoch doc, which is the
618
+ * whole point.
619
+ */
620
+ findIdByFields(modelName: string, fields: string[], values: unknown[]): string | null;
621
+ findIdByField(modelName: string, field: string, value: unknown): string | null;
622
+ /**
623
+ * Every record id a model holds in the merged view, ids only.
624
+ *
625
+ * The derived query tables are rebuilt from this on connect, and a rebuild
626
+ * has no reason to carry each record's whole `_data` through memory on the
627
+ * way.
628
+ */
629
+ recordIds(modelName: string): string[];
630
+ /**
631
+ * One PAGE of a model's record ids, in id order, after `after`.
632
+ *
633
+ * The whole-model list above is a JavaScript array of every id, which on the
634
+ * documents this epic exists for is millions of strings held at once —
635
+ * before a single row has been projected. A keyset page costs the same per
636
+ * row and bounds what is resident, so walking a model is a walk rather than
637
+ * an allocation (#2816).
638
+ */
639
+ recordIdsAfter(modelName: string, after: string | null, limit: number): string[];
640
+ /**
641
+ * Fold an overlay entry that arrived from the server.
642
+ *
643
+ * A model outside this device's hydration scope is DROPPED here rather than
644
+ * folded: the device declined that model because it has no room for it, and
645
+ * a row of it landing anyway would turn "this model is not held here" into
646
+ * "this model holds one record" — a merged view that reads like the document
647
+ * and is not it.
648
+ */
649
+ applyRemote(modelName: string, entry: OverlayRecordEntry): void;
650
+ /**
651
+ * Run `fn` as ONE commit. A catch-up fold of a whole overlay is a single
652
+ * transaction rather than one per record — the difference between one commit
653
+ * and thousands.
654
+ */
655
+ transaction<T>(fn: () => T): T;
656
+ /**
657
+ * Commit a LOCAL write: the merged row, the pending op, and the OVERLAY
658
+ * MUTATION that op stands for all land together, or none of them does.
659
+ * The returned sequence is what an `update.ack` later prunes against.
660
+ *
661
+ * The mutation is stored, not just the field names it touched, because the
662
+ * merged row cannot reproduce it: materializing a row loses the difference
663
+ * between a patch and a `_replace` create, turns an explicit `null` unset
664
+ * into an absent key, and drops stringset member tombstones. Those shapes
665
+ * survive only in the overlay — and the overlay's own persistence is a Yjs
666
+ * update flushed asynchronously AFTER this commit, so a crash in between
667
+ * would otherwise leave a pending op whose write nothing could reconstruct:
668
+ * the client would claim its sequence on the next catch-up frame, be acked
669
+ * for a write it never sent, and drop it.
670
+ */
671
+ commitLocalWrite(modelName: string, mutation: OverlayMutation, pending: PendingOpInput): number;
672
+ /** The next local sequence: one past the highest ever issued. */
673
+ nextSeq(): number;
674
+ /**
675
+ * The highest sequence this client has ever committed locally — what an
676
+ * outbound update frame claims, and what an `update.ack` has to reach before
677
+ * the document has nothing unsynced left.
678
+ */
679
+ highestLocalSeq(): number;
680
+ pendingOps(): PendingOp[];
681
+ /**
682
+ * Prune at or below the server's contiguous-sequence high-water mark.
683
+ * A lower (out-of-order or replayed) ack never lowers the mark and never
684
+ * resurrects a pruned op.
685
+ */
686
+ prunePendingOps(maxContiguousSeq: number): void;
687
+ ackedSeq(): number;
688
+ epoch(): number;
689
+ setEpoch(epoch: number): void;
690
+ lastSyncAt(): number | null;
691
+ setLastSyncAt(at: number): void;
692
+ /**
693
+ * The offline write window this document is held to (#2816, behavior 18),
694
+ * as the server last reported it — the deployment default until one has.
695
+ */
696
+ offlineWindowDays(): number;
697
+ /**
698
+ * Record that this client was in touch with the server, and (when the
699
+ * server said so) the window it is held to.
700
+ *
701
+ * Persisted together because the session that enforces the window is the one
702
+ * that cannot ask for it: a client that starts offline reads both back out
703
+ * of its own database.
704
+ */
705
+ noteSync(at: number, windowDays?: number | null): void;
706
+ /**
707
+ * Record the window without claiming a sync (#2816, behavior 18).
708
+ *
709
+ * The handshake carries the window whatever else it says, including to a
710
+ * client that is behind by whole epochs and cannot act on the rest of the
711
+ * frame. Taking the number is right there; moving the mark it is measured
712
+ * FROM is not — a frame is proof of contact, not proof that this client's
713
+ * merged view is the room's state, and a document past its window would
714
+ * become writable again on the strength of a handshake whose catch-up then
715
+ * failed.
716
+ */
717
+ noteOfflineWindow(windowDays: number | null | undefined): void;
718
+ /**
719
+ * How far this client's clock is behind the server's, in milliseconds
720
+ * (#2816, behavior 17).
721
+ *
722
+ * Zero until one has been measured — which is why {@link clockOffsetKnown}
723
+ * exists separately: a measured zero and never having measured are the same
724
+ * number and very different facts, and the replay resolver refuses to call a
725
+ * write "clearly older" on the second.
726
+ */
727
+ clockOffset(): number;
728
+ /** Whether the offset above was ever measured against a server. */
729
+ clockOffsetKnown(): boolean;
730
+ /** Record the offset measured against server time while connected. */
731
+ noteClockOffset(offsetMs: number): void;
732
+ /** Now, on the server's clock as far as this client can tell. */
733
+ correctedNow(now?: number): number;
734
+ /**
735
+ * Forget specific pending ops (#2816, behavior 17).
736
+ *
737
+ * The one caller is a replay whose recency resolution DROPPED them: the
738
+ * online write won, so the op must never be replayed — and leaving it in the
739
+ * log would have the next epoch move carry it anyway, and would report the
740
+ * document as holding unsynced changes for good. The merged row it wrote
741
+ * stays: it is what the client is showing, and the next update from the
742
+ * server corrects it.
743
+ */
744
+ forgetPendingOps(seqs: number[]): void;
745
+ /**
746
+ * Rewrite pending ops that survived a replay resolution only in PART
747
+ * (#2816, behavior 17).
748
+ *
749
+ * The same caller as {@link forgetPendingOps}, for the ops it narrowed
750
+ * rather than dropped: a patch that touched two fields, one of which an
751
+ * online write clearly beat. The row must be rewritten to the surviving
752
+ * delta — its field list AND the mutation it stands for — because the
753
+ * mutation is what the restore path puts back on the overlay after a crash,
754
+ * and putting the whole one back would resurrect exactly the field this
755
+ * resolution decided against.
756
+ */
757
+ narrowPendingOps(ops: Array<Pick<PendingOp, "seq" | "fields" | "mutation">>): void;
758
+ completedChunks(buildId: string): string[];
759
+ markChunkComplete(buildId: string, chunkKey: string): void;
760
+ /**
761
+ * Throw away a merged view that cannot be repaired (#2816, behavior 12).
762
+ *
763
+ * The one caller is a client whose sealed-overlay chain was refused: the
764
+ * rows it holds are the state of an epoch the room has left behind, and no
765
+ * available artifact turns them into the current state. It reloads from a
766
+ * base snapshot instead — and a snapshot is a set of rows, not a diff, so
767
+ * loading it over these would leave behind every record deleted since. The
768
+ * chunk marks go with them: a mark whose rows are gone is exactly what makes
769
+ * a resumed load skip a chunk and finish incomplete.
770
+ *
771
+ * What survives is what this client owes the server. Unacknowledged writes
772
+ * are replayed onto the epoch it lands on, and the acked high-water mark
773
+ * keeps their sequences from colliding with fresh ones — dropping either
774
+ * would lose writes the app has already been told were saved.
775
+ */
776
+ discardMergedView(): void;
777
+ private rows;
778
+ private scalar;
779
+ }
780
+
220
781
  interface ITransactionalDatabaseOperations {
221
782
  insert(modelName: string, data: any): Promise<void>;
222
783
  delete(modelName: string, id: string): Promise<void>;
@@ -272,6 +833,32 @@ declare abstract class DatabaseEngine {
272
833
  */
273
834
  deleteByDocumentId(_modelName: string, _docId: string): Promise<void>;
274
835
  getTableName(_modelName: string): string;
836
+ /**
837
+ * A SYNCHRONOUS SQL executor over this engine's database, used by the
838
+ * large-document (format 2) record store. That store commits a merged
839
+ * record row and its pending-operation entry as one transaction — the
840
+ * barrier a `save()` resolves behind — which the async `query()` API cannot
841
+ * express. Engines whose underlying SQLite is synchronous (better-sqlite3,
842
+ * SQL.js) provide it; anything else leaves it undefined and cannot host a
843
+ * large document.
844
+ */
845
+ createFormat2Sql?: () => Format2StoreSql;
846
+ /**
847
+ * Whether what {@link createFormat2Sql} writes survives this process.
848
+ *
849
+ * On a large document the merged record view IS the document: the Y.Doc
850
+ * holds one epoch's overlay, and everything else — including a record a
851
+ * `save()` has already resolved, and the pending write that still owes it to
852
+ * the server — lives only in this store. An engine whose database is in
853
+ * memory therefore cannot host one: a reload does not cost a cache, it costs
854
+ * the document and the writes nobody has acknowledged yet.
855
+ *
856
+ * The platform is asked separately about its storage quota; this answers the
857
+ * half only the engine knows. An engine that does not implement it is taken
858
+ * at the platform's word, which is what keeps hosts with storage of their
859
+ * own — a worker handed a file store, a test — working unchanged.
860
+ */
861
+ format2StorageIsDurable?: () => boolean;
275
862
  withTransaction<T>(_callback: (operations: ITransactionalDatabaseOperations) => Promise<T>): Promise<T>;
276
863
  }
277
864
 
@@ -432,6 +1019,129 @@ declare class DocumentQueryTranslator {
432
1019
  private buildDocumentClause;
433
1020
  }
434
1021
 
1022
+ /**
1023
+ * The binding state one ORM instance owns (#3039).
1024
+ *
1025
+ * Everything here used to live as `BaseModel` statics, so two `initJsBao`
1026
+ * instances in one process shared one engine, one set of connected documents,
1027
+ * one set of `subscribe()` listeners and one default-document mapping. The
1028
+ * static surface survives as accessors that resolve the acting class's
1029
+ * context; the state itself lives here, one per instance.
1030
+ *
1031
+ * Deliberately imports NOTHING at runtime: `BaseModel` reaches for this module
1032
+ * during its own evaluation, so a value import of `ModelRegistry` (which
1033
+ * imports `BaseModel`) would be a cycle that bites at module-init time.
1034
+ */
1035
+ declare class OrmContext {
1036
+ /** Declared before the field that reads it: static initialization is ordered. */
1037
+ private static instanceCounter;
1038
+ /**
1039
+ * Which instance this is, for diagnostics. A process holding several clients
1040
+ * needs a way to say WHICH one a warning is about, so every context takes the
1041
+ * next number as it is created and keeps it for its lifetime.
1042
+ */
1043
+ readonly instanceId: number;
1044
+ /** The engine this instance's models read and write through. */
1045
+ dbEngine: DatabaseEngine | null;
1046
+ connectedDocuments: Map<string, {
1047
+ yDoc: Y.Doc;
1048
+ permissionHint: DocumentPermissionHint;
1049
+ }>;
1050
+ /** `docId_modelName` → the Y.Map that document holds for that model. */
1051
+ documentYMaps: Map<string, Y.Map<any>>;
1052
+ /** `subscribe()` callbacks, by model name. */
1053
+ listenersMap: Map<string, Set<() => void>>;
1054
+ modelNameToDefaultDocId: Map<string, string>;
1055
+ globalDefaultDocId: string | undefined;
1056
+ defaultDocChangedListeners: Set<(payload: {
1057
+ previous?: string;
1058
+ current?: string;
1059
+ }) => void>;
1060
+ modelDocMappingChangedListeners: Set<(payload: {
1061
+ modelName: string;
1062
+ previous?: string;
1063
+ current?: string;
1064
+ }) => void>;
1065
+ /** Format-2 ("large document", #2816) record stores by document id. */
1066
+ format2Documents: Map<string, Format2RecordStore>;
1067
+ /** Format-2 epoch moves in flight (#2816). */
1068
+ format2Transitions: Map<string, Promise<void>>;
1069
+ /**
1070
+ * Nested-Y.Map stringset fields this context has already observed. Per
1071
+ * context, not global: a Y.Doc reconnected to a fresh instance has to be
1072
+ * observed again by that instance, and a global guard would silently
1073
+ * suppress it.
1074
+ */
1075
+ observedStringSetMaps: WeakSet<Y.Map<any>>;
1076
+ /**
1077
+ * The registry this instance's schema lookups use. `null` on the standing
1078
+ * default context, where callers fall back to the global singleton.
1079
+ */
1080
+ modelRegistry: ModelRegistry | null;
1081
+ /** Model classes routed to this context. */
1082
+ boundModels: Set<any>;
1083
+ /** True once {@link destroy} has run; bound classes keep pointing here. */
1084
+ destroyed: boolean;
1085
+ /** Yjs `unobserve` thunks by owning document id. */
1086
+ private observerDisposers;
1087
+ /** Teardown the owner (the document manager) registers for `destroy()`. */
1088
+ private teardownHooks;
1089
+ private static readonly NO_DOCUMENT_KEY;
1090
+ /**
1091
+ * Record how to remove an observer this context installed. Every `observe()`
1092
+ * in the ORM pairs with one of these: without it a torn-down instance's
1093
+ * callbacks keep firing on a Y.Doc that outlives it.
1094
+ */
1095
+ registerObserverDisposer(docId: string | null | undefined, dispose: () => void): void;
1096
+ /** Remove every observer this context installed for `docId`. */
1097
+ disposeObserversForDocument(docId: string): void;
1098
+ private disposeAllObservers;
1099
+ /** Run `hook` as part of `destroy()` (the format-2 binding registers here). */
1100
+ onDestroy(hook: () => void | Promise<void>): void;
1101
+ /** The record store for `docId`, or `null` when the document is format 1. */
1102
+ getFormat2Store(docId: string | null | undefined): Format2RecordStore | null;
1103
+ /**
1104
+ * The epoch move a write to `docId` has to wait out (#2816, behavior 10), or
1105
+ * `null` when none is in flight.
1106
+ */
1107
+ format2WritesHeld(docId: string | null | undefined): Promise<void> | null;
1108
+ /** Run an epoch move with `docId`'s local writes held back (#2816). */
1109
+ holdFormat2Writes<T>(docId: string, run: () => Promise<T>): Promise<T>;
1110
+ /** Notify `subscribe()` callbacks registered on this context for `modelName`. */
1111
+ notifyListeners(modelName: string, log?: (message: string) => void, onError?: (error: unknown) => void): void;
1112
+ setModelDefaultDocumentId(modelName: string, docId: string): void;
1113
+ removeModelDefaultDocumentId(modelName: string): void;
1114
+ clearModelDefaultDocumentIds(): void;
1115
+ setGlobalDefaultDocumentId(docId: string): void;
1116
+ clearGlobalDefaultDocumentId(): void;
1117
+ getModelDefaultDocumentMapping(): Record<string, string>;
1118
+ getDocumentIdForModel(modelName: string): string | undefined;
1119
+ getGlobalDefaultDocumentId(): string | undefined;
1120
+ onDefaultDocChanged(listener: (payload: {
1121
+ previous?: string;
1122
+ current?: string;
1123
+ }) => void): () => void;
1124
+ onModelDocMappingChanged(listener: (payload: {
1125
+ modelName: string;
1126
+ previous?: string;
1127
+ current?: string;
1128
+ }) => void): () => void;
1129
+ /** Drop the model→document defaults that point at a disconnected document. */
1130
+ clearMappingsForDocId(docId: string): void;
1131
+ private announceDefaultDoc;
1132
+ private announceModelDocMapping;
1133
+ /**
1134
+ * Tear this instance down: observers off, engine destroyed, state cleared.
1135
+ *
1136
+ * Deliberately does NOT unbind its model classes. They keep resolving this
1137
+ * context, whose engine is now null, so an operation through them throws the
1138
+ * ordinary not-initialized error instead of silently landing on whatever
1139
+ * other instance happens to be live (#3039, behavior 7). Only a later
1140
+ * `initJsBao` may rebind a class whose context is destroyed.
1141
+ */
1142
+ destroy(): Promise<void>;
1143
+ }
1144
+
435
1145
  interface StringSetMembership {
436
1146
  field: string;
437
1147
  contains: string;
@@ -500,19 +1210,59 @@ declare class RecordNotFoundError extends Error {
500
1210
  declare function generateULID(): string;
501
1211
  declare class BaseModel implements StringSetChangeTracker {
502
1212
  static modelName?: string;
503
- private static listenersMap;
504
- private static modelNameToDefaultDocId;
505
- private static globalDefaultDocId;
506
- private static defaultDocChangedListeners;
507
- private static modelDocMappingChangedListeners;
1213
+ /**
1214
+ * The ORM instance this class is bound to (#3039), set by `initJsBao`.
1215
+ *
1216
+ * Read through the static prototype chain by {@link resolveOrmContext}, so a
1217
+ * subclass inherits its parent's binding. Never reassigned by `destroy()`:
1218
+ * a destroyed instance's classes keep pointing at their tombstone rather
1219
+ * than falling back to a surviving instance.
1220
+ */
1221
+ static _ormContext?: OrmContext;
1222
+ /**
1223
+ * Every field below is per-ORM-instance state (#3039). The accessors keep
1224
+ * the static surface consumers (and this file's own call sites) already use
1225
+ * while the state itself lives on the acting class's {@link OrmContext}.
1226
+ */
1227
+ protected static get listenersMap(): Map<string, Set<() => void>>;
1228
+ protected static set listenersMap(value: Map<string, Set<() => void>>);
1229
+ protected static get modelNameToDefaultDocId(): Map<string, string>;
1230
+ protected static set modelNameToDefaultDocId(value: Map<string, string>);
1231
+ protected static get globalDefaultDocId(): string | undefined;
1232
+ protected static set globalDefaultDocId(value: string | undefined);
1233
+ protected static get defaultDocChangedListeners(): Set<(payload: {
1234
+ previous?: string;
1235
+ current?: string;
1236
+ }) => void>;
1237
+ protected static get modelDocMappingChangedListeners(): Set<(payload: {
1238
+ modelName: string;
1239
+ previous?: string;
1240
+ current?: string;
1241
+ }) => void>;
508
1242
  private static readonly DEFAULT_LEGACY_DOC_ID;
509
- protected static dbInstance: DatabaseEngine | null;
510
- protected static connectedDocuments: Map<string, {
1243
+ protected static get dbInstance(): DatabaseEngine | null;
1244
+ protected static set dbInstance(value: DatabaseEngine | null);
1245
+ protected static get connectedDocuments(): Map<string, {
511
1246
  yDoc: Y.Doc;
512
1247
  permissionHint: DocumentPermissionHint;
513
1248
  }>;
514
- protected static documentYMaps: Map<string, Y.Map<any>>;
515
- private static _observedStringSetMaps;
1249
+ protected static set connectedDocuments(value: Map<string, {
1250
+ yDoc: Y.Doc;
1251
+ permissionHint: DocumentPermissionHint;
1252
+ }>);
1253
+ protected static get documentYMaps(): Map<string, Y.Map<any>>;
1254
+ protected static set documentYMaps(value: Map<string, Y.Map<any>>);
1255
+ /**
1256
+ * Format-2 ("large document", #2816) documents by id, each with the record
1257
+ * store that holds its merged view. Presence in this map IS format-2-ness on
1258
+ * the client: a document without an entry takes the legacy path unchanged.
1259
+ */
1260
+ private static get format2Documents();
1261
+ /**
1262
+ * Large documents whose epoch overlay is being replaced right now (#2816,
1263
+ * behavior 10), each with the promise that settles when the move is done.
1264
+ */
1265
+ protected static get format2Transitions(): Map<string, Promise<void>>;
516
1266
  private _localChanges;
517
1267
  private _isDirty;
518
1268
  private _isLoadingFromYjs;
@@ -529,6 +1279,53 @@ declare class BaseModel implements StringSetChangeTracker {
529
1279
  type: string;
530
1280
  static setLogLevel(level: LogLevel): void;
531
1281
  static getLogLevel(): LogLevel;
1282
+ /**
1283
+ * Mark a connected document as format 2 and bind the record store that holds
1284
+ * its merged view. Called by `connectDocument(..., { documentFormat: 2 })`.
1285
+ */
1286
+ static registerFormat2Document(docId: string, store: Format2RecordStore): void;
1287
+ static unregisterFormat2Document(docId: string): void;
1288
+ /**
1289
+ * Drop every format-2 binding. Called when the whole runtime is reset: the
1290
+ * stores run over the engine's SQL executor, so one left behind would answer
1291
+ * reads out of a destroyed database.
1292
+ */
1293
+ static clearFormat2Documents(): void;
1294
+ /** The record store for `docId`, or `null` when the document is format 1. */
1295
+ static getFormat2Store(docId: string | null | undefined): Format2RecordStore | null;
1296
+ static isFormat2Document(docId: string | null | undefined): boolean;
1297
+ /**
1298
+ * Run an epoch move with `docId`'s local writes held back (#2816,
1299
+ * behavior 10).
1300
+ *
1301
+ * A move replaces the document's overlay: the sealed Y.Doc is destroyed once
1302
+ * the fresh one is bound. A write that resolved its target document before
1303
+ * the move and lands its overlay entry after would land it in the doomed
1304
+ * doc — and it could never be recovered, because `_pending_ops` records
1305
+ * which fields a write touched, not the values it wrote. A write that starts
1306
+ * during the move therefore waits here and resolves the document again on
1307
+ * the other side. (Concurrent moves are serialized for the same reason.)
1308
+ */
1309
+ static holdFormat2Writes<T>(docId: string, run: () => Promise<T>): Promise<T>;
1310
+ /**
1311
+ * The move a write to `docId` has to wait out, or `null` when none is in
1312
+ * flight — which is every write on every document but the one being rotated.
1313
+ */
1314
+ static format2WritesHeld(docId: string | null | undefined): Promise<void> | null;
1315
+ /**
1316
+ * The record store, Y.Doc and model map a format-2 write should land on,
1317
+ * having waited out any epoch move in flight (#2816, behavior 10).
1318
+ *
1319
+ * Resolves to the callers' own values in the overwhelmingly common case that
1320
+ * no move is running, so an ordinary write pays one map lookup. When a move
1321
+ * IS running, the write waits for it and the freshly bound overlay — and its
1322
+ * rebuilt store — are what it lands on.
1323
+ */
1324
+ static resolveFormat2Target(docId: string, modelName: string, store: Format2RecordStore, yDoc: Y.Doc, modelYMap: Y.Map<any>): Promise<{
1325
+ store: Format2RecordStore;
1326
+ yDoc: Y.Doc;
1327
+ modelYMap: Y.Map<any>;
1328
+ }>;
532
1329
  static setModelDefaultDocumentId(modelName: string, docId: string): void;
533
1330
  static removeModelDefaultDocumentId(modelName: string): void;
534
1331
  static clearModelDefaultDocumentIds(): void;
@@ -578,10 +1375,24 @@ declare class BaseModel implements StringSetChangeTracker {
578
1375
  hasFieldChanged(fieldKey: string): boolean;
579
1376
  markStringSetChange(fieldName: string, operation: "add" | "remove" | "clear", value?: string): void;
580
1377
  private getStringSetCurrentValues;
1378
+ /** The schema default for a field, or `undefined` when it has none. */
1379
+ private schemaDefaultFor;
581
1380
  private getStringSetFromYjs;
582
1381
  private getOrCreateStringSet;
583
1382
  static initialize(_yDoc: Y.Doc, _db: DatabaseEngine): Promise<void>;
584
- static initializeForDocument(yDoc: Y.Doc, db: DatabaseEngine, docId: string, permissionHint: DocumentPermissionHint): Promise<void>;
1383
+ /**
1384
+ * Initialize this model for a connected document.
1385
+ *
1386
+ * `documentFormat` (#2816) decides where the records come from. On a legacy
1387
+ * document the Y.Map IS the records, so this loads them into the query
1388
+ * tables and observes the map. On a format-2 document the Y.Map is an epoch
1389
+ * OVERLAY of flat `recordId/field` entries — walking it as records would
1390
+ * read a scalar (or a `null` unset) as a record and mint junk rows, and
1391
+ * observing it would run every overlay change through the legacy
1392
+ * record-shaped path. There the tables are prepared here and hydrated and
1393
+ * observed by `Format2DocumentBinding` from the merged view instead.
1394
+ */
1395
+ static initializeForDocument(yDoc: Y.Doc, db: DatabaseEngine, docId: string, permissionHint: DocumentPermissionHint, documentFormat?: 1 | 2): Promise<void>;
585
1396
  static cleanupDocumentData(docId: string): Promise<void>;
586
1397
  static subscribe(callback: () => void): () => void;
587
1398
  protected static notifyListeners(): void;
@@ -616,9 +1427,88 @@ declare class BaseModel implements StringSetChangeTracker {
616
1427
  protected static _buildKeyFromValues(fields: string[], keyValues: any[], modelName: string, constraintName: string): string | null;
617
1428
  protected _buildUniqueKey(fields: string[], data: Record<string, any>, modelName: string, constraintName: string): string | null;
618
1429
  save(options?: SaveOptions): Promise<void>;
1430
+ /**
1431
+ * Save onto a format-2 (large) document (#2816).
1432
+ *
1433
+ * The record is NOT written as a nested `Y.Map`: the epoch doc carries flat
1434
+ * field-level overlay entries, and the merged `records` view is what reads
1435
+ * come from. Existence, `upsertOn` and uniqueness are therefore resolved
1436
+ * against that view, not against the doc, which is what makes a base-only
1437
+ * record patchable instead of silently re-created.
1438
+ *
1439
+ * The merged row and the `_pending_ops` entry commit TOGETHER and BEFORE the
1440
+ * overlay lands in the Y.Doc: that ordering is the commit barrier a resolved
1441
+ * `save()` stands behind, and it keeps a crash in between recoverable (the
1442
+ * durable pending op replays the write) rather than divergent.
1443
+ */
1444
+ private saveFormat2;
1445
+ /**
1446
+ * Mirror one merged-view row into the engine's per-model query tables, so
1447
+ * `query()` / `aggregate()` answer from the same state `find()` does. The
1448
+ * merged view remains the source; this is its derived query projection, and
1449
+ * it is awaited so a resolved save is immediately queryable.
1450
+ */
1451
+ /**
1452
+ * Project one merged-view row into the engine's per-model query tables, so
1453
+ * `query()` / `aggregate()` answer from the same state `find()` does. The
1454
+ * merged view stays the source; this is its derived query projection.
1455
+ *
1456
+ * Called (awaited) by the local write path — which is what makes a resolved
1457
+ * save immediately queryable — and enqueued by the overlay observer for
1458
+ * updates that arrive from the sync layer.
1459
+ */
1460
+ static projectFormat2Record(docId: string, recordId: string,
1461
+ /**
1462
+ * `notify: false` withholds the listener notification, for a caller
1463
+ * projecting many records at once (#2816): hydrating a model would
1464
+ * otherwise tell every subscriber once per record, which on a large
1465
+ * document is a re-render per row of the document.
1466
+ */
1467
+ options?: {
1468
+ notify?: boolean;
1469
+ }): Promise<void>;
1470
+ /**
1471
+ * Remove a PAGE of records from the query tables, without consulting the
1472
+ * merged view (#2816).
1473
+ *
1474
+ * The discard path: the rows are erased BEFORE the merged view is thrown
1475
+ * away, because the view is the only place that still names them. Projecting
1476
+ * would re-insert them — the record is still there at that point.
1477
+ */
1478
+ static deleteFormat2QueryRows(recordIds: readonly string[]): Promise<void>;
1479
+ /**
1480
+ * Project a PAGE of merged-view rows into the query tables (#2816).
1481
+ *
1482
+ * The hydration path: one notification for the page rather than one per
1483
+ * record, because a model being hydrated has no intermediate states worth
1484
+ * telling anyone about — and on a large document the per-record version is a
1485
+ * subscriber callback per row of the document.
1486
+ */
1487
+ static projectFormat2Records(docId: string, recordIds: readonly string[]): Promise<void>;
619
1488
  delete(): Promise<void>;
1489
+ /**
1490
+ * Delete from a format-2 (large) document (#2816): a `_deleted` tombstone in
1491
+ * the overlay plus the merged-view removal, committed with its pending op
1492
+ * before the overlay lands, exactly as `saveFormat2` orders its write.
1493
+ */
1494
+ private deleteFormat2;
620
1495
  protected getCurrentJSState(): Record<string, any>;
621
1496
  protected toJSON(): Record<string, any>;
1497
+ /**
1498
+ * Refuse a read through a class whose ORM instance has been destroyed
1499
+ * (#3039, the tombstone).
1500
+ *
1501
+ * `query()`, `count()` and `aggregate()` already refuse: they read the
1502
+ * engine, which a destroyed instance has nulled. The readers that walk the
1503
+ * connected documents instead — `find`, `findAll`, `findByUnique` — would
1504
+ * walk maps `destroy()` emptied and report absence, which a caller cannot
1505
+ * tell apart from a database that genuinely holds no such record. So they
1506
+ * say so instead.
1507
+ *
1508
+ * Only a DESTROYED context is refused. A class that was never bound still
1509
+ * reads as empty before any `initJsBao`, exactly as it always has.
1510
+ */
1511
+ private static assertInstanceAlive;
622
1512
  static find<T extends BaseModel>(this: new (...args: any[]) => T, id: string): Promise<T | null>;
623
1513
  /**
624
1514
  * Document-style query API - returns paginated results
@@ -698,6 +1588,27 @@ declare class BaseModel implements StringSetChangeTracker {
698
1588
  */
699
1589
  static count(this: new (...args: any[]) => any, filter?: DocumentFilter, options?: Pick<QueryOptions, "documents">): Promise<number>;
700
1590
  static findAll<T extends BaseModel>(this: new (...args: any[]) => T): Promise<T[]>;
1591
+ /**
1592
+ * Resolve a record by its unique-constraint field values via an engine query.
1593
+ * Used as a fallback when the `_uniqueIdx_<model>_<constraint>` map has no
1594
+ * entry for a key.
1595
+ *
1596
+ * The index map is a cache over the SQLite projection, not the source of
1597
+ * truth: every record is loaded into the projection on `initializeForDocument`
1598
+ * regardless of index presence, but a record written by a client predating
1599
+ * index maintenance (legacy data) is missing from the index. Such a record
1600
+ * silently escapes both `upsertByUnique` lookups and the `save()` uniqueness
1601
+ * check. A query over the constraint fields finds it reliably.
1602
+ *
1603
+ * Contract: RESOLVE ONLY. Returns the owning record (with `_metaDocId` set to
1604
+ * the document that holds it, via `queryOne`) or `null`. Callers decide
1605
+ * whether to heal the missing index entry (write paths) and do their own
1606
+ * logging — this method never mutates the index, so read paths stay
1607
+ * read-only. Mirrors `buildUniqueKey`: a null/undefined value in any
1608
+ * constraint field means the record does not participate in the constraint,
1609
+ * so no query runs.
1610
+ */
1611
+ private static _resolveByUniqueFallback;
701
1612
  static findByUnique<T extends BaseModel>(this: typeof BaseModel & (new (...args: any[]) => T), constraintName: string, value: any | any[]): Promise<T | null>;
702
1613
  static upsertByUnique<T extends BaseModel>(this: typeof BaseModel & (new (...args: any[]) => T), constraintName: string, uniqueLookupValue: any | any[], dataToUpsert: Partial<Omit<InstanceType<new (...args: any[]) => T>, keyof BaseModel | "toJSON">> & {
703
1614
  id?: string;
@@ -716,14 +1627,14 @@ declare class BaseModel implements StringSetChangeTracker {
716
1627
  * member arrives via Y.applyUpdate, or a local per-member set/delete).
717
1628
  * Idempotent — re-calling on the same Y.Map is a no-op.
718
1629
  */
719
- protected static observeStringSetMapOnce(nestedMap: Y.Map<any>): void;
1630
+ protected static observeStringSetMapOnce(nestedMap: Y.Map<any>, docId?: string | null): void;
720
1631
  /**
721
1632
  * Walk the schema's stringset fields on `recordYMap` and observe any nested
722
1633
  * Y.Map values. Called from observer setup and from the parent observer body
723
1634
  * so newly-arrived stringset Y.Maps (local migration or remote create) get
724
1635
  * an observer too.
725
1636
  */
726
- protected static attachStringSetObserversToRecord(recordYMap: Y.Map<any>, schema: any): void;
1637
+ protected static attachStringSetObserversToRecord(recordYMap: Y.Map<any>, schema: any, docId?: string | null): void;
727
1638
  /**
728
1639
  * Sets up deep observation on a nested YMap to sync field-level changes to the database
729
1640
  */
@@ -745,8 +1656,23 @@ declare class ModelRegistry {
745
1656
  private modelOptions;
746
1657
  private activeSessionModels;
747
1658
  private dbEngine;
748
- private constructor();
1659
+ constructor();
749
1660
  static getInstance(): ModelRegistry;
1661
+ /**
1662
+ * A registry for ONE `initJsBao` instance (#3039).
1663
+ *
1664
+ * The global singleton stays the registration target for
1665
+ * `attachAndRegisterModel` / `createModelClass`; this is the per-instance
1666
+ * view the ORM actually resolves schemas through, so two clients that
1667
+ * register different models under the same name no longer overwrite each
1668
+ * other's include translation and relationship validation.
1669
+ *
1670
+ * With explicit `models`, only those classes are in it. With `models`
1671
+ * undefined the legacy session semantics are kept exactly: the global
1672
+ * registry is SNAPSHOT at init time, so a model registered afterwards is not
1673
+ * silently adopted by an already-running instance.
1674
+ */
1675
+ static createInstanceRegistry(modelClasses?: (typeof BaseModel)[]): ModelRegistry;
750
1676
  registerModel(modelClass: any, options: ModelOptions, fields: Map<string, FieldOptions>): void;
751
1677
  getModelClass(name: string): typeof BaseModel | undefined;
752
1678
  getModelOptions(name: string): ModelOptions | undefined;
@@ -755,7 +1681,9 @@ declare class ModelRegistry {
755
1681
  private getModelNameFromClass;
756
1682
  setExplicitModelsForSession(modelClasses?: (typeof BaseModel)[]): void;
757
1683
  initializeAll(yDoc: Y.Doc, dbEngine: DatabaseEngine): Promise<void>;
758
- initializeAllForDocument(yDoc: Y.Doc, dbEngine: DatabaseEngine, docId: string, permissionHint: DocumentPermissionHint): Promise<void>;
1684
+ initializeAllForDocument(yDoc: Y.Doc, dbEngine: DatabaseEngine, docId: string, permissionHint: DocumentPermissionHint,
1685
+ /** #2816 — `2` prepares the tables without reading the Y.Map as records. */
1686
+ documentFormat?: 1 | 2): Promise<void>;
759
1687
  removeDocumentData(docId: string, dbEngine: DatabaseEngine): Promise<void>;
760
1688
  initializeRelationships(): Promise<void>;
761
1689
  private addPrototypeMethod;
@@ -764,48 +1692,579 @@ declare class ModelRegistry {
764
1692
  private validateSessionModels;
765
1693
  }
766
1694
 
767
- interface InitJsBaoOptions {
768
- databaseConfig: DatabaseConfig;
769
- /**
770
- * Optional array of model classes to initialize.
771
- * If not provided, the ORM will rely on models being registered
772
- * via attachAndRegisterModel (assuming they have been imported by the application).
773
- */
774
- models?: (typeof BaseModel)[];
1695
+ /**
1696
+ * The on-the-wire shape of a snapshot chunk (#2816, Phase 4).
1697
+ *
1698
+ * Shared by BOTH ends, like the overlay fold: the Durable Object writes these
1699
+ * bytes and the client materializes them, so one implementation encodes,
1700
+ * verifies and decodes them. A snapshot that meant something slightly
1701
+ * different on the two ends would put records into a client's authoritative
1702
+ * table that the server never wrote — silently, because on a format-2 document
1703
+ * there is no whole-document comparison left to catch it.
1704
+ *
1705
+ * ## Why chunks, and why they are verified
1706
+ *
1707
+ * A 2GB document cannot be one object: the load has to survive a dropped
1708
+ * connection, report progress, and let a model answer queries before the rest
1709
+ * has arrived. So a snapshot is a set of per-model gzip ndjson chunks plus a
1710
+ * manifest naming each one's row count and digest, and a chunk is checked
1711
+ * against its entry BEFORE it is decoded — a mismatch is a typed refusal the
1712
+ * loader retries on, cheaper than inflating bytes that are already wrong.
1713
+ *
1714
+ * ## The line format
1715
+ *
1716
+ * One `⟨id⟩\t⟨_data JSON verbatim⟩` line per record. Verbatim matters: the
1717
+ * data comes out of SQLite's `json_patch` and goes straight back into the
1718
+ * other end's `records` table, so re-serializing it here would be a chance to
1719
+ * round a number the server stored exactly. Compact JSON has no raw newline or
1720
+ * tab in it, and a row that somehow held one is refused rather than written —
1721
+ * it would come back as two records, or as one with its tail lost.
1722
+ */
1723
+ /** One row of the authoritative `records` table. */
1724
+ interface SnapshotRow {
1725
+ _type: string;
1726
+ _id: string;
1727
+ _data: string;
775
1728
  }
776
- interface InitJsBaoResult {
777
- dbEngine: DatabaseEngine;
778
- modelRegistry: ModelRegistry;
779
- connectDocument: (docId: string, yDoc: Y.Doc, permissionHint: DocumentPermissionHint) => Promise<void>;
780
- disconnectDocument: (docId: string) => Promise<void>;
781
- getConnectedDocuments: () => Map<string, ConnectedDocument>;
782
- isDocumentConnected: (docId: string) => boolean;
783
- onDocumentConnectionChange: (callback: DocumentConnectionCallback) => () => void;
784
- addDocumentModelMapping: (modelName: string, docId: string) => void;
785
- removeDocumentModelMapping: (modelName: string) => void;
786
- clearDocumentModelMappings: () => void;
787
- setDefaultDocumentId: (docId: string) => void;
788
- clearDefaultDocumentId: () => void;
789
- getDocumentModelMapping: () => Record<string, string>;
790
- getDocumentIdForModel: (modelName: string) => string | undefined;
791
- getDefaultDocumentId: () => string | undefined;
792
- onDefaultDocChanged: (listener: (payload: {
793
- previous?: string;
794
- current?: string;
795
- }) => void) => () => void;
796
- onModelDocMappingChanged: (listener: (payload: {
797
- modelName: string;
798
- previous?: string;
799
- current?: string;
800
- }) => void) => () => void;
1729
+ /** What the manifest says about one chunk: enough to verify it standalone. */
1730
+ interface SnapshotChunkEntry {
1731
+ /** R2 key, inside the document's own `{appId}/{docId}/` prefix. */
1732
+ key: string;
1733
+ /** The model whose records this chunk holds. */
1734
+ model: string;
1735
+ /** Stored (compressed) length, so a Range loader knows what it is owed. */
1736
+ bytes: number;
1737
+ /**
1738
+ * Uncompressed ndjson length.
1739
+ *
1740
+ * What the rows actually cost once they are somewhere other than a gzip
1741
+ * stream — which is the number a device deciding whether it can hold this
1742
+ * document needs. Compressed length is not a proxy for it: highly
1743
+ * compressible JSON is exactly the shape these chunks hold. Optional
1744
+ * because a manifest written before it was recorded carries none.
1745
+ */
1746
+ rawBytes?: number;
1747
+ rows: number;
1748
+ firstId: string;
1749
+ lastId: string;
1750
+ /** Hex SHA-256 of the STORED bytes — checkable before decompressing. */
1751
+ sha256: string;
801
1752
  }
802
1753
  /**
803
- * Initializes the js-bao system with multi-document support.
804
- * Sets up the database engine and prepares for document connections.
1754
+ * What one build wrote: the chunks, and everything a loader needs before the
1755
+ * first of them lands.
1756
+ *
1757
+ * `epoch` is the epoch this snapshot is a BASE for — the state as of that
1758
+ * epoch's start, give or take the torn read the overlays after it repair. The
1759
+ * schema (D5) travels with it so a client can create a model's tables without
1760
+ * a document to discover them from.
1761
+ */
1762
+ interface SnapshotManifest {
1763
+ version: 2;
1764
+ epoch: number;
1765
+ buildId: string;
1766
+ createdAt: number;
1767
+ schema: Record<string, unknown>;
1768
+ chunks: SnapshotChunkEntry[];
1769
+ totals: {
1770
+ rows: number;
1771
+ bytes: number;
1772
+ };
1773
+ }
1774
+ /**
1775
+ * Read a chunk back, verified against its manifest entry when one is offered.
1776
+ *
1777
+ * The builder round-trips its own bytes and has nothing to verify against; the
1778
+ * loader always passes the entry, which is what makes a truncated or swapped
1779
+ * chunk a typed refusal rather than records the server never wrote.
1780
+ */
1781
+ declare function readSnapshotChunk(body: Uint8Array, descriptor: SnapshotChunkEntry | {
1782
+ model: string;
1783
+ }): Promise<SnapshotRow[]>;
1784
+
1785
+ /**
1786
+ * The cold load of a large document (#2816, behavior 15).
1787
+ *
1788
+ * A new or evicted client does not replay a document's history — on a format-2
1789
+ * document there is none to replay — it streams the chunks of the latest base
1790
+ * snapshot into its own `records` table and then applies the overlays sealed
1791
+ * after it. This module is the first half: manifest in, merged view out.
1792
+ *
1793
+ * What makes it usable on a real device rather than only in principle:
1794
+ *
1795
+ * - **Resumable.** Each chunk's rows and its `_snapshot_load` mark are written
1796
+ * in ONE commit, so an interrupted load has no half-applied chunk and a
1797
+ * resume downloads only what is missing. On a multi-hundred-MB document that
1798
+ * is the difference between a load that eventually finishes and one that
1799
+ * starts over every time the connection drops.
1800
+ * - **Verified.** A chunk goes straight into the authoritative table, so a
1801
+ * truncated or swapped one would write records the server never had, with no
1802
+ * whole-document comparison left to notice. The digest is checked before
1803
+ * anything is applied and a bad chunk is re-fetched; a chunk that stays bad
1804
+ * ends the load rather than completing a wrong one.
1805
+ * - **Progressive.** A model is answerable as soon as ITS chunks are in, and
1806
+ * progress is reported per chunk — a load that reports only at the end is
1807
+ * indistinguishable from a hang.
1808
+ * - **Bounded by the device.** A load may be capped to the models a platform
1809
+ * that cannot hold the whole document has room for (Precondition B's
1810
+ * fallback, see `format2StorageCapability`); a capped-out model's chunks are
1811
+ * never fetched, so the cap is bandwidth saved as well as space.
1812
+ *
1813
+ * Materialization goes through `applyRemote`, the same fold an overlay entry
1814
+ * takes, so a snapshot cannot come to mean something an overlay does not.
1815
+ */
1816
+
1817
+ /** A chunk that did not become the bytes the manifest describes, twice. */
1818
+ declare class SnapshotLoadIntegrityError extends Error {
1819
+ readonly chunk: SnapshotChunkEntry;
1820
+ readonly cause: unknown;
1821
+ constructor(chunk: SnapshotChunkEntry, cause: unknown);
1822
+ }
1823
+ /** How far a load has got. */
1824
+ interface SnapshotLoadProgress {
1825
+ /** Rows materialized so far, including a resumed run's earlier ones. */
1826
+ rows: number;
1827
+ totalRows: number;
1828
+ chunks: number;
1829
+ totalChunks: number;
1830
+ /** The model the chunk just applied belongs to. */
1831
+ model: string;
1832
+ }
1833
+ /** What a finished load did. */
1834
+ interface SnapshotLoadResult {
1835
+ rows: number;
1836
+ chunks: number;
1837
+ /** Chunks a previous attempt had already committed. */
1838
+ resumed: number;
1839
+ /** Chunks that had to be fetched twice because the first copy was wrong. */
1840
+ refetched: number;
1841
+ models: string[];
1842
+ /**
1843
+ * Models the snapshot carries that this load left behind, because the
1844
+ * device could not hold them (#2816, Precondition B's fallback).
1845
+ */
1846
+ skipped: string[];
1847
+ }
1848
+ /**
1849
+ * Where one chunk is read from, WITHIN the grant for its build.
1850
+ *
1851
+ * `{model}/{index}` — not a key and not a path: the client never learns an R2
1852
+ * key, and the download route derives the object from the grant plus exactly
1853
+ * these two values.
1854
+ */
1855
+ declare function snapshotChunkPath(chunk: SnapshotChunkEntry): string;
1856
+ /**
1857
+ * Load a snapshot's chunks into the client's merged view.
1858
+ *
1859
+ * Chunks are applied in manifest order, which is `(model, id)` order — so a
1860
+ * model's chunks are contiguous and it can be reported ready as soon as its
1861
+ * last one lands.
1862
+ */
1863
+ declare function loadFormat2Snapshot(options: {
1864
+ store: Format2RecordStore;
1865
+ manifest: SnapshotManifest;
1866
+ /** Read one chunk's bytes; the caller owns the transport and the grant. */
1867
+ fetchChunk(chunk: SnapshotChunkEntry): Promise<Uint8Array>;
1868
+ onProgress?(progress: SnapshotLoadProgress): void;
1869
+ /**
1870
+ * A model whose last chunk has landed. Awaited: "ready" has to mean
1871
+ * QUERYABLE, and the caller may have derived tables to hydrate before it
1872
+ * can honestly say so.
1873
+ */
1874
+ onModelReady?(model: string): void | Promise<void>;
1875
+ /** Re-fetches allowed for a chunk that arrives corrupt. Default 1. */
1876
+ retries?: number;
1877
+ /**
1878
+ * The models to hydrate, from {@link planSnapshotHydration} — omitted to
1879
+ * take the whole snapshot.
1880
+ *
1881
+ * A model outside the cap is not fetched at all, so a device that cannot
1882
+ * hold the document does not spend its bandwidth on the part it will drop.
1883
+ */
1884
+ models?: readonly string[];
1885
+ }): Promise<SnapshotLoadResult>;
1886
+
1887
+ /**
1888
+ * What the transport needs to know about a format-2 document's local writes
1889
+ * (#2816, behavior 9).
1890
+ *
1891
+ * The client transport (`JsBaoClient`) owns the socket but not the record
1892
+ * store, and the ORM owns the store but not the socket. These two functions
1893
+ * are the whole boundary between them: the transport stamps an outbound update
1894
+ * frame with the sequences below, and hands back the server's
1895
+ * `update.ack {maxContiguousSeq}` — the only thing that may prune a durable
1896
+ * `_pending_ops` row.
1897
+ *
1898
+ * A document that is not format 2 has no marks, and both functions say so
1899
+ * rather than inventing zeroes, so the legacy path stays exactly as it was.
1900
+ */
1901
+
1902
+ interface Format2SyncMarks {
1903
+ /** Highest sequence this client has committed locally. */
1904
+ highestLocalSeq: number;
1905
+ /** Highest sequence the server has confirmed durable. */
1906
+ ackedSeq: number;
1907
+ /** The epoch this document's overlay belongs to; `0` before it learns one. */
1908
+ epoch: number;
1909
+ /**
1910
+ * Whether this client ever measured its clock against the server's (#2816,
1911
+ * behavior 17). Without it, a replay refuses to call an offline write
1912
+ * "clearly older" than an online one.
1913
+ */
1914
+ clockOffsetKnown: boolean;
1915
+ }
1916
+
1917
+ /**
1918
+ * Whether this platform can hold this document (#2816, Precondition B's
1919
+ * fallback).
1920
+ *
1921
+ * A cold load streams a base snapshot straight into the local record store,
1922
+ * chunk by chunk, and on the documents this epic exists for that is minutes of
1923
+ * writing. Precondition B is the case where the store underneath cannot take
1924
+ * it: a browser engine with no persistent VFS at all, or one whose origin
1925
+ * quota is well under the document — Safari/iPadOS OPFS, whose measurement is
1926
+ * what gates enabling the OPFS engine there.
1927
+ *
1928
+ * The design ships this check regardless of how that measurement lands,
1929
+ * because the failure it prevents is the quiet one. Without it a load starts,
1930
+ * writes for ten minutes, and throws on the chunk that crosses the quota,
1931
+ * leaving a `records` table holding some of the document — which the client
1932
+ * will nonetheless answer queries from, since the merged view IS the document
1933
+ * on format 2. A partial merged view is not a slower document, it is a wrong
1934
+ * one.
1935
+ *
1936
+ * So the decision is taken before the first chunk is fetched, from what the
1937
+ * platform reports:
1938
+ *
1939
+ * - **no persistent store** → typed refusal. A view that evaporates when the
1940
+ * tab closes cannot be the authoritative copy of anything.
1941
+ * - **quota the whole snapshot does not fit in** → hydrate the models the app
1942
+ * configured as the ones worth keeping, and say which were dropped. A device
1943
+ * that can hold the hot models is more useful than one that holds nothing.
1944
+ * - **quota not even those fit in** → typed refusal, never a partial load
1945
+ * reported as a whole one.
1946
+ * - **no quota reported** → hydrate everything. Refusing on ignorance would
1947
+ * take format 2 away from every engine without a quota API, and the platforms
1948
+ * that do not answer are the ones with room.
1949
+ *
1950
+ * The plan it returns is what {@link loadFormat2Snapshot} is given: the cap is
1951
+ * enforced by not fetching the skipped models' chunks at all, so it is bytes
1952
+ * not transferred, not just bytes not stored.
1953
+ */
1954
+
1955
+ /**
1956
+ * How much local space a chunk's UNCOMPRESSED ndjson takes once materialized.
1957
+ *
1958
+ * The rows land as SQLite rows plus their indexes, so the number is above one.
1959
+ * Two is deliberately pessimistic: the cost of over-estimating is hydrating
1960
+ * fewer models than a device could have held, and the cost of
1961
+ * under-estimating is the mid-load failure this module exists to prevent.
1962
+ */
1963
+ declare const SNAPSHOT_STORAGE_EXPANSION = 2;
1964
+ /**
1965
+ * What a chunk's STORED bytes are assumed to inflate to when its manifest does
1966
+ * not say.
1967
+ *
1968
+ * Only a manifest written before the uncompressed length was recorded needs
1969
+ * this. It is high because the guess must not be optimistic: gzipped ndjson of
1970
+ * repetitive JSON records routinely compresses by an order of magnitude, and
1971
+ * treating the compressed size as a proxy for the materialized one is exactly
1972
+ * how a device approves a document it cannot hold.
1973
+ */
1974
+ declare const SNAPSHOT_COMPRESSED_EXPANSION = 8;
1975
+ /**
1976
+ * What one chunk is expected to cost in the local store once materialized.
1977
+ *
1978
+ * From the uncompressed length the manifest carries, because the compressed
1979
+ * length says more about how repetitive the data is than about how much room
1980
+ * it needs.
1981
+ */
1982
+ declare function estimateMaterializedBytes(chunk: SnapshotChunkEntry): number;
1983
+ /** What the platform says about the store a merged view would live in. */
1984
+ interface StorageCapability {
1985
+ /** Whether what is written survives the tab or app going away. */
1986
+ persistent: boolean;
1987
+ /** Bytes this origin may keep, or `null` when the platform will not say. */
1988
+ quotaBytes: number | null;
1989
+ /** Bytes the origin already holds — other documents included. Default 0. */
1990
+ usedBytes?: number;
1991
+ /**
1992
+ * Whether a PLATFORM storage API answered, as opposed to there having been
1993
+ * none to ask.
1994
+ *
1995
+ * The difference matters when the engine underneath is in memory. A browser
1996
+ * that reports an origin-private file system is describing the platform, not
1997
+ * this session's database, so "persistent" from it plus a volatile engine is
1998
+ * a claim that is simply wrong — and the load is refused. A host with no
1999
+ * storage API at all (Node, a worker with its own store) was never claiming
2000
+ * anything: it chose the database it passed, so an in-memory one is its
2001
+ * decision to make, and it is warned rather than refused.
2002
+ */
2003
+ probed?: boolean;
2004
+ }
2005
+ /** A device that cannot hold the document, said so before the load begins. */
2006
+ declare class Format2StorageCapabilityError extends Error {
2007
+ /** `not-persistent`: nothing survives. `over-quota`: not enough room. */
2008
+ readonly reason: "not-persistent" | "over-quota";
2009
+ /**
2010
+ * Materialized bytes the refused hydration would have needed, or `0` when
2011
+ * nothing has been measured — the bind refuses before there is a manifest
2012
+ * to measure, and on a document that may have no snapshot at all.
2013
+ */
2014
+ readonly requiredBytes: number;
2015
+ /** Bytes the platform offered, or `null` when it reported no quota. */
2016
+ readonly availableBytes: number | null;
2017
+ /** The configured models this was measured against; empty when uncapped. */
2018
+ readonly models: string[];
2019
+ /** Stable across builds, so an app can branch on it rather than on text. */
2020
+ readonly code = "FORMAT2_STORAGE_UNAVAILABLE";
2021
+ constructor(
2022
+ /** `not-persistent`: nothing survives. `over-quota`: not enough room. */
2023
+ reason: "not-persistent" | "over-quota",
2024
+ /**
2025
+ * Materialized bytes the refused hydration would have needed, or `0` when
2026
+ * nothing has been measured — the bind refuses before there is a manifest
2027
+ * to measure, and on a document that may have no snapshot at all.
2028
+ */
2029
+ requiredBytes: number,
2030
+ /** Bytes the platform offered, or `null` when it reported no quota. */
2031
+ availableBytes: number | null,
2032
+ /** The configured models this was measured against; empty when uncapped. */
2033
+ models: string[]);
2034
+ }
2035
+ /** Which models a cold load should hydrate on this device. */
2036
+ interface HydrationPlan {
2037
+ /** `full` took the whole snapshot; `capped` left models behind. */
2038
+ kind: "full" | "capped";
2039
+ /** Models to hydrate, in the manifest's order. */
2040
+ models: string[];
2041
+ /** Models the snapshot carries that this device is not taking. */
2042
+ skipped: string[];
2043
+ /** Materialized bytes the plan is expected to cost. */
2044
+ bytes: number;
2045
+ }
2046
+ /**
2047
+ * What the platform underneath will let a merged view keep.
2048
+ *
2049
+ * `persistent` is "there is a durable store to put it in", not "the user
2050
+ * granted persistence": an origin whose data is evictable still holds the
2051
+ * document until it is evicted, and a client that has been evicted cold-loads
2052
+ * again. What it cannot survive is having nowhere to write at all — which on
2053
+ * the browser means no origin-private file system, the platform Precondition B
2054
+ * measures.
2055
+ *
2056
+ * A host that knows better (a worker with its own VFS, a test) passes a
2057
+ * capability of its own instead of calling this.
2058
+ */
2059
+ declare function probeStorageCapability(nav?: unknown): Promise<StorageCapability>;
2060
+ /**
2061
+ * Decide what to hydrate before the load starts.
2062
+ *
2063
+ * Throws {@link Format2StorageCapabilityError} when the device cannot hold
2064
+ * even the configured models — the app is told, rather than the load being
2065
+ * started and abandoned partway through.
2066
+ */
2067
+ declare function planSnapshotHydration(input: {
2068
+ manifest: SnapshotManifest;
2069
+ capability: StorageCapability;
2070
+ /**
2071
+ * The models to keep when the whole document will not fit — the app's
2072
+ * statement of what is worth the space it has. Ignored when everything fits:
2073
+ * this is a fallback for a device that cannot take the document, not a
2074
+ * permanent narrowing of it.
2075
+ */
2076
+ models?: readonly string[];
2077
+ /**
2078
+ * Chunk keys a previous attempt already committed (`_snapshot_load`). Their
2079
+ * rows are already on the device, so they are what it has to make room for
2080
+ * only once.
2081
+ */
2082
+ completedChunks?: Iterable<string>;
2083
+ }): HydrationPlan;
2084
+
2085
+ interface InitJsBaoOptions {
2086
+ databaseConfig: DatabaseConfig;
2087
+ /**
2088
+ * Optional array of model classes to initialize.
2089
+ * If not provided, the ORM will rely on models being registered
2090
+ * via attachAndRegisterModel (assuming they have been imported by the application).
2091
+ *
2092
+ * Either way the instance takes a SNAPSHOT of the models at this moment: a
2093
+ * class registered globally afterwards belongs to no live instance until the
2094
+ * next `initJsBao` picks it up. That is the session semantics this call has
2095
+ * always had; with per-instance state (#3039) it is worth saying out loud,
2096
+ * because a late registration now has several instances it could mean.
2097
+ */
2098
+ models?: (typeof BaseModel)[];
2099
+ /**
2100
+ * What this device can hold of a large document, and what to keep when it
2101
+ * cannot hold all of it (#2816, Precondition B's fallback).
2102
+ *
2103
+ * `capability` overrides the platform probe — a host that owns its own store
2104
+ * knows better than `navigator.storage` does. `models` is the app's
2105
+ * statement of which models are worth the space when the document does not
2106
+ * fit; with none configured, a device short of space is refused rather than
2107
+ * loaded arbitrarily.
2108
+ */
2109
+ largeDocumentStorage?: {
2110
+ capability?: StorageCapability;
2111
+ models?: string[];
2112
+ };
2113
+ }
2114
+ interface InitJsBaoResult {
2115
+ dbEngine: DatabaseEngine;
2116
+ modelRegistry: ModelRegistry;
2117
+ /**
2118
+ * Which instance this is. The first-wins warning a shared model class
2119
+ * produces names the instance that keeps the class and the one that loses
2120
+ * it by this number, so a process holding several clients can tell them
2121
+ * apart.
2122
+ */
2123
+ instanceId: number;
2124
+ /**
2125
+ * Tear THIS ORM instance down (#3039): its observers are removed, its engine
2126
+ * destroyed and its state cleared, without touching any other instance.
2127
+ *
2128
+ * Its model classes are deliberately not unbound — they keep resolving this
2129
+ * (now destroyed) instance, so an operation through them throws the ordinary
2130
+ * not-initialized error instead of silently landing on a surviving client's
2131
+ * database.
2132
+ */
2133
+ destroy: () => Promise<void>;
2134
+ connectDocument: (docId: string, yDoc: Y.Doc, permissionHint: DocumentPermissionHint, options?: ConnectDocumentOptions) => Promise<void>;
2135
+ disconnectDocument: (docId: string) => Promise<void>;
2136
+ /**
2137
+ * Format-2 (#2816) durable-acknowledgement marks for `docId`, or `null`
2138
+ * when it is a legacy document. The transport stamps an outbound frame
2139
+ * with these and prunes through `format2Acknowledge`.
2140
+ */
2141
+ format2SyncMarks: (docId: string) => Format2SyncMarks | null;
2142
+ /**
2143
+ * The epoch `docId`'s own RECORD DATABASE says it follows, readable before
2144
+ * the document is bound (#2816). `0` when this database follows none —
2145
+ * whatever the client's metadata claims — and `null` when the engine cannot
2146
+ * be asked.
2147
+ */
2148
+ format2StoredEpoch: (docId: string) => number | null;
2149
+ format2Acknowledge: (docId: string, maxContiguousSeq: number) => void;
2150
+ /** Record the epoch a connection joined, for a document with no mark yet. */
2151
+ format2NoteEpoch: (docId: string, epoch: number) => void;
2152
+ /**
2153
+ * Record that `docId` was in touch with the server, and the offline window
2154
+ * the server reported (#2816, behavior 18). Past that window without a sync
2155
+ * the document refuses local writes and keeps serving reads.
2156
+ */
2157
+ format2NoteSync: (docId: string, at: number, windowDays?: number) => void;
2158
+ /**
2159
+ * Record the offline window the handshake reported, without claiming a sync
2160
+ * (#2816, behavior 18) — a frame is proof of contact, not proof that this
2161
+ * client's merged view is the room's state.
2162
+ */
2163
+ format2NoteWindow: (docId: string, windowDays?: number) => void;
2164
+ /**
2165
+ * Resolves once the query tables have caught up with the merged view
2166
+ * (#2816). `find()` reads the merged view, which a fold updates
2167
+ * synchronously; `query()` reads the derived tables, which the projection
2168
+ * queue updates after it — so an update from the room counts as applied only
2169
+ * once this settles.
2170
+ */
2171
+ whenFormat2ProjectionSettled: () => Promise<void>;
2172
+ /** Record the clock offset measured against a server frame (#2816). */
2173
+ format2NoteClockOffset: (docId: string, offsetMs: number) => void;
2174
+ /** Forget the pending ops a recency resolution dropped (#2816). */
2175
+ format2ForgetPendingOps: (docId: string, seqs: number[]) => void;
2176
+ /**
2177
+ * Rewrite the pending ops a recency resolution narrowed (#2816): the durable
2178
+ * log records the surviving delta, so a crash before the replay is
2179
+ * acknowledged cannot put the dropped field back.
2180
+ */
2181
+ format2NarrowPendingOps: (docId: string, ops: Array<Pick<PendingOp, "seq" | "fields" | "mutation">>) => void;
2182
+ /**
2183
+ * The unacknowledged local writes of `docId` — what a client carries onto
2184
+ * the next epoch's overlay when it follows a seal (#2816, behavior 10).
2185
+ */
2186
+ format2PendingOps: (docId: string) => PendingOp[];
2187
+ /** Move the epoch mark after the new epoch's overlay has been installed. */
2188
+ format2SetEpoch: (docId: string, epoch: number) => void;
2189
+ /**
2190
+ * Run an epoch move with `docId`'s local writes held back: a write that
2191
+ * landed on the sealed overlay after what is owed had been read off it would
2192
+ * go down with that document (#2816, behavior 10).
2193
+ */
2194
+ format2HoldWrites: <T>(docId: string, run: () => Promise<T>) => Promise<T>;
2195
+ /**
2196
+ * Re-point a connected large document at the next epoch's overlay, without
2197
+ * disconnecting it — a rotation must not drop model→document mappings.
2198
+ */
2199
+ format2RebindEpochDocument: (docId: string, yDoc: Y.Doc, options?: {
2200
+ restorePending?: boolean;
2201
+ }) => Promise<void>;
2202
+ /**
2203
+ * Fold one sealed epoch's archived overlay into `docId`'s merged view — the
2204
+ * step a client behind by whole epochs repeats per archive to catch up
2205
+ * (#2816, behavior 12).
2206
+ */
2207
+ format2ApplySealedOverlay: (docId: string, update: Uint8Array) => Promise<void>;
2208
+ /**
2209
+ * Throw away a large document's merged view before it is rebuilt from a
2210
+ * base — what a client does when the sealed-overlay chain that would have
2211
+ * caught it up cannot be trusted (#2816, behavior 12).
2212
+ */
2213
+ format2DiscardMergedView: (docId: string) => Promise<void>;
2214
+ /**
2215
+ * Fold a large document's open-epoch overlay into its merged view again —
2216
+ * the step that finishes a cold load, since a base snapshot's rows replace
2217
+ * what the open epoch had already contributed (#2816, behavior 15).
2218
+ */
2219
+ format2RefoldOverlay: (docId: string) => Promise<void>;
2220
+ /** Materialize a base snapshot into a large document's merged view (#2816). */
2221
+ format2LoadSnapshot: (docId: string, manifest: SnapshotManifest, fetchChunk: (chunk: SnapshotChunkEntry) => Promise<Uint8Array>, callbacks?: {
2222
+ onProgress?: (progress: SnapshotLoadProgress) => void;
2223
+ onModelReady?: (model: string) => void | Promise<void>;
2224
+ }) => Promise<SnapshotLoadResult>;
2225
+ getConnectedDocuments: () => Map<string, ConnectedDocument>;
2226
+ isDocumentConnected: (docId: string) => boolean;
2227
+ onDocumentConnectionChange: (callback: DocumentConnectionCallback) => () => void;
2228
+ addDocumentModelMapping: (modelName: string, docId: string) => void;
2229
+ removeDocumentModelMapping: (modelName: string) => void;
2230
+ clearDocumentModelMappings: () => void;
2231
+ setDefaultDocumentId: (docId: string) => void;
2232
+ clearDefaultDocumentId: () => void;
2233
+ getDocumentModelMapping: () => Record<string, string>;
2234
+ getDocumentIdForModel: (modelName: string) => string | undefined;
2235
+ getDefaultDocumentId: () => string | undefined;
2236
+ onDefaultDocChanged: (listener: (payload: {
2237
+ previous?: string;
2238
+ current?: string;
2239
+ }) => void) => () => void;
2240
+ onModelDocMappingChanged: (listener: (payload: {
2241
+ modelName: string;
2242
+ previous?: string;
2243
+ current?: string;
2244
+ }) => void) => () => void;
2245
+ }
2246
+
2247
+ /**
2248
+ * Initializes the js-bao system with multi-document support.
2249
+ * Sets up the database engine and prepares for document connections.
2250
+ *
2251
+ * Every call returns its OWN instance: its own engine, model registry,
2252
+ * connected documents, default-document mappings and `subscribe()` listeners.
2253
+ * Nothing written through one instance is observable through another, and an
2254
+ * instance can be torn down on its own through the returned `destroy()`.
2255
+ *
2256
+ * Model classes bind first-wins. A class handed to a second live `initJsBao`
2257
+ * keeps routing to the instance it was bound to; the second call warns and
2258
+ * carries on. Give each client its own model classes — as the `schemaToml`
2259
+ * path already does — when they must be isolated.
805
2260
  */
806
2261
  declare function initJsBao(options: InitJsBaoOptions): Promise<InitJsBaoResult>;
807
2262
  /**
808
- * Function to reset the initialization state, primarily for testing or hot-reloading scenarios.
2263
+ * Reset the initialization state, primarily for testing or hot-reloading.
2264
+ *
2265
+ * Destroys EVERY live instance — engines included — leaves their model classes
2266
+ * pointing at the instance that is gone rather than at a survivor, and stands a
2267
+ * fresh default context up in their place.
809
2268
  */
810
2269
  declare function resetJsBao(): Promise<void>;
811
2270
 
@@ -988,6 +2447,17 @@ interface DiscoveredSchema {
988
2447
  * just a Y.Doc.
989
2448
  */
990
2449
  declare function discoverSchema(yDoc: Y.Doc): DiscoveredSchema;
2450
+ /**
2451
+ * The schema ONE model DECLARES, read from its `_meta_<model>` map alone.
2452
+ *
2453
+ * `discoverSchema` walks every top-level map, and falls back to inferring a
2454
+ * schema from record data for models without metadata. A caller that only
2455
+ * needs one model's declaration — a format-2 document's projector deciding
2456
+ * which fields are unique or indexed (#2816), on a write path that runs for
2457
+ * every update — pays neither: the meta map is small and epoch-independent,
2458
+ * and a model that declares nothing simply has no declaration.
2459
+ */
2460
+ declare function discoverDeclaredModel(yDoc: Y.Doc, modelName: string): DiscoveredModel | undefined;
991
2461
  /**
992
2462
  * List model names that have data in the YDoc (non-`_` prefixed top-level maps).
993
2463
  */
@@ -1002,6 +2472,41 @@ declare function discoverModelNames(yDoc: Y.Doc): string[];
1002
2472
  */
1003
2473
  declare function schemaToToml(schema: DiscoveredSchema): string;
1004
2474
 
2475
+ /**
2476
+ * Unique-constraint index helpers (shared CRDT contract).
2477
+ *
2478
+ * js-bao enforces unique constraints inside the Yjs document, not via a SQL
2479
+ * `UNIQUE`. Each constraint has a `_uniqueIdx_<model>_<constraint>` map
2480
+ * (uniqueKey -> surviving recordId) and a document observer that
2481
+ * deterministically discards duplicate losers. Both the client write path and
2482
+ * the server-direct write path (`mutateRecord` in js-bao-wss) must derive the
2483
+ * key and the index map name the SAME way, or a server-written record would
2484
+ * not be seen by the client-side resolver. These helpers are the single source
2485
+ * of truth for that derivation so neither side keeps a parallel copy.
2486
+ */
2487
+
2488
+ /**
2489
+ * Build the unique-constraint key for a record over the given fields.
2490
+ *
2491
+ * Mirrors the document-observer conflict resolver: a `null`/`undefined` value
2492
+ * in any field means the record does not participate in the constraint (returns
2493
+ * `null`); otherwise the per-field `String()` values joined by `"|"`. This is
2494
+ * the key the `_uniqueIdx_*` map is read/written with during conflict
2495
+ * resolution, so it is the key the resolver treats as authoritative.
2496
+ */
2497
+ declare function buildUniqueKey(recordData: Y.Map<any> | Record<string, any>, fields: string[]): string | null;
2498
+ /**
2499
+ * Name of the Yjs map that indexes a unique constraint within a document:
2500
+ * `_uniqueIdx_<model>_<constraint>` mapping uniqueKey -> surviving recordId.
2501
+ */
2502
+ declare function uniqueIndexMapName(modelName: string, constraintName: string): string;
2503
+ /**
2504
+ * The constraint name js-bao assigns to a single-field `unique: true` field
2505
+ * (see `resolveUniqueConstraints` in models/schema.ts). Compound constraints
2506
+ * keep their author-given name instead.
2507
+ */
2508
+ declare function fieldUniqueConstraintName(modelName: string, fieldName: string): string;
2509
+
1005
2510
  interface DumpOptions {
1006
2511
  includeIndexes?: boolean;
1007
2512
  }
@@ -1038,4 +2543,790 @@ declare function dumpYDocToPlain(yDoc: Y.Doc, options?: DumpOptions): PlainYDoc;
1038
2543
  */
1039
2544
  declare function summarizePlainYDoc(dump: PlainYDoc): DumpSummary;
1040
2545
 
1041
- export { BaseModel, type ConnectedDocument, type DatabaseConfig, DatabaseEngine, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, DocumentClosedError, type DocumentConnectionCallback, type DocumentConnectionEvent, type DocumentFilter, type DocumentManager, type DocumentPermissionHint, DocumentResolutionError, type DumpOptions, type DumpSummary, type DumpSummaryEntry, type FieldOptions, type IncludeSpec, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, type ModelConstructor, type ModelOptions, ModelRegistry, type PaginatedResult, type PaginationOptions, type PlainYDoc, type ProjectionSpec, type QueryOptions, type QueryResult, RecordNotFoundError, type SaveOptions, StringSet, UniqueConstraintViolationError, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, clearMetaSyncCache, createModelClass, defineModelSchema, discoverModelNames, discoverSchema, dumpYDocToPlain, generateULID, inferFieldType, initJsBao, loadSchemaFromToml, loadSchemaFromTomlString, registerFunctionDefault, resetJsBao, schemaToToml, summarizePlainYDoc, syncInferredMeta, syncModelMeta };
2546
+ /**
2547
+ * The client's epoch-doc observer for format-2 documents (#2816).
2548
+ *
2549
+ * The epoch Y.Doc is no longer the read model — it is a bounded overlay of
2550
+ * this epoch's changes. Whatever lands in it (a peer's update applied by the
2551
+ * sync layer, or this client's own write) has to be folded into the local
2552
+ * merged `records` view, which is what reads come from.
2553
+ *
2554
+ * The observer rides the Yjs transaction for the same reason the server's
2555
+ * projector does: `Transaction.changed` names precisely the overlay keys an
2556
+ * update touched, so a remote update costs one merge-upsert per touched record
2557
+ * instead of a walk of a document that may be 2GB.
2558
+ */
2559
+
2560
+ /** Whether a top-level key names an application model map. */
2561
+ declare function isFormat2ModelMap(key: string): boolean;
2562
+ /**
2563
+ * Fold every overlay change on `doc` into `store`. Returns the unsubscribe
2564
+ * function — a client calls it when the epoch is sealed and it switches to the
2565
+ * next epoch's document.
2566
+ */
2567
+ declare function observeFormat2Overlay(doc: Y.Doc, store: Format2RecordStore,
2568
+ /**
2569
+ * Called after each folded record, so the host can refresh derived state
2570
+ * (the per-model query projection) for exactly what changed.
2571
+ */
2572
+ onApplied?: (modelName: string, recordId: string) => void): () => void;
2573
+
2574
+ /**
2575
+ * Following a seal onto a fresh overlay (#2816, behavior 10 — client half).
2576
+ *
2577
+ * When the room seals epoch E it replaces its overlay with an empty one, so a
2578
+ * client's further DELTAS against the sealed overlay cannot be integrated
2579
+ * there. The obvious answer — resync the whole document — is the wrong one on
2580
+ * a large document: a state-complete sync of the sealed overlay would seed
2581
+ * E+1 with everything E held, the new epoch would re-cross the rotation
2582
+ * threshold at once, and the overlay would never actually be bounded. That is
2583
+ * precisely why automatic rotation stayed opt-in until this path existed.
2584
+ *
2585
+ * What a client actually owes the new epoch is narrow: the writes the server
2586
+ * has not acknowledged. Everything acknowledged is already in the server's
2587
+ * `records` table — the sealed overlay was archived and folded — and in the
2588
+ * client's own merged view, so re-sending it buys nothing and costs an epoch.
2589
+ *
2590
+ * The owed writes are carried as OVERLAY entries taken from the sealed
2591
+ * document rather than rebuilt from the merged view, because the overlay is
2592
+ * the only place their exact shape survives: an explicit `null` unset, a
2593
+ * stringset member tombstone, and the difference between a patch and a
2594
+ * `_replace` create all vanish once a row has been materialized.
2595
+ *
2596
+ * They are restricted to the fields the unacked ops touched, so replaying them
2597
+ * cannot clobber a peer's concurrent write to another field of the same
2598
+ * record. A create is the exception: `_replace` discards the base row, so it
2599
+ * is carried whole or not at all.
2600
+ */
2601
+
2602
+ /**
2603
+ * The name a client's persisted overlay is stored under.
2604
+ *
2605
+ * Epoch-scoped so a restart restores the CURRENT epoch's overlay — which still
2606
+ * holds its unacked writes — and never a sealed one, whose content the server
2607
+ * has already folded away. It also bounds what an IndexedDB reset costs: one
2608
+ * epoch to re-sync, not the whole document.
2609
+ */
2610
+ declare function format2EpochDocName(docId: string, epoch: number): string;
2611
+ /** One record's owed overlay, ready to be written on the new epoch's document. */
2612
+ interface CarriedOverlay {
2613
+ model: string;
2614
+ entry: OverlayRecordEntry;
2615
+ }
2616
+ /** Reads a record's whole overlay entry out of the epoch being left behind. */
2617
+ type OverlayReader = (model: string, recordId: string) => OverlayRecordEntry;
2618
+ /**
2619
+ * The overlay a client carries into the next epoch: one entry per record its
2620
+ * unacked ops touched, in the local order those ops were written.
2621
+ *
2622
+ * `ops` is the durable `_pending_ops` log — already exactly the unacked ones,
2623
+ * since an `update.ack` prunes at its contiguous high-water mark.
2624
+ */
2625
+ declare function carryUnackedOverlay(ops: PendingOp[], readOverlay: OverlayReader, options?: {
2626
+ /**
2627
+ * Records whose tombstone must not travel (#2816, behavior 17): the
2628
+ * client's own delete lost to a write made online after it, and the
2629
+ * lifecycle carryRecord reads off the overlay still records that delete.
2630
+ */
2631
+ suppressDeletes?: Array<{
2632
+ model: string;
2633
+ recordId: string;
2634
+ }>;
2635
+ }): CarriedOverlay[];
2636
+ /**
2637
+ * Write a carried entry's flat keys onto the new epoch's model map.
2638
+ *
2639
+ * Encoded here rather than through `encodeOverlayMutation` because a carried
2640
+ * entry is already an overlay — its explicit nulls, its member tombstones and
2641
+ * its markers travel verbatim, with no mutation semantics re-derived on top.
2642
+ */
2643
+ declare function writeCarriedOverlay(modelMap: Y.Map<any>, entry: OverlayRecordEntry): void;
2644
+
2645
+ /**
2646
+ * Switching a connected client's write target across a rotation (#2816,
2647
+ * behavior 10 — client half).
2648
+ *
2649
+ * Sealing epoch E leaves the room writing to an empty overlay. Everything a
2650
+ * client sends afterwards from its E document is a Yjs DELTA whose causal
2651
+ * dependencies the new overlay does not have, so it is parked rather than
2652
+ * integrated — which is why a client has to move to E+1 too.
2653
+ *
2654
+ * Moving means a FRESH document, not a resync of the old one. A state-complete
2655
+ * sync of the sealed overlay would seed E+1 with everything E held: the new
2656
+ * epoch would re-cross the rotation threshold immediately, and rotation would
2657
+ * never bound anything. The only thing worth carrying across is what the
2658
+ * server has not acknowledged — see `format2EpochHandoff`.
2659
+ *
2660
+ * The orchestration lives behind a host interface because the pieces it moves
2661
+ * belong to three different owners (the document manager holds the Y.Doc and
2662
+ * its persistence, the ORM holds the record store, the transport holds the
2663
+ * socket) and because the ordering — carry, install, resync — is the behavior.
2664
+ */
2665
+
2666
+ /** What following a seal needs from the client that owns the document. */
2667
+ interface EpochRotationHost {
2668
+ /** The overlay the client writes to now, or `null` when it holds none. */
2669
+ overlayDoc(): Y.Doc | null;
2670
+ /** The epoch the client is on; `0` when it has never followed one. */
2671
+ epoch(): number;
2672
+ /** The durable, still-unacknowledged local writes, oldest first. */
2673
+ pendingOps(): PendingOp[];
2674
+ /**
2675
+ * Decide which of those writes still stand (#2816, behavior 17).
2676
+ *
2677
+ * Set only by a client that is REJOINING after being away: the sealed
2678
+ * overlays it just applied say which records and fields were written online
2679
+ * meanwhile, so an offline write can be dropped where it clearly lost rather
2680
+ * than silently overwriting the newer value. A client following an ordinary
2681
+ * seal has nothing to resolve — it was there the whole time — and leaves
2682
+ * this unset, which carries every unacked write exactly as before.
2683
+ */
2684
+ resolveReplay?(ops: PendingOp[]): {
2685
+ ops: PendingOp[];
2686
+ suppressDeletes: Array<{
2687
+ model: string;
2688
+ recordId: string;
2689
+ }>;
2690
+ };
2691
+ /**
2692
+ * Drop whatever outbound frames are still queued for this document.
2693
+ *
2694
+ * They are deltas against the overlay the room has just replaced, so it
2695
+ * cannot integrate them: it withholds the acknowledgement for the whole
2696
+ * frame they are merged into — including any fresh write that rode along —
2697
+ * and asks for a resync. Nothing is lost by dropping them, because their
2698
+ * content is either already durable or recorded in `_pending_ops`, which is
2699
+ * exactly what this move carries across.
2700
+ */
2701
+ discardQueuedFrames(): void;
2702
+ /**
2703
+ * Adopt `doc` as the overlay for `epoch`: rebind the readers, re-point the
2704
+ * persistence at the epoch-scoped name, and record the new mark.
2705
+ *
2706
+ * `restorePending: false` asks the bind NOT to write the unacknowledged
2707
+ * writes back into the document. Set only by a returning client's deferred
2708
+ * move: those writes have not been resolved yet, and restoring them here
2709
+ * would put them in the epoch's own Y.Map keys before the epoch's content
2710
+ * arrives — the very race the deferral exists to avoid.
2711
+ */
2712
+ install(doc: Y.Doc, epoch: number, options?: {
2713
+ restorePending?: boolean;
2714
+ }): Promise<void>;
2715
+ /** Sync the fresh overlay with the room, which pushes what was carried. */
2716
+ resync(): Promise<void>;
2717
+ /**
2718
+ * Run the move with this document's local writes held back.
2719
+ *
2720
+ * The move destroys the sealed overlay once the fresh one is bound, so a
2721
+ * write that slipped in between reading what is owed and rebinding the
2722
+ * readers would land its overlay entry in the doomed document — and could
2723
+ * never be recovered, since `_pending_ops` records which fields a write
2724
+ * touched, not the values. Optional so a host that owns no writes (a test
2725
+ * driving the state machine) need not provide one.
2726
+ */
2727
+ hold?<T>(run: () => Promise<T>): Promise<T>;
2728
+ }
2729
+ /** Why a client did or did not move. */
2730
+ type EpochRotationOutcome = {
2731
+ moved: true;
2732
+ epoch: number;
2733
+ carried: number;
2734
+ /**
2735
+ * The replay this move DEFERRED (#2816, behavior 17).
2736
+ *
2737
+ * Present only on a returning client's move: the owed writes have been
2738
+ * read off the sealed overlay but not resolved or written anywhere,
2739
+ * because the epoch being joined is open and its own content — the rest
2740
+ * of the online side of every conflict — arrives on the sync this move
2741
+ * asked for. The caller runs this once that has landed; it resolves
2742
+ * against the evidence by then complete, and writes the survivors onto
2743
+ * the live overlay. Returns how many records it carried.
2744
+ */
2745
+ completeReplay?: () => Promise<number>;
2746
+ } | {
2747
+ moved: false;
2748
+ reason:
2749
+ /** Not a large document — nothing here applies. */
2750
+ "not-large"
2751
+ /** The client has never followed an epoch; it simply joins this one. */
2752
+ | "adopted"
2753
+ /** A duplicate or late notice of an epoch already left behind. */
2754
+ | "already-current"
2755
+ /** Epochs were missed: catching up is the fast-forward path's job. */
2756
+ | "behind";
2757
+ };
2758
+ /**
2759
+ * Move the client onto `nextEpoch`.
2760
+ *
2761
+ * Only ever moves one epoch forward from where the client is. A larger jump
2762
+ * means the client missed seals — its merged view is behind by whole sealed
2763
+ * overlays, which have to be applied in order before it may write again, so it
2764
+ * is reported rather than papered over.
2765
+ */
2766
+ declare function followEpoch(host: EpochRotationHost, nextEpoch: number, options?: {
2767
+ /**
2768
+ * Allow a move of more than one epoch.
2769
+ *
2770
+ * Only ever set by the fast-forward path (behavior 12), which has just
2771
+ * applied every sealed overlay in between: the epochs are no longer
2772
+ * missing, so the jump skips nothing. Without that, a jump means the
2773
+ * client's merged view has holes in it and the move is refused.
2774
+ */
2775
+ allowJump?: boolean;
2776
+ /**
2777
+ * Hold the owed writes back until the epoch being joined has been synced
2778
+ * (#2816, behavior 17).
2779
+ *
2780
+ * Honored only where there is something to resolve — a returning client
2781
+ * with a conflict ledger, i.e. a host that supplies `resolveReplay`. A
2782
+ * client that was present through the seal has no online side to compare
2783
+ * with, so it carries at once exactly as before.
2784
+ */
2785
+ deferCarry?: boolean;
2786
+ }): Promise<EpochRotationOutcome>;
2787
+
2788
+ /**
2789
+ * Catching a client up on the sealed overlays it missed (#2816, behavior 12).
2790
+ *
2791
+ * A client that was away while the room rotated is behind by whole epochs: its
2792
+ * merged view is missing every write since it left, and its own overlay
2793
+ * belongs to an epoch the room archived. It does NOT need the document back,
2794
+ * though. The sealed overlays between the epoch it holds and the one the room
2795
+ * is on are exactly the changes it missed, so applying them in order — each a
2796
+ * bounded, rotation-sized artifact — converges its merged view on the server's
2797
+ * `records` table without a base ever crossing the wire.
2798
+ *
2799
+ * Two halves, deliberately separate:
2800
+ *
2801
+ * - {@link planFastForward} decides whether the chain can be trusted, from the
2802
+ * handshake alone and before anything is downloaded. A gap, a pruned
2803
+ * archive, or an epoch flagged `baseDiscontinuity` all mean the sum of the
2804
+ * overlays is not the document, and the honest answer is to reload from a
2805
+ * snapshot rather than converge on a state that never existed.
2806
+ * - {@link applyFastForward} runs the plan. It knows nothing about record
2807
+ * stores or sockets: the host fetches an archive and folds a decoded overlay,
2808
+ * which is what makes the ordering testable without either.
2809
+ *
2810
+ * The epoch mark is the caller's to move, and only after the whole chain has
2811
+ * landed: applying an overlay is idempotent at the record level, so a chain
2812
+ * that fails halfway is simply run again.
2813
+ */
2814
+
2815
+ /** One sealed epoch as the handshake describes it. */
2816
+ interface SealedEpochChainEntry {
2817
+ epoch: number;
2818
+ /** Reserved (#2816): the next base is not this epoch's overlay applied. */
2819
+ baseDiscontinuity?: boolean;
2820
+ /**
2821
+ * Whatever the host needs to read this epoch's archive, opaque here. Absent
2822
+ * when retention has already pruned it.
2823
+ */
2824
+ download?: unknown;
2825
+ }
2826
+ /** One archive to apply. */
2827
+ interface FastForwardStep {
2828
+ epoch: number;
2829
+ download: unknown;
2830
+ }
2831
+ /** Why a chain cannot be trusted. */
2832
+ type FastForwardRefusal =
2833
+ /** The chain skips an epoch: the client cannot know what happened in it. */
2834
+ "gap"
2835
+ /** An epoch declares its base is not the previous overlay applied. */
2836
+ | "discontinuity"
2837
+ /** The archive is gone — pruned once a snapshot covered it. */
2838
+ | "unavailable";
2839
+ type FastForwardPlan = {
2840
+ kind: "none";
2841
+ reason: "current" | "adopt";
2842
+ } | {
2843
+ kind: "apply";
2844
+ from: number;
2845
+ to: number;
2846
+ steps: FastForwardStep[];
2847
+ } | {
2848
+ kind: "reload";
2849
+ reason: FastForwardRefusal;
2850
+ epoch: number;
2851
+ };
2852
+ /**
2853
+ * Decide how a client on `current` reaches `target`.
2854
+ *
2855
+ * The chain runs from the epoch the client HOLDS, not from the one after it:
2856
+ * writes landed in that epoch after this client went away, and its archive is
2857
+ * the only place they exist now. The target epoch itself is open — the client
2858
+ * reaches that one by syncing, as it always did.
2859
+ */
2860
+ declare function planFastForward(options: {
2861
+ current: number;
2862
+ target: number;
2863
+ sealed: SealedEpochChainEntry[];
2864
+ }): FastForwardPlan;
2865
+ /** What running a plan needs from the client around it. */
2866
+ interface FastForwardHost {
2867
+ /** Read one sealed epoch's archived overlay. */
2868
+ fetchArchive(step: FastForwardStep): Promise<Uint8Array>;
2869
+ /**
2870
+ * Fold one decoded overlay into the merged view. Awaited, so a host whose
2871
+ * store commits asynchronously cannot have two epochs in flight at once.
2872
+ */
2873
+ applyOverlay(doc: Y.Doc, epoch: number): void | Promise<void>;
2874
+ /** Called after each epoch lands, for progress reporting. */
2875
+ onEpochApplied?(epoch: number, index: number, total: number): void;
2876
+ }
2877
+ /** What the whole catch-up needs, beyond reading and folding archives. */
2878
+ interface FastForwardRunHost extends FastForwardHost {
2879
+ /**
2880
+ * Run the catch-up with this document's local writes held back.
2881
+ *
2882
+ * A save that resolved its target before the catch-up and lands during it
2883
+ * would write into an overlay the move is about to replace — and
2884
+ * `_pending_ops` records which fields a write touched, not the values, so
2885
+ * nothing could reconstruct it.
2886
+ */
2887
+ hold<T>(run: () => Promise<T>): Promise<T>;
2888
+ /**
2889
+ * Move onto `target`: install a fresh overlay for it, carrying whatever the
2890
+ * server has not acknowledged, and resync. Called only once the whole chain
2891
+ * has landed, so the jump is no longer a jump over anything unknown.
2892
+ */
2893
+ move(target: number): Promise<void>;
2894
+ }
2895
+ /** How a catch-up ended. */
2896
+ type FastForwardResult = {
2897
+ outcome: "none";
2898
+ reason: "current" | "adopt";
2899
+ } | {
2900
+ outcome: "caught-up";
2901
+ applied: number[];
2902
+ epoch: number;
2903
+ } | {
2904
+ outcome: "reload";
2905
+ /** `failed` is a verified chain that could not be read. */
2906
+ reason: FastForwardRefusal | "failed";
2907
+ epoch: number;
2908
+ /**
2909
+ * What stopped a `failed` chain. A document that quietly stops syncing
2910
+ * is the hardest kind of bug to diagnose, so the cause travels with the
2911
+ * refusal instead of being swallowed.
2912
+ */
2913
+ error?: unknown;
2914
+ };
2915
+ /**
2916
+ * Verify the chain, apply it, and move onto the room's epoch.
2917
+ *
2918
+ * The order is the behavior: nothing is downloaded until the chain is known to
2919
+ * be complete, and the epoch is not moved until every archive in it has been
2920
+ * folded. A chain that fails halfway leaves the client exactly where it was —
2921
+ * behind, readable, and told to reload — rather than half-caught-up on an
2922
+ * epoch it would then write into.
2923
+ */
2924
+ declare function runFastForward(host: FastForwardRunHost, options: {
2925
+ current: number;
2926
+ target: number;
2927
+ sealed: SealedEpochChainEntry[];
2928
+ }): Promise<FastForwardResult>;
2929
+ /**
2930
+ * Apply a verified chain, oldest epoch first, and report what landed.
2931
+ *
2932
+ * Strictly sequential: overlays are state deltas over one another, so applying
2933
+ * a later one before an earlier one would let a stale value win. A failure
2934
+ * propagates with the epochs before it already folded — harmless, because the
2935
+ * caller has not moved the epoch mark and re-applying is idempotent.
2936
+ */
2937
+ declare function applyFastForward(plan: Extract<FastForwardPlan, {
2938
+ kind: "apply";
2939
+ }>, host: FastForwardHost): Promise<number[]>;
2940
+
2941
+ /**
2942
+ * Opening a large document with nothing local to start from (#2816,
2943
+ * behavior 15).
2944
+ *
2945
+ * A fresh client is not "behind": it holds no overlay, so there is nothing to
2946
+ * catch up from and the fast-forward planner rightly says "adopt". Under
2947
+ * epochs that is not enough. The epoch it would adopt holds only the changes
2948
+ * since the last rotation — everything older lives in the server's `records`
2949
+ * table and in the base snapshot in R2 — so a client that just joined would
2950
+ * show an almost-empty document, and, because a create is decided against the
2951
+ * merged view, would `_replace` records that still exist.
2952
+ *
2953
+ * So a cold start on a document that has ever rotated rebuilds a base first:
2954
+ *
2955
+ * - the latest completed snapshot, then the overlays sealed since — one base
2956
+ * plus a bounded chain, whatever the document's age;
2957
+ * - or, before any build has completed, the sealed overlays from the very
2958
+ * first epoch, which retention keeps precisely until a snapshot covers them;
2959
+ * - or nothing, when neither is available — which is reported rather than
2960
+ * papered over, because opening a document with holes in it is worse than
2961
+ * not opening it.
2962
+ */
2963
+
2964
+ /** How a document with no local state should be opened. */
2965
+ type ColdStartPlan =
2966
+ /** The document already follows an epoch: the catch-up path owns it. */
2967
+ {
2968
+ kind: "catch-up";
2969
+ }
2970
+ /** Nothing was ever archived — the current overlay IS the document. */
2971
+ | {
2972
+ kind: "join";
2973
+ }
2974
+ /** Materialize this snapshot, then apply the overlays sealed since. */
2975
+ | {
2976
+ kind: "snapshot";
2977
+ base: number;
2978
+ }
2979
+ /** No snapshot yet: the sealed chain from `base` is the document. */
2980
+ | {
2981
+ kind: "overlays";
2982
+ base: number;
2983
+ }
2984
+ /** Neither a base nor a complete chain is available. */
2985
+ | {
2986
+ kind: "unavailable";
2987
+ reason: "no-base";
2988
+ };
2989
+ /**
2990
+ * Decide how to open a document the client has no epoch mark for.
2991
+ *
2992
+ * A snapshot is preferred whenever the client can chain forward from it: one
2993
+ * base plus the overlays after it, rather than replaying every epoch since the
2994
+ * document was created — the cost this epic exists to remove. When the chain
2995
+ * above the snapshot is broken but the whole chain from the first epoch is
2996
+ * intact, that chain is used instead; it says the same thing, more expensively.
2997
+ */
2998
+ declare function planColdStart(input: {
2999
+ /** The epoch this client's overlay belongs to; 0 when it has none. */
3000
+ held: number;
3001
+ /** The epoch the room reports. */
3002
+ reported: number;
3003
+ sealed: readonly SealedEpochChainEntry[];
3004
+ /** The newest completed snapshot the handshake offered, if any. */
3005
+ snapshot?: {
3006
+ epoch: number;
3007
+ } | null;
3008
+ }): ColdStartPlan;
3009
+ /** What running a plan does to the document around it. */
3010
+ interface ColdStartHost {
3011
+ /**
3012
+ * Adopt the reported epoch as the one this document now follows.
3013
+ *
3014
+ * Two callers: the document where nothing was ever archived (the overlay IS
3015
+ * the document), and the end of a successful rebuild. The second is not
3016
+ * optional bookkeeping — the mark is what a later open reads to tell "this
3017
+ * client holds the document as of epoch E" from "this client holds nothing".
3018
+ * Without it a client that has just streamed a whole base presents as fresh
3019
+ * again on its next handshake and rebuilds from the same base.
3020
+ */
3021
+ join(): void | Promise<void>;
3022
+ /**
3023
+ * Throw away a merged view that cannot be repaired, keeping the writes this
3024
+ * client still owes the server.
3025
+ */
3026
+ discard(): void | Promise<void>;
3027
+ /** Materialize the base snapshot for `base`. */
3028
+ loadSnapshot(base: number): Promise<void>;
3029
+ /**
3030
+ * Apply the sealed overlays from `base` on and move onto the room's epoch.
3031
+ * Returns whether the document ended up current.
3032
+ */
3033
+ applyChain(base: number): Promise<boolean>;
3034
+ /** Run the ordinary catch-up for a document that is merely behind. */
3035
+ catchUp(): Promise<boolean>;
3036
+ /**
3037
+ * Join the epoch the room is on, if the rebuild has not already.
3038
+ *
3039
+ * Needed because the base a refused client rebuilds from is usually the
3040
+ * epoch the room is ON — a snapshot built after the last seal has nothing
3041
+ * sealed above it — so the chain is empty and moves nothing. The document
3042
+ * would then keep an overlay belonging to the epoch it was rebuilt out of:
3043
+ * never joining, and blind to every write made since.
3044
+ */
3045
+ land(): Promise<boolean>;
3046
+ /**
3047
+ * Fold the OPEN epoch's overlay over the base that was just installed.
3048
+ *
3049
+ * A base snapshot is a set of rows, not a diff: every row it carries
3050
+ * REPLACES what the merged view held for that record. The open epoch's
3051
+ * overlay — the changes since the base was cut — is the one part of the
3052
+ * document that does not arrive through the rebuild at all: this client
3053
+ * syncs it as an ordinary Y.Doc, before the load, during it, or after. So
3054
+ * whatever of it had already been folded is overwritten by the load, and a
3055
+ * patch or a delete made in the open epoch would come back as the base's
3056
+ * older row.
3057
+ *
3058
+ * Folding it again at the end costs one walk of a bounded overlay and is
3059
+ * idempotent (overlay entries are state, not operations), which is why it is
3060
+ * done unconditionally rather than only when a race is suspected.
3061
+ */
3062
+ refoldOverlay?(): void | Promise<void>;
3063
+ /**
3064
+ * Report a base that could not be read. A document that quietly stops
3065
+ * opening is the hardest kind of bug to diagnose, so the cause is handed
3066
+ * back rather than swallowed here.
3067
+ */
3068
+ onLoadFailed?(error: unknown): void;
3069
+ }
3070
+ /**
3071
+ * Run a rebuild plan, and say whether the document came out current.
3072
+ *
3073
+ * `stale` is the whole difference between the two callers. A client opening a
3074
+ * document for the first time has nothing to throw away — and throwing away
3075
+ * anyway would destroy the chunk marks an interrupted load resumes from, which
3076
+ * on a multi-hundred-megabyte base means starting over on every dropped
3077
+ * connection. A client whose sealed-overlay chain was REFUSED does: its rows
3078
+ * are an epoch the room left behind, and a snapshot loaded over them would
3079
+ * leave behind every record deleted since (#2816, behavior 12's close-out).
3080
+ */
3081
+ declare function runColdStart(host: ColdStartHost, plan: ColdStartPlan, options?: {
3082
+ stale?: boolean;
3083
+ }): Promise<boolean>;
3084
+
3085
+ /**
3086
+ * Replaying what a client wrote while it was away (#2816, behavior 17 —
3087
+ * amended D1, recency-aware).
3088
+ *
3089
+ * A client reconnecting into a later epoch replays its unacknowledged writes
3090
+ * onto the epoch it lands on. Doing that unconditionally — the original D1 —
3091
+ * silently overwrites whatever anyone else wrote in between, including writes
3092
+ * made days after the offline one. Doing it with per-field timestamps would
3093
+ * mean stamping every field of every record, in the overlay, in `records` and
3094
+ * in every snapshot chunk, for a case that is rare by construction.
3095
+ *
3096
+ * Neither is necessary, because the client already holds both halves of the
3097
+ * evidence by the time it replays:
3098
+ *
3099
+ * - WHAT was written online: the sealed overlays it applies while catching up
3100
+ * are precisely the touched records and fields of the epochs it missed;
3101
+ * - WHEN: an epoch's window runs from the previous epoch's seal to its own,
3102
+ * and the open epoch's ends now.
3103
+ *
3104
+ * So each op's own `ts` — stamped with the client's server-offset-corrected
3105
+ * clock — is placed against the window of the epoch that touched the same
3106
+ * field. Clearly after it, the offline write wins. Clearly before it, the
3107
+ * online write wins and the offline one is DROPPED AND SURFACED rather than
3108
+ * silently applied. Inside the window the order is genuinely unknown, so the
3109
+ * offline write wins and is surfaced too.
3110
+ *
3111
+ * Two rules are not recency-gated:
3112
+ *
3113
+ * - a patch is never replayed onto a record deleted meanwhile — resurrecting
3114
+ * a deleted record is worse than losing an edit to it, whatever the clock
3115
+ * says (a re-create is different: `_replace` states the record exists, so it
3116
+ * is resolved on recency like any other write);
3117
+ * - an op whose base-epoch chain is unavailable (retention pruned it) cannot
3118
+ * be checked at all, so it falls back to plain offline-wins — surfaced as
3119
+ * unverifiable rather than silently trusted.
3120
+ *
3121
+ * And nothing is ever dropped on a clock this client has no reason to trust:
3122
+ * with no measured server offset, "clearly older" degrades to the ambiguous
3123
+ * case, which keeps the write.
3124
+ */
3125
+
3126
+ /** What one epoch did to one record, from that epoch's sealed overlay. */
3127
+ interface RecordTouch {
3128
+ /** The epoch, for reporting which write the offline one raced. */
3129
+ epoch: number;
3130
+ fields: Set<string>;
3131
+ deleted: boolean;
3132
+ /** A `_replace` create: it replaced the whole row, so it touched every field. */
3133
+ replaced: boolean;
3134
+ }
3135
+ /** The window an epoch's writes fall in: `[start, end]`, inclusive. */
3136
+ interface EpochWindow {
3137
+ /** The previous epoch's seal, or `null` when it is not known. */
3138
+ start: number | null;
3139
+ /** This epoch's seal, or `null` while it is still open. */
3140
+ end: number | null;
3141
+ }
3142
+ /**
3143
+ * What the sealed overlays a catch-up applied say about the online side.
3144
+ *
3145
+ * Built as the chain is applied — the overlays are already decoded there — so
3146
+ * conflict detection costs one walk of artifacts the client was downloading
3147
+ * anyway.
3148
+ */
3149
+ declare class OfflineConflictLedger {
3150
+ /** Latest touch per record per epoch, newest epoch last. */
3151
+ private readonly touches;
3152
+ private readonly sealedAt;
3153
+ private readonly notedEpochs;
3154
+ /** Record the seal times the handshake reported. */
3155
+ noteSealTimes(times: Array<{
3156
+ epoch: number;
3157
+ sealedAt: number | null | undefined;
3158
+ }>): void;
3159
+ /** Note one record's overlay entry from `epoch`. */
3160
+ noteEntry(epoch: number, model: string, entry: OverlayRecordEntry): void;
3161
+ /** Note everything one sealed epoch's decoded overlay touched. */
3162
+ noteOverlay(epoch: number, doc: Y.Doc): void;
3163
+ /**
3164
+ * Whether every epoch from `from` up to (but not including) `to` was applied
3165
+ * through this ledger — the chain an op's conflicts would have to be in.
3166
+ */
3167
+ covers(from: number, to: number): boolean;
3168
+ /** The window `epoch`'s writes fall in; `end` is `null` while it is open. */
3169
+ window(epoch: number, currentEpoch: number): EpochWindow;
3170
+ /** The latest epoch after `afterEpoch` that touched `field` of a record. */
3171
+ conflictFor(model: string, recordId: string, field: string, afterEpoch: number): RecordTouch | null;
3172
+ /** The latest epoch after `afterEpoch` that touched a record at all. */
3173
+ recordConflict(model: string, recordId: string, afterEpoch: number): RecordTouch | null;
3174
+ /** The latest epoch after `afterEpoch` that DELETED a record. */
3175
+ deletedAfter(model: string, recordId: string, afterEpoch: number): RecordTouch | null;
3176
+ }
3177
+ /** What the app is told about an offline write that did not simply apply. */
3178
+ interface OfflineReplayNotice {
3179
+ model: string;
3180
+ recordId: string;
3181
+ /** Absent for a whole-record outcome (a delete, or a dropped patch). */
3182
+ field?: string;
3183
+ op: PendingOp["op"];
3184
+ outcome: "dropped" | "kept-ambiguous";
3185
+ reason: "outdated" | "record-deleted" | "in-window" | "unverifiable";
3186
+ /** The epoch whose write it raced; `0` when there was no chain to check. */
3187
+ epoch: number;
3188
+ /** The offline write's own time, as the client stamped it. */
3189
+ ts: number;
3190
+ }
3191
+ /** A record whose carried overlay must NOT bring its tombstone along. */
3192
+ interface SuppressedDelete {
3193
+ model: string;
3194
+ recordId: string;
3195
+ }
3196
+ /** What the epoch handoff replays, after recency has had its say. */
3197
+ interface OfflineReplayPlan {
3198
+ /** The surviving ops, in the client's original local order. */
3199
+ ops: PendingOp[];
3200
+ suppressedDeletes: SuppressedDelete[];
3201
+ notices: OfflineReplayNotice[];
3202
+ /**
3203
+ * Ops that survived in PART — the durable `_pending_ops` row still records
3204
+ * the whole write and has to be rewritten to what survived.
3205
+ *
3206
+ * Narrowing an op in memory is not enough. The row carries the mutation the
3207
+ * write made, so a crash before the server acknowledges the replay would
3208
+ * have the restore path put the WHOLE mutation back on the overlay,
3209
+ * including the field this resolution just decided the online side had won.
3210
+ * The write that lost would come back — quietly, and only on the machines
3211
+ * that crashed at the wrong moment.
3212
+ */
3213
+ narrowed: PendingOp[];
3214
+ /**
3215
+ * Whether a dropped write leaves this client's merged view unable to state
3216
+ * the record correctly (#2816, behavior 17).
3217
+ *
3218
+ * A dropped DELETE is the case: the row was removed locally when the delete
3219
+ * was written, and the online writes folded on top of that absence created
3220
+ * the record again from their own fields alone. The fields the delete
3221
+ * removed live only in the base and the epochs before it, so no artifact
3222
+ * this client still holds can restore them — the merged view has to be
3223
+ * rebuilt from a base before the delete is abandoned.
3224
+ */
3225
+ rebuildRequired: boolean;
3226
+ }
3227
+ /**
3228
+ * Decide what a reconnecting client replays onto the epoch it landed on.
3229
+ *
3230
+ * Ops are returned in their original local order — a later op of the same
3231
+ * record must not overtake an earlier one — with their `fields` narrowed to
3232
+ * what survived. An op that lost everything is absent entirely.
3233
+ */
3234
+ declare function resolveOfflineReplay(options: {
3235
+ ops: PendingOp[];
3236
+ ledger: OfflineConflictLedger;
3237
+ /** The epoch being replayed onto (its window ends now). */
3238
+ currentEpoch: number;
3239
+ now: number;
3240
+ /** Whether the op timestamps were corrected against server time. */
3241
+ clockOffsetKnown?: boolean;
3242
+ }): OfflineReplayPlan;
3243
+
3244
+ /**
3245
+ * The offline write window of a large document (#2816, behavior 18).
3246
+ *
3247
+ * A format-2 client writes while it is offline: the write lands in the merged
3248
+ * view immediately and waits in `_pending_ops` for the reconnect that replays
3249
+ * it onto whatever epoch the room has reached. What makes that replay
3250
+ * resolvable is the sealed-overlay chain back to the epoch the op was written
3251
+ * against — and retention releases an overlay once a snapshot covers it and it
3252
+ * is older than the window W. So a client that has been away longer than W is
3253
+ * writing against a past the server can no longer reconcile it with.
3254
+ *
3255
+ * Past the window the document is READ-ONLY rather than a growing pile of
3256
+ * writes that will be resolved by guesswork: local writes are refused with a
3257
+ * typed error, reads keep answering out of the merged view the client already
3258
+ * holds, and a sync restores writes immediately. Nothing already acked is
3259
+ * touched, and a refused write leaves nothing behind — no merged row, no
3260
+ * pending op — so the refusal cannot itself become a write that replays later.
3261
+ *
3262
+ * The window is the server's number (the same per-app W retention prunes
3263
+ * archives by), delivered on `epoch.info` and persisted locally: the session
3264
+ * that has to enforce it is by definition the one that cannot ask.
3265
+ */
3266
+ /** The window a deployment gets when it configures nothing. */
3267
+ declare const DEFAULT_OFFLINE_WINDOW_DAYS = 7;
3268
+ /**
3269
+ * The range the window is clamped to — the same 1–14 days as overlay
3270
+ * retention, because the archive chain is exactly what an offline write is
3271
+ * replayed across.
3272
+ */
3273
+ declare const MIN_OFFLINE_WINDOW_DAYS = 1;
3274
+ declare const MAX_OFFLINE_WINDOW_DAYS = 14;
3275
+ /**
3276
+ * Read a configured window, clamped, with the default for anything
3277
+ * unparseable.
3278
+ *
3279
+ * Clamped rather than refused for the same reason retention clamps: the
3280
+ * alternative to a usable number here is a client that either never goes
3281
+ * read-only or goes read-only immediately, and both are worse than a number
3282
+ * that is merely not the one someone typed.
3283
+ */
3284
+ declare function configuredOfflineWindowDays(raw: unknown): number;
3285
+ /** Where a document stands relative to its offline write window. */
3286
+ interface OfflineWindowStatus {
3287
+ /** Whether a local write may be accepted now. */
3288
+ writable: boolean;
3289
+ /** The sync this is measured from, or `null` when none was ever recorded. */
3290
+ lastSyncAt: number | null;
3291
+ windowDays: number;
3292
+ windowMs: number;
3293
+ /** How far PAST the window the client is; `0` while it is inside it. */
3294
+ overdueMs: number;
3295
+ }
3296
+ /**
3297
+ * Decide the window from the mark the client persisted.
3298
+ *
3299
+ * A client with no mark is writable: the window measures time since the last
3300
+ * sync, and treating "no mark yet" as "infinitely stale" would strand a
3301
+ * freshly loaded document in read-only with no way out. The mark is written
3302
+ * the first time the client hears from the server, which is before it can
3303
+ * possibly be a week behind.
3304
+ */
3305
+ declare function offlineWindowStatus(input: {
3306
+ lastSyncAt: number | null | undefined;
3307
+ windowDays?: number | null;
3308
+ now?: number;
3309
+ }): OfflineWindowStatus;
3310
+ /** Raised for a local write on a document past its offline window. */
3311
+ declare class DocumentOfflineWindowError extends Error {
3312
+ readonly code = "DOCUMENT_OFFLINE_WINDOW_EXPIRED";
3313
+ readonly documentId: string;
3314
+ readonly lastSyncAt: number | null;
3315
+ readonly windowDays: number;
3316
+ readonly overdueMs: number;
3317
+ constructor(documentId: string, status: OfflineWindowStatus);
3318
+ }
3319
+ /** The window each large-document write is checked against. */
3320
+ interface OfflineWindowSource {
3321
+ lastSyncAt(): number | null;
3322
+ offlineWindowDays(): number;
3323
+ }
3324
+ /**
3325
+ * The one gate every format-2 local write passes.
3326
+ *
3327
+ * Checked BEFORE anything is committed, so a refused write leaves neither a
3328
+ * merged row nor a pending op behind.
3329
+ */
3330
+ declare function assertWithinOfflineWindow(documentId: string, source: OfflineWindowSource, now?: number): void;
3331
+
3332
+ export { BaseModel, type CarriedOverlay, type ColdStartHost, type ColdStartPlan, type ConnectedDocument, DEFAULT_OFFLINE_WINDOW_DAYS, DEFAULT_OVERLAY_TABLES, type DatabaseConfig, DatabaseEngine, type DiscoveredConstraint, type DiscoveredField, type DiscoveredModel, type DiscoveredRelationship, type DiscoveredSchema, DocumentClosedError, type DocumentConnectionCallback, type DocumentConnectionEvent, type DocumentFilter, type DocumentManager, DocumentOfflineWindowError, type DocumentPermissionHint, DocumentResolutionError, type DumpOptions, type DumpSummary, type DumpSummaryEntry, type EpochRotationHost, type EpochRotationOutcome, type EpochWindow, type FastForwardHost, type FastForwardPlan, type FastForwardRefusal, type FastForwardResult, type FastForwardRunHost, type FastForwardStep, type FieldOptions, Format2CompositeFieldError, Format2ModelNotHydratedError, Format2RecordStore, Format2StorageCapabilityError, type Format2StoreSql, type HydrationPlan, type IncludeSpec, type InferAttrs, type InitJsBaoOptions, type InitJsBaoResult, LogLevel, Logger, MAX_OFFLINE_WINDOW_DAYS, MIN_OFFLINE_WINDOW_DAYS, type ModelConstructor, type ModelOptions, ModelRegistry, OVERLAY_KEY_SEPARATOR, OVERLAY_MARKER_DELETED, OVERLAY_MARKER_REPLACE, OfflineConflictLedger, type OfflineReplayNotice, type OfflineReplayPlan, type OfflineWindowStatus, type OverlayMutation, type OverlayReader, type OverlayRecordEntry, type OverlaySql, type OverlayTables, type PaginatedResult, type PaginationOptions, type ParsedOverlayKey, type PendingOp, type PendingOpInput, type PlainYDoc, type ProjectionSpec, type QueryOptions, type QueryResult, RecordNotFoundError, SNAPSHOT_COMPRESSED_EXPANSION, SNAPSHOT_STORAGE_EXPANSION, type SaveOptions, type SealedEpochChainEntry, type SnapshotChunkEntry, SnapshotLoadIntegrityError, type SnapshotLoadProgress, type SnapshotLoadResult, type SnapshotManifest, type SnapshotRow, type StorageCapability, StringSet, type SuppressedDelete, UniqueConstraintViolationError, applyFastForward, applyOverlayMutation, assertNoFormat2CompositeFields, assertWithinOfflineWindow, attachAndRegisterModel, attachSchemaToClass, autoRegisterModel, buildUniqueKey, carryUnackedOverlay, clearMetaSyncCache, completeOverlayEntries, configuredOfflineWindowDays, createModelClass, decodeOverlaySegment, defineModelSchema, discoverDeclaredModel, discoverModelNames, discoverSchema, dumpYDocToPlain, encodeOverlayMutation, encodeOverlaySegment, estimateMaterializedBytes, fieldUniqueConstraintName, followEpoch, format2ClientDDL, format2EpochDocName, format2StringSetMembers, format2TableNames, generateULID, groupOverlayEntries, inferFieldType, initJsBao, isFormat2ModelMap, isFormat2StringSetYMap, loadFormat2Snapshot, loadSchemaFromToml, loadSchemaFromTomlString, materializeOverlayRecord, observeFormat2Overlay, offlineWindowStatus, overlayFieldKey, overlayMarkerKey, overlayMemberKey, parseOverlayKey, planColdStart, planFastForward, planSnapshotHydration, probeStorageCapability, projectOverlayEntry, readOverlayRecord, readSnapshotChunk, registerFunctionDefault, resetJsBao, resolveOfflineReplay, runColdStart, runFastForward, schemaToToml, snapshotChunkPath, summarizePlainYDoc, syncInferredMeta, syncModelMeta, uniqueIndexMapName, writeCarriedOverlay };