From cf4ee648f17293a8b39fc891dcca2a6e935626dc Mon Sep 17 00:00:00 2001 From: Kiro Agent <244629292+kiro-agent@users.noreply.github.com> Date: Fri, 27 Nov 2026 17:28:03 +0000 Subject: [PATCH 1/2] [feat] agent: add per-turn timing overview to conversation Trace (Slice B of #468) --- docs/development/turn-timing.md | 13 + docs/development/turn-timing.zh-CN.md | 13 + src/canvas/AssistantTracePage.tsx | 13 +- src/canvas/TraceTimingOverview.tsx | 67 ++++ src/canvas/assistant-trace.css | 23 ++ src/canvas/traceTimingOverview.ts | 69 ++++ src/i18n/messages/en-US/trace.ts | 7 +- src/i18n/messages/zh-CN/trace.ts | 7 +- tests/browser/manifest.json | 8 + tests/browser/trace_timing_overview.mjs | 462 ++++++++++++++++++++++++ tests/traceTimingOverview.test.ts | 108 ++++++ 11 files changed, 785 insertions(+), 5 deletions(-) create mode 100644 src/canvas/TraceTimingOverview.tsx create mode 100644 src/canvas/traceTimingOverview.ts create mode 100644 tests/browser/trace_timing_overview.mjs create mode 100644 tests/traceTimingOverview.test.ts diff --git a/docs/development/turn-timing.md b/docs/development/turn-timing.md index 94aaa56ec..93619a36e 100644 --- a/docs/development/turn-timing.md +++ b/docs/development/turn-timing.md @@ -103,3 +103,16 @@ PR #469 (issue #449) records where a turn's time goes. The two features measure | Read by | `scripts/bench_turn_timing.py` | Trace API, Chat and Trace UI | The two windows share nearly the same end. The start differs by the pre-accept work in `produce()` (validation, model availability, the `begin` write), usually milliseconds, so the numbers are not expected to match. Neither is derived from the other. Each keeps its own monotonic clock: #469's clock lives in `produce()` and a ledger of tool events, ours lives in `Conversations.update`, which is the only place that sees both the accept and the finish write. Persistence does not conflict: #469 writes a separate file, this work writes the message. Do not show `turnSeconds` in the UI and do not read `durationMs` in the ledger. + +### Trace timing overview + +The Conversation Trace page shows a compact overview above the turn list. Each turn is one button row with its number, status, a bar and the duration. Bar length is linear against the longest finished turn. It reads only the persisted `durationMs` of each turn, never `startedAt`/`finishedAt`, and it does not load any trace logs. + +- A running turn shows "Running" and no bar. A missing or invalid `durationMs` shows "Unavailable" and no bar. +- Stopped and interrupted turns show their status and bar when `durationMs` is valid. +- A zero duration gets a thin marker instead of a bar. Any other bar has a minimum visible width. +- With two or more timed turns, the slowest one is marked with an accent colour, a symbol and an accessible label. +- Selecting a row (click, Enter, Space) reuses the existing reveal: it opens the turn, lazy-loads its trace and scrolls to it. +- If no turn has valid timing, the overview shows "No timing data" and the turn list works as before. + +Logic lives in `src/canvas/traceTimingOverview.ts`. The overview adds none of the #449 metrics. diff --git a/docs/development/turn-timing.zh-CN.md b/docs/development/turn-timing.zh-CN.md index 55a0442e4..4ed2a38fb 100644 --- a/docs/development/turn-timing.zh-CN.md +++ b/docs/development/turn-timing.zh-CN.md @@ -103,3 +103,16 @@ PR #469(issue #449)记录一个轮次的时间花在哪里。两项功能度 | 读取方 | `scripts/bench_turn_timing.py` | Trace API、Chat 与 Trace 界面 | 两个窗口的终点几乎相同,起点相差 `produce()` 中接受前的工作(校验、模型可用性检查、`begin` 写入),通常为毫秒级,数值不要求完全一致,也不互相推导。各自保留单调时钟:#469 的时钟在 `produce()` 和工具事件账本中,本工作的时钟在 `Conversations.update`,那里是唯一同时看到接受与 finish 写入的位置。持久化不冲突:#469 写独立文件,本工作写消息。UI 不展示 `turnSeconds`,账本不读取 `durationMs`。 + +### Trace 耗时概览 + +对话 Trace 页面在轮次列表上方显示紧凑的耗时概览。每个轮次对应一个按钮行,包含编号、状态、条形和耗时。条形长度相对已结束轮次中最长的一个按线性比例绘制。概览只读取各轮已持久化的 `durationMs`,不使用 `startedAt`/`finishedAt`,也不加载任何 Trace 日志。 + +- 运行中的轮次显示“运行中”,不绘制条形。`durationMs` 缺失或无效时显示“不可用”,不绘制条形。 +- 已停止和已中断的轮次在 `durationMs` 有效时显示状态与条形。 +- 耗时为零时显示细标记而不是条形,其他条形有最小可见宽度。 +- 有两个及以上带耗时的轮次时,最慢的一轮以强调色、符号和无障碍文本标出。 +- 选中某行(点击、Enter、空格)复用现有的定位逻辑:展开该轮、懒加载其 Trace 并滚动到位。 +- 若没有任何轮次有有效耗时,概览显示“暂无耗时数据”,轮次列表照常可用。 + +逻辑位于 `src/canvas/traceTimingOverview.ts`。概览不引入 #449 的任何指标。 diff --git a/src/canvas/AssistantTracePage.tsx b/src/canvas/AssistantTracePage.tsx index b410a75f5..e752ba6ec 100644 --- a/src/canvas/AssistantTracePage.tsx +++ b/src/canvas/AssistantTracePage.tsx @@ -1,6 +1,6 @@ import { useT } from "../i18n/I18nProvider"; import type { MessageKey } from "../i18n/types"; -import { useEffect, useMemo, useRef, useState } from "react"; +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { traceCategory, isThinkingTokenEstimate, readableTraceValue } from "./traceCategory.ts"; @@ -8,6 +8,7 @@ import { turnTiming, TURN_STATUS_KEYS } from "./turnTiming.ts"; import { initialTurn, normalizeTrace, runtimeName, traceRuntime, traceSources, type ConversationTurn } from "./traceTurns.ts"; import { readTurnJobs, turnJobView, type TurnJob } from "./traceJobs.ts"; +import TraceTimingOverview from "./TraceTimingOverview.tsx"; import { downloadFile } from "./assets.ts"; import { conversationTraceArchive, TraceExportError } from "./traceExport.ts"; @@ -301,6 +302,7 @@ function ConversationTrace({ project, conversationId, target, turns }: { const t = useT(); const [open, setOpen] = useState>(() => new Set()); const [loaded, setLoaded] = useState>(() => new Set()); + const [selected, setSelected] = useState(null); const placed = useRef(false); const initial = initialTurn(turns, target); const [exporting, setExporting] = useState(false); @@ -348,12 +350,16 @@ function ConversationTrace({ project, conversationId, target, turns }: { if (value) next.add(id); else next.delete(id); return next; }); + // Opening selects the turn. Closing keeps the selection: it marks the last turn the user worked on, and the turnnav shows open state. + if (value) setSelected(id); if (value) setLoaded((previous) => previous.has(id) ? previous : new Set(previous).add(id)); } - function reveal(id: string) { + // Only calls state setters, so it is stable and memoized overview rows are not re-rendered by it. + const reveal = useCallback((id: string) => { + setSelected(id); toggle(id, true); window.requestAnimationFrame(() => document.getElementById(`turn-${id}`)?.scrollIntoView({ block: "start" })); - } + }, []); useEffect(() => { if (placed.current || !initial) return; placed.current = true; @@ -370,6 +376,7 @@ function ConversationTrace({ project, conversationId, target, turns }: { {running && !exporting && {t("trace.export.blockedRunning")}} {exportError && {exportError}} +