Skip to content

Commit 9d48dc6

Browse files
fix(server): schedule_task lets agents pick the model and reports it
schedule_task had no target input: it always copied the calling thread's model (or the project default) and kept the stored model on update, and the returned task never said what it runs with. schedule_task now takes the same `target` (provider, model, options) as the other work-starting tools, resolved and validated through resolveTarget against the caller's or project's selection on create and the stored selection on update. Task details now include providerInstanceId, model and options. Fixes #16833 AI-assisted; verified with the tests and tools listed above.
1 parent 16c43f6 commit 9d48dc6

4 files changed

Lines changed: 68 additions & 6 deletions

File tree

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ function scheduledTaskSummary(task: ScheduledTask, mayRun: boolean): Orchestrato
269269
projectId: task.projectId,
270270
boundThreadId: task.threadId,
271271
freshSession: scheduledTaskThreadMode(task) === "fresh_session",
272+
providerInstanceId: task.modelSelection.instanceId,
273+
model: task.modelSelection.model,
274+
options: task.modelSelection.options ?? [],
272275
schedule: task.schedule,
273276
nextRunAt: task.nextRunAt,
274277
lastRunStatus: task.lastRunStatus,
@@ -1330,7 +1333,12 @@ const make = Effect.gen(function* () {
13301333
};
13311334

13321335
const resolveTarget = Effect.fn("OrchestratorMcpService.resolveTarget")(function* (input: {
1333-
readonly parent: Pick<OrchestrationV2ThreadProjection, "thread">;
1336+
readonly parent: {
1337+
readonly thread: Pick<
1338+
OrchestrationV2ThreadProjection["thread"],
1339+
"modelSelection" | "projectId"
1340+
>;
1341+
};
13341342
readonly target: OrchestratorMcpTarget | undefined;
13351343
readonly providers: ReadonlyArray<ServerProvider>;
13361344
/** Receipt replays bypass current settings because they start no work. Dispatch must reuse the checked command ID. */
@@ -1465,6 +1473,30 @@ const make = Effect.gen(function* () {
14651473
};
14661474
});
14671475

1476+
const resolveScheduledTaskSelection = Effect.fn(
1477+
"OrchestratorMcpService.resolveScheduledTaskSelection",
1478+
)(function* (
1479+
project: Effect.Success<ReturnType<typeof requireProject>>,
1480+
inherited: ModelSelection | null,
1481+
target: OrchestratorMcpScheduleTaskInput["target"],
1482+
) {
1483+
if (target === undefined) {
1484+
return inherited ?? (yield* projectDefaultModelSelection(project));
1485+
}
1486+
const modelSelection =
1487+
inherited ??
1488+
project.defaultModelSelection ??
1489+
(target.providerInstanceId !== undefined && target.model !== undefined
1490+
? { instanceId: target.providerInstanceId, model: target.model }
1491+
: yield* projectDefaultModelSelection(project));
1492+
const resolved = yield* resolveTargetRechecking({
1493+
parent: { thread: { modelSelection, projectId: project.id } },
1494+
target,
1495+
providers: yield* loadProviders,
1496+
});
1497+
return resolved.modelSelection;
1498+
});
1499+
14681500
const requestKey = (clientRequestId: string | undefined): Effect.Effect<string> =>
14691501
clientRequestId === undefined ? random.uuidV4() : Effect.succeed(clientRequestId);
14701502

@@ -2027,8 +2059,11 @@ const make = Effect.gen(function* () {
20272059
: "bindToCurrentThread binds to this thread, which belongs to a different project.",
20282060
);
20292061
}
2030-
const modelSelection =
2031-
parent?.thread.modelSelection ?? (yield* projectDefaultModelSelection(project));
2062+
const modelSelection = yield* resolveScheduledTaskSelection(
2063+
project,
2064+
parent?.thread.modelSelection ?? null,
2065+
input.target,
2066+
);
20322067
const derivedTitle = input.prompt.split("\n")[0]?.trim() ?? "";
20332068
const title =
20342069
input.title ?? (derivedTitle.length > 0 ? derivedTitle.slice(0, 80) : "Scheduled task");
@@ -2150,7 +2185,14 @@ const make = Effect.gen(function* () {
21502185
? "new_thread"
21512186
: "continue",
21522187
workspaceStrategy,
2153-
modelSelection: existing.modelSelection,
2188+
modelSelection:
2189+
input.target === undefined
2190+
? existing.modelSelection
2191+
: yield* resolveScheduledTaskSelection(
2192+
yield* requireProject(existing.projectId),
2193+
existing.modelSelection,
2194+
input.target,
2195+
),
21542196
runtimeMode: existing.runtimeMode,
21552197
interactionMode: existing.interactionMode,
21562198
createdBy: existing.createdBy,

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ const handlers = {
102102
{ refused: "schedule tasks in this environment" },
103103
),
104104
McpToolAccess.writes(
105-
({ scheduledTaskId, prompt, title, schedule, enabled, freshSession, bindToCurrentThread }) =>
105+
({
106+
scheduledTaskId,
107+
prompt,
108+
title,
109+
schedule,
110+
enabled,
111+
freshSession,
112+
bindToCurrentThread,
113+
target,
114+
}) =>
106115
Effect.gen(function* () {
107116
if (scheduledTaskId === undefined) return yield* Effect.die("unreachable");
108117
const scope = yield* McpInvocationContext.McpInvocationContext;
@@ -115,6 +124,7 @@ const handlers = {
115124
enabled,
116125
freshSession,
117126
bindToCurrentThread,
127+
target,
118128
});
119129
}),
120130
),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"claudeSystemAppendBytes": 5597,
33
"codexAdditionalContextBytes": 7327,
4-
"mcpToolListJsonBytes": 48131
4+
"mcpToolListJsonBytes": 48770
55
}

‎packages/contracts/src/orchestratorMcp.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Effect from "effect/Effect";
22
import * as Schema from "effect/Schema";
33
import * as SchemaTransformation from "effect/SchemaTransformation";
4+
import * as Struct from "effect/Struct";
45

56
import { AuthMcpClientAccess } from "./auth.ts";
67
import {
@@ -688,6 +689,11 @@ export const OrchestratorMcpScheduleTaskInput = Schema.Struct({
688689
"Runs share one thread but each starts a fresh session without earlier runs' context. Unbound: the first run makes the thread.",
689690
}),
690691
),
692+
target: Schema.optional(
693+
OrchestratorMcpTarget.mapFields(Struct.omit(["environmentId", "projectId"])).annotate({
694+
description: "Provider, model and options each run uses. Default: yours, or the project's.",
695+
}),
696+
),
691697
clientRequestId: Schema.optional(OrchestratorMcpClientRequestId),
692698
});
693699
export type OrchestratorMcpScheduleTaskInput = typeof OrchestratorMcpScheduleTaskInput.Type;
@@ -710,6 +716,9 @@ export const OrchestratorMcpScheduledTask = Schema.Struct({
710716
projectId: ProjectId,
711717
boundThreadId: Schema.NullOr(ThreadId),
712718
freshSession: Schema.Boolean,
719+
providerInstanceId: ProviderInstanceId,
720+
model: Schema.String,
721+
options: Schema.Array(ProviderOptionSelection),
713722
schedule: ScheduledTaskSchedule,
714723
nextRunAt: Schema.NullOr(IsoDateTime),
715724
lastRunStatus: ScheduledTaskRunStatus,
@@ -753,6 +762,7 @@ export const OrchestratorMcpUpdateScheduledTaskInput = Schema.Struct({
753762
enabled: Schema.optional(Schema.Boolean),
754763
bindToCurrentThread: Schema.optional(Schema.Boolean),
755764
freshSession: Schema.optional(Schema.Boolean),
765+
target: OrchestratorMcpScheduleTaskInput.fields.target,
756766
});
757767
export type OrchestratorMcpUpdateScheduledTaskInput =
758768
typeof OrchestratorMcpUpdateScheduledTaskInput.Type;

0 commit comments

Comments
 (0)