kelora 2.0.0

A command-line log analysis tool with embedded Rhai scripting
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
use rhai::{EvalAltResult, Map, Scope};
use std::sync::{Arc, Mutex};

use super::RhaiEngine;

#[derive(Debug, Clone)]
pub struct DebugConfig {
    pub verbosity: u8,
    pub use_emoji: bool,
}

impl DebugConfig {
    pub fn new(verbose_count: u8) -> Self {
        DebugConfig {
            verbosity: verbose_count,
            use_emoji: true, // Default to true, will be overridden
        }
    }

    pub fn with_emoji(mut self, use_emoji: bool) -> Self {
        self.use_emoji = use_emoji;
        self
    }

    pub fn is_enabled(&self) -> bool {
        self.verbosity > 0
    }
}

#[derive(Debug, Clone, Default)]
pub struct ExecutionContext {
    pub position: Option<rhai::Position>,
    pub source_snippet: Option<String>,
    pub last_operation: Option<String>,
    pub error_location: Option<String>,
}

pub struct DebugTracker {
    pub config: DebugConfig,
    pub(crate) context: Arc<Mutex<ExecutionContext>>,
}

impl DebugTracker {
    pub fn new(config: DebugConfig) -> Self {
        DebugTracker {
            config,
            context: Arc::new(Mutex::new(ExecutionContext::default())),
        }
    }

    pub fn log_basic(&self, message: &str) {
        if self.config.is_enabled() && self.config.verbosity >= 1 {
            eprintln!("{}", message);
        }
    }

    pub fn log_detailed(&self, stage: &str, event_num: u64, operation: &str) {
        if self.config.is_enabled() && self.config.verbosity >= 2 {
            eprintln!("Trace: Event #{} {}{}", event_num, stage, operation);
        }
    }

    pub fn log_step(&self, step_info: &str, result: &str) {
        if self.config.is_enabled() && self.config.verbosity >= 3 {
            eprintln!("{}{}", step_info, result);
        }
    }

    pub fn update_context(&self, position: Option<rhai::Position>, source: Option<&str>) {
        if self.config.is_enabled() {
            if let Ok(mut ctx) = self.context.lock() {
                ctx.position = position;
                ctx.source_snippet = source.map(|s| s.to_string());
            }
        }
    }

    pub fn get_context(&self) -> ExecutionContext {
        if let Ok(ctx) = self.context.lock() {
            ctx.clone()
        } else {
            ExecutionContext::default()
        }
    }
}

impl Clone for DebugTracker {
    fn clone(&self) -> Self {
        DebugTracker {
            config: self.config.clone(),
            context: Arc::clone(&self.context),
        }
    }
}

pub struct ErrorEnhancer {
    debug_config: DebugConfig,
}

impl ErrorEnhancer {
    pub fn new(debug_config: DebugConfig) -> Self {
        ErrorEnhancer { debug_config }
    }

    pub fn enhance_error(
        &self,
        error: &EvalAltResult,
        scope: &Scope,
        script: &str,
        stage: &str,
        execution_context: &ExecutionContext,
    ) -> String {
        let mut output = String::new();
        let hint_prefix = if self.debug_config.use_emoji {
            "💡"
        } else {
            "Hint:"
        };

        // Header mirrors the non-debug diagnostic ("<stage> error"). The caller
        // already prefixes "<Stage> error:", so a "Error: Stage <stage> failed"
        // header here read as the redundant "Filter error: Error: Stage filter failed".
        if self.debug_config.use_emoji {
            output.push_str(&format!("🔸 {stage} error\n"));
        } else {
            output.push_str(&format!("{stage} error\n"));
        }
        output.push_str(&format!("  Code: {}\n", script.trim()));
        output.push_str(&format!("  Error: {}\n", error));

        if let Some(pos) = &execution_context.position {
            output.push_str(&format!("   Position: {}\n", pos));
        }

        if let Some(suggestions) = self.generate_suggestions(error, scope, Some(script)) {
            output.push_str(&format!("   {hint_prefix} {}\n", suggestions));
        }

        if self.debug_config.is_enabled() {
            output.push_str("\n   Variables in scope:\n");
            for (name, _is_const, value) in scope.iter() {
                let preview = format!("{:?}", value);
                let preview = if preview.len() > 50 {
                    format!("{}...", &preview[..47])
                } else {
                    preview
                };
                output.push_str(&format!(
                    "{}: {} = {}\n",
                    name,
                    value.type_name(),
                    preview
                ));
            }
        }

        output.push_str(&self.get_stage_help(stage, error));
        output
    }

    pub(crate) fn generate_suggestions(
        &self,
        error: &EvalAltResult,
        scope: &Scope,
        script: Option<&str>,
    ) -> Option<String> {
        let base = match error {
            EvalAltResult::ErrorVariableNotFound(var_name, _) => {
                // The most common newcomer mistake is referencing an event field
                // without the `e.` prefix (e.g. `status` instead of `e.status`).
                // If the bare identifier matches—or closely resembles—a real field
                // on the event, point straight at `e.<field>` rather than fall back
                // to scope-variable lookups that only know about e/meta/conf/line.
                if let Some(hint) = self.suggest_event_field_prefix(var_name, scope) {
                    Some(hint)
                } else {
                    let similar = self.find_similar_variables(var_name, scope);
                    if !similar.is_empty() {
                        Some(format!("Did you mean: {}?", similar.join(", ")))
                    } else if var_name.contains('.') {
                        Some("Check if the field exists and use safe access like 'if \"field\" in e { e.field } else { \"default\" }'".to_string())
                    } else if var_name.starts_with("e.") {
                        Some("Try using bracket notation for special characters: e[\"field-name\"] or e[\"field.with.dots\"]".to_string())
                    } else {
                        Some("Available variables: e (event), meta (metadata), conf (initialization data), line (raw line)".to_string())
                    }
                }
            }
            EvalAltResult::ErrorPropertyNotFound(prop_name, _) => {
                let mut suggestions = Vec::new();

                if let Some(fields) = self.event_field_names(scope) {
                    let similar: Vec<_> = fields
                        .iter()
                        .filter(|name| {
                            let sim = self.calculate_similarity(
                                &prop_name.to_lowercase(),
                                &name.to_lowercase(),
                            );
                            sim > 0.6
                                || name.contains(prop_name)
                                || prop_name.contains(name.as_str())
                        })
                        .take(3)
                        .cloned()
                        .collect();
                    if !similar.is_empty() {
                        suggestions.push(format!("Did you mean field: {}?", similar.join(", ")));
                    } else {
                        let preview: Vec<_> = fields.into_iter().take(5).collect();
                        if !preview.is_empty() {
                            suggestions.push(format!(
                                "Available fields include: {}{}",
                                preview.join(", "),
                                if preview.len() == 5 { " ..." } else { "" }
                            ));
                        }
                    }
                }

                suggestions
                    .push("Try `--stats` or `-F inspect` to see available fields".to_string());
                Some(suggestions.join(" "))
            }
            EvalAltResult::ErrorIndexNotFound(index, _) => Some(format!(
                "Index '{}' not found. Check array bounds with 'if e.array.len() > {} {{ ... }}'",
                index, index
            )),
            EvalAltResult::ErrorFunctionNotFound(func_sig, _) => {
                self.suggest_function_alternatives(func_sig)
            }
            EvalAltResult::ErrorMismatchDataType(expected, actual, _) => {
                let mut hints = vec![format!(
                    "Type mismatch: expected {}, got {}.",
                    expected, actual
                )];

                if expected.contains("bool") {
                    hints.push(
                        "Filters must return true/false; use comparisons like `e.level == \"ERROR\"` or `contains(...)`"
                            .to_string(),
                    );
                }
                if actual.contains("()") || expected.contains("()") {
                    hints.push(
                        "Missing fields return () by default; guard with e.has(\"field\") or e.get(\"field\", default) before chaining"
                            .to_string(),
                    );
                }
                hints.push(
                    "Use type_of() to check types or to_string()/to_number()/parse_json() for conversion"
                        .to_string(),
                );

                Some(hints.join(" "))
            }
            EvalAltResult::ErrorRuntime(msg, _) => {
                let msg_str = msg.to_string();
                if msg_str.contains("got ()") {
                    Some(
                        "Received (), which means a field is missing or returned no value. \
                         Use e.get_path('field.path', default) to provide defaults, \
                         or e.has_path('field.path') to check if a field exists first."
                            .to_string(),
                    )
                } else {
                    None
                }
            }
            // Traversing into a missing intermediate (e.g. `e.user.role` when
            // `user` is absent) leaves a () in the chain, so the next property
            // access fails with a getter-not-registered error on type '()'.
            // Surface the same missing-field guidance the other paths give.
            EvalAltResult::ErrorDotExpr(msg, _) if msg.contains("type '()'") => Some(
                "A field in the path is missing, so the value is (). \
                 Use e.get_path('a.b', default) to read nested fields safely, \
                 or e.has_path('a.b') to check the path exists first."
                    .to_string(),
            ),
            _ => None,
        };

        let raw_string_hint = script.and_then(|script| Self::raw_string_hint(error, script));

        match (base, raw_string_hint) {
            (Some(base), Some(hint)) => Some(format!("{} {}", base, hint)),
            (Some(base), None) => Some(base),
            (None, Some(hint)) => Some(hint),
            (None, None) => None,
        }
    }

    pub(crate) fn raw_string_hint(error: &EvalAltResult, script: &str) -> Option<String> {
        match error {
            EvalAltResult::ErrorParsing(_, _) if Self::contains_rust_raw_string(script) => Some(
                "It looks like a Rust raw string (r\"...\"). Rhai raw strings use #\"...\"# (or ##\"...\"## for embedded quotes)."
                    .to_string(),
            ),
            _ => None,
        }
    }

    fn contains_rust_raw_string(script: &str) -> bool {
        let bytes = script.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            if bytes[i] == b'r' {
                let prev = if i == 0 { None } else { Some(bytes[i - 1]) };
                let starts_token = prev.is_none_or(|c| !Self::is_ident_char(c));
                if starts_token {
                    let mut j = i + 1;
                    while j < bytes.len() && bytes[j] == b'#' {
                        j += 1;
                    }
                    if j < bytes.len() && bytes[j] == b'"' {
                        return true;
                    }
                }
            }
            i += 1;
        }
        false
    }

    fn is_ident_char(byte: u8) -> bool {
        byte.is_ascii_alphanumeric() || byte == b'_'
    }

    fn suggest_function_alternatives(&self, func_sig: &str) -> Option<String> {
        if func_sig.contains("()") {
            let func_name = func_sig.split('(').next().unwrap_or("").trim();

            if matches!(
                func_name,
                "+" | "-"
                    | "*"
                    | "/"
                    | "%"
                    | "=="
                    | "!="
                    | "<"
                    | ">"
                    | "<="
                    | ">="
                    | "&&"
                    | "||"
                    | "&"
                    | "|"
                    | "^"
            ) {
                return Some(format!(
                    "Field is missing. Use e.has(\"field\") or e.get_path(\"field\", default) before using '{}'",
                    func_name
                ));
            }

            if func_sig.contains(" (())")
                || func_sig.contains("((), ")
                || func_sig.contains(", ())")
            {
                return Some(
                    "Field is missing. Use e.has(\"field\") to check, or e.get_path(\"field\", default) to provide a default"
                        .to_string(),
                );
            }
        }

        let func_name = func_sig.split('(').next().unwrap_or(func_sig).trim();

        let mut best: Vec<(String, f64)> = RhaiEngine::function_catalog()
            .into_iter()
            .map(|candidate| {
                let sim =
                    self.calculate_similarity(&func_name.to_lowercase(), &candidate.to_lowercase());
                (candidate, sim)
            })
            .filter(|(_, sim)| *sim > 0.45)
            .collect();

        best.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        best.truncate(3);

        if !best.is_empty() {
            return Some(format!(
                "Did you mean: {}?",
                best.iter()
                    .map(|(candidate, _)| candidate.clone())
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
        }

        match func_name {
            "length" => Some("Use 'len()' instead of 'length()'".to_string()),
            "size" => Some("Use 'len()' instead of 'size()'".to_string()),
            "substr" | "substring" => Some(
                "Use string slicing: s[start..end] or extract_regex() for pattern matching"
                    .to_string(),
            ),
            "indexOf" | "index_of" => Some(
                "Use 'contains()' to check existence or 'split()' to find positions".to_string(),
            ),
            "push_back" | "append" => Some("Use 'push()' to add elements to arrays".to_string()),
            "to_int" | "parseInt" => {
                Some("Use 'parse()' or to_number() for type conversion".to_string())
            }
            "to_str" | "toString" => Some("Use 'to_string()' for string conversion".to_string()),
            "match" => Some(
                "Use 'extract_regex()' for regex matching or 'contains()' for simple checks"
                    .to_string(),
            ),
            name if name.ends_with("_re") => Some(
                "Regex functions: extract_regex(), extract_regexes(), extract_regex_maps(), split_regex(), replace_regex()"
                    .to_string(),
            ),
            _ => None,
        }
    }

    fn find_similar_variables(&self, target: &str, scope: &Scope) -> Vec<String> {
        let mut suggestions = Vec::new();
        let target_lower = target.to_lowercase();

        for (name, _is_const, _value) in scope.iter() {
            let name_lower = name.to_lowercase();
            let similarity = self.calculate_similarity(&target_lower, &name_lower);

            if similarity > 0.6
                || name_lower.contains(&target_lower)
                || target_lower.contains(&name_lower)
                || self.has_common_prefix(&target_lower, &name_lower)
            {
                suggestions.push(name.to_string());
            }
        }

        suggestions.sort_by(|a, b| {
            let sim_a = self.calculate_similarity(&target_lower, &a.to_lowercase());
            let sim_b = self.calculate_similarity(&target_lower, &b.to_lowercase());
            sim_b
                .partial_cmp(&sim_a)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        suggestions.truncate(3);
        suggestions
    }

    fn event_field_names(&self, scope: &Scope) -> Option<Vec<String>> {
        if let Some(e_map) = scope.get_value::<Map>("e") {
            let mut keys: Vec<String> = e_map
                .into_keys()
                .map(|k| k.to_string())
                .filter(|k| !k.is_empty())
                .collect();
            keys.sort();
            keys.dedup();
            return Some(keys);
        }
        None
    }

    /// If a bare identifier (used without the `e.` prefix) matches or resembles a
    /// field on the event, suggest the prefixed form `e.<field>`. Returns `None`
    /// when the identifier already carries a dot/prefix or no field is a good
    /// match, so the caller can fall back to scope-variable suggestions.
    fn suggest_event_field_prefix(&self, var_name: &str, scope: &Scope) -> Option<String> {
        if var_name.contains('.') {
            return None;
        }
        let fields = self.event_field_names(scope)?;

        // Exact field match: the missing `e.` prefix is the whole problem.
        if fields.iter().any(|f| f.as_str() == var_name) {
            return Some(format!(
                "Did you mean: e.{var_name}? Event fields are accessed through `e`, e.g. `e.{var_name}`."
            ));
        }

        // Otherwise offer the closest field names, already prefixed.
        let target_lower = var_name.to_lowercase();
        let similar: Vec<String> = fields
            .iter()
            .filter(|name| {
                let name_lower = name.to_lowercase();
                // `>=` (not `>`) so boundary-similarity transpositions like
                // `levle` -> `level` (distance 2 over 5 chars == 0.6) are caught.
                self.calculate_similarity(&target_lower, &name_lower) >= 0.6
                    || name_lower.contains(&target_lower)
                    || target_lower.contains(&name_lower)
            })
            .take(3)
            .map(|name| format!("e.{name}"))
            .collect();

        if similar.is_empty() {
            None
        } else {
            Some(format!("Did you mean: {}?", similar.join(", ")))
        }
    }

    fn calculate_similarity(&self, s1: &str, s2: &str) -> f64 {
        if s1 == s2 {
            return 1.0;
        }
        if s1.is_empty() || s2.is_empty() {
            return 0.0;
        }

        let max_len = s1.len().max(s2.len());
        let distance = self.levenshtein_distance(s1, s2);
        1.0 - (distance as f64 / max_len as f64)
    }

    fn levenshtein_distance(&self, s1: &str, s2: &str) -> usize {
        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();
        let len1 = chars1.len();
        let len2 = chars2.len();

        if len1 == 0 {
            return len2;
        }
        if len2 == 0 {
            return len1;
        }

        let mut prev_row: Vec<usize> = (0..=len2).collect();

        for i in 1..=len1 {
            let mut curr_row = vec![i];

            for j in 1..=len2 {
                let cost = if chars1[i - 1] == chars2[j - 1] { 0 } else { 1 };
                curr_row.push(
                    (curr_row[j - 1] + 1)
                        .min(prev_row[j] + 1)
                        .min(prev_row[j - 1] + cost),
                );
            }

            prev_row = curr_row;
        }

        prev_row[len2]
    }

    fn has_common_prefix(&self, s1: &str, s2: &str) -> bool {
        if s1.len() < 2 || s2.len() < 2 {
            return false;
        }
        let prefix_len = 2.min(s1.len()).min(s2.len());
        s1[..prefix_len] == s2[..prefix_len]
    }

    fn get_stage_help(&self, stage: &str, error: &EvalAltResult) -> String {
        let mut help = String::new();
        let bullet = if self.debug_config.use_emoji {
            "🔹 "
        } else {
            ""
        };

        match stage {
            "filter" => {
                help.push_str(&format!("\n   {bullet}Filter stage tips:\n"));
                help.push_str("   • Filters must return true/false (boolean values)\n");
                help.push_str("   • Use 'e.field_name' to access event fields\n");
                help.push_str(
                    "   • Use 'e[\"field-with-special-chars\"]' for complex field names\n",
                );
                help.push_str("   • Use 'if \"field\" in e { ... }' to check field existence\n");

                if let EvalAltResult::ErrorMismatchDataType(_, _, _) = error {
                    help.push_str(
                        "   • Remember: filters need boolean results, not strings or numbers\n",
                    );
                }
            }
            "exec" => {
                help.push_str(&format!("\n   {bullet}Exec stage tips:\n"));
                help.push_str("   • Use 'e.new_field = value' to add fields to events\n");
                help.push_str("   • Use 'e.field = ()' to remove fields from events\n");
                help.push_str("   • Use 'e = ()' to remove entire event (filter out)\n");
                help.push_str("   • Use 'let variable = value' for temporary variables\n");
                help.push_str("   • Use 'print(\"debug: \" + value)' for debugging output\n");
            }
            "begin" => {
                help.push_str(&format!("\n   {bullet}Begin stage tips:\n"));
                help.push_str("   • Use 'conf.field = value' to set global initialization data\n");
                help.push_str("   • Use 'read_file(\"path\")' to load external data\n");
                help.push_str("   • Variables set here are available in all event processing\n");
            }
            "end" => {
                help.push_str(&format!("\n   {bullet}End stage tips:\n"));
                help.push_str("   • Use 'metrics.key' to access accumulated tracking data\n");
                help.push_str("   • Use 'print()' to output final results\n");
                help.push_str("   • This runs after all events are processed\n");
            }
            _ => {}
        }

        help
    }
}