Files
opencode2api/internal/config/placeholder.go
T
CheerCandClaude Code fa9a25577e fix(upstream): stop retrying request-level rejections and ignore placeholder 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 <noreply@anthropic.com>
2026-10-20 21:22:25 +08:00

50 lines
1.4 KiB
Go

package config
import "strings"
// placeholderKeys are the example values the shipped documentation uses. They
// are not secrets and no upstream accepts them: a configuration that still
// carries one has simply not been filled in yet.
var placeholderKeys = []string{
"sk-your-zen-key",
"change-this-local-key",
}
// IsPlaceholderKey reports whether key is a documented placeholder rather than
// a real credential. Comparison is case-insensitive and ignores surrounding
// whitespace.
func IsPlaceholderKey(key string) bool {
trimmed := strings.ToLower(strings.TrimSpace(key))
for _, placeholder := range placeholderKeys {
if trimmed == placeholder {
return true
}
}
return false
}
// DropPlaceholderKeys removes placeholder credentials from the key pools. A
// placeholder can never authenticate, so keeping it only turns every fallback
// that reaches the keyed tiers into a 401 — which then hides the real error
// (the cause of the failover) from the client. Callers still see the configured
// list through the management API; the pools simply behave as if the tier was
// left empty.
func DropPlaceholderKeys(cfg Config) Config {
drop := func(keys []string) []string {
if len(keys) == 0 {
return keys
}
kept := keys[:0]
for _, key := range keys {
if IsPlaceholderKey(key) {
continue
}
kept = append(kept, key)
}
return kept
}
cfg.ZenKeys = drop(cfg.ZenKeys)
cfg.GoKeys = drop(cfg.GoKeys)
return cfg
}