Skip to main content

HistoryRoot

Struct HistoryRoot 

Source
pub struct HistoryRoot { /* private fields */ }
Expand description

Root directory of Claude Code’s on-disk history. Defaults to ~/.claude/projects; override with HistoryRoot::at for tests or non-default installs.

Implementations§

Source§

impl HistoryRoot

Source

pub fn home() -> Result<Self>

Resolve the default ~/.claude/projects. Errors if $HOME (or the platform-specific user home) cannot be determined.

Source

pub fn at(path: impl Into<PathBuf>) -> Self

Use a specific path as the projects root. Useful for tests (point at a tempdir) and for non-default installs.

Source

pub fn path(&self) -> &Path

The configured root directory.

Source

pub fn list_projects(&self) -> Result<Vec<ProjectSummary>>

List every project directory at the root, sorted by slug.

Convenience wrapper around Self::list_projects_with with ListOptions::default (no limit, no offset, name-ascending sort, includes empty projects). Existing callers keep their behavior; new callers wanting pagination or recency sort should use Self::list_projects_with.

Returns an empty vec if the root directory doesn’t exist.

Source

pub fn list_projects_with( &self, opts: &ListOptions, ) -> Result<Vec<ProjectSummary>>

List project directories with filter / sort / pagination.

Reads every direct child directory of the root, summarizes each, then applies (in order):

  1. Filter out empty projects (session_count == 0) when opts.include_empty is false.
  2. Sort by opts.sort (ListSort::NameAsc by default, ListSort::RecencyDesc for “most recent first”).
  3. Skip the first opts.offset items.
  4. Truncate to opts.limit items.

Returns an empty vec if the root directory doesn’t exist.

Source

pub fn list_sessions(&self, slug: Option<&str>) -> Result<Vec<SessionSummary>>

List sessions, optionally filtered to one project’s slug, sorted by session id.

Convenience wrapper around Self::list_sessions_with with ListOptions::default.

Source

pub fn list_sessions_with( &self, slug: Option<&str>, opts: &ListOptions, ) -> Result<Vec<SessionSummary>>

List sessions with filter / sort / pagination.

When slug is Some, only that project is walked. When None, every project directory is unioned. The options pipeline is the same as Self::list_projects_with: filter empty (message_count == 0) sessions unless opts.include_empty, sort, then offset + limit.

Source

pub fn project_slug(path: impl AsRef<Path>) -> String

Derive claude’s project-directory slug for a filesystem path, matching the CLI exactly: the path is canonicalized (resolving symlinks – e.g. /var -> /private/var on macOS, /tmp on Linux) and then every / and . is encoded as -.

This is the forward complement of ProjectSummary::decoded_path and the reliable way to locate the project directory for a working directory – see Self::sessions_for_path. Without the canonicalization and the .-encoding, a cwd under a symlinked root, or containing a . in a path segment, derives a slug that doesn’t match what claude wrote, so enumeration finds nothing.

Falls back to the path as given when it cannot be canonicalized (e.g. it does not exist on disk).

Source

pub fn sessions_for_path( &self, cwd: impl AsRef<Path>, ) -> Result<Vec<SessionSummary>>

List sessions for a specific working directory, deriving its project slug via Self::project_slug.

This is the current-project enumeration entry point: it canonicalizes and encodes the cwd exactly as claude does, so sessions written from symlinked roots (/tmp, /var) or dotted path segments are found. Convenience over list_sessions(Some(&HistoryRoot::project_slug(cwd))).

Source

pub fn sessions_for_path_with( &self, cwd: impl AsRef<Path>, opts: &ListOptions, ) -> Result<Vec<SessionSummary>>

Source

pub fn read_session(&self, session_id: &str) -> Result<SessionLog>

Read one session’s full entry log.

Walks every project directory looking for <session_id>.jsonl. Errors with Error::History if no session file matches. Malformed lines are skipped with a tracing warning.

Source

pub fn find_session( &self, session_id: &str, ) -> Result<Option<(PathBuf, String)>>

Locate the on-disk path for a session id, plus its project slug. Returns Ok(None) if no such session exists. Useful when a caller wants to read with non-default semantics (streaming, tailing, etc.) without going through Self::read_session.

Trait Implementations§

Source§

impl Clone for HistoryRoot

Source§

fn clone(&self) -> HistoryRoot

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HistoryRoot

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more