Files
magpie/internal/cli/providers_user_agent_cli_test.go
T
yetone abe7b00631 gateway: a key+URL provider is sent the agent's own User-Agent by default, through a remote magpie too
nb5p runs a gateway of their own behind magpie and saw nearly every
request arrive as "magpie/<version>". The agent's User-Agent now goes
on as the agent sent it, wherever magpie isn't itself the client.

What each path sends now:
- Key+URL providers (official vendor keys and custom OpenAI/Anthropic
  compatible ones), on chat, Anthropic, Responses, Gemini,
  count_tokens, embeddings, images and videos: the agent's User-Agent,
  as sent. Before, this was the opt-in passUserAgent (#1851) and the
  default was magpie's. The opt-out is magpieUserAgent:
  `magpie provider set <id> user-agent=magpie`, or untick "Send the
  agent's own User-Agent" in the editor. passUserAgent is no longer a
  field, so a file that set it true reads the same as the new default.
  A provider someone turned back off after opting in can't be told
  apart from one never touched, and now sends the agent's too.
- When the agent sent no User-Agent, and on magpie's own requests
  (classifier magpie-router/1, vision, search, the image tool, model
  lists): magpie's, unchanged.
- A header.User-Agent the user set: still wins over both.
- Subscription accounts (Claude, Codex, Copilot, Google, ZCode…),
  plugins, and OpenCode/Cline/Kilo: unchanged. Their signing sets the
  official client's UA (provider.PassesUserAgent still refuses an
  Account or OwnClient).
- Claude Code and Codex header copies: unchanged.
- Remote magpie: the near magpie still asks as magpie/<v>, because the
  far end trusts X-Magpie-Agent/Via only from that. It now adds
  X-Magpie-User-Agent (passOnUA, on forwardOnce, embeddings, draw and
  video; the classifier's decision request names none). The far end
  takes it only from a magpie/ request (callerFrom) and strips it on
  arrival (withCaller). agentUA/callerUA then hand it to userAgentFor,
  so the far vendor sees the agent's. A client that writes the header
  itself, straight to a magpie, is ignored. magpie's own UAs are never
  relayed (magpiesOwn).

Vendor check: invalid-key requests with magpie/, claude-cli, a Chrome
UA and pi's went to Anthropic, OpenAI, Gemini, DeepSeek, Moonshot, Kimi
For Coding, Zhipu, OpenRouter, xAI, DashScope, MiniMax, Volcengine Ark,
Groq and Mistral. Every vendor gave the same auth error for every UA,
so no edge-level UA gating was seen. Kimi For Coding's agent allow-list
sits behind a valid key, which wasn't tried.

Run (mote):
- go test -run 'UserAgent|RemoteMagpie|ZCode|Blocked' ./internal/gateway: ok
- go test ./internal/provider ./internal/cli ./internal/gui: ok
- node --test provider-user-agent, provider-native-compaction and
  i18n-keys (chromium and webkit; en/zh/zh-TW/ja/de; 900 and 440 wide):
  123 of 124 pass. The red is i18n-keys "a plugin's words are its own"
  (plugins' words said in files this change doesn't touch), already
  filed as #5327/#5332, #5426 and #5482.
- gofmt -l clean; go vet; GOOS=linux -tags nogui and GOOS=windows vet:
  ok at the rebased head (on f13d5f16f). Targeted gateway, provider,
  cli and gui packages (UserAgent|RemoteMagpie|ZCode|Blocked|Budget)
  ok there too; whole packages ran at the pre-rebase base 4461957b9.

Broken by hand:
- PassesUserAgent back to opt-in: TestUserAgentPassedOn/default,
  TestUserAgentFor, TestRemoteMagpieRelaysUserAgent, TestPassesUserAgent
  and TestProviderUserAgentPair go red.
- agentUA's relay off, or passOnUA a no-op: TestRemoteMagpieRelaysUserAgent
  goes red; the far vendor is asked as magpie/dev.
- withCaller's Del(X-Magpie-User-Agent) removed: stays green. Nothing
  copies incoming headers to a vendor today, so the Del only guards
  against that.

Not tried: a real remote magpie on two machines, a real valid-key vendor
request, and nb5p's own gateway.

Reported-by: nb5p on Discord
2026-11-07 17:44:31 +08:00

57 lines
2.7 KiB
Go

package cli
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A key+URL provider passes the agent's User-Agent on by default (#1851,
// nb5p on Discord): user-agent=magpie sends magpie's in its place,
// user-agent=agent goes back to the default, and a signed-in account, which
// goes as its own client, is refused agent.
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=magpie"}); err != nil || !p.MagpieUserAgent || p.PassesUserAgent() {
t.Fatalf("magpie: %v %v", p.MagpieUserAgent, err)
}
if err := applyPairs(&p, []string{"user-agent=agent"}); err != nil || p.MagpieUserAgent || !p.PassesUserAgent() {
t.Fatalf("agent: %v %v", p.MagpieUserAgent, 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.MagpieUserAgent {
t.Fatalf("an account took it: %v", err)
}
if err := applyPairs(&a, []string{"user-agent=magpie"}); err != nil || a.MagpieUserAgent {
t.Fatalf("an account turning it off: %v %v", a.MagpieUserAgent, 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)
}
}