gateway: xAI's own API takes neither a sealed item's null content nor a system turn in messages (#3392, kongkongklng)

The follow-up to 8ecab590, after the reporter merged it: the first Codex
turn through an xAI key went through and every one after it 400ed "Could
not decode the compaction blob. Ensure it is unmodified from the compact
response." — Codex hands the sealed reasoning item back with "content":
null beside its encrypted_content, which xAI's backend can't read. The
Grok subscription's plugin already drops that null (grokShaped), but the
xai preset shapes nothing, and the custom-tool refusal had only now let
a second turn reach the API. And every Claude call 400ed "Invalid request
content: Invalid message role.": Claude Code sends a system turn
mid-conversation (Anthropic made them GA), a role xAI's Messages takes
only as the top-level system.

xaiBody (api.x.ai, shaped in passthrough and on the built body in
forwardTranslated alike — a custom-tool refusal puts a Codex request on
the translated path from the next turn on) drops a reasoning item's null
content and folds each system turn into the top-level system, its text
and blocks as they came. Nothing is learned: xAI takes neither at all.

TestXAITakesCodexsSealedReasoningBack, TestXAILearnsCustomToolsThenTakesTheReplay
and TestXAIFoldsClaudesSystemTurn replay the reporter's session on a fake
api.x.ai and are red with the shaping off; the wording comes from the
report. gofmt clean, go vet and the builds (native, linux nogui, windows)
clean; go test -tags nogui ./... in a sandbox HOME: 66 packages ok, the
same 7 test failures as the clean branch head baselined the same session
(TestWriteAtomicReadOnlyHardLink, TestClaudeClientCacheAbsentUnchanged,
TestCodexFittedImagesPastTheBudget, TestMarshalRawIsEncodingJSON,
FuzzMarshalRaw, TestPluginSOCKSProxy, TestProgram). Not tried against the
real xAI API: no key here.

From skygunner's branch fix/issue-batch-1024pm; kongkongklng confirmed it on 10-25.
This commit is contained in:
vincent
2026-10-28 16:52:55 +08:00
committed by yetone
parent b630304322
commit d8938d2f00
4 changed files with 391 additions and 1 deletions
+2 -1
View File
@@ -279,7 +279,8 @@ recorded as a prediction failure (`noPrediction`).
- A model that refuses a conversation ending with the assistant's message (`refusesPrefill`: OpenCode Go's Claude models over Messages, "This model does not support assistant message prefill"; Volcengine's Agent Plan, "…for a model that does not support prefill"; Gemini's, "Requests ending with a model turn are not supported") is marked for that provider, model and API (`prefillRefused`, #1447); the provider's other models keep their prefill ([`prefill.go`](../../internal/gateway/prefill.go)). A Chat or Responses client's conversation ending with the assistant's message, which those APIs read as a turn already said (`Request.LastIsTurn`), is asked again with a user turn after it (`continueText`), translated (`forwardTranslated`) or relayed (`passthrough`, `userLastBody`), and so from then on. An Anthropic client's prefill is its own ask and gets the refusal as sent. magpie's own continuation of a cut reply (`continuation.go`) isn't asked of that model again; the try the model refused ends the reply with what cut it, not with the refusal. A request magpie builds in Gemini's own shape (`buildCodeAssistSent`: Vertex AI, Gemini CLI's and Antigravity's Code Assist, Factory) never ends with the model's turn, which all of them turn away: a user turn goes after it, `continueText`, or `compactLastText` when an agent is compacting with its ask in the system prompt (`modelLastAsk`), as one goes before a history that opens with the model's. Nor does it send a user turn that mixes a tool's `functionResponse` with text, which Vertex AI answers with the same "ending with a model turn" (#1533: Codex's compaction prompt after a tool's output): what the user said goes in a user turn of its own after the tools' responses (`hasResponse`), except for a Claude model on Antigravity, whose tool_result turn holds both.
- A tool's `strict` that an upstream turns away (`toolStrictRefusal` in [`strict_refused.go`](../../internal/gateway/strict_refused.go): "Unsupported Chat Completions API field: tools[0].function.strict", OpenAI-style unknown_parameter, "Extra inputs are not permitted" and pydantic's extra_forbidden on it) is asked once more without it, in Chat's `tools[].function` and Responses' `tools[]` alike, and marked for that provider and API (`toolStrictRefused`): its next requests go without it at once. OpenCode 2 sends `"strict": false` on every tool through `@ai-sdk/openai-compatible`, and a custom provider that refused it ended the session (#1980). An upstream that hasn't refused it still gets it, as the ChatGPT backend needs it (#383). `TestToolStrictLeftOutWhereRefused` (real OpenCode 2.0.25 requests), `TestToolStrictRefusalWordings`.
- A history call to a tool the request doesn't declare, which an agent that loads its tools on demand sends (pi's and T3 Code's `tool_search`: the tools a search found are called in the history but not declared in the next request's `tools`, and pi sends `"tools": []` with no tools left; Gearclickist on Discord), goes as sent where it is taken: OpenAI's Chat and Responses, the ChatGPT backend and Gemini take it, and Anthropic takes a call to a tool it isn't given ([`undeclared_tools.go`](../../internal/gateway/undeclared_tools.go)). Anthropic refuses calls with no tools at all ("Requests which include `tool_use` or `tool_result` blocks must define tools"), so `anthropicBody` declares a stub for each tool the history called when the request has none (description "Called earlier in this conversation; not available now.", any object as its input) with `tool_choice` `none`, as the Kiro plugin declares its history's tools for Kiro. An upstream that turns the request away naming such a tool as not found, unknown or not defined, or that says tools must be defined beside the calls (`refusesHistoryTools`: a relay in front of Kiro or Bedrock, a vendor checking each name), is asked once more with a stub declared for each tool the history called and `tools` lacks (`withHistoryTools`, Chat, Responses and Messages bodies, relayed or translated), `tool_choice` `none` only where the client offered no tools, and marked for that provider and API (`historyToolsRefused`): its next requests carry the stubs at once. A Responses call under a namespace is the namespace's, and a request whose calls are all declared goes as it was. `TestUndeclaredHistoryToolRefusedThenStubbed`, `TestToollessHistoryToAnthropicDeclaresStubs`, `TestToollessHistoryRefusedOnChat`, `TestUndeclaredHistoryToolTakenAsSent`, `TestRefusesHistoryToolsMatch`.
- A Codex custom tool (exec, apply_patch) that a Responses upstream turns away (`customToolRefusal` in [`custom_tool.go`](../../internal/gateway/custom_tool.go): DeepSeek's 400 "Unsupported custom tool: 'exec'. Only 'apply_patch' is supported.", #2656) ends the relay: `passthrough` marks the model (`customToolRefused`) and returns not done, and `attempt` translates the request on the same API (`ownAPI`), where each custom tool is offered as a function taking its `input` and a call comes back as the `custom_tool_call` Codex runs, its name kept. A marked model's next requests with a custom tool are translated at once; one without stays relayed, and an upstream that never refused them still gets them as sent. A compaction (`keepsCompaction`) isn't translated, and gets the refusal. Where the refusal reaches a group untranslated it is a shape refusal (`shapeRefused`): the next member is asked. `TestCustomToolTranslatedWhereRefused`, `TestCustomToolRefusalInGroup`, `TestCustomToolRefusalWordings`.
- A Codex custom tool (exec, apply_patch) that a Responses upstream turns away (`customToolRefusal` in [`custom_tool.go`](../../internal/gateway/custom_tool.go): DeepSeek's 400 "Unsupported custom tool: 'exec'. Only 'apply_patch' is supported.", #2656; xAI's 422 "tools[N].type: unknown variant `custom`, expected one of …", Rust's own wording of the type, #3392) ends the relay: `passthrough` marks the model (`customToolRefused`) and returns not done, and `attempt` translates the request on the same API (`ownAPI`), where each custom tool is offered as a function taking its `input` and a call comes back as the `custom_tool_call` Codex runs, its name kept. A marked model's next requests with a custom tool are translated at once; one without stays relayed, and an upstream that never refused them still gets them as sent. A compaction (`keepsCompaction`) isn't translated, and gets the refusal. Where the refusal reaches a group untranslated it is a shape refusal (`shapeRefused`): the next member is asked. `TestCustomToolTranslatedWhereRefused`, `TestCustomToolRefusalInGroup`, `TestCustomToolRefusalWordings`.
- xAI's own API (api.x.ai, wherever a Grok model is served — the Grok subscription's plugin shapes its own bodies, provider's `grokShaped`) takes neither of two things a client's wire puts in wrong (`xaiBody` in [`xai_body.go`](../../internal/gateway/xai_body.go), shaped in `passthrough` and on the built body in `forwardTranslated` alike, for a custom-tool refusal puts a Codex request on the translated path from the next turn on; there is nothing to learn, xAI takes neither at all, #3392's follow-up): Codex hands a sealed reasoning item back with `"content": null` beside its `encrypted_content`, which the backend answers "Could not decode the compaction blob. Ensure it is unmodified from the compact response." (the first turn goes through, every one after it 400s) — the null goes, the item otherwise as it came; and Claude Code, its messages carrying a system turn mid-conversation since Anthropic made them GA, sends one that xAI's Messages answers "Invalid request content: Invalid message role." — each folds into the top-level `system`, its text and blocks (a `cache_control` kept) as they came, the only place that API takes the role. `TestXAITakesCodexsSealedReasoningBack`, `TestXAILearnsCustomToolsThenTakesTheReplay`, `TestXAIFoldsClaudesSystemTurn`.
- A tool call's arguments reach the client as the vendor writes them, translated too (echo_ts on Discord: Codex on Copilot about 10x slower, an apply_patch shown only once whole). The Responses decoder hands each `function_call_arguments.delta` and `custom_tool_call_input.delta` on as a `KToolArgs` delta (`argsDelta`), holding back only the one that would make the JSON whole until `.done`, which `endCall` checks against the final arguments; the Responses encoder streams a custom tool's input as `custom_tool_call_input.delta` (`customStream` in [`custom_tool.go`](../../internal/gateway/custom_tool.go)) before its `.done`. A Copilot account's Responses request is relayed with a `web_search` tool in it (`searchesItself`; checked on a real account 2026-10-17), and Copilot is never picked to search for another provider's model (`searchRank`). It is translated instead when a tool marks a parameter `"encrypted": true` (`asksSealing`, MultiAgentV2's `spawn_agent`): Copilot seals such an argument and can't read its own seal on the subagent's turn. `TestCallArgsStreamWhenTranslated`, `TestCustomInputStreams`, `TestResponsesCallArgsStream`, `TestCopilotRelayedUnlessAskedToSeal`, `TestCopilotUltraSubagentWithWebSearch`.
- A relayed Responses stream gives each output item the one id its `response.output_item.added` gave it, in every event after that carries an `item_id` or an `item`, and in the `output` of `response.completed`, `response.incomplete` or `response.failed` (`idTidy` in [`itemids.go`](../../internal/gateway/itemids.go), applied in `passthrough` before `phaseTidy`). GitHub Copilot's `/responses` gives an item another id in every event (checked 2026-10-19 on a real account, gpt-5.5 with web_search), and Codex starts an item by the added's id and ends one by the done's, so a commentary message was shown twice and a web search never ended (echo_ts on Discord, `TestCopilotItemKeepsItsID`). An upstream whose ids agree goes through byte for byte (`TestItemIDsThatAgreeGoAsTheyCame`); a translated reply's ids are magpie's own.
- A model's reply limit is published within the window beside it (#1438, `provider.OutputWithin`, `Entry.PublishedOutput`): `/v1/models`' `max_output_tokens` (`modelObject`), Cursor Private Inference's `capabilities.max_output_tokens` (`cursorLocalModel`), Muse Code's `limit.output` (`museModels`), the GUI's model and group rows (`providerInfo`, `providersState`), `magpie model output`, and every agent's file (`agent.maxTokens`, #338). The window and the output can come from different sources: Grok's backend lists grok-4.7's 256000 window, and models.dev fills its 500000 output (`catalog.Decorate` and `Provider.replyLimit` fill an output only where the vendor gives none). An unknown window or output is published as it is. The catalog keeps the unclamped `Entry.Output`, and a group's member is still asked for no more than that (`outputLimit`, `withMaxOutput`), so a request isn't lowered by the clamp. A limit the user set stays in their settings as they set it (`TestModelListsKeepOutputWithinTheWindow`). Outside a group, only a Max output the user set for the model (`Entry.OutputSet`) lowers a request (`userOutput`): Claude Code asks a claude-* model for its own full limit (128000 on Opus 5.5) and is told no figure of magpie's for it (`claudeOutput`), and a key whose quota is held per request as max_tokens times the price turned every turn away (YaCo on Discord, `TestUserMaxOutputLowersAPlainProvidersRequest`). A vendor's or models.dev's figure doesn't lower it, since it may be stale.
+7
View File
@@ -4699,6 +4699,9 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
} else if !s.fits(p.ID, schemaRefused(model), proto) {
body, _ = formatInBody(proto, body, true)
}
// xAI's own API takes neither a sealed reasoning item's null content
// nor a system turn in Messages (xai_body.go, #3392)
body = xaiBody(p, proto, body)
// a Grok subscription is given Codex's namespaced functions flat
// (grokBody), and Zed's plugin likewise (ZedBody); a call to one goes
// back under its namespace (#404)
@@ -5623,6 +5626,10 @@ func (s *Server) forwardTranslated(ctx context.Context, p provider.Provider, to
// (undeclared_tools.go)
body, _, _ = withHistoryTools(to, body)
}
// xAI's own API takes neither a sealed reasoning item's null
// content nor a system turn in Messages (xai_body.go, #3392) —
// the built body as the relayed one
body = xaiBody(p, to, body)
if to == provider.CodeAssist && p.Account != nil {
// the envelope is named in there, where the id it carries is
// known: on Antigravity that is the variant the effort picked,
+139
View File
@@ -0,0 +1,139 @@
package gateway
import (
"bytes"
"encoding/json"
"github.com/yetone/magpie/internal/provider"
)
// xAI's own API (api.x.ai), wherever a Grok model is served, takes two
// things a client's wire puts in wrong, as the Grok subscription's plugin
// shapes its bodies too (provider's grokShaped):
//
// - Codex hands a sealed reasoning item back with "content": null
// beside its encrypted_content, which the backend answers "Could not
// decode the compaction blob. Ensure it is unmodified from the compact
// response." (#3392, kongkongklng: the first turn went through, every
// one after it 400ed) — the null goes.
// - Claude Code sends a system turn mid-conversation (Anthropic made
// them GA), which xAI's Messages answers "Invalid request content:
// Invalid message role." (#3392: every claude call 400ed) — each folds
// into the top-level system, the only place it takes the role.
//
// The relay and the translation shape alike: a refusal of custom tools
// puts a Codex request on the translated path from the next turn on
// (custom_tool.go), and there is nothing to learn from either answer —
// xAI takes no null content and no system turn at all.
// xaiBody shapes body as xAI's own API takes it on proto.
func xaiBody(p provider.Provider, proto provider.Protocol, body []byte) []byte {
if p.Host() != "api.x.ai" {
return body
}
switch proto {
case provider.Responses:
return withoutNullReasoningContent(body)
case provider.Anthropic:
return systemTurnsFolded(body)
}
return body
}
// withoutNullReasoningContent is a Responses body with the null content of
// a reasoning item gone (#3392).
func withoutNullReasoningContent(body []byte) []byte {
if !bytes.Contains(body, []byte(`"reasoning"`)) || !bytes.Contains(body, []byte(`"content"`)) {
return body
}
dec := json.NewDecoder(bytes.NewReader(body))
dec.UseNumber()
var m map[string]any
if dec.Decode(&m) != nil || m == nil {
return body
}
dirty := false
items, _ := m["input"].([]any)
for _, it := range items {
im, _ := it.(map[string]any)
if im == nil || im["type"] != "reasoning" {
continue
}
if c, there := im["content"]; there && c == nil {
delete(im, "content")
dirty = true
}
}
if !dirty {
return body
}
out, err := marshalPlain(m)
if err != nil {
return body
}
return out
}
// systemTurnsFolded is an Anthropic body with each system turn in messages
// folded into the top-level system, its text and blocks as they came
// (#3392).
func systemTurnsFolded(body []byte) []byte {
if !bytes.Contains(body, []byte(`"messages"`)) || !bytes.Contains(body, []byte(`"system"`)) {
return body
}
dec := json.NewDecoder(bytes.NewReader(body))
dec.UseNumber()
var m map[string]any
if dec.Decode(&m) != nil || m == nil {
return body
}
msgs, _ := m["messages"].([]any)
kept := msgs[:0:0]
var folded []any // each system turn's content, a string or blocks
for _, mm := range msgs {
im, _ := mm.(map[string]any)
if im == nil || im["role"] != "system" {
kept = append(kept, mm)
continue
}
switch c := im["content"].(type) {
case string:
folded = append(folded, c)
case []any:
folded = append(folded, c...)
}
}
if len(kept) == len(msgs) {
return body
}
m["messages"] = kept
// the system prompt: what was there, then each turn's text and blocks
var blocks []any
switch s := m["system"].(type) {
case string:
if s != "" {
blocks = append(blocks, map[string]any{"type": "text", "text": s})
}
case []any:
blocks = append(blocks, s...)
}
for _, f := range folded {
if s, ok := f.(string); ok {
if s != "" {
blocks = append(blocks, map[string]any{"type": "text", "text": s})
}
continue
}
blocks = append(blocks, f)
}
if len(blocks) > 0 {
m["system"] = blocks
} else {
delete(m, "system")
}
out, err := marshalPlain(m)
if err != nil {
return body
}
return out
}
+243
View File
@@ -0,0 +1,243 @@
package gateway
import (
"bytes"
"context"
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #3392's second half, as kongkongklng reported it after merging the
// custom-tool fix: through xAI's own key the first Codex turn went
// through and every one after it 400ed "Could not decode the compaction
// blob. Ensure it is unmodified from the compact response." (Codex hands
// the sealed reasoning item back with "content": null), and every Claude
// call 400ed "Invalid request content: Invalid message role." (Claude
// Code's system turn mid-conversation).
const (
xaiCompactionRefusal = `{"code":"invalid-argument","error":"Could not decode the compaction blob. Ensure it is unmodified from the compact response."}`
xaiUnknownCustom = `Failed to deserialize the JSON body into the target type: tools[6].type: unknown variant ` + "`custom`" + `, expected one of ` + "`function`" + `, ` + "`web_search`" + `, ` + "`shell`" + `, ` + "`tool_search`"
xaiInvalidRole = `{"type":"error","error":{"type":"invalid_request_error","message":"Invalid request content: Invalid message role."}}`
)
// The reporter's second turn: Codex's replay of the sealed reasoning item
// its first turn got, null content and all, beside the answer and the next
// ask (the texts cut short).
const xaiSecondTurn = `{"model":"fake/grok-4.7","stream":true,"store":false,"include":["reasoning.encrypted_content"],
"tools":[{"type":"function","name":"exec_command","parameters":{"type":"object"}},
{"type":"custom","name":"apply_patch","description":"Apply a patch.","format":{"type":"text"}}],
"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
{"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"The user said hi."}],"content":null,"encrypted_content":"gAABblob"},
{"type":"message","role":"assistant","id":"msg_1","content":[{"type":"output_text","text":"Hello!"}]},
{"type":"message","role":"user","content":[{"type":"input_text","text":"and now?"}]}]}`
// atXAI points magpie's fake provider at api.x.ai and its client at up,
// as xAI's own API is gated on the host.
func atXAI(t *testing.T, up *httptest.Server) *Server {
t.Helper()
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"grok-4.7"}, Responses: "http://api.x.ai/v1"}); err != nil {
t.Fatal(err)
}
s := New()
addr := strings.TrimPrefix(up.URL, "http://")
s.client = &http.Client{Transport: &http.Transport{DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, addr)
}}}
return s
}
// xaiResponsesUpstream refuses what xAI's API does (#3392): a custom tool
// with its 422 serde wording, a reasoning item's null content with the
// compaction-blob 400.
func xaiResponsesUpstream(f *fake) {
f.refuse = func(body []byte) (int, string) {
var q struct {
Tools []struct {
Type string `json:"type"`
} `json:"tools"`
Input []struct {
Type string `json:"type"`
Content json.RawMessage `json:"content"`
} `json:"input"`
}
json.Unmarshal(body, &q)
for _, t := range q.Tools {
if t.Type == "custom" {
return 422, xaiUnknownCustom
}
}
for _, it := range q.Input {
if it.Type == "reasoning" && bytes.Equal(bytes.TrimSpace(it.Content), []byte("null")) {
return 400, xaiCompactionRefusal
}
}
return 0, ""
}
}
// Codex's turns to xAI's own key, the reporter's session: the first is
// 422ed over its custom tool, translated on the same API; the second, the
// sealed reasoning item handed back with "content": null beside its
// encrypted_content, was 400ed "Could not decode the compaction blob" —
// the null goes, on the translated path the custom tool had put it on and
// on the relay alike.
func TestXAITakesCodexsSealedReasoningBack(t *testing.T) {
for _, turn := range []string{xaiSecondTurn,
// the relay alone: no custom tool to turn away, the replayed item
// shaped where the request is passed through as sent
strings.Replace(xaiSecondTurn, `,{"type":"custom","name":"apply_patch","description":"Apply a patch.","format":{"type":"text"}}`, "", 1)} {
f := &fake{t: t, reply: sse(
`data: {"type":"response.created","response":{"id":"resp_1","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok"}]}}`,
`data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","output":[],"usage":{"input_tokens":9,"output_tokens":1}}}`)}
xaiResponsesUpstream(f)
up := setup(t, provider.Responses, f)
s := atXAI(t, up)
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(turn)))
if rec.Code != 200 {
t.Fatalf("%d %s\nxAI got %s", rec.Code, rec.Body, f.got)
}
if bytes.Contains(f.got, []byte(`"content":null`)) {
t.Errorf("xAI got the null content: %s", f.got)
}
if !bytes.Contains(f.got, []byte(`"encrypted_content":"gAABblob"`)) || !bytes.Contains(f.got, []byte(`"rs_1"`)) {
t.Errorf("sealed reasoning not replayed as it came: %s", f.got)
}
}
}
// The custom tool's 422 is learned from (custom_tool.go) and the turn
// answered translated, so the second turn above reaches xAI through the
// translation: both halves of the reporter's session in one.
func TestXAILearnsCustomToolsThenTakesTheReplay(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"type":"response.created","response":{"id":"resp_1","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"ok"}]}}`,
`data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","output":[],"usage":{"input_tokens":9,"output_tokens":1}}}`)}
xaiResponsesUpstream(f)
up := setup(t, provider.Responses, f)
s := atXAI(t, up)
first := `{"model":"fake/grok-4.7","stream":true,"store":false,"include":["reasoning.encrypted_content"],
"tools":[{"type":"function","name":"exec_command","parameters":{"type":"object"}},
{"type":"custom","name":"apply_patch","description":"Apply a patch.","format":{"type":"text"}}],
"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}]}`
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(first)))
if rec.Code != 200 {
t.Fatalf("first: %d %s\nxAI got %s", rec.Code, rec.Body, f.got)
}
if bytes.Contains(f.got, []byte(`"type":"custom"`)) || !bytes.Contains(f.got, []byte(`"name":"apply_patch"`)) {
t.Errorf("first answered translated, but xAI got %s", f.got)
}
rec = httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(xaiSecondTurn)))
if rec.Code != 200 {
t.Fatalf("second: %d %s\nxAI got %s", rec.Code, rec.Body, f.got)
}
if bytes.Contains(f.got, []byte(`"content":null`)) || !bytes.Contains(f.got, []byte(`"encrypted_content":"gAABblob"`)) {
t.Errorf("second turn: xAI got %s", f.got)
}
}
// Claude Code's messages carry a system turn mid-conversation (Anthropic
// made them GA); xAI's Messages answers the role "Invalid message role",
// so each folds into the top-level system, the only place it takes the
// role (#3392: every claude call 400ed).
func TestXAIFoldsClaudesSystemTurn(t *testing.T) {
f := &fake{t: t, reply: sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"grok-4.7","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"ok"}}`,
`event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,
`event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)}
f.refuse = func(body []byte) (int, string) {
var q struct {
Messages []struct {
Role string `json:"role"`
} `json:"messages"`
}
json.Unmarshal(body, &q)
for _, m := range q.Messages {
if m.Role == "system" {
return 400, xaiInvalidRole
}
}
return 0, ""
}
up := setup(t, provider.Anthropic, f)
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"grok-4.7"}, Anthropic: "http://api.x.ai"}); err != nil {
t.Fatal(err)
}
s := New()
addr := strings.TrimPrefix(up.URL, "http://")
s.client = &http.Client{Transport: &http.Transport{DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, addr)
}}}
// the reporter's shape: the billing system prompt top-level, the
// environment one a system turn after the user's hello (texts cut)
rec := httptest.NewRecorder()
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"fake/grok-4.7","stream":true,"max_tokens":128000,
"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.292.c59;"},{"type":"text","text":"You are Claude Code, Anthropic's official CLI for Claude."}],
"messages":[{"role":"user","content":[{"type":"text","text":"hello"}]},
{"role":"system","content":[{"type":"text","text":"# Environment\nYou are powered by the model xai/grok-4.7.","cache_control":{"type":"ephemeral"}}]}],
"thinking":{"type":"adaptive","display":"updates"}}`)))
if rec.Code != 200 {
t.Fatalf("%d %s\nxAI got %s", rec.Code, rec.Body, f.got)
}
if strings.Contains(string(f.got), `"role":"system"`) {
t.Errorf("system turn left in messages: %s", f.got)
}
for _, said := range []string{"# Environment", "You are Claude Code", `"role":"user"`, `"cache_control":{"type":"ephemeral"}`} {
if !strings.Contains(string(f.got), said) {
t.Errorf("xAI's system lost %q: %s", said, f.got)
}
}
}
// A body xAI takes is passed through untouched: the two shapers change
// nothing else about it, and another host's own quirks stay its own.
func TestXaiBodyLeavesWhatItTakes(t *testing.T) {
plain := `{"model":"m","input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},{"type":"reasoning","id":"rs_1","encrypted_content":"gAABblob"}],"messages":[{"role":"user","content":"hi"}],"system":"prompt"}`
if got := withoutNullReasoningContent([]byte(plain)); !bytes.Equal(got, []byte(plain)) {
t.Errorf("reasoning without content changed: %s", got)
}
if got := systemTurnsFolded([]byte(plain)); !bytes.Equal(got, []byte(plain)) {
t.Errorf("no system turn changed: %s", got)
}
// a reasoning item's null content alone goes
got := withoutNullReasoningContent([]byte(xaiSecondTurn))
if bytes.Contains(got, []byte(`"content":null`)) || !bytes.Contains(got, []byte(`"input_text"`)) {
t.Errorf("null content not gone: %s", got)
}
// a system turn with a string content folds beside a string system
got = systemTurnsFolded([]byte(`{"model":"m","max_tokens":10,"system":"prompt","messages":[{"role":"user","content":"hi"},{"role":"system","content":"mid"}]}`))
var q struct {
System []struct {
Text string `json:"text"`
} `json:"system"`
Messages []struct {
Role string `json:"role"`
} `json:"messages"`
}
if err := json.Unmarshal(got, &q); err != nil {
t.Fatal(err)
}
if len(q.System) != 2 || q.System[0].Text != "prompt" || q.System[1].Text != "mid" {
t.Errorf("system folded to %s", got)
}
if len(q.Messages) != 1 || q.Messages[0].Role != "user" {
t.Errorf("messages left %s", got)
}
// another host's body is untouched
p := provider.Provider{ID: "other", Chat: "https://api.other/v1"}
if got := xaiBody(p, provider.Responses, []byte(xaiSecondTurn)); !bytes.Equal(got, []byte(xaiSecondTurn)) {
t.Errorf("other host shaped: %s", got)
}
}