provider: a saved Claude sign-in read before is not parsed again for each Providers poll
claudeSignedOut and claudeLoginVersion run for every saved Claude account
on each Providers poll (Logins, claudeStandIn), and each parsed the
sign-in whole through parseClaudeCredentials: a map[string]any, then a
Marshal and Unmarshal of claudeAiOauth. With real accounts that was
15.5 MB of what forty polls allocated. What they read of it, whether it
parses as a sign-in and its version, is now kept by the sign-in's bytes
(claudeAuthOf), at most 64; the bytes change only when the account is
signed in or refreshed, and a changed sign-in is read anew.
Run on mote at this tree: gofmt, go vet (darwin, windows, linux) of
internal/provider ok; TestClaudeSignedOutKeepsWhatItParsed alone 3/3 ok;
broken by hand (the kept read skipped) it fails "claudeSignedOut
allocated 8023 bytes, parsing the sign-in 7127"; go test ./internal/provider
ok (117.4s).
Real accounts, sandbox HOME, 40 Providers/Agents polls (abr.sh), against
the v0.1.2496 build: parseClaudeCredentials and claudeSignedOut gone from
the profile (15.5 MB before), provider.Logins 22.0 MB -> 11.0 MB, total
517 MB -> 504 MB; /api/state and the drift reply byte-identical.
Footprint peak 139 MB (was 134-141): this change doesn't move the peak.
Not run: the gui suite (no gui file changed).
Picked onto 2640ac0f0 (internal/provider changed there in allowance_renewed, key_allowance, plugins and routing, none of the files changed here); not run again there.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user