{"record":{"id":"e20a076f18ce9dc4","repo":"linshenkx/prompt-optimizer","slug":"iteration-failed-errormessage","errorCode":null,"errorMessage":"Iteration failed: ${errorMessage}","messagePattern":"Iteration failed: (.+?)","errorType":"exception","errorClass":"IterationError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/prompt/service.ts","lineNumber":308,"sourceCode":"      this.validateInput(lastOptimizedPrompt, modelKey);\n      this.validateInput(iterateInput, modelKey);\n\n      // 获取模型配置\n      const modelConfig = await this.modelManager.getModel(modelKey);\n      if (!modelConfig) {\n        throw new ServiceDependencyError(\"ModelManager\", \"Model not found\");\n      }\n\n      // 获取迭代提示词\n      let template;\n      try {\n        template = await this.templateManager.getTemplate(\n          templateId || DEFAULT_TEMPLATES.ITERATE,\n        );\n      } catch (error) {\n        const errorMessage =\n          error instanceof Error ? error.message : String(error);\n        throw new IterationError(\n          originalPrompt,\n          iterateInput,\n          `Iteration failed: ${errorMessage}`,\n        );\n      }\n\n      if (!template?.content) {\n        throw new IterationError(\n          originalPrompt,\n          iterateInput,\n          \"Iteration failed: Template not found or invalid\",\n        );\n      }\n\n      // 🔧 迭代功能必须使用高级模板（message array 格式）以支持变量替换\n      if (typeof template.content === \"string\") {\n        throw new IterationError(\n          originalPrompt,","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/prompt/service.ts#L290-L326","documentation":"Thrown when the TemplateManager fails while fetching the iteration template (templateId or DEFAULT_TEMPLATES.ITERATE). The underlying error message is embedded into an IterationError prefixed with 'Iteration failed:'. This is a wrapper around template-store lookup/retrieval failures, not a model problem.","triggerScenarios":"iteratePrompt with a templateId that does not exist in the TemplateManager, a corrupted/unreadable template store, or the default iterate template being deleted or not seeded. Any exception thrown inside templateManager.getTemplate is caught here and re-wrapped.","commonSituations":"Passing a custom templateId from stale UI state after the template was deleted; first run where seed templates were not installed; template store file permissions or schema migration issues after an upgrade.","solutions":["Inspect the embedded errorMessage to identify the real template-store failure","Verify the template exists: call templateManager.getTemplate(templateId || DEFAULT_TEMPLATES.ITERATE) directly","Restore/re-seed the default ITERATE template or fix the templateId passed by the caller","If the store is corrupted, repair or reset the template store and retry"],"exampleFix":"// before\nawait promptService.iteratePrompt(lastPrompt, input, modelKey, \"my-iter-tpl\");\n\n// after\nconst tplId = \"my-iter-tpl\";\nconst tpl = await templateManager.getTemplate(tplId).catch(() => null);\nif (!tpl) {\n  // fall back to the built-in iteration template\n  await promptService.iteratePrompt(lastPrompt, input, modelKey);\n} else {\n  await promptService.iteratePrompt(lastPrompt, input, modelKey, tplId);\n}","handlingStrategy":"try-catch","validationCode":"const tpl = await templateManager.getTemplate(templateId || DEFAULT_TEMPLATES.ITERATE).catch(() => null);\nif (!tpl) { /* fall back to default or abort early */ }","typeGuard":"null","tryCatchPattern":"try {\n  await promptService.iteratePrompt(last, input, modelKey, templateId);\n} catch (e) {\n  if (e instanceof IterationError && e.message.startsWith(\"Iteration failed:\")) {\n    // inspect suffix; retry with default template if template-related\n  } else throw e;\n}","preventionTips":["Pass DEFAULT_TEMPLATES.ITERATE explicitly instead of relying on stored ids","Validate template existence in UI state when templates are deleted","Keep template-store migrations tested to avoid corrupted lookups"],"tags":["template","template-manager","iteration","wrapper-error"],"backgroundTag":"template-not-found","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}