Skip to content

fix(upstream): handle free-tier IP block and stop retrying request-level 400s - #132

Merged
jasonxu114514 merged 2 commits into
jasonxu114514:mainfrom
cheerc:fix/free-tier-ip-abuse
Oct 20, 2026
Merged

jasonxu114514 merged 2 commits into
jasonxu114514:mainfrom
cheerc:fix/free-tier-ip-abuse

Conversation

@cheerc

@cheerc cheerc commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When using OpenCode's free tier, two distinct upstream error modes were improperly retried and amplified through the gateway's fallback tiers:

  1. 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.

  2. 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-key in zen_keys, the request failed auth 3 times, returning 401 "Upstream request failed: Invalid credential" to the client and burying the real 400 error.

Change

1. Free-tier IP abuse block (#131)

  • Classify the block (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 via peekResponseBody, keeping the response intact. Shape gate 403s are not matched.
  • Leave the lane (upstream.go): stops the anonymous walk immediately on IP block, marks the exit cooled (proxyTransport.blockedUntil, 15-minute TTL), and returns errFreeTierIPBlocked.
  • Skip the exit (pool.go): anonymousCursor.Next skips blocked exits in the first pass, #pool cycles, and cooldown fallback until the TTL expires.
  • Client response (gateway.go): returns 503 free_tier_ip_blocked with the gateway's own message instead of forwarding the upstream "turn off your VPN" text.
  • Availability probes (probe.go): records network_abuse_blocked instead of http_403.
  • Attempt counter (upstream.go): increments attempts++ in the anonymous loop (previously stayed at 0).

2. Request-level rejections & placeholder key filtering (#136)

  • Classify deterministic 4xx (internal/gateway/abuse.go): recognizes 400/422 with invalid_request_error, context_length_exceeded, maximum context length, string_above_max_length, or prompt is too long.
  • Stop on first attempt (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.
  • Ignore placeholder keys (internal/config/placeholder.go): documented example keys (sk-your-zen-key, change-this-local-key) are dropped during config Normalize(), 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/opencode2api pass.
  • End-to-end verified with real captured payloads:
    • Context-length-exceeded request returns 400 "…maximum context length allows…" on attempt 1 with key_id: anonymous.
    • Replayed IP-block response returns 503 free_tier_ip_blocked without falling through to key tiers.

fixes #131
fixes #136

🤖 Generated with Claude Code

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
cheerc force-pushed the fix/free-tier-ip-abuse branch from fb660a2 to 3e83185 Compare October 20, 2026 13:22
…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]>
@cheerc cheerc changed the title fix(upstream): treat the free-tier IP block as terminal for the exit fix(upstream): handle free-tier IP block and stop retrying request-level 400s Oct 20, 2026
@jasonxu114514
jasonxu114514 merged commit 07aa453 into jasonxu114514:main Oct 20, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants