From e165d4c558d0071abc1ea3392d9aa7f20e017e4a Mon Sep 17 00:00:00 2001 From: 0xsid0703 Date: Thu, 15 Jan 2026 04:07:10 +0100 Subject: [PATCH] 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. --- crates/platform-server/src/db/queries.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/platform-server/src/db/queries.rs b/crates/platform-server/src/db/queries.rs index 2a602a5f..ed2afadd 100644 --- a/crates/platform-server/src/db/queries.rs +++ b/crates/platform-server/src/db/queries.rs @@ -327,6 +327,10 @@ pub async fn get_evaluations_for_agent(pool: &Pool, agent_hash: &str) -> Result< // LEADERBOARD // ============================================================================ +/// Update leaderboard entry for an agent by calculating consensus score from evaluations +/// +/// Calculates the average score from all evaluations for the agent and updates the leaderboard. +/// Returns `None` if no evaluations exist for the agent. pub async fn update_leaderboard(pool: &Pool, agent_hash: &str) -> Result> { let evaluations = get_evaluations_for_agent(pool, agent_hash).await?; if evaluations.is_empty() { @@ -338,7 +342,11 @@ pub async fn update_leaderboard(pool: &Pool, agent_hash: &str) -> Result = evaluations.iter().map(|e| e.score).collect(); - let consensus_score = scores.iter().sum::() / scores.len() as f64; + let consensus_score = if scores.is_empty() { + 0.0 // Should not happen due to check above, but defensive + } else { + scores.iter().sum::() / scores.len() as f64 + }; let evaluation_count = evaluations.len() as i32; let first_epoch = submission.epoch as i64;