diff --git a/crates/goose-server/src/routes/session.rs b/crates/goose-server/src/routes/session.rs index 8ed509e46f3d..cafc23d496b8 100644 --- a/crates/goose-server/src/routes/session.rs +++ b/crates/goose-server/src/routes/session.rs @@ -187,14 +187,20 @@ async fn get_session_insights( // Track tokens - only add positive values to prevent negative totals if let Some(tokens) = session.metadata.accumulated_total_tokens { - if tokens > 0 { - total_tokens += tokens as i64; - } else if tokens < 0 { - // Log negative token values for debugging - info!( - "Warning: Session {} has negative accumulated_total_tokens: {}", - session.id, tokens - ); + match tokens.cmp(&0) { + std::cmp::Ordering::Greater => { + total_tokens += tokens as i64; + } + std::cmp::Ordering::Less => { + // Log negative token values for debugging + info!( + "Warning: Session {} has negative accumulated_total_tokens: {}", + session.id, tokens + ); + } + std::cmp::Ordering::Equal => { + // Zero tokens, no action needed + } } } diff --git a/crates/goose/src/session/info.rs b/crates/goose/src/session/info.rs index a772af05424e..6c60d3310dba 100644 --- a/crates/goose/src/session/info.rs +++ b/crates/goose/src/session/info.rs @@ -26,29 +26,56 @@ pub fn get_valid_sorted_sessions(sort_order: SortOrder) -> Result = sessions - .into_iter() - .filter_map(|(id, path)| { - let modified = path - .metadata() - .and_then(|m| m.modified()) - .map(|time| { - chrono::DateTime::::from(time) - .format("%Y-%m-%d %H:%M:%S UTC") - .to_string() - }) - .ok()?; - - let metadata = session::read_metadata(&path).ok()?; - - Some(SessionInfo { - id, - path: path.to_string_lossy().to_string(), - modified, - metadata, + + let mut session_infos: Vec = Vec::new(); + let mut corrupted_count = 0; + + for (id, path) in sessions { + // Get file modification time with fallback + let modified = path + .metadata() + .and_then(|m| m.modified()) + .map(|time| { + chrono::DateTime::::from(time) + .format("%Y-%m-%d %H:%M:%S UTC") + .to_string() }) - }) - .collect(); + .unwrap_or_else(|_| { + tracing::warn!("Failed to get modification time for session: {}", id); + "Unknown".to_string() + }); + + // Try to read metadata with error handling + match session::read_metadata(&path) { + Ok(metadata) => { + session_infos.push(SessionInfo { + id, + path: path.to_string_lossy().to_string(), + modified, + metadata, + }); + } + Err(e) => { + corrupted_count += 1; + tracing::warn!( + "Failed to read metadata for session '{}': {}. Skipping corrupted session.", + id, + e + ); + + // Optionally, we could create a placeholder entry for corrupted sessions + // to show them in the UI with an error indicator, but for now we skip them + continue; + } + } + } + + if corrupted_count > 0 { + tracing::warn!( + "Skipped {} corrupted sessions during listing", + corrupted_count + ); + } // Sort sessions by modified date // Since all dates are in ISO format (YYYY-MM-DD HH:MM:SS UTC), we can just use string comparison @@ -70,3 +97,42 @@ pub fn get_valid_sorted_sessions(sort_order: SortOrder) -> Result { try {