{"record":{"id":"75776c7395bc049b","repo":"mastra-ai/mastra","slug":"experiment-id-conflict","errorCode":"EXPERIMENT_ID_CONFLICT","errorMessage":"Experiment id ${args.id} already exists and does not match this experiment's dataset or target","messagePattern":"Experiment id (.+?) already exists and does not match this experiment's dataset or target","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/datasets/dataset.ts","lineNumber":705,"sourceCode":"      throw new MastraError({\n        id: 'DATASET_NOT_FOUND',\n        text: `Dataset not found: ${this.id}`,\n        domain: 'STORAGE',\n        category: 'USER',\n      });\n    }\n\n    // Idempotent create: a retried create with the same id returns the\n    // existing experiment instead of failing or duplicating.\n    if (args?.id) {\n      const existing = await experimentsStore.getExperimentById({ id: args.id, filters: this.#scope });\n      if (existing) {\n        const sameShape =\n          existing.datasetId === this.id &&\n          existing.targetType === (args.targetType ?? null) &&\n          existing.targetId === (args.targetId ?? null);\n        if (!sameShape) {\n          throw new MastraError({\n            id: 'EXPERIMENT_ID_CONFLICT',\n            text: `Experiment id ${args.id} already exists and does not match this experiment's dataset or target`,\n            domain: 'STORAGE',\n            category: 'USER',\n          });\n        }\n        // Repair a half-created record: a prior create that crashed between\n        // createExperiment and the status update leaves the row 'pending'.\n        // A retried create finishes the job so the caller never observes a\n        // permanently-pending experiment.\n        let status = existing.status;\n        let startedAt = existing.startedAt ?? null;\n        if (existing.status === 'pending') {\n          startedAt = new Date();\n          await experimentsStore.updateExperiment({ id: existing.id, status: 'running', startedAt });\n          status = 'running';\n        }\n        return {","sourceCodeStart":687,"sourceCodeEnd":723,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/datasets/dataset.ts#L687-L723","documentation":"createExperiment supports a caller-supplied experiment id (args.id). If an experiment with that id already exists but was created against a different dataset or target, reusing the id would silently mix results across shapes, so it is rejected. Same datasetId + targetType + targetId is accepted (idempotent reuse).","triggerScenarios":"Calling createExperiment with an explicit args.id that collides with an existing experiment bound to a different dataset or target.","commonSituations":"Hardcoded experiment ids in CI re-run against a changed agent; reusing an id after renaming the target; ids generated deterministically from a config that changed.","solutions":["Use a new unique experiment id","If re-running the same evaluation intentionally, keep dataset and target identical so the id matches","Delete the old experiment if it should be replaced, then recreate with the same id"],"exampleFix":"// before\nawait dataset.createExperiment({ id: 'nightly-1', targetType: 'agent', targetId: 'newAgent' }); // 'nightly-1' used agent 'oldAgent'\n// after\nawait dataset.createExperiment({ id: `nightly-1-${targetId}`, targetType: 'agent', targetId: 'newAgent' });","handlingStrategy":"validation","validationCode":"const existing = await experimentsStore.getExperimentById({ id: args.id, filters: scope });\nconst conflicts = existing && (existing.datasetId !== dataset.id || existing.targetType !== (args.targetType ?? null) || existing.targetId !== (args.targetId ?? null));\nif (conflicts) throw new Error(`Experiment id ${args.id} is bound to a different dataset/target`);","typeGuard":"function idIsReusable(existing: { datasetId: string; targetType: string | null; targetId: string | null } | null, datasetId: string, args: { targetType?: string; targetId?: string }): boolean { return !existing || (existing.datasetId === datasetId && existing.targetType === (args.targetType ?? null) && existing.targetId === (args.targetId ?? null)); }","tryCatchPattern":"try {\n  await dataset.createExperiment(args);\n} catch (e) {\n  if (isMastraError(e) && e.id === 'EXPERIMENT_ID_CONFLICT') {\n    console.warn(`Id ${args.id} taken by a different shape; generating a new id`);\n  } else throw e;\n}","preventionTips":["Generate experiment ids with dataset/target baked in (e.g. `${datasetId}-${targetId}-${runDate}`)","Only reuse explicit ids for idempotent re-runs of the identical dataset+target","Clean up old experiments before recycling their ids"],"tags":["storage","experiment","conflict","duplicate-id"],"backgroundTag":"identifier-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}