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.
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
package appdir
|
|
|
|
import (
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/agentenv"
|
|
)
|
|
|
|
// An agent's variable (agentenv.Vars) on Windows is read as a process
|
|
// started now would get it, not as magpie was started with it. Windows
|
|
// gives a process its variables once, from the registry, and a User or
|
|
// System variable changed after that never reaches it: magpie runs for
|
|
// days in the tray, so a PI_CODING_AGENT_DIR the user moved kept naming
|
|
// the old folder, and magpie wrote Pi's models.json and settings.json into
|
|
// it again after the user had deleted it (bm_rabbit on Discord, 0.1.2405).
|
|
//
|
|
// Only a variable that came from the registry follows it: one whose value
|
|
// in magpie's environment at start was the registry's then (unset in both
|
|
// counts). One the launcher set itself (`$env:CODEX_HOME=…; magpie`, a
|
|
// test's sandbox) is kept, and so is one changed inside magpie since
|
|
// (os.Setenv, a test's t.Setenv), as it was before. A value followed is
|
|
// set in magpie's environment too, so the programs magpie starts (Pi,
|
|
// Codex's app-server, an agent's CLI) read the folder magpie writes.
|
|
|
|
// regEnv is a variable as the registry gives it to a process started now
|
|
// — the user's value, else the machine's, %…% expanded — and whether it
|
|
// has one; nil where there is no registry.
|
|
var regEnv = osRegEnv
|
|
|
|
// followFor is how long a registry read is used before it is read again:
|
|
// the variables are read on every request and listing.
|
|
var followFor = time.Second
|
|
|
|
type envVal struct {
|
|
v string
|
|
ok bool
|
|
}
|
|
|
|
var follow struct {
|
|
sync.Mutex
|
|
vars map[string]*followed
|
|
}
|
|
|
|
// followed is one variable that follows the registry: the value magpie's
|
|
// environment holds while nothing in magpie has changed it, and the last
|
|
// registry read.
|
|
type followed struct {
|
|
env envVal
|
|
reg envVal
|
|
at time.Time
|
|
}
|
|
|
|
func init() { followStart() }
|
|
|
|
// followStart notes which agent variables came from the registry: those
|
|
// whose value in magpie's environment is the registry's now.
|
|
func followStart() {
|
|
follow.Lock()
|
|
defer follow.Unlock()
|
|
follow.vars = nil
|
|
if regEnv == nil {
|
|
return
|
|
}
|
|
follow.vars = map[string]*followed{}
|
|
now := time.Now()
|
|
for _, name := range agentenv.Vars {
|
|
v, ok := os.LookupEnv(name)
|
|
r, rok := regEnv(name)
|
|
if ok == rok && v == r {
|
|
follow.vars[name] = &followed{env: envVal{v, ok}, reg: envVal{r, rok}, at: now}
|
|
}
|
|
}
|
|
}
|
|
|
|
// lookupEnv is os.LookupEnv, but an agent variable that came from the
|
|
// registry as it is there now (see above).
|
|
func lookupEnv(name string) (string, bool) {
|
|
follow.Lock()
|
|
defer follow.Unlock()
|
|
f := follow.vars[name]
|
|
v, ok := os.LookupEnv(name)
|
|
if f == nil {
|
|
return v, ok
|
|
}
|
|
if (envVal{v, ok}) != f.env {
|
|
// changed inside magpie: what was set stands, from now on
|
|
delete(follow.vars, name)
|
|
return v, ok
|
|
}
|
|
if now := time.Now(); now.Sub(f.at) >= followFor || now.Before(f.at) {
|
|
r, rok := regEnv(name)
|
|
f.reg, f.at = envVal{r, rok}, now
|
|
}
|
|
if f.reg != f.env {
|
|
if f.reg.ok {
|
|
os.Setenv(name, f.reg.v)
|
|
} else {
|
|
os.Unsetenv(name)
|
|
}
|
|
f.env = f.reg
|
|
}
|
|
return f.env.v, f.env.ok
|
|
}
|