magpie wrote `headers: {User-Agent: omp}` into omp's models.yml for every
omp. A newer omp sends `omp/<version>` itself, and the header put that
version out of sight.
From omp's own source: pi-ai 18.1.6 (CHANGELOG 2026-09-03, "Inference
requests now identify as omp by default") wraps fetch in
withInferenceUserAgent, later transportFetch, which sets User-Agent
omp/<VERSION> only when none is set. Real omp runs against a stand-in
gateway on mote, sandbox HOME, `bun add @oh-my-pi/pi-coding-agent@<v>`,
every API (Chat, Responses, Messages, Gemini):
16.3.5 and 18.1.5: Bun/1.4.2
18.1.6 and 18.4.4: omp/<version>
18.15+ needs Bun >= 1.4.4 (mote has 1.4.2); its source (18.15.0,
18.22.2) has the same transportFetch.
Now (ompUserAgent): no header from 18.1.6 on; omp/<installed version>
before; plain omp when the version isn't known. The version comes from
the installed package.json (InstalledVersion), never the network.
ompKept takes a User-Agent of magpie's own (omp, or omp/<version>) for
magpie's, so an old `User-Agent: omp` is replaced or removed on the next
sync, and any other value, under any casing, is the user's and stays
alone. usage.AgentOf cuts at "/", so omp, omp/16.3.5 and omp/18.4.4 all
count as omp; nothing reads omp's version from the UA.
Tests (internal/agent/omp_useragent_test.go): TestOmpUserAgentByVersion,
TestOmpOldMagpieUserAgentSynced, TestOmpUsersOwnUserAgentStays.
Broken by hand: origin's omp.go with a stub ompOwnUserAgent, all three
red; ompKept returning yamlKept's result unchanged, the last two red.
Real omp with the new header (mica, same stand-in): 16.3.5 and 18.1.5
with `User-Agent: omp/<v>` send omp/<v> on all four APIs.
Docs: docs/subsystems/agent-wiring.md row "omp's User-Agent"; site
omp.html en/zh/ja.
Run at this tree (base ed405d1d7, mica):
gofmt -l on changed files: nothing
go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go
vet ./...: ok
go build, GOOS=linux go build -tags nogui, GOOS=windows go build: ok
go test -count=1 -timeout 45m ./... (sandbox HOME, mote, at the pre-rebase head): every package ok but
gateway TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes (red on origin/main be400389b too, #7117) and gui
TestSetPortServesWhenTheOldOneWasTaken (TempDir cleanup race under load 48; 5 of 5 ok alone).
After the rebase onto 352b124b3..be400389b: internal/agent (45m timeout), fx, usage ok.
No GUI files touched; GUI suite not run.
Not tried: omp 18.15+ run for real (needs Bun >= 1.4.4); a WSL omp.
146 lines
4.8 KiB
Go
146 lines
4.8 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// An omp that names itself (omp/<version>, from 18.1.6) is left to it: a
|
|
// User-Agent of magpie's would put its version out of sight (Zyvaletta on
|
|
// X). An older one, which asks with Bun's, is named with its version, and
|
|
// one whose version isn't known plain omp; the gateway takes each for omp.
|
|
func TestOmpUserAgentByVersion(t *testing.T) {
|
|
for _, c := range []struct{ version, want string }{
|
|
{"16.3.5", "omp/16.3.5"},
|
|
{"18.1.5", "omp/18.1.5"},
|
|
{"18.1.6", ""},
|
|
{"18.4.4", ""},
|
|
{"18.22.2", ""},
|
|
{"", "omp"},
|
|
} {
|
|
e := ompProviderAt("http://127.0.0.1:3425", c.version)
|
|
got := e.Headers["User-Agent"]
|
|
if got != c.want || c.want == "" && len(e.Headers) > 0 {
|
|
t.Errorf("omp %q: headers %v, want User-Agent %q", c.version, e.Headers, c.want)
|
|
}
|
|
if c.want == "" {
|
|
continue
|
|
}
|
|
if usage.AgentOf(got) != "omp" {
|
|
t.Errorf("omp %q: %q is taken for %q", c.version, got, usage.AgentOf(got))
|
|
}
|
|
if !ompOwnUserAgent(got) {
|
|
t.Errorf("omp %q: magpie's own %q read back as the user's", c.version, got)
|
|
}
|
|
}
|
|
// what omp sends itself is omp's to the gateway too
|
|
if a := usage.AgentOf("omp/18.4.4"); a != "omp" {
|
|
t.Errorf("omp/18.4.4 is taken for %q", a)
|
|
}
|
|
}
|
|
|
|
// magpie's provider entry in a sandbox's models.yml after a sync, with the
|
|
// omp on PATH at version.
|
|
func ompSyncedHeaders(t *testing.T, version string) (map[string]string, string) {
|
|
t.Helper()
|
|
was := ompVersion
|
|
ompVersion = func() string { return version }
|
|
defer func() { ompVersion = was }()
|
|
home := os.Getenv("HOME")
|
|
if err := omp(home).Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ := os.ReadFile(filepath.Join(home, ".omp", "agent", "models.yml"))
|
|
var doc struct {
|
|
Providers map[string]struct {
|
|
Headers map[string]string `yaml:"headers"`
|
|
} `yaml:"providers"`
|
|
}
|
|
if err := yaml.Unmarshal(raw, &doc); err != nil {
|
|
t.Fatalf("%v\n%s", err, raw)
|
|
}
|
|
return doc.Providers[magpieID].Headers, string(raw)
|
|
}
|
|
|
|
func ompUASandbox(t *testing.T) string {
|
|
t.Helper()
|
|
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"))
|
|
for _, k := range []string{"PI_CODING_AGENT_DIR", "PI_CONFIG_DIR", "OMP_PROFILE", "PI_PROFILE"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
was := ompVersion
|
|
ompVersion = func() string { return "" }
|
|
defer func() { ompVersion = was }()
|
|
if err := omp(home).Field("model").Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return filepath.Join(home, ".omp", "agent", "models.yml")
|
|
}
|
|
|
|
// The User-Agent: omp an older magpie wrote is magpie's: the next sync puts
|
|
// the one it writes now in its place, none for an omp that names itself,
|
|
// and a header of the user's own beside it stays.
|
|
func TestOmpOldMagpieUserAgentSynced(t *testing.T) {
|
|
models := ompUASandbox(t)
|
|
if err := edit.SetYAML(models,
|
|
edit.KV{Path: "providers.magpie.headers.User-Agent", Value: "omp"},
|
|
edit.KV{Path: "providers.magpie.headers.X-Mine", Value: "1"},
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h, raw := ompSyncedHeaders(t, "18.9.1")
|
|
if _, ok := h["User-Agent"]; ok || h["X-Mine"] != "1" {
|
|
t.Fatalf("omp 18.9.1 names itself, yet:\n%s", raw)
|
|
}
|
|
h, raw = ompSyncedHeaders(t, "16.3.5")
|
|
if h["User-Agent"] != "omp/16.3.5" || h["X-Mine"] != "1" {
|
|
t.Fatalf("omp 16.3.5:\n%s", raw)
|
|
}
|
|
// magpie's omp/<version> is magpie's too: an omp updated takes its own
|
|
h, raw = ompSyncedHeaders(t, "18.1.5")
|
|
if h["User-Agent"] != "omp/18.1.5" {
|
|
t.Fatalf("omp 18.1.5 after 16.3.5:\n%s", raw)
|
|
}
|
|
h, raw = ompSyncedHeaders(t, "18.1.6")
|
|
if _, ok := h["User-Agent"]; ok || h["X-Mine"] != "1" {
|
|
t.Fatalf("omp 18.1.6 after 18.1.5:\n%s", raw)
|
|
}
|
|
h, raw = ompSyncedHeaders(t, "")
|
|
if h["User-Agent"] != "omp" {
|
|
t.Fatalf("an omp of no known version:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
// A User-Agent the user set on magpie's provider is theirs, under any
|
|
// casing: no sync writes magpie's over it or beside it, for an omp of any
|
|
// version.
|
|
func TestOmpUsersOwnUserAgentStays(t *testing.T) {
|
|
for _, key := range []string{"User-Agent", "user-agent"} {
|
|
t.Run(key, func(t *testing.T) {
|
|
models := ompUASandbox(t)
|
|
if err := edit.SetYAML(models, edit.KV{Path: "providers.magpie.headers." + key, Value: "my-proxy/2"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, v := range []string{"16.3.5", "18.9.1", ""} {
|
|
h, raw := ompSyncedHeaders(t, v)
|
|
if h[key] != "my-proxy/2" || len(h) != 1 {
|
|
t.Fatalf("omp %q: the user's User-Agent not kept alone:\n%s", v, raw)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|