Skip to content
TypeScript

perf(server): each live event is projected once for all subscribers (#30860)

Navid Nadalide99330a3ba9
diff --git a/apps/server/src/guest/GuestShareService.ts b/apps/server/src/guest/GuestShareService.ts
index 75c4962f3cb..36be1d1e21c 100644
--- a/apps/server/src/guest/GuestShareService.ts
+++ b/apps/server/src/guest/GuestShareService.ts
@@ -51,9 +51,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";
@@ -550,11 +550,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 b210d3c9840..23db6c69dbd 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 0b48f82fe1f..b929fc6ece3 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 8bf0c20de98..e199decf352 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -486,6 +486,7 @@ export interface OrchestratorV2Shape {
threadId: ThreadId,
) => Effect.Effect<number, OrchestratorV2Error>;
readonly streamStoredEvents: Stream.Stream<OrchestrationV2StoredEvent, OrchestratorV2Error>;
+ /** 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 95d3fcde7f4..5c42cc1b8fe 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 c4916546cf8..c801180dcc0 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<OrchestrationV2StoredEvent, OrchestrationV2StoredEvent>();
+
+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<OrchestrationV2ThreadStreamItem, { readonly kind: "event" }>;
+
+const threadStreamEvents = new WeakMap<OrchestrationV2StoredEvent, ThreadStreamEvent>();
+
+/** 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<K, V>(memory: Map<K, V>, key: K, value: V): void {
diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts
index 855e59440ac..2be9e61c7c9 100644
--- a/apps/server/src/ws.test.ts
+++ b/apps/server/src/ws.test.ts
@@ -6,8 +6,11 @@ import {
AuthSessionId,
AuthSourceControlWriteScope,
EnvironmentId,
+ EventId,
+ MessageId,
ProjectId,
ThreadId,
+ TurnItemId,
ProviderDriverKind,
ProviderInstanceId,
SourceControlRepositoryError,
@@ -37,6 +40,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 AssetSigner from "./assets/AssetSigner.ts";
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
}).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 = <A>(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<OrchestrationV2StoredEvent> = 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<OrchestrationV2StoredEvent>);
+ 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 079adfb19ce..035f8b1688d 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";
@@ -917,11 +918,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) =>