lessence 0.4.1

Extract the essence of your logs — compress repetitive lines while preserving all unique information
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
// Central Pattern Registry with Deduplication
// Constitutional requirement: 30+ patterns, thread-safe access

use super::{FormatFamily, PatternPriority, PatternSource, TimestampFormat, TimestampPattern};
use regex::Regex;
use std::collections::HashMap;

/// Central registry for all timestamp patterns with deduplication
pub struct TimestampRegistry {
    patterns: Vec<TimestampPattern>,
}

impl Default for TimestampRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl TimestampRegistry {
    /// Initialize registry with patterns from both source implementations
    /// Constitutional requirement: Merge all patterns without loss
    pub fn new() -> Self {
        // Load patterns from both sources
        let timestamp_patterns = Self::load_original_timestamp_patterns();
        let essence_patterns = Self::load_original_essence_patterns();

        // Merge and deduplicate
        let mut merged_patterns =
            Self::merge_duplicate_patterns(timestamp_patterns, essence_patterns);

        // Assign priorities
        Self::assign_pattern_priorities(&mut merged_patterns);

        // Sort by priority (highest first)
        merged_patterns.sort_by(|a, b| {
            a.priority
                .effective_score()
                .cmp(&b.priority.effective_score())
        });

        TimestampRegistry {
            patterns: merged_patterns,
        }
    }

    /// Load patterns from original timestamp.rs implementation
    fn load_original_timestamp_patterns() -> Vec<TimestampPattern> {
        vec![
            // ISO 8601 Enhanced
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:?\d{2}?)\b").unwrap(),
                format_type: TimestampFormat::ISO8601Enhanced,
                priority: PatternPriority::new(100, FormatFamily::Structured),
                source: PatternSource::OriginalTimestamp,
            },
            // Week date format
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-W\d{2}-\d(?:T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:?\d{2}?)?)?\b").unwrap(),
                format_type: TimestampFormat::WeekDate,
                priority: PatternPriority::new(90, FormatFamily::Structured),
                source: PatternSource::OriginalTimestamp,
            },
            // Ordinal date format
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-\d{3}(?:T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:?\d{2}?)?)?\b").unwrap(),
                format_type: TimestampFormat::OrdinalDate,
                priority: PatternPriority::new(90, FormatFamily::Structured),
                source: PatternSource::OriginalTimestamp,
            },
            // Standard datetime
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:[.,]\d{1,9})?(?:\s*(?:UTC|GMT|[+-]\d{2}:?\d{2}?))?\b").unwrap(),
                format_type: TimestampFormat::ISO8601Full,
                priority: PatternPriority::new(85, FormatFamily::Structured),
                source: PatternSource::OriginalTimestamp,
            },
            // Java timestamp with comma
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{1,9}\b").unwrap(),
                format_type: TimestampFormat::JavaSimpleDate,
                priority: PatternPriority::new(75, FormatFamily::Application),
                source: PatternSource::OriginalTimestamp,
            },
            // 12-hour format with AM/PM
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2}(?:\.\d{1,9})?\s*(?:AM|PM|am|pm)\b").unwrap(),
                format_type: TimestampFormat::USDate,
                priority: PatternPriority::new(70, FormatFamily::Regional),
                source: PatternSource::OriginalTimestamp,
            },
            // MySQL YYMMDD
            TimestampPattern {
                regex: Regex::new(r"\b\d{6}\s+\d{2}:\d{2}:\d{2}\b").unwrap(),
                format_type: TimestampFormat::MySQLTimestamp,
                priority: PatternPriority::new(60, FormatFamily::Database),
                source: PatternSource::OriginalTimestamp,
            },
            // Oracle format
            TimestampPattern {
                regex: Regex::new(r"\b\d{2}-[A-Z]{3}-\d{2}\s+\d{2}\.\d{2}\.\d{2}(?:\.\d+)?\s*(?:AM|PM)?").unwrap(),
                format_type: TimestampFormat::Oracle,
                priority: PatternPriority::new(65, FormatFamily::Database),
                source: PatternSource::OriginalTimestamp,
            },
            // Compact timestamp
            TimestampPattern {
                regex: Regex::new(r"\b20\d{12}\b").unwrap(),
                format_type: TimestampFormat::CompactFormat,
                priority: PatternPriority::new(50, FormatFamily::Legacy),
                source: PatternSource::OriginalTimestamp,
            },
            // Apache/Nginx common log
            TimestampPattern {
                regex: Regex::new(r"\[\d{2}/[A-Z][a-z]{2}/\d{4}:\d{2}:\d{2}:\d{2}\s+[+-]\d{4}\]").unwrap(),
                format_type: TimestampFormat::ApacheCommon,
                priority: PatternPriority::new(80, FormatFamily::Application),
                source: PatternSource::OriginalTimestamp,
            },
            // Alternative web format
            TimestampPattern {
                regex: Regex::new(r"\b\d{2}/[A-Z][a-z]{2}/\d{4}:\d{2}:\d{2}:\d{2}\b").unwrap(),
                format_type: TimestampFormat::NginxAccess,
                priority: PatternPriority::new(75, FormatFamily::Application),
                source: PatternSource::OriginalTimestamp,
            },
            // US format MM/DD/YYYY
            TimestampPattern {
                regex: Regex::new(r"\b\d{1,2}/\d{1,2}/\d{4}\s+\d{1,2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:\s*(?:AM|PM))?\b").unwrap(),
                format_type: TimestampFormat::USDate,
                priority: PatternPriority::new(60, FormatFamily::Regional),
                source: PatternSource::OriginalTimestamp,
            },
            // US format MM-DD-YYYY
            TimestampPattern {
                regex: Regex::new(r"\b\d{1,2}-\d{1,2}-\d{4}\s+\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?\b").unwrap(),
                format_type: TimestampFormat::USDateDash,
                priority: PatternPriority::new(60, FormatFamily::Regional),
                source: PatternSource::OriginalTimestamp,
            },
            // European DD/MM/YYYY
            TimestampPattern {
                regex: Regex::new(r"\b[0-3]\d/[01]\d/\d{4}\s+\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?\b").unwrap(),
                format_type: TimestampFormat::EuropeanDate,
                priority: PatternPriority::new(60, FormatFamily::Regional),
                source: PatternSource::OriginalTimestamp,
            },
            // European DD.MM.YYYY
            TimestampPattern {
                regex: Regex::new(r"\b[0-3]\d\.[01]\d\.\d{4}\s+\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?\b").unwrap(),
                format_type: TimestampFormat::EuropeanDateDot,
                priority: PatternPriority::new(60, FormatFamily::Regional),
                source: PatternSource::OriginalTimestamp,
            },
            // Standard syslog
            TimestampPattern {
                regex: Regex::new(r"\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?\b").unwrap(),
                format_type: TimestampFormat::SyslogBSD,
                priority: PatternPriority::new(55, FormatFamily::Legacy),
                source: PatternSource::OriginalTimestamp,
            },
            // Syslog with year
            TimestampPattern {
                regex: Regex::new(r"\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{1,2}\s+\d{4}\s+\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?\b").unwrap(),
                format_type: TimestampFormat::SyslogWithYear,
                priority: PatternPriority::new(60, FormatFamily::Legacy),
                source: PatternSource::OriginalTimestamp,
            },
            // Kubernetes/Go log format
            TimestampPattern {
                regex: Regex::new(r"[IWEF]\d{4}\s+\d{2}:\d{2}:\d{2}\.\d+").unwrap(),
                format_type: TimestampFormat::KubernetesLog,
                priority: PatternPriority::new(85, FormatFamily::Application),
                source: PatternSource::OriginalTimestamp,
            },
            // Unix timestamp (lowest priority)
            TimestampPattern {
                regex: Regex::new(r"\b1[0-9]{9,10}(?:\.\d{1,9})?\b").unwrap(),
                format_type: TimestampFormat::UnixTimestamp,
                priority: PatternPriority::new(10, FormatFamily::Unix),
                source: PatternSource::OriginalTimestamp,
            },
            // Unix with @ prefix
            TimestampPattern {
                regex: Regex::new(r"@1[0-9]{9,10}(?:\.\d{1,9})?\b").unwrap(),
                format_type: TimestampFormat::UnixPrefixed,
                priority: PatternPriority::new(20, FormatFamily::Unix),
                source: PatternSource::OriginalTimestamp,
            },
            // Unix in brackets
            TimestampPattern {
                regex: Regex::new(r"\[1[0-9]{9,10}(?:\.\d{1,9})?\]").unwrap(),
                format_type: TimestampFormat::UnixBracketed,
                priority: PatternPriority::new(25, FormatFamily::Unix),
                source: PatternSource::OriginalTimestamp,
            },
            // Docker/Container logs
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{9}Z\b").unwrap(),
                format_type: TimestampFormat::DockerLog,
                priority: PatternPriority::new(95, FormatFamily::Application),
                source: PatternSource::OriginalTimestamp,
            },
            // Time only with timezone
            TimestampPattern {
                regex: Regex::new(r"\b\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:?\d{2}?)?\b").unwrap(),
                format_type: TimestampFormat::TimeOnly,
                priority: PatternPriority::new(30, FormatFamily::Legacy),
                source: PatternSource::OriginalTimestamp,
            },
            // ISO 8601 durations
            TimestampPattern {
                regex: Regex::new(r"\bP(?:\d+Y)?(?:\d+M)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?\b").unwrap(),
                format_type: TimestampFormat::Duration,
                priority: PatternPriority::new(35, FormatFamily::Legacy),
                source: PatternSource::OriginalTimestamp,
            },
            // IBM YY.DDD format
            TimestampPattern {
                regex: Regex::new(r"\b\d{2}\.\d{3}\s+\d{2}:\d{2}:\d{2}\b").unwrap(),
                format_type: TimestampFormat::IBMFormat,
                priority: PatternPriority::new(45, FormatFamily::Legacy),
                source: PatternSource::OriginalTimestamp,
            },
            // RFC 2822 format
            TimestampPattern {
                regex: Regex::new(r"\b(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\s+\d{1,2}\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{4}\s+\d{2}:\d{2}:\d{2}\s+[+-]\d{4}\b").unwrap(),
                format_type: TimestampFormat::RFC2822,
                priority: PatternPriority::new(85, FormatFamily::Structured),
                source: PatternSource::OriginalTimestamp,
            },
            // Log4j format
            TimestampPattern {
                regex: Regex::new(r"\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}\s+\[").unwrap(),
                format_type: TimestampFormat::Log4j,
                priority: PatternPriority::new(70, FormatFamily::Application),
                source: PatternSource::OriginalTimestamp,
            },
            // Splunk format
            TimestampPattern {
                regex: Regex::new(r"\b\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}\.\d{1,6}\b").unwrap(),
                format_type: TimestampFormat::Splunk,
                priority: PatternPriority::new(65, FormatFamily::Application),
                source: PatternSource::OriginalTimestamp,
            },
        ]
    }

    /// Load patterns from original essence/processor.rs implementation
    fn load_original_essence_patterns() -> Vec<TimestampPattern> {
        vec![
            // Additional patterns from essence mode that aren't in timestamp.rs
            TimestampPattern {
                regex: Regex::new(r"\d{1,2}/\d{1,2}/\d{4}\s+\d{1,2}:\d{2}:\d{2}\s+(?:AM|PM)")
                    .unwrap(),
                format_type: TimestampFormat::WindowsEvent,
                priority: PatternPriority::new(55, FormatFamily::Regional),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}").unwrap(),
                format_type: TimestampFormat::WindowsIIS,
                priority: PatternPriority::new(50, FormatFamily::Regional),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+\d{4}").unwrap(),
                format_type: TimestampFormat::GitCommit,
                priority: PatternPriority::new(55, FormatFamily::Legacy),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z").unwrap(),
                format_type: TimestampFormat::Aws,
                priority: PatternPriority::new(85, FormatFamily::Application),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z").unwrap(),
                format_type: TimestampFormat::Gcp,
                priority: PatternPriority::new(85, FormatFamily::Application),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{7}Z").unwrap(),
                format_type: TimestampFormat::Azure,
                priority: PatternPriority::new(85, FormatFamily::Application),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\w{3}\s+\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+\d{4}").unwrap(),
                format_type: TimestampFormat::Ansic,
                priority: PatternPriority::new(50, FormatFamily::Legacy),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\w{3},\s+\d{2}\s+\w{3}\s+\d{4}\s+\d{2}:\d{2}:\d{2}\s+GMT")
                    .unwrap(),
                format_type: TimestampFormat::RFC822,
                priority: PatternPriority::new(80, FormatFamily::Structured),
                source: PatternSource::OriginalEssence,
            },
            // Unix timestamp variants from essence
            TimestampPattern {
                regex: Regex::new(r"\b\d{13}\b").unwrap(),
                format_type: TimestampFormat::UnixTimestampMs,
                priority: PatternPriority::new(15, FormatFamily::Unix),
                source: PatternSource::OriginalEssence,
            },
            TimestampPattern {
                regex: Regex::new(r"\b\d{19}\b").unwrap(),
                format_type: TimestampFormat::UnixTimestampNs,
                priority: PatternPriority::new(18, FormatFamily::Unix),
                source: PatternSource::OriginalEssence,
            },
        ]
    }

    /// Merge duplicate patterns into comprehensive versions
    fn merge_duplicate_patterns(
        timestamp_patterns: Vec<TimestampPattern>,
        essence_patterns: Vec<TimestampPattern>,
    ) -> Vec<TimestampPattern> {
        let mut merged_patterns = Vec::new();

        // Start with timestamp patterns as base
        let mut pattern_map: HashMap<String, TimestampPattern> = HashMap::new();

        // Add all timestamp patterns first
        for pattern in timestamp_patterns {
            let key = pattern.regex.as_str().to_string();
            pattern_map.insert(key, pattern);
        }

        // Check essence patterns for duplicates and merge
        for essence_pattern in essence_patterns {
            let key = essence_pattern.regex.as_str().to_string();

            match pattern_map.entry(key) {
                std::collections::hash_map::Entry::Occupied(mut entry) => {
                    // Duplicate found - mark as merged
                    entry.get_mut().source = PatternSource::Merged;
                }
                std::collections::hash_map::Entry::Vacant(entry) => {
                    // Unique pattern from essence mode
                    entry.insert(essence_pattern);
                }
            }
        }

        // Convert to vector
        merged_patterns.extend(pattern_map.into_values());

        merged_patterns
    }

    /// Assign priority ordering to prevent conflicts
    fn assign_pattern_priorities(patterns: &mut [TimestampPattern]) {
        for pattern in patterns.iter_mut() {
            let specificity = pattern.format_type.specificity_score();
            let family = pattern.format_type.format_family();
            pattern.priority = PatternPriority::new(specificity, family);
        }
    }

    /// Get all patterns for detection operations
    pub fn get_patterns(&self) -> &[TimestampPattern] {
        &self.patterns
    }
}

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

    #[test]
    fn assign_priorities_sets_nonzero() {
        let registry = TimestampRegistry::new();
        for pattern in registry.get_patterns() {
            assert_ne!(
                pattern.priority.specificity_score, 0,
                "Pattern {:?} should have nonzero specificity",
                pattern.format_type
            );
        }
    }

    // ---- Mutant-killing: load_original_essence_patterns (replace with vec![]) ----

    #[test]
    fn essence_patterns_loaded() {
        // Kills mutant: `load_original_essence_patterns` replaced with `vec![]`
        // Verify that essence-specific formats are present in the registry
        let registry = TimestampRegistry::new();
        let has_essence_pattern = registry.get_patterns().iter().any(|p| {
            matches!(
                p.format_type,
                TimestampFormat::WindowsEvent
                    | TimestampFormat::GitCommit
                    | TimestampFormat::Aws
                    | TimestampFormat::Gcp
                    | TimestampFormat::Azure
            )
        });
        assert!(
            has_essence_pattern,
            "Registry should contain essence-specific patterns"
        );
    }

    // ---- Mutant-killing: assign_pattern_priorities (replace with ()) ----

    #[test]
    fn assign_priorities_actually_changes_priorities() {
        // Kills mutant: `assign_pattern_priorities` body replaced with `()`
        // After construction, each pattern should have a priority that matches
        // its format_type's specificity score, not the initial hardcoded value
        let registry = TimestampRegistry::new();
        for pattern in registry.get_patterns() {
            let expected_specificity = pattern.format_type.specificity_score();
            assert_eq!(
                pattern.priority.specificity_score, expected_specificity,
                "Pattern {:?} should have specificity {} from assign_pattern_priorities, got {}",
                pattern.format_type, expected_specificity, pattern.priority.specificity_score
            );
        }
    }

    #[test]
    fn assign_priorities_unix_lowest() {
        let registry = TimestampRegistry::new();
        let unix_pattern = registry.get_patterns().iter().find(|p| {
            matches!(
                p.format_type,
                crate::patterns::timestamp::formats::TimestampFormat::UnixTimestamp
            )
        });
        if let Some(up) = unix_pattern {
            let structured_pattern = registry.get_patterns().iter().find(|p| {
                matches!(
                    p.format_type,
                    crate::patterns::timestamp::formats::TimestampFormat::ISO8601Full
                )
            });
            if let Some(sp) = structured_pattern {
                assert!(
                    up.priority.effective_score() > sp.priority.effective_score(),
                    "Unix should have lower priority (higher score) than ISO8601"
                );
            }
        }
    }
}