{"record":{"id":"3565bddcfb6f9898","repo":"ruvnet/ruflo","slug":"agent-with-name-input-name-already-exists","errorCode":null,"errorMessage":"Agent with name '${input.name}' already exists","messagePattern":"Agent with name '(.+?)' already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/swarm/src/application/commands/spawn-agent.command.ts","lineNumber":46,"sourceCode":" */\nexport interface SpawnAgentResult {\n  success: boolean;\n  agentId: string;\n  agent: Agent;\n  startedAutomatically: boolean;\n}\n\n/**\n * Spawn Agent Command Handler\n */\nexport class SpawnAgentCommandHandler {\n  constructor(private readonly repository: IAgentRepository) {}\n\n  async execute(input: SpawnAgentInput): Promise<SpawnAgentResult> {\n    // Check if agent with same name exists\n    const existing = await this.repository.findByName(input.name);\n    if (existing) {\n      throw new Error(`Agent with name '${input.name}' already exists`);\n    }\n\n    // Create agent\n    const agent = Agent.create({\n      name: input.name,\n      role: input.role,\n      domain: input.domain,\n      capabilities: input.capabilities,\n      parentId: input.parentId,\n      metadata: input.metadata,\n      maxConcurrentTasks: input.maxConcurrentTasks,\n    });\n\n    // Auto-start if requested\n    let startedAutomatically = false;\n    if (input.autoStart) {\n      agent.start();\n      startedAutomatically = true;","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/swarm/src/application/commands/spawn-agent.command.ts#L28-L64","documentation":"SpawnAgentCommandHandler enforces name uniqueness: repository.findByName(input.name) must return nothing before Agent.create() runs. An existing agent with the same name aborts the spawn — agent names act as a natural key in the repository.","triggerScenarios":"Re-running a spawn script without terminating the previous agents; hardcoded names like 'worker-1' colliding across runs or teams; an agent from a previous session still registered because the repository was not cleared; concurrent spawns racing on the same name.","commonSituations":"Bootstrap/provision scripts executed twice; an autoscaler restarting agents while old ones remain registered; dev and test sharing a persistent agent repository.","solutions":["Generate unique names per spawn: append a uuid or counter (worker-${crypto.randomUUID().slice(0, 8)}).","Check findByName first and reuse the existing agent instead of spawning a duplicate.","Terminate the old agent before re-spawning the same name.","Reset the agent repository between dev/test runs."],"exampleFix":"// before\nawait spawnAgent.execute({ name: 'worker-1', role: 'coder' }); // second run throws\n\n// after — unique name per spawn, or reuse the existing agent\nconst existing = await agentRepository.findByName('worker-1');\nif (existing) {\n  return existing;\n}\nawait spawnAgent.execute({ name: `worker-${crypto.randomUUID().slice(0, 8)}`, role: 'coder' });","handlingStrategy":"validation","validationCode":"const existing = await agentRepository.findByName(input.name);\nif (existing) {\n  return existing; // reuse instead of spawn\n}\nawait spawnAgentHandler.execute(input);","typeGuard":null,"tryCatchPattern":"try {\n  await spawnAgentHandler.execute(input);\n} catch (e) {\n  if (e instanceof Error && /already exists$/.test(e.message)) {\n    const dup = await agentRepository.findByName(input.name);\n    // reuse dup, or retry with a suffixed unique name\n  } else {\n    throw e;\n  }\n}","preventionTips":["Never hardcode agent names in repeatable scripts — append a uuid or counter.","Treat a name collision as a reuse signal, not just an error.","Terminate or clear previous agents before re-running bootstrap scripts."],"tags":["agent","duplicate","unique-name","spawn"],"backgroundTag":"unique-constraint-violation","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}