refactor(usage): Cursor account usage comes from provider-cursor (#17578)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
23957c704b
commit
5a9c664b3e
25 files changed
+1074
-773
No files matched your search
@@ -10,7 +10,6 @@ import { afterEach, assert, beforeEach, describe, it } from "@effect/vitest";
|
||||
|
||||
import { TEST_FORMATS } from "./usageTestFormats.ts";
|
||||
import { readTranscriptRecords } from "./usageTranscriptReader.ts";
|
||||
import { readCursorAccountUsage } from "./cursorUsageReader.ts";
|
||||
import { makeAntigravityUsageCache, readAntigravityUsage } from "./antigravityUsageReader.ts";
|
||||
|
||||
let dir: string;
|
||||
@@ -252,235 +251,6 @@ describe("readTranscriptRecords resume", () => {
|
||||
});
|
||||
|
||||
describe("SQLite usage readers", () => {
|
||||
it("reads Cursor account history with the default macOS Keychain login", async () => {
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
let keychainReads = 0;
|
||||
const result = await readCursorAccountUsage(
|
||||
{ kind: "keychain" },
|
||||
0,
|
||||
1781000000000,
|
||||
async (_url, init) => {
|
||||
assert.include(new Headers(init.headers).get("cookie") ?? "", "demo%3A%3A");
|
||||
return Response.json({ totalUsageEventsCount: 0, usageEventsDisplay: [] });
|
||||
},
|
||||
async () => {
|
||||
keychainReads++;
|
||||
return accessToken;
|
||||
},
|
||||
);
|
||||
assert.strictEqual(keychainReads, 1);
|
||||
assert.isNull(result.error);
|
||||
assert.isFalse(result.missing);
|
||||
assert.isNotNull(result.accountKey);
|
||||
});
|
||||
|
||||
it("reads paginated Cursor account history including headless calls with separate cache tokens", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo", exp: 4102444800 })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const pages: number[] = [];
|
||||
const signals: AbortSignal[] = [];
|
||||
const request = async (url: string, init: RequestInit) => {
|
||||
assert.strictEqual(String(url), "https://cursor.com/api/dashboard/get-filtered-usage-events");
|
||||
assert.strictEqual(init?.redirect, "error");
|
||||
const headers = new Headers(init?.headers);
|
||||
assert.strictEqual(headers.get("origin"), "https://cursor.com");
|
||||
assert.include(headers.get("cookie") ?? "", "WorkosCursorSessionToken=demo%3A%3A");
|
||||
const body = JSON.parse(String(init?.body));
|
||||
pages.push(body.page);
|
||||
if (init.signal) signals.push(init.signal);
|
||||
return Response.json({
|
||||
totalUsageEventsCount: 1001,
|
||||
usageEventsDisplay: Array.from({ length: body.page === 1 ? 1000 : 1 }, (_, index) => ({
|
||||
timestamp: String(1780000000000 + ((body.page - 1) * 1000 + index) * 1000),
|
||||
model: "claude-sonnet-4-5",
|
||||
conversationId: `conversation-${body.page}`,
|
||||
isHeadless: body.page === 2,
|
||||
chargedCents: 0,
|
||||
tokenUsage: {
|
||||
inputTokens: 10,
|
||||
outputTokens: 5,
|
||||
cacheReadTokens: 30,
|
||||
cacheWriteTokens: 2,
|
||||
totalCents: 25,
|
||||
},
|
||||
})),
|
||||
});
|
||||
};
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, request);
|
||||
assert.isNull(result.error);
|
||||
assert.deepStrictEqual(pages, [1, 2]);
|
||||
assert.lengthOf(signals, 2);
|
||||
assert.notStrictEqual(signals[0], signals[1]);
|
||||
assert.strictEqual(result.records.length, 1001);
|
||||
assert.strictEqual(result.records.at(-1)?.sessionId, "conversation-2");
|
||||
assert.deepStrictEqual(result.records[0]?.totals, {
|
||||
uncachedInputTokens: 10,
|
||||
cachedInputTokens: 30,
|
||||
cacheCreationTokens: 2,
|
||||
outputTokens: 5,
|
||||
reasoningTokens: 0,
|
||||
});
|
||||
assert.strictEqual(result.records[0]?.reportedCostUsd, 0.25);
|
||||
assert.isFalse(result.accountKey?.includes("demo") ?? true);
|
||||
});
|
||||
|
||||
it("reads Cursor account history beyond 100 pages", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const fullPage = Array.from({ length: 1000 }, () => ({ tokenUsage: null }));
|
||||
let requests = 0;
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests += 1;
|
||||
return Response.json({
|
||||
totalUsageEventsCount: 100_001,
|
||||
usageEventsDisplay: requests <= 100 ? fullPage : [{ tokenUsage: null }],
|
||||
});
|
||||
});
|
||||
assert.isNull(result.error);
|
||||
assert.strictEqual(requests, 101);
|
||||
assert.deepStrictEqual(result.records, []);
|
||||
});
|
||||
|
||||
it("accepts confirmed empty Cursor usage but rejects error envelopes", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
for (const body of [
|
||||
{},
|
||||
{ totalUsageEventsCount: 0 },
|
||||
{ totalUsageEventsCount: 0, usageEventsDisplay: [] },
|
||||
]) {
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () =>
|
||||
Response.json(body),
|
||||
);
|
||||
assert.isNull(result.error);
|
||||
assert.deepStrictEqual(result.records, []);
|
||||
assert.isFalse(result.missing);
|
||||
}
|
||||
for (const body of [
|
||||
{ error: "upstream error" },
|
||||
{ detail: "unknown error envelope" },
|
||||
{ totalUsageEventsCount: 0, error: "upstream error" },
|
||||
null,
|
||||
[],
|
||||
"invalid",
|
||||
0,
|
||||
]) {
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () =>
|
||||
Response.json(body),
|
||||
);
|
||||
assert.isNotNull(result.error);
|
||||
assert.deepStrictEqual(result.records, []);
|
||||
}
|
||||
});
|
||||
|
||||
it("requires a terminal Cursor page after a full page reaches the reported count", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
let requests = 0;
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests++;
|
||||
return Response.json(
|
||||
requests === 1
|
||||
? {
|
||||
totalUsageEventsCount: 1000,
|
||||
usageEventsDisplay: Array.from({ length: 1000 }, (_, index) => ({
|
||||
timestamp: String(1780000000000 + index),
|
||||
model: "gpt-5",
|
||||
tokenUsage: { inputTokens: 10, outputTokens: 5 },
|
||||
})),
|
||||
}
|
||||
: { totalUsageEventsCount: 1000 },
|
||||
);
|
||||
});
|
||||
assert.isNull(result.error);
|
||||
assert.strictEqual(result.records.length, 1000);
|
||||
assert.strictEqual(requests, 2);
|
||||
});
|
||||
|
||||
it("removes only count-proven Cursor boundary copies and preserves identical billed events", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo" })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const event = (index: number) => ({
|
||||
timestamp: String(1780000000000 + index),
|
||||
model: "gpt-5",
|
||||
tokenUsage: { inputTokens: 10, outputTokens: 5, totalCents: 1 },
|
||||
});
|
||||
for (const total of [2000, 2001]) {
|
||||
let requests = 0;
|
||||
const result = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests++;
|
||||
return Response.json({
|
||||
totalUsageEventsCount: total,
|
||||
usageEventsDisplay:
|
||||
requests === 1
|
||||
? Array.from({ length: 1000 }, (_, index) => event(index))
|
||||
: requests === 2
|
||||
? Array.from({ length: 1000 }, (_, index) => event(999 + index))
|
||||
: [event(1999)],
|
||||
});
|
||||
});
|
||||
assert.isNull(result.error);
|
||||
assert.strictEqual(result.records.length, total);
|
||||
assert.strictEqual(requests, 3);
|
||||
assert.strictEqual(result.records.at(-1)?.timestampMs, 1780000001999);
|
||||
assert.strictEqual(
|
||||
result.records.filter((record) => record.timestampMs === 1780000000999).length,
|
||||
total === 2000 ? 1 : 2,
|
||||
);
|
||||
assert.strictEqual(new Set(result.records.map((record) => record.dedupeKey)).size, total);
|
||||
}
|
||||
let requests = 0;
|
||||
const inconsistent = await readCursorAccountUsage(authPath, 0, 1781000000000, async () => {
|
||||
requests++;
|
||||
return Response.json({
|
||||
totalUsageEventsCount: 1001,
|
||||
usageEventsDisplay:
|
||||
requests === 1
|
||||
? Array.from({ length: 1000 }, (_, index) => event(index))
|
||||
: [event(500), event(1000)],
|
||||
});
|
||||
});
|
||||
assert.isNotNull(inconsistent.error);
|
||||
assert.deepStrictEqual(inconsistent.records, []);
|
||||
});
|
||||
|
||||
it("does not present truncated Cursor account pages or authentication failures as complete history", async () => {
|
||||
const authPath = NodePath.join(dir, "auth.json");
|
||||
const accessToken = `header.${Buffer.from(JSON.stringify({ sub: "auth|demo", exp: 4102444800 })).toString("base64url")}.signature`;
|
||||
await NodeFSP.writeFile(authPath, JSON.stringify({ accessToken }));
|
||||
const truncated = await readCursorAccountUsage(authPath, 0, 1781000000000, async () =>
|
||||
Response.json({ totalUsageEventsCount: 101, usageEventsDisplay: [] }),
|
||||
);
|
||||
assert.isNotNull(truncated.error);
|
||||
assert.deepStrictEqual(truncated.records, []);
|
||||
const denied = await readCursorAccountUsage(
|
||||
authPath,
|
||||
0,
|
||||
1781000000000,
|
||||
async () => new Response(accessToken, { status: 401 }),
|
||||
);
|
||||
assert.isNotNull(denied.error);
|
||||
assert.isFalse(denied.error?.includes(accessToken) ?? true);
|
||||
assert.deepStrictEqual(denied.records, []);
|
||||
let requested = false;
|
||||
const missing = await readCursorAccountUsage(
|
||||
NodePath.join(dir, "missing.json"),
|
||||
0,
|
||||
1781000000000,
|
||||
async () => {
|
||||
requested = true;
|
||||
return Response.json({});
|
||||
},
|
||||
);
|
||||
assert.isTrue(missing.missing);
|
||||
assert.isFalse(requested);
|
||||
});
|
||||
|
||||
it("deduplicates Antigravity generation and step usage while preserving retry model and token buckets", async () => {
|
||||
const db = new NodeSqlite.DatabaseSync(NodePath.join(dir, "session-1.db"));
|
||||
const stamp = protoNumber(1, 1780000000);
|
||||
|
||||
Reference in new issue
Block a user