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
//! Template rendering and file reference validation.

use anyhow::Result;
use colored::Colorize;
use std::path::Path;
use std::sync::Arc;

use crate::cache::Cache;
use crate::cli::common::CommandContext;
use crate::core::ResourceType;
use crate::markdown::reference_extractor::{extract_file_references, validate_file_references};
use crate::templating::{RenderingMetadata, TemplateContextBuilder, TemplateRenderer};

use super::{OutputFormat, ValidationContext};

/// Validates template rendering and file references in markdown resources.
///
/// This function performs two validations:
/// 1. **Template Rendering**: Checks that all markdown resources can be successfully
///    rendered with their template syntax.
/// 2. **File References**: Validates that all file references within markdown content
///    point to existing files.
///
/// Requires a lockfile to build the template context.
///
/// # Arguments
///
/// * `ctx` - Validation context containing all necessary parameters
/// * `project_dir` - Path to the project directory
///
/// # Returns
///
/// Returns `Ok(())` if all templates render successfully and file references are valid,
/// or `Err` if validation fails.
pub async fn validate_templates(ctx: &mut ValidationContext<'_>, project_dir: &Path) -> Result<()> {
    ctx.print_verbose("\n🔍 Validating template rendering...");

    // Load lockfile - required for template context
    let lockfile_path = project_dir.join("agpm.lock");

    if !lockfile_path.exists() {
        let error_msg = "Lockfile required for template rendering (run 'agpm install' first)";
        ctx.errors.push(error_msg.to_string());

        if matches!(ctx.format, OutputFormat::Json) {
            ctx.validation_results.valid = false;
            ctx.validation_results.errors = ctx.errors.clone();
            ctx.validation_results.warnings = ctx.warnings.to_owned();
            println!("{}", serde_json::to_string_pretty(&ctx.validation_results)?);
            return Err(anyhow::anyhow!("{}", error_msg));
        } else {
            ctx.print(&format!("{} {}", "".red(), error_msg));
        }
        return Err(anyhow::anyhow!("{}", error_msg));
    }

    // Create command context for enhanced lockfile loading
    let command_context = CommandContext::new(ctx.manifest.clone(), project_dir.to_path_buf())?;

    // Use enhanced lockfile loading with automatic regeneration
    let lockfile = match command_context.load_lockfile_with_regeneration(true, "validate")? {
        Some(lockfile) => Arc::new(lockfile),
        None => {
            return Err(anyhow::anyhow!(
                "Lockfile was invalid and has been removed. Run 'agpm install' to regenerate it first."
            ));
        }
    };
    let cache = Arc::new(Cache::new()?);

    // Load global config for template rendering settings
    let global_config = crate::config::GlobalConfig::load().await.unwrap_or_default();
    let max_content_file_size = Some(global_config.max_content_file_size);

    // Collect all markdown resources from manifest
    let mut template_results = Vec::new();
    let mut templates_found = 0;
    let mut templates_rendered = 0;

    // Helper macro to validate template rendering
    macro_rules! validate_resource_template {
        ($name:expr, $entry:expr, $resource_type:expr) => {{
            // Read the resource content
            let content = if $entry.source.is_some() && $entry.resolved_commit.is_some() {
                // Git resource - read from worktree
                // Safe: checked is_some() above in the if condition
                let source_name = $entry.source.as_ref().unwrap();
                // Safe: checked is_some() above in the if condition
                let sha = $entry.resolved_commit.as_ref().unwrap();
                let url = match $entry.url.as_ref() {
                    Some(u) => u,
                    None => {
                        template_results.push(format!("{}: Missing URL for Git resource", $name));
                        continue;
                    }
                };

                let cache_dir = match cache
                    .get_or_create_worktree_for_sha(source_name, url, sha, Some($name))
                    .await
                {
                    Ok(dir) => dir,
                    Err(e) => {
                        template_results.push(format!("{}: {}", $name, e));
                        continue;
                    }
                };

                let source_path = cache_dir.join(&$entry.path);
                match tokio::fs::read_to_string(&source_path).await {
                    Ok(c) => c,
                    Err(e) => {
                        template_results.push(format!(
                            "{}: Failed to read file '{}': {}",
                            $name,
                            source_path.display(),
                            e
                        ));
                        continue;
                    }
                }
            } else {
                // Local resource - read from project directory
                let source_path = {
                    let candidate = Path::new(&$entry.path);
                    if candidate.is_absolute() {
                        candidate.to_path_buf()
                    } else {
                        project_dir.join(candidate)
                    }
                };

                match tokio::fs::read_to_string(&source_path).await {
                    Ok(c) => c,
                    Err(e) => {
                        template_results.push(format!(
                            "{}: Failed to read file '{}': {}",
                            $name,
                            source_path.display(),
                            e
                        ));
                        continue;
                    }
                }
            };

            // Check if it contains template syntax
            let has_template_syntax =
                content.contains("{{") || content.contains("{%") || content.contains("{#");

            if !has_template_syntax {
                continue; // Not a template
            }

            templates_found += 1;

            // Build template context
            let project_config = ctx.manifest.project.clone();
            let context_builder = TemplateContextBuilder::new(
                Arc::clone(&lockfile),
                project_config,
                Arc::clone(&cache),
                project_dir.to_path_buf(),
            );
            // Use canonical name from lockfile entry, not manifest key
            let resource_id = crate::lockfile::ResourceId::new(
                $entry.name.clone(),
                $entry.source.clone(),
                $entry.tool.clone(),
                $resource_type,
                $entry.variant_inputs.hash().to_string(),
            );
            let context = match context_builder
                .build_context(&resource_id, $entry.variant_inputs.json())
                .await
            {
                Ok((c, _checksum)) => c,
                Err(e) => {
                    template_results.push(format!("{}: {}", $name, e));
                    continue;
                }
            };

            // Try to render
            let mut renderer =
                match TemplateRenderer::new(true, project_dir.to_path_buf(), max_content_file_size)
                {
                    Ok(r) => r,
                    Err(e) => {
                        template_results.push(format!("{}: {}", $name, e));
                        continue;
                    }
                };

            // Create rendering metadata for better error messages
            let rendering_metadata = RenderingMetadata {
                resource_name: $entry.name.clone(),
                resource_type: $resource_type,
                dependency_chain: vec![], // Could be enhanced to include parent info
                source_path: Some($entry.path.clone().into()),
                depth: 0,
            };

            match renderer.render_template(&content, &context, Some(&rendering_metadata)) {
                Ok(_) => {
                    templates_rendered += 1;
                }
                Err(e) => {
                    template_results.push(format!("{}: {}", $name, e));
                }
            }
        }};
    }

    // Process each resource type
    // Use manifest_alias (if present) when matching manifest keys to lockfile entries
    for resource_type in
        &[ResourceType::Agent, ResourceType::Snippet, ResourceType::Command, ResourceType::Script]
    {
        let manifest_resources = ctx.manifest.get_resources(resource_type);
        let lockfile_resources = lockfile.get_resources(resource_type);

        for name in manifest_resources.keys() {
            if let Some(entry) = lockfile_resources
                .iter()
                .find(|e| e.manifest_alias.as_ref().unwrap_or(&e.name) == name)
            {
                validate_resource_template!(name, entry, *resource_type);
            }
        }
    }

    // Update validation results
    ctx.validation_results.templates_total = templates_found;
    ctx.validation_results.templates_rendered = templates_rendered;
    ctx.validation_results.templates_valid = template_results.is_empty();

    // Report results (only for text output, not JSON)
    if template_results.is_empty() {
        if templates_found > 0 {
            if !ctx.quiet && *ctx.format == OutputFormat::Text {
                println!("✓ All {} templates rendered successfully", templates_found);
            }
        } else if !ctx.quiet && *ctx.format == OutputFormat::Text {
            println!("⚠ No templates found in resources");
        }
    } else {
        let error_msg =
            format!("Template rendering failed for {} resource(s)", template_results.len());
        ctx.errors.push(error_msg.clone());

        if matches!(ctx.format, OutputFormat::Json) {
            ctx.validation_results.valid = false;
            ctx.validation_results.errors.extend(template_results);
            ctx.validation_results.errors.push(error_msg);
            ctx.validation_results.warnings = ctx.warnings.to_owned();
            println!("{}", serde_json::to_string_pretty(&ctx.validation_results)?);
            return Err(anyhow::anyhow!("Template rendering failed"));
        } else if !ctx.quiet {
            println!("{} {}", "".red(), error_msg);
            for error in &template_results {
                println!("  {}", error);
            }
        }
        return Err(anyhow::anyhow!("Template rendering failed"));
    }

    // Validate file references in markdown content
    if ctx.verbose && !ctx.quiet {
        println!("\n🔍 Validating file references in markdown content...");
    }

    let mut file_reference_errors = Vec::new();
    let mut total_references_checked = 0;

    // Helper macro to validate file references in markdown resources
    macro_rules! validate_file_references_in_resource {
        ($name:expr, $entry:expr) => {{
            // Read the resource content
            let content = if $entry.source.is_some() && $entry.resolved_commit.is_some() {
                // Git resource - read from worktree
                // Safe: checked is_some() above in the if condition
                let source_name = $entry.source.as_ref().unwrap();
                // Safe: checked is_some() above in the if condition
                let sha = $entry.resolved_commit.as_ref().unwrap();
                let url = match $entry.url.as_ref() {
                    Some(u) => u,
                    None => {
                        continue;
                    }
                };

                let cache_dir = match cache
                    .get_or_create_worktree_for_sha(source_name, url, sha, Some($name))
                    .await
                {
                    Ok(dir) => dir,
                    Err(_) => {
                        continue;
                    }
                };

                let source_path = cache_dir.join(&$entry.path);
                match tokio::fs::read_to_string(&source_path).await {
                    Ok(c) => c,
                    Err(e) => {
                        tracing::debug!(
                            "Failed to read source file '{}' for reference validation: {}",
                            source_path.display(),
                            e
                        );
                        continue;
                    }
                }
            } else {
                // Local resource - read from installed location
                let installed_path = project_dir.join(&$entry.installed_at);

                match tokio::fs::read_to_string(&installed_path).await {
                    Ok(c) => c,
                    Err(e) => {
                        tracing::debug!(
                            "Failed to read installed file '{}' for reference validation: {}",
                            installed_path.display(),
                            e
                        );
                        continue;
                    }
                }
            };

            // Extract file references from markdown content
            let references = extract_file_references(&content);

            if !references.is_empty() {
                total_references_checked += references.len();

                // Validate each reference exists
                match validate_file_references(&references, project_dir) {
                    Ok(missing) => {
                        for missing_ref in missing {
                            file_reference_errors.push(format!(
                                "{}: references non-existent file '{}'",
                                $entry.installed_at, missing_ref
                            ));
                        }
                    }
                    Err(e) => {
                        file_reference_errors.push(format!(
                            "{}: failed to validate references: {}",
                            $entry.installed_at, e
                        ));
                    }
                }
            }
        }};
    }

    // Process each markdown resource type from lockfile
    for entry in &lockfile.agents {
        validate_file_references_in_resource!(&entry.name, entry);
    }

    for entry in &lockfile.snippets {
        validate_file_references_in_resource!(&entry.name, entry);
    }

    for entry in &lockfile.commands {
        validate_file_references_in_resource!(&entry.name, entry);
    }

    for entry in &lockfile.scripts {
        validate_file_references_in_resource!(&entry.name, entry);
    }

    // Report file reference validation results
    if file_reference_errors.is_empty() {
        if total_references_checked > 0 {
            if !ctx.quiet && *ctx.format == OutputFormat::Text {
                println!(
                    "✓ All {} file references validated successfully",
                    total_references_checked
                );
            }
        } else if ctx.verbose && !ctx.quiet && *ctx.format == OutputFormat::Text {
            println!("⚠ No file references found in resources");
        }
        Ok(())
    } else {
        let error_msg = format!(
            "File reference validation failed: {} broken reference(s) found",
            file_reference_errors.len()
        );
        ctx.errors.push(error_msg.clone());

        if matches!(ctx.format, OutputFormat::Json) {
            ctx.validation_results.valid = false;
            ctx.validation_results.errors.extend(file_reference_errors);
            ctx.validation_results.errors.push(error_msg);
            ctx.validation_results.warnings = ctx.warnings.to_owned();
            println!("{}", serde_json::to_string_pretty(&ctx.validation_results)?);
            return Err(anyhow::anyhow!("File reference validation failed"));
        } else if !ctx.quiet {
            println!("{} {}", "".red(), error_msg);
            for error in &file_reference_errors {
                println!("  {}", error);
            }
        }
        Err(anyhow::anyhow!("File reference validation failed"))
    }
}