Files
magpie/internal/agent/agentenv_test.go
T
yetone 8f385e113b site: usemagpie.ai draws itself in Omarchy's look on Omarchy, in the theme Omarchy has on (owner: 整个官网和文档页面能否智能识别是不是 Omarchy 在访问,如果是 Omarchy 在访问,那么整个页面风格能否变成 Omarchy 风格?而且能够适配当前 Omarchy 就更牛逼了) — every page (home, /import, the docs in en/zh/ja) loads omarchy.js in a Linux desktop browser only; it tells Omarchy by its font (a page may load local("omarchy"), which Omarchy installs system-wide and nothing else has), remembers it, and from then on paints before the page shows: the theme's palette, the system monospace (Omarchy's font), square corners, no shadows or glass, a solid bar, buttons and what's chosen in the accent, images framed in it. Until magpie tells the theme it takes Omarchy's default (tokyo-night, or catppuccin-latte on a light desktop). magpie's gateway answers GET /v1/magpie/omarchy (name, mode, stamp and the CSS variables magpie's own windows use) to this machine only, readable only by usemagpie.ai (CORS + Private Network Access; MAGPIE_SITE_ORIGIN adds an origin to try the site locally), 404 off Omarchy. The page asks it by itself only once the browser allows it to reach this computer; a chip in the corner (Omarchy's logo and the theme) asks once on a click, lists the 22 built-in themes to pick one, and turns the look off; ?omarchy=1|0|<theme> sets it anywhere. Followed while the page shows: a theme set in Omarchy's menu comes through in about 3 s. Tried in the omarchy VM's Chromium 152 with this magpie (nogui serve): detection, the home page and zh docs in tokyo-night, the chip's menu, following the computer, and omarchy-theme-set catppuccin-latte repainting the open page light. TestOmarchyThemeForTheSite covers 404 off Omarchy, the theme and its CORS for usemagpie.ai only, LAN refused and the preflight. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. The permission prompt from the public site wasn't clicked through in a real browser.
2026-10-03 16:01:53 +08:00

114 lines
4.5 KiB
Go

package agent
import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/agentenv"
)
// readsEnv matches the two ways these packages name a variable they read to
// find an agent's folder: as the argument of a Getenv, a LookupEnv or a
// place's getenv, and as the env of a table entry, which is how Qoder's two
// builds are named (read through at.getenv(b.env)).
var readsEnv = regexp.MustCompile(`(?:Getenv|LookupEnv|getenv)\("([A-Z][A-Z_0-9]*)"\)|env:\s*"([A-Z][A-Z_0-9]*)"`)
// notAnAgent are the variables the same sources read that are not an agent's
// folder, so no sandbox clears them through agentenv.Vars.
var notAnAgent = map[string]string{
"APPDATA": "Windows' own folder, which a sandbox sets to one of its own rather than clears",
"LOCALAPPDATA": "the same",
"PATH": "the process' own",
"TZ": "the process' own",
"USER": "the process' own",
"XDG_CONFIG_HOME": "magpie's own folder's, which appdir decides",
"XDG_DATA_HOME": "the same",
"MAGPIE_ADDR": "magpie's own",
"MAGPIE_DEBUG": "magpie's own debug flag",
"MAGPIE_PUBLIC_URL": "magpie's own public URL",
"MAGPIE_EFFORT_UPDATES": "magpie's own switch for effort updates (#617)",
"MAGPIE_SITE_ORIGIN": "a site origin magpie's Omarchy theme answers, to try the site locally",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "Claude Code's behaviour flag, not an agent's folder",
}
// TestFolderVarsAreListed says every variable the packages that find an
// agent's folder read is one a sandbox clears. Nothing said so before, so a
// new agent could take a variable of its own and every sandbox would go on
// meeting the agent a developer really has installed — which is how #522 came
// about, four sandboxes clearing lists of their own that had each drifted.
//
// The five packages are this one, sessions, provider, library and gateway:
// every agent's folder variable magpie reads is read in one of them, and the
// rest of the repository reads only magpie's own variables, the process' and
// the desktop's. Their sources are read as files rather than imported, since a
// test in sessions or provider cannot import this package, which imports
// them; what the walk looks at is what they say, not what they do.
//
// What this cannot see is a variable read through a name the sources build at
// runtime, as omoDir reads OMO_CODING_AGENT_DIR and SENPI_CODING_AGENT_DIR
// out of a slice, or through a constant (const fooEnv = "FOO_HOME" and then
// os.Getenv(fooEnv)). Both are in agentenv.Vars; a third taken either way
// would not be caught here.
func TestFolderVarsAreListed(t *testing.T) {
listed := make(map[string]bool, len(agentenv.Vars))
for _, v := range agentenv.Vars {
listed[v] = true
}
dirs := []string{".", filepath.Join("..", "sessions"), filepath.Join("..", "provider"), filepath.Join("..", "library"), filepath.Join("..", "gateway")}
found := map[string]string{}
for _, dir := range dirs {
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
n := e.Name()
if e.IsDir() || !strings.HasSuffix(n, ".go") || strings.HasSuffix(n, "_test.go") {
continue
}
b, err := os.ReadFile(filepath.Join(dir, n))
if err != nil {
t.Fatal(err)
}
for _, m := range readsEnv.FindAllStringSubmatch(string(b), -1) {
name := m[1]
if name == "" {
name = m[2]
}
if _, ok := found[name]; !ok {
found[name] = filepath.Join(dir, n)
}
}
}
}
// One name read each way the pattern matches, so that a change in how a
// variable is read empties the scan loudly instead of leaving the test
// nothing to complain of.
for _, canary := range []string{"CODEX_HOME", "QODER_CONFIG_DIR"} {
if _, ok := found[canary]; !ok {
t.Errorf("%s was not found in these sources, so readsEnv no longer matches the way a variable is read", canary)
}
}
// And no excuse outlives the variable it was for.
for n := range notAnAgent {
if _, ok := found[n]; !ok {
t.Errorf("%s is in notAnAgent, but nothing in these sources reads it any more", n)
}
}
names := make([]string, 0, len(found))
for n := range found {
names = append(names, n)
}
slices.Sort(names)
for _, n := range names {
if listed[n] || notAnAgent[n] != "" {
continue
}
t.Errorf("%s, read by %s, is not in agentenv.Vars, so no sandbox clears it and a test meets the agent a developer really has installed: add it to Vars, or to notAnAgent here with a reason if it is not an agent's folder", n, found[n])
}
}