can1357/oh-my-pi · error
Agent file already exists: ${shortenPath(filePath)}
Error message
Agent file already exists: ${shortenPath(filePath)} What it means
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.
Source
Thrown at packages/coding-agent/src/modes/components/agents-hub.ts:821
}
}
async #saveGeneratedAgent(): Promise<void> {
const spec = this.#createSpec;
if (!spec) return;
const dirs = getConfigDirs("agents", {
user: this.#createScope === "user",
project: this.#createScope === "project",
cwd: this.#cwd,
});
const targetDir = dirs[0]?.path;
if (!targetDir) {
throw new Error(`Cannot resolve ${this.#createScope} agents directory.`);
}
const filePath = path.join(targetDir, `${spec.identifier}.md`);
try {
await fs.stat(filePath);
throw new Error(`Agent file already exists: ${shortenPath(filePath)}`);
} catch (error) {
if (!isEnoent(error)) throw error;
}
const frontmatter = YAML.stringify({ name: spec.identifier, description: spec.whenToUse }, null, 2).trimEnd();
const content = `---\n${frontmatter}\n---\n\n${spec.systemPrompt.trim()}\n`;
await Bun.write(filePath, content);
await refreshAgentDiscovery(this.#cwd, this.#extensionRoots());
this.#clearCreateFlow();
this.#notice = `Created agent ${spec.identifier} at ${shortenPath(filePath)}`;
await this.#reload();
}
// ═══════════════════════════════════════════════════════════════════════
// Input
// ═══════════════════════════════════════════════════════════════════════
handleInput(data: string): void {
if (data.startsWith("\x1b[<")) {View on GitHub (pinned to 9690622007)
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
Example fix
// before filePath: ~/.omp/agents/refactor-helper.md // exists // after rename existing to refactor-helper-old.md or use identifier 'refactor-helper-v2'
Defensive patterns
Strategy: validation
Validate before calling
const filePath = path.join(targetDir, `${identifier}.md`);
if (await Bun.file(filePath).exists()) throw new Error(`Agent exists: ${filePath}`); Try / catch
try {
await hub.saveGeneratedAgent(spec);
} catch (err) {
if (err instanceof Error && err.message.startsWith("Agent file already exists")) {
// pick a new identifier or remove the old file, then retry
} else throw err;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Managed skill "${name}" already exists. Use action "update"
- Cannot resolve ${this.#createScope} agents directory.
- unknown filetype: {ft_debug}
- Is a directory
- Too many levels of symbolic links
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/ee433be52e0bf207.
Report an issue: GitHub.