charmbracelet/crush · error

failed to reinitialize agent for non-interactive mode: %w

Error message

failed to reinitialize agent for non-interactive mode: %w

What it means

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.

Source

Thrown at internal/app/app.go:272

		sess, err := app.Sessions.GetLast(ctx)
		if err != nil {
			return session.Session{}, fmt.Errorf("no sessions found to continue")
		}
		return sess, nil

	default:
		return app.Sessions.Create(ctx, agent.DefaultSessionName)
	}
}

// RunNonInteractive runs the application in non-interactive mode with the
// given prompt, printing to stdout.
func (app *App) RunNonInteractive(ctx context.Context, output io.Writer, prompt, largeModel, smallModel string, hideSpinner bool, continueSessionID string, useLast bool) error {
	slog.Info("Running in non-interactive mode")

	// Re-initialize the coder agent without interactive-only tools.
	if err := app.InitCoderAgentNonInteractive(ctx); err != nil {
		return fmt.Errorf("failed to reinitialize agent for non-interactive mode: %w", err)
	}

	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	if largeModel != "" || smallModel != "" {
		if err := app.overrideModelsForNonInteractive(ctx, largeModel, smallModel); err != nil {
			return fmt.Errorf("failed to override models: %w", err)
		}
	}

	var (
		spinner   *format.Spinner
		stderrTTY bool
		progress  bool
	)

	stderrTTY = term.IsTerminal(os.Stderr.Fd())

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Run `crush` interactively once to validate config, or check crush.json/crushrc for invalid agent/model entries
  2. Run with debug logging (SLOG_LEVEL=debug) to see the underlying wrapped error
  3. Update config to a valid agent definition and model IDs for the provider
  4. Upgrade/reinstall crush if a template or tool file is missing

Example fix

// before
app.RunNonInteractive(ctx, os.Stdout, prompt, "", "", true, "", false) // assumes defaults are valid
// after
if err := cfg.ValidateAgents(); err != nil { // check config first
	return fmt.Errorf("invalid config: %w", err)
}
if err := app.RunNonInteractive(ctx, os.Stdout, prompt, "", "", true, "", false); err != nil {
	return fmt.Errorf("non-interactive run: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

if _, err := os.Stat(cfgPath); err != nil {
	return fmt.Errorf("crush config missing: %w", err)
}
if err := cfg.Validate(); err != nil {
	return fmt.Errorf("invalid crush config: %w", err)
}

Try / catch

if err := app.RunNonInteractive(ctx, os.Stdout, prompt, "", "", true, "", false); err != nil {
	var initErr error
	if errors.Unwrap(err) != nil {
		initErr = errors.Unwrap(err)
	}
	return fmt.Errorf("non-interactive run failed (agent init): %w (cause: %v)", err, initErr)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/63c4a36492ab3019. Report an issue: GitHub.