Skip to content

Commit 8cf0ce1

Browse files
committed
fix(server): continue work after agent model changes
1 parent cfdff56 commit 8cf0ce1

5 files changed

Lines changed: 262 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
type OrchestrationV2Command,
99
} from "@t3tools/contracts";
1010
import * as Effect from "effect/Effect";
11-
import { modelSelectionCommandType } from "@t3tools/shared/model";
1211

1312
import {
1413
newCommandId,
@@ -180,13 +179,14 @@ export const ThreadToolkitHandlersLive = ThreadToolkit.toLayer({
180179
threads,
181180
projection: { thread },
182181
} = yield* readWritableThread();
183-
const type = modelSelectionCommandType(thread.providerInstanceId, input.modelSelection);
184182
const result = yield* threads
185-
.dispatch({
186-
type,
183+
.configureModelSelection({
184+
projectId: thread.projectId,
187185
threadId: thread.id,
188186
commandId: yield* newCommandId(),
189187
modelSelection: input.modelSelection,
188+
createdBy: "agent",
189+
creationSource: "mcp",
190190
})
191191
.pipe(Effect.mapError(unavailable));
192192
return { sequence: result.sequence };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ const ThreadConfigurationTool = Tool.make("t3_thread_configuration", {
187187
const ThreadConfigureTool = Tool.make("t3_thread_configure", {
188188
...commandTool,
189189
description:
190-
"Set this calling thread's provider, model and options with the existing selection command. This does not change permission modes or other threads. Use orchestrator_capabilities to choose a selection.",
190+
"Set this calling thread's provider, model and options. A changed selection restarts a running turn and automatically continues its unfinished user request; the interrupted call may not receive a tool result. An already-active selection is a no-op. Providers that cannot restart reject the change without interrupting the turn. This does not change permission modes or other threads. Use orchestrator_capabilities to choose a selection.",
191191
parameters: Schema.Struct({ modelSelection: ModelSelection }),
192192
}).annotate(Tool.Destructive, true);
193193

‎apps/server/src/orchestration-v2/SelectionRestart.integration.test.ts‎

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
type ProviderAdapterV2TurnInput,
3636
} from "./ProviderAdapter.ts";
3737
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
38+
import * as ThreadManagementService from "./ThreadManagementService.ts";
3839
import { makeOrchestratorV2ReplayLayerWithRegistry } from "./testkit/ProviderReplayHarness.ts";
3940
import { checkpointWorkspace } from "./testkit/ReplayFixtureWorkspace.ts";
4041

@@ -391,6 +392,208 @@ function makeCompletingHandoffAdapter(startCount: Ref.Ref<number>): ProviderAdap
391392
};
392393
}
393394

395+
it.live.each([
396+
"saved-model",
397+
"options",
398+
"provider",
399+
"unchanged",
400+
"idle",
401+
"unsupported",
402+
"preparing",
403+
] as const)("configures the calling thread without losing work: %s", (scenario) =>
404+
Effect.scoped(
405+
Effect.gen(function* () {
406+
const name = `configure-continuation-${scenario}`;
407+
const cwd = yield* checkpointWorkspace(name);
408+
const threadId = ThreadId.make(`thread:${name}`);
409+
const projectId = ProjectId.make(`project:${name}`);
410+
const otherInstanceId = ProviderInstanceId.make("codex-configure-target");
411+
const selection = {
412+
...initialSelection,
413+
options: [
414+
{ id: "reasoningEffort", value: "low" },
415+
{ id: "fastMode", value: true },
416+
],
417+
} satisfies ModelSelection;
418+
const targetSelection: ModelSelection =
419+
scenario === "unchanged"
420+
? { ...selection, options: selection.options.toReversed() }
421+
: scenario === "options"
422+
? { ...selection, options: [{ id: "reasoningEffort", value: "xhigh" }] }
423+
: {
424+
...replacementSelection,
425+
instanceId: scenario === "provider" ? otherInstanceId : providerInstanceId,
426+
};
427+
const state = yield* Ref.make<RestartAdapterState>({
428+
activeTurn: null,
429+
opened: [],
430+
started: [],
431+
closedSessionCount: 0,
432+
failedReplacementOpen: true,
433+
});
434+
const starts: Array<ProviderAdapterV2TurnInput> = [];
435+
const capabilities = {
436+
...pooledCapabilities,
437+
turns: {
438+
...pooledCapabilities.turns,
439+
supportsSteeringByInterruptRestart: scenario !== "unsupported",
440+
},
441+
};
442+
const adapters = [providerInstanceId, otherInstanceId].map((instanceId) => {
443+
const base = makeRestartAdapter(state, capabilities, instanceId);
444+
return {
445+
...base,
446+
planSelectionTransition: () => Effect.succeed({ type: "apply_on_next_turn" } as const),
447+
openSession: (input) =>
448+
base.openSession(input).pipe(
449+
Effect.map((session) => ({
450+
...session,
451+
startTurn: (input) =>
452+
Effect.gen(function* () {
453+
starts.push(input);
454+
yield* session.startTurn(input);
455+
}),
456+
})),
457+
),
458+
} satisfies ProviderAdapterV2Shape;
459+
});
460+
const registry = Layer.succeed(ProviderAdapterRegistry.ProviderAdapterRegistryV2, {
461+
get: (instanceId) =>
462+
Effect.succeed(adapters.find((adapter) => adapter.instanceId === instanceId)!),
463+
list: () => Effect.succeed([providerInstanceId, otherInstanceId]),
464+
getMetadata: () =>
465+
Effect.succeed({
466+
driver,
467+
continuationKey: "codex:home:/shared",
468+
enabled: true,
469+
capabilities,
470+
}),
471+
});
472+
yield* Effect.gen(function* () {
473+
const orchestrator = yield* Orchestrator.OrchestratorV2;
474+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
475+
const threads = yield* ThreadManagementService.ThreadManagementService;
476+
yield* orchestrator.dispatch({
477+
type: "thread.create",
478+
commandId: CommandId.make(`${name}:create`),
479+
threadId,
480+
projectId,
481+
title: name,
482+
modelSelection: selection,
483+
runtimeMode: "full-access",
484+
interactionMode: "default",
485+
branch: null,
486+
worktreePath: cwd,
487+
createdBy: "user",
488+
creationSource: "web",
489+
});
490+
if (scenario !== "idle") {
491+
const afterSequence = yield* orchestrator.getThreadEventSequence(threadId);
492+
yield* orchestrator.dispatch({
493+
type: "message.dispatch",
494+
commandId: CommandId.make(`${name}:first`),
495+
threadId,
496+
messageId: MessageId.make(`${name}:first`),
497+
text: "Switch to the requested model and finish the poem.",
498+
attachments: [],
499+
dispatchMode: { type: "start_immediately" },
500+
createdBy: "user",
501+
creationSource: "web",
502+
});
503+
if (scenario !== "preparing") {
504+
yield* worker.drain();
505+
yield* orchestrator.streamStoredEventsFrom({ threadId, afterSequence }).pipe(
506+
Stream.filter(
507+
({ event }) =>
508+
event.type === "provider-turn.updated" && event.payload.status === "running",
509+
),
510+
Stream.take(1),
511+
Stream.runDrain,
512+
);
513+
}
514+
}
515+
if (scenario === "saved-model") {
516+
yield* orchestrator.dispatch({
517+
type: "thread.model-selection.set",
518+
commandId: CommandId.make(`${name}:save-next-model`),
519+
threadId,
520+
modelSelection: targetSelection,
521+
});
522+
yield* worker.drain();
523+
}
524+
const before = yield* orchestrator.getThreadProjection(threadId);
525+
const sequence = yield* orchestrator.getThreadEventSequence(threadId);
526+
const configure = threads.configureModelSelection({
527+
projectId,
528+
threadId,
529+
commandId: CommandId.make(`${name}:configure`),
530+
modelSelection: targetSelection,
531+
createdBy: "agent",
532+
creationSource: "mcp",
533+
});
534+
if (scenario === "unsupported" || scenario === "preparing") {
535+
const error = yield* Effect.flip(configure);
536+
assert.equal(
537+
error._tag,
538+
scenario === "preparing"
539+
? "ThreadManagementNoSteerableRunError"
540+
: "OrchestratorDispatchError",
541+
);
542+
assert.deepEqual(yield* orchestrator.getThreadProjection(threadId), before);
543+
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);
544+
return;
545+
}
546+
if (scenario === "unchanged" || scenario === "idle") {
547+
const result = yield* configure;
548+
const after = yield* orchestrator.getThreadProjection(threadId);
549+
if (scenario === "unchanged") {
550+
assert.equal(result.sequence, sequence);
551+
assert.deepEqual(after, before);
552+
} else {
553+
assert.deepEqual(after.thread.modelSelection, targetSelection);
554+
assert.isEmpty(after.runs);
555+
assert.isEmpty(after.messages);
556+
}
557+
return;
558+
}
559+
yield* configure;
560+
yield* worker.drain();
561+
yield* orchestrator.streamStoredEventsFrom({ threadId, afterSequence: sequence }).pipe(
562+
Stream.filter(({ event }) =>
563+
scenario === "options"
564+
? event.type === "provider-turn.updated" && event.payload.status === "running"
565+
: event.type === "run.updated" && event.payload.status === "completed",
566+
),
567+
Stream.take(1),
568+
Stream.runDrain,
569+
);
570+
yield* worker.drain();
571+
const after = yield* orchestrator.getThreadProjection(threadId);
572+
assert.lengthOf(starts, 2);
573+
assert.deepEqual(starts[1]?.modelSelection, targetSelection);
574+
assert.match(starts[1]!.message.text, /continue|finish/i);
575+
assert.equal(after.runs[0]?.id, before.runs[0]?.id);
576+
assert.deepEqual(after.runs[0]?.modelSelection, targetSelection);
577+
assert.deepEqual(after.thread.modelSelection, targetSelection);
578+
assert.lengthOf(after.attempts, 2);
579+
assert.equal(after.attempts[0]?.status, "superseded");
580+
assert.equal(after.messages[1]?.createdBy, "agent");
581+
assert.equal(after.messages[1]?.creationSource, "mcp");
582+
}).pipe(
583+
Effect.provide(
584+
ThreadManagementService.layer.pipe(
585+
Layer.provideMerge(
586+
makeOrchestratorV2ReplayLayerWithRegistry({ name }, registry, {
587+
runEffectWorker: scenario !== "preparing",
588+
}),
589+
),
590+
),
591+
),
592+
);
593+
}),
594+
),
595+
);
596+
394597
it.live("restarts selection as a new attempt and retries after old-session cleanup", () =>
395598
Effect.scoped(
396599
Effect.gen(function* () {

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
type ScheduledTaskId,
2424
ThreadId,
2525
} from "@t3tools/contracts";
26+
import { modelSelectionCommandType, modelSelectionsEqual } from "@t3tools/shared/model";
2627
import * as Context from "effect/Context";
2728
import * as DateTime from "effect/DateTime";
2829
import * as Duration from "effect/Duration";
@@ -306,6 +307,14 @@ export interface ThreadManagementServiceShape {
306307
readonly sendToThread: (
307308
input: ThreadManagementSendInput,
308309
) => Effect.Effect<ThreadManagementSendResult, ThreadManagementFailure>;
310+
readonly configureModelSelection: (
311+
input: ThreadManagementProvenance & {
312+
readonly projectId: ProjectId;
313+
readonly threadId: ThreadId;
314+
readonly commandId: CommandId;
315+
readonly modelSelection: ModelSelection;
316+
},
317+
) => Effect.Effect<{ readonly sequence: number }, ThreadManagementFailure>;
309318
readonly waitForThread: (
310319
input: ThreadManagementWaitInput,
311320
) => Effect.Effect<ThreadManagementWaitResult, ThreadManagementError>;
@@ -519,6 +528,49 @@ const make = Effect.gen(function* () {
519528
),
520529
);
521530

531+
const configureModelSelection = Effect.fn(
532+
"orchestrationV2.threadManagement.configureModelSelection",
533+
)(function* (input: Parameters<ThreadManagementServiceShape["configureModelSelection"]>[0]) {
534+
const target = yield* getProjectThreadRecords(input, ["runs", "providerTurns"]);
535+
const activeRun = latestActiveRun(target);
536+
if (
537+
modelSelectionsEqual(
538+
activeRun?.modelSelection ?? target.thread.modelSelection,
539+
input.modelSelection,
540+
)
541+
) {
542+
return { sequence: yield* orchestrator.getThreadEventSequence(input.threadId) };
543+
}
544+
if (activeRun === undefined) {
545+
return yield* orchestrator.dispatch({
546+
type: modelSelectionCommandType(target.thread.providerInstanceId, input.modelSelection),
547+
commandId: input.commandId,
548+
threadId: input.threadId,
549+
modelSelection: input.modelSelection,
550+
});
551+
}
552+
if (latestSteerableRun(target)?.id !== activeRun.id) {
553+
return yield* new ThreadManagementNoSteerableRunError({
554+
threadId: input.threadId,
555+
mode: "restart",
556+
});
557+
}
558+
// Persist the replacement input with the selection before interrupting the
559+
// calling turn, which may never receive its configure tool result.
560+
return yield* orchestrator.dispatch({
561+
type: "message.dispatch",
562+
commandId: input.commandId,
563+
threadId: input.threadId,
564+
messageId: MessageId.make(`${input.commandId}:model-continuation`),
565+
text: "The requested model change has been applied. Continue the unfinished user request from where the previous turn stopped. Do not repeat the model change if the requested selection is already active.",
566+
attachments: [],
567+
modelSelection: input.modelSelection,
568+
dispatchMode: { type: "restart_active", targetRunId: activeRun.id },
569+
createdBy: input.createdBy,
570+
creationSource: input.creationSource,
571+
});
572+
});
573+
522574
const sendToThread: ThreadManagementServiceShape["sendToThread"] = (input) =>
523575
Effect.gen(function* () {
524576
const target = yield* getProjectThreadRecords(input, ["runs", "providerTurns"]);
@@ -733,6 +785,7 @@ const make = Effect.gen(function* () {
733785
getThreadShell: orchestrator.getThreadShell,
734786
listProjectThreads,
735787
sendToThread,
788+
configureModelSelection,
736789
waitForThread,
737790
interruptThread,
738791
getThreadEventSequence: orchestrator.getThreadEventSequence,

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
203203
getProjectThread: unused,
204204
listProjectThreads: unused,
205205
sendToThread: unused,
206+
configureModelSelection: unused,
206207
waitForThread: unused,
207208
interruptThread: unused,
208209
getThreadEventSequence: unused,

0 commit comments

Comments
 (0)