gateway: Codex's compaction on Open Design goes without empty tools, and its upstream_error is asked again (Mugenrikka on X)
Codex compacting on any Open Design model got 400 "[code=upstream_error] Provider returned error" from amr-link, while other providers compacted. Codex's compaction is its one request with "tools": [], and the summary ask adds tool_choice and parallel_tool_calls beside them (#292). Since plugins 0.1.13 /48f976402the Responses body is relayed to amr-link as written, and an OpenAI-style validator refuses empty tools and a lone tool_choice. - passthrough: a Responses request to Open Design (its plugin, or a provider at amr-link.open-design.ai) goes without an empty tools list and the tool_choice/parallel_tool_calls beside it (withoutEmptyTools). A body that declares a tool is unchanged; every other upstream is sent Codex's shape as before. - codexCompact: amr-link's "[code=upstream_error]" counts as upstreamFailed, so a summary still refused is asked as sent, then as text, then made locally, never handed to Codex as the 400. upstreamFailed is only used by the compaction ladder. Run (mote; the suite at54117164c, then rebased on1bfa8d5a1and -run 'OpenDesign|Compact' ./internal/gateway run again there: ok): - TestOpenDesignCompactionSendsNoEmptyTools and TestOpenDesignCompactionRefusedAsksAsText, built from Codex Desktop 26.1007's real compaction (testdata/codex-26.1007-compaction.json) through the fake plugin as opendesign: both red on the old gateway.go/codex_backend.go with the reporter's 400, green with them. - go test -run 'OpenDesign|Compact|LoneToolChoice|ToolsOff' ./internal/gateway ok. - go test -timeout 45m ./...: every package ok. - go vet (darwin), GOOS=linux -tags nogui and GOOS=windows vet of internal/gateway ok; gofmt -l clean. Not tried: a real Open Design account (none here). Which of the empty tools, the lone choices or the history's calls amr-link refuses is not confirmed; the second change covers whichever it is.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -4128,13 +4128,14 @@ func summaryEndedShort(rec *recorder) bool {
|
||||
// upstreamFailed is a relay's own word that the provider behind it turned
|
||||
// the request away, without saying why: "flyapi: Upstream request failed"
|
||||
// came as a 404 (#866), "apex: Upstream request failed" as a 400
|
||||
// invalid_request_error (#1511). Codex asking the same relay itself
|
||||
// compacts, so the summary request is asked again as text, and compacted
|
||||
// without a model when that fails too.
|
||||
// invalid_request_error (#1511), Open Design's "[code=upstream_error]
|
||||
// Provider returned error" as a 400 (vendorUnsaidWords). Codex asking the
|
||||
// same relay itself compacts, so the summary request is asked again as
|
||||
// text, and compacted without a model when that fails too.
|
||||
func upstreamFailed(rec *recorder) bool {
|
||||
return rec.status >= 400 && rec.status < 500 && rec.status != http.StatusUnauthorized &&
|
||||
rec.status != http.StatusForbidden && rec.status != http.StatusTooManyRequests &&
|
||||
upstreamFailedWords.Match(rec.body.Bytes())
|
||||
(upstreamFailedWords.Match(rec.body.Bytes()) || vendorUnsaidWords.Match(rec.body.Bytes()))
|
||||
}
|
||||
|
||||
var upstreamFailedWords = lazyre.New(`(?i)upstream request failed`)
|
||||
@@ -4152,7 +4153,7 @@ func roleRefused(rec *recorder, err error) bool {
|
||||
if rec.status >= 400 {
|
||||
return upstreamFailed(rec) || rec.status == http.StatusBadRequest && systemRefusedWords.Match(rec.body.Bytes())
|
||||
}
|
||||
return err != nil && (systemRefusedWords.MatchString(err.Error()) || upstreamFailedWords.MatchString(err.Error()))
|
||||
return err != nil && (systemRefusedWords.MatchString(err.Error()) || upstreamFailedWords.MatchString(err.Error()) || vendorUnsaidWords.MatchString(err.Error()))
|
||||
}
|
||||
|
||||
var systemRefusedWords = lazyre.New(`(?i)system messages are not allowed`)
|
||||
|
||||
@@ -6958,6 +6958,12 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
|
||||
if refusesLoneToolChoice(p) {
|
||||
body = withoutLoneToolChoice(body)
|
||||
}
|
||||
// Open Design's amr-link answers Codex's compaction summary, its
|
||||
// one request with no tools, 400 upstream_error: it is sent no
|
||||
// empty tools and no choice beside them (opendesign_tools.go)
|
||||
if openDesign(p) {
|
||||
body = withoutEmptyTools(body)
|
||||
}
|
||||
case provider.Chat:
|
||||
if p.RejectsTemperature(model) {
|
||||
// a model whose list refuses them (a plugin's temperature:
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// amrRefusal is how Open Design's amr-link turns a request away for the
|
||||
// vendor behind it (#5219, #4852): a 400 upstream_error that names no
|
||||
// field.
|
||||
const amrRefusal = `{"error":{"message":"[code=upstream_error] Provider returned error","type":"upstream_error","code":"upstream_error"}}`
|
||||
|
||||
var amrSummary = sse(
|
||||
`data: {"type":"response.created","response":{"id":"r1","status":"in_progress","output":[]}}`,
|
||||
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"OD SUMMARY"}]}}`,
|
||||
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":2}}}`)
|
||||
|
||||
// openDesignCompaction posts Codex Desktop 26.1007's real compaction
|
||||
// (compaction2352) at Open Design's plugin, its model on chat completions
|
||||
// and served on Responses too, as amr-link serves every model (FAKE_ALSO),
|
||||
// to a vendor that refuses what refuse says. It gives Codex's answer and
|
||||
// the bodies the vendor got.
|
||||
func openDesignCompaction(t *testing.T, refuse func(b []byte) bool) (*httptest.ResponseRecorder, [][]byte) {
|
||||
t.Helper()
|
||||
t.Setenv("FAKE_ALSO", "1")
|
||||
var mu sync.Mutex
|
||||
var got [][]byte
|
||||
pid := besideFake(t, "opendesign", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
mu.Lock()
|
||||
got = append(got, b)
|
||||
mu.Unlock()
|
||||
if !strings.HasSuffix(r.URL.Path, "/responses") {
|
||||
t.Errorf("asked at %s", r.URL.Path)
|
||||
}
|
||||
if refuse(b) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(400)
|
||||
io.WriteString(w, amrRefusal)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
io.WriteString(w, amrSummary)
|
||||
}))
|
||||
body, turn := compaction2352(t)
|
||||
body = strings.Replace(body, `"model":"fake/m1"`, `"model":"`+pid+`/fake-also"`, 1)
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body))
|
||||
req.Header.Set("User-Agent", "Codex Desktop/26.1007 (Mac OS 26.2.0; arm64)")
|
||||
req.Header.Set("originator", "Codex Desktop")
|
||||
req.Header.Set(turnMetadataHeader, turn)
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return rec, got
|
||||
}
|
||||
|
||||
// Mugenrikka on X: Codex compacting on any Open Design model got 400
|
||||
// upstream_error, while the turns around it went through. Codex's
|
||||
// compaction is its one request with no tools ("tools": []), and magpie's
|
||||
// summary ask added tool_choice and parallel_tool_calls beside them. With
|
||||
// nothing to call, amr-link is sent none of the three, and the summary
|
||||
// comes back.
|
||||
func TestOpenDesignCompactionSendsNoEmptyTools(t *testing.T) {
|
||||
lone := func(b []byte) bool {
|
||||
var q map[string]json.RawMessage
|
||||
json.Unmarshal(b, &q)
|
||||
var tools []any
|
||||
json.Unmarshal(q["tools"], &tools)
|
||||
_, choice := q["tool_choice"]
|
||||
_, parallel := q["parallel_tool_calls"]
|
||||
_, listed := q["tools"]
|
||||
return len(tools) == 0 && (listed || choice || parallel)
|
||||
}
|
||||
rec, got := openDesignCompaction(t, lone)
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"type":"compaction"`) {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Errorf("asked %d times, want once", len(got))
|
||||
}
|
||||
if len(got) > 0 && (lone(got[0]) || !bytes.Contains(got[0], []byte("CONTEXT CHECKPOINT COMPACTION"))) {
|
||||
t.Errorf("amr-link got %.600s", got[0])
|
||||
}
|
||||
}
|
||||
|
||||
// A summary amr-link refuses in its unsaid words anyway (here over the
|
||||
// history's tool calls, which Anthropic's API refuses with no tools
|
||||
// declared) is asked again with the conversation as text, as a relay's
|
||||
// "Upstream request failed" is, and Codex gets the summary, not the 400.
|
||||
func TestOpenDesignCompactionRefusedAsksAsText(t *testing.T) {
|
||||
calls := func(b []byte) bool { return bytes.Contains(b, []byte(`"function_call"`)) }
|
||||
rec, got := openDesignCompaction(t, calls)
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"type":"compaction"`) {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||
}
|
||||
if n := len(got); n < 2 || calls(got[n-1]) || !bytes.Contains(got[n-1], []byte("This is the conversation so far")) {
|
||||
t.Errorf("asked %d times; last %.600s", n, got[len(got)-1])
|
||||
}
|
||||
if strings.Contains(rec.Body.String(), "magpie could not") {
|
||||
t.Errorf("compacted locally: %s", rec.Body)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/yetone/magpie/internal/lazyre"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// Open Design Cloud's amr-link answers a request it, or the vendor behind
|
||||
// it, turns away with a 400 "[code=upstream_error]" and often no more than
|
||||
// "Provider returned error". Codex's compaction is the one request Codex
|
||||
// makes with no tools ("tools": [], codex-26.1007-compaction.json), and
|
||||
// magpie's summary ask for it adds the tool_choice and parallel_tool_calls
|
||||
// a relay of the ChatGPT backend checks for (#292): every model of the
|
||||
// plan answered that summary 400 upstream_error while the turns around it,
|
||||
// tools declared, went through (Mugenrikka on X). With no tools those two
|
||||
// ask nothing, and a Chat API (OpenAI's own) refuses them, and an empty
|
||||
// tools list, outright; so none of the three goes to amr-link.
|
||||
|
||||
// openDesign is whether p is Open Design Cloud: its plugin, or a provider
|
||||
// at amr-link's address.
|
||||
func openDesign(p provider.Provider) bool {
|
||||
if p.PluginProvider() == "opendesign" {
|
||||
return true
|
||||
}
|
||||
for _, proto := range []provider.Protocol{provider.Responses, provider.Chat, provider.Anthropic} {
|
||||
if provider.HostOf(p.Base(proto)) == "amr-link.open-design.ai" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// withoutEmptyTools is a Responses or Chat body without an empty tools
|
||||
// list and the tool_choice and parallel_tool_calls beside it; a body
|
||||
// that declares a tool goes as it came.
|
||||
func withoutEmptyTools(body []byte) []byte {
|
||||
if !bodyHas(body, `"tools"`) && !bodyHas(body, `"tool_choice"`) && !bodyHas(body, `"parallel_tool_calls"`) {
|
||||
return body
|
||||
}
|
||||
var q struct {
|
||||
Tools []json.RawMessage `json:"tools"`
|
||||
}
|
||||
if decodeJSON(body, &q) != nil || len(q.Tools) > 0 {
|
||||
return body
|
||||
}
|
||||
return withoutFields(body, "tools", "tool_choice", "parallel_tool_calls")
|
||||
}
|
||||
|
||||
// vendorUnsaidWords is amr-link's word for a refusal it passes on from the
|
||||
// vendor behind it, said or not why: a summary ask so refused is asked
|
||||
// again with the roles as Codex sent them, then as text, then made
|
||||
// locally (codexCompact), as for a relay's "Upstream request failed".
|
||||
var vendorUnsaidWords = lazyre.New(`(?i)\[code=upstream_error\]`)
|
||||
Reference in New Issue
Block a user