use serde::{Deserialize, Serialize};
use std::ffi::OsStr;
use std::fs::OpenOptions;
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
pub fn default_path() -> Option<PathBuf> {
path_from(
std::env::var_os("BLYTE_USAGE_LOG").as_deref(),
std::env::var_os("XDG_STATE_HOME").as_deref(),
std::env::var_os("HOME").as_deref(),
)
}
pub fn path_from(
log: Option<&OsStr>,
xdg: Option<&OsStr>,
home: Option<&OsStr>,
) -> Option<PathBuf> {
if let Some(log) = log.filter(|s| !s.is_empty()) {
if log == "off" {
return None;
}
return Some(PathBuf::from(log));
}
if let Some(xdg) = xdg.filter(|s| !s.is_empty()) {
return Some(Path::new(xdg).join("blyte").join("usage.jsonl"));
}
if let Some(home) = home.filter(|s| !s.is_empty()) {
return Some(
Path::new(home)
.join(".local")
.join("state")
.join("blyte")
.join("usage.jsonl"),
);
}
None
}
pub fn session_from(blyte: Option<String>, claude: Option<String>) -> Option<String> {
blyte
.filter(|s| !s.trim().is_empty())
.or_else(|| claude.filter(|s| !s.trim().is_empty()))
}
pub const SURFACE_ID_ENV: &str = "BLYTE_SURFACE_ID";
pub const SURFACE_NAME_ENV: &str = "BLYTE_SURFACE_NAME";
pub fn surface_from(id: Option<String>, name: Option<String>) -> (Option<String>, Option<String>) {
let Some(id) = id.filter(|s| !s.trim().is_empty()) else {
return (None, None);
};
let name = name.filter(|s| !s.trim().is_empty());
(Some(id), name)
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Entry {
pub ts: u64,
pub repo: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub session: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub surface: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub surface_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dispatch: Option<String>,
pub research: bool,
pub model: String,
pub outcome: String,
pub turns: u32,
pub input: u64,
pub cached: u64,
pub output: u64,
pub thinking: u64,
#[serde(default)]
pub cost_usd: Option<f64>,
}
pub fn repo_of(root: &Path) -> String {
let mut prefix = PathBuf::new();
let mut found = false;
for component in root.components() {
if component.as_os_str() == ".wt" {
found = true;
break;
}
prefix.push(component);
}
if found {
prefix.to_string_lossy().into_owned()
} else {
root.to_string_lossy().into_owned()
}
}
pub fn append(path: &Path, entry: &Entry) -> std::io::Result<()> {
if let Some(parent) = path.parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
let mut file = OpenOptions::new().create(true).append(true).open(path)?;
let mut line = serde_json::to_vec(entry)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
line.push(b'\n');
file.write_all(&line)?;
Ok(())
}
pub fn read(path: &Path) -> Vec<Entry> {
let Ok(file) = std::fs::File::open(path) else {
return Vec::new();
};
let reader = BufReader::new(file);
let mut entries = Vec::new();
for line in reader.lines() {
let Ok(line) = line else {
continue;
};
let trimmed = line.trim();
if trimmed.is_empty() {
continue;
}
if let Ok(entry) = serde_json::from_str::<Entry>(trimmed) {
entries.push(entry);
}
}
entries
}
#[cfg(test)]
pub(crate) static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsStr;
#[test]
fn test_path_from() {
assert_eq!(
path_from(
Some(OsStr::new("/x/u.jsonl")),
Some(OsStr::new("/xdg")),
Some(OsStr::new("/home")),
),
Some(PathBuf::from("/x/u.jsonl"))
);
assert_eq!(
path_from(
Some(OsStr::new("off")),
Some(OsStr::new("/xdg")),
Some(OsStr::new("/home")),
),
None
);
assert_eq!(
path_from(None, Some(OsStr::new("/xdg")), Some(OsStr::new("/home"))),
Some(PathBuf::from("/xdg/blyte/usage.jsonl"))
);
assert_eq!(
path_from(None, None, Some(OsStr::new("/home"))),
Some(PathBuf::from("/home/.local/state/blyte/usage.jsonl"))
);
assert_eq!(
path_from(
Some(OsStr::new("")),
Some(OsStr::new("")),
Some(OsStr::new("/home")),
),
Some(PathBuf::from("/home/.local/state/blyte/usage.jsonl"))
);
assert_eq!(
path_from(
Some(OsStr::new("")),
Some(OsStr::new("/xdg")),
Some(OsStr::new("")),
),
Some(PathBuf::from("/xdg/blyte/usage.jsonl"))
);
assert_eq!(
path_from(
Some(OsStr::new("")),
Some(OsStr::new("")),
Some(OsStr::new("")),
),
None
);
assert_eq!(path_from(None, None, None), None);
}
#[test]
fn test_repo_of() {
assert_eq!(repo_of(Path::new("/r/.wt/T1")), "/r");
assert_eq!(repo_of(Path::new("/r")), "/r");
assert_eq!(repo_of(Path::new("/r/.wtx/a")), "/r/.wtx/a");
}
#[test]
fn test_append_twice_and_read() {
let tmp = tempfile::tempdir().unwrap();
let log_path = tmp.path().join("parent").join("sub").join("usage.jsonl");
let entry1 = Entry {
ts: 1700000000,
repo: "/repo/one".into(),
session: Some("sess-1".into()),
surface: None,
surface_name: None,
dispatch: Some("disp-1".into()),
research: false,
model: "claude-3-7-sonnet".into(),
outcome: "verified".into(),
turns: 3,
input: 1000,
cached: 200,
output: 300,
thinking: 50,
cost_usd: Some(0.015),
};
let entry2 = Entry {
ts: 1700000010,
repo: "/repo/two".into(),
session: None,
surface: None,
surface_name: None,
dispatch: None,
research: true,
model: "gemini-3.8-flash".into(),
outcome: "budgetexhausted".into(),
turns: 15,
input: 5000,
cached: 1000,
output: 800,
thinking: 0,
cost_usd: None,
};
append(&log_path, &entry1).expect("first append should succeed");
{
let mut f = OpenOptions::new()
.append(true)
.open(&log_path)
.expect("open log file to inject garbage");
f.write_all(b"not valid json here at all\n")
.expect("write garbage");
}
append(&log_path, &entry2).expect("second append should succeed");
let entries = read(&log_path);
assert_eq!(entries.len(), 2);
assert_eq!(entries[0], entry1);
assert_eq!(entries[1], entry2);
}
#[test]
fn test_session_none_serialization() {
let entry = Entry {
ts: 1700000000,
repo: "/r".into(),
session: None,
surface: None,
surface_name: None,
dispatch: None,
research: false,
model: "m".into(),
outcome: "verified".into(),
turns: 1,
input: 10,
cached: 0,
output: 5,
thinking: 0,
cost_usd: None,
};
let json_str = serde_json::to_string(&entry).unwrap();
assert!(
!json_str.contains("\"session\""),
"session key must be omitted when None"
);
assert!(
!json_str.contains("\"dispatch\""),
"dispatch key must be omitted when None"
);
let parsed: Entry = serde_json::from_str(&json_str).unwrap();
assert_eq!(parsed, entry);
}
#[test]
fn test_session_from() {
assert_eq!(
session_from(Some("b".into()), Some("c".into())),
Some("b".into())
);
assert_eq!(session_from(None, Some("c".into())), Some("c".into()));
assert_eq!(
session_from(Some("".into()), Some("c".into())),
Some("c".into())
);
assert_eq!(session_from(Some(" ".into()), None), None);
assert_eq!(session_from(None, None), None);
}
#[test]
fn the_ledger_is_off_under_cargo_test() {
let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
assert_eq!(default_path(), None);
}
}