Skip to content

Commit 9507698

Browse files
fix(server): configure switches a thread whose run cannot be restarted
When the active run is waiting or starting, configure now sends the ordinary selection command, as main does, instead of rejecting the change. Only a running turn gets the continuation restart. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 14ab951 commit 9507698

3 files changed

Lines changed: 81 additions & 8 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
230230
Effect.mapError((error) => {
231231
switch (error._tag) {
232232
case "ThreadManagementThreadArchivedError":
233-
case "ThreadManagementNoSteerableRunError":
234233
return new OrchestratorMcpFailure({
235234
code: "thread_not_sendable",
236235
message: error.message,

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,81 @@ const layerDispatchChokepointTest =
111111
Layer.provideMerge(layerReceiptsTest),
112112
);
113113

114+
it.effect("configuring a thread whose active run cannot be steered switches it like main", () => {
115+
const projectId = ProjectId.make("project:thread-management:configure-waiting");
116+
const threadId = ThreadId.make("thread:thread-management:configure-waiting");
117+
const current = { instanceId: ProviderInstanceId.make("codex_work"), model: "test" };
118+
const target = { instanceId: ProviderInstanceId.make("codex_other"), model: "test" };
119+
const dispatched: Array<string> = [];
120+
const layerTest = ThreadManagementService.layer.pipe(
121+
Layer.provide(Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({})),
122+
Layer.provide(
123+
Layer.mock(ProviderSwitchService.ProviderSwitchServiceV2)({
124+
plan: () =>
125+
Effect.succeed({
126+
instanceChanged: true,
127+
releaseProviderSessionIds: [ProviderSessionId.make("session:configure-waiting")],
128+
transition: { type: "restart_and_resume" },
129+
}),
130+
}),
131+
),
132+
Layer.provide(
133+
Layer.mock(Orchestrator.OrchestratorV2)({
134+
getThreadRecords: () =>
135+
Effect.succeed({
136+
thread: {
137+
id: threadId,
138+
projectId,
139+
providerInstanceId: current.instanceId,
140+
modelSelection: current,
141+
archivedAt: null,
142+
deletedAt: null,
143+
},
144+
runs: [
145+
{
146+
id: RunId.make("run:configure-waiting"),
147+
status: "waiting",
148+
ordinal: 1,
149+
modelSelection: current,
150+
activeAttemptId: null,
151+
},
152+
],
153+
providerTurns: [],
154+
providerSessions: [],
155+
providerThreads: [],
156+
} as unknown as OrchestrationV2ThreadProjection),
157+
dispatch: (command) =>
158+
Effect.sync(() => {
159+
dispatched.push(command.type);
160+
return { sequence: 1, storedEvents: [] } as never;
161+
}),
162+
}),
163+
),
164+
Layer.provide(
165+
ServerSettings.layerTest({
166+
providerInstances: {
167+
[current.instanceId]: { driver: "codex", enabled: true },
168+
[target.instanceId]: { driver: "codex", enabled: true },
169+
},
170+
}),
171+
),
172+
Layer.provideMerge(layerReceiptsTest),
173+
);
174+
175+
return Effect.gen(function* () {
176+
const threads = yield* ThreadManagementService.ThreadManagementService;
177+
yield* threads.configureModelSelection({
178+
projectId,
179+
threadId,
180+
commandId: CommandId.make("command:thread-management:configure-waiting"),
181+
modelSelection: target,
182+
createdBy: "agent",
183+
creationSource: "mcp",
184+
});
185+
expect(dispatched).toEqual(["provider.switch"]);
186+
}).pipe(Effect.provide(layerTest));
187+
});
188+
114189
it.effect(
115190
"the dispatch chokepoint refuses a continuation message.dispatch on a project-disabled instance before reaching the orchestrator",
116191
() => {

‎apps/server/src/orchestration-v2/ThreadManagementService.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -855,20 +855,19 @@ const make = Effect.gen(function* () {
855855
Effect.map((plan) => plan.releaseProviderSessionIds.length > 0),
856856
Effect.orElseSucceed(() => false),
857857
);
858-
if (activeRun === undefined || runMatches || !releasesSession) {
858+
if (
859+
activeRun === undefined ||
860+
runMatches ||
861+
!releasesSession ||
862+
latestSteerableRun(target)?.id !== activeRun.id
863+
) {
859864
return yield* dispatch({
860865
type: modelSelectionCommandType(target.thread.providerInstanceId, input.modelSelection),
861866
commandId: input.commandId,
862867
threadId: input.threadId,
863868
modelSelection: input.modelSelection,
864869
});
865870
}
866-
if (latestSteerableRun(target)?.id !== activeRun.id) {
867-
return yield* new ThreadManagementNoSteerableRunError({
868-
threadId: input.threadId,
869-
mode: "restart",
870-
});
871-
}
872871
// Persist the replacement input with the selection before interrupting the
873872
// calling turn, which may never receive its configure tool result.
874873
return yield* dispatch({

0 commit comments

Comments
 (0)