Moves the fantasy pin to the tip of `coder_2_33` (`85ffbbe`). That commit is coder/fantasy#72, which merges 87 upstream charmbracelet/fantasy commits, v0.40.0 to v0.45.5. Between the two pins 117 files change. Upstream's own v0.40.0 to v0.45.5 change, without tests, testdata, examples, `go.sum` and the kronk provider, is 43 files, +2271/−928. ## What reaches chatd chatd builds only the `anthropic`, `openai` and `openaicompat` fantasy clients (`chatprovider.ModelFromConfig`); other vendors reach it through the AI Gateway on one of those. So the upstream changes to the Google, Vercel, OpenRouter, Bedrock, Azure and kronk providers, and to the `fantasy.Agent` loop and its retries, do not run in chatd. These do: - **Chat Completions and openaicompat streams** keep reasoning on batched and DeepSeek-shaped chunks, keep `finish_reason: length` when tool calls are truncated, and stop sending empty reasoning fields. openaicompat drops assistant turns that hold only reasoning. - **Token usage.** When an OpenAI-shaped backend reports reasoning tokens apart from completion tokens (reasoning larger than output), output tokens now include them, so that backend's reported output tokens and cost rise. OpenAI's own shape, where reasoning is a subset, is unchanged. - **Responses.** A content-filtered turn no longer reads as a tool-call turn. - **Context overflow.** Baseten, Fireworks and Parasail prompt-length errors set `ContextTooLargeErr`, which compaction acts on. - **Anthropic.** Tool calls whose content block never closes are reported. For Claude 5 models, thinking defaults to `display: "summarized"` when the chat config sets none; a configured display still wins. `TestThinkingDropBlockModel_RetriesWithEffort` now expects the field in the retry body. - **Anthropic keepalives.** Fantasy forwards `message_start`, `message_delta` and unknown events as content-free `keepalive` parts. The first one arrives before any model output, so this PR makes `ttft_seconds` and the `time_to_first_token` stage skip them, as they skip warnings. They still reset the stream silence guard. `processStepStream` ignores them, and the debug recorder counts them in its part count but records no content. - **Tool-result media** now goes through upstream's shared `ToolRunBuffer`, which replaces coder/fantasy#64 and does the same thing. #72 keeps coder's anthropic-sdk-go and openai-go forks, Go 1.26 and kronk v1.31.5, so upstream's SDK and Go moves do not come along. `go mod tidy` raises the modules fantasy v0.45.5 requires, among them otel exporters v1.47, the AWS SDK, `google.golang.org/api` v0.307 and go-openapi v1. All are upgrades. The `go.mod` comment now names #72 as the upstream sync. coder/coder#29861 stacks on this PR to pin coder/fantasy#62. <details> <summary>Verification</summary> - `go build ./...`. - `go test` passes on every package that imports fantasy (`coderd/x/chatd/...`), and `go test ./coderd -run 'TestChat|TestExpChat|TestMCP|TestAIBridge'` passes. - `TestGuardedStreamTTFTStage/WarningsAndKeepalivesThenContentObservesOnce` fails without the TTFT change (TTFT observed 0s instead of 1s) and passes with it. - `make pre-commit` passes. The generated Prometheus docs pick up the metric help change. Not verified against live backends in this PR: the stream fixes, the token usage change and the context-overflow patterns have no coder test against a real provider. coder/fantasy#72 reports a local chatd run against the AI Gateway on Anthropic, Responses, OpenAI Chat and openaicompat. </details> > 🤖 This PR was created with the help of Coder Agents, and _will be_ reviewed by a human. 🏂🏻
134 lines
4.9 KiB
Go
134 lines
4.9 KiB
Go
package chatd
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"charm.land/fantasy"
|
|
fantasyanthropic "charm.land/fantasy/providers/anthropic"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"cdr.dev/slog/v3/sloggers/slogtest"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestThinkingDropBlockModel_RetriesWithEffort(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
requests := make(chan chattest.AnthropicRequest, 2)
|
|
url := chattest.NewAnthropic(t, func(req *chattest.AnthropicRequest) chattest.AnthropicResponse {
|
|
requests <- *req
|
|
if len(requests) == 1 {
|
|
return chattest.AnthropicResponse{Error: &chattest.ErrorResponse{
|
|
StatusCode: http.StatusBadRequest,
|
|
Type: "invalid_request_error",
|
|
Message: "The block is bound to a different conversation.",
|
|
}}
|
|
}
|
|
return chattest.AnthropicStreamingResponse(chattest.AnthropicTextChunks("ok")...)
|
|
})
|
|
client, err := fantasyanthropic.New(fantasyanthropic.WithAPIKey("test-key"), fantasyanthropic.WithBaseURL(url))
|
|
require.NoError(t, err)
|
|
lm, err := client.LanguageModel(ctx, "claude-opus-5-5")
|
|
require.NoError(t, err)
|
|
|
|
server := &Server{logger: slogtest.Make(t, nil)}
|
|
model := server.withThinkingDropBlock(chatprovider.NewModel(lm, nil), uuid.New(), codersdk.ChatModelCallConfig{}, uuid.New())
|
|
effort := fantasyanthropic.EffortHigh
|
|
stream, err := model.LanguageModel().Stream(ctx, fantasy.Call{
|
|
Prompt: fantasy.Prompt{fantasy.NewUserMessage("hello")},
|
|
ProviderOptions: fantasy.ProviderOptions{fantasyanthropic.Name: &fantasyanthropic.ProviderOptions{Effort: &effort}},
|
|
})
|
|
require.NoError(t, err)
|
|
for part := range stream {
|
|
require.NotEqual(t, fantasy.StreamPartTypeError, part.Type, part.Error)
|
|
}
|
|
|
|
testutil.RequireReceive(ctx, t, requests)
|
|
retry := testutil.RequireReceive(ctx, t, requests)
|
|
require.JSONEq(t, `{"type":"adaptive","display":"summarized","block_binding":{"prefix_mismatch_behavior":"drop_block"}}`, string(retry.Thinking))
|
|
require.Equal(t, thinkingBindingBeta, retry.Header.Get("Anthropic-Beta"))
|
|
}
|
|
|
|
func TestThinkingDropBlockModel_HintedRetriesOnlyDropBlockRejections(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
rejection string
|
|
wantDropBlock []bool
|
|
wantErr bool
|
|
wantHint bool
|
|
}{
|
|
{
|
|
name: "OtherError",
|
|
rejection: "messages.9.content.43.image.source.base64.data: At least one of the image dimensions exceed max allowed size for many-image requests: 3000 pixels",
|
|
wantDropBlock: []bool{true},
|
|
wantErr: true,
|
|
wantHint: true,
|
|
},
|
|
{
|
|
name: "BetaRejected",
|
|
rejection: "Unexpected value(s) `thinking-binding-controls-2026-08-01` for the `anthropic-beta` header. Please consult our documentation at platform.claude.com/docs or try again without the header.",
|
|
wantDropBlock: []bool{true, false},
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
requests := make(chan chattest.AnthropicRequest, 2)
|
|
url := chattest.NewAnthropic(t, func(req *chattest.AnthropicRequest) chattest.AnthropicResponse {
|
|
requests <- *req
|
|
if strings.Contains(string(req.Thinking), thinkingBindingDropBlock) {
|
|
return chattest.AnthropicResponse{Error: &chattest.ErrorResponse{
|
|
StatusCode: http.StatusBadRequest,
|
|
Type: "invalid_request_error",
|
|
Message: tc.rejection,
|
|
}}
|
|
}
|
|
return chattest.AnthropicStreamingResponse(chattest.AnthropicTextChunks("ok")...)
|
|
})
|
|
client, err := fantasyanthropic.New(fantasyanthropic.WithAPIKey("test-key"), fantasyanthropic.WithBaseURL(url))
|
|
require.NoError(t, err)
|
|
lm, err := client.LanguageModel(ctx, "claude-opus-5-5")
|
|
require.NoError(t, err)
|
|
|
|
server := &Server{logger: slogtest.Make(t, nil)}
|
|
key := thinkingDropBlockKey{providerID: uuid.New(), model: "claude-opus-5-5"}
|
|
server.thinkingDropBlock.Store(key, struct{}{})
|
|
model := server.withThinkingDropBlock(chatprovider.NewModel(lm, nil), key.providerID, codersdk.ChatModelCallConfig{}, uuid.New())
|
|
stream, err := model.LanguageModel().Stream(ctx, fantasy.Call{
|
|
Prompt: fantasy.Prompt{fantasy.NewUserMessage("hello")},
|
|
})
|
|
require.NoError(t, err)
|
|
var streamErr error
|
|
for part := range stream {
|
|
if part.Type == fantasy.StreamPartTypeError {
|
|
streamErr = part.Error
|
|
}
|
|
}
|
|
|
|
if tc.wantErr {
|
|
require.ErrorContains(t, streamErr, tc.rejection)
|
|
} else {
|
|
require.NoError(t, streamErr)
|
|
}
|
|
var dropBlock []bool
|
|
for len(requests) > 0 {
|
|
req := <-requests
|
|
dropBlock = append(dropBlock, strings.Contains(string(req.Thinking), thinkingBindingDropBlock))
|
|
}
|
|
require.Equal(t, tc.wantDropBlock, dropBlock)
|
|
_, hinted := server.thinkingDropBlock.Load(key)
|
|
require.Equal(t, tc.wantHint, hinted)
|
|
})
|
|
}
|
|
}
|