Files
magpie/internal/agent/rewire_test.go
T
yetone 2c75d89827 agent: AstrBot is an agent magpie connects: its default chat model one of magpie's, its own providers kept (orange27 on Discord)
orange27 asked on Discord for AstrBot (AstrBotDevs/AstrBot) as an agent.

What changed:
- internal/agent/astrbot.go: a new agent for AstrBot's data/cmd_config.json
  under its root ($ASTRBOT_ROOT, else ~/.astrbot, the desktop app's, else
  ~/astrbot / ~/AstrBot when it holds an install).
  - Connecting adds one "magpie" entry to provider_sources
    (openai_chat_completion at the gateway, key magpie-astrbot) and one
    "magpie/<model>" entry per model of magpie's to provider.
  - It sets the default chat model:
    agent_runner.config.model.provider_id on 4.28+, or
    provider_settings.default_provider_id on older layouts.
  - Field types are taken from AstrBot's own defaults and validator: key is
    a list, timeout and max_context_tokens are ints, modalities lists
    text/image/tool_use, and custom_headers / custom_extra_body are objects.
  - The user's own sources and models are kept byte for byte. What they
    change in magpie's entries (timeout, proxy, enable) stays through a
    sync, and a sync with nothing new doesn't rewrite the file.
  - The UTF-8 BOM AstrBot writes is kept.
  - The first connect copies the file to cmd_config.json.before-magpie.
  - Disconnect takes magpie's entries out and puts back the pick it found,
    so the file comes back byte for byte.
  - A pick the user makes while connected is theirs and stands.
  - Without the file it says to start AstrBot once (or set ASTRBOT_ROOT).
    Without provider_sources (before 4.10) it says to update.
  - The notice says to restart AstrBot, before saving in its WebUI: it
    reads the file only at start-up and its WebUI writes the whole file
    back on save.
  - Docker installs aren't reached: their root is inside the container.
- agents.go, agentenv.go: registers it; ASTRBOT_ROOT joins the agent env.
- icons/astrbot.svg: AstrBot's logo, as a mask icon.
- applied_test.go, rewire_test.go: the every-agent tests seed AstrBot's
  settings, as they do Hanako's.
- docs/subsystems/agent-wiring.md, docs/reference.md: AstrBot's row.

What was run:
- go test -tags nogui ./internal/agent -run '^TestAstrBot'. The tests went
  red when, by hand, I dropped the BOM, rewrote the user's entries, left
  the pick unrestored, overwrote the user's own pick, made a no-op sync
  write, rewrote the backup on a switch, or broke the legacy layout.
- Real use:
  - AstrBot 4.28.2 installed with uv into a scratch venv (Python 3.12),
    run from an isolated root under a sandbox HOME.
  - A sandbox magpie (MAGPIE_ADDR 127.0.0.1:3617, ASTRBOT_ROOT set) with a
    fake OpenAI upstream on 3618 ran `magpie astrbot magpie/fake/echo-1`.
  - AstrBot restarted, logged "Selected openai_chat_completion(magpie/
    fake/echo-1) as default chat model provider", and answered a WebUI
    chat (/api/chat/send) with the upstream's reply.
  - usage.jsonl recorded the turn under agent "astrbot".
  - `magpie astrbot default` gave back the file AstrBot wrote,
    byte-identical, and AstrBot started clean on it.
- The icon rendered as a mask in Chromium and WebKit. No GUI strings were
  added: the notice is the agent's own English line, like the others'.
- gofmt -l; go vet (darwin, GOOS=linux, GOOS=windows) on internal/agent and
  internal/agentenv; go test -tags nogui ./... under a temp HOME (load
  ~170-240): every package passed but internal/gateway. That package
  doesn't import internal/agent. It timed out at 10m, then with -timeout
  30m failed TestQueuedStreamWaitedOutIsTheStreamsError (its 400ms idle
  read and the 100ms "the first has the slot" sleep; a delay injected
  before the first request reproduces it). internal/agent and
  internal/agentenv were rerun green after rebasing on origin/main.
2026-10-11 00:41:09 +08:00

241 lines
7.4 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/settings"
)
// setPort is Settings' gateway port set to p, as the Settings page saves it.
func setPort(t *testing.T, p int) {
t.Helper()
s := settings.Load()
s.Port = p
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
}
// filesWith are the files under home holding s.
func filesWith(home, s string) []string {
var out []string
filepath.WalkDir(home, func(p string, e os.DirEntry, err error) error {
if err != nil || e.IsDir() {
return nil
}
if b, _ := os.ReadFile(p); strings.Contains(string(b), s) {
out = append(out, p)
}
return nil
})
return out
}
// The gateway's port set in Settings (Magic_zero on Discord) moves every
// agent magpie connected: each one's config is written with the gateway's
// URL on the new port, none keeps the old one, and each is still connected
// with nothing off.
func TestPortMovesEveryAgent(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 })
almaApp := startAlma(t)
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)
// AstrBot is connected once it has written its settings
os.MkdirAll(filepath.Join(home, ".astrbot", "data"), 0o755)
os.WriteFile(filepath.Join(home, ".astrbot", "data", "cmd_config.json"), []byte(astrbotConfig), 0o644)
setPort(t, 3591)
if gateway.URL() != "http://127.0.0.1:3591" {
t.Fatalf("Settings' port isn't the gateway's: %s", gateway.URL())
}
var wired []*Agent
for _, a := range All() {
// as TestDriftUnwiredEveryAgent: agy's gateway is in the command
// that starts it, and a WSL agent's files are beyond the sandbox
if a.Check == nil || a.Launch != nil || a.WSL != "" {
continue
}
if a.Native != nil {
if err := a.Connect(); err != nil {
t.Fatal(err)
}
wired = append(wired, a)
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)
}
wired = append(wired, a)
}
if len(filesWith(home, "127.0.0.1:3591")) == 0 {
t.Fatal("no agent's config has the gateway's URL")
}
on := OnGateway()
for _, a := range wired {
if !containsAgent(on, a.ID) {
t.Errorf("%s is connected but not on the gateway: %+v", a.ID, a.Drift())
}
}
setPort(t, 3592)
moved, err := Rewire(on)
if err != nil {
t.Fatal(err)
}
// OpenChamber reads OpenCode's config, which moves with OpenCode
if len(moved) < len(wired)-1 {
t.Errorf("moved %d of %d: %v", len(moved), len(wired), moved)
}
if left := filesWith(home, "127.0.0.1:3591"); len(left) > 0 {
t.Errorf("still on the old port: %v", left)
}
if almaApp.repoint("127.0.0.1:3591", "127.0.0.1:3591") > 0 {
t.Error("Alma still has the old port")
}
for _, a := range wired {
if !a.Wired() {
t.Errorf("%s isn't connected after the move", a.ID)
}
if d := a.Drift(); d != nil {
t.Errorf("%s is off after the move: %+v", a.ID, d)
}
}
}
func containsAgent(as []*Agent, id string) bool {
for _, a := range as {
if a.ID == id {
return true
}
}
return false
}
// Codex signed in with ChatGPT is joined to magpie beside its own models:
// the move writes its base URL on the new port and keeps the model it was
// on, rather than putting it on one of magpie's.
func TestPortMovesJoinedCodex(t *testing.T) {
t.Setenv("MAGPIE_ADDR", "")
home, read := codexHome(t, `{"auth_mode":"chatgpt","tokens":{"access_token":"at","refresh_token":"rt","id_token":"x.e30.x","account_id":"acc"}}`, "model = \"gpt-5.1\"\n")
setPort(t, 3591)
cx := codex(home)
c, err := cx.ConnectHow()
if err != nil || c.How != "joined" {
t.Fatalf("not joined: %+v %v\n%s", c, err, read())
}
on := OnGateway()
setPort(t, 3592)
if _, err := Rewire(on); err != nil {
t.Fatal(err)
}
cfg := read()
if !strings.Contains(cfg, "127.0.0.1:3592") || strings.Contains(cfg, "127.0.0.1:3591") {
t.Fatalf("Codex not moved:\n%s", cfg)
}
if !strings.Contains(cfg, `model = "gpt-5.1"`) || !cx.Joined() {
t.Fatalf("Codex's own model lost:\n%s", cfg)
}
if d := cx.Drift(); d != nil {
t.Fatalf("off after the move: %+v", d)
}
}
// An agent taken off magpie before the port moved is left as it is: it is
// the user's to set again, not the move's.
func TestPortLeavesAnUnwiredAgent(t *testing.T) {
t.Setenv("MAGPIE_ADDR", "")
home, read := codexHome(t, "", "")
setPort(t, 3591)
cx := codex(home)
if err := cx.Apply("model", "fake/m1"); err != nil {
t.Fatal(err)
}
cfg := filepath.Join(home, ".codex", "config.toml")
os.WriteFile(cfg, []byte(strings.ReplaceAll(read(), "127.0.0.1:3591", "127.0.0.1:9")), 0o644)
on := OnGateway()
setPort(t, 3592)
if _, err := Rewire(on); err != nil {
t.Fatal(err)
}
if strings.Contains(read(), "127.0.0.1:3592") {
t.Fatalf("an agent taken off magpie was moved:\n%s", read())
}
}
// A magpie MAGPIE_ADDR puts beside the installed one tells the installed
// one's wiring by the port the installed one's Settings have, not 3425.
func TestDriftInstalledMagpieOnItsPort(t *testing.T) {
home, _ := codexHome(t, "", "")
setPort(t, 3591)
t.Setenv("MAGPIE_ADDR", "127.0.0.1:3426")
cx := codex(home)
if err := cx.Apply("model", "fake/m1"); err != nil {
t.Fatal(err)
}
cfg := filepath.Join(home, ".codex", "config.toml")
os.WriteFile(cfg, []byte("openai_base_url = \"http://127.0.0.1:3591"+gateway.CodexPath+"\"\nmodel = \"fake/m1\"\n"), 0o644)
if d := cx.Drift(); d != nil {
t.Fatalf("the installed magpie's wiring taken for drift: %+v", d)
}
}
// Moved, an agent still puts back what it had before magpie when switched
// off: its own endpoint and token, not magpie's at the old port, which
// setting it again there would otherwise have noted as its own.
func TestPortKeepsWhatAnAgentHadBefore(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 })
path := filepath.Join(home, ".claude", "settings.json")
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(`{"env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"own-token"}}`), 0o644)
setPort(t, 3591)
cc := claude(home)
var want string
for _, o := range cc.Field("model").Options(cc.Values()) {
if o.Ref == "fake/m1" {
want = o.Value
}
}
if err := cc.Apply("model", want); err != nil {
t.Fatal(err)
}
on := OnGateway()
setPort(t, 3592)
if _, err := Rewire(on); err != nil {
t.Fatal(err)
}
if u, _ := edit.GetJSON(path, "env.ANTHROPIC_BASE_URL"); u != "http://127.0.0.1:3592" {
t.Fatalf("Claude Code not moved: %q", u)
}
if err := cc.Disconnect(); err != nil {
t.Fatal(err)
}
u, _ := edit.GetJSON(path, "env.ANTHROPIC_BASE_URL")
tok, _ := edit.GetJSON(path, "env.ANTHROPIC_AUTH_TOKEN")
if u != "https://relay.example" || tok != "own-token" {
t.Fatalf("switched off, Claude Code got %q %q back, not its own", u, tok)
}
}