Skip to content

Commit 6397a3b

Browse files
fix(server): close remaining MCP cross-thread write gaps
- t3_thread_interrupt now refuses a target thread that runs with broader modes than the caller, like t3_thread_send. - t3_thread_update and the pull-request tools need the calling thread's live run before they change another thread, like every other cross-thread write. - schedule_task checks a caller-named project exists before recording a task against it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8f02fe9 commit 6397a3b

7 files changed

Lines changed: 148 additions & 23 deletions

File tree

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,8 @@ const make = Effect.gen(function* () {
766766
const scheduledTasks = yield* ScheduledTaskService.ScheduledTaskService;
767767
const projects = yield* ProjectService.ProjectService;
768768

769-
/** A client has no thread to inherit a model from, so it falls back to the project default. */
770-
const projectDefaultModelSelection = (projectId: ProjectId) =>
769+
/** A caller-named project, which must exist before anything is recorded against it. */
770+
const requireProject = (projectId: ProjectId) =>
771771
projects.getById(projectId).pipe(
772772
Effect.mapError((error) =>
773773
failure("orchestration_error", `Unable to read project ${projectId}: ${error.message}`),
@@ -776,19 +776,24 @@ const make = Effect.gen(function* () {
776776
Option.match({
777777
onNone: () =>
778778
Effect.fail(failure("invalid_request", `Project ${projectId} was not found.`)),
779-
onSome: (project) =>
780-
project.defaultModelSelection === null
781-
? Effect.fail(
782-
failure(
783-
"invalid_request",
784-
`Project ${projectId} has no default model, so this caller cannot pick one for it.`,
785-
),
786-
)
787-
: Effect.succeed(project.defaultModelSelection),
779+
onSome: Effect.succeed,
788780
}),
789781
),
790782
);
791783

784+
/** A client has no thread to inherit a model from, so it falls back to the project default. */
785+
const projectDefaultModelSelection = (
786+
project: Effect.Success<ReturnType<typeof requireProject>>,
787+
) =>
788+
project.defaultModelSelection === null
789+
? Effect.fail(
790+
failure(
791+
"invalid_request",
792+
`Project ${project.id} has no default model, so this caller cannot pick one for it.`,
793+
),
794+
)
795+
: Effect.succeed(project.defaultModelSelection);
796+
792797
const requireCapability = (scope: McpInvocationScope) =>
793798
scope.capabilities.has("orchestration")
794799
? Effect.void
@@ -1261,6 +1266,7 @@ const make = Effect.gen(function* () {
12611266
Effect.gen(function* () {
12621267
const { parent, limits } = yield* loadCaller(scope);
12631268
const projectId = yield* resolveProjectTarget(parent, input.projectId);
1269+
const project = yield* requireProject(projectId);
12641270
// Binding means "wake this thread", which only a thread caller in that project has.
12651271
const bindToCurrentThread =
12661272
input.bindToCurrentThread ??
@@ -1277,7 +1283,7 @@ const make = Effect.gen(function* () {
12771283
);
12781284
}
12791285
const modelSelection =
1280-
parent?.thread.modelSelection ?? (yield* projectDefaultModelSelection(projectId));
1286+
parent?.thread.modelSelection ?? (yield* projectDefaultModelSelection(project));
12811287
const derivedTitle = input.prompt.split("\n")[0]?.trim() ?? "";
12821288
const title =
12831289
input.title ?? (derivedTitle.length > 0 ? derivedTitle.slice(0, 80) : "Scheduled task");
@@ -2004,7 +2010,10 @@ const make = Effect.gen(function* () {
20042010
}),
20052011
interruptThread: (scope, input) =>
20062012
Effect.gen(function* () {
2007-
const { target } = yield* loadScopedThread(scope, input.threadId);
2013+
const { limits, target } = yield* loadScopedThread(scope, input.threadId);
2014+
// Stopping another thread's work is a write: it must run within the caller's modes.
2015+
yield* resolveRuntimeMode(limits.runtimeMode, target.thread.runtimeMode);
2016+
yield* resolveInteractionMode(limits.interactionMode, target.thread.interactionMode);
20082017
const key = yield* requestKey(input.clientRequestId);
20092018
const result = yield* threadManagement
20102019
.interruptThread({

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,16 @@ describe("orchestrator MCP toolkit", () => {
632632
Layer.provide(registryLayer),
633633
Layer.provide(providerRegistryLayer),
634634
Layer.provide(scheduledTaskStubLayer),
635-
Layer.provide(Layer.mock(ProjectService.ProjectService)({})),
635+
Layer.provide(
636+
Layer.mock(ProjectService.ProjectService)({
637+
getById: (id) =>
638+
Effect.succeed(
639+
id === projectId
640+
? Option.some({ id, defaultModelSelection: null } as never)
641+
: Option.none(),
642+
),
643+
}),
644+
),
636645
Layer.provide(NodeServices.layer),
637646
);
638647

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ it.effect("refuses to change a thread that runs above the caller's modes", () =>
9090
projectId: "project",
9191
runtimeMode: "auto",
9292
interactionMode: "default",
93+
activeRunId: "run-live",
94+
archivedAt: null,
95+
providerInstanceId: "codex",
9396
deletedAt: null,
9497
},
9598
],
@@ -119,3 +122,47 @@ it.effect("refuses to change a thread that runs above the caller's modes", () =>
119122
expect(error.code).toBe("runtime_mode_escalation_denied");
120123
}),
121124
);
125+
126+
it.effect("refuses another thread's metadata to a thread caller whose run has ended", () =>
127+
Effect.gen(function* () {
128+
const otherThread = ThreadId.make("thread:metadata-other");
129+
const shells = new Map([
130+
[
131+
threadId,
132+
{
133+
id: threadId,
134+
projectId: "project",
135+
runtimeMode: "full-access",
136+
interactionMode: "default",
137+
activeRunId: null,
138+
archivedAt: null,
139+
providerInstanceId: "codex",
140+
deletedAt: null,
141+
},
142+
],
143+
[
144+
otherThread,
145+
{
146+
id: otherThread,
147+
projectId: "project",
148+
runtimeMode: "approval-required",
149+
interactionMode: "default",
150+
deletedAt: null,
151+
},
152+
],
153+
]);
154+
const error = yield* Effect.gen(function* () {
155+
const service = yield* ThreadMetadataMcp.ThreadMetadataMcpService;
156+
return yield* service.update(scope, {
157+
threadId: otherThread,
158+
action: "rename",
159+
title: "Renamed after the run ended",
160+
});
161+
}).pipe(
162+
Effect.provide(serviceLayer((id) => Effect.succeed((shells.get(id) ?? null) as never))),
163+
Effect.flip,
164+
);
165+
166+
expect(error.code).toBe("parent_not_active");
167+
}),
168+
);

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,20 @@ const make = Effect.gen(function* () {
185185
Effect.flatMap((caller) =>
186186
caller === null
187187
? Effect.fail(failure("thread_not_found", "The calling thread was not found."))
188-
: Effect.succeed({
189-
runtimeMode: caller.runtimeMode,
190-
interactionMode: caller.interactionMode,
191-
}),
188+
: // Like every other cross-thread write, a thread caller needs its live run.
189+
caller.archivedAt !== null ||
190+
caller.activeRunId === null ||
191+
caller.providerInstanceId !== scope.thread?.providerInstanceId
192+
? Effect.fail(
193+
failure(
194+
"parent_not_active",
195+
"The calling provider no longer owns an active thread run.",
196+
),
197+
)
198+
: Effect.succeed({
199+
runtimeMode: caller.runtimeMode,
200+
interactionMode: caller.interactionMode,
201+
}),
192202
),
193203
);
194204
yield* assertTargetWithinLimits(limits, shell);

‎apps/server/src/mcp/toolkits/core.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,45 @@ function scheduledTask(id: string, runtimeMode: "auto" | "full-access"): never {
406406
updatedAt: "2026-10-01T00:00:00.000Z",
407407
} as never;
408408
}
409+
410+
it.effect("a caller cannot interrupt a thread that runs above its own modes", () =>
411+
Effect.gen(function* () {
412+
const server = yield* McpServer.McpServer;
413+
const result = yield* server
414+
.callTool({ name: "t3_thread_interrupt", arguments: { threadId: "full-access-thread" } })
415+
.pipe(
416+
Effect.provideService(McpInvocationContext.McpInvocationContext, clientScope("auto")),
417+
Effect.provideService(McpSchema.McpServerClient, client),
418+
);
419+
expect(result.structuredContent).toMatchObject({ code: "runtime_mode_escalation_denied" });
420+
}).pipe(
421+
Effect.provide(
422+
McpHttpServer.OrchestratorToolkitRegistrationLive.pipe(
423+
Layer.provideMerge(McpServer.McpServer.layer),
424+
Layer.provide(NodeCrypto.layer),
425+
Layer.provide(
426+
Layer.mock(ThreadManagement.ThreadManagementService)({
427+
getThreadShell: () =>
428+
Effect.succeed({ projectId: "project-a", deletedAt: null } as never),
429+
getProjectThreadRecords: () =>
430+
Effect.succeed({
431+
thread: {
432+
id: ThreadId.make("full-access-thread"),
433+
projectId: "project-a",
434+
runtimeMode: "full-access",
435+
interactionMode: "default",
436+
deletedAt: null,
437+
},
438+
runs: [],
439+
} as never),
440+
interruptThread: () => Effect.die("interrupt must not dispatch above the ceiling"),
441+
}),
442+
),
443+
Layer.provide(Layer.mock(ProviderRegistry.ProviderRegistry)({})),
444+
Layer.provide(Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({})),
445+
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
446+
Layer.provide(Layer.mock(ProjectService.ProjectService)({})),
447+
),
448+
),
449+
),
450+
);

‎apps/server/src/mcp/toolkits/pullRequests/handlers.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,19 @@ const make = Effect.gen(function* () {
210210
}
211211
: yield* engine.getThreadShell(scope.thread.threadId).pipe(
212212
Effect.mapError((cause) => new Failure({ cause })),
213-
Effect.map((caller) => ({
214-
runtimeMode: caller?.runtimeMode ?? ("approval-required" as const),
215-
interactionMode: caller?.interactionMode ?? ("plan" as const),
216-
})),
213+
Effect.map((caller) =>
214+
// A thread caller changes other threads only while its own run is live.
215+
caller === null ||
216+
caller.archivedAt !== null ||
217+
caller.activeRunId === null ||
218+
caller.providerInstanceId !== scope.thread?.providerInstanceId
219+
? undefined
220+
: { runtimeMode: caller.runtimeMode, interactionMode: caller.interactionMode },
221+
),
217222
);
223+
if (limits === undefined) {
224+
return yield* new PullRequestThreadAboveLimitsError({ threadId: thread.id });
225+
}
218226
yield* assertTargetWithinLimits(limits, thread).pipe(
219227
Effect.mapError(() => new PullRequestThreadAboveLimitsError({ threadId: thread.id })),
220228
);

‎apps/server/src/mcp/toolkits/pullRequests/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class PullRequestThreadAboveLimitsError extends Schema.TaggedError<PullRe
101101
{ threadId: Schema.String },
102102
) {
103103
override get message(): string {
104-
return `Thread ${this.threadId} runs with broader permissions than this caller, so its pull requests cannot be changed from here.`;
104+
return `Thread ${this.threadId} cannot be changed from here: it runs with broader permissions than this caller, or the calling thread has no active run.`;
105105
}
106106
}
107107

0 commit comments

Comments
 (0)