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
55 changes: 39 additions & 16 deletions apps/desktop/src/ui/ThreadDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -29,6 +29,7 @@ import {
pullsForBranches,
readFolded,
type OverviewSection as SectionId,
type TurnEntry,
toggleFolded,
turnEntries,
turnHasTrace,
Expand Down Expand Up @@ -251,6 +252,8 @@ export function ThreadDetails() {
items={items ?? []}
running={running}
model={model}
harness={harnessId}
rawModel={summary?.model}
folded={isFolded("log")}
onToggle={toggle}
/>
Expand Down Expand Up @@ -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;
}) {
Expand Down Expand Up @@ -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"
>
<span
role="img"
Expand All @@ -396,21 +423,17 @@ function TurnsSection({
>
<Icon size={13} />
</span>
<span className="min-w-0 flex-1 truncate text-callout">{turn.prompt}</span>
<span className="w-14 shrink-0 text-right text-caption text-fg-muted tabular">
{turn.elapsedMs === undefined ? "" : formatDuration(turn.elapsedMs)}
</span>
{turn.cost !== undefined ? (
<span className="w-14 shrink-0 text-right text-caption text-fg-muted tabular">
{formatCost(turn.cost)}
</span>
) : turn.tokens !== undefined ? (
<span className="w-14 shrink-0 text-right text-caption text-fg-muted tabular">
{formatTokens(turn.tokens)}
<span className="flex min-w-0 flex-1 flex-col">
<span className="truncate text-callout">{turn.prompt}</span>
<span className="truncate text-caption text-fg-muted tabular">
{[
turn.elapsedMs === undefined ? undefined : formatDuration(turn.elapsedMs),
turnPrice(turn, harness, rawModel),
]
.filter(Boolean)
.join(" · ")}
</span>
) : (
<span className="w-14 shrink-0" />
)}
</span>
<ChevronRight size={14} className="shrink-0 text-fg-muted" />
</button>
);
Expand Down
19 changes: 19 additions & 0 deletions apps/desktop/src/ui/overview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
25 changes: 19 additions & 6 deletions apps/desktop/src/ui/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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). */
Expand All @@ -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<TurnEntry, "cost" | "tokens" | "counts"> {
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. */
Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Loading