TypeScript
fix(chat): keep latest command live between messages (#9098)
maria590a579f2e92
diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.tsindex d9bfdae04d4..d702d6ddda7 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts@@ -826,7 +826,7 @@ describe("deriveMessagesTimelineRows", () => {"assistant-final-entry","user-followup-entry","working-indicator-row",- "thinking-indicator-row",+ "live-activity-row",]);const finalRow = rows.find((row) => row.id === "assistant-final-entry");expect(finalRow?.kind === "message" && finalRow.showAssistantMeta).toBe(true);@@ -879,11 +879,11 @@ describe("deriveMessagesTimelineRows", () => {expect(rows.map((row) => row.id)).toEqual(["working-indicator-row","assistant-thought-entry",- "work-live:work-entry-1",+ "live-activity-row",]);});- it("keeps adjacent active tool calls in one replacing row", () => {+ it("keeps an actually running tool in the shared activity row", () => {const rows = deriveMessagesTimelineRows({timelineEntries: [{@@ -948,6 +948,7 @@ describe("deriveMessagesTimelineRows", () => {expect(rows.some((row) => row.kind === "thinking")).toBe(false);expect(rows.find((row) => row.kind === "work-live")).toMatchObject({entry: { id: "running-command" },+ active: true,groupedEntries: [{ id: "running-command" },{ id: "completed-edit" },@@ -1196,40 +1197,58 @@ describe("deriveMessagesTimelineRows", () => {]);});- it("shows thinking after the latest tool call completes while the turn is running", () => {- const rows = deriveMessagesTimelineRows({- timelineEntries: [- {- id: "latest-command-entry",- kind: "work",- createdAt: "2026-01-01T00:00:05Z",- entry: {- id: "latest-command",- createdAt: "2026-01-01T00:00:05Z",- turnId: "turn-1" as never,- label: "Ran rg",- command: "rg toolCall",- requestKind: "command",- tone: "tool" as const,- toolLifecycleStatus: "completed" as const,- },+ it("reuses one activity row for initial thinking and the latest tool", () => {+ const deriveRows = (toolLifecycleStatus: "inProgress" | "completed" | "declined" | null) =>+ deriveMessagesTimelineRows({+ timelineEntries:+ toolLifecycleStatus === null+ ? []+ : [+ {+ id: "latest-command-entry",+ kind: "work",+ createdAt: "2026-01-01T00:00:05Z",+ entry: {+ id: "latest-command",+ createdAt: "2026-01-01T00:00:05Z",+ turnId: "turn-1" as never,+ label: toolLifecycleStatus === "inProgress" ? "Running rg" : "Ran rg",+ command: "rg toolCall",+ requestKind: "command",+ tone: "tool" as const,+ toolLifecycleStatus,+ },+ },+ ],+ latestTurn: {+ turnId: "turn-1" as never,+ state: "running",+ startedAt: "2026-01-01T00:00:00Z",+ completedAt: null,},- ],- latestTurn: {- turnId: "turn-1" as never,- state: "running",- startedAt: "2026-01-01T00:00:00Z",- completedAt: null,- },- isWorking: true,- activeTurnStartedAt: "2026-01-01T00:00:00Z",- turnDiffSummaryByAssistantMessageId: new Map(),- revertTurnCountByUserMessageId: new Map(),- });+ isWorking: true,+ activeTurnStartedAt: "2026-01-01T00:00:00Z",+ turnDiffSummaryByAssistantMessageId: new Map(),+ revertTurnCountByUserMessageId: new Map(),+ });- expect(rows.map((row) => row.kind)).toEqual(["working", "work-live", "thinking"]);- expect(rows.find((row) => row.kind === "work-live")).toMatchObject({ active: false });- expect(rows.at(-1)).toMatchObject({ kind: "thinking" });+ const initialRows = deriveRows(null);+ const runningRows = deriveRows("inProgress");+ const completedRows = deriveRows("completed");+ const declinedRows = deriveRows("declined");+ const initialActivityRow = initialRows.find((row) => row.id === "live-activity-row");+ const runningActivityRow = runningRows.find((row) => row.id === "live-activity-row");+ const completedActivityRow = completedRows.find((row) => row.id === "live-activity-row");++ expect(initialActivityRow).toMatchObject({ kind: "thinking" });+ expect(runningActivityRow).toMatchObject({ kind: "work-live", active: true });+ expect(completedActivityRow).toMatchObject({ kind: "work-live", active: true });+ expect(declinedRows.find((row) => row.kind === "work-live")).toMatchObject({ active: false });+ expect(declinedRows.at(-1)).toMatchObject({ kind: "thinking", id: "live-activity-row" });+ expect(initialRows.filter((row) => row.id === "live-activity-row")).toHaveLength(1);+ expect(runningRows.filter((row) => row.id === "live-activity-row")).toHaveLength(1);+ expect(completedRows.filter((row) => row.id === "live-activity-row")).toHaveLength(1);+ expect(declinedRows.filter((row) => row.id === "live-activity-row")).toHaveLength(1);});it("does not fold the session's running turn when latestTurn regresses", () => {@@ -1290,7 +1309,7 @@ describe("deriveMessagesTimelineRows", () => {expect(rows.filter((row) => row.kind === "turn-fold").map((row) => row.turnId)).toEqual(["turn-1",]);- expect(rows.map((row) => row.id)).toContain("work-live:running-work-entry");+ expect(rows.map((row) => row.id)).toContain("live-activity-row");});it("only shows assistant metadata on the terminal assistant message", () => {@@ -1587,8 +1606,8 @@ describe("computeStableMessagesTimelineRows", () => {initial,);- const initialThinking = initial.byId.get("thinking-indicator-row");- const updatedThinking = updated.byId.get("thinking-indicator-row");+ const initialThinking = initial.byId.get("live-activity-row");+ const updatedThinking = updated.byId.get("live-activity-row");expect(initialThinking).toMatchObject({ kind: "thinking" });expect(updatedThinking).toBe(initialThinking);expect(updated.result.at(-1)).toBe(updatedThinking);diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.tsindex c787446f738..9f879480185 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts@@ -15,6 +15,7 @@ export {import {formatDuration,workEntryDisplayIndicatesToolFailure,+ workEntryIndicatesToolSuccess,workEntryIndicatesToolNeutralStatus,workLogEntryIsToolLike,type TimelineEntry,@@ -188,6 +189,8 @@ export type TimelineLatestTurn = Pick<"turnId" | "state" | "startedAt" | "completedAt">;+const LIVE_ACTIVITY_ROW_ID = "live-activity-row";+export type MessagesTimelineRow =| {kind: "work";@@ -598,21 +601,26 @@ export function deriveMessagesTimelineRows(input: {const latestRunningToolEntry = visibleActiveToolEntries.findLast((entry) =>workEntryIsActiveTurnActivity(entry.entry),);- const displayedToolEntry = latestRunningToolEntry ?? latestVisibleToolEntry;+ const latestToolKeepsActivityLive =+ latestRunningToolEntry !== undefined ||+ (latestVisibleToolEntry !== undefined &&+ workEntryIndicatesToolSuccess(latestVisibleToolEntry.entry));const activeWorkPlacementEntryId = latestVisibleToolEntry?.id;const activeWorkRow =- activeWorkAnchor && displayedToolEntry+ activeWorkAnchor && latestVisibleToolEntry? (() => {const groupId = workGroupId(activeWorkAnchor.id, activeWorkAnchor.entry);return {kind: "work-live" as const,- id: `work-live:${workGroupIdentity(activeWorkAnchor.id, activeWorkAnchor.entry)}`,+ id: latestToolKeepsActivityLive+ ? LIVE_ACTIVITY_ROW_ID+ : `work-live:${workGroupIdentity(activeWorkAnchor.id, activeWorkAnchor.entry)}`,createdAt: activeWorkAnchor.createdAt,- entry: displayedToolEntry.entry,+ entry: (latestRunningToolEntry ?? latestVisibleToolEntry).entry,groupedEntries: visibleActiveToolEntries.map((entry) => entry.entry),groupId,expanded: input.expandedWorkGroupIds?.has(groupId) ?? false,- active: latestRunningToolEntry !== undefined,+ active: latestToolKeepsActivityLive,};})(): null;@@ -626,11 +634,11 @@ export function deriveMessagesTimelineRows(input: {createdAt: input.activeTurnStartedAt,});};- let hasLiveWorkRow = false;+ let hasActivityRow = false;const appendActiveWorkRows = () => {if (activeWorkRow === null) return;nextRows.push(activeWorkRow);- hasLiveWorkRow ||= activeWorkRow.active;+ hasActivityRow ||= activeWorkRow.active;if (!activeWorkRow.expanded) return;for (const [entryIndex, workEntry] of activeWorkRow.groupedEntries.entries()) {nextRows.push({@@ -730,7 +738,7 @@ export function deriveMessagesTimelineRows(input: {expanded,active: true,});- hasLiveWorkRow = true;+ hasActivityRow = true;if (expanded) {for (const [entryIndex, workEntry] of visibleGroupedEntries.entries()) {nextRows.push({@@ -832,10 +840,10 @@ export function deriveMessagesTimelineRows(input: {if (input.isWorking && activeTurnHeaderIndex === input.timelineEntries.length) {appendWorkingRow();}- if (input.isWorking && !hasLiveWorkRow) {+ if (input.isWorking && !hasActivityRow) {nextRows.push({kind: "thinking",- id: "thinking-indicator-row",+ id: LIVE_ACTIVITY_ROW_ID,createdAt: input.activeTurnStartedAt,});}diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsxindex 024dfe69d27..5c6dbbf5b2e 100644--- a/apps/web/src/components/chat/MessagesTimeline.test.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx@@ -1308,7 +1308,30 @@ describe("MessagesTimeline", () => {expect(markup).not.toContain("tool call failed");});- it("keeps declined command copy visible while thinking continues", () => {+ it("renders initial thinking as the shared live activity row", () => {+ const turnId = TurnId.make("turn-live");+ const markup = renderToStaticMarkup(+ <MessagesTimeline+ {...buildProps()}+ isWorking+ activeTurnStartedAt={MESSAGE_CREATED_AT}+ latestTurn={{+ turnId,+ state: "running",+ startedAt: MESSAGE_CREATED_AT,+ completedAt: null,+ }}+ runningTurnId={turnId}+ timelineEntries={[]}+ />,+ );++ expect(markup).toContain("Thinking");+ expect(markup).toContain("lucide-brain");+ expect(markup).toContain('data-timeline-row-id="live-activity-row"');+ });++ it("keeps the completed command in the shared activity row", () => {const turnId = TurnId.make("turn-live");const markup = renderToStaticMarkup(<MessagesTimeline@@ -1324,28 +1347,31 @@ describe("MessagesTimeline", () => {runningTurnId={turnId}timelineEntries={[{- id: "entry-declined",+ id: "entry-completed",kind: "work",createdAt: MESSAGE_CREATED_AT,entry: {- id: "work-declined",+ id: "work-completed",createdAt: MESSAGE_CREATED_AT,turnId,- toolCallId: "call-declined",+ toolCallId: "call-completed",label: "Run lint",tone: "tool",itemType: "command_execution",command: "pnpm lint",- toolLifecycleStatus: "declined",+ toolLifecycleStatus: "completed",},},]}/>,);- expect(markup).toContain("Declined pnpm");- expect(markup).toContain("Thinking");- expect(markup).toContain("tool call failed");+ expect(markup).toContain("Running pnpm");+ expect(markup).toContain("lucide-terminal");+ expect(markup).toContain("live-activity-focus");+ expect(markup).not.toContain("Ran pnpm");+ expect(markup).not.toContain("Thinking");+ expect(markup).not.toContain('data-timeline-row-kind="thinking"');});it("renders review comment contexts as structured cards instead of raw tags", () => {diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex dbe8507aef8..df944a98485 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -62,6 +62,7 @@ import {import ChatMarkdown, { ChatMarkdownAssetImage } from "../ChatMarkdown";import {BotIcon,+ BrainIcon,CheckIcon,ChevronDownIcon,ChevronRightIcon,@@ -985,14 +986,15 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time: "pb-0": isExpandedToolGroupHeader? "pb-0"- : row.kind === "turn-fold" || row.kind === "working" || row.kind === "thinking"+ : row.kind === "turn-fold" || row.kind === "working"? "pb-1.5": (row.kind === "message" &&row.message.role === "assistant" &&!row.showAssistantMeta) ||row.kind === "work" ||row.kind === "work-live" ||- row.kind === "work-toggle"+ row.kind === "work-toggle" ||+ row.kind === "thinking"? "pb-2": "pb-4",row.kind === "message" && row.message.role === "assistant" ? "group/assistant" : null,@@ -1364,7 +1366,7 @@ function ThinkingTimelineRow() {// Reserve the activity row during setup so the handoff keeps the same height.return (<div className="min-h-7">- {isPreparingWorktree ? null : <LiveActivityRow label="Thinking" />}+ {isPreparingWorktree ? null : <LiveActivityRow label="Thinking" iconName="brain" />}</div>);}@@ -2123,6 +2125,7 @@ function formatWorkingTimerNow(startIso: string): string {type WorkEntryIconName =| "bot"+ | "brain"| "check"| "circle-alert"| "eye"@@ -2140,6 +2143,8 @@ function WorkEntryIconSvg({ name, className }: { name: WorkEntryIconName; classNswitch (name) {case "bot":return <BotIcon className={className} aria-hidden />;+ case "brain":+ return <BrainIcon className={className} aria-hidden />;case "check":return <CheckIcon className={className} aria-hidden />;case "circle-alert":@@ -2179,7 +2184,7 @@ function workToneIcon(tone: TimelineWorkEntry["tone"]): {}if (tone === "thinking") {return {- iconName: "bot",+ iconName: "brain",className: "text-foreground",};}