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.
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package gateway
|
|
|
|
import (
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A ChatGPT account's backend refuses a tool name outside
|
|
// ^[a-zA-Z0-9_.-]+\z, a call's in the history as much as a declared
|
|
// tool's, and the account's send gives it another (provider's
|
|
// CodexToolNames, wing3556 on Discord). A call the model makes to a
|
|
// declared tool so renamed comes back to the agent by the name it
|
|
// declared: from a translated reply's events (decoderFor), and from a
|
|
// Responses reply relayed as it came (nsTidy's back).
|
|
|
|
// codexNamesBack maps each declared tool's name the ChatGPT backend is
|
|
// given in its place back to the agent's; nil when none is renamed.
|
|
func codexNamesBack(tools []Tool) map[string]string {
|
|
if len(tools) == 0 {
|
|
return nil
|
|
}
|
|
names := make([]string, 0, len(tools))
|
|
for _, t := range tools {
|
|
names = append(names, t.Name)
|
|
}
|
|
return provider.CodexToolNamesBack(names)
|
|
}
|
|
|
|
// codexRenamedIn is codexNamesBack for body, a Responses request: the
|
|
// functions and custom tools it declares in tools or in an
|
|
// additional_tools item.
|
|
func codexRenamedIn(body []byte) map[string]string {
|
|
var names []string
|
|
add := func(ts gjson.Result) {
|
|
ts.ForEach(func(_, t gjson.Result) bool {
|
|
if k := t.Get("type").String(); k == "function" || k == "custom" {
|
|
if n := t.Get("name"); n.Type == gjson.String {
|
|
names = append(names, n.String())
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
top := gjson.GetManyBytes(body, "tools", "input")
|
|
add(top[0])
|
|
top[1].ForEach(func(_, it gjson.Result) bool {
|
|
if it.Get("type").String() == additionalToolsItem {
|
|
add(it.Get("tools"))
|
|
}
|
|
return true
|
|
})
|
|
return provider.CodexToolNamesBack(names)
|
|
}
|