{"record":{"id":"d3f5f7c5b78b240c","repo":"mastra-ai/mastra","slug":"failed-to-retrieve-created-version","errorCode":null,"errorMessage":"Failed to retrieve created version","messagePattern":"Failed to retrieve created version","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"packages/server/src/server/handlers/agent-versions.ts","lineNumber":191,"sourceCode":"        : null;\n\n      const changedFields = calculateChangedFields(previousConfig, currentConfig);\n\n      // Create the new version with retry logic to handle race conditions\n      // Config fields are passed top-level\n      const { versionId } = await createVersionWithRetry(\n        agentsStore as unknown as VersionedStoreInterface,\n        agentId,\n        'agentId',\n        currentConfig,\n        changedFields,\n        { changeMessage },\n      );\n\n      // Get the created version to return\n      const version = await agentsStore.getVersion(versionId);\n      if (!version) {\n        throw new HTTPException(500, { message: 'Failed to retrieve created version' });\n      }\n\n      // Enforce retention limit - delete oldest versions if we exceed the max\n      await enforceRetentionLimit(\n        agentsStore as unknown as VersionedStoreInterface,\n        agentId,\n        'agentId',\n        agent.activeVersionId,\n      );\n\n      return version;\n    } catch (error) {\n      return handleError(error, 'Error creating agent version');\n    }\n  },\n});\n\n/**","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/agent-versions.ts#L173-L209","documentation":"A 500 thrown right after createVersionWithRetry succeeds: the handler calls agentsStore.getVersion(versionId) to return the newly created snapshot, and it comes back undefined. This is an internal consistency failure — the store reported a created versionId but cannot read it back.","triggerScenarios":"createVersionWithRetry returns a versionId but a subsequent getVersion(versionId) finds no row — e.g. an eventual-consistency read lag, a concurrent retention-limit deletion, or a buggy custom VersionedStore whose create/get are inconsistent.","commonSituations":"Custom storage adapters with non-atomic create-then-read behavior; concurrent requests racing with enforceRetentionLimit deleting rows; replication lag on a read replica.","solutions":["Retry the request — transient read-after-write races usually resolve","If using a custom VersionedStore, make getVersion immediately consistent after createVersion (read-your-writes)","Check for concurrent deletion paths (retention enforcement, manual deletes) targeting the same agent","Upgrade @mastra/core/server storage packages if using a maintained adapter with this bug"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"let lastErr: unknown;\nfor (let i = 0; i < 3; i++) {\n  try {\n    const res = await fetch(`/api/stored/agents/${agentId}/versions`, { method: 'POST', body });\n    if (res.ok) return await res.json();\n    if (res.status !== 500) throw new Error(`Create failed: ${res.status}`);\n  } catch (e) { lastErr = e; }\n  await new Promise(r => setTimeout(r, 2 ** i * 100));\n}\nthrow new Error(`Failed to retrieve created version after retries: ${lastErr}`);","preventionTips":["Avoid hammering create-version concurrently for the same agent (retention deletion races)","Use storage adapters with read-your-writes consistency for version stores","If on a custom store, ensure createVersion commits before returning the ID"],"tags":["storage","internal-error","agent-versions","consistency"],"backgroundTag":"read-after-write-inconsistency","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}