fix(usage): OpenAI percent normalization for low values (#137)#174
Open
quangdang46 wants to merge 1 commit into
Open
fix(usage): OpenAI percent normalization for low values (#137)#174quangdang46 wants to merge 1 commit into
quangdang46 wants to merge 1 commit into
Conversation
The wham/usage and OpenAI account-usage endpoints always return
`used_percent` in [0, 100]. The previous heuristic (`raw > 1.0`)
misclassified low integer values like `used_percent: 1` (1% used) as
the already-normalized ratio `1.0` (100% used), which made the CLI
and `/usage` overlay report ChatGPT subscriptions as fully exhausted
when the live endpoint reported only a few percent consumed.
Drop the heuristic in both src/usage/openai_helpers.rs and the legacy
src/usage_openai.rs and divide by 100 unconditionally. Add two
regression tests in src/usage/tests.rs:
- direct unit coverage of the helper across 0/1/5/50/100/>100/-5/NaN
- parse_openai_usage_payload regression built from a real wham/usage
response with used_percent: 1 in the secondary window
Ports upstream PR 1jehuang#178 by
@ktmyname (Cargo.lock version-bump hunk skipped, our fork tracks its
own version).
Closes #137
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.
What
jcode usage(and the inline/usageoverlay) was reporting ChatGPTsubscriptions as fully exhausted (
100% used, exhausted bar) even whenthe live
wham/usageendpoint reported only a few percent consumed.This addresses issue #137: #137
Root cause
Both
src/usage/openai_helpers.rsand the legacysrc/usage_openai.rshad a
normalize_ratio_valuehelper that tried to auto-detect ratio-vs-percent inputs using
raw > 1.0:The OpenAI
wham/usageendpoint unambiguously reportsused_percentin
[0, 100]. A real low-usage payload (used_percent: 1) hit theelsebranch and was clamped to1.0, rendering as fully exhausted.Existing tests only exercised values like
25.0,50.0,75.0,100.0so the bug never surfaced.Changes
unconditionally. Made
normalize_ratio_valuepub(super)so thetest module can call it directly. Added a docstring explaining why
the heuristic was removed.
test_normalize_ratio_value_treats_low_integer_values_as_percentlocks the contract across
0,1,5,50,100,>100,negative, and
NaNinputs.test_parse_openai_usage_payload_reports_low_percentages_correctlyfeeds a real-shaped wham/usage payload with
used_percent: 1inthe secondary window and asserts
7-day window == 1.0.Tests
Both new tests pass; all pre-existing usage tests still pass.
Acceptance criteria
used_percentin[0, 100]nolonger collapse low values to
1.0ratio —src/usage/openai_helpers.rs:15-23src/usage_openai.rs:15-23src/usage/tests.rs:589src/usage/tests.rs:608Notes
its own version in Cargo.toml.
clippy::question_markwarning incrates/jcode-tui-mermaid/src/mermaid_cache_render.rs:169is unrelatedto this change (reproducible on master).