Files
magpie/internal/agent/lan_kept_test.go
T
yetone 78b3f1f21a agent: an agent pointed at magpie on another machine keeps that address and key through a start's sync (coeo91 on Discord)
coeo91: after an update or a restart, the base URL in an agent's config
sometimes went from the LAN address back to 127.0.0.1.

Every start of magpie (and a provider added, a catalog fetched) runs
SyncCatalog, which runs each agent's Sync to bring magpie's models in
its files up to date. Each Sync writes magpie's whole provider block,
base URL and key included, from gateway.URL(), so a block the user had
pointed at a magpie on another machine (a NAS's, or this machine's LAN
address) went back to 127.0.0.1 with magpie's own key. Only ZCode
(zcodeAddress), WorkBuddy (buddyWriteAt) and Codex (#816) kept such an
address; in a sandbox HOME, 27 files of the other agents went back
(Qoder, OpenCode, Pi, Crush, Cline, Qwen Code, Kimi, Grok, Hermes, omp,
MiniMax, Snow, Hanako, VS Code, Copilot JetBrains, Goose, dsh, ...).

The fix is one place for all of them: SyncCatalog runs each Sync
through synced, which passes every file written through keptAway
(edit.Filtered, a filter on edit.WriteAtomic while it runs). In a file
without the gateway's own address, with an address on another machine
and none a WSL distro was seen at, a write that brings gateway.URL() in
place of that address keeps the address, and one that brings magpie's
own key (gateway.Token, TokenFor, the LAN key, "Bearer " too) in place
of another key keeps that key. An address on this machine's loopback
(another port, localhost) still follows the gateway, a WSL distro's
way to Windows is still magpie's to move, magpie's own files are never
filtered, and with two addresses or keys gone the write is left as it
is. A pick made in magpie still points the agent at this magpie.

Tests:
- TestSyncKeepsAnAddressOnAnotherMachine wires every agent in a sandbox
  HOME, points each file at 192.168.50.10 with a key of its own, adds a
  provider and runs SyncCatalog: no file is back on 127.0.0.1, the
  address and key are kept, and the new model is written. With
  SyncCatalog calling a.Sync() directly it fails on 27 files.
- TestKeptAwayOnlyWhatIsSure: old port, localhost, WSL and two-gone
  cases follow the gateway; a NAS beside a local Ollama is kept; Qwen
  Code's key written on its own is kept.
- TestKeyValuesFormats, TestFilteredWrites.

Run: gofmt -l (clean); GOOS=linux and GOOS=windows go vet -tags nogui
./internal/agent ./internal/edit; go test -tags nogui ./... in a temp HOME.
2026-10-11 00:59:45 +08:00

173 lines
6.8 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// An agent the user pointed at magpie on another machine (coeo91 on
// Discord: the LAN address, which went back to 127.0.0.1 after an update or
// a restart) keeps that address and the key it had there through the sync
// every start of magpie runs (SyncCatalog), for every agent magpie writes
// its models into; the sync still brings the models up to date.
func TestSyncKeepsAnAddressOnAnotherMachine(t *testing.T) {
t.Setenv("MAGPIE_ADDR", "")
home, _ := codexHome(t, "", "")
managed := claudeManaged
claudeManaged = func() string { return filepath.Join(home, "managed-settings.json") }
t.Cleanup(func() { claudeManaged = managed })
os.MkdirAll(filepath.Join(home, ".hanako", "agents", "hana"), 0o755)
os.WriteFile(filepath.Join(home, ".hanako", "agents", "hana", "config.yaml"), []byte("agent:\n name: Hana\n"), 0o644)
t.Setenv("ASTRBOT_ROOT", "")
writeFile(t, filepath.Join(home, ".astrbot", "data", "cmd_config.json"), astrbotConfig)
setPort(t, 3591)
for _, a := range All() {
// Alma is told through its running app, and Cursor's local
// gateway is started by launchctl: neither is in the sandbox
if a.Check == nil || a.Launch != nil || a.WSL != "" || a.Native != nil || a.ID == "alma" || a.ID == "cursorlocal" {
continue
}
var key, want string
for _, g := range a.Fields {
for _, o := range g.Options(a.Values()) {
if want == "" && (o.Ref == "fake/m1" || o.Value == magpieID) {
key, want = g.Key, o.Value
}
}
}
if want == "" {
t.Fatalf("%s: no field takes magpie's models", a.ID)
}
if err := a.Apply(key, want); err != nil {
t.Fatalf("%s: %v", a.ID, err)
}
}
// the user points each file at the NAS's magpie, with its key
moved := filesWith(home, "127.0.0.1:3591")
if len(moved) < 20 {
t.Fatalf("only %d files have the gateway's address: %v", len(moved), moved)
}
keys := map[string]int{}
for _, p := range moved {
b, _ := os.ReadFile(p)
b = []byte(strings.ReplaceAll(string(b), "127.0.0.1:3591", "192.168.50.10:3591"))
kvs := keyValues(b)
for i := len(kvs) - 1; i >= 0; i-- {
if kv := kvs[i]; ourToken(kv.value) {
v := "nas-key"
if strings.HasPrefix(kv.value, "Bearer ") {
v = "Bearer nas-key"
}
b = append(b[:kv.start:kv.start], append([]byte(v), b[kv.end:]...)...)
}
}
keys[p] = strings.Count(string(b), "nas-key")
os.WriteFile(p, b, 0o644)
}
if err := provider.Save(provider.Provider{ID: "fake2", Name: "Fake2", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m2"}}); err != nil {
t.Fatal(err)
}
SyncCatalog()
synced := 0
for _, p := range moved {
b, _ := os.ReadFile(p)
s, name := string(b), strings.TrimPrefix(p, home)
if strings.Contains(s, "m2") {
synced++
}
if strings.Contains(s, "127.0.0.1:3591") {
t.Errorf("%s is back on this machine's gateway", name)
}
if !strings.Contains(s, "192.168.50.10:3591") {
t.Errorf("%s lost the NAS's address", name)
}
if n := strings.Count(s, "nas-key"); n < keys[p] {
t.Errorf("%s has the NAS's key %d times, not %d", name, n, keys[p])
}
for _, kv := range keyValues(b) {
if ourToken(kv.value) {
t.Errorf("%s has magpie's own key back: %q", name, kv.value)
}
}
}
if synced < len(moved)/2 {
t.Errorf("the sync wrote the new model into %d of %d files", synced, len(moved))
}
}
// What the sync writes is left as it is where the file says nothing sure of
// another machine: the gateway's address on this machine (another port, or
// localhost) follows the gateway, as does one a WSL distro reached Windows
// at; with two addresses gone, which went where can't be told.
func TestKeptAwayOnlyWhatIsSure(t *testing.T) {
syncHome(t)
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3591")
dir := t.TempDir()
now := `{"p":{"baseUrl":"http://127.0.0.1:3591/v1","apiKey":"magpie"}}`
for _, c := range []struct{ name, was, want string }{
{"nas", `{"p":{"baseUrl":"http://192.168.1.20:3425/v1","apiKey":"nas-key"}}`,
`{"p":{"baseUrl":"http://192.168.1.20:3425/v1","apiKey":"nas-key"}}`},
{"nas, magpie's key", `{"p":{"baseUrl":"http://nas.local:3425/v1","apiKey":"magpie"}}`,
`{"p":{"baseUrl":"http://nas.local:3425/v1","apiKey":"magpie"}}`},
{"old port", `{"p":{"baseUrl":"http://127.0.0.1:3425/v1","apiKey":"magpie"}}`, now},
{"localhost", `{"p":{"baseUrl":"http://localhost:3591/v1","apiKey":"other"}}`, now},
{"two gone", `{"p":{"baseUrl":"http://192.168.1.20:3425/v1","apiKey":"a"},"q":{"baseUrl":"http://192.168.1.21:3425/v1"}}`, now},
{"wsl", `{"p":{"baseUrl":"http://172.29.0.1:3591/v1","apiKey":"lan"}}`, now},
} {
wsl.Lock()
wsl.seen = map[string]*distro{"Ubuntu": {Gateway: "172.30.0.1", Was: []string{"172.29.0.1"}}}
wsl.Unlock()
p := filepath.Join(dir, strings.ReplaceAll(c.name, " ", "-")+".json")
os.WriteFile(p, []byte(c.was), 0o644)
if got := string(keptAway(p, []byte(now))); got != c.want {
t.Errorf("%s: %s, want %s", c.name, got, c.want)
}
}
// a model of the user's own on this machine (Ollama's) is no sign of
// magpie here
p := filepath.Join(dir, "ollama.json")
os.WriteFile(p, []byte(`{"o":{"baseUrl":"http://localhost:11434/v1"},"p":{"baseUrl":"http://192.168.1.20:3425/v1","apiKey":"nas-key"}}`), 0o644)
if got, want := string(keptAway(p, []byte(`{"o":{"baseUrl":"http://localhost:11434/v1"},"p":{"baseUrl":"http://127.0.0.1:3591/v1","apiKey":"magpie"}}`))),
`{"o":{"baseUrl":"http://localhost:11434/v1"},"p":{"baseUrl":"http://192.168.1.20:3425/v1","apiKey":"nas-key"}}`; got != want {
t.Errorf("beside Ollama: %s, want %s", got, want)
}
// Qwen Code writes its key on its own, before the models
p = filepath.Join(dir, "qwen.json")
os.WriteFile(p, []byte(`{"env":{"MAGPIE_QWEN_API_KEY":"nas-key"},"m":{"baseUrl":"http://192.168.1.20:3425/v1"}}`), 0o644)
if got := string(keptAway(p, []byte(`{"env":{"MAGPIE_QWEN_API_KEY":"magpie"},"m":{"baseUrl":"http://192.168.1.20:3425/v1"}}`))); !strings.Contains(got, `"nas-key"`) {
t.Errorf("the key alone went back to magpie's: %s", got)
}
wsl.Lock()
wsl.seen = nil
wsl.Unlock()
}
// The keys keptAway carries over are those a file keeps what the agent
// sends the gateway in, in each format magpie writes, and never a model's
// token limit.
func TestKeyValuesFormats(t *testing.T) {
in := strings.Join([]string{
`"apiKey": "magpie-qoder",`,
`api_key = "magpie"`,
` apiKey: magpie`,
`GEMINI_API_KEY=magpie`,
`"Authorization": "Bearer magpie"`,
`http_headers = { "x-openai-actor-authorization" = "magpie" }`,
`"max_tokens": 8192,`,
`"name": "magpie"`,
`"token": {"x": 1},`,
}, "\n")
var got []string
for _, kv := range keyValues([]byte(in)) {
got = append(got, in[kv.start:kv.end])
}
want := []string{"magpie-qoder", "magpie", "magpie", "magpie", "Bearer magpie", "magpie"}
if strings.Join(got, "|") != strings.Join(want, "|") {
t.Errorf("got %q, want %q", got, want)
}
}