1use crate::config::{Config, EnvFn};
18use std::path::PathBuf;
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum Kind {
24 Plugin,
27 Skill,
29 Context,
32 Config,
34}
35
36impl Kind {
37 pub fn as_str(self) -> &'static str {
39 match self {
40 Kind::Plugin => "plugin",
41 Kind::Skill => "skill",
42 Kind::Context => "context",
43 Kind::Config => "config",
44 }
45 }
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct Problem {
51 pub kind: Kind,
52 pub text: String,
53 pub path: Option<PathBuf>,
55}
56
57impl Problem {
58 pub fn new(kind: Kind, text: impl Into<String>) -> Self {
59 Self {
60 kind,
61 text: text.into(),
62 path: None,
63 }
64 }
65
66 pub fn at(mut self, path: impl Into<PathBuf>) -> Self {
67 self.path = Some(path.into());
68 self
69 }
70}
71
72#[derive(Debug, Clone, Default, PartialEq, Eq)]
74pub struct Report {
75 pub problems: Vec<Problem>,
76 pub checked: Vec<String>,
79}
80
81impl Report {
82 pub fn is_empty(&self) -> bool {
83 self.problems.is_empty()
84 }
85
86 pub fn count(&self, kind: Kind) -> usize {
88 self.problems.iter().filter(|p| p.kind == kind).count()
89 }
90
91 pub fn summary(&self) -> String {
93 if self.problems.is_empty() {
94 format!(
95 "nothing to fix — {} thing(s) checked and all of them loaded",
96 self.checked.len()
97 )
98 } else {
99 format!(
100 "{} problem(s): {} plugin, {} skill, {} context, {} config",
101 self.problems.len(),
102 self.count(Kind::Plugin),
103 self.count(Kind::Skill),
104 self.count(Kind::Context),
105 self.count(Kind::Config)
106 )
107 }
108 }
109}
110
111pub fn collect(cfg: &Config, env: EnvFn) -> Report {
117 let workspace = cfg.workspace_root(env);
118 let plugin_dirs = cfg.plugins_dirs(env);
119 let loaded = crate::plugins::load(&plugin_dirs, &cfg.plugins.enabled)
120 .with_scripts_dir(cfg.scripts_dir(env))
121 .with_mcp(crate::plugins::McpSettings::from_config(cfg))
122 .with_config_mcp(cfg);
123 let mut report = Report::default();
124
125 for dir in &plugin_dirs {
128 report
129 .checked
130 .push(format!("plugin dir: {}", dir.display()));
131 }
132 for problem in &loaded.problems {
133 report
134 .problems
135 .push(Problem::new(Kind::Plugin, problem.clone()));
136 }
137 for plugin in &loaded.plugins {
138 report.checked.push(format!("plugin:{}", plugin.name));
139 }
140
141 let roots = crate::skills::roots_for(cfg, env, &loaded, &workspace);
142 for root in &roots {
143 report
144 .checked
145 .push(format!("skill root: {}", root.dir.display()));
146 }
147 let skills = crate::skills::discover(&roots);
148 for problem in &skills.problems {
149 report
150 .problems
151 .push(Problem::new(Kind::Skill, problem.clone()));
152 }
153 for broken in skills.broken() {
154 let why = match &broken.state {
157 Ok(_) => continue,
158 Err(why) => why.clone(),
159 };
160 report.problems.push(
161 Problem::new(Kind::Skill, format!("{}: {why}", broken.name)).at(broken.path.clone()),
162 );
163 }
164 for (found, _) in skills.active() {
165 report.checked.push(format!("skill:{}", found.name));
166 }
167 for shadowed in skills.found.iter().filter(|f| !f.shadows.is_empty()) {
168 report.checked.push(format!(
169 "skill:{} (won the name in {})",
170 shadowed.name,
171 shadowed.dir.display()
172 ));
173 }
174
175 let ctx = crate::context_files::for_config(cfg, env, &loaded);
176 for note in &ctx.skill_notes {
177 report
178 .problems
179 .push(Problem::new(Kind::Context, note.clone()));
180 }
181 for layer in ctx
184 .instructions
185 .iter()
186 .chain(ctx.user.iter())
187 .chain(ctx.memory.iter())
188 {
189 report
190 .checked
191 .push(format!("context:{}", layer.path.display()));
192 }
193
194 report
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 fn config_with(home: &std::path::Path) -> Config {
202 let env = [("HOME".to_string(), home.display().to_string())];
203 let lookup = move |k: &str| env.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
204 Config::load_with_profile(None, None, &lookup).expect("a config")
205 }
206
207 #[test]
208 fn a_directory_that_is_not_there_is_not_a_problem() {
209 let dir = tempfile::tempdir().unwrap();
210 let home = dir.path().join("home");
211 std::fs::create_dir_all(home.join(".eclipse")).unwrap();
212 let cfg = config_with(&home);
213 let env = [("HOME".to_string(), home.display().to_string())];
214 let lookup = move |k: &str| env.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
215 let report = collect(&cfg, &lookup);
216 assert!(
217 report.is_empty(),
218 "an empty install has nothing to fix: {:?}",
219 report.problems
220 );
221 assert!(report.summary().contains("nothing to fix"));
222 assert!(
223 report.checked.iter().any(|c| c.starts_with("skill root:")),
224 "the readout says what was searched, even when it found nothing: {:?}",
225 report.checked
226 );
227 }
228
229 #[test]
230 fn a_broken_skill_and_a_broken_manifest_both_appear() {
231 let dir = tempfile::tempdir().unwrap();
232 let home = dir.path().join("home");
233 let config_dir = home.join(".eclipse");
234 std::fs::create_dir_all(config_dir.join("skills/broken")).unwrap();
235 std::fs::write(
237 config_dir.join("skills/broken/SKILL.md"),
238 "just some text with no frontmatter\n",
239 )
240 .unwrap();
241 std::fs::write(
245 config_dir.join("config.toml"),
246 "[plugins]\nenabled = [\"bad\"]\n",
247 )
248 .unwrap();
249 std::fs::create_dir_all(config_dir.join("plugins/bad")).unwrap();
250 std::fs::write(config_dir.join("plugins/bad/plugin.toml"), "name = \n").unwrap();
251
252 let cfg = config_with(&home);
253 let env = [("HOME".to_string(), home.display().to_string())];
254 let lookup = move |k: &str| env.iter().find(|(n, _)| n == k).map(|(_, v)| v.clone());
255 let report = collect(&cfg, &lookup);
256 assert!(
257 report.count(Kind::Skill) >= 1,
258 "the broken skill is reported: {:?}",
259 report.problems
260 );
261 assert!(
262 report.count(Kind::Plugin) >= 1,
263 "the broken manifest is reported: {:?}",
264 report.problems
265 );
266 let summary = report.summary();
267 assert!(summary.contains("skill"), "{summary}");
268 assert!(summary.contains("plugin"), "{summary}");
269 std::fs::write(
271 config_dir.join("skills/broken/SKILL.md"),
272 "---\nname: broken\ndescription: now valid\n---\n\nbody\n",
273 )
274 .unwrap();
275 std::fs::write(
276 config_dir.join("plugins/bad/plugin.toml"),
277 "name = \"bad\"\nversion = \"0.1.0\"\n",
278 )
279 .unwrap();
280 let after = collect(&cfg, &lookup);
281 assert!(
282 after.is_empty(),
283 "both are gone once fixed: {:?}",
284 after.problems
285 );
286 }
287
288 #[test]
289 fn a_kind_says_the_word_the_dashboard_groups_by() {
290 assert_eq!(Kind::Plugin.as_str(), "plugin");
291 assert_eq!(Kind::Skill.as_str(), "skill");
292 assert_eq!(Kind::Context.as_str(), "context");
293 assert_eq!(Kind::Config.as_str(), "config");
294 }
295}