@serfab/cadre-core 0.10.0 → 0.11.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.
Files changed (105) hide show
  1. package/README.md +255 -255
  2. package/dist/cadre-node.d.ts +203 -28
  3. package/dist/cadre-node.js +445 -84
  4. package/dist/cadre-node.js.map +1 -1
  5. package/dist/control-cohort.d.ts +26 -0
  6. package/dist/control-cohort.js +26 -0
  7. package/dist/control-cohort.js.map +1 -1
  8. package/dist/control-database.d.ts +82 -117
  9. package/dist/control-database.js +217 -295
  10. package/dist/control-database.js.map +1 -1
  11. package/dist/control-formation-recorder.d.ts +8 -7
  12. package/dist/control-formation-recorder.js +8 -7
  13. package/dist/control-formation-recorder.js.map +1 -1
  14. package/dist/control-schema.d.ts +1 -1
  15. package/dist/control-schema.js +789 -774
  16. package/dist/control-schema.js.map +1 -1
  17. package/dist/control-write-retry.d.ts +15 -5
  18. package/dist/control-write-retry.js +19 -18
  19. package/dist/control-write-retry.js.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.js +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/peer-addr-book.d.ts +92 -0
  24. package/dist/peer-addr-book.js +220 -0
  25. package/dist/peer-addr-book.js.map +1 -0
  26. package/dist/seed-bootstrap.d.ts +7 -0
  27. package/dist/seed-bootstrap.js +15 -13
  28. package/dist/seed-bootstrap.js.map +1 -1
  29. package/dist/strand-cohort.d.ts +4 -37
  30. package/dist/strand-cohort.js +4 -15
  31. package/dist/strand-cohort.js.map +1 -1
  32. package/dist/strand-database.d.ts +11 -16
  33. package/dist/strand-database.js +19 -7
  34. package/dist/strand-database.js.map +1 -1
  35. package/dist/strand-formation-manager.d.ts +8 -8
  36. package/dist/strand-formation-manager.js +15 -18
  37. package/dist/strand-formation-manager.js.map +1 -1
  38. package/dist/strand-instance-manager.d.ts +52 -21
  39. package/dist/strand-instance-manager.js +100 -34
  40. package/dist/strand-instance-manager.js.map +1 -1
  41. package/dist/strand-membership-writer.d.ts +6 -4
  42. package/dist/strand-membership-writer.js +38 -36
  43. package/dist/strand-membership-writer.js.map +1 -1
  44. package/dist/types.d.ts +39 -31
  45. package/dist/types.js.map +1 -1
  46. package/package.json +8 -7
  47. package/src/arachnode-stub.ts +159 -159
  48. package/src/bootstrap-peer-store-file.ts +44 -44
  49. package/src/bootstrap-peer-store.ts +172 -172
  50. package/src/cadre-node.ts +5158 -4771
  51. package/src/canonical-json.ts +30 -30
  52. package/src/control-authorization.ts +114 -114
  53. package/src/control-cohort.ts +130 -103
  54. package/src/control-database.ts +2406 -2506
  55. package/src/control-formation-recorder.ts +305 -304
  56. package/src/control-schema.ts +801 -786
  57. package/src/control-stream.ts +205 -205
  58. package/src/control-write-retry.ts +483 -473
  59. package/src/delegate-admission.ts +302 -302
  60. package/src/device-token.ts +94 -94
  61. package/src/diagnostics/connection-path.ts +277 -277
  62. package/src/diagnostics/webrtc-turn-tracker.ts +222 -222
  63. package/src/ed25519-key.ts +134 -134
  64. package/src/enrollment.ts +199 -199
  65. package/src/file-durable-slot.ts +61 -61
  66. package/src/formation-approval.ts +604 -604
  67. package/src/fs-atomic.ts +107 -107
  68. package/src/hibernation-manager.ts +437 -437
  69. package/src/identity-key.ts +109 -109
  70. package/src/index.ts +1 -0
  71. package/src/key-store-file.ts +132 -132
  72. package/src/key-store.ts +99 -99
  73. package/src/membership-connection-gater.ts +204 -204
  74. package/src/node-local-snapshot.ts +260 -260
  75. package/src/peer-addr-book.ts +250 -0
  76. package/src/peer-authorization.ts +259 -259
  77. package/src/peer-record.ts +164 -164
  78. package/src/push-credentials.ts +84 -84
  79. package/src/push-node.ts +64 -64
  80. package/src/push-notifier-apns.ts +247 -247
  81. package/src/push-notifier-fcm.ts +220 -220
  82. package/src/push-notifier-shared.ts +28 -28
  83. package/src/push-notifier.ts +52 -52
  84. package/src/relay-addrs.ts +111 -111
  85. package/src/relay-reservation.ts +823 -823
  86. package/src/seed-bootstrap.ts +15 -15
  87. package/src/seed-trust-policy.ts +140 -140
  88. package/src/strand-addr-protocol.ts +441 -441
  89. package/src/strand-backfill.ts +366 -366
  90. package/src/strand-cohort.ts +38 -77
  91. package/src/strand-database.ts +202 -204
  92. package/src/strand-formation-manager.ts +491 -494
  93. package/src/strand-formation-protocol.ts +692 -692
  94. package/src/strand-instance-manager.ts +108 -50
  95. package/src/strand-member-key.ts +39 -39
  96. package/src/strand-member-registry.ts +182 -182
  97. package/src/strand-membership-writer.ts +1432 -1430
  98. package/src/strand-solicitation.ts +472 -472
  99. package/src/strand-transport-key.ts +58 -58
  100. package/src/strand-wake-payload.ts +32 -32
  101. package/src/strand-wake-protocol.ts +314 -314
  102. package/src/strand-watcher.ts +314 -314
  103. package/src/trusted-owner-store-file.ts +41 -41
  104. package/src/trusted-owner-store.ts +171 -171
  105. package/src/types.ts +1309 -1303
@@ -1,314 +1,314 @@
1
- /**
2
- * Control-network push-wake transport.
3
- *
4
- * Lets a same-cadre peer — typically an always-on server that participates in a
5
- * strand and sees new activity — signal a hibernating cadre peer to bring that
6
- * strand online, pull pending activity, and re-hibernate. Push-wake travels the
7
- * **control network** (the per-party network connecting this party's own cadre
8
- * nodes), which is the only network a hibernating peer keeps connected.
9
- *
10
- * Modeled directly on `seed-bootstrap.ts`: a dedicated libp2p protocol id,
11
- * 4-byte big-endian length-prefixed JSON frames, `node.handle` for the receiver,
12
- * `node.dialProtocol` for the sender, and the shared `ControlStream` primitives
13
- * from `control-stream.ts`. The exchange is a single request → single ack on one
14
- * stream (like seed delivery), so each side reads to EOF (under a read timeout)
15
- * and decodes one frame via the shared {@link decodeLengthPrefixedFrame} guard.
16
- *
17
- * **Authorization (v1):** a wake is low-risk — it only causes the receiver to
18
- * spend resources coming online for a strand it already participates in — so the
19
- * receiver carries no per-request signature and instead defers entirely to the
20
- * injected `isMember` predicate. `CadreNode` injects its AUTHORIZED-membership
21
- * predicate there (`isAuthorizedMember`: the sender's `CadrePeer` row must carry
22
- * a voucher that verifies against an owner key in the receiver's node-local
23
- * trusted-owner anchor), so a peer that merely published rows into the replicated
24
- * control DB is refused. This module stays agnostic: it enforces whatever
25
- * predicate it is given.
26
- */
27
-
28
- import debug from 'debug';
29
- import type { Libp2p, Connection } from '@libp2p/interface';
30
- import type { Multiaddr } from '@multiformats/multiaddr';
31
- import type { StrandInstance, WakeRequest, WakeAck } from './types.js';
32
- import { decodeLengthPrefixedFrame } from './seed-bootstrap.js';
33
- import { type ControlStream, writeFrame, withDeadline, exchangeFrame, readStreamToEnd } from './control-stream.js';
34
-
35
- const log = debug('sereus:cadre:strand-wake');
36
-
37
- /** Protocol id for control-network push-wake (parallel to `/sereus/seed/1.0.0`). */
38
- export const WAKE_PROTOCOL = '/sereus/strand-wake/1.0.0';
39
-
40
- /**
41
- * Maximum wake frame size. Wake messages are tiny (a strand id + short reason),
42
- * so this is a defensive cap — far below the 1MB seed ceiling — that bounds the
43
- * bytes a peer can make the receiver buffer per stream.
44
- */
45
- const MAX_WAKE_SIZE = 64 * 1024;
46
-
47
- /** Default time to wait for the ack before abandoning a wake dial (ms). */
48
- const DEFAULT_WAKE_TIMEOUT_MS = 10_000;
49
-
50
- /** Default time the receiver waits for an inbound wake frame before aborting (ms). */
51
- const DEFAULT_WAKE_READ_TIMEOUT_MS = 10_000;
52
-
53
- /** Default cap on concurrent inbound wake streams a single peer can pin open. */
54
- const DEFAULT_MAX_CONCURRENT_WAKES = 100;
55
-
56
- /**
57
- * Read a libp2p stream to EOF and decode the single length-prefixed JSON frame
58
- * it carries. Bounded by `timeoutMs` (a never-half-closing peer is aborted, not
59
- * awaited forever) and capped at {@link MAX_WAKE_SIZE}, reusing the shared
60
- * {@link decodeLengthPrefixedFrame} guard for the prefix/length checks.
61
- */
62
- async function readFrame<T>(stream: ControlStream, timeoutMs: number): Promise<T> {
63
- const data = await readStreamToEnd(stream, { maxBytes: MAX_WAKE_SIZE, timeoutMs, label: 'Wake' });
64
- const body = decodeLengthPrefixedFrame(data, MAX_WAKE_SIZE);
65
- return JSON.parse(new TextDecoder().decode(body)) as T;
66
- }
67
-
68
- /**
69
- * Dependencies the {@link StrandWakeService} receiver needs from its host
70
- * (`CadreNode`), injected so the service is testable without a full node.
71
- */
72
- export interface StrandWakeServiceOptions {
73
- /** Membership gate: is the remote peer a `CadrePeer` member of this cadre? */
74
- isMember(remotePeerId: string): Promise<boolean>;
75
- /** Look up a local strand instance by id (undefined if not participated in). */
76
- getStrand(strandId: string): StrandInstance | undefined;
77
- /**
78
- * Trigger the local wake path for a hibernating/idle strand. Wired to
79
- * `CadreNode.wakeStrand` (→ `HibernationManager` → `resumeStrand`), whose
80
- * resume coalescing prevents a push-wake racing a concurrent check-in.
81
- */
82
- wake(strandId: string): Promise<void>;
83
- /**
84
- * Time to wait for the inbound wake frame before aborting the read (ms).
85
- * Defaults to {@link DEFAULT_WAKE_READ_TIMEOUT_MS}. Bounds a buggy/compromised
86
- * own-cadre node that opens a stream and never half-closes its write end.
87
- */
88
- readTimeoutMs?: number;
89
- /**
90
- * Cap on concurrent inbound wake streams (defaults to
91
- * {@link DEFAULT_MAX_CONCURRENT_WAKES}). Over the cap, a non-accepting ack is
92
- * returned without invoking the wake path.
93
- */
94
- maxConcurrent?: number;
95
- }
96
-
97
- /**
98
- * Receiver side of the push-wake protocol. Registers a `WAKE_PROTOCOL` handler
99
- * on the control node and, for each inbound {@link WakeRequest}, gates on cadre
100
- * membership, then resumes the named strand if it is hibernating/idle and we
101
- * participate in it — replying with a {@link WakeAck}.
102
- */
103
- export class StrandWakeService {
104
- private readonly options: StrandWakeServiceOptions;
105
- private readonly readTimeoutMs: number;
106
- private readonly maxConcurrent: number;
107
- private node: Libp2p | null = null;
108
- /** In-flight inbound wake streams, used to enforce {@link maxConcurrent}. */
109
- private activeStreams = 0;
110
-
111
- constructor(options: StrandWakeServiceOptions) {
112
- this.options = options;
113
- this.readTimeoutMs = options.readTimeoutMs ?? DEFAULT_WAKE_READ_TIMEOUT_MS;
114
- this.maxConcurrent = options.maxConcurrent ?? DEFAULT_MAX_CONCURRENT_WAKES;
115
- }
116
-
117
- /** Number of in-flight inbound wake streams. */
118
- get activeCount(): number {
119
- return this.activeStreams;
120
- }
121
-
122
- /** Register the wake protocol handler on the control node. */
123
- initialize(node: Libp2p): void {
124
- this.node = node;
125
- // `runOnLimitedConnection: true` is REQUIRED for the relay path: a NAT'd
126
- // receiver is reached over a circuit-relay connection, which libp2p marks
127
- // "limited" (the relay caps its data/duration). Without this the receiver
128
- // would refuse the inbound wake stream on exactly the connection the
129
- // protocol is designed to use (see the relay note on `dialWake`).
130
- void node.handle(WAKE_PROTOCOL, async (rawStream: unknown, rawConnection: unknown) => {
131
- const remotePeerId = (rawConnection as Connection).remotePeer.toString();
132
- await this.handleStream(rawStream as ControlStream, remotePeerId);
133
- }, { runOnLimitedConnection: true });
134
- log('StrandWakeService registered handler: %s', WAKE_PROTOCOL);
135
- }
136
-
137
- /** Unregister the handler and release the node reference. */
138
- async shutdown(): Promise<void> {
139
- if (this.node) {
140
- await this.node.unhandle(WAKE_PROTOCOL);
141
- this.node = null;
142
- log('StrandWakeService shutdown');
143
- }
144
- }
145
-
146
- /**
147
- * Read the inbound request, decide + execute the wake, and write the ack.
148
- *
149
- * Three hardening layers, all reported as a non-accepting ack rather than a
150
- * dropped/hung stream: a concurrency cap (over {@link maxConcurrent}, reply
151
- * without touching the wake path), a read timeout (a peer that never
152
- * half-closes is aborted inside {@link readFrame}/`readStreamToEnd`), and the
153
- * existing malformed/oversized-frame guard.
154
- */
155
- private async handleStream(stream: ControlStream, remotePeerId: string): Promise<void> {
156
- log('Incoming wake request from: %s', remotePeerId);
157
-
158
- if (this.activeStreams >= this.maxConcurrent) {
159
- log('Rejecting wake from %s: %d concurrent streams at cap %d', remotePeerId, this.activeStreams, this.maxConcurrent);
160
- const ack: WakeAck = { accepted: false, reason: 'Too many concurrent wake requests' };
161
- try {
162
- writeFrame(stream, ack);
163
- } catch {
164
- // Ignore send errors on the reject path.
165
- }
166
- try {
167
- await stream.close();
168
- } catch {
169
- // Ignore close errors.
170
- }
171
- return;
172
- }
173
-
174
- this.activeStreams++;
175
- try {
176
- const request = await readFrame<WakeRequest>(stream, this.readTimeoutMs);
177
- const ack = await this.processWakeRequest(request, remotePeerId);
178
- writeFrame(stream, ack);
179
- } catch (err) {
180
- log('Error handling wake request from %s: %o', remotePeerId, err);
181
- const ack: WakeAck = {
182
- accepted: false,
183
- reason: err instanceof Error ? err.message : 'Unknown error',
184
- };
185
- try {
186
- writeFrame(stream, ack);
187
- } catch {
188
- // Ignore send errors on the error path.
189
- }
190
- } finally {
191
- this.activeStreams--;
192
- try {
193
- await stream.close();
194
- } catch {
195
- // Ignore close errors.
196
- }
197
- }
198
- }
199
-
200
- /**
201
- * Decide and execute the wake for a decoded request. Exposed (not private) so
202
- * the decision matrix can be unit-tested directly.
203
- *
204
- * - Non-member sender → rejected (`accepted: false`).
205
- * - Unknown / not-participated strand → rejected.
206
- * - Hibernating or idle strand → resumed via the wake path, then `accepted`.
207
- * - Already-live strand → no-op, `accepted` with current status.
208
- */
209
- async processWakeRequest(request: WakeRequest, remotePeerId: string): Promise<WakeAck> {
210
- // The injected membership predicate is the whole v1 authorization (CadreNode
211
- // supplies the voucher-anchored one); only a peer it admits may ask us to wake.
212
- if (!(await this.options.isMember(remotePeerId))) {
213
- log('Rejecting wake from non-member %s', remotePeerId);
214
- return { accepted: false, reason: 'Sender is not a cadre member' };
215
- }
216
-
217
- const instance = this.options.getStrand(request.strandId);
218
- if (!instance) {
219
- log('Rejecting wake for unknown/unparticipated strand %s', request.strandId);
220
- return { accepted: false, reason: 'Strand not found or not participated in' };
221
- }
222
-
223
- if (instance.status === 'hibernating' || instance.status === 'idle') {
224
- log('Waking strand %s on push from %s (reason=%s)', request.strandId, remotePeerId, request.reason ?? 'unspecified');
225
- await this.options.wake(request.strandId);
226
- } else {
227
- log('Strand %s already %s; push-wake is a no-op', request.strandId, instance.status);
228
- }
229
-
230
- // The wake path mutates the shared instance, so re-read its current status.
231
- return { accepted: true, status: instance.status };
232
- }
233
- }
234
-
235
- /** Options for {@link dialWake}. */
236
- export interface DialWakeOptions {
237
- /** Per-dial timeout in ms (default {@link DEFAULT_WAKE_TIMEOUT_MS}). */
238
- timeoutMs?: number;
239
- /** Override the protocol id (defaults to {@link WAKE_PROTOCOL}). */
240
- protocolId?: string;
241
- }
242
-
243
- /**
244
- * Sender side: dial a target's control-network address(es), send a
245
- * {@link WakeRequest} over `WAKE_PROTOCOL`, and return the peer's {@link WakeAck}.
246
- *
247
- * Tries each candidate address in order (signaling/relay first, as produced by
248
- * `CadreNode.resolvePeerAddrs`) until one dials, so a NAT'd peer is reachable via
249
- * its circuit-relay address. Throws if no address is dialable.
250
- */
251
- export async function dialWake(
252
- node: Libp2p,
253
- addrs: Multiaddr[],
254
- request: WakeRequest,
255
- options: DialWakeOptions = {}
256
- ): Promise<WakeAck> {
257
- if (addrs.length === 0) {
258
- throw new Error('No dialable address for wake target');
259
- }
260
- const protocolId = options.protocolId ?? WAKE_PROTOCOL;
261
- const timeoutMs = options.timeoutMs ?? DEFAULT_WAKE_TIMEOUT_MS;
262
-
263
- let lastError: Error | null = null;
264
- for (const addr of addrs) {
265
- try {
266
- // One deadline per attempt: its signal aborts the in-flight dialProtocol and
267
- // resets the live stream, so neither the connect nor the ack-read leaks.
268
- return await withDeadline(
269
- timeoutMs,
270
- `Wake dial ${addr.toString()}`,
271
- (signal) => sendWake(node, addr, protocolId, request, timeoutMs, signal),
272
- );
273
- } catch (err) {
274
- lastError = err instanceof Error ? err : new Error(String(err));
275
- log('Wake dial to %s failed: %o', addr.toString(), err);
276
- }
277
- }
278
- throw lastError ?? new Error('Wake dial failed');
279
- }
280
-
281
- /**
282
- * Open one stream, send the request, half-close, and read the ack.
283
- *
284
- * `signal` is the per-attempt deadline from {@link dialWake}: it goes to
285
- * `dialProtocol` so a timeout during connect aborts the dial, and into
286
- * {@link exchangeFrame} so a timeout after the stream is open resets it —
287
- * releasing the otherwise unbounded ack-read.
288
- */
289
- async function sendWake(
290
- node: Libp2p,
291
- addr: Multiaddr,
292
- protocolId: string,
293
- request: WakeRequest,
294
- timeoutMs: number,
295
- signal: AbortSignal,
296
- ): Promise<WakeAck> {
297
- // `runOnLimitedConnection: true`: the target may be reachable only over a
298
- // circuit-relay connection (the signaling-first addr), which libp2p marks
299
- // "limited". The wake exchange is a single tiny request→ack well within the
300
- // relay's data/duration cap, so opening it on the limited connection is safe
301
- // and is the whole point of dialing the relay address.
302
- const rawStream = await node.dialProtocol(addr, protocolId, { runOnLimitedConnection: true, signal });
303
-
304
- const ack = await exchangeFrame(
305
- rawStream as unknown as ControlStream,
306
- signal,
307
- request,
308
- (stream) => readFrame<WakeAck>(stream, timeoutMs),
309
- 'Wake dial aborted by timeout',
310
- );
311
-
312
- log('Wake ack from %s: accepted=%s status=%s', addr.toString(), ack.accepted, ack.status ?? '-');
313
- return ack;
314
- }
1
+ /**
2
+ * Control-network push-wake transport.
3
+ *
4
+ * Lets a same-cadre peer — typically an always-on server that participates in a
5
+ * strand and sees new activity — signal a hibernating cadre peer to bring that
6
+ * strand online, pull pending activity, and re-hibernate. Push-wake travels the
7
+ * **control network** (the per-party network connecting this party's own cadre
8
+ * nodes), which is the only network a hibernating peer keeps connected.
9
+ *
10
+ * Modeled directly on `seed-bootstrap.ts`: a dedicated libp2p protocol id,
11
+ * 4-byte big-endian length-prefixed JSON frames, `node.handle` for the receiver,
12
+ * `node.dialProtocol` for the sender, and the shared `ControlStream` primitives
13
+ * from `control-stream.ts`. The exchange is a single request → single ack on one
14
+ * stream (like seed delivery), so each side reads to EOF (under a read timeout)
15
+ * and decodes one frame via the shared {@link decodeLengthPrefixedFrame} guard.
16
+ *
17
+ * **Authorization (v1):** a wake is low-risk — it only causes the receiver to
18
+ * spend resources coming online for a strand it already participates in — so the
19
+ * receiver carries no per-request signature and instead defers entirely to the
20
+ * injected `isMember` predicate. `CadreNode` injects its AUTHORIZED-membership
21
+ * predicate there (`isAuthorizedMember`: the sender's `CadrePeer` row must carry
22
+ * a voucher that verifies against an owner key in the receiver's node-local
23
+ * trusted-owner anchor), so a peer that merely published rows into the replicated
24
+ * control DB is refused. This module stays agnostic: it enforces whatever
25
+ * predicate it is given.
26
+ */
27
+
28
+ import debug from 'debug';
29
+ import type { Libp2p, Connection } from '@libp2p/interface';
30
+ import type { Multiaddr } from '@multiformats/multiaddr';
31
+ import type { StrandInstance, WakeRequest, WakeAck } from './types.js';
32
+ import { decodeLengthPrefixedFrame } from './seed-bootstrap.js';
33
+ import { type ControlStream, writeFrame, withDeadline, exchangeFrame, readStreamToEnd } from './control-stream.js';
34
+
35
+ const log = debug('sereus:cadre:strand-wake');
36
+
37
+ /** Protocol id for control-network push-wake (parallel to `/sereus/seed/1.0.0`). */
38
+ export const WAKE_PROTOCOL = '/sereus/strand-wake/1.0.0';
39
+
40
+ /**
41
+ * Maximum wake frame size. Wake messages are tiny (a strand id + short reason),
42
+ * so this is a defensive cap — far below the 1MB seed ceiling — that bounds the
43
+ * bytes a peer can make the receiver buffer per stream.
44
+ */
45
+ const MAX_WAKE_SIZE = 64 * 1024;
46
+
47
+ /** Default time to wait for the ack before abandoning a wake dial (ms). */
48
+ const DEFAULT_WAKE_TIMEOUT_MS = 10_000;
49
+
50
+ /** Default time the receiver waits for an inbound wake frame before aborting (ms). */
51
+ const DEFAULT_WAKE_READ_TIMEOUT_MS = 10_000;
52
+
53
+ /** Default cap on concurrent inbound wake streams a single peer can pin open. */
54
+ const DEFAULT_MAX_CONCURRENT_WAKES = 100;
55
+
56
+ /**
57
+ * Read a libp2p stream to EOF and decode the single length-prefixed JSON frame
58
+ * it carries. Bounded by `timeoutMs` (a never-half-closing peer is aborted, not
59
+ * awaited forever) and capped at {@link MAX_WAKE_SIZE}, reusing the shared
60
+ * {@link decodeLengthPrefixedFrame} guard for the prefix/length checks.
61
+ */
62
+ async function readFrame<T>(stream: ControlStream, timeoutMs: number): Promise<T> {
63
+ const data = await readStreamToEnd(stream, { maxBytes: MAX_WAKE_SIZE, timeoutMs, label: 'Wake' });
64
+ const body = decodeLengthPrefixedFrame(data, MAX_WAKE_SIZE);
65
+ return JSON.parse(new TextDecoder().decode(body)) as T;
66
+ }
67
+
68
+ /**
69
+ * Dependencies the {@link StrandWakeService} receiver needs from its host
70
+ * (`CadreNode`), injected so the service is testable without a full node.
71
+ */
72
+ export interface StrandWakeServiceOptions {
73
+ /** Membership gate: is the remote peer a `CadrePeer` member of this cadre? */
74
+ isMember(remotePeerId: string): Promise<boolean>;
75
+ /** Look up a local strand instance by id (undefined if not participated in). */
76
+ getStrand(strandId: string): StrandInstance | undefined;
77
+ /**
78
+ * Trigger the local wake path for a hibernating/idle strand. Wired to
79
+ * `CadreNode.wakeStrand` (→ `HibernationManager` → `resumeStrand`), whose
80
+ * resume coalescing prevents a push-wake racing a concurrent check-in.
81
+ */
82
+ wake(strandId: string): Promise<void>;
83
+ /**
84
+ * Time to wait for the inbound wake frame before aborting the read (ms).
85
+ * Defaults to {@link DEFAULT_WAKE_READ_TIMEOUT_MS}. Bounds a buggy/compromised
86
+ * own-cadre node that opens a stream and never half-closes its write end.
87
+ */
88
+ readTimeoutMs?: number;
89
+ /**
90
+ * Cap on concurrent inbound wake streams (defaults to
91
+ * {@link DEFAULT_MAX_CONCURRENT_WAKES}). Over the cap, a non-accepting ack is
92
+ * returned without invoking the wake path.
93
+ */
94
+ maxConcurrent?: number;
95
+ }
96
+
97
+ /**
98
+ * Receiver side of the push-wake protocol. Registers a `WAKE_PROTOCOL` handler
99
+ * on the control node and, for each inbound {@link WakeRequest}, gates on cadre
100
+ * membership, then resumes the named strand if it is hibernating/idle and we
101
+ * participate in it — replying with a {@link WakeAck}.
102
+ */
103
+ export class StrandWakeService {
104
+ private readonly options: StrandWakeServiceOptions;
105
+ private readonly readTimeoutMs: number;
106
+ private readonly maxConcurrent: number;
107
+ private node: Libp2p | null = null;
108
+ /** In-flight inbound wake streams, used to enforce {@link maxConcurrent}. */
109
+ private activeStreams = 0;
110
+
111
+ constructor(options: StrandWakeServiceOptions) {
112
+ this.options = options;
113
+ this.readTimeoutMs = options.readTimeoutMs ?? DEFAULT_WAKE_READ_TIMEOUT_MS;
114
+ this.maxConcurrent = options.maxConcurrent ?? DEFAULT_MAX_CONCURRENT_WAKES;
115
+ }
116
+
117
+ /** Number of in-flight inbound wake streams. */
118
+ get activeCount(): number {
119
+ return this.activeStreams;
120
+ }
121
+
122
+ /** Register the wake protocol handler on the control node. */
123
+ initialize(node: Libp2p): void {
124
+ this.node = node;
125
+ // `runOnLimitedConnection: true` is REQUIRED for the relay path: a NAT'd
126
+ // receiver is reached over a circuit-relay connection, which libp2p marks
127
+ // "limited" (the relay caps its data/duration). Without this the receiver
128
+ // would refuse the inbound wake stream on exactly the connection the
129
+ // protocol is designed to use (see the relay note on `dialWake`).
130
+ void node.handle(WAKE_PROTOCOL, async (rawStream: unknown, rawConnection: unknown) => {
131
+ const remotePeerId = (rawConnection as Connection).remotePeer.toString();
132
+ await this.handleStream(rawStream as ControlStream, remotePeerId);
133
+ }, { runOnLimitedConnection: true });
134
+ log('StrandWakeService registered handler: %s', WAKE_PROTOCOL);
135
+ }
136
+
137
+ /** Unregister the handler and release the node reference. */
138
+ async shutdown(): Promise<void> {
139
+ if (this.node) {
140
+ await this.node.unhandle(WAKE_PROTOCOL);
141
+ this.node = null;
142
+ log('StrandWakeService shutdown');
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Read the inbound request, decide + execute the wake, and write the ack.
148
+ *
149
+ * Three hardening layers, all reported as a non-accepting ack rather than a
150
+ * dropped/hung stream: a concurrency cap (over {@link maxConcurrent}, reply
151
+ * without touching the wake path), a read timeout (a peer that never
152
+ * half-closes is aborted inside {@link readFrame}/`readStreamToEnd`), and the
153
+ * existing malformed/oversized-frame guard.
154
+ */
155
+ private async handleStream(stream: ControlStream, remotePeerId: string): Promise<void> {
156
+ log('Incoming wake request from: %s', remotePeerId);
157
+
158
+ if (this.activeStreams >= this.maxConcurrent) {
159
+ log('Rejecting wake from %s: %d concurrent streams at cap %d', remotePeerId, this.activeStreams, this.maxConcurrent);
160
+ const ack: WakeAck = { accepted: false, reason: 'Too many concurrent wake requests' };
161
+ try {
162
+ writeFrame(stream, ack);
163
+ } catch {
164
+ // Ignore send errors on the reject path.
165
+ }
166
+ try {
167
+ await stream.close();
168
+ } catch {
169
+ // Ignore close errors.
170
+ }
171
+ return;
172
+ }
173
+
174
+ this.activeStreams++;
175
+ try {
176
+ const request = await readFrame<WakeRequest>(stream, this.readTimeoutMs);
177
+ const ack = await this.processWakeRequest(request, remotePeerId);
178
+ writeFrame(stream, ack);
179
+ } catch (err) {
180
+ log('Error handling wake request from %s: %o', remotePeerId, err);
181
+ const ack: WakeAck = {
182
+ accepted: false,
183
+ reason: err instanceof Error ? err.message : 'Unknown error',
184
+ };
185
+ try {
186
+ writeFrame(stream, ack);
187
+ } catch {
188
+ // Ignore send errors on the error path.
189
+ }
190
+ } finally {
191
+ this.activeStreams--;
192
+ try {
193
+ await stream.close();
194
+ } catch {
195
+ // Ignore close errors.
196
+ }
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Decide and execute the wake for a decoded request. Exposed (not private) so
202
+ * the decision matrix can be unit-tested directly.
203
+ *
204
+ * - Non-member sender → rejected (`accepted: false`).
205
+ * - Unknown / not-participated strand → rejected.
206
+ * - Hibernating or idle strand → resumed via the wake path, then `accepted`.
207
+ * - Already-live strand → no-op, `accepted` with current status.
208
+ */
209
+ async processWakeRequest(request: WakeRequest, remotePeerId: string): Promise<WakeAck> {
210
+ // The injected membership predicate is the whole v1 authorization (CadreNode
211
+ // supplies the voucher-anchored one); only a peer it admits may ask us to wake.
212
+ if (!(await this.options.isMember(remotePeerId))) {
213
+ log('Rejecting wake from non-member %s', remotePeerId);
214
+ return { accepted: false, reason: 'Sender is not a cadre member' };
215
+ }
216
+
217
+ const instance = this.options.getStrand(request.strandId);
218
+ if (!instance) {
219
+ log('Rejecting wake for unknown/unparticipated strand %s', request.strandId);
220
+ return { accepted: false, reason: 'Strand not found or not participated in' };
221
+ }
222
+
223
+ if (instance.status === 'hibernating' || instance.status === 'idle') {
224
+ log('Waking strand %s on push from %s (reason=%s)', request.strandId, remotePeerId, request.reason ?? 'unspecified');
225
+ await this.options.wake(request.strandId);
226
+ } else {
227
+ log('Strand %s already %s; push-wake is a no-op', request.strandId, instance.status);
228
+ }
229
+
230
+ // The wake path mutates the shared instance, so re-read its current status.
231
+ return { accepted: true, status: instance.status };
232
+ }
233
+ }
234
+
235
+ /** Options for {@link dialWake}. */
236
+ export interface DialWakeOptions {
237
+ /** Per-dial timeout in ms (default {@link DEFAULT_WAKE_TIMEOUT_MS}). */
238
+ timeoutMs?: number;
239
+ /** Override the protocol id (defaults to {@link WAKE_PROTOCOL}). */
240
+ protocolId?: string;
241
+ }
242
+
243
+ /**
244
+ * Sender side: dial a target's control-network address(es), send a
245
+ * {@link WakeRequest} over `WAKE_PROTOCOL`, and return the peer's {@link WakeAck}.
246
+ *
247
+ * Tries each candidate address in order (signaling/relay first, as produced by
248
+ * `CadreNode.resolvePeerAddrs`) until one dials, so a NAT'd peer is reachable via
249
+ * its circuit-relay address. Throws if no address is dialable.
250
+ */
251
+ export async function dialWake(
252
+ node: Libp2p,
253
+ addrs: Multiaddr[],
254
+ request: WakeRequest,
255
+ options: DialWakeOptions = {}
256
+ ): Promise<WakeAck> {
257
+ if (addrs.length === 0) {
258
+ throw new Error('No dialable address for wake target');
259
+ }
260
+ const protocolId = options.protocolId ?? WAKE_PROTOCOL;
261
+ const timeoutMs = options.timeoutMs ?? DEFAULT_WAKE_TIMEOUT_MS;
262
+
263
+ let lastError: Error | null = null;
264
+ for (const addr of addrs) {
265
+ try {
266
+ // One deadline per attempt: its signal aborts the in-flight dialProtocol and
267
+ // resets the live stream, so neither the connect nor the ack-read leaks.
268
+ return await withDeadline(
269
+ timeoutMs,
270
+ `Wake dial ${addr.toString()}`,
271
+ (signal) => sendWake(node, addr, protocolId, request, timeoutMs, signal),
272
+ );
273
+ } catch (err) {
274
+ lastError = err instanceof Error ? err : new Error(String(err));
275
+ log('Wake dial to %s failed: %o', addr.toString(), err);
276
+ }
277
+ }
278
+ throw lastError ?? new Error('Wake dial failed');
279
+ }
280
+
281
+ /**
282
+ * Open one stream, send the request, half-close, and read the ack.
283
+ *
284
+ * `signal` is the per-attempt deadline from {@link dialWake}: it goes to
285
+ * `dialProtocol` so a timeout during connect aborts the dial, and into
286
+ * {@link exchangeFrame} so a timeout after the stream is open resets it —
287
+ * releasing the otherwise unbounded ack-read.
288
+ */
289
+ async function sendWake(
290
+ node: Libp2p,
291
+ addr: Multiaddr,
292
+ protocolId: string,
293
+ request: WakeRequest,
294
+ timeoutMs: number,
295
+ signal: AbortSignal,
296
+ ): Promise<WakeAck> {
297
+ // `runOnLimitedConnection: true`: the target may be reachable only over a
298
+ // circuit-relay connection (the signaling-first addr), which libp2p marks
299
+ // "limited". The wake exchange is a single tiny request→ack well within the
300
+ // relay's data/duration cap, so opening it on the limited connection is safe
301
+ // and is the whole point of dialing the relay address.
302
+ const rawStream = await node.dialProtocol(addr, protocolId, { runOnLimitedConnection: true, signal });
303
+
304
+ const ack = await exchangeFrame(
305
+ rawStream as unknown as ControlStream,
306
+ signal,
307
+ request,
308
+ (stream) => readFrame<WakeAck>(stream, timeoutMs),
309
+ 'Wake dial aborted by timeout',
310
+ );
311
+
312
+ log('Wake ack from %s: accepted=%s status=%s', addr.toString(), ack.accepted, ack.status ?? '-');
313
+ return ack;
314
+ }