@loro-dev/roost 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -12,7 +12,7 @@ The `Inbox` module adds atomic raw receipt/cursor storage, without authenticatin
12
12
  or applying those bytes.
13
13
 
14
14
  ```sh
15
- npm install @loro-dev/roost@0.1.0
15
+ npm install @loro-dev/roost@0.1.2
16
16
  ```
17
17
 
18
18
  For source development and browser tests:
@@ -271,6 +271,14 @@ the core outbox. Discovery page size bounds each page, while `maxSend` bounds on
271
271
  sends. Partial replicas keep directed `Link.bridgeTurns` and its `waitingParent`
272
272
  result for missing dependencies. See the discovery and upgrade contract.
273
273
 
274
+ Browser `HistoryReader.fetchWindow(source, budgetBytes, pageLimitBytes, stop,
275
+ { before, completePrefixes: false })` resumes at the previous window's complete
276
+ message boundary. This option stops at the byte budget without filtering extra
277
+ prefix-repair pages into the window range. Use explicit `completePrefixes` to
278
+ repair pending window turns. Persist the oldest contiguous boundary only after
279
+ intake commits, and initialize `Link.catchUpAt` from the original window tail,
280
+ never a later `head()` result. The Node window API is unchanged.
281
+
274
282
  `pnpm pack:consumer -- /absolute/output/directory` builds the npm tarball and
275
283
  checks its public entries. It depends on the exact registry release
276
284
  `@loro-dev/streams-client@0.8.0`, with no bundled SDK. `pnpm check:consumer`
@@ -358,6 +366,63 @@ polling and rendering. Browser hosts should subscribe before initializing the
358
366
  view; the Node adapter currently uses host polling/change hints. The external
359
367
  Lody UI is not wired by this library change.
360
368
 
369
+ New sessions can use the adapter's active-view primitives without changing the
370
+ existing LoroDoc path:
371
+
372
+ ```ts
373
+ const first = await history.acceptToView({
374
+ viewId: sessionId,
375
+ expectedRevision: 0n,
376
+ expectedHead: null,
377
+ operationId: requestId,
378
+ input: { kind: 'message', businessId: userId, segmentId: 'primary', parents: [], content },
379
+ });
380
+ const page = await history.readActiveBranchPage(sessionId, { latest: true, limit: 40 });
381
+ if (!page.complete) throw new Error('Active branch is incomplete');
382
+ const older = page.cursor
383
+ ? await history.readActiveBranchPage(sessionId, { before: page.cursor, limit: 40 })
384
+ : null;
385
+ ```
386
+
387
+ Use `forkAndActivate` for an edit/resend. Its replacement Create must include
388
+ the retained base Turn as a parent; the old suffix remains stored and immutable.
389
+ The active-view revision/head is checked with the same atomic write and event
390
+ cursor CAS as the Turn Create. Repeating an operation ID is idempotent, while a
391
+ different input under that ID is a conflict. `LodyHistory` and `NodeLodyHistory`
392
+ expose the same contract. The caller still owns queue, steer and ACP lifecycle
393
+ decisions.
394
+
395
+ `LodyHistory.readOnly(stream)` indexes and projects admitted native history without
396
+ a signing identity; authoring through that instance rejects. The host still owns
397
+ stream authorization and seal verification. `restoreActiveBranch` is available in
398
+ both browser and Node adapters: it atomically creates and seals a non-message
399
+ `branch_state` record with its local branch/operation indexes. Remote indexers
400
+ reconstruct the rollback, including an empty restored view, from that native
401
+ record. `branch_state` is readable as an internal history identity but is excluded
402
+ from `HistoryWriteIdentity`, `AcceptHistory` and batch commands; generic authoring
403
+ rejects it at runtime as well. Use `restoreActiveBranch` as the only write path. It
404
+ does not edit sealed Turns or change the Rust state machine/wire.
405
+
406
+ Active-branch pages return `startPosition`, `totalCount`, `hasMoreOlder`, and an
407
+ opaque ancestry cursor alongside the selected messages. Positions/counts refer
408
+ to logical `businessId` rows: use `primary` for the initial segment and distinct
409
+ successor IDs for late output. Project all returned segments of a business ID
410
+ into one row. Linear branches written with this metadata read a bounded page;
411
+ legacy/nonlinear ancestry may rebuild a complete directory. `readActiveBranch`
412
+ remains available for explicit full snapshots, fenced by the durable event cursor
413
+ so a concurrent import cannot mix branch membership and bodies. A partial replica
414
+ missing an older boundary reports incomplete and preserves the logical count;
415
+ the host fetches or repairs that boundary instead of declaring shorter history.
416
+ Generic published-message reverse
417
+ pages include superseded messages and cannot substitute for active-branch pages.
418
+
419
+ An append preserves older cursors while adding any intervening late successors.
420
+ A fork that removes the cursor's ancestry rejects it as `stale`; refresh the
421
+ loaded window and obtain a new cursor. The last reverse page can contain fewer
422
+ than `limit` rows and never extends past the cursor. Lody uses these APIs in its
423
+ Node owner and read-only browser replica. Static checks of that composition do
424
+ not establish live-service synchronization or product performance acceptance.
425
+
361
426
  New records in the private `lody_history_published_v1` table have a `version: "2"`
362
427
  membership-only value. Current readers also accept legacy full-snapshot values;
363
428
  there is no separate startup scan solely to convert these membership values
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- /** Application entry point. Experimental: pin the package and storage writer revision. */
1
+ /** Application entry point. Pin the package and storage writer revision. */
2
2
  export { IndexedDbStorage } from './indexeddb.js';
3
3
  export { Identity } from './identity.js';
4
4
  export { Store, Stream, Writer, StoreError, MAX_PAGE_HEADERS } from './store.js';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** Application entry point. Experimental: pin the package and storage writer revision. */
1
+ /** Application entry point. Pin the package and storage writer revision. */
2
2
  export { IndexedDbStorage } from './indexeddb.js';
3
3
  export { Identity } from './identity.js';
4
4
  export { Store, Stream, Writer, StoreError, MAX_PAGE_HEADERS } from './store.js';
@@ -1,4 +1,4 @@
1
- /** Experimental application adapter; not yet wired into Lody's runtime. */
1
+ /** Lody's durable logical-history adapter. */
2
2
  /** Host-owned 32-byte signing seed. Roost never persists, recovers, or writes
3
3
  * seed files. The host calls `loadSeed()` and passes the bytes into
4
4
  * `RoostNodeClient`. A missing host identity is a host gap, not a Roost
@@ -10,26 +10,29 @@ import { type ContentOp, type ParentRef } from './codec.js';
10
10
  import { type JsonValue } from './json.js';
11
11
  import { Identity } from './identity.js';
12
12
  import { Stream, type TurnReadResult } from './store.js';
13
- type Kind = 'message' | 'permission_response' | 'dispatch_intent' | 'batch_receipt' | 'publication';
13
+ type Kind = 'message' | 'permission_response' | 'dispatch_intent' | 'batch_receipt' | 'publication' | 'branch_state';
14
14
  export type HistoryIdentity = {
15
15
  businessId: string;
16
16
  segmentId: string;
17
17
  kind: Kind;
18
18
  };
19
+ export type HistoryWriteIdentity = Omit<HistoryIdentity, 'kind'> & {
20
+ kind: Exclude<Kind, 'branch_state'>;
21
+ };
19
22
  export type HistoryBatchCommand = {
20
23
  type: 'accept';
21
- identity: HistoryIdentity;
24
+ identity: HistoryWriteIdentity;
22
25
  parents: ParentRef[];
23
26
  content: JsonValue;
24
27
  attachments?: AttachmentIdentity[];
25
28
  } | {
26
29
  type: 'setContent';
27
- identity: HistoryIdentity;
30
+ identity: HistoryWriteIdentity;
28
31
  expectedNextSeq: bigint;
29
32
  content: JsonValue;
30
33
  } | {
31
34
  type: 'finish';
32
- identity: HistoryIdentity;
35
+ identity: HistoryWriteIdentity;
33
36
  expectedNextSeq: bigint;
34
37
  } | {
35
38
  type: 'permission';
@@ -54,6 +57,10 @@ export type HistoryProjectedMessage = {
54
57
  content: JsonValue;
55
58
  nextSeq: bigint;
56
59
  sealed: boolean;
60
+ /** The exact signed parent edges of this physical segment. */
61
+ parents: ParentRef[];
62
+ /** The sealed body hash, when the segment is sealed. */
63
+ sealedHash: Uint8Array | null;
57
64
  };
58
65
  export type HistoryProjectedPage = {
59
66
  messages: HistoryProjectedMessage[];
@@ -66,7 +73,7 @@ export type AttachmentIdentity = {
66
73
  name: string;
67
74
  size: bigint;
68
75
  };
69
- export type AcceptHistory = HistoryIdentity & {
76
+ export type AcceptHistory = HistoryWriteIdentity & {
70
77
  parents: ParentRef[];
71
78
  content: JsonValue;
72
79
  attachments?: AttachmentIdentity[];
@@ -75,6 +82,76 @@ export type HistoryDiagnostic = {
75
82
  turnId: Uint8Array;
76
83
  reason: 'invalid_envelope';
77
84
  };
85
+ export type BranchViewState = {
86
+ viewId: string;
87
+ revision: bigint;
88
+ head: HistoryIdentity | null;
89
+ operationId: string | null;
90
+ logicalCount?: bigint;
91
+ conflict?: boolean;
92
+ };
93
+ export type BranchMutationResult = {
94
+ operationId: string;
95
+ created: boolean;
96
+ oldHead: HistoryIdentity | null;
97
+ newHead: HistoryIdentity;
98
+ revision: bigint;
99
+ turnId: Uint8Array;
100
+ };
101
+ export type BranchAcceptInput = {
102
+ viewId: string;
103
+ expectedRevision: bigint;
104
+ expectedHead: HistoryIdentity | null;
105
+ operationId: string;
106
+ input: AcceptHistory;
107
+ };
108
+ export type ForkAndActivateInput = BranchAcceptInput & {
109
+ /** Last retained Turn. The replacement must name it as a parent when present. */
110
+ baseTurn: Uint8Array | null;
111
+ /** Business identities from the old active suffix. They are provenance only. */
112
+ supersedes: HistoryIdentity[];
113
+ };
114
+ export type ActiveBranchRead = {
115
+ state: BranchViewState;
116
+ messages: HistoryProjectedMessage[];
117
+ complete: boolean;
118
+ };
119
+ export type ActiveBranchPageRead = {
120
+ state: BranchViewState;
121
+ totalCount: number;
122
+ startPosition: number;
123
+ messages: HistoryProjectedMessage[];
124
+ hasMoreOlder: boolean;
125
+ cursor: ActiveBranchPageCursor | null;
126
+ complete: boolean;
127
+ };
128
+ export type ActiveBranchPageCursor = {
129
+ viewId: string;
130
+ turnId: Uint8Array;
131
+ position: number;
132
+ revision: bigint;
133
+ headTurnId: Uint8Array;
134
+ pendingSegments: {
135
+ businessId: string;
136
+ segmentId: string;
137
+ turnId: Uint8Array;
138
+ }[];
139
+ };
140
+ export type RestoreActiveBranchInput = {
141
+ viewId: string;
142
+ expectedRevision: bigint;
143
+ expectedHead: HistoryIdentity | null;
144
+ restoreHead: HistoryIdentity | null;
145
+ operationId: string;
146
+ };
147
+ export type RestoreActiveBranchResult = {
148
+ operationId: string;
149
+ created: boolean;
150
+ oldHead: HistoryIdentity | null;
151
+ newHead: HistoryIdentity | null;
152
+ revision: bigint;
153
+ turnId: Uint8Array | null;
154
+ };
78
155
  type Binding = {
79
156
  version: 1;
80
157
  turnId: number[];
@@ -86,8 +163,12 @@ type Binding = {
86
163
  * never replaces an older signature. Dispatch/Flock and locations are host-owned. */
87
164
  export declare class LodyHistory {
88
165
  readonly stream: Stream;
89
- readonly identity: Identity;
90
- constructor(stream: Stream, identity: Identity);
166
+ private readonly signingIdentity?;
167
+ constructor(stream: Stream, signingIdentity?: Identity | undefined);
168
+ /** A replica can project admitted history without owning a signing key.
169
+ * Index maintenance is local; authoring through this instance is rejected. */
170
+ static readOnly(stream: Stream): LodyHistory;
171
+ get identity(): Identity;
91
172
  private get space();
92
173
  private get cursorSpace();
93
174
  indexPage(limit?: number): Promise<boolean>;
@@ -100,6 +181,18 @@ export declare class LodyHistory {
100
181
  turnId: Uint8Array;
101
182
  created: boolean;
102
183
  }>;
184
+ /** Append a message to an active view and advance that view atomically. */
185
+ acceptToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
186
+ /** Create a replacement Turn and switch the same logical view to it. */
187
+ forkAndActivate(input: ForkAndActivateInput): Promise<BranchMutationResult>;
188
+ restoreActiveBranch(input: RestoreActiveBranchInput): Promise<RestoreActiveBranchResult>;
189
+ readActiveBranch(viewId: string): Promise<ActiveBranchRead>;
190
+ readActiveBranchPage(viewId: string, input?: {
191
+ before?: ActiveBranchPageCursor;
192
+ from?: number;
193
+ latest?: boolean;
194
+ limit?: number;
195
+ }): Promise<ActiveBranchPageRead>;
103
196
  append(id: Uint8Array, expectedNextSeq: bigint, ops: ContentOp[]): Promise<import("./store.js").Commit>;
104
197
  finish(id: Uint8Array, expectedNextSeq: bigint): Promise<import("./store.js").Commit>;
105
198
  respondPermission(input: {
@@ -209,11 +302,13 @@ export interface LodyNodeHost {
209
302
  readTurnHeader(id: Uint8Array): Promise<{
210
303
  kind: string;
211
304
  turn?: {
305
+ parents: ParentRef[];
212
306
  sealed: Uint8Array | null;
213
307
  nextSeq: bigint;
214
308
  bodyVerified: boolean;
215
309
  };
216
310
  prefix?: {
311
+ parents: ParentRef[];
217
312
  sealed: Uint8Array | null;
218
313
  nextSeq: bigint;
219
314
  bodyVerified: boolean;
@@ -322,6 +417,16 @@ export declare class NodeLodyHistory {
322
417
  turnId: Uint8Array;
323
418
  created: boolean;
324
419
  }>;
420
+ acceptToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
421
+ forkAndActivate(input: ForkAndActivateInput): Promise<BranchMutationResult>;
422
+ restoreActiveBranch(input: RestoreActiveBranchInput): Promise<RestoreActiveBranchResult>;
423
+ readActiveBranch(viewId: string): Promise<ActiveBranchRead>;
424
+ readActiveBranchPage(viewId: string, input?: {
425
+ before?: ActiveBranchPageCursor;
426
+ from?: number;
427
+ latest?: boolean;
428
+ limit?: number;
429
+ }): Promise<ActiveBranchPageRead>;
325
430
  respondPermission(input: {
326
431
  requestId: string;
327
432
  assistantBusinessId: string;