kelora 1.5.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
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
//! Worker thread for parallel processing
//!
//! Contains the worker thread that processes batches of lines or events.

use anyhow::Result;
use crossbeam_channel::{select, Receiver, Sender};
use rhai::Dynamic;
use std::collections::HashMap;
use std::time::{Duration, Instant};

use crate::event::Event;
use crate::parsers::type_conversion::TypeMap;
use crate::pipeline::{self, PipelineBuilder};
use crate::platform::Ctrl;
use crate::rhai_functions::tracking;
use crate::stats::{get_thread_stats, stats_finish_processing, stats_start_timer};

use super::types::{Batch, BatchResult, EventBatch, ProcessedEvent, WorkMessage};

/// Worker thread: processes batches in parallel
pub(crate) fn worker_thread(
    _worker_id: usize,
    work_receiver: Receiver<WorkMessage>,
    result_sender: Sender<BatchResult>,
    pipeline_builder: PipelineBuilder,
    stages: Vec<crate::config::ScriptStageType>,
    multiline_timeout: Option<Duration>,
    ctrl_rx: Receiver<Ctrl>,
) -> Result<()> {
    crate::rhai_functions::strings::set_parallel_mode(true);

    stats_start_timer();

    let (mut pipeline, mut ctx) = pipeline_builder.clone().build_worker(stages.clone())?;

    let mut current_csv_headers: Option<Vec<String>> = None;
    let mut current_csv_type_map: Option<TypeMap> = None;
    let mut immediate_shutdown = false;

    let ctrl_rx = ctrl_rx;
    let work_receiver = work_receiver;

    'worker_loop: loop {
        if immediate_shutdown {
            break;
        }

        let flush_deadline = multiline_timeout
            .filter(|_| pipeline.has_pending_chunk())
            .map(|timeout| Instant::now() + timeout);

        if let Some(deadline) = flush_deadline {
            let wait = deadline.saturating_duration_since(Instant::now());
            let timeout = crossbeam_channel::after(wait);
            select! {
                recv(ctrl_rx) -> msg => {
                    if handle_worker_ctrl(
                        msg,
                        &mut immediate_shutdown,
                        &mut pipeline,
                        &mut ctx,
                        &result_sender,
                    )? {
                        break 'worker_loop;
                    }
                }
                recv(work_receiver) -> msg => {
                    match msg {
                        Ok(work_msg) => {
                            let continue_processing = match work_msg {
                                WorkMessage::LineBatch(batch) => {
                                    worker_process_batch(
                                        batch,
                                        &mut pipeline,
                                        &mut ctx,
                                        &pipeline_builder,
                                        &stages,
                                        &result_sender,
                                        &mut current_csv_headers,
                                        &mut current_csv_type_map,
                                    )?
                                }
                                WorkMessage::EventBatch(event_batch) => {
                                    worker_process_event_batch(
                                        event_batch,
                                        &mut pipeline,
                                        &mut ctx,
                                        &pipeline_builder,
                                        &stages,
                                        &result_sender,
                                        &mut current_csv_headers,
                                        &mut current_csv_type_map,
                                    )?
                                }
                            };
                            if !continue_processing {
                                break 'worker_loop;
                            }
                        }
                        Err(_) => break 'worker_loop,
                    }
                }
                recv(timeout) -> _ => {
                    worker_flush_pipeline(
                        &mut pipeline,
                        &mut ctx,
                        &result_sender,
                        false,
                    )?;
                }
            }
        } else {
            select! {
                recv(ctrl_rx) -> msg => {
                    if handle_worker_ctrl(
                        msg,
                        &mut immediate_shutdown,
                        &mut pipeline,
                        &mut ctx,
                        &result_sender,
                    )? {
                        break 'worker_loop;
                    }
                }
                recv(work_receiver) -> msg => {
                    match msg {
                        Ok(work_msg) => {
                            let continue_processing = match work_msg {
                                WorkMessage::LineBatch(batch) => {
                                    worker_process_batch(
                                        batch,
                                        &mut pipeline,
                                        &mut ctx,
                                        &pipeline_builder,
                                        &stages,
                                        &result_sender,
                                        &mut current_csv_headers,
                                        &mut current_csv_type_map,
                                    )?
                                }
                                WorkMessage::EventBatch(event_batch) => {
                                    worker_process_event_batch(
                                        event_batch,
                                        &mut pipeline,
                                        &mut ctx,
                                        &pipeline_builder,
                                        &stages,
                                        &result_sender,
                                        &mut current_csv_headers,
                                        &mut current_csv_type_map,
                                    )?
                                }
                            };
                            if !continue_processing {
                                break 'worker_loop;
                            }
                        }
                        Err(_) => break 'worker_loop,
                    }
                }
            }
        }
    }

    if !immediate_shutdown {
        worker_flush_pipeline(&mut pipeline, &mut ctx, &result_sender, true)?;
    }

    stats_finish_processing();

    Ok(())
}

/// Handle control messages for worker thread
fn handle_worker_ctrl(
    msg: Result<Ctrl, crossbeam_channel::RecvError>,
    immediate_shutdown: &mut bool,
    pipeline: &mut pipeline::Pipeline,
    ctx: &mut pipeline::PipelineContext,
    result_sender: &Sender<BatchResult>,
) -> Result<bool> {
    match msg {
        Ok(Ctrl::Shutdown { immediate }) => {
            if immediate {
                *immediate_shutdown = true;
                return Ok(true);
            }

            worker_flush_pipeline(pipeline, ctx, result_sender, false)?;
            Ok(false)
        }
        Ok(Ctrl::PrintStats) => {
            // Worker threads don't print stats directly - ignore
            Ok(false)
        }
        Err(_) => {
            // Treat channel closure as graceful shutdown request
            worker_flush_pipeline(pipeline, ctx, result_sender, false)?;
            Ok(true)
        }
    }
}

/// Flush the worker pipeline and send any pending results
fn worker_flush_pipeline(
    pipeline: &mut pipeline::Pipeline,
    ctx: &mut pipeline::PipelineContext,
    result_sender: &Sender<BatchResult>,
    final_flush: bool,
) -> Result<()> {
    match pipeline.flush(ctx) {
        Ok(mut flush_results) => {
            if final_flush {
                if let Some(trailing) = pipeline.finish_formatter() {
                    if !trailing.line.is_empty() {
                        flush_results.push(trailing);
                    }
                }
            }

            if flush_results.is_empty() {
                return Ok(());
            }

            let mut flush_batch_results = Vec::with_capacity(flush_results.len());
            for formatted_result in flush_results {
                let pipeline::FormattedOutput {
                    line,
                    timestamp,
                    file_ops,
                } = formatted_result;
                let mut dummy_event = Event::default_with_line(line);
                dummy_event.set_metadata(0, None);

                flush_batch_results.push(ProcessedEvent {
                    event: dummy_event,
                    captured_prints: Vec::new(),
                    captured_eprints: Vec::new(),
                    captured_messages: Vec::new(),
                    timestamp,
                    file_ops,
                });
            }

            let mut flush_user_updates = HashMap::new();
            let mut flush_internal_updates = HashMap::new();

            for (key, value) in &ctx.internal_tracker {
                flush_internal_updates.insert(key.clone(), value.clone());
            }

            for (key, value) in &ctx.tracker {
                flush_user_updates.insert(key.clone(), value.clone());
            }

            for (key, value) in ctx
                .internal_tracker
                .iter()
                .filter(|(k, _)| k.starts_with("__op_"))
            {
                flush_user_updates.insert(key.clone(), value.clone());
            }

            let thread_user = tracking::get_thread_tracking_state();
            for (key, value) in thread_user {
                flush_user_updates.insert(key, value);
            }

            let thread_internal = tracking::get_thread_internal_state();
            for (key, value) in thread_internal
                .iter()
                .filter(|(k, _)| k.starts_with("__op_"))
            {
                flush_user_updates.insert(key.clone(), value.clone());
            }
            for (key, value) in thread_internal {
                flush_internal_updates.insert(key, value);
            }

            let flush_batch_result = BatchResult {
                batch_id: u64::MAX - 1,
                results: flush_batch_results,
                user_tracked_updates: flush_user_updates,
                internal_tracked_updates: flush_internal_updates,
                worker_stats: crate::stats::ProcessingStats::new(),
            };

            let _ = result_sender.send(flush_batch_result);
            Ok(())
        }
        Err(e) => {
            if ctx.config.strict {
                return Err(e);
            }
            eprintln!("Warning: Failed to flush worker pipeline: {}", e);
            Ok(())
        }
    }
}

/// Process a batch of lines
#[allow(clippy::too_many_arguments)]
fn worker_process_batch(
    batch: Batch,
    pipeline: &mut pipeline::Pipeline,
    ctx: &mut pipeline::PipelineContext,
    pipeline_builder: &PipelineBuilder,
    stages: &[crate::config::ScriptStageType],
    result_sender: &Sender<BatchResult>,
    current_csv_headers: &mut Option<Vec<String>>,
    current_csv_type_map: &mut Option<TypeMap>,
) -> Result<bool> {
    if (batch.csv_headers.is_some() && batch.csv_headers != *current_csv_headers)
        || (batch.csv_type_map.is_some() && batch.csv_type_map != *current_csv_type_map)
    {
        if batch.csv_headers.is_some() {
            *current_csv_headers = batch.csv_headers.clone();
        }
        if batch.csv_type_map.is_some() {
            *current_csv_type_map = batch.csv_type_map.clone();
        }

        let mut new_pipeline_builder = pipeline_builder.clone();
        if let Some(ref headers) = current_csv_headers {
            new_pipeline_builder = new_pipeline_builder.with_csv_headers(headers.clone());
        }
        if let Some(ref type_map) = current_csv_type_map {
            new_pipeline_builder = new_pipeline_builder.with_csv_type_map(type_map.clone());
        }
        let (new_pipeline, new_ctx) = new_pipeline_builder.build_worker(stages.to_vec())?;
        *pipeline = new_pipeline;
        ctx.rhai = new_ctx.rhai;
    }

    let before_internal = ctx.internal_tracker.clone();

    let mut batch_results = Vec::with_capacity(batch.lines.len());

    for (line_idx, line) in batch.lines.iter().enumerate() {
        let current_line_num = batch.start_line_num + line_idx;
        ctx.meta.line_num = Some(current_line_num);
        ctx.meta.filename = batch.filenames.get(line_idx).cloned().flatten();

        crate::rhai_functions::strings::clear_captured_prints();
        crate::rhai_functions::strings::clear_captured_eprints();

        match pipeline.process_line(line.clone(), ctx) {
            Ok(formatted_results) => {
                if !formatted_results.is_empty() {
                    ctx.internal_tracker
                        .entry("__kelora_stats_output".to_string())
                        .and_modify(|v| *v = Dynamic::from(v.as_int().unwrap_or(0) + 1))
                        .or_insert(Dynamic::from(1i64));
                    ctx.internal_tracker.insert(
                        "__op___kelora_stats_output".to_string(),
                        Dynamic::from("count"),
                    );
                }

                let captured_prints = crate::rhai_functions::strings::take_captured_prints();
                let captured_eprints = crate::rhai_functions::strings::take_captured_eprints();
                let captured_messages = crate::rhai_functions::strings::take_captured_messages();

                if formatted_results.is_empty()
                    && (!captured_prints.is_empty()
                        || !captured_eprints.is_empty()
                        || !captured_messages.is_empty())
                {
                    let dummy_event = Event::default_with_line(String::new());
                    batch_results.push(ProcessedEvent {
                        event: dummy_event,
                        captured_prints,
                        captured_eprints,
                        captured_messages,
                        timestamp: None,
                        file_ops: Vec::new(),
                    });
                } else {
                    for formatted_result in formatted_results {
                        let pipeline::FormattedOutput {
                            line,
                            timestamp,
                            file_ops,
                        } = formatted_result;
                        let mut dummy_event = Event::default_with_line(line);
                        dummy_event.set_metadata(current_line_num, None);

                        batch_results.push(ProcessedEvent {
                            event: dummy_event,
                            captured_prints: captured_prints.clone(),
                            captured_eprints: captured_eprints.clone(),
                            captured_messages: captured_messages.clone(),
                            timestamp,
                            file_ops,
                        });
                    }
                }
            }
            Err(e) => {
                let captured_eprints = crate::rhai_functions::strings::take_captured_eprints();
                let captured_messages = crate::rhai_functions::strings::take_captured_messages();

                if !captured_eprints.is_empty() || !captured_messages.is_empty() {
                    let dummy_event = Event::default_with_line(String::new());
                    batch_results.push(ProcessedEvent {
                        event: dummy_event,
                        captured_prints: Vec::new(),
                        captured_eprints,
                        captured_messages,
                        timestamp: None,
                        file_ops: Vec::new(),
                    });
                }

                if ctx.config.strict {
                    return Err(e);
                } else {
                    continue;
                }
            }
        }

        if crate::rhai_functions::process::is_exit_requested() {
            let exit_code = crate::rhai_functions::process::get_exit_code();
            std::process::exit(exit_code);
        }
    }

    let mut internal_deltas = std::collections::HashMap::new();
    for (key, value) in &ctx.internal_tracker {
        if key.starts_with("__op_") {
            // Operation metadata is added alongside the associated value when needed
            continue;
        }

        let op_key = format!("__op_{}", key);
        let operation = ctx
            .internal_tracker
            .get(&op_key)
            .and_then(|v| v.clone().into_string().ok())
            .unwrap_or_else(|| "replace".to_string());

        match operation.as_str() {
            "count" | "sum" => {
                let before_value = before_internal.get(key);
                let diff_dynamic =
                    if value.is_float() || before_value.map(|v| v.is_float()).unwrap_or(false) {
                        let current = if value.is_float() {
                            value.as_float().unwrap_or(0.0)
                        } else {
                            value.as_int().unwrap_or(0) as f64
                        };
                        let previous = before_value
                            .and_then(|v| {
                                if v.is_float() {
                                    v.as_float().ok()
                                } else {
                                    v.as_int().ok().map(|i| i as f64)
                                }
                            })
                            .unwrap_or(0.0);
                        Dynamic::from(current - previous)
                    } else {
                        let current = value.as_int().unwrap_or(0);
                        let previous = before_value.and_then(|v| v.as_int().ok()).unwrap_or(0);
                        Dynamic::from(current - previous)
                    };

                let is_zero = if diff_dynamic.is_float() {
                    diff_dynamic.as_float().unwrap_or(0.0).abs() < f64::EPSILON
                } else {
                    diff_dynamic.as_int().unwrap_or(0) == 0
                };

                if !is_zero {
                    internal_deltas.insert(key.clone(), diff_dynamic);
                    internal_deltas.insert(op_key.clone(), Dynamic::from(operation));
                }
            }
            _ => {
                internal_deltas.insert(key.clone(), value.clone());
                if let Some(op_value) = ctx.internal_tracker.get(&op_key) {
                    internal_deltas.insert(op_key.clone(), op_value.clone());
                }
            }
        }
    }

    let mut user_deltas = std::collections::HashMap::new();
    let thread_user = tracking::get_thread_tracking_state();
    for (key, value) in thread_user {
        user_deltas.insert(key, value);
    }

    let thread_internal_meta = tracking::get_thread_internal_state();
    for (key, value) in thread_internal_meta
        .iter()
        .filter(|(k, _)| k.starts_with("__op_"))
    {
        user_deltas.insert(key.clone(), value.clone());
    }

    let batch_result = BatchResult {
        batch_id: batch.id,
        results: batch_results,
        user_tracked_updates: user_deltas,
        internal_tracked_updates: internal_deltas,
        worker_stats: get_thread_stats(),
    };

    if result_sender.send(batch_result).is_err() {
        return Ok(false);
    }

    ctx.tracker.clear();

    Ok(true)
}

/// Process a batch of pre-chunked events (for multiline processing)
#[allow(clippy::too_many_arguments)]
fn worker_process_event_batch(
    event_batch: EventBatch,
    pipeline: &mut pipeline::Pipeline,
    ctx: &mut pipeline::PipelineContext,
    pipeline_builder: &PipelineBuilder,
    stages: &[crate::config::ScriptStageType],
    result_sender: &Sender<BatchResult>,
    current_csv_headers: &mut Option<Vec<String>>,
    current_csv_type_map: &mut Option<TypeMap>,
) -> Result<bool> {
    if (event_batch.csv_headers.is_some() && event_batch.csv_headers != *current_csv_headers)
        || (event_batch.csv_type_map.is_some() && event_batch.csv_type_map != *current_csv_type_map)
    {
        if event_batch.csv_headers.is_some() {
            *current_csv_headers = event_batch.csv_headers.clone();
        }
        if event_batch.csv_type_map.is_some() {
            *current_csv_type_map = event_batch.csv_type_map.clone();
        }

        let mut new_pipeline_builder = pipeline_builder.clone();
        if let Some(ref headers) = current_csv_headers {
            new_pipeline_builder = new_pipeline_builder.with_csv_headers(headers.clone());
        }
        if let Some(ref type_map) = current_csv_type_map {
            new_pipeline_builder = new_pipeline_builder.with_csv_type_map(type_map.clone());
        }
        let (new_pipeline, new_ctx) = new_pipeline_builder.build_worker(stages.to_vec())?;
        *pipeline = new_pipeline;
        ctx.rhai = new_ctx.rhai;
    }

    let before_internal = ctx.internal_tracker.clone();

    let mut batch_results = Vec::with_capacity(event_batch.events.len());

    for (event_idx, event_string) in event_batch.events.iter().enumerate() {
        let current_line_num = event_batch.start_line_num + event_idx;
        ctx.meta.line_num = Some(current_line_num);
        ctx.meta.filename = event_batch.filenames.get(event_idx).cloned().flatten();

        crate::rhai_functions::strings::clear_captured_prints();
        crate::rhai_functions::strings::clear_captured_eprints();

        // For event batches, skip chunking and go directly to parsing
        match pipeline.process_event_string(event_string.clone(), ctx) {
            Ok(formatted_results) => {
                if !formatted_results.is_empty() {
                    ctx.internal_tracker
                        .entry("__kelora_stats_output".to_string())
                        .and_modify(|v| *v = Dynamic::from(v.as_int().unwrap_or(0) + 1))
                        .or_insert(Dynamic::from(1i64));
                    ctx.internal_tracker.insert(
                        "__op___kelora_stats_output".to_string(),
                        Dynamic::from("count"),
                    );
                }

                let captured_prints = crate::rhai_functions::strings::take_captured_prints();
                let captured_eprints = crate::rhai_functions::strings::take_captured_eprints();
                let captured_messages = crate::rhai_functions::strings::take_captured_messages();

                if formatted_results.is_empty()
                    && (!captured_prints.is_empty()
                        || !captured_eprints.is_empty()
                        || !captured_messages.is_empty())
                {
                    let dummy_event = Event::default_with_line(String::new());
                    batch_results.push(ProcessedEvent {
                        event: dummy_event,
                        captured_prints,
                        captured_eprints,
                        captured_messages,
                        timestamp: None,
                        file_ops: Vec::new(),
                    });
                } else {
                    for formatted_result in formatted_results {
                        let pipeline::FormattedOutput {
                            line,
                            timestamp,
                            file_ops,
                        } = formatted_result;
                        let mut dummy_event = Event::default_with_line(line);
                        dummy_event.set_metadata(current_line_num, None);

                        batch_results.push(ProcessedEvent {
                            event: dummy_event,
                            captured_prints: captured_prints.clone(),
                            captured_eprints: captured_eprints.clone(),
                            captured_messages: captured_messages.clone(),
                            timestamp,
                            file_ops,
                        });
                    }
                }
            }
            Err(e) => {
                let captured_eprints = crate::rhai_functions::strings::take_captured_eprints();
                let captured_messages = crate::rhai_functions::strings::take_captured_messages();

                if !captured_eprints.is_empty() || !captured_messages.is_empty() {
                    let dummy_event = Event::default_with_line(String::new());
                    batch_results.push(ProcessedEvent {
                        event: dummy_event,
                        captured_prints: Vec::new(),
                        captured_eprints,
                        captured_messages,
                        timestamp: None,
                        file_ops: Vec::new(),
                    });
                }

                if ctx.config.strict {
                    return Err(e);
                } else {
                    continue;
                }
            }
        }

        if crate::rhai_functions::process::is_exit_requested() {
            let exit_code = crate::rhai_functions::process::get_exit_code();
            std::process::exit(exit_code);
        }
    }

    let mut internal_deltas = std::collections::HashMap::new();
    for (key, value) in &ctx.internal_tracker {
        if key.starts_with("__op_") {
            continue;
        }

        let op_key = format!("__op_{}", key);
        let operation = ctx
            .internal_tracker
            .get(&op_key)
            .and_then(|v| v.clone().into_string().ok())
            .unwrap_or_else(|| "replace".to_string());

        match operation.as_str() {
            "count" | "sum" => {
                let before_value = before_internal.get(key);
                let diff_dynamic =
                    if value.is_float() || before_value.map(|v| v.is_float()).unwrap_or(false) {
                        let current = if value.is_float() {
                            value.as_float().unwrap_or(0.0)
                        } else {
                            value.as_int().unwrap_or(0) as f64
                        };
                        let previous = before_value
                            .and_then(|v| {
                                if v.is_float() {
                                    v.as_float().ok()
                                } else {
                                    v.as_int().ok().map(|i| i as f64)
                                }
                            })
                            .unwrap_or(0.0);
                        Dynamic::from(current - previous)
                    } else {
                        let current = value.as_int().unwrap_or(0);
                        let previous = before_value.and_then(|v| v.as_int().ok()).unwrap_or(0);
                        Dynamic::from(current - previous)
                    };

                let is_zero = if diff_dynamic.is_float() {
                    diff_dynamic.as_float().unwrap_or(0.0).abs() < f64::EPSILON
                } else {
                    diff_dynamic.as_int().unwrap_or(0) == 0
                };

                if !is_zero {
                    internal_deltas.insert(key.clone(), diff_dynamic);
                    internal_deltas.insert(op_key.clone(), Dynamic::from(operation));
                }
            }
            _ => {
                internal_deltas.insert(key.clone(), value.clone());
                if let Some(op_value) = ctx.internal_tracker.get(&op_key) {
                    internal_deltas.insert(op_key.clone(), op_value.clone());
                }
            }
        }
    }

    let mut user_deltas = std::collections::HashMap::new();
    let thread_user = tracking::get_thread_tracking_state();
    for (key, value) in thread_user {
        user_deltas.insert(key, value);
    }

    let batch_result = BatchResult {
        batch_id: event_batch.id,
        results: batch_results,
        user_tracked_updates: user_deltas,
        internal_tracked_updates: internal_deltas,
        worker_stats: get_thread_stats(),
    };

    if result_sender.send(batch_result).is_err() {
        return Ok(false);
    }

    ctx.tracker.clear();

    Ok(true)
}

/// Chunker thread: converts line batches to event batches for multiline processing
pub(crate) fn chunker_thread(
    line_batch_receiver: Receiver<super::types::Batch>,
    event_batch_sender: Sender<WorkMessage>,
    multiline_config: crate::config::MultilineConfig,
    input_format: crate::config::InputFormat,
    ctrl_rx: Receiver<Ctrl>,
) -> Result<()> {
    // Create a chunker for multiline processing
    let mut chunker =
        crate::pipeline::multiline::create_multiline_chunker(&multiline_config, input_format)
            .map_err(|e| anyhow::anyhow!("Failed to create chunker: {}", e))?;

    // Track metadata required to emit accurate event batches across line boundaries
    let mut pending_event_filename: Option<Option<String>> = None;
    let mut next_event_batch_id = 0u64;
    let mut last_start_line_num: usize = 0;
    let mut last_csv_headers: Option<Vec<String>> = None;
    let mut last_csv_type_map: Option<TypeMap> = None;

    while let Ok(batch) = line_batch_receiver.recv() {
        // Check for shutdown
        if let Ok(Ctrl::Shutdown { .. }) = ctrl_rx.try_recv() {
            break;
        }

        last_start_line_num = batch.start_line_num;
        if batch.csv_headers.is_some() {
            last_csv_headers = batch.csv_headers.clone();
        }
        if batch.csv_type_map.is_some() {
            last_csv_type_map = batch.csv_type_map.clone();
        }

        let mut events = Vec::new();
        let mut event_filenames = Vec::new();

        // Process each line through chunker
        for (line_idx, line) in batch.lines.iter().enumerate() {
            let line_filename = batch.filenames.get(line_idx).cloned().flatten();

            if pending_event_filename.is_none() || !chunker.has_pending() {
                pending_event_filename = Some(line_filename.clone());
            }

            // Feed line to chunker and collect complete events
            if let Some(chunk) = chunker.feed_line(line.clone()) {
                let event_filename = pending_event_filename
                    .take()
                    .unwrap_or_else(|| line_filename.clone());

                events.push(chunk);
                event_filenames.push(event_filename);

                // Current line becomes the first line of the next buffered event
                pending_event_filename = Some(line_filename.clone());
            }
        }

        // Send event batch to workers if we have events
        if !events.is_empty() {
            let event_batch = EventBatch {
                id: next_event_batch_id,
                events,
                start_line_num: batch.start_line_num,
                filenames: event_filenames,
                csv_headers: batch.csv_headers,
                csv_type_map: batch.csv_type_map,
            };

            next_event_batch_id = next_event_batch_id.wrapping_add(1);

            if event_batch_sender
                .send(WorkMessage::EventBatch(event_batch))
                .is_err()
            {
                break;
            }
        }
    }

    // Flush any remaining buffered event after input closes or shutdown
    if chunker.has_pending() {
        if let Some(chunk) = chunker.flush() {
            let flushed_filename = pending_event_filename.take().unwrap_or(None);

            let event_batch = EventBatch {
                id: next_event_batch_id,
                events: vec![chunk],
                start_line_num: last_start_line_num,
                filenames: vec![flushed_filename],
                csv_headers: last_csv_headers,
                csv_type_map: last_csv_type_map,
            };

            let _ = event_batch_sender.send(WorkMessage::EventBatch(event_batch));
        }
    }

    Ok(())
}