gateway: a 200 stream ended with finish_reason "error" fails over (#2471)

What changed: a vendor that ends its 200 stream with finish_reason
"error" - WorkBuddy's and a DeepSeek harness's word for a turn it failed
rather than finished - had the chunk taken for content: the held stream
went to the agent, the try stood as a 200 success, and the group's next
member was never asked. streamEvent now reads the word as the failure it
says, held for the next candidate as an error event before content is;
in the candidate walk the try fails as an empty reply's or a cut-short
summary's does (#1221, #2274): the next is asked, and nobody rests, as
the reply's shape failed, not the account. Content already passed to the
agent can't be taken back, and is not touched.

Run: TestFinishReasonErrorFailsOver (new) red on a15e5765 before the fix
(the reporter's shape: one try, status 200, Fail empty, the second member
never asked), green after; red again with fallback.go and gateway.go
stashed, tests kept. TestStreamErrorBeforeContentFailsOver, the
streamEvent tables, and the stream families (Stream/Finish/Refusal/
Overload/LastOneLeft/GroupFail/Retry, Loop/Hold/Pass/Queue/Patience/
Incomplete/EndedShort/Grok) green. gofmt -l clean; vet on darwin, linux
(nogui) and windows clean; build ok on all three.

Review (magpie): the maintainer's call this waited on is made: the
literal "error" is a failure from any vendor, no vendor sends it for a
finished turn, so it is not keyed per vendor. Alone (no group), the
failure is the last one left's, tried again as a 502 is (15 asks in
the test's clock, the same count a plain 502 gets), then the vendor's
stream goes to the agent as it came. Content already shown is not taken
back. Not covered, still open on #2471: an empty response.completed
and a Chat stream with no finish_reason at all. Run on the Mac mini:
go test ./internal/gateway -count=1 ok. Broken by hand: fallback.go and
gateway.go at the base - TestFinishReasonErrorFailsOver (200, b never
asked) and the streamEvent table red. Not tried: a real WorkBuddy
reply ending so.

Generated-by: GLM-5.3-Flash (ZCode)
This commit is contained in:
ggbdpq
2026-10-31 02:00:10 +08:00
committed by yetone
parent d965a239f5
commit 86fef947e8
4 changed files with 68 additions and 1 deletions
File diff suppressed because one or more lines are too long
+14
View File
@@ -2958,6 +2958,12 @@ var filterReasons = map[string]string{
"IMAGE_SAFETY": "safety",
}
// finishFailed is what a 200 stream the vendor ends with finish_reason
// "error" (#2471) says of itself, in the failure the hold keeps: the turn
// failed, as the word says, and the group's next member answers it, while
// taken for content it reached the agent and the try stood as a success.
const finishFailed = `the reply ended with finish_reason "error"`
// refusedNote is what a refusal is noted as: the vendor's own reason.
func refusedNote(reason string) string {
return "safety filter: " + reason
@@ -3254,6 +3260,14 @@ func streamEvent(ev []byte) (kind, status int, msg string) {
if r, ok := filterReasons[*c.FinishReason]; ok {
return refusal(r)
}
if *c.FinishReason == "error" {
// WorkBuddy's and a DeepSeek harness's word for a turn
// it failed, not finished (#2471): the failure an error
// event is, held for the group's next candidate, where
// taken for content it went to the agent and the try
// stood as a success
return eventError, streamStatus(finishFailed), finishFailed
}
return eventContent, 0, ""
}
}
+14
View File
@@ -3750,6 +3750,20 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
s.trace.update(tr, func(t *Route) { t.Order, t.Left = askUnlisted(t.Order, t.Left, c, nextW) })
}
}
if !last && hw.failed() && strings.Contains(call.Error, finishFailed) {
// a 200 stream the vendor ended with finish_reason "error"
// (#2471): the turn failed, as the word says, but the account
// is at fault no more than one whose reply said nothing
// (#1221) or was cut short by its output limit (#2274): the
// next is asked, and nobody rests
if other == nil {
other = &Try{Status: call.Status, Error: call.Error}
}
try.Fail = failOther
s.trace.update(tr, func(t *Route) { t.Tries[len(t.Tries)-1] = try })
skipped = append(skipped, c.label()+": "+call.Error)
continue
}
if !last && hw.failed() {
if f := failure(hw.code(), hw.errBody()); other == nil && f != failQuota && f != failCredit {
other = &Try{Status: call.Status, Error: call.Error}
+39
View File
@@ -165,6 +165,8 @@ func TestStreamErrorBeforeContentFailsOver(t *testing.T) {
{`: keep-alive`, eventLead},
{`data: {"id":"c","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]}`, eventLead},
{`data: {"id":"c","choices":[{"index":0,"delta":{"content":"hi"}}]}`, eventContent},
{`data: {"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`, eventContent},
{`data: {"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"error"}]}`, eventError},
{`data: {"error":{"message":"upstream failed"}}`, eventError},
{`event: response.created` + "\n" + `data: {"type":"response.created","response":{"id":"r"}}`, eventLead},
{`event: codex.rate_limits` + "\n" + `data: {"type":"codex.rate_limits","plan_type":"plus","rate_limits":{"allowed":true}}`, eventLead},
@@ -178,6 +180,43 @@ func TestStreamErrorBeforeContentFailsOver(t *testing.T) {
}
}
// A 200 stream a vendor ends with finish_reason "error" — the word
// WorkBuddy's and a DeepSeek harness's replies carry when the turn is not
// finished but failed (#2471) — is the failure it says, held for the
// group's next member as an error event before content is, and nobody
// rests: the reply's shape failed, not the account, as one that looped
// (#1359).
func TestFinishReasonErrorFailsOver(t *testing.T) {
fresh(t)
broken := sse(`data: {"id":"c","choices":[{"index":0,"delta":{"role":"assistant"}}]}`,
`data: {"id":"c","choices":[{"index":0,"delta":{},"finish_reason":"error"}],"usage":{"prompt_tokens":5,"completion_tokens":0,"total_tokens":5}}`,
`data: [DONE]`)
good := sse(`data: {"id":"c2","choices":[{"index":0,"delta":{"role":"assistant"}}]}`,
`data: {"id":"c2","choices":[{"index":0,"delta":{"content":"from b"}}]}`,
`data: {"id":"c2","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`)
a := &scripted{replies: []reply{{200, "text/event-stream", broken}}}
b := &scripted{replies: []reply{{200, "text/event-stream", good}}}
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, body := postAs(t, s, "", `{"model":"group/g","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, `"error"`) {
t.Fatalf("%d %s %+v", code, body, s.trace.routes[len(s.trace.routes)-1])
}
if a.n != 1 || b.n != 1 {
t.Fatalf("a asked %d, b asked %d", a.n, b.n)
}
r := s.trace.routes[len(s.trace.routes)-1]
if len(r.Tries) != 2 || r.Tries[0].Status != 502 || r.Tries[0].Fail != failOther || r.Tries[0].Rest != nil ||
!strings.Contains(r.Tries[0].Error, `finish_reason "error"`) {
t.Fatalf("tries: %+v", r.Tries)
}
}
// The last one left — or the only one — is tried again after a failure
// that may pass, a moment later; one that won't is the agent's at once.
func TestLastOneLeftIsTriedAgain(t *testing.T) {