@kumiai/rpc 0.10.2 → 0.10.4
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/anchor.d.ts +15 -14
- package/lib/anchor.js +2 -12
- package/lib/app-cursor.d.ts +5 -8
- package/lib/app-cursor.js +3 -6
- package/lib/app-lane.js +16 -21
- package/lib/app-outbox.d.ts +57 -0
- package/lib/app-outbox.js +97 -0
- package/lib/commit.d.ts +6 -0
- package/lib/crypto.d.ts +67 -16
- package/lib/errors.d.ts +15 -0
- package/lib/errors.js +20 -0
- package/lib/frame-size.d.ts +8 -0
- package/lib/frame-size.js +13 -0
- package/lib/handshake.d.ts +2 -0
- package/lib/handshake.js +7 -1
- package/lib/host-boundary.d.ts +109 -0
- package/lib/host-boundary.js +258 -0
- package/lib/hub-mux.d.ts +2 -4
- package/lib/hub-mux.js +33 -12
- package/lib/index.d.ts +8 -5
- package/lib/index.js +4 -2
- package/lib/log-delivery.d.ts +46 -0
- package/lib/log-delivery.js +177 -0
- package/lib/peer-epoch.d.ts +49 -0
- package/lib/peer-epoch.js +387 -0
- package/lib/peer-types.d.ts +343 -0
- package/lib/peer-types.js +26 -0
- package/lib/peer.d.ts +4 -305
- package/lib/peer.js +1037 -669
- package/lib/recovery-confirmation.d.ts +63 -0
- package/lib/recovery-confirmation.js +132 -0
- package/lib/recovery.d.ts +14 -0
- package/lib/recovery.js +72 -8
- package/package.json +11 -11
package/lib/peer.d.ts
CHANGED
|
@@ -1,305 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { type Runtime } from '@sozai/runtime';
|
|
6
|
-
import type { AnchorStore } from './anchor.js';
|
|
7
|
-
import type { AppCursorStore, AppWindowPruned } from './app-cursor.js';
|
|
8
|
-
import { type AppDeliveryResumed, type AppDeliveryStalled } from './app-lane.js';
|
|
9
|
-
import { type CommitJournal, type LaneResult, type PendingCommit } from './commit.js';
|
|
10
|
-
import { type GroupCrypto, type GroupMLS } from './crypto.js';
|
|
11
|
-
import { type GroupProcedureHandlers } from './handlers.js';
|
|
12
|
-
import { type ReceiveLaneEnded, type SubscribeFailure } from './hub-mux.js';
|
|
13
|
-
import { type GroupProtocolDefinition } from './protocol.js';
|
|
14
|
-
/**
|
|
15
|
-
* How long the hub is asked to keep the commit log: **28 days**. Bounds how long a member may be
|
|
16
|
-
* offline and still converge by pulling alone, without another member awake to heal it.
|
|
17
|
-
*
|
|
18
|
-
* Deliberately below the reference hub ceiling (`createMemoryStore`'s `DEFAULT_MAX_RETENTION`, 30
|
|
19
|
-
* days): a hub refuses a retention above its ceiling rather than clamping it, so a default sitting
|
|
20
|
-
* exactly on the ceiling would leave no room for an upward override — refused outright, and the
|
|
21
|
-
* peer not a subscriber of its own commit topic. Asserted in `peer-control-lanes.test.ts`, since a
|
|
22
|
-
* tighter operator cap still refuses this default (reported via `hub-mux`).
|
|
23
|
-
*/
|
|
24
|
-
export declare const DEFAULT_COMMIT_LOG_RETENTION_SECONDS: number;
|
|
25
|
-
/**
|
|
26
|
-
* How long the hub is asked to keep an app topic's log. Aligned to the commit window so a
|
|
27
|
-
* returning member never rebuilds its membership without also recovering its messages. A separate
|
|
28
|
-
* dial, not the commit one reused — the alignment is a choice a host may override.
|
|
29
|
-
*/
|
|
30
|
-
export declare const DEFAULT_APP_LOG_RETENTION_SECONDS: number;
|
|
31
|
-
/**
|
|
32
|
-
* The MLS half of a peer: the lifecycle port, the durable journal, the restart-adopt hook, and the
|
|
33
|
-
* durable anchor/cursor stores. They arrive together or not at all — each missing piece fails
|
|
34
|
-
* SILENTLY: no journal loses a commit whose process died mid-acceptance; no anchor store re-seeds
|
|
35
|
-
* at the live epoch and silently partitions from the group; no cursor store re-reads app history
|
|
36
|
-
* from the hub's retention floor every restart. The type is what stops a host wiring only some.
|
|
37
|
-
*/
|
|
38
|
-
export type GroupPeerMLSParams = {
|
|
39
|
-
/** MLS lifecycle port. When provided, the peer runs the commit lane. */
|
|
40
|
-
mls: GroupMLS;
|
|
41
|
-
/** Durable single-slot journal. Written before every publish, cleared on both outcomes. */
|
|
42
|
-
journal: CommitJournal;
|
|
43
|
-
/**
|
|
44
|
-
* Durable store for the app-lane anchor. Written on every rotation, read once at construction.
|
|
45
|
-
* Persisted, not derived — see {@link anchor} for why it cannot be re-derived from the handle.
|
|
46
|
-
*/
|
|
47
|
-
anchorStore: AnchorStore;
|
|
48
|
-
/**
|
|
49
|
-
* Durable read position for the app lane, per topic. Written as each drain finishes, read as
|
|
50
|
-
* each segment is pulled — what lets a returning peer resume from where it got to, and the only
|
|
51
|
-
* thing a below-retention gap can be detected against (the gap IS the distance between the two).
|
|
52
|
-
*
|
|
53
|
-
* The drain may only advance it past a frame it is done with: delivered, or sealed at an epoch
|
|
54
|
-
* this peer can never hold again. See {@link "app-cursor".AppCursorStore}.
|
|
55
|
-
*/
|
|
56
|
-
appCursorStore: AppCursorStore;
|
|
57
|
-
/**
|
|
58
|
-
* Adopt a journalled commit now confirmed accepted — the restart half of
|
|
59
|
-
* {@link PendingCommit.onAccepted}: deserialize the post-commit handle, adopt it, deliver any
|
|
60
|
-
* Welcome it carried.
|
|
61
|
-
*
|
|
62
|
-
* MUST be idempotent, like `onAccepted`: the peer cannot tell an entry whose `onAccepted`
|
|
63
|
-
* already ran from one whose process died before it. The Welcome resend is at-least-once by
|
|
64
|
-
* design — see {@link PendingCommit.onAccepted}.
|
|
65
|
-
*/
|
|
66
|
-
adoptJournalled: (journal: Uint8Array) => Promise<void>;
|
|
67
|
-
};
|
|
68
|
-
export type StrandKind = 'own-unmerged' | 'fork-losing' | 'ahead' | 'unknown-version';
|
|
69
|
-
/**
|
|
70
|
-
* For `own-unmerged`, `authenticated` means this device sealed a commit at this epoch that the
|
|
71
|
-
* hub now places in the log. `readCommitHeader` identifies its leaf from PrivateMessage sender
|
|
72
|
-
* data, AEAD-opened with this epoch's `sender_data_secret` and a ciphertext sample. Commit content
|
|
73
|
-
* is unverified; a hub can tamper past the sample and serve a captured, rejected frame.
|
|
74
|
-
*/
|
|
75
|
-
export type StrandConfidence = 'authenticated' | 'observed' | 'claimed';
|
|
76
|
-
/** A stranded-frame notice. `own-unmerged` does not prove commit content or hub acceptance; heal. */
|
|
77
|
-
export type StrandObservation = {
|
|
78
|
-
groupID: string;
|
|
79
|
-
position: string;
|
|
80
|
-
commitDigest: string | null;
|
|
81
|
-
localEpoch: number;
|
|
82
|
-
claimedEpoch: number | null;
|
|
83
|
-
kind: StrandKind;
|
|
84
|
-
confidence: StrandConfidence;
|
|
85
|
-
};
|
|
86
|
-
export type RecoveryTrigger = 'automatic' | 'consumer';
|
|
87
|
-
export type RecoveryFailureReason = 'no-responder' | 'bootstrap-failed' | 'deadline' | 'disposed' | 'error';
|
|
88
|
-
type RecoveryEventBase = {
|
|
89
|
-
groupID: string;
|
|
90
|
-
attemptID: string;
|
|
91
|
-
trigger: RecoveryTrigger;
|
|
92
|
-
};
|
|
93
|
-
export type RecoveryEvent = (RecoveryEventBase & {
|
|
94
|
-
phase: 'started';
|
|
95
|
-
}) | (RecoveryEventBase & {
|
|
96
|
-
phase: 'succeeded';
|
|
97
|
-
}) | (RecoveryEventBase & {
|
|
98
|
-
phase: 'failed';
|
|
99
|
-
reason: RecoveryFailureReason;
|
|
100
|
-
error?: unknown;
|
|
101
|
-
}) | (RecoveryEventBase & {
|
|
102
|
-
phase: 'bootstrapped';
|
|
103
|
-
});
|
|
104
|
-
export type GroupPeerParams<Protocols extends Record<string, GroupProtocolDefinition>> = {
|
|
105
|
-
hub: LogHub;
|
|
106
|
-
crypto: GroupCrypto;
|
|
107
|
-
localDID: string;
|
|
108
|
-
protocols: Protocols;
|
|
109
|
-
handlers: {
|
|
110
|
-
[K in keyof Protocols]: GroupProcedureHandlers<Protocols[K]>;
|
|
111
|
-
};
|
|
112
|
-
suppress?: SuppressConfig;
|
|
113
|
-
/** Runtime providing platform primitives. Defaults to `createRuntime()`. */
|
|
114
|
-
runtime?: Runtime;
|
|
115
|
-
/**
|
|
116
|
-
* Recovery rendezvous tuning. `timeoutMs`: how long one request waits for a reply. `getDelayMs`:
|
|
117
|
-
* responder reply jitter. `deadlineMs`: how long `recover()` keeps re-requesting and rebuilding
|
|
118
|
-
* before giving up and leaving the peer degraded.
|
|
119
|
-
*/
|
|
120
|
-
recovery?: {
|
|
121
|
-
timeoutMs?: number;
|
|
122
|
-
getDelayMs?: () => number;
|
|
123
|
-
deadlineMs?: number;
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Commit-log retention the hub is asked to hold, in seconds. Default 28 days — see
|
|
127
|
-
* {@link DEFAULT_COMMIT_LOG_RETENTION_SECONDS}. A liveness dial: within it a returning member
|
|
128
|
-
* converges by pulling the log; beyond it, another live member must heal it.
|
|
129
|
-
*/
|
|
130
|
-
commitLogRetentionSeconds?: number;
|
|
131
|
-
/**
|
|
132
|
-
* App-log retention the hub is asked to hold, in seconds. Default 28 days — see
|
|
133
|
-
* {@link DEFAULT_APP_LOG_RETENTION_SECONDS}. Overridable up to the hub operator's own cap.
|
|
134
|
-
*/
|
|
135
|
-
appLogRetentionSeconds?: number;
|
|
136
|
-
/**
|
|
137
|
-
* How long `commit` rebases before giving up, in ms. Default 30s. Losing a compare-and-set is
|
|
138
|
-
* the expected path, not an error path.
|
|
139
|
-
*/
|
|
140
|
-
commitDeadlineMs?: number;
|
|
141
|
-
/**
|
|
142
|
-
* Called when the app lane finds a gap below the hub's retention floor: frames published to a
|
|
143
|
-
* topic this peer had a read position on, and aged out before it came back for them.
|
|
144
|
-
*
|
|
145
|
-
* OPTIONAL: unlike the stores above, a host that ignores this loses no message — the frames
|
|
146
|
-
* that survived are still delivered — it only turns an absence the host could not see into one
|
|
147
|
-
* it can.
|
|
148
|
-
*
|
|
149
|
-
* Fire-and-forget: a throw is swallowed and the drain carries on.
|
|
150
|
-
*/
|
|
151
|
-
onAppWindowPruned?: (event: AppWindowPruned) => void | Promise<void>;
|
|
152
|
-
onStrand?: (observation: StrandObservation) => void | Promise<void>;
|
|
153
|
-
onRecovery?: (event: RecoveryEvent) => void | Promise<void>;
|
|
154
|
-
onAppDeliveryStalled?: (event: AppDeliveryStalled) => void | Promise<void>;
|
|
155
|
-
onAppDeliveryResumed?: (event: AppDeliveryResumed) => void | Promise<void>;
|
|
156
|
-
/**
|
|
157
|
-
* Called when the hub definitively refuses to subscribe this peer to a topic — most plausibly a
|
|
158
|
-
* retention setting above the operator's own cap, which a hub refuses rather than clamps.
|
|
159
|
-
*
|
|
160
|
-
* Optional only because it is not the enforcement: every publish and fetch on a refused topic
|
|
161
|
-
* throws (see {@link "hub-mux".createHubMux}), so a host that wires nothing still cannot mistake
|
|
162
|
-
* such a peer for a healthy one. This is how a host learns PROMPTLY, and the only way a
|
|
163
|
-
* read-only peer on that topic learns at all.
|
|
164
|
-
*
|
|
165
|
-
* Fire-and-forget: a throw is swallowed.
|
|
166
|
-
*/
|
|
167
|
-
onSubscribeFailed?: (failure: SubscribeFailure) => void;
|
|
168
|
-
/**
|
|
169
|
-
* The push lane has ended and nothing will restart it. See {@link "hub-mux".ReceiveLaneEnded}.
|
|
170
|
-
*
|
|
171
|
-
* The connection belongs to the HOST, not the peer, so only the host can reconnect — without
|
|
172
|
-
* this the ending is invisible, and a dead lane looks like a group with nothing to say. A host
|
|
173
|
-
* that reconnects should build a new peer over the new connection.
|
|
174
|
-
*
|
|
175
|
-
* Not called on `dispose`. Fire-and-forget: a throw is swallowed.
|
|
176
|
-
*/
|
|
177
|
-
onReceiveEnded?: (ended: ReceiveLaneEnded) => void;
|
|
178
|
-
} & (GroupPeerMLSParams | {
|
|
179
|
-
mls?: undefined;
|
|
180
|
-
journal?: undefined;
|
|
181
|
-
adoptJournalled?: undefined;
|
|
182
|
-
anchorStore?: undefined;
|
|
183
|
-
appCursorStore?: undefined;
|
|
184
|
-
});
|
|
185
|
-
type FilterNever<T> = {
|
|
186
|
-
[K in keyof T as T[K] extends never ? never : K]: T[K];
|
|
187
|
-
};
|
|
188
|
-
type GroupEventDefs<Protocol extends GroupProtocolDefinition> = FilterNever<{
|
|
189
|
-
[P in keyof Protocol & string]: Protocol[P] extends EventProcedureDefinition ? {
|
|
190
|
-
Data: DataOf<Protocol[P]['data']>;
|
|
191
|
-
} : never;
|
|
192
|
-
}>;
|
|
193
|
-
type GroupRequestDefs<Protocol extends GroupProtocolDefinition> = FilterNever<{
|
|
194
|
-
[P in keyof Protocol & string]: Protocol[P] extends RequestProcedureDefinition ? {
|
|
195
|
-
Param: DataOf<Protocol[P]['param']>;
|
|
196
|
-
Result: ReturnOf<Protocol[P]['result']>;
|
|
197
|
-
} : never;
|
|
198
|
-
}>;
|
|
199
|
-
export type ProtocolSurface<Protocol extends GroupProtocolDefinition> = ProtocolSurfaceOf<Protocol>;
|
|
200
|
-
type ProtocolSurfaceOf<Protocol extends GroupProtocolDefinition, Events extends GroupEventDefs<Protocol> = GroupEventDefs<Protocol>, Requests extends GroupRequestDefs<Protocol> = GroupRequestDefs<Protocol>> = {
|
|
201
|
-
dispatch: <P extends keyof Events & string, T extends Events[P] = Events[P]>(prc: P, ...args: T['Data'] extends never ? [config?: {
|
|
202
|
-
data?: never;
|
|
203
|
-
}] : [config: {
|
|
204
|
-
data: T['Data'];
|
|
205
|
-
}]) => Promise<void>;
|
|
206
|
-
request: <P extends keyof Requests & string, T extends Requests[P] = Requests[P]>(prc: P, ...args: T['Param'] extends never ? [config?: {
|
|
207
|
-
param?: never;
|
|
208
|
-
} & RequestOptions] : [config: {
|
|
209
|
-
param: T['Param'];
|
|
210
|
-
} & RequestOptions]) => Promise<T['Result']>;
|
|
211
|
-
gather: <P extends keyof Requests & string, T extends Requests[P] = Requests[P]>(prc: P, ...args: T['Param'] extends never ? [config?: {
|
|
212
|
-
param?: never;
|
|
213
|
-
} & GatherOptions<T['Result']>] : [config: {
|
|
214
|
-
param: T['Param'];
|
|
215
|
-
} & GatherOptions<T['Result']>]) => Promise<Array<GatheredReply<T['Result']>>>;
|
|
216
|
-
to: (memberDID: string) => Promise<Client<Protocol>>;
|
|
217
|
-
};
|
|
218
|
-
export type InternalSurface = {
|
|
219
|
-
dispatch: (prc: string, config?: {
|
|
220
|
-
data?: Record<string, unknown>;
|
|
221
|
-
}) => Promise<void>;
|
|
222
|
-
request: (prc: string, config?: {
|
|
223
|
-
param?: unknown;
|
|
224
|
-
} & RequestOptions) => Promise<unknown>;
|
|
225
|
-
gather: (prc: string, config?: {
|
|
226
|
-
param?: unknown;
|
|
227
|
-
} & GatherOptions) => Promise<Array<GatheredReply>>;
|
|
228
|
-
to: (memberDID: string) => Promise<Client<ProtocolDefinition>>;
|
|
229
|
-
};
|
|
230
|
-
export type GroupPeer<Protocols extends Record<string, GroupProtocolDefinition>> = {
|
|
231
|
-
protocol: <K extends keyof Protocols>(name: K) => ProtocolSurface<Protocols[K]>;
|
|
232
|
-
/** Accept loss of one buffered sealed app frame, then resume the journal-first walk. */
|
|
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>;
|
|
236
|
-
/**
|
|
237
|
-
* Commit to the group, rebasing until it lands.
|
|
238
|
-
*
|
|
239
|
-
* Replays the journal, pulls the log to the end, calls `build()`, journals the result, and
|
|
240
|
-
* publishes conditionally on the head it pulled to. Lose (someone committed first): drop the
|
|
241
|
-
* pending commit untouched and call `build()` again against the now-current handle — expected,
|
|
242
|
-
* not an error. `build()` must read the host's live handle each attempt and have no side effects
|
|
243
|
-
* until `onAccepted` runs, since a losing attempt is discarded whole.
|
|
244
|
-
*
|
|
245
|
-
* Holds the commit mutex for its whole run, so two `build()` calls never race one handle.
|
|
246
|
-
*
|
|
247
|
-
* A RESULT means it landed and `onAccepted` ran; a THROW means it did not — stranded, ledger
|
|
248
|
-
* incomplete ({@link "commit".RecoveryRequiredError}), or deadline lost
|
|
249
|
-
* ({@link "commit".CommitDeadlineError}). Call {@link replay} after a throw to collect any
|
|
250
|
-
* undrained `lost` / `reenact` work.
|
|
251
|
-
*/
|
|
252
|
-
commit: (build: () => Promise<PendingCommit>) => Promise<LaneResult>;
|
|
253
|
-
/**
|
|
254
|
-
* Replay the journal on its own, for startup: republish any pending commit under its original
|
|
255
|
-
* idempotency key and hand back what did not survive. The host's collector to call before
|
|
256
|
-
* anything else, and after a `commit()` that threw.
|
|
257
|
-
*
|
|
258
|
-
* Builds and publishes nothing, so an incomplete ledger is no hazard here — it retries the
|
|
259
|
-
* bootstrap and returns WITHOUT throwing, leaving the peer degraded until a responder answers.
|
|
260
|
-
* A `{}` result means "no orphaned work to re-issue", never "the peer is whole" — the
|
|
261
|
-
* completeness gate lives on `commit()`.
|
|
262
|
-
*/
|
|
263
|
-
replay: () => Promise<LaneResult>;
|
|
264
|
-
/**
|
|
265
|
-
* Heal a peer the group has left behind: rejoin by external commit, refold the ledger, hand
|
|
266
|
-
* back the entries the group's ledger does not already hold.
|
|
267
|
-
*
|
|
268
|
-
* A TOP-LEVEL lane operation, never called from inside another — takes the commit mutex itself.
|
|
269
|
-
* The external commit races at the head like any commit; losing (the likely outcome) discards
|
|
270
|
-
* the GroupInfo too, since it describes a tree the winner already changed and a commit rebuilt
|
|
271
|
-
* from it is one no member can apply.
|
|
272
|
-
*
|
|
273
|
-
* A heal is TWO commits: the rejoin carries no entries, so the entries this peer still owes ride
|
|
274
|
-
* an ordinary `commit()` the CALLER makes after this releases the lane — `reenact`, filtered by
|
|
275
|
-
* MEMBERSHIP: re-enact an entry iff the group's ledger does not already hold it (the ledger does
|
|
276
|
-
* not dedup, so re-enacting a held entry would revert a later admin's write).
|
|
277
|
-
*
|
|
278
|
-
* `{ advanced: false }` when no member answers, or the rejoin landed but the ledger could not be
|
|
279
|
-
* bootstrapped (an incomplete ledger is a reset roster; reporting it healed would hand the host a
|
|
280
|
-
* group with every role gone). A peer merely BEHIND never needs this — it pulls and catches up.
|
|
281
|
-
*/
|
|
282
|
-
recover: () => Promise<{
|
|
283
|
-
advanced: boolean;
|
|
284
|
-
reenact: Array<string>;
|
|
285
|
-
}>;
|
|
286
|
-
resync: () => Promise<void>;
|
|
287
|
-
/**
|
|
288
|
-
* The epoch the app-lane anchor sits at — see {@link anchor} for the rotation rule. Exposed so a
|
|
289
|
-
* caller can observe a roster change being detected without reaching into the port.
|
|
290
|
-
*/
|
|
291
|
-
anchorEpoch: () => number;
|
|
292
|
-
/**
|
|
293
|
-
* Re-drive any hub subscription this peer latched as refused, on the host's word that the refusal
|
|
294
|
-
* may now be answered differently. The one case that needs it: a peer whose group topics were
|
|
295
|
-
* subscribed the instant it joined — before the app-level authorization that gates them had landed
|
|
296
|
-
* — is refused `AuthorizationDeniedError`, and that refusal is latched permanent (a busy retry
|
|
297
|
-
* against an answer would be worse). Once the host knows the peer is authorized (its membership
|
|
298
|
-
* row has replicated), it calls this to ask again. Single-shot per call: a topic the hub still
|
|
299
|
-
* refuses simply re-latches, so call it on each authorization change rather than to poll.
|
|
300
|
-
*/
|
|
301
|
-
reauthorize: () => void;
|
|
302
|
-
dispose: () => Promise<void>;
|
|
303
|
-
};
|
|
304
|
-
export declare function createGroupPeer<Protocols extends Record<string, ProtocolDefinition>>(params: GroupPeerParams<Protocols>): GroupPeer<Protocols>;
|
|
305
|
-
export {};
|
|
1
|
+
import type { ProtocolDefinition } from '@enkaku/protocol';
|
|
2
|
+
import { type GroupPeer, type GroupPeerParams } from './peer-types.js';
|
|
3
|
+
export { DEFAULT_APP_LOG_RETENTION_SECONDS, DEFAULT_COMMIT_LOG_RETENTION_SECONDS, type GroupPeer, type GroupPeerMLSParams, type GroupPeerParams, type InternalSurface, type ProtocolSurface, type RecoveryEvent, type RecoveryFailureDetails, type RecoveryFailureReason, type RecoveryTrigger, type StrandConfidence, type StrandKind, type StrandObservation, } from './peer-types.js';
|
|
4
|
+
export declare function createGroupPeer<Protocols extends Record<string, ProtocolDefinition>>(input: GroupPeerParams<Protocols>): GroupPeer<Protocols>;
|