Skip to content

Commit 1c80f6e

Browse files
committed
fix(chat): group thoughts into the changing tool activity line
1 parent 384de08 commit 1c80f6e

7 files changed

Lines changed: 913 additions & 111 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,33 +1476,31 @@ function renderFeedEntry(
14761476
if (entry.type === "message") {
14771477
const { message } = entry;
14781478
if (message.role === "reasoning") {
1479-
// Only the live turn may claim to still be thinking, and only while the
1480-
// thread is actually working: a block left open by a crashed provider
1481-
// must not shimmer on a turn that settled long ago. Same test as web.
1482-
const liveReasoning =
1483-
Boolean(message.streaming) &&
1484-
props.isWorking &&
1485-
message.turnId !== null &&
1486-
message.turnId === props.unsettledTurnId;
1479+
const messages = entry.reasoningMessages ?? [message];
14871480
return (
14881481
<ThreadReasoningRow
14891482
rowSizing={props.workRowSizing}
14901483
iconSubtleColor={iconSubtleColor}
1491-
expanded={props.expandedReasoningMessageIds.has(message.id)}
1492-
label={liveReasoning ? "Thinking" : "Thought"}
1493-
streaming={liveReasoning}
1494-
onToggle={() => props.onToggleReasoning(message.id)}
1484+
expanded={props.expandedReasoningMessageIds.has(entry.id)}
1485+
label={`Thought${messages.length > 1 ? ` (×${messages.length})` : ""}`}
1486+
streaming={false}
1487+
onToggle={() => props.onToggleReasoning(entry.id)}
14951488
>
14961489
<MarkdownImageAvailableWidthContext
14971490
value={props.markdownContentWidth - REASONING_CONTENT_INSET}
14981491
>
1499-
<AssistantMarkdownContent
1500-
markdown={message.text}
1501-
markdownStyles={markdownStyles.assistant}
1502-
linkHandlers={props.markdownLinkHandlers}
1503-
renderImage={props.renderMarkdownImage}
1504-
skills={props.skills}
1505-
/>
1492+
<View className="gap-3">
1493+
{messages.map((reasoningMessage) => (
1494+
<AssistantMarkdownContent
1495+
key={reasoningMessage.id}
1496+
markdown={reasoningMessage.text}
1497+
markdownStyles={markdownStyles.assistant}
1498+
linkHandlers={props.markdownLinkHandlers}
1499+
renderImage={props.renderMarkdownImage}
1500+
skills={props.skills}
1501+
/>
1502+
))}
1503+
</View>
15061504
</MarkdownImageAvailableWidthContext>
15071505
</ThreadReasoningRow>
15081506
);
@@ -2656,8 +2654,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
26562654

26572655
const onToggleReasoning = useCallback(
26582656
(messageId: string) => {
2659-
// The anchor must be the feed row id, which for a message row is the
2660-
// message id, or position restoration is skipped for every row.
2657+
// Reasoning details use their own row within the expanded activity history.
26612658
suspendEndScrollMaintenanceForDisclosure(messageId);
26622659
setInteractionState((current) => {
26632660
const next = new Set(current.expandedReasoningMessageIds);
@@ -2700,8 +2697,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
27002697
switch (entry.type) {
27012698
case "message":
27022699
// A collapsed reasoning row is the same chrome as a work toggle.
2703-
return entry.message.role === "reasoning" &&
2704-
!expandedReasoningMessageIds.has(entry.message.id)
2700+
return entry.message.role === "reasoning" && !expandedReasoningMessageIds.has(entry.id)
27052701
? WORK_GROUP_TOGGLE_HEIGHT
27062702
: undefined;
27072703
case "turn-fold":

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ export function ThreadWorkGroupToggle(props: {
920920
readonly iconSubtleColor: import("react-native").ColorValue;
921921
readonly summary: string;
922922
readonly summaryKind: ToolGroupSummaryKind;
923-
readonly summaryToolIcon?: "browser" | "device" | "t3-code" | "pull-request";
923+
readonly summaryToolIcon?: "browser" | "device" | "t3-code" | "pull-request" | "brain";
924924
readonly themeAppearance: "light" | "dark";
925925
readonly toolSurface?: import("@t3tools/contracts").ToolActivitySurface;
926926
readonly toolIcon?: ToolActivityIcon;

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,251 @@ describe("buildThreadFeed", () => {
22952295
},
22962296
);
22972297

2298+
it("groups ordered reasoning blocks, keeps the live slot, and restores the group after unfolding", () => {
2299+
const turnId = TurnId.make("reasoning-group");
2300+
const messages: OrchestrationThread["messages"] = [1, 2, 3, 4].map((second) => ({
2301+
id: MessageId.make(`reasoning-${second}`),
2302+
role: "reasoning",
2303+
text: `**Step ${second}**\n\nCheck ${second}.`,
2304+
turnId,
2305+
streaming: second === 4,
2306+
createdAt: `2026-04-01T00:00:0${second}.000Z`,
2307+
updatedAt: `2026-04-01T00:00:0${second}.000Z`,
2308+
}));
2309+
const thread = makeThread({
2310+
id: ThreadId.make("reasoning-group"),
2311+
projectId: ProjectId.make("project-1"),
2312+
title: "Reasoning",
2313+
messages,
2314+
latestTurn: {
2315+
turnId,
2316+
state: "running",
2317+
requestedAt: "2026-04-01T00:00:00.000Z",
2318+
startedAt: "2026-04-01T00:00:00.000Z",
2319+
completedAt: null,
2320+
assistantMessageId: null,
2321+
},
2322+
});
2323+
const feed = buildThreadFeed(thread);
2324+
const rows = deriveThreadFeedPresentation(feed, thread.latestTurn, new Set(), new Set(), "now");
2325+
expect(rows).toMatchObject([
2326+
{ type: "work-toggle", id: "live-activity-row", summary: "Thinking", hiddenCount: 4 },
2327+
]);
2328+
expect(rows).toHaveLength(1);
2329+
expect(
2330+
deriveThreadFeedPresentation(feed, thread.latestTurn, new Set(), new Set(), "now")[0],
2331+
).toBe(rows[0]);
2332+
2333+
const settledTurn = {
2334+
...thread.latestTurn!,
2335+
state: "completed" as const,
2336+
completedAt: "2026-04-01T00:00:06.000Z",
2337+
};
2338+
// Reasoning alone stays visible, including a streaming flag left behind on settlement.
2339+
expect(deriveThreadFeedPresentation(feed, settledTurn, new Set())).toMatchObject([
2340+
{ type: "work-toggle", summary: "Thought (×4)", hiddenCount: 4 },
2341+
]);
2342+
const completedMessages = messages.map((message) => ({ ...message, streaming: false }));
2343+
const waiting = deriveThreadFeedPresentation(
2344+
buildThreadFeed({ ...thread, messages: completedMessages }),
2345+
thread.latestTurn,
2346+
new Set(),
2347+
new Set(),
2348+
"now",
2349+
);
2350+
expect(waiting).toMatchObject([
2351+
{ type: "work-toggle", id: "live-activity-row", summary: "Thinking", hiddenCount: 4 },
2352+
]);
2353+
const feedWithWork = buildThreadFeed({
2354+
...thread,
2355+
activities: [
2356+
makeActivity({
2357+
id: EventId.make("reasoning-tool"),
2358+
kind: "tool.completed",
2359+
tone: "tool",
2360+
summary: "Read files",
2361+
createdAt: "2026-04-01T00:00:05.000Z",
2362+
turnId,
2363+
payload: { itemType: "file_read", status: "completed" },
2364+
}),
2365+
],
2366+
});
2367+
const toolRunning = deriveThreadFeedPresentation(
2368+
feedWithWork,
2369+
thread.latestTurn,
2370+
new Set(),
2371+
new Set(),
2372+
"now",
2373+
);
2374+
expect(toolRunning).toMatchObject([
2375+
{ type: "work-toggle", id: "live-activity-row", shimmer: true, hiddenCount: 5 },
2376+
]);
2377+
expect(toolRunning[0]).not.toMatchObject({ summary: "Thinking" });
2378+
const nextThought = {
2379+
...messages[3]!,
2380+
id: MessageId.make("reasoning-after-tool"),
2381+
createdAt: "2026-04-01T00:00:06.000Z",
2382+
updatedAt: "2026-04-01T00:00:06.000Z",
2383+
};
2384+
const reasoningAgainFeed: ThreadFeedEntry[] = [
2385+
...feedWithWork,
2386+
{
2387+
type: "message",
2388+
id: nextThought.id,
2389+
createdAt: nextThought.createdAt,
2390+
message: nextThought,
2391+
},
2392+
];
2393+
const reasoningAgain = deriveThreadFeedPresentation(
2394+
reasoningAgainFeed,
2395+
thread.latestTurn,
2396+
new Set(),
2397+
new Set(),
2398+
"now",
2399+
);
2400+
expect(reasoningAgain.filter((row) => row.id === "live-activity-row")).toMatchObject([
2401+
{ type: "work-toggle", summary: "Thinking", hiddenCount: 6 },
2402+
]);
2403+
expect(reasoningAgain).toHaveLength(1);
2404+
const expandedLive = deriveThreadFeedPresentation(
2405+
reasoningAgainFeed,
2406+
thread.latestTurn,
2407+
new Set(),
2408+
new Set([`activity-run:${messages[0]!.id}`]),
2409+
"now",
2410+
);
2411+
expect(expandedLive.map((entry) => entry.type)).toEqual([
2412+
"work-toggle",
2413+
"message",
2414+
"activity-group",
2415+
"message",
2416+
]);
2417+
expect(expandedLive[1]).toMatchObject({ reasoningMessages: messages });
2418+
expect(expandedLive[3]).toMatchObject({ message: nextThought });
2419+
expect(deriveThreadFeedPresentation(feedWithWork, settledTurn, new Set())).toMatchObject([
2420+
{ type: "turn-fold", expanded: false },
2421+
]);
2422+
const reopened = deriveThreadFeedPresentation(
2423+
feedWithWork,
2424+
settledTurn,
2425+
new Set([turnId]),
2426+
new Set([`activity-run:${messages[0]!.id}`]),
2427+
);
2428+
expect(reopened.map((entry) => entry.type)).toEqual([
2429+
"turn-fold",
2430+
"work-toggle",
2431+
"message",
2432+
"activity-group",
2433+
]);
2434+
expect(reopened[2]).toMatchObject({ id: messages[0]!.id, reasoningMessages: messages });
2435+
const toolFirstFeed = feedWithWork.filter((entry) => entry.type === "activity-group");
2436+
const toolFirst = deriveThreadFeedPresentation(
2437+
toolFirstFeed,
2438+
thread.latestTurn,
2439+
new Set(),
2440+
new Set(),
2441+
"now",
2442+
)[0];
2443+
expect(toolFirst?.type).toBe("work-toggle");
2444+
if (toolFirst?.type !== "work-toggle") return;
2445+
const preservedExpansion = deriveThreadFeedPresentation(
2446+
[...toolFirstFeed, reasoningAgainFeed.at(-1)!],
2447+
thread.latestTurn,
2448+
new Set(),
2449+
new Set([toolFirst.groupId]),
2450+
"now",
2451+
);
2452+
expect(preservedExpansion.map((entry) => entry.type)).toEqual([
2453+
"work-toggle",
2454+
"activity-group",
2455+
"message",
2456+
]);
2457+
expect(preservedExpansion[0]).toMatchObject({
2458+
id: "live-activity-row",
2459+
groupId: toolFirst.groupId,
2460+
expanded: true,
2461+
summary: "Thinking",
2462+
});
2463+
});
2464+
2465+
it.each(["tool", "failed-tool", "assistant", "turn", "unknown-turn"] as const)(
2466+
"preserves a %s boundary in expanded activity history",
2467+
(boundary) => {
2468+
const turnId = TurnId.make("reasoning-boundary");
2469+
const messages: OrchestrationThread["messages"] = [1, 3].map((second) => ({
2470+
id: MessageId.make(`reasoning-${second}`),
2471+
role: "reasoning",
2472+
text: `Step ${second}`,
2473+
turnId:
2474+
boundary === "unknown-turn"
2475+
? null
2476+
: boundary === "turn" && second === 3
2477+
? TurnId.make("other-turn")
2478+
: turnId,
2479+
streaming: false,
2480+
createdAt: `2026-04-01T00:00:0${second}.000Z`,
2481+
updatedAt: `2026-04-01T00:00:0${second}.000Z`,
2482+
}));
2483+
const thread = makeThread({
2484+
id: ThreadId.make("reasoning-boundary"),
2485+
projectId: ProjectId.make("project-1"),
2486+
title: "Reasoning",
2487+
messages:
2488+
boundary === "assistant"
2489+
? [
2490+
messages[0]!,
2491+
{
2492+
...messages[0]!,
2493+
id: MessageId.make("assistant-between"),
2494+
role: "assistant",
2495+
text: "Checking the next file.",
2496+
createdAt: "2026-04-01T00:00:02.000Z",
2497+
},
2498+
messages[1]!,
2499+
]
2500+
: messages,
2501+
activities:
2502+
boundary === "tool" || boundary === "failed-tool"
2503+
? [
2504+
makeActivity({
2505+
id: EventId.make("tool-between"),
2506+
kind: "tool.completed",
2507+
tone: "tool",
2508+
summary: "Read files",
2509+
createdAt: "2026-04-01T00:00:02.000Z",
2510+
turnId,
2511+
payload: {
2512+
itemType: "file_read",
2513+
status: boundary === "failed-tool" ? "failed" : "completed",
2514+
},
2515+
}),
2516+
]
2517+
: [],
2518+
});
2519+
const rows = deriveThreadFeedPresentation(
2520+
buildThreadFeed(thread),
2521+
null,
2522+
new Set([turnId]),
2523+
new Set(messages.map((message) => `activity-run:${message.id}`)),
2524+
);
2525+
const reasoningRows = rows.filter(
2526+
(entry) => entry.type === "message" && entry.message.role === "reasoning",
2527+
);
2528+
if (boundary === "failed-tool") {
2529+
expect(rows.filter((entry) => entry.type === "work-toggle")).toHaveLength(3);
2530+
expect(rows.some((entry) => entry.type === "work-toggle" && entry.hasFailure)).toBe(true);
2531+
}
2532+
expect(reasoningRows).toEqual(
2533+
messages.map((message) => ({
2534+
type: "message",
2535+
id: message.id,
2536+
createdAt: message.createdAt,
2537+
message,
2538+
})),
2539+
);
2540+
},
2541+
);
2542+
22982543
it("shows one Thinking row while a turn works without live tool activity", () => {
22992544
const turnId = TurnId.make("turn-thinking");
23002545
const latestTurn = {

0 commit comments

Comments
 (0)