use crate::ingest::Turn;
use serde_json::Value;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct SessionRef {
pub label: String,
pub path: PathBuf,
pub native_id: Option<String>,
pub cwd: Option<String>,
pub precomputed_fp: Option<String>,
}
impl SessionRef {
pub fn session_id(&self) -> String {
self.native_id.clone().unwrap_or_else(|| {
self.path
.file_stem()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_default()
})
}
pub fn namespaced_id(&self, source_id: &str) -> String {
let id = self.session_id();
if source_id == "claude" {
id
} else {
format!("{}:{}", source_id, id)
}
}
}
fn scan_files(dir: &Path, pred: impl Fn(&str) -> bool) -> Vec<PathBuf> {
let mut out = vec![];
let mut collect = |d: &Path| {
if let Ok(rd) = std::fs::read_dir(d) {
for entry in rd.flatten() {
let p = entry.path();
if p.is_file()
&& let Some(name) = p.file_name().and_then(|n| n.to_str())
&& pred(name)
{
out.push(p.clone());
}
}
}
};
collect(dir);
if let Ok(rd) = std::fs::read_dir(dir) {
for entry in rd.flatten() {
let p = entry.path();
if p.is_dir() {
collect(&p);
}
}
}
out
}
pub trait SessionSource {
fn id(&self) -> &'static str;
fn label(&self) -> &'static str;
fn is_available(&self) -> bool;
fn discover_all(&self) -> Vec<SessionRef>;
fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef>;
fn parse(&self, session: &SessionRef) -> Vec<Turn>;
fn fingerprint(&self, session: &SessionRef) -> Option<String> {
if let Some(fp) = &session.precomputed_fp {
return Some(fp.clone());
}
let m = std::fs::metadata(&session.path).ok()?;
let mtime = m
.modified()
.ok()?
.duration_since(std::time::UNIX_EPOCH)
.ok()?
.as_secs();
Some(format!("{}:{}", mtime, m.len()))
}
fn last_activity(&self, session: &SessionRef) -> Option<i64> {
let m = std::fs::metadata(&session.path).ok()?;
let secs = m
.modified()
.ok()?
.duration_since(std::time::UNIX_EPOCH)
.ok()?
.as_secs();
Some(secs as i64)
}
fn discover_in(&self, _dir: &Path) -> Vec<SessionRef> {
Vec::new()
}
}
pub struct ClaudeCode;
impl ClaudeCode {
fn projects_dir() -> PathBuf {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("~"))
.join(".claude")
.join("projects")
}
fn label_for(hash: &str) -> String {
let Some(stripped) = hash.strip_prefix('-') else {
return hash.to_string();
};
let parts: Vec<&str> = stripped.split('-').collect();
if parts.len() >= 2 {
format!("{}/{}", parts[parts.len() - 2], parts[parts.len() - 1])
} else {
hash.to_string()
}
}
fn read_cwd(path: &Path) -> Option<String> {
use std::io::{BufRead, BufReader};
let f = std::fs::File::open(path).ok()?;
for line in BufReader::new(f).lines().take(20).map_while(Result::ok) {
let Ok(v) = serde_json::from_str::<Value>(&line) else {
continue;
};
if let Some(cwd) = v.get("cwd").and_then(|x| x.as_str()) {
if !cwd.is_empty() {
return Some(cwd.to_string());
}
}
}
None
}
fn jsonl_files(dir: &Path) -> Vec<PathBuf> {
let mut files: Vec<PathBuf> = std::fs::read_dir(dir)
.into_iter()
.flatten()
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().map(|e| e == "jsonl").unwrap_or(false))
.collect();
files.sort_by_key(|p| p.metadata().and_then(|m| m.modified()).ok());
files
}
}
impl SessionSource for ClaudeCode {
fn id(&self) -> &'static str {
"claude"
}
fn label(&self) -> &'static str {
"Claude Code"
}
fn is_available(&self) -> bool {
Self::projects_dir().is_dir()
}
fn discover_all(&self) -> Vec<SessionRef> {
let Ok(entries) = std::fs::read_dir(Self::projects_dir()) else {
return vec![];
};
let mut per_project: Vec<(String, Vec<PathBuf>)> = entries
.flatten()
.map(|e| e.path())
.filter(|p| p.is_dir())
.filter_map(|path| {
let hash = path.file_name()?.to_string_lossy().to_string();
let files = Self::jsonl_files(&path);
(!files.is_empty()).then(|| (Self::label_for(&hash), files))
})
.collect();
per_project.sort_by(|a, b| a.0.cmp(&b.0));
per_project
.into_iter()
.flat_map(|(label, files)| {
files.into_iter().map(move |path| SessionRef {
label: label.clone(),
cwd: Self::read_cwd(&path),
path,
native_id: None, precomputed_fp: None,
})
})
.collect()
}
fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef> {
let hash = project_dir.to_string_lossy().replace('/', "-");
let dir = Self::projects_dir().join(&hash);
if !dir.exists() {
return vec![];
}
let label = Self::label_for(&hash);
let dir_cwd = project_dir.to_string_lossy().to_string();
Self::jsonl_files(&dir)
.into_iter()
.map(|path| SessionRef {
label: label.clone(),
cwd: Self::read_cwd(&path).or_else(|| Some(dir_cwd.clone())),
path,
native_id: None,
precomputed_fp: None,
})
.collect()
}
fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
scan_files(dir, |n| n.ends_with(".jsonl") && !n.starts_with("rollout-"))
.into_iter()
.map(|path| {
let label = path
.parent()
.and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| "scan".to_string());
SessionRef {
cwd: Self::read_cwd(&path),
label,
path,
native_id: None,
precomputed_fp: None,
}
})
.collect()
}
fn parse(&self, session: &SessionRef) -> Vec<Turn> {
crate::ingest::parse_turns(&session.path)
}
}
pub struct Codex;
impl Codex {
fn roots() -> Vec<PathBuf> {
let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("~"));
let codex = home.join(".codex");
vec![codex.join("sessions"), codex.join("archived_sessions")]
}
fn rollout_files(dir: &Path) -> Vec<PathBuf> {
let mut out = vec![];
let mut stack = vec![dir.to_path_buf()];
while let Some(d) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&d) else {
continue;
};
for e in entries.flatten() {
let p = e.path();
if p.is_dir() {
stack.push(p);
} else if p.extension().map(|x| x == "jsonl").unwrap_or(false)
&& p.file_name()
.map(|n| n.to_string_lossy().starts_with("rollout-"))
.unwrap_or(false)
{
out.push(p);
}
}
}
out.sort_by_key(|p| p.metadata().and_then(|m| m.modified()).ok());
out
}
fn read_meta(path: &Path) -> Option<(String, String)> {
use std::io::{BufRead, BufReader};
let f = std::fs::File::open(path).ok()?;
for line in BufReader::new(f).lines().take(10).map_while(Result::ok) {
let v: Value = match serde_json::from_str(&line) {
Ok(v) => v,
Err(_) => continue,
};
if v.get("type").and_then(|t| t.as_str()) != Some("session_meta") {
continue;
}
let p = v.get("payload")?;
let id = p
.get("id")
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string();
let cwd = p
.get("cwd")
.and_then(|x| x.as_str())
.unwrap_or("")
.to_string();
return Some((id, cwd));
}
None
}
fn label_for_cwd(cwd: &str) -> String {
let parts: Vec<&str> = cwd
.trim_end_matches('/')
.split('/')
.filter(|s| !s.is_empty())
.collect();
match parts.len() {
0 => "unknown".to_string(),
1 => parts[0].to_string(),
n => format!("{}/{}", parts[n - 2], parts[n - 1]),
}
}
}
impl SessionSource for Codex {
fn id(&self) -> &'static str {
"codex"
}
fn label(&self) -> &'static str {
"Codex"
}
fn is_available(&self) -> bool {
Self::roots().iter().any(|r| r.is_dir())
}
fn discover_all(&self) -> Vec<SessionRef> {
let mut refs: Vec<SessionRef> = Self::roots()
.iter()
.filter(|r| r.is_dir())
.flat_map(|r| Self::rollout_files(r))
.map(|path| {
let (id, cwd) = Self::read_meta(&path).unwrap_or_default();
SessionRef {
label: if cwd.is_empty() {
"unknown".to_string()
} else {
Self::label_for_cwd(&cwd)
},
cwd: (!cwd.is_empty()).then_some(cwd),
native_id: (!id.is_empty()).then_some(id),
path,
precomputed_fp: None,
}
})
.collect();
refs.sort_by(|a, b| a.label.cmp(&b.label));
refs
}
fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef> {
let want = project_dir.to_string_lossy().to_string();
let want = want.trim_end_matches('/').to_string();
self.discover_all()
.into_iter()
.filter(|r| {
r.cwd
.as_deref()
.map(|c| c.trim_end_matches('/') == want)
.unwrap_or(false)
})
.collect()
}
fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
let mut refs: Vec<SessionRef> =
scan_files(dir, |n| n.starts_with("rollout-") && n.ends_with(".jsonl"))
.into_iter()
.map(|path| {
let (id, cwd) = Self::read_meta(&path).unwrap_or_default();
SessionRef {
label: if cwd.is_empty() {
"unknown".to_string()
} else {
Self::label_for_cwd(&cwd)
},
cwd: (!cwd.is_empty()).then_some(cwd),
native_id: (!id.is_empty()).then_some(id),
path,
precomputed_fp: None,
}
})
.collect();
refs.sort_by(|a, b| a.label.cmp(&b.label));
refs
}
fn parse(&self, session: &SessionRef) -> Vec<Turn> {
crate::codex::parse_rollout(&session.path)
}
}
pub struct Gemini;
impl Gemini {
fn gemini_dir() -> PathBuf {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("~"))
.join(".gemini")
}
fn project_dirs() -> std::collections::HashMap<String, String> {
let mut out = std::collections::HashMap::new();
let path = Self::gemini_dir().join("projects.json");
if let Ok(text) = std::fs::read_to_string(&path)
&& let Ok(v) = serde_json::from_str::<Value>(&text)
&& let Some(map) = v.get("projects").and_then(|p| p.as_object())
{
for (abs, name) in map {
if let Some(name) = name.as_str() {
out.insert(name.to_string(), abs.clone());
}
}
}
out
}
fn session_files() -> Vec<(String, PathBuf)> {
let tmp = Self::gemini_dir().join("tmp");
let Ok(projects) = std::fs::read_dir(&tmp) else {
return vec![];
};
let mut out = vec![];
for proj in projects.flatten() {
let name = proj.file_name().to_string_lossy().to_string();
let chats = proj.path().join("chats");
let Ok(files) = std::fs::read_dir(&chats) else {
continue;
};
for f in files.flatten() {
let p = f.path();
let is_session = p
.file_name()
.map(|n| {
let n = n.to_string_lossy();
n.starts_with("session-") && n.ends_with(".json")
})
.unwrap_or(false);
if is_session {
out.push((name.clone(), p));
}
}
}
out.sort_by(|a, b| {
a.1.metadata()
.and_then(|m| m.modified())
.ok()
.cmp(&b.1.metadata().and_then(|m| m.modified()).ok())
});
out
}
fn read_session_id(path: &Path) -> Option<String> {
use std::io::Read;
let mut f = std::fs::File::open(path).ok()?;
let mut buf = vec![0u8; 4096];
let n = f.read(&mut buf).ok()?;
let head = String::from_utf8_lossy(&buf[..n]);
let key = "\"sessionId\"";
let i = head.find(key)? + key.len();
let rest = &head[i..];
let start = rest.find('"')? + 1;
let end = rest[start..].find('"')? + start;
Some(rest[start..end].to_string())
}
}
impl SessionSource for Gemini {
fn id(&self) -> &'static str {
"gemini"
}
fn label(&self) -> &'static str {
"Gemini"
}
fn is_available(&self) -> bool {
Self::gemini_dir().join("tmp").is_dir()
}
fn discover_all(&self) -> Vec<SessionRef> {
let dirs = Self::project_dirs();
let mut refs: Vec<SessionRef> = Self::session_files()
.into_iter()
.map(|(project, path)| {
let cwd = dirs.get(&project).cloned();
SessionRef {
label: cwd
.as_deref()
.map(Codex::label_for_cwd)
.unwrap_or_else(|| project.clone()),
cwd,
native_id: Self::read_session_id(&path),
path,
precomputed_fp: None,
}
})
.collect();
refs.sort_by(|a, b| a.label.cmp(&b.label));
refs
}
fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef> {
let want = project_dir
.to_string_lossy()
.trim_end_matches('/')
.to_string();
self.discover_all()
.into_iter()
.filter(|r| {
r.cwd
.as_deref()
.map(|c| c.trim_end_matches('/') == want)
.unwrap_or(false)
})
.collect()
}
fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
let mut refs: Vec<SessionRef> =
scan_files(dir, |n| n.starts_with("session-") && n.ends_with(".json"))
.into_iter()
.map(|path| {
let label = path
.parent()
.and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| "gemini".to_string());
SessionRef {
native_id: Self::read_session_id(&path),
label,
cwd: None,
path,
precomputed_fp: None,
}
})
.collect();
refs.sort_by(|a, b| a.label.cmp(&b.label));
refs
}
fn parse(&self, session: &SessionRef) -> Vec<Turn> {
let mut turns = crate::gemini::parse_session(&session.path);
if let Some(cwd) = &session.cwd {
for t in &mut turns {
if t.cwd.is_none() {
t.cwd = Some(cwd.clone());
}
}
}
turns
}
}
pub struct Cursor;
impl Cursor {
fn global_db() -> PathBuf {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("~"))
.join("Library/Application Support/Cursor/User/globalStorage/state.vscdb")
}
fn sessions_in_db(db: &Path) -> Vec<SessionRef> {
crate::cursor::list_sessions(db)
.into_iter()
.map(|s| SessionRef {
label: s.name.clone().unwrap_or_else(|| "Cursor".to_string()),
cwd: None, native_id: Some(s.composer_id),
path: db.to_path_buf(),
precomputed_fp: Some(format!("cursor:{}", s.last_updated_at)),
})
.collect()
}
}
impl SessionSource for Cursor {
fn id(&self) -> &'static str {
"cursor"
}
fn label(&self) -> &'static str {
"Cursor"
}
fn is_available(&self) -> bool {
Self::global_db().is_file()
}
fn discover_all(&self) -> Vec<SessionRef> {
Self::sessions_in_db(&Self::global_db())
}
fn discover_for_project(&self, _project_dir: &Path) -> Vec<SessionRef> {
vec![]
}
fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
let db = if dir.is_file() {
dir.to_path_buf()
} else {
dir.join("state.vscdb")
};
if db.is_file() {
Self::sessions_in_db(&db)
} else {
vec![]
}
}
fn parse(&self, session: &SessionRef) -> Vec<Turn> {
let Some(id) = &session.native_id else {
return vec![];
};
crate::cursor::parse_session(&session.path, id)
}
fn last_activity(&self, session: &SessionRef) -> Option<i64> {
let ms: i64 = session
.precomputed_fp
.as_ref()?
.strip_prefix("cursor:")?
.parse()
.ok()?;
(ms > 0).then_some(ms / 1000)
}
}
pub struct Generic;
#[derive(serde::Deserialize)]
struct GenericTranscript {
#[serde(default)]
turns: Vec<GenericTurn>,
}
#[derive(serde::Deserialize)]
struct GenericTurn {
#[serde(default)]
user: String,
#[serde(default)]
assistant: String,
#[serde(default)]
timestamp: String,
#[serde(default)]
model: String,
#[serde(default)]
tool_calls: Vec<String>,
}
impl SessionSource for Generic {
fn id(&self) -> &'static str {
"generic"
}
fn label(&self) -> &'static str {
"Imported"
}
fn is_available(&self) -> bool {
true
}
fn discover_all(&self) -> Vec<SessionRef> {
vec![] }
fn discover_for_project(&self, _project_dir: &Path) -> Vec<SessionRef> {
vec![]
}
fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
scan_files(dir, |n| n.ends_with(".json"))
.into_iter()
.map(|path| SessionRef {
label: "Imported".to_string(),
path,
native_id: None,
cwd: None,
precomputed_fp: None,
})
.collect()
}
fn parse(&self, session: &SessionRef) -> Vec<Turn> {
let Ok(text) = std::fs::read_to_string(&session.path) else {
return vec![];
};
let Ok(t) = serde_json::from_str::<GenericTranscript>(&text) else {
eprintln!(
" warn: {} is not a valid generic transcript",
session.path.display()
);
return vec![];
};
t.turns
.into_iter()
.map(|g| Turn {
user_text: g.user,
assistant_text: g.assistant,
tool_calls: g.tool_calls,
tool_calls_raw: vec![],
tokens: crate::ingest::TurnTokens::default(),
model: g.model,
timestamp: g.timestamp,
cwd: None,
git_branches: vec![],
git_commit: None,
})
.collect()
}
}
pub fn all_sources() -> Vec<Box<dyn SessionSource>> {
vec![
Box::new(ClaudeCode),
Box::new(Codex),
Box::new(Gemini),
Box::new(Cursor),
Box::new(Generic),
]
}
pub fn source_by_id(id: &str) -> Option<Box<dyn SessionSource>> {
all_sources().into_iter().find(|s| s.id() == id)
}
pub fn source_ref_for_path(path: &Path) -> Option<(Box<dyn SessionSource>, SessionRef)> {
let dir = path.parent()?;
let want = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
for src in all_sources() {
let hit = src
.discover_in(dir)
.into_iter()
.find(|r| std::fs::canonicalize(&r.path).unwrap_or_else(|_| r.path.clone()) == want);
if let Some(r) = hit {
return Some((src, r));
}
}
None
}
pub fn group_by_label(refs: Vec<SessionRef>) -> Vec<(String, Vec<PathBuf>)> {
let mut out: Vec<(String, Vec<PathBuf>)> = vec![];
for r in refs {
match out.last_mut() {
Some((label, files)) if *label == r.label => files.push(r.path),
_ => out.push((r.label.clone(), vec![r.path])),
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn generic_source_parses_neutral_transcript() {
let dir = std::env::temp_dir().join(format!("ctx-gen-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let p = dir.join("s.json");
std::fs::write(
&p,
r#"{"turns":[
{"user":"do X","assistant":"did X with enough text to be substantial here","model":"gpt-5","tool_calls":["Edit: a.rs"]},
{"user":"do Y","assistant":"did Y"}
]}"#,
)
.unwrap();
let sref = SessionRef {
label: "Imported".into(),
path: p,
native_id: None,
cwd: None,
precomputed_fp: None,
};
let turns = Generic.parse(&sref);
assert_eq!(turns.len(), 2);
assert_eq!(turns[0].user_text, "do X");
assert_eq!(turns[0].model, "gpt-5");
assert_eq!(turns[0].tool_calls, vec!["Edit: a.rs".to_string()]);
assert_eq!(turns[1].assistant_text, "did Y");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn source_ref_for_path_routes_codex_and_claude_by_filename() {
let dir = std::env::temp_dir().join(format!("ctx-src-test-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let codex =
dir.join("rollout-2026-06-15T17-37-13-019ecda5-753a-7e31-87d2-a78b2cb024e8.jsonl");
let claude = dir.join("abc-123.jsonl");
std::fs::write(&codex, "").unwrap();
std::fs::write(&claude, "").unwrap();
let (src, _) = source_ref_for_path(&codex).expect("codex rollout should be detected");
assert_eq!(src.id(), "codex");
let (src, _) = source_ref_for_path(&claude).expect("claude transcript should be detected");
assert_eq!(src.id(), "claude");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn label_recovers_last_two_components() {
assert_eq!(
ClaudeCode::label_for("-Users-user-Documents-Project-CTXone"),
"Project/CTXone"
);
}
#[test]
fn label_passes_through_unhashed_names() {
assert_eq!(ClaudeCode::label_for("plain"), "plain");
}
#[test]
fn session_id_falls_back_to_file_stem() {
let r = SessionRef {
label: "x/y".into(),
cwd: None,
path: PathBuf::from("/tmp/abc-123.jsonl"),
native_id: None,
precomputed_fp: None,
};
assert_eq!(r.session_id(), "abc-123");
}
#[test]
fn session_id_prefers_native_id() {
let r = SessionRef {
label: "x/y".into(),
cwd: None,
path: PathBuf::from("/tmp/state.vscdb"),
native_id: Some("composer-7".into()),
precomputed_fp: None,
};
assert_eq!(r.session_id(), "composer-7");
}
#[test]
fn claude_ids_are_not_namespaced_so_existing_rows_keep_working() {
let r = SessionRef {
label: "a/b".into(),
cwd: None,
path: PathBuf::from("/x/9117346d.jsonl"),
native_id: None,
precomputed_fp: None,
};
assert_eq!(r.namespaced_id("claude"), "9117346d");
}
#[test]
fn other_sources_are_namespaced_to_avoid_collisions() {
let r = SessionRef {
label: "a/b".into(),
cwd: None,
path: PathBuf::from("/x/rollout-abc.jsonl"),
native_id: Some("019e5540".into()),
precomputed_fp: None,
};
assert_eq!(r.namespaced_id("codex"), "codex:019e5540");
}
#[test]
fn codex_label_matches_the_claude_two_component_style() {
assert_eq!(Codex::label_for_cwd("/Users/user/Apps/Thing"), "Apps/Thing");
assert_eq!(Codex::label_for_cwd("/solo"), "solo");
assert_eq!(Codex::label_for_cwd(""), "unknown");
}
#[test]
fn group_by_label_keeps_projects_together() {
let mk = |label: &str, p: &str| SessionRef {
label: label.into(),
cwd: None,
path: PathBuf::from(p),
native_id: None,
precomputed_fp: None,
};
let grouped = group_by_label(vec![
mk("a/b", "/1.jsonl"),
mk("a/b", "/2.jsonl"),
mk("c/d", "/3.jsonl"),
]);
assert_eq!(grouped.len(), 2);
assert_eq!(grouped[0].1.len(), 2);
assert_eq!(grouped[1].0, "c/d");
}
}