@serfab/cadre-core 0.12.0 → 1.0.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 (155) hide show
  1. package/README.md +23 -8
  2. package/dist/bootstrap-peer-store-file.js +2 -2
  3. package/dist/bootstrap-peer-store.d.ts +32 -12
  4. package/dist/bootstrap-peer-store.js +41 -15
  5. package/dist/bootstrap-peer-store.js.map +1 -1
  6. package/dist/cadre-node.d.ts +883 -102
  7. package/dist/cadre-node.js +1800 -209
  8. package/dist/cadre-node.js.map +1 -1
  9. package/dist/control-authorization.d.ts +2 -2
  10. package/dist/control-authorization.js +1 -0
  11. package/dist/control-authorization.js.map +1 -1
  12. package/dist/control-cohort.d.ts +13 -26
  13. package/dist/control-cohort.js +0 -26
  14. package/dist/control-cohort.js.map +1 -1
  15. package/dist/control-database.d.ts +318 -44
  16. package/dist/control-database.js +516 -74
  17. package/dist/control-database.js.map +1 -1
  18. package/dist/control-read-retry.d.ts +40 -11
  19. package/dist/control-read-retry.js +48 -15
  20. package/dist/control-read-retry.js.map +1 -1
  21. package/dist/control-retry.d.ts +58 -3
  22. package/dist/control-retry.js +58 -3
  23. package/dist/control-retry.js.map +1 -1
  24. package/dist/control-schema.d.ts +1 -1
  25. package/dist/control-schema.js +133 -21
  26. package/dist/control-schema.js.map +1 -1
  27. package/dist/control-write-retry.d.ts +80 -11
  28. package/dist/control-write-retry.js +168 -29
  29. package/dist/control-write-retry.js.map +1 -1
  30. package/dist/enrolled-machine-store-file.d.ts +13 -0
  31. package/dist/enrolled-machine-store-file.js +41 -0
  32. package/dist/enrolled-machine-store-file.js.map +1 -0
  33. package/dist/enrolled-machine-store.d.ts +81 -0
  34. package/dist/enrolled-machine-store.js +225 -0
  35. package/dist/enrolled-machine-store.js.map +1 -0
  36. package/dist/file-durable-slot.js +5 -3
  37. package/dist/file-durable-slot.js.map +1 -1
  38. package/dist/index.d.ts +17 -8
  39. package/dist/index.js +54 -9
  40. package/dist/index.js.map +1 -1
  41. package/dist/membership-connection-gater.d.ts +15 -1
  42. package/dist/membership-connection-gater.js +9 -3
  43. package/dist/membership-connection-gater.js.map +1 -1
  44. package/dist/node-local-snapshot.d.ts +21 -7
  45. package/dist/node-local-snapshot.js +30 -9
  46. package/dist/node-local-snapshot.js.map +1 -1
  47. package/dist/peer-dial.d.ts +143 -0
  48. package/dist/peer-dial.js +176 -0
  49. package/dist/peer-dial.js.map +1 -0
  50. package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
  51. package/dist/{strand-backfill.js → peer-join-backfill.js} +133 -52
  52. package/dist/peer-join-backfill.js.map +1 -0
  53. package/dist/relay-addrs.d.ts +160 -49
  54. package/dist/relay-addrs.js +287 -60
  55. package/dist/relay-addrs.js.map +1 -1
  56. package/dist/relay-reservation.d.ts +71 -3
  57. package/dist/relay-reservation.js +116 -8
  58. package/dist/relay-reservation.js.map +1 -1
  59. package/dist/seed-bootstrap.d.ts +30 -1
  60. package/dist/seed-bootstrap.js +62 -17
  61. package/dist/seed-bootstrap.js.map +1 -1
  62. package/dist/storage-scope.d.ts +62 -0
  63. package/dist/storage-scope.js +75 -0
  64. package/dist/storage-scope.js.map +1 -0
  65. package/dist/strand-database.d.ts +41 -11
  66. package/dist/strand-database.js +48 -18
  67. package/dist/strand-database.js.map +1 -1
  68. package/dist/strand-first-sync-gate.d.ts +152 -0
  69. package/dist/strand-first-sync-gate.js +208 -0
  70. package/dist/strand-first-sync-gate.js.map +1 -0
  71. package/dist/strand-formation-manager.d.ts +99 -5
  72. package/dist/strand-formation-manager.js +119 -8
  73. package/dist/strand-formation-manager.js.map +1 -1
  74. package/dist/strand-formation-protocol.d.ts +97 -3
  75. package/dist/strand-formation-protocol.js +108 -2
  76. package/dist/strand-formation-protocol.js.map +1 -1
  77. package/dist/strand-instance-manager.d.ts +389 -19
  78. package/dist/strand-instance-manager.js +678 -65
  79. package/dist/strand-instance-manager.js.map +1 -1
  80. package/dist/strand-member-key.d.ts +20 -12
  81. package/dist/strand-member-key.js +20 -12
  82. package/dist/strand-member-key.js.map +1 -1
  83. package/dist/strand-member-registry.js +3 -0
  84. package/dist/strand-member-registry.js.map +1 -1
  85. package/dist/strand-membership-reconciler.d.ts +361 -0
  86. package/dist/strand-membership-reconciler.js +548 -0
  87. package/dist/strand-membership-reconciler.js.map +1 -0
  88. package/dist/strand-membership-writer.d.ts +143 -11
  89. package/dist/strand-membership-writer.js +419 -135
  90. package/dist/strand-membership-writer.js.map +1 -1
  91. package/dist/strand-network-config.d.ts +116 -0
  92. package/dist/strand-network-config.js +192 -0
  93. package/dist/strand-network-config.js.map +1 -0
  94. package/dist/strand-revocation-enforcer.d.ts +392 -0
  95. package/dist/strand-revocation-enforcer.js +533 -0
  96. package/dist/strand-revocation-enforcer.js.map +1 -0
  97. package/dist/strand-solicitation.d.ts +17 -1
  98. package/dist/strand-solicitation.js +13 -2
  99. package/dist/strand-solicitation.js.map +1 -1
  100. package/dist/strand-wake-protocol.d.ts +11 -7
  101. package/dist/strand-wake-protocol.js +20 -52
  102. package/dist/strand-wake-protocol.js.map +1 -1
  103. package/dist/strand-watcher.js +12 -1
  104. package/dist/strand-watcher.js.map +1 -1
  105. package/dist/timed-step.d.ts +12 -0
  106. package/dist/timed-step.js +28 -0
  107. package/dist/timed-step.js.map +1 -0
  108. package/dist/timeout-scheduler.d.ts +19 -0
  109. package/dist/timeout-scheduler.js +22 -0
  110. package/dist/timeout-scheduler.js.map +1 -0
  111. package/dist/types.d.ts +472 -46
  112. package/dist/types.js +7 -1
  113. package/dist/types.js.map +1 -1
  114. package/package.json +16 -8
  115. package/src/bootstrap-peer-store-file.ts +2 -2
  116. package/src/bootstrap-peer-store.ts +69 -27
  117. package/src/cadre-node.ts +1926 -228
  118. package/src/control-authorization.ts +2 -1
  119. package/src/control-cohort.ts +122 -130
  120. package/src/control-database.ts +586 -74
  121. package/src/control-read-retry.ts +202 -167
  122. package/src/control-retry.ts +297 -190
  123. package/src/control-schema.ts +133 -21
  124. package/src/control-write-retry.ts +525 -383
  125. package/src/enrolled-machine-store-file.ts +43 -0
  126. package/src/enrolled-machine-store.ts +280 -0
  127. package/src/file-durable-slot.ts +5 -3
  128. package/src/index.ts +134 -15
  129. package/src/membership-connection-gater.ts +9 -3
  130. package/src/node-local-snapshot.ts +38 -9
  131. package/src/peer-addr-book.ts +251 -250
  132. package/src/peer-dial.ts +215 -0
  133. package/src/{strand-backfill.ts → peer-join-backfill.ts} +478 -389
  134. package/src/relay-addrs.ts +458 -218
  135. package/src/relay-reservation.ts +951 -828
  136. package/src/seed-bootstrap.ts +76 -18
  137. package/src/storage-scope.ts +78 -0
  138. package/src/strand-database.ts +61 -21
  139. package/src/strand-first-sync-gate.ts +258 -0
  140. package/src/strand-formation-manager.ts +162 -8
  141. package/src/strand-formation-protocol.ts +850 -692
  142. package/src/strand-instance-manager.ts +877 -74
  143. package/src/strand-member-key.ts +20 -12
  144. package/src/strand-member-registry.ts +3 -0
  145. package/src/strand-membership-reconciler.ts +680 -0
  146. package/src/strand-membership-writer.ts +550 -190
  147. package/src/strand-network-config.ts +250 -0
  148. package/src/strand-revocation-enforcer.ts +653 -0
  149. package/src/strand-solicitation.ts +499 -471
  150. package/src/strand-wake-protocol.ts +21 -66
  151. package/src/strand-watcher.ts +325 -314
  152. package/src/timed-step.ts +28 -0
  153. package/src/timeout-scheduler.ts +28 -0
  154. package/src/types.ts +487 -52
  155. package/dist/strand-backfill.js.map +0 -1
@@ -0,0 +1,392 @@
1
+ /**
2
+ * Strand revoked-peer enforcement: a per-strand deny gate that refuses the
3
+ * network nodes of a REMOVED party, so removal means "we stop talking to you"
4
+ * rather than only deleting a row.
5
+ *
6
+ * ## The deny set is revocation-keyed, never membership-keyed
7
+ *
8
+ * A strand node maintains a materialized in-memory set of revoked peer ids:
9
+ * every `Strand.MemberPeer.PeerId` whose `MemberKey` has no live `Strand.Member`
10
+ * row. `MemberPeer.MemberExists` runs on insert, so an orphaned binding can only
11
+ * mean "this member existed and was removed" — it is exactly the durable,
12
+ * replicated record of a removed party's machines. Everything NOT in the set is
13
+ * admitted. Deliberate consequences:
14
+ *
15
+ * - **Open strands are untouched.** `Member`/`MemberPeer` carry `OnlyClosed`
16
+ * checks, so an open strand has no rows and the set is empty — and
17
+ * `StrandInstanceManager` additionally skips arming this enforcer entirely for
18
+ * `Type !== 'c'`, so an open strand does not even poll.
19
+ * - **A joining party mid-admission is admitted** (it has no orphaned binding).
20
+ * An allowlist ("only peers bound to live members") was considered and
21
+ * rejected: a joiner's first membership writes must travel through the very
22
+ * cohort an allowlist would close to it — the same mutual-denial trap the
23
+ * control network's gater doc describes (`membership-connection-gater.ts`),
24
+ * with no enrollment window to carve out. A deny-list on positive evidence
25
+ * has no such trap.
26
+ * - **Stale-view failure direction: fail-open.** A remaining member that has not
27
+ * yet replicated the revocation keeps serving the removed party until the
28
+ * tombstoned state arrives — bounded by replication, consistent with
29
+ * "revocation is forward-looking". The opposite error (denying a legitimate
30
+ * member) is only reachable for a revoked-then-re-admitted party at a node
31
+ * that saw the revoke but not the re-add; the re-add gives the member key a
32
+ * live `Member` row again, which removes its peers from the deny set as the
33
+ * row replicates — self-healing, and the re-adding manager's own node updates
34
+ * first, so the heal never depends on talking to the peer being wrongly denied.
35
+ * - **Orphaned `MemberPeer` rows are load-bearing**: they ARE the deny record —
36
+ * the `Strand.Revocation` tombstone keeps only the stamp, not the peer id — so
37
+ * nothing here (or anywhere) tombstones or auto-cleans them at revocation.
38
+ * The manager-cleanup path (`removeMemberPeer`'s manager branch) stays as the
39
+ * only way to clear a wrongly-registered binding, and clearing an orphan
40
+ * forgets the denial (the NOTE at that branch says so).
41
+ *
42
+ * ## Removal cuts LIVE sessions too — the refresh IS the teardown seam
43
+ *
44
+ * Refusing NEW connections only half-honors "no longer be answered to or
45
+ * communicated with": a long-lived strand connection may never re-dial, so
46
+ * without teardown a removed party keeps whatever session it already holds.
47
+ * There is no push notification to hang teardown off — a REPLICATED revocation
48
+ * arrives with no change event (the strand database has no reactive
49
+ * subscription; `StrandWatcher` polls for the same reason), and a LOCAL
50
+ * `revokeMember`/`leaveStrand` writes through a bare `Database` handle this
51
+ * runtime never sees — so teardown rides the snapshot refresh itself, with
52
+ * `refresh()` exposed for immediacy
53
+ * (`StrandInstanceManager.refreshRevocationEnforcement` →
54
+ * `CadreNode.refreshRevocationEnforcement`).
55
+ *
56
+ * The sweep is driven by MEMBERSHIP in the deny set, never by an
57
+ * entered-the-set diff: every refresh hangs up every currently-connected
58
+ * revoked peer, so a resume, a crashed refresh, or a connection established
59
+ * between two refreshes cannot leave a session standing (a diff would be a
60
+ * strict subset of this, so none is kept). A peer with no open connection is
61
+ * simply not swept, and a failed `hangUp` is logged, never thrown — the next
62
+ * refresh retries it. `hangUp` closes DIRECT and RELAYED connections alike, and
63
+ * on a strand node running the relay server it also drops any reservation the
64
+ * revoked peer holds there (a reservation dies with its connection — see the
65
+ * relay-seam doc in `membership-connection-gater.ts`).
66
+ *
67
+ * Teardown is also what stops OUTBOUND traffic. The gate is one-directional —
68
+ * `authorizeStream` judges streams the revoked peer opens to US — so nothing
69
+ * else prevents this node from pushing over an already-open connection, which
70
+ * `PeerJoinBackfill` does for that connection's whole life. Closing the
71
+ * connection stops both directions at once, which is why no separate outbound
72
+ * gate exists.
73
+ *
74
+ * If this node's OWN peer id is in the set, it was itself removed (or it left —
75
+ * a voluntary departure files the same tombstone). Nothing is torn down for
76
+ * that: the other side's gate already refuses us, and every other revoked peer
77
+ * is still swept. Instead `onSelfRevoked` fires ONCE per entry into the set
78
+ * (re-armed if the party is re-admitted, so a second removal signals again),
79
+ * which `CadreNode` surfaces as `strand:revoked` so the embedding app decides
80
+ * what to do — this module never stops or leaves a strand on its own.
81
+ * Best-effort by nature: a removed party learns only if the tombstone
82
+ * replicated to it before the cut.
83
+ *
84
+ * ## What transfers from the control gater (`membership-connection-gater.ts`)
85
+ *
86
+ * Transfers: the two-layer shape (fail-closed per-stream gate as PRIMARY, an
87
+ * opportunistic connection-level deny on top), the synchronous in-memory
88
+ * snapshot judged by the stream gate (a live DB read inside a gate deadlocks —
89
+ * the same argument as the control node's `authorizeInboundControlStream`), the
90
+ * fail-open-on-error/timeout posture for the connection hooks
91
+ * (`decideWithinDeadline`, reused as-is), and the "compose over the caller's
92
+ * gater, every base hook preserved" mechanics.
93
+ *
94
+ * Does NOT transfer — the relay-reservation seam (`'admit-for-relay'` + the
95
+ * unauthorized budget). That machinery exists because the control gate denies on
96
+ * ABSENCE of placement, where a member whose row is in flight is
97
+ * indistinguishable from an outsider and a wrong reservation deny is
98
+ * unrecoverable. This gate denies on POSITIVE revocation evidence, so a
99
+ * reservation deny is never that kind of wrong answer; the one stale-wrong case
100
+ * (a missed re-add) heals via replication as above. `denyInboundRelayReservation`
101
+ * here is therefore a plain "deny iff revoked" — no budget, no admit-for-relay
102
+ * verdict, no reserve deadline.
103
+ *
104
+ * Does NOT transfer — the bring-up quiet period. A strand node's DB bring-up
105
+ * NEEDS its cross-party cohort connections; this enforcer simply starts with an
106
+ * empty snapshot (admit everything) until its first successful read — the same
107
+ * fail-open posture as everywhere else here.
108
+ *
109
+ * Does NOT transfer — every stranger carve-out (enrollment window, formation
110
+ * window, delegate grants). None of those protocols exist on a strand node.
111
+ *
112
+ * Unlike the control stream gate, no empty-snapshot carve-out is needed: an
113
+ * empty deny set means "nothing revoked", which correctly admits everyone — the
114
+ * deny-list shape makes the cold-start case and the steady case the same code
115
+ * path.
116
+ */
117
+ import type { ConnectionGater, PeerId } from '@libp2p/interface';
118
+ import type { Database } from '@quereus/quereus';
119
+ /**
120
+ * Default cadence of the deny-set refresh. Bounded staleness is the accepted
121
+ * failure direction (see the module doc's stale-view paragraph); 30 s keeps a
122
+ * revoked peer's post-replication admit window in the same order as the
123
+ * replication delay itself. Overridable via
124
+ * {@link StrandRevocationEnforcementConfig.pollIntervalMs}.
125
+ */
126
+ export declare const DEFAULT_REVOCATION_POLL_INTERVAL_MS = 30000;
127
+ /**
128
+ * Embedder tuning for the strand revoked-peer gate, threaded from
129
+ * `CadreNodeConfig.strandRevocationEnforcement` to
130
+ * `StartStrandConfig.revocationEnforcement` the way the backfill knob is.
131
+ */
132
+ export interface StrandRevocationEnforcementConfig {
133
+ /**
134
+ * Default true. False disarms the gate entirely for this node's strands —
135
+ * the pre-existing behaviour (a removed party's peers keep being served).
136
+ */
137
+ enabled?: boolean;
138
+ /** Deny-set refresh cadence, ms. Default {@link DEFAULT_REVOCATION_POLL_INTERVAL_MS}. */
139
+ pollIntervalMs?: number;
140
+ }
141
+ /** One `Strand.MemberPeer` row, as the deny-set derivation consumes it. */
142
+ export interface StrandMemberPeerBinding {
143
+ memberKey: string;
144
+ peerId: string;
145
+ }
146
+ /** The membership rows one deny-set derivation reads. */
147
+ export interface StrandRevocationRows {
148
+ /** Every live `Strand.Member.Key`. */
149
+ memberKeys: ReadonlySet<string>;
150
+ /** Every `Strand.MemberPeer` row (live-membered and orphaned alike). */
151
+ bindings: readonly StrandMemberPeerBinding[];
152
+ }
153
+ /**
154
+ * Timer seam so the refresh cadence is testable without fake timers — the
155
+ * injectable-clock idiom of `UnauthorizedReservationBudget`'s `now` argument,
156
+ * shaped for an interval-driven component. The default schedules a real
157
+ * interval and unrefs it so an armed enforcer never holds a process open.
158
+ */
159
+ export interface RevocationRefreshScheduler {
160
+ setInterval(fn: () => void, ms: number): unknown;
161
+ clearInterval(handle: unknown): void;
162
+ }
163
+ /**
164
+ * The minimum of a libp2p node the teardown sweep needs: who WE are, what is
165
+ * open, and how to close it. Deliberately not the node itself, so the sweep
166
+ * unit-tests against a three-property stub; `Libp2p` (hence
167
+ * `Libp2pNodeWithRepo`) satisfies it structurally, which is what
168
+ * `StrandInstanceManager` passes.
169
+ */
170
+ export interface StrandRevocationNetwork {
171
+ /** This node's own peer id — how the enforcer recognizes ITSELF in the deny set. */
172
+ readonly peerId: PeerId;
173
+ /** Every open connection. Only `remotePeer` is read — and reused as the hangUp target. */
174
+ getConnections(): readonly {
175
+ readonly remotePeer: PeerId;
176
+ }[];
177
+ /** Close every connection to `peer`: direct, relayed, and any reservation riding one. */
178
+ hangUp(peer: PeerId): Promise<void>;
179
+ }
180
+ export interface StrandRevocationEnforcerDeps {
181
+ /** Log tag naming which strand this enforcer serves (the strand id). */
182
+ label: string;
183
+ /**
184
+ * Read the membership rows the deny set is derived from. Injectable so the
185
+ * enforcer unit-tests without a strand database; production wires
186
+ * {@link readStrandRevocationRows} over the strand's live DB. A throw or
187
+ * rejection KEEPS the previous snapshot (never clears it) — the control
188
+ * model, `refreshAuthorizedControlPeers`.
189
+ */
190
+ readRows: () => Promise<StrandRevocationRows>;
191
+ /**
192
+ * Handle on the strand's libp2p node for the teardown sweep, read LAZILY on
193
+ * every sweep — never captured. It has to be: the enforcer is constructed
194
+ * BEFORE the node (the node's options embed the enforcer's predicates), and
195
+ * the node is dropped again on quiesce. Omit it, or return undefined, and the
196
+ * gate still refuses new connections — only teardown of OPEN ones is skipped.
197
+ */
198
+ getNetwork?: () => StrandRevocationNetwork | undefined;
199
+ /**
200
+ * Called ONCE each time this node's own peer id ENTERS the deny set — i.e.
201
+ * this node's party was removed from the strand, or left it. Not re-fired
202
+ * while it stays there; re-armed if the party is re-admitted, so a second
203
+ * removal signals again. Wired by `StrandInstanceManager` through to
204
+ * `CadreNode`'s `strand:revoked` event. A resume rebuilds the enforcer, so a
205
+ * still-revoked strand also legitimately re-fires once per resume. A throw is
206
+ * logged and swallowed — an embedder's handler must not derail the sweep.
207
+ */
208
+ onSelfRevoked?: () => void;
209
+ /** Timer seam; omit for real (unref'd) intervals. */
210
+ scheduler?: RevocationRefreshScheduler;
211
+ }
212
+ /**
213
+ * Read the deny-set inputs from a strand database: full-scan
214
+ * `Strand.MemberPeer` and `Strand.Member`, to be joined in JavaScript by the
215
+ * enforcer. Scan-and-filter, never seek — the composite-PK point-lookup
216
+ * unreliability documented at `scanMemberPeers` in `strand-membership-writer.ts`
217
+ * applies, so correctness depends only on each scan returning a superset of the
218
+ * live rows.
219
+ *
220
+ * **Scan order is load-bearing, and it is BINDINGS FIRST.** The two scans are
221
+ * separate reads with nothing holding them to one snapshot, so a write that
222
+ * lands between them is seen by the second scan and not the first. A member
223
+ * joins as `Member` row then `MemberPeer` row (`MemberExists` forces that
224
+ * order), so reading `Member` first would let a join that commits mid-read
225
+ * produce a binding whose member key is absent from the key set — a brand-new
226
+ * member classified as REVOKED, the one fail-CLOSED outcome this module exists
227
+ * to avoid. Reading bindings first inverts the skew: the new binding is simply
228
+ * not in the older bindings snapshot, so the joiner is unclassified and
229
+ * admitted, and a revocation that lands mid-read is still seen (the key is
230
+ * missing from the NEWER key scan). Every skew window then resolves fail-open,
231
+ * matching the module doc.
232
+ *
233
+ * NOTE: two whole-table scans per refresh (default every 30 s per closed
234
+ * strand). Fine at strand scale (a handful of members and bindings); if
235
+ * membership churn ever makes these tables large, the fix is a reliable
236
+ * filtered read — which needs the networked point-lookup gap closed
237
+ * (`debt-composite-pk-point-lookup-unreliable-untracked`) — not a bigger scan.
238
+ */
239
+ export declare function readStrandRevocationRows(db: Database): Promise<StrandRevocationRows>;
240
+ /**
241
+ * The per-strand revoked-peer deny set and the synchronous predicates over it.
242
+ *
243
+ * Lifecycle mirrors `PeerJoinBackfill`: created in
244
+ * `StrandInstanceManager.buildStrandRuntime` (closed strands only), started
245
+ * after the strand database initializes, stopped and dropped in
246
+ * `releaseRuntime` — so quiesce → resume rebuilds it with a fresh snapshot.
247
+ * The snapshot starts EMPTY (admit everything) and repopulates on the first
248
+ * successful read; a revoked peer therefore gets a short admit window per
249
+ * resume at the connection level. Accepted: fail-open posture, and resume is
250
+ * never blocked on a refresh.
251
+ */
252
+ export declare class StrandRevocationEnforcer {
253
+ private readonly deps;
254
+ /** The materialized deny set — peer ids with an orphaned binding and no live one. */
255
+ private revokedPeerIds;
256
+ private readonly scheduler;
257
+ private readonly pollIntervalMs;
258
+ /** Tail of the refresh chain — what serializes refreshes (no two reads in flight). */
259
+ private refreshTail;
260
+ private refreshing;
261
+ private intervalHandle;
262
+ private started;
263
+ private stopped;
264
+ /** Latch for the one-shot self-revocation signal — see {@link StrandRevocationEnforcerDeps.onSelfRevoked}. */
265
+ private selfRevokedSignaled;
266
+ constructor(deps: StrandRevocationEnforcerDeps, config?: StrandRevocationEnforcementConfig);
267
+ /** Number of peers currently denied — test/diagnostic surface. */
268
+ get revokedCount(): number;
269
+ /** Kick an immediate refresh (not awaited — bring-up never blocks on it) and arm the poll. */
270
+ start(): void;
271
+ /** Disarm the poll; a read already in flight completes but its result is discarded. */
272
+ stop(): void;
273
+ /**
274
+ * Refresh the deny set now. Never rejects. Serialized: concurrent calls chain,
275
+ * so no two reads overlap, and each explicit call gets a read that STARTS
276
+ * after the call (an on-demand refresh after a revocation commit observes that
277
+ * commit). A failed or slow read keeps the previous snapshot — never clears it.
278
+ */
279
+ refresh(): Promise<void>;
280
+ /**
281
+ * Is `remotePeerId` positively revoked in the current snapshot? Synchronous by
282
+ * design — this is judged inside libp2p gates where an await against the DB
283
+ * would deadlock (the control stream gate's argument).
284
+ */
285
+ isRevoked(remotePeerId: string): boolean;
286
+ /**
287
+ * The fail-closed PRIMARY layer: the per-stream predicate wired as
288
+ * `createLibp2pNode({ authorizeInboundStream })`, gating all four
289
+ * `/optimystic/strand-<id>/{repo,cluster,sync,block-transfer}` protocols.
290
+ * Also what catches a revoked peer arriving over a RELAYED connection, or
291
+ * over a connection admitted before the revocation replicated here.
292
+ */
293
+ authorizeStream(remotePeerId: string, _protocol: string): boolean;
294
+ /**
295
+ * One serialized snapshot rebuild; contains every failure (contract: never rejects).
296
+ *
297
+ * NOTE: nothing here carries a deadline — a `readRows` or a `hangUp` that never
298
+ * settles wedges the refresh chain, which freezes the deny set (the poll skips
299
+ * while `refreshing`, and every later `refresh()` chains behind the stuck one).
300
+ * Both are bounded in practice (Quereus reads settle; libp2p's connection close
301
+ * has its own timeout). If a strand is ever seen with a stale deny set and no
302
+ * refresh log line, wrap both awaits in a timeout rather than hunting further.
303
+ */
304
+ private doRefresh;
305
+ /**
306
+ * Close what is already OPEN to the peers of `revoked` — the teardown half of
307
+ * enforcement (module doc: "Removal cuts LIVE sessions too"). Never rejects:
308
+ * every failure is contained, so the refresh loop survives and simply retries
309
+ * on the next tick.
310
+ *
311
+ * `revoked` is passed IN rather than read off the field, so the sweep judges
312
+ * the exact snapshot its own refresh produced; likewise the connection list is
313
+ * enumerated once, before any await, so a peer re-admitted mid-sweep is at
314
+ * worst hung up once and reconnects.
315
+ *
316
+ * NOTE: dropping the revoked peer from the strand's Optimystic cohort needs no
317
+ * separate action — machines quiesce strands routinely and the remaining cohort
318
+ * keeps committing, so an unreachable cohort peer is already tolerated, and
319
+ * `denyDialPeer` makes the exclusion fast (a local refusal, not a dial timeout).
320
+ * Two residual facts, both accepted: the removed node keeps every block it had
321
+ * already replicated (revocation is forward-looking), and a block whose only
322
+ * holders were the removed party's machines can be unavailable to the rest
323
+ * until repair.
324
+ *
325
+ * NOTE: post-removal commit latency is real and MEASURED, not hypothetical. On
326
+ * the two-parties-by-two-machines fixture in
327
+ * `integration-tests/src/scenarios/strand-removal-cuts-network.integration.ts`
328
+ * the remaining cohort's FIRST write after a cut fails repeatedly with
329
+ * `BlockUnavailableError: … (peers-unreachable)` before it commits — three or
330
+ * four failures, 3.5-4.3 s, over four runs — because the cohort still listed
331
+ * the removed party's now unreachable machines and had to downsize to the
332
+ * live holders first. It
333
+ * recovers on its own, so this is a latency cost, not a defect, and it is
334
+ * stated for app authors in `docs/strands.md` → Removing Members. If it ever
335
+ * stops recovering — a write that never commits, or `cluster-fetch:no-quorum` —
336
+ * that is an upstream (Optimystic) conversation, not a workaround here.
337
+ */
338
+ private tearDownRevoked;
339
+ /** The lazily-read network handle; an accessor that throws costs this pass only. */
340
+ private resolveNetwork;
341
+ /** One hangUp, contained: a stopping node or an already-closed connection is not an error here. */
342
+ private hangUpRevoked;
343
+ /**
344
+ * Latch {@link StrandRevocationEnforcerDeps.onSelfRevoked} to the TRANSITION
345
+ * into the revoked state: fired once on entry, and re-armed the moment this
346
+ * node is out of the set again — a manager can re-admit a party it removed
347
+ * (a fresh `Member` row makes the orphaned bindings live again), and a second
348
+ * removal has to be reported like the first.
349
+ */
350
+ private trackSelfRevoked;
351
+ }
352
+ /** The one capability the gater composition needs from the enforcer. */
353
+ export type StrandRevocationJudge = Pick<StrandRevocationEnforcer, 'isRevoked'>;
354
+ /**
355
+ * Build a closed strand's connection gater: the caller-supplied gater (if any)
356
+ * with revoked-peer denial composed onto three hooks. The opportunistic
357
+ * SECONDARY layer — the per-stream gate above is the fail-closed primary.
358
+ *
359
+ * - `denyInboundEncryptedConnection`: deny iff revoked, so a removed party's
360
+ * node is refused before any protocol negotiation.
361
+ * - `denyDialPeer`: deny iff revoked — the owner ruling covers "communicated
362
+ * with", not just "answered to", and refusing the dial locally is instant,
363
+ * which keeps Optimystic from wasting a dial timeout on a peer this node
364
+ * will not talk to anyway.
365
+ * - `denyInboundRelayReservation`: deny iff revoked — matters when the strand
366
+ * node runs the relay server (the storage-profile default), where admitting
367
+ * the reservation would let a removed party keep using this node's
368
+ * forwarding capacity.
369
+ *
370
+ * Composition semantics: every hook of `base` is preserved as-is; on the three
371
+ * composed hooks a deny from EITHER the base gater or the revocation check
372
+ * denies, and an error (or a decision slower than `decisionTimeoutMs`) in
373
+ * either is fail-open — the base error admits through to the revocation check,
374
+ * a revocation-check error admits outright, and the fail-closed stream gate
375
+ * still stands behind both. The deadline is belt-and-braces: the snapshot read
376
+ * is synchronous.
377
+ *
378
+ * NOTE: accepted tradeoff — swallowing a BASE hook's throw diverges from
379
+ * `createMembershipConnectionGater`, which lets it propagate to libp2p (a
380
+ * fail-closed outcome there). Uniform fail-open was chosen for this module so
381
+ * every hook has one failure direction, and it was the shape the plan specified;
382
+ * the cost is that an embedder gater which throws is silently admitted here
383
+ * while the same gater passed raw to an OPEN strand would refuse. Loud in the
384
+ * log either way. Revisit if an embedder ever ships a gater whose throw is a
385
+ * meaningful deny, or if the two gaters are unified.
386
+ *
387
+ * NOTE: `base` is spread, so a gater passed as a CLASS INSTANCE would lose its
388
+ * prototype methods — same caveat as `createMembershipConnectionGater`; every
389
+ * caller in this repo supplies a plain object (the RN/web permissive gaters,
390
+ * e.g. `{ denyDialMultiaddr: () => false }`, keep working unchanged).
391
+ */
392
+ export declare function createRevocationConnectionGater(enforcer: StrandRevocationJudge, base?: ConnectionGater, decisionTimeoutMs?: number): ConnectionGater;