eggsact 1.2.0

Deterministic MCP and in-process utility tools for coding agents
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
use crate::text::confusables::find_confusables;

#[derive(Debug, Clone)]
pub struct PathNormalizeResult {
    pub normalized: String,
    pub is_absolute: bool,
    pub components: Vec<String>,
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct PathAnalyzeResult {
    pub input: String,
    pub style: String,
    pub absolute: bool,
    pub has_traversal: bool,
    pub components: Vec<String>,
    pub parent: Option<String>,
    pub name: Option<String>,
    pub stem: Option<String>,
    pub suffix: Option<String>,
    pub suffixes: Vec<String>,
    pub hidden: bool,
    pub normalized_lexical: String,
    pub warnings: Vec<String>,
    pub summary: String,
}

#[derive(Debug, Clone)]
pub struct PathCompareResult {
    pub equal: bool,
    pub left_normalized: String,
    pub right_normalized: String,
    pub differences: Vec<String>,
    pub findings: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct PathScopeCheckResult {
    pub inside_root: bool,
    pub root_normalized: String,
    pub target_normalized: String,
    pub relative_path: String,
    pub escapes_via_dotdot: bool,
    pub absolute_target: String,
    pub findings: Vec<String>,
}

fn _detect_windows_path(path: &str) -> bool {
    if path.len() < 2 {
        return false;
    }
    if path.chars().nth(1) == Some(':') {
        return true;
    }
    if path.starts_with("\\\\") {
        return true;
    }
    if path.contains('\\') {
        return true;
    }
    false
}

fn _split_posix_components(path: &str) -> (Vec<&str>, Option<&str>) {
    if path.is_empty() {
        return (vec![], None);
    }

    if let Some(rest) = path.strip_prefix('/') {
        let root = "/";
        if rest.is_empty() {
            return (vec![], Some(root));
        }
        let parts: Vec<&str> = rest.split('/').filter(|p| !p.is_empty()).collect();
        return (parts, Some(root));
    }

    let parts: Vec<&str> = path.split('/').filter(|p| !p.is_empty()).collect();
    (parts, None)
}

fn _split_windows_components(path: &str) -> (Vec<&str>, Option<String>) {
    if path.is_empty() {
        return (vec![], None);
    }

    if path.chars().count() >= 2 {
        let chars: Vec<char> = path.chars().collect();
        if chars[1] == ':' {
            let root = path[..2].to_string();
            let rest = &path[2..];
            if rest.is_empty() {
                return (vec![], Some(root));
            }
            let parts: Vec<&str> = rest.split(['/', '\\']).filter(|p| !p.is_empty()).collect();
            return (parts, Some(root));
        }
    }

    if path.starts_with("\\\\") {
        let parts: Vec<&str> = path.split(['/', '\\']).filter(|p| !p.is_empty()).collect();
        if parts.len() >= 3 {
            let root = format!("\\\\{}\\{}", parts[0], parts[1]);
            let components: Vec<&str> = parts[2..].to_vec();
            return (components, Some(root));
        } else {
            return (vec![], Some(path.to_string()));
        }
    }

    if path.contains('\\') {
        let parts: Vec<&str> = path.split(['/', '\\']).filter(|p| !p.is_empty()).collect();
        return (parts, None);
    }

    let parts: Vec<&str> = path.split('/').filter(|p| !p.is_empty()).collect();
    (parts, None)
}

fn _get_suffixes(name: &str) -> Vec<String> {
    if name.is_empty() || name == "." {
        return vec![];
    }

    let parts: Vec<&str> = name.split('.').collect();
    if parts.len() <= 1 {
        return vec![];
    }

    let mut suffixes = vec![];
    for i in 1..parts.len() {
        let suffix = format!(".{}", parts[i..].join("."));
        suffixes.push(suffix);
    }

    suffixes
}

pub fn path_analyze(path: &str, style: &str) -> PathAnalyzeResult {
    let mut warnings: Vec<String> = vec![];
    let input_path = path.to_string();

    let actual_style = if style == "auto" {
        if _detect_windows_path(path) {
            "windows".to_string()
        } else {
            "posix".to_string()
        }
    } else {
        style.to_string()
    };

    let (raw_components, root): (Vec<&str>, Option<String>) = if actual_style == "windows" {
        _split_windows_components(path)
    } else {
        let (comps, root_str) = _split_posix_components(path);
        (comps, root_str.map(|s| s.to_string()))
    };

    let sep = if actual_style == "windows" { "\\" } else { "/" };

    let mut components: Vec<&str> = vec![];
    let mut normalized_parts: Vec<&str> = vec![];

    for (i, comp) in raw_components.iter().enumerate() {
        if *comp == "." {
            warnings.push(format!(
                "Redundant current directory segment at position {}",
                i
            ));
            components.push(comp);
            normalized_parts.push(comp);
        } else if *comp == ".." {
            warnings.push(format!("Parent traversal segment at position {}", i));
            components.push(comp);
            normalized_parts.push(comp);
        } else {
            components.push(comp);
            normalized_parts.push(comp);
        }
    }

    let has_traversal = raw_components.contains(&"..");
    let absolute = root.is_some();

    let confusables = find_confusables(path);
    if !confusables.is_empty() {
        warnings.push(format!(
            "Path contains {} confusable character(s)",
            confusables.len()
        ));
    }

    let name = components.last().map(|s| s.to_string());

    let (suffixes, suffix, stem) = if let Some(ref name_str) = name {
        let suffs = _get_suffixes(name_str);
        let suff = suffs.last().cloned();
        let full_suff = suffs.first().cloned();
        let stm = if let Some(ref fs) = full_suff {
            if !fs.is_empty() {
                let name_len = name_str.len();
                let fs_len = fs.len();
                name_str[..name_len - fs_len].to_string()
            } else {
                name_str.to_string()
            }
        } else {
            name_str.to_string()
        };
        (suffs, suff, Some(stm))
    } else {
        (vec![], None, None)
    };

    let parent = if !components.is_empty() {
        let parent_parts = &components[..components.len() - 1];
        if !parent_parts.is_empty() {
            let joined = parent_parts.join(sep);
            if let Some(ref root_str) = root {
                if actual_style == "posix" {
                    Some(format!("{}{}", sep, joined))
                } else {
                    Some(format!("{}{}{}", root_str, sep, joined))
                }
            } else {
                Some(joined)
            }
        } else {
            None
        }
    } else {
        None
    };

    let mut hidden = false;
    if let Some(ref name_str) = name {
        if name_str != "." && name_str != ".." {
            hidden = name_str.starts_with('.');
        }
    }

    let mut normalized = normalized_parts.join(sep);
    if root.is_some() && actual_style == "posix" {
        normalized = format!("{}{}", sep, normalized);
    }

    let mut summary_parts = vec![];
    if actual_style != "auto" {
        summary_parts.push(actual_style.to_uppercase());
    }
    if absolute {
        summary_parts.push("absolute".to_string());
    } else {
        summary_parts.push("relative".to_string());
    }
    if hidden {
        summary_parts.push("hidden".to_string());
    }
    if has_traversal {
        summary_parts.push("with traversal".to_string());
    }
    if components.len() == 1 {
        summary_parts.push(format!("single component '{}'", components[0]));
    } else if !components.is_empty() {
        summary_parts.push(format!("{} components", components.len()));
    }
    if let Some(ref suff) = suffix {
        if suffixes.len() > 1 {
            summary_parts.push(format!("suffixes {:?}", suffixes));
        } else {
            summary_parts.push(format!("suffix '{}'", suff));
        }
    }

    let summary = if !summary_parts.is_empty() {
        summary_parts.join(", ")
    } else {
        "empty path".to_string()
    };

    PathAnalyzeResult {
        input: input_path,
        style: actual_style,
        absolute,
        has_traversal,
        components: components.into_iter().map(|s| s.to_string()).collect(),
        parent,
        name,
        stem,
        suffix,
        suffixes,
        hidden,
        normalized_lexical: normalized,
        warnings,
        summary,
    }
}

pub fn path_normalize(
    path: &str,
    platform: &str,
    collapse_dot_segments: bool,
    preserve_trailing_separator: bool,
) -> PathNormalizeResult {
    let mut warnings: Vec<String> = vec![];
    let mut has_dot_dot = false;
    let mut has_dot = false;
    let had_trailing_separator = path.ends_with('/') || path.ends_with('\\');

    let actual_platform = if platform != "posix" && platform != "windows" {
        "posix"
    } else {
        platform
    };

    let sep = if actual_platform == "posix" {
        "/"
    } else {
        "\\"
    };

    let mut components: Vec<&str> = vec![];
    let mut is_unc_track =
        actual_platform == "windows" && (path.starts_with("\\\\") || path.starts_with("//"));

    let split_seps: &[char] = if actual_platform == "windows" {
        &['/', '\\']
    } else {
        &['/']
    };

    for part in path.split(split_seps) {
        if part.is_empty() {
            continue;
        }
        if part == "." {
            has_dot = true;
            if collapse_dot_segments {
                warnings.push("Collapsing dot segment".to_string());
                continue;
            } else {
                components.push(part);
                continue;
            }
        } else if part == ".." {
            has_dot_dot = true;
            if collapse_dot_segments {
                warnings.push("Collapsing dot-dot segment".to_string());
                if is_unc_track {
                    if !components.is_empty()
                        && components.last() != Some(&"")
                        && components.last() != Some(&"..")
                    {
                        if components.last() != Some(&"server") || components.len() == 1 {
                            components.pop();
                        } else {
                            components.push("..");
                        }
                    } else {
                        components.push("..");
                    }
                } else if !components.is_empty() && components.last() != Some(&"..") {
                    components.pop();
                } else {
                    components.push("..");
                }
            } else {
                components.push(part);
            }
            continue;
        } else if is_unc_track && (part == "server" || part == "share") {
            if components.len() >= 2 {
                is_unc_track = false;
            }
            components.push(part);
        } else if !part.is_empty() && part != "." && part != ".." {
            components.push(part);
        }
    }

    if preserve_trailing_separator && had_trailing_separator && !components.is_empty() {
        components.push("");
    }

    let mut normalized = if components.is_empty() {
        String::new()
    } else {
        components.join(sep)
    };

    if actual_platform == "posix" && path.starts_with('/') && !normalized.starts_with('/') {
        normalized = format!("/{}", normalized);
    } else if actual_platform == "windows" {
        if is_unc_track {
            normalized = format!("\\\\{}", normalized);
        } else if path.len() >= 2 && path.chars().nth(1) == Some(':') {
            // The drive letter is already embedded as the first component; strip
            // it from the joined string before prepending so we don't duplicate.
            let drive = &path[..2];
            let tail = normalized
                .strip_prefix(drive)
                .unwrap_or(normalized.as_str());
            normalized = format!("{}{}", drive, tail);
        }
    }

    if normalized.is_empty() {
        if actual_platform == "posix" && path.starts_with('/') {
            normalized = "/".to_string();
        } else if actual_platform == "windows" && is_unc_track {
            normalized = "\\\\".to_string();
        }
    }

    let is_absolute = if actual_platform == "posix" {
        path.starts_with('/')
    } else {
        (path.len() >= 2 && path.chars().nth(1) == Some(':')) || is_unc_track
    };

    if has_dot && !collapse_dot_segments {
        warnings.push("Path contains dot segments".to_string());
    }
    if has_dot_dot && !collapse_dot_segments {
        warnings.push("Path contains parent traversal segments".to_string());
    }

    PathNormalizeResult {
        normalized,
        is_absolute,
        components: components.into_iter().map(|s| s.to_string()).collect(),
        warnings,
    }
}

fn _normalize_path_for_compare(
    path: &str,
    platform: &str,
    collapse_dot_segments: bool,
    normalize_separators: bool,
) -> String {
    let mut result = path.to_string();
    if normalize_separators {
        if platform == "posix" {
            result = result.replace('\\', "/");
        } else {
            result = result.replace('/', "\\");
        }
    }
    let norm_result = path_normalize(&result, platform, collapse_dot_segments, false);
    norm_result.normalized
}

pub fn path_compare(
    left: &str,
    right: &str,
    platform: &str,
    case_sensitive: bool,
    normalize_separators: bool,
    collapse_dot_segments: bool,
) -> PathCompareResult {
    let mut findings: Vec<String> = vec![];

    let actual_platform = if platform != "posix" && platform != "windows" {
        "posix"
    } else {
        platform
    };

    let left_normalized = _normalize_path_for_compare(
        left,
        actual_platform,
        collapse_dot_segments,
        normalize_separators,
    );
    let right_normalized = _normalize_path_for_compare(
        right,
        actual_platform,
        collapse_dot_segments,
        normalize_separators,
    );

    let mut left_cmp = left_normalized.clone();
    let mut right_cmp = right_normalized.clone();

    if !case_sensitive {
        left_cmp = left_cmp.to_lowercase();
        right_cmp = right_cmp.to_lowercase();
    }

    let equal = left_cmp == right_cmp;

    let mut differences: Vec<String> = vec![];
    if !equal {
        differences.push(format!(
            "Normalized forms differ: '{}' vs '{}'",
            left_normalized, right_normalized
        ));
    }

    if !case_sensitive {
        findings.push("Case-insensitive comparison used".to_string());
    }
    if normalize_separators {
        findings.push("Separators normalized to platform default".to_string());
    }
    if collapse_dot_segments {
        findings.push("Dot segments collapsed".to_string());
    }

    PathCompareResult {
        equal,
        left_normalized,
        right_normalized,
        differences,
        findings,
    }
}

pub fn path_scope_check(
    root: &str,
    target: &str,
    platform: &str,
    case_sensitive: bool,
) -> PathScopeCheckResult {
    let mut findings: Vec<String> = vec![];

    let actual_platform = if platform != "posix" && platform != "windows" {
        "posix"
    } else {
        platform
    };

    fn pre_normalize(p: &str, platform: &str) -> String {
        if platform == "windows" {
            p.replace('/', "\\")
        } else {
            p.replace('\\', "/")
        }
    }

    let root_pre = pre_normalize(root, actual_platform);
    let target_pre = pre_normalize(target, actual_platform);

    let root_norm = path_normalize(&root_pre, actual_platform, true, false);
    let target_norm = path_normalize(&target_pre, actual_platform, true, false);

    let root_normalized = root_norm.normalized;
    let target_normalized = target_norm.normalized;

    let root_is_abs = root_norm.is_absolute;
    let target_is_abs = target_norm.is_absolute;

    if target_is_abs && !root_is_abs {
        findings.push("Target is absolute but root is relative".to_string());
    }

    let mut absolute_target = target_normalized.clone();
    if !target_is_abs {
        if actual_platform == "posix" {
            let stripped_root = root_normalized.trim_end_matches('/');
            absolute_target = format!("{}/{}", stripped_root, target_normalized);
        } else {
            let stripped_root = root_normalized.trim_end_matches('\\');
            absolute_target = format!("{}\\{}", stripped_root, target_normalized);
        }
        let abs_norm = path_normalize(&absolute_target, actual_platform, true, false);
        absolute_target = abs_norm.normalized;
    }

    let mut root_cmp = root_normalized.clone();
    let mut target_cmp = absolute_target.clone();
    if !case_sensitive {
        root_cmp = root_cmp.to_lowercase();
        target_cmp = target_cmp.to_lowercase();
    }

    let root_prefix = if actual_platform == "posix" {
        format!("{}/", root_cmp.trim_end_matches('/'))
    } else {
        format!("{}\\", root_cmp.trim_end_matches('\\'))
    };

    let inside_root = target_cmp.starts_with(&root_prefix) || target_cmp == root_cmp;

    let escapes_via_dotdot = target.split(['/', '\\']).any(|seg| seg == "..");

    let mut relative_path = String::new();
    if inside_root {
        relative_path = target_cmp
            .get(root_prefix.len()..)
            .unwrap_or("")
            .to_string();
        if relative_path.is_empty() {
            relative_path = ".".to_string();
        }
    }

    if !case_sensitive {
        findings.push("Case-insensitive comparison used".to_string());
    }
    if escapes_via_dotdot {
        findings.push("Target path contains parent traversal segments".to_string());
    }
    if !target_is_abs {
        findings.push("Target is relative, resolved against root".to_string());
    }

    PathScopeCheckResult {
        inside_root,
        root_normalized,
        target_normalized,
        relative_path,
        escapes_via_dotdot,
        absolute_target,
        findings,
    }
}