ac4af582301a4e8a29245dbba767eba31334ddf9
1
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
f5149a5332 |
gateway: Claude Code is given a schema without its root $schema (#3036)
Codex's title requests carry zod's draft 2020-12 schema, with "$schema": "https://json-schema.org/draft/2020-12/schema" at the root. magpie passed it to `claude --json-schema` as it came. Claude Code exited with "--json-schema is not a valid JSON Schema: no schema with key or ref ...", and the client got a 502 that was asked three times. What Claude Code rejects. 2.1.307 checks --json-schema with Ajv 8's default class, which is draft-07: new Ajv({allErrors, validateFormats:false, multipleOfPrecision:6}), then validateSchema and compile, in strict mode. I ran Ajv 8.20.0 with those options: - A root $schema for 2020-12, 2019-09 or draft-04 is refused (no schema with key or ref). - Keywords only a later draft has (prefixItems, unevaluatedProperties, $anchor, $dynamicAnchor, dependentRequired) are refused (strict mode: unknown keyword). - A draft-07 $schema, $id, $defs, definitions, $comment, and a nested $schema or $id all pass. The real claude 2.1.307 agrees. I ran it in a sandbox HOME, with a fake `security` on PATH so the Keychain was never touched. The 2020-12 schema exits 1 with the reporter's exact words. The same schema with $id, and one without $schema, both get past the check. The fix. cliSchema drops only the root $schema and keeps everything else as it came. A schema with no root $schema goes byte for byte. Later-draft keywords are not rewritten. A schema that uses them is refused, and is now answered as below. No more retries. A schema Claude Code refuses is refused the same way on every try and every account. When its stderr says "--json-schema is not a valid JSON Schema" (or "is not valid JSON" / "must be a JSON object"), the run now ends in a 400 with Claude Code's words from the flag on. It is not retried, the saved-session restart is skipped, and no account rests. The 400 holds both when Claude Code exits before it takes its input (cliSchemaRefused from endedBefore) and when it exits after it took the input (schemaEnded, emitted as a 400 KError before the segment closes). Before, the second case was an empty 200 for a whole reply, or a 502 "ended without an answer" for a stream. Any other exit is handled as before. Siblings, left as they are: - anthropic.go's output_config.format goes to the Anthropic API, which takes a 2020-12 $schema; Claude Code's own tool schemas in testdata/context/claude-code.json carry one. - codeassist.go's responseJsonSchema (Gemini, Antigravity) is not passed through plainSchema. Not tried against the real API; no report of it refusing $schema. - format.go's chat()/responses() go to OpenAI unchanged. - MCP tool input schemas go to Claude Code's MCP, not this check. - No other CLI is given --json-schema. Tests (internal/gateway/claude_cli_schema_test.go). A fake claude does what Ajv does: it fails on a 2020-12 root $schema or on prefixItems, in 2.1.307's words. - TestClaudeCodeTakesCodexTitleSchema sends the reporter's title schema through a Responses request. It checks for a 200 with the title, and that the schema reached the CLI minus only its $schema. - TestCLISchemaKeepsTheRest checks that $id, $defs and a nested $schema are kept, and that a schema without $schema, non-JSON and `true` pass byte for byte. - TestClaudeSchemaRefusedIsAskedOnce/{whole,stream} sends a prefixItems schema with two Claude accounts. It checks for a 400 in Claude Code's words, one run, and no account rested. Broken by hand: - cliSchema returning raw: the first two tests go red. - No 400 for a refusal: both subtests go red (an empty 200 and a 502). - No late-exit emit only: both subtests go red. Record for this push: 13 commits on origin/main |