{"record":{"id":"2962851d23898616","repo":"mastra-ai/mastra","slug":"source-thread-with-id-sourcethreadid-not-found","errorCode":null,"errorMessage":"Source thread with id ${sourceThreadId} not found","messagePattern":"Source thread with id (.+?) not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/memory/inmemory.ts","lineNumber":658,"sourceCode":"        metadata: {\n          ...resource.metadata,\n          ...metadata,\n        },\n        updatedAt: new Date(),\n      };\n    }\n\n    this.db.resources.set(resourceId, resource);\n    return resource;\n  }\n\n  async cloneThread(args: StorageCloneThreadInput): Promise<StorageCloneThreadOutput> {\n    const { sourceThreadId, newThreadId: providedThreadId, resourceId, title, metadata, options } = args;\n\n    // Get the source thread\n    const sourceThread = this.db.threads.get(sourceThreadId);\n    if (!sourceThread) {\n      throw new Error(`Source thread with id ${sourceThreadId} not found`);\n    }\n\n    // Use provided ID or generate a new one\n    const newThreadId = providedThreadId || crypto.randomUUID();\n\n    // Check if the new thread ID already exists\n    if (this.db.threads.has(newThreadId)) {\n      throw new Error(`Thread with id ${newThreadId} already exists`);\n    }\n\n    // Get messages from the source thread\n    let sourceMessages = Array.from(this.db.messages.values())\n      .filter((msg: StorageMessageType) => msg.thread_id === sourceThreadId)\n      .sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());\n\n    // Apply message filters if provided\n    if (options?.messageFilter) {\n      const { startDate, endDate, messageIds } = options.messageFilter;","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/memory/inmemory.ts#L640-L676","documentation":"cloneThread looks up the source thread by id in the in-memory thread map and throws this error if no thread with that id exists. The clone cannot proceed without a source, so it fails fast with a message containing the offending id. This is a lookup/state error, not an argument-shape error.","triggerScenarios":"Calling cloneThread({ sourceThreadId: 'missing-id', newThreadId: 'x' }) where the source id was deleted, never created in this storage instance, or belongs to a different storage/database (e.g. after restarting an in-memory store, which wipes all data).","commonSituations":"Stale UI references to deleted threads; copying ids between dev/prod or between two storage instances; in-memory data lost on process restart while a client still holds the old thread id; typos in hardcoded ids.","solutions":["Verify the thread exists with getThreadById({ threadId }) before cloning.","Confirm you are connected to the same storage instance that owns the thread (in-memory stores do not share state across processes).","Recreate or re-persist the source thread if the store was reset; do not rely on in-memory data surviving restarts."],"exampleFix":"// before\nawait storage.cloneThread({ sourceThreadId: id, newThreadId: cloneId });\n// after\nconst source = await storage.getThreadById({ threadId: id });\nif (!source) throw new Error(`Cannot clone: thread ${id} does not exist`);\nawait storage.cloneThread({ sourceThreadId: id, newThreadId: cloneId });","handlingStrategy":"try-catch","validationCode":"const source = await storage.getThreadById({ threadId: sourceThreadId });\nif (!source) {\n  throw new Error(`Source thread ${sourceThreadId} not found; cannot clone`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await storage.cloneThread({ sourceThreadId, newThreadId });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Source thread') && e.message.includes('not found')) {\n    // stale id: refresh thread list or notify user\n    return null;\n  }\n  throw e;\n}","preventionTips":["Check thread existence before cloning.","Do not cache thread ids across in-memory store restarts or across storage instances.","Refresh UI thread lists after deletes so stale ids are not reused."],"tags":["storage","not-found","thread","state"],"backgroundTag":"entity-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}