Files
magpie/internal/gui/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

71 lines
2.5 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The editor's Compaction row (#1099): a Save that sends nativeCompaction
// sets it, one that leaves it out keeps it, and a provider without a
// Responses API is refused it. /api/providers says which providers can
// carry it, for the editor to offer the row.
func TestProviderSaveNativeCompaction(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(body string) *httptest.ResponseRecorder {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body)))
return w
}
on := func(id string) bool {
t.Helper()
p, err := provider.Find(id)
if err != nil {
t.Fatal(err)
}
return p.NativeCompaction
}
if w := post(`{"id":"relay","name":"Relay","key":"sk-r","responses":"http://127.0.0.1:1/v1","icon":"generic","new":true,"nativeCompaction":true}`); w.Code != 200 || !on("relay") {
t.Fatalf("new, on: %d %s", w.Code, w.Body)
}
if w := post(`{"id":"relay","from":"relay","name":"Relay","key":"","responses":"http://127.0.0.1:1/v1","icon":"generic","models":[],"headers":{}}`); w.Code != 200 || !on("relay") {
t.Fatalf("a save without it: %d %s", w.Code, w.Body)
}
if w := post(`{"id":"relay","from":"relay","name":"Relay","key":"","responses":"http://127.0.0.1:1/v1","icon":"generic","models":[],"headers":{},"nativeCompaction":false}`); w.Code != 200 || on("relay") {
t.Fatalf("off: %d %s", w.Code, w.Body)
}
if w := post(`{"id":"chat","name":"Chat","key":"sk-c","chat":"http://127.0.0.1:1/v1","icon":"generic","new":true,"nativeCompaction":true}`); w.Code == 200 || !strings.Contains(w.Body.String(), "can't carry") {
t.Fatalf("a chat-only provider took it: %d %s", w.Code, w.Body)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/providers", nil))
var got struct {
Providers []struct {
ID string `json:"id"`
CarriesCompaction bool `json:"carriesCompaction"`
} `json:"providers"`
}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatalf("%v %s", err, w.Body)
}
carries := map[string]bool{}
for _, p := range got.Providers {
carries[p.ID] = p.CarriesCompaction
}
if !carries["relay"] {
t.Errorf("the Responses relay isn't said to carry it: %s", w.Body)
}
}