{"record":{"id":"25bbfaa98901891f","repo":"mastra-ai/mastra","slug":"agent-with-id-agent-id-already-exists","errorCode":null,"errorMessage":"Agent with id ${agent.id} already exists","messagePattern":"Agent with id (.+?) already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/agents/inmemory.ts","lineNumber":49,"sourceCode":"  async dangerouslyClearAll(): Promise<void> {\n    this.db.agents.clear();\n    this.db.agentVersions.clear();\n  }\n\n  // ==========================================================================\n  // Agent CRUD Methods\n  // ==========================================================================\n\n  async getById(id: string): Promise<StorageAgentType | null> {\n    const agent = this.db.agents.get(id);\n    return agent ? this.deepCopyAgent(agent) : null;\n  }\n\n  async create(input: { agent: StorageCreateAgentInput }): Promise<StorageAgentType> {\n    const { agent } = input;\n\n    if (this.db.agents.has(agent.id)) {\n      throw new Error(`Agent with id ${agent.id} already exists`);\n    }\n\n    const now = new Date();\n    // Default visibility to 'private' when an authorId is set; leave undefined for legacy unowned rows.\n    const visibility = agent.visibility ?? (agent.authorId ? 'private' : undefined);\n    const newAgent: StorageAgentType = {\n      id: agent.id,\n      status: 'draft',\n      activeVersionId: undefined,\n      authorId: agent.authorId,\n      visibility,\n      metadata: agent.metadata,\n      favoriteCount: 0,\n      createdAt: now,\n      updatedAt: now,\n    };\n\n    this.db.agents.set(agent.id, newAgent);","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/agents/inmemory.ts#L31-L67","documentation":"The in-memory agents domain enforces unique agent IDs: `create` throws if `this.db.agents` already contains an entry with the given `agent.id`. Agent creation is create-only; use `update` to modify existing agents.","triggerScenarios":"Calling `create` (directly or via `seedAgent`) with a `{ agent: { id } }` whose `id` already exists in the store — e.g. re-running a seed script or generating agents in a loop with a constant ID.","commonSituations":"Re-running seeding/bootstrapping scripts against the same in-memory instance; ID generation collisions (hardcoded or non-unique IDs); retrying a create after a timeout when the first attempt actually succeeded.","solutions":["Check existence first (e.g. `getAgent(id)` or `db.agents.has(id)`) and skip or update instead of creating","Generate unique IDs (crypto.randomUUID()) instead of hardcoded ones","Catch this error and treat it as idempotent already-created during seeding"],"exampleFix":"// before\nawait agents.create({ agent: { id: 'agent-1', ... } });\n// after\nconst existing = await agents.get({ agentId: 'agent-1' });\nif (!existing) await agents.create({ agent: { id: 'agent-1', ... } });","handlingStrategy":"try-catch","validationCode":"const existing = await agentsDomain.get({ agentId: agent.id });\nif (existing) {\n  // skip create or route to update instead\n}","typeGuard":"null","tryCatchPattern":"try {\n  await agentsDomain.create({ agent });\n} catch (e) {\n  if (e instanceof Error && /Agent with id .* already exists/.test(e.message)) {\n    return agentsDomain.get({ agentId: agent.id }); // idempotent path\n  }\n  throw e;\n}","preventionTips":["Generate unique IDs with crypto.randomUUID() instead of hardcoding","Make seed scripts idempotent: check-then-create or catch duplicates","Never reuse agent IDs across runs against the same store","Route edits through update(), not repeated create()"],"tags":["storage","duplicate","in-memory"],"backgroundTag":"duplicate-resource-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}