use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OfficialEvalResult {
pub resolved: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestResults {
pub passed: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Candidate {
pub trial: u32,
pub patch: String,
pub patch_bytes: usize,
pub patch_lines: usize,
pub has_source_edit: bool,
pub has_test_edit: bool,
pub syntax_check_passed: bool,
pub test_results: Option<TestResults>,
pub official_eval: Option<OfficialEvalResult>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CandidatePool {
pub candidates: Vec<Candidate>,
}
impl CandidatePool {
pub fn new(candidates: Vec<Candidate>) -> Self {
Self { candidates }
}
pub fn select_best(&self) -> Option<&Candidate> {
self.candidates.iter().max_by(|a, b| {
let a_official = a
.official_eval
.as_ref()
.map(|e| e.resolved)
.unwrap_or(false);
let b_official = b
.official_eval
.as_ref()
.map(|e| e.resolved)
.unwrap_or(false);
a_official
.cmp(&b_official)
.then_with(|| {
let a_good = a.has_source_edit && !a.has_test_edit;
let b_good = b.has_source_edit && !b.has_test_edit;
a_good.cmp(&b_good)
})
.then_with(|| b.patch_lines.cmp(&a.patch_lines))
.then_with(|| a.syntax_check_passed.cmp(&b.syntax_check_passed))
})
}
pub fn pass_at_1(&self) -> bool {
self.select_best()
.and_then(|c| c.official_eval.as_ref())
.map(|e| e.resolved)
.unwrap_or(false)
}
pub fn pass_at_k_oracle(&self) -> bool {
if self.has_any_official_eval() {
return self.candidates.iter().any(|c| {
c.official_eval
.as_ref()
.map(|e| e.resolved)
.unwrap_or(false)
});
}
self.candidates
.iter()
.any(|c| c.has_source_edit && !c.has_test_edit && c.syntax_check_passed)
}
pub fn has_any_official_eval(&self) -> bool {
self.candidates.iter().any(|c| c.official_eval.is_some())
}
pub fn all_have_official_eval(&self) -> bool {
!self.candidates.is_empty() && self.candidates.iter().all(|c| c.official_eval.is_some())
}
}
#[cfg(test)]
#[path = "../../../tests/unit/bench_harness/swebench_pro/candidate/candidate_test.rs"]
mod tests;