{"record":{"id":"b217184db8d5ed4f","repo":"mastra-ai/mastra","slug":"version-with-id-input-id-already-exists-b21718","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/source.ts","lineNumber":251,"sourceCode":"      if (this.providerVersions.get(versionId)?.agentId === id) {\n        this.providerVersions.delete(versionId);\n      }\n    }\n    await this.memory.delete(id);\n  }\n\n  async list(args?: StorageListAgentsInput): Promise<StorageListAgentsOutput> {\n    this.refreshKnownAgentIds();\n    await this.discoverProviderAgentIds();\n    await Promise.all([...this.knownAgentIds].map(agentId => this.hydrateAgent(agentId)));\n    return this.memory.list(args);\n  }\n\n  async createVersion(input: CreateVersionInput): Promise<AgentVersion> {\n    await this.hydrateAgent(input.agentId);\n    const existingVersion = await this.memory.getVersion(input.id);\n    if (existingVersion) {\n      throw new Error(`Version with id ${input.id} already exists`);\n    }\n    const existingVersionNumber = await this.memory.getVersionByNumber(input.agentId, input.versionNumber);\n    if (existingVersionNumber) {\n      throw new Error(`Version number ${input.versionNumber} already exists for agent ${input.agentId}`);\n    }\n\n    const snapshot = snapshotFromVersion({ ...input, createdAt: new Date() } as AgentVersion);\n    const result = await this.persistSnapshot(input.agentId, snapshot, input.changeMessage);\n    const version = await this.memory.createVersion(input);\n    this.rememberProviderVersion(input.agentId, version, result);\n    return version;\n  }\n\n  async getVersion(id: string): Promise<AgentVersion | null> {\n    const providerVersion = this.providerVersions.get(id);\n    if (providerVersion) {\n      return structuredClone(providerVersion);\n    }","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/agents/source.ts#L233-L269","documentation":"createVersion() checks the memory store for an existing version with the same version id before inserting. If a version record with that id already exists, it throws to preserve version-record uniqueness. This prevents duplicate/idempotent-replay writes from corrupting version history.","triggerScenarios":"Calling storage.agents.version()/createVersion() with input.id matching an already-stored AgentVersion id (after hydrating the agent).","commonSituations":"Retrying a createVersion call after a network/timeout where the first write succeeded, generating version ids non-uniquely (e.g. from timestamps with low resolution), replaying an event or migration twice.","solutions":["Generate a genuinely unique version id (UUID) for each version","Fetch the existing version and reuse it instead of creating a new one","Make the call idempotent: catch this error and treat the version as already recorded","Fix retry logic to not blindly replay writes with the same id"],"exampleFix":"// before\nawait storage.agents.createVersion({ id: fixedId, agentId, versionNumber: 2, ... })\n// after\nconst id = crypto.randomUUID();\nawait storage.agents.createVersion({ id, agentId, versionNumber: 2, ... });","handlingStrategy":"try-catch","validationCode":"const existing = await memory.getVersion(input.id);\nif (existing) return existing; // already recorded","typeGuard":"null","tryCatchPattern":"try {\n  await storage.agents.createVersion(input);\n} catch (e) {\n  if (e.message === `Version with id ${input.id} already exists`) return; // idempotent replay\n  throw e;\n}","preventionTips":["Generate version ids with crypto.randomUUID()","Treat createVersion as idempotent in retry logic","Never derive ids from low-resolution timestamps","Log version ids on first write to correlate retries"],"tags":["storage","agents","versioning","duplicate-key"],"backgroundTag":"duplicate-resource-id","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}