Skip to content

Commit 0be4b7c

Browse files
committed
fix(server): t3_thread_configure refuses a model that delegate_task refuses
delegate_task refuses a model that the provider instance does not advertise, but t3_thread_configure dispatched any model. An agent could start a child on an advertised model and then move it onto the refused one. The advertised-model rule now lives in one helper, assertModelAdvertised, used by both tools. Configure applies it when the provider or model changes. An options-only change on the current model, an instance with no advertised models, and an unknown instance behave as before.
1 parent d145077 commit 0be4b7c

4 files changed

Lines changed: 105 additions & 11 deletions

File tree

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,20 @@ export const threadNotFound = Effect.fn("OrchestratorMcpService.threadNotFound")
251251
);
252252
});
253253

254+
/** An instance that advertises no models accepts any slug. */
255+
export const assertModelAdvertised = (
256+
provider: Pick<ServerProvider, "instanceId" | "models">,
257+
model: string,
258+
): Effect.Effect<void, OrchestratorMcpFailure> =>
259+
provider.models.length === 0 || provider.models.some((candidate) => candidate.slug === model)
260+
? Effect.void
261+
: Effect.fail(
262+
failure(
263+
"model_unavailable",
264+
`Model ${model} is not advertised by provider ${provider.instanceId}.`,
265+
),
266+
);
267+
254268
function threadManagementFailure(error: unknown): OrchestratorMcpFailure {
255269
if (!isThreadManagementError(error)) return failure("orchestration_error", errorMessage(error));
256270
switch (error._tag) {
@@ -1421,17 +1435,7 @@ const make = Effect.gen(function* () {
14211435
`Provider ${instanceId} has no model available for inheritance.`,
14221436
);
14231437
}
1424-
if (
1425-
requestedModel !== undefined &&
1426-
provider !== undefined &&
1427-
provider.models.length > 0 &&
1428-
!provider.models.some((candidate) => candidate.slug === requestedModel)
1429-
) {
1430-
return yield* failure(
1431-
"model_unavailable",
1432-
`Model ${requestedModel} is not advertised by provider ${instanceId}.`,
1433-
);
1434-
}
1438+
if (requestedModel !== undefined) yield* assertModelAdvertised(provider, requestedModel);
14351439

14361440
const requestedOptions = input.target?.options;
14371441
if (requestedOptions !== undefined) {

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ const decodeThreadReadResult = Schema.decodeUnknownEffect(OrchestratorMcpThreadR
7272
const layerThreadToolkit = McpHttpServer.layerThreadToolkit.pipe(
7373
Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})),
7474
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
75+
Layer.provide(
76+
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
77+
),
7578
);
7679

7780
const threadId = ThreadId.make("mcp-core-thread");
@@ -572,6 +575,77 @@ it.effect("a read-only client reads threads and is refused every write before it
572575
);
573576
const dispatched: Array<string> = [];
574577

578+
it.effect("t3_thread_configure refuses a model that delegate_task would refuse", () => {
579+
// The caller's thread runs codex on gpt-5 (McpToolAccessTestkit.liveThreadShell), which codex no longer advertises.
580+
const providers = [
581+
{ instanceId: "codex", models: [{ slug: "gpt-5.5", name: "gpt-5.5" }] },
582+
{ instanceId: "claude", models: [] },
583+
];
584+
const configured: Array<unknown> = [];
585+
return Effect.gen(function* () {
586+
const server = yield* McpServer.McpServer;
587+
const configure = (modelSelection: Record<string, unknown>) =>
588+
server
589+
.callTool({ name: "t3_thread_configure", arguments: { modelSelection } })
590+
.pipe(
591+
Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
592+
Effect.provideService(McpSchema.McpServerClient, client),
593+
);
594+
595+
const refused = yield* configure({ instanceId: "codex", model: "gpt-6" });
596+
expect(declaredFailure(refused)).toEqual({
597+
_tag: "OrchestratorMcpFailure",
598+
code: "model_unavailable",
599+
message: "Model gpt-6 is not advertised by provider codex.",
600+
});
601+
expect(configured).toEqual([]);
602+
603+
expect((yield* configure({ instanceId: "codex", model: "gpt-5.5" })).isError).toBe(false);
604+
expect((yield* configure({ instanceId: "claude", model: "any-slug" })).isError).toBe(false);
605+
const optionsOnly = {
606+
instanceId: "codex",
607+
model: "gpt-5",
608+
options: [{ id: "effort", value: "high" }],
609+
};
610+
expect((yield* configure(optionsOnly)).isError).toBe(false);
611+
expect(configured).toEqual([
612+
{ instanceId: "codex", model: "gpt-5.5" },
613+
{ instanceId: "claude", model: "any-slug" },
614+
optionsOnly,
615+
]);
616+
}).pipe(
617+
Effect.provide(
618+
McpHttpServer.layerThreadToolkit.pipe(
619+
Layer.provideMerge(McpServer.McpServer.layer),
620+
Layer.provide(NodeCrypto.layer),
621+
Layer.provide(Layer.mock(ThreadSearch.ThreadSearch)({})),
622+
Layer.provide(Layer.mock(ScheduledTaskService.ScheduledTaskService)({})),
623+
Layer.provide(
624+
Layer.mock(ProviderRegistry.ProviderRegistry)({
625+
getProviders: Effect.succeed(providers as never),
626+
}),
627+
),
628+
Layer.provide(
629+
Layer.mock(ThreadManagement.ThreadManagementService)({
630+
getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)),
631+
getProjectThreadRecords: ({ threadId: id }) =>
632+
Effect.succeed(
633+
McpToolAccessTestkit.idleThreadProjection(
634+
McpToolAccessTestkit.liveThreadShell(id),
635+
) as never,
636+
),
637+
dispatch: (command) =>
638+
Effect.sync(() => {
639+
if ("modelSelection" in command) configured.push(command.modelSelection);
640+
return { sequence: 7 } as never;
641+
}),
642+
}),
643+
),
644+
),
645+
),
646+
);
647+
});
648+
575649
it.effect("refuses act-as-caller tools to a client caller", () =>
576650
Effect.gen(function* () {
577651
const server = yield* McpServer.McpServer;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { modelSelectionCommandType } from "@t3tools/shared/model";
1212
import { formatThreadLink } from "@t3tools/shared/threadLinks";
1313

1414
import * as McpToolAccess from "../../McpToolAccess.ts";
15+
import * as OrchestrationMcp from "../../OrchestratorMcpService.ts";
1516
import {
1617
dispatchFailure,
1718
newCommandId,
@@ -20,6 +21,7 @@ import {
2021
unavailable,
2122
} from "../../threadAccess.ts";
2223
import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
24+
import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
2325
import * as ScheduledTasks from "../../../scheduledTasks/ScheduledTaskService.ts";
2426
import { queuedRunsInDeliveryOrder } from "../../../orchestration-v2/QueuedRunOrder.ts";
2527
import { ThreadToolkit } from "./tools.ts";
@@ -206,6 +208,18 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
206208
threads,
207209
projection: { thread },
208210
} = yield* readThread(input.threadId);
211+
const { instanceId, model } = modelSelection;
212+
// Keeping the current model only changes options, as an inherited model does in delegate_task.
213+
if (
214+
instanceId !== thread.modelSelection.instanceId ||
215+
model !== thread.modelSelection.model
216+
) {
217+
const registry = yield* ProviderRegistry.ProviderRegistry;
218+
const providers = yield* registry.getProviders;
219+
const provider = providers.find((candidate) => candidate.instanceId === instanceId);
220+
if (provider !== undefined)
221+
yield* OrchestrationMcp.assertModelAdvertised(provider, model);
222+
}
209223
const type = modelSelectionCommandType(thread.providerInstanceId, modelSelection);
210224
const result = yield* threads
211225
.dispatch({ type, threadId: thread.id, commandId: yield* newCommandId(), modelSelection })

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import * as Schema from "effect/Schema";
2424
import { Tool, Toolkit } from "effect/ai";
2525

2626
import * as ThreadSearch from "../../../orchestration-v2/ThreadSearch.ts";
27+
import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
2728
import * as ScheduledTaskService from "../../../scheduledTasks/ScheduledTaskService.ts";
2829
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
2930
import * as McpInvocationContext from "../../McpInvocationContext.ts";
@@ -154,6 +155,7 @@ const ThreadConfigureTool = Tool.make("t3_thread_configure", {
154155
modelSelection: Schema.optional(ModelSelection),
155156
}),
156157
success: Schema.Union([threadConfiguration, OrchestrationV2DispatchCommandResult]),
158+
dependencies: [...commandTool.dependencies, ProviderRegistry.ProviderRegistry],
157159
}).annotate(Tool.Destructive, true);
158160

159161
const ThreadTransferTool = Tool.make("t3_thread_transfer", {

0 commit comments

Comments
 (0)