Skip to content

Commit b68cc2a

Browse files
committed
perf(server): scan threads once for message and activity events
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Apa4PipuLUNhbdABEvHXJB
1 parent a9b335c commit b68cc2a

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎apps/server/src/orchestration/projector.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,15 @@ function updateThread(
125125
patch: ThreadPatch,
126126
): ReadonlyArray<OrchestrationThread> {
127127
const index = threads.findIndex((thread) => thread.id === threadId);
128-
if (index === -1) return threads;
128+
return index === -1 ? threads : patchThreadAt(threads, index, patch);
129+
}
130+
131+
/** For callers that already located the thread and must not scan again. */
132+
function patchThreadAt(
133+
threads: ReadonlyArray<OrchestrationThread>,
134+
index: number,
135+
patch: ThreadPatch,
136+
): ReadonlyArray<OrchestrationThread> {
129137
const next = threads.slice();
130138
next[index] = { ...threads[index]!, ...patch };
131139
return next;
@@ -773,7 +781,8 @@ export function projectEvent(
773781
event.type,
774782
"payload",
775783
);
776-
const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
784+
const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
785+
const thread = nextBase.threads[threadIndex];
777786
if (!thread) {
778787
return nextBase;
779788
}
@@ -821,7 +830,7 @@ export function projectEvent(
821830

822831
return {
823832
...nextBase,
824-
threads: updateThread(nextBase.threads, payload.threadId, {
833+
threads: patchThreadAt(nextBase.threads, threadIndex, {
825834
messages: cappedMessages,
826835
updatedAt: event.occurredAt,
827836
}),
@@ -1061,7 +1070,8 @@ export function projectEvent(
10611070
"payload",
10621071
).pipe(
10631072
Effect.map((payload) => {
1064-
const thread = nextBase.threads.find((entry) => entry.id === payload.threadId);
1073+
const threadIndex = nextBase.threads.findIndex((entry) => entry.id === payload.threadId);
1074+
const thread = nextBase.threads[threadIndex];
10651075
if (!thread) {
10661076
return nextBase;
10671077
}
@@ -1075,7 +1085,7 @@ export function projectEvent(
10751085

10761086
return {
10771087
...nextBase,
1078-
threads: updateThread(nextBase.threads, payload.threadId, {
1088+
threads: patchThreadAt(nextBase.threads, threadIndex, {
10791089
activities,
10801090
updatedAt: event.occurredAt,
10811091
}),

0 commit comments

Comments
 (0)