Skip to content

Commit 7e86a3c

Browse files
fix(web): batch in-place timeline entry updates and respect anchored order
Re-derive all changed tool/work items in one call so the attempt, node, and plan lookups build once per update instead of once per changed item. Fall back to a full rebuild when a changed item's createdAt moves while anchored messages are present, since those are placed by createdAt. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f5f8af6 commit 7e86a3c

2 files changed

Lines changed: 63 additions & 26 deletions

File tree

‎apps/web/src/session-logic.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,33 @@ describe("incremental v2 timeline entries", () => {
14881488
if (entry.id === "live-work") expect(next.entries[index]).not.toBe(entry);
14891489
else expect(next.entries[index]).toBe(entry);
14901490
}
1491+
1492+
const anchoredInput = {
1493+
...nextInput,
1494+
anchoredMessages: [
1495+
{
1496+
id: MessageId.make("feedback"),
1497+
runId: null,
1498+
role: "assistant" as const,
1499+
text: "Feedback received",
1500+
streaming: false,
1501+
createdAt: fixture.time(6),
1502+
updatedAt: fixture.time(6),
1503+
},
1504+
],
1505+
};
1506+
const anchored = deriveTimelineEntriesFromVisibleTurnItemsWithState(anchoredInput);
1507+
const movedInput = {
1508+
...anchoredInput,
1509+
visibleTurnItems: anchoredInput.visibleTurnItems.map((row) =>
1510+
row.item.id === "live-work"
1511+
? { ...row, item: { ...row.item, startedAt: DateTime.makeUnsafe(fixture.time(8)) } }
1512+
: row,
1513+
),
1514+
};
1515+
expect(
1516+
deriveTimelineEntriesFromVisibleTurnItemsWithState(movedInput, anchored).entries,
1517+
).toEqual(deriveTimelineEntriesFromVisibleTurnItems(movedInput));
14911518
});
14921519

14931520
it.each(["completion", "provenance", "ordering", "attachment", "run"] as const)(

‎apps/web/src/session-logic.ts‎

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -905,29 +905,23 @@ export function isStreamingTurnItemTextUpdate(
905905

906906
/**
907907
* A changed tool or work item maps to exactly one entry with the item's id.
908-
* Messages and question answers take part in cross-entry dedupe, so they rebuild.
908+
* Messages and question answers take part in cross-entry dedupe, and anchored
909+
* messages are placed by createdAt, so those changes rebuild.
909910
*/
910-
function rederivedNonMessageEntry(
911+
function canRederiveInPlace(
911912
input: TimelineEntriesInput,
912913
previousItem: OrchestrationV2ProjectedTurnItem,
913914
item: OrchestrationV2ProjectedTurnItem,
914-
): TimelineEntry | null {
915-
if (
916-
item.item.id !== previousItem.item.id ||
917-
item.item.type !== previousItem.item.type ||
918-
item.item.type === "user_message" ||
919-
item.item.type === "assistant_message" ||
920-
item.item.type === "user_input_request"
921-
) {
922-
return null;
923-
}
924-
const entries = deriveTimelineEntriesFromVisibleTurnItems({
925-
...input,
926-
visibleTurnItems: [item],
927-
optimisticMessages: [],
928-
anchoredMessages: [],
929-
});
930-
return entries.length === 1 && entries[0]!.id === item.item.id ? entries[0]! : null;
915+
): boolean {
916+
return (
917+
item.item.id === previousItem.item.id &&
918+
item.item.type === previousItem.item.type &&
919+
item.item.type !== "user_message" &&
920+
item.item.type !== "assistant_message" &&
921+
item.item.type !== "user_input_request" &&
922+
((input.anchoredMessages?.length ?? 0) === 0 ||
923+
projectedItemCreatedAt(item) === projectedItemCreatedAt(previousItem))
924+
);
931925
}
932926

933927
function reuseTimelineEntries(
@@ -970,7 +964,7 @@ function reuseTimelineEntries(
970964
OrchestrationV2ProjectedTurnItem,
971965
OrchestrationV2ProjectedTurnItem
972966
>();
973-
const rederivedEntries = new Map<string, TimelineEntry>();
967+
const changedItems: OrchestrationV2ProjectedTurnItem[] = [];
974968
for (let index = 0; index < before.visibleTurnItems.length; index += 1) {
975969
const previousItem = before.visibleTurnItems[index]!;
976970
const item = input.visibleTurnItems[index]!;
@@ -979,19 +973,35 @@ function reuseTimelineEntries(
979973
replacements.set(previousItem, item);
980974
continue;
981975
}
982-
const entry = rederivedNonMessageEntry(input, previousItem, item);
983-
if (entry === null) return null;
984-
rederivedEntries.set(entry.id, entry);
976+
if (!canRederiveInPlace(input, previousItem, item)) return null;
977+
changedItems.push(item);
978+
}
979+
// One batch keeps the attempt/node/plan lookups to a single build.
980+
const rederived =
981+
changedItems.length === 0
982+
? []
983+
: deriveTimelineEntriesFromVisibleTurnItems({
984+
...input,
985+
visibleTurnItems: changedItems,
986+
optimisticMessages: [],
987+
anchoredMessages: [],
988+
});
989+
if (
990+
rederived.length !== changedItems.length ||
991+
rederived.some((entry, index) => entry.id !== changedItems[index]!.item.id)
992+
) {
993+
return null;
985994
}
995+
const rederivedEntries = new Map(rederived.map((entry) => [entry.id, entry]));
986996
if (replacements.size === 0 && rederivedEntries.size === 0 && !appended) {
987997
return previous.entries;
988998
}
989999
let rederivedCount = 0;
9901000
const entries = previous.entries.map((entry): TimelineEntry => {
991-
const rederived = rederivedEntries.get(entry.id);
992-
if (rederived !== undefined) {
1001+
const rederivedEntry = rederivedEntries.get(entry.id);
1002+
if (rederivedEntry !== undefined) {
9931003
rederivedCount += 1;
994-
return rederived;
1004+
return rederivedEntry;
9951005
}
9961006
const row =
9971007
entry.kind === "message" && entry.projectedItem !== undefined

0 commit comments

Comments
 (0)