Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/server/src/mcp/McpToolAccess.race.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import * as McpHttpServer from "./McpHttpServer.ts";
import * as McpInvocationContext from "./McpInvocationContext.ts";
import * as McpToolAccess from "./McpToolAccess.ts";
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import * as ProviderRegistryMock from "../provider/testUtils/providerRegistryMock.ts";

const instanceId = ProviderInstanceId.make("codex");
const adapter = {
Expand Down Expand Up @@ -182,6 +183,7 @@ it.layer(layerOrchestrator)("writesThreads against a mode raise", (it) => {
}).pipe(
Effect.provide(
ThreadManagement.layer.pipe(
Layer.provide(ProviderRegistryMock.layer()),
Layer.provide(ServerSettings.layerTest()),
Layer.provide(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))),
),
Expand Down
123 changes: 123 additions & 0 deletions apps/server/src/mcp/OrchestratorMcpService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
import { resolveRuntimeMode } from "./delegatedTaskModes.ts";
import type { ProjectionRecordFilter } from "../orchestration-v2/ProjectionStore.ts";
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import * as ProviderRegistryMock from "../provider/testUtils/providerRegistryMock.ts";

const decodeTarget = Schema.decodeUnknownEffect(OrchestratorMcpTarget);

Expand Down Expand Up @@ -2573,6 +2574,126 @@ describe("OrchestratorMcpService provider resolution", () => {
}),
);

it.effect("refuses a Grok model on claudeAgent and accepts a model that instance serves", () =>
Effect.gen(function* () {
const claudeInstanceId = ProviderInstanceId.make("claudeAgent");
const claude = providerSnapshot({
instanceId: claudeInstanceId,
driver: ProviderDriverKind.make("claudeAgent"),
model: "claude-opus",
});
const dispatched = yield* Ref.make<
ReadonlyArray<{ modelSelection?: { instanceId: string; model: string } }>
>([]);
let delegated = false;
const task = {
id: taskId,
threadId: parentThreadId,
runId: parentRunId,
parentNodeId,
origin: "app_owned",
createdBy: "agent",
driver: ProviderDriverKind.make("claudeAgent"),
providerInstanceId: claudeInstanceId,
providerThreadId: null,
childThreadId,
nativeTaskRef: null,
prompt: "Summarize the diff.",
title: null,
model: "claude-opus",
status: "running",
result: null,
startedAt: null,
completedAt: null,
};
const layerDependencies = Layer.mergeAll(
NodeServices.layer,
NodeCrypto.layer,
Layer.mock(ThreadManagementService.ThreadManagementService)({
hasReceipt: () => Effect.succeed(false),
getThreadRecords: (threadId) =>
Effect.succeed(
threadId === parentThreadId
? parentProjection(delegated ? [task] : [])
: childProjection,
),
dispatch: (command) =>
Ref.update(dispatched, (commands) => [...commands, command as never]).pipe(
Effect.andThen(
Effect.sync(() => {
delegated = true;
}),
),
Effect.as({
sequence: 1,
storedEvents: [
{
sequence: 1,
commandId: null,
event: { type: "subagent.updated", payload: task },
},
],
} as never),
),
}),
ServerSettings.layerTest(),
providerRegistryLayer([
providerSnapshot({
instanceId: codexInstanceId,
driver: ProviderDriverKind.make("codex"),
model: "gpt-5.4",
}),
{
...claude,
models: [
{
slug: "claude-opus",
name: "Opus",
aliases: ["Opus"],
isCustom: false,
capabilities: null,
},
],
},
]),
adapterRegistryLayer([codexInstanceId, claudeInstanceId]),
Layer.mock(ProjectService.ProjectService)({}),
Layer.mock(SecretRequests.SecretRequests)({}),
Layer.mock(RemoteDelegation.RemoteDelegation)({}),
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
);

yield* Effect.gen(function* () {
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
const refused = yield* service
.delegateTask(scope, {
task: "Summarize the diff.",
target: { providerInstanceId: claudeInstanceId, model: "grok/grok-4.7" },
mode: "async",
clientRequestId: "delegate-grok-on-claude",
})
.pipe(Effect.flip);
assert.equal(refused.code, "model_unavailable");
assert.include(refused.message, "grok/grok-4.7");
assert.include(refused.message, "claudeAgent");
assert.include(refused.message, "claude-opus");
assert.equal((yield* Ref.get(dispatched)).length, 0);

const accepted = yield* service.delegateTask(scope, {
task: "Summarize the diff.",
target: { providerInstanceId: claudeInstanceId, model: "Opus" },
mode: "async",
clientRequestId: "delegate-opus-on-claude",
});
assert.equal(accepted.status, "running");
const commands = yield* Ref.get(dispatched);
assert.equal(commands.length, 1);
assert.equal(commands[0]?.modelSelection?.instanceId, claudeInstanceId);
assert.equal(commands[0]?.modelSelection?.model, "Opus");
}).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies))));
}),
);

it.effect("driver-only delegation skips an instance the project turned off", () =>
Effect.gen(function* () {
const altInstanceId = ProviderInstanceId.make("codex-alt");
Expand Down Expand Up @@ -2835,6 +2956,7 @@ describe("OrchestratorMcpService provider resolution", () => {
},
});
const layerThreads = ThreadManagementService.layer.pipe(
Layer.provide(ProviderRegistryMock.layer()),
Layer.provideMerge(layerOrchestrator),
Layer.provide(
LegacyV1ThreadImporter.layer.pipe(
Expand Down Expand Up @@ -3017,6 +3139,7 @@ describe("OrchestratorMcpService provider resolution", () => {
providerInstances: { [codexInstanceId]: { driver: "codex", enabled: true } },
});
const layerThreads = ThreadManagementService.layer.pipe(
Layer.provide(ProviderRegistryMock.layer()),
Layer.provideMerge(layerOrchestrator),
Layer.provide(
LegacyV1ThreadImporter.layer.pipe(
Expand Down
51 changes: 15 additions & 36 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import { isSnoozed } from "../orchestration-v2/ThreadSettlementService.ts";
import { userFacingDispatchErrorMessage } from "../orchestration-v2/UserFacingErrors.ts";
import * as ProjectService from "../project/ProjectService.ts";
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
import { findServedModel, unservedModelMessage } from "../provider/servedModel.ts";
import * as ScheduledTaskService from "../scheduledTasks/ScheduledTaskService.ts";
import * as ServerSettings from "../serverSettings.ts";
import {
Expand Down Expand Up @@ -369,42 +370,8 @@ function listCapabilityModels(input: {
return { listed, omitted: visible.length - listed.length };
}

/** Names advertised slugs to retry with: same last `/` segment first, else a short list. */
function advertisedModelsHint(models: ReadonlyArray<ServerProviderModel>, requested: string) {
const lastSegment = (slug: string) => slug.slice(slug.lastIndexOf("/") + 1).toLowerCase();
const wanted = lastSegment(requested);
const close = models.filter((model) => lastSegment(model.slug) === wanted);
if (close.length > 0) {
return `Did you mean ${close
.slice(0, LISTED_MODEL_LIMIT)
.map((model) => model.slug)
.join(", ")}?`;
}
const listed = models
.toSorted((a, b) => Number(a.isLegacy ?? false) - Number(b.isLegacy ?? false))
.slice(0, LISTED_MODEL_LIMIT);
const omitted = models.length - listed.length;
return `Advertised models: ${listed.map((model) => model.slug).join(", ")}${
omitted > 0 ? ` and ${omitted} more; orchestrator_capabilities with search finds them` : ""
}.`;
}

/** Option ids and allowed values are all a delegating agent needs; labels are UI copy. */
function capabilityModelOption(descriptor: ProviderOptionDescriptor): OrchestratorMcpModelOption {
const defaultValue = getProviderOptionCurrentValue(descriptor);
return {
id: descriptor.id,
values:
descriptor.type === "boolean" ? [false, true] : descriptor.options.map((choice) => choice.id),
...(defaultValue === undefined ? {} : { default: defaultValue }),
};
}

const advertisedModel = (provider: ServerProvider, model: string) =>
provider.models.find((candidate) => candidate.slug === model) ??
provider.models.find((candidate) =>
candidate.aliases?.some((alias) => alias.toLowerCase() === model.toLowerCase()),
);
findServedModel(provider.models, model);

const withV = (version: string) => (version.startsWith("v") ? version : `v${version}`);

Expand All @@ -421,12 +388,24 @@ export const requireAdvertisedModel = (provider: ServerProvider, model: string)
failure(
"model_unavailable",
updateRequired === undefined
? `Model ${model} is not advertised by provider ${provider.instanceId}. ${advertisedModelsHint(provider.models, model)}`
? (unservedModelMessage(provider.instanceId, model, provider.models) ??
`Model ${model} is not advertised by provider ${provider.instanceId}.`)
: `Model ${model} needs provider ${provider.instanceId} at ${withV(updateRequired.minVersion)} or newer, but the detected CLI is ${detectedVersion === null ? "an unknown version" : withV(detectedVersion)}. Ask the user to update it or point the provider's binary path at a newer install.`,
),
);
};

/** Option ids and allowed values are all a delegating agent needs; labels are UI copy. */
function capabilityModelOption(descriptor: ProviderOptionDescriptor): OrchestratorMcpModelOption {
const defaultValue = getProviderOptionCurrentValue(descriptor);
return {
id: descriptor.id,
values:
descriptor.type === "boolean" ? [false, true] : descriptor.options.map((choice) => choice.id),
...(defaultValue === undefined ? {} : { default: defaultValue }),
};
}

function providerConstraints(
provider: ServerProvider | undefined,
supportsOrchestrationV2: boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ describe("orchestrator MCP toolkit", () => {
).pipe(
Layer.provide(
ThreadManagementService.layer.pipe(
Layer.provide(ProviderRegistryMock.layer()),
Layer.provide(layerOrchestrator),
Layer.provide(ServerSettings.layerTest()),
Layer.provide(
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/mcp/linkOrigin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import * as PeerForwarding from "../peer/PeerForwarding.ts";
import * as ThreadSearch from "../orchestration-v2/ThreadSearch.ts";
import * as PeerLinkRequests from "../peer/PeerLinkRequests.ts";
import * as PeerLinks from "../peer/PeerLinks.ts";
import * as ProviderRegistryMock from "../provider/testUtils/providerRegistryMock.ts";

// A linked environment's session drives this one's real orchestrator through
// its real T3 tools. What the link starts carries its origin, and the link
Expand All @@ -73,6 +74,7 @@ const layerOrchestrator = Layer.mergeAll(
),
);
const layerOrchestration = ThreadManagement.layer.pipe(
Layer.provide(ProviderRegistryMock.layer()),
Layer.provide(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))),
Layer.provideMerge(Layer.merge(layerOrchestrator, ServerSettings.layerTest())),
);
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/mcp/toolkits/project/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ it.effect("a launch with a model its provider does not advertise creates no thre

for (const [instanceId, model] of [
["claudeAgent", "claude-unadvertised"],
["claudeAgent", "grok/grok-4.7"],
["codex", "opus"],
] as const) {
expect(yield* launchWith(instanceId, model)).toMatchObject({
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/mcp/toolkits/project/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,12 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
});
}
// The administrator's ceiling refuses the mode; say so rather than "unavailable".
if (error._tag === "ThreadLaunchError" && error.operation === "reject-model") {
return new OrchestratorMcpFailure({
code: "model_unavailable",
message: error.message,
});
}
if (error._tag === "ThreadLaunchError" && isAboveCeilingError(error.cause)) {
return new OrchestratorMcpFailure({
code: "invalid_request",
Expand Down
Loading
Loading