Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/CodexAcpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ export class CodexAcpClient {

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

const result = await loginCompletedPromise;
Expand Down
2 changes: 1 addition & 1 deletion src/CodexAcpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ export class CodexAcpServer {
const isAuthenticated = await this.runWithProcessCheck(() => this.codexAcpClient.authenticate(_params, elicitationRequester));
if (!isAuthenticated) {
logger.log("Authenticate request failed");
throw RequestError.invalidParams();
throw RequestError.authRequired({methodId: _params.methodId}, "Sign-in did not complete");
}
await this.refreshAuthState(this.getAuthProviderForAuthenticateRequest(_params));
logger.log("Authenticate request completed");
Expand Down
29 changes: 26 additions & 3 deletions src/__tests__/CodexACPAgent/CodexAcpClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('ACP server test', { timeout: 40_000 }, () => {
});
});

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

await expect(codexAcpAgent.authenticate({ methodId: "chat-gpt-device-code" }, 42))
.rejects.toThrow();
.rejects.toMatchObject({
code: -32800,
message: "Request cancelled: ChatGPT device code sign-in was cancelled",
data: { methodId: "chat-gpt-device-code", action },
});
expect(cancelSpy).toHaveBeenCalledWith({ loginId: "login-1" });
expect(deviceFixture.getAcpConnectionEvents([])
.some(event => event.method === "completeElicitation"))
.toBe(false);
});

it('should report an incomplete sign-in when ChatGPT device code login fails', async () => {
const { deviceFixture, completeLogin, loginCompletedSubscribed } = createDeviceCodeFixture();
const codexAcpAgent = deviceFixture.getCodexAcpAgent();
await codexAcpAgent.initialize({
protocolVersion: 1,
clientCapabilities: { elicitation: { url: {} } },
});
deviceFixture.setElicitationResponse({ action: "accept" });

const authPromise = codexAcpAgent.authenticate({ methodId: "chat-gpt-device-code" }, 42);
await vi.waitFor(() => expect(loginCompletedSubscribed()).toBe(true));
completeLogin(false);
await expect(authPromise).rejects.toMatchObject({
code: -32000,
message: "Authentication required: Sign-in did not complete",
data: { methodId: "chat-gpt-device-code" },
});
});

it('should complete URL elicitation when login finishes before the elicitation response', async () => {
const { deviceFixture, completeLogin, loginCompletedSubscribed } = createDeviceCodeFixture();
const codexAcpAgent = deviceFixture.getCodexAcpAgent();
Expand Down
Loading