Skip to content

fix(zhipu): read the quota response when it has no data envelope - #6479

Open
ntdatt812 wants to merge 1 commit into
farion1231:mainfrom
ntdatt812:fix/zhipu-quota-top-level-array
Open

ntdatt812 wants to merge 1 commit into
farion1231:mainfrom
ntdatt812:fix/zhipu-quota-top-level-array

Conversation

@ntdatt812

Copy link
Copy Markdown
Contributor

Refs #6402.

What happens

The GLM Coding Plan card renders no tiers at all for some accounts, while the request itself succeeds — pointing a custom extractor script at the same endpoint shows the quota fine. So the transport is healthy; the built-in parser is what drops the data.

Why

zhipu_quota_from_body requires body.data:

let data = match body.get("data") {
    Some(d) => d,
    None => return make_error("Missing 'data' field in response".to_string()),
};

and parse_zhipu_token_tiers then requires data.limits.

GET /api/monitor/usage/quota/limit has also been observed returning the limit list as a bare top-level array, with no envelope at all. From the dump in the issue:

[
  {"type":"CREDIT_LIMIT","unit":3,"number":5,"usage":28000,"currentValue":2585,"remaining":25414,"percentage":9,"nextResetTime":1786592963348},
  {"type":"CREDIT_LIMIT","unit":6,"number":1,"usage":140000,"currentValue":58386,"remaining":81613,"percentage":41,"nextResetTime":1786692650981}
]

Against that body body.get("data") is None, so the parser returns Missing 'data' field in response before it ever looks at the entries.

Scope note

The issue titles the CREDIT_LIMIT type filter, but that half is already fixed on main — the filter accepts CREDIT_LIMIT alongside TOKENS_LIMIT, case-insensitively. The part that still reproduces is the envelope, which the reporter flagged separately at the end of the report. This PR covers only that, hence Refs rather than Fixes.

Change

Find the limit array where it actually is, rather than pinning one envelope shape — the bare array, or a limits array on whatever object we were handed. unit-based window classification and every other behaviour is untouched.

A body with no limits anywhere still returns an error, so a genuinely broken response is not silently converted into an empty-but-successful quota.

Tests

Three added:

  • zhipu_quota_reads_a_top_level_limit_array — the reporter's real response verbatim, asserting both tiers and their percentages
  • zhipu_quota_reads_limits_without_a_data_envelope — { "limits": [...] } with no data
  • zhipu_quota_still_reports_a_body_with_no_limits_anywhere — an unrecognised body must still surface an error

Reverting the two source hunks and leaving the tests in place fails both new envelope tests with error: Some("Missing 'data' field in response") — the exact symptom from the report.

All 20 pre-existing zhipu* tests still pass unchanged.

cargo test --lib: 2480 passed, 2 failed. Both failures are codex_config::tests::resolve_catalog_rejects_symlink_escaping_config_dir and services::session_usage_grokbuild::tests::symlink_cycle_does_not_cause_stack_overflow, which panic with Os { code: 1314, "A required privilege is not held by the client." } — Windows refuses symlink creation without elevation, and they fail identically on the base commit. cargo fmt --check and cargo clippy --lib --all-features -- -D warnings are clean.

@ntdatt812
ntdatt812 requested a review from farion1231 as a code owner August 15, 2026 12:33
@github-actions github-actions Bot added the backend Backend (Rust/Tauri) label Aug 15, 2026
@farion1231

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 32e0a076c8

ℹ️ 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".

@ntdatt812
ntdatt812 force-pushed the fix/zhipu-quota-top-level-array branch from 32e0a07 to 036c6ad Compare August 27, 2026 13:51
@ntdatt812

Copy link
Copy Markdown
Contributor Author

Rebased onto main — mergeable again. The conflict was only in the test-module use list: main had added detect_provider and parse_opencode_go_tiers while this branch added zhipu_quota_from_body. Resolved as the union of both; no logic changed.

I re-checked that the fix is still needed rather than assuming. main today still has:

let data = match body.get("data") {
    Some(d) => d,
    None => return make_error("Missing 'data' field in response".to_string()),
};

which is exactly the path #6402 hits — the live response is a top-level array with no data envelope, so the user sees "Missing 'data' field" instead of their quota.

cargo check --lib --tests clean. I could not run the test binary here: linking fails on the x86_64-pc-windows-gnu toolchain because MSVC-style flags (/MANIFEST:EMBED) reach the GNU ld. That is my environment, not this branch — saying so rather than implying I ran a suite I didn't.

The GLM Coding Plan card renders nothing for some accounts even though the
request itself succeeds — a custom extractor script against the same
endpoint shows the quota fine.

`zhipu_quota_from_body` requires `body.data`, and `parse_zhipu_token_tiers`
then requires `data.limits`. `GET /api/monitor/usage/quota/limit` has also
been observed returning the limit list as a bare top-level array, with no
envelope at all:

    [
      {"type":"CREDIT_LIMIT","unit":3,"percentage":9,"nextResetTime":...},
      {"type":"CREDIT_LIMIT","unit":6,"percentage":41,"nextResetTime":...}
    ]

Against that body `body.get("data")` is None, so the parser returns
"Missing 'data' field in response" and the card has nothing to show.

Look for the limit array where it actually is, instead of pinning one
envelope shape: the bare array, or a `limits` array on whatever object we
were handed. A body with no limits anywhere still reports an error, so a
genuinely broken response is not silently turned into an empty quota.

The `CREDIT_LIMIT` half of the report is already handled — the type filter
accepts it alongside `TOKENS_LIMIT` — so this only covers the envelope.

Refs farion1231#6402
@ntdatt812
ntdatt812 force-pushed the fix/zhipu-quota-top-level-array branch from 036c6ad to a2ab436 Compare September 1, 2026 04:34

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend (Rust/Tauri)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants