Skip to content

Commit b01a381

Browse files
Duncanwpfleger96
andcommitted
feat(desktop): display cache-read/write/fresh-input breakdown in agent-usage focused view
The plumbing already returned cacheReadTokens/cacheWriteTokens/freshInputTokens at every scope of AgentUsageSeries, but the focused view rendered only the four top-level stats and never surfaced them. Add an Input-breakdown subsection to the totals card (Cache read / Cache write / Fresh input) gated on any known cache value, and a compact per-model breakdown line. Fields follow the existing unknown-not-zero semantics: absent subsets are omitted, a known-but-incomplete value shows its lower bound with a Partial marker, and a scope with no cache data renders no subsection at all. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 3d723a7 commit b01a381

5 files changed

Lines changed: 393 additions & 34 deletions

File tree

‎desktop/src/features/agent-usage/lib/agentUsage.test.mjs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import {
1313
formatCoverageDate,
1414
formatEstimatedCostUsd,
1515
formatLocalDate,
16+
formatModelCacheBreakdown,
1617
formatTokenCountCompact,
1718
formatTokenCountExact,
19+
hasKnownCacheData,
1820
isPartialField,
1921
isUnknownField,
2022
MAX_RANGE_DAYS,
@@ -443,6 +445,85 @@ test("isPartialField and isUnknownField classify usage fields correctly", () =>
443445
assert.equal(isUnknownField(usageField({ value: "0" })), false);
444446
});
445447

448+
// ── formatModelCacheBreakdown / hasKnownCacheData ────────────────────────────
449+
450+
test("hasKnownCacheData is true iff any cache subset carries a known value", () => {
451+
assert.equal(hasKnownCacheData(reportedUsage()), false, "all absent → false");
452+
for (const field of [
453+
"cacheReadTokens",
454+
"cacheWriteTokens",
455+
"freshInputTokens",
456+
]) {
457+
assert.equal(
458+
hasKnownCacheData(reportedUsage({ [field]: usageField({ value: "0" }) })),
459+
true,
460+
`a known ${field} (even zero) → true`,
461+
);
462+
}
463+
// A field that is incomplete but has no value is still unknown, not known.
464+
assert.equal(
465+
hasKnownCacheData(
466+
reportedUsage({
467+
cacheReadTokens: usageField({ value: null, incomplete: true }),
468+
}),
469+
),
470+
false,
471+
"incomplete with null value is unknown, not known",
472+
);
473+
});
474+
475+
test("formatModelCacheBreakdown omits absent subsets, never renders them as zero, and marks a known lower bound Partial", () => {
476+
const build = (overrides) =>
477+
modelUsage("m", null, { usage: reportedUsage(overrides) });
478+
479+
// No cache data at all → null, so the caller omits the line entirely.
480+
assert.equal(formatModelCacheBreakdown(build({})), null);
481+
482+
// Each known subset appears compact-formatted; absent ones are omitted
483+
// rather than shown as "0".
484+
assert.equal(
485+
formatModelCacheBreakdown(
486+
build({
487+
cacheReadTokens: usageField({ value: "1500" }),
488+
cacheWriteTokens: usageField({ value: "300" }),
489+
freshInputTokens: usageField({ value: "1200000" }),
490+
}),
491+
),
492+
"Cache read 1.5K · Cache write 300 · Fresh 1.2M",
493+
);
494+
495+
// Only cache-read known — the other two are unknown and omitted, not zero.
496+
assert.equal(
497+
formatModelCacheBreakdown(
498+
build({ cacheReadTokens: usageField({ value: "800" }) }),
499+
),
500+
"Cache read 800",
501+
);
502+
503+
// An incomplete known field appends a single trailing Partial marker.
504+
assert.equal(
505+
formatModelCacheBreakdown(
506+
build({
507+
cacheReadTokens: usageField({ value: "800", incomplete: true }),
508+
cacheWriteTokens: usageField({ value: "200" }),
509+
}),
510+
),
511+
"Cache read 800 · Cache write 200 · Partial",
512+
);
513+
514+
// An incomplete field with NO value carries no known lower bound: it is
515+
// omitted and does not trigger Partial on its own.
516+
assert.equal(
517+
formatModelCacheBreakdown(
518+
build({
519+
cacheReadTokens: usageField({ value: "800" }),
520+
cacheWriteTokens: usageField({ value: null, incomplete: true }),
521+
}),
522+
),
523+
"Cache read 800",
524+
);
525+
});
526+
446527
// ── sumKnownBucketTotals ──────────────────────────────────────────────────────
447528

448529
function bucket(overrides = {}) {

‎desktop/src/features/agent-usage/lib/agentUsage.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,57 @@ export function isUnknownField(field: UsageField | CostField): boolean {
534534
return field.value === null;
535535
}
536536

537+
/**
538+
* Compact per-model input breakdown for the focused view: the cache-read,
539+
* cache-write, and fresh-input subsets of input, each shown only when known
540+
* (`value !== null`) and never as zero. Returns `null` when no subset is
541+
* known at all, so the caller omits the line entirely rather than printing
542+
* three unknowns. A trailing ` · Partial` marks that at least one shown field
543+
* is a known-but-incomplete lower bound — matching the row's Partial badge and
544+
* {@link deriveUsageIngressTrailing}'s text convention. Absent (`null`) fields
545+
* are simply omitted, never marked Partial (they carry no known lower bound).
546+
*/
547+
export function formatModelCacheBreakdown(
548+
model: AgentUsageModel,
549+
): string | null {
550+
const { cacheReadTokens, cacheWriteTokens, freshInputTokens } = model.usage;
551+
const parts: string[] = [];
552+
const push = (label: string, field: UsageField) => {
553+
const parsed = parseTokenCount(field.value);
554+
if (parsed !== null) {
555+
parts.push(`${label} ${formatTokenCountCompact(parsed)}`);
556+
}
557+
};
558+
push("Cache read", cacheReadTokens);
559+
push("Cache write", cacheWriteTokens);
560+
push("Fresh", freshInputTokens);
561+
if (parts.length === 0) return null;
562+
const partial =
563+
isPartialField(cacheReadTokens) ||
564+
isPartialField(cacheWriteTokens) ||
565+
isPartialField(freshInputTokens);
566+
return partial ? `${parts.join(" · ")} · Partial` : parts.join(" · ");
567+
}
568+
569+
/**
570+
* True when a usage scope has any known cache-read, cache-write, or
571+
* fresh-input value — the gate for showing the focused view's input-breakdown
572+
* subsection. When every cache subset is unknown (old-harness data that never
573+
* reported cache tokens), the subsection is omitted rather than rendering a
574+
* row of "—", which keeps absence honest without visual noise.
575+
*/
576+
export function hasKnownCacheData(usage: {
577+
cacheReadTokens: UsageField;
578+
cacheWriteTokens: UsageField;
579+
freshInputTokens: UsageField;
580+
}): boolean {
581+
return (
582+
!isUnknownField(usage.cacheReadTokens) ||
583+
!isUnknownField(usage.cacheWriteTokens) ||
584+
!isUnknownField(usage.freshInputTokens)
585+
);
586+
}
587+
537588
/**
538589
* Truthful trailing summary for the profile Info-tab Usage ingress row
539590
* (plan:328): the viewer's own agent's 7-day known total, `Partial` when

‎desktop/src/features/agent-usage/ui/AgentUsageFocusedView.tsx‎

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import {
1818
describeRange,
1919
formatCoverageDate,
2020
formatEstimatedCostUsd,
21+
formatModelCacheBreakdown,
2122
formatTokenCountCompact,
2223
formatTokenCountExact,
24+
hasKnownCacheData,
2325
isPartialField,
2426
isUnknownField,
2527
parseTokenCount,
@@ -218,6 +220,8 @@ function AgentUsageFocusedTotals({
218220

219221
// Display total for the Total tokens stat.
220222
const displayTotal = deriveDisplayTotal(agent.usage);
223+
const { cacheReadTokens, cacheWriteTokens, freshInputTokens } = agent.usage;
224+
const showCacheBreakdown = hasKnownCacheData(agent.usage);
221225

222226
return (
223227
<Card className="space-y-4 p-6" data-testid="agent-usage-focused-totals">
@@ -236,6 +240,34 @@ function AgentUsageFocusedTotals({
236240
/>
237241
</div>
238242

243+
{showCacheBreakdown ? (
244+
<div
245+
className="space-y-2 border-t border-border pt-4"
246+
data-testid="agent-usage-focused-cache"
247+
>
248+
<h3 className="text-sm font-medium text-foreground">
249+
Input breakdown
250+
</h3>
251+
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3">
252+
<TokenStat
253+
field={cacheReadTokens}
254+
label="Cache read"
255+
testId="agent-usage-focused-cache-read-value"
256+
/>
257+
<TokenStat
258+
field={cacheWriteTokens}
259+
label="Cache write"
260+
testId="agent-usage-focused-cache-write-value"
261+
/>
262+
<TokenStat
263+
field={freshInputTokens}
264+
label="Fresh input"
265+
testId="agent-usage-focused-fresh-input-value"
266+
/>
267+
</div>
268+
</div>
269+
) : null}
270+
239271
{agent.buckets.length > 0 ? (
240272
<div
241273
className="space-y-2 border-t border-border pt-4"
@@ -253,35 +285,10 @@ function AgentUsageFocusedTotals({
253285
>
254286
<h3 className="text-sm font-medium text-foreground">By model</h3>
255287
{models.map((model) => (
256-
<div
257-
className="flex items-center justify-between gap-3 text-sm"
288+
<FocusedModelRow
258289
key={`${model.harness ?? ""}:${model.model ?? "unknown"}`}
259-
>
260-
<span className="min-w-0 truncate text-muted-foreground">
261-
{model.model ?? "Unknown model"}
262-
{model.harness !== null ? (
263-
<span
264-
className="ml-1.5 text-xs text-muted-foreground/70"
265-
data-testid="agent-usage-model-harness-label"
266-
>
267-
{model.harness}
268-
</span>
269-
) : null}
270-
</span>
271-
<span className="shrink-0 font-medium text-foreground">
272-
{isUnknownField(model.usage.totalTokens)
273-
? formatModelIndependentFields(model)
274-
: formatTokenCountExact(
275-
parseTokenCount(model.usage.totalTokens.value) ?? 0n,
276-
)}
277-
{isPartialField(model.usage.totalTokens) ||
278-
isModelIoPartial(model) ? (
279-
<Badge className="ml-2" variant="outline">
280-
Partial
281-
</Badge>
282-
) : null}
283-
</span>
284-
</div>
290+
model={model}
291+
/>
285292
))}
286293
</div>
287294
) : null}
@@ -318,16 +325,19 @@ function AgentUsageFocusedTotals({
318325
function TokenStat({
319326
field,
320327
label,
328+
testId,
321329
}: {
322330
field: { value: string | null; incomplete: boolean };
323331
label: string;
332+
testId?: string;
324333
}) {
325334
const parsed = parseTokenCount(field.value);
326335
return (
327336
<UsageStat
328337
display={parsed !== null ? formatTokenCountExact(parsed) : null}
329338
isPartial={isPartialField(field)}
330339
label={label}
340+
testId={testId}
331341
/>
332342
);
333343
}
@@ -400,6 +410,55 @@ function formatCoverageRange(coverage: AgentUsageSeries["coverage"]): string {
400410
return `${formatCoverageDate(firstReportedAt)} – ${formatCoverageDate(lastReportedAt)}`;
401411
}
402412

413+
/**
414+
* One "By model" row: the model/harness label with its display total and
415+
* Partial badge, plus a muted cache/fresh-input breakdown line beneath it
416+
* when any cache subset is known. The breakdown is computed once and omitted
417+
* entirely when no subset was reported, so old-harness models read no
418+
* differently than before.
419+
*/
420+
function FocusedModelRow({ model }: { model: AgentUsageModel }) {
421+
const cacheBreakdown = formatModelCacheBreakdown(model);
422+
return (
423+
<div className="space-y-0.5">
424+
<div className="flex items-center justify-between gap-3 text-sm">
425+
<span className="min-w-0 truncate text-muted-foreground">
426+
{model.model ?? "Unknown model"}
427+
{model.harness !== null ? (
428+
<span
429+
className="ml-1.5 text-xs text-muted-foreground/70"
430+
data-testid="agent-usage-model-harness-label"
431+
>
432+
{model.harness}
433+
</span>
434+
) : null}
435+
</span>
436+
<span className="shrink-0 font-medium text-foreground">
437+
{isUnknownField(model.usage.totalTokens)
438+
? formatModelIndependentFields(model)
439+
: formatTokenCountExact(
440+
parseTokenCount(model.usage.totalTokens.value) ?? 0n,
441+
)}
442+
{isPartialField(model.usage.totalTokens) ||
443+
isModelIoPartial(model) ? (
444+
<Badge className="ml-2" variant="outline">
445+
Partial
446+
</Badge>
447+
) : null}
448+
</span>
449+
</div>
450+
{cacheBreakdown !== null ? (
451+
<p
452+
className="text-xs text-muted-foreground/70"
453+
data-testid="agent-usage-model-cache-breakdown"
454+
>
455+
{cacheBreakdown}
456+
</p>
457+
) : null}
458+
</div>
459+
);
460+
}
461+
403462
/**
404463
* Render known model I/O fields when the model total is unknown — never
405464
* collapses to "No usage reported" when input or output is actually known

‎desktop/tests/e2e/agent-usage-screenshots.spec.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ function reportedUsage(
2424
outputTokens: string | null;
2525
totalTokens: string | null;
2626
estimatedCostUsd: number | null;
27+
cacheReadTokens: string | null;
28+
cacheWriteTokens: string | null;
29+
freshInputTokens: string | null;
2730
}> = {},
2831
) {
2932
return {
3033
estimatedCostUsd: costField(overrides.estimatedCostUsd ?? null),
3134
inputTokens: usageField(overrides.inputTokens ?? null),
3235
outputTokens: usageField(overrides.outputTokens ?? null),
3336
totalTokens: usageField(overrides.totalTokens ?? null),
34-
cacheReadTokens: usageField(null),
35-
cacheWriteTokens: usageField(null),
36-
freshInputTokens: usageField(null),
37+
cacheReadTokens: usageField(overrides.cacheReadTokens ?? null),
38+
cacheWriteTokens: usageField(overrides.cacheWriteTokens ?? null),
39+
freshInputTokens: usageField(overrides.freshInputTokens ?? null),
3740
};
3841
}
3942

@@ -296,6 +299,9 @@ test.describe("agent usage screenshots", () => {
296299
inputTokens: "2100",
297300
outputTokens: "700",
298301
estimatedCostUsd: 0.35,
302+
cacheReadTokens: "1400",
303+
cacheWriteTokens: "300",
304+
freshInputTokens: "400",
299305
}),
300306
},
301307
{
@@ -308,6 +314,9 @@ test.describe("agent usage screenshots", () => {
308314
inputTokens: "600",
309315
outputTokens: "200",
310316
estimatedCostUsd: 0.04,
317+
cacheReadTokens: "350",
318+
cacheWriteTokens: "50",
319+
freshInputTokens: "200",
311320
}),
312321
},
313322
],
@@ -316,6 +325,9 @@ test.describe("agent usage screenshots", () => {
316325
inputTokens: "2700",
317326
outputTokens: "900",
318327
totalTokens: "3600",
328+
cacheReadTokens: "1750",
329+
cacheWriteTokens: "350",
330+
freshInputTokens: "600",
319331
}),
320332
}),
321333
],

0 commit comments

Comments
 (0)