Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

perf(web): reuse work log rows during streaming (#8006)

Bilal Bakrfa219001dc2f
diff --git a/apps/web/src/session-logic.test.ts b/apps/web/src/session-logic.test.ts
index e94712d3e..afabd47d4 100644
--- a/apps/web/src/session-logic.test.ts
+++ b/apps/web/src/session-logic.test.ts
@@ -2205,3 +2205,64 @@ describe("rerun workflows", () => {
expect(spawnRows.map((row) => row.turnId)).toEqual(["turn-1", "turn-2"]);
});
});
+
+describe("session activity performance", () => {
+ it("reuses entries for unchanged activities", () => {
+ const activities = ["status", "diff", "log"].map((command, index) =>
+ makeActivity({
+ id: `stable-tool-${index}`,
+ kind: "tool.completed",
+ sequence: index,
+ payload: {
+ itemType: "command_execution",
+ data: { toolCallId: `stable-tool-${index}`, item: { command: ["git", command] } },
+ },
+ }),
+ );
+
+ const initialEntries = deriveWorkLogEntries(activities.slice(0, 2));
+ const appendedEntries = deriveWorkLogEntries(activities);
+ expect(appendedEntries[0]).toBe(initialEntries[0]);
+ expect(appendedEntries[1]).toBe(initialEntries[1]);
+ });
+
+ it("updates 20,000 ordered tool activities within 100 ms", () => {
+ const activities = Array.from({ length: 20_000 }, (_, index) =>
+ makeActivity({
+ id: `benchmark-tool-${index}`,
+ createdAt: new Date(1_700_000_000_000 + index).toISOString(),
+ kind: "tool.completed",
+ summary: "Ran command",
+ sequence: index,
+ payload: {
+ itemType: "command_execution",
+ title: "Ran command",
+ data: {
+ toolCallId: `benchmark-tool-${index}`,
+ item: { command: ["git", "status"] },
+ },
+ },
+ }),
+ );
+ deriveWorkLogEntries(activities);
+ const updatedActivities = [
+ ...activities,
+ makeActivity({
+ id: "benchmark-tool-appended",
+ createdAt: new Date(1_700_000_000_000 + activities.length).toISOString(),
+ kind: "tool.completed",
+ summary: "Ran command",
+ sequence: activities.length,
+ payload: {
+ itemType: "command_execution",
+ title: "Ran command",
+ data: { toolCallId: "benchmark-tool-appended", item: { command: ["git", "diff"] } },
+ },
+ }),
+ ];
+
+ const startedAt = performance.now();
+ expect(deriveWorkLogEntries(updatedActivities)).toHaveLength(20_001);
+ expect(performance.now() - startedAt).toBeLessThan(100);
+ });
+});
diff --git a/apps/web/src/session-logic.ts b/apps/web/src/session-logic.ts
index 482425842..ff06ad57d 100644
--- a/apps/web/src/session-logic.ts
+++ b/apps/web/src/session-logic.ts
@@ -98,15 +98,22 @@ export interface WorkLogEntry {
};
}
+const workLogCollapseKey = Symbol();
+
interface DerivedWorkLogEntry extends WorkLogEntry {
- activityKind: OrchestrationThreadActivity["kind"];
- collapseKey?: string;
+ sourceActivityKind: OrchestrationThreadActivity["kind"];
+ [workLogCollapseKey]?: string;
toolCallId?: string;
isWorkflowCoordinator?: boolean;
/** Shell/monitor/plan tasks: ordinary work-log rows, never spawn CTAs. */
isBackgroundTask?: boolean;
}
+const derivedWorkLogEntryByActivity = new WeakMap<
+ OrchestrationThreadActivity,
+ DerivedWorkLogEntry
+>();
+
export interface PendingApproval {
requestId: ApprovalRequestId;
requestKind: "command" | "file-read" | "file-change";
@@ -854,10 +861,7 @@ export function deriveWorkLogEntries(
if (isAgentInternalActivity(activity)) continue;
entries.push(toDerivedWorkLogEntry(activity));
}
- return collapseDerivedWorkLogEntries(entries).map((entry) => {
- const { activityKind, collapseKey: _collapseKey, ...rest } = entry;
- return Object.assign(rest, { sourceActivityKind: activityKind });
- });
+ return collapseDerivedWorkLogEntries(entries);
}
function isPlanBoundaryToolActivity(activity: OrchestrationThreadActivity): boolean {
@@ -892,6 +896,10 @@ function extractWorkLogToolLifecycleStatus(
}
function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWorkLogEntry {
+ const cachedEntry = derivedWorkLogEntryByActivity.get(activity);
+ if (cachedEntry) {
+ return cachedEntry;
+ }
const payload =
activity.payload && typeof activity.payload === "object"
? (activity.payload as Record<string, unknown>)
@@ -935,7 +943,7 @@ function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWo
: activity.tone === "approval"
? "info"
: activity.tone,
- activityKind: activity.kind,
+ sourceActivityKind: activity.kind,
};
const itemType = extractWorkLogItemType(payload);
const requestKind = extractWorkLogRequestKind(payload);
@@ -994,8 +1002,9 @@ function toDerivedWorkLogEntry(activity: OrchestrationThreadActivity): DerivedWo
}
const collapseKey = deriveToolLifecycleCollapseKey(entry);
if (collapseKey) {
- entry.collapseKey = collapseKey;
+ entry[workLogCollapseKey] = collapseKey;
}
+ derivedWorkLogEntryByActivity.set(activity, entry);
return entry;
}
@@ -1026,7 +1035,10 @@ function agentSpawnGroupKey(entry: DerivedWorkLogEntry): string {
}
function toolLifecycleCollapseMapKey(entry: DerivedWorkLogEntry): string | undefined {
- if (entry.activityKind !== "tool.updated" && entry.activityKind !== "tool.completed") {
+ if (
+ entry.sourceActivityKind !== "tool.updated" &&
+ entry.sourceActivityKind !== "tool.completed"
+ ) {
return undefined;
}
return entry.toolCallId ? `tool:${entry.turnId ?? "no-turn"}:${entry.toolCallId}` : undefined;
@@ -1053,9 +1065,9 @@ function collapseDerivedWorkLogEntries(
const isTaskRow =
entry.taskId !== undefined &&
!entry.isBackgroundTask &&
- (entry.activityKind === "task.started" ||
- entry.activityKind === "task.progress" ||
- entry.activityKind === "task.completed");
+ (entry.sourceActivityKind === "task.started" ||
+ entry.sourceActivityKind === "task.progress" ||
+ entry.sourceActivityKind === "task.completed");
if (isTaskRow && entry.taskId !== undefined) {
const rememberedKey = groupKeyByTaskId.get(entry.taskId);
const groupKey = rememberedKey ?? agentSpawnGroupKey(entry);
@@ -1131,19 +1143,25 @@ function shouldCollapseToolLifecycleEntries(
previous: DerivedWorkLogEntry,
next: DerivedWorkLogEntry,
): boolean {
- if (previous.activityKind !== "tool.updated" && previous.activityKind !== "tool.completed") {
+ if (
+ previous.sourceActivityKind !== "tool.updated" &&
+ previous.sourceActivityKind !== "tool.completed"
+ ) {
return false;
}
- if (next.activityKind !== "tool.updated" && next.activityKind !== "tool.completed") {
+ if (next.sourceActivityKind !== "tool.updated" && next.sourceActivityKind !== "tool.completed") {
return false;
}
if (previous.turnId !== next.turnId) {
return false;
}
- if (previous.activityKind === "tool.completed") {
+ if (previous.sourceActivityKind === "tool.completed") {
return false;
}
- if (previous.collapseKey !== undefined && previous.collapseKey === next.collapseKey) {
+ if (
+ previous[workLogCollapseKey] !== undefined &&
+ previous[workLogCollapseKey] === next[workLogCollapseKey]
+ ) {
return true;
}
return (
@@ -1166,7 +1184,7 @@ function mergeDerivedWorkLogEntries(
const toolTitle = next.toolTitle ?? previous.toolTitle;
const itemType = next.itemType ?? previous.itemType;
const requestKind = next.requestKind ?? previous.requestKind;
- const collapseKey = next.collapseKey ?? previous.collapseKey;
+ const collapseKey = next[workLogCollapseKey] ?? previous[workLogCollapseKey];
const toolCallId = next.toolCallId ?? previous.toolCallId;
const toolLifecycleStatus = next.toolLifecycleStatus ?? previous.toolLifecycleStatus;
const toolData = next.toolData ?? previous.toolData;
@@ -1180,7 +1198,7 @@ function mergeDerivedWorkLogEntries(
...(toolTitle ? { toolTitle } : {}),
...(itemType ? { itemType } : {}),
...(requestKind ? { requestKind } : {}),
- ...(collapseKey ? { collapseKey } : {}),
+ ...(collapseKey ? { [workLogCollapseKey]: collapseKey } : {}),
...(toolCallId ? { toolCallId } : {}),
...(toolLifecycleStatus !== undefined ? { toolLifecycleStatus } : {}),
...(toolData !== undefined ? { toolData } : {}),
@@ -1203,11 +1221,14 @@ function deriveToolLifecycleCollapseKey(entry: DerivedWorkLogEntry): string | un
// progress ticks fold into it, the terminal row wins the label.
if (
entry.taskId &&
- (entry.activityKind === "task.progress" || entry.activityKind === "task.completed")
+ (entry.sourceActivityKind === "task.progress" || entry.sourceActivityKind === "task.completed")
) {
return `task${entry.taskId}`;
}
- if (entry.activityKind !== "tool.updated" && entry.activityKind !== "tool.completed") {
+ if (
+ entry.sourceActivityKind !== "tool.updated" &&
+ entry.sourceActivityKind !== "tool.completed"
+ ) {
return undefined;
}
if (entry.toolCallId) {