{"record":{"id":"71895b9509f76e7c","repo":"charmbracelet/crush","slug":"failed-to-create-session-for-non-interactive-mode","errorCode":null,"errorMessage":"failed to create session for non-interactive mode: %w","messagePattern":"failed to create session for non-interactive mode: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/app/app.go","lineNumber":330,"sourceCode":"\t}\n\n\t// Non-interactive runs get a single shot at the tool palette, so wait for\n\t// MCP initialization to settle before reading MCP tools. The coordinator\n\t// waits again for the same reason (it is the gate the client/server path\n\t// goes through); doing it here too surfaces the failure before we create a\n\t// session, and lets the UpdateModels below see every MCP tool.\n\tif err := mcp.WaitForInit(ctx); err != nil {\n\t\treturn fmt.Errorf(\"failed to wait for MCP initialization: %w\", err)\n\t}\n\n\t// force update of agent models before running so mcp tools are loaded\n\tapp.AgentCoordinator.UpdateModels(ctx)\n\n\tdefer stopSpinner()\n\n\tsess, err := app.resolveSession(ctx, continueSessionID, useLast)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create session for non-interactive mode: %w\", err)\n\t}\n\n\tif continueSessionID != \"\" || useLast {\n\t\tslog.Info(\"Continuing session for non-interactive run\", \"session_id\", sess.ID)\n\t\t// If no explicit model override was requested, restore the\n\t\t// model/provider from the last assistant message in the\n\t\t// session, provided it is still available.\n\t\tif largeModel == \"\" && smallModel == \"\" {\n\t\t\tif err := app.restoreModelFromSession(ctx, sess.ID); err != nil {\n\t\t\t\tslog.Warn(\"Failed to restore model from session\", \"error\", err)\n\t\t\t}\n\t\t}\n\t} else {\n\t\tslog.Info(\"Created session for non-interactive run\", \"session_id\", sess.ID)\n\t}\n\n\t// Automatically approve all permission requests for this non-interactive\n\t// session.","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/app/app.go#L312-L348","documentation":"RunNonInteractive resolves the session via app.resolveSession (creating a new session, or continuing an existing one given continueSessionID/useLast); any error is wrapped with this message. Session creation touches the SQLite store, so this usually indicates a database problem rather than an LLM issue.","triggerScenarios":"resolveSession fails: the SQLite DB cannot be opened/migrated, the continueSessionID does not exist, or the 'use last session' lookup fails.","commonSituations":"Read-only or full disk where the crush DB lives; corrupted ~/.cache/crush DB after an abrupt kill; passing a stale --continue session id that was deleted; schema migration failure after a version upgrade.","solutions":["Verify the DB file location is writable (check $TMPDIR/$XDG_CACHE_HOME paths)","Pass a valid --continue session id (list sessions via `crush sessions`) or omit it to start fresh","Move/delete the corrupt DB file so migrations run again on a fresh DB","Upgrade crush if the DB was written by an incompatible version"],"exampleFix":"// before\ncrush run --continue abc123-definitely-stale \"continue\"\n// after\ncrush sessions // find a valid id\ncrush run --continue 8f2c... \"continue\"","handlingStrategy":"validation","validationCode":"if continueSessionID != \"\" {\n\tsessions := listSessions() // e.g. via `crush sessions`\n\tif !contains(sessions, continueSessionID) {\n\t\treturn fmt.Errorf(\"unknown session id: %s\", continueSessionID)\n\t}\n}\nif _, err := os.Stat(dbPath); err != nil || !writable(dbPath) {\n\treturn fmt.Errorf(\"session DB not writable: %s\", dbPath)\n}","typeGuard":null,"tryCatchPattern":"if err := app.RunNonInteractive(ctx, w, prompt, \"\", \"\", true, contID, useLast); err != nil {\n\tif strings.Contains(err.Error(), \"failed to create session\") {\n\t\t// retry once without continue to start a fresh session\n\t\treturn app.RunNonInteractive(ctx, w, prompt, \"\", \"\", true, \"\", false)\n\t}\n\treturn err\n}","preventionTips":["Refresh session ids from `crush sessions` instead of hardcoding them","Keep the crush cache/data directory writable and non-full","Avoid downgrading crush across DB migrations"],"tags":["go","sqlite","session","persistence"],"backgroundTag":"database-open-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}