From 1ab8b9abfb035a16afc6ba50f20b1191d1f80c73 Mon Sep 17 00:00:00 2001 From: Parker Loflin Date: Wed, 18 Nov 2026 09:39:00 +0000 Subject: [PATCH] edit_prediction_cli: Fix total token percentiles (#67124) ## Summary `print_report` sorted the inserted and deleted token counts separately, then added them by rank, so "Total tokens" percentiles mixed counts from different predictions. It now adds each prediction's counts before sorting. ## Testing - `token_totals_are_summed_per_prediction` checks the sorted totals. It fails without the fix. - `cargo test -p edit_prediction_cli` and `./script/clippy` pass. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - N/A --- crates/edit_prediction_cli/src/score.rs | 35 +++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/crates/edit_prediction_cli/src/score.rs b/crates/edit_prediction_cli/src/score.rs index e911f030273..3442886fea1 100644 --- a/crates/edit_prediction_cli/src/score.rs +++ b/crates/edit_prediction_cli/src/score.rs @@ -546,14 +546,8 @@ pub fn print_report( // Print token change percentile summary (only for predictions with a patch) if !patch_inserted_tokens.is_empty() { - patch_inserted_tokens.sort_unstable(); - patch_deleted_tokens.sort_unstable(); - let mut patch_total_tokens: Vec = patch_inserted_tokens - .iter() - .zip(patch_deleted_tokens.iter()) - .map(|(i, d)| i + d) - .collect(); - patch_total_tokens.sort_unstable(); + let (patch_inserted_tokens, patch_deleted_tokens, patch_total_tokens) = + sorted_token_counts(patch_inserted_tokens, patch_deleted_tokens); let patch_rate = predictions_with_patch as f32 / total_scores as f32 * 100.0; println!(); @@ -766,6 +760,22 @@ fn percentile(sorted_values: &[usize], p: usize) -> usize { sorted_values[idx.min(sorted_values.len() - 1)] } +fn sorted_token_counts( + mut inserted_tokens: Vec, + mut deleted_tokens: Vec, +) -> (Vec, Vec, Vec) { + // Sum before sorting so each total comes from one prediction. + let mut total_tokens: Vec = inserted_tokens + .iter() + .zip(&deleted_tokens) + .map(|(inserted, deleted)| inserted + deleted) + .collect(); + inserted_tokens.sort_unstable(); + deleted_tokens.sort_unstable(); + total_tokens.sort_unstable(); + (inserted_tokens, deleted_tokens, total_tokens) +} + fn truncate_name(name: &str, max_len: usize) -> String { if name.len() <= max_len { name.to_string() @@ -865,6 +875,15 @@ mod tests { assert_eq!(summary.retrieved_context_examples, Some(1)); } + #[test] + fn token_totals_are_summed_per_prediction() { + let (inserted, deleted, total) = sorted_token_counts(vec![10, 0, 5], vec![0, 10, 1]); + + assert_eq!(inserted, vec![0, 5, 10]); + assert_eq!(deleted, vec![0, 1, 10]); + assert_eq!(total, vec![6, 10, 10]); + } + fn example_with_related_files( related_files: Option>, score_count: usize,