sdd-layer 0.19.0

Spec-Driven Development CLI and agent harness
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Tipos fundacionais e utilitários para Agent Teams paralelos na TUI (T-01..T-05).
//! Nenhuma I/O real ocorre aqui — apenas definições de tipos e funções puras.

use std::path::PathBuf;

use crate::tui::runner::{required_sections_for, StageEvent, StageResult, TokenUsageSample};
use crate::tui::stage::Stage;

/// Status de um slot de agente numa execução paralela.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AgentSlotStatus {
    Aguardando,
    EmExecucao,
    Concluido,
    Erro,
}

impl AgentSlotStatus {
    pub fn is_terminal(self) -> bool {
        matches!(self, AgentSlotStatus::Concluido | AgentSlotStatus::Erro)
    }

    pub fn label(self) -> &'static str {
        match self {
            AgentSlotStatus::Aguardando => "Aguardando",
            AgentSlotStatus::EmExecucao => "Em execução",
            AgentSlotStatus::Concluido => "Concluído",
            AgentSlotStatus::Erro => "Erro",
        }
    }
}

/// Handle de um agente em execução paralela.
///
/// Não implementa `Clone` porque `Receiver<StageEvent>` não é `Clone`.
pub struct AgentHandle {
    pub task_id: String,
    pub task_label: String,
    pub rx: std::sync::mpsc::Receiver<StageEvent>,
    pub status: AgentSlotStatus,
    pub trace: Vec<String>,
    pub usage: Option<TokenUsageSample>,
    pub result: Option<StageResult>,
    pub fragment_path: PathBuf,
}

/// Referência a uma task que pode ser executada em paralelo.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TaskRef {
    pub id: String,
    pub title: String,
    pub independent: bool,
}

/// Resultado serializado de um slot após conclusão.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SlotResult {
    pub task_id: String,
    pub task_label: String,
    pub status: AgentSlotStatus,
    pub fragment: String,
}

/// Indica se uma etapa suporta execução paralela de Agent Teams.
///
/// Apenas `Execution` e `Review` suportam paralelismo real — as demais
/// etapas são sequenciais por design (contrato SDD).
pub fn supports_parallel(stage: Stage) -> bool {
    matches!(stage, Stage::Execution | Stage::Review)
}

/// Planeja os slots paralelos a partir do Markdown de tasks (T-02).
pub fn plan_parallel_slots(tasks_md: &str) -> Vec<TaskRef> {
    let independent_ids = extract_independent_ids(tasks_md);
    parse_backlog_table(tasks_md, &independent_ids)
}

/// Extrai IDs de tasks declaradas como independentes na seção
/// "Tasks independentes entre si".
fn extract_independent_ids(md: &str) -> std::collections::HashSet<String> {
    let mut ids = std::collections::HashSet::new();
    let mut in_section = false;

    for line in md.lines() {
        let trimmed = line.trim();

        // Detecta entrada na seção de independentes (h2, h3 ou h4)
        if trimmed.starts_with('#') {
            let heading = trimmed.trim_start_matches('#').trim().to_lowercase();
            in_section = heading.contains("independentes");
            continue;
        }

        if !in_section {
            continue;
        }

        // Extrai todos os padrões T-<dígitos> da linha
        let mut rest = trimmed;
        while let Some(pos) = rest.find("T-") {
            let after = &rest[pos + 2..];
            let digits: String = after.chars().take_while(|c| c.is_ascii_digit()).collect();
            if !digits.is_empty() {
                ids.insert(format!("T-{}", digits));
            }
            // avança além do "T-" encontrado para evitar loop infinito
            rest = &rest[pos + 2..];
        }
    }

    ids
}

/// Faz parse da tabela de Backlog e retorna as tasks com flag `independent`.
fn parse_backlog_table(
    md: &str,
    independent_ids: &std::collections::HashSet<String>,
) -> Vec<TaskRef> {
    let mut tasks = Vec::new();
    let mut in_backlog = false;

    for line in md.lines() {
        let trimmed = line.trim();

        // Detecta início da seção Backlog
        if trimmed.starts_with('#') {
            let heading = trimmed.trim_start_matches('#').trim().to_lowercase();
            in_backlog = heading == "backlog";
            continue;
        }

        if !in_backlog {
            continue;
        }

        // Linha de tabela com task: deve começar com "| T-"
        if !trimmed.starts_with("| T-") {
            continue;
        }

        // Divide a linha em células; pipe inicial e final geram células vazias
        let cols: Vec<&str> = trimmed.split('|').collect();
        // cols[0] = "" (antes do primeiro pipe)
        // cols[1] = ID, cols[2] = Título, cols[3] = Estimativa, cols[4] = Dependências
        if cols.len() < 5 {
            continue;
        }

        let id = cols[1].trim().to_string();
        let title = cols[2].trim().to_string();
        let deps = cols[4].trim();

        // Valida que o ID tem o formato T-<dígitos>
        if !id.starts_with("T-") || id[2..].chars().any(|c| !c.is_ascii_digit()) {
            continue;
        }

        let independent = independent_ids.contains(&id) || has_no_declared_dependency(deps);

        tasks.push(TaskRef {
            id,
            title,
            independent,
        });
    }

    tasks
}

fn has_no_declared_dependency(deps: &str) -> bool {
    let normalized = deps.trim();
    normalized.is_empty() || matches!(normalized, "" | "-" | "" | "Nenhuma" | "nenhuma")
}

/// Compõe o artefato final consolidado a partir dos resultados dos slots (T-03).
pub fn compose_parallel_artifact(stage: Stage, results: &[SlotResult]) -> String {
    let mut out = String::new();
    out.push_str(&format!("# {} — Agent Teams Paralelos\n\n", stage.label()));
    out.push_str("## Rastreabilidade\n");
    out.push_str("- Origem: `docs/<slug>/04-tasks.md`\n");
    out.push_str(&format!("- Etapa: `{}`\n", stage.key()));
    out.push_str("- Modo: execução paralela por AgentSlot\n\n");
    out.push_str(&traceability_table(results));
    out.push('\n');

    match stage {
        Stage::Execution => append_execution_sections(&mut out, results),
        Stage::Review => append_review_sections(&mut out, results),
        _ => append_generic_sections(&mut out, stage, results),
    }
    ensure_required_sections(&mut out, stage);
    out
}

fn append_execution_sections(out: &mut String, results: &[SlotResult]) {
    out.push_str("## Tarefa\n");
    out.push_str(&format!(
        "- Executar {} task(s) por Agent Teams paralelos.\n",
        results.len()
    ));
    out.push_str("- Cada task recebeu exatamente um AgentSlot no artefato consolidado.\n\n");

    out.push_str("## Resumo da implementação\n");
    append_slot_fragments(out, results);

    out.push_str("## Arquivos alterados\n");
    out.push_str(
        "- Consolidado a partir dos fragmentos emitidos pelos AgentSlots; detalhes ficam nos fragmentos de cada task.\n\n",
    );

    out.push_str("## Testes e evidências\n");
    append_status_evidence(out, results);

    out.push_str("## Riscos e pendências\n");
    append_risks(out, results);
}

fn append_review_sections(out: &mut String, results: &[SlotResult]) {
    out.push_str("## Escopo revisado\n");
    out.push_str(&format!(
        "- Review paralela de {} task(s) planejadas.\n\n",
        results.len()
    ));

    out.push_str("## Evidências\n");
    append_status_evidence(out, results);

    out.push_str("## Achados\n");
    append_slot_fragments(out, results);

    out.push_str("## Testes\n");
    let errors = results
        .iter()
        .filter(|result| result.status == AgentSlotStatus::Erro)
        .count();
    if errors == 0 {
        out.push_str("- Todos os AgentSlots concluíram sem erro reportado.\n\n");
    } else {
        out.push_str(&format!(
            "- {} AgentSlot(s) falharam; revisar pendências antes de aprovar.\n\n",
            errors
        ));
    }

    out.push_str("## Veredito\n");
    if errors == 0 {
        out.push_str("- Aprovado para checkpoint humano.\n\n");
    } else {
        out.push_str("- Aprovado com ressalvas: há AgentSlots em erro registrados acima.\n\n");
    }
}

fn append_generic_sections(out: &mut String, stage: Stage, results: &[SlotResult]) {
    for section in required_sections_for(stage) {
        if *section == "Rastreabilidade" {
            continue;
        }
        out.push_str(&format!("## {section}\n"));
        append_slot_fragments(out, results);
    }
}

fn append_slot_fragments(out: &mut String, results: &[SlotResult]) {
    if results.is_empty() {
        out.push_str("- Nenhum AgentSlot foi planejado.\n\n");
        return;
    }

    for result in results {
        out.push_str(&format!(
            "### {}{}\n\n",
            result.task_id, result.task_label
        ));
        match result.status {
            AgentSlotStatus::Concluido => {
                let fragment = result.fragment.trim();
                if fragment.is_empty() {
                    out.push_str("> [ERRO] slot concluiu sem fragmento materializado\n\n");
                } else {
                    out.push_str(fragment);
                    out.push_str("\n\n");
                }
            }
            _ => {
                out.push_str("> [ERRO] slot falhou — fragmento ausente\n\n");
            }
        }
    }
}

fn append_status_evidence(out: &mut String, results: &[SlotResult]) {
    if results.is_empty() {
        out.push_str("- Nenhum AgentSlot executado.\n\n");
        return;
    }
    for result in results {
        out.push_str(&format!(
            "- `{}`: {}.\n",
            result.task_id,
            result.status.label()
        ));
    }
    out.push('\n');
}

fn append_risks(out: &mut String, results: &[SlotResult]) {
    let failed = results
        .iter()
        .filter(|result| result.status == AgentSlotStatus::Erro)
        .collect::<Vec<_>>();
    if failed.is_empty() {
        out.push_str("- Nenhuma pendência automática registrada pelos AgentSlots.\n\n");
        return;
    }
    out.push_str("- Resultado parcial aceito ou barreira concluída com falhas isoladas.\n");
    for result in failed {
        out.push_str(&format!(
            "- `{}` — {}: AgentSlot em erro; fragmento não disponível.\n",
            result.task_id, result.task_label
        ));
    }
    out.push('\n');
}

fn ensure_required_sections(out: &mut String, stage: Stage) {
    for section in required_sections_for(stage) {
        if out.contains(&format!("## {section}\n")) {
            continue;
        }
        out.push_str(&format!("## {section}\n"));
        out.push_str("- Seção preservada para compatibilidade com o contrato do artefato.\n\n");
    }
}

/// Gera a tabela de rastreabilidade em Markdown a partir dos resultados dos slots (T-03).
pub fn traceability_table(results: &[SlotResult]) -> String {
    let mut out = String::new();
    out.push_str("| Task ID | AgentSlot | Título | Status |\n");
    out.push_str("|---------|-----------|--------|--------|\n");
    for r in results {
        out.push_str(&format!(
            "| {} | agent-{} | {} | {} |\n",
            escape_markdown_cell(&r.task_id),
            escape_markdown_cell(&r.task_id),
            escape_markdown_cell(&r.task_label),
            r.status.label()
        ));
    }
    out
}

fn escape_markdown_cell(input: &str) -> String {
    input.replace('|', "\\|").replace('\n', " ")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn supports_parallel_returns_true_for_execution() {
        assert!(supports_parallel(Stage::Execution));
    }

    #[test]
    fn supports_parallel_returns_true_for_review() {
        assert!(supports_parallel(Stage::Review));
    }

    #[test]
    fn supports_parallel_returns_false_for_idea() {
        assert!(!supports_parallel(Stage::Idea));
    }

    #[test]
    fn supports_parallel_returns_false_for_prd() {
        assert!(!supports_parallel(Stage::Prd));
    }

    #[test]
    fn supports_parallel_returns_false_for_techspec() {
        assert!(!supports_parallel(Stage::Techspec));
    }

    #[test]
    fn supports_parallel_returns_false_for_tasks() {
        assert!(!supports_parallel(Stage::Tasks));
    }

    #[test]
    fn supports_parallel_returns_false_for_refinement() {
        assert!(!supports_parallel(Stage::Refinement));
    }

    #[test]
    fn supports_parallel_returns_false_for_memory() {
        assert!(!supports_parallel(Stage::Memory));
    }

    // --- testes de plan_parallel_slots (T-02) ---

    #[test]
    fn empty_string_returns_empty() {
        assert!(plan_parallel_slots("").is_empty());
    }

    #[test]
    fn no_table_returns_empty() {
        let md = "# Título\n\nAlgum texto sem tabela.\n";
        assert!(plan_parallel_slots(md).is_empty());
    }

    #[test]
    fn identifies_independent_tasks() {
        let md = "\
## Backlog

| ID | Título | Estimativa | Dependências | Arquivos | Status |
|----|--------|-----------|-------------|----------|--------|
| T-01 | Criar módulo base | P (2h) | — | src/lib.rs | Todo |
| T-02 | Implementar parser | P (2h) | T-01 | src/parser.rs | Todo |
| T-03 | Adicionar testes | P (1h) | T-01 | tests/mod.rs | Todo |

### Tasks independentes entre si (podem rodar em paralelo)

- **Grupo A** (após T-01 concluída): T-02 e T-03 são independentes entre si.
";
        let result = plan_parallel_slots(md);
        assert_eq!(result.len(), 3);

        let t01 = result
            .iter()
            .find(|t| t.id == "T-01")
            .expect("T-01 presente");
        let t02 = result
            .iter()
            .find(|t| t.id == "T-02")
            .expect("T-02 presente");
        let t03 = result
            .iter()
            .find(|t| t.id == "T-03")
            .expect("T-03 presente");

        // T-01: dependência "—" → independent
        assert!(t01.independent, "T-01 deve ser independent (dep=—)");
        // T-02: listada como independente na seção → independent
        assert!(t02.independent, "T-02 deve ser independent (seção)");
        // T-03: listada como independente na seção → independent
        assert!(t03.independent, "T-03 deve ser independent (seção)");
    }

    #[test]
    fn task_with_explicit_dep_not_independent() {
        let md = "\
## Backlog

| ID | Título | Estimativa | Dependências | Arquivos | Status |
|----|--------|-----------|-------------|----------|--------|
| T-01 | Base | P (1h) | — | src/lib.rs | Todo |
| T-02 | Dependente | P (1h) | T-01 | src/dep.rs | Todo |
";
        let result = plan_parallel_slots(md);
        let t02 = result
            .iter()
            .find(|t| t.id == "T-02")
            .expect("T-02 presente");
        assert!(
            !t02.independent,
            "T-02 tem dep explícita, nao deve ser independent"
        );
    }

    #[test]
    fn preserves_declaration_order() {
        let md = "\
## Backlog

| ID | Título | Estimativa | Dependências | Arquivos | Status |
|----|--------|-----------|-------------|----------|--------|
| T-03 | Terceira | P (1h) | — | a.rs | Todo |
| T-01 | Primeira | P (1h) | — | b.rs | Todo |
| T-02 | Segunda | P (1h) | — | c.rs | Todo |
";
        let result = plan_parallel_slots(md);
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].id, "T-03");
        assert_eq!(result[1].id, "T-01");
        assert_eq!(result[2].id, "T-02");
    }

    #[test]
    fn malformed_markdown_no_panic() {
        let result = plan_parallel_slots("não é um markdown válido\n### cabeçalho\n| lixo |");
        // não panics, retorna Vec (possivelmente vazio)
        let _ = result;
    }

    // --- testes de traceability_table e compose_parallel_artifact (T-03) ---

    fn slot(id: &str, label: &str, status: AgentSlotStatus, fragment: &str) -> SlotResult {
        SlotResult {
            task_id: id.to_string(),
            task_label: label.to_string(),
            status,
            fragment: fragment.to_string(),
        }
    }

    #[test]
    fn traceability_table_has_header() {
        let table = traceability_table(&[]);
        assert!(table.contains("| Task ID | AgentSlot | Título | Status |"));
        assert!(table.contains("|---------|-----------|--------|--------|"));
    }

    #[test]
    fn traceability_table_maps_all_statuses() {
        let results = vec![
            slot("T-01", "Alpha", AgentSlotStatus::Aguardando, ""),
            slot("T-02", "Beta", AgentSlotStatus::EmExecucao, ""),
            slot("T-03", "Gamma", AgentSlotStatus::Concluido, "ok"),
            slot("T-04", "Delta", AgentSlotStatus::Erro, ""),
        ];
        let table = traceability_table(&results);
        assert!(table.contains("Aguardando"));
        assert!(table.contains("Em execução"));
        assert!(table.contains("Concluído"));
        assert!(table.contains("Erro"));
    }

    #[test]
    fn compose_includes_rastreabilidade_no_topo() {
        let results = vec![
            slot("T-01", "Slot A", AgentSlotStatus::Concluido, "fragmento A"),
            slot("T-02", "Slot B", AgentSlotStatus::Concluido, "fragmento B"),
        ];
        let out = compose_parallel_artifact(Stage::Execution, &results);
        let table_pos = out.find("| Task ID |").expect("tabela presente");
        let frag_pos = out.find("### T-01").expect("fragmento presente");
        assert!(
            table_pos < frag_pos,
            "tabela de rastreabilidade deve aparecer antes dos fragmentos"
        );
        for section in required_sections_for(Stage::Execution) {
            assert!(
                out.contains(&format!("## {section}\n")),
                "seção obrigatória ausente: {section}"
            );
        }
    }

    #[test]
    fn compose_marks_error_slot() {
        let results = vec![
            slot("T-01", "Slot OK", AgentSlotStatus::Concluido, "conteúdo ok"),
            slot("T-02", "Slot Falho", AgentSlotStatus::Erro, ""),
        ];
        let out = compose_parallel_artifact(Stage::Review, &results);
        assert!(
            out.contains("[ERRO]"),
            "slot de erro deve conter marcador [ERRO]"
        );
        assert!(
            out.contains("conteúdo ok"),
            "slot concluído deve preservar fragmento"
        );
    }

    #[test]
    fn compose_is_pure_same_output_twice() {
        let results = vec![
            slot("T-01", "Alpha", AgentSlotStatus::Concluido, "frag"),
            slot("T-02", "Beta", AgentSlotStatus::Erro, ""),
        ];
        let first = compose_parallel_artifact(Stage::Execution, &results);
        let second = compose_parallel_artifact(Stage::Execution, &results);
        assert_eq!(
            first, second,
            "função pura: saída idêntica em chamadas repetidas"
        );
    }

    #[test]
    fn compose_empty_results() {
        // não deve entrar em pânico com slice vazio
        let out = compose_parallel_artifact(Stage::Execution, &[]);
        assert!(out.contains("# Execution"));
    }
}