{"record":{"id":"b7efcd35bf0786bd","repo":"mastra-ai/mastra","slug":"version-number-input-versionnumber-already-exis-b7efcd","errorCode":null,"errorMessage":"Version number ${input.versionNumber} already exists for agent ${input.agentId}","messagePattern":"Version number (.+?) already exists for agent (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/agents/source.ts","lineNumber":255,"sourceCode":"    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    }\n    return this.memory.getVersion(id);\n  }\n\n  async getVersionByNumber(agentId: string, versionNumber: number): Promise<AgentVersion | null> {","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/agents/source.ts#L237-L273","documentation":"createVersion() also enforces per-agent version-number uniqueness: before inserting, it queries getVersionByNumber(agentId, versionNumber) and throws if that number is already taken for the agent. Version numbers must be strictly unique within an agent's history.","triggerScenarios":"Calling createVersion with input.versionNumber equal to a version number already recorded for input.agentId (version-id is unique but number collides).","commonSituations":"Manually assigning version numbers (e.g. always 1) instead of computing next-number, concurrent publishes of two versions with the same number, replaying old version payloads after a rollback.","solutions":["Compute the next version number from the agent's existing history before calling","Use listVersions/getVersionByNumber to check availability first","Serialize concurrent version creation (lock or single writer) per agent","Let the storage/source layer auto-assign numbers rather than supplying them"],"exampleFix":"// before\nawait storage.agents.createVersion({ id, agentId, versionNumber: 1, ... })\n// after\nconst versions = await storage.agents.listVersions({ agentId });\nconst next = versions.reduce((m, v) => Math.max(m, v.versionNumber), 0) + 1;\nawait storage.agents.createVersion({ id, agentId, versionNumber: next, ... });","handlingStrategy":"validation","validationCode":"const dup = await memory.getVersionByNumber(agentId, versionNumber);\nif (dup) throw new Error(`versionNumber ${versionNumber} already used`);","typeGuard":"null","tryCatchPattern":"try {\n  await storage.agents.createVersion(input);\n} catch (e) {\n  if (e.message.includes('already exists for agent')) {\n    input.versionNumber = await nextVersionNumber(input.agentId);\n    await storage.agents.createVersion(input);\n    return;\n  }\n  throw e;\n}","preventionTips":["Always compute next number from existing history, never hardcode","Serialize version creation per agent (single writer/lock)","Use storage-assigned or monotonic numbering","Test concurrent publish paths"],"tags":["storage","agents","versioning","conflict"],"backgroundTag":"duplicate-resource-id","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}