@serfab/cadre-core 0.12.0 → 0.13.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 (86) hide show
  1. package/README.md +3 -2
  2. package/dist/cadre-node.d.ts +328 -25
  3. package/dist/cadre-node.js +760 -59
  4. package/dist/cadre-node.js.map +1 -1
  5. package/dist/control-database.d.ts +19 -0
  6. package/dist/control-database.js +51 -9
  7. package/dist/control-database.js.map +1 -1
  8. package/dist/control-schema.d.ts +1 -1
  9. package/dist/control-schema.js +30 -2
  10. package/dist/control-schema.js.map +1 -1
  11. package/dist/enrolled-machine-store-file.d.ts +13 -0
  12. package/dist/enrolled-machine-store-file.js +41 -0
  13. package/dist/enrolled-machine-store-file.js.map +1 -0
  14. package/dist/enrolled-machine-store.d.ts +81 -0
  15. package/dist/enrolled-machine-store.js +225 -0
  16. package/dist/enrolled-machine-store.js.map +1 -0
  17. package/dist/file-durable-slot.js +5 -3
  18. package/dist/file-durable-slot.js.map +1 -1
  19. package/dist/index.d.ts +5 -3
  20. package/dist/index.js +23 -5
  21. package/dist/index.js.map +1 -1
  22. package/dist/membership-connection-gater.d.ts +15 -1
  23. package/dist/membership-connection-gater.js +9 -3
  24. package/dist/membership-connection-gater.js.map +1 -1
  25. package/dist/node-local-snapshot.d.ts +6 -0
  26. package/dist/node-local-snapshot.js +7 -1
  27. package/dist/node-local-snapshot.js.map +1 -1
  28. package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
  29. package/dist/{strand-backfill.js → peer-join-backfill.js} +133 -52
  30. package/dist/peer-join-backfill.js.map +1 -0
  31. package/dist/relay-addrs.d.ts +73 -0
  32. package/dist/relay-addrs.js +195 -0
  33. package/dist/relay-addrs.js.map +1 -1
  34. package/dist/strand-database.d.ts +12 -0
  35. package/dist/strand-database.js +16 -0
  36. package/dist/strand-database.js.map +1 -1
  37. package/dist/strand-formation-manager.d.ts +12 -0
  38. package/dist/strand-formation-manager.js +10 -3
  39. package/dist/strand-formation-manager.js.map +1 -1
  40. package/dist/strand-formation-protocol.d.ts +76 -2
  41. package/dist/strand-formation-protocol.js +78 -2
  42. package/dist/strand-formation-protocol.js.map +1 -1
  43. package/dist/strand-instance-manager.d.ts +147 -14
  44. package/dist/strand-instance-manager.js +225 -45
  45. package/dist/strand-instance-manager.js.map +1 -1
  46. package/dist/strand-membership-writer.d.ts +12 -0
  47. package/dist/strand-membership-writer.js +23 -1
  48. package/dist/strand-membership-writer.js.map +1 -1
  49. package/dist/strand-network-config.d.ts +84 -0
  50. package/dist/strand-network-config.js +136 -0
  51. package/dist/strand-network-config.js.map +1 -0
  52. package/dist/strand-revocation-enforcer.d.ts +392 -0
  53. package/dist/strand-revocation-enforcer.js +533 -0
  54. package/dist/strand-revocation-enforcer.js.map +1 -0
  55. package/dist/strand-solicitation.d.ts +7 -0
  56. package/dist/strand-solicitation.js +8 -2
  57. package/dist/strand-solicitation.js.map +1 -1
  58. package/dist/strand-watcher.js +6 -1
  59. package/dist/strand-watcher.js.map +1 -1
  60. package/dist/types.d.ts +210 -16
  61. package/dist/types.js +7 -1
  62. package/dist/types.js.map +1 -1
  63. package/package.json +16 -8
  64. package/src/cadre-node.ts +6353 -5603
  65. package/src/control-database.ts +53 -9
  66. package/src/control-schema.ts +30 -2
  67. package/src/enrolled-machine-store-file.ts +43 -0
  68. package/src/enrolled-machine-store.ts +280 -0
  69. package/src/file-durable-slot.ts +5 -3
  70. package/src/index.ts +52 -10
  71. package/src/membership-connection-gater.ts +9 -3
  72. package/src/node-local-snapshot.ts +13 -1
  73. package/src/peer-addr-book.ts +251 -250
  74. package/src/{strand-backfill.ts → peer-join-backfill.ts} +478 -389
  75. package/src/relay-addrs.ts +218 -0
  76. package/src/strand-database.ts +17 -0
  77. package/src/strand-formation-manager.ts +22 -3
  78. package/src/strand-formation-protocol.ts +808 -692
  79. package/src/strand-instance-manager.ts +317 -54
  80. package/src/strand-membership-writer.ts +23 -1
  81. package/src/strand-network-config.ts +172 -0
  82. package/src/strand-revocation-enforcer.ts +653 -0
  83. package/src/strand-solicitation.ts +484 -471
  84. package/src/strand-watcher.ts +319 -314
  85. package/src/types.ts +1554 -1354
  86. package/dist/strand-backfill.js.map +0 -1
@@ -118,6 +118,224 @@ export function resolveListenAddrs(
118
118
  return dedupe([...(listenAddrs ?? [DEFAULT_DIRECT_LISTEN_ADDR]), ...relayEntries]);
119
119
  }
120
120
 
121
+ /**
122
+ * The transport-derived `createLibp2pNode` options a resolved listen set implies —
123
+ * see {@link resolveTransportOptions}.
124
+ */
125
+ export interface ListenTransportOptions {
126
+ /**
127
+ * Present when some listen entry names WebSocket. Its VALUE is never bound; it is
128
+ * the switch that makes `@optimystic/db-p2p` add `webSockets()` — see
129
+ * {@link WS_TRANSPORT_SWITCH_PORT}.
130
+ */
131
+ wsPort?: number;
132
+ }
133
+
134
+ /**
135
+ * The `wsPort` value passed purely to turn `@optimystic/db-p2p`'s WebSocket transport
136
+ * ON. It is never bound, so it is deliberately `0` rather than a port scraped from a
137
+ * listen entry.
138
+ *
139
+ * WHY it is a switch and not a port: `createLibp2pNode`
140
+ * (`../optimystic/packages/db-p2p/src/libp2p-node.ts`) feeds `wsPort` into BOTH its
141
+ * default transports and its default listen addrs, and `createLibp2pNodeBase`
142
+ * (`libp2p-node-base.ts:490-491`) falls back to those two defaults INDEPENDENTLY:
143
+ *
144
+ * ```ts
145
+ * const listenAddrs = options.listenAddrs ?? defaults.listenAddrs;
146
+ * const transports = options.transports ?? defaults.transports;
147
+ * ```
148
+ *
149
+ * `cadre-core` always supplies explicit `listenAddrs` alongside it, so the synthesized
150
+ * `/ip4/<wsHost>/tcp/<wsPort>/ws` default ADDRESS is discarded while the `webSockets()`
151
+ * transport it added survives. A scraped port would also be a lie whenever a config
152
+ * names several WebSocket addresses on different ports, and the strand path rewrites
153
+ * fixed ports to `0` regardless (`strand-network-config.ts`).
154
+ *
155
+ * NOTE: db-p2p's own doc comment on `wsPort` (`libp2p-node-base.ts:170-172`) says it is
156
+ * "Ignored when `transports`/`listenAddrs` are explicitly provided" — true of the
157
+ * address half, NOT of the transport half, which is the half this depends on. Re-verify
158
+ * empirically rather than trusting that comment: `test/listen-transport-options.spec.ts`
159
+ * boots a real node and asserts a `/ws` multiaddr appears. If db-p2p ever makes the
160
+ * comment true of both halves, it needs an explicit `enableWebSockets` switch and this
161
+ * constant becomes unusable.
162
+ */
163
+ const WS_TRANSPORT_SWITCH_PORT = 0;
164
+
165
+ /**
166
+ * The transport-derived libp2p options `listenAddrs` implies, and the gate that stops a
167
+ * listen address the node has no transport for from being SILENTLY dropped.
168
+ *
169
+ * libp2p's transport manager sorts configured listen addresses by which transport claims
170
+ * them, discards the unclaimed ones, and raises `UnsupportedListenAddressesError` only
171
+ * when EVERY address was discarded. Pair a TCP address with a WebSocket one — which is
172
+ * exactly what the shipped configs do — and the TCP address carries the start while the
173
+ * missing WebSocket listener is never reported. Two halves of `NetworkConfig` that have
174
+ * to agree (`listenAddrs`, and the transports the node will actually have) were never
175
+ * checked against each other; this is that check, and it covers both node kinds because
176
+ * both resolve their listen set through {@link resolveListenAddrs}.
177
+ *
178
+ * The two transport classes are handled differently, on purpose:
179
+ *
180
+ * - **WebSocket is DERIVED.** It is the one non-TCP transport the shipped configs need
181
+ * (the React Native reference app's companion drone; `cadre start --ws-port`), and
182
+ * `@optimystic/db-p2p` already knows how to add it. `cadre-core` grows no dependency
183
+ * and decides no transport policy — it flips db-p2p's own switch
184
+ * ({@link WS_TRANSPORT_SWITCH_PORT}).
185
+ * - **Everything else is REFUSED.** Deriving `/quic-v1`, `/webrtc` or `/webtransport`
186
+ * would mean importing transport packages into every `cadre-core` consumer including
187
+ * the React Native and browser bundles (against the cross-platform rule in
188
+ * `AGENTS.md`) and duplicating policy `libp2p-node.ts` owns. So name the address, name
189
+ * the transport it needs, and refuse to start — matching how `network.relayAddrs` and
190
+ * `network.announceAddrs` already treat an operator typo.
191
+ *
192
+ * Returns `{}` unconditionally when `network.transports` is set: a programmatic embedder
193
+ * supplying transport factories owns the policy, and the factories are opaque — nothing
194
+ * can be inferred from them. That is what keeps the RN phone, the web app, and the
195
+ * integration-test harness unaffected.
196
+ *
197
+ * NOTE: pairing this with {@link resolveListenAddrs} is a CONVENTION, not a structure —
198
+ * a third libp2p-node build site could resolve listen addrs and forget to call this,
199
+ * putting the original bug back on that path. Both existing sites are covered
200
+ * (`cadre-node.ts` → `buildControlNodeOptions`, `strand-network-config.ts` →
201
+ * `strandNodeAddrs`), and they are the only two `createLibp2pNode` callers in this repo
202
+ * that derive their listen set from a `NetworkConfig` — the other callers
203
+ * (`integration-tests/src/harness/test-party.ts`, `quereus-plugin-sereus`'s `connect.ts`
204
+ * and `connect-browser.ts`) pass no `listenAddrs` at all, or pass their own transports
205
+ * with it. If a third CONFIG-derived site appears, fold the two functions into one that
206
+ * returns listen addrs and transport options together, so forgetting becomes impossible
207
+ * rather than merely unlikely.
208
+ *
209
+ * @param listenAddrs the ALREADY-resolved listen set — the output of
210
+ * {@link resolveListenAddrs} after any per-node rewriting — so the check reads what
211
+ * this node will actually bind.
212
+ * @throws {UnbindableListenAddressError} when a listen entry names a transport outside
213
+ * {tcp, ws/wss, p2p-circuit}.
214
+ */
215
+ export function resolveTransportOptions(
216
+ network: NetworkConfig | undefined,
217
+ listenAddrs: readonly string[] | undefined
218
+ ): ListenTransportOptions {
219
+ if (network?.transports) {
220
+ return {};
221
+ }
222
+ const kinds = (listenAddrs ?? []).map((addr) => [addr, listenTransportKind(addr)] as const);
223
+ const unsupported = kinds.filter(([, kind]) => kind === 'unsupported').map(([addr]) => addr);
224
+ if (unsupported.length > 0) {
225
+ throw new UnbindableListenAddressError(unsupported);
226
+ }
227
+ return kinds.some(([, kind]) => kind === 'websockets') ? { wsPort: WS_TRANSPORT_SWITCH_PORT } : {};
228
+ }
229
+
230
+ /**
231
+ * Thrown at config resolution when `network.listenAddrs` names an address none of the
232
+ * default transports can bind. Names each offending address alongside the libp2p
233
+ * transport package it would need — what the operator has without this is a node that
234
+ * starts and silently never listens there.
235
+ */
236
+ export class UnbindableListenAddressError extends Error {
237
+ constructor(readonly listenAddrs: readonly string[]) {
238
+ super(
239
+ 'network.listenAddrs names an address this node has no transport for: ' +
240
+ `${listenAddrs.map((a) => `${a} — ${unbindableReason(a)}`).join('; ')}. ` +
241
+ 'Default transports bind TCP (including /unix/<path>), WebSocket (/ws, /wss) and circuit-relay addresses only. ' +
242
+ 'Either drop the address, or supply the transport programmatically via network.transports.'
243
+ );
244
+ this.name = 'UnbindableListenAddressError';
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Which default transport claims `listenAddr`, or `'unsupported'` when none does.
250
+ *
251
+ * Classification is a multiaddr COMPONENT question, not a string question, so this
252
+ * parses and reads the transport stack — the component names left once the addressing
253
+ * and security layers are dropped. That stack is outermost-LAST
254
+ * (`/ip4/…/tcp/443/tls/ws` → `['tcp', 'ws']`), which is why its terminal entry decides.
255
+ *
256
+ * An unparsable entry is `'ignored'` and passes through untouched, following
257
+ * `isConfiguredCircuitListenAddr` below and `ephemeralPortListenAddr`
258
+ * (`strand-network-config.ts`): libp2p reports a bad listen addr itself, and this check
259
+ * must only ever ADD a denial.
260
+ */
261
+ function listenTransportKind(listenAddr: string): 'tcp' | 'websockets' | 'circuit' | 'ignored' | 'unsupported' {
262
+ let stack: readonly string[];
263
+ try {
264
+ stack = transportStack(listenAddr);
265
+ } catch {
266
+ return 'ignored';
267
+ }
268
+ const outermost = stack.at(-1);
269
+ if (outermost === undefined) {
270
+ // Nothing but addressing components — a bare `/ip4/1.2.3.4`. No transport claims
271
+ // it, so it is dropped by exactly the same silent path this check exists to close.
272
+ return 'unsupported';
273
+ }
274
+ if (outermost === 'p2p-circuit') {
275
+ return 'circuit';
276
+ }
277
+ if (outermost === 'ws' || outermost === 'wss') {
278
+ return 'websockets';
279
+ }
280
+ // Only a BARE tcp or unix stack is TCP. `@libp2p/tcp` binds both — its `listenFilter`
281
+ // accepts an exact TCP match OR a `/unix/<path>` address (a named pipe on Windows) —
282
+ // and nothing layered on top of either, which is why `/tcp/<port>/<anything else>` is
283
+ // NOT waved through merely because a `tcp` component happens to appear.
284
+ return stack.length === 1 && (outermost === 'tcp' || outermost === 'unix') ? 'tcp' : 'unsupported';
285
+ }
286
+
287
+ /**
288
+ * Component names of `listenAddr` with the addressing and security layers removed, so
289
+ * what remains names transports. Host components (`ip4`, `dns4`, …) and the layers that
290
+ * ride inside a transport without being one (`tls`, `sni`, `certhash`, `p2p`) say
291
+ * nothing about which transport has to be configured.
292
+ */
293
+ function transportStack(listenAddr: string): string[] {
294
+ return multiaddr(listenAddr).getComponents()
295
+ .map((component) => component.name)
296
+ .filter((name) => !NON_TRANSPORT_COMPONENTS.has(name));
297
+ }
298
+
299
+ /** Components that address or secure a connection rather than name its transport. */
300
+ const NON_TRANSPORT_COMPONENTS = new Set([
301
+ 'ip4', 'ip6', 'ip6zone', 'dns', 'dns4', 'dns6', 'dnsaddr',
302
+ 'tls', 'sni', 'certhash', 'p2p', 'noise'
303
+ ]);
304
+
305
+ /**
306
+ * Why `listenAddr` cannot be bound, phrased for the operator: the libp2p package that
307
+ * would make it bindable where one is known, and otherwise what is wrong with it.
308
+ *
309
+ * Read OUTERMOST-first (`/udp/…/quic-v1/webtransport` needs `@libp2p/webtransport`,
310
+ * not the `@libp2p/quic` it rides on), matching how `listenTransportKind` decides.
311
+ */
312
+ function unbindableReason(listenAddr: string): string {
313
+ let stack: readonly string[];
314
+ try {
315
+ stack = transportStack(listenAddr);
316
+ } catch {
317
+ return 'not a parsable multiaddr';
318
+ }
319
+ if (stack.length === 0) {
320
+ return 'it names no transport component';
321
+ }
322
+ const pkg = [...stack].reverse().map((name) => TRANSPORT_PACKAGES[name]).find((p) => p !== undefined);
323
+ return pkg === undefined ? 'needs an unrecognized transport' : `needs ${pkg}`;
324
+ }
325
+
326
+ /**
327
+ * The libp2p package each unbindable transport component comes from. Naming the package
328
+ * is the actionable half of the refusal — the operator either drops the address or wires
329
+ * that package in through `network.transports`.
330
+ */
331
+ const TRANSPORT_PACKAGES: Record<string, string> = {
332
+ 'quic': '@libp2p/quic',
333
+ 'quic-v1': '@libp2p/quic',
334
+ 'webtransport': '@libp2p/webtransport',
335
+ 'webrtc': '@libp2p/webrtc',
336
+ 'webrtc-direct': '@libp2p/webrtc'
337
+ };
338
+
121
339
  /**
122
340
  * Thrown out of `CadreNode.start()` when the boot-path reservation drive for
123
341
  * `network.relayAddrs` produces no `/p2p-circuit` address on its FIRST attempt.
@@ -154,6 +154,23 @@ export class StrandDatabase {
154
154
  return strandMemberKeyPair(memberPrivateKey);
155
155
  }
156
156
 
157
+ /**
158
+ * Run the founder membership bootstrap against an ALREADY-LIVE database — the
159
+ * seam {@link StrandInstanceManager.foundExistingStrand} uses when a founder
160
+ * request arrives for an instance that was first launched as a joiner.
161
+ * Idempotent: every bootstrap write is insert-if-absent
162
+ * ({@link bootstrapFounderMembership}), so calling it on an instance that
163
+ * already founded writes nothing. Also flips the captured config's `founder`,
164
+ * so this object's own record of how it was launched stays coherent with what
165
+ * actually ran (a construction-time `founder: false` is a statement about the
166
+ * launch, not a permanent identity).
167
+ */
168
+ async ensureFounderBootstrap(): Promise<void> {
169
+ this.ensureInitialized();
170
+ this.config.founder = true;
171
+ await this.bootstrapFounder();
172
+ }
173
+
157
174
  /**
158
175
  * Get the underlying database for queries
159
176
  */
@@ -96,6 +96,17 @@ export interface StrandFormationManagerOptions {
96
96
  partyId: string;
97
97
  /** This party's cadre peer addresses */
98
98
  cadrePeerAddrs?: string[];
99
+ /**
100
+ * This node's live STRAND-network multiaddrs for a strand it is running (responder
101
+ * side). Wired by `CadreNode` to its own per-strand address lookup; the manager has no
102
+ * route to the strand runtime itself.
103
+ *
104
+ * A hook, not a `config` knob, so it sits with the other responder-side seams
105
+ * (`strandProvisioner`, `formationUsageRecorder`) rather than among the timeouts. Left
106
+ * unwired — every mock/transport test — the responder simply discloses no strand
107
+ * addresses and cross-party joiners fall back to an empty seed.
108
+ */
109
+ resolveStrandAddrs?: (strandId: string) => string[];
99
110
  /** Configuration options */
100
111
  config?: StrandFormationManagerConfig;
101
112
  }
@@ -120,6 +131,7 @@ export class StrandFormationManager {
120
131
  private readonly formationResponseValidator?: FormationResponseValidator;
121
132
  private readonly partyId: string;
122
133
  private readonly cadrePeerAddrs: string[];
134
+ private readonly resolveStrandAddrs?: (strandId: string) => string[];
123
135
  private readonly config: StrandFormationManagerConfig;
124
136
  private readonly listener: FormationListener;
125
137
  private readonly registeredNodes = new Set<Libp2p>();
@@ -132,6 +144,7 @@ export class StrandFormationManager {
132
144
  this.formationResponseValidator = options.formationResponseValidator;
133
145
  this.partyId = options.partyId;
134
146
  this.cadrePeerAddrs = options.cadrePeerAddrs ?? [];
147
+ this.resolveStrandAddrs = options.resolveStrandAddrs;
135
148
  this.config = options.config ?? {};
136
149
 
137
150
  this.listener = new FormationListener({
@@ -140,6 +153,9 @@ export class StrandFormationManager {
140
153
  provisionStrand: (contact, signal) =>
141
154
  this.provisionAsResponder(contact, signal),
142
155
  getResponderIdentity: () => ({ partyId: this.partyId, cadrePeerAddrs: this.cadrePeerAddrs }),
156
+ // Forwarded only when wired, so `FormationListenerOptions.resolveStrandAddrs`
157
+ // stays genuinely absent (and the listener short-circuits) for an unwired manager.
158
+ ...(this.resolveStrandAddrs && { resolveStrandAddrs: this.resolveStrandAddrs }),
143
159
  sessionTimeoutMs: this.config.sessionTimeoutMs,
144
160
  stepTimeoutMs: this.config.stepTimeoutMs,
145
161
  provisionTimeoutMs: this.config.provisionTimeoutMs,
@@ -204,7 +220,7 @@ export class StrandFormationManager {
204
220
 
205
221
  this.dialerSessions++;
206
222
  try {
207
- const provision = await dialFormation(node, {
223
+ const dialed = await dialFormation(node, {
208
224
  contact,
209
225
  responderAddrs: invitation.bootstrap,
210
226
  validateResponse: (response) => this.validateResponse(invitation, disclosure, response),
@@ -214,7 +230,8 @@ export class StrandFormationManager {
214
230
  protocolId: this.config.protocolId
215
231
  });
216
232
 
217
- log('Strand formed: %s', provision.strand.strandId);
233
+ const provision = dialed.provision;
234
+ log('Strand formed: %s (%d cross-party strand addr(s))', provision.strand.strandId, dialed.strandAddrs.length);
218
235
 
219
236
  return {
220
237
  memberKey: contact.partyId,
@@ -223,7 +240,9 @@ export class StrandFormationManager {
223
240
  // The host strand's membership key, delivered through the protocol (provision-then-record).
224
241
  // Undefined for an open strand. Kept separate from invitePrivateKey (the initiator's
225
242
  // generated signing key), which is set by the StrandSolicitationService layer.
226
- memberPrivateKey: provision.memberPrivateKey
243
+ memberPrivateKey: provision.memberPrivateKey,
244
+ // Possibly empty — the responder may hold no live strand node yet.
245
+ strandAddrs: dialed.strandAddrs
227
246
  };
228
247
  } finally {
229
248
  this.dialerSessions--;