Skip to content

Commit c65f8d5

Browse files
t3dotggclaude
andauthored
feat(chat): threads show when you switch models or effort (#28995)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0338e99 commit c65f8d5

15 files changed

Lines changed: 483 additions & 6 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {
1515
enrichedSkillReference,
1616
} from "../../lib/enrichedLinkPresentation";
1717
import { ThreadContextDivider } from "./thread-context-divider";
18-
import { ThreadHandoffDivider, ThreadHandoffRow } from "./thread-handoff-row";
18+
import {
19+
ThreadHandoffDivider,
20+
ThreadHandoffRow,
21+
ThreadModelSwitchDivider,
22+
} from "./thread-handoff-row";
1923
import { GuestBubbleTint, GuestMessageName } from "./guestMessageStyle";
2024
import { ThreadGuestPendingMessage, useThreadGuestShare } from "./ThreadGuestShareBar";
2125
import { LinkRequestCard } from "./LinkRequestCard";
@@ -3590,6 +3594,16 @@ export const ThreadFeed = memo(function ThreadFeed({
35903594
workspaceRoot={props.workspaceRoot}
35913595
>
35923596
<ThreadMediaVisibility>
3597+
{info.item.type === "message" &&
3598+
!isCompactionCommandFeedEntry(info.item) &&
3599+
info.item.message.projectedItem?.item.type === "user_message" &&
3600+
info.item.message.projectedItem.item.modelSwitch ? (
3601+
<ThreadModelSwitchDivider
3602+
environmentId={props.environmentId}
3603+
modelSwitch={info.item.message.projectedItem.item.modelSwitch}
3604+
iconColor={iconSubtleColor}
3605+
/>
3606+
) : null}
35933607
{renderFeedEntry(info, {
35943608
userMessageGrouping: info.item.userMessageGrouping,
35953609
environmentId: props.environmentId,

‎apps/mobile/src/features/threads/thread-context-divider.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ShimmeringWorkContent } from "./thread-work-log";
66

77
export function ThreadContextDivider(props: {
88
label: string;
9-
icon: AppSymbolName;
9+
icon?: AppSymbolName;
1010
iconColor: ColorValue;
1111
active?: boolean;
1212
failed?: boolean;
@@ -16,7 +16,9 @@ export function ThreadContextDivider(props: {
1616
<View className="mb-3 flex-row items-center gap-3 px-1 py-1">
1717
<View className="h-px min-w-2 flex-1 bg-separator" />
1818
<View className="shrink flex-row flex-wrap items-center justify-center gap-1.5">
19-
<SymbolView name={props.icon} size={12} tintColor={props.iconColor} type="monochrome" />
19+
{props.icon ? (
20+
<SymbolView name={props.icon} size={12} tintColor={props.iconColor} type="monochrome" />
21+
) : null}
2022
{props.active ? (
2123
<ShimmeringWorkContent
2224
className="flex-none"

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as Dialog from "../../components/ui/Dialog";
22
import { useAtomValue } from "@effect/atom-react";
33
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4-
import { resolveHandoffEndpoints, type HandoffEndpoints } from "@t3tools/client-runtime/handoff";
4+
import {
5+
resolveHandoffEndpoints,
6+
resolveModelSwitchChange,
7+
type HandoffEndpoints,
8+
} from "@t3tools/client-runtime/handoff";
59
import { resolveModelShortName } from "@t3tools/client-runtime/provider-clients";
610
import {
711
normalizeProviderAccentColor,
@@ -10,6 +14,8 @@ import {
1014
} from "@t3tools/client-runtime/state/provider-instance-display";
1115
import type {
1216
EnvironmentId,
17+
ModelSelection,
18+
OrchestrationV2ModelSwitch,
1319
OrchestrationV2ProjectedTurnItem,
1420
ProviderInstanceId,
1521
ServerProvider,
@@ -45,6 +51,33 @@ function handoffEndpointsAtom(
4551
});
4652
}
4753

54+
/** A same-harness model or effort change. Shows only what changed. */
55+
export function ThreadModelSwitchDivider(props: {
56+
environmentId: EnvironmentId;
57+
modelSwitch: OrchestrationV2ModelSwitch;
58+
iconColor: ColorValue;
59+
}) {
60+
const providers = useAtomValue(serverEnvironment.providersValueAtom(props.environmentId)) ?? [];
61+
const change = resolveModelSwitchChange(props.modelSwitch, providers);
62+
if (change === null) return null;
63+
const describe = (selection: ModelSelection, effort: string | undefined) => {
64+
const provider = providers.find((candidate) => candidate.instanceId === selection.instanceId);
65+
const model = provider?.models.find((candidate) => candidate.slug === selection.model);
66+
const modelName =
67+
(model ? resolveModelShortName(model, provider?.driver) : undefined) ??
68+
model?.name ??
69+
selection.model;
70+
return [change.modelChanged ? modelName : null, effort].filter(Boolean).join(" ");
71+
};
72+
const { from, to } = props.modelSwitch;
73+
return (
74+
<ThreadContextDivider
75+
label={`${describe(from, change.effort?.from)} → ${describe(to, change.effort?.to)}`}
76+
iconColor={props.iconColor}
77+
/>
78+
);
79+
}
80+
4881
export function ThreadHandoffRow(props: {
4982
environmentId: EnvironmentId;
5083
projectedItem: OrchestrationV2ProjectedTurnItem;

‎apps/mobile/src/lib/filePreview.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ vi.mock("./openExternalUrl", () => ({}));
269269
vi.mock("./attachmentDownload", () => ({}));
270270
vi.mock("./uuid", () => ({}));
271271
vi.mock("../features/threads/thread-context-divider", () => ({}));
272-
vi.mock("../features/threads/thread-handoff-row", () => ({}));
272+
vi.mock("../features/threads/thread-handoff-row", () => ({ ThreadModelSwitchDivider: () => null }));
273273
vi.mock("../features/threads/ThreadGuestShareBar", () => ({ useThreadGuestShare: () => null }));
274274
vi.mock("../features/threads/SecretRequestCard", () => ({}));
275275
vi.mock("../features/threads/LinkRequestCard", () => ({ LinkRequestCard: () => null }));

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
type OrchestrationV2ProviderThread,
4949
type OrchestrationV2ProviderTurn,
5050
type OrchestrationV2RemoteTaskChild,
51+
type OrchestrationV2ModelSwitch,
5152
type OrchestrationV2Run,
5253
type OrchestrationV2RunAttempt,
5354
type OrchestrationV2ThreadShell,
@@ -75,6 +76,7 @@ import {
7576
managedRuntimeModeCeilingMessage,
7677
} from "@t3tools/contracts";
7778
import { modelSelectionsEqual } from "@t3tools/shared/model";
79+
import { modelSwitchSince } from "./modelSwitch.ts";
7880
import { recordThreadOpen } from "@t3tools/shared/paletteSearch";
7981
import {
8082
backgroundWorkHoldsCompletion,
@@ -2055,6 +2057,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
20552057
startedAt: now,
20562058
completedAt: now,
20572059
updatedAt: now,
2060+
...(activeHandoff === null ? modelSwitchSince(projection, queuedRun.modelSelection) : {}),
20582061
};
20592062
const handoffTurnItem: OrchestrationV2TurnItem | null =
20602063
activeHandoff === null
@@ -4796,6 +4799,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
47964799
readonly providerTurnId: typeof providerTurn.id | null;
47974800
readonly providerThreadId: OrchestrationV2ProviderThread["id"];
47984801
readonly providerInstanceId: ProviderInstanceId;
4802+
readonly modelSwitch?: OrchestrationV2ModelSwitch | undefined;
47994803
}) {
48004804
const message: OrchestrationV2ConversationMessage = {
48014805
createdBy: input.createdBy,
@@ -4851,6 +4855,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
48514855
text: input.text,
48524856
attachments: input.attachments,
48534857
...(input.context ? { context: input.context } : {}),
4858+
...(messageInput.modelSwitch === undefined
4859+
? {}
4860+
: { modelSwitch: messageInput.modelSwitch }),
48544861
};
48554862
yield* emitEvent({
48564863
type: "message.updated",
@@ -5311,6 +5318,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
53115318
providerTurnId: null,
53125319
providerThreadId: restartProviderThread.id,
53135320
providerInstanceId: input.modelSelection.instanceId,
5321+
// Measured on the pre-restart projection, so the rewritten run is not its own baseline.
5322+
...(restartHandoff === null ? modelSwitchSince(input.projection, input.modelSelection) : {}),
53145323
});
53155324
const interruptedAttemptId = targetRun.activeAttemptId;
53165325
if (interruptedAttemptId === null) {
@@ -6424,6 +6433,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
64246433
text: dispatchText,
64256434
...(command.context ? { context: command.context } : {}),
64266435
attachments: command.attachments,
6436+
...(run.contextHandoffId === null ? modelSwitchSince(projection, modelSelection) : {}),
64276437
};
64286438
const preparationTurnItem: OrchestrationV2TurnItem | null =
64296439
dispatchMode.type === "defer_start"
@@ -7175,6 +7185,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
71757185
text: dispatchText,
71767186
...(command.context ? { context: command.context } : {}),
71777187
attachments: command.attachments,
7188+
...(run.contextHandoffId === null
7189+
? modelSwitchSince(projection, modelSelection, sourceRun?.modelSelection)
7190+
: {}),
71787191
};
71797192
const activeHandoff = portableForkHandoff ?? mergeBackHandoff ?? providerSwitchHandoff;
71807193
const handoffSourceRuns =
@@ -9349,7 +9362,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
93499362
"runtimeRequests",
93509363
"subagents",
93519364
],
9352-
{ turnItemTypes: [], messageRoles: ["user"] },
9365+
// User messages measure a restart's model switch.
9366+
{ turnItemTypes: ["user_message"], messageRoles: ["user"] },
93539367
);
93549368
if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) {
93559369
return yield* new OrchestratorDispatchError({

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MessageId,
66
type ModelSelection,
77
type OrchestrationV2ProviderCapabilities,
8+
type OrchestrationV2ThreadProjection,
89
type OrchestrationV2ProviderSession,
910
type OrchestrationV2ProviderThread,
1011
ProjectId,
@@ -47,6 +48,10 @@ const seedSelection = {
4748
instanceId: providerInstanceId,
4849
model: "seed-model",
4950
} satisfies ModelSelection;
51+
const modelSwitches = (projection: OrchestrationV2ThreadProjection) =>
52+
projection.turnItems.flatMap((item) =>
53+
item.type === "user_message" && item.modelSwitch !== undefined ? [item.modelSwitch] : [],
54+
);
5055
const pooledCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
5156
const exclusiveCapabilities: OrchestrationV2ProviderCapabilities = {
5257
...CodexProviderCapabilitiesV2,
@@ -407,6 +412,9 @@ it.live("retries a failed selection restart session open before completing", ()
407412
"selection restart supersede must not project hard-Stop interrupt items",
408413
);
409414
assert.equal(projection.runs.at(-1)?.modelSelection.model, replacementSelection.model);
415+
assert.deepEqual(modelSwitches(projection), [
416+
{ from: initialSelection, to: replacementSelection },
417+
]);
410418
assert.equal(projection.runs.at(-1)?.status, "completed");
411419
assert.isTrue(captured.failedReplacementOpen);
412420
assert.equal(captured.closedSessionCount, 0);
@@ -690,6 +698,9 @@ it.live(
690698
const { projection, captured } = result;
691699
assert.lengthOf(projection.runs, 2);
692700
assert.equal(projection.runs[1]?.modelSelection.model, replacementSelection.model);
701+
assert.deepEqual(modelSwitches(projection), [
702+
{ from: seedSelection, to: replacementSelection },
703+
]);
693704
assert.deepEqual(result.detachedSessionIds, [result.liveSessionId]);
694705
assert.equal(captured.closedSessionCount, 1);
695706
assert.deepEqual(

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,55 @@ it.effect("steers a promoted queued message with the selection it was queued wit
11751175
),
11761176
);
11771177

1178+
it.effect("marks the model switch on a promoted message that restarts the turn", () =>
1179+
Effect.scoped(
1180+
Effect.gen(function* () {
1181+
const { started, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
1182+
"promote-restart-switch",
1183+
true,
1184+
false,
1185+
);
1186+
const otherSelection = { ...runSelection, model: "other-model" };
1187+
yield* Effect.gen(function* () {
1188+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1189+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1190+
const threadId = yield* startFirstTurn;
1191+
yield* orchestrator.dispatch({
1192+
type: "message.dispatch",
1193+
commandId: CommandId.make("queued"),
1194+
threadId,
1195+
messageId: MessageId.make("message:queued"),
1196+
text: "queued",
1197+
attachments: [],
1198+
modelSelection: otherSelection,
1199+
dispatchMode: { type: "queue_after_active" },
1200+
createdBy: "user",
1201+
creationSource: "web",
1202+
});
1203+
const queuedRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
1204+
(run) => run.status === "queued",
1205+
)!;
1206+
yield* orchestrator.dispatch({
1207+
type: "queued-message.promote-to-steer",
1208+
commandId: CommandId.make("promote"),
1209+
threadId,
1210+
queuedRunId: queuedRun.id,
1211+
targetRunId: started[0]!.runId,
1212+
});
1213+
yield* worker.drain();
1214+
1215+
const promoted = (yield* orchestrator.getThreadProjection(threadId)).turnItems.find(
1216+
(item) => item.type === "user_message" && item.messageId === "message:queued",
1217+
);
1218+
assert.deepEqual(promoted?.type === "user_message" ? promoted.modelSwitch : undefined, {
1219+
from: runSelection,
1220+
to: otherSelection,
1221+
});
1222+
}).pipe(Effect.provide(layer));
1223+
}),
1224+
),
1225+
);
1226+
11781227
it.effect("keeps a queued message queued when promoted into a run that Stop reached", () =>
11791228
Effect.scoped(
11801229
Effect.gen(function* () {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {
2+
ContextHandoffId,
3+
MessageId,
4+
ProviderInstanceId,
5+
type OrchestrationV2ThreadProjection,
6+
} from "@t3tools/contracts";
7+
import { describe, expect, it } from "vite-plus/test";
8+
9+
import { modelSwitchSince } from "./modelSwitch.ts";
10+
11+
const instanceId = ProviderInstanceId.make("codex");
12+
const selection = (model: string) => ({ instanceId, model });
13+
const run = (
14+
message: string,
15+
ordinal: number,
16+
model: string,
17+
status: OrchestrationV2ThreadProjection["runs"][number]["status"] = "completed",
18+
) =>
19+
({
20+
ordinal,
21+
status,
22+
completedAt: null,
23+
contextHandoffId: null,
24+
userMessageId: MessageId.make(message),
25+
modelSelection: selection(model),
26+
}) as OrchestrationV2ThreadProjection["runs"][number];
27+
const shown = (...messages: ReadonlyArray<string>) =>
28+
messages.map((message) => ({
29+
type: "user_message",
30+
messageId: MessageId.make(message),
31+
inputIntent: "turn_start",
32+
status: "completed",
33+
text: message,
34+
attachments: [],
35+
})) as unknown as OrchestrationV2ThreadProjection["turnItems"];
36+
37+
describe("modelSwitchSince", () => {
38+
it("measures from the latest run whose message is in the timeline", () => {
39+
// B failed before it started; its message shows "a -> b", so the next one must show "b -> a".
40+
expect(
41+
modelSwitchSince(
42+
{
43+
runs: [
44+
run("first", 1, "a"),
45+
run("failed", 2, "b", "failed"),
46+
run("queued", 3, "c", "queued"),
47+
run("undone", 4, "d", "rolled_back"),
48+
],
49+
turnItems: shown("first", "failed", "queued", "undone"),
50+
},
51+
selection("a"),
52+
),
53+
).toEqual({ modelSwitch: { from: selection("b"), to: selection("a") } });
54+
});
55+
56+
it("skips messages clients hide", () => {
57+
const [first, pending, compact, answer] = shown(
58+
"first",
59+
"pending",
60+
"/compact",
61+
"async-answer:question",
62+
);
63+
expect(
64+
modelSwitchSince(
65+
{
66+
runs: [
67+
run("first", 1, "a"),
68+
run("pending", 2, "b", "cancelled"),
69+
run("/compact", 3, "c"),
70+
run("async-answer:question", 4, "d"),
71+
],
72+
turnItems: [
73+
first!,
74+
{ ...pending!, inputIntent: "queued_turn", status: "pending" },
75+
compact!,
76+
answer!,
77+
] as OrchestrationV2ThreadProjection["turnItems"],
78+
},
79+
selection("c"),
80+
),
81+
).toEqual({ modelSwitch: { from: selection("a"), to: selection("c") } });
82+
});
83+
84+
it("counts a wake whose handoff row shows the swap", () => {
85+
const wake = {
86+
...run("wake", 2, "other"),
87+
contextHandoffId: ContextHandoffId.make("handoff"),
88+
};
89+
expect(
90+
modelSwitchSince(
91+
{ runs: [run("first", 1, "a"), wake], turnItems: shown("first") },
92+
selection("other"),
93+
),
94+
).toEqual({});
95+
});
96+
97+
it("records nothing for a first run or an unchanged selection", () => {
98+
expect(modelSwitchSince({ runs: [], turnItems: [] }, selection("a"))).toEqual({});
99+
// A fork's first message compares with the run it forked from.
100+
expect(modelSwitchSince({ runs: [], turnItems: [] }, selection("a"), selection("b"))).toEqual({
101+
modelSwitch: { from: selection("b"), to: selection("a") },
102+
});
103+
expect(
104+
modelSwitchSince({ runs: [run("first", 1, "a")], turnItems: shown("first") }, selection("a")),
105+
).toEqual({});
106+
});
107+
});

0 commit comments

Comments
 (0)