{"record":{"id":"74b68da1952a08ca","repo":"mastra-ai/mastra","slug":"version-with-id-input-id-already-exists","errorCode":null,"errorMessage":"Version with id ${input.id} already exists","messagePattern":"Version with id (.+?) already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/agents/inmemory.ts","lineNumber":224,"sourceCode":"    const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n\n    return {\n      agents: clonedAgents.slice(offset, offset + perPage),\n      total: clonedAgents.length,\n      page,\n      perPage: perPageForResponse,\n      hasMore: offset + perPage < clonedAgents.length,\n    };\n  }\n\n  // ==========================================================================\n  // Agent Version Methods\n  // ==========================================================================\n\n  async createVersion(input: CreateVersionInput): Promise<AgentVersion> {\n    // Check if version with this ID already exists (versions are immutable)\n    if (this.db.agentVersions.has(input.id)) {\n      throw new Error(`Version with id ${input.id} already exists`);\n    }\n\n    // Check for duplicate (agentId, versionNumber) pair\n    for (const version of this.db.agentVersions.values()) {\n      if (version.agentId === input.agentId && version.versionNumber === input.versionNumber) {\n        throw new Error(`Version number ${input.versionNumber} already exists for agent ${input.agentId}`);\n      }\n    }\n\n    const version: AgentVersion = {\n      ...input,\n      createdAt: new Date(),\n    };\n\n    // Deep clone before storing to prevent external mutation\n    this.db.agentVersions.set(input.id, this.deepCopyVersion(version));\n    return this.deepCopyVersion(version);\n  }","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/agents/inmemory.ts#L206-L242","documentation":"Agent versions are immutable and identified by unique IDs; `createVersion` throws if `this.db.agentVersions` already contains `input.id`. Immutability means a conflicting ID can never be overwritten — a new version row with a new ID is required.","triggerScenarios":"Calling `createVersion` with an `id` that already exists in the versions store — e.g. deterministic IDs derived from (agentId, versionNumber) that were already created, or replayed/duplicated requests.","commonSituations":"Retry logic re-submitting the same version creation; seed scripts run twice; deriving version IDs from content or number without checking existence first.","solutions":["Check `getVersion(id)` (or the map) before creating; skip or fetch the existing version","Generate unique version IDs (e.g. crypto.randomUUID()) rather than deterministic reused ones","Treat the throw as idempotent success in retry paths"],"exampleFix":"// before\nawait agents.createVersion({ id: 'v1', agentId: 'a1', versionNumber: 1, ... });\n// after\nconst id = crypto.randomUUID();\nawait agents.createVersion({ id, agentId: 'a1', versionNumber: 1, ... });","handlingStrategy":"try-catch","validationCode":"const existing = await agentsDomain.getVersion({ versionId: version.id });\nif (existing) {\n  return existing; // already created\n}","typeGuard":"null","tryCatchPattern":"try {\n  return await agentsDomain.createVersion(version);\n} catch (e) {\n  if (e instanceof Error && /Version with id .* already exists/.test(e.message)) {\n    return agentsDomain.getVersion({ versionId: version.id });\n  }\n  throw e;\n}","preventionTips":["Use crypto.randomUUID() for version IDs, not deterministic reused ones","Treat duplicate errors as idempotent success in retries","Make seed scripts run-once or check existence first","Never attempt to mutate an existing version — versions are immutable"],"tags":["storage","duplicate","versioning"],"backgroundTag":"duplicate-resource-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}