Skip to content

Commit f13fd31

Browse files
committed
fix(server): time out unanswered Cursor Keychain prompts
- Reuse pending Keychain reads after callers time out - Tell users where to allow Keychain access
1 parent 501961c commit f13fd31

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

‎apps/server/src/provider/cursorCredentialStore.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { assert, describe, it } from "@effect/vitest";
22

3-
import { makeCachedCursorAccessTokenReader } from "./cursorCredentialStore.ts";
3+
import {
4+
CursorKeychainTimeoutError,
5+
makeCachedCursorAccessTokenReader,
6+
} from "./cursorCredentialStore.ts";
47

58
describe("Cursor Keychain reader", () => {
69
it("shares concurrent reads and rechecks after the cache expires", async () => {
@@ -19,4 +22,25 @@ describe("Cursor Keychain reader", () => {
1922
time = 5 * 60_000;
2023
assert.strictEqual(await read(), "token-2");
2124
});
25+
26+
it("gives up on an unanswered prompt and reuses it on the next read", async () => {
27+
let reads = 0;
28+
let allow: (token: string) => void = () => {};
29+
const read = makeCachedCursorAccessTokenReader(
30+
() => {
31+
reads++;
32+
return new Promise((resolve) => {
33+
allow = resolve;
34+
});
35+
},
36+
() => 0,
37+
1,
38+
);
39+
const error = await read().catch((cause: unknown) => cause);
40+
assert.instanceOf(error, CursorKeychainTimeoutError);
41+
const retry = read();
42+
allow("token");
43+
assert.strictEqual(await retry, "token");
44+
assert.strictEqual(reads, 1);
45+
});
2246
});

‎apps/server/src/provider/cursorCredentialStore.ts‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,47 @@ const CACHE_MS = 5 * 60_000;
44

55
const requireForKeyring = NodeModule.createRequire(import.meta.url);
66

7-
/** Share one Keychain request across usage history and limits in this server process. */
7+
/** Rejected when nobody answers the macOS Keychain prompt in time. */
8+
export class CursorKeychainTimeoutError extends Error {
9+
constructor() {
10+
super("Timed out waiting for Keychain access.");
11+
}
12+
}
13+
14+
/**
15+
* Share one Keychain request across usage history and limits in this server process.
16+
*
17+
* macOS shows the access prompt on the server's own screen, which a remote
18+
* client cannot answer, so callers give up after `timeoutMs`. The read stays in
19+
* flight: the next call reuses it instead of stacking a second prompt, and picks
20+
* up the token once someone allows access.
21+
*/
822
export function makeCachedCursorAccessTokenReader(
923
read: () => Promise<string | null>,
1024
now: () => number = Date.now,
25+
timeoutMs = 30_000,
1126
): () => Promise<string | null> {
1227
let cached: { token: string; until: number } | null = null;
1328
let pending: Promise<string | null> | null = null;
1429
return () => {
1530
if (cached && cached.until > now()) return Promise.resolve(cached.token);
16-
if (pending) return pending;
17-
pending = read()
31+
pending ??= read()
1832
.then((token) => {
1933
cached = token ? { token, until: now() + CACHE_MS } : null;
2034
return token;
2135
})
2236
.finally(() => {
2337
pending = null;
2438
});
25-
return pending;
39+
const deadline = AbortSignal.timeout(timeoutMs);
40+
return Promise.race([
41+
pending,
42+
new Promise<never>((_, reject) => {
43+
deadline.addEventListener("abort", () => reject(new CursorKeychainTimeoutError()), {
44+
once: true,
45+
});
46+
}),
47+
]);
2648
};
2749
}
2850

‎apps/server/src/usage/cursorUsageReader.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import * as NodeCrypto from "node:crypto";
55
import * as NodeTimersPromises from "node:timers/promises";
66

77
import type { UsageRecord } from "./usageTranscripts.ts";
8-
import { readMacCursorAccessToken } from "../provider/cursorCredentialStore.ts";
8+
import {
9+
CursorKeychainTimeoutError,
10+
readMacCursorAccessToken,
11+
} from "../provider/cursorCredentialStore.ts";
912

1013
function object(value: unknown): Record<string, unknown> {
1114
return typeof value === "object" && value !== null && !Array.isArray(value)
@@ -86,7 +89,9 @@ export async function readCursorAccountUsage(
8689
? null
8790
: typeof credentialSource === "string"
8891
? "Cursor credentials could not be read."
89-
: "Cursor Keychain credentials could not be read.",
92+
: cause instanceof CursorKeychainTimeoutError
93+
? "Allow Keychain access on the Mac running T3 Code, then refresh."
94+
: "Cursor Keychain credentials could not be read.",
9095
};
9196
}
9297
if (typeof accessToken !== "string" || !accessToken) {

0 commit comments

Comments
 (0)