gateway: only the vendors seen to hide an overflow have a bare failure told as too long (#5486)
NEKO-CwC asked, after v0.1.3141, why a custom API-key Responses relay's 400/413/422 is still turned into context_length_exceeded when the relay never said so. It shouldn't be. tooLongUnsaid read any provider's 400/413/422 that said nothing of length, for a request 1.2 times past the model's window, as "prompt is too long". That rule came from three vendors' refusals inb3ab3e42e(v0.1.735, 10-03): Devin's 502 "capacity issues", Qoder's 400 "Error in upstream response" and WorkBuddy's 400 "Invalid request parameters". #711 (v0.1.808) then kept the 413 case for Factory's byte cap.978284dd6(v0.1.3141) had already scoped the 5xx to Devin. The 4xx half still applied to every provider. Now: - unsaidStatus lists who gets the inference, keyed by the id of the plugin that serves the vendor (all of them are retired as built-ins), never by the provider's id, which a user's own provider can share: devin 500/502/503/504; qoder, qoder-cn, workbuddy, workbuddy-ai 400/413/422; factory 413. unsaidOverflows replaces busyOverflows. - Every other provider's failure reaches the client with its own status and words: custom and by-address providers, API-key relays, OpenAI, Anthropic, other plugins. Transport failures are never inferred (unchanged since978284dd6). - An upstream that says it overflowed (tooLongRe, overflow codes) is still passed on as an overflow. A 400/422 whose own words say the request is too long (saysLength, #4420) is still read past half the window for any vendor, because the vendor named the cause. - The single call site in serve runs for every client route (Responses, Codex's /responses, Chat, Messages) and every upstream protocol, held or streamed, before anything is sent. Mid-stream SSE errors were never inferred. Tests: - TestRelayFailureKeepsItsOwnWords (new, 126 subtests): a custom relay speaking Responses, Chat or Messages answers 400 "Invalid request parameters", 400 "Error in upstream response", 413, 422 or (Responses) 502/503 to a request past 1.2 of its window. Asked through /v1/responses (held and streamed), Codex's /responses, /v1/chat (held and streamed) and /v1/messages (held and streamed), the client gets that status and the relay's words, with no context_length_exceeded. - TestUnsaidVendorsStillOverflow (new): each listed vendor and status, under its own id and a -plugin id, is told as too long past the window and left alone within it. Another vendor's plugin, and a user provider named like the vendor, are not. Statuses a vendor wasn't seen to use are not. - TestQoderPluginUnsaidOverflow (new): through the fake plugin moved as qoder, end to end. - TestOverflowTheVendorDidntSay's 400/413 rows now expect the vendor's status and words. TestPayloadTooLargeIsNotContextOverflow and TestRegionRefusalIsNotTooLong use Factory's and WorkBuddy AI's plugin. TestWindowNotBelowWhatWasAnswered and TestWindowNotBelowListedMax now use a length-words 400, the one path a generic provider still has. Broken by hand, on mote: - the old fallback.go (origin/main) with the new tests: 84 of TestRelayFailureKeepsItsOwnWords' subtests red (every 4xx one; the 5xx ones were already fixed by978284dd6), and TestUnsaidVendorsStillOverflow, TestOverflowTheVendorDidntSay and TestPayloadTooLargeIsNotContextOverflow red; - unsaidOverflows returning false: TestUnsaidVendorsStillOverflow, TestQoderPluginUnsaidOverflow, TestDevinBusyOverflowStillTooLong, TestPayloadTooLargeIsNotContextOverflow and TestRegionRefusalIsNotTooLong red; - keyed by p.ID instead of the plugin: TestUnsaidVendorsStillOverflow (user provider named like the vendor) and TestDevinBusyOverflowStillTooLong (devin-plugin) red. Run (mote-run, at this tree): gofmt -l clean; go vet ./internal/gateway for darwin, GOOS=linux and GOOS=windows ok; go test -count=1 -timeout 45m ./internal/gateway: ok (809s) at basebe5e8f29f. Rebased ontoe8df78f33(two routing commits touched gateway.go and trace.go), then at the pushed tree: go vet ok and the overflow, too-long, payload, window and response-id tests ok (14s). Not tried: a real relay, or real Devin, Qoder, WorkBuddy or Factory accounts, against this change. A vendor not listed that hides an overflow behind a bare 400 now hands the agent that 400 rather than compacting; it gets added to unsaidStatus when it is seen. Reported-by: NEKO-CwC (GitHub), ne9ko9 (Discord)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1947,37 +1947,35 @@ func saysLength(status int, body []byte) bool {
|
||||
}
|
||||
|
||||
// tooLongUnsaid says a failure that doesn't say why was the request being too
|
||||
// long for the model, as some vendors put it: Devin's 502 "capacity
|
||||
// issues", WorkBuddy's 400 "Invalid request parameters", Qoder's 400
|
||||
// "Error in upstream response" — each for a conversation well past the
|
||||
// model's window, and answered at a size it holds. Retried, or asked of
|
||||
// another account of the model, it fails the same, and the agent, told it
|
||||
// as a vendor's error, never compacts. The message it is told instead
|
||||
// says the prompt is too long. Not for a rate limit, an account's
|
||||
// trouble, a model it doesn't serve, a region it doesn't serve, or one
|
||||
// already saying it overflowed. A request error that speaks of length
|
||||
// (saysLength) needs only lengthMargin of the window.
|
||||
// long for the model, as a few vendors put it (unsaidOverflows): Devin's
|
||||
// 502 "capacity issues", WorkBuddy's 400 "Invalid request parameters",
|
||||
// Qoder's 400 "Error in upstream response" — each for a conversation well
|
||||
// past the model's window, and answered at a size it holds (b3ab3e42e) —
|
||||
// and Factory's 413 "Request Entity Too Large" for one whose text alone is
|
||||
// past it (#711). Retried, or asked of another account of the model, it
|
||||
// fails the same, and the agent, told it as a vendor's error, never
|
||||
// compacts. The message it is told instead says the prompt is too long.
|
||||
// Not for a rate limit, an account's trouble, a model it doesn't serve, a
|
||||
// region it doesn't serve, or one already saying it overflowed.
|
||||
//
|
||||
// A 5xx is a vendor's busy or broken moment, which a client asks again,
|
||||
// and is read so only for a vendor seen to answer an overflow with one
|
||||
// (busyOverflows): a custom Responses relay's plain 502 "Upstream service
|
||||
// temporarily unavailable", at 385K tokens of a conversation it had just
|
||||
// answered at 454K, was told as more than gpt-6-astra's 272K, and Codex
|
||||
// ended the turn instead of asking again (ne9ko9 on Discord). Nor is a
|
||||
// failure that is no answer at all (transportFailed): a dial or read
|
||||
// that timed out, a connection reset, a stream cut short, each in
|
||||
// magpie's own words. held is the most the model was seen to answer for
|
||||
// this provider (Server.heldOf), and the window is never less than it,
|
||||
// nor than the most the provider's list says the model may be asked for
|
||||
// (windowMost).
|
||||
// Every other provider's failure reaches the agent as the vendor said it,
|
||||
// its status, code and body, however long the request: a custom
|
||||
// Responses relay's plain 502, and the 400 or 413 any relay, OpenAI or
|
||||
// Anthropic may give for something else entirely, aren't magpie's to
|
||||
// read as an overflow (#5486, NEKO-CwC: ne9ko9's relay had a 502 at 385K
|
||||
// told to Codex as more than gpt-6-astra's 272K, and Codex ended the
|
||||
// turn). The one exception is a request error whose own words say the
|
||||
// request is too long (saysLength), from any vendor: the vendor named
|
||||
// the cause, and lengthMargin of the window rules out the other things
|
||||
// such words are said of. Nor is a failure that is no answer at all
|
||||
// (transportFailed): a dial or read that timed out, a connection reset, a
|
||||
// stream cut short, each in magpie's own words. held is the most the
|
||||
// model was seen to answer for this provider (Server.heldOf), and the
|
||||
// window is never less than it, nor than the most the provider's list
|
||||
// says the model may be asked for (windowMost).
|
||||
func tooLongUnsaid(p provider.Provider, model string, tokens, held, status int, body []byte) (string, bool) {
|
||||
switch status {
|
||||
case 400, 413, 422:
|
||||
case 500, 502, 503, 504:
|
||||
if !busyOverflows(p) {
|
||||
return "", false
|
||||
}
|
||||
default:
|
||||
unsaid := unsaidOverflows(p, status)
|
||||
if !unsaid && status != 400 && status != 422 {
|
||||
return "", false
|
||||
}
|
||||
raw := body
|
||||
@@ -1986,6 +1984,10 @@ func tooLongUnsaid(p provider.Provider, model string, tokens, held, status int,
|
||||
if transportFailed(raw) || transportFailed(body) {
|
||||
return "", false
|
||||
}
|
||||
length := saysLength(status, raw)
|
||||
if !unsaid && !length {
|
||||
return "", false
|
||||
}
|
||||
if overflowed(status, body) || failure(status, body) != failOther || quotaWords.Match(body) ||
|
||||
unservedWords.Match(body) || refusedWords.Match(body) || provider.EdgeBlocked(body) || regionRefused(status, body) || zdrRefused(status, body) {
|
||||
return "", false
|
||||
@@ -1996,7 +1998,7 @@ func tooLongUnsaid(p provider.Provider, model string, tokens, held, status int,
|
||||
}
|
||||
window = max(window, windowMost(p, model), held)
|
||||
margin := unsaidMargin
|
||||
if saysLength(status, raw) {
|
||||
if length {
|
||||
margin = lengthMargin
|
||||
}
|
||||
if float64(tokens) < float64(window)*margin {
|
||||
@@ -2017,13 +2019,30 @@ func tooLongUnsaid(p provider.Provider, model string, tokens, held, status int,
|
||||
return fmt.Sprintf("%s (%s answered %d)", msg, p.Name, status), true
|
||||
}
|
||||
|
||||
// busyOverflows says p's vendor answers a request past its model's window
|
||||
// with a 5xx that says nothing of length: Devin's 502 "We are currently
|
||||
// experiencing capacity issues" for a conversation of about 300K tokens
|
||||
// on swe-1.7-lightning (202,752), answered at a size it holds (b3ab3e42e).
|
||||
// Devin is served by its plugin now, under its own id or another.
|
||||
func busyOverflows(p provider.Provider) bool {
|
||||
return p.ID == "devin" || buildHost(p) == "devin"
|
||||
// unsaidStatus are the statuses a vendor was seen to answer a request
|
||||
// past its model's window with, saying nothing of length, by the id of
|
||||
// the plugin that serves it now (each is retired as a built-in, so only
|
||||
// its plugin serves it): Devin's 502 "We are currently experiencing
|
||||
// capacity issues" for about 300K tokens on swe-1.7-lightning (202,752),
|
||||
// Qoder's 400 "Error in upstream response" for 1.8M on qmodel (1M) and
|
||||
// WorkBuddy's 400 "Invalid request parameters" on glm-5.1, each answered
|
||||
// at a size it holds (b3ab3e42e, v0.1.735), and Factory's 413 for a
|
||||
// request whose text alone is past the window (#711, v0.1.808). Keyed by
|
||||
// the plugin, never by the provider's id, which a user's own provider
|
||||
// may share.
|
||||
var unsaidStatus = map[string][]int{
|
||||
"devin": {500, 502, 503, 504},
|
||||
"qoder": {400, 413, 422},
|
||||
"qoder-cn": {400, 413, 422},
|
||||
"workbuddy": {400, 413, 422},
|
||||
"workbuddy-ai": {400, 413, 422},
|
||||
"factory": {413},
|
||||
}
|
||||
|
||||
// unsaidOverflows says p's vendor answers a request past its model's
|
||||
// window with status, saying nothing of length (unsaidStatus).
|
||||
func unsaidOverflows(p provider.Provider, status int) bool {
|
||||
return slices.Contains(unsaidStatus[p.PluginProvider()], status)
|
||||
}
|
||||
|
||||
// transportWords are a failure that never reached a vendor's answer, as Go,
|
||||
|
||||
@@ -33,7 +33,14 @@ func TestPayloadTooLargeIsNotContextOverflow(t *testing.T) {
|
||||
if inferred, ok := tooLongUnsaid(p, "m", tokens, 0, 413, []byte(msg)); ok {
|
||||
t.Errorf("byte limit inferred as token limit: %s", inferred)
|
||||
}
|
||||
if inferred, ok := tooLongUnsaid(p, "m", 1000, 0, 413, []byte(msg)); !ok || !strings.Contains(inferred, "prompt is too long") {
|
||||
// a 413 is read as an overflow only for Factory (#711), whose
|
||||
// plugin serves it; any other vendor's byte cap is its own
|
||||
if inferred, ok := tooLongUnsaid(p, "m", 1000, 0, 413, []byte(msg)); ok {
|
||||
t.Errorf("another vendor's byte cap inferred as a token limit: %s", inferred)
|
||||
}
|
||||
factory := provider.PluginForTest("factory", "factory")
|
||||
factory.Name, factory.Contexts = "Factory", map[string]int{"m": 100}
|
||||
if inferred, ok := tooLongUnsaid(factory, "m", 1000, 0, 413, []byte(msg)); !ok || !strings.Contains(inferred, "prompt is too long") {
|
||||
t.Errorf("text estimate past the window lost compaction: %s", inferred)
|
||||
}
|
||||
if retryable(413, []byte(msg)) {
|
||||
|
||||
@@ -42,7 +42,8 @@ func TestRegionRefusalIsRetryable(t *testing.T) {
|
||||
// A conversation past the model's window that is refused for its region
|
||||
// is told as the region refusal, not as a prompt too long to compact.
|
||||
func TestRegionRefusalIsNotTooLong(t *testing.T) {
|
||||
p := provider.Provider{ID: "fake", Name: "Fake", Contexts: map[string]int{"m": 100}}
|
||||
p := provider.PluginForTest("workbuddy-ai", "workbuddy-ai")
|
||||
p.Name, p.Contexts = "WorkBuddy AI", map[string]int{"m": 100}
|
||||
for _, body := range []string{antigravityRegion400, googleRegion400} {
|
||||
if said, ok := tooLongUnsaid(p, "m", 1000, 0, 400, []byte(body)); ok {
|
||||
t.Errorf("region refusal read as too long: %s", said)
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A custom API-key relay's failure reaches the agent as the relay said it:
|
||||
// its status and its words, whatever the request's size, on every route a
|
||||
// client asks by and every protocol the relay speaks, held or streamed.
|
||||
// magpie only translates the protocol; it never makes it an overflow the
|
||||
// relay didn't say (#5486, NEKO-CwC: ne9ko9's Responses relay had a 502 at
|
||||
// 385K told to Codex as more than gpt-6-astra's 272K, and the 400/413/422
|
||||
// inference stayed for every provider after v0.1.3141).
|
||||
func TestRelayFailureKeepsItsOwnWords(t *testing.T) {
|
||||
big := strings.Repeat("word ", 2400) // about 3000 tokens, past 1.2 of the window of 2000
|
||||
asks := []struct{ name, path, body string }{
|
||||
{"responses", "/v1/responses", `{"model":"fake/m1","input":"` + big + `"}`},
|
||||
{"responses streamed", "/v1/responses", `{"model":"fake/m1","stream":true,"input":"` + big + `"}`},
|
||||
{"codex", CodexPath + "/responses", `{"model":"fake/m1","stream":true,"input":[{"role":"user","content":[{"type":"input_text","text":"` + big + `"}]}]}`},
|
||||
{"chat", "/v1/chat/completions", `{"model":"fake/m1","messages":[{"role":"user","content":"` + big + `"}]}`},
|
||||
{"chat streamed", "/v1/chat/completions", `{"model":"fake/m1","stream":true,"messages":[{"role":"user","content":"` + big + `"}]}`},
|
||||
{"messages", "/v1/messages", `{"model":"fake/m1","max_tokens":10,"messages":[{"role":"user","content":"` + big + `"}]}`},
|
||||
{"messages streamed", "/v1/messages", `{"model":"fake/m1","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"` + big + `"}]}`},
|
||||
}
|
||||
type failing struct {
|
||||
status int
|
||||
reply string
|
||||
said string
|
||||
protos []provider.Protocol
|
||||
}
|
||||
all := []provider.Protocol{provider.Responses, provider.Chat, provider.Anthropic}
|
||||
for _, fl := range []failing{
|
||||
{400, `{"error":{"message":"Invalid request parameters","type":"invalid_request_error"}}`, "Invalid request parameters", all},
|
||||
{400, `{"error":{"message":"Error in upstream response"}}`, "Error in upstream response", all},
|
||||
{413, `{"error":{"message":"Request Entity Too Large"}}`, "Request Entity Too Large", all},
|
||||
{422, `{"error":{"message":"Unprocessable request"}}`, "Unprocessable request", all},
|
||||
// the reporter's 502, and a busy 503: asked again, then handed on
|
||||
{502, `{"code":"upstream_error","message":"Upstream service temporarily unavailable"}`, "Upstream service temporarily unavailable", []provider.Protocol{provider.Responses}},
|
||||
{503, `{"error":{"message":"We are experiencing capacity issues"}}`, "capacity issues", []provider.Protocol{provider.Responses}},
|
||||
} {
|
||||
for _, proto := range fl.protos {
|
||||
for _, ask := range asks {
|
||||
t.Run(fmt.Sprintf("%d %s/%s via %s", fl.status, fl.said, proto, ask.name), func(t *testing.T) {
|
||||
f := &fake{code: fl.status, ctype: "application/json", reply: fl.reply}
|
||||
up := setup(t, proto, f)
|
||||
p := provider.Provider{ID: "fake", Name: "Relay", Key: "k", Models: []string{"m1"}, Contexts: map[string]int{"m1": 2000}}
|
||||
switch proto {
|
||||
case provider.Chat:
|
||||
p.Chat = up.URL + "/v1"
|
||||
case provider.Responses:
|
||||
p.Responses = up.URL + "/v1"
|
||||
default:
|
||||
p.Anthropic = up.URL
|
||||
}
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, body := post(t, ask.path, ask.body)
|
||||
if code != fl.status || !strings.Contains(body, fl.said) || overflowTold(body) {
|
||||
t.Fatalf("want %d with the relay's words, got %d after %d calls: %s", fl.status, code, f.calls, body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The vendors seen to answer a request past the model's window without
|
||||
// saying so, each served by its plugin now, still have it told as too
|
||||
// long, at once (b3ab3e42e, #711): under the plugin's own id or another.
|
||||
// The same failure from a plugin of any other vendor, or from a user's
|
||||
// provider that took the vendor's id, is the vendor's error.
|
||||
func TestUnsaidVendorsStillOverflow(t *testing.T) {
|
||||
cases := []struct {
|
||||
vendor string
|
||||
status int
|
||||
body string
|
||||
}{
|
||||
{"devin", 502, `{"error":{"message":"We are currently experiencing capacity issues. Please try again later."}}`},
|
||||
{"devin", 503, `{"error":{"message":"Service unavailable"}}`},
|
||||
{"qoder", 400, `{"error":{"message":"Error in upstream response"}}`},
|
||||
{"qoder-cn", 400, `{"error":{"message":"Error in upstream response"}}`},
|
||||
{"workbuddy", 400, `{"error":{"message":"Invalid request parameters"}}`},
|
||||
{"workbuddy-ai", 400, `{"error":{"message":"Invalid request parameters"}}`},
|
||||
{"workbuddy-ai", 422, `{"error":{"message":"Invalid request parameters"}}`},
|
||||
{"factory", 413, `{"error":{"message":"Request Entity Too Large"}}`},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
for _, id := range []string{tc.vendor, tc.vendor + "-plugin"} {
|
||||
p := provider.PluginForTest(id, tc.vendor)
|
||||
p.Name, p.Contexts = tc.vendor, map[string]int{"m": 2000}
|
||||
if said, ok := tooLongUnsaid(p, "m", 3000, 0, tc.status, []byte(tc.body)); !ok || !strings.Contains(said, "prompt is too long") {
|
||||
t.Errorf("%s (%s) %d: past the window: %q %v", id, tc.vendor, tc.status, said, ok)
|
||||
}
|
||||
if said, ok := tooLongUnsaid(p, "m", 1000, 0, tc.status, []byte(tc.body)); ok {
|
||||
t.Errorf("%s (%s) %d: within the window: %s", id, tc.vendor, tc.status, said)
|
||||
}
|
||||
}
|
||||
other := provider.PluginForTest("fakeco", "fakeco")
|
||||
other.Name, other.Contexts = "Fakeco", map[string]int{"m": 2000}
|
||||
own := provider.Provider{ID: tc.vendor, Name: tc.vendor, Contexts: map[string]int{"m": 2000}}
|
||||
for _, p := range []provider.Provider{other, own} {
|
||||
if said, ok := tooLongUnsaid(p, "m", 3000, 0, tc.status, []byte(tc.body)); ok {
|
||||
t.Errorf("%s, not %s's plugin, %d: told as too long: %s", p.ID, tc.vendor, tc.status, said)
|
||||
}
|
||||
}
|
||||
}
|
||||
// a status a vendor wasn't seen to say it with stays its error
|
||||
devin := provider.PluginForTest("devin", "devin")
|
||||
devin.Contexts = map[string]int{"m": 2000}
|
||||
factory := provider.PluginForTest("factory", "factory")
|
||||
factory.Contexts = map[string]int{"m": 2000}
|
||||
qoder := provider.PluginForTest("qoder", "qoder")
|
||||
qoder.Contexts = map[string]int{"m": 2000}
|
||||
for _, x := range []struct {
|
||||
p provider.Provider
|
||||
status int
|
||||
}{{devin, 400}, {factory, 400}, {factory, 502}, {qoder, 502}} {
|
||||
if said, ok := tooLongUnsaid(x.p, "m", 3000, 0, x.status, []byte(`{"error":{"message":"Something went wrong"}}`)); ok {
|
||||
t.Errorf("%s %d: told as too long: %s", x.p.ID, x.status, said)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// End to end through Qoder's plugin: its 400 "Error in upstream response"
|
||||
// for a request past fake-gemini's 3000 (the fake plugin's model whose
|
||||
// window the catalog has) reaches the agent as an overflow, and a
|
||||
// short one's as Qoder said it.
|
||||
func TestQoderPluginUnsaidOverflow(t *testing.T) {
|
||||
f := &fake{t: t, code: 400, ctype: "application/json", reply: `{"error":{"message":"Error in upstream response"}}`}
|
||||
movedFake(t, "qoder", f)
|
||||
s := New()
|
||||
code, body := postTo(t, s, "/v1/chat/completions", `{"model":"qoder/fake-gemini","messages":[{"role":"user","content":"`+strings.Repeat("word ", 3200)+`"}]}`)
|
||||
if code != 400 || !strings.Contains(body, "context_length_exceeded") {
|
||||
t.Fatalf("past the window: %d %s", code, body)
|
||||
}
|
||||
code, body = postTo(t, s, "/v1/chat/completions", `{"model":"qoder/fake-gemini","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 400 || overflowTold(body) || !strings.Contains(body, "Error in upstream response") {
|
||||
t.Fatalf("short: %d %s", code, body)
|
||||
}
|
||||
}
|
||||
@@ -123,30 +123,31 @@ func TestUnreachableIsNotTooLong(t *testing.T) {
|
||||
|
||||
// A model that has answered a prompt holds one of that size, whatever its
|
||||
// window is said to be: gpt-6-astra, said at 272K, answered 454K and 474K
|
||||
// in the reporter's session. A failure that doesn't say why, of a request
|
||||
// no larger, is the vendor's error, not an overflow; one well past what
|
||||
// was answered still is.
|
||||
// in the reporter's session. A refusal that says the request is too long
|
||||
// (saysLength, the one any vendor's is read by), of a request no larger,
|
||||
// is the vendor's error, not an overflow; one well past what was answered
|
||||
// still is.
|
||||
func TestWindowNotBelowWhatWasAnswered(t *testing.T) {
|
||||
fresh(t)
|
||||
invalid := `{"error":{"message":"Invalid request parameters"}}`
|
||||
long := `{"error":{"message":"Request too large. Please shorten it and try again."}}`
|
||||
answered := `{"id":"ok","choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5000,"completion_tokens":1,"total_tokens":5001}}`
|
||||
s := &scripted{replies: []reply{{400, "", invalid}, {200, "", answered}, {400, "", invalid}, {400, "", invalid}}}
|
||||
s := &scripted{replies: []reply{{400, "", long}, {200, "", answered}, {400, "", long}, {400, "", long}}}
|
||||
roomyMember(t, "relay", 2000, s)
|
||||
srv := New()
|
||||
ask := func(words int) (int, string) {
|
||||
return postAs(t, srv, "", `{"model":"relay/m","messages":[{"role":"user","content":"`+strings.Repeat("word ", words)+`"}]}`)
|
||||
}
|
||||
// nothing answered yet: past the window, it overflowed
|
||||
if code, body := ask(2400); code != 400 || !overflowTold(body) {
|
||||
// nothing answered yet: about 2000, past half the window of 2000
|
||||
if code, body := ask(1600); code != 400 || !overflowTold(body) {
|
||||
t.Fatalf("before an answer: %d %s", code, body)
|
||||
}
|
||||
if code, body := ask(10); code != 200 {
|
||||
t.Fatalf("the answer of 5000: %d %s", code, body)
|
||||
}
|
||||
if code, body := ask(2400); code != 400 || overflowTold(body) || !strings.Contains(body, "Invalid request parameters") {
|
||||
t.Fatalf("after an answer of 5000, at about 3000: %d %s", code, body)
|
||||
if code, body := ask(1600); code != 400 || overflowTold(body) || !strings.Contains(body, "Request too large") {
|
||||
t.Fatalf("after an answer of 5000, at about 2000: %d %s", code, body)
|
||||
}
|
||||
// about 7500, past 5000 by more than the margin
|
||||
// about 7500, past half of 5000
|
||||
if code, body := ask(6000); code != 400 || !overflowTold(body) {
|
||||
t.Fatalf("well past what was answered: %d %s", code, body)
|
||||
}
|
||||
@@ -157,8 +158,8 @@ func TestWindowNotBelowWhatWasAnswered(t *testing.T) {
|
||||
// max_context_window of 872K above its 272K context_window, which Codex
|
||||
// is handed and works to (ne9ko9 on Discord: Codex's window 828,400).
|
||||
func TestWindowNotBelowListedMax(t *testing.T) {
|
||||
invalid := `{"error":{"message":"Invalid request parameters"}}`
|
||||
f := &fake{code: 400, ctype: "application/json", reply: invalid}
|
||||
long := `{"error":{"message":"Request too large. Please shorten it and try again."}}`
|
||||
f := &fake{code: 400, ctype: "application/json", reply: long}
|
||||
up := setup(t, provider.Chat, f)
|
||||
if err := catalog.SaveLive("fake", up.URL+"/v1", []catalog.Model{{ID: "m1", Context: 2000, MaxContext: 8000}}); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -178,10 +179,11 @@ func TestWindowNotBelowListedMax(t *testing.T) {
|
||||
ask := func(words int) (int, string) {
|
||||
return post(t, "/v1/chat/completions", `{"model":"fake/m1","messages":[{"role":"user","content":"`+strings.Repeat("word ", words)+`"}]}`)
|
||||
}
|
||||
// about 3000: past half of 2000, under half of 8000
|
||||
if code, body := ask(2400); code != 400 || overflowTold(body) {
|
||||
t.Fatalf("about 3000, within the max: %d %s", code, body)
|
||||
}
|
||||
// about 10000, past the max by more than the margin
|
||||
// about 10000, past half the max
|
||||
if code, body := ask(8000); code != 400 || !overflowTold(body) || !strings.Contains(body, "8000") {
|
||||
t.Fatalf("past the max: %d %s", code, body)
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
// window) with a 502 "capacity issues", WorkBuddy one for glm-5.1 with a
|
||||
// 400 "Invalid request parameters", Qoder one of 2M for qmodel with a 400
|
||||
// "Error in upstream response" — each answered at a size the model holds.
|
||||
// Asked again in place three times, the agent was then handed a 502 and
|
||||
// never compacted. A request well past the model's window that fails so
|
||||
// is told as one too long, at once.
|
||||
// Those vendors' are still told as too long (TestUnsaidVendorsStillOverflow).
|
||||
// The same failures from any other provider, past the window or not, reach
|
||||
// the agent with the vendor's own status and words (#5486, NEKO-CwC).
|
||||
func TestOverflowTheVendorDidntSay(t *testing.T) {
|
||||
big := strings.Repeat("word ", 2400) // about 3000 tokens as estimated
|
||||
small := strings.Repeat("word ", 100) // about 125
|
||||
@@ -30,10 +30,12 @@ func TestOverflowTheVendorDidntSay(t *testing.T) {
|
||||
// (TestDevinBusyOverflowStillTooLong): a plain provider's is its
|
||||
// busy moment, asked again (ne9ko9 on Discord)
|
||||
{"502 plain", "/v1/messages", big, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 502, 1 + lastRetries + replanRounds, "capacity issues"},
|
||||
{"400 chat", "/v1/chat/completions", big, 400, `{"error":{"message":"Invalid request parameters"}}`, 400, 1, "context_length_exceeded"},
|
||||
// Text alone well past the window is enough to ask for compaction.
|
||||
{"413 bytes", "/v1/messages", big, 413, `{"error":{"message":"Request Entity Too Large"}}`, 400, 1, "prompt is too long"},
|
||||
{"413 request_too_large", "/v1/messages", big, 413, `{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`, 400, 1, "prompt is too long"},
|
||||
// nor a 400 or 413 that says nothing of length: only the vendors
|
||||
// seen to answer an overflow so (TestUnsaidVendorsStillOverflow)
|
||||
// have it read as one (#5486, NEKO-CwC)
|
||||
{"400 chat", "/v1/chat/completions", big, 400, `{"error":{"message":"Invalid request parameters"}}`, 400, 1, "Invalid request parameters"},
|
||||
{"413 bytes", "/v1/messages", big, 413, `{"error":{"message":"Request Entity Too Large"}}`, 413, 1, "Request Entity Too Large"},
|
||||
{"413 request_too_large", "/v1/messages", big, 413, `{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`, 413, 1, "Request exceeds the maximum size"},
|
||||
// not the conversation's length: a plain provider's busy reply is
|
||||
// asked again past the same-one retries, within its patience,
|
||||
// until replan's rounds run out
|
||||
@@ -64,10 +66,8 @@ func TestOverflowTheVendorDidntSay(t *testing.T) {
|
||||
if code != tc.wantCode || !strings.Contains(body, tc.want) || f.calls != tc.calls {
|
||||
t.Fatalf("status %d after %d calls (want %d after %d): %s", code, f.calls, tc.wantCode, tc.calls, body)
|
||||
}
|
||||
if tc.wantCode != 400 || tc.name == "quota" {
|
||||
if strings.Contains(body, "prompt is too long") || strings.Contains(body, "context_length_exceeded") {
|
||||
t.Fatalf("told as an overflow: %s", body)
|
||||
}
|
||||
if overflowTold(body) {
|
||||
t.Fatalf("told as an overflow: %s", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user