Skip to content

Commit 0e605b5

Browse files
t3dotggclaude
andcommitted
perf(server): MCP waits subscribe to the event types they need
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent ce31c1b commit 0e605b5

4 files changed

Lines changed: 67 additions & 50 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ import type { McpInvocationScope } from "./McpInvocationContext.ts";
7474

7575
const DEFAULT_WAIT_TIMEOUT_MS = 10 * 60 * 1_000;
7676
const MAX_WAIT_TIMEOUT_MS = 60 * 60 * 1_000;
77-
// Events that can move a delegated task's status: runs and tasks on the parent
78-
// or child thread, and a child's background work (pending provider tasks).
79-
const TASK_WAKE_EVENT_TYPES: ReadonlySet<string> = new Set([
80-
"run.created",
81-
"run.updated",
82-
"subagent.updated",
83-
"provider-thread.updated",
84-
]);
77+
// Events that can make a delegated task terminal: the parent's task record,
78+
// and the child's runs, nested tasks, and pending provider background work.
79+
const TASK_WAKE_EVENTS = [
80+
{ thread: "parent", eventType: "subagent.updated" },
81+
{ thread: "child", eventType: "run.updated" },
82+
{ thread: "child", eventType: "subagent.updated" },
83+
{ thread: "child", eventType: "provider-thread.updated" },
84+
] as const;
8585
const DEFAULT_THREAD_LIST_LIMIT = 50;
8686
const DEFAULT_THREAD_READ_LIMIT = 50;
8787
const DEFAULT_THREAD_RUN_LIMIT = 10;
@@ -1175,15 +1175,18 @@ const make = Effect.gen(function* () {
11751175
.pipe(Effect.mapError(streamError));
11761176
const initial = yield* readTask(scope, taskId, false, true);
11771177
if (isTerminalTaskStatus(initial.status)) return Option.some(initial);
1178-
return yield* Stream.merge(
1179-
threadManagement.streamStoredEventsFrom({ threadId: scope.threadId, afterSequence }),
1180-
threadManagement.streamStoredEventsFrom({
1181-
threadId: initial.childThreadId,
1182-
afterSequence,
1183-
}),
1178+
// One stream per event type, so transcript events never fill a buffer.
1179+
return yield* Stream.mergeAll(
1180+
TASK_WAKE_EVENTS.map(({ thread, eventType }) =>
1181+
threadManagement.streamStoredEventsFrom({
1182+
threadId: thread === "parent" ? scope.threadId : initial.childThreadId,
1183+
afterSequence,
1184+
eventType,
1185+
}),
1186+
),
1187+
{ concurrency: "unbounded" },
11841188
).pipe(
11851189
Stream.mapError(streamError),
1186-
Stream.filter((stored) => TASK_WAKE_EVENT_TYPES.has(stored.event.type)),
11871190
Stream.mapEffect(() => readTask(scope, taskId, false, true)),
11881191
Stream.filter((result) => isTerminalTaskStatus(result.status)),
11891192
Stream.runHead,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ export interface OrchestratorV2Shape {
293293
readonly streamStoredEventsFrom: (input?: {
294294
readonly threadId?: ThreadId;
295295
readonly afterSequence?: number;
296+
/** Keep only this type, before the bounded buffer retains anything. */
297+
readonly eventType?: OrchestrationV2DomainEvent["type"];
296298
}) => Stream.Stream<OrchestrationV2StoredEvent, OrchestratorV2Error>;
297299
readonly streamDomainEvents: Stream.Stream<OrchestrationV2DomainEvent, OrchestratorV2Error>;
298300
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,14 @@ it.effect("waitForThread reads the run again only when the run updates", () =>
437437
Layer.provide(
438438
Layer.mock(Orchestrator.OrchestratorV2)({
439439
getThreadEventSequence: () => Effect.succeed(0),
440-
streamStoredEventsFrom: () =>
441-
Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
442-
Stream.drain,
443-
Stream.concat(Stream.fromQueue(events)),
444-
),
440+
// Only the run.updated stream carries events in this test.
441+
streamStoredEventsFrom: (input) =>
442+
input?.eventType === "run.updated"
443+
? Stream.fromEffect(Deferred.succeed(subscribed, undefined)).pipe(
444+
Stream.drain,
445+
Stream.concat(Stream.fromQueue(events)),
446+
)
447+
: Stream.never,
445448
getThreadRecords: () =>
446449
Effect.sync(() => {
447450
reads += 1;
@@ -461,12 +464,12 @@ it.effect("waitForThread reads the run again only when the run updates", () =>
461464
.pipe(Effect.forkChild);
462465
yield* Deferred.await(subscribed);
463466
status = "completed";
464-
yield* Queue.offer(events, stored(1, { type: "turn-item.updated", payload: { id: "item" } }));
467+
yield* Queue.offer(events, stored(1, { type: "run.updated", payload: { id: "other-run" } }));
465468
yield* Queue.offer(events, stored(2, { type: "run.updated", payload: { id: runId, status } }));
466469
const result = yield* Fiber.join(fiber);
467470

468471
expect(result).toMatchObject({ timedOut: false, run: { id: runId, status: "completed" } });
469-
// The first read plus one for the run update. The turn item caused none.
472+
// The first read plus one for this run's update. The other run caused none.
470473
expect(reads).toBe(2);
471474
}),
472475
);

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

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -639,35 +639,44 @@ const make = Effect.gen(function* () {
639639
}
640640

641641
// Re-read the run only when the thread records a run update or its
642-
// deletion, instead of polling the projection for up to an hour.
643-
const wait = orchestrator
644-
.streamStoredEventsFrom({ threadId: input.threadId, afterSequence })
645-
.pipe(
646-
Stream.mapError(loadError),
647-
Stream.filter(
648-
(stored) =>
649-
(stored.event.type === "run.updated" && stored.event.payload.id === selectedRun.id) ||
650-
stored.event.type === "thread.deleted",
651-
),
652-
Stream.mapEffect(() =>
653-
getProjectThreadRecords(input, ["runs"], { runIds: [selectedRun.id] }).pipe(
654-
Effect.flatMap((current) => {
655-
const run = current.runs.find((candidate) => candidate.id === selectedRun.id);
656-
return run === undefined
657-
? Effect.fail(
658-
new ThreadManagementRunNotFoundError({
659-
threadId: input.threadId,
660-
runId: selectedRun.id,
661-
}),
662-
)
663-
: Effect.succeed(run);
664-
}),
665-
),
642+
// deletion, instead of polling the projection for up to an hour. Each
643+
// stream keeps one event type, so transcript events never fill its buffer.
644+
const wait = Stream.merge(
645+
orchestrator.streamStoredEventsFrom({
646+
threadId: input.threadId,
647+
afterSequence,
648+
eventType: "run.updated",
649+
}),
650+
orchestrator.streamStoredEventsFrom({
651+
threadId: input.threadId,
652+
afterSequence,
653+
eventType: "thread.deleted",
654+
}),
655+
).pipe(
656+
Stream.mapError(loadError),
657+
Stream.filter(
658+
(stored) =>
659+
stored.event.type !== "run.updated" || stored.event.payload.id === selectedRun.id,
660+
),
661+
Stream.mapEffect(() =>
662+
getProjectThreadRecords(input, ["runs"], { runIds: [selectedRun.id] }).pipe(
663+
Effect.flatMap((current) => {
664+
const run = current.runs.find((candidate) => candidate.id === selectedRun.id);
665+
return run === undefined
666+
? Effect.fail(
667+
new ThreadManagementRunNotFoundError({
668+
threadId: input.threadId,
669+
runId: selectedRun.id,
670+
}),
671+
)
672+
: Effect.succeed(run);
673+
}),
666674
),
667-
Stream.filter((run) => isTerminalRunStatus(run.status)),
668-
Stream.runHead,
669-
Effect.timeoutOption(Duration.millis(Math.max(1, input.timeoutMs))),
670-
);
675+
),
676+
Stream.filter((run) => isTerminalRunStatus(run.status)),
677+
Stream.runHead,
678+
Effect.timeoutOption(Duration.millis(Math.max(1, input.timeoutMs))),
679+
);
671680
const waited = Option.flatten(yield* wait);
672681
if (Option.isSome(waited)) {
673682
return { threadId: input.threadId, run: waited.value, timedOut: false };

0 commit comments

Comments
 (0)