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
impl HistoryRoot
Sourcepub fn home() -> Result<Self>
pub fn home() -> Result<Self>
Resolve the default ~/.claude/projects. Errors if $HOME
(or the platform-specific user home) cannot be determined.
Sourcepub fn at(path: impl Into<PathBuf>) -> Self
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.
Sourcepub fn list_projects(&self) -> Result<Vec<ProjectSummary>>
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.
Sourcepub fn list_projects_with(
&self,
opts: &ListOptions,
) -> Result<Vec<ProjectSummary>>
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):
- Filter out empty projects (
session_count == 0) whenopts.include_emptyisfalse. - Sort by
opts.sort(ListSort::NameAscby default,ListSort::RecencyDescfor “most recent first”). - Skip the first
opts.offsetitems. - Truncate to
opts.limititems.
Returns an empty vec if the root directory doesn’t exist.
Sourcepub fn list_sessions(&self, slug: Option<&str>) -> Result<Vec<SessionSummary>>
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.
Sourcepub fn list_sessions_with(
&self,
slug: Option<&str>,
opts: &ListOptions,
) -> Result<Vec<SessionSummary>>
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.
Sourcepub fn project_slug(path: impl AsRef<Path>) -> String
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).
Sourcepub fn sessions_for_path(
&self,
cwd: impl AsRef<Path>,
) -> Result<Vec<SessionSummary>>
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))).
Sourcepub fn sessions_for_path_with(
&self,
cwd: impl AsRef<Path>,
opts: &ListOptions,
) -> Result<Vec<SessionSummary>>
pub fn sessions_for_path_with( &self, cwd: impl AsRef<Path>, opts: &ListOptions, ) -> Result<Vec<SessionSummary>>
Self::sessions_for_path with explicit ListOptions.
Sourcepub fn read_session(&self, session_id: &str) -> Result<SessionLog>
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.
Sourcepub fn find_session(
&self,
session_id: &str,
) -> Result<Option<(PathBuf, String)>>
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
impl Clone for HistoryRoot
Source§fn clone(&self) -> HistoryRoot
fn clone(&self) -> HistoryRoot
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more