{"record":{"id":"54ce5f8a02374d85","repo":"mastra-ai/mastra","slug":"version-number-input-versionnumber-already-exis-54ce5f","errorCode":null,"errorMessage":"Version number ${input.versionNumber} already exists for workspace ${input.workspaceId}","messagePattern":"Version number (.+?) already exists for workspace (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/workspaces/inmemory.ts","lineNumber":258,"sourceCode":"      perPage: perPageForResponse,\n      hasMore: offset + perPage < clonedConfigs.length,\n    };\n  }\n\n  // ==========================================================================\n  // Workspace Version Methods\n  // ==========================================================================\n\n  async createVersion(input: CreateWorkspaceVersionInput): Promise<WorkspaceVersion> {\n    // Check if version with this ID already exists\n    if (this.db.workspaceVersions.has(input.id)) {\n      throw new Error(`Version with id ${input.id} already exists`);\n    }\n\n    // Check for duplicate (workspaceId, versionNumber) pair\n    for (const version of this.db.workspaceVersions.values()) {\n      if (version.workspaceId === input.workspaceId && version.versionNumber === input.versionNumber) {\n        throw new Error(`Version number ${input.versionNumber} already exists for workspace ${input.workspaceId}`);\n      }\n    }\n\n    const version: WorkspaceVersion = {\n      ...input,\n      createdAt: new Date(),\n    };\n\n    // Deep clone before storing\n    this.db.workspaceVersions.set(input.id, this.deepCopyVersion(version));\n    return this.deepCopyVersion(version);\n  }\n\n  async getVersion(id: string): Promise<WorkspaceVersion | null> {\n    const version = this.db.workspaceVersions.get(id);\n    return version ? this.deepCopyVersion(version) : null;\n  }\n","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/workspaces/inmemory.ts#L240-L276","documentation":"createVersion() enforces a composite uniqueness rule: (workspaceId, versionNumber) pairs must be unique. Reusing a version number within the same workspace would make version ordering and diffing ambiguous, so it throws.","triggerScenarios":"Inserting two versions with versionNumber: 1 for the same workspace (e.g. fixture reuse); manually specifying versionNumber instead of computing nextVersion = latest.versionNumber + 1; concurrent createVersion calls racing on the same next number (no lock in the in-memory path).","commonSituations":"Replaying version history from an export; test suites sharing one storage instance across cases that each seed 'v1'; code that hardcodes versionNumber: 1 for 'initial version' multiple times.","solutions":["Fetch the latest version and use versionNumber: (latest?.versionNumber ?? 0) + 1","Catch this error to detect a concurrently-created version, then re-read latest and retry with the incremented number","Deduplicate imported version data on (workspaceId, versionNumber) before insertion","Give each test fixture its own storage instance or unique workspace ids"],"exampleFix":"// before\nawait storage.workspaces.createVersion({ id: crypto.randomUUID(), workspaceId, versionNumber: 1, config });\n// after\nconst latest = await storage.workspaces.getLatestVersion(workspaceId);\nawait storage.workspaces.createVersion({\n  id: crypto.randomUUID(),\n  workspaceId,\n  versionNumber: (latest?.versionNumber ?? 0) + 1,\n  config,\n});","handlingStrategy":"try-catch","validationCode":"const latest = await storage.workspaces.getLatestVersion(workspaceId);\nconst next = (latest?.versionNumber ?? 0) + 1;\nconst clash = (await storage.workspaces.listVersions(workspaceId)).versions.some(v => v.versionNumber === next);\nif (clash) throw new Error(`versionNumber ${next} already taken for workspace ${workspaceId}`);","typeGuard":null,"tryCatchPattern":"try {\n  await storage.workspaces.createVersion(input);\n} catch (err) {\n  if (err instanceof Error && /already exists for workspace/.test(err.message)) {\n    const latest = await storage.workspaces.getLatestVersion(input.workspaceId);\n    return storage.workspaces.createVersion({ ...input, versionNumber: (latest?.versionNumber ?? 0) + 1 });\n  }\n  throw err;\n}","preventionTips":["Derive versionNumber from getLatestVersion() rather than hardcoding 1","Serialize version creation per workspace if concurrent writers are possible","Give tests isolated storage instances or unique workspace ids","Deduplicate imported history on (workspaceId, versionNumber)"],"tags":["storage","workspace","duplicate-key","versioning"],"backgroundTag":"duplicate-key-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}