From 373e44b9c74787cce9be6ba68f587e161bd6a451 Mon Sep 17 00:00:00 2001 From: sausheong Date: Sat, 13 Jun 2026 12:48:10 +0800 Subject: [PATCH] fix(anthropic): surface in-band stream error + Builder accumulation (R7c) --- providers/anthropic/anthropic.go | 23 +++++++++++++++++------ providers/anthropic/anthropic_test.go | 9 +++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/providers/anthropic/anthropic.go b/providers/anthropic/anthropic.go index 07b8cb9..0604505 100644 --- a/providers/anthropic/anthropic.go +++ b/providers/anthropic/anthropic.go @@ -4,6 +4,7 @@ import ( "context" "encoding/base64" "encoding/json" + "fmt" "log/slog" "strings" @@ -164,8 +165,8 @@ func (p *AnthropicProvider) ChatStream(ctx context.Context, req llm.ChatRequest) type pendingTC struct { id string name string - inputJSON string // built from input_json_delta events - startInput string // captured from content_block_start (proxy fallback) + inputJSON strings.Builder // built from input_json_delta events + startInput string // captured from content_block_start (proxy fallback) } var pendingTools []pendingTC var currentBlockType string @@ -220,18 +221,21 @@ func (p *AnthropicProvider) ChatStream(ctx context.Context, req llm.ChatRequest) } case "input_json_delta": if len(pendingTools) > 0 { - pendingTools[len(pendingTools)-1].inputJSON += delta.PartialJSON + pendingTools[len(pendingTools)-1].inputJSON.WriteString(delta.PartialJSON) } } case "content_block_stop": if currentBlockType == "tool_use" && len(pendingTools) > 0 { - tc := pendingTools[len(pendingTools)-1] + // Index in place rather than copying the element: the + // pendingTC holds a strings.Builder, which must not be + // copied after first write (vet copylock). + tc := &pendingTools[len(pendingTools)-1] // Prefer streamed deltas (canonical Anthropic). Fall // back to start-time input (proxy emitting full input // upfront). Default to "{}" so MCP servers receive a // valid empty-args envelope rather than null/empty. - inp := tc.inputJSON + inp := tc.inputJSON.String() if inp == "" { inp = tc.startInput } @@ -281,7 +285,14 @@ func (p *AnthropicProvider) ChatStream(ctx context.Context, req llm.ChatRequest) // Final event — nothing extra to emit case "error": - slog.Error("anthropic stream error", "event", event.Type) + // The SDK surfaces error detail via stream.Err() (handled after + // the loop); this in-band branch must not be a silent no-op — + // emit an EventError so the runtime treats it as a failure. + slog.Error("anthropic stream error event", "event", event.Type) + events <- llm.ChatEvent{ + Type: llm.EventError, + Error: fmt.Errorf("anthropic stream error event"), + } } } diff --git a/providers/anthropic/anthropic_test.go b/providers/anthropic/anthropic_test.go index 681e6a4..70e6cad 100644 --- a/providers/anthropic/anthropic_test.go +++ b/providers/anthropic/anthropic_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "strings" "testing" anthropic "github.com/anthropics/anthropic-sdk-go" @@ -708,3 +709,11 @@ data: {"type":"message_stop"} assert.Equal(t, "end_turn", done.StopReason) assert.Empty(t, done.StopCategory) } + +func TestAnthropicInputAccumulation(t *testing.T) { + var b strings.Builder + for _, part := range []string{`{"a"`, `:1`, `}`} { + b.WriteString(part) + } + require.Equal(t, `{"a":1}`, b.String()) +}