Repository navigation
fix(selection): bound reputation above 1.0 in compute_score - #1461
Conversation
`Candidate.reputation` is documented as domain [0.0, 1.0], but the score denominator clamped it below only. An out-of-domain value bought an unearned bonus rather than being ignored: rep = 10.0 divided the score by 100 and outranked a perfect-reputation peer by 100x, and f32::INFINITY produced score 0.0 -- first place under total_cmp, unconditionally. Not reachable today (LocalReputation::fold ends in .clamp(0.0, 1.0)), but the guarantee crosses a crate boundary by convention: peer_reputation in node_origin narrows the engine's f64 with an unchecked `as f32`. Bound the consumer too. Deliberately `.max(REPUTATION_FLOOR).min(1.0)` and NOT `.clamp(REPUTATION_FLOOR, 1.0)`: f32::max/min implement IEEE maxNum/minNum and ignore NaN, so a NaN reputation lands on the floor, whereas clamp propagates NaN and would regress score_nan_reputation_clamps_to_floor. clippy::manual_clamp is waived at the statement with that reasoning -- the lint's own note concedes both the NaN-propagation and the min > max panic. Adds coverage at both ends of the domain; the upper end had none. Closes #1458 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Warning Review limit reached
Next review available in: 34 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Free Run ID: 📒 Files selected for processing (2)
WalkthroughReputation inputs in ChangesReputation scoring
Estimated code review effort: 2 (Simple) | ~10 minutes Note 🎁 Summarized by CodeRabbit FreeYour organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login. Comment |
Coverage reportTotal: 81.08% (was 81.07%, ▲ +0.01%) — 52409/64642 lines
History
|
… ordering test Review follow-ups on the #1458 clamp. No behavior change to compute_score. - The doc-comment formula still read `1 / max(reputation, 0.1)^2` while the prose two lines below described a two-sided clamp. Fix the formula and note that the upper bound extends ADR 001 rather than restating it; add the same half-sentence to ADR 001 so the spec and the code stop disagreeing. - Name the ceiling (REPUTATION_CEILING) instead of writing 1.0 inline. It appeared as a bare literal at five code-and-prose sites that have to move together, while the floor was already a named const. - Drop a wrong causal claim from score_reputation_above_1_0_clamps_to_ceiling: peer_reputation's `as f32` cannot produce a value above 1.0 from an f64 in [0,1] (1.0 is exactly representable and the rounding is monotonic), and the claim contradicted node_origin.rs's own cast-truncation waiver. The real reason the guard is needed is that the domain is a convention on a plain f32 field, not a type. - Record that the operand ORDER is load-bearing: `.min()` first would send NaN to the ceiling, turning a garbage reputation into a perfect peer. - Cut ~12 lines of rationale that had been stated four times over, including a paragraph quoting clippy's lint text verbatim. - Correct the INFINITY test's account of the failure: score 0.0 does not merely sort first, it also takes tie_group_end's `pivot == 0.0` branch and becomes a tie group of one, so no tie-break tier can dislodge it. Also note that the `is_finite` assertion is a NaN guard and is NOT what fails against the unclamped version -- 0.0 is finite. Two tests added, both observed red with the ceiling removed: rank_candidates_denies_out_of_domain_reputation_the_top_slot (the ordering half no compute_score test can reach, mirroring the existing negative-reputation test) and score_reputation_one_ulp_above_domain_clamps_to_ceiling (1.0 + f32::EPSILON scores 999.9997615814635 unclamped). A stale `ReputationEngine::score()` reference in a neighbouring comment names a type that exists nowhere in the workspace. Pre-existing and outside this diff, so filed as #1464 rather than bundled. cargo fmt --check and cargo clippy --workspace --all-targets -- -D warnings are clean; cargo nextest run -p decdn-node --no-fail-fast is 869 passed, 0 failed. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
What
Candidate.reputationis documented as domain[0.0, 1.0], butcompute_scoreclamped it below only, so an out-of-domain value bought an unearned bonus instead of being ignored:reputation = 10.0→ score divided by 100 → beats a perfect-reputation node by 100×.reputation = f32::INFINITY→ score0.0→ sorts first undertotal_cmp, and thentie_group_end'spivot == 0.0branch hands it a tie group of one — so no tie-break tier (geo, stake, random) can dislodge it either.This is option (a) from #1458 — the cheap local clamp, plus a test at each end of the domain. The upper half had no defense and no test (#1456 deleted the last test touching that end).
Not reachable today:
LocalReputation::foldends in.clamp(0.0, 1.0),decayinterpolates convexly toward neutral, andrequire_unit_intervalvalidates the config seeds. What the guard buys is that the domain is currently a convention on a plainf32field, not a type — nothing at theCandidateboundary enforces it, and a future producer need not be as careful as today's one.Why
.max().min()and not.clamp()f32::max/f32::minare documented to ignore NaN and return the other operand, soNaN.max(0.1)is0.1and no NaN reaches the score.f32::clamppropagates NaN (and panics ifmin > max), so the.clamp(REPUTATION_FLOOR, REPUTATION_CEILING)form would regressscore_nan_reputation_clamps_to_floor.The operand order is load-bearing for the same reason:
.min()first would send NaN to the ceiling (NaN.min(1.0) == 1.0), turning a garbage reputation into a perfect peer.clippy::manual_clampis therefore waived at the statement — the lint's own note concedes both hazards.Test evidence — red before, green after
Both original tests were written first and observed failing against unmodified
compute_score, withgot 10 vs 1000measuring the 100× bug directly and the second covering the0.0-sorts-first case. The same check was repeated for the two tests added in the follow-up commit, by removing.min(REPUTATION_CEILING):Review follow-up (161689d)
A comprehensive review of the first commit found no correctness issues in the clamp, and the following cleanups, all applied:
1 / max(reputation, 0.1)²while the prose two lines below described a two-sided clamp. Fixed, andadr/001-network.mdgains the matching half-sentence so spec and code stop disagreeing.REPUTATION_CEILINGreplaces a bare1.0that appeared at five code-and-prose sites which have to move together; the floor was already a named const.peer_reputation's uncheckedas f32. That cast cannot produce a value above 1.0 from an f64 in[0,1]— 1.0 is exactly representable and the rounding is monotonic — and the claim contradictednode_origin.rs's own cast-truncation waiver. Corrected above and in the code.rank_candidates_denies_out_of_domain_reputation_the_top_slot(the ordering half nocompute_scoretest can reach, mirroring the existing negative-reputation test) andscore_reputation_one_ulp_above_domain_clamps_to_ceiling.Verification
Out of scope
Option (b) — the
Reputationnewtype atpeer_reputation's cast — is deliberately not done here, per the issue's suggestion to keep it a separate call once someone is innode_originanyway. A staleReputationEngine::score()reference in a neighbouring pre-existing comment is filed as #1464 rather than bundled.Closes #1458
🤖 Generated with Claude Code