Skip to content

🧹 Fix unsafe unwrap() on partial_cmp in telegram runtime and claude usage - #79

Merged
undivisible merged 2 commits into
mainfrom
code-health-fix-unsafe-unwrap-1547466174559491649
Sep 5, 2026
Merged

🧹 Fix unsafe unwrap() on partial_cmp in telegram runtime and claude usage#79
undivisible merged 2 commits into
mainfrom
code-health-fix-unsafe-unwrap-1547466174559491649

Conversation

@undivisible

@undivisible undivisible commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

🎯 What: The code health issue addressed
Replaced unsafe .unwrap() calls on .partial_cmp() when sorting f64 floats with a safe .unwrap_or(std::cmp::Ordering::Equal) fallback. This was fixed in both src/telegram_runtime.rs and src/tools/claude_usage.rs.

💡 Why: How this improves maintainability
Using .unwrap() on .partial_cmp() with floats is dangerous in Rust because if a NaN value is encountered, the comparison returns None, which causes .unwrap() to panic and crash the program. By providing a safe fallback (like Ordering::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() on f64::partial_cmp with .unwrap_or(Ordering::Equal) in two places that sort per-model costs for display: Telegram /cost and the claude_usage tool’s cost output.

Why: partial_cmp returns None when 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.

@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@mergify

mergify Bot commented Aug 27, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/telegram_runtime.rs
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));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

google-labs-jules Bot and others added 2 commits September 5, 2026 19:00
- 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>
@undivisible
undivisible force-pushed the code-health-fix-unsafe-unwrap-1547466174559491649 branch from f604cf6 to f134139 Compare September 5, 2026 11:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-05T11:03:41.479285Z f134139 New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@undivisible
undivisible merged commit f3335c7 into main Sep 5, 2026
3 checks passed
@undivisible
undivisible deleted the code-health-fix-unsafe-unwrap-1547466174559491649 branch September 5, 2026 11:22
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.

1 participant