Files
magpie/internal/agent/stash_test.go
T
yetone 270b52573a agent: stash.json is changed under a lock and written whole, and one that doesn't parse is kept aside, not written over as no stash
stash.json holds every agent's config from before magpie wired it. stash,
forget and unstash read it, changed it and wrote it back with
os.WriteFile, with no lock, and a read that didn't parse came back empty.
Two changes at once (Connect all, a gateway move setting each agent again)
could leave the file half written, and the next change read that as no
stash and wrote it back: every agent's originals gone, so Disconnect had
nothing to put back. Now each change runs under stashMu and is written
with edit.WriteAtomic; a file that doesn't parse is copied to
stash.json.bad-<time> first; a new file is made 0600, since it holds keys.
rewire.go's stashPut and StepCode's stepcodeStoreState go through the same
stashWrite (StepCode keeps its own strict read).

TestStashKeepsChangesMadeAtOnce runs 40 stashes at once;
TestUnreadStashKeptAside stashes over a half-written file.

Ran: both new tests on origin/main's stash.go (red: "0 of 40 agents'
originals kept", "kept aside: []") and with the change (green); go vet on
darwin, GOOS=linux and GOOS=windows for internal/agent; go test -count=1
./internal/agent/ (ok, 278s); go test -race -count=3 on the stash and
StepCode tests (ok).
2026-10-11 04:28:31 +08:00

72 lines
2.0 KiB
Go

package agent
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
)
func stashHome(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
}
// Every agent's originals are in one stash.json, and each agent's connect
// and disconnect read, change and write it: two at once (the GUI's Connect
// all, a gateway move setting every agent again) must keep both changes.
func TestStashKeepsChangesMadeAtOnce(t *testing.T) {
stashHome(t)
var wg sync.WaitGroup
for i := range 40 {
wg.Add(1)
go func() {
defer wg.Done()
stash(map[string]string{fmt.Sprintf("agent%d.was", i): "theirs"})
}()
}
wg.Wait()
if n := len(stashLoad()); n != 40 {
t.Fatalf("%d of 40 agents' originals kept", n)
}
}
// A stash.json that doesn't parse is read as no stash, and the next change
// writes over it: what it held, every agent's originals, is copied aside
// first, so it can still be got back.
func TestUnreadStashKeptAside(t *testing.T) {
stashHome(t)
p := stashPath()
os.MkdirAll(filepath.Dir(p), 0o755)
half := `{"claude.was": "sk-their-own", "codex.wa`
if err := os.WriteFile(p, []byte(half), 0o600); err != nil {
t.Fatal(err)
}
stash(map[string]string{"pi.was": "x"})
if got := stashLoad(); got["pi.was"] != "x" {
t.Fatalf("stash after the change: %v", got)
}
bad, _ := filepath.Glob(p + ".bad-*")
if len(bad) != 1 {
t.Fatalf("kept aside: %v", bad)
}
if b, _ := os.ReadFile(bad[0]); string(b) != half {
t.Fatalf("kept aside as %q", b)
}
// a stash that parses is written over as before, nothing more kept
forget("pi.was")
if more, _ := filepath.Glob(p + ".bad-*"); len(more) != 1 {
t.Fatalf("kept aside after a good write: %v", more)
}
if fi, err := os.Stat(p); err != nil {
t.Fatal(err)
} else if runtime.GOOS != "windows" && fi.Mode().Perm() != 0o600 {
t.Fatalf("stash.json is %v; it holds keys", fi.Mode().Perm())
}
}