Skip to content
TypeScript

perf(web): long threads stream with 70% less work-row derivation (#29971)

Entrpiebfca09e91e9
diff --git a/packages/client-runtime/src/work-log/presentation.ts b/packages/client-runtime/src/work-log/presentation.ts
index 29c5f938b5b..9fa515e7939 100644
--- a/packages/client-runtime/src/work-log/presentation.ts
+++ b/packages/client-runtime/src/work-log/presentation.ts
@@ -291,12 +291,31 @@ function workEntryIndicatesToolFailureFromOutput(
return output.length > 0 && toolOutputIndicatesFailure(output);
}
+// Rows are re-derived on every timeline update but entries are reused while
+// their item is unchanged, so these per-entry checks are computed once.
+const entryChecks = new WeakMap<
+ WorkLogPresentationEntry,
+ { failure?: boolean; displayFailure?: boolean; action?: ToolGroupAction }
+>();
+
+function checksFor(entry: WorkLogPresentationEntry) {
+ let checks = entryChecks.get(entry);
+ if (!checks) {
+ checks = {};
+ entryChecks.set(entry, checks);
+ }
+ return checks;
+}
+
export function workEntryIndicatesToolFailure(entry: WorkLogPresentationEntry): boolean {
- return workEntryIndicatesToolFailureFromOutput(entry, true);
+ return (checksFor(entry).failure ??= workEntryIndicatesToolFailureFromOutput(entry, true));
}
export function workEntryDisplayIndicatesToolFailure(entry: WorkLogPresentationEntry): boolean {
- return workEntryIndicatesToolFailureFromOutput(entry, false);
+ return (checksFor(entry).displayFailure ??= workEntryIndicatesToolFailureFromOutput(
+ entry,
+ false,
+ ));
}
function workLogEntryIsLocalCodeSearch(entry: WorkLogPresentationEntry): boolean {
@@ -308,6 +327,10 @@ function workLogEntryIsLocalCodeSearch(entry: WorkLogPresentationEntry): boolean
}
export function toolGroupAction(entry: WorkLogPresentationEntry): ToolGroupAction {
+ return (checksFor(entry).action ??= computeToolGroupAction(entry));
+}
+
+function computeToolGroupAction(entry: WorkLogPresentationEntry): ToolGroupAction {
if (entry.itemType === "thread_created") return "thread-create";
const presentation = resolveWorkEntryToolPresentation(entry);
if (presentation?.action !== undefined) return presentation.action;
diff --git a/packages/shared/src/t3McpToolPresentation.ts b/packages/shared/src/t3McpToolPresentation.ts
index 864bcd8950d..7a6c2fab911 100644
--- a/packages/shared/src/t3McpToolPresentation.ts
+++ b/packages/shared/src/t3McpToolPresentation.ts
@@ -380,8 +380,24 @@ function normalizeT3McpToolLabel(value: string): string {
return value.replace(/\s+(?:complete|completed)\s*$/i, "").trim();
}
-// ACP tool prefixes vary; match loosely but require a known display-name entry.
+// Timelines resolve every work row's tool name on each update; names repeat across updates.
+// ACP titles can carry whole commands, so long labels are parsed but not kept.
+const toolNameCache = new Map<string, string | null>();
+const TOOL_NAME_CACHE_LIMIT = 10_000;
+const TOOL_NAME_CACHE_MAX_KEY_LENGTH = 256;
+
function resolveT3McpToolName(value: string): string | null {
+ if (value.length > TOOL_NAME_CACHE_MAX_KEY_LENGTH) return parseT3McpToolName(value);
+ const cached = toolNameCache.get(value);
+ if (cached !== undefined) return cached;
+ const name = parseT3McpToolName(value);
+ if (toolNameCache.size >= TOOL_NAME_CACHE_LIMIT) toolNameCache.clear();
+ toolNameCache.set(value, name);
+ return name;
+}
+
+// ACP tool prefixes vary; match loosely but require a known display-name entry.
+function parseT3McpToolName(value: string): string | null {
const label = normalizeT3McpToolLabel(value);
const mcpMatch = /^mcp__(?<server>.+?)__(?<tool>.+)$/i.exec(label);
if (mcpMatch?.groups) {