Fix OpenAI usage percent normalization for low values#178
Open
ktmyname wants to merge 1 commit into
Open
Conversation
The wham/usage endpoint and OpenAI account-usage helpers report 'used_percent' as a value in [0, 100]. The previous heuristic in 'normalize_ratio' tried to auto-detect ratio-vs-percent by checking 'raw > 1.0', which incorrectly mapped legitimate API responses like 'used_percent: 1' (1% used) to a ratio of 1.0, displayed as a fully exhausted '100%' bar. This made 'jcode usage' (and the inline /usage overlay) show ChatGPT subscriptions as completely rate-limited even when only ~1% of the weekly bucket had been consumed. The Codex desktop app, which reads the same endpoint, correctly showed the actual remaining quota. Treat the API value as a percent unconditionally and clamp to [0, 1]. Also add direct unit tests and a regression test that mirrors a real production payload from the wham/usage endpoint with single-digit 'used_percent' values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
jcode usage(and the inline/usageoverlay) shows ChatGPT subscriptionsas fully rate-limited (
100% used, exhausted bar) even when the livewham/usageendpoint reports only a few percent consumed. The Codexdesktop app, which reads the same endpoint with the same OAuth token,
correctly shows the actual remaining quota.
Root cause
Both
src/usage/openai_helpers.rsandsrc/usage_openai.rsdefine anormalize_ratiohelper:It tries to auto-detect whether the input is a ratio (
0..=1) or apercent (
0..=100) usingraw > 1.0. The OpenAIwham/usageendpointunambiguously returns
used_percentin[0, 100]. A real productionpayload with a barely-touched weekly bucket looks like:
{ "rate_limit": { "primary_window": { "used_percent": 5, ... }, "secondary_window":{ "used_percent": 1, ... } } }used_percent: 1(1% used) hits theelsebranch, clamps to1.0,and is rendered as 100% used / fully exhausted — even though the
user has 99% of their weekly Codex quota remaining.
The existing tests only exercise values like
25.0,50.0,75.0,100.0, so the bug never surfaced in CI.Fix
The wham/usage and OpenAI account-usage endpoints always report
percentages in
[0, 100]. Drop the heuristic and just divide:Applied to both
src/usage/openai_helpers.rsand the legacysrc/usage_openai.rs.Tests
Added two tests in
src/usage/tests.rs:test_normalize_ratio_treats_low_integer_values_as_percent— directunit coverage of the helper across
0,1,5,50,100, plus>100, negative, andNaNinputs.test_parse_openai_usage_payload_reports_low_percentages_correctly—regression test built from a real
wham/usagepayload withused_percent: 1in the secondary window. Asserts the parserreturns
1.0, not100.0.All 33
usage::tests::*pass oncargo test --release --lib.Verification (live API)
Before patch:
After patch (same account, same
wham/usageresponse, ~10 minutes later):This now matches what the Codex desktop app shows for the same account.
Impact
jcode usage, the/usageoverlay, and the info-widget compact usagebar all consume
normalize_ratioviaparse_wham_window, so all threenow report correctly.
usage_ratio/seven_day_ratiopreviously assumed users were exhausted at 1% realusage; they now see the true value and stop spuriously failing over.
Need help on this PR? Tag
@codesmithwith what you need.