provider: a CLI's sign-in is asked again only when its files change (#3185)

magpie asked each agent CLI who is signed in once a minute, for as long
as it ran. cursor-agent is a Node program that asks Cursor's servers:
~0.5s of CPU and ~200 MB each time. devin auth status took ~1.9s wall.
On a laptop that is a process spawned every minute, all day.

cliIdentity now takes a mark: a cheap fingerprint of where the CLI keeps
its sign-in (Cursor: authInfo in ~/.cursor/cli-config.json; Devin: a hash
of its credentials file). While the mark reads as it did when the answer
was asked, nobody can have signed in or out, and the CLI is asked again
only after 30 minutes. A changed mark, or one that can't be read, is
asked at once as before. The mark is a file read, never the keychain, so
nothing that can block runs under the identity's lock.

Run:
- TestCLIIdentityAskedOnASignInChange: red with unchanged() forced false
  ("sign-in unchanged, a minute on: asked 2 times"), green with the fix.
- go test ./internal/provider -run 'CLIIdentity|Cursor|Devin': ok.
- Real use: magpie serve in a sandbox HOME holding copies of the owner's
  Devin and Cursor CLI installs and sign-ins, Providers open in WebKit,
  every child of serve logged every 0.3s for 5 minutes, caches warm:
  origin/main b1a2eec2c: devin auth status 3 times, serve CPU 12.66s
  this change:           devin auth status 0 times, serve CPU 12.32s
- Not tried: cursor-agent about in that run; the sandbox's Cursor plugin
  wasn't signed in, so Cursor's path is covered by the unit test only.
- Full suite at 4354a7db2 (on origin/main b1a2eec2c): go test -timeout 45m
  ./..., 68 packages ok (gateway 660s, agent 530s, provider 272s).
  gofmt -l clean; go vet for darwin, and linux and windows -tags nogui.
- Rebased onto 21fb17e4d (only internal/gateway/gateway.go and a test
  changed); at the pushed head go build ./... and go test
  ./internal/provider -run 'CLIIdentity|Cursor|Devin' ok.
This commit is contained in:
yetone
2026-10-22 02:25:34 +08:00
committed by yetone
parent 21fb17e4df
commit 6fc532198f
4 changed files with 121 additions and 7 deletions
+32 -5
View File
@@ -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; "" unknown
at time.Time
refreshing bool
done 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.mark
go 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 = m
c.user, c.plan, c.ok = u, p, ok
c.keep()
}()
@@ -189,5 +215,6 @@ func (c *cliIdentity) forget() {
c.gen++
c.refreshing, c.waited = false, false
c.retry = 0
c.markAt = ""
c.Unlock()
}
+49
View File
@@ -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 "me@example.com", "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)
}
}
+23 -1
View File
@@ -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) {
+17 -1
View File
@@ -15,6 +15,8 @@ package provider
import (
"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) {