fix: clamp z.ai raw-percentage usedPercent fallback to 0...100#2342
fix: clamp z.ai raw-percentage usedPercent fallback to 0...100#2342OfficialAbhinavSingh wants to merge 1 commit into
Conversation
ZaiLimitEntry.usedPercent returns computedUsedPercent (already clamped) when quota fields are present, but the fallback returned self.percentage raw. z.ai omits/misreports quota fields, so an out-of-range API percentage flowed unclamped into RateWindow.usedPercent. Clamp the fallback like computedUsedPercent and sibling providers.
|
Codex review: needs real behavior proof before merge. Reviewed July 19, 2026, 12:37 PM ET / 16:37 UTC. Summary Reproducibility: yes. at source level: a Review metrics: 2 noteworthy metrics.
Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Proof guidance:
Risk before merge
Maintainer options:
Next step before merge
Security Review detailsBest possible solution: Retain the narrow clamp and regression tests, then land it after redacted runtime evidence shows the Z.ai CLI or menu path converts an out-of-range fallback response into a bounded usage value. Do we have a high-confidence way to reproduce the issue? Yes, at source level: a Is this the best way to solve the issue? Yes. Clamping at the fallback is the narrowest maintainable fix because the computed branch already bounds the same user-facing percentage and the tests preserve its precedence. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 3b4d9a85cbec. Label changesLabel changes:
Label justifications:
Evidence reviewedWhat I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
Summary
ZaiLimitEntry.usedPercentreturnscomputedUsedPercent(which already clampsmin(100, max(0, …))) when z.ai reports quota fields. But when those fields are absent it falls back toreturn self.percentage— the raw API percentage, unclamped:z.ai is known to omit/misreport quota fields (see #1855), so an out-of-range
percentageflows straight intoRateWindow.usedPercentand the menu bar — every other provider, and this struct's own computed branch, clamp to0…100.Fix
Matches
computedUsedPercent(line 150) and sibling providers. No change for in-range values.Test
TestsLinux/ZaiUsedPercentLinuxTests.swift(usage== nilforces the fallback path):150 → 100,-5 → 0(clamped)42 → 42(in-range unchanged)usage 100 / used 25 / percentage 999 → 25(raw 999 does not leak)Proof (real run)
Runs on CI in the
build-linux-cli"Swift Test (Linux only)" step. Local red→green inswift:6.3.3:Before the fix — fallback returns the raw value:
After the fix — all pass:
Build clean,
swiftlint --strict0 violations,swiftformat --lintclean.Small and self-contained — happy to adjust or close if not wanted.