Files
magpie/internal/sessions/wsl_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

81 lines
2.2 KiB
Go

package sessions
import (
"os"
"path/filepath"
"testing"
)
// sessions-wsl.json is the listing of session files in WSL distros, kept
// so a stopped distro is not started to list it again. A file that doesn't
// parse is read as no listing, and the next list writes that over it.
func TestUnreadWSLSessionListKeptAside(t *testing.T) {
p := wslListHome(t)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
half := `{"Ubuntu":{"home":"/home/me","files":[{"agent":"claude","key":"s1","path":"/home/me/.claude/projects/a/s1.jsonl","size":1,"mod":"2026-10-01T00:00:00Z"}]`
if err := os.WriteFile(p, []byte(half), 0o644); err != nil {
t.Fatal(err)
}
wslFiles("claude")
wslSettle()
kept := keptWSLAside(t, p)
if string(kept) != half {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
}
// JSON that isn't the listing map is not a listing either.
func TestUnreadWSLSessionListArrayKeptAside(t *testing.T) {
p := wslListHome(t)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
arr := []byte(`[]`)
if err := os.WriteFile(p, arr, 0o644); err != nil {
t.Fatal(err)
}
wslFiles("claude")
wslSettle()
if kept := keptWSLAside(t, p); string(kept) != string(arr) {
t.Fatalf("the file kept aside isn't the one that was there: %s", kept)
}
}
func wslListHome(t *testing.T) string {
t.Helper()
dir := t.TempDir()
t.Setenv("HOME", filepath.Join(dir, "home"))
t.Setenv("USERPROFILE", filepath.Join(dir, "home"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(dir, "cache"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
prevHomes, prevOff, prevRun := WSLHomes, WSLOff, WSLRunning
WSLHomes = func() []WSLHome { return nil }
WSLOff = nil
WSLRunning = nil
t.Cleanup(func() {
wslSettle()
WSLHomes, WSLOff, WSLRunning = prevHomes, prevOff, prevRun
Reset()
})
Reset()
return wslListPath()
}
func keptWSLAside(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
}