jiabubu on Discord: a channel that errs once and answers a moment later had the conversation moved off it at the first error, under every rule, and the next one was sent it all uncached. Only the last candidate left (or Strict's lone one) was ever asked again. Now when the conversation is kept where it is (Affinity.Kept: Auto with a warm cache, Session, or within a turn) and that key or account fails in a way that passes (busy: overloaded 503/529, another 5xx that says nothing of quota, 408), and not as a 429, quota, refusal or sign-in, it is asked once more after retryPause (or a Retry-After up to longestPause) without resting. Once per request; failing again it rests and the next is asked as before. A conversation nobody answered yet, or under Stays Off, moves at once (keptAgain in affinity.go). The trace marks the try (Try.Kept) and the Routing page says it was asked again for what its vendor cached, not "nobody else is left to ask", in en, zh, zh-TW, ja and de. TestFixedMemberEffort and TestHeldRuleFailsOverWithinItsMember had a kept member fail with a lasting 500; they now expect it asked once more (kq1 kq1 kq2) before it moves, the outcome unchanged. Run on mote (mote-run): - TestKeptConversationAskedAgainBeforeItMoves (7 cases: Auto, Use before expiry, Session, failing twice, 429, new conversation, Off), TestFixedMemberEffort, TestHeldRuleFailsOverWithinItsMember: ok. - go test -count=1 -timeout 45m ./internal/gateway/: all ok but TestGroupBackupStandingInWhileTheFirstIsBackAndBehind and TestGroupMemberPutFirstOnItsMeritsKeepsItsCache, red with keptAgain switched off too: #5209. - routing-kept-again.test.cjs: 20 of 20 (chromium + webkit, en zh zh-TW ja de, 1100 and 420 wide). - GUI routing-*, group-*, i18n*, gui-* (2967 tests, concurrency 4): 42 red, all in files this doesn't touch; 14 of those files alone: 28 red, and the same files at origin/main7dd2434d3alone: 29 red, the same tests (mote at load 58-118). Filed: #5088, #4585, #4627, #4628, #3954, #1804. - gofmt -l clean; go vet ./internal/gateway darwin, GOOS=linux -tags nogui, GOOS=windows: ok. At the head rebased on78128b510: vet (darwin, linux, windows), go test -run 'TestKeptConversation…|TestFixedMemberEffort|TestHeldRule…| Affinity|Strict|Patience' ./internal/gateway: ok; routing-kept-again, i18n-keys, routing-agent-gone: 40 of 40. Broken by hand: keptAgain's call made false: the four kept cases of the Go test fail (b answers, a asked once); the story branch made false: routing-kept-again 0 of 20. Not tried: a real vendor's 503 on a real account; Linux/Windows runs of the tests; the full ./... suite before this push (runs after the tag).
135 lines
4.3 KiB
Go
135 lines
4.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// failsNext is an Anthropic Messages upstream that answers its next fails
|
|
// requests with status (an overload's 503 or a rate limit's 429), then as
|
|
// cacheWriter does, writing the whole prompt to its cache.
|
|
type failsNext struct {
|
|
cacheWriter
|
|
mu sync.Mutex
|
|
fails int
|
|
status int
|
|
asked int
|
|
}
|
|
|
|
func (f *failsNext) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
f.asked++
|
|
fail := f.fails > 0
|
|
if fail {
|
|
f.fails--
|
|
}
|
|
status := f.status
|
|
f.mu.Unlock()
|
|
if !fail {
|
|
f.cacheWriter.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if status == http.StatusTooManyRequests {
|
|
io.WriteString(w, `{"type":"error","error":{"type":"rate_limit_error","message":"rate limited"}}`)
|
|
return
|
|
}
|
|
io.WriteString(w, `{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}`)
|
|
}
|
|
|
|
func (f *failsNext) failNext(n, status int) {
|
|
f.mu.Lock()
|
|
f.fails, f.status, f.asked = n, status, 0
|
|
f.mu.Unlock()
|
|
}
|
|
|
|
func (f *failsNext) askedTimes() int {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return f.asked
|
|
}
|
|
|
|
// A conversation kept on a member for what its vendor cached of it, whose
|
|
// member fails once in a way that passes (an overloaded 503), is asked of
|
|
// that member again after a moment rather than moved at once to the next,
|
|
// which would be sent it all uncached (jiabubu on Discord: a channel that
|
|
// errs once and is back a moment later, and every rule moved the
|
|
// conversation off it at the first error). Asked again once only: failing
|
|
// again, it moves. A rate limit moves at once as before, and so does a
|
|
// conversation nobody has answered yet, which has no cache to keep.
|
|
func TestKeptConversationAskedAgainBeforeItMoves(t *testing.T) {
|
|
const (
|
|
first = `{"model":"group/g","messages":[{"role":"user","content":"hi"}]}`
|
|
again = `{"model":"group/g","messages":[{"role":"user","content":"hi"},{"role":"assistant","content":"yo"},{"role":"user","content":"more"}]}`
|
|
)
|
|
for _, c := range []struct {
|
|
name string
|
|
routing string
|
|
affinity string
|
|
fails int
|
|
status int
|
|
fresh bool // the failing request opens a conversation of its own
|
|
want string
|
|
aAsked int
|
|
}{
|
|
{"overloaded once, Auto", "", "", 1, 503, false, "a", 2},
|
|
{"overloaded once, Use before expiry", provider.Pace, "", 1, 503, false, "a", 2},
|
|
{"overloaded once, Session", "", provider.AffinitySession, 1, 503, false, "a", 2},
|
|
{"overloaded twice", "", "", 2, 503, false, "b", 2},
|
|
{"rate limited", "", "", 1, 429, false, "b", 1},
|
|
{"a new conversation", "", "", 1, 503, true, "b", 1},
|
|
{"Stays Off", "", provider.AffinityOff, 1, 503, false, "b", 1},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
fresh(t)
|
|
pause := retryPause
|
|
retryPause = time.Millisecond
|
|
t.Cleanup(func() { retryPause = pause })
|
|
a := &failsNext{cacheWriter: cacheWriter{wrote: 3000}}
|
|
b := &failsNext{cacheWriter: cacheWriter{wrote: 3000}}
|
|
au, bu := httptest.NewServer(a), httptest.NewServer(b)
|
|
t.Cleanup(au.Close)
|
|
t.Cleanup(bu.Close)
|
|
for _, p := range []provider.Provider{
|
|
{ID: "pa", Name: "A", Key: "a-key", Models: []string{"m"}, Anthropic: au.URL + "/v1"},
|
|
{ID: "pb", Name: "B", Key: "b-key", Models: []string{"m"}, Anthropic: bu.URL + "/v1"},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "g", Name: "G", Members: []string{"pa/m", "pb/m"}, Routing: c.routing, Affinity: c.affinity}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
if code, body := postAs(t, s, "s1", first); code != 200 || !strings.Contains(body, "from a-key") {
|
|
t.Fatalf("first turn %d %s, want a's answer", code, body)
|
|
}
|
|
a.failNext(c.fails, c.status)
|
|
session, body := "s1", again
|
|
if c.fresh {
|
|
session, body = "s2", first
|
|
}
|
|
code, got := postAs(t, s, session, body)
|
|
if code != 200 || !strings.Contains(got, "from "+c.want+"-key") {
|
|
t.Fatalf("next request %d %s, want %s's answer", code, got, c.want)
|
|
}
|
|
if n := a.askedTimes(); n != c.aAsked {
|
|
t.Fatalf("a asked %d times, want %d", n, c.aAsked)
|
|
}
|
|
if c.want == "a" && b.askedTimes() != 0 {
|
|
t.Fatalf("b asked %d times, want none", b.askedTimes())
|
|
}
|
|
})
|
|
}
|
|
}
|