Repository navigation
fix(apicompat): Responses→Anthropic 不再以 assistant / thinking 结尾 / Never end converted history on assistant or a thinking block - #8832
Conversation
|
There is a two-request replay gap in the appended
I reproduced the missing turn through The documented preserved-thinking prefix check includes the messages preceding each signed block. This local prefix change therefore appears to invalidate the replayed block's binding; that consequence is inferred from the documented contract. The signatures here are opaque fixtures: I did not call a live provider or observe an upstream 400. The inserted user turn needs a representation that survives subsequent input replay. Explicitly retaining it is the passing control below. Offline reproduction: save as backend/internal/pkg/apicompat/continuation_replay_test.gopackage apicompat
import (
"encoding/json"
"github.com/stretchr/testify/require"
"testing"
)
func oss8832Message(role, text string) ResponsesInputItem {
content, _ := json.Marshal(text)
return ResponsesInputItem{Type: "message", Role: role, Content: content}
}
func oss8832Convert(t *testing.T, model string, items []ResponsesInputItem) *AnthropicRequest {
t.Helper()
input, err := json.Marshal(items)
require.NoError(t, err)
req, err := ResponsesToAnthropicRequest(&ResponsesRequest{Model: model, Input: input})
require.NoError(t, err)
return req
}
func oss8832Prefix(t *testing.T, messages []AnthropicMessage, signature string) []AnthropicMessage {
t.Helper()
for i, m := range messages {
blocks := parseContentBlocks(m.Content)
for j, b := range blocks {
if b.Type == "thinking" && b.Signature == signature {
prefix := append([]AnthropicMessage(nil), messages[:i]...)
if j > 0 {
prefix = append(prefix, anthropicMessageFromBlocks(m.Role, blocks[:j]))
}
return prefix
}
}
}
t.Fatal("signed fixture block not replayed")
return nil
}
func oss8832CanonicalPrefix(messages []AnthropicMessage) []AnthropicMessage {
messages = mergeConsecutiveMessages(messages)
out := make([]AnthropicMessage, 0, len(messages))
for _, message := range messages {
out = append(out, anthropicMessageFromBlocks(message.Role, parseContentBlocks(message.Content)))
}
return out
}
// This compares the actual conversation prefix sent on two conversions. The
// signature is an opaque fixture; this does not emulate Anthropic cryptography
// or claim to have observed an upstream HTTP 400.
func TestOSS8832ContinuationSurvivesSignedThinkingReplay(t *testing.T) {
for _, model := range []string{"claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-5-5", "claude-fable-5-2", "claude-mythos-5-2"} {
for _, shape := range []string{"normal_user_history", "assistant_continuation", "recorded_continuation_control"} {
t.Run(model+"/"+shape, func(t *testing.T) {
items := []ResponsesInputItem{oss8832Message("user", "Original user request")}
if shape != "normal_user_history" {
items = append(items, oss8832Message("assistant", "Partial commentary before reconnect"))
}
sent := oss8832Convert(t, model, items)
require.Equal(t, "user", sent.Messages[len(sent.Messages)-1].Role)
if shape == "recorded_continuation_control" {
items = append(items, oss8832Message("user", "Continue."))
}
response := AnthropicToResponsesResponse(&AnthropicResponse{
Model: model,
Content: []AnthropicContentBlock{
{Type: "thinking", Thinking: "opaque fixture summary", Signature: "fixture-bound-to-first-sent-prefix"},
{Type: "text", Text: "Completed assistant reply"},
},
})
encoded, err := json.Marshal(response.Output)
require.NoError(t, err)
var output []ResponsesInputItem
require.NoError(t, json.Unmarshal(encoded, &output))
items = append(items, output...)
items = append(items, oss8832Message("user", "Next real user request"))
replayed := oss8832Convert(t, model, items)
got := oss8832Prefix(t, replayed.Messages, "fixture-bound-to-first-sent-prefix")
wantJSON, err := json.Marshal(oss8832CanonicalPrefix(sent.Messages))
require.NoError(t, err)
gotJSON, err := json.Marshal(oss8832CanonicalPrefix(got))
require.NoError(t, err)
if model == "claude-opus-5-5" {
t.Logf("first sent prefix: %s; replayed prefix: %s", wantJSON, gotJSON)
}
require.JSONEq(t, string(wantJSON), string(gotJSON), "the prefix of a replayed signed block must still contain the synthetic continuation sent before it was generated")
})
}
}
}Run from go test -tags=unit ./internal/pkg/apicompat -run '^TestOSS8832ContinuationSurvivesSignedThinkingReplay$' -count=1The comparison normalizes string/array text representations and adjacent same-role messages, so the failing distinction is the missing user turn. AI-assisted local analysis and fixture testing. |
…fill or trailing thinking block
…uation after trailing assistant
322f6b4 to
0fab930
Compare
Restore Antigravity Responses custom-tool roundtrips and correct Embeddings platform scheduling. Harden OAuth probe fencing, request-scoped Gemini 403 behavior, OpenAI stream failover and WebSocket current-turn safety. Preserve Claude/GPT-6.2/Grok protocol details and model observations; correct Fable/Kimi quota refreshes and user-vs-account cost reporting. Bound scheduler fallback, request memory retention and shutdown/stream lifecycles without unapproved pricing or schema changes. Local checks: complete Go unit packages, PostgreSQL 18.1/Redis 8.4 repository and audit integration, focused -race, frontend typecheck/lint/1858 Vitest/build, backend lint 0 issues and embedded server build. Remote CI/Security Scan remain pending. Upstream references: Wei-Shaw/sub2api Wei-Shaw#8833 Wei-Shaw#8875 Wei-Shaw#8856 Wei-Shaw#8730 Wei-Shaw#8990 Wei-Shaw#8806 Wei-Shaw#8832 Wei-Shaw#8809 Wei-Shaw#8164 Wei-Shaw#8804 Wei-Shaw#8773 Wei-Shaw#8309.
问题 / Problem
/v1/responses(Codex 等客户端)经ResponsesToAnthropicRequest转成 Anthropic Messages 后,会因为历史末尾的形态被 Claude 5.5 上游以 400 拒绝,客户端重试发送的是同样的历史,所以一直失败。我们生产上 7 天内出现过 38 次,两种报错:This model does not support assistant message prefill. The conversation must end with a user message.转换后的
messages以assistant结尾。常见于 Codex 在一条 assistant commentary 之后断流重连,或末尾一个没有function_call_output的function_call被normalizeAnthropicToolPairing剥掉后只剩前面的 assistant 文本。OpenAI Responses 协议没有 assistant prefill 语义:请求永远是"继续生成下一轮"。messages.N: The final block in an assistant message cannot bethinking.同一轮里 signed
thinking(来自reasoningitem)后面跟着一个未配对的function_call,normalizeAnthropicToolPairing删掉tool_use后,这条 assistant 消息的末尾块就成了thinking。Responses → Anthropicconversion could produce histories Claude 5.5 rejects with a 400, and since the client retries the same history it keeps failing:messagesend onassistant(e.g. Codex reconnects after an assistant commentary message, or a trailing unansweredfunction_callis stripped bynormalizeAnthropicToolPairing, leaving the preceding assistant text). Responses has no prefill semantics, so this is never intended.thinkingblock when thetool_useafter it was dropped as unanswered.改动 / Change
新增
stripTrailingThinkingFromAssistantMessages:在工具配对之后,去掉每条 assistant 消息末尾的thinking/redacted_thinking块;整条只剩 thinking 的消息直接丢弃,再合并相邻同角色消息。ResponsesToAnthropicRequest:转换结果仍以assistant结尾时,追加一条最小的 user 消息Continue.。放在ResponsesToAnthropicRequest而不是只对自适应模型生效,因为模型别名(如带档位后缀的名字)不一定能被识别,而 Responses 协议本身永远不需要 prefill。保留最后那条 assistant 内容(而不是删掉它),模型不会丢失刚输出的上下文。Add
stripTrailingThinkingFromAssistantMessages, run after tool pairing: trailingthinking/redacted_thinkingblocks are removed from every assistant message; a message that held only thinking is dropped and adjacent same-role messages are re-merged.ResponsesToAnthropicRequestappends a minimal user turn (Continue.) when the converted messages still end onassistant. It is not limited to adaptive models, since aliased model names may not be recognized and Responses never needs prefill. The last assistant content is kept rather than dropped, so the model keeps what it just said.测试 / Tests
先写测试:新增
responses_to_anthropic_adaptive_invariants_test.go,两个用例在没有修复时都失败,修复后通过。go test ./internal/pkg/apicompat/... ./internal/handler/... ./internal/service/... ./internal/pkg/antigravity/...通过(Go 1.27.3),apicompat在-tags unit下也通过。已在我们的生产环境运行,用以 assistant 消息结尾的claude-opus-5-5请求实测返回 200。Test-first: both new cases fail without the fix and pass with it. The listed packages pass (Go 1.27.3);
apicompatalso passes with-tags unit.TestFilterThinkingBlocksSignedHistorySkipsDecode(an allocation count) occasionally fails in a fullinternal/servicerun under load and passes alone; it does not touch this code. Running in our production gateway: anassistant-terminatedclaude-opus-5-5request now returns 200.