OpenCode's gateway answers some of s-leeq's turn-start requests with a
400 whose body is {"model":"deepseek-v4.1-flash"} and nothing else: no
error, message, code, type or param, nothing in or out, no reply id.
retryable() read it as the request's own fault, so a group stopped there
and a lone provider's turn ended on it, with a 400 nothing retries. Their
log ties it to the request, not the key: 30 of 214 requests carrying
~246K tokens of tools failed so, none of 420 under 1K, and the same
request was answered 200 some 13s to 25s after the first refusal.
openCodeBareRefusal (ported from ggbdpq's patch on the issue) reads a
400 from an OpenCode host whose body is a JSON object naming no error
field as the 502 it stands for (asBadGateway). A group asks its next
member; a lone provider is asked again within its patience (60s, with
replan pauses), which outlasts the reporter's window. Any 400 that names
something, a body that isn't a JSON object, and every other vendor's
400 go on as they came.
Changed from the patch:
- restAfterMarked rests nobody for this 502. The patch rested the key
for fallbackCooldown in a group (a scratch test showed ocgo resting
60s), over a refusal tied to request content: one heavy prompt would
have set aside a key that serves every other request.
- The body is read through a 64KB limit and put back with a
MultiReader, so nothing is cut; a body at the limit is left alone.
The patch read it whole.
- TestOpenCodeGoBare400ToldAs502 asserts at least 3 asks, not exactly
3: on current main the lone provider's patience asks 15 times.
Run on mote:
- At the pushed head (on c272e5582): gofmt -l clean; go vet
./internal/gateway for darwin, linux and windows; go test
./internal/gateway -run 'OpenCode|Rest|Fallback|Patience|Retry' ok (83s).
- On 576b5dcbc, before main took four more gateway commits
(82ff88933..c272e5582): go test ./internal/gateway -count=1
-timeout 45m ok (615.8s).
- TestOpenCodeGoBare400AskedAgain, ToldAs502, RestsNobody (new: a group
of OpenCode Go and another provider; the first request's bare 400 is
served by the other, the next request still reaches OpenCode) and
TestOpenCodeGoError400Untouched pass.
- Before the rebase, the package was red only in
TestClaudeCallerEnvironmentInAMessageIsTheLastGiven, which 576b5dcbc
fixed on main since.
Broken by hand:
- rest exemption off (`if false &&`): RestsNobody red, "OpenCode asked
1, the other 2".
- openCodeBareRefusal taken out of forwardOnce: AskedAgain, ToldAs502
and RestsNobody red; AskedAgain shows the issue's exact 400.
- A real-time scratch test (refusals for 25s, 2.5s each, the real
retryPause, lone provider) got its 200 after 32s, asked 6 times.
Deleted after.
Not tried: a real OpenCode Go key or the reporter's DeepSeek Harness; the
bodies are the issue's bytes. A turn whose refusals outlast 60s still
ends, now as a 502 the client can retry.
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
182 lines
6.5 KiB
Go
182 lines
6.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// openCodeBareBody is what OpenCode's gateway answered s-leeq's turn-start
|
|
// requests (yetone/magpie#3740): a 400 whose body names the model and
|
|
// nothing else — no error in it, no reason, no request id.
|
|
const openCodeBareBody = `{"model":"deepseek-v4.1-flash"}`
|
|
|
|
// openCodeServed is a turn the gateway answers.
|
|
const openCodeServed = `{"id":"gw-1","object":"chat.completion","model":"deepseek-v4.1-flash","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":1,"total_tokens":6}}`
|
|
|
|
// bareOpenCode answers the first refusals requests with the bare 400,
|
|
// then the rest with served; beyond that, replies[i], the last one after
|
|
// that. It counts what it was asked.
|
|
type bareOpenCode struct {
|
|
mu sync.Mutex
|
|
refusals int
|
|
replies []bareReply
|
|
asked int
|
|
}
|
|
|
|
// bareReply is an answer of the gateway's, with the status it came as.
|
|
type bareReply struct {
|
|
code int
|
|
body string
|
|
}
|
|
|
|
func (b *bareOpenCode) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
if r.Body != nil {
|
|
io.Copy(io.Discard, r.Body)
|
|
r.Body.Close()
|
|
}
|
|
b.mu.Lock()
|
|
i := b.asked
|
|
b.asked++
|
|
b.mu.Unlock()
|
|
var x bareReply
|
|
switch {
|
|
case i < b.refusals:
|
|
x = bareReply{400, openCodeBareBody}
|
|
case i-b.refusals < len(b.replies):
|
|
x = b.replies[i-b.refusals]
|
|
default:
|
|
x = b.replies[len(b.replies)-1]
|
|
}
|
|
return &http.Response{StatusCode: x.code, Status: statusText(x.code),
|
|
Header: http.Header{"Content-Type": {"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(x.body)), Request: r}, nil
|
|
}
|
|
|
|
func statusText(code int) string {
|
|
if code == 200 {
|
|
return "200 OK"
|
|
}
|
|
return "400 Bad Request"
|
|
}
|
|
|
|
func (b *bareOpenCode) went() int {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
return b.asked
|
|
}
|
|
|
|
func openCodeGoOf(t *testing.T, b http.RoundTripper) *Server {
|
|
t.Helper()
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
if err := provider.Save(provider.Provider{ID: "ocgo", Name: "OpenCode Go", Key: "k",
|
|
Chat: "https://opencode.ai/zen/go/v1", Models: []string{"deepseek-v4.1-flash"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pause := retryPause
|
|
retryPause = time.Millisecond
|
|
t.Cleanup(func() { retryPause = pause })
|
|
s := New()
|
|
s.client = &http.Client{Transport: b}
|
|
return s
|
|
}
|
|
|
|
// postChat asks the gateway as s-leeq's client did, on /v1/chat/completions.
|
|
func postChat(t *testing.T, s *Server) (int, string) {
|
|
t.Helper()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions",
|
|
strings.NewReader(`{"model":"ocgo/deepseek-v4.1-flash","messages":[{"role":"user","content":"hello"}]}`))
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
return rec.Code, rec.Body.String()
|
|
}
|
|
|
|
// The gateway refusing a turn's first request while the backend it names
|
|
// has nothing cached for it yet, then serving the same request (s-leeq's
|
|
// timeline: four 400s over 13s, a 200 of the same prompt 28s in) is the
|
|
// gateway's fault of a moment, not the request's: it is asked again, and
|
|
// the answer reaches the client. Read as the 400 it came as, nothing
|
|
// retries it: the caller's turn ended on it (#3740).
|
|
func TestOpenCodeGoBare400AskedAgain(t *testing.T) {
|
|
b := &bareOpenCode{refusals: 1, replies: []bareReply{{200, openCodeServed}}}
|
|
code, out := postChat(t, openCodeGoOf(t, b))
|
|
if code != 200 || !strings.Contains(out, `"ok"`) || b.went() != 2 {
|
|
t.Fatalf("%d %q, asked %d times", code, out, b.went())
|
|
}
|
|
}
|
|
|
|
// A bare 400 the retries don't outlive is the gateway's refusal before
|
|
// the model was asked, not the request's own fault — said as the 502 it
|
|
// stands for, which a caller retries, not as a 400 that says the request
|
|
// was at fault, which nothing retries (#3740).
|
|
func TestOpenCodeGoBare400ToldAs502(t *testing.T) {
|
|
b := &bareOpenCode{refusals: 99, replies: []bareReply{{200, openCodeServed}}}
|
|
code, out := postChat(t, openCodeGoOf(t, b))
|
|
if code != http.StatusBadGateway || !strings.Contains(out, "not an API reply") || strings.Contains(out, `"ok"`) {
|
|
t.Fatalf("%d %q", code, out)
|
|
}
|
|
if b.went() < 3 { // asked again: the passing retries, then patience
|
|
t.Fatalf("asked %d times", b.went())
|
|
}
|
|
}
|
|
|
|
// The bare 400 is about the request (s-leeq: 30 of 214 requests with
|
|
// ~246K tokens of tools, none of 420 with under 1K): in a group the next
|
|
// member serves it, and the OpenCode key is set aside for nobody, so the
|
|
// next request goes to it as before (#3740).
|
|
func TestOpenCodeGoBare400RestsNobody(t *testing.T) {
|
|
b := &bareOpenCode{refusals: 1, replies: []bareReply{{200, openCodeServed}}}
|
|
s := openCodeGoOf(t, b)
|
|
other := 0
|
|
s.client = &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
|
|
if provider.OpenCodeURL(r.URL.String()) {
|
|
return b.RoundTrip(r)
|
|
}
|
|
io.Copy(io.Discard, r.Body)
|
|
other++
|
|
return &http.Response{StatusCode: 200, Status: "200 OK", Header: http.Header{"Content-Type": {"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(openCodeServed)), Request: r}, nil
|
|
})}
|
|
if err := provider.Save(provider.Provider{ID: "oth", Name: "Other", Key: "k",
|
|
Chat: "https://other.example/v1", Models: []string{"deepseek-v4.1-flash"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "ds", Name: "DS", Routing: provider.Ordered,
|
|
Members: []string{"ocgo/deepseek-v4.1-flash", "oth/deepseek-v4.1-flash"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ask := func(say string) int {
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions",
|
|
strings.NewReader(`{"model":"ds","messages":[{"role":"user","content":"`+say+`"}]}`))
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, req)
|
|
return rec.Code
|
|
}
|
|
if code := ask("the big one"); code != 200 || b.went() != 1 || other != 1 {
|
|
t.Fatalf("first: %d, OpenCode asked %d, the other %d", code, b.went(), other)
|
|
}
|
|
if code := ask("a small one"); code != 200 || b.went() != 2 || other != 1 {
|
|
t.Fatalf("second: %d, OpenCode asked %d, the other %d: the key rested for a bare 400", code, b.went(), other)
|
|
}
|
|
}
|
|
|
|
// A 400 that names anything — here the session OpenCode answers a request
|
|
// without — is an error, and goes to the caller as it came.
|
|
func TestOpenCodeGoError400Untouched(t *testing.T) {
|
|
b := &bareOpenCode{replies: []bareReply{{400, openCodeMissing}}}
|
|
code, out := postChat(t, openCodeGoOf(t, b))
|
|
if code != http.StatusBadRequest || !strings.Contains(out, "x-opencode-session") {
|
|
t.Fatalf("%d %q", code, out)
|
|
}
|
|
if b.went() != 1 {
|
|
t.Fatalf("asked %d times", b.went())
|
|
}
|
|
}
|