Files
magpie/internal/gateway/grok_schema.go
T
yetone 97b4174a50 gateway: a Grok model is sent a tool's ^[^\s[\]]+$ as Rust's regex reads it, not turned away as "is not a regex" (#2317, x121381)
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
2026-10-15 23:08:02 +08:00

69 lines
2.2 KiB
Go

package gateway
import (
"bytes"
"strings"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// grokToolPatterns is body with each "pattern" in its tools' schemas
// spelled so a Rust regex reads it as the ECMAScript regex JSON Schema
// says it is. xAI turns away a tool whose pattern Rust's regex syntax
// can't parse, and there a "[" inside a character class opens a nested
// class (and "&&", "--" and "~~" are set operations), where ECMAScript
// reads a plain character. A tool in a Claude Code session on Windows has
// "^[^\s[\]]+$" for a breakpoint's class_name and selector, so a Grok
// model was turned away on every turn: "Invalid request content: Schema
// validation failed: … pattern: "^[^\\s[\\]]+$" is not a "regex"" (#2317,
// grok-4.6 through a relay). Such a character is escaped instead, which is
// the same character to ECMAScript.
//
// It is Grok's whoever serves it (grokModel), the model named in the body
// as it goes upstream, or xAI's own API; every other model's tools go as
// they came.
func grokToolPatterns(p provider.Provider, to provider.Protocol, body []byte) []byte {
if p.Host() != "api.x.ai" && !grokModel(gjson.GetBytes(body, "model").String()) {
return body
}
if !bytes.Contains(body, []byte(`"pattern"`)) {
return body
}
return toolPatterns(body, ecmaClassForRust)
}
// ecmaClassForRust escapes, inside each character class of an ECMAScript
// pattern, a "[" and the second character of "&&", "--" and "~~", which a
// Rust regex would read as a nested class or a set operation. Escapes are
// kept as they are, and nothing outside a class changes.
func ecmaClassForRust(pat string) string {
var b strings.Builder
in := false
for i := 0; i < len(pat); i++ {
c := pat[i]
switch {
case c == '\\' && i+1 < len(pat):
b.WriteByte(c)
b.WriteByte(pat[i+1])
i++
continue
case !in && c == '[':
in = true
case in && c == ']':
in = false
case in && c == '[':
b.WriteString(`\[`)
continue
case in && (c == '&' || c == '-' || c == '~') && i+1 < len(pat) && pat[i+1] == c:
b.WriteByte(c)
b.WriteByte('\\')
b.WriteByte(c)
i++
continue
}
b.WriteByte(c)
}
return b.String()
}