Files
magpie/internal/gateway/message_threads.go
T
yetone 46a13f8375 gateway: a Claude Code Message Threads turn is refused so it is sent again whole (#2480, zane668, Vincentzyx)
Claude Code's Message Threads (message-threads-2026-08-12) send
"thread": {"type":"create"} on a thread's first turn and
{"type":"continue","previous_message_id":...} after, and a continue
carries only what came after that message: the history, the system
prompt and the tools are left to the server. magpie keeps no thread and
never read the field, so a continue went on to the upstream as the
latest turn alone. Vincentzyx's case (#2480): 50,381 input tokens on one
turn, 2,034 on the next, and the model asked what the task was.

What changed: /v1/messages (and /messages) answers a request that names
a thread, create or continue, before it is routed, with Anthropic's
refusal for a thread it can't serve: 400 invalid_request_error,
error.details.error_code "thread_unsupported_request". Claude Code's
tether reads that code as "unsupported_request" and sends the same turn
again stateless, with the whole history, system and tools, and keeps
that thread stateless on that model for the rest of the session (read
from Claude Code 2.1.316's own code: "[tether] ...: resending this turn
stateless; this thread stays stateless on this model for the session").
A request with no thread, or "thread": null, is served as before.
Nothing is passed through for an upstream that might keep threads: magpie
routes each turn on its own, so a continue can't be trusted to reach the
upstream that saw its create.

Run at the pushed head, on mote:
- TestMessageThreadsSentAgainWhole (new; #2480's own synthetic bodies,
  both paths, create and continue; and the turn sent again whole is
  served with its system and tools): ok.
- Broken by hand (the check turned off): red, the create and the
  continue both reach the fake upstream with "thread" in them.
- go test -tags nogui -timeout 45m ./internal/gateway/ at the rebased
  head: every test ok except
  TestCodexPoolSaysTheClientsVersion, red the same in a full run at a
  clean d39358f1b (origin/main) copy and green alone there; it is
  #4807 (another test's Codex identity reaches it).
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway/: ok;
  gofmt -l: clean.

Not tried: a real Claude Code session through magpie on this build (no
real thread was sent here; the retry is read from the client's code, not
watched). count_tokens isn't checked for a thread; Claude Code wasn't
seen sending one there.
2026-11-02 10:58:10 +08:00

48 lines
2.0 KiB
Go

package gateway
import (
"net/http"
"github.com/tidwall/gjson"
)
// Claude Code's Message Threads (the message-threads-2026-08-12 beta):
// a turn names a thread, {"type":"create"} on its first and
// {"type":"continue","previous_message_id":…} after, and a continue
// carries only what came after that message, leaving the earlier
// history, the system prompt and the tools to the server. magpie keeps
// no thread: it routes each request on its own, often to an upstream
// that never saw the first, and a continue it passed on reached the
// model as the latest turn alone, which answered as if the conversation
// had just begun (#2480, zane668; Vincentzyx's 50,381 input tokens one
// turn and 2,034 the next).
//
// So a request that names a thread is refused before it is routed, as
// Anthropic refuses one a thread can't serve: HTTP 400 with
// error.details.error_code thread_unsupported_request. Claude Code
// (2.1.316, its tether's "unsupported_request") then sends the same turn
// again stateless, with the whole history, system and tools, and keeps
// that thread stateless on that model for the rest of the session.
// threadUnsupportedCode is the code Claude Code reads from
// error.details.error_code.
const threadUnsupportedCode = "thread_unsupported_request"
// namesThread says a Messages request names a thread: a top-level
// "thread" that is not null, of any type, create as well as continue (a
// create would be followed by continues).
func namesThread(body []byte) bool {
t := gjson.GetBytes(body, "thread")
return t.Exists() && t.Type != gjson.Null
}
// refuseThread answers a request that names a thread as Anthropic's API
// answers one it can't serve on a thread.
func refuseThread(w http.ResponseWriter) {
writeJSON(w, http.StatusBadRequest, map[string]any{"type": "error", "error": map[string]any{
"type": "invalid_request_error",
"message": "Message Threads are not supported by this gateway; send the full conversation without thread.",
"details": map[string]any{"error_code": threadUnsupportedCode},
}})
}