{"record":{"id":"ee433be52e0bf207","repo":"can1357/oh-my-pi","slug":"agent-file-already-exists-shortenpath-filepath","errorCode":null,"errorMessage":"Agent file already exists: ${shortenPath(filePath)}","messagePattern":"Agent file already exists: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/modes/components/agents-hub.ts","lineNumber":821,"sourceCode":"\t\t}\n\t}\n\n\tasync #saveGeneratedAgent(): Promise<void> {\n\t\tconst spec = this.#createSpec;\n\t\tif (!spec) return;\n\t\tconst dirs = getConfigDirs(\"agents\", {\n\t\t\tuser: this.#createScope === \"user\",\n\t\t\tproject: this.#createScope === \"project\",\n\t\t\tcwd: this.#cwd,\n\t\t});\n\t\tconst targetDir = dirs[0]?.path;\n\t\tif (!targetDir) {\n\t\t\tthrow new Error(`Cannot resolve ${this.#createScope} agents directory.`);\n\t\t}\n\t\tconst filePath = path.join(targetDir, `${spec.identifier}.md`);\n\t\ttry {\n\t\t\tawait fs.stat(filePath);\n\t\t\tthrow new Error(`Agent file already exists: ${shortenPath(filePath)}`);\n\t\t} catch (error) {\n\t\t\tif (!isEnoent(error)) throw error;\n\t\t}\n\t\tconst frontmatter = YAML.stringify({ name: spec.identifier, description: spec.whenToUse }, null, 2).trimEnd();\n\t\tconst content = `---\\n${frontmatter}\\n---\\n\\n${spec.systemPrompt.trim()}\\n`;\n\t\tawait Bun.write(filePath, content);\n\t\tawait refreshAgentDiscovery(this.#cwd, this.#extensionRoots());\n\t\tthis.#clearCreateFlow();\n\t\tthis.#notice = `Created agent ${spec.identifier} at ${shortenPath(filePath)}`;\n\t\tawait this.#reload();\n\t}\n\n\t// ═══════════════════════════════════════════════════════════════════════\n\t// Input\n\t// ═══════════════════════════════════════════════════════════════════════\n\n\thandleInput(data: string): void {\n\t\tif (data.startsWith(\"\\x1b[<\")) {","sourceCodeStart":803,"sourceCodeEnd":839,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/modes/components/agents-hub.ts#L803-L839","documentation":"Before writing the generated agent file, the hub stats <targetDir>/<identifier>.md and throws if it already exists, preventing silent overwrite of an existing agent definition. The path is shown shortened (home replaced with ~) in the message.","triggerScenarios":"Creating an agent whose identifier (lowercase kebab-case name) collides with an existing .md file in the resolved agents directory — either a previously generated agent or a hand-written one.","commonSituations":"Re-running creation for a similar description producing the same identifier; an agent with that name already defined manually; user-vs-project scope both containing same-named agents and the resolved dir already has the file.","solutions":["Delete or rename the existing agent .md file at the reported path","Re-run creation with a description that yields a different identifier","Edit the existing agent instead of creating a new one","Choose the other scope (user vs project) where the name is free"],"exampleFix":"// before\nfilePath: ~/.omp/agents/refactor-helper.md  // exists\n// after\nrename existing to refactor-helper-old.md or use identifier 'refactor-helper-v2'","handlingStrategy":"validation","validationCode":"const filePath = path.join(targetDir, `${identifier}.md`);\nif (await Bun.file(filePath).exists()) throw new Error(`Agent exists: ${filePath}`);","typeGuard":null,"tryCatchPattern":"try {\n  await hub.saveGeneratedAgent(spec);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith(\"Agent file already exists\")) {\n    // pick a new identifier or remove the old file, then retry\n  } else throw err;\n}","preventionTips":["List existing agents before creating one with a similar description","Adopt a naming convention to reduce identifier collisions","Remove or archive obsolete agent files","Check the reported shortened path and edit the existing agent when appropriate"],"tags":["filesystem","conflict","agents"],"backgroundTag":"file-already-exists","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}