Skip to content

Commit 1612de4

Browse files
t3dotggclaude
andcommitted
fix: steering restarts keep their model and effort dividers
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6cc2d46 commit 1612de4

12 files changed

Lines changed: 109 additions & 30 deletions

File tree

‎apps/mobile/src/features/threads/thread-handoff-row.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ export function modelSwitchesAtom(environmentId: EnvironmentId, threadId: Thread
5858
let previous: { signature: string; value: ReadonlyMap<MessageId, ModelSwitch> } | null = null;
5959
const ref = scopeThreadRef(environmentId, threadId);
6060
return Atom.make((get) => {
61-
const runs = get(environmentThreadDetails.threadAtom(ref))?.projection?.runs ?? [];
61+
const projection = get(environmentThreadDetails.threadAtom(ref))?.projection;
6262
const providers = get(serverEnvironment.configValueAtom(environmentId))?.providers;
63-
const value = resolveModelSwitches(runs, providers);
63+
const value = resolveModelSwitches(
64+
projection?.runs ?? [],
65+
projection?.attempts ?? [],
66+
providers,
67+
);
6468
// Run activity changes frequently; keep the map identity until a switch changes.
6569
const signature = JSON.stringify([...value]);
6670
if (previous?.signature !== signature) previous = { signature, value };

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5183,6 +5183,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
51835183
status: "pending",
51845184
startedAt: null,
51855185
completedAt: null,
5186+
restartedFrom: {
5187+
userMessageId: targetRun.userMessageId,
5188+
modelSelection: targetRun.modelSelection,
5189+
},
51865190
};
51875191
const nextRootNode: OrchestrationV2ExecutionNode = {
51885192
id: nextRootNodeId,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ it.live("retries a failed selection restart session open before completing", ()
407407
"selection restart supersede must not project hard-Stop interrupt items",
408408
);
409409
assert.equal(projection.runs.at(-1)?.modelSelection.model, replacementSelection.model);
410+
assert.deepEqual(projection.attempts[1]?.restartedFrom, {
411+
userMessageId: MessageId.make("message:selection-restart:first"),
412+
modelSelection: initialSelection,
413+
});
410414
assert.equal(projection.runs.at(-1)?.status, "completed");
411415
assert.isTrue(captured.failedReplacementOpen);
412416
assert.equal(captured.closedSessionCount, 0);

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3431,6 +3431,7 @@ export default function ChatView(props: ChatViewProps) {
34313431
footer={timelineFooter}
34323432
listRef={legendListRef}
34333433
runs={serverProjection?.runs ?? []}
3434+
attempts={serverProjection?.attempts ?? []}
34343435
latestRun={activeActivityRun}
34353436
announcedRun={announcedRun(activeActivityRun, serverProjection?.runs ?? [])}
34363437
detailBehindShell={isDetailBehindShell(

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function baseProps(): Omit<TimelineProps, "timelineEntries"> {
9797
latestRun: null,
9898
turnDiffSummaries: [],
9999
runs: [],
100+
attempts: [],
100101
routeThreadKey: `environment-local:thread-${Math.random().toString(36).slice(2)}`,
101102
onOpenTurnDiff: () => {},
102103
onOpenThread: () => {},

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import { useMediaQuery } from "~/hooks/useMediaQuery";
9696
import { cn } from "~/lib/utils";
9797
import { type TimestampFormat } from "@t3tools/contracts/settings";
9898
import { type HandoffTimelineRun } from "./V2LifecycleRow";
99-
import type { ModelSwitchRun } from "@t3tools/client-runtime/handoff";
99+
import type { ModelSwitchAttempt, ModelSwitchRun } from "@t3tools/client-runtime/handoff";
100100
import { useAtomValue } from "@effect/atom-react";
101101
import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
102102
import { TimelineSystemDivider } from "./TimelineSystemDivider";
@@ -300,6 +300,8 @@ interface MessagesTimelineProps {
300300
workspaceRoot: string | undefined;
301301
skills?: ReadonlyArray<ServerProviderSkill>;
302302
runs: ReadonlyArray<HandoffTimelineRun & ModelSwitchRun>;
303+
/** Steering restarts rewrite their run; attempts keep what each one replaced. */
304+
attempts: ReadonlyArray<ModelSwitchAttempt>;
303305
anchorMessageId: MessageId | null;
304306
onAnchorReady: (messageId: MessageId, anchorIndex: number) => void;
305307
onAnchorSizeChanged: (messageId: MessageId, size: number) => void;
@@ -399,6 +401,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
399401
workspaceRoot,
400402
skills = EMPTY_TIMELINE_SKILLS,
401403
runs: runsProp,
404+
attempts,
402405
anchorMessageId,
403406
onAnchorReady,
404407
onAnchorSizeChanged,
@@ -719,7 +722,7 @@ const ConversationTimeline = memo(function ConversationTimeline({
719722
const providers =
720723
useAtomValue(serverEnvironment.providersValueAtom(activeThreadEnvironmentId)) ??
721724
EMPTY_SERVER_PROVIDERS;
722-
const modelSwitches = useStableModelSwitches(runsProp, providers);
725+
const modelSwitches = useStableModelSwitches(runsProp, attempts, providers);
723726
const minimapItems = deriveTimelineMinimapItems(rows);
724727
const minimapRowIndices = minimapItems.map((item) => item.rowIndex);
725728
const restoreRowIndex =

‎apps/web/src/components/chat/useTimelineRows.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
resolveModelSwitches,
33
type ModelSwitch,
4+
type ModelSwitchAttempt,
45
type ModelSwitchRun,
56
} from "@t3tools/client-runtime/handoff";
67
import type { MessageId, ServerProvider } from "@t3tools/contracts";
@@ -46,19 +47,23 @@ const EMPTY_MODEL_SWITCHES: ReadonlyMap<MessageId, ModelSwitch> = new Map();
4647
/** Run status changes per stream event; keep the map identity until a switch changes. */
4748
export function useStableModelSwitches(
4849
runs: ReadonlyArray<ModelSwitchRun>,
50+
attempts: ReadonlyArray<ModelSwitchAttempt>,
4951
providers: ReadonlyArray<ServerProvider>,
5052
): ReadonlyMap<MessageId, ModelSwitch> {
51-
const [state, setState] = useState<{
52-
readonly runs: ReadonlyArray<ModelSwitchRun> | null;
53-
readonly providers: ReadonlyArray<ServerProvider> | null;
54-
readonly signature: string;
55-
readonly value: ReadonlyMap<MessageId, ModelSwitch>;
56-
}>({ runs: null, providers: null, signature: "[]", value: EMPTY_MODEL_SWITCHES });
57-
if (state.runs === runs && state.providers === providers) return state.value;
58-
const next = resolveModelSwitches(runs, providers);
53+
const [state, setState] = useState({
54+
runs: null as ReadonlyArray<ModelSwitchRun> | null,
55+
attempts,
56+
providers,
57+
signature: "[]",
58+
value: EMPTY_MODEL_SWITCHES,
59+
});
60+
if (state.runs === runs && state.attempts === attempts && state.providers === providers) {
61+
return state.value;
62+
}
63+
const next = resolveModelSwitches(runs, attempts, providers);
5964
const signature = JSON.stringify([...next]);
6065
const value = signature === state.signature ? state.value : next;
61-
setState({ runs, providers, signature, value });
66+
setState({ runs, attempts, providers, signature, value });
6267
return value;
6368
}
6469

‎apps/web/src/components/guest/GuestPage.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ function GuestThread({
514514
runningRunId={runningRunId}
515515
turnDiffSummaries={NO_TURN_DIFFS}
516516
runs={projection.runs}
517+
attempts={projection.attempts}
517518
routeThreadKey={threadKey}
518519
onOpenThread={ignore}
519520
supportsConversationRollback={false}

‎apps/web/test/renderScenarios.renders.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function timelineFor() {
9595
latestRun={null}
9696
turnDiffSummaries={[]}
9797
runs={[]}
98+
attempts={[]}
9899
routeThreadKey={`environment-local:${thread}`}
99100
onOpenTurnDiff={() => {}}
100101
onOpenThread={() => {}}

‎packages/client-runtime/src/handoff.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ describe("model switches", () => {
187187
effort?: string,
188188
overrides: Partial<ModelSwitchRun> = {},
189189
): ModelSwitchRun => ({
190+
id: RunId.make(`run-${id}`),
190191
ordinal,
191192
status: "completed",
192193
startedAt: DateTime.makeUnsafe("2026-10-11T00:00:00Z"),
@@ -224,6 +225,29 @@ describe("model switches", () => {
224225
).toMatchObject({ label: "Effort changed", effortChanged: true });
225226
});
226227

228+
it("marks each steering restart against what it replaced", () => {
229+
const restart = (attemptOrdinal: number, message: string, model: string) => ({
230+
runId: RunId.make("run-b"),
231+
attemptOrdinal,
232+
restartedFrom: {
233+
userMessageId: MessageId.make(`message-${message}`),
234+
modelSelection: { instanceId: from, model },
235+
},
236+
});
237+
const switches = resolveModelSwitches(
238+
[
239+
switchRun("a", 1, "gpt", undefined, { id: RunId.make("run-a") }),
240+
switchRun("s2", 2, "gpt", undefined, { id: RunId.make("run-b") }),
241+
],
242+
[restart(3, "s1", "max"), restart(2, "b", "mini")],
243+
);
244+
expect([...switches].map(([id, change]) => [id, change.from.model, change.to.model])).toEqual([
245+
["message-b", "gpt", "mini"],
246+
["message-s1", "mini", "max"],
247+
["message-s2", "max", "gpt"],
248+
]);
249+
});
250+
227251
it("reads an unset effort as the model's default", () => {
228252
const providers = [
229253
{
@@ -251,6 +275,7 @@ describe("model switches", () => {
251275
switchRun("c", 3, "gpt", "high"),
252276
switchRun("d", 4, "gpt"),
253277
],
278+
[],
254279
providers,
255280
);
256281
expect(keys(switches)).toEqual(["c", "d"]);
@@ -293,6 +318,7 @@ describe("model switches", () => {
293318
});
294319
const effortSwitch = resolveModelSwitches(
295320
[thought("a", 1), thought("b", 2, "deep")],
321+
[],
296322
providers,
297323
).get(MessageId.make("message-b"));
298324
expect(effortSwitch?.label).toBe("Effort changed");

0 commit comments

Comments
 (0)