The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ProviderInstanceId } from "@t3tools/contracts"; |
| 2 | import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; |
| 3 | import type { ProviderUsageScan } from "@t3tools/provider-core/server/usage"; |
| 4 | import { writeFileStringAtomically } from "@t3tools/shared/atomicWrite"; |
| 5 | import * as Clock from "effect/Clock"; |
| 6 | import * as Context from "effect/Context"; |
| 7 | import * as Digest from "effect/crypto/Digest"; |
| 8 | import * as Deferred from "effect/Deferred"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Hex from "effect/encoding/Hex"; |
| 11 | import * as Fiber from "effect/Fiber"; |
| 12 | import * as FileSystem from "effect/FileSystem"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Path from "effect/Path"; |
| 15 | import * as Schema from "effect/Schema"; |
| 16 | import * as Semaphore from "effect/Semaphore"; |
| 17 | |
| 18 | import { |
| 19 | CURSOR_ACCOUNT_CACHE_FILE_NAME, |
| 20 | CURSOR_ACCOUNT_TTL_MS, |
| 21 | CURSOR_MIN_FETCH_SPAN_MS, |
| 22 | cursorFetchRange, |
| 23 | decodeCursorAccountCaches, |
| 24 | encodeCursorAccountCaches, |
| 25 | isCursorCacheFresh, |
| 26 | mergeCursorFetch, |
| 27 | type CursorAccountCache, |
| 28 | type CursorCredentialSource, |
| 29 | } from "./accountCache.ts"; |
| 30 | import * as CursorAccountReader from "./CursorAccountReader.ts"; |
| 31 | import { readStoredCursorApiKey } from "./credentialStore.ts"; |
| 32 | |
| 33 | const CURSOR_ACCOUNT_READ_ERROR = "Cursor account usage could not be read."; |
| 34 | |
| 35 | const decodeCacheFile = Schema.decodeUnknownEffect( |
| 36 | Schema.fromJsonString(Schema.Unknown as unknown as Schema.Codec<unknown>), |
| 37 | ); |
| 38 | |
| 39 | export class CursorUsageAccounts extends Context.Service< |
| 40 | CursorUsageAccounts, |
| 41 | { |
| 42 | readonly scan: (input: { |
| 43 | readonly instances: ReadonlyArray<{ |
| 44 | readonly instanceId: ProviderInstanceId; |
| 45 | readonly environment: NodeJS.ProcessEnv; |
| 46 | }>; |
| 47 | readonly windowStartMs: number; |
| 48 | readonly retentionCutoffMs: number; |
| 49 | readonly awaitRefresh: boolean; |
| 50 | }) => Effect.Effect<ReadonlyArray<ProviderUsageScan>>; |
| 51 | readonly awaitPersisted: Effect.Effect<void>; |
| 52 | } |
| 53 | >()("@t3tools/provider-cursor/server/CursorUsageAccounts") {} |
| 54 | |
| 55 | const make = Effect.gen(function* () { |
| 56 | const fileSystem = yield* FileSystem.FileSystem; |
| 57 | const path = yield* Path.Path; |
| 58 | const host = yield* ProviderHost.ProviderHost; |
| 59 | const accountReader = yield* CursorAccountReader.CursorAccountReader; |
| 60 | const digest = yield* Digest.Digest; |
| 61 | |
| 62 | const cachePath = path.join(host.paths.stateDir, CURSOR_ACCOUNT_CACHE_FILE_NAME); |
| 63 | const caches = new Map<string, CursorAccountCache>(); |
| 64 | let cacheDirty = false; |
| 65 | /** Failed refreshes share the cache TTL to prevent repeated requests; null means no login. */ |
| 66 | const failures = new Map<string, { readonly atMs: number; readonly error: string | null }>(); |
| 67 | const refreshes = new Map<string, Deferred.Deferred<void>>(); |
| 68 | /** Widest range the next read asks for, narrowed after failures until the cache is complete. */ |
| 69 | const fetchSpans = new Map<string, number>(); |
| 70 | |
| 71 | const ensureLoaded = yield* Effect.cached( |
| 72 | fileSystem.readFileString(cachePath).pipe( |
| 73 | Effect.flatMap((raw) => decodeCacheFile(raw)), |
| 74 | Effect.catchCause(() => Effect.succeed(null)), |
| 75 | Effect.map((document) => { |
| 76 | for (const [key, cache] of decodeCursorAccountCaches(document)) caches.set(key, cache); |
| 77 | }), |
| 78 | ), |
| 79 | ); |
| 80 | |
| 81 | // Serialize snapshots; clear dirty before encoding and restore it on failure so changes retry. |
| 82 | const persistLock = yield* Semaphore.make(1); |
| 83 | const persist = Effect.gen(function* () { |
| 84 | if (!cacheDirty) return; |
| 85 | cacheDirty = false; |
| 86 | yield* Effect.sync(() => encodeCursorAccountCaches(caches)).pipe( |
| 87 | Effect.flatMap((contents) => writeFileStringAtomically({ filePath: cachePath, contents })), |
| 88 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 89 | Effect.provideService(Path.Path, path), |
| 90 | Effect.catchCause(() => |
| 91 | Effect.sync(() => { |
| 92 | cacheDirty = true; |
| 93 | }), |
| 94 | ), |
| 95 | ); |
| 96 | }).pipe(persistLock.withPermit, Effect.withSpan("CursorUsageAccounts.persist")); |
| 97 | |
| 98 | const pendingPersists = new Set<Fiber.Fiber<void>>(); |
| 99 | const schedulePersist = Effect.forkDetach(persist).pipe( |
| 100 | Effect.map((fiber) => { |
| 101 | pendingPersists.add(fiber); |
| 102 | fiber.addObserver(() => pendingPersists.delete(fiber)); |
| 103 | }), |
| 104 | ); |
| 105 | const awaitPersisted = Effect.suspend(() => Fiber.awaitAll([...pendingPersists])).pipe( |
| 106 | Effect.asVoid, |
| 107 | ); |
| 108 | yield* Effect.addFinalizer(() => awaitPersisted); |
| 109 | |
| 110 | const refreshLock = yield* Semaphore.make(2); |
| 111 | const refreshAccount = Effect.fn("CursorUsageAccounts.refreshAccount")(function* ( |
| 112 | credential: CursorCredentialSource, |
| 113 | credentialKey: string, |
| 114 | retentionStartMs: number, |
| 115 | ) { |
| 116 | const nowMs = yield* Clock.currentTimeMillis; |
| 117 | let checkedAccount = false; |
| 118 | let span = fetchSpans.get(credentialKey) ?? Number.POSITIVE_INFINITY; |
| 119 | // Widen after each success so a span narrowed by earlier failures recovers in a few reads. |
| 120 | let widening = true; |
| 121 | let widened = false; |
| 122 | for (;;) { |
| 123 | const base = caches.get(credentialKey); |
| 124 | const range = cursorFetchRange(base, retentionStartMs, nowMs, span); |
| 125 | const result = yield* accountReader.read(credential, range.sinceMs, range.untilMs); |
| 126 | if ( |
| 127 | !checkedAccount && |
| 128 | base !== undefined && |
| 129 | result.accountKey !== null && |
| 130 | result.accountKey !== base.accountKey |
| 131 | ) { |
| 132 | caches.delete(credentialKey); |
| 133 | cacheDirty = true; |
| 134 | checkedAccount = true; |
| 135 | continue; |
| 136 | } |
| 137 | checkedAccount = true; |
| 138 | if (result.missing || result.error !== null || result.accountKey === null) { |
| 139 | // A large account's full history can outlast the read deadlines; try a narrower range. |
| 140 | span = Math.max(CURSOR_MIN_FETCH_SPAN_MS, (range.untilMs - range.sinceMs) / 2); |
| 141 | fetchSpans.set(credentialKey, span); |
| 142 | if (widened) { |
| 143 | // The span before widening just worked: keep going with it instead of failing. |
| 144 | widened = false; |
| 145 | widening = false; |
| 146 | continue; |
| 147 | } |
| 148 | failures.set(credentialKey, { |
| 149 | atMs: nowMs, |
| 150 | error: result.missing || result.error !== null ? result.error : CURSOR_ACCOUNT_READ_ERROR, |
| 151 | }); |
| 152 | yield* schedulePersist; |
| 153 | return; |
| 154 | } |
| 155 | const merged = mergeCursorFetch( |
| 156 | base, |
| 157 | result.accountKey, |
| 158 | range, |
| 159 | result.records, |
| 160 | nowMs, |
| 161 | retentionStartMs, |
| 162 | ); |
| 163 | caches.set(credentialKey, merged.cache); |
| 164 | failures.delete(credentialKey); |
| 165 | if (merged.changed) cacheDirty = true; |
| 166 | yield* schedulePersist; |
| 167 | if (merged.cache.sinceMs <= retentionStartMs && merged.cache.untilMs >= nowMs) break; |
| 168 | widened = widening && Number.isFinite(span); |
| 169 | if (widened) { |
| 170 | span *= 2; |
| 171 | fetchSpans.set(credentialKey, span); |
| 172 | } |
| 173 | } |
| 174 | fetchSpans.delete(credentialKey); |
| 175 | }, refreshLock.withPermit); |
| 176 | |
| 177 | const startRefresh = ( |
| 178 | credential: CursorCredentialSource, |
| 179 | credentialKey: string, |
| 180 | retentionStartMs: number, |
| 181 | ) => |
| 182 | // Enroll and fork atomically so interruption cannot strand a registered refresh. |
| 183 | Effect.uninterruptible( |
| 184 | Effect.gen(function* () { |
| 185 | const current = refreshes.get(credentialKey); |
| 186 | if (current !== undefined) return current; |
| 187 | const done = Deferred.makeUnsafe<void>(); |
| 188 | refreshes.set(credentialKey, done); |
| 189 | // Detached: a departing client must not cancel a fetch later reads reuse. |
| 190 | yield* refreshAccount(credential, credentialKey, retentionStartMs).pipe( |
| 191 | Effect.catchCause(() => |
| 192 | Clock.currentTimeMillis.pipe( |
| 193 | Effect.map((atMs) => |
| 194 | failures.set(credentialKey, { atMs, error: CURSOR_ACCOUNT_READ_ERROR }), |
| 195 | ), |
| 196 | ), |
| 197 | ), |
| 198 | Effect.ensuring( |
| 199 | Effect.suspend(() => { |
| 200 | refreshes.delete(credentialKey); |
| 201 | return Deferred.succeed(done, undefined); |
| 202 | }), |
| 203 | ), |
| 204 | Effect.forkDetach, |
| 205 | ); |
| 206 | return done; |
| 207 | }), |
| 208 | ); |
| 209 | |
| 210 | const accountSource = Effect.fn("CursorUsageAccounts.accountSource")(function* ( |
| 211 | credential: CursorCredentialSource, |
| 212 | windowStartMs: number, |
| 213 | retentionStartMs: number, |
| 214 | awaitRefresh: boolean, |
| 215 | ) { |
| 216 | yield* ensureLoaded; |
| 217 | const credentialKey = `api-key:${Hex.encode(yield* digest.digest(Digest.sha256, new TextEncoder().encode(credential.apiKey)))}`; |
| 218 | const nowMs = yield* Clock.currentTimeMillis; |
| 219 | const recentFailure = failures.get(credentialKey); |
| 220 | let refreshing = false; |
| 221 | if ( |
| 222 | (recentFailure === undefined || nowMs - recentFailure.atMs >= CURSOR_ACCOUNT_TTL_MS) && |
| 223 | !isCursorCacheFresh(caches.get(credentialKey), nowMs, retentionStartMs) |
| 224 | ) { |
| 225 | const refresh = yield* startRefresh(credential, credentialKey, retentionStartMs); |
| 226 | if (awaitRefresh) { |
| 227 | yield* Deferred.await(refresh); |
| 228 | if ( |
| 229 | !failures.has(credentialKey) && |
| 230 | !isCursorCacheFresh(caches.get(credentialKey), nowMs, retentionStartMs) |
| 231 | ) { |
| 232 | const retry = yield* startRefresh(credential, credentialKey, retentionStartMs); |
| 233 | yield* Deferred.await(retry); |
| 234 | } |
| 235 | } else refreshing = true; |
| 236 | } |
| 237 | |
| 238 | const cache = caches.get(credentialKey); |
| 239 | const failure = refreshing ? undefined : failures.get(credentialKey); |
| 240 | const failureMessage = failure === undefined ? undefined : failure.error; |
| 241 | if (failureMessage === null) return null; |
| 242 | if (cache === undefined) { |
| 243 | return { |
| 244 | dir: "cursor-account", |
| 245 | ...(refreshing |
| 246 | ? { files: [], refreshing: true } |
| 247 | : { files: null, message: failureMessage ?? CURSOR_ACCOUNT_READ_ERROR }), |
| 248 | } satisfies ProviderUsageScan; |
| 249 | } |
| 250 | // Account history spans machines; a stable fingerprint prevents duplicate environment counts. |
| 251 | const source = `cursor-account:${cache.accountKey}`; |
| 252 | return { |
| 253 | dir: source, |
| 254 | hostId: "cursor.com", |
| 255 | volumeId: cache.accountKey, |
| 256 | files: [ |
| 257 | { |
| 258 | path: source, |
| 259 | records: cache.records.filter((record) => record.timestampMs >= windowStartMs), |
| 260 | }, |
| 261 | ], |
| 262 | ...(failureMessage === undefined |
| 263 | ? { status: "ok" } |
| 264 | : { status: "partial", message: failureMessage }), |
| 265 | ...(refreshing ? { refreshing: true } : {}), |
| 266 | } satisfies ProviderUsageScan; |
| 267 | }); |
| 268 | |
| 269 | const scan: CursorUsageAccounts["Service"]["scan"] = Effect.fn("CursorUsageAccounts.scan")( |
| 270 | function* ({ instances, windowStartMs, retentionCutoffMs, awaitRefresh }) { |
| 271 | const apiKeys = yield* Effect.forEach(instances, ({ instanceId, environment }) => |
| 272 | environment.CURSOR_API_KEY?.trim() |
| 273 | ? Effect.succeed(environment.CURSOR_API_KEY.trim()) |
| 274 | : readStoredCursorApiKey(instanceId).pipe( |
| 275 | Effect.provideService(ProviderHost.ProviderHost, host), |
| 276 | Effect.orElseSucceed(() => undefined), |
| 277 | ), |
| 278 | ); |
| 279 | const uniqueApiKeys = [...new Set(apiKeys.filter((apiKey) => apiKey !== undefined))]; |
| 280 | if (uniqueApiKeys.length > 0) { |
| 281 | const sources: ReadonlyArray<ProviderUsageScan | null> = yield* Effect.forEach( |
| 282 | uniqueApiKeys, |
| 283 | (apiKey) => |
| 284 | accountSource( |
| 285 | { kind: "apiKey", apiKey }, |
| 286 | windowStartMs, |
| 287 | retentionCutoffMs, |
| 288 | awaitRefresh, |
| 289 | ), |
| 290 | { concurrency: 2 }, |
| 291 | ); |
| 292 | // Keys for one account share its source, and a repeated source would count sessions twice. |
| 293 | // A failed key can hold older records, so a current read wins, then a refresh the client |
| 294 | // must follow up on. |
| 295 | const rank = (source: ProviderUsageScan) => |
| 296 | source.status === "ok" ? 2 : source.refreshing ? 1 : 0; |
| 297 | const byAccount = new Map<string, ProviderUsageScan>(); |
| 298 | for (const source of sources) { |
| 299 | if (source === null) continue; |
| 300 | const kept = byAccount.get(source.dir); |
| 301 | if (kept === undefined || rank(source) > rank(kept)) { |
| 302 | byAccount.set(source.dir, source); |
| 303 | } |
| 304 | } |
| 305 | return [...byAccount.values()]; |
| 306 | } |
| 307 | return []; |
| 308 | }, |
| 309 | ); |
| 310 | |
| 311 | return CursorUsageAccounts.of({ scan, awaitPersisted }); |
| 312 | }); |
| 313 | |
| 314 | export const layer = Layer.effect(CursorUsageAccounts, make); |