agpm-cli 0.4.9

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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
//! Dependency resolution and conflict detection for AGPM.
//!
//! This module implements the core dependency resolution algorithm that transforms
//! manifest dependencies into locked versions. It handles version constraint solving,
//! conflict detection, transitive dependency resolution,
//! parallel source synchronization, and relative path preservation during installation.
//!
//! # Service-Based Architecture
//!
//! This resolver has been refactored to use a service-based architecture:
//! - **ResolutionCore**: Shared immutable state
//! - **VersionResolutionService**: Git operations and version resolution
//! - **PatternExpansionService**: Glob pattern expansion
//! - **TransitiveDependencyService**: Transitive dependency resolution
//! - **ConflictService**: Conflict detection
//! - **ResourceFetchingService**: Resource content fetching

// Declare service modules
pub mod conflict_service;
pub mod dependency_graph;
pub mod lockfile_builder;
pub mod path_resolver;
pub mod pattern_expander;
pub mod resource_service;
pub mod source_context;
pub mod transitive_resolver;
pub mod types;
pub mod version_resolver;

// Re-export utility functions for compatibility
pub use path_resolver::{extract_meaningful_path, is_file_relative_path, normalize_bare_filename};

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::Result;

use crate::cache::Cache;
use crate::core::{OperationContext, ResourceType};
use crate::lockfile::{LockFile, LockedResource};
use crate::manifest::{Manifest, ResourceDependency};
use crate::source::SourceManager;

// Re-export services for external use
pub use conflict_service::ConflictService;
pub use pattern_expander::PatternExpansionService;
pub use resource_service::ResourceFetchingService;
pub use types::ResolutionCore;
pub use version_resolver::{
    VersionResolutionService, VersionResolver as VersionResolverExport, find_best_matching_tag,
    is_version_constraint, parse_tags_to_versions,
};

// Legacy re-exports for compatibility
pub use dependency_graph::{DependencyGraph, DependencyNode};
pub use lockfile_builder::LockfileBuilder;
pub use pattern_expander::{expand_pattern_to_concrete_deps, generate_dependency_name};
pub use types::{
    DependencyKey, ManifestOverride, ManifestOverrideIndex, OverrideKey, ResolutionContext,
    TransitiveContext,
};

pub use version_resolver::{PreparedSourceVersion, VersionResolver, WorktreeManager};

/// Main dependency resolver with service-based architecture.
///
/// This orchestrates multiple specialized services to handle different aspects
/// of the dependency resolution process while maintaining compatibility
/// with existing interfaces.
#[allow(dead_code)] // Some fields not yet used in service-based refactoring
pub struct DependencyResolver {
    /// Core shared context with immutable state
    core: ResolutionCore,

    /// Version resolution and Git operations service
    version_service: VersionResolutionService,

    /// Pattern expansion service for glob dependencies
    pattern_service: PatternExpansionService,

    /// Conflict detection service
    conflict_service: ConflictService,

    /// Resource fetching and metadata service
    resource_service: ResourceFetchingService,

    /// Conflict detector for version conflicts
    conflict_detector: crate::version::conflict::ConflictDetector,

    /// Dependency tracking state
    dependency_map: HashMap<DependencyKey, Vec<String>>,

    /// Pattern alias tracking for expanded patterns
    pattern_alias_map: HashMap<(ResourceType, String), String>,

    /// Transitive dependency custom names
    transitive_custom_names: HashMap<DependencyKey, String>,

    /// Track if sources have been pre-synced to avoid duplicate work
    sources_pre_synced: bool,
}

impl DependencyResolver {
    /// Create a new dependency resolver.
    ///
    /// # Arguments
    ///
    /// * `manifest` - Project manifest with dependencies
    /// * `cache` - Cache for Git operations and worktrees
    ///
    /// # Errors
    ///
    /// Returns an error if source manager cannot be created
    pub async fn new(manifest: Manifest, cache: Cache) -> Result<Self> {
        Self::new_with_context(manifest, cache, None).await
    }

    /// Create a new dependency resolver with operation context.
    ///
    /// # Arguments
    ///
    /// * `manifest` - Project manifest with dependencies
    /// * `cache` - Cache for Git operations and worktrees
    /// * `operation_context` - Optional context for warning deduplication
    ///
    /// # Errors
    ///
    /// Returns an error if source manager cannot be created
    pub async fn new_with_context(
        manifest: Manifest,
        cache: Cache,
        operation_context: Option<Arc<OperationContext>>,
    ) -> Result<Self> {
        // Create source manager from manifest
        let source_manager = SourceManager::from_manifest(&manifest)?;

        // Create resolution core with shared state
        let core = ResolutionCore::new(manifest, cache, source_manager, operation_context);

        // Initialize all services
        let version_service = VersionResolutionService::new(core.cache().clone());
        let pattern_service = PatternExpansionService::new();
        let conflict_service = ConflictService::new();
        let resource_service = ResourceFetchingService::new();

        Ok(Self {
            core,
            version_service,
            pattern_service,
            conflict_service,
            resource_service,
            conflict_detector: crate::version::conflict::ConflictDetector::new(),
            dependency_map: HashMap::new(),
            pattern_alias_map: HashMap::new(),
            transitive_custom_names: HashMap::new(),
            sources_pre_synced: false,
        })
    }

    /// Create a new resolver with global configuration support.
    ///
    /// This loads both manifest sources and global sources from `~/.agpm/config.toml`.
    ///
    /// # Arguments
    ///
    /// * `manifest` - Project manifest with dependencies
    /// * `cache` - Cache for Git operations and worktrees
    ///
    /// # Errors
    ///
    /// Returns an error if global configuration cannot be loaded
    pub async fn new_with_global(manifest: Manifest, cache: Cache) -> Result<Self> {
        Self::new_with_global_context(manifest, cache, None).await
    }

    /// Creates a new dependency resolver with custom cache directory.
    ///
    /// # Arguments
    ///
    /// * `cache` - Cache for Git operations and worktrees
    ///
    /// # Errors
    ///
    /// Returns an error if source manager cannot be created
    pub async fn with_cache(manifest: Manifest, cache: Cache) -> Result<Self> {
        Self::new_with_context(manifest, cache, None).await
    }

    /// Create a new resolver with global configuration and operation context.
    ///
    /// This loads both manifest sources and global sources from `~/.agpm/config.toml`.
    ///
    /// # Arguments
    ///
    /// * `manifest` - Project manifest with dependencies
    /// * `cache` - Cache for Git operations and worktrees
    /// * `operation_context` - Optional context for warning deduplication
    ///
    /// # Errors
    ///
    /// Returns an error if global configuration cannot be loaded
    pub async fn new_with_global_context(
        manifest: Manifest,
        cache: Cache,
        _operation_context: Option<Arc<OperationContext>>,
    ) -> Result<Self> {
        let source_manager = SourceManager::from_manifest_with_global(&manifest).await?;

        let core = ResolutionCore::new(manifest, cache, source_manager, _operation_context);

        let version_service = VersionResolutionService::new(core.cache().clone());
        let pattern_service = PatternExpansionService::new();
        let conflict_service = ConflictService::new();
        let resource_service = ResourceFetchingService::new();

        Ok(Self {
            core,
            version_service,
            pattern_service,
            conflict_service,
            resource_service,
            conflict_detector: crate::version::conflict::ConflictDetector::new(),
            dependency_map: HashMap::new(),
            pattern_alias_map: HashMap::new(),
            transitive_custom_names: HashMap::new(),
            sources_pre_synced: false,
        })
    }

    /// Get a reference to the resolution core.
    pub fn core(&self) -> &ResolutionCore {
        &self.core
    }

    /// Resolve all dependencies and generate a complete lockfile.
    ///
    /// This is the main resolution method.
    ///
    /// # Errors
    ///
    /// Returns an error if any step of resolution fails
    pub async fn resolve(&mut self) -> Result<LockFile> {
        self.resolve_with_options(true).await
    }

    /// Resolve dependencies with transitive resolution option.
    ///
    /// # Arguments
    ///
    /// * `enable_transitive` - Whether to resolve transitive dependencies
    ///
    /// # Errors
    ///
    /// Returns an error if resolution fails
    pub async fn resolve_with_options(&mut self, enable_transitive: bool) -> Result<LockFile> {
        let mut lockfile = LockFile::new();

        // Add sources to lockfile
        for (name, url) in &self.core.manifest().sources {
            lockfile.add_source(name.clone(), url.clone(), String::new());
        }

        // Phase 1: Extract dependencies from manifest with types
        let base_deps: Vec<(String, ResourceDependency, ResourceType)> = self
            .core
            .manifest()
            .all_dependencies_with_types()
            .into_iter()
            .map(|(name, dep, resource_type)| (name.to_string(), dep.into_owned(), resource_type))
            .collect();

        // Add direct dependencies to conflict detector
        for (name, dep, _) in &base_deps {
            self.add_to_conflict_detector(name, dep, "manifest");
        }

        // Phase 2: Pre-sync all sources if not already done
        if !self.sources_pre_synced {
            let deps_for_sync: Vec<(String, ResourceDependency)> =
                base_deps.iter().map(|(name, dep, _)| (name.clone(), dep.clone())).collect();
            self.version_service.pre_sync_sources(&self.core, &deps_for_sync).await?;
            self.sources_pre_synced = true;
        }

        // Phase 3: Resolve transitive dependencies
        let all_deps = if enable_transitive {
            self.resolve_transitive_dependencies(&base_deps).await?
        } else {
            base_deps.clone()
        };

        // Phase 4: Resolve each dependency to a locked resource
        for (name, dep, resource_type) in &all_deps {
            if dep.is_pattern() {
                // Pattern dependencies resolve to multiple resources
                let entries = self.resolve_pattern_dependency(name, dep, *resource_type).await?;

                // Add each resolved entry with deduplication
                for entry in entries {
                    let entry_name = entry.name.clone();
                    self.add_or_update_lockfile_entry(&mut lockfile, &entry_name, entry);
                }
            } else {
                // Regular single dependency
                let entry = self.resolve_dependency(name, dep, *resource_type).await?;
                self.add_or_update_lockfile_entry(&mut lockfile, name, entry);
            }
        }

        // Phase 5: Detect conflicts
        let conflicts = self.conflict_detector.detect_conflicts();
        if !conflicts.is_empty() {
            let mut error_msg = String::from("Version conflicts detected:\n\n");
            for conflict in &conflicts {
                error_msg.push_str(&format!("{conflict}\n"));
            }
            return Err(anyhow::anyhow!("{}", error_msg));
        }

        // Phase 6: Post-process dependencies and detect target conflicts
        self.add_version_to_dependencies(&mut lockfile)?;
        self.detect_target_conflicts(&lockfile)?;

        Ok(lockfile)
    }

    /// Pre-sync sources for the given dependencies.
    ///
    /// This performs Git operations to ensure all required sources are available
    /// before the main resolution process begins.
    ///
    /// # Arguments
    ///
    /// * `deps` - List of (name, dependency) pairs to sync sources for
    ///
    /// # Errors
    ///
    /// Returns an error if source synchronization fails
    pub async fn pre_sync_sources(&mut self, deps: &[(String, ResourceDependency)]) -> Result<()> {
        // Pre-sync all sources using version service
        self.version_service.pre_sync_sources(&self.core, deps).await?;
        self.sources_pre_synced = true;
        Ok(())
    }

    /// Update dependencies with existing lockfile and specific dependencies to update.
    ///
    /// # Arguments
    ///
    /// * `existing` - Existing lockfile to update
    /// * `deps_to_update` - Optional specific dependency names to update (None = all)
    ///
    /// # Errors
    ///
    /// Returns an error if update process fails
    pub async fn update(
        &mut self,
        existing: &LockFile,
        deps_to_update: Option<Vec<String>>,
    ) -> Result<LockFile> {
        // For now, just resolve all dependencies
        // TODO: Implement proper incremental update logic using deps_to_update names
        let _existing = existing; // Suppress unused warning for now
        let _deps_to_update = deps_to_update; // Suppress unused warning for now
        self.resolve_with_options(true).await
    }

    /// Get available versions for a repository.
    ///
    /// # Arguments
    ///
    /// * `repo_path` - Path to the Git repository
    ///
    /// # Returns
    ///
    /// List of available version strings (tags and branches)
    pub async fn get_available_versions(&self, repo_path: &Path) -> Result<Vec<String>> {
        VersionResolutionService::get_available_versions(&self.core, repo_path).await
    }

    /// Verify that existing lockfile is still valid.
    ///
    /// # Arguments
    ///
    /// * `_lockfile` - Existing lockfile to verify
    ///
    /// # Errors
    ///
    /// Returns an error if verification fails
    pub async fn verify(&self, _lockfile: &LockFile) -> Result<()> {
        // TODO: Implement verification logic using services
        Ok(())
    }

    /// Get current operation context if available.
    pub fn operation_context(&self) -> Option<&Arc<OperationContext>> {
        self.core.operation_context()
    }

    /// Set the operation context for warning deduplication.
    ///
    /// # Arguments
    ///
    /// * `context` - The operation context to use
    pub fn set_operation_context(&mut self, context: Arc<OperationContext>) {
        self.core.operation_context = Some(context);
    }
}

// Private helper methods
impl DependencyResolver {
    /// Build an index of manifest overrides for deduplication with transitive deps.
    ///
    /// This method creates a mapping from resource identity (source, path, tool, variant_hash)
    /// to the customizations (filename, target, install, template_vars) specified in the manifest.
    /// When a transitive dependency is discovered that matches a manifest dependency, the manifest
    /// version's customizations will take precedence.
    fn build_manifest_override_index(
        &self,
        base_deps: &[(String, ResourceDependency, ResourceType)],
    ) -> types::ManifestOverrideIndex {
        use crate::resolver::types::{ManifestOverride, OverrideKey, normalize_lookup_path};

        let mut index = HashMap::new();

        for (name, dep, resource_type) in base_deps {
            // Skip pattern dependencies (they expand later)
            if dep.is_pattern() {
                continue;
            }

            // Build the override key
            let normalized_path = normalize_lookup_path(dep.get_path());
            let source = dep.get_source().map(std::string::ToString::to_string);

            // Determine tool for this dependency
            let tool = dep
                .get_tool()
                .map(str::to_string)
                .unwrap_or_else(|| self.core.manifest().get_default_tool(*resource_type));

            // Compute variant_hash from MERGED variant_inputs (dep + global config)
            // This ensures manifest overrides use the same hash as LockedResources
            let merged_variant_inputs =
                lockfile_builder::build_merged_variant_inputs(self.core.manifest(), dep);
            let variant_hash = crate::utils::compute_variant_inputs_hash(&merged_variant_inputs)
                .unwrap_or_else(|_| crate::utils::EMPTY_VARIANT_INPUTS_HASH.to_string());

            let key = OverrideKey {
                resource_type: *resource_type,
                normalized_path,
                source,
                tool,
                variant_hash,
            };

            // Build the override info
            let override_info = ManifestOverride {
                filename: dep.get_filename().map(std::string::ToString::to_string),
                target: dep.get_target().map(std::string::ToString::to_string),
                install: dep.get_install(),
                manifest_alias: Some(name.clone()),
                template_vars: dep.get_template_vars().cloned(),
            };

            tracing::debug!(
                "Adding manifest override for {:?}:{} (tool={}, variant_hash={})",
                resource_type,
                dep.get_path(),
                key.tool,
                key.variant_hash
            );

            index.insert(key, override_info);
        }

        tracing::info!("Built manifest override index with {} entries", index.len());
        index
    }

    /// Resolve transitive dependencies starting from base dependencies.
    ///
    /// Discovers dependencies declared in resource files, expands patterns,
    /// builds dependency graph with cycle detection, and returns all dependencies
    /// in topological order.
    async fn resolve_transitive_dependencies(
        &mut self,
        base_deps: &[(String, ResourceDependency, ResourceType)],
    ) -> Result<Vec<(String, ResourceDependency, ResourceType)>> {
        use crate::resolver::transitive_resolver;

        // Build override index FIRST from manifest dependencies
        let manifest_overrides = self.build_manifest_override_index(base_deps);

        // Build ResolutionContext for the transitive resolver
        let resolution_ctx = ResolutionContext {
            manifest: self.core.manifest(),
            cache: self.core.cache(),
            source_manager: self.core.source_manager(),
            operation_context: self.core.operation_context(),
        };

        // Build TransitiveContext with mutable state and the override index
        let mut ctx = TransitiveContext {
            base: resolution_ctx,
            dependency_map: &mut self.dependency_map,
            transitive_custom_names: &mut self.transitive_custom_names,
            conflict_detector: &mut self.conflict_detector,
            manifest_overrides: &manifest_overrides,
        };

        // Get prepared versions from version service (clone to avoid borrow conflicts)
        let prepared_versions = self.version_service.prepared_versions().clone();

        // Create services container
        let mut services = transitive_resolver::ResolutionServices {
            version_service: &mut self.version_service,
            pattern_service: &mut self.pattern_service,
        };

        // Call the service-based transitive resolver
        transitive_resolver::resolve_with_services(
            &mut ctx,
            &self.core,
            base_deps,
            true, // enable_transitive
            &prepared_versions,
            &mut self.pattern_alias_map,
            &mut services,
        )
        .await
    }

    /// Get the list of transitive dependencies for a resource.
    ///
    /// Returns the dependency IDs (format: "type/name") for all transitive
    /// dependencies discovered during resolution.
    fn get_dependencies_for(
        &self,
        name: &str,
        source: Option<&str>,
        resource_type: ResourceType,
        tool: Option<&str>,
        variant_hash: &str,
    ) -> Vec<String> {
        let key = (
            resource_type,
            name.to_string(),
            source.map(std::string::ToString::to_string),
            tool.map(std::string::ToString::to_string),
            variant_hash.to_string(),
        );
        let result = self.dependency_map.get(&key).cloned().unwrap_or_default();
        tracing::debug!(
            "[DEBUG] get_dependencies_for: name='{}', type={:?}, source={:?}, tool={:?}, hash={}, found={} deps",
            name,
            resource_type,
            source,
            tool,
            &variant_hash[..8],
            result.len()
        );
        result
    }

    /// Get pattern alias for a concrete dependency.
    ///
    /// Returns the pattern name if this dependency was created from a pattern expansion.
    fn get_pattern_alias_for_dependency(
        &self,
        name: &str,
        resource_type: ResourceType,
    ) -> Option<String> {
        // Check if this dependency was created from a pattern expansion
        self.pattern_alias_map.get(&(resource_type, name.to_string())).cloned()
    }

    /// Resolve a single dependency to a lockfile entry.
    ///
    /// Handles both local and remote dependencies, computing proper installation
    /// paths and including all necessary metadata.
    async fn resolve_dependency(
        &mut self,
        name: &str,
        dep: &ResourceDependency,
        resource_type: ResourceType,
    ) -> Result<LockedResource> {
        use crate::resolver::lockfile_builder;
        use crate::resolver::path_resolver as install_path_resolver;
        use crate::utils::normalize_path_for_storage;

        tracing::debug!(
            "resolve_dependency: name={}, path={}, source={:?}, is_local={}",
            name,
            dep.get_path(),
            dep.get_source(),
            dep.is_local()
        );

        if dep.is_local() {
            // Local dependency
            let filename = if let Some(custom_filename) = dep.get_filename() {
                custom_filename.to_string()
            } else {
                extract_meaningful_path(Path::new(dep.get_path()))
            };

            let artifact_type_string = dep
                .get_tool()
                .map(|s| s.to_string())
                .unwrap_or_else(|| self.core.manifest().get_default_tool(resource_type));
            let artifact_type = artifact_type_string.as_str();

            let installed_at = install_path_resolver::resolve_install_path(
                self.core.manifest(),
                dep,
                artifact_type,
                resource_type,
                &filename,
            )?;

            // Determine manifest_alias: only set for direct manifest dependencies or pattern-expanded
            let has_pattern_alias = self.get_pattern_alias_for_dependency(name, resource_type);
            let is_in_manifest = self
                .core
                .manifest()
                .get_dependencies(resource_type)
                .is_some_and(|deps| deps.contains_key(name));

            let manifest_alias = if let Some(ref pattern_alias) = has_pattern_alias {
                // Pattern-expanded dependency - use pattern name as manifest_alias
                Some(pattern_alias.clone())
            } else if is_in_manifest {
                // Direct manifest dependency - use name as manifest_alias
                Some(name.to_string())
            } else {
                // Transitive dependency - no manifest_alias
                None
            };

            tracing::debug!(
                "manifest_alias calculation: name={}, path={}, has_pattern_alias={}, is_in_manifest={}, manifest_alias={:?}",
                name,
                dep.get_path(),
                has_pattern_alias.is_some(),
                is_in_manifest,
                manifest_alias
            );

            let applied_patches = lockfile_builder::get_patches_for_resource(
                self.core.manifest(),
                resource_type,
                name,
                manifest_alias.as_deref(),
            );

            // Generate canonical name for local dependencies using source context
            let canonical_name =
                if let Some(manifest_dir) = self.core.manifest().manifest_dir.as_ref() {
                    // Get the full path to the local dependency
                    let full_path = if Path::new(dep.get_path()).is_absolute() {
                        PathBuf::from(dep.get_path())
                    } else {
                        manifest_dir.join(dep.get_path())
                    };

                    // Normalize the path to handle ../ and ./ components deterministically
                    // Use normalize_path instead of canonicalize() to avoid filesystem-dependent behavior
                    // that can cause non-deterministic results across runs
                    let canonical_path = crate::utils::fs::normalize_path(&full_path);

                    let source_context =
                        crate::resolver::source_context::SourceContext::local(manifest_dir);
                    generate_dependency_name(&canonical_path.to_string_lossy(), &source_context)
                } else {
                    // Fallback to name if manifest_dir is not available
                    name.to_string()
                };

            let variant_inputs = lockfile_builder::VariantInputs::new(
                lockfile_builder::build_merged_variant_inputs(self.core.manifest(), dep),
            );

            Ok(LockedResource {
                name: canonical_name,
                source: None,
                url: None,
                path: normalize_path_for_storage(dep.get_path()),
                version: None,
                resolved_commit: None,
                checksum: String::new(),
                installed_at,
                dependencies: self.get_dependencies_for(
                    name,
                    None,
                    resource_type,
                    Some(&artifact_type_string),
                    variant_inputs.hash(),
                ),
                resource_type,
                tool: Some(artifact_type_string),
                manifest_alias,
                applied_patches,
                install: dep.get_install(),
                variant_inputs,
                context_checksum: None,
            })
        } else {
            // Remote dependency - use canonical naming for consistency
            let source_name = dep
                .get_source()
                .ok_or_else(|| anyhow::anyhow!("Dependency '{}' has no source specified", name))?;

            // Generate canonical name using remote source context
            let source_context =
                crate::resolver::source_context::SourceContext::remote(source_name);
            let canonical_name = generate_dependency_name(dep.get_path(), &source_context);

            let source_url = self
                .core
                .source_manager()
                .get_source_url(source_name)
                .ok_or_else(|| anyhow::anyhow!("Source '{}' not found", source_name))?;

            let version_key =
                dep.get_version().map_or_else(|| "HEAD".to_string(), |v| v.to_string());
            let group_key = format!("{}::{}", source_name, version_key);

            let prepared =
                self.version_service.get_prepared_version(&group_key).ok_or_else(|| {
                    anyhow::anyhow!(
                        "Prepared state missing for source '{}' @ '{}'",
                        source_name,
                        version_key
                    )
                })?;

            let filename = if let Some(custom_filename) = dep.get_filename() {
                custom_filename.to_string()
            } else {
                Path::new(dep.get_path()).to_string_lossy().to_string()
            };

            let artifact_type_string = dep
                .get_tool()
                .map(|s| s.to_string())
                .unwrap_or_else(|| self.core.manifest().get_default_tool(resource_type));
            let artifact_type = artifact_type_string.as_str();

            let installed_at = install_path_resolver::resolve_install_path(
                self.core.manifest(),
                dep,
                artifact_type,
                resource_type,
                &filename,
            )?;

            // Determine manifest_alias: only set for direct manifest dependencies or pattern-expanded
            let manifest_alias = if let Some(pattern_alias) =
                self.get_pattern_alias_for_dependency(name, resource_type)
            {
                // Pattern-expanded dependency - use pattern name as manifest_alias
                Some(pattern_alias)
            } else if self
                .core
                .manifest()
                .get_dependencies(resource_type)
                .is_some_and(|deps| deps.contains_key(name))
            {
                // Direct manifest dependency - use name as manifest_alias
                Some(name.to_string())
            } else {
                // Transitive dependency - no manifest_alias
                None
            };

            let applied_patches = lockfile_builder::get_patches_for_resource(
                self.core.manifest(),
                resource_type,
                name,
                manifest_alias.as_deref(),
            );

            let variant_inputs = lockfile_builder::VariantInputs::new(
                lockfile_builder::build_merged_variant_inputs(self.core.manifest(), dep),
            );

            Ok(LockedResource {
                name: canonical_name, // Use canonical name for internal consistency
                source: Some(source_name.to_string()),
                url: Some(source_url.clone()),
                path: normalize_path_for_storage(dep.get_path()),
                version: prepared.resolved_version.clone(),
                resolved_commit: Some(prepared.resolved_commit.clone()),
                checksum: String::new(),
                installed_at,
                dependencies: self.get_dependencies_for(
                    name,
                    Some(source_name),
                    resource_type,
                    Some(&artifact_type_string),
                    variant_inputs.hash(),
                ),
                resource_type,
                tool: Some(artifact_type_string),
                manifest_alias,
                applied_patches,
                install: dep.get_install(),
                variant_inputs,
                context_checksum: None,
            })
        }
    }

    /// Resolve a pattern dependency to multiple locked resources.
    async fn resolve_pattern_dependency(
        &mut self,
        name: &str,
        dep: &ResourceDependency,
        resource_type: ResourceType,
    ) -> Result<Vec<LockedResource>> {
        use crate::pattern::PatternResolver;
        use crate::resolver::{
            lockfile_builder, path_resolver as install_path_resolver, path_resolver,
        };
        use crate::utils::{
            compute_relative_install_path, normalize_path, normalize_path_for_storage,
        };

        if !dep.is_pattern() {
            return Err(anyhow::anyhow!(
                "Expected pattern dependency but no glob characters found in path"
            ));
        }

        let pattern = dep.get_path();

        if dep.is_local() {
            // Local pattern
            let (base_path, pattern_str) = path_resolver::parse_pattern_base_path(pattern);
            let pattern_resolver = PatternResolver::new();
            let matches = pattern_resolver.resolve(&pattern_str, &base_path)?;

            let artifact_type_string = dep
                .get_tool()
                .map(|s| s.to_string())
                .unwrap_or_else(|| self.core.manifest().get_default_tool(resource_type));
            let artifact_type = artifact_type_string.as_str();

            // Compute variant inputs once for all matched files in the pattern
            let variant_inputs = lockfile_builder::VariantInputs::new(
                lockfile_builder::build_merged_variant_inputs(self.core.manifest(), dep),
            );

            let mut resources = Vec::new();
            for matched_path in matches {
                let resource_name = crate::pattern::extract_resource_name(&matched_path);
                let full_relative_path =
                    path_resolver::construct_full_relative_path(&base_path, &matched_path);
                let filename = path_resolver::extract_pattern_filename(&base_path, &matched_path);

                let installed_at = install_path_resolver::resolve_install_path(
                    self.core.manifest(),
                    dep,
                    artifact_type,
                    resource_type,
                    &filename,
                )?;

                resources.push(LockedResource {
                    name: resource_name.clone(),
                    source: None,
                    url: None,
                    path: full_relative_path,
                    version: None,
                    resolved_commit: None,
                    checksum: String::new(),
                    installed_at,
                    dependencies: vec![],
                    resource_type,
                    tool: Some(artifact_type_string.clone()),
                    manifest_alias: Some(name.to_string()),
                    applied_patches: lockfile_builder::get_patches_for_resource(
                        self.core.manifest(),
                        resource_type,
                        &resource_name, // Use canonical resource name
                        Some(name),     // Use manifest_alias for patch lookups
                    ),
                    install: dep.get_install(),
                    variant_inputs: variant_inputs.clone(),
                    context_checksum: None,
                });
            }

            Ok(resources)
        } else {
            // Remote pattern
            // Preserve the original pattern name since it might be shadowed later
            let pattern_name = name;

            let source_name = dep.get_source().ok_or_else(|| {
                anyhow::anyhow!("Pattern dependency '{}' has no source specified", name)
            })?;

            let source_url = self
                .core
                .source_manager()
                .get_source_url(source_name)
                .ok_or_else(|| anyhow::anyhow!("Source '{}' not found", source_name))?;

            let version_key =
                dep.get_version().map_or_else(|| "HEAD".to_string(), |v| v.to_string());
            let group_key = format!("{}::{}", source_name, version_key);

            let prepared =
                self.version_service.get_prepared_version(&group_key).ok_or_else(|| {
                    anyhow::anyhow!(
                        "Prepared state missing for source '{}' @ '{}'",
                        source_name,
                        version_key
                    )
                })?;

            let repo_path = Path::new(&prepared.worktree_path);
            let pattern_resolver = PatternResolver::new();
            let matches = pattern_resolver.resolve(pattern, repo_path)?;

            let artifact_type_string = dep
                .get_tool()
                .map(|s| s.to_string())
                .unwrap_or_else(|| self.core.manifest().get_default_tool(resource_type));
            let artifact_type = artifact_type_string.as_str();

            // Compute variant inputs once for all matched files in the pattern
            let variant_inputs = lockfile_builder::VariantInputs::new(
                lockfile_builder::build_merged_variant_inputs(self.core.manifest(), dep),
            );

            let mut resources = Vec::new();
            for matched_path in matches {
                let resource_name = crate::pattern::extract_resource_name(&matched_path);

                // Compute installation path
                let installed_at = match resource_type {
                    ResourceType::Hook | ResourceType::McpServer => {
                        install_path_resolver::resolve_merge_target_path(
                            self.core.manifest(),
                            artifact_type,
                            resource_type,
                        )
                    }
                    _ => {
                        let artifact_path = self
                            .core
                            .manifest()
                            .get_artifact_resource_path(artifact_type, resource_type)
                            .ok_or_else(|| {
                                anyhow::anyhow!(
                                    "Resource type '{}' is not supported by tool '{}'",
                                    resource_type,
                                    artifact_type
                                )
                            })?;

                        let dep_flatten = dep.get_flatten();
                        let tool_flatten = self
                            .core
                            .manifest()
                            .get_tool_config(artifact_type)
                            .and_then(|config| config.resources.get(resource_type.to_plural()))
                            .and_then(|resource_config| resource_config.flatten);

                        let flatten = dep_flatten.or(tool_flatten).unwrap_or(false);

                        let base_target = if let Some(custom_target) = dep.get_target() {
                            PathBuf::from(artifact_path.display().to_string())
                                .join(custom_target.trim_start_matches('/'))
                        } else {
                            artifact_path.to_path_buf()
                        };

                        let filename = repo_path.join(&matched_path).to_string_lossy().to_string();
                        let relative_path = compute_relative_install_path(
                            &base_target,
                            Path::new(&filename),
                            flatten,
                        );
                        normalize_path_for_storage(normalize_path(&base_target.join(relative_path)))
                    }
                };

                resources.push(LockedResource {
                    name: resource_name.clone(),
                    source: Some(source_name.to_string()),
                    url: Some(source_url.clone()),
                    path: normalize_path_for_storage(matched_path.to_string_lossy().to_string()),
                    version: prepared.resolved_version.clone(),
                    resolved_commit: Some(prepared.resolved_commit.clone()),
                    checksum: String::new(),
                    installed_at,
                    dependencies: vec![],
                    resource_type,
                    tool: Some(artifact_type_string.clone()),
                    manifest_alias: Some(pattern_name.to_string()),
                    applied_patches: lockfile_builder::get_patches_for_resource(
                        self.core.manifest(),
                        resource_type,
                        &resource_name,     // Use canonical resource name
                        Some(pattern_name), // Use manifest_alias for patch lookups
                    ),
                    install: dep.get_install(),
                    variant_inputs: variant_inputs.clone(),
                    context_checksum: None,
                });
            }

            Ok(resources)
        }
    }

    /// Add or update a lockfile entry with deduplication.
    fn add_or_update_lockfile_entry(
        &self,
        lockfile: &mut LockFile,
        _name: &str,
        entry: LockedResource,
    ) {
        let resources = lockfile.get_resources_mut(&entry.resource_type);

        if let Some(existing) =
            resources.iter_mut().find(|e| lockfile_builder::is_duplicate_entry(e, &entry))
        {
            // Replace only if the new entry is more authoritative than the existing one
            // Priority: Direct (manifest_alias != None) > Transitive (manifest_alias == None)
            let existing_is_direct = existing.manifest_alias.is_some();
            let new_is_direct = entry.manifest_alias.is_some();

            if new_is_direct || !existing_is_direct {
                // Replace if:
                // - New is direct (always wins)
                // - Both are transitive (newer wins)
                tracing::debug!(
                    "Replacing {} (direct={}) with {} (direct={})",
                    existing.name,
                    existing_is_direct,
                    entry.name,
                    new_is_direct
                );
                *existing = entry;
            } else {
                // Keep existing direct entry, ignore transitive replacement
                tracing::debug!("Keeping direct {} over transitive {}", existing.name, entry.name);
            }
        } else {
            resources.push(entry);
        }
    }

    /// Add version information to dependency references in lockfile.
    fn add_version_to_dependencies(&self, lockfile: &mut LockFile) -> Result<()> {
        use crate::resolver::lockfile_builder;

        lockfile_builder::add_version_to_all_dependencies(lockfile);
        Ok(())
    }

    /// Detect target path conflicts between resources.
    fn detect_target_conflicts(&self, lockfile: &LockFile) -> Result<()> {
        use crate::resolver::lockfile_builder;

        lockfile_builder::detect_target_conflicts(lockfile)
    }

    /// Add a dependency to the conflict detector.
    fn add_to_conflict_detector(
        &mut self,
        _name: &str,
        dep: &ResourceDependency,
        required_by: &str,
    ) {
        use crate::resolver::types as dependency_helpers;

        // Skip local dependencies (no version conflicts possible)
        if dep.is_local() {
            return;
        }

        // Build resource identifier
        let resource_id = dependency_helpers::build_resource_id(dep);

        // Get version constraint (None means HEAD/unspecified)
        let version = dep.get_version().unwrap_or("HEAD");

        // Add to conflict detector
        self.conflict_detector.add_requirement(&resource_id, required_by, version);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_resolver_creation() {
        let manifest = Manifest::default();
        let cache = Cache::new().unwrap();
        let resolver = DependencyResolver::new(manifest, cache).await;
        assert!(resolver.is_ok());
    }

    #[tokio::test]
    async fn test_resolver_with_global() {
        let manifest = Manifest::default();
        let cache = Cache::new().unwrap();
        let resolver = DependencyResolver::new_with_global(manifest, cache).await;
        assert!(resolver.is_ok());
    }
}