rch 2.1.32

Remote Compilation Helper - PreToolUse hook CLI
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
//! WorkerTable - Rich terminal display for `rch workers list` command.
//!
//! This module provides context-aware worker fleet display using rich_rust.
//! Falls back to plain text when rich output is not available.

use crate::status_types::WorkerStatusFromApi;
use crate::ui::console::RchConsole;

#[cfg(all(feature = "rich-ui", unix))]
use rich_rust::prelude::*;
#[cfg(all(feature = "rich-ui", unix))]
use rich_rust::renderables::{Column, Row, Table};

use rch_common::ui::{Icons, OutputContext, RchTheme};

/// WorkerTable renders worker fleet status with rich formatting.
///
/// Displays:
/// - Worker ID and host
/// - Status with colored indicator
/// - Slot usage (used/total)
/// - Speed score
/// - Circuit breaker state
/// - Last error (if any)
pub struct WorkerTable<'a> {
    workers: &'a [WorkerStatusFromApi],
    context: OutputContext,
}

impl<'a> WorkerTable<'a> {
    /// Create a new WorkerTable from worker status list.
    #[must_use]
    pub fn new(workers: &'a [WorkerStatusFromApi], context: OutputContext) -> Self {
        Self { workers, context }
    }

    /// Create from workers with auto-detected context.
    #[must_use]
    pub fn from_workers(workers: &'a [WorkerStatusFromApi]) -> Self {
        Self::new(workers, OutputContext::detect())
    }

    /// Render the worker table using RchConsole.
    pub fn render(&self, console: &RchConsole) {
        if console.is_machine() {
            // JSON mode - don't render, caller should use print_json
            return;
        }

        #[cfg(all(feature = "rich-ui", unix))]
        if console.is_rich() {
            self.render_rich(console);
            return;
        }

        self.render_plain(console);
    }

    /// Render rich output using rich_rust.
    #[cfg(all(feature = "rich-ui", unix))]
    fn render_rich(&self, console: &RchConsole) {
        if self.workers.is_empty() {
            self.render_empty_rich(console);
            return;
        }

        let mut table = Table::new()
            .title("Workers")
            .border_style(RchTheme::secondary());

        // Add columns
        table = table
            .with_column(Column::new("ID").header_style(RchTheme::table_header()))
            .with_column(Column::new("Host").header_style(RchTheme::table_header()))
            .with_column(Column::new("Status").header_style(RchTheme::table_header()))
            .with_column(Column::new("Slots").header_style(RchTheme::table_header()))
            .with_column(Column::new("Speed").header_style(RchTheme::table_header()))
            .with_column(Column::new("Circuit").header_style(RchTheme::table_header()));

        // Add rows
        for worker in self.workers {
            let status_text = self.format_status_rich(worker);
            // Visual slot bar with ratio (e.g., "██░░░░░░ 2/8")
            let slot_bar = format_slot_bar(worker.used_slots, worker.total_slots, self.context);
            let slots_text = format!("{} {}/{}", slot_bar, worker.used_slots, worker.total_slots);
            // Speed with context indicator
            let speed_text = format_speed_score(worker.speed_score, self.context);
            let circuit_text = self.format_circuit_state(worker);

            let row = Row::new(vec![
                Cell::new(worker.id.as_str()),
                Cell::new(worker.host.as_str()),
                Cell::new(status_text.as_str()),
                Cell::new(slots_text.as_str()),
                Cell::new(speed_text.as_str()),
                Cell::new(circuit_text.as_str()),
            ]);
            table = table.with_row(row);
        }

        console.print_renderable(&table);

        // Summary footer
        self.render_summary_rich(console);
    }

    /// Render empty state with rich formatting.
    #[cfg(all(feature = "rich-ui", unix))]
    fn render_empty_rich(&self, console: &RchConsole) {
        let info = Icons::info(self.context);
        let content = format!(
            "{} No workers configured\n\nAdd workers with: rch workers init",
            info
        );
        let panel = Panel::from_text(&content)
            .title("Workers")
            .border_style(RchTheme::muted())
            .rounded();
        console.print_renderable(&panel);
    }

    /// Format status for rich display.
    #[cfg(all(feature = "rich-ui", unix))]
    fn format_status_rich(&self, worker: &WorkerStatusFromApi) -> String {
        match worker.status.as_str() {
            "healthy" | "online" => format!("{} Online", Icons::status_healthy(self.context)),
            "busy" => format!("{} Busy", Icons::worker(self.context)),
            "degraded" => format!("{} Degraded", Icons::warning(self.context)),
            "draining" => format!("{} Draining", Icons::info(self.context)),
            "drained" => format!("{} Drained", Icons::info(self.context)),
            "unreachable" | "offline" | "unhealthy" => {
                format!("{} Offline", Icons::cross(self.context))
            }
            "disabled" => format!("{} Disabled", Icons::cross(self.context)),
            other => other.to_string(),
        }
    }

    /// Format circuit breaker state.
    fn format_circuit_state(&self, worker: &WorkerStatusFromApi) -> String {
        match worker.circuit_state.as_str() {
            "closed" => "OK".to_string(),
            "half_open" | "half-open" => "Testing".to_string(),
            "open" => {
                if let Some(secs) = worker.recovery_in_secs {
                    format!("Open ({secs}s)")
                } else {
                    "Open".to_string()
                }
            }
            other => other.to_string(),
        }
    }

    /// Render summary footer.
    #[cfg(all(feature = "rich-ui", unix))]
    fn render_summary_rich(&self, console: &RchConsole) {
        console.print_plain(&self.summary());
    }

    /// Active jobs retain their reservations when disk capacity shrinks. Sum
    /// each worker's remaining capacity independently so an overcommitted worker
    /// neither underflows nor consumes another worker's available slots.
    fn summary(&self) -> String {
        let online = self
            .workers
            .iter()
            .filter(|w| w.status == "healthy" || w.status == "online")
            .count();
        let total = self.workers.len();
        let total_slots: u64 = self.workers.iter().map(|w| u64::from(w.total_slots)).sum();
        let available_slots: u64 = self
            .workers
            .iter()
            .map(|w| u64::from(w.total_slots.saturating_sub(w.used_slots)))
            .sum();

        format!(
            "Total: {} workers ({} online) | Slots: {}/{} available",
            total, online, available_slots, total_slots
        )
    }

    /// Render plain text output (no rich formatting).
    fn render_plain(&self, console: &RchConsole) {
        if self.workers.is_empty() {
            console.print_plain("=== Workers ===");
            console.print_plain("");
            let info = Icons::info(self.context);
            console.print_plain(&format!("{info} No workers configured"));
            console.print_plain("  Add workers with: rch workers init");
            return;
        }

        console.print_plain("=== Workers ===");
        console.print_plain("");

        // Header
        console.print_plain(&format!(
            "{:12} {:20} {:12} {:16} {:8} {:10}",
            "ID", "Host", "Status", "Slots", "Speed", "Circuit"
        ));
        console.print_plain(&"-".repeat(80));

        // Rows
        for worker in self.workers {
            let status_text = self.format_status_plain(worker);
            // Visual slot bar with ratio
            let slot_bar = format_slot_bar(worker.used_slots, worker.total_slots, self.context);
            let slots_text = format!("{} {}/{}", slot_bar, worker.used_slots, worker.total_slots);
            // Speed with context
            let speed_text = format_speed_score(worker.speed_score, self.context);
            let circuit_text = self.format_circuit_state(worker);

            console.print_plain(&format!(
                "{:12} {:20} {:12} {:16} {:8} {:10}",
                truncate_str(&worker.id, 12),
                truncate_str(&worker.host, 20),
                status_text,
                slots_text,
                speed_text,
                circuit_text
            ));

            // Show error if present
            if let Some(ref err) = worker.last_error {
                let warning = Icons::warning(self.context);
                console.print_plain(&format!("  {warning} {err}"));
            }
        }

        // Summary
        console.print_plain("");
        console.print_plain(&self.summary());
    }

    /// Format status for plain display.
    fn format_status_plain(&self, worker: &WorkerStatusFromApi) -> String {
        let icon = match worker.status.as_str() {
            "healthy" | "online" => Icons::status_healthy(self.context),
            "busy" => Icons::worker(self.context),
            "degraded" => Icons::warning(self.context),
            "offline" | "unhealthy" => Icons::cross(self.context),
            "draining" => Icons::info(self.context),
            _ => " ",
        };

        let status_label = match worker.status.as_str() {
            "healthy" | "online" => "Online",
            "busy" => "Busy",
            "degraded" => "Degraded",
            "offline" | "unhealthy" => "Offline",
            "draining" => "Draining",
            other => other,
        };

        format!("{} {}", icon, status_label)
    }
}

/// Truncate a string for display.
fn truncate_str(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    }
}

/// Create a visual slot bar showing used vs total slots.
///
/// Example: 2/8 slots → "██░░░░░░" (8 chars)
fn format_slot_bar(used: u32, total: u32, ctx: OutputContext) -> String {
    if total == 0 {
        return "-".to_string();
    }

    let filled = Icons::slot_filled(ctx);
    let empty = Icons::slot_empty(ctx);

    // Cap at 8 characters for consistent width
    let bar_width = total.min(8) as usize;
    let filled_count = if total <= 8 {
        used as usize
    } else {
        // Scale proportionally for workers with many slots
        ((used as f64 / total as f64) * bar_width as f64).round() as usize
    };

    let mut bar = String::new();
    for i in 0..bar_width {
        if i < filled_count {
            bar.push_str(filled);
        } else {
            bar.push_str(empty);
        }
    }
    bar
}

/// Format speed score with visual indicator and context.
///
/// Speed scores are relative: 1.0 = baseline, >1.0 = faster, <1.0 = slower.
/// Example: 1.5 → "⚡1.5x" (fast), 0.5 → "🐢0.5x" (slow)
fn format_speed_score(score: f64, ctx: OutputContext) -> String {
    if score <= 0.0 {
        return "-".to_string();
    }

    let indicator = if score >= 1.5 {
        Icons::lightning(ctx) // ⚡ Fast
    } else if score >= 1.0 {
        "" // Normal, no indicator
    } else if score >= 0.5 {
        "~" // Slow-ish
    } else {
        "!" // Very slow
    };

    if indicator.is_empty() {
        format!("{score:.1}x")
    } else {
        format!("{indicator}{score:.1}x")
    }
}

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

    fn sample_workers() -> Vec<WorkerStatusFromApi> {
        vec![
            WorkerStatusFromApi {
                id: "worker-1".to_string(),
                host: "192.168.1.10".to_string(),
                user: "ubuntu".to_string(),
                status: "healthy".to_string(),
                circuit_state: "closed".to_string(),
                used_slots: 2,
                total_slots: 8,
                speed_score: 1.5,
                last_error: None,
                consecutive_failures: 0,
                recovery_in_secs: None,
                failure_history: vec![],
                pressure_state: None,
                pressure_confidence: None,
                pressure_reason_code: None,
                pressure_policy_rule: None,
                pressure_disk_free_gb: None,
                pressure_disk_total_gb: None,
                pressure_disk_free_ratio: None,
                pressure_build_disk_free_gb: None,
                pressure_build_disk_total_gb: None,
                pressure_disk_io_util_pct: None,
                pressure_memory_pressure: None,
                pressure_telemetry_age_secs: None,
                pressure_telemetry_fresh: None,
                bypass: None,
            },
            WorkerStatusFromApi {
                id: "worker-2".to_string(),
                host: "192.168.1.11".to_string(),
                user: "ubuntu".to_string(),
                status: "offline".to_string(),
                circuit_state: "open".to_string(),
                used_slots: 0,
                total_slots: 8,
                speed_score: 0.0,
                last_error: Some("Connection refused".to_string()),
                consecutive_failures: 3,
                recovery_in_secs: Some(45),
                failure_history: vec![false, false, false],
                pressure_state: None,
                pressure_confidence: None,
                pressure_reason_code: None,
                pressure_policy_rule: None,
                pressure_disk_free_gb: None,
                pressure_disk_total_gb: None,
                pressure_disk_free_ratio: None,
                pressure_build_disk_free_gb: None,
                pressure_build_disk_total_gb: None,
                pressure_disk_io_util_pct: None,
                pressure_memory_pressure: None,
                pressure_telemetry_age_secs: None,
                pressure_telemetry_fresh: None,
                bypass: None,
            },
        ]
    }

    #[test]
    fn test_worker_table_creation() {
        let workers = sample_workers();
        let ctx = OutputContext::Plain;
        let table = WorkerTable::new(&workers, ctx);
        assert_eq!(table.context, OutputContext::Plain);
        assert_eq!(table.workers.len(), 2);
    }

    #[test]
    fn summary_preserves_other_workers_capacity_when_disk_limit_shrinks() {
        let mut workers = sample_workers();
        workers[0].total_slots = 1;
        workers[0].used_slots = 4;
        workers[1].status = "healthy".to_string();
        workers[1].used_slots = 2;
        let table = WorkerTable::new(&workers, OutputContext::Plain);
        assert_eq!(
            table.summary(),
            "Total: 2 workers (2 online) | Slots: 6/9 available"
        );
        assert_eq!(
            format_slot_bar(4, 1, OutputContext::Plain),
            Icons::slot_filled(OutputContext::Plain)
        );
        workers[0].total_slots = 0;
        let table = WorkerTable::new(&workers[..1], OutputContext::Plain);
        assert_eq!(
            table.summary(),
            "Total: 1 workers (1 online) | Slots: 0/0 available"
        );
        assert_eq!(format_slot_bar(4, 0, OutputContext::Plain), "-");
    }

    #[test]
    fn test_worker_table_from_workers() {
        let workers = sample_workers();
        let table = WorkerTable::from_workers(&workers);
        let _ = table.context; // Should not panic
    }

    #[test]
    fn test_format_circuit_state_closed() {
        let workers = sample_workers();
        let table = WorkerTable::new(&workers, OutputContext::Plain);
        let result = table.format_circuit_state(&workers[0]);
        assert_eq!(result, "OK");
    }

    #[test]
    fn test_format_circuit_state_open_with_recovery() {
        let workers = sample_workers();
        let table = WorkerTable::new(&workers, OutputContext::Plain);
        let result = table.format_circuit_state(&workers[1]);
        assert_eq!(result, "Open (45s)");
    }

    #[test]
    fn test_truncate_str_short() {
        assert_eq!(truncate_str("hello", 10), "hello");
    }

    #[test]
    fn test_truncate_str_long() {
        let result = truncate_str("hello_world_this_is_long", 10);
        assert!(result.len() <= 10);
        assert!(result.ends_with("..."));
    }

    #[test]
    fn test_render_plain_mode() {
        let workers = sample_workers();
        let console = RchConsole::with_context(OutputContext::Plain);
        let table = WorkerTable::new(&workers, OutputContext::Plain);
        // Should not panic
        table.render(&console);
    }

    #[test]
    fn test_render_machine_mode_no_output() {
        let workers = sample_workers();
        let console = RchConsole::with_context(OutputContext::Machine);
        let table = WorkerTable::new(&workers, OutputContext::Machine);
        // Should not panic, should do nothing
        table.render(&console);
    }

    #[test]
    fn test_render_empty_workers_plain() {
        let workers: Vec<WorkerStatusFromApi> = vec![];
        let console = RchConsole::with_context(OutputContext::Plain);
        let table = WorkerTable::new(&workers, OutputContext::Plain);
        // Should not panic
        table.render(&console);
    }

    #[test]
    fn test_format_status_plain_online() {
        let workers = sample_workers();
        let table = WorkerTable::new(&workers, OutputContext::Plain);
        let result = table.format_status_plain(&workers[0]);
        assert!(result.contains("Online"));
    }

    #[test]
    fn test_format_status_plain_offline() {
        let workers = sample_workers();
        let table = WorkerTable::new(&workers, OutputContext::Plain);
        let result = table.format_status_plain(&workers[1]);
        assert!(result.contains("Offline"));
    }

    #[test]
    fn test_format_slot_bar_full() {
        let bar = format_slot_bar(8, 8, OutputContext::Plain);
        // All slots used - should be all filled characters
        assert!(!bar.is_empty());
        assert!(!bar.contains('-')); // No empty slots in ASCII mode
    }

    #[test]
    fn test_format_slot_bar_empty() {
        let bar = format_slot_bar(0, 8, OutputContext::Plain);
        // No slots used - should be all empty characters
        assert!(!bar.is_empty());
        assert!(!bar.contains('#')); // No filled slots in ASCII mode
    }

    #[test]
    fn test_format_slot_bar_partial() {
        let bar = format_slot_bar(2, 8, OutputContext::Plain);
        // 2 of 8 slots used
        assert_eq!(bar.len(), 8); // 8 characters
        assert!(bar.starts_with("##")); // 2 filled
        assert!(bar.ends_with("-")); // Rest empty
    }

    #[test]
    fn test_format_slot_bar_zero_total() {
        let bar = format_slot_bar(0, 0, OutputContext::Plain);
        assert_eq!(bar, "-");
    }

    #[test]
    fn test_format_speed_score_fast() {
        let speed = format_speed_score(1.8, OutputContext::Plain);
        assert!(speed.contains("1.8x"));
        assert!(speed.contains("!")); // Lightning ASCII fallback
    }

    #[test]
    fn test_format_speed_score_normal() {
        let speed = format_speed_score(1.0, OutputContext::Plain);
        assert_eq!(speed, "1.0x");
    }

    #[test]
    fn test_format_speed_score_slow() {
        let speed = format_speed_score(0.5, OutputContext::Plain);
        assert!(speed.contains("0.5x"));
        assert!(speed.contains("~")); // Slow indicator
    }

    #[test]
    fn test_format_speed_score_zero() {
        let speed = format_speed_score(0.0, OutputContext::Plain);
        assert_eq!(speed, "-");
    }
}