{"record":{"id":"40225c2bbfb79829","repo":"mastra-ai/mastra","slug":"stored-agent-with-id-storedagentid-not-found","errorCode":null,"errorMessage":"Stored agent with id ${storedAgentId} not found","messagePattern":"Stored agent with id (.+?) not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"packages/server/src/server/handlers/stored-agent-favorites.ts","lineNumber":57,"sourceCode":"  summary: 'Favorite a stored agent',\n  description: 'Marks the stored agent as favorited by the calling user. Idempotent.',\n  tags: ['Stored Agents'],\n  requiresAuth: true,\n  requiresPermission: 'stored-agents:read',\n  handler: async ({ mastra, requestContext, storedAgentId }) => {\n    try {\n      await requireBuilderFeature(mastra, 'favorites');\n\n      const callerId = getCallerAuthorId(requestContext);\n      if (!callerId) {\n        throw new HTTPException(401, { message: 'Authentication required' });\n      }\n\n      const { agentStore, favoritesStore } = await getFavoritesContext(mastra);\n\n      const agent = await agentStore.getById(storedAgentId);\n      if (!agent) {\n        throw new HTTPException(404, { message: `Stored agent with id ${storedAgentId} not found` });\n      }\n      assertStoredResourceScope(agent, await getStoredResourceScope(mastra, requestContext));\n\n      // Throws 404 if the caller cannot read the agent (private + not owner/admin).\n      assertReadAccess({ requestContext, resource: 'stored-agents', resourceId: storedAgentId, record: agent });\n\n      const result = await favoritesStore.favorite({\n        userId: callerId,\n        entityType: 'agent',\n        entityId: storedAgentId,\n      });\n      return result;\n    } catch (error) {\n      return handleError(error, 'Error favoriting stored agent');\n    }\n  },\n});\n","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/stored-agent-favorites.ts#L39-L75","documentation":"After authentication, the favorites handler loads the stored agent via agentStore.getById(storedAgentId). If no record matches that id, it throws HTTPException 404 with the offending id in the message. Subsequent scope and read-access checks only run when the agent exists.","triggerScenarios":"Toggling favorites for a storedAgentId that was deleted, never existed, or belongs to a different storage backend than the one queried.","commonSituations":"Stale id cached in the UI after the agent was deleted; hard-coded/copy-pasted id from another environment; id typo; storage pointing at a different database than where the agent was created.","solutions":["Confirm the storedAgentId exists via the stored agents list endpoint","Refresh the UI/client cache and retry with the current id","Check that the server's storage points at the environment where the agent lives","Handle 404 in the client by removing the stale agent from local state"],"exampleFix":"// before\nawait api.put(`/stored-agents/${staleId}/favorite`);\n// after\nconst agent = await api.get(`/stored-agents/${id}`);\nif (agent) await api.put(`/stored-agents/${id}/favorite`);\nelse removeStaleAgent(id);","handlingStrategy":"validation","validationCode":"const agent = await api.getStoredAgent(storedAgentId).catch(() => null);\nif (!agent) throw new Error(`Stored agent ${storedAgentId} does not exist`);","typeGuard":"function isStoredAgent(x: unknown): x is { id: string } {\n  return typeof x === 'object' && x !== null && typeof (x as any).id === 'string';\n}","tryCatchPattern":"try {\n  await api.toggleFavorite(storedAgentId);\n} catch (e) {\n  if (e.status === 404) { purgeLocalAgent(storedAgentId); }\n  else throw e;\n}","preventionTips":["Refresh agent lists before acting on cached ids","Invalidate local caches when agents are deleted","Never hard-code storedAgentIds across environments"],"tags":["http-404","not-found","storage","stored-agents"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}