|
| 1 | +use std::collections::{BTreeSet, HashMap}; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use crate::core; |
| 5 | + |
| 6 | +pub(super) fn apply_context_source_tags( |
| 7 | + mut comments: Vec<core::Comment>, |
| 8 | + verification_context: &HashMap<PathBuf, Vec<core::LLMContextChunk>>, |
| 9 | +) -> Vec<core::Comment> { |
| 10 | + let tags_by_file = verification_context |
| 11 | + .iter() |
| 12 | + .filter_map(|(file_path, chunks)| { |
| 13 | + let tags = artifact_tags_for_chunks(chunks); |
| 14 | + (!tags.is_empty()).then(|| (file_path.clone(), tags)) |
| 15 | + }) |
| 16 | + .collect::<HashMap<_, _>>(); |
| 17 | + |
| 18 | + for comment in &mut comments { |
| 19 | + let Some(tags) = tags_by_file.get(&comment.file_path) else { |
| 20 | + continue; |
| 21 | + }; |
| 22 | + |
| 23 | + for tag in tags { |
| 24 | + push_unique_tag(&mut comment.tags, tag); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + comments |
| 29 | +} |
| 30 | + |
| 31 | +fn artifact_tags_for_chunks(chunks: &[core::LLMContextChunk]) -> Vec<String> { |
| 32 | + chunks |
| 33 | + .iter() |
| 34 | + .filter_map(|chunk| chunk.provenance.as_ref()) |
| 35 | + .filter_map(core::ContextProvenance::artifact_tag) |
| 36 | + .collect::<BTreeSet<_>>() |
| 37 | + .into_iter() |
| 38 | + .collect() |
| 39 | +} |
| 40 | + |
| 41 | +fn push_unique_tag(tags: &mut Vec<String>, tag: &str) { |
| 42 | + if !tags.iter().any(|existing| existing == tag) { |
| 43 | + tags.push(tag.to_string()); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use std::collections::HashMap; |
| 50 | + use std::path::PathBuf; |
| 51 | + |
| 52 | + use super::*; |
| 53 | + use crate::core::comment::{Category, CommentStatus, FixEffort, Severity}; |
| 54 | + |
| 55 | + fn make_comment(file_path: &str) -> core::Comment { |
| 56 | + core::Comment { |
| 57 | + id: "comment-1".to_string(), |
| 58 | + file_path: PathBuf::from(file_path), |
| 59 | + line_number: 7, |
| 60 | + content: "Missing validation.".to_string(), |
| 61 | + rule_id: None, |
| 62 | + severity: Severity::Warning, |
| 63 | + category: Category::Bug, |
| 64 | + suggestion: None, |
| 65 | + confidence: 0.8, |
| 66 | + code_suggestion: None, |
| 67 | + tags: vec!["security".to_string()], |
| 68 | + fix_effort: FixEffort::Medium, |
| 69 | + feedback: None, |
| 70 | + status: CommentStatus::Open, |
| 71 | + resolved_at: None, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn applies_unique_context_source_tags_for_matching_file() { |
| 77 | + let mut verification_context = HashMap::new(); |
| 78 | + verification_context.insert( |
| 79 | + PathBuf::from("src/api.rs"), |
| 80 | + vec![ |
| 81 | + core::LLMContextChunk::documentation( |
| 82 | + PathBuf::from("src/api.rs"), |
| 83 | + "Custom context notes".to_string(), |
| 84 | + ) |
| 85 | + .with_provenance(core::ContextProvenance::CustomContextNotes), |
| 86 | + core::LLMContextChunk::reference( |
| 87 | + PathBuf::from("patterns/sql.md"), |
| 88 | + "Pattern repository rule".to_string(), |
| 89 | + ) |
| 90 | + .with_provenance( |
| 91 | + core::ContextProvenance::pattern_repository_context("patterns/security-rules"), |
| 92 | + ), |
| 93 | + core::LLMContextChunk::reference( |
| 94 | + PathBuf::from("patterns/sql.md"), |
| 95 | + "Pattern repository rule".to_string(), |
| 96 | + ) |
| 97 | + .with_provenance( |
| 98 | + core::ContextProvenance::pattern_repository_source("patterns/security-rules"), |
| 99 | + ), |
| 100 | + core::LLMContextChunk::documentation( |
| 101 | + PathBuf::from("src/api.rs"), |
| 102 | + "Analyzer guidance".to_string(), |
| 103 | + ) |
| 104 | + .with_provenance(core::ContextProvenance::analyzer("contracts")), |
| 105 | + ], |
| 106 | + ); |
| 107 | + |
| 108 | + let comments = apply_context_source_tags( |
| 109 | + vec![make_comment("src/api.rs"), make_comment("src/other.rs")], |
| 110 | + &verification_context, |
| 111 | + ); |
| 112 | + |
| 113 | + assert_eq!( |
| 114 | + comments[0].tags, |
| 115 | + vec![ |
| 116 | + "security".to_string(), |
| 117 | + "context-source:custom-context".to_string(), |
| 118 | + "context-source:pattern-repository:patterns/security-rules".to_string(), |
| 119 | + ] |
| 120 | + ); |
| 121 | + assert_eq!(comments[1].tags, vec!["security".to_string()]); |
| 122 | + } |
| 123 | +} |
0 commit comments