{"record":{"id":"32ea06c1fb30d0c4","repo":"mastra-ai/mastra","slug":"thread-with-id-id-not-found","errorCode":null,"errorMessage":"Thread with id ${id} not found","messagePattern":"Thread with id (.+?) not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/memory/inmemory.ts","lineNumber":87,"sourceCode":"  async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {\n    const key = thread.id;\n    this.db.threads.set(key, thread);\n    return thread;\n  }\n\n  async updateThread({\n    id,\n    title,\n    metadata,\n  }: {\n    id: string;\n    title?: string;\n    metadata?: Record<string, unknown>;\n  }): Promise<StorageThreadType> {\n    const thread = this.db.threads.get(id);\n\n    if (!thread) {\n      throw new Error(`Thread with id ${id} not found`);\n    }\n\n    if (thread) {\n      if (title !== undefined) thread.title = title;\n      thread.metadata = { ...thread.metadata, ...metadata };\n      thread.updatedAt = new Date();\n    }\n    return thread;\n  }\n\n  async deleteThread({ threadId }: { threadId: string }): Promise<void> {\n    this.db.threads.delete(threadId);\n\n    this.db.messages.forEach((msg, key) => {\n      if (msg.thread_id === threadId) {\n        this.db.messages.delete(key);\n      }\n    });","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/memory/inmemory.ts#L69-L105","documentation":"InMemoryStorage.updateThread throws when no thread exists in the in-memory store with the given id. The lookup this.db.threads.get(id) returns undefined and the library fails fast rather than silently creating or no-oping an update.","triggerScenarios":"Calling updateThread({ id }) with an id that was never created in the current process, or after the in-memory store was restarted/cleared; updating a thread from a different storage instance than the one that created it.","commonSituations":"Server restarts wiping the in-memory store while clients hold stale thread ids; switching between dev (in-memory) and prod (persistent) storage; typos or case mismatches in thread ids; tests reusing ids across fresh storage instances.","solutions":["Verify the thread exists (getThreadById / listThreads) before updating, or create it if missing.","Persist threads with a real storage adapter (LibSQL/Postgres/etc.) so ids survive restarts.","Ensure the same storage instance is used for create and update within the process.","Log and handle the error as 'thread not found' (404-style) rather than a retryable failure."],"exampleFix":"// before\nawait storage.updateThread({ id: threadId, title: 'New' }); // throws if missing\n// after\nconst existing = await storage.getThreadById({ threadId });\nif (!existing) {\n  await storage.saveThread({ thread: { id: threadId, title: 'New', createdAt: new Date(), updatedAt: new Date(), metadata: {} } });\n} else {\n  await storage.updateThread({ id: threadId, title: 'New' });\n}","handlingStrategy":"try-catch","validationCode":"async function updateThreadIfExists(storage, id, patch) {\n  const existing = await storage.getThreadById({ threadId: id });\n  if (!existing) return null;\n  return storage.updateThread({ id, ...patch });\n}","typeGuard":"function isThread(t: unknown): t is { id: string; title?: string; metadata?: Record<string, unknown> } {\n  return !!t && typeof (t as any).id === 'string';\n}","tryCatchPattern":"try {\n  await storage.updateThread({ id, title });\n} catch (e) {\n  if (e instanceof Error && e.message === `Thread with id ${id} not found`) {\n    // create the thread or return 404 to the caller\n    return null;\n  }\n  throw e;\n}","preventionTips":["Check existence with getThreadById before updating","Use persistent storage adapters in production, not in-memory","Do not reuse thread ids across storage instances or process restarts"],"tags":["storage","not-found","in-memory"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}