Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: show the correct LLM profile per conversation (#1082) (#1123)

Vasco Schiavoe1b9eeae9fb3
diff --git a/__tests__/api/agent-server-adapter.test.ts b/__tests__/api/agent-server-adapter.test.ts
index e14c7bbd1e..2ced037888 100644
--- a/__tests__/api/agent-server-adapter.test.ts
+++ b/__tests__/api/agent-server-adapter.test.ts
@@ -624,6 +624,27 @@ describe("toAppConversation", () => {
}
});
+ it("hydrates active_profile from stored metadata so the switcher shows the exact profile (#1082)", () => {
+ setStoredConversationMetadata(baseInfo.id, {
+ selected_repository: null,
+ selected_branch: null,
+ git_provider: null,
+ active_profile: "claude-sonnet-4.6",
+ });
+ try {
+ const result = toAppConversation({
+ ...baseInfo,
+ agent: {
+ kind: "Agent",
+ llm: { model: "litellm_proxy/claude-sonnet-4-6" },
+ },
+ });
+ expect(result.active_profile).toBe("claude-sonnet-4.6");
+ } finally {
+ removeStoredConversationMetadata(baseInfo.id);
+ }
+ });
+
it("marks openhands conversations and surfaces the agent.llm.model", () => {
const result = toAppConversation({
...baseInfo,
diff --git a/__tests__/api/use-create-conversation-metadata.test.ts b/__tests__/api/use-create-conversation-metadata.test.ts
index e3ab1536cc..5fae5835c3 100644
--- a/__tests__/api/use-create-conversation-metadata.test.ts
+++ b/__tests__/api/use-create-conversation-metadata.test.ts
@@ -15,12 +15,14 @@ const {
mockSettingsClient,
mockGetSettings,
mockGetSettingsForConversation,
+ mockUseLlmProfiles,
} = vi.hoisted(() => ({
mockHttpPost: vi.fn(),
mockConversationClient: vi.fn(),
mockSettingsClient: vi.fn(),
mockGetSettings: vi.fn(),
mockGetSettingsForConversation: vi.fn(),
+ mockUseLlmProfiles: vi.fn(),
}));
vi.mock("@openhands/typescript-client/clients", async () => {
@@ -66,6 +68,10 @@ vi.mock("#/hooks/use-tracking", () => ({
useTracking: () => ({ trackConversationCreated: vi.fn() }),
}));
+vi.mock("#/hooks/query/use-llm-profiles", () => ({
+ useLlmProfiles: () => mockUseLlmProfiles(),
+}));
+
const wrapper = ({ children }: { children: React.ReactNode }) => {
const client = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
@@ -76,6 +82,10 @@ const wrapper = ({ children }: { children: React.ReactNode }) => {
describe("useCreateConversation persists selected repository metadata", () => {
beforeEach(() => {
window.localStorage.clear();
+ mockUseLlmProfiles.mockReset();
+ // Default: no active profile, so metadata is written only for repo/
+ // workspace attachments (as before). Individual tests override this.
+ mockUseLlmProfiles.mockReturnValue({ data: { active_profile: null } });
mockHttpPost.mockReset();
mockGetSettings.mockReset();
mockGetSettingsForConversation.mockReset();
@@ -164,4 +174,24 @@ describe("useCreateConversation persists selected repository metadata", () => {
expect(getStoredConversationMetadata("conv-new")).toBeNull();
});
+
+ it("stamps the active LLM profile even when no repo or workspace is attached (#1082)", async () => {
+ mockUseLlmProfiles.mockReturnValue({
+ data: { active_profile: "team-default" },
+ });
+
+ const { result } = renderHook(() => useCreateConversation(), { wrapper });
+
+ result.current.mutate({ query: "scratch session" });
+
+ await waitFor(() => expect(result.current.isSuccess).toBe(true));
+
+ expect(getStoredConversationMetadata("conv-new")).toEqual({
+ selected_repository: null,
+ selected_branch: null,
+ git_provider: null,
+ selected_workspace: null,
+ active_profile: "team-default",
+ });
+ });
});
diff --git a/__tests__/components/features/chat/switch-profile-button.test.tsx b/__tests__/components/features/chat/switch-profile-button.test.tsx
index be02276003..39d318ada8 100644
--- a/__tests__/components/features/chat/switch-profile-button.test.tsx
+++ b/__tests__/components/features/chat/switch-profile-button.test.tsx
@@ -41,6 +41,24 @@ const profiles = [
{ name: "gpt", model: "openai/gpt-4o", base_url: null, api_key_set: true },
];
+// Two profiles that resolve to the SAME underlying model but differ by name
+// β€” the #1082 scenario. "broken" is listed first (alphabetical) so a
+// model-only match would wrongly pick it over the user's actual choice.
+const sameModelProfiles = [
+ {
+ name: "broken",
+ model: "litellm_proxy/claude-sonnet-4-6",
+ base_url: null,
+ api_key_set: true,
+ },
+ {
+ name: "claude-sonnet-4.6",
+ model: "litellm_proxy/claude-sonnet-4-6",
+ base_url: null,
+ api_key_set: true,
+ },
+];
+
describe("SwitchProfileButton", () => {
const switchAndLog = vi.fn();
@@ -164,4 +182,65 @@ describe("SwitchProfileButton", () => {
screen.queryByTestId("switch-profile-button"),
).not.toBeInTheDocument();
});
+
+ it("labels the button with the conversation's stamped profile when several profiles share a model (#1082)", () => {
+ useLlmProfilesMock.mockReturnValue({
+ data: { profiles: sameModelProfiles, active_profile: "broken" },
+ });
+ useActiveConversationMock.mockReturnValue({
+ data: {
+ id: "conv-1",
+ agent_kind: "openhands",
+ llm_model: "litellm_proxy/claude-sonnet-4-6",
+ active_profile: "claude-sonnet-4.6",
+ },
+ });
+
+ renderWithProviders(<SwitchProfileButton />);
+
+ const button = screen.getByTestId("switch-profile-button");
+ expect(button).toHaveTextContent("claude-sonnet-4.6");
+ expect(button).not.toHaveTextContent("broken");
+ });
+
+ it("falls back to model-matching when the conversation has no stamped profile", () => {
+ useLlmProfilesMock.mockReturnValue({
+ data: { profiles: sameModelProfiles, active_profile: "broken" },
+ });
+ useActiveConversationMock.mockReturnValue({
+ data: {
+ id: "conv-1",
+ agent_kind: "openhands",
+ llm_model: "litellm_proxy/claude-sonnet-4-6",
+ },
+ });
+
+ renderWithProviders(<SwitchProfileButton />);
+
+ // No stamp (e.g. created by an older client) β†’ first profile whose model
+ // matches, preserving today's behavior.
+ expect(screen.getByTestId("switch-profile-button")).toHaveTextContent(
+ "broken",
+ );
+ });
+
+ it("ignores a stamped profile that no longer exists and falls back to model-matching", () => {
+ useLlmProfilesMock.mockReturnValue({
+ data: { profiles: sameModelProfiles, active_profile: "broken" },
+ });
+ useActiveConversationMock.mockReturnValue({
+ data: {
+ id: "conv-1",
+ agent_kind: "openhands",
+ llm_model: "litellm_proxy/claude-sonnet-4-6",
+ active_profile: "deleted-profile",
+ },
+ });
+
+ renderWithProviders(<SwitchProfileButton />);
+
+ expect(screen.getByTestId("switch-profile-button")).toHaveTextContent(
+ "broken",
+ );
+ });
});
diff --git a/__tests__/hooks/mutation/use-create-conversation.test.tsx b/__tests__/hooks/mutation/use-create-conversation.test.tsx
index c65641901f..ebc562b165 100644
--- a/__tests__/hooks/mutation/use-create-conversation.test.tsx
+++ b/__tests__/hooks/mutation/use-create-conversation.test.tsx
@@ -11,6 +11,17 @@ vi.mock("#/hooks/use-tracking", () => ({
}),
}));
+// The hook stamps the active LLM profile onto the conversation (#1082).
+// Mock it so the captured value is deterministic β€” the real hook fires a
+// query the global MSW layer would answer non-deterministically under test
+// timing.
+const { useLlmProfilesMock } = vi.hoisted(() => ({
+ useLlmProfilesMock: vi.fn(() => ({ data: { active_profile: null } })),
+}));
+vi.mock("#/hooks/query/use-llm-profiles", () => ({
+ useLlmProfiles: () => useLlmProfilesMock(),
+}));
+
describe("useCreateConversation", () => {
it("passes suggested tasks to the V1 create conversation API", async () => {
const createConversationSpy = vi
@@ -88,32 +99,33 @@ describe("useCreateConversation", () => {
});
it("invalidates the conversation list and start-tasks queries on success", async () => {
- vi.spyOn(AgentServerConversationService, "createConversation").mockResolvedValue(
- {
- id: "task-id",
- created_by_user_id: null,
- status: "READY",
- detail: null,
- app_conversation_id: "conv-1",
- agent_server_url: "http://agent-server.local",
- request: {
- initial_message: null,
- processors: [],
- llm_model: null,
- selected_repository: null,
- selected_branch: null,
- git_provider: "github",
- suggested_task: null,
- title: null,
- trigger: null,
- pr_number: [],
- parent_conversation_id: null,
- agent_type: "default",
- },
- created_at: new Date().toISOString(),
- updated_at: new Date().toISOString(),
+ vi.spyOn(
+ AgentServerConversationService,
+ "createConversation",
+ ).mockResolvedValue({
+ id: "task-id",
+ created_by_user_id: null,
+ status: "READY",
+ detail: null,
+ app_conversation_id: "conv-1",
+ agent_server_url: "http://agent-server.local",
+ request: {
+ initial_message: null,
+ processors: [],
+ llm_model: null,
+ selected_repository: null,
+ selected_branch: null,
+ git_provider: "github",
+ suggested_task: null,
+ title: null,
+ trigger: null,
+ pr_number: [],
+ parent_conversation_id: null,
+ agent_type: "default",
},
- );
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ });
const queryClient = new QueryClient();
const invalidateSpy = vi.spyOn(queryClient, "invalidateQueries");
diff --git a/__tests__/hooks/mutation/use-switch-llm-profile-and-log.test.tsx b/__tests__/hooks/mutation/use-switch-llm-profile-and-log.test.tsx
new file mode 100644
index 0000000000..421fb74f8b
--- /dev/null
+++ b/__tests__/hooks/mutation/use-switch-llm-profile-and-log.test.tsx
@@ -0,0 +1,76 @@
+import { renderHook } from "@testing-library/react";
+import { describe, expect, it, vi, beforeEach } from "vitest";
+
+// The wrapper drives the base mutation; mock it so we can deterministically
+// trigger the success / error callbacks the wrapper passes in.
+const switchMutateMock = vi.fn();
+vi.mock("#/hooks/mutation/use-switch-llm-profile", () => ({
+ useSwitchLlmProfile: () => ({ mutate: switchMutateMock, isPending: false }),
+}));
+
+vi.mock("#/hooks/chat/record-model-switch-message", () => ({
+ recordModelSwitchMessage: vi.fn(),
+}));
+
+vi.mock("#/hooks/chat/model-command-event-anchor", () => ({
+ getLastRenderableEventId: () => null,
+}));
+
+vi.mock("#/utils/custom-toast-handlers", () => ({
+ displayErrorToast: vi.fn(),
+}));
+
+import { useSwitchLlmProfileAndLog } from "#/hooks/mutation/use-switch-llm-profile-and-log";
+import {
+ getStoredConversationMetadata,
+ setStoredConversationMetadata,
+} from "#/api/conversation-metadata-store";
+
+describe("useSwitchLlmProfileAndLog", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ window.localStorage.clear();
+ });
+
+ it("stamps the switched-to profile onto the conversation metadata, preserving repo/workspace (#1082)", () => {
+ switchMutateMock.mockImplementation((_vars, opts) => opts?.onSuccess?.());
+ // Repo metadata persisted at creation must survive the merge.
+ setStoredConversationMetadata("conv-1", {
+ selected_repository: "octocat/hello-world",
+ selected_branch: "main",
+ git_provider: "github",
+ });
+
+ const { result } = renderHook(() => useSwitchLlmProfileAndLog());
+ result.current.switchAndLog("conv-1", "claude-sonnet-4.6");
+
+ expect(getStoredConversationMetadata("conv-1")).toEqual({
+ selected_repository: "octocat/hello-world",
+ selected_branch: "main",
+ git_provider: "github",
+ selected_workspace: null,
+ active_profile: "claude-sonnet-4.6",
+ });
+ });
+
+ it("does not stamp metadata for the home-page activate path (conversationId === null)", () => {
+ switchMutateMock.mockImplementation((_vars, opts) => opts?.onSuccess?.());
+
+ const { result } = renderHook(() => useSwitchLlmProfileAndLog());
+ result.current.switchAndLog(null, "claude-sonnet-4.6");
+
+ // No conversation to scope the stamp to β†’ nothing written.
+ expect(getStoredConversationMetadata("conv-1")).toBeNull();
+ });
+
+ it("does not stamp metadata when the switch fails", () => {
+ switchMutateMock.mockImplementation((_vars, opts) =>
+ opts?.onError?.(new Error("boom")),
+ );
+
+ const { result } = renderHook(() => useSwitchLlmProfileAndLog());
+ result.current.switchAndLog("conv-1", "claude-sonnet-4.6");
+
+ expect(getStoredConversationMetadata("conv-1")).toBeNull();
+ });
+});
diff --git a/src/api/agent-server-adapter.ts b/src/api/agent-server-adapter.ts
index 044a3bc64d..3695cb8c5f 100644
--- a/src/api/agent-server-adapter.ts
+++ b/src/api/agent-server-adapter.ts
@@ -268,6 +268,7 @@ export function toAppConversation(
selected_branch: metadata?.selected_branch ?? null,
git_provider: metadata?.git_provider ?? null,
selected_workspace: metadata?.selected_workspace ?? null,
+ active_profile: metadata?.active_profile ?? null,
title: info.title?.trim()
? info.title
: getDefaultConversationTitle(info.id),
diff --git a/src/api/conversation-metadata-store.ts b/src/api/conversation-metadata-store.ts
index 7f129837f5..a9de5aa7b6 100644
--- a/src/api/conversation-metadata-store.ts
+++ b/src/api/conversation-metadata-store.ts
@@ -15,6 +15,14 @@ export interface ConversationMetadata {
* git baseline to compare against.
*/
selected_workspace?: string | null;
+ /**
+ * The LLM profile the conversation was created with (or last switched to).
+ * Client-side only. Lets the chat-header switcher show the exact profile
+ * name even when several profiles share the same underlying model β€” the
+ * agent-server only round-trips the model string, so matching on it alone
+ * is ambiguous (issue #1082).
+ */
+ active_profile?: string | null;
}
type StoredMetadata = Record<string, ConversationMetadata>;
diff --git a/src/api/conversation-service/agent-server-conversation-service.types.ts b/src/api/conversation-service/agent-server-conversation-service.types.ts
index 09243fdfb4..a8b9274de2 100644
--- a/src/api/conversation-service/agent-server-conversation-service.types.ts
+++ b/src/api/conversation-service/agent-server-conversation-service.types.ts
@@ -166,6 +166,15 @@ export interface AppConversation {
* the runtime actually operates in).
*/
selected_workspace?: string | null;
+ /**
+ * The LLM profile this conversation was created with / last switched to.
+ * Hydrated from client-side metadata (see
+ * `ConversationMetadata.active_profile`). Preferred over matching
+ * `llm_model` against the profile list, which is ambiguous when several
+ * profiles share a model (#1082). Null when unknown (e.g. created by an
+ * older client) β€” consumers fall back to model-matching.
+ */
+ active_profile?: string | null;
public?: boolean;
sub_conversation_ids: string[];
}
diff --git a/src/components/features/chat/switch-profile-button.tsx b/src/components/features/chat/switch-profile-button.tsx
index 03340f2b9c..0301629635 100644
--- a/src/components/features/chat/switch-profile-button.tsx
+++ b/src/components/features/chat/switch-profile-button.tsx
@@ -49,11 +49,21 @@ export function SwitchProfileButton() {
// Resolution priority for the active profile name:
// 1. Optimistic (just-clicked) β€” instant feedback before the refetch.
- // 2. Profile whose model matches the running llm_model β€” cold loads.
- // 3. User-level active_profile β€” home page / before the conversation has
+ // 2. Profile stamped on the conversation at creation / last switch β€”
+ // exact identity, survives reload, and is unambiguous when several
+ // profiles share one underlying model (#1082). Validated against the
+ // live list so a since-deleted/renamed profile falls through.
+ // 3. Profile whose model matches the running llm_model β€” legacy fallback.
+ // 4. User-level active_profile β€” home page / before the conversation has
// sent any messages.
+ const stampedProfile = conversation?.active_profile ?? null;
+ const conversationProfile =
+ stampedProfile && profiles.some((p) => p.name === stampedProfile)
+ ? stampedProfile
+ : null;
const activeProfileName =
optimisticActiveProfile ??
+ conversationProfile ??
(conversationModel
? (profiles.find((p) => p.model === conversationModel)?.name ?? null)
: (data?.active_profile ?? null));
diff --git a/src/hooks/mutation/use-create-conversation.ts b/src/hooks/mutation/use-create-conversation.ts
index cd76f30725..41fedff3ce 100644
--- a/src/hooks/mutation/use-create-conversation.ts
+++ b/src/hooks/mutation/use-create-conversation.ts
@@ -4,6 +4,11 @@ import { PluginSpec } from "#/api/conversation-service/agent-server-conversation
import { SuggestedTask } from "#/utils/types";
import { Provider } from "#/types/settings";
import { useTracking } from "#/hooks/use-tracking";
+import { useLlmProfiles } from "#/hooks/query/use-llm-profiles";
+import {
+ getStoredConversationMetadata,
+ setStoredConversationMetadata,
+} from "#/api/conversation-metadata-store";
interface CreateConversationVariables {
query?: string;
@@ -30,6 +35,10 @@ interface CreateConversationResponse {
export const useCreateConversation = () => {
const queryClient = useQueryClient();
const { trackConversationCreated } = useTracking();
+ // Cache-warm on the home page (the profile picker reads the same query).
+ // Stamped onto the conversation at creation so the switcher can show the
+ // exact profile even when several profiles share a model (#1082).
+ const { data: llmProfiles } = useLlmProfiles();
return useMutation({
mutationKey: ["create-conversation"],
@@ -63,6 +72,23 @@ export const useCreateConversation = () => {
agentType,
);
+ // Stamp the active LLM profile onto the (local) conversation so the
+ // chat switcher shows the exact profile even when several profiles
+ // share a model (#1082). Cloud conversations don't use local profiles
+ // (app_conversation_id stays null until the sandbox is READY). Merge so
+ // the repo/workspace metadata the service just persisted is preserved.
+ const localConversationId = conversation.app_conversation_id;
+ if (localConversationId && llmProfiles?.active_profile) {
+ const prev = getStoredConversationMetadata(localConversationId);
+ setStoredConversationMetadata(localConversationId, {
+ selected_repository: prev?.selected_repository ?? null,
+ selected_branch: prev?.selected_branch ?? null,
+ git_provider: prev?.git_provider ?? null,
+ selected_workspace: prev?.selected_workspace ?? null,
+ active_profile: llmProfiles.active_profile,
+ });
+ }
+
// OpenHands cloud pattern: when the start task isn't immediately
// READY (cloud sandbox is still provisioning),
// app_conversation_id is null. We return a `task-{id}` URL so the
diff --git a/src/hooks/mutation/use-switch-llm-profile-and-log.ts b/src/hooks/mutation/use-switch-llm-profile-and-log.ts
index 28cb2197e1..752594f261 100644
--- a/src/hooks/mutation/use-switch-llm-profile-and-log.ts
+++ b/src/hooks/mutation/use-switch-llm-profile-and-log.ts
@@ -3,6 +3,10 @@ import { useTranslation } from "react-i18next";
import { getLastRenderableEventId } from "#/hooks/chat/model-command-event-anchor";
import { recordModelSwitchMessage } from "#/hooks/chat/record-model-switch-message";
import { useSwitchLlmProfile } from "#/hooks/mutation/use-switch-llm-profile";
+import {
+ getStoredConversationMetadata,
+ setStoredConversationMetadata,
+} from "#/api/conversation-metadata-store";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { I18nKey } from "#/i18n/declaration";
@@ -32,6 +36,17 @@ export function useSwitchLlmProfileAndLog() {
profileName,
anchorEventId,
);
+ // Keep the per-conversation profile identity fresh so the
+ // chat-header switcher shows the right name after a reload
+ // (the agent-server only round-trips the model string). #1082
+ const prev = getStoredConversationMetadata(conversationId);
+ setStoredConversationMetadata(conversationId, {
+ selected_repository: prev?.selected_repository ?? null,
+ selected_branch: prev?.selected_branch ?? null,
+ git_provider: prev?.git_provider ?? null,
+ selected_workspace: prev?.selected_workspace ?? null,
+ active_profile: profileName,
+ });
}
},
onError: (err: unknown) => {