Skip to content

Commit 48075a9

Browse files
fix(usage): guard the task-level counters; one term for checkpoint (zh-CN) and token (zh-CN, zh-TW)
1 parent 7e5449b commit 48075a9

5 files changed

Lines changed: 57 additions & 14 deletions

File tree

‎src/i18n/locales/zh-CN/common.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webview-ui/src/i18n/locales/zh-CN/settings.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webview-ui/src/i18n/locales/zh-TW/settings.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webview-ui/src/utils/__tests__/usage.spec.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,43 @@ describe("a history with corrupted token counts in its per-model usage", () => {
358358
})
359359
})
360360

361+
describe("a history with corrupted counters on the task itself", () => {
362+
const corrupted = item({
363+
tokensIn: "900" as unknown as number,
364+
tokensOut: Number.NaN,
365+
cacheReads: Number.POSITIVE_INFINITY,
366+
cacheWrites: -40,
367+
totalCost: Number.NaN,
368+
})
369+
const sound = item({ tokensIn: 100, tokensOut: 50, cacheReads: 30, cacheWrites: 10, totalCost: 0.5 })
370+
371+
it("adds up only what is a count, so no total and no rate is NaN, a string or infinite", () => {
372+
const s = summarizeUsage([corrupted, sound], { range: "all", now: NOW })
373+
374+
expect(s.tokensIn).toBe(100)
375+
expect(s.tokensOut).toBe(50)
376+
expect(s.totalTokens).toBe(150)
377+
expect(s.totalCost).toBe(0.5)
378+
expect(s.cacheHitRate).toBeCloseTo(0.3, 9)
379+
expect(Number.isFinite(s.cacheHitRate)).toBe(true)
380+
expect(s.cacheWrites).toBe(10)
381+
})
382+
383+
it("ranks the tasks by cost and then by real tokens, whatever the corrupted counters hold", () => {
384+
const cheapBig = item({ id: "cheap-big", totalCost: 1, tokensIn: 5000, tokensOut: 0 })
385+
const cheapSmall = item({
386+
id: "cheap-small",
387+
totalCost: 1,
388+
tokensIn: "9999" as unknown as number,
389+
tokensOut: 1,
390+
})
391+
392+
const s = summarizeUsage([cheapSmall, cheapBig, corrupted], { range: "all", now: NOW })
393+
394+
expect(s.topTasks.map((t) => t.id)).toEqual(["cheap-big", "cheap-small", corrupted.id])
395+
})
396+
})
397+
361398
describe("primaryUsage", () => {
362399
it("returns the provider+model that used the most tokens", () => {
363400
const task = item({

‎webview-ui/src/utils/usage.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,17 @@ export interface UsageOptions {
115115
topTaskLimit?: number
116116
}
117117

118-
/**
119-
* The provider+model that did most of a task's work (by tokens), for labelling
120-
* a task row. Undefined when nothing was recorded.
121-
*/
122118
/** A token count of a recorded request: a history file that was corrupted or edited by hand may hold anything else. */
123119
const tokenCount = (value: unknown): number =>
124120
typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0
125121

122+
/** A cost, with what is not a finite number counted as nothing (the totals do the same). */
123+
const finiteCost = (value: unknown): number => (typeof value === "number" && Number.isFinite(value) ? value : 0)
124+
125+
/**
126+
* The provider+model that did most of a task's work (by tokens), for labelling
127+
* a task row. Undefined when nothing was recorded.
128+
*/
126129
export function primaryUsage(item: HistoryItem): { apiProvider?: string; modelId?: string } | undefined {
127130
const entries = item.usageByModel?.filter((e) => e.apiProvider || e.modelId)
128131
if (!entries?.length) return undefined
@@ -238,15 +241,16 @@ export function summarizeUsage(history: HistoryItem[], options: UsageOptions): U
238241

239242
for (const item of items) {
240243
const cost = Number.isFinite(item.totalCost) ? item.totalCost : 0
241-
const tIn = item.tokensIn || 0
242-
const tOut = item.tokensOut || 0
244+
// A history file that was corrupted or edited by hand may hold anything in a counter: only a count is added.
245+
const tIn = tokenCount(item.tokensIn)
246+
const tOut = tokenCount(item.tokensOut)
243247
const tokens = tIn + tOut
244248

245249
totalCost += cost
246250
tokensIn += tIn
247251
tokensOut += tOut
248-
cacheReads += item.cacheReads || 0
249-
cacheWrites += item.cacheWrites || 0
252+
cacheReads += tokenCount(item.cacheReads)
253+
cacheWrites += tokenCount(item.cacheWrites)
250254

251255
// Provider/model from what was recorded per request. A task with no record
252256
// (created before this was tracked) goes to a single "unrecorded" bucket.
@@ -333,8 +337,10 @@ export function summarizeUsage(history: HistoryItem[], options: UsageOptions): U
333337
topTasks: [...items]
334338
.sort(
335339
(a, b) =>
336-
(b.totalCost || 0) - (a.totalCost || 0) ||
337-
(b.tokensIn || 0) + (b.tokensOut || 0) - ((a.tokensIn || 0) + (a.tokensOut || 0)),
340+
finiteCost(b.totalCost) - finiteCost(a.totalCost) ||
341+
tokenCount(b.tokensIn) +
342+
tokenCount(b.tokensOut) -
343+
(tokenCount(a.tokensIn) + tokenCount(a.tokensOut)),
338344
)
339345
.slice(0, topTaskLimit),
340346
}

0 commit comments

Comments
 (0)