eggsact 1.2.7

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
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
use crate::mcp::machine_codes;
use crate::mcp::response::{disposition, finding, severity, verdict};
use crate::mcp::schemas::ToolResponse;
use crate::tools::helpers::*;
use serde_json::Value;
use std::collections::HashMap;

pub fn path_normalize_tool(args: &Value) -> ToolResponse {
    let path = match args.get("path").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'path' parameter",
                None,
                Some("path_normalize"),
            )
        }
    };
    let platform = args
        .get("platform")
        .and_then(|v| v.as_str())
        .unwrap_or("posix");
    let collapse_dot_segments = args
        .get("collapse_dot_segments")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let preserve_trailing_separator = args
        .get("preserve_trailing_separator")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    let valid_platforms = ["posix", "windows"];
    if !valid_platforms.contains(&platform) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported platform: {}", platform),
            Some(vec![format!("Use one of: {}", valid_platforms.join(", "))]),
            Some("path_normalize"),
        );
    }

    let result = crate::text::path::path_normalize(
        path,
        platform,
        collapse_dot_segments,
        preserve_trailing_separator,
    );
    ToolResponse::success(
        serde_json::json!({
            "normalized": result.normalized,
            "is_absolute": result.is_absolute,
            "components": result.components,
            "warnings": result.warnings,
        }),
        Some("path_normalize"),
    )
    .with_tool("path_normalize")
}

pub fn path_analyze(args: &Value) -> ToolResponse {
    let path = match args.get("path").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'path' parameter",
                None,
                Some("path_analyze"),
            )
        }
    };
    let style = args.get("style").and_then(|v| v.as_str()).unwrap_or("auto");
    let detail = args
        .get("detail")
        .and_then(|v| v.as_str())
        .unwrap_or("normal");

    if path.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "Path length {} exceeds MAX_TEXT_LENGTH {}",
                path.len(),
                MAX_TEXT_LENGTH
            ),
            None,
            Some("path_analyze"),
        );
    }

    let valid_styles = ["auto", "posix", "windows"];
    if !valid_styles.contains(&style) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported style: {}", style),
            Some(vec![format!("Use one of: {}", valid_styles.join(", "))]),
            Some("path_analyze"),
        );
    }

    let valid_details = ["summary", "normal", "full"];
    if !valid_details.contains(&detail) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported detail level: {}", detail),
            Some(vec![format!("Use one of: {}", valid_details.join(", "))]),
            Some("path_analyze"),
        );
    }

    let result = crate::text::path_analyze(path, style);

    let has_traversal = result.has_traversal;
    let is_hidden = result.hidden;
    let mut envelope_findings: Vec<serde_json::Value> = Vec::new();
    if has_traversal {
        envelope_findings.push(serde_json::json!({
            "code": "PATH_TRAVERSAL",
            "severity": "warn",
            "message": "Path contains parent directory traversal (..)",
            "details": {"normalized_lexical": result.normalized_lexical},
        }));
    }
    if is_hidden {
        envelope_findings.push(serde_json::json!({
            "code": "PATH_HIDDEN",
            "severity": "info",
            "message": "Path starts with a dot (hidden file/directory)",
        }));
    }
    let machine_code = if has_traversal {
        Some(machine_codes::PATH_HAS_TRAVERSAL)
    } else if is_hidden {
        Some(machine_codes::PATH_IS_HIDDEN)
    } else {
        None
    };

    if detail == "summary" {
        let mut resp = ToolResponse::success(
            serde_json::json!({
                "summary": result.summary,
                "style": result.style,
                "absolute": result.absolute,
                "hidden": result.hidden,
                "has_traversal": result.has_traversal,
                "warnings": result.warnings,
            }),
            Some("path_analyze"),
        )
        .with_tool("path_analyze");
        if !envelope_findings.is_empty() {
            resp = resp.with_findings(envelope_findings.clone());
        }
        if let Some(code) = machine_code {
            resp = resp.with_machine_code(code);
        }
        resp
    } else {
        let mut resp = ToolResponse::success(
            serde_json::json!({
                "input": result.input,
                "style": result.style,
                "absolute": result.absolute,
                "has_traversal": result.has_traversal,
                "components": result.components,
                "parent": result.parent,
                "name": result.name,
                "stem": result.stem,
                "suffix": result.suffix,
                "suffixes": result.suffixes,
                "hidden": result.hidden,
                "normalized_lexical": result.normalized_lexical,
                "warnings": result.warnings,
                "summary": result.summary,
            }),
            Some("path_analyze"),
        )
        .with_tool("path_analyze");
        if !envelope_findings.is_empty() {
            resp = resp.with_findings(envelope_findings);
        }
        if let Some(code) = machine_code {
            resp = resp.with_machine_code(code);
        }
        resp
    }
}

pub fn path_compare(args: &Value) -> ToolResponse {
    let left = match args.get("left").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'left' parameter",
                None,
                Some("path_compare"),
            )
        }
    };
    let right = match args.get("right").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'right' parameter",
                None,
                Some("path_compare"),
            )
        }
    };
    let platform = args
        .get("platform")
        .and_then(|v| v.as_str())
        .unwrap_or("posix");
    let case_sensitive = args
        .get("case_sensitive")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let normalize_separators = args
        .get("normalize_separators")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let collapse_dot_segments = args
        .get("collapse_dot_segments")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    if left.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            "Left path exceeds MAX_TEXT_LENGTH",
            None,
            Some("path_compare"),
        );
    }
    if right.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            "Right path exceeds MAX_TEXT_LENGTH",
            None,
            Some("path_compare"),
        );
    }

    let valid_platforms = ["posix", "windows"];
    if !valid_platforms.contains(&platform) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported platform: {}", platform),
            Some(vec![format!("Use one of: {}", valid_platforms.join(", "))]),
            Some("path_compare"),
        );
    }

    let result = crate::text::path_compare(
        left,
        right,
        platform,
        case_sensitive,
        normalize_separators,
        collapse_dot_segments,
    );

    ToolResponse::success(
        serde_json::json!({
            "equal": result.equal,
            "left_normalized": result.left_normalized,
            "right_normalized": result.right_normalized,
            "differences": result.differences,
            "findings": result.findings,
        }),
        Some("path_compare"),
    )
    .with_tool("path_compare")
}

pub fn path_scope_check(args: &Value) -> ToolResponse {
    let root = match args.get("root").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'root' parameter",
                None,
                Some("path_scope_check"),
            )
        }
    };
    let target = match args.get("target").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'target' parameter",
                None,
                Some("path_scope_check"),
            )
        }
    };
    let platform = args
        .get("platform")
        .and_then(|v| v.as_str())
        .unwrap_or("posix");
    let case_sensitive = args
        .get("case_sensitive")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    if root.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            "Root path exceeds MAX_TEXT_LENGTH",
            None,
            Some("path_scope_check"),
        );
    }
    if target.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            "Target path exceeds MAX_TEXT_LENGTH",
            None,
            Some("path_scope_check"),
        );
    }

    let valid_platforms = ["posix", "windows"];
    if !valid_platforms.contains(&platform) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported platform: {}", platform),
            Some(vec![format!("Use one of: {}", valid_platforms.join(", "))]),
            Some("path_scope_check"),
        );
    }

    let result = crate::text::path_scope_check(root, target, platform, case_sensitive);

    ToolResponse::success(
        serde_json::json!({
            "inside_root": result.inside_root,
            "root_normalized": result.root_normalized,
            "target_normalized": result.target_normalized,
            "relative_path": result.relative_path,
            "escapes_via_dotdot": result.escapes_via_dotdot,
            "absolute_target": result.absolute_target,
            "findings": result.findings,
        }),
        Some("path_scope_check"),
    )
    .with_tool("path_scope_check")
}

pub fn glob_match_tool(args: &Value) -> ToolResponse {
    let pattern = match args.get("pattern").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'pattern' parameter",
                None,
                Some("glob_match"),
            )
        }
    };
    let path = match args.get("path").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'path' parameter",
                None,
                Some("glob_match"),
            )
        }
    };
    let platform = args
        .get("platform")
        .and_then(|v| v.as_str())
        .unwrap_or("posix");
    let case_sensitive = args
        .get("case_sensitive")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    if pattern.len() > MAX_TEXT_LENGTH || path.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            "Pattern or path exceeds maximum length",
            None,
            Some("glob_match"),
        );
    }

    let valid_platforms = ["posix", "windows"];
    if !valid_platforms.contains(&platform) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported platform: {}", platform),
            Some(vec![format!("Use one of: {}", valid_platforms.join(", "))]),
            Some("glob_match"),
        );
    }

    let result = crate::text::glob::glob_match(pattern, path, platform, case_sensitive);

    ToolResponse::success(
        serde_json::json!({
            "matches": result.matches,
            "normalized_pattern": result.normalized_pattern,
            "normalized_path": result.normalized_path,
            "matched_segment": result.matched_segment,
            "unmatched_segment": result.unmatched_segment,
            "summary": result.summary,
        }),
        Some("glob_match"),
    )
    .with_tool("glob_match")
}

pub fn path_batch_scope_check(args: &Value) -> ToolResponse {
    let root = match args.get("root").and_then(|v| v.as_str()) {
        Some(s) => s,
        None => {
            return ToolResponse::error_with_code(
                "invalid_arguments",
                machine_codes::INVALID_ARGUMENTS,
                "Missing 'root' parameter",
                None,
                Some("path_batch_scope_check"),
            )
        }
    };

    let targets = match require_array_arg(args, "targets", "path_batch_scope_check") {
        Ok(arr) => arr,
        Err(resp) => return *resp,
    };

    let max_targets = args
        .get("max_targets")
        .and_then(|v| v.as_i64())
        .unwrap_or(1000) as usize;
    let allow_absolute = args
        .get("allow_absolute")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let case_sensitive = args
        .get("case_sensitive")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);

    if root.len() > MAX_TEXT_LENGTH {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            "Root path exceeds MAX_TEXT_LENGTH",
            None,
            Some("path_batch_scope_check"),
        );
    }

    if targets.len() > max_targets {
        return ToolResponse::error_with_code(
            "input_too_large",
            machine_codes::INPUT_TOO_LARGE,
            &format!(
                "Too many targets: {} exceeds max_targets {}",
                targets.len(),
                max_targets
            ),
            None,
            Some("path_batch_scope_check"),
        );
    }

    let valid_platforms = ["posix", "windows"];
    let platform = args
        .get("platform")
        .and_then(|v| v.as_str())
        .unwrap_or("posix");
    if !valid_platforms.contains(&platform) {
        return ToolResponse::error_with_code(
            "invalid_arguments",
            machine_codes::INVALID_ARGUMENTS,
            &format!("Unsupported platform: {}", platform),
            Some(vec![format!("Use one of: {}", valid_platforms.join(", "))]),
            Some("path_batch_scope_check"),
        );
    }

    let mut escaping_targets = Vec::new();
    let mut absolute_targets = Vec::new();
    let mut dotdot_targets = Vec::new();
    let mut normalized_targets = Vec::new();
    let mut findings = Vec::new();
    let mut seen_normalized: HashMap<String, Vec<String>> = HashMap::new();
    let mut all_inside = true;
    let mut skipped_non_string: usize = 0;
    let mut skipped_too_large: usize = 0;

    for target_val in targets {
        let target = match target_val.as_str() {
            Some(s) => s,
            None => {
                skipped_non_string += 1;
                findings.push(finding(
                    machine_codes::INVALID_ARGUMENTS,
                    severity::MEDIUM,
                    "Non-string target path in targets array",
                    Some(disposition::BLOCKING),
                    None,
                ));
                all_inside = false;
                continue;
            }
        };

        if target.len() > MAX_TEXT_LENGTH {
            skipped_too_large += 1;
            findings.push(finding(
                machine_codes::INPUT_TOO_LARGE,
                severity::HIGH,
                &format!("Target path '{}' exceeds MAX_TEXT_LENGTH", target),
                Some(disposition::BLOCKING),
                None,
            ));
            all_inside = false;
            continue;
        }

        let result = crate::text::path_scope_check(root, target, platform, case_sensitive);

        // Track normalized for duplicate detection
        seen_normalized
            .entry(result.target_normalized.clone())
            .or_default()
            .push(target.to_string());

        normalized_targets.push(serde_json::json!({
            "original": target,
            "normalized": result.target_normalized,
            "inside_root": result.inside_root,
        }));

        if !result.inside_root {
            all_inside = false;
            escaping_targets.push(target.to_string());
            findings.push(finding(
                machine_codes::PATH_SCOPE_ESCAPE,
                severity::HIGH,
                &format!("Target '{}' escapes root", target),
                Some(disposition::BLOCKING),
                None,
            ));
        }

        let is_abs = if !target.is_empty() {
            // Use same collapse_dot_segments=true as path_scope_check to avoid
            // classification inconsistency (e.g. "a/../" would differ).
            crate::text::path::path_normalize(target, platform, true, false).is_absolute
        } else {
            false
        };
        if is_abs {
            absolute_targets.push(target.to_string());
            if !allow_absolute {
                findings.push(finding(
                    machine_codes::PATH_SCOPE_ESCAPE,
                    severity::MEDIUM,
                    &format!("Absolute target '{}' not allowed", target),
                    Some(disposition::CAUTION),
                    None,
                ));
            }
        }

        // This is informational reporting only; path_scope_check above is the
        // security boundary that determines whether traversal escapes root.
        if target.contains("..") {
            dotdot_targets.push(target.to_string());
            if result.escapes_via_dotdot && result.inside_root {
                // dotdot that normalizes inside is informational
                findings.push(finding(
                    machine_codes::PATH_HAS_TRAVERSAL,
                    severity::LOW,
                    &format!(
                        "Target '{}' contains '..' but normalizes inside root",
                        target
                    ),
                    Some(disposition::INFORMATIONAL),
                    None,
                ));
            }
        }
    }

    // Detect duplicates
    let mut duplicate_normalized_targets = Vec::new();
    for (norm, originals) in &seen_normalized {
        if originals.len() > 1 {
            duplicate_normalized_targets.push(serde_json::json!({
                "normalized": norm,
                "originals": originals,
            }));
            findings.push(finding(
                machine_codes::PATH_BATCH_REVIEW,
                severity::LOW,
                &format!(
                    "Duplicate normalized path '{}' reached by {} inputs",
                    norm,
                    originals.len()
                ),
                Some(disposition::CAUTION),
                None,
            ));
        }
    }

    let machine_code = if all_inside && escaping_targets.is_empty() {
        machine_codes::PATH_BATCH_OK
    } else {
        machine_codes::PATH_BATCH_REVIEW
    };

    let v = if all_inside && escaping_targets.is_empty() {
        verdict::ALLOW
    } else {
        verdict::REVIEW
    };

    ToolResponse::success(
        serde_json::json!({
            "all_inside_root": all_inside,
            "targets_checked": targets.len(),
            "skipped_non_string_targets": skipped_non_string,
            "skipped_too_large_targets": skipped_too_large,
            "escaping_targets": escaping_targets,
            "absolute_targets": absolute_targets,
            "dotdot_targets": dotdot_targets,
            "normalized_targets": normalized_targets,
            "duplicate_normalized_targets": duplicate_normalized_targets,
            "findings": findings,
            "verdict": v,
            "machine_code": machine_code,
        }),
        Some("path_batch_scope_check"),
    )
    .with_tool("path_batch_scope_check")
    .with_machine_code(machine_code)
    .with_verdict(v)
}