Files
magpie/internal/gateway/request_idle_test.go
T
yetone 97f264c7fb gateway: a 413 on the OpenAI routes says request_too_large, so pi compacts (@wilsonxuuuu on X)
@wilsonxuuuu's pi, on gpt-6.1 through magpie, stopped on
"magpie API error (413): {...,"type":"api_error"}" at 114k of a 1M
window and never auto-compacted. The vendor's 413 was a body byte cap.
pi's overflow table (pi-ai utils/overflow.ts) knows a byte cap only as
request_too_large, Anthropic's type, which magpie already gave on the
Anthropic route but not on chat/responses. writeErrorSaid now types
every 413 request_too_large (Gemini keeps its own status shape). The
status stays 413 and the byte-limit hint stays; it is not turned into
context_length_exceeded, so Codex and Claude Code read it as before
(Codex reads code, not type). pi compacts once and retries once
(_overflowRecoveryAttempted), so a body that is still too big after
compacting stops instead of looping.

Run:
- pi 1.1.0's own isContextOverflow on the reporter's body: api_error
  false, request_too_large true.
- TestPayloadTooLargeSaysRequestTooLargeToPi (an nginx 413 page from a
  chat upstream) and the new Chat/Responses rows of
  TestBodyRejectionErrorShapes fail with the gateway.go change reverted
  by hand, pass with it.
- go test -tags nogui -count=1 -timeout 45m ./... under a sandbox HOME:
  every package ok. gofmt clean; GOOS=linux and windows go vet of
  internal/gateway clean.
Not tried: a real pi session hitting a real vendor 413.
2026-10-16 06:09:50 +08:00

139 lines
4.0 KiB
Go

package gateway
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
func TestRequestBodyProgressRenewsIdleDeadline(t *testing.T) {
for _, encoding := range []string{"identity", "gzip", "zstd"} {
t.Run(encoding, func(t *testing.T) {
payload := bytes.Repeat([]byte("body"), 128)
wire := encodedBody(t, payload, encoding)
s := New()
s.requestLimits.readTimeout = 200 * time.Millisecond
gw := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, ok := s.readRequestBody(w, r, provider.Responses, 0)
if !ok {
return
}
if !bytes.Equal(body, payload) {
t.Error("body changed")
}
io.WriteString(w, "ok")
}))
defer gw.Close()
conn, err := net.Dial("tcp", strings.TrimPrefix(gw.URL, "http://"))
if err != nil {
t.Fatal(err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(3 * time.Second))
fmt.Fprintf(conn, "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: %d\r\nContent-Encoding: %s\r\n\r\n", len(wire), encoding)
// Six progressing reads take longer than one idle interval.
step := (len(wire) + 5) / 6
for offset := 0; offset < len(wire); offset += step {
time.Sleep(50 * time.Millisecond)
if _, err := conn.Write(wire[offset:min(offset+step, len(wire))]); err != nil {
break
}
}
res, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("progressing upload: %d", res.StatusCode)
}
body, err := io.ReadAll(res.Body)
if err != nil || string(body) != "ok" {
t.Fatalf("response: %q, %v", body, err)
}
})
}
}
func TestAdditionalBodyReadersHaveDeadlines(t *testing.T) {
for _, api := range []string{"systemone", "mcp"} {
t.Run(api, func(t *testing.T) {
deadlines := []time.Time{}
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
if api == "systemone" {
New().serveSystemOne(deadlineRecorder{w, &deadlines}, r)
} else {
r.SetPathValue("token", "run")
b := &subscriptionBridge{runs: map[string]*subscriptionRun{"run": {}}}
b.mcpCall(deadlineRecorder{w, &deadlines}, r)
}
if len(deadlines) < 2 || deadlines[0].IsZero() || !deadlines[len(deadlines)-1].IsZero() {
t.Fatalf("body deadlines: %v", deadlines)
}
})
}
}
func TestMCPBodyTooLarge(t *testing.T) {
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
r.SetPathValue("token", "run")
r.ContentLength = 17 << 20
w := httptest.NewRecorder()
b := &subscriptionBridge{runs: map[string]*subscriptionRun{"run": {}}}
b.mcpCall(w, r)
if w.Code != 413 {
t.Fatalf("oversize MCP: %d", w.Code)
}
}
func TestMediaJSONPastOldBodyLimit(t *testing.T) {
payload := append(bytes.Repeat([]byte(" "), 64<<20), []byte(`{"model":"fake/m1","prompt":"tail"}`)...)
for _, api := range []string{"images", "videos"} {
t.Run(api, func(t *testing.T) {
r := httptest.NewRequest("POST", "/", bytes.NewReader(payload))
var prompt string
var err error
if api == "images" {
d, e := readDrawing(r)
prompt, err = d.Prompt, e
} else {
f, e := readFilming(r)
prompt, err = f.Prompt, e
}
if err != nil || prompt != "tail" {
t.Fatalf("media body: %q, %v", prompt, err)
}
})
}
}
func TestBodyRejectionErrorShapes(t *testing.T) {
for _, tc := range []struct {
proto provider.Protocol
status int
want string
}{
{provider.Anthropic, 413, `"type":"request_too_large"`},
{provider.Chat, 413, `"type":"request_too_large"`},
{provider.Responses, 413, `"type":"request_too_large"`},
{provider.Gemini, 413, `"status":"INVALID_ARGUMENT"`},
{provider.Gemini, 408, `"status":"DEADLINE_EXCEEDED"`},
} {
w := httptest.NewRecorder()
writeError(w, tc.proto, tc.status, "request body rejected")
if !strings.Contains(w.Body.String(), tc.want) {
t.Fatalf("%s/%d: %s", tc.proto, tc.status, w.Body.String())
}
}
}