@kumiai/rpc 0.10.0 → 0.10.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 +5 -0
- package/lib/app-lane.d.ts +4 -0
- package/lib/app-lane.js +107 -35
- package/lib/index.d.ts +1 -1
- package/lib/peer.d.ts +4 -1
- package/lib/peer.js +172 -38
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -94,6 +94,11 @@ throw an error recognized by `isAppFrameStorageError`; the app lane retries with
|
|
|
94
94
|
past that frame. `onAppDeliveryStalled` reports a persistent storage block, a missing protocol on
|
|
95
95
|
restore, or a future-epoch frame once per blocking frame. A missing-protocol record stays
|
|
96
96
|
pending and can be delivered after that protocol is registered on a later start. An operator can
|
|
97
|
+
call `retryAppDelivery(topicID?)` to reset backoff and schedule an immediate retained pull and
|
|
98
|
+
matching delivery workers without replacing the peer. It returns when the attempt is scheduled.
|
|
99
|
+
`onAppDeliveryResumed` reports the same group, protocol, topic, position, and optional reason once
|
|
100
|
+
the reported block clears through delivery, drop, dead-frame handling, or segment rotation.
|
|
101
|
+
Disposal does not report a resume. An operator can
|
|
97
102
|
accept a frame's loss with `dropAppFrame(topicID, position)`; it can explicitly discard a
|
|
98
103
|
missing-protocol record, but refuses a pending frame under a registered protocol. It also refuses
|
|
99
104
|
to drop a sealed frame behind an earlier pending or sealed frame, because the durable cursor cannot
|
package/lib/app-lane.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ export type AppDeliveryStalled = {
|
|
|
27
27
|
error: Error;
|
|
28
28
|
reason?: 'unknown-protocol' | 'future-epoch';
|
|
29
29
|
};
|
|
30
|
+
export type AppDeliveryResumed = Omit<AppDeliveryStalled, 'error'>;
|
|
30
31
|
export type AppLaneParams = {
|
|
31
32
|
mux: HubMux;
|
|
32
33
|
crypto: GroupCrypto;
|
|
@@ -43,6 +44,7 @@ export type AppLaneParams = {
|
|
|
43
44
|
appCursorStore?: AppCursorStore | undefined;
|
|
44
45
|
onAppWindowPruned?: ((event: AppWindowPruned) => void | Promise<void>) | undefined;
|
|
45
46
|
onAppDeliveryStalled?: ((event: AppDeliveryStalled) => void) | undefined;
|
|
47
|
+
onAppDeliveryResumed?: ((event: AppDeliveryResumed) => void) | undefined;
|
|
46
48
|
/** Start workers after the caller's serialization boundary; direct lanes start immediately. */
|
|
47
49
|
scheduleDelivery?: ((start: () => void) => void) | undefined;
|
|
48
50
|
/**
|
|
@@ -62,6 +64,8 @@ export type AppLane = {
|
|
|
62
64
|
*/
|
|
63
65
|
deliver: () => Promise<void>;
|
|
64
66
|
dropFrame: (topicID: string, position: string) => Promise<void>;
|
|
67
|
+
/** Wake queued delivery for a topic, or every topic, without waiting for completion. */
|
|
68
|
+
retryDelivery: (topicID?: string) => void;
|
|
65
69
|
/** Record a log-class frame the live lane was pushed, at the moment it arrives. */
|
|
66
70
|
note: (name: string, topicID: string, message: StoredMessage, failure: unknown) => void;
|
|
67
71
|
/**
|
package/lib/app-lane.js
CHANGED
|
@@ -4,6 +4,7 @@ import { fromUTF, toB64U, toUTF } from '@sozai/codec';
|
|
|
4
4
|
import { decodeAppAAD, encodeAppAAD } from './app-aad.js';
|
|
5
5
|
import { isAppFrameStorageError, isFrameAhead } from './crypto.js';
|
|
6
6
|
import { asLogPosition, assertForwardPage } from './cursor.js';
|
|
7
|
+
import { notifyHost } from './host-notice.js';
|
|
7
8
|
import { retentionOf } from './protocol.js';
|
|
8
9
|
import { protocolTopic } from './topic.js';
|
|
9
10
|
const APP_FETCH_LIMIT = 100;
|
|
@@ -51,7 +52,7 @@ function frameID(topicID, bytes) {
|
|
|
51
52
|
* outside it may touch the buffer. That rule was a comment when this state sat in the peer's own
|
|
52
53
|
* closure; here it is the module boundary.
|
|
53
54
|
*/ export function createAppLane(params) {
|
|
54
|
-
const { mux, crypto, localDID, protocols, eventHandlers: appEventHandlers, retentionSeconds, appCursorStore, onAppWindowPruned, onAppDeliveryStalled, scheduleDelivery = (start)=>start(), anchor, groupID } = params;
|
|
55
|
+
const { mux, crypto, localDID, protocols, eventHandlers: appEventHandlers, retentionSeconds, appCursorStore, onAppWindowPruned, onAppDeliveryStalled, onAppDeliveryResumed, scheduleDelivery = (start)=>start(), anchor, groupID } = params;
|
|
55
56
|
/**
|
|
56
57
|
* The current SEGMENT's retained app frames, per protocol, in log order. A segment is the run of
|
|
57
58
|
* epochs between two roster changes — exactly the run one app topic spans — so it is pulled once
|
|
@@ -60,19 +61,66 @@ function frameID(topicID, bytes) {
|
|
|
60
61
|
const pendingRecords = [];
|
|
61
62
|
const blockingFrames = new Map();
|
|
62
63
|
let disposed = false;
|
|
63
|
-
const reportedUnknown = new
|
|
64
|
+
const reportedUnknown = new Map();
|
|
65
|
+
let pendingNotices;
|
|
66
|
+
const notifyAppHost = (callback, event)=>{
|
|
67
|
+
if (callback == null) return;
|
|
68
|
+
// A notice queued behind an in-flight save is dropped if the lane is disposed meanwhile.
|
|
69
|
+
const notify = ()=>{
|
|
70
|
+
if (!disposed) notifyHost(callback, event);
|
|
71
|
+
};
|
|
72
|
+
if (pendingNotices == null) notify();
|
|
73
|
+
else pendingNotices.push(notify);
|
|
74
|
+
};
|
|
75
|
+
const blockingKey = (topicID, position)=>`${topicID}\u0000${position}`;
|
|
76
|
+
const resumeBlocked = (topicID, position)=>{
|
|
77
|
+
const key = blockingKey(topicID, position);
|
|
78
|
+
const event = blockingFrames.get(key);
|
|
79
|
+
if (event == null || disposed) return;
|
|
80
|
+
blockingFrames.delete(key);
|
|
81
|
+
notifyAppHost(onAppDeliveryResumed, event);
|
|
82
|
+
};
|
|
83
|
+
const resumeUnknown = (record)=>{
|
|
84
|
+
const event = reportedUnknown.get(record.frame.id);
|
|
85
|
+
if (event == null || disposed) return;
|
|
86
|
+
reportedUnknown.delete(record.frame.id);
|
|
87
|
+
notifyAppHost(onAppDeliveryResumed, event);
|
|
88
|
+
};
|
|
89
|
+
const reportBlocked = (protocol, topicID, position, error, reason)=>{
|
|
90
|
+
const key = blockingKey(topicID, position);
|
|
91
|
+
if (disposed || onAppDeliveryStalled == null || blockingFrames.has(key)) return;
|
|
92
|
+
const group = groupID();
|
|
93
|
+
if (group == null) return;
|
|
94
|
+
const event = {
|
|
95
|
+
groupID: group,
|
|
96
|
+
protocol,
|
|
97
|
+
topicID,
|
|
98
|
+
position,
|
|
99
|
+
...reason == null ? {} : {
|
|
100
|
+
reason
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
blockingFrames.set(key, event);
|
|
104
|
+
notifyAppHost(onAppDeliveryStalled, {
|
|
105
|
+
...event,
|
|
106
|
+
error
|
|
107
|
+
});
|
|
108
|
+
};
|
|
64
109
|
const reportUnknown = (record)=>{
|
|
65
|
-
if (disposed || protocols[record.frame.protocol] != null || reportedUnknown.has(record.frame.id)) return;
|
|
110
|
+
if (disposed || onAppDeliveryStalled == null || protocols[record.frame.protocol] != null || reportedUnknown.has(record.frame.id)) return;
|
|
66
111
|
const group = groupID();
|
|
67
112
|
if (group == null) return;
|
|
68
|
-
|
|
69
|
-
onAppDeliveryStalled?.({
|
|
113
|
+
const event = {
|
|
70
114
|
groupID: group,
|
|
71
115
|
protocol: record.frame.protocol,
|
|
72
116
|
topicID: record.frame.topicID,
|
|
73
117
|
position: record.frame.position,
|
|
74
|
-
error: new Error(`unknown app protocol: ${record.frame.protocol}`),
|
|
75
118
|
reason: 'unknown-protocol'
|
|
119
|
+
};
|
|
120
|
+
reportedUnknown.set(record.frame.id, event);
|
|
121
|
+
notifyAppHost(onAppDeliveryStalled, {
|
|
122
|
+
...event,
|
|
123
|
+
error: new Error(`unknown app protocol: ${record.frame.protocol}`)
|
|
76
124
|
});
|
|
77
125
|
};
|
|
78
126
|
const workers = new Map();
|
|
@@ -101,7 +149,18 @@ function frameID(topicID, bytes) {
|
|
|
101
149
|
* and a push splicing it mid-iteration would step over frames.
|
|
102
150
|
*/ let tail = Promise.resolve();
|
|
103
151
|
const runAppLane = (fn)=>{
|
|
104
|
-
const op = tail.then(
|
|
152
|
+
const op = tail.then(async ()=>{
|
|
153
|
+
const notices = [];
|
|
154
|
+
pendingNotices = notices;
|
|
155
|
+
try {
|
|
156
|
+
return await fn();
|
|
157
|
+
} finally{
|
|
158
|
+
// State and cursor writes settle before observers can re-enter this lane.
|
|
159
|
+
// A reentrant reset can append more notices to this batch.
|
|
160
|
+
for(let index = 0; index < notices.length; index++)notices[index]?.();
|
|
161
|
+
pendingNotices = undefined;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
105
164
|
tail = op.then(()=>{}, ()=>{});
|
|
106
165
|
return op;
|
|
107
166
|
};
|
|
@@ -278,6 +337,8 @@ function frameID(topicID, bytes) {
|
|
|
278
337
|
await runAppLane(async ()=>{
|
|
279
338
|
const index = pendingRecords.findIndex((item)=>item.frame.id === record.frame.id);
|
|
280
339
|
if (index !== -1) pendingRecords.splice(index, 1);
|
|
340
|
+
resumeBlocked(record.frame.topicID, record.frame.position);
|
|
341
|
+
resumeUnknown(record);
|
|
281
342
|
const cursor = cursors.get(record.frame.protocol);
|
|
282
343
|
if (cursor?.topicID !== record.frame.topicID) return;
|
|
283
344
|
const frames = segment.get(record.frame.protocol);
|
|
@@ -320,6 +381,7 @@ function frameID(topicID, bytes) {
|
|
|
320
381
|
worker = {
|
|
321
382
|
running: false,
|
|
322
383
|
requested: false,
|
|
384
|
+
retryNow: false,
|
|
323
385
|
backoff: 1000
|
|
324
386
|
};
|
|
325
387
|
workers.set(name, worker);
|
|
@@ -348,7 +410,10 @@ function frameID(topicID, bytes) {
|
|
|
348
410
|
}
|
|
349
411
|
} finally{
|
|
350
412
|
worker.running = false;
|
|
351
|
-
if (
|
|
413
|
+
if (worker.retryNow) {
|
|
414
|
+
worker.retryNow = false;
|
|
415
|
+
scheduleWorker(name);
|
|
416
|
+
} else if (failed) {
|
|
352
417
|
const retry = worker.backoff;
|
|
353
418
|
worker.backoff = Math.min(worker.backoff * 2, 60_000);
|
|
354
419
|
scheduleWorker(name, retry);
|
|
@@ -359,6 +424,26 @@ function frameID(topicID, bytes) {
|
|
|
359
424
|
})();
|
|
360
425
|
}, delay);
|
|
361
426
|
};
|
|
427
|
+
const retryDelivery = (topicID)=>{
|
|
428
|
+
if (disposed) return;
|
|
429
|
+
const names = new Set([
|
|
430
|
+
...workers.keys(),
|
|
431
|
+
...pendingRecords.map((record)=>record.frame.protocol)
|
|
432
|
+
]);
|
|
433
|
+
for (const name of names){
|
|
434
|
+
if (topicID != null && cursors.get(name)?.topicID !== topicID && !pendingRecords.some((record)=>record.frame.protocol === name && record.frame.topicID === topicID)) continue;
|
|
435
|
+
const worker = workers.get(name);
|
|
436
|
+
if (worker != null) {
|
|
437
|
+
worker.backoff = 1000;
|
|
438
|
+
worker.retryNow = worker.running;
|
|
439
|
+
if (worker.timer != null) {
|
|
440
|
+
clearTimeout(worker.timer);
|
|
441
|
+
worker.timer = undefined;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
scheduleWorker(name);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
362
447
|
/**
|
|
363
448
|
* Take the staged pushes into the buffer and move the durable cursor over them, off the back of
|
|
364
449
|
* live traffic alone — a peer that is online and walks no commit still has to keep its position,
|
|
@@ -449,6 +534,7 @@ function frameID(topicID, bytes) {
|
|
|
449
534
|
frame.sealed = {
|
|
450
535
|
state: 'done'
|
|
451
536
|
};
|
|
537
|
+
resumeBlocked(cursor.topicID, frame.position);
|
|
452
538
|
continue;
|
|
453
539
|
}
|
|
454
540
|
if (crypto.pending != null) {
|
|
@@ -457,6 +543,7 @@ function frameID(topicID, bytes) {
|
|
|
457
543
|
frame.sealed = {
|
|
458
544
|
state: 'done'
|
|
459
545
|
};
|
|
546
|
+
resumeBlocked(cursor.topicID, frame.position);
|
|
460
547
|
continue;
|
|
461
548
|
}
|
|
462
549
|
}
|
|
@@ -485,37 +572,12 @@ function frameID(topicID, bytes) {
|
|
|
485
572
|
if (isFrameAhead(error)) {
|
|
486
573
|
// A future claim keeps its bytes and position even when the hub omits its commit.
|
|
487
574
|
if (crypto.pending != null) {
|
|
488
|
-
|
|
489
|
-
if (blockingFrames.get(name) !== key) {
|
|
490
|
-
blockingFrames.set(name, key);
|
|
491
|
-
const group = groupID();
|
|
492
|
-
if (group != null) onAppDeliveryStalled?.({
|
|
493
|
-
groupID: group,
|
|
494
|
-
protocol: name,
|
|
495
|
-
topicID: cursor.topicID,
|
|
496
|
-
position: frame.position,
|
|
497
|
-
error: new Error(`frame claims future epoch ${error.frameEpoch}`),
|
|
498
|
-
reason: 'future-epoch'
|
|
499
|
-
});
|
|
500
|
-
}
|
|
575
|
+
reportBlocked(name, cursor.topicID, frame.position, new Error(`frame claims future epoch ${error.frameEpoch}`), 'future-epoch');
|
|
501
576
|
}
|
|
502
577
|
continue;
|
|
503
578
|
}
|
|
504
579
|
if (crypto.pending != null && isAppFrameStorageError(error)) {
|
|
505
|
-
|
|
506
|
-
if (blockingFrames.get(name) !== key) {
|
|
507
|
-
blockingFrames.set(name, key);
|
|
508
|
-
const group = groupID();
|
|
509
|
-
if (group != null) {
|
|
510
|
-
onAppDeliveryStalled?.({
|
|
511
|
-
groupID: group,
|
|
512
|
-
protocol: name,
|
|
513
|
-
topicID: cursor.topicID,
|
|
514
|
-
position: frame.position,
|
|
515
|
-
error
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
}
|
|
580
|
+
reportBlocked(name, cursor.topicID, frame.position, error);
|
|
519
581
|
throw error;
|
|
520
582
|
}
|
|
521
583
|
// Claimed this epoch and the handle refused it — OR its AAD did not match this topic (a
|
|
@@ -525,6 +587,7 @@ function frameID(topicID, bytes) {
|
|
|
525
587
|
frame.sealed = {
|
|
526
588
|
state: 'done'
|
|
527
589
|
};
|
|
590
|
+
resumeBlocked(cursor.topicID, frame.position);
|
|
528
591
|
continue;
|
|
529
592
|
}
|
|
530
593
|
if (ref != null) {
|
|
@@ -545,6 +608,7 @@ function frameID(topicID, bytes) {
|
|
|
545
608
|
frame.sealed = {
|
|
546
609
|
state: 'done'
|
|
547
610
|
};
|
|
611
|
+
resumeBlocked(cursor.topicID, frame.position);
|
|
548
612
|
if (events == null) continue;
|
|
549
613
|
// This `unwrap` is its own ingress — not fed by `peer.ts`'s normalized inbound path — so
|
|
550
614
|
// the recovered sender is normalized HERE, once, before either use below: `localDID` (the
|
|
@@ -587,6 +651,7 @@ function frameID(topicID, bytes) {
|
|
|
587
651
|
if (unknown != null) {
|
|
588
652
|
await crypto.pending.complete(unknown.frame.id);
|
|
589
653
|
pendingRecords.splice(pendingRecords.indexOf(unknown), 1);
|
|
654
|
+
resumeUnknown(unknown);
|
|
590
655
|
return;
|
|
591
656
|
}
|
|
592
657
|
const entry = [
|
|
@@ -605,8 +670,10 @@ function frameID(topicID, bytes) {
|
|
|
605
670
|
frame.sealed = {
|
|
606
671
|
state: 'done'
|
|
607
672
|
};
|
|
673
|
+
resumeBlocked(topicID, position);
|
|
608
674
|
await advanceCursor(name, frames);
|
|
609
675
|
}),
|
|
676
|
+
retryDelivery,
|
|
610
677
|
/**
|
|
611
678
|
* MUST run BEFORE the apply, never after: once the commit applies the handle holds different
|
|
612
679
|
* key material, and those bytes are ciphertext forever. Per-FRAME-EPOCH, not per-rotation:
|
|
@@ -670,6 +737,11 @@ function frameID(topicID, bytes) {
|
|
|
670
737
|
}
|
|
671
738
|
},
|
|
672
739
|
reset: ()=>{
|
|
740
|
+
const cleared = [
|
|
741
|
+
...blockingFrames.values()
|
|
742
|
+
];
|
|
743
|
+
blockingFrames.clear();
|
|
744
|
+
if (!disposed) for (const event of cleared)notifyAppHost(onAppDeliveryResumed, event);
|
|
673
745
|
segment = new Map();
|
|
674
746
|
cursors = new Map();
|
|
675
747
|
// Staged pushes go with the buffer for the same reason: a rotation mid-push carries its own
|
package/lib/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
export type { Anchor, AnchorStore } from './anchor.js';
|
|
7
7
|
export { type AppAAD, decodeAppAAD, encodeAppAAD } from './app-aad.js';
|
|
8
8
|
export type { AppCursorStore, AppWindowPruned } from './app-cursor.js';
|
|
9
|
-
export type { AppDeliveryStalled } from './app-lane.js';
|
|
9
|
+
export type { AppDeliveryResumed, AppDeliveryStalled } from './app-lane.js';
|
|
10
10
|
export { type AppliedCommit, type ClassifyCommitParams, type CommitClassifierState, type CommitDisposition, type CommitFrameEvidence, classifyCommit, digestAppliedCommit, UNKNOWN_FRAME_VERSION, } from './classify.js';
|
|
11
11
|
export { CommitDeadlineError, type CommitJournal, type CommitKind, isHeadMismatch, type JournalEntry, JournalEpochError, type LaneResult, type LostCommit, type PendingCommit, RecoveryRequiredError, } from './commit.js';
|
|
12
12
|
export { COMMIT_FRAME_VERSION, type CommitFrame, decodeCommitFrame, encodeCommitFrame, isUnsupportedCommitFrameVersion, UnsupportedCommitFrameVersionError, } from './commit-frame.js';
|
package/lib/peer.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { LogHub } from '@kumiai/hub-tunnel';
|
|
|
5
5
|
import { type Runtime } from '@sozai/runtime';
|
|
6
6
|
import type { AnchorStore } from './anchor.js';
|
|
7
7
|
import type { AppCursorStore, AppWindowPruned } from './app-cursor.js';
|
|
8
|
-
import { type AppDeliveryStalled } from './app-lane.js';
|
|
8
|
+
import { type AppDeliveryResumed, type AppDeliveryStalled } from './app-lane.js';
|
|
9
9
|
import { type CommitJournal, type LaneResult, type PendingCommit } from './commit.js';
|
|
10
10
|
import { type GroupCrypto, type GroupMLS } from './crypto.js';
|
|
11
11
|
import { type GroupProcedureHandlers } from './handlers.js';
|
|
@@ -152,6 +152,7 @@ export type GroupPeerParams<Protocols extends Record<string, GroupProtocolDefini
|
|
|
152
152
|
onStrand?: (observation: StrandObservation) => void | Promise<void>;
|
|
153
153
|
onRecovery?: (event: RecoveryEvent) => void | Promise<void>;
|
|
154
154
|
onAppDeliveryStalled?: (event: AppDeliveryStalled) => void | Promise<void>;
|
|
155
|
+
onAppDeliveryResumed?: (event: AppDeliveryResumed) => void | Promise<void>;
|
|
155
156
|
/**
|
|
156
157
|
* Called when the hub definitively refuses to subscribe this peer to a topic — most plausibly a
|
|
157
158
|
* retention setting above the operator's own cap, which a hub refuses rather than clamps.
|
|
@@ -230,6 +231,8 @@ export type GroupPeer<Protocols extends Record<string, GroupProtocolDefinition>>
|
|
|
230
231
|
protocol: <K extends keyof Protocols>(name: K) => ProtocolSurface<Protocols[K]>;
|
|
231
232
|
/** Accept loss of one buffered sealed app frame, then resume the journal-first walk. */
|
|
232
233
|
dropAppFrame: (topicID: string, position: string) => Promise<void>;
|
|
234
|
+
/** Schedule an immediate app pull and wake matching delivery workers. No-op after disposal. */
|
|
235
|
+
retryAppDelivery: (topicID?: string) => Promise<void>;
|
|
233
236
|
/**
|
|
234
237
|
* Commit to the group, rebasing until it lands.
|
|
235
238
|
*
|
package/lib/peer.js
CHANGED
|
@@ -144,6 +144,7 @@ export function createGroupPeer(params) {
|
|
|
144
144
|
secret: new Uint8Array(),
|
|
145
145
|
epoch: crypto.epoch()
|
|
146
146
|
};
|
|
147
|
+
let appNoticeInSerial = false;
|
|
147
148
|
/**
|
|
148
149
|
* The peer's app lane: the retained-frame buffer, its durable read position, and the drain. It
|
|
149
150
|
* reads the anchor back through the accessor below rather than being handed one, since the
|
|
@@ -163,9 +164,22 @@ export function createGroupPeer(params) {
|
|
|
163
164
|
...onAppWindowPruned != null ? {
|
|
164
165
|
onAppWindowPruned
|
|
165
166
|
} : {},
|
|
166
|
-
onAppDeliveryStalled
|
|
167
|
-
|
|
168
|
-
|
|
167
|
+
...params.onAppDeliveryStalled != null ? {
|
|
168
|
+
onAppDeliveryStalled: (event)=>{
|
|
169
|
+
hostOutbox.push(()=>{
|
|
170
|
+
if (!disposed) notifyHost(params.onAppDeliveryStalled, event);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
} : {},
|
|
174
|
+
...params.onAppDeliveryResumed != null ? {
|
|
175
|
+
onAppDeliveryResumed: (event)=>{
|
|
176
|
+
const notify = ()=>{
|
|
177
|
+
if (!disposed) notifyHost(params.onAppDeliveryResumed, event);
|
|
178
|
+
};
|
|
179
|
+
if (appNoticeInSerial) hostOutbox.push(notify);
|
|
180
|
+
else queueMicrotask(notify);
|
|
181
|
+
}
|
|
182
|
+
} : {},
|
|
169
183
|
scheduleDelivery: (start)=>{
|
|
170
184
|
hostOutbox.push(start);
|
|
171
185
|
}
|
|
@@ -189,6 +203,7 @@ export function createGroupPeer(params) {
|
|
|
189
203
|
*/ const captureAnchor = async ()=>{
|
|
190
204
|
// Secret and epoch from one export: a pair from two reads can straddle a handle move.
|
|
191
205
|
const { secret, epoch } = await crypto.exportSecret(APP_TOPIC_LABEL);
|
|
206
|
+
if (disposed) return;
|
|
192
207
|
anchor = {
|
|
193
208
|
secret,
|
|
194
209
|
epoch
|
|
@@ -196,6 +211,7 @@ export function createGroupPeer(params) {
|
|
|
196
211
|
sealError = undefined;
|
|
197
212
|
finishSealBarrier();
|
|
198
213
|
await anchorStore?.save(anchor);
|
|
214
|
+
if (disposed) return;
|
|
199
215
|
// The anchor moving IS the segment boundary, so every capture ends the segment the buffer
|
|
200
216
|
// belongs to. AFTER the assignment above: the lane rebuilds its cursors off the live anchor.
|
|
201
217
|
appLane.reset();
|
|
@@ -223,6 +239,7 @@ export function createGroupPeer(params) {
|
|
|
223
239
|
let appPullActive = false;
|
|
224
240
|
let appPullTimer;
|
|
225
241
|
let appPullBackoff = 1000;
|
|
242
|
+
let appPullRetryNow = false;
|
|
226
243
|
const armAppPull = (delay)=>{
|
|
227
244
|
if (disposed || appPullActive || appPullTimer != null) return;
|
|
228
245
|
appPullTimer = setTimeout(()=>{
|
|
@@ -247,7 +264,10 @@ export function createGroupPeer(params) {
|
|
|
247
264
|
appPullBackoff = Math.min(appPullBackoff * 2, 60_000);
|
|
248
265
|
} finally{
|
|
249
266
|
appPullActive = false;
|
|
250
|
-
if (appPullNeeded && !disposed)
|
|
267
|
+
if (appPullNeeded && !disposed) {
|
|
268
|
+
armAppPull(appPullRetryNow ? 0 : retryDelay);
|
|
269
|
+
appPullRetryNow = false;
|
|
270
|
+
}
|
|
251
271
|
}
|
|
252
272
|
})();
|
|
253
273
|
}, delay);
|
|
@@ -256,6 +276,17 @@ export function createGroupPeer(params) {
|
|
|
256
276
|
appPullNeeded = true;
|
|
257
277
|
armAppPull(0);
|
|
258
278
|
};
|
|
279
|
+
const retryAppPull = ()=>{
|
|
280
|
+
if (disposed) return;
|
|
281
|
+
appPullBackoff = 1000;
|
|
282
|
+
appPullNeeded = true;
|
|
283
|
+
if (appPullTimer != null) {
|
|
284
|
+
clearTimeout(appPullTimer);
|
|
285
|
+
appPullTimer = undefined;
|
|
286
|
+
}
|
|
287
|
+
appPullRetryNow = appPullActive;
|
|
288
|
+
armAppPull(0);
|
|
289
|
+
};
|
|
259
290
|
/**
|
|
260
291
|
* The app lane's inbound path: one open per topic, fanned out as plaintext, with each frame's
|
|
261
292
|
* log position noted once its open settles. Every consumer's own `unwrap` is then a pure lookup of the
|
|
@@ -557,7 +588,9 @@ export function createGroupPeer(params) {
|
|
|
557
588
|
};
|
|
558
589
|
};
|
|
559
590
|
const rebuildEpoch = async ()=>{
|
|
591
|
+
if (disposed) return;
|
|
560
592
|
await teardownEpoch();
|
|
593
|
+
if (disposed) return;
|
|
561
594
|
await buildEpoch();
|
|
562
595
|
};
|
|
563
596
|
/**
|
|
@@ -568,10 +601,8 @@ export function createGroupPeer(params) {
|
|
|
568
601
|
* dispose and would otherwise land their publish after it.
|
|
569
602
|
*
|
|
570
603
|
* The check belongs immediately after each entry point's `await ready`, because that await is
|
|
571
|
-
* where the race lives.
|
|
572
|
-
*
|
|
573
|
-
* enough to run to completion against runtimes a teardown is about to walk. Unguarded, each
|
|
574
|
-
* entry point fails differently and none of them says why: `to()` handed back a live-looking
|
|
604
|
+
* where the race lives. Disposal can tear down while a call queued during init resumes.
|
|
605
|
+
* Unguarded, each entry point fails differently and none says why: `to()` handed back a live-looking
|
|
575
606
|
* client over an already-aborted transport, `resync()` rebuilt a whole epoch onto a disposed
|
|
576
607
|
* mux, and `commit()` published to the hub from a peer that is gone. In the mirror ordering,
|
|
577
608
|
* where teardown got there first and emptied `runtimes`, a protocol call blamed
|
|
@@ -702,9 +733,14 @@ export function createGroupPeer(params) {
|
|
|
702
733
|
* why a loss is RETURNED to the host, never handed to it under the lock.
|
|
703
734
|
*/ let commitTail = Promise.resolve();
|
|
704
735
|
const runSerial = (fn)=>{
|
|
705
|
-
const op = commitTail.then(()=>{
|
|
736
|
+
const op = commitTail.then(async ()=>{
|
|
706
737
|
journalReplayed = false;
|
|
707
|
-
|
|
738
|
+
appNoticeInSerial = true;
|
|
739
|
+
try {
|
|
740
|
+
return await fn();
|
|
741
|
+
} finally{
|
|
742
|
+
appNoticeInSerial = false;
|
|
743
|
+
}
|
|
708
744
|
});
|
|
709
745
|
commitTail = op.then(()=>{}, ()=>{});
|
|
710
746
|
return op.then((value)=>{
|
|
@@ -869,10 +905,13 @@ export function createGroupPeer(params) {
|
|
|
869
905
|
// it. Per frame-epoch, not per rotation: a segment spanning five epochs is dispensed five
|
|
870
906
|
// times off the one pull.
|
|
871
907
|
await appLane.deliver();
|
|
908
|
+
assertLive();
|
|
872
909
|
// Normalized at this ingress so an MLS-recovered form flip between the two reads (never a
|
|
873
910
|
// real membership change) does not read as one — see {@link detectRosterChange}.
|
|
874
911
|
const rosterBefore = (await port.rosterEntries()).map((e)=>normalizeDID(e.did));
|
|
912
|
+
assertLive();
|
|
875
913
|
const epochBefore = await port.readEpoch();
|
|
914
|
+
assertLive();
|
|
876
915
|
sealBarrier = new Promise((resolve)=>{
|
|
877
916
|
releaseSealBarrier = resolve;
|
|
878
917
|
});
|
|
@@ -931,6 +970,7 @@ export function createGroupPeer(params) {
|
|
|
931
970
|
commitLogHead = head == null ? null : asLogPosition(head);
|
|
932
971
|
};
|
|
933
972
|
while(true){
|
|
973
|
+
if (disposed) return false;
|
|
934
974
|
const pageAfter = reconciledHead;
|
|
935
975
|
const result = await mux.fetchTopic({
|
|
936
976
|
topicID,
|
|
@@ -941,6 +981,7 @@ export function createGroupPeer(params) {
|
|
|
941
981
|
} : {},
|
|
942
982
|
limit: COMMIT_FETCH_LIMIT
|
|
943
983
|
});
|
|
984
|
+
if (disposed) return false;
|
|
944
985
|
// A short page may carry a one-shot fork reveal below the cursor. Only a full page loops.
|
|
945
986
|
if (result.messages.length === COMMIT_FETCH_LIMIT) {
|
|
946
987
|
assertForwardPage(pageAfter, result.messages);
|
|
@@ -971,6 +1012,7 @@ export function createGroupPeer(params) {
|
|
|
971
1012
|
// No digest: the frame's version put its commit bytes out of reach entirely. Settled at
|
|
972
1013
|
// `ahead` before the digest is read.
|
|
973
1014
|
const localEpoch = await port.readEpoch();
|
|
1015
|
+
if (disposed) return false;
|
|
974
1016
|
const unreadable = classifyCommit({
|
|
975
1017
|
header: UNKNOWN_FRAME_VERSION,
|
|
976
1018
|
sequenceID: position,
|
|
@@ -1014,6 +1056,7 @@ export function createGroupPeer(params) {
|
|
|
1014
1056
|
// heal from — dropping it would step over the group's whole future. To the classifier.
|
|
1015
1057
|
if (isUnsupportedCommitFrameVersion(error)) {
|
|
1016
1058
|
const localEpoch = await port.readEpoch();
|
|
1059
|
+
if (disposed) return false;
|
|
1017
1060
|
const unreadable = classifyCommit({
|
|
1018
1061
|
header: UNKNOWN_FRAME_VERSION,
|
|
1019
1062
|
sequenceID: position,
|
|
@@ -1054,8 +1097,10 @@ export function createGroupPeer(params) {
|
|
|
1054
1097
|
// trusted: it could stamp every recipient's own DID onto one poison frame and make the
|
|
1055
1098
|
// whole group heal at once.
|
|
1056
1099
|
let localEpoch = await port.readEpoch();
|
|
1100
|
+
if (disposed) return false;
|
|
1057
1101
|
while(true){
|
|
1058
1102
|
const readHeader = await port.readCommitHeader(commitFrame.commit);
|
|
1103
|
+
if (disposed) return false;
|
|
1059
1104
|
// Normalized before `classifyCommit` compares it against `state.localDID` (also
|
|
1060
1105
|
// normalized, at construction) — an own-unmerged commit authenticated under a different
|
|
1061
1106
|
// form of this member's own DID must still be recognized as its own.
|
|
@@ -1136,12 +1181,20 @@ export function createGroupPeer(params) {
|
|
|
1136
1181
|
}
|
|
1137
1182
|
// Framed at this peer's epoch, by somebody else: a frame it can apply. Everything below is
|
|
1138
1183
|
// the port's answer to it.
|
|
1184
|
+
if (disposed) return false;
|
|
1139
1185
|
const framedEpoch = localEpoch;
|
|
1140
1186
|
let applied;
|
|
1141
1187
|
try {
|
|
1142
1188
|
// Through the seam, like every other site that ratchets the handle: it reads this
|
|
1143
1189
|
// epoch's app frames ahead of the apply and takes the anchor if the roster moved.
|
|
1144
|
-
applied = await advanceHandle(port, ()=>{
|
|
1190
|
+
applied = await advanceHandle(port, async ()=>{
|
|
1191
|
+
if (disposed) {
|
|
1192
|
+
return {
|
|
1193
|
+
advanced: false,
|
|
1194
|
+
epochBefore: framedEpoch,
|
|
1195
|
+
epochAfter: framedEpoch
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1145
1198
|
return port.processCommit(commitFrame.commit, {
|
|
1146
1199
|
senderDID: message.senderDID,
|
|
1147
1200
|
// The resolver, not the bodies: the blob opens only if the port asks for entries
|
|
@@ -1156,6 +1209,7 @@ export function createGroupPeer(params) {
|
|
|
1156
1209
|
// Only an APPLIED commit says anything about the group.
|
|
1157
1210
|
(result)=>result.advanced && header?.external === true, (result)=>result);
|
|
1158
1211
|
} catch (error) {
|
|
1212
|
+
if (disposed) return false;
|
|
1159
1213
|
if (!isMissingLedgerEntries(error)) {
|
|
1160
1214
|
// The port broke its contract. The cursor stays and the frame is re-read — the pull is
|
|
1161
1215
|
// a retry, and this is not an outcome it can name.
|
|
@@ -1173,6 +1227,7 @@ export function createGroupPeer(params) {
|
|
|
1173
1227
|
reconciledHead = position;
|
|
1174
1228
|
break;
|
|
1175
1229
|
}
|
|
1230
|
+
if (disposed) return false;
|
|
1176
1231
|
if (!applied.advanced && applied.epochBefore !== framedEpoch) {
|
|
1177
1232
|
advancedEpoch = true;
|
|
1178
1233
|
localEpoch = applied.epochBefore;
|
|
@@ -1208,25 +1263,35 @@ export function createGroupPeer(params) {
|
|
|
1208
1263
|
* nothing further is coming to prompt them, so a peer whose backlog is entirely at its current
|
|
1209
1264
|
* epoch (or whose log held no commits at all) would otherwise never read a thing.
|
|
1210
1265
|
*/ const pullCommits = async ()=>{
|
|
1266
|
+
if (disposed) return false;
|
|
1211
1267
|
if (!journalReplayed) {
|
|
1212
1268
|
throw new Error('pullCommits: the journal must be replayed first in every lane operation, or a peer that crashed on its own commit heals from it instead of adopting it');
|
|
1213
1269
|
}
|
|
1214
1270
|
if (mls == null || commitTopicID == null) return false;
|
|
1215
1271
|
const epochBefore = await mls.readEpoch();
|
|
1272
|
+
if (disposed) return false;
|
|
1216
1273
|
const anchorBefore = anchor;
|
|
1217
1274
|
try {
|
|
1218
1275
|
const advanced = await walkCommits(mls, commitTopicID);
|
|
1276
|
+
if (disposed) return false;
|
|
1219
1277
|
await appLane.deliver();
|
|
1278
|
+
if (disposed) return false;
|
|
1220
1279
|
return advanced;
|
|
1221
1280
|
} catch (error) {
|
|
1281
|
+
if (disposed) return false;
|
|
1222
1282
|
// A failed final drain can follow applied commits. Refresh the runtime before a retry.
|
|
1223
|
-
if (inboxLane != null
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1283
|
+
if (inboxLane != null) {
|
|
1284
|
+
const epochAfter = await mls.readEpoch();
|
|
1285
|
+
if (disposed) return false;
|
|
1286
|
+
if (epochAfter !== epochBefore || anchor !== anchorBefore) {
|
|
1287
|
+
try {
|
|
1288
|
+
await rebuildEpoch();
|
|
1289
|
+
} catch {
|
|
1290
|
+
// The walk's original failure remains the retryable cause.
|
|
1291
|
+
}
|
|
1228
1292
|
}
|
|
1229
1293
|
}
|
|
1294
|
+
if (disposed) return false;
|
|
1230
1295
|
if (crypto.pending != null && !appPullActive) {
|
|
1231
1296
|
appPullNeeded = true;
|
|
1232
1297
|
armAppPull(appPullBackoff);
|
|
@@ -1237,7 +1302,7 @@ export function createGroupPeer(params) {
|
|
|
1237
1302
|
};
|
|
1238
1303
|
/** Pull the commit log, and rebuild the app lane if the pull moved the epoch. */ const reconcileCommits = async ()=>{
|
|
1239
1304
|
const advanced = await pullCommits();
|
|
1240
|
-
if (advanced) await rebuildEpoch();
|
|
1305
|
+
if (!disposed && advanced) await rebuildEpoch();
|
|
1241
1306
|
};
|
|
1242
1307
|
/**
|
|
1243
1308
|
* A commit-topic delivery is a WAKEUP, nothing more. Frames come from the pull, never the push:
|
|
@@ -1246,20 +1311,23 @@ export function createGroupPeer(params) {
|
|
|
1246
1311
|
* never become the cursor.
|
|
1247
1312
|
*/ const onCommitDelivery = (_message, ack)=>{
|
|
1248
1313
|
ack();
|
|
1249
|
-
void
|
|
1250
|
-
|
|
1251
|
-
//
|
|
1252
|
-
// `settled`, never the mutex. Refused SILENTLY, unlike every host-facing entry point — there
|
|
1253
|
-
// is no caller to tell, and the catch below would swallow a throw anyway.
|
|
1314
|
+
void ready.then(()=>{
|
|
1315
|
+
// `ready` uses this mutex for its seed and pending pull. Never hold it while waiting for
|
|
1316
|
+
// initialization, and refuse a delivery that resumed after disposal.
|
|
1254
1317
|
if (disposed) return;
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1318
|
+
return runSerial(async ()=>{
|
|
1319
|
+
// A delivery queued before disposal is refused silently; it has no caller to notify.
|
|
1320
|
+
if (disposed) return;
|
|
1321
|
+
// A wakeup is a lane operation: step 0, the ledger invariant, then the pull. No return
|
|
1322
|
+
// value, so anything found is stashed for the next call that has one.
|
|
1323
|
+
const replayed = await replayJournal();
|
|
1324
|
+
if (mls != null && await ensureLedger(Date.now() + recoveryTimeoutMs)) {
|
|
1325
|
+
await finalizeBootstrap(mls);
|
|
1326
|
+
}
|
|
1327
|
+
if (disposed) return;
|
|
1328
|
+
const pulled = await pullCommits();
|
|
1329
|
+
if (!disposed && (replayed || pulled)) await rebuildEpoch();
|
|
1330
|
+
});
|
|
1263
1331
|
}).catch(()=>{
|
|
1264
1332
|
// pull failed (e.g. processCommit threw); the cursor did not advance, so the next wakeup
|
|
1265
1333
|
// re-reads those frames
|
|
@@ -1300,6 +1368,7 @@ export function createGroupPeer(params) {
|
|
|
1300
1368
|
const initControlLanes = async ()=>{
|
|
1301
1369
|
if (mls == null) return;
|
|
1302
1370
|
const recoverySecret = await mls.exportRecoverySecret();
|
|
1371
|
+
if (disposed) return;
|
|
1303
1372
|
commitTopicID = commitTopic(recoverySecret);
|
|
1304
1373
|
rendezvousTopicID = rendezvousTopic(recoverySecret);
|
|
1305
1374
|
// Both topics subscribed once for the peer's whole life — NOT rebuilt on resync, so a
|
|
@@ -1313,11 +1382,18 @@ export function createGroupPeer(params) {
|
|
|
1313
1382
|
// exactly the ones no push will bring it. A lane operation, so the journal replays AHEAD of
|
|
1314
1383
|
// it. Neither step rebuilds the epoch — buildEpoch runs next.
|
|
1315
1384
|
await runSerial(async ()=>{
|
|
1385
|
+
if (disposed) return;
|
|
1316
1386
|
await replayJournal();
|
|
1387
|
+
if (disposed) return;
|
|
1317
1388
|
// A peer restored with an incomplete ledger was killed between rejoining and bootstrapping.
|
|
1318
1389
|
// The invariant finds it here and at every later lane operation, with no memory of how it
|
|
1319
1390
|
// got there.
|
|
1320
|
-
|
|
1391
|
+
const ledgerReady = await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
1392
|
+
if (disposed) return;
|
|
1393
|
+
if (ledgerReady) {
|
|
1394
|
+
await finalizeBootstrap(mls);
|
|
1395
|
+
if (disposed) return;
|
|
1396
|
+
}
|
|
1321
1397
|
await pullCommits();
|
|
1322
1398
|
}).catch(()=>{
|
|
1323
1399
|
// a failed seed leaves the cursor put; the next wakeup replays and pulls again
|
|
@@ -1354,7 +1430,11 @@ export function createGroupPeer(params) {
|
|
|
1354
1430
|
journalReplayed = true;
|
|
1355
1431
|
if (mls == null || journal == null || commitTopicID == null) return false;
|
|
1356
1432
|
const entry = await journal.get();
|
|
1357
|
-
if (entry == null) return false;
|
|
1433
|
+
if (disposed || entry == null) return false;
|
|
1434
|
+
const adoptIfLive = async ()=>{
|
|
1435
|
+
if (disposed) return;
|
|
1436
|
+
await adoptJournalled(entry.journal);
|
|
1437
|
+
};
|
|
1358
1438
|
if (entry.acceptedAs != null) {
|
|
1359
1439
|
// It landed and this peer recorded that before adopting. Nothing to ask: no republish, no
|
|
1360
1440
|
// re-seal, no network. The recorded sequenceID is both the last position processed and the
|
|
@@ -1369,7 +1449,8 @@ export function createGroupPeer(params) {
|
|
|
1369
1449
|
});
|
|
1370
1450
|
// Through the seam: the adopt ratchets the handle, so this epoch's app frames are read
|
|
1371
1451
|
// first and the anchor is taken if the journalled commit moved the roster.
|
|
1372
|
-
await advanceHandle(mls,
|
|
1452
|
+
await advanceHandle(mls, adoptIfLive);
|
|
1453
|
+
if (disposed) return false;
|
|
1373
1454
|
await journal.clear(entry.publishID);
|
|
1374
1455
|
return true;
|
|
1375
1456
|
}
|
|
@@ -1377,10 +1458,12 @@ export function createGroupPeer(params) {
|
|
|
1377
1458
|
// which equals the framed epoch only while `onAccepted` is the sole place the host adopts.
|
|
1378
1459
|
// Sealing anyway publishes a blob no member can open and wedges the lane for the whole group.
|
|
1379
1460
|
const handleEpoch = await mls.readEpoch();
|
|
1461
|
+
if (disposed) return false;
|
|
1380
1462
|
if (handleEpoch !== entry.epoch) {
|
|
1381
1463
|
throw new JournalEpochError(`commit replay: the journalled commit was framed at epoch ${entry.epoch}, and this group is now at ${handleEpoch}. A commit is adopted in onAccepted, and nowhere else.`);
|
|
1382
1464
|
}
|
|
1383
1465
|
const { payload } = await frameCommit(entry.commit, entry.bodies, entry.epoch);
|
|
1466
|
+
if (disposed) return false;
|
|
1384
1467
|
let sequenceID;
|
|
1385
1468
|
try {
|
|
1386
1469
|
sequenceID = (await mux.publish({
|
|
@@ -1390,7 +1473,9 @@ export function createGroupPeer(params) {
|
|
|
1390
1473
|
expectedHead: entry.expectedHead,
|
|
1391
1474
|
publishID: entry.publishID
|
|
1392
1475
|
})).sequenceID;
|
|
1476
|
+
if (disposed) return false;
|
|
1393
1477
|
} catch (error) {
|
|
1478
|
+
if (disposed) return false;
|
|
1394
1479
|
if (!isHeadMismatch(error)) {
|
|
1395
1480
|
// Outcome UNKNOWN — the hub may have accepted and failed to say so. Leave the slot
|
|
1396
1481
|
// exactly as it is: the next lane operation asks again.
|
|
@@ -1400,6 +1485,7 @@ export function createGroupPeer(params) {
|
|
|
1400
1485
|
// again — the process that held it is gone — so hand back what survived and clear the slot:
|
|
1401
1486
|
// the notice must not be lost, never the slot.
|
|
1402
1487
|
await journal.clear(entry.publishID);
|
|
1488
|
+
if (disposed) return false;
|
|
1403
1489
|
lostCommit = entry.kind === 'ledger' ? {
|
|
1404
1490
|
kind: 'ledger',
|
|
1405
1491
|
tokens: entry.bodies,
|
|
@@ -1417,6 +1503,7 @@ export function createGroupPeer(params) {
|
|
|
1417
1503
|
// framed epoch, and a crash between the two would leave a journalled commit indistinguishable
|
|
1418
1504
|
// from a host that adopted out of band. Written first, replay is idempotent.
|
|
1419
1505
|
await journal.markAccepted(entry.publishID, sequenceID);
|
|
1506
|
+
if (disposed) return false;
|
|
1420
1507
|
// This peer's own accepted frame is BOTH the last thing it processed and the log's tip —
|
|
1421
1508
|
// otherwise a `commit()` right after a `replay()` would anchor on a stale tip.
|
|
1422
1509
|
const accepted = asLogPosition(sequenceID);
|
|
@@ -1426,7 +1513,8 @@ export function createGroupPeer(params) {
|
|
|
1426
1513
|
sequenceID: accepted,
|
|
1427
1514
|
digest: digestAppliedCommit(entry.commit)
|
|
1428
1515
|
});
|
|
1429
|
-
await advanceHandle(mls,
|
|
1516
|
+
await advanceHandle(mls, adoptIfLive);
|
|
1517
|
+
if (disposed) return false;
|
|
1430
1518
|
await journal.clear(entry.publishID);
|
|
1431
1519
|
return true;
|
|
1432
1520
|
};
|
|
@@ -1448,15 +1536,17 @@ export function createGroupPeer(params) {
|
|
|
1448
1536
|
};
|
|
1449
1537
|
};
|
|
1450
1538
|
const repairRejoin = async ()=>{
|
|
1451
|
-
if (awaitingBootstrap == null) return;
|
|
1539
|
+
if (disposed || awaitingBootstrap == null) return;
|
|
1452
1540
|
// Restore the subscription before gathering the ledger: responders may already have
|
|
1453
1541
|
// rotated to the new app anchor. A failed repair remains pending for the next lane call.
|
|
1454
1542
|
if (rejoinAnchorNeedsCapture) {
|
|
1455
1543
|
await captureAnchor();
|
|
1544
|
+
if (disposed) return;
|
|
1456
1545
|
rejoinAnchorNeedsCapture = false;
|
|
1457
1546
|
}
|
|
1458
1547
|
if (rejoinRuntimeNeedsBuild) {
|
|
1459
1548
|
await rebuildEpoch();
|
|
1549
|
+
if (disposed) return;
|
|
1460
1550
|
rejoinRuntimeNeedsBuild = false;
|
|
1461
1551
|
}
|
|
1462
1552
|
};
|
|
@@ -1506,9 +1596,12 @@ export function createGroupPeer(params) {
|
|
|
1506
1596
|
*/ const ensureLedger = async (deadline)=>{
|
|
1507
1597
|
if (mls == null || rendezvousTopicID == null) return true;
|
|
1508
1598
|
await repairRejoin();
|
|
1599
|
+
if (disposed) return false;
|
|
1509
1600
|
const port = mls;
|
|
1510
1601
|
const topicID = rendezvousTopicID;
|
|
1511
|
-
|
|
1602
|
+
const complete = await port.isLedgerComplete();
|
|
1603
|
+
if (disposed) return false;
|
|
1604
|
+
if (complete) return true;
|
|
1512
1605
|
// Gather the WHOLE ordered ledger — not "the missing ids", which nothing enumerates: the
|
|
1513
1606
|
// authenticated head is a chain digest, not a list. Every responder with a complete ledger
|
|
1514
1607
|
// answers, each checked against the head this handle carries: a lying responder can withhold,
|
|
@@ -1586,8 +1679,10 @@ export function createGroupPeer(params) {
|
|
|
1586
1679
|
const slot = journal;
|
|
1587
1680
|
const topicID = commitTopicID;
|
|
1588
1681
|
const op = runSerial(async ()=>{
|
|
1682
|
+
assertLive();
|
|
1589
1683
|
// 0. Replay the journal, ahead of the pull.
|
|
1590
1684
|
if (await replayJournal()) await rebuildEpoch();
|
|
1685
|
+
assertLive();
|
|
1591
1686
|
// 0.5. Refuse on an incomplete ledger. A rejoin whose bootstrap never finished leaves a
|
|
1592
1687
|
// reset roster: the fold sees only the genesis creator as admin, every promotion since
|
|
1593
1688
|
// is invisible. `ensureLedger` repairs it in place when a responder answers; when none
|
|
@@ -1599,14 +1694,17 @@ export function createGroupPeer(params) {
|
|
|
1599
1694
|
// would rotate the tree for nothing — the gather that just failed IS the repair,
|
|
1600
1695
|
// re-running at the head of the next lane operation.
|
|
1601
1696
|
if (!await ensureLedger(Date.now() + recoveryTimeoutMs)) {
|
|
1697
|
+
assertLive();
|
|
1602
1698
|
throw new RecoveryRequiredError('commit: the ledger is incomplete, so this handle rejoined the group and its bootstrap has not completed — its roster has reset, and a commit built now would be judged against a group whose admins it cannot see. It must finish bootstrapping its ledger before it can commit again.');
|
|
1603
1699
|
}
|
|
1604
1700
|
await finalizeBootstrap(mls);
|
|
1701
|
+
assertLive();
|
|
1605
1702
|
const deadline = Date.now() + commitDeadlineMs;
|
|
1606
1703
|
for(let attempt = 0; attempt < COMMIT_ATTEMPT_CEILING; attempt++){
|
|
1607
1704
|
// 1. Pull the log to the end: every frame processed, and the tip to race at learned from
|
|
1608
1705
|
// the store's own reply.
|
|
1609
1706
|
await reconcileCommits();
|
|
1707
|
+
assertLive();
|
|
1610
1708
|
// The pull found positive evidence this peer is off the group's line, which stands
|
|
1611
1709
|
// whether or not a following heal lands. Unwind rather than race: on the `ahead` path the
|
|
1612
1710
|
// pull already took the live tip, so a commit here would win at an epoch it never caught
|
|
@@ -1618,6 +1716,7 @@ export function createGroupPeer(params) {
|
|
|
1618
1716
|
// 2. Build against the host's CURRENT handle, adopting nothing. `build` closes over that
|
|
1619
1717
|
// handle, so a rebased retry frames at the rebased epoch.
|
|
1620
1718
|
const pending = await build();
|
|
1719
|
+
assertLive();
|
|
1621
1720
|
// 3. Journal BEFORE publishing, durably: from here to the hub's answer is the crash
|
|
1622
1721
|
// window, and the slot is the only thing that survives it.
|
|
1623
1722
|
//
|
|
@@ -1626,6 +1725,7 @@ export function createGroupPeer(params) {
|
|
|
1626
1725
|
const publishID = newPublishID();
|
|
1627
1726
|
const expectedHead = commitLogHead;
|
|
1628
1727
|
const { payload, epoch: framedEpoch } = await frameCommit(pending.commit, pending.bodies, crypto.frameEpoch(pending.commit));
|
|
1728
|
+
assertLive();
|
|
1629
1729
|
await slot.put({
|
|
1630
1730
|
publishID,
|
|
1631
1731
|
expectedHead,
|
|
@@ -1637,6 +1737,7 @@ export function createGroupPeer(params) {
|
|
|
1637
1737
|
kind: pending.kind,
|
|
1638
1738
|
journal: pending.journal
|
|
1639
1739
|
});
|
|
1740
|
+
assertLive();
|
|
1640
1741
|
// 4. Publish, conditional on the head the pull reached.
|
|
1641
1742
|
let sequenceID;
|
|
1642
1743
|
try {
|
|
@@ -1647,7 +1748,11 @@ export function createGroupPeer(params) {
|
|
|
1647
1748
|
expectedHead,
|
|
1648
1749
|
publishID
|
|
1649
1750
|
})).sequenceID;
|
|
1751
|
+
assertLive();
|
|
1650
1752
|
} catch (error) {
|
|
1753
|
+
if (disposed) throw new PeerDisposedError('Peer is disposed', {
|
|
1754
|
+
cause: error
|
|
1755
|
+
});
|
|
1651
1756
|
if (!isHeadMismatch(error)) {
|
|
1652
1757
|
// Unknown outcome: the frame may be in the log. The slot STAYS — the next lane
|
|
1653
1758
|
// operation replays it and asks the store which it was.
|
|
@@ -1656,6 +1761,7 @@ export function createGroupPeer(params) {
|
|
|
1656
1761
|
// 6. Lost the compare-and-set: someone committed first — expected, not an error. Drop
|
|
1657
1762
|
// the pending commit untouched, clear the slot, and go back to step 1.
|
|
1658
1763
|
await slot.clear(publishID);
|
|
1764
|
+
assertLive();
|
|
1659
1765
|
if (Date.now() >= deadline) {
|
|
1660
1766
|
// The last lost compare-and-set, carried: it is the proximate reason this attempt
|
|
1661
1767
|
// restarted, and the deadline only names how many such losses ran out the clock.
|
|
@@ -1670,6 +1776,7 @@ export function createGroupPeer(params) {
|
|
|
1670
1776
|
// carrying none at a later epoch is a host that adopted outside `onAccepted`. Recorded
|
|
1671
1777
|
// after the adopt, the two would be indistinguishable.
|
|
1672
1778
|
await slot.markAccepted(publishID, sequenceID);
|
|
1779
|
+
assertLive();
|
|
1673
1780
|
// The commit is the group's now — this frame is both the last position processed and the
|
|
1674
1781
|
// log's new tip.
|
|
1675
1782
|
const accepted = asLogPosition(sequenceID);
|
|
@@ -1688,8 +1795,11 @@ export function createGroupPeer(params) {
|
|
|
1688
1795
|
// Add sits on a topic the new member's handle cannot derive — silently, and no restart
|
|
1689
1796
|
// heals it.
|
|
1690
1797
|
await advanceHandle(mls, ()=>pending.onAccepted());
|
|
1798
|
+
assertLive();
|
|
1691
1799
|
await slot.clear(publishID);
|
|
1800
|
+
assertLive();
|
|
1692
1801
|
await rebuildEpoch();
|
|
1802
|
+
assertLive();
|
|
1693
1803
|
return takeLost();
|
|
1694
1804
|
}
|
|
1695
1805
|
throw new CommitDeadlineError(`commit: gave up after ${COMMIT_ATTEMPT_CEILING} attempts inside its deadline`);
|
|
@@ -2021,7 +2131,7 @@ export function createGroupPeer(params) {
|
|
|
2021
2131
|
* flight absorbs any trigger raised while it runs: the frame that raised it is still in the log,
|
|
2022
2132
|
* and the next pull raises it again if the heal did not settle it.
|
|
2023
2133
|
*/ const healIfRequested = async ()=>{
|
|
2024
|
-
if (!healRequested || activeRecovery != null) return;
|
|
2134
|
+
if (disposed || !healRequested || activeRecovery != null) return;
|
|
2025
2135
|
healRequested = false;
|
|
2026
2136
|
try {
|
|
2027
2137
|
await runRecovery('automatic');
|
|
@@ -2072,25 +2182,33 @@ export function createGroupPeer(params) {
|
|
|
2072
2182
|
// at its own add epoch — the same epoch every existing member rotates to on applying that
|
|
2073
2183
|
// add, so the two agree with no exchange between them.
|
|
2074
2184
|
const stored = await anchorStore?.load();
|
|
2185
|
+
if (disposed) return;
|
|
2075
2186
|
if (stored != null) {
|
|
2076
2187
|
anchor = stored;
|
|
2077
2188
|
} else {
|
|
2078
2189
|
await captureAnchor();
|
|
2079
2190
|
}
|
|
2191
|
+
if (disposed) return;
|
|
2080
2192
|
await restorePending();
|
|
2081
2193
|
if (disposed) return;
|
|
2082
2194
|
await initControlLanes();
|
|
2195
|
+
if (disposed) return;
|
|
2083
2196
|
await buildEpoch();
|
|
2197
|
+
if (disposed) return;
|
|
2084
2198
|
// The seed pull precedes the app listeners. Read once more after registration to close
|
|
2085
2199
|
// the publication gap; a failed read is retried on the same independent schedule.
|
|
2086
2200
|
if (crypto.pending != null) {
|
|
2087
2201
|
try {
|
|
2088
2202
|
await runSerial(async ()=>{
|
|
2203
|
+
if (disposed) return;
|
|
2089
2204
|
await replayJournal();
|
|
2205
|
+
if (disposed) return;
|
|
2090
2206
|
await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
2207
|
+
if (disposed) return;
|
|
2091
2208
|
await reconcileCommits();
|
|
2092
2209
|
});
|
|
2093
2210
|
} catch {
|
|
2211
|
+
if (disposed) return;
|
|
2094
2212
|
appPullNeeded = true;
|
|
2095
2213
|
armAppPull(appPullBackoff);
|
|
2096
2214
|
}
|
|
@@ -2151,15 +2269,23 @@ export function createGroupPeer(params) {
|
|
|
2151
2269
|
};
|
|
2152
2270
|
return {
|
|
2153
2271
|
protocol: protocolMethod,
|
|
2272
|
+
retryAppDelivery: async (topicID)=>{
|
|
2273
|
+
if (disposed) return;
|
|
2274
|
+
appLane.retryDelivery(topicID);
|
|
2275
|
+
retryAppPull();
|
|
2276
|
+
},
|
|
2154
2277
|
dropAppFrame: async (topicID, position)=>{
|
|
2155
2278
|
await ready;
|
|
2156
2279
|
assertLive();
|
|
2157
2280
|
await runSerial(async ()=>{
|
|
2281
|
+
assertLive();
|
|
2158
2282
|
await appLane.dropFrame(topicID, position);
|
|
2283
|
+
assertLive();
|
|
2159
2284
|
await replayJournal();
|
|
2160
2285
|
await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
2161
2286
|
await reconcileCommits();
|
|
2162
2287
|
});
|
|
2288
|
+
assertLive();
|
|
2163
2289
|
},
|
|
2164
2290
|
commit,
|
|
2165
2291
|
replay,
|
|
@@ -2177,6 +2303,7 @@ export function createGroupPeer(params) {
|
|
|
2177
2303
|
// one set of runtimes. Safe to wrap only because `rebuildEpoch` takes no lock itself and
|
|
2178
2304
|
// this is a top-level entry — `runSerial` is not reentrant.
|
|
2179
2305
|
await runSerial(()=>rebuildEpoch());
|
|
2306
|
+
assertLive();
|
|
2180
2307
|
},
|
|
2181
2308
|
anchorEpoch: ()=>anchor.epoch,
|
|
2182
2309
|
reauthorize: ()=>{
|
|
@@ -2207,7 +2334,14 @@ export function createGroupPeer(params) {
|
|
|
2207
2334
|
])finish();
|
|
2208
2335
|
disposePromise = (async ()=>{
|
|
2209
2336
|
// Tear down even a peer whose init failed — it still holds a hub drain.
|
|
2210
|
-
|
|
2337
|
+
// Initialization may be waiting on a host port that never answers. Disposal closes the
|
|
2338
|
+
// mux now; late initialization checks `disposed` before registering more listeners.
|
|
2339
|
+
await Promise.race([
|
|
2340
|
+
settled,
|
|
2341
|
+
pendingRestoreAborted
|
|
2342
|
+
]);
|
|
2343
|
+
// The host owns a bootstrap already touching its handle, including one started by init.
|
|
2344
|
+
// Drain it without waiting for ready, which can itself depend on the seed lane.
|
|
2211
2345
|
await Promise.allSettled([
|
|
2212
2346
|
...inFlightBootstraps
|
|
2213
2347
|
]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/rpc",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
4
4
|
"description": "High-level MLS-aware group RPC for Enkaku: bus broadcast/anycast/gather plus directed RPC over the pub/sub hub",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rpc",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"lib/*"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@enkaku/client": "^0.21.
|
|
30
|
-
"@enkaku/protocol": "^0.21.
|
|
31
|
-
"@enkaku/server": "^0.21.
|
|
32
|
-
"@enkaku/transport": "^0.21.
|
|
29
|
+
"@enkaku/client": "^0.21.3",
|
|
30
|
+
"@enkaku/protocol": "^0.21.3",
|
|
31
|
+
"@enkaku/server": "^0.21.3",
|
|
32
|
+
"@enkaku/transport": "^0.21.3",
|
|
33
33
|
"@kokuin/token": "^0.5.0",
|
|
34
34
|
"@kumiai/broadcast": "^0.10.0",
|
|
35
35
|
"@kumiai/hub-protocol": "^0.10.0",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@kumiai/hub-conformance": "^0.10.0",
|
|
46
46
|
"@kumiai/rpc-conformance": "^0.10.0",
|
|
47
47
|
"@noble/curves": "^2.4.0",
|
|
48
|
-
"vitest": "^5.0.
|
|
48
|
+
"vitest": "^5.0.2"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public"
|