Skip to content

Commit 48ed19c

Browse files
committed
fix(server): thread-bound scheduled tasks run on the thread's current provider
A scheduled task bound to a thread passed the model selection it was created with on every run, so a thread the user had switched to another provider was dragged back to the old one each time the task fired. Leave the selection out for thread-bound runs so they use whatever provider and model the thread is on now. Refs #17932
1 parent 397a461 commit 48ed19c

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import * as ServerSettings from "../serverSettings.ts";
5050
import * as ScheduledTasks from "../scheduledTasks/ScheduledTaskService.ts";
5151
import * as SecretRequests from "../secrets/SecretRequests.ts";
5252
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
53+
import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts";
5354
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
5455
import * as CommandReceiptStore from "./CommandReceiptStore.ts";
5556
import * as EffectOutbox from "./EffectOutbox.ts";
@@ -96,6 +97,18 @@ const adapter = {
9697
openSession: () => Effect.die("provider execution is disabled in launch tests"),
9798
} as ProviderAdapter.ProviderAdapterV2["Service"];
9899

100+
const claudeModelSelection = {
101+
instanceId: ProviderInstanceId.make("claudeAgent"),
102+
model: "claude-opus-5-5",
103+
} as const;
104+
const claudeAdapter = {
105+
instanceId: claudeModelSelection.instanceId,
106+
driver: ProviderDriverKind.make("claudeAgent"),
107+
getCapabilities: () => Effect.succeed(ClaudeProviderCapabilitiesV2),
108+
planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" as const }),
109+
openSession: () => Effect.die("provider execution is disabled in launch tests"),
110+
} as ProviderAdapter.ProviderAdapterV2["Service"];
111+
99112
interface HarnessOptions {
100113
readonly managedFolders?: Layer.Layer<ManagedProjectFolders.ManagedProjectFolders>;
101114
readonly createWorktree?: GitWorkflow.GitWorkflowService["Service"]["createWorktree"];
@@ -112,7 +125,7 @@ interface HarnessOptions {
112125

113126
function makeHarness(options: HarnessOptions = {}) {
114127
const layerDatabase = SqlitePersistence.layerMemory;
115-
const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([adapter]);
128+
const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([adapter, claudeAdapter]);
116129
const layerOrchestrator = ProviderReplayHarness.layerWithRegistry(
117130
{ name: "thread-launch" },
118131
layerRegistry,
@@ -365,6 +378,59 @@ it.effect.each(
365378
},
366379
);
367380

381+
it.effect("runs a thread-bound automation on the provider the thread uses now", () => {
382+
const harness = makeHarness();
383+
const layerScheduledTasks = ScheduledTasks.layer.pipe(
384+
Layer.provide(
385+
Layer.mergeAll(
386+
harness.layer,
387+
NodeCrypto.layer,
388+
Scheduler.layer,
389+
Layer.mock(SecretRequests.SecretRequests)({}),
390+
),
391+
),
392+
);
393+
return Effect.gen(function* () {
394+
const tasks = yield* ScheduledTasks.ScheduledTaskService;
395+
const launches = yield* ThreadLaunch.ThreadLaunchService;
396+
const threads = yield* ThreadManagement.ThreadManagementService;
397+
const launched = yield* launches.launch(
398+
launchInput({ command: "command:switched", thread: "thread:switched" }),
399+
);
400+
// The task is created while the thread is on Codex...
401+
const { task } = yield* tasks.upsert({
402+
id: ScheduledTaskId.make("scheduled-task:switched"),
403+
title: "Hourly check",
404+
prompt: "Check on the delegated workers.",
405+
enabled: false,
406+
schedule: { type: "interval", everyMs: 3_600_000 },
407+
projectId,
408+
threadId: launched.threadId,
409+
workspaceStrategy: { type: "root" },
410+
modelSelection,
411+
runtimeMode: "full-access",
412+
interactionMode: "default",
413+
createdBy: "agent",
414+
creationSource: "mcp",
415+
});
416+
// ...and the user then switches the thread to Claude.
417+
yield* threads.dispatch({
418+
type: "thread.model-selection.set",
419+
commandId: CommandId.make("command:switch-to-claude"),
420+
threadId: launched.threadId,
421+
modelSelection: claudeModelSelection,
422+
});
423+
424+
const result = yield* tasks.runNow({ id: task.id });
425+
assert.equal(result.task.lastRunStatus, "succeeded");
426+
const projection = yield* threads.getThreadProjection(launched.threadId);
427+
const message = projection.messages.find((item) => item.scheduledTaskId === task.id);
428+
const run = projection.runs.find((item) => item.id === message?.runId);
429+
assert.deepStrictEqual(run?.modelSelection, claudeModelSelection);
430+
assert.deepStrictEqual(projection.thread.modelSelection, claudeModelSelection);
431+
}).pipe(Effect.provide(Layer.mergeAll(harness.layer, layerScheduledTasks)));
432+
});
433+
368434
it.effect("retains automation and sender attribution while a message waits in the queue", () => {
369435
const harness = makeHarness({ runSetup: () => Effect.never });
370436
return Effect.gen(function* () {

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ export const layer = Layer.effect(
814814
}),
815815
)
816816
: yield* Effect.exit(
817+
// No modelSelection: the run follows the thread's current
818+
// provider and model, not the one the task was created with.
817819
threadManagement.sendToThread({
818820
projectId: active.projectId,
819821
commandId,
@@ -822,7 +824,6 @@ export const layer = Layer.effect(
822824
scheduledTaskId: active.id,
823825
text: prompt,
824826
attachments: [],
825-
modelSelection: active.modelSelection,
826827
// Scheduled prompts must not interrupt tools in the bound thread.
827828
mode: "queue",
828829
createdBy: active.createdBy,

0 commit comments

Comments
 (0)