use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use serde_json::Value;
use super::{Scope, claude_project_dir, estimate_tokens};
const LONGEST_LINE: usize = 8 * 1024 * 1024;
pub fn transcript_path(claude: &Path, project: &Path, session: &str) -> PathBuf {
claude_project_dir(claude, project).join(format!("{session}.jsonl"))
}
#[derive(Clone, Debug, PartialEq)]
pub struct Loaded {
pub path: PathBuf,
pub scope: Scope,
pub text: String,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FileRead {
pub path: PathBuf,
pub reads: usize,
pub searches: usize,
pub edits: usize,
pub tokens: usize,
pub turns: Vec<usize>,
pub last_read: Option<u64>,
pub last_edit: Option<u64>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Search {
pub tool: String,
pub pattern: String,
pub path: Option<String>,
pub tokens: usize,
pub turn: usize,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Breakdown {
pub system: usize,
pub tools: usize,
pub instructions: usize,
pub listings: usize,
pub mcp: usize,
pub files: usize,
pub output: usize,
pub messages: usize,
pub total: usize,
}
impl Breakdown {
pub fn before(&self) -> usize {
self.system + self.tools + self.instructions + self.listings + self.mcp
}
pub fn conversation(&self) -> usize {
self.files + self.output + self.messages
}
}
#[derive(Clone, Debug, Default)]
pub struct Session {
pub loaded: Vec<Loaded>,
pub skills: String,
pub subagents: Vec<String>,
pub mcp: Vec<(String, String)>,
pub invoked: Vec<String>,
pub files: Vec<FileRead>,
pub searches: Vec<Search>,
pub turns: usize,
pub model: Option<String>,
pub breakdown: Breakdown,
pub compactions: usize,
}
impl Session {
pub fn window(&self) -> usize {
let long = self
.model
.as_ref()
.is_some_and(|m| m.contains("[1m]") || m.ends_with("-1m"));
if long || self.breakdown.total > 200_000 {
1_000_000
} else {
200_000
}
}
pub fn stale(&self) -> Vec<&FileRead> {
self.files
.iter()
.filter(|f| {
let Some(read) = f.last_read else {
return false;
};
let own = f.last_edit.is_some_and(|e| e >= read);
let modified = std::fs::metadata(&f.path).and_then(|m| m.modified()).ok();
let modified = modified.and_then(|m| m.duration_since(std::time::UNIX_EPOCH).ok());
!own && modified.is_some_and(|m| m.as_secs() > read + 1)
})
.collect()
}
}
pub(crate) fn unix_seconds(stamp: &str) -> Option<u64> {
let (date, time) = stamp.split_once('T')?;
let mut d = date.split('-').map(|p| p.parse::<i64>().ok());
let (y, m, day) = (d.next()??, d.next()??, d.next()??);
let time = time.trim_end_matches('Z');
let time = time.split(['+', '-']).next()?;
let mut t = time.split(':');
let h: i64 = t.next()?.parse().ok()?;
let min: i64 = t.next()?.parse().ok()?;
let s: f64 = t.next().unwrap_or("0").parse().ok()?;
let y = if m <= 2 { y - 1 } else { y };
let era = y.div_euclid(400);
let yoe = y - era * 400;
let mp = (m + 9) % 12;
let doy = (153 * mp + 2) / 5 + day - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
let days = era * 146_097 + doe - 719_468;
u64::try_from(days * 86_400 + h * 3600 + min * 60 + s as i64).ok()
}
fn text_of(content: &Value) -> String {
match content {
Value::String(s) => s.clone(),
Value::Array(parts) => parts
.iter()
.filter_map(|p| p["text"].as_str().or_else(|| p.as_str()))
.collect::<Vec<_>>()
.join("\n"),
_ => String::new(),
}
}
pub fn read_session(path: &Path) -> std::io::Result<Session> {
let reader = BufReader::new(File::open(path)?);
let mut session = Session::default();
let mut index: HashMap<PathBuf, usize> = HashMap::new();
let mut pending: HashMap<String, (String, Option<usize>, Option<usize>)> = HashMap::new();
let mut since_compact_files = 0;
let mut since_compact_output = 0;
let mut usage_total = 0;
for line in reader.split(b'\n') {
let Ok(line) = line else { break };
if line.len() > LONGEST_LINE {
continue;
}
let Ok(record) = serde_json::from_slice::<Value>(&line) else {
continue;
};
if record["isSidechain"].as_bool() == Some(true) {
continue;
}
let when = record["timestamp"].as_str().and_then(unix_seconds);
match record["type"].as_str() {
Some("attachment") => attachment(&record["attachment"], &mut session),
Some("api-request-shape") => {
let shape = &record["shape"];
session.breakdown.system = estimate_tokens(&text_of(&shape["system"]));
session.breakdown.tools = estimate_tokens(&shape["tools"].to_string());
}
Some("system") if record["subtype"] == "compact_boundary" => {
session.compactions += 1;
since_compact_files = 0;
since_compact_output = 0;
}
Some("assistant") => {
let message = &record["message"];
if let Some(model) = message["model"].as_str().filter(|m| !m.starts_with('<')) {
session.model = Some(model.to_string());
}
let usage = &message["usage"];
let total: u64 = [
"input_tokens",
"cache_creation_input_tokens",
"cache_read_input_tokens",
]
.iter()
.filter_map(|k| usage[*k].as_u64())
.sum();
if total > 0 {
usage_total = total as usize;
}
for block in message["content"].as_array().into_iter().flatten() {
if block["type"] == "tool_use" {
let id = block["id"].as_str().unwrap_or("").to_string();
let call = tool_call(block, &mut session, &mut index, when);
pending.insert(id, call);
}
}
}
Some("user") => {
let content = &record["message"]["content"];
let prompt = match content {
Value::String(_) => true,
Value::Array(parts) => parts.iter().any(|p| p["type"] == "text"),
_ => false,
};
if prompt && record["isMeta"].as_bool() != Some(true) {
session.turns += 1;
}
for part in content.as_array().into_iter().flatten() {
if part["type"] != "tool_result" {
continue;
}
let tokens = estimate_tokens(&text_of(&part["content"]));
let id = part["tool_use_id"].as_str().unwrap_or("");
match pending.remove(id) {
Some((tool, Some(file), _)) if tool == "Read" => {
session.files[file].tokens += tokens;
since_compact_files += tokens;
}
Some((_, _, Some(search))) => {
session.searches[search].tokens += tokens;
since_compact_output += tokens;
}
_ => since_compact_output += tokens,
}
}
}
_ => {}
}
}
let b = &mut session.breakdown;
b.instructions = session
.loaded
.iter()
.map(|l| estimate_tokens(&l.text))
.sum();
b.listings = estimate_tokens(&session.skills)
+ session
.subagents
.iter()
.map(|s| estimate_tokens(s))
.sum::<usize>();
b.mcp = session.mcp.iter().map(|(_, t)| estimate_tokens(t)).sum();
b.total = usage_total;
b.files = since_compact_files;
b.output = since_compact_output;
let known = b.before() + b.files + b.output;
b.messages = usage_total.saturating_sub(known);
session
.files
.sort_by(|a, b| b.tokens.cmp(&a.tokens).then(a.path.cmp(&b.path)));
Ok(session)
}
fn attachment(a: &Value, session: &mut Session) {
match a["type"].as_str() {
Some("instructions") => {
for file in a["files"].as_array().into_iter().flatten() {
let Some(path) = file["path"].as_str() else {
continue;
};
let loaded = Loaded {
path: PathBuf::from(path),
scope: Scope::from_claude(file["type"].as_str().unwrap_or("")),
text: file["content"].as_str().unwrap_or("").to_string(),
};
match session.loaded.iter_mut().find(|l| l.path == loaded.path) {
Some(known) => *known = loaded,
None => session.loaded.push(loaded),
}
}
}
Some("skill_listing") => session.skills = a["content"].as_str().unwrap_or("").to_string(),
Some("agent_listing_delta") => {
for line in a["addedLines"]
.as_array()
.into_iter()
.flatten()
.filter_map(Value::as_str)
{
if !session.subagents.iter().any(|s| s == line) {
session.subagents.push(line.to_string());
}
}
}
Some("mcp_instructions_delta") => {
let names = a["addedNames"]
.as_array()
.into_iter()
.flatten()
.filter_map(Value::as_str);
let blocks = a["addedBlocks"]
.as_array()
.into_iter()
.flatten()
.filter_map(Value::as_str);
for (name, block) in names.zip(blocks) {
session.mcp.retain(|(n, _)| n != name);
session.mcp.push((name.to_string(), block.to_string()));
}
}
Some("invoked_skills") => {
for skill in a["skills"].as_array().into_iter().flatten() {
if let Some(name) = skill["name"].as_str()
&& !session.invoked.iter().any(|n| n == name)
{
session.invoked.push(name.to_string());
}
}
}
_ => {}
}
}
fn tool_call(
block: &Value,
session: &mut Session,
index: &mut HashMap<PathBuf, usize>,
when: Option<u64>,
) -> (String, Option<usize>, Option<usize>) {
let tool = block["name"].as_str().unwrap_or("").to_string();
let input = &block["input"];
let turn = session.turns.max(1);
let mut file_at = |path: &str, session: &mut Session| -> usize {
let path = PathBuf::from(path);
*index.entry(path.clone()).or_insert_with(|| {
session.files.push(FileRead {
path,
..FileRead::default()
});
session.files.len() - 1
})
};
match tool.as_str() {
"Read" | "NotebookRead" => {
let path = input["file_path"]
.as_str()
.or_else(|| input["notebook_path"].as_str());
let Some(path) = path else {
return (tool, None, None);
};
let i = file_at(path, session);
let file = &mut session.files[i];
file.reads += 1;
file.last_read = when.or(file.last_read);
if !file.turns.contains(&turn) {
file.turns.push(turn);
}
(tool, Some(i), None)
}
"Edit" | "Write" | "MultiEdit" | "NotebookEdit" => {
let path = input["file_path"]
.as_str()
.or_else(|| input["notebook_path"].as_str());
if let Some(path) = path {
let i = file_at(path, session);
session.files[i].edits += 1;
session.files[i].last_edit = when.or(session.files[i].last_edit);
}
(tool, None, None)
}
"Grep" | "Glob" => {
let pattern = input["pattern"].as_str().unwrap_or("").to_string();
let path = input["path"].as_str().map(str::to_string);
if let Some(p) = &path
&& tool == "Grep"
&& Path::new(p).extension().is_some()
{
let i = file_at(p, session);
session.files[i].searches += 1;
}
session.searches.push(Search {
tool: tool.clone(),
pattern,
path,
tokens: 0,
turn,
});
let at = session.searches.len() - 1;
(tool, None, Some(at))
}
_ => (tool, None, None),
}
}