selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
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
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
#![allow(dead_code, unused_imports, unused_variables)]
//! Swarm Visualization System
//!
//! Terminal-based real-time visualization for multi-agent coordination.
//! Displays agent status panels, consensus history, and activity timelines
//! with animated progress bars and color-coded state indicators.

use std::fmt;
use std::time::{Duration, Instant};

use super::style::{Glyphs, SelfwareStyle};
use super::task_display::format_duration;

// ============================================================================
// Spinner Frames
// ============================================================================

/// Braille spinner frames for working agents.
const SPINNER_FRAMES: &[char] = &[
    '\u{280B}', //    '\u{2819}', //    '\u{2839}', //    '\u{2838}', //    '\u{283C}', //    '\u{2834}', //    '\u{2826}', //    '\u{2827}', //    '\u{2807}', //    '\u{280F}', //];

/// Progress bar fill character.
const BAR_FILL: char = '\u{2588}'; ///// Progress bar empty character.
const BAR_EMPTY: char = '\u{2591}'; //
// ============================================================================
// AgentState
// ============================================================================

/// The current operational state of a swarm agent.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentState {
    /// Agent is registered but has no active task.
    Idle,
    /// Agent is actively processing its task.
    Working,
    /// Agent is waiting for a consensus decision.
    Waiting,
    /// Agent has completed its task successfully.
    Done,
    /// Agent encountered an unrecoverable error.
    Failed,
}

impl fmt::Display for AgentState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Idle => write!(f, "Idle"),
            Self::Working => write!(f, "Working"),
            Self::Waiting => write!(f, "Waiting"),
            Self::Done => write!(f, "Done"),
            Self::Failed => write!(f, "Failed"),
        }
    }
}

// ============================================================================
// AgentStatus
// ============================================================================

/// Live status of a single agent in the swarm.
pub struct AgentStatus {
    /// Role label, e.g. "Architect", "Coder", "Tester".
    pub role: String,
    /// Current operational state.
    pub state: AgentState,
    /// Description of the task the agent is working on, if any.
    pub current_task: Option<String>,
    /// Progress from 0.0 (not started) to 1.0 (complete).
    pub progress: f32,
    /// Total tool invocations by this agent.
    pub tool_calls: u32,
    /// Total tokens consumed by this agent.
    pub tokens_used: u64,
    /// Timestamp of the most recent status update.
    pub last_update: Instant,
}

impl AgentStatus {
    /// Create a new agent status in the Idle state.
    fn new(role: &str) -> Self {
        Self {
            role: role.to_string(),
            state: AgentState::Idle,
            current_task: None,
            progress: 0.0,
            tool_calls: 0,
            tokens_used: 0,
            last_update: Instant::now(),
        }
    }

    /// Return the emoji glyph for this agent's role.
    fn role_icon(&self) -> &'static str {
        match self.role.as_str() {
            "Architect" => "\u{1F3D7}\u{FE0F} ",    // 🏗️
            "Coder" => "\u{1F4BB}",                 // 💻
            "Tester" => "\u{1F9EA}",                // 🧪
            "Reviewer" => "\u{1F4CB}",              // 📋
            "DevOps" => "\u{2699}\u{FE0F} ",        // ⚙️
            "Security" => "\u{1F512}",              // 🔒
            "Documenter" => "\u{1F4DD}",            // 📝
            "Performance" => "\u{26A1}",            //            "VisualCritic" => "\u{1F441}\u{FE0F} ", // 👁️
            _ => "\u{1F916}",                       // 🤖
        }
    }

    /// Return the state indicator suffix.
    fn state_suffix(&self) -> &'static str {
        match self.state {
            AgentState::Done => " \u{2713}",   //            AgentState::Failed => " \u{2717}", //            _ => "",
        }
    }
}

// ============================================================================
// ConsensusEvent
// ============================================================================

/// A recorded consensus decision among agents.
pub struct ConsensusEvent {
    /// When the consensus occurred.
    pub timestamp: Instant,
    /// The topic being decided.
    pub topic: String,
    /// The conflict resolution strategy used.
    pub strategy: String,
    /// Votes cast by each agent: (role, approved).
    pub votes: Vec<(String, bool)>,
    /// Whether the proposal was accepted.
    pub outcome: bool,
}

// ============================================================================
// TimelineEntry
// ============================================================================

/// A single entry in the activity timeline.
struct TimelineEntry {
    /// When this event occurred.
    timestamp: Instant,
    /// Human-readable description of the event.
    description: String,
}

// ============================================================================
// SwarmVisualization
// ============================================================================

/// Terminal-based real-time visualization for multi-agent swarm coordination.
///
/// Tracks agent statuses, consensus history, and an activity timeline,
/// then renders them as bordered terminal panels with animated progress
/// bars and color-coded state indicators.
pub struct SwarmVisualization {
    /// Status of each agent in the swarm, keyed by role name.
    agents: Vec<AgentStatus>,
    /// Historical record of consensus decisions.
    consensus_history: Vec<ConsensusEvent>,
    /// Activity timeline entries.
    timeline: Vec<TimelineEntry>,
    /// When the swarm was started.
    start_time: Instant,
    /// Monotonically increasing tick for spinner animation.
    tick: u64,
}

impl SwarmVisualization {
    /// Create a new, empty swarm visualization.
    pub fn new() -> Self {
        Self {
            agents: Vec::new(),
            consensus_history: Vec::new(),
            timeline: Vec::new(),
            start_time: Instant::now(),
            tick: 0,
        }
    }

    // ────────────────────────────────────────────────────────────────
    // Mutation helpers
    // ────────────────────────────────────────────────────────────────

    /// Register a new agent by role and return a mutable reference to it.
    pub fn add_agent(&mut self, role: &str) -> &mut AgentStatus {
        self.timeline.push(TimelineEntry {
            timestamp: Instant::now(),
            description: format!("{} joined swarm", role),
        });
        self.agents.push(AgentStatus::new(role));
        self.agents.last_mut().unwrap()
    }

    /// Update an existing agent's state, task, and progress.
    pub fn update_agent(
        &mut self,
        role: &str,
        state: AgentState,
        task: Option<&str>,
        progress: f32,
    ) {
        if let Some(agent) = self.agents.iter_mut().find(|a| a.role == role) {
            let old_state = agent.state;
            agent.state = state;
            agent.current_task = task.map(|t| t.to_string());
            agent.progress = progress.clamp(0.0, 1.0);
            agent.last_update = Instant::now();

            // Record notable state transitions in the timeline.
            if old_state != state {
                let desc = match state {
                    AgentState::Working => {
                        if let Some(t) = task {
                            format!("{} started {}", role, t)
                        } else {
                            format!("{} started working", role)
                        }
                    }
                    AgentState::Done => format!("{} completed", role),
                    AgentState::Failed => format!("{} failed", role),
                    AgentState::Waiting => format!("{} waiting for consensus", role),
                    AgentState::Idle => format!("{} went idle", role),
                };
                self.timeline.push(TimelineEntry {
                    timestamp: Instant::now(),
                    description: desc,
                });
            }
        }
    }

    /// Record a tool call by an agent and add it to the timeline.
    pub fn record_tool_call(&mut self, role: &str, tool: &str) {
        if let Some(agent) = self.agents.iter_mut().find(|a| a.role == role) {
            agent.tool_calls += 1;
            agent.last_update = Instant::now();
        }
        self.timeline.push(TimelineEntry {
            timestamp: Instant::now(),
            description: format!("{} \u{2192} {}", role, tool), //        });
    }

    /// Record a consensus event and add it to the timeline.
    pub fn record_consensus(&mut self, event: ConsensusEvent) {
        let summary = format!("Consensus: {}", event.topic);
        self.timeline.push(TimelineEntry {
            timestamp: event.timestamp,
            description: summary,
        });
        self.consensus_history.push(event);
    }

    /// Advance the animation tick (call periodically from a timer).
    pub fn advance_tick(&mut self) {
        self.tick = self.tick.wrapping_add(1);
    }

    // ────────────────────────────────────────────────────────────────
    // Rendering
    // ────────────────────────────────────────────────────────────────

    /// Render all panels: agent status, consensus log, and timeline.
    pub fn render_full(&self) -> String {
        let mut output = String::new();
        output.push_str(&self.render_agent_panel());
        output.push('\n');
        output.push_str(&self.render_consensus_panel());
        output.push('\n');
        output.push_str(&self.render_timeline_panel());
        output
    }

    /// Render a compact one-line summary of swarm status.
    ///
    /// Example: `Swarm: 3/5 active | Architect:80% Coder:done Tester:40%`
    pub fn render_compact(&self) -> String {
        let total = self.agents.len();
        let active = self
            .agents
            .iter()
            .filter(|a| a.state == AgentState::Working || a.state == AgentState::Waiting)
            .count();

        let mut parts: Vec<String> = Vec::new();
        for agent in &self.agents {
            let status = match agent.state {
                AgentState::Done => "done".to_string(),
                AgentState::Failed => "FAIL".to_string(),
                AgentState::Idle => "idle".to_string(),
                AgentState::Waiting => "wait".to_string(),
                AgentState::Working => format!("{}%", (agent.progress * 100.0) as u32),
            };
            parts.push(format!("{}:{}", agent.role, status));
        }

        format!("Swarm: {}/{} active | {}", active, total, parts.join(" "))
    }

    /// Render the progress bar for a single agent identified by role.
    ///
    /// Returns an empty string if the role is not found.
    pub fn render_agent_progress(&self, role: &str) -> String {
        if let Some(agent) = self.agents.iter().find(|a| a.role == role) {
            let bar = render_progress_bar(agent.progress, 10);
            let pct = format!("{}%", (agent.progress * 100.0) as u32);
            format!(
                "{} {} {} {} {}{}",
                agent.role_icon(),
                pad_right(&agent.role, 12),
                bar,
                pad_right(&pct, 4),
                agent.state,
                agent.state_suffix(),
            )
        } else {
            String::new()
        }
    }

    // ────────────────────────────────────────────────────────────────
    // Panel renderers (private)
    // ────────────────────────────────────────────────────────────────

    /// Render the agent status panel.
    fn render_agent_panel(&self) -> String {
        let inner_width: usize = 48;
        let h = Glyphs::horiz();
        let v = Glyphs::vert();

        let mut lines: Vec<String> = Vec::new();

        // Top border
        let title = " Swarm Status ";
        let title_len = title.chars().count();
        let remaining = inner_width.saturating_sub(title_len);
        lines.push(format!(
            "{}{}{}{}{}",
            Glyphs::corner_tl(),
            h,
            title,
            h.repeat(remaining.saturating_sub(1)),
            Glyphs::corner_tr(),
        ));

        // Empty line
        lines.push(format!("{} {:width$} {}", v, "", v, width = inner_width));

        // Agent lines
        for agent in &self.agents {
            let spinner = self.spinner_for(agent);
            let bar = render_progress_bar(agent.progress, 10);
            let pct = format!("{}%", (agent.progress * 100.0) as u32);
            let state_str = format!("{}{}", agent.state, agent.state_suffix());

            let content = format!(
                "{}  {} {} {} {}",
                spinner,
                pad_right(&agent.role, 12),
                bar,
                pad_right(&pct, 4),
                state_str,
            );
            // Pad to inner width
            let content_display_len = display_width(&content);
            let padding = inner_width.saturating_sub(content_display_len);
            lines.push(format!("{} {}{} {}", v, content, " ".repeat(padding), v));
        }

        // Empty line
        lines.push(format!("{} {:width$} {}", v, "", v, width = inner_width));

        // Consensus summary
        let decisions_made = self.consensus_history.len();
        let total_decisions = decisions_made + self.pending_consensus_count();
        let strategy = self.dominant_strategy();
        let consensus_line = format!(
            "Consensus: {}/{} decisions made",
            decisions_made, total_decisions
        );
        let strategy_line = format!("Strategy: {}", strategy);

        let c_pad = inner_width.saturating_sub(consensus_line.chars().count());
        lines.push(format!(
            "{} {}{} {}",
            v,
            consensus_line,
            " ".repeat(c_pad),
            v
        ));
        let s_pad = inner_width.saturating_sub(strategy_line.chars().count());
        lines.push(format!(
            "{} {}{} {}",
            v,
            strategy_line,
            " ".repeat(s_pad),
            v
        ));

        // Bottom border
        lines.push(format!(
            "{}{}{}",
            Glyphs::corner_bl(),
            h.repeat(inner_width + 2),
            Glyphs::corner_br(),
        ));

        lines.join("\n")
    }

    /// Render the consensus history panel.
    fn render_consensus_panel(&self) -> String {
        let inner_width: usize = 48;
        let h = Glyphs::horiz();
        let v = Glyphs::vert();

        let mut lines: Vec<String> = Vec::new();

        // Top border
        let title = " Consensus Log ";
        let title_len = title.chars().count();
        let remaining = inner_width.saturating_sub(title_len);
        lines.push(format!(
            "{}{}{}{}{}",
            Glyphs::corner_tl(),
            h,
            title,
            h.repeat(remaining.saturating_sub(1)),
            Glyphs::corner_tr(),
        ));

        if self.consensus_history.is_empty() {
            let msg = "No consensus events yet.";
            let pad = inner_width.saturating_sub(msg.len());
            lines.push(format!("{} {}{} {}", v, msg, " ".repeat(pad), v));
        } else {
            // Show last 5 consensus events
            let start = self.consensus_history.len().saturating_sub(5);
            for event in &self.consensus_history[start..] {
                let elapsed = event.timestamp.duration_since(self.start_time);
                let time_tag = format_elapsed_compact(elapsed);

                let topic_line = format!("[{}] {}", time_tag, event.topic);
                let t_pad = inner_width.saturating_sub(topic_line.chars().count());
                lines.push(format!("{} {}{} {}", v, topic_line, " ".repeat(t_pad), v,));

                // Votes line
                let votes_str: Vec<String> = event
                    .votes
                    .iter()
                    .map(|(role, approved)| {
                        let mark = if *approved { "\u{2713}" } else { "\u{2717}" };
                        format!("{}:{}", role, mark)
                    })
                    .collect();
                let outcome_mark = if event.outcome {
                    "\u{2192} approved"
                } else {
                    "\u{2192} rejected"
                };
                let votes_line = format!("       {} {}", votes_str.join(" "), outcome_mark);
                let vl_chars = votes_line.chars().count();
                let v_pad = inner_width.saturating_sub(vl_chars);
                lines.push(format!("{} {}{} {}", v, votes_line, " ".repeat(v_pad), v,));
            }
        }

        // Bottom border
        lines.push(format!(
            "{}{}{}",
            Glyphs::corner_bl(),
            h.repeat(inner_width + 2),
            Glyphs::corner_br(),
        ));

        lines.join("\n")
    }

    /// Render the activity timeline panel.
    fn render_timeline_panel(&self) -> String {
        let inner_width: usize = 48;
        let h = Glyphs::horiz();
        let v = Glyphs::vert();

        let mut lines: Vec<String> = Vec::new();

        // Top border
        let title = " Timeline ";
        let title_len = title.chars().count();
        let remaining = inner_width.saturating_sub(title_len);
        lines.push(format!(
            "{}{}{}{}{}",
            Glyphs::corner_tl(),
            h,
            title,
            h.repeat(remaining.saturating_sub(1)),
            Glyphs::corner_tr(),
        ));

        if self.timeline.is_empty() {
            let msg = "No activity yet.";
            let pad = inner_width.saturating_sub(msg.len());
            lines.push(format!("{} {}{} {}", v, msg, " ".repeat(pad), v));
        } else {
            // Show last 10 timeline entries
            let start = self.timeline.len().saturating_sub(10);
            for entry in &self.timeline[start..] {
                let elapsed = entry.timestamp.duration_since(self.start_time);
                let time_tag = format_elapsed_compact(elapsed);

                let line_content = format!("\u{25B8} {}  {}", time_tag, entry.description,);
                let lc_chars = line_content.chars().count();
                // Truncate if too long
                let display = if lc_chars > inner_width {
                    let truncated: String = line_content.chars().take(inner_width - 3).collect();
                    format!("{}...", truncated)
                } else {
                    line_content
                };
                let d_chars = display.chars().count();
                let pad = inner_width.saturating_sub(d_chars);
                lines.push(format!("{} {}{} {}", v, display, " ".repeat(pad), v));
            }
        }

        // Bottom border
        lines.push(format!(
            "{}{}{}",
            Glyphs::corner_bl(),
            h.repeat(inner_width + 2),
            Glyphs::corner_br(),
        ));

        lines.join("\n")
    }

    // ────────────────────────────────────────────────────────────────
    // Internal helpers
    // ────────────────────────────────────────────────────────────────

    /// Get the spinner character for an agent based on its state and the
    /// current tick.
    fn spinner_for(&self, agent: &AgentStatus) -> String {
        match agent.state {
            AgentState::Working => {
                let idx = (self.tick as usize) % SPINNER_FRAMES.len();
                format!("{}", SPINNER_FRAMES[idx])
            }
            _ => agent.role_icon().to_string(),
        }
    }

    /// Count agents currently waiting (a rough proxy for pending consensus).
    fn pending_consensus_count(&self) -> usize {
        self.agents
            .iter()
            .filter(|a| a.state == AgentState::Waiting)
            .count()
    }

    /// Determine the dominant conflict resolution strategy from history.
    fn dominant_strategy(&self) -> String {
        if self.consensus_history.is_empty() {
            return "none".to_string();
        }
        let mut counts: std::collections::HashMap<&str, usize> = std::collections::HashMap::new();
        for event in &self.consensus_history {
            *counts.entry(event.strategy.as_str()).or_insert(0) += 1;
        }
        counts
            .into_iter()
            .max_by_key(|(_, count)| *count)
            .map(|(strategy, _)| strategy.to_string())
            .unwrap_or_else(|| "none".to_string())
    }
}

impl Default for SwarmVisualization {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Free functions
// ============================================================================

/// Render a progress bar of the given width.
///
/// Example: `[████████░░] 80%`
pub fn render_progress_bar(progress: f32, width: usize) -> String {
    let clamped = progress.clamp(0.0, 1.0);
    let filled = (clamped * width as f32).round() as usize;
    let empty = width.saturating_sub(filled);

    format!(
        "[{}{}]",
        BAR_FILL.to_string().repeat(filled),
        BAR_EMPTY.to_string().repeat(empty),
    )
}

/// Format an elapsed duration as compact `M:SS`.
fn format_elapsed_compact(duration: Duration) -> String {
    let total_secs = duration.as_secs();
    let mins = total_secs / 60;
    let secs = total_secs % 60;
    format!("{}:{:02}", mins, secs)
}

/// Pad a string on the right to the given width.
fn pad_right(s: &str, width: usize) -> String {
    let len = s.chars().count();
    if len >= width {
        s.to_string()
    } else {
        format!("{}{}", s, " ".repeat(width - len))
    }
}

/// Estimate the display width of a string (each char counts as 1 for
/// simplicity; emoji may be wider in some terminals but this keeps
/// alignment predictable).
fn display_width(s: &str) -> usize {
    s.chars().count()
}

// ============================================================================
// Tests
// ============================================================================

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

    // ── Construction ────────────────────────────────────────────────

    #[test]
    fn test_new_visualization() {
        let viz = SwarmVisualization::new();
        assert!(viz.agents.is_empty());
        assert!(viz.consensus_history.is_empty());
        assert!(viz.timeline.is_empty());
        assert_eq!(viz.tick, 0);
    }

    #[test]
    fn test_default_impl() {
        let viz = SwarmVisualization::default();
        assert!(viz.agents.is_empty());
    }

    // ── Agent management ────────────────────────────────────────────

    #[test]
    fn test_add_agent() {
        let mut viz = SwarmVisualization::new();
        let agent = viz.add_agent("Architect");
        assert_eq!(agent.role, "Architect");
        assert_eq!(agent.state, AgentState::Idle);
        assert_eq!(agent.progress, 0.0);
        assert_eq!(agent.tool_calls, 0);
        assert_eq!(agent.tokens_used, 0);
        assert!(agent.current_task.is_none());
        assert_eq!(viz.agents.len(), 1);
        // Adding an agent creates a timeline entry
        assert_eq!(viz.timeline.len(), 1);
        assert!(viz.timeline[0].description.contains("Architect"));
    }

    #[test]
    fn test_add_multiple_agents() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Architect");
        viz.add_agent("Coder");
        viz.add_agent("Tester");
        assert_eq!(viz.agents.len(), 3);
        assert_eq!(viz.agents[0].role, "Architect");
        assert_eq!(viz.agents[1].role, "Coder");
        assert_eq!(viz.agents[2].role, "Tester");
    }

    // ── Agent updates ───────────────────────────────────────────────

    #[test]
    fn test_update_agent_state() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");
        viz.update_agent("Coder", AgentState::Working, Some("implementing API"), 0.5);

        let agent = &viz.agents[0];
        assert_eq!(agent.state, AgentState::Working);
        assert_eq!(agent.current_task.as_deref(), Some("implementing API"));
        assert!((agent.progress - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn test_update_agent_progress_clamped() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Tester");

        viz.update_agent("Tester", AgentState::Working, None, 1.5);
        assert!((viz.agents[0].progress - 1.0).abs() < f32::EPSILON);

        viz.update_agent("Tester", AgentState::Working, None, -0.5);
        assert!((viz.agents[0].progress - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_update_nonexistent_agent_is_noop() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");
        // Should not panic
        viz.update_agent("Ghost", AgentState::Working, None, 0.5);
        assert_eq!(viz.agents.len(), 1);
    }

    #[test]
    fn test_update_agent_creates_timeline_entry_on_state_change() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Architect");
        let initial_timeline_len = viz.timeline.len();

        viz.update_agent("Architect", AgentState::Working, Some("planning"), 0.1);
        assert_eq!(viz.timeline.len(), initial_timeline_len + 1);
        assert!(viz.timeline.last().unwrap().description.contains("started"));

        // Same state again should NOT add a timeline entry
        let len_before = viz.timeline.len();
        viz.update_agent(
            "Architect",
            AgentState::Working,
            Some("still planning"),
            0.3,
        );
        assert_eq!(viz.timeline.len(), len_before);

        // Transition to Done
        viz.update_agent("Architect", AgentState::Done, None, 1.0);
        assert!(viz
            .timeline
            .last()
            .unwrap()
            .description
            .contains("completed"));
    }

    #[test]
    fn test_update_agent_timeline_descriptions() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Security");

        viz.update_agent("Security", AgentState::Waiting, None, 0.0);
        assert!(viz.timeline.last().unwrap().description.contains("waiting"));

        viz.update_agent("Security", AgentState::Failed, None, 0.0);
        assert!(viz.timeline.last().unwrap().description.contains("failed"));

        viz.update_agent("Security", AgentState::Idle, None, 0.0);
        assert!(viz.timeline.last().unwrap().description.contains("idle"));
    }

    // ── Tool call recording ─────────────────────────────────────────

    #[test]
    fn test_record_tool_call() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");
        viz.record_tool_call("Coder", "file_write");
        viz.record_tool_call("Coder", "shell_exec");

        assert_eq!(viz.agents[0].tool_calls, 2);
        // Each tool call adds a timeline entry (plus 1 for add_agent)
        assert!(viz.timeline.len() >= 3);
        assert!(viz
            .timeline
            .iter()
            .any(|e| e.description.contains("file_write")));
    }

    #[test]
    fn test_record_tool_call_unknown_agent() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");
        // Should not panic; timeline still gets an entry
        let before = viz.timeline.len();
        viz.record_tool_call("Ghost", "file_read");
        assert_eq!(viz.timeline.len(), before + 1);
        // Agent tool count should not change
        assert_eq!(viz.agents[0].tool_calls, 0);
    }

    // ── Consensus recording ─────────────────────────────────────────

    #[test]
    fn test_record_consensus() {
        let mut viz = SwarmVisualization::new();
        let event = ConsensusEvent {
            timestamp: Instant::now(),
            topic: "REST vs GraphQL".to_string(),
            strategy: "majority".to_string(),
            votes: vec![
                ("Architect".to_string(), true),
                ("Coder".to_string(), true),
                ("Reviewer".to_string(), false),
            ],
            outcome: true,
        };
        viz.record_consensus(event);

        assert_eq!(viz.consensus_history.len(), 1);
        assert_eq!(viz.consensus_history[0].topic, "REST vs GraphQL");
        assert!(viz.consensus_history[0].outcome);
        // Also creates a timeline entry
        assert!(viz
            .timeline
            .iter()
            .any(|e| e.description.contains("Consensus")));
    }

    // ── Animation ───────────────────────────────────────────────────

    #[test]
    fn test_advance_tick() {
        let mut viz = SwarmVisualization::new();
        assert_eq!(viz.tick, 0);
        viz.advance_tick();
        assert_eq!(viz.tick, 1);
        viz.advance_tick();
        viz.advance_tick();
        assert_eq!(viz.tick, 3);
    }

    #[test]
    fn test_spinner_cycles() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");
        viz.update_agent("Coder", AgentState::Working, None, 0.5);

        let mut seen = std::collections::HashSet::new();
        for _ in 0..SPINNER_FRAMES.len() {
            let s = viz.spinner_for(&viz.agents[0]);
            seen.insert(s);
            viz.advance_tick();
        }
        assert_eq!(seen.len(), SPINNER_FRAMES.len());
    }

    #[test]
    fn test_spinner_idle_uses_role_icon() {
        let viz = SwarmVisualization::new();
        let agent = AgentStatus::new("Architect");
        let s = viz.spinner_for(&agent);
        assert_eq!(s, agent.role_icon());
    }

    // ── Progress bar ────────────────────────────────────────────────

    #[test]
    fn test_render_progress_bar_zero() {
        let bar = render_progress_bar(0.0, 10);
        assert!(bar.starts_with('['));
        assert!(bar.ends_with(']'));
        assert!(!bar.contains(BAR_FILL));
        // All empty chars
        let count = bar.chars().filter(|c| *c == BAR_EMPTY).count();
        assert_eq!(count, 10);
    }

    #[test]
    fn test_render_progress_bar_full() {
        let bar = render_progress_bar(1.0, 10);
        let count = bar.chars().filter(|c| *c == BAR_FILL).count();
        assert_eq!(count, 10);
        assert!(!bar.contains(BAR_EMPTY));
    }

    #[test]
    fn test_render_progress_bar_half() {
        let bar = render_progress_bar(0.5, 10);
        let filled: usize = bar.chars().filter(|c| *c == BAR_FILL).count();
        let empty: usize = bar.chars().filter(|c| *c == BAR_EMPTY).count();
        assert_eq!(filled, 5);
        assert_eq!(empty, 5);
    }

    #[test]
    fn test_render_progress_bar_clamped() {
        let bar_over = render_progress_bar(1.5, 10);
        let bar_full = render_progress_bar(1.0, 10);
        assert_eq!(bar_over, bar_full);

        let bar_under = render_progress_bar(-0.5, 10);
        let bar_zero = render_progress_bar(0.0, 10);
        assert_eq!(bar_under, bar_zero);
    }

    #[test]
    fn test_render_progress_bar_various_widths() {
        for width in [1, 5, 20, 50] {
            let bar = render_progress_bar(0.5, width);
            // Total chars between brackets should equal width
            let inner_len = bar.len() - "[".len() - "]".len();
            // Note: multi-byte chars, so count chars not bytes
            let inner_chars: usize = bar
                .chars()
                .filter(|c| *c == BAR_FILL || *c == BAR_EMPTY)
                .count();
            assert_eq!(inner_chars, width);
        }
    }

    // ── Compact rendering ───────────────────────────────────────────

    #[test]
    fn test_render_compact_empty() {
        let viz = SwarmVisualization::new();
        let compact = viz.render_compact();
        assert!(compact.starts_with("Swarm: 0/0 active"));
    }

    #[test]
    fn test_render_compact_with_agents() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Architect");
        viz.add_agent("Coder");
        viz.add_agent("Tester");

        viz.update_agent("Architect", AgentState::Working, None, 0.8);
        viz.update_agent("Coder", AgentState::Done, None, 1.0);
        viz.update_agent("Tester", AgentState::Working, None, 0.4);

        let compact = viz.render_compact();
        assert!(compact.contains("2/3 active"));
        assert!(compact.contains("Architect:80%"));
        assert!(compact.contains("Coder:done"));
        assert!(compact.contains("Tester:40%"));
    }

    #[test]
    fn test_render_compact_all_states() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("A");
        viz.add_agent("B");
        viz.add_agent("C");
        viz.add_agent("D");
        viz.add_agent("E");

        viz.update_agent("A", AgentState::Idle, None, 0.0);
        viz.update_agent("B", AgentState::Working, None, 0.5);
        viz.update_agent("C", AgentState::Waiting, None, 0.0);
        viz.update_agent("D", AgentState::Done, None, 1.0);
        viz.update_agent("E", AgentState::Failed, None, 0.0);

        let compact = viz.render_compact();
        assert!(compact.contains("A:idle"));
        assert!(compact.contains("B:50%"));
        assert!(compact.contains("C:wait"));
        assert!(compact.contains("D:done"));
        assert!(compact.contains("E:FAIL"));
        // B (Working) + C (Waiting) = 2 active
        assert!(compact.contains("2/5 active"));
    }

    // ── Single agent progress ───────────────────────────────────────

    #[test]
    fn test_render_agent_progress_found() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Tester");
        viz.update_agent("Tester", AgentState::Working, None, 0.4);

        let line = viz.render_agent_progress("Tester");
        assert!(line.contains("Tester"));
        assert!(line.contains("40%"));
        assert!(line.contains("Working"));
        assert!(line.contains('['));
    }

    #[test]
    fn test_render_agent_progress_not_found() {
        let viz = SwarmVisualization::new();
        let line = viz.render_agent_progress("Ghost");
        assert!(line.is_empty());
    }

    #[test]
    fn test_render_agent_progress_done() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");
        viz.update_agent("Coder", AgentState::Done, None, 1.0);

        let line = viz.render_agent_progress("Coder");
        assert!(line.contains("Done"));
        assert!(line.contains("\u{2713}")); //    }

    #[test]
    fn test_render_agent_progress_failed() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Security");
        viz.update_agent("Security", AgentState::Failed, None, 0.3);

        let line = viz.render_agent_progress("Security");
        assert!(line.contains("Failed"));
        assert!(line.contains("\u{2717}")); //    }

    // ── Full rendering ──────────────────────────────────────────────

    #[test]
    fn test_render_full_contains_all_panels() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Architect");
        viz.add_agent("Coder");
        viz.update_agent("Architect", AgentState::Working, Some("planning"), 0.8);
        viz.update_agent("Coder", AgentState::Done, None, 1.0);

        viz.record_consensus(ConsensusEvent {
            timestamp: Instant::now(),
            topic: "REST vs GraphQL".to_string(),
            strategy: "majority".to_string(),
            votes: vec![("Architect".to_string(), true), ("Coder".to_string(), true)],
            outcome: true,
        });

        let full = viz.render_full();
        assert!(full.contains("Swarm Status"));
        assert!(full.contains("Consensus Log"));
        assert!(full.contains("Timeline"));
        assert!(full.contains("Architect"));
        assert!(full.contains("Coder"));
        assert!(full.contains("REST vs GraphQL"));
    }

    #[test]
    fn test_render_full_empty_swarm() {
        let viz = SwarmVisualization::new();
        let full = viz.render_full();
        assert!(full.contains("Swarm Status"));
        assert!(full.contains("Consensus Log"));
        assert!(full.contains("Timeline"));
        assert!(full.contains("No consensus events yet"));
        assert!(full.contains("No activity yet"));
    }

    // ── Panel borders ───────────────────────────────────────────────

    #[test]
    fn test_agent_panel_has_borders() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");

        let panel = viz.render_agent_panel();
        assert!(panel.contains(Glyphs::corner_tl()));
        assert!(panel.contains(Glyphs::corner_tr()));
        assert!(panel.contains(Glyphs::corner_bl()));
        assert!(panel.contains(Glyphs::corner_br()));
    }

    #[test]
    fn test_consensus_panel_has_borders() {
        let viz = SwarmVisualization::new();
        let panel = viz.render_consensus_panel();
        assert!(panel.contains(Glyphs::corner_tl()));
        assert!(panel.contains(Glyphs::corner_br()));
    }

    #[test]
    fn test_timeline_panel_has_borders() {
        let viz = SwarmVisualization::new();
        let panel = viz.render_timeline_panel();
        assert!(panel.contains(Glyphs::corner_tl()));
        assert!(panel.contains(Glyphs::corner_br()));
    }

    // ── Consensus panel rendering ───────────────────────────────────

    #[test]
    fn test_consensus_panel_shows_votes() {
        let mut viz = SwarmVisualization::new();
        viz.record_consensus(ConsensusEvent {
            timestamp: viz.start_time,
            topic: "Architecture choice".to_string(),
            strategy: "unanimous".to_string(),
            votes: vec![
                ("Architect".to_string(), true),
                ("Coder".to_string(), false),
            ],
            outcome: false,
        });

        let panel = viz.render_consensus_panel();
        assert!(panel.contains("Architecture choice"));
        assert!(panel.contains("Architect:\u{2713}"));
        assert!(panel.contains("Coder:\u{2717}"));
        assert!(panel.contains("rejected"));
    }

    #[test]
    fn test_consensus_panel_approved() {
        let mut viz = SwarmVisualization::new();
        viz.record_consensus(ConsensusEvent {
            timestamp: viz.start_time,
            topic: "Use REST".to_string(),
            strategy: "majority".to_string(),
            votes: vec![("Architect".to_string(), true), ("Coder".to_string(), true)],
            outcome: true,
        });

        let panel = viz.render_consensus_panel();
        assert!(panel.contains("approved"));
    }

    // ── Timeline rendering ──────────────────────────────────────────

    #[test]
    fn test_timeline_shows_entries() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Architect");
        viz.record_tool_call("Architect", "file_read src/main.rs");

        let panel = viz.render_timeline_panel();
        assert!(panel.contains("Architect"));
        assert!(panel.contains("file_read"));
        // Should contain the bullet marker
        assert!(panel.contains("\u{25B8}")); //    }

    #[test]
    fn test_timeline_limits_to_10_entries() {
        let mut viz = SwarmVisualization::new();
        viz.add_agent("Coder");
        for i in 0..15 {
            viz.record_tool_call("Coder", &format!("tool_{}", i));
        }

        let panel = viz.render_timeline_panel();
        // Should contain the most recent entries
        assert!(panel.contains("tool_14"));
        // The earliest entries should be truncated from view
        // (though they're still in the vec)
        assert!(viz.timeline.len() > 10);
    }

    // ── Helpers ─────────────────────────────────────────────────────

    #[test]
    fn test_format_elapsed_compact() {
        assert_eq!(format_elapsed_compact(Duration::from_secs(0)), "0:00");
        assert_eq!(format_elapsed_compact(Duration::from_secs(5)), "0:05");
        assert_eq!(format_elapsed_compact(Duration::from_secs(23)), "0:23");
        assert_eq!(format_elapsed_compact(Duration::from_secs(60)), "1:00");
        assert_eq!(format_elapsed_compact(Duration::from_secs(105)), "1:45");
        assert_eq!(format_elapsed_compact(Duration::from_secs(3661)), "61:01");
    }

    #[test]
    fn test_pad_right() {
        assert_eq!(pad_right("foo", 6), "foo   ");
        assert_eq!(pad_right("hello", 5), "hello");
        assert_eq!(pad_right("longer", 3), "longer");
        assert_eq!(pad_right("", 4), "    ");
    }

    #[test]
    fn test_display_width() {
        assert_eq!(display_width("hello"), 5);
        assert_eq!(display_width(""), 0);
        assert_eq!(display_width("abc"), 3);
    }

    // ── AgentState Display ──────────────────────────────────────────

    #[test]
    fn test_agent_state_display() {
        assert_eq!(format!("{}", AgentState::Idle), "Idle");
        assert_eq!(format!("{}", AgentState::Working), "Working");
        assert_eq!(format!("{}", AgentState::Waiting), "Waiting");
        assert_eq!(format!("{}", AgentState::Done), "Done");
        assert_eq!(format!("{}", AgentState::Failed), "Failed");
    }

    // ── Role icons ──────────────────────────────────────────────────

    #[test]
    fn test_role_icons() {
        let roles = [
            "Architect",
            "Coder",
            "Tester",
            "Reviewer",
            "DevOps",
            "Security",
            "Documenter",
            "Performance",
            "VisualCritic",
            "Unknown",
        ];
        for role in roles {
            let agent = AgentStatus::new(role);
            assert!(!agent.role_icon().is_empty());
        }
    }

    // ── Dominant strategy ───────────────────────────────────────────

    #[test]
    fn test_dominant_strategy_empty() {
        let viz = SwarmVisualization::new();
        assert_eq!(viz.dominant_strategy(), "none");
    }

    #[test]
    fn test_dominant_strategy_single() {
        let mut viz = SwarmVisualization::new();
        viz.record_consensus(ConsensusEvent {
            timestamp: Instant::now(),
            topic: "test".to_string(),
            strategy: "weighted".to_string(),
            votes: vec![],
            outcome: true,
        });
        assert_eq!(viz.dominant_strategy(), "weighted");
    }

    #[test]
    fn test_dominant_strategy_majority_wins() {
        let mut viz = SwarmVisualization::new();
        for strategy in &["majority", "majority", "unanimous"] {
            viz.record_consensus(ConsensusEvent {
                timestamp: Instant::now(),
                topic: "test".to_string(),
                strategy: strategy.to_string(),
                votes: vec![],
                outcome: true,
            });
        }
        assert_eq!(viz.dominant_strategy(), "majority");
    }

    // ── State suffix ────────────────────────────────────────────────

    #[test]
    fn test_state_suffix() {
        let mut agent = AgentStatus::new("Test");

        agent.state = AgentState::Done;
        assert_eq!(agent.state_suffix(), " \u{2713}");

        agent.state = AgentState::Failed;
        assert_eq!(agent.state_suffix(), " \u{2717}");

        agent.state = AgentState::Working;
        assert_eq!(agent.state_suffix(), "");

        agent.state = AgentState::Idle;
        assert_eq!(agent.state_suffix(), "");

        agent.state = AgentState::Waiting;
        assert_eq!(agent.state_suffix(), "");
    }

    // ── Integration: full workflow ──────────────────────────────────

    #[test]
    fn test_full_workflow() {
        let mut viz = SwarmVisualization::new();

        // Add agents
        viz.add_agent("Architect");
        viz.add_agent("Coder");
        viz.add_agent("Tester");
        viz.add_agent("Reviewer");
        viz.add_agent("Security");

        // Architect starts planning
        viz.update_agent("Architect", AgentState::Working, Some("planning"), 0.2);
        viz.record_tool_call("Architect", "file_read src/main.rs");
        viz.advance_tick();

        // Progress
        viz.update_agent("Architect", AgentState::Working, Some("planning"), 0.8);

        // Consensus
        viz.record_consensus(ConsensusEvent {
            timestamp: Instant::now(),
            topic: "REST vs GraphQL".to_string(),
            strategy: "majority".to_string(),
            votes: vec![
                ("Architect".to_string(), true),
                ("Coder".to_string(), true),
                ("Reviewer".to_string(), false),
            ],
            outcome: true,
        });

        // Architect done, Coder starts
        viz.update_agent("Architect", AgentState::Done, None, 1.0);
        viz.update_agent("Coder", AgentState::Working, Some("implementing"), 0.3);
        viz.record_tool_call("Coder", "file_write src/api.rs");
        viz.advance_tick();

        // Tester starts
        viz.update_agent("Tester", AgentState::Working, Some("test generation"), 0.1);

        // Render everything
        let full = viz.render_full();
        assert!(full.contains("Swarm Status"));
        assert!(full.contains("Architect"));
        assert!(full.contains("Done"));
        assert!(full.contains("Coder"));
        assert!(full.contains("Working"));
        assert!(full.contains("REST vs GraphQL"));
        assert!(full.contains("Timeline"));

        let compact = viz.render_compact();
        assert!(compact.contains("Swarm:"));
        assert!(compact.contains("Architect:done"));

        // Verify agent progress rendering
        let arch_progress = viz.render_agent_progress("Architect");
        assert!(arch_progress.contains("100%"));
        assert!(arch_progress.contains("\u{2713}")); //    }
}