Skip to content

Commit de99330

Browse files
navidnadaliclaude
andauthored
perf(server): each live event is projected once for all subscribers (pingdotgg#30860)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent ca308a6 commit de99330

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
@@ -51,9 +51,9 @@ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
5151
import { coalesceThreadLiveStream } from "../orchestration-v2/ThreadLiveEventCoalescer.ts";
5252
import { buildBoundedThreadStreamSnapshot } from "../orchestration-v2/ThreadStream.ts";
5353
import {
54-
projectDomainEventForWire,
5554
projectTurnItemForDetail,
5655
projectTurnItemForWire,
56+
threadStreamEventItem,
5757
} from "../orchestration-v2/WireProjection.ts";
5858
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
5959
import * as ServerSettings from "../serverSettings.ts";
@@ -550,11 +550,7 @@ const make = Effect.gen(function* () {
550550
const live = orchestrator
551551
.streamStoredEventsFrom({ threadId: thread.id, afterSequence: snapshot.snapshotSequence })
552552
.pipe(
553-
Stream.map((stored) => ({
554-
kind: "event" as const,
555-
sequence: stored.sequence,
556-
event: projectDomainEventForWire(stored.event),
557-
})),
553+
Stream.map(threadStreamEventItem),
558554
// Ending the source here flushes what the coalescer holds, so `ended` is not held back.
559555
Stream.takeUntil((item) => endsShare(item.event)),
560556
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
@@ -486,6 +486,7 @@ export interface OrchestratorV2Shape {
486486
threadId: ThreadId,
487487
) => Effect.Effect<number, OrchestratorV2Error>;
488488
readonly streamStoredEvents: Stream.Stream<OrchestrationV2StoredEvent, OrchestratorV2Error>;
489+
/** Events arrive projected for the wire and shared by every subscriber; never mutate them. */
489490
readonly streamStoredEventsFrom: (input?: {
490491
readonly threadId?: ThreadId;
491492
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
@@ -6,8 +6,11 @@ import {
66
AuthSessionId,
77
AuthSourceControlWriteScope,
88
EnvironmentId,
9+
EventId,
10+
MessageId,
911
ProjectId,
1012
ThreadId,
13+
TurnItemId,
1114
ProviderDriverKind,
1215
ProviderInstanceId,
1316
SourceControlRepositoryError,
@@ -37,6 +40,7 @@ import { Headers } from "effect/http";
3740
import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc";
3841
import * as SqlClient from "effect/sql/SqlClient";
3942
import * as Statement from "effect/sql/Statement";
43+
import { vi } from "vite-plus/test";
4044

4145
import * as AssetSigner from "./assets/AssetSigner.ts";
4246
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
556560
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
557561
);
558562

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+
559661
it.effect("shell snapshots omit idle children and older settled threads only on request", () =>
560662
Effect.gen(function* () {
561663
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";
@@ -917,11 +918,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
917918
afterSequence,
918919
})
919920
.pipe(
920-
Stream.map((stored) => ({
921-
kind: "event" as const,
922-
sequence: stored.sequence,
923-
event: stored.event,
924-
})),
921+
Stream.map(threadStreamEventItem),
925922
coalesceThreadLiveStream,
926923
Stream.mapError(
927924
(cause) =>

0 commit comments

Comments
 (0)