agpm-cli 0.4.14

AGent Package Manager - A Git-based package manager 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
//! File reference extraction and validation for markdown documents.
//!
//! This module provides utilities to extract and validate markdown file references
//! within markdown content. It helps catch broken cross-references before
//! installation by checking that referenced files actually exist.
//!
//! # Supported Reference Types
//!
//! - **Markdown links**: `[text](path.md)` - only `.md` files
//! - **Direct file paths**: `.agpm/snippets/file.md`, `docs/guide.md` - only `.md` files
//!
//! # Extraction Rules
//!
//! The extractor intelligently filters references to avoid false positives:
//! - Skips absolute URLs (http://, https://, etc.)
//! - Skips absolute filesystem paths (starting with /)
//! - Skips content inside YAML frontmatter (--- delimited)
//! - Skips content inside code blocks (``` delimited)
//! - Skips content inside inline code (` delimited)
//! - Only extracts relative markdown file paths (.md extension)
//!
//! # Usage
//!
//! ```rust,no_run
//! use agpm_cli::markdown::reference_extractor::{extract_file_references, validate_file_references};
//! use std::path::Path;
//!
//! # fn example() -> anyhow::Result<()> {
//! let markdown = r#"
//! See [documentation](../docs/guide.md) for details.
//!
//! Also check `.agpm/snippets/example.md` for examples.
//! "#;
//!
//! let references = extract_file_references(markdown);
//! // Returns: ["../docs/guide.md", ".agpm/snippets/example.md"]
//!
//! // Validate references exist
//! let project_dir = Path::new("/path/to/project");
//! let missing = validate_file_references(&references, project_dir)?;
//! # Ok(())
//! # }
//! ```

use anyhow::Result;
use regex::Regex;
use std::path::Path;

use crate::markdown::frontmatter::FrontmatterParser;

/// A missing file reference found during validation.
///
/// This struct captures information about a file reference that was found
/// in markdown content but does not exist on the filesystem.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MissingReference {
    /// The markdown file that contains the broken reference
    pub source_file: String,

    /// The referenced path that was not found
    pub referenced_path: String,
}

impl MissingReference {
    /// Create a new missing reference record.
    ///
    /// # Arguments
    ///
    /// * `source_file` - The file containing the reference
    /// * `referenced_path` - The path that was referenced but not found
    #[must_use]
    pub fn new(source_file: String, referenced_path: String) -> Self {
        Self {
            source_file,
            referenced_path,
        }
    }
}

/// Extract markdown file references from markdown content.
///
/// This function scans markdown content for markdown file path references and returns
/// a deduplicated list of relative markdown file paths. It intelligently filters out
/// URLs, absolute paths, non-markdown files, and references inside code blocks.
///
/// # Extracted Reference Types
///
/// - Markdown links: `[text](path.md)` → extracts `path.md` (only `.md` files)
/// - Direct file paths: `.agpm/snippets/file.md` → extracts `.agpm/snippets/file.md` (only `.md` files)
///
/// # Filtering Rules
///
/// References are excluded if they:
/// - Start with URL schemes (http://, https://, ftp://, etc.)
/// - Are absolute paths (starting with /)
/// - Appear inside YAML frontmatter (--- delimited at file start)
/// - Appear inside code blocks (``` delimited)
/// - Appear inside inline code (` delimited)
/// - Don't have the .md extension
/// - Contain URL-like patterns (://)
///
/// # Arguments
///
/// * `content` - The markdown content to scan
///
/// # Returns
///
/// A vector of unique relative file paths found in the content
///
/// # Examples
///
/// ```rust,no_run
/// # use agpm_cli::markdown::reference_extractor::extract_file_references;
/// let markdown = r#"
/// Check [docs](./guide.md) and `.agpm/snippets/example.md`.
///
/// But not this [external link](https://example.com) or `inline code .md`.
/// "#;
///
/// let refs = extract_file_references(markdown);
/// assert_eq!(refs.len(), 2);
/// assert!(refs.contains(&"./guide.md".to_string()));
/// assert!(refs.contains(&".agpm/snippets/example.md".to_string()));
/// ```
#[must_use]
pub fn extract_file_references(content: &str) -> Vec<String> {
    let mut references = Vec::new();

    // Remove frontmatter and code blocks to avoid extracting paths from metadata
    let content_without_frontmatter = remove_frontmatter(content);
    let content_without_code = remove_code_blocks(&content_without_frontmatter);

    // Extract markdown links: [text](path.md) - only .md files
    if let Ok(link_regex) = Regex::new(r"\[([^\]]+)\]\(([^)]+)\)") {
        for cap in link_regex.captures_iter(&content_without_code) {
            if let Some(path) = cap.get(2) {
                let path_str = path.as_str();
                // Only include markdown files
                if path_str.ends_with(".md") && is_valid_file_reference(path_str) {
                    references.push(path_str.to_string());
                }
            }
        }
    }

    // Extract direct file paths with markdown extensions
    // Pattern: paths containing / with .md extension only
    if let Ok(path_regex) = Regex::new(r#"(?:^|\s|["'`])([./a-zA-Z_][\w./-]*\.md)(?:\s|["'`]|$)"#) {
        for cap in path_regex.captures_iter(&content_without_code) {
            if let Some(path) = cap.get(1) {
                let path_str = path.as_str();
                if is_valid_file_reference(path_str) {
                    references.push(path_str.to_string());
                }
            }
        }
    }

    // Deduplicate while preserving order
    let mut seen = std::collections::HashSet::new();
    references.retain(|r| seen.insert(r.clone()));

    references
}

/// Remove YAML frontmatter from markdown content.
///
/// This prevents extracting dependency paths from frontmatter metadata,
/// which are transitive dependencies rather than actual file references in
/// the content.
///
/// # Arguments
///
/// * `content` - The markdown content
///
/// # Returns
///
/// Content with frontmatter removed (--- delimited at the start)
fn remove_frontmatter(content: &str) -> String {
    let parser = FrontmatterParser::new();
    parser.strip_frontmatter(content)
}

/// Remove code blocks from markdown content.
///
/// This helps prevent extracting file paths that appear in code block examples,
/// which should not be validated as actual file references. Inline code (single
/// backticks) is preserved since it may contain legitimate file path references.
///
/// # Arguments
///
/// * `content` - The markdown content
///
/// # Returns
///
/// Content with code blocks removed (``` delimited)
fn remove_code_blocks(content: &str) -> String {
    let mut result = String::new();
    let mut in_code_block = false;
    let mut chars = content.chars().peekable();

    while let Some(ch) = chars.next() {
        // Check for code block delimiter (```)
        if ch == '`' {
            let mut backtick_count = 1;

            // Count consecutive backticks
            while chars.peek() == Some(&'`') {
                backtick_count += 1;
                chars.next();
            }

            // Three or more backticks toggle code block mode
            if backtick_count >= 3 {
                in_code_block = !in_code_block;
                // Replace code block delimiter with spaces
                for _ in 0..backtick_count {
                    result.push(' ');
                }
                continue;
            }
            // It's inline code (1-2 backticks), preserve it
            for _ in 0..backtick_count {
                result.push('`');
            }
            continue;
        }

        // Skip content inside code blocks
        if in_code_block {
            result.push(' '); // Maintain structure with spaces
        } else {
            result.push(ch);
        }
    }

    result
}

/// Check if a path string is a valid file reference to validate.
///
/// This function filters out URLs, absolute paths, and other patterns
/// that should not be validated as local file references.
///
/// # Valid References
///
/// - Relative paths: `./file.md`, `../docs/guide.md`
/// - Dot-prefixed paths: `.agpm/snippets/file.md`
/// - Simple paths: `docs/guide.md`
///
/// # Invalid References (Filtered Out)
///
/// - URLs: `http://example.com`, `https://github.com/...`
/// - Absolute paths: `/usr/local/file.md`
/// - Paths with URL schemes: `file://...`, `ftp://...`
/// - Empty or whitespace-only strings
///
/// # Arguments
///
/// * `path` - The path string to validate
///
/// # Returns
///
/// `true` if the path should be validated, `false` otherwise
#[must_use]
pub fn is_valid_file_reference(path: &str) -> bool {
    let trimmed = path.trim();

    // Skip empty strings
    if trimmed.is_empty() {
        return false;
    }

    // Skip URLs (any scheme://...)
    if trimmed.contains("://") {
        return false;
    }

    // Skip absolute paths
    if trimmed.starts_with('/') {
        return false;
    }

    // Skip anchor links
    if trimmed.starts_with('#') {
        return false;
    }

    // Must have a file extension
    if !trimmed.contains('.') {
        return false;
    }

    // Must contain a path separator (/) to be considered a file path
    // This filters out simple filenames like "example.md" that aren't paths
    if !trimmed.contains('/') {
        return false;
    }

    true
}

/// Validate that file references exist on the filesystem.
///
/// This function takes a list of relative file paths and checks if they
/// exist relative to the given project directory. It returns a list of
/// missing references for error reporting.
///
/// # Arguments
///
/// * `references` - List of relative file paths to validate
/// * `project_dir` - Base directory to resolve relative paths against
///
/// # Returns
///
/// A list of references that were not found
///
/// # Errors
///
/// Returns an error if the project directory cannot be accessed
///
/// # Examples
///
/// ```rust,no_run
/// # use agpm_cli::markdown::reference_extractor::validate_file_references;
/// # use std::path::Path;
/// # fn example() -> anyhow::Result<()> {
/// let references = vec![
///     ".agpm/snippets/existing.md".to_string(),
///     ".agpm/snippets/missing.md".to_string(),
/// ];
///
/// let project_dir = Path::new("/path/to/project");
/// let missing = validate_file_references(&references, project_dir)?;
/// // Returns only the missing.md entry
/// # Ok(())
/// # }
/// ```
pub fn validate_file_references(references: &[String], project_dir: &Path) -> Result<Vec<String>> {
    let mut missing = Vec::new();

    for reference in references {
        let full_path = project_dir.join(reference);

        if !full_path.exists() {
            missing.push(reference.clone());
        }
    }

    Ok(missing)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_extract_markdown_links() {
        let content = r#"
Check the [documentation](./docs/guide.md) for more info.
Also see [examples](../examples/demo.md).
"#;

        let refs = extract_file_references(content);
        assert_eq!(refs.len(), 2);
        assert!(refs.contains(&"./docs/guide.md".to_string()));
        assert!(refs.contains(&"../examples/demo.md".to_string()));
    }

    #[test]
    fn test_extract_direct_file_paths() {
        let content = r#"
See `.agpm/snippets/example.md` for the implementation.
Check `./docs/overview.md` and `.claude/agents/test.md`.
"#;

        let refs = extract_file_references(content);
        assert!(refs.contains(&".agpm/snippets/example.md".to_string()));
        assert!(refs.contains(&".claude/agents/test.md".to_string()));
        assert!(refs.contains(&"./docs/overview.md".to_string()));
    }

    #[test]
    fn test_skip_urls() {
        let content = r#"
Visit [GitHub](https://github.com/user/repo) for source.
Or check http://example.com/page.html.
"#;

        let refs = extract_file_references(content);
        assert_eq!(refs.len(), 0);
    }

    #[test]
    fn test_skip_code_blocks() {
        let content = r#"
Normal reference: `.agpm/snippets/real.md`

```bash
# This should be skipped: `.agpm/snippets/code.md`
cat .agpm/snippets/example.md
```

Another real reference: `docs/guide.md`
"#;

        let refs = extract_file_references(content);
        assert!(refs.contains(&".agpm/snippets/real.md".to_string()));
        assert!(refs.contains(&"docs/guide.md".to_string()));
        // Should not contain references from code block
        assert!(!refs.iter().any(|r| r.contains("code.md")));
    }

    #[test]
    fn test_inline_code_path_extraction() {
        let content = "Check `.agpm/real.md` for details.";

        let refs = extract_file_references(content);
        // File paths in inline code are still extracted if they look like actual paths
        assert!(refs.contains(&".agpm/real.md".to_string()));
    }

    #[test]
    fn test_deduplication() {
        let content = r#"
See `.agpm/snippets/example.md` for details.
Also check `.agpm/snippets/example.md` again.
"#;

        let refs = extract_file_references(content);
        assert_eq!(refs.len(), 1);
    }

    #[test]
    fn test_is_valid_file_reference() {
        // Valid references
        assert!(is_valid_file_reference("./docs/guide.md"));
        assert!(is_valid_file_reference(".agpm/snippets/file.md"));
        assert!(is_valid_file_reference("../parent/file.json"));

        // Invalid references
        assert!(!is_valid_file_reference("https://example.com"));
        assert!(!is_valid_file_reference("http://test.com/file.md"));
        assert!(!is_valid_file_reference("/absolute/path.md"));
        assert!(!is_valid_file_reference("#anchor"));
        assert!(!is_valid_file_reference(""));
        assert!(!is_valid_file_reference("no-extension"));
    }

    #[test]
    fn test_validate_file_references() -> Result<()> {
        let temp_dir = tempdir()?;
        let project_dir = temp_dir.path();

        // Create some test files
        let existing_dir = project_dir.join(".agpm").join("snippets");
        fs::create_dir_all(&existing_dir)?;
        fs::write(existing_dir.join("existing.md"), "content")?;

        let references = vec![
            ".agpm/snippets/existing.md".to_string(),
            ".agpm/snippets/missing.md".to_string(),
            "nonexistent/file.md".to_string(),
        ];

        let missing = validate_file_references(&references, project_dir)?;

        assert_eq!(missing.len(), 2);
        assert!(missing.contains(&".agpm/snippets/missing.md".to_string()));
        assert!(missing.contains(&"nonexistent/file.md".to_string()));
        assert!(!missing.contains(&".agpm/snippets/existing.md".to_string()));

        Ok(())
    }

    #[test]
    fn test_remove_code_blocks() {
        let content = r#"
Normal text with `.agpm/file.md`

```rust
let path = ".agpm/in_code.md";
```

More normal text `.agpm/another.md`
"#;

        let cleaned = remove_code_blocks(content);
        assert!(cleaned.contains(".agpm/file.md"));
        assert!(cleaned.contains(".agpm/another.md"));
        // Code block content should be replaced with spaces
        assert!(
            !cleaned.contains("in_code.md")
                || cleaned.split_whitespace().all(|word| !word.contains("in_code.md"))
        );
    }

    #[test]
    fn test_remove_frontmatter() {
        let content = r#"---
dependencies:
  agents:
    - path: agents/helper.md
  snippets:
    - path: snippets/utils.md
---

# Main Content

See [documentation](./docs/guide.md) for details.
"#;

        let cleaned = remove_frontmatter(content);
        // Frontmatter should be removed
        assert!(!cleaned.contains("dependencies:"));
        assert!(!cleaned.contains("agents/helper.md"));
        assert!(!cleaned.contains("snippets/utils.md"));
        // Content should remain
        assert!(cleaned.contains("# Main Content"));
        assert!(cleaned.contains("./docs/guide.md"));
    }

    #[test]
    fn test_extract_with_frontmatter_dependencies() {
        let content = r#"---
dependencies:
  agents:
    - path: agents/helper.md
      version: v1.0.0
  snippets:
    - path: .agpm/snippets/utils.md
---

# Command

See [real reference](./docs/guide.md) for details.
Check `.claude/agents/example.md` for the implementation.
"#;

        let refs = extract_file_references(content);

        // Should extract content references
        assert!(refs.contains(&"./docs/guide.md".to_string()));
        assert!(refs.contains(&".claude/agents/example.md".to_string()));

        // Should NOT extract frontmatter dependencies
        assert!(!refs.contains(&"agents/helper.md".to_string()));
        assert!(!refs.contains(&".agpm/snippets/utils.md".to_string()));
    }

    #[test]
    fn test_complex_markdown_with_mixed_references() {
        let content = r#"
# Documentation

See the [main guide](./docs/guide.md) for details.

## Implementation

The core logic is in `.agpm/snippets/core.md` file.

```rust
// This code reference should be ignored
let path = ".agpm/snippets/ignored.md";
```

Also check:
- [Examples](../examples/demo.md)
- External: https://github.com/user/repo
- `.claude/agents/helper.md`

Inline code like `example.md` should be skipped.
"#;

        let refs = extract_file_references(content);

        // Should extract these
        assert!(refs.contains(&"./docs/guide.md".to_string()));
        assert!(refs.contains(&".agpm/snippets/core.md".to_string()));
        assert!(refs.contains(&"../examples/demo.md".to_string()));
        assert!(refs.contains(&".claude/agents/helper.md".to_string()));

        // Should NOT extract these
        assert!(!refs.iter().any(|r| r.contains("github.com")));
        assert!(!refs.iter().any(|r| r.contains("ignored.md")));
        assert!(!refs.contains(&"example.md".to_string())); // Was in inline code
    }
}