agent: a magpie-models.json that doesn't parse is kept aside, not written over or removed whole

What changed: #2263 gave magpie's state files whose reader takes an
unparseable file as empty the lastgood.KeepUnparsed copy-aside, and its
sweep named the files it covered. Codex's magpie-models.json was in
neither its covered list nor its cache exemption, and its reader has the
exact missed shape: codexcat.Keep (#1450) returns the new list for a
file it can't parse, and the next sync, model switch or stepping back in
wrote that over the file, ending the keys the user added by hand
(supports_reasoning_summaries and friends) with no copy. Stepping out
was worse: dropList took an unparseable file for one holding nothing of
the user's and removed it outright.

Now the three writers of the list go through writeOwnList, which keeps
an unparsed file aside (magpie-models.json.bad-<time>) before the write
and stops when the copy can't be made, and dropList keeps one aside
before it removes the file.

Run:
- gofmt -l internal/agent: clean.
- go test -tags nogui -p 2 -count=1 -timeout 25m ./internal/agent: ok
  (183s).
- TestUnparsedCodexCatalogKeptAside fails on the code before this ("the
  unparsed list was written over with no copy kept"); the ablation
  replays it against this commit's parent.
- go vet -tags nogui ./... for darwin, linux and windows ok; go build
  for the three ok (reviewer, at the batch head).
Full suite: go test -tags nogui -count=1 -timeout 45m ./... ok, every
  package (reviewer, at the batch head before #2424 was added).

Generated-by: GLM-5.3-Flash (ZCode)
This commit is contained in:
ggbdpq
2026-10-16 00:16:43 +08:00
committed by yetone
parent d830d460f0
commit a786c51cb0
3 changed files with 86 additions and 4 deletions
+1 -1
View File
@@ -204,7 +204,7 @@ and `TestHistoricalWindowHoldsUsesEachWindowsOwnReading`.
- An unreadable `providers.json` is an error, never an empty list. Saving over an empty list would lose every provider.
- When `logins.json` cannot be decoded as saved accounts, reads keep the accounts read before. Before saving a change, `lastgood.Keep` ([lastgood.go](../../internal/lastgood/lastgood.go)) keeps the file there now: as `logins.json.bak`, its last good generation, when it reads, or copied aside as `logins.json.bad-<time>` (`-2`, `-3`… for another within one second) when it doesn't, including valid JSON with invalid account field types, so its credentials can be recovered. If the existing file cannot be read or the copy fails, saving returns the error and leaves the original file unchanged. A missing file can be created normally.
- A Windows machine that went down mid-write left `logins.json` and `plugin-auth.json` at their full length, every byte zero (#1505): the rename reached the disk before the data. Every write of magpie's own state goes through `steady.WriteFile` (or `edit.WriteAtomic`), which flushes the temp file before renaming it in, and the plugin host's `writeDurable` does the same. So does every write of an agent's own sign-in (#2239): `codexToken`'s rotated tokens into Codex's `auth.json` (`~/.codex`, or `$CODEX_HOME` when set; its keychain item instead when Codex keeps the sign-in there) and `writeClaudeCredential`'s `.credentials.json` go through `writePrivate`, never a truncating `os.WriteFile`, so a machine going down mid-write leaves the old sign-in whole, and on Windows a file another program holds for a moment is waited out (`steady.Rename`) instead of the rotation being dropped while the refresh token on the disk is spent. A file that is there but doesn't read on a cold start is read from its `.bak` (`lastgood.Read`/`Fallback`): `logins.json`, `providers.json`, `migrations.json`, `settings.json`, `profiles.json`, `plugins.json`, `plugin-auth.json` and `mcp-signins.json`. `caller-keys.json` is flushed the same way 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. The recovery is logged and listed in `/api/state`'s `recovered`, which the window tells once (`recoveredOnce`). The `.bak` lags one write behind on purpose: a crash zeroes the latest write, so a copy made with it could be zeroed too. With no good `.bak` the file is unknown, never empty: a sign-out leaves `plugin-auth.json` or `mcp-signins.json` as it is (magpie's own returns the error), and a sign-in added copies the file aside before it writes.
- A state file whose reader takes one that doesn't unmarshal into its type as empty is copied aside before it is written over (`lastgood.KeepUnparsed` with `lastgood.Into[T]`, #2263): valid JSON of another shape, `[]` for a map, counts as not parsing. A copy that can't be made stops the write. These are `stash.json`, `applied.json` and `wsl.json` ([agent](../../internal/agent/)), `sessions-wsl.json` and `gateway-sessions-hidden.json` ([sessions](../../internal/sessions/)), `stepfun-auth.json`, `login-returns.json`, `quota-history.json`, `balance-history.json` and `credits-daily.json` ([provider](../../internal/provider/)), `claude-desktop.tiers.json`, `plugin-updates.json`, `usage-computer.json` and `sync-state.json`. Caches magpie fills again by itself (last quota readings, check-in and warm-up marks, plugin icons, npm and GitHub lookups, client shapes, seats, alert marks, `served.json`, `bun/state.json`) are still written over: losing one costs a refetch or a repeated notice.
- A state file whose reader takes one that doesn't unmarshal into its type as empty is copied aside before it is written over (`lastgood.KeepUnparsed` with `lastgood.Into[T]`, #2263): valid JSON of another shape, `[]` for a map, counts as not parsing. A copy that can't be made stops the write. These are `stash.json`, `applied.json`, `wsl.json` and Codex's `magpie-models.json` ([agent](../../internal/agent/), whose hand-added model keys `codexcat.Keep` can't hold when the file doesn't parse), `sessions-wsl.json` and `gateway-sessions-hidden.json` ([sessions](../../internal/sessions/)), `stepfun-auth.json`, `login-returns.json`, `quota-history.json`, `balance-history.json` and `credits-daily.json` ([provider](../../internal/provider/)), `claude-desktop.tiers.json`, `plugin-updates.json`, `usage-computer.json` and `sync-state.json`. Caches magpie fills again by itself (last quota readings, check-in and warm-up marks, plugin icons, npm and GitHub lookups, client shapes, seats, alert marks, `served.json`, `bun/state.json`) are still written over: losing one costs a refetch or a repeated notice.
- The gateway and a magpie run from a terminal change the same files (#1674). `loginsMu` is a [`filelock.Mutex`](../../internal/filelock/filelock.go): besides the mutex it holds an OS lock on `logins.json.lock` (flock, `LockFileEx` on Windows), and taking it has `logins.json` read afresh (`filememo.Look`), so every read a change is made from sees what another magpie wrote, and a refresh the gateway wrote is never written back over. `savedTokenMu` holds `logins.json.renew.lock` the same way, so two magpies never spend one refresh token. `caller-keys.json` (`access`) and `mcp-signins.json` (`mcpauth`, with `mcp-signins.json.renew.lock` around a renewal) are changed under such locks too; `plugin-auth.json` already was, under `plugin-auth.json.lock`, which `host.js` takes too. A lock another magpie holds is waited for up to `filelock.Wait` (10s), then gone on without, as before there was one. `rememberLogins` writes nothing when only `Seen` would move on and the saved one is less than `seenEvery` (10 minutes) old, so a run that only lists accounts (`magpie ls`, `providers`, `sessions`, `usage`) leaves `logins.json` as it is. [`logins_lock_test.go`](../../internal/provider/logins_lock_test.go) covers both, the second with another test process writing between this one's read and write.
- The account Codex or Claude Code is signed in to can be paused (`SetLoginOn` off, #263) only while another of its accounts is on. The gateway then sends it nothing: Codex's own-model turns go through routing, which passes over it (`OwnPaused`), and a native compaction (`/responses/compact`) goes to the first account on that isn't resting, signed as that account (`compactOn` in [codex_backend.go](../../internal/gateway/codex_backend.go)). The last other account off, the paused one is used again. A switched-off Codex provider is not a pause: Codex's own-model requests still relay on its sign-in as they came.
- A switched-off provider (`Off`, #163) stays saved. The gateway says it is off instead of saying the model is unknown.
+32 -3
View File
@@ -21,6 +21,7 @@ import (
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/lastgood"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
@@ -204,6 +205,28 @@ func codexEfforts(ms []catalog.Model, model string) []Option {
return append([]Option{d}, static(e...)...)
}
// catalogParses is whether b is a model list magpie writes for Codex, the
// shape Keep reads of the file there.
func catalogParses(b []byte) bool {
var v struct {
Models []map[string]any `json:"models"`
}
return json.Unmarshal(b, &v) == nil
}
// writeOwnList writes the model list magpie keeps for Codex at path. A file
// there that doesn't parse is copied aside first: Keep (#1450) holds
// nothing of a file it can't read, so writing the new list over one would
// end the keys the user added by hand with no copy (#2263's rule for
// magpie's state files, which this file had missed). An error stops the
// write.
func writeOwnList(path string, b []byte) error {
if err := lastgood.KeepUnparsed(path, catalogParses); err != nil {
return err
}
return edit.WriteAtomic(path, b)
}
func codexIn(at place) *Agent {
dir := at.codexHome()
path := filepath.Join(dir, "config.toml")
@@ -257,6 +280,12 @@ func codexIn(at place) *Agent {
}
b := list()
if string(codexcat.Keep(cur, b)) == string(b) {
// one that doesn't parse is copied aside before it goes: Keep
// held nothing of it, so removing it outright would end the
// user's own keys with no copy
if err := lastgood.KeepUnparsed(catalogPath, catalogParses); err != nil {
return
}
os.Remove(catalogPath)
}
}
@@ -323,7 +352,7 @@ func codexIn(at place) *Agent {
case asProvider() && ownCatalog(get("model_catalog_json")):
b := ownList()
if cur, _ := edit.Read(catalogPath); string(cur) != string(b) {
return edit.WriteAtomic(catalogPath, b)
return writeOwnList(catalogPath, b)
}
case viaBase():
return codexStaleCache(filepath.Join(dir, "models_cache.json"), provider.CodexListTag())
@@ -980,7 +1009,7 @@ func codexIn(at place) *Agent {
if err := putProvider(); err != nil {
return err
}
if err := edit.WriteAtomic(catalogPath, ownList()); err != nil {
if err := writeOwnList(catalogPath, ownList()); err != nil {
return err
}
// a thread started on Codex's built-in provider stays on it when
@@ -1177,7 +1206,7 @@ func codexIn(at place) *Agent {
}
b := ownList()
if cur, _ := edit.Read(catalogPath); string(cur) != string(b) {
if err := edit.WriteAtomic(catalogPath, b); err != nil {
if err := writeOwnList(catalogPath, b); err != nil {
return err
}
}
@@ -0,0 +1,53 @@
package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A magpie-models.json that doesn't parse is kept aside before magpie
// writes its list over the file or takes the file away: Keep (#1450) can
// hold nothing of a file it can't read, so the keys the user added by hand
// would end with no copy — the rule the other state files were given
// (#2263), which this file missed.
func TestUnparsedCodexCatalogKeptAside(t *testing.T) {
home, _ := codexHome(t, "", "")
list := filepath.Join(home, ".codex", "magpie-models.json")
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
// the file as it stands, broken the way a half-save or a sync conflict
// leaves one: a cut before the last brace, no longer a catalog
b := readFile(list)
writeFile(t, list, b[:len(b)-2])
// a provider added: the sync writes the list again
if err := provider.Save(provider.Provider{ID: "added", Name: "Added", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m2"}}); err != nil {
t.Fatal(err)
}
SyncCatalog()
if !strings.Contains(readFile(list), `"added/m2"`) {
t.Fatal("sync didn't write the new model")
}
if bad, _ := filepath.Glob(list + ".bad-*"); len(bad) == 0 {
t.Fatalf("the unparsed list was written over with no copy kept")
}
// the list broken again, and magpie steps out: the file goes, but a
// copy of what it held stays
writeFile(t, list, b[:len(b)-2])
if err := cx.Fields[0].Set(""); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(list); err == nil {
t.Error("a list magpie steps out of stayed")
}
if bad, _ := filepath.Glob(list + ".bad-*"); len(bad) == 0 {
t.Fatalf("the unparsed list was removed with no copy kept")
}
}