mana-core 0.3.2

Core library for mana — task tracker for AI 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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use regex::Regex;
use std::collections::HashSet;
use std::fs;
use std::io;
use std::path::{Component, Path};
use std::sync::LazyLock;

// Compiled once, reused across all calls
static PATH_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    // Match file paths with supported extensions (tsx and yml added)
    Regex::new(r"([a-zA-Z0-9_.][a-zA-Z0-9_./\-]*\.(rs|tsx?|py|md|json|toml|ya?ml|sh|go|java))\b")
        .expect("Invalid regex pattern")
});

/// Extracts file paths from a unit description using regex pattern matching.
///
/// Matches relative file paths with the following extensions:
/// .rs, .ts, .py, .md, .json, .toml, .yaml, .sh, .go, .java
///
/// Examples:
/// - "Modify src/main.rs" → ["src/main.rs"]
/// - "See src/foo.rs and tests/bar.rs" → ["src/foo.rs", "tests/bar.rs"]
/// - "File: src/main.rs." → ["src/main.rs"]
///
/// # Arguments
/// * `description` - The description text to search for file paths
///
/// # Returns
/// A Vec of deduplicated file paths in order of appearance
pub fn extract_paths(description: &str) -> Vec<String> {
    let mut result = Vec::new();
    let mut seen = HashSet::new();

    for cap in PATH_REGEX.captures_iter(description) {
        if let Some(path) = cap.get(1) {
            let path_str = path.as_str();
            let path_start = path.start();

            // Filter out absolute paths: if preceded directly by /
            // Use byte access (O(1)) since '/' is ASCII
            if path_start > 0 && description.as_bytes()[path_start - 1] == b'/' {
                continue;
            }

            // Filter out URLs (check if preceded by :// in the description)
            let before = &description[path_start.saturating_sub(3)..path_start];
            if before.ends_with("://") {
                continue;
            }

            // Reject path traversal: any path containing ".." components
            // could escape the project directory
            if Path::new(path_str)
                .components()
                .any(|c| matches!(c, Component::ParentDir))
            {
                continue;
            }

            // Deduplicate and add to result
            if seen.insert(path_str.to_string()) {
                result.push(path_str.to_string());
            }
        }
    }

    result
}

/// Maximum file size to read (1 MB). Files referenced in unit descriptions
/// are embedded into LLM prompts, so reading very large files is wasteful
/// and risks unbounded memory usage.
const MAX_FILE_SIZE: u64 = 1_024 * 1_024;

/// Reads a file from disk and returns its contents as a string.
///
/// # Arguments
/// * `path` - The file path to read
///
/// # Returns
/// * `Ok(String)` - The file contents
/// * `Err` - If the file doesn't exist, is too large, is binary, or is not valid UTF-8
///
/// # Behavior
/// - Rejects files larger than 1 MB
/// - Reads raw bytes first, then checks for binary content (null bytes)
/// - Converts to UTF-8 only after binary check passes
pub fn read_file(path: &Path) -> io::Result<String> {
    let metadata = fs::metadata(path)?;
    if metadata.len() > MAX_FILE_SIZE {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "File too large ({} bytes, max {})",
                metadata.len(),
                MAX_FILE_SIZE
            ),
        ));
    }

    // Read raw bytes first so we can detect binary files that aren't valid UTF-8
    let bytes = fs::read(path)?;

    if bytes.contains(&0) {
        eprintln!("Warning: Skipping binary file: {}", path.display());
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "File appears to be binary (contains null bytes)",
        ));
    }

    String::from_utf8(bytes)
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "File is not valid UTF-8"))
}

/// Detects the programming language from a file extension.
///
/// Supports: rs, ts, tsx, py, go, java, json, yaml, toml, sh, md
fn detect_language(path: &str) -> &str {
    match path.split('.').next_back() {
        Some("rs") => "rust",
        Some("ts") => "typescript",
        Some("tsx") => "typescript",
        Some("py") => "python",
        Some("go") => "go",
        Some("java") => "java",
        Some("json") => "json",
        Some("yaml") | Some("yml") => "yaml",
        Some("toml") => "toml",
        Some("sh") => "sh",
        Some("md") => "markdown",
        _ => "text",
    }
}

/// Formats a file's content as a markdown code block.
///
/// # Arguments
/// * `path` - The file path (used for display and language detection)
/// * `content` - The file contents
///
/// # Returns
/// A markdown-formatted string with the file header and code fence
///
/// # Format
/// ````text
/// ## File: {path}
/// ```{lang}
/// {content}
/// ```
/// ````
pub fn format_file_block(path: &str, content: &str) -> String {
    let language = detect_language(path);
    format!("## File: {}\n```{}\n{}\n```\n", path, language, content)
}

/// Assembles context from multiple files into a single markdown document.
///
/// # Arguments
/// * `paths` - File paths to include
/// * `base_dir` - The base directory to resolve relative paths against
///
/// # Returns
/// * `Ok(String)` - Markdown containing all readable files (empty if none succeed)
/// * `Err` - If `base_dir` cannot be canonicalized
///
/// # Behavior
/// - Validates each resolved path stays within `base_dir` (prevents directory traversal)
/// - Skips files that escape the project directory, can't be read, or are binary/too large
/// - Continues even if some files fail
pub fn assemble_context(paths: Vec<String>, base_dir: &Path) -> io::Result<String> {
    let canonical_base = base_dir.canonicalize().map_err(|e| {
        io::Error::new(
            e.kind(),
            format!(
                "Cannot canonicalize base directory {}: {}",
                base_dir.display(),
                e
            ),
        )
    })?;

    let mut output = String::new();

    for path_str in paths {
        let full_path = base_dir.join(&path_str);

        // Canonicalize the resolved path and verify it stays within the project.
        // This catches symlinks and any traversal that survived extract_paths filtering.
        let canonical = match full_path.canonicalize() {
            Ok(p) => p,
            Err(_) => {
                // File doesn't exist or can't be resolved — skip silently
                eprintln!("Warning: Could not read file {}: not found", path_str);
                continue;
            }
        };

        if !canonical.starts_with(&canonical_base) {
            eprintln!(
                "Warning: Skipping file outside project directory: {}",
                path_str
            );
            continue;
        }

        match read_file(&canonical) {
            Ok(content) => {
                output.push_str(&format_file_block(&path_str, &content));
                output.push('\n');
            }
            Err(e) => {
                eprintln!("Warning: Could not read file {}: {}", path_str, e);
            }
        }
    }

    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::{assemble_context, detect_language, extract_paths, format_file_block, read_file};
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_single_path() {
        let result = extract_paths("Modify src/main.rs");
        assert_eq!(result, vec!["src/main.rs"]);
    }

    #[test]
    fn test_multiple_paths() {
        let result = extract_paths("See src/foo.rs and tests/bar.rs");
        assert_eq!(result, vec!["src/foo.rs", "tests/bar.rs"]);
    }

    #[test]
    fn test_deduplicate_paths() {
        let result = extract_paths("Update src/main.rs to fix src/main.rs");
        assert_eq!(result, vec!["src/main.rs"]);
    }

    #[test]
    fn test_with_punctuation() {
        let result = extract_paths("File: src/main.rs.");
        assert_eq!(result, vec!["src/main.rs"]);
    }

    #[test]
    fn test_no_paths() {
        let result = extract_paths("No files mentioned here");
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_various_extensions() {
        let description =
            "Check src/config.rs, tests/test.ts, docs/guide.md, package.json, and Cargo.toml";
        let result = extract_paths(description);
        assert_eq!(
            result,
            vec![
                "src/config.rs",
                "tests/test.ts",
                "docs/guide.md",
                "package.json",
                "Cargo.toml"
            ]
        );
    }

    #[test]
    fn test_paths_with_hyphens() {
        let result = extract_paths("See src/my-module.rs and tests/integration-test.rs");
        assert_eq!(
            result,
            vec!["src/my-module.rs", "tests/integration-test.rs"]
        );
    }

    #[test]
    fn test_paths_with_underscores() {
        let result = extract_paths("Update src/my_module.rs in tests/my_test.rs");
        assert_eq!(result, vec!["src/my_module.rs", "tests/my_test.rs"]);
    }

    #[test]
    fn test_deeply_nested_paths() {
        let result = extract_paths("Modify deeply/nested/path/to/src/main.rs");
        assert_eq!(result, vec!["deeply/nested/path/to/src/main.rs"]);
    }

    #[test]
    fn test_ignores_absolute_paths() {
        // Absolute paths starting with / should not match
        let result = extract_paths("Do not match /absolute/path/file.rs");
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_ignores_urls() {
        // URLs should not match due to :// and domain patterns
        let result = extract_paths("See https://example.com/file.rs for details");
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_mixed_valid_and_invalid() {
        let description = "Check src/main.rs at https://example.com/file.rs and tests/test.ts";
        let result = extract_paths(description);
        assert_eq!(result, vec!["src/main.rs", "tests/test.ts"]);
    }

    #[test]
    fn test_order_of_appearance() {
        let description = "Start with z/file.rs, then a/file.rs, then m/file.rs";
        let result = extract_paths(description);
        assert_eq!(result, vec!["z/file.rs", "a/file.rs", "m/file.rs"]);
    }

    #[test]
    fn test_yaml_and_json_extensions() {
        let result = extract_paths("Update config.yaml and settings.json");
        assert_eq!(result, vec!["config.yaml", "settings.json"]);
    }

    #[test]
    fn test_go_and_java_extensions() {
        let result = extract_paths("Implement src/main.go and src/Main.java");
        assert_eq!(result, vec!["src/main.go", "src/Main.java"]);
    }

    #[test]
    fn test_tsx_extension() {
        let result = extract_paths("Update components/Button.tsx and pages/Home.tsx");
        assert_eq!(result, vec!["components/Button.tsx", "pages/Home.tsx"]);
    }

    #[test]
    fn test_yml_extension() {
        let result = extract_paths("Edit .github/workflows/ci.yml and docker-compose.yml");
        assert_eq!(
            result,
            vec![".github/workflows/ci.yml", "docker-compose.yml"]
        );
    }

    #[test]
    fn test_shell_script_extension() {
        let result = extract_paths("Run scripts/deploy.sh for deployment");
        assert_eq!(result, vec!["scripts/deploy.sh"]);
    }

    #[test]
    fn test_empty_string() {
        let result = extract_paths("");
        assert_eq!(result.len(), 0);
    }

    #[test]
    fn test_path_in_middle_of_sentence() {
        let result = extract_paths("The file src/config.rs needs updating because reasons");
        assert_eq!(result, vec!["src/config.rs"]);
    }

    #[test]
    fn test_path_at_start_of_string() {
        let result = extract_paths("src/main.rs is the entry point");
        assert_eq!(result, vec!["src/main.rs"]);
    }

    #[test]
    fn test_path_at_end_of_string() {
        let result = extract_paths("Please modify src/main.rs");
        assert_eq!(result, vec!["src/main.rs"]);
    }

    #[test]
    fn test_adjacent_paths() {
        let result = extract_paths("src/foo.rs src/bar.rs");
        assert_eq!(result, vec!["src/foo.rs", "src/bar.rs"]);
    }

    #[test]
    fn test_paths_with_numbers() {
        let result = extract_paths("Update src/v2/main.rs and test_1.rs");
        assert_eq!(result, vec!["src/v2/main.rs", "test_1.rs"]);
    }

    // Tests for path traversal rejection
    #[test]
    fn test_rejects_parent_traversal() {
        let result = extract_paths("Read ../../etc/shadow.md for secrets");
        assert!(result.is_empty());
    }

    #[test]
    fn test_rejects_mid_path_traversal() {
        let result = extract_paths("Check src/../../../.ssh/config.json");
        assert!(result.is_empty());
    }

    #[test]
    fn test_rejects_traversal_keeps_valid() {
        let result = extract_paths("Check src/main.rs and ../../etc/passwd.yaml");
        assert_eq!(result, vec!["src/main.rs"]);
    }

    #[test]
    fn test_allows_dots_in_filenames() {
        // ".." as a path component is rejected, but dots in filenames are fine
        let result = extract_paths("Check src/my.module.rs");
        assert_eq!(result, vec!["src/my.module.rs"]);
    }

    // Tests for read_file function
    #[test]
    fn test_read_file_success() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        let content = "fn main() {\n    println!(\"Hello\");\n}\n";
        fs::write(&test_file, content).unwrap();

        let result = read_file(&test_file).unwrap();
        assert_eq!(result, content);
    }

    #[test]
    fn test_read_file_missing() {
        let temp_dir = TempDir::new().unwrap();
        let missing_file = temp_dir.path().join("nonexistent.rs");

        let result = read_file(&missing_file);
        assert!(result.is_err());
    }

    #[test]
    fn test_read_file_binary() {
        let temp_dir = TempDir::new().unwrap();
        let binary_file = temp_dir.path().join("binary.bin");
        let binary_content = vec![0, 1, 2, 3, 0, 255];
        fs::write(&binary_file, binary_content).unwrap();

        let result = read_file(&binary_file);
        assert!(result.is_err());
    }

    #[test]
    fn test_read_file_rejects_oversized() {
        let temp_dir = TempDir::new().unwrap();
        let big_file = temp_dir.path().join("huge.rs");
        let content = "x".repeat(1_024 * 1_024 + 1);
        fs::write(&big_file, &content).unwrap();

        let result = read_file(&big_file);
        assert!(result.is_err());
        assert!(
            result.unwrap_err().to_string().contains("too large"),
            "Error message should mention size"
        );
    }

    #[test]
    fn test_read_file_rejects_non_utf8() {
        let temp_dir = TempDir::new().unwrap();
        let bad_file = temp_dir.path().join("bad.rs");
        // Invalid UTF-8 sequence without null bytes
        fs::write(&bad_file, [0xFF, 0xFE, 0x41, 0x42]).unwrap();

        let result = read_file(&bad_file);
        assert!(result.is_err());
    }

    // Tests for detect_language function
    #[test]
    fn test_detect_language_rust() {
        assert_eq!(detect_language("src/main.rs"), "rust");
    }

    #[test]
    fn test_detect_language_python() {
        assert_eq!(detect_language("script.py"), "python");
    }

    #[test]
    fn test_detect_language_json() {
        assert_eq!(detect_language("config.json"), "json");
    }

    #[test]
    fn test_detect_language_yaml() {
        assert_eq!(detect_language("config.yaml"), "yaml");
    }

    #[test]
    fn test_detect_language_yml() {
        assert_eq!(detect_language("config.yml"), "yaml");
    }

    #[test]
    fn test_detect_language_typescript() {
        assert_eq!(detect_language("index.ts"), "typescript");
    }

    #[test]
    fn test_detect_language_tsx() {
        assert_eq!(detect_language("component.tsx"), "typescript");
    }

    #[test]
    fn test_detect_language_go() {
        assert_eq!(detect_language("main.go"), "go");
    }

    #[test]
    fn test_detect_language_java() {
        assert_eq!(detect_language("Main.java"), "java");
    }

    #[test]
    fn test_detect_language_shell() {
        assert_eq!(detect_language("deploy.sh"), "sh");
    }

    #[test]
    fn test_detect_language_markdown() {
        assert_eq!(detect_language("README.md"), "markdown");
    }

    #[test]
    fn test_detect_language_toml() {
        assert_eq!(detect_language("Cargo.toml"), "toml");
    }

    #[test]
    fn test_detect_language_unknown() {
        assert_eq!(detect_language("file.unknown"), "text");
    }

    // Tests for format_file_block function
    #[test]
    fn test_format_file_block_rust() {
        let path = "src/main.rs";
        let content = "fn main() {}";
        let result = format_file_block(path, content);

        assert!(result.contains("## File: src/main.rs"));
        assert!(result.contains("```rust"));
        assert!(result.contains("fn main() {}"));
        assert!(result.contains("```"));
    }

    #[test]
    fn test_format_file_block_python() {
        let path = "script.py";
        let content = "print('hello')";
        let result = format_file_block(path, content);

        assert!(result.contains("## File: script.py"));
        assert!(result.contains("```python"));
        assert!(result.contains("print('hello')"));
    }

    #[test]
    fn test_format_file_block_json() {
        let path = "config.json";
        let content = r#"{"key": "value"}"#;
        let result = format_file_block(path, content);

        assert!(result.contains("## File: config.json"));
        assert!(result.contains("```json"));
        assert!(result.contains(r#"{"key": "value"}"#));
    }

    #[test]
    fn test_format_file_block_multiline() {
        let path = "src/lib.rs";
        let content = "pub fn foo() {\n    // comment\n    return 42;\n}";
        let result = format_file_block(path, content);

        assert!(result.contains("## File: src/lib.rs"));
        assert!(result.contains("```rust"));
        assert!(result.contains("pub fn foo()"));
        assert!(result.contains("// comment"));
        assert!(result.contains("return 42;"));
    }

    // Tests for assemble_context function
    #[test]
    fn test_assemble_context_single_file() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let result = assemble_context(vec!["test.rs".to_string()], temp_dir.path()).unwrap();

        assert!(result.contains("## File: test.rs"));
        assert!(result.contains("```rust"));
        assert!(result.contains("fn main() {}"));
    }

    #[test]
    fn test_assemble_context_multiple_files() {
        let temp_dir = TempDir::new().unwrap();

        let file1 = temp_dir.path().join("file1.rs");
        fs::write(&file1, "// file 1").unwrap();

        let file2 = temp_dir.path().join("file2.py");
        fs::write(&file2, "# file 2").unwrap();

        let result = assemble_context(
            vec!["file1.rs".to_string(), "file2.py".to_string()],
            temp_dir.path(),
        )
        .unwrap();

        assert!(result.contains("## File: file1.rs"));
        assert!(result.contains("```rust"));
        assert!(result.contains("// file 1"));

        assert!(result.contains("## File: file2.py"));
        assert!(result.contains("```python"));
        assert!(result.contains("# file 2"));
    }

    #[test]
    fn test_assemble_context_skips_missing_files() {
        let temp_dir = TempDir::new().unwrap();

        let existing = temp_dir.path().join("exists.rs");
        fs::write(&existing, "fn hello() {}").unwrap();

        let result = assemble_context(
            vec!["exists.rs".to_string(), "missing.rs".to_string()],
            temp_dir.path(),
        )
        .unwrap();

        // Should contain existing file
        assert!(result.contains("## File: exists.rs"));
        assert!(result.contains("fn hello() {}"));

        // Should not contain missing file
        assert!(!result.contains("missing.rs"));
    }

    #[test]
    fn test_assemble_context_empty_paths() {
        let temp_dir = TempDir::new().unwrap();

        let result = assemble_context(vec![], temp_dir.path()).unwrap();

        assert_eq!(result.trim(), "");
    }

    #[test]
    fn test_assemble_context_rejects_symlink_escape() {
        let temp_dir = TempDir::new().unwrap();
        let project = temp_dir.path().join("project");
        fs::create_dir(&project).unwrap();

        // Create a secret file outside the project
        let secret = temp_dir.path().join("secret.json");
        fs::write(&secret, r#"{"api_key": "leaked"}"#).unwrap();

        // Create a symlink inside the project pointing outside
        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(&secret, project.join("secret.json")).unwrap();
            let result = assemble_context(vec!["secret.json".to_string()], &project).unwrap();
            assert!(
                !result.contains("leaked"),
                "Symlink escape should be blocked"
            );
        }
    }

    #[test]
    fn test_assemble_context_preserves_content() {
        let temp_dir = TempDir::new().unwrap();

        let test_file = temp_dir.path().join("test.json");
        let content = r#"{
  "key": "value",
  "nested": {
    "inner": 42
  }
}"#;
        fs::write(&test_file, content).unwrap();

        let result = assemble_context(vec!["test.json".to_string()], temp_dir.path()).unwrap();

        assert!(result.contains(content));
    }
}