@serfab/cadre-core 0.13.0 → 1.1.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 (131) hide show
  1. package/README.md +20 -6
  2. package/dist/bootstrap-peer-store-file.js +2 -2
  3. package/dist/bootstrap-peer-store.d.ts +32 -12
  4. package/dist/bootstrap-peer-store.js +41 -15
  5. package/dist/bootstrap-peer-store.js.map +1 -1
  6. package/dist/cadre-node.d.ts +558 -80
  7. package/dist/cadre-node.js +1060 -170
  8. package/dist/cadre-node.js.map +1 -1
  9. package/dist/control-authorization.d.ts +2 -2
  10. package/dist/control-authorization.js +1 -0
  11. package/dist/control-authorization.js.map +1 -1
  12. package/dist/control-cohort.d.ts +13 -26
  13. package/dist/control-cohort.js +0 -26
  14. package/dist/control-cohort.js.map +1 -1
  15. package/dist/control-database.d.ts +299 -44
  16. package/dist/control-database.js +469 -69
  17. package/dist/control-database.js.map +1 -1
  18. package/dist/control-read-retry.d.ts +40 -11
  19. package/dist/control-read-retry.js +48 -15
  20. package/dist/control-read-retry.js.map +1 -1
  21. package/dist/control-retry.d.ts +58 -3
  22. package/dist/control-retry.js +58 -3
  23. package/dist/control-retry.js.map +1 -1
  24. package/dist/control-schema.d.ts +1 -1
  25. package/dist/control-schema.js +103 -19
  26. package/dist/control-schema.js.map +1 -1
  27. package/dist/control-write-retry.d.ts +80 -11
  28. package/dist/control-write-retry.js +176 -29
  29. package/dist/control-write-retry.js.map +1 -1
  30. package/dist/enrolled-machine-store.js +2 -2
  31. package/dist/index.d.ts +14 -7
  32. package/dist/index.js +33 -6
  33. package/dist/index.js.map +1 -1
  34. package/dist/node-local-snapshot.d.ts +15 -7
  35. package/dist/node-local-snapshot.js +23 -8
  36. package/dist/node-local-snapshot.js.map +1 -1
  37. package/dist/peer-dial.d.ts +143 -0
  38. package/dist/peer-dial.js +176 -0
  39. package/dist/peer-dial.js.map +1 -0
  40. package/dist/relay-addrs.d.ts +90 -52
  41. package/dist/relay-addrs.js +91 -59
  42. package/dist/relay-addrs.js.map +1 -1
  43. package/dist/relay-reservation.d.ts +71 -3
  44. package/dist/relay-reservation.js +116 -8
  45. package/dist/relay-reservation.js.map +1 -1
  46. package/dist/seed-bootstrap.d.ts +30 -1
  47. package/dist/seed-bootstrap.js +62 -17
  48. package/dist/seed-bootstrap.js.map +1 -1
  49. package/dist/storage-scope.d.ts +62 -0
  50. package/dist/storage-scope.js +75 -0
  51. package/dist/storage-scope.js.map +1 -0
  52. package/dist/strand-database.d.ts +34 -16
  53. package/dist/strand-database.js +38 -24
  54. package/dist/strand-database.js.map +1 -1
  55. package/dist/strand-first-sync-gate.d.ts +152 -0
  56. package/dist/strand-first-sync-gate.js +208 -0
  57. package/dist/strand-first-sync-gate.js.map +1 -0
  58. package/dist/strand-formation-manager.d.ts +87 -5
  59. package/dist/strand-formation-manager.js +109 -5
  60. package/dist/strand-formation-manager.js.map +1 -1
  61. package/dist/strand-formation-protocol.d.ts +21 -1
  62. package/dist/strand-formation-protocol.js +30 -0
  63. package/dist/strand-formation-protocol.js.map +1 -1
  64. package/dist/strand-instance-manager.d.ts +243 -6
  65. package/dist/strand-instance-manager.js +470 -37
  66. package/dist/strand-instance-manager.js.map +1 -1
  67. package/dist/strand-member-key.d.ts +20 -12
  68. package/dist/strand-member-key.js +20 -12
  69. package/dist/strand-member-key.js.map +1 -1
  70. package/dist/strand-member-registry.js +3 -0
  71. package/dist/strand-member-registry.js.map +1 -1
  72. package/dist/strand-membership-reconciler.d.ts +361 -0
  73. package/dist/strand-membership-reconciler.js +548 -0
  74. package/dist/strand-membership-reconciler.js.map +1 -0
  75. package/dist/strand-membership-writer.d.ts +131 -11
  76. package/dist/strand-membership-writer.js +396 -134
  77. package/dist/strand-membership-writer.js.map +1 -1
  78. package/dist/strand-network-config.d.ts +46 -14
  79. package/dist/strand-network-config.js +79 -23
  80. package/dist/strand-network-config.js.map +1 -1
  81. package/dist/strand-solicitation.d.ts +10 -1
  82. package/dist/strand-solicitation.js +5 -0
  83. package/dist/strand-solicitation.js.map +1 -1
  84. package/dist/strand-wake-protocol.d.ts +11 -7
  85. package/dist/strand-wake-protocol.js +20 -52
  86. package/dist/strand-wake-protocol.js.map +1 -1
  87. package/dist/strand-watcher.js +6 -0
  88. package/dist/strand-watcher.js.map +1 -1
  89. package/dist/timed-step.d.ts +12 -0
  90. package/dist/timed-step.js +28 -0
  91. package/dist/timed-step.js.map +1 -0
  92. package/dist/timeout-scheduler.d.ts +19 -0
  93. package/dist/timeout-scheduler.js +22 -0
  94. package/dist/timeout-scheduler.js.map +1 -0
  95. package/dist/types.d.ts +262 -30
  96. package/dist/types.js.map +1 -1
  97. package/package.json +8 -8
  98. package/src/bootstrap-peer-store-file.ts +2 -2
  99. package/src/bootstrap-peer-store.ts +69 -27
  100. package/src/cadre-node.ts +7301 -6353
  101. package/src/control-authorization.ts +2 -1
  102. package/src/control-cohort.ts +122 -130
  103. package/src/control-database.ts +537 -69
  104. package/src/control-read-retry.ts +202 -167
  105. package/src/control-retry.ts +297 -190
  106. package/src/control-schema.ts +103 -19
  107. package/src/control-write-retry.ts +533 -383
  108. package/src/enrolled-machine-store.ts +2 -2
  109. package/src/index.ts +83 -6
  110. package/src/node-local-snapshot.ts +25 -8
  111. package/src/peer-dial.ts +215 -0
  112. package/src/relay-addrs.ts +458 -436
  113. package/src/relay-reservation.ts +951 -828
  114. package/src/seed-bootstrap.ts +76 -18
  115. package/src/storage-scope.ts +78 -0
  116. package/src/strand-database.ts +50 -27
  117. package/src/strand-first-sync-gate.ts +258 -0
  118. package/src/strand-formation-manager.ts +140 -5
  119. package/src/strand-formation-protocol.ts +43 -1
  120. package/src/strand-instance-manager.ts +577 -37
  121. package/src/strand-member-key.ts +20 -12
  122. package/src/strand-member-registry.ts +3 -0
  123. package/src/strand-membership-reconciler.ts +680 -0
  124. package/src/strand-membership-writer.ts +527 -189
  125. package/src/strand-network-config.ts +102 -24
  126. package/src/strand-solicitation.ts +16 -1
  127. package/src/strand-wake-protocol.ts +21 -66
  128. package/src/strand-watcher.ts +6 -0
  129. package/src/timed-step.ts +28 -0
  130. package/src/timeout-scheduler.ts +28 -0
  131. package/src/types.ts +1789 -1554
@@ -10,7 +10,21 @@ import {
10
10
  readStrandRevocationRows,
11
11
  type StrandRevocationEnforcementConfig
12
12
  } from './strand-revocation-enforcer.js';
13
+ import {
14
+ StrandMembershipReconciler,
15
+ type PendingMembershipInviteSource,
16
+ type StrandMembershipReconciliationConfig
17
+ } from './strand-membership-reconciler.js';
18
+ import { removeMemberPeer } from './strand-membership-writer.js';
19
+ import { strandMemberKeyPair } from './strand-member-key.js';
13
20
  import { assertSchemaSignature } from './schema-verification.js';
21
+ import {
22
+ StrandFirstSyncGate,
23
+ StrandAwaitingFirstSyncError,
24
+ strandFirstSyncComplete,
25
+ DEFAULT_STRAND_FIRST_SYNC_TIMEOUT_MS,
26
+ type StrandFirstSyncConfig
27
+ } from './strand-first-sync-gate.js';
14
28
  import type {
15
29
  StrandInstance,
16
30
  StrandRow,
@@ -25,10 +39,46 @@ import type {
25
39
  } from './types.js';
26
40
  import { resolveStrandClusterSize, strandClusterPolicy } from './types.js';
27
41
  import { strandNodeAddrs } from './strand-network-config.js';
42
+ import { superviseRelayReservation, type RelayReservationSupervisor } from './relay-reservation.js';
28
43
 
29
44
  const log = debug('sereus:cadre:strand-manager');
30
45
  const timing = debug('sereus:cadre:timing');
31
46
 
47
+ /** One caller blocked in {@link StrandInstanceManager.whenWritable}. */
48
+ interface WritableWaiter {
49
+ resolve: () => void;
50
+ reject: (error: Error) => void;
51
+ }
52
+
53
+ /**
54
+ * Whether a tracked instance is launched but waiting for its first sync: the runtime is
55
+ * up (`libp2pNode` set) while the database is still withheld (`database` unset). That
56
+ * is the first-sync gate's state regardless of what `status` says — the hibernation
57
+ * manager's idle timer can flip a long-gated joiner to `'idle'` without changing it.
58
+ *
59
+ * NOTE: `status` is therefore not the source of truth for "gated": a joiner that reaches
60
+ * nobody for `idleTimeout` (5 min at the interactive hint) reads `'idle'`, then
61
+ * `'hibernating'`, while still holding no database. Harmless today — every decision the
62
+ * runtime makes goes through this predicate, {@link StrandInstanceManager.publishDatabase}
63
+ * announces on the idle-flipped instance exactly as on a `'syncing'` one, and an app that
64
+ * shows "waiting for the other member" on `'syncing'` merely sees "idle" instead after
65
+ * five minutes. If an app ever needs the gated LABEL to survive the idle flip, make the
66
+ * hibernation manager's idle transition preserve `'syncing'` rather than adding a flag.
67
+ */
68
+ export function isAwaitingFirstSync(instance: StrandInstance): boolean {
69
+ return instance.libp2pNode !== undefined && instance.database === undefined;
70
+ }
71
+
72
+ /**
73
+ * The status a LIVE instance (runtime rebuilt or woken) should report: `'active'` once its
74
+ * database is published, `'syncing'` while the first-sync gate still withholds it. Every
75
+ * site that would otherwise write `'active'` after a build or a wake goes through this,
76
+ * so a gated joiner never reads as writable.
77
+ */
78
+ export function liveStrandStatus(instance: StrandInstance): 'active' | 'syncing' {
79
+ return instance.database ? 'active' : 'syncing';
80
+ }
81
+
32
82
  /**
33
83
  * Configuration for starting a strand instance
34
84
  */
@@ -60,6 +110,17 @@ export interface StartStrandConfig {
60
110
  * never through a repeat `startStrand`.
61
111
  */
62
112
  founder?: boolean;
113
+ /**
114
+ * THIS party's own strand membership private key (base64 protobuf) for a closed
115
+ * strand — the identity the founder bootstrap derives `Member.Key` /
116
+ * `Manager.MemberKey` from. Resolved by the caller (`CadreNode.launchStrand`: an
117
+ * explicit attach-time key, else the control-layer `StrandPartyKey` row, healed by
118
+ * mint-on-launch for the founding machine) and retained with the launch config, so a
119
+ * hibernation wake rebuilds under the same identity. Deliberately NOT the strand
120
+ * row's shared `MemberPrivateKey` (see `StrandDatabaseConfig.partyMemberPrivateKey`).
121
+ * Absent for open strands, and for joiners with no persisted party key.
122
+ */
123
+ partyMemberPrivateKey?: string;
63
124
  /**
64
125
  * Number of nodes Optimystic is told this strand's replication cluster should
65
126
  * have. Same rule as {@link CadreNodeConfig.strandClusterSize}, which CadreNode
@@ -119,6 +180,28 @@ export interface StartStrandConfig {
119
180
  */
120
181
  revocationEnforcement?: StrandRevocationEnforcementConfig;
121
182
 
183
+ /**
184
+ * The staged formation membership invitation for THIS strand — the seam the
185
+ * bring-up membership reconciler (`strand-membership-reconciler.ts`) reads to
186
+ * redeem the party's `Strand.Member` seat, and clears once the invitation is
187
+ * spent, burned, or dead. `CadreNode.launchStrand` wires it over its in-memory
188
+ * `pendingMembershipInvites` map; read lazily per pass, so a re-formation that
189
+ * replaces the entry between passes is picked up. Only consulted for closed
190
+ * strands launched with a {@link partyMemberPrivateKey}; harmless otherwise.
191
+ */
192
+ pendingMembershipInvite?: PendingMembershipInviteSource;
193
+
194
+ /**
195
+ * Tuning for the CLOSED-strand membership reconciler, forwarded from
196
+ * {@link CadreNodeConfig.strandMembershipReconciliation}. Armed only for
197
+ * closed strands launched with a {@link partyMemberPrivateKey};
198
+ * `{ enabled: false }` disarms the loop (test fixtures that hand-drive the
199
+ * membership writers). When it names no `pollIntervalMs` the reconciler
200
+ * mirrors {@link revocationEnforcement}'s cadence — as its IDLE cadence and
201
+ * the cap of its unfinished-join retry ladder, not as a flat retry interval.
202
+ */
203
+ membershipReconciliation?: StrandMembershipReconciliationConfig;
204
+
122
205
  /**
123
206
  * Called when THIS node's own peer id turns up in the strand's revoked set —
124
207
  * this node's party was removed from the closed strand, or left it. Fires once
@@ -129,6 +212,40 @@ export interface StartStrandConfig {
129
212
  * this node's behalf — what to do about it is the app's call.
130
213
  */
131
214
  onSelfRevoked?: (strandId: string) => void;
215
+
216
+ /**
217
+ * Tuning for the JOINING machine's first-sync write gate (`strand-first-sync-gate.ts`),
218
+ * forwarded from {@link CadreNodeConfig.strandFirstSync}: the Header probe cadence while
219
+ * a non-founder launch is `'syncing'`, and the default budget {@link whenWritable} waits
220
+ * before rejecting. Founders and machines that already hold the Header are never gated.
221
+ */
222
+ firstSync?: StrandFirstSyncConfig;
223
+
224
+ /**
225
+ * Called when a launch that came up `'syncing'` (see {@link firstSync}) becomes
226
+ * writable: the strand's `Strand.Header` arrived from a peer — or a founder request
227
+ * ran the bootstrap against the gated database — and `instance.database` is now set.
228
+ * Never called for a launch that was writable when `startStrand`/`resumeStrand`
229
+ * resolved. `CadreNode` wires it to its `strand:writable` event. Retained with the
230
+ * launch config, so a hibernation resume that comes up gated announces too.
231
+ */
232
+ onWritable?: (strandId: string) => void;
233
+
234
+ /**
235
+ * Re-announce this strand's delegate peer id (`delegatePeerId` — the strand
236
+ * node's own transport peer id) to ONE relay (`relayAddr`, a direct dial addr
237
+ * with a trailing `/p2p/<relayPeerId>`), unthrottled. Awaited before every
238
+ * relay-reservation RE-drive of that relay — never before the first attempt,
239
+ * which the launch/resume seed pass has already announced for.
240
+ *
241
+ * Why: a party control node running the relay server admits the strand node on
242
+ * an in-memory delegate grant that a relay restart drops, so a re-drive that did
243
+ * not re-announce first would be denied at the relay's gate. `CadreNode` wires
244
+ * its own control-mesh announce here; a throw or rejection is logged by the
245
+ * supervisor and the re-drive still runs. Retained with the launch config, so a
246
+ * hibernation wake's rebuilt supervisors carry it too.
247
+ */
248
+ announceDelegateToRelay?: (strandId: string, relayAddr: string, delegatePeerId: string) => Promise<void>;
132
249
  }
133
250
 
134
251
  /**
@@ -242,6 +359,45 @@ export class StrandInstanceManager {
242
359
  * resume rebuilds it with a fresh (initially empty, fail-open) snapshot.
243
360
  */
244
361
  private revocationEnforcers: Map<string, StrandRevocationEnforcer> = new Map();
362
+ /**
363
+ * The per-strand membership reconciler (closed strands launched with a party
364
+ * key only), keyed by strand id. Same lifecycle rationale as {@link backfills}:
365
+ * created in `buildStrandRuntime`, stopped and dropped in `releaseRuntime`, so
366
+ * quiesce → resume rebuilds it and re-runs the idempotent join ladder (redeem a
367
+ * staged invitation, write this machine's own `MemberPeer` binding) from scratch.
368
+ */
369
+ private membershipReconcilers: Map<string, StrandMembershipReconciler> = new Map();
370
+ /**
371
+ * The per-strand first-sync gate (`strand-first-sync-gate.ts`), keyed by strand id:
372
+ * holds a launch's `StrandDatabase` while it is NOT yet published on the instance.
373
+ * Present from the moment the database object exists in `buildStrandRuntime` until
374
+ * {@link publishDatabase} hands it to the app — so it doubles as the rollback handle
375
+ * for a failed `initialize()` — and, for a joiner that holds no `Strand.Header` yet,
376
+ * for as long as the launch stays `'syncing'`. Same lifecycle as {@link backfills}:
377
+ * stopped (its database closed) and dropped in `releaseRuntime`, so a quiesce →
378
+ * resume rebuild re-probes over the same store. An entry and `instance.database`
379
+ * are mutually exclusive.
380
+ */
381
+ private firstSyncGates: Map<string, StrandFirstSyncGate> = new Map();
382
+ /**
383
+ * Callers blocked in {@link whenWritable}, keyed by strand id. Resolved by
384
+ * {@link publishDatabase}, rejected by {@link stopStrand}; each waiter also carries
385
+ * its own timeout. Deliberately NOT cleared by `releaseRuntime`: a waiter outlives a
386
+ * quiesce → resume cycle, since the rebuild is what may finally publish the database.
387
+ */
388
+ private writableWaiters: Map<string, Set<WritableWaiter>> = new Map();
389
+ /**
390
+ * The per-strand relay-reservation supervisors — ONE PER CONFIGURED RELAY, each
391
+ * over the node's own bare `/p2p-circuit` listener for that relay
392
+ * (`strand-network-config.ts`) — keyed by strand id. Same lifecycle rationale as
393
+ * {@link backfills}: started in `buildStrandRuntime` right after the libp2p node
394
+ * exists, stopped FIRST and dropped in `releaseRuntime` (before the database
395
+ * closes and the node stops, so no re-drive dials a node being torn down), so
396
+ * quiesce → resume rebuilds them over the new node. What makes a strand node's
397
+ * lost relay slot come back on its own — the control node has the same loop in
398
+ * `CadreNode.reserveRelays`.
399
+ */
400
+ private relaySupervisors: Map<string, RelayReservationSupervisor[]> = new Map();
245
401
  /**
246
402
  * The resolved (cache-wrapped) raw storage per strand id — the instance's OWN
247
403
  * store, resolved once in `startStrand` and held until `stopStrand` disposes it.
@@ -420,20 +576,19 @@ export class StrandInstanceManager {
420
576
  // Determine relay mode: if explicitly set in config, use that;
421
577
  // otherwise default to true for storage profile nodes.
422
578
  const enableRelay = config.network?.enableRelay ?? (config.profile === 'storage');
423
- // The CONFIGURED relay route (the default), deliberately NOT the control node's
424
- // `'search'` route: nothing drives an explicit reservation for a strand node, so
425
- // a bare `/p2p-circuit` search entry would register a pending reservation that
426
- // never gets filled and leave the strand node undialable. The ordering hazard
427
- // that pushed the control node onto the search route does not exist here — a
428
- // strand node's protocol ids are namespaced `/optimystic/strand-<id>/…`, so a
429
- // relay dialed from inside `libp2p.start()` is never in the strand's cohort and
430
- // cannot refuse its database bring-up. See `relay-addrs.ts`.
431
- //
432
579
  // The strand-node VIEW of the machine's one `NetworkConfig`, not the control
433
580
  // node's resolution: fixed direct listen ports become ephemeral (two nodes cannot
434
- // bind one port) and the announce config is dropped (it names the control node's
435
- // address). See `strand-network-config.ts` for both, and for the tradeoff.
436
- const addrOptions = strandNodeAddrs(config.network);
581
+ // bind one port), the announce config is dropped (it names the control node's
582
+ // address), and every configured relay becomes one bare `/p2p-circuit` SEARCH
583
+ // listener plus a dial addr in `relayAddrs` for the per-relay supervisor started
584
+ // below — the same route the control node takes, because the configured
585
+ // `<relay>/p2p-circuit` shape loses its address on a relay restart, a hangup, or
586
+ // libp2p's own reservation refresh and recovers from none of them. See
587
+ // `strand-network-config.ts` for all three, and `relay-reservation.ts`.
588
+ //
589
+ // `relayAddrs` is destructured OFF: it is not a `createLibp2pNode` option, and
590
+ // spreading it would hand db-p2p a key it does not know.
591
+ const { relayAddrs: relayDialAddrs = [], ...addrOptions } = strandNodeAddrs(config.network);
437
592
 
438
593
  // The CLOSED-strand revoked-peer gate (see strand-revocation-enforcer.ts).
439
594
  // Constructed BEFORE the libp2p node because the node's options embed its
@@ -515,13 +670,12 @@ export class StrandInstanceManager {
515
670
  // Listen entries plus the WebSocket transport switch they imply — a strand node
516
671
  // announces nothing the operator configured (`strand-network-config.ts`), and
517
672
  // spreads AFTER `transports` above because the switch is a no-op whenever the
518
- // embedder supplied its own factories. An inherited configured `/p2p-circuit` entry is
519
- // deliberate and survives the derivation untouched: it is what gives a NAT'd
520
- // strand node a reachable relay slot, and it works because the launch path
521
- // announces this strand's derived peerId to the relay first (delegate admission;
522
- // see cadre-node.ts). Those circuit entries come either from a hand-written
523
- // `network.listenAddrs` or from `network.relayAddrs`, which the resolution folds
524
- // into the same list on the configured route this call takes (see above).
673
+ // embedder supplied its own factories. The bare `/p2p-circuit` entries (one per
674
+ // relay) open no connection here; each registers a pending reservation that the
675
+ // matching supervisor below fills, which is what gives a NAT'd strand node a
676
+ // reachable relay slot — and works against a party-run relay because the launch
677
+ // path announced this strand's derived peerId to it first (delegate admission;
678
+ // see cadre-node.ts).
525
679
  ...addrOptions,
526
680
  // The raw configured gater (open strands), or the revocation-composed
527
681
  // gater plus the fail-closed per-stream revoked-peer gate (closed
@@ -532,10 +686,21 @@ export class StrandInstanceManager {
532
686
 
533
687
  instance.libp2pNode = node;
534
688
 
689
+ // One reservation supervisor PER RELAY, started now so the first attempts
690
+ // overlap the database bring-up below (the relay is never in a strand's
691
+ // Optimystic cohort — its protocol ids are namespaced `/optimystic/strand-<id>/…`
692
+ // — so an open relay connection cannot disturb that bring-up). Their first
693
+ // attempts are awaited before the strand goes `active`, below. Registered in the
694
+ // map immediately so the failure rollback (releaseRuntime) stops them like every
695
+ // other runtime component.
696
+ this.relaySupervisors.set(strandId, this.startRelaySupervisors(strandId, node, relayDialAddrs, config));
697
+
535
698
  // Create and initialize the StrandDatabase.
536
699
  //
537
- // Attach before initialize so a failed init is cleaned up by
538
- // releaseRuntime below (close() is safe on a partially-initialized db).
700
+ // Held by the first-sync gate (registered before initialize) rather than attached
701
+ // to the instance: a failed init is then cleaned up by releaseRuntime below through
702
+ // the gate (close() is safe on a partially-initialized db), and the app only ever
703
+ // sees `instance.database` once this machine is allowed to write to the strand.
539
704
  t0 = performance.now();
540
705
  const strandDb = new StrandDatabase({
541
706
  strandId,
@@ -543,16 +708,81 @@ export class StrandInstanceManager {
543
708
  libp2pNode: node,
544
709
  coordinatedRepo: node.coordinatedRepo,
545
710
  // Founder bootstrap inputs: the strand's type drives which membership rows
546
- // are written, and the closed-strand MemberPrivateKey derives the founding
547
- // Member/Manager key. Both come off the control-network strand row.
711
+ // are written, and the PARTY's own key (partyMemberPrivateKey, resolved by the
712
+ // caller from the control-layer StrandPartyKey row) derives the founding
713
+ // Member/Manager key. The row's shared MemberPrivateKey rides along as the
714
+ // read-gating secret only — it derives nobody's identity.
548
715
  strandType: config.strandRow.Type,
549
716
  memberPrivateKey: config.strandRow.MemberPrivateKey ?? undefined,
717
+ partyMemberPrivateKey: config.partyMemberPrivateKey,
550
718
  founder: config.founder
551
719
  });
552
- instance.database = strandDb;
720
+ const gate = new StrandFirstSyncGate({
721
+ label: strandId,
722
+ database: strandDb,
723
+ onHeaderHeld: () => this.publishDatabase(instance, strandDb)
724
+ }, config.firstSync);
725
+ this.firstSyncGates.set(strandId, gate);
553
726
  await strandDb.initialize();
554
727
  timing('[buildStrandRuntime:%s] strandDatabase.initialize: %dms', strandId, Math.round(performance.now() - t0));
555
728
 
729
+ // The first-sync write gate. A founder just wrote the Header in its bootstrap; any
730
+ // other machine may commit only once it holds the Header — which it can only have
731
+ // received from a peer (now, or on an earlier run over the same store) — and has read
732
+ // each App table once, so their collections are fetched too. Publishing the database
733
+ // is what makes the strand writable to the app; until then the instance stays
734
+ // `'syncing'` and the gate re-probes on its cadence. Everything armed below reads
735
+ // `instance.database` lazily, so the reconciler and enforcer wait with it — and
736
+ // `publishDatabase` kicks the reconciler when the wait ends, so a joiner's membership
737
+ // rows land with the strand rather than one retry later.
738
+ t0 = performance.now();
739
+ if (config.founder === true || await strandFirstSyncComplete(strandDb.getDatabase(), strandId)) {
740
+ this.publishDatabase(instance, strandDb);
741
+ } else {
742
+ gate.start();
743
+ }
744
+ timing('[buildStrandRuntime:%s] first-sync probe: %dms', strandId, Math.round(performance.now() - t0));
745
+
746
+ // The membership reconciler: finish this party's join on every machine —
747
+ // redeem a staged formation invitation if one is pending, then write the
748
+ // durable machine→party binding (Strand.MemberPeer). Armed here, the one
749
+ // seam where the strand's transport peer id and a live Database both exist
750
+ // for launch AND hibernation resume, for closed strands that carry the
751
+ // party's own membership key. start() kicks an immediate pass WITHOUT
752
+ // awaiting it — a joiner at this instant has not synced the founder's rows
753
+ // and may lack write quorum, so bring-up is never blocked; an unfinished join
754
+ // then retries on a short doubling ladder capped at the configured
755
+ // pollIntervalMs (which is also the flat cadence while nobody has admitted this
756
+ // party yet), and the loop stops once the member row and this machine's own
757
+ // binding are both in place. Armed AFTER the gate above, so a gated launch
758
+ // always has a reconciler registered by the time the gate can publish.
759
+ if (config.strandRow.Type === 'c' && config.partyMemberPrivateKey
760
+ && config.membershipReconciliation?.enabled !== false) {
761
+ const reconciler = new StrandMembershipReconciler({
762
+ label: strandId,
763
+ partyMemberPrivateKey: config.partyMemberPrivateKey,
764
+ // Read per pass, never captured — same lifecycle argument as the
765
+ // enforcer's getNetwork: quiesce drops both handles.
766
+ getDatabase: () => instance.database?.getDatabase(),
767
+ getOwnPeerId: () => instance.libp2pNode?.peerId.toString(),
768
+ pendingInvite: config.pendingMembershipInvite,
769
+ // The enforcer's CURRENT snapshot view of this node's own peer id — the
770
+ // reconciler stops rather than fight a self-revocation. Absent when the
771
+ // gate is disarmed (fail-open: the loop just runs).
772
+ isSelfRevoked: revocationEnforcer
773
+ ? () => {
774
+ const ownPeerId = instance.libp2pNode?.peerId.toString();
775
+ return ownPeerId !== undefined && revocationEnforcer.isRevoked(ownPeerId);
776
+ }
777
+ : undefined
778
+ }, {
779
+ pollIntervalMs: config.membershipReconciliation?.pollIntervalMs
780
+ ?? config.revocationEnforcement?.pollIntervalMs
781
+ });
782
+ this.membershipReconcilers.set(strandId, reconciler);
783
+ reconciler.start();
784
+ }
785
+
556
786
  // Arm the revoked-peer deny-set poll now that the database it reads
557
787
  // exists. start() kicks an immediate refresh WITHOUT awaiting it —
558
788
  // bring-up and resume are never blocked on a membership read; until the
@@ -593,8 +823,27 @@ export class StrandInstanceManager {
593
823
  }
594
824
  }
595
825
 
596
- instance.status = 'active';
826
+ // The relay supervisors' FIRST attempts, all of them, before `active`: the happy
827
+ // path still publishes its circuit addr before `addStrand` resolves (the
828
+ // strand-addr RPC answers read it, and the same-party circuit scenario asserts
829
+ // it). Fail-SOFT — a first attempt that lands nothing does not fail the launch.
830
+ // The strand's database is up, and the supervisor keeps trying on its backoff;
831
+ // failing here would only trade that for `StrandWatcher`'s full-rebuild retry.
832
+ //
833
+ // NOTE: a relay that is down costs this launch one full drive (10 s), and
834
+ // `StrandWatcher` launches strands one at a time — so N strands cost N × 10 s
835
+ // of bring-up during a relay outage. If that ever matters, stop awaiting here
836
+ // (the circuit addr then lands after `active`) rather than shortening the drive.
837
+ t0 = performance.now();
838
+ await this.awaitFirstRelayAttempts(strandId);
839
+ timing('[buildStrandRuntime:%s] relay first attempts: %dms', strandId, Math.round(performance.now() - t0));
840
+
841
+ instance.status = liveStrandStatus(instance);
597
842
  instance.lastActivity = new Date();
843
+ if (instance.status === 'syncing') {
844
+ log('Strand %s launched as a joiner with no Strand.Header held yet — writes are withheld until ' +
845
+ 'a member of the strand is reached', strandId);
846
+ }
598
847
  } catch (error) {
599
848
  // Roll back any partially-attached runtime so the instance is left with
600
849
  // NEITHER handle. Otherwise the `libp2pNode || database` "already live"
@@ -608,17 +857,30 @@ export class StrandInstanceManager {
608
857
  }
609
858
 
610
859
  /**
611
- * Release an instance's strand-network runtime: close the StrandDatabase, then
612
- * stop the libp2p node (construction order in reverse), clearing both fields
613
- * and zeroing connectedPeers. Tolerant of partially-built state — either handle
614
- * may be absent — so it doubles as rollback for a failed `buildStrandRuntime`.
615
- * Shared by `quiesceStrand`, `stopStrand`, and that rollback path.
860
+ * Release an instance's strand-network runtime: stop the relay supervisors and
861
+ * the other background loops, close the StrandDatabase, then stop the libp2p
862
+ * node (construction order in reverse), clearing both fields and zeroing
863
+ * connectedPeers. Tolerant of partially-built state — either handle may be
864
+ * absent — so it doubles as rollback for a failed `buildStrandRuntime`. Shared
865
+ * by `quiesceStrand`, `stopStrand`, and that rollback path.
616
866
  *
617
867
  * Leaves `strandStorages` untouched by design — the store outlives the runtime it
618
868
  * was built into, which is what makes a resume warm. Only `stopStrand` disposes it.
619
869
  */
620
870
  private async releaseRuntime(instance: StrandInstance): Promise<void> {
621
- // Backfill first — before the database closes and the libp2p node stops — so
871
+ // Relay supervisors FIRST of all — before anything below is torn down — so no
872
+ // re-drive is scheduled against a node being stopped. `stop()` is synchronous
873
+ // and never awaits a drive, so a relay that is down cannot delay this teardown.
874
+ // NOTE: a drive ALREADY in flight cannot be aborted (the drive takes no
875
+ // AbortSignal — `backlog/bug-relay-drive-not-cancellable`); it fails soft
876
+ // against the stopped node within its own 10 s deadline and its result is
877
+ // discarded.
878
+ const relaySupervisors = this.relaySupervisors.get(instance.strandId);
879
+ if (relaySupervisors) {
880
+ relaySupervisors.forEach((supervisor) => supervisor.stop());
881
+ this.relaySupervisors.delete(instance.strandId);
882
+ }
883
+ // Backfill next — before the database closes and the libp2p node stops — so
622
884
  // no NEW catch-up push is issued against a torn-down transport. A push already
623
885
  // in flight is not awaited; it fails into the module's own per-chunk catch.
624
886
  const backfill = this.backfills.get(instance.strandId);
@@ -633,6 +895,22 @@ export class StrandInstanceManager {
633
895
  revocationEnforcer.stop();
634
896
  this.revocationEnforcers.delete(instance.strandId);
635
897
  }
898
+ // The membership reconciler likewise: a resume rebuilds it and re-runs the
899
+ // idempotent ladder, which is what makes resume-after-partial-join heal.
900
+ const membershipReconciler = this.membershipReconcilers.get(instance.strandId);
901
+ if (membershipReconciler) {
902
+ membershipReconciler.stop();
903
+ this.membershipReconcilers.delete(instance.strandId);
904
+ }
905
+ // A still-gated database (a joiner that never received the Header, or a launch whose
906
+ // initialize() failed) is closed through its gate; a published one through the
907
+ // instance. Never both — publishing moves the handle from the gate to the instance.
908
+ const gate = this.firstSyncGates.get(instance.strandId);
909
+ if (gate) {
910
+ gate.stop();
911
+ this.firstSyncGates.delete(instance.strandId);
912
+ await gate.database.close();
913
+ }
636
914
  if (instance.database) {
637
915
  await instance.database.close();
638
916
  instance.database = undefined;
@@ -644,6 +922,53 @@ export class StrandInstanceManager {
644
922
  instance.connectedPeers = 0;
645
923
  }
646
924
 
925
+ /**
926
+ * One {@link superviseRelayReservation} per relay dial addr, each over exactly
927
+ * that relay so "held" is judged per relay (`circuitMultiaddrsVia`) and losing
928
+ * one relay re-drives only that one. Default timings — the control node's (2 s
929
+ * doubling to 60 s between failed attempts, a 5 s liveness check while held).
930
+ *
931
+ * Each supervisor's `beforeRedrive` is the caller's
932
+ * {@link StartStrandConfig.announceDelegateToRelay} for THIS relay and THIS
933
+ * node's peer id, so a re-drive after a relay restart is preceded by a fresh
934
+ * delegate grant on the relay about to be dialed. Returns `[]` when no relay is
935
+ * configured — the strand then simply has no circuit listener to fill.
936
+ */
937
+ private startRelaySupervisors(
938
+ strandId: string,
939
+ node: Libp2pNodeWithRepo,
940
+ relayDialAddrs: readonly string[],
941
+ config: StartStrandConfig
942
+ ): RelayReservationSupervisor[] {
943
+ if (relayDialAddrs.length === 0) {
944
+ return [];
945
+ }
946
+ const announce = config.announceDelegateToRelay;
947
+ const delegatePeerId = node.peerId.toString();
948
+ return relayDialAddrs.map((relayAddr) => superviseRelayReservation(node, [relayAddr], {
949
+ ...(announce && { beforeRedrive: () => announce(strandId, relayAddr, delegatePeerId) })
950
+ }));
951
+ }
952
+
953
+ /**
954
+ * Wait for every relay supervisor's first attempt to settle, concurrently, and log
955
+ * the ones that landed nothing. Never throws — a strand whose relay is unreachable
956
+ * at launch still comes up, with its supervisors retrying in the background.
957
+ */
958
+ private async awaitFirstRelayAttempts(strandId: string): Promise<void> {
959
+ const supervisors = this.relaySupervisors.get(strandId) ?? [];
960
+ if (supervisors.length === 0) {
961
+ return;
962
+ }
963
+ await Promise.all(supervisors.map((supervisor) => supervisor.firstAttempt));
964
+ supervisors.forEach((supervisor) => {
965
+ if (supervisor.lastError !== null) {
966
+ log('Strand %s: relay reservation not held after the first attempt (retrying in the background): %s',
967
+ strandId, supervisor.lastError);
968
+ }
969
+ });
970
+ }
971
+
647
972
  /**
648
973
  * Drop the strand's owned store and release this strand's claim on its cache. The
649
974
  * wrapper counts holders, so the cache is emptied and unregistered from the shared
@@ -694,6 +1019,60 @@ export class StrandInstanceManager {
694
1019
  await enforcer.refresh();
695
1020
  }
696
1021
 
1022
+ /**
1023
+ * Best-effort removal of THIS machine's own `Strand.MemberPeer` binding — the
1024
+ * self arm of `removeMemberPeer`, signed with the retained launch config's party
1025
+ * key. Called by `CadreNode.unpublishStrand` BEFORE the local stop, while the
1026
+ * strand database and transport are still live: after the unpublish commits, the
1027
+ * party's `StrandPartyKey` control row is gone, so no restart could ever sign
1028
+ * this removal again — the in-memory retained key is the last chance.
1029
+ *
1030
+ * Never throws. Quiet no-op for a strand that is untracked, open, or launched
1031
+ * without a party key; a quiesced (hibernating) strand, or a strand whose write
1032
+ * quorum is already unreachable, leaves the stale binding behind with a log —
1033
+ * it grants nothing today (admission is not allowlist-gated) and only
1034
+ * mis-credits diversity. The strand's own membership reconciler is stopped —
1035
+ * and its in-flight pass awaited — FIRST, so a pass that was already past its
1036
+ * own stopped check cannot re-register the binding this is about to clear.
1037
+ *
1038
+ * NOTE: deliberately NOT handled here (or anywhere yet): clearing the bindings
1039
+ * of a machine removed from the CADRE at the control layer, or of a party's
1040
+ * OTHER machines when the party unpublishes — the party still holds the key on
1041
+ * every surviving machine and can clear from any of them, and the stale rows
1042
+ * grant nothing while admission is deny-list only. Revisit when
1043
+ * `feat-strand-member-allowlist-admission` lands and a stale binding starts
1044
+ * granting admission rather than merely mis-crediting diversity.
1045
+ */
1046
+ async clearOwnMemberPeerBinding(strandId: string): Promise<void> {
1047
+ const instance = this.instances.get(strandId);
1048
+ const config = this.launchConfigs.get(strandId);
1049
+ if (!instance || !config || config.strandRow.Type !== 'c' || !config.partyMemberPrivateKey) {
1050
+ return;
1051
+ }
1052
+ const reconciler = this.membershipReconcilers.get(strandId);
1053
+ if (reconciler) {
1054
+ reconciler.stop();
1055
+ await reconciler.settle();
1056
+ }
1057
+ const db = instance.database?.getDatabase();
1058
+ const peerId = instance.libp2pNode?.peerId.toString();
1059
+ if (!db || !peerId) {
1060
+ log('clearOwnMemberPeerBinding: strand %s has no live runtime — own MemberPeer binding left in place', strandId);
1061
+ return;
1062
+ }
1063
+ try {
1064
+ // Background write on the app's database: never join a transaction the app has open.
1065
+ await removeMemberPeer(db, {
1066
+ memberKeyPair: strandMemberKeyPair(config.partyMemberPrivateKey),
1067
+ peerId
1068
+ }, { joinOpenTransaction: false });
1069
+ log('clearOwnMemberPeerBinding: strand %s — own binding for peer %s cleared', strandId, peerId);
1070
+ } catch (error) {
1071
+ log('clearOwnMemberPeerBinding: strand %s — best-effort clear failed (strand may already be unreachable): %o',
1072
+ strandId, error);
1073
+ }
1074
+ }
1075
+
697
1076
  /**
698
1077
  * Quiesce a strand: release its strand-network resources (stop the libp2p node,
699
1078
  * close the StrandDatabase) while RETAINING the instance record — identity,
@@ -797,10 +1176,22 @@ export class StrandInstanceManager {
797
1176
  * database), so the bootstrap could not run here: the CALLER must wake the
798
1177
  * strand (`CadreNode.wakeStrand`, which owns the hibernation bookkeeping this
799
1178
  * manager does not) so the rebuild — which now founds — runs it.
1179
+ * @param resolvePartyKey - Asked for the party's own membership key when (and only
1180
+ * when) a CLOSED strand's retained config carries none — the instance was launched
1181
+ * as a joiner before its `StrandPartyKey` row existed or replicated. The resolved
1182
+ * key is retained alongside the founder flip so the bootstrap (now, or on the
1183
+ * caller's wake for `'needs-resume'`) can seat the founding Member/Manager.
800
1184
  * @throws when the strand is not tracked — this seam exists only for the
801
1185
  * tracked-instance launch path; an untracked id is a caller bug.
1186
+ * @throws whatever the live bootstrap throws (e.g. `PreSplitStrandIdentityError`),
1187
+ * after {@link withdrawFounderRequest} — so a retry re-attempts the founding rather
1188
+ * than resolving `'already-founder'` over an instance that never founded. A caller
1189
+ * whose `'needs-resume'` wake fails owes the same withdrawal.
802
1190
  */
803
- async foundExistingStrand(strandId: string): Promise<'already-founder' | 'bootstrapped' | 'needs-resume'> {
1191
+ async foundExistingStrand(
1192
+ strandId: string,
1193
+ resolvePartyKey?: () => Promise<string | undefined>
1194
+ ): Promise<'already-founder' | 'bootstrapped' | 'needs-resume'> {
804
1195
  const instance = this.instances.get(strandId);
805
1196
  const config = this.launchConfigs.get(strandId);
806
1197
  if (!instance || !config) {
@@ -809,16 +1200,151 @@ export class StrandInstanceManager {
809
1200
  if (config.founder === true) {
810
1201
  return 'already-founder';
811
1202
  }
1203
+ const partyMemberPrivateKey =
1204
+ config.partyMemberPrivateKey
1205
+ ?? (config.strandRow.Type === 'c' ? await resolvePartyKey?.() : undefined);
812
1206
  // A fresh object rather than mutating in place: startStrand retains the CALLER'S
813
1207
  // config object, which is not ours to rewrite.
814
- this.launchConfigs.set(strandId, { ...config, founder: true });
815
- if (!instance.database) {
1208
+ this.launchConfigs.set(strandId, { ...config, founder: true, partyMemberPrivateKey });
1209
+ if (!instance.database && !this.firstSyncGates.has(strandId)) {
816
1210
  return 'needs-resume';
817
1211
  }
818
- await this.ensureFounderBootstrap(strandId);
1212
+ try {
1213
+ await this.ensureFounderBootstrap(strandId);
1214
+ } catch (error) {
1215
+ this.withdrawFounderRequest(strandId);
1216
+ throw error;
1217
+ }
819
1218
  return 'bootstrapped';
820
1219
  }
821
1220
 
1221
+ /**
1222
+ * Whether `strandId` is launched but still waiting for its first sync — a joiner whose
1223
+ * runtime is up while its database is withheld (status `'syncing'`, or `'idle'` after
1224
+ * the hibernation manager's idle timer fired on it). `false` for a writable, quiesced,
1225
+ * or untracked strand. The predicate {@link whenWritable}'s callers gate on.
1226
+ */
1227
+ isAwaitingFirstSync(strandId: string): boolean {
1228
+ const instance = this.instances.get(strandId);
1229
+ return instance !== undefined && isAwaitingFirstSync(instance);
1230
+ }
1231
+
1232
+ /**
1233
+ * Resolve once the strand's database is published to the app — immediately for a
1234
+ * writable strand — or reject with {@link StrandAwaitingFirstSyncError} after
1235
+ * `timeoutMs` (default: the retained launch config's `firstSync.timeoutMs`, else
1236
+ * {@link DEFAULT_STRAND_FIRST_SYNC_TIMEOUT_MS}). The rejection is RETRYABLE: nothing is
1237
+ * torn down, the gate keeps probing, and a later call waits afresh. A quiesced
1238
+ * (hibernating) strand is not woken here — the wait spans a resume, so a caller that
1239
+ * wants one wakes it (`CadreNode.wakeStrand`) and waits. Rejects immediately for an
1240
+ * untracked strand, and whenever the strand is stopped while a wait is pending.
1241
+ */
1242
+ whenWritable(strandId: string, options?: { timeoutMs?: number }): Promise<StrandInstance> {
1243
+ const instance = this.instances.get(strandId);
1244
+ if (!instance) {
1245
+ return Promise.reject(new Error(`Cannot wait for strand ${strandId} to become writable: not tracked`));
1246
+ }
1247
+ if (instance.database) {
1248
+ return Promise.resolve(instance);
1249
+ }
1250
+ const timeoutMs = options?.timeoutMs
1251
+ ?? this.launchConfigs.get(strandId)?.firstSync?.timeoutMs
1252
+ ?? DEFAULT_STRAND_FIRST_SYNC_TIMEOUT_MS;
1253
+ const startedAt = Date.now();
1254
+ return new Promise<StrandInstance>((resolve, reject) => {
1255
+ const waiters = this.writableWaiters.get(strandId) ?? new Set<WritableWaiter>();
1256
+ this.writableWaiters.set(strandId, waiters);
1257
+ const settle = (): void => {
1258
+ clearTimeout(timer);
1259
+ waiters.delete(waiter);
1260
+ if (waiters.size === 0) {
1261
+ this.writableWaiters.delete(strandId);
1262
+ }
1263
+ };
1264
+ const waiter: WritableWaiter = {
1265
+ resolve: () => { settle(); resolve(instance); },
1266
+ reject: (error) => { settle(); reject(error); }
1267
+ };
1268
+ const timer = setTimeout(
1269
+ () => waiter.reject(new StrandAwaitingFirstSyncError(strandId, Date.now() - startedAt)),
1270
+ timeoutMs);
1271
+ (timer as { unref?: () => void }).unref?.();
1272
+ waiters.add(waiter);
1273
+ });
1274
+ }
1275
+
1276
+ /**
1277
+ * Hand a launch's database to the app: set `instance.database`, retire the gate that
1278
+ * held it, release every {@link whenWritable} waiter, and — when the launch had already
1279
+ * been reported gated — flip it `'active'` and announce it through the retained config's
1280
+ * `onWritable`. "Reported gated" is any status but `'starting'`: `'syncing'`, or the
1281
+ * `'idle'` the hibernation manager's idle timer flips a long-gated joiner to (see
1282
+ * {@link isAwaitingFirstSync}) — the Header arriving after that flip must announce too,
1283
+ * or an app hanging on `strand:writable` never learns the strand opened. A publish that
1284
+ * lands while the instance is still `'starting'` (the Header arrived during the rest of
1285
+ * bring-up) announces nothing: `startStrand` / `resumeStrand` are about to report the
1286
+ * strand `'active'`, and `strand:writable` is defined as the follow-up to a gated
1287
+ * launch, never a duplicate of `strand:started`.
1288
+ *
1289
+ * It is also the one seam where a WITHHELD database becomes available, so it kicks the
1290
+ * membership reconciler: on a gated launch that loop's own immediate pass ran while
1291
+ * `instance.database` was still unset, found no database, and would otherwise not try
1292
+ * again until its retry timer fired. A launch that was never gated publishes here BEFORE
1293
+ * the reconciler is constructed, so there is nothing registered to kick and the loop's own
1294
+ * immediate pass already sees the database — which is why the kick needs no `wasGated`
1295
+ * test of its own.
1296
+ */
1297
+ private publishDatabase(instance: StrandInstance, database: StrandDatabase): void {
1298
+ const strandId = instance.strandId;
1299
+ const gate = this.firstSyncGates.get(strandId);
1300
+ if (gate) {
1301
+ gate.open();
1302
+ this.firstSyncGates.delete(strandId);
1303
+ }
1304
+ instance.database = database;
1305
+ instance.lastActivity = new Date();
1306
+ const wasGated = instance.status !== 'starting';
1307
+ if (wasGated) {
1308
+ instance.status = 'active';
1309
+ log('Strand %s is now writable: Strand.Header held', strandId);
1310
+ }
1311
+ const waiters = this.writableWaiters.get(strandId);
1312
+ if (waiters) {
1313
+ this.writableWaiters.delete(strandId);
1314
+ waiters.forEach((waiter) => waiter.resolve());
1315
+ }
1316
+ if (wasGated) {
1317
+ this.launchConfigs.get(strandId)?.onWritable?.(strandId);
1318
+ }
1319
+ // Not awaited: publishing must stay synchronous, and the pass chains on the loop's own
1320
+ // tail, so it cannot overlap a pass already in flight.
1321
+ void this.membershipReconcilers.get(strandId)?.reconcile();
1322
+ }
1323
+
1324
+ /** Reject every {@link whenWritable} waiter for a strand that is going away. */
1325
+ private rejectWritableWaiters(strandId: string, reason: string): void {
1326
+ const waiters = this.writableWaiters.get(strandId);
1327
+ if (!waiters) {
1328
+ return;
1329
+ }
1330
+ this.writableWaiters.delete(strandId);
1331
+ waiters.forEach((waiter) => waiter.reject(new Error(`Strand ${strandId} ${reason} before becoming writable`)));
1332
+ }
1333
+
1334
+ /**
1335
+ * Undo {@link foundExistingStrand}'s founder flip after the founding it promised
1336
+ * failed, so the retained config again says what this instance actually runs as (a
1337
+ * joiner) and the next founder request re-attempts the bootstrap. The resolved party
1338
+ * key stays retained — it is this party's identity for the strand either way. No-op
1339
+ * when the strand is untracked or its config does not found.
1340
+ */
1341
+ withdrawFounderRequest(strandId: string): void {
1342
+ const config = this.launchConfigs.get(strandId);
1343
+ if (config?.founder === true) {
1344
+ this.launchConfigs.set(strandId, { ...config, founder: false });
1345
+ }
1346
+ }
1347
+
822
1348
  /**
823
1349
  * Run the (idempotent) founder bootstrap against a tracked strand's LIVE
824
1350
  * database, independently of what the retained launch config says.
@@ -831,6 +1357,11 @@ export class StrandInstanceManager {
831
1357
  * and is the only thing that makes "founding resolves once the Header is
832
1358
  * written" true on that path.
833
1359
  *
1360
+ * A strand still `'syncing'` (its database withheld behind the first-sync gate) is
1361
+ * founded through the gate: the bootstrap writes the Header this machine was waiting
1362
+ * to receive, so a successful run publishes the database and the strand goes
1363
+ * `'active'` — the one legitimate way a joiner launch becomes writable without a peer.
1364
+ *
834
1365
  * @throws when the strand is not tracked, or is still quiesced (no live
835
1366
  * database) — both mean the bootstrap did NOT run, which a founder request
836
1367
  * must never swallow.
@@ -840,13 +1371,21 @@ export class StrandInstanceManager {
840
1371
  if (!instance) {
841
1372
  throw new Error(`Cannot run the founder bootstrap for strand ${strandId}: not tracked`);
842
1373
  }
843
- if (!instance.database) {
1374
+ const gate = this.firstSyncGates.get(strandId);
1375
+ const database = instance.database ?? gate?.database;
1376
+ if (!database) {
844
1377
  throw new Error(
845
1378
  `Cannot run the founder bootstrap for strand ${strandId}: it is quiesced, so there ` +
846
1379
  'is no live database to write to — resume it first.'
847
1380
  );
848
1381
  }
849
- await instance.database.ensureFounderBootstrap();
1382
+ // Forward the RETAINED config's party key: a foundExistingStrand that flipped a
1383
+ // joiner launch to founder may have resolved a key the live database's captured
1384
+ // config (built at the original launch) never saw.
1385
+ await database.ensureFounderBootstrap(this.launchConfigs.get(strandId)?.partyMemberPrivateKey);
1386
+ if (!instance.database) {
1387
+ this.publishDatabase(instance, database);
1388
+ }
850
1389
  }
851
1390
 
852
1391
  /**
@@ -861,6 +1400,7 @@ export class StrandInstanceManager {
861
1400
 
862
1401
  log('Stopping strand instance: %s', strandId);
863
1402
  instance.status = 'stopping';
1403
+ this.rejectWritableWaiters(strandId, 'was stopped');
864
1404
 
865
1405
  try {
866
1406
  await this.releaseRuntime(instance);