Skip to content

Commit 12ae3f9

Browse files
committed
fix(server): continue work after agent model changes
1 parent 29e09df commit 12ae3f9

5 files changed

Lines changed: 263 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 5 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 * as McpToolAccess from "../../McpToolAccess.ts";
1413
import {
@@ -188,15 +187,16 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
188187
threads,
189188
projection: { thread },
190189
} = yield* readThread(input.threadId);
191-
const type = modelSelectionCommandType(thread.providerInstanceId, input.modelSelection);
192190
const result = yield* threads
193-
.dispatch({
194-
type,
191+
.configureModelSelection({
192+
projectId: thread.projectId,
195193
threadId: thread.id,
196194
commandId: yield* newCommandId(),
197195
modelSelection: input.modelSelection,
196+
createdBy: "agent",
197+
creationSource: "mcp",
198198
})
199-
.pipe(Effect.mapError(dispatchFailure));
199+
.pipe(Effect.mapError(unavailable));
200200
return { sequence: result.sequence };
201201
}),
202202
),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const ThreadConfigurationTool = Tool.make("t3_thread_configuration", {
192192
const ThreadConfigureTool = Tool.make("t3_thread_configure", {
193193
...commandTool,
194194
description:
195-
"Set a thread's provider, model and options with the existing selection command. Omit threadId for this thread. This does not change permission modes. Use orchestrator_capabilities to choose a selection.",
195+
"Set a thread's provider, model and options. Omit threadId for this thread. A changed selection restarts a running turn and automatically continues its unfinished user request; an interrupted calling turn 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. Use orchestrator_capabilities to choose a selection.",
196196
parameters: Schema.Struct({
197197
threadId: Schema.optional(ThreadId),
198198
modelSelection: ModelSelection,

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

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
3232
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
3333
import { checkpointWorkspace } from "@t3tools/provider-testing/replayWorkspace";
3434
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
35+
import * as ThreadManagementService from "./ThreadManagementService.ts";
3536

3637
const driver = ProviderDriverKind.make("codex");
3738
const providerInstanceId = ProviderInstanceId.make("codex-restart-test");
@@ -388,6 +389,208 @@ function makeCompletingHandoffAdapter(
388389
};
389390
}
390391

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

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
ThreadId,
3030
type TurnItemId,
3131
} from "@t3tools/contracts";
32+
import { modelSelectionCommandType, modelSelectionsEqual } from "@t3tools/shared/model";
3233
import * as Context from "effect/Context";
3334
import * as DateTime from "effect/DateTime";
3435
import * as Duration from "effect/Duration";
@@ -334,6 +335,14 @@ export interface ThreadManagementServiceShape {
334335
readonly sendToThread: (
335336
input: ThreadManagementSendInput,
336337
) => Effect.Effect<ThreadManagementSendResult, ThreadManagementFailure>;
338+
readonly configureModelSelection: (
339+
input: ThreadManagementProvenance & {
340+
readonly projectId: ProjectId;
341+
readonly threadId: ThreadId;
342+
readonly commandId: CommandId;
343+
readonly modelSelection: ModelSelection;
344+
},
345+
) => Effect.Effect<{ readonly sequence: number }, ThreadManagementFailure>;
337346
readonly waitForThread: (
338347
input: ThreadManagementWaitInput,
339348
) => Effect.Effect<ThreadManagementWaitResult, ThreadManagementError>;
@@ -585,6 +594,49 @@ const make = Effect.gen(function* () {
585594
),
586595
);
587596

597+
const configureModelSelection = Effect.fn(
598+
"orchestrationV2.threadManagement.configureModelSelection",
599+
)(function* (input: Parameters<ThreadManagementServiceShape["configureModelSelection"]>[0]) {
600+
const target = yield* getProjectThreadRecords(input, ["runs", "providerTurns"]);
601+
const activeRun = latestActiveRun(target);
602+
if (
603+
modelSelectionsEqual(
604+
activeRun?.modelSelection ?? target.thread.modelSelection,
605+
input.modelSelection,
606+
)
607+
) {
608+
return { sequence: yield* orchestrator.getThreadEventSequence(input.threadId) };
609+
}
610+
if (activeRun === undefined) {
611+
return yield* orchestrator.dispatch({
612+
type: modelSelectionCommandType(target.thread.providerInstanceId, input.modelSelection),
613+
commandId: input.commandId,
614+
threadId: input.threadId,
615+
modelSelection: input.modelSelection,
616+
});
617+
}
618+
if (latestSteerableRun(target)?.id !== activeRun.id) {
619+
return yield* new ThreadManagementNoSteerableRunError({
620+
threadId: input.threadId,
621+
mode: "restart",
622+
});
623+
}
624+
// Persist the replacement input with the selection before interrupting the
625+
// calling turn, which may never receive its configure tool result.
626+
return yield* orchestrator.dispatch({
627+
type: "message.dispatch",
628+
commandId: input.commandId,
629+
threadId: input.threadId,
630+
messageId: MessageId.make(`${input.commandId}:model-continuation`),
631+
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.",
632+
attachments: [],
633+
modelSelection: input.modelSelection,
634+
dispatchMode: { type: "restart_active", targetRunId: activeRun.id },
635+
createdBy: input.createdBy,
636+
creationSource: input.creationSource,
637+
});
638+
});
639+
588640
const sendToThread: ThreadManagementServiceShape["sendToThread"] = (input) =>
589641
Effect.gen(function* () {
590642
const target = yield* getProjectThreadRecords(input, ["runs", "providerTurns"]);
@@ -914,6 +966,7 @@ const make = Effect.gen(function* () {
914966
getThreadShell: orchestrator.getThreadShell,
915967
listProjectThreads,
916968
sendToThread,
969+
configureModelSelection,
917970
waitForThread,
918971
settleAfterRun,
919972
settleThread,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
208208
getProjectThread: unused,
209209
listProjectThreads: unused,
210210
sendToThread: unused,
211+
configureModelSelection: unused,
211212
waitForThread: unused,
212213
settleAfterRun: unused,
213214
settleThread: unused,

0 commit comments

Comments
 (0)