Skip to content

Sessions and Rewind

ox-core persists conversations in a SQLite database managed by SessionStore and exposes frontend-agnostic rewind primitives on Agent. For mid-stream steering persistence, see Mid-Stream Steering.

SessionStore::open enables PRAGMA journal_mode = WAL, PRAGMA foreign_keys = ON, and a 5-second busy timeout, then applies idempotent migrations tracked by PRAGMA user_version (currently 1).

Column Type Description
id TEXT PK Unique session identifier
title TEXT Human-readable session title
model TEXT Model alias or identifier
working_dir TEXT Working directory where the session was opened
parent_session_id TEXT Parent session identifier when forked (ON DELETE SET NULL)
forked_from_seq INTEGER Inclusive sequence number in the parent session at the fork point
created_at INTEGER Creation timestamp in Unix epoch milliseconds
updated_at INTEGER Last modification timestamp in milliseconds

An index idx_sessions_updated_at ON sessions (updated_at DESC, id ASC) backs SessionStore::list_sessions.

Column Type Description
id INTEGER PK Auto-incrementing row identifier
session_id TEXT Owning session (REFERENCES sessions(id) ON DELETE CASCADE)
seq INTEGER Zero-based sequence number within the session
role TEXT "user" or "assistant"
content TEXT JSON-encoded Vec<ContentBlock>
created_at INTEGER Insertion timestamp in Unix epoch milliseconds

UNIQUE (session_id, seq) enforces a single ordered message per sequence slot within each session.

Column Type Description
sha256 TEXT PK Lowercase hex SHA-256 digest of raw blob bytes
media_type TEXT MIME media type of the stored attachment
size_bytes INTEGER Raw byte length of the stored attachment
data BLOB Raw attachment bytes
created_at INTEGER Insertion timestamp in Unix epoch milliseconds
  • Every append_message call runs in a single TransactionBehavior::Immediate transaction that updates sessions.updated_at, computes COALESCE(MAX(seq), -1) + 1, and inserts the row.
  • load_messages returns rows ordered by seq ASC.
  • After a rewind deletes rows with seq > target_seq, the next append_message assigns target_seq + 1, keeping seq contiguous from 0 for the surviving branch.

is_user_turn(&Message) returns true only when role == Role::User and content contains at least one non-ContentBlock::ToolResult block. Tool-result feedback messages are therefore excluded when indexing user turns.

  • SessionStore::rewind_to_message(session_id, message_id) runs in a single TransactionBehavior::Immediate transaction: it bumps sessions.updated_at, verifies the session (SessionError::NotFound) and target message (SessionError::MessageNotFound), deletes all rows with seq > target_seq, and returns the deleted row count.
  • SessionStore::rewind_to_user_turn(session_id, user_turn_index) runs in a single TransactionBehavior::Immediate transaction: it bumps sessions.updated_at, verifies the session, resolves the zero-based human user turn (SessionError::InvalidRewindTarget when out of bounds), deletes all rows with seq > target_seq, and returns the deleted row count.
  • Agent::rewind_to_user_message(user_turn_index) rewinds the attached SessionStore before truncating Agent::history, providing strong exception safety so a storage error leaves in-memory history unchanged. When no SessionStore is attached, it truncates Agent::history directly.

Forking creates a new independent session from an existing parent session without modifying the parent or duplicating content-addressed blobs.

  • SessionStore::fork_session(source_id, up_to_seq) runs in a single TransactionBehavior::Immediate transaction, copies the parent’s title, model, and working_dir, records parent_session_id and forked_from_seq, and copies all messages with seq <= up_to_seq (or all messages when up_to_seq is None).
  • SessionStore::fork_session_at_user_turn(source_id, user_turn_index) resolves the zero-based human user turn index using is_user_turn and forks through that turn’s sequence number.
  • Agent::fork_session(user_turn_index) forks the attached session and switches the active session identifier to the new fork, returning Ok(None) when no SessionStore is attached.
  • From the CLI, pass --fork (optionally with --session <id>) to ox or ox run to fork before starting the turn. In the interactive TUI, run /fork or /fork <turn> to fork the active session in place.