Skip to content

Commit bd5ed4d

Browse files
fix(provider-cursor): Cursor sign-in shows why it failed (pingdotgg#25938)
1 parent 2cd7996 commit bd5ed4d

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

‎packages/provider-cursor/src/server/auth.test.ts‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
44
import * as Layer from "effect/Layer";
55
import { expect, it } from "@effect/vitest";
66
import { ProviderInstanceId, ProviderSetupError, type ProviderAuthState } from "@t3tools/contracts";
7+
import * as Cause from "effect/Cause";
78
import * as Clock from "effect/Clock";
89
import * as Deferred from "effect/Deferred";
910
import * as Effect from "effect/Effect";
1011
import * as Exit from "effect/Exit";
1112
import * as Fiber from "effect/Fiber";
13+
import * as Logger from "effect/Logger";
1214
import * as Option from "effect/Option";
1315
import * as Stream from "effect/Stream";
1416

1517
import { makeCursorAuth, type CursorAuthOptions } from "./auth.ts";
1618
import { loadCursorSdk } from "./sdk.ts";
1719

18-
const { InMemoryCredentialStore } = loadCursorSdk();
20+
const { AuthenticationError, InMemoryCredentialStore } = loadCursorSdk();
1921

2022
const instanceId = ProviderInstanceId.make("cursor-personal");
2123
const owner = "owner-client";
@@ -73,6 +75,13 @@ const makeHarness = Effect.fn("makeCursorAuthHarness")(function* (
7375
};
7476
});
7577

78+
const captureWarnings = (warnings: string[]) =>
79+
Logger.layer([
80+
Logger.make(({ logLevel, cause }) => {
81+
if (logLevel === "Warn") warnings.push(Cause.pretty(cause));
82+
}),
83+
]);
84+
7685
type Auth = Effect.Success<ReturnType<typeof makeCursorAuth>>;
7786
const phase = (auth: Auth, phase: ProviderAuthState["phase"], session = owner) =>
7887
auth.controller.subscribe(session).pipe(
@@ -171,8 +180,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("CursorAuth", (it
171180
}).pipe(Effect.scoped),
172181
);
173182

174-
it.effect("aborts cancellation and ignores a late successful SDK login", () =>
175-
Effect.gen(function* () {
183+
it.effect("aborts cancellation and ignores a late successful SDK login", () => {
184+
const warnings: string[] = [];
185+
return Effect.gen(function* () {
176186
const harness = yield* makeHarness();
177187
yield* harness.auth.controller.start(owner);
178188
const waiting = yield* phase(harness.auth, "waiting");
@@ -184,8 +194,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("CursorAuth", (it
184194
yield* Effect.promise(() => harness.returned.promise);
185195
expect(yield* harness.auth.readApiKey).toBeUndefined();
186196
expect(harness.changes).toEqual([]);
187-
}).pipe(Effect.scoped),
188-
);
197+
expect(warnings).toEqual([]);
198+
}).pipe(Effect.scoped, Effect.provide(captureWarnings(warnings)));
199+
});
189200

190201
it.effect("discards a credential rejected by verification and permits a fresh login", () =>
191202
Effect.gen(function* () {
@@ -219,6 +230,24 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))("CursorAuth", (it
219230
}).pipe(Effect.scoped),
220231
);
221232

233+
it.effect("shows a fixed reason when browser login fails and logs the SDK cause", () => {
234+
const warnings: string[] = [];
235+
return Effect.gen(function* () {
236+
const sdkMessage =
237+
"Login succeeded, but creating an SDK API key failed: [permission_denied] user API keys are disabled for this team (your team's settings may restrict user API keys — ask a team admin, or pass an existing API key directly)";
238+
const harness = yield* makeHarness({
239+
login: () => Promise.reject(new AuthenticationError(sdkMessage)),
240+
});
241+
yield* harness.auth.controller.start(owner);
242+
const failed = yield* phase(harness.auth, "failed");
243+
expect(failed.message).toContain("could not create an API key");
244+
expect(failed.message).not.toContain("permission_denied");
245+
expect(yield* harness.auth.readApiKey).toBeUndefined();
246+
expect(warnings).toHaveLength(1);
247+
expect(warnings[0]).toContain(sdkMessage);
248+
}).pipe(Effect.scoped, Effect.provide(captureWarnings(warnings)));
249+
});
250+
222251
it.effect("clears only this instance's credential after its sessions stop", () =>
223252
Effect.gen(function* () {
224253
const harness = yield* makeHarness();

‎packages/provider-cursor/src/server/auth.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,24 @@ import * as Semaphore from "effect/Semaphore";
1818
import * as Stream from "effect/Stream";
1919
import * as SubscriptionRef from "effect/SubscriptionRef";
2020

21-
import { loadCursorSdk } from "./sdk.ts";
21+
import { isCursorSdkError, loadCursorSdk } from "./sdk.ts";
2222
import type { ProviderAuthController } from "@t3tools/provider-core/server/auth";
2323

2424
const AUTH_TIMEOUT_MS = 300_000;
2525

26+
// The SDK has no code distinguishing these failures, so classify by its fixed message prefixes.
27+
const loginFailureDetail = (cause: unknown) => {
28+
if (isCursorSdkError(cause, "CursorSdkError")) {
29+
if (cause.message.startsWith("Login succeeded, but creating an SDK API key failed")) {
30+
return "Cursor signed you in but could not create an API key. Your team's settings may restrict API keys: ask a team admin, or add CURSOR_API_KEY in provider settings.";
31+
}
32+
if (cause.message.startsWith("Login failed or timed out")) {
33+
return "Cursor sign-in failed or timed out. Check that this machine can reach Cursor, then start sign-in again.";
34+
}
35+
}
36+
return "Cursor sign-in failed. Start sign-in again.";
37+
};
38+
2639
interface AuthFlow {
2740
readonly id: string;
2841
readonly owner: string;
@@ -204,7 +217,7 @@ export const makeCursorAuth = Effect.fn("makeCursorAuth")(function* (options: Cu
204217
new ProviderSetupError({
205218
instanceId: options.instanceId,
206219
operation: "start",
207-
detail: "Cursor sign-in failed. Start sign-in again.",
220+
detail: loginFailureDetail(cause),
208221
cause,
209222
}),
210223
});
@@ -270,6 +283,7 @@ export const makeCursorAuth = Effect.fn("makeCursorAuth")(function* (options: Cu
270283
}),
271284
),
272285
}),
286+
Effect.tapCause((cause) => Effect.logWarning("Cursor sign-in failed", cause)),
273287
Effect.exit,
274288
Effect.flatMap((result) =>
275289
lock.withPermits(1)(

0 commit comments

Comments
 (0)