Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a compaction a model answers with a tool call is asked again as text
yetone8edb9dbb75eb
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex 24a0bb063..1451ce8d8 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -232,7 +232,7 @@ recorded as a prediction failure (`noPrediction`).- The Codex app and its VS Code extension read only the first 100 of that list by priority, hidden entries counted: each asks its app-server for `model/list` with `limit: 100` and never for the next page (ChatGPT.app 26.1013, openai.chatgpt 26.51021; the CLI's `/model` lists every one). So `codexcat.Fit` ranks the list last of all: hidden entries after every listed one, and, with more than 100 listed (`codexcat.AppList`), the models Codex used in the last 30 days (`usage.CodexUsed`, most recent first, at most 100) brought into the first 100 in the order they stood, the rest kept in theirs. The user's own order (`codexcat.Order`, #855) goes first. The same holds for `/v1/codex/models` (with the models that key's Codex used alone, `usage.CodexUsedBy`, before the 1 MiB cut) and the `model_catalog_json` magpie writes (`codexcat.CatalogUsed`). `codexListTag` carries a tag of the used set (`codexcat.UsedTag`), so the ETag and `X-Models-Etag` change when Codex uses a model it hadn't; `usage.CodexUsed` is read again a minute on, or at once when `Append` writes a Codex call to a model it doesn't hold (`codexUsedNew`). `TestCodexModelListKeepsUsedModelsInTheAppsHundred`, `TestCodexCatalogKeepsTheKeysUsedModelsFirst`, `TestFitKeepsUsedModelsWhereTheCodexAppReads` and `TestCodexUsed` cover it (@ZhenLuoTTAWDTT on X).- 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. Across APIs, only Claude's reasoning is carried to a Responses client, below.- Claude's reasoning goes to a Responses client (Codex) in an envelope and comes back as Claude wrote it (mysterious_dove_96933 on Discord). Anthropic wants a tool loop's next request to have the turn's thinking blocks, signed, and its `redacted_thinking` whole, or answers 400; Codex with `store: false` keeps only a reply's items, and of a reasoning item only `encrypted_content` is opaque. So the Anthropic decoders name a signature as Claude's (`KSig` with `sealAnthropic`, `Part.SignedBy`), and the Responses encoder and `renderResponses` give each thinking block a reasoning item whose `encrypted_content` is `magpie-claude1:` and the block's JSON in base64 (`claudeThinking`, [`internal/claudeseal`](../../internal/claudeseal/claudeseal.go)): its summary with it, an empty summary for a block signed with no text, and a `redacted_thinking` block as an item of its own. `parseResponses` reads such an item back into the block (`claudePart`), in order, so `anthropicBody` sends it as it was; an envelope that can't be read is its summary, as before. A Gemini thought's signature isn't named, and isn't sealed. The envelope goes to no Responses API: `passthrough` and `forwardOnce` take it out (`withoutClaudeSeals`: the item keeps its summary, one with none goes), `codexInputAs` leaves such an item out for Codex's own models as it does one with nothing sealed, and `codexUpstreamOn` takes it out before the ChatGPT backend; OpenAI's own `encrypted_content` is untouched. `redact.MaskJSON` keeps it whole (a reasoning item's seal), so the placeholders Claude was sent stay in what its signature covers. Claude's subscription names its signatures too, so Codex gets the same items, though its tool loop goes on inside Claude Code's own run, which keeps the thinking itself. `TestCodexToolLoopGivesClaudeItsThinkingBack`, `TestClaudeBlocksNeverGoToAResponsesAPI`, `TestGeminiSignatureIsNotSealedAsClaudes`, `TestMaskJSONKeepsClaudeBlocks`.-- 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 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`). A reply that called a tool instead (`calledTool`) is asked once more with the conversation as text (`plainCompact`), and compacted locally when that writes no summary either: a model deep in an exchange of calls goes on with it whatever it is offered (Lucy on Discord: DeepSeek through group/auto-deepseek-v4-1-flash answered reasoning and an `exec_command` call 3 times in a row; `TestCodexCompactToolCallAsksAgainAsText`, `TestCodexCompactToolCallEveryTimeCompactsLocally`, over Chat, Responses and Messages, a group and a single model). 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 trigger is kept only when a key or account the request would go over (the provider's and its fallbacks', or a group's members', held and capped ones too, not one whose own list lacks the model) can be asked it on Responses (`plannedCarrier`, read from `perKeyBarred` without weighing, so a provider routed in turn isn't moved along by the decision). When the vendor lists the model only to keys that speak no Responses there, a key set to Chat say, `codexCompact` gives magpie's summary through the listed key, as with native compaction off; before, the trigger was kept and refused 400 `carrierError` with no upstream asked (#2725, ggbdpq; `TestNativeCompactionTriggerSummarisedWhenNoListedKeyCarriesIt`, `TestNativeCompactionTriggerThroughTheListedKeyThatCarriesIt`, `TestNativeCompactionDecisionLeavesTheTurnsAlone`). 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)). A reasoning item carrying Claude's block (`magpie-claude1:`, above) is written as its summary, as the gateway sends it to OpenAI (`TestPortLineDropsClaudeBlocks`). 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. Every rewritten line keeps its length (`fitLine`: a shorter one padded with spaces after its JSON, a longer one in a paginated rollout not written), as Codex keeps places in a rollout by byte: a fork's `history_base.end_byte_offset` and the state database's turn and projection offsets. A shorter parent left a fork's place past its end, and Codex refused to open the fork ("cutoff byte offset is past the source rollout", xybio on Discord); once per Codex home a pass sets each such fork's place again from the ordinal it names (`repairLineages`). `MoveCodexProvider` keeps the session_meta line's length the same way and refuses a longer provider id for a paginated rollout. 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 95a66641c..7e05e55ee 100644--- a/internal/gateway/codex_backend.go+++ b/internal/gateway/codex_backend.go@@ -2959,18 +2959,42 @@ func (s *Server) codexCompact(w http.ResponseWriter, r *http.Request, body []bytwriteError(w, provider.Responses, 502, msg)return}+ if local == "" && strings.TrimSpace(res.summary()) == "" && calledTool(res.Output) {+ // a model deep in a conversation of tool calls goes on calling+ // them, offered none or not: DeepSeek through a group answered the+ // summary request with reasoning and an exec_command call, 3 times+ // in a row (Lucy on Discord, auto-deepseek-v4-1-flash). Whatever+ // let it — an upstream that reads calls out of its text with no+ // tools offered, a history tool declared as a stub that it calls+ // anyway — the history's calls are what it follows. Asked again+ // with the conversation as text, there's no exchange of calls to+ // go on with; when it still writes no summary, magpie compacts+ // without a model, so Codex goes on rather than failing over and+ // over+ who, _ := compactFailure(body, rec)+ called := answeredWith(res.Output)+ log.Printf("codex compaction: %s wrote no summary%s; asking again with the conversation as text", who, called)+ prec, pres, perr := ask(plainCompact(body))+ switch {+ case perr == nil && prec.status < 400 && pres.Status != "incomplete" && strings.TrimSpace(pres.summary()) != "":+ rec, res = prec, pres+ default:+ again := "no summary" + answeredWith(pres.Output)+ if perr != nil {+ again = perr.Error()+ } else if prec.status >= 400 {+ _, msg := compactFailure(body, prec)+ again = fmt.Sprintf("%d: %s", prec.status, msg)+ }+ log.Printf("codex compaction: %s asked again as text: %s; compacting locally", who, again)+ local = localSummary(body, who+" wrote no summary"+called+", asked again as text: "+again)+ }+ }var sum strings.Builderif local != "" {sum.WriteString(local)} else {- for _, o := range res.Output {- if o.Type != "message" {- continue- }- for _, c := range o.Content {- sum.WriteString(c.Text)- }- }+ sum.WriteString(res.summary())}if strings.TrimSpace(sum.String()) == "" {why := "the model wrote no summary" + answeredWith(res.Output)@@ -3207,6 +3231,26 @@ type compactResult struct {} `json:"error"`}+// summary is the text of a summary reply's messages.+func (res compactResult) summary() string {+ var b strings.Builder+ for _, o := range res.Output {+ if o.Type != "message" {+ continue+ }+ for _, c := range o.Content {+ b.WriteString(c.Text)+ }+ }+ return b.String()+}++// calledTool is whether a summary reply called a tool: a function_call,+// custom_tool_call, tool_search_call, a hosted tool's call.+func calledTool(out []compactOutput) bool {+ return slices.ContainsFunc(out, func(o compactOutput) bool { return strings.HasSuffix(o.Type, "_call") })+}+// answeredWith says what a summary reply without a summary held instead// — " (it answered with function_call shell)", " (it answered with// reasoning)", " (it answered with nothing)" — so the failure says whetherdiff --git a/internal/gateway/codex_compact_toolcall_test.go b/internal/gateway/codex_compact_toolcall_test.gonew file mode 100644index 000000000..78f0a266a--- /dev/null+++ b/internal/gateway/codex_compact_toolcall_test.go@@ -0,0 +1,207 @@+package gateway++import (+ "bytes"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync"+ "testing"++ "github.com/yetone/magpie/internal/provider"+)++// Lucy on Discord (v0.1.1676, Codex, group/auto-deepseek-v4-1-flash):+// "compaction: group/auto-deepseek-v4-1-flash failed to summarise this+// conversation 3 times in a row (last: the model wrote no summary (it+// answered with reasoning, function_call exec_command))". The summary+// request offered no tools, but its history was Codex's exchange of+// exec_command calls, and DeepSeek went on with it: it thought, then called+// exec_command again rather than writing the summary.+//+// The upstreams here answer as DeepSeek did, in each protocol's own shape:+// reasoning and an exec_command call while the history it is given holds+// calls, the summary only when it doesn't.++const compactExecRequest = `{"model":"MODEL","stream":true,"store":false,"tools":[],"tool_choice":"auto",+ "input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the failing test in pkg/calc"}]},+ {"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"run the tests"}]},+ {"type":"function_call","id":"fc_1","call_id":"call_1","name":"exec_command","arguments":"{\"cmd\":\"go test ./pkg/calc/...\"}"},+ {"type":"function_call_output","call_id":"call_1","output":"--- FAIL: TestAdd: Add(2,2) = 5, want 4"},+ {"type":"function_call","id":"fc_2","call_id":"call_2","name":"exec_command","arguments":"{\"cmd\":\"sed -n 1,40p pkg/calc/calc.go\"}"},+ {"type":"function_call_output","call_id":"call_2","output":"func Add(a, b int) int { return a + b + 1 }"},+ {"type":"compaction_trigger"}]}`++// deepseekCalls is DeepSeek's reply calling exec_command, as proto streams+// it; deepseekSummary its summary.+func deepseekCalls(proto provider.Protocol) string {+ switch proto {+ case provider.Chat:+ return sse(+ `data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-flash","choices":[{"index":0,"delta":{"role":"assistant","content":null,"reasoning_content":"The test still fails; run it again to check."},"finish_reason":null}]}`,+ `data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-flash","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_00_x1","type":"function","function":{"name":"exec_command","arguments":""}}]},"finish_reason":null}]}`,+ `data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-flash","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"cmd\":\"go test ./...\"}"}}]},"finish_reason":null}]}`,+ `data: {"id":"c1","object":"chat.completion.chunk","model":"deepseek-flash","choices":[{"index":0,"delta":{"content":""},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":900,"completion_tokens":40,"total_tokens":940}}`,+ `data: [DONE]`)+ case provider.Anthropic:+ return sse(+ `event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"deepseek-flash","content":[],"stop_reason":null,"usage":{"input_tokens":900,"output_tokens":0}}}`,+ `event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}`,+ `event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"The test still fails; run it again to check."}}`,+ `event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,+ `event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"call_00_x1","name":"exec_command","input":{}}}`,+ `event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"cmd\":\"go test ./...\"}"}}`,+ `event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":1}`,+ `event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":40}}`,+ `event: message_stop`+"\n"+`data: {"type":"message_stop"}`)+ }+ return sse(+ `data: {"type":"response.created","response":{"id":"resp_1","status":"in_progress","output":[]}}`,+ `data: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_x","summary":[{"type":"summary_text","text":"The test still fails; run it again to check."}]}}`,+ `data: {"type":"response.output_item.done","output_index":1,"item":{"type":"function_call","id":"fc_x","call_id":"call_00_x1","name":"exec_command","arguments":"{\"cmd\":\"go test ./...\"}","status":"completed"}}`,+ `data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","output":[],"usage":{"input_tokens":900,"output_tokens":40}}}`)+}++func deepseekSummary(proto provider.Protocol) string {+ switch proto {+ case provider.Chat:+ return sse(+ `data: {"id":"c2","object":"chat.completion.chunk","model":"deepseek-flash","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":"Summarise."},"finish_reason":null}]}`,+ `data: {"id":"c2","object":"chat.completion.chunk","model":"deepseek-flash","choices":[{"index":0,"delta":{"content":"SUMMARY: Add adds 1 too many"},"finish_reason":null}]}`,+ `data: {"id":"c2","object":"chat.completion.chunk","model":"deepseek-flash","choices":[{"index":0,"delta":{"content":""},"finish_reason":"stop"}],"usage":{"prompt_tokens":800,"completion_tokens":20,"total_tokens":820}}`,+ `data: [DONE]`)+ case provider.Anthropic:+ return sse(+ `event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"msg_2","type":"message","role":"assistant","model":"deepseek-flash","content":[],"stop_reason":null,"usage":{"input_tokens":800,"output_tokens":0}}}`,+ `event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,+ `event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"SUMMARY: Add adds 1 too many"}}`,+ `event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,+ `event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":20}}`,+ `event: message_stop`+"\n"+`data: {"type":"message_stop"}`)+ }+ return sse(+ `data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"SUMMARY: Add adds 1 too many"}]}}`,+ `data: {"type":"response.completed","response":{"id":"resp_2","status":"completed","output":[],"usage":{"input_tokens":800,"output_tokens":20}}}`)+}++// historyCalls is whether a request in proto carries an exchange of calls+// in its history, as that protocol writes one.+func historyCalls(proto provider.Protocol, b []byte) bool {+ switch proto {+ case provider.Chat:+ return bytes.Contains(b, []byte(`"tool_calls"`))+ case provider.Anthropic:+ return bytes.Contains(b, []byte(`"tool_use"`))+ }+ return bytes.Contains(b, []byte(`"function_call"`))+}++// deepseekUpstream serves proto at a test server as DeepSeek answered:+// calls while the history holds calls (always, when stubborn).+func deepseekUpstream(t *testing.T, proto provider.Protocol, stubborn bool) (*httptest.Server, func() [][]byte) {+ var mu sync.Mutex+ var got [][]byte+ up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ b, _ := io.ReadAll(r.Body)+ mu.Lock()+ got = append(got, b)+ mu.Unlock()+ w.Header().Set("Content-Type", "text/event-stream")+ if stubborn || historyCalls(proto, b) {+ io.WriteString(w, deepseekCalls(proto))+ return+ }+ io.WriteString(w, deepseekSummary(proto))+ }))+ t.Cleanup(up.Close)+ return up, func() [][]byte {+ mu.Lock()+ defer mu.Unlock()+ return append([][]byte(nil), got...)+ }+}++// saveDeepSeek saves a provider "ds" serving deepseek-flash over proto at+// up, and the group auto-ds of it.+func saveDeepSeek(t *testing.T, proto provider.Protocol, up string) {+ t.Helper()+ noRests()+ t.Cleanup(noRests)+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())+ t.Setenv("XDG_CACHE_HOME", t.TempDir())+ p := provider.Provider{ID: "ds", Name: "DS", Key: "k", Models: []string{"deepseek-flash"}}+ switch proto {+ case provider.Chat:+ p.Chat = up + "/v1"+ case provider.Responses:+ p.Responses = up + "/v1"+ case provider.Anthropic:+ p.Anthropic = up+ }+ if err := provider.Save(p); err != nil {+ t.Fatal(err)+ }+ if err := provider.SaveGroup(provider.Group{ID: "auto-ds", Name: "auto-ds", Members: []string{"ds/deepseek-flash"}, Routing: provider.Ordered}); err != nil {+ t.Fatal(err)+ }+}++var compactProtos = []provider.Protocol{provider.Chat, provider.Responses, provider.Anthropic}++func TestCodexCompactToolCallAsksAgainAsText(t *testing.T) {+ for _, proto := range compactProtos {+ for _, model := range []string{"group/auto-ds", "ds/deepseek-flash"} {+ t.Run(string(proto)+"/"+model, func(t *testing.T) {+ up, got := deepseekUpstream(t, proto, false)+ saveDeepSeek(t, proto, up.URL)+ code, body := codexPost(t, strings.Replace(compactExecRequest, "MODEL", model, 1))+ if code != 200 {+ t.Fatalf("%d %s", code, body)+ }+ if s := compactedText(t, body); s != "SUMMARY: Add adds 1 too many" {+ t.Errorf("summary %q", s)+ }+ asked := got()+ if len(asked) != 2 {+ t.Fatalf("asked %d times, not twice", len(asked))+ }+ // the first as Codex's conversation, the second as its text,+ // with every call and output in it and Codex's prompt after+ if !historyCalls(proto, asked[0]) {+ t.Errorf("the first summary request lost the calls: %s", asked[0])+ }+ again := string(asked[1])+ for _, want := range []string{"fix the failing test in pkg/calc", "exec_command", "go test ./pkg/calc/...", "Add(2,2) = 5, want 4", "a + b + 1", "CONTEXT CHECKPOINT COMPACTION"} {+ if !strings.Contains(again, want) {+ t.Errorf("the request asked again lacks %q: %s", want, again)+ }+ }+ })+ }+ }+}++// A model that calls a tool however it is asked: compacted without a+// model, with why, so Codex goes on instead of being held for minutes.+func TestCodexCompactToolCallEveryTimeCompactsLocally(t *testing.T) {+ for _, proto := range compactProtos {+ t.Run(string(proto), func(t *testing.T) {+ up, got := deepseekUpstream(t, proto, true)+ saveDeepSeek(t, proto, up.URL)+ code, body := codexPost(t, strings.Replace(compactExecRequest, "MODEL", "group/auto-ds", 1))+ if code != 200 {+ t.Fatalf("%d %s", code, body)+ }+ s := compactedText(t, body)+ for _, want := range []string{"without a model", "fix the failing test in pkg/calc", "function_call exec_command"} {+ if !strings.Contains(s, want) {+ t.Errorf("local summary lacks %q:\n%s", want, s)+ }+ }+ if n := len(got()); n != 2 {+ t.Errorf("asked %d times, not twice", n)+ }+ })+ }+}