Skip to content

Commit e165d4c

Browse files
committed
fix: prevent division by zero in leaderboard calculation
Add defensive check for empty scores vector before division to prevent potential panic. While evaluations.is_empty() is checked above, this adds an extra layer of safety in case the mapping produces an empty vector. Fixes division by zero bug in update_leaderboard function.
1 parent cd15c6f commit e165d4c

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

crates/platform-server/src/db/queries.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ pub async fn get_evaluations_for_agent(pool: &Pool, agent_hash: &str) -> Result<
327327
// LEADERBOARD
328328
// ============================================================================
329329

330+
/// Update leaderboard entry for an agent by calculating consensus score from evaluations
331+
///
332+
/// Calculates the average score from all evaluations for the agent and updates the leaderboard.
333+
/// Returns `None` if no evaluations exist for the agent.
330334
pub async fn update_leaderboard(pool: &Pool, agent_hash: &str) -> Result<Option<LeaderboardEntry>> {
331335
let evaluations = get_evaluations_for_agent(pool, agent_hash).await?;
332336
if evaluations.is_empty() {
@@ -338,7 +342,11 @@ pub async fn update_leaderboard(pool: &Pool, agent_hash: &str) -> Result<Option<
338342
.ok_or_else(|| anyhow!("Submission not found for agent_hash: {}", agent_hash))?;
339343

340344
let scores: Vec<f64> = evaluations.iter().map(|e| e.score).collect();
341-
let consensus_score = scores.iter().sum::<f64>() / scores.len() as f64;
345+
let consensus_score = if scores.is_empty() {
346+
0.0 // Should not happen due to check above, but defensive
347+
} else {
348+
scores.iter().sum::<f64>() / scores.len() as f64
349+
};
342350
let evaluation_count = evaluations.len() as i32;
343351
let first_epoch = submission.epoch as i64;
344352

0 commit comments

Comments
 (0)