Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/usage/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,14 @@ export async function readUsageEntriesForManagement(): Promise<PersistedUsageEnt
return (await readUsageSnapshotForManagement()).entries;
}

/** Keep legacy optional fields permissive, but reject rows that cannot be safely attributed. */
function normalizePersistedUsageRow(value: unknown): PersistedUsageEntry | undefined {
if (value === null || typeof value !== "object" || Array.isArray(value)) return undefined;
const row = value as Record<string, unknown>;
if (typeof row.requestId !== "string" || typeof row.provider !== "string") return undefined;
return normalizeUsageEntry(row as unknown as PersistedUsageEntry);
}

export function readUsageEntries(): PersistedUsageEntry[] {
const path = usageLogPath();
if (!existsSync(path)) return [];
Expand All @@ -1150,10 +1158,8 @@ export function readUsageEntries(): PersistedUsageEntry[] {
for (const line of lines) {
if (!line.trim()) continue;
try {
const parsed = JSON.parse(line) as PersistedUsageEntry;
if (parsed && typeof parsed === "object" && typeof parsed.requestId === "string") {
entries.push(normalizeUsageEntry(parsed));
}
const parsed = normalizePersistedUsageRow(JSON.parse(line));
if (parsed) entries.push(parsed);
} catch {
/* keep reading after a partially written or hand-edited line */
}
Expand All @@ -1166,10 +1172,8 @@ function parseUsageLines(lines: string[]): PersistedUsageEntry[] {
for (const line of lines) {
if (!line.trim()) continue;
try {
const parsed = JSON.parse(line) as PersistedUsageEntry;
if (parsed && typeof parsed === "object" && typeof parsed.requestId === "string") {
entries.push(normalizeUsageEntry(parsed));
}
const parsed = normalizePersistedUsageRow(JSON.parse(line));
if (parsed) entries.push(parsed);
} catch {
/* skip partial / hand-edited lines */
}
Expand Down
20 changes: 16 additions & 4 deletions tests/usage-log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ describe("usage log", () => {
test("usage byte-prefix truncation and entry-count truncation report independent metadata", async () => {
writeFileSync(
usageLogPath(),
`${Array.from({ length: 500_001 }, (_, index) => JSON.stringify({ requestId: String(index) })).join("\n")}\n`,
`${Array.from({ length: 500_001 }, (_, index) => JSON.stringify({ requestId: String(index), provider: "p" })).join("\n")}\n`,
);
const snapshot = await readUsageSnapshotForManagement();
expect(snapshot.entries).toHaveLength(500_000);
Expand Down Expand Up @@ -739,14 +739,26 @@ describe("usage log", () => {
}]);
});

test("skips malformed JSONL lines while keeping valid entries", () => {
test("skips malformed JSONL and rows without string usage identities", async () => {
writeFileSync(usageLogPath(), [
"{\"requestId\":\"a\",\"timestamp\":1,\"provider\":\"p\",\"model\":\"m\",\"status\":200,\"durationMs\":1,\"usageStatus\":\"unreported\"}",
persistedLine("a"),
"{not-json",
"{\"requestId\":\"b\",\"timestamp\":2,\"provider\":\"p\",\"model\":\"m\",\"status\":200,\"durationMs\":1,\"usageStatus\":\"reported\",\"usage\":{\"inputTokens\":1,\"outputTokens\":2},\"totalTokens\":3}",
"null",
"42",
"[]",
"{}",
JSON.stringify({ provider: "p", timestamp: 2 }),
JSON.stringify({ requestId: 42, provider: "p", timestamp: 2 }),
JSON.stringify({ requestId: "missing-provider", timestamp: 2 }),
JSON.stringify({ requestId: "null-provider", provider: null, timestamp: 2 }),
JSON.stringify({ requestId: "number-provider", provider: 42, timestamp: 2 }),
JSON.stringify({ requestId: "object-provider", provider: {}, timestamp: 2 }),
JSON.stringify({ requestId: "array-provider", provider: [], timestamp: 2 }),
persistedLine("b"),
].join("\n"));

expect(readUsageEntries().map(entry => entry.requestId)).toEqual(["a", "b"]);
expect((await readUsageEntriesForManagement()).map(entry => entry.requestId)).toEqual(["a", "b"]);
});

test("keeps missing usage distinct from zero usage", () => {
Expand Down
Loading