@kumiai/rpc 0.2.0 → 0.3.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/lib/classify.d.ts +132 -0
- package/lib/classify.js +48 -0
- package/lib/commit-frame.d.ts +32 -0
- package/lib/commit-frame.js +43 -0
- package/lib/commit.d.ts +188 -0
- package/lib/commit.js +50 -0
- package/lib/crypto.d.ts +201 -37
- package/lib/crypto.js +16 -8
- package/lib/cursor.d.ts +33 -0
- package/lib/cursor.js +26 -0
- package/lib/directed-crypto.d.ts +4 -4
- package/lib/directed-crypto.js +1 -1
- package/lib/directed.d.ts +5 -6
- package/lib/directed.js +10 -13
- package/lib/handshake.d.ts +13 -6
- package/lib/handshake.js +12 -7
- package/lib/hub-mux.d.ts +51 -12
- package/lib/hub-mux.js +66 -26
- package/lib/index.d.ts +9 -5
- package/lib/index.js +8 -3
- package/lib/ledger-entries.d.ts +22 -0
- package/lib/ledger-entries.js +78 -0
- package/lib/peer.d.ts +105 -15
- package/lib/peer.js +1002 -105
- package/lib/recovery.d.ts +24 -2
- package/lib/recovery.js +66 -54
- package/lib/topic.d.ts +18 -7
- package/lib/topic.js +18 -7
- package/package.json +5 -4
- package/lib/memory-group-mls.d.ts +0 -23
- package/lib/memory-group-mls.js +0 -75
package/lib/hub-mux.js
CHANGED
|
@@ -1,43 +1,50 @@
|
|
|
1
|
+
import { asDeliveryPosition } from './cursor.js';
|
|
1
2
|
/**
|
|
2
|
-
* Multiplex a single hub `receive` drain into a BroadcastBus view, a
|
|
3
|
-
*
|
|
4
|
-
* accept).
|
|
3
|
+
* Multiplex a single hub `receive` drain into a BroadcastBus view, a mailbox-hub view (for
|
|
4
|
+
* directed tunnels), and an onInbound hook (for lazy directed-server accept).
|
|
5
5
|
*
|
|
6
|
-
* Per inbound message, in order: (1) fire `onInbound` listeners for the topic,
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* Per inbound message, in order: (1) fire `onInbound` listeners for the topic, then (2) push
|
|
7
|
+
* to every `mailbox.receive` sink — so a listener may create a directed tunnel synchronously
|
|
8
|
+
* and still receive the triggering frame. Topics are refcounted across all three views; the
|
|
9
|
+
* first registration subscribes on the hub.
|
|
10
|
+
*
|
|
11
|
+
* **The refcount tracks LOCAL LISTENERS and never unsubscribes.** A subscription is a durable
|
|
12
|
+
* member-topic relationship, not a session: the hub holds a subscriber's undelivered frames,
|
|
13
|
+
* and `unsubscribe` tells the store to drop them and free any mailbox frame it was the last
|
|
14
|
+
* reader of. So no local lifecycle event may unsubscribe — dropping a listener, rotating an
|
|
15
|
+
* epoch, disposing the mux all mean "not listening", never "I have read my mail, discard the
|
|
16
|
+
* rest". Only an explicit leave-the-group would unsubscribe, and nothing here does. That
|
|
17
|
+
* outliving subscription is what lets a member return and find its mail; disposing the peer is
|
|
18
|
+
* what backgrounding a mobile app calls.
|
|
11
19
|
*/ export function createHubMux(params) {
|
|
12
20
|
const { hub, localDID } = params;
|
|
13
21
|
const listeners = new Map();
|
|
14
22
|
const refcount = new Map();
|
|
15
23
|
const sinks = new Set();
|
|
16
24
|
let disposed = false;
|
|
17
|
-
const retain = (topicID)=>{
|
|
25
|
+
const retain = (topicID, options)=>{
|
|
18
26
|
const next = (refcount.get(topicID) ?? 0) + 1;
|
|
19
27
|
refcount.set(topicID, next);
|
|
20
|
-
if (next === 1) void Promise.resolve(hub.subscribe(localDID, topicID)).catch(()=>{});
|
|
28
|
+
if (next === 1) void Promise.resolve(hub.subscribe(localDID, topicID, options)).catch(()=>{});
|
|
21
29
|
};
|
|
30
|
+
// Drops a local listener's reference, and NOTHING at the hub. The subscription stands: the
|
|
31
|
+
// frames this member has been sent and not read are its own, and a caller that has merely
|
|
32
|
+
// stopped listening has not read them.
|
|
22
33
|
const release = (topicID)=>{
|
|
23
34
|
const current = refcount.get(topicID) ?? 0;
|
|
24
35
|
if (current <= 0) return;
|
|
25
36
|
const next = current - 1;
|
|
26
|
-
if (next === 0)
|
|
27
|
-
|
|
28
|
-
void Promise.resolve(hub.unsubscribe?.(localDID, topicID)).catch(()=>{});
|
|
29
|
-
} else {
|
|
30
|
-
refcount.set(topicID, next);
|
|
31
|
-
}
|
|
37
|
+
if (next === 0) refcount.delete(topicID);
|
|
38
|
+
else refcount.set(topicID, next);
|
|
32
39
|
};
|
|
33
|
-
const onInbound = (topicID, listener)=>{
|
|
40
|
+
const onInbound = (topicID, listener, options)=>{
|
|
34
41
|
let set = listeners.get(topicID);
|
|
35
42
|
if (set == null) {
|
|
36
43
|
set = new Set();
|
|
37
44
|
listeners.set(topicID, set);
|
|
38
45
|
}
|
|
39
46
|
set.add(listener);
|
|
40
|
-
retain(topicID);
|
|
47
|
+
retain(topicID, options);
|
|
41
48
|
let removed = false;
|
|
42
49
|
return ()=>{
|
|
43
50
|
if (removed) return;
|
|
@@ -61,7 +68,11 @@
|
|
|
61
68
|
if (disposed || result.done) return;
|
|
62
69
|
const message = result.value;
|
|
63
70
|
const ack = ()=>{
|
|
64
|
-
|
|
71
|
+
// An ack names a place in THIS recipient's delivery queue, not in the topic's
|
|
72
|
+
// log. The two are different sequences and must never be crossed, so the
|
|
73
|
+
// position is named for what it is and never leaves this closure.
|
|
74
|
+
const position = asDeliveryPosition(message.sequenceID);
|
|
75
|
+
void Promise.resolve(subscription.ack?.(position)).catch(()=>{});
|
|
65
76
|
};
|
|
66
77
|
for (const listener of listeners.get(message.topicID) ?? []){
|
|
67
78
|
try {
|
|
@@ -83,7 +94,7 @@
|
|
|
83
94
|
})).then(()=>{}),
|
|
84
95
|
subscribe: (topicID, onMessage)=>onInbound(topicID, (message)=>onMessage(message.payload))
|
|
85
96
|
};
|
|
86
|
-
const
|
|
97
|
+
const mailbox = {
|
|
87
98
|
publish: (publishParams)=>hub.publish(publishParams),
|
|
88
99
|
subscribe: (_subscriberDID, topicID)=>{
|
|
89
100
|
retain(topicID);
|
|
@@ -158,9 +169,40 @@
|
|
|
158
169
|
};
|
|
159
170
|
}
|
|
160
171
|
};
|
|
172
|
+
const publish = (params)=>Promise.resolve(hub.publish({
|
|
173
|
+
senderDID: localDID,
|
|
174
|
+
topicID: params.topicID,
|
|
175
|
+
payload: params.payload,
|
|
176
|
+
...params.retain != null ? {
|
|
177
|
+
retain: params.retain
|
|
178
|
+
} : {},
|
|
179
|
+
// `expectedHead: null` is a compare-and-set against an empty topic and must reach
|
|
180
|
+
// the hub; only an ABSENT key means "append unconditionally". Keyed on presence,
|
|
181
|
+
// never on nullness — the first commit of a group's life is exactly the null case.
|
|
182
|
+
...'expectedHead' in params ? {
|
|
183
|
+
expectedHead: params.expectedHead
|
|
184
|
+
} : {},
|
|
185
|
+
...params.publishID != null ? {
|
|
186
|
+
publishID: params.publishID
|
|
187
|
+
} : {}
|
|
188
|
+
}));
|
|
189
|
+
const fetchTopic = (params)=>// Called on `hub`, not through a detached reference: a LogHub is often a class, and
|
|
190
|
+
// an unbound method loses its receiver.
|
|
191
|
+
Promise.resolve(hub.fetchTopic({
|
|
192
|
+
subscriberDID: localDID,
|
|
193
|
+
topicID: params.topicID,
|
|
194
|
+
...params.after != null ? {
|
|
195
|
+
after: params.after
|
|
196
|
+
} : {},
|
|
197
|
+
...params.limit != null ? {
|
|
198
|
+
limit: params.limit
|
|
199
|
+
} : {}
|
|
200
|
+
}));
|
|
161
201
|
return {
|
|
162
202
|
bus,
|
|
163
|
-
|
|
203
|
+
mailbox,
|
|
204
|
+
publish,
|
|
205
|
+
fetchTopic,
|
|
164
206
|
onInbound,
|
|
165
207
|
dispose: async ()=>{
|
|
166
208
|
if (disposed) return;
|
|
@@ -169,11 +211,9 @@
|
|
|
169
211
|
...sinks
|
|
170
212
|
])sink.close();
|
|
171
213
|
sinks.clear();
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
void Promise.resolve(hub.unsubscribe?.(localDID, topicID)).catch(()=>{});
|
|
176
|
-
}
|
|
214
|
+
// Listeners go, the drain stops, SUBSCRIPTIONS STAND. Disposing means "stopped reading",
|
|
215
|
+
// not "read everything, discard the rest". On mobile this is what backgrounding calls;
|
|
216
|
+
// unsubscribing here would delete the user's unread mail on every app switch.
|
|
177
217
|
refcount.clear();
|
|
178
218
|
listeners.clear();
|
|
179
219
|
iterator.return?.();
|
package/lib/index.d.ts
CHANGED
|
@@ -4,9 +4,13 @@
|
|
|
4
4
|
* @module rpc
|
|
5
5
|
*/
|
|
6
6
|
export { defineGroupProtocol, type GroupProtocolDefinition } from '@kumiai/broadcast';
|
|
7
|
-
export type
|
|
7
|
+
export { type CommitClassifierState, type CommitDisposition, classifyCommit, } from './classify.js';
|
|
8
|
+
export { CommitDeadlineError, type CommitJournal, type CommitKind, isHeadMismatch, type JournalEntry, JournalEpochError, type LaneResult, type LostCommit, type PendingCommit, RecoveryRequiredError, } from './commit.js';
|
|
9
|
+
export { type CommitFrame, decodeCommitFrame, encodeCommitFrame, } from './commit-frame.js';
|
|
10
|
+
export { type CommitContext, type CommitHeader, type GroupCrypto, type GroupMLS, isMissingLedgerEntries, type PendingRecovery, } from './crypto.js';
|
|
11
|
+
export { asDeliveryPosition, asLogPosition, type DeliveryPosition, type LogPosition, } from './cursor.js';
|
|
8
12
|
export { decodeHandshakeFrame, encodeHandshakeFrame, HANDSHAKE_KIND, HANDSHAKE_MAGIC, HANDSHAKE_VERSION, type HandshakeKind, } from './handshake.js';
|
|
9
|
-
export {
|
|
10
|
-
export { createGroupPeer, type GroupPeer, type GroupPeerParams, type ProtocolSurface, } from './peer.js';
|
|
11
|
-
export { decodeRecoveryReply, decodeRecoveryRequest, encodeRecoveryReply, encodeRecoveryRequest, } from './recovery.js';
|
|
12
|
-
export {
|
|
13
|
+
export { createLedgerEntryResolver, decodeLedgerEntries, encodeLedgerEntries, } from './ledger-entries.js';
|
|
14
|
+
export { createGroupPeer, type GroupPeer, type GroupPeerMLSParams, type GroupPeerParams, type ProtocolSurface, } from './peer.js';
|
|
15
|
+
export { decodeLedgerReply, decodeLedgerRequest, decodeRecoveryReply, decodeRecoveryRequest, encodeLedgerReply, encodeLedgerRequest, encodeRecoveryReply, encodeRecoveryRequest, } from './recovery.js';
|
|
16
|
+
export { COMMIT_LABEL, commitTopic, discoveryTopic, INBOX_LABEL, inboxTopic, protocolTopic, RENDEZVOUS_LABEL, rendezvousTopic, } from './topic.js';
|
package/lib/index.js
CHANGED
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module rpc
|
|
5
5
|
*/ export { defineGroupProtocol } from '@kumiai/broadcast';
|
|
6
|
+
export { classifyCommit } from './classify.js';
|
|
7
|
+
export { CommitDeadlineError, isHeadMismatch, JournalEpochError, RecoveryRequiredError } from './commit.js';
|
|
8
|
+
export { decodeCommitFrame, encodeCommitFrame } from './commit-frame.js';
|
|
9
|
+
export { isMissingLedgerEntries } from './crypto.js';
|
|
10
|
+
export { asDeliveryPosition, asLogPosition } from './cursor.js';
|
|
6
11
|
export { decodeHandshakeFrame, encodeHandshakeFrame, HANDSHAKE_KIND, HANDSHAKE_MAGIC, HANDSHAKE_VERSION } from './handshake.js';
|
|
7
|
-
export {
|
|
12
|
+
export { createLedgerEntryResolver, decodeLedgerEntries, encodeLedgerEntries } from './ledger-entries.js';
|
|
8
13
|
export { createGroupPeer } from './peer.js';
|
|
9
|
-
export { decodeRecoveryReply, decodeRecoveryRequest, encodeRecoveryReply, encodeRecoveryRequest } from './recovery.js';
|
|
10
|
-
export {
|
|
14
|
+
export { decodeLedgerReply, decodeLedgerRequest, decodeRecoveryReply, decodeRecoveryRequest, encodeLedgerReply, encodeLedgerRequest, encodeRecoveryReply, encodeRecoveryRequest } from './recovery.js';
|
|
15
|
+
export { COMMIT_LABEL, commitTopic, discoveryTopic, INBOX_LABEL, inboxTopic, protocolTopic, RENDEZVOUS_LABEL, rendezvousTopic } from './topic.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Unwrap } from '@kumiai/broadcast';
|
|
2
|
+
/** Encode the signed tokens a Commit enacts, for sealing into its frame. */
|
|
3
|
+
export declare function encodeLedgerEntries(tokens: Array<string>): Uint8Array;
|
|
4
|
+
/** Decode the signed tokens sealed into a commit frame. Throws on bytes that are not a list. */
|
|
5
|
+
export declare function decodeLedgerEntries(bytes: Uint8Array): Array<string>;
|
|
6
|
+
/**
|
|
7
|
+
* A ledger-entry resolver over the blob of the commit frame being applied. The MLS port calls
|
|
8
|
+
* it with the ids the Commit's control envelope names; it answers with the bodies sealed into
|
|
9
|
+
* that same frame — making body delivery atomic with the commit, so first delivery cannot
|
|
10
|
+
* strand.
|
|
11
|
+
*
|
|
12
|
+
* Answers with every token in the blob, not just the named subset: tokens are
|
|
13
|
+
* content-addressed, so the caller binds each body to its id by digesting, and an unrequested
|
|
14
|
+
* body is ignored. A responder can fail to answer, never inject.
|
|
15
|
+
*
|
|
16
|
+
* A blob this peer cannot open yields NO entries, not an error: the commit fails to resolve,
|
|
17
|
+
* the port raises missing-entries, and the lane leaves the cursor put so the frame is re-read
|
|
18
|
+
* — never reported as corruption. In practice this cannot arise on the apply path: the port
|
|
19
|
+
* asks only for a commit it is applying, framed at this peer's epoch, the epoch the blob is
|
|
20
|
+
* sealed under.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createLedgerEntryResolver(sealedEntries: Uint8Array, unwrap: Unwrap): (ids: Array<string>) => Promise<Array<string>>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { fromUTF, toUTF } from '@sozai/codec';
|
|
2
|
+
/**
|
|
3
|
+
* The blob a commit frame carries: the signed control-ledger tokens the Commit enacts, sealed
|
|
4
|
+
* with `GroupCrypto.wrap` under the epoch secret the Commit is framed at (the epoch every peer
|
|
5
|
+
* that can apply it holds). The hub sees only the sealed bytes, never a body.
|
|
6
|
+
*
|
|
7
|
+
* [ count(2, LE) | (tokenLength(4, LE) | token utf8)... ]
|
|
8
|
+
*
|
|
9
|
+
* This module is the ONLY place a commit frame's blob is opened, via a resolver that runs only
|
|
10
|
+
* when the MLS port asks for the bodies of a commit it is applying. Unwrapping is a consequence
|
|
11
|
+
* of "I can apply this frame", never a precondition of reading it.
|
|
12
|
+
*/ const COUNT_BYTES = 2;
|
|
13
|
+
const LENGTH_BYTES = 4;
|
|
14
|
+
/** Encode the signed tokens a Commit enacts, for sealing into its frame. */ export function encodeLedgerEntries(tokens) {
|
|
15
|
+
const encoded = tokens.map((token)=>fromUTF(token));
|
|
16
|
+
const size = encoded.reduce((total, bytes)=>total + LENGTH_BYTES + bytes.length, COUNT_BYTES);
|
|
17
|
+
const out = new Uint8Array(size);
|
|
18
|
+
const view = new DataView(out.buffer);
|
|
19
|
+
view.setUint16(0, encoded.length, true);
|
|
20
|
+
let offset = COUNT_BYTES;
|
|
21
|
+
for (const bytes of encoded){
|
|
22
|
+
view.setUint32(offset, bytes.length, true);
|
|
23
|
+
out.set(bytes, offset + LENGTH_BYTES);
|
|
24
|
+
offset += LENGTH_BYTES + bytes.length;
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
/** Decode the signed tokens sealed into a commit frame. Throws on bytes that are not a list. */ export function decodeLedgerEntries(bytes) {
|
|
29
|
+
if (bytes.length < COUNT_BYTES) {
|
|
30
|
+
throw new Error('ledger entry blob is too short');
|
|
31
|
+
}
|
|
32
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
33
|
+
const count = view.getUint16(0, true);
|
|
34
|
+
const tokens = [];
|
|
35
|
+
let offset = COUNT_BYTES;
|
|
36
|
+
for(let i = 0; i < count; i++){
|
|
37
|
+
if (offset + LENGTH_BYTES > bytes.length) {
|
|
38
|
+
throw new Error('ledger entry blob is truncated');
|
|
39
|
+
}
|
|
40
|
+
const length = view.getUint32(offset, true);
|
|
41
|
+
const start = offset + LENGTH_BYTES;
|
|
42
|
+
if (start + length > bytes.length) {
|
|
43
|
+
throw new Error('ledger entry blob is truncated');
|
|
44
|
+
}
|
|
45
|
+
tokens.push(toUTF(bytes.subarray(start, start + length)));
|
|
46
|
+
offset = start + length;
|
|
47
|
+
}
|
|
48
|
+
return tokens;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A ledger-entry resolver over the blob of the commit frame being applied. The MLS port calls
|
|
52
|
+
* it with the ids the Commit's control envelope names; it answers with the bodies sealed into
|
|
53
|
+
* that same frame — making body delivery atomic with the commit, so first delivery cannot
|
|
54
|
+
* strand.
|
|
55
|
+
*
|
|
56
|
+
* Answers with every token in the blob, not just the named subset: tokens are
|
|
57
|
+
* content-addressed, so the caller binds each body to its id by digesting, and an unrequested
|
|
58
|
+
* body is ignored. A responder can fail to answer, never inject.
|
|
59
|
+
*
|
|
60
|
+
* A blob this peer cannot open yields NO entries, not an error: the commit fails to resolve,
|
|
61
|
+
* the port raises missing-entries, and the lane leaves the cursor put so the frame is re-read
|
|
62
|
+
* — never reported as corruption. In practice this cannot arise on the apply path: the port
|
|
63
|
+
* asks only for a commit it is applying, framed at this peer's epoch, the epoch the blob is
|
|
64
|
+
* sealed under.
|
|
65
|
+
*/ export function createLedgerEntryResolver(sealedEntries, unwrap) {
|
|
66
|
+
return async (_ids)=>{
|
|
67
|
+
try {
|
|
68
|
+
const opened = await unwrap(sealedEntries);
|
|
69
|
+
const payload = opened instanceof Uint8Array ? opened : opened.payload;
|
|
70
|
+
// Body trust comes from the token's own signature + content-address, which the caller
|
|
71
|
+
// re-verifies — not from the seal, which only buys confidentiality from the hub. The
|
|
72
|
+
// recovered sender is deliberately unused.
|
|
73
|
+
return decodeLedgerEntries(payload);
|
|
74
|
+
} catch {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
package/lib/peer.d.ts
CHANGED
|
@@ -2,13 +2,35 @@ import type { Client } from '@enkaku/client';
|
|
|
2
2
|
import type { ProtocolDefinition } from '@enkaku/protocol';
|
|
3
3
|
import type { ProcedureHandlers } from '@enkaku/server';
|
|
4
4
|
import { type GatheredReply, type GatherOptions, type RequestOptions, type SuppressConfig } from '@kumiai/broadcast';
|
|
5
|
-
import type {
|
|
6
|
-
import type
|
|
5
|
+
import type { LogHub } from '@kumiai/hub-tunnel';
|
|
6
|
+
import { type CommitJournal, type LaneResult, type PendingCommit } from './commit.js';
|
|
7
|
+
import { type GroupCrypto, type GroupMLS } from './crypto.js';
|
|
8
|
+
/**
|
|
9
|
+
* The MLS half of a peer: the lifecycle port, the durable journal that carries a pending commit
|
|
10
|
+
* across a crash, and the host hook that adopts one after a restart. They arrive together or not
|
|
11
|
+
* at all — a peer with a port and no journal would silently lose every commit whose process died
|
|
12
|
+
* in the acceptance window, and the type is what stops a host wiring that.
|
|
13
|
+
*/
|
|
14
|
+
export type GroupPeerMLSParams = {
|
|
15
|
+
/** MLS lifecycle port. When provided, the peer runs the commit lane. */
|
|
16
|
+
mls: GroupMLS;
|
|
17
|
+
/** Durable single-slot journal. Written before every publish, cleared on both outcomes. */
|
|
18
|
+
journal: CommitJournal;
|
|
19
|
+
/**
|
|
20
|
+
* Adopt a journalled commit now confirmed accepted — the restart half of
|
|
21
|
+
* {@link PendingCommit.onAccepted}, over the same opaque blob: deserialize the post-commit
|
|
22
|
+
* handle, adopt it, deliver any Welcome it carried.
|
|
23
|
+
*
|
|
24
|
+
* MUST be idempotent, as `onAccepted` must: the peer cannot tell an entry whose `onAccepted`
|
|
25
|
+
* already ran from one whose process died before it. The Welcome goes out again, at-least-once
|
|
26
|
+
* and by design — see {@link PendingCommit.onAccepted} for why the sender must not suppress the
|
|
27
|
+
* repeat, and what absorbs it.
|
|
28
|
+
*/
|
|
29
|
+
adoptJournalled: (journal: Uint8Array) => Promise<void>;
|
|
30
|
+
};
|
|
7
31
|
export type GroupPeerParams<Protocols extends Record<string, ProtocolDefinition>> = {
|
|
8
|
-
hub:
|
|
32
|
+
hub: LogHub;
|
|
9
33
|
crypto: GroupCrypto;
|
|
10
|
-
/** MLS lifecycle port. When provided, the peer runs the handshake lane. */
|
|
11
|
-
mls?: GroupMLS;
|
|
12
34
|
localDID: string;
|
|
13
35
|
protocols: Protocols;
|
|
14
36
|
handlers: {
|
|
@@ -16,12 +38,32 @@ export type GroupPeerParams<Protocols extends Record<string, ProtocolDefinition>
|
|
|
16
38
|
};
|
|
17
39
|
suppress?: SuppressConfig;
|
|
18
40
|
getRandomID?: () => string;
|
|
19
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Recovery rendezvous tuning. `timeoutMs`: how long one request waits for a reply. `getDelayMs`:
|
|
43
|
+
* responder reply jitter. `deadlineMs`: how long `recover()` keeps re-requesting and rebuilding
|
|
44
|
+
* before giving up and leaving the peer degraded.
|
|
45
|
+
*/
|
|
20
46
|
recovery?: {
|
|
21
47
|
timeoutMs?: number;
|
|
22
48
|
getDelayMs?: () => number;
|
|
49
|
+
deadlineMs?: number;
|
|
23
50
|
};
|
|
24
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Commit-log retention the hub is asked to hold, in seconds. Default 30 days. A liveness dial:
|
|
53
|
+
* within it a returning member converges by pulling the log; beyond it, another live member
|
|
54
|
+
* must heal it.
|
|
55
|
+
*/
|
|
56
|
+
commitLogRetentionSeconds?: number;
|
|
57
|
+
/**
|
|
58
|
+
* How long `commit` rebases before giving up, in ms. Default 30s. Losing a compare-and-set is
|
|
59
|
+
* the expected path, not an error path.
|
|
60
|
+
*/
|
|
61
|
+
commitDeadlineMs?: number;
|
|
62
|
+
} & (GroupPeerMLSParams | {
|
|
63
|
+
mls?: undefined;
|
|
64
|
+
journal?: undefined;
|
|
65
|
+
adoptJournalled?: undefined;
|
|
66
|
+
});
|
|
25
67
|
export type ProtocolSurface<Protocol extends ProtocolDefinition> = {
|
|
26
68
|
dispatch: (prc: string, data?: Record<string, unknown>) => Promise<void>;
|
|
27
69
|
request: (prc: string, prm?: unknown, options?: RequestOptions) => Promise<unknown>;
|
|
@@ -31,19 +73,67 @@ export type ProtocolSurface<Protocol extends ProtocolDefinition> = {
|
|
|
31
73
|
export type GroupPeer<Protocols extends Record<string, ProtocolDefinition>> = {
|
|
32
74
|
protocol: <K extends keyof Protocols>(name: K) => ProtocolSurface<Protocols[K]>;
|
|
33
75
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
76
|
+
* Commit to the group, rebasing until it lands.
|
|
77
|
+
*
|
|
78
|
+
* The peer replays its journal, pulls the log to the end, calls `build()`, journals the result,
|
|
79
|
+
* and publishes conditionally on the head it pulled to. Win: `onAccepted` runs, slot clears.
|
|
80
|
+
* Lose (someone committed first): drop the pending commit untouched and call `build()` again
|
|
81
|
+
* against the now-current handle — the expected path, not an error path.
|
|
82
|
+
*
|
|
83
|
+
* `build()` produces a commit against the host's CURRENT handle and does not adopt it. Called
|
|
84
|
+
* once per attempt; it MUST read the host's live handle each time and have no side effects until
|
|
85
|
+
* `onAccepted` runs — a losing attempt is discarded whole.
|
|
86
|
+
*
|
|
87
|
+
* Holds the commit mutex for its whole run. The compare-and-set resolves races between devices,
|
|
88
|
+
* not two callers on one: two `build()` calls against a single handle would both frame at that
|
|
89
|
+
* handle's epoch and diverge.
|
|
90
|
+
*
|
|
91
|
+
* A RESULT means it landed and `onAccepted` ran; a THROW means it did not — stranded or ledger
|
|
92
|
+
* incomplete ({@link "commit".RecoveryRequiredError}), or lost the compare-and-set past its
|
|
93
|
+
* deadline ({@link "commit".CommitDeadlineError}). A throw publishes and advances nothing but
|
|
94
|
+
* may leave earlier `lost` / `reenact` work undrained (only the success path returns it) — call
|
|
95
|
+
* {@link replay} after a throw to collect it.
|
|
96
|
+
*/
|
|
97
|
+
commit: (build: () => Promise<PendingCommit>) => Promise<LaneResult>;
|
|
98
|
+
/**
|
|
99
|
+
* Replay the journal on its own, for startup: republish any pending commit under its original
|
|
100
|
+
* idempotency key and hand back what did not survive. Every lane operation replays first, so
|
|
101
|
+
* this is not the only way a loss surfaces — but it is the one a host can call before anything
|
|
102
|
+
* else, and the collector to call after a `commit()` that threw.
|
|
103
|
+
*
|
|
104
|
+
* Builds and publishes nothing, so unlike `commit()` an incomplete ledger is no hazard: it
|
|
105
|
+
* re-attempts the bootstrap like any lane operation and returns what it holds WITHOUT throwing,
|
|
106
|
+
* leaving the peer degraded until a responder answers. A `{}` result means "no orphaned work to
|
|
107
|
+
* re-issue", never "the peer is whole" — the completeness gate that a reset roster must not
|
|
108
|
+
* commit lives on `commit()`, and `replay()` is the retry that eventually clears it.
|
|
37
109
|
*/
|
|
38
|
-
|
|
110
|
+
replay: () => Promise<LaneResult>;
|
|
39
111
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
112
|
+
* Heal a peer the group has left behind: rejoin by external commit, refold the ledger, hand
|
|
113
|
+
* back the entries the group's ledger does not already hold.
|
|
114
|
+
*
|
|
115
|
+
* A TOP-LEVEL lane operation with a compare-and-set loop of its own — takes the commit mutex
|
|
116
|
+
* itself, never called from inside another lane operation. The external commit changes the
|
|
117
|
+
* ratchet tree, so it races at the head like any commit; losing (the likely outcome) DISCARDS
|
|
118
|
+
* THE GROUPINFO as well as the commit built from it — the GroupInfo describes a tree the winner
|
|
119
|
+
* already changed, so rebuilding from it would publish a commit no member can apply.
|
|
120
|
+
*
|
|
121
|
+
* A heal is TWO commits: the rejoin carries no entries (a GroupInfo has nowhere to put them), so
|
|
122
|
+
* the entries this peer still owes ride an ordinary `commit()` the CALLER makes after this
|
|
123
|
+
* releases the lane. That is `reenact`, filtered by MEMBERSHIP and never by which failure
|
|
124
|
+
* brought the peer here: re-enact an entry iff the group's authenticated ledger does not already
|
|
125
|
+
* contain it. The ledger does not dedup — a re-appended entry WINS the fold — so re-enacting one
|
|
126
|
+
* the group already holds silently reverts whatever a later admin wrote over it.
|
|
127
|
+
*
|
|
128
|
+
* `{ advanced: false }` when no member answers the rendezvous, and when the rejoin landed but
|
|
129
|
+
* the ledger could not be bootstrapped: an incomplete ledger is a reset roster, and reporting it
|
|
130
|
+
* healed would hand the host a group with every role gone. It stays degraded and retries.
|
|
131
|
+
*
|
|
132
|
+
* A peer merely BEHIND never needs this — it pulls the log and catches up.
|
|
44
133
|
*/
|
|
45
134
|
recover: () => Promise<{
|
|
46
135
|
advanced: boolean;
|
|
136
|
+
reenact: Array<string>;
|
|
47
137
|
}>;
|
|
48
138
|
resync: () => Promise<void>;
|
|
49
139
|
dispose: () => Promise<void>;
|