agpm-cli 0.4.10

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
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
//! Dependency extraction functionality for templates.
//!
//! This module provides methods for extracting custom dependency names and
//! dependency specifications from resource files.

use crate::core::file_error::{FileOperation, FileResultExt};
use anyhow::{Result, bail};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;

use crate::core::ResourceType;
use crate::lockfile::lockfile_dependency_ref::LockfileDependencyRef;
use crate::lockfile::{LockFile, LockedResource, ResourceId};

use crate::templating::cache::RenderCache;
use crate::templating::content::ContentExtractor;

/// Helper function to create a LockfileDependencyRef string from a resource.
///
/// This centralizes the logic for creating dependency references based on whether
/// the resource has a source (Git) or is local.
pub(crate) fn create_dependency_ref_string(
    source: Option<String>,
    resource_type: ResourceType,
    name: String,
    version: Option<String>,
) -> String {
    if let Some(source) = source {
        LockfileDependencyRef::git(source, resource_type, name, version).to_string()
    } else {
        LockfileDependencyRef::local(resource_type, name, version).to_string()
    }
}

/// Trait for dependency extraction methods on TemplateContextBuilder.
pub(crate) trait DependencyExtractor: ContentExtractor {
    /// Get the lockfile
    fn lockfile(&self) -> &Arc<LockFile>;

    /// Get the render cache
    fn render_cache(&self) -> &Arc<std::sync::Mutex<RenderCache>>;

    /// Get the custom names cache
    fn custom_names_cache(
        &self,
    ) -> &Arc<std::sync::Mutex<HashMap<String, BTreeMap<String, String>>>>;

    /// Get the dependency specs cache
    fn dependency_specs_cache(
        &self,
    ) -> &Arc<std::sync::Mutex<HashMap<String, BTreeMap<String, crate::manifest::DependencySpec>>>>;

    /// Extract custom dependency names from a resource's frontmatter.
    ///
    /// Parses the resource file to extract the `dependencies` declaration with `name:` fields
    /// and maps dependency references to their custom names.
    ///
    /// # Returns
    ///
    /// A BTreeMap mapping dependency references (e.g., "snippet/rust-best-practices") to custom
    /// names (e.g., "best_practices") as declared in the resource's YAML frontmatter.
    /// BTreeMap ensures deterministic iteration order for consistent context checksums.
    ///
    /// # Errors
    ///
    /// Returns an error if the dependency file cannot be read or parsed.
    async fn extract_dependency_custom_names(
        &self,
        resource: &LockedResource,
    ) -> Result<BTreeMap<String, String>> {
        // Build cache key from resource name and type
        let cache_key = format!("{}@{:?}", resource.name, resource.resource_type);

        // Check cache first
        if let Ok(cache) = self.custom_names_cache().lock() {
            if let Some(cached_names) = cache.get(&cache_key) {
                tracing::debug!(
                    "Custom names cache HIT for '{}' ({} names)",
                    resource.name,
                    cached_names.len()
                );
                return Ok(cached_names.clone());
            }
        }

        tracing::debug!("Custom names cache MISS for '{}'", resource.name);

        let mut custom_names = BTreeMap::new();

        // Build a lookup structure upfront to avoid O(n³) nested loops
        // Map: type -> Vec<(basename, full_dep_ref)>
        // Use BTreeMap for deterministic iteration order
        let mut lockfile_lookup: BTreeMap<String, Vec<(String, String)>> = BTreeMap::new();

        // Use parsed_dependencies() helper to parse all dependencies
        for dep_ref in resource.parsed_dependencies() {
            let lockfile_type = dep_ref.resource_type.to_string();
            let lockfile_name = &dep_ref.path;
            let lockfile_dep_ref = dep_ref.to_string();

            // Extract basename from lockfile name
            let lockfile_basename = std::path::Path::new(lockfile_name)
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or(lockfile_name)
                .to_string();

            lockfile_lookup
                .entry(lockfile_type)
                .or_default()
                .push((lockfile_basename, lockfile_dep_ref));
        }

        // Determine source path (same logic as extract_content)
        let source_path = if let Some(_source_name) = &resource.source {
            // Has source - check if local or Git
            let url = match resource.url.as_ref() {
                Some(u) => u,
                None => bail!("Resource '{}' has source but no URL", resource.name),
            };

            let is_local_source = resource.resolved_commit.as_deref().is_none_or(str::is_empty);

            if is_local_source {
                // Local source
                std::path::PathBuf::from(url).join(&resource.path)
            } else {
                // Git source
                let sha = match resource.resolved_commit.as_deref() {
                    Some(s) => s,
                    None => bail!("Resource '{}' has no resolved commit", resource.name),
                };
                match self.cache().get_worktree_path(url, sha) {
                    Ok(worktree_dir) => worktree_dir.join(&resource.path),
                    Err(e) => {
                        bail!("Failed to get worktree path for resource '{}': {}", resource.name, e)
                    }
                }
            }
        } else {
            // Local file
            let local_path = std::path::Path::new(&resource.path);
            if local_path.is_absolute() {
                local_path.to_path_buf()
            } else {
                self.project_dir().join(local_path)
            }
        };

        // Read and parse the file based on type
        if resource.path.ends_with(".md") {
            // Parse markdown frontmatter with template rendering
            let content = tokio::fs::read_to_string(&source_path).await.with_file_context(
                FileOperation::Read,
                &source_path,
                "reading markdown dependency file",
                "templating_dependencies",
            )?;

            // Use templated parsing to handle conditional blocks ({% if %}) in frontmatter
            if let Ok(doc) = crate::markdown::MarkdownDocument::parse_with_templating(
                &content,
                Some(resource.variant_inputs.json()),
                Some(&source_path),
            ) {
                // Extract dependencies from parsed metadata
                if let Some(markdown_metadata) = &doc.metadata {
                    // Convert MarkdownMetadata to DependencyMetadata
                    // Merge both root-level dependencies and agpm.dependencies
                    let dependency_metadata = crate::manifest::DependencyMetadata::new(
                        markdown_metadata.dependencies.clone(),
                        markdown_metadata.get_agpm_metadata(),
                    );

                    if let Some(deps_map) = dependency_metadata.get_dependencies() {
                        // Process each resource type (agents, snippets, commands, etc.)
                        for (resource_type_str, deps_array) in deps_map {
                            // Convert frontmatter type to lockfile type (singular)
                            let lockfile_type: String = match resource_type_str.as_str() {
                                "agents" | "agent" => "agent".to_string(),
                                "snippets" | "snippet" => "snippet".to_string(),
                                "commands" | "command" => "command".to_string(),
                                "scripts" | "script" => "script".to_string(),
                                "hooks" | "hook" => "hook".to_string(),
                                "mcp-servers" | "mcp-server" => "mcp-server".to_string(),
                                _ => continue, // Skip unknown types
                            };

                            // Get lockfile entries for this type only (O(1) lookup instead of O(n) iteration)
                            let type_entries = match lockfile_lookup.get(&lockfile_type) {
                                Some(entries) => entries,
                                None => continue, // No lockfile deps of this type
                            };

                            // deps_array is Vec<DependencySpec>
                            for dep_spec in deps_array {
                                let path = &dep_spec.path;
                                if let Some(custom_name) = &dep_spec.name {
                                    // Extract basename from the path (without extension)
                                    let basename = std::path::Path::new(path)
                                        .file_stem()
                                        .and_then(|s| s.to_str())
                                        .unwrap_or(path);

                                    tracing::info!(
                                        "Found custom name '{}' for path '{}' (basename: '{}')",
                                        custom_name,
                                        path,
                                        basename
                                    );

                                    // Check if basename has template variables
                                    if basename.contains("{{") {
                                        // Template variable in basename - try suffix matching
                                        // e.g., "{{ agpm.project.language }}-best-practices" -> "-best-practices"
                                        if let Some(static_suffix_start) = basename.find("}}") {
                                            let static_suffix =
                                                &basename[static_suffix_start + 2..];

                                            // Search for any lockfile basename ending with this suffix
                                            for (lockfile_basename, lockfile_dep_ref) in
                                                type_entries
                                            {
                                                if lockfile_basename.ends_with(static_suffix) {
                                                    custom_names.insert(
                                                        lockfile_dep_ref.clone(),
                                                        custom_name.to_string(),
                                                    );
                                                }
                                            }
                                        }
                                    } else {
                                        // No template variables - exact basename match (O(n) but only within type)
                                        for (lockfile_basename, lockfile_dep_ref) in type_entries {
                                            if lockfile_basename == basename {
                                                custom_names.insert(
                                                    lockfile_dep_ref.clone(),
                                                    custom_name.to_string(),
                                                );
                                                break; // Found exact match, no need to continue
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if resource.path.ends_with(".json") {
            // Parse JSON dependencies field with template rendering
            let content = tokio::fs::read_to_string(&source_path).await.with_file_context(
                FileOperation::Read,
                &source_path,
                "reading JSON dependency file",
                "templating_dependencies",
            )?;

            // Apply templating to JSON content to handle conditional blocks
            let mut parser = crate::markdown::frontmatter::FrontmatterParser::new();
            let templated_content = parser
                .apply_templating(&content, Some(resource.variant_inputs.json()), &source_path)
                .unwrap_or_else(|_| content.clone());

            // Parse JSON and extract dependencies field
            if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(&templated_content) {
                // Extract both root-level dependencies and agpm.dependencies
                let root_deps = json_value.get("dependencies").and_then(|v| {
                    serde_json::from_value::<
                        BTreeMap<String, Vec<crate::manifest::DependencySpec>>,
                    >(v.clone())
                    .ok()
                });

                let agpm_metadata = json_value.get("agpm").and_then(|v| {
                    serde_json::from_value::<crate::manifest::dependency_spec::AgpmMetadata>(
                        v.clone(),
                    )
                    .ok()
                });

                // Merge both dependency sources
                let dependency_metadata =
                    crate::manifest::DependencyMetadata::new(root_deps, agpm_metadata);

                if let Some(deps_map) = dependency_metadata.get_dependencies() {
                    // Process each resource type (agents, snippets, commands, etc.)
                    for (resource_type_str, deps_array) in deps_map {
                        // Convert frontmatter type to lockfile type (singular)
                        let lockfile_type: String = match resource_type_str.as_str() {
                            "agents" | "agent" => "agent".to_string(),
                            "snippets" | "snippet" => "snippet".to_string(),
                            "commands" | "command" => "command".to_string(),
                            "scripts" | "script" => "script".to_string(),
                            "hooks" | "hook" => "hook".to_string(),
                            "mcp-servers" | "mcp-server" => "mcp-server".to_string(),
                            _ => continue, // Skip unknown types
                        };

                        // Get lockfile entries for this type only (O(1) lookup instead of O(n) iteration)
                        let type_entries = match lockfile_lookup.get(&lockfile_type) {
                            Some(entries) => entries,
                            None => continue, // No lockfile deps of this type
                        };

                        // deps_array is Vec<DependencySpec>
                        for dep_spec in deps_array {
                            let path = &dep_spec.path;
                            if let Some(custom_name) = &dep_spec.name {
                                // Extract basename from the path (without extension)
                                let basename = std::path::Path::new(path)
                                    .file_stem()
                                    .and_then(|s| s.to_str())
                                    .unwrap_or(path);

                                tracing::info!(
                                    "Found custom name '{}' for path '{}' (basename: '{}') from JSON",
                                    custom_name,
                                    path,
                                    basename
                                );

                                // Check if basename has template variables
                                if basename.contains("{{") {
                                    // Template variable in basename - try suffix matching
                                    // e.g., "{{ agpm.project.language }}-best-practices" -> "-best-practices"
                                    if let Some(static_suffix_start) = basename.find("}}") {
                                        let static_suffix = &basename[static_suffix_start + 2..];

                                        // Search for any lockfile basename ending with this suffix
                                        for (lockfile_basename, lockfile_dep_ref) in type_entries {
                                            if lockfile_basename.ends_with(static_suffix) {
                                                custom_names.insert(
                                                    lockfile_dep_ref.clone(),
                                                    custom_name.to_string(),
                                                );
                                            }
                                        }
                                    }
                                } else {
                                    // No template variables - exact basename match (O(n) but only within type)
                                    for (lockfile_basename, lockfile_dep_ref) in type_entries {
                                        if lockfile_basename == basename {
                                            custom_names.insert(
                                                lockfile_dep_ref.clone(),
                                                custom_name.to_string(),
                                            );
                                            break; // Found exact match, no need to continue
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Store in cache before returning
        if let Ok(mut cache) = self.custom_names_cache().lock() {
            cache.insert(cache_key, custom_names.clone());
            tracing::debug!(
                "Stored {} custom names in cache for '{}'",
                custom_names.len(),
                resource.name
            );
        }

        Ok(custom_names)
    }

    /// Extract full dependency specifications from a resource's frontmatter.
    ///
    /// Parses the resource file to extract complete DependencySpec objects including
    /// tool, name, flatten, and install fields. This information is used to build
    /// complete ResourceIds for dependency lookups.
    ///
    /// # Returns
    ///
    /// A BTreeMap mapping dependency references (e.g., "snippet:snippets/commands/commit")
    /// to their full DependencySpec objects. BTreeMap ensures deterministic iteration.
    ///
    /// # Errors
    ///
    /// Returns an error if the dependency file cannot be read or parsed.
    async fn extract_dependency_specs(
        &self,
        resource: &LockedResource,
    ) -> Result<BTreeMap<String, crate::manifest::DependencySpec>> {
        // Build cache key from resource name and type
        let cache_key = format!("{}@{:?}", resource.name, resource.resource_type);

        // Check cache first
        if let Ok(cache) = self.dependency_specs_cache().lock() {
            if let Some(cached_specs) = cache.get(&cache_key) {
                tracing::debug!(
                    "Dependency specs cache HIT for '{}' ({} specs)",
                    resource.name,
                    cached_specs.len()
                );
                return Ok(cached_specs.clone());
            }
        }

        tracing::debug!("Dependency specs cache MISS for '{}'", resource.name);

        let mut dependency_specs = BTreeMap::new();

        // Determine source path (same logic as extract_content)
        let source_path = if let Some(_source_name) = &resource.source {
            // Has source - check if local or Git
            let url = match resource.url.as_ref() {
                Some(u) => u,
                None => bail!("Resource '{}' has source but no URL", resource.name),
            };

            let is_local_source = resource.resolved_commit.as_deref().is_none_or(str::is_empty);

            if is_local_source {
                // Local source
                std::path::PathBuf::from(url).join(&resource.path)
            } else {
                // Git source
                let sha = match resource.resolved_commit.as_deref() {
                    Some(s) => s,
                    None => bail!("Resource '{}' has no resolved commit", resource.name),
                };
                match self.cache().get_worktree_path(url, sha) {
                    Ok(worktree_dir) => worktree_dir.join(&resource.path),
                    Err(e) => {
                        bail!("Failed to get worktree path for resource '{}': {}", resource.name, e)
                    }
                }
            }
        } else {
            // Local file
            let local_path = std::path::Path::new(&resource.path);
            if local_path.is_absolute() {
                local_path.to_path_buf()
            } else {
                self.project_dir().join(local_path)
            }
        };

        // Read and parse the file based on type
        if resource.path.ends_with(".md") {
            // Parse markdown frontmatter with template rendering
            let content = tokio::fs::read_to_string(&source_path).await.with_file_context(
                FileOperation::Read,
                &source_path,
                "reading markdown dependency file",
                "templating_dependencies",
            )?;

            // Use templated parsing to handle conditional blocks ({% if %}) in frontmatter
            if let Ok(doc) = crate::markdown::MarkdownDocument::parse_with_templating(
                &content,
                Some(resource.variant_inputs.json()),
                Some(&source_path),
            ) {
                // Extract dependencies from parsed metadata
                if let Some(markdown_metadata) = &doc.metadata {
                    // Convert MarkdownMetadata to DependencyMetadata
                    let dependency_metadata = crate::manifest::DependencyMetadata::new(
                        markdown_metadata.dependencies.clone(),
                        markdown_metadata.get_agpm_metadata(),
                    );

                    if let Some(deps_map) = dependency_metadata.get_dependencies() {
                        // Process each resource type
                        for (resource_type_str, deps_array) in deps_map {
                            // Convert frontmatter type to ResourceType
                            let resource_type = match resource_type_str.as_str() {
                                "agents" | "agent" => crate::core::ResourceType::Agent,
                                "snippets" | "snippet" => crate::core::ResourceType::Snippet,
                                "commands" | "command" => crate::core::ResourceType::Command,
                                "scripts" | "script" => crate::core::ResourceType::Script,
                                "hooks" | "hook" => crate::core::ResourceType::Hook,
                                "mcp-servers" | "mcp-server" => {
                                    crate::core::ResourceType::McpServer
                                }
                                _ => continue,
                            };

                            // Store each DependencySpec with its lockfile reference as key
                            for dep_spec in deps_array {
                                // Canonicalize the frontmatter path to match lockfile format
                                // Frontmatter paths are relative to the resource file itself
                                // We need to resolve them relative to source root (not filesystem paths!)
                                let canonical_path = if dep_spec.path.starts_with("../")
                                    || dep_spec.path.starts_with("./")
                                {
                                    // Relative path - resolve using source-relative paths, not filesystem paths
                                    // Get the parent directory of the resource within the source
                                    let resource_parent = std::path::Path::new(&resource.path)
                                        .parent()
                                        .unwrap_or_else(|| std::path::Path::new(""));

                                    // Join with the relative dependency path (still may have ..)
                                    let joined = resource_parent.join(&dep_spec.path);

                                    // Normalize to remove .. and . components, then format for storage
                                    let normalized = crate::utils::normalize_path(&joined);
                                    crate::utils::normalize_path_for_storage(&normalized)
                                } else {
                                    // Absolute or already canonical
                                    dep_spec.path.clone()
                                };

                                // Remove extension to match lockfile format
                                let normalized_path = std::path::Path::new(&canonical_path)
                                    .with_extension("")
                                    .to_string_lossy()
                                    .to_string();

                                // Build the dependency reference string
                                let dep_ref = if let Some(ref src) = resource.source {
                                    LockfileDependencyRef::git(
                                        src.clone(),
                                        resource_type,
                                        normalized_path,
                                        resource.version.clone(),
                                    )
                                    .to_string()
                                } else {
                                    LockfileDependencyRef::local(
                                        resource_type,
                                        normalized_path,
                                        resource.version.clone(),
                                    )
                                    .to_string()
                                };

                                dependency_specs.insert(dep_ref, dep_spec.clone());
                            }
                        }
                    }
                }
            }
        } else if resource.path.ends_with(".json") {
            // Parse JSON dependencies field with template rendering
            let content = tokio::fs::read_to_string(&source_path).await.with_file_context(
                FileOperation::Read,
                &source_path,
                "reading JSON dependency file",
                "templating_dependencies",
            )?;

            // Apply templating to JSON content to handle conditional blocks
            let mut parser = crate::markdown::frontmatter::FrontmatterParser::new();
            let templated_content = parser
                .apply_templating(&content, Some(resource.variant_inputs.json()), &source_path)
                .unwrap_or_else(|_| content.clone());

            if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(&templated_content) {
                // Extract both root-level dependencies and agpm.dependencies
                let root_deps = json_value.get("dependencies").and_then(|v| {
                    serde_json::from_value::<
                        BTreeMap<String, Vec<crate::manifest::DependencySpec>>,
                    >(v.clone())
                    .ok()
                });

                let agpm_metadata = json_value.get("agpm").and_then(|v| {
                    serde_json::from_value::<crate::manifest::dependency_spec::AgpmMetadata>(
                        v.clone(),
                    )
                    .ok()
                });

                // Merge both dependency sources
                let dependency_metadata =
                    crate::manifest::DependencyMetadata::new(root_deps, agpm_metadata);

                if let Some(deps_map) = dependency_metadata.get_dependencies() {
                    // Process each resource type
                    for (resource_type_str, deps_array) in deps_map {
                        // Convert frontmatter type to ResourceType
                        let resource_type = match resource_type_str.as_str() {
                            "agents" | "agent" => crate::core::ResourceType::Agent,
                            "snippets" | "snippet" => crate::core::ResourceType::Snippet,
                            "commands" | "command" => crate::core::ResourceType::Command,
                            "scripts" | "script" => crate::core::ResourceType::Script,
                            "hooks" | "hook" => crate::core::ResourceType::Hook,
                            "mcp-servers" | "mcp-server" => crate::core::ResourceType::McpServer,
                            _ => continue,
                        };

                        // Store each DependencySpec with its lockfile reference as key
                        for dep_spec in deps_array {
                            // Canonicalize the frontmatter path to match lockfile format
                            // Frontmatter paths are relative to the resource file itself
                            // We need to resolve them relative to source root (not filesystem paths!)
                            let canonical_path = if dep_spec.path.starts_with("../")
                                || dep_spec.path.starts_with("./")
                            {
                                // Relative path - resolve using source-relative paths, not filesystem paths
                                // Get the parent directory of the resource within the source
                                let resource_parent = std::path::Path::new(&resource.path)
                                    .parent()
                                    .unwrap_or_else(|| std::path::Path::new(""));

                                // Join with the relative dependency path (still may have ..)
                                let joined = resource_parent.join(&dep_spec.path);

                                // Normalize to remove .. and . components, then format for storage
                                let normalized = crate::utils::normalize_path(&joined);
                                crate::utils::normalize_path_for_storage(&normalized)
                            } else {
                                // Absolute or already canonical
                                dep_spec.path.clone()
                            };

                            // Remove extension to match lockfile format
                            let normalized_path = std::path::Path::new(&canonical_path)
                                .with_extension("")
                                .to_string_lossy()
                                .to_string();

                            // Build the dependency reference string
                            let dep_ref = if let Some(ref src) = resource.source {
                                LockfileDependencyRef::git(
                                    src.clone(),
                                    resource_type,
                                    normalized_path,
                                    resource.version.clone(),
                                )
                                .to_string()
                            } else {
                                LockfileDependencyRef::local(
                                    resource_type,
                                    normalized_path,
                                    resource.version.clone(),
                                )
                                .to_string()
                            };

                            dependency_specs.insert(dep_ref, dep_spec.clone());
                        }
                    }
                }
            }
        }

        // Store in cache before returning
        if let Ok(mut cache) = self.dependency_specs_cache().lock() {
            cache.insert(cache_key, dependency_specs.clone());
            tracing::debug!(
                "Stored {} dependency specs in cache for '{}'",
                dependency_specs.len(),
                resource.name
            );
        }

        Ok(dependency_specs)
    }

    /// Generate dependency name from a path (matching resolver logic).
    ///
    /// For local transitive dependencies, the resolver uses the full relative path
    /// (without extension) as the resource name to maintain uniqueness.
    /// Build dependency data for the template context.
    ///
    /// This creates a nested structure containing:
    /// 1. ALL resources from the lockfile (path-based names) - for universal access
    /// 2. Current resource's declared dependencies (custom alias names) - for scoped access
    ///
    /// This dual approach ensures:
    /// - Any resource can access any other resource via path-based names
    /// - Resources can use custom aliases for their dependencies without collisions
    ///
    /// # Arguments
    ///
    /// * `current_resource` - The resource being rendered (for scoped alias mapping)
    async fn build_dependencies_data(
        &self,
        current_resource: &crate::lockfile::LockedResource,
        rendering_stack: &mut HashSet<String>,
    ) -> Result<BTreeMap<String, BTreeMap<String, crate::templating::context::DependencyData>>>;

    /// Build context with visited tracking (for recursive rendering).
    ///
    /// This method should be implemented by the context builder to support
    /// recursive template rendering with cycle detection.
    async fn build_context_with_visited(
        &self,
        resource_id: &ResourceId,
        variant_inputs: &serde_json::Value,
        rendering_stack: &mut HashSet<String>,
    ) -> Result<tera::Context>;
}