Files
magpie/internal/provider/codex_names.go
T
yetone b4a9cd8db5 codex: a tool name the ChatGPT backend refuses goes as one it takes, and comes back as the agent's
wing3556 on Discord (11-02): a compaction through a Codex account failed
400 "Codex: Invalid response.create payload: Invalid 'input[83].name':
string does not match pattern. Expected a string that matches the
pattern '^[a-zA-Z0-9_.-]+\z'." The ChatGPT backend checks every tool
name in a request, a call's in input as well as a tool's in tools, and
refuses the whole request when one fails. A conversation can hold a call
by such a name: a model another provider served wrote one with a space,
a colon or non-ASCII in it, and the agent kept it beside its "no such
tool" answer. Every request that replayed it, the compaction above all,
then failed on Codex. Nothing on the way checked: buildResponses copies a
call's name as it is, and redaction keeps names as written.

What changed:
- provider.CodexToolName: a valid name stays as it is. Otherwise each
  refused byte becomes "_", the result is cut to 55 and gets "_" plus 8
  hex of its sha256, so it is the same on every request and two names
  never meet. CodexToolNames applies it to tools (namespace children
  too), function_call/custom_tool_call items, additional_tools and
  tool_search_output items, and tool_choice. A body with no bad name goes
  byte for byte.
- It runs where every request to the ChatGPT backend passes: codexBody
  (an account's, translated or relayed) and codexUpstreamOn (Codex's
  own sign-in, /responses and /responses/compact), beside DeclareSearch.
- A tool the agent declared under such a name is called back by its own
  name: translated replies through decoderFor (now given the request's
  tools; codexNamesBack), relayed Responses replies through nsTidy's new
  back map (codexRenamedIn).
- docs/subsystems/gateway-routing.md says so beside #1270's paragraph.

Not covered: namespaces, the OpenAI API (whose pattern has no "." and a
64 limit), Azure and Copilot Responses, filed as #4875.

Run on mote at this head:
- TestCodexToolNamesTheBackendTakes and TestCodexToolNamesKeepValid
  pass. The fake backend refuses a bad name with the reporter's 400 word
  for word; the test covers Claude Code's compaction on /v1/messages with
  four bad history names (sent twice, byte-equal), Chat with a declared
  "search:web" called back by that name, Responses relayed the same, and
  Codex's own /backend-api/codex/responses.
- go test ./internal/provider ./internal/gateway (-timeout 45m): provider ok;
  gateway ok but TestCodexPoolSaysTheClientsVersion, the red already
  filed as #4807 (full package run only, another test's Codex identity
  reaches it); it passes alone at this head.
- go vet ./... (darwin) and GOOS=linux/windows CGO_ENABLED=0 go vet
  -tags nogui ./...: ok. gofmt -l: clean.

Broken by hand:
- codexBody hook off: compaction red with the reporter's 400.
- codexUpstreamOn hook off, decoder restore off, passthrough restore off
  (together): the codex, chat and responses cases each red.

Not tried: wing3556's real conversation (we don't have its bytes, and
the agent is unknown; Claude Code is likely). No real ChatGPT account
was asked with a bad name, so whether the real backend also limits
length is unknown.
2026-11-02 15:15:21 +08:00

186 lines
5.2 KiB
Go

package provider
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"regexp"
"github.com/tidwall/gjson"
)
// The ChatGPT backend checks every tool name a request carries, a call's
// in the input as much as a tool's in tools, against ^[a-zA-Z0-9_.-]+\z,
// and turns the whole request away when one fails: 400 "Invalid
// 'input[83].name': string does not match pattern" (wing3556 on Discord,
// on a compaction). A conversation can hold a call by a name no tool has:
// a model another provider served wrote one with a space, a colon or a
// word of its own script in it, and the agent kept the call in its
// history beside the "no such tool" it answered. Each request that
// replays it, a compaction's above all, failed on Codex every time. Such a
// name goes as CodexToolName makes it; a valid one goes as it is, so a
// conversation that worked is sent byte for byte as before.
// codexNameOK is a name the ChatGPT backend takes.
var codexNameOK = regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
// CodexToolName is name as the ChatGPT backend takes it: as it is when
// it is valid, else each character the backend refuses as "_", cut to
// 55, and a hash of the whole after it (as FlatName cuts a long name), so
// a name is made the same on every request and two names that differ
// never come out alike. An agent's declared tool so renamed is called
// back by its own name (CodexToolNamesBack).
func CodexToolName(name string) string {
if codexNameOK.MatchString(name) {
return name
}
b := []byte(name)
out := make([]byte, 0, len(b)+9)
for _, c := range b {
if c == '_' || c == '.' || c == '-' || c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' {
out = append(out, c)
} else {
out = append(out, '_')
}
}
if len(out) > 55 {
out = out[:55]
}
sum := sha256.Sum256(b)
return string(out) + "_" + hex.EncodeToString(sum[:4])
}
// CodexToolNamesBack maps each name of declared that CodexToolName
// changes back to the name the agent declared; nil when none changes.
func CodexToolNamesBack(declared []string) map[string]string {
var back map[string]string
for _, n := range declared {
if c := CodexToolName(n); c != n {
if back == nil {
back = map[string]string{}
}
back[c] = n
}
}
return back
}
// CodexToolNames is body, a Responses request for the ChatGPT backend,
// with every tool name in it as CodexToolName makes it, or body byte for
// byte when each one is valid already.
func CodexToolNames(body []byte) []byte {
if !codexNamesBad(body) {
return body
}
dec := json.NewDecoder(bytes.NewReader(body))
dec.UseNumber()
var m map[string]any
if dec.Decode(&m) != nil || m == nil || !codexToolNames(m) {
return body
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if enc.Encode(m) != nil {
return body
}
return bytes.TrimSuffix(buf.Bytes(), []byte("\n"))
}
// codexNamesBad reports whether body names a tool the backend refuses,
// read where codexToolNames reads it, without decoding the conversation.
func codexNamesBad(body []byte) bool {
bad := false
name := func(v gjson.Result) {
if n := v.Get("name"); n.Type == gjson.String && !codexNameOK.MatchString(n.String()) {
bad = true
}
}
var tools func(ts gjson.Result)
tools = func(ts gjson.Result) {
ts.ForEach(func(_, t gjson.Result) bool {
switch t.Get("type").String() {
case "function", "custom":
name(t)
case "namespace":
tools(t.Get("tools"))
}
return !bad
})
}
top := gjson.GetManyBytes(body, "tools", "input", "tool_choice")
tools(top[0])
top[1].ForEach(func(_, it gjson.Result) bool {
switch it.Get("type").String() {
case "function_call", "custom_tool_call":
name(it)
case "additional_tools", "tool_search_output":
tools(it.Get("tools"))
}
return !bad
})
if tc := top[2]; tc.IsObject() {
switch tc.Get("type").String() {
case "function", "custom":
name(tc)
case "allowed_tools":
tools(tc.Get("tools"))
}
}
return bad
}
// codexToolNames makes every tool name in m, a Responses request, one the
// backend takes: a function's or a custom tool's in tools (a namespace's
// own among them), in an additional_tools item or a tool search's output,
// a call's in the input, and the one tool_choice names. It says whether
// it changed one.
func codexToolNames(m map[string]any) bool {
changed := false
fix := func(o map[string]any) {
if n, ok := o["name"].(string); ok {
if c := CodexToolName(n); c != n {
o["name"] = c
changed = true
}
}
}
var tools func(ts []any)
tools = func(ts []any) {
for _, t := range ts {
tm, _ := t.(map[string]any)
switch tm["type"] {
case "function", "custom":
fix(tm)
case "namespace":
nested, _ := tm["tools"].([]any)
tools(nested)
}
}
}
ts, _ := m["tools"].([]any)
tools(ts)
input, _ := m["input"].([]any)
for _, raw := range input {
it, _ := raw.(map[string]any)
switch it["type"] {
case "function_call", "custom_tool_call":
fix(it)
case "additional_tools", "tool_search_output":
nested, _ := it["tools"].([]any)
tools(nested)
}
}
if tc, _ := m["tool_choice"].(map[string]any); tc != nil {
switch tc["type"] {
case "function", "custom":
fix(tc)
case "allowed_tools":
nested, _ := tc["tools"].([]any)
tools(nested)
}
}
return changed
}