diff --git a/backend/internal/service/openai_responses_tool_schema.go b/backend/internal/service/openai_responses_tool_schema.go index 440a1b0a9de2..55624ab9129f 100644 --- a/backend/internal/service/openai_responses_tool_schema.go +++ b/backend/internal/service/openai_responses_tool_schema.go @@ -102,10 +102,9 @@ var ( // Short needles on purpose: bytes.Contains switches to a much slower // algorithm for longer needles (a quoted 7-byte key costs ~12x a 5-byte one) // and these run on every request. - openAIResponsesOneOfHint = []byte("oneOf") - openAIResponsesAnyOfHint = []byte("anyOf") - openAIResponsesRegexGroupBytes = []byte("(?") - openAIResponsesJSONUnicodeEscape = []byte(`\u`) + openAIResponsesOneOfHint = []byte("oneOf") + openAIResponsesAnyOfHint = []byte("anyOf") + openAIResponsesRegexGroupBytes = []byte("(?") // Any escaped spelling of an ASCII letter is \u00XX, so this also stands in // for a union keyword written that way. openAIResponsesUnicodeLetterEscape = []byte(`\u00`) @@ -125,7 +124,20 @@ func openAIResponsesBodyMayNeedToolParameterRepair(body []byte) bool { return bytes.Contains(body, openAIResponsesNullLiteral) || bytes.Contains(body, openAIResponsesOneOfHint) || bytes.Contains(body, openAIResponsesAnyOfHint) || - bytes.Contains(body, openAIResponsesUnicodeLetterEscape) + openAIResponsesContainsUnicode00Prefix(body) +} + +// A history full of escaped quotes and newlines has many backslashes to reject. +// Skip to the first possible zero before searching for the complete prefix. +func openAIResponsesContainsUnicode00Prefix(body []byte) bool { + slash := bytes.IndexByte(body, '\\') + if slash < 0 { + return false + } + body = body[slash:] + // In "\u00" the first zero is two bytes after the start of the match. + zero := bytes.IndexByte(body, '0') + return zero >= 0 && bytes.Contains(body[max(0, zero-2):], openAIResponsesUnicodeLetterEscape) } // openAIResponsesBodyMayContainUnsupportedRegex is a necessary condition for @@ -148,11 +160,8 @@ func openAIResponsesBodyMayContainUnsupportedRegex(body []byte) bool { if bytes.Contains(body, openAIResponsesRegexGroupBytes) { return true } - if !bytes.Contains(body, openAIResponsesJSONUnicodeEscape) { - return false - } // 任何 \u00XX 转义都可能写成上述三种形态里的括号、问号或反斜杠。 - return bytes.Contains(body, openAIResponsesUnicodeLetterEscape) + return openAIResponsesContainsUnicode00Prefix(body) } // openAIResponsesJSONPatternIsUnsupported reports whether a JSON string span diff --git a/backend/internal/service/openai_responses_tool_schema_benchmark_test.go b/backend/internal/service/openai_responses_tool_schema_benchmark_test.go index 9d775d22c2d8..ad82a1647c2a 100644 --- a/backend/internal/service/openai_responses_tool_schema_benchmark_test.go +++ b/backend/internal/service/openai_responses_tool_schema_benchmark_test.go @@ -127,3 +127,21 @@ func BenchmarkSanitizeOpenAIResponsesToolSchemasForPlatformClean1MiB(b *testing. _, _, _ = sanitizeOpenAIResponsesToolSchemasForPlatform(body, PlatformOpenAI) } } + +func BenchmarkOpenAIResponsesToolSchemasEscapedHistory(b *testing.B) { + for _, tc := range []struct{ name, line string }{ + {"code", `func example() string { return \"example\" }\n`}, + {"code_with_numbers", `if a[0] == 10 { log(\"ok\") }\n`}, + {"unicode_escapes", `\u6d4b\u8bd5\u6570\u636e\n`}, + } { + b.Run(tc.name, func(b *testing.B) { + history := strings.Repeat(tc.line, (1<<20)/len(tc.line)) + body := []byte(`{"tools":[{"type":"function","name":"sample","parameters":{"type":"object"}}],"input":"` + history + `"}`) + b.SetBytes(int64(len(body))) + b.ReportAllocs() + for b.Loop() { + _, _, _ = sanitizeOpenAIResponsesToolSchemasForPlatform(body, PlatformOpenAI) + } + }) + } +} diff --git a/backend/internal/service/openai_responses_tool_schema_scan_test.go b/backend/internal/service/openai_responses_tool_schema_scan_test.go index f98a80b4d7b0..708281814bc1 100644 --- a/backend/internal/service/openai_responses_tool_schema_scan_test.go +++ b/backend/internal/service/openai_responses_tool_schema_scan_test.go @@ -1,6 +1,7 @@ package service import ( + "bytes" "encoding/json" "fmt" "strings" @@ -320,6 +321,15 @@ func TestOpenAIResponsesToolSchemaGatesAreNecessary(t *testing.T) { } } +func FuzzOpenAIResponsesContainsUnicode00Prefix(f *testing.F) { + for _, seed := range []string{"", `0\`, `\u0`, `\u00`, `0\n0\u00`, `\\u00ff`, `\u1234 00`} { + f.Add([]byte(seed)) + } + f.Fuzz(func(t *testing.T, body []byte) { + require.Equal(t, bytes.Contains(body, []byte(`\u00`)), openAIResponsesContainsUnicode00Prefix(body)) + }) +} + func FuzzOpenAIResponsesToolSchemaGatesAreNecessary(f *testing.F) { for _, seed := range openAIResponsesToolSchemaGateCorpus() { f.Add([]byte(seed))