Skip to content

Commit dfb71ec

Browse files
committed
fix(server): pair a retargeted fork with the model its source actually runs
When an unstarted fork follows its source onto another instance, the model now comes from the source's live runtime session, else a stored selection that already targets that instance, else the instance's default model. The source's stored selection is no longer paired blindly with the bound instance, so a picker change on a stopped source cannot start the fork with another driver's model.
1 parent 1458495 commit dfb71ec

2 files changed

Lines changed: 156 additions & 10 deletions

File tree

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ describe("ProviderCommandReactor", () => {
185185
readonly additionalProviders?: ReadonlyArray<{
186186
readonly instanceId: ProviderInstanceId;
187187
readonly driver: ProviderDriverKind;
188-
readonly models: ReadonlyArray<{ readonly slug: string; readonly name: string }>;
188+
readonly models: ReadonlyArray<{
189+
readonly slug: string;
190+
readonly name: string;
191+
readonly isDefault?: boolean;
192+
}>;
189193
}>;
190194
}) {
191195
const now = "2026-01-01T00:00:00.000Z";
@@ -1276,6 +1280,100 @@ describe("ProviderCommandReactor", () => {
12761280
expect(harness.sendTurn).toHaveBeenCalledTimes(1);
12771281
});
12781282

1283+
it("starts a fork of a stopped source on its bound instance's default model when the source selection points at another driver", async () => {
1284+
const boundInstanceId = ProviderInstanceId.make("codex-work");
1285+
const harness = await createHarness({
1286+
additionalProviders: [
1287+
{
1288+
instanceId: boundInstanceId,
1289+
driver: ProviderDriverKind.make("codex"),
1290+
models: [
1291+
{ slug: "gpt-5-codex", name: "GPT-5 Codex" },
1292+
{ slug: "gpt-5.6-sol", name: "GPT-5.6 Sol", isDefault: true },
1293+
],
1294+
},
1295+
],
1296+
});
1297+
const sourceThreadId = ThreadId.make("thread-1");
1298+
const forkThreadId = ThreadId.make("thread-fork-stopped-source-other-driver");
1299+
1300+
harness.persistSessionBinding({
1301+
threadId: sourceThreadId,
1302+
provider: ProviderDriverKind.make("codex"),
1303+
providerInstanceId: boundInstanceId,
1304+
runtimeMode: "approval-required",
1305+
status: "stopped",
1306+
resumeCursor: { opaque: "source-bound" },
1307+
});
1308+
await harness.runEffect(
1309+
harness.engine.dispatch({
1310+
type: "thread.session.set",
1311+
commandId: CommandId.make("cmd-stopped-source-other-driver-session"),
1312+
threadId: sourceThreadId,
1313+
session: {
1314+
threadId: sourceThreadId,
1315+
status: "stopped",
1316+
providerName: ProviderDriverKind.make("codex"),
1317+
providerInstanceId: boundInstanceId,
1318+
runtimeMode: "approval-required",
1319+
activeTurnId: null,
1320+
lastError: null,
1321+
updatedAt: "2026-09-03T12:00:09.000Z",
1322+
},
1323+
createdAt: "2026-09-03T12:00:09.000Z",
1324+
}),
1325+
);
1326+
// The picker on the stopped source now points at another driver; the fork
1327+
// inherits that selection but the conversation still lives on codex-work.
1328+
await harness.runEffect(
1329+
harness.engine.dispatch({
1330+
type: "thread.meta.update",
1331+
commandId: CommandId.make("cmd-stopped-source-picker-other-driver"),
1332+
threadId: sourceThreadId,
1333+
modelSelection: {
1334+
instanceId: ProviderInstanceId.make("claudeAgent"),
1335+
model: "claude-opus-5",
1336+
},
1337+
}),
1338+
);
1339+
await harness.runEffect(
1340+
harness.engine.dispatch({
1341+
type: "thread.fork",
1342+
commandId: CommandId.make("cmd-fork-stopped-source-other-driver"),
1343+
threadId: forkThreadId,
1344+
sourceThreadId,
1345+
sideChat: false,
1346+
createdAt: "2026-09-03T12:00:10.000Z",
1347+
}),
1348+
);
1349+
await harness.runEffect(
1350+
harness.engine.dispatch({
1351+
type: "thread.turn.start",
1352+
commandId: CommandId.make("cmd-start-fork-stopped-source-other-driver"),
1353+
threadId: forkThreadId,
1354+
message: {
1355+
messageId: asMessageId("message-fork-stopped-source-other-driver"),
1356+
role: "user",
1357+
text: "Continue where the source really lives.",
1358+
attachments: [],
1359+
},
1360+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
1361+
runtimeMode: "approval-required",
1362+
createdAt: "2026-09-03T12:00:12.000Z",
1363+
}),
1364+
);
1365+
await harness.drain();
1366+
1367+
expect(harness.startSession).toHaveBeenCalledTimes(1);
1368+
expect(harness.startSession.mock.calls[0]?.[1]).toMatchObject({
1369+
threadId: forkThreadId,
1370+
providerInstanceId: boundInstanceId,
1371+
modelSelection: { instanceId: boundInstanceId, model: "gpt-5.6-sol" },
1372+
forkFrom: { threadId: sourceThreadId },
1373+
});
1374+
expect(harness.sendTurn).toHaveBeenCalledTimes(1);
1375+
});
1376+
12791377
it("starts a latest-turn fork whose source thread was deleted", async () => {
12801378
const harness = await createHarness({ sessionFork: "latest-turn" });
12811379
const sourceThreadId = ThreadId.make("thread-1");

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.ts‎

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
ProviderDriverKind,
88
type ProjectId,
99
type OrchestrationSession,
10+
type ProviderInstanceId,
11+
type ServerProvider,
1012
ThreadId,
1113
type ProviderSession,
1214
type RuntimeMode,
@@ -74,6 +76,28 @@ function toNonEmptyProviderInput(value: string | undefined): string | undefined
7476
return normalized && normalized.length > 0 ? normalized : undefined;
7577
}
7678

79+
// Selection for a fork whose source conversation lives on `instanceId`. The
80+
// model the source is running wins; then a stored selection that already
81+
// targets that instance; then the instance's default model, because a
82+
// selection made for another driver cannot be reused there.
83+
function resolveForkSourceSelection(input: {
84+
readonly instanceId: ProviderInstanceId;
85+
readonly runtimeModel: string | undefined;
86+
readonly candidates: ReadonlyArray<ModelSelection>;
87+
readonly provider: ServerProvider | undefined;
88+
}): ModelSelection | undefined {
89+
const stored = input.candidates.find((candidate) => candidate.instanceId === input.instanceId);
90+
if (input.runtimeModel !== undefined) {
91+
return { ...stored, instanceId: input.instanceId, model: input.runtimeModel };
92+
}
93+
if (stored !== undefined) return stored;
94+
const defaultModel =
95+
input.provider?.models.find((model) => model.isDefault === true) ?? input.provider?.models[0];
96+
return defaultModel === undefined
97+
? undefined
98+
: { instanceId: input.instanceId, model: defaultModel.slug };
99+
}
100+
77101
function mapProviderSessionStatusToOrchestrationStatus(
78102
status: "connecting" | "ready" | "running" | "error" | "closed",
79103
): OrchestrationSession["status"] {
@@ -551,12 +575,20 @@ const make = Effect.gen(function* () {
551575
// it, and a source with neither stays on the selection the fork
552576
// inherited. The source's stored selection alone is not enough: it can
553577
// change before any turn moves the conversation.
578+
const resolveActiveSession = (threadId: ThreadId) =>
579+
providerService
580+
.listSessions()
581+
.pipe(Effect.map((sessions) => sessions.find((session) => session.threadId === threadId)));
554582
const forkSourceSession =
555583
forkSource?.session != null &&
556584
forkSource.session.status !== "stopped" &&
557585
forkSource.session.status !== "error"
558586
? forkSource.session
559587
: null;
588+
const forkSourceRuntimeSession =
589+
forkSource !== undefined && forkSourceSession !== null
590+
? yield* resolveActiveSession(forkSource.id)
591+
: undefined;
560592
const forkSourceBinding =
561593
forkSource !== undefined && forkSourceSession?.providerInstanceId === undefined
562594
? yield* providerService.getSessionBinding(forkSource.id)
@@ -567,10 +599,6 @@ const make = Effect.gen(function* () {
567599
: (forkSourceSession?.providerInstanceId ??
568600
forkSourceBinding?.providerInstanceId ??
569601
thread.modelSelection.instanceId);
570-
const resolveActiveSession = (threadId: ThreadId) =>
571-
providerService
572-
.listSessions()
573-
.pipe(Effect.map((sessions) => sessions.find((session) => session.threadId === threadId)));
574602

575603
const activeSession = yield* resolveActiveSession(threadId);
576604
const activeThreadSession =
@@ -598,13 +626,33 @@ const make = Effect.gen(function* () {
598626
const baseDesiredModelSelection = requestedModelSelection ?? thread.modelSelection;
599627
// When the source moved, the fork follows it onto the source's current
600628
// instance and model: the inherited selection described the old home.
601-
const forkSourceMoved =
629+
let movedForkSourceSelection: ModelSelection | undefined;
630+
if (
602631
forkSource !== undefined &&
603632
forkSourceInstanceId !== undefined &&
604-
forkSourceInstanceId !== thread.modelSelection.instanceId;
605-
const movedForkSourceSelection: ModelSelection | undefined = forkSourceMoved
606-
? { ...forkSource.modelSelection, instanceId: forkSourceInstanceId }
607-
: undefined;
633+
forkSourceInstanceId !== thread.modelSelection.instanceId
634+
) {
635+
movedForkSourceSelection = resolveForkSourceSelection({
636+
instanceId: forkSourceInstanceId,
637+
runtimeModel:
638+
forkSourceRuntimeSession?.providerInstanceId === forkSourceInstanceId
639+
? forkSourceRuntimeSession.model
640+
: undefined,
641+
candidates: [forkSource.modelSelection, thread.modelSelection],
642+
provider: (yield* providerRegistry.getProviders).find(
643+
(provider) => provider.instanceId === forkSourceInstanceId,
644+
),
645+
});
646+
if (movedForkSourceSelection === undefined) {
647+
return yield* new ProviderAdapterRequestError({
648+
provider: providerErrorLabelFromInstanceHint({
649+
instanceId: String(forkSourceInstanceId),
650+
}),
651+
method: "thread.turn.start",
652+
detail: `Fork '${threadId}' cannot start because source thread '${forkSource.id}' lives on provider instance '${forkSourceInstanceId}', which is not configured in this build.`,
653+
});
654+
}
655+
}
608656
const desiredModelSelection = movedForkSourceSelection ?? baseDesiredModelSelection;
609657
const desiredInstanceId = desiredModelSelection.instanceId;
610658
const inheritedForkInstanceId =

0 commit comments

Comments
 (0)