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::{CANONICAL_TARGET_ROOT, 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#[derive(Debug, Clone)]
22pub struct TargetState {
23 pub items: IndexMap<DestPath, TargetItem>,
25}
26
27#[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 pub source_path: PathBuf,
36 pub dest_path: DestPath,
38 pub source_hash: ContentHash,
40 pub is_flat_skill: bool,
42 pub rewritten_content: Option<String>,
44}
45
46#[derive(Debug, Clone)]
48pub struct ExplicitSkillRename {
49 pub original_name: ItemName,
50 pub new_name: ItemName,
51 pub source_name: SourceName,
52}
53
54pub 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 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
197pub use crate::sync::rewrite::rewrite_skill_refs;
199
200#[derive(Debug, Clone, PartialEq, Eq)]
202pub struct UnmanagedCollision {
203 pub source_name: SourceName,
204 pub path: DestPath,
205}
206
207pub 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_output(CANONICAL_TARGET_ROOT, dest_key) {
222 continue;
223 }
224
225 let disk_path = target_item.dest_path.resolve(install_target);
226 if disk_path.exists() {
227 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
267fn 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 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 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 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 version_constraints: std::collections::HashMap::new(),
461 };
462 let config = EffectiveConfig {
463 dependencies: config_dependencies,
464 settings: Settings::default(),
465 };
466 (graph, config)
467 }
468
469 #[test]
472 fn build_single_source_no_filter() {
473 let tree = make_source_tree(&[("coder.md", "# coder")], &[("planning", "# planning")]);
474 let (graph, config) = make_graph_and_config(vec![(
475 "base",
476 &tree,
477 Some("https://github.com/org/base"),
478 FilterMode::All,
479 )]);
480
481 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
482 assert!(renames.is_empty());
483 assert_eq!(target.items.len(), 2);
484 assert!(target.items.contains_key("agents/coder.md"));
485 assert!(target.items.contains_key("skills/planning"));
486 }
487
488 #[test]
489 #[cfg(not(target_os = "windows"))]
490 fn invalid_windows_agent_filename_emits_diagnostic_and_skips() {
491 let tree = make_source_tree(&[("bad:name.md", "# bad"), ("coder.md", "# coder")], &[]);
495 let (graph, config) = make_graph_and_config(vec![(
496 "base",
497 &tree,
498 Some("https://github.com/org/base"),
499 FilterMode::All,
500 )]);
501 let mut diag = DiagnosticCollector::new();
502
503 let (target, _) = build_with_collisions_and_diag(&graph, &config, &mut diag).unwrap();
504 let diagnostics = diag.drain();
505
506 assert!(!target.items.contains_key("agents/bad:name.md"));
507 assert!(target.items.contains_key("agents/coder.md"));
508 assert_eq!(diagnostics.len(), 1);
509 assert_eq!(diagnostics[0].code, "invalid-agent-filename");
510 }
511
512 #[test]
513 fn build_with_path_rename_mapping() {
514 let tree = make_source_tree(&[("old-name.md", "# old")], &[]);
515
516 let (graph, mut config) = make_graph_and_config(vec![(
517 "base",
518 &tree,
519 Some("https://github.com/org/base"),
520 FilterMode::All,
521 )]);
522
523 config
525 .dependencies
526 .get_mut("base")
527 .unwrap()
528 .rename
529 .insert("agents/old-name.md".into(), "agents/new-name.md".into());
530
531 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
532 assert!(renames.is_empty());
533 assert_eq!(target.items.len(), 1);
534 assert!(target.items.contains_key("agents/new-name.md"));
535 assert_eq!(target.items["agents/new-name.md"].id.name, "new-name");
536 }
537
538 #[test]
539 fn default_dest_path_uses_forward_slashes_for_agents_and_skills() {
540 let agent = default_dest_path(ItemKind::Agent, "coder");
541 let skill = default_dest_path(ItemKind::Skill, "planning");
542
543 assert_eq!(agent.as_str(), "agents/coder.md");
544 assert_eq!(skill.as_str(), "skills/planning");
545 assert!(!agent.as_str().contains('\\'));
546 assert!(!skill.as_str().contains('\\'));
547 }
548
549 #[test]
550 fn parse_rename_dest_normalizes_backslashes_to_forward_slashes() {
551 let source_name = SourceName::from("base");
552
553 let agent =
554 parse_rename_dest(ItemKind::Agent, r"agents\nested\renamed.md", &source_name).unwrap();
555 let skill =
556 parse_rename_dest(ItemKind::Skill, r"skills\nested\planning", &source_name).unwrap();
557
558 assert_eq!(agent.as_str(), "agents/nested/renamed.md");
559 assert_eq!(skill.as_str(), "skills/nested/planning");
560 assert!(!agent.as_str().contains('\\'));
561 assert!(!skill.as_str().contains('\\'));
562 }
563
564 #[test]
565 fn parse_rename_dest_rejects_absolute_and_escape_destinations() {
566 let source_name = SourceName::from("base");
567
568 let absolute = parse_rename_dest(ItemKind::Agent, "/tmp/escape", &source_name)
569 .expect_err("absolute rename should fail");
570 assert!(matches!(absolute, MarsError::Source { .. }));
571
572 let traversal = parse_rename_dest(ItemKind::Skill, "../escape", &source_name)
573 .expect_err("traversal rename should fail");
574 assert!(matches!(traversal, MarsError::Source { .. }));
575 }
576
577 #[test]
578 fn build_with_invalid_rename_destination_returns_error() {
579 let tree = make_source_tree(&[("old-name.md", "# old")], &[]);
580
581 let (graph, mut config) =
582 make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
583
584 config
585 .dependencies
586 .get_mut("base")
587 .unwrap()
588 .rename
589 .insert("agents/old-name.md".into(), "../escape.md".into());
590
591 let err = build_with_collisions(&graph, &config).unwrap_err();
592 assert!(matches!(err, MarsError::Source { .. }));
593 }
594
595 #[test]
598 fn collision_errors_instead_of_auto_renaming() {
599 let tree1 = make_source_tree(&[("coder.md", "# coder from source 1")], &[]);
600 let tree2 = make_source_tree(&[("coder.md", "# coder from source 2")], &[]);
601
602 let (graph, config) = make_graph_and_config(vec![
603 (
604 "source-a",
605 &tree1,
606 Some("https://github.com/alice/agents"),
607 FilterMode::All,
608 ),
609 (
610 "source-b",
611 &tree2,
612 Some("https://github.com/bob/agents"),
613 FilterMode::All,
614 ),
615 ]);
616
617 let err = build_with_collisions(&graph, &config).unwrap_err();
618 assert!(matches!(err, MarsError::Collision { .. }));
619 }
620
621 #[test]
622 fn no_collision_no_renames() {
623 let tree1 = make_source_tree(&[("coder.md", "# coder")], &[]);
624 let tree2 = make_source_tree(&[("reviewer.md", "# reviewer")], &[]);
625
626 let (graph, config) = make_graph_and_config(vec![
627 (
628 "source-a",
629 &tree1,
630 Some("https://github.com/alice/agents"),
631 FilterMode::All,
632 ),
633 (
634 "source-b",
635 &tree2,
636 Some("https://github.com/bob/agents"),
637 FilterMode::All,
638 ),
639 ]);
640
641 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
642 assert!(renames.is_empty());
643 assert_eq!(target.items.len(), 2);
644 }
645
646 #[test]
649 fn build_with_agents_filter_pulls_transitive_skills() {
650 let tree = make_source_tree(
651 &[("coder.md", "---\nskills:\n - planning\n---\n# Coder\n")],
652 &[("planning", "# Planning"), ("unused-skill", "# Unused")],
653 );
654
655 let (graph, config) = make_graph_and_config(vec![(
656 "base",
657 &tree,
658 None,
659 FilterMode::Include {
660 agents: vec!["coder".into()],
661 skills: vec![],
662 },
663 )]);
664
665 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
666 assert!(renames.is_empty());
667 assert_eq!(target.items.len(), 2); assert!(target.items.contains_key("agents/coder.md"));
669 assert!(target.items.contains_key("skills/planning"));
670 assert!(!target.items.contains_key("skills/unused-skill"));
672 }
673
674 #[test]
675 fn build_with_exclude_filter() {
676 let tree = make_source_tree(&[("coder.md", "# coder"), ("deprecated.md", "# old")], &[]);
677
678 let (graph, config) = make_graph_and_config(vec![(
679 "base",
680 &tree,
681 None,
682 FilterMode::Exclude(vec!["deprecated".into()]),
683 )]);
684
685 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
686 assert!(renames.is_empty());
687 assert_eq!(target.items.len(), 1);
688 assert!(target.items.contains_key("agents/coder.md"));
689 }
690
691 #[test]
692 fn build_unions_multiple_include_filters_for_same_source() {
693 let tree = make_source_tree(
694 &[],
695 &[
696 ("skill-a", "# Skill A"),
697 ("skill-b", "# Skill B"),
698 ("skill-c", "# Skill C"),
699 ],
700 );
701
702 let (mut graph, config) =
703 make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
704 graph.filters.insert(
705 "base".into(),
706 vec![
707 FilterMode::Include {
708 agents: vec![],
709 skills: vec!["skill-a".into(), "skill-b".into()],
710 },
711 FilterMode::Include {
712 agents: vec![],
713 skills: vec!["skill-b".into(), "skill-c".into()],
714 },
715 ],
716 );
717
718 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
719 assert!(renames.is_empty());
720 assert_eq!(target.items.len(), 3);
721 assert!(target.items.contains_key("skills/skill-a"));
722 assert!(target.items.contains_key("skills/skill-b"));
723 assert!(target.items.contains_key("skills/skill-c"));
724 }
725
726 #[test]
727 fn build_target_items_have_correct_hashes() {
728 let content = "# agent content for hash test";
729 let tree = make_source_tree(&[("test.md", content)], &[]);
730
731 let (graph, config) = make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
732
733 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
734 assert!(renames.is_empty());
735 let item = &target.items["agents/test.md"];
736 let expected_hash = hash::hash_bytes(content.as_bytes());
737 assert_eq!(item.source_hash, expected_hash);
738 }
739
740 #[test]
741 fn unmanaged_disk_path_collision_reported() {
742 let tree = make_source_tree(&[("coder.md", "# managed")], &[]);
743 let (graph, config) = make_graph_and_config(vec![(
744 "base",
745 &tree,
746 Some("https://github.com/org/base"),
747 FilterMode::All,
748 )]);
749
750 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
751 assert!(renames.is_empty());
752 let install_root = TempDir::new().unwrap();
753
754 let existing = install_root.path().join("agents").join("coder.md");
756 fs::create_dir_all(existing.parent().unwrap()).unwrap();
757 fs::write(&existing, "# user-authored").unwrap();
758
759 let collisions =
760 check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target);
761 assert_eq!(collisions.len(), 1);
762 assert_eq!(collisions[0].source_name.as_ref(), "base");
763 assert_eq!(collisions[0].path.as_str(), "agents/coder.md");
764 }
765
766 #[test]
767 fn unmanaged_collision_skipped_when_hash_matches() {
768 let content = "# managed agent";
769 let tree = make_source_tree(&[("coder.md", content)], &[]);
770 let (graph, config) = make_graph_and_config(vec![(
771 "base",
772 &tree,
773 Some("https://github.com/org/base"),
774 FilterMode::All,
775 )]);
776
777 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
778 assert!(renames.is_empty());
779 let install_root = TempDir::new().unwrap();
780
781 let existing = install_root.path().join("agents").join("coder.md");
783 fs::create_dir_all(existing.parent().unwrap()).unwrap();
784 fs::write(&existing, content).unwrap();
785
786 let collisions =
788 check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target);
789 assert!(collisions.is_empty());
790 }
791
792 #[test]
793 fn unmanaged_collision_reported_on_different_content() {
794 let tree = make_source_tree(&[("coder.md", "# managed")], &[]);
795 let (graph, config) = make_graph_and_config(vec![(
796 "base",
797 &tree,
798 Some("https://github.com/org/base"),
799 FilterMode::All,
800 )]);
801
802 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
803 assert!(renames.is_empty());
804 let install_root = TempDir::new().unwrap();
805
806 let existing = install_root.path().join("agents").join("coder.md");
808 fs::create_dir_all(existing.parent().unwrap()).unwrap();
809 fs::write(&existing, "# different user content").unwrap();
810
811 let collisions =
812 check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target);
813 assert_eq!(collisions.len(), 1);
814 assert_eq!(collisions[0].source_name.as_ref(), "base");
815 assert_eq!(collisions[0].path.as_str(), "agents/coder.md");
816 }
817}