diff --git a/internal/gateway/call_item_ids_test.go b/internal/gateway/call_item_ids_test.go index e11925817..c087a9cfb 100644 --- a/internal/gateway/call_item_ids_test.go +++ b/internal/gateway/call_item_ids_test.go @@ -172,7 +172,7 @@ func TestResponsesRelayCallIDs(t *testing.T) { if err := json.Unmarshal([]byte(last), &sent); err != nil { t.Fatal(err) } - if sent["tools"] != nil || !strings.Contains(last, "CONTEXT CHECKPOINT COMPACTION") { + if string(sent["tools"]) != "[]" || !strings.Contains(last, "CONTEXT CHECKPOINT COMPACTION") { t.Errorf("not a compaction request: %s", last) } if !strings.Contains(rec.Body.String(), `"type":"compaction"`) || !strings.Contains(rec.Body.String(), magpieCompaction) { diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go index 87bc3f150..71479b86d 100644 --- a/internal/gateway/codex_backend.go +++ b/internal/gateway/codex_backend.go @@ -1201,11 +1201,13 @@ func codexInput(body []byte, magpieModel bool) (_ []byte, compact bool) { b, _ := json.Marshal(out) q["input"] = b if compact { - // the summary is text; a tool call would be no summary. Otherwise - // it goes as Codex asks for its own summary, streamed with its - // tool_choice: a relay in front of the ChatGPT backend turns away - // one that isn't ("invalid codex request", #292) - delete(q, "tools") + // the summary is text; a tool call would be no summary, so it + // offers none. Otherwise it goes as Codex asks for its own summary + // from a provider it reaches itself: "tools": [] (not left out, + // #1511), streamed with its tool_choice — a relay in front of the + // ChatGPT backend turns away one that isn't ("invalid codex + // request", #292) + q["tools"] = json.RawMessage("[]") q["tool_choice"] = json.RawMessage(`"auto"`) q["parallel_tool_calls"] = json.RawMessage("false") } @@ -1646,7 +1648,7 @@ func (s *Server) codexCompact(w http.ResponseWriter, r *http.Request, body []byt } rec, res, err := ask(body) local := "" - if first := rec.status; first == http.StatusNotFound || itemNotFound(rec) || protectionRefused(first, rec.body.Bytes()) || + if first := rec.status; first == http.StatusNotFound || itemNotFound(rec) || upstreamFailed(rec) || protectionRefused(first, rec.body.Bytes()) || err != nil && protectionWords.MatchString(err.Error()) { who, msg := compactFailure(body, rec) if err != nil { @@ -1723,6 +1725,20 @@ func (s *Server) codexCompact(w http.ResponseWriter, r *http.Request, body []byt } } +// 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. +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()) +} + +var upstreamFailedWords = regexp.MustCompile(`(?i)upstream request failed`) + // itemNotFound is a 400 refusing an item of the input by its id, as Azure // OpenAI's and OpenAI's API answer one they never stored ("Item with id // 'rs_…' not found", #1008), which a relay in front of them passes on. diff --git a/internal/gateway/codex_compact_404_test.go b/internal/gateway/codex_compact_404_test.go index c2b6d3a99..0406a2782 100644 --- a/internal/gateway/codex_compact_404_test.go +++ b/internal/gateway/codex_compact_404_test.go @@ -97,3 +97,61 @@ func TestCodexCompactOtherFailuresStay(t *testing.T) { } } } + +// #1511 (TooPub): a relay answered a magpie model's summary request +// 400 invalid_request_error "apex: Upstream request failed", and Codex's +// compaction stopped there, while Codex asking the same relay itself +// compacted. The reporter's body, as it came. +const relayApex400 = `{"error":{"code":null,"message":"apex: Upstream request failed","param":null,"type":"invalid_request_error"}}` + +func TestCodexCompactUpstreamFailed400AsksAgainAsText(t *testing.T) { + f := &fake{t: t, reply: sse( + `data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"SUMMARY"}]}}`, + `data: {"type":"response.completed","response":{"id":"resp_p","status":"completed","output":[]}}`)} + f.refuse = func(body []byte) (int, string) { + if strings.Contains(string(body), `"rs_1"`) || strings.Contains(string(body), `"fc_1"`) { + return 400, relayApex400 + } + return 0, "" + } + setup(t, provider.Responses, f) + code, body := codexPost(t, compact404Request) + if code != 200 { + t.Fatalf("%d %s", code, body) + } + if got := compactedText(t, body); got != "SUMMARY" { + t.Errorf("summary %q", got) + } + if f.calls != 2 || !strings.Contains(string(f.got), "fix the login bug") { + t.Errorf("asked again (calls %d) with: %s", f.calls, f.got) + } +} + +func TestCodexCompactUpstreamFailed400CompactsLocally(t *testing.T) { + for _, proto := range []provider.Protocol{provider.Responses, provider.Chat} { + f := &fake{t: t, refuse: func([]byte) (int, string) { return 400, relayApex400 }} + setup(t, proto, f) + code, body := codexPost(t, compact404Request) + if code != 200 { + t.Fatalf("%v: %d %s", proto, code, body) + } + got := compactedText(t, body) + for _, want := range []string{"fix the login bug", "now add a test", "apex: Upstream request failed", "400"} { + if !strings.Contains(got, want) { + t.Errorf("%v: local summary lacks %q:\n%s", proto, want, got) + } + } + } +} + +// The relay's words on an account's refusal still go back to Codex. +func TestCodexCompactUpstreamFailedAccountStays(t *testing.T) { + for _, status := range []int{401, 403, 429} { + f := &fake{t: t, refuse: func([]byte) (int, string) { return status, relayApex400 }} + setup(t, provider.Responses, f) + code, body := codexPost(t, compact404Request) + if code == 200 || strings.Contains(string(f.got), "This is the conversation so far") { + t.Errorf("%d: %d %s (calls %d)", status, code, body, f.calls) + } + } +} diff --git a/internal/gateway/codex_compact_relay_test.go b/internal/gateway/codex_compact_relay_test.go index ee8c9d1c6..451426b08 100644 --- a/internal/gateway/codex_compact_relay_test.go +++ b/internal/gateway/codex_compact_relay_test.go @@ -41,7 +41,10 @@ func TestCodexCompactsThroughCodexRelay(t *testing.T) { if code != 200 { t.Fatalf("%d %s\nrelay got %s", code, body, f.got) } - if strings.Contains(string(f.got), `"tools"`) || !strings.Contains(string(f.got), "CONTEXT CHECKPOINT COMPACTION") { + // no tool offered, written as Codex writes its own summary request: + // "tools": [] (#1511) + if !strings.Contains(string(f.got), `"tools":[]`) || strings.Contains(string(f.got), "shell") || + !strings.Contains(string(f.got), "CONTEXT CHECKPOINT COMPACTION") { t.Errorf("relay got %s", f.got) } var item, done map[string]any