@serfab/cadre-core 1.23.0 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (313) hide show
  1. package/README.md +216 -294
  2. package/dist/arachnode-stub.d.ts +12 -4
  3. package/dist/arachnode-stub.js +15 -8
  4. package/dist/arachnode-stub.js.map +1 -1
  5. package/dist/cadre-invite-protocol.js +5 -4
  6. package/dist/cadre-invite-protocol.js.map +1 -1
  7. package/dist/cadre-node.d.ts +145 -44
  8. package/dist/cadre-node.js +239 -79
  9. package/dist/cadre-node.js.map +1 -1
  10. package/dist/control-authorization.d.ts +14 -2
  11. package/dist/control-authorization.js +8 -3
  12. package/dist/control-authorization.js.map +1 -1
  13. package/dist/control-cohort.d.ts +27 -13
  14. package/dist/control-cohort.js +21 -14
  15. package/dist/control-cohort.js.map +1 -1
  16. package/dist/control-database.d.ts +18 -16
  17. package/dist/control-database.js +57 -22
  18. package/dist/control-database.js.map +1 -1
  19. package/dist/control-protocol-guard.d.ts +1 -1
  20. package/dist/control-protocol-guard.js +7 -3
  21. package/dist/control-protocol-guard.js.map +1 -1
  22. package/dist/control-read-retry.d.ts +49 -11
  23. package/dist/control-read-retry.js +56 -15
  24. package/dist/control-read-retry.js.map +1 -1
  25. package/dist/control-retry.d.ts +32 -3
  26. package/dist/control-retry.js +35 -4
  27. package/dist/control-retry.js.map +1 -1
  28. package/dist/control-schema-version.d.ts +5 -5
  29. package/dist/control-schema-version.js +5 -5
  30. package/dist/control-schema-version.js.map +1 -1
  31. package/dist/control-schema.d.ts +5 -5
  32. package/dist/control-schema.js +1032 -1011
  33. package/dist/control-schema.js.map +1 -1
  34. package/dist/control-write-retry.d.ts +54 -65
  35. package/dist/control-write-retry.js +134 -158
  36. package/dist/control-write-retry.js.map +1 -1
  37. package/dist/delegate-admission.d.ts +1 -1
  38. package/dist/delegate-admission.js +1 -1
  39. package/dist/edge-node/config.d.ts +4 -3
  40. package/dist/edge-node/config.js +11 -9
  41. package/dist/edge-node/config.js.map +1 -1
  42. package/dist/edge-node/dissolve.d.ts +4 -4
  43. package/dist/edge-node/dissolve.js +4 -4
  44. package/dist/edge-node/edge-join.d.ts +1 -1
  45. package/dist/edge-node/edge-join.js +1 -1
  46. package/dist/edge-node/edge-node.js +2 -2
  47. package/dist/edge-node/edge-node.js.map +1 -1
  48. package/dist/edge-node/index.d.ts +1 -0
  49. package/dist/edge-node/index.js +1 -0
  50. package/dist/edge-node/index.js.map +1 -1
  51. package/dist/edge-node/join-credential.d.ts +1 -1
  52. package/dist/edge-node/join-credential.js +1 -1
  53. package/dist/edge-node/lazy-raw-storage.js +12 -0
  54. package/dist/edge-node/lazy-raw-storage.js.map +1 -1
  55. package/dist/edge-node/listen-addrs.d.ts +21 -0
  56. package/dist/edge-node/listen-addrs.js +25 -0
  57. package/dist/edge-node/listen-addrs.js.map +1 -0
  58. package/dist/edge-node/owner-grant.d.ts +1 -1
  59. package/dist/edge-node/owner-grant.js +1 -1
  60. package/dist/edge-node/platform.d.ts +5 -4
  61. package/dist/edge-node/platform.js.map +1 -1
  62. package/dist/edge-node/strand-move.d.ts +1 -1
  63. package/dist/edge-node/strand-move.js +1 -1
  64. package/dist/edge-node/strands.js +27 -11
  65. package/dist/edge-node/strands.js.map +1 -1
  66. package/dist/index.d.ts +7 -5
  67. package/dist/index.js +18 -7
  68. package/dist/index.js.map +1 -1
  69. package/dist/invitation-bootstrap.d.ts +28 -5
  70. package/dist/invitation-bootstrap.js +41 -8
  71. package/dist/invitation-bootstrap.js.map +1 -1
  72. package/dist/link-budget.d.ts +53 -4
  73. package/dist/link-budget.js +58 -5
  74. package/dist/link-budget.js.map +1 -1
  75. package/dist/machine-removal.d.ts +2 -2
  76. package/dist/machine-removal.js +4 -3
  77. package/dist/machine-removal.js.map +1 -1
  78. package/dist/membership-connection-gater.d.ts +2 -2
  79. package/dist/membership-connection-gater.js +2 -2
  80. package/dist/membership-view.d.ts +2 -2
  81. package/dist/membership-view.js +2 -2
  82. package/dist/node-claim-payload.d.ts +15 -7
  83. package/dist/node-claim-payload.js +25 -9
  84. package/dist/node-claim-payload.js.map +1 -1
  85. package/dist/node-class.d.ts +36 -0
  86. package/dist/node-class.js +26 -0
  87. package/dist/node-class.js.map +1 -0
  88. package/dist/node-default-transports.d.ts +50 -3
  89. package/dist/node-default-transports.js +48 -3
  90. package/dist/node-default-transports.js.map +1 -1
  91. package/dist/node-roles.d.ts +118 -0
  92. package/dist/node-roles.js +142 -0
  93. package/dist/node-roles.js.map +1 -0
  94. package/dist/node-transports.d.ts +6 -2
  95. package/dist/node-transports.js +39 -7
  96. package/dist/node-transports.js.map +1 -1
  97. package/dist/owner-anchor-sync.js +1 -1
  98. package/dist/owner-key-export.js +1 -1
  99. package/dist/owner-key-flows.d.ts +1 -1
  100. package/dist/owner-key-store.js +1 -1
  101. package/dist/owner-signer.js +1 -1
  102. package/dist/peer-authorization.d.ts +3 -1
  103. package/dist/peer-authorization.js +3 -1
  104. package/dist/peer-authorization.js.map +1 -1
  105. package/dist/peer-dial.d.ts +74 -10
  106. package/dist/peer-dial.js +89 -17
  107. package/dist/peer-dial.js.map +1 -1
  108. package/dist/peer-join-backfill.js +4 -4
  109. package/dist/peer-join-backfill.js.map +1 -1
  110. package/dist/peer-record.d.ts +34 -9
  111. package/dist/peer-record.js +66 -13
  112. package/dist/peer-record.js.map +1 -1
  113. package/dist/pending-join-runner.d.ts +1 -1
  114. package/dist/pending-join-runner.js +1 -1
  115. package/dist/provider/client.d.ts +6 -5
  116. package/dist/provider/client.js +3 -3
  117. package/dist/provider/client.js.map +1 -1
  118. package/dist/provider/descriptor.d.ts +1 -1
  119. package/dist/provider/descriptor.js +1 -1
  120. package/dist/provider/oauth.d.ts +1 -1
  121. package/dist/provider/oauth.js +1 -1
  122. package/dist/provider/rent-node.d.ts +4 -3
  123. package/dist/provider/rent-node.js +3 -3
  124. package/dist/provider/rent-node.js.map +1 -1
  125. package/dist/relay-addrs.d.ts +62 -7
  126. package/dist/relay-addrs.js +74 -11
  127. package/dist/relay-addrs.js.map +1 -1
  128. package/dist/relay-reservation.d.ts +1 -1
  129. package/dist/relay-reservation.js +1 -1
  130. package/dist/relay-server.d.ts +6 -5
  131. package/dist/relay-server.js +5 -5
  132. package/dist/relay-server.js.map +1 -1
  133. package/dist/schema-verification.d.ts +13 -10
  134. package/dist/schema-verification.js +32 -24
  135. package/dist/schema-verification.js.map +1 -1
  136. package/dist/sealed-party-key.js +1 -1
  137. package/dist/seed-bootstrap.d.ts +4 -3
  138. package/dist/seed-bootstrap.js +6 -7
  139. package/dist/seed-bootstrap.js.map +1 -1
  140. package/dist/strand-addr-protocol.d.ts +8 -6
  141. package/dist/strand-addr-protocol.js +12 -10
  142. package/dist/strand-addr-protocol.js.map +1 -1
  143. package/dist/strand-app-schema-report.d.ts +16 -2
  144. package/dist/strand-app-schema-report.js +15 -0
  145. package/dist/strand-app-schema-report.js.map +1 -1
  146. package/dist/strand-cohort.d.ts +1 -4
  147. package/dist/strand-cohort.js.map +1 -1
  148. package/dist/strand-commit-durability.d.ts +1 -1
  149. package/dist/strand-database.d.ts +13 -6
  150. package/dist/strand-database.js +17 -9
  151. package/dist/strand-database.js.map +1 -1
  152. package/dist/strand-first-sync-gate.d.ts +3 -2
  153. package/dist/strand-first-sync-gate.js +3 -2
  154. package/dist/strand-first-sync-gate.js.map +1 -1
  155. package/dist/strand-formation-deadlines.d.ts +2 -2
  156. package/dist/strand-formation-deadlines.js +2 -2
  157. package/dist/strand-formation-protocol.d.ts +1 -1
  158. package/dist/strand-formation-protocol.js +51 -16
  159. package/dist/strand-formation-protocol.js.map +1 -1
  160. package/dist/strand-instance-manager.d.ts +67 -33
  161. package/dist/strand-instance-manager.js +164 -55
  162. package/dist/strand-instance-manager.js.map +1 -1
  163. package/dist/strand-membership-reconciler.d.ts +8 -3
  164. package/dist/strand-membership-reconciler.js +8 -3
  165. package/dist/strand-membership-reconciler.js.map +1 -1
  166. package/dist/strand-membership-writer.d.ts +2 -2
  167. package/dist/strand-membership-writer.js +41 -39
  168. package/dist/strand-membership-writer.js.map +1 -1
  169. package/dist/strand-move.js +1 -1
  170. package/dist/strand-network-config.d.ts +15 -5
  171. package/dist/strand-network-config.js +7 -6
  172. package/dist/strand-network-config.js.map +1 -1
  173. package/dist/strand-party-key-rotation.d.ts +7 -3
  174. package/dist/strand-party-key-rotation.js +7 -3
  175. package/dist/strand-party-key-rotation.js.map +1 -1
  176. package/dist/strand-party-machines.d.ts +19 -8
  177. package/dist/strand-party-machines.js +26 -11
  178. package/dist/strand-party-machines.js.map +1 -1
  179. package/dist/strand-party-peers.d.ts +36 -14
  180. package/dist/strand-party-peers.js +24 -15
  181. package/dist/strand-party-peers.js.map +1 -1
  182. package/dist/strand-peer-redial.d.ts +1 -1
  183. package/dist/strand-peer-redial.js +1 -1
  184. package/dist/strand-reactivity.d.ts +20 -17
  185. package/dist/strand-reactivity.js +31 -18
  186. package/dist/strand-reactivity.js.map +1 -1
  187. package/dist/strand-revocation-enforcer.d.ts +3 -3
  188. package/dist/strand-revocation-enforcer.js +3 -3
  189. package/dist/strand-wake-protocol.d.ts +19 -7
  190. package/dist/strand-wake-protocol.js +22 -10
  191. package/dist/strand-wake-protocol.js.map +1 -1
  192. package/dist/strand-watcher.d.ts +1 -1
  193. package/dist/strand-watcher.js +1 -1
  194. package/dist/table-change-watch.js +1 -1
  195. package/dist/types.d.ts +116 -55
  196. package/dist/types.js.map +1 -1
  197. package/dist/webrtc-upgrade.d.ts +254 -0
  198. package/dist/webrtc-upgrade.js +355 -0
  199. package/dist/webrtc-upgrade.js.map +1 -0
  200. package/package.json +9 -9
  201. package/src/announce-addrs.ts +93 -93
  202. package/src/arachnode-stub.ts +18 -10
  203. package/src/bootstrap-peer-store-file.ts +44 -44
  204. package/src/bootstrap-peer-store.ts +214 -214
  205. package/src/cadre-invite-protocol.ts +741 -740
  206. package/src/cadre-node.ts +12462 -12280
  207. package/src/claim-proof.ts +147 -147
  208. package/src/control-authorization.ts +290 -276
  209. package/src/control-cohort.ts +223 -204
  210. package/src/control-database.ts +72 -28
  211. package/src/control-formation-recorder.ts +359 -359
  212. package/src/control-protocol-guard.ts +370 -366
  213. package/src/control-read-retry.ts +222 -180
  214. package/src/control-retry.ts +36 -4
  215. package/src/control-schema-version.ts +301 -301
  216. package/src/control-schema.ts +1108 -1087
  217. package/src/control-write-retry.ts +639 -669
  218. package/src/delegate-admission.ts +351 -351
  219. package/src/describe-thrown.ts +60 -60
  220. package/src/ed25519-key.ts +185 -185
  221. package/src/edge-node/config.ts +13 -10
  222. package/src/edge-node/dissolve.ts +252 -252
  223. package/src/edge-node/edge-join.ts +320 -320
  224. package/src/edge-node/edge-node.ts +1095 -1095
  225. package/src/edge-node/index.ts +60 -59
  226. package/src/edge-node/join-credential.ts +16 -16
  227. package/src/edge-node/lazy-raw-storage.ts +194 -178
  228. package/src/edge-node/listen-addrs.ts +26 -0
  229. package/src/edge-node/node-local.ts +84 -84
  230. package/src/edge-node/options.ts +195 -195
  231. package/src/edge-node/owner-grant.ts +63 -63
  232. package/src/edge-node/platform.ts +5 -4
  233. package/src/edge-node/strand-move.ts +266 -266
  234. package/src/edge-node/strands.ts +27 -12
  235. package/src/enrolled-machine-store-file.ts +43 -43
  236. package/src/enrolled-machine-store.ts +280 -280
  237. package/src/fetch-impl.ts +23 -23
  238. package/src/file-durable-slot.ts +64 -64
  239. package/src/file-safe-name.ts +32 -32
  240. package/src/formation-approval.ts +604 -604
  241. package/src/formation-token.ts +63 -63
  242. package/src/fs-atomic.ts +92 -92
  243. package/src/index.ts +987 -957
  244. package/src/invitation-bootstrap.ts +152 -118
  245. package/src/joined-strand-store.ts +553 -553
  246. package/src/key-store-file.ts +142 -142
  247. package/src/link-budget.ts +61 -5
  248. package/src/machine-removal.ts +211 -210
  249. package/src/member-redemption.ts +643 -643
  250. package/src/membership-connection-gater.ts +650 -650
  251. package/src/membership-view.ts +147 -147
  252. package/src/node-claim-payload.ts +34 -12
  253. package/src/node-class.ts +47 -0
  254. package/src/node-default-transports.ts +65 -3
  255. package/src/node-roles.ts +220 -0
  256. package/src/node-transports.ts +86 -49
  257. package/src/owner-anchor-sync.ts +172 -172
  258. package/src/owner-grant-protocol.ts +547 -547
  259. package/src/owner-key-export.ts +348 -348
  260. package/src/owner-key-flows.ts +109 -109
  261. package/src/owner-key-store.ts +1 -1
  262. package/src/owner-keys.ts +179 -179
  263. package/src/owner-signer.ts +92 -92
  264. package/src/peer-addr-book.ts +252 -252
  265. package/src/peer-authorization.ts +584 -582
  266. package/src/peer-dial.ts +698 -605
  267. package/src/peer-join-backfill.ts +961 -961
  268. package/src/peer-record.ts +71 -16
  269. package/src/pending-join-runner.ts +798 -798
  270. package/src/primary-lan-address.ts +47 -47
  271. package/src/provider/client.ts +327 -326
  272. package/src/provider/descriptor.ts +129 -129
  273. package/src/provider/index.ts +59 -59
  274. package/src/provider/oauth.ts +285 -285
  275. package/src/provider/rent-node.ts +276 -275
  276. package/src/relay-addrs.ts +90 -12
  277. package/src/relay-reservation.ts +1 -1
  278. package/src/relay-server.ts +99 -98
  279. package/src/schema-verification.ts +38 -23
  280. package/src/sealed-party-key.ts +190 -190
  281. package/src/seed-bootstrap.ts +8 -11
  282. package/src/seed-trust-policy.ts +353 -353
  283. package/src/self-address-update.ts +19 -19
  284. package/src/strand-addr-protocol.ts +17 -14
  285. package/src/strand-app-schema-report.ts +147 -122
  286. package/src/strand-cohort.ts +1 -5
  287. package/src/strand-commit-durability.ts +165 -165
  288. package/src/strand-database.ts +522 -510
  289. package/src/strand-first-sync-gate.ts +3 -2
  290. package/src/strand-formation-deadlines.ts +227 -227
  291. package/src/strand-formation-manager.ts +731 -731
  292. package/src/strand-formation-protocol.ts +1174 -1138
  293. package/src/strand-fret-addrs.ts +179 -179
  294. package/src/strand-instance-manager.ts +188 -63
  295. package/src/strand-membership-reconciler.ts +1315 -1310
  296. package/src/strand-membership-writer.ts +2152 -2150
  297. package/src/strand-move.ts +1 -1
  298. package/src/strand-network-config.ts +16 -6
  299. package/src/strand-offline.ts +178 -178
  300. package/src/strand-party-key-rotation.ts +440 -436
  301. package/src/strand-party-machines.ts +308 -293
  302. package/src/strand-party-peers.ts +151 -129
  303. package/src/strand-peer-redial.ts +1 -1
  304. package/src/strand-reactivity.ts +36 -19
  305. package/src/strand-revocation-enforcer.ts +758 -758
  306. package/src/strand-wake-protocol.ts +406 -394
  307. package/src/strand-watcher.ts +1 -1
  308. package/src/table-change-watch.ts +285 -285
  309. package/src/timed-step.ts +28 -28
  310. package/src/timeout-scheduler.ts +29 -29
  311. package/src/trusted-owner-store.ts +242 -242
  312. package/src/types.ts +3182 -3121
  313. package/src/webrtc-upgrade.ts +439 -0
@@ -1,180 +1,222 @@
1
- import { BlockUnavailableError } from '@optimystic/db-core';
2
- import { causeChain, chainMessages, reportsUnreachableCohort, retryControlOperation } from './control-retry.js';
3
- import type { ControlRetryOptions } from './control-retry.js';
4
- import { isUncommittedTransactorAggregate } from './control-write-retry.js';
5
-
6
- /**
7
- * Bounded retry for TRANSIENT control-read failures — the classifier and policy behind
8
- * `ControlDatabase.readRows`, the single funnel every control read passes through.
9
- *
10
- * Why this exists: control WRITES have absorbed transient cluster failures since
11
- * `control-write-retry.ts` landed, but a read had no second chance — one stream reset
12
- * during a scan ended the read outright (measured: a rigged single `eval` failure killed
13
- * `queryRevokedStamps` after exactly one attempt while the same injection against `exec`
14
- * was absorbed). This module gives reads the same bounded re-presentation with a SHORTER
15
- * deadline and a NARROWER failure set, because the tightest caller deadline over a control
16
- * read is the inbound admission gate's 2 s fail-open timeout
17
- * (`ADMISSION_DECISION_TIMEOUT_MS`, `link-budget.ts`) — a read budget that
18
- * does not fit inside it with headroom spends its retries after the gate has already
19
- * admitted.
20
- *
21
- * This is deliberately NOT "the write classifier minus its commit veto" — the retriable
22
- * set differs in both directions (see {@link isRetriableControlReadFailure}). The loop
23
- * lives in `control-retry.ts`, shared with the write policy; this module owns only the
24
- * read policy. A block-unavailability failure is matched by TYPE: `OptimysticVirtualTable`
25
- * rethrows a scan failure through `rewrapAsQueryError`
26
- * (`quereus-plugin-optimystic/src/optimystic-module.ts`), which keeps the typed
27
- * `BlockUnavailableError` on `cause`, and Quereus wraps that as
28
- * `Error during query on table '<T>': …`, again preserving `cause`
29
- * (`quereus/src/runtime/emit/scan.ts`). The transactor's aggregate is matched by TEXT, since
30
- * upstream raises it as a plain `Error`. Every matcher fails CLOSED — an upstream rewording or
31
- * a lost `cause` stops the retry engaging, it never makes it unsafe.
32
- */
33
-
34
- /**
35
- * Attempts allowed per control read: the first, plus two retries. Attempts are cheap here —
36
- * the observed transient failure (the transactor's read-phase aggregate off a stream still
37
- * forming) surfaces in ~25 ms — and {@link CONTROL_READ_RETRY_BUDGET_MS} is what terminates
38
- * the loop when an attempt is slow instead.
39
- */
40
- export const CONTROL_READ_ATTEMPTS = 3;
41
-
42
- /**
43
- * Backoff before read retry N, jittered ±50% and capped at the largest base by the shared
44
- * loop — so worst case 150 ms + 400 ms = 550 ms of total sleep (the second delay's
45
- * upside jitter is clipped by the cap). Much shorter than the write list because the
46
- * whole loop must fit under {@link CONTROL_READ_RETRY_BUDGET_MS}; the spec pins that
47
- * relationship rather than the number.
48
- */
49
- export const CONTROL_READ_RETRY_DELAYS_MS: readonly number[] = [100, 400];
50
-
51
- /**
52
- * Elapsed-time ceiling on the whole read-retry loop, checked after a failed attempt BEFORE
53
- * sleeping — so one slow attempt (e.g. a `cohort-unreachable` read that burned the
54
- * transactor's own deadline) terminates the loop rather than compounding.
55
- *
56
- * 1500 ms, sized against `ADMISSION_DECISION_TIMEOUT_MS` (2000 ms): the inbound admission
57
- * gate reads the control DB and is FAIL-OPEN on both throw and timeout, so a read that
58
- * outlives the gate's deadline spends its retries after the gate has already admitted an
59
- * unplaced peer. Under the deadline, a read that succeeds on its second attempt lets the
60
- * gate make the real decision instead. `control-read-retry.spec.ts` asserts the
61
- * relationship so a future edit to either constant reddens instead of silently
62
- * reintroducing the fail-open admit. NOTE: `queryCadrePeers` / `queryPeerRecord` each
63
- * issue TWO network reads (the revoked-stamp filter plus the row scan) and the budget is
64
- * per read, so a membership read can spend up to two budgets back to back — still inside
65
- * 2 s only because each stays well under it. Do not raise these numbers without
66
- * re-checking against `ADMISSION_DECISION_TIMEOUT_MS`.
67
- *
68
- * It cuts the loop off by design, so it does not grow with the link or with the number that
69
- * dominates a slow attempt: an attempt whose cohort consult finds a silent peer costs
70
- * `clusterPolicy.cohortQueryTimeoutMs`, two link round trips at the declared link
71
- * (`cohortReadDeadlineMs` in `link-budget.ts`, 7 000 ms at the default declaration; the plugin's
72
- * `COHORT_READ_DEADLINE_MS` is the same number). That is more than four times this budget, so
73
- * such an attempt is never retried and the caller gets its error unchanged. That is deliberate:
74
- * the caller this budget is sized for, the admission gate, has
75
- * already taken its fail-open answer by then, and a retry would only spend time after it. What
76
- * the budget still buys is the fast failures it was built for: the ~25 ms transactor read-phase
77
- * aggregate off a stream still forming, and a `cohort-unreachable` read during bring-up, which
78
- * fails fast because there is no connection to ask. Both retry twice well inside 1500 ms. See
79
- * `docs/cadre-consistency.md` → "Deadlines Over Optimystic's Reads and Commits".
80
- */
81
- // eslint-disable-next-line no-restricted-syntax -- cuts off by design: a slow attempt is not retried, because the admission gate this budget is sized for has already failed open by then; see docs/cadre-consistency.md → "Deadlines Over Optimystic's Reads and Commits"
82
- export const CONTROL_READ_RETRY_BUDGET_MS = 1_500;
83
-
84
- /**
85
- * Pacing overrides for {@link retryControlRead} — the read twin of
86
- * `ControlWriteRetryOptions`. Every field defaults to the shipped read policy
87
- * ({@link CONTROL_READ_ATTEMPTS}, {@link CONTROL_READ_RETRY_DELAYS_MS},
88
- * {@link isRetriableControlReadFailure}); field shapes and semantics live on the shared
89
- * {@link ControlRetryOptions}.
90
- */
91
- export type ControlReadRetryOptions = ControlRetryOptions;
92
-
93
- /**
94
- * Did this control read fail in a way a repeat read can improve on?
95
- *
96
- * Two classes, both meaning "the cluster could not be ASKED properly just now":
97
- *
98
- * - the transactor's read-phase aggregate (`Some peers did not complete: …[block:…]`),
99
- * reused verbatim from the write classifier — a `get` batch formats the single-block
100
- * `[block:` token, and a read can never produce the commit-phase `[blocks:` token, so
101
- * the write side's indeterminate-commit veto has nothing to veto here (a read commits
102
- * nothing and re-running it is always safe);
103
- * - a block read the cohort could not answer ({@link reportsUnreachableCohort}, the
104
- * predicate the write classifier shares — see it for the reasons and the measured
105
- * exclusions).
106
- *
107
- * The aggregate is matched by message at any depth of the `cause` chain (`chainMessages`),
108
- * the unavailability by type on the same chain. Anything that is not an `Error` is never
109
- * retried, and anything unmatched is not retried either.
110
- */
111
- export function isRetriableControlReadFailure(error: unknown): boolean {
112
- if (!(error instanceof Error)) {
113
- return false;
114
- }
115
- return reportsUnreachableCohort(causeChain(error))
116
- || chainMessages(error).some(isUncommittedTransactorAggregate);
117
- }
118
-
119
- /**
120
- * Did this control read fail because no cohort member other than the answering node could
121
- * be asked about a block nobody here holds — a `BlockUnavailableError` with reason
122
- * `cohort-unreachable` anywhere on the `cause` chain?
123
- *
124
- * Not a retry classifier. It answers "is there a better answer to wait for?", and is used
125
- * by exactly one reader, `ControlDatabase.queryRevokedStamps`, to read an isolated node's
126
- * never-received `Revocation` block as "no revocations known" (the reasoning is in the NOTE
127
- * there). Upstream names this the one reason a caller may treat permissively
128
- * (`db-core/src/transactor/network-transactor.ts`). The other reasons do NOT match:
129
- * `peers-unreachable` means part of the cohort answered (or no coordinator did), and
130
- * `claimed-elsewhere` / `unmaterializable` mean the block is known to exist, so an empty
131
- * answer could hide a real revocation. NOTE: narrower than upstream's same-named
132
- * `isCohortUnreachableRead` (`@optimystic/db-core`), which also matches `peers-unreachable`
133
- * and is what the retry classifiers use ({@link reportsUnreachableCohort}); do not swap one
134
- * for the other.
135
- *
136
- * Matched by TYPE, since the typed error survives upstream's rewraps (module comment).
137
- * Fails CLOSED: a non-`Error`, a text-only copy of the message, and an error built by a
138
- * second loaded copy of `@optimystic/db-core` (which `instanceof` cannot recognise) all
139
- * answer false, and the reader throws as it did before this existed.
140
- */
141
- export function isCohortUnreachableRead(error: unknown): boolean {
142
- return causeChain(error).some(link =>
143
- link instanceof BlockUnavailableError && link.reason === 'cohort-unreachable');
144
- }
145
-
146
- /**
147
- * Run `attempt` — a full drain of one control read — up to
148
- * {@link CONTROL_READ_ATTEMPTS} times, retrying only failures
149
- * {@link isRetriableControlReadFailure} calls transient, and only while
150
- * {@link CONTROL_READ_RETRY_BUDGET_MS} has not elapsed.
151
- *
152
- * Reads are idempotent, so `attempt` needs no atomicity contract — but it must be a
153
- * COLLECTING drain, not a live iterator: a half-consumed iterator cannot be retried
154
- * without re-yielding rows the caller already saw, which is why
155
- * `ControlDatabase.readRows` materializes before this loop ever sees a failure.
156
- *
157
- * On exhaustion the LAST error is rethrown unchanged. Log lines carry the
158
- * `Control read [<label>] …` prefix so a read's retries can be attributed among the
159
- * several reads in flight concurrently in a real party.
160
- *
161
- * No reader passes an `onAbandon` observer, deliberately, though the shared options type
162
- * offers one ({@link ControlRetryOptions.onAbandon}) and the loop would honour it. That hook
163
- * exists because an abandoned WRITE can have no caller at all — the background self-record
164
- * republish is `void`-ed with a `debug`-only catch, so giving up on it is invisible. Every
165
- * control read is awaited by the caller that issued it, so an abandoned read surfaces as that
166
- * caller's rejection; there is nothing here to lose silently. A fire-and-forget read would be
167
- * the reason to start passing one.
168
- */
169
- export function retryControlRead<T>(
170
- attempt: () => Promise<T>,
171
- options: ControlReadRetryOptions = {}
172
- ): Promise<T> {
173
- return retryControlOperation(attempt, {
174
- attempts: CONTROL_READ_ATTEMPTS,
175
- delaysMs: CONTROL_READ_RETRY_DELAYS_MS,
176
- budgetMs: CONTROL_READ_RETRY_BUDGET_MS,
177
- isRetriable: isRetriableControlReadFailure,
178
- logPrefix: 'Control read',
179
- }, options);
180
- }
1
+ import { BlockUnavailableError } from '@optimystic/db-core';
2
+ import { FindCoordinatorError } from '@optimystic/db-p2p';
3
+ import { causeChain, reportsUnreachableCohort, retryControlOperation } from './control-retry.js';
4
+ import type { ControlRetryOptions } from './control-retry.js';
5
+ import { isUncommittedTransactorAggregate } from './control-write-retry.js';
6
+
7
+ /**
8
+ * Bounded retry for TRANSIENT control-read failures — the classifier and policy behind
9
+ * `ControlDatabase.readRows`, the single funnel every control read passes through.
10
+ *
11
+ * Why this exists: control WRITES have absorbed transient cluster failures since
12
+ * `control-write-retry.ts` landed, but a read had no second chance — one stream reset
13
+ * during a scan ended the read outright (measured: a rigged single `eval` failure killed
14
+ * `queryRevokedStamps` after exactly one attempt while the same injection against `exec`
15
+ * was absorbed). This module gives reads the same bounded re-presentation with a SHORTER
16
+ * deadline and a NARROWER failure set, because the tightest caller deadline over a control
17
+ * read is the inbound admission gate's 2 s fail-open timeout
18
+ * (`ADMISSION_DECISION_TIMEOUT_MS`, `link-budget.ts`) — a read budget that
19
+ * does not fit inside it with headroom spends its retries after the gate has already
20
+ * admitted.
21
+ *
22
+ * This is deliberately NOT "the write classifier minus its commit veto" — the retriable
23
+ * set differs in both directions (see {@link isRetriableControlReadFailure}). The loop
24
+ * lives in `control-retry.ts`, shared with the write policy; this module owns only the
25
+ * read policy. A block-unavailability failure is matched by TYPE: `OptimysticVirtualTable`
26
+ * rethrows a scan failure through `rewrapAsQueryError`
27
+ * (`quereus-plugin-optimystic/src/optimystic-module.ts`), which keeps the typed
28
+ * `BlockUnavailableError` on `cause`, and Quereus wraps that as
29
+ * `Error during query on table '<T>': …`, again preserving `cause`
30
+ * (`quereus/src/runtime/emit/scan.ts`). The transactor's aggregate is matched by type too — it
31
+ * arrives as a `PeersIncompleteError` naming the phase it came from. Both matchers fail CLOSED:
32
+ * a lost `cause` stops the retry engaging, it never makes it unsafe.
33
+ */
34
+
35
+ /**
36
+ * Attempts allowed per control read: the first, plus two retries. Attempts are cheap here —
37
+ * the observed transient failure (the transactor's read-phase aggregate off a stream still
38
+ * forming) surfaces in ~25 ms — and {@link CONTROL_READ_RETRY_BUDGET_MS} is what terminates
39
+ * the loop when an attempt is slow instead.
40
+ */
41
+ export const CONTROL_READ_ATTEMPTS = 3;
42
+
43
+ /**
44
+ * Backoff before read retry N, jittered ±50% and capped at the largest base by the shared
45
+ * loop — so worst case 150 ms + 400 ms = 550 ms of total sleep (the second delay's
46
+ * upside jitter is clipped by the cap). Much shorter than the write list because the
47
+ * whole loop must fit under {@link CONTROL_READ_RETRY_BUDGET_MS}; the spec pins that
48
+ * relationship rather than the number.
49
+ */
50
+ export const CONTROL_READ_RETRY_DELAYS_MS: readonly number[] = [100, 400];
51
+
52
+ /**
53
+ * Elapsed-time ceiling on the whole read-retry loop, checked after a failed attempt BEFORE
54
+ * sleeping — so one slow attempt (e.g. a `cohort-unreachable` read that burned the
55
+ * transactor's own deadline) terminates the loop rather than compounding.
56
+ *
57
+ * 1500 ms, sized against `ADMISSION_DECISION_TIMEOUT_MS` (2000 ms): the inbound admission
58
+ * gate reads the control DB and is FAIL-OPEN on both throw and timeout, so a read that
59
+ * outlives the gate's deadline spends its retries after the gate has already admitted an
60
+ * unplaced peer. Under the deadline, a read that succeeds on its second attempt lets the
61
+ * gate make the real decision instead. `control-read-retry.spec.ts` asserts the
62
+ * relationship so a future edit to either constant reddens instead of silently
63
+ * reintroducing the fail-open admit. NOTE: `queryCadrePeers` / `queryPeerRecord` each
64
+ * issue TWO network reads (the revoked-stamp filter plus the row scan) and the budget is
65
+ * per read, so a membership read can spend up to two budgets back to back — still inside
66
+ * 2 s only because each stays well under it. Do not raise these numbers without
67
+ * re-checking against `ADMISSION_DECISION_TIMEOUT_MS`.
68
+ *
69
+ * It cuts the loop off by design, so it does not grow with the link or with the number that
70
+ * dominates a slow attempt: an attempt whose cohort consult finds a silent peer costs
71
+ * `clusterPolicy.cohortQueryTimeoutMs`, two link round trips at the declared link
72
+ * (`cohortReadDeadlineMs` in `link-budget.ts`, 7 000 ms at the default declaration; the plugin's
73
+ * `COHORT_READ_DEADLINE_MS` is the same number). That is more than four times this budget, so
74
+ * such an attempt is never retried and the caller gets its error unchanged. That is deliberate:
75
+ * the caller this budget is sized for, the admission gate, has
76
+ * already taken its fail-open answer by then, and a retry would only spend time after it. What
77
+ * the budget still buys is the fast failures it was built for: the ~25 ms transactor read-phase
78
+ * aggregate off a stream still forming, and a `cohort-unreachable` read during bring-up, which
79
+ * fails fast because there is no connection to ask. Both retry twice well inside 1500 ms. See
80
+ * `docs/network/deadlines.md` → "Deadlines over Optimystic's reads and commits".
81
+ */
82
+ // eslint-disable-next-line no-restricted-syntax -- cuts off by design: a slow attempt is not retried, because the admission gate this budget is sized for has already failed open by then; see docs/network/deadlines.md → "Deadlines over Optimystic's reads and commits"
83
+ export const CONTROL_READ_RETRY_BUDGET_MS = 1_500;
84
+
85
+ /**
86
+ * Pacing overrides for {@link retryControlRead} — the read twin of
87
+ * `ControlWriteRetryOptions`. Every field defaults to the shipped read policy
88
+ * ({@link CONTROL_READ_ATTEMPTS}, {@link CONTROL_READ_RETRY_DELAYS_MS},
89
+ * {@link isRetriableControlReadFailure}); field shapes and semantics live on the shared
90
+ * {@link ControlRetryOptions}.
91
+ */
92
+ export type ControlReadRetryOptions = ControlRetryOptions;
93
+
94
+ /**
95
+ * Did this control read fail in a way a repeat read can improve on?
96
+ *
97
+ * Two classes, both meaning "the cluster could not be ASKED properly just now":
98
+ *
99
+ * - the transactor's read-phase aggregate ({@link isUncommittedTransactorAggregate}),
100
+ * reused verbatim from the write classifier — including its `pend` arm, which a read
101
+ * cannot produce at all because it presents no transaction, so claiming that phase here
102
+ * is unreachable rather than unsafe and one shared predicate is one thing to keep
103
+ * correct. The write side's indeterminate-commit veto likewise has nothing to veto here
104
+ * (a read commits nothing and re-running it is always safe);
105
+ * - a block read the cohort could not answer ({@link reportsUnreachableCohort}, the
106
+ * predicate the write classifier shares — see it for the reasons and the measured
107
+ * exclusions).
108
+ *
109
+ * Both are matched by TYPE, over one walk of the `cause` chain. Anything that is not an
110
+ * `Error` is never retried, and anything unmatched is not retried either.
111
+ */
112
+ export function isRetriableControlReadFailure(error: unknown): boolean {
113
+ if (!(error instanceof Error)) {
114
+ return false;
115
+ }
116
+ const links = causeChain(error);
117
+ return reportsUnreachableCohort(links) || isUncommittedTransactorAggregate(links);
118
+ }
119
+
120
+ /**
121
+ * Did this control read fail because no cohort member other than the answering node could
122
+ * be asked about a block nobody here holds — a `BlockUnavailableError` with reason
123
+ * `cohort-unreachable` anywhere on the `cause` chain?
124
+ *
125
+ * Not a retry classifier. It answers "is there a better answer to wait for?", and is used
126
+ * by exactly one reader, `ControlDatabase.queryRevokedStamps`, to read an isolated node's
127
+ * never-received `Revocation` block as "no revocations known" (the reasoning is in the NOTE
128
+ * there). Upstream names this the one reason a caller may treat permissively
129
+ * (`db-core/src/transactor/network-transactor.ts`). The other reasons do NOT match:
130
+ * `peers-unreachable` means part of the cohort answered (or no coordinator did), and
131
+ * `claimed-elsewhere` / `unmaterializable` mean the block is known to exist, so an empty
132
+ * answer could hide a real revocation. NOTE: narrower than upstream's same-named
133
+ * `isCohortUnreachableRead` (`@optimystic/db-core`), which also matches `peers-unreachable`
134
+ * and is what the retry classifiers use ({@link reportsUnreachableCohort}); do not swap one
135
+ * for the other.
136
+ *
137
+ * Matched by TYPE, since the typed error survives upstream's rewraps (module comment).
138
+ * Fails CLOSED: a non-`Error`, a text-only copy of the message, and an error built by a
139
+ * second loaded copy of `@optimystic/db-core` (which `instanceof` cannot recognise) all
140
+ * answer false, and the reader throws as it did before this existed.
141
+ */
142
+ export function isCohortUnreachableRead(error: unknown): boolean {
143
+ return causeChain(error).some(link =>
144
+ link instanceof BlockUnavailableError && link.reason === 'cohort-unreachable');
145
+ }
146
+
147
+ /**
148
+ * Did this failure mean no coordinator could be resolved for a block AT ALL — a
149
+ * `FindCoordinatorError` anywhere on the `cause` chain?
150
+ *
151
+ * The sibling of {@link isCohortUnreachableRead} for the failure shape a machine that stores
152
+ * none of a strand's blocks produces. Such a node's `findCluster` returns an EMPTY cohort, so
153
+ * every block falls through to `findCoordinator`, which waits out its bounded retry window and
154
+ * throws `FindCoordinatorError` — code `NO_NETWORK_COORDINATOR` when the only peers it saw were
155
+ * unconfirmed or serve another network, `NO_COORDINATOR_AVAILABLE` otherwise.
156
+ *
157
+ * Matched by CLASS, so all four of `FIND_COORDINATOR_ERROR_CODES` match, not only the two a
158
+ * client produces. That is the intent, not laxity: every code means "no coordinator was
159
+ * resolved", NONE of them means "the block does not exist", and the one reader
160
+ * (`StrandInstanceManager.buildStrandDatabase`) waits for a machine that can answer in every
161
+ * case. The two self-coordination codes reach a Sereus node only from a detected partition or
162
+ * suspicious cohort shrinkage — Sereus never switches self-coordination off by config — and
163
+ * waiting is the right answer there too.
164
+ *
165
+ * Not a retry classifier, and deliberately NOT folded into {@link isCohortUnreachableRead}:
166
+ * that predicate's other reader, `ControlDatabase.queryRevokedStamps`, reads an unmatched block
167
+ * as "no revocations known", so admitting a new class of failure there would widen a
168
+ * security-relevant default. One predicate per reader is what keeps that default where it was.
169
+ *
170
+ * The walk is over the same {@link causeChain}, because `StrandDatabase.initialize` runs the
171
+ * failure through Quereus's `db.exec`, which may wrap it. Fails CLOSED the same way: a
172
+ * non-`Error`, a text-only copy of the message, and an error built by a second loaded copy of
173
+ * `@optimystic/db-p2p` (which `instanceof` cannot recognise) all answer false, and the caller
174
+ * throws as it did before this existed.
175
+ *
176
+ * NOTE: matching the class rather than `.code` is the deliberate call, and the one code it
177
+ * admits against its own upstream advice is `SELF_COORDINATION_BLOCKED`, whose comment says
178
+ * retrying is unlikely to help. It costs a launch that hits it a `'syncing'` runtime retrying
179
+ * every 5 s instead of a watcher rebuild on a doubling delay, and only a SERVING node can hit
180
+ * it at all (a client is never a candidate coordinator, so it gets `NO_COORDINATOR_AVAILABLE`).
181
+ * Narrow this to `.code` if a config switch for self-coordination is ever passed from Sereus, or
182
+ * if a partition verdict is ever made from something that does not heal on its own.
183
+ */
184
+ export function isNoCoordinatorResolved(error: unknown): boolean {
185
+ return causeChain(error).some(link => link instanceof FindCoordinatorError);
186
+ }
187
+
188
+ /**
189
+ * Run `attempt` — a full drain of one control read — up to
190
+ * {@link CONTROL_READ_ATTEMPTS} times, retrying only failures
191
+ * {@link isRetriableControlReadFailure} calls transient, and only while
192
+ * {@link CONTROL_READ_RETRY_BUDGET_MS} has not elapsed.
193
+ *
194
+ * Reads are idempotent, so `attempt` needs no atomicity contract — but it must be a
195
+ * COLLECTING drain, not a live iterator: a half-consumed iterator cannot be retried
196
+ * without re-yielding rows the caller already saw, which is why
197
+ * `ControlDatabase.readRows` materializes before this loop ever sees a failure.
198
+ *
199
+ * On exhaustion the LAST error is rethrown unchanged. Log lines carry the
200
+ * `Control read [<label>] …` prefix so a read's retries can be attributed among the
201
+ * several reads in flight concurrently in a real party.
202
+ *
203
+ * No reader passes an `onAbandon` observer, deliberately, though the shared options type
204
+ * offers one ({@link ControlRetryOptions.onAbandon}) and the loop would honour it. That hook
205
+ * exists because an abandoned WRITE can have no caller at all — the background self-record
206
+ * republish is `void`-ed with a `debug`-only catch, so giving up on it is invisible. Every
207
+ * control read is awaited by the caller that issued it, so an abandoned read surfaces as that
208
+ * caller's rejection; there is nothing here to lose silently. A fire-and-forget read would be
209
+ * the reason to start passing one.
210
+ */
211
+ export function retryControlRead<T>(
212
+ attempt: () => Promise<T>,
213
+ options: ControlReadRetryOptions = {}
214
+ ): Promise<T> {
215
+ return retryControlOperation(attempt, {
216
+ attempts: CONTROL_READ_ATTEMPTS,
217
+ delaysMs: CONTROL_READ_RETRY_DELAYS_MS,
218
+ budgetMs: CONTROL_READ_RETRY_BUDGET_MS,
219
+ isRetriable: isRetriableControlReadFailure,
220
+ logPrefix: 'Control read',
221
+ }, options);
222
+ }
@@ -1,5 +1,5 @@
1
1
  import debug from 'debug';
2
- import { isCohortUnreachableRead } from '@optimystic/db-core';
2
+ import { isCohortUnreachableRead, PromiseShortfallError } from '@optimystic/db-core';
3
3
  import { unwrapError } from '@quereus/quereus';
4
4
 
5
5
  const log = debug('sereus:cadre:control-db');
@@ -233,9 +233,10 @@ function notifyAbandoned(
233
233
  }
234
234
 
235
235
  /**
236
- * Every message in the failure's `cause` chain — the substrate both retry classifiers match
237
- * their text matchers against (the transactor's aggregate and the coordinator's shortfall are
238
- * plain `Error`s, recognisable only by text); every wrap on the way out of optimystic and
236
+ * Every message in the failure's `cause` chain — the substrate for the matchers that have only
237
+ * TEXT to work with: the schema-init self-coordination refusal (`control-write-retry.ts`, whose
238
+ * `NOTE:` says why) and `ControlDatabase`'s own message probes. Both retry classifiers claim their
239
+ * failures by TYPE over {@link causeChain} instead. Every wrap on the way out of optimystic and
239
240
  * Quereus embeds the inner message, so the text of the deepest failure is visible at every level.
240
241
  *
241
242
  * `unwrapError` declares its `message` as `string`, but it follows `.cause` without checking
@@ -318,6 +319,37 @@ export function reportsUnreachableCohort(links: readonly Error[]): boolean {
318
319
  return links.some(link => isCohortUnreachableRead(link));
319
320
  }
320
321
 
322
+ /**
323
+ * Does any link of the `cause` chain report the cluster coordinator's super-majority shortfall with
324
+ * NOBODY having voted no — a `PromiseShortfallError` whose `rejections` is zero? The promise round
325
+ * reached no verdict (too few approvals to pass, too few rejections to count as a rejection), so the
326
+ * cohort was simply not all there, and a moment later it may be.
327
+ *
328
+ * The one shortfall test both write policies share — the control-write classifier
329
+ * (`isRetriableControlWriteFailure`) and the edge node's `retryAfterRestart` — so the funnel every
330
+ * local control write passes through and the helper an app wraps its first writes in cannot
331
+ * disagree on which shortfall is worth re-presenting.
332
+ *
333
+ * A non-zero `rejections` is deliberately excluded: a member actually voted no (that branch carries
334
+ * `membership-not-admitted` rejections), and no backoff changes that answer — re-presenting it would
335
+ * re-present a spent signature to a cohort that already refused it.
336
+ *
337
+ * Matched by TYPE, which upstream's own comment on the class asks consumers to prefer over its
338
+ * sentence. Every wrapper between the raise site and either caller keeps the error on `cause` — the
339
+ * optimystic plugin's `rewrapAsQueryError`, Quereus' statement-execution rewrap, its module-create
340
+ * wrapper and its DDL wrapper — so the claim reaches any depth. NOTE: an error built by a second
341
+ * loaded copy of `@optimystic/db-core`, or one rebuilt as a plain `Error` from its text, fails
342
+ * `instanceof` and is not claimed — the same fail-closed direction
343
+ * {@link reportsUnreachableCohort} already accepts.
344
+ *
345
+ * This says only that the round went unanswered. WHERE that leaves the write is the caller's: the
346
+ * control-write funnel still vetoes a shortfall carried by a commit-phase batch, because a commit
347
+ * may have landed with only the reply lost.
348
+ */
349
+ export function reportsUnansweredShortfall(links: readonly Error[]): boolean {
350
+ return links.some(link => link instanceof PromiseShortfallError && link.rejections === 0);
351
+ }
352
+
321
353
  /**
322
354
  * Backoff for the retry that follows attempt `attemptNumber`: the matching base delay
323
355
  * (last entry repeats), jittered ±50%, then capped at the list's largest base so no single