tetrad 0.1.0

MCP de Consenso Quádruplo para Claude Code - Valida código usando Codex, Gemini e Qwen
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Agregador de votos do Tetrad.
//!
//! Responsável por:
//! - Agregar votos dos executores
//! - Extrair issues comuns (consenso em problemas)
//! - Consolidar feedback em mensagem coerente
//! - Calcular score agregado

use std::collections::HashMap;

use crate::types::responses::{Decision, EvaluationResult, Finding, ModelVote, Severity, Vote};

use super::rules::ConsensusRule;

/// Agregador de votos.
pub struct VoteAggregator;

impl VoteAggregator {
    /// Agrega votos e retorna o resultado da avaliação.
    pub fn aggregate(
        votes: HashMap<String, ModelVote>,
        rule: &dyn ConsensusRule,
        min_score: u8,
        request_id: &str,
    ) -> EvaluationResult {
        let decision = rule.evaluate(&votes, min_score);
        let consensus_achieved = rule.is_consensus_achieved(&votes, min_score);
        let score = Self::calculate_score(&votes);
        let findings = Self::extract_findings(&votes);
        let feedback = Self::consolidate_feedback(&votes, &decision);

        EvaluationResult {
            request_id: request_id.to_string(),
            decision,
            score,
            votes,
            findings,
            feedback,
            consensus_achieved,
            timestamp: chrono::Utc::now(),
        }
    }

    /// Calcula o score agregado (média dos scores).
    pub fn calculate_score(votes: &HashMap<String, ModelVote>) -> u8 {
        if votes.is_empty() {
            return 0;
        }

        let total: u32 = votes.values().map(|v| v.score as u32).sum();
        (total / votes.len() as u32) as u8
    }

    /// Calcula o score mínimo entre os votos.
    pub fn calculate_min_score(votes: &HashMap<String, ModelVote>) -> u8 {
        votes.values().map(|v| v.score).min().unwrap_or(0)
    }

    /// Extrai findings dos votos, consolidando issues comuns.
    pub fn extract_findings(votes: &HashMap<String, ModelVote>) -> Vec<Finding> {
        let mut findings: Vec<Finding> = Vec::new();
        let mut issue_counts: HashMap<String, (Vec<String>, Severity)> = HashMap::new();

        // Conta quantos executores reportaram cada issue
        for (executor, vote) in votes {
            for issue in &vote.issues {
                let key = Self::normalize_issue(issue);
                let entry = issue_counts
                    .entry(key.clone())
                    .or_insert_with(|| (Vec::new(), Self::infer_severity(issue)));
                entry.0.push(executor.clone());
            }
        }

        // Cria findings para issues reportados por múltiplos executores (consenso)
        for (issue, (executors, severity)) in &issue_counts {
            let consensus_strength = if executors.len() >= 3 {
                "forte"
            } else if executors.len() >= 2 {
                "moderado"
            } else {
                "fraco"
            };

            // Busca sugestão correspondente
            let suggestion = Self::find_suggestion_for_issue(votes, issue);

            // Infere categoria do issue
            let category = Self::infer_category(issue);

            findings.push(Finding {
                issue: issue.clone(),
                severity: *severity,
                category,
                lines: None,
                suggestion,
                source: executors.join(", "),
                consensus_strength: consensus_strength.to_string(),
            });
        }

        // Ordena por severidade (Critical > Error > Warning > Info)
        findings.sort_by(|a, b| {
            let severity_order = |s: &Severity| match s {
                Severity::Critical => 0,
                Severity::Error => 1,
                Severity::Warning => 2,
                Severity::Info => 3,
            };
            severity_order(&a.severity).cmp(&severity_order(&b.severity))
        });

        findings
    }

    /// Consolida feedback de todos os executores.
    pub fn consolidate_feedback(votes: &HashMap<String, ModelVote>, decision: &Decision) -> String {
        let mut feedback = String::new();

        // Cabeçalho baseado na decisão
        let header = match decision {
            Decision::Pass => "## Avaliação Aprovada",
            Decision::Revise => "## Revisão Necessária",
            Decision::Block => "## Avaliação Bloqueada",
        };
        feedback.push_str(header);
        feedback.push_str("\n\n");

        // Resumo dos votos
        let pass_count = votes.values().filter(|v| v.vote == Vote::Pass).count();
        let warn_count = votes.values().filter(|v| v.vote == Vote::Warn).count();
        let fail_count = votes.values().filter(|v| v.vote == Vote::Fail).count();

        feedback.push_str(&format!(
            "**Votos:** {} PASS | {} WARN | {} FAIL\n\n",
            pass_count, warn_count, fail_count
        ));

        // Feedback individual de cada executor
        feedback.push_str("### Feedback dos Avaliadores\n\n");

        for (executor, vote) in votes {
            let icon = match vote.vote {
                Vote::Pass => "",
                Vote::Warn => "",
                Vote::Fail => "",
            };

            feedback.push_str(&format!(
                "**{} {}** (score: {})\n",
                icon, executor, vote.score
            ));

            if !vote.reasoning.is_empty() {
                feedback.push_str(&format!("> {}\n", vote.reasoning));
            }

            if !vote.issues.is_empty() {
                feedback.push_str("\nIssues:\n");
                for issue in &vote.issues {
                    feedback.push_str(&format!("- {}\n", issue));
                }
            }

            if !vote.suggestions.is_empty() {
                feedback.push_str("\nSugestões:\n");
                for suggestion in &vote.suggestions {
                    feedback.push_str(&format!("- {}\n", suggestion));
                }
            }

            feedback.push('\n');
        }

        // Ações recomendadas
        feedback.push_str("### Ações Recomendadas\n\n");
        match decision {
            Decision::Pass => {
                feedback.push_str("O código foi aprovado por todos os avaliadores. ");
                feedback.push_str("Você pode prosseguir com a implementação.\n");
            }
            Decision::Revise => {
                feedback.push_str("O código precisa de ajustes antes de ser aprovado. ");
                feedback.push_str("Revise os issues acima e submeta novamente.\n");
            }
            Decision::Block => {
                feedback.push_str("O código foi bloqueado devido a problemas críticos. ");
                feedback.push_str("Corrija TODOS os issues marcados como Critical ou Error antes de prosseguir.\n");
            }
        }

        feedback
    }

    /// Normaliza um issue para comparação (lowercase, trim).
    fn normalize_issue(issue: &str) -> String {
        issue.to_lowercase().trim().to_string()
    }

    /// Infere a severidade de um issue baseado em keywords.
    fn infer_severity(issue: &str) -> Severity {
        let issue_lower = issue.to_lowercase();

        if issue_lower.contains("critical")
            || issue_lower.contains("security")
            || issue_lower.contains("vulnerability")
            || issue_lower.contains("injection")
        {
            Severity::Critical
        } else if issue_lower.contains("error")
            || issue_lower.contains("bug")
            || issue_lower.contains("fail")
            || issue_lower.contains("crash")
        {
            Severity::Error
        } else if issue_lower.contains("warning")
            || issue_lower.contains("warn")
            || issue_lower.contains("should")
            || issue_lower.contains("consider")
        {
            Severity::Warning
        } else {
            Severity::Info
        }
    }

    /// Infere a categoria de um issue baseado em keywords.
    fn infer_category(issue: &str) -> String {
        let issue_lower = issue.to_lowercase();

        if issue_lower.contains("security")
            || issue_lower.contains("injection")
            || issue_lower.contains("vulnerability")
            || issue_lower.contains("password")
            || issue_lower.contains("credential")
        {
            "security".to_string()
        } else if issue_lower.contains("performance")
            || issue_lower.contains("slow")
            || issue_lower.contains("memory")
            || issue_lower.contains("allocation")
        {
            "performance".to_string()
        } else if issue_lower.contains("logic")
            || issue_lower.contains("bug")
            || issue_lower.contains("incorrect")
            || issue_lower.contains("wrong")
        {
            "logic".to_string()
        } else if issue_lower.contains("style")
            || issue_lower.contains("convention")
            || issue_lower.contains("naming")
            || issue_lower.contains("format")
        {
            "style".to_string()
        } else if issue_lower.contains("architecture")
            || issue_lower.contains("design")
            || issue_lower.contains("pattern")
            || issue_lower.contains("structure")
        {
            "architecture".to_string()
        } else {
            "general".to_string()
        }
    }

    /// Busca uma sugestão correspondente a um issue.
    fn find_suggestion_for_issue(
        votes: &HashMap<String, ModelVote>,
        issue: &str,
    ) -> Option<String> {
        let issue_normalized = Self::normalize_issue(issue);

        for vote in votes.values() {
            for (i, vote_issue) in vote.issues.iter().enumerate() {
                if Self::normalize_issue(vote_issue) == issue_normalized {
                    if let Some(suggestion) = vote.suggestions.get(i) {
                        return Some(suggestion.clone());
                    }
                }
            }

            // Se não encontrou por índice, tenta a primeira sugestão disponível
            if !vote.suggestions.is_empty() {
                // Usa chars() para slice seguro em UTF-8 (evita panic em caracteres não-ASCII)
                let issue_prefix: String = issue_normalized.chars().take(20).collect();
                for suggestion in &vote.suggestions {
                    if suggestion.to_lowercase().contains(&issue_prefix) {
                        return Some(suggestion.clone());
                    }
                }
            }
        }

        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::consensus::rules::StrongRule;

    fn create_vote(name: &str, vote: Vote, score: u8) -> (String, ModelVote) {
        (name.to_string(), ModelVote::new(name, vote, score))
    }

    fn create_vote_with_issues(
        name: &str,
        vote: Vote,
        score: u8,
        issues: Vec<&str>,
        suggestions: Vec<&str>,
    ) -> (String, ModelVote) {
        let mut mv = ModelVote::new(name, vote, score);
        mv.issues = issues.into_iter().map(String::from).collect();
        mv.suggestions = suggestions.into_iter().map(String::from).collect();
        (name.to_string(), mv)
    }

    #[test]
    fn test_calculate_score() {
        let votes: HashMap<String, ModelVote> = vec![
            create_vote("Codex", Vote::Pass, 80),
            create_vote("Gemini", Vote::Pass, 90),
            create_vote("Qwen", Vote::Pass, 85),
        ]
        .into_iter()
        .collect();

        assert_eq!(VoteAggregator::calculate_score(&votes), 85);
    }

    #[test]
    fn test_calculate_min_score() {
        let votes: HashMap<String, ModelVote> = vec![
            create_vote("Codex", Vote::Pass, 80),
            create_vote("Gemini", Vote::Pass, 90),
            create_vote("Qwen", Vote::Warn, 60),
        ]
        .into_iter()
        .collect();

        assert_eq!(VoteAggregator::calculate_min_score(&votes), 60);
    }

    #[test]
    fn test_extract_findings_common_issues() {
        let votes: HashMap<String, ModelVote> = vec![
            create_vote_with_issues(
                "Codex",
                Vote::Warn,
                70,
                vec!["SQL injection vulnerability"],
                vec!["Use parameterized queries"],
            ),
            create_vote_with_issues(
                "Gemini",
                Vote::Warn,
                65,
                vec!["sql injection vulnerability"],
                vec!["Sanitize inputs"],
            ),
            create_vote_with_issues("Qwen", Vote::Pass, 85, vec![], vec![]),
        ]
        .into_iter()
        .collect();

        let findings = VoteAggregator::extract_findings(&votes);
        assert!(!findings.is_empty());

        // Deve haver um finding para SQL injection
        let sql_finding = findings.iter().find(|f| f.issue.contains("sql injection"));
        assert!(sql_finding.is_some());
    }

    #[test]
    fn test_aggregate_pass() {
        let votes: HashMap<String, ModelVote> = vec![
            create_vote("Codex", Vote::Pass, 85),
            create_vote("Gemini", Vote::Pass, 90),
            create_vote("Qwen", Vote::Pass, 88),
        ]
        .into_iter()
        .collect();

        let rule = StrongRule;
        let result = VoteAggregator::aggregate(votes, &rule, 70, "test-123");

        assert_eq!(result.decision, Decision::Pass);
        assert!(result.consensus_achieved);
        assert_eq!(result.score, 87); // (85+90+88)/3
    }

    #[test]
    fn test_consolidate_feedback_pass() {
        let votes: HashMap<String, ModelVote> = vec![
            create_vote("Codex", Vote::Pass, 85),
            create_vote("Gemini", Vote::Pass, 90),
        ]
        .into_iter()
        .collect();

        let feedback = VoteAggregator::consolidate_feedback(&votes, &Decision::Pass);

        assert!(feedback.contains("Avaliação Aprovada"));
        assert!(feedback.contains("2 PASS"));
    }

    #[test]
    fn test_consolidate_feedback_block() {
        let votes: HashMap<String, ModelVote> = vec![
            create_vote("Codex", Vote::Fail, 30),
            create_vote("Gemini", Vote::Fail, 25),
        ]
        .into_iter()
        .collect();

        let feedback = VoteAggregator::consolidate_feedback(&votes, &Decision::Block);

        assert!(feedback.contains("Avaliação Bloqueada"));
        assert!(feedback.contains("2 FAIL"));
    }

    #[test]
    fn test_infer_severity() {
        assert_eq!(
            VoteAggregator::infer_severity("SQL injection vulnerability"),
            Severity::Critical
        );
        assert_eq!(
            VoteAggregator::infer_severity("Error in logic"),
            Severity::Error
        );
        assert_eq!(
            VoteAggregator::infer_severity("Warning: consider refactoring"),
            Severity::Warning
        );
        assert_eq!(
            VoteAggregator::infer_severity("Minor style issue"),
            Severity::Info
        );
    }
}