@optimystic/db-core 1.9.0 → 1.10.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 (117) hide show
  1. package/dist/src/cohort-topic/addressing.d.ts +46 -6
  2. package/dist/src/cohort-topic/addressing.d.ts.map +1 -1
  3. package/dist/src/cohort-topic/addressing.js +58 -18
  4. package/dist/src/cohort-topic/addressing.js.map +1 -1
  5. package/dist/src/cohort-topic/member-engine.d.ts +21 -1
  6. package/dist/src/cohort-topic/member-engine.d.ts.map +1 -1
  7. package/dist/src/cohort-topic/member-engine.js +39 -9
  8. package/dist/src/cohort-topic/member-engine.js.map +1 -1
  9. package/dist/src/cohort-topic/membership/publisher.js +1 -1
  10. package/dist/src/cohort-topic/membership/publisher.js.map +1 -1
  11. package/dist/src/cohort-topic/membership/source.d.ts +4 -0
  12. package/dist/src/cohort-topic/membership/source.d.ts.map +1 -1
  13. package/dist/src/cohort-topic/membership/source.js +4 -0
  14. package/dist/src/cohort-topic/membership/source.js.map +1 -1
  15. package/dist/src/cohort-topic/membership/verifier.d.ts +51 -9
  16. package/dist/src/cohort-topic/membership/verifier.d.ts.map +1 -1
  17. package/dist/src/cohort-topic/membership/verifier.js +138 -46
  18. package/dist/src/cohort-topic/membership/verifier.js.map +1 -1
  19. package/dist/src/cohort-topic/ports.d.ts +85 -14
  20. package/dist/src/cohort-topic/ports.d.ts.map +1 -1
  21. package/dist/src/cohort-topic/ports.js +1 -1
  22. package/dist/src/cohort-topic/ports.js.map +1 -1
  23. package/dist/src/cohort-topic/registration/renewal.d.ts +26 -5
  24. package/dist/src/cohort-topic/registration/renewal.d.ts.map +1 -1
  25. package/dist/src/cohort-topic/registration/renewal.js +71 -21
  26. package/dist/src/cohort-topic/registration/renewal.js.map +1 -1
  27. package/dist/src/cohort-topic/service.d.ts +34 -2
  28. package/dist/src/cohort-topic/service.d.ts.map +1 -1
  29. package/dist/src/cohort-topic/service.js +86 -28
  30. package/dist/src/cohort-topic/service.js.map +1 -1
  31. package/dist/src/cohort-topic/sig/payloads.d.ts +5 -1
  32. package/dist/src/cohort-topic/sig/payloads.d.ts.map +1 -1
  33. package/dist/src/cohort-topic/sig/payloads.js +6 -1
  34. package/dist/src/cohort-topic/sig/payloads.js.map +1 -1
  35. package/dist/src/cohort-topic/sig/threshold.d.ts +29 -4
  36. package/dist/src/cohort-topic/sig/threshold.d.ts.map +1 -1
  37. package/dist/src/cohort-topic/sig/threshold.js +39 -7
  38. package/dist/src/cohort-topic/sig/threshold.js.map +1 -1
  39. package/dist/src/cohort-topic/walk.d.ts +19 -4
  40. package/dist/src/cohort-topic/walk.d.ts.map +1 -1
  41. package/dist/src/cohort-topic/walk.js +29 -6
  42. package/dist/src/cohort-topic/walk.js.map +1 -1
  43. package/dist/src/cohort-topic/willingness.d.ts +17 -1
  44. package/dist/src/cohort-topic/willingness.d.ts.map +1 -1
  45. package/dist/src/cohort-topic/willingness.js +18 -3
  46. package/dist/src/cohort-topic/willingness.js.map +1 -1
  47. package/dist/src/cohort-topic/wire/index.d.ts +1 -1
  48. package/dist/src/cohort-topic/wire/index.d.ts.map +1 -1
  49. package/dist/src/cohort-topic/wire/index.js +1 -1
  50. package/dist/src/cohort-topic/wire/index.js.map +1 -1
  51. package/dist/src/cohort-topic/wire/payloads.d.ts +14 -0
  52. package/dist/src/cohort-topic/wire/payloads.d.ts.map +1 -1
  53. package/dist/src/cohort-topic/wire/payloads.js +20 -0
  54. package/dist/src/cohort-topic/wire/payloads.js.map +1 -1
  55. package/dist/src/cohort-topic/wire/primitives.d.ts +8 -0
  56. package/dist/src/cohort-topic/wire/primitives.d.ts.map +1 -1
  57. package/dist/src/cohort-topic/wire/primitives.js +24 -4
  58. package/dist/src/cohort-topic/wire/primitives.js.map +1 -1
  59. package/dist/src/cohort-topic/wire/types.d.ts +37 -0
  60. package/dist/src/cohort-topic/wire/types.d.ts.map +1 -1
  61. package/dist/src/cohort-topic/wire/validate.d.ts +9 -1
  62. package/dist/src/cohort-topic/wire/validate.d.ts.map +1 -1
  63. package/dist/src/cohort-topic/wire/validate.js +23 -4
  64. package/dist/src/cohort-topic/wire/validate.js.map +1 -1
  65. package/dist/src/reactivity/config.d.ts +9 -10
  66. package/dist/src/reactivity/config.d.ts.map +1 -1
  67. package/dist/src/reactivity/config.js +9 -9
  68. package/dist/src/reactivity/config.js.map +1 -1
  69. package/dist/src/reactivity/index.d.ts +4 -3
  70. package/dist/src/reactivity/index.d.ts.map +1 -1
  71. package/dist/src/reactivity/index.js +4 -3
  72. package/dist/src/reactivity/index.js.map +1 -1
  73. package/dist/src/reactivity/rotation.d.ts +73 -57
  74. package/dist/src/reactivity/rotation.d.ts.map +1 -1
  75. package/dist/src/reactivity/rotation.js +54 -54
  76. package/dist/src/reactivity/rotation.js.map +1 -1
  77. package/dist/src/reactivity/subscription.d.ts +12 -9
  78. package/dist/src/reactivity/subscription.d.ts.map +1 -1
  79. package/dist/src/reactivity/subscription.js +9 -6
  80. package/dist/src/reactivity/subscription.js.map +1 -1
  81. package/dist/src/reactivity/topic-anchor.d.ts +36 -31
  82. package/dist/src/reactivity/topic-anchor.d.ts.map +1 -1
  83. package/dist/src/reactivity/topic-anchor.js +41 -39
  84. package/dist/src/reactivity/topic-anchor.js.map +1 -1
  85. package/dist/src/reactivity/verify.d.ts +26 -9
  86. package/dist/src/reactivity/verify.d.ts.map +1 -1
  87. package/dist/src/reactivity/verify.js +26 -11
  88. package/dist/src/reactivity/verify.js.map +1 -1
  89. package/dist/src/reactivity/wire.d.ts +1 -1
  90. package/dist/src/reactivity/wire.d.ts.map +1 -1
  91. package/dist/src/transactor/change-notifier.d.ts +1 -1
  92. package/package.json +1 -1
  93. package/src/cohort-topic/addressing.ts +83 -23
  94. package/src/cohort-topic/member-engine.ts +77 -8
  95. package/src/cohort-topic/membership/publisher.ts +1 -1
  96. package/src/cohort-topic/membership/source.ts +4 -0
  97. package/src/cohort-topic/membership/verifier.ts +169 -51
  98. package/src/cohort-topic/ports.ts +87 -15
  99. package/src/cohort-topic/registration/renewal.ts +87 -24
  100. package/src/cohort-topic/service.ts +157 -35
  101. package/src/cohort-topic/sig/payloads.ts +7 -2
  102. package/src/cohort-topic/sig/threshold.ts +41 -7
  103. package/src/cohort-topic/walk.ts +46 -9
  104. package/src/cohort-topic/willingness.ts +34 -3
  105. package/src/cohort-topic/wire/index.ts +1 -0
  106. package/src/cohort-topic/wire/payloads.ts +21 -0
  107. package/src/cohort-topic/wire/primitives.ts +26 -4
  108. package/src/cohort-topic/wire/types.ts +37 -0
  109. package/src/cohort-topic/wire/validate.ts +26 -3
  110. package/src/reactivity/config.ts +10 -10
  111. package/src/reactivity/index.ts +4 -3
  112. package/src/reactivity/rotation.ts +86 -78
  113. package/src/reactivity/subscription.ts +12 -9
  114. package/src/reactivity/topic-anchor.ts +42 -45
  115. package/src/reactivity/verify.ts +36 -17
  116. package/src/reactivity/wire.ts +1 -1
  117. package/src/transactor/change-notifier.ts +1 -1
@@ -22,6 +22,21 @@ import type { MembershipCertV1 } from "./wire/types.js";
22
22
  */
23
23
  export type RingCoord = Uint8Array;
24
24
 
25
+ declare const topicRouteKeyBrand: unique symbol;
26
+
27
+ /**
28
+ * The bytes a cohort-topic frame is routed on: FRET hashes them exactly once into the cohort's ring
29
+ * coordinate. FRET's `routeAct` hashes the key it is handed — at the origin and again at every forwarding
30
+ * hop — so handing it a coordinate lands the frame at the hash of that coordinate, a ring position no
31
+ * cohort serves. Branded so the only way to obtain one is `TierAddressing.routeKey`, which builds the
32
+ * preimage of `TierAddressing.coord` for the same inputs; a {@link RingCoord} does not type-check where a
33
+ * route key belongs.
34
+ *
35
+ * A separate brand from `RoutingKey` (`network/routing-key.ts`): that one is obtainable only from a block
36
+ * id, and these preimages are not block keys.
37
+ */
38
+ export type TopicRouteKey = Uint8Array & { readonly [topicRouteKeyBrand]: true };
39
+
25
40
  /** A peer, referenced only by its opaque byte-array id. No multiaddr, no libp2p PeerId here. */
26
41
  export interface PeerRef {
27
42
  readonly id: Uint8Array;
@@ -33,13 +48,57 @@ export interface PeerRef {
33
48
  */
34
49
  export interface ITopicRouter {
35
50
  /**
36
- * Route `activity` to the cohort owning `key` and run the cohort's activity callback,
37
- * collecting up to `wantK` participants and at least `minSigs` signatures.
51
+ * Route `activity` to the cohort at `H(key)` and run the cohort's activity callback,
52
+ * collecting up to `wantK` participants and at least `minSigs` signatures. The router hashes `key`
53
+ * once; build it with `TierAddressing.routeKey` so that hash is the tier's coordinate.
38
54
  * @returns the encoded cohort reply.
39
55
  */
40
- routeAndAct(key: RingCoord, activity: Uint8Array, opts: { wantK: number; minSigs: number }): Promise<Uint8Array>;
56
+ routeAndAct(key: TopicRouteKey, activity: Uint8Array, opts: { wantK: number; minSigs: number }): Promise<Uint8Array>;
41
57
  /** Direct dial to a cached primary; falls back to {@link routeAndAct} on failure (caller decides). */
42
58
  dialMember(member: PeerRef, activity: Uint8Array): Promise<Uint8Array>;
59
+ /**
60
+ * Deliver `activity` to the **root group** of a root-placed topic — the peers responsible for
61
+ * `rootKey` — and return the encoded reply. Optional: the walk uses it for the tier-0 step of a
62
+ * root-placed topic when the router has it, and otherwise falls back to ring routing on the root key,
63
+ * which reaches the root coordinate `H(rootKey)`.
64
+ *
65
+ * It exists because the two can disagree: `routeAndAct` lands on the ring's nearest peer to the
66
+ * coordinate, which on a ring shared with another network may be a peer of that other network, whereas
67
+ * the root group is chosen by the rule storage placement uses. db-p2p binds this to the key network.
68
+ */
69
+ routeToRoot?(rootKey: Uint8Array, activity: Uint8Array): Promise<Uint8Array>;
70
+ }
71
+
72
+ /**
73
+ * The per-coord rule for a **root-placed** cohort — the root of a topic whose tier-0 coordinate is
74
+ * `H(rootKey)` (see `addressing.ts`). Such a cohort is the group of peers responsible for the root key
75
+ * rather than the FRET cohort of `wantK`, so the node-wide `minSigs` does not describe it: its signature
76
+ * threshold is `ceil(memberCount × quorumRatio)`.
77
+ *
78
+ * `quorumRatio` is always the **verifying node's own** configuration, never a value read off a message or
79
+ * a certificate. Build one with `createRootPlacement` (`sig/threshold.ts`), which rejects a ratio outside
80
+ * `(0, 1]`.
81
+ */
82
+ export interface RootPlacement {
83
+ /** Fraction of a root-placed cohort's members whose signatures make a quorum, in `(0, 1]`. */
84
+ readonly quorumRatio: number;
85
+ /**
86
+ * The routing key whose ring position is the root coordinate, when the caller knows it — for a reactivity
87
+ * root, the tail block's routing key (`routingKeyForBlock(tailId)`, the raw UTF-8 of the id). A direct
88
+ * anchor that can locate the group's own commit record from the key (db-p2p's commit-log anchor) needs it;
89
+ * one that cannot ignores it. It takes no part in cache identity: a coordinate is the hash of its key, so
90
+ * one coordinate has one key, and the verifier keys its cache by `quorumRatio` alone.
91
+ */
92
+ readonly rootKey?: Uint8Array;
93
+ }
94
+
95
+ /** Options for a membership-cert lookup ({@link IMembershipSource}). */
96
+ export interface MembershipLookupOptions {
97
+ /**
98
+ * The coord is the root of a root-placed topic, so its cert is held by (and must be fetched from) the
99
+ * root group rather than the FRET cohort around the coord.
100
+ */
101
+ readonly rootPlaced?: true;
43
102
  }
44
103
 
45
104
  /** Intra-cohort gossip transport (FRET cohort gossip underneath). */
@@ -53,9 +112,9 @@ export interface ICohortGossipTransport {
53
112
  /** Authoritative cohort membership snapshots (FRET `MembershipCertV1` / stabilization underneath). */
54
113
  export interface IMembershipSource {
55
114
  /** Current cached membership cert for `coord`, encoded; `undefined` if none is cached. */
56
- current(coord: RingCoord): Promise<Uint8Array | undefined>;
115
+ current(coord: RingCoord, opts?: MembershipLookupOptions): Promise<Uint8Array | undefined>;
57
116
  /** Force one refresh of `coord`'s membership cert (stale-cache retry); encoded, or `undefined`. */
58
- fetch(coord: RingCoord): Promise<Uint8Array | undefined>;
117
+ fetch(coord: RingCoord, opts?: MembershipLookupOptions): Promise<Uint8Array | undefined>;
59
118
  }
60
119
 
61
120
  /**
@@ -77,10 +136,11 @@ export interface ICohortThresholdCrypto {
77
136
  /**
78
137
  * Sink the cohort uses to publish a `MembershipCertV1` (over FRET's `/membership` protocol).
79
138
  * db-core's publisher builds + threshold-signs the cert and hands it here; db-p2p serves it.
139
+ * A node serves one cohort per coordinate it is in, so a cert is published under its coordinate.
80
140
  */
81
141
  export interface IMembershipPublishSink {
82
- /** Publish (advertise/serve) an already-signed membership certificate, encoded. */
83
- publish(encodedCert: Uint8Array): void;
142
+ /** Publish (advertise/serve) an already-signed membership certificate, encoded, for cohort coord `coord`. */
143
+ publish(coord: RingCoord, encodedCert: Uint8Array): void;
84
144
  }
85
145
 
86
146
  /** Network-size estimate feeding `d_max`. */
@@ -106,15 +166,27 @@ export type TrustAnchorVerdict = "anchored" | "rejected" | "unknown";
106
166
  * Direct (base-case) trust anchor for a {@link MembershipCertV1}'s `coord → keyset` binding.
107
167
  *
108
168
  * db-core owns the chain-of-attestations and trust-root logic; the **direct** anchor is
109
- * tier/transport-specific (FRET ring agreement for T2/T3, tx-log commit certificate for T0/T1) and
110
- * is therefore delegated through this port. db-p2p binds it; **db-core never imports FRET**. A node
111
- * with no local authority for the coord returns `"unknown"` so the verifier falls through to the
112
- * chain / interim fallback instead of breaking distant verification. See {@link noAuthorityTrustAnchor}
113
- * for the db-core default (every coord `"unknown"`).
169
+ * tier/transport-specific and is therefore delegated through this port. db-p2p binds two: the FRET ring
170
+ * agreement for a coord this node serves (`FretTrustAnchor`), and, for a reactivity root — a root-placed
171
+ * coord whose placement names its key — the tail block's own commit proof, fetched from the tail's storage
172
+ * group (`CommitLogTrustAnchor`); the host asks the first and falls through to the second on `"unknown"`.
173
+ * **db-core never imports FRET.** A node with no authority for the coord returns `"unknown"` so the
174
+ * verifier falls through to the chain / interim fallback instead of breaking distant verification. See
175
+ * {@link noAuthorityTrustAnchor} for the db-core default (every coord `"unknown"`).
176
+ *
177
+ * **The verdict may be asynchronous.** An anchor that judges from local state answers synchronously; one
178
+ * that has to ask the network (the commit-log anchor) returns a promise, and the verifier's trust gate
179
+ * awaits either. An anchor is total: it never throws or rejects, and answers `"unknown"` for anything it
180
+ * cannot decode or reach.
114
181
  */
115
182
  export interface IMembershipTrustAnchor {
116
- /** Judge, from a directly-trusted source, whether `cert.members` is authoritative for `cert.cohortCoord` at `tier`. */
117
- directAnchor(cert: MembershipCertV1, tier: number): TrustAnchorVerdict;
183
+ /**
184
+ * Judge, from a directly-trusted source, whether `cert.members` is authoritative for `cert.cohortCoord`
185
+ * at `tier`. `placement` is present when the verifier was told the coord is root-placed, so the anchor
186
+ * compares against the root group (and its threshold) rather than the FRET cohort around the coord; its
187
+ * `rootKey`, when the caller knew it, is what lets an anchor locate that group's own records.
188
+ */
189
+ directAnchor(cert: MembershipCertV1, tier: number, placement?: RootPlacement): TrustAnchorVerdict | Promise<TrustAnchorVerdict>;
118
190
  }
119
191
 
120
192
  /**
@@ -139,7 +211,7 @@ export interface TrustRoot {
139
211
  * the interim TOFU behavior (no regression) until db-p2p binds a real anchor.
140
212
  */
141
213
  export const noAuthorityTrustAnchor: IMembershipTrustAnchor = {
142
- directAnchor(_cert: MembershipCertV1, _tier: number): TrustAnchorVerdict {
214
+ directAnchor(_cert: MembershipCertV1, _tier: number, _placement?: RootPlacement): TrustAnchorVerdict {
143
215
  return "unknown";
144
216
  },
145
217
  };
@@ -3,7 +3,13 @@
3
3
  *
4
4
  * Per `docs/cohort-topic.md` §TTL and renewal and §Failure modes:
5
5
  *
6
- * - Participant pings `primary` every `ttl/3`. Success → primary touches `lastPing` and gossips it.
6
+ * - Participant pings `primary` every `ttl/3`. The four replies:
7
+ * - `ok` — the primary touched `lastPing` and gossips it; the failure count resets.
8
+ * - `primary_moved` — a rotation moved the slot; the participant adopts the named primary/backups/epoch
9
+ * and the failure count resets.
10
+ * - `unknown_registration` / `withdrawn` — the member holds no record for this participant (it restarted,
11
+ * the record expired, a stale withdraw removed it, or gossip has not reached it yet). Counted as a failed
12
+ * ping, exactly like no answer: an answer from a member that does not hold the record keeps nothing alive.
7
13
  * - Three consecutive ping failures → participant promotes `backups[0]` via a re-attach RPC: a renew
8
14
  * carrying a **signed `reattach` flag** (no full re-registration). The backup accepts when it holds
9
15
  * the record locally *and* is a computed backup under the current epoch — re-stamping `primary` to
@@ -13,7 +19,11 @@
13
19
  * payload instead of promoting the contacted backup (ignoring a reply that points back at the dead
14
20
  * primary). **Resolved (GROUNDING):** the participant's `cohortEpoch` hint refreshes lazily — on the
15
21
  * *next* ping/renewal after failover, not eagerly at failover time.
16
- * - All of primary + backups fail → participant re-runs lookup from `d_max` (walk ticket, injected).
22
+ * - All of primary + backups fail → participant re-runs lookup from `d_max` (injected) and adopts where the
23
+ * re-walk landed — primary, backups, epoch hint and the accepted register's correlation id. A walk that is
24
+ * not accepted keeps the old assignment. Either way the failure count resets, so the next re-walk is at
25
+ * least {@link MAX_PING_FAILURES} pings away: at most one per TTL, under the cohort's per-peer register
26
+ * rate limit, and a one-off `unknown_registration` from replication lag clears on the next ping.
17
27
  * - Cohort-side: evict where `now − lastPing > ttl`; eviction is gossiped so members converge.
18
28
  *
19
29
  * Both sides take their transport/gossip by injection so storage + sharding + TTL stay unit-testable
@@ -57,12 +67,24 @@ export interface RenewalParticipant {
57
67
  /** The unsigned body of a renew/re-attach RPC; the injected signer turns it into a {@link RenewV1}. */
58
68
  export type UnsignedRenew = Omit<RenewV1, "signature">;
59
69
 
70
+ /** Where a re-run register walk placed the registration; the participant adopts it in place of its own. */
71
+ export interface RenewalAssignment {
72
+ readonly primary: Uint8Array;
73
+ readonly backups: readonly Uint8Array[];
74
+ readonly cohortEpoch: Uint8Array;
75
+ /** The accepted register's correlation id, base64url — echoed on every later renew. */
76
+ readonly correlationId: string;
77
+ }
78
+
60
79
  /** Transport the participant drives; supplied by db-p2p (FRET dial / RouteAndMaybeAct underneath). */
61
80
  export interface RenewalParticipantTransport {
62
81
  /** Send a renew/re-attach to `target`; resolves with the reply, rejects on RPC failure. */
63
82
  send(target: Uint8Array, msg: RenewV1): Promise<RenewReplyV1>;
64
- /** Primary + all backups unreachable: re-run lookup from `d_max` (walk ticket owns the body). */
65
- relookup(): Promise<void>;
83
+ /**
84
+ * Primary + all backups failed: re-run the register walk from `d_max`. Resolves the accepted walk's
85
+ * assignment, or `undefined` when the walk was not accepted (a back-off or redirect it could not finish).
86
+ */
87
+ relookup(): Promise<RenewalAssignment | undefined>;
66
88
  }
67
89
 
68
90
  export interface RenewalParticipantDeps {
@@ -71,7 +93,7 @@ export interface RenewalParticipantDeps {
71
93
  clock: () => number;
72
94
  /** Signs the renew body; db-p2p supplies the (async) peer-key signature. */
73
95
  sign: (body: UnsignedRenew) => Promise<string>;
74
- /** Correlation id matching the original RegisterV1, base64url. */
96
+ /** Correlation id matching the original RegisterV1, base64url; replaced when a relookup lands. */
75
97
  correlationId: string;
76
98
  /** Initial cohort-epoch hint from the registration reply (refreshed lazily thereafter). */
77
99
  initialCohortEpoch?: Uint8Array;
@@ -81,10 +103,12 @@ class TtlRenewalParticipant implements RenewalParticipant {
81
103
  private consecutiveFailures = 0;
82
104
  private current: RegistrationRecord;
83
105
  private epochHint: Uint8Array | undefined;
106
+ private correlationId: string;
84
107
 
85
108
  constructor(initial: RegistrationRecord, private readonly deps: RenewalParticipantDeps) {
86
109
  this.current = initial;
87
110
  this.epochHint = deps.initialCohortEpoch;
111
+ this.correlationId = deps.correlationId;
88
112
  }
89
113
 
90
114
  get record(): RegistrationRecord {
@@ -101,15 +125,10 @@ class TtlRenewalParticipant implements RenewalParticipant {
101
125
 
102
126
  async pingLoop(): Promise<void> {
103
127
  const reply = await this.trySend(this.current.primary);
104
- if (reply !== undefined) {
105
- this.onPingSuccess(reply);
106
- return;
107
- }
108
- this.consecutiveFailures++;
109
- if (this.consecutiveFailures < MAX_PING_FAILURES) {
128
+ if (reply !== undefined && this.acceptPingReply(reply)) {
110
129
  return;
111
130
  }
112
- await this.failover();
131
+ await this.strike();
113
132
  }
114
133
 
115
134
  async reattach(target: Uint8Array): Promise<RenewReplyV1> {
@@ -136,11 +155,37 @@ class TtlRenewalParticipant implements RenewalParticipant {
136
155
  }
137
156
  }
138
157
 
139
- private onPingSuccess(reply: RenewReplyV1): void {
140
- this.consecutiveFailures = 0;
141
- if (reply.result === "primary_moved") {
142
- // Lazy cohortEpoch refresh lands here: a move discovered on a normal ping updates the hint.
143
- this.applyPrimaryMoved(reply);
158
+ /**
159
+ * Act on the primary's answer to a plain ping. Returns `false` when the answer says the member holds no
160
+ * record for this participant — a failed ping, which the caller counts as a strike.
161
+ */
162
+ private acceptPingReply(reply: RenewReplyV1): boolean {
163
+ switch (reply.result) {
164
+ case "ok": {
165
+ this.consecutiveFailures = 0;
166
+ return true;
167
+ }
168
+ case "primary_moved": {
169
+ this.consecutiveFailures = 0;
170
+ // Lazy cohortEpoch refresh lands here: a move discovered on a normal ping updates the hint.
171
+ this.applyPrimaryMoved(reply);
172
+ return true;
173
+ }
174
+ case "unknown_registration":
175
+ case "withdrawn":
176
+ return false;
177
+ default: {
178
+ const unexpected: never = reply.result;
179
+ throw new Error(`cohort-topic: unexpected renew result ${String(unexpected)}`);
180
+ }
181
+ }
182
+ }
183
+
184
+ /** Count a failed ping; the {@link MAX_PING_FAILURES}th in a row fails over. */
185
+ private async strike(): Promise<void> {
186
+ this.consecutiveFailures++;
187
+ if (this.consecutiveFailures >= MAX_PING_FAILURES) {
188
+ await this.failover();
144
189
  }
145
190
  }
146
191
 
@@ -149,7 +194,7 @@ class TtlRenewalParticipant implements RenewalParticipant {
149
194
  * backup accepted the crash-failover promotion → promote it locally. A `primary_moved` means a real
150
195
  * rotation moved primary to a different live member → adopt that payload (not the contacted backup),
151
196
  * **unless** it points back at the just-failed primary (the bounce guard) — then keep trying. All
152
- * backups exhausted → re-run lookup from `d_max`.
197
+ * backups exhausted → re-run lookup from `d_max` and adopt where it landed.
153
198
  */
154
199
  private async failover(): Promise<void> {
155
200
  const failedPrimary = this.current.primary;
@@ -177,11 +222,29 @@ class TtlRenewalParticipant implements RenewalParticipant {
177
222
  }
178
223
  // unknown_registration (replication lag) or anything else: try the next backup.
179
224
  }
180
- await this.deps.transport.relookup();
181
- // relookup is a terminal recovery action (it owns re-establishing the registration out of
182
- // band); reset the counter so a still-dead primary backs off to one relookup per
183
- // MAX_PING_FAILURES cycles rather than re-running the d_max walk on every subsequent ping.
184
- this.consecutiveFailures = 0;
225
+ // Reset the counter however the relookup ends — landed, not accepted, or thrown — so a registration
226
+ // that stays unreachable backs off to one d_max walk per MAX_PING_FAILURES pings rather than one per
227
+ // ping. A throw still propagates to the caller of `pingLoop`.
228
+ try {
229
+ const landing = await this.deps.transport.relookup();
230
+ if (landing !== undefined) {
231
+ this.adopt(landing);
232
+ }
233
+ } finally {
234
+ this.consecutiveFailures = 0;
235
+ }
236
+ }
237
+
238
+ /** Take a re-walk's assignment as the registration: later pings go to its primary under its correlation id. */
239
+ private adopt(landing: RenewalAssignment): void {
240
+ this.current = {
241
+ ...this.current,
242
+ primary: landing.primary,
243
+ backups: [...landing.backups],
244
+ lastPing: this.deps.clock(),
245
+ };
246
+ this.epochHint = landing.cohortEpoch;
247
+ this.correlationId = landing.correlationId;
185
248
  }
186
249
 
187
250
  private async tryReattach(target: Uint8Array): Promise<RenewReplyV1 | undefined> {
@@ -228,7 +291,7 @@ class TtlRenewalParticipant implements RenewalParticipant {
228
291
  v: 1,
229
292
  topicId: bytesKey(this.current.topicId),
230
293
  participantId: bytesKey(this.current.participantId),
231
- correlationId: this.deps.correlationId,
294
+ correlationId: this.correlationId,
232
295
  timestamp: this.deps.clock(),
233
296
  };
234
297
  if (opts.reattach === true) {