sdd-layer 0.21.5

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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
//! Execução real de tasks no TUI: parser de backlog, diff por snapshot e
//! composição do artefato final de Execution.

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::path::{Component, Path};

use similar::TextDiff;
use walkdir::WalkDir;

use super::runner::StageResult;
use super::stage::Stage;

const MAX_TRACKED_FILE_BYTES: u64 = 512 * 1024;
const MAX_PATCH_CHARS_PER_TASK: usize = 12_000;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ExecutionTask {
    pub id: String,
    pub title: String,
    pub body: String,
    pub dependencies: Vec<String>,
    pub independent: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TaskRunStatus {
    Pending,
    Running,
    Succeeded,
    Failed,
    Skipped,
}

impl TaskRunStatus {
    pub fn label(self) -> &'static str {
        match self {
            TaskRunStatus::Pending => "Aguardando",
            TaskRunStatus::Running => "Em execução",
            TaskRunStatus::Succeeded => "Concluída",
            TaskRunStatus::Failed => "Erro",
            TaskRunStatus::Skipped => "Pulada",
        }
    }

    pub fn marker(self) -> &'static str {
        match self {
            TaskRunStatus::Pending => "",
            TaskRunStatus::Running => "",
            TaskRunStatus::Succeeded => "",
            TaskRunStatus::Failed => "",
            TaskRunStatus::Skipped => "",
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DiffReport {
    pub changed_files: Vec<String>,
    pub summary: String,
    pub patch: String,
}

impl DiffReport {
    pub fn is_empty(&self) -> bool {
        self.changed_files.is_empty()
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct WorkspaceSnapshot {
    files: BTreeMap<String, TrackedFile>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum TrackedFile {
    Text(String),
    BinaryOrLarge,
}

#[derive(Clone, Debug)]
pub struct CompletedTask {
    pub task: ExecutionTask,
    pub status: TaskRunStatus,
    pub report: String,
    pub diff: DiffReport,
    pub result: Option<StageResult>,
    pub trace: Vec<String>,
}

/// Planeja as tasks de execução a partir do Markdown canônico de `04-tasks.md`.
/// Aceita tanto a tabela legada `T-01` quanto seções `### TSK-01 — ...`.
pub fn parse_execution_tasks(md: &str) -> Vec<ExecutionTask> {
    let independent_ids = extract_independent_ids(md);
    let section_tasks = parse_section_tasks(md, &independent_ids);
    let table_tasks = parse_backlog_table(md, &independent_ids);
    if !table_tasks.is_empty() {
        return merge_table_and_section_tasks(table_tasks, section_tasks);
    }
    section_tasks
}

fn extract_independent_ids(md: &str) -> HashSet<String> {
    let mut ids = HashSet::new();
    let mut in_section = false;

    for line in md.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            let heading = trimmed.trim_start_matches('#').trim().to_lowercase();
            in_section = heading.contains("independente") || heading.contains("paralel");
            continue;
        }
        if in_section {
            for id in extract_task_ids(trimmed) {
                ids.insert(id);
            }
        }
    }
    ids
}

fn parse_backlog_table(md: &str, independent_ids: &HashSet<String>) -> Vec<ExecutionTask> {
    let mut tasks = Vec::new();
    let mut in_backlog = false;

    for line in md.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            let heading = trimmed.trim_start_matches('#').trim().to_lowercase();
            in_backlog = heading == "backlog";
            continue;
        }
        if !in_backlog || !trimmed.starts_with('|') {
            continue;
        }

        let cols = trimmed
            .split('|')
            .map(str::trim)
            .filter(|cell| !cell.is_empty())
            .collect::<Vec<_>>();
        if cols.len() < 2 {
            continue;
        }
        let Some((id, _)) = task_id_at_start(cols[0]) else {
            continue;
        };
        let title = cols[1].trim().to_string();
        if title.is_empty() {
            continue;
        }
        let deps = cols.get(3).copied().unwrap_or_default();
        let dependencies = extract_task_ids(deps);
        let independent = independent_ids.contains(&id) || has_no_declared_dependency(deps);
        tasks.push(ExecutionTask {
            id,
            title,
            body: String::new(),
            dependencies,
            independent,
        });
    }

    tasks
}

fn parse_section_tasks(md: &str, independent_ids: &HashSet<String>) -> Vec<ExecutionTask> {
    let mut tasks = Vec::new();
    let mut in_backlog = false;
    let mut current: Option<ExecutionTask> = None;

    for line in md.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("## ") {
            if let Some(task) = current.take() {
                tasks.push(finalize_section_task(task, independent_ids));
            }
            let heading = trimmed.trim_start_matches('#').trim().to_lowercase();
            in_backlog = is_task_detail_section(&heading);
            continue;
        }

        if !in_backlog && current.is_none() {
            continue;
        }

        if trimmed.starts_with('#') {
            if let Some((id, title)) = parse_task_heading(trimmed) {
                if let Some(task) = current.take() {
                    tasks.push(finalize_section_task(task, independent_ids));
                }
                current = Some(ExecutionTask {
                    id,
                    title,
                    body: String::new(),
                    dependencies: Vec::new(),
                    independent: false,
                });
                continue;
            }
        }

        if let Some(task) = current.as_mut() {
            if let Some(value) = trimmed.strip_prefix("- Dependências:") {
                task.dependencies.extend(extract_task_ids(value));
            }
            task.body.push_str(line);
            task.body.push('\n');
        }
    }

    if let Some(task) = current.take() {
        tasks.push(finalize_section_task(task, independent_ids));
    }

    tasks
}

fn is_task_detail_section(heading: &str) -> bool {
    matches!(
        heading,
        "backlog" | "prompts agent" | "prompts de execução" | "prompts de execucao"
    )
}

fn merge_table_and_section_tasks(
    mut table_tasks: Vec<ExecutionTask>,
    section_tasks: Vec<ExecutionTask>,
) -> Vec<ExecutionTask> {
    let details_by_id: BTreeMap<String, ExecutionTask> = section_tasks
        .into_iter()
        .map(|task| (task.id.clone(), task))
        .collect();

    for task in &mut table_tasks {
        let Some(detail) = details_by_id.get(&task.id) else {
            continue;
        };
        if !detail.body.trim().is_empty() {
            task.body = detail.body.clone();
        }
        let mut seen: BTreeSet<String> = task.dependencies.iter().cloned().collect();
        for dep in &detail.dependencies {
            if seen.insert(dep.clone()) {
                task.dependencies.push(dep.clone());
            }
        }
        task.independent = task.independent || (task.dependencies.is_empty() && detail.independent);
    }

    table_tasks
}

fn finalize_section_task(
    mut task: ExecutionTask,
    independent_ids: &HashSet<String>,
) -> ExecutionTask {
    task.body = task.body.trim().to_string();
    task.independent = independent_ids.contains(&task.id) || task.dependencies.is_empty();
    task
}

fn parse_task_heading(line: &str) -> Option<(String, String)> {
    let heading = line.trim_start_matches('#').trim();
    let (id, end) = task_id_at_start(heading)?;
    let title = heading[end..]
        .trim()
        .trim_start_matches(['', '-', ':', ''])
        .trim()
        .to_string();
    if title.is_empty() {
        None
    } else {
        Some((id, title))
    }
}

fn task_id_at_start(input: &str) -> Option<(String, usize)> {
    let trimmed = input.trim_start();
    let offset = input.len().saturating_sub(trimmed.len());
    for prefix in ["TSK-", "T-"] {
        if !trimmed.starts_with(prefix) {
            continue;
        }
        let digits: String = trimmed[prefix.len()..]
            .chars()
            .take_while(|ch| ch.is_ascii_digit())
            .collect();
        if digits.is_empty() {
            continue;
        }
        let end = offset + prefix.len() + digits.len();
        return Some((format!("{prefix}{digits}"), end));
    }
    None
}

fn extract_task_ids(input: &str) -> Vec<String> {
    let mut ids = Vec::new();
    let mut seen = HashSet::new();
    let chars = input.char_indices().collect::<Vec<_>>();
    for (idx, _) in &chars {
        let tail = &input[*idx..];
        if let Some((id, _)) = task_id_at_start(tail) {
            if seen.insert(id.clone()) {
                ids.push(id);
            }
        }
    }
    ids
}

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

pub fn capture_workspace_snapshot(
    root: &Path,
    artifact_dir_to_exclude: Option<&Path>,
) -> std::io::Result<WorkspaceSnapshot> {
    let mut files = BTreeMap::new();
    let artifact_dir = artifact_dir_to_exclude.and_then(|path| path.canonicalize().ok());
    let root_canonical = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());

    for entry in WalkDir::new(root)
        .follow_links(false)
        .into_iter()
        .filter_entry(|entry| {
            !should_skip_entry(entry.path(), &root_canonical, artifact_dir.as_deref())
        })
    {
        let entry = entry?;
        if !entry.file_type().is_file() {
            continue;
        }
        let metadata = entry.metadata()?;
        let rel = match entry.path().strip_prefix(root) {
            Ok(rel) => normalize_path(rel),
            Err(_) => continue,
        };
        if metadata.len() > MAX_TRACKED_FILE_BYTES {
            files.insert(rel, TrackedFile::BinaryOrLarge);
            continue;
        }
        let bytes = std::fs::read(entry.path())?;
        match String::from_utf8(bytes) {
            Ok(text) => {
                files.insert(rel, TrackedFile::Text(text));
            }
            Err(_) => {
                files.insert(rel, TrackedFile::BinaryOrLarge);
            }
        }
    }

    Ok(WorkspaceSnapshot { files })
}

fn should_skip_entry(path: &Path, root: &Path, artifact_dir: Option<&Path>) -> bool {
    let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
    if artifact_dir.is_some_and(|artifact_dir| canonical.starts_with(artifact_dir)) {
        return true;
    }
    let rel = canonical.strip_prefix(root).unwrap_or(&canonical);
    crate::runtime::optimization::path_has_ignored_component(rel)
}

fn normalize_path(path: &Path) -> String {
    path.components()
        .filter_map(|component| match component {
            Component::Normal(value) => Some(value.to_string_lossy().to_string()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("/")
}

pub fn diff_snapshots(before: &WorkspaceSnapshot, after: &WorkspaceSnapshot) -> DiffReport {
    let mut keys = BTreeSet::new();
    keys.extend(before.files.keys().cloned());
    keys.extend(after.files.keys().cloned());

    let mut changed_files = Vec::new();
    let mut summary = Vec::new();
    let mut patch = String::new();

    for path in keys {
        let old = before.files.get(&path);
        let new = after.files.get(&path);
        if old == new {
            continue;
        }
        changed_files.push(path.clone());
        match (old, new) {
            (None, Some(TrackedFile::Text(new_text))) => {
                summary.push(format!("A {path}"));
                append_unified_diff(&mut patch, &path, "", new_text);
            }
            (Some(TrackedFile::Text(old_text)), None) => {
                summary.push(format!("D {path}"));
                append_unified_diff(&mut patch, &path, old_text, "");
            }
            (Some(TrackedFile::Text(old_text)), Some(TrackedFile::Text(new_text))) => {
                summary.push(format!("M {path}"));
                append_unified_diff(&mut patch, &path, old_text, new_text);
            }
            (None, Some(TrackedFile::BinaryOrLarge)) => {
                summary.push(format!("A {path} (binário/grande)"));
                patch.push_str(&format!(
                    "diff -- {path}\nArquivo binário ou grande adicionado.\n\n"
                ));
            }
            (Some(TrackedFile::BinaryOrLarge), None) => {
                summary.push(format!("D {path} (binário/grande)"));
                patch.push_str(&format!(
                    "diff -- {path}\nArquivo binário ou grande removido.\n\n"
                ));
            }
            _ => {
                summary.push(format!("M {path} (binário/grande)"));
                patch.push_str(&format!(
                    "diff -- {path}\nArquivo binário ou grande alterado.\n\n"
                ));
            }
        }
    }

    DiffReport {
        changed_files,
        summary: summary.join("\n"),
        patch,
    }
}

fn append_unified_diff(out: &mut String, path: &str, old_text: &str, new_text: &str) {
    let diff = TextDiff::from_lines(old_text, new_text);
    let rendered = diff
        .unified_diff()
        .header(&format!("a/{path}"), &format!("b/{path}"))
        .to_string();
    if rendered.trim().is_empty() {
        return;
    }
    out.push_str(&rendered);
    if !out.ends_with('\n') {
        out.push('\n');
    }
    out.push('\n');
}

pub fn git_summary(root: &Path) -> Option<String> {
    if !root.join(".git").exists() {
        return None;
    }
    let status = std::process::Command::new("git")
        .arg("-C")
        .arg(root)
        .arg("status")
        .arg("--short")
        .output()
        .ok()?;
    if !status.status.success() {
        return None;
    }
    let diff_stat = std::process::Command::new("git")
        .arg("-C")
        .arg(root)
        .arg("diff")
        .arg("--stat")
        .output()
        .ok();
    let mut out = String::new();
    let status_text = String::from_utf8_lossy(&status.stdout);
    if status_text.trim().is_empty() {
        out.push_str("git status --short: sem alterações rastreadas.\n");
    } else {
        out.push_str("git status --short:\n");
        out.push_str(status_text.trim());
        out.push('\n');
    }
    if let Some(diff_stat) = diff_stat.filter(|output| output.status.success()) {
        let stat = String::from_utf8_lossy(&diff_stat.stdout);
        if !stat.trim().is_empty() {
            out.push_str("\ngit diff --stat:\n");
            out.push_str(stat.trim());
            out.push('\n');
        }
    }
    Some(out)
}

pub fn compose_execution_artifact(
    orchestration_name: &str,
    tasks: &[CompletedTask],
    git_summary: Option<&str>,
    partial: bool,
) -> String {
    let mut out = String::new();
    out.push_str(&format!("# Execution - {orchestration_name}\n\n"));
    out.push_str("## Rastreabilidade\n\n");
    out.push_str(&format!("- Orquestração: {orchestration_name}\n"));
    out.push_str(&format!("- Stage: `{}`\n", Stage::Execution.key()));
    out.push_str("- Modo TUI: execução real task-by-task com diff por task\n");
    out.push_str("- Fonte das tasks: `04-tasks.md`\n");
    out.push_str(&format!("- Total de tasks planejadas: {}\n", tasks.len()));
    out.push_str(&format!(
        "- Estado do artefato: {}\n\n",
        if partial {
            "execução parcial documentada"
        } else {
            "execução concluída e documentada"
        }
    ));

    out.push_str("## Tarefa\n\n");
    out.push_str("| Task | Status | Arquivos alterados |\n");
    out.push_str("|------|--------|--------------------|\n");
    for task in tasks {
        let files = if task.diff.changed_files.is_empty() {
            "nenhum".to_string()
        } else {
            task.diff.changed_files.join(", ")
        };
        out.push_str(&format!(
            "| {}{} | {} | {} |\n",
            escape_cell(&task.task.id),
            escape_cell(&task.task.title),
            task.status.label(),
            escape_cell(&files)
        ));
    }
    out.push('\n');

    out.push_str("## Resumo da implementação\n\n");
    for task in tasks {
        out.push_str(&format!("### {}{}\n\n", task.task.id, task.task.title));
        match task.status {
            TaskRunStatus::Succeeded => out.push_str("- Status: concluída.\n"),
            TaskRunStatus::Failed => out.push_str("- Status: falhou durante a execução.\n"),
            TaskRunStatus::Skipped => {
                out.push_str("- Status: pulada ao aceitar resultado parcial.\n")
            }
            TaskRunStatus::Pending | TaskRunStatus::Running => {
                out.push_str("- Status: não finalizada.\n")
            }
        }
        if let Some(result) = &task.result {
            out.push_str(&format!(
                "- Exit code: `{}` · duração: {}ms.\n",
                result.exit_code, result.duration_ms
            ));
        }
        if !task.report.trim().is_empty() {
            out.push('\n');
            out.push_str(clip_chars(task.report.trim(), 4_000).trim());
            out.push_str("\n\n");
        } else if !task.trace.is_empty() {
            out.push_str("\nTrace final:\n");
            for item in task
                .trace
                .iter()
                .rev()
                .take(5)
                .collect::<Vec<_>>()
                .into_iter()
                .rev()
            {
                out.push_str(&format!("- {item}\n"));
            }
            out.push('\n');
        }
    }

    out.push_str("## Arquivos alterados\n\n");
    let files = tasks
        .iter()
        .flat_map(|task| task.diff.changed_files.iter().cloned())
        .collect::<BTreeSet<_>>();
    if files.is_empty() {
        out.push_str("- Nenhum arquivo alterado foi detectado pelo snapshot da TUI.\n\n");
    } else {
        for file in files {
            out.push_str(&format!("- `{file}`\n"));
        }
        out.push('\n');
    }
    if let Some(git_summary) = git_summary.filter(|value| !value.trim().is_empty()) {
        out.push_str("### Resumo Git\n\n```text\n");
        out.push_str(git_summary.trim());
        out.push_str("\n```\n\n");
    }

    out.push_str("## Testes e evidências\n\n");
    for task in tasks {
        out.push_str(&format!("- `{}`: {}.\n", task.task.id, task.status.label()));
    }
    out.push('\n');

    out.push_str("## Riscos e pendências\n\n");
    let failed = tasks
        .iter()
        .filter(|task| task.status == TaskRunStatus::Failed)
        .collect::<Vec<_>>();
    if failed.is_empty() && !partial {
        out.push_str("- Nenhuma falha automática registrada na execução das tasks.\n\n");
    } else {
        if partial {
            out.push_str("- Resultado parcial aceito no TUI; revisar tasks não concluídas antes de aprovar para produção.\n");
        }
        for task in failed {
            out.push_str(&format!(
                "- `{}` — {} falhou; revisar relatório e diff antes de prosseguir.\n",
                task.task.id, task.task.title
            ));
        }
        out.push('\n');
    }

    out.push_str("## Diff por task\n\n");
    for task in tasks {
        out.push_str(&format!("### {}{}\n\n", task.task.id, task.task.title));
        if task.diff.is_empty() {
            out.push_str("- Sem diff detectado para esta task.\n\n");
            continue;
        }
        out.push_str("Resumo:\n\n```text\n");
        out.push_str(task.diff.summary.trim());
        out.push_str("\n```\n\n");
        out.push_str("Patch:\n\n```diff\n");
        out.push_str(clip_chars(&task.diff.patch, MAX_PATCH_CHARS_PER_TASK).trim());
        out.push_str("\n```\n\n");
    }

    ensure_required_sections(&mut out);
    out
}

fn ensure_required_sections(out: &mut String) {
    for section in [
        "Rastreabilidade",
        "Tarefa",
        "Resumo da implementação",
        "Arquivos alterados",
        "Testes e evidências",
        "Riscos e pendências",
    ] {
        if !out.contains(&format!("## {section}\n")) {
            out.push_str(&format!("## {section}\n\n"));
            out.push_str("- Seção preservada para compatibilidade com o contrato do artefato.\n\n");
        }
    }
}

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

fn clip_chars(input: &str, max: usize) -> String {
    if input.chars().count() <= max {
        return input.to_string();
    }
    let mut out = input.chars().take(max).collect::<String>();
    out.push_str("\n[conteúdo truncado]");
    out
}

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

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

### TSK-01 — Validar contrato real

- Objetivo: mapear arquivos

### TSK-02 — Implementar ordenação

- Dependências: TSK-01
- Objetivo: alterar listagem
";
        let tasks = parse_execution_tasks(md);
        assert_eq!(tasks.len(), 2);
        assert_eq!(tasks[0].id, "TSK-01");
        assert_eq!(tasks[0].title, "Validar contrato real");
        assert!(tasks[0].independent);
        assert_eq!(tasks[1].dependencies, vec!["TSK-01"]);
        assert!(!tasks[1].independent);
    }

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

| ID | Título | Estimativa | Dependências |
|----|--------|------------|--------------|
| T-01 | Base | P | — |
| T-02 | UI | P | T-01 |

### Tasks independentes entre si

- T-02 pode rodar em paralelo.
";
        let tasks = parse_execution_tasks(md);
        assert_eq!(tasks.len(), 2);
        assert_eq!(tasks[0].id, "T-01");
        assert!(tasks[0].independent);
        assert!(tasks[1].independent);
    }

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

| ID | Título | Estimativa | Dependências | Arquivos |
|----|--------|------------|--------------|----------|
| T-01 | Base | P | — | src/base.rs |
| T-02 | UI | P | T-01 | src/ui.rs |

## Prompts Agent

### T-02 — UI detalhada

- Dependências: T-01
- Objetivo: implementar a interface principal.

#### Prompt Agent
Execute apenas a UI, rode testes focados e reporte evidências.
";
        let tasks = parse_execution_tasks(md);
        assert_eq!(tasks.len(), 2);
        assert_eq!(tasks[1].id, "T-02");
        assert_eq!(tasks[1].title, "UI");
        assert_eq!(tasks[1].dependencies, vec!["T-01"]);
        assert!(tasks[1].body.contains("Prompt Agent"));
        assert!(tasks[1].body.contains("Execute apenas a UI"));
    }

    #[test]
    fn snapshot_diff_detects_added_modified_removed_files_without_git() {
        let dir = tempfile::tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(dir.path().join("src/a.txt"), "one\n").unwrap();
        fs::write(dir.path().join("src/remove.txt"), "bye\n").unwrap();
        let before = capture_workspace_snapshot(dir.path(), None).unwrap();

        fs::write(dir.path().join("src/a.txt"), "one\ntwo\n").unwrap();
        fs::write(dir.path().join("src/new.txt"), "new\n").unwrap();
        fs::remove_file(dir.path().join("src/remove.txt")).unwrap();
        fs::create_dir_all(dir.path().join("node_modules/pkg")).unwrap();
        fs::write(dir.path().join("node_modules/pkg/ignored.txt"), "ignored").unwrap();
        let after = capture_workspace_snapshot(dir.path(), None).unwrap();

        let diff = diff_snapshots(&before, &after);
        assert!(diff.summary.contains("M src/a.txt"));
        assert!(diff.summary.contains("A src/new.txt"));
        assert!(diff.summary.contains("D src/remove.txt"));
        assert!(!diff.summary.contains("node_modules"));
        assert!(diff.patch.contains("--- a/src/a.txt"));
    }

    #[test]
    fn snapshot_excludes_artifact_dir() {
        let dir = tempfile::tempdir().unwrap();
        let artifact = dir.path().join("docs/feat");
        fs::create_dir_all(&artifact).unwrap();
        fs::write(artifact.join("06-execution.md"), "ignored").unwrap();

        let snapshot = capture_workspace_snapshot(dir.path(), Some(&artifact)).unwrap();

        assert!(snapshot.files.is_empty());
    }

    #[test]
    fn compose_execution_artifact_contains_required_sections_and_diff() {
        let task = ExecutionTask {
            id: "TSK-01".to_string(),
            title: "Alterar lista".to_string(),
            body: String::new(),
            dependencies: Vec::new(),
            independent: true,
        };
        let artifact = compose_execution_artifact(
            "Minha Feature",
            &[CompletedTask {
                task,
                status: TaskRunStatus::Succeeded,
                report: "Implementado.".to_string(),
                diff: DiffReport {
                    changed_files: vec!["src/a.rs".to_string()],
                    summary: "M src/a.rs".to_string(),
                    patch: "--- a/src/a.rs\n+++ b/src/a.rs\n".to_string(),
                },
                result: None,
                trace: Vec::new(),
            }],
            Some("git status --short:\n M src/a.rs"),
            false,
        );
        assert!(artifact.contains("## Rastreabilidade"));
        assert!(artifact.contains("## Diff por task"));
        assert!(artifact.contains("src/a.rs"));
    }
}