{"record":{"id":"b3f045943469b13a","repo":"mastra-ai/mastra","slug":"failed-to-resolve-created-agent","errorCode":null,"errorMessage":"Failed to resolve created agent","messagePattern":"Failed to resolve created agent","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"packages/server/src/server/handlers/stored-agents.ts","lineNumber":723,"sourceCode":"      // reliably determine it before saving — so the flag is overridden here.\n      const isCodeSourceEditor = editor?.getSource?.() === 'code';\n      const { versions } = await agentsStore.listVersions({ agentId: id, perPage: 1 });\n      const initialVersion = versions[0];\n      if (initialVersion && (autoPublish !== false || isCodeSourceEditor)) {\n        await agentsStore.update({\n          id,\n          activeVersionId: initialVersion.id,\n          status: 'published',\n        });\n      }\n      editor?.agent.clearCache(id);\n\n      // Return the resolved agent (thin record + version config). Published resolution falls\n      // back to the latest version, so an unpublished code-agent override still returns the\n      // config the caller just saved.\n      const resolved = await agentsStore.getByIdResolved(id, { status: 'published' });\n      if (!resolved) {\n        throw new HTTPException(500, { message: 'Failed to resolve created agent' });\n      }\n\n      return enrichOrStripFavorites(mastra, requestContext, 'agent', resolved);\n    } catch (error) {\n      return handleError(error, 'Error creating stored agent');\n    }\n  },\n});\n\n/**\n * PATCH /stored/agents/:storedAgentId - Update a stored agent\n */\nexport const UPDATE_STORED_AGENT_ROUTE: ServerRoute<\n  InferParams<typeof storedAgentIdPathParams, undefined, typeof updateStoredAgentBodySchema>,\n  z.infer<typeof updateStoredAgentResponseSchema>,\n  'json',\n  RouteSchemas<\n    typeof storedAgentIdPathParams,","sourceCodeStart":705,"sourceCodeEnd":741,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/stored-agents.ts#L705-L741","documentation":"Internal error (HTTP 500) thrown immediately after creating a stored agent: the handler re-reads the record via `agentsStore.getByIdResolved(id, { status: 'published' })` and got null. Since the handler just created and (by default) published the agent, a null resolution means the created record or its published version isn't visible to the read path — an inconsistent storage state rather than a client mistake.","triggerScenarios":"Create-agent request where the record was written (create succeeded) but getByIdResolved with status 'published' returns nothing — e.g. autoPublish=false leaving only a draft, a storage adapter whose write isn't yet readable, or a corrupted/partial version write.","commonSituations":"Custom storage adapters with read-after-write inconsistencies; adapter versions where the versioning tables weren't migrated; calling create with autoPublish:false and then expecting published resolution; concurrent requests racing between create and resolve.","solutions":["Use an up-to-date official storage adapter and run pending migrations so versions/agents tables are consistent","If staging a draft with autoPublish:false, fetch via draft status instead of assuming published resolution","Retry the read after create; if it persists, inspect the agents/versions rows to find the missing version record","If using a custom adapter, ensure getByIdResolved correctly joins the thin record with its versions"],"exampleFix":"// before\nconst resolved = await agentsStore.getByIdResolved(id, { status: 'published' }); // null when autoPublish=false\n\n// after\nconst resolved =\n  (await agentsStore.getByIdResolved(id, { status: 'published' })) ??\n  (await agentsStore.getByIdResolved(id, { status: 'draft' }));","handlingStrategy":"retry","validationCode":"const created = await createStoredAgent(input);\nconst resolved = await getStoredAgent(created.id);\nif (!resolved) console.warn('created agent not yet resolvable — check storage adapter/versioning');","typeGuard":"function isResolvedAgent(a: unknown): a is { id: string; config: unknown } {\n  return !!a && typeof a === 'object' && 'id' in a && 'config' in a;\n}","tryCatchPattern":"try {\n  return await createStoredAgent(input);\n} catch (e) {\n  if (isHttpError(e) && e.status === 500 && /Failed to resolve created agent/.test(e.message)) {\n    await sleep(200);\n    return getStoredAgent(input.id); // verify post-hoc\n  }\n  throw e;\n}","preventionTips":["Keep storage adapters and core versions up to date (versioning/resolution APIs evolve)","Verify read-after-write behavior of any custom adapter in integration tests","Avoid mixing autoPublish:false creates with published-status reads"],"tags":["storage","resolution","http-500","read-after-write"],"backgroundTag":"resource-not-resolvable","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}