diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index c2925794c..dea5b3a95 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -43,6 +43,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi - Each refresh token has exactly one holder. Vendors rotate tokens on refresh, so two copies of one token would sign each other out. `savedTokenMu` stops two requests refreshing one saved account at once. - A Claude subscription account is used only for Claude Code, through the genuine Claude Code binary (see [Claude subscription bridge](claude-subscription-bridge.md)). - 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, `keepUnreadLogins` copies the original file aside, including valid JSON with invalid account field types, so its credentials can be recovered. - A switched-off provider (`Off`, #163) stays saved. The gateway says it is off instead of saying the model is unknown. - A provider removed or switched off takes its models out of the catalog. [Agent wiring](agent-wiring.md)'s `Reseat` moves agents off them. - A gateway key held to some accounts (#905) is held to those of the providers its list names: a provider it names no account of, the key uses as it always did. The list keeps an account by its stable id — `logins.json`'s `id`, set once written and kept through renames; the agents that keep no logins (and plugins) are known by a hash of the account's name, so an account renamed there drops out of a key's list, holding it closer, never wider. Storing an id for every agent is a larger change, not this one's. diff --git a/internal/provider/logins.go b/internal/provider/logins.go index a474e0a49..cbbef075b 100644 --- a/internal/provider/logins.go +++ b/internal/provider/logins.go @@ -189,7 +189,12 @@ func readLogins() []savedLogin { // is written over, so the accounts in it can still be got back. func keepUnreadLogins(path string) { b, err := os.ReadFile(path) - if err != nil || json.Valid(b) { + if err != nil { + return + } + // Match readLogins: valid JSON can still have unreadable field types. + var ls []savedLogin + if json.Unmarshal(b, &ls) == nil { return } bad := path + ".bad-" + time.Now().Format("20060102-150405") diff --git a/internal/provider/logins_unread_test.go b/internal/provider/logins_unread_test.go index a71fe596d..dedd90172 100644 --- a/internal/provider/logins_unread_test.go +++ b/internal/provider/logins_unread_test.go @@ -1,6 +1,7 @@ package provider import ( + "encoding/json" "os" "path/filepath" "slices" @@ -8,6 +9,75 @@ import ( "testing" ) +// Valid JSON can still be unreadable as saved accounts. On a cold start, +// adding an account must keep those original credentials in a backup. +func TestUnreadLoginsFieldTypesKeptBeforeWrite(t *testing.T) { + for _, tc := range []struct { + name string + text string + }{ + {"boolean as string", `[{"agent":"antigravity","user":"old@example.com","on":"true","auth":{"refresh_token":"old-refresh-token"}}]`}, + {"invalid time", `[{"agent":"antigravity","user":"old@example.com","seen":"yesterday","auth":{"refresh_token":"old-refresh-token"}}]`}, + {"object instead of list", `{"agent":"antigravity","user":"old@example.com","auth":{"refresh_token":"old-refresh-token"}}`}, + } { + t.Run(tc.name, func(t *testing.T) { + googleSandbox(t, &fakeGoogle{}) + if !json.Valid([]byte(tc.text)) { + t.Fatal("fixture must be syntactically valid JSON") + } + p := loginsPath() + if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(tc.text), 0o600); err != nil { + t.Fatal(err) + } + lastLoginsMu.Lock() + lastLogins = nil // magpie just started: no previously read accounts + lastLoginsMu.Unlock() + if err := addGoogleLogin("antigravity", "new@example.com", "", googleAuth{RefreshToken: "new-refresh-token", Project: "p"}); err != nil { + t.Fatal(err) + } + kept, err := filepath.Glob(p + ".bad-*") + if err != nil { + t.Fatal(err) + } + if len(kept) != 1 { + t.Fatalf("unreadable saved accounts not kept before adding an account: %v", kept) + } + b, err := os.ReadFile(kept[0]) + if err != nil { + t.Fatal(err) + } + if string(b) != tc.text { + t.Fatalf("backup = %q, want original credentials %q", b, tc.text) + } + if ls := readLogins(); len(ls) != 1 || ls[0].User != "new@example.com" { + t.Fatalf("new account not saved: %v", ls) + } + }) + } +} + +func TestReadableLoginsNotKeptAside(t *testing.T) { + googleSandbox(t, &fakeGoogle{}) + for _, u := range []string{"a@x.com", "b@x.com"} { + if err := addGoogleLogin("antigravity", u, "", googleAuth{RefreshToken: "1//" + u, Project: "p"}); err != nil { + t.Fatal(err) + } + } + kept, err := filepath.Glob(loginsPath() + ".bad-*") + if err != nil { + t.Fatal(err) + } + if len(kept) != 0 { + t.Fatalf("readable saved accounts unnecessarily backed up: %v", kept) + } + if ls := readLogins(); len(ls) != 2 { + t.Fatalf("readable accounts lost when adding an account: %v", ls) + } +} + // A logins.json that doesn't parse (half written, or written by something // else) was read as no accounts, and the next account added wrote the // file anew with that one alone: every saved sign-in gone.