Files
magpie/internal/agent/applied_keep_test.go
T
yetoneandggbdpq 58651568b3 agent, sessions, provider: a state file that doesn't parse is kept aside, not written over as empty (#2263, ggbdpq)
applied.json, wsl.json and sessions-wsl.json were read as empty when they
didn't parse, and the next save wrote that emptiness over them. What
magpie had applied to each agent, the WSL distros and the WSL session
listing were lost with no copy. stash.json already kept a bad file aside
as .bad-<time>. Each of these now does the same through one helper,
lastgood.KeepUnparsed, instead of a copy per file:
- It does nothing when the file is missing or empty.
- When the file doesn't unmarshal into the reader's own type
  (lastgood.Into[T]), it copies the file aside (KeepBad).
- It stops the write when the copy can't be kept.

This also fixes the issue's claim about stash.json. Stash judged a file
with json.Valid, so `[]`, which a map reader takes as empty, was written
over unkept. Its failed copy was only logged, and the write went ahead
anyway. Both are fixed (TestUnreadStashArrayKeptAside), and
gateway-hidden.json uses the same helper.

Siblings found by going through every single-file JSON reader that takes
a bad file as empty and then writes it whole:
- stepfun-auth
- login-returns
- quota-history
- balance-history
- credits-daily
- Claude Desktop tiers
- plugin updates
- usage's computer id
- davsync's sync state

Each now keeps a bad copy. Caches that are rebuilt from the vendor are
left alone; docs/subsystems/providers-accounts.md lists them.

Run:
- gofmt -l, go vet (darwin -tags nogui, GOOS=linux -tags nogui,
  GOOS=windows): clean.
- go test -tags nogui -p 4 -timeout 40m ./... with the other three
  commits of this push: see the last one's body.

Broken by hand: on the code before this, these fail:
- the issue's applied_keep_test.go and wsl_keep_test.go
- TestUnreadStashArrayKeptAside
- TestUnreadStateKeptAside (10 subtests)
- the gateway, usage, davsync and plugin _keep tests
TestKeepUnparsed and TestKeepUnparsedFailsWhenItCantBeKept cover the
helper.

I dropped a test, "not written when it can't be kept", for stash. It passed
on the old code too, because a read-only folder also stops the atomic
write.

Not tried: a real WSL distro.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-15 09:36:26 +08:00

178 lines
5.1 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
// applied.json holds what magpie last set on every agent. A file that
// doesn't parse is read as no record, and the next record writes that
// over it: every other agent's record is copied aside first.
func TestUnreadAppliedKeptAside(t *testing.T) {
stashHome(t)
p := appliedPath()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
half := `{"claude":{"at":"2026-10-01T00:00:00Z","fields":{"model":"magpie/opus"}},"codex":{"at":"2026-10-01T00:00:00Z","fields":{"model":"magpie/gpt"}}`
if err := os.WriteFile(p, []byte(half), 0o600); err != nil {
t.Fatal(err)
}
record("pi", "model", "magpie/pi-model")
kept := keptAside(t, p)
if string(kept) != half {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
live, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(live), `"magpie/pi-model"`) {
t.Fatalf("applied.json after the record: %s", live)
}
// a file that parses is written over as before, nothing more kept
good := `{"claude":{"at":"2026-10-01T00:00:00Z","fields":{"model":"magpie/opus"}}}`
if err := os.WriteFile(p, []byte(good), 0o600); err != nil {
t.Fatal(err)
}
record("pi", "model", "magpie/pi-model")
if more, _ := filepath.Glob(p + ".bad-*"); len(more) != 1 {
t.Fatalf("kept aside after a good write: %v", more)
}
live, err = os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(live), `"magpie/opus"`) || !strings.Contains(string(live), `"magpie/pi-model"`) {
t.Fatalf("applied.json after a good record: %s", live)
}
}
// JSON that isn't the record map (an array, as a hand edit or another
// program might leave) is not a record of what magpie set either.
func TestUnreadAppliedArrayKeptAside(t *testing.T) {
stashHome(t)
p := appliedPath()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
arr := []byte(`[]`)
if err := os.WriteFile(p, arr, 0o600); err != nil {
t.Fatal(err)
}
record("pi", "model", "magpie/pi-model")
if kept := keptAside(t, p); string(kept) != string(arr) {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
}
// A copy that can't be made stops the write: the unread file stays.
func TestUnreadAppliedNotWrittenWhenItCantBeKept(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("a directory without write permission still accepts a new name")
}
stashHome(t)
p := appliedPath()
dir := filepath.Dir(p)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
half := `{"claude":{"at":"2026-10-01T00:00:00Z","fields":{"model":"magpie/opus"}}`
if err := os.WriteFile(p, []byte(half), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chmod(dir, 0o555); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chmod(dir, 0o755) })
if f, err := os.Create(filepath.Join(dir, "probe")); err == nil {
f.Close()
t.Skip("the filesystem does not enforce the read-only folder")
}
record("pi", "model", "magpie/pi-model")
live, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
if string(live) != half {
t.Fatalf("applied.json = %q, want it left as %q", live, half)
}
}
// wsl.json holds every distro's last probe. A file that doesn't parse is
// read as no distro, and the next save writes that over it.
func TestUnreadWSLConfigKeptAside(t *testing.T) {
stashHome(t)
p := wslStatePath()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
half := `{"Ubuntu":{"name":"Ubuntu","home":"/home/me","probe":2,"values":{"claude.model":"magpie/opus"}}`
if err := os.WriteFile(p, []byte(half), 0o600); err != nil {
t.Fatal(err)
}
saveFreshWSL(t)
kept := keptAside(t, p)
if string(kept) != half {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
live, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(live), `"Fresh"`) {
t.Fatalf("wsl.json after the save: %s", live)
}
}
func TestUnreadWSLConfigArrayKeptAside(t *testing.T) {
stashHome(t)
p := wslStatePath()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
arr := []byte(`[]`)
if err := os.WriteFile(p, arr, 0o600); err != nil {
t.Fatal(err)
}
saveFreshWSL(t)
if kept := keptAside(t, p); string(kept) != string(arr) {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
}
func saveFreshWSL(t *testing.T) {
t.Helper()
wsl.Lock()
oldSeen, oldDirty, oldAt, oldRun := wsl.seen, wsl.dirty, wsl.at, wsl.runAt
wsl.seen = map[string]*distro{"Fresh": {Name: "Fresh", Home: "/home/fresh", Probe: wslProbeVersion}}
wsl.dirty = true
wsl.Unlock()
t.Cleanup(func() {
wsl.Lock()
wsl.seen, wsl.dirty, wsl.at, wsl.runAt = oldSeen, oldDirty, oldAt, oldRun
wsl.Unlock()
})
wslSave()
}
func keptAside(t *testing.T, path string) []byte {
t.Helper()
matches, err := filepath.Glob(path + ".bad-*")
if err != nil {
t.Fatal(err)
}
if len(matches) != 1 {
t.Fatalf("kept aside %d copies, want 1: %v", len(matches), matches)
}
b, err := os.ReadFile(matches[0])
if err != nil {
t.Fatal(err)
}
return b
}