Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
provider: a CLI's sign-in is asked again only when its files change (#3185)
yetone6fc532198fcf
diff --git a/internal/provider/cli_identity.go b/internal/provider/cli_identity.goindex 9ccdb4daf..436955514 100644--- a/internal/provider/cli_identity.go+++ b/internal/provider/cli_identity.go@@ -26,9 +26,15 @@ import (// signed in all the same, as no token is kept (#1278).type cliIdentity struct {sync.Mutex- name string- exe func() string- ask func() (user, plan string, ok bool, err error) // err: couldn't tell+ name string+ exe func() string+ ask func() (user, plan string, ok bool, err error) // err: couldn't tell+ // mark, when set, is a cheap fingerprint of where the CLI keeps its+ // sign-in (a token, a file's size and time): while it reads as it did+ // at the last answer, nobody can have signed in or out, and the CLI is+ // asked again only after unchangedAsk+ mark func() string+ markAt string // mark when the answer served was asked; "" unknownat time.Timerefreshing booldone chan struct{} // closed when the ask under way has answered@@ -130,12 +136,24 @@ func (c *cliIdentity) get() (user, plan string, ok bool) {}c.Lock()}- } else if time.Since(c.at) > max(time.Minute, c.retry) {+ } else if since := time.Since(c.at); since > max(time.Minute, c.retry) && !c.unchanged(since) {c.refresh()}return c.user, c.plan, c.ok}+// unchanged says the CLI's sign-in reads as it did when the answer served+// was asked, and that was less than unchangedAsk ago. Called with c locked.+func (c *cliIdentity) unchanged(since time.Duration) bool {+ return c.mark != nil && c.markAt != "" && since < unchangedAsk && c.mark() == c.markAt+}++// unchangedAsk is how often a CLI whose sign-in reads the same is asked+// anyway, for a plan changed on the vendor's side. Asked every minute,+// cursor-agent alone, a Node program asking Cursor's servers, took ~0.5s of+// CPU and 200 MB each time, for as long as magpie ran (#3185).+const unchangedAsk = 30 * time.Minute+// firstAsk is how long a look waits for a CLI never answered before.var firstAsk = 3 * time.Second@@ -154,7 +172,13 @@ func (c *cliIdentity) refresh() chan struct{} {}c.refreshing, c.done = true, make(chan struct{})gen, done := c.gen, c.done+ mark := c.markgo func() {+ // read before the ask, so a sign-in during it is asked again+ var m string+ if mark != nil {+ m = mark()+ }start := time.Now()u, p, ok, err := c.ask()slow := time.Since(start) >= slowAsk@@ -173,8 +197,10 @@ func (c *cliIdentity) refresh() chan struct{} {c.retry = 0}if err != nil {- return // what was served stays+ c.markAt = "" // couldn't tell: asked on the next minute's look+ return // what was served stays}+ c.markAt = mc.user, c.plan, c.ok = u, p, okc.keep()}()@@ -189,5 +215,6 @@ func (c *cliIdentity) forget() {c.gen++c.refreshing, c.waited = false, falsec.retry = 0+ c.markAt = ""c.Unlock()}diff --git a/internal/provider/cli_identity_test.go b/internal/provider/cli_identity_test.goindex 8481d73fe..be13d094d 100644--- a/internal/provider/cli_identity_test.go+++ b/internal/provider/cli_identity_test.go@@ -239,3 +239,52 @@ func TestCLIIdentityNotKeptInATestBinary(t *testing.T) {t.Fatalf("written in a test binary: %v", es)}}++// A CLI whose sign-in reads the same isn't started every minute: asked+// every minute, cursor-agent took ~0.5s of CPU and 200 MB each time for as+// long as magpie ran (#3185). A sign-in or sign-out in it is asked at the+// next look a minute on, and the same one again after unchangedAsk.+func TestCLIIdentityAskedOnASignInChange(t *testing.T) {+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())+ var asked atomic.Int32+ var mark atomic.Value+ mark.Store("a")+ c := &cliIdentity{name: "x", exe: func() string { return "/bin/sh" },+ ask: func() (string, string, bool, error) { asked.Add(1); return "[email protected]", "Pro", true, nil },+ mark: func() string { return mark.Load().(string) }}+ look := func(ago time.Duration) int32 {+ c.Lock()+ c.at = time.Now().Add(-ago)+ c.Unlock()+ c.get()+ c.Lock()+ done, busy := c.done, c.refreshing+ c.Unlock()+ if busy {+ <-done+ }+ return asked.Load()+ }+ if c.get(); asked.Load() != 1 {+ t.Fatalf("first look asked %d times", asked.Load())+ }+ for i := 0; i < 5; i++ {+ if n := look(61 * time.Second); n != 1 {+ t.Fatalf("sign-in unchanged, a minute on: asked %d times", n)+ }+ }+ mark.Store("b") // signed in as someone else+ if n := look(61 * time.Second); n != 2 {+ t.Fatalf("after a sign-in: asked %d times", n)+ }+ if n := look(61 * time.Second); n != 2 {+ t.Fatal("asked again with the new sign-in unchanged")+ }+ if n := look(unchangedAsk + time.Second); n != 3 {+ t.Fatalf("after %v: asked %d times", unchangedAsk, n)+ }+ mark.Store("") // unreadable: asked as before+ if n := look(61 * time.Second); n != 4 {+ t.Fatalf("mark unreadable: asked %d times", n)+ }+}diff --git a/internal/provider/cursor.go b/internal/provider/cursor.goindex 72583a679..30fe75514 100644--- a/internal/provider/cursor.go+++ b/internal/provider/cursor.go@@ -56,7 +56,29 @@ func isCursorAgent(path string) bool {return err == nil && strings.Contains(real, "cursor-agent")}-var cursorStatus = &cliIdentity{name: "cursor", exe: func() string { return CursorExecutable() }, ask: askCursorStatus}+var cursorStatus = &cliIdentity{name: "cursor", exe: func() string { return CursorExecutable() }, ask: askCursorStatus, mark: cursorSignInMark}++// cursorSignInMark is who cli-config.json says signed in, its authInfo, as+// the mark cliIdentity asks again on a change of: about rewrites the file+// on every run, so its size and time can't be the mark. "" when it can't+// be read.+func cursorSignInMark() string {+ home, _ := os.UserHomeDir()+ b, err := os.ReadFile(filepath.Join(home, ".cursor", "cli-config.json"))+ if err != nil {+ if errors.Is(err, os.ErrNotExist) {+ return "none"+ }+ return ""+ }+ var c struct {+ AuthInfo json.RawMessage `json:"authInfo"`+ }+ if json.Unmarshal(b, &c) != nil {+ return "" // half-written: asked+ }+ return "auth:" + string(c.AuthInfo)+}// cursorIdentity is who Cursor's CLI says is signed in; see cliIdentity.func cursorIdentity() (user, plan string, ok bool) {diff --git a/internal/provider/devin.go b/internal/provider/devin.goindex 14d74c0fa..0fb041dab 100644--- a/internal/provider/devin.go+++ b/internal/provider/devin.go@@ -15,6 +15,8 @@ package providerimport ("context"+ "crypto/sha256"+ "encoding/hex""encoding/json""errors""fmt"@@ -64,7 +66,21 @@ func DevinCredentialsPath() string {return filepath.Join(base, "devin", "credentials.toml")}-var devinStatus = &cliIdentity{name: "devin", exe: func() string { return DevinExecutable() }, ask: askDevinStatus}+var devinStatus = &cliIdentity{name: "devin", exe: func() string { return DevinExecutable() }, ask: askDevinStatus, mark: devinSignInMark}++// devinSignInMark is the key credentials.toml keeps, hashed, as the mark+// cliIdentity asks again on a change of; "" when it can't be read.+func devinSignInMark() string {+ b, err := readAccountFile(DevinCredentialsPath())+ if errors.Is(err, os.ErrNotExist) {+ return "none"+ }+ if err != nil {+ return ""+ }+ sum := sha256.Sum256(b)+ return hex.EncodeToString(sum[:8])+}// devinIdentity is who Devin's CLI says is signed in; see cliIdentity.func devinIdentity() (user, plan string, ok bool) {