@loro-dev/roost 0.3.2 → 0.4.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.
@@ -1,4 +1,19 @@
1
- /** Lody's durable logical-history adapter. */
1
+ /** Lody's multi-writer append-only logical-history adapter.
2
+ *
3
+ * All writes are append-only Roost Turns with no single-writer branch lock,
4
+ * no branch-revision conflict state, and no unstable global `position` or
5
+ * `totalCount`. Visible history equals all message primaries minus targets
6
+ * listed in valid `supersession` records (matched by physical `primaryTurnId`),
7
+ * ordered by ascending Roost `TurnId`.
8
+ *
9
+ * Writer-scoped segments (`response:<ownerHex>`, `state:<primaryTurnId>:<ownerHex>`)
10
+ * prevent cross-writer binding conflicts while retaining exact `(kind, businessId, segmentId)`
11
+ * equivocation detection and O(k) lookup by `(kind, businessId)` or `primaryTurnId`.
12
+ */
13
+ import { type ContentOp, type ParentRef } from './codec.js';
14
+ import { type JsonValue } from './json.js';
15
+ import { Identity } from './identity.js';
16
+ import { Stream, type TurnReadResult } from './store.js';
2
17
  /** Host-owned 32-byte signing seed. Roost never persists, recovers, or writes
3
18
  * seed files. The host calls `loadSeed()` and passes the bytes into
4
19
  * `RoostNodeClient`. A missing host identity is a host gap, not a Roost
@@ -6,299 +21,252 @@
6
21
  export type HostOwnerIdentity = {
7
22
  loadSeed(): Promise<Uint8Array>;
8
23
  };
9
- import { type ContentOp, type ParentRef } from './codec.js';
10
- import { type JsonValue } from './json.js';
11
- import { Identity } from './identity.js';
12
- import { Stream, type TurnReadResult } from './store.js';
13
- type Kind = 'message' | 'permission_response' | 'dispatch_intent' | 'batch_receipt' | 'publication' | 'branch_state';
24
+ export type HistoryKind = 'message' | 'permission_response' | 'supersession';
14
25
  export type HistoryIdentity = {
15
26
  businessId: string;
16
27
  segmentId: string;
17
- kind: Kind;
28
+ kind: HistoryKind;
18
29
  };
19
30
  export type HistoryWriteIdentity = Omit<HistoryIdentity, 'kind'> & {
20
- kind: Exclude<Kind, 'branch_state'>;
31
+ kind: Exclude<HistoryKind, 'supersession'>;
21
32
  };
22
- export type HistoryBatchCommand = {
23
- type: 'accept';
24
- identity: HistoryWriteIdentity;
33
+ export type AttachmentIdentity = {
34
+ attachmentId: string;
35
+ sha256: string;
36
+ name: string;
37
+ size: bigint;
38
+ };
39
+ export type AcceptHistory = HistoryWriteIdentity & {
25
40
  parents: ParentRef[];
26
41
  content: JsonValue;
27
42
  attachments?: AttachmentIdentity[];
28
- } | {
29
- type: 'setContent';
30
- identity: HistoryWriteIdentity;
31
- expectedNextSeq: bigint;
43
+ };
44
+ export type AcceptStateInput = {
45
+ businessId: string;
46
+ primaryTurnId: Uint8Array;
32
47
  content: JsonValue;
33
- } | {
34
- type: 'finish';
35
- identity: HistoryWriteIdentity;
36
- expectedNextSeq: bigint;
37
- } | {
38
- type: 'permission';
48
+ /** Optional suffix for `state:<primaryTurnId>:<ownerHex>:<suffix>` (used on execution-machine re-migration or post-seal state updates). */
49
+ suffix?: string;
50
+ parents?: ParentRef[];
51
+ attachments?: AttachmentIdentity[];
52
+ /** Seal immediately when true; defaults to false so an active machine can edit state transitions via `append`. */
53
+ seal?: boolean;
54
+ };
55
+ export type RespondPermissionInput = {
39
56
  requestId: string;
40
57
  assistantBusinessId: string;
41
58
  outcome: JsonValue;
42
- parents: ParentRef[];
43
- } | {
44
- type: 'dispatch';
45
- latestUserMsgId: string;
46
- state: 'pending' | 'published';
59
+ parents?: ParentRef[];
60
+ };
61
+ export type SupersedeInput = {
62
+ operationId: string;
63
+ /** Physical primary TurnIds to hide. Every target must exist locally and be sealed. */
64
+ supersedes: readonly Uint8Array[];
47
65
  };
48
- export type HistoryBatchResult = {
49
- batchId: string;
66
+ export type SupersedeResult = {
67
+ operationId: string;
68
+ turnId: Uint8Array;
50
69
  created: boolean;
51
- revision: bigint;
70
+ supersedes: Uint8Array[];
71
+ };
72
+ export type ReplacementMessageInput = {
73
+ businessId: string;
74
+ /** Defaults to `'primary'`. When reusing an existing businessId, supply e.g. `'primary:<opId>'`. */
75
+ segmentId?: string;
76
+ parents: ParentRef[];
77
+ content: JsonValue;
78
+ attachments?: AttachmentIdentity[];
79
+ /** Defaults to true (atomically creates and seals replacement in the same batch). */
80
+ seal?: boolean;
52
81
  };
53
- export type HistoryBatchOptions = {
54
- /** Keep pending commands in this owner database instead of replicated Turns.
55
- * Move unfinished work with a whole-database backup, not a Turn export. */
56
- recovery?: 'replicated' | 'local';
82
+ export type SupersedeAndAppendInput = {
83
+ operationId: string;
84
+ /** Physical primary TurnIds to hide. Every target must exist locally and be sealed. */
85
+ supersedes: readonly Uint8Array[];
86
+ /** One or more replacement primary messages committed atomically with the supersession record. */
87
+ replacements: readonly ReplacementMessageInput[];
88
+ };
89
+ export type SupersedeAndAppendResult = {
90
+ operationId: string;
91
+ supersessionTurnId: Uint8Array;
92
+ replacementTurnIds: Uint8Array[];
93
+ created: boolean;
94
+ supersedes: Uint8Array[];
57
95
  };
58
96
  export type HistoryProjectedMessage = {
59
97
  businessId: string;
60
98
  segmentId: string;
61
- physicalSegmentId?: string;
62
99
  turnId: Uint8Array;
100
+ owner: Uint8Array;
63
101
  content: JsonValue;
64
102
  nextSeq: bigint;
65
103
  sealed: boolean;
66
- /** The exact signed parent edges of this physical segment. */
104
+ /** Exact signed parent edges of this physical segment. */
67
105
  parents: ParentRef[];
68
- /** The sealed body hash, when the segment is sealed. */
106
+ /** Sealed body hash when the segment is sealed. */
69
107
  sealedHash: Uint8Array | null;
108
+ attachments: AttachmentIdentity[];
109
+ };
110
+ export type VisibleMessageEntry = {
111
+ primary: HistoryProjectedMessage;
112
+ /** All state records targeting `primary.turnId` across all authors, ordered by ascending TurnId. */
113
+ states: HistoryProjectedMessage[];
114
+ };
115
+ export type VisiblePageInput = {
116
+ /** Read newest `limit` items (default when no cursor or direction is specified). */
117
+ latest?: boolean;
118
+ /** Read oldest `limit` items in forward ascending `TurnId` order (equivalent to `{ after: null }`). */
119
+ oldest?: boolean;
120
+ /** Read up to `limit` items strictly before this `TurnId`. */
121
+ before?: Uint8Array;
122
+ /** Read up to `limit` items strictly after this `TurnId`, or from the start when `null`. */
123
+ after?: Uint8Array | null;
124
+ limit?: number;
125
+ };
126
+ export type VisiblePrimaryPage = {
127
+ entries: VisibleMessageEntry[];
128
+ hasMore: boolean;
129
+ oldestTurnId: Uint8Array | null;
130
+ newestTurnId: Uint8Array | null;
70
131
  };
71
- export type HistoryProjectedPage = {
132
+ export type VisibleSegmentPage = {
72
133
  messages: HistoryProjectedMessage[];
73
134
  hasMore: boolean;
74
- cursor: Uint8Array | null;
75
- };
76
- export type AttachmentIdentity = {
77
- attachmentId: string;
78
- sha256: string;
79
- name: string;
80
- size: bigint;
81
- };
82
- export type AcceptHistory = HistoryWriteIdentity & {
83
- parents: ParentRef[];
84
- content: JsonValue;
85
- attachments?: AttachmentIdentity[];
135
+ oldestTurnId: Uint8Array | null;
136
+ newestTurnId: Uint8Array | null;
86
137
  };
87
138
  export type HistoryDiagnostic = {
88
139
  turnId: Uint8Array;
89
- reason: 'invalid_envelope';
140
+ reason: 'invalid_envelope' | 'invalid_supersession';
90
141
  };
91
- export type BranchViewState = {
92
- viewId: string;
93
- revision: bigint;
94
- head: HistoryIdentity | null;
95
- operationId: string | null;
96
- logicalCount?: bigint;
97
- membershipBase?: HistoryIdentity;
98
- conflict?: boolean;
99
- };
100
- export type BranchMutationResult = {
101
- operationId: string;
102
- created: boolean;
103
- oldHead: HistoryIdentity | null;
104
- newHead: HistoryIdentity;
105
- revision: bigint;
142
+ export type FrameClassification = {
143
+ kind: 'primary_create';
106
144
  turnId: Uint8Array;
107
- };
108
- export type BranchAcceptInput = {
109
- viewId: string;
110
- expectedRevision: bigint;
111
- expectedHead: HistoryIdentity | null;
112
- operationId: string;
113
- input: AcceptHistory;
114
- };
115
- /** Immutable application input; branch placement is chosen by its owner. */
116
- export type HistoryInputIdentity = {
117
- viewId: string;
118
- operationId: string;
119
- input: Omit<AcceptHistory, 'parents'>;
120
- };
121
- export type ForkAndActivateInput = BranchAcceptInput & {
122
- /** Last retained Turn. The replacement must name it as a parent when present. */
123
- baseTurn: Uint8Array | null;
124
- /** Business identities from the old active suffix. They are provenance only. */
125
- supersedes: HistoryIdentity[];
126
- };
127
- export type ActiveBranchRead = {
128
- state: BranchViewState;
129
- messages: HistoryProjectedMessage[];
130
- complete: boolean;
131
- };
132
- export type ActiveBranchPageRead = {
133
- state: BranchViewState;
134
- totalCount: number;
135
- startPosition: number;
136
- messages: HistoryProjectedMessage[];
137
- hasMoreOlder: boolean;
138
- cursor: ActiveBranchPageCursor | null;
139
- complete: boolean;
140
- };
141
- /** Read active member references and headers. */
142
- export type ActiveBranchMembersRead = Omit<ActiveBranchPageRead, 'messages'> & {
143
- members: ActiveBranchSegmentRef[];
144
- };
145
- export type ActiveBranchPageCursor = {
146
- viewId: string;
145
+ businessId: string;
146
+ segmentId: string;
147
+ owner: Uint8Array;
148
+ } | {
149
+ kind: 'state_create';
147
150
  turnId: Uint8Array;
148
- position: number;
149
- revision: bigint;
150
- headTurnId: Uint8Array;
151
- pendingSegments: {
152
- businessId: string;
153
- segmentId: string;
154
- turnId: Uint8Array;
155
- }[];
156
- };
157
- /** Directory transition only. Retained bodies remain the caller's snapshots. */
158
- export type ActiveBranchPageAdvance = Omit<ActiveBranchPageRead, 'messages'> & {
159
- retainedFrom: number;
160
- message: HistoryProjectedMessage;
161
- through: bigint;
162
- };
163
- export type ActiveBranchMemberIdentity = {
164
- identity: HistoryIdentity;
151
+ businessId: string;
165
152
  segmentId: string;
166
- };
167
- export type ReplaceActiveBranchMembersInput = {
168
- viewId: string;
169
- expectedRevision: bigint;
170
- expectedHead: HistoryIdentity | null;
171
- operationId: string;
172
- members: ActiveBranchMemberIdentity[];
173
- messages?: {
174
- identity: HistoryWriteIdentity;
175
- content: JsonValue;
176
- parents?: ParentRef[];
177
- attachments?: AcceptHistory['attachments'];
178
- sealed?: boolean;
179
- }[];
180
- supersedes?: HistoryIdentity[];
181
- receipts?: Extract<HistoryBatchCommand, {
182
- type: 'accept';
183
- }>[];
184
- expectedEventCursor: bigint;
185
- };
186
- export type RestoreActiveBranchInput = {
187
- viewId: string;
188
- expectedRevision: bigint;
189
- expectedHead: HistoryIdentity | null;
190
- restoreHead: HistoryIdentity | null;
191
- operationId: string;
192
- };
193
- export type RestoreActiveBranchResult = {
153
+ targetTurnId: Uint8Array;
154
+ owner: Uint8Array;
155
+ } | {
156
+ kind: 'permission_response_create';
157
+ turnId: Uint8Array;
158
+ requestId: string;
159
+ segmentId: string;
160
+ owner: Uint8Array;
161
+ } | {
162
+ kind: 'supersession_create';
163
+ turnId: Uint8Array;
194
164
  operationId: string;
195
- created: boolean;
196
- oldHead: HistoryIdentity | null;
197
- newHead: HistoryIdentity | null;
198
- revision: bigint;
199
- turnId: Uint8Array | null;
165
+ supersedes: Uint8Array[];
166
+ owner: Uint8Array;
167
+ } | {
168
+ kind: 'other_op';
169
+ } | {
170
+ kind: 'invalid';
200
171
  };
201
- type Binding = {
172
+ export type HistoryBinding = {
202
173
  version: 1;
203
174
  turnId: number[];
204
175
  owner: number[];
205
176
  digest: number[];
206
177
  conflict?: boolean;
207
178
  };
208
- /** A business message may contain append-only execution segments. A new segment
209
- * never replaces an older signature. Dispatch/Flock and locations are host-owned. */
179
+ export declare const PRIMARY_SEGMENT_ID = "primary";
180
+ export declare const SUPERSESSION_SEGMENT_ID = "supersession";
181
+ export declare function isPrimarySegmentId(segmentId: string): boolean;
182
+ export declare function stateSegmentId(primaryTurnId: Uint8Array, owner: Uint8Array, suffix?: string): string;
183
+ /** Extract the target physical `primaryTurnId` from writer-scoped
184
+ * `state:<turnIdHex>:<ownerHex>` (with optional `:<suffix>`). Returns `null`
185
+ * for non-state or malformed segment IDs. */
186
+ export declare function stateTargetTurnId(segmentId: string): Uint8Array | null;
187
+ export declare function permissionResponseSegmentId(owner: Uint8Array): string;
188
+ /** Classify a raw Roost v1 frame (`encodeFrame`) without storage I/O. Useful
189
+ * for `tail-join` diagnostics and frame-level primary create counting. */
190
+ export declare function classifyHistoryFrame(rawFrame: Uint8Array): FrameClassification;
191
+ type IndexKey = {
192
+ table: string;
193
+ key: Uint8Array;
194
+ };
195
+ type IndexEntry = IndexKey & {
196
+ value: Uint8Array | null;
197
+ };
198
+ /** Browser/IndexedDB multi-writer logical history adapter. Keeps no cross-transaction
199
+ * mutable state in memory so multiple browser tabs share one IndexedDB replica safely. */
210
200
  export declare class LodyHistory {
201
+ #private;
211
202
  readonly stream: Stream;
212
203
  private readonly signingIdentity?;
213
204
  constructor(stream: Stream, signingIdentity?: Identity | undefined);
214
- /** A replica can project admitted history without owning a signing key.
215
- * Index maintenance is local; authoring through this instance is rejected. */
205
+ /** A replica can project admitted history without owning a signing key. */
216
206
  static readOnly(stream: Stream): LodyHistory;
217
207
  get identity(): Identity;
218
- private get space();
219
- private get cursorSpace();
220
208
  indexPage(limit?: number): Promise<boolean>;
221
209
  catchUpIndex(): Promise<void>;
210
+ observedEventCursor(): Promise<bigint>;
222
211
  invalidMessages(limit?: number): Promise<HistoryDiagnostic[]>;
223
- lookup(input: HistoryIdentity): Promise<TurnReadResult | undefined>;
212
+ latestSealedTurn(): Promise<ParentRef | null>;
224
213
  accept(input: AcceptHistory, options?: {
225
- publish?: boolean;
214
+ seal?: boolean;
226
215
  }): Promise<{
227
216
  turnId: Uint8Array;
228
217
  created: boolean;
229
218
  }>;
230
- /** Append a message to an active view and advance that view atomically. */
231
- acceptToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
232
- /** Atomically bind immutable input to its first accepted branch placement. */
233
- acceptInputToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
234
- /** Missing means unobserved, not that an in-flight acceptance cannot commit. */
235
- readInputAcceptance(input: HistoryInputIdentity): Promise<BranchMutationResult | undefined>;
236
- /** Create a replacement Turn and switch the same logical view to it. */
237
- forkAndActivate(input: ForkAndActivateInput): Promise<BranchMutationResult>;
238
- restoreActiveBranch(input: RestoreActiveBranchInput): Promise<RestoreActiveBranchResult>;
239
- readActiveBranch(viewId: string): Promise<ActiveBranchRead>;
240
- readActiveBranchPage(viewId: string, input?: {
241
- before?: ActiveBranchPageCursor;
242
- from?: number;
243
- latest?: boolean;
244
- limit?: number;
245
- }): Promise<ActiveBranchPageRead>;
219
+ acceptState(input: AcceptStateInput): Promise<{
220
+ turnId: Uint8Array;
221
+ segmentId: string;
222
+ created: boolean;
223
+ }>;
246
224
  append(id: Uint8Array, expectedNextSeq: bigint, ops: ContentOp[]): Promise<import("./store.js").Commit>;
247
225
  finish(id: Uint8Array, expectedNextSeq: bigint): Promise<import("./store.js").Commit>;
248
- respondPermission(input: {
249
- requestId: string;
250
- assistantBusinessId: string;
251
- outcome: JsonValue;
252
- parents: ParentRef[];
253
- }): Promise<{
226
+ respondPermission(input: RespondPermissionInput): Promise<{
254
227
  turnId: Uint8Array;
228
+ segmentId: string;
255
229
  created: boolean;
256
230
  }>;
231
+ supersede(input: SupersedeInput): Promise<SupersedeResult>;
232
+ supersedeAndAppend(input: SupersedeAndAppendInput): Promise<SupersedeAndAppendResult>;
233
+ lookup(input: HistoryIdentity): Promise<TurnReadResult | undefined>;
234
+ listSegments(kind: HistoryKind, businessId: string): Promise<HistoryProjectedMessage[]>;
235
+ listStateSegments(primaryTurnId: Uint8Array): Promise<HistoryProjectedMessage[]>;
236
+ listPermissionResponses(requestId: string): Promise<HistoryProjectedMessage[]>;
237
+ isSuperseded(turnId: Uint8Array): Promise<boolean>;
238
+ listSuperseded(after?: Uint8Array, limit?: number): Promise<{
239
+ targetTurnId: Uint8Array;
240
+ supersessionTurnId: Uint8Array;
241
+ }[]>;
242
+ countVisiblePrimaries(atLeast?: number): Promise<number>;
243
+ readVisiblePrimaryPage(input?: VisiblePageInput): Promise<VisiblePrimaryPage>;
244
+ readVisiblePrimaries(): Promise<VisibleMessageEntry[]>;
245
+ readVisibleSegmentsPage(input?: VisiblePageInput): Promise<VisibleSegmentPage>;
246
+ readVisibleMessagesByTurnIds(turnIds: readonly Uint8Array[]): Promise<HistoryProjectedMessage[]>;
247
+ readVisibleByBusinessId(businessId: string): Promise<VisibleMessageEntry[]>;
248
+ readVisiblePrimaryTurnIdsByBusinessIds(businessIds: readonly string[]): Promise<Map<string, Uint8Array[]>>;
257
249
  attachments(id: Uint8Array, resolve: (identity: AttachmentIdentity) => Promise<string | undefined>): Promise<{
258
- identity: {
259
- attachmentId: string;
260
- sha256: string;
261
- name: string;
262
- size: bigint;
263
- };
250
+ identity: AttachmentIdentity;
264
251
  location: string | undefined;
265
252
  }[]>;
266
- commitHistoryBatch(batchId: string, commands: HistoryBatchCommand[], options?: HistoryBatchOptions): Promise<HistoryBatchResult>;
267
- recoverPendingBatches(): Promise<void>;
268
- /** A consumer-owned cache. Call refresh on change hints (or host polling). */
269
- createMessageView(limit?: number): HistoryMessageView;
270
- readProjectedMessagesById(ids: Uint8Array[]): Promise<HistoryProjectedMessage[]>;
271
- readProjectedMessages(): Promise<HistoryProjectedMessage[]>;
272
- readProjectedMessagePage(input?: {
273
- after?: Uint8Array;
274
- before?: Uint8Array;
275
- latest?: boolean;
276
- limit?: number;
277
- }): Promise<HistoryProjectedPage>;
278
- readProjectedEntries(): Promise<{
279
- businessId: string;
280
- segmentId: string;
281
- turnId: Uint8Array;
282
- entry: unknown;
283
- nextSeq: bigint;
284
- sealed: boolean;
285
- }[]>;
286
- observedEventCursor(): Promise<bigint>;
287
253
  indexedBindings(): Promise<{
288
254
  identity: HistoryIdentity;
289
- binding: Binding;
255
+ binding: HistoryBinding;
290
256
  }[]>;
291
257
  }
292
- /** Process-host variant. The durable source of this rebuildable index is the
293
- * owner's operation/event log; it never opens a second database connection. */
294
- type NodeHistoryTurn = {
258
+ type NodeHistoryHeader = {
295
259
  turnId: Uint8Array;
296
- contentJson: string;
260
+ owner: Uint8Array;
261
+ parents: ParentRef[];
297
262
  nextSeq: bigint;
298
263
  sealed: Uint8Array | null;
299
264
  bodyVerified: boolean;
300
265
  hashVerified: boolean;
301
266
  };
267
+ type NodeHistoryTurn = NodeHistoryHeader & {
268
+ contentJson: string;
269
+ };
302
270
  export type NodeHistoryRead = {
303
271
  kind: 'found';
304
272
  turn: NodeHistoryTurn;
@@ -310,6 +278,17 @@ export type NodeHistoryRead = {
310
278
  turnId: Uint8Array;
311
279
  prefix: NodeHistoryTurn | null;
312
280
  };
281
+ export type NodeHistoryHeaderRead = {
282
+ kind: 'found';
283
+ turn: NodeHistoryHeader;
284
+ } | {
285
+ kind: 'notFound';
286
+ turnId: Uint8Array;
287
+ } | {
288
+ kind: 'incomplete';
289
+ turnId: Uint8Array;
290
+ prefix: NodeHistoryHeader | null;
291
+ };
313
292
  type NodeHistoryPath = ({
314
293
  key: string;
315
294
  } | {
@@ -339,7 +318,7 @@ export type NodeHistoryOp = {
339
318
  delete: bigint;
340
319
  valuesJson: string;
341
320
  };
342
- export type NodeHistoryIndexPut = NodeIndexKey & {
321
+ export type NodeHistoryIndexPut = IndexKey & {
343
322
  value: Uint8Array;
344
323
  onceKey?: Uint8Array;
345
324
  emitEvent?: boolean;
@@ -348,15 +327,15 @@ export type NodeHistoryBatchOptions = {
348
327
  requireLocalInterval?: boolean;
349
328
  indexPuts?: NodeHistoryIndexPut[];
350
329
  expectedEventCursor?: bigint;
351
- expectedIndexes?: NodeIndexEntry[];
352
- receiptChecks?: NodeIndexEntry[];
330
+ expectedIndexes?: IndexEntry[];
331
+ receiptChecks?: IndexEntry[];
353
332
  commitMetadata?: {
354
333
  scope: Uint8Array;
355
334
  value: Uint8Array;
356
335
  };
357
336
  derivedIndexes?: {
358
337
  through: bigint;
359
- indexPuts: (NodeIndexKey & {
338
+ indexPuts: (IndexKey & {
360
339
  value: Uint8Array;
361
340
  })[];
362
341
  };
@@ -389,6 +368,7 @@ export interface LodyNodeHost {
389
368
  readonly exclusiveBatches?: boolean;
390
369
  readCreateFields?(id: Uint8Array, selectors: {
391
370
  path: NodeHistoryPath;
371
+ info?: boolean;
392
372
  }[], omitDigestKeys: string[]): Promise<{
393
373
  fields: ({
394
374
  state: 'missing';
@@ -409,37 +389,15 @@ export interface LodyNodeHost {
409
389
  }>;
410
390
  readTurn(id: Uint8Array): Promise<NodeHistoryRead>;
411
391
  readTurns(ids: Uint8Array[]): Promise<NodeHistoryRead[]>;
412
- readTurnHeader(id: Uint8Array): Promise<{
413
- kind: string;
414
- turn?: {
415
- parents: ParentRef[];
416
- sealed: Uint8Array | null;
417
- nextSeq: bigint;
418
- bodyVerified: boolean;
419
- };
420
- prefix?: {
421
- parents: ParentRef[];
422
- sealed: Uint8Array | null;
423
- nextSeq: bigint;
424
- bodyVerified: boolean;
425
- } | null;
426
- }>;
427
- readValue(id: Uint8Array, path: ({
428
- key: string;
429
- } | {
430
- index: bigint;
431
- })[]): Promise<{
392
+ readTurnHeader(id: Uint8Array): Promise<NodeHistoryHeaderRead>;
393
+ latestTurnHeaders(limit: bigint): Promise<NodeHistoryHeader[]>;
394
+ turnHeadersBefore(before: Uint8Array, limit: bigint): Promise<NodeHistoryHeader[]>;
395
+ readValue?(id: Uint8Array, path: NodeHistoryPath): Promise<{
432
396
  contentJson: string;
433
397
  nextSeq: bigint;
434
398
  }>;
435
399
  edit(id: Uint8Array, seq: bigint, ops: NodeHistoryOp[]): Promise<unknown>;
436
400
  seal(id: Uint8Array, seq: bigint): Promise<unknown>;
437
- turnHeadersAfter?(after: Uint8Array | null, limit: bigint): Promise<{
438
- kind: string;
439
- turn?: {
440
- turnId: Uint8Array;
441
- };
442
- }[]>;
443
401
  writeBatch(writes: ({
444
402
  kind: 'create';
445
403
  parents: ParentRef[];
@@ -472,159 +430,73 @@ export interface LodyNodeHost {
472
430
  key: Uint8Array;
473
431
  value: Uint8Array;
474
432
  }[]>;
475
- scanIndexReverse?(table: string, end: Uint8Array | null, limit: bigint): Promise<{
433
+ scanIndexReverse(table: string, end: Uint8Array | null, limit: bigint): Promise<{
476
434
  key: Uint8Array;
477
435
  value: Uint8Array;
478
436
  }[]>;
479
- readIndex(keys: NodeIndexKey[]): Promise<(Uint8Array | null)[]>;
480
- compareAndWriteIndex(expected: NodeIndexEntry[], changes: NodeIndexEntry[], options?: {
481
- eventKeys?: NodeIndexKey[];
437
+ readIndex(keys: IndexKey[]): Promise<(Uint8Array | null)[]>;
438
+ compareAndWriteIndex(expected: IndexEntry[], changes: IndexEntry[], options?: {
439
+ eventKeys?: IndexKey[];
482
440
  }): Promise<boolean>;
483
441
  }
484
- type NodeIndexKey = {
485
- table: string;
486
- key: Uint8Array;
487
- };
488
- type NodeIndexEntry = NodeIndexKey & {
489
- value: Uint8Array | null;
490
- };
442
+ /** Process-host / Node SQLite multi-writer logical history adapter. The durable
443
+ * source of all derived indexes is the native event journal; never caches stale
444
+ * cross-transaction history state in memory. */
491
445
  export declare class NodeLodyHistory {
446
+ #private;
492
447
  readonly stream: LodyNodeHost;
493
- readonly owner: Uint8Array;
494
- constructor(stream: LodyNodeHost, owner: Uint8Array);
495
- /** One bounded local event page. The cursor belongs to this database's
496
- * event journal, including P2P imports and pending operations when applied.
497
- * All computation is outside the owner transaction; its cursor CAS rejects
498
- * a stale page together with every derived change. */
448
+ readonly owner: Uint8Array | undefined;
449
+ constructor(stream: LodyNodeHost, owner?: Uint8Array);
450
+ static readOnly(stream: LodyNodeHost): NodeLodyHistory;
499
451
  indexPage(limit?: number): Promise<boolean>;
500
452
  catchUpIndex(): Promise<void>;
501
- /** Requires a host supporting derived-indexes-v1. The SDK, never the consumer,
502
- * owns private checkpoint formats. General/unfinished/watched writes keep the
503
- * ordinary catch-up path. Application derived puts share the same proof. */
504
- publicationFence(cursor: bigint): Promise<NodeIndexEntry[]>;
505
- commitContentBatch(writes: Parameters<LodyNodeHost['writeBatch']>[0], options?: Omit<NodeHistoryBatchOptions, 'derivedIndexes'> & {
506
- derivedIndexPuts?: (NodeIndexKey & {
507
- value: Uint8Array;
508
- })[];
509
- unclaimedIdentities?: HistoryIdentity[];
510
- }): Promise<NodeHistoryBatchResult>;
453
+ observedEventCursor(): Promise<bigint>;
511
454
  invalidMessages(limit?: number): Promise<HistoryDiagnostic[]>;
512
- lookup(input: HistoryIdentity): Promise<NodeHistoryRead | undefined>;
513
- acceptJson(input: {
514
- businessId: string;
515
- segmentId: string;
516
- parents: ParentRef[];
517
- contentJson: string;
455
+ latestSealedTurn(): Promise<ParentRef | null>;
456
+ accept(input: AcceptHistory, options?: {
457
+ seal?: boolean;
518
458
  }): Promise<{
519
459
  turnId: Uint8Array;
520
460
  created: boolean;
521
461
  }>;
522
- read(id: Uint8Array): Promise<NodeHistoryRead>;
523
- append(id: Uint8Array, seq: bigint, ops: NodeHistoryOp[]): Promise<void>;
524
- finish(id: Uint8Array, seq: bigint): Promise<void>;
525
- accept(input: AcceptHistory, options?: {
526
- publish?: boolean;
527
- }): Promise<{
462
+ acceptState(input: AcceptStateInput): Promise<{
528
463
  turnId: Uint8Array;
464
+ segmentId: string;
529
465
  created: boolean;
530
466
  }>;
531
- acceptToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
532
- acceptInputToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
533
- readInputAcceptance(input: HistoryInputIdentity): Promise<BranchMutationResult | undefined>;
534
- forkAndActivate(input: ForkAndActivateInput): Promise<BranchMutationResult>;
535
- restoreActiveBranch(input: RestoreActiveBranchInput): Promise<RestoreActiveBranchResult>;
536
- readActiveBranch(viewId: string): Promise<ActiveBranchRead>;
537
- readActiveBranchPage(viewId: string, input?: {
538
- before?: ActiveBranchPageCursor;
539
- from?: number;
540
- latest?: boolean;
541
- limit?: number;
542
- }): Promise<ActiveBranchPageRead>;
543
- readActiveBranchMembers(viewId: string, input?: {
544
- before?: ActiveBranchPageCursor;
545
- from?: number;
546
- latest?: boolean;
547
- limit?: number;
548
- }): Promise<ActiveBranchMembersRead>;
549
- /** Resolve an identity to its Turn header. */
550
- lookupMember(identity: HistoryIdentity): Promise<{
551
- turnId: Uint8Array;
552
- nextSeq: bigint;
553
- sealed: boolean;
554
- } | undefined>;
555
- replaceActiveBranchMembers(input: ReplaceActiveBranchMembersInput): Promise<RestoreActiveBranchResult>;
556
- readActiveBranchMessages(viewId: string, businessIds: readonly string[]): Promise<HistoryProjectedMessage[]>;
557
- advanceActiveBranchDirectory(page: ActiveBranchPageRead, result: BranchMutationResult, limit?: number): ActiveBranchPageAdvance | undefined;
558
- respondPermission(input: {
559
- requestId: string;
560
- assistantBusinessId: string;
561
- outcome: JsonValue;
562
- parents: ParentRef[];
563
- }): Promise<{
467
+ append(id: Uint8Array, expectedNextSeq: bigint, ops: NodeHistoryOp[]): Promise<unknown>;
468
+ finish(id: Uint8Array, expectedNextSeq: bigint): Promise<unknown>;
469
+ respondPermission(input: RespondPermissionInput): Promise<{
564
470
  turnId: Uint8Array;
471
+ segmentId: string;
565
472
  created: boolean;
566
473
  }>;
567
- commitHistoryBatch(batchId: string, commands: HistoryBatchCommand[], options?: HistoryBatchOptions): Promise<HistoryBatchResult>;
568
- recoverPendingBatches(): Promise<void>;
569
- /** A consumer-owned cache. Call refresh on change hints (or host polling). */
570
- createMessageView(limit?: number): HistoryMessageView;
571
- readProjectedMessagesById(ids: Uint8Array[]): Promise<HistoryProjectedMessage[]>;
572
- readProjectedMessages(): Promise<HistoryProjectedMessage[]>;
573
- readProjectedMessagePage(input?: {
574
- after?: Uint8Array;
575
- before?: Uint8Array;
576
- latest?: boolean;
577
- limit?: number;
578
- }): Promise<HistoryProjectedPage>;
579
- readProjectedEntries(): Promise<{
580
- businessId: string;
581
- segmentId: string;
582
- turnId: Uint8Array;
583
- entry: unknown;
584
- nextSeq: bigint;
585
- sealed: boolean;
474
+ supersede(input: SupersedeInput): Promise<SupersedeResult>;
475
+ supersedeAndAppend(input: SupersedeAndAppendInput): Promise<SupersedeAndAppendResult>;
476
+ lookup(input: HistoryIdentity): Promise<NodeHistoryRead | undefined>;
477
+ listSegments(kind: HistoryKind, businessId: string): Promise<HistoryProjectedMessage[]>;
478
+ listStateSegments(primaryTurnId: Uint8Array): Promise<HistoryProjectedMessage[]>;
479
+ listPermissionResponses(requestId: string): Promise<HistoryProjectedMessage[]>;
480
+ isSuperseded(turnId: Uint8Array): Promise<boolean>;
481
+ listSuperseded(after?: Uint8Array, limit?: number): Promise<{
482
+ targetTurnId: Uint8Array;
483
+ supersessionTurnId: Uint8Array;
484
+ }[]>;
485
+ countVisiblePrimaries(atLeast?: number): Promise<number>;
486
+ readVisiblePrimaryPage(input?: VisiblePageInput): Promise<VisiblePrimaryPage>;
487
+ readVisiblePrimaries(): Promise<VisibleMessageEntry[]>;
488
+ readVisibleSegmentsPage(input?: VisiblePageInput): Promise<VisibleSegmentPage>;
489
+ readVisibleMessagesByTurnIds(turnIds: readonly Uint8Array[]): Promise<HistoryProjectedMessage[]>;
490
+ readVisibleByBusinessId(businessId: string): Promise<VisibleMessageEntry[]>;
491
+ readVisiblePrimaryTurnIdsByBusinessIds(businessIds: readonly string[]): Promise<Map<string, Uint8Array[]>>;
492
+ attachments(id: Uint8Array, resolve: (identity: AttachmentIdentity) => Promise<string | undefined>): Promise<{
493
+ identity: AttachmentIdentity;
494
+ location: string | undefined;
586
495
  }[]>;
587
- observedEventCursor(): Promise<bigint>;
588
496
  indexedBindings(): Promise<{
589
497
  identity: HistoryIdentity;
590
- binding: Binding;
498
+ binding: HistoryBinding;
591
499
  }[]>;
592
- pendingBatchBindings(): AsyncGenerator<{
593
- identity: HistoryIdentity;
594
- binding: Binding;
595
- }>;
596
- }
597
- type HistoryHost = LodyHistory | NodeLodyHistory;
598
- type ActiveBranchSegmentRef = {
599
- businessId: string;
600
- segmentId: string;
601
- turnId: Uint8Array;
602
- physicalSegmentId?: string;
603
- };
604
- export type HistoryMessageViewUpdate = {
605
- /** Treat rows and their nested content as read-only. Unchanged rows retain identity. */
606
- messages: readonly HistoryProjectedMessage[];
607
- changed: readonly HistoryProjectedMessage[];
608
- hasMoreOlder: boolean;
609
- eventCursor: bigint;
610
- };
611
- /** Loaded latest window plus any explicitly loaded older pages. Refresh consumes
612
- * local events and reads only affected published bodies, never the whole page.
613
- * A view is stream-local and consumer-owned; recreate it after closing the host. */
614
- export declare class HistoryMessageView {
615
- private readonly host;
616
- private readonly limit;
617
- private state;
618
- private queue;
619
- private closed;
620
- constructor(host: HistoryHost, limit?: number);
621
- get snapshot(): HistoryMessageViewUpdate | undefined;
622
- initialize(): Promise<HistoryMessageViewUpdate>;
623
- refresh(): Promise<HistoryMessageViewUpdate>;
624
- loadOlder(): Promise<HistoryMessageViewUpdate>;
625
- dispose(): void;
626
- private enqueue;
627
- private read;
628
500
  }
629
501
  /** Application conversion only: safe integers use integer tags; every other
630
502
  * finite JS number keeps its actual IEEE value as f64, including -0. This cannot