@serfab/cadre-core 0.11.0 → 0.12.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/dist/announce-addrs.d.ts +49 -0
  2. package/dist/announce-addrs.js +78 -0
  3. package/dist/announce-addrs.js.map +1 -0
  4. package/dist/cadre-node.d.ts +281 -30
  5. package/dist/cadre-node.js +513 -103
  6. package/dist/cadre-node.js.map +1 -1
  7. package/dist/control-database.d.ts +155 -23
  8. package/dist/control-database.js +272 -104
  9. package/dist/control-database.js.map +1 -1
  10. package/dist/control-read-retry.d.ts +103 -0
  11. package/dist/control-read-retry.js +145 -0
  12. package/dist/control-read-retry.js.map +1 -0
  13. package/dist/control-retry.d.ts +101 -0
  14. package/dist/control-retry.js +108 -0
  15. package/dist/control-retry.js.map +1 -0
  16. package/dist/control-schema.d.ts +1 -1
  17. package/dist/control-schema.js +20 -4
  18. package/dist/control-schema.js.map +1 -1
  19. package/dist/control-write-retry.d.ts +59 -35
  20. package/dist/control-write-retry.js +21 -92
  21. package/dist/control-write-retry.js.map +1 -1
  22. package/dist/delegate-admission.d.ts +17 -15
  23. package/dist/delegate-admission.js +17 -15
  24. package/dist/delegate-admission.js.map +1 -1
  25. package/dist/index.d.ts +6 -4
  26. package/dist/index.js +17 -8
  27. package/dist/index.js.map +1 -1
  28. package/dist/membership-connection-gater.d.ts +211 -25
  29. package/dist/membership-connection-gater.js +309 -29
  30. package/dist/membership-connection-gater.js.map +1 -1
  31. package/dist/peer-record.d.ts +26 -0
  32. package/dist/peer-record.js +32 -0
  33. package/dist/peer-record.js.map +1 -1
  34. package/dist/relay-addrs.d.ts +76 -26
  35. package/dist/relay-addrs.js +115 -27
  36. package/dist/relay-addrs.js.map +1 -1
  37. package/dist/relay-reservation.d.ts +20 -15
  38. package/dist/relay-reservation.js +18 -14
  39. package/dist/relay-reservation.js.map +1 -1
  40. package/dist/strand-addr-protocol.js +16 -0
  41. package/dist/strand-addr-protocol.js.map +1 -1
  42. package/dist/strand-backfill.js +13 -4
  43. package/dist/strand-backfill.js.map +1 -1
  44. package/dist/strand-formation-manager.d.ts +10 -9
  45. package/dist/strand-formation-manager.js +10 -9
  46. package/dist/strand-formation-manager.js.map +1 -1
  47. package/dist/strand-formation-protocol.d.ts +7 -7
  48. package/dist/strand-formation-protocol.js +8 -8
  49. package/dist/strand-formation-protocol.js.map +1 -1
  50. package/dist/strand-instance-manager.d.ts +4 -3
  51. package/dist/strand-instance-manager.js +31 -6
  52. package/dist/strand-instance-manager.js.map +1 -1
  53. package/dist/strand-membership-writer.d.ts +106 -14
  54. package/dist/strand-membership-writer.js +180 -14
  55. package/dist/strand-membership-writer.js.map +1 -1
  56. package/dist/strand-solicitation.d.ts +3 -3
  57. package/dist/strand-solicitation.js +4 -5
  58. package/dist/strand-solicitation.js.map +1 -1
  59. package/dist/strand-wake-protocol.d.ts +47 -2
  60. package/dist/strand-wake-protocol.js +76 -6
  61. package/dist/strand-wake-protocol.js.map +1 -1
  62. package/dist/types.d.ts +74 -29
  63. package/dist/types.js.map +1 -1
  64. package/package.json +8 -8
  65. package/src/announce-addrs.ts +93 -0
  66. package/src/cadre-node.ts +5603 -5158
  67. package/src/control-database.ts +2584 -2406
  68. package/src/control-read-retry.ts +167 -0
  69. package/src/control-retry.ts +190 -0
  70. package/src/control-schema.ts +20 -4
  71. package/src/control-write-retry.ts +30 -130
  72. package/src/delegate-admission.ts +17 -15
  73. package/src/index.ts +435 -407
  74. package/src/membership-connection-gater.ts +533 -204
  75. package/src/peer-record.ts +33 -0
  76. package/src/relay-addrs.ts +218 -111
  77. package/src/relay-reservation.ts +828 -823
  78. package/src/strand-addr-protocol.ts +17 -0
  79. package/src/strand-backfill.ts +389 -366
  80. package/src/strand-formation-manager.ts +492 -491
  81. package/src/strand-formation-protocol.ts +8 -8
  82. package/src/strand-instance-manager.ts +636 -611
  83. package/src/strand-membership-writer.ts +209 -14
  84. package/src/strand-solicitation.ts +6 -7
  85. package/src/strand-wake-protocol.ts +94 -8
  86. package/src/types.ts +74 -29
@@ -1,611 +1,636 @@
1
- import debug from 'debug';
2
- import type { PrivateKey } from '@libp2p/interface';
3
- import { createLibp2pNode, type IRawStorage } from '@optimystic/db-p2p';
4
- import { wrapStorageWithCache, disposeStorageCache } from '@serfab/quereus-plugin-sereus';
5
- import { StrandDatabase } from './strand-database.js';
6
- import { StrandBackfill, type StrandBackfillConfig } from './strand-backfill.js';
7
- import { assertSchemaSignature } from './schema-verification.js';
8
- import type {
9
- StrandInstance,
10
- StrandRow,
11
- StorageConfig,
12
- NetworkConfig,
13
- LatencyHint,
14
- NodeProfile,
15
- SAppConfig,
16
- SAppInfo,
17
- RawStorageProvider,
18
- Libp2pNodeWithRepo
19
- } from './types.js';
20
- import { resolveStrandClusterSize, STRAND_CLUSTER_POLICY } from './types.js';
21
- import { resolveListenAddrs } from './relay-addrs.js';
22
-
23
- const log = debug('sereus:cadre:strand-manager');
24
- const timing = debug('sereus:cadre:timing');
25
-
26
- /**
27
- * Configuration for starting a strand instance
28
- */
29
- export interface StartStrandConfig {
30
- strandRow: StrandRow;
31
- /** sApp configuration provided by the hosting application */
32
- sAppConfig: SAppConfig;
33
- storage?: StorageConfig;
34
- network?: NetworkConfig;
35
- profile: NodeProfile;
36
- defaultLatencyHint: LatencyHint;
37
- privateKey?: PrivateKey;
38
- /** Cohort-derived discovery seed (multiaddr strings). Defaults to [] when omitted. */
39
- bootstrapNodes?: string[];
40
- /**
41
- * Require a valid author signature on the sApp schema before bring-up.
42
- * Defaults to true (fail closed) when omitted; set false only for dev/test
43
- * with unsigned demo schemas. Mirrors {@link CadreNodeConfig.requireSignedSchemas}.
44
- */
45
- requireSignedSchemas?: boolean;
46
- /**
47
- * Whether this node founds the strand (vs. joins it). Forwarded to the
48
- * StrandDatabase so the founder bootstrap (Header, founding Member/Manager)
49
- * runs once at bring-up. Joiners leave this unset and write nothing. See
50
- * {@link StrandConfig.founder}.
51
- */
52
- founder?: boolean;
53
- /**
54
- * Number of nodes Optimystic is told this strand's replication cluster should
55
- * have. Same rule as {@link CadreNodeConfig.strandClusterSize}, which CadreNode
56
- * forwards here: every node on the strand should use the same value, and it is
57
- * frozen when the strand's libp2p node is created. Defaults to
58
- * `DEFAULT_STRAND_CLUSTER_SIZE` (4); values below `MIN_CLUSTER_SIZE` (2) are
59
- * rejected by `resolveStrandClusterSize`. Strand-only — the
60
- * control network's breadth is the fixed `CONTROL_REPLICATION_BREADTH`.
61
- */
62
- clusterSize?: number;
63
- /**
64
- * Tuning for the strand peer-join block catch-up ({@link StrandBackfill}),
65
- * forwarded from {@link CadreNodeConfig.strandBackfill}. When the strand's
66
- * libp2p node connects to a peer this runtime has not yet caught up, every
67
- * block in the strand's own raw store is pushed to it. Runs only on strands
68
- * with per-strand storage; `{ enabled: false }` restores the pre-existing
69
- * no-backfill behaviour.
70
- */
71
- backfill?: StrandBackfillConfig;
72
- }
73
-
74
- /**
75
- * Volatile inputs re-resolved when resuming a quiesced strand. The cohort
76
- * discovery seed is the only one — it grows as peers are learned since the
77
- * strand first launched.
78
- */
79
- export interface ResumeStrandOverrides {
80
- /** Freshly-resolved cohort discovery seed (multiaddr strings). */
81
- bootstrapNodes?: string[];
82
- }
83
-
84
- /**
85
- * Get the isolated storage path for a specific strand.
86
- *
87
- * @deprecated This helper is Node-only and throws in React Native (it assumes a
88
- * filesystem layout). Use a storage provider factory function instead, which
89
- * receives the strandId and can create strand-specific storage paths using
90
- * platform-appropriate methods.
91
- *
92
- * @example
93
- * // Instead of using getStrandStoragePath, use a storage provider factory:
94
- * const storage = {
95
- * provider: (strandId: string) => new FileRawStorage(`./data/strands/${strandId}`)
96
- * };
97
- */
98
- export function getStrandStoragePath(basePath: string, strandId: string): string {
99
- // Check if we're in a Node.js environment
100
- if (typeof process === 'undefined' || !process.versions?.node) {
101
- throw new Error(
102
- 'getStrandStoragePath is not available in React Native. ' +
103
- 'Use a storage provider factory function instead.'
104
- );
105
- }
106
-
107
- // Sanitize strandId for filesystem safety (UUIDs should be safe, but just in case)
108
- const safeId = strandId.replace(/[^a-zA-Z0-9-]/g, '_');
109
-
110
- // Build the path with plain string joins rather than the Node `path` module.
111
- // A static `require('path')` forces RN bundlers (e.g. Metro) to *resolve* the
112
- // module at bundle time even though this Node-only helper throws above before
113
- // ever reaching here — joining by hand keeps the module free of any Node
114
- // built-in reference, so RN bundles need no `path` shim.
115
- const trimmedBase = basePath.replace(/[\\/]+$/, '');
116
- return `${trimmedBase}/strands/${safeId}`;
117
- }
118
-
119
- /**
120
- * Resolve a storage provider for a specific strand.
121
- * If the provider is a factory function, call it with the strandId.
122
- *
123
- * Called only from {@link StrandInstanceManager.startStrand}, which owns the result
124
- * for the instance's lifetime — see `strandStorages`.
125
- *
126
- * @param provider - Storage provider (instance or factory)
127
- * @param strandId - The strand ID to create storage for
128
- * @returns The resolved IRawStorage instance, or undefined if no provider
129
- */
130
- function resolveStrandStorage(
131
- provider: RawStorageProvider | undefined,
132
- strandId: string
133
- ): IRawStorage | undefined {
134
- if (!provider) {
135
- return undefined;
136
- }
137
- const storage = typeof provider === 'function' ? provider(strandId) : provider;
138
- // Wrapped in the write-through raw-storage cache (quereus-plugin-sereus's cached-storage.ts).
139
- // Called ONCE per strand launch — `startStrand` keeps the result for the instance's
140
- // lifetime — so the wrap survives every runtime rebuild the instance goes through.
141
- return wrapStorageWithCache(storage, strandId);
142
- }
143
-
144
- /**
145
- * Manages individual strand instances - creates and destroys isolated libp2p nodes
146
- * for each strand the cadre participates in.
147
- */
148
- export class StrandInstanceManager {
149
- private instances: Map<string, StrandInstance> = new Map();
150
- /**
151
- * Retained launch config per strand, captured in `startStrand` and cleared in
152
- * `stopStrand`. `resumeStrand` reuses it to rebuild a quiesced strand's runtime
153
- * without the caller re-threading storage/network/profile/key/sApp config.
154
- */
155
- private launchConfigs: Map<string, StartStrandConfig> = new Map();
156
- /**
157
- * The per-strand peer-join block catch-up, keyed by strand id. Private (not on
158
- * the public {@link StrandInstance}) because it is runtime plumbing with the
159
- * same lifetime as the strand's libp2p node: created in `buildStrandRuntime`,
160
- * stopped and dropped in `releaseRuntime` — so quiesce → resume rebuilds it
161
- * with a fresh caught-up-peer memo, which is intended (a resumed node may have
162
- * missed writes).
163
- */
164
- private backfills: Map<string, StrandBackfill> = new Map();
165
- /**
166
- * The resolved (cache-wrapped) raw storage per strand id — the instance's OWN
167
- * store, resolved once in `startStrand` and held until `stopStrand` disposes it.
168
- * Private for the same reason `backfills` is: runtime plumbing, not part of the
169
- * public {@link StrandInstance}.
170
- *
171
- * Deliberately NOT touched by `releaseRuntime`: that is what lets a quiesce →
172
- * resume cycle rebuild the libp2p node over the SAME store, keeping its
173
- * write-through cache warm (and, on an in-memory backend, keeping the strand's
174
- * blocks at all). An entry exists iff `instances` does AND the launch config
175
- * supplied a storage provider.
176
- *
177
- * NOTE: a hibernating strand therefore keeps its store — and its share of the
178
- * process-wide cache pool — resident for as long as the instance is tracked. That
179
- * is the point (a warm wake), and the pool evicts under pressure, so the cost is
180
- * one map entry per hibernating strand today. If a device ever hibernates strands
181
- * by the hundred, revisit: dropping the store at quiesce and paying for a cold
182
- * wake becomes the better trade.
183
- */
184
- private strandStorages: Map<string, IRawStorage> = new Map();
185
- private stopping = false;
186
-
187
- constructor() {
188
- log('StrandInstanceManager created');
189
- }
190
-
191
- /**
192
- * Get all current strand instances
193
- */
194
- getInstances(): Map<string, StrandInstance> {
195
- return new Map(this.instances);
196
- }
197
-
198
- /**
199
- * Get a specific strand instance
200
- */
201
- getInstance(strandId: string): StrandInstance | undefined {
202
- return this.instances.get(strandId);
203
- }
204
-
205
- /**
206
- * Check if a strand is currently running
207
- */
208
- hasStrand(strandId: string): boolean {
209
- return this.instances.has(strandId);
210
- }
211
-
212
- /**
213
- * Start a new strand instance.
214
- *
215
- * A failed launch leaves NOTHING tracked: the instance and its retained launch
216
- * config are both dropped before the error is rethrown, so the strand id is
217
- * free for a genuine retry. This matches the pre-registration failure path
218
- * (a rejected schema signature, which throws before anything is recorded) —
219
- * both failure modes of this call leave the same residue: none. Callers learn
220
- * of the failure from the rejected promise (and, on the control-discovered
221
- * path, from CadreNode's `strand:error` event), not from an error record left
222
- * behind in `instances`.
223
- *
224
- * The strand's raw storage is resolved HERE, once, and held for the instance's
225
- * lifetime (see `strandStorages`) — `buildStrandRuntime` only reads it, so a
226
- * hibernation wake never re-enters the embedder's provider.
227
- */
228
- async startStrand(config: StartStrandConfig): Promise<StrandInstance> {
229
- const { strandRow, sAppConfig } = config;
230
- const strandId = strandRow.Id;
231
-
232
- if (this.stopping) {
233
- throw new Error('StrandInstanceManager is stopping');
234
- }
235
-
236
- if (this.instances.has(strandId)) {
237
- log('Strand %s already running', strandId);
238
- return this.instances.get(strandId)!;
239
- }
240
-
241
- log('Starting strand instance: %s (sApp: %s v%s)', strandId, sAppConfig.id, sAppConfig.version);
242
- const tTotal = performance.now();
243
-
244
- // Verify schema signature before proceeding (fail-closed by default)
245
- const requireSignature = config.requireSignedSchemas ?? true;
246
- assertSchemaSignature(sAppConfig, { requireSignature });
247
- log('Strand %s sApp schema signature verified (author: %s)', strandId, sAppConfig.id);
248
-
249
- // Convert SAppConfig to SAppInfo for the instance
250
- const sAppInfo: SAppInfo = {
251
- id: sAppConfig.id,
252
- version: sAppConfig.version,
253
- schema: sAppConfig.schema,
254
- signature: sAppConfig.signature
255
- };
256
-
257
- // Resolve this strand's storage ONCE, before anything is recorded. If a factory
258
- // function is provided, it is called with the strandId to create strand-specific
259
- // storage (e.g. strand-isolated directories). A provider that throws therefore
260
- // fails the launch alongside the schema-signature check, leaving nothing tracked.
261
- const strandStorage = resolveStrandStorage(config.storage?.provider, strandId);
262
- if (strandStorage) {
263
- log('Strand %s using provided storage provider', strandId);
264
- }
265
-
266
- // Determine latency hint: sApp config > default
267
- const latencyHint = sAppConfig.latencyHint ?? config.defaultLatencyHint;
268
-
269
- const instance: StrandInstance = {
270
- strandId,
271
- status: 'starting',
272
- sAppInfo,
273
- memberPrivateKey: strandRow.MemberPrivateKey ?? undefined,
274
- connectedPeers: 0,
275
- lastActivity: new Date(),
276
- latencyHint
277
- };
278
-
279
- this.instances.set(strandId, instance);
280
- this.launchConfigs.set(strandId, config);
281
- if (strandStorage) {
282
- this.strandStorages.set(strandId, strandStorage);
283
- }
284
-
285
- try {
286
- await this.buildStrandRuntime(instance, config);
287
- timing('[startStrand:%s] total: %dms', strandId, Math.round(performance.now() - tTotal));
288
- log('Strand %s started successfully with sApp %s', strandId, sAppConfig.id);
289
- return instance;
290
- } catch (error) {
291
- // Status/error first — the (now discarded) record is still what `log` reports on.
292
- instance.status = 'error';
293
- instance.error = error instanceof Error ? error.message : String(error);
294
- log('Failed to start strand %s: %s', strandId, instance.error);
295
- // Drop the dead record so this strand id can be launched again. Keep the
296
- // `launchConfigs` has an entry iff `instances` does invariant — resumeStrand
297
- // reads both, and a config without an instance would strand the config.
298
- this.instances.delete(strandId);
299
- this.launchConfigs.delete(strandId);
300
- // The store this launch resolved goes with it: nothing owns it any more, and a
301
- // retained cache wrapper would be handed back (already retired) on a retry.
302
- await this.disposeStrandStorage(strandId);
303
- throw error;
304
- }
305
- }
306
-
307
- /**
308
- * Build (or rebuild) the libp2p node + StrandDatabase for an instance and
309
- * attach them, transitioning it to `active`. Shared by `startStrand` (fresh
310
- * launch) and `resumeStrand` (rehydrating a quiesced instance). Reads all
311
- * volatile inputs (bootstrapNodes, network, profile, privateKey, sApp config)
312
- * from `config`, so the caller controls the cohort-derived values. Storage is the
313
- * one input it does NOT re-read from `config`: that belongs to the instance and
314
- * comes from `strandStorages`.
315
- */
316
- private async buildStrandRuntime(instance: StrandInstance, config: StartStrandConfig): Promise<void> {
317
- const strandId = instance.strandId;
318
- const { sAppConfig } = config;
319
-
320
- // The store the instance OWNS (resolved once in `startStrand`), not a fresh
321
- // resolution: a rebuild must reach the same backend through the same warm cache.
322
- const strandStorage = this.strandStorages.get(strandId);
323
-
324
- // db-p2p namespaces every one of the node's protocol ids by network name
325
- // (`/optimystic/<networkName>/...`), so anything dialing this node's own
326
- // services must derive its prefix from the SAME string the node was built
327
- // with — hence one binding for both, not two literals that can drift.
328
- const networkName = `strand-${strandId}`;
329
- const protocolPrefix = `/optimystic/${networkName}`;
330
-
331
- // Determine relay mode: if explicitly set in config, use that;
332
- // otherwise default to true for storage profile nodes.
333
- const enableRelay = config.network?.enableRelay ?? (config.profile === 'storage');
334
- const listenAddrs = resolveListenAddrs(config.network);
335
-
336
- try {
337
- let t0 = performance.now();
338
- const node = await createLibp2pNode({
339
- port: 0, // Random port
340
- bootstrapNodes: config.bootstrapNodes ?? [],
341
- networkName,
342
- storage: strandStorage,
343
- fretProfile: config.profile === 'storage' ? 'core' : 'edge',
344
- relay: enableRelay,
345
- clusterSize: resolveStrandClusterSize(config.clusterSize),
346
- // Deliberately NOT CONTROL_CLUSTER_POLICY: a strand is application data with its own
347
- // breadth reasoning, and the shape match with the control policy is a coincidence.
348
- clusterPolicy: STRAND_CLUSTER_POLICY,
349
- arachnode: {
350
- enableRingZulu: config.profile === 'storage'
351
- },
352
- ...(config.privateKey && { privateKey: config.privateKey }),
353
- ...(config.network?.transports && { transports: config.network.transports }),
354
- // NOTE: strand nodes receive the same RESOLVED listen addrs as the
355
- // control node. A fixed-port listen addr (e.g. cadre-cli's example
356
- // `/ip4/0.0.0.0/tcp/4001`) would have control + strand nodes racing to
357
- // bind one port — EADDRINUSE. Unverified; if a deployment configures a
358
- // fixed port and strands fail to start, rewrite the port per node here.
359
- // An inherited `/p2p-circuit` addr, by contrast, is deliberate — it is
360
- // what gives a NAT'd strand node a reachable relay slot — and works
361
- // because the launch path announces this strand's derived peerId to
362
- // the relay first (delegate admission; see cadre-node.ts). Those circuit
363
- // entries come either from a hand-written `network.listenAddrs` or from
364
- // `network.relayAddrs`, which `resolveListenAddrs` folds into the same list.
365
- ...(listenAddrs && { listenAddrs }),
366
- ...(config.network?.connectionGater && { connectionGater: config.network.connectionGater })
367
- }) as Libp2pNodeWithRepo;
368
- timing('[buildStrandRuntime:%s] createLibp2pNode: %dms', strandId, Math.round(performance.now() - t0));
369
-
370
- instance.libp2pNode = node;
371
-
372
- // Create and initialize the StrandDatabase.
373
- //
374
- // Attach before initialize so a failed init is cleaned up by
375
- // releaseRuntime below (close() is safe on a partially-initialized db).
376
- t0 = performance.now();
377
- const strandDb = new StrandDatabase({
378
- strandId,
379
- sAppConfig,
380
- libp2pNode: node,
381
- coordinatedRepo: node.coordinatedRepo,
382
- // Founder bootstrap inputs: the strand's type drives which membership rows
383
- // are written, and the closed-strand MemberPrivateKey derives the founding
384
- // Member/Manager key. Both come off the control-network strand row.
385
- strandType: config.strandRow.Type,
386
- memberPrivateKey: config.strandRow.MemberPrivateKey ?? undefined,
387
- founder: config.founder
388
- });
389
- instance.database = strandDb;
390
- await strandDb.initialize();
391
- timing('[buildStrandRuntime:%s] strandDatabase.initialize: %dms', strandId, Math.round(performance.now() - t0));
392
-
393
- // Peer-join block catch-up: push this strand's own blocks to each newly
394
- // connected peer, so a machine that joined after blocks were committed
395
- // still ends up physically holding them (without per-strand storage there
396
- // is nothing to copy). Armed for EVERY stored strand: `StrandBackfill`
397
- // only does work when the strand's libp2p node reports a peer connection,
398
- // so on a device that is genuinely alone it is inert, and arming it at
399
- // launch is what closes the "founded alone, never replicates" hole — a
400
- // peer that joins later gets the founder's blocks without any relaunch.
401
- //
402
- // NOTE: cost is one StrandBackfill object + one `connection:open` listener
403
- // per running strand — linear in strand count, negligible at the handful a
404
- // device or host runs today. If a node ever hosts strands by the hundred,
405
- // move to one shared listener that dispatches by strand id.
406
- if (strandStorage && config.backfill?.enabled !== false) {
407
- if (node.keyNetwork) {
408
- const backfill = new StrandBackfill({
409
- strandId,
410
- libp2p: node,
411
- peerNetwork: node.keyNetwork,
412
- storage: strandStorage,
413
- // The same prefix the receiver registered its block-transfer handler
414
- // under — derived from networkName above, never re-spelled here.
415
- protocolPrefix
416
- }, config.backfill);
417
- backfill.start();
418
- this.backfills.set(strandId, backfill);
419
- } else {
420
- log('Strand %s: libp2p node exposes no keyNetwork; peer-join block catch-up is inert', strandId);
421
- }
422
- }
423
-
424
- instance.status = 'active';
425
- instance.lastActivity = new Date();
426
- } catch (error) {
427
- // Roll back any partially-attached runtime so the instance is left with
428
- // NEITHER handle. Otherwise the `libp2pNode || database` "already live"
429
- // guard in resumeStrand/handleStrandWake would treat a half-built strand
430
- // as healthy — leaking the libp2p node and never retrying the rebuild.
431
- await this.releaseRuntime(instance).catch((cleanupErr) => {
432
- log('buildStrandRuntime cleanup for strand %s also failed: %o', strandId, cleanupErr);
433
- });
434
- throw error;
435
- }
436
- }
437
-
438
- /**
439
- * Release an instance's strand-network runtime: close the StrandDatabase, then
440
- * stop the libp2p node (construction order in reverse), clearing both fields
441
- * and zeroing connectedPeers. Tolerant of partially-built state — either handle
442
- * may be absent — so it doubles as rollback for a failed `buildStrandRuntime`.
443
- * Shared by `quiesceStrand`, `stopStrand`, and that rollback path.
444
- *
445
- * Leaves `strandStorages` untouched by design — the store outlives the runtime it
446
- * was built into, which is what makes a resume warm. Only `stopStrand` disposes it.
447
- */
448
- private async releaseRuntime(instance: StrandInstance): Promise<void> {
449
- // Backfill first — before the database closes and the libp2p node stops — so
450
- // no NEW catch-up push is issued against a torn-down transport. A push already
451
- // in flight is not awaited; it fails into the module's own per-chunk catch.
452
- const backfill = this.backfills.get(instance.strandId);
453
- if (backfill) {
454
- backfill.stop();
455
- this.backfills.delete(instance.strandId);
456
- }
457
- if (instance.database) {
458
- await instance.database.close();
459
- instance.database = undefined;
460
- }
461
- if (instance.libp2pNode) {
462
- await instance.libp2pNode.stop();
463
- instance.libp2pNode = undefined;
464
- }
465
- instance.connectedPeers = 0;
466
- }
467
-
468
- /**
469
- * Drop the strand's owned store and release its cache registration with the shared
470
- * pool. Called only where the strand's whole lifetime ends (`stopStrand`, and the
471
- * failed-launch rollback) — never on a quiesce.
472
- *
473
- * A dispose failure is logged, not thrown: the store is already unreferenced here,
474
- * and failing the stop over a cache-bookkeeping error would leave the caller unable
475
- * to tear the strand down. `disposeStorageCache` no-ops for an unwrapped store
476
- * (e.g. `MemoryRawStorage`), so no instanceof test is needed.
477
- */
478
- private async disposeStrandStorage(strandId: string): Promise<void> {
479
- const storage = this.strandStorages.get(strandId);
480
- if (!storage) {
481
- return;
482
- }
483
- this.strandStorages.delete(strandId);
484
- try {
485
- await disposeStorageCache(storage);
486
- } catch (error) {
487
- log('Failed to dispose storage cache for strand %s: %o', strandId, error);
488
- }
489
- }
490
-
491
- /**
492
- * Quiesce a strand: release its strand-network resources (stop the libp2p node,
493
- * close the StrandDatabase) while RETAINING the instance record — identity,
494
- * sAppInfo, keys, latency hint, metadata — and its launch config so it can be
495
- * resumed later. Mechanically this is `stopStrand` minus the instance/config
496
- * deletion. The caller sets the post-quiesce status (e.g. `hibernating`).
497
- * No-ops when the strand is missing or already quiesced.
498
- */
499
- async quiesceStrand(strandId: string): Promise<void> {
500
- const instance = this.instances.get(strandId);
501
- if (!instance) {
502
- log('quiesceStrand: strand %s not found', strandId);
503
- return;
504
- }
505
- if (!instance.libp2pNode && !instance.database) {
506
- log('quiesceStrand: strand %s already quiesced', strandId);
507
- return;
508
- }
509
-
510
- log('Quiescing strand instance: %s', strandId);
511
- await this.releaseRuntime(instance);
512
- log('Strand %s quiesced (resources released, instance retained)', strandId);
513
- }
514
-
515
- /**
516
- * Resume a previously-quiesced strand: rebuild its libp2p node + StrandDatabase
517
- * from the retained launch config and re-attach them, transitioning it back to
518
- * `active`. `overrides` re-applies volatile inputs that may have changed since
519
- * launch (the cohort `bootstrapNodes` seed) and updates the retained config so
520
- * a later resume reuses the latest values. Returns the live instance unchanged
521
- * if it is already running.
522
- */
523
- async resumeStrand(strandId: string, overrides?: ResumeStrandOverrides): Promise<StrandInstance> {
524
- if (this.stopping) {
525
- throw new Error('StrandInstanceManager is stopping');
526
- }
527
- const instance = this.instances.get(strandId);
528
- if (!instance) {
529
- throw new Error(`Cannot resume strand ${strandId}: not tracked`);
530
- }
531
- const launchConfig = this.launchConfigs.get(strandId);
532
- if (!launchConfig) {
533
- throw new Error(`Cannot resume strand ${strandId}: no retained launch config`);
534
- }
535
- if (instance.libp2pNode || instance.database) {
536
- log('resumeStrand: strand %s already live', strandId);
537
- return instance;
538
- }
539
-
540
- log('Resuming strand instance: %s', strandId);
541
- const tTotal = performance.now();
542
-
543
- // Re-apply volatile inputs and persist them so a subsequent resume reuses them.
544
- const resumeConfig: StartStrandConfig = {
545
- ...launchConfig,
546
- bootstrapNodes: overrides?.bootstrapNodes ?? launchConfig.bootstrapNodes
547
- };
548
- this.launchConfigs.set(strandId, resumeConfig);
549
-
550
- instance.status = 'starting';
551
- try {
552
- await this.buildStrandRuntime(instance, resumeConfig);
553
- timing('[resumeStrand:%s] total: %dms', strandId, Math.round(performance.now() - tTotal));
554
- log('Strand %s resumed successfully', strandId);
555
- return instance;
556
- } catch (error) {
557
- instance.status = 'error';
558
- instance.error = error instanceof Error ? error.message : String(error);
559
- log('Failed to resume strand %s: %s', strandId, instance.error);
560
- throw error;
561
- }
562
- }
563
-
564
- /**
565
- * Stop a strand instance
566
- */
567
- async stopStrand(strandId: string): Promise<void> {
568
- const instance = this.instances.get(strandId);
569
- if (!instance) {
570
- log('Strand %s not found', strandId);
571
- return;
572
- }
573
-
574
- log('Stopping strand instance: %s', strandId);
575
- instance.status = 'stopping';
576
-
577
- try {
578
- await this.releaseRuntime(instance);
579
- instance.status = 'stopped';
580
- this.instances.delete(strandId);
581
- this.launchConfigs.delete(strandId);
582
- // Storage is released only here — NOT in releaseRuntime, which a quiesce shares.
583
- await this.disposeStrandStorage(strandId);
584
- log('Strand %s stopped successfully', strandId);
585
- } catch (error) {
586
- instance.status = 'error';
587
- instance.error = error instanceof Error ? error.message : String(error);
588
- log('Error stopping strand %s: %s', strandId, instance.error);
589
- throw error;
590
- }
591
- }
592
-
593
- /**
594
- * Stop all strand instances
595
- */
596
- async stopAll(): Promise<void> {
597
- this.stopping = true;
598
- log('Stopping all strand instances (%d)', this.instances.size);
599
-
600
- const stopPromises = Array.from(this.instances.keys()).map(id =>
601
- this.stopStrand(id).catch(err => {
602
- log('Error stopping strand %s during shutdown: %s', id, err);
603
- })
604
- );
605
-
606
- await Promise.all(stopPromises);
607
- this.stopping = false;
608
- log('All strand instances stopped');
609
- }
610
- }
611
-
1
+ import debug from 'debug';
2
+ import type { PrivateKey } from '@libp2p/interface';
3
+ import { createLibp2pNode, type IRawStorage } from '@optimystic/db-p2p';
4
+ import { wrapStorageWithCache, disposeStorageCache } from '@serfab/quereus-plugin-sereus';
5
+ import { StrandDatabase } from './strand-database.js';
6
+ import { StrandBackfill, type StrandBackfillConfig } from './strand-backfill.js';
7
+ import { assertSchemaSignature } from './schema-verification.js';
8
+ import type {
9
+ StrandInstance,
10
+ StrandRow,
11
+ StorageConfig,
12
+ NetworkConfig,
13
+ LatencyHint,
14
+ NodeProfile,
15
+ SAppConfig,
16
+ SAppInfo,
17
+ RawStorageProvider,
18
+ Libp2pNodeWithRepo
19
+ } from './types.js';
20
+ import { resolveStrandClusterSize, STRAND_CLUSTER_POLICY } from './types.js';
21
+ import { resolveListenAddrs } from './relay-addrs.js';
22
+ import { resolveAnnounceAddrs } from './announce-addrs.js';
23
+
24
+ const log = debug('sereus:cadre:strand-manager');
25
+ const timing = debug('sereus:cadre:timing');
26
+
27
+ /**
28
+ * Configuration for starting a strand instance
29
+ */
30
+ export interface StartStrandConfig {
31
+ strandRow: StrandRow;
32
+ /** sApp configuration provided by the hosting application */
33
+ sAppConfig: SAppConfig;
34
+ storage?: StorageConfig;
35
+ network?: NetworkConfig;
36
+ profile: NodeProfile;
37
+ defaultLatencyHint: LatencyHint;
38
+ privateKey?: PrivateKey;
39
+ /** Cohort-derived discovery seed (multiaddr strings). Defaults to [] when omitted. */
40
+ bootstrapNodes?: string[];
41
+ /**
42
+ * Require a valid author signature on the sApp schema before bring-up.
43
+ * Defaults to true (fail closed) when omitted; set false only for dev/test
44
+ * with unsigned demo schemas. Mirrors {@link CadreNodeConfig.requireSignedSchemas}.
45
+ */
46
+ requireSignedSchemas?: boolean;
47
+ /**
48
+ * Whether this node founds the strand (vs. joins it). Forwarded to the
49
+ * StrandDatabase so the founder bootstrap (Header, founding Member/Manager)
50
+ * runs once at bring-up. Joiners leave this unset and write nothing. See
51
+ * {@link StrandConfig.founder}.
52
+ */
53
+ founder?: boolean;
54
+ /**
55
+ * Number of nodes Optimystic is told this strand's replication cluster should
56
+ * have. Same rule as {@link CadreNodeConfig.strandClusterSize}, which CadreNode
57
+ * forwards here: every node on the strand should use the same value, and it is
58
+ * frozen when the strand's libp2p node is created. Defaults to
59
+ * `DEFAULT_STRAND_CLUSTER_SIZE` (4); values below `MIN_CLUSTER_SIZE` (2) are
60
+ * rejected by `resolveStrandClusterSize`. Strand-only — the
61
+ * control network's breadth is the fixed `CONTROL_REPLICATION_BREADTH`.
62
+ */
63
+ clusterSize?: number;
64
+ /**
65
+ * Tuning for the strand peer-join block catch-up ({@link StrandBackfill}),
66
+ * forwarded from {@link CadreNodeConfig.strandBackfill}. When the strand's
67
+ * libp2p node connects to a peer this runtime has not yet caught up, every
68
+ * block in the strand's own raw store is pushed to it. Runs only on strands
69
+ * with per-strand storage; `{ enabled: false }` restores the pre-existing
70
+ * no-backfill behaviour.
71
+ */
72
+ backfill?: StrandBackfillConfig;
73
+ }
74
+
75
+ /**
76
+ * Volatile inputs re-resolved when resuming a quiesced strand. The cohort
77
+ * discovery seed is the only one — it grows as peers are learned since the
78
+ * strand first launched.
79
+ */
80
+ export interface ResumeStrandOverrides {
81
+ /** Freshly-resolved cohort discovery seed (multiaddr strings). */
82
+ bootstrapNodes?: string[];
83
+ }
84
+
85
+ /**
86
+ * Get the isolated storage path for a specific strand.
87
+ *
88
+ * @deprecated This helper is Node-only and throws in React Native (it assumes a
89
+ * filesystem layout). Use a storage provider factory function instead, which
90
+ * receives the strandId and can create strand-specific storage paths using
91
+ * platform-appropriate methods.
92
+ *
93
+ * @example
94
+ * // Instead of using getStrandStoragePath, use a storage provider factory:
95
+ * const storage = {
96
+ * provider: (strandId: string) => new FileRawStorage(`./data/strands/${strandId}`)
97
+ * };
98
+ */
99
+ export function getStrandStoragePath(basePath: string, strandId: string): string {
100
+ // Check if we're in a Node.js environment
101
+ if (typeof process === 'undefined' || !process.versions?.node) {
102
+ throw new Error(
103
+ 'getStrandStoragePath is not available in React Native. ' +
104
+ 'Use a storage provider factory function instead.'
105
+ );
106
+ }
107
+
108
+ // Sanitize strandId for filesystem safety (UUIDs should be safe, but just in case)
109
+ const safeId = strandId.replace(/[^a-zA-Z0-9-]/g, '_');
110
+
111
+ // Build the path with plain string joins rather than the Node `path` module.
112
+ // A static `require('path')` forces RN bundlers (e.g. Metro) to *resolve* the
113
+ // module at bundle time even though this Node-only helper throws above before
114
+ // ever reaching here — joining by hand keeps the module free of any Node
115
+ // built-in reference, so RN bundles need no `path` shim.
116
+ const trimmedBase = basePath.replace(/[\\/]+$/, '');
117
+ return `${trimmedBase}/strands/${safeId}`;
118
+ }
119
+
120
+ /**
121
+ * Resolve a storage provider for a specific strand.
122
+ * If the provider is a factory function, call it with the strandId.
123
+ *
124
+ * Called only from {@link StrandInstanceManager.startStrand}, which owns the result
125
+ * for the instance's lifetime — see `strandStorages`.
126
+ *
127
+ * @param provider - Storage provider (instance or factory)
128
+ * @param strandId - The strand ID to create storage for
129
+ * @returns The resolved IRawStorage instance, or undefined if no provider
130
+ */
131
+ function resolveStrandStorage(
132
+ provider: RawStorageProvider | undefined,
133
+ strandId: string
134
+ ): IRawStorage | undefined {
135
+ if (!provider) {
136
+ return undefined;
137
+ }
138
+ const storage = typeof provider === 'function' ? provider(strandId) : provider;
139
+ // Wrapped in the write-through raw-storage cache (quereus-plugin-sereus's cached-storage.ts).
140
+ // Called ONCE per strand launch — `startStrand` keeps the result for the instance's
141
+ // lifetime — so the wrap survives every runtime rebuild the instance goes through.
142
+ return wrapStorageWithCache(storage, strandId);
143
+ }
144
+
145
+ /**
146
+ * Manages individual strand instances - creates and destroys isolated libp2p nodes
147
+ * for each strand the cadre participates in.
148
+ */
149
+ export class StrandInstanceManager {
150
+ private instances: Map<string, StrandInstance> = new Map();
151
+ /**
152
+ * Retained launch config per strand, captured in `startStrand` and cleared in
153
+ * `stopStrand`. `resumeStrand` reuses it to rebuild a quiesced strand's runtime
154
+ * without the caller re-threading storage/network/profile/key/sApp config.
155
+ */
156
+ private launchConfigs: Map<string, StartStrandConfig> = new Map();
157
+ /**
158
+ * The per-strand peer-join block catch-up, keyed by strand id. Private (not on
159
+ * the public {@link StrandInstance}) because it is runtime plumbing with the
160
+ * same lifetime as the strand's libp2p node: created in `buildStrandRuntime`,
161
+ * stopped and dropped in `releaseRuntime` — so quiesce → resume rebuilds it
162
+ * with a fresh caught-up-peer memo, which is intended (a resumed node may have
163
+ * missed writes).
164
+ */
165
+ private backfills: Map<string, StrandBackfill> = new Map();
166
+ /**
167
+ * The resolved (cache-wrapped) raw storage per strand id — the instance's OWN
168
+ * store, resolved once in `startStrand` and held until `stopStrand` disposes it.
169
+ * Private for the same reason `backfills` is: runtime plumbing, not part of the
170
+ * public {@link StrandInstance}.
171
+ *
172
+ * Deliberately NOT touched by `releaseRuntime`: that is what lets a quiesce →
173
+ * resume cycle rebuild the libp2p node over the SAME store, keeping its
174
+ * write-through cache warm (and, on an in-memory backend, keeping the strand's
175
+ * blocks at all). An entry exists iff `instances` does AND the launch config
176
+ * supplied a storage provider.
177
+ *
178
+ * NOTE: a hibernating strand therefore keeps its store — and its share of the
179
+ * process-wide cache pool — resident for as long as the instance is tracked. That
180
+ * is the point (a warm wake), and the pool evicts under pressure, so the cost is
181
+ * one map entry per hibernating strand today. If a device ever hibernates strands
182
+ * by the hundred, revisit: dropping the store at quiesce and paying for a cold
183
+ * wake becomes the better trade.
184
+ */
185
+ private strandStorages: Map<string, IRawStorage> = new Map();
186
+ private stopping = false;
187
+
188
+ constructor() {
189
+ log('StrandInstanceManager created');
190
+ }
191
+
192
+ /**
193
+ * Get all current strand instances
194
+ */
195
+ getInstances(): Map<string, StrandInstance> {
196
+ return new Map(this.instances);
197
+ }
198
+
199
+ /**
200
+ * Get a specific strand instance
201
+ */
202
+ getInstance(strandId: string): StrandInstance | undefined {
203
+ return this.instances.get(strandId);
204
+ }
205
+
206
+ /**
207
+ * Check if a strand is currently running
208
+ */
209
+ hasStrand(strandId: string): boolean {
210
+ return this.instances.has(strandId);
211
+ }
212
+
213
+ /**
214
+ * Start a new strand instance.
215
+ *
216
+ * A failed launch leaves NOTHING tracked: the instance and its retained launch
217
+ * config are both dropped before the error is rethrown, so the strand id is
218
+ * free for a genuine retry. This matches the pre-registration failure path
219
+ * (a rejected schema signature, which throws before anything is recorded) —
220
+ * both failure modes of this call leave the same residue: none. Callers learn
221
+ * of the failure from the rejected promise (and, on the control-discovered
222
+ * path, from CadreNode's `strand:error` event), not from an error record left
223
+ * behind in `instances`.
224
+ *
225
+ * The strand's raw storage is resolved HERE, once, and held for the instance's
226
+ * lifetime (see `strandStorages`) — `buildStrandRuntime` only reads it, so a
227
+ * hibernation wake never re-enters the embedder's provider.
228
+ */
229
+ async startStrand(config: StartStrandConfig): Promise<StrandInstance> {
230
+ const { strandRow, sAppConfig } = config;
231
+ const strandId = strandRow.Id;
232
+
233
+ if (this.stopping) {
234
+ throw new Error('StrandInstanceManager is stopping');
235
+ }
236
+
237
+ if (this.instances.has(strandId)) {
238
+ log('Strand %s already running', strandId);
239
+ return this.instances.get(strandId)!;
240
+ }
241
+
242
+ log('Starting strand instance: %s (sApp: %s v%s)', strandId, sAppConfig.id, sAppConfig.version);
243
+ const tTotal = performance.now();
244
+
245
+ // Verify schema signature before proceeding (fail-closed by default)
246
+ const requireSignature = config.requireSignedSchemas ?? true;
247
+ assertSchemaSignature(sAppConfig, { requireSignature });
248
+ log('Strand %s sApp schema signature verified (author: %s)', strandId, sAppConfig.id);
249
+
250
+ // Convert SAppConfig to SAppInfo for the instance
251
+ const sAppInfo: SAppInfo = {
252
+ id: sAppConfig.id,
253
+ version: sAppConfig.version,
254
+ schema: sAppConfig.schema,
255
+ signature: sAppConfig.signature
256
+ };
257
+
258
+ // Resolve this strand's storage ONCE, before anything is recorded. If a factory
259
+ // function is provided, it is called with the strandId to create strand-specific
260
+ // storage (e.g. strand-isolated directories). A provider that throws therefore
261
+ // fails the launch alongside the schema-signature check, leaving nothing tracked.
262
+ const strandStorage = resolveStrandStorage(config.storage?.provider, strandId);
263
+ if (strandStorage) {
264
+ log('Strand %s using provided storage provider', strandId);
265
+ }
266
+
267
+ // Determine latency hint: sApp config > default
268
+ const latencyHint = sAppConfig.latencyHint ?? config.defaultLatencyHint;
269
+
270
+ const instance: StrandInstance = {
271
+ strandId,
272
+ status: 'starting',
273
+ sAppInfo,
274
+ memberPrivateKey: strandRow.MemberPrivateKey ?? undefined,
275
+ connectedPeers: 0,
276
+ lastActivity: new Date(),
277
+ latencyHint
278
+ };
279
+
280
+ this.instances.set(strandId, instance);
281
+ this.launchConfigs.set(strandId, config);
282
+ if (strandStorage) {
283
+ this.strandStorages.set(strandId, strandStorage);
284
+ }
285
+
286
+ try {
287
+ await this.buildStrandRuntime(instance, config);
288
+ timing('[startStrand:%s] total: %dms', strandId, Math.round(performance.now() - tTotal));
289
+ log('Strand %s started successfully with sApp %s', strandId, sAppConfig.id);
290
+ return instance;
291
+ } catch (error) {
292
+ // Status/error first — the (now discarded) record is still what `log` reports on.
293
+ instance.status = 'error';
294
+ instance.error = error instanceof Error ? error.message : String(error);
295
+ log('Failed to start strand %s: %s', strandId, instance.error);
296
+ // Drop the dead record so this strand id can be launched again. Keep the
297
+ // `launchConfigs` has an entry iff `instances` does invariant — resumeStrand
298
+ // reads both, and a config without an instance would strand the config.
299
+ this.instances.delete(strandId);
300
+ this.launchConfigs.delete(strandId);
301
+ // The store this launch resolved goes with it: nothing owns it any more, and a
302
+ // retained cache wrapper would be handed back (already retired) on a retry.
303
+ await this.disposeStrandStorage(strandId);
304
+ throw error;
305
+ }
306
+ }
307
+
308
+ /**
309
+ * Build (or rebuild) the libp2p node + StrandDatabase for an instance and
310
+ * attach them, transitioning it to `active`. Shared by `startStrand` (fresh
311
+ * launch) and `resumeStrand` (rehydrating a quiesced instance). Reads all
312
+ * volatile inputs (bootstrapNodes, network, profile, privateKey, sApp config)
313
+ * from `config`, so the caller controls the cohort-derived values. Storage is the
314
+ * one input it does NOT re-read from `config`: that belongs to the instance and
315
+ * comes from `strandStorages`.
316
+ */
317
+ private async buildStrandRuntime(instance: StrandInstance, config: StartStrandConfig): Promise<void> {
318
+ const strandId = instance.strandId;
319
+ const { sAppConfig } = config;
320
+
321
+ // The store the instance OWNS (resolved once in `startStrand`), not a fresh
322
+ // resolution: a rebuild must reach the same backend through the same warm cache.
323
+ const strandStorage = this.strandStorages.get(strandId);
324
+
325
+ // db-p2p namespaces every one of the node's protocol ids by network name
326
+ // (`/optimystic/<networkName>/...`), so anything dialing this node's own
327
+ // services must derive its prefix from the SAME string the node was built
328
+ // with — hence one binding for both, not two literals that can drift.
329
+ const networkName = `strand-${strandId}`;
330
+ const protocolPrefix = `/optimystic/${networkName}`;
331
+
332
+ // Determine relay mode: if explicitly set in config, use that;
333
+ // otherwise default to true for storage profile nodes.
334
+ const enableRelay = config.network?.enableRelay ?? (config.profile === 'storage');
335
+ // The CONFIGURED relay route (the default), deliberately NOT the control node's
336
+ // `'search'` route: nothing drives an explicit reservation for a strand node, so
337
+ // a bare `/p2p-circuit` search entry would register a pending reservation that
338
+ // never gets filled and leave the strand node undialable. The ordering hazard
339
+ // that pushed the control node onto the search route does not exist here — a
340
+ // strand node's protocol ids are namespaced `/optimystic/strand-<id>/…`, so a
341
+ // relay dialed from inside `libp2p.start()` is never in the strand's cohort and
342
+ // cannot refuse its database bring-up. See `relay-addrs.ts`.
343
+ const listenAddrs = resolveListenAddrs(config.network);
344
+
345
+ try {
346
+ let t0 = performance.now();
347
+ const node = await createLibp2pNode({
348
+ port: 0, // Random port
349
+ bootstrapNodes: config.bootstrapNodes ?? [],
350
+ networkName,
351
+ storage: strandStorage,
352
+ fretProfile: config.profile === 'storage' ? 'core' : 'edge',
353
+ relay: enableRelay,
354
+ clusterSize: resolveStrandClusterSize(config.clusterSize),
355
+ // Deliberately NOT CONTROL_CLUSTER_POLICY: a strand is application data with its own
356
+ // breadth reasoning, and the shape match with the control policy is a coincidence.
357
+ clusterPolicy: STRAND_CLUSTER_POLICY,
358
+ arachnode: {
359
+ enableRingZulu: config.profile === 'storage'
360
+ },
361
+ ...(config.privateKey && { privateKey: config.privateKey }),
362
+ ...(config.network?.transports && { transports: config.network.transports }),
363
+ // NOTE: strand nodes receive the same RESOLVED listen addrs as the
364
+ // control node. A fixed-port listen addr (e.g. cadre-cli's example
365
+ // `/ip4/0.0.0.0/tcp/4001`) would have control + strand nodes racing to
366
+ // bind one port — EADDRINUSE. Unverified; if a deployment configures a
367
+ // fixed port and strands fail to start, rewrite the port per node here.
368
+ // An inherited configured `/p2p-circuit` addr, by contrast, is deliberate —
369
+ // it is what gives a NAT'd strand node a reachable relay slot — and works
370
+ // because the launch path announces this strand's derived peerId to
371
+ // the relay first (delegate admission; see cadre-node.ts). Those circuit
372
+ // entries come either from a hand-written `network.listenAddrs` or from
373
+ // `network.relayAddrs`, which `resolveListenAddrs` folds into the same list
374
+ // on the configured route this call takes (see the comment above it).
375
+ ...(listenAddrs && { listenAddrs }),
376
+ // A strand node behind a reverse proxy has the same advertising need as the
377
+ // control node, and inherits the same `NetworkConfig` — so it announces the same
378
+ // addresses.
379
+ //
380
+ // NOTE: an announce addr naming a PORT therefore names the control node's port,
381
+ // not this strand node's — the same second-node-on-one-host problem the listen
382
+ // NOTE above describes, and the reason both belong to one fix. It is currently
383
+ // unreachable: naming a fixed public port means configuring a fixed
384
+ // `listenAddrs` port, and that collides at bind time before any strand node gets
385
+ // far enough to advertise anything. Whatever gives strand nodes their own listen
386
+ // port has to give them their own announce addrs in the same pass, or a strand
387
+ // will advertise an address that reaches the control node. Tracked as an arm of
388
+ // `tickets/backlog/strand-network-nat-relay-reachability.md`.
389
+ ...resolveAnnounceAddrs(config.network),
390
+ ...(config.network?.connectionGater && { connectionGater: config.network.connectionGater })
391
+ }) as Libp2pNodeWithRepo;
392
+ timing('[buildStrandRuntime:%s] createLibp2pNode: %dms', strandId, Math.round(performance.now() - t0));
393
+
394
+ instance.libp2pNode = node;
395
+
396
+ // Create and initialize the StrandDatabase.
397
+ //
398
+ // Attach before initialize so a failed init is cleaned up by
399
+ // releaseRuntime below (close() is safe on a partially-initialized db).
400
+ t0 = performance.now();
401
+ const strandDb = new StrandDatabase({
402
+ strandId,
403
+ sAppConfig,
404
+ libp2pNode: node,
405
+ coordinatedRepo: node.coordinatedRepo,
406
+ // Founder bootstrap inputs: the strand's type drives which membership rows
407
+ // are written, and the closed-strand MemberPrivateKey derives the founding
408
+ // Member/Manager key. Both come off the control-network strand row.
409
+ strandType: config.strandRow.Type,
410
+ memberPrivateKey: config.strandRow.MemberPrivateKey ?? undefined,
411
+ founder: config.founder
412
+ });
413
+ instance.database = strandDb;
414
+ await strandDb.initialize();
415
+ timing('[buildStrandRuntime:%s] strandDatabase.initialize: %dms', strandId, Math.round(performance.now() - t0));
416
+
417
+ // Peer-join block catch-up: push this strand's own blocks to each newly
418
+ // connected peer, so a machine that joined after blocks were committed
419
+ // still ends up physically holding them (without per-strand storage there
420
+ // is nothing to copy). Armed for EVERY stored strand: `StrandBackfill`
421
+ // only does work when the strand's libp2p node reports a peer connection,
422
+ // so on a device that is genuinely alone it is inert, and arming it at
423
+ // launch is what closes the "founded alone, never replicates" hole — a
424
+ // peer that joins later gets the founder's blocks without any relaunch.
425
+ //
426
+ // NOTE: cost is one StrandBackfill object + one `connection:open` listener
427
+ // per running strand — linear in strand count, negligible at the handful a
428
+ // device or host runs today. If a node ever hosts strands by the hundred,
429
+ // move to one shared listener that dispatches by strand id.
430
+ if (strandStorage && config.backfill?.enabled !== false) {
431
+ if (node.keyNetwork) {
432
+ const backfill = new StrandBackfill({
433
+ strandId,
434
+ libp2p: node,
435
+ peerNetwork: node.keyNetwork,
436
+ storage: strandStorage,
437
+ // The same prefix the receiver registered its block-transfer handler
438
+ // under — derived from networkName above, never re-spelled here.
439
+ protocolPrefix
440
+ }, config.backfill);
441
+ backfill.start();
442
+ this.backfills.set(strandId, backfill);
443
+ } else {
444
+ log('Strand %s: libp2p node exposes no keyNetwork; peer-join block catch-up is inert', strandId);
445
+ }
446
+ }
447
+
448
+ instance.status = 'active';
449
+ instance.lastActivity = new Date();
450
+ } catch (error) {
451
+ // Roll back any partially-attached runtime so the instance is left with
452
+ // NEITHER handle. Otherwise the `libp2pNode || database` "already live"
453
+ // guard in resumeStrand/handleStrandWake would treat a half-built strand
454
+ // as healthy — leaking the libp2p node and never retrying the rebuild.
455
+ await this.releaseRuntime(instance).catch((cleanupErr) => {
456
+ log('buildStrandRuntime cleanup for strand %s also failed: %o', strandId, cleanupErr);
457
+ });
458
+ throw error;
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Release an instance's strand-network runtime: close the StrandDatabase, then
464
+ * stop the libp2p node (construction order in reverse), clearing both fields
465
+ * and zeroing connectedPeers. Tolerant of partially-built state — either handle
466
+ * may be absent — so it doubles as rollback for a failed `buildStrandRuntime`.
467
+ * Shared by `quiesceStrand`, `stopStrand`, and that rollback path.
468
+ *
469
+ * Leaves `strandStorages` untouched by design — the store outlives the runtime it
470
+ * was built into, which is what makes a resume warm. Only `stopStrand` disposes it.
471
+ */
472
+ private async releaseRuntime(instance: StrandInstance): Promise<void> {
473
+ // Backfill first — before the database closes and the libp2p node stops — so
474
+ // no NEW catch-up push is issued against a torn-down transport. A push already
475
+ // in flight is not awaited; it fails into the module's own per-chunk catch.
476
+ const backfill = this.backfills.get(instance.strandId);
477
+ if (backfill) {
478
+ backfill.stop();
479
+ this.backfills.delete(instance.strandId);
480
+ }
481
+ if (instance.database) {
482
+ await instance.database.close();
483
+ instance.database = undefined;
484
+ }
485
+ if (instance.libp2pNode) {
486
+ await instance.libp2pNode.stop();
487
+ instance.libp2pNode = undefined;
488
+ }
489
+ instance.connectedPeers = 0;
490
+ }
491
+
492
+ /**
493
+ * Drop the strand's owned store and release this strand's claim on its cache. The
494
+ * wrapper counts holders, so the cache is emptied and unregistered from the shared
495
+ * pool only if no other scope still holds it. Called only where the strand's whole
496
+ * lifetime ends (`stopStrand`, and the failed-launch rollback) — never on a quiesce.
497
+ *
498
+ * A dispose failure is logged, not thrown: the store is already unreferenced here,
499
+ * and failing the stop over a cache-bookkeeping error would leave the caller unable
500
+ * to tear the strand down. `disposeStorageCache` no-ops for an unwrapped store
501
+ * (e.g. `MemoryRawStorage`), so no instanceof test is needed.
502
+ */
503
+ private async disposeStrandStorage(strandId: string): Promise<void> {
504
+ const storage = this.strandStorages.get(strandId);
505
+ if (!storage) {
506
+ return;
507
+ }
508
+ this.strandStorages.delete(strandId);
509
+ try {
510
+ await disposeStorageCache(storage);
511
+ } catch (error) {
512
+ log('Failed to dispose storage cache for strand %s: %o', strandId, error);
513
+ }
514
+ }
515
+
516
+ /**
517
+ * Quiesce a strand: release its strand-network resources (stop the libp2p node,
518
+ * close the StrandDatabase) while RETAINING the instance record — identity,
519
+ * sAppInfo, keys, latency hint, metadata — and its launch config so it can be
520
+ * resumed later. Mechanically this is `stopStrand` minus the instance/config
521
+ * deletion. The caller sets the post-quiesce status (e.g. `hibernating`).
522
+ * No-ops when the strand is missing or already quiesced.
523
+ */
524
+ async quiesceStrand(strandId: string): Promise<void> {
525
+ const instance = this.instances.get(strandId);
526
+ if (!instance) {
527
+ log('quiesceStrand: strand %s not found', strandId);
528
+ return;
529
+ }
530
+ if (!instance.libp2pNode && !instance.database) {
531
+ log('quiesceStrand: strand %s already quiesced', strandId);
532
+ return;
533
+ }
534
+
535
+ log('Quiescing strand instance: %s', strandId);
536
+ await this.releaseRuntime(instance);
537
+ log('Strand %s quiesced (resources released, instance retained)', strandId);
538
+ }
539
+
540
+ /**
541
+ * Resume a previously-quiesced strand: rebuild its libp2p node + StrandDatabase
542
+ * from the retained launch config and re-attach them, transitioning it back to
543
+ * `active`. `overrides` re-applies volatile inputs that may have changed since
544
+ * launch (the cohort `bootstrapNodes` seed) and updates the retained config so
545
+ * a later resume reuses the latest values. Returns the live instance unchanged
546
+ * if it is already running.
547
+ */
548
+ async resumeStrand(strandId: string, overrides?: ResumeStrandOverrides): Promise<StrandInstance> {
549
+ if (this.stopping) {
550
+ throw new Error('StrandInstanceManager is stopping');
551
+ }
552
+ const instance = this.instances.get(strandId);
553
+ if (!instance) {
554
+ throw new Error(`Cannot resume strand ${strandId}: not tracked`);
555
+ }
556
+ const launchConfig = this.launchConfigs.get(strandId);
557
+ if (!launchConfig) {
558
+ throw new Error(`Cannot resume strand ${strandId}: no retained launch config`);
559
+ }
560
+ if (instance.libp2pNode || instance.database) {
561
+ log('resumeStrand: strand %s already live', strandId);
562
+ return instance;
563
+ }
564
+
565
+ log('Resuming strand instance: %s', strandId);
566
+ const tTotal = performance.now();
567
+
568
+ // Re-apply volatile inputs and persist them so a subsequent resume reuses them.
569
+ const resumeConfig: StartStrandConfig = {
570
+ ...launchConfig,
571
+ bootstrapNodes: overrides?.bootstrapNodes ?? launchConfig.bootstrapNodes
572
+ };
573
+ this.launchConfigs.set(strandId, resumeConfig);
574
+
575
+ instance.status = 'starting';
576
+ try {
577
+ await this.buildStrandRuntime(instance, resumeConfig);
578
+ timing('[resumeStrand:%s] total: %dms', strandId, Math.round(performance.now() - tTotal));
579
+ log('Strand %s resumed successfully', strandId);
580
+ return instance;
581
+ } catch (error) {
582
+ instance.status = 'error';
583
+ instance.error = error instanceof Error ? error.message : String(error);
584
+ log('Failed to resume strand %s: %s', strandId, instance.error);
585
+ throw error;
586
+ }
587
+ }
588
+
589
+ /**
590
+ * Stop a strand instance
591
+ */
592
+ async stopStrand(strandId: string): Promise<void> {
593
+ const instance = this.instances.get(strandId);
594
+ if (!instance) {
595
+ log('Strand %s not found', strandId);
596
+ return;
597
+ }
598
+
599
+ log('Stopping strand instance: %s', strandId);
600
+ instance.status = 'stopping';
601
+
602
+ try {
603
+ await this.releaseRuntime(instance);
604
+ instance.status = 'stopped';
605
+ this.instances.delete(strandId);
606
+ this.launchConfigs.delete(strandId);
607
+ // Storage is released only here — NOT in releaseRuntime, which a quiesce shares.
608
+ await this.disposeStrandStorage(strandId);
609
+ log('Strand %s stopped successfully', strandId);
610
+ } catch (error) {
611
+ instance.status = 'error';
612
+ instance.error = error instanceof Error ? error.message : String(error);
613
+ log('Error stopping strand %s: %s', strandId, instance.error);
614
+ throw error;
615
+ }
616
+ }
617
+
618
+ /**
619
+ * Stop all strand instances
620
+ */
621
+ async stopAll(): Promise<void> {
622
+ this.stopping = true;
623
+ log('Stopping all strand instances (%d)', this.instances.size);
624
+
625
+ const stopPromises = Array.from(this.instances.keys()).map(id =>
626
+ this.stopStrand(id).catch(err => {
627
+ log('Error stopping strand %s during shutdown: %s', id, err);
628
+ })
629
+ );
630
+
631
+ await Promise.all(stopPromises);
632
+ this.stopping = false;
633
+ log('All strand instances stopped');
634
+ }
635
+ }
636
+