Skip to main content

Module history

Module history 

Source
Expand description

Read-side access to the CLI’s on-disk session logs.

Read-only. Mutation goes through the CLI, via ArchiveCommand and DeleteCommand.

§On-disk layout

$CODEX_HOME/sessions/<YYYY>/<MM>/<DD>/rollout-<ISO8601>-<uuid>.jsonl

A reverted thread’s file is named rollout-<ISO8601>-<uuid>_<uuid>.jsonl: the thread id, then the id of that rollout.

The CLI compresses older sessions to .jsonl.zst, and restores the plain file when one is resumed. Either path reads whichever of the two exists.

The date partitioning is why SessionQuery::after and SessionQuery::before are cheap: they filter directories, without opening a file.

§Two envelope generations

A real machine holds sessions written by many CLI versions, and the line format changed. Both were found on one machine while writing this, 205 files spanning both:

Modern, from around 0.47 onward. Every line is an envelope:

{"timestamp":"...","type":"session_meta","payload":{"id":"...","cwd":"..."}}
{"timestamp":"...","type":"response_item","payload":{"type":"message","role":"user"}}

Legacy, older files. No envelope at all: the first line is the metadata, and later lines are bare records.

{"id":"...","timestamp":"...","git":{},"instructions":null}
{"id":"...","type":"message","role":"user","content":[]}

A parser written against only the modern shape returns nothing at all for the older half of a real history, silently. SessionEntry::entry_type is None for a legacy line, and its payload is the whole line.

Field-level drift is handled the same way: every metadata field is optional, because older files carry no cli_version, no cwd, and a different instructions key. SessionMeta::raw keeps whatever this crate does not name.

§Coverage of current history modes

CLI 0.163.0 writes materialized, persistent legacy and paginated threads under the layout above. Both use the modern envelope; the current history_mode: "legacy" setting is distinct from the older bare-line format. SessionMeta::raw preserves history_mode. Paginated item_completed payloads are retained as written, including their nested item.

This is a file reader, not the provider’s logical thread-history view. Discovery does not include archived sessions, ephemeral or unmaterialized threads, or a server’s other storage roots. It does not query the state database. Entries are not normalized into turns, replayed, or deduplicated. Envelope fields other than timestamp, type, and payload (including paginated ordinal) are not exposed. Invalid JSON lines are skipped.

For the provider’s view, enable app-server and use AppServerHandle::request with thread/read, thread/turns/list, or thread/items/list. In an isolated 0.163.0 capture, thread/read and cursor-paginated thread/turns/list worked for both materialized modes. thread/items/list worked for paginated history and rejected legacy history. Continue each method’s opaque nextCursor until absent; do not derive a cursor from rollout ordinals or treat one page as complete history. Availability depends on CLI version, mode, and materialization. These probes used failed offline turns and do not qualify every item kind or history recovery scenario. Versioned fixture provenance ships in tests/history/.

§Following a live session

read_from continues from a Position, and SessionEntry::record gives typed access to the entries it returns.

use codex_wrapper::history::{self, Continuation, Position, SessionRecord};

match history::read_from(path, &position)? {
    Continuation::Continued { entries, position: next } => {
        for entry in &entries {
            if let Some(SessionRecord::Prompt { text, .. }) = entry.record() {
                println!("{text}");
            }
        }
        position = next;
    }
    // Not the file that was being followed, so start again.
    Continuation::Truncated | Continuation::Replaced => position = Position::default(),
}

To attach to a long session, start at Position::before_end and use read_chunk, which can limit a read and gives the offset of each entry.

§Example

use codex_wrapper::history::{self, SessionQuery};

for session in history::list(&SessionQuery::new().after(2026, 8, 1))? {
    let log = history::read(&session.path)?;
    println!("{} in {:?}", session.id, log.meta.and_then(|m| m.cwd));
}

Structs§

ChunkEntry
An entry and its place in the log.
GitMeta
Git metadata recorded with a session.
Position
How far a session log has been read. The default is the start.
SessionEntry
One line of a session log.
SessionFile
A rollout file on disk, identified without opening it.
SessionLog
A parsed session log.
SessionMeta
The session’s opening metadata.
SessionQuery
Which sessions to list.

Enums§

Chunk
What read_chunk found.
Continuation
What read_from found.
SessionRecord
What a SessionEntry means.

Functions§

list
List sessions for the current environment, newest first.
list_in
list, but against an explicit CODEX_HOME.
read
Read and parse one session log.
read_chunk
read_from, with the offset of each entry and an optional limit.
read_from
Read the entries written to a session log since position.