Skip to content

Commit f6787e8

Browse files
fix(web): reuse timeline rows while thinking and plans stream (pingdotgg#24121)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 9fb4492 commit f6787e8

5 files changed

Lines changed: 484 additions & 15 deletions

File tree

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

Lines changed: 219 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
workEntryDisplayIndicatesToolFailure,
1919
type TimelineEntry,
2020
} from "../../session-logic";
21-
import { makeStreamingTimelineFixture } from "../../test-fixtures";
21+
import {
22+
makeStreamingTimelineFixture,
23+
makeStreamingWorkTimelineFixture,
24+
withStreamedText,
25+
} from "../../test-fixtures";
2226
import type { TurnDiffSummary } from "../../types";
2327
import { describe, expect, it } from "vite-plus/test";
2428
import { MessageId, RunId } from "@t3tools/contracts";
@@ -3891,6 +3895,220 @@ describe("streaming v2 row projection", () => {
38913895
})),
38923896
});
38933897
});
3898+
3899+
it.each(["reasoning", "proposed_plan"] as const)(
3900+
"keeps every row not holding streaming %s text when it changes",
3901+
(kind) => {
3902+
const source = makeStreamingWorkTimelineFixture(kind);
3903+
const rowsInput = (timelineEntries: ReadonlyArray<TimelineEntry>) => ({
3904+
timelineEntries,
3905+
latestRun: {
3906+
runId: source.runId,
3907+
status: "running" as const,
3908+
startedAt: source.time(5),
3909+
completedAt: null,
3910+
},
3911+
runningRunId: source.runId,
3912+
isWorking: true,
3913+
activeTurnStartedAt: source.time(5),
3914+
turnDiffSummaries: [],
3915+
supportsConversationRollback: true,
3916+
});
3917+
let timelineInput = {
3918+
visibleTurnItems: withStreamedText(
3919+
source.visibleTurnItems,
3920+
source.streamingItemId,
3921+
"Let",
3922+
source.time(20),
3923+
),
3924+
optimisticMessages: [],
3925+
};
3926+
let timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState(timelineInput);
3927+
let projection = deriveMessagesTimelineRowsWithState(rowsInput(timeline.entries));
3928+
for (const [step, text] of ["Let me", "Let me check the tests"].entries()) {
3929+
timelineInput = {
3930+
visibleTurnItems: withStreamedText(
3931+
timelineInput.visibleTurnItems,
3932+
source.streamingItemId,
3933+
text,
3934+
source.time(21 + step),
3935+
),
3936+
optimisticMessages: [],
3937+
};
3938+
const previous = projection;
3939+
timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState(timelineInput, timeline);
3940+
projection = deriveMessagesTimelineRowsWithState(rowsInput(timeline.entries), previous);
3941+
expect(projection.rows).toEqual(
3942+
deriveMessagesTimelineRows(
3943+
rowsInput(deriveTimelineEntriesFromVisibleTurnItems(timelineInput)),
3944+
),
3945+
);
3946+
expect(projection.rows).toHaveLength(previous.rows.length);
3947+
for (const [index, row] of previous.rows.entries()) {
3948+
const holdsItem =
3949+
row.id === source.streamingItemId ||
3950+
((row.kind === "work" || row.kind === "work-live") &&
3951+
row.groupedEntries.some((entry) => entry.id === source.streamingItemId));
3952+
if (!holdsItem) expect(projection.rows[index]).toBe(row);
3953+
}
3954+
}
3955+
},
3956+
);
3957+
3958+
it.each(["reasoning", "proposed_plan"] as const)(
3959+
"matches a full derivation when streaming %s text turns blank or completes",
3960+
(kind) => {
3961+
const source = makeStreamingWorkTimelineFixture(kind);
3962+
const rowsInput = (timelineEntries: ReadonlyArray<TimelineEntry>) => ({
3963+
timelineEntries,
3964+
latestRun: {
3965+
runId: source.runId,
3966+
status: "running" as const,
3967+
startedAt: source.time(5),
3968+
completedAt: null,
3969+
},
3970+
runningRunId: source.runId,
3971+
isWorking: true,
3972+
activeTurnStartedAt: source.time(5),
3973+
turnDiffSummaries: [],
3974+
supportsConversationRollback: true,
3975+
});
3976+
let visibleTurnItems = source.visibleTurnItems;
3977+
let timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState({
3978+
visibleTurnItems,
3979+
optimisticMessages: [],
3980+
});
3981+
let projection = deriveMessagesTimelineRowsWithState(rowsInput(timeline.entries));
3982+
const steps = [
3983+
...["Let", " ", "Let me"].map(
3984+
(text, step) => (rows: typeof visibleTurnItems) =>
3985+
withStreamedText(rows, source.streamingItemId, text, source.time(20 + step)),
3986+
),
3987+
(rows: typeof visibleTurnItems) =>
3988+
rows.map((row) =>
3989+
row.item.id === source.streamingItemId &&
3990+
(row.item.type === "reasoning" || row.item.type === "proposed_plan")
3991+
? {
3992+
...row,
3993+
item: {
3994+
...row.item,
3995+
status: "completed" as const,
3996+
streaming: false,
3997+
completedAt: DateTime.makeUnsafe(source.time(30)),
3998+
updatedAt: DateTime.makeUnsafe(source.time(30)),
3999+
},
4000+
}
4001+
: row,
4002+
),
4003+
];
4004+
for (const step of steps) {
4005+
visibleTurnItems = step(visibleTurnItems);
4006+
const timelineInput = { visibleTurnItems, optimisticMessages: [] };
4007+
timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState(timelineInput, timeline);
4008+
expect(timeline.entries).toEqual(deriveTimelineEntriesFromVisibleTurnItems(timelineInput));
4009+
projection = deriveMessagesTimelineRowsWithState(rowsInput(timeline.entries), projection);
4010+
expect(projection.rows).toEqual(
4011+
deriveMessagesTimelineRows(
4012+
rowsInput(deriveTimelineEntriesFromVisibleTurnItems(timelineInput)),
4013+
),
4014+
);
4015+
}
4016+
},
4017+
);
4018+
4019+
it.each([
4020+
"expanded live group",
4021+
"expanded live group followed by a hidden tool",
4022+
"thinking superseded by a running tool",
4023+
"settled turn",
4024+
] as const)("matches a full derivation when streaming thinking sits in a %s", (scenario) => {
4025+
const source = makeStreamingWorkTimelineFixture("reasoning");
4026+
const running = scenario !== "settled turn";
4027+
const hiddenTool = scenario === "expanded live group followed by a hidden tool";
4028+
const rowsInput = (
4029+
timelineEntries: ReadonlyArray<TimelineEntry>,
4030+
expandedWorkGroupIds?: ReadonlySet<string>,
4031+
) => ({
4032+
timelineEntries,
4033+
latestRun: {
4034+
runId: source.runId,
4035+
status: running ? ("running" as const) : ("completed" as const),
4036+
startedAt: source.time(5),
4037+
completedAt: running ? null : source.time(40),
4038+
},
4039+
runningRunId: running ? source.runId : null,
4040+
isWorking: running,
4041+
activeTurnStartedAt: source.time(5),
4042+
turnDiffSummaries: [],
4043+
supportsConversationRollback: true,
4044+
...(expandedWorkGroupIds ? { expandedWorkGroupIds } : {}),
4045+
...(running ? {} : { expandedRunIds: new Set([source.runId]) }),
4046+
...(hiddenTool ? { toolCallVisibility: "hideAll" as const } : {}),
4047+
});
4048+
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+
});
4061+
let visibleTurnItems =
4062+
scenario === "thinking superseded by a running tool" || hiddenTool
4063+
? [...source.visibleTurnItems, laterTool(hiddenTool ? "completed" : "running")]
4064+
: // Alone in a settled group, the thought's text becomes the group summary.
4065+
source.visibleTurnItems.filter((row) => running || row !== tool);
4066+
visibleTurnItems = withStreamedText(
4067+
visibleTurnItems,
4068+
source.streamingItemId,
4069+
"Let",
4070+
source.time(20),
4071+
);
4072+
let timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState({
4073+
visibleTurnItems,
4074+
optimisticMessages: [],
4075+
});
4076+
const liveRow = deriveMessagesTimelineRows(rowsInput(timeline.entries)).find(
4077+
(row) => row.kind === "work-live",
4078+
);
4079+
const expanded =
4080+
scenario.startsWith("expanded live group") && liveRow?.kind === "work-live"
4081+
? new Set([liveRow.groupId])
4082+
: undefined;
4083+
let projection = deriveMessagesTimelineRowsWithState(rowsInput(timeline.entries, expanded));
4084+
for (const [step, text] of ["Let me", "Let me check the tests"].entries()) {
4085+
visibleTurnItems = withStreamedText(
4086+
visibleTurnItems,
4087+
source.streamingItemId,
4088+
text,
4089+
source.time(21 + step),
4090+
);
4091+
const timelineInput = { visibleTurnItems, optimisticMessages: [] };
4092+
const previous = projection;
4093+
timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState(timelineInput, timeline);
4094+
projection = deriveMessagesTimelineRowsWithState(
4095+
rowsInput(timeline.entries, expanded),
4096+
previous,
4097+
);
4098+
expect(projection.rows).toEqual(
4099+
deriveMessagesTimelineRows(
4100+
rowsInput(deriveTimelineEntriesFromVisibleTurnItems(timelineInput), expanded),
4101+
),
4102+
);
4103+
if (!running) continue;
4104+
for (const [index, row] of previous.rows.entries()) {
4105+
const holdsItem =
4106+
(row.kind === "work" || row.kind === "work-live") &&
4107+
row.groupedEntries.some((entry) => entry.id === source.streamingItemId);
4108+
if (!holdsItem) expect(projection.rows[index]).toBe(row);
4109+
}
4110+
}
4111+
});
38944112
});
38954113

38964114
describe("linked timeline resources", () => {

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

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,19 +2481,33 @@ function replaceStreamingMessageRows(
24812481
return null;
24822482
}
24832483
const replacements = new Map<ChatMessage, Extract<TimelineEntry, { kind: "message" }>>();
2484+
const thoughts = new Map<string, WorkLogEntry>();
2485+
const plans = new Map<string, ProposedPlan>();
24842486
for (let index = 0; index < timelineEntries.length; index += 1) {
24852487
const entry = timelineEntries[index]!;
24862488
const previousEntry = previousEntries[index]!;
24872489
if (entry === previousEntry) continue;
24882490
if (
2489-
entry.kind !== "message" ||
2490-
previousEntry.kind !== "message" ||
2491+
entry.kind !== previousEntry.kind ||
24912492
entry.id !== previousEntry.id ||
24922493
entry.createdAt !== previousEntry.createdAt ||
24932494
entry.attempt !== previousEntry.attempt
24942495
) {
24952496
return null;
24962497
}
2498+
if (entry.kind === "work" && previousEntry.kind === "work") {
2499+
if (!isStreamingThoughtUpdate(previousEntry.entry, entry.entry)) return null;
2500+
thoughts.set(entry.id, entry.entry);
2501+
continue;
2502+
}
2503+
if (entry.kind === "proposed-plan" && previousEntry.kind === "proposed-plan") {
2504+
const { planMarkdown: _before, updatedAt: _beforeAt, ...before } = previousEntry.proposedPlan;
2505+
const { planMarkdown: _after, updatedAt: _afterAt, ...after } = entry.proposedPlan;
2506+
if (!shallow(before, after)) return null;
2507+
plans.set(entry.id, entry.proposedPlan);
2508+
continue;
2509+
}
2510+
if (entry.kind !== "message" || previousEntry.kind !== "message") return null;
24972511
if (
24982512
entry.projectedItem !== previousEntry.projectedItem &&
24992513
(entry.projectedItem === undefined ||
@@ -2510,8 +2524,54 @@ function replaceStreamingMessageRows(
25102524
if (!isStreamingMessageTextUpdate(previousEntry.message, entry.message)) return null;
25112525
replacements.set(previousEntry.message, entry);
25122526
}
2513-
if (replacements.size === 0) return previous.rows;
2514-
return previous.rows.map((row) => {
2527+
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+
};
2545+
const patchedGroups = new Map<WorkLogEntry[], WorkLogEntry[]>();
2546+
const patchGroup = (group: WorkLogEntry[]) => {
2547+
if (!group.some((entry) => thoughts.has(entry.id))) return group;
2548+
const patched = patchedGroups.get(group) ?? group.map(patchThought);
2549+
patchedGroups.set(group, patched);
2550+
return patched;
2551+
};
2552+
const patchedLiveThoughtIds = new Set<string>();
2553+
const rows = previous.rows.map((row): MessagesTimelineRow => {
2554+
if (row.kind === "proposed-plan") {
2555+
const proposedPlan = plans.get(row.id);
2556+
return proposedPlan ? { ...row, proposedPlan } : row;
2557+
}
2558+
if (row.kind === "work-live") {
2559+
const groupedEntries = patchGroup(row.groupedEntries);
2560+
if (groupedEntries === row.groupedEntries) return row;
2561+
for (const entry of groupedEntries) {
2562+
if (thoughts.has(entry.id)) patchedLiveThoughtIds.add(entry.id);
2563+
}
2564+
return {
2565+
...row,
2566+
entry: patchThought(row.entry),
2567+
groupedEntries,
2568+
...(row.thought ? { thought: patchThought(row.thought) } : {}),
2569+
};
2570+
}
2571+
if (row.kind === "work") {
2572+
const groupedEntries = patchGroup(row.groupedEntries);
2573+
return groupedEntries === row.groupedEntries ? row : { ...row, groupedEntries };
2574+
}
25152575
if (row.kind !== "message" && row.kind !== "assistant-meta") return row;
25162576
const entry = replacements.get(row.message);
25172577
return entry
@@ -2522,6 +2582,39 @@ function replaceStreamingMessageRows(
25222582
}
25232583
: row;
25242584
});
2585+
// Only a live row holds thinking without deriving a label from its text. Settled
2586+
// groups, folded turns, and work toggles summarize it, so they need the full rebuild.
2587+
for (const id of thoughts.keys()) if (!patchedLiveThoughtIds.has(id)) return null;
2588+
return rows;
2589+
}
2590+
2591+
/**
2592+
* Rows read a thought's text only through its blankness (visibility, the live
2593+
* thought line) and through labels that live rows do not store.
2594+
*/
2595+
function isStreamingThoughtUpdate(previous: WorkLogEntry, next: WorkLogEntry): boolean {
2596+
if (
2597+
previous.itemType !== "reasoning" ||
2598+
previous.projectedItem === undefined ||
2599+
next.projectedItem === undefined ||
2600+
!isStreamingTurnItemTextUpdate(previous.projectedItem, next.projectedItem) ||
2601+
Boolean(previous.detail?.trim()) !== Boolean(next.detail?.trim())
2602+
) {
2603+
return false;
2604+
}
2605+
const {
2606+
detail: _before,
2607+
structuredPayload: _beforeItem,
2608+
projectedItem: _beforeRow,
2609+
...rest
2610+
} = previous;
2611+
const {
2612+
detail: _after,
2613+
structuredPayload: _afterItem,
2614+
projectedItem: _afterRow,
2615+
...nextRest
2616+
} = next;
2617+
return shallow(rest, nextRest);
25252618
}
25262619

25272620
export function deriveMessagesTimelineRowsWithState(

0 commit comments

Comments
 (0)