pub mod claude_code;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Source {
Tool(String),
Attachment(String),
}
impl Source {
#[must_use]
pub fn label(&self) -> &str {
match self {
Self::Tool(name) | Self::Attachment(name) => name,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoadEvent {
pub session: String,
pub ts: String,
pub source: Source,
pub path: Option<String>,
pub bytes: usize,
pub spilled: Option<usize>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Turn {
pub session: String,
pub ts: String,
pub input: Option<u64>,
pub cache_creation: Option<u64>,
pub cache_read: Option<u64>,
pub output: Option<u64>,
pub cum_content_bytes: u64,
}
impl Turn {
#[must_use]
pub fn billed_input(&self) -> Option<u64> {
let parts = [self.input, self.cache_creation, self.cache_read];
if parts.iter().all(Option::is_none) {
return None;
}
Some(
parts
.iter()
.flatten()
.fold(0u64, |a, b| a.saturating_add(*b)),
)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Compaction {
pub trigger: String,
pub pre_tokens: u64,
pub post_tokens: u64,
pub ts: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Session {
pub events: Vec<LoadEvent>,
pub turns: Vec<Turn>,
pub skipped: usize,
pub content_bytes: u64,
}
#[must_use]
pub fn growth(turns: &[Turn]) -> u64 {
let mut previous: Option<u64> = None;
let mut total = 0u64;
let windows = turns.iter().filter_map(Turn::billed_input);
for window in windows.filter(|w| *w > 0) {
let step = previous.map_or(window, |p| window.saturating_sub(p));
total = total.saturating_add(step);
previous = Some(window);
}
total
}
#[must_use]
pub fn compact_point(compactions: &[Compaction]) -> Option<u64> {
auto_compactions(compactions).map(|c| c.pre_tokens).min()
}
pub fn auto_compactions(
compactions: &[Compaction],
) -> impl Iterator<Item = &Compaction> {
compactions.iter().filter(|c| c.trigger == "auto")
}
impl Session {
#[must_use]
pub fn billed_input(&self) -> u64 {
self.turns
.iter()
.filter_map(Turn::billed_input)
.fold(0u64, u64::saturating_add)
}
#[must_use]
pub fn accounted_bytes(&self) -> usize {
self.events
.iter()
.map(|e| e.bytes)
.fold(0usize, usize::saturating_add)
}
}
#[must_use]
pub fn complete_prefix(text: &str) -> &str {
match text.rfind('\n') {
Some(i) => text.get(..i.saturating_add(1)).unwrap_or(""),
None => "",
}
}
#[must_use]
pub fn content_key(text: &str) -> String {
let prefix = complete_prefix(text);
let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
for b in prefix.as_bytes() {
hash ^= u64::from(*b);
hash = hash.wrapping_mul(0x100_0000_01b3);
}
format!("{}-{hash:016x}", prefix.len())
}
#[must_use]
pub fn window(turns: &[Turn]) -> Option<u64> {
let last = turns.last()?;
let parts = [last.cache_creation, last.cache_read, last.input];
if parts.iter().all(Option::is_none) {
return None;
}
Some(
parts
.iter()
.flatten()
.fold(0u64, |a, b| a.saturating_add(*b)),
)
}
impl Session {
#[must_use]
pub fn window(&self) -> Option<u64> {
window(&self.turns)
}
#[must_use]
pub fn accounted_tokens(&self) -> u64 {
self.events
.iter()
.map(|e| u64::try_from(e.bytes / 4).unwrap_or(u64::MAX))
.fold(0u64, u64::saturating_add)
}
#[must_use]
pub fn entered(&self) -> Option<u64> {
match (self.fresh_input(), self.cache_written()) {
(None, None) => None,
(fresh, written) => {
Some(fresh.unwrap_or(0).saturating_add(written.unwrap_or(0)))
}
}
}
#[must_use]
pub fn output_tokens(&self) -> Option<u64> {
total_of(&self.turns, |t| t.output)
}
#[must_use]
pub fn unaccounted_tokens(&self) -> Option<u64> {
Some(self.window()?.saturating_sub(self.accounted_tokens()))
}
}
fn total_of(turns: &[Turn], pick: fn(&Turn) -> Option<u64>) -> Option<u64> {
let mut seen = false;
let mut sum = 0u64;
for t in turns {
if let Some(v) = pick(t) {
seen = true;
sum = sum.saturating_add(v);
}
}
seen.then_some(sum)
}
impl Session {
#[must_use]
pub fn fresh_input(&self) -> Option<u64> {
total_of(&self.turns, |t| t.input)
}
#[must_use]
pub fn cache_written(&self) -> Option<u64> {
total_of(&self.turns, |t| t.cache_creation)
}
#[must_use]
pub fn cache_read(&self) -> Option<u64> {
total_of(&self.turns, |t| t.cache_read)
}
}
impl Session {
#[must_use]
pub fn occupancy(&self) -> Vec<u64> {
self.turns
.iter()
.filter_map(Turn::billed_input)
.filter(|w| *w > 0)
.collect()
}
#[must_use]
pub fn rate(&self, n: usize) -> Option<u64> {
let occ = self.occupancy();
let last = occ.last()?;
let first = occ.get(occ.len().checked_sub(n.checked_add(1)?)?)?;
u64::try_from(n)
.ok()
.filter(|d| *d > 0)
.map(|d| last.saturating_sub(*first).checked_div(d).unwrap_or(0))
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ColdCache {
pub turns: usize,
pub written: u64,
pub read: u64,
}
impl Session {
#[must_use]
pub fn cold_cache(&self) -> Option<ColdCache> {
let mut seen = false;
let mut out = ColdCache::default();
for t in &self.turns {
let (Some(w), Some(r)) = (t.cache_creation, t.cache_read) else {
continue;
};
seen = true;
if w > r {
out.turns = out.turns.saturating_add(1);
out.written = out.written.saturating_add(w);
out.read = out.read.saturating_add(r);
}
}
seen.then_some(out)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Fit {
pub overhead: u64,
pub scale_milli: u64,
pub fitted: usize,
pub validated: usize,
pub band_permille: u64,
}
pub const MIN_FIT_TURNS: usize = 20;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Verdict {
Fits,
Over,
TooClose,
}
impl Fit {
#[must_use]
pub fn predict(&self, content_bytes: u64) -> u64 {
let units = content_bytes.checked_div(4).unwrap_or(0);
let scaled = units
.checked_mul(self.scale_milli)
.and_then(|v| v.checked_div(1000))
.unwrap_or(u64::MAX);
self.overhead.saturating_add(scaled)
}
#[must_use]
pub fn verdict(&self, predicted: u64, window: u64) -> Verdict {
let margin = predicted
.saturating_mul(self.band_permille)
.checked_div(1000)
.unwrap_or(0);
if predicted.saturating_add(margin) <= window {
Verdict::Fits
} else if predicted.saturating_sub(margin) > window {
Verdict::Over
} else {
Verdict::TooClose
}
}
}
impl Session {
#[must_use]
pub fn fit(&self) -> Option<Fit> {
let s: Vec<(f64, f64)> = self.samples();
if s.len() < MIN_FIT_TURNS {
return None;
}
let half = s.len().checked_div(2)?;
let (train, test) = (s.get(..half)?, s.get(half..)?);
let (scale, overhead) = least_squares(train)?;
Some(Fit {
overhead: overhead.max(0.0) as u64,
scale_milli: (scale * 1000.0).max(0.0) as u64,
fitted: train.len(),
validated: test.len(),
band_permille: worst_error(test, scale, overhead),
})
}
fn samples(&self) -> Vec<(f64, f64)> {
self.turns
.iter()
.filter_map(|t| Some((t.cum_content_bytes, t.billed_input()?)))
.filter(|(_, w)| *w > 0)
.map(|(c, w)| ((c / 4) as f64, w as f64))
.collect()
}
}
fn least_squares(pts: &[(f64, f64)]) -> Option<(f64, f64)> {
let n = pts.len() as f64;
let sx: f64 = pts.iter().map(|(x, _)| x).sum();
let sy: f64 = pts.iter().map(|(_, y)| y).sum();
let sxx: f64 = pts.iter().map(|(x, _)| x * x).sum();
let sxy: f64 = pts.iter().map(|(x, y)| x * y).sum();
let denom = n * sxx - sx * sx;
if denom == 0.0 || !denom.is_finite() {
return None;
}
let slope = (n * sxy - sx * sy) / denom;
let intercept = (sy - slope * sx) / n;
(slope.is_finite() && intercept.is_finite()).then_some((slope, intercept))
}
fn worst_error(pts: &[(f64, f64)], slope: f64, intercept: f64) -> u64 {
pts.iter()
.filter(|(_, actual)| *actual > 0.0)
.map(|(x, actual)| {
let err = (intercept + slope * x - actual).abs() / actual;
(err * 1000.0) as u64
})
.max()
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
fn windows(ws: &[Option<u64>]) -> Vec<Turn> {
ws.iter()
.map(|w| Turn {
input: *w,
..Turn::default()
})
.collect()
}
#[test]
fn growth_counts_the_first_window_whole_and_then_the_deltas() {
let turns = windows(&[Some(30_000), Some(35_000), Some(50_000)]);
assert_eq!(growth(&turns), 50_000);
}
#[test]
fn a_compaction_drops_out_of_growth_rather_than_subtracting() {
let turns = windows(&[Some(900_000), Some(12_000), Some(20_000)]);
assert_eq!(growth(&turns), 908_000);
}
#[test]
fn a_zero_window_turn_is_excluded_from_every_delta() {
let turns = windows(&[Some(40_000), Some(0), Some(41_000)]);
assert_eq!(growth(&turns), 41_000);
}
#[test]
fn turns_without_usage_contribute_no_growth() {
assert_eq!(growth(&windows(&[None, None])), 0);
assert_eq!(growth(&[]), 0);
}
#[test]
fn growth_of_a_monotone_session_is_its_last_window() {
let turns = windows(&[Some(10), Some(20), Some(30), Some(31)]);
assert_eq!(growth(&turns), 31);
}
#[test]
fn entered_is_fresh_input_plus_what_was_written_to_cache() {
let session = Session {
turns: vec![Turn {
input: Some(100),
cache_creation: Some(2_000),
cache_read: Some(500_000),
..Turn::default()
}],
..Session::default()
};
assert_eq!(session.entered(), Some(2_100));
assert_eq!(session.cache_read(), Some(500_000));
}
#[test]
fn entered_is_absent_when_neither_field_was_reported() {
let session = Session {
turns: vec![Turn {
cache_read: Some(500_000),
..Turn::default()
}],
..Session::default()
};
assert_eq!(session.entered(), None);
}
#[test]
fn output_is_summed_across_turns_and_absent_when_never_reported() {
let with = Session {
turns: vec![
Turn {
output: Some(1_000),
..Turn::default()
},
Turn {
output: Some(2_500),
..Turn::default()
},
],
..Session::default()
};
assert_eq!(with.output_tokens(), Some(3_500));
assert_eq!(Session::default().output_tokens(), None);
}
}