Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(settings): flag taken names in the LLM and agent profile editors (#18144)

awss916c641a872d
diff --git a/__tests__/components/settings/agent-profiles/agent-profiles-local-view.test.tsx b/__tests__/components/settings/agent-profiles/agent-profiles-local-view.test.tsx
index 2806bd5da0..8f770ea60d 100644
--- a/__tests__/components/settings/agent-profiles/agent-profiles-local-view.test.tsx
+++ b/__tests__/components/settings/agent-profiles/agent-profiles-local-view.test.tsx
@@ -93,7 +93,10 @@ vi.mock("#/hooks/mutation/use-rename-agent-profile", () => ({
useRenameAgentProfile: () => ({ mutateAsync: renameMutate }),
}));
-const agentProfilesData = { profiles: [], active_agent_profile_id: null };
+let agentProfilesData: {
+ profiles: { name: string }[];
+ active_agent_profile_id: string | null;
+};
vi.mock("#/hooks/query/use-agent-profiles", () => ({
useAgentProfiles: () => ({ data: agentProfilesData }),
}));
@@ -132,6 +135,7 @@ describe("AgentProfilesLocalView save mapping", () => {
beforeEach(() => {
vi.clearAllMocks();
emitControl = null;
+ agentProfilesData = { profiles: [], active_agent_profile_id: null };
llmProfilesData = {
profiles: [{ name: "default", model: "gpt-5" }],
active_profile: "default",
@@ -607,4 +611,77 @@ describe("AgentProfilesLocalView save mapping", () => {
);
expect(saveMutate).not.toHaveBeenCalled();
});
+
+ describe("duplicate profile name", () => {
+ beforeEach(() => {
+ agentProfilesData = {
+ profiles: [{ name: "default" }, { name: "triage" }],
+ active_agent_profile_id: null,
+ };
+ emitControl = {
+ agentType: "openhands",
+ isValid: true,
+ isDirty: false,
+ buildAgentProfileFields: () => ({
+ agent_kind: "openhands",
+ mcp_server_refs: null,
+ }),
+ credentials: { isDirty: false, save: vi.fn(), reset: vi.fn() },
+ };
+ });
+
+ it("tells the user a new profile's name is taken until it is unique", async () => {
+ render(<AgentProfilesLocalView />);
+ const user = await openCreateAndName("default");
+ const input = screen.getByTestId("agent-profile-name-input");
+
+ expect(input).toHaveAttribute("aria-invalid", "true");
+ expect(input).toHaveAccessibleDescription(
+ "SETTINGS$META_PROFILE_NAME_TAKEN",
+ );
+ expect(screen.getByRole("alert")).toHaveTextContent(
+ "SETTINGS$META_PROFILE_NAME_TAKEN",
+ );
+ expect(screen.getByTestId("save-agent-profile-btn")).toBeDisabled();
+
+ await user.type(input, "-2");
+
+ expect(input).toHaveAttribute("aria-invalid", "false");
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
+ expect(screen.getByTestId("save-agent-profile-btn")).toBeEnabled();
+ });
+
+ it("flags a rename to another profile's name but not the profile's own name", async () => {
+ vi.mocked(AgentProfilesService.getProfile).mockResolvedValue({
+ name: "default",
+ profile: {
+ schema_version: 3,
+ id: "p-1",
+ name: "default",
+ revision: 1,
+ agent_kind: "openhands",
+ llm_profile_ref: "default",
+ },
+ } as never);
+
+ render(<AgentProfilesLocalView />);
+ const user = userEvent.setup();
+ await user.click(screen.getByTestId("edit-agent-profile"));
+ await screen.findByTestId("mock-agent-settings");
+ const input = screen.getByTestId("agent-profile-name-input");
+
+ expect(input).toHaveValue("default");
+ expect(input).toHaveAttribute("aria-invalid", "false");
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
+
+ await user.clear(input);
+ await user.type(input, "triage");
+
+ expect(input).toHaveAttribute("aria-invalid", "true");
+ expect(input).toHaveAccessibleDescription(
+ "SETTINGS$META_PROFILE_NAME_TAKEN",
+ );
+ expect(screen.getByTestId("save-agent-profile-btn")).toBeDisabled();
+ });
+ });
});
diff --git a/__tests__/components/settings/llm-profiles/llm-settings-local-view.test.tsx b/__tests__/components/settings/llm-profiles/llm-settings-local-view.test.tsx
index 929ac1e672..3c016832b7 100644
--- a/__tests__/components/settings/llm-profiles/llm-settings-local-view.test.tsx
+++ b/__tests__/components/settings/llm-profiles/llm-settings-local-view.test.tsx
@@ -514,6 +514,60 @@ describe("LlmSettingsLocalView", () => {
});
});
+ describe("duplicate profile name", () => {
+ it("tells the user a new profile's name is taken until it is unique", async () => {
+ const user = userEvent.setup();
+ renderWithProviders(<LlmSettingsLocalView />);
+ await user.click(screen.getByTestId("add-llm-profile"));
+ const nameInput = screen.getByTestId("profile-name-input");
+
+ await user.clear(nameInput);
+ await user.type(nameInput, "claude-profile");
+
+ expect(nameInput).toHaveAttribute("aria-invalid", "true");
+ expect(nameInput).toHaveAccessibleDescription(
+ "SETTINGS$META_PROFILE_NAME_TAKEN",
+ );
+ expect(screen.getByRole("alert")).toHaveTextContent(
+ "SETTINGS$META_PROFILE_NAME_TAKEN",
+ );
+ expect(screen.getByTestId("save-profile-btn")).toBeDisabled();
+
+ await user.type(nameInput, "-2");
+
+ expect(nameInput).toHaveAttribute("aria-invalid", "false");
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
+ expect(screen.getByTestId("save-profile-btn")).toBeEnabled();
+ });
+
+ it("flags a rename to another profile's name but not the profile's own name", async () => {
+ const user = userEvent.setup();
+ vi.mocked(ProfilesService.getProfile).mockResolvedValue({
+ name: "gpt-4-profile",
+ api_key_set: true,
+ config: { model: "openai/gpt-4" },
+ });
+
+ renderWithProviders(<LlmSettingsLocalView />);
+ await user.click(screen.getAllByTestId("profile-menu-trigger")[0]);
+ await user.click(screen.getByTestId("profile-edit"));
+ const nameInput = await screen.findByTestId("profile-name-input");
+ await waitFor(() => expect(nameInput).toHaveValue("gpt-4-profile"));
+
+ expect(nameInput).toHaveAttribute("aria-invalid", "false");
+ expect(screen.queryByRole("alert")).not.toBeInTheDocument();
+
+ await user.clear(nameInput);
+ await user.type(nameInput, "claude-profile");
+
+ expect(nameInput).toHaveAttribute("aria-invalid", "true");
+ expect(nameInput).toHaveAccessibleDescription(
+ "SETTINGS$META_PROFILE_NAME_TAKEN",
+ );
+ expect(screen.getByTestId("save-profile-btn")).toBeDisabled();
+ });
+ });
+
describe("edit mode form initialization", () => {
it("populates profile name when editing an existing profile", async () => {
const user = userEvent.setup();
diff --git a/src/components/features/settings/agent-profiles/agent-profiles-local-view.tsx b/src/components/features/settings/agent-profiles/agent-profiles-local-view.tsx
index 430d81a19f..5f0d20dc51 100644
--- a/src/components/features/settings/agent-profiles/agent-profiles-local-view.tsx
+++ b/src/components/features/settings/agent-profiles/agent-profiles-local-view.tsx
@@ -109,19 +109,16 @@ export function AgentProfilesLocalView() {
[profilesData],
);
- // The shared validator rejects whitespace, so duplicate checks compare raw.
- const isNameValid = useMemo(() => {
- if (!isProfileNameValid(profileName, { isRequired: true })) return false;
- if (viewMode === "create" && existingNames.has(profileName)) return false;
- if (
- viewMode === "edit" &&
- profileName !== editingProfile?.name &&
- existingNames.has(profileName)
- ) {
- return false;
- }
- return true;
- }, [profileName, viewMode, existingNames, editingProfile?.name]);
+ // The shared validator rejects whitespace, so the duplicate check compares raw.
+ const isNameFormatValid = isProfileNameValid(profileName, {
+ isRequired: true,
+ });
+ const isDuplicateName =
+ isNameFormatValid &&
+ existingNames.has(profileName) &&
+ (viewMode === "create" ||
+ (viewMode === "edit" && profileName !== editingProfile?.name));
+ const isNameValid = isNameFormatValid && !isDuplicateName;
const handleAddProfile = useCallback(() => {
setProfileName("");
@@ -322,6 +319,11 @@ export function AgentProfilesLocalView() {
value={profileName}
onChange={setProfileName}
isRequired
+ error={
+ isDuplicateName
+ ? t(I18nKey.SETTINGS$META_PROFILE_NAME_TAKEN)
+ : undefined
+ }
/>
{/* Reuse the existing Agent settings form to define the agent. */}
diff --git a/src/components/features/settings/llm-profiles/llm-settings-local-view.tsx b/src/components/features/settings/llm-profiles/llm-settings-local-view.tsx
index b5453bd513..521d43734c 100644
--- a/src/components/features/settings/llm-profiles/llm-settings-local-view.tsx
+++ b/src/components/features/settings/llm-profiles/llm-settings-local-view.tsx
@@ -156,21 +156,17 @@ export function LlmSettingsLocalView() {
);
// Validate profile name. The shared validator rejects any whitespace, so
- // duplicate checks below compare the raw value directly.
- const isNameValid = useMemo(() => {
- if (!isProfileNameValid(profileName, { isRequired: true })) return false;
- // In create mode, check for duplicates
- if (viewMode === "create" && existingNames.has(profileName)) return false;
- // In edit mode, name can match current profile name
- if (
- viewMode === "edit" &&
- profileName !== editingProfile?.profile.name &&
- existingNames.has(profileName)
- ) {
- return false;
- }
- return true;
- }, [profileName, viewMode, existingNames, editingProfile?.profile.name]);
+ // the duplicate check below compares the raw value directly.
+ const isNameFormatValid = isProfileNameValid(profileName, {
+ isRequired: true,
+ });
+ // In edit mode, name can match current profile name
+ const isDuplicateName =
+ isNameFormatValid &&
+ existingNames.has(profileName) &&
+ (viewMode === "create" ||
+ (viewMode === "edit" && profileName !== editingProfile?.profile.name));
+ const isNameValid = isNameFormatValid && !isDuplicateName;
const handleProfileNameChange = useCallback((value: string) => {
setProfileName(value);
@@ -509,6 +505,11 @@ export function LlmSettingsLocalView() {
value={profileName}
onChange={handleProfileNameChange}
isRequired
+ error={
+ isDuplicateName
+ ? t(I18nKey.SETTINGS$META_PROFILE_NAME_TAKEN)
+ : undefined
+ }
/>
{/* Profile form - key ensures form remounts when switching profiles.
diff --git a/src/components/features/settings/llm-profiles/profile-name-input.tsx b/src/components/features/settings/llm-profiles/profile-name-input.tsx
index 7f45e8d0d4..44d0ce5f67 100644
--- a/src/components/features/settings/llm-profiles/profile-name-input.tsx
+++ b/src/components/features/settings/llm-profiles/profile-name-input.tsx
@@ -17,6 +17,11 @@ interface ProfileNameInputProps {
isOptional?: boolean;
/** When true, empty values will show red validation styling (required field behavior). */
isRequired?: boolean;
+ /**
+ * A caller-side validation message, such as a name that is already taken.
+ * Marks the input invalid and becomes its accessible description.
+ */
+ error?: string;
}
export const ProfileNameInput = forwardRef<
@@ -33,6 +38,7 @@ export const ProfileNameInput = forwardRef<
isDisabled,
isOptional,
isRequired = false,
+ error,
},
ref,
) {
@@ -63,6 +69,7 @@ export const ProfileNameInput = forwardRef<
isDisabled={isDisabled}
ariaDescribedBy={describedById}
ariaInvalid={!isValid}
+ error={error}
/>
<p
id={describedById}