batty-cli 0.11.63

Supervised agent execution for software teams
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
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
//! Dependency graph visualization for board tasks.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt::Write as _;
use std::path::Path;

use anyhow::{Result, bail};

use crate::task::{Task, load_tasks_from_dir};

/// Output format for dependency graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DepsFormat {
    Tree,
    Flat,
    Dot,
}

/// A node in the dependency graph.
#[derive(Debug)]
struct TaskNode {
    id: u32,
    title: String,
    status: String,
    priority: String,
    depends_on: Vec<u32>,
}

impl TaskNode {
    fn from_task(task: &Task) -> Self {
        Self {
            id: task.id,
            title: task.title.clone(),
            status: task.status.clone(),
            priority: task.priority.clone(),
            depends_on: task.depends_on.clone(),
        }
    }

    fn truncated_title(&self, max_len: usize) -> String {
        if self.title.len() <= max_len {
            self.title.clone()
        } else {
            format!("{}...", &self.title[..max_len - 3])
        }
    }

    fn status_indicator(&self) -> &str {
        match self.status.as_str() {
            "done" => "[x]",
            "in-progress" => "[>]",
            "review" => "[R]",
            "todo" => "[ ]",
            "backlog" => "[-]",
            "blocked" => "[!]",
            _ => "[?]",
        }
    }

    fn is_incomplete(&self) -> bool {
        self.status != "done"
    }
}

/// Build and render the dependency graph.
pub fn render_deps(board_dir: &Path, format: DepsFormat) -> Result<String> {
    let tasks_dir = board_dir.join("tasks");
    if !tasks_dir.is_dir() {
        bail!("no tasks directory found at {}", tasks_dir.display());
    }

    let tasks = load_tasks_from_dir(&tasks_dir)?;
    let nodes: BTreeMap<u32, TaskNode> = tasks
        .iter()
        .map(|t| (t.id, TaskNode::from_task(t)))
        .collect();

    // Check for cycles before rendering
    if let Some(cycle) = detect_cycle(&nodes) {
        let cycle_str = cycle
            .iter()
            .map(|id| format!("#{id}"))
            .collect::<Vec<_>>()
            .join(" -> ");
        bail!("dependency cycle detected: {cycle_str}");
    }

    match format {
        DepsFormat::Tree => render_tree(&nodes),
        DepsFormat::Flat => render_flat(&nodes),
        DepsFormat::Dot => render_dot(&nodes),
    }
}

pub(crate) fn detect_cycle_for_tasks(tasks: &[Task]) -> Option<Vec<u32>> {
    let nodes: BTreeMap<u32, TaskNode> = tasks
        .iter()
        .map(|task| (task.id, TaskNode::from_task(task)))
        .collect();
    detect_cycle(&nodes)
}

/// Detect cycles using DFS. Returns the cycle path if found.
fn detect_cycle(nodes: &BTreeMap<u32, TaskNode>) -> Option<Vec<u32>> {
    let mut visited = HashSet::new();
    let mut in_stack = HashSet::new();
    let mut path = Vec::new();

    for &id in nodes.keys() {
        if !visited.contains(&id) {
            if let Some(cycle) = dfs_cycle(id, nodes, &mut visited, &mut in_stack, &mut path) {
                return Some(cycle);
            }
        }
    }
    None
}

fn dfs_cycle(
    id: u32,
    nodes: &BTreeMap<u32, TaskNode>,
    visited: &mut HashSet<u32>,
    in_stack: &mut HashSet<u32>,
    path: &mut Vec<u32>,
) -> Option<Vec<u32>> {
    visited.insert(id);
    in_stack.insert(id);
    path.push(id);

    if let Some(node) = nodes.get(&id) {
        for &dep in &node.depends_on {
            if !nodes.contains_key(&dep) {
                continue; // skip references to non-existent tasks
            }
            if in_stack.contains(&dep) {
                // Found a cycle — extract it
                let cycle_start = path.iter().position(|&x| x == dep).unwrap();
                let mut cycle = path[cycle_start..].to_vec();
                cycle.push(dep);
                return Some(cycle);
            }
            if !visited.contains(&dep) {
                if let Some(cycle) = dfs_cycle(dep, nodes, visited, in_stack, path) {
                    return Some(cycle);
                }
            }
        }
    }

    path.pop();
    in_stack.remove(&id);
    None
}

/// Render tree format: roots at top, children indented below.
fn render_tree(nodes: &BTreeMap<u32, TaskNode>) -> Result<String> {
    // Build reverse map: parent -> children (tasks that depend on parent)
    let mut children: BTreeMap<u32, BTreeSet<u32>> = BTreeMap::new();
    let mut has_parent = HashSet::new();

    for node in nodes.values() {
        for &dep in &node.depends_on {
            if nodes.contains_key(&dep) {
                children.entry(dep).or_default().insert(node.id);
                has_parent.insert(node.id);
            }
        }
    }

    // Roots are tasks with no dependencies (or deps pointing to non-existent tasks)
    let roots: Vec<u32> = nodes
        .keys()
        .filter(|id| !has_parent.contains(id))
        .copied()
        .collect();

    if roots.is_empty() && !nodes.is_empty() {
        // All tasks have parents — shouldn't happen without cycles
        return Ok("(no root tasks found)\n".to_string());
    }

    let mut out = String::new();

    // Show blocked chains header
    let blocked = find_blocked_chains(nodes);
    if !blocked.is_empty() {
        writeln!(out, "Blocked chains:").unwrap();
        for chain in &blocked {
            let parts: Vec<String> = chain
                .iter()
                .map(|&id| {
                    let node = &nodes[&id];
                    format!("#{} {}", id, node.truncated_title(30))
                })
                .collect();
            writeln!(out, "  {} (waiting)", parts.join(" -> ")).unwrap();
        }
        writeln!(out).unwrap();
    }

    // Show critical path
    let critical = find_critical_path(nodes);
    if critical.len() > 1 {
        let parts: Vec<String> = critical.iter().map(|&id| format!("#{id}")).collect();
        writeln!(
            out,
            "Critical path ({}): {}",
            critical.len(),
            parts.join(" -> ")
        )
        .unwrap();
        writeln!(out).unwrap();
    }

    // Render tree
    writeln!(out, "Dependency tree:").unwrap();
    for &root in &roots {
        render_tree_node(root, nodes, &children, &mut out, "", true);
    }

    Ok(out)
}

fn render_tree_node(
    id: u32,
    nodes: &BTreeMap<u32, TaskNode>,
    children: &BTreeMap<u32, BTreeSet<u32>>,
    out: &mut String,
    prefix: &str,
    is_last: bool,
) {
    let Some(node) = nodes.get(&id) else { return };

    let connector = if prefix.is_empty() {
        ""
    } else if is_last {
        "└── "
    } else {
        "├── "
    };

    let priority_tag = if node.priority.is_empty() {
        String::new()
    } else {
        format!(" ({})", node.priority)
    };

    writeln!(
        out,
        "{prefix}{connector}{} #{} {}{}",
        node.status_indicator(),
        node.id,
        node.truncated_title(50),
        priority_tag,
    )
    .unwrap();

    let child_ids: Vec<u32> = children
        .get(&id)
        .map(|s| s.iter().copied().collect())
        .unwrap_or_default();

    let child_prefix = if prefix.is_empty() {
        String::new()
    } else if is_last {
        format!("{prefix}    ")
    } else {
        format!("{prefix}")
    };

    for (i, &child) in child_ids.iter().enumerate() {
        let child_is_last = i == child_ids.len() - 1;
        render_tree_node(child, nodes, children, out, &child_prefix, child_is_last);
    }
}

/// Render flat format: just dependency pairs.
fn render_flat(nodes: &BTreeMap<u32, TaskNode>) -> Result<String> {
    let mut out = String::new();
    let mut has_deps = false;

    for node in nodes.values() {
        for &dep in &node.depends_on {
            if nodes.contains_key(&dep) {
                writeln!(out, "#{} -> #{}", node.id, dep).unwrap();
                has_deps = true;
            }
        }
    }

    if !has_deps {
        writeln!(out, "(no dependencies)").unwrap();
    }
    Ok(out)
}

/// Render graphviz DOT format.
fn render_dot(nodes: &BTreeMap<u32, TaskNode>) -> Result<String> {
    let mut out = String::new();
    writeln!(out, "digraph deps {{").unwrap();
    writeln!(out, "  rankdir=BT;").unwrap();
    writeln!(out, "  node [shape=box];").unwrap();

    for node in nodes.values() {
        if node.depends_on.is_empty() && !nodes.values().any(|n| n.depends_on.contains(&node.id)) {
            continue; // skip isolated nodes
        }

        let color = match node.status.as_str() {
            "done" => "green",
            "in-progress" => "yellow",
            "review" => "orange",
            "todo" | "backlog" => "white",
            _ => "gray",
        };

        writeln!(
            out,
            "  t{} [label=\"#{} {}\" style=filled fillcolor={}];",
            node.id,
            node.id,
            node.truncated_title(30).replace('"', "\\\""),
            color,
        )
        .unwrap();
    }

    writeln!(out).unwrap();

    for node in nodes.values() {
        for &dep in &node.depends_on {
            if nodes.contains_key(&dep) {
                writeln!(out, "  t{} -> t{};", node.id, dep).unwrap();
            }
        }
    }

    writeln!(out, "}}").unwrap();
    Ok(out)
}

/// Find chains where an incomplete task is blocked on another incomplete task.
fn find_blocked_chains(nodes: &BTreeMap<u32, TaskNode>) -> Vec<Vec<u32>> {
    let mut chains = Vec::new();

    for node in nodes.values() {
        if !node.is_incomplete() {
            continue;
        }
        for &dep in &node.depends_on {
            if let Some(dep_node) = nodes.get(&dep) {
                if dep_node.is_incomplete() {
                    chains.push(vec![node.id, dep]);
                }
            }
        }
    }

    chains.sort();
    chains
}

/// Find the longest chain of incomplete tasks (critical path).
fn find_critical_path(nodes: &BTreeMap<u32, TaskNode>) -> Vec<u32> {
    let mut memo: HashMap<u32, Vec<u32>> = HashMap::new();
    let mut best = Vec::new();

    for &id in nodes.keys() {
        let path = longest_path(id, nodes, &mut memo);
        if path.len() > best.len() {
            best = path;
        }
    }

    best
}

fn longest_path(
    id: u32,
    nodes: &BTreeMap<u32, TaskNode>,
    memo: &mut HashMap<u32, Vec<u32>>,
) -> Vec<u32> {
    if let Some(cached) = memo.get(&id) {
        return cached.clone();
    }

    let Some(node) = nodes.get(&id) else {
        return Vec::new();
    };

    if !node.is_incomplete() {
        memo.insert(id, Vec::new());
        return Vec::new();
    }

    let mut best_child = Vec::new();
    for &dep in &node.depends_on {
        if nodes.contains_key(&dep) {
            let child_path = longest_path(dep, nodes, memo);
            if child_path.len() > best_child.len() {
                best_child = child_path;
            }
        }
    }

    let mut path = vec![id];
    path.extend(best_child);
    memo.insert(id, path.clone());
    path
}

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

    fn write_task(
        dir: &Path,
        id: u32,
        title: &str,
        status: &str,
        priority: &str,
        depends_on: &[u32],
    ) {
        let deps = if depends_on.is_empty() {
            "depends_on: []".to_string()
        } else {
            let items: Vec<String> = depends_on.iter().map(|d| format!("  - {d}")).collect();
            format!("depends_on:\n{}", items.join("\n"))
        };
        let content = format!(
            "---\nid: {id}\ntitle: {title}\nstatus: {status}\npriority: {priority}\n{deps}\nclass: standard\n---\n\nDescription.\n"
        );
        fs::write(dir.join(format!("{id:03}-task.md")), content).unwrap();
    }

    fn setup_board(tasks: &[(u32, &str, &str, &str, &[u32])]) -> TempDir {
        let tmp = TempDir::new().unwrap();
        let tasks_dir = tmp.path().join("tasks");
        fs::create_dir_all(&tasks_dir).unwrap();
        for &(id, title, status, priority, deps) in tasks {
            write_task(&tasks_dir, id, title, status, priority, deps);
        }
        tmp
    }

    #[test]
    fn empty_board() {
        let tmp = TempDir::new().unwrap();
        let tasks_dir = tmp.path().join("tasks");
        fs::create_dir_all(&tasks_dir).unwrap();

        let output = render_deps(tmp.path(), DepsFormat::Tree).unwrap();
        assert!(output.contains("Dependency tree:"));

        let flat = render_deps(tmp.path(), DepsFormat::Flat).unwrap();
        assert!(flat.contains("(no dependencies)"));
    }

    #[test]
    fn no_dependencies() {
        let tmp = setup_board(&[
            (1, "Task A", "todo", "high", &[]),
            (2, "Task B", "todo", "medium", &[]),
        ]);

        let flat = render_deps(tmp.path(), DepsFormat::Flat).unwrap();
        assert!(flat.contains("(no dependencies)"));

        let tree = render_deps(tmp.path(), DepsFormat::Tree).unwrap();
        assert!(tree.contains("#1"));
        assert!(tree.contains("#2"));
    }

    #[test]
    fn linear_chain() {
        let tmp = setup_board(&[
            (1, "Foundation", "done", "high", &[]),
            (2, "Build on foundation", "in-progress", "high", &[1]),
            (3, "Final step", "todo", "medium", &[2]),
        ]);

        let tree = render_deps(tmp.path(), DepsFormat::Tree).unwrap();
        assert!(tree.contains("#1"));
        assert!(tree.contains("#2"));
        assert!(tree.contains("#3"));
        assert!(tree.contains("Foundation"));

        let flat = render_deps(tmp.path(), DepsFormat::Flat).unwrap();
        assert!(flat.contains("#2 -> #1"));
        assert!(flat.contains("#3 -> #2"));
    }

    #[test]
    fn diamond_dependencies() {
        let tmp = setup_board(&[
            (1, "Root", "done", "high", &[]),
            (2, "Left", "todo", "medium", &[1]),
            (3, "Right", "todo", "medium", &[1]),
            (4, "Join", "todo", "low", &[2, 3]),
        ]);

        let flat = render_deps(tmp.path(), DepsFormat::Flat).unwrap();
        assert!(flat.contains("#2 -> #1"));
        assert!(flat.contains("#3 -> #1"));
        assert!(flat.contains("#4 -> #2"));
        assert!(flat.contains("#4 -> #3"));
    }

    #[test]
    fn cycle_detection() {
        let tmp = setup_board(&[
            (1, "Task A", "todo", "high", &[2]),
            (2, "Task B", "todo", "high", &[1]),
        ]);

        let result = render_deps(tmp.path(), DepsFormat::Tree);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("cycle"));
    }

    #[test]
    fn blocked_chains_shown() {
        let tmp = setup_board(&[
            (1, "Blocker", "todo", "high", &[]),
            (2, "Blocked task", "todo", "medium", &[1]),
        ]);

        let tree = render_deps(tmp.path(), DepsFormat::Tree).unwrap();
        assert!(tree.contains("Blocked chains:"));
        assert!(tree.contains("#2"));
        assert!(tree.contains("#1"));
    }

    #[test]
    fn done_deps_not_blocked() {
        let tmp = setup_board(&[
            (1, "Done task", "done", "high", &[]),
            (2, "Ready task", "todo", "medium", &[1]),
        ]);

        let tree = render_deps(tmp.path(), DepsFormat::Tree).unwrap();
        assert!(!tree.contains("Blocked chains:"));
    }

    #[test]
    fn critical_path_shown() {
        let tmp = setup_board(&[
            (1, "Step 1", "todo", "high", &[]),
            (2, "Step 2", "todo", "high", &[1]),
            (3, "Step 3", "todo", "high", &[2]),
        ]);

        let tree = render_deps(tmp.path(), DepsFormat::Tree).unwrap();
        assert!(tree.contains("Critical path (3)"));
        assert!(tree.contains("#3 -> #2 -> #1"));
    }

    #[test]
    fn dot_format() {
        let tmp = setup_board(&[
            (1, "Root", "done", "high", &[]),
            (2, "Child", "todo", "medium", &[1]),
        ]);

        let dot = render_deps(tmp.path(), DepsFormat::Dot).unwrap();
        assert!(dot.contains("digraph deps {"));
        assert!(dot.contains("t2 -> t1;"));
        assert!(dot.contains("fillcolor=green"));
        assert!(dot.contains("fillcolor=white"));
        assert!(dot.contains("}"));
    }

    #[test]
    fn title_truncation() {
        let node = TaskNode {
            id: 1,
            title: "A very long task title that should be truncated to fit within limits"
                .to_string(),
            status: "todo".to_string(),
            priority: "high".to_string(),
            depends_on: vec![],
        };
        let truncated = node.truncated_title(50);
        assert!(truncated.len() <= 50);
        assert!(truncated.ends_with("..."));
    }

    #[test]
    fn status_indicators() {
        let statuses = vec![
            ("done", "[x]"),
            ("in-progress", "[>]"),
            ("review", "[R]"),
            ("todo", "[ ]"),
            ("backlog", "[-]"),
            ("blocked", "[!]"),
            ("unknown", "[?]"),
        ];

        for (status, expected) in statuses {
            let node = TaskNode {
                id: 1,
                title: "Test".to_string(),
                status: status.to_string(),
                priority: "".to_string(),
                depends_on: vec![],
            };
            assert_eq!(node.status_indicator(), expected, "status={status}");
        }
    }

    #[test]
    fn missing_tasks_dir_errors() {
        let tmp = TempDir::new().unwrap();
        let result = render_deps(tmp.path(), DepsFormat::Tree);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("no tasks directory")
        );
    }

    #[test]
    fn deps_to_nonexistent_tasks_ignored() {
        let tmp = setup_board(&[(1, "Task", "todo", "high", &[999])]);

        // Should not error — just skip the nonexistent dep
        let flat = render_deps(tmp.path(), DepsFormat::Flat).unwrap();
        assert!(flat.contains("(no dependencies)"));
    }

    #[test]
    fn three_node_cycle_detected() {
        let tmp = setup_board(&[
            (1, "A", "todo", "high", &[3]),
            (2, "B", "todo", "high", &[1]),
            (3, "C", "todo", "high", &[2]),
        ]);

        let result = render_deps(tmp.path(), DepsFormat::Flat);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("cycle"));
    }
}