diff --git a/apps/server/src/cli/thread.test.ts b/apps/server/src/cli/thread.test.ts index a20331ee17d92..2cc487265ab73 100644 --- a/apps/server/src/cli/thread.test.ts +++ b/apps/server/src/cli/thread.test.ts @@ -140,6 +140,135 @@ it.effect( ), ); +it.effect("send --model and --effort switch the thread's model with the queued message", () => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const baseDir = yield* fs + .makeTempDirectoryScoped({ prefix: "t3-thread-send-model-" }) + .pipe(Effect.flatMap((directory) => fs.realPath(directory))); + yield* persistServerRuntimeState({ + path: path.join(baseDir, "userdata", "server-runtime.json"), + state: yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port: 3_773, + }), + }); + const sends: Array = []; + using _session = vi.spyOn(McpBridge, "mcpToolSession").mockReturnValue( + Effect.succeed({ + call: (tool, args) => { + if (tool === "t3_thread_read") { + return Effect.succeed({ + structuredContent: { thread: { providerInstanceId: "provider" } }, + }); + } + if (tool === "orchestrator_capabilities") { + return Effect.succeed({ + structuredContent: { + parentThreadId: null, + inheritedProviderInstanceId: null, + inheritedModel: null, + runtimeMode: "full-access", + interactionMode: "default", + providers: [ + { + providerInstanceId: "provider", + driverKind: args.search === "gpt-6.1-sol" ? "codex" : "claudeAgent", + displayName: null, + models: [ + { + id: args.search, + options: [ + { + id: args.search === "gpt-6.1-sol" ? "reasoningEffort" : "effort", + values: ["high"], + }, + ], + }, + ], + canRunChildTask: true, + canRunCrossProviderChildTask: true, + constraints: [], + }, + ], + features: { + appOwnedSubagents: true, + asyncPolling: true, + cancellation: true, + batchThreadCreation: true, + threadManagement: true, + incrementalThreadRead: true, + scheduledTasks: true, + maxBatchThreads: 10, + }, + }, + }); + } + if (tool !== "t3_thread_send") return Effect.die(`unexpected ${tool} call`); + sends.push(args); + return Effect.succeed({ + structuredContent: { + threadId, + messageId: "message-1", + runId, + status: "queued", + delivery: "queued", + }, + }); + }, + }), + ); + const run = (...args: ReadonlyArray) => + Command.runWith(threadCommand, { version: "0.0.0" })([ + "send", + threadId, + "go", + "--base-dir", + baseDir, + ...args, + ]); + + yield* run(); + yield* run("--model", "claude-sonnet-5-5", "--effort", "high"); + yield* run("--model", "claude-opus-5-5"); + yield* run("--model", "gpt-6.1-sol", "--effort", "high"); + + assert.deepStrictEqual(sends, [ + { threadId, message: "go", mode: "queue" }, + { + threadId, + message: "go", + mode: "queue", + modelSelection: { + model: "claude-sonnet-5-5", + options: [{ id: "effort", value: "high" }], + }, + }, + { threadId, message: "go", mode: "queue", modelSelection: { model: "claude-opus-5-5" } }, + { + threadId, + message: "go", + mode: "queue", + modelSelection: { + model: "gpt-6.1-sol", + options: [{ id: "reasoningEffort", value: "high" }], + }, + }, + ]); + }).pipe( + Effect.scoped, + Effect.provide( + Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + NetService.layer, + ConfigProvider.layer(ConfigProvider.fromEnv({ env: { T3CODE_PORT: "3773" } })), + ), + ), + ), +); + it.effect("does not sign in to whatever took over a dead server's port", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; diff --git a/apps/server/src/cli/thread.ts b/apps/server/src/cli/thread.ts index 3566b75e20773..ba6c2eef471f6 100644 --- a/apps/server/src/cli/thread.ts +++ b/apps/server/src/cli/thread.ts @@ -1,4 +1,5 @@ import { + OrchestratorMcpCapabilitiesResult, OrchestratorMcpFailure, OrchestratorMcpThreadListResult, OrchestratorMcpThreadReadResult, @@ -895,6 +896,16 @@ const threadSendCommand = Command.make("send", { ...projectLocationFlags, thread: threadArgument, message: messageArgument, + model: Flag.String("model").pipe( + Flag.withDescription( + "Switch the thread to this model, on its own provider, from this message on.", + ), + Flag.optional, + ), + effort: Flag.String("effort").pipe( + Flag.withDescription("Effort for --model, such as high. Pass with --model."), + Flag.optional, + ), wait: waitFlag, json: jsonFlag, }).pipe( @@ -904,15 +915,54 @@ const threadSendCommand = Command.make("send", { Command.withHandler((flags) => Effect.gen(function* () { const threadId = yield* decodeThreadId(flags.thread); + if (Option.isSome(flags.effort) && Option.isNone(flags.model)) { + return yield* new ThreadUsageError({ detail: "--effort goes with --model." }); + } const message = yield* readMessageArgument(flags.message); yield* withThreadTools( flags, Effect.fn("cli.thread.send")(function* (tools) { + let effortId = "effort"; + if (Option.isSome(flags.effort) && Option.isSome(flags.model)) { + const requestedModel = flags.model.value; + const { thread } = yield* tools.call( + "t3_thread_read", + { threadId, limit: 1, runLimit: 1 }, + Schema.Struct({ thread: Schema.Struct({ providerInstanceId: Schema.String }) }), + ); + const capabilities = yield* tools.call( + "orchestrator_capabilities", + { search: requestedModel }, + OrchestratorMcpCapabilitiesResult, + ); + const provider = capabilities.providers.find( + (provider) => provider.providerInstanceId === thread.providerInstanceId, + ); + const model = provider?.models.find((model) => model.id === requestedModel); + effortId = + model?.options?.find( + (option) => option.id === "effort" || option.id === "reasoningEffort", + )?.id ?? (provider?.driverKind === "codex" ? "reasoningEffort" : effortId); + } // Work already running when this is sent is not this message's to wait for. const followWakes = flags.wait ? yield* threadIsIdle(tools, threadId) : false; const sent = yield* tools.call( "t3_thread_send", - { threadId, message, mode: "queue" }, + { + threadId, + message, + mode: "queue", + ...(Option.isSome(flags.model) + ? { + modelSelection: { + model: flags.model.value, + ...(Option.isSome(flags.effort) + ? { options: [{ id: effortId, value: flags.effort.value }] } + : {}), + }, + } + : {}), + }, OrchestratorMcpThreadSendResult, ); if (flags.wait) { diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 802f3dfd761e3..bab88ba018283 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -4567,6 +4567,148 @@ describe("OrchestratorMcpService provider resolution", () => { }), ); }); + + describe("t3_thread_send modelSelection", () => { + const provider: ServerProvider = { + ...providerSnapshot({ + instanceId: codexInstanceId, + driver: ProviderDriverKind.make("codex"), + }), + models: [ + { + slug: "gpt-6", + name: "Sol", + aliases: ["sol"], + isCustom: false, + capabilities: { + optionDescriptors: [ + { + id: "effort", + label: "Reasoning", + type: "select", + options: [ + { id: "low", label: "Low" }, + { id: "high", label: "High" }, + ], + }, + ], + }, + }, + ], + }; + + const send = ( + input: Parameters< + OrchestratorMcpService.OrchestratorMcpService["Service"]["sendToThread"] + >[1], + // What a replayed receipt's run kept, when it differs from this request. + storedSelection?: { model: string; options?: ReadonlyArray<{ id: string; value: string }> }, + ) => + Effect.gen(function* () { + const sent: Array = []; + const dependencies = Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + Layer.mock(ThreadManagementService.ThreadManagementService)({ + getThreadRecords: () => Effect.succeed(parentProjection([])), + sendToThread: (request) => + Effect.sync(() => { + sent.push(request); + return { + // The stored run carries the selection the send applied. + run: { + id: RunId.make("run:mcp-send-model"), + status: "queued", + modelSelection: storedSelection ?? request.modelSelection, + }, + delivery: "queued", + } as never; + }), + }), + ServerSettings.layerTest(), + providerRegistryLayer([provider]), + adapterRegistryLayer([codexInstanceId]), + Layer.mock(ProjectService.ProjectService)({}), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(RemoteDelegation.RemoteDelegation)({}), + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), + ); + const result = yield* Effect.gen(function* () { + const service = yield* OrchestratorMcpService.OrchestratorMcpService; + return yield* service.sendToThread(scope, input).pipe(Effect.result); + }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(dependencies)))); + return { result, sent }; + }); + + it.effect("queues a switch on the thread's own provider and reports what applied", () => + Effect.gen(function* () { + const { result, sent } = yield* send({ + threadId: parentThreadId, + message: "continue", + modelSelection: { model: "sol", options: [{ id: "effort", value: "high" }] }, + }); + assert.isTrue(result._tag === "Success"); + const expected = { model: "gpt-6", options: [{ id: "effort", value: "high" }] }; + assert.deepEqual(sent[0]?.modelSelection, { instanceId: codexInstanceId, ...expected }); + assert.equal(sent[0]?.mode, "queue"); + if (result._tag === "Success") assert.deepEqual(result.success.modelSelection, expected); + }), + ); + + it.effect("reports the run's stored selection when a retry replays an older send", () => + Effect.gen(function* () { + const stored = { model: "gpt-6", options: [{ id: "effort", value: "low" }] }; + const { result } = yield* send( + { + threadId: parentThreadId, + message: "continue", + modelSelection: { model: "gpt-6", options: [{ id: "effort", value: "high" }] }, + }, + stored, + ); + assert.isTrue(result._tag === "Success"); + if (result._tag === "Success") assert.deepEqual(result.success.modelSelection, stored); + }), + ); + + it.effect.each(["steer", "restart"] as const)("refuses a switch with mode %s", (mode) => + Effect.gen(function* () { + const { result, sent } = yield* send({ + threadId: parentThreadId, + message: "continue", + mode, + modelSelection: { model: "gpt-6" }, + }); + assert.isTrue(result._tag === "Failure" && result.failure.code === "invalid_request"); + assert.deepEqual(sent, []); + }), + ); + + it.effect.each([ + { + name: "an unknown model", + model: "gpt-nope", + options: undefined, + code: "model_unavailable", + }, + { + name: "an unknown effort", + model: "gpt-6", + options: [{ id: "effort", value: "max" }], + code: "invalid_request", + }, + ] as const)("refuses $name and sends nothing", ({ model, options, code }) => + Effect.gen(function* () { + const { result, sent } = yield* send({ + threadId: parentThreadId, + message: "continue", + modelSelection: { model, ...(options === undefined ? {} : { options }) }, + }); + assert.isTrue(result._tag === "Failure" && result.failure.code === code); + assert.deepEqual(sent, []); + }), + ); + }); }); describe("OrchestratorMcpService thread lookup", () => { diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 95e269c64c97f..2dfd04e7ea8b3 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3466,7 +3466,48 @@ const make = Effect.gen(function* () { yield* assertLiveCallerForOtherThread(scope, parent, target); yield* assertTargetWithinLimits(limits, target.thread); - const mode = input.mode ?? "auto"; + if ( + input.modelSelection !== undefined && + (input.mode === "steer" || input.mode === "restart") + ) { + return yield* failure( + "invalid_request", + `modelSelection needs a send that starts or queues a turn, not mode:'${input.mode}'.`, + ); + } + // A switch cannot join a running turn, so `auto` queues instead of steering. + const mode = + input.modelSelection !== undefined && (input.mode ?? "auto") === "auto" + ? "queue" + : (input.mode ?? "auto"); + const providers = input.modelSelection === undefined ? [] : yield* loadProviders; + const switchedTo = + input.modelSelection === undefined + ? undefined + : (yield* resolveTargetRechecking({ + parent: target, + target: input.modelSelection, + providers, + })).modelSelection; + // Look the alias up in the catalog that accepted it: resolveTargetRechecking may have + // refreshed the instance, so the list loaded above can be stale. + const switchedProvider = + switchedTo === undefined + ? undefined + : (yield* loadProviders).find( + (provider) => provider.instanceId === switchedTo.instanceId, + ); + // A model given as an alias is stored under its slug, as the picker would store it. + const switchedSelection = + switchedTo === undefined + ? undefined + : { + ...switchedTo, + model: + (switchedProvider === undefined + ? undefined + : advertisedModel(switchedProvider, switchedTo.model)?.slug) ?? switchedTo.model, + }; const key = yield* requestKey(input.clientRequestId); const messageId = stableOperationMessageId({ scope, @@ -3492,6 +3533,7 @@ const make = Effect.gen(function* () { attachments: [], mode, ...(input.freshContext ? { freshContext: true } : {}), + ...(switchedSelection === undefined ? {} : { modelSelection: switchedSelection }), createdBy: "agent", creationSource: "mcp", }) @@ -3513,6 +3555,17 @@ const make = Effect.gen(function* () { ...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }), status: result.run.status, delivery: result.delivery, + ...(switchedSelection === undefined + ? {} + : { + // Report what the run stored, not what was requested. + modelSelection: { + model: result.run.modelSelection.model, + ...(result.run.modelSelection.options === undefined + ? {} + : { options: result.run.modelSelection.options }), + }, + }), } satisfies OrchestratorMcpThreadSendResult; }), waitForThread: Effect.fn("OrchestratorMcpService.waitForThread")(function* (scope, input) { diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 9c305c266f3dd..f453e3d7da483 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 5930, "codexAdditionalContextBytes": 7324, - "mcpToolListJsonBytes": 52145 + "mcpToolListJsonBytes": 52721 } diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index fa2770bb10cf7..e0aee8879a74f 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -446,6 +446,14 @@ The target runtime and interaction modes may not be broader than the caller's. Stable command and message IDs are derived from `clientRequestId` for idempotent retries. +`modelSelection` (`model`, optional `options`) switches the thread's model from +the new turn on, like the model picker. It names no provider instance, so a send +never moves a thread to another provider. It starts or queues a turn: `auto` +queues, and `steer` or `restart` are rejected. The model and options are +validated against the thread's provider; an alias is stored as its slug. The +thread takes the selection when the queued run starts, and later messages follow +it. The result's `modelSelection` is what applied. + ### `t3_thread_wait` Waits for a selected run to become `completed`, `failed`, `cancelled`, diff --git a/docs/user/outside-agents.md b/docs/user/outside-agents.md index 97bc0d385a3d7..212d3eaa18c5e 100644 --- a/docs/user/outside-agents.md +++ b/docs/user/outside-agents.md @@ -143,6 +143,7 @@ without a sign-in: ```sh id=$(t3 thread start . "Find why the tests are slow") t3 thread send "$id" "Now fix it" --wait +t3 thread send "$id" "Review it" --model gpt-6 --effort high t3 thread show "$id" t3 thread list ``` @@ -152,6 +153,8 @@ its repository; a project id also works. A new thread uses the project's default model and permission mode, and runs in the project folder unless you pass `--worktree `. `send` never interrupts: a busy thread gets the message as its next turn. Pass `-` as the message to read it from stdin. +`--model` (and `--effort`) switch the thread to that model of its own provider +from this message on. To choose the model, pass `--provider --model `. Add `--option id=value` for each model option, such as `--option effort=high`. diff --git a/packages/contracts/src/orchestratorMcp.test.ts b/packages/contracts/src/orchestratorMcp.test.ts index d2908fa42db8c..12b8520f67709 100644 --- a/packages/contracts/src/orchestratorMcp.test.ts +++ b/packages/contracts/src/orchestratorMcp.test.ts @@ -4,14 +4,35 @@ import * as Schema from "effect/Schema"; import { OrchestratorMcpDelegateTaskInput, OrchestratorMcpDelegateTaskResult, + OrchestratorMcpThreadSendInput, OrchestratorMcpThreadWaitResult, } from "./orchestratorMcp.ts"; const decodeDelegateTaskInput = Schema.decodeUnknownSync(OrchestratorMcpDelegateTaskInput); const decodeDelegateTaskResult = Schema.decodeUnknownSync(OrchestratorMcpDelegateTaskResult); +const decodeThreadSendInput = Schema.decodeUnknownSync(OrchestratorMcpThreadSendInput); const decodeThreadWaitResult = Schema.decodeUnknownSync(OrchestratorMcpThreadWaitResult); describe("orchestrator MCP contracts", () => { + it("decodes a send's model switch from the record or the array options form", () => { + const base = { threadId: "thread-1", message: "go", mode: "queue" } as const; + const fromRecord = decodeThreadSendInput({ + ...base, + modelSelection: { model: "claude-opus-5-5", options: { effort: "high" } }, + }); + const fromArray = decodeThreadSendInput({ + ...base, + modelSelection: { model: "claude-opus-5-5", options: [{ id: "effort", value: "high" }] }, + }); + expect(fromRecord.modelSelection).toEqual({ + model: "claude-opus-5-5", + options: [{ id: "effort", value: "high" }], + }); + expect(fromArray).toEqual(fromRecord); + expect(decodeThreadSendInput(base).modelSelection).toBeUndefined(); + expect(() => decodeThreadSendInput({ ...base, modelSelection: {} })).toThrow(); + }); + it("decodes cross-provider delegated task requests and durable results", () => { const request = decodeDelegateTaskInput({ task: "Inspect the workspace and report the result.", diff --git a/packages/contracts/src/orchestratorMcp.ts b/packages/contracts/src/orchestratorMcp.ts index d7c607744d4a7..39e2823a8b566 100644 --- a/packages/contracts/src/orchestratorMcp.ts +++ b/packages/contracts/src/orchestratorMcp.ts @@ -550,6 +550,23 @@ export const OrchestratorMcpThreadReadResult = Schema.Struct({ }); export type OrchestratorMcpThreadReadResult = typeof OrchestratorMcpThreadReadResult.Type; +/** + * A model switch for a send. It names no provider instance: the target thread's own + * instance is used, so a send never moves a thread to another provider. + */ +export const OrchestratorMcpThreadSendModelSelection = Schema.Struct({ + model: TrimmedNonEmptyString.annotate({ + description: "Slug or alias on the thread's provider.", + }), + options: Schema.optional( + OrchestratorMcpTargetOptions.annotate({ + description: "Such as {effort:'high'}. Overrides the thread's current ones. [] clears.", + }), + ), +}); +export type OrchestratorMcpThreadSendModelSelection = + typeof OrchestratorMcpThreadSendModelSelection.Type; + export const OrchestratorMcpThreadSendInput = Schema.Struct({ environmentId: OrchestratorMcpEnvironmentTarget, threadId: ThreadId, @@ -561,6 +578,12 @@ export const OrchestratorMcpThreadSendInput = Schema.Struct({ "Start a new provider session without the thread's earlier context, like /clear. Never steers.", }), ), + modelSelection: Schema.optional( + OrchestratorMcpThreadSendModelSelection.annotate({ + description: + "Switch the thread's model from the new turn on. `auto` queues; `steer` and `restart` are refused.", + }), + ), clientRequestId: Schema.optional(OrchestratorMcpClientRequestId), }); export type OrchestratorMcpThreadSendInput = typeof OrchestratorMcpThreadSendInput.Type; @@ -573,6 +596,13 @@ export const OrchestratorMcpThreadSendResult = Schema.Struct({ taskId: Schema.optional(NodeId), status: OrchestrationV2RunStatus, delivery: Schema.Literals(["started", "queued", "steered", "restarted"]), + /** Present when the send asked for a model switch: the model and options that applied. */ + modelSelection: Schema.optional( + Schema.Struct({ + model: Schema.String, + options: Schema.optional(Schema.Array(ProviderOptionSelection)), + }), + ), }); export type OrchestratorMcpThreadSendResult = typeof OrchestratorMcpThreadSendResult.Type;