Skip to content

Commit bdac9ed

Browse files
committed
perf(server): skip unused caller prompts during thread reads
1 parent d58381a commit bdac9ed

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as SecretRequests from "../secrets/SecretRequests.ts";
2525
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
2626
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
2727
import * as RemoteDelegation from "../peer/RemoteDelegation.ts";
28+
import * as Orchestrator from "../orchestration-v2/Orchestrator.ts";
2829
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
2930
import type * as McpInvocationContext from "./McpInvocationContext.ts";
3031
import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
@@ -116,7 +117,8 @@ function makeRun(input: {
116117
};
117118
}
118119

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;
120122
const projection = {
121123
thread: baseThread({
122124
threadId: parentThreadId,
@@ -142,15 +144,24 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a
142144
Layer.mock(ThreadManagementService.ThreadManagementService)({
143145
getTimelinePage: () => Effect.succeed({ items: [], totalItems: 0, hasMore: false }),
144146
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}`),
148157
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+
),
154165
getProjectThreadRecords: (input) =>
155166
input.threadId === parentThreadId
156167
? Effect.succeed(projection)
@@ -180,6 +191,17 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a
180191
expect(result.thread.status).toBe("running");
181192
expect(result.thread.latestRunId).toBe(cancelledRunId);
182193
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}:`);
183205
}).pipe(Effect.provide(layer), Effect.runPromise);
184206
});
185207

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ const make = Effect.gen(function* () {
10091009
),
10101010
);
10111011

1012-
const loadProjection = (threadId: ThreadId) =>
1012+
const loadProjection = (threadId: ThreadId, includeUserMessages = true) =>
10131013
threadManagement
10141014
.getThreadRecords(
10151015
threadId,
@@ -1023,7 +1023,7 @@ const make = Effect.gen(function* () {
10231023
"contextTransfers",
10241024
"turnItems",
10251025
],
1026-
{ turnItemTypes: [], messageRoles: ["user"] },
1026+
{ turnItemTypes: [], messageRoles: includeUserMessages ? ["user"] : [] },
10271027
)
10281028
.pipe(
10291029
Effect.mapError((error) =>
@@ -1073,6 +1073,7 @@ const make = Effect.gen(function* () {
10731073

10741074
const loadCaller = Effect.fn("OrchestratorMcpService.loadCaller")(function* (
10751075
scope: McpInvocationScope,
1076+
includeUserMessages = true,
10761077
) {
10771078
yield* requireCapability(scope);
10781079
if (scope.thread === undefined) {
@@ -1081,7 +1082,7 @@ const make = Effect.gen(function* () {
10811082
limits: clientModeCeiling(scope.client),
10821083
} as const;
10831084
}
1084-
const parent = yield* loadProjection(scope.thread.threadId);
1085+
const parent = yield* loadProjection(scope.thread.threadId, includeUserMessages);
10851086
return {
10861087
parent,
10871088
limits: {
@@ -1190,7 +1191,8 @@ const make = Effect.gen(function* () {
11901191
scope: McpInvocationScope,
11911192
threadId: ThreadId,
11921193
) {
1193-
const { parent } = yield* loadCaller(scope);
1194+
// Caller prompts are not used to read a timeline or acknowledge a child result.
1195+
const { parent } = yield* loadCaller(scope, false);
11941196
const shell = yield* threadManagement
11951197
.getThreadShell(threadId)
11961198
.pipe(Effect.mapError(threadManagementFailure));

0 commit comments

Comments
 (0)