The best way to control Claude Code, Codex and more from any computer or phone
perf(server): each live event is projected once for all subscribers (#30860)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Navid Nadali committed
Nov 6, 2026 at 04:40 UTC
de99330a3ba95bd0eb46eab396286e41ed1fd978
8 files changed
+197
-21
apps/server/src/guest/GuestShareService.ts
+2
-6
@@ -51,9 +51,9 @@ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
51
import { coalesceThreadLiveStream } from "../orchestration-v2/ThreadLiveEventCoalescer.ts";
52
import { buildBoundedThreadStreamSnapshot } from "../orchestration-v2/ThreadStream.ts";
53
import {
54
- projectDomainEventForWire,
54
projectTurnItemForDetail,
55
projectTurnItemForWire,
56
+ threadStreamEventItem,
57
} from "../orchestration-v2/WireProjection.ts";
58
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
59
import * as ServerSettings from "../serverSettings.ts";
@@ -550,11 +550,7 @@ const make = Effect.gen(function* () {
550
const live = orchestrator
551
.streamStoredEventsFrom({ threadId: thread.id, afterSequence: snapshot.snapshotSequence })
552
.pipe(
553
- Stream.map((stored) => ({
554
- kind: "event" as const,
555
- sequence: stored.sequence,
556
- event: projectDomainEventForWire(stored.event),
557
- })),
553
+ Stream.map(threadStreamEventItem),
554
// Ending the source here flushes what the coalescer holds, so `ended` is not held back.
555
Stream.takeUntil((item) => endsShare(item.event)),
556
coalesceThreadLiveStream,
apps/server/src/orchestration-v2/EventSink.ts
+2
-2
@@ -25,7 +25,7 @@ import * as SqlClient from "effect/sql/SqlClient";
25
26
import { replayAndBufferProjectedLiveEvents } from "./LiveStreamBudget.ts";
27
import type { UnsequencedProjectEvent } from "../persistence/OrchestrationEventStore.ts";
28
-import { projectDomainEventForWire } from "./WireProjection.ts";
28
+import { projectStoredEventForWire } from "./WireProjection.ts";
29
import { makeRunOutcomeRecorder, type RunOutcome } from "./RunMetrics.ts";
30
31
import * as CommandReceiptStore from "./CommandReceiptStore.ts";
@@ -863,7 +863,7 @@ const layerBase: Layer.Layer<
863
afterSequence,
864
filter: matches,
865
replay,
866
- project: (stored) => ({ ...stored, event: projectDomainEventForWire(stored.event) }),
866
+ project: projectStoredEventForWire,
867
});
868
}
869
const subscription = yield* PubSub.subscribe(pubsub);
apps/server/src/orchestration-v2/FoundationPersistence.test.ts
+23
-8
@@ -326,7 +326,7 @@ it.effect("paces scheduled compaction while preserving retention state and fixed
326
);
327
328
it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
329
- it.effect("projects oversized tool bodies before both replay and live RPC retention", () =>
329
+ it.effect("projects oversized tool bodies once before replay and live RPC retention", () =>
330
Effect.scoped(
331
Effect.gen(function* () {
332
const sink = yield* EventSink.EventSinkV2;
@@ -369,17 +369,25 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
369
},
370
],
371
});
372
- const pull = yield* Stream.toPull(
373
- sink.stream({
374
- threadId: thread.id,
375
- afterSequence: created!.sequence,
376
- bounded: true,
377
- }),
378
- );
372
+ const subscribe = () =>
373
+ Stream.toPull(
374
+ sink.stream({
375
+ threadId: thread.id,
376
+ afterSequence: created!.sequence,
377
+ bounded: true,
378
+ }),
379
+ );
380
+ const pull = yield* subscribe();
381
+ const other = yield* subscribe();
382
const replay = yield* pull;
383
assert.lengthOf(replay, 1);
384
+ assert.lengthOf(yield* other, 1);
385
assert.equal(replay[0]!.event.type, "turn-item.updated");
386
assert.isBelow(Buffer.byteLength(yield* encodeJson(replay)), 4_000);
387
+ const stringify = yield* Effect.acquireRelease(
388
+ Effect.sync(() => vi.spyOn(JSON, "stringify")),
389
+ (spy) => Effect.sync(() => spy.mockRestore()),
390
+ );
391
const [completed] = yield* sink.write({
392
events: [
393
{
@@ -392,10 +400,17 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
400
],
401
});
402
const live = yield* pull;
403
+ const otherLive = yield* other;
404
assert.deepEqual(
405
live.map((stored) => stored.sequence),
406
[completed!.sequence],
407
);
408
+ // Both subscribers get one shared projection, so the live budget sizes it once.
409
+ assert.strictEqual(otherLive[0], live[0]);
410
+ assert.lengthOf(
411
+ stringify.mock.calls.filter(([value]) => value === live[0] || value === otherLive[0]),
412
+ 1,
413
+ );
414
assert.isBelow(Buffer.byteLength(yield* encodeJson(live)), 4_000);
415
for (const stored of [...replay, ...live]) {
416
assert.equal(stored.event.type, "turn-item.updated");
apps/server/src/orchestration-v2/Orchestrator.ts
+1
@@ -486,6 +486,7 @@ export interface OrchestratorV2Shape {
486
threadId: ThreadId,
487
) => Effect.Effect<number, OrchestratorV2Error>;
488
readonly streamStoredEvents: Stream.Stream<OrchestrationV2StoredEvent, OrchestratorV2Error>;
489
+ /** Events arrive projected for the wire and shared by every subscriber; never mutate them. */
490
readonly streamStoredEventsFrom: (input?: {
491
readonly threadId?: ThreadId;
492
readonly afterSequence?: number;
apps/server/src/orchestration-v2/WireProjection.test.ts
+35
@@ -4,6 +4,7 @@ import {
4
CheckpointScopeId,
5
MessageId,
6
ContextHandoffId,
7
+ ProviderSessionId,
8
ProviderThreadId,
9
NodeId,
10
EventId,
@@ -13,6 +14,7 @@ import {
14
ThreadId,
15
TurnItemId,
16
type OrchestrationV2DomainEvent,
17
+ type OrchestrationV2StoredEvent,
18
type OrchestrationV2ThreadProjection,
19
type OrchestrationV2ThreadStreamItem,
20
OrchestrationV2TurnItem,
@@ -28,6 +30,7 @@ import {
30
projectTurnItemForWire,
31
projectTurnItemForDetail,
32
projectDomainEventForWire,
33
+ projectStoredEventForWire,
34
} from "./WireProjection.ts";
35
import { threadShellFromProjection } from "./ProjectionStore.ts";
36
import { MAX_TOOL_OUTPUT_IMAGES, toolOutputImages } from "@t3tools/shared/toolOutput";
@@ -338,6 +341,38 @@ describe("orchestration V2 wire projection", () => {
341
expect(event.payload.output).toBe(output);
342
expect(projectDomainEventForWire(projected)).toEqual(projected);
343
});
344
+
345
+ it("shares one wire projection per stored event", () => {
346
+ const tool: OrchestrationV2StoredEvent = {
347
+ sequence: 1,
348
+ commandId: null,
349
+ event: {
350
+ id: EventId.make("shared-tool-event"),
351
+ type: "turn-item.updated",
352
+ threadId: base.threadId,
353
+ occurredAt: base.updatedAt,
354
+ payload: { ...base, output: { content: [{ type: "text", text: "PRIVATE_BODY" }] } },
355
+ },
356
+ };
357
+ const projected = projectStoredEventForWire(tool);
358
+ expect(projected).toEqual({ ...tool, event: projectDomainEventForWire(tool.event) });
359
+ expect(projectStoredEventForWire(tool)).toBe(projected);
360
+ const detached: OrchestrationV2StoredEvent = {
361
+ sequence: 2,
362
+ commandId: null,
363
+ event: {
364
+ id: EventId.make("detached-event"),
365
+ type: "provider-session.detached",
366
+ threadId: base.threadId,
367
+ occurredAt: base.updatedAt,
368
+ payload: {
369
+ providerSessionId: ProviderSessionId.make("session"),
370
+ detachedAt: base.updatedAt,
371
+ },
372
+ },
373
+ };
374
+ expect(projectStoredEventForWire(detached)).toBe(detached);
375
+ });
376
});
377
378
describe("live body deduper", () => {
apps/server/src/orchestration-v2/WireProjection.ts
+30
@@ -5,6 +5,7 @@ import type {
5
OrchestrationV2Checkpoint,
6
OrchestrationV2DomainEvent,
7
OrchestrationV2ContextHandoff,
8
+ OrchestrationV2StoredEvent,
9
OrchestrationV2Subagent,
10
OrchestrationV2ThreadProjection,
11
OrchestrationV2ThreadStreamItem,
@@ -323,6 +324,35 @@ export function projectDomainEventForWire(
324
: event;
325
}
326
327
+// Keyed on the stored event PubSub hands every subscriber, so all of them share one projection
328
+// and LiveStreamBudget sizes it once. Consumers must not mutate the shared result.
329
+const projectedStoredEvents = new WeakMap<OrchestrationV2StoredEvent, OrchestrationV2StoredEvent>();
330
+
331
+export function projectStoredEventForWire(
332
+ stored: OrchestrationV2StoredEvent,
333
+): OrchestrationV2StoredEvent {
334
+ const cached = projectedStoredEvents.get(stored);
335
+ if (cached !== undefined) return cached;
336
+ const event = projectDomainEventForWire(stored.event);
337
+ if (event === stored.event) return stored;
338
+ const projected = { ...stored, event };
339
+ projectedStoredEvents.set(stored, projected);
340
+ return projected;
341
+}
342
+
343
+type ThreadStreamEvent = Extract<OrchestrationV2ThreadStreamItem, { readonly kind: "event" }>;
344
+
345
+const threadStreamEvents = new WeakMap<OrchestrationV2StoredEvent, ThreadStreamEvent>();
346
+
347
+/** For an event already projected for the wire; subscribers share the item and its cached size. */
348
+export function threadStreamEventItem(stored: OrchestrationV2StoredEvent): ThreadStreamEvent {
349
+ const cached = threadStreamEvents.get(stored);
350
+ if (cached !== undefined) return cached;
351
+ const item = { kind: "event" as const, sequence: stored.sequence, event: stored.event };
352
+ threadStreamEvents.set(stored, item);
353
+ return item;
354
+}
355
+
356
const LIVE_DEDUPE_MEMORY = 32;
357
358
function remember<K, V>(memory: Map<K, V>, key: K, value: V): void {
apps/server/src/ws.test.ts
+102
@@ -6,8 +6,11 @@ import {
6
AuthSessionId,
7
AuthSourceControlWriteScope,
8
EnvironmentId,
9
+ EventId,
10
+ MessageId,
11
ProjectId,
12
ThreadId,
13
+ TurnItemId,
14
ProviderDriverKind,
15
ProviderInstanceId,
16
SourceControlRepositoryError,
@@ -37,6 +40,7 @@ import { Headers } from "effect/http";
40
import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc";
41
import * as SqlClient from "effect/sql/SqlClient";
42
import * as Statement from "effect/sql/Statement";
43
+import { vi } from "vite-plus/test";
44
45
import * as AssetSigner from "./assets/AssetSigner.ts";
46
import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts";
@@ -556,6 +560,104 @@ it.effect("shell groups read distinct threads once and keep each event's own seq
560
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
561
);
562
563
+it.effect("shares each live event item across subscribers and dedupes bodies per subscriber", () =>
564
+ Effect.gen(function* () {
565
+ const threadId = ThreadId.make("thread:shared-live");
566
+ const messageId = MessageId.make("message:shared-live");
567
+ const at = DateTime.makeUnsafe("2026-10-31T00:00:00.000Z");
568
+ const owner = { threadId, runId: null, nodeId: null, createdAt: at, updatedAt: at };
569
+ // DateTime memoizes its formatted parts, so only the plain records are frozen.
570
+ const freeze = <A>(value: A): A => {
571
+ if (typeof value === "object" && value !== null && !DateTime.isDateTime(value)) {
572
+ for (const child of Object.values(value)) freeze(child);
573
+ Object.freeze(value);
574
+ }
575
+ return value;
576
+ };
577
+ const live: ReadonlyArray<OrchestrationV2StoredEvent> = freeze([
578
+ {
579
+ sequence: 1,
580
+ commandId: null,
581
+ event: {
582
+ id: EventId.make("event:shared-live:message"),
583
+ type: "message.updated",
584
+ threadId,
585
+ occurredAt: at,
586
+ payload: {
587
+ ...owner,
588
+ id: messageId,
589
+ role: "assistant",
590
+ text: "Done",
591
+ attachments: [],
592
+ streaming: false,
593
+ createdBy: "agent",
594
+ creationSource: "provider",
595
+ },
596
+ },
597
+ },
598
+ {
599
+ sequence: 2,
600
+ commandId: null,
601
+ event: {
602
+ id: EventId.make("event:shared-live:reply"),
603
+ type: "turn-item.updated",
604
+ threadId,
605
+ occurredAt: at,
606
+ payload: {
607
+ ...owner,
608
+ id: TurnItemId.make("item:shared-live:reply"),
609
+ type: "assistant_message",
610
+ providerThreadId: null,
611
+ providerTurnId: null,
612
+ nativeItemRef: null,
613
+ parentItemId: null,
614
+ ordinal: 1,
615
+ title: null,
616
+ status: "completed",
617
+ messageId,
618
+ text: "Done",
619
+ streaming: false,
620
+ startedAt: at,
621
+ completedAt: at,
622
+ },
623
+ },
624
+ },
625
+ ] as ReadonlyArray<OrchestrationV2StoredEvent>);
626
+ const stringify = yield* Effect.acquireRelease(
627
+ Effect.sync(() => vi.spyOn(JSON, "stringify")),
628
+ (spy) => Effect.sync(() => spy.mockRestore()),
629
+ );
630
+ // The EventSink hands every subscriber the same projected objects.
631
+ const subscribe = (acceptDedupedBodies: boolean) =>
632
+ subscribeOrchestrationV2Thread({ threadId, afterSequence: 0, acceptDedupedBodies }).pipe(
633
+ Effect.flatMap(Stream.runCollect),
634
+ Effect.map((items) => Array.from(items)),
635
+ Effect.provideService(ThreadManagement.ThreadManagementService, {
636
+ ensureLegacyTranscript: () => Effect.void,
637
+ streamStoredEventsFrom: () => Stream.fromIterable(live),
638
+ } as unknown as ThreadManagement.ThreadManagementService["Service"]),
639
+ );
640
+ const [deduped, first, second] = yield* Effect.forEach([true, false, false], subscribe);
641
+
642
+ assert.lengthOf(first!, live.length);
643
+ first!.forEach((item, index) => {
644
+ assert.strictEqual(item, second![index]);
645
+ assert.strictEqual(item.kind === "event" && item.event, live[index]!.event);
646
+ });
647
+ assert.strictEqual(deduped![0], first![0]);
648
+ const reply = deduped![1];
649
+ assert.deepInclude(reply?.kind === "event" ? reply.event.payload : undefined, {
650
+ text: "",
651
+ textOmitted: true,
652
+ });
653
+ // The live buffer sizes each shared item once, not once per subscriber.
654
+ assert.lengthOf(
655
+ stringify.mock.calls.filter(([value]) => value?.kind === "event"),
656
+ live.length,
657
+ );
658
+ }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
659
+);
660
+
661
it.effect("shell snapshots omit idle children and older settled threads only on request", () =>
662
Effect.gen(function* () {
663
const shell = (
apps/server/src/ws.ts
+2
-5
@@ -165,6 +165,7 @@ import {
165
makeLiveBodyDeduper,
166
projectDomainEventForWire,
167
projectThreadProjectionForWire,
168
+ threadStreamEventItem,
169
} from "./orchestration-v2/WireProjection.ts";
170
import { dedupeSnapshotBodies } from "@t3tools/shared/orchestrationV2DedupedBodies";
171
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
@@ -917,11 +918,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
918
afterSequence,
919
})
920
.pipe(
920
- Stream.map((stored) => ({
921
- kind: "event" as const,
922
- sequence: stored.sequence,
923
- event: stored.event,
924
- })),
921
+ Stream.map(threadStreamEventItem),
922
coalesceThreadLiveStream,
923
Stream.mapError(
924
(cause) =>