Skip to content

Commit 5bcebc3

Browse files
committed
fix: retire model selection ownership
1 parent 3940d1f commit 5bcebc3

6 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎ui/src/pages/chat/chat-command-executor.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function createSessionCapability(client: GatewayBrowserClient): SessionCapabilit
2828
create: async () => null,
2929
patch: (key: string, patch: SessionPatch, options: SessionPatchOptions = {}) =>
3030
request("sessions.patch", { key, agentId: options.agentId, ...patch }),
31+
setModelOverride: () => undefined,
3132
delete: async () => false,
3233
deleteMany: async () => ({ deleted: [], errors: [], preservedWorktrees: [] }),
3334
reset: async () => true,
@@ -176,6 +177,7 @@ describe("executeSlashCommand directives", () => {
176177
expect.objectContaining({
177178
agentId: "work",
178179
deferModelOverride: true,
180+
ownsModelOverride: expect.any(Function),
179181
}),
180182
);
181183
expect(setModelOverride).toHaveBeenCalledWith("global", "openai/gpt-5-mini");

‎ui/src/pages/chat/chat-command-executor.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ async function executeModel(
316316
},
317317
{
318318
deferModelOverride: true,
319+
ownsModelOverride: context.ownsModelOverride,
319320
reconcile: async (result) => {
320321
const resolvedModel = result.resolved?.model ?? requestedModel;
321322
let resolvedValue = resolvePreferredServerChatModelValue(

‎ui/src/pages/chat/chat-pane-context.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { resolveSessionKey } from "../../lib/sessions/index.ts";
1414
import {
1515
buildAgentMainSessionKey,
1616
canonicalUiSessionKeyForPersistence,
17+
isUiGlobalSessionKey,
1718
parseAgentSessionKey,
1819
resolveUiConfiguredMainKey,
1920
uiSessionEventMatches,
@@ -26,6 +27,7 @@ import { resolveAssistantAttachmentAuthToken } from "./chat-pane-state.ts";
2627
import { markQueuedChatSendsWaitingForReconnect } from "./chat-queue.ts";
2728
import { stopChatRealtimeTalk } from "./chat-realtime.ts";
2829
import { retryReconnectableQueuedChatSends } from "./chat-send-actions.ts";
30+
import { retireChatModelSelectionOwnership } from "./chat-session.ts";
2931
import {
3032
invalidateChatMetadataCache,
3133
refreshChatModelAuthStatus,
@@ -169,6 +171,7 @@ export abstract class ChatPaneContext extends ChatPaneLifecycle {
169171
}
170172
const previousMediaAuthToken = resolveAssistantAttachmentAuthToken(state);
171173
const wasConnected = state.connected;
174+
const previousAssistantAgentId = state.assistantAgentId;
172175
const previousSidebarSessionKey = canonicalUiSessionKeyForPersistence(state, state.sessionKey);
173176
const connectionLifecycle = (this.gatewayConnectionLifecycle ??=
174177
createGatewayConnectionLifecycle({
@@ -216,6 +219,13 @@ export abstract class ChatPaneContext extends ChatPaneLifecycle {
216219
this.resetOlderMessagesViewport();
217220
state.chatLoading = false;
218221
}
222+
if (
223+
sourceChanged ||
224+
(previousAssistantAgentId !== snapshot.assistantAgentId &&
225+
isUiGlobalSessionKey(state.sessionKey))
226+
) {
227+
retireChatModelSelectionOwnership(state);
228+
}
219229
state.client = snapshot.client;
220230
state.connected = snapshot.phase === "connected";
221231
state.connectionEpoch = this.connectionGeneration;

‎ui/src/pages/chat/chat-pane-lifecycle.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,51 @@ describe("chat pane connection lifecycle", () => {
920920
expect(state.chatIsProgrammaticScroll).toBe(false);
921921
});
922922

923+
it("retires pending model selection state when the Gateway owner changes", () => {
924+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
925+
const setModelOverride = vi.fn();
926+
const sessions = { setModelOverride } as unknown as SessionCapability;
927+
const { pane, state } = createTestChatPane({ client, sessions });
928+
state.sessionKey = "global";
929+
state.chatModelSwitchPromises = {
930+
global: new Promise<boolean>(() => {}),
931+
};
932+
933+
pane.applyGatewaySnapshot({
934+
...pane.context.gateway.snapshot,
935+
client,
936+
phase: "reconnecting",
937+
hello: null,
938+
});
939+
940+
expect(state.chatModelSwitchPromises).toEqual({});
941+
expect(setModelOverride).toHaveBeenCalledWith("global", undefined);
942+
});
943+
944+
it("retires pending global model selection state when the selected agent changes", () => {
945+
const client = { request: vi.fn() } as unknown as GatewayBrowserClient;
946+
const setModelOverride = vi.fn();
947+
const sessions = { setModelOverride } as unknown as SessionCapability;
948+
const { pane, state } = createTestChatPane({ client, sessions });
949+
const snapshot = {
950+
...pane.context.gateway.snapshot,
951+
client,
952+
phase: "connected" as const,
953+
assistantAgentId: "work",
954+
};
955+
pane.applyGatewaySnapshot(snapshot);
956+
state.sessionKey = "global";
957+
state.chatModelSwitchPromises = {
958+
global: new Promise<boolean>(() => {}),
959+
};
960+
setModelOverride.mockClear();
961+
962+
pane.applyGatewaySnapshot({ ...snapshot, assistantAgentId: "main" });
963+
964+
expect(state.chatModelSwitchPromises).toEqual({});
965+
expect(setModelOverride).toHaveBeenCalledWith("global", undefined);
966+
});
967+
923968
it("refreshes the transcript before secondary hydration after a same-client reconnect", () => {
924969
const request = vi.fn(() => new Promise<never>(() => {}));
925970
const client = {

‎ui/src/pages/chat/chat-session.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ type ChatIdleSessionReconciliationHost = SessionScopeHost & {
5454
sessionsResult?: SessionsListResult | null;
5555
};
5656

57+
export function retireChatModelSelectionOwnership(
58+
host: Pick<
59+
ChatModelSettingsHost,
60+
"chatModelSwitchPromises" | "requestUpdate" | "sessionKey" | "sessions"
61+
>,
62+
): void {
63+
const hasPendingSwitch = Object.keys(host.chatModelSwitchPromises ?? {}).length > 0;
64+
const hasModelOverride = Object.hasOwn(
65+
host.sessions.state?.modelOverrides ?? {},
66+
host.sessionKey,
67+
);
68+
if (!hasPendingSwitch && !hasModelOverride) {
69+
return;
70+
}
71+
host.chatModelSwitchPromises = {};
72+
host.sessions.setModelOverride(host.sessionKey, undefined);
73+
host.requestUpdate?.();
74+
}
75+
5776
function buildChatSessionListOptions(
5877
state: ChatSessionListHost,
5978
options: { offset?: number; append?: boolean; search?: string | null } = {},

‎ui/src/pages/chat/chat-state-page.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { fetchAssistantIdentity } from "../../app/assistant-identity.ts";
44
import type { ApplicationContext } from "../../app/context.ts";
55
import { loadLocalUserIdentity, loadSettings, patchSettings } from "../../app/settings.ts";
66
import { resolveSafeExternalUrl } from "../../lib/open-external-url.ts";
7-
import { canonicalUiSessionKeyForPersistence } from "../../lib/sessions/session-key.ts";
7+
import {
8+
canonicalUiSessionKeyForPersistence,
9+
isUiGlobalSessionKey,
10+
} from "../../lib/sessions/session-key.ts";
811
import { resolveAgentIdForSession } from "./chat-avatar.ts";
912
import { removeQueuedMessage } from "./chat-queue.ts";
1013
import { attachChatRealtimeActions, createInitialChatRealtimeState } from "./chat-realtime.ts";
@@ -14,6 +17,7 @@ import {
1417
steerQueuedChatMessage,
1518
} from "./chat-send-actions.ts";
1619
import { handleSendChat } from "./chat-send-submit.ts";
20+
import { retireChatModelSelectionOwnership } from "./chat-session.ts";
1721
import type { ChatPageHost } from "./chat-state-host.ts";
1822
import {
1923
handleChatDraftChange,
@@ -86,6 +90,12 @@ async function loadPageAssistantIdentity(
8690
) {
8791
return;
8892
}
93+
if (
94+
state.assistantAgentId !== (identity.agentId ?? null) &&
95+
isUiGlobalSessionKey(state.sessionKey)
96+
) {
97+
retireChatModelSelectionOwnership(state);
98+
}
8999
state.assistantName = identity.name;
90100
state.assistantAvatar = identity.avatar;
91101
state.assistantAvatarSource = identity.avatarSource ?? null;

0 commit comments

Comments
 (0)