{"record":{"id":"f750a09cc8aa275c","repo":"sigoden/aichat","slug":"cannot-perform-this-operation-because-the-session","errorCode":null,"errorMessage":"Cannot perform this operation because the session has messages, please `.empty session` first.","messagePattern":"Cannot perform this operation because the session has messages, please `\\.empty session` first\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/config/session.rs","lineNumber":464,"sourceCode":"            )\n        })?;\n\n        if is_repl {\n            println!(\"✓ Saved the session to '{}'.\", session_path.display());\n        }\n\n        if self.name() != session_name {\n            self.name = session_name.to_string()\n        }\n\n        self.dirty = false;\n\n        Ok(())\n    }\n\n    pub fn guard_empty(&self) -> Result<()> {\n        if !self.is_empty() {\n            bail!(\"Cannot perform this operation because the session has messages, please `.empty session` first.\");\n        }\n        Ok(())\n    }\n\n    pub fn add_message(&mut self, input: &Input, output: &str) -> Result<()> {\n        if input.continue_output().is_some() {\n            if let Some(message) = self.messages.last_mut() {\n                if let MessageContent::Text(text) = &mut message.content {\n                    *text = format!(\"{text}{output}\");\n                }\n            }\n        } else if input.regenerate() {\n            if let Some(message) = self.messages.last_mut() {\n                if let MessageContent::Text(text) = &mut message.content {\n                    *text = output.to_string();\n                }\n            }\n        } else {","sourceCodeStart":446,"sourceCodeEnd":482,"githubUrl":"https://github.com/sigoden/aichat/blob/82976d349ad97ac9aae0655ad631dace5e2a6385/src/config/session.rs#L446-L482","documentation":"`Session::guard_empty` refuses to run when the current session already contains messages. The library requires a clean session for the operation being invoked (e.g. replacing session state or starting a directive that assumes no history). Throwing this guard prevents an operation from silently mixing with stale conversation history.","triggerScenarios":"Calling any API that invokes `session.guard_empty()` while `session.is_empty()` is false — i.e. at least one input/output pair has been added via `Session::add_message`.","commonSituations":"Users pipe a prompt into a command that needs a fresh session but the loaded session file already has prior turns; a session was resumed from disk and then an operation expecting a blank session is invoked.","solutions":["Run the `.empty session` directive (or call `session.clear()`/equivalent) before the operation","Start with a new/unnamed session instead of a persisted one","Check `session.is_empty()` before performing the operation"],"exampleFix":"// before\nsession.add_message(&input, &output)?;\nsession.guard_empty()?;\n// after\nsession.clear()?; // or run `.empty session`\nsession.guard_empty()?;","handlingStrategy":"validation","validationCode":"if !session.is_empty() {\n    session.clear()?; // or run `.empty session`\n}\nsession.guard_empty()?;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call session.clear() before operations that require a fresh session","Track whether a session has been used before reusing it","Prefer spawning a new session instead of reusing persisted ones"],"tags":["session","state","cli"],"backgroundTag":"empty-required-field","analyzedSha":"82976d349ad97ac9aae0655ad631dace5e2a6385","analyzedAt":"2026-09-09T18:33:06.139Z","contentChangedAt":"2026-09-09T18:33:06.139Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}