Files
magpie/internal/gateway/streamless_member_test.go
T
yetoneandggbdpq df341e7671 gateway: a member that answers a stream request with a whole reply is asked once, not again and again (#2455)
An upstream that answers stream:true with a whole JSON reply (a relay
that doesn't speak SSE) fails as a 502 "did not stream". magpie read
that 502 as a failure a moment may mend: the same-one retries (passing)
asked it twice more, then a group's patience (busy, #1418) kept asking
it until the budget was spent. The failure is the same however often it
is asked. With production pauses that is about 7 requests and 60s at the
default Patience, about 14 at 240s; the test's 1ms pauses let the 12
replan rounds run out first, 15 requests (ggbdpq's correction on the
issue).

busy() and passing() now leave a body that says didNotStream alone, and
the four places that word the error (translate, streamTranslated,
askTranslated, holdWriter.settle) share that constant. A group still
moves on to the next member, whose API may stream; retryable() is
unchanged.

Patch by ggbdpq (f9e09ca7), applied on main with the docs sentence
placed by hand (the hunk no longer applied).

Run:
- TestStreamlessMemberIsAskedOnce with both exclusions taken out: FAIL,
  whole and stream ("asked 15 times"); with only busy()'s: FAIL ("asked
  13 times"); with only passing()'s: FAIL ("asked 3 times"). Restored:
  ok (asked once, the agent gets the 502 naming "did not stream").
- TestStreamlessMemberHandsOnToTheNextMember (b, which streams, answers
  after a): ok.
- TestQuietNotAReplyLastFails, TestPatience*, TestStreamOverflow*,
  TestCodexOverflowInGroupNotAskedAgain, TestAgentGoneInTheRetryPause,
  TestRelayInputCap, TestOverflowGoesToAMemberWithRoom: ok.
- Full suite, go test -tags nogui -timeout 45m ./... (set -o pipefail,
  sandbox HOME, load ~350) at 0224e826f (these four commits on
  ebf3f48f7) plus #2545's patch: every package ok except internal/agent,
  whose one red (TestRekeyGivesWSLAgentTheSharingKey) is #2545's, which
  was dropped (problems posted on #2545). internal/agent alone at
  0224e826f: ok. Rebased on dccfefdf7 (#2635, internal/library only):
  build, vet, internal/library ok. gofmt -l clean; go vet darwin (gui),
  GOOS=linux and GOOS=windows -tags nogui: clean. GUI (i18n-keys,
  gui-de/ja/zh-TW, import-*, key-import, login-import): all pass except
  login-import's Chromium case, which fails the same alone on origin/main
  ebf3f48f7 (#1307, output posted there).
Not changed: a same-protocol passthrough whose vendor answers 200 with
a whole reply is still relayed as it came (named on the issue). Not
tried against a real relay that doesn't stream.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-17 11:54:56 +08:00

73 lines
3.0 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A member that answers a stream request with a whole JSON reply — a relay
// that doesn't speak SSE — fails the same way every time it is asked: the
// request is never read as a stream, however often it goes out. It is a
// failure of how the member answers, not of the moment it is asked in, so
// the group's patience doesn't keep asking it, and the one left isn't tried
// again on the chance a moment mends it. The next member of a group is
// asked, as its API may stream (#1418's patience is for what passes).
func TestStreamlessMemberIsAskedOnce(t *testing.T) {
for _, c := range []struct {
name string
query string
}{
{"whole", ``},
{"stream", `"stream":true,`},
} {
t.Run(c.name, func(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{200, "application/json", chatOK}}}
scriptedOn(t, "a", provider.Chat, a)
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m"}, Routing: provider.Ordered}); err != nil {
t.Fatal(err)
}
s := New()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"group/g","max_tokens":50,`+c.query+`"messages":[{"role":"user","content":"hi"}]}`))
s.Handler().ServeHTTP(rec, req)
if rec.Code != 502 || !strings.Contains(rec.Body.String(), "did not stream") {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
if a.n != 1 {
t.Errorf("a member that never streams was asked %d times", a.n)
}
})
}
}
// The group still moves on from it: the next member, whose API streams, is
// asked and answers.
func TestStreamlessMemberHandsOnToTheNextMember(t *testing.T) {
fresh(t)
a := &scripted{replies: []reply{{200, "application/json", chatOK}}}
b := &scripted{replies: []reply{{200, "text/event-stream", sse(
`data: {"id":"c1","object":"chat.completion.chunk","created":1,"model":"m","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","created":1,"model":"m","choices":[{"index":0,"delta":{"content":"hello"},"finish_reason":null}]}`,
`data: {"id":"c1","object":"chat.completion.chunk","created":1,"model":"m","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`,
)}}}
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()
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"group/g","max_tokens":50,"stream":true,"messages":[{"role":"user","content":"hi"}]}`))
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "hello") {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
if a.n != 1 || b.n != 1 {
t.Errorf("asked a %d times and b %d", a.n, b.n)
}
}