Files
magpie/internal/gateway/toolong_unsaid_test.go
T
yetone 11df525f06 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 in b3ab3e42e (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 since 978284dd6).
- 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 by 978284dd6), 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 base be5e8f29f. Rebased onto e8df78f33 (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)
2026-11-06 17:25:04 +08:00

75 lines
3.8 KiB
Go

package gateway
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Some vendors don't say a conversation is too long for the model: Devin
// answered one of about 225k tokens for swe-1.7-lightning (a 202,752
// 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.
// 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
near := strings.Repeat("word ", 1760) // about 2200, which may yet fit
for _, tc := range []struct {
name, path, body string
code int
reply string
wantCode, calls int
want string
}{
// a 5xx only from Devin, which answers an overflow so
// (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"},
// 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
{"small 502", "/v1/messages", small, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 502, 1 + lastRetries + replanRounds, "capacity issues"},
{"near the window", "/v1/messages", near, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 502, 1 + lastRetries + replanRounds, "capacity issues"},
{"429", "/v1/messages", big, 429, `{"error":{"message":"Rate limit reached"}}`, 429, 1 + rateRetries + replanRounds, "Rate limit"},
{"401", "/v1/messages", big, 401, `{"error":{"message":"invalid token"}}`, 401, 1, "invalid token"},
{"quota", "/v1/messages", big, 400, `{"error":{"message":"insufficient balance"}}`, 400, 1, "insufficient balance"},
} {
t.Run(tc.name, func(t *testing.T) {
proto := provider.Anthropic
if strings.Contains(tc.path, "chat") {
proto = provider.Chat
}
f := &fake{code: tc.code, ctype: "application/json", reply: tc.reply}
up := setup(t, proto, f)
p := provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Contexts: map[string]int{"m1": 2000}}
if proto == provider.Chat {
p.Chat = up.URL + "/v1"
} else {
p.Anthropic = up.URL
}
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
req := `{"model":"fake/m1","max_tokens":10,"messages":[{"role":"user","content":"` + tc.body + `"}]}`
code, body := post(t, tc.path, req)
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 overflowTold(body) {
t.Fatalf("told as an overflow: %s", body)
}
})
}
}