Files
magpie/internal/provider/native_compaction.go
T
yetone 82f48f0935 gateway, provider, gui: a provider set to compact natively is asked Codex's compaction_trigger as sent and its sealed compaction goes back unchanged; a checkpoint stays with its writer (#1099)
A new per-provider option, off by default. With it on, Codex's
compaction_trigger reaches the provider as Codex sent it, and the
provider's encrypted compaction item goes back to Codex unchanged instead
of magpie's magpie1: summary. Off, nothing changes.

Where it can be on (provider.CarriesCompaction): a provider with a
Responses API (Sub2API and other OpenAI relays, a remote magpie), a
ChatGPT (codex) account, and the built-in GitHub Copilot on its GPT
Responses path. Copilot's Claude models are asked on chat completions, so
they still get magpie's summary (CompactsOn). A group compacts natively
only when every member does.

No silent downgrade:
- A turn carrying a sealed compaction goes only to the key or account that
  wrote it (compactions.json beside affinity.json, 0600, 1024 entries /
  30 days). With none left, Codex gets a 400 saying the conversation was
  compacted natively on <who>, not a summarised or dropped context.
- A provider whose request would have to be translated (not relayed) is
  refused with a 400 instead of losing the item.
- The upstream's own refusal goes back to Codex as it came, one call.
- base_instructions and Codex's identity are untouched.

Turning it on:
- GUI: the provider editor's Compaction row, "Codex compacts on this
  provider" (shown only where the wire can carry it; zh, zh-TW, ja, de).
- CLI: magpie provider set <id> compaction=native|magpie. A provider that
  can't carry it is refused.

Run on origin/main 00501401a, then rebased onto 84f14e555 (no file in
common) and reran gofmt, vet and go test for gateway, gui, provider,
cli, usage and sessions: all ok. Rebased again onto 8d2a4a7da (gateway.go
and codex_backend.go changed on both sides, merged clean) and reran
gofmt, vet (darwin, linux nogui, windows) and go test for gateway, cli,
provider and gui: all ok. Last rebased onto 7a3fed6bb (only
gateway-routing.md shared, merged clean): gofmt, vet on all three OSes,
and go test -run for the native compaction, sink, CORS, rebind and origin
tests in gateway plus the provider tests: ok.
- gofmt -l: clean. go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
  clean.
- set -o pipefail; go test -timeout 40m ./...: every package ok but
  internal/provider's TestCopilotSecretToolUsername (copilot_cli_test.go:93
  token "", 10.05s), the open load flake #1627; it passes alone -count=5
  and this change doesn't touch it.
- GUI (node --test, chromium + webkit): the provider-editor files, 946
  pass, 0 fail; the new provider-native-compaction.test.cjs runs 5
  languages at 1100 and 420 (60 cases).
- New Go tests: TestNativeCompactionPassesTheTrigger, ...RefusalGoesBack,
  ...StaysWithItsWriter, ...Copilot (gateway), TestCarriesCompaction,
  TestSetNativeCompaction (provider), TestProviderSaveNativeCompaction
  (gui), TestProviderCompactionPair (cli).

Broken by hand, each red, then restored:
1. codexInputAs dropping the trigger even when native: all 4 gateway
   tests fail.
2. checkpoint filter off: StaysWithItsWriter fails.
3. forced relay off: all 4 gateway tests fail (the web_search tool sends
   the turn to translation).
4. GUI save sending nativeCompaction false: the GUI test's first case
   fails.

Not tried: any real upstream (Sub2API, ChatGPT, Copilot). Copilot may
refuse a compacted history holding web_search items; that refusal would
go back to Codex as-is.
2026-10-15 05:20:17 +08:00

50 lines
1.8 KiB
Go

package provider
import (
"fmt"
"slices"
)
// CarriesCompaction says whether p's wire can carry Codex's own
// compaction (NativeCompaction, #1099): a compaction_trigger asked and a
// sealed compaction item given back, on the Responses API, as Codex sent
// them. A provider of no sign-in with a Responses API does (Sub2API and
// other relays of OpenAI's, OpenAI's own, a remote magpie), as do a
// ChatGPT account and GitHub Copilot, whose GPT models answer on
// Responses. Another signed-in agent's, or a plugin's, is asked in its own
// client's format, which has no such item.
func (p Provider) CarriesCompaction() bool {
if p.Account != nil {
return !p.IsPlugin() && (p.Account.Agent == "codex" || p.Account.Agent == "copilot") && p.Base(Responses) != ""
}
return p.Base(Responses) != ""
}
// CompactsOn says whether a Codex compaction for model at p goes to it as
// Codex sent it: the user turned NativeCompaction on, its wire carries
// it, and model is asked on Responses there (Copilot's Claude models are
// not). The gateway also asks whether the model fits Responses as it has
// seen it answer (Server.compactsNatively).
func (p Provider) CompactsOn(model string) bool {
if !p.NativeCompaction || !p.CarriesCompaction() {
return false
}
apis := p.APIs(model)
return apis == nil && !p.NamedOnlyOn(Responses) || slices.Contains(apis, Responses)
}
// SetNativeCompaction turns a provider's native compaction on or off
// (Provider.NativeCompaction). One whose wire can't carry it is refused
// turning it on.
func SetNativeCompaction(id string, on bool) error {
p, err := Find(id)
if err != nil {
return err
}
if on && !p.CarriesCompaction() {
return fmt.Errorf("%s can't carry Codex's native compaction: only a provider with a Responses API, a ChatGPT account or GitHub Copilot can", p.Name)
}
p.NativeCompaction = on
return Save(*p)
}