Skip to content

Commit 58de765

Browse files
committed
Refresh session auth state after logout
1 parent 5ed7964 commit 58de765

4 files changed

Lines changed: 28 additions & 13 deletions

File tree

src/CodexAcpClient.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ export class CodexAcpClient {
217217
return response.requiresOpenaiAuth && !response.account;
218218
}
219219

220+
hasGatewayAuth(): boolean {
221+
return this.gatewayConfig !== null;
222+
}
223+
220224
async getAccount(): Promise<GetAccountResponse> {
221225
return this.codexClient.accountRead({refreshToken: false});
222226
}

src/CodexAcpServer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class CodexAcpServer {
163163
connection,
164164
codexAcpClient,
165165
(operation) => this.runWithProcessCheck(operation),
166-
() => this.markSessionsLoggedOut()
166+
() => this.refreshSessionsAuthState()
167167
);
168168
}
169169

@@ -410,7 +410,7 @@ export class CodexAcpServer {
410410
}
411411

412412
private async getActiveAuthState(): Promise<ActiveAuthState> {
413-
if (this.codexAcpClient.getModelProvider()) {
413+
if (this.codexAcpClient.hasGatewayAuth()) {
414414
return {
415415
account: null,
416416
authConfigured: true,
@@ -586,15 +586,15 @@ export class CodexAcpServer {
586586
async logout(_params: acp.LogoutRequest): Promise<void> {
587587
logger.log("Logout request received");
588588
await this.runWithProcessCheck(() => this.codexAcpClient.logout());
589-
this.markSessionsLoggedOut();
589+
await this.refreshSessionsAuthState();
590590
logger.log("Logout request completed");
591591
}
592592

593-
private markSessionsLoggedOut(): void {
594-
const authConfigured = this.codexAcpClient.getModelProvider() !== null;
593+
private async refreshSessionsAuthState(): Promise<void> {
594+
const authState = await this.getActiveAuthState();
595595
for (const sessionState of this.sessions.values()) {
596-
sessionState.account = null;
597-
sessionState.authConfigured = authConfigured;
596+
sessionState.account = authState.account;
597+
sessionState.authConfigured = authState.authConfigured;
598598
}
599599
}
600600

src/CodexCommands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type CommandHandleOptions = {
2121
onTurnStarted?: (turnId: string, threadId: string) => void;
2222
};
2323

24-
export type LogoutHandler = () => void;
24+
export type LogoutHandler = () => void | Promise<void>;
2525

2626
export class CodexCommands {
2727
private readonly connection: AcpClientConnection;
@@ -203,7 +203,7 @@ export class CodexCommands {
203203
}
204204
case "logout": {
205205
await this.runWithProcessCheck(() => this.codexAcpClient.logout());
206-
this.onLogout();
206+
await this.onLogout();
207207
const session = new ACPSessionConnection(this.connection, sessionId);
208208
await session.update({
209209
sessionUpdate: "agent_message_chunk",

src/__tests__/CodexACPAgent/CodexAcpClient.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,10 +1427,20 @@ describe('ACP server test', { timeout: 40_000 }, () => {
14271427
const currentModelId = ModelId.create(model.id, model.defaultReasoningEffort).toString();
14281428

14291429
vi.spyOn(codexAcpClient, "authRequired").mockResolvedValue(false);
1430-
vi.spyOn(codexAcpClient, "getAccount").mockResolvedValue({
1431-
account: { type: "apiKey" },
1432-
requiresOpenaiAuth: false,
1433-
});
1430+
vi.spyOn(codexAcpClient, "getModelProvider").mockReturnValue("openai");
1431+
const getAccountSpy = vi.spyOn(codexAcpClient, "getAccount")
1432+
.mockResolvedValueOnce({
1433+
account: { type: "apiKey" },
1434+
requiresOpenaiAuth: false,
1435+
})
1436+
.mockResolvedValueOnce({
1437+
account: { type: "apiKey" },
1438+
requiresOpenaiAuth: false,
1439+
})
1440+
.mockResolvedValueOnce({
1441+
account: null,
1442+
requiresOpenaiAuth: true,
1443+
});
14341444
vi.spyOn(codexAcpClient, "newSession")
14351445
.mockResolvedValueOnce({
14361446
sessionId: "session-1",
@@ -1457,6 +1467,7 @@ describe('ACP server test', { timeout: 40_000 }, () => {
14571467
});
14581468

14591469
expect(logoutSpy).toHaveBeenCalledOnce();
1470+
expect(getAccountSpy).toHaveBeenCalledTimes(3);
14601471
expect(codexAcpAgent.getSessionState(session1.sessionId)).toMatchObject({
14611472
account: null,
14621473
authConfigured: false,

0 commit comments

Comments
 (0)