diff --git a/internal/provider/claude_auth.go b/internal/provider/claude_auth.go index 2b3c0abf5..83706f8ef 100644 --- a/internal/provider/claude_auth.go +++ b/internal/provider/claude_auth.go @@ -15,6 +15,7 @@ import ( "crypto/sha256" "encoding/hex" "strings" + "sync" ) // claudeAuthLapse is why a saved Claude account whose credential Anthropic @@ -56,8 +57,8 @@ func (c claudeCredentials) cleared() bool { } func claudeLoginVersion(l savedLogin) string { - if c, ok := parseClaudeCredentials(l.Auth); ok { - return c.version() + if f := claudeAuthOf(l.Auth); f.ok { + return f.version } if tok := claudeSetupToken(l.Auth); tok != "" { return claudeTokenVersion(tok) @@ -65,6 +66,45 @@ func claudeLoginVersion(l savedLogin) string { return "" } +// claudeAuthFacts is what claudeSignedOut and claudeLoginVersion read of a +// saved sign-in: whether it parses as one (parseClaudeCredentials), and +// its version. +type claudeAuthFacts struct { + ok bool + version string +} + +// claudeAuthKept keeps claudeAuthFacts by the sign-in's bytes. Both are +// read for every saved Claude account on each Providers poll, and parsing +// each sign-in whole for them was 15 MB of what forty polls allocated with +// real accounts. The bytes change only when the account is signed in or +// refreshed; at most claudeAuthKeep are kept. +var claudeAuthKept struct { + sync.Mutex + m map[string]claudeAuthFacts +} + +const claudeAuthKeep = 64 + +func claudeAuthOf(auth []byte) claudeAuthFacts { + claudeAuthKept.Lock() + f, hit := claudeAuthKept.m[string(auth)] + claudeAuthKept.Unlock() + if hit { + return f + } + if c, ok := parseClaudeCredentials(auth); ok { + f = claudeAuthFacts{ok: true, version: c.version()} + } + claudeAuthKept.Lock() + if claudeAuthKept.m == nil || len(claudeAuthKept.m) >= claudeAuthKeep { + claudeAuthKept.m = map[string]claudeAuthFacts{} + } + claudeAuthKept.m[string(auth)] = f + claudeAuthKept.Unlock() + return f +} + // ClaudeLoginVersion is the credential the saved Claude account user has // now, which a run started on it goes by (NoteClaudeSignInFailure). func ClaudeLoginVersion(user string) string { diff --git a/internal/provider/claude_auth_kept_test.go b/internal/provider/claude_auth_kept_test.go new file mode 100644 index 000000000..d279af348 --- /dev/null +++ b/internal/provider/claude_auth_kept_test.go @@ -0,0 +1,42 @@ +package provider + +import ( + "strings" + "testing" +) + +// TestClaudeSignedOutKeepsWhatItParsed: claudeSignedOut and +// claudeLoginVersion run for every saved Claude account on each Providers +// poll, and read a sign-in they have read before without parsing it again, +// while a sign-in changed since is read anew. +func TestClaudeSignedOutKeepsWhatItParsed(t *testing.T) { + auth := []byte(`{"claudeAiOauth":{"accessToken":"sk-ant-oat01-` + strings.Repeat("a", 100) + + `","refreshToken":"sk-ant-ort01-b","expiresAt":1,"scopes":["user:inference","user:profile"],"subscriptionType":"max"},"mcpOAuth":{"x":{"accessToken":"m"}}}`) + l := savedLogin{Agent: "claude", User: "a@example.com", Auth: auth, Lapsed: claudeAuthLapse, Refused: "old"} + parsed := allocatedBy(func() { + c, _ := parseClaudeCredentials(auth) + _ = c.version() + _, _ = parseClaudeCredentials(auth) + }) + read := allocatedBy(func() { _ = claudeSignedOut(l) }) + if read*4 > parsed { + t.Fatalf("claudeSignedOut allocated %d bytes, parsing the sign-in %d", read, parsed) + } + if got := claudeSignedOut(l); got != "" { + t.Fatalf("a credential other than the refused one reads %q", got) + } + c, _ := parseClaudeCredentials(auth) + l.Refused = c.version() + if got := claudeSignedOut(l); got != claudeAuthLapse { + t.Fatalf("the refused credential reads %q", got) + } + // signed in again: its new credential isn't the one refused + l.Auth = []byte(strings.Replace(string(auth), "ort01-b", "ort01-c", 1)) + if got := claudeSignedOut(l); got != "" { + t.Fatalf("a credential changed since reads %q", got) + } + l.Lapsed, l.Refused, l.Auth = "", "", []byte(`{"claudeAiOauth":{"accessToken":""}}`) + if got := claudeSignedOut(l); got != claudeGoneLapse { + t.Fatalf("an emptied sign-in reads %q", got) + } +} diff --git a/internal/provider/claude_dirs.go b/internal/provider/claude_dirs.go index 64db92ac3..1bc848c18 100644 --- a/internal/provider/claude_dirs.go +++ b/internal/provider/claude_dirs.go @@ -389,7 +389,7 @@ func claudeSignedOut(l savedLogin) string { } return l.Lapsed } - if _, ok := parseClaudeCredentials(l.Auth); ok || claudeSetupToken(l.Auth) != "" { + if claudeAuthOf(l.Auth).ok || claudeSetupToken(l.Auth) != "" { return "" } return claudeGoneLapse