TypeScript
fix(web): thinking row after a failed tool expands the run's tool calls (#15056)
maria70e5a40c20dd
diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts b/apps/web/src/components/chat/MessagesTimeline.logic.test.tsindex f87cb75b34b..173a964d5ef 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.test.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.test.ts@@ -2684,8 +2684,10 @@ describe("deriveMessagesTimelineRows", () => {it("reuses one activity row for initial thinking and the latest tool", () => {const deriveRows = (toolLifecycleStatus: "inProgress" | "completed" | "failed" | "declined" | null,+ expandedWorkGroupIds?: ReadonlySet<string>,) =>deriveMessagesTimelineRows({+ ...(expandedWorkGroupIds ? { expandedWorkGroupIds } : {}),timelineEntries:toolLifecycleStatus === null? []@@ -2733,6 +2735,19 @@ describe("deriveMessagesTimelineRows", () => {expect(completedActivityRow).toMatchObject({ kind: "work-live", active: true });expect(failedRows.some((row) => row.kind === "work-live")).toBe(false);expect(failedRows.at(-1)).toMatchObject({ kind: "thinking", id: "live-activity-row" });+ const failedThinkingRow = failedRows.at(-1);+ const failedGroupId =+ failedThinkingRow?.kind === "thinking" ? failedThinkingRow.groupId : undefined;+ expect(failedGroupId).toBeDefined();+ const expandedFailedRows = deriveRows("failed", new Set([failedGroupId!]));+ expect(expandedFailedRows.slice(-2)).toMatchObject([+ { kind: "thinking", id: "live-activity-row", expanded: true },+ {+ kind: "work",+ isExpandedToolGroup: true,+ groupedEntries: [{ id: "latest-command" }],+ },+ ]);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);diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.tsindex 09784aaf9db..280cc367210 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts@@ -509,6 +509,9 @@ type MessagesTimelineRowContent =kind: "thinking";id: string;createdAt: string | null;+ /** Tool calls this row stands in for after the latest one failed. */+ groupId?: string;+ expanded?: boolean;}| {kind: "work-toggle";@@ -1737,11 +1740,35 @@ export function deriveMessagesTimelineRows(input: {!hasActiveCompaction &&(!hasActivityRow || latestToolFailed)) {- nextRows.push({- kind: "thinking",- id: LIVE_ACTIVITY_ROW_ID,- createdAt: input.activeTurnStartedAt ?? null,- });+ // A failed latest tool hands the row back to thinking, but its group+ // stays reachable through the same disclosure the live row offers.+ const failedGroupAnchor = latestToolFailed ? activeWorkAnchor : undefined;+ if (failedGroupAnchor) {+ const groupId = workGroupId(failedGroupAnchor.id);+ const expanded = input.expandedWorkGroupIds?.has(groupId) ?? false;+ nextRows.push({+ kind: "thinking",+ id: LIVE_ACTIVITY_ROW_ID,+ createdAt: input.activeTurnStartedAt ?? null,+ groupId,+ expanded,+ });+ if (expanded) {+ nextRows.push(+ expandedWorkGroupRow(+ groupId,+ failedGroupAnchor.createdAt,+ visibleActiveToolEntries.map((entry) => entry.entry),+ ),+ );+ }+ } else {+ nextRows.push({+ kind: "thinking",+ id: LIVE_ACTIVITY_ROW_ID,+ createdAt: input.activeTurnStartedAt ?? null,+ });+ }}const result = attachTrailingToolGroupsToAssistant(@@ -1952,8 +1979,11 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): booleanswitch (a.kind) {case "working":- case "thinking":return a.createdAt === (b as typeof a).createdAt;+ case "thinking": {+ const bt = b as typeof a;+ return a.createdAt === bt.createdAt && a.groupId === bt.groupId && a.expanded === bt.expanded;+ }case "worktree-setup":return a.snapshot === (b as typeof a).snapshot;diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex b5113b5f41a..8b2808b3a18 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -1741,7 +1741,8 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Timerow.kind === "work-toggle" ||row.kind === "thinking";const isExpandedToolGroupHeader =- (row.kind === "work-toggle" && row.expanded) || (row.kind === "work-live" && row.expanded);+ (row.kind === "work-toggle" || row.kind === "work-live" || row.kind === "thinking") &&+ row.expanded === true;return (<div@@ -1793,7 +1794,7 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time) : null}{row.kind === "work-live" ? <LiveWorkEntryTimelineRow row={row} /> : null}{row.kind === "work-toggle" ? <WorkGroupToggleTimelineRow row={row} /> : null}- {row.kind === "thinking" ? <ThinkingTimelineRow /> : null}+ {row.kind === "thinking" ? <ThinkingTimelineRow row={row} /> : null}</WorkLogBlock>) : null}{row.kind === "turn-fold" ? <TurnFoldTimelineRow row={row} /> : null}@@ -3461,13 +3462,23 @@ function CompactingLabel() {);}-function ThinkingTimelineRow() {+function ThinkingTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "thinking" }> }) {+ const ctx = use(TimelineRowCtx);const { isCompacting, isPreparingWorktree } = use(TimelineRowActivityCtx);// Reserve the activity row during setup so the handoff keeps the same height.- return isPreparingWorktree || isCompacting ? (- <WorkLogRow label="" />- ) : (- <LiveActivityRow label="Thinking" iconName="brain" active shimmer />+ if (isPreparingWorktree || isCompacting) return <WorkLogRow label="" />;+ const activity = <LiveActivityRow label="Thinking" iconName="brain" active shimmer />;+ const { groupId } = row;+ if (groupId === undefined) return activity;+ return (+ <button+ type="button"+ className="group/live-work flex min-h-6 w-full max-w-full cursor-pointer items-center rounded-md text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"+ aria-expanded={row.expanded === true}+ onClick={() => ctx.onToggleWorkGroup(groupId, row.id)}+ >+ {activity}+ </button>);}