gateway: a native compaction a fallback provider seals is never remembered, so the next turn answers 200 without the compacted history
What's wrong
Native compaction (#1099, the provider editor's Compaction: native, shipped in v0.1.1242) keeps Codex's compaction_trigger on the wire when the asked provider is set to it (nativeCompactionFor), and any candidate whose wire carries compaction may be asked it — a fallback included. When a fallback (a provider whose own setting is off, but whose Responses wire carries compaction) answers the trigger with a sealed compaction item, magpie relays the seal to Codex but never records who sealed it: the writer is only recorded when the candidate that answered has the setting itself (internal/gateway/gateway.go, the compaction watcher's defer: if s.compactsNatively(c.p, c.model) { sealedBy = &c }). compactions.json stays empty.
The next turn Codex sends carries that seal, and then one of two things happens:
- to another provider (the user switched models, or the conversation moved):
checkpointOffinds nothing, so the request is not pinned to its writer; the provider refuses the foreign sealed item, magpie matches the refusal as foreign reasoning, and becausecheckpoint == nilit takes the compaction out (withoutCompaction) and answers 200 — the conversation goes on without its earlier context, unasked; - to the same chain: the request is translated because
keepsCompactionkeeps a sealed compaction only for a provider set to compact natively — and the translation leaves thecompactionitem out. The fallback that sealed it receives only the latest user message, and Codex gets 200.
Both are exactly what the feature's own contract rules out: "the compaction is never sent to another to be refused, dropped (withoutCompaction), or summarised in its place, which would lose the conversation's earlier context unasked" (internal/gateway/native_compaction.go doc comment, #1099).
Why this is exceptionally important
- What is lost, and how quietly: the conversation's compacted history. Codex only carries the sealed item; once magpie drops it, the earlier context is gone from that thread for good, and the turn is answered 200 with no error and nothing in the UI. This is the data-loss class the feature was built to prevent, and the failure is invisible until the model starts answering without the compacted context.
- Who is hit: anyone using the new option on a provider with a fallback whose wire carries compaction, from the moment the primary key fails inside a session (down, rate-limited, 5xx) — which is exactly the situation fallbacks exist for. A fallback that is a second ChatGPT/Copilot account or any Responses relay qualifies.
- The feature is one day old and still being adopted:
82f48f09(2026-10-15) landed in v0.1.1242; v0.1.1242–v0.1.1244 (current) carry the hole, so the first wave of users of this option can lose history before they have a reason to distrust it. - It defeats the maintainer-reviewed invariant at its core: #1099's whole design is "only the key or account that sealed it reads it, and it is never silently dropped"; recording only native-set answers is an oversight in that recording, not a design choice.
Minimal reproduction (commit-level; branch below carries it as TestNativeCompactionFallbackSealStaysWithItsWriter)
Three fakes, no real upstream: fa/m1 set Compaction: native with Fallback: [fb/m1] and answering 500; fb/m1 (native off, Responses wire) that seals the trigger with nativeSealedReply; a third provider to continue on.
- Codex sends a compaction turn to
fa/m1(the trigger). Primary 500s,fbis asked the trigger as Codex sent it, and seals it: 200, the seal relayed to Codex. - Codex's next turn carries the seal.
Expected: the writer is remembered, the next turn goes only to fb as it came (as it does for a provider set to compact natively), and any other provider is answered 400 naming the writer. Actual: nothing is recorded; the same chain is sent the turn without the seal and answered 200, and another provider is sent the seal, refuses it, and the turn is still answered 200 without it.
Raw output, before (fix stashed, test in place):
=== RUN TestNativeCompactionFallbackSealStaysWithItsWriter
native_compaction_test.go:175: the writer: 200 ... fb got {"input":[{"content":[{"text":"go on","type":"input_text"}],"role":"user","type":"message"}],"model":"m1","store":false,"stream":true}
--- FAIL: TestNativeCompactionFallbackSealStaysWithItsWriter (15.10s)
Raw output, after (branch head, probe against the same three fakes):
zz_probe_test.go:63: compactions.json (recorded=true): {"668fa3aa12f69f87f1c18b690e50391fc5d69c9bc3ab502968181e773fc4f207":{"who":"fb#74fec0c33e","provider":"fb","model":"m1","at":"..."}}
zz_probe_test.go:71: continuing elsewhere: 400 {"error":{"code":null,"message":"This conversation was compacted natively by fb#74fec0c33e, and only that key or account can read the compaction it sealed. ..."} } (the other provider was sent [])
zz_probe_test.go:78: chain next turn: 200
zz_probe_test.go:81: fb got (seal kept=true): {"model":"m1","stream":true,"tools":[{"type":"web_search"}],"input":[{"type":"compaction","id":"cmp_1","encrypted_content":"gAAAAB-sealed-by-fb"},{"type":"message","role":"user","content":[{"type":"input_text","text":"go on"}]}]}
--- PASS: TestProbeFallbackSealRemembered (4.32s)
The change, since this account cannot open a pull request on this repo (README: "This repository doesn't take pull requests"; gh pr create --repo yetone/magpie --base main --head JasonBuildAI:fix/q7k3v-fallback-compaction-seal answers "JasonBuildAI does not have the correct permissions to execute CreatePullRequest (createPullRequest)"): branch fix/q7k3v-fallback-compaction-seal in the fork JasonBuildAI/magpie, commit 33cd8eb9 on main at bc470c6b:
git fetch https://github.com/JasonBuildAI/magpie fix/q7k3v-fallback-compaction-seal && git cherry-pick FETCH_HEAD
internal/gateway/gateway.go: the writer is recorded when the request's compaction was asked natively (nativeRoute, from the samenativeCompactionForthe entry point used to keep the trigger), whoever answered it — what answered sealed it, and only that key or account can read it.internal/gateway/native_compaction.go:keepsCompactionkeeps a body whose seal is recorded to this provider (checkpointOf), whatever its own setting — the request routed to the writer is its own to read, and a translation would drop the compacted history unasked.checkpointErrorno longer opens with "native compaction is on for", which isn't true of a fallback. internal/gateway/native_compaction_test.go:TestNativeCompactionFallbackSealStaysWithItsWriter(the reproduction above; the existingTestNativeCompaction*still pass).docs/subsystems/gateway-routing.md: the native compaction entry now says the writer is recorded whoever answered, and that a seal goes as it came to the one that sealed it.
Ran on the branch: the new test fails on main with the two source files stashed and passes with them; gofmt -l internal/gateway clean; go vet ./internal/gateway clean; TestNativeCompaction* (all five), TestForeignCompactionToGrok, TestCodexCompactsThroughXAI, TestCodexCompactsThroughCodexRelay, TestCodexCompactionTriggerOnV1, TestCodexOwnModelCompactionPassesThrough, TestCodexCompactItemNotFoundAsksAgainAsText pass; the package's compaction, codex-compact, foreign-seal, compacting and gateway-key tests together pass. Not tried against a real upstream.
AI disclosure: reported with AI assistance at my request, under my direction and review.
Took this one (state was OPEN with no claim; git log --grep=2271 shows no fix on main as of 707fbb24). Confirmed the mechanism by reading the code before changing anything. Since the repo doesn't take pull requests, the fix is on a branch of my fork for cherry-picking:
git fetch https://github.com/Niko-Wu/magpie fix/2271 && git cherry-pick ac0bbb3e
(based on current 707fbb24, single commit, no conflict surface)
Root cause — when a group falls back and a different provider seals a native compaction, the seal is never recorded as the compaction's writer, so the next turn answers 200 without the compacted history:
internal/gateway/gateway.go:3114recorded the writer only when the answering candidate itself had native compaction on. A fallback seal wrote nothing tocompactions.json.keepsCompactionthen refused to forward the seal, andcheckpointErrorproduced the "native compaction is on for" error that doesn't hold for a fallback provider.
The fix
gateway.go:2153-2160— a seal made for a turn that asked for native compaction records the writer no matter which provider answered (nativeRoute = s.nativeCompactionFor(asked)).gateway.go:3119—sealedBynow also matches on that route.native_compaction.go:110-126—keepsCompactionforwards the seal when the writer is the provider it was sealed for, regardless of that provider's own setting.native_compaction.go:304-309—checkpointErrorno longer claims "native compaction is on for", which is false for a fallback. docs/subsystems/gateway-routing.md:133— the compaction entry now says the writer is recorded whoever answers.
Test — new TestNativeCompactionFallbackSealStaysWithItsWriter (native_compaction_test.go:140-194): fa/m1 with native compaction on answers 500, fb/m1 seals the compaction, a third provider serves the next turn. Red on main with the failure matching the report verbatim (the writer: 200 ... fb got {"input":[{"content":[{"text":"go on"...), green with the fix.
Verification
go test -tags nogui -count=1 -timeout 45m ./internal/gateway/→ok github.com/yetone/magpie/internal/gateway 400.880s(full package, on the rebased head)GOOS=windows go vet -tags nogui ./internal/gateway/→ cleanGOOS=linux go vet -tags nogui ./internal/gateway/→ cleangofmt -lon changed files → empty