Skip to content

Commit 62a1eac

Browse files
committed
fix(usage): keep large and partial histories accurate
1 parent 393a8f3 commit 62a1eac

4 files changed

Lines changed: 58 additions & 10 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ export async function readCursorAccountUsage(
9090
let completed = false;
9191
const pageSize = 1000;
9292
let total: number | undefined;
93-
for (let page = 1; page <= 100; page++) {
93+
for (let page = 1; ; page++) {
94+
// A count can include overlapping page boundaries. Allow room to
95+
// reconcile them without imposing a fixed account-size limit.
96+
if (page > (total === undefined ? 1000 : Math.ceil(total / pageSize) * 2 + 1)) {
97+
throw new Error("Account usage page limit exceeded");
98+
}
9499
const response = await request("https://cursor.com/api/dashboard/get-filtered-usage-events", {
95100
method: "POST",
96101
redirect: "error",
@@ -134,7 +139,6 @@ export async function readCursorAccountUsage(
134139
(typeof count !== "number" ||
135140
!Number.isSafeInteger(count) ||
136141
count < 0 ||
137-
count > pageSize * 100 ||
138142
(total !== undefined && count !== total))) ||
139143
!Array.isArray(events) ||
140144
events.length > pageSize ||

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,24 @@ describe("SQLite usage readers", () => {
288288
assert.isFalse(result.accountKey?.includes("demo") ?? true);
289289
});
290290

291+
it("reads Cursor account history beyond 100 pages", async () => {
292+
const authPath = NodePath.join(dir, "auth.json");
293+
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
294+
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
295+
const fullPage = Array.from({ length: 1000 }, () => ({ tokenUsage: null }));
296+
let requests = 0;
297+
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
298+
requests += 1;
299+
return Response.json({
300+
totalUsageEventsCount: 100_001,
301+
usageEventsDisplay: requests <= 100 ? fullPage : [{ tokenUsage: null }],
302+
});
303+
});
304+
assert.isNull(result.error);
305+
assert.strictEqual(requests, 101);
306+
assert.deepStrictEqual(result.records, []);
307+
});
308+
291309
it("accepts confirmed empty Cursor usage but rejects error envelopes", async () => {
292310
const authPath = NodePath.join(dir, "auth.json");
293311
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;

‎packages/shared/src/usageMerge.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@ describe("mergeUsage", () => {
227227
}
228228
});
229229

230+
it("prefers a complete scan over a newer partial scan of the same directory", () => {
231+
const source = { provider: "claude" as const, hostId: "mac", homePath: "/home/theo/.claude" };
232+
const incomplete = summary([bucket({ costUsd: 4, records: 2 })], [source]);
233+
const partial = environment("new", {
234+
...incomplete,
235+
readAt: "2026-08-07T01:00:00.000Z",
236+
sources: incomplete.sources.map((entry) => ({ ...entry, status: "partial" as const })),
237+
});
238+
const complete = environment("old", summary([bucket()], [source]));
239+
240+
for (const ordered of [
241+
[partial, complete],
242+
[complete, partial],
243+
]) {
244+
const merged = mergeUsage(ordered, USAGE_CONTRACT_VERSION);
245+
expect(merged.costUsd).toBe(10);
246+
expect(merged.contributingEnvironments).toEqual(["old"]);
247+
expect(merged.duplicateSources).toEqual(["new: /home/theo/.claude"]);
248+
}
249+
expect(mergeUsage([partial], USAGE_CONTRACT_VERSION).costUsd).toBe(4);
250+
});
251+
230252
it("excludes an environment reporting an older contract version", () => {
231253
const merged = mergeUsage(
232254
[

‎packages/shared/src/usageMerge.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,19 @@ function claimSources(environments: readonly EnvironmentUsage[]): {
135135
a.environmentId.localeCompare(b.environmentId),
136136
);
137137

138-
for (const environment of ordered) {
139-
for (const source of environment.summary.sources) {
140-
if (source.status === "missing") continue;
141-
const key = fingerprintKey(source.fingerprint);
142-
if (ownerByFingerprint.has(key)) {
143-
duplicates.push(`${environment.label}: ${source.fingerprint.resolvedHomePath}`);
144-
continue;
138+
// A complete scan takes precedence over a newer partial scan of the same
139+
// directory. Partial history still contributes when no complete copy exists.
140+
for (const status of ["ok", "partial", "failed"] as const) {
141+
for (const environment of ordered) {
142+
for (const source of environment.summary.sources) {
143+
if (source.status !== status) continue;
144+
const key = fingerprintKey(source.fingerprint);
145+
if (ownerByFingerprint.has(key)) {
146+
duplicates.push(`${environment.label}: ${source.fingerprint.resolvedHomePath}`);
147+
continue;
148+
}
149+
ownerByFingerprint.set(key, environment.environmentId);
145150
}
146-
ownerByFingerprint.set(key, environment.environmentId);
147151
}
148152
}
149153

0 commit comments

Comments
 (0)