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

56 lines
2.5 KiB
Go

package cli
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// user-agent=agent has a key+URL provider pass the agent's User-Agent on
// (#1851), user-agent=magpie goes back to magpie's, and a signed-in
// account, which goes as its own client, is refused it.
func TestProviderUserAgentPair(t *testing.T) {
p := provider.Provider{ID: "plan", Name: "Plan", Chat: "https://plan.example/v1", Key: "k"}
if err := applyPairs(&p, []string{"user-agent=agent"}); err != nil || !p.PassUserAgent {
t.Fatalf("agent: %v %v", p.PassUserAgent, err)
}
if err := applyPairs(&p, []string{"user-agent=magpie"}); err != nil || p.PassUserAgent {
t.Fatalf("magpie: %v %v", p.PassUserAgent, err)
}
if err := applyPairs(&p, []string{"user-agent=claude"}); err == nil || !strings.Contains(err.Error(), "agent|magpie") {
t.Fatalf("a made-up value: %v", err)
}
a := provider.Provider{ID: "zcode", Name: "ZCode", Account: &provider.Account{Agent: "zcode"}}
if err := applyPairs(&a, []string{"user-agent=agent"}); err == nil || a.PassUserAgent {
t.Fatalf("an account took it: %v", err)
}
if err := applyPairs(&a, []string{"user-agent=magpie"}); err != nil {
t.Fatalf("an account turning it off: %v", err)
}
}
// compaction=native has a provider whose wire carries it be asked Codex's
// own compaction (#1099), compaction=magpie goes back to magpie's summary,
// and a provider without a Responses API is refused it.
func TestProviderCompactionPair(t *testing.T) {
p := provider.Provider{ID: "relay", Name: "Relay", Responses: "https://relay.example/v1", Key: "k"}
if err := applyPairs(&p, []string{"compaction=native"}); err != nil || !p.NativeCompaction {
t.Fatalf("native: %v %v", p.NativeCompaction, err)
}
if err := applyPairs(&p, []string{"compaction=magpie"}); err != nil || p.NativeCompaction {
t.Fatalf("magpie: %v %v", p.NativeCompaction, err)
}
if err := applyPairs(&p, []string{"compaction=sometimes"}); err == nil || !strings.Contains(err.Error(), "native|magpie") {
t.Fatalf("a made-up value: %v", err)
}
c := provider.Provider{ID: "chat", Name: "Chat", Chat: "https://chat.example/v1", Key: "k"}
if err := applyPairs(&c, []string{"compaction=native"}); err == nil {
t.Fatalf("a chat-only provider took it")
}
cp := provider.Provider{ID: "copilot", Name: "GitHub Copilot", Responses: "https://api.githubcopilot.com", Account: &provider.Account{Agent: "copilot"}}
if err := applyPairs(&cp, []string{"compaction=native"}); err != nil || !cp.NativeCompaction {
t.Fatalf("copilot: %v %v", cp.NativeCompaction, err)
}
}