Files
magpie/internal/gateway/tool_use_ids.go
T
yetone 09944544d6 gateway: two calls a relayed reply gave one id go back under two, each answered
An upstream reply with two tool_use blocks that share an id (a
relay's minted "call_00_dup" twice) reached the client as X and
salted(X) (#4910). On the next request saltedCallIDsBack unsalted
both to X, and pairedMessages then took the second tool_result for a
second answer to the same call and dropped it. Upstream got two
tool_use X and one result, which DeepSeek's Anthropic endpoint turns
away with 400 "tool_use ids must be unique".

saltedCallIDsBack now leaves a salted id salted, on both the call and
its result, when the unsalted id is already taken in that message by
a plain call or an earlier salted one (sharedCallIDs). The first
call keeps its own id and the second goes upstream under the mp_ id,
as the translated route already sends salted ids (anthropicInID).

Run (mote, at this commit on 4d79bebfa):
- TestMessagesRelayedSharedIDBothAnswered (new, fake upstream, no
  network). Red on the old code: upstream got calls
  ["call_00_dup","call_00_dup"] answered by ["call_00_dup"], with
  ANSWER-TWO missing. Broken by hand (kept := nil): red the same way.
  With the fix it passes, as does -run 'ToolUseID|SharedID|ToolBlocks|Paired'.
- go test ./... -timeout 45m -count=1: every package ok, internal/gateway
  included (1171s), except:
  - internal/cli: TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
    TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
    TestUpdateFindsAMagpieThatWroteNoRecord. Red the same at 4d79bebfa:
    mote's 127.0.0.1:3425 is an ssh forward (#6223, #6471).
  - internal/gui TestLaneLimitRoutes: it read the live gateway's busy
    zcode lane (#5150). Passes 3/3 at both this head and base when the
    lane is idle.
  - internal/gui TestSetPortServesWhenTheOldOneWasTaken: the TempDir
    cleanup found .config/magpie not empty. Passes alone; filed as #7112.
- gofmt -l clean; GOOS=linux go vet -tags nogui ./... and GOOS=windows
  go vet ./... ok.
- Live DeepSeek (api.deepseek.com/anthropic, deepseek-flash): the new
  shape (own id plus the mp_ id, both answered) gets 200. The old shape
  (same id twice, one result) and the same id twice with both results
  each get 400 "tool_use ids must be unique".

Not tried: whether wizcheu's 400 ("tool_use ids were found without
tool_result blocks immediately after") comes from this path. DeepSeek
words this path's error differently, so probably not.
2026-11-18 22:02:34 +08:00

323 lines
8.8 KiB
Go

package gateway
import (
"bytes"
"encoding/base64"
"encoding/json"
"strings"
"unicode/utf8"
"github.com/tidwall/gjson"
)
// Claude Code keeps every tool_use id of a session in one Set, and drops a
// block whose id it has already seen; a turn whose only block is dropped
// becomes the placeholder "[Tool use interrupted]" (#4910). An upstream
// that speaks Messages itself and is relayed as it came may number its
// calls' ids by their place in the reply (LiteLLM's bridge: fc_0 on every
// turn), so the id the client saw on one turn comes again on the next, and
// every later call of the session is lost. A reply whose tool_use id is one
// the client already holds — an assistant block of the request the reply
// answers, or one the reply itself said before it — goes out under the
// mark, the upstream's own id and a fresh salt (saltCallID), which nothing
// the client holds names; the answer comes back under the salted id and
// goes to the upstream as the id it gave (saltedCallIDsBack). An id the
// client has seen no more goes as it came, so an upstream whose ids are its
// own is relayed byte for byte.
// saltCallID is id for a Messages client, salted so that no two replies of
// a session make it alike: the mark and the base64url of the id, a NUL and
// the salt, which unsaltCallID turns back into the id alone.
func saltCallID(id, salt string) string {
return anthropicIDMark + base64.RawURLEncoding.EncodeToString([]byte(id+"\x00"+salt))
}
// unsaltCallID is the id saltCallID was given, and whether it was one. An
// id that wasn't — a client's own, or one another mark holds — is none.
func unsaltCallID(id string) (string, bool) {
if !strings.HasPrefix(id, anthropicIDMark) || !safeIDChars.MatchString(id) {
return "", false
}
raw, err := base64.RawURLEncoding.DecodeString(id[len(anthropicIDMark):])
if err != nil || !utf8.Valid(raw) {
return "", false
}
at := bytes.IndexByte(raw, 0)
if at <= 0 || at == len(raw)-1 {
return "", false // no salt, or no id
}
return string(raw[:at]), true
}
// clientToolUseIDs is every tool_use id of a Messages request's assistant
// turns: what the client holds, and would drop a reply's block over saying
// again (#4910).
func clientToolUseIDs(body []byte) map[string]bool {
if !bodyHasBytes(body, toolUseType) {
return nil
}
var q struct {
Messages []struct {
Content json.RawMessage `json:"content"`
} `json:"messages"`
}
if decodeJSON(body, &q) != nil {
return nil
}
var seen map[string]bool
for _, m := range q.Messages {
var blocks []struct {
Type string `json:"type"`
ID string `json:"id"`
}
if decodeJSON(m.Content, &blocks) != nil {
continue // a content of text alone, or a string
}
for _, b := range blocks {
if b.Type == "tool_use" && b.ID != "" {
if seen == nil {
seen = map[string]bool{}
}
seen[b.ID] = true
}
}
}
return seen
}
// saltedCallIDsBack gives a Messages request the ids the upstream made: an
// id that went out salted comes back as the client holds it, and the
// upstream that made the call gets its own again, on the call and on the
// result answering it. The rest goes as it came.
//
// Two calls of one reply the upstream gave one id (a bridge numbering them
// by place, #4910) went out under two; they go back under two as well:
// the first under the upstream's id, each later one under the salted id
// the client holds, on the call and on its result. Given back the one id
// twice, the pairing (tool_blocks_paired.go) took the second result for a
// second answer to the first call and left it out, and the upstream got
// two calls with one result, a 400 "tool_use ids were found without
// tool_result blocks immediately after" on every later request of the
// conversation.
func saltedCallIDsBack(body []byte) []byte {
if !bodyHas(body, `"`+anthropicIDMark) {
return body
}
var reply map[string]json.RawMessage
if decodeJSON(body, &reply) != nil || reply == nil {
return body
}
var msgs []json.RawMessage
if decodeJSON(reply["messages"], &msgs) != nil {
return body
}
kept := sharedCallIDs(msgs)
changed := false
for i, m := range msgs {
var one map[string]json.RawMessage
if decodeJSON(m, &one) != nil || one == nil {
continue
}
var blocks []map[string]json.RawMessage
if decodeJSON(one["content"], &blocks) != nil {
continue
}
mended := false
for _, b := range blocks {
for _, field := range []string{"id", "tool_use_id"} {
var id string
if decodeJSON(b[field], &id) != nil || id == "" || kept[id] {
continue
}
raw, ok := unsaltCallID(id)
if !ok {
continue
}
b[field], _ = marshalPlain(raw)
mended, changed = true, true
}
}
if mended {
one["content"], _ = marshalPlain(blocks)
msgs[i], _ = marshalPlain(one)
}
}
if !changed {
return body
}
reply["messages"], _ = marshalPlain(msgs)
nb, err := marshalPlain(reply)
if err != nil {
return body
}
return nb
}
// sharedCallIDs is the salted tool_use ids that would name the same call
// as another tool_use of their message once unsalted: an id that came
// plain takes it first, then the salted ones in order, the first of which
// takes it if no plain one did. Those after stay salted.
func sharedCallIDs(msgs []json.RawMessage) map[string]bool {
var kept map[string]bool
for _, m := range msgs {
var one struct {
Content json.RawMessage `json:"content"`
}
var blocks []struct {
Type string `json:"type"`
ID string `json:"id"`
}
if decodeJSON(m, &one) != nil || decodeJSON(one.Content, &blocks) != nil {
continue
}
taken := map[string]bool{}
var salted []string
for _, b := range blocks {
if b.Type != "tool_use" || b.ID == "" {
continue
}
if _, ok := unsaltCallID(b.ID); ok {
salted = append(salted, b.ID)
} else {
taken[b.ID] = true
}
}
for _, id := range salted {
raw, _ := unsaltCallID(id)
if !taken[raw] {
taken[raw] = true
continue
}
if kept == nil {
kept = map[string]bool{}
}
kept[id] = true
}
}
return kept
}
// toolUseType is the kind of block whose id the client holds against a
// reply's own.
var toolUseType = []byte(`"tool_use"`)
// toolUseIDs salts the tool_use ids of a Messages reply relayed as it came:
// one the client already holds, or one this reply itself has said, goes out
// salted, so no block of the session names another's id again (#4910). A
// line that names no tool_use, or an id the client holds no more, passes as
// it came; a reply that isn't a stream is held whole and gone through at
// the end.
type toolUseIDs struct {
seen map[string]bool // the client's, and this reply's own, so far
sse bool
buf []byte
}
func (t *toolUseIDs) write(b []byte) []byte {
t.buf = append(t.buf, b...)
if !t.sse {
return nil
}
out, rest := tidyEvents(t.buf, t.event)
t.buf = rest
return out
}
func (t *toolUseIDs) flush() []byte {
b := t.buf
t.buf = nil
if !t.sse {
return t.whole(b)
}
// what events remain go as they came, the reply may end inside one
out, rest := tidyEvents(b, t.event)
return append(out, rest...)
}
func (t *toolUseIDs) event(ev []byte) []byte {
data := eventData(ev)
if len(data) == 0 || !bodyHasBytes(data, toolUseType) || !gjson.ValidBytes(data) {
return ev
}
e := gjson.ParseBytes(data)
if e.Get("type").String() != "content_block_start" ||
e.Get("content_block.type").String() != "tool_use" {
return ev
}
id := e.Get("content_block.id").String()
if id == "" {
return ev
}
if !t.seen[id] {
if t.seen == nil {
t.seen = map[string]bool{}
}
t.seen[id] = true
return ev
}
salted := saltCallID(id, newID())
var m map[string]any
if decodeNumbers(data, &m) != nil {
return ev
}
if cb, _ := m["content_block"].(map[string]any); cb != nil {
cb["id"] = salted
} else {
return ev
}
nd, err := marshalPlain(m)
if err != nil {
return ev
}
if t.seen == nil {
t.seen = map[string]bool{}
}
t.seen[salted] = true
return withData(ev, nd)
}
// whole is a reply that came whole: the tool_use ids of its content the
// client already holds, or an earlier block of the reply said, go out
// salted.
func (t *toolUseIDs) whole(b []byte) []byte {
if !bodyHasBytes(b, toolUseType) {
return b
}
var reply map[string]json.RawMessage
if decodeJSON(b, &reply) != nil || reply == nil {
return b
}
var blocks []map[string]json.RawMessage
if decodeJSON(reply["content"], &blocks) != nil {
return b
}
changed := false
for _, cb := range blocks {
var typ, id string
if decodeJSON(cb["type"], &typ) != nil || typ != "tool_use" {
continue
}
if decodeJSON(cb["id"], &id) != nil || id == "" {
continue
}
if !t.seen[id] {
if t.seen == nil {
t.seen = map[string]bool{}
}
t.seen[id] = true
continue
}
cb["id"], _ = marshalPlain(saltCallID(id, newID()))
changed = true
}
if !changed {
return b
}
reply["content"], _ = marshalPlain(blocks)
nb, err := marshalPlain(reply)
if err != nil {
return b
}
return nb
}