gateway: a byte cap after the stream has begun says request_too_large, so Pi compacts (#2533)
A vendor's 413 that came after the stream's headers had gone out (a keepalive sent while the vendor was quiet) reached Pi as a stream error of type api_error saying "413 Request Entity Too Large". Pi compacts only when the error's text says request_too_large (overflow.ts), so the turn stopped. A 413 before the headers was already typed request_too_large (writeErrorSaid,97f264c7). streamError, which every failure in a stream already begun goes through (failTo and holdWriter's), appends "(request_too_large)" to the text of a 413 that isn't a context overflow and doesn't say it already; the Chat and Anthropic encoders type that event request_too_large. Gemini keeps its own status shape; an overflow stays context_length_exceeded. Review change (on top of ggbdpq's 8a278645): the test matched request_too_large anywhere in the body, so Chat's new type alone passed it: with the text append taken out by hand it stayed green. It now reads the error event's message (what Pi reads) and type. The new TestStreamErrorByteCapInEveryShape runs streamError for Chat, Anthropic, Responses and Gemini, and for a 413 that says the prompt is too long. Run: - With gateway.go, chat.go and anthropic.go put back: both tests FAIL. With only the text append taken out: both FAIL. With only Anthropic's type taken out: TestStreamErrorByteCapInEveryShape FAILs ("anthropic … want "type":"request_too_large""). Restored: ok. - TestBodyRejectionErrorShapes, TestStreamErrorSaysItPasses, TestGroupLastFailureInStreamSaysItPasses: ok. - Full suite, go test -tags nogui -timeout 45m ./... (set -o pipefail, sandbox HOME, load ~350) at 0224e826f (these four commits onebf3f48f7) plus #2545's patch: every package ok except internal/agent, whose one red (TestRekeyGivesWSLAgentTheSharingKey) is #2545's, which was dropped (problems posted on #2545). internal/agent alone at 0224e826f: ok. Rebased ondccfefdf7(#2635, internal/library only): build, vet, internal/library ok. gofmt -l clean; go vet darwin (gui), GOOS=linux and GOOS=windows -tags nogui: clean. GUI (i18n-keys, gui-de/ja/zh-TW, import-*, key-import, login-import): all pass except login-import's Chromium case, which fails the same alone on origin/mainebf3f48f7(#1307, output posted there). Not tried: a real nginx 413 or a real Pi compacting after it. Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
committed by
yetone
co-authored by
ggbdpq
parent
7ea0e047bc
commit
08400f4ebd
File diff suppressed because one or more lines are too long
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
@@ -1522,7 +1523,12 @@ func (e *anthropicEncoder) event(ev Event) {
|
||||
e.index++
|
||||
case KError:
|
||||
e.close()
|
||||
failed := map[string]any{"type": "api_error", "message": ev.Text}
|
||||
typ := "api_error"
|
||||
if ev.Status == http.StatusRequestEntityTooLarge && ev.Code != "context_length_exceeded" {
|
||||
// the same type a 413 JSON body carries (writeErrorSaid)
|
||||
typ = "request_too_large"
|
||||
}
|
||||
failed := map[string]any{"type": typ, "message": ev.Text}
|
||||
if ev.Code != "" {
|
||||
failed["code"] = ev.Code // preserve the upstream error type, including refusals
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"maps"
|
||||
"mime"
|
||||
"net"
|
||||
"net/http"
|
||||
"path"
|
||||
"regexp"
|
||||
"slices"
|
||||
@@ -1434,7 +1435,12 @@ func (e *chatEncoder) event(ev Event) {
|
||||
"function": map[string]any{"arguments": ev.Text}}}}, nil, nil)
|
||||
}
|
||||
case KError:
|
||||
failed := map[string]any{"message": ev.Text, "type": "api_error"}
|
||||
typ := "api_error"
|
||||
if ev.Status == http.StatusRequestEntityTooLarge && ev.Code != "context_length_exceeded" {
|
||||
// the same type a 413 JSON body carries (writeErrorSaid)
|
||||
typ = "request_too_large"
|
||||
}
|
||||
failed := map[string]any{"message": ev.Text, "type": typ}
|
||||
if ev.Code != "" {
|
||||
failed["code"] = ev.Code // preserve the upstream error type, including refusals
|
||||
}
|
||||
|
||||
@@ -6497,10 +6497,22 @@ func failTo(w http.ResponseWriter, a *keptAlive, proto provider.Protocol, status
|
||||
func streamError(w http.ResponseWriter, proto provider.Protocol, status int, msg string) {
|
||||
sw := newSSEWriter(w)
|
||||
sw.begun = true
|
||||
ev := Event{Kind: KError, Text: saysPassing(status, msg), Status: status}
|
||||
if tooLong(status, msg) {
|
||||
text := saysPassing(status, msg)
|
||||
ev := Event{Kind: KError, Text: text, Status: status}
|
||||
overflow := tooLong(status, msg)
|
||||
if overflow {
|
||||
ev.Code = "context_length_exceeded"
|
||||
}
|
||||
// A 413 before the headers go out is typed request_too_large
|
||||
// (writeErrorSaid). Once they have, Pi reads only this event's text
|
||||
// (#2372) and does not compact on "Request Entity Too Large". Gemini
|
||||
// keeps its own status shape.
|
||||
byteCap := status == http.StatusRequestEntityTooLarge && !overflow
|
||||
ownShape := proto == provider.Gemini
|
||||
alreadySaid := strings.Contains(strings.ToLower(text), "request_too_large")
|
||||
if byteCap && !ownShape && !alreadySaid {
|
||||
ev.Text = text + " (request_too_large)"
|
||||
}
|
||||
makeEncoder(proto, sw, &Request{}).event(ev)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,13 @@ package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
@@ -99,3 +101,69 @@ func TestGroupLastFailureInStreamSaysItPasses(t *testing.T) {
|
||||
t.Fatalf("the stream's error %q is one Pi doesn't ask again for: %q", said, body)
|
||||
}
|
||||
}
|
||||
|
||||
// A vendor byte cap that lands after the stream's headers have already
|
||||
// gone out (a keepalive while the vendor is still quiet) reaches the
|
||||
// agent as an error event. Pi compacts when that text says
|
||||
// request_too_large (overflow.ts), the same word a 413 JSON body carries
|
||||
// before the stream starts. "Request Entity Too Large" does not, so the
|
||||
// turn stops (@wilsonxuuuu, once the stream is already open).
|
||||
func TestStreamedByteCapSaysRequestTooLargeToPi(t *testing.T) {
|
||||
quietFast(t)
|
||||
fresh(t)
|
||||
const nginx = "<html>\r\n<head><title>413 Request Entity Too Large</title></head>\r\n<body>\r\n<center><h1>413 Request Entity Too Large</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n"
|
||||
piOverflow := regexp.MustCompile(`(?i)request_too_large`)
|
||||
hang := make(chan struct{})
|
||||
scriptedOn(t, "a", provider.Chat, &scripted{hang: hang, replies: []reply{{413, "text/html", nginx}}})
|
||||
heard := make(chan struct{})
|
||||
go func() {
|
||||
select {
|
||||
case <-heard:
|
||||
case <-time.After(5 * time.Second):
|
||||
}
|
||||
close(hang)
|
||||
}()
|
||||
code, body, _ := askChat(t, New(), "a/m", heard)
|
||||
if !strings.Contains(body, ": keepalive") {
|
||||
t.Fatalf("the stream's headers never went out ahead of the 413: %d %q", code, body)
|
||||
}
|
||||
// Pi reads the error's message, not its type: the word is in the text
|
||||
var msg, typ string
|
||||
for _, line := range strings.Split(body, "\n") {
|
||||
if d, ok := strings.CutPrefix(line, "data: "); ok && gjson.Get(d, "error").Exists() {
|
||||
msg, typ = gjson.Get(d, "error.message").String(), gjson.Get(d, "error.type").String()
|
||||
}
|
||||
}
|
||||
if !piOverflow.MatchString(msg) || typ != "request_too_large" || strings.Contains(body, "context_length_exceeded") {
|
||||
t.Fatalf("byte cap in a stream already begun: %d %s", code, body)
|
||||
}
|
||||
}
|
||||
|
||||
// Every shape a stream already begun says a 413 in (#2533): the text names
|
||||
// request_too_large for Chat, Anthropic and Responses, and Chat's and
|
||||
// Anthropic's type is it; Gemini keeps its own status shape; a 413 that
|
||||
// says the context is too long stays context_length_exceeded.
|
||||
func TestStreamErrorByteCapInEveryShape(t *testing.T) {
|
||||
const capped = "A: 413 Request Entity Too Large"
|
||||
for _, c := range []struct {
|
||||
proto provider.Protocol
|
||||
msg string
|
||||
text, kind string // what the event's message and type say
|
||||
}{
|
||||
{provider.Chat, capped, "request_too_large", `"type":"request_too_large"`},
|
||||
{provider.Anthropic, capped, "request_too_large", `"type":"request_too_large"`},
|
||||
{provider.Responses, capped, "request_too_large", `"code":"server_error"`},
|
||||
{provider.Gemini, capped, "", `"status"`},
|
||||
{provider.Chat, "A: 413 prompt is too long: 300000 tokens > 200000 maximum", "", `"code":"context_length_exceeded"`},
|
||||
} {
|
||||
rec := httptest.NewRecorder()
|
||||
streamError(rec, c.proto, http.StatusRequestEntityTooLarge, c.msg)
|
||||
body := rec.Body.String()
|
||||
if !strings.Contains(body, c.kind) {
|
||||
t.Errorf("%s %q: want %s in %s", c.proto, c.msg, c.kind, body)
|
||||
}
|
||||
if said := strings.Contains(body, "(request_too_large)"); said != (c.text != "") {
|
||||
t.Errorf("%s %q: the text says request_too_large: %v, want %v: %s", c.proto, c.msg, said, c.text != "", body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user