x121381 runs grok-4.6 from a third-party API key in Claude Code (Claude for Windows). Every turn came back 400 from upstream: "Invalid request content: Schema validation failed: /properties/breakpoints/items/oneOf/1/ properties/class_name/allOf/1/pattern: "^[^\\s[\\]]+$" is not a "regex"" (and the same for selector). The pattern is valid ECMAScript, which JSON Schema's pattern is; a "[" inside a class is a plain character there. In Rust's regex syntax it opens a nested class, so the class is never closed and the pattern doesn't parse; "&&", "--" and "~~" in a class are set operations there too. grokToolPatterns (forwardOnce, beside deepseekToolPatterns) escapes those characters inside a class of every tool schema's "pattern" when the model named in the body is Grok's (grokModel: grok-*, xai/grok-*, x-ai/grok-*) or the provider is api.x.ai. The escaped character is the same character to ECMAScript, so the tool means what it meant. Patterns are spliced in place (toolPatterns, the walker deepseekToolPatterns used, now taking the fix as a function), so the rest of the body, its key order and a cache's prefix, stays byte for byte; other models' tools go as they came. Tests: TestECMAClassForRust (the reporter's pattern, POSIX-looking [[:alpha:]], &&, --, ~~, escapes and text outside a class); TestGrokToolPatterns2317 builds the schema path the error names (breakpoints.items.oneOf[1].class_name/selector.allOf[1].pattern) and checks only the two patterns change, for grok-4.6, xai/grok-4.7, x-ai/grok-4 and api.x.ai by any model name; TestGrokToolPatternsOtherModelsAsTheyCame keeps gpt/claude/deepseek/kimi bodies and a Grok body with nothing to fix byte for byte. Run: gofmt -l internal/gateway clean; go vet -tags nogui ./internal/gateway clean. NOT run: go test (the disk had 208 MB free, under the 1 GB the session needs before a test run), and nothing broken by hand yet. Before landing: go test -tags nogui -p 1 -run 'TestECMAClassForRust|TestGrokToolPatterns|TestDeepSeekToolPatterns|TestUnicodeNullEscape' ./internal/gateway/, then drop the grokToolPatterns call in forwardOnce / the in-class "[" case and watch TestGrokToolPatterns2317 go red. Not tried against xAI or the reporter's relay; that Rust regex syntax is what xAI checks with is read from the error, not from xAI's docs. Full suite: SUITE_LINE
79 lines
3.5 KiB
Go
79 lines
3.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
func TestECMAClassForRust(t *testing.T) {
|
|
for _, tc := range [][2]string{
|
|
// #2317's pattern: "[" inside a class is a character
|
|
{`^[^\s[\]]+$`, `^[^\s\[\]]+$`},
|
|
{`^[^\s\[\]]+$`, `^[^\s\[\]]+$`},
|
|
{`[[:alpha:]]`, `[\[:alpha:]]`},
|
|
{`[a&&b]`, `[a&\&b]`},
|
|
{`[+--]`, `[+-\-]`},
|
|
{`[~~]`, `[~\~]`},
|
|
// outside a class nothing changes
|
|
{`a&&b--c~~d\[x`, `a&&b--c~~d\[x`},
|
|
{`^https?://[a-z]+\.[a-z]{2,}$`, `^https?://[a-z]+\.[a-z]{2,}$`},
|
|
{`\\[a[b]`, `\\[a\[b]`},
|
|
} {
|
|
if got := ecmaClassForRust(tc[0]); got != tc[1] {
|
|
t.Errorf("%q: got %q, want %q", tc[0], got, tc[1])
|
|
}
|
|
}
|
|
}
|
|
|
|
// grok2317Tools is the tool schema #2317's error names: a breakpoint's
|
|
// class_name and selector, each with ^[^\s[\]]+$ in an allOf, in one of a
|
|
// oneOf, as an Anthropic → Chat translation sends it.
|
|
const grok2317Tools = `"tools":[{"type":"function","function":{"name":"set_breakpoints","description":"<a> & b","parameters":{"type":"object","properties":{"breakpoints":{"type":"array","items":{"oneOf":[{"type":"object","properties":{"line":{"type":"integer"}}},{"type":"object","properties":{"class_name":{"allOf":[{"type":"string"},{"pattern":"^[^\\s[\\]]+$"}]},"selector":{"allOf":[{"type":"string"},{"pattern":"^[^\\s[\\]]+$"}]}}}]}}}}}}]`
|
|
|
|
func TestGrokToolPatterns2317(t *testing.T) {
|
|
body := []byte(`{"model":"grok-4.6","messages":[{"role":"user","content":"[^\\s[\\]]"}],` + grok2317Tools + `}`)
|
|
relay := provider.Provider{ID: "gwork", Chat: "https://relay.example/v1"}
|
|
out := grokToolPatterns(relay, provider.Chat, body)
|
|
at := "tools.0.function.parameters.properties.breakpoints.items.oneOf.1.properties."
|
|
for _, f := range []string{"class_name", "selector"} {
|
|
if got := gjson.GetBytes(out, at+f+".allOf.1.pattern").String(); got != `^[^\s\[\]]+$` {
|
|
t.Errorf("%s pattern = %q", f, got)
|
|
}
|
|
}
|
|
// only the patterns change: the message and the rest stay byte for byte
|
|
want := bytes.ReplaceAll(body, []byte(`"^[^\\s[\\]]+$"`), []byte(`"^[^\\s\\[\\]]+$"`))
|
|
if !bytes.Equal(out, want) {
|
|
t.Fatalf("body changed beyond its patterns:\n%s", out)
|
|
}
|
|
// a relay's or router's name for Grok, and xAI's own API by any name
|
|
for _, model := range []string{"xai/grok-4.7", "x-ai/grok-4"} {
|
|
b := bytes.Replace(body, []byte(`"grok-4.6"`), []byte(`"`+model+`"`), 1)
|
|
if bytes.Equal(grokToolPatterns(relay, provider.Chat, b), b) {
|
|
t.Errorf("%s: pattern not fixed", model)
|
|
}
|
|
}
|
|
xai := provider.Provider{ID: "xai", Chat: "https://api.x.ai/v1"}
|
|
b := bytes.Replace(body, []byte(`"grok-4.6"`), []byte(`"my-alias"`), 1)
|
|
if bytes.Equal(grokToolPatterns(xai, provider.Chat, b), b) {
|
|
t.Error("api.x.ai: pattern not fixed")
|
|
}
|
|
}
|
|
|
|
func TestGrokToolPatternsOtherModelsAsTheyCame(t *testing.T) {
|
|
relay := provider.Provider{ID: "gwork", Chat: "https://relay.example/v1"}
|
|
for _, model := range []string{"gpt-6-astra", "claude-sonnet-5", "deepseek-v4", "kimi-k3"} {
|
|
body := []byte(`{"model":"` + model + `",` + grok2317Tools + `}`)
|
|
if out := grokToolPatterns(relay, provider.Chat, body); !bytes.Equal(out, body) {
|
|
t.Errorf("%s: body changed:\n%s", model, out)
|
|
}
|
|
}
|
|
// a Grok request whose patterns need nothing passes through as it came
|
|
body := []byte(`{"model":"grok-4.6","tools":[{"name":"x","input_schema":{"properties":{"u":{"pattern":"^https?://[a-z]+$"}}}}]}`)
|
|
if out := grokToolPatterns(relay, provider.Anthropic, body); !bytes.Equal(out, body) {
|
|
t.Errorf("unchanged body re-encoded:\n%s", out)
|
|
}
|
|
}
|