Skip to content

fix(usage): OpenAI percent normalization for low values (#137)#174

Open
quangdang46 wants to merge 1 commit into
masterfrom
fix/openai-usage-percent-normalization
Open

fix(usage): OpenAI percent normalization for low values (#137)#174
quangdang46 wants to merge 1 commit into
masterfrom
fix/openai-usage-percent-normalization

Conversation

@quangdang46
Copy link
Copy Markdown
Owner

What

jcode usage (and the inline /usage overlay) was reporting ChatGPT
subscriptions as fully exhausted (100% used, exhausted bar) even when
the live wham/usage endpoint reported only a few percent consumed.

This addresses issue #137: #137

Root cause

Both src/usage/openai_helpers.rs and the legacy src/usage_openai.rs
had a normalize_ratio_value helper that tried to auto-detect ratio-vs-
percent inputs using raw > 1.0:

if raw > 1.0 {
    (raw / 100.0).clamp(0.0, 1.0)
} else {
    raw.clamp(0.0, 1.0)   // bug: maps 1.0 -> 1.0 (100% used)
}

The OpenAI wham/usage endpoint unambiguously reports used_percent
in [0, 100]. A real low-usage payload (used_percent: 1) hit the
else branch and was clamped to 1.0, rendering as fully exhausted.
Existing tests only exercised values like 25.0, 50.0, 75.0,
100.0 so the bug never surfaced.

Changes

  • src/usage/openai_helpers.rs: drop the heuristic; divide by 100
    unconditionally. Made normalize_ratio_value pub(super) so the
    test module can call it directly. Added a docstring explaining why
    the heuristic was removed.
  • src/usage_openai.rs: same fix in the legacy duplicate.
  • src/usage/tests.rs: two new regression tests:
    • test_normalize_ratio_value_treats_low_integer_values_as_percent
      locks the contract across 0, 1, 5, 50, 100, >100,
      negative, and NaN inputs.
    • test_parse_openai_usage_payload_reports_low_percentages_correctly
      feeds a real-shaped wham/usage payload with used_percent: 1 in
      the secondary window and asserts 7-day window == 1.0.

Tests

$ cargo test -p jcode --lib usage::tests::
test result: ok. 38 passed; 0 failed; 0 ignored

Both new tests pass; all pre-existing usage tests still pass.

Acceptance criteria

  • Live wham/usage payloads with used_percent in [0, 100] no
    longer collapse low values to 1.0 ratio — src/usage/openai_helpers.rs:15-23
  • Same fix applied to legacy src/usage_openai.rs:15-23
  • Regression test for the helper locking the contract — src/usage/tests.rs:589
  • Regression test for the full parse path with a realistic payload — src/usage/tests.rs:608
  • All existing usage tests continue to pass

Notes

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
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