chenhg5/cc-connect · error
parse auth.json: %w
Error message
parse auth.json: %w
What it means
After reading auth.json, readOAuthTokens unmarshals the bytes into a struct expecting {"tokens":{"access_token":...,"account_id":...}}. This error wraps the json.Unmarshal failure, so the cause carries the exact JSON syntax problem (offset, unexpected token, type mismatch). It is thrown whenever the file exists but is not valid JSON matching the expected shape.
Source
Thrown at agent/codex/usage.go:78
func (a *Agent) readOAuthTokens(readFile func(string) ([]byte, error)) (codexOAuthTokens, error) {
path, err := codexAuthPath()
if err != nil {
return codexOAuthTokens{}, err
}
data, err := readFile(path)
if err != nil {
return codexOAuthTokens{}, fmt.Errorf("read %s: %w", path, err)
}
var payload struct {
Tokens struct {
AccessToken string `json:"access_token"`
AccountID string `json:"account_id"`
} `json:"tokens"`
}
if err := json.Unmarshal(data, &payload); err != nil {
return codexOAuthTokens{}, fmt.Errorf("parse auth.json: %w", err)
}
if strings.TrimSpace(payload.Tokens.AccessToken) == "" {
return codexOAuthTokens{}, fmt.Errorf("auth.json missing tokens.access_token")
}
if strings.TrimSpace(payload.Tokens.AccountID) == "" {
return codexOAuthTokens{}, fmt.Errorf("auth.json missing tokens.account_id")
}
return codexOAuthTokens{
AccessToken: payload.Tokens.AccessToken,
AccountID: payload.Tokens.AccountID,
}, nil
}
func (a *Agent) fetchUsage(ctx context.Context, client *http.Client, tokens codexOAuthTokens) (*core.UsageReport, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, codexUsageURL, nil)
if err != nil {
return nil, errView on GitHub (pinned to 4000b2338a)
Solutions
- Re-run `codex login` to regenerate a valid auth.json
- Validate the file: `python3 -m json.tool ~/.codex/auth.json` to see the syntax error
- Restore from backup or re-copy the auth.json from the machine where Codex was logged in
- Check for editors/scripts that may have mangled the file (BOM, truncation)
Example fix
// before
// silently ignores malformed auth.json until GetUsage fails
usage, err := agent.GetUsage(ctx)
// after: pre-validate JSON before calling
var probe map[string]any
if err := json.Unmarshal(authBytes, &probe); err != nil {
log.Fatalf("auth.json is not valid JSON: %v — run `codex login`", err)
}
usage, err := agent.GetUsage(ctx) Defensive patterns
Strategy: validation
Validate before calling
var probe any
if err := json.Unmarshal(b, &probe); err != nil { return fmt.Errorf("auth.json is not valid JSON: %w", err) } Try / catch
if err := json.Unmarshal(data, &payload); err != nil {
var je *json.SyntaxError
if errors.As(err, &je) { return fmt.Errorf("auth.json invalid at offset %d: %v", je.Offset, je) }
return fmt.Errorf("auth.json unreadable, re-run `codex login`: %w", err)
} Prevention
- Never hand-edit auth.json; regenerate via `codex login`
- Validate JSON after copying auth.json between machines (`python3 -m json.tool`)
- Watch for editors introducing BOM/truncation
- Back up auth.json before Codex CLI upgrades
When it happens
Trigger: json.Unmarshal(data, &payload) returns an error inside readOAuthTokens, e.g. auth.json contains truncated content, invalid JSON syntax, or a top-level JSON array/string instead of an object. Callers: GetUsage and the InvalidJSON test.
Common situations: auth.json corrupted by a partial write or disk-full during `codex login`; file manually edited and syntax broken; another tool overwrote auth.json with a different format; encoding issues (BOM) after editing on Windows.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- read %s: %w
- auth.json missing tokens.access_token
- auth.json missing tokens.account_id
- decode usage response: %w
- parse existing Agy hooks %s: %w
AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06).
Data as JSON: /api/errors/fefcc19e3702d84d.
Report an issue: GitHub.