sigoden/aichat · error

Cannot perform this operation because the session has…

Error message

Cannot perform this operation because the session has messages, please `.empty session` first.

What it means

`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.

Solutions

  1. Run the `.empty session` directive (or call `session.clear()`/equivalent) before the operation
  2. Start with a new/unnamed session instead of a persisted one
  3. Check `session.is_empty()` before performing the operation

Example fix

// before
session.add_message(&input, &output)?;
session.guard_empty()?;
// after
session.clear()?; // or run `.empty session`
session.guard_empty()?;
Defensive patterns

Strategy: validation

Validate before calling

if !session.is_empty() {
    session.clear()?; // or run `.empty session`
}
session.guard_empty()?;

Prevention

When it happens

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

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

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of sigoden/aichat@82976d349a (2026-09-09). Data as JSON: /api/errors/f750a09cc8aa275c. Report an issue: GitHub.

Appendix: source

Thrown at src/config/session.rs:464

            )
        })?;

        if is_repl {
            println!("✓ Saved the session to '{}'.", session_path.display());
        }

        if self.name() != session_name {
            self.name = session_name.to_string()
        }

        self.dirty = false;

        Ok(())
    }

    pub fn guard_empty(&self) -> Result<()> {
        if !self.is_empty() {
            bail!("Cannot perform this operation because the session has messages, please `.empty session` first.");
        }
        Ok(())
    }

    pub fn add_message(&mut self, input: &Input, output: &str) -> Result<()> {
        if input.continue_output().is_some() {
            if let Some(message) = self.messages.last_mut() {
                if let MessageContent::Text(text) = &mut message.content {
                    *text = format!("{text}{output}");
                }
            }
        } else if input.regenerate() {
            if let Some(message) = self.messages.last_mut() {
                if let MessageContent::Text(text) = &mut message.content {
                    *text = output.to_string();
                }
            }
        } else {

View on GitHub (pinned to 82976d349a)