Skip to content

Commit a814490

Browse files
navidnadaliclaude
andcommitted
perf(server): each live event is projected once for all subscribers
Keep one wire projection and one thread-stream item per stored event, so every subscriber of the event sink, the thread stream and the guest share stream receives the same object and the live-stream budget sizes it once. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 8b44f42 commit a814490

8 files changed

Lines changed: 197 additions & 21 deletions

File tree

‎apps/server/src/guest/GuestShareService.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
5656
import { coalesceThreadLiveStream } from "../orchestration-v2/ThreadLiveEventCoalescer.ts";
5757
import { buildBoundedThreadStreamSnapshot } from "../orchestration-v2/ThreadStream.ts";
5858
import {
59-
projectDomainEventForWire,
6059
projectTurnItemForDetail,
6160
projectTurnItemForWire,
61+
threadStreamEventItem,
6262
} from "../orchestration-v2/WireProjection.ts";
6363
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
6464
import * as ServerSettings from "../serverSettings.ts";
@@ -557,11 +557,7 @@ const make = Effect.gen(function* () {
557557
const live = orchestrator
558558
.streamStoredEventsFrom({ threadId: thread.id, afterSequence: snapshot.snapshotSequence })
559559
.pipe(
560-
Stream.map((stored) => ({
561-
kind: "event" as const,
562-
sequence: stored.sequence,
563-
event: projectDomainEventForWire(stored.event),
564-
})),
560+
Stream.map(threadStreamEventItem),
565561
// Ending the source here flushes what the coalescer holds, so `ended` is not held back.
566562
Stream.takeUntil((item) => endsShare(item.event)),
567563
coalesceThreadLiveStream,

‎apps/server/src/orchestration-v2/EventSink.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import * as SqlClient from "effect/sql/SqlClient";
2525

2626
import { replayAndBufferProjectedLiveEvents } from "./LiveStreamBudget.ts";
2727
import type { UnsequencedProjectEvent } from "../persistence/OrchestrationEventStore.ts";
28-
import { projectDomainEventForWire } from "./WireProjection.ts";
28+
import { projectStoredEventForWire } from "./WireProjection.ts";
2929
import { makeRunOutcomeRecorder, type RunOutcome } from "./RunMetrics.ts";
3030

3131
import * as CommandReceiptStore from "./CommandReceiptStore.ts";
@@ -863,7 +863,7 @@ const layerBase: Layer.Layer<
863863
afterSequence,
864864
filter: matches,
865865
replay,
866-
project: (stored) => ({ ...stored, event: projectDomainEventForWire(stored.event) }),
866+
project: projectStoredEventForWire,
867867
});
868868
}
869869
const subscription = yield* PubSub.subscribe(pubsub);

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ it.effect("paces scheduled compaction while preserving retention state and fixed
326326
);
327327

328328
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", () =>
330330
Effect.scoped(
331331
Effect.gen(function* () {
332332
const sink = yield* EventSink.EventSinkV2;
@@ -369,17 +369,25 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
369369
},
370370
],
371371
});
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();
379382
const replay = yield* pull;
380383
assert.lengthOf(replay, 1);
384+
assert.lengthOf(yield* other, 1);
381385
assert.equal(replay[0]!.event.type, "turn-item.updated");
382386
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+
);
383391
const [completed] = yield* sink.write({
384392
events: [
385393
{
@@ -392,10 +400,17 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
392400
],
393401
});
394402
const live = yield* pull;
403+
const otherLive = yield* other;
395404
assert.deepEqual(
396405
live.map((stored) => stored.sequence),
397406
[completed!.sequence],
398407
);
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+
);
399414
assert.isBelow(Buffer.byteLength(yield* encodeJson(live)), 4_000);
400415
for (const stored of [...replay, ...live]) {
401416
assert.equal(stored.event.type, "turn-item.updated");

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ export interface OrchestratorV2Shape {
481481
threadId: ThreadId,
482482
) => Effect.Effect<number, OrchestratorV2Error>;
483483
readonly streamStoredEvents: Stream.Stream<OrchestrationV2StoredEvent, OrchestratorV2Error>;
484+
/** Events arrive projected for the wire and shared by every subscriber; never mutate them. */
484485
readonly streamStoredEventsFrom: (input?: {
485486
readonly threadId?: ThreadId;
486487
readonly afterSequence?: number;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
CheckpointScopeId,
55
MessageId,
66
ContextHandoffId,
7+
ProviderSessionId,
78
ProviderThreadId,
89
NodeId,
910
EventId,
@@ -13,6 +14,7 @@ import {
1314
ThreadId,
1415
TurnItemId,
1516
type OrchestrationV2DomainEvent,
17+
type OrchestrationV2StoredEvent,
1618
type OrchestrationV2ThreadProjection,
1719
type OrchestrationV2ThreadStreamItem,
1820
OrchestrationV2TurnItem,
@@ -28,6 +30,7 @@ import {
2830
projectTurnItemForWire,
2931
projectTurnItemForDetail,
3032
projectDomainEventForWire,
33+
projectStoredEventForWire,
3134
} from "./WireProjection.ts";
3235
import { threadShellFromProjection } from "./ProjectionStore.ts";
3336
import { MAX_TOOL_OUTPUT_IMAGES, toolOutputImages } from "@t3tools/shared/toolOutput";
@@ -338,6 +341,38 @@ describe("orchestration V2 wire projection", () => {
338341
expect(event.payload.output).toBe(output);
339342
expect(projectDomainEventForWire(projected)).toEqual(projected);
340343
});
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+
});
341376
});
342377

343378
describe("live body deduper", () => {

‎apps/server/src/orchestration-v2/WireProjection.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
OrchestrationV2Checkpoint,
66
OrchestrationV2DomainEvent,
77
OrchestrationV2ContextHandoff,
8+
OrchestrationV2StoredEvent,
89
OrchestrationV2Subagent,
910
OrchestrationV2ThreadProjection,
1011
OrchestrationV2ThreadStreamItem,
@@ -323,6 +324,35 @@ export function projectDomainEventForWire(
323324
: event;
324325
}
325326

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+
326356
const LIVE_DEDUPE_MEMORY = 32;
327357

328358
function remember<K, V>(memory: Map<K, V>, key: K, value: V): void {

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { assert, it } from "@effect/vitest";
55
import {
66
AuthSessionId,
77
EnvironmentId,
8+
EventId,
9+
MessageId,
810
ProjectId,
911
ThreadId,
12+
TurnItemId,
1013
ProviderDriverKind,
1114
ProviderInstanceId,
1215
WS_METHODS,
@@ -34,6 +37,7 @@ import { Headers } from "effect/http";
3437
import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc";
3538
import * as SqlClient from "effect/sql/SqlClient";
3639
import * as Statement from "effect/sql/Statement";
40+
import { vi } from "vite-plus/test";
3741

3842
import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts";
3943
import * as ServerBrowser from "./preview/ServerBrowser.ts";
@@ -550,6 +554,104 @@ it.effect("shell groups read distinct threads once and keep each event's own seq
550554
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
551555
);
552556

557+
it.effect("shares each live event item across subscribers and dedupes bodies per subscriber", () =>
558+
Effect.gen(function* () {
559+
const threadId = ThreadId.make("thread:shared-live");
560+
const messageId = MessageId.make("message:shared-live");
561+
const at = DateTime.makeUnsafe("2026-10-31T00:00:00.000Z");
562+
const owner = { threadId, runId: null, nodeId: null, createdAt: at, updatedAt: at };
563+
// DateTime memoizes its formatted parts, so only the plain records are frozen.
564+
const freeze = <A>(value: A): A => {
565+
if (typeof value === "object" && value !== null && !DateTime.isDateTime(value)) {
566+
for (const child of Object.values(value)) freeze(child);
567+
Object.freeze(value);
568+
}
569+
return value;
570+
};
571+
const live: ReadonlyArray<OrchestrationV2StoredEvent> = freeze([
572+
{
573+
sequence: 1,
574+
commandId: null,
575+
event: {
576+
id: EventId.make("event:shared-live:message"),
577+
type: "message.updated",
578+
threadId,
579+
occurredAt: at,
580+
payload: {
581+
...owner,
582+
id: messageId,
583+
role: "assistant",
584+
text: "Done",
585+
attachments: [],
586+
streaming: false,
587+
createdBy: "agent",
588+
creationSource: "provider",
589+
},
590+
},
591+
},
592+
{
593+
sequence: 2,
594+
commandId: null,
595+
event: {
596+
id: EventId.make("event:shared-live:reply"),
597+
type: "turn-item.updated",
598+
threadId,
599+
occurredAt: at,
600+
payload: {
601+
...owner,
602+
id: TurnItemId.make("item:shared-live:reply"),
603+
type: "assistant_message",
604+
providerThreadId: null,
605+
providerTurnId: null,
606+
nativeItemRef: null,
607+
parentItemId: null,
608+
ordinal: 1,
609+
title: null,
610+
status: "completed",
611+
messageId,
612+
text: "Done",
613+
streaming: false,
614+
startedAt: at,
615+
completedAt: at,
616+
},
617+
},
618+
},
619+
] as ReadonlyArray<OrchestrationV2StoredEvent>);
620+
const stringify = yield* Effect.acquireRelease(
621+
Effect.sync(() => vi.spyOn(JSON, "stringify")),
622+
(spy) => Effect.sync(() => spy.mockRestore()),
623+
);
624+
// The EventSink hands every subscriber the same projected objects.
625+
const subscribe = (acceptDedupedBodies: boolean) =>
626+
subscribeOrchestrationV2Thread({ threadId, afterSequence: 0, acceptDedupedBodies }).pipe(
627+
Effect.flatMap(Stream.runCollect),
628+
Effect.map((items) => Array.from(items)),
629+
Effect.provideService(ThreadManagement.ThreadManagementService, {
630+
ensureLegacyTranscript: () => Effect.void,
631+
streamStoredEventsFrom: () => Stream.fromIterable(live),
632+
} as unknown as ThreadManagement.ThreadManagementService["Service"]),
633+
);
634+
const [deduped, first, second] = yield* Effect.forEach([true, false, false], subscribe);
635+
636+
assert.lengthOf(first!, live.length);
637+
first!.forEach((item, index) => {
638+
assert.strictEqual(item, second![index]);
639+
assert.strictEqual(item.kind === "event" && item.event, live[index]!.event);
640+
});
641+
assert.strictEqual(deduped![0], first![0]);
642+
const reply = deduped![1];
643+
assert.deepInclude(reply?.kind === "event" ? reply.event.payload : undefined, {
644+
text: "",
645+
textOmitted: true,
646+
});
647+
// The live buffer sizes each shared item once, not once per subscriber.
648+
assert.lengthOf(
649+
stringify.mock.calls.filter(([value]) => value?.kind === "event"),
650+
live.length,
651+
);
652+
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
653+
);
654+
553655
it.effect("shell snapshots omit idle children and older settled threads only on request", () =>
554656
Effect.gen(function* () {
555657
const shell = (

‎apps/server/src/ws.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ import {
165165
makeLiveBodyDeduper,
166166
projectDomainEventForWire,
167167
projectThreadProjectionForWire,
168+
threadStreamEventItem,
168169
} from "./orchestration-v2/WireProjection.ts";
169170
import { dedupeSnapshotBodies } from "@t3tools/shared/orchestrationV2DedupedBodies";
170171
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
@@ -872,11 +873,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
872873
afterSequence,
873874
})
874875
.pipe(
875-
Stream.map((stored) => ({
876-
kind: "event" as const,
877-
sequence: stored.sequence,
878-
event: stored.event,
879-
})),
876+
Stream.map(threadStreamEventItem),
880877
coalesceThreadLiveStream,
881878
Stream.mapError(
882879
(cause) =>

0 commit comments

Comments
 (0)