earendil-works/pi · error · Error

Failed to read auth.json

Error message

Failed to read auth.json: ${error instanceof Error ? error.message : String(error)}

What it means

Thrown by AuthStorage.load when reading auth.json fails with an error other than ENOENT (which is treated as empty): JSON parse failures, EACCES, or other I/O problems. The underlying error message is appended; the offending file path is this.authPath (default: agent dir auth.json).

Solutions

  1. Open auth.json and fix or remove the malformed JSON
  2. Check file permissions if reading failed
  3. Delete auth.json to start fresh (existing logins are lost) and re-login
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at packages/coding-agent/src/core/auth-storage.ts:222 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24). Data as JSON: /api/errors/3d16dc40ee785268. Report an issue: GitHub.

Appendix: source

Thrown at packages/coding-agent/src/core/auth-storage.ts:222

	private readonly authPath: string;
	private data: AuthStorageData | undefined;

	constructor(authPath: string = join(getAgentDir(), "auth.json")) {
		this.authPath = normalizePath(authPath);
	}

	private load(): AuthStorageData {
		if (this.data) return this.data;

		let parsed: unknown;
		try {
			parsed = JSON.parse(stripBom(readFileSync(this.authPath, "utf-8")));
		} catch (error) {
			if ((error as NodeJS.ErrnoException).code === "ENOENT") {
				this.data = {};
				return this.data;
			}
			throw new Error(`Failed to read auth.json: ${error instanceof Error ? error.message : String(error)}`);
		}

		if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
			throw new Error("Invalid auth.json: expected an object");
		}
		for (const [providerId, credential] of Object.entries(parsed)) {
			if (typeof credential !== "object" || credential === null || Array.isArray(credential)) {
				throw new Error(`Invalid auth.json credential for provider "${providerId}"`);
			}
			const value = credential as Record<string, unknown>;
			if (value.type === "api_key") {
				const validKey = value.key === undefined || typeof value.key === "string";
				const validEnv =
					value.env === undefined ||
					(typeof value.env === "object" &&
						value.env !== null &&
						!Array.isArray(value.env) &&
						Object.values(value.env).every((entry) => typeof entry === "string"));

View on GitHub (pinned to 4af9d21d3b)