Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: rememberLogins checks the time before it takes logins.json's lock
yetonec087834d6f45
diff --git a/internal/provider/claude_auth_test.go b/internal/provider/claude_auth_test.goindex 201b3d1aa..4f393dad5 100644--- a/internal/provider/claude_auth_test.go+++ b/internal/provider/claude_auth_test.go@@ -160,7 +160,7 @@ func TestClaudeOwnLoginLapsesOnlyForWhatHappened(t *testing.T) {look := func() {forgetClaudeCredential()loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()rememberLogins(true)}diff --git a/internal/provider/claude_logout_test.go b/internal/provider/claude_logout_test.goindex a038369aa..9ce6edaac 100644--- a/internal/provider/claude_logout_test.go+++ b/internal/provider/claude_logout_test.go@@ -30,7 +30,7 @@ func TestClaudeAccountsOutliveLogout(t *testing.T) {{"agent": "claude", "user": "[email protected]", "plan": "max", "on": true, "seen": seen.Add(-time.Hour), "auth": oauth("tok-two")},})loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()if p, ok := find(All(), "claude"); !ok || p.Account.User != "[email protected]" {t.Fatalf("signed in: %v %+v", ok, p.Account)@@ -41,7 +41,7 @@ func TestClaudeAccountsOutliveLogout(t *testing.T) {os.Remove(filepath.Join(home, ".claude.json"))forgetClaudeCredential()loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()// the one Claude Code held went with its /logout, which revokes thediff --git a/internal/provider/claude_status_test.go b/internal/provider/claude_status_test.goindex 90ba144c4..7f5aa89ce 100644--- a/internal/provider/claude_status_test.go+++ b/internal/provider/claude_status_test.go@@ -37,7 +37,7 @@ func TestClaudeAccountNamedAsItsLogin(t *testing.T) {testenv.Program(t, exe, "#!/bin/sh\necho \"token=$ANTHROPIC_AUTH_TOKEN\" > "+env+"\necho '{\"loggedIn\":true,\"authMethod\":\"oauth_token\",\"apiProvider\":\"firstParty\"}'\n")claudeExecutable = func() string { return exe }loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()p, ok := claudeAccount()diff --git a/internal/provider/left_alone_test.go b/internal/provider/left_alone_test.goindex 6231768db..77019dc7c 100644--- a/internal/provider/left_alone_test.go+++ b/internal/provider/left_alone_test.go@@ -56,7 +56,7 @@ func TestLeftAloneRunsNoCLI(t *testing.T) {c.Unlock()}loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()}loop := func() {diff --git a/internal/provider/logins.go b/internal/provider/logins.goindex e329ea112..d2ecd61db 100644--- a/internal/provider/logins.go+++ b/internal/provider/logins.go@@ -23,6 +23,7 @@ import ("sort""strings""sync"+ "sync/atomic""time""github.com/yetone/magpie/internal/appdir"@@ -130,9 +131,19 @@ var (Path: func() string { return loginsPath() + ".lock" },Locked: func() { filememo.Look(loginsPath()) },}- loginsSeenAt time.Time+ // loginsSeenAt is when rememberLogins last looked at the agents'+ // sign-ins (UnixNano), 0 for not since this magpie started or since+ // a change made the next look due at once. It is read without+ // loginsMu, so a look not yet due takes no lock (#1665).+ loginsSeenAt atomic.Int64)+// loginsLookDue says the 30 seconds since the last look are up.+func loginsLookDue() bool {+ at := loginsSeenAt.Load()+ return at == 0 || time.Since(time.Unix(0, at)) >= 30*time.Second+}+// switchable agents: those whose sign-in magpie can save and put back.var loginAgents = []string{"claude", "codex"}@@ -720,12 +731,20 @@ func rememberLogins(force bool) {if settings.AccountDiscoveryOff() {return}+ // the time first, and the lock only for a look that is due: taken+ // first, every ask (each list of accounts, each page's read) made and+ // locked logins.json.lock, ~370 times in a start's first 25s on+ // Windows, where an antivirus' filter driver works on each (#1665)+ if !force && !loginsLookDue() {+ return+ }loginsMu.Lock()defer loginsMu.Unlock()- if !force && time.Since(loginsSeenAt) < 30*time.Second {+ // another ask may have looked while this one waited for the lock+ if !force && !loginsLookDue() {return}- loginsSeenAt = time.Now()+ loginsSeenAt.Store(time.Now().UnixNano())was := readLogins()ls := slices.Clone(was)now := time.Now().UTC().Truncate(time.Second)@@ -1137,7 +1156,7 @@ func switchSavedLogin(agent, user string) (from string, _ error) {if err != nil {return "", err}- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)forgetAccountCaches()return from, nil}@@ -1312,7 +1331,7 @@ func loginAgentName(agent string) string {// a caller that changed a sign-in behind magpie's back (a test's home).func ForgetAccounts() {loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()// an account asked for its list a moment ago is asked again: the// accounts read afresh may be others under the same iddiff --git a/internal/provider/logins_due_test.go b/internal/provider/logins_due_test.gonew file mode 100644index 000000000..736ebaa51--- /dev/null+++ b/internal/provider/logins_due_test.go@@ -0,0 +1,40 @@+package provider++import (+ "testing"+ "time"+)++// #1665: rememberLogins took loginsMu, and with it made and locked+// logins.json.lock, before it looked whether 30 seconds had passed since+// its last look: every list of accounts did, ~370 times in a Windows+// start's first 25s, each a file an antivirus' filter driver works on.+// A look that isn't due takes no lock: it returns while another holds it.+func TestRememberLoginsNotDueTakesNoLock(t *testing.T) {+ signIn(t)+ rememberLogins(true) // a look, just now++ loginsMu.Lock() // another, writing logins.json+ done := make(chan struct{})+ go func() {+ for range 100 {+ rememberLogins(false)+ }+ close(done)+ }()+ select {+ case <-done:+ case <-time.After(5 * time.Second):+ loginsMu.Unlock()+ <-done+ t.Fatal("a look not due waited for logins.json's lock")+ }+ loginsMu.Unlock()++ // due again, it looks, under the lock+ loginsSeenAt.Store(time.Now().Add(-31 * time.Second).UnixNano())+ rememberLogins(false)+ if loginsLookDue() {+ t.Fatal("a due look didn't look")+ }+}diff --git a/internal/provider/logins_lock_test.go b/internal/provider/logins_lock_test.goindex 205016cbd..515c66fec 100644--- a/internal/provider/logins_lock_test.go+++ b/internal/provider/logins_lock_test.go@@ -17,7 +17,7 @@ import (// remembered of when the accounts were last looked at.func newProcess() {loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()}diff --git a/internal/provider/quota_history_background_test.go b/internal/provider/quota_history_background_test.goindex e981f62a2..fc5c90ded 100644--- a/internal/provider/quota_history_background_test.go+++ b/internal/provider/quota_history_background_test.go@@ -42,7 +42,7 @@ func TestBackgroundLoginReadNotesQuotaHistory(t *testing.T) {// rememberLogins is memoized for 30s process-wide; another test may// have just run it, and then this account is never saved and read.loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()// routing's read, no Usage page involved@@ -126,7 +126,7 @@ func TestHistoryWriteHoldsNoWaiter(t *testing.T) {t.Cleanup(func() { CodexBase = old })loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()release := make(chan struct{})diff --git a/internal/provider/quota_history_waiter_test.go b/internal/provider/quota_history_waiter_test.goindex f8bb82d95..e9aba6a3f 100644--- a/internal/provider/quota_history_waiter_test.go+++ b/internal/provider/quota_history_waiter_test.go@@ -48,7 +48,7 @@ func TestHistoryWriteHoldsNoReadingWaiter(t *testing.T) {t.Cleanup(func() { CodexBase = old })loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()noteEntered := make(chan struct{})@@ -133,7 +133,7 @@ func TestCachedBatchNoteSkipsTheRead(t *testing.T) {t.Cleanup(func() { CodexBase = old })loginsMu.Lock()- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)loginsMu.Unlock()ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)diff --git a/internal/provider/signin.go b/internal/provider/signin.goindex ac5a3292b..7b6acfec7 100644--- a/internal/provider/signin.go+++ b/internal/provider/signin.go@@ -921,7 +921,7 @@ func addLogin(l savedLogin) (using bool, err error) {// agent's own is an older sign-in, and gives wayforgetClaudeDir(l.User)}- loginsSeenAt = time.Time{}+ loginsSeenAt.Store(0)forgetAccountCaches()return using, nil}