ESern29751 on X (v0.1.4341): in a routing group with intent recognition, magpie often 499-cancels the intent classifier's request, with MiniMax, GPT-6 Luna and Grok 4.6 as the classifier. Cause: those models reason before they answer. The classifier asks for reasoning_effort none, but MiniMax thinks anyway and Grok 4.6 and Luna go no lower than low. Through the live gateway with the classify prompt over a real routing prompt (~5k tokens of context): grok-4.6 took 4.7-8.1s (288-462 reasoning tokens), minimax-m2.5 3.7-10.3s (439-1012 output tokens), gpt-6-luna 1.2-7.2s; cold calls reached 12-29s. The unset classifier timeout was 8 seconds, so most turns were cut off, fell back to the group's order and lost their intent routing. Each cut call was then logged 499 "the agent canceled the request", which usage counts as no failure, though no agent had canceled anything: askChat's own context.WithTimeout had no cause, the mislabel #2318 fixed for an image's description but never for the classifier. Fix: - settings.ClassifyTimeoutDefault 8 -> 15 (an existing Settings segment). A user who saved 8 keeps 8; only the unset value moves. The GUI's fallback segment follows (app.js), and the docs (en/zh/ja intent and start, gateway-routing.md) say 15. - askChat's wait is context.WithTimeoutCause(..., sideTimedOut{"decision model", wait}): the usage row says "magpie stopped waiting: its decision model had no answer in Ns", a failure. - Siblings checked: the advisor and Codex titles use the client's context, compaction and vision already carry sideTimedOut; askJev (decision providers) logs no 499 row of its own. - Tests that assumed the 8-second unset wait now use 15 (classify, decide, decide_group, nested_classify_wait, settings). - app.js grows by the one byte of the fallback's second digit: both core budgets +1 with the reason (TestCoreScriptBudget was red at exactly 1 over without it). A shorter `|| 15` would show a hand-edited negative value as its own segment, so it wasn't taken. - site: llms-full.txt and the search indexes regenerated for the docs pages (node llms-full.mjs, node search-index.mjs). Test: TestReasoningClassifierAnswersInTheWaitUnset - a classifier answering in 10 units routes the turn with the timeout unset, and one slower than 60 units gives no answer with one 499 usage row that says magpie stopped waiting for its decision model and counts as a failure. Broken by hand (mica-run): with askChat back on a plain WithTimeout the test is red ("the classifier's cut call: 499 "the agent canceled the request""); with the default back at 8 it is red ("gave no answer in 400ms"). Run at 0479372b8, this commit's tree (mica, through ~/suite.lock; mote was over its cores): - gofmt -l: clean. go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go vet: rc 0 (darwin vet rc 0 at the first try on mote). - go test -tags nogui -count=1 -timeout 45m ./...: rc 0, 103 ok. (The first run, on mote before the budget raise and the rebase ontoadfa196db, was red on TestCoreScriptBudget alone.) - node --test site/*.test.mjs: 132 of 132. - GUI (node --test, Chromium and WebKit, five languages, 1100 and 420): classify-timeout-setting, settings-places, i18n-en-no-cjk green; i18n-keys red only on "a plugin's words are its own" over claude_image_budget_test.go's ", with an image", already #6877 from2896e82cc, untouched here. Not tried: the real reporter's group in the GUI with these models after the change (the timings above were measured through the live gateway before it); the whole GUI suite (only a fallback value changed in app.js).
292 lines
11 KiB
Go
292 lines
11 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// decideFleet is System One APIs whose every key answers as status says
|
|
// (200 when unset), and the keys asked in turn.
|
|
type decideFleet struct {
|
|
mu sync.Mutex
|
|
status map[string]int
|
|
asked []string
|
|
}
|
|
|
|
func (f *decideFleet) set(key string, code int) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.status[key] = code
|
|
}
|
|
|
|
func (f *decideFleet) took() []string {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
out := f.asked
|
|
f.asked = nil
|
|
return out
|
|
}
|
|
|
|
func (f *decideFleet) serve() *httptest.Server {
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/systemone" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
|
var q struct{ Model string }
|
|
json.NewDecoder(r.Body).Decode(&q)
|
|
f.mu.Lock()
|
|
f.asked = append(f.asked, key)
|
|
code := f.status[key]
|
|
f.mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if code != 0 && code != 200 {
|
|
w.WriteHeader(code)
|
|
io.WriteString(w, `{"detail":{"message":"no capacity for `+key+`"}}`)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"model": q.Model, "answers": map[string]any{
|
|
"levels": map[string]any{"type": "noul", "noul": 0},
|
|
"intent": map[string]any{"type": "choice", "choice": "bug", "confidence": 0.9}},
|
|
"usage": map[string]int{"input_tokens": 10, "output_tokens": 2}})
|
|
}))
|
|
}
|
|
|
|
// A routing group of decision models answers /v1/systemone (ARNO on
|
|
// Discord): its keys and accounts asked in the group's order, one that
|
|
// fails as a chat's would resting and the next asked, every try in the
|
|
// routing log; asked by "group/<id>" or by the group's name.
|
|
func TestSystemOneDecisionGroupFailsOver(t *testing.T) {
|
|
fresh(t)
|
|
f := &decideFleet{status: map[string]int{}}
|
|
a, b := f.serve(), f.serve()
|
|
defer a.Close()
|
|
defer b.Close()
|
|
for _, p := range []provider.Provider{
|
|
// a decision provider alone, two keys: each one a try of its own
|
|
{ID: "ja", Name: "JA", Key: "ka1", Keys: []provider.KeyAccount{{Key: "ka2"}}, Decide: a.URL + "/v1"},
|
|
{ID: "jb", Name: "JB", Key: "kb", Decide: b.URL + "/v1"},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "jevs", Name: "Jevs", Members: []string{"ja/jev-latest", "jb/jev-latest"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
call := func(path, body string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", path, strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer magpie")
|
|
s.Handler().ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
const ask = `{"model":%q,"state":{"message":"hi"},"questions":{"intent":{"type":"choice"}}}`
|
|
f.set("ka1", 429)
|
|
f.set("ka2", 503)
|
|
rec := call("/v1/systemone", strings.Replace(ask, "%q", `"group/jevs"`, 1))
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"choice":"bug"`) {
|
|
t.Fatalf("group: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if got := strings.Join(f.took(), ","); got != "ka1,ka2,kb" {
|
|
t.Fatalf("asked %s, want ka1,ka2,kb", got)
|
|
}
|
|
if n := len(s.trace.routes); n != 1 {
|
|
t.Fatalf("traced %d", n)
|
|
}
|
|
r := s.trace.routes[0]
|
|
if r.Model != "group/jevs" || r.Group == nil || r.Provider != "jb" || !r.Done || r.Status != 200 || len(r.Tries) != 3 {
|
|
t.Fatalf("route %+v", r)
|
|
}
|
|
for i, want := range []int{429, 503, 200} {
|
|
try := r.Tries[i]
|
|
if try.Status != want || try.Model != "jev-latest" || (want != 200) != (try.Rest != nil) {
|
|
t.Errorf("try %d: %+v, want %d, a rest when it failed", i, try, want)
|
|
}
|
|
}
|
|
if len(r.Usage) != 1 || r.Usage[0].Provider != "jb" {
|
|
t.Fatalf("usage %+v", r.Usage)
|
|
}
|
|
// the two that failed rest: by the group's name, the one that answered
|
|
// goes first
|
|
rec = call("/v1/systemone", strings.Replace(ask, "%q", `"Jevs"`, 1))
|
|
if rec.Code != 200 {
|
|
t.Fatalf("by name: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if got := strings.Join(f.took(), ","); got != "kb" {
|
|
t.Fatalf("by name asked %s, want kb", got)
|
|
}
|
|
// all failing: the one not resting tried, then of those resting only
|
|
// the one back soonest (ka1, rested first, #1799), and the last
|
|
// one's answer passed on
|
|
f.set("kb", 429)
|
|
rec = call("/v1/systemone", strings.Replace(ask, "%q", `"group/jevs"`, 1))
|
|
if got := strings.Join(f.took(), ","); got != "kb,ka1" {
|
|
t.Fatalf("all failing asked %s, want kb,ka1", got)
|
|
}
|
|
if rec.Code != 429 || !strings.Contains(rec.Body.String(), "no capacity for ka1") {
|
|
t.Fatalf("all failing: %d %s", rec.Code, rec.Body)
|
|
}
|
|
// a chat request to it is told where it is asked
|
|
rec = call("/v1/chat/completions", `{"model":"group/jevs","messages":[{"role":"user","content":"hi"}]}`)
|
|
if rec.Code != 400 || !strings.Contains(rec.Body.String(), "/v1/systemone") {
|
|
t.Fatalf("chat to a decision group: %d %s", rec.Code, rec.Body)
|
|
}
|
|
}
|
|
|
|
// A decision group as a chat group's classifier fails over the same way.
|
|
func TestDecisionGroupClassifierFailsOver(t *testing.T) {
|
|
fresh(t)
|
|
f := &decideFleet{status: map[string]int{"kc": 503}}
|
|
a, b := f.serve(), f.serve()
|
|
defer a.Close()
|
|
defer b.Close()
|
|
for _, p := range []provider.Provider{
|
|
{ID: "jc", Name: "JC", Key: "kc", Decide: a.URL + "/v1"},
|
|
{ID: "jd", Name: "JD", Key: "kd", Decide: b.URL + "/v1"},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "judges", Name: "Judges", Members: []string{"jc/jev-latest", "jd/jev-latest"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
if v, err := s.askClassifier("group/judges", []string{"feature", "bug"}, before{}, false, "fix this"); err != nil || v.Intent != "bug" {
|
|
t.Fatalf("classify: %+v %v", v, err)
|
|
}
|
|
asked := f.took()
|
|
if len(asked) < 2 || asked[0] != "kc" || asked[len(asked)-1] != "kd" {
|
|
t.Fatalf("asked %v", asked)
|
|
}
|
|
// jc rests: the next classification asks jd first
|
|
if _, err := s.askClassifier("group/judges", []string{"feature", "bug"}, before{}, false, "fix this"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if asked := f.took(); len(asked) == 0 || asked[0] != "kd" {
|
|
t.Fatalf("after the rest asked %v", asked)
|
|
}
|
|
}
|
|
|
|
// One decision provider of several keys (fflake33 on Discord: TypeSafe Jev,
|
|
// Smart, a key out of quota) is asked over its keys in its routing's
|
|
// order, at /v1/systemone and as a group's classifier, as a decision
|
|
// group's members are: the key refused rests, the next answers, and the
|
|
// next request asks the one that answered first.
|
|
func TestDecisionProviderKeysFailOver(t *testing.T) {
|
|
for _, routing := range []string{"", provider.Ordered} {
|
|
t.Run("routing="+routing, func(t *testing.T) {
|
|
fresh(t)
|
|
f := &decideFleet{status: map[string]int{}}
|
|
up := f.serve()
|
|
defer up.Close()
|
|
for _, p := range []provider.Provider{
|
|
{ID: "jev", Name: "TypeSafe Jev", Key: "k1", Keys: []provider.KeyAccount{{Key: "k2"}}, Decide: up.URL + "/v1", Routing: routing},
|
|
{ID: "judge", Name: "Judge", Key: "c1", Keys: []provider.KeyAccount{{Key: "c2"}}, Decide: up.URL + "/v1", Routing: routing},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
s := New()
|
|
call := func() *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/systemone", strings.NewReader(`{"model":"jev/jev-latest","state":{"message":"hi"},"questions":{"intent":{"type":"choice"}}}`))
|
|
req.Header.Set("Authorization", "Bearer magpie")
|
|
s.Handler().ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
// the first key out of quota: the second answers
|
|
f.set("k1", 429)
|
|
if rec := call(); rec.Code != 200 || !strings.Contains(rec.Body.String(), `"choice":"bug"`) {
|
|
t.Fatalf("systemone: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if got := strings.Join(f.took(), ","); got != "k1,k2" {
|
|
t.Fatalf("asked %s, want k1,k2", got)
|
|
}
|
|
r := s.trace.routes[len(s.trace.routes)-1]
|
|
if r.Model != "jev/jev-latest" || r.Group != nil || !r.Done || r.Status != 200 || len(r.Tries) != 2 || r.Tries[0].Rest == nil || r.Tries[1].Rest != nil {
|
|
t.Fatalf("route %+v", r)
|
|
}
|
|
if len(r.Usage) != 1 || r.Usage[0].Provider != "jev" {
|
|
t.Fatalf("usage %+v", r.Usage)
|
|
}
|
|
// it rests: the next request asks the second alone
|
|
if rec := call(); rec.Code != 200 {
|
|
t.Fatalf("again: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if got := strings.Join(f.took(), ","); got != "k2" {
|
|
t.Fatalf("again asked %s, want k2", got)
|
|
}
|
|
|
|
// as a group's classifier, the same
|
|
f.set("c1", 429)
|
|
if v, err := s.askClassifier("judge/jev-latest", []string{"feature", "bug"}, before{}, false, "fix this"); err != nil || v.Intent != "bug" {
|
|
t.Fatalf("classify: %+v %v", v, err)
|
|
}
|
|
asked := f.took()
|
|
if len(asked) < 2 || asked[0] != "c1" || asked[len(asked)-1] != "c2" {
|
|
t.Fatalf("classifier asked %v, want c1 then c2", asked)
|
|
}
|
|
if _, err := s.askClassifier("judge/jev-latest", []string{"feature", "bug"}, before{}, false, "fix this"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if asked := f.took(); len(asked) == 0 || asked[0] != "c2" {
|
|
t.Fatalf("classifier after the rest asked %v, want c2 first", asked)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A decision model's keys share the one classifier's wait, as Settings
|
|
// says it is given, its failover included: a key that never answers
|
|
// spends it, and the next is not asked with a whole wait of its own. Each
|
|
// try given the whole wait held a request a wait per key before routing
|
|
// it (Jason on Discord, held 2m and stopped with a 504).
|
|
func TestDecisionTriesShareTheClassifierWait(t *testing.T) {
|
|
fresh(t)
|
|
was := classifyUnit
|
|
classifyUnit = 20 * time.Millisecond // the 15 seconds unset are 300ms
|
|
t.Cleanup(func() { classifyUnit = was })
|
|
var mu sync.Mutex
|
|
var asked []string
|
|
stop := make(chan struct{})
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
asked = append(asked, strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "))
|
|
mu.Unlock()
|
|
// never answers: the body read, so a client gone ends r's context
|
|
io.Copy(io.Discard, r.Body)
|
|
select {
|
|
case <-r.Context().Done():
|
|
case <-stop:
|
|
}
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
t.Cleanup(func() { close(stop) }) // runs before up.Close
|
|
if err := provider.Save(provider.Provider{ID: "jw", Name: "JW", Key: "w1", Keys: []provider.KeyAccount{{Key: "w2"}, {Key: "w3"}}, Decide: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
_, err := s.askClassifier("jw/jev-latest", []string{"feature", "bug"}, before{}, false, "fix this")
|
|
if err == nil || !strings.Contains(err.Error(), "no answer in 300ms") {
|
|
t.Fatalf("a decision model that never answers: %v", err)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(asked) != 1 {
|
|
t.Fatalf("asked %v in one classifier's wait, want the first key alone", asked)
|
|
}
|
|
}
|