From 2e92d71aab355deb70815b973fc35ffa03943ea4 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 12 Nov 2026 18:02:05 +0800 Subject: [PATCH] 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 --- internal/gateway/continuation.go | 21 +++++++- internal/gateway/continuation_test.go | 73 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/internal/gateway/continuation.go b/internal/gateway/continuation.go index 019ec181b..428bc2708 100644 --- a/internal/gateway/continuation.go +++ b/internal/gateway/continuation.go @@ -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 +} diff --git a/internal/gateway/continuation_test.go b/internal/gateway/continuation_test.go index 99565302f..72b326916 100644 --- a/internal/gateway/continuation_test.go +++ b/internal/gateway/continuation_test.go @@ -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]") +}