Skip to main content

mars_agents/sync/
target.rs

1use std::collections::HashSet;
2use std::path::{Path, PathBuf};
3
4use indexmap::IndexMap;
5
6use crate::config::{EffectiveConfig, FilterMode};
7use crate::diagnostic::{DiagnosticCategory, DiagnosticCollector};
8use crate::discover;
9use crate::error::MarsError;
10use crate::hash;
11use crate::lock::{ItemId, ItemKind, LockFile, LockIndex};
12use crate::resolve::ResolvedGraph;
13use crate::sync::filter::apply_filter;
14use crate::types::{
15    ContentHash, DestPath, ItemName, RenameMap, SourceId, SourceName, SourceOrigin,
16};
17
18/// What the `.mars/` canonical store should look like after sync.
19///
20/// Built from the resolved graph with intent-based filtering applied.
21#[derive(Debug, Clone)]
22pub struct TargetState {
23    /// Keyed by dest_path (relative to `.mars/`).
24    pub items: IndexMap<DestPath, TargetItem>,
25}
26
27/// A single item in the desired target state.
28#[derive(Debug, Clone)]
29pub struct TargetItem {
30    pub id: ItemId,
31    pub source_name: SourceName,
32    pub origin: SourceOrigin,
33    pub source_id: SourceId,
34    /// Path to content in fetched source tree.
35    pub source_path: PathBuf,
36    /// Relative path under `.mars/` (reflects rename if any).
37    pub dest_path: DestPath,
38    /// SHA-256 of source content.
39    pub source_hash: ContentHash,
40    /// True when this item comes from root-level `SKILL.md` flat skill discovery.
41    pub is_flat_skill: bool,
42    /// Optional in-memory content override after frontmatter rewrites.
43    pub rewritten_content: Option<String>,
44}
45
46/// Explicit skill rename that changes the installed skill name.
47#[derive(Debug, Clone)]
48pub struct ExplicitSkillRename {
49    pub original_name: ItemName,
50    pub new_name: ItemName,
51    pub source_name: SourceName,
52}
53
54/// Build target state with collision detection integrated.
55///
56/// This is the main entry point — it builds the target, applies explicit
57/// rename mappings, and raises hard collisions when two sources want the same
58/// destination.
59pub fn build_with_collisions(
60    graph: &ResolvedGraph,
61    config: &EffectiveConfig,
62) -> Result<(TargetState, Vec<ExplicitSkillRename>), MarsError> {
63    let mut diag = DiagnosticCollector::new();
64    build_with_collisions_and_diag(graph, config, &mut diag)
65}
66
67pub fn build_with_collisions_and_diag(
68    graph: &ResolvedGraph,
69    config: &EffectiveConfig,
70    diag: &mut DiagnosticCollector,
71) -> Result<(TargetState, Vec<ExplicitSkillRename>), MarsError> {
72    let mut items: IndexMap<DestPath, TargetItem> = IndexMap::new();
73    let mut explicit_skill_renames = Vec::new();
74
75    for source_name in &graph.order {
76        let node = &graph.nodes[source_name];
77        let source_config = config.dependencies.get(source_name);
78
79        let discovered = discover::discover_resolved_source(
80            &node.rooted_ref.package_root,
81            Some(source_name.as_str()),
82        )?;
83
84        let source_id = source_config
85            .map(|s| s.id.clone())
86            .unwrap_or_else(|| node.source_id.clone());
87
88        let Some(filters) = graph
89            .filters
90            .get(source_name)
91            .filter(|filters| !filters.is_empty())
92            .cloned()
93            .or_else(|| source_config.map(|source| vec![source.filter.clone()]))
94        else {
95            // No materialization request reached this transitive source.
96            continue;
97        };
98
99        let renames = source_config
100            .map(|s| &s.rename)
101            .cloned()
102            .unwrap_or_default();
103
104        let filtered = apply_filter_union(&discovered, &filters, &node.rooted_ref.package_root)?;
105
106        for item in filtered {
107            let is_flat_skill =
108                item.id.kind == ItemKind::Skill && item.source_path == Path::new(".");
109            let source_content_path = node.rooted_ref.package_root.join(&item.source_path);
110            let source_hash = if is_flat_skill {
111                ContentHash::from(hash::compute_skill_hash_filtered(
112                    &source_content_path,
113                    crate::fs::FLAT_SKILL_EXCLUDED_TOP_LEVEL,
114                )?)
115            } else {
116                ContentHash::from(hash::compute_hash(&source_content_path, item.id.kind)?)
117            };
118
119            let (dest_name, dest_path) =
120                apply_item_rename(item.id.kind, &item.id.name, &renames, source_name)?;
121            if item.id.kind == ItemKind::Agent
122                && let Err(message) = crate::target::validate_agent_filename(dest_name.as_str())
123            {
124                diag.error_with_category(
125                    "invalid-agent-filename",
126                    format!("{message}; skipping agent from source `{source_name}`"),
127                    DiagnosticCategory::Validation,
128                );
129                continue;
130            }
131            if item.id.kind == ItemKind::Skill && dest_name != item.id.name {
132                explicit_skill_renames.push(ExplicitSkillRename {
133                    original_name: item.id.name.clone(),
134                    new_name: dest_name.clone(),
135                    source_name: source_name.clone(),
136                });
137            }
138
139            let target_item = TargetItem {
140                id: ItemId {
141                    kind: item.id.kind,
142                    name: dest_name,
143                },
144                source_name: source_name.clone(),
145                origin: SourceOrigin::Dependency(source_name.clone()),
146                source_id: source_id.clone(),
147                source_path: source_content_path,
148                dest_path,
149                source_hash,
150                is_flat_skill,
151                rewritten_content: None,
152            };
153
154            if let Some(existing) = items.get(&target_item.dest_path) {
155                return Err(MarsError::Collision {
156                    item: format!("{} `{}`", target_item.id.kind, target_item.id.name),
157                    source_a: existing.source_name.to_string(),
158                    source_b: target_item.source_name.to_string(),
159                });
160            }
161
162            items.insert(target_item.dest_path.clone(), target_item);
163        }
164    }
165
166    Ok((TargetState { items }, explicit_skill_renames))
167}
168
169fn apply_filter_union(
170    discovered: &[discover::DiscoveredItem],
171    filters: &[FilterMode],
172    package_root: &Path,
173) -> Result<Vec<discover::DiscoveredItem>, MarsError> {
174    if filters.is_empty() {
175        return Ok(discovered.to_vec());
176    }
177
178    let mut union: HashSet<(ItemKind, ItemName, PathBuf)> = HashSet::new();
179    for filter in filters {
180        let filtered = apply_filter(discovered, filter, package_root)?;
181        union.extend(
182            filtered
183                .iter()
184                .map(|item| (item.id.kind, item.id.name.clone(), item.source_path.clone())),
185        );
186    }
187
188    Ok(discovered
189        .iter()
190        .filter(|item| {
191            union.contains(&(item.id.kind, item.id.name.clone(), item.source_path.clone()))
192        })
193        .cloned()
194        .collect())
195}
196
197// Re-export for API compatibility — rewrite_skill_refs moved to sync::rewrite.
198pub use crate::sync::rewrite::rewrite_skill_refs;
199
200/// Existing on-disk destination that is not lock-managed.
201#[derive(Debug, Clone, PartialEq, Eq)]
202pub struct UnmanagedCollision {
203    pub source_name: SourceName,
204    pub path: DestPath,
205}
206
207/// Detect target installs that would overwrite unmanaged on-disk content.
208///
209/// If a target destination already exists but is not tracked in the lock file,
210/// treat it as user-authored content and report it as a collision so callers can
211/// skip installation while leaving existing files untouched.
212pub fn check_unmanaged_collisions(
213    install_target: &Path,
214    lock: &LockFile,
215    target: &TargetState,
216) -> Vec<UnmanagedCollision> {
217    let mut collisions = Vec::new();
218    let lock_index = LockIndex::new(lock);
219
220    for (dest_key, target_item) in &target.items {
221        if lock_index.contains_dest_path(dest_key) {
222            continue;
223        }
224
225        let disk_path = target_item.dest_path.resolve(install_target);
226        if disk_path.exists() {
227            // Check if disk content matches what we'd install — if so,
228            // this is a partial prior install (crash recovery), not an
229            // unmanaged user file. Safe to overwrite.
230            let hash_path = hash_path_for_kind(&disk_path, target_item.id.kind);
231            if let Ok(disk_hash) = hash::compute_hash(&hash_path, target_item.id.kind)
232                && disk_hash == target_item.source_hash.as_str()
233            {
234                continue;
235            }
236
237            collisions.push(UnmanagedCollision {
238                source_name: target_item.source_name.clone(),
239                path: target_item.dest_path.clone(),
240            });
241        }
242    }
243
244    collisions
245}
246
247fn apply_item_rename(
248    kind: ItemKind,
249    item_name: &str,
250    renames: &RenameMap,
251    source_name: &SourceName,
252) -> Result<(ItemName, DestPath), MarsError> {
253    let default_dest = default_dest_path(kind, item_name);
254    let default_key = default_dest.as_str();
255
256    let rename_value = renames.get(default_key).or_else(|| renames.get(item_name));
257
258    let dest_path = match rename_value {
259        Some(value) => parse_rename_dest(kind, value.as_str(), source_name)?,
260        None => default_dest,
261    };
262    let dest_name = dest_name_from_dest(&dest_path, kind);
263
264    Ok((ItemName::from(dest_name), dest_path))
265}
266
267/// Construct the default destination path for an item.
268/// Uses string formatting to guarantee forward slashes on all platforms.
269fn default_dest_path(kind: ItemKind, name: &str) -> DestPath {
270    let path_str = match kind {
271        ItemKind::Agent => format!("agents/{name}.md"),
272        ItemKind::Skill => format!("skills/{name}"),
273        ItemKind::Hook => format!("hooks/{name}"),
274        ItemKind::McpServer => format!("mcp/{name}"),
275        ItemKind::BootstrapDoc => format!("bootstrap/{name}/BOOTSTRAP.md"),
276    };
277    // Safe: internal paths constructed from validated item names
278    DestPath::new(path_str).expect("internal default path is always valid")
279}
280
281fn parse_rename_dest(
282    kind: ItemKind,
283    rename_value: &str,
284    source_name: &SourceName,
285) -> Result<DestPath, MarsError> {
286    // Normalize backslashes to forward slashes for cross-platform handling
287    let normalized = rename_value.replace('\\', "/");
288    let has_prefix = normalized.starts_with("agents/")
289        || normalized.starts_with("skills/")
290        || normalized.starts_with("hooks/")
291        || normalized.starts_with("mcp/")
292        || normalized.starts_with("bootstrap/");
293    let has_parent = normalized.contains('/');
294
295    if has_prefix || has_parent {
296        let dest = if kind == ItemKind::BootstrapDoc && !normalized.ends_with("/BOOTSTRAP.md") {
297            format!("{normalized}/BOOTSTRAP.md")
298        } else {
299            normalized.clone()
300        };
301        return DestPath::new(&dest).map_err(|e| MarsError::Source {
302            source_name: source_name.to_string(),
303            message: format!("invalid rename destination `{rename_value}`: {e}"),
304        });
305    }
306
307    let path_str = match kind {
308        ItemKind::Agent => {
309            if normalized.ends_with(".md") {
310                format!("agents/{normalized}")
311            } else {
312                format!("agents/{normalized}.md")
313            }
314        }
315        ItemKind::Skill => format!("skills/{normalized}"),
316        ItemKind::Hook => format!("hooks/{normalized}"),
317        ItemKind::McpServer => format!("mcp/{normalized}"),
318        ItemKind::BootstrapDoc => format!("bootstrap/{normalized}/BOOTSTRAP.md"),
319    };
320    DestPath::new(path_str).map_err(|e| MarsError::Source {
321        source_name: source_name.to_string(),
322        message: format!("invalid rename destination `{rename_value}`: {e}"),
323    })
324}
325
326fn dest_name_from_dest(dest_path: &DestPath, kind: ItemKind) -> String {
327    match kind {
328        ItemKind::BootstrapDoc => dest_path.item_name(kind),
329        _ => {
330            let last = dest_path.as_str().rsplit('/').next().unwrap_or("");
331            match kind {
332                ItemKind::Agent => last.strip_suffix(".md").unwrap_or(last).to_string(),
333                ItemKind::Skill | ItemKind::Hook | ItemKind::McpServer => last.to_string(),
334                ItemKind::BootstrapDoc => unreachable!("handled above"),
335            }
336        }
337    }
338}
339
340fn hash_path_for_kind(path: &Path, kind: ItemKind) -> PathBuf {
341    if kind == ItemKind::BootstrapDoc {
342        path.parent()
343            .map(Path::to_path_buf)
344            .unwrap_or_else(|| path.to_path_buf())
345    } else {
346        path.to_path_buf()
347    }
348}
349
350#[cfg(test)]
351mod tests {
352    use super::*;
353    use crate::config::*;
354    use crate::lock::LockFile;
355    use crate::resolve::{ResolvedGraph, ResolvedNode};
356    use crate::source::ResolvedRef;
357    use indexmap::IndexMap;
358    use std::fs;
359    use tempfile::TempDir;
360
361    /// Helper: create a source tree with agents and skills
362    fn make_source_tree(agents: &[(&str, &str)], skills: &[(&str, &str)]) -> TempDir {
363        let dir = TempDir::new().unwrap();
364        if !agents.is_empty() {
365            let agents_dir = dir.path().join("agents");
366            fs::create_dir_all(&agents_dir).unwrap();
367            for (name, content) in agents {
368                fs::write(agents_dir.join(name), content).unwrap();
369            }
370        }
371        if !skills.is_empty() {
372            let skills_dir = dir.path().join("skills");
373            fs::create_dir_all(&skills_dir).unwrap();
374            for (name, content) in skills {
375                let skill_dir = skills_dir.join(name);
376                fs::create_dir_all(&skill_dir).unwrap();
377                fs::write(skill_dir.join("SKILL.md"), content).unwrap();
378            }
379        }
380        dir
381    }
382
383    fn make_graph_and_config(
384        sources: Vec<(&str, &TempDir, Option<&str>, FilterMode)>,
385    ) -> (ResolvedGraph, EffectiveConfig) {
386        let mut nodes = IndexMap::new();
387        let mut order = Vec::new();
388        let mut config_dependencies = IndexMap::new();
389
390        for (name, tree, url, filter) in sources {
391            let url_str = url.map(|u| u.to_string());
392            nodes.insert(
393                name.into(),
394                ResolvedNode {
395                    source_name: name.into(),
396                    source_id: if let Some(u) = url {
397                        SourceId::git(crate::types::SourceUrl::from(u))
398                    } else {
399                        SourceId::Path {
400                            canonical: tree.path().to_path_buf(),
401                            subpath: None,
402                        }
403                    },
404                    rooted_ref: crate::resolve::RootedSourceRef {
405                        checkout_root: tree.path().to_path_buf(),
406                        package_root: tree.path().to_path_buf(),
407                    },
408                    resolved_ref: ResolvedRef {
409                        source_name: name.into(),
410                        version: None,
411                        version_tag: None,
412                        commit: None,
413                        tree_path: tree.path().to_path_buf(),
414                    },
415                    latest_version: None,
416                    manifest: None,
417                    deps: vec![],
418                },
419            );
420            order.push(name.into());
421
422            let spec = if let Some(u) = url {
423                SourceSpec::Git(GitSpec {
424                    url: crate::types::SourceUrl::from(u),
425                    version: None,
426                })
427            } else {
428                SourceSpec::Path(tree.path().to_path_buf())
429            };
430
431            config_dependencies.insert(
432                name.into(),
433                EffectiveDependency {
434                    name: name.into(),
435                    id: if let Some(u) = url {
436                        SourceId::git(crate::types::SourceUrl::from(u))
437                    } else {
438                        SourceId::Path {
439                            canonical: tree.path().to_path_buf(),
440                            subpath: None,
441                        }
442                    },
443                    spec,
444                    subpath: None,
445                    filter,
446                    rename: RenameMap::new(),
447                    is_overridden: false,
448                    original_git: url_str.map(|u| GitSpec {
449                        url: crate::types::SourceUrl::from(u),
450                        version: None,
451                    }),
452                },
453            );
454        }
455
456        let graph = ResolvedGraph {
457            nodes,
458            order,
459            filters: std::collections::HashMap::new(),
460        };
461        let config = EffectiveConfig {
462            dependencies: config_dependencies,
463            settings: Settings::default(),
464        };
465        (graph, config)
466    }
467
468    // === Target build tests ===
469
470    #[test]
471    fn build_single_source_no_filter() {
472        let tree = make_source_tree(&[("coder.md", "# coder")], &[("planning", "# planning")]);
473        let (graph, config) = make_graph_and_config(vec![(
474            "base",
475            &tree,
476            Some("https://github.com/org/base"),
477            FilterMode::All,
478        )]);
479
480        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
481        assert!(renames.is_empty());
482        assert_eq!(target.items.len(), 2);
483        assert!(target.items.contains_key("agents/coder.md"));
484        assert!(target.items.contains_key("skills/planning"));
485    }
486
487    #[test]
488    fn invalid_windows_agent_filename_emits_diagnostic_and_skips() {
489        let tree = make_source_tree(&[("bad:name.md", "# bad"), ("coder.md", "# coder")], &[]);
490        let (graph, config) = make_graph_and_config(vec![(
491            "base",
492            &tree,
493            Some("https://github.com/org/base"),
494            FilterMode::All,
495        )]);
496        let mut diag = DiagnosticCollector::new();
497
498        let (target, _) = build_with_collisions_and_diag(&graph, &config, &mut diag).unwrap();
499        let diagnostics = diag.drain();
500
501        assert!(!target.items.contains_key("agents/bad:name.md"));
502        assert!(target.items.contains_key("agents/coder.md"));
503        assert_eq!(diagnostics.len(), 1);
504        assert_eq!(diagnostics[0].code, "invalid-agent-filename");
505    }
506
507    #[test]
508    fn build_with_path_rename_mapping() {
509        let tree = make_source_tree(&[("old-name.md", "# old")], &[]);
510
511        let (graph, mut config) = make_graph_and_config(vec![(
512            "base",
513            &tree,
514            Some("https://github.com/org/base"),
515            FilterMode::All,
516        )]);
517
518        // Add rename mapping
519        config
520            .dependencies
521            .get_mut("base")
522            .unwrap()
523            .rename
524            .insert("agents/old-name.md".into(), "agents/new-name.md".into());
525
526        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
527        assert!(renames.is_empty());
528        assert_eq!(target.items.len(), 1);
529        assert!(target.items.contains_key("agents/new-name.md"));
530        assert_eq!(target.items["agents/new-name.md"].id.name, "new-name");
531    }
532
533    #[test]
534    fn default_dest_path_uses_forward_slashes_for_agents_and_skills() {
535        let agent = default_dest_path(ItemKind::Agent, "coder");
536        let skill = default_dest_path(ItemKind::Skill, "planning");
537
538        assert_eq!(agent.as_str(), "agents/coder.md");
539        assert_eq!(skill.as_str(), "skills/planning");
540        assert!(!agent.as_str().contains('\\'));
541        assert!(!skill.as_str().contains('\\'));
542    }
543
544    #[test]
545    fn parse_rename_dest_normalizes_backslashes_to_forward_slashes() {
546        let source_name = SourceName::from("base");
547
548        let agent =
549            parse_rename_dest(ItemKind::Agent, r"agents\nested\renamed.md", &source_name).unwrap();
550        let skill =
551            parse_rename_dest(ItemKind::Skill, r"skills\nested\planning", &source_name).unwrap();
552
553        assert_eq!(agent.as_str(), "agents/nested/renamed.md");
554        assert_eq!(skill.as_str(), "skills/nested/planning");
555        assert!(!agent.as_str().contains('\\'));
556        assert!(!skill.as_str().contains('\\'));
557    }
558
559    #[test]
560    fn parse_rename_dest_rejects_absolute_and_escape_destinations() {
561        let source_name = SourceName::from("base");
562
563        let absolute = parse_rename_dest(ItemKind::Agent, "/tmp/escape", &source_name)
564            .expect_err("absolute rename should fail");
565        assert!(matches!(absolute, MarsError::Source { .. }));
566
567        let traversal = parse_rename_dest(ItemKind::Skill, "../escape", &source_name)
568            .expect_err("traversal rename should fail");
569        assert!(matches!(traversal, MarsError::Source { .. }));
570    }
571
572    #[test]
573    fn build_with_invalid_rename_destination_returns_error() {
574        let tree = make_source_tree(&[("old-name.md", "# old")], &[]);
575
576        let (graph, mut config) =
577            make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
578
579        config
580            .dependencies
581            .get_mut("base")
582            .unwrap()
583            .rename
584            .insert("agents/old-name.md".into(), "../escape.md".into());
585
586        let err = build_with_collisions(&graph, &config).unwrap_err();
587        assert!(matches!(err, MarsError::Source { .. }));
588    }
589
590    // === Collision tests ===
591
592    #[test]
593    fn collision_errors_instead_of_auto_renaming() {
594        let tree1 = make_source_tree(&[("coder.md", "# coder from source 1")], &[]);
595        let tree2 = make_source_tree(&[("coder.md", "# coder from source 2")], &[]);
596
597        let (graph, config) = make_graph_and_config(vec![
598            (
599                "source-a",
600                &tree1,
601                Some("https://github.com/alice/agents"),
602                FilterMode::All,
603            ),
604            (
605                "source-b",
606                &tree2,
607                Some("https://github.com/bob/agents"),
608                FilterMode::All,
609            ),
610        ]);
611
612        let err = build_with_collisions(&graph, &config).unwrap_err();
613        assert!(matches!(err, MarsError::Collision { .. }));
614    }
615
616    #[test]
617    fn no_collision_no_renames() {
618        let tree1 = make_source_tree(&[("coder.md", "# coder")], &[]);
619        let tree2 = make_source_tree(&[("reviewer.md", "# reviewer")], &[]);
620
621        let (graph, config) = make_graph_and_config(vec![
622            (
623                "source-a",
624                &tree1,
625                Some("https://github.com/alice/agents"),
626                FilterMode::All,
627            ),
628            (
629                "source-b",
630                &tree2,
631                Some("https://github.com/bob/agents"),
632                FilterMode::All,
633            ),
634        ]);
635
636        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
637        assert!(renames.is_empty());
638        assert_eq!(target.items.len(), 2);
639    }
640
641    // === Source with agents filter + skill deps ===
642
643    #[test]
644    fn build_with_agents_filter_pulls_transitive_skills() {
645        let tree = make_source_tree(
646            &[("coder.md", "---\nskills:\n  - planning\n---\n# Coder\n")],
647            &[("planning", "# Planning"), ("unused-skill", "# Unused")],
648        );
649
650        let (graph, config) = make_graph_and_config(vec![(
651            "base",
652            &tree,
653            None,
654            FilterMode::Include {
655                agents: vec!["coder".into()],
656                skills: vec![],
657            },
658        )]);
659
660        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
661        assert!(renames.is_empty());
662        assert_eq!(target.items.len(), 2); // coder + planning
663        assert!(target.items.contains_key("agents/coder.md"));
664        assert!(target.items.contains_key("skills/planning"));
665        // unused-skill should NOT be present
666        assert!(!target.items.contains_key("skills/unused-skill"));
667    }
668
669    #[test]
670    fn build_with_exclude_filter() {
671        let tree = make_source_tree(&[("coder.md", "# coder"), ("deprecated.md", "# old")], &[]);
672
673        let (graph, config) = make_graph_and_config(vec![(
674            "base",
675            &tree,
676            None,
677            FilterMode::Exclude(vec!["deprecated".into()]),
678        )]);
679
680        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
681        assert!(renames.is_empty());
682        assert_eq!(target.items.len(), 1);
683        assert!(target.items.contains_key("agents/coder.md"));
684    }
685
686    #[test]
687    fn build_unions_multiple_include_filters_for_same_source() {
688        let tree = make_source_tree(
689            &[],
690            &[
691                ("skill-a", "# Skill A"),
692                ("skill-b", "# Skill B"),
693                ("skill-c", "# Skill C"),
694            ],
695        );
696
697        let (mut graph, config) =
698            make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
699        graph.filters.insert(
700            "base".into(),
701            vec![
702                FilterMode::Include {
703                    agents: vec![],
704                    skills: vec!["skill-a".into(), "skill-b".into()],
705                },
706                FilterMode::Include {
707                    agents: vec![],
708                    skills: vec!["skill-b".into(), "skill-c".into()],
709                },
710            ],
711        );
712
713        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
714        assert!(renames.is_empty());
715        assert_eq!(target.items.len(), 3);
716        assert!(target.items.contains_key("skills/skill-a"));
717        assert!(target.items.contains_key("skills/skill-b"));
718        assert!(target.items.contains_key("skills/skill-c"));
719    }
720
721    #[test]
722    fn build_target_items_have_correct_hashes() {
723        let content = "# agent content for hash test";
724        let tree = make_source_tree(&[("test.md", content)], &[]);
725
726        let (graph, config) = make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
727
728        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
729        assert!(renames.is_empty());
730        let item = &target.items["agents/test.md"];
731        let expected_hash = hash::hash_bytes(content.as_bytes());
732        assert_eq!(item.source_hash, expected_hash);
733    }
734
735    #[test]
736    fn unmanaged_disk_path_collision_reported() {
737        let tree = make_source_tree(&[("coder.md", "# managed")], &[]);
738        let (graph, config) = make_graph_and_config(vec![(
739            "base",
740            &tree,
741            Some("https://github.com/org/base"),
742            FilterMode::All,
743        )]);
744
745        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
746        assert!(renames.is_empty());
747        let install_root = TempDir::new().unwrap();
748
749        // Existing user-authored file at the same destination.
750        let existing = install_root.path().join("agents").join("coder.md");
751        fs::create_dir_all(existing.parent().unwrap()).unwrap();
752        fs::write(&existing, "# user-authored").unwrap();
753
754        let collisions =
755            check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target);
756        assert_eq!(collisions.len(), 1);
757        assert_eq!(collisions[0].source_name.as_ref(), "base");
758        assert_eq!(collisions[0].path.as_str(), "agents/coder.md");
759    }
760
761    #[test]
762    fn unmanaged_collision_skipped_when_hash_matches() {
763        let content = "# managed agent";
764        let tree = make_source_tree(&[("coder.md", content)], &[]);
765        let (graph, config) = make_graph_and_config(vec![(
766            "base",
767            &tree,
768            Some("https://github.com/org/base"),
769            FilterMode::All,
770        )]);
771
772        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
773        assert!(renames.is_empty());
774        let install_root = TempDir::new().unwrap();
775
776        // Simulate partial prior install: file on disk with same content
777        let existing = install_root.path().join("agents").join("coder.md");
778        fs::create_dir_all(existing.parent().unwrap()).unwrap();
779        fs::write(&existing, content).unwrap();
780
781        // Should skip collision — disk content matches planned install (crash recovery)
782        let collisions =
783            check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target);
784        assert!(collisions.is_empty());
785    }
786
787    #[test]
788    fn unmanaged_collision_reported_on_different_content() {
789        let tree = make_source_tree(&[("coder.md", "# managed")], &[]);
790        let (graph, config) = make_graph_and_config(vec![(
791            "base",
792            &tree,
793            Some("https://github.com/org/base"),
794            FilterMode::All,
795        )]);
796
797        let (target, renames) = build_with_collisions(&graph, &config).unwrap();
798        assert!(renames.is_empty());
799        let install_root = TempDir::new().unwrap();
800
801        // User-authored file with different content
802        let existing = install_root.path().join("agents").join("coder.md");
803        fs::create_dir_all(existing.parent().unwrap()).unwrap();
804        fs::write(&existing, "# different user content").unwrap();
805
806        let collisions =
807            check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target);
808        assert_eq!(collisions.len(), 1);
809        assert_eq!(collisions[0].source_name.as_ref(), "base");
810        assert_eq!(collisions[0].path.as_str(), "agents/coder.md");
811    }
812}