Skip to content

inputToMastraDBMessage throws for cross-thread messages when using resource-scoped observational memory #15367

@emekdahl

Description

@emekdahl

Bug

inputToMastraDBMessage() throws Received input message with wrong threadId when an agent is called with memory that uses resource-scoped observational memory and the user has messages on multiple threads.

Reproduction

  1. Configure memory with observationalMemory: { scope: "resource" } and lastMessages: 5
  2. Create messages on thread A for a resource (e.g. a chat thread)
  3. Call agent.generate() with memory: { thread: "thread-B", resource: "same-resource" }
  4. Wait for the user to accumulate enough messages for observational memory to trigger (30k+ tokens)
  5. On the next agent call, getContext() calls listMessagesByResourceId() which fetches messages from ALL threads (A and B)
  6. Messages from thread A have threadId: "thread-A" but the agent expects threadId: "thread-B"
  7. inputToMastraDBMessage() throws because messageSource !== "memory" and threadIds don't match

Root Cause

In @mastra/memory, getContext() at index.js:17362-17369 uses listMessagesByResourceId() when omEngine.scope === "resource" — this returns messages from all threads. But inputToMastraDBMessage() in @mastra/core at input-converter.ts validates that every non-memory message's threadId matches the agent's target thread. The retrieval and validation are incompatible.

Expected Behavior

Cross-thread messages retrieved by getContext() should either:

  • Be tagged with messageSource: "memory" so they bypass the validation
  • Have their threadId reassigned to the target thread
  • Or the validation should be scoped to only user-provided input messages, not internally-retrieved ones

Versions

  • @mastra/core: 1.23.0, 1.24.1 (confirmed present in both)
  • @mastra/memory: 1.14.0, 1.15.0

Workaround

We patched inputToMastraDBMessage() to reassign message.threadId = context.memoryInfo.threadId instead of throwing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions