Skip to content

Commit e17e31f

Browse files
Yuruzuuclaude
andcommitted
perf(server): shell updates read a burst of threads once
Each coalesced shell event group read every thread with its own getThreadShell transaction. A group touching N threads repeated about four SELECTs per thread, per subscriber, and fork siblings reread shared ancestors. Groups with more than one distinct thread now make one targeted getShellSnapshot read. Groups with one thread keep the scalar read, which is one SELECT cheaper. Every item still carries its own stored event sequence, never the snapshot's global sequence. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c65f8d5 commit e17e31f

3 files changed

Lines changed: 157 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,17 @@ const assertProjectScopedShells = Effect.fn("assertProjectScopedShells")(functio
13741374
all.archivedThreads.map((thread) => thread.id),
13751375
[archivedThreadId],
13761376
);
1377+
const missingThreadId = ThreadId.make(`thread:${suffix}:missing`);
1378+
const targeted = yield* projectionStore.getShellSnapshot({
1379+
threadIds: [forkThreadId, archivedThreadId, missingThreadId],
1380+
});
1381+
for (const threadId of [forkThreadId, archivedThreadId, missingThreadId]) {
1382+
assert.deepEqual(
1383+
[...targeted.threads, ...targeted.archivedThreads].find((shell) => shell.id === threadId) ??
1384+
null,
1385+
yield* projectionStore.getThreadShell(threadId),
1386+
);
1387+
}
13771388
return { projectId, activeThreadId, forkThreadId, otherThreadId };
13781389
});
13791390

‎apps/server/src/ws.test.ts‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,127 @@ it.effect("coalesces streaming replay before synchronization and the live tail",
421421
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
422422
);
423423

424+
it.effect("shell groups read distinct threads once and keep each event's own sequence", () =>
425+
Effect.gen(function* () {
426+
const sql = yield* SqlClient.SqlClient;
427+
const now = "2026-10-27T00:00:00.000Z";
428+
const insert = (aggregateKind: "thread" | "project", streamId: string, version: number) =>
429+
sql<{ sequence: number }>`
430+
INSERT INTO orchestration_events (
431+
event_id, aggregate_kind, stream_id, stream_version, event_type,
432+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
433+
) VALUES (
434+
${`shell-group:${streamId}:${version}`}, ${aggregateKind}, ${streamId}, ${version},
435+
${aggregateKind === "thread" ? "provider-session.detached" : "project.meta-updated"},
436+
${now}, 'server', ${JSON.stringify(
437+
aggregateKind === "thread"
438+
? { providerSessionId: "session:shell-group", detachedAt: now }
439+
: { projectId: streamId, updatedAt: now },
440+
)}, '{}', 2
441+
) RETURNING sequence
442+
`.pipe(Effect.map(([row]) => row!.sequence));
443+
const shell = (id: string, archivedAt: string | null = null) =>
444+
({ id: ThreadId.make(id), archivedAt }) as unknown as OrchestrationV2ThreadShell;
445+
const project = {
446+
id: ProjectId.make("project:group"),
447+
} as unknown as OrchestrationProjectShell;
448+
449+
const after = yield* insert("project", "project:before", 0);
450+
yield* insert("thread", "thread:active", 0);
451+
const archived = yield* insert("thread", "thread:archived", 0);
452+
const projectUpdated = yield* insert("project", project.id, 0);
453+
const active = yield* insert("thread", "thread:active", 1);
454+
const missing = yield* insert("thread", "thread:missing", 0);
455+
456+
const shellReads: Array<ReadonlyArray<string> | undefined> = [];
457+
let batchFails = false;
458+
const collect = (afterSequence: number) =>
459+
subscribeOrchestrationV2Shell({ afterSequence, requestCompletionMarker: true }).pipe(
460+
Effect.flatMap((stream) =>
461+
stream.pipe(
462+
Stream.takeUntil((item) => item.kind === "synchronized"),
463+
Stream.runCollect,
464+
),
465+
),
466+
Effect.provideService(ThreadManagement.ThreadManagementService, {
467+
listActiveSubagentThreadIds: () => Effect.succeed([]),
468+
getShellSnapshot: (options?: { readonly threadIds?: ReadonlyArray<string> }) =>
469+
Effect.suspend(() => {
470+
shellReads.push(options?.threadIds);
471+
return batchFails
472+
? Effect.fail("batch failed")
473+
: Effect.succeed({
474+
schemaVersion: 1,
475+
snapshotSequence: Number.MAX_SAFE_INTEGER,
476+
threads: [shell("thread:active")],
477+
archivedThreads: [shell("thread:archived", now)],
478+
});
479+
}),
480+
getThreadShell: (threadId: string) =>
481+
Effect.sync(() => {
482+
shellReads.push([`scalar:${threadId}`]);
483+
return shell(threadId);
484+
}),
485+
} as unknown as ThreadManagement.ThreadManagementService["Service"]),
486+
Effect.provideService(ProjectStore.ProjectStoreV2, {
487+
listShells: () => Effect.succeed([]),
488+
} as unknown as ProjectStore.ProjectStoreV2["Service"]),
489+
Effect.provideService(ProjectService.ProjectService, {
490+
getShell: () => Effect.succeedSome(project),
491+
} as unknown as ProjectService.ProjectService["Service"]),
492+
Effect.provideServiceEffect(
493+
ProjectEnrichment.ProjectEnrichmentService,
494+
PubSub.unbounded().pipe(
495+
Effect.map(
496+
(changes) =>
497+
({
498+
subscribeChanges: PubSub.subscribe(changes),
499+
}) as unknown as ProjectEnrichment.ProjectEnrichmentService["Service"],
500+
),
501+
),
502+
),
503+
);
504+
505+
const grouped = yield* collect(after);
506+
assert.deepEqual(shellReads, [["thread:archived", "thread:active", "thread:missing"]]);
507+
assert.deepEqual(
508+
grouped.flatMap((item): ReadonlyArray<ReadonlyArray<string | number>> =>
509+
item.kind === "thread.updated"
510+
? [[item.kind, item.sequence, item.thread.id]]
511+
: item.kind === "thread.removed"
512+
? [[item.kind, item.sequence, item.threadId]]
513+
: item.kind === "project.updated"
514+
? [[item.kind, item.sequence, item.project.id]]
515+
: [],
516+
),
517+
[
518+
["thread.removed", archived, "thread:archived"],
519+
["project.updated", projectUpdated, project.id],
520+
["thread.updated", active, "thread:active"],
521+
["thread.removed", missing, "thread:missing"],
522+
],
523+
);
524+
525+
shellReads.length = 0;
526+
const singleton = yield* collect(active);
527+
assert.deepEqual(shellReads, [["scalar:thread:missing"]]);
528+
assert.deepEqual(
529+
singleton.flatMap((item) => (item.kind === "thread.updated" ? [item.sequence] : [])),
530+
[missing],
531+
);
532+
533+
shellReads.length = 0;
534+
batchFails = true;
535+
yield* collect(after);
536+
assert.deepEqual(shellReads, [
537+
["thread:archived", "thread:active", "thread:missing"],
538+
["scalar:thread:archived"],
539+
["scalar:thread:active"],
540+
["scalar:thread:missing"],
541+
]);
542+
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
543+
);
544+
424545
it.effect("shell snapshots omit idle children and older settled threads only on request", () =>
425546
Effect.gen(function* () {
426547
const shell = (

‎apps/server/src/ws.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,14 +1104,37 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
11041104
const projectShellItems = Effect.fn("ws.orchestrationV2.projectShellItems")(function* (
11051105
events: ReadonlyArray<ShellApplicationEvent>,
11061106
) {
1107+
const coalesced = coalesceShellApplicationEvents(events);
1108+
const threadIds = coalesced.flatMap((stored) =>
1109+
"aggregateKind" in stored ? [] : [stored.event.threadId],
1110+
);
1111+
// A failed batch can't name its thread, so the scalar reads below report which one is bad.
1112+
const batch =
1113+
threadIds.length > 1
1114+
? yield* threadManagement
1115+
.getShellSnapshot({ threadIds })
1116+
.pipe(Effect.orElseSucceed(() => null))
1117+
: null;
1118+
const batchedShells =
1119+
batch === null
1120+
? null
1121+
: new Map(
1122+
[...batch.threads, ...batch.archivedThreads].map(
1123+
(shell) => [shell.id, shell] as const,
1124+
),
1125+
);
11071126
return yield* Effect.forEach(
1108-
coalesceShellApplicationEvents(events),
1127+
coalesced,
11091128
(stored) =>
11101129
Effect.gen(function* () {
11111130
if ("aggregateKind" in stored) {
11121131
return yield* projectItem(stored);
11131132
}
1114-
const shell = yield* threadManagement.getThreadShell(stored.event.threadId);
1133+
const threadId = stored.event.threadId;
1134+
const shell =
1135+
batchedShells === null
1136+
? yield* threadManagement.getThreadShell(threadId)
1137+
: (batchedShells.get(threadId) ?? null);
11151138
return shellStreamItemFromThreadShell({ stored, shell });
11161139
}),
11171140
{ concurrency: 8 },

0 commit comments

Comments
 (0)