diff --git a/apps/desktop/src/ui/ThreadDetails.tsx b/apps/desktop/src/ui/ThreadDetails.tsx index 850f5d11a..0f51cd3c0 100644 --- a/apps/desktop/src/ui/ThreadDetails.tsx +++ b/apps/desktop/src/ui/ThreadDetails.tsx @@ -11,7 +11,7 @@ import { type GitChanges, gitChangeSummary, openExternal, openPath } from "../li import { appPlatform, fileManagerName } from "../lib/platform"; import { contextPercent } from "../state/details"; import { turnItemsByEnd } from "../state/fold"; -import { formatCost } from "../state/pricing"; +import { estimateUsd, formatCost } from "../state/pricing"; import { PULL_LIMIT, usePullList } from "../state/pullQueries"; import { engineBusy } from "../state/runState"; import { showPanel, useApp, worktreeOf } from "../state/store"; @@ -29,6 +29,7 @@ import { pullsForBranches, readFolded, type OverviewSection as SectionId, + type TurnEntry, toggleFolded, turnEntries, turnHasTrace, @@ -251,6 +252,8 @@ export function ThreadDetails() { items={items ?? []} running={running} model={model} + harness={harnessId} + rawModel={summary?.model} folded={isFolded("log")} onToggle={toggle} /> @@ -322,16 +325,40 @@ const OUTCOME = { } as const; /** Finished turns, newest first. Open a row for its messages and calls. */ +/** + * A turn's price: what the harness reported, else its tokens at list price + * (marked `~`), else a dash, so every row has one. + */ +function turnPrice(turn: TurnEntry, harness: string | undefined, model: string | undefined): string { + if (turn.cost !== undefined) return formatCost(turn.cost); + const counts = turn.counts; + const estimate = + harness && counts + ? estimateUsd(harness, model, { + input: counts.inputTokens, + output: counts.outputTokens, + cached: counts.cachedTokens, + }) + : undefined; + if (estimate !== undefined) return `~${formatCost(estimate)}`; + return "$–"; +} + function TurnsSection({ items, running, model, + harness, + rawModel, folded, onToggle, }: { items: readonly TranscriptItem[]; running?: boolean; model?: string; + harness?: string; + /** The model's id as the harness names it, for list prices. */ + rawModel?: string; folded: boolean; onToggle: (id: SectionId) => void; }) { @@ -387,7 +414,7 @@ function TurnsSection({ type="button" aria-haspopup="dialog" onClick={() => setOpen(turn.id)} - className="flex min-h-8 w-full items-center gap-2 rounded-md px-2 text-left hover:bg-fill-subtle" + className="flex w-full items-center gap-2 rounded-md px-2 py-1 text-left hover:bg-fill-subtle" > - {turn.prompt} - - {turn.elapsedMs === undefined ? "" : formatDuration(turn.elapsedMs)} - - {turn.cost !== undefined ? ( - - {formatCost(turn.cost)} - - ) : turn.tokens !== undefined ? ( - - {formatTokens(turn.tokens)} + + {turn.prompt} + + {[ + turn.elapsedMs === undefined ? undefined : formatDuration(turn.elapsedMs), + turnPrice(turn, harness, rawModel), + ] + .filter(Boolean) + .join(" · ")} - ) : ( - - )} + ); diff --git a/apps/desktop/src/ui/overview.test.ts b/apps/desktop/src/ui/overview.test.ts index 7b119d34d..f796139cb 100644 --- a/apps/desktop/src/ui/overview.test.ts +++ b/apps/desktop/src/ui/overview.test.ts @@ -201,6 +201,25 @@ describe("the thread's turns", () => { expect(turn?.tokens).toBe(260); }); + test("a turn begun before the app knew the totals is priced from the turn before it", () => { + const sample = (inputTokens: number, cost: number) => ({ + used: 0, + size: 0, + estimated: false, + inputTokens, + outputTokens: 0, + cost, + }); + const turns = turnEntries([ + { kind: "user", id: "u1", text: "One" }, + turnEnd("e1", { usage: { before: sample(0, 0), samples: [{ at: 1, usage: sample(100, 0.2) }] } }), + { kind: "user", id: "u2", text: "Two" }, + turnEnd("e2", { usage: { samples: [{ at: 2, usage: sample(250, 0.5) }] } }), + ]); + expect(turns[1]?.cost).toBeCloseTo(0.3); + expect(turns[1]?.tokens).toBe(150); + }); + test("a turn the engine priced nothing for has no numbers, rather than a guess", () => { const [turn] = turnEntries([ { kind: "user", id: "u1", text: "Go" }, diff --git a/apps/desktop/src/ui/overview.ts b/apps/desktop/src/ui/overview.ts index f0dfdb48b..a3ba01f6a 100644 --- a/apps/desktop/src/ui/overview.ts +++ b/apps/desktop/src/ui/overview.ts @@ -6,7 +6,7 @@ import type { PullRequest } from "../acp/rustcode"; import type { FileDiff } from "../acp/types"; import { readStorage, writeStorage } from "../lib/storage"; -import { rowTokens, usageDelta } from "../lib/trace"; +import { rowTokens, type TraceUsage, usageDelta } from "../lib/trace"; import { type DelegateNode, delegateTree } from "../state/delegates"; import type { Link } from "../state/orchestration"; import { diffCounts, stripHarnessMarkup, type TranscriptItem, type TurnUsage } from "../state/thread"; @@ -284,6 +284,8 @@ export interface TurnEntry { cost?: number; /** Its input and output tokens together, when the engine reported them. */ tokens?: number; + /** Its token counts, so a turn the harness did not price can be estimated at list price. */ + counts?: TraceUsage; } /** What the log's stats row adds up (docs/design.md › the panel's rates). */ @@ -296,12 +298,20 @@ export interface LogTotals { agentMs: number; } -/** What a turn used: the last sample the engine sent it, minus the session's totals at its start. */ -function turnUsage(usage: TurnUsage | undefined): { cost?: number; tokens?: number } { +/** + * What a turn used: the last sample the engine sent it, minus the session's + * totals at its start. A turn that began before the app knew those totals + * (the first one after a reload) has no baseline of its own, so it starts + * from the last sample of the turn before it: the numbers are cumulative. + */ +function turnUsage( + usage: TurnUsage | undefined, + previous: TraceUsage | undefined, +): Pick { const last = usage?.samples.at(-1)?.usage; - const delta = usageDelta(last, usage?.before); + const delta = usageDelta(last, usage?.before ?? previous); if (!delta) return {}; - return { cost: delta.cost, tokens: rowTokens(delta) }; + return { cost: delta.cost, tokens: rowTokens(delta), counts: delta }; } /** A turn's opening line, without harness markup or the wake marker that starts one. */ @@ -325,6 +335,7 @@ export function turnEntries(items: readonly TranscriptItem[], running = false): let prompt = ""; let userId: string | undefined; let worked = false; + let lastSample: TraceUsage | undefined; for (let i = 0; i <= items.length; i++) { const item = items[i]; const ended = i > from && items[i - 1]!.kind === "turnEnd"; @@ -342,8 +353,9 @@ export function turnEntries(items: readonly TranscriptItem[], running = false): startedAt: end.startedAt, outcome: end.outcome, elapsedMs: end.elapsedMs, - ...turnUsage(end.usage), + ...turnUsage(end.usage, lastSample), }); + lastSample = end.usage?.samples.at(-1)?.usage; from = i; userId = undefined; worked = false; @@ -353,6 +365,7 @@ export function turnEntries(items: readonly TranscriptItem[], running = false): prompt: prompt || "No message", outcome: "completed", }); + lastSample = undefined; from = i; userId = undefined; worked = false;