{"record":{"id":"47bd36ed49237d8f","repo":"mastra-ai/mastra","slug":"resource-id-is-required-to-list-threads","errorCode":null,"errorMessage":"Resource ID is required to list threads","messagePattern":"Resource ID is required to list threads","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/memory/src/tools/om-tools.ts","lineNumber":232,"sourceCode":"  limit = 20,\n  before,\n  after,\n}: {\n  memory: RecallMemory;\n  resourceId: string;\n  currentThreadId: string;\n  page?: number;\n  limit?: number;\n  before?: string;\n  after?: string;\n}): Promise<{\n  threads: string;\n  count: number;\n  page: number;\n  hasMore: boolean;\n}> {\n  if (!resourceId) {\n    throw new Error('Resource ID is required to list threads');\n  }\n\n  const MAX_LIMIT = 50;\n  const normalizedLimit = Math.min(Math.max(limit, 1), MAX_LIMIT);\n\n  const hasDateFilter = !!(before || after);\n  const beforeDate = before ? new Date(before) : null;\n  const afterDate = after ? new Date(after) : null;\n\n  // When date filtering, fetch all threads and filter client-side\n  // (storage layer doesn't support date range on threads)\n  const result = await memory.listThreads({\n    filter: { resourceId },\n    page: hasDateFilter ? 0 : page,\n    perPage: hasDateFilter ? false : normalizedLimit,\n    orderBy: { field: 'updatedAt', direction: 'DESC' },\n  });\n","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/tools/om-tools.ts#L214-L250","documentation":"`listThreadsForResource` lists an agent's threads and requires a `resourceId` to scope the query. If the argument is falsy (undefined, null, or empty string) it throws before touching storage, because listing threads without a resource owner would leak or return unscoped data.","triggerScenarios":"Calling listThreadsForResource with resourceId omitted, or passing an empty string from an uninitialized user/session variable; wiring the om listThreads tool without a resourceId resolver so it defaults to ''. Called by result/page1/page2/recallTool paths.","commonSituations":"Auth context not yet resolved when the tool runs (user id undefined); agent config missing the resourceId wiring; a migration where resourceId was optional in an older API version and is now mandatory.","solutions":["Pass the authenticated user/resource identifier explicitly: listThreadsForResource({ memory, resourceId: user.id, currentThreadId }).","Resolve resourceId from your auth/session context before invoking the tool, and fail early with a friendly message if absent.","If using the LLM-facing tool, keep resourceId out of the model's hands — inject it server-side via the tool's execute closure."],"exampleFix":"// before\nawait listThreadsForResource({ memory, resourceId: session?.userId, currentThreadId });\n// after\nif (!session?.userId) {\n  throw new Error('Sign in before listing threads.');\n}\nawait listThreadsForResource({ memory, resourceId: session.userId, currentThreadId });","handlingStrategy":"validation","validationCode":"if (typeof resourceId !== 'string' || resourceId.trim() === '') {\n  throw new Error('resourceId is required to list threads.');\n}\nawait listThreadsForResource({ memory, resourceId, currentThreadId });","typeGuard":"function hasResourceId(args: { resourceId?: string | null }): args is { resourceId: string } {\n  return typeof args.resourceId === 'string' && args.resourceId.trim().length > 0;\n}","tryCatchPattern":"try {\n  return await listThreadsForResource({ memory, resourceId, currentThreadId });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Resource ID is required')) {\n    return { threads: '[]', count: 0, page: 0, hasMore: false }; // or prompt for sign-in\n  }\n  throw e;\n}","preventionTips":["Inject resourceId from the auth context server-side; never let the model supply it.","Fail fast at request entry when the session has no user ID.","Add a unit test asserting the error when resourceId is missing."],"tags":["validation","memory","resource-id","threads"],"backgroundTag":"missing-required-parameter","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}