From f703183bb395ee1c1c6d54929f7517cce551566b Mon Sep 17 00:00:00 2001 From: Pete Date: Tue, 10 Nov 2026 10:20:36 +0000 Subject: [PATCH 1/3] fix: respond with AuthRequired if the auth account read fails Previously this responded with an InternalError. --- src/CodexAcpClient.ts | 13 +++++++++++-- src/CodexAcpServer.ts | 2 +- .../CodexACPAgent/account-read-unavailable.test.ts | 11 +++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) 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..1636e6228 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; } diff --git a/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts b/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts index b96806b8d..6e6eec39e 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 () => { @@ -486,7 +490,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)", + }); // The read of `initialize` pushes the none status. The load pushes nothing more. expect(authStatusUpdates(fixture)).toEqual([NONE_STATUS]); expect(appServer.accountLogout).not.toHaveBeenCalled(); From c55afaee6aa2c125f583a16c150d99982cb96f93 Mon Sep 17 00:00:00 2001 From: Pete Date: Fri, 4 Dec 2026 15:39:14 +0000 Subject: [PATCH 2/3] fix: Remove unused check and correct docstring --- src/CodexAcpServer.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/CodexAcpServer.ts b/src/CodexAcpServer.ts index 1636e6228..183ee5079 100644 --- a/src/CodexAcpServer.ts +++ b/src/CodexAcpServer.ts @@ -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; From fd8e0cef0e3d0830a275dfbac1859ee7b06393c5 Mon Sep 17 00:00:00 2001 From: Pete Date: Fri, 4 Dec 2026 15:45:21 +0000 Subject: [PATCH 3/3] fix: Tweak test name and use unauthorized() helper --- src/__tests__/CodexACPAgent/account-read-unavailable.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts b/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts index 6e6eec39e..f63bd8702 100644 --- a/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts +++ b/src/__tests__/CodexACPAgent/account-read-unavailable.test.ts @@ -483,10 +483,10 @@ 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: []}))