@serfab/cadre-core 0.7.1 → 0.8.1

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 (166) hide show
  1. package/README.md +9 -8
  2. package/dist/authority-key.d.ts +41 -0
  3. package/dist/authority-key.js +52 -0
  4. package/dist/authority-key.js.map +1 -0
  5. package/dist/cadre-node.d.ts +770 -11
  6. package/dist/cadre-node.js +1900 -88
  7. package/dist/cadre-node.js.map +1 -1
  8. package/dist/canonical-datetime.d.ts +33 -0
  9. package/dist/canonical-datetime.js +38 -0
  10. package/dist/canonical-datetime.js.map +1 -0
  11. package/dist/canonical-json.d.ts +12 -0
  12. package/dist/canonical-json.js +33 -0
  13. package/dist/canonical-json.js.map +1 -0
  14. package/dist/control-cohort.d.ts +63 -0
  15. package/dist/control-cohort.js +80 -0
  16. package/dist/control-cohort.js.map +1 -0
  17. package/dist/control-database.d.ts +259 -5
  18. package/dist/control-database.js +541 -128
  19. package/dist/control-database.js.map +1 -1
  20. package/dist/control-formation-recorder.d.ts +89 -0
  21. package/dist/control-formation-recorder.js +139 -0
  22. package/dist/control-formation-recorder.js.map +1 -0
  23. package/dist/control-schema.d.ts +12 -0
  24. package/dist/control-schema.js +199 -0
  25. package/dist/control-schema.js.map +1 -0
  26. package/dist/control-stream.d.ts +54 -0
  27. package/dist/control-stream.js +104 -0
  28. package/dist/control-stream.js.map +1 -0
  29. package/dist/device-token.d.ts +50 -0
  30. package/dist/device-token.js +66 -0
  31. package/dist/device-token.js.map +1 -0
  32. package/dist/diagnostics/connection-path.d.ts +118 -0
  33. package/dist/diagnostics/connection-path.js +204 -0
  34. package/dist/diagnostics/connection-path.js.map +1 -0
  35. package/dist/diagnostics/webrtc-turn-tracker.d.ts +50 -0
  36. package/dist/diagnostics/webrtc-turn-tracker.js +196 -0
  37. package/dist/diagnostics/webrtc-turn-tracker.js.map +1 -0
  38. package/dist/hibernation-manager.d.ts +79 -0
  39. package/dist/hibernation-manager.js +173 -23
  40. package/dist/hibernation-manager.js.map +1 -1
  41. package/dist/index.d.ts +25 -4
  42. package/dist/index.js +59 -4
  43. package/dist/index.js.map +1 -1
  44. package/dist/key-store-file.d.ts +52 -0
  45. package/dist/key-store-file.js +194 -0
  46. package/dist/key-store-file.js.map +1 -0
  47. package/dist/key-store.d.ts +75 -0
  48. package/dist/key-store.js +66 -0
  49. package/dist/key-store.js.map +1 -0
  50. package/dist/peer-authorization.d.ts +26 -0
  51. package/dist/peer-authorization.js +40 -0
  52. package/dist/peer-authorization.js.map +1 -0
  53. package/dist/peer-record.d.ts +89 -0
  54. package/dist/peer-record.js +117 -0
  55. package/dist/peer-record.js.map +1 -0
  56. package/dist/push-credentials.d.ts +34 -0
  57. package/dist/push-credentials.js +81 -0
  58. package/dist/push-credentials.js.map +1 -0
  59. package/dist/push-fanout.d.ts +137 -0
  60. package/dist/push-fanout.js +197 -0
  61. package/dist/push-fanout.js.map +1 -0
  62. package/dist/push-notifier-apns.d.ts +53 -0
  63. package/dist/push-notifier-apns.js +195 -0
  64. package/dist/push-notifier-apns.js.map +1 -0
  65. package/dist/push-notifier-fcm.d.ts +38 -0
  66. package/dist/push-notifier-fcm.js +183 -0
  67. package/dist/push-notifier-fcm.js.map +1 -0
  68. package/dist/push-notifier-shared.d.ts +18 -0
  69. package/dist/push-notifier-shared.js +25 -0
  70. package/dist/push-notifier-shared.js.map +1 -0
  71. package/dist/push-notifier.d.ts +71 -0
  72. package/dist/push-notifier.js +50 -0
  73. package/dist/push-notifier.js.map +1 -0
  74. package/dist/schema-verification.d.ts +11 -2
  75. package/dist/schema-verification.js +15 -4
  76. package/dist/schema-verification.js.map +1 -1
  77. package/dist/seed-bootstrap.d.ts +197 -9
  78. package/dist/seed-bootstrap.js +396 -137
  79. package/dist/seed-bootstrap.js.map +1 -1
  80. package/dist/seed-trust-policy.d.ts +63 -0
  81. package/dist/seed-trust-policy.js +56 -0
  82. package/dist/seed-trust-policy.js.map +1 -0
  83. package/dist/strand-addr-protocol.d.ts +132 -0
  84. package/dist/strand-addr-protocol.js +313 -0
  85. package/dist/strand-addr-protocol.js.map +1 -0
  86. package/dist/strand-cohort.d.ts +54 -0
  87. package/dist/strand-cohort.js +38 -0
  88. package/dist/strand-cohort.js.map +1 -0
  89. package/dist/strand-database.d.ts +54 -6
  90. package/dist/strand-database.js +88 -100
  91. package/dist/strand-database.js.map +1 -1
  92. package/dist/strand-formation-manager.d.ts +79 -19
  93. package/dist/strand-formation-manager.js +196 -104
  94. package/dist/strand-formation-manager.js.map +1 -1
  95. package/dist/strand-formation-protocol.d.ts +162 -0
  96. package/dist/strand-formation-protocol.js +279 -0
  97. package/dist/strand-formation-protocol.js.map +1 -0
  98. package/dist/strand-instance-manager.d.ts +73 -4
  99. package/dist/strand-instance-manager.js +167 -41
  100. package/dist/strand-instance-manager.js.map +1 -1
  101. package/dist/strand-member-key.d.ts +29 -0
  102. package/dist/strand-member-key.js +38 -0
  103. package/dist/strand-member-key.js.map +1 -0
  104. package/dist/strand-member-registry.d.ts +98 -0
  105. package/dist/strand-member-registry.js +129 -0
  106. package/dist/strand-member-registry.js.map +1 -0
  107. package/dist/strand-membership-writer.d.ts +361 -0
  108. package/dist/strand-membership-writer.js +449 -0
  109. package/dist/strand-membership-writer.js.map +1 -0
  110. package/dist/strand-solicitation.d.ts +77 -2
  111. package/dist/strand-solicitation.js +25 -4
  112. package/dist/strand-solicitation.js.map +1 -1
  113. package/dist/strand-wake-payload.d.ts +30 -0
  114. package/dist/strand-wake-payload.js +20 -0
  115. package/dist/strand-wake-payload.js.map +1 -0
  116. package/dist/strand-wake-protocol.d.ts +114 -0
  117. package/dist/strand-wake-protocol.js +257 -0
  118. package/dist/strand-wake-protocol.js.map +1 -0
  119. package/dist/strand-watcher.d.ts +6 -2
  120. package/dist/strand-watcher.js +57 -17
  121. package/dist/strand-watcher.js.map +1 -1
  122. package/dist/types.d.ts +406 -23
  123. package/dist/types.js +22 -5
  124. package/dist/types.js.map +1 -1
  125. package/package.json +14 -8
  126. package/src/authority-key.ts +68 -0
  127. package/src/cadre-node.ts +2937 -948
  128. package/src/canonical-datetime.ts +39 -0
  129. package/src/canonical-json.ts +30 -0
  130. package/src/control-cohort.ts +103 -0
  131. package/src/control-database.ts +931 -383
  132. package/src/control-formation-recorder.ts +151 -0
  133. package/src/control-schema.ts +198 -0
  134. package/src/control-stream.ts +128 -0
  135. package/src/device-token.ts +89 -0
  136. package/src/diagnostics/connection-path.ts +277 -0
  137. package/src/diagnostics/webrtc-turn-tracker.ts +222 -0
  138. package/src/hibernation-manager.ts +197 -28
  139. package/src/index.ts +273 -79
  140. package/src/key-store-file.ts +203 -0
  141. package/src/key-store.ts +99 -0
  142. package/src/peer-authorization.ts +53 -0
  143. package/src/peer-record.ts +146 -0
  144. package/src/push-credentials.ts +84 -0
  145. package/src/push-fanout.ts +272 -0
  146. package/src/push-notifier-apns.ts +243 -0
  147. package/src/push-notifier-fcm.ts +221 -0
  148. package/src/push-notifier-shared.ts +28 -0
  149. package/src/push-notifier.ts +93 -0
  150. package/src/schema-verification.ts +18 -4
  151. package/src/seed-bootstrap.ts +464 -188
  152. package/src/seed-trust-policy.ts +107 -0
  153. package/src/strand-addr-protocol.ts +410 -0
  154. package/src/strand-cohort.ts +77 -0
  155. package/src/strand-database.ts +113 -141
  156. package/src/strand-formation-manager.ts +233 -142
  157. package/src/strand-formation-protocol.ts +391 -0
  158. package/src/strand-instance-manager.ts +207 -56
  159. package/src/strand-member-key.ts +39 -0
  160. package/src/strand-member-registry.ts +177 -0
  161. package/src/strand-membership-writer.ts +640 -0
  162. package/src/strand-solicitation.ts +93 -4
  163. package/src/strand-wake-payload.ts +32 -0
  164. package/src/strand-wake-protocol.ts +331 -0
  165. package/src/strand-watcher.ts +61 -17
  166. package/src/types.ts +1012 -582
@@ -1,15 +1,38 @@
1
1
  import debug from 'debug';
2
2
  import { toString as uint8ArrayToString, fromString as uint8ArrayFromString } from 'uint8arrays';
3
+ import { generateKeyPair, privateKeyToProtobuf, privateKeyFromProtobuf } from '@libp2p/crypto/keys';
4
+ import { peerIdFromString } from '@libp2p/peer-id';
3
5
  import { createLibp2pNode } from '@optimystic/db-p2p';
6
+ import { multiaddr } from '@multiformats/multiaddr';
7
+ import { DEFAULT_CHECKIN_WINDOW_MS } from './types.js';
8
+ import { sign } from '@optimystic/quereus-plugin-crypto';
9
+ import { authorityKeyFromLibp2p } from './authority-key.js';
10
+ import { DEFAULT_IDENTITY_KEY_ID } from './key-store.js';
11
+ import { ed25519PublicKeyB64FromPeerId } from './seed-bootstrap.js';
12
+ import { signPeerRecord, verifyPeerRecordSignature, isPeerRecordFresh, orderSignalingFirst, isSignalingAddr, currentMemberTrustPolicy, DEFAULT_PEER_RECORD_MAX_AGE_MS, DEFAULT_PEER_RECORD_HEARTBEAT_MS } from './peer-record.js';
13
+ import { signDeviceTokenRecord, verifyDeviceTokenSignature, isPushPlatform } from './device-token.js';
4
14
  import { StrandWatcher } from './strand-watcher.js';
5
15
  import { StrandInstanceManager } from './strand-instance-manager.js';
16
+ import { deriveCohortMembers, selectStrandMode } from './strand-cohort.js';
17
+ import { selectControlCohortDials, DEFAULT_CONTROL_COHORT_RECONCILE_MS, DEFAULT_CONTROL_COHORT_TARGET_DEGREE } from './control-cohort.js';
6
18
  import { EnrollmentService } from './enrollment.js';
7
19
  import { HibernationManager } from './hibernation-manager.js';
8
20
  import { ControlDatabase } from './control-database.js';
9
21
  import { SeedBootstrapService } from './seed-bootstrap.js';
10
22
  import { StrandSolicitationService } from './strand-solicitation.js';
23
+ import { StrandWakeService, dialWake } from './strand-wake-protocol.js';
24
+ import { StrandAddrService, collectStrandAddrs } from './strand-addr-protocol.js';
25
+ import { PushFanoutService } from './push-fanout.js';
26
+ import { summarizeConnectionPaths, classifyTransport } from './diagnostics/connection-path.js';
27
+ import { TurnRelayTracker } from './diagnostics/webrtc-turn-tracker.js';
11
28
  const log = debug('sereus:cadre:node');
12
29
  const timing = debug('sereus:cadre:timing');
30
+ /**
31
+ * Window (ms) within which a queued TURN-relay settlement is correlated to a
32
+ * `connection:open`. Generous relative to the tight async gap between an
33
+ * `RTCPeerConnection` reaching `connected` and libp2p surfacing the connection.
34
+ */
35
+ const TURN_CONSUME_WINDOW_MS = 1000;
13
36
  /**
14
37
  * CadreNode is the main entry point for a cadre member.
15
38
  * It manages:
@@ -26,10 +49,125 @@ export class CadreNode {
26
49
  this.strandWatcher = null;
27
50
  this.seedBootstrapService = null;
28
51
  this.strandSolicitationService = null;
29
- this.running = false;
52
+ this.strandWakeService = null;
53
+ /**
54
+ * Control-network strand-address responder. Answers a co-cadre sibling's
55
+ * on-demand request for this node's live strand-network multiaddrs so the
56
+ * sibling can seed a strand mesh from us — the read side of the seed path that
57
+ * stops conflating control addresses with strand seeding.
58
+ */
59
+ this.strandAddrService = null;
60
+ /**
61
+ * Server-side push-wake fan-out. Constructed by {@link start} only when
62
+ * `config.push` (FCM/APNs credentials) is present — without it the node behaves
63
+ * exactly as before (no notifier, no fan-out). Owns who/when to wake
64
+ * hibernating mobile peers on strand activity.
65
+ */
66
+ this.pushFanoutService = null;
67
+ /** Backing field for the {@link running} / {@link isRunning} getters. */
68
+ this._running = false;
69
+ /**
70
+ * In-flight {@link serviceWake} operations keyed by strandId. Coalesces
71
+ * concurrent on-demand wakes for the same strand into one runtime build + one
72
+ * window + one re-hibernate decision (a second caller joins the first's
73
+ * promise), complementing {@link HibernationManager}'s wake coalescing.
74
+ */
75
+ this.serviceWakePromises = new Map();
76
+ /**
77
+ * Live wake-window waiters (see {@link holdWakeWindow}). Tracked so
78
+ * {@link cleanup} can clear the timer AND resolve the promise on teardown — a
79
+ * window must never fire (or hang an in-flight serviceWake) after stop().
80
+ */
81
+ this.windowWaiters = new Set();
30
82
  this.eventHandlers = new Map();
31
83
  /** Map of strandId -> sAppConfig for sAppId filtering and management */
32
84
  this.sAppConfigs = new Map();
85
+ /**
86
+ * Most-recently pushed invite addresses (see {@link setInviteAddresses}).
87
+ * When non-null these take priority over `libp2pNode.getMultiaddrs()` when
88
+ * minting invites — the host pushes NAT-resolved addresses here so the
89
+ * control-network node never needs to dial back to the manager.
90
+ */
91
+ this.latestInviteAddresses = null;
92
+ /** Initial self-registration timer (see {@link scheduleSelfRegistration}). */
93
+ this.selfRegistrationTimer = null;
94
+ /** TTL heartbeat that re-publishes the self record before it goes stale. */
95
+ this.recordRefreshTimer = null;
96
+ /** Listener that re-publishes the self record when reachable addresses change. */
97
+ this.selfPeerUpdateHandler = null;
98
+ /**
99
+ * Recurring proactive control-cohort dial cadence (see
100
+ * {@link reconcileControlCohort}). Wired alongside {@link recordRefreshTimer}
101
+ * in {@link startRecordRefresh} and torn down symmetrically in
102
+ * {@link stopRecordRefresh}; `.unref()`'d so it never keeps the loop alive.
103
+ */
104
+ this.controlCohortReconcileTimer = null;
105
+ /**
106
+ * Single-flight guard for {@link reconcileControlCohort}. The eager start pass,
107
+ * the recurring interval, and the `self:peer:update` trigger can fire close
108
+ * together; collapsing concurrent passes into one in-flight run prevents two
109
+ * passes from double-dialing the same siblings (mirrors {@link registerSelfInFlight}).
110
+ */
111
+ this.reconcileControlCohortInFlight = null;
112
+ /**
113
+ * Single-flight guard for {@link registerSelf}. Concurrent callers (the explicit
114
+ * CLI `--authority` publish, the 1s startup timer, the TTL heartbeat, and the
115
+ * address-change listener) share one in-flight publish so two of them can never
116
+ * both read "no row yet" and race a duplicate INSERT (a `CadrePeer` PK conflict).
117
+ */
118
+ this.registerSelfInFlight = null;
119
+ // ── Write-while-alone re-replication (control-write-ensure-replicated) ──────
120
+ /**
121
+ * Re-replication queue for authority control writes that committed while this
122
+ * node was alone — `controlNode.getConnections().length === 0` at write time
123
+ * means the Optimystic commit was local-only (the block's cluster was ≤1) and
124
+ * never broadcast. Maps the affected subject peerId → the write to re-issue once
125
+ * the cohort grows. A later op for the same subject overwrites an earlier one
126
+ * (authorize then remove → remove wins). Drained on the 0→≥1 control-connection
127
+ * transition by {@link drainPendingControlReplication}.
128
+ */
129
+ this.pendingPeerWrites = new Map();
130
+ /** This node's own `CadrePeer` self-write committed local-only (re-touched on growth). */
131
+ this.pendingSelfPeerWrite = false;
132
+ /** This node's own `DeviceToken` self-write committed local-only (re-touched on growth). */
133
+ this.pendingSelfDeviceWrite = false;
134
+ /**
135
+ * Guards the one-shot, first-cohort-growth reconstruction: an authority re-touches
136
+ * every membership row it may have authored that could be unreplicated (covering
137
+ * writes made before this process started, which the in-memory queue cannot know).
138
+ * Set true after the first drain so later passes only drain the in-memory queue.
139
+ */
140
+ this.reconstructedLocalOnlyWrites = false;
141
+ /** Single-flight guard for the re-replication drain (mirrors {@link registerSelfInFlight}). */
142
+ this.drainControlReplicationInFlight = null;
143
+ /**
144
+ * Tracks the control-connection presence edge so the drain fires only on the
145
+ * 0→≥1 transition (the earliest point a re-issue can broadcast), not on every
146
+ * subsequent `connection:open`. Re-armed to false once connections return to 0.
147
+ */
148
+ this.hasControlConnection = false;
149
+ /** `connection:open` listener driving the growth-triggered drain (teardown in {@link stopRecordRefresh}). */
150
+ this.controlConnectionOpenHandler = null;
151
+ /** `connection:close` listener re-arming the growth edge (teardown in {@link stopRecordRefresh}). */
152
+ this.controlConnectionCloseHandler = null;
153
+ // ── WebRTC TURN-relay detection (turn-relayed-path-metrics) ─────────────────
154
+ /**
155
+ * Hooks `globalThis.RTCPeerConnection` to observe whether ICE selected a TURN
156
+ * relay candidate for each WebRTC session. Installed in {@link start}, disposed
157
+ * in {@link cleanup}; inert on Node.js (no `RTCPeerConnection`).
158
+ */
159
+ this.turnTracker = new TurnRelayTracker();
160
+ /**
161
+ * Peer IDs whose current WebRTC connection was observed to be TURN-relayed.
162
+ * Populated by {@link handleTurnConnectionOpen} (drains the tracker queue on a
163
+ * `/webrtc` `connection:open`), cleared per-peer on `connection:close`. Read by
164
+ * {@link getConnectionPaths} to promote `webrtc` → `webrtc-turn` (relayed).
165
+ */
166
+ this.turnRelayedPeers = new Set();
167
+ /** `connection:open` listener feeding TURN detection (teardown in {@link stopRecordRefresh}). */
168
+ this.turnConnectionOpenHandler = null;
169
+ /** `connection:close` listener clearing a peer's TURN flag (teardown in {@link stopRecordRefresh}). */
170
+ this.turnConnectionCloseHandler = null;
33
171
  this.config = config;
34
172
  this.strandManager = new StrandInstanceManager();
35
173
  this.enrollmentService = new EnrollmentService();
@@ -37,7 +175,8 @@ export class CadreNode {
37
175
  const hibernationCallbacks = {
38
176
  onIdle: async (strandId) => this.handleStrandIdle(strandId),
39
177
  onHibernate: async (strandId) => this.handleStrandHibernate(strandId),
40
- onWake: async (strandId) => this.handleStrandWake(strandId)
178
+ onWake: async (strandId) => this.handleStrandWake(strandId),
179
+ onCheckIn: async (strandId) => this.handleStrandCheckIn(strandId)
41
180
  };
42
181
  this.hibernationManager = new HibernationManager(config.hibernation ?? { enabled: false }, hibernationCallbacks);
43
182
  log('CadreNode created for party: %s', config.controlNetwork.partyId);
@@ -54,6 +193,12 @@ export class CadreNode {
54
193
  get peerId() {
55
194
  return this.controlNode?.peerId;
56
195
  }
196
+ /**
197
+ * The party ID this node serves (control-network identity).
198
+ */
199
+ get partyId() {
200
+ return this.config.controlNetwork.partyId;
201
+ }
57
202
  /**
58
203
  * Get the multiaddrs of this node (available after start)
59
204
  */
@@ -66,7 +211,49 @@ export class CadreNode {
66
211
  * Check if the node is running
67
212
  */
68
213
  get isRunning() {
69
- return this.running;
214
+ return this._running;
215
+ }
216
+ /**
217
+ * Synchronous lifecycle snapshot for headless callers (a mobile
218
+ * `BackgroundRunner` that boots in a background task and must *query* state
219
+ * rather than subscribe to `control:connected`/`control:disconnected`).
220
+ * Equivalent to {@link isRunning}.
221
+ */
222
+ get running() {
223
+ return this._running;
224
+ }
225
+ /**
226
+ * Synchronous readiness snapshot: whether the control network is currently
227
+ * connected (the node is running and its control-network libp2p node is up).
228
+ * Tracks the same edge the `control:connected`/`control:disconnected` events
229
+ * announce, but pollable.
230
+ */
231
+ get controlConnected() {
232
+ return this._running && this.controlNode !== null;
233
+ }
234
+ /**
235
+ * Classify every open control-network connection as relayed
236
+ * (`/p2p-circuit`) vs direct, tag its transport, and summarise counts plus a
237
+ * stuck-on-relay condition. Pure, read-only snapshot over
238
+ * `controlNode.getConnections()`. Returns an empty (all-zero) summary when
239
+ * the node has not been started.
240
+ *
241
+ * @param settleWindowMs - grace period before a relayed connection with no
242
+ * direct sibling is considered stuck (default 10_000ms)
243
+ */
244
+ getConnectionPaths(settleWindowMs) {
245
+ const conns = this.controlNode?.getConnections() ?? [];
246
+ // Annotate each connection with the TURN-relay hint so a WebRTC session whose
247
+ // ICE selected a TURN candidate classifies as relayed/webrtc-turn rather than
248
+ // direct/webrtc (the multiaddr alone cannot reveal it).
249
+ const annotated = conns.map((c) => ({
250
+ remotePeer: c.remotePeer,
251
+ remoteAddr: c.remoteAddr,
252
+ direction: c.direction,
253
+ timeline: c.timeline,
254
+ turnRelayed: this.turnRelayedPeers.has(c.remotePeer.toString()),
255
+ }));
256
+ return summarizeConnectionPaths(annotated, settleWindowMs);
70
257
  }
71
258
  /**
72
259
  * Get all strand instances
@@ -90,13 +277,21 @@ export class CadreNode {
90
277
  * Start the cadre node
91
278
  */
92
279
  async start() {
93
- if (this.running) {
280
+ if (this._running) {
94
281
  log('CadreNode already running');
95
282
  return;
96
283
  }
97
284
  log('Starting CadreNode for party: %s', this.config.controlNetwork.partyId);
98
285
  try {
99
286
  const tTotal = performance.now();
287
+ // Install the WebRTC TURN-relay tracker BEFORE any libp2p bring-up, so it
288
+ // wraps globalThis.RTCPeerConnection before the control node can create one.
289
+ // Inert on Node.js (no RTCPeerConnection); disposed in cleanup().
290
+ this.turnTracker.install();
291
+ // Resolve the node identity (keyStore | privateKey | ephemeral) BEFORE any
292
+ // libp2p/network bring-up, so a misconfiguration or an access-denied secure
293
+ // store fails closed before a node is created.
294
+ await this.resolveIdentityKey();
100
295
  // Create the control network libp2p node
101
296
  let t0 = performance.now();
102
297
  this.controlNode = await this.createControlNode();
@@ -131,10 +326,53 @@ export class CadreNode {
131
326
  timing('[start] strandWatcher.start: %dms', Math.round(performance.now() - t0));
132
327
  // Start hibernation manager
133
328
  this.hibernationManager.start();
134
- this.running = true;
329
+ // Register the control-network push-wake receiver: a same-cadre peer can
330
+ // signal us to bring a hibernating strand online. Gated on CadrePeer
331
+ // membership; the wake routes through the same path as a local wake.
332
+ this.strandWakeService = new StrandWakeService({
333
+ isMember: (peerId) => this.isMember(peerId),
334
+ getStrand: (strandId) => this.strandManager.getInstance(strandId),
335
+ wake: (strandId) => this.wakeStrand(strandId),
336
+ });
337
+ this.strandWakeService.initialize(this.controlNode);
338
+ // Register the control-network strand-address responder: a same-cadre peer
339
+ // resolving a strand's bootstrap seed asks us for our live strand-network
340
+ // multiaddrs (its CadrePeer row only knows our *control* address). Gated on
341
+ // CadrePeer membership; answers only for strands we are actively meshing.
342
+ this.strandAddrService = new StrandAddrService({
343
+ isMember: (peerId) => this.isMember(peerId),
344
+ getStrandMultiaddrs: (strandId) => this.getStrandMultiaddrs(strandId),
345
+ });
346
+ this.strandAddrService.initialize(this.controlNode);
347
+ // Server-side push-wake fan-out: only when push credentials are configured.
348
+ // The PushNotifier reaches for node:http2/node:crypto, so it is loaded via a
349
+ // guarded DYNAMIC import — a cross-platform (RN/browser) node that never sets
350
+ // config.push never pulls those modules into its static graph. The fan-out
351
+ // service itself is import-clean (PushNotifier type only) and statically imported.
352
+ if (this.config.push) {
353
+ const { createPushNotifier } = await import('./push-notifier.js');
354
+ const notifier = createPushNotifier(this.config.push);
355
+ this.pushFanoutService = new PushFanoutService({
356
+ listMembers: () => this.listMembers(),
357
+ getStrand: (strandId) => this.strandManager.getInstance(strandId),
358
+ selfPeerId: () => this.controlNode?.peerId.toString(),
359
+ pushWake: (peerId, strandId, reason) => this.pushWake(peerId, strandId, reason),
360
+ resolveDeviceToken: (peerId) => this.resolveDeviceToken(peerId),
361
+ expireDeviceToken: (peerId) => this.expireDeviceToken(peerId),
362
+ notifier,
363
+ cooldownMs: this.config.push.cooldownMs,
364
+ debounceMs: this.config.push.debounceMs,
365
+ });
366
+ log('Push-wake fan-out enabled (FCM=%s, APNs=%s)', !!this.config.push.fcm, !!this.config.push.apns);
367
+ }
368
+ this._running = true;
135
369
  this.emit('control:connected', undefined);
136
370
  timing('[start] total: %dms', Math.round(performance.now() - tTotal));
137
371
  log('CadreNode started successfully');
372
+ // Wire the control-connection growth listeners immediately (not via the
373
+ // delayed refresh path) so no early `connection:open` is missed — the
374
+ // write-while-alone re-replication drain must fire on the first 0→≥1 edge.
375
+ this.wireControlConnectionListeners();
138
376
  // Schedule self-registration in background
139
377
  this.scheduleSelfRegistration();
140
378
  }
@@ -148,13 +386,13 @@ export class CadreNode {
148
386
  * Stop the cadre node
149
387
  */
150
388
  async stop() {
151
- if (!this.running) {
389
+ if (!this._running) {
152
390
  log('CadreNode not running');
153
391
  return;
154
392
  }
155
393
  log('Stopping CadreNode');
156
394
  await this.cleanup();
157
- this.running = false;
395
+ this._running = false;
158
396
  this.emit('control:disconnected', undefined);
159
397
  log('CadreNode stopped');
160
398
  }
@@ -183,8 +421,58 @@ export class CadreNode {
183
421
  }
184
422
  });
185
423
  }
424
+ /**
425
+ * Resolve the node identity into {@link identityKey} exactly once, fail-closed,
426
+ * before any network bring-up. Resolution order:
427
+ *
428
+ * 1. Both `keyStore` and `privateKey` set ⇒ configuration error (throws).
429
+ * 2. `keyStore` set ⇒ load protobuf bytes from `identityKeyId` (default
430
+ * {@link DEFAULT_IDENTITY_KEY_ID}). Found ⇒ deserialize; empty ⇒ generate a
431
+ * fresh Ed25519 key, persist it, and use it. A rejected `get` (access denied
432
+ * / backend failure) PROPAGATES — we never generate a new key on a read
433
+ * error, which would silently orphan the real identity.
434
+ * 3. `privateKey` set ⇒ use it directly.
435
+ * 4. Neither ⇒ leave undefined; libp2p generates an ephemeral key.
436
+ *
437
+ * Idempotent: a second call (or a stop()→start() cycle) reuses the already
438
+ * resolved key rather than regenerating or re-persisting.
439
+ */
440
+ async resolveIdentityKey() {
441
+ if (this.identityKey) {
442
+ return;
443
+ }
444
+ const { keyStore, privateKey, identityKeyId } = this.config;
445
+ if (keyStore && privateKey) {
446
+ throw new Error('CadreNodeConfig: `keyStore` and `privateKey` are mutually exclusive — ' +
447
+ 'configure at most one source for the node identity');
448
+ }
449
+ if (keyStore) {
450
+ const keyId = identityKeyId ?? DEFAULT_IDENTITY_KEY_ID;
451
+ // A rejection here (e.g. KeyStoreAccessError) must propagate — do NOT fall
452
+ // through to generation, which would orphan an existing but unreadable key.
453
+ const bytes = await keyStore.get(keyId);
454
+ if (bytes) {
455
+ // Corrupt/garbage bytes throw here; surface loudly rather than
456
+ // regenerating (which would orphan the real identity).
457
+ this.identityKey = privateKeyFromProtobuf(bytes);
458
+ log('Identity key loaded from key store (slot present)');
459
+ return;
460
+ }
461
+ const generated = await generateKeyPair('Ed25519');
462
+ await keyStore.set(keyId, privateKeyToProtobuf(generated));
463
+ this.identityKey = generated;
464
+ log('Identity key generated and persisted to key store (first run)');
465
+ return;
466
+ }
467
+ if (privateKey) {
468
+ this.identityKey = privateKey;
469
+ return;
470
+ }
471
+ // Neither configured: libp2p generates an ephemeral key internally.
472
+ }
186
473
  async createControlNode() {
187
- const { controlNetwork, network, storage, profile, privateKey } = this.config;
474
+ const { controlNetwork, network, storage, profile } = this.config;
475
+ const identityKey = this.identityKey;
188
476
  // Determine relay mode: if explicitly set in config, use that;
189
477
  // otherwise default to true for storage profile nodes (better connectivity/uptime)
190
478
  const enableRelay = network?.enableRelay ?? (profile === 'storage');
@@ -205,9 +493,10 @@ export class CadreNode {
205
493
  clusterSize: 3,
206
494
  clusterPolicy: { allowDownsize: true, sizeTolerance: 0.5 },
207
495
  arachnode: { enableRingZulu: this.config.profile === 'storage' },
208
- ...(privateKey && { privateKey }),
496
+ ...(identityKey && { privateKey: identityKey }),
209
497
  ...(network?.transports && { transports: network.transports }),
210
- ...(network?.listenAddrs && { listenAddrs: network.listenAddrs })
498
+ ...(network?.listenAddrs && { listenAddrs: network.listenAddrs }),
499
+ ...(network?.connectionGater && { connectionGater: network.connectionGater })
211
500
  };
212
501
  return await createLibp2pNode(nodeOptions);
213
502
  }
@@ -224,74 +513,1007 @@ export class CadreNode {
224
513
  };
225
514
  }
226
515
  /**
227
- * Schedule self-registration in the CadrePeer table.
228
- * This runs as a background task after the node is started.
516
+ * Schedule the node's initial self-record publish + ongoing refresh shortly
517
+ * after start (non-blocking). {@link registerSelf} is idempotent and safely
518
+ * no-ops when it cannot yet sign/insert (e.g. authority key not installed),
519
+ * so the timer is harmless even when registration only becomes possible later.
229
520
  */
230
521
  scheduleSelfRegistration() {
231
- // Run in background - don't block start()
232
- setTimeout(async () => {
522
+ this.selfRegistrationTimer = setTimeout(() => {
523
+ void (async () => {
524
+ try {
525
+ await this.registerSelf();
526
+ }
527
+ catch (error) {
528
+ // Background task — a failed publish must not crash the node.
529
+ log('Self-registration failed: %o', error);
530
+ }
531
+ // Wire the ongoing refresh + control-cohort reconcile cadence, then run
532
+ // an eager reconcile pass once the node has settled (the recurring
533
+ // interval was just armed inside startRecordRefresh).
534
+ this.startRecordRefresh();
535
+ void this.reconcileControlCohort().catch((error) => log('Control-cohort reconcile (start) failed: %o', error));
536
+ })();
537
+ }, 1000); // Small delay to ensure node is fully started
538
+ // Node-only: don't keep the event loop alive solely for this timer.
539
+ this.selfRegistrationTimer?.unref?.();
540
+ }
541
+ /**
542
+ * Publish (or refresh) this node's own signed `CadrePeer` address record so
543
+ * other members can resolve its current signaling/relay multiaddrs from its
544
+ * PeerId alone. Public, awaitable, and idempotent.
545
+ *
546
+ * - Builds a `PeerAddressRecord` from the node's current dialable addrs
547
+ * (signaling/`p2p-circuit` first), signed with the ed25519 key behind its
548
+ * PeerId (the resolved node identity from `keyStore`/`config.privateKey`).
549
+ * - If the row already exists: a self-signed UPDATE bumping `UpdatedAt`.
550
+ * - If not, and the node is its own authority: an authority-signed INSERT that
551
+ * also carries the self-signature.
552
+ * - Otherwise: logs and returns (a non-authority node with no row yet must
553
+ * wait for an authority to insert it; it can then self-refresh).
554
+ *
555
+ * Safe to call repeatedly (heartbeat / address-change driven); each successful
556
+ * publish strictly increases `UpdatedAt`. Concurrent calls are collapsed into a
557
+ * single in-flight publish (see {@link registerSelfInFlight}) so the explicit
558
+ * startup publish and the background timers can never race a duplicate INSERT.
559
+ *
560
+ * @returns what the publish did — `inserted`, `refreshed`, or `skipped`.
561
+ */
562
+ async registerSelf() {
563
+ // Join an in-flight publish rather than starting a second one. Without this,
564
+ // the explicit CLI call and the 1s timer could both observe "no row yet" and
565
+ // both attempt the INSERT — the loser hits a CadrePeer PK conflict.
566
+ if (this.registerSelfInFlight) {
567
+ return this.registerSelfInFlight;
568
+ }
569
+ const op = this.publishSelfRecord();
570
+ this.registerSelfInFlight = op;
571
+ try {
572
+ return await op;
573
+ }
574
+ finally {
575
+ this.registerSelfInFlight = null;
576
+ }
577
+ }
578
+ /** The body of {@link registerSelf}; serialised by its single-flight guard. */
579
+ async publishSelfRecord() {
580
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
581
+ log('Cannot register self - node or database not initialized');
582
+ return 'skipped';
583
+ }
584
+ const signingKey = this.getSelfSigningKey();
585
+ if (!signingKey) {
586
+ log('registerSelf: no self-signing key available (node identity unavailable or not matching peerId); skipping');
587
+ return 'skipped';
588
+ }
589
+ const peerId = this.controlNode.peerId.toString();
590
+ const addrs = await this.collectSelfAddrs();
591
+ const existing = await this.controlDatabase.queryPeerRecord(peerId);
592
+ // Strictly increase UpdatedAt even on a same-millisecond re-publish.
593
+ const updatedAt = Math.max(Date.now(), (existing?.updatedAt ?? 0) + 1);
594
+ const record = signPeerRecord({ peerId, publicKey: signingKey.publicKeyB64, addrs, updatedAt }, signingKey.privateKeyB64);
595
+ if (existing) {
596
+ await this.controlDatabase.updateSelfPeerRecord(record);
597
+ if (this.committedAlone())
598
+ this.pendingSelfPeerWrite = true;
599
+ log('registerSelf: refreshed own CadrePeer record (updatedAt=%d, %d addrs)', updatedAt, addrs.length);
600
+ return 'refreshed';
601
+ }
602
+ if (this.seedBootstrapService) {
603
+ // First-time row: requires an authority signature (the node is its own
604
+ // authority). insertSelfPeerRecord throws if no authority key is present.
605
+ await this.seedBootstrapService.insertSelfPeerRecord(record);
606
+ if (this.committedAlone())
607
+ this.pendingSelfPeerWrite = true;
608
+ log('registerSelf: inserted own CadrePeer record (authority-signed, updatedAt=%d, %d addrs)', updatedAt, addrs.length);
609
+ return 'inserted';
610
+ }
611
+ log('registerSelf: not yet a CadrePeer member and no authority service to self-insert; skipping (an authority must add this peer first)');
612
+ return 'skipped';
613
+ }
614
+ /**
615
+ * The ed25519 keypair (base64url) the node signs its own record with — the key
616
+ * behind its libp2p PeerId. Sourced from the resolved {@link identityKey}
617
+ * (which a `keyStore` or `config.privateKey` supplies); returns null when
618
+ * absent (ephemeral identity) or (defensively) when it does not match the
619
+ * control node's PeerId, in which case self-publish is skipped rather than
620
+ * producing an unresolvable row.
621
+ */
622
+ getSelfSigningKey() {
623
+ const peerId = this.controlNode?.peerId.toString();
624
+ if (!peerId || !this.identityKey) {
625
+ return null;
626
+ }
627
+ try {
628
+ const { privateKeyB64, publicKeyB64 } = authorityKeyFromLibp2p(this.identityKey);
629
+ if (publicKeyB64 === ed25519PublicKeyB64FromPeerId(peerId)) {
630
+ return { privateKeyB64, publicKeyB64 };
631
+ }
632
+ log('getSelfSigningKey: resolved identity key does not match control node peerId; cannot self-sign');
633
+ }
634
+ catch (error) {
635
+ // Logs the error shape only; authorityKeyFromLibp2p never embeds key material.
636
+ log('getSelfSigningKey: failed to derive ed25519 key from identity key: %o', error);
637
+ }
638
+ return null;
639
+ }
640
+ /**
641
+ * The authority keypair (base64url Ed25519) derived from this node's resolved
642
+ * identity key. In the single-key reference model the authority signing key is
643
+ * *derived from* the node identity (see {@link authorityKeyFromLibp2p}), so the
644
+ * same key material protected in a secure enclave backs both.
645
+ *
646
+ * Exposed so the hosting app retains control of authority genesis: cadre-core
647
+ * resolves + protects the identity, then the app sources this pair to drive
648
+ * `ensureAuthorityKey(pub)` + `initializeSeedBootstrap(priv)` itself — cadre-core
649
+ * never silently runs genesis. A future separate-authority slot would return a
650
+ * distinct key here instead of the identity-derived one.
651
+ *
652
+ * @returns The base64url seed/public-key authority pair.
653
+ * @throws If called before {@link start} has resolved the identity, or when the
654
+ * node runs on an ephemeral libp2p key (no `keyStore`/`privateKey` configured),
655
+ * since that key is internal to libp2p and not exposed.
656
+ */
657
+ getIdentityAuthorityKey() {
658
+ if (!this.identityKey) {
659
+ throw new Error('getIdentityAuthorityKey: node identity not resolved — call start() first, and ' +
660
+ 'configure `keyStore` or `privateKey` (an ephemeral libp2p identity exposes no authority key)');
661
+ }
662
+ return authorityKeyFromLibp2p(this.identityKey);
663
+ }
664
+ /**
665
+ * Collect this node's current dialable addresses for publication, signaling
666
+ * (`/p2p-circuit`) first. Prefers the best invite/NAT-resolved set and folds
667
+ * in the relay/signaling address (the WebRTC dial input) when not already
668
+ * present.
669
+ */
670
+ async collectSelfAddrs() {
671
+ const resolved = await this.resolveInviteAddresses();
672
+ const relay = await this.getRelayAddress();
673
+ const merged = relay && !resolved.includes(relay) ? [...resolved, relay] : resolved;
674
+ return orderSignalingFirst([...new Set(merged)]);
675
+ }
676
+ /**
677
+ * Wire the ongoing self-record refresh: re-publish whenever libp2p reports an
678
+ * address change (relay reservation rotation, NAT change) and on a TTL
679
+ * heartbeat at half the freshness ceiling. Idempotent — repeated calls do not
680
+ * stack listeners/timers.
681
+ */
682
+ startRecordRefresh() {
683
+ if (!this.controlNode || this.recordRefreshTimer) {
684
+ return;
685
+ }
686
+ const republish = (reason) => {
687
+ void this.registerSelf().catch((error) => log('Record refresh (%s) failed: %o', reason, error));
688
+ };
689
+ const reconcile = (reason) => {
690
+ void this.reconcileControlCohort().catch((error) => log('Control-cohort reconcile (%s) failed: %o', reason, error));
691
+ };
692
+ // Address churn re-publishes the self record AND re-checks the control cohort:
693
+ // a relay-reservation rotation / NAT change can drop a sibling connection, so
694
+ // the next pass should re-observe and re-dial it.
695
+ this.selfPeerUpdateHandler = () => {
696
+ republish('self:peer:update');
697
+ reconcile('self:peer:update');
698
+ };
699
+ this.controlNode.addEventListener('self:peer:update', this.selfPeerUpdateHandler);
700
+ this.recordRefreshTimer = setInterval(() => republish('heartbeat'), DEFAULT_PEER_RECORD_HEARTBEAT_MS);
701
+ this.recordRefreshTimer?.unref?.();
702
+ const reconcileMs = this.config.network?.controlCohort?.reconcileMs ?? DEFAULT_CONTROL_COHORT_RECONCILE_MS;
703
+ this.controlCohortReconcileTimer = setInterval(() => reconcile('interval'), reconcileMs);
704
+ this.controlCohortReconcileTimer?.unref?.();
705
+ log('Record refresh wired (heartbeat=%dms, cohortReconcile=%dms)', DEFAULT_PEER_RECORD_HEARTBEAT_MS, reconcileMs);
706
+ }
707
+ /** Tear down the self-record refresh timers + listener (see {@link cleanup}). */
708
+ stopRecordRefresh() {
709
+ if (this.selfRegistrationTimer) {
710
+ clearTimeout(this.selfRegistrationTimer);
711
+ this.selfRegistrationTimer = null;
712
+ }
713
+ if (this.recordRefreshTimer) {
714
+ clearInterval(this.recordRefreshTimer);
715
+ this.recordRefreshTimer = null;
716
+ }
717
+ if (this.controlCohortReconcileTimer) {
718
+ clearInterval(this.controlCohortReconcileTimer);
719
+ this.controlCohortReconcileTimer = null;
720
+ }
721
+ if (this.controlNode) {
722
+ if (this.selfPeerUpdateHandler) {
723
+ this.controlNode.removeEventListener('self:peer:update', this.selfPeerUpdateHandler);
724
+ }
725
+ if (this.controlConnectionOpenHandler) {
726
+ this.controlNode.removeEventListener('connection:open', this.controlConnectionOpenHandler);
727
+ }
728
+ if (this.controlConnectionCloseHandler) {
729
+ this.controlNode.removeEventListener('connection:close', this.controlConnectionCloseHandler);
730
+ }
731
+ if (this.turnConnectionOpenHandler) {
732
+ this.controlNode.removeEventListener('connection:open', this.turnConnectionOpenHandler);
733
+ }
734
+ if (this.turnConnectionCloseHandler) {
735
+ this.controlNode.removeEventListener('connection:close', this.turnConnectionCloseHandler);
736
+ }
737
+ }
738
+ this.selfPeerUpdateHandler = null;
739
+ this.controlConnectionOpenHandler = null;
740
+ this.controlConnectionCloseHandler = null;
741
+ this.turnConnectionOpenHandler = null;
742
+ this.turnConnectionCloseHandler = null;
743
+ // Reset the write-while-alone re-replication state so a stop()→start() cycle
744
+ // re-arms the growth edge and re-runs the first-growth reconstruction. Any
745
+ // queued local-only writes are moot once the node is torn down; authority
746
+ // inserts are re-covered by reconstruction on the next start, and the
747
+ // (already-loudly-logged) delete gap is tracked for follow-up.
748
+ this.hasControlConnection = false;
749
+ this.reconstructedLocalOnlyWrites = false;
750
+ this.pendingPeerWrites.clear();
751
+ this.pendingSelfPeerWrite = false;
752
+ this.pendingSelfDeviceWrite = false;
753
+ }
754
+ /**
755
+ * Resolve a peer's current, signed, trust-checkable multiaddrs from only its
756
+ * PeerId — the transport-agnostic input a NAT-to-NAT WebRTC (or any) dial path
757
+ * consumes, with no copy/paste of a relayed dial string.
758
+ *
759
+ * Reads the peer's `CadrePeer` record and gates it through, in order:
760
+ * 1. record present (else `[]`),
761
+ * 2. `publicKey <-> peerId` binding (the stored key's libp2p identity must be
762
+ * the requested peerId),
763
+ * 3. self-signature verifies against `publicKey`,
764
+ * 4. freshness — rejected once older than `maxAgeMs` (never a dead relay
765
+ * reservation),
766
+ * 5. the pluggable trust gate (`opts.trustPolicy`).
767
+ * Survivors are returned signaling (`/p2p-circuit`) first, filtered to
768
+ * signaling-only when requested, as parsed `Multiaddr`s (unparsable addrs
769
+ * dropped). Any gate failure yields an empty array rather than throwing.
770
+ */
771
+ async resolvePeerAddrs(peerId, opts = {}) {
772
+ if (!this.controlDatabase) {
773
+ throw new Error('CadreNode must be started before resolving peer addrs');
774
+ }
775
+ const record = await this.controlDatabase.queryPeerRecord(peerId);
776
+ if (!record) {
777
+ log('resolvePeerAddrs: no record for %s', peerId);
778
+ return [];
779
+ }
780
+ // publicKey <-> peerId binding: the stored key must be the one embedded in
781
+ // the requested Ed25519 peer id (also rejects a non-Ed25519 / missing key).
782
+ if (!record.publicKey || ed25519PublicKeyB64FromPeerId(peerId) !== record.publicKey) {
783
+ log('resolvePeerAddrs: publicKey does not match peerId for %s', peerId);
784
+ return [];
785
+ }
786
+ // Self-signature over (peerId, addrs, updatedAt).
787
+ if (!verifyPeerRecordSignature(record)) {
788
+ log('resolvePeerAddrs: signature verification failed for %s', peerId);
789
+ return [];
790
+ }
791
+ // Freshness: never hand back a dead relay reservation.
792
+ const maxAgeMs = opts.maxAgeMs ?? DEFAULT_PEER_RECORD_MAX_AGE_MS;
793
+ if (!isPeerRecordFresh(record.updatedAt, maxAgeMs, Date.now())) {
794
+ log('resolvePeerAddrs: record for %s is stale (updatedAt=%d, maxAgeMs=%d)', peerId, record.updatedAt, maxAgeMs);
795
+ return [];
796
+ }
797
+ // Pluggable trust gate (defaults to current-member).
798
+ const trustPolicy = opts.trustPolicy ?? currentMemberTrustPolicy();
799
+ const trusted = await trustPolicy.evaluate({
800
+ peerId,
801
+ publicKey: record.publicKey,
802
+ partyId: this.partyId,
803
+ record,
804
+ });
805
+ if (!trusted) {
806
+ log('resolvePeerAddrs: trust policy rejected %s', peerId);
807
+ return [];
808
+ }
809
+ // Order signaling-first (the on-record order was what we verified above),
810
+ // optionally restrict to signaling addrs, then parse — dropping any addr
811
+ // that does not parse as a multiaddr.
812
+ let addrs = orderSignalingFirst(record.addrs);
813
+ if (opts.signalingOnly) {
814
+ addrs = addrs.filter(isSignalingAddr);
815
+ }
816
+ return this.parseMultiaddrs(addrs);
817
+ }
818
+ /** Parse multiaddr strings, dropping (and logging) any that fail to parse. */
819
+ parseMultiaddrs(addrs) {
820
+ const out = [];
821
+ for (const addr of addrs) {
233
822
  try {
234
- await this.registerSelf();
823
+ out.push(multiaddr(addr));
235
824
  }
236
825
  catch (error) {
237
- log('Self-registration failed: %o', error);
238
- // Don't emit error event - this is a background task
239
- // The node can still function without self-registration
826
+ log('resolvePeerAddrs: dropping unparsable multiaddr %s: %o', addr, error);
240
827
  }
241
- }, 1000); // Small delay to ensure node is fully started
828
+ }
829
+ return out;
242
830
  }
831
+ // ============================================================================
832
+ // Proactive control-cohort dial
833
+ //
834
+ // The control collections only replicate once a party's nodes are
835
+ // transport-connected (so FRET seats each peer in the others' keyspace cohort)
836
+ // AND a write happens while that cohort has ≥2 members. There is no production
837
+ // mechanism that makes a party's control nodes actively connect to each other;
838
+ // the convergence test does it by hand with a manual dial(). reconcileControlCohort
839
+ // productionizes that: each node resolves its known siblings' control addresses
840
+ // and proactively dials a bounded, backbone-preferential set, re-observing and
841
+ // re-dialing dropped connections on each pass. See docs/architecture.md (Control
842
+ // Network) and control-cohort.ts for the selection policy.
843
+ // ============================================================================
243
844
  /**
244
- * Register this peer in the CadrePeer table.
245
- * This allows other cadre members to discover this node.
845
+ * Run one proactive control-cohort dial pass to keep this node connected to its
846
+ * cadre siblings (so the `CadreControl` collections form a replicating cohort).
847
+ * Public so the cohort-growth-driven re-replication path
848
+ * (`control-write-ensure-replicated`) and tests can trigger a pass on demand;
849
+ * normally driven by the eager start pass, the recurring interval, and
850
+ * `self:peer:update` (all wired in {@link startRecordRefresh}).
851
+ *
852
+ * Concurrent triggers collapse into a single in-flight pass
853
+ * (see {@link reconcileControlCohortInFlight}) so two passes never double-dial.
854
+ * Best-effort throughout: a failure to resolve/dial any one sibling is logged
855
+ * and the pass continues; the whole pass is a no-op when the node is alone.
246
856
  */
247
- async registerSelf() {
248
- if (!this.controlNode || !this.controlDatabase) {
249
- log('Cannot register self - node or database not initialized');
857
+ async reconcileControlCohort() {
858
+ if (this.reconcileControlCohortInFlight) {
859
+ return this.reconcileControlCohortInFlight;
860
+ }
861
+ const op = this.runReconcileControlCohort();
862
+ this.reconcileControlCohortInFlight = op;
863
+ try {
864
+ await op;
865
+ }
866
+ finally {
867
+ this.reconcileControlCohortInFlight = null;
868
+ }
869
+ }
870
+ /** Body of {@link reconcileControlCohort}; serialised by its single-flight guard. */
871
+ async runReconcileControlCohort() {
872
+ // Shutdown / not-yet-started guard (mirrors publishSelfRecord). A pass that
873
+ // fires after stop() began must early-return rather than touch a torn-down node.
874
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
875
+ return;
876
+ }
877
+ const selfPeerId = this.controlNode.peerId.toString();
878
+ // 1. Enumerate known siblings. This membership read is itself a pull-on-read
879
+ // that helps the CadrePeer table converge — a reader-only node converges
880
+ // purely by these reads.
881
+ const members = await this.listMembers();
882
+ const siblings = members.filter((m) => m.peerId !== selfPeerId);
883
+ if (siblings.length === 0) {
884
+ // Genuinely alone (cold start with no rows, or a solo cadre): nothing to
885
+ // dial. Must not throw or busy-loop.
886
+ return;
887
+ }
888
+ // Re-guard after the await: a stop() may have raced the membership read.
889
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
890
+ return;
891
+ }
892
+ // 2. Classify backbone (authority) members and select a bounded dial set.
893
+ const authorityKeys = await this.controlDatabase.getAuthorityKeys();
894
+ if (!this._running || !this.controlNode) {
895
+ return;
896
+ }
897
+ const targetDegree = this.config.network?.controlCohort?.targetDegree
898
+ ?? DEFAULT_CONTROL_COHORT_TARGET_DEGREE;
899
+ const { dials, cappedNonAuthority } = selectControlCohortDials(siblings, authorityKeys, targetDegree);
900
+ if (cappedNonAuthority > 0) {
901
+ // Don't silently bound coverage — surface what the out-degree cap dropped.
902
+ log('reconcileControlCohort: capped %d non-authority sibling(s) at targetDegree=%d', cappedNonAuthority, targetDegree);
903
+ }
904
+ // 3. Skip already-connected peers (no re-dial / churn for live connections).
905
+ const connected = new Set(this.controlNode.getConnections().map((c) => c.remotePeer.toString()));
906
+ // 4. Resolve + dial each selected, not-yet-connected sibling, best-effort.
907
+ let dialed = 0;
908
+ for (const sibling of dials) {
909
+ if (!this._running || !this.controlNode) {
910
+ return;
911
+ }
912
+ if (connected.has(sibling.peerId)) {
913
+ continue;
914
+ }
915
+ if (await this.dialControlSibling(sibling)) {
916
+ dialed++;
917
+ }
918
+ }
919
+ log('reconcileControlCohort: pass complete (siblings=%d, selected=%d, dialed=%d)', siblings.length, dials.length, dialed);
920
+ }
921
+ /**
922
+ * Resolve one sibling's control-network dial addresses and dial it, best-effort.
923
+ * Returns whether a dial was attempted (false when no address resolves).
924
+ *
925
+ * A per-peer failure (NAT, offline, relay down, connection-gater denial) is
926
+ * logged and swallowed so one unreachable sibling never aborts the pass —
927
+ * exactly like {@link SeedBootstrapService.applySeed}'s authority-dial loop. A
928
+ * failed dial is simply retried on the next pass.
929
+ */
930
+ async dialControlSibling(sibling) {
931
+ const controlNode = this.controlNode;
932
+ if (!controlNode) {
933
+ return false;
934
+ }
935
+ const addrs = await this.resolveControlDialAddrs(sibling.peerId);
936
+ if (addrs.length === 0) {
937
+ log('reconcileControlCohort: no dialable control address for sibling %s; skipping', sibling.peerId);
938
+ return false;
939
+ }
940
+ try {
941
+ log('reconcileControlCohort: dialing sibling %s (%d addr(s))', sibling.peerId, addrs.length);
942
+ await controlNode.dial(addrs);
943
+ return true;
944
+ }
945
+ catch (error) {
946
+ log('reconcileControlCohort: dial of sibling %s failed (continuing): %o', sibling.peerId, error);
947
+ return false;
948
+ }
949
+ }
950
+ /**
951
+ * Resolve a sibling's control-network dial addresses for the reconcile pass.
952
+ *
953
+ * Primary (steady state): the signed, fresh, trust-gated control addresses from
954
+ * the converged `CadrePeer` record via {@link resolvePeerAddrs}. Fallback (cold
955
+ * start): the libp2p peerStore entries `applySeed` populated, used only while the
956
+ * record is not yet resolvable. Returns `[]` (never throws) when neither yields
957
+ * an address — that sibling is skipped this pass.
958
+ */
959
+ async resolveControlDialAddrs(peerId) {
960
+ const resolved = await this.resolvePeerAddrs(peerId);
961
+ if (resolved.length > 0) {
962
+ return resolved;
963
+ }
964
+ return this.peerStoreAddrs(peerId);
965
+ }
966
+ /**
967
+ * Cold-start fallback: the libp2p peerStore multiaddrs for `peerId` (seeded by
968
+ * {@link SeedBootstrapService.applySeed}). Returns `[]` on a missing entry or any
969
+ * parse/lookup failure — never throws.
970
+ */
971
+ async peerStoreAddrs(peerId) {
972
+ if (!this.controlNode) {
973
+ return [];
974
+ }
975
+ try {
976
+ const peer = await this.controlNode.peerStore.get(peerIdFromString(peerId));
977
+ // Re-parse through the top-level multiaddr parser so the returned type matches
978
+ // resolvePeerAddrs (the peerStore bundles its own @multiformats/multiaddr copy).
979
+ return this.parseMultiaddrs(peer.addresses.map((a) => a.multiaddr.toString()));
980
+ }
981
+ catch (error) {
982
+ log('reconcileControlCohort: peerStore lookup for %s failed: %o', peerId, error);
983
+ return [];
984
+ }
985
+ }
986
+ // ============================================================================
987
+ // Write-while-alone re-replication (control-write-ensure-replicated)
988
+ //
989
+ // Optimystic's coordinator commits a control write WITHOUT broadcasting when the
990
+ // block's cluster has ≤1 member. An authority that authorizes/removes a peer or
991
+ // (re)publishes its own record while no sibling is connected therefore writes a
992
+ // row that exists ONLY in its local control DB — a sibling that connects later
993
+ // never observes it (pull-on-read routes to the block's cluster, which never
994
+ // learned of the write). reconcileControlCohort shrinks the alone window but
995
+ // cannot close it (a write in the instant before any sibling connects is still
996
+ // local-only). The remedy: detect "wrote while alone" (0 connections is a sound
997
+ // lower bound), queue the affected row, and RE-ISSUE the write once the cohort
998
+ // grows (0→≥1 connection), as an idempotent monotonic update that now broadcasts.
999
+ // See docs/architecture.md (Control Network → write-while-alone durability).
1000
+ // ============================================================================
1001
+ /**
1002
+ * Whether a control write happening now would commit local-only. A sound lower
1003
+ * bound is "no connected control peers": 0 connections ⇒ the block's cluster is
1004
+ * ≤1 ⇒ Optimystic commits without broadcasting. This is the pragmatic proxy for
1005
+ * the precise signal (the block's `getClusterSize`); it over-approximates safely
1006
+ * — a connected-but-not-in-this-block's-cluster write may still be local-only and
1007
+ * is caught by the cohort's periodic pull-on-read instead. Re-issuing an already-
1008
+ * replicated row is harmless (an idempotent monotonic bump), so the coarse proxy
1009
+ * is acceptable as the agreed first cut.
1010
+ */
1011
+ committedAlone() {
1012
+ return (this.controlNode?.getConnections().length ?? 0) === 0;
1013
+ }
1014
+ /**
1015
+ * Record (or clear) a just-committed authority membership write in the
1016
+ * write-while-alone re-replication queue. If the control node had no connections
1017
+ * at commit, the Optimystic write was local-only and must be re-issued once the
1018
+ * cohort grows; otherwise the write replicated and any stale queue entry for this
1019
+ * subject is dropped.
1020
+ *
1021
+ * A `remove` that commits alone is a security-relevant durability gap (a revoked
1022
+ * peer may persist as a member elsewhere), so it is logged loudly — and, because
1023
+ * a physical delete leaves no local trace to re-issue from, its on-growth
1024
+ * re-issue is best-effort only (see {@link drainPendingPeerWrites}).
1025
+ */
1026
+ noteControlWrite(peerId, kind) {
1027
+ if (!this.committedAlone()) {
1028
+ this.pendingPeerWrites.delete(peerId);
1029
+ return;
1030
+ }
1031
+ this.pendingPeerWrites.set(peerId, kind);
1032
+ if (kind === 'remove') {
1033
+ log('removePeer(%s) committed while ALONE (0 control connections): the deletion is ' +
1034
+ 'local-only, so a revoked peer may persist in the cohort until re-replication. The ' +
1035
+ 'on-growth re-issue is best-effort (a physical delete cannot be replayed without a ' +
1036
+ 'schema tombstone — see control-delete-while-alone-tombstone).', peerId);
1037
+ }
1038
+ else {
1039
+ log('authorizePeer(%s) committed while alone (0 control connections); queued for ' +
1040
+ 're-replication on cohort growth', peerId);
1041
+ }
1042
+ }
1043
+ /**
1044
+ * Drain the write-while-alone re-replication queue: re-issue the writes that
1045
+ * committed local-only now that the cohort can broadcast them. Public so the
1046
+ * 0→≥1 growth trigger ({@link handleControlConnectionChange}) and tests can drive
1047
+ * it; concurrent calls collapse into one in-flight drain
1048
+ * ({@link drainControlReplicationInFlight}) so two growth signals never
1049
+ * double-issue. Best-effort throughout — a per-row failure leaves that entry
1050
+ * queued for the next growth rather than aborting the drain.
1051
+ */
1052
+ async drainPendingControlReplication(reason) {
1053
+ if (this.drainControlReplicationInFlight) {
1054
+ return this.drainControlReplicationInFlight;
1055
+ }
1056
+ const op = this.runDrainControlReplication(reason);
1057
+ this.drainControlReplicationInFlight = op;
1058
+ try {
1059
+ await op;
1060
+ }
1061
+ finally {
1062
+ this.drainControlReplicationInFlight = null;
1063
+ }
1064
+ }
1065
+ /** Body of {@link drainPendingControlReplication}; serialised by its single-flight guard. */
1066
+ async runDrainControlReplication(reason) {
1067
+ // Shutdown / not-yet-started guard (mirrors runReconcileControlCohort).
1068
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
1069
+ return;
1070
+ }
1071
+ const firstGrowth = !this.reconstructedLocalOnlyWrites;
1072
+ log('Draining control re-replication (reason=%s, firstGrowth=%s, pendingPeers=%d, self=%s, deviceToken=%s)', reason, firstGrowth, this.pendingPeerWrites.size, this.pendingSelfPeerWrite, this.pendingSelfDeviceWrite);
1073
+ // 1. Self rows. On the first growth always re-touch (covers a self row carried
1074
+ // over from a prior process that committed local-only before this start);
1075
+ // afterwards only when a self-write-while-alone was recorded this session.
1076
+ // registerSelf is idempotent + single-flight, so this never races a
1077
+ // duplicate publish with the heartbeat republish.
1078
+ if (firstGrowth || this.pendingSelfPeerWrite) {
1079
+ await this.registerSelf().catch((error) => log('drain: registerSelf failed: %o', error));
1080
+ this.pendingSelfPeerWrite = false;
1081
+ }
1082
+ if (firstGrowth || this.pendingSelfDeviceWrite) {
1083
+ await this.retouchSelfDeviceToken();
1084
+ this.pendingSelfDeviceWrite = false;
1085
+ }
1086
+ // 2. First growth: an authority reconstructs membership rows it may have
1087
+ // authored that never replicated (covers writes from before this process
1088
+ // started). Rows already tracked in the in-memory queue are handled by
1089
+ // step 3 — skipped here to avoid a double re-touch.
1090
+ if (firstGrowth) {
1091
+ this.reconstructedLocalOnlyWrites = true;
1092
+ await this.reconstructAuthoredMembership();
1093
+ }
1094
+ // 3. In-session pending authority membership writes (authorize / remove).
1095
+ await this.drainPendingPeerWrites();
1096
+ }
1097
+ /**
1098
+ * Re-touch this node's own `DeviceToken` row (re-sign + bump `UpdatedAt` via
1099
+ * {@link registerDeviceToken}) so a self device-token that committed local-only
1100
+ * re-broadcasts on cohort growth. No-op when no row exists (nothing to
1101
+ * re-replicate) or the stored platform is unknown. Best-effort — never throws to
1102
+ * the drain.
1103
+ */
1104
+ async retouchSelfDeviceToken() {
1105
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
1106
+ return;
1107
+ }
1108
+ const peerId = this.controlNode.peerId.toString();
1109
+ try {
1110
+ const existing = await this.controlDatabase.queryDeviceToken(peerId);
1111
+ if (!existing || !isPushPlatform(existing.platform)) {
1112
+ return;
1113
+ }
1114
+ await this.registerDeviceToken(existing.platform, existing.token);
1115
+ }
1116
+ catch (error) {
1117
+ log('drain: retouch self DeviceToken failed: %o', error);
1118
+ }
1119
+ }
1120
+ /**
1121
+ * One-shot, first-cohort-growth reconstruction of the write-while-alone queue for
1122
+ * an AUTHORITY node: re-touch every membership row that may be an unreplicated
1123
+ * authority insert. A row is a candidate iff it is not self (handled by
1124
+ * {@link registerSelf}), is not already tracked in the in-memory queue (handled by
1125
+ * {@link drainPendingPeerWrites}), and carries no self-`Sig` yet (an
1126
+ * authority-authored row the peer has not self-published — the only kind safe to
1127
+ * bump without invalidating a self-signature). O(rows) on the small control
1128
+ * tables; safe to over-apply (a monotonic authority bump on an already-replicated
1129
+ * row is a no-op-equivalent).
1130
+ *
1131
+ * A node with no authority private key skips this entirely — it cannot re-sign
1132
+ * rows for other peers, and rows it merely holds are not its to re-issue. DELETEs
1133
+ * cannot be reconstructed here (a removed row leaves no local trace and the schema
1134
+ * carries no tombstone) — see the delete-path note in {@link drainPendingPeerWrites}.
1135
+ */
1136
+ async reconstructAuthoredMembership() {
1137
+ if (!this.controlDatabase || !this.controlNode) {
1138
+ return;
1139
+ }
1140
+ if (!this.seedBootstrapService?.canAuthorize()) {
1141
+ return;
1142
+ }
1143
+ const selfPeerId = this.controlNode.peerId.toString();
1144
+ const members = await this.controlDatabase.queryCadrePeers();
1145
+ let touched = 0;
1146
+ for (const member of members) {
1147
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
1148
+ return;
1149
+ }
1150
+ if (member.peerId === selfPeerId || this.pendingPeerWrites.has(member.peerId)) {
1151
+ continue;
1152
+ }
1153
+ try {
1154
+ const record = await this.controlDatabase.queryPeerRecord(member.peerId);
1155
+ // Missing (raced a delete) or peer-owned (self-Sig present) → not ours to bump.
1156
+ if (!record || record.sig) {
1157
+ continue;
1158
+ }
1159
+ await this.reissuePeerAuthorize(member.peerId, record.updatedAt);
1160
+ touched++;
1161
+ }
1162
+ catch (error) {
1163
+ log('drain: reconstruction re-touch of %s failed (continuing): %o', member.peerId, error);
1164
+ }
1165
+ }
1166
+ if (touched > 0) {
1167
+ log('drain: reconstructed %d authority-authored membership row(s) on first cohort growth', touched);
1168
+ }
1169
+ }
1170
+ /**
1171
+ * Drain the in-session write-while-alone queue: re-issue each pending authority
1172
+ * membership write now that the cohort can broadcast it. Sequential (no fan-out)
1173
+ * to avoid a thundering re-touch; an entry is cleared only on success, so a
1174
+ * failure (or a still-alone re-commit) leaves it queued for the next growth.
1175
+ *
1176
+ * - `authorize`: re-issue as an idempotent monotonic authority UPDATE
1177
+ * ({@link reissuePeerAuthorize}). Skipped (and cleared) if the row vanished
1178
+ * (raced a delete) or now carries a self-`Sig` (the peer self-published and owns
1179
+ * its republish).
1180
+ * - `remove` (security-relevant): a removePeer-while-alone physically removed the
1181
+ * row locally, so a re-issued DELETE matches nothing and does NOT propagate —
1182
+ * full delete durability requires a schema tombstone (deferred:
1183
+ * `control-delete-while-alone-tombstone`). We still attempt the re-issue
1184
+ * (harmless, and it propagates in the row-still-present case); the commit-alone
1185
+ * was already logged loudly in {@link noteControlWrite}.
1186
+ */
1187
+ async drainPendingPeerWrites() {
1188
+ if (this.pendingPeerWrites.size === 0) {
1189
+ return;
1190
+ }
1191
+ if (!this.seedBootstrapService?.canAuthorize()) {
1192
+ // A non-authority node cannot have authored these writes; drop any stray entries.
1193
+ this.pendingPeerWrites.clear();
1194
+ return;
1195
+ }
1196
+ for (const [peerId, kind] of [...this.pendingPeerWrites]) {
1197
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
1198
+ return;
1199
+ }
1200
+ try {
1201
+ if (kind === 'authorize') {
1202
+ const record = await this.controlDatabase.queryPeerRecord(peerId);
1203
+ if (!record || record.sig) {
1204
+ // Removed since, or peer self-published — nothing for the authority to re-issue.
1205
+ this.pendingPeerWrites.delete(peerId);
1206
+ continue;
1207
+ }
1208
+ await this.reissuePeerAuthorize(peerId, record.updatedAt);
1209
+ }
1210
+ else {
1211
+ // Best-effort delete re-issue (see method doc): propagates only if the row
1212
+ // is somehow still present locally.
1213
+ await this.seedBootstrapService.removePeer(peerId);
1214
+ }
1215
+ this.pendingPeerWrites.delete(peerId);
1216
+ }
1217
+ catch (error) {
1218
+ log('drain: re-issue of %s (%s) failed; leaving queued for next growth: %o', peerId, kind, error);
1219
+ }
1220
+ }
1221
+ }
1222
+ /**
1223
+ * Re-issue an authority membership row as a monotonic authority UPDATE: bump
1224
+ * `UpdatedAt` strictly above the stored value (and the wall clock) and re-sign via
1225
+ * the authority branch of `CadrePeer.AuthorizedUpdate`. The row already exists
1226
+ * locally (it committed there, just local-only), so this is an UPDATE, not the
1227
+ * original INSERT.
1228
+ */
1229
+ async reissuePeerAuthorize(peerId, currentUpdatedAt) {
1230
+ if (!this.seedBootstrapService) {
1231
+ return;
1232
+ }
1233
+ const updatedAt = Math.max(Date.now(), (currentUpdatedAt ?? 0) + 1);
1234
+ await this.seedBootstrapService.reauthorizePeer(peerId, updatedAt);
1235
+ }
1236
+ /**
1237
+ * On a control connection opening, detect the 0→≥1 transition and drain the
1238
+ * write-while-alone re-replication queue (single-flight; fires only on the edge,
1239
+ * not on every subsequent `connection:open`). Best-effort — a drain failure is
1240
+ * logged, not thrown.
1241
+ */
1242
+ handleControlConnectionChange() {
1243
+ if (!this._running || !this.controlNode) {
1244
+ return;
1245
+ }
1246
+ const connected = this.controlNode.getConnections().length > 0;
1247
+ if (!connected || this.hasControlConnection) {
1248
+ return;
1249
+ }
1250
+ this.hasControlConnection = true;
1251
+ log('Control cohort grew (0 → ≥1 connection); draining write-while-alone re-replication queue');
1252
+ void this.drainPendingControlReplication('connection:open')
1253
+ .catch((error) => log('Control re-replication drain (connection:open) failed: %o', error));
1254
+ }
1255
+ /** Re-arm the growth edge once all control connections drop, so a later reconnect re-drains. */
1256
+ handleControlConnectionClose() {
1257
+ if (!this.controlNode) {
1258
+ return;
1259
+ }
1260
+ if (this.controlNode.getConnections().length === 0) {
1261
+ this.hasControlConnection = false;
1262
+ }
1263
+ }
1264
+ /**
1265
+ * Wire the control-connection growth listeners that drive write-while-alone
1266
+ * re-replication. Wired in {@link start} (not the delayed refresh path) so no
1267
+ * early `connection:open` is missed; torn down in {@link stopRecordRefresh}.
1268
+ * Idempotent.
1269
+ */
1270
+ wireControlConnectionListeners() {
1271
+ if (!this.controlNode || this.controlConnectionOpenHandler) {
1272
+ return;
1273
+ }
1274
+ this.controlConnectionOpenHandler = () => this.handleControlConnectionChange();
1275
+ this.controlConnectionCloseHandler = () => this.handleControlConnectionClose();
1276
+ this.controlNode.addEventListener('connection:open', this.controlConnectionOpenHandler);
1277
+ this.controlNode.addEventListener('connection:close', this.controlConnectionCloseHandler);
1278
+ // TURN-relay detection: tag/untag peers whose WebRTC ICE used a TURN candidate.
1279
+ this.turnConnectionOpenHandler = (evt) => this.handleTurnConnectionOpen(evt.detail);
1280
+ this.turnConnectionCloseHandler = (evt) => this.handleTurnConnectionClose(evt.detail);
1281
+ this.controlNode.addEventListener('connection:open', this.turnConnectionOpenHandler);
1282
+ this.controlNode.addEventListener('connection:close', this.turnConnectionCloseHandler);
1283
+ }
1284
+ /**
1285
+ * On a control connection opening: if it is a WebRTC connection, drain the TURN
1286
+ * tracker for the just-settled ICE verdict and, when it relayed, mark the peer
1287
+ * so {@link getConnectionPaths} classifies it `webrtc-turn` (relayed). The
1288
+ * settlement↔open correlation is timing-based and best-effort; an unknown
1289
+ * verdict degrades to not-relayed. Never throws to the event loop.
1290
+ */
1291
+ handleTurnConnectionOpen(conn) {
1292
+ try {
1293
+ const addr = conn.remoteAddr?.toString() ?? '';
1294
+ // Only a genuine `/webrtc` (ICE) session can be TURN-relayed and promoted to
1295
+ // webrtc-turn. Use the classifier (which checks `/webrtc-direct` first) rather
1296
+ // than a bare substring — `'/webrtc-direct'.includes('/webrtc')` is true, and a
1297
+ // webrtc-direct open must not drain a settlement meant for a real webrtc dial.
1298
+ if (classifyTransport(addr).transport !== 'webrtc') {
1299
+ return;
1300
+ }
1301
+ if (this.turnTracker.consume(TURN_CONSUME_WINDOW_MS) === true) {
1302
+ const peerId = conn.remotePeer.toString();
1303
+ this.turnRelayedPeers.add(peerId);
1304
+ log('TURN-relayed WebRTC connection detected for peer %s', peerId);
1305
+ }
1306
+ }
1307
+ catch (error) {
1308
+ log('handleTurnConnectionOpen failed: %o', error);
1309
+ }
1310
+ }
1311
+ /** On a control connection closing, drop any TURN-relayed flag for its peer. */
1312
+ handleTurnConnectionClose(conn) {
1313
+ try {
1314
+ this.turnRelayedPeers.delete(conn.remotePeer.toString());
1315
+ }
1316
+ catch (error) {
1317
+ log('handleTurnConnectionClose failed: %o', error);
1318
+ }
1319
+ }
1320
+ // ============================================================================
1321
+ // Device-token registry (control-network push-token publish + resolve)
1322
+ //
1323
+ // The control network is the only network a hibernating peer keeps connected,
1324
+ // so it is where a mobile peer self-publishes its FCM/APNs push token and where
1325
+ // a server peer resolves it to deliver a push-wake to a suspended app. The write
1326
+ // + gate paths mirror registerSelf / resolvePeerAddrs (see DeviceToken in
1327
+ // control-schema.ts and device-token.ts).
1328
+ // ============================================================================
1329
+ /**
1330
+ * Publish (or refresh) this node's own self-signed `DeviceToken` row so a server
1331
+ * peer can resolve its FCM/APNs push token from its PeerId alone. Mirrors
1332
+ * {@link registerSelf}:
1333
+ *
1334
+ * - If the row already exists: a self-signed UPDATE bumping `UpdatedAt` (works
1335
+ * for any member — the `AuthorizedUpdate` self-branch verifies the new `Sig`
1336
+ * against the bound `CadrePeer.PublicKey`). Platform/Token may change here
1337
+ * (rotation / platform switch / reinstall are all normal self-updates).
1338
+ * - If not, and the node holds an authority service: an authority-signed INSERT
1339
+ * that also carries the self-signature.
1340
+ * - Otherwise: throws. Like `CadrePeer`, the first `DeviceToken` row requires an
1341
+ * authority signature; a non-authority peer (e.g. a phone) must have its row
1342
+ * seeded by an authority — typically the server it enrolled with — before it
1343
+ * can self-refresh. (Establishing that phone→server registration handshake is
1344
+ * the downstream "RN registration" ticket; this node only owns the cadre-core
1345
+ * write path.)
1346
+ *
1347
+ * `UpdatedAt` strictly increases on every publish (even a same-millisecond
1348
+ * re-publish), so a replayed older record is rejected by the schema.
1349
+ *
1350
+ * @param platform - `'fcm'` (Android/Firebase) or `'apns'` (Apple).
1351
+ * @param token - the opaque platform device/registration token.
1352
+ * @throws if the node is not started, exposes no self-signing key, or has no
1353
+ * existing row and no authority service to self-insert.
1354
+ */
1355
+ async registerDeviceToken(platform, token) {
1356
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
1357
+ throw new Error('CadreNode must be started before registering a device token');
1358
+ }
1359
+ const signingKey = this.getSelfSigningKey();
1360
+ if (!signingKey) {
1361
+ throw new Error('Cannot register device token: no self-signing key available ' +
1362
+ '(node identity unavailable or not matching the node PeerId).');
1363
+ }
1364
+ const peerId = this.controlNode.peerId.toString();
1365
+ const existing = await this.controlDatabase.queryDeviceToken(peerId);
1366
+ // Strictly increase UpdatedAt even on a same-millisecond re-publish (rotation).
1367
+ const updatedAt = Math.max(Date.now(), (existing?.updatedAt ?? 0) + 1);
1368
+ const record = signDeviceTokenRecord({ peerId, platform, token, updatedAt }, signingKey.privateKeyB64);
1369
+ if (existing) {
1370
+ await this.controlDatabase.updateSelfDeviceToken(record);
1371
+ if (this.committedAlone())
1372
+ this.pendingSelfDeviceWrite = true;
1373
+ log('registerDeviceToken: refreshed own DeviceToken (platform=%s, updatedAt=%d)', platform, updatedAt);
250
1374
  return;
251
1375
  }
1376
+ if (this.seedBootstrapService) {
1377
+ await this.seedBootstrapService.insertSelfDeviceToken(record);
1378
+ if (this.committedAlone())
1379
+ this.pendingSelfDeviceWrite = true;
1380
+ log('registerDeviceToken: inserted own DeviceToken (authority-signed, platform=%s, updatedAt=%d)', platform, updatedAt);
1381
+ return;
1382
+ }
1383
+ throw new Error(`Cannot register device token for ${peerId}: no existing row to self-update and no ` +
1384
+ 'authority service to self-insert. An authority must seed this peer\'s DeviceToken ' +
1385
+ 'row first (mirrors CadrePeer enrollment).');
1386
+ }
1387
+ /**
1388
+ * Resolve a cadre peer's FCM/APNs push token from only its PeerId — the input a
1389
+ * server's push-wake fan-out consumes to deliver a platform push to a suspended
1390
+ * app. Applies the same gating shape as {@link resolvePeerAddrs}, returning
1391
+ * `null` (never throwing) on any failure:
1392
+ *
1393
+ * 1. membership — the peer has a `CadrePeer` row with a `PublicKey`,
1394
+ * 2. `publicKey <-> peerId` binding — the stored key's libp2p identity is the
1395
+ * requested peerId,
1396
+ * 3. a `DeviceToken` row exists with a known {@link PushPlatform},
1397
+ * 4. self-signature verifies against the bound `CadrePeer.PublicKey`,
1398
+ * 5. freshness — `updatedAt` is positive and within `opts.maxAgeMs` (default:
1399
+ * no ceiling, since a push token is valid until it rotates).
1400
+ *
1401
+ * A peer that is not a current member, or whose token has no backing `CadrePeer`
1402
+ * record, resolves to `null` — a server must not attempt to push to a non-cadre
1403
+ * peer.
1404
+ */
1405
+ async resolveDeviceToken(peerId, opts = {}) {
1406
+ if (!this.controlDatabase) {
1407
+ throw new Error('CadreNode must be started before resolving a device token');
1408
+ }
1409
+ // Membership + publicKey<->peerId binding: read the peer's CadrePeer row and
1410
+ // confirm the stored key is the one embedded in the requested Ed25519 peer id.
1411
+ const peerRecord = await this.controlDatabase.queryPeerRecord(peerId);
1412
+ if (!peerRecord || !peerRecord.publicKey) {
1413
+ log('resolveDeviceToken: no CadrePeer/PublicKey for %s', peerId);
1414
+ return null;
1415
+ }
1416
+ if (ed25519PublicKeyB64FromPeerId(peerId) !== peerRecord.publicKey) {
1417
+ log('resolveDeviceToken: publicKey does not match peerId for %s', peerId);
1418
+ return null;
1419
+ }
1420
+ const record = await this.controlDatabase.queryDeviceToken(peerId);
1421
+ if (!record) {
1422
+ log('resolveDeviceToken: no device token for %s', peerId);
1423
+ return null;
1424
+ }
1425
+ if (!isPushPlatform(record.platform)) {
1426
+ log('resolveDeviceToken: unknown platform %s for %s', record.platform, peerId);
1427
+ return null;
1428
+ }
1429
+ // Self-signature over (peerId, platform, token, updatedAt), verified against the
1430
+ // CadrePeer.PublicKey bound to this peerId.
1431
+ if (!verifyDeviceTokenSignature(record, peerRecord.publicKey)) {
1432
+ log('resolveDeviceToken: signature verification failed for %s', peerId);
1433
+ return null;
1434
+ }
1435
+ // Freshness: a positive stamp, optionally bounded. No ceiling by default — a
1436
+ // suspended phone's token must stay resolvable for push-wake long after publish.
1437
+ const maxAgeMs = opts.maxAgeMs ?? Number.POSITIVE_INFINITY;
1438
+ if (!isPeerRecordFresh(record.updatedAt, maxAgeMs, Date.now())) {
1439
+ log('resolveDeviceToken: record for %s is stale (updatedAt=%d, maxAgeMs=%d)', peerId, record.updatedAt, maxAgeMs);
1440
+ return null;
1441
+ }
1442
+ return record;
1443
+ }
1444
+ /**
1445
+ * Delete this node's own `DeviceToken` row (logout / token invalidation). No-op
1446
+ * when no row exists. Like {@link registerDeviceToken}'s first insert, the delete
1447
+ * is gated on an authority signature (`DeviceToken.AuthorizedInsert` covers insert
1448
+ * AND delete), so it requires this node's authority service; a non-authority peer
1449
+ * must route the clear through its authority (downstream RN registration path).
1450
+ *
1451
+ * @throws if the node is not started, or a row exists but no authority service is
1452
+ * available to sign the delete.
1453
+ */
1454
+ async clearDeviceToken() {
1455
+ if (!this._running || !this.controlNode || !this.controlDatabase) {
1456
+ throw new Error('CadreNode must be started before clearing a device token');
1457
+ }
252
1458
  const peerId = this.controlNode.peerId.toString();
253
- const multiaddrs = this.controlNode.getMultiaddrs();
254
- if (multiaddrs.length === 0) {
255
- log('No multiaddrs available for self-registration');
256
- return;
257
- }
258
- // Use the first available multiaddr (typically the one with the peer ID)
259
- const multiaddr = multiaddrs[0].toString();
260
- log('Registering self: peerId=%s, multiaddr=%s', peerId, multiaddr);
261
- // TODO: This requires signing the update to satisfy the AuthorizedUpdate constraint
262
- // For now, we log what would happen. Full implementation requires:
263
- // 1. Either an authority key to authorize the insert
264
- // 2. Or the peer's private key to sign the update
265
- log('Self-registration requires authorization - skipping for now');
266
- // const db = this.controlDatabase.getDatabase();
267
- // await db.exec(
268
- // 'insert or replace into CadrePeer (PeerId, Multiaddr) values (?, ?)',
269
- // [peerId, multiaddr]
270
- // );
1459
+ const existing = await this.controlDatabase.queryDeviceToken(peerId);
1460
+ if (!existing) {
1461
+ log('clearDeviceToken: no DeviceToken row for %s; no-op', peerId);
1462
+ return;
1463
+ }
1464
+ if (!this.seedBootstrapService) {
1465
+ throw new Error(`Cannot clear device token for ${peerId}: delete requires an authority signature ` +
1466
+ 'and no authority service is initialized.');
1467
+ }
1468
+ await this.seedBootstrapService.deleteDeviceToken(peerId);
1469
+ log('clearDeviceToken: deleted DeviceToken for %s', peerId);
1470
+ }
1471
+ /**
1472
+ * Expire ANOTHER peer's stale `DeviceToken` after a platform reported it
1473
+ * unregistered during a push-wake fan-out. Unlike {@link clearDeviceToken}
1474
+ * (self-only — it hardcodes the local peerId), this takes an arbitrary peerId.
1475
+ *
1476
+ * - When this node holds an authority seed service, it deletes the row
1477
+ * (`deleteDeviceToken` is authority-gated and accepts any peerId), so the peer
1478
+ * is not retried until it re-registers.
1479
+ * - When this node is NOT an authority it cannot delete the row, so it only logs
1480
+ * that a re-registration is needed. The fan-out's own in-memory dead-token set
1481
+ * is what actually stops re-pushing to the dead token this process — see
1482
+ * {@link PushFanoutService}. That set is acceptably lossy across restarts (a
1483
+ * restart re-learns staleness on the next failed send).
1484
+ *
1485
+ * Best-effort: never throws to the (best-effort) fan-out caller.
1486
+ */
1487
+ async expireDeviceToken(peerId) {
1488
+ if (this.seedBootstrapService) {
1489
+ try {
1490
+ await this.seedBootstrapService.deleteDeviceToken(peerId);
1491
+ log('expireDeviceToken: authority-deleted stale DeviceToken for %s', peerId);
1492
+ }
1493
+ catch (error) {
1494
+ log('expireDeviceToken: authority delete for %s failed: %o', peerId, error);
1495
+ }
1496
+ return;
1497
+ }
1498
+ log('expireDeviceToken: %s token is stale but this node is not an authority; re-registration required', peerId);
271
1499
  }
272
1500
  async handleStrandAdded(strand) {
273
1501
  log('Handling strand added from control network: %s', strand.Id);
274
1502
  // Check if we have sApp config for this strand
275
1503
  const sAppConfig = this.sAppConfigs.get(strand.Id);
276
1504
  if (!sAppConfig) {
277
- log('No sAppConfig registered for strand %s - skipping auto-start', strand.Id);
278
- // Strand detected in control network but not yet configured via addStrand
279
- // This is normal - the app will call addStrand with the config
1505
+ log('No sAppConfig registered for strand %s - emitting strand:discovered', strand.Id);
1506
+ // Strand created by another member and not yet configured locally. Surface
1507
+ // it as a discovery event so the hosting app can decide whether to join
1508
+ // (register a config + addStrand); the strand-agnostic seam keeps this
1509
+ // class free of any app's join policy. A self-configured strand (config
1510
+ // already present) keeps auto-starting below, unchanged.
1511
+ this.emit('strand:discovered', { strandId: strand.Id, strand });
280
1512
  return;
281
1513
  }
282
1514
  try {
283
- const instance = await this.strandManager.startStrand({
284
- strandRow: strand,
285
- sAppConfig,
286
- storage: this.config.storage,
287
- network: this.config.network,
288
- profile: this.config.profile,
289
- defaultLatencyHint: this.config.hibernation?.defaultLatencyHint ?? 'interactive',
290
- privateKey: this.config.privateKey
291
- });
292
- // Register with hibernation manager
293
- this.hibernationManager.trackStrand(instance);
294
- this.emit('strand:started', { strandId: strand.Id });
1515
+ // Discovery path: no explicit mode — infer from cohort membership.
1516
+ await this.launchStrand(strand, sAppConfig);
295
1517
  }
296
1518
  catch (error) {
297
1519
  log('Error starting strand %s: %o', strand.Id, error);
@@ -320,6 +1542,16 @@ export class CadreNode {
320
1542
  }
321
1543
  }
322
1544
  async cleanup() {
1545
+ // Resolve + clear any in-flight wake windows first, so an in-flight check-in
1546
+ // or serviceWake unblocks and tears down cleanly rather than firing a stale
1547
+ // window timer (or hanging) after the strand manager is stopped below.
1548
+ this.clearWindowWaiters();
1549
+ // Stop self-record refresh timers + address-change listener (before the
1550
+ // control node is torn down, so removeEventListener has a live target).
1551
+ this.stopRecordRefresh();
1552
+ // Restore the wrapped globalThis.RTCPeerConnection and drop TURN-relay state.
1553
+ this.turnTracker.dispose();
1554
+ this.turnRelayedPeers.clear();
323
1555
  // Stop hibernation manager
324
1556
  this.hibernationManager.stop();
325
1557
  // Stop seed bootstrap service
@@ -327,6 +1559,22 @@ export class CadreNode {
327
1559
  await this.seedBootstrapService.shutdown();
328
1560
  this.seedBootstrapService = null;
329
1561
  }
1562
+ // Stop strand wake service (unregister the WAKE_PROTOCOL handler)
1563
+ if (this.strandWakeService) {
1564
+ await this.strandWakeService.shutdown();
1565
+ this.strandWakeService = null;
1566
+ }
1567
+ // Stop strand-addr service (unregister the STRAND_ADDR_PROTOCOL handler so a
1568
+ // restart does not hit DuplicateProtocolHandlerError).
1569
+ if (this.strandAddrService) {
1570
+ await this.strandAddrService.shutdown();
1571
+ this.strandAddrService = null;
1572
+ }
1573
+ // Tear down the push-wake fan-out (releases the notifier's APNs HTTP/2 session).
1574
+ if (this.pushFanoutService) {
1575
+ await this.pushFanoutService.close().catch((err) => log('Push fan-out close failed: %o', err));
1576
+ this.pushFanoutService = null;
1577
+ }
330
1578
  // Unregister strand solicitation service
331
1579
  if (this.strandSolicitationService && this.controlNode) {
332
1580
  this.strandSolicitationService.unregisterResponder(this.controlNode);
@@ -361,57 +1609,373 @@ export class CadreNode {
361
1609
  this.emit('strand:idle', { strandId });
362
1610
  }
363
1611
  }
1612
+ /**
1613
+ * Hibernate a strand: release its strand-network resources via the strand
1614
+ * manager (stop the libp2p node, close the StrandDatabase) and mark it
1615
+ * `hibernating`. A quiesced strand holds no open strand-network connections,
1616
+ * transports, or DB handles. No-ops if the strand is missing; if already
1617
+ * quiesced (defensive), just marks status and emits.
1618
+ */
364
1619
  async handleStrandHibernate(strandId) {
365
1620
  const instance = this.strandManager.getInstance(strandId);
366
- if (instance) {
1621
+ if (!instance) {
1622
+ log('handleStrandHibernate: strand %s not found', strandId);
1623
+ return;
1624
+ }
1625
+ if (!instance.libp2pNode && !instance.database) {
1626
+ // Already quiesced — only the status flag needs updating.
367
1627
  instance.status = 'hibernating';
368
- // Optionally disconnect libp2p to save resources
369
- // For now we keep it connected but could stop it here
370
- log('Strand %s transitioned to hibernating', strandId);
1628
+ log('Strand %s already quiesced; marked hibernating', strandId);
371
1629
  this.emit('strand:hibernating', { strandId });
1630
+ return;
372
1631
  }
1632
+ log('Hibernating strand %s — releasing strand-network resources', strandId);
1633
+ await this.strandManager.quiesceStrand(strandId);
1634
+ instance.status = 'hibernating';
1635
+ this.emit('strand:hibernating', { strandId });
1636
+ log('Strand %s hibernating (resources released)', strandId);
373
1637
  }
1638
+ /**
1639
+ * Wake a strand. If it was hibernating (quiesced — no libp2p node), re-resolve
1640
+ * the cohort discovery seed and mode exactly as `launchStrand` does and rebuild
1641
+ * its runtime via the strand manager. If it is still live (e.g. waking an idle
1642
+ * strand, which retains its resources), just flip the status. Overlapping wake
1643
+ * triggers are coalesced upstream by `HibernationManager`, so this runs once
1644
+ * per wake; `resumeStrand` is itself idempotent as a backstop.
1645
+ */
374
1646
  async handleStrandWake(strandId) {
375
1647
  const instance = this.strandManager.getInstance(strandId);
376
- if (instance) {
1648
+ if (!instance) {
1649
+ log('handleStrandWake: strand %s not found', strandId);
1650
+ return;
1651
+ }
1652
+ // Still live (idle wake, or defensive double-wake): no rebuild needed.
1653
+ if (instance.libp2pNode || instance.database) {
377
1654
  instance.status = 'active';
378
1655
  instance.lastActivity = new Date();
379
- log('Strand %s woke up', strandId);
1656
+ log('Strand %s woke (already live)', strandId);
380
1657
  this.emit('strand:waking', { strandId });
1658
+ return;
1659
+ }
1660
+ // Quiesced: re-resolve volatile cohort inputs (the seed may have grown, the
1661
+ // mode may have shifted bootstrap → networked) and rebuild the runtime.
1662
+ log('Waking strand %s — rebuilding strand-network resources', strandId);
1663
+ await this.resumeStrandRuntime(strandId);
1664
+ instance.lastActivity = new Date();
1665
+ this.emit('strand:waking', { strandId });
1666
+ log('Strand %s awake (resources rebuilt)', strandId);
1667
+ }
1668
+ /**
1669
+ * Rebuild a quiesced strand's runtime, re-resolving the volatile cohort inputs
1670
+ * first: the discovery seed may have grown and cohort membership may have
1671
+ * shifted the mode `bootstrap → networked` since the strand last ran. Shared by
1672
+ * the wake (`handleStrandWake`) and check-in (`handleStrandCheckIn`) paths so
1673
+ * both apply the same fresh seed/mode resolution. `resumeStrand` is idempotent
1674
+ * (returns the live instance unchanged) as a backstop against double-resume.
1675
+ */
1676
+ async resumeStrandRuntime(strandId) {
1677
+ const seed = await this.resolveCohortSeed(strandId);
1678
+ const mode = selectStrandMode(undefined, seed.hasOtherPeers);
1679
+ await this.strandManager.resumeStrand(strandId, {
1680
+ bootstrapNodes: seed.bootstrapNodes,
1681
+ mode
1682
+ });
1683
+ }
1684
+ /**
1685
+ * Real cohort check-in for a hibernating strand (the `onCheckIn` callback).
1686
+ *
1687
+ * Optimystic syncs pull-on-read, not on connect, and exposes no cheap
1688
+ * repo-level "pull pending" hook (`IRepo` is get/pend/commit/cancel only —
1689
+ * see the review handoff). So "query the cohort for pending activity" is
1690
+ * realized as a resume → bounded window → re-hibernate-if-idle cycle that
1691
+ * reuses the existing quiesce/resume primitives rather than a bespoke probe:
1692
+ *
1693
+ * 1. Resume the strand (rebuild node + db, re-resolve cohort seed/mode) so
1694
+ * its strand network can reach cohort peers — exactly as a wake does.
1695
+ * 2. Hold it resumed for a bounded window, during which the app may drive
1696
+ * reads (pull-on-read) and record activity.
1697
+ * 3. If activity was recorded during the window, leave the strand `active`
1698
+ * (the idle/hibernate timers + backoff reset take over). Otherwise
1699
+ * quiesce again and leave it `hibernating`, so `HibernationManager`
1700
+ * schedules the next, longer-delayed check-in.
1701
+ *
1702
+ * No-ops unless the strand is currently `hibernating` — a concurrent wake may
1703
+ * have already resumed it.
1704
+ */
1705
+ async handleStrandCheckIn(strandId) {
1706
+ const instance = this.strandManager.getInstance(strandId);
1707
+ if (!instance) {
1708
+ log('handleStrandCheckIn: strand %s not found', strandId);
1709
+ return;
1710
+ }
1711
+ if (instance.status !== 'hibernating') {
1712
+ log('handleStrandCheckIn: strand %s not hibernating (status=%s); skipping', strandId, instance.status);
1713
+ return;
1714
+ }
1715
+ try {
1716
+ // 1. Resume exactly as a wake does: re-resolve the (possibly grown) cohort
1717
+ // seed + mode, then rebuild the runtime.
1718
+ log('Check-in: resuming strand %s to probe the cohort for pending activity', strandId);
1719
+ await this.resumeStrandRuntime(strandId);
1720
+ // 2-3. Bounded window for the strand network to connect + the app to act,
1721
+ // then re-hibernate-if-idle. Shared with the on-demand serviceWake.
1722
+ const windowMs = this.config.hibernation?.checkInWindowMs ?? DEFAULT_CHECKIN_WINDOW_MS;
1723
+ await this.runWakeWindow(instance, windowMs);
1724
+ }
1725
+ catch (err) {
1726
+ // A check-in that throws part-way — resume failing on a flaky network (the
1727
+ // very scenario hibernation targets), the window rejecting, or quiesce
1728
+ // throwing — must leave the strand HIBERNATING, not in the `error` status
1729
+ // that `resumeStrand` sets on failure. `HibernationManager.runCheckIn`
1730
+ // decides wake-vs-escalate purely from `instance.status` after this
1731
+ // resolves: an `error` status reads as "woke", which STOPS the check-in
1732
+ // chain and strands the strand with no runtime and no future check-in.
1733
+ // Forcing it back to `hibernating` (after a best-effort quiesce to release
1734
+ // any partially-rebuilt runtime) makes the manager escalate the backoff and
1735
+ // retry on the next tick.
1736
+ log('Check-in failed for strand %s; re-hibernating to retry on backoff: %o', strandId, err);
1737
+ await this.strandManager.quiesceStrand(strandId).catch((cleanupErr) => {
1738
+ log('Check-in cleanup quiesce for strand %s failed: %o', strandId, cleanupErr);
1739
+ });
1740
+ instance.status = 'hibernating';
381
1741
  }
382
1742
  }
1743
+ /**
1744
+ * Window-then-decide for a just-resumed strand, shared by the check-in timer
1745
+ * path ({@link handleStrandCheckIn}) and the on-demand {@link serviceWake}:
1746
+ *
1747
+ * 1. Capture the post-resume activity marker. `recordActivity` assigns a
1748
+ * FRESH `Date`, so a changed reference after the window means real
1749
+ * activity landed during it — not millisecond-resolution noise.
1750
+ * 2. Hold the strand live for `windowMs` so its strand network reaches the
1751
+ * cohort and the app can drive pull-on-read activity.
1752
+ * 3. If activity landed, leave the strand `active` (return `true`); otherwise
1753
+ * quiesce and mark it `hibernating` again (return `false`).
1754
+ *
1755
+ * @returns whether activity was observed during the window (strand left active).
1756
+ */
1757
+ async runWakeWindow(instance, windowMs) {
1758
+ const strandId = instance.strandId;
1759
+ const activityMark = instance.lastActivity;
1760
+ await this.holdWakeWindow(instance, windowMs);
1761
+ const sawActivity = instance.lastActivity !== activityMark;
1762
+ if (sawActivity) {
1763
+ instance.status = 'active';
1764
+ this.emit('strand:waking', { strandId });
1765
+ log('Wake window: strand %s saw activity during the window; staying active', strandId);
1766
+ return true;
1767
+ }
1768
+ log('Wake window: no activity for strand %s; re-hibernating', strandId);
1769
+ await this.strandManager.quiesceStrand(strandId);
1770
+ instance.status = 'hibernating';
1771
+ return false;
1772
+ }
1773
+ /**
1774
+ * Hold a just-resumed strand live for `windowMs` (default
1775
+ * {@link DEFAULT_CHECKIN_WINDOW_MS} is applied by callers). A non-positive
1776
+ * window resolves immediately. The pending timer is tracked in
1777
+ * {@link windowWaiters} so {@link cleanup} can both clear it and resolve the
1778
+ * promise on teardown — a `stop()` during an in-flight window must neither fire
1779
+ * the timer afterward nor hang the awaiting check-in/serviceWake. Extracted as
1780
+ * its own method so tests can stub the wait (and inject activity during it).
1781
+ */
1782
+ async holdWakeWindow(_instance, windowMs) {
1783
+ if (windowMs <= 0)
1784
+ return;
1785
+ await new Promise((resolve) => {
1786
+ const waiter = { timer: undefined, resolve };
1787
+ waiter.timer = setTimeout(() => {
1788
+ this.windowWaiters.delete(waiter);
1789
+ resolve();
1790
+ }, windowMs);
1791
+ this.windowWaiters.add(waiter);
1792
+ });
1793
+ }
1794
+ /**
1795
+ * Clear every in-flight wake window: cancel its timer and resolve its promise
1796
+ * so any awaiting check-in/serviceWake completes promptly rather than hanging
1797
+ * past teardown. Called from {@link cleanup}.
1798
+ */
1799
+ clearWindowWaiters() {
1800
+ for (const waiter of this.windowWaiters) {
1801
+ clearTimeout(waiter.timer);
1802
+ waiter.resolve();
1803
+ }
1804
+ this.windowWaiters.clear();
1805
+ }
383
1806
  /**
384
1807
  * Add a strand with its sApp configuration.
385
1808
  * The hosting application must provide the sApp schema when creating a strand.
386
1809
  */
387
1810
  async addStrand(config) {
388
- if (!this.running) {
1811
+ if (!this._running) {
389
1812
  throw new Error('CadreNode not running');
390
1813
  }
391
- const { strandRow, sAppConfig, mode } = config;
1814
+ const { strandRow, sAppConfig, mode, founder } = config;
392
1815
  // Store sApp config for this strand
393
1816
  this.sAppConfigs.set(strandRow.Id, sAppConfig);
394
- log('Registered sAppConfig for strand %s (sApp: %s, mode: %s)', strandRow.Id, sAppConfig.id, mode ?? 'networked');
1817
+ log('Registered sAppConfig for strand %s (sApp: %s, mode: %s, founder: %s)', strandRow.Id, sAppConfig.id, mode ?? 'inferred', founder ?? false);
1818
+ // Pass `mode` (possibly undefined) through: an explicit mode wins, while a
1819
+ // caller that omits it gets the same cohort-inferred mode as the discovery path.
1820
+ // `founder` only flows from the explicit addStrand path — the control-discovered
1821
+ // join path never founds, so its rows arrive via sync (see handleStrandAdded).
1822
+ return await this.launchStrand(strandRow, sAppConfig, mode, founder);
1823
+ }
1824
+ /**
1825
+ * Publish a strand row to the shared control database under this node's own
1826
+ * authority identity, so other cadre members discover it via control-network
1827
+ * sync (their {@link StrandWatcher} fires `strand:discovered`).
1828
+ *
1829
+ * This is the authority-signed `Strand` INSERT that {@link addStrand}
1830
+ * deliberately omits: `addStrand` only starts the LOCAL strand instance,
1831
+ * whereas publishing makes the strand visible cadre-wide. A typical creator
1832
+ * does both (start locally + publish); a discovering peer only does
1833
+ * `addStrand` (the row already exists).
1834
+ *
1835
+ * The insert is signed with the ed25519 key behind this node's PeerId — which
1836
+ * {@link authorityKeyFromLibp2p} also exposes as the node's authority keypair,
1837
+ * so peer identity and authority key are one and the same. That key must be
1838
+ * enrolled in `AuthorityKey` (e.g. via {@link ControlDatabase.ensureAuthorityKey}
1839
+ * at genesis) or the schema's `Strand.Authorized` constraint rejects the write.
1840
+ * Failing loudly here is intentional: a silently-unpublished strand would run
1841
+ * as a local-only island that no peer could ever discover or join.
1842
+ *
1843
+ * @param strandId - Unique strand identifier (typically the same id passed to
1844
+ * {@link addStrand}).
1845
+ * @param type - `'o'` for open (default) or `'c'` for closed.
1846
+ * @param memberPrivateKey - Optional membership key for a closed strand.
1847
+ * @throws if the node is not started, exposes no authority signing key, or the
1848
+ * control DB rejects the (unauthorized) insert.
1849
+ */
1850
+ async publishStrand(strandId, type = 'o', memberPrivateKey) {
1851
+ if (!this._running || !this.controlDatabase) {
1852
+ throw new Error('CadreNode must be started before publishing a strand');
1853
+ }
1854
+ const signingKey = this.getSelfSigningKey();
1855
+ if (!signingKey) {
1856
+ throw new Error(`Cannot publish strand ${strandId}: no authority signing key available ` +
1857
+ '(node identity is unavailable or does not match the node PeerId). Run authority ' +
1858
+ 'genesis (ensureAuthorityKey + initializeSeedBootstrap) before publishing.');
1859
+ }
1860
+ // insertStrand hands this callback the canonical row-bound message BYTES (see
1861
+ // buildAuthorizationMessage); ed25519-sign them directly (no pre-hash) with
1862
+ // the authority private key, returning a base64url signature.
1863
+ const signMessage = (message) => sign(message, signingKey.privateKeyB64, 'ed25519', 'bytes', 'base64url', 'base64url');
1864
+ await this.controlDatabase.insertStrand(strandId, type, signingKey.publicKeyB64, signMessage, memberPrivateKey);
1865
+ log('Published strand %s (type %s) to control DB under authority %s', strandId, type, signingKey.publicKeyB64);
1866
+ }
1867
+ /**
1868
+ * Publish an authority-signed `FormationInvite` (open-invitation token) to the
1869
+ * shared control database, so a later {@link formStrand} redemption can be
1870
+ * validated against it (the consent branch of `Strand.Authorized`).
1871
+ *
1872
+ * Counterpart to {@link createOpenInvitation}, which only mints the
1873
+ * out-of-band {@link OpenInvitation} envelope: persisting the matching
1874
+ * `FormationInvite` row is what makes the token *redeemable* — the host's
1875
+ * {@link ControlFormationUsageRecorder} answers `isTokenValid`/`isTokenUsed`
1876
+ * from this row. A host minting a closed-strand invite does both (mint +
1877
+ * publish), exactly as the integration harness's `createInvitation` does.
1878
+ *
1879
+ * Signs with the same self-authority key as {@link publishStrand} (the ed25519
1880
+ * key behind this node's PeerId, which must be an enrolled `AuthorityKey`).
1881
+ * Throws loudly if the node isn't started or exposes no signing key.
1882
+ *
1883
+ * @param token - Invitation token (the `FormationInvite` primary key); use the
1884
+ * `token` of the {@link OpenInvitation} from {@link createOpenInvitation}.
1885
+ * @param sAppId - The sApp a redeemed strand will use.
1886
+ * @param options - Optional `expiresAtMs` (epoch ms), `totalUses`, `validationUrl`,
1887
+ * `strandId` (bind a closed/pre-existing host strand for provision-then-record).
1888
+ */
1889
+ async publishFormationInvite(token, sAppId, options = {}) {
1890
+ if (!this._running || !this.controlDatabase) {
1891
+ throw new Error('CadreNode must be started before publishing a formation invite');
1892
+ }
1893
+ const signingKey = this.getSelfSigningKey();
1894
+ if (!signingKey) {
1895
+ throw new Error(`Cannot publish formation invite ${token}: no authority signing key available ` +
1896
+ '(node identity is unavailable or does not match the node PeerId). Run authority ' +
1897
+ 'genesis (ensureAuthorityKey + initializeSeedBootstrap) before publishing.');
1898
+ }
1899
+ const signMessage = (message) => sign(message, signingKey.privateKeyB64, 'ed25519', 'bytes', 'base64url', 'base64url');
1900
+ await this.controlDatabase.insertFormationInvite(token, sAppId, signingKey.publicKeyB64, signMessage, options);
1901
+ log('Published formation invite %s (sApp %s) under authority %s', token, sAppId, signingKey.publicKeyB64);
1902
+ }
1903
+ /**
1904
+ * Shared strand launch path for both the explicit (`addStrand`) and the
1905
+ * control-discovered (`handleStrandAdded`) entry points. Resolves the cohort
1906
+ * seed, selects the mode, starts the strand, and registers it with the
1907
+ * hibernation manager before emitting `strand:started`.
1908
+ */
1909
+ async launchStrand(strand, sAppConfig, explicitMode, founder) {
1910
+ const seed = await this.resolveCohortSeed(strand.Id);
1911
+ const mode = selectStrandMode(explicitMode, seed.hasOtherPeers);
395
1912
  const instance = await this.strandManager.startStrand({
396
- strandRow,
1913
+ strandRow: strand,
397
1914
  sAppConfig,
398
1915
  storage: this.config.storage,
399
1916
  network: this.config.network,
400
1917
  profile: this.config.profile,
401
1918
  defaultLatencyHint: this.config.hibernation?.defaultLatencyHint ?? 'interactive',
402
- privateKey: this.config.privateKey,
403
- mode
1919
+ privateKey: this.identityKey,
1920
+ bootstrapNodes: seed.bootstrapNodes,
1921
+ mode,
1922
+ requireSignedSchemas: this.config.requireSignedSchemas,
1923
+ founder
404
1924
  });
405
- // Register with hibernation manager
406
1925
  this.hibernationManager.trackStrand(instance);
407
- this.emit('strand:started', { strandId: strandRow.Id });
1926
+ this.emit('strand:started', { strandId: strand.Id });
408
1927
  return instance;
409
1928
  }
1929
+ /**
1930
+ * Resolve a strand's discovery seed. Membership (and thus mode) comes from the
1931
+ * control network's CadrePeer rows; the **strand-network** bootstrap addresses
1932
+ * are resolved on demand over the control mesh via the strand-addr RPC —
1933
+ * deliberately NOT from `CadrePeer.Multiaddr`, which carries *control*
1934
+ * addresses that must not seed the strand mesh.
1935
+ *
1936
+ * Only siblings we already hold an open control connection to are RPC'd: they
1937
+ * are the ones that can answer right now, and dialing them by peerId reuses the
1938
+ * live connection. When no connected sibling yet runs the strand the seed is
1939
+ * empty (`[]`) — mode still follows membership, and the empty seed self-heals
1940
+ * on the next resume / check-in pass. Returns an empty seed when the control DB
1941
+ * or node is absent (not yet started / torn down).
1942
+ */
1943
+ async resolveCohortSeed(strandId) {
1944
+ if (!this.controlDatabase || !this.controlNode) {
1945
+ return { bootstrapNodes: [], hasOtherPeers: false };
1946
+ }
1947
+ const peers = await this.controlDatabase.queryCadrePeers();
1948
+ const { otherPeerIds, hasOtherPeers } = deriveCohortMembers(peers, this.controlNode.peerId.toString());
1949
+ // RPC only siblings we already have a control connection to — they can answer
1950
+ // now, and `dialProtocol` by peerId reuses the open connection.
1951
+ const connected = new Set(this.controlNode.getConnections().map((c) => c.remotePeer.toString()));
1952
+ const targets = otherPeerIds.filter((id) => connected.has(id));
1953
+ const bootstrapNodes = targets.length
1954
+ ? await collectStrandAddrs(this.controlNode, targets.map((peerId) => ({ peerId })), strandId)
1955
+ : [];
1956
+ return { bootstrapNodes, hasOtherPeers };
1957
+ }
1958
+ /**
1959
+ * The local strand instance's dialable strand-network multiaddrs for the
1960
+ * strand-addr RPC, ordered signaling-first (reusing the control node's
1961
+ * {@link orderSignalingFirst}). Returns `[]` when the strand is not running
1962
+ * locally or has no live libp2p node (hibernating / quiescing / never
1963
+ * participated) — a node only answers for a strand it is actively meshing,
1964
+ * regardless of the strand's mode. A `bootstrap`-mode first node still has a
1965
+ * live node, so it answers and a later sibling can dial in.
1966
+ */
1967
+ getStrandMultiaddrs(strandId) {
1968
+ const node = this.strandManager.getInstance(strandId)?.libp2pNode;
1969
+ if (!node) {
1970
+ return [];
1971
+ }
1972
+ return orderSignalingFirst(node.getMultiaddrs().map((ma) => ma.toString()));
1973
+ }
410
1974
  /**
411
1975
  * Remove a strand
412
1976
  */
413
1977
  async removeStrand(strandId) {
414
- if (!this.running) {
1978
+ if (!this._running) {
415
1979
  throw new Error('CadreNode not running');
416
1980
  }
417
1981
  // Untrack from hibernation
@@ -422,13 +1986,34 @@ export class CadreNode {
422
1986
  this.emit('strand:stopped', { strandId });
423
1987
  }
424
1988
  /**
425
- * Record activity on a strand (resets hibernation timer)
1989
+ * Record activity on a strand (resets hibernation timer).
1990
+ *
1991
+ * Also drives the server push-wake fan-out: whatever already drives activity on
1992
+ * this node's strand (its relay/app layer doing pull-on-read) additionally wakes
1993
+ * hibernating mobile peers — the same imperative seam local-wake uses, with no
1994
+ * new contract. No-op for the fan-out when push is not configured.
426
1995
  */
427
1996
  recordStrandActivity(strandId) {
428
1997
  const instance = this.strandManager.getInstance(strandId);
429
1998
  if (instance) {
430
1999
  this.hibernationManager.recordActivity(instance);
431
2000
  }
2001
+ this.notifyStrandActivity(strandId);
2002
+ }
2003
+ /**
2004
+ * Explicit fan-out trigger: an always-on host/relay/sApp calls this when it
2005
+ * observes activity for a strand this node participates in, to wake hibernating
2006
+ * mobile members over a direct control-network dial (falling back to FCM/APNs
2007
+ * for suspended phones). This is the supported, honest v1 trigger — Optimystic
2008
+ * exposes no passive repo-level "new transaction" hook to drive it automatically
2009
+ * (see the deferred passive-detector follow-up). No-op when push is not
2010
+ * configured; best-effort (never throws — the check-in wake is the backstop).
2011
+ *
2012
+ * @param strandId - the strand that saw activity.
2013
+ * @param reason - free-form cause hint carried in the wake (default `activity`).
2014
+ */
2015
+ notifyStrandActivity(strandId, reason) {
2016
+ void this.pushFanoutService?.notify(strandId, reason);
432
2017
  }
433
2018
  /**
434
2019
  * Force wake a hibernating strand
@@ -436,6 +2021,144 @@ export class CadreNode {
436
2021
  async wakeStrand(strandId) {
437
2022
  await this.hibernationManager.wakeStrand(strandId);
438
2023
  }
2024
+ // ============================================================================
2025
+ // Mobile background lifecycle primitives
2026
+ //
2027
+ // Imperative control a mobile BackgroundRunner drives from OS app-state and
2028
+ // push events, rather than from the internal idle/hibernate/check-in timers.
2029
+ // ============================================================================
2030
+ /**
2031
+ * Force a single strand to hibernate immediately, bypassing the idle/hibernate
2032
+ * timers — the background-entry path. No-op if the strand is realtime
2033
+ * (never-hibernate latency hint), already hibernating, or unknown.
2034
+ *
2035
+ * Routes through {@link HibernationManager.forceHibernate}, which cancels the
2036
+ * strand's pending idle/hibernate (and check-in) timers — so a stale timer
2037
+ * can't re-fire on or resurrect the strand — then runs the same `onHibernate`
2038
+ * path as the timer (`quiesceStrand` + `status='hibernating'` +
2039
+ * `strand:hibernating`). Unlike the timer path it does NOT re-arm check-ins:
2040
+ * the strand stays down until the caller drives a wake (e.g. {@link serviceWake}).
2041
+ */
2042
+ async hibernateStrand(strandId) {
2043
+ const instance = this.strandManager.getInstance(strandId);
2044
+ if (!instance) {
2045
+ log('hibernateStrand: strand %s unknown; no-op', strandId);
2046
+ return;
2047
+ }
2048
+ if (!this.hibernationManager.hibernates(instance)) {
2049
+ log('hibernateStrand: strand %s is realtime; no-op', strandId);
2050
+ return;
2051
+ }
2052
+ if (instance.status === 'hibernating') {
2053
+ log('hibernateStrand: strand %s already hibernating; no-op', strandId);
2054
+ return;
2055
+ }
2056
+ await this.hibernationManager.forceHibernate(instance);
2057
+ }
2058
+ /**
2059
+ * Force-hibernate every tracked strand whose latency hint is not realtime,
2060
+ * tolerating per-strand failure (one strand failing to quiesce never aborts
2061
+ * the others). Realtime strands are left running — the caller keeps the control
2062
+ * connection and realtime strands alive for as long as the OS permits.
2063
+ *
2064
+ * @returns the strandIds actually hibernated (now in `hibernating` status);
2065
+ * realtime strands are excluded.
2066
+ */
2067
+ async hibernateAll() {
2068
+ const hibernated = [];
2069
+ for (const [strandId, instance] of this.strandManager.getInstances()) {
2070
+ if (!this.hibernationManager.hibernates(instance)) {
2071
+ log('hibernateAll: skipping realtime strand %s', strandId);
2072
+ continue;
2073
+ }
2074
+ try {
2075
+ await this.hibernateStrand(strandId);
2076
+ if (instance.status === 'hibernating') {
2077
+ hibernated.push(strandId);
2078
+ }
2079
+ }
2080
+ catch (error) {
2081
+ // Collect-and-continue: a single strand's quiesce failure must not strand
2082
+ // the rest of the background-entry sweep.
2083
+ log('hibernateAll: strand %s failed to hibernate (continuing): %o', strandId, error);
2084
+ }
2085
+ }
2086
+ log('hibernateAll: hibernated %d strand(s)', hibernated.length);
2087
+ return hibernated;
2088
+ }
2089
+ /**
2090
+ * On-demand equivalent of a check-in cycle, for a push-delivered wake on
2091
+ * mobile: resume the strand, hold it live for `windowMs` so its strand network
2092
+ * reaches the cohort and the app can pull pending activity, then re-hibernate
2093
+ * if no activity was recorded (else leave it active).
2094
+ *
2095
+ * Idempotent / coalesced two ways: concurrent `serviceWake`s for the same
2096
+ * strand share one in-flight operation ({@link serviceWakePromises}), and the
2097
+ * underlying resume coalesces with a racing push-wake via
2098
+ * {@link HibernationManager}'s wake coalescing — one runtime build, one window,
2099
+ * one re-hibernate decision. Returns `{ serviced: false }` (never throws) when
2100
+ * the node is not running or the strand is unknown, and surfaces a resume
2101
+ * failure as `{ serviced: true, hadActivity: false }` after re-hibernating.
2102
+ *
2103
+ * @param strandId - the strand a push said has pending activity.
2104
+ * @param opts.windowMs - override the live-window duration (defaults to the
2105
+ * configured `checkInWindowMs` / {@link DEFAULT_CHECKIN_WINDOW_MS}).
2106
+ */
2107
+ async serviceWake(strandId, opts) {
2108
+ const existing = this.serviceWakePromises.get(strandId);
2109
+ if (existing) {
2110
+ log('serviceWake: joining in-flight wake for strand %s', strandId);
2111
+ return existing;
2112
+ }
2113
+ const op = this.runServiceWake(strandId, opts);
2114
+ this.serviceWakePromises.set(strandId, op);
2115
+ try {
2116
+ return await op;
2117
+ }
2118
+ finally {
2119
+ this.serviceWakePromises.delete(strandId);
2120
+ }
2121
+ }
2122
+ /** Body of {@link serviceWake}; serialised per-strand by its coalescing guard. */
2123
+ async runServiceWake(strandId, opts) {
2124
+ // Not-running / control-absent guard (mirrors pushWake): a background task
2125
+ // must get a branchable result, never a throw.
2126
+ if (!this._running || !this.controlNode) {
2127
+ log('serviceWake: node not running; not serviced (strand %s)', strandId);
2128
+ return { strandId, serviced: false, hadActivity: false };
2129
+ }
2130
+ const instance = this.strandManager.getInstance(strandId);
2131
+ if (!instance) {
2132
+ log('serviceWake: strand %s unknown to this node; not serviced', strandId);
2133
+ return { strandId, serviced: false, hadActivity: false };
2134
+ }
2135
+ // Already live (active or idle — both retain their runtime): servicing is a
2136
+ // no-op success. Do NOT run a window that would re-hibernate a strand the app
2137
+ // may be actively using, and do NOT rebuild a second runtime.
2138
+ if (instance.libp2pNode || instance.database) {
2139
+ log('serviceWake: strand %s already live; no-op success', strandId);
2140
+ return { strandId, serviced: true, hadActivity: true };
2141
+ }
2142
+ try {
2143
+ // Coalesced resume: routes through wakeStrand → HibernationManager.beginWake
2144
+ // so a racing push-wake shares this single runtime build.
2145
+ await this.wakeStrand(strandId);
2146
+ const windowMs = opts?.windowMs ?? this.config.hibernation?.checkInWindowMs ?? DEFAULT_CHECKIN_WINDOW_MS;
2147
+ const hadActivity = await this.runWakeWindow(instance, windowMs);
2148
+ return { strandId, serviced: true, hadActivity };
2149
+ }
2150
+ catch (error) {
2151
+ // Resume failing mid-window (network unreachable inside a Doze grant, etc.)
2152
+ // must not throw out of a background task: re-hibernate and report no
2153
+ // activity, mirroring handleStrandCheckIn's re-hibernate-on-error.
2154
+ log('serviceWake: strand %s failed during wake window; re-hibernating: %o', strandId, error);
2155
+ await this.strandManager.quiesceStrand(strandId).catch((cleanupErr) => {
2156
+ log('serviceWake cleanup quiesce for strand %s failed: %o', strandId, cleanupErr);
2157
+ });
2158
+ instance.status = 'hibernating';
2159
+ return { strandId, serviced: true, hadActivity: false };
2160
+ }
2161
+ }
439
2162
  /**
440
2163
  * Get the control network node (for advanced use)
441
2164
  */
@@ -476,9 +2199,8 @@ export class CadreNode {
476
2199
  this.seedBootstrapService = new SeedBootstrapService({
477
2200
  partyId: this.config.controlNetwork.partyId,
478
2201
  authorityPrivateKey,
479
- ...(this.config.network?.inviteAddressResolver
480
- ? { inviteAddressResolver: this.config.network.inviteAddressResolver }
481
- : {}),
2202
+ inviteAddressResolver: () => this.resolveInviteAddresses(),
2203
+ trustPolicy: this.config.seedTrustPolicy,
482
2204
  });
483
2205
  this.seedBootstrapService.setEventCallbacks({
484
2206
  onSeedReceived: (partyId, peerId) => this.emit('seed:received', { partyId, peerId }),
@@ -488,6 +2210,70 @@ export class CadreNode {
488
2210
  this.seedBootstrapService.initialize(this.controlNode, this.controlDatabase);
489
2211
  log('Seed bootstrap service initialized');
490
2212
  }
2213
+ /**
2214
+ * Push the multiaddrs that future invites should advertise. Pass `null` to
2215
+ * revert to the libp2p-reported addresses (the default). The host calls this
2216
+ * at spawn and on every NAT change.
2217
+ */
2218
+ setInviteAddresses(addresses) {
2219
+ this.latestInviteAddresses = addresses;
2220
+ log('Invite addresses updated: %s', addresses ? `${addresses.length} pushed` : 'cleared (libp2p fallback)');
2221
+ }
2222
+ /**
2223
+ * Resolve the addresses to embed in invites. Prefers pushed addresses, then
2224
+ * any config-supplied resolver, then the libp2p-observed multiaddrs.
2225
+ */
2226
+ async resolveInviteAddresses() {
2227
+ if (this.latestInviteAddresses !== null) {
2228
+ return this.latestInviteAddresses;
2229
+ }
2230
+ if (this.config.network?.inviteAddressResolver) {
2231
+ return this.config.network.inviteAddressResolver();
2232
+ }
2233
+ return this.getMultiaddrs();
2234
+ }
2235
+ /**
2236
+ * Enumerate the cadre's `CadrePeer` membership.
2237
+ */
2238
+ async listMembers() {
2239
+ if (!this.controlDatabase) {
2240
+ throw new Error('CadreNode must be started before listing members');
2241
+ }
2242
+ return this.controlDatabase.queryCadrePeers();
2243
+ }
2244
+ /**
2245
+ * Probe whether a given peer is a `CadrePeer` member.
2246
+ */
2247
+ async isMember(peerId) {
2248
+ const members = await this.listMembers();
2249
+ return members.some(m => m.peerId === peerId);
2250
+ }
2251
+ /**
2252
+ * Push-wake a hibernating cadre peer over the control network.
2253
+ *
2254
+ * Resolves the target's signed control-network address from its `CadrePeer`
2255
+ * record (via {@link resolvePeerAddrs}, signaling/relay first — so a NAT'd peer
2256
+ * is reachable through its circuit-relay address), dials `WAKE_PROTOCOL`, sends
2257
+ * the {@link WakeRequest}, and returns the peer's {@link WakeAck}. The receiver
2258
+ * gates the request on cadre membership and only resumes a strand it already
2259
+ * participates in.
2260
+ *
2261
+ * @param targetPeerId - The hibernating cadre peer to wake.
2262
+ * @param strandId - The strand the caller knows has pending activity.
2263
+ * @param reason - Optional cause hint, e.g. `"activity"` or `"manual"`.
2264
+ * @throws if the node is not started or the target has no dialable address.
2265
+ */
2266
+ async pushWake(targetPeerId, strandId, reason) {
2267
+ if (!this.controlNode) {
2268
+ throw new Error('CadreNode must be started before pushing wakes');
2269
+ }
2270
+ const addrs = await this.resolvePeerAddrs(targetPeerId);
2271
+ if (addrs.length === 0) {
2272
+ throw new Error(`No dialable control-network address for peer ${targetPeerId}`);
2273
+ }
2274
+ const request = { strandId, reason };
2275
+ return await dialWake(this.controlNode, addrs, request);
2276
+ }
491
2277
  /**
492
2278
  * Enable the seed listener for receiving seeds via the /sereus/seed/1.0.0 protocol.
493
2279
  * This is for drone nodes that need to receive seeds without being an authority.
@@ -505,9 +2291,8 @@ export class CadreNode {
505
2291
  this.seedBootstrapService = new SeedBootstrapService({
506
2292
  partyId: this.config.controlNetwork.partyId,
507
2293
  // No authority key - this node only receives seeds
508
- ...(this.config.network?.inviteAddressResolver
509
- ? { inviteAddressResolver: this.config.network.inviteAddressResolver }
510
- : {}),
2294
+ inviteAddressResolver: () => this.resolveInviteAddresses(),
2295
+ trustPolicy: this.config.seedTrustPolicy,
511
2296
  });
512
2297
  this.seedBootstrapService.setEventCallbacks({
513
2298
  onSeedReceived: (partyId, peerId) => this.emit('seed:received', { partyId, peerId }),
@@ -535,6 +2320,8 @@ export class CadreNode {
535
2320
  throw new Error('Seed bootstrap service not initialized. Call initializeSeedBootstrap() first.');
536
2321
  }
537
2322
  await this.seedBootstrapService.authorizePeer({ peerId, multiaddrs });
2323
+ // Queue for re-replication if this committed local-only (no connected cohort).
2324
+ this.noteControlWrite(peerId, 'authorize');
538
2325
  }
539
2326
  /**
540
2327
  * Remove a previously-authorized peer from the cadre.
@@ -547,6 +2334,8 @@ export class CadreNode {
547
2334
  throw new Error('Seed bootstrap service not initialized. Call initializeSeedBootstrap() first.');
548
2335
  }
549
2336
  await this.seedBootstrapService.removePeer(peerId);
2337
+ // Track + loudly flag a delete that committed local-only (security-relevant).
2338
+ this.noteControlWrite(peerId, 'remove');
550
2339
  }
551
2340
  /**
552
2341
  * Create a seed from the current control network state.
@@ -560,20 +2349,36 @@ export class CadreNode {
560
2349
  }
561
2350
  /**
562
2351
  * Apply a seed to populate the peer cache and enable connections.
563
- * Validates the seed signature before applying.
2352
+ *
2353
+ * Validates the seed signature, then evaluates a trust anchor for the signer
2354
+ * key (see `SeedTrustPolicy`). An enrollment caller can pass a per-seed
2355
+ * `trustPolicy` override — e.g. a `pinnedKeyTrustPolicy` built from a
2356
+ * `CadreInvite.authorityKeys` — so a cold-start node can accept its first
2357
+ * seed without reconfiguring the service.
564
2358
  */
565
- async applySeed(seed) {
2359
+ async applySeed(seed, options) {
566
2360
  if (!this.seedBootstrapService) {
567
- // Create a temporary service for applying seeds (doesn't need authority key)
2361
+ // Create a temporary service for applying seeds (doesn't need authority key).
2362
+ // partyId is the attacker-influenced seed.partyId — it only labels logs; the
2363
+ // trust decision rests solely on signerKey vs the anchor set (configured
2364
+ // default below, or the per-call options.trustPolicy override).
2365
+ //
2366
+ // This temp service is discarded after the call, so it must NOT own the shared
2367
+ // node's inbound seed handler: pass { registerHandler: false }. That keeps
2368
+ // repeated service-less applySeed/dialInvite idempotent (no handler leak, no
2369
+ // DuplicateProtocolHandlerError). The temp service still applies this seed; a
2370
+ // node that wants to RECEIVE inbound seeds needs a persistent service
2371
+ // (enableSeedListener / initializeSeedBootstrap) to own the handler.
568
2372
  const tempService = new SeedBootstrapService({
569
2373
  partyId: seed.partyId,
2374
+ trustPolicy: this.config.seedTrustPolicy,
570
2375
  });
571
2376
  if (this.controlNode && this.controlDatabase) {
572
- tempService.initialize(this.controlNode, this.controlDatabase);
2377
+ tempService.initialize(this.controlNode, this.controlDatabase, { registerHandler: false });
573
2378
  }
574
- return await tempService.applySeed(seed);
2379
+ return await tempService.applySeed(seed, options);
575
2380
  }
576
- return await this.seedBootstrapService.applySeed(seed);
2381
+ return await this.seedBootstrapService.applySeed(seed, options);
577
2382
  }
578
2383
  /**
579
2384
  * Deliver a seed directly to a peer via the /sereus/seed/1.0.0 protocol.
@@ -676,12 +2481,19 @@ export class CadreNode {
676
2481
  */
677
2482
  async dialInvite(invite) {
678
2483
  if (!this.seedBootstrapService) {
679
- // Create a temporary service for dialing
2484
+ // Create a temporary service that only dials the invite. It is discarded after
2485
+ // the call, so it does NOT own the shared node's inbound seed handler:
2486
+ // initialize with { registerHandler: false }. This temp dialInvite never
2487
+ // applies a seed itself — a node that wants to RECEIVE an inbound seed back
2488
+ // must have a persistent service (enableSeedListener / initializeSeedBootstrap)
2489
+ // own the /sereus/seed/1.0.0 handler. trustPolicy is therefore effectively dead
2490
+ // on this dial-only path; it is kept for symmetry with the applySeed temp site.
680
2491
  const tempService = new SeedBootstrapService({
681
2492
  partyId: invite.partyId,
2493
+ trustPolicy: this.config.seedTrustPolicy,
682
2494
  });
683
2495
  if (this.controlNode && this.controlDatabase) {
684
- tempService.initialize(this.controlNode, this.controlDatabase);
2496
+ tempService.initialize(this.controlNode, this.controlDatabase, { registerHandler: false });
685
2497
  }
686
2498
  await tempService.dialInvite(invite);
687
2499
  return;
@@ -689,7 +2501,7 @@ export class CadreNode {
689
2501
  await this.seedBootstrapService.dialInvite(invite);
690
2502
  }
691
2503
  // ============================================================================
692
- // Strand Solicitation API (strand-proto integration)
2504
+ // Strand Solicitation API (native cadre-core formation transport)
693
2505
  // ============================================================================
694
2506
  /**
695
2507
  * Initialize the strand solicitation service.