beads_viewer_rust 0.2.1

Spec-first Rust port of beads_viewer (bv) — graph-aware triage for beads issue trackers (CLI binary: bvr)
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
use std::collections::{HashMap, HashSet};

use serde::Serialize;

use crate::analysis::graph::IssueGraph;

#[derive(Debug, Clone, Serialize)]
pub struct ExecutionItem {
    pub id: String,
    pub title: String,
    pub status: String,
    pub priority: i32,
    pub score: f64,
    pub unblocks: Vec<String>,
    pub claim_command: String,
    pub show_command: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExecutionTrack {
    #[serde(rename = "track_id")]
    pub id: String,
    pub items: Vec<ExecutionItem>,
    pub reason: String,
}

#[derive(Debug, Clone, Serialize, Default)]
pub struct PlanSummary {
    pub track_count: usize,
    pub actionable_count: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unblocks_count: Option<usize>,
    pub highest_impact: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub impact_reason: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExecutionPlan {
    pub total_actionable: usize,
    pub total_blocked: usize,
    pub tracks: Vec<ExecutionTrack>,
    pub summary: PlanSummary,
}

fn unique_unblocks_count<'a>(items: impl IntoIterator<Item = &'a ExecutionItem>) -> usize {
    items
        .into_iter()
        .flat_map(|item| item.unblocks.iter().cloned())
        .collect::<HashSet<_>>()
        .len()
}

pub fn compute_execution_plan(
    graph: &IssueGraph,
    score_by_id: &HashMap<String, f64>,
) -> ExecutionPlan {
    let components = graph.connected_open_components();
    let actionable: HashSet<String> = graph.actionable_ids().into_iter().collect();

    let mut tracks = Vec::<ExecutionTrack>::new();
    let mut track_number: usize = 0;

    for component in &components {
        let mut items = Vec::<ExecutionItem>::new();

        for issue_id in component {
            if !actionable.contains(issue_id) {
                continue;
            }
            let Some(issue) = graph.issue(issue_id) else {
                continue;
            };

            let mut unblocks = graph
                .dependents(issue_id)
                .into_iter()
                .filter(|dependent_id| {
                    graph
                        .issue(dependent_id)
                        .is_some_and(crate::model::Issue::is_open_like)
                })
                .collect::<Vec<_>>();
            unblocks.sort();

            items.push(ExecutionItem {
                id: issue.id.clone(),
                title: issue.title.clone(),
                status: issue.status.clone(),
                priority: issue.priority,
                score: score_by_id.get(issue_id).copied().unwrap_or_default(),
                unblocks,
                claim_command: format!("br update {} --status=in_progress", issue.id),
                show_command: format!("br show {}", issue.id),
            });
        }

        items.sort_by(|left, right| {
            right
                .score
                .total_cmp(&left.score)
                .then_with(|| left.id.cmp(&right.id))
        });

        if items.is_empty() {
            continue;
        }

        // Build descriptive reason for this track.
        let total_unblocks = unique_unblocks_count(items.iter());
        let reason = if items.len() == 1 {
            let item = &items[0];
            if item.unblocks.is_empty() {
                "independent issue — can execute in parallel".to_string()
            } else {
                format!(
                    "completing {} unblocks {} issue(s)",
                    item.id,
                    item.unblocks.len()
                )
            }
        } else if total_unblocks > 0 {
            format!(
                "connected component of {} actionable items unblocking {} downstream issue(s)",
                items.len(),
                total_unblocks
            )
        } else {
            format!("connected component of {} independent items", items.len())
        };

        track_number += 1;
        tracks.push(ExecutionTrack {
            id: format!("track-{track_number}"),
            items,
            reason,
        });
    }

    tracks.sort_by(|left, right| {
        let left_score = left
            .items
            .first()
            .map(|item| item.score)
            .unwrap_or_default();
        let right_score = right
            .items
            .first()
            .map(|item| item.score)
            .unwrap_or_default();
        right_score
            .total_cmp(&left_score)
            .then_with(|| left.id.cmp(&right.id))
    });

    // Exclude in_progress issues from highest_impact to match legacy behavior:
    // the "highest impact" pick should surface new work, not work already claimed.
    let highest_impact_item = tracks
        .iter()
        .flat_map(|track| track.items.iter())
        .find(|item| {
            graph
                .issue(&item.id)
                .is_none_or(|issue| issue.normalized_status() != "in_progress")
        });

    let highest_impact = highest_impact_item.map(|item| item.id.clone());

    let impact_reason = highest_impact_item.map(|item| {
        let mut parts = Vec::new();
        parts.push(format!("score {:.2}", item.score));
        if !item.unblocks.is_empty() {
            parts.push(format!("unblocks {} issue(s)", item.unblocks.len()));
        }
        format!("highest impact: {} ({})", item.id, parts.join(", "))
    });

    let actionable_count: usize = tracks.iter().map(|track| track.items.len()).sum();
    let total_blocked = graph
        .issues
        .iter()
        .filter(|issue| issue.is_open_like() && !graph.open_blockers(&issue.id).is_empty())
        .count();
    let total_unblocks = unique_unblocks_count(tracks.iter().flat_map(|track| track.items.iter()));
    let track_count = tracks.len();

    ExecutionPlan {
        total_actionable: actionable_count,
        total_blocked,
        tracks,
        summary: PlanSummary {
            track_count,
            actionable_count,
            unblocks_count: Some(total_unblocks),
            highest_impact,
            impact_reason,
        },
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::analysis::graph::IssueGraph;
    use crate::model::{Dependency, Issue};

    use super::{compute_execution_plan, unique_unblocks_count};

    #[test]
    fn plan_groups_by_components() {
        let issues = vec![
            Issue {
                id: "A".to_string(),
                title: "A".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                priority: 1,
                ..Issue::default()
            },
            Issue {
                id: "B".to_string(),
                title: "B".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                priority: 1,
                ..Issue::default()
            },
            Issue {
                id: "C".to_string(),
                title: "C".to_string(),
                status: "blocked".to_string(),
                issue_type: "task".to_string(),
                dependencies: vec![Dependency {
                    depends_on_id: "A".to_string(),
                    dep_type: "blocks".to_string(),
                    ..Dependency::default()
                }],
                ..Issue::default()
            },
        ];

        let graph = IssueGraph::build(&issues);
        let mut scores = HashMap::new();
        scores.insert("A".to_string(), 0.8);
        scores.insert("B".to_string(), 0.7);

        let plan = compute_execution_plan(&graph, &scores);
        assert_eq!(plan.summary.actionable_count, 2);
        assert!(plan.summary.track_count >= 1);
        assert_eq!(plan.summary.track_count, plan.tracks.len());
    }

    #[test]
    fn plan_track_reason_describes_component() {
        let issues = vec![
            Issue {
                id: "A".to_string(),
                title: "Root".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                priority: 1,
                ..Issue::default()
            },
            Issue {
                id: "B".to_string(),
                title: "Depends".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                dependencies: vec![Dependency {
                    depends_on_id: "A".to_string(),
                    dep_type: "blocks".to_string(),
                    ..Dependency::default()
                }],
                ..Issue::default()
            },
            Issue {
                id: "C".to_string(),
                title: "Independent".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                ..Issue::default()
            },
        ];

        let graph = IssueGraph::build(&issues);
        let mut scores = HashMap::new();
        scores.insert("A".to_string(), 0.8);
        scores.insert("C".to_string(), 0.5);

        let plan = compute_execution_plan(&graph, &scores);

        // Track with A should mention unblocking.
        let track_a = plan
            .tracks
            .iter()
            .find(|t| t.items.iter().any(|i| i.id == "A"));
        assert!(track_a.is_some());
        assert!(
            !track_a.unwrap().reason.is_empty(),
            "track reason should not be empty"
        );

        // Independent track should mention independence.
        let track_c = plan
            .tracks
            .iter()
            .find(|t| t.items.iter().any(|i| i.id == "C"));
        assert!(track_c.is_some());
        assert!(track_c.unwrap().reason.contains("independent"));
    }

    #[test]
    fn plan_impact_reason_present_when_tracks_exist() {
        let issues = vec![Issue {
            id: "A".to_string(),
            title: "Only".to_string(),
            status: "open".to_string(),
            issue_type: "task".to_string(),
            priority: 1,
            ..Issue::default()
        }];
        let graph = IssueGraph::build(&issues);
        let mut scores = HashMap::new();
        scores.insert("A".to_string(), 0.9);

        let plan = compute_execution_plan(&graph, &scores);
        assert!(plan.summary.impact_reason.is_some());
        let reason = plan.summary.impact_reason.unwrap();
        assert!(reason.contains("A"), "should mention the issue ID");
        assert!(reason.contains("0.90"), "should mention the score");
    }

    #[test]
    fn plan_impact_reason_none_when_no_tracks() {
        let issues: Vec<Issue> = vec![];
        let graph = IssueGraph::build(&issues);
        let plan = compute_execution_plan(&graph, &HashMap::new());
        assert!(plan.summary.impact_reason.is_none());
    }

    #[test]
    fn plan_reason_serializes_to_json() {
        let issues = vec![Issue {
            id: "A".to_string(),
            title: "A".to_string(),
            status: "open".to_string(),
            issue_type: "task".to_string(),
            ..Issue::default()
        }];
        let graph = IssueGraph::build(&issues);
        let mut scores = HashMap::new();
        scores.insert("A".to_string(), 0.5);
        let plan = compute_execution_plan(&graph, &scores);

        let json = serde_json::to_string(&plan).unwrap();
        assert!(json.contains("\"reason\""));
        assert!(json.contains("\"impact_reason\""));
    }

    #[test]
    fn plan_summary_track_count_reflects_non_empty_tracks_only() {
        let issues = vec![
            Issue {
                id: "A".to_string(),
                title: "A".to_string(),
                status: "blocked".to_string(),
                issue_type: "task".to_string(),
                dependencies: vec![Dependency {
                    depends_on_id: "B".to_string(),
                    dep_type: "blocks".to_string(),
                    ..Dependency::default()
                }],
                ..Issue::default()
            },
            Issue {
                id: "B".to_string(),
                title: "B".to_string(),
                status: "blocked".to_string(),
                issue_type: "task".to_string(),
                dependencies: vec![Dependency {
                    depends_on_id: "A".to_string(),
                    dep_type: "blocks".to_string(),
                    ..Dependency::default()
                }],
                ..Issue::default()
            },
        ];

        let graph = IssueGraph::build(&issues);
        let scores = HashMap::new();
        let plan = compute_execution_plan(&graph, &scores);

        assert_eq!(plan.tracks.len(), 0);
        assert_eq!(plan.summary.track_count, 0);
        assert_eq!(plan.summary.actionable_count, 0);
        assert!(plan.summary.highest_impact.is_none());
    }

    #[test]
    fn plan_track_ids_are_contiguous() {
        // Create 3 components where the middle one has no actionable items,
        // so it gets skipped. Track IDs should still be contiguous (1, 2).
        let issues = vec![
            Issue {
                id: "A".to_string(),
                title: "A".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                priority: 1,
                ..Issue::default()
            },
            // B and C form a component where both are blocked (no actionable items).
            Issue {
                id: "B".to_string(),
                title: "B".to_string(),
                status: "blocked".to_string(),
                issue_type: "task".to_string(),
                dependencies: vec![Dependency {
                    depends_on_id: "C".to_string(),
                    dep_type: "blocks".to_string(),
                    ..Dependency::default()
                }],
                ..Issue::default()
            },
            Issue {
                id: "C".to_string(),
                title: "C".to_string(),
                status: "blocked".to_string(),
                issue_type: "task".to_string(),
                dependencies: vec![Dependency {
                    depends_on_id: "B".to_string(),
                    dep_type: "blocks".to_string(),
                    ..Dependency::default()
                }],
                ..Issue::default()
            },
            Issue {
                id: "D".to_string(),
                title: "D".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                priority: 1,
                ..Issue::default()
            },
        ];

        let graph = IssueGraph::build(&issues);
        let mut scores = HashMap::new();
        scores.insert("A".to_string(), 0.8);
        scores.insert("D".to_string(), 0.7);

        let plan = compute_execution_plan(&graph, &scores);
        assert_eq!(plan.tracks.len(), 2);
        // Track IDs should be contiguous: track-1, track-2 (no gaps).
        let mut ids: Vec<&str> = plan.tracks.iter().map(|t| t.id.as_str()).collect();
        ids.sort();
        assert_eq!(ids, vec!["track-1", "track-2"]);
    }

    #[test]
    fn unique_unblocks_are_counted_once_in_summary_and_reason() {
        let issues = vec![
            Issue {
                id: "A".to_string(),
                title: "A".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                ..Issue::default()
            },
            Issue {
                id: "B".to_string(),
                title: "B".to_string(),
                status: "open".to_string(),
                issue_type: "task".to_string(),
                ..Issue::default()
            },
            Issue {
                id: "C".to_string(),
                title: "C".to_string(),
                status: "blocked".to_string(),
                issue_type: "task".to_string(),
                dependencies: vec![
                    Dependency {
                        depends_on_id: "A".to_string(),
                        dep_type: "blocks".to_string(),
                        ..Dependency::default()
                    },
                    Dependency {
                        depends_on_id: "B".to_string(),
                        dep_type: "blocks".to_string(),
                        ..Dependency::default()
                    },
                ],
                ..Issue::default()
            },
        ];

        let graph = IssueGraph::build(&issues);
        let mut scores = HashMap::new();
        scores.insert("A".to_string(), 0.8);
        scores.insert("B".to_string(), 0.7);

        let plan = compute_execution_plan(&graph, &scores);
        assert_eq!(plan.summary.unblocks_count, Some(1));
        assert_eq!(plan.tracks.len(), 1);
        assert!(
            plan.tracks[0]
                .reason
                .contains("unblocking 1 downstream issue")
        );
        assert_eq!(unique_unblocks_count(plan.tracks[0].items.iter()), 1);
    }
}