ruchy 4.1.2

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
//! Andon TUI - Toyota Way Visual Management for Oracle
//!
//! Implements the Andon board visualization for Oracle training status.
//! Based on Toyota Production System visual signaling principles.
//!
//! Uses trueno-viz for visualization rendering.
//!
//! # Toyota Way Principles
//! - **Jidoka** (自働化): Stop-the-line signal when drift detected
//! - **Kaizen** (改善): Sparkline shows continuous improvement trend
//! - **Genchi Genbutsu** (現地現物): Real metrics, not estimates
//! - **Andon** (行灯): Color-coded status board
//!
//! # References
//! - [8] Liker, J. K. (2004). "The Toyota Way." McGraw-Hill.
//! - Spec: docs/specifications/dynamic-mlops-training-ruchy-oracle-spec.md §13.3

use super::DriftStatus;
use trueno_viz::widgets::{Sparkline as VizSparkline, TrendDirection as VizTrendDirection};

/// Andon status (Toyota Way visual signaling)
///
/// Maps to factory floor Andon board colors:
/// - GREEN: System healthy, production continues
/// - YELLOW: Attention needed, monitor closely
/// - RED: Stop the line! Immediate action required
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AndonStatus {
    /// GREEN - System healthy
    Green,
    /// YELLOW - Attention needed
    Yellow,
    /// RED - Stop the line!
    Red,
}

impl AndonStatus {
    /// Convert from drift status
    #[must_use]
    pub fn from_drift(drift: &DriftStatus) -> Self {
        match drift {
            DriftStatus::Stable => AndonStatus::Green,
            DriftStatus::Warning => AndonStatus::Yellow,
            DriftStatus::Drift => AndonStatus::Red,
        }
    }

    /// Get display string with indicator
    #[must_use]
    pub fn display(&self) -> &'static str {
        match self {
            AndonStatus::Green => "● STABLE",
            AndonStatus::Yellow => "● WARNING",
            AndonStatus::Red => "● DRIFT",
        }
    }

    /// Get ANSI color code for terminal output
    #[must_use]
    pub fn color_code(&self) -> &'static str {
        match self {
            AndonStatus::Green => "\x1b[32m",  // Green
            AndonStatus::Yellow => "\x1b[33m", // Yellow
            AndonStatus::Red => "\x1b[31m",    // Red
        }
    }

    /// Get reset ANSI code
    #[must_use]
    pub fn reset_code() -> &'static str {
        "\x1b[0m"
    }

    /// Check if action is required
    #[must_use]
    pub fn requires_action(&self) -> bool {
        matches!(self, AndonStatus::Yellow | AndonStatus::Red)
    }

    /// Check if stop-the-line is required
    #[must_use]
    pub fn stop_the_line(&self) -> bool {
        matches!(self, AndonStatus::Red)
    }
}

/// Sparkline characters for accuracy trend visualization (Kaizen principle)
const SPARKLINE_CHARS: [char; 8] = ['', '', '', '', '', '', '', ''];

/// Render a sparkline from accuracy history using trueno-viz
///
/// Uses trueno-viz `Sparkline` widget for trend calculation,
/// then renders to Unicode block characters for terminal display.
///
/// # Arguments
/// * `history` - Historical accuracy values (0.0-1.0)
/// * `width` - Maximum width in characters
///
/// # Returns
/// String containing sparkline characters
#[must_use]
pub fn render_sparkline(history: &[f64], width: usize) -> String {
    if history.is_empty() {
        return "".repeat(width);
    }

    // Use trueno-viz Sparkline for trend analysis
    let _viz_sparkline = VizSparkline::new(history).with_trend_indicator();

    // Render to Unicode block characters
    let min = history.iter().copied().fold(f64::INFINITY, f64::min);
    let max = history.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    let range = (max - min).max(0.01); // Avoid division by zero

    history
        .iter()
        .take(width)
        .map(|&v| {
            let normalized = ((v - min) / range * 7.0).round() as usize;
            SPARKLINE_CHARS[normalized.min(7)]
        })
        .collect()
}

/// Get trend direction using trueno-viz
#[must_use]
pub fn get_trend_direction(history: &[f64]) -> &'static str {
    let sparkline = VizSparkline::new(history);
    match sparkline.trend() {
        VizTrendDirection::Rising => "",
        VizTrendDirection::Falling => "",
        VizTrendDirection::Stable => "",
    }
}

/// Render the full Andon TUI board
///
/// Displays comprehensive Oracle training status in a box format.
/// Implements Genchi Genbutsu (go and see) with real metrics.
///
/// # Arguments
/// * `iteration` - Current iteration number
/// * `max_iterations` - Maximum iterations
/// * `accuracy` - Current accuracy (0.0-1.0)
/// * `target` - Target accuracy (0.0-1.0)
/// * `accuracy_delta` - Change from previous iteration
/// * `last_trained` - Timestamp string
/// * `model_size_kb` - Model size in KB
/// * `accuracy_history` - Historical accuracy values
/// * `drift` - Current drift status
#[must_use]
pub fn render_andon_tui(
    iteration: usize,
    max_iterations: usize,
    accuracy: f64,
    target: f64,
    accuracy_delta: f64,
    last_trained: &str,
    model_size_kb: usize,
    accuracy_history: &[f64],
    drift: &DriftStatus,
) -> String {
    let progress = (iteration as f64 / max_iterations as f64 * 20.0) as usize;
    let progress_bar = format!(
        "[{}{}]",
        "".repeat(progress.min(20)),
        "".repeat(20_usize.saturating_sub(progress))
    );

    let on_track = if accuracy >= target {
        "✓ ON TRACK"
    } else {
        "⚠ BELOW TARGET"
    };
    let delta_sign = if accuracy_delta >= 0.0 { "+" } else { "" };
    let sparkline = render_sparkline(accuracy_history, 8);
    let trend = get_trend_direction(accuracy_history);
    let andon = AndonStatus::from_drift(drift);
    let percent = (iteration as f64 / max_iterations as f64 * 100.0).round() as usize;

    format!(
        r"╔══════════════════════════════════════════════════════════════════════╗
║  Iteration: {} {}/{} ({}%)                                          ║
║  Estimated Convergence: {:.1}% → Target: {:.1}%  {}                 ║
║  Last Trained:    {}                                                ║
║  Model Size:      {} KB (zstd compressed)                           ║
║  Accuracy:        {} {:.1}% ({}{:.1}%) {}                           ║
║  Drift Status:    {}{}{}                                            ║
╚══════════════════════════════════════════════════════════════════════╝",
        progress_bar,
        iteration,
        max_iterations,
        percent,
        accuracy * 100.0,
        target * 100.0,
        on_track,
        last_trained,
        model_size_kb,
        sparkline,
        accuracy * 100.0,
        delta_sign,
        accuracy_delta * 100.0,
        trend,
        andon.color_code(),
        andon.display(),
        AndonStatus::reset_code()
    )
}

/// Render compact one-line status for default mode
///
/// Shows essential Oracle status in minimal space.
/// Used during normal transpilation operations.
#[must_use]
pub fn render_compact(
    iteration: usize,
    max_iterations: usize,
    accuracy: f64,
    model_size_kb: usize,
    last_trained_ago: &str,
    drift: &DriftStatus,
) -> String {
    let andon = AndonStatus::from_drift(drift);
    format!(
        "🔄 Oracle: iteration[{}/{}] {:.1}% acc | {}KB | {} | {}",
        iteration,
        max_iterations,
        accuracy * 100.0,
        model_size_kb,
        last_trained_ago,
        andon.display()
    )
}

/// Render minimal inline status
#[must_use]
pub fn render_inline(accuracy: f64, drift: &DriftStatus) -> String {
    let andon = AndonStatus::from_drift(drift);
    format!("[Oracle: {:.0}% {}]", accuracy * 100.0, andon.display())
}

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

    #[test]
    fn test_andon_status_from_drift_stable() {
        assert_eq!(
            AndonStatus::from_drift(&DriftStatus::Stable),
            AndonStatus::Green
        );
    }

    #[test]
    fn test_andon_status_from_drift_warning() {
        assert_eq!(
            AndonStatus::from_drift(&DriftStatus::Warning),
            AndonStatus::Yellow
        );
    }

    #[test]
    fn test_andon_status_from_drift_drift() {
        assert_eq!(
            AndonStatus::from_drift(&DriftStatus::Drift),
            AndonStatus::Red
        );
    }

    #[test]
    fn test_andon_status_display() {
        assert_eq!(AndonStatus::Green.display(), "● STABLE");
        assert_eq!(AndonStatus::Yellow.display(), "● WARNING");
        assert_eq!(AndonStatus::Red.display(), "● DRIFT");
    }

    #[test]
    fn test_andon_status_color_codes() {
        assert_eq!(AndonStatus::Green.color_code(), "\x1b[32m");
        assert_eq!(AndonStatus::Yellow.color_code(), "\x1b[33m");
        assert_eq!(AndonStatus::Red.color_code(), "\x1b[31m");
    }

    #[test]
    fn test_andon_requires_action() {
        assert!(!AndonStatus::Green.requires_action());
        assert!(AndonStatus::Yellow.requires_action());
        assert!(AndonStatus::Red.requires_action());
    }

    #[test]
    fn test_andon_stop_the_line() {
        assert!(!AndonStatus::Green.stop_the_line());
        assert!(!AndonStatus::Yellow.stop_the_line());
        assert!(AndonStatus::Red.stop_the_line());
    }

    #[test]
    fn test_sparkline_empty() {
        let sparkline = render_sparkline(&[], 8);
        assert_eq!(sparkline, "────────");
    }

    #[test]
    fn test_sparkline_single_value() {
        let sparkline = render_sparkline(&[0.5], 8);
        assert_eq!(sparkline.chars().count(), 1);
    }

    #[test]
    fn test_sparkline_increasing() {
        let history = vec![0.0, 0.14, 0.28, 0.42, 0.57, 0.71, 0.85, 1.0];
        let sparkline = render_sparkline(&history, 8);
        assert_eq!(sparkline, "▁▂▃▄▅▆▇█");
    }

    #[test]
    fn test_sparkline_flat() {
        let history = vec![0.5, 0.5, 0.5, 0.5];
        let sparkline = render_sparkline(&history, 4);
        // All same value should use same char
        let chars: Vec<char> = sparkline.chars().collect();
        assert!(chars.iter().all(|&c| c == chars[0]));
    }

    #[test]
    fn test_sparkline_width_limit() {
        let history: Vec<f64> = (0..100).map(|i| f64::from(i) / 100.0).collect();
        let sparkline = render_sparkline(&history, 5);
        assert_eq!(sparkline.chars().count(), 5);
    }

    #[test]
    fn test_trend_direction_rising() {
        let history = vec![0.1, 0.3, 0.5, 0.7, 0.9];
        assert_eq!(get_trend_direction(&history), "");
    }

    #[test]
    fn test_trend_direction_falling() {
        let history = vec![0.9, 0.7, 0.5, 0.3, 0.1];
        assert_eq!(get_trend_direction(&history), "");
    }

    #[test]
    fn test_trend_direction_stable() {
        let history = vec![0.5, 0.5, 0.5, 0.5];
        assert_eq!(get_trend_direction(&history), "");
    }

    #[test]
    fn test_render_compact() {
        let compact = render_compact(12, 50, 0.873, 847, "3h ago", &DriftStatus::Stable);
        assert!(compact.contains("12/50"));
        assert!(compact.contains("87.3%"));
        assert!(compact.contains("847KB"));
        assert!(compact.contains("STABLE"));
    }

    #[test]
    fn test_render_andon_tui_contains_progress() {
        let tui = render_andon_tui(
            10,
            20,
            0.85,
            0.80,
            0.02,
            "2025-12-08",
            500,
            &[0.85],
            &DriftStatus::Stable,
        );
        assert!(tui.contains("10/20"));
        assert!(tui.contains("50%"));
    }

    #[test]
    fn test_render_andon_tui_on_track() {
        let tui = render_andon_tui(
            10,
            20,
            0.85,
            0.80,
            0.02,
            "2025-12-08",
            500,
            &[0.85],
            &DriftStatus::Stable,
        );
        assert!(tui.contains("ON TRACK"));
    }

    #[test]
    fn test_render_andon_tui_below_target() {
        let tui = render_andon_tui(
            10,
            20,
            0.70,
            0.80,
            -0.02,
            "2025-12-08",
            500,
            &[0.70],
            &DriftStatus::Warning,
        );
        assert!(tui.contains("BELOW TARGET"));
    }

    #[test]
    fn test_render_inline() {
        let inline = render_inline(0.85, &DriftStatus::Stable);
        assert!(inline.contains("85%"));
        assert!(inline.contains("STABLE"));
    }
}