Skip to content

Commit fa47d53

Browse files
committed
fix(chat): preserve activity boundaries and current status
1 parent 1c80f6e commit fa47d53

4 files changed

Lines changed: 96 additions & 3 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,6 +2460,36 @@ describe("buildThreadFeed", () => {
24602460
expanded: true,
24612461
summary: "Thinking",
24622462
});
2463+
const strandedToolFeed = buildThreadFeed({
2464+
...thread,
2465+
messages: [],
2466+
activities: [
2467+
makeActivity({
2468+
id: EventId.make("stranded-tool"),
2469+
kind: "tool.updated",
2470+
tone: "tool",
2471+
summary: "Running command",
2472+
createdAt: "2026-04-01T00:00:00.000Z",
2473+
turnId,
2474+
payload: {
2475+
toolCallId: "stranded-tool",
2476+
itemType: "command_execution",
2477+
command: "sleep 60",
2478+
status: "inProgress",
2479+
},
2480+
}),
2481+
],
2482+
});
2483+
const afterStrandedTool = deriveThreadFeedPresentation(
2484+
[...strandedToolFeed, ...feedWithWork],
2485+
thread.latestTurn,
2486+
new Set(),
2487+
new Set(),
2488+
"now",
2489+
);
2490+
expect(afterStrandedTool).toMatchObject([
2491+
{ type: "work-toggle", id: "live-activity-row", summary: toolFirst.summary, hiddenCount: 6 },
2492+
]);
24632493
});
24642494

24652495
it.each(["tool", "failed-tool", "assistant", "turn", "unknown-turn"] as const)(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,12 +1950,16 @@ function appendMixedActivityRun(
19501950
const activities = history.flatMap((entry) =>
19511951
entry.type === "activity-group" ? entry.activities : [],
19521952
);
1953+
const trailingGroup = history.at(-1);
1954+
// A missing completion before the latest thought must not reclaim the live line.
1955+
const summaryActivities =
1956+
live && trailingGroup?.type === "activity-group" ? trailingGroup.activities : activities;
19531957
const toolRows: ThreadFeedEntry[] = [];
1954-
if (activities.length > 0) {
1958+
if (summaryActivities.length > 0) {
19551959
appendToolGroupRows(
19561960
toolRows,
19571961
{ type: "activity-group", id: first.id, createdAt: first.createdAt, turnId, activities },
1958-
activities,
1962+
summaryActivities,
19591963
new Set(),
19601964
unsettledTurnId,
19611965
isWorking,

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,62 @@ describe("deriveMessagesTimelineRows", () => {
21222122
});
21232123
});
21242124

2125+
it("keeps the assistant footer before a trailing thought-only group", () => {
2126+
const answer = answerEntry("assistant-entry", "2026-01-01T00:00:01Z", "turn-1");
2127+
const thought = reasoningEntry("reasoning-after", "2026-01-01T00:00:02Z", "turn-1");
2128+
const rows = deriveMessagesTimelineRows({
2129+
timelineEntries: [answer, thought],
2130+
isWorking: false,
2131+
activeTurnStartedAt: null,
2132+
turnDiffSummaries: [],
2133+
supportsConversationRollback: false,
2134+
});
2135+
expect(rows.map((row) => row.kind)).toEqual(["message", "activity-group"]);
2136+
expect(rows[0]).toMatchObject({ message: answer.message, showAssistantMeta: true });
2137+
expect(rows[1]).toMatchObject({ entries: [thought] });
2138+
});
2139+
2140+
it("shows each tool once across expanded activity histories separated by a failed tool", () => {
2141+
const thought = reasoningEntry("reasoning-entry", "2026-01-01T00:00:01Z", "turn-1");
2142+
const tools = ["a", "b", "c"].map((id, index) => {
2143+
const entry = toolEntry(id, `2026-01-01T00:00:0${index + 2}Z`, "turn-1");
2144+
return {
2145+
...entry,
2146+
entry: {
2147+
...entry.entry,
2148+
command: `echo ${id}`,
2149+
toolCallId: id,
2150+
toolLifecycleStatus: id === "b" ? ("failed" as const) : ("completed" as const),
2151+
sourceActivityKind: "tool.completed" as const,
2152+
},
2153+
};
2154+
});
2155+
const input = {
2156+
timelineEntries: [thought, ...tools],
2157+
runningTurnId: TurnId.make("turn-1"),
2158+
isWorking: true,
2159+
activeTurnStartedAt: "2026-01-01T00:00:00Z",
2160+
turnDiffSummaries: [],
2161+
supportsConversationRollback: false,
2162+
} satisfies Parameters<typeof deriveMessagesTimelineRows>[0];
2163+
const rows = deriveMessagesTimelineRows(input);
2164+
const expanded = deriveMessagesTimelineRows({
2165+
...input,
2166+
expandedWorkGroupIds: new Set(rows.flatMap((row) => ("groupId" in row ? [row.groupId] : []))),
2167+
});
2168+
const visibleTools = expanded.flatMap((row) =>
2169+
row.kind === "activity-group" && row.expanded
2170+
? row.entries.flatMap((entry) => (entry.kind === "work" ? [entry.entry.id] : []))
2171+
: row.kind === "work"
2172+
? row.groupedEntries.map((entry) => entry.id)
2173+
: [],
2174+
);
2175+
expect(visibleTools).toEqual(["a", "b", "c"]);
2176+
expect(expanded.filter((row) => row.id === "live-activity-row")).toMatchObject([
2177+
{ kind: "work-live", entry: { id: "c" } },
2178+
]);
2179+
});
2180+
21252181
it("folds mixed activity under worked-for and restores ordered details when expanded", () => {
21262182
const entries = [
21272183
reasoningEntry("reasoning-entry", "2026-01-01T00:00:01Z", "turn-1"),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,9 @@ function attachTrailingToolGroupsToAssistant(
844844
break;
845845
}
846846
if (
847-
(candidate.kind === "work-toggle" || candidate.kind === "activity-group") &&
847+
(candidate.kind === "work-toggle" ||
848+
(candidate.kind === "activity-group" &&
849+
candidate.entries.some((entry) => entry.kind === "work"))) &&
848850
candidate.turnId === turnId
849851
) {
850852
hasTrailingToolGroup = true;
@@ -1023,6 +1025,7 @@ export function deriveMessagesTimelineRows(input: {
10231025
break;
10241026
}
10251027
activeToolEntries.unshift(entry);
1028+
if (workEntryDisplayIndicatesToolFailure(entry.entry)) break;
10261029
}
10271030
const visibleActiveToolEntries = omitSupersededLifecycleMarkers(
10281031
activeToolEntries.filter((entry) => workEntryIsVisibleInGroup(entry.entry, true)),

0 commit comments

Comments
 (0)