{"record":{"id":"4a6cdbe8c7e593f0","repo":"mastra-ai/mastra","slug":"received-input-message-with-wrong-resourceid-inpu","errorCode":null,"errorMessage":"Received input message with wrong resourceId. Input ${message.resourceId}, expected ${context.memoryInfo.resourceId}","messagePattern":"Received input message with wrong resourceId\\. Input (.+?), expected (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/agent/message-list/conversion/input-converter.ts","lineNumber":58,"sourceCode":"    message.threadId &&\n    context.memoryInfo &&\n    message.threadId !== context.memoryInfo.threadId\n  ) {\n    throw new Error(\n      `Received input message with wrong threadId. Input ${message.threadId}, expected ${context.memoryInfo.threadId}`,\n    );\n  }\n\n  // Validate resourceId matches (except for memory messages, which can carry a\n  // system resourceId — e.g. observational-memory continuation messages)\n  if (\n    messageSource !== `memory` &&\n    `resourceId` in message &&\n    message.resourceId &&\n    context.memoryInfo?.resourceId &&\n    message.resourceId !== context.memoryInfo.resourceId\n  ) {\n    throw new Error(\n      `Received input message with wrong resourceId. Input ${message.resourceId}, expected ${context.memoryInfo.resourceId}`,\n    );\n  }\n\n  if (TypeDetector.isMastraMessageV1(message)) {\n    return stampMessageParts(mastraMessageV1ToMastraDBMessage(message, messageSource, context), messageSource);\n  }\n  if (TypeDetector.isMastraDBMessage(message)) {\n    return stampMessageParts(hydrateMastraDBMessageFields(message, context, messageSource), messageSource);\n  }\n  if (TypeDetector.isAIV4CoreMessage(message)) {\n    return stampMessageParts(AIV4Adapter.fromCoreMessage(message, context, messageSource), messageSource);\n  }\n  if (TypeDetector.isAIV4UIMessage(message)) {\n    return stampMessageParts(\n      AIV4Adapter.fromUIMessage(message as UIMessageV4 | UIMessageWithMetadata, context, messageSource),\n      messageSource,\n    );","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/agent/message-list/conversion/input-converter.ts#L40-L76","documentation":"Error thrown by inputToMastraDBMessage when an input message's resourceId does not match the resourceId of the active memory context. This guards against attaching messages owned by one user/resource to another resource's thread. Memory-sourced messages are exempt (e.g. observational-memory continuation messages may carry a system resourceId).","triggerScenarios":"Passing messages whose resourceId differs from context.memoryInfo.resourceId — e.g. injecting another user's stored messages into a new user's thread, or sending messages saved under an old resourceId after the resource was renamed/re-keyed.","commonSituations":"Multi-tenant apps where messages are fetched for the wrong user; changing resourceId scheme (e.g. from email to user ID) without migrating stored messages; shared/cached message arrays reused across users; copy-pasted test fixtures containing a different resourceId.","solutions":["Strip resourceId from input messages and let the agent stamp the current resource's id.","Ensure the messages you pass were fetched for the same resourceId the agent call uses in memory options.","Migrate stored messages if you changed your resourceId convention (update their resourceId field to the new key).","Verify per-user data fetching so one tenant's messages never reach another tenant's agent call."],"exampleFix":"// before\nawait agent.generate(otherUsersMessages, { memory: { thread, resource: 'user-123' } });\n\n// after\nconst mine = await memory.getMessages({ threadId, resourceId: 'user-123' });\nawait agent.generate(mine, { memory: { thread, resource: 'user-123' } });","handlingStrategy":"validation","validationCode":"function assertResourceMatches(messages: Array<{ resourceId?: string }>, expectedResourceId: string): void {\n  for (const m of messages) {\n    if (m.resourceId && m.resourceId !== expectedResourceId) {\n      throw new Error(`Message resourceId ${m.resourceId} != expected ${expectedResourceId}`);\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await agent.generate(messages, { memory: { thread, resource: resourceId } });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('wrong resourceId')) {\n    const scoped = messages.map(({ resourceId: _rid, ...m }) => m);\n    return agent.generate(scoped, { memory: { thread, resource: resourceId } });\n  }\n  throw e;\n}","preventionTips":["Always fetch messages scoped to the same resourceId used in the agent call's memory options.","Migrate stored messages when changing resourceId conventions (email -> user id, etc.).","Strip resourceId from messages before passing them as raw input.","In multi-tenant code, assert tenant/resource ownership of message arrays before agent calls."],"tags":["agent","memory","resource","input-validation"],"backgroundTag":"resource-id-mismatch","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}