Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions apps/web/src/session-logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
61 changes: 41 additions & 20 deletions apps/web/src/session-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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>)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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 (
Expand All @@ -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;
Expand All @@ -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 } : {}),
Expand All @@ -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) {
Expand Down
Loading