Files
magpie/internal/agent/wsl_nvm_test.go
T
yetone 6287385f91 perf: Codex's bundled instructions and the WSL probe are built on first use
Two package-level values were built on every launch, `magpie
statusline` on each Claude Code message included, for paths it never
takes:
- codexcat's bundled: json.Unmarshal of the 231 KB
  codex_instructions.json into a map (~0.48 MB). Now sync.OnceValue,
  read the first time a model's instructions are looked up.
- agent's wslProbeScript: the WSL probe's shell script, grown with `s +=`
  over every WSL agent, so each step copied the whole string (~0.5 MB),
  on macOS and Linux too. Now sync.OnceValue, built the first time a
  distro is probed. Callers and tests call it.

Measured on mote, sandbox HOME, t44 (v0.1.3519) against t45 (this):
- GODEBUG=inittrace=1 sum: 3.94 MB -> 2.98 MB
- `magpie statusline`, 3 runs: instructions retired ~156M -> ~137M

Run: go vet ./internal/agent ./internal/codexcat (darwin, GOOS=linux,
GOOS=windows) ok; go test ./internal/codexcat ok, ./internal/agent ok
(290s); root go vet ./... after rebase below.
2026-11-10 13:11:19 +08:00

64 lines
2.1 KiB
Go

package agent
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// A command the WSL probe finds only under nvm is the one under the Node a
// terminal there gets: nvm's default, as nvm itself resolves it, else the
// highest version. The probe's glob took nvm's folders in name order, so
// a codex under v18 was taken for the default v22's, and v9's came last.
func TestWSLProbeNvmDefault(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the probe runs in a Linux distro")
}
for _, c := range []struct {
name, nvmSh, want string
}{
{"no nvm.sh: the highest, not the first by name", "", "v22.12.0"},
// a stand-in for nvm.sh: the probe asks nvm_version default, which
// the real one resolves through alias/default
{"nvm's default", "nvm_version() { [ \"$1\" = default ] && echo v9.11.2; }\n", "v9.11.2"},
{"nvm's default not installed: the highest", "nvm_version() { echo N/A; }\n", "v22.12.0"},
} {
t.Run(c.name, func(t *testing.T) {
home := t.TempDir()
sys := filepath.Join(home, "sys") // PATH: nothing of codex's
os.MkdirAll(sys, 0o755)
nvm := filepath.Join(home, ".nvm")
for _, v := range []string{"v18.20.4", "v22.12.0", "v9.11.2"} {
bin := filepath.Join(nvm, "versions", "node", v, "bin")
os.MkdirAll(bin, 0o755)
testenv.Program(t, filepath.Join(bin, "codex"), "#!/bin/sh\n")
}
if c.nvmSh != "" {
to, err := exec.LookPath("timeout")
if err != nil {
t.Skip("no timeout(1)")
}
if err := os.Symlink(to, filepath.Join(sys, "timeout")); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(nvm, "nvm.sh"), []byte(c.nvmSh), 0o644)
}
cmd := exec.Command("/bin/sh", "-c", wslProbeScript())
cmd.Env = []string{"HOME=" + home, "PATH=" + sys + ":/usr/bin:/bin"}
out, err := cmd.Output()
if err != nil {
t.Fatalf("%v: %s", err, out)
}
want := "bin:codex " + filepath.Join(nvm, "versions", "node", c.want, "bin", "codex") + "\n"
if !strings.Contains(string(out), want) {
t.Fatalf("probe said:\n%s\nwant %q", out, want)
}
})
}
}