beads_rust 0.5.7

Agent-first issue tracker (SQLite + JSONL)
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! Epic command implementation.

use crate::cli::{EpicCloseEligibleArgs, EpicCommands, EpicStatusArgs};
use crate::config;
use crate::error::Result;
use crate::format::sanitize_terminal_inline;
use crate::model::{EpicStatus, IssueType, Status};
use crate::output::{OutputContext, OutputMode};
use crate::storage::{IssueUpdate, ListFilters, SqliteStorage};
use chrono::Utc;
use crossterm::style::Stylize;
use rich_rust::prelude::*;
use serde::Serialize;

/// Execute the epic command.
///
/// # Errors
///
/// Returns an error if database operations fail.
pub fn execute(
    command: &EpicCommands,
    json: bool,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
) -> Result<()> {
    match command {
        EpicCommands::Status(args) => execute_status(args, json, cli, ctx),
        EpicCommands::CloseEligible(args) => execute_close_eligible(args, json, cli, ctx),
    }
}

/// Execute a read-only epic command using storage that was already opened by the caller.
///
/// Returns `Ok(false)` when the command needs the normal mutating path.
///
/// # Errors
///
/// Returns an error if database operations fail.
pub fn execute_with_storage_ctx(
    command: &EpicCommands,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
    storage_ctx: &config::OpenStorageResult,
) -> Result<bool> {
    match command {
        EpicCommands::Status(args) => {
            execute_status_with_storage_ctx(args, cli, ctx, storage_ctx)?;
            Ok(true)
        }
        EpicCommands::CloseEligible(_) => Ok(false),
    }
}

fn execute_status(
    args: &EpicStatusArgs,
    _json: bool,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
) -> Result<()> {
    let beads_dir = config::discover_beads_dir_with_cli(cli)?;
    let storage_ctx = config::open_storage_with_cli(&beads_dir, cli)?;
    execute_status_with_storage_ctx(args, cli, ctx, &storage_ctx)
}

fn execute_status_with_storage_ctx(
    args: &EpicStatusArgs,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
    storage_ctx: &config::OpenStorageResult,
) -> Result<()> {
    let config_layer = storage_ctx.load_config(cli)?;
    let use_color = config::should_use_color(&config_layer);

    let mut epics = load_epic_statuses(&storage_ctx.storage)?;
    if args.eligible_only {
        epics.retain(|e| e.eligible_for_close);
    }

    if ctx.is_toon() {
        ctx.toon(&epics);
        return Ok(());
    }

    if ctx.is_json() {
        ctx.json_pretty(&epics);
        return Ok(());
    }

    if ctx.is_quiet() {
        return Ok(());
    }

    if epics.is_empty() {
        if matches!(ctx.mode(), OutputMode::Rich) {
            render_empty_epics_rich(ctx);
        } else {
            println!("No open epics found");
        }
        return Ok(());
    }

    if matches!(ctx.mode(), OutputMode::Rich) {
        render_epic_status_list_rich(&epics, ctx);
    } else {
        for epic_status in &epics {
            render_epic_status(epic_status, use_color);
        }
    }

    Ok(())
}

#[derive(Debug, Serialize)]
struct CloseEligibleResult {
    closed: Vec<String>,
    count: usize,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    warnings: Vec<crate::close_policy::WorkflowCapacityWarning>,
}

fn execute_close_eligible(
    args: &EpicCloseEligibleArgs,
    _json: bool,
    cli: &config::CliOverrides,
    ctx: &OutputContext,
) -> Result<()> {
    let beads_dir = config::discover_beads_dir_with_cli(cli)?;
    let mut storage_ctx = config::open_storage_with_cli(&beads_dir, cli)?;
    let config_layer = storage_ctx.load_config(cli)?;
    let actor = config::resolve_actor(&config_layer);

    let storage = &mut storage_ctx.storage;
    let mut epics = load_epic_statuses(storage)?;
    epics.retain(|e| e.eligible_for_close);

    if args.dry_run {
        if ctx.is_toon() {
            ctx.toon(&epics);
        } else if ctx.is_json() {
            ctx.json_pretty(&epics);
        } else if ctx.is_quiet() {
            return Ok(());
        } else if matches!(ctx.mode(), OutputMode::Rich) {
            render_dry_run_rich(&epics, ctx);
        } else {
            println!("Would close {} epic(s):", epics.len());
            for epic_status in &epics {
                let id = format_epic_id(&epic_status.epic.id, false);
                let title = format_epic_title(&epic_status.epic.title, false);
                println!("  - {id}: {title}");
            }
        }
        return Ok(());
    }

    if epics.is_empty() {
        if ctx.is_toon() {
            let result = CloseEligibleResult {
                closed: Vec::new(),
                count: 0,
                warnings: Vec::new(),
            };
            ctx.toon(&result);
        } else if ctx.is_json() {
            let result = CloseEligibleResult {
                closed: Vec::new(),
                count: 0,
                warnings: Vec::new(),
            };
            ctx.json_pretty(&result);
        } else if ctx.is_quiet() {
            return Ok(());
        } else if matches!(ctx.mode(), OutputMode::Rich) {
            render_no_eligible_rich(ctx);
        } else {
            println!("No epics eligible for closure");
        }
        return Ok(());
    }

    let now = Utc::now();
    let reason = "All children completed";
    let closed_ids = close_eligible_epics_atomically(
        storage,
        &epics,
        &actor,
        now,
        reason,
        args.transition_comment.as_deref(),
    )?;
    let capacity_warnings = storage.take_capacity_warnings();

    storage_ctx.flush_no_db_if_dirty()?;

    if ctx.is_toon() {
        let result = CloseEligibleResult {
            closed: closed_ids.clone(),
            count: closed_ids.len(),
            warnings: capacity_warnings.clone(),
        };
        ctx.toon(&result);
    } else if ctx.is_json() {
        let result = CloseEligibleResult {
            closed: closed_ids.clone(),
            count: closed_ids.len(),
            warnings: capacity_warnings.clone(),
        };
        ctx.json_pretty(&result);
    } else if ctx.is_quiet() {
        return Ok(());
    } else if matches!(ctx.mode(), OutputMode::Rich) {
        render_close_result_rich(&closed_ids, ctx);
        for warning in &capacity_warnings {
            ctx.warning(&warning.to_string());
        }
    } else {
        println!("✓ Closed {} epic(s)", closed_ids.len());
        for id in &closed_ids {
            println!("  - {}", format_epic_id(id, false));
        }
        for warning in &capacity_warnings {
            ctx.warning(&warning.to_string());
        }
    }
    Ok(())
}

fn close_eligible_epics_atomically(
    storage: &mut SqliteStorage,
    epics: &[EpicStatus],
    actor: &str,
    now: chrono::DateTime<Utc>,
    reason: &str,
    transition_comment: Option<&str>,
) -> Result<Vec<String>> {
    let updates = epics
        .iter()
        .map(|epic_status| {
            (
                epic_status.epic.id.clone(),
                IssueUpdate {
                    status: Some(Status::Closed),
                    closed_at: Some(Some(now)),
                    close_reason: Some(Some(reason.to_string())),
                    transition_comment: transition_comment.map(str::to_string),
                    ..Default::default()
                },
            )
        })
        .collect::<Vec<_>>();

    // The shared storage chokepoint preflights required fields, transition
    // gates, and final-state capacity for the entire batch before mutating any
    // epic. It then records comments, status/close events, and dirty markers in
    // that same transaction.
    storage.update_issues_atomically(&updates, actor)?;
    Ok(updates.into_iter().map(|(id, _)| id).collect())
}

fn load_epic_statuses(storage: &SqliteStorage) -> Result<Vec<EpicStatus>> {
    let filters = ListFilters {
        types: Some(vec![IssueType::Epic]),
        include_closed: false,
        include_deferred: true,
        ..Default::default()
    };
    let epics = storage.list_issues(&filters)?;
    if epics.is_empty() {
        return Ok(Vec::new());
    }

    let counts = storage.get_epic_counts()?;

    let mut statuses = Vec::new();
    for epic in epics {
        let (total_children, closed_children) = counts.get(&epic.id).copied().unwrap_or((0, 0));
        let eligible_for_close = total_children > 0 && closed_children == total_children;

        statuses.push(EpicStatus {
            epic,
            total_children,
            closed_children,
            eligible_for_close,
        });
    }

    Ok(statuses)
}

fn render_epic_status(epic_status: &EpicStatus, use_color: bool) {
    let total = epic_status.total_children;
    let closed = epic_status.closed_children;
    let percentage = completion_percentage(closed, total);
    let status_icon = render_status_icon(epic_status.eligible_for_close, percentage, use_color);

    let id = format_epic_id(&epic_status.epic.id, use_color);
    let title = format_epic_title(&epic_status.epic.title, use_color);

    println!("{status_icon} {id} {title}");
    println!("   Progress: {closed}/{total} children closed ({percentage}%)");
    if epic_status.eligible_for_close {
        let line = if use_color {
            "Eligible for closure".green().to_string()
        } else {
            "Eligible for closure".to_string()
        };
        println!("   {line}");
    }
    println!();
}

fn render_status_icon(eligible: bool, percentage: usize, use_color: bool) -> String {
    if eligible {
        if use_color {
            "".green().to_string()
        } else {
            "".to_string()
        }
    } else if percentage > 0 {
        if use_color {
            "".yellow().to_string()
        } else {
            "".to_string()
        }
    } else {
        "".to_string()
    }
}

fn format_epic_id(id: &str, use_color: bool) -> String {
    let id = sanitize_terminal_inline(id);
    if use_color {
        id.as_ref().cyan().to_string()
    } else {
        id.into_owned()
    }
}

fn format_epic_title(title: &str, use_color: bool) -> String {
    let title = sanitize_terminal_inline(title);
    if use_color {
        title.as_ref().bold().to_string()
    } else {
        title.into_owned()
    }
}

// ─────────────────────────────────────────────────────────────
// Rich Output Rendering
// ─────────────────────────────────────────────────────────────

/// Render the epic status list with rich formatting.
fn render_epic_status_list_rich(epics: &[EpicStatus], ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");

    for (i, epic_status) in epics.iter().enumerate() {
        if i > 0 {
            content.append("\n");
        }

        let total = epic_status.total_children;
        let closed = epic_status.closed_children;
        let percentage = completion_percentage(closed, total);

        // Status icon
        if epic_status.eligible_for_close {
            content.append_styled("", theme.success.clone());
        } else if percentage > 0 {
            content.append_styled("", theme.warning.clone());
        } else {
            content.append_styled("", theme.dimmed.clone());
        }

        // ID and title
        content.append_styled(
            sanitize_terminal_inline(&epic_status.epic.id).as_ref(),
            theme.issue_id.clone(),
        );
        content.append(" ");
        content.append_styled(
            sanitize_terminal_inline(&epic_status.epic.title).as_ref(),
            theme.emphasis.clone(),
        );
        content.append("\n");

        // Progress bar
        content.append("   ");
        render_progress_bar(&mut content, closed, total, percentage, theme);
        content.append("\n");

        // Eligible notice
        if epic_status.eligible_for_close {
            content.append("   ");
            content.append_styled("Ready for closure", theme.success.clone());
            content.append("\n");
        }
    }

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Epic Status", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

/// Render a progress bar inline.
fn render_progress_bar(
    content: &mut Text,
    closed: usize,
    total: usize,
    percentage: usize,
    theme: &crate::output::Theme,
) {
    let bar_width = 20;
    let filled = completion_bar_filled_width(closed, total, bar_width);
    let empty = bar_width - filled;

    content.append_styled("[", theme.dimmed.clone());
    if filled > 0 {
        content.append_styled(&"".repeat(filled), theme.success.clone());
    }
    if empty > 0 {
        content.append_styled(&"".repeat(empty), theme.dimmed.clone());
    }
    content.append_styled("] ", theme.dimmed.clone());

    content.append(&format!("{closed}/{total} "));
    content.append_styled(&format!("({percentage}%)"), theme.dimmed.clone());
}

fn completion_percentage(closed: usize, total: usize) -> usize {
    if total == 0 {
        return 0;
    }

    let closed = closed.min(total) as u128;
    let total = total as u128;
    usize::try_from((closed * 100) / total).unwrap_or(100)
}

fn completion_bar_filled_width(closed: usize, total: usize, bar_width: usize) -> usize {
    if total == 0 {
        return 0;
    }

    let closed = closed.min(total) as u128;
    let total = total as u128;
    let bar_width = bar_width as u128;
    usize::try_from((closed * bar_width) / total).unwrap_or(usize::MAX)
}

/// Render empty epics message with rich formatting.
fn render_empty_epics_rich(ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");
    content.append_styled("No open epics found", theme.dimmed.clone());
    content.append("\n");

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Epic Status", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

/// Render no eligible epics message with rich formatting.
fn render_no_eligible_rich(ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");
    content.append_styled("No epics eligible for closure", theme.dimmed.clone());
    content.append("\n");

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Epic Close", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

/// Render dry-run results with rich formatting.
fn render_dry_run_rich(epics: &[EpicStatus], ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");

    content.append_styled("⚡ Dry-run mode ", theme.warning.clone());
    content.append_styled("(no changes will be made)\n\n", theme.dimmed.clone());

    content.append(&format!(
        "Would close {} epic{}:\n\n",
        epics.len(),
        if epics.len() == 1 { "" } else { "s" }
    ));

    for epic_status in epics {
        content.append_styled("", theme.dimmed.clone());
        content.append_styled(
            sanitize_terminal_inline(&epic_status.epic.id).as_ref(),
            theme.issue_id.clone(),
        );
        content.append(" ");
        content.append(sanitize_terminal_inline(&epic_status.epic.title).as_ref());
        content.append("\n");
    }

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled(
            "Epic Close (Dry Run)",
            theme.panel_title.clone(),
        ))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

/// Render close results with rich formatting.
fn render_close_result_rich(closed_ids: &[String], ctx: &OutputContext) {
    let console = Console::default();
    let theme = ctx.theme();
    let width = ctx.width();

    let mut content = Text::new("");

    content.append_styled("", theme.success.clone());
    content.append_styled(
        &format!(
            "Closed {} epic{}\n",
            closed_ids.len(),
            if closed_ids.len() == 1 { "" } else { "s" }
        ),
        theme.success.clone(),
    );

    if !closed_ids.is_empty() {
        content.append("\n");
        for id in closed_ids {
            content.append_styled("", theme.dimmed.clone());
            content.append_styled(
                sanitize_terminal_inline(id).as_ref(),
                theme.issue_id.clone(),
            );
            content.append("\n");
        }
    }

    let panel = Panel::from_rich_text(&content, width)
        .title(Text::styled("Epic Close", theme.panel_title.clone()))
        .box_style(theme.box_style);

    console.print_renderable(&panel);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Issue, Priority, Status};
    use crate::storage::IssueUpdate;
    use chrono::TimeZone;

    fn base_issue(id: &str, title: &str, issue_type: IssueType, status: Status) -> Issue {
        Issue {
            id: id.to_string(),
            content_hash: None,
            title: title.to_string(),
            description: None,
            design: None,
            acceptance_criteria: None,
            notes: None,
            status,
            priority: Priority::MEDIUM,
            issue_type,
            assignee: None,
            owner: None,
            estimated_minutes: None,
            created_at: Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap(),
            created_by: None,
            updated_at: Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap(),
            closed_at: None,
            close_reason: None,
            closed_by_session: None,
            due_at: None,
            defer_until: None,
            external_ref: None,
            source_system: None,
            source_repo: None,
            source_repo_path: None,
            agent_context: None,
            deleted_at: None,
            deleted_by: None,
            delete_reason: None,
            original_type: None,
            compaction_level: None,
            compacted_at: None,
            compacted_at_commit: None,
            original_size: None,
            sender: None,
            ephemeral: false,
            pinned: false,
            is_template: false,
            labels: vec![],
            dependencies: vec![],
            comments: vec![],
        }
    }

    fn find_epic<'a>(epics: &'a [EpicStatus], id: &str) -> Option<&'a EpicStatus> {
        epics.iter().find(|e| e.epic.id == id)
    }

    #[test]
    fn epic_status_tracks_children_and_eligibility() {
        let mut storage = SqliteStorage::open_memory().unwrap();

        let epic = base_issue("bd-epic-1", "Epic", IssueType::Epic, Status::Open);
        let task1 = base_issue("bd-task-1", "Task 1", IssueType::Task, Status::Open);
        let task2 = base_issue("bd-task-2", "Task 2", IssueType::Task, Status::Open);

        storage.create_issue(&epic, "tester").unwrap();
        storage.create_issue(&task1, "tester").unwrap();
        storage.create_issue(&task2, "tester").unwrap();
        storage
            .add_dependency("bd-task-1", "bd-epic-1", "parent-child", "tester")
            .unwrap();
        storage
            .add_dependency("bd-task-2", "bd-epic-1", "parent-child", "tester")
            .unwrap();

        let epics = load_epic_statuses(&storage).unwrap();
        let epic_status = find_epic(&epics, "bd-epic-1").expect("epic not found");
        assert_eq!(epic_status.total_children, 2);
        assert_eq!(epic_status.closed_children, 0);
        assert!(!epic_status.eligible_for_close);

        let update = IssueUpdate {
            status: Some(Status::Closed),
            closed_at: Some(Some(Utc::now())),
            close_reason: Some(Some("Done".to_string())),
            ..Default::default()
        };
        storage
            .update_issue("bd-task-1", &update, "tester")
            .unwrap();

        let epics = load_epic_statuses(&storage).unwrap();
        let epic_status = find_epic(&epics, "bd-epic-1").expect("epic not found");
        assert_eq!(epic_status.total_children, 2);
        assert_eq!(epic_status.closed_children, 1);
        assert!(!epic_status.eligible_for_close);

        storage
            .update_issue("bd-task-2", &update, "tester")
            .unwrap();
        let epics = load_epic_statuses(&storage).unwrap();
        let epic_status = find_epic(&epics, "bd-epic-1").expect("epic not found");
        assert_eq!(epic_status.total_children, 2);
        assert_eq!(epic_status.closed_children, 2);
        assert!(epic_status.eligible_for_close);
    }

    #[test]
    fn epic_status_childless_epic_not_eligible() {
        let mut storage = SqliteStorage::open_memory().unwrap();
        let epic = base_issue("bd-epic-2", "Childless", IssueType::Epic, Status::Open);
        storage.create_issue(&epic, "tester").unwrap();

        let epics = load_epic_statuses(&storage).unwrap();
        let epic_status = find_epic(&epics, "bd-epic-2").expect("epic not found");
        assert_eq!(epic_status.total_children, 0);
        assert_eq!(epic_status.closed_children, 0);
        assert!(!epic_status.eligible_for_close);
    }

    #[test]
    fn close_eligible_epics_rolls_back_entire_batch_on_capacity_failure() {
        let mut storage = SqliteStorage::open_memory().unwrap();
        for suffix in ["a", "b"] {
            let epic_id = format!("bd-epic-cap-{suffix}");
            let child_id = format!("bd-child-cap-{suffix}");
            storage
                .create_issue(
                    &base_issue(&epic_id, "Epic", IssueType::Epic, Status::Open),
                    "tester",
                )
                .unwrap();
            let mut child = base_issue(&child_id, "Child", IssueType::Task, Status::Closed);
            child.closed_at = Some(Utc::now());
            child.close_reason = Some("Done".to_string());
            storage.create_issue(&child, "tester").unwrap();
            storage
                .add_dependency(&child_id, &epic_id, "parent-child", "tester")
                .unwrap();
        }

        let epics: Vec<EpicStatus> = load_epic_statuses(&storage)
            .unwrap()
            .into_iter()
            .filter(|epic| epic.eligible_for_close)
            .collect();
        assert_eq!(epics.len(), 2);

        let mut policy = crate::close_policy::CapacityPolicy::default();
        policy.statuses.insert(
            "closed".to_string(),
            crate::close_policy::CapacityLimit {
                soft: None,
                hard: Some(3),
            },
        );
        storage.set_workflow_capacity_policy(policy);

        let error = close_eligible_epics_atomically(
            &mut storage,
            &epics,
            "tester",
            Utc::now(),
            "All children completed",
            None,
        )
        .unwrap_err();
        assert!(matches!(
            error,
            crate::error::BeadsError::WorkflowCapacityExceeded { .. }
        ));
        for id in ["bd-epic-cap-a", "bd-epic-cap-b"] {
            assert_eq!(storage.get_issue(id).unwrap().unwrap().status, Status::Open);
        }

        let mut policy = crate::close_policy::CapacityPolicy::default();
        policy.statuses.insert(
            "closed".to_string(),
            crate::close_policy::CapacityLimit {
                soft: Some(3),
                hard: Some(5),
            },
        );
        storage.set_workflow_capacity_policy(policy);

        let closed = close_eligible_epics_atomically(
            &mut storage,
            &epics,
            "tester",
            Utc::now(),
            "All children completed",
            None,
        )
        .unwrap();
        assert_eq!(closed.len(), 2);

        let warnings = storage.take_capacity_warnings();
        assert_eq!(
            warnings.len(),
            1,
            "one final-state warning is emitted per affected capacity"
        );
        assert_eq!(warnings[0].capacity_kind, "status");
        assert_eq!(warnings[0].capacity_name, "closed");
        assert_eq!(warnings[0].current, 2);
        assert_eq!(warnings[0].prospective, 4);
        assert_eq!(warnings[0].soft_limit, 3);
    }

    #[test]
    fn epic_status_includes_deferred_epics() {
        let mut storage = SqliteStorage::open_memory().unwrap();
        let epic = base_issue(
            "bd-epic-deferred",
            "Deferred epic",
            IssueType::Epic,
            Status::Deferred,
        );
        storage.create_issue(&epic, "tester").unwrap();

        let epics = load_epic_statuses(&storage).unwrap();
        let epic_status = find_epic(&epics, "bd-epic-deferred").expect("deferred epic not found");
        assert_eq!(epic_status.total_children, 0);
        assert_eq!(epic_status.closed_children, 0);
        assert!(!epic_status.eligible_for_close);
    }

    #[test]
    fn epic_display_fields_sanitize_terminal_controls() {
        let id = format_epic_id("bd-epic\x1b]52;c;bad\x07", false);
        let title = format_epic_title("Title\x1b[2J\rreset", false);

        assert!(!id.contains('\x1b'));
        assert!(!id.contains('\x07'));
        assert!(!title.contains('\x1b'));
        assert!(!title.contains('\r'));
        assert_eq!(id, "bd-epic\\u{1b}]52;c;bad\\u{7}");
        assert_eq!(title, "Title\\u{1b}[2J\\rreset");
    }

    #[test]
    fn epic_completion_helpers_handle_zero_and_inconsistent_counts() {
        assert_eq!(completion_percentage(0, 0), 0);
        assert_eq!(completion_percentage(2, 4), 50);
        assert_eq!(completion_percentage(usize::MAX, 2), 100);
        assert_eq!(completion_percentage(usize::MAX, usize::MAX), 100);

        assert_eq!(completion_bar_filled_width(0, 0, 20), 0);
        assert_eq!(completion_bar_filled_width(1, 4, 20), 5);
        assert_eq!(completion_bar_filled_width(usize::MAX, 2, 20), 20);
        assert_eq!(completion_bar_filled_width(usize::MAX, usize::MAX, 20), 20);
    }

    #[test]
    fn epic_progress_bar_clamps_inconsistent_closed_counts() {
        let theme = crate::output::Theme::default();
        let mut content = Text::new("");

        render_progress_bar(&mut content, 3, 2, completion_percentage(3, 2), &theme);

        let rendered = content.plain();
        assert_eq!(rendered.matches('').count(), 20);
        assert!(!rendered.contains(''));
        assert!(rendered.contains("3/2 (100%)"));
    }
}