@serfab/cadre-core 0.9.0 → 0.11.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 (193) hide show
  1. package/README.md +255 -253
  2. package/dist/bootstrap-peer-store-file.d.ts +13 -0
  3. package/dist/bootstrap-peer-store-file.js +42 -0
  4. package/dist/bootstrap-peer-store-file.js.map +1 -0
  5. package/dist/bootstrap-peer-store.d.ts +73 -0
  6. package/dist/bootstrap-peer-store.js +128 -0
  7. package/dist/bootstrap-peer-store.js.map +1 -0
  8. package/dist/cadre-node.d.ts +1121 -81
  9. package/dist/cadre-node.js +2276 -262
  10. package/dist/cadre-node.js.map +1 -1
  11. package/dist/control-authorization.d.ts +93 -0
  12. package/dist/control-authorization.js +57 -0
  13. package/dist/control-authorization.js.map +1 -0
  14. package/dist/control-cohort.d.ts +26 -0
  15. package/dist/control-cohort.js +26 -0
  16. package/dist/control-cohort.js.map +1 -1
  17. package/dist/control-database.d.ts +959 -54
  18. package/dist/control-database.js +1421 -148
  19. package/dist/control-database.js.map +1 -1
  20. package/dist/control-formation-recorder.d.ts +90 -10
  21. package/dist/control-formation-recorder.js +130 -14
  22. package/dist/control-formation-recorder.js.map +1 -1
  23. package/dist/control-schema.d.ts +1 -1
  24. package/dist/control-schema.js +651 -72
  25. package/dist/control-schema.js.map +1 -1
  26. package/dist/control-stream.d.ts +38 -4
  27. package/dist/control-stream.js +70 -4
  28. package/dist/control-stream.js.map +1 -1
  29. package/dist/control-write-retry.d.ts +161 -0
  30. package/dist/control-write-retry.js +412 -0
  31. package/dist/control-write-retry.js.map +1 -0
  32. package/dist/delegate-admission.d.ts +134 -0
  33. package/dist/delegate-admission.js +244 -0
  34. package/dist/delegate-admission.js.map +1 -0
  35. package/dist/device-token.d.ts +11 -11
  36. package/dist/device-token.js +14 -13
  37. package/dist/device-token.js.map +1 -1
  38. package/dist/ed25519-key.d.ts +26 -0
  39. package/dist/ed25519-key.js +56 -1
  40. package/dist/ed25519-key.js.map +1 -1
  41. package/dist/file-durable-slot.d.ts +20 -0
  42. package/dist/file-durable-slot.js +53 -0
  43. package/dist/file-durable-slot.js.map +1 -0
  44. package/dist/formation-approval.d.ts +157 -0
  45. package/dist/formation-approval.js +375 -0
  46. package/dist/formation-approval.js.map +1 -0
  47. package/dist/fs-atomic.d.ts +38 -0
  48. package/dist/fs-atomic.js +97 -0
  49. package/dist/fs-atomic.js.map +1 -0
  50. package/dist/identity-key.d.ts +49 -0
  51. package/dist/identity-key.js +81 -0
  52. package/dist/identity-key.js.map +1 -0
  53. package/dist/index.d.ts +17 -7
  54. package/dist/index.js +40 -6
  55. package/dist/index.js.map +1 -1
  56. package/dist/key-store-file.d.ts +2 -24
  57. package/dist/key-store-file.js +10 -77
  58. package/dist/key-store-file.js.map +1 -1
  59. package/dist/membership-connection-gater.d.ts +146 -0
  60. package/dist/membership-connection-gater.js +180 -0
  61. package/dist/membership-connection-gater.js.map +1 -0
  62. package/dist/node-local-snapshot.d.ts +109 -0
  63. package/dist/node-local-snapshot.js +182 -0
  64. package/dist/node-local-snapshot.js.map +1 -0
  65. package/dist/peer-addr-book.d.ts +92 -0
  66. package/dist/peer-addr-book.js +220 -0
  67. package/dist/peer-addr-book.js.map +1 -0
  68. package/dist/peer-authorization.d.ts +135 -18
  69. package/dist/peer-authorization.js +152 -21
  70. package/dist/peer-authorization.js.map +1 -1
  71. package/dist/peer-record.d.ts +21 -8
  72. package/dist/peer-record.js +26 -10
  73. package/dist/peer-record.js.map +1 -1
  74. package/dist/relay-addrs.d.ts +45 -0
  75. package/dist/relay-addrs.js +103 -0
  76. package/dist/relay-addrs.js.map +1 -0
  77. package/dist/relay-reservation.d.ts +266 -0
  78. package/dist/relay-reservation.js +613 -0
  79. package/dist/relay-reservation.js.map +1 -0
  80. package/dist/seed-bootstrap.d.ts +228 -56
  81. package/dist/seed-bootstrap.js +340 -198
  82. package/dist/seed-bootstrap.js.map +1 -1
  83. package/dist/seed-trust-policy.d.ts +47 -21
  84. package/dist/seed-trust-policy.js +24 -15
  85. package/dist/seed-trust-policy.js.map +1 -1
  86. package/dist/strand-addr-protocol.d.ts +42 -13
  87. package/dist/strand-addr-protocol.js +58 -52
  88. package/dist/strand-addr-protocol.js.map +1 -1
  89. package/dist/strand-backfill.d.ts +109 -0
  90. package/dist/strand-backfill.js +279 -0
  91. package/dist/strand-backfill.js.map +1 -0
  92. package/dist/strand-cohort.d.ts +4 -37
  93. package/dist/strand-cohort.js +4 -15
  94. package/dist/strand-cohort.js.map +1 -1
  95. package/dist/strand-database.d.ts +11 -16
  96. package/dist/strand-database.js +19 -7
  97. package/dist/strand-database.js.map +1 -1
  98. package/dist/strand-formation-manager.d.ts +36 -7
  99. package/dist/strand-formation-manager.js +115 -15
  100. package/dist/strand-formation-manager.js.map +1 -1
  101. package/dist/strand-formation-protocol.d.ts +103 -4
  102. package/dist/strand-formation-protocol.js +232 -4
  103. package/dist/strand-formation-protocol.js.map +1 -1
  104. package/dist/strand-instance-manager.d.ts +88 -19
  105. package/dist/strand-instance-manager.js +178 -34
  106. package/dist/strand-instance-manager.js.map +1 -1
  107. package/dist/strand-member-registry.d.ts +5 -5
  108. package/dist/strand-member-registry.js +18 -10
  109. package/dist/strand-member-registry.js.map +1 -1
  110. package/dist/strand-membership-writer.d.ts +484 -88
  111. package/dist/strand-membership-writer.js +751 -128
  112. package/dist/strand-membership-writer.js.map +1 -1
  113. package/dist/strand-solicitation.d.ts +99 -33
  114. package/dist/strand-solicitation.js +100 -52
  115. package/dist/strand-solicitation.js.map +1 -1
  116. package/dist/strand-transport-key.d.ts +40 -0
  117. package/dist/strand-transport-key.js +56 -0
  118. package/dist/strand-transport-key.js.map +1 -0
  119. package/dist/strand-wake-protocol.d.ts +9 -7
  120. package/dist/strand-wake-protocol.js +22 -46
  121. package/dist/strand-wake-protocol.js.map +1 -1
  122. package/dist/strand-watcher.d.ts +17 -2
  123. package/dist/strand-watcher.js +47 -3
  124. package/dist/strand-watcher.js.map +1 -1
  125. package/dist/trusted-owner-store-file.d.ts +12 -0
  126. package/dist/trusted-owner-store-file.js +39 -0
  127. package/dist/trusted-owner-store-file.js.map +1 -0
  128. package/dist/trusted-owner-store.d.ts +64 -0
  129. package/dist/trusted-owner-store.js +126 -0
  130. package/dist/trusted-owner-store.js.map +1 -0
  131. package/dist/types.d.ts +307 -44
  132. package/dist/types.js +13 -0
  133. package/dist/types.js.map +1 -1
  134. package/package.json +20 -8
  135. package/src/arachnode-stub.ts +159 -159
  136. package/src/bootstrap-peer-store-file.ts +44 -0
  137. package/src/bootstrap-peer-store.ts +172 -0
  138. package/src/cadre-node.ts +5158 -2981
  139. package/src/canonical-json.ts +30 -30
  140. package/src/control-authorization.ts +114 -0
  141. package/src/control-cohort.ts +130 -103
  142. package/src/control-database.ts +2406 -948
  143. package/src/control-formation-recorder.ts +305 -151
  144. package/src/control-schema.ts +651 -72
  145. package/src/control-stream.ts +205 -128
  146. package/src/control-write-retry.ts +483 -0
  147. package/src/delegate-admission.ts +302 -0
  148. package/src/device-token.ts +94 -89
  149. package/src/diagnostics/connection-path.ts +277 -277
  150. package/src/diagnostics/webrtc-turn-tracker.ts +222 -222
  151. package/src/ed25519-key.ts +66 -1
  152. package/src/enrollment.ts +199 -199
  153. package/src/file-durable-slot.ts +61 -0
  154. package/src/formation-approval.ts +604 -0
  155. package/src/fs-atomic.ts +107 -0
  156. package/src/hibernation-manager.ts +437 -437
  157. package/src/identity-key.ts +109 -0
  158. package/src/index.ts +137 -5
  159. package/src/key-store-file.ts +132 -203
  160. package/src/key-store.ts +99 -99
  161. package/src/membership-connection-gater.ts +204 -0
  162. package/src/node-local-snapshot.ts +260 -0
  163. package/src/peer-addr-book.ts +250 -0
  164. package/src/peer-authorization.ts +259 -81
  165. package/src/peer-record.ts +164 -146
  166. package/src/push-credentials.ts +84 -84
  167. package/src/push-node.ts +64 -64
  168. package/src/push-notifier-apns.ts +247 -247
  169. package/src/push-notifier-fcm.ts +220 -220
  170. package/src/push-notifier-shared.ts +28 -28
  171. package/src/push-notifier.ts +52 -52
  172. package/src/relay-addrs.ts +111 -0
  173. package/src/relay-reservation.ts +823 -0
  174. package/src/seed-bootstrap.ts +409 -209
  175. package/src/seed-trust-policy.ts +140 -107
  176. package/src/strand-addr-protocol.ts +441 -410
  177. package/src/strand-backfill.ts +366 -0
  178. package/src/strand-cohort.ts +38 -77
  179. package/src/strand-database.ts +202 -204
  180. package/src/strand-formation-manager.ts +491 -368
  181. package/src/strand-formation-protocol.ts +692 -391
  182. package/src/strand-instance-manager.ts +201 -46
  183. package/src/strand-member-key.ts +39 -39
  184. package/src/strand-member-registry.ts +182 -177
  185. package/src/strand-membership-writer.ts +938 -146
  186. package/src/strand-solicitation.ts +472 -407
  187. package/src/strand-transport-key.ts +58 -0
  188. package/src/strand-wake-payload.ts +32 -32
  189. package/src/strand-wake-protocol.ts +314 -331
  190. package/src/strand-watcher.ts +314 -251
  191. package/src/trusted-owner-store-file.ts +41 -0
  192. package/src/trusted-owner-store.ts +171 -0
  193. package/src/types.ts +1309 -1032
@@ -6,6 +6,9 @@ import optimysticPlugin from '@optimystic/quereus-plugin-optimystic/plugin';
6
6
  import { digest, randomBytes } from '@optimystic/quereus-plugin-crypto';
7
7
  import { CONTROL_SCHEMA } from './control-schema.js';
8
8
  import { canonicalDatetime } from './canonical-datetime.js';
9
+ import { controlAuthorizationFields, CONTROL_TABLES } from './control-authorization.js';
10
+ import { requireEd25519PublicKeyB64 } from './ed25519-key.js';
11
+ import { retryControlWrite, SCHEMA_INIT_RETRY_POLICY } from './control-write-retry.js';
9
12
  const log = debug('sereus:cadre:control-db');
10
13
  const timing = debug('sereus:cadre:timing');
11
14
  /**
@@ -29,6 +32,71 @@ export function generateStampId(peerId) {
29
32
  // Convert to base64url
30
33
  return uint8ArrayToString(combined, 'base64url');
31
34
  }
35
+ /**
36
+ * A `FormationUsage` was to be recorded against a host `Strand` row that is not
37
+ * present locally.
38
+ *
39
+ * Consent is bound to the strand's one-off `StampId` (see the consent branch of
40
+ * `Strand.AuthorizedInsert`), so the writer must read the live row before inserting.
41
+ * The ordinary "host strand has not converged on this responder yet" case is caught
42
+ * earlier and reported as `missing` by `ControlFormationUsageRecorder.resolveStrand`;
43
+ * reaching here means the row vanished between that check and the write, which is a
44
+ * genuine race and is surfaced rather than left to fail the deferred `StrandExists`
45
+ * CHECK at commit (which would drop the write with a far less legible error).
46
+ */
47
+ export class MissingHostStrandError extends Error {
48
+ constructor(strandId, token) {
49
+ super(`Cannot record formation usage for token ${token}: host strand ${strandId} is not present`);
50
+ this.strandId = strandId;
51
+ this.token = token;
52
+ this.name = 'MissingHostStrandError';
53
+ }
54
+ }
55
+ /**
56
+ * A formation write was abandoned because the caller's `AbortSignal` fired BEFORE the
57
+ * `FormationUsage` insert was issued — the invite's single use is NOT spent.
58
+ *
59
+ * Thrown by the formation write paths ({@link ControlDatabase.recordFormationUsage},
60
+ * {@link ControlDatabase.redeemInvitation}) and by `ControlFormationUsageRecorder`'s
61
+ * pre-approval checks when the responder's provisioning budget expires while the work is
62
+ * still queued (e.g. behind the write lock) or still asking the approval hook. Never
63
+ * thrown once the insert has been issued: a half-issued write must be allowed to land,
64
+ * and the formation listener's settle grace then adopts it as a successful join. The
65
+ * manager rethrows this error instead of mapping it to a retryable conflict — the
66
+ * listener's timeout path owns the reply.
67
+ */
68
+ export class FormationAbortedError extends Error {
69
+ constructor(token, operation, options) {
70
+ super(`Formation ${operation} for token ${token} was aborted before its write was issued`, options);
71
+ this.token = token;
72
+ this.name = 'FormationAbortedError';
73
+ }
74
+ }
75
+ /**
76
+ * The invitation's seat budget is already spent: the count of recorded `FormationUsage` rows
77
+ * for the token has reached `FormationInvite.TotalUses`, so this redemption cannot be given a
78
+ * seat.
79
+ *
80
+ * Raised off the committed-count check ({@link ControlDatabase.assertSeatRemains}) ahead of
81
+ * the write, inside {@link ControlDatabase.redeemInvitation} /
82
+ * {@link ControlDatabase.recordFormationUsage} — and off nothing else. It exists because
83
+ * without it the refusal surfaces as a generic `CHECK constraint failed: Authorized` from the
84
+ * schema's own count-based cap clause, which the manager reports as a retryable
85
+ * `Formation conflict, retry` — telling the joiner to retry something that can never succeed.
86
+ * Two redemptions of one token racing on the SAME node serialize behind the local write
87
+ * queue, so the loser reads the winner's committed row and is refused here by name —
88
+ * `StrandFormationManager.validateToken`'s `isTokenUsed` check cannot catch that, since it
89
+ * ran before either redemption wrote anything.
90
+ */
91
+ export class InvitationExhaustedError extends Error {
92
+ constructor(token, usesRecorded, totalUses) {
93
+ super(`Invitation ${token} is exhausted: ${usesRecorded} of ${totalUses} use(s) already recorded`);
94
+ this.token = token;
95
+ this.usesRecorded = usesRecorded;
96
+ this.totalUses = totalUses;
97
+ this.name = 'InvitationExhaustedError';
98
+ }
99
+ }
32
100
  /**
33
101
  * Parse a stored Quereus `datetime` value into epoch milliseconds.
34
102
  *
@@ -43,40 +111,123 @@ function parseStoredDatetimeMs(value) {
43
111
  const hasZone = /[zZ]$|[+-]\d\d:?\d\d$/.test(value);
44
112
  return new Date(hasZone ? value : `${value}Z`).getTime();
45
113
  }
114
+ /**
115
+ * Parse a nullable stored `datetime` column into epoch ms, mapping both "absent"
116
+ * and "unparseable" to null — i.e. "no expiry". Shared by every `ExpiresAt`
117
+ * reader so the NaN guard cannot drift between them.
118
+ */
119
+ function parseNullableStoredDatetimeMs(value) {
120
+ if (value === null || value === undefined)
121
+ return null;
122
+ const parsed = parseStoredDatetimeMs(value);
123
+ return Number.isNaN(parsed) ? null : parsed;
124
+ }
46
125
  /**
47
126
  * Build the canonical authorization message that owner signatures are bound to.
48
127
  *
49
- * The message is a SINGLE framed SHA-256 digest over the ordered tuple of fields (the
50
- * crypto plugin's injective multi-field encoding), in a fixed field order, with the
51
- * single-use StampId as the final field:
128
+ * The message is a SINGLE framed SHA-256 digest over the ordered field vector from
129
+ * {@link controlAuthorizationFields} (the crypto plugin's injective multi-field
130
+ * encoding): two fixed literals — the domain tag naming the table rule and the action
131
+ * tag — followed by the row fields in the schema's fixed order, with the single-use
132
+ * StampId as the final field where the table has one:
52
133
  *
53
- * message = sha256(encodeFields([field_1, field_2, ..., StampId])) // raw digest bytes
134
+ * message = sha256(encodeFields([domain, action, field_1, ..., StampId])) // raw digest bytes
54
135
  *
55
136
  * ed25519 signs these raw digest bytes DIRECTLY (no second hash). The SQL constraints
56
137
  * verify the identical bytes with one variadic call
57
- * (`verify(digest(field_1, ..., StampId), context.Signature, A.Key, 'ed25519')`): SQL
58
- * `digest(...)` returns the base64url string of the same digest, which `verify`'s default
138
+ * (`verify(digest('CadreControl.X', 'add', field_1, ...), context.Signature, A.Key, 'ed25519')`):
139
+ * SQL `digest(...)` returns the base64url string of the same digest, which `verify`'s default
59
140
  * base64url input encoding decodes back to those raw bytes — so signer and verifier
60
141
  * operate on the same bytes. Every field is TEXT on both sides (the SQL columns are
61
142
  * `cast(... as text)` / `coalesce(...,'')`; the TS args are strings), so the per-field
62
- * type tags agree. This binds the signature to the row contents (not a bare stamp),
63
- * closing the captured-stamp replay / privilege-escalation hole. Single source of truth:
64
- * every signed writer (and every test/harness signer) MUST build the message through this
65
- * function in the schema's field order, or `verify` will reject the row.
143
+ * type tags agree. Binding the row contents closes captured-stamp replay; the leading
144
+ * domain/action tags scope the signature to ONE table rule, so an approval minted for
145
+ * one constraint can never satisfy another (e.g. a ValidationKey enrollment can no
146
+ * longer double as an OwnerKey enrollment). Single source of truth: every signed writer
147
+ * (and every test/harness signer) MUST build the message through this function with the
148
+ * schema's tags and field order, or `verify` will reject the row.
66
149
  */
67
- export function buildAuthorizationMessage(fields) {
68
- return digest([...fields], 'sha256', 'bytes');
150
+ export function buildAuthorizationMessage(domain, action, rowFields) {
151
+ return digest(controlAuthorizationFields(domain, action, rowFields), 'sha256', 'bytes');
69
152
  }
70
- /** Runtime guard mirroring {@link ControlTable} for the dynamic-`from` count. */
71
- const CONTROL_TABLES = new Set([
72
- 'OwnerKey',
73
- 'ValidationKey',
74
- 'Strand',
75
- 'CadrePeer',
76
- 'DeviceToken',
77
- 'FormationInvite',
78
- 'FormationUsage',
79
- ]);
153
+ /**
154
+ * The exact bytes an outside approver signs to authorize ONE redemption of a
155
+ * `ValidationUrl`-bearing `FormationInvite` — the TS mirror of the `'vouch'` digest in
156
+ * `FormationUsage.Authorized`, in the schema's field order.
157
+ *
158
+ * The approval covers the whole redemption: the invitation (`token`), the single-use nonce
159
+ * the JOINING peer minted for THIS redemption (`usageStampId`), the network being joined
160
+ * (`strandId`), the joining peer's own ed25519 public key (`peerKey`), and the disclosure
161
+ * text. That makes it non-transferable — an approval cannot be re-presented for another use
162
+ * of the same invitation, another network, or another joiner — and the nonce's `unique`
163
+ * column makes a verbatim re-presentation a duplicate-row rejection. The joiner mints the
164
+ * nonce ({@link generateStampId}) and sends it in its contact message; sign these bytes over
165
+ * that same nonce, then pass BOTH the nonce and the signature to
166
+ * {@link ControlDatabase.redeemInvitation} / {@link ControlDatabase.recordFormationUsage}:
167
+ * signing one nonce and inserting another fails the CHECK.
168
+ */
169
+ export function formationVouchMessage(fields) {
170
+ return buildAuthorizationMessage('CadreControl.FormationUsage', 'vouch', [
171
+ fields.token, fields.usageStampId, fields.strandId, fields.peerKey, fields.disclosure,
172
+ ]);
173
+ }
174
+ /**
175
+ * The exact bytes the JOINING peer signs to consent to ONE redemption of a
176
+ * `FormationInvite` — the TS mirror of the `'consent'` digest in the schema's
177
+ * `FormationUsage.PeerConsented` constraint, in its field order.
178
+ *
179
+ * A sibling of {@link formationVouchMessage}, not a replacement: the approver's vouch
180
+ * says the redemption may proceed, this says the joiner itself agreed to it, and the
181
+ * two are signed by different keys over the same nonce so neither can stand in for
182
+ * the other. Verify the result with `peer-authorization.ts`'s
183
+ * `verifyFormationConsent`, whose `formationConsentDigest` is the base64url twin of
184
+ * this vector — the two must stay in lockstep.
185
+ *
186
+ * `strandId` is deliberately absent, unlike its vouch sibling: the joiner cannot know
187
+ * the strand when it signs (a bound invite's host strand arrives only in the result
188
+ * frame; an unbound strand is minted by the responder). The responder cannot
189
+ * substitute one anyway — a bound invite is pinned to its own strand by `Authorized`,
190
+ * and an unbound redemption mints a fresh strand, so there is no victim to name.
191
+ */
192
+ export function formationConsentMessage(fields) {
193
+ return buildAuthorizationMessage('CadreControl.FormationUsage', 'consent', [
194
+ fields.token, fields.usageStampId, fields.peerKey, fields.disclosure,
195
+ ]);
196
+ }
197
+ /** Runtime guard for the dynamic-`from` count, over the one table list. */
198
+ const CONTROL_TABLE_SET = new Set(CONTROL_TABLES);
199
+ /**
200
+ * Which column identifies one row of each guarded table — the ONLY place that mapping
201
+ * lives, so a stamp read and the delete that binds to it can never disagree about it.
202
+ * Derived rather than passed: every guarded table has exactly one primary key, so a
203
+ * `(table, keyColumn)` pair on a signature is a mismatch waiting to happen.
204
+ */
205
+ const GUARDED_KEY_COLUMN = {
206
+ OwnerKey: 'Key',
207
+ CadrePeer: 'PeerId',
208
+ ValidationKey: 'Key',
209
+ Strand: 'Id',
210
+ DeviceToken: 'PeerId',
211
+ };
212
+ /**
213
+ * Guarded tables a node may reap locally once their tombstone has committed — the
214
+ * tables whose `AuthorizedDelete` carries the REAP branch (see the constraint comment on
215
+ * `CadrePeer.AuthorizedDelete` in the schema). `Strand` is deliberately absent: its row
216
+ * carries `MemberPrivateKey`, the party's own membership secret for that network, stored
217
+ * nowhere else (tickets/backlog/debt-strand-tombstone-reap.md owns any future change).
218
+ * `OwnerKey` has no production removal path and `MinOneOwner` makes an automated
219
+ * owner-key reap a party-bricking hazard.
220
+ */
221
+ export const REAPABLE_TABLES = ['CadrePeer', 'DeviceToken', 'ValidationKey'];
222
+ /**
223
+ * Runtime membership test for {@link REAPABLE_TABLES}, narrowing a tombstone's
224
+ * `TableName` to the subset {@link ControlDatabase.reapRevokedRow} accepts. A tombstone
225
+ * naming an excluded table (`Strand`, `OwnerKey`) is skipped silently — `Strand`
226
+ * tombstones are the common case in a party that has unpublished a strand, so a log line
227
+ * per skip would be per-pass noise.
228
+ */
229
+ const REAPABLE_TABLE_SET = new Set(REAPABLE_TABLES);
230
+ const isReapableTable = (table) => REAPABLE_TABLE_SET.has(table);
80
231
  /**
81
232
  * ControlDatabase manages the CadreControl schema using Quereus with Optimystic backend.
82
233
  * It provides typed query methods for accessing control network data.
@@ -86,6 +237,17 @@ export class ControlDatabase {
86
237
  this.db = null;
87
238
  this.collectionFactory = null;
88
239
  this.initialized = false;
240
+ this.membershipListener = null;
241
+ this.guardedDeleteListener = null;
242
+ /** Tail of the local-write chain — see {@link withWriteLock}. */
243
+ this.writeQueue = Promise.resolve();
244
+ /**
245
+ * Pacing seams for {@link lockedWithRetry}. Production leaves this empty (real backoff,
246
+ * real clock); specs inject a recorded `sleep` / fake `now` so no test waits out a real
247
+ * backoff. Applied OVER the call site's policy, so it overrides pacing without a spec
248
+ * having to restate the policy's attempts or classifier.
249
+ */
250
+ this.controlWriteRetryPacing = {};
89
251
  this.config = config;
90
252
  }
91
253
  /**
@@ -151,6 +313,20 @@ export class ControlDatabase {
151
313
  timing('[controlDb] hydrate: %dms (tables=%d, indexes=%d)', Math.round(performance.now() - t0), hydrated.tables, hydrated.indexes);
152
314
  log('Hydrated control catalog (tables=%d, indexes=%d)', hydrated.tables, hydrated.indexes);
153
315
  // Load and execute the schema
316
+ // NOTE: this is where a slow launch is felt. Duration is (raw-storage operations
317
+ // issued) × per-operation storage latency: ~1ms/op on an idle machine, but
318
+ // 50-90ms/op on a loaded disk or a phone's flash under launch contention. A cold
319
+ // start now reaches the backend 172 times (8 tables + 1 index, 21 distinct blocks
320
+ // — dominated by its 130 genuine writes), a warm restart 52, because cadre-core
321
+ // wraps every embedder storage in `@optimystic/db-p2p`'s write-through cache
322
+ // (@serfab/quereus-plugin-sereus's cached-storage.ts). Uncached the same start issued ~2000 operations — the
323
+ // upstream re-read amplification measured in
324
+ // tickets/complete/optimystic-block-read-amplification-on-control-start.md (and
325
+ // its follow-up ...-schema-catalog-reread-per-write-blows-storage-budgets.md),
326
+ // which also rules out the retry policy and cluster deadlines. The counts are
327
+ // pinned by packages/cadre-core/test/control-start-storage-op-budget.spec.ts; if
328
+ // they grow — e.g. the cache leaves this path — that spec fails before anyone
329
+ // has to debug a launch.
154
330
  t0 = performance.now();
155
331
  await this.loadSchema();
156
332
  timing('[controlDb] loadSchema: %dms', Math.round(performance.now() - t0));
@@ -188,7 +364,36 @@ export class ControlDatabase {
188
364
  log('Using embedded control schema');
189
365
  schemaContent = CONTROL_SCHEMA;
190
366
  }
191
- await this.db.exec(schemaContent);
367
+ // Schema init is a DISTRIBUTED write, so it gets the same bounded transient-failure
368
+ // retry every other control write does ({@link lockedWithRetry}). Every CadreControl
369
+ // table is optimystic-backed (the default vtab was routed to optimystic just above),
370
+ // so each `create table` needs a super-majority of the party's peers to answer. On a
371
+ // node joining a party that already has two live members a single unanswered peer
372
+ // during any one create otherwise kills startup outright.
373
+ //
374
+ // Re-running the WHOLE `exec` after a partial failure is safe, and this is the
375
+ // non-obvious part:
376
+ //
377
+ // - `apply schema` is a DIFF, not a replay. Quereus collects the live catalog, diffs
378
+ // the declared schema against it, and only emits DDL for what is missing — which is
379
+ // exactly why `initialize` hydrates persisted optimystic schemas BEFORE getting here.
380
+ // Tables that already landed generate no statements on the second pass.
381
+ // - a failed `create table` leaves the catalog CLEAN. `SchemaManager.createTable` calls
382
+ // the module's `create` first and only registers the table on success, so the table
383
+ // that failed is not in the catalog, and neither are the ones after it.
384
+ //
385
+ // Together: attempt 2 re-emits exactly the failed table and its successors. No
386
+ // `if not exists` juggling, no per-table loop, no statement splitting.
387
+ //
388
+ // Only the "cohort did not answer, nothing committed" class is retried; the classifier
389
+ // vetoes indeterminate commits and does not match `Missing block`, which is a durable
390
+ // convergence fault a retry cannot heal (tracked separately) and must keep propagating.
391
+ //
392
+ // This is the ONE call site on a non-default policy: the re-run safety argued above is
393
+ // also what lets schema init absorb optimystic's self-coordination grace refusal, which
394
+ // is unsafe for control writes in general. Why, on
395
+ // `RETRIABLE_SCHEMA_INIT_MATCHERS` in `control-write-retry.ts`.
396
+ await this.lockedWithRetry(() => this.db.exec(schemaContent), SCHEMA_INIT_RETRY_POLICY, 'schema-init');
192
397
  log('Schema loaded and executed');
193
398
  }
194
399
  /**
@@ -226,7 +431,7 @@ export class ControlDatabase {
226
431
  /**
227
432
  * Count rows in a CadreControl table as seen by THIS database instance.
228
433
  *
229
- * `table` is validated against {@link CONTROL_TABLES} before it is interpolated
434
+ * `table` is validated against {@link CONTROL_TABLE_SET} before it is interpolated
230
435
  * into the `from` clause: the names are not user input, but the check keeps the
231
436
  * dynamic query off the injection surface and fails loudly on a typo instead of
232
437
  * emitting a malformed statement. The count reflects only the rows this node's
@@ -236,7 +441,7 @@ export class ControlDatabase {
236
441
  */
237
442
  async countRows(table) {
238
443
  this.ensureInitialized();
239
- if (!CONTROL_TABLES.has(table)) {
444
+ if (!CONTROL_TABLE_SET.has(table)) {
240
445
  throw new Error(`Unknown CadreControl table: ${table}`);
241
446
  }
242
447
  for await (const row of this.db.eval(`select count(1) as Count from CadreControl.${table}`)) {
@@ -269,11 +474,12 @@ export class ControlDatabase {
269
474
  */
270
475
  async ensureOwnerKey(key) {
271
476
  this.ensureInitialized();
477
+ const trimmed = requireEd25519PublicKeyB64(key, 'owner key');
272
478
  if (await this.hasOwnerKey()) {
273
479
  log('Owner key already present; skipping genesis insert');
274
480
  return false;
275
481
  }
276
- await this.insertOwnerKey(key);
482
+ await this.insertOwnerKey(trimmed);
277
483
  return true;
278
484
  }
279
485
  /**
@@ -294,15 +500,42 @@ export class ControlDatabase {
294
500
  }
295
501
  /**
296
502
  * Enumerate the CadrePeer rows (cadre membership) for admin/membership reads.
503
+ * Includes the persisted voucher columns, which
504
+ * {@link CadreNode.listAuthorizedMembers} re-checks against its node-local anchor.
505
+ *
506
+ * Rows whose `StampId` is retired in `CadreControl.Revocation` are excluded HERE, so
507
+ * every membership reader inherits the exclusion — a revoked peer is neither
508
+ * authorizable nor addressable (a replayed row planted at a retired stamp must not
509
+ * be RPC'd or handed out as an address). Reads the retired set through
510
+ * {@link queryRevokedStamps} rather than inlining the SQL, so tests can interpose on
511
+ * that seam to model the replicated live-row-plus-tombstone merge state.
512
+ *
513
+ * NOTE: runs a second query (the retired-stamp set) per call, and this sits on the
514
+ * membership-gate refresh path; control tables are a handful of rows today, so this
515
+ * is cheap — if they ever grow, fold the exclusion into one statement or cache the
516
+ * retired-stamp set.
517
+ *
518
+ * NOTE: this filter is HAND-MIRRORED by the gate suites' fake control databases
519
+ * (`test/membership-gate-helpers.ts` and `test/cadre-node-authorized-surface.spec.ts`),
520
+ * which feed the node rows directly. Nothing enforces that the copies stay true, so a
521
+ * change to the filter's SHAPE here (what it keys on, which table it consults) must be
522
+ * carried into both, or those suites keep passing against a contract the database no
523
+ * longer honours. Fine while the filter is one `Set.has` on `StampId`; if it grows a
524
+ * second dimension, give the fakes a shared fixture or a contract test instead.
297
525
  */
298
526
  async queryCadrePeers() {
299
527
  this.ensureInitialized();
528
+ const revoked = await this.queryRevokedStamps('CadrePeer');
300
529
  const rows = [];
301
530
  for await (const row of this.db.eval('select PeerId, Multiaddr, StampId, VouchOwner, VouchSig from CadreControl.CadrePeer')) {
531
+ const stampId = row.StampId ?? null;
532
+ if (stampId !== null && revoked.has(stampId)) {
533
+ continue;
534
+ }
302
535
  rows.push({
303
536
  peerId: row.PeerId,
304
537
  multiaddr: row.Multiaddr ?? null,
305
- stampId: row.StampId ?? null,
538
+ stampId,
306
539
  vouchOwner: row.VouchOwner ?? null,
307
540
  vouchSig: row.VouchSig ?? null,
308
541
  });
@@ -310,22 +543,127 @@ export class ControlDatabase {
310
543
  return rows;
311
544
  }
312
545
  /**
313
- * Read a single `CadrePeer` row's single-use `StampId` nonce (null when the row
314
- * does not exist). Used by the owner delete / re-touch paths, which must bind
315
- * their signature to the row's CURRENT nonce ({@link cadrePeerRemoveDigest} /
316
- * {@link cadrePeerVoucherDigest}).
546
+ * Every approver public key currently enrolled in `CadreControl.ValidationKey` — the
547
+ * set a `ValidationUrl` redemption's approval signature must be signed by (see
548
+ * {@link ControlFormationUsageRecorder}, which checks one key at a time via
549
+ * {@link queryValidationKeyStampId}).
550
+ *
551
+ * Sorted in TypeScript rather than SQL so the order is stable regardless of storage
552
+ * order; the enrolled set is a handful of keys, so the sort is free.
553
+ */
554
+ async queryValidationKeys() {
555
+ this.ensureInitialized();
556
+ const keys = [];
557
+ for await (const row of this.db.eval('select Key from CadreControl.ValidationKey')) {
558
+ keys.push(row.Key);
559
+ }
560
+ return keys.sort();
561
+ }
562
+ /**
563
+ * Read one guarded row's single-use `StampId` nonce (null when the row does not
564
+ * exist). Every owner-signed delete / re-touch path must bind its signature to the
565
+ * row's CURRENT nonce, so they all read through here first.
566
+ *
567
+ * Deliberately RAW — no `Revocation` filtering (unlike {@link queryCadrePeers} /
568
+ * {@link queryPeerRecord}): {@link deleteGuardedRow} and the insert-if-absent guard
569
+ * in the `CadrePeer` upsert need to see a physically present row. Filtering here
570
+ * would make an insert-if-absent guard read "absent" for a row that exists and
571
+ * collide on the primary key.
572
+ *
573
+ * `table` and its {@link GUARDED_KEY_COLUMN} column are interpolated into the SQL; both
574
+ * come from closed literal unions — no caller-supplied string can reach the statement
575
+ * (same injection-surface discipline as {@link countRows}'s `CONTROL_TABLE_SET` guard).
317
576
  */
318
- async queryCadrePeerStampId(peerId) {
577
+ async queryStampId(table, keyValue) {
319
578
  this.ensureInitialized();
320
- for await (const row of this.db.eval('select StampId from CadreControl.CadrePeer where PeerId = ?', [peerId])) {
579
+ const sql = `select StampId from CadreControl.${table} where ${GUARDED_KEY_COLUMN[table]} = ?`;
580
+ for await (const row of this.db.eval(sql, [keyValue])) {
321
581
  return row.StampId ?? null;
322
582
  }
323
583
  return null;
324
584
  }
585
+ /**
586
+ * `CadrePeer` stamp nonce — read by {@link SeedBootstrapService.removePeer} as its
587
+ * row-present gate before it delegates to {@link deleteCadrePeer}. The in-class
588
+ * `CadrePeer` writers ({@link insertCadrePeer}, {@link reauthorizeCadrePeer}) go
589
+ * straight to {@link queryStampId}.
590
+ */
591
+ queryCadrePeerStampId(peerId) {
592
+ return this.queryStampId('CadrePeer', peerId);
593
+ }
594
+ /**
595
+ * `DeviceToken` stamp nonce. {@link deleteDeviceToken} reads the stamp itself, so this
596
+ * is the reader for callers that need to observe a live row's nonce — asserting a token
597
+ * was seated, or that a removal retired the stamp it named.
598
+ */
599
+ queryDeviceTokenStampId(peerId) {
600
+ return this.queryStampId('DeviceToken', peerId);
601
+ }
602
+ /** `Strand` stamp nonce — bound into {@link deleteStrand}'s remove digest. */
603
+ queryStrandStampId(strandId) {
604
+ return this.queryStampId('Strand', strandId);
605
+ }
606
+ /** `ValidationKey` stamp nonce — bound into {@link deleteValidationKey}'s remove digest. */
607
+ queryValidationKeyStampId(key) {
608
+ return this.queryStampId('ValidationKey', key);
609
+ }
610
+ /**
611
+ * Collect the retired `StampId` nonces recorded in `CadreControl.Revocation` for one
612
+ * {@link RevocableTable}. A stamp
613
+ * lands here when its row is removed ({@link SeedBootstrapService.removePeer},
614
+ * {@link deleteValidationKey}, {@link deleteStrand}), and retirement is permanent —
615
+ * rows are never deleted (only their `ReissuedAt` counter moves, via
616
+ * {@link reissueRevocations}). Read-side mitigation for the write-time race: the
617
+ * schema's `NotRevoked` CHECK only sees locally visible tombstones, so a node that
618
+ * converged on a resurrected row before its tombstone can hold both; readers
619
+ * ({@link queryCadrePeers}, {@link queryPeerRecord},
620
+ * {@link CadreNode.resolveDeviceToken}) drop any row whose stamp appears here.
621
+ *
622
+ * NOTE: re-reads the whole retired set on every call, and the caller runs per inbound
623
+ * gate request while the table only ever grows. Cheap today (a cadre removes peers
624
+ * rarely); if removals ever become routine, cache the set and invalidate it on write.
625
+ */
626
+ async queryRevokedStamps(tableName) {
627
+ this.ensureInitialized();
628
+ const stamps = new Set();
629
+ for await (const row of this.db.eval('select StampId from CadreControl.Revocation where TableName = ?', [tableName])) {
630
+ stamps.add(row.StampId);
631
+ }
632
+ return stamps;
633
+ }
634
+ /**
635
+ * Every locally-held `CadreControl.Revocation` tombstone — identity triple plus its
636
+ * `ReissuedAt` counter. Consumed by the cohort-growth re-issue sweep, which
637
+ * enumerates what this node holds before {@link reissueRevocations} re-broadcasts
638
+ * it. Plain scan with no `where`, so the composite-primary-key point-lookup hazard
639
+ * (see the statement comment in {@link reissueRevocations}) does not arise. Unlocked,
640
+ * like every other read.
641
+ */
642
+ async queryRevocations() {
643
+ this.ensureInitialized();
644
+ const rows = [];
645
+ for await (const row of this.db.eval('select TableName, RowKey, StampId, ReissuedAt from CadreControl.Revocation')) {
646
+ rows.push({
647
+ tableName: row.TableName,
648
+ rowKey: row.RowKey,
649
+ stampId: row.StampId,
650
+ reissuedAt: row.ReissuedAt ?? 0,
651
+ });
652
+ }
653
+ return rows;
654
+ }
325
655
  /**
326
656
  * Read a single peer's address record (the full `CadrePeer` row) by PeerId.
327
657
  *
328
- * Returns null when no row exists. Missing/legacy column values are coalesced
658
+ * Returns null when no row exists, or when the row's `StampId` is retired in
659
+ * `CadreControl.Revocation` — a revoked peer reads as absent, so the resolver
660
+ * ({@link CadreNode.resolvePeerAddrs}) never hands out a revoked peer's addresses.
661
+ * The stamp is read for that check only and then dropped; the returned
662
+ * {@link PeerAddressRecord} shape is unchanged. Reads the retired set through
663
+ * {@link queryRevokedStamps} rather than inlining the SQL, so tests can interpose on
664
+ * that seam (see {@link queryCadrePeers}).
665
+ *
666
+ * Missing/legacy column values are coalesced
329
667
  * to their empty form (`''` key/sig, `[]` addrs, `0` stamp) so the caller's
330
668
  * verify/freshness gates uniformly reject an unpublished or malformed row.
331
669
  * The split `addrs` re-join to the exact stored `Multiaddr` (split-on-`,` is
@@ -334,7 +672,12 @@ export class ControlDatabase {
334
672
  */
335
673
  async queryPeerRecord(peerId) {
336
674
  this.ensureInitialized();
337
- for await (const row of this.db.eval('select PeerId, PublicKey, Multiaddr, UpdatedAt, Sig from CadreControl.CadrePeer where PeerId = ?', [peerId])) {
675
+ const revoked = await this.queryRevokedStamps('CadrePeer');
676
+ for await (const row of this.db.eval('select PeerId, PublicKey, Multiaddr, UpdatedAt, Sig, StampId from CadreControl.CadrePeer where PeerId = ?', [peerId])) {
677
+ const stampId = row.StampId ?? null;
678
+ if (stampId !== null && revoked.has(stampId)) {
679
+ return null;
680
+ }
338
681
  const multiaddr = row.Multiaddr ?? '';
339
682
  return {
340
683
  peerId: row.PeerId,
@@ -355,16 +698,29 @@ export class ControlDatabase {
355
698
  * is the refresh path for any member — owner or drone — once its row
356
699
  * exists. `PublicKey` is intentionally not in the SET list (it is immutable on
357
700
  * self-update and the constraint enforces `new.PublicKey = old.PublicKey`).
701
+ *
702
+ * Deliberately NOT wrapped in {@link mutateCadrePeer}, the one `CadrePeer` mutator that
703
+ * is not: it only ever touches THIS node's own row (sole caller
704
+ * `CadreNode.publishSelfRecord`), and `CadreNode.listAuthorizedMembers` filters self out
705
+ * of the membership snapshot, so it cannot change that snapshot. It also runs on the
706
+ * periodic self-registration refresh, where a notify would add a recurring membership
707
+ * read for nothing.
708
+ *
709
+ * Its INSERT counterpart (`SeedBootstrapService.insertSelfPeerRecord`) does notify. Not an
710
+ * inconsistency: it shares the one owner-signed insert path with every other member's
711
+ * row, and a self insert happens once at startup, so the wasted refresh is a single read
712
+ * — cheaper than a conditional carve-out inside the shared writer. Only the repeating
713
+ * path is worth exempting.
358
714
  */
359
715
  async updateSelfPeerRecord(record) {
360
716
  this.ensureInitialized();
361
717
  const multiaddr = record.addrs.join(',');
362
- await this.db.exec(`
363
- update CadreControl.CadrePeer
364
- with context OwnerKey = null, Signature = ?
365
- set Multiaddr = ?, UpdatedAt = ?, Sig = ?
366
- where PeerId = ?
367
- `, [record.sig, multiaddr, record.updatedAt, record.sig, record.peerId]);
718
+ await this.execWrite(`
719
+ update CadreControl.CadrePeer
720
+ with context OwnerKey = null, Signature = ?
721
+ set Multiaddr = ?, UpdatedAt = ?, Sig = ?
722
+ where PeerId = ?
723
+ `, [record.sig, multiaddr, record.updatedAt, record.sig, record.peerId], 'self-record-update');
368
724
  log('Self peer record updated: %s (updatedAt=%d)', record.peerId, record.updatedAt);
369
725
  }
370
726
  /**
@@ -375,16 +731,21 @@ export class ControlDatabase {
375
731
  * gates uniformly reject an unpublished or malformed row. `platform` is returned
376
732
  * verbatim (the resolver validates it against {@link PushPlatform} and re-verifies
377
733
  * the self-signature, which covers the platform field).
734
+ *
735
+ * `stampId` rides along ({@link DeviceTokenRow}) because the resolver must drop a
736
+ * row whose stamp is retired in `CadreControl.Revocation`; it is NOT part of
737
+ * {@link DeviceTokenRecord}, which is the self-signed shape the peer's `Sig` covers.
378
738
  */
379
739
  async queryDeviceToken(peerId) {
380
740
  this.ensureInitialized();
381
- for await (const row of this.db.eval('select PeerId, Platform, Token, UpdatedAt, Sig from CadreControl.DeviceToken where PeerId = ?', [peerId])) {
741
+ for await (const row of this.db.eval('select PeerId, Platform, Token, UpdatedAt, Sig, StampId from CadreControl.DeviceToken where PeerId = ?', [peerId])) {
382
742
  return {
383
743
  peerId: row.PeerId,
384
744
  platform: (row.Platform ?? ''),
385
745
  token: row.Token ?? '',
386
746
  updatedAt: row.UpdatedAt ?? 0,
387
747
  sig: row.Sig ?? '',
748
+ stampId: row.StampId ?? '',
388
749
  };
389
750
  }
390
751
  return null;
@@ -402,12 +763,12 @@ export class ControlDatabase {
402
763
  */
403
764
  async updateSelfDeviceToken(record) {
404
765
  this.ensureInitialized();
405
- await this.db.exec(`
406
- update CadreControl.DeviceToken
407
- with context OwnerKey = null, Signature = ?
408
- set Platform = ?, Token = ?, UpdatedAt = ?, Sig = ?
409
- where PeerId = ?
410
- `, [record.sig, record.platform, record.token, record.updatedAt, record.sig, record.peerId]);
766
+ await this.execWrite(`
767
+ update CadreControl.DeviceToken
768
+ with context OwnerKey = null, Signature = ?
769
+ set Platform = ?, Token = ?, UpdatedAt = ?, Sig = ?
770
+ where PeerId = ?
771
+ `, [record.sig, record.platform, record.token, record.updatedAt, record.sig, record.peerId], 'device-token-update');
411
772
  log('Self device token updated: %s (platform=%s, updatedAt=%d)', record.peerId, record.platform, record.updatedAt);
412
773
  }
413
774
  /**
@@ -416,16 +777,20 @@ export class ControlDatabase {
416
777
  async insertOwnerKey(key) {
417
778
  this.ensureInitialized();
418
779
  log('Inserting owner key: %s', key);
419
- // Bootstrap is authorized by the schema's `(select count(1) from OwnerKey) <= 1`
420
- // branch, so no signature is needed. We still persist a fresh, unique StampId in the
421
- // row's own column to satisfy the not-null/unique anti-replay constraint — the StampId
422
- // is now a real column value, not the optimystic `StampId()` SQL function.
780
+ // Bootstrap is authorized by the schema's genesis branch — `(select count(1) from
781
+ // committed.OwnerKey) = 0`, i.e. the party had no owner before this transaction — so no
782
+ // signature is needed. Every other branch of `OwnerKey.Authorized` requires a signature
783
+ // from a PRE-EXISTING owner, so this method only ever succeeds on a fresh party; seating
784
+ // a second owner (or removing one) has no writer here and must sign the digests
785
+ // documented on the schema's `Authorized` constraint. We still persist a fresh, unique
786
+ // StampId in the row's own column to satisfy the not-null/unique anti-replay constraint —
787
+ // the StampId is a real column value, not the optimystic `StampId()` SQL function.
423
788
  const stampId = generateStampId(this.config.libp2pNode.peerId.toString());
424
- await this.db.exec(`
425
- insert into CadreControl.OwnerKey (Key, StampId)
426
- with context OwnerKey = null, Signature = null
427
- values (?, ?)
428
- `, [key, stampId]);
789
+ await this.execWrite(`
790
+ insert into CadreControl.OwnerKey (Key, StampId)
791
+ with context OwnerKey = null, Signature = null
792
+ values (?, ?)
793
+ `, [key, stampId], 'owner-key-insert');
429
794
  log('Owner key inserted');
430
795
  }
431
796
  /**
@@ -449,18 +814,39 @@ export class ControlDatabase {
449
814
  // Generate a unique stamp ID using the peer ID for distributed uniqueness
450
815
  const peerId = this.config.libp2pNode.peerId.toString();
451
816
  const stampId = generateStampId(peerId);
452
- // Field order MUST match the schema's Strand `Authorized` verify:
817
+ // Field order MUST match the schema's Strand `AuthorizedInsert` verify:
453
818
  // Id, Type, MemberPrivateKey ('' when null), StampId.
454
- const message = buildAuthorizationMessage([strandId, type, memberPrivateKey ?? '', stampId]);
819
+ const message = buildAuthorizationMessage('CadreControl.Strand', 'add', [strandId, type, memberPrivateKey ?? '', stampId]);
455
820
  const signature = signMessage(message);
456
821
  // StampId is a real, unique column (single-use anti-replay), no longer a context value.
457
- await this.db.exec(`
458
- insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId)
459
- with context OwnerKey = ?, Signature = ?
460
- values (?, ?, ?, ?)
461
- `, [ownerKey, signature, strandId, type, memberPrivateKey ?? null, stampId]);
822
+ await this.execWrite(`
823
+ insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId)
824
+ with context OwnerKey = ?, Signature = ?
825
+ values (?, ?, ?, ?)
826
+ `, [ownerKey, signature, strandId, type, memberPrivateKey ?? null, stampId], 'strand-insert');
462
827
  log('Strand inserted: %s', strandId);
463
828
  }
829
+ /**
830
+ * Delete a strand from the control database using an owner signature.
831
+ *
832
+ * Mirrors {@link insertStrand}'s row-bound approach for the delete half: the owner
833
+ * signs the canonical `'remove'`-tagged authorization message over (Id, StampId) — the
834
+ * schema's `Strand.AuthorizedDelete` verifies this DISTINCT digest, so the insert
835
+ * approval (which never expires) can never be replayed as a removal. The delete and
836
+ * the `Revocation` tombstone retiring the row's StampId commit in ONE transaction —
837
+ * `Strand.RevocationRecorded` refuses a bare delete, and without the tombstone the
838
+ * stamp would free up and the original formation approval could re-seat the strand.
839
+ * Body shared with every other guarded delete via {@link deleteGuardedRow}.
840
+ *
841
+ * The remove digest binds only (Id, StampId) — not Type/MemberPrivateKey — so this
842
+ * works identically for open and closed strands.
843
+ *
844
+ * A no-op (no throw, no tombstone) when the row does not exist — `false` then, `true`
845
+ * when a row was actually removed.
846
+ */
847
+ deleteStrand(strandId, ownerKey, signMessage) {
848
+ return this.lockedWithRetry(() => this.deleteGuardedRow('Strand', strandId, ownerKey, signMessage), {}, 'strand-delete');
849
+ }
464
850
  /**
465
851
  * Insert a validation key into the control database using an owner signature.
466
852
  *
@@ -479,32 +865,769 @@ export class ControlDatabase {
479
865
  log('Inserting validation key: %s', key);
480
866
  const peerId = this.config.libp2pNode.peerId.toString();
481
867
  const stampId = generateStampId(peerId);
482
- // Field order MUST match the schema's ValidationKey `Authorized` verify: Key, StampId.
483
- const message = buildAuthorizationMessage([key, stampId]);
868
+ // Field order MUST match the schema's ValidationKey `AuthorizedInsert` verify: Key, StampId.
869
+ const message = buildAuthorizationMessage('CadreControl.ValidationKey', 'add', [key, stampId]);
484
870
  const signature = signMessage(message);
485
- await this.db.exec(`
486
- insert into CadreControl.ValidationKey (Key, StampId)
487
- with context OwnerKey = ?, Signature = ?
488
- values (?, ?)
489
- `, [ownerKey, signature, key, stampId]);
871
+ await this.execWrite(`
872
+ insert into CadreControl.ValidationKey (Key, StampId)
873
+ with context OwnerKey = ?, Signature = ?
874
+ values (?, ?)
875
+ `, [ownerKey, signature, key, stampId], 'validation-key-insert');
490
876
  log('Validation key inserted: %s', key);
491
877
  }
878
+ /**
879
+ * Delete a validation key from the control database using an owner signature.
880
+ *
881
+ * Mirrors {@link insertValidationKey}'s row-bound approach for the delete half: the
882
+ * owner signs the canonical `'remove'`-tagged authorization message over (Key,
883
+ * StampId) — the schema's `ValidationKey.AuthorizedDelete` verifies this DISTINCT
884
+ * digest, so the enrollment approval (which never expires) can never be replayed as a
885
+ * removal. The delete and the `Revocation` tombstone retiring the row's StampId commit
886
+ * in ONE transaction — `ValidationKey.RevocationRecorded` refuses a bare delete, and
887
+ * without the tombstone the stamp would free up and the original enrollment approval
888
+ * could re-seat the key. Body shared with every other guarded delete via
889
+ * {@link deleteGuardedRow}.
890
+ *
891
+ * A no-op (no throw, no tombstone) when the row does not exist — `false` then, `true`
892
+ * when a row was actually removed.
893
+ */
894
+ deleteValidationKey(key, ownerKey, signMessage) {
895
+ return this.lockedWithRetry(() => this.deleteGuardedRow('ValidationKey', key, ownerKey, signMessage), {}, 'validation-key-delete');
896
+ }
897
+ /**
898
+ * Owner-vouched INSERT of one `CadrePeer` membership row, notifying the membership
899
+ * listener once it has committed.
900
+ *
901
+ * Mints a fresh single-use `StampId` and signs the `'vouch'`-tagged authorization
902
+ * message over (PeerId, StampId) — see {@link buildAuthorizationMessage} — satisfying
903
+ * the schema's `CadrePeer.AuthorizedInsert`. Binding the peer id to the nonce means a
904
+ * captured insert approval cannot be replayed (live rows are blocked by the unique
905
+ * `StampId` column, removed rows by `Revocation` retirement via `CadrePeer.NotRevoked`)
906
+ * and cannot be repurposed as a delete (which signs a distinct `'remove'`-tagged digest).
907
+ *
908
+ * The owner signature does NOT cover the address columns — those are vouched only as far
909
+ * as the owner asserts them, and a peer's own `Sig` (when present) is what makes the row
910
+ * resolvable.
911
+ *
912
+ * The {@link mutateCadrePeer} wrapper lives here rather than in the caller so no
913
+ * `CadrePeer` inserter can forget it — the write itself is what lets the admitted peer's
914
+ * traffic in.
915
+ *
916
+ * Idempotent on an already-present row: two writers can legitimately race the SAME peer's
917
+ * first row — the node's own background self-publish ({@link CadreNode.registerSelf})
918
+ * against a foreground {@link SeedBootstrapService.authorizePeer} of that node's id — and
919
+ * the write lock only serializes them; the loser would hit the `CadrePeer.PeerId` UNIQUE
920
+ * constraint. The existence check runs INSIDE the locked body, so it sees the winner's
921
+ * committed row (a pre-lock check would re-open the read-then-insert window). The existing
922
+ * row — voucher, addresses, self-`Sig` — is left untouched; re-touching a live row is
923
+ * {@link reauthorizeCadrePeer}'s job.
924
+ *
925
+ * @param row - the membership row's columns; `multiaddr` is the comma-joined address list.
926
+ * @param ownerKey - public key of the vouching owner, persisted into `VouchOwner`.
927
+ * @param signMessage - ed25519-signs the raw message bytes (no pre-hash) with that owner's
928
+ * private key, returning a base64url signature.
929
+ * @returns `true` when this call performed the INSERT, `false` when the in-lock existence
930
+ * check found the row already seated. The loser needs to know: an authorize seats a row
931
+ * with a null `Sig`, so a self-publish that lost the race must fall through to a
932
+ * self-update or its record never lands.
933
+ */
934
+ async insertCadrePeer(row, ownerKey, signMessage) {
935
+ this.ensureInitialized();
936
+ // Stamp is minted from the ADMITTED peer's id, not this node's — the nonce names the
937
+ // row it vouches.
938
+ const stampId = generateStampId(row.peerId);
939
+ const signature = signMessage(buildAuthorizationMessage('CadreControl.CadrePeer', 'vouch', [row.peerId, stampId]));
940
+ return await this.mutateCadrePeer('peer-insert', async () => {
941
+ if (await this.queryStampId('CadrePeer', row.peerId) !== null) {
942
+ log('CadrePeer row already present for %s; insert skipped (already a member)', row.peerId);
943
+ return false;
944
+ }
945
+ // Bare `exec`: already inside the write lock, which is NOT re-entrant (see the NOTE
946
+ // on withWriteLock — re-entry via execWrite hangs silently and permanently).
947
+ //
948
+ // Persist the vouching (owner, signature) onto the row (VouchOwner/VouchSig) —
949
+ // identical to the context pair, which the AuthorizedInsert constraint binds — so a
950
+ // reader can later re-check the voucher against its node-local trusted-owner anchor.
951
+ await this.db.exec(`
952
+ insert into CadreControl.CadrePeer (PeerId, PublicKey, Multiaddr, UpdatedAt, Sig, StampId, VouchOwner, VouchSig)
953
+ with context OwnerKey = ?, Signature = ?
954
+ values (?, ?, ?, ?, ?, ?, ?, ?)
955
+ `, [ownerKey, signature, row.peerId, row.publicKey, row.multiaddr, row.updatedAt, row.sig, stampId, ownerKey, signature]);
956
+ return true;
957
+ });
958
+ }
959
+ /**
960
+ * Owner "re-touch" of an existing `CadrePeer` membership row: bump `UpdatedAt` and
961
+ * rewrite `VouchOwner`/`VouchSig` under the owner branch of `CadrePeer.AuthorizedUpdate`
962
+ * (a signature over the SAME `'vouch'`-tagged digest {@link insertCadrePeer} builds), so
963
+ * the row is re-emitted as a fresh, broadcasting transaction.
964
+ *
965
+ * This is the write-while-alone re-replication primitive: a membership row that committed
966
+ * local-only (its block's cluster ≤1 at insert) is pushed to the cohort once it grows, by
967
+ * re-issuing this monotonic bump. It is an UPDATE (not the original INSERT) because the row
968
+ * already exists locally; a re-INSERT would hit the `PeerId` PK. Only the freshness stamp
969
+ * and voucher change — `PublicKey` / `Multiaddr` / `Sig` are left intact — so it is safe
970
+ * over a row whose peer has not self-published (`Sig` null); the caller must skip a row
971
+ * that already carries a self-`Sig` (that row is the owning peer's to refresh, and bumping
972
+ * `UpdatedAt` without re-signing would invalidate its self-signature).
973
+ *
974
+ * Signs over the row's CURRENT `StampId` (unchanged by the re-touch), read BEFORE the lock
975
+ * is taken. An absent row returns `false` WITHOUT notifying: nothing was written, so the
976
+ * membership snapshot cannot have changed.
977
+ *
978
+ * NOTE: that stamp read is outside the lock, same as {@link deleteGuardedRow}'s. A writer
979
+ * that removes the row in between makes the `update` match nothing, and this still returns
980
+ * `true` and notifies — harmless today (the sole caller, the write-while-alone drain, only
981
+ * logs the result, and a spurious notify just re-reads the member set), and a remove-then-
982
+ * re-add in the same window fails loudly instead, since the signature binds the retired
983
+ * stamp. If a caller ever acts on `true` as proof the row was written, fold the stamp read
984
+ * into the locked body.
985
+ *
986
+ * Notifies like the insert/remove paths even though this is "only" a re-touch: it rewrites
987
+ * VouchOwner/VouchSig, which the authorized-membership predicate judges on, so it CAN
988
+ * change the member set. Keeping the rule uniform ("every CadrePeer mutator notifies")
989
+ * beats a per-method exception the next reader has to relearn.
990
+ *
991
+ * NOTE: this rebinds VouchOwner to the CALLING owner's key, and the authorized-membership
992
+ * predicate (`CadreNode.listAuthorizedMembers`) now judges rows by that column against each
993
+ * reader's node-local anchor. Benign today because the only caller — the write-while-alone
994
+ * drain — re-touches solely rows this node itself authored (`pendingPeerWrites`), so the
995
+ * voucher is rewritten to the key that already signed it. If a future path ever lets one
996
+ * owner re-touch a row a DIFFERENT owner vouched, the voucher silently flips: readers that
997
+ * anchor the original owner but not this one would drop a legitimate member. Such a path
998
+ * must re-vouch deliberately (or preserve the existing VouchOwner/VouchSig) rather than
999
+ * inherit this rebinding.
1000
+ *
1001
+ * @param peerId - the membership row to re-touch.
1002
+ * @param updatedAt - the strictly-increasing freshness stamp to write.
1003
+ * @param ownerKey - public key of the re-vouching owner, rewritten into `VouchOwner`.
1004
+ * @param signMessage - ed25519-signs the raw message bytes with that owner's private key.
1005
+ * @returns `true` when the row was re-touched, `false` when no row exists (no notify).
1006
+ */
1007
+ async reauthorizeCadrePeer(peerId, updatedAt, ownerKey, signMessage) {
1008
+ this.ensureInitialized();
1009
+ const stampId = await this.queryStampId('CadrePeer', peerId);
1010
+ if (stampId === null) {
1011
+ log('reauthorizeCadrePeer: no CadrePeer row for %s (nothing to re-touch)', peerId);
1012
+ return false;
1013
+ }
1014
+ const signature = signMessage(buildAuthorizationMessage('CadreControl.CadrePeer', 'vouch', [peerId, stampId]));
1015
+ await this.mutateCadrePeer('peer-reauthorize', async () => {
1016
+ // Bare `exec` — inside the non-re-entrant write lock (see insertCadrePeer).
1017
+ await this.db.exec(`
1018
+ update CadreControl.CadrePeer
1019
+ with context OwnerKey = ?, Signature = ?
1020
+ set UpdatedAt = ?, VouchOwner = ?, VouchSig = ?
1021
+ where PeerId = ?
1022
+ `, [ownerKey, signature, updatedAt, ownerKey, signature, peerId]);
1023
+ });
1024
+ return true;
1025
+ }
1026
+ /**
1027
+ * Owner-signed removal of one `CadrePeer` membership row, notifying the membership
1028
+ * listener once it has committed.
1029
+ *
1030
+ * Mirrors {@link deleteStrand} for the membership table: the owner signs the DISTINCT
1031
+ * `'remove'`-tagged digest over (PeerId, StampId), so the row's stored `VouchSig` can
1032
+ * never be replayed to delete it, and the `Revocation` tombstone retiring the stamp
1033
+ * lands in the SAME transaction — `CadrePeer.RevocationRecorded` refuses a bare delete,
1034
+ * and without the tombstone the never-expiring admission approval (which the removed
1035
+ * peer holds a copy of) could re-seat the row.
1036
+ *
1037
+ * The {@link mutateCadrePeer} wrapper lives here rather than in the caller so no
1038
+ * `CadrePeer` remover can forget it. It notifies whenever the body resolves, including
1039
+ * the absent-row no-op below — a caller that must not notify for an already-absent peer
1040
+ * gates on {@link queryCadrePeerStampId} first (see
1041
+ * {@link SeedBootstrapService.removePeer}).
1042
+ *
1043
+ * A no-op (no throw, no tombstone) when the row does not exist — `false` then, `true`
1044
+ * when a row was actually removed.
1045
+ */
1046
+ deleteCadrePeer(peerId, ownerKey, signMessage) {
1047
+ return this.mutateCadrePeer('peer-remove', () => this.deleteGuardedRow('CadrePeer', peerId, ownerKey, signMessage));
1048
+ }
1049
+ /**
1050
+ * Owner-signed removal of one peer's `DeviceToken` row (logout / token invalidation).
1051
+ *
1052
+ * Mirrors {@link deleteStrand}: the owner signs the DISTINCT `'remove'`-tagged digest
1053
+ * over (PeerId, StampId), so the insert approval can never be replayed to clear a token,
1054
+ * and the `Revocation` tombstone retiring the stamp lands in the SAME transaction —
1055
+ * `DeviceToken.RevocationRecorded` refuses a bare delete, and without the tombstone the
1056
+ * never-expiring insert approval (which the cleared device holds a copy of) could
1057
+ * re-seat the token.
1058
+ *
1059
+ * A no-op (no throw, no tombstone) when the row does not exist — `false` then, `true`
1060
+ * when a row was actually removed.
1061
+ */
1062
+ deleteDeviceToken(peerId, ownerKey, signMessage) {
1063
+ return this.lockedWithRetry(() => this.deleteGuardedRow('DeviceToken', peerId, ownerKey, signMessage), {}, 'device-token-delete');
1064
+ }
1065
+ /**
1066
+ * Owner-signed delete of one guarded row plus the `Revocation` tombstone retiring its
1067
+ * stamp, in ONE transaction. The single body behind EVERY guarded delete —
1068
+ * {@link deleteStrand}, {@link deleteValidationKey}, {@link deleteCadrePeer},
1069
+ * {@link deleteDeviceToken}; see any of them for the per-table security rationale. Each
1070
+ * of those is a thin named wrapper, so callers never pass a table name and this
1071
+ * generic shape stays off the public surface.
1072
+ *
1073
+ * `OwnerKey` is excluded from `table` only because no owner-key removal path exists in
1074
+ * production yet — the schema has the `'remove'` branch (`OwnerKey.Authorized`, which
1075
+ * requires a DIFFERENT owner as signer) and `control-revocation-replay.spec.ts` drives
1076
+ * it with hand-rolled SQL. Widen this and add a wrapper when that path lands; the body
1077
+ * needs no change.
1078
+ *
1079
+ * The row's CURRENT stamp is read first and signed over, so the remove digest binds to
1080
+ * this exact row instance. A no-op (no throw, no tombstone) when the row is absent —
1081
+ * reported as `false` so a caller can tell "removed" from "was never there" without a
1082
+ * second read (e.g. {@link CadreNode.unpublishStrand}'s committed-while-alone warning,
1083
+ * which must not fire for a no-op).
1084
+ *
1085
+ * NOTE: the stamp read is outside the transaction. A concurrent writer that removes
1086
+ * the row in between makes the signature bind a stamp that is no longer live; the
1087
+ * delete then matches nothing and the tombstone insert collides with the other
1088
+ * writer's on `Revocation`'s (TableName, StampId) primary key, so the transaction
1089
+ * fails rather than silently half-applying. If concurrent owner-device removals ever
1090
+ * become routine, fold the stamp read into the transaction instead.
1091
+ *
1092
+ * Deliberately NOT wrapped in {@link withWriteLock}: every public entry point already
1093
+ * holds the (non-re-entrant) lock, so taking it again here would self-deadlock.
1094
+ *
1095
+ * `table` and its {@link GUARDED_KEY_COLUMN} column are interpolated into the SQL; both
1096
+ * come from closed literal unions — no caller-supplied string reaches the statement.
1097
+ */
1098
+ async deleteGuardedRow(table, keyValue, ownerKey, signMessage) {
1099
+ this.ensureInitialized();
1100
+ const stampId = await this.queryStampId(table, keyValue);
1101
+ if (stampId === null) {
1102
+ log('delete %s: no row for %s (already absent)', table, keyValue);
1103
+ return false;
1104
+ }
1105
+ const message = buildAuthorizationMessage(`CadreControl.${table}`, 'remove', [keyValue, stampId]);
1106
+ const signature = signMessage(message);
1107
+ // The tombstone carries its OWN owner signature (`Revocation.Authorized`): retiring a
1108
+ // stamp is permanent and party-wide, so the delete's signature deliberately does not
1109
+ // cover it — the digests are domain-separated and neither replays as the other.
1110
+ const revocationSignature = signMessage(buildAuthorizationMessage('CadreControl.Revocation', 'remove', [table, keyValue, stampId]));
1111
+ await this.inTransaction(`delete ${table}`, async () => {
1112
+ await this.db.exec(`
1113
+ delete from CadreControl.${table}
1114
+ with context OwnerKey = ?, Signature = ?
1115
+ where ${GUARDED_KEY_COLUMN[table]} = ?
1116
+ `, [ownerKey, signature, keyValue]);
1117
+ // ReissuedAt named explicitly at 0 (the only value FreshTombstone accepts) rather
1118
+ // than leaning on the column default — the seat-at-zero rule is load-bearing for
1119
+ // reissueRevocations' monotonic bump, so state it at the write site.
1120
+ await this.db.exec(`
1121
+ insert into CadreControl.Revocation (TableName, RowKey, StampId, ReissuedAt)
1122
+ with context OwnerKey = ?, Signature = ?
1123
+ values (?, ?, ?, 0)
1124
+ `, [ownerKey, revocationSignature, table, keyValue, stampId]);
1125
+ });
1126
+ log('%s deleted: %s (stamp retired)', table, keyValue);
1127
+ if (this.guardedDeleteListener) {
1128
+ try {
1129
+ this.guardedDeleteListener({ tableName: table, rowKey: keyValue, stampId });
1130
+ }
1131
+ catch (error) {
1132
+ log('guarded-delete listener threw (committed delete unaffected): %o', error);
1133
+ }
1134
+ }
1135
+ return true;
1136
+ }
1137
+ /**
1138
+ * Delete one guarded row that an ALREADY-COMMITTED `Revocation` tombstone retires —
1139
+ * the local catch-up for a node that converged on a removal's tombstone while still
1140
+ * holding the removed row. That live-row-plus-tombstone state is reachable only by
1141
+ * replication merge (local writes cannot build it: `Revocation.RowIsGone` refuses a
1142
+ * tombstone while the row is live), and replication cannot carry the delete itself —
1143
+ * replaying a delete of a row that is already gone locally is a no-op — so without
1144
+ * this the stale row is permanent garbage on every node that held it at revocation
1145
+ * time. Authorized by the REAP branch of the table's `AuthorizedDelete`: the
1146
+ * tombstone is itself owner-signed (`Revocation.Authorized`), so no owner key is
1147
+ * needed HERE — any node holding row + tombstone may reap, drones included.
1148
+ *
1149
+ * Returns whether a row was removed: `false` without writing when the row is absent
1150
+ * locally (the common case on most nodes), or when its live stamp is not `stampId`
1151
+ * (a fresh incarnation the owner re-seated, which the old tombstone must not touch).
1152
+ *
1153
+ * The delete's WHERE clause binds `StampId` too — required, not belt-and-braces: an
1154
+ * owner may re-seat the row (fresh stamp) between the stamp read below and the
1155
+ * statement, and without the clause the reap would delete the owner's brand-new row.
1156
+ * With it the statement matches nothing and the reap is a silent no-op (the schema
1157
+ * branch would also refuse that delete, but as a constraint error thrown into a
1158
+ * background sweep — matched-nothing is the better failure mode). Like
1159
+ * {@link reauthorizeCadrePeer}, a race lost AFTER the guard still returns `true`;
1160
+ * harmless — nothing was deleted, and the next sweep re-reads.
1161
+ *
1162
+ * Writes NOTHING to `Revocation`: `RevocationRecorded` is satisfied by the same
1163
+ * committed tombstone that authorizes the reap, so a reap files no second tombstone.
1164
+ * It therefore does NOT fire the guarded-delete listener either — that seam exists to
1165
+ * queue a tombstone for re-issue when it committed while alone, and a reap has no
1166
+ * tombstone of its own to re-issue. A reap that commits while alone needs no
1167
+ * re-replication at all: every OTHER node either already lacks the row, or holds the
1168
+ * same committed tombstone and reaps its own copy.
1169
+ *
1170
+ * NOTE: the reap delete is keyed on the primary key; the `StampId` predicate is applied
1171
+ * where the statement runs. If a reap on one node ever has to be reconciled against an
1172
+ * owner re-seat of the same key that landed on another node, whether the delete or the
1173
+ * re-seat wins is decided by the collection's merge order, not by this clause — the same
1174
+ * ordering question the owner-signed delete path already carries
1175
+ * (`tickets/blocked/forked-control-collection-sync-livelocks.md`). Only matters once a
1176
+ * sweep drives this automatically on connected nodes; if that reconcile ever drops a
1177
+ * freshly re-seated row, this is the site.
1178
+ *
1179
+ * `table` and its {@link GUARDED_KEY_COLUMN} column come from closed literal unions —
1180
+ * no caller-supplied string reaches the statement (same injection-surface discipline
1181
+ * as {@link deleteGuardedRow}). The `with context` clause is PRESENT and bound to
1182
+ * nulls, never omitted: Quereus cannot resolve `context.OwnerKey` at plan time when
1183
+ * the clause is absent while a constraint references `context.*` (nulls through a
1184
+ * present clause are the shape {@link redeemInvitation}'s consent-branch insert ships).
1185
+ *
1186
+ * Driven automatically by {@link reapRevokedRows}, which the periodic control-cohort
1187
+ * reconcile pass runs while the node has at least one control connection.
1188
+ */
1189
+ async reapRevokedRow(table, rowKey, stampId) {
1190
+ this.ensureInitialized();
1191
+ const liveStamp = await this.queryStampId(table, rowKey);
1192
+ if (liveStamp === null) {
1193
+ log('reap %s: no row for %s (nothing to reap)', table, rowKey);
1194
+ return false;
1195
+ }
1196
+ if (liveStamp !== stampId) {
1197
+ log('reap %s: %s holds a fresh incarnation (live stamp differs from the tombstoned one); leaving it', table, rowKey);
1198
+ return false;
1199
+ }
1200
+ const sql = `
1201
+ delete from CadreControl.${table}
1202
+ with context OwnerKey = null, Signature = null
1203
+ where ${GUARDED_KEY_COLUMN[table]} = ? and StampId = ?
1204
+ `;
1205
+ if (table === 'CadrePeer') {
1206
+ // Through mutateCadrePeer so the "EVERY CadrePeer writer goes through here"
1207
+ // invariant keeps holding with no new documented exception. The membership
1208
+ // snapshot cannot actually change — queryCadrePeers already dropped this row via
1209
+ // the retired-stamp gate — so the notify is a redundant refresh. Bare exec:
1210
+ // already inside the non-re-entrant write lock (see insertCadrePeer).
1211
+ //
1212
+ // NOTE: reapRevokedRows calls this in a loop, so a sweep that clears K CadrePeer
1213
+ // rows fires K redundant gate refreshes (two table scans each), awaited serially.
1214
+ // Harmless today — K is the backlog of unreaped rows, which a party burns down to
1215
+ // 0 on its first connected tick and holds there. If a party ever reaps many rows
1216
+ // in one pass, hoist the notify to one refresh after the sweep instead.
1217
+ await this.mutateCadrePeer('peer-reap', () => this.db.exec(sql, [rowKey, stampId]));
1218
+ }
1219
+ else {
1220
+ await this.execWrite(sql, [rowKey, stampId], `reap-${table}`);
1221
+ }
1222
+ // A row leaving the control plane without an owner signature at the write site
1223
+ // should be visible in a log.
1224
+ log('REAPED %s row %s (stamp %s): committed tombstone authorized local removal, no owner signature', table, rowKey, stampId);
1225
+ return true;
1226
+ }
1227
+ /**
1228
+ * Sweep every locally-held `Revocation` tombstone and {@link reapRevokedRow} the guarded
1229
+ * row each one retires, returning how many rows were actually removed.
1230
+ *
1231
+ * This is the enumeration half of the reap: replication carries the tombstone but cannot
1232
+ * carry the delete (replaying a delete of an already-absent row is a no-op), so a node
1233
+ * that converged on a removal while holding the removed row keeps that row as inert
1234
+ * garbage until something walks the tombstones and drops it. `CadreNode`'s periodic
1235
+ * control-cohort reconcile pass is that something, gated on the node having at least one
1236
+ * control connection (a reap is a write, and a write committed alone is local-only).
1237
+ *
1238
+ * Enumerated UNLOCKED, deleted per-row LOCKED: {@link reapRevokedRow} takes the write
1239
+ * lock itself (via {@link mutateCadrePeer} / {@link execWrite}) and the lock is not
1240
+ * re-entrant, so this loop must not hold it. Same structure as
1241
+ * `CadreNode.reissueAuthoredMembershipRows`.
1242
+ *
1243
+ * Two rows are deliberately left alone:
1244
+ *
1245
+ * - **Tables outside {@link REAPABLE_TABLES}** (`Strand`, `OwnerKey`) — no reap branch
1246
+ * exists on their `AuthorizedDelete`, so the delete would throw rather than no-op.
1247
+ * - **This node's OWN `CadrePeer` / `DeviceToken` row** (`selfPeerId`). Reaping it would
1248
+ * fight this node's own re-registration: `registerSelf` / `retouchSelfDeviceToken` run
1249
+ * on the heartbeat and the growth drain, and after a self-reap their insert-if-absent
1250
+ * guard takes the INSERT path — which needs an owner signature `NotRevoked` refuses for
1251
+ * a retired stamp, and which a revoked drone cannot produce at all. The trade is
1252
+ * recurring failure noise every cycle against removing the one stale row nobody else
1253
+ * reads from this node (every other node already filters it by tombstone). A revoked
1254
+ * node therefore keeps its own copy of its own row; "a node that learns it has been
1255
+ * revoked should shut itself down" is a distinct behaviour and its own ticket.
1256
+ * `ValidationKey` has no self notion and is not special-cased.
1257
+ *
1258
+ * A per-row failure is logged and skipped rather than aborting the sweep: an abort would
1259
+ * starve every tombstone after the failing one on every subsequent pass, and each row is
1260
+ * independent. Returns the count so far if the database is closed mid-sweep.
1261
+ *
1262
+ * `selfPeerId` is a parameter rather than read from the injected libp2p node so the
1263
+ * skip-self rule is exercisable without one.
1264
+ *
1265
+ * NOTE: cost is O(tombstones), not O(live rows) — one {@link queryRevocations} scan plus
1266
+ * one {@link queryStampId} point lookup per tombstone, and the empty-table early return
1267
+ * makes the common case (a party that has never revoked anyone) a single scan per pass.
1268
+ * But `Revocation` is append-only and unbounded, so this is O(all tombstones ever) point
1269
+ * lookups on every reconcile tick. Fine while revocations stay rare for a cadre-sized
1270
+ * party; if the table ever grows, persist a node-local high-water mark of what has
1271
+ * already been reaped instead of re-scanning everything (the same bound the sweep in
1272
+ * `CadreNode.drainPendingRevocations` wants).
1273
+ */
1274
+ async reapRevokedRows(selfPeerId) {
1275
+ this.ensureInitialized();
1276
+ const held = await this.queryRevocations();
1277
+ if (held.length === 0) {
1278
+ return 0;
1279
+ }
1280
+ let reaped = 0;
1281
+ for (const tombstone of held) {
1282
+ // Teardown guard, per row: close() can land mid-sweep (the reconcile pass that
1283
+ // drives this outlives no node, but it does not hold the node's lifecycle either).
1284
+ if (!this.initialized || !this.db) {
1285
+ return reaped;
1286
+ }
1287
+ if (!isReapableTable(tombstone.tableName)) {
1288
+ continue;
1289
+ }
1290
+ if (tombstone.rowKey === selfPeerId
1291
+ && (tombstone.tableName === 'CadrePeer' || tombstone.tableName === 'DeviceToken')) {
1292
+ continue;
1293
+ }
1294
+ try {
1295
+ if (await this.reapRevokedRow(tombstone.tableName, tombstone.rowKey, tombstone.stampId)) {
1296
+ reaped++;
1297
+ }
1298
+ }
1299
+ catch (error) {
1300
+ log('reap sweep: %s row %s (stamp %s) failed (continuing): %o', tombstone.tableName, tombstone.rowKey, tombstone.stampId, error);
1301
+ }
1302
+ }
1303
+ return reaped;
1304
+ }
1305
+ /**
1306
+ * Owner-signed re-issue of a batch of `Revocation` tombstones: bump each row's
1307
+ * `ReissuedAt` to `reissuedAt` in ONE transaction. Re-writing the row is what makes
1308
+ * the storage layer re-broadcast it — a tombstone that committed while the node was
1309
+ * alone is local-only, and unlike an insert a delete cannot be replayed (the guarded
1310
+ * row is already gone locally), so the tombstone is the half of a removal that can
1311
+ * still carry it to the rest of the party.
1312
+ *
1313
+ * One transaction, not one per row: a sweep may cover every tombstone in the table,
1314
+ * and each separate commit is a separate round of network work. One owner signature
1315
+ * per row: `AuthorizedReissue`'s digest binds (TableName, RowKey, StampId,
1316
+ * ReissuedAt). `reissuedAt` must be STRICTLY above every affected row's current
1317
+ * value (`ReissueOnly`); callers derive it as `Math.max(Date.now(), max(existing) + 1)`,
1318
+ * mirroring {@link CadreNode.reissuePeerAuthorize}'s monotonic bump. Re-issuing a
1319
+ * tombstone whose guarded row was never present locally is the normal case (a node
1320
+ * that converged on the tombstone but never held the row) — a re-issue files no
1321
+ * delete, so `RowIsGone` / `RevocationRecorded` are not involved.
1322
+ *
1323
+ * A constraint failure on any row rolls the WHOLE batch back and propagates —
1324
+ * {@link lockedWithRetry}'s classifier only re-presents transient cluster failures,
1325
+ * so a real refusal (non-owner signer, or a stale counter when two owner devices
1326
+ * sweep concurrently and the loser's `ReissueOnly` CHECK fails) surfaces to the
1327
+ * caller, which retries with a fresh counter on its next sweep. A row whose
1328
+ * `StampId` matches nothing locally is a silent no-op statement; callers enumerate
1329
+ * via {@link queryRevocations} first, so they only name stamps they hold.
1330
+ *
1331
+ * Returns how many UPDATE statements ran (`rows.length` on success).
1332
+ */
1333
+ reissueRevocations(rows, reissuedAt, ownerKey, signMessage) {
1334
+ this.ensureInitialized();
1335
+ // Signatures minted OUTSIDE the locked body: a retried attempt must re-present the
1336
+ // exact signed message, never re-mint (the contract on withWriteLock). ReissuedAt
1337
+ // rides in the digest as a string — SQL-side it is cast(new.ReissuedAt as text),
1338
+ // and every digest field is TEXT on both sides (see buildAuthorizationMessage).
1339
+ const signed = rows.map((row) => ({
1340
+ row,
1341
+ signature: signMessage(buildAuthorizationMessage('CadreControl.Revocation', 'reissue', [row.tableName, row.rowKey, row.stampId, String(reissuedAt)])),
1342
+ }));
1343
+ return this.lockedWithRetry(async () => {
1344
+ // Counter lives INSIDE the locked body so a lockedWithRetry re-run (which
1345
+ // re-executes the whole body) starts from zero instead of double-counting.
1346
+ let executed = 0;
1347
+ await this.inTransaction('reissue revocations', async () => {
1348
+ for (const { row, signature } of signed) {
1349
+ // Keyed on StampId ALONE, deliberately. Equality on every column of the
1350
+ // composite primary key (TableName, StampId) makes the optimystic vtab claim
1351
+ // the predicate as a point lookup with no engine-side re-check, and that
1352
+ // descent has been observed returning zero rows for a row that provably
1353
+ // exists (tickets/backlog/debt-composite-pk-point-lookup-unreliable-untracked).
1354
+ // A re-issue that silently matched nothing would report success and
1355
+ // replicate nothing — the exact failure this path exists to remove. StampId
1356
+ // alone is a non-leading subset of the key, which the module declines, so
1357
+ // the read is a scan the engine filters; a StampId is 128 bits of CSPRNG
1358
+ // output minted per row incarnation (generateStampId), so it identifies one
1359
+ // row across all TableName values. Do NOT "fix" this by adding TableName to
1360
+ // the where clause.
1361
+ await this.db.exec(`
1362
+ update CadreControl.Revocation
1363
+ with context OwnerKey = ?, Signature = ?
1364
+ set ReissuedAt = ?
1365
+ where StampId = ?
1366
+ `, [ownerKey, signature, reissuedAt, row.stampId]);
1367
+ executed++;
1368
+ }
1369
+ });
1370
+ log('Reissued %d revocation tombstone(s) at %d', executed, reissuedAt);
1371
+ return executed;
1372
+ }, {}, 'revocation-reissue');
1373
+ }
1374
+ /**
1375
+ * Run `body` between `beginTransaction` and `commit`, rolling back on failure.
1376
+ *
1377
+ * A failed `commit()` has already torn the transaction down, so the `rollback()` in
1378
+ * the failure path would itself throw "No transaction active" and mask the real
1379
+ * cause — that secondary throw is logged and swallowed, and the original error always
1380
+ * propagates.
1381
+ *
1382
+ * It opens a transaction unconditionally (Quereus hard-throws on a nested
1383
+ * `beginTransaction`), so a caller already inside one must not call it. A `CadrePeer`
1384
+ * write nests this INSIDE {@link mutateCadrePeer}, never the reverse — see
1385
+ * {@link assertCommitBoundary}.
1386
+ *
1387
+ * Private: every multi-statement control write lives in this class (the owner-signed
1388
+ * delete/tombstone pairs all go through {@link deleteGuardedRow}), so needing to widen
1389
+ * this is a sign the writer that wants it belongs in here too.
1390
+ *
1391
+ * Deliberately NOT wrapped in {@link withWriteLock}: callers already hold the
1392
+ * (non-re-entrant) lock, so taking it again here would self-deadlock.
1393
+ *
1394
+ * @param label - What the transaction was doing, for the rollback log line.
1395
+ */
1396
+ async inTransaction(label, body) {
1397
+ this.ensureInitialized();
1398
+ await this.db.beginTransaction();
1399
+ try {
1400
+ await body();
1401
+ await this.db.commit();
1402
+ }
1403
+ catch (error) {
1404
+ try {
1405
+ await this.db.rollback();
1406
+ }
1407
+ catch (rollbackError) {
1408
+ log('Rollback after %s failure was a no-op: %s', label, rollbackError);
1409
+ }
1410
+ throw error;
1411
+ }
1412
+ }
1413
+ /**
1414
+ * Wire (or clear, with null) the single listener notified after every committed
1415
+ * `CadreControl.CadrePeer` row write.
1416
+ *
1417
+ * At most one listener: a ControlDatabase instance belongs to exactly one
1418
+ * {@link CadreNode}, which wires this in `start()` and clears it on teardown. A
1419
+ * second call replaces the first rather than fanning out, so a stale node can
1420
+ * never keep receiving notifications for a database it no longer owns.
1421
+ */
1422
+ setMembershipChangeListener(listener) {
1423
+ this.membershipListener = listener;
1424
+ }
1425
+ /**
1426
+ * Wire (or clear, with null) the single listener notified after every committed
1427
+ * guarded-table delete. Same ownership contract as
1428
+ * {@link setMembershipChangeListener}: one CadreNode, one listener, a second
1429
+ * call replaces the first.
1430
+ */
1431
+ setGuardedDeleteListener(listener) {
1432
+ this.guardedDeleteListener = listener;
1433
+ }
1434
+ /**
1435
+ * Run a `CadrePeer` row mutation and notify the membership listener once it has
1436
+ * COMMITTED.
1437
+ *
1438
+ * EVERY `CadrePeer` writer goes through here, with one documented exception
1439
+ * ({@link updateSelfPeerRecord}, which cannot change the snapshot) — that is what makes
1440
+ * the party-membership snapshot refresh automatic rather than a caller obligation. A
1441
+ * writer necessarily holds the target node's ControlDatabase (the `SeedBootstrapService`'s
1442
+ * event callbacks are NOT a viable seam: the temp services `CadreNode.applySeed`/
1443
+ * `dialInvite` build, and services constructed outside `CadreNode` entirely, never get
1444
+ * callbacks wired), so this is the one point on the write path that cannot be bypassed.
1445
+ *
1446
+ * `body` owns its own transaction if it needs one and must COMMIT before returning, so
1447
+ * the notification never makes the listener read uncommitted state. A throwing `body`
1448
+ * propagates and does NOT notify: nothing changed.
1449
+ *
1450
+ * That contract is enforced, not merely documented — see {@link assertCommitBoundary}.
1451
+ *
1452
+ * Runs under {@link withWriteLock}, notification included: with the lock held through
1453
+ * the notify, no other local writer can have a transaction open while the listener
1454
+ * reads, so the listener always sees exactly the committed state it was told about.
1455
+ * The listener itself only READS (it re-materializes the membership snapshot) — a
1456
+ * listener that wrote through a locked method would deadlock.
1457
+ *
1458
+ * Reaches the lock through {@link lockedWithRetry}, so after a transient cluster
1459
+ * failure the WHOLE locked body re-runs — `body` must be atomic and re-runnable (the
1460
+ * contract on {@link withWriteLock}). The notify sits after a successful `body` inside
1461
+ * the same attempt and a throwing attempt never reaches it, so a retried mutation
1462
+ * still notifies exactly once: on the attempt that commits, and not at all on
1463
+ * exhaustion.
1464
+ *
1465
+ * @param reason - Label for the log line only (e.g. `'peer-insert'`).
1466
+ */
1467
+ async mutateCadrePeer(reason, body) {
1468
+ this.ensureInitialized();
1469
+ return this.lockedWithRetry(async () => {
1470
+ this.assertCommitBoundary(reason, 'on entry to');
1471
+ const result = await body();
1472
+ this.assertCommitBoundary(reason, 'on return from');
1473
+ await this.notifyMembershipChanged(reason);
1474
+ return result;
1475
+ }, {}, reason);
1476
+ }
1477
+ /**
1478
+ * Run one local write under the database-wide write lock, serializing it against
1479
+ * every other local writer on this ControlDatabase.
1480
+ *
1481
+ * Quereus tracks transaction state per `Database` (`getAutocommit()`), and a write
1482
+ * statement's implicit transaction stays open across the awaits inside `exec`. Two
1483
+ * local writers interleaving mid-statement therefore either trip
1484
+ * {@link assertCommitBoundary} (any `CadrePeer` path) or silently join each other's
1485
+ * open transaction — a torn commit if either side rolls back. This lock closes that
1486
+ * class: every public write method of this class runs its statement(s) under it, and
1487
+ * `SeedBootstrapService` wraps its direct `db.exec` writes in it too. The race is
1488
+ * real, not theoretical: the background self-record publish on a control connection
1489
+ * opening (`CadreNode.drainPendingControlReplication` → `registerSelf`) collided with
1490
+ * a foreground `authorizePeer` — both `CadrePeer` inserts — and tripped the boundary
1491
+ * assert.
1492
+ *
1493
+ * Reads are deliberately not locked: they take no transaction of their own, and
1494
+ * serializing them here would let a listener that reads during a notify (see
1495
+ * {@link mutateCadrePeer}) deadlock. The lock is NOT re-entrant — a locked writer's
1496
+ * body must never call another locked public method; the private bodies it composes
1497
+ * ({@link deleteGuardedRow}, {@link inTransaction}) stay bare for exactly that reason.
1498
+ *
1499
+ * NOTE: re-entry fails SILENTLY and PERMANENTLY. A locked body that calls another
1500
+ * locked public method (including a {@link mutateCadrePeer} body, which runs an
1501
+ * arbitrary caller-supplied callback) queues behind its own tail and never resolves —
1502
+ * no error, and it strands the whole write queue, not only that call. There is no
1503
+ * cheap fail-fast: a "held" flag cannot tell re-entry from a legitimately queued
1504
+ * concurrent writer. Compose bare private bodies instead.
1505
+ *
1506
+ * A locked body must also be ATOMIC and RE-RUNNABLE: the public write surface reaches
1507
+ * this lock through {@link lockedWithRetry}, which re-runs the WHOLE body after a
1508
+ * transient cluster failure. Atomic holds for every body today — one statement, or
1509
+ * wrapped in {@link inTransaction} — so a failed attempt leaves nothing half-applied;
1510
+ * re-runnable means a retry re-runs the body's reads too, which is what makes it safe.
1511
+ * Signatures and stamp ids minted OUTSIDE the body are deliberately NOT re-minted per
1512
+ * attempt: a retry re-presents the exact signed message the first attempt presented.
1513
+ */
1514
+ async withWriteLock(fn) {
1515
+ // Chain behind the current tail regardless of how it settled, then park a
1516
+ // swallowed copy as the new tail so one failed write never poisons the queue.
1517
+ const run = this.writeQueue.then(fn, fn);
1518
+ this.writeQueue = run.then(() => undefined, () => undefined);
1519
+ return run;
1520
+ }
1521
+ /**
1522
+ * {@link withWriteLock} plus the bounded transient-failure retry
1523
+ * ({@link retryControlWrite}) — the wrapper the whole public write surface goes
1524
+ * through, so a control write that failed because the cluster cohort did not answer is
1525
+ * re-presented a moment later instead of surfacing to ~19 callers that are not
1526
+ * uniformly written to retry.
1527
+ *
1528
+ * The retry wraps the LOCK, it does not live inside it: each attempt takes and
1529
+ * releases the lock, and the backoff sleeps with NO lock held, so a write parked in
1530
+ * backoff never stalls the other local writers queued behind it. Safe because every
1531
+ * locked body is atomic and re-runnable (the contract on {@link withWriteLock}).
1532
+ *
1533
+ * Also carries {@link loadSchema}'s distributed DDL, the one caller that runs BEFORE
1534
+ * `initialized` is set — so it reaches this method directly rather than through
1535
+ * {@link execWrite}, whose {@link ensureInitialized} would reject it. The write queue is
1536
+ * necessarily empty at that point, so the lock is uncontended there; it is taken anyway
1537
+ * rather than carving out a lockless path. It is also the ONLY caller that passes a
1538
+ * `policy` — everything else takes the default, whose classifier and attempt count are
1539
+ * unchanged by that call site existing.
1540
+ *
1541
+ * `label` names the operation in this loop's debug lines and nothing else
1542
+ * ({@link ControlWriteRetryOptions.label}). Every call site in this class supplies one:
1543
+ * several writes retry CONCURRENTLY in a real party, so an unlabelled line cannot be
1544
+ * attributed to a write — keep new call sites labelled.
1545
+ */
1546
+ lockedWithRetry(fn, policy = {}, label) {
1547
+ // The label lands LAST so a call site's own name survives both the policy and the
1548
+ // spec-injected pacing; it changes log attribution only, never behaviour.
1549
+ return retryControlWrite(() => this.withWriteLock(fn), { ...policy, ...this.controlWriteRetryPacing, ...(label !== undefined ? { label } : {}) });
1550
+ }
1551
+ /**
1552
+ * One-statement local write, serialized by {@link withWriteLock} and retried on a
1553
+ * transient cluster failure via {@link lockedWithRetry}. Prefer this over a bare
1554
+ * `getDatabase().exec` so a new writer cannot forget the lock — the hazard
1555
+ * {@link assertCommitBoundary} can only catch after the fact.
1556
+ *
1557
+ * Public so `SeedBootstrapService`'s direct `CadrePeer`/`DeviceToken` SQL writes go
1558
+ * through the same seam. Must NOT be called from inside an already-locked body (the
1559
+ * lock is not re-entrant); such a caller uses a bare `getDatabase().exec` instead.
1560
+ *
1561
+ * `label` names the write in the retry loop's debug lines (see {@link lockedWithRetry});
1562
+ * optional only because it is log-only, but every caller in this repo passes one.
1563
+ */
1564
+ execWrite(sql, params, label) {
1565
+ this.ensureInitialized();
1566
+ return this.lockedWithRetry(() => this.db.exec(sql, params), {}, label);
1567
+ }
1568
+ /**
1569
+ * Throw unless the database sits at a committed boundary — `getAutocommit()` is false
1570
+ * exactly while a transaction is open.
1571
+ *
1572
+ * Both ends of a {@link mutateCadrePeer} body are checked, because either open
1573
+ * transaction would notify a listener that then reads PRE-commit state and materializes
1574
+ * a membership snapshot silently missing the write it was told about:
1575
+ *
1576
+ * - open on entry: the wrapper sits INSIDE an enclosing transaction and must be moved
1577
+ * out to enclose that outer commit.
1578
+ * - open on return: the body opened a transaction and never committed it.
1579
+ *
1580
+ * Neither is reachable from today's callers, so this is a fail-fast guard on a future
1581
+ * mistake rather than a live code path.
1582
+ *
1583
+ * NOTE: `getAutocommit()` reports the whole `Database`, not this call. Local writers
1584
+ * serialize through {@link withWriteLock}, so with every writer locked this can only
1585
+ * trip on the two misuses above. A writer that bypasses the lock (a new method that
1586
+ * forgets it, or raw `getDatabase().exec` outside a locked wrapper) re-opens the
1587
+ * concurrent-writer window, and this assert is what catches it — it throws instead of
1588
+ * silently joining the other write's transaction.
1589
+ */
1590
+ assertCommitBoundary(reason, where) {
1591
+ if (this.db.getAutocommit()) {
1592
+ return;
1593
+ }
1594
+ throw new Error(`mutateCadrePeer(${reason}): a transaction is open ${where} the mutation body — a ` +
1595
+ 'CadrePeer write must commit before the membership listener runs; move the ' +
1596
+ 'mutateCadrePeer wrapper out to enclose the commit');
1597
+ }
1598
+ /**
1599
+ * Best-effort notify. A listener that throws is logged and swallowed: the write has
1600
+ * already committed, and it must never be reported as failed because a downstream
1601
+ * snapshot refresh did. A null listener (no `CadreNode` attached — e.g. the service
1602
+ * unit tests drive a bare control DB) is a silent no-op.
1603
+ */
1604
+ async notifyMembershipChanged(reason) {
1605
+ if (!this.membershipListener) {
1606
+ return;
1607
+ }
1608
+ try {
1609
+ await this.membershipListener(reason);
1610
+ }
1611
+ catch (error) {
1612
+ log('Membership change listener failed (%s): %s', reason, error);
1613
+ }
1614
+ }
492
1615
  /**
493
1616
  * Insert an owner-signed `FormationInvite` (open invitation token).
494
1617
  *
495
1618
  * The invite is the on-network record that later authorizes an
496
1619
  * owner-signature-FREE `Strand` creation: an invited cadre peer redeems it
497
1620
  * by inserting a matching `FormationUsage` row (see {@link redeemInvitation}),
498
- * which satisfies the consent branch of `Strand.Authorized`.
1621
+ * which satisfies the consent branch of `Strand.AuthorizedInsert`.
499
1622
  *
500
1623
  * Like {@link insertStrand}/{@link insertValidationKey}, the owner signs the
501
1624
  * canonical row-bound authorization message (see {@link buildAuthorizationMessage})
502
1625
  * over (Token, sAppId, ExpiresAt, TotalUses, ValidationUrl, StrandId, StampId) — NOT a
503
1626
  * bare stamp — so the signature is bound to this invite's contents and cannot be
504
1627
  * transplanted onto an attacker-chosen row. The StampId is persisted as a unique
505
- * column for single-use anti-replay. `FormationInvite.AuthorizedAddOrRemove` gates
506
- * both insert and delete; its `coalesce(new, old)` verify binds the NEW row on
507
- * insert and the OLD row on delete.
1628
+ * column for single-use anti-replay. `FormationInvite.AuthorizedInsert` gates insert
1629
+ * over an `'add'`-tagged digest; deletes verify a DISTINCT `'remove'`-tagged digest
1630
+ * (`AuthorizedDelete`), so this insert approval can never be replayed as a revocation.
508
1631
  *
509
1632
  * `StrandId` binds the invite to a pre-existing host strand (provision-then-record):
510
1633
  * when set, a responder redeeming this token records a `FormationUsage` against that
@@ -545,19 +1668,19 @@ export class ControlDatabase {
545
1668
  const totalUsesField = options.totalUses == null ? '' : String(options.totalUses);
546
1669
  const validationUrlField = options.validationUrl ?? '';
547
1670
  const strandIdField = options.strandId ?? '';
548
- // Field order MUST match the schema's FormationInvite `AuthorizedAddOrRemove` verify:
1671
+ // Field order MUST match the schema's FormationInvite `AuthorizedInsert` verify:
549
1672
  // Token, sAppId, ExpiresAt, TotalUses, ValidationUrl, StrandId, StampId.
550
- const message = buildAuthorizationMessage([
1673
+ const message = buildAuthorizationMessage('CadreControl.FormationInvite', 'add', [
551
1674
  token, sAppId, expiresAtField, totalUsesField, validationUrlField, strandIdField, stampId,
552
1675
  ]);
553
1676
  const signature = signMessage(message);
554
1677
  // Persist the canonical ExpiresAt string (datetime parse is idempotent on it) so the
555
1678
  // signed source-of-truth and the stored value are produced once. StampId is a real,
556
1679
  // unique column (single-use anti-replay), no longer a context value.
557
- await this.db.exec(`
558
- insert into CadreControl.FormationInvite (Token, sAppId, ExpiresAt, TotalUses, ValidationUrl, StrandId, StampId)
559
- with context OwnerKey = ?, Signature = ?
560
- values (?, ?, ?, ?, ?, ?, ?)
1680
+ await this.execWrite(`
1681
+ insert into CadreControl.FormationInvite (Token, sAppId, ExpiresAt, TotalUses, ValidationUrl, StrandId, StampId)
1682
+ with context OwnerKey = ?, Signature = ?
1683
+ values (?, ?, ?, ?, ?, ?, ?)
561
1684
  `, [
562
1685
  ownerKey, signature,
563
1686
  token, sAppId,
@@ -566,7 +1689,7 @@ export class ControlDatabase {
566
1689
  options.validationUrl ?? null,
567
1690
  options.strandId ?? null,
568
1691
  stampId,
569
- ]);
1692
+ ], 'formation-invite-insert');
570
1693
  log('Formation invite inserted: %s', token);
571
1694
  }
572
1695
  /**
@@ -574,78 +1697,163 @@ export class ControlDatabase {
574
1697
  * `FormationUsage` row **atomically, in one transaction**.
575
1698
  *
576
1699
  * The two CHECK constraints are mutually circular under immediate evaluation:
577
- * `Strand.Authorized`'s consent branch requires the `FormationUsage` row, while
578
- * `FormationUsage.StrandExists` requires the `Strand` row. Both CHECKs contain
1700
+ * `Strand.AuthorizedInsert`'s consent branch requires a `FormationUsage` row naming
1701
+ * this strand's `(Id, StampId)`, while `FormationUsage.StrandExists` requires a
1702
+ * `Strand` row matching that same pair — the ONE freshly-minted `strandStampId` below
1703
+ * satisfies both, which is what binds the consent record to this specific strand ROW
1704
+ * (so a later owner-signed, tombstoned removal cannot be undone by re-inserting the
1705
+ * id with a fresh stamp). Both CHECKs contain
579
1706
  * subqueries, so Quereus auto-defers them to transaction commit — wrapping both
580
1707
  * inserts in a single explicit `begin … commit` lets both deferred CHECKs see
581
1708
  * both rows at commit. The strand is authorised WITHOUT an owner signature
582
- * (the `FormationUsage` branch of `Strand.Authorized`) but still gets a fresh,
1709
+ * (the `FormationUsage` branch of `Strand.AuthorizedInsert`) but still gets a fresh,
583
1710
  * unique `StampId` column to satisfy the not-null/unique anti-replay column.
584
1711
  *
585
- * `UseNumber` is computed as `max(UseNumber)+1` for the token (the `Monotonic`
586
- * constraint); callers redeeming concurrently against the same token must
587
- * serialise, since the next use number is read before the insert.
1712
+ * The seated strand is always open (`'o'`) and keyless — the consent branch of
1713
+ * `Strand.AuthorizedInsert` accepts nothing else, the invite must be UNBOUND
1714
+ * (`FormationInvite.StrandId` null; a bound invite's host strand is owner-provisioned
1715
+ * and only ever record-only, see {@link recordFormationUsage}), and a given strand id
1716
+ * may be consent-seated once, EVER: after an owner-signed removal, re-joining that id
1717
+ * takes an owner re-seat ({@link insertStrand}) plus a bound invite, never another
1718
+ * redemption.
1719
+ *
1720
+ * The usage row is keyed by the joiner's own nonce (`UsageStampId` is the primary key), so
1721
+ * concurrent redemptions of one token never contend for a shared row key — there is no
1722
+ * lost race to retry and no second trip through the approval hook. The invite's seat
1723
+ * budget is checked by COUNT ({@link assertSeatRemains}) inside the write lock, ahead of
1724
+ * the write, so a spent invite is refused by name (`InvitationExhaustedError`) instead of
1725
+ * as the schema cap clause's generic `Authorized` CHECK failure.
588
1726
  */
589
1727
  async redeemInvitation(params) {
590
1728
  this.ensureInitialized();
591
- const { token, strandId, type = 'o', memberPrivateKey, disclosure = '', peerId, peerSignature, nowMs, validationKey, validationSignature, } = params;
1729
+ const { token, strandId, disclosure = '', peerKey, peerSignature, usageStampId, nowMs, validationKey, validationSignature, signal, totalUses, } = params;
592
1730
  log('Redeeming invitation %s -> strand %s', token, strandId);
593
1731
  const localPeerId = this.config.libp2pNode.peerId.toString();
1732
+ // A failed attempt rolls its whole transaction back, so nothing was ever persisted under
1733
+ // this stamp, and it is deliberately not part of the approver's signed digest.
594
1734
  const strandStampId = generateStampId(localPeerId);
595
- const useNumber = await this.nextUseNumber(token);
596
- await this.db.beginTransaction();
597
- try {
598
- // 1. Strand row — authorised by the FormationUsage branch (no owner sig),
599
- // still carrying a fresh unique StampId for the anti-replay column.
600
- await this.db.exec(`
601
- insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId)
602
- with context OwnerKey = null, Signature = null
603
- values (?, ?, ?, ?)
604
- `, [strandId, type, memberPrivateKey ?? null, strandStampId]);
605
- // 2. FormationUsage row — authorised by the matching FormationInvite.
606
- await this.execFormationUsageInsert({
607
- token, useNumber, disclosure, strandId,
608
- peerId: peerId ?? localPeerId, peerSignature: peerSignature ?? null, nowMs: nowMs ?? Date.now(),
609
- validationKey: validationKey ?? null, validationSignature: validationSignature ?? null,
610
- });
611
- await this.db.commit();
612
- log('Redeemed invitation %s -> strand %s (use #%d)', token, strandId, useNumber);
613
- }
614
- catch (error) {
615
- // A failed commit() already tears down the transaction, so rollback() would
616
- // throw "No transaction active" and mask the real cause — swallow only that.
617
- try {
618
- await this.db.rollback();
1735
+ // Ordering inside the locked body is contractual: the abort check, then the seat check,
1736
+ // then the write — FormationAbortedError may only ever be thrown before the write is
1737
+ // issued, and a transient-failure retry (lockedWithRetry re-runs the whole body) must
1738
+ // re-check both before re-presenting the write.
1739
+ await this.lockedWithRetry(async () => {
1740
+ // Inside the lock: a write still parked behind another writer when the caller's
1741
+ // budget expired is abandoned rather than executed.
1742
+ if (signal?.aborted) {
1743
+ throw new FormationAbortedError(token, 'redemption');
619
1744
  }
620
- catch (rollbackError) {
621
- log('Rollback after redemption failure was a no-op: %s', rollbackError);
622
- }
623
- throw error;
624
- }
1745
+ await this.assertSeatRemains(token, totalUses);
1746
+ await this.inTransaction('redemption', async () => {
1747
+ // 1. Strand row — authorised by the FormationUsage branch (no owner sig),
1748
+ // still carrying a fresh unique StampId for the anti-replay column.
1749
+ // Hard-coded open + keyless: the consent branch admits no other shape.
1750
+ await this.db.exec(`
1751
+ insert into CadreControl.Strand (Id, Type, MemberPrivateKey, StampId)
1752
+ with context OwnerKey = null, Signature = null
1753
+ values (?, 'o', null, ?)
1754
+ `, [strandId, strandStampId]);
1755
+ // 2. FormationUsage row — authorised by the matching FormationInvite, and
1756
+ // carrying the strand's stamp so it authorizes THIS row and no other.
1757
+ await this.execFormationUsageInsert({
1758
+ token, usageStampId, disclosure, strandId, strandStampId,
1759
+ peerKey, peerSignature, nowMs: nowMs ?? Date.now(),
1760
+ validationKey: validationKey ?? null, validationSignature: validationSignature ?? null,
1761
+ });
1762
+ });
1763
+ }, {}, 'formation-usage');
1764
+ log('Redeemed invitation %s -> strand %s (stamp %s)', token, strandId, usageStampId);
1765
+ return { usageStampId };
625
1766
  }
626
1767
  /**
627
1768
  * Record a `FormationUsage` against an **already-existing** `Strand` (no strand
628
1769
  * insert). This is the redemption path when the strand was provisioned
629
1770
  * separately (e.g. owner-signed) and the consent record is added after the
630
1771
  * fact: the single insert auto-commits, and the deferred `StrandExists` CHECK
631
- * is satisfied by the pre-existing committed strand row. Returns the assigned
632
- * `UseNumber`.
1772
+ * is satisfied by the pre-existing committed strand row. Echoes back the
1773
+ * redemption's `usageStampId` (the row's primary key).
633
1774
  *
634
1775
  * Use {@link redeemInvitation} instead when the strand must be created by
635
1776
  * consent atomically with the usage.
1777
+ *
1778
+ * The strand's live `StampId` is read first and written onto the usage row:
1779
+ * `FormationUsage.StrandExists` matches the (id, stamp) PAIR, and
1780
+ * `Strand.AuthorizedInsert`'s consent branch reads the same pair back, so a consent
1781
+ * record authorizes exactly the strand ROW it was recorded against. A missing strand
1782
+ * THROWS here rather than being left to the deferred `StrandExists` CHECK — the
1783
+ * ordinary "host strand has not converged yet" case is already reported as `missing`
1784
+ * by {@link ControlFormationUsageRecorder.resolveStrand}, so an absent row at this
1785
+ * point is a genuine race and deserves a named error, not a silent rollback.
1786
+ *
1787
+ * The row is keyed by the joiner's own nonce (`UsageStampId` is the primary key), so this
1788
+ * path — the one production actually races on (a bound invite published by `cadre-web` /
1789
+ * `cadre-phone` is always record-only) — never contends with another redemption for a
1790
+ * shared row key: there is no lost race to retry and no second trip through the approval
1791
+ * hook. The invite's seat budget is checked by COUNT ({@link assertSeatRemains}) inside
1792
+ * the write lock, ahead of the write.
636
1793
  */
637
1794
  async recordFormationUsage(params) {
638
1795
  this.ensureInitialized();
639
- const { token, strandId, disclosure = '', peerId, peerSignature, nowMs, validationKey, validationSignature, } = params;
640
- const localPeerId = this.config.libp2pNode.peerId.toString();
641
- const useNumber = await this.nextUseNumber(token);
642
- await this.execFormationUsageInsert({
643
- token, useNumber, disclosure, strandId,
644
- peerId: peerId ?? localPeerId, peerSignature: peerSignature ?? null, nowMs: nowMs ?? Date.now(),
645
- validationKey: validationKey ?? null, validationSignature: validationSignature ?? null,
646
- });
647
- log('Recorded formation usage: token=%s strand=%s (use #%d)', token, strandId, useNumber);
648
- return useNumber;
1796
+ const { token, strandId, disclosure = '', peerKey, peerSignature, usageStampId, nowMs, validationKey, validationSignature, signal, totalUses, } = params;
1797
+ // The host strand is pre-existing and owner-signed; its stamp names the strand ROW the
1798
+ // consent record authorizes.
1799
+ const strandStampId = await this.queryStrandStampId(strandId);
1800
+ if (strandStampId === null) {
1801
+ throw new MissingHostStrandError(strandId, token);
1802
+ }
1803
+ // Ordering inside the locked body is contractual — abort check, seat check, write — see
1804
+ // the matching note in redeemInvitation.
1805
+ await this.lockedWithRetry(async () => {
1806
+ if (signal?.aborted) {
1807
+ throw new FormationAbortedError(token, 'usage recording');
1808
+ }
1809
+ await this.assertSeatRemains(token, totalUses);
1810
+ await this.execFormationUsageInsert({
1811
+ token, usageStampId, disclosure, strandId, strandStampId,
1812
+ peerKey, peerSignature, nowMs: nowMs ?? Date.now(),
1813
+ validationKey: validationKey ?? null, validationSignature: validationSignature ?? null,
1814
+ });
1815
+ }, {}, 'formation-usage');
1816
+ log('Recorded formation usage: token=%s strand=%s (stamp %s)', token, strandId, usageStampId);
1817
+ return { usageStampId };
1818
+ }
1819
+ /**
1820
+ * Refuse a redemption that would consume a seat the invite does not have.
1821
+ *
1822
+ * The schema's count-based cap clause (`FormationUsage.Authorized`) refuses an over-limit
1823
+ * write at the database anyway — but it fails as a generic `CHECK constraint failed:
1824
+ * Authorized`, which the manager reports as a retryable conflict. Catching it here as
1825
+ * {@link InvitationExhaustedError} lets the joiner be told the invitation is spent instead
1826
+ * of being sent around a loop that can never close. Runs inside the write lock, so on a
1827
+ * same-node race the loser reads the winner's committed row and is refused by name.
1828
+ *
1829
+ * `knownTotalUses` lets a caller that already read the `FormationInvite` row (both
1830
+ * production callers in `ControlFormationUsageRecorder` do) skip a second read on the common,
1831
+ * non-racing path. `undefined` falls back to a fresh read here.
1832
+ *
1833
+ * NOTE: a passed-in budget is a value read BEFORE the write lock (and, for a validating
1834
+ * invite, before an outbound approval call). Safe today because `FormationInvite` is
1835
+ * insert/delete only (its `Immutable` constraint), so the only way to stale it is an owner
1836
+ * revoking the token and re-issuing it with MORE seats mid-redemption; if invites ever gain
1837
+ * an update path, drop the parameter and read here instead.
1838
+ */
1839
+ async assertSeatRemains(token, knownTotalUses) {
1840
+ // A missing invite is left to `Authorized` — it is not an exhaustion, and the CHECK's
1841
+ // rejection is already the right, non-retryable answer for it. That fallback is also what
1842
+ // makes this read's failure mode safe: it is a full-primary-key point lookup
1843
+ // (`FormationInvite where Token = ?`), the shape whose reliability on a networked strand
1844
+ // is still open (tracked by `debt-composite-pk-point-lookup-unreliable-untracked`). A
1845
+ // spurious empty result only costs the attempt its NAMED exhaustion error, reverting it to
1846
+ // today's generic `Authorized` refusal — never a seat the invite does not have.
1847
+ const totalUses = knownTotalUses !== undefined
1848
+ ? knownTotalUses
1849
+ : (await this.queryFormationInvite(token))?.totalUses ?? null;
1850
+ if (totalUses == null) {
1851
+ return;
1852
+ }
1853
+ const used = await this.countFormationUsage(token);
1854
+ if (used >= totalUses) {
1855
+ throw new InvitationExhaustedError(token, used, totalUses);
1856
+ }
649
1857
  }
650
1858
  /** Parameterised `FormationUsage` insert shared by redeem + record paths. */
651
1859
  async execFormationUsageInsert(opts) {
@@ -658,14 +1866,14 @@ export class ControlDatabase {
658
1866
  // robustness/consistency change, matching the strand layer's `consumeInvite`,
659
1867
  // not a fix for an observable mis-ordering.
660
1868
  const nowCanonical = await canonicalDatetime(this.db, opts.nowMs);
661
- await this.db.exec(`
662
- insert into CadreControl.FormationUsage (Token, UseNumber, Disclosure, StrandId)
663
- with context PeerId = ?, PeerSignature = ?, Now = ?, ValidationKey = ?, ValidationSignature = ?
664
- values (?, ?, ?, ?)
1869
+ await this.db.exec(`
1870
+ insert into CadreControl.FormationUsage (Token, UsageStampId, PeerKey, PeerSig, Disclosure, StrandId, StrandStampId)
1871
+ with context Now = ?, ValidationKey = ?, ValidationSignature = ?
1872
+ values (?, ?, ?, ?, ?, ?, ?)
665
1873
  `, [
666
- opts.peerId, opts.peerSignature, nowCanonical,
667
- opts.validationKey, opts.validationSignature,
668
- opts.token, opts.useNumber, opts.disclosure, opts.strandId,
1874
+ nowCanonical, opts.validationKey, opts.validationSignature,
1875
+ opts.token, opts.usageStampId, opts.peerKey,
1876
+ opts.peerSignature, opts.disclosure, opts.strandId, opts.strandStampId,
669
1877
  ]);
670
1878
  }
671
1879
  /**
@@ -676,14 +1884,10 @@ export class ControlDatabase {
676
1884
  async queryFormationInvite(token) {
677
1885
  this.ensureInitialized();
678
1886
  for await (const row of this.db.eval('select Token, sAppId, ExpiresAt, TotalUses, ValidationUrl, StrandId from CadreControl.FormationInvite where Token = ?', [token])) {
679
- const expiresAt = row.ExpiresAt;
680
- const expiresAtMs = expiresAt === null || expiresAt === undefined
681
- ? null
682
- : parseStoredDatetimeMs(expiresAt);
683
1887
  return {
684
1888
  token: row.Token,
685
1889
  sAppId: row.sAppId,
686
- expiresAtMs: expiresAtMs !== null && Number.isNaN(expiresAtMs) ? null : expiresAtMs,
1890
+ expiresAtMs: parseNullableStoredDatetimeMs(row.ExpiresAt),
687
1891
  totalUses: row.TotalUses ?? null,
688
1892
  validationUrl: row.ValidationUrl ?? null,
689
1893
  strandId: row.StrandId ?? null,
@@ -693,6 +1897,22 @@ export class ControlDatabase {
693
1897
  }
694
1898
  /**
695
1899
  * Count `FormationUsage` rows recorded against a token (uses consumed so far).
1900
+ *
1901
+ * NOTE: `Token` left the primary key in `formation-unique-token-redesign` and gained the
1902
+ * `FormationUsageByToken` secondary index, so this filter is now served by an index SEEK
1903
+ * (a descent through the index collection) where it used to be a leading-key partial match
1904
+ * the optimystic vtab declined and the engine served by scan. That matters because the seat
1905
+ * cap is this count: a descent that under-reports admits a seat the invite does not have,
1906
+ * where a scan could not. Descent misses have been observed on a NETWORKED store (tracked
1907
+ * by `debt-composite-pk-point-lookup-unreliable-untracked`, for full-primary-key lookups);
1908
+ * no miss has been observed on this index on a SINGLE node. Across nodes the index is
1909
+ * currently blind: a sibling never learns index entries for rows written elsewhere (the
1910
+ * distributed commit drops the index update — measured 2026-08-12, tracked by
1911
+ * `blocked/secondary-index-seek-blind-to-sibling-rows`), so on a multi-machine party this
1912
+ * count under-reports and the cap over-admits until the upstream fix lands. If
1913
+ * over-admission ever shows up on a converged single node — a token with more usage rows
1914
+ * than its `TotalUses` and no concurrent redeemers to explain it — suspect this seek first
1915
+ * and drop the index to fall back to the scan.
696
1916
  */
697
1917
  async countFormationUsage(token) {
698
1918
  this.ensureInitialized();
@@ -701,17 +1921,70 @@ export class ControlDatabase {
701
1921
  }
702
1922
  return 0;
703
1923
  }
704
- /** Next `UseNumber` for a token = max(existing)+1, per the `Monotonic` constraint. */
705
- async nextUseNumber(token) {
706
- for await (const row of this.db.eval('select coalesce(max(UseNumber), 0) as MaxUse from CadreControl.FormationUsage where Token = ?', [token])) {
707
- return row.MaxUse + 1;
1924
+ /**
1925
+ * Is any `FormationInvite` row still redeemable — unexpired AND with usage
1926
+ * below its `TotalUses`? A null `ExpiresAt` never expires and a null
1927
+ * `TotalUses` is unlimited, matching {@link ControlFormationUsageRecorder}'s
1928
+ * per-token semantics (`isTokenValid` / `isTokenUsed`).
1929
+ *
1930
+ * Answers the control-network connection gate's coarse "does this node expect
1931
+ * a stranger?" question, which has no token to ask about. The expiry
1932
+ * comparison is `expiresAtMs <= now` — identical to `isTokenValid`'s — so an
1933
+ * invite the formation handler would reject can never hold the gate open.
1934
+ *
1935
+ * The scan is deliberately not pushed into SQL: nothing else here compares a
1936
+ * stored `datetime` with an inequality, so the parse stays in JS via the
1937
+ * shared {@link parseNullableStoredDatetimeMs}. Only invites that are
1938
+ * unexpired AND use-metered cost a {@link countFormationUsage} read, and an
1939
+ * unlimited-use invite anywhere in the scan short-circuits all of them.
1940
+ */
1941
+ async hasOutstandingFormationInvite(nowMs = Date.now()) {
1942
+ this.ensureInitialized();
1943
+ // NOTE: scans every FormationInvite row (expired ones included) on the
1944
+ // stranger path of an inbound connection. Cadre-scale invite counts make
1945
+ // that free today; if a long-lived cadre accumulates thousands of expired
1946
+ // invites and inbound upgrades slow down, add an expiry-ordered index or
1947
+ // prune redeemed/expired rows.
1948
+ const metered = [];
1949
+ let unlimitedOutstanding = false;
1950
+ for await (const row of this.db.eval('select Token, ExpiresAt, TotalUses from CadreControl.FormationInvite')) {
1951
+ const expiresAtMs = parseNullableStoredDatetimeMs(row.ExpiresAt);
1952
+ if (expiresAtMs !== null && expiresAtMs <= nowMs) {
1953
+ continue;
1954
+ }
1955
+ const totalUses = row.TotalUses ?? null;
1956
+ if (totalUses === null) {
1957
+ unlimitedOutstanding = true;
1958
+ }
1959
+ else {
1960
+ metered.push({ token: row.Token, totalUses });
1961
+ }
708
1962
  }
709
- return 1;
1963
+ if (unlimitedOutstanding) {
1964
+ return true;
1965
+ }
1966
+ for (const invite of metered) {
1967
+ if (await this.countFormationUsage(invite.token) < invite.totalUses) {
1968
+ return true;
1969
+ }
1970
+ }
1971
+ return false;
710
1972
  }
711
1973
  /**
712
- * Close the database and cleanup resources
1974
+ * Close the database and cleanup resources.
1975
+ *
1976
+ * Drains the local-write chain first: {@link withWriteLock} can park a write behind
1977
+ * others across an await, and a queued closure evaluates `this.db!` only when it
1978
+ * finally runs — so nulling the handle out from under it would throw a TypeError on a
1979
+ * null handle instead of committing. The tail never rejects (it swallows both
1980
+ * outcomes), so the bare await is safe.
1981
+ *
1982
+ * NOTE: this makes `close()` wait on a stuck write. Acceptable today — every locked
1983
+ * body is a bounded local `exec` — but revisit (bounded drain, or abandon after a
1984
+ * deadline) if a write can ever hang.
713
1985
  */
714
1986
  async close() {
1987
+ await this.writeQueue;
715
1988
  if (this.collectionFactory) {
716
1989
  await this.collectionFactory.shutdown();
717
1990
  this.collectionFactory = null;