{"record":{"id":"b6d7f438504689c2","repo":"mastra-ai/mastra","slug":"dataset-not-found-args-datasetid-b6d7f4","errorCode":null,"errorMessage":"Dataset not found: ${args.datasetId}","messagePattern":"Dataset not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/datasets/inmemory.ts","lineNumber":236,"sourceCode":"    const end = perPageInput === false ? datasets.length : start + perPage;\n\n    return {\n      datasets: datasets.slice(start, end).map(toDatasetRecord),\n      pagination: {\n        total: datasets.length,\n        page,\n        perPage: perPageForResponse,\n        hasMore: perPageInput === false ? false : datasets.length > end,\n      },\n    };\n  }\n\n  // --- SCD-2 item mutations ---\n\n  protected async _doAddItem(args: AddDatasetItemInput): Promise<DatasetItem> {\n    const dataset = this.db.datasets.get(args.datasetId);\n    if (!dataset) {\n      throw new Error(`Dataset not found: ${args.datasetId}`);\n    }\n\n    // Bump version (T3.7, T3.26 — only bumps version, not updatedAt)\n    const newVersion = dataset.version + 1;\n    this.db.datasets.set(args.datasetId, { ...dataset, version: newVersion });\n\n    const now = new Date();\n    const id = crypto.randomUUID();\n    const row: DatasetItemRow = {\n      id,\n      datasetId: args.datasetId,\n      datasetVersion: newVersion,\n      externalId: args.externalId ?? null,\n      // Tenancy inherited from parent dataset (Option B — never settable per item)\n      organizationId: dataset.organizationId ?? null,\n      projectId: dataset.projectId ?? null,\n      validTo: null,\n      isDeleted: false,","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/datasets/inmemory.ts#L218-L254","documentation":"InMemory storage's _doAddItem() fetches the dataset by args.datasetId and throws this plain Error if missing, before bumping the dataset version and inserting the SCD-2 row. Normally batchInsertItems() already verified existence, so this fires when the dataset vanishes between the base-class check and the subclass mutation, or when _doAddItem is reached with a stale/foreign datasetId.","triggerScenarios":"addItems()/batchInsertItems() on an InMemory store where the datasetId does not exist, was deleted concurrently, or tenancy filters removed the row after the base check.","commonSituations":"Inserting items into a dataset after a concurrent deleteDataset, restart-erased InMemory state, or test scaffolding that never created the dataset.","solutions":["Create the dataset first (createDataset) or confirm getDatasetById({id: datasetId}) returns a row.","Check for concurrent deleteDataset calls racing with item inserts; serialize mutations per dataset.","Use a persistent storage adapter if the process restarts between creating datasets and adding items.","Fix tenancy filters so they match the dataset's stored organizationId/projectId."],"exampleFix":"// before\nawait storage.datasets.addItems({ datasetId: 'ds', items: [item] });\n// after\nconst ds = await storage.datasets.getDatasetById({ id: 'ds' });\nif (!ds) throw new Error(`Create dataset 'ds' before adding items`);\nawait storage.datasets.addItems({ datasetId: 'ds', items: [item] });","handlingStrategy":"validation","validationCode":"const ds = await storage.datasets.getDatasetById({ id: datasetId });\nif (!ds) throw new Error(`Dataset '${datasetId}' must exist before adding items`);","typeGuard":"function datasetExists(ds: Awaited<ReturnType<typeof storage.datasets.getDatasetById>>): ds is NonNullable<typeof ds> {\n  return ds !== null;\n}","tryCatchPattern":"try {\n  await storage.datasets.addItems({ datasetId, items });\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Dataset not found:')) {\n    await storage.datasets.createDataset({ id: datasetId, name: datasetId });\n    await storage.datasets.addItems({ datasetId, items });\n  } else throw e;\n}","preventionTips":["Create the dataset before any item inserts; bootstrap it in setup code.","Avoid deleteDataset racing with concurrent item inserts.","Use persistent storage if datasets/items must outlive the process."],"tags":["storage","datasets","in-memory","not-found"],"backgroundTag":"dataset-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}