Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions backend/internal/service/openai_responses_tool_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
10 changes: 10 additions & 0 deletions backend/internal/service/openai_responses_tool_schema_scan_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -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))
Expand Down
Loading