fix(usage): OpenAI percent normalization for low values (#137)#174
Merged
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
zombi3butt
pushed a commit
to zombi3butt/jcode
that referenced
this pull request
May 22, 2026
The fork's CI has never run (`gh api repos/quangdang46/jcode/actions/runs`
returns total_count=0 for both pull_request and push events) because
upstream's workflows assume a `DEPLOY_KEY` SSH secret that the fork
does not have.
Two changes:
1. Gate every `webfactory/ssh-agent@v0.9.0` step on
`if: ${{ secrets.DEPLOY_KEY != '' }}` so the SSH agent setup is
simply skipped when the secret is absent. This is safe here because
none of this fork's Cargo git dependencies use SSH URLs:
- `agentgrep` -> https://github.com/1jehuang/agentgrep.git
- `mermaid-rs-renderer` -> https://github.com/1jehuang/mermaid-rs-renderer.git
So the `actions/checkout@v4` step's empty `ssh-key` falls back to
the GITHUB_TOKEN HTTPS path automatically. Applies to 5 jobs in
ci.yml + 1 step in release.yml.
2. Add `workflow_dispatch:` to ci.yml so maintainers can trigger CI
manually from the Actions tab while debugging without having to
force-push a no-op commit.
Note for the fork owner: GitHub disables Actions on a freshly-forked
repo until you visit the Actions tab once and click 'I understand my
workflows, go ahead and enable them'. After this PR is merged AND the
fork's Actions are enabled, future PRs will automatically run CI.
Refs the absence of CI runs blocking PRs quangdang46#174-quangdang46#195.
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).