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, 10:45 UTC
perf(server): skip unused projections in thread reads (#27039)
Adamulek123 committed
Oct 31, 2026 at 02:44 UTC
f98ff73a1d2b6558d172e31219527409f422d35f
3 files changed
+89
-26
apps/server/src/mcp/OrchestratorMcpService.activity.test.ts
+31
-9
@@ -25,6 +25,7 @@ import * as SecretRequests from "../secrets/SecretRequests.ts";
25
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
26
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
27
import * as RemoteDelegation from "../peer/RemoteDelegation.ts";
28
+import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
29
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
30
import type * as McpInvocationContext from "./McpInvocationContext.ts";
31
import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
@@ -116,7 +117,8 @@ function makeRun(input: {
117
};
118
}
119
119
-it("readThread prefers activity-run status over a newer cancelled queued run", async () => {
120
+it("readThread preserves caller access errors and prefers activity-run status over a newer cancelled queued run", async () => {
121
+ let callerUnavailable = false;
122
const projection = {
123
thread: baseThread({
124
threadId: parentThreadId,
@@ -142,15 +144,24 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a
144
Layer.mock(ThreadManagementService.ThreadManagementService)({
145
getTimelinePage: () => Effect.succeed({ items: [], totalItems: 0, hasMore: false }),
146
getThreadRecords: (threadId) =>
145
- threadId === parentThreadId
146
- ? Effect.succeed(projection)
147
- : Effect.die(`unexpected thread ${threadId}`),
147
+ callerUnavailable
148
+ ? Effect.fail(
149
+ new Orchestrator.OrchestratorProjectionError({
150
+ threadId,
151
+ cause: "Caller unavailable",
152
+ }),
153
+ )
154
+ : threadId === parentThreadId
155
+ ? Effect.succeed(projection)
156
+ : Effect.die(`unexpected thread ${threadId}`),
157
getThreadShell: (threadId) =>
149
- Effect.succeed(
150
- threadId === parentThreadId
151
- ? (projection.thread as unknown as OrchestrationV2ThreadShell)
152
- : null,
153
- ),
158
+ callerUnavailable
159
+ ? Effect.die("A failed caller read must not reach the target")
160
+ : Effect.succeed(
161
+ threadId === parentThreadId
162
+ ? (projection.thread as unknown as OrchestrationV2ThreadShell)
163
+ : null,
164
+ ),
165
getProjectThreadRecords: (input) =>
166
input.threadId === parentThreadId
167
? Effect.succeed(projection)
@@ -180,6 +191,17 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a
191
expect(result.thread.status).toBe("running");
192
expect(result.thread.latestRunId).toBe(cancelledRunId);
193
expect(result.thread.activeRunId).toBe(activeRunId);
194
+
195
+ callerUnavailable = true;
196
+ const denied = yield* service
197
+ .readThread({ ...makeScope(), capabilities: new Set() }, { threadId: parentThreadId })
198
+ .pipe(Effect.flip);
199
+ expect(denied.code).toBe("capability_denied");
200
+ const missingCaller = yield* service
201
+ .readThread(makeScope(), { threadId: parentThreadId })
202
+ .pipe(Effect.flip);
203
+ expect(missingCaller.code).toBe("orchestration_error");
204
+ expect(missingCaller.message).toContain(`Unable to read thread ${parentThreadId}:`);
205
}).pipe(Effect.provide(layer), Effect.runPromise);
206
});
207
apps/server/src/mcp/OrchestratorMcpService.ts
+19
-11
@@ -1034,7 +1034,7 @@ const make = Effect.gen(function* () {
1034
),
1035
);
1036
1037
- const loadProjection = (threadId: ThreadId) =>
1037
+ const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>
1038
threadManagement
1039
.getThreadRecords(
1040
threadId,
@@ -1047,7 +1047,7 @@ const make = Effect.gen(function* () {
1047
"contextTransfers",
1048
"turnItems",
1049
],
1050
- { turnItemTypes: [], messageRoles: ["user"] },
1050
+ { turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] },
1051
)
1052
.pipe(
1053
Effect.mapError((error) =>
@@ -1106,6 +1106,7 @@ const make = Effect.gen(function* () {
1106
1107
const loadCaller = Effect.fn("OrchestratorMcpService.loadCaller")(function* (
1108
scope: McpInvocationScope,
1109
+ includeUserMessages = true,
1110
) {
1111
yield* requireCapability(scope);
1112
if (scope.thread === undefined) {
@@ -1114,7 +1115,7 @@ const make = Effect.gen(function* () {
1115
limits: clientModeCeiling(scope.client),
1116
} as const;
1117
}
1117
- const parent = yield* loadProjection(scope.thread.threadId);
1118
+ const parent = yield* loadProjection(scope.thread.threadId, includeUserMessages);
1119
return {
1120
parent,
1121
limits: {
@@ -1223,7 +1224,8 @@ const make = Effect.gen(function* () {
1224
scope: McpInvocationScope,
1225
threadId: ThreadId,
1226
) {
1226
- const { parent } = yield* loadCaller(scope);
1227
+ // Caller prompts are not used to read a timeline or acknowledge a child result.
1228
+ const { parent } = yield* loadCaller(scope, false);
1229
const shell = yield* threadManagement
1230
.getThreadShell(threadId)
1231
.pipe(Effect.mapError(threadManagementFailure));
@@ -1234,13 +1236,19 @@ const make = Effect.gen(function* () {
1236
`Thread ${threadId} is no longer available.`,
1237
);
1238
}
1237
- const target = yield* threadManagement
1238
- .getProjectThreadRecords({ projectId: shell.projectId, threadId }, [
1239
- "runs",
1240
- "runtimeRequests",
1241
- "contextTransfers",
1242
- ])
1243
- .pipe(Effect.mapError(threadManagementFailure));
1239
+ const input = { projectId: shell.projectId, threadId };
1240
+ // Transfers are needed only to acknowledge a caller's child-task result.
1241
+ const records =
1242
+ parent === undefined
1243
+ ? threadManagement
1244
+ .getProjectThreadRecords(input, ["runs", "runtimeRequests"])
1245
+ .pipe(Effect.map((target) => ({ ...target, contextTransfers: [] })))
1246
+ : threadManagement.getProjectThreadRecords(input, [
1247
+ "runs",
1248
+ "runtimeRequests",
1249
+ "contextTransfers",
1250
+ ]);
1251
+ const target = yield* records.pipe(Effect.mapError(threadManagementFailure));
1252
return { parent, target, shell } as const;
1253
});
1254
apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
+39
-6
@@ -1,3 +1,4 @@
1
+import * as NodeURL from "node:url";
2
import * as NodeServices from "@effect/platform-node/NodeServices";
3
import { describe, expect, it } from "@effect/vitest";
4
import {
@@ -22,6 +23,7 @@ import * as Layer from "effect/Layer";
23
import * as Option from "effect/Option";
24
import * as Schema from "effect/Schema";
25
import * as Stream from "effect/Stream";
26
+import * as Tracer from "effect/Tracer";
27
import { McpSchema, McpServer } from "effect/ai";
28
29
import { CodexOrchestratorReplayHarness } from "../orchestration-v2/Adapters/CodexAdapterV2.testkit.ts";
@@ -96,9 +98,7 @@ const readDelegatedTaskStatusTranscript = Effect.fn("readDelegatedTaskStatusTran
98
followupResult: string,
99
) {
100
const fs = yield* FileSystem.FileSystem;
99
- const text = yield* fs.readFileString(
100
- decodeURIComponent(delegatedTaskStatusTranscriptFile.pathname),
101
- );
101
+ const text = yield* fs.readFileString(NodeURL.fileURLToPath(delegatedTaskStatusTranscriptFile));
102
const transcript = yield* decodeProviderReplayNdjson(
103
text.replaceAll(queuedFollowupResult, followupResult),
104
);
@@ -315,11 +315,11 @@ describe("orchestrator MCP toolkit", () => {
315
capabilities: new Set(["orchestration"]),
316
issuedAt: 1,
317
};
318
- const invoke = (name: string, args: Record<string, unknown>) =>
318
+ const invoke = (name: string, args: Record<string, unknown>, scope = invocation) =>
319
server
320
.callTool({ name, arguments: args })
321
.pipe(
322
- Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
322
+ Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
323
Effect.provideService(McpSchema.McpServerClient, client),
324
);
325
@@ -596,11 +596,44 @@ describe("orchestrator MCP toolkit", () => {
596
expect(finalStatus.latestTerminalResultContextTransferId).not.toBe(
597
delegated.resultContextTransferId,
598
);
599
+ const transferReads: Array<string> = [];
600
+ const tracer = Tracer.make({
601
+ span(options) {
602
+ const span = new Tracer.NativeSpan(options);
603
+ const end = span.end.bind(span);
604
+ span.end = (endTime, exit) => {
605
+ end(endTime, exit);
606
+ const query = span.attributes.get("db.query.text");
607
+ if (
608
+ typeof query === "string" &&
609
+ /SELECT[\s\S]*FROM orchestration_v2_projection_context_transfers/i.test(query)
610
+ ) {
611
+ transferReads.push(query);
612
+ }
613
+ };
614
+ return span;
615
+ },
616
+ });
617
+ const parentlessRead = yield* invoke(
618
+ "t3_thread_read",
619
+ { threadId: delegated.childThreadId, limit: 100 },
620
+ { ...invocation, thread: undefined },
621
+ ).pipe(Effect.withTracer(tracer));
622
+ expect(parentlessRead.isError).not.toBe(true);
623
+ expect(transferReads).toHaveLength(0);
624
+ const afterParentlessRead = yield* orchestrator.getThreadProjection(parentThreadId);
625
+ expect(
626
+ afterParentlessRead.subagents.find((task) => task.id === queuedFollowup.taskId)
627
+ ?.completionDelivery?.state,
628
+ ).toBe("claimed");
629
+
630
const read = yield* invoke("t3_thread_read", {
631
threadId: delegated.childThreadId,
632
limit: 100,
602
- });
633
+ }).pipe(Effect.withTracer(tracer));
634
expect(read.isError).not.toBe(true);
635
+ expect(transferReads.length).toBeGreaterThan(0);
636
+ expect(read.structuredContent).toEqual(parentlessRead.structuredContent);
637
const afterRead = yield* orchestrator.getThreadProjection(parentThreadId);
638
expect(
639
afterRead.subagents.find((task) => task.id === queuedFollowup.taskId)