TypeScript
fix(chat): keep agent activity visible between actions (#8984)
maria9ecfc07a8b3a
diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.tsindex 9e18f8f0044..d9bfdae04d4 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts@@ -826,10 +826,11 @@ describe("deriveMessagesTimelineRows", () => {"assistant-final-entry","user-followup-entry","working-indicator-row",+ "thinking-indicator-row",]);const finalRow = rows.find((row) => row.id === "assistant-final-entry");expect(finalRow?.kind === "message" && finalRow.showAssistantMeta).toBe(true);- expect(rows.find((row) => row.kind === "working")).toMatchObject({ showThinking: true });+ expect(rows.at(-1)).toMatchObject({ kind: "thinking" });});it("does not fold the active in-progress turn", () => {@@ -886,18 +887,18 @@ describe("deriveMessagesTimelineRows", () => {const rows = deriveMessagesTimelineRows({timelineEntries: [{- id: "completed-command-entry",+ id: "running-command-entry",kind: "work",createdAt: "2026-01-01T00:00:05Z",entry: {- id: "completed-command",+ id: "running-command",createdAt: "2026-01-01T00:00:05Z",turnId: "turn-1" as never,- label: "Ran rg",+ label: "Running rg",command: "rg toolCall",requestKind: "command",tone: "tool" as const,- toolLifecycleStatus: "completed" as const,+ toolLifecycleStatus: "inProgress" as const,},},{@@ -916,18 +917,18 @@ describe("deriveMessagesTimelineRows", () => {},},{- id: "running-command-entry",+ id: "completed-command-entry",kind: "work",createdAt: "2026-01-01T00:00:07Z",entry: {- id: "running-command",+ id: "completed-command",createdAt: "2026-01-01T00:00:07Z",turnId: "turn-1" as never,- label: "Running tests",+ label: "Ran tests",command: "vp test run",requestKind: "command",tone: "tool" as const,- toolLifecycleStatus: "inProgress" as const,+ toolLifecycleStatus: "completed" as const,},},],@@ -944,13 +945,13 @@ describe("deriveMessagesTimelineRows", () => {});expect(rows.map((row) => row.kind)).toEqual(["working", "work-live"]);- expect(rows.find((row) => row.kind === "working")).toMatchObject({ showThinking: false });+ expect(rows.some((row) => row.kind === "thinking")).toBe(false);expect(rows.find((row) => row.kind === "work-live")).toMatchObject({entry: { id: "running-command" },groupedEntries: [- { id: "completed-command" },- { id: "completed-edit" },{ id: "running-command" },+ { id: "completed-edit" },+ { id: "completed-command" },],});});@@ -1195,7 +1196,7 @@ describe("deriveMessagesTimelineRows", () => {]);});- it("keeps the latest completed tool call live while the turn is running", () => {+ it("shows thinking after the latest tool call completes while the turn is running", () => {const rows = deriveMessagesTimelineRows({timelineEntries: [{@@ -1226,11 +1227,9 @@ describe("deriveMessagesTimelineRows", () => {revertTurnCountByUserMessageId: new Map(),});- expect(rows.map((row) => row.kind)).toEqual(["working", "work-live"]);- expect(rows.find((row) => row.kind === "work-live")).toMatchObject({- entry: { id: "latest-command" },- groupedEntries: [{ id: "latest-command" }],- });+ 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" });});it("does not fold the session's running turn when latestTurn regresses", () => {@@ -1378,7 +1377,7 @@ describe("deriveMessagesTimelineRows", () => {expect(assistantRow?.showAssistantMeta).toBe(false);expect(assistantRow?.showAssistantCopyButton).toBe(false);- expect(rows.find((row) => row.kind === "working")).toMatchObject({ showThinking: false });+ expect(rows.at(-1)).toMatchObject({ kind: "thinking" });});it.each([@@ -1547,7 +1546,7 @@ describe("deriveMessagesTimelineRows", () => {});describe("computeStableMessagesTimelineRows", () => {- it.each(["", " \n"])("replaces Thinking when assistant content grows from %j", (text) => {+ it.each(["", " \n"])("keeps Thinking after assistant content grows from %j", (text) => {const startedAt = "2026-01-01T00:00:00Z";const turnId = TurnId.make("turn-1");const input = {@@ -1588,11 +1587,11 @@ describe("computeStableMessagesTimelineRows", () => {initial,);- const initialWorking = initial.byId.get("working-indicator-row");- const updatedWorking = updated.byId.get("working-indicator-row");- expect(initialWorking).toMatchObject({ showThinking: true });- expect(updatedWorking).toMatchObject({ showThinking: false });- expect(updatedWorking).not.toBe(initialWorking);+ const initialThinking = initial.byId.get("thinking-indicator-row");+ const updatedThinking = updated.byId.get("thinking-indicator-row");+ expect(initialThinking).toMatchObject({ kind: "thinking" });+ expect(updatedThinking).toBe(initialThinking);+ expect(updated.result.at(-1)).toBe(updatedThinking);});it("returns the previous result when row order and content are unchanged", () => {diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.tsindex c190643ee7f..66a3b3b6744 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts@@ -193,6 +193,7 @@ export type MessagesTimelineRow =groupedEntries: WorkLogEntry[];groupId: string;expanded: boolean;+ active: boolean;}| {kind: "work-toggle";@@ -235,7 +236,11 @@ export type MessagesTimelineRow =kind: "working";id: string;createdAt: string | null;- showThinking: boolean;+ }+ | {+ kind: "thinking";+ id: string;+ createdAt: string | null;};export interface StableMessagesTimelineRowsState {@@ -512,6 +517,14 @@ function timelineEntryTurnId(entry: TimelineEntry): TurnId | null {return entry.kind === "work" ? (entry.entry.turnId ?? null) : null;}+function workEntryIsActiveTurnActivity(entry: WorkLogEntry): boolean {+ return (+ entry.toolLifecycleStatus === "inProgress" ||+ entry.sourceActivityKind === "task.progress" ||+ (entry.toolLifecycleStatus === undefined && workLogEntryIsToolLike(entry))+ );+}+/*** Settled turns keep only their terminal assistant message visible.* Everything before it folds behind a "Worked for ..." row anchored at the@@ -702,24 +715,6 @@ export function deriveMessagesTimelineRows(input: {unsettledTurnId !== null &&entry.toolLifecycleStatus === "inProgress" &&entry.turnId === unsettledTurnId;- const activeEntries = input.isWorking- ? input.timelineEntries.filter((entry, index) => entryBelongsToActiveTurn(entry, index))- : [];- const activeTurnHasVisibleContent = activeEntries.some((entry) => {- if (entry.kind === "message") {- return entry.message.role === "assistant" && (entry.message.text?.trim().length ?? 0) > 0;- }- if (entry.kind === "work") {- return (- entry.entry.agentSpawn === undefined &&- workLogEntryIsToolLike(entry.entry) &&- entry.entry.toolLifecycleStatus === "inProgress"- );- }- if (entry.kind === "proposed-plan") return true;- return false;- });-const activeToolEntries: Array<Extract<TimelineEntry, { kind: "work" }>> = [];for (let index = input.timelineEntries.length - 1; index >= activeTurnHeaderIndex; index -= 1) {const entry = input.timelineEntries[index]!;@@ -733,40 +728,48 @@ export function deriveMessagesTimelineRows(input: {}activeToolEntries.unshift(entry);}- const activeWorkEntryIds = new Set(activeToolEntries.map((entry) => entry.id));const visibleActiveToolEntries = omitSupersededLifecycleMarkers(activeToolEntries.filter((entry) => workEntryIsVisibleInGroup(entry.entry, true)),(entry) => entry.entry,);const activeWorkAnchor = activeToolEntries[0];- const latestActiveToolEntry = visibleActiveToolEntries.at(-1);- const activeWorkPlacementEntryId = latestActiveToolEntry?.id;+ const latestVisibleToolEntry = visibleActiveToolEntries.at(-1);+ const latestRunningToolEntry = visibleActiveToolEntries.findLast((entry) =>+ workEntryIsActiveTurnActivity(entry.entry),+ );+ const displayedToolEntry = latestRunningToolEntry ?? latestVisibleToolEntry;+ const activeWorkPlacementEntryId = latestVisibleToolEntry?.id;const activeWorkRow =- activeWorkAnchor && latestActiveToolEntry+ activeWorkAnchor && displayedToolEntry? (() => {const groupId = workGroupId(activeWorkAnchor.id, activeWorkAnchor.entry);return {kind: "work-live" as const,id: `work-live:${workGroupIdentity(activeWorkAnchor.id, activeWorkAnchor.entry)}`,createdAt: activeWorkAnchor.createdAt,- entry: latestActiveToolEntry.entry,+ entry: displayedToolEntry.entry,groupedEntries: visibleActiveToolEntries.map((entry) => entry.entry),groupId,expanded: input.expandedWorkGroupIds?.has(groupId) ?? false,+ active: latestRunningToolEntry !== undefined,};})(): null;+ const activeWorkEntryIds = new Set(+ activeWorkRow === null ? [] : activeToolEntries.map((entry) => entry.id),+ );const appendWorkingRow = () => {nextRows.push({kind: "working",id: "working-indicator-row",createdAt: input.activeTurnStartedAt,- showThinking: activeWorkRow === null && !activeTurnHasVisibleContent,});};+ let hasLiveWorkRow = false;const appendActiveWorkRows = () => {if (activeWorkRow === null) return;nextRows.push(activeWorkRow);+ hasLiveWorkRow ||= activeWorkRow.active;if (!activeWorkRow.expanded) return;for (const [entryIndex, workEntry] of activeWorkRow.groupedEntries.entries()) {nextRows.push({@@ -864,7 +867,9 @@ export function deriveMessagesTimelineRows(input: {groupedEntries: visibleGroupedEntries,groupId,expanded,+ active: true,});+ hasLiveWorkRow = true;if (expanded) {for (const [entryIndex, workEntry] of visibleGroupedEntries.entries()) {nextRows.push({@@ -966,6 +971,13 @@ export function deriveMessagesTimelineRows(input: {if (input.isWorking && activeTurnHeaderIndex === input.timelineEntries.length) {appendWorkingRow();}+ if (input.isWorking && !hasLiveWorkRow) {+ nextRows.push({+ kind: "thinking",+ id: "thinking-indicator-row",+ createdAt: input.activeTurnStartedAt,+ });+ }return nextRows;}@@ -996,9 +1008,8 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): booleanswitch (a.kind) {case "working":- return (- a.createdAt === (b as typeof a).createdAt && a.showThinking === (b as typeof a).showThinking- );+ case "thinking":+ return a.createdAt === (b as typeof a).createdAt;case "turn-fold": {const bf = b as typeof a;@@ -1023,6 +1034,7 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): booleana.createdAt === bw.createdAt &&a.groupId === bw.groupId &&a.expanded === bw.expanded &&+ a.active === bw.active &&Equal.equals(a.entry, bw.entry) &&Equal.equals(a.groupedEntries, bw.groupedEntries));diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsxindex 9a8ad8f6c03..024dfe69d27 100644--- a/apps/web/src/components/chat/MessagesTimeline.test.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx@@ -1308,7 +1308,7 @@ describe("MessagesTimeline", () => {expect(markup).not.toContain("tool call failed");});- it("keeps terminal command copy live while the parent turn is active", () => {+ it("keeps declined command copy visible while thinking continues", () => {const turnId = TurnId.make("turn-live");const markup = renderToStaticMarkup(<MessagesTimeline@@ -1324,26 +1324,27 @@ describe("MessagesTimeline", () => {runningTurnId={turnId}timelineEntries={[{- id: "entry-failed",+ id: "entry-declined",kind: "work",createdAt: MESSAGE_CREATED_AT,entry: {- id: "work-failed",+ id: "work-declined",createdAt: MESSAGE_CREATED_AT,turnId,- toolCallId: "call-failed",+ toolCallId: "call-declined",label: "Run lint",tone: "tool",itemType: "command_execution",command: "pnpm lint",- toolLifecycleStatus: "failed",+ toolLifecycleStatus: "declined",},},]}/>,);- expect(markup).toContain("Running pnpm");+ expect(markup).toContain("Declined pnpm");+ expect(markup).toContain("Thinking");expect(markup).toContain("tool call failed");});diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 81ab67f6ffe..d49c355e5b3 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -981,7 +981,7 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time: "pb-0": isExpandedToolGroupHeader? "pb-0"- : row.kind === "turn-fold" || row.kind === "working"+ : row.kind === "turn-fold" || row.kind === "working" || row.kind === "thinking"? "pb-1.5": (row.kind === "message" &&row.message.role === "assistant" &&@@ -1013,6 +1013,7 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time) : null}{row.kind === "proposed-plan" ? <ProposedPlanTimelineRow row={row} /> : null}{row.kind === "working" ? <WorkingTimelineRow row={row} /> : null}+ {row.kind === "thinking" ? <ThinkingTimelineRow /> : null}</div>);});@@ -1330,34 +1331,36 @@ function ProposedPlanTimelineRow({function WorkingTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "working" }> }) {const { isPreparingWorktree } = use(TimelineRowActivityCtx);return (- <div>- <div className="border-b border-border/60 pb-2 pt-1">- <div className="flex h-6 min-w-0 items-baseline px-1 text-sm leading-relaxed text-muted-foreground tabular-nums">- <span- key={isPreparingWorktree ? "setup" : "working"}- className="relative shrink-0 overflow-hidden whitespace-nowrap transition-opacity duration-150 starting:opacity-0 motion-reduce:transition-none"- >- {isPreparingWorktree ? (- <>- Setting up worktree…- <ActivityShimmerOverlay>Setting up worktree…</ActivityShimmerOverlay>- </>- ) : row.createdAt ? (- <>- Working for <WorkingTimer createdAt={row.createdAt} />- </>- ) : (- "Working..."- )}- </span>- </div>+ <div className="border-b border-border/60 pb-2 pt-1">+ <div className="flex h-6 min-w-0 items-baseline px-1 text-sm leading-relaxed text-muted-foreground tabular-nums">+ <span+ key={isPreparingWorktree ? "setup" : "working"}+ className="relative shrink-0 overflow-hidden whitespace-nowrap transition-opacity duration-150 starting:opacity-0 motion-reduce:transition-none"+ >+ {isPreparingWorktree ? (+ <>+ Setting up worktree…+ <ActivityShimmerOverlay>Setting up worktree…</ActivityShimmerOverlay>+ </>+ ) : row.createdAt ? (+ <>+ Working for <WorkingTimer createdAt={row.createdAt} />+ </>+ ) : (+ "Working..."+ )}+ </span></div>- {row.showThinking ? (- // Reserve the activity row during setup so the handoff keeps the same height.- <div className="mt-1 min-h-7">- {isPreparingWorktree ? null : <LiveActivityRow label="Thinking" />}- </div>- ) : null}+ </div>+ );+}++function ThinkingTimelineRow() {+ const { isPreparingWorktree } = use(TimelineRowActivityCtx);+ // Reserve the activity row during setup so the handoff keeps the same height.+ return (+ <div className="min-h-7">+ {isPreparingWorktree ? null : <LiveActivityRow label="Thinking" />}</div>);}@@ -1514,7 +1517,7 @@ function LiveActivityContent({function LiveWorkEntryTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "work-live" }> }) {const ctx = use(TimelineRowCtx);- const label = liveWorkEntryLabel(row.entry, ctx.workspaceRoot);+ const label = liveWorkEntryLabel(row.entry, ctx.workspaceRoot, row.active);const failed = workEntryDisplayIndicatesToolFailure(row.entry);return (@@ -1525,7 +1528,18 @@ function LiveWorkEntryTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "aria-expanded={row.expanded}onClick={() => ctx.onToggleWorkGroup(row.groupId, row.id)}>- <LiveActivityRow label={label} iconName={workEntryIconName(row.entry)} failed={failed} />+ {row.active ? (+ <LiveActivityRow label={label} iconName={workEntryIconName(row.entry)} failed={failed} />+ ) : (+ <div className="min-h-6 w-fit max-w-full min-w-0 overflow-hidden rounded-md text-sm leading-relaxed">+ <LiveActivityContent+ label={label}+ iconName={workEntryIconName(row.entry)}+ failed={failed}+ announceFailure={failed}+ />+ </div>+ )}</button>);}@@ -2205,14 +2219,18 @@ function workEntryRawCommand(function liveWorkEntryLabel(workEntry: TimelineWorkEntry,workspaceRoot: string | undefined,+ active: boolean,): string {const command = workEntry.command?.trim();if (command) {- // This row describes the active parent turn, not the command lifecycle.- // Keep its live "Running" copy until the turn or contiguous tool run settles.const program = commandProgramName(command);- if (program) return `Running ${program}`;- return "Running command";+ const verb = active+ ? "Running"+ : workEntry.toolLifecycleStatus === "declined"+ ? "Declined"+ : "Ran";+ if (program) return `${verb} ${program}`;+ return `${verb} command`;}return workEntryPreview(workEntry, workspaceRoot) ?? toolWorkEntryHeading(workEntry);