@serfab/cadre-core 0.10.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 (145) hide show
  1. package/README.md +255 -255
  2. package/dist/announce-addrs.d.ts +49 -0
  3. package/dist/announce-addrs.js +78 -0
  4. package/dist/announce-addrs.js.map +1 -0
  5. package/dist/cadre-node.d.ts +484 -58
  6. package/dist/cadre-node.js +953 -182
  7. package/dist/cadre-node.js.map +1 -1
  8. package/dist/control-cohort.d.ts +26 -0
  9. package/dist/control-cohort.js +26 -0
  10. package/dist/control-cohort.js.map +1 -1
  11. package/dist/control-database.d.ts +222 -125
  12. package/dist/control-database.js +356 -266
  13. package/dist/control-database.js.map +1 -1
  14. package/dist/control-formation-recorder.d.ts +8 -7
  15. package/dist/control-formation-recorder.js +8 -7
  16. package/dist/control-formation-recorder.js.map +1 -1
  17. package/dist/control-read-retry.d.ts +103 -0
  18. package/dist/control-read-retry.js +145 -0
  19. package/dist/control-read-retry.js.map +1 -0
  20. package/dist/control-retry.d.ts +101 -0
  21. package/dist/control-retry.js +108 -0
  22. package/dist/control-retry.js.map +1 -0
  23. package/dist/control-schema.d.ts +1 -1
  24. package/dist/control-schema.js +805 -774
  25. package/dist/control-schema.js.map +1 -1
  26. package/dist/control-write-retry.d.ts +65 -31
  27. package/dist/control-write-retry.js +33 -103
  28. package/dist/control-write-retry.js.map +1 -1
  29. package/dist/delegate-admission.d.ts +17 -15
  30. package/dist/delegate-admission.js +17 -15
  31. package/dist/delegate-admission.js.map +1 -1
  32. package/dist/index.d.ts +7 -5
  33. package/dist/index.js +18 -9
  34. package/dist/index.js.map +1 -1
  35. package/dist/membership-connection-gater.d.ts +211 -25
  36. package/dist/membership-connection-gater.js +309 -29
  37. package/dist/membership-connection-gater.js.map +1 -1
  38. package/dist/peer-addr-book.d.ts +92 -0
  39. package/dist/peer-addr-book.js +220 -0
  40. package/dist/peer-addr-book.js.map +1 -0
  41. package/dist/peer-record.d.ts +26 -0
  42. package/dist/peer-record.js +32 -0
  43. package/dist/peer-record.js.map +1 -1
  44. package/dist/relay-addrs.d.ts +76 -26
  45. package/dist/relay-addrs.js +115 -27
  46. package/dist/relay-addrs.js.map +1 -1
  47. package/dist/relay-reservation.d.ts +20 -15
  48. package/dist/relay-reservation.js +18 -14
  49. package/dist/relay-reservation.js.map +1 -1
  50. package/dist/seed-bootstrap.d.ts +7 -0
  51. package/dist/seed-bootstrap.js +15 -13
  52. package/dist/seed-bootstrap.js.map +1 -1
  53. package/dist/strand-addr-protocol.js +16 -0
  54. package/dist/strand-addr-protocol.js.map +1 -1
  55. package/dist/strand-backfill.js +13 -4
  56. package/dist/strand-backfill.js.map +1 -1
  57. package/dist/strand-cohort.d.ts +4 -37
  58. package/dist/strand-cohort.js +4 -15
  59. package/dist/strand-cohort.js.map +1 -1
  60. package/dist/strand-database.d.ts +11 -16
  61. package/dist/strand-database.js +19 -7
  62. package/dist/strand-database.js.map +1 -1
  63. package/dist/strand-formation-manager.d.ts +18 -17
  64. package/dist/strand-formation-manager.js +25 -27
  65. package/dist/strand-formation-manager.js.map +1 -1
  66. package/dist/strand-formation-protocol.d.ts +7 -7
  67. package/dist/strand-formation-protocol.js +8 -8
  68. package/dist/strand-formation-protocol.js.map +1 -1
  69. package/dist/strand-instance-manager.d.ts +53 -21
  70. package/dist/strand-instance-manager.js +128 -37
  71. package/dist/strand-instance-manager.js.map +1 -1
  72. package/dist/strand-membership-writer.d.ts +113 -19
  73. package/dist/strand-membership-writer.js +219 -51
  74. package/dist/strand-membership-writer.js.map +1 -1
  75. package/dist/strand-solicitation.d.ts +3 -3
  76. package/dist/strand-solicitation.js +4 -5
  77. package/dist/strand-solicitation.js.map +1 -1
  78. package/dist/strand-wake-protocol.d.ts +47 -2
  79. package/dist/strand-wake-protocol.js +76 -6
  80. package/dist/strand-wake-protocol.js.map +1 -1
  81. package/dist/types.d.ts +107 -54
  82. package/dist/types.js.map +1 -1
  83. package/package.json +8 -7
  84. package/src/announce-addrs.ts +93 -0
  85. package/src/arachnode-stub.ts +159 -159
  86. package/src/bootstrap-peer-store-file.ts +44 -44
  87. package/src/bootstrap-peer-store.ts +172 -172
  88. package/src/cadre-node.ts +1026 -194
  89. package/src/canonical-json.ts +30 -30
  90. package/src/control-authorization.ts +114 -114
  91. package/src/control-cohort.ts +130 -103
  92. package/src/control-database.ts +376 -298
  93. package/src/control-formation-recorder.ts +305 -304
  94. package/src/control-read-retry.ts +167 -0
  95. package/src/control-retry.ts +190 -0
  96. package/src/control-schema.ts +817 -786
  97. package/src/control-stream.ts +205 -205
  98. package/src/control-write-retry.ts +383 -473
  99. package/src/delegate-admission.ts +304 -302
  100. package/src/device-token.ts +94 -94
  101. package/src/diagnostics/connection-path.ts +277 -277
  102. package/src/diagnostics/webrtc-turn-tracker.ts +222 -222
  103. package/src/ed25519-key.ts +134 -134
  104. package/src/enrollment.ts +199 -199
  105. package/src/file-durable-slot.ts +61 -61
  106. package/src/formation-approval.ts +604 -604
  107. package/src/fs-atomic.ts +107 -107
  108. package/src/hibernation-manager.ts +437 -437
  109. package/src/identity-key.ts +109 -109
  110. package/src/index.ts +435 -406
  111. package/src/key-store-file.ts +132 -132
  112. package/src/key-store.ts +99 -99
  113. package/src/membership-connection-gater.ts +368 -39
  114. package/src/node-local-snapshot.ts +260 -260
  115. package/src/peer-addr-book.ts +250 -0
  116. package/src/peer-authorization.ts +259 -259
  117. package/src/peer-record.ts +197 -164
  118. package/src/push-credentials.ts +84 -84
  119. package/src/push-node.ts +64 -64
  120. package/src/push-notifier-apns.ts +247 -247
  121. package/src/push-notifier-fcm.ts +220 -220
  122. package/src/push-notifier-shared.ts +28 -28
  123. package/src/push-notifier.ts +52 -52
  124. package/src/relay-addrs.ts +134 -27
  125. package/src/relay-reservation.ts +22 -17
  126. package/src/seed-bootstrap.ts +15 -15
  127. package/src/seed-trust-policy.ts +140 -140
  128. package/src/strand-addr-protocol.ts +458 -441
  129. package/src/strand-backfill.ts +28 -5
  130. package/src/strand-cohort.ts +38 -77
  131. package/src/strand-database.ts +202 -204
  132. package/src/strand-formation-manager.ts +26 -28
  133. package/src/strand-formation-protocol.ts +692 -692
  134. package/src/strand-instance-manager.ts +636 -553
  135. package/src/strand-member-key.ts +39 -39
  136. package/src/strand-member-registry.ts +182 -182
  137. package/src/strand-membership-writer.ts +1627 -1430
  138. package/src/strand-solicitation.ts +471 -472
  139. package/src/strand-transport-key.ts +58 -58
  140. package/src/strand-wake-payload.ts +32 -32
  141. package/src/strand-wake-protocol.ts +400 -314
  142. package/src/strand-watcher.ts +314 -314
  143. package/src/trusted-owner-store-file.ts +41 -41
  144. package/src/trusted-owner-store.ts +171 -171
  145. package/src/types.ts +1354 -1303
@@ -1,553 +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 { StrandDatabase } from './strand-database.js';
5
- import { StrandBackfill, type StrandBackfillConfig } from './strand-backfill.js';
6
- import { assertSchemaSignature } from './schema-verification.js';
7
- import type {
8
- StrandInstance,
9
- StrandRow,
10
- StorageConfig,
11
- NetworkConfig,
12
- LatencyHint,
13
- NodeProfile,
14
- SAppConfig,
15
- SAppInfo,
16
- RawStorageProvider,
17
- StrandMode,
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
- * Lifecycle mode for this strand; selects the default transactor used by the
42
- * StrandDatabase. When omitted, the StrandDatabase falls back to `'networked'`;
43
- * callers (CadreNode) infer it from cohort membership before reaching here.
44
- */
45
- mode?: StrandMode;
46
- /**
47
- * Require a valid author signature on the sApp schema before bring-up.
48
- * Defaults to true (fail closed) when omitted; set false only for dev/test
49
- * with unsigned demo schemas. Mirrors {@link CadreNodeConfig.requireSignedSchemas}.
50
- */
51
- requireSignedSchemas?: boolean;
52
- /**
53
- * Whether this node founds the strand (vs. joins it). Forwarded to the
54
- * StrandDatabase so the founder bootstrap (Header, founding Member/Manager)
55
- * runs once at bring-up. Joiners leave this unset and write nothing. See
56
- * {@link StrandConfig.founder}.
57
- */
58
- founder?: boolean;
59
- /**
60
- * Number of nodes Optimystic is told this strand's replication cluster should
61
- * have. Same rule as {@link CadreNodeConfig.strandClusterSize}, which CadreNode
62
- * forwards here: every node on the strand should use the same value, and it is
63
- * frozen when the strand's libp2p node is created. Defaults to
64
- * `DEFAULT_STRAND_CLUSTER_SIZE` (4); values below `MIN_CLUSTER_SIZE` (2) are
65
- * rejected by `resolveStrandClusterSize`. Strand-only — the
66
- * control network's breadth is the fixed `CONTROL_REPLICATION_BREADTH`.
67
- */
68
- clusterSize?: number;
69
- /**
70
- * Tuning for the strand peer-join block catch-up ({@link StrandBackfill}),
71
- * forwarded from {@link CadreNodeConfig.strandBackfill}. When the strand's
72
- * libp2p node connects to a peer this runtime has not yet caught up, every
73
- * block in the strand's own raw store is pushed to it. Runs only on
74
- * `networked` strands with per-strand storage; `{ enabled: false }` restores
75
- * the pre-existing no-backfill behaviour.
76
- */
77
- backfill?: StrandBackfillConfig;
78
- }
79
-
80
- /**
81
- * Volatile inputs re-resolved when resuming a quiesced strand. These may have
82
- * changed since the strand first launched — the cohort discovery seed grows as
83
- * peers are learned, and cohort membership can push a strand `bootstrap → networked`.
84
- */
85
- export interface ResumeStrandOverrides {
86
- /** Freshly-resolved cohort discovery seed (multiaddr strings). */
87
- bootstrapNodes?: string[];
88
- /** Freshly-resolved lifecycle mode. */
89
- mode?: StrandMode;
90
- }
91
-
92
- /**
93
- * Get the isolated storage path for a specific strand.
94
- *
95
- * @deprecated This helper is Node-only and throws in React Native (it assumes a
96
- * filesystem layout). Use a storage provider factory function instead, which
97
- * receives the strandId and can create strand-specific storage paths using
98
- * platform-appropriate methods.
99
- *
100
- * @example
101
- * // Instead of using getStrandStoragePath, use a storage provider factory:
102
- * const storage = {
103
- * provider: (strandId: string) => new FileRawStorage(`./data/strands/${strandId}`)
104
- * };
105
- */
106
- export function getStrandStoragePath(basePath: string, strandId: string): string {
107
- // Check if we're in a Node.js environment
108
- if (typeof process === 'undefined' || !process.versions?.node) {
109
- throw new Error(
110
- 'getStrandStoragePath is not available in React Native. ' +
111
- 'Use a storage provider factory function instead.'
112
- );
113
- }
114
-
115
- // Sanitize strandId for filesystem safety (UUIDs should be safe, but just in case)
116
- const safeId = strandId.replace(/[^a-zA-Z0-9-]/g, '_');
117
-
118
- // Build the path with plain string joins rather than the Node `path` module.
119
- // A static `require('path')` forces RN bundlers (e.g. Metro) to *resolve* the
120
- // module at bundle time even though this Node-only helper throws above before
121
- // ever reaching here — joining by hand keeps the module free of any Node
122
- // built-in reference, so RN bundles need no `path` shim.
123
- const trimmedBase = basePath.replace(/[\\/]+$/, '');
124
- return `${trimmedBase}/strands/${safeId}`;
125
- }
126
-
127
- /**
128
- * Resolve a storage provider for a specific strand.
129
- * If the provider is a factory function, call it with the strandId.
130
- *
131
- * @param provider - Storage provider (instance or factory)
132
- * @param strandId - The strand ID to create storage for
133
- * @returns The resolved IRawStorage instance, or undefined if no provider
134
- */
135
- function resolveStrandStorage(
136
- provider: RawStorageProvider | undefined,
137
- strandId: string
138
- ): IRawStorage | undefined {
139
- if (!provider) {
140
- return undefined;
141
- }
142
- return typeof provider === 'function' ? provider(strandId) : provider;
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
- private stopping = false;
167
-
168
- constructor() {
169
- log('StrandInstanceManager created');
170
- }
171
-
172
- /**
173
- * Get all current strand instances
174
- */
175
- getInstances(): Map<string, StrandInstance> {
176
- return new Map(this.instances);
177
- }
178
-
179
- /**
180
- * Get a specific strand instance
181
- */
182
- getInstance(strandId: string): StrandInstance | undefined {
183
- return this.instances.get(strandId);
184
- }
185
-
186
- /**
187
- * Check if a strand is currently running
188
- */
189
- hasStrand(strandId: string): boolean {
190
- return this.instances.has(strandId);
191
- }
192
-
193
- /**
194
- * Start a new strand instance.
195
- *
196
- * A failed launch leaves NOTHING tracked: the instance and its retained launch
197
- * config are both dropped before the error is rethrown, so the strand id is
198
- * free for a genuine retry. This matches the pre-registration failure path
199
- * (a rejected schema signature, which throws before anything is recorded) —
200
- * both failure modes of this call leave the same residue: none. Callers learn
201
- * of the failure from the rejected promise (and, on the control-discovered
202
- * path, from CadreNode's `strand:error` event), not from an error record left
203
- * behind in `instances`.
204
- */
205
- async startStrand(config: StartStrandConfig): Promise<StrandInstance> {
206
- const { strandRow, sAppConfig } = config;
207
- const strandId = strandRow.Id;
208
-
209
- if (this.stopping) {
210
- throw new Error('StrandInstanceManager is stopping');
211
- }
212
-
213
- if (this.instances.has(strandId)) {
214
- log('Strand %s already running', strandId);
215
- return this.instances.get(strandId)!;
216
- }
217
-
218
- log('Starting strand instance: %s (sApp: %s v%s)', strandId, sAppConfig.id, sAppConfig.version);
219
- const tTotal = performance.now();
220
-
221
- // Verify schema signature before proceeding (fail-closed by default)
222
- const requireSignature = config.requireSignedSchemas ?? true;
223
- assertSchemaSignature(sAppConfig, { requireSignature });
224
- log('Strand %s sApp schema signature verified (author: %s)', strandId, sAppConfig.id);
225
-
226
- // Convert SAppConfig to SAppInfo for the instance
227
- const sAppInfo: SAppInfo = {
228
- id: sAppConfig.id,
229
- version: sAppConfig.version,
230
- schema: sAppConfig.schema,
231
- signature: sAppConfig.signature
232
- };
233
-
234
- // Determine latency hint: sApp config > default
235
- const latencyHint = sAppConfig.latencyHint ?? config.defaultLatencyHint;
236
-
237
- const instance: StrandInstance = {
238
- strandId,
239
- status: 'starting',
240
- sAppInfo,
241
- memberPrivateKey: strandRow.MemberPrivateKey ?? undefined,
242
- connectedPeers: 0,
243
- lastActivity: new Date(),
244
- latencyHint,
245
- // Seed only — buildStrandRuntime overwrites this with the same resolution,
246
- // so an instance that errors mid-build still reads sensibly.
247
- mode: config.mode ?? 'networked'
248
- };
249
-
250
- this.instances.set(strandId, instance);
251
- this.launchConfigs.set(strandId, config);
252
-
253
- try {
254
- await this.buildStrandRuntime(instance, config);
255
- timing('[startStrand:%s] total: %dms', strandId, Math.round(performance.now() - tTotal));
256
- log('Strand %s started successfully with sApp %s', strandId, sAppConfig.id);
257
- return instance;
258
- } catch (error) {
259
- // Status/error first — the (now discarded) record is still what `log` reports on.
260
- instance.status = 'error';
261
- instance.error = error instanceof Error ? error.message : String(error);
262
- log('Failed to start strand %s: %s', strandId, instance.error);
263
- // Drop the dead record so this strand id can be launched again. Keep the
264
- // `launchConfigs` has an entry iff `instances` does invariant — resumeStrand
265
- // reads both, and a config without an instance would strand the config.
266
- this.instances.delete(strandId);
267
- this.launchConfigs.delete(strandId);
268
- throw error;
269
- }
270
- }
271
-
272
- /**
273
- * Build (or rebuild) the libp2p node + StrandDatabase for an instance and
274
- * attach them, transitioning it to `active`. Shared by `startStrand` (fresh
275
- * launch) and `resumeStrand` (rehydrating a quiesced instance). Reads all
276
- * volatile inputs (bootstrapNodes, mode, storage, network, profile,
277
- * privateKey, sApp config) from `config`, so the caller controls the
278
- * cohort-derived values.
279
- */
280
- private async buildStrandRuntime(instance: StrandInstance, config: StartStrandConfig): Promise<void> {
281
- const strandId = instance.strandId;
282
- const { sAppConfig } = config;
283
- const mode = config.mode ?? 'networked';
284
-
285
- // Resolve storage for this strand. If a factory function is provided, it is
286
- // called with the strandId to create strand-specific storage (e.g.,
287
- // strand-isolated directories).
288
- const strandStorage = resolveStrandStorage(config.storage?.provider, strandId);
289
- if (strandStorage) {
290
- log('Strand %s using provided storage provider', strandId);
291
- }
292
-
293
- // db-p2p namespaces every one of the node's protocol ids by network name
294
- // (`/optimystic/<networkName>/...`), so anything dialing this node's own
295
- // services must derive its prefix from the SAME string the node was built
296
- // with — hence one binding for both, not two literals that can drift.
297
- const networkName = `strand-${strandId}`;
298
- const protocolPrefix = `/optimystic/${networkName}`;
299
-
300
- // Determine relay mode: if explicitly set in config, use that;
301
- // otherwise default to true for storage profile nodes.
302
- const enableRelay = config.network?.enableRelay ?? (config.profile === 'storage');
303
- const listenAddrs = resolveListenAddrs(config.network);
304
-
305
- try {
306
- let t0 = performance.now();
307
- const node = await createLibp2pNode({
308
- port: 0, // Random port
309
- bootstrapNodes: config.bootstrapNodes ?? [],
310
- networkName,
311
- storage: strandStorage,
312
- fretProfile: config.profile === 'storage' ? 'core' : 'edge',
313
- relay: enableRelay,
314
- clusterSize: resolveStrandClusterSize(config.clusterSize),
315
- // Deliberately NOT CONTROL_CLUSTER_POLICY: a strand is application data with its own
316
- // breadth reasoning, and the shape match with the control policy is a coincidence.
317
- clusterPolicy: STRAND_CLUSTER_POLICY,
318
- arachnode: {
319
- enableRingZulu: config.profile === 'storage'
320
- },
321
- ...(config.privateKey && { privateKey: config.privateKey }),
322
- ...(config.network?.transports && { transports: config.network.transports }),
323
- // NOTE: strand nodes receive the same RESOLVED listen addrs as the
324
- // control node. A fixed-port listen addr (e.g. cadre-cli's example
325
- // `/ip4/0.0.0.0/tcp/4001`) would have control + strand nodes racing to
326
- // bind one port — EADDRINUSE. Unverified; if a deployment configures a
327
- // fixed port and strands fail to start, rewrite the port per node here.
328
- // An inherited `/p2p-circuit` addr, by contrast, is deliberate — it is
329
- // what gives a NAT'd strand node a reachable relay slot — and works
330
- // because the launch path announces this strand's derived peerId to
331
- // the relay first (delegate admission; see cadre-node.ts). Those circuit
332
- // entries come either from a hand-written `network.listenAddrs` or from
333
- // `network.relayAddrs`, which `resolveListenAddrs` folds into the same list.
334
- ...(listenAddrs && { listenAddrs }),
335
- ...(config.network?.connectionGater && { connectionGater: config.network.connectionGater })
336
- }) as Libp2pNodeWithRepo;
337
- timing('[buildStrandRuntime:%s] createLibp2pNode: %dms', strandId, Math.round(performance.now() - t0));
338
-
339
- instance.libp2pNode = node;
340
- // Assigned on every runtime (re)build — resume re-enters here with
341
- // resumeConfig, so a bootstrap → networked shift refreshes the field.
342
- instance.mode = mode;
343
-
344
- // Create and initialize the StrandDatabase. In bootstrap mode the same
345
- // strandStorage instance also backs the optimystic local transactor so
346
- // DML lands on the host's persistent storage (e.g. LevelDB on RN). Sharing
347
- // the instance — not creating a second one over the same id+prefix —
348
- // avoids cache divergence between the libp2p and database paths.
349
- //
350
- // Attach before initialize so a failed init is cleaned up by
351
- // releaseRuntime below (close() is safe on a partially-initialized db).
352
- t0 = performance.now();
353
- const strandDb = new StrandDatabase({
354
- strandId,
355
- sAppConfig,
356
- libp2pNode: node,
357
- coordinatedRepo: node.coordinatedRepo,
358
- mode,
359
- rawStorage: strandStorage,
360
- // Founder bootstrap inputs: the strand's type drives which membership rows
361
- // are written, and the closed-strand MemberPrivateKey derives the founding
362
- // Member/Manager key. Both come off the control-network strand row.
363
- strandType: config.strandRow.Type,
364
- memberPrivateKey: config.strandRow.MemberPrivateKey ?? undefined,
365
- founder: config.founder
366
- });
367
- instance.database = strandDb;
368
- await strandDb.initialize();
369
- timing('[buildStrandRuntime:%s] strandDatabase.initialize: %dms', strandId, Math.round(performance.now() - t0));
370
-
371
- // Peer-join block catch-up: push this strand's own blocks to each newly
372
- // connected peer, so a machine that joined after blocks were committed
373
- // still ends up physically holding them (a bootstrap strand has no mesh
374
- // to catch up, and without per-strand storage there is nothing to copy).
375
- if (mode === 'networked' && strandStorage && config.backfill?.enabled !== false) {
376
- if (node.keyNetwork) {
377
- const backfill = new StrandBackfill({
378
- strandId,
379
- libp2p: node,
380
- peerNetwork: node.keyNetwork,
381
- storage: strandStorage,
382
- // The same prefix the receiver registered its block-transfer handler
383
- // under — derived from networkName above, never re-spelled here.
384
- protocolPrefix
385
- }, config.backfill);
386
- backfill.start();
387
- this.backfills.set(strandId, backfill);
388
- } else {
389
- log('Strand %s: libp2p node exposes no keyNetwork; peer-join block catch-up is inert', strandId);
390
- }
391
- }
392
-
393
- instance.status = 'active';
394
- instance.lastActivity = new Date();
395
- } catch (error) {
396
- // Roll back any partially-attached runtime so the instance is left with
397
- // NEITHER handle. Otherwise the `libp2pNode || database` "already live"
398
- // guard in resumeStrand/handleStrandWake would treat a half-built strand
399
- // as healthy — leaking the libp2p node and never retrying the rebuild.
400
- await this.releaseRuntime(instance).catch((cleanupErr) => {
401
- log('buildStrandRuntime cleanup for strand %s also failed: %o', strandId, cleanupErr);
402
- });
403
- throw error;
404
- }
405
- }
406
-
407
- /**
408
- * Release an instance's strand-network runtime: close the StrandDatabase, then
409
- * stop the libp2p node (construction order in reverse), clearing both fields
410
- * and zeroing connectedPeers. Tolerant of partially-built state — either handle
411
- * may be absent — so it doubles as rollback for a failed `buildStrandRuntime`.
412
- * Shared by `quiesceStrand`, `stopStrand`, and that rollback path.
413
- */
414
- private async releaseRuntime(instance: StrandInstance): Promise<void> {
415
- // Backfill first — before the database closes and the libp2p node stops — so
416
- // no NEW catch-up push is issued against a torn-down transport. A push already
417
- // in flight is not awaited; it fails into the module's own per-chunk catch.
418
- const backfill = this.backfills.get(instance.strandId);
419
- if (backfill) {
420
- backfill.stop();
421
- this.backfills.delete(instance.strandId);
422
- }
423
- if (instance.database) {
424
- await instance.database.close();
425
- instance.database = undefined;
426
- }
427
- if (instance.libp2pNode) {
428
- await instance.libp2pNode.stop();
429
- instance.libp2pNode = undefined;
430
- }
431
- instance.connectedPeers = 0;
432
- }
433
-
434
- /**
435
- * Quiesce a strand: release its strand-network resources (stop the libp2p node,
436
- * close the StrandDatabase) while RETAINING the instance record — identity,
437
- * sAppInfo, keys, latency hint, metadata — and its launch config so it can be
438
- * resumed later. Mechanically this is `stopStrand` minus the instance/config
439
- * deletion. The caller sets the post-quiesce status (e.g. `hibernating`).
440
- * No-ops when the strand is missing or already quiesced.
441
- */
442
- async quiesceStrand(strandId: string): Promise<void> {
443
- const instance = this.instances.get(strandId);
444
- if (!instance) {
445
- log('quiesceStrand: strand %s not found', strandId);
446
- return;
447
- }
448
- if (!instance.libp2pNode && !instance.database) {
449
- log('quiesceStrand: strand %s already quiesced', strandId);
450
- return;
451
- }
452
-
453
- log('Quiescing strand instance: %s', strandId);
454
- await this.releaseRuntime(instance);
455
- log('Strand %s quiesced (resources released, instance retained)', strandId);
456
- }
457
-
458
- /**
459
- * Resume a previously-quiesced strand: rebuild its libp2p node + StrandDatabase
460
- * from the retained launch config and re-attach them, transitioning it back to
461
- * `active`. `overrides` re-applies volatile inputs that may have changed since
462
- * launch (cohort `bootstrapNodes`, lifecycle `mode`) and updates the retained
463
- * config so a later resume reuses the latest values. Returns the live instance
464
- * unchanged if it is already running.
465
- */
466
- async resumeStrand(strandId: string, overrides?: ResumeStrandOverrides): Promise<StrandInstance> {
467
- if (this.stopping) {
468
- throw new Error('StrandInstanceManager is stopping');
469
- }
470
- const instance = this.instances.get(strandId);
471
- if (!instance) {
472
- throw new Error(`Cannot resume strand ${strandId}: not tracked`);
473
- }
474
- const launchConfig = this.launchConfigs.get(strandId);
475
- if (!launchConfig) {
476
- throw new Error(`Cannot resume strand ${strandId}: no retained launch config`);
477
- }
478
- if (instance.libp2pNode || instance.database) {
479
- log('resumeStrand: strand %s already live', strandId);
480
- return instance;
481
- }
482
-
483
- log('Resuming strand instance: %s', strandId);
484
- const tTotal = performance.now();
485
-
486
- // Re-apply volatile inputs and persist them so a subsequent resume reuses them.
487
- const resumeConfig: StartStrandConfig = {
488
- ...launchConfig,
489
- bootstrapNodes: overrides?.bootstrapNodes ?? launchConfig.bootstrapNodes,
490
- mode: overrides?.mode ?? launchConfig.mode
491
- };
492
- this.launchConfigs.set(strandId, resumeConfig);
493
-
494
- instance.status = 'starting';
495
- try {
496
- await this.buildStrandRuntime(instance, resumeConfig);
497
- timing('[resumeStrand:%s] total: %dms', strandId, Math.round(performance.now() - tTotal));
498
- log('Strand %s resumed successfully', strandId);
499
- return instance;
500
- } catch (error) {
501
- instance.status = 'error';
502
- instance.error = error instanceof Error ? error.message : String(error);
503
- log('Failed to resume strand %s: %s', strandId, instance.error);
504
- throw error;
505
- }
506
- }
507
-
508
- /**
509
- * Stop a strand instance
510
- */
511
- async stopStrand(strandId: string): Promise<void> {
512
- const instance = this.instances.get(strandId);
513
- if (!instance) {
514
- log('Strand %s not found', strandId);
515
- return;
516
- }
517
-
518
- log('Stopping strand instance: %s', strandId);
519
- instance.status = 'stopping';
520
-
521
- try {
522
- await this.releaseRuntime(instance);
523
- instance.status = 'stopped';
524
- this.instances.delete(strandId);
525
- this.launchConfigs.delete(strandId);
526
- log('Strand %s stopped successfully', strandId);
527
- } catch (error) {
528
- instance.status = 'error';
529
- instance.error = error instanceof Error ? error.message : String(error);
530
- log('Error stopping strand %s: %s', strandId, instance.error);
531
- throw error;
532
- }
533
- }
534
-
535
- /**
536
- * Stop all strand instances
537
- */
538
- async stopAll(): Promise<void> {
539
- this.stopping = true;
540
- log('Stopping all strand instances (%d)', this.instances.size);
541
-
542
- const stopPromises = Array.from(this.instances.keys()).map(id =>
543
- this.stopStrand(id).catch(err => {
544
- log('Error stopping strand %s during shutdown: %s', id, err);
545
- })
546
- );
547
-
548
- await Promise.all(stopPromises);
549
- this.stopping = false;
550
- log('All strand instances stopped');
551
- }
552
- }
553
-
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
+