Repository navigation
fix(upstream): handle free-tier IP block and stop retrying request-level 400s - #132
Merged
Merged
Conversation
The free tier answers an egress IP it flagged with 403 "abusive traffic". The gateway read that as an ordinary rejection: it tried the second anonymous lane on the same IP, then the keyed tiers from the same IP, and remembered nothing, so every later request and every probe repeated the walk. A single client request produced four upstream 403s on an instance with one exit. - classify the three known IP-block wordings (abuse.go) without consuming the body, so the upstream response stays intact for the caller; - cool only that exit (proxyTransport.blockedUntil) and leave the lane: another lane on the same IP cannot help, and neither can a keyed tier; - keep blocked exits out of anonymousCursor.Next, including its cooldown fallback, and bring them back after a 15 minute TTL; - skip the keyed tiers once the anonymous phase reports the block; - answer clients with 503 free_tier_ip_blocked instead of forwarding the upstream "turn off your VPN" text; - record probes as network_abuse_blocked instead of http_403; - count anonymous attempts, which stayed at zero and broke the telemetry attempt numbers and the request budget. Co-Authored-By: Claude Code <[email protected]>
cheerc
force-pushed
the
fix/free-tier-ip-abuse
branch
from
October 20, 2026 13:22
fb660a2 to
3e83185
Compare
…eholder keys A free-tier 400 (context_length_exceeded, invalid_request_error) was treated as retryable: the gateway walked the second anonymous lane, then the keyed tier, and the client received whatever the last tier answered. With the documented placeholder in zen_keys, a 941 KB request that only needed compaction came back as "401 Invalid credential". - classify request-level rejections (400/422 with invalid_request_error, context_length_exceeded, maximum context length, string_above_max_length or prompt is too long) and end the request after the first attempt on both the anonymous and keyed lanes, forwarding the original status and body; 401/403/429 stay retryable because they can be key-specific; - read the body once with peekResponseBody so classification does not consume what the client eventually receives; - drop documented placeholder keys (sk-your-zen-key, change-this-local-key) during config normalization: they never authenticate, and keeping them turns every free-tier fallback into a 401 that hides the real error. Verified end to end by replaying the recorded request against the built binary with the original configuration: 400 with the upstream message, one upstream attempt, an anonymous key_id, and zen/go pools of 0. Co-Authored-By: Claude Code <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using OpenCode's free tier, two distinct upstream error modes were improperly retried and amplified through the gateway's fallback tiers:
Free-tier IP abuse block (
403 … abusive traffic from this IP address) (fixes Free-tier IP abuse 403 is retried on the same exit and in the keyed tiers #131):The gateway read the 403 as an ordinary upstream rejection and retried the second anonymous lane on the same egress IP, then fell through to the keyed tiers from the same IP, remembering nothing. A single client request produced 4 upstream 403s on a single-exit gateway.
Request-level rejections (
400 context_length_exceeded,invalid_request_error) (fixes A request-level 400 (context length) is retried through every tier and reaches the client as a 401 #136):A deterministic client error (such as a request exceeding the model's context window during compaction) was treated as retryable: the gateway tried the second anonymous lane, then fell through to the Zen key tier. With the documented placeholder
sk-your-zen-keyinzen_keys, the request failed auth 3 times, returning401 "Upstream request failed: Invalid credential"to the client and burying the real 400 error.Change
1. Free-tier IP abuse block (#131)
internal/gateway/abuse.go): detects the three known IP-block wordings (abusive traffic,Detect Abuse Traffic,Abuse of traffic) and network-level phrasing. The body is peeked and replayed viapeekResponseBody, keeping the response intact. Shape gate 403s are not matched.upstream.go): stops the anonymous walk immediately on IP block, marks the exit cooled (proxyTransport.blockedUntil, 15-minute TTL), and returnserrFreeTierIPBlocked.pool.go):anonymousCursor.Nextskips blocked exits in the first pass,#poolcycles, and cooldown fallback until the TTL expires.gateway.go): returns503 free_tier_ip_blockedwith the gateway's own message instead of forwarding the upstream "turn off your VPN" text.probe.go): recordsnetwork_abuse_blockedinstead ofhttp_403.upstream.go): incrementsattempts++in the anonymous loop (previously stayed at 0).2. Request-level rejections & placeholder key filtering (#136)
internal/gateway/abuse.go): recognizes 400/422 withinvalid_request_error,context_length_exceeded,maximum context length,string_above_max_length, orprompt is too long.upstream.go): returns the 4xx immediately from both anonymous and keyed lanes without falling through to other tiers, preserving the original upstream status and error message for the client.internal/config/placeholder.go): documented example keys (sk-your-zen-key,change-this-local-key) are dropped during configNormalize(), so an unconfigured key does not create an upstream pool or trigger 401s during fallback.Verification
go test ./...passes. New unit test suites:internal/gateway/free_tier_block_test.go: 10 tests covering IP block classification, single attempt per request, no keyed retry, multi-exit failover, cursor skipping, TTL recovery, 503 response, and probe reason.internal/gateway/free_tier_error_test.go: 5 tests covering deterministic 400 stopping, 401 auth retry preservation, response classifier, and upstream status/body preservation.go vet ./...,gofmt -l,go build -trimpath ./cmd/opencode2apipass.400 "…maximum context length allows…"on attempt 1 withkey_id: anonymous.503 free_tier_ip_blockedwithout falling through to key tiers.fixes #131
fixes #136
🤖 Generated with Claude Code