Skip to content

Commit 51b7b39

Browse files
bmdavis419claude
andcommitted
fix(usage): address review findings
- Count a stale environment as able to change every provider, so totals dim during a refresh even when its old summary had no usage. - Mute every loading chart line, including stale ones still on screen, and scale to everything drawn. - Scan transcript directories one at a time so open files stay bounded. - Keep 92 days of cache, covering the 90-day window and its slack. - Provide the Cursor account reader as a service instead of a reference whose default closes over network and filesystem access. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 65a76da commit 51b7b39

9 files changed

Lines changed: 105 additions & 110 deletions

File tree

‎apps/server/src/server.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ import * as NativeTelemetryClient from "./resourceTelemetry/NativeTelemetryClien
153153
import * as ResourceAttribution from "./resourceTelemetry/ResourceAttribution.ts";
154154
import * as ResourceMonitorBinary from "./resourceTelemetry/ResourceMonitorBinary.ts";
155155
import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts";
156+
import * as CursorUsageReader from "./usage/cursorUsageReader.ts";
156157
import * as UsageService from "./usage/UsageService.ts";
157158
import * as RuntimeLayer from "./orchestration-v2/runtimeLayer.ts";
158159
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
@@ -225,7 +226,10 @@ const layerBackground = BackgroundPolicy.layer.pipe(
225226
Layer.provideMerge(layerServerSettings),
226227
);
227228

228-
const layerUsage = UsageService.layer.pipe(Layer.provide(layerServerSettings));
229+
const layerUsage = UsageService.layer.pipe(
230+
Layer.provide(layerServerSettings),
231+
Layer.provide(CursorUsageReader.layer),
232+
);
229233

230234
const layerResourceDiagnostics = Layer.mergeAll(
231235
HostResources.layer,

‎apps/server/src/usage/UsageService.test.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { HttpClient, HttpClientResponse } from "effect/http";
3232

3333
import * as ServerConfig from "../config.ts";
3434
import * as ServerSettings from "../serverSettings.ts";
35-
import { CursorAccountReader } from "./cursorAccountCache.ts";
35+
import * as CursorUsageReader from "./cursorUsageReader.ts";
3636
import type { UsageRecord } from "./usageTranscripts.ts";
3737
import * as UsageService from "./UsageService.ts";
3838

@@ -93,6 +93,7 @@ const layerService = (input: {
9393
}) =>
9494
ServerConfig.layerTest(process.cwd(), { prefix: input.prefix }).pipe(
9595
Layer.provideMerge(NodeServices.layer),
96+
Layer.provideMerge(CursorUsageReader.layer),
9697
Layer.provideMerge(Layer.succeed(HostProcessPlatform, input.platform ?? "linux")),
9798
Layer.provideMerge(ServerSettings.layerTest(input.settings)),
9899
Layer.provideMerge(
@@ -164,7 +165,7 @@ function totalOutputTokens(summary: { buckets: readonly { totals: { outputTokens
164165
const CURSOR_NOW = Date.parse("2026-08-02T12:00:00Z");
165166
const HOUR_MS = 60 * 60 * 1000;
166167
/** The service's cache retention, which the Cursor account cache always covers. */
167-
const CURSOR_RETENTION_MS = 90 * 24 * HOUR_MS;
168+
const CURSOR_RETENTION_MS = 92 * 24 * HOUR_MS;
168169

169170
/**
170171
* Stands in for Cursor's dashboard API. Each read returns the account's events
@@ -370,7 +371,7 @@ describe("UsageService", () => {
370371
cursor.state.gate = gate;
371372
yield* Effect.gen(function* () {
372373
const service = yield* UsageService.make.pipe(
373-
Effect.provideService(CursorAccountReader, cursor.read),
374+
Effect.provideService(CursorUsageReader.CursorAccountReader, { read: cursor.read }),
374375
);
375376
// Cold: nothing cached yet, so Cursor answers empty while it refreshes.
376377
const cold = yield* service.readSummary(WINDOW);
@@ -427,7 +428,7 @@ describe("UsageService", () => {
427428
];
428429
yield* Effect.gen(function* () {
429430
const service = yield* UsageService.make.pipe(
430-
Effect.provideService(CursorAccountReader, cursor.read),
431+
Effect.provideService(CursorUsageReader.CursorAccountReader, { read: cursor.read }),
431432
);
432433
const read = (input: UsageSummaryInput) =>
433434
service.readSummary({ ...input, awaitRefresh: true });
@@ -476,7 +477,7 @@ describe("UsageService", () => {
476477
cursor.state.events = [{ timestampMs: CURSOR_NOW - HOUR_MS * 3, outputTokens: 5 }];
477478
yield* Effect.gen(function* () {
478479
const service = yield* UsageService.make.pipe(
479-
Effect.provideService(CursorAccountReader, cursor.read),
480+
Effect.provideService(CursorUsageReader.CursorAccountReader, { read: cursor.read }),
480481
);
481482
yield* service.readSummary({ ...WINDOW, awaitRefresh: true });
482483

@@ -519,15 +520,15 @@ describe("UsageService", () => {
519520
];
520521
yield* Effect.gen(function* () {
521522
const first = yield* UsageService.make.pipe(
522-
Effect.provideService(CursorAccountReader, before.read),
523+
Effect.provideService(CursorUsageReader.CursorAccountReader, { read: before.read }),
523524
);
524525
const original = yield* first.readSummary({ ...WINDOW, awaitRefresh: true });
525526
yield* first.awaitPersisted;
526527

527528
const after = makeFakeCursor();
528529
after.state.events = before.state.events;
529530
const restarted = yield* UsageService.make.pipe(
530-
Effect.provideService(CursorAccountReader, after.read),
531+
Effect.provideService(CursorUsageReader.CursorAccountReader, { read: after.read }),
531532
);
532533
const restored = yield* restarted.readSummary(WINDOW);
533534
assert.isUndefined(cursorSource(restored)?.refreshing);

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import { readAntigravityUsage } from "./antigravityUsageReader.ts";
6262
import {
6363
CURSOR_ACCOUNT_CACHE_FILE_NAME,
6464
CURSOR_ACCOUNT_TTL_MS,
65-
CursorAccountReader,
6665
cursorFetchRange,
6766
decodeCursorAccountCaches,
6867
encodeCursorAccountCaches,
@@ -71,6 +70,7 @@ import {
7170
type CursorAccountCache,
7271
type CursorCredentialSource,
7372
} from "./cursorAccountCache.ts";
73+
import * as CursorUsageReader from "./cursorUsageReader.ts";
7474
import { resolveModelAliases, UsageAggregator } from "./usageAggregation.ts";
7575
import { createOverrideRateTable, parseRateTable, type RateTable } from "./usagePricing.ts";
7676
import {
@@ -106,8 +106,11 @@ const RATES_REFRESH_FLOOR_MS = 60 * 1000;
106106
const MTIME_SLACK_MS = 36 * 60 * 60 * 1000;
107107
const MAX_HOURLY_WINDOW_MS = 24 * 60 * 60 * 1000;
108108

109-
/** Longest window the UI offers, plus slack. Older entries are pruned. */
110-
const CACHE_RETENTION_DAYS = 90;
109+
/**
110+
* The longest window the UI offers, 90 days, plus its `MTIME_SLACK_MS`, rounded
111+
* up. Older entries are pruned.
112+
*/
113+
const CACHE_RETENTION_DAYS = 92;
111114

112115
const CURSOR_ACCOUNT_READ_ERROR = "Cursor account usage could not be read.";
113116

@@ -212,7 +215,7 @@ export const make = Effect.gen(function* () {
212215
const httpClient = yield* HttpClient.HttpClient;
213216
const hostEnvironment = yield* HostProcessEnvironment;
214217
const platform = yield* HostProcessPlatform;
215-
const readCursorAccount = yield* CursorAccountReader;
218+
const cursorAccountReader = yield* CursorUsageReader.CursorAccountReader;
216219

217220
const fileCache: ScanCache = new Map();
218221
const sourceCache = new Map<string, typeof CachedSource.Type>();
@@ -662,9 +665,9 @@ export const make = Effect.gen(function* () {
662665
const nowMs = yield* Clock.currentTimeMillis;
663666
const fetchMissing = (cache: CursorAccountCache | undefined) => {
664667
const range = cursorFetchRange(cache, retentionStartMs, nowMs);
665-
return readCursorAccount(credential, range.sinceMs, range.untilMs).pipe(
666-
Effect.map((result) => ({ range, result })),
667-
);
668+
return cursorAccountReader
669+
.read(credential, range.sinceMs, range.untilMs)
670+
.pipe(Effect.map((result) => ({ range, result })));
668671
};
669672
let base = cursorCaches.get(credentialKey);
670673
let fetched = yield* fetchMissing(base);
@@ -981,13 +984,12 @@ export const make = Effect.gen(function* () {
981984
return source === null ? [] : [source];
982985
});
983986

984-
// Independent sources scan together. The result keeps this order, since
985-
// aggregation keeps the first copy of a duplicate.
987+
// Independent sources scan together. Transcript directories go one at a
988+
// time, so open files stay at `TRANSCRIPT_READ_CONCURRENCY`. The result
989+
// keeps this order, since aggregation keeps the first copy of a duplicate.
986990
const [transcripts, openCodeDirs, antigravityDirs, cursorDirs] = yield* Effect.all(
987991
[
988-
Effect.forEach(dirs, (dir) => scanTranscriptDir(dir, windowStartMs), {
989-
concurrency: "unbounded",
990-
}),
992+
Effect.forEach(dirs, (dir) => scanTranscriptDir(dir, windowStartMs)),
991993
openCode,
992994
antigravity,
993995
cursor,

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,11 @@
1212
*
1313
* @module cursorAccountCache
1414
*/
15-
import * as Context from "effect/Context";
16-
import * as Effect from "effect/Effect";
17-
18-
import { cursorRateModel, readCursorAccountUsage } from "./cursorUsageReader.ts";
15+
import { cursorRateModel } from "./cursorUsageReader.ts";
1916
import type { UsageRecord } from "./usageTranscripts.ts";
2017

2118
export type CursorCredentialSource = string | { readonly kind: "keychain" };
2219

23-
/** Reads one range of account usage. A reference so tests can stand in for Cursor's API. */
24-
export const CursorAccountReader = Context.Reference(
25-
"t3/usage/cursorAccountCache/CursorAccountReader",
26-
{
27-
defaultValue:
28-
() => (credentialSource: CursorCredentialSource, sinceMs: number, untilMs: number) =>
29-
Effect.promise(() => readCursorAccountUsage(credentialSource, sinceMs, untilMs)),
30-
},
31-
);
32-
3320
/** Matches the clients' stale time, so a page refetching on focus reuses one fetch. */
3421
export const CURSOR_ACCOUNT_TTL_MS = 60 * 1000;
3522

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import * as NodeFSP from "node:fs/promises";
44
import * as NodeCrypto from "node:crypto";
55
import * as NodeTimersPromises from "node:timers/promises";
66

7+
import * as Context from "effect/Context";
8+
import * as Effect from "effect/Effect";
9+
import * as Layer from "effect/Layer";
10+
711
import type { UsageRecord } from "./usageTranscripts.ts";
812
import {
913
CursorKeychainTimeoutError,
@@ -326,3 +330,24 @@ export async function readCursorAccountUsage(
326330
};
327331
}
328332
}
333+
334+
/** Reads one range of Cursor account usage. A service so tests can stand in for Cursor's API. */
335+
export class CursorAccountReader extends Context.Service<
336+
CursorAccountReader,
337+
{
338+
readonly read: (
339+
credentialSource: string | { readonly kind: "keychain" },
340+
sinceMs: number,
341+
untilMs: number,
342+
) => Effect.Effect<CursorAccountUsageReadResult>;
343+
}
344+
>()("t3/usage/cursorUsageReader/CursorAccountReader") {}
345+
346+
/** Reads Cursor's dashboard API with the saved CLI or Keychain login. */
347+
export const layer = Layer.succeed(
348+
CursorAccountReader,
349+
CursorAccountReader.of({
350+
read: (credentialSource, sinceMs, untilMs) =>
351+
Effect.promise(() => readCursorAccountUsage(credentialSource, sinceMs, untilMs)),
352+
}),
353+
);

‎apps/web/src/components/usage/UsagePage.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,14 @@ export function UsagePage() {
221221
),
222222
[loading.providers, providersWithData],
223223
);
224-
// Lines with nothing to draw yet stay flat until their usage lands.
225224
const chartLoadingProviders = useMemo(
226225
() =>
227226
new Set(
228227
activeProviders.filter(
229-
(provider) => loading.providers.has(provider) && !providersWithData.includes(provider),
228+
(provider) => loading.everyProvider || loading.providers.has(provider),
230229
),
231230
),
232-
[activeProviders, loading.providers, providersWithData],
231+
[activeProviders, loading.everyProvider, loading.providers],
233232
);
234233
const selectedModel =
235234
selectedModelKey === null

‎apps/web/src/components/usage/UsageProviderChart.test.ts‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,24 @@ import { buildPeriodColumns, chartScale, niceScale } from "./UsageProviderChart"
44
import { providersWithUsage } from "./usageProviders";
55

66
describe("chartScale", () => {
7-
const columns = [
8-
{
9-
total: 0,
10-
bands: [
11-
{ provider: "codex" as const, value: 40 },
12-
{ provider: "cursor" as const, value: 900 },
13-
],
14-
},
15-
];
16-
17-
it("scales to providers that have answered", () => {
18-
const scale = chartScale(columns, ["codex", "cursor"], new Set(["cursor" as const]));
19-
20-
expect(scale.max).toBe(40);
21-
expect(scale.labeled).toBe(true);
7+
const column = (codex: number) => ({
8+
total: codex,
9+
bands: [{ provider: "codex" as const, value: codex }],
2210
});
2311

24-
it("holds unlabeled placeholder gridlines until something answers", () => {
25-
const scale = chartScale(columns, ["codex", "cursor"], new Set(["codex", "cursor"] as const));
12+
it("holds unlabeled placeholder gridlines while loading providers have nothing to show", () => {
13+
const scale = chartScale([column(0)], new Set(["codex" as const]));
2614

2715
expect(scale.labeled).toBe(false);
2816
expect(scale.ticks.length).toBeGreaterThan(1);
2917
});
18+
19+
it("scales to what is on screen, loading or not", () => {
20+
expect(chartScale([column(40)], new Set(["codex" as const]))).toMatchObject({
21+
max: 40,
22+
labeled: true,
23+
});
24+
});
3025
});
3126

3227
describe("niceScale", () => {

0 commit comments

Comments
 (0)