gateway: an error in a stream already begun says its status and that it may be retried, so Pi asks again (#2372, zminds)
Once a group has sent the agent the stream's headers (the keepalive
while a member is slow), a later failure reaches the agent as an error
event in a 200 stream, with the member's words only: "no first token in
30s", "Upstream service temporarily unavailable", "521 status code (no
body)". Pi decides whether to retry from that text alone, and none of
these match its list, so the turn ended after one try where Claude Code
and Codex retried.
streamError, the one place every in-stream error passes, now runs the
text through saysPassing: for 408, 429 or 5xx whose text names none of
the words such agents retry on, it appends "(<status>: you can retry
your request)". A text that already says so, and a request's own fault
(400, 401, 404 ...), are left as they were. A response not yet begun
already carries its real status; unchanged.
Not done: an X-Magpie-Retryable header (can't be sent once the stream
has begun), and widening retryable()/passing() for relay wordings and
521-527 (overlaps the #2313 work on relay 400s).
Tests (internal/gateway/stream_error_passing_test.go, with Pi 1.2.0's
own allow and deny regexes as the reporter quoted them):
- TestStreamErrorSaysItPasses: the reporter's literal texts.
- TestGroupLastFailureInStreamSaysItPasses: a slow first member, a
keepalive, then a last member's relay 503; the stream's error is one
Pi retries. Broken by hand (Text: msg back): FAIL.
Run at origin/main 9d97fd073 + this change and the #2373/#2372/#2374 commits beside it (one tree), -tags nogui -count=1
-timeout 45m: internal/gateway, plugin, provider, agent,
catalog, gui ok. gofmt -l clean; go vet -tags nogui ./... for GOOS
darwin, linux, windows clean.
Full suite: SUITE_LINE
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -6248,13 +6248,33 @@ func failTo(w http.ResponseWriter, a *keptAlive, proto provider.Protocol, status
|
||||
func streamError(w http.ResponseWriter, proto provider.Protocol, status int, msg string) {
|
||||
sw := newSSEWriter(w)
|
||||
sw.begun = true
|
||||
ev := Event{Kind: KError, Text: msg, Status: status}
|
||||
ev := Event{Kind: KError, Text: saysPassing(status, msg), Status: status}
|
||||
if tooLong(status, msg) {
|
||||
ev.Code = "context_length_exceeded"
|
||||
}
|
||||
makeEncoder(proto, sw, &Request{}).event(ev)
|
||||
}
|
||||
|
||||
// passingSaid matches the words an agent that reads only an error's text
|
||||
// takes for a failure worth asking again (Pi's isRetryableError, #2372):
|
||||
// one of the usual statuses, a timeout, a rate limit, a busy or
|
||||
// unreachable server.
|
||||
var passingSaid = regexp.MustCompile(`(?i)\b(429|500|502|503|504)\b|timed? ?out|rate.?limit|too many requests|overloaded|service.?unavailable|server.?error|internal.?error|connection.?(error|refused|reset)|retry your request|try your request again`)
|
||||
|
||||
// saysPassing is msg, an error sent in a stream whose status the agent
|
||||
// can no longer be told, with the status said in it when it is one a
|
||||
// moment may clear (408, 429, 5xx) and msg says so in no words an agent
|
||||
// knows: "Upstream service temporarily unavailable" and "no first token in
|
||||
// 30s" reached Pi, which judges by the text alone, as errors not worth
|
||||
// asking again, while Claude Code and Codex, told the same as a status,
|
||||
// asked again and were answered (#2372).
|
||||
func saysPassing(status int, msg string) string {
|
||||
if status != http.StatusRequestTimeout && status != http.StatusTooManyRequests && status < 500 || passingSaid.MatchString(msg) {
|
||||
return msg
|
||||
}
|
||||
return fmt.Sprintf("%s (%d: you can retry your request)", msg, status)
|
||||
}
|
||||
|
||||
// writeError answers in the client's own error shape.
|
||||
func writeError(w http.ResponseWriter, proto provider.Protocol, status int, msg string) int {
|
||||
return writeErrorSaid(w, proto, status, msg, nil, nil)
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// piRetryable is Pi's isRetryableError (agent-session.ts, 1.2.0), as
|
||||
// zminds quoted it on #2372: the text alone decides, a deny-list first.
|
||||
var (
|
||||
piRetry = regexp.MustCompile(`(?i)overloaded|rate.?limit|too many requests|429|500|502|503|504|520|524|service.?unavailable|server.?error|internal.?error|timed? out|timeout|terminated|connection.?error|connection.?refused|ended without|socket hang up|fetch failed|ENOTFOUND|EAI_AGAIN|websocket closed|retry delay|you can retry your request|try your request again|please retry your request|ResourceExhausted`)
|
||||
piDeny = regexp.MustCompile(`(?i)insufficient_quota|quota exceeded|available balance|out of budget|billing|GoUsageLimitError|FreeUsageLimitError|Monthly usage limit reached|subscription_sharing_usage_limit_exceeded`)
|
||||
)
|
||||
|
||||
func piRetries(msg string) bool { return !piDeny.MatchString(msg) && piRetry.MatchString(msg) }
|
||||
|
||||
// An error that reaches the agent in a stream already begun can't say its
|
||||
// status, and the words of one a moment may clear are said for it: Pi
|
||||
// judges an error by its text alone, and ended the turn on each of these,
|
||||
// which Claude Code and Codex asked again (#2372, the texts zminds' Pi
|
||||
// sessions recorded). A request's own fault is left as it was said.
|
||||
func TestStreamErrorSaysItPasses(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
status int
|
||||
msg string
|
||||
}{
|
||||
{503, "Upstream service temporarily unavailable"},
|
||||
{502, "Upstream response stream was interrupted"},
|
||||
{504, "A: no first token in 30s"},
|
||||
{502, "Upstream request failed"},
|
||||
{502, "Upstream HTTP/2 stream failed"},
|
||||
{521, "521 status code (no body)"},
|
||||
{503, "The service is temporarily unavailable. Please retry later."},
|
||||
{429, "Too many pending requests, please retry later"},
|
||||
{408, "the request took too long"},
|
||||
} {
|
||||
if got := saysPassing(c.status, c.msg); !piRetries(got) || !strings.HasPrefix(got, c.msg) {
|
||||
t.Errorf("%d %q is told %q", c.status, c.msg, got)
|
||||
}
|
||||
}
|
||||
for _, msg := range []string{"Upstream rate limit exceeded, please retry later", "503: Service temporarily unavailable", "connection error"} {
|
||||
if got := saysPassing(503, msg); got != msg {
|
||||
t.Errorf("%q, already said, is told %q", msg, got)
|
||||
}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
status int
|
||||
msg string
|
||||
}{
|
||||
{400, "Invalid request parameters"},
|
||||
{401, "invalid api key"},
|
||||
{404, "model not found"},
|
||||
} {
|
||||
if got := saysPassing(c.status, c.msg); got != c.msg {
|
||||
t.Errorf("%d %q is told %q", c.status, c.msg, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Through a group: a member let go for no first token, the stream's
|
||||
// headers sent to keep the agent alive meanwhile, and the last member
|
||||
// failing with a relay's words — the agent's stream error says what Pi
|
||||
// asks again for, in each protocol it reads.
|
||||
func TestGroupLastFailureInStreamSaysItPasses(t *testing.T) {
|
||||
old := retryPause
|
||||
retryPause = time.Millisecond
|
||||
t.Cleanup(func() { retryPause = old })
|
||||
k := keepHeldAfter
|
||||
keepHeldAfter = 30 * time.Millisecond
|
||||
t.Cleanup(func() { keepHeldAfter = k })
|
||||
fresh(t)
|
||||
keepFast(t, keepaliveGap, 20*time.Millisecond, keepaliveLongest)
|
||||
a := &slowStart{wait: 10 * time.Second}
|
||||
b := &scripted{replies: []reply{{503, "application/json", `{"error":{"message":"Upstream service temporarily unavailable","type":"upstream_error"}}`}}}
|
||||
slowOn(t, "a", a)
|
||||
scriptedOn(t, "b", provider.Chat, b)
|
||||
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered, FirstToken: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
code, body := postAs(t, s, "", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != http.StatusOK || !strings.Contains(body, ": keepalive") {
|
||||
t.Fatalf("the agent wasn't sent the stream's headers first: %d %q", code, body)
|
||||
}
|
||||
i := strings.Index(body, "Upstream service temporarily unavailable")
|
||||
if i < 0 {
|
||||
t.Fatalf("no error in the stream: %q", body)
|
||||
}
|
||||
said := body[i:]
|
||||
if j := strings.Index(said, `"`); j >= 0 {
|
||||
said = said[:j]
|
||||
}
|
||||
if !piRetries(said) {
|
||||
t.Fatalf("the stream's error %q is one Pi doesn't ask again for: %q", said, body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user