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)
96 lines
4.0 KiB
Go
96 lines
4.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// googleRegion400 is Google's answer from a place it doesn't serve the API
|
|
// in, as Gemini, Code Assist and Antigravity send it.
|
|
const googleRegion400 = `{"error":{"code":400,"message":"User location is not supported for the API use.","status":"FAILED_PRECONDITION"}}`
|
|
|
|
// antigravityRegion400 is #1621's reproduction, the error as the agent was
|
|
// shown it.
|
|
const antigravityRegion400 = `{"error":{"message":"Antigravity: User location is not supported for the API use."}}`
|
|
|
|
// A refusal for where the request comes from is not about the request: it
|
|
// is retryable at 400, and the boundaries #1621 lists stay as they were.
|
|
func TestRegionRefusalIsRetryable(t *testing.T) {
|
|
for _, x := range []struct {
|
|
status int
|
|
body string
|
|
want bool
|
|
}{
|
|
{400, antigravityRegion400, true},
|
|
{400, googleRegion400, true},
|
|
{400, `{"error":{"message":"user location is NOT supported for the API use"}}`, true},
|
|
{400, `{"error":{"code":"unsupported_country_region_territory","message":"Country, region, or territory not supported","type":"request_forbidden"}}`, true},
|
|
{400, `{"error":{"message":"location field is not supported"}}`, false},
|
|
{400, `{"error":{"message":"Invalid value for location parameter"}}`, false},
|
|
{400, `{"error":{"message":"messages: field required"}}`, false},
|
|
{200, antigravityRegion400, false},
|
|
} {
|
|
if got := retryable(x.status, []byte(x.body)); got != x.want {
|
|
t.Errorf("retryable(%d, %s) = %v, want %v", x.status, x.body, got, x.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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.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)
|
|
}
|
|
}
|
|
// the same size, refused without saying why, still is
|
|
if _, ok := tooLongUnsaid(p, "m", 1000, 0, 400, []byte(`{"error":{"message":"Invalid request parameters"}}`)); !ok {
|
|
t.Error("an unsaid overflow is no longer inferred")
|
|
}
|
|
}
|
|
|
|
// #1621: a group's member refused for its region hands the request to the
|
|
// next member, and rests not at all (#5182): not as one out of credit or
|
|
// quota, nor as any failure.
|
|
func TestGroupFailsOverARegionRefusal(t *testing.T) {
|
|
for _, body := range []string{antigravityRegion400, googleRegion400} {
|
|
fresh(t)
|
|
a := &scripted{replies: []reply{{400, "", body}}}
|
|
b := &scripted{replies: []reply{{200, "", chatOK}}}
|
|
scriptedOn(t, "a", provider.Chat, a)
|
|
scriptedOn(t, "b", provider.Chat, b)
|
|
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
code, got := postAs(t, s, "", `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 || !strings.Contains(got, "hello") || a.n != 1 || b.n != 1 {
|
|
t.Fatalf("%d %s (a %d, b %d)", code, got, a.n, b.n)
|
|
}
|
|
r := s.trace.routes[len(s.trace.routes)-1]
|
|
if len(r.Tries) != 2 || r.Tries[0].Status != 400 || r.Tries[0].Rest == nil {
|
|
t.Fatalf("tries %+v", r.Tries)
|
|
}
|
|
if rest := r.Tries[0].Rest; rest.Why != failOther || rest.By != "off" || rest.Failures != 0 || time.Until(rest.Until) > 0 {
|
|
t.Fatalf("rested %+v", rest)
|
|
}
|
|
}
|
|
}
|
|
|
|
// #1621: a provider refused for its region goes to its fallback.
|
|
func TestFallbackOnRegionRefusal(t *testing.T) {
|
|
plan := &fake{t: t, ctype: "application/json", code: 400, reply: antigravityRegion400}
|
|
spare := &fake{t: t, ctype: "application/json", reply: `{"id":"from-spare","choices":[]}`}
|
|
twoProviders(t, plan, spare)
|
|
code, body := post(t, "/v1/chat/completions", chatReq)
|
|
if code != 200 || !strings.Contains(body, "from-spare") {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
}
|