Skip to content

fix(gateway): classify "insufficient credits" as a missing balance - #379

Open
sunnysab wants to merge 1 commit into
ThinkWatchProject:mainfrom
sunnysab:fix/no-balance-insufficient-credits
Open

sunnysab wants to merge 1 commit into
ThinkWatchProject:mainfrom
sunnysab:fix/no-balance-insufficient-credits

Conversation

@sunnysab

Copy link
Copy Markdown
Contributor

What

NO_BALANCE in crates/tw-gateway/src/failure.rs now recognises "insufficient credits".

Why

Reported in #378: an ordered strategy group never failed over. The upstream (a relay) answered:

400 {"message":"You have insufficient credits to make this request. Please purchase more credits to continue using the service.","type":"invalid_request_error","code":"BAD_REQUEST"}

classify() checked the body against NO_BALANCE first — but the list knew insufficient balance and credit balance is too low, not insufficient credits. Nothing matched, so the 400 fell through to ClientError: the error went back to the client as-is, the upstream was neither swapped for the next one nor paused, and every retry hit the same dead upstream until it was disabled by hand.

With the new phrases the same body is Failed(Cause::NoBalance) → next candidate, plus no_balance_pause_secs on the used-up one.

The three phrases are insufficient credit, not enough credit and have enough credit — the last one because the contracted form ("you don't have enough credits") misses a phrase starting with "not".

Tests

a_missing_balance_is_recognised_whatever_the_status gains the exact 400 body from the report and a "don't have enough credits" variant.

Note

I could not run cargo test/clippy locally (no wasm clang on this machine, tw-plugin's build script stops the workspace build). cargo fmt --all -- --check passes. CI is the real check here.

A relay that answered `400 {"code":"BAD_REQUEST"}` with "You have
insufficient credits to make this request." matched none of the
`NO_BALANCE` phrases: the list knew "insufficient balance" and "credit
balance is too low", not "insufficient credits". The verdict fell through
to `ClientError`, so the request went back to the client, the upstream was
never swapped and never paused — the client retried the same dead
upstream until the operator disabled it by hand.

Add the credit phrasings, including "don't have enough credits" (the
contracting variant a phrase starting with "not enough" misses).
Copilot AI balanced review requested due to automatic review settings October 28, 2026 06:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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.

2 participants