The best way to control Claude Code, Codex and more from any computer or phone
perf(server): shell updates read a burst of threads once (#29101)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Andrei committed
Nov 3, 2026 at 09:42 UTC
e0513631af509202e49aa925d3859cb8d1de7a03
3 files changed
+161
-2
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+11
@@ -1641,6 +1641,17 @@ const assertProjectScopedShells = Effect.fn("assertProjectScopedShells")(functio
1641
all.archivedThreads.map((thread) => thread.id),
1642
[archivedThreadId],
1643
);
1644
+ const missingThreadId = ThreadId.make(`thread:${suffix}:missing`);
1645
+ const targeted = yield* projectionStore.getShellSnapshot({
1646
+ threadIds: [forkThreadId, archivedThreadId, missingThreadId],
1647
+ });
1648
+ for (const threadId of [forkThreadId, archivedThreadId, missingThreadId]) {
1649
+ assert.deepEqual(
1650
+ [...targeted.threads, ...targeted.archivedThreads].find((shell) => shell.id === threadId) ??
1651
+ null,
1652
+ yield* projectionStore.getThreadShell(threadId),
1653
+ );
1654
+ }
1655
return { projectId, activeThreadId, forkThreadId, otherThreadId };
1656
});
1657
apps/server/src/ws.test.ts
+125
@@ -425,6 +425,131 @@ it.effect("coalesces streaming replay before synchronization and the live tail",
425
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
426
);
427
428
+it.effect("shell groups read distinct threads once and keep each event's own sequence", () =>
429
+ Effect.gen(function* () {
430
+ const sql = yield* SqlClient.SqlClient;
431
+ const now = "2026-10-27T00:00:00.000Z";
432
+ const insert = (aggregateKind: "thread" | "project", streamId: string, version: number) =>
433
+ sql<{ sequence: number }>`
434
+ INSERT INTO orchestration_events (
435
+ event_id, aggregate_kind, stream_id, stream_version, event_type,
436
+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
437
+ ) VALUES (
438
+ ${`shell-group:${streamId}:${version}`}, ${aggregateKind}, ${streamId}, ${version},
439
+ ${aggregateKind === "thread" ? "provider-session.detached" : "project.meta-updated"},
440
+ ${now}, 'server', ${JSON.stringify(
441
+ aggregateKind === "thread"
442
+ ? { providerSessionId: "session:shell-group", detachedAt: now }
443
+ : { projectId: streamId, updatedAt: now },
444
+ )}, '{}', 2
445
+ ) RETURNING sequence
446
+ `.pipe(Effect.map(([row]) => row!.sequence));
447
+ const shell = (id: string, archivedAt: string | null = null) =>
448
+ ({ id: ThreadId.make(id), archivedAt }) as unknown as OrchestrationV2ThreadShell;
449
+ const project = {
450
+ id: ProjectId.make("project:group"),
451
+ } as unknown as OrchestrationProjectShell;
452
+
453
+ const after = yield* insert("project", "project:before", 0);
454
+ yield* insert("thread", "thread:active", 0);
455
+ const archived = yield* insert("thread", "thread:archived", 0);
456
+ const projectUpdated = yield* insert("project", project.id, 0);
457
+ const active = yield* insert("thread", "thread:active", 1);
458
+ const missing = yield* insert("thread", "thread:missing", 0);
459
+
460
+ const shellReads: Array<ReadonlyArray<string> | undefined> = [];
461
+ let batchFails = false;
462
+ const collect = (afterSequence: number) =>
463
+ subscribeOrchestrationV2Shell({ afterSequence, requestCompletionMarker: true }).pipe(
464
+ Effect.flatMap((stream) =>
465
+ stream.pipe(
466
+ Stream.takeUntil((item) => item.kind === "synchronized"),
467
+ Stream.runCollect,
468
+ ),
469
+ ),
470
+ Effect.provideService(ThreadManagement.ThreadManagementService, {
471
+ listActiveSubagentThreadIds: () => Effect.succeed([]),
472
+ getShellSnapshot: (options?: { readonly threadIds?: ReadonlyArray<string> }) =>
473
+ Effect.suspend(() => {
474
+ shellReads.push(options?.threadIds);
475
+ return batchFails
476
+ ? Effect.fail("batch failed")
477
+ : Effect.succeed({
478
+ schemaVersion: 1,
479
+ snapshotSequence: Number.MAX_SAFE_INTEGER,
480
+ threads: [shell("thread:active")],
481
+ archivedThreads: [shell("thread:archived", now)],
482
+ });
483
+ }),
484
+ getThreadShell: (threadId: string) =>
485
+ Effect.sync(() => {
486
+ shellReads.push([`scalar:${threadId}`]);
487
+ return threadId === "thread:missing"
488
+ ? null
489
+ : shell(threadId, threadId === "thread:archived" ? now : null);
490
+ }),
491
+ } as unknown as ThreadManagement.ThreadManagementService["Service"]),
492
+ Effect.provideService(ProjectStore.ProjectStoreV2, {
493
+ listShells: () => Effect.succeed([]),
494
+ } as unknown as ProjectStore.ProjectStoreV2["Service"]),
495
+ Effect.provideService(ProjectService.ProjectService, {
496
+ getShell: () => Effect.succeedSome(project),
497
+ } as unknown as ProjectService.ProjectService["Service"]),
498
+ Effect.provideServiceEffect(
499
+ ProjectEnrichment.ProjectEnrichmentService,
500
+ PubSub.unbounded().pipe(
501
+ Effect.map(
502
+ (changes) =>
503
+ ({
504
+ subscribeChanges: PubSub.subscribe(changes),
505
+ }) as unknown as ProjectEnrichment.ProjectEnrichmentService["Service"],
506
+ ),
507
+ ),
508
+ ),
509
+ );
510
+
511
+ const collectSummary = (afterSequence: number) =>
512
+ collect(afterSequence).pipe(
513
+ Effect.map((items) =>
514
+ items.flatMap((item): ReadonlyArray<ReadonlyArray<string | number>> =>
515
+ item.kind === "thread.updated"
516
+ ? [[item.kind, item.sequence, item.thread.id]]
517
+ : item.kind === "thread.removed"
518
+ ? [[item.kind, item.sequence, item.threadId]]
519
+ : item.kind === "project.updated"
520
+ ? [[item.kind, item.sequence, item.project.id]]
521
+ : [],
522
+ ),
523
+ ),
524
+ );
525
+ const expectedGroup = [
526
+ ["thread.removed", archived, "thread:archived"],
527
+ ["project.updated", projectUpdated, project.id],
528
+ ["thread.updated", active, "thread:active"],
529
+ ["thread.removed", missing, "thread:missing"],
530
+ ];
531
+
532
+ assert.deepEqual(yield* collectSummary(after), expectedGroup);
533
+ assert.deepEqual(shellReads, [["thread:archived", "thread:active", "thread:missing"]]);
534
+
535
+ shellReads.length = 0;
536
+ assert.deepEqual(yield* collectSummary(active), [
537
+ ["thread.removed", missing, "thread:missing"],
538
+ ]);
539
+ assert.deepEqual(shellReads, [["scalar:thread:missing"]]);
540
+
541
+ shellReads.length = 0;
542
+ batchFails = true;
543
+ assert.deepEqual(yield* collectSummary(after), expectedGroup);
544
+ assert.deepEqual(shellReads, [
545
+ ["thread:archived", "thread:active", "thread:missing"],
546
+ ["scalar:thread:archived"],
547
+ ["scalar:thread:active"],
548
+ ["scalar:thread:missing"],
549
+ ]);
550
+ }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
551
+);
552
+
553
it.effect("shell snapshots omit idle children and older settled threads only on request", () =>
554
Effect.gen(function* () {
555
const shell = (
apps/server/src/ws.ts
+25
-2
@@ -1119,14 +1119,37 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
1119
const projectShellItems = Effect.fn("ws.orchestrationV2.projectShellItems")(function* (
1120
events: ReadonlyArray<ShellApplicationEvent>,
1121
) {
1122
+ const coalesced = coalesceShellApplicationEvents(events);
1123
+ const threadIds = coalesced.flatMap((stored) =>
1124
+ "aggregateKind" in stored ? [] : [stored.event.threadId],
1125
+ );
1126
+ // A failed batch can't name its thread, so the scalar reads below report which one is bad.
1127
+ const batch =
1128
+ threadIds.length > 1
1129
+ ? yield* threadManagement
1130
+ .getShellSnapshot({ threadIds })
1131
+ .pipe(Effect.orElseSucceed(() => null))
1132
+ : null;
1133
+ const batchedShells =
1134
+ batch === null
1135
+ ? null
1136
+ : new Map(
1137
+ [...batch.threads, ...batch.archivedThreads].map(
1138
+ (shell) => [shell.id, shell] as const,
1139
+ ),
1140
+ );
1141
return yield* Effect.forEach(
1123
- coalesceShellApplicationEvents(events),
1142
+ coalesced,
1143
(stored) =>
1144
Effect.gen(function* () {
1145
if ("aggregateKind" in stored) {
1146
return yield* projectItem(stored);
1147
}
1129
- const shell = yield* threadManagement.getThreadShell(stored.event.threadId);
1148
+ const threadId = stored.event.threadId;
1149
+ const shell =
1150
+ batchedShells === null
1151
+ ? yield* threadManagement.getThreadShell(threadId)
1152
+ : (batchedShells.get(threadId) ?? null);
1153
return shellStreamItemFromThreadShell({ stored, shell });
1154
}),
1155
{ concurrency: 8 },