{"record":{"id":"a4a35c166016e6d1","repo":"zed-industries/zed","slug":"profile-with-id-profile-id-already-exists","errorCode":null,"errorMessage":"profile with ID '{profile_id}' already exists","messagePattern":"profile with ID '(.+?)' already exists","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/agent_settings/src/agent_profile.rs","lineNumber":163,"sourceCode":"    pub fn is_context_server_tool_enabled(&self, server_id: &str, tool_name: &str) -> bool {\n        self.context_servers\n            .get(server_id)\n            .and_then(|preset| preset.tools.get(tool_name).copied())\n            .unwrap_or(self.enable_all_context_servers)\n    }\n\n    pub fn save_to_settings(\n        &self,\n        profile_id: AgentProfileId,\n        content: &mut SettingsContent,\n    ) -> Result<()> {\n        let profiles = content\n            .agent\n            .get_or_insert_default()\n            .profiles\n            .get_or_insert_default();\n        if profiles.contains_key(&profile_id.0) {\n            bail!(\"profile with ID '{profile_id}' already exists\");\n        }\n\n        profiles.insert(\n            profile_id.0,\n            AgentProfileContent {\n                name: self.name.clone().into(),\n                tools: self.tools.clone(),\n                enable_all_context_servers: Some(self.enable_all_context_servers),\n                context_servers: self\n                    .context_servers\n                    .clone()\n                    .into_iter()\n                    .map(|(server_id, preset)| {\n                        (\n                            server_id,\n                            ContextServerPresetContent {\n                                tools: preset.tools,\n                            },","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/agent_settings/src/agent_profile.rs#L145-L181","documentation":"`AgentProfile::save_to_settings` inserts a new profile into `SettingsContent.agent.profiles` and first checks `profiles.contains_key(&profile_id.0)`. Creating a profile with an ID that already exists bails with this message rather than overwriting, so the settings file never loses data implicitly.","triggerScenarios":"Calling profile-creation flow (e.g. the agent configuration UI's save) twice with the same `AgentProfileId`, or programmatically calling `save_to_settings` with an ID already present in `settings.json`'s `agent.profiles` map.","commonSituations":"Double-submitting the profile form; retrying a save after an unclear error; two team members syncing a settings file that already contains the same profile ID.","solutions":["Use the update/edit path instead of create when modifying an existing profile.","Delete or rename the existing profile with that ID in `settings.json` before re-creating it.","Generate a fresh unique ID for the new profile instead of reusing the colliding one.","Pre-check `content.agent.profiles.contains_key(&id)` before calling `save_to_settings` and branch to update vs create."],"exampleFix":"// before — unconditional create\ncurrent_profile.save_to_settings(profile_id.clone(), &mut content)?;\n\n// after — create only when absent, else update\nif content.agent.as_ref()\n    .and_then(|agent| agent.profiles.as_ref())\n    .map_or(false, |profiles| profiles.contains_key(&profile_id.0))\n{\n    current_profile.update_in_settings(profile_id, &mut content)?; // existing entry\n} else {\n    current_profile.save_to_settings(profile_id, &mut content)?;    // new entry\n}","handlingStrategy":"validation","validationCode":"let exists = content.agent.as_ref()\n    .and_then(|agent| agent.profiles.as_ref())\n    .map_or(false, |profiles| profiles.contains_key(&profile_id.0));\nif exists {\n    // take the update path instead of save_to_settings\n}","typeGuard":"fn profile_id_is_free(content: &SettingsContent, id: &AgentProfileId) -> bool {\n    content.agent.as_ref()\n        .and_then(|agent| agent.profiles.as_ref())\n        .map_or(true, |profiles| !profiles.contains_key(&id.0))\n}","tryCatchPattern":"match profile.save_to_settings(id.clone(), &mut content) {\n    Err(e) if e.to_string().contains(\"already exists\") => {\n        // load and update the existing profile instead of duplicating\n    }\n    rest => rest,\n}","preventionTips":["Branch create-vs-update on `contains_key` before saving.","Derive profile IDs from stable unique names to avoid collisions.","Disable double-submit on profile forms in the UI."],"tags":["rust","zed","agent","settings","profile","duplicate-key"],"backgroundTag":"duplicate-profile-id","analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}