Skip to content
TypeScript

fix(cursor): cache successful model discovery between refreshes (#9918)

maria0d8a91a25a35
diff --git a/apps/server/src/provider/Drivers/CursorDriver.ts b/apps/server/src/provider/Drivers/CursorDriver.ts
index ddfda0f9133..5466af802e5 100644
--- a/apps/server/src/provider/Drivers/CursorDriver.ts
+++ b/apps/server/src/provider/Drivers/CursorDriver.ts
@@ -29,6 +29,7 @@ import { makeCursorAdapter } from "../Layers/CursorAdapter.ts";
import {
buildInitialCursorProviderSnapshot,
checkCursorProviderStatus,
+ makeCursorModelDiscovery,
enrichCursorSnapshot,
} from "../Layers/CursorProvider.ts";
import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
@@ -136,7 +137,12 @@ export const CursorDriver: ProviderDriver<CursorSettings, CursorDriverEnv> = {
});
const textGeneration = yield* makeCursorTextGeneration(effectiveConfig, processEnv);
- const checkProvider = checkCursorProviderStatus(effectiveConfig, processEnv).pipe(
+ const discoverModels = yield* makeCursorModelDiscovery(effectiveConfig, processEnv);
+ const checkProvider = checkCursorProviderStatus(
+ effectiveConfig,
+ processEnv,
+ discoverModels,
+ ).pipe(
Effect.map(stampIdentity),
Effect.provideService(Crypto.Crypto, crypto),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
diff --git a/apps/server/src/provider/Layers/CursorProvider.test.ts b/apps/server/src/provider/Layers/CursorProvider.test.ts
index 4fa382c788d..adda9f44d46 100644
--- a/apps/server/src/provider/Layers/CursorProvider.test.ts
+++ b/apps/server/src/provider/Layers/CursorProvider.test.ts
@@ -16,6 +16,7 @@ import {
buildCursorCapabilitiesFromConfigOptions,
checkCursorProviderStatus,
discoverCursorModelsViaAcp,
+ makeCursorModelDiscovery,
getCursorParameterizedModelPickerUnsupportedMessage,
parseCursorAboutOutput,
parseCursorCliConfigChannel,
@@ -627,6 +628,42 @@ describe("checkCursorProviderStatus", () => {
});
describe("discoverCursorModelsViaAcp", () => {
+ it("reuses successful discovery until the CLI version or account changes", async () => {
+ await runNode(
+ Effect.gen(function* () {
+ const { requestLogPath, wrapperPath } = yield* makeProviderStatusEnvFixture();
+ const fileSystem = yield* FileSystem.FileSystem;
+ const settings = {
+ enabled: true,
+ binaryPath: wrapperPath,
+ apiEndpoint: "",
+ customModels: [],
+ };
+ const discover = yield* makeCursorModelDiscovery(settings, {
+ ...process.env,
+ T3_ACP_REQUEST_LOG_PATH: requestLogPath,
+ });
+ const about = {
+ version: "2026.08.11",
+ auth: { status: "authenticated" as const, label: "[email protected]" },
+ };
+ const first = yield* discover(about);
+ expect(first.length).toBeGreaterThan(0);
+ yield* fileSystem.writeFileString(requestLogPath, "");
+ expect(yield* discover(about)).toEqual(first);
+ expect(yield* fileSystem.readFileString(requestLogPath)).toBe("");
+ yield* discover({ ...about, version: "2026.08.12" });
+ expect(yield* fileSystem.readFileString(requestLogPath)).toContain("initialize");
+ yield* fileSystem.writeFileString(requestLogPath, "");
+ yield* discover({
+ version: "2026.08.12",
+ auth: { ...about.auth, label: "[email protected]" },
+ });
+ expect(yield* fileSystem.readFileString(requestLogPath)).toContain("initialize");
+ }),
+ );
+ });
+
it("keeps the ACP probe runtime alive long enough to discover models", async () => {
const wrapperPath = await runNode(makeMockAgentWrapper());
diff --git a/apps/server/src/provider/Layers/CursorProvider.ts b/apps/server/src/provider/Layers/CursorProvider.ts
index 48fbfe27c48..cf4f00ac967 100644
--- a/apps/server/src/provider/Layers/CursorProvider.ts
+++ b/apps/server/src/provider/Layers/CursorProvider.ts
@@ -10,6 +10,8 @@ import type {
} from "@t3tools/contracts";
import type * as EffectAcpSchema from "effect-acp/schema";
import { causeErrorTag } from "@t3tools/shared/observability";
+import * as Cache from "effect/Cache";
+import * as Duration from "effect/Duration";
import * as Crypto from "effect/Crypto";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
@@ -572,6 +574,23 @@ export const discoverCursorModelsViaAcp = (
environment?: NodeJS.ProcessEnv,
) => discoverCursorModelsViaListAvailableModels(cursorSettings, environment);
+// Each driver instance owns its cache; version and account changes invalidate it.
+export const makeCursorModelDiscovery = Effect.fn("makeCursorModelDiscovery")(function* (
+ cursorSettings: CursorSettings,
+ environment?: NodeJS.ProcessEnv,
+) {
+ const cache = yield* Cache.makeWith(
+ (_key: string) => discoverCursorModelsViaAcp(cursorSettings, environment),
+ {
+ capacity: 1,
+ timeToLive: (exit) =>
+ Exit.isSuccess(exit) && exit.value.length > 0 ? Duration.minutes(30) : Duration.zero,
+ },
+ );
+ return (about: Pick<CursorAboutResult, "version" | "auth">) =>
+ Cache.get(cache, JSON.stringify([about.version, about.auth]));
+});
+
function getCursorFallbackModels(
cursorSettings: Pick<CursorSettings, "customModels">,
): ReadonlyArray<ServerProviderModel> {
@@ -989,6 +1008,7 @@ const runCursorAboutCommand = (cursorSettings: CursorSettings, environment?: Nod
export const checkCursorProviderStatus = Effect.fn("checkCursorProviderStatus")(function* (
cursorSettings: CursorSettings,
environment?: NodeJS.ProcessEnv,
+ discoverModels?: (about: CursorAboutResult) => ReturnType<typeof discoverCursorModelsViaAcp>,
): Effect.fn.Return<
ServerProviderDraft,
never,
@@ -1086,9 +1106,10 @@ export const checkCursorProviderStatus = Effect.fn("checkCursorProviderStatus")(
let discoveryWarning: string | undefined;
if (parsed.auth.status !== "unauthenticated") {
const discoveryExit = yield* Effect.exit(
- discoverCursorModelsViaAcp(cursorSettings, environment).pipe(
- Effect.timeoutOption(CURSOR_ACP_MODEL_DISCOVERY_TIMEOUT_MS),
- ),
+ (discoverModels
+ ? discoverModels(parsed)
+ : discoverCursorModelsViaAcp(cursorSettings, environment)
+ ).pipe(Effect.timeoutOption(CURSOR_ACP_MODEL_DISCOVERY_TIMEOUT_MS)),
);
if (Exit.isFailure(discoveryExit)) {
yield* Effect.logWarning("Cursor ACP model discovery failed", {