Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions apps/server/src/guest/GuestShareService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/orchestration-v2/EventSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 23 additions & 8 deletions apps/server/src/orchestration-v2/FoundationPersistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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: [
{
Expand All @@ -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");
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,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;
Expand Down
35 changes: 35 additions & 0 deletions apps/server/src/orchestration-v2/WireProjection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CheckpointScopeId,
MessageId,
ContextHandoffId,
ProviderSessionId,
ProviderThreadId,
NodeId,
EventId,
Expand All @@ -13,6 +14,7 @@ import {
ThreadId,
TurnItemId,
type OrchestrationV2DomainEvent,
type OrchestrationV2StoredEvent,
type OrchestrationV2ThreadProjection,
type OrchestrationV2ThreadStreamItem,
OrchestrationV2TurnItem,
Expand All @@ -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";
Expand Down Expand Up @@ -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", () => {
Expand Down
30 changes: 30 additions & 0 deletions apps/server/src/orchestration-v2/WireProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
OrchestrationV2Checkpoint,
OrchestrationV2DomainEvent,
OrchestrationV2ContextHandoff,
OrchestrationV2StoredEvent,
OrchestrationV2Subagent,
OrchestrationV2ThreadProjection,
OrchestrationV2ThreadStreamItem,
Expand Down Expand Up @@ -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 {
Expand Down
102 changes: 102 additions & 0 deletions apps/server/src/ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { assert, it } from "@effect/vitest";
import {
AuthSessionId,
EnvironmentId,
EventId,
MessageId,
ProjectId,
ThreadId,
TurnItemId,
ProviderDriverKind,
ProviderInstanceId,
WS_METHODS,
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 = <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 = (
Expand Down
7 changes: 2 additions & 5 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) =>
Expand Down
Loading