Skip to content

fix(selection): bound reputation above 1.0 in compute_score - #1461

Merged
thiras merged 2 commits into
mainfrom
fix/1458-reputation-upper-clamp
Jul 26, 2026
Merged

thiras merged 2 commits into
mainfrom
fix/1458-reputation-upper-clamp

Conversation

@yigitdot

@yigitdot yigitdot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

What

Candidate.reputation is documented as domain [0.0, 1.0], but compute_score clamped 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 → score 0.0 → sorts first under total_cmp, and then tie_group_end's pivot == 0.0 branch 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).

-    let rep = f64::from(reputation.max(REPUTATION_FLOOR));
+    let rep = f64::from(reputation.max(REPUTATION_FLOOR).min(REPUTATION_CEILING));

Not reachable today: LocalReputation::fold ends in .clamp(0.0, 1.0), decay interpolates convexly toward neutral, and require_unit_interval validates the config seeds. What the guard buys is that the domain is currently a convention on a plain f32 field, not a type — nothing at the Candidate boundary enforces it, and a future producer need not be as careful as today's one.

Why .max().min() and not .clamp()

f32::max/f32::min are documented to ignore NaN and return the other operand, so NaN.max(0.1) is 0.1 and no NaN reaches the score. f32::clamp propagates NaN (and panics if min > max), so the .clamp(REPUTATION_FLOOR, REPUTATION_CEILING) form would regress score_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_clamp is 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, with got 10 vs 1000 measuring the 100× bug directly and the second covering the 0.0-sorts-first case. The same check was repeated for the two tests added in the follow-up commit, by removing .min(REPUTATION_CEILING):

FAIL rank_candidates_denies_out_of_domain_reputation_the_top_slot
FAIL score_reputation_one_ulp_above_domain_clamps_to_ceiling
     panicked at crates/node/src/selection.rs:627:9: got 999.9997615814635 vs 1000

Review follow-up (161689d)

A comprehensive review of the first commit found no correctness issues in the clamp, and the following cleanups, all applied:

  • Stale formula. The doc-comment head still read 1 / max(reputation, 0.1)² while the prose two lines below described a two-sided clamp. Fixed, and adr/001-network.md gains the matching half-sentence so spec and code stop disagreeing.
  • Named the ceiling. REPUTATION_CEILING replaces a bare 1.0 that appeared at five code-and-prose sites which have to move together; the floor was already a named const.
  • Removed a wrong causal claim. The first commit (and this description) blamed peer_reputation's unchecked as 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 contradicted node_origin.rs's own cast-truncation waiver. Corrected above and in the code.
  • Two tests added: 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.
  • Trimmed ~12 lines of rationale that had been stated four times over.

Verification

cargo fmt -- --check                                     clean
cargo clippy --workspace --all-targets -- -D warnings    clean
cargo nextest run -p decdn-node --no-fail-fast           869 passed, 0 failed

Out of scope

Option (b) — the Reputation newtype at peer_reputation's cast — is deliberately not done here, per the issue's suggestion to keep it a separate call once someone is in node_origin anyway. A stale ReputationEngine::score() reference in a neighbouring pre-existing comment is filed as #1464 rather than bundled.

Closes #1458

🤖 Generated with Claude Code

`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]>
@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@yigitdot, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 34 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: 3e7f57aa-0408-4942-9fdf-f5732be11bb6

📥 Commits

Reviewing files that changed from the base of the PR and between 9dbc42e and 161689d.

📒 Files selected for processing (2)
  • adr/001-network.md
  • crates/node/src/selection.rs

Walkthrough

Reputation inputs in compute_score are now bounded between REPUTATION_FLOOR and 1.0, with NaN-safe max/min operations. Documentation and regression tests cover values above 1.0 and positive infinity.

Changes

Reputation scoring

Layer / File(s) Summary
Bounded reputation score and regression coverage
crates/node/src/selection.rs
compute_score clamps reputation to both bounds using NaN-safe operations, documents the ceiling behavior, and tests values above 1.0 and f32::INFINITY against the baseline score.

Estimated code review effort: 2 (Simple) | ~10 minutes


Note

🎁 Summarized by CodeRabbit Free

Your 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 @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Jul 25, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 81.08% (was 81.07%, ▲ +0.01%) — 52409/64642 lines

File Coverage Lines Δ
crates/node/src/selection.rs 97.53% 473/485 ▲ +0.14%
History
Commit Total Δ vs base
9dbc42e 81.07% ● 0.00%
161689d (HEAD) 81.08% ▲ +0.01%

… 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]>
@thiras
thiras merged commit d2e0478 into main Jul 26, 2026
24 checks passed
@thiras
thiras deleted the fix/1458-reputation-upper-clamp branch July 26, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

selection: reputation has no upper bound — f32::INFINITY wins selection unconditionally

2 participants