Skip to content

Commit e065c5b

Browse files
committed
fix(web): thinking row after a failed tool expands the run's tool calls
1 parent 798e945 commit e065c5b

3 files changed

Lines changed: 69 additions & 13 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.logic.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,8 +2614,10 @@ describe("deriveMessagesTimelineRows", () => {
26142614
it("reuses one activity row for initial thinking and the latest tool", () => {
26152615
const deriveRows = (
26162616
toolLifecycleStatus: "inProgress" | "completed" | "failed" | "declined" | null,
2617+
expandedWorkGroupIds?: ReadonlySet<string>,
26172618
) =>
26182619
deriveMessagesTimelineRows({
2620+
...(expandedWorkGroupIds ? { expandedWorkGroupIds } : {}),
26192621
timelineEntries:
26202622
toolLifecycleStatus === null
26212623
? []
@@ -2663,6 +2665,19 @@ describe("deriveMessagesTimelineRows", () => {
26632665
expect(completedActivityRow).toMatchObject({ kind: "work-live", active: true });
26642666
expect(failedRows.some((row) => row.kind === "work-live")).toBe(false);
26652667
expect(failedRows.at(-1)).toMatchObject({ kind: "thinking", id: "live-activity-row" });
2668+
const failedThinkingRow = failedRows.at(-1);
2669+
const failedGroupId =
2670+
failedThinkingRow?.kind === "thinking" ? failedThinkingRow.groupId : undefined;
2671+
expect(failedGroupId).toBeDefined();
2672+
const expandedFailedRows = deriveRows("failed", new Set([failedGroupId!]));
2673+
expect(expandedFailedRows.slice(-2)).toMatchObject([
2674+
{ kind: "thinking", id: "live-activity-row", expanded: true },
2675+
{
2676+
kind: "work",
2677+
isExpandedToolGroup: true,
2678+
groupedEntries: [{ id: "latest-command" }],
2679+
},
2680+
]);
26662681
expect(declinedRows.find((row) => row.kind === "work-live")).toMatchObject({ active: false });
26672682
expect(declinedRows.at(-1)).toMatchObject({ kind: "thinking", id: "live-activity-row" });
26682683
expect(initialRows.filter((row) => row.id === "live-activity-row")).toHaveLength(1);

‎apps/web/src/components/chat/MessagesTimeline.logic.ts‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ type MessagesTimelineRowContent =
509509
kind: "thinking";
510510
id: string;
511511
createdAt: string | null;
512+
/** Tool calls this row stands in for after the latest one failed. */
513+
groupId?: string;
514+
expanded?: boolean;
512515
}
513516
| {
514517
kind: "work-toggle";
@@ -1728,11 +1731,35 @@ export function deriveMessagesTimelineRows(input: {
17281731
!hasActiveCompaction &&
17291732
(!hasActivityRow || latestToolFailed)
17301733
) {
1731-
nextRows.push({
1732-
kind: "thinking",
1733-
id: LIVE_ACTIVITY_ROW_ID,
1734-
createdAt: input.activeTurnStartedAt ?? null,
1735-
});
1734+
// A failed latest tool hands the row back to thinking, but its group
1735+
// stays reachable through the same disclosure the live row offers.
1736+
const failedGroupAnchor = latestToolFailed ? activeWorkAnchor : undefined;
1737+
if (failedGroupAnchor) {
1738+
const groupId = workGroupId(failedGroupAnchor.id);
1739+
const expanded = input.expandedWorkGroupIds?.has(groupId) ?? false;
1740+
nextRows.push({
1741+
kind: "thinking",
1742+
id: LIVE_ACTIVITY_ROW_ID,
1743+
createdAt: input.activeTurnStartedAt ?? null,
1744+
groupId,
1745+
expanded,
1746+
});
1747+
if (expanded) {
1748+
nextRows.push(
1749+
expandedWorkGroupRow(
1750+
groupId,
1751+
failedGroupAnchor.createdAt,
1752+
visibleActiveToolEntries.map((entry) => entry.entry),
1753+
),
1754+
);
1755+
}
1756+
} else {
1757+
nextRows.push({
1758+
kind: "thinking",
1759+
id: LIVE_ACTIVITY_ROW_ID,
1760+
createdAt: input.activeTurnStartedAt ?? null,
1761+
});
1762+
}
17361763
}
17371764

17381765
const result = attachTrailingToolGroupsToAssistant(
@@ -1943,8 +1970,11 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
19431970

19441971
switch (a.kind) {
19451972
case "working":
1946-
case "thinking":
19471973
return a.createdAt === (b as typeof a).createdAt;
1974+
case "thinking": {
1975+
const bt = b as typeof a;
1976+
return a.createdAt === bt.createdAt && a.groupId === bt.groupId && a.expanded === bt.expanded;
1977+
}
19481978
case "worktree-setup":
19491979
return a.snapshot === (b as typeof a).snapshot;
19501980

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,8 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time
17411741
row.kind === "work-toggle" ||
17421742
row.kind === "thinking";
17431743
const isExpandedToolGroupHeader =
1744-
(row.kind === "work-toggle" && row.expanded) || (row.kind === "work-live" && row.expanded);
1744+
((row.kind === "work-toggle" || row.kind === "work-live" || row.kind === "thinking") &&
1745+
row.expanded === true);
17451746

17461747
return (
17471748
<div
@@ -1793,7 +1794,7 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time
17931794
) : null}
17941795
{row.kind === "work-live" ? <LiveWorkEntryTimelineRow row={row} /> : null}
17951796
{row.kind === "work-toggle" ? <WorkGroupToggleTimelineRow row={row} /> : null}
1796-
{row.kind === "thinking" ? <ThinkingTimelineRow /> : null}
1797+
{row.kind === "thinking" ? <ThinkingTimelineRow row={row} /> : null}
17971798
</WorkLogBlock>
17981799
) : null}
17991800
{row.kind === "turn-fold" ? <TurnFoldTimelineRow row={row} /> : null}
@@ -3461,13 +3462,23 @@ function CompactingLabel() {
34613462
);
34623463
}
34633464

3464-
function ThinkingTimelineRow() {
3465+
function ThinkingTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "thinking" }> }) {
3466+
const ctx = use(TimelineRowCtx);
34653467
const { isCompacting, isPreparingWorktree } = use(TimelineRowActivityCtx);
34663468
// Reserve the activity row during setup so the handoff keeps the same height.
3467-
return isPreparingWorktree || isCompacting ? (
3468-
<WorkLogRow label="" />
3469-
) : (
3470-
<LiveActivityRow label="Thinking" iconName="brain" active shimmer />
3469+
if (isPreparingWorktree || isCompacting) return <WorkLogRow label="" />;
3470+
const activity = <LiveActivityRow label="Thinking" iconName="brain" active shimmer />;
3471+
const { groupId } = row;
3472+
if (groupId === undefined) return activity;
3473+
return (
3474+
<button
3475+
type="button"
3476+
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"
3477+
aria-expanded={row.expanded === true}
3478+
onClick={() => ctx.onToggleWorkGroup(groupId, row.id)}
3479+
>
3480+
{activity}
3481+
</button>
34713482
);
34723483
}
34733484

0 commit comments

Comments
 (0)