cli_engineer 2.0.0

An autonomous CLI coding agent
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
use crate::event_bus::{Event, EventBus, EventEmitter};
use crate::impl_event_emitter;
use anyhow::Result;

use colored::*;
use crossterm::{
    cursor::{MoveTo, Show},
    execute,
    terminal::{Clear, ClearType, size},
};
use std::io::{self, Write};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio;

/// Dashboard UI that updates in-place without scrolling
use std::collections::VecDeque;

// Static mutex to prevent concurrent renders
static RENDER_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());

pub struct DashboardUI {
    headless: bool,
    event_bus: Option<Arc<EventBus>>,
    start_time: Instant,
    // Log buffer
    log_lines: Arc<Mutex<VecDeque<String>>>,
    // Reasoning traces from LLM models
    reasoning_traces: Arc<Mutex<VecDeque<String>>>,
    // Current status
    current_phase: Arc<Mutex<String>>,
    current_task: Arc<Mutex<String>>,
    current_status: Arc<Mutex<String>>,
    progress: Arc<Mutex<f32>>,

    // Metrics
    api_calls: Arc<Mutex<usize>>,
    artifacts_created: Arc<Mutex<usize>>,
    tasks_completed: Arc<Mutex<usize>>,
    tasks_total: Arc<Mutex<usize>>,
    total_cost: Arc<Mutex<f64>>,
    context_usage: Arc<Mutex<f32>>,
    last_update: Instant,
}

impl DashboardUI {
    pub fn new(headless: bool) -> Self {
        Self {
            headless,
            event_bus: None,
            start_time: Instant::now(),
            current_phase: Arc::new(Mutex::new("Initializing".to_string())),
            current_task: Arc::new(Mutex::new(String::new())),
            current_status: Arc::new(Mutex::new(String::new())),
            progress: Arc::new(Mutex::new(0.0)),
            api_calls: Arc::new(Mutex::new(0)),
            artifacts_created: Arc::new(Mutex::new(0)),
            tasks_completed: Arc::new(Mutex::new(0)),
            tasks_total: Arc::new(Mutex::new(0)),
            total_cost: Arc::new(Mutex::new(0.0)),
            context_usage: Arc::new(Mutex::new(0.0)),
            last_update: Instant::now(),
            log_lines: Arc::new(Mutex::new(VecDeque::with_capacity(30))),
            reasoning_traces: Arc::new(Mutex::new(VecDeque::with_capacity(30))),
        }
    }

    pub fn start(&mut self) -> Result<()> {
        if self.headless {
            return Ok(());
        }

        // Clear entire screen and move to top
        execute!(io::stdout(), Clear(ClearType::All), MoveTo(0, 0))?;

        // Start background event listener if event bus is available
        if let Some(event_bus) = &self.event_bus {
            let receiver = event_bus.subscribe();
            let log_lines = self.log_lines.clone();
            let current_phase = self.current_phase.clone();
            let current_task = self.current_task.clone();
            let current_status = self.current_status.clone();
            let progress = self.progress.clone();
            let api_calls = self.api_calls.clone();
            let artifacts_created = self.artifacts_created.clone();
            let tasks_completed = self.tasks_completed.clone();
            let tasks_total = self.tasks_total.clone();
            let total_cost = self.total_cost.clone();
            let context_usage = self.context_usage.clone();
            let reasoning_traces = self.reasoning_traces.clone();

            tokio::spawn(async move {
                let mut event_receiver = receiver;
                while let Ok(event) = event_receiver.recv().await {
                    match event {
                        Event::LogLine { level, message } => {
                            let colored = match level.as_str() {
                                "ERROR" => format!("[ERROR] {}", message).red().to_string(),
                                "WARN" => format!("[WARN ] {}", message).yellow().to_string(),
                                "INFO" => format!("[INFO ] {}", message).cyan().to_string(),
                                "DEBUG" => format!("[DEBUG] {}", message).white().to_string(),
                                "TRACE" => format!("[TRACE] {}", message).dimmed().to_string(),
                                _ => format!("[{}] {}", level, message),
                            };
                            let mut logs = log_lines.lock().unwrap();
                            if logs.len() >= 30 {
                                logs.pop_front();
                            }
                            logs.push_back(colored.clone());
                        }
                        Event::TaskStarted { description, .. } => {
                            *current_task.lock().unwrap() = description;
                            *current_status.lock().unwrap() = "Running".to_string();
                        }
                        Event::TaskCompleted { .. } => {
                            *current_status.lock().unwrap() = "Completed".to_string();
                            *progress.lock().unwrap() = 1.0;
                            *tasks_completed.lock().unwrap() += 1;
                        }
                        Event::ExecutionStarted { .. } => {
                            *tasks_total.lock().unwrap() += 1;
                            let iter_count = *tasks_total.lock().unwrap();
                            *current_phase.lock().unwrap() = format!("Iteration {}", iter_count);
                        }
                        Event::APICallStarted { provider, model } => {
                            *api_calls.lock().unwrap() += 1;
                            *current_status.lock().unwrap() =
                                format!("Calling {}/{}", provider, model);
                        }
                        Event::APICallCompleted { cost, .. } => {
                            *total_cost.lock().unwrap() += cost as f64;
                            *current_status.lock().unwrap() = "API response received".to_string();
                        }
                        Event::ArtifactCreated { .. } => {
                            *artifacts_created.lock().unwrap() += 1;
                        }
                        Event::ContextUsageChanged {
                            usage_percentage, ..
                        } => {
                            *context_usage.lock().unwrap() = usage_percentage;
                        }
                        Event::ExecutionProgress { step, progress: prog } => {
                            // Progress updated successfully
                            *current_phase.lock().unwrap() = step;
                            *progress.lock().unwrap() = prog;
                            *current_status.lock().unwrap() = format!("Progress: {:.0}%", prog * 100.0);

                        }
                        Event::ReasoningTrace { message } => {
                            if !message.trim().is_empty() {
                                let mut traces = reasoning_traces.lock().unwrap();
                                if traces.len() >= 30 {
                                    traces.pop_front();
                                }
                                traces.push_back(message);
                            }
                        }
                        _ => {}
                    }
                }
            });
        }

        Ok(())
    }

    pub fn finish(&mut self) -> Result<()> {
        if self.headless {
            return Ok(());
        }

        // Show cursor again
        execute!(io::stdout(), Show)?;

        // Move to bottom and print summary
        let (_, height) = size()?;
        execute!(io::stdout(), MoveTo(0, height - 2))?;

        let elapsed = self.start_time.elapsed();
        println!("\n{}", "=".repeat(120).bright_blue());
        println!(
            "{} {} in {:.1}s",
            "".green().bold(),
            "Task completed".bright_white().bold(),
            elapsed.as_secs_f32()
        );
        println!(
            "  {} iterations | {} API calls | {} artifacts | ${:.3} cost",
            self.tasks_total.lock().unwrap().to_string().cyan(),
            self.api_calls.lock().unwrap().to_string().yellow(),
            self.artifacts_created.lock().unwrap().to_string().green(),
            format!("{:.3}", self.total_cost.lock().unwrap()).magenta()
        );

        Ok(())
    }

    fn render(&self) -> Result<()> {
        if self.headless {
            return Ok(());
        }

        // Acquire the render mutex
        let _lock = RENDER_MUTEX.lock().unwrap();

        // Clear entire screen and move to top
        execute!(io::stdout(), Clear(ClearType::All), MoveTo(0, 0))?;

        // Box width constants
        const _BOX_WIDTH: usize = 120;
        const CONTENT_WIDTH: usize = 118; // BOX_WIDTH - 2 (for borders)

        // Calculate elapsed time
        let elapsed = self.start_time.elapsed();
        let minutes = elapsed.as_secs() / 60;
        let seconds = elapsed.as_secs() % 60;

        // Header
        println!("{}", "╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗".bright_blue());

        // Title line with time
        let title = "cli_engineer";
        let time_str = format!("{}:{:02}", minutes, seconds);
        let padding = CONTENT_WIDTH.saturating_sub(title.len() + time_str.len() + 3);
        println!(
            "{} {}{}{} {}{}",
            "".bright_blue(),
            title.bright_white().bold(),
            " ".repeat(padding),
            time_str,
            " ", // add 1 space after time
            "".bright_blue()
        );

        println!("{}", "╠══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣".bright_blue());

        // Phase and Progress
        let phase_label = "Phase: ";
        let phase_text_raw = if let Ok(guard) = self.current_phase.try_lock() {
            guard.clone()
        } else {
            "Loading...".to_string()
        };
        let progress_bar_str = self.render_progress_bar(40);
        let progress_bar_visible = strip_ansi_codes(&progress_bar_str);
        let progress_bar_width = progress_bar_visible.len();

        // Allow more generous space for phase text - only truncate if really necessary
        let min_progress_space = progress_bar_width + 20; // Progress bar + reasonable padding
        let max_phase_width = CONTENT_WIDTH.saturating_sub(phase_label.len() + min_progress_space + 1);
        
        // Only truncate if phase text is significantly longer than available space
        let phase_text = if phase_text_raw.len() > max_phase_width && max_phase_width > 20 {
            // Find a good truncation point - prefer after colons or spaces
            let target_len = max_phase_width.saturating_sub(3);
            if let Some(pos) = phase_text_raw[..target_len].rfind([' ', ':', '-']) {
                format!("{}...", &phase_text_raw[..pos])
            } else {
                let truncate_at = phase_text_raw
                    .char_indices()
                    .nth(target_len)
                    .map(|(i, _)| i)
                    .unwrap_or(target_len);
                format!("{}...", &phase_text_raw[..truncate_at])
            }
        } else {
            phase_text_raw
        };

        // Strategic padding: 1 space after %, dynamic padding before progress bar
        // Match exact Task line pattern: CONTENT_WIDTH.saturating_sub(label.len() + text.len() + 1)
        let visible_phase_text = strip_ansi_codes(&phase_text);
        let right_padding = 1; // Fixed: exactly 1 space after %
        
        // Calculate progress bar visible length dynamically based on actual percentage
        let progress_val = if let Ok(guard) = self.progress.try_lock() { *guard } else { 0.0 };
        let percent_str = format!("{:.0}%", progress_val * 100.0);
        let progress_bar_visible_len = 1 + 40 + 2 + percent_str.len(); // [40chars] XX%
        
        // Use exact Task line pattern: CONTENT_WIDTH.saturating_sub(label.len() + text.len() + 1)
        // But subtract space needed for progress bar and right padding
        let base_padding = CONTENT_WIDTH.saturating_sub(phase_label.len() + visible_phase_text.len() + 1);
        let dynamic_padding = base_padding.saturating_sub(progress_bar_visible_len + right_padding);

        print!(
            "{} {}{}",
            "".bright_blue(),
            phase_label.bright_white(),
            phase_text.cyan()
        );
        print!("{}", " ".repeat(dynamic_padding)); // Dynamic padding before progress bar
        print!("{}", progress_bar_str);
        print!("{}", " ".repeat(right_padding)); // Fixed 1 space after %
        println!("{}", "".bright_blue());
        io::stdout().flush()?;

        // Current Task
        let task_label = "Task: ";
        let max_task_len = CONTENT_WIDTH.saturating_sub(task_label.len() + 1);
        let task_text = if let Ok(guard) = self.current_task.try_lock() {
            if guard.len() > max_task_len {
                // Use char-safe truncation to avoid broken UTF-8
                let truncated_len = max_task_len.saturating_sub(3);
                let mut char_count = 0;
                let mut end_idx = 0;
                for (i, _) in guard.char_indices() {
                    if char_count >= truncated_len {
                        break;
                    }
                    end_idx = i;
                    char_count += 1;
                }
                if char_count < guard.chars().count() {
                    format!("{}...", &guard[..end_idx])
                } else {
                    guard.clone()
                }
            } else {
                guard.clone()
            }
        } else {
            "Loading...".to_string()
        };
        let task_padding = CONTENT_WIDTH.saturating_sub(task_label.len() + strip_ansi_codes(&task_text).len() + 1);

        print!(
            "{} {}{}",
            "".bright_blue(),
            task_label.bright_white(),
            task_text.yellow()
        );
        print!("{}", " ".repeat(task_padding));
        println!("{}", "".bright_blue());
        io::stdout().flush()?;

        // Status - only render if there's actual status content
        let status_text = if let Ok(guard) = self.current_status.try_lock() {
            guard.clone()
        } else {
            String::new()
        };
        
        if !status_text.is_empty() {
            let status_label = "Status: ";
            let max_status_len = CONTENT_WIDTH.saturating_sub(status_label.len() + 1);
            let status_text = if status_text.len() > max_status_len {
                // Use char_indices to find safe character boundaries for truncation
                let truncate_at = status_text
                    .char_indices()
                    .nth(max_status_len.saturating_sub(3))
                    .map(|(i, _)| i)
                    .unwrap_or(status_text.len());
                format!("{}...", &status_text[..truncate_at])
            } else {
                status_text
            };
            let status_color = if status_text.starts_with("") {
                status_text.green()
            } else if status_text.starts_with("") {
                status_text.red()
            } else {
                status_text.white()
            };
            let status_padding =
                CONTENT_WIDTH.saturating_sub(status_label.len() + status_text.len() + 1);

            print!(
                "{} {}{}",
                "".bright_blue(),
                status_label.bright_white(),
                status_color
            );
            print!("{}", " ".repeat(status_padding));
            println!("{}", "".bright_blue());
            io::stdout().flush()?;
        }

        println!("{}", "╠══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣".bright_blue());

        // Metrics - build the complete metrics line first
        let api_calls = if let Ok(guard) = self.api_calls.try_lock() {
            *guard
        } else {
            0
        };
        let artifacts = if let Ok(guard) = self.artifacts_created.try_lock() {
            *guard
        } else {
            0
        };
        let tasks_completed = if let Ok(guard) = self.tasks_completed.try_lock() {
            *guard
        } else {
            0
        };
        let tasks_total = if let Ok(guard) = self.tasks_total.try_lock() {
            *guard
        } else {
            0
        };
        let total_cost = if let Ok(guard) = self.total_cost.try_lock() {
            *guard
        } else {
            0.0
        };
        let context_usage = if let Ok(guard) = self.context_usage.try_lock() {
            *guard
        } else {
            0.0
        };

        let formatted_cost = format!("{:.3}", total_cost);
        let formatted_tasks = format!("{}/{}", tasks_completed, tasks_total);
        let formatted_api_calls = api_calls.to_string();
        let formatted_artifacts = artifacts.to_string();
        let formatted_context = format!("{:.1}", context_usage);

        // Calculate padding for metrics line
        let content = format!(
            "📊 Tasks: {} | 🤖 API Calls: {} | 💰 Cost: ${} | 📝 Artifacts: {} | 💾 Context: {}%",
            formatted_tasks,
            formatted_api_calls,
            formatted_cost,
            formatted_artifacts,
            formatted_context
        );
        let emoji_adjustment = 10; // Account for emoji display width
        let metrics_padding = CONTENT_WIDTH.saturating_sub(content.len() + 1 - emoji_adjustment);

        print!("{} ", "".bright_blue());
        print!(
            "📊 Tasks: {} | 🤖 API Calls: {} | 💰 Cost: ${} | 📝 Artifacts: {} | 💾 Context: {}%",
            formatted_tasks.cyan(),
            formatted_api_calls.yellow(),
            formatted_cost.green(),
            formatted_artifacts.green(),
            formatted_context
        );
        print!("{}", " ".repeat(metrics_padding));
        println!("{}", "".bright_blue());
        println!("{}", "╠══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣".bright_blue());
        io::stdout().flush()?;

        // Split log area into two sections: upper for logs, lower for reasoning traces
        let log_lines = if let Ok(guard) = self.log_lines.try_lock() {
            guard.clone()
        } else {
            std::collections::VecDeque::new()
        };

        let reasoning_traces = if let Ok(guard) = self.reasoning_traces.try_lock() {
            guard.clone()
        } else {
            std::collections::VecDeque::new()
        };

        // Upper section: Regular logs (15 lines)
        let log_section_lines = 15;
        for (i, log_line) in log_lines.iter().enumerate() {
            if i >= log_section_lines { break; }
            
            let max_log_len = CONTENT_WIDTH.saturating_sub(4); // More generous space before truncation
            let visible_log = strip_ansi_codes(log_line);
            
            // Smart truncation - prefer breaking at word boundaries
            let display_log = if visible_log.len() > max_log_len {
                let target_len = max_log_len.saturating_sub(3);
                // Try to find a good break point (space, comma, colon)
                if let Some(pos) = visible_log[..target_len].rfind([' ', ',', ':', '(', ')']) {
                    format!("{}...", &visible_log[..pos])
                } else {
                    // Fallback to character boundary truncation
                    let truncate_at = visible_log
                        .char_indices()
                        .nth(target_len)
                        .map(|(i, _)| i)
                        .unwrap_or(target_len);
                    format!("{}...", &visible_log[..truncate_at])
                }
            } else {
                log_line.clone()
            };
            
            let visible_display = strip_ansi_codes(&display_log);
            let log_padding = CONTENT_WIDTH.saturating_sub(visible_display.len() + 1); // +1 for the space after ║
            print!(
                "{} {}{}",
                "".bright_blue(),
                display_log,
                " ".repeat(log_padding)
            );
            println!("{}", "".bright_blue());
            io::stdout().flush()?;
        }

        // Fill remaining log lines
        let used_log_lines = std::cmp::min(log_lines.len(), log_section_lines);
        for _ in used_log_lines..log_section_lines {
            let log_padding = CONTENT_WIDTH - 1;
            print!("{} {}", "".bright_blue(), " ".repeat(log_padding));
            println!("{}", "".bright_blue());
            io::stdout().flush()?;
        }

        println!("{}", "╠═══════════════════════════════════════════════ 🤔 Model Reasoning ═══════════════════════════════════════════════════╣".bright_blue());

        // Lower section: Reasoning traces (15 lines)
        let trace_section_lines = 15;
        
        // Calculate which traces to show (most recent ones)
        let traces_to_show: Vec<_> = if reasoning_traces.len() > trace_section_lines {
            reasoning_traces.iter()
                .skip(reasoning_traces.len() - trace_section_lines)
                .collect()
        } else {
            reasoning_traces.iter().collect()
        };
        
        // Render the traces
        let mut lines_rendered = 0;
        for trace in traces_to_show.iter() {
            if lines_rendered >= trace_section_lines { break; }
            
            // Split trace into lines and render each line
            for line in trace.split('\n') {
                if lines_rendered >= trace_section_lines { break; }
                
                //let max_trace_len = 110; // Wrap reasoning traces at 110 characters
                let max_trace_len = CONTENT_WIDTH - 2; // +1 for the space after ║
                let visible_line = strip_ansi_codes(line);
                
                // Wrap the line instead of truncating
                let wrapped_lines = wrap_text(&visible_line, max_trace_len);
                
                for wrapped_line in wrapped_lines {
                    if lines_rendered >= trace_section_lines { break; }
                    
                    let visual_width_wrapped = visual_width(&wrapped_line);
                    let trace_padding = CONTENT_WIDTH.saturating_sub(visual_width_wrapped + 1); // +1 for the space after ║
                    print!(
                        "{} {}{}",
                        "".bright_blue(),
                        wrapped_line.bright_black(), // Show reasoning traces in gray
                        " ".repeat(trace_padding)
                    );
                    println!("{}", "".bright_blue());
                    io::stdout().flush()?;
                    lines_rendered += 1;
                }
            }
        }

        // Fill remaining trace lines if we have fewer lines than allocated space
        for _ in lines_rendered..trace_section_lines {
            let trace_padding = CONTENT_WIDTH - 1;
            print!("{} {}", "".bright_blue(), " ".repeat(trace_padding));
            println!("{}", "".bright_blue());
            io::stdout().flush()?;
        }

        println!("{}", "╚══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝".bright_blue());

        // Flush output
        io::stdout().flush()?;

        Ok(())
    }

    fn render_progress_bar(&self, width: usize) -> String {
        let progress_val = if let Ok(guard) = self.progress.try_lock() {
            *guard
        } else {
            0.0
        };
        let filled = ((progress_val * width as f32) as usize).min(width);
        let empty = width - filled;

        format!(
            "[{}{}] {:.0}%",
            "".repeat(filled).green(),
            "".repeat(empty).bright_black(),
            progress_val * 100.0
        )
    }

    #[allow(dead_code)]
    pub fn update_phase(&mut self, phase: &str) -> Result<()> {
        *self.current_phase.lock().unwrap() = phase.to_string();
        *self.progress.lock().unwrap() = 0.0;
        self.throttled_render()
    }

    #[allow(dead_code)]
    pub fn update_task(&mut self, task: &str) -> Result<()> {
        *self.current_task.lock().unwrap() = task.to_string();
        self.throttled_render()
    }

    pub fn update_status(&mut self, status: &str) -> Result<()> {
        *self.current_status.lock().unwrap() = status.to_string();
        self.throttled_render()
    }

    #[allow(dead_code)]
    pub fn update_progress(&mut self, progress: f32) -> Result<()> {
        *self.progress.lock().unwrap() = progress.clamp(0.0, 1.0);
        self.throttled_render()
    }

    #[allow(dead_code)]
    pub fn update_metrics(
        &mut self,
        api_calls: usize,
        artifacts: usize,
        tasks_completed: usize,
        tasks_total: usize,
        total_cost: f64,
    ) -> Result<()> {
        *self.api_calls.lock().unwrap() = api_calls;
        *self.artifacts_created.lock().unwrap() = artifacts;
        *self.tasks_completed.lock().unwrap() = tasks_completed;
        *self.tasks_total.lock().unwrap() = tasks_total;
        *self.total_cost.lock().unwrap() = total_cost;
        self.throttled_render()
    }

    /// Only render if enough time has passed to avoid flickering
    pub fn throttled_render(&mut self) -> Result<()> {
        if self.last_update.elapsed() > Duration::from_millis(100) {
            self.last_update = Instant::now();
            self.render()?;
        }
        Ok(())
    }

    #[allow(dead_code)]
    pub fn handle_event(&mut self, event: Event) -> Result<()> {
        match event {
            Event::LogLine { level, message } => {
                let colored = match level.as_str() {
                    "ERROR" => format!("[ERROR] {}", message).red().to_string(),
                    "WARN" => format!("[WARN ] {}", message).yellow().to_string(),
                    "INFO" => format!("[INFO ] {}", message).cyan().to_string(),
                    "DEBUG" => format!("[DEBUG] {}", message).white().to_string(),
                    "TRACE" => format!("[TRACE] {}", message).dimmed().to_string(),
                    _ => format!("[{}] {}", level, message),
                };
                let mut logs = self.log_lines.lock().unwrap();
                if logs.len() >= 30 {
                    logs.pop_front();
                }
                logs.push_back(colored.clone());
            }
            Event::TaskStarted { description, .. } => {
                self.update_task(&description)?;
                self.update_status("Running")?;
            }
            Event::TaskCompleted { .. } => {
                self.update_status("Completed")?;
                self.update_progress(1.0)?;
                *self.tasks_completed.lock().unwrap() += 1;
            }
            Event::ExecutionStarted { .. } => {
                *self.tasks_total.lock().unwrap() += 1;
                let iter_count = *self.tasks_total.lock().unwrap();
                self.update_phase(&format!("Iteration {}", iter_count))?;
            }
            Event::APICallStarted { provider, model } => {
                *self.api_calls.lock().unwrap() += 1;
                self.update_status(&format!("Calling {}/{}", provider, model))?;
            }
            Event::APICallCompleted { cost, .. } => {
                *self.total_cost.lock().unwrap() += cost as f64;
                self.update_status("API response received")?;
            }
            Event::ArtifactCreated { .. } => {
                *self.artifacts_created.lock().unwrap() += 1;
            }
            Event::ContextUsageChanged {
                usage_percentage, ..
            } => {
                *self.context_usage.lock().unwrap() = usage_percentage;
            }
            Event::ExecutionProgress { step, progress } => {
                // Progress updated successfully
                match self.update_phase(&step) {
                    Ok(_) => {}, // Phase updated successfully
                    Err(_) => {}, // Phase update failed silently
                }
                match self.update_progress(progress) {
                    Ok(_) => {}, // Progress updated successfully
                    Err(_) => {}, // Progress update failed silently
                }
                self.update_status(&format!("Progress: {:.0}%", progress * 100.0))?;
            }
            Event::ReasoningTrace { message } => {
                if !message.trim().is_empty() {
                    let mut traces = self.reasoning_traces.lock().unwrap();
                    if traces.len() >= 30 {
                        traces.pop_front();
                    }
                    traces.push_back(message);
                }
            }
            _ => {}
        }
        Ok(())
    }

    pub fn display_error(&mut self, error: &str) -> Result<()> {
        self.update_status(&format!("{}", error))
    }

    #[allow(dead_code)]
    pub fn display_task(&mut self, task: &str) -> Result<()> {
        self.update_task(task)
    }
}

// Implement EventEmitter trait
impl_event_emitter!(DashboardUI);

// Helper to strip ANSI escape codes
fn strip_ansi_codes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\u{1b}' {
            // Skip until 'm'
            while let Some(nc) = chars.next() {
                if nc == 'm' {
                    break;
                }
            }
        } else {
            result.push(c);
        }
    }
    result
}

// Helper function to calculate visual width (accounting for emoji width)
fn visual_width(s: &str) -> usize {
    s.chars().map(|c| {
        match c {
            // Common emojis used in reasoning traces
            '🤔' | '' | '🔍' | '💭' | '🧠' | '' | '🎯' | '💡' => 2,
            // Regular characters
            _ => 1,
        }
    }).sum()
}

// Helper function to wrap text at word boundaries
fn wrap_text(text: &str, max_width: usize) -> Vec<String> {
    let mut lines = Vec::new();
    let mut current_line = String::new();
    let mut current_width = 0;

    for word in text.split_whitespace() {
        let word_visual_width = visual_width(word);
        
        // Check if adding this word would exceed the limit
        if current_width + word_visual_width + (if current_line.is_empty() { 0 } else { 1 }) <= max_width {
            if !current_line.is_empty() {
                current_line.push(' ');
                current_width += 1;
            }
            current_line.push_str(word);
            current_width += word_visual_width;
        } else {
            // Start a new line
            if !current_line.is_empty() {
                lines.push(current_line);
            }
            current_line = word.to_string();
            current_width = word_visual_width;
        }
    }
    
    if !current_line.is_empty() {
        lines.push(current_line);
    }
    
    if lines.is_empty() {
        lines.push(String::new());
    }
    
    lines
}