The Responses encoder read the open function_call's id and name from the
collector's last part. When a hidden thinking block came after a
tool_use (a redacted_thinking block, or a thinking block signed with no
text, from content_block_start or a signature_delta), the collector had
already appended that thinking, last(ToolCall) was nil, and closing the
call dereferenced it. The server recovered the panic mid-stream, so the
client saw a 200 chunked body cut short ("unexpected EOF", "stream
disconnected before completion") and no response.completed.
The encoder now keeps the open call's id and name itself (e.call), set
when the item opens, for closing it and for the custom-tool check on its
argument deltas. The other stream encoders (Chat, Anthropic, Gemini)
track their open tool call in their own state and never read the
collector's last part, so only this one had the bug.
Verified:
- TestResponsesStreamToolThenHiddenThinking runs the reporter's sequence
through a real gateway server (httptest) to a fake Anthropic upstream
and a real HTTP client: redacted_thinking, empty thinking signed at its
start, empty thinking signed in a delta, and visible thinking as the
control. With the old responses.go the three hidden cases FAIL with
"status 200, the stream was cut short: unexpected EOF" and the log shows
the nil pointer panic; the visible case passes on both.
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., go test -tags nogui ./... (see the push).
Not done here: the max_tokens setting the issue also asks for is a
separate feature request.
97 lines
5.3 KiB
Go
97 lines
5.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A Claude turn that calls a tool and then thinks without showing it, a
|
|
// redacted_thinking block or a thinking block signed with no text, is a
|
|
// whole Responses stream for Codex: the call, then response.completed. The
|
|
// encoder read the open call from the collector's last part, which by then
|
|
// was the thinking, and panicked, so the client got a chunked body cut short
|
|
// ("unexpected EOF", "stream disconnected before completion", #1786).
|
|
func TestResponsesStreamToolThenHiddenThinking(t *testing.T) {
|
|
const head = "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_01\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"claude-opus-4-6\",\"content\":[],\"stop_reason\":null,\"usage\":{\"input_tokens\":12,\"output_tokens\":1}}}\n\n" +
|
|
"event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_01\",\"name\":\"shell\",\"input\":{}}}\n\n" +
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"command\\\":[\\\"ls\\\"]}\"}}\n\n" +
|
|
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
|
|
const tail = "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"output_tokens\":30}}\n\n" +
|
|
"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
|
|
for _, c := range []struct {
|
|
name, block string
|
|
visible bool
|
|
}{
|
|
{"redacted_thinking", "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"redacted_thinking\",\"data\":\"EmwKAhgBEgy3va3pzix\"}}\n\n" +
|
|
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n", false},
|
|
{"empty thinking, signed at its start", "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\",\"signature\":\"fixture\"}}\n\n" +
|
|
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n", false},
|
|
{"empty thinking, signed in a delta", "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\",\"signature\":\"\"}}\n\n" +
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"signature_delta\",\"signature\":\"fixture\"}}\n\n" +
|
|
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n", false},
|
|
{"visible thinking", "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"\",\"signature\":\"\"}}\n\n" +
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"thinking_delta\",\"thinking\":\"Now list it.\"}}\n\n" +
|
|
"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"signature_delta\",\"signature\":\"fixture\"}}\n\n" +
|
|
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n", true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
fresh(t)
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, head+c.block+tail)
|
|
}))
|
|
defer up.Close()
|
|
if err := provider.Save(provider.Provider{ID: "anth", Name: "Anth", Key: "k", Anthropic: up.URL, Models: []string{"claude-opus-4-6"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gw := httptest.NewServer(New().Handler())
|
|
defer gw.Close()
|
|
resp, err := http.Post(gw.URL+"/v1/responses", "application/json", strings.NewReader(
|
|
`{"model":"anth/claude-opus-4-6","stream":true,"input":"list the files","reasoning":{"effort":"high"},`+
|
|
`"tools":[{"type":"function","name":"shell","parameters":{"type":"object","properties":{"command":{"type":"array","items":{"type":"string"}}}}}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("status %d, the stream was cut short: %v\n%s", resp.StatusCode, err, body)
|
|
}
|
|
var completed map[string]any
|
|
var call map[string]any
|
|
var reasoning int
|
|
for _, ev := range events(string(body)) {
|
|
switch ev["type"] {
|
|
case "response.completed":
|
|
completed = ev
|
|
case "response.output_item.done":
|
|
item, _ := ev["item"].(map[string]any)
|
|
switch item["type"] {
|
|
case "function_call":
|
|
call = item
|
|
case "reasoning":
|
|
reasoning++
|
|
}
|
|
}
|
|
}
|
|
if completed == nil {
|
|
t.Fatalf("no response.completed:\n%s", body)
|
|
}
|
|
if call == nil || call["call_id"] != "toolu_01" || call["name"] != "shell" || call["arguments"] != `{"command":["ls"]}` {
|
|
b, _ := json.Marshal(call)
|
|
t.Fatalf("function_call = %s, want toolu_01 shell {\"command\":[\"ls\"]}\n%s", b, body)
|
|
}
|
|
if want := map[bool]int{true: 1, false: 0}[c.visible]; reasoning != want {
|
|
t.Errorf("%d reasoning items, want %d", reasoning, want)
|
|
}
|
|
})
|
|
}
|
|
}
|