Skip to content

Commit b3eb122

Browse files
perf(mobile): reuse unchanged tool rows during chat sync
1 parent c14f601 commit b3eb122

2 files changed

Lines changed: 123 additions & 2 deletions

File tree

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,100 @@ function makeThread(
291291
}
292292

293293
describe("buildThreadFeed", () => {
294+
it("keeps completed tool rows stable when another activity arrives", () => {
295+
const turnId = TurnId.make("tool-history");
296+
const started = makeActivity({
297+
id: EventId.make("tool-start"),
298+
kind: "tool.updated",
299+
summary: "Run checks",
300+
createdAt: "2026-04-01T00:00:01.000Z",
301+
turnId,
302+
payload: {
303+
toolCallId: "checks",
304+
itemType: "command_execution",
305+
data: { command: "vp test" },
306+
status: "inProgress",
307+
},
308+
});
309+
const completed = makeActivity({
310+
...started,
311+
id: EventId.make("tool-complete"),
312+
kind: "tool.completed",
313+
createdAt: "2026-04-01T00:00:02.000Z",
314+
payload: { toolCallId: "checks", status: "completed", detail: "All checks passed" },
315+
});
316+
const thread = { messages: [], activities: [started, completed] };
317+
const rows = (activities: ReadonlyArray<OrchestrationThreadActivity>) =>
318+
buildThreadFeed({ ...thread, activities }).flatMap((entry) =>
319+
entry.type === "activity-group" ? entry.activities : [],
320+
);
321+
const previous = rows(thread.activities)[0]!;
322+
const appended = makeActivity({
323+
id: EventId.make("next-tool"),
324+
kind: "tool.completed",
325+
summary: "Read file",
326+
createdAt: "2026-04-01T00:00:03.000Z",
327+
turnId,
328+
payload: { toolCallId: "read", itemType: "file_read", status: "completed" },
329+
});
330+
const next = rows([...thread.activities, appended]);
331+
expect(next).toHaveLength(2);
332+
expect(next[0]).toBe(previous);
333+
expect(next[0]?.getCopyText()).toContain("All checks passed");
334+
expect(next[0]?.status).toBe("success");
335+
336+
const failed = {
337+
...completed,
338+
payload: { toolCallId: "checks", status: "failed", detail: "Checks failed" },
339+
};
340+
const updated = rows([started, failed, appended]);
341+
expect(updated[0]).not.toBe(previous);
342+
expect(updated[0]?.status).toBe("failure");
343+
expect(updated[0]?.getCopyText()).toContain("Checks failed");
344+
expect(updated[1]).toBe(next[1]);
345+
});
346+
347+
it("recomputes a tool merge when pagination changes its earlier activity", () => {
348+
const completed = makeActivity({
349+
id: EventId.make("completion"),
350+
kind: "tool.completed",
351+
summary: "Command completed",
352+
createdAt: "2026-04-01T00:00:02.000Z",
353+
payload: { toolCallId: "call", status: "completed", detail: "Done" },
354+
});
355+
const started = makeActivity({
356+
...completed,
357+
id: EventId.make("start"),
358+
kind: "tool.updated",
359+
createdAt: "2026-04-01T00:00:01.000Z",
360+
payload: {
361+
toolCallId: "call",
362+
data: { command: "original command" },
363+
status: "inProgress",
364+
},
365+
});
366+
const row = (activities: ReadonlyArray<OrchestrationThreadActivity>) =>
367+
buildThreadFeed({ messages: [], activities }).flatMap((entry) =>
368+
entry.type === "activity-group" ? entry.activities : [],
369+
)[0]!;
370+
const original = row([started, completed]);
371+
expect(original.workEntry.command).toBe("original command");
372+
const replaced = row([
373+
{
374+
...started,
375+
payload: {
376+
toolCallId: "call",
377+
data: { command: "replacement command" },
378+
status: "inProgress",
379+
},
380+
},
381+
completed,
382+
]);
383+
expect(replaced.workEntry.command).toBe("replacement command");
384+
expect(row([completed]).workEntry.command).toBeUndefined();
385+
expect(row([started, completed]).workEntry.command).toBe("original command");
386+
});
387+
294388
it("reuses unchanged feed and presentation rows during an assistant text update", () => {
295389
const completedTurnId = TurnId.make("completed-turn");
296390
const activeTurnId = TurnId.make("active-turn");

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ const activityEntriesCache = new WeakMap<
238238
ReadonlyArray<OrchestrationThreadActivity>,
239239
ReadonlyArray<Extract<RawThreadFeedEntry, { readonly type: "activity" }>>
240240
>();
241+
// Activity arrays change on every tool update; their unchanged rows must not
242+
// repeatedly parse old output or invalidate the rendered work log.
243+
const derivedActivityCache = new WeakMap<OrchestrationThreadActivity, DerivedWorkLogEntry>();
244+
const mergedWorkEntriesCache = new WeakMap<
245+
DerivedWorkLogEntry,
246+
WeakMap<DerivedWorkLogEntry, DerivedWorkLogEntry>
247+
>();
248+
const activityEntryCache = new WeakMap<
249+
DerivedWorkLogEntry,
250+
Extract<RawThreadFeedEntry, { readonly type: "activity" }>
251+
>();
241252
const messageEntriesCache = new WeakMap<
242253
OrchestrationThread["messages"][number],
243254
Extract<RawThreadFeedEntry, { readonly type: "message" }>
@@ -476,6 +487,8 @@ function isPlanBoundaryToolActivity(activity: OrchestrationThreadActivity): bool
476487
const decodeQuestionAttachmentAnswer = Schema.decodeUnknownOption(UserInputAttachmentAnswerPayload);
477488

478489
function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWorkLogEntry {
490+
const cached = derivedActivityCache.get(activity);
491+
if (cached) return cached;
479492
const payload =
480493
activity.payload && typeof activity.payload === "object"
481494
? (activity.payload as Record<string, unknown>)
@@ -630,6 +643,7 @@ function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWo
630643
if (collapseKey) {
631644
entry.collapseKey = collapseKey;
632645
}
646+
derivedActivityCache.set(activity, entry);
633647
return entry;
634648
}
635649

@@ -867,6 +881,9 @@ function mergeDerivedWorkLogEntries(
867881
previous: DerivedWorkLogEntry,
868882
next: DerivedWorkLogEntry,
869883
): DerivedWorkLogEntry {
884+
let byPrevious = mergedWorkEntriesCache.get(next);
885+
const cached = byPrevious?.get(previous);
886+
if (cached) return cached;
870887
const changedFiles = mergeChangedFiles(previous.changedFiles, next.changedFiles);
871888
const detail = next.detail ?? previous.detail;
872889
const viewedImagePath = next.viewedImagePath ?? previous.viewedImagePath;
@@ -882,7 +899,7 @@ function mergeDerivedWorkLogEntries(
882899
const toolLifecycleStatus = next.toolLifecycleStatus ?? previous.toolLifecycleStatus;
883900
const toolCallId = next.toolCallId ?? previous.toolCallId;
884901
const toolData = next.toolData ?? previous.toolData;
885-
return {
902+
const merged = {
886903
...previous,
887904
...next,
888905
id: previous.id,
@@ -903,6 +920,12 @@ function mergeDerivedWorkLogEntries(
903920
...(toolCallId ? { toolCallId } : {}),
904921
...(toolData !== undefined ? { toolData } : {}),
905922
};
923+
if (byPrevious === undefined) {
924+
byPrevious = new WeakMap();
925+
mergedWorkEntriesCache.set(next, byPrevious);
926+
}
927+
byPrevious.set(previous, merged);
928+
return merged;
906929
}
907930

908931
function mergeChangedFiles(
@@ -2459,6 +2482,8 @@ function getThreadFeedActivityEntries(activities: ReadonlyArray<OrchestrationThr
24592482
function toThreadFeedActivityEntry(
24602483
entry: DerivedWorkLogEntry,
24612484
): Extract<RawThreadFeedEntry, { readonly type: "activity" }> {
2485+
const cached = activityEntryCache.get(entry);
2486+
if (cached) return cached;
24622487
const summary = workEntryHeading(entry);
24632488
const detail = workEntryPreview(entry);
24642489
const getFullDetail = memoizeValue(() => buildWorkEntryExpandedBody(entry));
@@ -2478,7 +2503,7 @@ function toThreadFeedActivityEntry(
24782503
})
24792504
.join("\n");
24802505
});
2481-
return {
2506+
const result: Extract<RawThreadFeedEntry, { readonly type: "activity" }> = {
24822507
type: "activity",
24832508
id: entry.id,
24842509
createdAt: entry.createdAt,
@@ -2499,4 +2524,6 @@ function toThreadFeedActivityEntry(
24992524
workEntry: entry,
25002525
},
25012526
};
2527+
activityEntryCache.set(entry, result);
2528+
return result;
25022529
}

0 commit comments

Comments
 (0)