perf(server): stop remapping every thread on each thread event (#13720)

Co-authored-by: Claude <[email protected]>
This commit is contained in:
Theo BrowneandClaude authored and GitHub committed 2026-09-25 19:11:58 -07:00
1 parent 2a9832b801
commit c216ba4dad
1 file changed
+22 -6
+22 -6
View File
@@ -117,12 +117,26 @@ function settledTurnStateForSessionStatus(
}
}
// Runs for every thread event (including streaming deltas) against every
// thread the server has ever seen, so copy the array rather than map it.
function updateThread(
threads: ReadonlyArray<OrchestrationThread>,
threadId: ThreadId,
patch: ThreadPatch,
): OrchestrationThread[] {
return threads.map((thread) => (thread.id === threadId ? { ...thread, ...patch } : thread));
): ReadonlyArray<OrchestrationThread> {
const index = threads.findIndex((thread) => thread.id === threadId);
return index === -1 ? threads : patchThreadAt(threads, index, patch);
}
/** For callers that already located the thread and must not scan again. */
function patchThreadAt(
threads: ReadonlyArray<OrchestrationThread>,
index: number,
patch: ThreadPatch,
): ReadonlyArray<OrchestrationThread> {
const next = threads.slice();
next[index] = { ...threads[index]!, ...patch };
return next;
}
/** Patch that swaps a thread's links and re-derives the legacy single-PR field from them. */
@@ -767,7 +781,8 @@ export function projectEvent(
event.type,
"payload",
);
const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
const thread = nextBase.threads[threadIndex];
if (!thread) {
return nextBase;
}
@@ -815,7 +830,7 @@ export function projectEvent(
return {
...nextBase,
threads: updateThread(nextBase.threads, payload.threadId, {
threads: patchThreadAt(nextBase.threads, threadIndex, {
messages: cappedMessages,
updatedAt: event.occurredAt,
}),
@@ -1055,7 +1070,8 @@ export function projectEvent(
"payload",
).pipe(
Effect.map((payload) => {
const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
const thread = nextBase.threads[threadIndex];
if (!thread) {
return nextBase;
}
@@ -1069,7 +1085,7 @@ export function projectEvent(
return {
...nextBase,
threads: updateThread(nextBase.threads, payload.threadId, {
threads: patchThreadAt(nextBase.threads, threadIndex, {
activities,
updatedAt: event.occurredAt,
}),