{"record":{"id":"15c8d6ca0850fe8d","repo":"nikivdev/code","slug":"agent-already-exists-at","errorCode":null,"errorMessage":"Agent '{}' already exists at {}","messagePattern":"Agent '(.+?)' already exists at (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/hive.rs","lineNumber":375,"sourceCode":"\n    Ok(())\n}\n\n/// Create a new agent spec file\npub fn create_agent(name: &str, global: bool) -> Result<PathBuf> {\n    let path = if global {\n        let home = dirs::home_dir().context(\"Could not find home directory\")?;\n        let dir = home.join(\".hive/agents\").join(name);\n        fs::create_dir_all(&dir)?;\n        dir.join(\"spec.md\")\n    } else {\n        let dir = PathBuf::from(\".flow/agents\");\n        fs::create_dir_all(&dir)?;\n        dir.join(format!(\"{}.md\", name))\n    };\n\n    if path.exists() {\n        anyhow::bail!(\"Agent '{}' already exists at {}\", name, path.display());\n    }\n\n    let template = format!(\n        r#\"# Agent: {}\n# Purpose: <describe what this agent does>\n#\n# Rules:\n# - <rule 1>\n# - <rule 2>\n#\n# Tools:\n# - bash\n\"#,\n        name\n    );\n\n    fs::write(&path, template)?;\n    Ok(path)","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/hive.rs#L357-L393","documentation":"`create_agent` scaffolds a new agent markdown file under `.flow/agents/<name>.md` (or a caller-supplied path). To avoid silently clobbering user-authored agents, it bails with this error if the target file already exists, echoing the agent name and full path.","triggerScenarios":"Running `create_agent` with a name whose `<name>.md` file already exists in `.flow/agents/` (or at the resolved path); re-running a scaffold script that already ran once.","commonSituations":"Re-running an init/scaffold command after a previous successful creation; name collision with an agent created by a teammate or checked into the repo; expecting the command to overwrite/regenerate an existing agent template.","solutions":["Pick a different, unused agent name","Delete or rename the existing file first: `mv .flow/agents/<name>.md .flow/agents/<name>.bak`","If intentional, remove the existing file before recreating: `rm .flow/agents/<name>.md`","Edit the existing agent file in place instead of re-scaffolding"],"exampleFix":"// before\ncreate_agent(\"reviewer\")?; // .flow/agents/reviewer.md exists\n// after\nlet path = PathBuf::from(\".flow/agents/reviewer.md\");\nif !path.exists() {\n    create_agent(\"reviewer\")?;\n} else {\n    eprintln!(\"reusing existing agent at {}\", path.display());\n}","handlingStrategy":"validation","validationCode":"let path = PathBuf::from(format!(\".flow/agents/{}.md\", name));\nif path.exists() {\n    eprintln!(\"agent '{}' already exists at {}\", name, path.display());\n    std::process::exit(1);\n}","typeGuard":"fn agent_file_exists(name: &str) -> bool {\n    PathBuf::from(format!(\".flow/agents/{}.md\", name)).exists()\n}","tryCatchPattern":"match create_agent(name) {\n    Err(e) if e.to_string().contains(\"already exists\") => {\n        eprintln!(\"{e}\");\n        eprintln!(\"choose a new name, or edit the existing file\");\n    }\n    r => r?,\n}","preventionTips":["Check for the target file before scaffolding; prompt to overwrite if intended","Use unique, descriptive agent names (prefix by team/feature) to avoid collisions","Don't commit agent re-scaffolding scripts that run unconditionally","Keep `.flow/agents/` reviewed in version control so collisions are visible in PRs"],"tags":["file-conflict","scaffolding","agents","rust"],"backgroundTag":"file-already-exists","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}