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.
SQLite Schema
Section titled “SQLite Schema”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).
sessions
Section titled “sessions”| 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.
messages
Section titled “messages”| 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 |
Sequence Ordering Invariants
Section titled “Sequence Ordering Invariants”- Every
append_messagecall runs in a singleTransactionBehavior::Immediatetransaction that updatessessions.updated_at, computesCOALESCE(MAX(seq), -1) + 1, and inserts the row. load_messagesreturns rows ordered byseq ASC.- After a rewind deletes rows with
seq > target_seq, the nextappend_messageassignstarget_seq + 1, keepingseqcontiguous from0for the surviving branch.
Rewind Semantics
Section titled “Rewind Semantics”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 singleTransactionBehavior::Immediatetransaction: it bumpssessions.updated_at, verifies the session (SessionError::NotFound) and target message (SessionError::MessageNotFound), deletes all rows withseq > target_seq, and returns the deleted row count.SessionStore::rewind_to_user_turn(session_id, user_turn_index)runs in a singleTransactionBehavior::Immediatetransaction: it bumpssessions.updated_at, verifies the session, resolves the zero-based human user turn (SessionError::InvalidRewindTargetwhen out of bounds), deletes all rows withseq > target_seq, and returns the deleted row count.Agent::rewind_to_user_message(user_turn_index)rewinds the attachedSessionStorebefore truncatingAgent::history, providing strong exception safety so a storage error leaves in-memory history unchanged. When noSessionStoreis attached, it truncatesAgent::historydirectly.
Forking Sessions
Section titled “Forking Sessions”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 singleTransactionBehavior::Immediatetransaction, copies the parent’stitle,model, andworking_dir, recordsparent_session_idandforked_from_seq, and copies all messages withseq <= up_to_seq(or all messages whenup_to_seqisNone).SessionStore::fork_session_at_user_turn(source_id, user_turn_index)resolves the zero-based human user turn index usingis_user_turnand 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, returningOk(None)when noSessionStoreis attached.- From the CLI, pass
--fork(optionally with--session <id>) tooxorox runto fork before starting the turn. In the interactive TUI, run/forkor/fork <turn>to fork the active session in place.