mastra-ai/mastra · error

Could not resolve current thread.

Error message

Could not resolve current thread.

What it means

The recall tool accepts threadId: 'current' as a sentinel meaning "the thread this run belongs to" (om-tools.ts:1366, 1372). If the LLM passes 'current' but context.agent.threadId is unset — the run was started without a thread — the sentinel cannot be resolved and the tool throws 'Could not resolve current thread.'

Source

Thrown at packages/memory/src/tools/om-tools.ts:1373

        detail?: RecallDetail;
        partType?: 'text' | 'tool-call' | 'tool-result' | 'reasoning' | 'image' | 'file';
        toolName?: string;
        partIndex?: number;
        charOffset?: number;
        before?: string;
        after?: string;
      };
      const memory = (context as any)?.memory as RecallMemory | undefined;
      const currentThreadId = context?.agent?.threadId;
      const resourceId = context?.agent?.resourceId;
      const resolvedExplicitThreadId = explicitThreadId === 'current' ? currentThreadId : explicitThreadId;

      if (!memory) {
        throw new Error('Memory instance is required for recall');
      }

      if (explicitThreadId === 'current' && !currentThreadId) {
        throw new Error('Could not resolve current thread.');
      }

      // Search mode
      if (mode === 'search') {
        // Schema validation rejects mode="search" when search is disabled, but
        // validation is skipped for resumed runs and builder-validated input —
        // a stale search call on those paths would otherwise reach
        // Memory.searchMessages and throw. Return guidance instead.
        if (!searchEnabled) {
          return { results: SEARCH_NOT_CONFIGURED_MESSAGE, count: 0 };
        }
        if (!query) {
          throw new Error('query is required for mode="search"');
        }
        if (!resourceId) {
          throw new Error('Resource ID is required for recall');
        }
        return searchMessagesForResource({

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Always start memory-backed conversations with a threadId: agent.generate({ messages, resourceId, threadId }).
  2. If no thread exists, pass a concrete threadId (or call mode='threads' after creating one) instead of 'current'.
  3. Have the client/UI create a thread up front via memory.createThread and reuse its ID.
  4. Catch this error in tooling and return guidance telling the model a thread must be created first.

Example fix

// before
await agent.generate({ messages, resourceId }); // no threadId
// after
await agent.generate({ messages, resourceId, threadId: thread.id });
Defensive patterns

Strategy: validation

Validate before calling

// before relying on threadId: 'current'
if (explicitThreadId === 'current' && !context?.agent?.threadId) {
  throw new Error("threadId='current' requested but the run has no thread");
}

Type guard

function canResolveCurrentThread(
  ctx?: { agent?: { threadId?: string } }
): ctx is { agent: { threadId: string } } {
  return typeof ctx?.agent?.threadId === 'string' && ctx.agent.threadId.length > 0;
}

Try / catch

try {
  return await recallExecute(inputData, context);
} catch (err) {
  if (err instanceof Error && err.message === 'Could not resolve current thread.') {
    return { error: 'No current thread — start a conversation with a threadId first.' };
  }
  throw err;
}

Prevention

When it happens

Trigger: LLM calls the recall tool with threadId='current' (or resource-scope mode where threadId defaults to current) during an agent run launched without a threadId parameter, so context.agent.threadId is undefined.

Common situations: Stateless one-off agent.generate/stream calls with no threadId; interactive UIs that forgot to create/attach a thread per user; memory threads pruned between turns; tests invoking the agent without thread scaffolding.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/88cfa84fa3c1c81b. Report an issue: GitHub.