{"record":{"id":"fadaa1ab2106fb58","repo":"charmbracelet/crush","slug":"no-sessions-found","errorCode":null,"errorMessage":"no sessions found","messagePattern":"no sessions found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/session.go","lineNumber":378,"sourceCode":"\nfunc runSessionLast(cmd *cobra.Command, _ []string) error {\n\tevent.SetNonInteractive(true)\n\n\tctx, svc, cleanup, err := sessionSetup(cmd)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer cleanup()\n\n\tevent.SessionLastShown(sessionLastJSON)\n\n\tlist, err := svc.sessions.List(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to list sessions: %w\", err)\n\t}\n\n\tif len(list) == 0 {\n\t\treturn fmt.Errorf(\"no sessions found\")\n\t}\n\n\tsess := list[0]\n\n\tmsgs, err := svc.messages.List(ctx, sess.ID)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to list messages: %w\", err)\n\t}\n\n\tmsgPtrs := messagePtrs(msgs)\n\tif sessionLastJSON {\n\t\treturn outputSessionJSON(cmd.OutOrStdout(), sess, msgPtrs)\n\t}\n\treturn outputSessionHuman(ctx, svc.cfg, sess, msgPtrs)\n}\n\nconst (\n\tsessionOutputWidth     = 80","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/session.go#L360-L396","documentation":"`crush session last` looks up the most recent session by listing all sessions from the SQLite session store and taking the first entry. When the list comes back empty it throws `no sessions found` because there is no 'last' session to display. It is not a transport or DB failure — the query succeeded but the database holds zero session rows.","triggerScenarios":"Running `crush session last` (or `crush sessions last`) in a project/data-dir where no session has ever been created, i.e. `svc.sessions.List(ctx)` returned an empty slice at internal/cmd/session.go:377. Also occurs after wiping the data directory (deleting ~/.local/share/crush or a custom --data-dir) or pointing at a fresh data-dir that has no sessions yet.","commonSituations":"New user runs `session last` before ever starting a chat; CI or scripts query `session last` against an isolated --data-dir that was never used; user removed the data directory or moved machines without migrating the DB.","solutions":["Run an interactive session (`crush`) at least once so a session row is created, then retry `crush session last`.","Use `crush sessions list` to confirm whether any sessions exist for the current data directory.","If you expected sessions, check that you are not pointing at an unexpected --data-dir; the session store is per data directory.","If scripting, treat empty history as a normal case and fall back to creating a new session instead of calling `session last`."],"exampleFix":"// before\ncmd := exec.Command(\"crush\", \"session\", \"last\")\nout, err := cmd.Output()\n// after\nlist := exec.Command(\"crush\", \"sessions\", \"list\")\nlistOut, _ := list.Output()\nif len(bytes.TrimSpace(listOut)) == 0 {\n    fmt.Println(\"no sessions yet; starting a new one\")\n    return\n}\nout, err := exec.Command(\"crush\", \"session\", \"last\").Output()","handlingStrategy":"fallback","validationCode":"// Check for existing sessions before requesting 'last'\nout, err := exec.Command(\"crush\", \"sessions\", \"list\").Output()\nif err != nil {\n    log.Fatal(err)\n}\nif len(bytes.TrimSpace(out)) == 0 || strings.Contains(string(out), \"No sessions\") {\n    fmt.Println(\"no sessions exist yet\")\n    return\n}","typeGuard":"func hasSessions(listOut []byte) bool {\n    return len(bytes.TrimSpace(listOut)) > 0 &&\n        !strings.Contains(strings.ToLower(string(listOut)), \"no sessions\")\n}","tryCatchPattern":"out, err := exec.Command(\"crush\", \"session\", \"last\").CombinedOutput()\nif err != nil {\n    if strings.Contains(string(out), \"no sessions found\") {\n        fmt.Println(\"No sessions yet — run `crush` to start one.\")\n        return\n    }\n    log.Fatalf(\"unexpected error: %v: %s\", err, out)\n}","preventionTips":["Run `crush sessions list` before `session last` in scripts.","Treat empty history as a normal state, not a failure, in automation.","Pin a dedicated --data-dir per environment so you always know which store you query.","Create a session (run `crush` once) during environment setup before scripting against it."],"tags":["sessions","cli","empty-state","sqlite"],"backgroundTag":"no-sessions-found","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}