Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a failed Codex compaction says what failed, never "Unknown error" (outrageous_piglet)
yetone083a8a2d4750
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex 4e28b674b..04c9c9555 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -172,7 +172,7 @@ recorded as a prediction failure (`noPrediction`).- A relayed Chat reply whose choice made calls and ended `stop` ends `tool_calls`, streamed (`chatTidy`) and whole (`chatWhole`), as the translations' collector already ends it (`collector.finish`): told it stopped, a client that reads `finish_reason` ends its turn with the calls not run (喜欢一个人 on Discord, a relay in front of deepseek-v4.1-flash). Any other reason stays as it came. `TestChatCallsEndToolCalls` in [`chattidy_finish_test.go`](../../internal/gateway/chattidy_finish_test.go).- Codex's model list from the backend path (`codexModels`) is the ChatGPT backend's for its sign-in and then magpie's (`provider.CodexListed`), its ETag and every reply's `X-Models-Etag` tagged with `provider.CodexListTag` so a change has Codex ask again. A Codex there only for account failover, not connected (`CodexOwnOnly`, `agent.CodexOwnOnly`; #1385), gets the backend's alone, tagged with `provider.CodexOwnListTag`; neither tag contains the other, so connecting or disconnecting has Codex fetch the list again. The user's picks on the codex provider (`provider.CodexNativePicked`) narrow the backend's listed models. They keep its hidden ones (`visibility: "hide"`), which no picker shows. One of those is `codex-auto-review`: when the list lacks it and no reviewer is set, Codex's auto-review runs on the conversation's model at low effort (#1460, `TestCodexModelListPicksKeepHidden`). The account's own models kept out of agents' lists for routing groups (`provider.CodexNativeUnlisted`) are dropped from it too, hidden ones kept: every one when the codex provider is "Only through routing groups" (`Provider.Unlisted`), and those a group that unlists its members names (`Group.Unlists`). A Codex there only for failover keeps them, as it is handed no groups; `spawnChoices` drops the same ones (#1602, `TestCodexModelListUnlistedForGroups`).- Reasoning state goes through magpie's translation as the vendor wrote it when the agent and the upstream speak the same API (#1445, XYenon): a non-streaming Messages request to a stream-only account, web search on the agent's own API, a plugin, failover. The IR carries it sealed (`Part.Sealed`, `SealedBy`; events `KThinkStart`, `KSealed`, `KTextSig` in [`ir.go`](../../internal/gateway/ir.go)), so each thinking block or reasoning item stays apart, whole or streamed, and a seal is written only for its own API (`sealedBy`). Anthropic's `redacted_thinking` goes back raw (to DeepSeek as a thinking block, below), and a thinking block signed with no text keeps its signature (`aBlock` still writes `"thinking": ""`, #1447), on Claude's subscription too. A Responses reasoning item keeps its id, summary and `encrypted_content`, and goes back to a Responses upstream as it came when it has no reasoning text (`sealedReasoning`; text, another vendor's, is still left out for OpenAI, #1411). Gemini's `thoughtSignature` stays on the call it signed and on text; a later call of a signed step goes without one, and only an unsigned step or Claude on Antigravity gets `skip_thought_signature_validator`. Another API's agent gets the call's signature in its id, as on chat (#687), with its length in the mark (`__ts<n>__`), so an id a client shortened carries no signature and the call goes as unsigned (#1536); it gets no one else's seal. The signature itself is kept in magpie's folder, `gemini-signatures/<key>`, named by its hash (`keepSignature` in [`gemini_sigstore.go`](../../internal/gateway/gemini_sigstore.go)), and the id carries only the key (`__tsk16__<key>`), so an id stays under 64 characters however long the step's signature (#2411: LobeHub's PostgreSQL index turned away a 3KB id). A key magpie no longer has, or whose file doesn't hash to it, reads as no signature; one not read for 90 days is removed. Where the folder can't be written, the id carries the signature as before. Carrying reasoning across APIs (an envelope) isn't done.-- A translated Responses reply gives Codex the reasoning items a Responses upstream sent, as it sent them; any other upstream's reasoning becomes items magpie made: an `rs_` id and a summary, nothing sealed. Codex hands them back on every turn. A request relayed as is to OpenAI's API or Azure OpenAI goes without them (`withoutBareReasoning`) unless `store` is true, because both look the id up among stored items and answer 400 "Item with id 'rs_…' not found" (#1008). Any other Responses upstream gets them, because a DeepSeek model wants its reasoning back (#388). When one answers 400 naming the input (`refusesInput`: `param` is `input`, or the error names an item by id), as a relay in front of OpenAI does with a message of its own, `passthrough` asks once more without them and, once that works, leaves them out for that provider and model from then on (`bareReasoningRefused`, #1044). Codex's compaction offers no tools, so it is relayed even when its turns were translated. The summary request magpie makes offers none either: `codexInputAs` sends `tools: []` and leaves out the `additional_tools` input item, where Codex's Responses Lite (the entry of a ChatGPT account's gpt-6 model, copied from Codex's own) puts every tool; left in, the model may call one in place of the summary (the likely cause of WuSix's codex/gpt-6-astra "the model wrote no summary" on Discord, not confirmed on a real reply), and a reply with no summary says what it held instead (`answeredWith`). When a provider answers its summary request 404, or 400 for an item it didn't find (`itemNotFound`, a relay in front of either), `codexCompact` asks once more with the conversation as text (`plainCompact`), then compacts locally (#866, #1008). So does the ChatGPT backend's 502 "response protection is unavailable" (`protectionRefused`): a long history refused so was answered as text. Codex also compacts remotely, a `compaction_trigger` on `/responses` that wants exactly one compaction item back, on any provider table named "OpenAI", as CC Switch names its relay tables. magpie points those at `/v1` while a magpie model is on, so `handle` makes the summary there too (`codexInput`, `codexCompact`), and magpie's summaries go back to the model as text (#1301). A summary is wanted only whole (`askWhole`, `holdWriter.wantWhole`): its stream is held to its end, not let through at its first words, so a reply cut short (`errEndedShort`), failed halfway (a 429 "Individual quota reached" after some of it) or ended `incomplete` (`endedIncomplete`, `incompleteReply`) fails over to the next candidate like a failure before any of it, and rests the account as that failure would; one that reached its output limit rests nobody. With nobody left, `codexCompact` answers 502 and never hands Codex the words before the cut as the summary (#2274, huangzuomin: Antigravity cutting each summary short). Codex with context_management's experimental mode compacts in the background at every step while the conversation is over its threshold and drops a failed compaction without a word, so a conversation whose summaries keep failing is asked again every few seconds, each paying for the whole history (1169 compactions in 6 hours there). After `compactFailsMost` (3) failed summaries in a row of one conversation (`prompt_cache_key`) on one model, each within `compactHeldFor` (5 minutes) of the last, the next is answered at once with a 400 naming the last failure, which Codex doesn't ask again, and no model is asked until 5 minutes have passed (`compactFails` in [`codex_compact_guard.go`](../../internal/gateway/codex_compact_guard.go)); a summary that comes clears the count, and a sign-in's 401 or 403 isn't counted. `TestCodexCompactCutSummaryFailsOver`, `TestCodexCompactCutSummaryIsNoSummary`, `TestCodexCompactKeepsFailingIsHeld`.+- A translated Responses reply gives Codex the reasoning items a Responses upstream sent, as it sent them; any other upstream's reasoning becomes items magpie made: an `rs_` id and a summary, nothing sealed. Codex hands them back on every turn. A request relayed as is to OpenAI's API or Azure OpenAI goes without them (`withoutBareReasoning`) unless `store` is true, because both look the id up among stored items and answer 400 "Item with id 'rs_…' not found" (#1008). Any other Responses upstream gets them, because a DeepSeek model wants its reasoning back (#388). When one answers 400 naming the input (`refusesInput`: `param` is `input`, or the error names an item by id), as a relay in front of OpenAI does with a message of its own, `passthrough` asks once more without them and, once that works, leaves them out for that provider and model from then on (`bareReasoningRefused`, #1044). Codex's compaction offers no tools, so it is relayed even when its turns were translated. The summary request magpie makes offers none either: `codexInputAs` sends `tools: []` and leaves out the `additional_tools` input item, where Codex's Responses Lite (the entry of a ChatGPT account's gpt-6 model, copied from Codex's own) puts every tool; left in, the model may call one in place of the summary (the likely cause of WuSix's codex/gpt-6-astra "the model wrote no summary" on Discord, not confirmed on a real reply), and a reply with no summary says what it held instead (`answeredWith`). When a provider answers its summary request 404, 405 or 501 (`lacksEndpoint`), or 400 for an item it didn't find (`itemNotFound`, a relay in front of either), `codexCompact` asks once more with the conversation as text (`plainCompact`), then compacts locally (#866, #1008). Any other failure goes back with its status, said as the compaction's with who answered it (`compactFailed`: "compaction: <provider> (<model>, <base URL>) answered the summary request 502 Bad Gateway: …"), the vendor's code, param and Retry-After kept. A compaction relayed to OpenAI (`/responses/compact` on Codex's own model, as a `/responses` turn) that OpenAI answers with an error status and an empty body, which Codex shows as "unexpected status 502 Bad Gateway: Unknown error", or with a web page, is said in words naming the model, the status, and the account it went to (`codexSaidNothing`, outrageous_piglet on Discord); an error OpenAI says in JSON goes as it came. `TestCodexOwnCompactFailureIsSaid`, `TestCodexCompactFailureSaysCompaction`, `TestCodexCompactNoEndpointCompactsLocally`. So does the ChatGPT backend's 502 "response protection is unavailable" (`protectionRefused`): a long history refused so was answered as text. Codex also compacts remotely, a `compaction_trigger` on `/responses` that wants exactly one compaction item back, on any provider table named "OpenAI", as CC Switch names its relay tables. magpie points those at `/v1` while a magpie model is on, so `handle` makes the summary there too (`codexInput`, `codexCompact`), and magpie's summaries go back to the model as text (#1301). A summary is wanted only whole (`askWhole`, `holdWriter.wantWhole`): its stream is held to its end, not let through at its first words, so a reply cut short (`errEndedShort`), failed halfway (a 429 "Individual quota reached" after some of it) or ended `incomplete` (`endedIncomplete`, `incompleteReply`) fails over to the next candidate like a failure before any of it, and rests the account as that failure would; one that reached its output limit rests nobody. With nobody left, `codexCompact` answers 502 and never hands Codex the words before the cut as the summary (#2274, huangzuomin: Antigravity cutting each summary short). Codex with context_management's experimental mode compacts in the background at every step while the conversation is over its threshold and drops a failed compaction without a word, so a conversation whose summaries keep failing is asked again every few seconds, each paying for the whole history (1169 compactions in 6 hours there). After `compactFailsMost` (3) failed summaries in a row of one conversation (`prompt_cache_key`) on one model, each within `compactHeldFor` (5 minutes) of the last, the next is answered at once with a 400 naming the last failure, which Codex doesn't ask again, and no model is asked until 5 minutes have passed (`compactFails` in [`codex_compact_guard.go`](../../internal/gateway/codex_compact_guard.go)); a summary that comes clears the count, and a sign-in's 401 or 403 isn't counted. `TestCodexCompactCutSummaryFailsOver`, `TestCodexCompactCutSummaryIsNoSummary`, `TestCodexCompactKeepsFailingIsHeld`.- A reasoning item with its text in it (`content` parts: DeepSeek's Responses API writes `reasoning_text`, relayed to Codex as it came) is another vendor's. OpenAI's API and the ChatGPT backend take a reasoning item's `content` only empty, and refuse the whole request over one with 400 "Invalid 'input[n].content': array too long. Expected an array with maximum length 0" (`array_above_max_length`). Codex hands the item back on every later turn, so it never goes to them: for one of Codex's own models, `codexInput(body, false)` leaves it out (and a reasoning item with nothing sealed in it) on `/backend-api/codex`'s `/responses` and on `/responses/compact`, before Codex's own sign-in, its API key or a magpie Codex account (`compactOn`) is asked. Codex compacts a thread before its first turn back on its own model, and a refused compaction shows as that turn's error (jue on Discord). `passthrough` leaves it out for OpenAI, Azure OpenAI and a Codex account (`withoutReasoningText`), and for any other upstream once it has refused one (`refusesReasoningText`, `reasoningTextRefused`, #1411). OpenAI's own sealed reasoning (`encrypted_content`, no text) stays, and a DeepSeek model (`replaysReasoning`) still gets the text. `TestForeignReasoningNeverReachesOpenAI`, `TestReasoningTextLeftOutWhereRefused`, `TestReasoningTextNotSentToCodexAccount`.- Native compaction (#1099, off by default): a provider with `nativeCompaction` on (`provider.Provider.CompactsOn`; the editor's Compaction row, `magpie provider set <id> compaction=native`) is asked Codex's `compaction_trigger` as Codex sent it, and the compaction item it seals goes back to Codex unchanged, not as a `magpie1:` summary. Only a wire that carries it can be set so (`CarriesCompaction`): a Responses API, a ChatGPT account, GitHub Copilot (its models asked on Responses; its Claude models keep magpie's summary). The decision is made where `handle` and `codexBackend` read the input (`nativeCompactionFor`, `codexInputAs`): a group compacts natively only when every member does. Such a request, and a later one carrying a sealed compaction to a provider set so or to the provider recorded as its writer, is relayed, never translated, which would drop the item (`keepsCompaction` in `attempt`); a provider that can't be asked it on Responses answers a 400 saying so. The key or account that answered is recorded, whoever it is: a fallback or another member that answered the trigger sealed it whatever its own setting, and only it can read the seal (#2271, JasonBuildAI: a fallback's seal went unrecorded, and the next turn was answered 200 without the compacted history; `TestNativeCompactionFallbackSealStaysWithItsWriter`). It is recorded by the hash of what it sealed (`wroteCheckpoint`, `compactions.json` beside `affinity.json`, 0600, at most 1024 for 30 days). A later request carrying it goes only to that writer (`checkpointReaders`); a writer whose own list has since dropped the model is tried still, as the last resort an account whose list lacks the model is — it sealed the model's compaction, and no other can read it (`planned.unlisted`, `TestNativeCompactionWriterWhoseListLacksTheModel`, #2642: it was refused as "switched off, removed, or no longer serves" though it was on, and switching it on changed nothing). With the writer gone from the candidates, magpie answers 400 `checkpointError` with the reason its settings give (`checkpointGone`: the provider removed, the account signed out, its sign-in lapsed, paused while others are on, switched off, kept from the model in Names & levels, the key off or gone, the model disabled, or the model or group asked for not going to it), what lets the conversation go on, and whether native compaction is on for the writer's provider now, and never sends it to another key, drops it, or summarises it in its place (`TestNativeCompactionWriterGoneSaysWhy`). One of Codex's own models asked on the backend path is decided as a magpie model is when magpie's Codex accounts serve it (`codexAccounts`): the trigger is kept only with the codex provider's native compaction on, else `codexCompact` gives magpie's summary, which any account reads, and no writer is recorded (#2582, ump45nose; before, every such compaction was sealed natively whatever the setting, and the conversation was held to its account; `TestCodexOwnModelCompactionHonoursTheToggle`). Codex's own sign-in alone, relayed as it came, still compacts natively, unrecorded, as does `/responses/compact`; but a request carrying a compaction magpie recorded the writer of goes through routing even then (`codexSealedFor`), so with the writer switched off the one account left isn't sent it. A legacy `/responses/compact` carrying one is judged at its passthrough (`sealedCompact`): the writer on in magpie answers it signed as the writer — its own seal while the sign-in can be asked, a key's seal, a request signing with its own `chatgpt-account-id`, or an unreadable sign-in state relays as it came — and a writer gone is refused with `checkpointError`'s reason, the same words as on `/responses` (`TestNativeCompactionCompactJudgesItsWriter`, #2727). The writer matched is the account user the record holds, so two Team seats of one email read as one and such a compact relays as it came. A gateway key held to some accounts (#905) is asked of the account the compact goes to (`compactSigninHeld`): the writer when it answers it, so a writer the key may not use is refused 403 (`sealedCompactHeldError`) with nothing asked, and one it may use answers though the sign-in isn't the key's (`TestNativeCompactionCompactKeepsTheKeysAccounts`). With the writer held at its usage cap or for credits it may not spend, the answer is 429 `checkpointCappedError` (record `checkpointCappedRecord`, "native compaction's account at its usage cap"), naming that account, that the others serving the model weren't tried for the compaction, and that neither the routing mode nor how conversations stay changes that: wait for the window, raise or lift its cap, or start a new conversation (#2517, lm0711; before, it read "every account at its usage cap" though another had room; `TestNativeCompactionWriterAtItsCap`). `withoutCompaction` (the drop after a refused reasoning item) isn't used for such a request. A route carrying a compaction names its sealer (`Route.Compaction`), and Routing says that is why no other account was asked, not that it is "the only account on" for the model (#2639; `TestNativeCompactionRouteNamesItsWriter`, `routing-compaction.test.cjs`). A refusal of the trigger reaches Codex as the upstream sent it. Not tried against a real upstream; Copilot may refuse a history that holds Codex's `web_search` items. [`native_compaction.go`](../../internal/gateway/native_compaction.go), `TestNativeCompaction*`.- A compaction magpie answers itself (`codexCompact`: a magpie model, Codex's own models through magpie's Codex accounts, a CC Switch table) goes to Codex as a `compaction` item whose `encrypted_content` is `magpie1:` and the summary in base64 (`codexsummary.Encrypted`), and `codexInputAs` sends Codex's own summary message in its place (`codexsummary.Message`: Codex's summary prefix, a newline and the summary, as a user message). Codex keeps the item in its rollout and replays it, and OpenAI refuses it once Codex talks to OpenAI itself (`invalid_encrypted_content`, LukeMa on X). So `sessions.PortCodexRollouts` writes that same message into the rollouts in its place ([`codex_portable.go`](../../internal/sessions/codex_portable.go)). It changes only magpie's items, keeps every other line's bytes and the file's line count, mode and time, and drops an unreadable item together with its `replacement_history_metadata` entry. It publishes as Codex publishes a compressed rollout: under `thread-writer-locks/.coordination.lock`, only with the thread's lock free, through a temp file and a rename. A thread a Codex has open is left for a later pass, and a Codex home without writer locks is left alone. It runs 2 minutes after the GUI or `magpie serve` starts and every 10 minutes after, on Codex Disconnect (3s), and as the GUI quits (3s). Each pass reads what changed since the last whole pass, plus what was held (`codex-portable.json`). A ported thread was resumed by Codex 0.162.1 against an upstream that refuses `magpie1:` as OpenAI does. Not tried: a Codex in a WSL distro, whose locks this machine can't take, so it is skipped. `TestPortCodexRollouts*`, `TestPortedCompactionReadsTheSame`.diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.goindex d638837a6..195d2f9c8 100644--- a/internal/gateway/codex_backend.go+++ b/internal/gateway/codex_backend.go@@ -7,6 +7,7 @@ import ("encoding/json""errors""fmt"+ "html""io""log""net/http"@@ -1222,7 +1223,7 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest stend(res.StatusCode, res.Status, 0, 0)return}- if rest == "/responses" && res.StatusCode >= 400 {+ if (rest == "/responses" || rest == "/responses/compact") && res.StatusCode >= 400 {msg, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))res.Body = io.NopCloser(bytes.NewReader(msg))if overload && codexBusy(res.StatusCode, msg) && s.codexFallsBack(modelOf(body), body) {@@ -1233,10 +1234,11 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest sts.codexFallBack(w0, r, body0, kept)return}- if len(bytes.TrimSpace(msg)) == 0 {+ if said, ok := codexSaidNothing(rest, modelOf(body), codexWho(base, on), res, msg); ok {// a failure with nothing said of it, which Codex shows as- // "Unknown error" alone (#409): said where the turn went- said := codexFailedEmpty(modelOf(body), res.Status)+ // "Unknown error" alone (#409), or a web page Codex shows+ // as its markup: said where the turn, or the compaction+ // (outrageous_piglet on Discord), wentfailWith(res.StatusCode, said)s.record(Call{Time: start, From: provider.Responses, To: provider.Responses, Model: modelOf(body),Provider: "openai", Agent: agentOf(r), Kind: callKind(r.Header), Status: res.StatusCode,@@ -1576,6 +1578,51 @@ func codexFailedEmpty(model, status string) string {"(provider/model, or set it in magpie's Agents view), or try again later"}+// codexWho names who a request relayed to OpenAI went to: Codex's own+// sign-in, its API key, or one of magpie's ChatGPT accounts (on).+func codexWho(base string, on *provider.Provider) string {+ switch {+ case on != nil && on.Account != nil && on.Account.User != "":+ return "OpenAI (" + on.Account.User + ")"+ case base == codexAPIBase:+ return "OpenAI's API"+ }+ return "OpenAI"+}++// codexSaidNothing is what Codex is told of a failure OpenAI answered a+// turn or a compaction with that it can't show: an empty body, which+// Codex shows as "unexpected status 502 Bad Gateway: Unknown error"+// (#409, outrageous_piglet on Discord: a compaction), or a web page in+// place of an API error, which it shows as markup. ok is false for an+// error Codex reads as it came.+func codexSaidNothing(rest, model, who string, res *http.Response, msg []byte) (string, bool) {+ t := bytes.TrimSpace(msg)+ page := len(t) > 0 && (htmlStart.Match(t) || strings.HasPrefix(strings.ToLower(res.Header.Get("Content-Type")), "text/html"))+ if len(t) > 0 && !page {+ return "", false+ }+ if rest == "/responses" && len(t) == 0 && who == "OpenAI" {+ return codexFailedEmpty(model, res.Status), true+ }+ what := "this turn"+ if rest == "/responses/compact" {+ what = "this compaction"+ }+ said := " and said nothing more"+ if page {+ said = " with a web page in place of an API error"+ if m := pageTitle.FindSubmatch(msg); m != nil {+ if title := strings.Join(strings.Fields(html.UnescapeString(string(m[1]))), " "); title != "" {+ said += " (“" + clipRunes(title, 120) + "”)"+ }+ }+ }+ return who + " answered " + what + " for " + model + " " + res.Status + said ++ ". " + model + " is one of Codex's own models, which goes to OpenAI, not to a provider in magpie; try again later, " ++ "or pick one of magpie's models in Codex (provider/model, or set it in magpie's Agents view)", true+}+// apiKey reports whether Codex signed in with an API key rather than a// ChatGPT account.func apiKey(h http.Header) bool {@@ -2625,7 +2672,7 @@ func userMessage(text string) map[string]any { return codexsummary.UserMessage(t//// A provider that answers the summary 404 — a relay that serves the// conversation turn by turn but not the summary of it, "Upstream request-// failed" (#866) — or 400 for an item of it it doesn't have (#1008) is+// failed" (#866) — or 405 or 501 (lacksEndpoint), or 400 for an item of it it doesn't have (#1008) is// asked once more with the conversation as plain text, none of its items'// ids or sealed reasoning in it; when that fails too the summary is// magpie's own, the conversation's user messages and last reply,@@ -2634,7 +2681,8 @@ func userMessage(text string) map[string]any { return codexsummary.UserMessage(t// Discord): the same long history failed so on every account and model,// and as plain text was summarised — and one it breaks off with that// refusal after the reply began, HTTP 200 and an error event (#1270). Any-// other failure (401, 429, 500) goes back to Codex as it came.+// other failure (401, 429, 500) goes back to Codex with its status, said+// as the compaction's, with who answered it (compactFailed).//// The summary is asked for whole (askWhole): a reply that breaks off, or// ends incomplete, is a failure the next account or member answers, as@@ -2669,7 +2717,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) || upstreamFailed(rec) || protectionRefused(first, rec.body.Bytes()) ||+ if first := rec.status; lacksEndpoint(first) || itemNotFound(rec) || upstreamFailed(rec) || protectionRefused(first, rec.body.Bytes()) ||err != nil && protectionWords.MatchString(err.Error()) {who, msg := compactFailure(body, rec)if err != nil {@@ -2690,13 +2738,18 @@ func (s *Server) codexCompact(w http.ResponseWriter, r *http.Request, body []byt}}if rec.status >= 400 {- _, msg := compactFailure(body, rec)+ // said as the compaction's failure, with who answered it: a bare+ // "Fake: 502 Bad Gateway" read as Codex's own, and a body with+ // nothing in it as "Unknown error" (outrageous_piglet on Discord);+ // the vendor's code, param and Retry-After go with it+ who, msg := compactFailure(body, rec)s.compactFails.failed(key, rec.status, msg)for k, vs := range rec.header {- w.Header()[k] = vs+ if k != "Content-Length" && k != "Content-Type" {+ w.Header()[k] = vs+ }}- w.WriteHeader(rec.status)- w.Write(rec.body.Bytes())+ writeVendorError(w, provider.Responses, rec.status, compactFailed(who, rec.status, msg), rec.body.Bytes())return}if err == nil && local == "" && res.Status == "incomplete" {@@ -2765,6 +2818,24 @@ func (s *Server) codexCompact(w http.ResponseWriter, r *http.Request, body []byt}}+// compactFailed is what Codex is told of a summary request that failed:+// that it was the compaction's, who answered it and what they said —+// never nothing, which Codex shows as "Unknown error".+func compactFailed(who string, status int, msg string) string {+ if strings.TrimSpace(msg) == "" {+ msg = "nothing more"+ }+ return fmt.Sprintf("compaction: %s answered the summary request %d %s: %s", who, status, http.StatusText(status), msg)+}++// lacksEndpoint is a status saying the provider has no summary request to+// answer at that address: 404, 405 (a relay that takes GETs there) or 501.+// The summary is asked again as plain text, and made without a model when+// that fails too, as for a relay's 404 (#866).+func lacksEndpoint(status int) bool {+ return status == http.StatusNotFound || status == http.StatusMethodNotAllowed || status == http.StatusNotImplemented+}+// 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 400diff --git a/internal/gateway/codex_compact_said_test.go b/internal/gateway/codex_compact_said_test.gonew file mode 100644index 000000000..a3db16856--- /dev/null+++ b/internal/gateway/codex_compact_said_test.go@@ -0,0 +1,109 @@+package gateway++import (+ "encoding/json"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/yetone/magpie/internal/provider"+)++// codexErrorMessage is error.message of a reply, as Codex reads it+// (codex-rs UnexpectedResponseError::extract_error_message): "" is what+// Codex shows as "Unknown error" when the body is empty too.+func codexErrorMessage(body string) string {+ var e struct {+ Error struct {+ Message string `json:"message"`+ } `json:"error"`+ }+ json.Unmarshal([]byte(body), &e)+ return strings.TrimSpace(e.Error.Message)+}++// outrageous_piglet on Discord: Codex's compaction ended "unexpected+// status 502 Bad Gateway: Unknown error", which Codex says of an error+// status whose body is empty. A compaction of Codex's own model relayed+// to OpenAI (/responses/compact) went back with OpenAI's body as it came,+// nothing in it, or a web page; it is said now, as a turn's is (#409).+func TestCodexOwnCompactFailureIsSaid(t *testing.T) {+ for _, path := range []string{"/responses/compact", "/responses"} {+ for _, b := range []string{"", "\n", "<html><head><title>502 Bad Gateway</title></head><body>cloudflare</body></html>"} {+ setup(t, provider.Chat, &fake{t: t})+ chatgpt(t, func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusBadGateway)+ io.WriteString(w, b)+ })+ rec := httptest.NewRecorder()+ req := httptest.NewRequest("POST", CodexPath+path, strings.NewReader(`{"model":"gpt-6-sol","stream":true,"input":[{"type":"message","role":"user","content":"remember this"},{"type":"compaction_trigger"}]}`))+ New().Handler().ServeHTTP(rec, req)+ msg := codexErrorMessage(rec.Body.String())+ if rec.Code != http.StatusBadGateway || !strings.Contains(msg, "502") || !strings.Contains(msg, "gpt-6-sol") {+ t.Errorf("%s %q: %d %q", path, b, rec.Code, rec.Body)+ }+ if path == "/responses/compact" && !strings.Contains(msg, "compaction") {+ t.Errorf("%s %q doesn't say it was the compaction: %q", path, b, msg)+ }+ if strings.HasPrefix(b, "<html>") && (!strings.Contains(msg, "web page") || !strings.Contains(msg, "“502 Bad Gateway”")) {+ t.Errorf("%s: the page isn't named: %q", path, msg)+ }+ }+ }+}++// An error OpenAI says in words goes to Codex as it came.+func TestCodexOwnCompactErrorAsSaid(t *testing.T) {+ setup(t, provider.Chat, &fake{t: t})+ const said = `{"error":{"message":"busy now","type":"server_error"}}`+ chatgpt(t, func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusBadGateway)+ io.WriteString(w, said)+ })+ rec := httptest.NewRecorder()+ req := httptest.NewRequest("POST", CodexPath+"/responses/compact", strings.NewReader(`{"model":"gpt-6-sol","input":[{"type":"message","role":"user","content":"hi"}]}`))+ New().Handler().ServeHTTP(rec, req)+ if rec.Code != http.StatusBadGateway || rec.Body.String() != said {+ t.Errorf("%d %q", rec.Code, rec.Body)+ }+}++// A magpie model's summary request that fails goes back said as the+// compaction's, with who answered it: "Fake: 502 Bad Gateway" alone read+// as Codex's own failure, whatever the provider said or didn't.+func TestCodexCompactFailureSaysCompaction(t *testing.T) {+ for _, b := range []string{"", "<html>bad gateway</html>", `{"error":{"message":"upstream overloaded","code":"overloaded"}}`} {+ f := &fake{t: t, refuse: func([]byte) (int, string) { return http.StatusBadGateway, b }}+ setup(t, provider.Responses, f)+ code, body := codexPost(t, compact404Request)+ msg := codexErrorMessage(body)+ if code != http.StatusBadGateway || !strings.HasPrefix(msg, "compaction: ") || !strings.Contains(msg, "fake (fake/m1") || !strings.Contains(msg, "502") {+ t.Errorf("%q: %d %s", b, code, body)+ }+ if strings.Contains(b, "overloaded") && (!strings.Contains(msg, "upstream overloaded") || !strings.Contains(body, `"code":"overloaded"`)) {+ t.Errorf("the vendor's words or code are lost: %s", body)+ }+ }+}++// A provider with no summary request to answer there — 405 or 501, as a+// relay's 404 (#866) — is asked again as text, then compacted without a+// model, so Codex's compaction completes.+func TestCodexCompactNoEndpointCompactsLocally(t *testing.T) {+ for _, status := range []int{http.StatusMethodNotAllowed, http.StatusNotImplemented} {+ f := &fake{t: t, refuse: func([]byte) (int, string) { return status, "" }}+ setup(t, provider.Responses, f)+ code, body := codexPost(t, compact404Request)+ if code != 200 {+ t.Fatalf("%d: %d %s", status, code, body)+ }+ if got := compactedText(t, body); !strings.Contains(got, "fix the login bug") || !strings.Contains(got, "now add a test") {+ t.Errorf("%d: local summary %q", status, got)+ }+ if f.calls != 2 {+ t.Errorf("%d: asked %d times, want the summary and its plain retry", status, f.calls)+ }+ }+}