gateway: a response.completed with no output and no tokens fails over (#2471)
What changed: a Responses upstream that ends its 200 stream with a
response.completed carrying no output and counting no token - WorkBuddy
AI's account answered Codex's request with created, in_progress,
completed and `output: []` three times running (MurmuredVelpro) - had
the completed event taken for a reply's end: the held stream went to the
agent as a completed turn, the try stood as a 200 success, and the
group's healthy members were never asked. streamEvent now reads a
completed with an empty output and a zero usage as the no reply it is -
the same shape a translated reply that said nothing and counted nothing
has failed as since #2086 (repliesCounted) - and fails the held try for
the group's next candidate, in the candidate walk as an empty reply's
does: nobody rests, as the reply's shape failed, not the account. A
completed that counted a token stays a reply, as Claude's own empty
end_turn does; and an event this reaches is one nothing was said before,
since the hold lets the first content through and reads no event beside
it, so nothing of the try has reached the agent when it fires.
The fields said are what is read: a completed that names neither field
goes as it came, as the streamEvent table pinned it
(TestStreamEventRefusals, which caught the first draft reading an
omitted output as an empty one), and a completed that counted a token
stays a reply.
Run: TestEmptyCompletedFailsOver (new) red on 06c2c35d1 before the fix -
one try, status 200, Fail empty, the second member never asked, the
reported ledger shape byte for byte - green after, with
TestEmptyCompletedLeavesAWholeReplyAlone (new: a completed with output
and usage goes as it came) and TestFinishReasonErrorFailsOver green.
Not tried: a real WorkBuddy AI account (the stream is built from the
frames the reporter pasted).
Generated-by: GLM-5.3-Flash (ZCode)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -3262,6 +3262,25 @@ var filterReasons = map[string]string{
|
||||
// taken for content it reached the agent and the try stood as a success.
|
||||
const finishFailed = `the reply ended with finish_reason "error"`
|
||||
|
||||
// emptyCompleted is what a 200 Responses stream the vendor ends with a
|
||||
// response.completed that named its output empty and its usage zero
|
||||
// (#2471, MurmuredVelpro) says of itself, in the failure the hold keeps:
|
||||
// the turn was answered with nothing, as a translated reply that said
|
||||
// nothing and counted nothing is (repliesCounted, #2086), and the
|
||||
// group's next member answers it, while taken for a reply's end it
|
||||
// reached the agent as a completed turn and the try stood as a success.
|
||||
// One that counted a token stays a reply, as Claude's own empty end_turn
|
||||
// does; one that names neither field is taken as it came, a relay's
|
||||
// completed carrying neither — the fields said are what is read.
|
||||
const emptyCompleted = `the reply completed with nothing in it and counted no tokens`
|
||||
|
||||
// outputEmpty tells whether a Responses reply named its output as carrying
|
||||
// nothing: an empty list, or null — the field said, not left out.
|
||||
func outputEmpty(raw json.RawMessage) bool {
|
||||
raw = bytes.TrimSpace(raw)
|
||||
return string(raw) == "null" || string(raw) == "[]"
|
||||
}
|
||||
|
||||
// refusedNote is what a refusal is noted as: the vendor's own reason.
|
||||
func refusedNote(reason string) string {
|
||||
return "safety filter: " + reason
|
||||
@@ -3388,6 +3407,8 @@ func streamEvent(ev []byte) (kind, status int, msg string) {
|
||||
IncompleteDetails *struct {
|
||||
Reason string `json:"reason"`
|
||||
} `json:"incomplete_details"`
|
||||
Output json.RawMessage `json:"output"`
|
||||
Usage *rUsage `json:"usage"`
|
||||
} `json:"response"`
|
||||
Choices *[]struct {
|
||||
Delta map[string]any `json:"delta"`
|
||||
@@ -3486,6 +3507,16 @@ func streamEvent(ev []byte) (kind, status int, msg string) {
|
||||
return refusal(r)
|
||||
}
|
||||
}
|
||||
case typ == "response.completed" && v.Response.Status != "incomplete" &&
|
||||
outputEmpty(v.Response.Output) && v.Response.Usage != nil && uncounted(v.Response.Usage.usage()):
|
||||
// completed, and the reply named its output empty and its usage
|
||||
// zero (#2471, MurmuredVelpro): no reply at all, as a translated
|
||||
// one that said nothing and counted nothing is (repliesCounted,
|
||||
// #2086). An event this reaches is one nothing was said before —
|
||||
// the hold lets the first content through and reads no event
|
||||
// beside it — so nothing of the try has reached the agent, and
|
||||
// the failure is still held for the group's next candidate
|
||||
return eventError, streamStatus(emptyCompleted), emptyCompleted
|
||||
case typ == "" && v.Candidates != nil:
|
||||
// a Gemini chunk
|
||||
if r, ok := filterReasons[v.PromptFeedback.BlockReason]; ok {
|
||||
|
||||
@@ -3953,12 +3953,13 @@ 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) {
|
||||
if !last && hw.failed() && (strings.Contains(call.Error, finishFailed) || strings.Contains(call.Error, emptyCompleted)) {
|
||||
// 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
|
||||
// (#2471), or with a response.completed that carried nothing
|
||||
// and counted no token (also #2471): the turn failed, as the
|
||||
// words say, 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}
|
||||
}
|
||||
|
||||
@@ -66,6 +66,8 @@ func scriptedOn(t *testing.T, id string, proto provider.Protocol, s *scripted) {
|
||||
switch proto {
|
||||
case provider.Anthropic:
|
||||
p.Anthropic = up.URL
|
||||
case provider.Responses:
|
||||
p.Responses = up.URL
|
||||
default:
|
||||
p.Chat = up.URL + "/v1"
|
||||
}
|
||||
@@ -217,6 +219,66 @@ func TestFinishReasonErrorFailsOver(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A 200 Responses stream that ends completed with nothing in it — no
|
||||
// output, no token counted (MurmuredVelpro on #2471: WorkBuddy AI's
|
||||
// account answered Codex's request with created, in_progress, completed
|
||||
// and `output: []` three times running) — is no reply: the held stream
|
||||
// fails before it is let through, the group's next member answers, and
|
||||
// nobody rests, as a translated reply that said nothing and counted
|
||||
// nothing does (#2086).
|
||||
func TestEmptyCompletedFailsOver(t *testing.T) {
|
||||
fresh(t)
|
||||
broken := sse(`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r","status":"in_progress","output":[]}}`,
|
||||
`event: response.in_progress`+"\n"+`data: {"type":"response.in_progress","response":{"id":"r","status":"in_progress","output":[]}}`,
|
||||
`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r","status":"completed","output":[],"usage":{"input_tokens":0,"output_tokens":0,"total_tokens":0}}}`)
|
||||
good := sse(`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r2","status":"in_progress","output":[]}}`,
|
||||
`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"from b"}`,
|
||||
`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r2","status":"completed","output":[{"type":"message","role":"assistant","id":"m1","content":[{"type":"output_text","text":"from b"}]}],"usage":{"input_tokens":5,"output_tokens":2,"total_tokens":7}}}`)
|
||||
a := &scripted{replies: []reply{{200, "text/event-stream", broken}}}
|
||||
b := &scripted{replies: []reply{{200, "text/event-stream", good}}}
|
||||
scriptedOn(t, "a", provider.Responses, a)
|
||||
scriptedOn(t, "b", provider.Responses, 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()
|
||||
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses",
|
||||
strings.NewReader(`{"model":"group/g","stream":true,"input":"hi"}`)))
|
||||
code, body := rec.Code, rec.Body.String()
|
||||
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: %+v", a.n, b.n, s.trace.routes[len(s.trace.routes)-1])
|
||||
}
|
||||
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, "completed with nothing in it") {
|
||||
t.Fatalf("tries: %+v", r.Tries)
|
||||
}
|
||||
}
|
||||
|
||||
// A completed response that carried output and counted its tokens stays
|
||||
// the whole reply it is: only one that said nothing and counted nothing
|
||||
// fails (repliesCounted, #2086).
|
||||
func TestEmptyCompletedLeavesAWholeReplyAlone(t *testing.T) {
|
||||
fresh(t)
|
||||
whole := sse(`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r","status":"in_progress","output":[]}}`,
|
||||
`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"hi there"}`,
|
||||
`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r","status":"completed","output":[{"type":"message","role":"assistant","id":"m1","content":[{"type":"output_text","text":"hi there"}]}],"usage":{"input_tokens":5,"output_tokens":2,"total_tokens":7}}}`)
|
||||
a := &scripted{replies: []reply{{200, "text/event-stream", whole}}}
|
||||
scriptedOn(t, "a", provider.Responses, a)
|
||||
s := New()
|
||||
rec := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses",
|
||||
strings.NewReader(`{"model":"a/m","stream":true,"input":"hi"}`)))
|
||||
code, body := rec.Code, rec.Body.String()
|
||||
if code != 200 || !strings.Contains(body, "hi there") || strings.Contains(body, "nothing in it") {
|
||||
t.Fatalf("%d %s", code, body)
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user