Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: Claude Code is given a schema without its root $schema (#3036)

yetonef5149a53321e
diff --git a/internal/gateway/claude_cli_schema_test.go b/internal/gateway/claude_cli_schema_test.go
new file mode 100644
index 000000000..b6f0a2d96
--- /dev/null
+++ b/internal/gateway/claude_cli_schema_test.go
@@ -0,0 +1,169 @@
+package gateway
+
+import (
+ "encoding/json"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "reflect"
+ "runtime"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+ "github.com/yetone/magpie/internal/testenv"
+)
+
+// codexTitleSchema is a Codex title request's schema as #3036's reporter
+// had it reach Claude Code: zod's toJSONSchema, its draft 2020-12 named
+// at the root.
+const codexTitleSchema = `{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"title":{"type":"string"}},"required":["title"],"additionalProperties":false}`
+
+// fakeClaudeAjv stands in for Claude Code checking --json-schema as its
+// Ajv does: a schema naming another draft than 07 at its root, or one
+// with a keyword only a later draft has, ends it before it reads its
+// input, with Claude Code 2.1.307's own words. Each run's arguments are
+// kept in dir.
+func fakeClaudeAjv(t *testing.T) string {
+ t.Helper()
+ if runtime.GOOS == "windows" {
+ t.Skip("a shell script stands in for Claude Code")
+ }
+ dir := t.TempDir()
+ script := `#!/bin/sh
+case "$1" in auth) exit 1;; esac
+if [ "$2" = "/usage" ]; then
+ echo '{"type":"result","is_error":false,"result":"Current session: 10% used"}'
+ exit 0
+fi
+for a in "$@"; do echo "$a"; done > ` + dir + `/args.$$
+for a in "$@"; do
+ case "$a" in
+ *'"$schema":"https://json-schema.org/draft/2020-12/schema"'*)
+ echo 'Error: --json-schema is not a valid JSON Schema: no schema with key or ref "https://json-schema.org/draft/2020-12/schema"' >&2
+ exit 1;;
+ *'"prefixItems"'*)
+ echo 'Error: --json-schema is not a valid JSON Schema: strict mode: unknown keyword: "prefixItems"' >&2
+ exit 1;;
+ esac
+done
+while read -r line; do
+ echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-haiku-5-5","usage":{"input_tokens":1}}}}'
+ echo '{"type":"stream_event","event":{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"t1","name":"StructuredOutput","input":{}}}}'
+ echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"title\": \"Fix login bug\"}"}}}'
+ echo '{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","is_error":false,"content":"Structured output provided successfully"}]}}'
+ echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":5}}}'
+ echo '{"type":"stream_event","event":{"type":"message_stop"}}'
+ echo '{"type":"result","subtype":"success","is_error":false,"result":"","structured_output":{"title":"Fix login bug"}}'
+done
+`
+ testenv.Program(t, filepath.Join(dir, "claude"), script)
+ t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
+ return dir
+}
+
+// cliSchemas are the --json-schema arguments of the runs fakeClaudeAjv
+// kept in dir.
+func cliSchemas(t *testing.T, dir string) []string {
+ t.Helper()
+ files, _ := filepath.Glob(filepath.Join(dir, "args.*"))
+ var out []string
+ for _, f := range files {
+ b, _ := os.ReadFile(f)
+ args := strings.Split(strings.TrimSpace(string(b)), "\n")
+ if i := slices.Index(args, "--json-schema"); i >= 0 && i+1 < len(args) {
+ out = append(out, args[i+1])
+ }
+ }
+ return out
+}
+
+// A Codex title request whose schema names draft 2020-12 is answered
+// through a Claude subscription (#3036): Claude Code is given the schema
+// without its root $schema, which its Ajv can't look up, and the rest of
+// it as it came.
+func TestClaudeCodeTakesCodexTitleSchema(t *testing.T) {
+ dir := fakeClaudeAjv(t)
+ s := New()
+ t.Cleanup(s.subscription.abortAll)
+ p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}}
+ body := `{"model":"claude-haiku-5-5","stream":true,"reasoning":{"effort":"low"},"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"Generate a concise title. User prompt:\nfix the login bug"}]}],` +
+ `"text":{"format":{"type":"json_schema","name":"codex_output_schema","strict":true,"schema":` + codexTitleSchema + `}}}`
+ rec := httptest.NewRecorder()
+ var u Usage
+ if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)), provider.Responses, p, "claude-haiku-5-5", []byte(body), &u); code != 200 {
+ t.Fatalf("%d %s", code, msg)
+ }
+ if text, _ := outputText(t, rec.Body.String()); text != `{"title":"Fix login bug"}` {
+ t.Fatalf("answer %q", text)
+ }
+ schemas := cliSchemas(t, dir)
+ if len(schemas) != 1 {
+ t.Fatalf("--json-schema of runs: %q", schemas)
+ }
+ var got, want map[string]any
+ json.Unmarshal([]byte(schemas[0]), &got)
+ json.Unmarshal([]byte(codexTitleSchema), &want)
+ delete(want, "$schema")
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("--json-schema %s, want %v", schemas[0], want)
+ }
+}
+
+// cliSchema takes the root $schema out and nothing else: $id, $defs and a
+// $schema below the root pass Claude Code's Ajv as they are, and a schema
+// without a root $schema goes byte for byte.
+func TestCLISchemaKeepsTheRest(t *testing.T) {
+ in := `{"$schema":"https://json-schema.org/draft/2020-12/schema","$id":"https://example.com/title.json","type":"object",` +
+ `"properties":{"title":{"$ref":"#/$defs/t"},"note":{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"string"}},"$defs":{"t":{"type":"string"}}}`
+ var got, want map[string]any
+ json.Unmarshal(cliSchema(json.RawMessage(in)), &got)
+ json.Unmarshal([]byte(in), &want)
+ delete(want, "$schema")
+ if !reflect.DeepEqual(got, want) {
+ t.Fatalf("cliSchema: %v", got)
+ }
+ for _, plain := range []string{`{"type":"object","properties":{"title":{"type":"string"}}}`, `not json`, `true`} {
+ if out := string(cliSchema(json.RawMessage(plain))); out != plain {
+ t.Fatalf("%s became %s", plain, out)
+ }
+ }
+}
+
+// A schema Claude Code's Ajv won't compile ends it the same way every
+// time, on every account: the request's 400, said in Claude Code's words,
+// from one run — not a 502 asked again after a moment and of the next
+// account, three runs and more for the same refusal (#3036) — and no
+// account rests for it.
+func TestClaudeSchemaRefusedIsAskedOnce(t *testing.T) {
+ for _, stream := range []bool{false, true} {
+ t.Run(map[bool]string{false: "whole", true: "stream"}[stream], func(t *testing.T) { schemaRefusedOnce(t, stream) })
+ }
+}
+
+func schemaRefusedOnce(t *testing.T, stream bool) {
+ fresh(t)
+ claudeMadeFirst(t, false) // two Claude accounts
+ dir := fakeClaudeAjv(t)
+ s := New()
+ t.Cleanup(s.subscription.abortAll)
+ streamed := ""
+ if stream {
+ streamed = `"stream":true,`
+ }
+ body := `{"model":"claude/claude-sonnet-5","max_tokens":100,` + streamed + `"messages":[{"role":"user","content":"pair?"}],` +
+ `"output_config":{"format":{"type":"json_schema","schema":{"type":"array","prefixItems":[{"type":"string"},{"type":"number"}]}}}}`
+ r := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
+ r.Header.Set("x-api-key", Token)
+ r.RemoteAddr = "127.0.0.1:12345"
+ w := httptest.NewRecorder()
+ lanGuard(s.Handler()).ServeHTTP(w, r)
+ if w.Code != 400 || !strings.Contains(w.Body.String(), `--json-schema is not a valid JSON Schema: strict mode: unknown keyword`) {
+ t.Fatalf("%d %s", w.Code, w.Body)
+ }
+ if runs := cliSchemas(t, dir); len(runs) != 1 {
+ t.Fatalf("Claude Code ran %d times: %q", len(runs), runs)
+ }
+ noneRested(t, "claude")
+}
diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go
index 656d9a862..4a6dedde2 100644
--- a/internal/gateway/claude_subscription.go
+++ b/internal/gateway/claude_subscription.go
@@ -595,7 +595,8 @@ const claudeMCPPath = "/_magpie/claude-mcp/"
func (b *subscriptionBridge) start(ctx context.Context, req *Request, model, configDir, owner string, from *savedSession) (*subscriptionRun, <-chan Event, error) {
run, events, err := b.startRun(ctx, req, model, configDir, owner, from)
var full *claudeFull
- if err != nil && from != nil && !errors.As(err, &full) && ctx.Err() == nil {
+ var refused *cliSchemaRefused
+ if err != nil && from != nil && !errors.As(err, &full) && !errors.As(err, &refused) && ctx.Err() == nil {
log.Printf("claude: Claude Code could not go on from the conversation's saved session (%v); a new one is told the whole conversation", err)
return b.startRun(ctx, req, model, configDir, owner, nil)
}
@@ -676,7 +677,7 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model,
args = append(args, "--thinking", "disabled")
}
if len(req.Format.schema()) > 0 {
- args = append(args, "--json-schema", string(req.Format.schema()))
+ args = append(args, "--json-schema", string(cliSchema(req.Format.schema())))
}
work, err := claudeWorkDir()
var sessions []string
@@ -3669,6 +3670,9 @@ func (r *subscriptionRun) launch() error {
if room != nil {
room(left)
}
+ if err := r.schemaEnded(); err != nil {
+ r.emit(Event{Kind: KError, Text: err.Error(), Status: http.StatusBadRequest})
+ }
r.finish()
}()
return nil
@@ -3723,12 +3727,74 @@ func (r *subscriptionRun) endedBefore(what string) error {
} else if r.exit != nil {
why += " (" + r.exit.Error() + ")"
}
- if said := strings.TrimSpace(r.stderr.String()); said != "" {
+ said := strings.TrimSpace(r.stderr.String())
+ if at := cliSchemaWords.FindStringIndex(said); at != nil {
+ // its words from the flag on, which a long list of Ajv's
+ // errors after them would push out of the tail
+ said = said[at[0]:]
+ if len(said) > 300 {
+ said = strings.ToValidUTF8(said[:300], "") + "…"
+ }
+ return &cliSchemaRefused{why + ": " + said}
+ }
+ if said != "" {
why += ": " + clipTail(said, 300)
}
return errors.New(why)
}
+// schemaEnded is the error of a run whose Claude Code took its input and
+// then ended over its --json-schema, nil for any other end: it checks the
+// schema while it starts, and its input may be taken before it gets to
+// it, so the turn told it heard no answer, and was "ended without an
+// answer", a 502 asked again.
+func (r *subscriptionRun) schemaEnded() error {
+ r.mu.Lock()
+ killed, said := r.killed, r.stderr.String()
+ r.mu.Unlock()
+ if killed || !cliSchemaWords.MatchString(said) {
+ return nil
+ }
+ return r.endedBefore("answered")
+}
+
+// cliSchema is a schema as Claude Code's --json-schema takes it: without
+// its root $schema. Claude Code checks the schema with Ajv's default
+// class, which knows draft-07's meta-schema alone, and exits "no schema
+// with key or ref" over any other a schema names, as Codex's titles name
+// draft 2020-12 (zod's toJSONSchema), before it reads its input (#3036).
+// Without it the schema is read as draft-07. The rest is kept as written:
+// $id, $defs and a $schema below the root pass Ajv as they are.
+func cliSchema(raw json.RawMessage) json.RawMessage {
+ var root map[string]json.RawMessage
+ if json.Unmarshal(raw, &root) != nil {
+ return raw
+ }
+ if _, ok := root["$schema"]; !ok {
+ return raw
+ }
+ delete(root, "$schema")
+ out, err := json.Marshal(root)
+ if err != nil {
+ return raw
+ }
+ return out
+}
+
+// cliSchemaWords are how Claude Code exits over the --json-schema it was
+// given, before it reads its input: not JSON, not an object, or a schema
+// its Ajv won't compile ("strict mode: unknown keyword" for one only a
+// later draft has).
+var cliSchemaWords = regexp.MustCompile(`--json-schema (is not a valid JSON Schema|is not valid JSON|must be a JSON object)`)
+
+// cliSchemaRefused is a run's Claude Code ending over the request's schema:
+// it ends the same for the same schema on every account and every try,
+// so it is the request's 400, neither asked again nor of the next
+// account, and no account rests for it (#3036: asked three times).
+type cliSchemaRefused struct{ msg string }
+
+func (e *cliSchemaRefused) Error() string { return e.msg }
+
// clipTail is s's last n bytes at most, cut at a rune, "…" before them
// where some went.
func clipTail(s string, n int) string {
@@ -4218,8 +4284,11 @@ func (s *Server) serveSubscription(w http.ResponseWriter, r *http.Request, from
// the account: a 503 passes it to the next (#2956)
code := 502
var full *claudeFull
+ var refused *cliSchemaRefused
if errors.As(err, &full) {
code = 503
+ } else if errors.As(err, &refused) {
+ code = 400
}
return writeError(w, from, code, name+": "+err.Error()), err.Error()
}