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).
58 lines
2.1 KiB
Go
58 lines
2.1 KiB
Go
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)
|
|
}
|
|
}
|