From a8144903c5fc0a2f1a09ad9374751ed964e32271 Mon Sep 17 00:00:00 2001 From: Navid Nadali Date: Wed, 4 Nov 2026 10:54:03 +0000 Subject: [PATCH] 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 --- apps/server/src/guest/GuestShareService.ts | 8 +- apps/server/src/orchestration-v2/EventSink.ts | 4 +- .../FoundationPersistence.test.ts | 31 ++++-- .../src/orchestration-v2/Orchestrator.ts | 1 + .../orchestration-v2/WireProjection.test.ts | 35 ++++++ .../src/orchestration-v2/WireProjection.ts | 30 ++++++ apps/server/src/ws.test.ts | 102 ++++++++++++++++++ apps/server/src/ws.ts | 7 +- 8 files changed, 197 insertions(+), 21 deletions(-) diff --git a/apps/server/src/guest/GuestShareService.ts b/apps/server/src/guest/GuestShareService.ts index fd9a0efc3b66b..15714f235fa98 100644 --- a/apps/server/src/guest/GuestShareService.ts +++ b/apps/server/src/guest/GuestShareService.ts @@ -56,9 +56,9 @@ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts"; import { coalesceThreadLiveStream } from "../orchestration-v2/ThreadLiveEventCoalescer.ts"; import { buildBoundedThreadStreamSnapshot } from "../orchestration-v2/ThreadStream.ts"; import { - projectDomainEventForWire, projectTurnItemForDetail, projectTurnItemForWire, + threadStreamEventItem, } from "../orchestration-v2/WireProjection.ts"; import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; import * as ServerSettings from "../serverSettings.ts"; @@ -557,11 +557,7 @@ const make = Effect.gen(function* () { const live = orchestrator .streamStoredEventsFrom({ threadId: thread.id, afterSequence: snapshot.snapshotSequence }) .pipe( - Stream.map((stored) => ({ - kind: "event" as const, - sequence: stored.sequence, - event: projectDomainEventForWire(stored.event), - })), + Stream.map(threadStreamEventItem), // Ending the source here flushes what the coalescer holds, so `ended` is not held back. Stream.takeUntil((item) => endsShare(item.event)), coalesceThreadLiveStream, diff --git a/apps/server/src/orchestration-v2/EventSink.ts b/apps/server/src/orchestration-v2/EventSink.ts index b210d3c9840ae..23db6c69dbdbb 100644 --- a/apps/server/src/orchestration-v2/EventSink.ts +++ b/apps/server/src/orchestration-v2/EventSink.ts @@ -25,7 +25,7 @@ import * as SqlClient from "effect/sql/SqlClient"; import { replayAndBufferProjectedLiveEvents } from "./LiveStreamBudget.ts"; import type { UnsequencedProjectEvent } from "../persistence/OrchestrationEventStore.ts"; -import { projectDomainEventForWire } from "./WireProjection.ts"; +import { projectStoredEventForWire } from "./WireProjection.ts"; import { makeRunOutcomeRecorder, type RunOutcome } from "./RunMetrics.ts"; import * as CommandReceiptStore from "./CommandReceiptStore.ts"; @@ -863,7 +863,7 @@ const layerBase: Layer.Layer< afterSequence, filter: matches, replay, - project: (stored) => ({ ...stored, event: projectDomainEventForWire(stored.event) }), + project: projectStoredEventForWire, }); } const subscription = yield* PubSub.subscribe(pubsub); diff --git a/apps/server/src/orchestration-v2/FoundationPersistence.test.ts b/apps/server/src/orchestration-v2/FoundationPersistence.test.ts index 475e0a0e4ae7b..61553725254c1 100644 --- a/apps/server/src/orchestration-v2/FoundationPersistence.test.ts +++ b/apps/server/src/orchestration-v2/FoundationPersistence.test.ts @@ -326,7 +326,7 @@ it.effect("paces scheduled compaction while preserving retention state and fixed ); it.layer(layerTest)("orchestration V2 foundation persistence", (it) => { - it.effect("projects oversized tool bodies before both replay and live RPC retention", () => + it.effect("projects oversized tool bodies once before replay and live RPC retention", () => Effect.scoped( Effect.gen(function* () { const sink = yield* EventSink.EventSinkV2; @@ -369,17 +369,25 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => { }, ], }); - const pull = yield* Stream.toPull( - sink.stream({ - threadId: thread.id, - afterSequence: created!.sequence, - bounded: true, - }), - ); + const subscribe = () => + Stream.toPull( + sink.stream({ + threadId: thread.id, + afterSequence: created!.sequence, + bounded: true, + }), + ); + const pull = yield* subscribe(); + const other = yield* subscribe(); const replay = yield* pull; assert.lengthOf(replay, 1); + assert.lengthOf(yield* other, 1); assert.equal(replay[0]!.event.type, "turn-item.updated"); assert.isBelow(Buffer.byteLength(yield* encodeJson(replay)), 4_000); + const stringify = yield* Effect.acquireRelease( + Effect.sync(() => vi.spyOn(JSON, "stringify")), + (spy) => Effect.sync(() => spy.mockRestore()), + ); const [completed] = yield* sink.write({ events: [ { @@ -392,10 +400,17 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => { ], }); const live = yield* pull; + const otherLive = yield* other; assert.deepEqual( live.map((stored) => stored.sequence), [completed!.sequence], ); + // Both subscribers get one shared projection, so the live budget sizes it once. + assert.strictEqual(otherLive[0], live[0]); + assert.lengthOf( + stringify.mock.calls.filter(([value]) => value === live[0] || value === otherLive[0]), + 1, + ); assert.isBelow(Buffer.byteLength(yield* encodeJson(live)), 4_000); for (const stored of [...replay, ...live]) { assert.equal(stored.event.type, "turn-item.updated"); diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index b4bdbd705c290..8e235462e6519 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -481,6 +481,7 @@ export interface OrchestratorV2Shape { threadId: ThreadId, ) => Effect.Effect; readonly streamStoredEvents: Stream.Stream; + /** Events arrive projected for the wire and shared by every subscriber; never mutate them. */ readonly streamStoredEventsFrom: (input?: { readonly threadId?: ThreadId; readonly afterSequence?: number; diff --git a/apps/server/src/orchestration-v2/WireProjection.test.ts b/apps/server/src/orchestration-v2/WireProjection.test.ts index 95d3fcde7f401..5c42cc1b8fe33 100644 --- a/apps/server/src/orchestration-v2/WireProjection.test.ts +++ b/apps/server/src/orchestration-v2/WireProjection.test.ts @@ -4,6 +4,7 @@ import { CheckpointScopeId, MessageId, ContextHandoffId, + ProviderSessionId, ProviderThreadId, NodeId, EventId, @@ -13,6 +14,7 @@ import { ThreadId, TurnItemId, type OrchestrationV2DomainEvent, + type OrchestrationV2StoredEvent, type OrchestrationV2ThreadProjection, type OrchestrationV2ThreadStreamItem, OrchestrationV2TurnItem, @@ -28,6 +30,7 @@ import { projectTurnItemForWire, projectTurnItemForDetail, projectDomainEventForWire, + projectStoredEventForWire, } from "./WireProjection.ts"; import { threadShellFromProjection } from "./ProjectionStore.ts"; import { MAX_TOOL_OUTPUT_IMAGES, toolOutputImages } from "@t3tools/shared/toolOutput"; @@ -338,6 +341,38 @@ describe("orchestration V2 wire projection", () => { expect(event.payload.output).toBe(output); expect(projectDomainEventForWire(projected)).toEqual(projected); }); + + it("shares one wire projection per stored event", () => { + const tool: OrchestrationV2StoredEvent = { + sequence: 1, + commandId: null, + event: { + id: EventId.make("shared-tool-event"), + type: "turn-item.updated", + threadId: base.threadId, + occurredAt: base.updatedAt, + payload: { ...base, output: { content: [{ type: "text", text: "PRIVATE_BODY" }] } }, + }, + }; + const projected = projectStoredEventForWire(tool); + expect(projected).toEqual({ ...tool, event: projectDomainEventForWire(tool.event) }); + expect(projectStoredEventForWire(tool)).toBe(projected); + const detached: OrchestrationV2StoredEvent = { + sequence: 2, + commandId: null, + event: { + id: EventId.make("detached-event"), + type: "provider-session.detached", + threadId: base.threadId, + occurredAt: base.updatedAt, + payload: { + providerSessionId: ProviderSessionId.make("session"), + detachedAt: base.updatedAt, + }, + }, + }; + expect(projectStoredEventForWire(detached)).toBe(detached); + }); }); describe("live body deduper", () => { diff --git a/apps/server/src/orchestration-v2/WireProjection.ts b/apps/server/src/orchestration-v2/WireProjection.ts index c4916546cf8e1..c801180dcc0f8 100644 --- a/apps/server/src/orchestration-v2/WireProjection.ts +++ b/apps/server/src/orchestration-v2/WireProjection.ts @@ -5,6 +5,7 @@ import type { OrchestrationV2Checkpoint, OrchestrationV2DomainEvent, OrchestrationV2ContextHandoff, + OrchestrationV2StoredEvent, OrchestrationV2Subagent, OrchestrationV2ThreadProjection, OrchestrationV2ThreadStreamItem, @@ -323,6 +324,35 @@ export function projectDomainEventForWire( : event; } +// Keyed on the stored event PubSub hands every subscriber, so all of them share one projection +// and LiveStreamBudget sizes it once. Consumers must not mutate the shared result. +const projectedStoredEvents = new WeakMap(); + +export function projectStoredEventForWire( + stored: OrchestrationV2StoredEvent, +): OrchestrationV2StoredEvent { + const cached = projectedStoredEvents.get(stored); + if (cached !== undefined) return cached; + const event = projectDomainEventForWire(stored.event); + if (event === stored.event) return stored; + const projected = { ...stored, event }; + projectedStoredEvents.set(stored, projected); + return projected; +} + +type ThreadStreamEvent = Extract; + +const threadStreamEvents = new WeakMap(); + +/** For an event already projected for the wire; subscribers share the item and its cached size. */ +export function threadStreamEventItem(stored: OrchestrationV2StoredEvent): ThreadStreamEvent { + const cached = threadStreamEvents.get(stored); + if (cached !== undefined) return cached; + const item = { kind: "event" as const, sequence: stored.sequence, event: stored.event }; + threadStreamEvents.set(stored, item); + return item; +} + const LIVE_DEDUPE_MEMORY = 32; function remember(memory: Map, key: K, value: V): void { diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts index d1d0d36414ab8..39ee452b6b44f 100644 --- a/apps/server/src/ws.test.ts +++ b/apps/server/src/ws.test.ts @@ -5,8 +5,11 @@ import { assert, it } from "@effect/vitest"; import { AuthSessionId, EnvironmentId, + EventId, + MessageId, ProjectId, ThreadId, + TurnItemId, ProviderDriverKind, ProviderInstanceId, WS_METHODS, @@ -34,6 +37,7 @@ import { Headers } from "effect/http"; import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc"; import * as SqlClient from "effect/sql/SqlClient"; import * as Statement from "effect/sql/Statement"; +import { vi } from "vite-plus/test"; import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts"; 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 }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)), ); +it.effect("shares each live event item across subscribers and dedupes bodies per subscriber", () => + Effect.gen(function* () { + const threadId = ThreadId.make("thread:shared-live"); + const messageId = MessageId.make("message:shared-live"); + const at = DateTime.makeUnsafe("2026-10-31T00:00:00.000Z"); + const owner = { threadId, runId: null, nodeId: null, createdAt: at, updatedAt: at }; + // DateTime memoizes its formatted parts, so only the plain records are frozen. + const freeze = (value: A): A => { + if (typeof value === "object" && value !== null && !DateTime.isDateTime(value)) { + for (const child of Object.values(value)) freeze(child); + Object.freeze(value); + } + return value; + }; + const live: ReadonlyArray = freeze([ + { + sequence: 1, + commandId: null, + event: { + id: EventId.make("event:shared-live:message"), + type: "message.updated", + threadId, + occurredAt: at, + payload: { + ...owner, + id: messageId, + role: "assistant", + text: "Done", + attachments: [], + streaming: false, + createdBy: "agent", + creationSource: "provider", + }, + }, + }, + { + sequence: 2, + commandId: null, + event: { + id: EventId.make("event:shared-live:reply"), + type: "turn-item.updated", + threadId, + occurredAt: at, + payload: { + ...owner, + id: TurnItemId.make("item:shared-live:reply"), + type: "assistant_message", + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal: 1, + title: null, + status: "completed", + messageId, + text: "Done", + streaming: false, + startedAt: at, + completedAt: at, + }, + }, + }, + ] as ReadonlyArray); + const stringify = yield* Effect.acquireRelease( + Effect.sync(() => vi.spyOn(JSON, "stringify")), + (spy) => Effect.sync(() => spy.mockRestore()), + ); + // The EventSink hands every subscriber the same projected objects. + const subscribe = (acceptDedupedBodies: boolean) => + subscribeOrchestrationV2Thread({ threadId, afterSequence: 0, acceptDedupedBodies }).pipe( + Effect.flatMap(Stream.runCollect), + Effect.map((items) => Array.from(items)), + Effect.provideService(ThreadManagement.ThreadManagementService, { + ensureLegacyTranscript: () => Effect.void, + streamStoredEventsFrom: () => Stream.fromIterable(live), + } as unknown as ThreadManagement.ThreadManagementService["Service"]), + ); + const [deduped, first, second] = yield* Effect.forEach([true, false, false], subscribe); + + assert.lengthOf(first!, live.length); + first!.forEach((item, index) => { + assert.strictEqual(item, second![index]); + assert.strictEqual(item.kind === "event" && item.event, live[index]!.event); + }); + assert.strictEqual(deduped![0], first![0]); + const reply = deduped![1]; + assert.deepInclude(reply?.kind === "event" ? reply.event.payload : undefined, { + text: "", + textOmitted: true, + }); + // The live buffer sizes each shared item once, not once per subscriber. + assert.lengthOf( + stringify.mock.calls.filter(([value]) => value?.kind === "event"), + live.length, + ); + }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)), +); + it.effect("shell snapshots omit idle children and older settled threads only on request", () => Effect.gen(function* () { const shell = ( diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts index 2bd0acc22b8c3..dd7969e9682a0 100644 --- a/apps/server/src/ws.ts +++ b/apps/server/src/ws.ts @@ -165,6 +165,7 @@ import { makeLiveBodyDeduper, projectDomainEventForWire, projectThreadProjectionForWire, + threadStreamEventItem, } from "./orchestration-v2/WireProjection.ts"; import { dedupeSnapshotBodies } from "@t3tools/shared/orchestrationV2DedupedBodies"; import * as ProjectStore from "./orchestration-v2/ProjectStore.ts"; @@ -872,11 +873,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs afterSequence, }) .pipe( - Stream.map((stored) => ({ - kind: "event" as const, - sequence: stored.sequence, - event: stored.event, - })), + Stream.map(threadStreamEventItem), coalesceThreadLiveStream, Stream.mapError( (cause) =>