Files
magpie/internal/provider/native_compaction_test.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

86 lines
3.1 KiB
Go

package provider
import (
"path/filepath"
"strings"
"testing"
)
// Codex's own compaction (#1099) is offered only where the wire carries
// it: a Responses API, a ChatGPT account, GitHub Copilot. Another agent's
// sign-in, a plugin's, and a provider without a Responses API can't be
// set to it; a model a provider asks on another API isn't compacted there.
func TestCarriesCompaction(t *testing.T) {
for _, tt := range []struct {
name string
p Provider
want bool
}{
{"a relay on Responses", Provider{Responses: "https://relay.example/v1"}, true},
{"chat completions only", Provider{Chat: "https://relay.example/v1"}, false},
{"anthropic only", Provider{Anthropic: "https://relay.example"}, false},
{"a ChatGPT account", Provider{Responses: "https://chatgpt.com/backend-api/codex", Account: &Account{Agent: "codex"}}, true},
{"GitHub Copilot", Provider{Chat: "https://api.githubcopilot.com", Responses: "https://api.githubcopilot.com", Account: &Account{Agent: "copilot"}}, true},
{"another agent's sign-in", Provider{Responses: "https://x.example/v1", Account: &Account{Agent: "workbuddy"}}, false},
{"a plugin's", Provider{Responses: "plugin://x", Account: &Account{Agent: "plugin:x"}}, false},
} {
if got := tt.p.CarriesCompaction(); got != tt.want {
t.Errorf("%s: %v, want %v", tt.name, got, tt.want)
}
}
on := Provider{Responses: "https://relay.example/v1", NativeCompaction: true}
if !on.CompactsOn("m") {
t.Error("a relay set to compact natively doesn't")
}
on.NativeCompaction = false
if on.CompactsOn("m") {
t.Error("a relay not set to compacts natively")
}
chat := Provider{Chat: "https://relay.example/v1", NativeCompaction: true}
if chat.CompactsOn("m") {
t.Error("a chat-only relay compacts natively")
}
}
// The option is saved, refused where the wire can't carry it, turned off
// again, and kept by a copy.
func TestSetNativeCompaction(t *testing.T) {
isolate(t)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
if err := Save(Provider{ID: "relay", Name: "Relay", Responses: "https://relay.example/v1", Key: "sk-1"}); err != nil {
t.Fatal(err)
}
if err := Save(Provider{ID: "chat", Name: "Chat", Chat: "https://chat.example/v1", Key: "sk-2"}); err != nil {
t.Fatal(err)
}
if err := SetNativeCompaction("relay", true); err != nil {
t.Fatal(err)
}
if p, _ := Find("relay"); !p.NativeCompaction {
t.Fatal("not saved")
}
if err := SetNativeCompaction("chat", true); err == nil || !strings.Contains(err.Error(), "can't carry") {
t.Fatalf("a chat-only provider took it: %v", err)
}
if p, _ := Find("chat"); p.NativeCompaction {
t.Fatal("saved on a chat-only provider")
}
id, err := AddCopy(Provider{Name: "Relay copy", Responses: "https://relay.example/v1", Models: []string{"m"}}, "relay")
if err != nil {
t.Fatal(err)
}
if c, _ := Find(id); !c.NativeCompaction {
t.Error("a copy lost it")
}
if err := SetNativeCompaction("relay", false); err != nil {
t.Fatal(err)
}
if p, _ := Find("relay"); p.NativeCompaction {
t.Fatal("not turned off")
}
}