Files
magpie/internal/plugin/zeroed_1505_test.go
T
yetoneandggbdpq 89271cd4e8 provider, plugin, settings: accounts left all zero by a crash are read from their last good copy, and every state file is flushed before it is renamed in (#1505, Zhangwier)
Zhangwier's Windows machine went down hard, and logins.json (10783 bytes)
and plugin-auth.json (29926 bytes) came back at full length with every byte
0x00. Every account was gone. The rename had reached the disk before the
data. magpie then read "no accounts", and its next write would have made
that final.

What changed:
- Flushed before rename: steady.WriteFile writes a temp file, fsyncs it,
  then renames it in. A failed write or flush removes the temp file and
  leaves the old file as it was. edit.WriteAtomic, Kiro's writer, the
  Codex session rewrite and the plugin host's writeDurable (host.js)
  flush the same way. So do writePrivate (providers, logins, migrations),
  settings, profiles, plugins.json, plugin-auth.json and mcp-signins.json.
- A last good generation: before each write, lastgood.Keep keeps the
  valid file there as <file>.bak. A file that isn't valid is copied aside
  as <file>.bad-<time>, with -2, -3 when two land in one second (O_EXCL),
  and is never overwritten before that copy exists. The .bak lags one
  write behind on purpose, since a crash zeroes the latest write.
- A failed read is unknown, never empty. On a cold start, logins.json,
  providers.json, migrations.json, settings.json, profiles.json,
  plugins.json, plugin-auth.json (Go and the host) and mcp-signins.json
  are read from their .bak when they don't read. The recovery is logged
  and listed in /api/state's recovered, and the window says it once in
  every language. With no good .bak, a sign-out leaves plugin-auth.json
  and mcp-signins.json as they are (the host's changeAuth no longer
  writes {} over them). magpie's own sign-out returns the error, and a
  sign-in copies the bad file aside before writing.
- caller-keys.json is flushed but never read from a .bak. A key revoked
  in the last write would come back, so a store that doesn't read still
  refuses every key, as its tests require.

ggbdpq's patch (f230bcae) is where the fsync-before-rename in
writePrivate, writeWhole and writeAuth came from. This change puts it in
one shared helper and reaches every other writer of magpie's state. The
JS write cleans up its temp file. The .bak recovery, the .bad copies and
the GUI notice are added here.

What was run:
- New tests, built from the literal case (10783 and 29926 zero bytes):
  - internal/lastgood
  - provider: TestZeroedLogins*, TestZeroedMigrations*,
    TestZeroedProviders*, TestWritePrivateSyncsBeforeRename
  - plugin: TestZeroedPluginAuth* against the real bun host,
    TestPluginAuthSyncedBeforeRename with a plugin that makes fsync fail,
    TestZeroedPluginList*
  - TestZeroedSettings*, TestZeroedProfiles*, TestZeroedMCPSignIns*
  - steady TestWriteFileSyncsBeforeRename, edit
    TestWriteAtomicSyncsBeforeRename
  - gui recovered-file.test.cjs (en/zh/zh-TW/ja/de, Chromium and WebKit)
- Broken by hand, each going red:
  - Without the sync: the steady, edit, writePrivate and host fsync tests.
  - Without the .bak fallback: every TestZeroed* test.
  - The old changeAuth writes "{}\n" over the zeros.
  - The old readLogins default.
  - KeepBad without suffixes fails the one-second test.
  - Without recoveredOnce, the GUI test fails 5/5.
- macOS: go test -tags nogui ./... in a sandbox HOME. The only failures
  were the two access tests (then fixed by not recovering caller keys) and
  TestProvidersCmdSaysUnreadable, which now removes the .bak to test the
  no-backup case. Both packages were rerun and pass. gofmt -l prints
  nothing. go vet, and GOOS=linux and GOOS=windows go vet -tags nogui, are
  clean on the touched packages.
- GUI: recovered-file, whats-new, click-scroll, gui-de, gui-ja,
  gui-zh-tw and i18n-keys pass.
- Windows box (cross-compiled test exes, sandboxed USERPROFILE/APPDATA):
  lastgood, steady, provider, plugin (real bun host), settings, mcpauth,
  edit and access pass. In profile, TestProfileSwitchesModelList and
  TestOldProfileKeepsModelList fail the same on origin/main (filed as
  #1517).

Reported-by: Zhangwier
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-10 22:54:09 +08:00

156 lines
5.1 KiB
Go

package plugin
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/lastgood"
)
// zeroedAuth is plugin-auth.json as #1505's Windows crash left it: its full
// length, 29,926 bytes, every one 0x00.
var zeroedAuth = make([]byte, 29926)
func addRenew(t *testing.T, ctx context.Context) {
t.Helper()
t.Setenv("FAKE_BASE", "http://127.0.0.1:1/v1")
t.Setenv("RENEW_LOG", filepath.Join(t.TempDir(), "renewals"))
abs, _ := filepath.Abs("testdata/renew/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
}
func importRenew(t *testing.T, ctx context.Context, who string) string {
t.Helper()
k, err := Import(ctx, "renewco", map[string]any{"type": "oauth", "refresh": "r-ok-" + who, "access": "a", "expires": time.Now().Add(time.Hour).UnixMilli(), "accountId": who})
if err != nil {
t.Fatal(err)
}
return k
}
// The host reads a zeroed plugin-auth.json from its last good generation,
// tells magpie, which tells the user, and a sign-in added keeps the
// accounts and the zeroed file aside.
func TestZeroedPluginAuthReadFromBackup(t *testing.T) {
sandbox(t)
lastgood.Reset()
t.Cleanup(lastgood.Reset)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
addRenew(t, ctx)
a := importRenew(t, ctx, "a@renew")
b := importRenew(t, ctx, "b@renew")
Restart()
if _, err := os.Stat(lastgood.Bak(AuthPath())); err != nil {
t.Fatalf("no last good generation kept: %v", err)
}
if err := os.WriteFile(AuthPath(), zeroedAuth, 0o600); err != nil {
t.Fatal(err)
}
// magpie's own reads: the .bak lags one write, so it holds a
if got := Auths("renewco"); len(got) != 1 || got[a] == nil {
t.Fatalf("a zeroed plugin-auth.json read as %v", got)
}
if len(lastgood.Recovered()) != 1 {
t.Fatalf("magpie's read not noted: %+v", lastgood.Recovered())
}
lastgood.Reset() // the host's own read is told magpie too
// the host's
c := importRenew(t, ctx, "c@renew")
all := authFile(t)
if all[a] == nil || all[c] == nil {
t.Fatalf("after a sign-in added over a zeroed file: %v", all)
}
_ = b // the write the crash took
kept, _ := filepath.Glob(AuthPath() + ".bad-*")
if len(kept) != 1 {
t.Fatalf("the zeroed file not kept aside: %v", kept)
}
if got, _ := os.ReadFile(kept[0]); !bytes.Equal(got, zeroedAuth) {
t.Fatalf("kept %d bytes", len(got))
}
for wait := time.Now().Add(5 * time.Second); len(lastgood.Recovered()) == 0 && time.Now().Before(wait); {
time.Sleep(20 * time.Millisecond)
}
if n := lastgood.Recovered(); len(n) != 1 || n[0].File != "plugin-auth.json" || n[0].Why != "29926 bytes, every one zero" {
t.Fatalf("the host's recovery isn't told: %+v", n)
}
}
// With no backup, a zeroed plugin-auth.json is unknown, never none: a
// sign-out doesn't write none over it, magpie's or the host's.
func TestZeroedPluginAuthWithoutBackupNotWrittenOver(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
addRenew(t, ctx)
if _, err := get(ctx); err != nil {
t.Fatal(err)
}
os.MkdirAll(filepath.Dir(AuthPath()), 0o700)
os.WriteFile(AuthPath(), zeroedAuth, 0o600)
// one account named, and every one of the provider's
for _, account := range []string{"renewco#a@renew", ""} {
if err := SignOut(ctx, "renewco", account); err != nil {
t.Fatal(err)
}
if got, _ := os.ReadFile(AuthPath()); !bytes.Equal(got, zeroedAuth) {
t.Fatalf("the host's sign-out of %q wrote %q over sign-ins that don't read", account, got)
}
}
Restart()
if err := SignOut(ctx, "renewco", ""); err == nil {
t.Fatal("magpie's sign-out of sign-ins that don't read said done")
}
if got, _ := os.ReadFile(AuthPath()); !bytes.Equal(got, zeroedAuth) {
t.Fatalf("magpie's sign-out wrote %q over sign-ins that don't read", got)
}
// a sign-in added writes, the zeroed file kept aside first
k := importRenew(t, ctx, "new@renew")
if authFile(t)[k] == nil {
t.Fatal("the sign-in added wasn't kept")
}
if kept, _ := filepath.Glob(AuthPath() + ".bad-*"); len(kept) != 1 {
t.Fatalf("the zeroed file not kept aside: %v", kept)
}
}
// The host flushes plugin-auth.json before renaming it over the old: a
// flush that fails leaves the sign-ins there as they were.
func TestPluginAuthSyncedBeforeRename(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
flag := filepath.Join(t.TempDir(), "fail")
t.Setenv("FSYNC_FAIL", flag)
abs, _ := filepath.Abs("testdata/nofsync/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
if _, err := Import(ctx, "syncco", map[string]any{"type": "api", "key": "k-old"}); err != nil {
t.Fatal(err)
}
before, err := os.ReadFile(AuthPath())
if err != nil {
t.Fatal(err)
}
os.WriteFile(flag, nil, 0o600)
if _, err := Import(ctx, "syncco", map[string]any{"type": "api", "key": "k-new"}); err == nil {
t.Fatal("a sign-in saved without its flush said done")
}
if after, _ := os.ReadFile(AuthPath()); !bytes.Equal(after, before) {
t.Fatalf("a failed flush changed plugin-auth.json:\n%s\nwas\n%s", after, before)
}
if left, _ := filepath.Glob(AuthPath() + ".tmp-*"); len(left) != 0 {
t.Fatalf("a failed write left %v", left)
}
}