{"record":{"id":"63c4a36492ab3019","repo":"charmbracelet/crush","slug":"failed-to-reinitialize-agent-for-non-interactive-m","errorCode":null,"errorMessage":"failed to reinitialize agent for non-interactive mode: %w","messagePattern":"failed to reinitialize agent for non-interactive mode: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/app/app.go","lineNumber":272,"sourceCode":"\t\tsess, err := app.Sessions.GetLast(ctx)\n\t\tif err != nil {\n\t\t\treturn session.Session{}, fmt.Errorf(\"no sessions found to continue\")\n\t\t}\n\t\treturn sess, nil\n\n\tdefault:\n\t\treturn app.Sessions.Create(ctx, agent.DefaultSessionName)\n\t}\n}\n\n// RunNonInteractive runs the application in non-interactive mode with the\n// given prompt, printing to stdout.\nfunc (app *App) RunNonInteractive(ctx context.Context, output io.Writer, prompt, largeModel, smallModel string, hideSpinner bool, continueSessionID string, useLast bool) error {\n\tslog.Info(\"Running in non-interactive mode\")\n\n\t// Re-initialize the coder agent without interactive-only tools.\n\tif err := app.InitCoderAgentNonInteractive(ctx); err != nil {\n\t\treturn fmt.Errorf(\"failed to reinitialize agent for non-interactive mode: %w\", err)\n\t}\n\n\tctx, cancel := context.WithCancel(ctx)\n\tdefer cancel()\n\n\tif largeModel != \"\" || smallModel != \"\" {\n\t\tif err := app.overrideModelsForNonInteractive(ctx, largeModel, smallModel); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to override models: %w\", err)\n\t\t}\n\t}\n\n\tvar (\n\t\tspinner   *format.Spinner\n\t\tstderrTTY bool\n\t\tprogress  bool\n\t)\n\n\tstderrTTY = term.IsTerminal(os.Stderr.Fd())","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/app/app.go#L254-L290","documentation":"RunNonInteractive wraps any error from InitCoderAgentNonInteractive with this message. The coder agent must be rebuilt without interactive-only tools (edit/view TUI-specific tooling) before a headless run; if that re-initialization fails (prompt template loading, tool wiring, provider resolution), the whole non-interactive run aborts before any LLM call.","triggerScenarios":"app.RunNonInteractive is called and app.InitCoderAgentNonInteractive(ctx) returns an error — typically because the agent definition referenced by the 'coder' agent is missing/invalid, a system prompt template fails to load, or a tool fails to initialize.","commonSituations":"Corrupt or missing crush.json/crushrc agent configuration; a custom agent definition referencing an unknown model; a broken embedded prompt template after a version upgrade; misconfigured MCP tool causing the agent tool palette to fail wiring.","solutions":["Run `crush` interactively once to validate config, or check crush.json/crushrc for invalid agent/model entries","Run with debug logging (SLOG_LEVEL=debug) to see the underlying wrapped error","Update config to a valid agent definition and model IDs for the provider","Upgrade/reinstall crush if a template or tool file is missing"],"exampleFix":"// before\napp.RunNonInteractive(ctx, os.Stdout, prompt, \"\", \"\", true, \"\", false) // assumes defaults are valid\n// after\nif err := cfg.ValidateAgents(); err != nil { // check config first\n\treturn fmt.Errorf(\"invalid config: %w\", err)\n}\nif err := app.RunNonInteractive(ctx, os.Stdout, prompt, \"\", \"\", true, \"\", false); err != nil {\n\treturn fmt.Errorf(\"non-interactive run: %w\", err)\n}","handlingStrategy":"validation","validationCode":"if _, err := os.Stat(cfgPath); err != nil {\n\treturn fmt.Errorf(\"crush config missing: %w\", err)\n}\nif err := cfg.Validate(); err != nil {\n\treturn fmt.Errorf(\"invalid crush config: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := app.RunNonInteractive(ctx, os.Stdout, prompt, \"\", \"\", true, \"\", false); err != nil {\n\tvar initErr error\n\tif errors.Unwrap(err) != nil {\n\t\tinitErr = errors.Unwrap(err)\n\t}\n\treturn fmt.Errorf(\"non-interactive run failed (agent init): %w (cause: %v)\", err, initErr)\n}","preventionTips":["Validate agent/model config in CI before headless runs","Keep crush updated so embedded prompt templates match the binary","Log the wrapped cause with %v/errors.Unwrap when handling this error"],"tags":["go","agent-initialization","non-interactive"],"backgroundTag":"agent-init-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}