bm_rabbit (0.1.2405, Windows x64, Pi 1.4.0) moved Pi's AgentDir from
F:\piData\.pi\agent to F:\HarnessData\Pi\.pi\agent, deleted the old one and
set the User variable PI_CODING_AGENT_DIR to the new path. Changing Pi's
model in magpie then made the deleted folder again with only models.json
and settings.json in it.
Cause: Windows gives a process its variables once, from the registry, and
never passes a changed User variable to one already running. magpie runs
for days in the tray, so appdir.Getenv("PI_CODING_AGENT_DIR") (piDir) kept
the old path, and edit's write did MkdirAll on it. Every agent folder
variable (CODEX_HOME, CLAUDE_CONFIG_DIR, ...) had the same problem.
Fix, where every agent folder is read (appdir.LookupEnv, follow.go): on
Windows a variable in agentenv.Vars that came from the registry (its value
in magpie's environment at start matched the registry's, unset in both
counts too) is read from HKCU\Environment, else HKLM's Session Manager
Environment, with %...% expanded, at most once a second, and set in
magpie's environment so the programs magpie starts get it too. A value
the launcher set itself, or one set inside magpie since (a test sandbox),
stands. The direct os.Getenv reads of agent variables go through appdir
now: claudetui's CLAUDE_CONFIG_DIR, sessions' CODEX_SQLITE_HOME and
PI_DESKTOP_DATA_DIR, cliupdate's GROK_HOME, omp's OMP_PROFILE/PI_PROFILE.
And edit's writeFile no longer makes a missing tree of folders that an
agent's variable names: when the folder the variable names is missing
and so is the folder above it (bm_rabbit's whole F:\piData was gone),
appdir.Gone returns a GoneError naming the variable and the folder. The
named folder itself is still made when its parent is there, as mkdir
would (a CODEX_HOME or ANTE_HOME set before the agent first ran; 12
internal/agent tests rely on it), and an agent's default folder is made
as before. TestAnteHome's ANTE_HOME was <tmp>/elsewhere/ante with
elsewhere missing; it now makes elsewhere first. Writes that call
os.MkdirAll themselves don't ask yet: #4634.
Pi's install-layout.json isn't Pi's: Pi 1.4.0's getAgentDir
(@earendil-works/pi-coding-agent dist/config.js) reads PI_CODING_AGENT_DIR,
else ~/.pi/agent, and no file in the package names install-layout.
magpie doesn't read it.
Tests: TestAgentVarFollowsTheRegistry, TestAgentVarAddedLaterFollows,
TestAgentVarSetByHandStands, TestRegistryReadAtMostEachFollowFor,
TestGoneNamedFolderIsNotMade (internal/appdir, a fake registry), and
TestWriteNeverMakesANamedFolderAgain (internal/edit).
Broken by hand: LookupEnv back on os.LookupEnv, TestAgentVarFollowsTheRegistry
and TestAgentVarAddedLaterFollows FAIL ("after the user moved it,
PI_CODING_AGENT_DIR = .../old, want .../new"); Gone's refusal turned off,
TestGoneNamedFolderIsNotMade and TestWriteNeverMakesANamedFolderAgain FAIL
("= <nil>, want a GoneError", "the folder was made again"); Gone refusing
whenever the named folder is missing, TestGoneNamedFolderIsNotMade FAILs
("a named folder whose parent exists: ..."). All put back, green.
Real Windows run on xiaomi-gpu, sandbox USERPROFILE/APPDATA/LOCALAPPDATA,
at this head: a probe built from this tree started with
PI_CODING_AGENT_DIR=<sb>\piData\.pi\agent in its environment and the User
registry; then the User variable was set to <sb>\HarnessData\Pi\.pi\agent
and <sb>\piData deleted, and agent.Find("pi").Apply("model", ...) ran in
the same process. Pi's dir and os.Getenv are the new folder,
settings.json written there, piData not made. With HarnessData deleted
too: "PI_CODING_AGENT_DIR names <sb>\HarnessData\Pi\.pi\agent, which
doesn't exist and neither does the folder above it: ...", not made. An
earlier run of the old code (LookupEnv on os.LookupEnv, no Gone) wrote
into the old folder and made it again: bm_rabbit's case. The User
variable was removed after; the box had none.
Full run at this change on d714c4216: on mote,
gofmt -l (nothing), go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., and go test -tags nogui -timeout 45m ./...:
83 ok, 2 red, each open and not this change's: internal/gui
TestSettingsSaveKeepsWhatItDoesNotSend (#4514, red alone on origin/main)
and TestColdTurnsTaken (#4510, warm-up ordering race, 0.00s), and
internal/cli TestLibraryMCPHub (#4140, TempDir cleanup; passes alone).
internal/appdir, internal/edit and internal/agent again alone: ok.
Rebased onto 76c130b29 (eight commits, edit's and appdir's among them;
file.go's imports merged by hand): gofmt -l (nothing), the three go vets,
and internal/appdir, internal/edit, internal/agent, internal/claudetui,
internal/sessions on mote: all ok. Not the whole suite again there.
Rebased again onto 88372a39d at push (six commits, none in appdir or
edit; dd7442f28 in internal/agent): go vet ./internal/..., internal/appdir
and internal/edit ok; internal/agent not run again at this head.
Windows (xiaomi-gpu, at d714c4216): internal/appdir, internal/edit, internal/claudetui
ok; internal/agent's 12 tests that make a named folder, TestGrok* and
TestPi* ok; internal/sessions red only in TestAgySessions and
TestAgyRowOnly, the same alone on origin/main (#4115).
Not tried: magpie's own GUI on Windows (the probe calls the same
agent.Apply the GUI's /api/set reaches); a REG_EXPAND_SZ value and a
Machine-only value on the real box (code path read, not run); bm_rabbit's
junction at C:\Users\Administrator\.pi.
162 lines
5.4 KiB
Go
162 lines
5.4 KiB
Go
package appdir
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fakeRegistry stands in for Windows' registry: reg is what a process
|
|
// started now would get. followStart is taken again over it, as magpie's
|
|
// start would, and both are put back after the test.
|
|
func fakeRegistry(t *testing.T, reg map[string]string) {
|
|
t.Helper()
|
|
was, wasFor := regEnv, followFor
|
|
regEnv = func(name string) (string, bool) { v, ok := reg[name]; return v, ok }
|
|
followFor = 0
|
|
t.Cleanup(func() { regEnv, followFor = was, wasFor; followStart() })
|
|
}
|
|
|
|
// unset clears name for the test, put back after it.
|
|
func unset(t *testing.T, name string) {
|
|
t.Helper()
|
|
t.Setenv(name, "")
|
|
os.Unsetenv(name)
|
|
}
|
|
|
|
// bm_rabbit on Discord: Pi's AgentDir moved, PI_CODING_AGENT_DIR changed
|
|
// in the user's variables while magpie ran, and magpie wrote Pi's files
|
|
// into the old folder again. A variable that came from the registry reads
|
|
// as the registry has it now, and the programs magpie starts get it too.
|
|
func TestAgentVarFollowsTheRegistry(t *testing.T) {
|
|
const v = "PI_CODING_AGENT_DIR"
|
|
old, moved := filepath.Join(t.TempDir(), "old"), filepath.Join(t.TempDir(), "new")
|
|
reg := map[string]string{v: old}
|
|
fakeRegistry(t, reg)
|
|
t.Setenv(v, old) // as Windows gave it to magpie at its start
|
|
followStart()
|
|
if got := Getenv(v); got != old {
|
|
t.Fatalf("at start %s = %q, want %q", v, got, old)
|
|
}
|
|
reg[v] = moved // the user changes it in System Properties
|
|
if got := Getenv(v); got != moved {
|
|
t.Fatalf("after the user moved it, %s = %q, want %q (the registry's now, not magpie's start's)", v, got, moved)
|
|
}
|
|
if got := os.Getenv(v); got != moved {
|
|
t.Errorf("magpie's environment, which the programs it starts get, has %s = %q, want %q", v, got, moved)
|
|
}
|
|
delete(reg, v) // and then deletes it
|
|
if got, ok := LookupEnv(v); ok {
|
|
t.Errorf("after the user deleted it, %s = %q, want it unset", v, got)
|
|
}
|
|
if _, ok := os.LookupEnv(v); ok {
|
|
t.Errorf("magpie's environment still has %s after the user deleted it", v)
|
|
}
|
|
}
|
|
|
|
// One set while none was in the registry at start is followed too: a
|
|
// CODEX_HOME the user adds while magpie runs.
|
|
func TestAgentVarAddedLaterFollows(t *testing.T) {
|
|
const v = "CODEX_HOME"
|
|
reg := map[string]string{}
|
|
fakeRegistry(t, reg)
|
|
unset(t, v)
|
|
followStart()
|
|
d := t.TempDir()
|
|
reg[v] = d
|
|
if got := Getenv(v); got != d {
|
|
t.Errorf("%s added to the user's variables reads %q, want %q", v, got, d)
|
|
}
|
|
}
|
|
|
|
// A value the launcher set, unlike the registry's, and one set inside
|
|
// magpie since it started, stand as they are.
|
|
func TestAgentVarSetByHandStands(t *testing.T) {
|
|
const v, w = "CLAUDE_CONFIG_DIR", "CODEX_HOME"
|
|
mine, reg1, reg2 := t.TempDir(), t.TempDir(), t.TempDir()
|
|
reg := map[string]string{v: reg1, w: reg1}
|
|
fakeRegistry(t, reg)
|
|
t.Setenv(v, mine) // `$env:CLAUDE_CONFIG_DIR=…; magpie`
|
|
t.Setenv(w, reg1)
|
|
followStart()
|
|
reg[v], reg[w] = reg2, reg2
|
|
if got := Getenv(v); got != mine {
|
|
t.Errorf("the launcher's %s reads %q, want %q", v, got, mine)
|
|
}
|
|
os.Setenv(w, mine) // inside magpie, a test's sandbox
|
|
if got := Getenv(w); got != mine {
|
|
t.Errorf("%s set inside magpie reads %q, want %q", w, got, mine)
|
|
}
|
|
}
|
|
|
|
// What isn't an agent's variable is magpie's environment as it is.
|
|
func TestOtherVarsDontFollow(t *testing.T) {
|
|
reg := map[string]string{"MAGPIE_FOLLOW_TEST": "reg"}
|
|
fakeRegistry(t, reg)
|
|
unset(t, "MAGPIE_FOLLOW_TEST")
|
|
followStart()
|
|
if _, ok := LookupEnv("MAGPIE_FOLLOW_TEST"); ok {
|
|
t.Error("a variable no agent reads followed the registry")
|
|
}
|
|
}
|
|
|
|
// Read again after followFor, not on every call.
|
|
func TestRegistryReadAtMostEachFollowFor(t *testing.T) {
|
|
const v = "PI_CODING_AGENT_DIR"
|
|
reads := 0
|
|
was, wasFor := regEnv, followFor
|
|
regEnv = func(name string) (string, bool) {
|
|
if name == v {
|
|
reads++
|
|
}
|
|
return "", false
|
|
}
|
|
followFor = time.Hour
|
|
t.Cleanup(func() { regEnv, followFor = was, wasFor; followStart() })
|
|
unset(t, v)
|
|
followStart()
|
|
start := reads
|
|
for range 100 {
|
|
Getenv(v)
|
|
}
|
|
if reads != start {
|
|
t.Errorf("100 reads within followFor read the registry %d times, want 0", reads-start)
|
|
}
|
|
}
|
|
|
|
func TestGoneNamedFolderIsNotMade(t *testing.T) {
|
|
root := t.TempDir()
|
|
gone := filepath.Join(root, "piData", ".pi", "agent")
|
|
t.Setenv("PI_CODING_AGENT_DIR", gone)
|
|
var g *GoneError
|
|
for _, d := range []string{gone, filepath.Join(gone, "sessions")} {
|
|
if err := Gone(d); !errors.As(err, &g) || g.Var != "PI_CODING_AGENT_DIR" || g.Dir != gone {
|
|
t.Errorf("Gone(%q) = %v, want a GoneError naming PI_CODING_AGENT_DIR and %s", d, err, gone)
|
|
}
|
|
}
|
|
// the folder there: what is under it is made as before
|
|
if err := os.MkdirAll(gone, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Gone(filepath.Join(gone, "sessions")); err != nil {
|
|
t.Errorf("under a folder that exists: %v", err)
|
|
}
|
|
// the named folder alone missing, its parent there: it is made
|
|
t.Setenv("PI_CODING_AGENT_DIR", filepath.Join(root, "fresh"))
|
|
if err := Gone(filepath.Join(root, "fresh", "sessions")); err != nil {
|
|
t.Errorf("a named folder whose parent exists: %v", err)
|
|
}
|
|
// a folder no variable names, the default one, is made
|
|
t.Setenv("PI_CODING_AGENT_DIR", "")
|
|
if err := Gone(filepath.Join(root, "home", ".pi", "agent")); err != nil {
|
|
t.Errorf("a default folder: %v", err)
|
|
}
|
|
// a name that only starts the same is another folder
|
|
t.Setenv("CODEX_HOME", filepath.Join(root, "codex"))
|
|
if err := Gone(filepath.Join(root, "codex-other")); err != nil {
|
|
t.Errorf("a sibling whose name starts the same: %v", err)
|
|
}
|
|
}
|