diff --git a/src/CodexAcpClient.ts b/src/CodexAcpClient.ts index fcded43f0..438795e93 100644 --- a/src/CodexAcpClient.ts +++ b/src/CodexAcpClient.ts @@ -151,6 +151,8 @@ export interface AuthRequirement { required: boolean; /** The account read that gave the answer, or `null` when the adapter did not read the account. */ account: GetAccountResponse | null; + /** Why the stored login does not work, from the failed account read. Absent for a missing login. */ + reason?: string; } export interface UrlElicitationRequester { @@ -470,8 +472,15 @@ export class CodexAcpClient { return {required: false, account: null}; } - const response = await this.codexClient.accountRead({refreshToken: false}); - return {required: response.requiresOpenaiAuth && !response.account, account: response}; + try { + const response = await this.codexClient.accountRead({refreshToken: false}); + return {required: response.requiresOpenaiAuth && !response.account, account: response}; + } catch (error) { + if (isAccountReadAuthFailureError(error)) { + return {required: true, account: null, reason: (error as Error).message}; + } + throw error; + } } /** diff --git a/src/CodexAcpServer.ts b/src/CodexAcpServer.ts index ea0e1824c..183ee5079 100644 --- a/src/CodexAcpServer.ts +++ b/src/CodexAcpServer.ts @@ -1246,7 +1246,7 @@ export class CodexAcpServer { logger.log("Authentication completed"); } else { logger.log("Authentication required but no default auth request provided, return to IDE"); - throw RequestError.authRequired(); + throw RequestError.authRequired(undefined, requirement.reason); } return null; } @@ -1691,9 +1691,8 @@ export class CodexAcpServer { * A newer status, from any source, also makes it old. An old answer changes nothing. * * - A read that found no login needed sets the account of the session and pushes the auth status. - * - A read that found that the agent needs a login (`auth_required`), or that failed because the login - * does not work ({@link isAccountReadAuthFailureError}, for example a revoked token), clears the account - * and pushes the `none` status at once, with no second read. The client then shows a login request, + * - A read that found that the agent needs a login (`auth_required`) clears the account and pushes + * the `none` status at once, with no second read. The client then shows a login request, * as it does for an `auth_required` error of the load. * - A read that failed because another client logged in or out reads the account again and pushes it. * - An unavailable read changes nothing. @@ -1727,7 +1726,7 @@ export class CodexAcpServer { }, async error => { if (!answerIsCurrent()) return; - if (isAuthRequiredError(error) || isAccountReadAuthFailureError(error)) { + if (isAuthRequiredError(error)) { logger.log("A late account read found that the agent needs a login", {error: String(error)}); this.lastAccountRead = {account: null, requiresOpenaiAuth: true}; sessionState.account = null; diff --git a/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts b/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts index b96806b8d..f63bd8702 100644 --- a/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts +++ b/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts @@ -1,4 +1,5 @@ import {afterEach, beforeEach, describe, expect, it, vi} from "vitest"; +import {RequestError} from "@agentclientprotocol/sdk/unstable"; import {ResponseError} from "vscode-jsonrpc/node"; import { createCodexMockTestFixture, @@ -390,7 +391,10 @@ describe("CodexACPAgent - an unavailable account read", () => { await initialize(fixture); await expect(fixture.getCodexAcpAgent().loadSession({sessionId: SESSION_ID, cwd: "/test/project", mcpServers: []})) - .rejects.toThrow("workspace routing discovery unauthorized (401)"); + .rejects.toMatchObject({ + code: RequestError.authRequired().code, + message: "Authentication required: workspace routing discovery unauthorized (401)", + }); }); it("keeps the sessions and pushes nothing when an auth refresh read is unavailable", async () => { @@ -479,14 +483,17 @@ describe("CodexACPAgent - an unavailable account read", () => { }); }); - it("fails the load with the error of a refused token, as before", async () => { + it("fails the load with auth_required when the read finds a revoked token", async () => { const fixture = fixtureWithSession(); const appServer = fixture.getCodexAppServerClient(); - appServer.accountRead = vi.fn().mockRejectedValue(new ResponseError(-32603, "workspace routing discovery unauthorized (401)")); + appServer.accountRead = vi.fn().mockRejectedValue(unauthorized()); await initialize(fixture); await expect(fixture.getCodexAcpAgent().loadSession({sessionId: SESSION_ID, cwd: "/test/project", mcpServers: []})) - .rejects.toThrow("workspace routing discovery unauthorized (401)"); + .rejects.toMatchObject({ + code: RequestError.authRequired().code, + message: "Authentication required: workspace routing discovery unauthorized (401)", + }); // The read of `initialize` pushes the none status. The load pushes nothing more. expect(authStatusUpdates(fixture)).toEqual([NONE_STATUS]); expect(appServer.accountLogout).not.toHaveBeenCalled();