@devries-cameron20 / t3code-1 / commits / 8e49f230d8
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): the thread lineage panel compiles without manual memoization (#20720)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 21:54 UTC 8e49f230d8a6267e2ccea9c72f358096d605fa53
1 file changed +72 -60
apps/web/src/components/chat/ThreadRelationshipsControl.tsx
+72 -60
@@ -30,6 +30,7 @@ import {
30 NodeId,
31 type EnvironmentId,
32 type OrchestrationV2Subagent,
33 + type OrchestrationV2ThreadProjection,
34 type OrchestrationV2ThreadShell,
35 type ThreadId,
36 } from "@t3tools/contracts";
@@ -48,7 +49,7 @@ import {
49 SquareIcon,
50 UnplugIcon,
51 } from "lucide-react";
51 -import { useMemo, useState, type ReactNode } from "react";
52 +import { useState, type ReactNode } from "react";
53
54 import { useArchivedThreadSnapshots } from "../../lib/archivedThreadsState";
55 import { buildThreadRouteParams } from "../../threadRoutes";
@@ -204,6 +205,54 @@ function currentSubagent<Agent extends RuntimeSubagent>(
205 };
206 }
207
208 +// These lookups and the graph walk every subagent and thread; the panel calls
209 +// several hooks after them, which would stop the compiler caching them inline.
210 +function useSubagentLookups(subagents: ReadonlyArray<OrchestrationV2Subagent> | undefined) {
211 + "use memo";
212 + const projected = subagents ?? [];
213 + const runtimeSubagents = projectedSubagentsToRuntime(projected);
214 + const byId = new Map(runtimeSubagents.map((agent) => [agent.id, agent]));
215 + const subagentsByThreadId = new Map(
216 + projected
217 + .filter((subagent) => subagent.childThreadId !== null)
218 + .map((subagent) => [
219 + subagent.childThreadId,
220 + {
221 + ...byId.get(subagent.id)!,
222 + id: subagent.id,
223 + threadId: subagent.threadId,
224 + runId: subagent.runId,
225 + nativeTaskRef: subagent.nativeTaskRef,
226 + nativeStatus: subagent.status,
227 + driver: subagent.driver,
228 + providerInstanceId: subagent.providerInstanceId,
229 + origin: subagent.origin,
230 + modelSelection: subagent.modelSelection,
231 + },
232 + ]),
233 + );
234 + const workflowGroupsById = new Map(
235 + deriveWorkflowGroups(runtimeSubagents).map((group) => [group.workflow.id, group]),
236 + );
237 + return { subagentsByThreadId, workflowGroupsById };
238 +}
239 +
240 +function useThreadRelationshipGraph(
241 + threadShells: ReturnType<typeof useThreadShells>,
242 + archivedShells: ReadonlyArray<OrchestrationV2ThreadShell> | undefined,
243 + projection: OrchestrationV2ThreadProjection | null,
244 + environmentId: EnvironmentId,
245 +) {
246 + "use memo";
247 + const shells: ReadonlyArray<OrchestrationV2ThreadShell> = [
248 + ...threadShells
249 + .filter((thread) => thread.environmentId === environmentId)
250 + .map((thread) => thread.source),
251 + ...(archivedShells ?? []),
252 + ];
253 + return deriveThreadRelationshipGraph({ threads: shells, projection });
254 +}
255 +
256 export function ThreadRelationshipsPanel(props: {
257 readonly environmentId: EnvironmentId;
258 readonly threadId: ThreadId;
@@ -211,52 +260,19 @@ export function ThreadRelationshipsPanel(props: {
260 const ref = scopeThreadRef(props.environmentId, props.threadId);
261 const projection = useThreadProjection(ref)?.projection ?? null;
262 const providers = useServerConfigs().get(props.environmentId)?.providers;
214 - const runtimeSubagents = useMemo(
215 - () => projectedSubagentsToRuntime(projection?.subagents ?? []),
216 - [projection?.subagents],
217 - );
218 - const subagentsByThreadId = useMemo(() => {
219 - const byId = new Map(runtimeSubagents.map((agent) => [agent.id, agent]));
220 - return new Map(
221 - (projection?.subagents ?? [])
222 - .filter((subagent) => subagent.childThreadId !== null)
223 - .map((subagent) => [
224 - subagent.childThreadId,
225 - {
226 - ...byId.get(subagent.id)!,
227 - id: subagent.id,
228 - threadId: subagent.threadId,
229 - runId: subagent.runId,
230 - nativeTaskRef: subagent.nativeTaskRef,
231 - nativeStatus: subagent.status,
232 - driver: subagent.driver,
233 - providerInstanceId: subagent.providerInstanceId,
234 - origin: subagent.origin,
235 - modelSelection: subagent.modelSelection,
236 - },
237 - ]),
238 - );
239 - }, [projection?.subagents, runtimeSubagents]);
240 - const workflowGroupsById = useMemo(
241 - () =>
242 - new Map(deriveWorkflowGroups(runtimeSubagents).map((group) => [group.workflow.id, group])),
243 - [runtimeSubagents],
244 - );
263 + const { subagentsByThreadId, workflowGroupsById } = useSubagentLookups(projection?.subagents);
264 const threadShells = useThreadShells();
265 const projects = useProjects().filter((project) => project.environmentId === props.environmentId);
266 const archived = useArchivedThreadSnapshots([props.environmentId]);
267 const archivedShells = archived.snapshots.find(
268 (entry) => entry.environmentId === props.environmentId,
269 )?.snapshot.threads;
251 - const graph = useMemo(() => {
252 - const shells: ReadonlyArray<OrchestrationV2ThreadShell> = [
253 - ...threadShells
254 - .filter((thread) => thread.environmentId === props.environmentId)
255 - .map((thread) => thread.source),
256 - ...(archivedShells ?? []),
257 - ];
258 - return deriveThreadRelationshipGraph({ threads: shells, projection });
259 - }, [archivedShells, projection, props.environmentId, threadShells]);
270 + const graph = useThreadRelationshipGraph(
271 + threadShells,
272 + archivedShells,
273 + projection,
274 + props.environmentId,
275 + );
276 const currentThread = projection?.thread ?? graph.nodes.get(props.threadId)?.thread;
277 const currentProject = projects.find((project) => project.id === currentThread?.projectId);
278 const navigate = useNavigate();
@@ -271,25 +287,21 @@ export function ThreadRelationshipsPanel(props: {
287 const [stoppingId, setStoppingId] = useState<string | null>(null);
288 const latestMergeBackRun = projection === null ? null : resolveLatestMergeBackRun(projection);
289 const mergeTargetThreadId = resolveMergeBackTargetThreadId(projection);
274 - const relationshipRows = useMemo(
275 - () =>
276 - orderWebThreadLineageRows({
277 - graph,
278 - rows: immediateThreadRelationships(graph, props.threadId),
279 - currentThreadId: props.threadId,
280 - mergeTargetThreadId,
281 - }).map((row) => {
282 - const agent =
283 - row.edge.kind === "subagent" && !isParentThreadRelationship(row.edge, props.threadId)
284 - ? subagentsByThreadId.get(row.threadId)
285 - : undefined;
286 - const workflowGroup = agent === undefined ? undefined : workflowGroupsById.get(agent.id);
287 - return workflowGroup === undefined
288 - ? row
289 - : { ...row, edge: { ...row.edge, status: workflowGroup.workflow.status } };
290 - }),
291 - [graph, mergeTargetThreadId, props.threadId, subagentsByThreadId, workflowGroupsById],
292 - );
290 + const relationshipRows = orderWebThreadLineageRows({
291 + graph,
292 + rows: immediateThreadRelationships(graph, props.threadId),
293 + currentThreadId: props.threadId,
294 + mergeTargetThreadId,
295 + }).map((row) => {
296 + const agent =
297 + row.edge.kind === "subagent" && !isParentThreadRelationship(row.edge, props.threadId)
298 + ? subagentsByThreadId.get(row.threadId)
299 + : undefined;
300 + const workflowGroup = agent === undefined ? undefined : workflowGroupsById.get(agent.id);
301 + return workflowGroup === undefined
302 + ? row
303 + : { ...row, edge: { ...row.edge, status: workflowGroup.workflow.status } };
304 + });
305 const canMerge = mergeTargetThreadId !== null && latestMergeBackRun !== null;
306 const canDetach = projection ? canDetachThreadProviderSession(projection) : false;
307