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 force: bool,
217) -> Vec<UnmanagedCollision> {
218 let mut collisions = Vec::new();
219 let lock_index = LockIndex::new(lock);
220
221 for (dest_key, target_item) in &target.items {
222 if lock_index.contains_output(CANONICAL_TARGET_ROOT, dest_key) {
223 continue;
224 }
225
226 let disk_path = target_item.dest_path.resolve(install_target);
227 if disk_path.exists() {
228 if force {
229 continue;
230 }
231 let hash_path = hash_path_for_kind(&disk_path, target_item.id.kind);
235 if let Ok(disk_hash) = hash::compute_hash(&hash_path, target_item.id.kind)
236 && disk_hash == target_item.source_hash.as_str()
237 {
238 continue;
239 }
240
241 collisions.push(UnmanagedCollision {
242 source_name: target_item.source_name.clone(),
243 path: target_item.dest_path.clone(),
244 });
245 }
246 }
247
248 collisions
249}
250
251fn apply_item_rename(
252 kind: ItemKind,
253 item_name: &str,
254 renames: &RenameMap,
255 source_name: &SourceName,
256) -> Result<(ItemName, DestPath), MarsError> {
257 let default_dest = default_dest_path(kind, item_name);
258 let default_key = default_dest.as_str();
259
260 let rename_value = renames.get(default_key).or_else(|| renames.get(item_name));
261
262 let dest_path = match rename_value {
263 Some(value) => parse_rename_dest(kind, value.as_str(), source_name)?,
264 None => default_dest,
265 };
266 let dest_name = dest_name_from_dest(&dest_path, kind);
267
268 Ok((ItemName::from(dest_name), dest_path))
269}
270
271fn default_dest_path(kind: ItemKind, name: &str) -> DestPath {
274 let path_str = match kind {
275 ItemKind::Agent => format!("agents/{name}.md"),
276 ItemKind::Skill => format!("skills/{name}"),
277 ItemKind::Hook => format!("hooks/{name}"),
278 ItemKind::McpServer => format!("mcp/{name}"),
279 ItemKind::BootstrapDoc => format!("bootstrap/{name}/BOOTSTRAP.md"),
280 };
281 DestPath::new(path_str).expect("internal default path is always valid")
283}
284
285fn parse_rename_dest(
286 kind: ItemKind,
287 rename_value: &str,
288 source_name: &SourceName,
289) -> Result<DestPath, MarsError> {
290 let normalized = rename_value.replace('\\', "/");
292 let has_prefix = normalized.starts_with("agents/")
293 || normalized.starts_with("skills/")
294 || normalized.starts_with("hooks/")
295 || normalized.starts_with("mcp/")
296 || normalized.starts_with("bootstrap/");
297 let has_parent = normalized.contains('/');
298
299 if has_prefix || has_parent {
300 let dest = if kind == ItemKind::BootstrapDoc && !normalized.ends_with("/BOOTSTRAP.md") {
301 format!("{normalized}/BOOTSTRAP.md")
302 } else {
303 normalized.clone()
304 };
305 return DestPath::new(&dest).map_err(|e| MarsError::Source {
306 source_name: source_name.to_string(),
307 message: format!("invalid rename destination `{rename_value}`: {e}"),
308 });
309 }
310
311 let path_str = match kind {
312 ItemKind::Agent => {
313 if normalized.ends_with(".md") {
314 format!("agents/{normalized}")
315 } else {
316 format!("agents/{normalized}.md")
317 }
318 }
319 ItemKind::Skill => format!("skills/{normalized}"),
320 ItemKind::Hook => format!("hooks/{normalized}"),
321 ItemKind::McpServer => format!("mcp/{normalized}"),
322 ItemKind::BootstrapDoc => format!("bootstrap/{normalized}/BOOTSTRAP.md"),
323 };
324 DestPath::new(path_str).map_err(|e| MarsError::Source {
325 source_name: source_name.to_string(),
326 message: format!("invalid rename destination `{rename_value}`: {e}"),
327 })
328}
329
330fn dest_name_from_dest(dest_path: &DestPath, kind: ItemKind) -> String {
331 match kind {
332 ItemKind::BootstrapDoc => dest_path.item_name(kind),
333 _ => {
334 let last = dest_path.as_str().rsplit('/').next().unwrap_or("");
335 match kind {
336 ItemKind::Agent => last.strip_suffix(".md").unwrap_or(last).to_string(),
337 ItemKind::Skill | ItemKind::Hook | ItemKind::McpServer => last.to_string(),
338 ItemKind::BootstrapDoc => unreachable!("handled above"),
339 }
340 }
341 }
342}
343
344fn hash_path_for_kind(path: &Path, kind: ItemKind) -> PathBuf {
345 if kind == ItemKind::BootstrapDoc {
346 path.parent()
347 .map(Path::to_path_buf)
348 .unwrap_or_else(|| path.to_path_buf())
349 } else {
350 path.to_path_buf()
351 }
352}
353
354#[cfg(test)]
355mod tests {
356 use super::*;
357 use crate::config::*;
358 use crate::lock::LockFile;
359 use crate::resolve::{ResolvedGraph, ResolvedNode};
360 use crate::source::ResolvedRef;
361 use indexmap::IndexMap;
362 use std::fs;
363 use tempfile::TempDir;
364
365 fn make_source_tree(agents: &[(&str, &str)], skills: &[(&str, &str)]) -> TempDir {
367 let dir = TempDir::new().unwrap();
368 if !agents.is_empty() {
369 let agents_dir = dir.path().join("agents");
370 fs::create_dir_all(&agents_dir).unwrap();
371 for (name, content) in agents {
372 fs::write(agents_dir.join(name), content).unwrap();
373 }
374 }
375 if !skills.is_empty() {
376 let skills_dir = dir.path().join("skills");
377 fs::create_dir_all(&skills_dir).unwrap();
378 for (name, content) in skills {
379 let skill_dir = skills_dir.join(name);
380 fs::create_dir_all(&skill_dir).unwrap();
381 fs::write(skill_dir.join("SKILL.md"), content).unwrap();
382 }
383 }
384 dir
385 }
386
387 fn make_graph_and_config(
388 sources: Vec<(&str, &TempDir, Option<&str>, FilterMode)>,
389 ) -> (ResolvedGraph, EffectiveConfig) {
390 let mut nodes = IndexMap::new();
391 let mut order = Vec::new();
392 let mut config_dependencies = IndexMap::new();
393
394 for (name, tree, url, filter) in sources {
395 let url_str = url.map(|u| u.to_string());
396 nodes.insert(
397 name.into(),
398 ResolvedNode {
399 source_name: name.into(),
400 source_id: if let Some(u) = url {
401 SourceId::git(crate::types::SourceUrl::from(u))
402 } else {
403 SourceId::Path {
404 canonical: tree.path().to_path_buf(),
405 subpath: None,
406 }
407 },
408 rooted_ref: crate::resolve::RootedSourceRef {
409 checkout_root: tree.path().to_path_buf(),
410 package_root: tree.path().to_path_buf(),
411 },
412 resolved_ref: ResolvedRef {
413 source_name: name.into(),
414 version: None,
415 version_tag: None,
416 commit: None,
417 tree_path: tree.path().to_path_buf(),
418 },
419 latest_version: None,
420 manifest: None,
421 deps: vec![],
422 },
423 );
424 order.push(name.into());
425
426 let spec = if let Some(u) = url {
427 SourceSpec::Git(GitSpec {
428 url: crate::types::SourceUrl::from(u),
429 version: None,
430 })
431 } else {
432 SourceSpec::Path(tree.path().to_path_buf())
433 };
434
435 config_dependencies.insert(
436 name.into(),
437 EffectiveDependency {
438 name: name.into(),
439 id: if let Some(u) = url {
440 SourceId::git(crate::types::SourceUrl::from(u))
441 } else {
442 SourceId::Path {
443 canonical: tree.path().to_path_buf(),
444 subpath: None,
445 }
446 },
447 spec,
448 subpath: None,
449 filter,
450 rename: RenameMap::new(),
451 is_overridden: false,
452 original_git: url_str.map(|u| GitSpec {
453 url: crate::types::SourceUrl::from(u),
454 version: None,
455 }),
456 },
457 );
458 }
459
460 let graph = ResolvedGraph {
461 nodes,
462 order,
463 filters: std::collections::HashMap::new(),
464 version_constraints: std::collections::HashMap::new(),
465 };
466 let config = EffectiveConfig {
467 dependencies: config_dependencies,
468 settings: Settings::default(),
469 };
470 (graph, config)
471 }
472
473 #[test]
476 fn build_single_source_no_filter() {
477 let tree = make_source_tree(&[("coder.md", "# coder")], &[("planning", "# planning")]);
478 let (graph, config) = make_graph_and_config(vec![(
479 "base",
480 &tree,
481 Some("https://github.com/org/base"),
482 FilterMode::All,
483 )]);
484
485 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
486 assert!(renames.is_empty());
487 assert_eq!(target.items.len(), 2);
488 assert!(target.items.contains_key("agents/coder.md"));
489 assert!(target.items.contains_key("skills/planning"));
490 }
491
492 #[test]
493 #[cfg(not(target_os = "windows"))]
494 fn invalid_windows_agent_filename_emits_diagnostic_and_skips() {
495 let tree = make_source_tree(&[("bad:name.md", "# bad"), ("coder.md", "# coder")], &[]);
499 let (graph, config) = make_graph_and_config(vec![(
500 "base",
501 &tree,
502 Some("https://github.com/org/base"),
503 FilterMode::All,
504 )]);
505 let mut diag = DiagnosticCollector::new();
506
507 let (target, _) = build_with_collisions_and_diag(&graph, &config, &mut diag).unwrap();
508 let diagnostics = diag.drain();
509
510 assert!(!target.items.contains_key("agents/bad:name.md"));
511 assert!(target.items.contains_key("agents/coder.md"));
512 assert_eq!(diagnostics.len(), 1);
513 assert_eq!(diagnostics[0].code, "invalid-agent-filename");
514 }
515
516 #[test]
517 fn build_with_path_rename_mapping() {
518 let tree = make_source_tree(&[("old-name.md", "# old")], &[]);
519
520 let (graph, mut config) = make_graph_and_config(vec![(
521 "base",
522 &tree,
523 Some("https://github.com/org/base"),
524 FilterMode::All,
525 )]);
526
527 config
529 .dependencies
530 .get_mut("base")
531 .unwrap()
532 .rename
533 .insert("agents/old-name.md".into(), "agents/new-name.md".into());
534
535 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
536 assert!(renames.is_empty());
537 assert_eq!(target.items.len(), 1);
538 assert!(target.items.contains_key("agents/new-name.md"));
539 assert_eq!(target.items["agents/new-name.md"].id.name, "new-name");
540 }
541
542 #[test]
543 fn default_dest_path_uses_forward_slashes_for_agents_and_skills() {
544 let agent = default_dest_path(ItemKind::Agent, "coder");
545 let skill = default_dest_path(ItemKind::Skill, "planning");
546
547 assert_eq!(agent.as_str(), "agents/coder.md");
548 assert_eq!(skill.as_str(), "skills/planning");
549 assert!(!agent.as_str().contains('\\'));
550 assert!(!skill.as_str().contains('\\'));
551 }
552
553 #[test]
554 fn parse_rename_dest_normalizes_backslashes_to_forward_slashes() {
555 let source_name = SourceName::from("base");
556
557 let agent =
558 parse_rename_dest(ItemKind::Agent, r"agents\nested\renamed.md", &source_name).unwrap();
559 let skill =
560 parse_rename_dest(ItemKind::Skill, r"skills\nested\planning", &source_name).unwrap();
561
562 assert_eq!(agent.as_str(), "agents/nested/renamed.md");
563 assert_eq!(skill.as_str(), "skills/nested/planning");
564 assert!(!agent.as_str().contains('\\'));
565 assert!(!skill.as_str().contains('\\'));
566 }
567
568 #[test]
569 fn parse_rename_dest_rejects_absolute_and_escape_destinations() {
570 let source_name = SourceName::from("base");
571
572 let absolute = parse_rename_dest(ItemKind::Agent, "/tmp/escape", &source_name)
573 .expect_err("absolute rename should fail");
574 assert!(matches!(absolute, MarsError::Source { .. }));
575
576 let traversal = parse_rename_dest(ItemKind::Skill, "../escape", &source_name)
577 .expect_err("traversal rename should fail");
578 assert!(matches!(traversal, MarsError::Source { .. }));
579 }
580
581 #[test]
582 fn build_with_invalid_rename_destination_returns_error() {
583 let tree = make_source_tree(&[("old-name.md", "# old")], &[]);
584
585 let (graph, mut config) =
586 make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
587
588 config
589 .dependencies
590 .get_mut("base")
591 .unwrap()
592 .rename
593 .insert("agents/old-name.md".into(), "../escape.md".into());
594
595 let err = build_with_collisions(&graph, &config).unwrap_err();
596 assert!(matches!(err, MarsError::Source { .. }));
597 }
598
599 #[test]
602 fn collision_errors_instead_of_auto_renaming() {
603 let tree1 = make_source_tree(&[("coder.md", "# coder from source 1")], &[]);
604 let tree2 = make_source_tree(&[("coder.md", "# coder from source 2")], &[]);
605
606 let (graph, config) = make_graph_and_config(vec![
607 (
608 "source-a",
609 &tree1,
610 Some("https://github.com/alice/agents"),
611 FilterMode::All,
612 ),
613 (
614 "source-b",
615 &tree2,
616 Some("https://github.com/bob/agents"),
617 FilterMode::All,
618 ),
619 ]);
620
621 let err = build_with_collisions(&graph, &config).unwrap_err();
622 assert!(matches!(err, MarsError::Collision { .. }));
623 }
624
625 #[test]
626 fn no_collision_no_renames() {
627 let tree1 = make_source_tree(&[("coder.md", "# coder")], &[]);
628 let tree2 = make_source_tree(&[("reviewer.md", "# reviewer")], &[]);
629
630 let (graph, config) = make_graph_and_config(vec![
631 (
632 "source-a",
633 &tree1,
634 Some("https://github.com/alice/agents"),
635 FilterMode::All,
636 ),
637 (
638 "source-b",
639 &tree2,
640 Some("https://github.com/bob/agents"),
641 FilterMode::All,
642 ),
643 ]);
644
645 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
646 assert!(renames.is_empty());
647 assert_eq!(target.items.len(), 2);
648 }
649
650 #[test]
653 fn build_with_agents_filter_pulls_transitive_skills() {
654 let tree = make_source_tree(
655 &[("coder.md", "---\nskills:\n - planning\n---\n# Coder\n")],
656 &[("planning", "# Planning"), ("unused-skill", "# Unused")],
657 );
658
659 let (graph, config) = make_graph_and_config(vec![(
660 "base",
661 &tree,
662 None,
663 FilterMode::Include {
664 agents: vec!["coder".into()],
665 skills: vec![],
666 },
667 )]);
668
669 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
670 assert!(renames.is_empty());
671 assert_eq!(target.items.len(), 2); assert!(target.items.contains_key("agents/coder.md"));
673 assert!(target.items.contains_key("skills/planning"));
674 assert!(!target.items.contains_key("skills/unused-skill"));
676 }
677
678 #[test]
679 fn build_with_exclude_filter() {
680 let tree = make_source_tree(&[("coder.md", "# coder"), ("deprecated.md", "# old")], &[]);
681
682 let (graph, config) = make_graph_and_config(vec![(
683 "base",
684 &tree,
685 None,
686 FilterMode::Exclude(vec!["deprecated".into()]),
687 )]);
688
689 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
690 assert!(renames.is_empty());
691 assert_eq!(target.items.len(), 1);
692 assert!(target.items.contains_key("agents/coder.md"));
693 }
694
695 #[test]
696 fn build_unions_multiple_include_filters_for_same_source() {
697 let tree = make_source_tree(
698 &[],
699 &[
700 ("skill-a", "# Skill A"),
701 ("skill-b", "# Skill B"),
702 ("skill-c", "# Skill C"),
703 ],
704 );
705
706 let (mut graph, config) =
707 make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
708 graph.filters.insert(
709 "base".into(),
710 vec![
711 FilterMode::Include {
712 agents: vec![],
713 skills: vec!["skill-a".into(), "skill-b".into()],
714 },
715 FilterMode::Include {
716 agents: vec![],
717 skills: vec!["skill-b".into(), "skill-c".into()],
718 },
719 ],
720 );
721
722 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
723 assert!(renames.is_empty());
724 assert_eq!(target.items.len(), 3);
725 assert!(target.items.contains_key("skills/skill-a"));
726 assert!(target.items.contains_key("skills/skill-b"));
727 assert!(target.items.contains_key("skills/skill-c"));
728 }
729
730 #[test]
731 fn build_target_items_have_correct_hashes() {
732 let content = "# agent content for hash test";
733 let tree = make_source_tree(&[("test.md", content)], &[]);
734
735 let (graph, config) = make_graph_and_config(vec![("base", &tree, None, FilterMode::All)]);
736
737 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
738 assert!(renames.is_empty());
739 let item = &target.items["agents/test.md"];
740 let expected_hash = hash::hash_bytes(content.as_bytes());
741 assert_eq!(item.source_hash, expected_hash);
742 }
743
744 #[test]
745 fn unmanaged_disk_path_collision_reported() {
746 let tree = make_source_tree(&[("coder.md", "# managed")], &[]);
747 let (graph, config) = make_graph_and_config(vec![(
748 "base",
749 &tree,
750 Some("https://github.com/org/base"),
751 FilterMode::All,
752 )]);
753
754 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
755 assert!(renames.is_empty());
756 let install_root = TempDir::new().unwrap();
757
758 let existing = install_root.path().join("agents").join("coder.md");
760 fs::create_dir_all(existing.parent().unwrap()).unwrap();
761 fs::write(&existing, "# user-authored").unwrap();
762
763 let collisions =
764 check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target, false);
765 assert_eq!(collisions.len(), 1);
766 assert_eq!(collisions[0].source_name.as_ref(), "base");
767 assert_eq!(collisions[0].path.as_str(), "agents/coder.md");
768 }
769
770 #[test]
771 fn unmanaged_collision_skipped_when_hash_matches() {
772 let content = "# managed agent";
773 let tree = make_source_tree(&[("coder.md", content)], &[]);
774 let (graph, config) = make_graph_and_config(vec![(
775 "base",
776 &tree,
777 Some("https://github.com/org/base"),
778 FilterMode::All,
779 )]);
780
781 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
782 assert!(renames.is_empty());
783 let install_root = TempDir::new().unwrap();
784
785 let existing = install_root.path().join("agents").join("coder.md");
787 fs::create_dir_all(existing.parent().unwrap()).unwrap();
788 fs::write(&existing, content).unwrap();
789
790 let collisions =
792 check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target, false);
793 assert!(collisions.is_empty());
794 }
795
796 #[test]
797 fn unmanaged_collision_reported_on_different_content() {
798 let tree = make_source_tree(&[("coder.md", "# managed")], &[]);
799 let (graph, config) = make_graph_and_config(vec![(
800 "base",
801 &tree,
802 Some("https://github.com/org/base"),
803 FilterMode::All,
804 )]);
805
806 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
807 assert!(renames.is_empty());
808 let install_root = TempDir::new().unwrap();
809
810 let existing = install_root.path().join("agents").join("coder.md");
812 fs::create_dir_all(existing.parent().unwrap()).unwrap();
813 fs::write(&existing, "# different user content").unwrap();
814
815 let collisions =
816 check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target, false);
817 assert_eq!(collisions.len(), 1);
818 assert_eq!(collisions[0].source_name.as_ref(), "base");
819 assert_eq!(collisions[0].path.as_str(), "agents/coder.md");
820 }
821
822 #[test]
823 fn unmanaged_collision_skipped_under_force() {
824 let tree = make_source_tree(&[("coder.md", "# managed")], &[]);
825 let (graph, config) = make_graph_and_config(vec![(
826 "base",
827 &tree,
828 Some("https://github.com/org/base"),
829 FilterMode::All,
830 )]);
831
832 let (target, renames) = build_with_collisions(&graph, &config).unwrap();
833 assert!(renames.is_empty());
834 let install_root = TempDir::new().unwrap();
835
836 let existing = install_root.path().join("agents").join("coder.md");
837 fs::create_dir_all(existing.parent().unwrap()).unwrap();
838 fs::write(&existing, "# stale cache content").unwrap();
839
840 let collisions =
841 check_unmanaged_collisions(install_root.path(), &LockFile::empty(), &target, true);
842 assert!(collisions.is_empty());
843 }
844}