← Back to Pull requests

Return Error::Overflow from WeightedIndex::update_weights on integer overflow #1846

Open
unknown opened this pull request with 201 lines diff across 2 files
View on GitHub
unknown unknown commented on Nov 23, 2026 05:40 UTC
Author

  • Added a CHANGELOG.md entry
  • Summary

    WeightedIndex::update_weights adds integer weights with a plain +=, while new uses checked_add_assign. An update whose sum overflows panics in debug builds and wraps in release builds, contrary to the documented Error::Overflow and "In case of error, self is not modified".

    Motivation

    let mut w = WeightedIndex::new([200u8, 50]).unwrap();
    w.update_weights(&[(1, &100)])
    

    before, debug:    panicked at src/distr/weighted/weighted_index.rs:201:13: attempt to add with overflow
    before, release:  Ok(()), total_weight = 44; index 1 is never sampled again (10000 samples: [10000, 0])
    after:            Err(Overflow), total_weight = 250; 10000 samples: [8022, 1978]
    

    Details

    Each weight is now added with checked_add_assign, as new does, and any overflow returns Error::Overflow, including an intermediate one in a multi-index update. For floats checked_add_assign never fails, so the float arithmetic is unchanged.

    This adds a Weight bound to update_weights. new already requires it, so only a WeightedIndex obtained through Deserialize could lack it.

    The new update_weights_overflow test fails on master (debug: the panic; release: Ok(()) instead of Err(Overflow)). cargo test (default, serde,log, --lib --tests --no-default-features [--features=alloc,sys_rng], +nightly --all-features), cargo +1.85 test --lib, cargo fmt --all -- --check and cargo clippy --workspace --lib --bins --tests -- -D warnings (stable) pass.

    Written with AI assistance (Claude); I have reviewed the change.

    Diff: 201 lines (+150 / -51) across 2 files Reviewer: youdie006 Review duration: 10587m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.36) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.36 · 201 lines diff across 2 files
    0.36
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.318
    Real PR Conversation Timeline
    1 comments
    "Review active on #1846"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 36.2% (MEDIUM) · Higher = larger blast radius
    36.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.7% · Safe to merge
    20.7%