🧹 Fix unsafe unwrap() on partial_cmp in telegram runtime and claude usage - #79
Conversation
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Tick the box to add this pull request to the merge queue (same as
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 236236af77
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let summary = runner.get_cost_summary().await; | ||
| let mut by_model: Vec<_> = summary.by_model.iter().collect(); | ||
| by_model.sort_by(|a, b| b.1.partial_cmp(a.1).unwrap()); | ||
| by_model.sort_by(|a, b| b.1.partial_cmp(a.1).unwrap_or(std::cmp::Ordering::Equal)); |
There was a problem hiding this comment.
Use a total ordering instead of equating NaN
When a NaN is present—the edge case this change is intended to handle—returning Equal for every unordered comparison makes the comparator non-transitive. Because by_model comes from a HashMap, some iteration orders can leave finite costs unsorted (for example, descending-sort [1.0, NaN, 3.0, 2.0] remains unchanged). Use f64::total_cmp or explicitly place non-finite values, and apply the same fix and a regression case to the comparator in src/tools/claude_usage.rs.
AGENTS.md reference: AGENTS.md:L526-L528
Useful? React with 👍 / 👎.
- Replaced `unwrap()` with `unwrap_or(std::cmp::Ordering::Equal)` in `src/telegram_runtime.rs` and `src/tools/claude_usage.rs`. - This prevents potential runtime panics if `NaN` values are encountered during the sorting of floating-point values (`f64`). Co-authored-by: undivisible <136312656+undivisible@users.noreply.github.com>
- Replaced `unwrap()` with `unwrap_or(std::cmp::Ordering::Equal)` in `src/telegram_runtime.rs` and `src/tools/claude_usage.rs`. - This prevents potential runtime panics if `NaN` values are encountered during the sorting of floating-point values (`f64`). Co-authored-by: undivisible <136312656+undivisible@users.noreply.github.com>
f604cf6 to
f134139
Compare
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
🎯 What: The code health issue addressed
Replaced unsafe
.unwrap()calls on.partial_cmp()when sortingf64floats with a safe.unwrap_or(std::cmp::Ordering::Equal)fallback. This was fixed in bothsrc/telegram_runtime.rsandsrc/tools/claude_usage.rs.💡 Why: How this improves maintainability
Using
.unwrap()on.partial_cmp()with floats is dangerous in Rust because if aNaNvalue is encountered, the comparison returnsNone, which causes.unwrap()to panic and crash the program. By providing a safe fallback (likeOrdering::Equal), we guarantee the application remains stable.✅ Verification: How you confirmed the change is safe
Ran the relevant tool tests (
cargo test --lib tools::claude_usage), and the wider test suite. Verified that the fallback doesn't change standard sorting behavior but adds safety against edge cases.✨ Result: The improvement achieved
The codebase is safer and more robust without any changes in observed functionality.
PR created automatically by Jules for task 1547466174559491649 started by @undivisible
Note
Low Risk
Display-only sorting change with a safe fallback; no auth, billing, or persistence logic touched.
Overview
Replaces
.unwrap()onf64::partial_cmpwith.unwrap_or(Ordering::Equal)in two places that sort per-model costs for display: Telegram/costand theclaude_usagetool’s cost output.Why:
partial_cmpreturnsNonewhen either operand is NaN; unwrapping would panic. Treating incomparable pairs as equal keeps sorting stable and prevents crashes if bad cost data ever appears.Reviewed by Cursor Bugbot for commit 236236a. Configure here.