diff --git a/packages/provider-cursor/src/server/usageLimits.test.ts b/packages/provider-cursor/src/server/usageLimits.test.ts index ce8a2cbc27cf3..30622218a5a78 100644 --- a/packages/provider-cursor/src/server/usageLimits.test.ts +++ b/packages/provider-cursor/src/server/usageLimits.test.ts @@ -4,6 +4,8 @@ import * as HostProcess from "@t3tools/shared/HostProcess"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; +import * as Fiber from "effect/Fiber"; +import * as TestClock from "effect/testing/TestClock"; import * as HttpClient from "effect/http/HttpClient"; import * as HttpClientResponse from "effect/http/HttpClientResponse"; import * as CursorKeychain from "./CursorKeychain.ts"; @@ -228,6 +230,48 @@ describe("Cursor usage limits", () => { }), ); + it.effect("waits for Keychain approval before starting the HTTP request timeout", () => + Effect.gen(function* () { + const pending = yield* withNodeServices( + readCursorUsageLimits({ apiEndpoint: "" }, {}, true).pipe( + Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({ planUsage: { totalPercentUsed: 42 } }), + ), + ), + ), + ), + ), + Effect.sleep("12 seconds").pipe(Effect.as("keychain-token")), + ).pipe(Effect.forkChild); + yield* TestClock.adjust("12 seconds"); + const limits = yield* Fiber.join(pending); + expect(limits.windows[0]?.usedPercent).toBe(42); + }), + ); + + it.effect("still bounds a stalled HTTP request after Keychain approval", () => + Effect.gen(function* () { + const pending = yield* withNodeServices( + readCursorUsageLimits({ apiEndpoint: "" }, {}, true).pipe( + Effect.provideService(HostProcess.Platform, "darwin"), + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make(() => Effect.never), + ), + ), + Effect.sleep("12 seconds").pipe(Effect.as("keychain-token")), + ).pipe(Effect.forkChild); + yield* TestClock.adjust("22 seconds"); + expect((yield* Fiber.join(pending)).unavailable?.reason).toBe("probeFailed"); + }), + ); + it.effect("reports a Keychain initialization failure without failing the provider refresh", () => Effect.gen(function* () { const limits = yield* withNodeServices( diff --git a/packages/provider-cursor/src/server/usageLimits.ts b/packages/provider-cursor/src/server/usageLimits.ts index 049e646d2c492..b98d429e56fd8 100644 --- a/packages/provider-cursor/src/server/usageLimits.ts +++ b/packages/provider-cursor/src/server/usageLimits.ts @@ -127,22 +127,36 @@ export const readCursorUsageLimits = Effect.fn("readCursorUsageLimits")(function } if (!token) return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" }); const client = yield* HttpClient.HttpClient; - const response = yield* client.execute( - HttpClientRequest.post(`${endpoint}/aiserver.v1.DashboardService/GetCurrentPeriodUsage`).pipe( - HttpClientRequest.bearerToken(token), - HttpClientRequest.setHeaders({ - "connect-protocol-version": "1", - "x-cursor-client-type": "cli", - }), - HttpClientRequest.bodyJsonUnsafe({}), - ), - ); - const body = yield* HttpClientResponse.schemaBodyJson(CursorUsageResponse)( - yield* HttpClientResponse.filterStatusOk(response), - ); + const body = yield* client + .execute( + HttpClientRequest.post( + `${endpoint}/aiserver.v1.DashboardService/GetCurrentPeriodUsage`, + ).pipe( + HttpClientRequest.bearerToken(token), + HttpClientRequest.setHeaders({ + "connect-protocol-version": "1", + "x-cursor-client-type": "cli", + }), + HttpClientRequest.bodyJsonUnsafe({}), + ), + ) + .pipe( + Effect.flatMap(HttpClientResponse.filterStatusOk), + Effect.flatMap(HttpClientResponse.schemaBodyJson(CursorUsageResponse)), + Effect.timeout("10 seconds"), + ); return cursorUsageResponseToLimits(body, checkedAt); }).pipe( - Effect.timeout("10 seconds"), + Effect.catchTags({ + CursorKeychainTimeoutError: () => + Effect.succeed( + makeUnavailableUsageLimits({ + checkedAt, + reason: "probeFailed", + message: "Allow Keychain access on the Mac running T3 Code, then refresh limits.", + }), + ), + }), Effect.orElseSucceed(() => makeUnavailableUsageLimits({ checkedAt,