Session

Struct Session 

Source
pub struct Session {
Show 40 fields pub session_id: String, pub machine_id: String, pub framework: Option<String>, pub first_started_at: DateTime<Utc>, pub last_started_at: DateTime<Utc>, pub first_ended_at: Option<DateTime<Utc>>, pub last_ended_at: Option<DateTime<Utc>>, pub last_activity: DateTime<Utc>, pub revival_count: u32, pub status: Option<SessionStatus>, pub status_updated_at: Option<DateTime<Utc>>, pub pid: Option<i32>, pub process_start_time: Option<i64>, pub cwd: Option<String>, pub local_git_dir: Option<String>, pub git_branch: Option<String>, pub git_worktree_path: Option<String>, pub github_repo: Option<String>, pub github_issue: Option<i32>, pub github_pr: Option<i32>, pub git_additions: Option<i32>, pub git_deletions: Option<i32>, pub first_user_message: Option<String>, pub last_user_message: Option<String>, pub model: Option<String>, pub compaction_count: u32, pub message_count: u32, pub tool_call_count: u32, pub api_request_count: u32, pub api_error_count: u32, pub tokens_input: i64, pub tokens_output: i64, pub tokens_cache_read: i64, pub tokens_cache_write: i64, pub cost_usd: Option<f64>, pub context: Option<i64>, pub n_hook_events: u32, pub n_otel_events: u32, pub transcript_available: bool, pub transcript_path: Option<String>,
}
Expand description

A Claude Code session with aggregated metadata.

Sessions are derived from events but stored separately for efficient querying. All aggregate fields are updated incrementally when events are inserted.

§Schema v2

This struct implements Sessions Schema v2 which uses unified columns with explicit source tracking instead of source-specific columns (e.g., tokens_input instead of tokens_input_otel and tokens_input_transcript).

Source priority is: transcript > otel > hook

  • Transcript is preferred as the authoritative source (includes is_sidechain info)
  • OTEL is used as fallback for token/API data
  • Hook provides real-time status and prompts

§Session Revival

Sessions can be revived after ending. The schema tracks:

  • first_started_at / last_started_at: Original and most recent start times
  • first_ended_at / last_ended_at: Original and most recent end times
  • revival_count: Number of times the session was revived

Fields§

§session_id: String

Unique session identifier

§machine_id: String

Machine identifier for multi-machine sync

§framework: Option<String>

Source framework (claude_code, gemini, codex, etc.)

§first_started_at: DateTime<Utc>

Original session start time (immutable)

§last_started_at: DateTime<Utc>

Most recent start/revival time

§first_ended_at: Option<DateTime<Utc>>

First time session ended (immutable historical record)

§last_ended_at: Option<DateTime<Utc>>

Most recent end time (None = running)

§last_activity: DateTime<Utc>

Timestamp of most recent activity

§revival_count: u32

Number of times the session was revived after ending

§status: Option<SessionStatus>

Current session status (idle, busy, wait, done)

§status_updated_at: Option<DateTime<Utc>>

Timestamp when status was last updated (prevents stale updates)

§pid: Option<i32>

Process ID (NULL when process ended, can change on revival)

§process_start_time: Option<i64>

Process start time in seconds (Unix timestamp). Used for deterministic PID identity validation - (pid, start_time) is globally unique. This prevents false positives from PID reuse and handles late mi6 installation.

§cwd: Option<String>

Working directory

§local_git_dir: Option<String>

Absolute path to .git directory

§git_branch: Option<String>

Current git branch name

§git_worktree_path: Option<String>

Worktree root if in a worktree

§github_repo: Option<String>

Repository in owner/repo format (e.g., “paradigmxyz/mi6”)

§github_issue: Option<i32>

GitHub issue number from gh commands (most recent)

§github_pr: Option<i32>

GitHub PR number from gh commands (most recent)

§git_additions: Option<i32>

Lines added (from git diff vs main/master or gh pr view)

§git_deletions: Option<i32>

Lines deleted (from git diff vs main/master or gh pr view)

§first_user_message: Option<String>

First user prompt (transcript can correct hook’s initial value)

§last_user_message: Option<String>

Most recent user message text

§model: Option<String>

Model used in most recent API request

§compaction_count: u32

Number of context compactions

§message_count: u32

Total user message count

§tool_call_count: u32

Total tool call count

§api_request_count: u32

Total API request count

§api_error_count: u32

Total failed API request count

§tokens_input: i64

Total input tokens

§tokens_output: i64

Total output tokens

§tokens_cache_read: i64

Total cache read tokens

§tokens_cache_write: i64

Total cache write tokens

§cost_usd: Option<f64>

Total cost in USD

§context: Option<i64>

Current context window usage (raw token count)

§n_hook_events: u32

Cumulative count of hook events (enables priority logic)

§n_otel_events: u32

Cumulative count of OTEL events (enables priority logic)

§transcript_available: bool

Whether transcript has been successfully parsed

§transcript_path: Option<String>

Full absolute path to transcript file

Implementations§

Source§

impl Session

Source

pub fn is_active(&self) -> bool

Check if the session is still active (no end time recorded)

Source

pub fn is_process_running(&self) -> bool

Check if the session’s process is still running.

Returns true if the session has a PID and that process is still alive. Returns false if there’s no PID or the process is not running.

Source

pub fn total_tokens(&self) -> i64

Total tokens across all categories

Source

pub fn duration(&self) -> Duration

Duration of the session (from first start to last end or last activity)

Source

pub fn metrics_source(&self) -> &'static str

Determine which source currently owns the metrics.

Priority: transcript > otel > hook

Source

pub fn should_otel_update_tokens(&self) -> bool

Whether OTEL events should update token metrics.

OTEL can update tokens only when transcript is not available.

Source

pub fn should_hook_update_counts(&self) -> bool

Whether hook events should update message/tool counts.

Hooks can update counts only when transcript is not available.

Source

pub fn uses_shared_process(&self) -> bool

Check if this session uses a shared process (one process for many sessions).

For frameworks like Cursor IDE, a single long-running process hosts multiple sessions. PID-based liveness detection doesn’t work reliably for these because the process stays alive even after individual sessions end.

Use this to skip PID timestamp validation for shared-process frameworks.

Source

pub fn is_amp(&self) -> bool

Check if this is an Amp session.

Amp sessions have limited data availability compared to other frameworks because Amp doesn’t support hooks. Features like tool calls, API request counts, compactions, and OTEL events are not tracked for Amp sessions.

Trait Implementations§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Session

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Session

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Session

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Session

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Session

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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, 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,