Files
magpie/internal/provider/user_agent_test.go
T
yetone 248a68598c gateway, provider, gui, cli: a key+URL provider may be sent the agent's own User-Agent in place of magpie's (#1851)
Every forward went with User-Agent magpie/<version>. A coding plan that
serves only the agents it names (pi, OpenCode, Claude Code…) knows them by
their User-Agent, so it turned magpie away.

What changed
- provider: PassUserAgent (passUserAgent in providers.json), off by
  default. PassesUserAgent takes it only for a key+URL provider: never a
  signed-in account, a plugin's, or a vendor whose own client magpie goes
  as (OwnClient: OpenCode, Cline, Kilo, another magpie). A copy keeps it.
- gateway: userAgentFor in forwardOnce (chat, Anthropic, Responses,
  Gemini, count_tokens) sends the inbound User-Agent unchanged where the
  provider passes it on. It sends magpie's where it doesn't, where the
  agent sent none, and on magpie's own requests (magpiesOwn: magpie/…,
  magpie-router/vision/search, the image tool's "<client> magpie-image/1",
  which is built from MCP clientInfo, not a User-Agent). magpie never
  invents another client's. The claude-cli/Codex header copies, the
  OpenCode/Cline/Kilo overrides and account signing run after it as
  before, and the user's header.User-Agent is put on at signing, so it
  wins.
- gateway: embeddings, images and videos (retrieveFrom, sendWith,
  remoteVideoContent) now stamp the same for a key+URL provider through
  stampUA, from the caller's User-Agent kept on the request ctx
  (caller.ua, withCaller). Before, they went with Go's default
  (Go-http-client/1.1). Account and remote-magpie paths are untouched.
- cli: `magpie provider set <id> user-agent=agent|magpie`, refused for a
  signed-in account; `magpie provider <id>` shows it.
- gui: the provider editor's User-Agent row, "Send the agent's own
  User-Agent", with an explanation, for a provider with a key only (the
  JSON says ownClient for presets and providers); en/zh/zh-TW/ja/de.
  The TUI only adds presets, so it has no field for this.
- docs: reference.md (provider set) and subsystems/gateway-routing.md.

What was run
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and
  GOOS=windows.
- go test -count=1 ./internal/gateway ./internal/provider ./internal/cli
  ./internal/gui: all ok (sandbox HOME, private GOCACHE).
- TestUserAgentPassedOn covers 7 routes (messages, count_tokens, chat,
  responses, gemini, embeddings, images) x off / on / on with no UA / on
  with magpie-router/1 / on with the image tool's UA / the user's header
  with it on and off. TestUserAgentAccountUntouched: a Factory account
  with passUserAgent in providers.json is still asked as factory-cli/.
  Also TestUserAgentClaudeCodeAsBefore, TestUserAgentFor,
  TestPassesUserAgent, TestAddCopyPassUserAgent, TestProviderUserAgentPair.
- Broken by hand, each went red: forwardOnce back to magpie's UA (5
  routes red); stampUA removed from embeddings/draw (both red, seen as
  Go-http-client/1.1); PassesUserAgent ignoring accounts/OwnClient;
  magpiesOwn dropped (the internal UAs leaked on all 7 routes); the GUI
  row shown for OpenCode and Save not posting it.
- provider-user-agent.test.cjs: 60/60 in chromium and webkit, en, zh,
  zh-TW, ja, de, at 900 and 440 wide. The provider/editor/add/i18n GUI
  tests in chromium: 244/244.

Not run
- Not tried against a real coding plan that checks User-Agent, nor with a
  real agent through a live gateway. Videos have no test of their own
  (they go through sendWith, which the images test covers).
- The GUI suite was not run in full; the Windows box was not used.
2026-10-13 02:56:34 +08:00

57 lines
2.1 KiB
Go

package provider
import (
"path/filepath"
"testing"
)
// Only a key+URL provider passes the agent's User-Agent on (#1851): a
// signed-in account, a plugin's, and one that goes as a client of its
// vendor's own (OpenCode, Cline, Kilo, another magpie) keep the identity
// their signing gives, set or not.
func TestPassesUserAgent(t *testing.T) {
for _, tt := range []struct {
name string
p Provider
want bool
}{
{"key", Provider{PassUserAgent: true, Chat: "https://api.example.com/v1"}, true},
{"key, off", Provider{Chat: "https://api.example.com/v1"}, false},
{"an account", Provider{PassUserAgent: true, Chat: "https://api.z.ai/api/coding/paas/v4", Account: &Account{Agent: "zcode"}}, false},
{"a plugin's", Provider{PassUserAgent: true, Chat: "https://x.example/v1", Account: &Account{Agent: "plugin:x"}}, false},
{"opencode", Provider{PassUserAgent: true, Chat: "https://opencode.ai/zen/v1"}, false},
{"cline", Provider{PassUserAgent: true, Preset: "clinepass", Chat: "https://api.cline.bot/api/v1"}, false},
{"kilo", Provider{PassUserAgent: true, Preset: "kilo", Chat: "https://api.kilo.ai/api/gateway"}, false},
{"another magpie", Provider{PassUserAgent: true, Preset: RemoteMagpiePreset, Chat: "http://10.0.0.2:3425/v1"}, false},
} {
if got := tt.p.PassesUserAgent(); got != tt.want {
t.Errorf("%s: %v, want %v", tt.name, got, tt.want)
}
}
}
// A copy of a provider that passes the agent's User-Agent on passes it on
// too, the form not carrying it.
func TestAddCopyPassUserAgent(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: "plan", Name: "Plan", Chat: "https://plan.example/v1", Key: "sk-1", PassUserAgent: true}); err != nil {
t.Fatal(err)
}
id, err := AddCopy(Provider{Name: "Plan copy", Chat: "https://plan.example/v1", Models: []string{"m"}}, "plan")
if err != nil {
t.Fatal(err)
}
c, err := Find(id)
if err != nil {
t.Fatal(err)
}
if !c.PassUserAgent {
t.Fatal("the copy goes as magpie")
}
}