use parking_lot::Mutex;
use std::collections::HashMap;
use std::time::Instant;
pub struct SessionContext {
inner: Mutex<SessionInner>,
}
struct SessionInner {
symbols: HashMap<(String, String), u32>,
files: HashMap<String, u32>,
total_tokens: u64,
started_at: Instant,
}
pub struct SessionSnapshot {
pub symbols: Vec<(String, String, u32)>, pub files: Vec<(String, u32)>, pub total_tokens: u64,
pub duration_secs: u64,
}
impl SessionContext {
pub fn new() -> Self {
Self {
inner: Mutex::new(SessionInner {
symbols: HashMap::new(),
files: HashMap::new(),
total_tokens: 0,
started_at: Instant::now(),
}),
}
}
pub fn record_symbol(&self, path: &str, name: &str, tokens: u32) {
let mut inner = self.inner.lock();
let key = (path.to_string(), name.to_string());
inner.symbols.entry(key).or_insert(0);
inner.symbols.insert((path.to_string(), name.to_string()), tokens);
inner.total_tokens += tokens as u64;
}
pub fn record_file(&self, path: &str, tokens: u32) {
let mut inner = self.inner.lock();
inner.files.insert(path.to_string(), tokens);
inner.total_tokens += tokens as u64;
}
pub fn has_symbol(&self, path: &str, name: &str) -> bool {
let inner = self.inner.lock();
inner.symbols.contains_key(&(path.to_string(), name.to_string()))
}
pub fn has_file(&self, path: &str) -> bool {
let inner = self.inner.lock();
inner.files.contains_key(path)
}
pub fn snapshot(&self) -> SessionSnapshot {
let inner = self.inner.lock();
let mut symbols: Vec<(String, String, u32)> = inner
.symbols
.iter()
.map(|((p, n), t)| (p.clone(), n.clone(), *t))
.collect();
symbols.sort_by(|a, b| b.2.cmp(&a.2));
let mut files: Vec<(String, u32)> = inner
.files
.iter()
.map(|(p, t)| (p.clone(), *t))
.collect();
files.sort_by(|a, b| b.1.cmp(&a.1));
SessionSnapshot {
symbols,
files,
total_tokens: inner.total_tokens,
duration_secs: inner.started_at.elapsed().as_secs(),
}
}
}
pub fn format_context_inventory(snap: &SessionSnapshot) -> String {
let minutes = snap.duration_secs / 60;
let total_items = snap.symbols.len() + snap.files.len();
let mut lines = vec![format!(
"Session Context ({} minutes, {} items, ~{} tokens total)",
minutes, total_items, snap.total_tokens
)];
if !snap.symbols.is_empty() {
lines.push(String::new());
lines.push(format!("Symbols loaded ({}):", snap.symbols.len()));
for (path, name, tokens) in snap.symbols.iter().take(15) {
lines.push(format!(" {name} ({path}) — ~{tokens} tokens"));
}
if snap.symbols.len() > 15 {
lines.push(format!(" ... and {} more", snap.symbols.len() - 15));
}
}
if !snap.files.is_empty() {
lines.push(String::new());
lines.push(format!("Files loaded ({}):", snap.files.len()));
for (path, tokens) in snap.files.iter().take(10) {
lines.push(format!(" {path} — ~{tokens} tokens"));
}
if snap.files.len() > 10 {
lines.push(format!(" ... and {} more", snap.files.len() - 10));
}
}
lines.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_record_and_query() {
let ctx = SessionContext::new();
assert!(!ctx.has_symbol("src/lib.rs", "main"));
ctx.record_symbol("src/lib.rs", "main", 100);
assert!(ctx.has_symbol("src/lib.rs", "main"));
assert!(!ctx.has_file("src/lib.rs"));
ctx.record_file("src/lib.rs", 500);
assert!(ctx.has_file("src/lib.rs"));
}
#[test]
fn test_snapshot() {
let ctx = SessionContext::new();
ctx.record_symbol("a.rs", "foo", 100);
ctx.record_symbol("b.rs", "bar", 200);
ctx.record_file("c.rs", 300);
let snap = ctx.snapshot();
assert_eq!(snap.symbols.len(), 2);
assert_eq!(snap.files.len(), 1);
assert_eq!(snap.total_tokens, 600);
}
#[test]
fn test_format_inventory() {
let ctx = SessionContext::new();
ctx.record_symbol("src/lib.rs", "LiveIndex", 500);
ctx.record_file("src/main.rs", 1000);
let snap = ctx.snapshot();
let output = format_context_inventory(&snap);
assert!(output.contains("LiveIndex"));
assert!(output.contains("src/main.rs"));
assert!(output.contains("1500"));
}
}