nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! TUI Diagnostics Bridge
//!
//! Bridges the Two-Phase IR analyzer with the TUI editor, providing
//! real-time error highlighting and inline diagnostics display.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────────┐
//! │  Editor Buffer                                                          │
//! │       │                                                                 │
//! │       │ on_change()                                                     │
//! │       ▼                                                                 │
//! │  ┌─────────────────────────────────────────────────────────┐           │
//! │  │  DiagnosticsEngine                                       │           │
//! │  │  ├── analyze_text() → Parse + Analyze                    │           │
//! │  │  ├── get_diagnostics() → Vec<TuiDiagnostic>              │           │
//! │  │  └── get_line_diagnostics(line) → For gutter display     │           │
//! │  └─────────────────────────────────────────────────────────┘           │
//! │       │                                                                 │
//! │       ▼                                                                 │
//! │  Styled Line Rendering (error highlights, gutter icons)                 │
//! └─────────────────────────────────────────────────────────────────────────┘
//! ```

use ratatui::style::{Color, Modifier, Style};

use crate::ast::analyzer::{analyze, AnalyzeError};
use crate::ast::raw::{self, ParseError};
use crate::source::FileId;

/// Severity level for diagnostics
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticSeverity {
    /// Critical error that prevents workflow execution
    Error,
    /// Non-blocking warning
    Warning,
    /// Informational hint
    Hint,
}

impl DiagnosticSeverity {
    /// Get the gutter icon for this severity
    pub fn gutter_icon(&self) -> &'static str {
        match self {
            DiagnosticSeverity::Error => "",
            DiagnosticSeverity::Warning => "",
            DiagnosticSeverity::Hint => "",
        }
    }

    /// Get the style for this severity (Solarized-compatible)
    pub fn style(&self) -> Style {
        match self {
            DiagnosticSeverity::Error => Style::default()
                .fg(Color::Rgb(220, 50, 47)) // Solarized red
                .add_modifier(Modifier::BOLD),
            DiagnosticSeverity::Warning => Style::default()
                .fg(Color::Rgb(203, 75, 22)) // Solarized orange
                .add_modifier(Modifier::BOLD),
            DiagnosticSeverity::Hint => Style::default()
                .fg(Color::Rgb(38, 139, 210)) // Solarized blue
                .add_modifier(Modifier::ITALIC),
        }
    }

    /// Get the underline style for inline highlighting
    pub fn underline_style(&self) -> Style {
        match self {
            DiagnosticSeverity::Error => Style::default()
                .fg(Color::Rgb(220, 50, 47))
                .add_modifier(Modifier::UNDERLINED),
            DiagnosticSeverity::Warning => Style::default()
                .fg(Color::Rgb(203, 75, 22))
                .add_modifier(Modifier::UNDERLINED),
            DiagnosticSeverity::Hint => Style::default()
                .fg(Color::Rgb(38, 139, 210))
                .add_modifier(Modifier::UNDERLINED),
        }
    }
}

/// A diagnostic message for the TUI editor
#[derive(Debug, Clone)]
pub struct TuiDiagnostic {
    /// Error code (e.g., "NIKA-140")
    pub code: String,
    /// Human-readable message
    pub message: String,
    /// Severity level
    pub severity: DiagnosticSeverity,
    /// Start line (0-indexed)
    pub start_line: usize,
    /// Start column (0-indexed)
    pub start_col: usize,
    /// End line (0-indexed)
    pub end_line: usize,
    /// End column (0-indexed)
    pub end_col: usize,
}

impl TuiDiagnostic {
    /// Create from an AnalyzeError
    pub fn from_analyze_error(error: &AnalyzeError, source: &str) -> Self {
        let (start_line, start_col) = offset_to_line_col(error.span.start.into(), source);
        let (end_line, end_col) = offset_to_line_col(error.span.end.into(), source);

        Self {
            code: error.kind.code().to_string(),
            message: error.message.clone(),
            severity: DiagnosticSeverity::Error, // All analyze errors are errors
            start_line,
            start_col,
            end_line,
            end_col,
        }
    }

    /// Create from a ParseError
    pub fn from_parse_error(error: &ParseError, source: &str) -> Self {
        let (start_line, start_col) = offset_to_line_col(error.span.start.into(), source);
        let (end_line, end_col) = offset_to_line_col(error.span.end.into(), source);

        Self {
            code: error.kind.code().to_string(),
            message: error.message.clone(),
            severity: DiagnosticSeverity::Error,
            start_line,
            start_col,
            end_line,
            end_col,
        }
    }

    /// Check if this diagnostic affects a given line
    pub fn affects_line(&self, line: usize) -> bool {
        line >= self.start_line && line <= self.end_line
    }

    /// Get the column range for a specific line
    pub fn column_range_for_line(&self, line: usize) -> Option<(usize, usize)> {
        if !self.affects_line(line) {
            return None;
        }

        let start = if line == self.start_line {
            self.start_col
        } else {
            0
        };

        let end = if line == self.end_line {
            self.end_col
        } else {
            usize::MAX // Full line
        };

        Some((start, end))
    }

    /// Format for display in a message area
    pub fn display_message(&self) -> String {
        format!("[{}] {}", self.code, self.message)
    }

    /// Format with location for status bar
    pub fn status_message(&self) -> String {
        format!(
            "[{}] line {}:{} - {}",
            self.code,
            self.start_line + 1,
            self.start_col + 1,
            self.message
        )
    }
}

/// Convert byte offset to (line, column) - both 0-indexed
fn offset_to_line_col(offset: usize, source: &str) -> (usize, usize) {
    let mut line = 0;
    let mut col = 0;

    for (i, ch) in source.char_indices() {
        if i >= offset {
            break;
        }
        if ch == '\n' {
            line += 1;
            col = 0;
        } else {
            col += 1;
        }
    }

    (line, col)
}

/// Diagnostics engine for the TUI editor
///
/// Caches analysis results and provides efficient line-based lookups.
#[derive(Debug, Default)]
pub struct DiagnosticsEngine {
    /// Current diagnostics
    diagnostics: Vec<TuiDiagnostic>,
    /// Hash of last analyzed text (for caching)
    last_text_hash: u64,
}

impl DiagnosticsEngine {
    /// Create a new diagnostics engine
    pub fn new() -> Self {
        Self {
            diagnostics: Vec::new(),
            last_text_hash: 0,
        }
    }

    /// Analyze text and update diagnostics
    ///
    /// Returns true if diagnostics changed.
    pub fn analyze(&mut self, source: &str) -> bool {
        // Quick hash check to avoid re-analyzing identical content
        let hash = xxhash_rust::xxh3::xxh3_64(source.as_bytes());
        if hash == self.last_text_hash {
            return false;
        }
        self.last_text_hash = hash;

        self.diagnostics.clear();

        let file_id = FileId(0);

        // Phase 1: Parse to Raw AST
        match raw::parse(source, file_id) {
            Ok(raw_workflow) => {
                // Phase 2: Analyze
                let result = analyze(raw_workflow);

                // Convert analysis errors to TUI diagnostics
                for error in &result.errors {
                    self.diagnostics
                        .push(TuiDiagnostic::from_analyze_error(error, source));
                }
            }
            Err(parse_error) => {
                // Parse failed
                self.diagnostics
                    .push(TuiDiagnostic::from_parse_error(&parse_error, source));
            }
        }

        true
    }

    /// Get all diagnostics
    pub fn diagnostics(&self) -> &[TuiDiagnostic] {
        &self.diagnostics
    }

    /// Get diagnostics affecting a specific line
    pub fn diagnostics_for_line(&self, line: usize) -> Vec<&TuiDiagnostic> {
        self.diagnostics
            .iter()
            .filter(|d| d.affects_line(line))
            .collect()
    }

    /// Check if a line has any diagnostics
    pub fn has_diagnostics_on_line(&self, line: usize) -> bool {
        self.diagnostics.iter().any(|d| d.affects_line(line))
    }

    /// Get the most severe diagnostic on a line (for gutter icon)
    pub fn most_severe_on_line(&self, line: usize) -> Option<&TuiDiagnostic> {
        self.diagnostics_for_line(line)
            .into_iter()
            .min_by_key(|d| match d.severity {
                DiagnosticSeverity::Error => 0,
                DiagnosticSeverity::Warning => 1,
                DiagnosticSeverity::Hint => 2,
            })
    }

    /// Get error count
    pub fn error_count(&self) -> usize {
        self.diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Error)
            .count()
    }

    /// Get warning count
    pub fn warning_count(&self) -> usize {
        self.diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Warning)
            .count()
    }

    /// Clear all diagnostics
    pub fn clear(&mut self) {
        self.diagnostics.clear();
        self.last_text_hash = 0;
    }

    /// Check if there are any errors
    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| d.severity == DiagnosticSeverity::Error)
    }

    /// Get the first error (for status bar display)
    pub fn first_error(&self) -> Option<&TuiDiagnostic> {
        self.diagnostics
            .iter()
            .find(|d| d.severity == DiagnosticSeverity::Error)
    }
}

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

    #[test]
    fn test_offset_to_line_col() {
        let source = "line1\nline2\nline3";
        assert_eq!(offset_to_line_col(0, source), (0, 0));
        assert_eq!(offset_to_line_col(3, source), (0, 3));
        assert_eq!(offset_to_line_col(6, source), (1, 0)); // Start of line2
        assert_eq!(offset_to_line_col(8, source), (1, 2)); // "ne" of line2
        assert_eq!(offset_to_line_col(12, source), (2, 0)); // Start of line3
    }

    #[test]
    fn test_diagnostic_affects_line() {
        let diag = TuiDiagnostic {
            code: "NIKA-140".to_string(),
            message: "Test error".to_string(),
            severity: DiagnosticSeverity::Error,
            start_line: 2,
            start_col: 5,
            end_line: 4,
            end_col: 10,
        };

        assert!(!diag.affects_line(0));
        assert!(!diag.affects_line(1));
        assert!(diag.affects_line(2));
        assert!(diag.affects_line(3));
        assert!(diag.affects_line(4));
        assert!(!diag.affects_line(5));
    }

    #[test]
    fn test_diagnostic_column_range() {
        let diag = TuiDiagnostic {
            code: "NIKA-140".to_string(),
            message: "Test error".to_string(),
            severity: DiagnosticSeverity::Error,
            start_line: 2,
            start_col: 5,
            end_line: 4,
            end_col: 10,
        };

        // Line before range
        assert_eq!(diag.column_range_for_line(1), None);

        // Start line
        assert_eq!(diag.column_range_for_line(2), Some((5, usize::MAX)));

        // Middle line
        assert_eq!(diag.column_range_for_line(3), Some((0, usize::MAX)));

        // End line
        assert_eq!(diag.column_range_for_line(4), Some((0, 10)));

        // Line after range
        assert_eq!(diag.column_range_for_line(5), None);
    }

    #[test]
    fn test_single_line_diagnostic_column_range() {
        let diag = TuiDiagnostic {
            code: "NIKA-140".to_string(),
            message: "Test error".to_string(),
            severity: DiagnosticSeverity::Error,
            start_line: 3,
            start_col: 5,
            end_line: 3,
            end_col: 15,
        };

        assert_eq!(diag.column_range_for_line(3), Some((5, 15)));
    }

    #[test]
    fn test_diagnostics_engine_empty() {
        let engine = DiagnosticsEngine::new();
        assert!(engine.diagnostics().is_empty());
        assert!(!engine.has_errors());
        assert_eq!(engine.error_count(), 0);
    }

    #[test]
    fn test_diagnostics_engine_valid_workflow() {
        let mut engine = DiagnosticsEngine::new();
        let yaml = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
"#;

        engine.analyze(yaml);
        assert!(
            !engine.has_errors(),
            "Should have no errors: {:?}",
            engine.diagnostics()
        );
    }

    #[test]
    fn test_diagnostics_engine_duplicate_task() {
        let mut engine = DiagnosticsEngine::new();
        let yaml = r#"schema: nika/workflow@0.12
workflow: test

tasks:
  - id: step1
    infer: "Hello"
  - id: step1
    exec: "echo duplicate"
"#;

        engine.analyze(yaml);
        assert!(engine.has_errors());
        assert!(engine.diagnostics().iter().any(|d| d.code == "NIKA-141"));
    }

    #[test]
    fn test_diagnostics_engine_parse_error() {
        let mut engine = DiagnosticsEngine::new();
        let yaml = "schema: nika/workflow@0.12\ntasks: [unclosed";

        engine.analyze(yaml);
        assert!(engine.has_errors());
        assert!(engine.diagnostics().iter().any(|d| d.code == "NIKA-160"));
    }

    #[test]
    fn test_severity_styles() {
        let error_style = DiagnosticSeverity::Error.style();
        let warning_style = DiagnosticSeverity::Warning.style();
        let hint_style = DiagnosticSeverity::Hint.style();

        // Just verify they return valid styles
        assert!(error_style.fg.is_some());
        assert!(warning_style.fg.is_some());
        assert!(hint_style.fg.is_some());
    }

    #[test]
    fn test_gutter_icons() {
        assert_eq!(DiagnosticSeverity::Error.gutter_icon(), "");
        assert_eq!(DiagnosticSeverity::Warning.gutter_icon(), "");
        assert_eq!(DiagnosticSeverity::Hint.gutter_icon(), "");
    }

    #[test]
    fn test_diagnostics_engine_caching() {
        let mut engine = DiagnosticsEngine::new();
        let yaml = r#"schema: nika/workflow@0.12
workflow: test
tasks:
  - id: step1
    infer: "Hello"
"#;

        // First analysis
        let changed1 = engine.analyze(yaml);
        assert!(changed1);

        // Same content - should not change
        let changed2 = engine.analyze(yaml);
        assert!(!changed2);

        // Different content
        let changed3 = engine.analyze("schema: nika/workflow@0.12\n");
        assert!(changed3);
    }

    #[test]
    fn test_most_severe_on_line() {
        // This would require multiple diagnostics on the same line
        // which is harder to construct, so we test basic functionality
        let mut engine = DiagnosticsEngine::new();
        let yaml = r#"schema: nika/workflow@0.12
workflow: test
tasks:
  - id: step1
    infer: "Hello"
"#;
        engine.analyze(yaml);

        // No errors, so no most severe
        assert!(engine.most_severe_on_line(0).is_none());
    }
}