Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/CodexAcpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)) {
Comment thread
anvilpete marked this conversation as resolved.
Resolved
return {required: true, account: null, reason: (error as Error).message};
}
throw error;
}
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/CodexAcpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 11 additions & 4 deletions src/__tests__/CodexACPAgent/account-read-unavailable.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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({
Comment thread
anvilpete marked this conversation as resolved.
Resolved
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();
Expand Down