Skip to content

Commit a7e7069

Browse files
fix(web): streamed thinking keeps its settled status when later tools are hidden
The incremental row path settled superseded thinking by position in the unfiltered entries, while full derivation settles after tool visibility filtering. A hidden tool after a still-streaming thought marked it completed. Keep the status the row already had instead, which is valid because the fast path only accepts text-only updates. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1aebd8e commit a7e7069

2 files changed

Lines changed: 56 additions & 43 deletions

File tree

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4018,11 +4018,13 @@ describe("streaming v2 row projection", () => {
40184018

40194019
it.each([
40204020
"expanded live group",
4021+
"expanded live group followed by a hidden tool",
40214022
"thinking superseded by a running tool",
40224023
"settled turn",
40234024
] as const)("matches a full derivation when streaming thinking sits in a %s", (scenario) => {
40244025
const source = makeStreamingWorkTimelineFixture("reasoning");
40254026
const running = scenario !== "settled turn";
4027+
const hiddenTool = scenario === "expanded live group followed by a hidden tool";
40264028
const rowsInput = (
40274029
timelineEntries: ReadonlyArray<TimelineEntry>,
40284030
expandedWorkGroupIds?: ReadonlySet<string>,
@@ -4041,25 +4043,24 @@ describe("streaming v2 row projection", () => {
40414043
supportsConversationRollback: true,
40424044
...(expandedWorkGroupIds ? { expandedWorkGroupIds } : {}),
40434045
...(running ? {} : { expandedRunIds: new Set([source.runId]) }),
4046+
...(hiddenTool ? { toolCallVisibility: "hideAll" as const } : {}),
40444047
});
40454048
const tool = source.visibleTurnItems.find((row) => row.item.id === "live-work")!;
4049+
const laterTool = (status: "running" | "completed") => ({
4050+
...tool,
4051+
position: source.visibleTurnItems.length,
4052+
sourceItemId: TurnItemId.make("live-later-tool"),
4053+
item: {
4054+
...tool.item,
4055+
id: TurnItemId.make("live-later-tool"),
4056+
ordinal: 8,
4057+
status,
4058+
...(status === "running" ? { completedAt: null } : {}),
4059+
},
4060+
});
40464061
let visibleTurnItems =
4047-
scenario === "thinking superseded by a running tool"
4048-
? [
4049-
...source.visibleTurnItems,
4050-
{
4051-
...tool,
4052-
position: source.visibleTurnItems.length,
4053-
sourceItemId: TurnItemId.make("live-running-tool"),
4054-
item: {
4055-
...tool.item,
4056-
id: TurnItemId.make("live-running-tool"),
4057-
ordinal: 8,
4058-
status: "running" as const,
4059-
completedAt: null,
4060-
},
4061-
},
4062-
]
4062+
scenario === "thinking superseded by a running tool" || hiddenTool
4063+
? [...source.visibleTurnItems, laterTool(hiddenTool ? "completed" : "running")]
40634064
: // Alone in a settled group, the thought's text becomes the group summary.
40644065
source.visibleTurnItems.filter((row) => running || row !== tool);
40654066
visibleTurnItems = withStreamedText(
@@ -4076,7 +4077,7 @@ describe("streaming v2 row projection", () => {
40764077
(row) => row.kind === "work-live",
40774078
);
40784079
const expanded =
4079-
scenario === "expanded live group" && liveRow?.kind === "work-live"
4080+
scenario.startsWith("expanded live group") && liveRow?.kind === "work-live"
40804081
? new Set([liveRow.groupId])
40814082
: undefined;
40824083
let projection = deriveMessagesTimelineRowsWithState(rowsInput(timeline.entries, expanded));

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,29 +1363,25 @@ function withoutSubagentDelegationRows(entries: ReadonlyArray<TimelineEntry>) {
13631363
});
13641364
}
13651365

1366-
type WorkTimelineEntry = Extract<TimelineEntry, { kind: "work" }>;
1367-
const supersededReasoningEntries = new WeakMap<WorkTimelineEntry, WorkTimelineEntry>();
1368-
1369-
function settleReasoning(entry: WorkTimelineEntry, superseded: boolean): WorkTimelineEntry {
1370-
if (
1371-
!superseded ||
1372-
entry.entry.itemType !== "reasoning" ||
1373-
entry.entry.toolLifecycleStatus !== "inProgress"
1374-
) {
1375-
return entry;
1376-
}
1377-
let settled = supersededReasoningEntries.get(entry);
1378-
if (!settled) {
1379-
settled = { ...entry, entry: { ...entry.entry, toolLifecycleStatus: "completed" } };
1380-
supersededReasoningEntries.set(entry, settled);
1381-
}
1382-
return settled;
1383-
}
1366+
const supersededReasoningEntries = new WeakMap<TimelineEntry, TimelineEntry>();
13841367

13851368
function settleSupersededReasoning(entries: ReadonlyArray<TimelineEntry>) {
1386-
return entries.map((entry, index) =>
1387-
entry.kind === "work" ? settleReasoning(entry, index < entries.length - 1) : entry,
1388-
);
1369+
return entries.map((entry, index) => {
1370+
if (
1371+
index === entries.length - 1 ||
1372+
entry.kind !== "work" ||
1373+
entry.entry.itemType !== "reasoning" ||
1374+
entry.entry.toolLifecycleStatus !== "inProgress"
1375+
) {
1376+
return entry;
1377+
}
1378+
let settled = supersededReasoningEntries.get(entry);
1379+
if (!settled) {
1380+
settled = { ...entry, entry: { ...entry.entry, toolLifecycleStatus: "completed" } };
1381+
supersededReasoningEntries.set(entry, settled);
1382+
}
1383+
return settled;
1384+
});
13891385
}
13901386

13911387
function workShownAsFinalAnswerOnly(
@@ -2501,7 +2497,7 @@ function replaceStreamingMessageRows(
25012497
}
25022498
if (entry.kind === "work" && previousEntry.kind === "work") {
25032499
if (!isStreamingThoughtUpdate(previousEntry.entry, entry.entry)) return null;
2504-
thoughts.set(entry.id, settleReasoning(entry, index < timelineEntries.length - 1).entry);
2500+
thoughts.set(entry.id, entry.entry);
25052501
continue;
25062502
}
25072503
if (entry.kind === "proposed-plan" && previousEntry.kind === "proposed-plan") {
@@ -2529,11 +2525,27 @@ function replaceStreamingMessageRows(
25292525
replacements.set(previousEntry.message, entry);
25302526
}
25312527
if (replacements.size === 0 && thoughts.size === 0 && plans.size === 0) return previous.rows;
2528+
// Rows settle superseded thinking after tool visibility filtering, which these entries
2529+
// have not seen; the fast path keeps item status, so the row's settled status still holds.
2530+
const patchedThoughts = new Map<WorkLogEntry, WorkLogEntry>();
2531+
const patchThought = (entry: WorkLogEntry) => {
2532+
const thought = thoughts.get(entry.id);
2533+
if (!thought) return entry;
2534+
let patched = patchedThoughts.get(entry);
2535+
if (!patched) {
2536+
patched =
2537+
entry.toolLifecycleStatus === thought.toolLifecycleStatus ||
2538+
entry.toolLifecycleStatus === undefined
2539+
? thought
2540+
: { ...thought, toolLifecycleStatus: entry.toolLifecycleStatus };
2541+
patchedThoughts.set(entry, patched);
2542+
}
2543+
return patched;
2544+
};
25322545
const patchedGroups = new Map<WorkLogEntry[], WorkLogEntry[]>();
25332546
const patchGroup = (group: WorkLogEntry[]) => {
25342547
if (!group.some((entry) => thoughts.has(entry.id))) return group;
2535-
const patched =
2536-
patchedGroups.get(group) ?? group.map((entry) => thoughts.get(entry.id) ?? entry);
2548+
const patched = patchedGroups.get(group) ?? group.map(patchThought);
25372549
patchedGroups.set(group, patched);
25382550
return patched;
25392551
};
@@ -2551,9 +2563,9 @@ function replaceStreamingMessageRows(
25512563
}
25522564
return {
25532565
...row,
2554-
entry: thoughts.get(row.entry.id) ?? row.entry,
2566+
entry: patchThought(row.entry),
25552567
groupedEntries,
2556-
...(row.thought ? { thought: thoughts.get(row.thought.id) ?? row.thought } : {}),
2568+
...(row.thought ? { thought: patchThought(row.thought) } : {}),
25572569
};
25582570
}
25592571
if (row.kind === "work") {

0 commit comments

Comments
 (0)