@devries-cameron20 / t3code Mirror · read-only / commits / 20f45bf23e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 09:30 UTC

perf(server): avoid unused history reads during thread waits (#27032)

Co-authored-by: maria-rcks <maria@kuuro.net>

Adamulek123 committed Nov 1, 2026 at 03:17 UTC 20f45bf23ed6d9bfbf7aa7eab7a946d2e2eaad7c
6 files changed +191 -10
apps/server/src/mcp/OrchestratorMcpService.test.ts
+69 -6
@@ -2652,7 +2652,11 @@ describe("OrchestratorMcpService thread lookup", () => {
2652 capabilities: new Set(["orchestration"]),
2653 issuedAt: 1,
2654 };
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 + ) =>
2660 layerOrchestratorService.pipe(
2661 Layer.provide(
2662 Layer.mergeAll(
@@ -2661,6 +2665,7 @@ describe("OrchestratorMcpService thread lookup", () => {
2665 getThreadShell: (threadId) =>
2666 Effect.succeed(shells.find((shell) => shell.id === threadId) ?? null),
2667 sendToThread: () => Ref.update(sent, (count) => count + 1).pipe(Effect.as({} as never)),
2668 + ...overrides,
2669 }),
2670 ServerSettings.layerTest(),
2671 Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -2683,14 +2688,15 @@ describe("OrchestratorMcpService thread lookup", () => {
2688 const send = yield* mcp
2689 .sendToThread(client, { threadId: bareId, message: "hello" })
2690 .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) };
2693 }).pipe(Effect.provide(service(shells, sent)));
2694 });
2695
2696 it.effect("points a bare uuid at the mcp: thread it belongs to", () =>
2697 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]) {
2700 assert.equal(error.code, "thread_not_found");
2701 assert.include(error.message, `mcp:${bareId}`);
2702 }
@@ -2700,19 +2706,76 @@ describe("OrchestratorMcpService thread lookup", () => {
2706
2707 it.effect("keeps the plain message for an id that matches nothing", () =>
2708 Effect.gen(function* () {
2703 - const { read, send } = yield* failuresFor([]);
2709 + const { read, send, wait } = yield* failuresFor([]);
2710 assert.equal(read.code, "thread_not_found");
2711 assert.equal(read.message, `Thread ${bareId} is no longer available.`);
2712 assert.equal(send.code, "thread_not_found");
2713 assert.equal(send.message, `Thread ${bareId} was not found.`);
2714 + assert.equal(wait.code, "thread_not_found");
2715 + assert.equal(wait.message, send.message);
2716 }),
2717 );
2718
2719 it.effect("does not name an mcp: thread that was deleted", () =>
2720 Effect.gen(function* () {
2713 - const { read, send } = yield* failuresFor([deletedLaunched]);
2721 + const { read, send, wait } = yield* failuresFor([deletedLaunched]);
2722 assert.equal(read.message, `Thread ${bareId} is no longer available.`);
2723 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 + );
2779 }),
2780 );
2781 });
apps/server/src/mcp/OrchestratorMcpService.ts
+10 -3
@@ -1152,6 +1152,7 @@ const make = Effect.gen(function* () {
1152
1153 const loadTargetThread = Effect.fn("OrchestratorMcpService.loadTargetThread")(function* (
1154 threadId: ThreadId,
1155 + includeHistory = true,
1156 ) {
1157 const shell = yield* threadManagement
1158 .getThreadShell(threadId)
@@ -1159,18 +1160,24 @@ const make = Effect.gen(function* () {
1160 if (shell === null || shell.deletedAt !== null) {
1161 return yield* threadNotFound(threadManagement, threadId, `Thread ${threadId} was not found.`);
1162 }
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));
1169 });
1170
1171 const loadScopedThread = Effect.fn("OrchestratorMcpService.loadScopedThread")(function* (
1172 scope: McpInvocationScope,
1173 threadId: ThreadId,
1174 + includeHistory = true,
1175 ) {
1176 const caller = yield* loadCaller(scope);
1177 const target =
1178 caller.parent !== undefined && threadId === caller.parent.thread.id
1179 ? caller.parent
1173 - : yield* loadTargetThread(threadId);
1180 + : yield* loadTargetThread(threadId, includeHistory);
1181 return { ...caller, target } as const;
1182 });
1183
@@ -3019,7 +3026,7 @@ const make = Effect.gen(function* () {
3026 } satisfies OrchestratorMcpThreadSendResult;
3027 }),
3028 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);
3030 const result = yield* threadManagement
3031 .waitForThread({
3032 projectId: target.thread.projectId,
apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
+44
@@ -597,6 +597,7 @@ describe("orchestrator MCP toolkit", () => {
597 delegated.resultContextTransferId,
598 );
599 const transferReads: Array<string> = [];
600 + const messageReads: Array<string> = [];
601 const tracer = Tracer.make({
602 span(options) {
603 const span = new Tracer.NativeSpan(options);
@@ -610,10 +611,53 @@ describe("orchestrator MCP toolkit", () => {
611 ) {
612 transferReads.push(query);
613 }
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 + }
620 };
621 return span;
622 },
623 });
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);
661 const parentlessRead = yield* invoke(
662 "t3_thread_read",
663 { threadId: delegated.childThreadId, limit: 100 },
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+65
@@ -215,6 +215,71 @@ const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate
215 return threadId;
216 });
217
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 +
283 it.effect("SQL queue readiness skips held runs without bypassing active work", () =>
284 Effect.gen(function* () {
285 const store = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+2
@@ -330,6 +330,7 @@ export interface ProjectionRuntimeResponseContext {
330
331 export interface ProjectionRecordFilter {
332 readonly messageRoles?: ReadonlyArray<OrchestrationV2ConversationMessage["role"]>;
333 + readonly monitorNotificationsOnly?: boolean;
334 readonly turnItemRunId?: RunId;
335 readonly messageIds?: ReadonlyArray<MessageId>;
336 readonly messageRunIds?: ReadonlyArray<RunId>;
@@ -3422,6 +3423,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3423 WHERE thread_id = ${threadId}
3424 ${filter?.messageIds === undefined ? sql`` : sql`AND message_id IN (SELECT value FROM json_each(${encodeIdList(filter.messageIds)}))`}
3425 ${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``}
3427 ${filter?.messageRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.messageRunIds)}))`}
3428 ORDER BY created_at ASC, message_id ASC
3429 `
apps/server/src/orchestration-v2/ThreadManagementService.ts
+1 -1
@@ -940,7 +940,7 @@ const make = Effect.gen(function* () {
940 const records = yield* getProjectThreadRecords(
941 input,
942 ["runs", "messages", "subagents", "providerThreads"],
943 - { messageRoles: ["user"] },
943 + { messageRoles: ["user"], monitorNotificationsOnly: true },
944 );
945 const progress = delegatedTaskProgress(records);
946 // A thread rolled back past all its work, with nothing left running, has no answer to wait for.