Files
magpie/internal/gateway/thinking_bound.go
T
yetone f3335dd1b4 gateway: DeepSeek's thinking is left out of a request to Claude (GgbondHi6930 on X)
GgbondHi6930 on X: a Smart group of DeepSeek and a Claude API key. A
conversation ran on DeepSeek at max effort; with DeepSeek unticked its
next turn went to Claude and failed "signature in thinking". A new
conversation worked.

Cause: DeepSeek's Anthropic endpoint signs each thinking block with its
reply's id, a UUID (checked against api.deepseek.com/anthropic). The
agent keeps those blocks and sends them back, so the first Claude turn
carries them, and Claude refuses the request, 400 "messages.1.content.0:
Invalid `signature` in `thinking` block" (real reply through
OpenRouter's Anthropic route, req_011CgfP4zyRcUhPiTg5sLpoe). Until now
magpie mended this only after the refusal, in forward's bound-thinking
retry. That needs the refusal to reach forward as a 400/422 with
Claude's wording, and it spends a request. With fakes that answer as
Claude does, main recovers through it, on Messages (whole and streamed)
and on Codex's Responses. So the reporter's failure is where that retry
can't read the refusal. A relay's status or wording is the likely case;
it is not reproduced exactly.

Change: forwardOnce, which every route to an Anthropic upstream passes
(relayed, translated Chat/Responses, group fallback and Smart routing,
a plugin's fetch, count_tokens), now calls withoutForeignThinking. For
an upstream serving Claude (api.anthropic.com, or a claude- model name)
and not DeepSeek, it leaves out each thinking block whose signature is a
UUID. Those blocks never reach Claude, and refuseBoundThinking reads a
refusal's indices against that same body. These go byte for byte:
- Claude's own signatures (base64; 3,000 of Claude Code's own were
  checked, all base64 and 800+ characters);
- unsigned blocks (Claude took one in a real request);
- redacted_thinking;
- other vendors' signatures, which the retry still mends.
DeepSeek's endpoint and every other upstream get the body unchanged.

Tests (thinking_switch_test.go):
- TestDeepSeekThinkingThenClaude: the reporter's case, whole and
  streamed. 3 DeepSeek tool turns, then DeepSeek unticked and 3 Claude
  turns. Claude refuses nothing and gets its own blocks back as signed.
  With DeepSeek ticked again and Claude unticked, DeepSeek is sent every
  block, its own and Claude's.
- TestDeepSeekThinkingThenClaudeResponses: the same through Codex's
  /v1/responses.
- TestWithoutForeignThinking: Claude, DeepSeek and Kimi upstreams.

Run on mote (mote-run):
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: clean, at cfa9a6987 (base 17d2ef10f).
- go test -timeout 45m ./internal/gateway/... ./internal/provider/...
  at cfa9a6987: provider ok. Gateway ok except
  TestCodexPoolSaysTheClientsVersion, which is #4807 (passes alone 3/3;
  commented there). It was fixed on main by 17f2387e1, which this commit
  is rebased onto.
- After the rebase onto 17f2387e1, which touched none of these files'
  code: -run 'Thinking|Bound|TestCodexPoolSaysTheClientsVersion' ok.

Broken by hand: with withoutForeignThinking returning the body as it
came, all three tests go red ("Claude refused 1 of its 4 requests",
"Claude refused 1 of its 3 requests", "message 1 = [thinking,text]").

Not tried:
- a real Claude account through magpie; no Anthropic key was usable
  here, and the relay accounts found (aihubmix, yylx) are out of credit;
- the reporter's own relay;
- Chat Completions clients, which carry no signatures;
- Kimi, GLM and MiniMax signatures, which the refusal path still mends.
2026-11-03 13:16:35 +08:00

244 lines
8.4 KiB
Go

package gateway
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"regexp"
"strconv"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// Claude's thinking block is signed for the conversation it was made in
// and the account that made it. A block an upstream doesn't take as its
// own is refused with the whole request (ferock on Discord, Command Code's
// plugin): 400 "messages.3.content.20: Invalid signature in thinking
// block. The block is bound to a different conversation. Remove the
// block, or set thinking.block_binding.prefix_mismatch_behavior to
// "drop_block"" (that setting needs a beta magpie doesn't send for an
// agent). Such a block comes from a conversation that moved between
// accounts or providers (a routing group's fallback, an account switch,
// a relay that serves Claude from more than one account of its own) or
// whose earlier turns changed since.
//
// forward does what the refusal asks: it leaves out the block it names
// and every thinking block after it (their signatures cover the turns
// before them, the left-out block among them), asks once more, and
// remembers those signatures for that provider and account
// (thinkingRefused), so the conversation's later turns go without them
// the first time and stay the conversation the model has gone on in.
// Other blocks, and every request to another provider or account, go as
// the agent wrote them. Only Claude's refusal text starts it, so no other
// vendor's request changes.
// boundRefusal is Claude's refusal of a thinking block it didn't sign for
// this conversation or account: the signature's wording, the newer
// binding's, and a redacted_thinking block's.
var boundRefusal = regexp.MustCompile("(?i)invalid `?signature`? in `?thinking`? block|bound to a different conversation|not created in this conversation|invalid data in `?redacted_thinking`? block")
// boundAt is where the refusal says the block is: messages.<m>.content.<b>.
var boundAt = regexp.MustCompile(`messages\.(\d+)\.content\.(\d+)`)
// thinkingRefusedMost bounds the signatures remembered; past it the
// memory starts again, and a conversation still going is mended by one
// more refusal.
const thinkingRefusedMost = 1 << 14
// thinkingOwner is who a refusal is remembered for: the provider, and the
// account signed in to it.
func thinkingOwner(p provider.Provider) string {
if p.Account != nil {
return p.ID + "\x00" + p.Account.User
}
return p.ID
}
// thinkingKey names a thinking or redacted_thinking block by its
// signature (a redacted block's data), or its text where it has neither.
func thinkingKey(b gjson.Result) string {
k := b.Get("signature").String()
if k == "" {
k = b.Get("data").String()
}
if k == "" {
k = b.Get("thinking").String()
}
h := sha256.Sum256([]byte(b.Get("type").String() + "\x00" + k))
return hex.EncodeToString(h[:16])
}
func isThinkingBlock(b gjson.Result) bool {
t := b.Get("type").String()
return t == "thinking" || t == "redacted_thinking"
}
// thinkingRefused tells whether p refused the block with key.
func (s *Server) thinkingRefused(owner, key string) bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.refusedThinking[owner+"\x00"+key]
}
func (s *Server) refuseThinking(owner string, keys []string) {
s.mu.Lock()
defer s.mu.Unlock()
if s.refusedThinking == nil || len(s.refusedThinking)+len(keys) > thinkingRefusedMost {
s.refusedThinking = make(map[string]bool)
}
for _, k := range keys {
s.refusedThinking[owner+"\x00"+k] = true
}
}
// withoutRefusedThinking is a Messages body without the thinking blocks p
// refused before; an assistant message left with nothing goes too. A body
// with none of them goes byte for byte.
func (s *Server) withoutRefusedThinking(p provider.Provider, body []byte) []byte {
if !bytes.Contains(body, []byte(`thinking"`)) {
return body
}
owner := thinkingOwner(p)
s.mu.Lock()
none := len(s.refusedThinking) == 0
s.mu.Unlock()
if none {
return body
}
return withoutThinkingBlocks(body, func(_, _ int, b gjson.Result) bool {
return s.thinkingRefused(owner, thinkingKey(b))
})
}
// withoutThinkingBlocks leaves out of a Messages body each thinking or
// redacted_thinking block drop says to, at message m, block i, and an
// assistant message that had only those.
func withoutThinkingBlocks(body []byte, drop func(m, i int, b gjson.Result) bool) []byte {
var q map[string]json.RawMessage
var msgs []json.RawMessage
if decodeJSON(body, &q) != nil || decodeJSON(q["messages"], &msgs) != nil {
return body
}
out := make([]json.RawMessage, 0, len(msgs))
changed := false
for mi, raw := range msgs {
content := gjson.GetBytes(raw, "content")
if !content.IsArray() {
out = append(out, raw)
continue
}
var kept []json.RawMessage
gone := false
for bi, b := range content.Array() {
if isThinkingBlock(b) && drop(mi, bi, b) {
gone = true
continue
}
kept = append(kept, json.RawMessage(b.Raw))
}
if !gone {
out = append(out, raw)
continue
}
changed = true
if len(kept) == 0 {
continue
}
var m map[string]json.RawMessage
if decodeJSON(raw, &m) != nil {
out = append(out, raw)
continue
}
m["content"], _ = json.Marshal(kept)
nb, err := json.Marshal(m)
if err != nil {
return body
}
out = append(out, nb)
}
if !changed {
return body
}
q["messages"], _ = json.Marshal(out)
b, err := json.Marshal(q)
if err != nil {
return body
}
return b
}
// refuseBoundThinking reads a 400 from an Anthropic upstream: when it is
// Claude refusing a thinking block's signature, the block it names and
// every thinking block after it in body (as forwardOnce sends it, without
// those refused before) are remembered as refused by p. fresh is whether
// any was new, so asking again can go otherwise.
func (s *Server) refuseBoundThinking(p provider.Provider, errBody, body []byte) (fresh bool) {
if !boundRefusal.Match(errBody) {
return false
}
sent := withoutForeignThinking(p, s.withoutRefusedThinking(p, body))
msgs := gjson.GetBytes(sent, "messages").Array()
// from the block named, or from the start when it names none, or one
// that isn't a thinking block in what was sent
fromM, fromB := 0, 0
if at := boundAt.FindSubmatch(errBody); at != nil {
m, _ := strconv.Atoi(string(at[1]))
b, _ := strconv.Atoi(string(at[2]))
if m < len(msgs) {
if blocks := msgs[m].Get("content").Array(); b < len(blocks) && isThinkingBlock(blocks[b]) {
fromM, fromB = m, b
}
}
}
owner := thinkingOwner(p)
var keys []string
for mi := fromM; mi < len(msgs); mi++ {
for bi, b := range msgs[mi].Get("content").Array() {
if mi == fromM && bi < fromB || !isThinkingBlock(b) {
continue
}
if k := thinkingKey(b); !s.thinkingRefused(owner, k) {
keys = append(keys, k)
}
}
}
if len(keys) == 0 {
return false
}
s.refuseThinking(owner, keys)
return true
}
// A conversation that ran on DeepSeek's Anthropic endpoint first (a
// routing group with DeepSeek unticked, then Claude: GgbondHi6930 on X)
// holds thinking blocks DeepSeek signed with its reply's id, a UUID.
// Claude refuses each one, 400 "messages.1.content.0: Invalid `signature`
// in `thinking` block" (checked on 2026-11-03), which forward mends only
// where the refusal reaches it as Claude's own 400, and with a request
// spent. withoutForeignThinking leaves those blocks out of a request to an
// upstream serving Claude before it sees them: Claude's own signatures are
// base64 (3,000 of Claude Code's, 800 characters and longer), never a
// UUID. A block with no signature Claude takes (checked the same day), so
// it goes as it was, and so does every other vendor's signature, which
// refuseBoundThinking still mends. Every other upstream, DeepSeek's among
// them, gets the body byte for byte.
func withoutForeignThinking(p provider.Provider, body []byte) []byte {
if !bytes.Contains(body, []byte(`"signature"`)) {
return body
}
model := modelOf(body)
if anthropicReplaysThinking(p.Host(), model) || p.Host() != "api.anthropic.com" && !anthropicModel.MatchString(model) {
return body
}
return withoutThinkingBlocks(body, func(_, _ int, b gjson.Result) bool {
return b.Get("type").String() == "thinking" && deepseekSignature.MatchString(b.Get("signature").String())
})
}
// deepseekSignature is the signature DeepSeek's Anthropic endpoint gives
// its thinking: the reply's id, a UUID.
var deepseekSignature = regexp.MustCompile(`(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)