Files
magpie/internal/gateway/stream_refused_test.go
T
yetone 1f99f6f0b5 provider, gateway: a backend that only streams is tested and served (Cyrus_4433 on X: claude-relay-service "400 · Stream must be set to true")
A CRS Responses endpoint, like Codex's own backend, turns away a request that
doesn't ask for stream:true. The editor's Test models and protocol detection
send one small body, so every model of such a relay was red; an agent that sent
stream:false got the same 400 through the gateway.

Probe: probeAt asks once more with stream:true when a 400/422 says the stream
must be set (provider.StreamRequired, WithStream). Chosen over streaming every
probe so a vendor that takes one body keeps being asked for one; every probe
(model test, per-model check, detect, key check) goes through probeAt.
Gateway: passthrough marks the model stream-only on that 400 for a request
asking one body and returns done=false; attempt then translates on the same
API (always streamed upstream, whole reply to the client), and asks so at once
from then on, as a refused custom tool is.

Run (mote): new tests built from the reporter's literal error, red on the old
code (TestProbeStreamsWhereTheBackendOnlyStreams, TestDetectFindsAStreamOnlyBackend,
TestWholeReplyFromAStreamOnlyBackend), green on the new; go vet darwin, windows,
linux nogui; go test ./internal/provider ./internal/gateway ok. Each fix broken
by hand (probe retry stashed, gateway hook stashed): red. Not tried: a real CRS
relay; the rest of ./... (started after the tag).
2026-11-11 10:37:13 +08:00

83 lines
3.5 KiB
Go

package gateway
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// A claude-relay-service Responses endpoint answers a request for one body
// 400 "Stream must be set to true" (Cyrus_4433 on X). An agent that sent
// stream:false gets its whole reply all the same: the request is asked
// again streamed, and the next one is asked so at once.
func TestWholeReplyFromAStreamOnlyBackend(t *testing.T) {
fresh(t)
var mu sync.Mutex
var bodies []string
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
bodies = append(bodies, string(b))
mu.Unlock()
if !gjson.GetBytes(b, "stream").Bool() {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error":{"message":"Stream must be set to true","type":"invalid_request_error"}}`)
return
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"r1","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[]}}`,
`data: {"type":"response.output_text.delta","output_index":0,"item_id":"m1","content_index":0,"delta":"pong"}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","status":"completed","content":[{"type":"output_text","text":"pong"}]}}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[{"type":"message","id":"m1","role":"assistant","status":"completed","content":[{"type":"output_text","text":"pong"}]}],"usage":{"input_tokens":3,"output_tokens":1}}}`,
))
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "relay", Name: "My Relay", Key: "k", Responses: up.URL + "/v1", Models: []string{"gpt-6-sol"}}); err != nil {
t.Fatal(err)
}
h := New().Handler()
for i := range 2 {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"relay/gpt-6-sol","stream":false,"input":"hi"}`)))
if rec.Code != 200 || gjson.Get(rec.Body.String(), "output.0.content.0.text").String() != "pong" {
t.Fatalf("ask %d: HTTP %d: %s", i, rec.Code, rec.Body.String())
}
}
mu.Lock()
defer mu.Unlock()
// the first is refused once, then asked streamed; the second is asked
// streamed at once
if len(bodies) != 3 || gjson.Get(bodies[0], "stream").Bool() || !gjson.Get(bodies[1], "stream").Bool() || !gjson.Get(bodies[2], "stream").Bool() {
t.Fatalf("asked %d times: %q", len(bodies), bodies)
}
}
// Another 400 is the client's, as before: a request for one body that is
// refused for something else is asked once.
func TestOtherRefusalsAreNotStreamOnly(t *testing.T) {
fresh(t)
n := 0
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n++
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error":{"message":"input is too long"}}`)
}))
defer up.Close()
if err := provider.Save(provider.Provider{ID: "relay", Name: "My Relay", Key: "k", Responses: up.URL + "/v1", Models: []string{"gpt-6-sol"}}); err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(`{"model":"relay/gpt-6-sol","input":"hi"}`)))
if rec.Code != 400 || n != 1 {
t.Fatalf("HTTP %d after %d asks: %s", rec.Code, n, rec.Body.String())
}
}