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