gateway: Anthropic's message_stop and a Responses upstream's completed end a translated read too (#5936, ggbdpq and polaris-yhl on X)

The [DONE] fix in the previous commit (ggbdpq's patch, applied with git am,
reviewed: three hunks in continuation.go plus tests, nothing else, nothing
that reaches out) ended a Chat upstream's read at its own end. The other two
translated upstreams had the same hole: a relay that holds the connection
after message_stop, or after response.completed/incomplete/failed, kept the
translated reply's end waiting for the hangup. saidOver now names each
protocol's last word and the read ends there.

Run: TestAnthropicStopEndsTranslatedReply and
TestResponsesCompletedEndsTranslatedReply (a held upstream; Responses client
over Anthropic, Chat client over Responses) plus ggbdpq's two tests; all four
red on origin/main's continuation.go (5s/30s timeouts), green with the fix.
go test ./internal/gateway ok (1039s, on mote); go vet darwin/linux/windows
clean; gofmt clean.
Not tried: a real OfficeAce account, the full ./... suite before the tag.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-11-12 18:02:08 +08:00
co-authored by ggbdpq
parent 48875e580a
commit 2e92d71aab
2 changed files with 93 additions and 1 deletions
+20 -1
View File
@@ -680,7 +680,7 @@ func (s *Server) streamTranslated(w http.ResponseWriter, r *http.Request, p prov
if cut || relooped {
return errStreamCut
}
if actual == provider.Chat && strings.TrimSpace(data) == "[DONE]" {
if saidOver(actual, data) {
// the upstream's own end: what the reply has is
// whole, hold or not (#5936)
return errDoneSaid
@@ -835,3 +835,22 @@ func (s *Server) streamTranslated(w http.ResponseWriter, r *http.Request, p prov
}
return 200, failed
}
// saidOver reports whether an upstream event is its protocol's own last
// word on a reply that went well: Chat's [DONE], Anthropic's message_stop,
// Responses' completed, incomplete or failed. A relay that holds the
// connection after it must not hold the translated reply's end (#5936).
func saidOver(proto provider.Protocol, data string) bool {
switch proto {
case provider.Chat:
return strings.TrimSpace(data) == "[DONE]"
case provider.Anthropic:
return eventType(data) == "message_stop"
case provider.Responses:
switch eventType(data) {
case "response.completed", "response.incomplete", "response.failed":
return true
}
}
return false
}
+73
View File
@@ -871,3 +871,76 @@ func TestChatDoneEndsTranslatedReply(t *testing.T) {
t.Errorf("completed's usage = %v, want 9 in and 4 out", usage)
}
}
// holdAfter is an upstream that streams the given events whole and then
// holds the connection (#5936's siblings: Anthropic's message_stop and a
// Responses upstream's response.completed).
func holdAfter(t *testing.T, events ...string) *httptest.Server {
t.Helper()
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.ReadAll(r.Body)
w.Header().Set("Content-Type", "text/event-stream")
for _, e := range events {
io.WriteString(w, e+"\n\n")
}
w.(http.Flusher).Flush()
<-r.Context().Done()
}))
t.Cleanup(up.Close)
return up
}
func endsWhileHeld(t *testing.T, p provider.Provider, path, body, want string) {
t.Helper()
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(New().Handler())
t.Cleanup(srv.Close)
res, err := http.Post(srv.URL+path, "application/json", strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
got := make(chan bool, 1)
go func() {
sc := bufio.NewScanner(res.Body)
for sc.Scan() {
if strings.Contains(sc.Text(), want) {
got <- true
return
}
}
got <- false
}()
select {
case ok := <-got:
if !ok {
t.Fatalf("the reply closed without %q", want)
}
case <-time.After(5 * time.Second):
t.Fatalf("no %q while the upstream holds its connection", want)
}
}
func TestAnthropicStopEndsTranslatedReply(t *testing.T) {
fresh(t)
up := holdAfter(t,
`data: {"type":"message_start","message":{"id":"m","model":"c","usage":{"input_tokens":3}}}`,
`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hello"}}`,
`data: {"type":"content_block_stop","index":0}`,
`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}`,
`data: {"type":"message_stop"}`)
endsWhileHeld(t, provider.Provider{ID: "up", Name: "UP", Key: "k", Models: []string{"c"}, Anthropic: up.URL + "/v1"},
"/v1/responses", `{"model":"up/c","stream":true,"input":[{"role":"user","content":"hi"}]}`, "response.completed")
}
func TestResponsesCompletedEndsTranslatedReply(t *testing.T) {
fresh(t)
up := holdAfter(t,
`data: {"type":"response.output_text.delta","delta":"hello"}`,
`data: {"type":"response.completed","response":{"id":"r","status":"completed","output":[],"usage":{"input_tokens":3,"output_tokens":2}}}`)
endsWhileHeld(t, provider.Provider{ID: "up", Name: "UP", Key: "k", Models: []string{"c"}, Responses: up.URL + "/v1"},
"/v1/chat/completions", `{"model":"up/c","stream":true,"messages":[{"role":"user","content":"hi"}]}`, "[DONE]")
}