@loro-dev/roost 0.1.2 → 0.1.3

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.2
15
+ npm install @loro-dev/roost@0.1.3
16
16
  ```
17
17
 
18
18
  For source development and browser tests:
@@ -392,6 +392,21 @@ different input under that ID is a conflict. `LodyHistory` and `NodeLodyHistory`
392
392
  expose the same contract. The caller still owns queue, steer and ACP lifecycle
393
393
  decisions.
394
394
 
395
+ For immutable input delivery, use `acceptInputToView` with the same
396
+ `BranchAcceptInput` shape, then query `readInputAcceptance({ viewId, operationId,
397
+ input })`. Both browser and Node adapters bind an immutable input fingerprint
398
+ and operation receipt in the same transaction as the branch mutation. Input is
399
+ copied before asynchronous work begins. The fingerprint includes owner, business/segment identity, content and attachment
400
+ identity; the receipt key binds view and operation identity. Mutable parents,
401
+ expected head and revision are excluded.
402
+
403
+ Repeating an identical accept returns its original receipt even after edits,
404
+ later appends or active-branch removal. Changed input conflicts. Read-only lookup
405
+ returns `undefined` when unobserved, never inserts history, and does not prove a
406
+ pending accept cannot later commit. A host may retry the identical operation
407
+ without inventing a new input. Receipts remain owner-local derived indexes;
408
+ hosts own access checks, retained-generation lookup, execution and guide recovery.
409
+
395
410
  `LodyHistory.readOnly(stream)` indexes and projects admitted native history without
396
411
  a signing identity; authoring through that instance rejects. The host still owns
397
412
  stream authorization and seal verification. `restoreActiveBranch` is available in
@@ -105,6 +105,12 @@ export type BranchAcceptInput = {
105
105
  operationId: string;
106
106
  input: AcceptHistory;
107
107
  };
108
+ /** Immutable application input; branch placement is chosen by its owner. */
109
+ export type HistoryInputIdentity = {
110
+ viewId: string;
111
+ operationId: string;
112
+ input: Omit<AcceptHistory, 'parents'>;
113
+ };
108
114
  export type ForkAndActivateInput = BranchAcceptInput & {
109
115
  /** Last retained Turn. The replacement must name it as a parent when present. */
110
116
  baseTurn: Uint8Array | null;
@@ -183,6 +189,10 @@ export declare class LodyHistory {
183
189
  }>;
184
190
  /** Append a message to an active view and advance that view atomically. */
185
191
  acceptToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
192
+ /** Atomically bind immutable input to its first accepted branch placement. */
193
+ acceptInputToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
194
+ /** Missing means unobserved, not that an in-flight acceptance cannot commit. */
195
+ readInputAcceptance(input: HistoryInputIdentity): Promise<BranchMutationResult | undefined>;
186
196
  /** Create a replacement Turn and switch the same logical view to it. */
187
197
  forkAndActivate(input: ForkAndActivateInput): Promise<BranchMutationResult>;
188
198
  restoreActiveBranch(input: RestoreActiveBranchInput): Promise<RestoreActiveBranchResult>;
@@ -418,6 +428,8 @@ export declare class NodeLodyHistory {
418
428
  created: boolean;
419
429
  }>;
420
430
  acceptToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
431
+ acceptInputToView(input: BranchAcceptInput): Promise<BranchMutationResult>;
432
+ readInputAcceptance(input: HistoryInputIdentity): Promise<BranchMutationResult | undefined>;
421
433
  forkAndActivate(input: ForkAndActivateInput): Promise<BranchMutationResult>;
422
434
  restoreActiveBranch(input: RestoreActiveBranchInput): Promise<RestoreActiveBranchResult>;
423
435
  readActiveBranch(viewId: string): Promise<ActiveBranchRead>;
@@ -99,11 +99,12 @@ function parseBranchState(raw) {
99
99
  const previous = value instanceof Map ? value.get('conflict') : undefined;
100
100
  return { viewId: metadata.viewId, revision: metadata.revision, head: metadata.head, operationId: metadata.operationId, digest: metadata.digest, ...(metadata.logicalCount !== undefined ? { logicalCount: metadata.logicalCount } : {}), ...(previous === true ? { conflict: true } : {}) };
101
101
  }
102
- function branchOperationValue(result, digest) {
102
+ function branchOperationValue(result, digest, inputDigest) {
103
103
  return encodeJson(new Map([
104
104
  ['version', '1'], ['operationId', result.operationId], ['created', result.created],
105
105
  ['oldHead', result.oldHead ? identityValue(result.oldHead) : null], ['newHead', result.newHead ? identityValue(result.newHead) : null],
106
106
  ['revision', { kind: 'u64', value: result.revision }], ['turnId', result.turnId ? hexBytes(result.turnId) : null], ['digest', digest],
107
+ ...(inputDigest === undefined ? [] : [['inputDigest', inputDigest]]),
107
108
  ]));
108
109
  }
109
110
  function parseBranchOperation(raw) {
@@ -115,11 +116,14 @@ function parseBranchOperation(raw) {
115
116
  throw new StoreError('corrupt', 'invalid history branch operation');
116
117
  const oldHead = branchIdentity(value.get('oldHead'), 'history branch operation old head');
117
118
  const newHead = branchIdentity(value.get('newHead'), 'history branch operation new head');
119
+ const inputDigest = value.get('inputDigest');
120
+ if (inputDigest !== undefined && (typeof inputDigest !== 'string' || !/^[0-9a-f]{64}$/.test(inputDigest)))
121
+ throw new StoreError('corrupt', 'invalid input acceptance digest');
118
122
  if ((newHead !== null && newHead.kind !== 'message') || (oldHead !== null && oldHead.kind !== 'message'))
119
123
  throw new StoreError('corrupt', 'invalid history branch operation head');
120
124
  if ((turnId !== null && !/^[0-9a-f]{32}$/.test(turnId)) || !/^[0-9a-f]{64}$/.test(digest))
121
125
  throw new StoreError('corrupt', 'invalid history branch operation encoding');
122
- return { operationId: text(operationId), created, oldHead, newHead, revision: revision.value, turnId: turnId === null ? null : unhexBytes(turnId, 16), digest };
126
+ return { operationId: text(operationId), created, oldHead, newHead, revision: revision.value, turnId: turnId === null ? null : unhexBytes(turnId, 16), digest, ...(inputDigest === undefined ? {} : { inputDigest }) };
123
127
  }
124
128
  function branchMetadataFromContent(content) {
125
129
  if (!(content instanceof Map))
@@ -414,6 +418,14 @@ export class LodyHistory {
414
418
  acceptToView(input) {
415
419
  return branchAccept(this, input);
416
420
  }
421
+ /** Atomically bind immutable input to its first accepted branch placement. */
422
+ acceptInputToView(input) {
423
+ return acceptInputToView(this, ownBranchInput(input));
424
+ }
425
+ /** Missing means unobserved, not that an in-flight acceptance cannot commit. */
426
+ readInputAcceptance(input) {
427
+ return readInputAcceptance(this, ownInputIdentity(input));
428
+ }
417
429
  /** Create a replacement Turn and switch the same logical view to it. */
418
430
  forkAndActivate(input) {
419
431
  return branchFork(this, input);
@@ -712,6 +724,8 @@ export class NodeLodyHistory {
712
724
  throw new StoreError('stale', 'history discovery retry budget exhausted');
713
725
  }
714
726
  acceptToView(input) { return branchAccept(this, input); }
727
+ acceptInputToView(input) { return acceptInputToView(this, ownBranchInput(input)); }
728
+ readInputAcceptance(input) { return readInputAcceptance(this, ownInputIdentity(input)); }
715
729
  forkAndActivate(input) { return branchFork(this, input); }
716
730
  restoreActiveBranch(input) { return branchRestore(this, input); }
717
731
  readActiveBranch(viewId) { return readActiveBranch(this, viewId); }
@@ -873,7 +887,53 @@ async function readBranchTurn(host, turnId) {
873
887
  sealed: result.turn.sealed?.slice() ?? null,
874
888
  };
875
889
  }
876
- async function branchMutation(host, input, baseTurn, supersedes) {
890
+ function ownInputIdentity(input) {
891
+ return { viewId: text(input.viewId), operationId: text(input.operationId), input: {
892
+ kind: input.input.kind, businessId: text(input.input.businessId), segmentId: text(input.input.segmentId),
893
+ content: decodeJson(encodeJson(input.input.content)),
894
+ ...(input.input.attachments === undefined ? {} : { attachments: input.input.attachments.map(item => ({ ...item })) }),
895
+ } };
896
+ }
897
+ function ownBranchInput(input) {
898
+ const owned = ownInputIdentity(input);
899
+ return { ...owned, expectedRevision: input.expectedRevision, expectedHead: input.expectedHead ? { ...input.expectedHead } : null,
900
+ input: { ...owned.input, parents: input.input.parents.map(parent => ({ id: parent.id.slice(), hash: parent.hash.slice() })) } };
901
+ }
902
+ async function branchOperationResult(host, operation) {
903
+ if (operation.newHead === null)
904
+ throw new StoreError('corrupt', 'branch operation head missing');
905
+ const turnId = operation.turnId ?? await branchTurnByIdentity(host, operation.newHead);
906
+ if (!turnId)
907
+ throw new StoreError('corrupt', 'branch operation Turn missing');
908
+ return { operationId: operation.operationId, created: false, oldHead: operation.oldHead, newHead: operation.newHead, revision: operation.revision, turnId };
909
+ }
910
+ async function immutableInputDigest(host, input) {
911
+ text(input.viewId);
912
+ text(input.operationId);
913
+ if (input.input.kind !== 'message')
914
+ throw invalid('input acceptance only accepts message Turns');
915
+ assertWritableIdentity(input.input);
916
+ return hexBytes(createDigest(await branchOwner(host), [], envelope({ ...input.input, parents: [] })));
917
+ }
918
+ async function readInputAcceptance(host, input) {
919
+ const digest = await immutableInputDigest(host, input);
920
+ await branchCursor(host);
921
+ const prior = await readBranchOperation(host, input.viewId, input.operationId);
922
+ if (!prior)
923
+ return;
924
+ if (prior.inputDigest !== digest)
925
+ throw new StoreError('conflict', 'accepted input changed');
926
+ return branchOperationResult(host, prior);
927
+ }
928
+ async function acceptInputToView(host, input) {
929
+ const digest = await immutableInputDigest(host, input);
930
+ const prior = await readInputAcceptance(host, input);
931
+ if (prior)
932
+ return prior;
933
+ const baseTurn = input.expectedHead ? await branchTurnByIdentity(host, input.expectedHead) ?? null : null;
934
+ return branchMutation(host, input, baseTurn, [], digest);
935
+ }
936
+ async function branchMutation(host, input, baseTurn, supersedes, inputDigest) {
877
937
  if (input.input.kind !== 'message')
878
938
  throw invalid('active branch only accepts message Turns');
879
939
  if (typeof input.expectedRevision !== 'bigint' || input.expectedRevision < 0n || input.expectedRevision >= U64_MAX)
@@ -899,7 +959,7 @@ async function branchMutation(host, input, baseTurn, supersedes) {
899
959
  const metadata = branchMetadataFor({ ...input, viewId, operationId, expectedHead }, owner, expectedHead, baseTurn, supersedes, logicalCount);
900
960
  const createKey = keyOf(input.input);
901
961
  const branchValue = branchStateValue(metadata);
902
- const operationValue = branchOperationValue({ operationId, created: true, oldHead: expectedHead, newHead: metadata.head, revision: metadata.revision, turnId: null }, metadata.digest);
962
+ const operationValue = branchOperationValue({ operationId, created: true, oldHead: expectedHead, newHead: metadata.head, revision: metadata.revision, turnId: null }, metadata.digest, inputDigest);
903
963
  for (let attempt = 0; attempt < 4; attempt++) {
904
964
  // Fence every state read below, not only the final write. A concurrent
905
965
  // branch mutation after these checks must invalidate this attempt.
@@ -909,6 +969,11 @@ async function branchMutation(host, input, baseTurn, supersedes) {
909
969
  readBranchOperation(host, viewId, operationId),
910
970
  ]);
911
971
  if (priorOperation) {
972
+ if (inputDigest !== undefined) {
973
+ if (priorOperation.inputDigest !== inputDigest)
974
+ throw new StoreError('conflict', 'accepted input changed');
975
+ return branchOperationResult(host, priorOperation);
976
+ }
912
977
  if (priorOperation.digest !== metadata.digest || !branchStateEqual(priorOperation.oldHead, expectedHead) || !branchStateEqual(priorOperation.newHead, metadata.head))
913
978
  throw new StoreError('conflict', 'branch operation input changed');
914
979
  if (priorOperation.newHead === null)
package/package.json CHANGED
@@ -1,19 +1,12 @@
1
1
  {
2
2
  "name": "@loro-dev/roost",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Browser conversation history and sync for Loro Roost",
5
5
  "license": "MIT",
6
- "publishConfig": { "access": "public" },
7
- "type": "module",
8
- "packageManager": "pnpm@12.3.4",
9
- "scripts": {
10
- "check": "tsc --noEmit",
11
- "build": "tsc",
12
- "prepack": "pnpm build",
13
- "test": "pnpm build && node --test tests/*.test.mjs",
14
- "pack:consumer": "pnpm build && node scripts/pack-consumer.mjs",
15
- "check:consumer": "pnpm build && node scripts/check-consumer.mjs"
6
+ "publishConfig": {
7
+ "access": "public"
16
8
  },
9
+ "type": "module",
17
10
  "devDependencies": {
18
11
  "playwright": "1.58.2",
19
12
  "typescript": "5.9.3"
@@ -49,5 +42,12 @@
49
42
  "node/client.d.mts",
50
43
  "README.md",
51
44
  "LICENSE"
52
- ]
53
- }
45
+ ],
46
+ "scripts": {
47
+ "check": "tsc --noEmit",
48
+ "build": "tsc",
49
+ "test": "pnpm build && node --test tests/*.test.mjs",
50
+ "pack:consumer": "pnpm build && node scripts/pack-consumer.mjs",
51
+ "check:consumer": "pnpm build && node scripts/check-consumer.mjs"
52
+ }
53
+ }