@serfab/cadre-core 1.23.0 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (313) hide show
  1. package/README.md +216 -294
  2. package/dist/arachnode-stub.d.ts +12 -4
  3. package/dist/arachnode-stub.js +15 -8
  4. package/dist/arachnode-stub.js.map +1 -1
  5. package/dist/cadre-invite-protocol.js +5 -4
  6. package/dist/cadre-invite-protocol.js.map +1 -1
  7. package/dist/cadre-node.d.ts +145 -44
  8. package/dist/cadre-node.js +239 -79
  9. package/dist/cadre-node.js.map +1 -1
  10. package/dist/control-authorization.d.ts +14 -2
  11. package/dist/control-authorization.js +8 -3
  12. package/dist/control-authorization.js.map +1 -1
  13. package/dist/control-cohort.d.ts +27 -13
  14. package/dist/control-cohort.js +21 -14
  15. package/dist/control-cohort.js.map +1 -1
  16. package/dist/control-database.d.ts +18 -16
  17. package/dist/control-database.js +57 -22
  18. package/dist/control-database.js.map +1 -1
  19. package/dist/control-protocol-guard.d.ts +1 -1
  20. package/dist/control-protocol-guard.js +7 -3
  21. package/dist/control-protocol-guard.js.map +1 -1
  22. package/dist/control-read-retry.d.ts +49 -11
  23. package/dist/control-read-retry.js +56 -15
  24. package/dist/control-read-retry.js.map +1 -1
  25. package/dist/control-retry.d.ts +32 -3
  26. package/dist/control-retry.js +35 -4
  27. package/dist/control-retry.js.map +1 -1
  28. package/dist/control-schema-version.d.ts +5 -5
  29. package/dist/control-schema-version.js +5 -5
  30. package/dist/control-schema-version.js.map +1 -1
  31. package/dist/control-schema.d.ts +5 -5
  32. package/dist/control-schema.js +1032 -1011
  33. package/dist/control-schema.js.map +1 -1
  34. package/dist/control-write-retry.d.ts +54 -65
  35. package/dist/control-write-retry.js +134 -158
  36. package/dist/control-write-retry.js.map +1 -1
  37. package/dist/delegate-admission.d.ts +1 -1
  38. package/dist/delegate-admission.js +1 -1
  39. package/dist/edge-node/config.d.ts +4 -3
  40. package/dist/edge-node/config.js +11 -9
  41. package/dist/edge-node/config.js.map +1 -1
  42. package/dist/edge-node/dissolve.d.ts +4 -4
  43. package/dist/edge-node/dissolve.js +4 -4
  44. package/dist/edge-node/edge-join.d.ts +1 -1
  45. package/dist/edge-node/edge-join.js +1 -1
  46. package/dist/edge-node/edge-node.js +2 -2
  47. package/dist/edge-node/edge-node.js.map +1 -1
  48. package/dist/edge-node/index.d.ts +1 -0
  49. package/dist/edge-node/index.js +1 -0
  50. package/dist/edge-node/index.js.map +1 -1
  51. package/dist/edge-node/join-credential.d.ts +1 -1
  52. package/dist/edge-node/join-credential.js +1 -1
  53. package/dist/edge-node/lazy-raw-storage.js +12 -0
  54. package/dist/edge-node/lazy-raw-storage.js.map +1 -1
  55. package/dist/edge-node/listen-addrs.d.ts +21 -0
  56. package/dist/edge-node/listen-addrs.js +25 -0
  57. package/dist/edge-node/listen-addrs.js.map +1 -0
  58. package/dist/edge-node/owner-grant.d.ts +1 -1
  59. package/dist/edge-node/owner-grant.js +1 -1
  60. package/dist/edge-node/platform.d.ts +5 -4
  61. package/dist/edge-node/platform.js.map +1 -1
  62. package/dist/edge-node/strand-move.d.ts +1 -1
  63. package/dist/edge-node/strand-move.js +1 -1
  64. package/dist/edge-node/strands.js +27 -11
  65. package/dist/edge-node/strands.js.map +1 -1
  66. package/dist/index.d.ts +7 -5
  67. package/dist/index.js +18 -7
  68. package/dist/index.js.map +1 -1
  69. package/dist/invitation-bootstrap.d.ts +28 -5
  70. package/dist/invitation-bootstrap.js +41 -8
  71. package/dist/invitation-bootstrap.js.map +1 -1
  72. package/dist/link-budget.d.ts +53 -4
  73. package/dist/link-budget.js +58 -5
  74. package/dist/link-budget.js.map +1 -1
  75. package/dist/machine-removal.d.ts +2 -2
  76. package/dist/machine-removal.js +4 -3
  77. package/dist/machine-removal.js.map +1 -1
  78. package/dist/membership-connection-gater.d.ts +2 -2
  79. package/dist/membership-connection-gater.js +2 -2
  80. package/dist/membership-view.d.ts +2 -2
  81. package/dist/membership-view.js +2 -2
  82. package/dist/node-claim-payload.d.ts +15 -7
  83. package/dist/node-claim-payload.js +25 -9
  84. package/dist/node-claim-payload.js.map +1 -1
  85. package/dist/node-class.d.ts +36 -0
  86. package/dist/node-class.js +26 -0
  87. package/dist/node-class.js.map +1 -0
  88. package/dist/node-default-transports.d.ts +50 -3
  89. package/dist/node-default-transports.js +48 -3
  90. package/dist/node-default-transports.js.map +1 -1
  91. package/dist/node-roles.d.ts +118 -0
  92. package/dist/node-roles.js +142 -0
  93. package/dist/node-roles.js.map +1 -0
  94. package/dist/node-transports.d.ts +6 -2
  95. package/dist/node-transports.js +39 -7
  96. package/dist/node-transports.js.map +1 -1
  97. package/dist/owner-anchor-sync.js +1 -1
  98. package/dist/owner-key-export.js +1 -1
  99. package/dist/owner-key-flows.d.ts +1 -1
  100. package/dist/owner-key-store.js +1 -1
  101. package/dist/owner-signer.js +1 -1
  102. package/dist/peer-authorization.d.ts +3 -1
  103. package/dist/peer-authorization.js +3 -1
  104. package/dist/peer-authorization.js.map +1 -1
  105. package/dist/peer-dial.d.ts +74 -10
  106. package/dist/peer-dial.js +89 -17
  107. package/dist/peer-dial.js.map +1 -1
  108. package/dist/peer-join-backfill.js +4 -4
  109. package/dist/peer-join-backfill.js.map +1 -1
  110. package/dist/peer-record.d.ts +34 -9
  111. package/dist/peer-record.js +66 -13
  112. package/dist/peer-record.js.map +1 -1
  113. package/dist/pending-join-runner.d.ts +1 -1
  114. package/dist/pending-join-runner.js +1 -1
  115. package/dist/provider/client.d.ts +6 -5
  116. package/dist/provider/client.js +3 -3
  117. package/dist/provider/client.js.map +1 -1
  118. package/dist/provider/descriptor.d.ts +1 -1
  119. package/dist/provider/descriptor.js +1 -1
  120. package/dist/provider/oauth.d.ts +1 -1
  121. package/dist/provider/oauth.js +1 -1
  122. package/dist/provider/rent-node.d.ts +4 -3
  123. package/dist/provider/rent-node.js +3 -3
  124. package/dist/provider/rent-node.js.map +1 -1
  125. package/dist/relay-addrs.d.ts +62 -7
  126. package/dist/relay-addrs.js +74 -11
  127. package/dist/relay-addrs.js.map +1 -1
  128. package/dist/relay-reservation.d.ts +1 -1
  129. package/dist/relay-reservation.js +1 -1
  130. package/dist/relay-server.d.ts +6 -5
  131. package/dist/relay-server.js +5 -5
  132. package/dist/relay-server.js.map +1 -1
  133. package/dist/schema-verification.d.ts +13 -10
  134. package/dist/schema-verification.js +32 -24
  135. package/dist/schema-verification.js.map +1 -1
  136. package/dist/sealed-party-key.js +1 -1
  137. package/dist/seed-bootstrap.d.ts +4 -3
  138. package/dist/seed-bootstrap.js +6 -7
  139. package/dist/seed-bootstrap.js.map +1 -1
  140. package/dist/strand-addr-protocol.d.ts +8 -6
  141. package/dist/strand-addr-protocol.js +12 -10
  142. package/dist/strand-addr-protocol.js.map +1 -1
  143. package/dist/strand-app-schema-report.d.ts +16 -2
  144. package/dist/strand-app-schema-report.js +15 -0
  145. package/dist/strand-app-schema-report.js.map +1 -1
  146. package/dist/strand-cohort.d.ts +1 -4
  147. package/dist/strand-cohort.js.map +1 -1
  148. package/dist/strand-commit-durability.d.ts +1 -1
  149. package/dist/strand-database.d.ts +13 -6
  150. package/dist/strand-database.js +17 -9
  151. package/dist/strand-database.js.map +1 -1
  152. package/dist/strand-first-sync-gate.d.ts +3 -2
  153. package/dist/strand-first-sync-gate.js +3 -2
  154. package/dist/strand-first-sync-gate.js.map +1 -1
  155. package/dist/strand-formation-deadlines.d.ts +2 -2
  156. package/dist/strand-formation-deadlines.js +2 -2
  157. package/dist/strand-formation-protocol.d.ts +1 -1
  158. package/dist/strand-formation-protocol.js +51 -16
  159. package/dist/strand-formation-protocol.js.map +1 -1
  160. package/dist/strand-instance-manager.d.ts +67 -33
  161. package/dist/strand-instance-manager.js +164 -55
  162. package/dist/strand-instance-manager.js.map +1 -1
  163. package/dist/strand-membership-reconciler.d.ts +8 -3
  164. package/dist/strand-membership-reconciler.js +8 -3
  165. package/dist/strand-membership-reconciler.js.map +1 -1
  166. package/dist/strand-membership-writer.d.ts +2 -2
  167. package/dist/strand-membership-writer.js +41 -39
  168. package/dist/strand-membership-writer.js.map +1 -1
  169. package/dist/strand-move.js +1 -1
  170. package/dist/strand-network-config.d.ts +15 -5
  171. package/dist/strand-network-config.js +7 -6
  172. package/dist/strand-network-config.js.map +1 -1
  173. package/dist/strand-party-key-rotation.d.ts +7 -3
  174. package/dist/strand-party-key-rotation.js +7 -3
  175. package/dist/strand-party-key-rotation.js.map +1 -1
  176. package/dist/strand-party-machines.d.ts +19 -8
  177. package/dist/strand-party-machines.js +26 -11
  178. package/dist/strand-party-machines.js.map +1 -1
  179. package/dist/strand-party-peers.d.ts +36 -14
  180. package/dist/strand-party-peers.js +24 -15
  181. package/dist/strand-party-peers.js.map +1 -1
  182. package/dist/strand-peer-redial.d.ts +1 -1
  183. package/dist/strand-peer-redial.js +1 -1
  184. package/dist/strand-reactivity.d.ts +20 -17
  185. package/dist/strand-reactivity.js +31 -18
  186. package/dist/strand-reactivity.js.map +1 -1
  187. package/dist/strand-revocation-enforcer.d.ts +3 -3
  188. package/dist/strand-revocation-enforcer.js +3 -3
  189. package/dist/strand-wake-protocol.d.ts +19 -7
  190. package/dist/strand-wake-protocol.js +22 -10
  191. package/dist/strand-wake-protocol.js.map +1 -1
  192. package/dist/strand-watcher.d.ts +1 -1
  193. package/dist/strand-watcher.js +1 -1
  194. package/dist/table-change-watch.js +1 -1
  195. package/dist/types.d.ts +116 -55
  196. package/dist/types.js.map +1 -1
  197. package/dist/webrtc-upgrade.d.ts +254 -0
  198. package/dist/webrtc-upgrade.js +355 -0
  199. package/dist/webrtc-upgrade.js.map +1 -0
  200. package/package.json +9 -9
  201. package/src/announce-addrs.ts +93 -93
  202. package/src/arachnode-stub.ts +18 -10
  203. package/src/bootstrap-peer-store-file.ts +44 -44
  204. package/src/bootstrap-peer-store.ts +214 -214
  205. package/src/cadre-invite-protocol.ts +741 -740
  206. package/src/cadre-node.ts +12462 -12280
  207. package/src/claim-proof.ts +147 -147
  208. package/src/control-authorization.ts +290 -276
  209. package/src/control-cohort.ts +223 -204
  210. package/src/control-database.ts +72 -28
  211. package/src/control-formation-recorder.ts +359 -359
  212. package/src/control-protocol-guard.ts +370 -366
  213. package/src/control-read-retry.ts +222 -180
  214. package/src/control-retry.ts +36 -4
  215. package/src/control-schema-version.ts +301 -301
  216. package/src/control-schema.ts +1108 -1087
  217. package/src/control-write-retry.ts +639 -669
  218. package/src/delegate-admission.ts +351 -351
  219. package/src/describe-thrown.ts +60 -60
  220. package/src/ed25519-key.ts +185 -185
  221. package/src/edge-node/config.ts +13 -10
  222. package/src/edge-node/dissolve.ts +252 -252
  223. package/src/edge-node/edge-join.ts +320 -320
  224. package/src/edge-node/edge-node.ts +1095 -1095
  225. package/src/edge-node/index.ts +60 -59
  226. package/src/edge-node/join-credential.ts +16 -16
  227. package/src/edge-node/lazy-raw-storage.ts +194 -178
  228. package/src/edge-node/listen-addrs.ts +26 -0
  229. package/src/edge-node/node-local.ts +84 -84
  230. package/src/edge-node/options.ts +195 -195
  231. package/src/edge-node/owner-grant.ts +63 -63
  232. package/src/edge-node/platform.ts +5 -4
  233. package/src/edge-node/strand-move.ts +266 -266
  234. package/src/edge-node/strands.ts +27 -12
  235. package/src/enrolled-machine-store-file.ts +43 -43
  236. package/src/enrolled-machine-store.ts +280 -280
  237. package/src/fetch-impl.ts +23 -23
  238. package/src/file-durable-slot.ts +64 -64
  239. package/src/file-safe-name.ts +32 -32
  240. package/src/formation-approval.ts +604 -604
  241. package/src/formation-token.ts +63 -63
  242. package/src/fs-atomic.ts +92 -92
  243. package/src/index.ts +987 -957
  244. package/src/invitation-bootstrap.ts +152 -118
  245. package/src/joined-strand-store.ts +553 -553
  246. package/src/key-store-file.ts +142 -142
  247. package/src/link-budget.ts +61 -5
  248. package/src/machine-removal.ts +211 -210
  249. package/src/member-redemption.ts +643 -643
  250. package/src/membership-connection-gater.ts +650 -650
  251. package/src/membership-view.ts +147 -147
  252. package/src/node-claim-payload.ts +34 -12
  253. package/src/node-class.ts +47 -0
  254. package/src/node-default-transports.ts +65 -3
  255. package/src/node-roles.ts +220 -0
  256. package/src/node-transports.ts +86 -49
  257. package/src/owner-anchor-sync.ts +172 -172
  258. package/src/owner-grant-protocol.ts +547 -547
  259. package/src/owner-key-export.ts +348 -348
  260. package/src/owner-key-flows.ts +109 -109
  261. package/src/owner-key-store.ts +1 -1
  262. package/src/owner-keys.ts +179 -179
  263. package/src/owner-signer.ts +92 -92
  264. package/src/peer-addr-book.ts +252 -252
  265. package/src/peer-authorization.ts +584 -582
  266. package/src/peer-dial.ts +698 -605
  267. package/src/peer-join-backfill.ts +961 -961
  268. package/src/peer-record.ts +71 -16
  269. package/src/pending-join-runner.ts +798 -798
  270. package/src/primary-lan-address.ts +47 -47
  271. package/src/provider/client.ts +327 -326
  272. package/src/provider/descriptor.ts +129 -129
  273. package/src/provider/index.ts +59 -59
  274. package/src/provider/oauth.ts +285 -285
  275. package/src/provider/rent-node.ts +276 -275
  276. package/src/relay-addrs.ts +90 -12
  277. package/src/relay-reservation.ts +1 -1
  278. package/src/relay-server.ts +99 -98
  279. package/src/schema-verification.ts +38 -23
  280. package/src/sealed-party-key.ts +190 -190
  281. package/src/seed-bootstrap.ts +8 -11
  282. package/src/seed-trust-policy.ts +353 -353
  283. package/src/self-address-update.ts +19 -19
  284. package/src/strand-addr-protocol.ts +17 -14
  285. package/src/strand-app-schema-report.ts +147 -122
  286. package/src/strand-cohort.ts +1 -5
  287. package/src/strand-commit-durability.ts +165 -165
  288. package/src/strand-database.ts +522 -510
  289. package/src/strand-first-sync-gate.ts +3 -2
  290. package/src/strand-formation-deadlines.ts +227 -227
  291. package/src/strand-formation-manager.ts +731 -731
  292. package/src/strand-formation-protocol.ts +1174 -1138
  293. package/src/strand-fret-addrs.ts +179 -179
  294. package/src/strand-instance-manager.ts +188 -63
  295. package/src/strand-membership-reconciler.ts +1315 -1310
  296. package/src/strand-membership-writer.ts +2152 -2150
  297. package/src/strand-move.ts +1 -1
  298. package/src/strand-network-config.ts +16 -6
  299. package/src/strand-offline.ts +178 -178
  300. package/src/strand-party-key-rotation.ts +440 -436
  301. package/src/strand-party-machines.ts +308 -293
  302. package/src/strand-party-peers.ts +151 -129
  303. package/src/strand-peer-redial.ts +1 -1
  304. package/src/strand-reactivity.ts +36 -19
  305. package/src/strand-revocation-enforcer.ts +758 -758
  306. package/src/strand-wake-protocol.ts +406 -394
  307. package/src/strand-watcher.ts +1 -1
  308. package/src/table-change-watch.ts +285 -285
  309. package/src/timed-step.ts +28 -28
  310. package/src/timeout-scheduler.ts +29 -29
  311. package/src/trusted-owner-store.ts +242 -242
  312. package/src/types.ts +3182 -3121
  313. package/src/webrtc-upgrade.ts +439 -0
@@ -1,204 +1,223 @@
1
- /**
2
- * Control-cohort dial selection: the pure policy behind
3
- * {@link CadreNode.reconcileControlCohort}. Decides WHICH known cadre siblings a
4
- * node proactively dials each pass to keep the `CadreControl` collections' cohort
5
- * connected (and thus replicating), without devolving into an N² mesh.
6
- *
7
- * FRET only needs a *connected* graph with the block clusters reachable, not a
8
- * full mesh. Always-on (`storage` profile) machines are the stable, dialable
9
- * backbone that hold the control blocks, so the policy is **backbone-preferential
10
- * with a bounded out-degree**:
11
- *
12
- * - **Always dial every backbone (always-on) member** — a party runs few of them,
13
- * and they are the machines that stay reachable while phones sleep or roam.
14
- * - **Fill the remainder** up to `targetDegree` with the other members (phones,
15
- * and machines that have not yet published a profile), ordered
16
- * deterministically (peerId sort) so every node makes the same stable choice
17
- * across passes and load spreads predictably.
18
- *
19
- * For a small party (members ≤ degree) this degenerates to a full mesh, which is
20
- * correct, cheap, and matches what the convergence tests do by hand; for a large
21
- * party it bounds out-degree to `backbone + targetDegree`.
22
- */
23
-
24
- import type { CohortPeerRow } from './strand-cohort.js';
25
-
26
- /**
27
- * Default backstop for {@link CadreNode.reconcileControlCohort} (ms): a pass runs this long
28
- * after the last one settled whether or not anything woke it. Passes normally run on a
29
- * change — a membership table changing, a sibling connecting or disconnecting, an address
30
- * change — and on the retries a pass arms for itself ({@link nextReconcileWakeMs}); the
31
- * backstop bounds how stale this node can be after a change it hears nothing of: one that
32
- * reaches it with no storage event, which happens to a machine outside the block's cohort
33
- * (a cadre larger than `CONTROL_REPLICATION_BREADTH`) and to one in the cohort that the
34
- * writer could not reach when it committed. The membership rows view's maximum age
35
- * (`MEMBERSHIP_VIEW_MAX_AGE_MS`, 15 s) is shorter than this, so the backstop pass reads
36
- * storage and starts the cohort consult that brings such a change in.
37
- *
38
- * `network.controlCohort.reconcileMs` overrides it. An interval between a pass settling and
39
- * the next start, not a deadline on a pass: passes are single-flight, and one unreachable
40
- * sibling's dial alone can take the whole per-peer dial budget (86 s at the default
41
- * declared link).
42
- *
43
- * NOTE: a machine whose storage takes no event for a change — one outside the block's cohort
44
- * in a cadre larger than `CONTROL_REPLICATION_BREADTH` (16 machines), or one the writer could
45
- * not reach when it committed — sees it only at this backstop, where the change-driven path
46
- * serves a machine the commit reached within the 50 ms coalescing window. If a party ever
47
- * needs a tighter bound, lower the backstop for its nodes or give the plugin's change bridge
48
- * a path for outside-the-cohort reads.
49
- */
50
- export const DEFAULT_CONTROL_COHORT_RECONCILE_MS = 120_000;
51
-
52
- /**
53
- * First redial delay after a pass's dial of a selected sibling, or a cold-start bootstrap peer,
54
- * fails (ms). Doubles per consecutive such pass and is capped at the backstop, so a
55
- * sibling that is offline costs one dial per backstop once the ladder saturates, and a
56
- * stranded cold-start node keeps trying forever (15 s, 30 s, 60 s, then every backstop).
57
- */
58
- export const CONTROL_COHORT_REDIAL_MS = 15_000;
59
-
60
- /**
61
- * How often an owner retries a node it has just claimed while that node restarts into the
62
- * owner's party (ms; `CadreNode.claimNode`). A poll step, not a deadline: the retries run for
63
- * {@link CONTROL_COHORT_REDIAL_MS} after the node accepts the claim, the delay of the reconcile
64
- * pass's first redial, which would otherwise be the owner's next dial of the node.
65
- */
66
- export const CLAIM_RESTART_REDIAL_STEP_MS = 1_000;
67
-
68
- /**
69
- * Default cap on the number of **non-backbone** members a single reconcile pass
70
- * proactively dials. Backbone (always-on) members are always dialed and do NOT
71
- * count against this cap.
72
- */
73
- export const DEFAULT_CONTROL_COHORT_TARGET_DEGREE = 6;
74
-
75
- // The per-sibling and per-address dial time limits live in `peer-dial.ts`
76
- // (`DEFAULT_CONTROL_COHORT_DIAL_TIMEOUT_MS`,
77
- // `DEFAULT_CONTROL_COHORT_PER_ADDRESS_DIAL_TIMEOUT_MS`), beside the dial they
78
- // bound, which `seed-bootstrap.ts` also uses and cannot import from here.
79
-
80
- /** Outcome of {@link selectControlCohortDials}: who to dial + what the cap dropped. */
81
- export interface ControlCohortSelection {
82
- /**
83
- * Siblings to dial this pass, backbone first then the bounded non-backbone
84
- * fill. Already self-excluded (the caller must pass self-excluded `siblings`).
85
- * Still includes peers that may turn out to be already-connected — the caller
86
- * diffs against live connections separately.
87
- */
88
- dials: CohortPeerRow[];
89
- /** Non-backbone members dropped by the `targetDegree` cap (0 when none). */
90
- cappedNonBackbone: number;
91
- }
92
-
93
- /** What one {@link CadreNode.reconcileControlCohort} pass did. */
94
- export interface ControlCohortReconcileResult {
95
- /**
96
- * Peer ids this pass dialled and whose dial resolved, in dial order: selected
97
- * siblings on the steady-state pass, retained bootstrap peers on the cold-start
98
- * pass (the pass takes one branch or the other, never both). A peer already
99
- * connected when the pass began is never listed — the pass skips it. A resolved
100
- * dial can still be refused moments later by the remote's membership gate, so
101
- * an entry means "this pass opened a connection", not "that connection is still
102
- * open".
103
- */
104
- dialed: string[];
105
- }
106
-
107
- /** Stable peerId ordering so the bounded fill is identical across passes/nodes. */
108
- function byPeerId(a: CohortPeerRow, b: CohortPeerRow): number {
109
- if (a.peerId < b.peerId) return -1;
110
- if (a.peerId > b.peerId) return 1;
111
- return 0;
112
- }
113
-
114
- /**
115
- * Backbone-preferential, bounded-out-degree selection of control-cohort siblings
116
- * to dial.
117
- *
118
- * A member is **backbone** iff its `CadrePeer` row declares the `storage` profile
119
- * (an always-on machine). All backbone members are selected; the rest fill up to
120
- * `targetDegree` in deterministic peerId order. A member whose profile is still
121
- * unknown (it has not self-published) is not backbone until it publishes one.
122
- * A profile is a dial preference only: it is self-declared, and a machine
123
- * misdeclaring one costs extra dials, never access.
124
- *
125
- * @param siblings - cadre members EXCLUDING self (the caller filters self out).
126
- * @param targetDegree - cap on non-backbone dials (negative is treated as 0).
127
- */
128
- export function selectControlCohortDials(
129
- siblings: CohortPeerRow[],
130
- targetDegree: number,
131
- ): ControlCohortSelection {
132
- const backbone: CohortPeerRow[] = [];
133
- const rest: CohortPeerRow[] = [];
134
-
135
- // NOTE: the backbone is uncapped, so out-degree grows with the number of always-on
136
- // machines (and with any sibling that declares `storage` without being one); if a party
137
- // ever runs more than a handful, cap the backbone too.
138
- for (const peer of siblings) {
139
- if (peer.profile === 'storage') {
140
- backbone.push(peer);
141
- } else {
142
- rest.push(peer);
143
- }
144
- }
145
-
146
- backbone.sort(byPeerId);
147
- rest.sort(byPeerId);
148
-
149
- const cap = Math.max(0, targetDegree);
150
- const fill = rest.slice(0, cap);
151
-
152
- return {
153
- dials: [...backbone, ...fill],
154
- cappedNonBackbone: rest.length - fill.length,
155
- };
156
- }
157
-
158
- /** What {@link nextReconcileWakeMs} decides from, all times epoch milliseconds. */
159
- export interface ReconcileWakeInput {
160
- /**
161
- * Consecutive passes, this one included, whose dial of a selected sibling or a cold-start
162
- * bootstrap peer failed; 0 when none of this pass's dials did.
163
- */
164
- redialStreak: number;
165
- /** When the pass that just ended began. */
166
- passStartedAt: number;
167
- /** Every (sibling, strand) strand-address due time this node holds. */
168
- strandAddrDueAt: Iterable<number>;
169
- now: number;
170
- /** The scheduler's backstop (ms); no wake is later than it. */
171
- backstopMs: number;
172
- }
173
-
174
- /**
175
- * How long after a pass the next one should run for the pass's own reasons, or `undefined`
176
- * when the backstop covers everything:
177
- *
178
- * - **Redial.** A streak of unconnected passes wakes after
179
- * `min(CONTROL_COHORT_REDIAL_MS × 2^(streak − 1), backstop)`.
180
- * - **Strand addresses.** The earliest strand-address due time LATER THAN the pass's start,
181
- * when it is sooner than the backstop, wakes when it falls due (now at the latest). A due
182
- * time at or before the start belongs to a (sibling, strand) the pass skipped for another
183
- * reason — the strand stopped, the sibling disconnected — and waking for it would spin; the
184
- * strand watcher's launch retry keeps the same rule.
185
- *
186
- * The smaller wins.
187
- */
188
- export function nextReconcileWakeMs(input: ReconcileWakeInput): number | undefined {
189
- const { redialStreak, passStartedAt, strandAddrDueAt, now, backstopMs } = input;
190
- let wake: number | undefined;
191
- if (redialStreak > 0) {
192
- wake = Math.min(CONTROL_COHORT_REDIAL_MS * 2 ** (redialStreak - 1), backstopMs);
193
- }
194
- for (const dueAt of strandAddrDueAt) {
195
- if (dueAt <= passStartedAt) {
196
- continue;
197
- }
198
- const delay = Math.max(0, dueAt - now);
199
- if (delay < backstopMs && (wake === undefined || delay < wake)) {
200
- wake = delay;
201
- }
202
- }
203
- return wake;
204
- }
1
+ /**
2
+ * Control-cohort dial selection: the pure policy behind
3
+ * {@link CadreNode.reconcileControlCohort}. Decides WHICH known cadre siblings a
4
+ * node proactively dials each pass to keep the `CadreControl` collections' cohort
5
+ * connected (and thus replicating), without devolving into an N² mesh.
6
+ *
7
+ * FRET only needs a *connected* graph with the block clusters reachable, not a
8
+ * full mesh. Always-on machines are the stable, dialable backbone that hold the
9
+ * control blocks, so the policy is **backbone-preferential with a bounded
10
+ * out-degree**:
11
+ *
12
+ * - **Always dial every backbone (always-on) member** — a party runs few of them,
13
+ * and they are the machines that stay reachable while phones sleep or roam.
14
+ * - **Fill the remainder** up to `targetDegree` with the other members (devices,
15
+ * and machines that have not yet published whether they are always on), ordered
16
+ * deterministically (peerId sort) so every node makes the same stable choice
17
+ * across passes and load spreads predictably.
18
+ *
19
+ * For a small party (members ≤ degree) this degenerates to a full mesh, which is
20
+ * correct, cheap, and matches what the convergence tests do by hand; for a large
21
+ * party it bounds out-degree to `backbone + targetDegree`.
22
+ */
23
+
24
+ import { decodeAlwaysOn } from './node-class.js';
25
+ import type { CohortPeerRow } from './strand-cohort.js';
26
+ import type { CadrePeerRow } from './types.js';
27
+
28
+ /**
29
+ * A sibling as this policy judges it: its addressing row plus the backbone fact, decoded from
30
+ * the stored `CadrePeer.Profile` code by {@link controlCohortSiblingsOf}. Required, not
31
+ * optional, so a caller cannot hand the policy raw rows and silently get no backbone.
32
+ */
33
+ export interface ControlCohortSibling extends CohortPeerRow {
34
+ /** Whether the machine declared itself always on; null until it self-publishes. */
35
+ alwaysOn: boolean | null;
36
+ }
37
+
38
+ /**
39
+ * Live `CadrePeer` rows as this policy reads them — the one place the stored `Profile` code
40
+ * reaches a dial decision, so no policy compares it.
41
+ */
42
+ export function controlCohortSiblingsOf(rows: readonly CadrePeerRow[]): ControlCohortSibling[] {
43
+ return rows.map(({ peerId, multiaddr, profile }) => ({ peerId, multiaddr, alwaysOn: decodeAlwaysOn(profile) }));
44
+ }
45
+
46
+ /**
47
+ * Default backstop for {@link CadreNode.reconcileControlCohort} (ms): a pass runs this long
48
+ * after the last one settled whether or not anything woke it. Passes normally run on a
49
+ * change — a membership table changing, a sibling connecting or disconnecting, an address
50
+ * change — and on the retries a pass arms for itself ({@link nextReconcileWakeMs}); the
51
+ * backstop bounds how stale this node can be after a change it hears nothing of: one that
52
+ * reaches it with no storage event, which happens to a machine outside the block's cohort
53
+ * (a cadre larger than `CONTROL_REPLICATION_BREADTH`) and to one in the cohort that the
54
+ * writer could not reach when it committed. The membership rows view's maximum age
55
+ * (`MEMBERSHIP_VIEW_MAX_AGE_MS`, 15 s) is shorter than this, so the backstop pass reads
56
+ * storage and starts the cohort consult that brings such a change in.
57
+ *
58
+ * `network.controlCohort.reconcileMs` overrides it. An interval between a pass settling and
59
+ * the next start, not a deadline on a pass: passes are single-flight, and one unreachable
60
+ * sibling's dial alone can take the whole per-peer dial budget (86 s at the default
61
+ * declared link).
62
+ *
63
+ * NOTE: a machine whose storage takes no event for a change — one outside the block's cohort
64
+ * in a cadre larger than `CONTROL_REPLICATION_BREADTH` (16 machines), or one the writer could
65
+ * not reach when it committed — sees it only at this backstop, where the change-driven path
66
+ * serves a machine the commit reached within the 50 ms coalescing window. If a party ever
67
+ * needs a tighter bound, lower the backstop for its nodes or give the plugin's change bridge
68
+ * a path for outside-the-cohort reads.
69
+ */
70
+ export const DEFAULT_CONTROL_COHORT_RECONCILE_MS = 120_000;
71
+
72
+ /**
73
+ * First redial delay after a pass's dial of a selected sibling, or a cold-start bootstrap peer,
74
+ * fails (ms). Doubles per consecutive such pass and is capped at the backstop, so a
75
+ * sibling that is offline costs one dial per backstop once the ladder saturates, and a
76
+ * stranded cold-start node keeps trying forever (15 s, 30 s, 60 s, then every backstop).
77
+ */
78
+ export const CONTROL_COHORT_REDIAL_MS = 15_000;
79
+
80
+ /**
81
+ * How often an owner retries a node it has just claimed while that node restarts into the
82
+ * owner's party (ms; `CadreNode.claimNode`). A poll step, not a deadline: the retries run for
83
+ * {@link CONTROL_COHORT_REDIAL_MS} after the node accepts the claim, the delay of the reconcile
84
+ * pass's first redial, which would otherwise be the owner's next dial of the node.
85
+ */
86
+ export const CLAIM_RESTART_REDIAL_STEP_MS = 1_000;
87
+
88
+ /**
89
+ * Default cap on the number of **non-backbone** members a single reconcile pass
90
+ * proactively dials. Backbone (always-on) members are always dialed and do NOT
91
+ * count against this cap.
92
+ */
93
+ export const DEFAULT_CONTROL_COHORT_TARGET_DEGREE = 6;
94
+
95
+ // The per-sibling and per-address dial time limits live in `peer-dial.ts`
96
+ // (`DEFAULT_CONTROL_COHORT_DIAL_TIMEOUT_MS`,
97
+ // `DEFAULT_CONTROL_COHORT_PER_ADDRESS_DIAL_TIMEOUT_MS`), beside the dial they
98
+ // bound, which `seed-bootstrap.ts` also uses and cannot import from here.
99
+
100
+ /** Outcome of {@link selectControlCohortDials}: who to dial + what the cap dropped. */
101
+ export interface ControlCohortSelection {
102
+ /**
103
+ * Siblings to dial this pass, backbone first then the bounded non-backbone
104
+ * fill. Already self-excluded (the caller must pass self-excluded `siblings`).
105
+ * Still includes peers that may turn out to be already-connected — the caller
106
+ * diffs against live connections separately.
107
+ */
108
+ dials: ControlCohortSibling[];
109
+ /** Non-backbone members dropped by the `targetDegree` cap (0 when none). */
110
+ cappedNonBackbone: number;
111
+ }
112
+
113
+ /** What one {@link CadreNode.reconcileControlCohort} pass did. */
114
+ export interface ControlCohortReconcileResult {
115
+ /**
116
+ * Peer ids this pass dialled and whose dial resolved, in dial order: selected
117
+ * siblings on the steady-state pass, retained bootstrap peers on the cold-start
118
+ * pass (the pass takes one branch or the other, never both). A peer already
119
+ * connected when the pass began is never listed — the pass skips it. A resolved
120
+ * dial can still be refused moments later by the remote's membership gate, so
121
+ * an entry means "this pass opened a connection", not "that connection is still
122
+ * open".
123
+ */
124
+ dialed: string[];
125
+ }
126
+
127
+ /** Stable peerId ordering so the bounded fill is identical across passes/nodes. */
128
+ function byPeerId(a: ControlCohortSibling, b: ControlCohortSibling): number {
129
+ if (a.peerId < b.peerId) return -1;
130
+ if (a.peerId > b.peerId) return 1;
131
+ return 0;
132
+ }
133
+
134
+ /**
135
+ * Backbone-preferential, bounded-out-degree selection of control-cohort siblings
136
+ * to dial.
137
+ *
138
+ * A member is **backbone** iff its `CadrePeer` row declares it always on. All
139
+ * backbone members are selected; the rest fill up to `targetDegree` in
140
+ * deterministic peerId order. A member that has not self-published yet is not
141
+ * backbone until it does. Being always on is a dial preference only: it is
142
+ * self-declared, and a machine misdeclaring it costs extra dials, never access.
143
+ *
144
+ * @param siblings - cadre members EXCLUDING self (the caller filters self out).
145
+ * @param targetDegree - cap on non-backbone dials (negative is treated as 0).
146
+ */
147
+ export function selectControlCohortDials(
148
+ siblings: ControlCohortSibling[],
149
+ targetDegree: number,
150
+ ): ControlCohortSelection {
151
+ const backbone: ControlCohortSibling[] = [];
152
+ const rest: ControlCohortSibling[] = [];
153
+
154
+ // NOTE: the backbone is uncapped, so out-degree grows with the number of always-on
155
+ // machines (and with any sibling that declares itself always on without being one); if a
156
+ // party ever runs more than a handful, cap the backbone too.
157
+ for (const peer of siblings) {
158
+ if (peer.alwaysOn === true) {
159
+ backbone.push(peer);
160
+ } else {
161
+ rest.push(peer);
162
+ }
163
+ }
164
+
165
+ backbone.sort(byPeerId);
166
+ rest.sort(byPeerId);
167
+
168
+ const cap = Math.max(0, targetDegree);
169
+ const fill = rest.slice(0, cap);
170
+
171
+ return {
172
+ dials: [...backbone, ...fill],
173
+ cappedNonBackbone: rest.length - fill.length,
174
+ };
175
+ }
176
+
177
+ /** What {@link nextReconcileWakeMs} decides from, all times epoch milliseconds. */
178
+ export interface ReconcileWakeInput {
179
+ /**
180
+ * Consecutive passes, this one included, whose dial of a selected sibling or a cold-start
181
+ * bootstrap peer failed; 0 when none of this pass's dials did.
182
+ */
183
+ redialStreak: number;
184
+ /** When the pass that just ended began. */
185
+ passStartedAt: number;
186
+ /** Every (sibling, strand) strand-address due time this node holds. */
187
+ strandAddrDueAt: Iterable<number>;
188
+ now: number;
189
+ /** The scheduler's backstop (ms); no wake is later than it. */
190
+ backstopMs: number;
191
+ }
192
+
193
+ /**
194
+ * How long after a pass the next one should run for the pass's own reasons, or `undefined`
195
+ * when the backstop covers everything:
196
+ *
197
+ * - **Redial.** A streak of unconnected passes wakes after
198
+ * `min(CONTROL_COHORT_REDIAL_MS × 2^(streak − 1), backstop)`.
199
+ * - **Strand addresses.** The earliest strand-address due time LATER THAN the pass's start,
200
+ * when it is sooner than the backstop, wakes when it falls due (now at the latest). A due
201
+ * time at or before the start belongs to a (sibling, strand) the pass skipped for another
202
+ * reason — the strand stopped, the sibling disconnected — and waking for it would spin; the
203
+ * strand watcher's launch retry keeps the same rule.
204
+ *
205
+ * The smaller wins.
206
+ */
207
+ export function nextReconcileWakeMs(input: ReconcileWakeInput): number | undefined {
208
+ const { redialStreak, passStartedAt, strandAddrDueAt, now, backstopMs } = input;
209
+ let wake: number | undefined;
210
+ if (redialStreak > 0) {
211
+ wake = Math.min(CONTROL_COHORT_REDIAL_MS * 2 ** (redialStreak - 1), backstopMs);
212
+ }
213
+ for (const dueAt of strandAddrDueAt) {
214
+ if (dueAt <= passStartedAt) {
215
+ continue;
216
+ }
217
+ const delay = Math.max(0, dueAt - now);
218
+ if (delay < backstopMs && (wake === undefined || delay < wake)) {
219
+ wake = delay;
220
+ }
221
+ }
222
+ return wake;
223
+ }
@@ -9,12 +9,14 @@ import type { Libp2p } from '@libp2p/interface';
9
9
  import type { IRepo } from '@optimystic/db-core';
10
10
  import { createEmptyCollections } from '@serfab/quereus-plugin-sereus';
11
11
  import type { CollectionRegistrySource } from '@serfab/quereus-plugin-sereus';
12
- import type { StrandRow, StrandPeerRow, PendingStrandPartyKeyRow, SealedPendingPartyKey, JoinRequestRow, JoinOutcome, PendingJoin, NodeProfile, PeerAddressRecord, CadrePeerRow, OwnerKeyRow, RevocationRow, DeviceTokenRecord, DeviceTokenRow, PushPlatform, CadreInviteRow, CadreInviteUsageRow, CadreInviteRedemptionResult, CadreInviteStatus, OwnerGrantRow, OwnerGrantUsageRow, OwnerGrantRedemptionResult, OwnerGrantStatus, SeedPeer, MachineRemovalWrites } from './types.js';
12
+ import type { StrandRow, StrandPeerRow, StrandPeerRole, PendingStrandPartyKeyRow, SealedPendingPartyKey, JoinRequestRow, JoinOutcome, PendingJoin, PeerAddressRecord, CadrePeerRow, OwnerKeyRow, RevocationRow, DeviceTokenRecord, DeviceTokenRow, PushPlatform, CadreInviteRow, CadreInviteUsageRow, CadreInviteRedemptionResult, CadreInviteStatus, OwnerGrantRow, OwnerGrantUsageRow, OwnerGrantRedemptionResult, OwnerGrantStatus, SeedPeer, MachineRemovalWrites } from './types.js';
13
13
  import { APPLY_CONTROL_SCHEMA, CONTROL_SCHEMA_DECLARATION, CONTROL_SCHEMA_NAME, controlSchemaFileDeclaration } from './control-schema.js';
14
14
  import { refuseIncompatibleControlStore } from './control-schema-version.js';
15
+ import { decodeAlwaysOn } from './node-class.js';
16
+ import type { PublishedProfile } from './node-class.js';
15
17
  import { canonicalDatetime } from './canonical-datetime.js';
16
18
  import { controlAuthorizationFields, unboundAuthorizationFields, cadreInviteRowFields, ownerGrantRowFields, strandPeerRowFields, pendingStrandPartyKeyRowFields, CONTROL_TABLES } from './control-authorization.js';
17
- import type { ControlTable, RevocableTable, BoundControlDomain, UnboundControlDomain, ControlAction, CadreInviteSignedFields, OwnerGrantSignedFields } from './control-authorization.js';
19
+ import type { ControlTable, RevocableTable, BoundControlDomain, UnboundControlDomain, ControlAction, CadreInviteSignedFields, OwnerGrantSignedFields, StrandPeerSignedFields } from './control-authorization.js';
18
20
  import { requireEd25519PublicKeyB64 } from './ed25519-key.js';
19
21
  import { strandMemberKeyPair } from './strand-member-key.js';
20
22
  import { verifyCadreInviteRow, verifyOwnerGrantRow, verifyStrandPeerRow } from './peer-authorization.js';
@@ -579,6 +581,51 @@ function pendingPartyKeyOfRow(row: Record<string, SqlValue>): PendingStrandParty
579
581
  };
580
582
  }
581
583
 
584
+ /** The columns every `StrandPeer` read takes, as {@link strandPeerRowOf} maps them. */
585
+ const STRAND_PEER_COLUMNS = 'StrandId, PeerId, StrandPeerId, Role, Sig';
586
+
587
+ /**
588
+ * A stored `StrandPeer.Role` as this build knows it. Null, and a row written before the column
589
+ * existed, read as a serving machine's. A value this build does not know also reads as null, and
590
+ * the row then fails {@link verifyStrandPeerRow}, whose digest omits a null role, and is dropped:
591
+ * a reader acts only on roles it knows, as a version-4 reader drops a row marked `'client'`. Such
592
+ * a value reaches a store only from a machine that bypassed its own `KnownStrandPeerRole` check,
593
+ * or from a later schema version that adds one.
594
+ */
595
+ function strandPeerRoleOf(value: SqlValue): StrandPeerRole | null {
596
+ return value === 'client' ? value : null;
597
+ }
598
+
599
+ /** A stored `StrandPeer` row as {@link StrandPeerRow}. */
600
+ function strandPeerRowOf(row: Record<string, SqlValue>): StrandPeerRow {
601
+ return {
602
+ strandId: row.StrandId as string,
603
+ peerId: row.PeerId as string,
604
+ strandPeerId: row.StrandPeerId as string,
605
+ role: strandPeerRoleOf(row.Role),
606
+ sig: row.Sig as string,
607
+ };
608
+ }
609
+
610
+ /** How a `StrandPeer` role reads in a log line: the null role is the serving machine's. */
611
+ const strandPeerRoleName = (role: StrandPeerRole | null): string => role ?? 'serving';
612
+
613
+ /**
614
+ * The "already present" line of {@link ControlDatabase.insertStrandPeer}. The row is immutable, so
615
+ * when the held row states a role other than the one this machine now publishes, the line names
616
+ * both: the stored role stands until the machine is removed and re-added.
617
+ */
618
+ function logHeldStrandPeer(held: StrandPeerRow, role: StrandPeerRole | null): void {
619
+ if (held.role === role) {
620
+ log('StrandPeer row already present for strand %s (names %s); insert skipped', held.strandId, held.strandPeerId);
621
+ return;
622
+ }
623
+ log(
624
+ 'StrandPeer row already present for strand %s (names %s) and states role %s; this machine now publishes role %s, and the row cannot be changed; insert skipped',
625
+ held.strandId, held.strandPeerId, strandPeerRoleName(held.role), strandPeerRoleName(role)
626
+ );
627
+ }
628
+
582
629
  /**
583
630
  * The key the party moves to in one strand, for {@link ControlDatabase.removeMachine}: its public
584
631
  * half in the clear, its private half sealed to the machines that remain (`sealed-party-key.ts`).
@@ -852,16 +899,16 @@ function errorChainMatches(error: unknown, pattern: RegExp): boolean {
852
899
  *
853
900
  * A peer is `preferred` when it is the minter, the one machine certain to be reachable by the
854
901
  * receiver at that moment, since it is delivering the seed or the reply, or an always-on
855
- * machine (`Profile = 'storage'`), the ones that stay dialable. Preference is a dial hint and
856
- * reads no `OwnerKey` row: owner keys are not machines, and a receiver derives trust from its
857
- * own anchor, never from this list. A `Profile` is self-declared and not checked here, so a
858
- * machine misdeclaring one costs the receiver a wasted dial at most.
902
+ * machine, the ones that stay dialable. Preference is a dial hint and reads no `OwnerKey` row:
903
+ * owner keys are not machines, and a receiver derives trust from its own anchor, never from
904
+ * this list. Being always on is self-declared and not checked here, so a machine misdeclaring
905
+ * it costs the receiver a wasted dial at most.
859
906
  */
860
907
  export function projectSeedPeers(rows: readonly CadrePeerRow[], minterPeerId: string): SeedPeer[] {
861
908
  return rows.map(({ peerId, multiaddr, profile }) => ({
862
909
  peerId,
863
910
  multiaddrs: multiaddr ? multiaddr.split(',') : [],
864
- preferred: peerId === minterPeerId || profile === 'storage',
911
+ preferred: peerId === minterPeerId || decodeAlwaysOn(profile) === true,
865
912
  }));
866
913
  }
867
914
 
@@ -950,7 +997,7 @@ function cadrePeerRowOf(row: Record<string, SqlValue>): CadrePeerRow {
950
997
  publicKey: (row.PublicKey as string | null) ?? null,
951
998
  multiaddr: (row.Multiaddr as string | null) ?? null,
952
999
  updatedAt: (row.UpdatedAt as number | null) ?? 0,
953
- profile: (row.Profile as NodeProfile | null) ?? null,
1000
+ profile: (row.Profile as PublishedProfile | null) ?? null,
954
1001
  sig: (row.Sig as string | null) ?? null,
955
1002
  stampId: (row.StampId as string | null) ?? null,
956
1003
  vouchOwner: (row.VouchOwner as string | null) ?? null,
@@ -2371,19 +2418,12 @@ export class ControlDatabase {
2371
2418
  */
2372
2419
  private async readStrandPeerRows(strandId: string, retry: boolean): Promise<StrandPeerRow[]> {
2373
2420
  const rows = await this.readRows(
2374
- 'select StrandId, PeerId, StrandPeerId, Sig from CadreControl.StrandPeer where StrandId = ?',
2421
+ `select ${STRAND_PEER_COLUMNS} from CadreControl.StrandPeer where StrandId = ?`,
2375
2422
  [strandId],
2376
2423
  'strand-peers',
2377
2424
  retry
2378
2425
  );
2379
- return rows
2380
- .filter((row) => row.StrandId === strandId)
2381
- .map((row) => ({
2382
- strandId,
2383
- peerId: row.PeerId as string,
2384
- strandPeerId: row.StrandPeerId as string,
2385
- sig: row.Sig as string,
2386
- }));
2426
+ return rows.filter((row) => row.StrandId === strandId).map(strandPeerRowOf);
2387
2427
  }
2388
2428
 
2389
2429
  /** `ValidationKey` stamp nonce — bound into {@link deleteValidationKey}'s remove digest. */
@@ -3511,32 +3551,36 @@ export class ControlDatabase {
3511
3551
  }
3512
3552
 
3513
3553
  /**
3514
- * Publish this machine's peer id in one strand (`StrandPeer` row), signed by this machine's
3515
- * own identity key. Insert-if-absent: `true` when a row was written, `false` when this machine
3516
- * already has one for the strand. The schema refuses the insert while this node does not hold
3517
- * the machine's `CadrePeer` row, and that rejection propagates.
3554
+ * Publish this machine's peer id in one strand, and whether it serves the strand (`StrandPeer`
3555
+ * row), signed by this machine's own identity key in the form its role selects
3556
+ * (`strandPeerRowFields`). Insert-if-absent: `true` when a row was written, `false` when this
3557
+ * machine already has one for the strand. The row is immutable, so a held row stands even when
3558
+ * its role differs from `row.role`; that case is logged ({@link logHeldStrandPeer}). The schema
3559
+ * refuses the insert while this node does not hold the machine's `CadrePeer` row, and that
3560
+ * rejection propagates.
3518
3561
  *
3519
- * @param row - The strand, and this machine's peer id in it (`strandTransportKey`).
3562
+ * @param row - The strand, this machine's peer id in it (`strandTransportKey`), and its role
3563
+ * there: `'client'` when it stores none of the strand, null when it serves it.
3520
3564
  * @param sign - Signs the raw message bytes with the identity key behind this machine's
3521
3565
  * control peer id. A callback for the reason {@link updateSelfPeerRecord} takes one: this
3522
3566
  * class holds no private key.
3523
3567
  */
3524
- async insertStrandPeer(row: { strandId: string; strandPeerId: string }, sign: ControlSign): Promise<boolean> {
3568
+ async insertStrandPeer(row: Omit<StrandPeerSignedFields, 'peerId'>, sign: ControlSign): Promise<boolean> {
3525
3569
  this.ensureInitialized();
3526
3570
  const peerId = this.config.libp2pNode.peerId.toString();
3527
- const fields = { strandId: row.strandId, peerId, strandPeerId: row.strandPeerId };
3571
+ const fields: StrandPeerSignedFields = { strandId: row.strandId, peerId, strandPeerId: row.strandPeerId, role: row.role };
3528
3572
  return this.lockedWithRetry(async () => {
3529
3573
  // retry: false — this guard runs inside the locked write body (see queryStampId's NOTE).
3530
3574
  const held = (await this.readStrandPeerRows(row.strandId, false)).find((stored) => stored.peerId === peerId);
3531
3575
  if (held !== undefined) {
3532
- log('StrandPeer row already present for strand %s (names %s); insert skipped', row.strandId, held.strandPeerId);
3576
+ logHeldStrandPeer(held, row.role);
3533
3577
  return false;
3534
3578
  }
3535
3579
  const sig = await sign(buildAuthorizationMessage('CadreControl.StrandPeer', 'add', this.config.partyId, strandPeerRowFields(fields)));
3536
3580
  // Bare `exec`: already inside the write lock, which is NOT re-entrant.
3537
3581
  await this.db!.exec(
3538
- 'insert into CadreControl.StrandPeer (StrandId, PeerId, StrandPeerId, Sig) values (?, ?, ?, ?)',
3539
- [fields.strandId, fields.peerId, fields.strandPeerId, sig]
3582
+ `insert into CadreControl.StrandPeer (${STRAND_PEER_COLUMNS}) values (?, ?, ?, ?, ?)`,
3583
+ [fields.strandId, fields.peerId, fields.strandPeerId, fields.role, sig]
3540
3584
  );
3541
3585
  return true;
3542
3586
  }, {}, 'strand-peer-insert');
@@ -3849,7 +3893,7 @@ export class ControlDatabase {
3849
3893
  multiaddr: string;
3850
3894
  updatedAt: number;
3851
3895
  sig: string | null;
3852
- profile: NodeProfile | null;
3896
+ profile: PublishedProfile | null;
3853
3897
  },
3854
3898
  ownerKey: string,
3855
3899
  signMessage: ControlSign