Skip to content

Commit a6c76a1

Browse files
committed
feat(es-app): size distribution and neutral notes on coalesced tool row (anomalyco#231)
1 parent b92bb53 commit a6c76a1

5 files changed

Lines changed: 91 additions & 10 deletions

File tree

‎packages/es-app/src/components/tool-run.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createMemo, For, Show, type ComponentProps, type JSX } from "solid-js"
22
import type { Part } from "@opencode-ai/sdk/v2"
33
import { Message, type ToolRenderers } from "@opencode-ai/session-ui/message-part"
44
import { runSummary, type ToolRunGroup } from "../transcript-groups"
5+
import { SIZE_LEGEND, sizeDistribution, toolMagnitude } from "../tool-magnitude"
56

67
// Collapsed by default. Members mount only while open, as their original chips.
78
// The summary is deterministic: tool names and counts, wall-clock span and
@@ -34,14 +35,18 @@ export const ToolRun = (props: {
3435
})
3536
const byPart = createMemo(() => new Map(props.run.members.map((member) => [member.partID, member] as const)))
3637
const summary = createMemo(() => runSummary([...members().values()].map((item) => item.part)))
37-
const label = () => [`${props.run.members.length} tool calls`, summary().tools, summary().spanLabel, ...summary().notes].filter(Boolean).join(", ")
38+
// Members that have not loaded yet still count, as not sized, so bins sum to the run size.
39+
const sizes = createMemo(() => sizeDistribution([...[...members().values()].map((item) => toolMagnitude(item.part)),
40+
...Array.from({ length: props.run.members.length - members().size }, () => ({ kind: "unknown" as const, reason: "call is not loaded" }))]))
41+
const label = () => [`${props.run.members.length} tool calls`, summary().tools, sizes().label, summary().spanLabel, ...summary().notes].filter(Boolean).join(", ")
3842
const id = () => `tool-run-${props.run.key.replace(/[^\w-]/g, "_")}`
3943
return <section class="tool-run" data-tool-run={props.run.key} data-open={props.open ? "true" : undefined}
4044
data-timeline-part-ids={props.run.members.map((member) => member.partID).join(",")} aria-label={`${props.run.members.length} tool calls`}>
4145
<button class="tool-run-summary" aria-expanded={props.open} aria-controls={id()} aria-label={label()} onClick={() => props.onToggle(!props.open)}>
4246
<span class="tool-run-disclosure" aria-hidden="true">›</span>
4347
<span class="tool-run-kind"><bdi dir="auto">{props.run.members.length} tool calls</bdi></span>
4448
<span class="tool-run-tools" dir="ltr" title={summary().tools}>{summary().tools}</span>
49+
<span class="tool-run-sizes" dir="ltr" title={`${sizes().label}\n${SIZE_LEGEND}`}>{sizes().text}</span>
4550
<Show when={summary().notes.length}><span class="tool-run-notes" dir="auto">{summary().notes.join(" · ")}</span></Show>
4651
<Show when={summary().span}>{(span) => <span class="tool-run-span" dir="ltr" title={summary().spanLabel}>{span()}</span>}</Show>
4752
</button>

‎packages/es-app/src/index.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,8 @@ textarea.vim-normal::selection { background: var(--lavender); color: var(--crust
19311931
}
19321932
.tool-run-kind { color: var(--text); font-weight: 500; white-space: nowrap; }
19331933
.tool-run-tools { flex: 1 1 6rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--font-mono); font-size: 12px; unicode-bidi: isolate; text-align: start; }
1934-
.tool-run-notes { min-width: 0; unicode-bidi: isolate; color: var(--red); font-size: 12px; overflow-wrap: anywhere; }
1934+
.tool-run-sizes { unicode-bidi: isolate; color: var(--subtext0); font: 400 12px/20px var(--font-mono); white-space: nowrap; }
1935+
.tool-run-notes { min-width: 0; unicode-bidi: isolate; color: var(--subtext0); font-size: 12px; overflow-wrap: anywhere; }
19351936
.tool-run-span { unicode-bidi: isolate; color: var(--subtext0); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
19361937
.tool-run-disclosure { flex: 0 0 16px; text-align: center; color: var(--subtext0); font-size: 16px; line-height: 1; }
19371938
&:dir(rtl) .tool-run-disclosure { transform: rotate(180deg); }

‎packages/es-app/src/tool-magnitude.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "bun:test"
22
import type { ToolPart } from "@opencode-ai/sdk/v2"
3-
import { magnitudeLabel, magnitudeRange, sizeClass, toolMagnitude } from "./tool-magnitude"
3+
import { magnitudeLabel, magnitudeRange, sizeClass, sizeDistribution, toolMagnitude } from "./tool-magnitude"
44

55
let next = 0
66
const completed = (input: Record<string, unknown>, output: string, extra: Partial<{ start: number; end: number; compacted: number; attachments: unknown[] }> = {}) => ({
@@ -75,3 +75,38 @@ describe("tool magnitude", () => {
7575
expect(magnitudeRange([running].map(toolMagnitude))).toBeUndefined()
7676
})
7777
})
78+
79+
describe("size distribution", () => {
80+
const sized = (chars: number) => toolMagnitude(completed({}, "x".repeat(chars)))
81+
82+
test("counts calls per class in canonical order, showing only nonzero classes", () => {
83+
const result = sizeDistribution([sized(40_000), sized(10), sized(5_000), sized(20), sized(15_000), sized(2_000), sized(30)])
84+
expect(result.text).toBe("XS×3 · S×1 · M×1 · L×1 · XL×1")
85+
expect(result.label).toBe("Size distribution of 7 calls: 3 XS, 1 S, 1 M, 1 L, 1 XL")
86+
expect(sizeDistribution([sized(10), sized(5_000), sized(6_000)]).text).toBe("XS×1 · M×2")
87+
})
88+
89+
test("lower-bound, compacted, unavailable and running sizes are unknown with reasons, and counts sum to the batch", () => {
90+
const truncated = completed({}, "x")
91+
truncated.state = { ...truncated.state, metadata: { truncated: true } } as ToolPart["state"]
92+
const capped = completed(Object.fromEntries(Array.from({ length: 500 }, (_, index) => [`k${index}`, "v"])), "x")
93+
const running = completed({}, "x")
94+
running.state = { status: "running", input: {}, time: { start: 0 } }
95+
const items = [sized(10), toolMagnitude(truncated), toolMagnitude(capped), toolMagnitude(completed({}, "x", { compacted: 5 })), toolMagnitude(running)]
96+
const result = sizeDistribution(items)
97+
expect(result.text).toBe("XS×1 · ?×4")
98+
expect(result.bins.reduce((sum, bin) => sum + bin.count, 0) + result.unknown).toBe(items.length)
99+
expect(result.label).toBe("Size distribution of 5 calls: 1 XS, 4 not sized (lower bound only: input walk capped or output truncated ×2; " +
100+
"output was cleared by compaction ×1; tool is running ×1)")
101+
})
102+
103+
test("attachments keep the text-size class and are stated as excluded", () => {
104+
const result = sizeDistribution([toolMagnitude(completed({}, "ok", { attachments: [{ type: "file" }] })), sized(10)])
105+
expect(result.text).toBe("XS×2")
106+
expect(result.label).toBe("Size distribution of 2 calls: 2 XS; attachments in 1 call(s) not counted")
107+
})
108+
109+
test("wall time never moves a call to another class", () => {
110+
expect(sizeDistribution([toolMagnitude(completed({}, "ok", { start: 0, end: 7_200_000 })), sized(10)]).text).toBe("XS×2")
111+
})
112+
})

‎packages/es-app/src/tool-magnitude.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ export const magnitudeRange = (items: ToolMagnitude[]) => {
6565
}
6666
}
6767

68+
// Batch distribution: how many CALLS fall in each class, in canonical order.
69+
// A lower-bound or unavailable size never claims a class; it is counted as
70+
// unknown with its reason, so the counts always sum to the number of calls.
71+
export const sizeDistribution = (items: ToolMagnitude[]) => {
72+
const counts = SIZE_CLASSES.map(() => 0)
73+
const reasons = new Map<string, number>()
74+
let attachments = 0
75+
items.forEach((item) => {
76+
const reason = item.kind === "running" ? "tool is running" : item.kind === "unknown" ? item.reason
77+
: item.partial ? "lower bound only: input walk capped or output truncated" : undefined
78+
if (reason) reasons.set(reason, (reasons.get(reason) ?? 0) + 1)
79+
if (item.kind === "sized" && !reason) counts[item.rank]! += 1
80+
if (item.kind === "sized" && item.attachments) attachments += 1
81+
})
82+
const unknown = items.length - counts.reduce((sum, count) => sum + count, 0)
83+
const bins = SIZE_CLASSES.flatMap((size, rank) => counts[rank] ? [{ size, count: counts[rank]! }] : [])
84+
const text = [...bins.map((bin) => `${bin.size}×${bin.count}`), ...(unknown ? [`?×${unknown}`] : [])].join(" · ")
85+
const label = `Size distribution of ${items.length} calls: ` +
86+
[...bins.map((bin) => `${bin.count} ${bin.size}`), ...(unknown ? [`${unknown} not sized (${[...reasons].map(([reason, count]) => `${reason} ×${count}`).join("; ")})`] : [])].join(", ") +
87+
(attachments ? `; attachments in ${attachments} call(s) not counted` : "")
88+
return { bins, unknown, attachments, text, label }
89+
}
90+
6891
export const chars = (value: number) => value < 1000 ? String(value) : `${(value / 1000).toFixed(value < 10_000 ? 1 : 0)}k`
6992

7093
export const duration = (ms: number) => {

‎packages/es-app/test/tool-runs.browser.mjs‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ const run = async (viewport, label) => {
4444
await expect(first.locator(".tool-run-span")).toHaveText("200ms")
4545
await expect(first.locator(".tool-run-notes")).toHaveCount(0)
4646
await expect(first.locator(".tool-size")).toHaveCount(0)
47+
// Per-call size classes, counted per call in canonical order.
48+
await expect(first.locator(".tool-run-sizes")).toHaveText("XS×4")
49+
await expect(first.locator(".tool-run-sizes")).toHaveAttribute("title", /^Size distribution of 4 calls: 4 XS\nSize = characters/)
4750
const second = runFor("p21_big")
4851
await expect(second).toHaveAttribute("data-timeline-part-ids", "p21_big,p22_fail,p23_a,p24_b")
4952
await expect(second.locator(".tool-run-tools")).toHaveText("read ×3 · bash")
5053
await expect(second.locator(".tool-run-notes")).toHaveText("1 failed")
5154
// Wall clock from first start to last end, not a sum.
5255
await expect(second.locator(".tool-run-span")).toHaveText("2.0s")
53-
await expect(second.locator(".tool-run-summary")).toHaveAttribute("aria-label", "4 tool calls, read ×3 · bash, 2.0s wall clock: first call start to last call end, 1 failed")
56+
await expect(second.locator(".tool-run-sizes")).toHaveText("XS×3 · L×1")
57+
await expect(second.locator(".tool-run-summary")).toHaveAttribute("aria-label",
58+
"4 tool calls, read ×3 · bash, Size distribution of 4 calls: 3 XS, 1 L, 2.0s wall clock: first call start to last call end, 1 failed")
59+
// Failure notes are quiet text, muted like the span, not a red alert.
60+
expect(await second.locator(".tool-run-notes").evaluate((element) => getComputedStyle(element).color))
61+
.toBe(await second.locator(".tool-run-span").evaluate((element) => getComputedStyle(element).color))
5462
// A shell call that reported a nonzero exit is counted, not split out.
5563
const third = runFor("p40_ta")
5664
await expect(third).toHaveAttribute("data-timeline-part-ids", "p40_ta,p41_tb,p42_tc,p43_fail,p44_sa,p45_sb,p46_sc")
@@ -119,8 +127,10 @@ const run = async (viewport, label) => {
119127
for (const id of ["p61_la", "p62_lb", "p63_lc", "p70_ma", "p71_mb"]) await expect(chip(id)).toHaveCount(engaged ? 1 : 0)
120128
await shot(page, `${label}-completed`)
121129
// The collapsed summary follows member updates rather than a cached snapshot.
122-
emitPart("msg_1012", { ...preview.tool("p71_mb", "edit", { filePath: "/workspace/m-b" }, "Error: stale file", { status: "error" }) })
130+
await expect(tail.locator(".tool-run-sizes")).toHaveText("XS×5")
131+
emitPart("msg_1012", { ...preview.tool("p71_mb", "edit", { filePath: "/workspace/m-b" }, "Error: stale file " + "e".repeat(12_000), { status: "error" }) })
123132
await expect(tail.locator(".tool-run-notes")).toHaveText("1 failed")
133+
await expect(tail.locator(".tool-run-sizes")).toHaveText("XS×4 · L×1")
124134
await expect(tail.locator(".tool-run-span")).toHaveText("2.0s")
125135
if (engaged) await tail.locator(".tool-run-summary").click()
126136
await first.locator(".tool-run-summary").click()
@@ -291,6 +301,7 @@ const many = async () => {
291301
const runs = page.locator(".tool-run")
292302
await expect(runs).toHaveCount(1, { timeout: 30_000 })
293303
await expect(runs.first().locator(".tool-run-kind")).toHaveText("300 tool calls")
304+
await expect(runs.first().locator(".tool-run-sizes")).toHaveText("XS×300")
294305
await expect(runs.first().locator(".tool-run-tools")).toHaveText("read ×60 · bash ×60 · edit ×60 · grep ×60 · task ×60")
295306
await expect(runs.first().locator(".tool-run-span")).toHaveText("499ms")
296307
await runs.first().locator(".tool-run-summary").click()
@@ -306,17 +317,18 @@ const many = async () => {
306317
// At 320px a run with every kind of status note wraps inside the viewport, LTR and RTL.
307318
const narrow = async () => {
308319
const small = await startToolRunsPreview(0)
309-
const untimed = small.tool("prt_n4", "read", { filePath: "/workspace/n4" }, "n4")
320+
const untimed = small.tool("prt_n4", "read", { filePath: "/workspace/n4" }, "L".repeat(15_000))
310321
delete untimed.state.time.end
311322
small.fixture.messages = [
312323
{ info: { id: "msg_n0000", sessionID: "ses_a", role: "user", agent: "build", model: { providerID: "fixture", modelID: "test" }, time: { created: 1 } },
313324
parts: [{ id: "msg_n0000_text", messageID: "msg_n0000", sessionID: "ses_a", type: "text", text: "Mixed outcomes." }] },
314325
small.assistant("msg_n0001", "msg_n0000", [
315326
small.tool("prt_n1", "bash", { command: "false" }, "boom", { metadata: { exit: 1 } }),
316-
small.tool("prt_n2", "apply_patch", { patchText: "x" }, "Tool execution aborted", { status: "error" }),
317-
small.tool("prt_n3", "bash", { command: "sleep" }, "", { metadata: { exit: null } }),
327+
small.tool("prt_n2", "apply_patch", { patchText: "x" }, "Tool execution aborted " + "s".repeat(2_000), { status: "error" }),
328+
small.tool("prt_n3", "bash", { command: "sleep" }, "M".repeat(5_000), { metadata: { exit: null } }),
318329
untimed,
319-
small.tool("prt_n5", "webfetch", { url: "https://example.com/a-very-long-url-segment" }, "page"),
330+
small.tool("prt_n5", "webfetch", { url: "https://example.com/a-very-long-url-segment" }, "X".repeat(40_000)),
331+
small.tool("prt_n6", "bash", { command: "yes" }, "y", { metadata: { exit: 0, truncated: true } }),
320332
], { info: { finish: "tool-calls" } }),
321333
small.assistant("msg_n0002", "msg_n0000", [small.text("prt_n_done", "Done.")], { info: { finish: "stop" } }),
322334
]
@@ -330,25 +342,30 @@ const narrow = async () => {
330342
const runs = page.locator(".tool-run")
331343
await expect(runs).toHaveCount(1, { timeout: 30_000 })
332344
await expect(runs.first().locator(".tool-run-notes")).toHaveText("1 failed · 1 interrupted · 1 exit unknown · 1 untimed")
345+
await expect(runs.first().locator(".tool-run-sizes")).toHaveText("XS×1 · S×1 · M×1 · L×1 · XL×1 · ?×1")
346+
await expect(runs.first().locator(".tool-run-sizes")).toHaveAttribute("title", /1 not sized \(lower bound only: input walk capped or output truncated ×1\)/)
333347
await expect(runs.first().locator(".tool-run-span")).toHaveText(/^≥/)
334348
for (const dir of ["ltr", "rtl"]) {
335349
await page.evaluate((value) => { document.documentElement.dir = value }, dir)
336350
await runs.first().scrollIntoViewIfNeeded()
337351
const box = await runs.first().locator(".tool-run-summary").evaluate((element) => {
338352
const rect = element.getBoundingClientRect()
339353
const notes = element.querySelector(".tool-run-notes")
354+
const sizes = element.querySelector(".tool-run-sizes")
340355
return { left: rect.left, right: rect.right, overflow: element.scrollWidth - element.clientWidth,
341-
notesClipped: notes.scrollWidth - notes.clientWidth, width: document.documentElement.clientWidth,
356+
notesClipped: notes.scrollWidth - notes.clientWidth, sizesClipped: sizes.scrollWidth - sizes.clientWidth, width: document.documentElement.clientWidth,
342357
children: [...element.children].map((child) => { const r = child.getBoundingClientRect(); return [r.left, r.right] }) }
343358
})
344359
expect(box.overflow).toBeLessThanOrEqual(0)
345360
expect(box.notesClipped).toBeLessThanOrEqual(0)
361+
expect(box.sizesClipped).toBeLessThanOrEqual(0)
346362
expect(box.left).toBeGreaterThanOrEqual(0)
347363
expect(box.right).toBeLessThanOrEqual(box.width)
348364
for (const [left, right] of box.children) expect(left >= box.left - 1 && right <= box.right + 1).toBe(true)
349365
// English notes and the span keep their reading order in an RTL layout.
350366
expect(await runs.first().locator(".tool-run-notes").evaluate((element) => getComputedStyle(element).direction)).toBe("ltr")
351367
expect(await runs.first().locator(".tool-run-span").evaluate((element) => getComputedStyle(element).direction)).toBe("ltr")
368+
expect(await runs.first().locator(".tool-run-sizes").evaluate((element) => getComputedStyle(element).direction)).toBe("ltr")
352369
await shot(page, `narrow-${dir}`)
353370
}
354371
} finally {

0 commit comments

Comments
 (0)