Skip to content

Commit 951844f

Browse files
authored
fix: report cancelled device-code sign-in as auth error instead of invalid params (#594)
[IJAI-2673](https://youtrack.jetbrains.com/issue/IJAI-2673)
1 parent 4583cb3 commit 951844f

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎src/CodexAcpClient.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ export class CodexAcpClient {
293293

294294
if (!acp.CreateElicitationResponse.isAccept(first.response)) {
295295
await this.codexClient.accountLoginCancel({loginId: loginResponse.loginId});
296-
return false;
296+
throw RequestError.requestCancelled(
297+
{methodId: "chat-gpt-device-code", action: first.response.action},
298+
"ChatGPT device code sign-in was cancelled",
299+
);
297300
}
298301

299302
const result = await loginCompletedPromise;

‎src/CodexAcpServer.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ export class CodexAcpServer {
12261226
const isAuthenticated = await this.runWithProcessCheck(() => this.codexAcpClient.authenticate(_params, elicitationRequester));
12271227
if (!isAuthenticated) {
12281228
logger.log("Authenticate request failed");
1229-
throw RequestError.invalidParams();
1229+
throw RequestError.authRequired({methodId: _params.methodId}, "Sign-in did not complete");
12301230
}
12311231
await this.refreshAuthState(this.getAuthProviderForAuthenticateRequest(_params));
12321232
logger.log("Authenticate request completed");

‎src/__tests__/CodexACPAgent/CodexAcpClient.test.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe('ACP server test', { timeout: 40_000 }, () => {
274274
});
275275
});
276276

277-
it('should cancel ChatGPT device code login when URL elicitation is declined', async () => {
277+
it.each(["decline", "cancel"] as const)('should cancel ChatGPT device code login when URL elicitation is answered with %s', async (action) => {
278278
const { deviceFixture, codexAppServerClient } = createDeviceCodeFixture();
279279
const codexAcpAgent = deviceFixture.getCodexAcpAgent();
280280
await codexAcpAgent.initialize({
@@ -283,16 +283,39 @@ describe('ACP server test', { timeout: 40_000 }, () => {
283283
});
284284
const cancelSpy = vi.spyOn(codexAppServerClient, "accountLoginCancel")
285285
.mockResolvedValue({ status: "canceled" });
286-
deviceFixture.setElicitationResponse({ action: "decline" });
286+
deviceFixture.setElicitationResponse({ action });
287287

288288
await expect(codexAcpAgent.authenticate({ methodId: "chat-gpt-device-code" }, 42))
289-
.rejects.toThrow();
289+
.rejects.toMatchObject({
290+
code: -32800,
291+
message: "Request cancelled: ChatGPT device code sign-in was cancelled",
292+
data: { methodId: "chat-gpt-device-code", action },
293+
});
290294
expect(cancelSpy).toHaveBeenCalledWith({ loginId: "login-1" });
291295
expect(deviceFixture.getAcpConnectionEvents([])
292296
.some(event => event.method === "completeElicitation"))
293297
.toBe(false);
294298
});
295299

300+
it('should report an incomplete sign-in when ChatGPT device code login fails', async () => {
301+
const { deviceFixture, completeLogin, loginCompletedSubscribed } = createDeviceCodeFixture();
302+
const codexAcpAgent = deviceFixture.getCodexAcpAgent();
303+
await codexAcpAgent.initialize({
304+
protocolVersion: 1,
305+
clientCapabilities: { elicitation: { url: {} } },
306+
});
307+
deviceFixture.setElicitationResponse({ action: "accept" });
308+
309+
const authPromise = codexAcpAgent.authenticate({ methodId: "chat-gpt-device-code" }, 42);
310+
await vi.waitFor(() => expect(loginCompletedSubscribed()).toBe(true));
311+
completeLogin(false);
312+
await expect(authPromise).rejects.toMatchObject({
313+
code: -32000,
314+
message: "Authentication required: Sign-in did not complete",
315+
data: { methodId: "chat-gpt-device-code" },
316+
});
317+
});
318+
296319
it('should complete URL elicitation when login finishes before the elicitation response', async () => {
297320
const { deviceFixture, completeLogin, loginCompletedSubscribed } = createDeviceCodeFixture();
298321
const codexAcpAgent = deviceFixture.getCodexAcpAgent();

0 commit comments

Comments
 (0)