Skip to content

Commit 20f45bf

Browse files
perf(server): avoid unused history reads during thread waits (#27032)
Co-authored-by: maria-rcks <[email protected]>
1 parent 6f2e48d commit 20f45bf

6 files changed

Lines changed: 191 additions & 10 deletions

File tree

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

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,7 +2652,11 @@ describe("OrchestratorMcpService thread lookup", () => {
26522652
capabilities: new Set(["orchestration"]),
26532653
issuedAt: 1,
26542654
};
2655-
const service = (shells: ReadonlyArray<OrchestrationV2ThreadShell>, sent: Ref.Ref<number>) =>
2655+
const service = (
2656+
shells: ReadonlyArray<OrchestrationV2ThreadShell>,
2657+
sent: Ref.Ref<number>,
2658+
overrides: Partial<ThreadManagementService.ThreadManagementService["Service"]> = {},
2659+
) =>
26562660
layerOrchestratorService.pipe(
26572661
Layer.provide(
26582662
Layer.mergeAll(
@@ -2661,6 +2665,7 @@ describe("OrchestratorMcpService thread lookup", () => {
26612665
getThreadShell: (threadId) =>
26622666
Effect.succeed(shells.find((shell) => shell.id === threadId) ?? null),
26632667
sendToThread: () => Ref.update(sent, (count) => count + 1).pipe(Effect.as({} as never)),
2668+
...overrides,
26642669
}),
26652670
ServerSettings.layerTest(),
26662671
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -2683,14 +2688,15 @@ describe("OrchestratorMcpService thread lookup", () => {
26832688
const send = yield* mcp
26842689
.sendToThread(client, { threadId: bareId, message: "hello" })
26852690
.pipe(Effect.flip);
2686-
return { read, send, sends: yield* Ref.get(sent) };
2691+
const wait = yield* mcp.waitForThread(client, { threadId: bareId }).pipe(Effect.flip);
2692+
return { read, send, wait, sends: yield* Ref.get(sent) };
26872693
}).pipe(Effect.provide(service(shells, sent)));
26882694
});
26892695

26902696
it.effect("points a bare uuid at the mcp: thread it belongs to", () =>
26912697
Effect.gen(function* () {
2692-
const { read, send, sends } = yield* failuresFor([launched]);
2693-
for (const error of [read, send]) {
2698+
const { read, send, wait, sends } = yield* failuresFor([launched]);
2699+
for (const error of [read, send, wait]) {
26942700
assert.equal(error.code, "thread_not_found");
26952701
assert.include(error.message, `mcp:${bareId}`);
26962702
}
@@ -2700,19 +2706,76 @@ describe("OrchestratorMcpService thread lookup", () => {
27002706

27012707
it.effect("keeps the plain message for an id that matches nothing", () =>
27022708
Effect.gen(function* () {
2703-
const { read, send } = yield* failuresFor([]);
2709+
const { read, send, wait } = yield* failuresFor([]);
27042710
assert.equal(read.code, "thread_not_found");
27052711
assert.equal(read.message, `Thread ${bareId} is no longer available.`);
27062712
assert.equal(send.code, "thread_not_found");
27072713
assert.equal(send.message, `Thread ${bareId} was not found.`);
2714+
assert.equal(wait.code, "thread_not_found");
2715+
assert.equal(wait.message, send.message);
27082716
}),
27092717
);
27102718

27112719
it.effect("does not name an mcp: thread that was deleted", () =>
27122720
Effect.gen(function* () {
2713-
const { read, send } = yield* failuresFor([deletedLaunched]);
2721+
const { read, send, wait } = yield* failuresFor([deletedLaunched]);
27142722
assert.equal(read.message, `Thread ${bareId} is no longer available.`);
27152723
assert.equal(send.message, `Thread ${bareId} was not found.`);
2724+
assert.equal(wait.message, send.message);
2725+
}),
2726+
);
2727+
2728+
it.effect("refuses waiting without orchestration capability before reading the target", () =>
2729+
Effect.gen(function* () {
2730+
const sent = yield* Ref.make(0);
2731+
return yield* Effect.gen(function* () {
2732+
const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
2733+
const error = yield* mcp
2734+
.waitForThread({ ...client, capabilities: new Set() }, { threadId: bareId })
2735+
.pipe(Effect.flip);
2736+
assert.equal(error.code, "capability_denied");
2737+
}).pipe(
2738+
Effect.provide(
2739+
service([], sent, {
2740+
getThreadShell: () => Effect.die("Unauthorized target access"),
2741+
}),
2742+
),
2743+
);
2744+
}),
2745+
);
2746+
2747+
it.effect("waits on the caller's own thread without a separate shell lookup", () =>
2748+
Effect.gen(function* () {
2749+
const sent = yield* Ref.make(0);
2750+
return yield* Effect.gen(function* () {
2751+
const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
2752+
const result = yield* mcp.waitForThread(
2753+
{
2754+
...client,
2755+
thread: {
2756+
threadId: launched.id,
2757+
providerSessionId: "provider-session:wait-self",
2758+
providerInstanceId: ProviderInstanceId.make("codex"),
2759+
},
2760+
},
2761+
{ threadId: launched.id },
2762+
);
2763+
assert.deepEqual(result, {
2764+
threadId: launched.id,
2765+
runId: null,
2766+
status: "idle",
2767+
timedOut: false,
2768+
});
2769+
}).pipe(
2770+
Effect.provide(
2771+
service([], sent, {
2772+
getThreadShell: () => Effect.die("Unexpected shell lookup for the caller"),
2773+
getThreadRecords: () => Effect.succeed(idleThreadProjection(launched)),
2774+
waitForThread: () =>
2775+
Effect.succeed({ threadId: launched.id, run: null, timedOut: false }),
2776+
}),
2777+
),
2778+
);
27162779
}),
27172780
);
27182781
});

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,25 +1152,32 @@ const make = Effect.gen(function* () {
11521152

11531153
const loadTargetThread = Effect.fn("OrchestratorMcpService.loadTargetThread")(function* (
11541154
threadId: ThreadId,
1155+
includeHistory = true,
11551156
) {
11561157
const shell = yield* threadManagement
11571158
.getThreadShell(threadId)
11581159
.pipe(Effect.mapError(threadManagementFailure));
11591160
if (shell === null || shell.deletedAt !== null) {
11601161
return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`);
11611162
}
1162-
return yield* loadProjectThread(shell.projectId, threadId);
1163+
// Native wait owns its history reads; keep legacy import and project validation here.
1164+
return yield* includeHistory
1165+
? loadProjectThread(shell.projectId, threadId)
1166+
: threadManagement
1167+
.getProjectThreadRecords({ projectId: shell.projectId, threadId }, [])
1168+
.pipe(Effect.mapError(threadManagementFailure));
11631169
});
11641170

11651171
const loadScopedThread = Effect.fn("OrchestratorMcpService.loadScopedThread")(function* (
11661172
scope: McpInvocationScope,
11671173
threadId: ThreadId,
1174+
includeHistory = true,
11681175
) {
11691176
const caller = yield* loadCaller(scope);
11701177
const target =
11711178
caller.parent !== undefined && threadId === caller.parent.thread.id
11721179
? caller.parent
1173-
: yield* loadTargetThread(threadId);
1180+
: yield* loadTargetThread(threadId, includeHistory);
11741181
return { ...caller, target } as const;
11751182
});
11761183

@@ -3019,7 +3026,7 @@ const make = Effect.gen(function* () {
30193026
} satisfies OrchestratorMcpThreadSendResult;
30203027
}),
30213028
waitForThread: Effect.fn("OrchestratorMcpService.waitForThread")(function* (scope, input) {
3022-
const { parent, target } = yield* loadScopedThread(scope, input.threadId);
3029+
const { parent, target } = yield* loadScopedThread(scope, input.threadId, false);
30233030
const result = yield* threadManagement
30243031
.waitForThread({
30253032
projectId: target.thread.projectId,

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ describe("orchestrator MCP toolkit", () => {
597597
delegated.resultContextTransferId,
598598
);
599599
const transferReads: Array<string> = [];
600+
const messageReads: Array<string> = [];
600601
const tracer = Tracer.make({
601602
span(options) {
602603
const span = new Tracer.NativeSpan(options);
@@ -610,10 +611,53 @@ describe("orchestrator MCP toolkit", () => {
610611
) {
611612
transferReads.push(query);
612613
}
614+
if (
615+
typeof query === "string" &&
616+
/SELECT\s+payload_json\s+FROM orchestration_v2_projection_messages/i.test(query)
617+
) {
618+
messageReads.push(query);
619+
}
613620
};
614621
return span;
615622
},
616623
});
624+
const wait = yield* invoke(
625+
"t3_thread_wait",
626+
{ threadId: delegated.childThreadId, runId: queuedFollowup.runId },
627+
{ ...invocation, thread: undefined },
628+
).pipe(Effect.withTracer(tracer));
629+
expect(wait.isError).not.toBe(true);
630+
const expectedWait = {
631+
threadId: delegated.childThreadId,
632+
runId: queuedFollowup.runId,
633+
status: "completed",
634+
timedOut: false,
635+
};
636+
expect(wait.structuredContent).toEqual(expectedWait);
637+
expect(messageReads).toEqual([]);
638+
expect(transferReads).toHaveLength(0);
639+
const settledWait = yield* invoke(
640+
"t3_thread_wait",
641+
{
642+
threadId: delegated.childThreadId,
643+
runId: queuedFollowup.runId,
644+
untilDelegatedWorkSettled: true,
645+
},
646+
{ ...invocation, thread: undefined },
647+
).pipe(Effect.withTracer(tracer));
648+
expect(settledWait.isError).not.toBe(true);
649+
expect(settledWait.structuredContent).toMatchObject({
650+
...expectedWait,
651+
resultRun: { runId: queuedFollowup.runId, status: "completed" },
652+
});
653+
expect(messageReads.length).toBeGreaterThan(0);
654+
expect(
655+
messageReads.filter((query) => query.includes("$.notification.source.kind")),
656+
).toHaveLength(1);
657+
// The orchestrator's restart-continuation guard still reads its own records.
658+
expect(
659+
messageReads.filter((query) => !query.includes("$.notification.source.kind")),
660+
).toHaveLength(1);
617661
const parentlessRead = yield* invoke(
618662
"t3_thread_read",
619663
{ threadId: delegated.childThreadId, limit: 100 },

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,71 @@ const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate
215215
return threadId;
216216
});
217217

218+
it.effect("record reads filter notification sources without dropping matching message bodies", () =>
219+
Effect.gen(function* () {
220+
const store = yield* ProjectionStore.ProjectionStoreV2;
221+
const threadId = yield* addRolledBackRecoveryCandidate("notification-filter");
222+
const now = yield* DateTime.now;
223+
for (const [id, role, kind] of [
224+
["ordinary", "user", null],
225+
["monitor", "user", "monitor"],
226+
["system", "user", "system"],
227+
["assistant-monitor", "assistant", "monitor"],
228+
] as const) {
229+
yield* store.apply({
230+
id: EventId.make(`notification-filter:${id}`),
231+
type: "message.updated",
232+
threadId,
233+
occurredAt: now,
234+
payload: {
235+
id: MessageId.make(id),
236+
threadId,
237+
runId: null,
238+
nodeId: null,
239+
createdBy: "agent",
240+
creationSource: "server",
241+
role,
242+
text: `Complete body for ${id}`,
243+
attachments: [],
244+
streaming: false,
245+
createdAt: now,
246+
updatedAt: now,
247+
...(kind === null
248+
? {}
249+
: {
250+
notification: {
251+
source: { kind },
252+
outcome: "updated",
253+
summary: "Background notification",
254+
},
255+
}),
256+
},
257+
});
258+
}
259+
const matching = yield* store.getThreadRecords(threadId, ["messages"], {
260+
messageRoles: ["user"],
261+
monitorNotificationsOnly: true,
262+
});
263+
assert.deepEqual(
264+
matching.messages.map((message) => [message.id, message.text]),
265+
[["monitor", "Complete body for monitor"]],
266+
);
267+
assert.lengthOf((yield* store.getThreadRecords(threadId, ["messages"])).messages, 4);
268+
assert.lengthOf(
269+
(yield* store.getThreadRecords(threadId, ["messages"], {
270+
monitorNotificationsOnly: true,
271+
})).messages,
272+
2,
273+
);
274+
assert.lengthOf(
275+
(yield* store.getThreadRecords(threadId, ["messages"], {
276+
monitorNotificationsOnly: false,
277+
})).messages,
278+
4,
279+
);
280+
}).pipe(Effect.provide(layerTest)),
281+
);
282+
218283
it.effect("SQL queue readiness skips held runs without bypassing active work", () =>
219284
Effect.gen(function* () {
220285
const store = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export interface ProjectionRuntimeResponseContext {
330330

331331
export interface ProjectionRecordFilter {
332332
readonly messageRoles?: ReadonlyArray<OrchestrationV2ConversationMessage["role"]>;
333+
readonly monitorNotificationsOnly?: boolean;
333334
readonly turnItemRunId?: RunId;
334335
readonly messageIds?: ReadonlyArray<MessageId>;
335336
readonly messageRunIds?: ReadonlyArray<RunId>;
@@ -3422,6 +3423,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
34223423
WHERE thread_id = ${threadId}
34233424
${filter?.messageIds === undefined ? sql`` : sql`AND message_id IN (SELECT value FROM json_each(${encodeIdList(filter.messageIds)}))`}
34243425
${filter?.messageRoles === undefined ? sql`` : sql`AND role IN (SELECT value FROM json_each(${encodeIdList(filter.messageRoles)}))`}
3426+
${filter?.monitorNotificationsOnly === true ? sql`AND json_extract(payload_json, '$.notification.source.kind') = 'monitor'` : sql``}
34253427
${filter?.messageRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.messageRunIds)}))`}
34263428
ORDER BY created_at ASC, message_id ASC
34273429
`

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ const make = Effect.gen(function* () {
940940
const records = yield* getProjectThreadRecords(
941941
input,
942942
["runs", "messages", "subagents", "providerThreads"],
943-
{ messageRoles: ["user"] },
943+
{ messageRoles: ["user"], monitorNotificationsOnly: true },
944944
);
945945
const progress = delegatedTaskProgress(records);
946946
// A thread rolled back past all its work, with nothing left running, has no answer to wait for.

0 commit comments

Comments
 (0)