{"record":{"id":"052ac4a5a9113902","repo":"mastra-ai/mastra","slug":"duplicate-worker-name-w-name-in-the-workers","errorCode":null,"errorMessage":"Duplicate worker name \"${w.name}\" in the 'workers' option","messagePattern":"Duplicate worker name \"(.+?)\" in the 'workers' option","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/mastra/index.ts","lineNumber":1418,"sourceCode":"      // `handleWorkflowEvent` directly to the pubsub during startWorkers().\n      const pubsubModes = this.#pubsub.supportedModes ?? ['pull'];\n      const defaultWorkers: MastraWorker[] = [];\n      if (pubsubModes.includes('pull')) {\n        defaultWorkers.push(new OrchestrationWorker());\n      }\n      // SchedulerWorker is added in startWorkers() rather than here so its\n      // storage-backed runtime is only initialized when workers actually start.\n      if (config?.backgroundTasks?.enabled) {\n        defaultWorkers.push(new BackgroundTaskWorker(config.backgroundTasks));\n      }\n      // Merge custom workers with the defaults: a custom worker replaces a\n      // default sharing its name (e.g. a custom OrchestrationWorker), and\n      // duplicate names within the custom array fail loud.\n      const customWorkers = workersOption ?? [];\n      const customNames = new Set<string>();\n      for (const w of customWorkers) {\n        if (customNames.has(w.name)) {\n          throw new Error(`Duplicate worker name \"${w.name}\" in the 'workers' option`);\n        }\n        customNames.add(w.name);\n      }\n      this.#workers = [...defaultWorkers.filter(w => !customNames.has(w.name)), ...customWorkers];\n      for (const w of this.#workers) {\n        w.__registerMastra(this);\n      }\n    }\n\n    let logger: TLogger;\n    if (config?.logger === false) {\n      logger = noopLogger as unknown as TLogger;\n      this.#loggerExplicit = true;\n    } else {\n      if (config?.logger) {\n        logger = config.logger;\n        this.#loggerExplicit = true;\n      } else {","sourceCodeStart":1400,"sourceCodeEnd":1436,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/mastra/index.ts#L1400-L1436","documentation":"When constructing the Mastra instance, custom workers from the 'workers' option are scanned for duplicate names; the constructor throws a plain Error naming the duplicated worker so worker resolution stays unambiguous. Default workers sharing a custom worker's name are silently replaced by the custom one, but duplicates inside the custom array fail loudly.","triggerScenarios":"Passing Mastra({ workers: [...] }) where two custom workers in the array have the same .name property (including two instances of the same custom OrchestrationWorker).","commonSituations":"Spreading a workers array that accidentally contains the same worker twice; generating workers in a loop with a fixed name; copy-pasting a worker config without renaming it.","solutions":["Give each custom worker in the workers array a unique name.","Deduplicate the array before constructing Mastra (e.g. new Map(workers.map(w => [w.name, w])).values()).","If the same worker instance is included twice, include it once.","Fix loop code that assigns a constant name instead of a per-item name."],"exampleFix":"// before\nnew Mastra({ workers: [new MyWorker({ name: 'ingest' }), new MyWorker({ name: 'ingest' })] });\n// after\nnew Mastra({ workers: [new MyWorker({ name: 'ingest' }), new MyWorker({ name: 'enrich' })] });","handlingStrategy":"validation","validationCode":"const names = (workersOption ?? []).map(w => w.name);\nconst dupes = names.filter((n, i) => names.indexOf(n) !== i);\nif (dupes.length) throw new Error(`Duplicate worker names: ${dupes.join(', ')}`);\nnew Mastra({ workers: workersOption });","typeGuard":"function hasUniqueWorkerNames(ws: { name: string }[]): ws is { name: string }[] {\n  return new Set(ws.map(w => w.name)).size === ws.length;\n}","tryCatchPattern":"try {\n  const mastra = new Mastra({ workers });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Duplicate worker name')) {\n    workers = [...new Map(workers.map(w => [w.name, w])).values()];\n  }\n  throw e;\n}","preventionTips":["Deduplicate worker arrays before constructing Mastra","Name workers after their function/queue to avoid collisions","Avoid building workers in loops with fixed names"],"tags":["workers","configuration","duplicate","mastra-constructor"],"backgroundTag":"duplicate-identifier","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}