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.
115 lines
3.4 KiB
Go
115 lines
3.4 KiB
Go
package sessions
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
)
|
|
|
|
// PI-Desktop (vastsa/PI-Desktop) keeps its chats as its own jsonl, not
|
|
// Pi's, and their names only in its index database: pi.sqlite's sessions
|
|
// table (id, title, deleted_at; its crates/host-core/src/db/schema.rs). A
|
|
// chat it sends through magpie with X-Magpie-Session is a gateway session
|
|
// with no file magpie reads, so it went by its id (#1967, dgmico); these
|
|
// are the names it goes by instead.
|
|
|
|
// PIDesktopDir is where PI-Desktop keeps its data: $PI_DESKTOP_DATA_DIR,
|
|
// else ~/.pi-desktop (a packaged install's, its 04-data-storage.md).
|
|
func PIDesktopDir() string {
|
|
if d := appdir.Getenv("PI_DESKTOP_DATA_DIR"); d != "" {
|
|
return d
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".pi-desktop")
|
|
}
|
|
|
|
var piDesktopTitles struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
stamp string // pi.sqlite's and its log's size and time when read
|
|
names map[string]string // session id → its title
|
|
}
|
|
|
|
// piDesktopTitlesHeld is how long the names read stand before the
|
|
// database is looked at again. Tests set it to 0.
|
|
var piDesktopTitlesHeld = time.Second
|
|
|
|
// PIDesktopTitles are the titles PI-Desktop gave the chats ids name, those
|
|
// it has one for. pi.sqlite is only read: opened immutable, or from a copy
|
|
// when its write-ahead log holds what isn't in it yet, as Cursor's
|
|
// store.db is (openCursorStore). A database that can't be read leaves the
|
|
// names last read standing: unknown, not untitled.
|
|
func PIDesktopTitles(ids []string) map[string]string {
|
|
if !Reads("pi") {
|
|
return map[string]string{}
|
|
}
|
|
out := map[string]string{}
|
|
if len(ids) == 0 {
|
|
return out
|
|
}
|
|
piDesktopTitles.Lock()
|
|
defer piDesktopTitles.Unlock()
|
|
if piDesktopTitles.names == nil || time.Since(piDesktopTitles.at) >= piDesktopTitlesHeld {
|
|
scanPIDesktopTitles()
|
|
}
|
|
for _, id := range ids {
|
|
if name := piDesktopTitles.names[id]; name != "" {
|
|
out[id] = name
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// scanPIDesktopTitles reads the names again when pi.sqlite or its log
|
|
// changed; the caller holds piDesktopTitles.
|
|
func scanPIDesktopTitles() {
|
|
piDesktopTitles.at = time.Now()
|
|
path := filepath.Join(PIDesktopDir(), "pi.sqlite")
|
|
main, err := os.Stat(path)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
piDesktopTitles.names, piDesktopTitles.stamp = map[string]string{}, ""
|
|
return
|
|
} else if err != nil {
|
|
return
|
|
}
|
|
stamp := main.ModTime().String() + "|" + strconv.FormatInt(main.Size(), 10)
|
|
if wal, err := os.Stat(path + "-wal"); err == nil {
|
|
stamp += "|" + wal.ModTime().String() + "|" + strconv.FormatInt(wal.Size(), 10)
|
|
}
|
|
if piDesktopTitles.names != nil && stamp == piDesktopTitles.stamp {
|
|
return
|
|
}
|
|
db, closeDB, err := openCursorStore(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer closeDB()
|
|
rows, err := db.Query(`SELECT id, title FROM sessions WHERE deleted_at IS NULL AND title != ''`)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
names := map[string]string{}
|
|
for rows.Next() {
|
|
var id, name string
|
|
if rows.Scan(&id, &name) == nil && id != "" {
|
|
names[id] = title(name)
|
|
}
|
|
}
|
|
if rows.Err() != nil {
|
|
return
|
|
}
|
|
piDesktopTitles.names, piDesktopTitles.stamp = names, stamp
|
|
}
|
|
|
|
// isPIDesktop says whether a gateway session's agent may be PI-Desktop's:
|
|
// its own name, or Pi's, which it goes by unless given a User-Agent of
|
|
// its own (#1966).
|
|
func isPIDesktop(agent string) bool { return agent == "pi-desktop" || agent == "pi" }
|