Skip to content

Commit a43c3ac

Browse files
t3dotggclaude
andcommitted
fix(server): order switches by execution, compare a fork's first turn with its source
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 02c26e1 commit a43c3ac

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7196,7 +7196,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
71967196
text: dispatchText,
71977197
...(command.context ? { context: command.context } : {}),
71987198
attachments: command.attachments,
7199-
...(run.contextHandoffId === null ? modelSwitchSince(projection, modelSelection) : {}),
7199+
...(run.contextHandoffId === null
7200+
? modelSwitchSince(projection, modelSelection, sourceRun?.modelSelection)
7201+
: {}),
72007202
};
72017203
const activeHandoff = portableForkHandoff ?? mergeBackHandoff ?? providerSwitchHandoff;
72027204
const handoffSourceRuns =

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const run = (
1818
({
1919
ordinal,
2020
status,
21+
completedAt: null,
2122
userMessageId: MessageId.make(message),
2223
modelSelection: selection(model),
2324
}) as OrchestrationV2ThreadProjection["runs"][number];
@@ -46,8 +47,12 @@ describe("modelSwitchSince", () => {
4647
).toEqual({ modelSwitch: { from: selection("b"), to: selection("a") } });
4748
});
4849

49-
it("records nothing for the first run or an unchanged selection", () => {
50+
it("records nothing for a first run or an unchanged selection", () => {
5051
expect(modelSwitchSince({ runs: [], turnItems: [] }, selection("a"))).toEqual({});
52+
// A fork's first message compares with the run it forked from.
53+
expect(modelSwitchSince({ runs: [], turnItems: [] }, selection("a"), selection("b"))).toEqual({
54+
modelSwitch: { from: selection("b"), to: selection("a") },
55+
});
5156
expect(
5257
modelSwitchSince({ runs: [run("first", 1, "a")], turnItems: shown("first") }, selection("a")),
5358
).toEqual({});

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ import type {
44
OrchestrationV2TurnItem,
55
} from "@t3tools/contracts";
66
import { modelSelectionsEqual } from "@t3tools/shared/model";
7+
import { runRanAfter } from "@t3tools/shared/orchestrationV2ThreadError";
78

89
/**
910
* The model or effort change a new run brings, measured from the latest run whose message is in
10-
* the timeline, so consecutive dividers always chain. Callers skip starts with a context handoff;
11-
* the handoff item already shows those.
11+
* the timeline, so consecutive dividers always chain. `baseline` stands in when there is none,
12+
* such as a fork's source run. Callers skip starts with a context handoff; the handoff item already
13+
* shows those.
1214
*/
1315
export function modelSwitchSince(
1416
projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems">,
1517
to: ModelSelection,
18+
baseline?: ModelSelection,
1619
): Pick<Extract<OrchestrationV2TurnItem, { type: "user_message" }>, "modelSwitch"> {
1720
const shown = new Set(
1821
projection.turnItems.flatMap((item) => (item.type === "user_message" ? [item.messageId] : [])),
1922
);
2023
let previous: OrchestrationV2ThreadProjection["runs"][number] | undefined;
2124
for (const run of projection.runs) {
2225
if (run.status === "rolled_back" || !shown.has(run.userMessageId)) continue;
23-
if (previous === undefined || run.ordinal > previous.ordinal) previous = run;
26+
if (previous === undefined || runRanAfter(run, previous)) previous = run;
2427
}
25-
return previous === undefined || modelSelectionsEqual(previous.modelSelection, to)
26-
? {}
27-
: { modelSwitch: { from: previous.modelSelection, to } };
28+
const from = previous?.modelSelection ?? baseline;
29+
return from === undefined || modelSelectionsEqual(from, to) ? {} : { modelSwitch: { from, to } };
2830
}

0 commit comments

Comments
 (0)