Skip to content
Closed
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
14 changes: 10 additions & 4 deletions src/usage/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,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") {
const parsed: unknown = JSON.parse(line);
if (isPersistedUsageEntry(parsed)) {
entries.push(normalizeUsageEntry(parsed));
}
} catch {
Expand All @@ -626,13 +626,19 @@ export function readUsageEntries(): PersistedUsageEntry[] {
return entries;
}

function isPersistedUsageEntry(value: unknown): value is PersistedUsageEntry {
if (!value || typeof value !== "object") return false;
const entry = value as Partial<PersistedUsageEntry>;
return typeof entry.requestId === "string" && typeof entry.provider === "string";
}

function parseUsageLines(lines: string[]): PersistedUsageEntry[] {
const entries: 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") {
const parsed: unknown = JSON.parse(line);
if (isPersistedUsageEntry(parsed)) {
entries.push(normalizeUsageEntry(parsed));
}
} catch {
Expand Down
7 changes: 5 additions & 2 deletions tests/usage-log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,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 @@ -654,14 +654,17 @@ describe("usage log", () => {
}]);
});

test("skips malformed JSONL lines while keeping valid entries", () => {
test("skips malformed JSONL and provider-less entries while keeping valid entries", async () => {
writeFileSync(usageLogPath(), [
"{\"requestId\":\"a\",\"timestamp\":1,\"provider\":\"p\",\"model\":\"m\",\"status\":200,\"durationMs\":1,\"usageStatus\":\"unreported\"}",
"{not-json",
"{\"requestId\":\"missing-provider\",\"timestamp\":2}",
"{\"requestId\":\"invalid-provider\",\"timestamp\":2,\"provider\":42}",
"{\"requestId\":\"b\",\"timestamp\":2,\"provider\":\"p\",\"model\":\"m\",\"status\":200,\"durationMs\":1,\"usageStatus\":\"reported\",\"usage\":{\"inputTokens\":1,\"outputTokens\":2},\"totalTokens\":3}",
].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