provider: a Codex auth.json is parsed once for its bytes, not on every look
codexWho (email and workspace) and codexLoginIn's user and plan are read
from an auth.json's bytes alone, yet were a JSON parse and a JWT decode on
every call: codexName runs sameLogin over every saved account against each
other, and liveLogin("codex") is asked from four loops at rest
(loginsNow, rememberLogins, heldByCodex, renewLogins). Both now go
through authMemo, a map keyed by the bytes (a hit makes no string and
allocates nothing), emptied at 64 entries so tokens renewed every hour
don't pile up.
Measured on mote with a copy of the real accounts (acidle.sh, alloc
between 60s and 180s at rest):
- t17 (0d99261dc): codexLoginIn 3.08MB, codexWho 2.56MB
- t18 (this): codexLoginIn 0.52MB, codexWho gone from the profile
/api/providers ×40 is the same (117.5MB vs 115.4MB, sampling noise).
Tests: TestCodexWhoKeptByBytes (two accounts and a malformed one read
right twice; a second read, also from another slice of the same bytes,
allocates 0 times) and TestAuthMemoBounded. Broken by hand: codexWho
reading past the memo makes the test red (29 allocs, want 0).
Run on mote at this tree: go vet ./internal/provider, gofmt -l clean,
go test -timeout 45m ./internal/provider ./internal/gui: gui ok; provider
ok on 2 of 3 full runs, the third red on
TestCodexAppWorkspaceSwitchReadsEachWorkspace/reclaimed=false/backHome=true
(ws-team's usage never read), which is #5819, filed on main before this
change; alone, -count=20 passes 20 of 20 both on origin/main and here.
Not tried: Windows and Linux (no OS-specific code touched).
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestCodexWhoKeptByBytes says codexWho reads an auth.json once for its
|
||||
// bytes: codexName compares every saved account with each other one, and
|
||||
// liveLogin is asked from four loops at rest, so each read was a JSON parse
|
||||
// and a JWT decode of the same few accounts. Another account's bytes read
|
||||
// their own email and workspace, and a malformed one reads nothing.
|
||||
func TestCodexWhoKeptByBytes(t *testing.T) {
|
||||
auth := func(email, ws string) json.RawMessage {
|
||||
b, _ := json.Marshal(map[string]any{"tokens": map[string]any{
|
||||
"id_token": fakeJWT(map[string]any{"email": email,
|
||||
"https://api.openai.com/auth": map[string]any{"chatgpt_account_id": ws}}),
|
||||
"access_token": "at"}})
|
||||
return b
|
||||
}
|
||||
a, b := auth("a@example.com", "ws-a"), auth("b@example.com", "ws-b")
|
||||
for range 2 {
|
||||
if e, ws := codexWho(a); e != "a@example.com" || ws != "ws-a" {
|
||||
t.Fatalf("a reads %q %q", e, ws)
|
||||
}
|
||||
if e, ws := codexWho(b); e != "b@example.com" || ws != "ws-b" {
|
||||
t.Fatalf("b reads %q %q", e, ws)
|
||||
}
|
||||
if e, ws := codexWho(json.RawMessage(`{`)); e != "" || ws != "" {
|
||||
t.Fatalf("a malformed auth reads %q %q", e, ws)
|
||||
}
|
||||
}
|
||||
if n := testing.AllocsPerRun(50, func() { codexWho(a) }); n != 0 {
|
||||
t.Errorf("a second read of the same auth allocates %v times, want 0", n)
|
||||
}
|
||||
// a fresh copy of the same bytes is the same account
|
||||
c := append(json.RawMessage(nil), a...)
|
||||
if n := testing.AllocsPerRun(50, func() { codexWho(c) }); n != 0 {
|
||||
t.Errorf("the same bytes in another slice allocate %v times, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAuthMemoBounded says the memo never holds more than authMemoMax
|
||||
// credentials, so tokens renewed every hour don't pile up in a magpie
|
||||
// running for weeks.
|
||||
func TestAuthMemoBounded(t *testing.T) {
|
||||
var m authMemo[int]
|
||||
for i := range 3 * authMemoMax {
|
||||
m.of([]byte{byte(i), byte(i >> 8)}, func() int { return i })
|
||||
if len(m.m) > authMemoMax {
|
||||
t.Fatalf("%d entries kept, want at most %d", len(m.m), authMemoMax)
|
||||
}
|
||||
}
|
||||
if got := m.of([]byte{1, 2}, func() int { return -1 }); got != -1 {
|
||||
t.Fatalf("a new key read %d, want what read gives", got)
|
||||
}
|
||||
}
|
||||
+64
-20
@@ -447,18 +447,55 @@ func sameLogin(a, b savedLogin) bool {
|
||||
return strings.EqualFold(a.User, b.User)
|
||||
}
|
||||
|
||||
// codexWho reads the email and workspace of a Codex auth.json.
|
||||
// codexWho reads the email and workspace of a Codex auth.json. What it
|
||||
// reads is the bytes' alone, so it is kept by them (authMemo): codexName
|
||||
// asks it of every saved account for each one it compares, and every look
|
||||
// at the signed-in account (liveLogin, from four loops at rest) asks it
|
||||
// again, each a JSON parse and a JWT decode of the same few accounts.
|
||||
func codexWho(auth json.RawMessage) (email, workspace string) {
|
||||
var a codexAuth
|
||||
if json.Unmarshal(auth, &a) != nil {
|
||||
return "", ""
|
||||
w := codexWhos.of(auth, func() [2]string {
|
||||
var a codexAuth
|
||||
if json.Unmarshal(auth, &a) != nil {
|
||||
return [2]string{}
|
||||
}
|
||||
id := jwtClaims(a.Tokens.IDToken)
|
||||
ws := a.Tokens.AccountID
|
||||
if ws == "" {
|
||||
ws = claimString(id, "https://api.openai.com/auth", "chatgpt_account_id")
|
||||
}
|
||||
return [2]string{claimString(id, "email"), ws}
|
||||
})
|
||||
return w[0], w[1]
|
||||
}
|
||||
|
||||
var codexWhos authMemo[[2]string]
|
||||
|
||||
// authMemo keeps what is read from a credential's bytes, by those bytes:
|
||||
// a few accounts' worth, emptied when it reaches authMemoMax, so a token
|
||||
// renewed every hour leaves nothing behind for long. The lookup by the
|
||||
// bytes makes no string, so a hit allocates nothing.
|
||||
type authMemo[T any] struct {
|
||||
sync.Mutex
|
||||
m map[string]T
|
||||
}
|
||||
|
||||
const authMemoMax = 64
|
||||
|
||||
func (c *authMemo[T]) of(b []byte, read func() T) T {
|
||||
c.Lock()
|
||||
v, ok := c.m[string(b)]
|
||||
c.Unlock()
|
||||
if ok {
|
||||
return v
|
||||
}
|
||||
id := jwtClaims(a.Tokens.IDToken)
|
||||
workspace = a.Tokens.AccountID
|
||||
if workspace == "" {
|
||||
workspace = claimString(id, "https://api.openai.com/auth", "chatgpt_account_id")
|
||||
v = read()
|
||||
c.Lock()
|
||||
if c.m == nil || len(c.m) >= authMemoMax {
|
||||
c.m = make(map[string]T)
|
||||
}
|
||||
return claimString(id, "email"), workspace
|
||||
c.m[string(b)] = v
|
||||
c.Unlock()
|
||||
return v
|
||||
}
|
||||
|
||||
// codexName is the name a Codex account goes by among the saved ones ls:
|
||||
@@ -808,24 +845,31 @@ func codexLoginIn(home string) (savedLogin, bool) {
|
||||
if err != nil {
|
||||
return savedLogin{}, false
|
||||
}
|
||||
var a codexAuth
|
||||
if json.Unmarshal(b, &a) != nil || a.Tokens.AccessToken == "" || a.AuthMode == "apikey" {
|
||||
return savedLogin{}, false
|
||||
}
|
||||
id := jwtClaims(a.Tokens.IDToken)
|
||||
user := codexUser(id)
|
||||
if user == "" {
|
||||
user = a.Tokens.AccountID
|
||||
}
|
||||
// read once for the bytes, as codexWho is: liveLogin is asked from
|
||||
// four loops at rest, each a parse and a JWT decode of one auth.json
|
||||
w := codexLogins.of(b, func() [2]string {
|
||||
var a codexAuth
|
||||
if json.Unmarshal(b, &a) != nil || a.Tokens.AccessToken == "" || a.AuthMode == "apikey" {
|
||||
return [2]string{}
|
||||
}
|
||||
id := jwtClaims(a.Tokens.IDToken)
|
||||
user := codexUser(id)
|
||||
if user == "" {
|
||||
user = a.Tokens.AccountID
|
||||
}
|
||||
return [2]string{user, claimString(id, "https://api.openai.com/auth", "chatgpt_plan_type")}
|
||||
})
|
||||
user := w[0]
|
||||
if user == "" {
|
||||
return savedLogin{}, false
|
||||
}
|
||||
l := savedLogin{Agent: "codex", User: user, Plan: claimString(id, "https://api.openai.com/auth", "chatgpt_plan_type"),
|
||||
Auth: json.RawMessage(bytes.TrimSpace(b))}
|
||||
l := savedLogin{Agent: "codex", User: user, Plan: w[1], Auth: json.RawMessage(bytes.TrimSpace(b))}
|
||||
l.User = codexLiveName(user, l.Auth)
|
||||
return l, true
|
||||
}
|
||||
|
||||
var codexLogins authMemo[[2]string]
|
||||
|
||||
// codexOtherHomes are the user's Codex folders but the one magpie and
|
||||
// Codex run with (appdir.CodexHomes): one ChatGPT account each, Codex run
|
||||
// in it with CODEX_HOME set.
|
||||
|
||||
Reference in New Issue
Block a user