fix(chat): reuse one row for live activity (#9062)

Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
mariaandmaria-rcks authored and GitHub committed 2026-09-01 16:48:27 -04:00
1 parent cb00746916
commit a924fbe08e
4 files changed
+106 -59

No files matched your search

@@ -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,54 @@ 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" | 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 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: false });
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);
});
it("does not fold the session's running turn when latestTurn regresses", () => {
@@ -1290,7 +1305,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 +1602,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);
@@ -188,6 +188,8 @@ export type TimelineLatestTurn = Pick<
"turnId" | "state" | "startedAt" | "completedAt"
>;
const LIVE_ACTIVITY_ROW_ID = "live-activity-row";
export type MessagesTimelineRow =
| {
kind: "work";
@@ -598,17 +600,16 @@ export function deriveMessagesTimelineRows(input: {
const latestRunningToolEntry = visibleActiveToolEntries.findLast((entry) =>
workEntryIsActiveTurnActivity(entry.entry),
);
const displayedToolEntry = latestRunningToolEntry ?? latestVisibleToolEntry;
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: LIVE_ACTIVITY_ROW_ID,
createdAt: activeWorkAnchor.createdAt,
entry: displayedToolEntry.entry,
entry: (latestRunningToolEntry ?? latestVisibleToolEntry).entry,
groupedEntries: visibleActiveToolEntries.map((entry) => entry.entry),
groupId,
expanded: input.expandedWorkGroupIds?.has(groupId) ?? false,
@@ -626,11 +627,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 = true;
if (!activeWorkRow.expanded) return;
for (const [entryIndex, workEntry] of activeWorkRow.groupedEntries.entries()) {
nextRows.push({
@@ -730,7 +731,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 +833,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,
});
}
@@ -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("Ran pnpm");
expect(markup).toContain("lucide-terminal");
expect(markup).not.toContain("live-activity-focus");
expect(markup).not.toContain("Running 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", () => {
@@ -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; classN
switch (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",
};
}