Skip to content

fix(gateway): balance review fixes before 0.70.0 - #323

Merged
fylorn merged 1 commit into
mainfrom
fix/balance-review
Oct 10, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/balance-review

Conversation

@fylorn

@fylorn fylorn commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Fixes from the pre-release review of upstream balances (#321, #322; unreleased, protocol 46). Each finding was checked against the code, and for the third-party shapes against the providers' public API docs or source, before it was changed.

What changes

1. OpenRouter keys without a limit no longer fail. /api/v1/credits answers only management keys (an ordinary key gets 403), so most OpenRouter upstreams showed "the upstream rejected the key". The reader now calls only GET /api/v1/key:

  • limit / limit_remaining → quota when a limit is set (used = limit − remaining, as before);
  • usage_monthly → spent this month (usage → spent in total when the monthly figure is missing);
  • expires_at → expires.

OpenRouter no longer reports a wallet. Tests use recorded shapes for a limited and an unlimited key and assert /api/v1/credits is never requested.

2. An aborted read no longer stops background reads for that upstream. Tracker::claim returns a Claim guard. Claim::settle records the result. Dropping a Claim without settling it (the task was cancelled or panicked) releases the upstream and puts back the "a request ended" mark the claim cleared. refresh_balance runs the read in a spawned task and awaits it, so a client that disconnects no longer cancels the read: it still lands and emits balance_updated. Tests: a dropped claim gives the upstream back (unit); a refresh whose caller is aborted mid-read still finishes; a background read aborted mid-read leaves the upstream to the timer, which reads it again. Both integration tests fail on the old code.

3. Sub2API "spent today" is what the key was charged. The reader prefers usage.today.actual_cost (after the group rate) and falls back to cost.

4. New API amounts carry the site's real unit. New API's billing endpoints convert by the site's quota display type, so the amounts are not always dollars. The reader asks the public GET /api/status, without sending the key:

  • data.quota_display_type: USD → USD, CNY → CNY, TOKENS → tokens. CUSTOM → USD, because the billing endpoints convert custom display to dollars (the custom rate is applied only in the web UI).
  • Older New API and One API have only data.display_in_currency: true → USD, false → tokens.
  • If the site answers but does not say, the unit is unknown (an honest plain number, not dollars). If /api/status fails outright (connect, timeout, 5xx, 429), the read fails and the previous reading is kept.
  • Unlimited marker: New API writes exactly 100000000 for an unlimited key in every display mode. It is taken as >= 1e8 for money, but only as == 1e8 for tokens or an unknown unit, so a real token limit above it stays a quota.

5. A reload can no longer send the key without its proxy. Background reads and refreshes take the provider config and its HTTP client from one runtime snapshot. A provider with no client in that snapshot is skipped instead of falling back to the default client.

6. A 3xx is an answer, and refresh reports why detection is undecided.

  • Detection treats a 3xx as an answer. The outbound client does not follow redirects, so a redirect to a login page means the endpoint is not there, and that probe counts as Neither.
  • When detection is undecided because a probe failed, POST /providers/{name}/balance/refresh answers 502 with that failure's existing gw.balance.* message instead of 404 control.balance_none. Detection::Undecided now carries the reason.

7. Check connection waits at most 5 s for the balance. If the balance is not read by then, the test result is returned without one. A saved upstream is read in the background as usual.

Protocol

CONTROL_API_VERSION stays 46: all of this is inside the unreleased 46.

  • POST /providers/{name}/balance/refresh can now answer 502 with a gw.balance.* message (detection undecided).
  • BalanceQuota.unit and Spent.currency can be unknown (New API site that does not say) and tokens (New API site in token mode). Documented on the types.
  • OpenRouter readings have no wallet; they carry quota (if limited) and spent (period month, or total).

New messages

None. The refresh 502 reuses the existing gw.balance.* codes.

Verification

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • env -u HTTP_PROXY -u HTTPS_PROXY -u http_proxy -u https_proxy cargo test --workspace
  • cargo test -p twcore --test msg_codes (no code changes)
  • ./scripts/smoke.sh: 82 passed, 0 failed

No real third-party service was called. All reads in the tests go to local fake servers.

🤖 Generated with Claude Code

OpenRouter reads only GET /api/v1/key. The account endpoint
(/api/v1/credits) answers only management keys, so every upstream whose
key had no limit showed "the upstream rejected the key". A key with a
limit is a quota as before; every key also reports what it spent
(usage_monthly, or usage when that is missing) and its expires_at. No
wallet is reported for OpenRouter any more.

A read no longer leaves an upstream "running" forever. Tracker::claim now
returns a Claim guard: Claim::settle records the result, and dropping it
unsettled (a cancelled task, a panic) gives the upstream back to the
timer, restoring the "a request ended" mark it took. The refresh endpoint
runs the read in its own task, so a client that goes away no longer
cancels it; the read still lands and emits balance_updated.

Sub2API's "spent today" is usage.today.actual_cost (what the key was
charged after the group rate), falling back to cost.

New API converts its billing endpoints by the site's quota display type,
not always to dollars. The reader now asks the public GET /api/status
(no key sent): quota_display_type USD/CUSTOM -> USD, CNY -> CNY,
TOKENS -> tokens; older New API and One API only have
display_in_currency (true -> USD, false -> tokens). A site that answers
without saying is labelled "unknown" rather than USD; a status request
that fails outright fails the read and keeps the previous reading. The
unlimited marker (exactly 100000000, written in every display mode) is
taken as ">= 1e8" for money but only "== 1e8" for tokens or unknown, so a
real token limit above it stays a quota.

Background reads take the provider config and its client from one
runtime snapshot; a reload in between could pair the key with the
default client and skip its proxy. A provider without a client in that
snapshot is skipped.

Detection counts a 3xx as an answer (the outbound client does not follow
redirects, so a login-page redirect means the endpoint is not there).
When detection is undecided because a probe failed, the refresh endpoint
answers 502 with that gw.balance.* reason instead of
404 control.balance_none.

Check connection waits at most 5 s for the balance and otherwise returns
the test result without one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 439e0d8 into main Oct 10, 2026
5 checks passed
@fylorn
fylorn deleted the fix/balance-review branch October 10, 2026 16:17
@fylorn fylorn mentioned this pull request Oct 10, 2026
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