Skip to main content

rpi_cli/
resource_dirs.rs

1//! Resource-directory resolution + **project-wins dedupe** for skills and
2//! prompt templates. Mirrors the precedence contract from pi's
3//! `DefaultResourceLoader`/`package-manager` (`resource-loader.ts:676-681`,
4//! `package-manager.ts:178-181`): resources are ranked project=0/1 < user=2/3
5//! < package=4, and `addSkills`/`dedupePrompts` are **first-registration-wins**
6//! on name → loading project *before* global means **project wins** on
7//! collision (skills.ts:399-428), with a collision diagnostic naming the winner
8//! (kept) and loser (dropped) paths.
9//!
10//! rpi's library loaders (`rpi_harness::skills::load_skills`,
11//! `rpi_harness::prompt_templates::load_prompt_templates`) are **append-only**
12//! (no dedup-by-name) — they correctly mirror pi's *per-directory* discovery
13//! (SKILL.md-first / root-.md / subdir recursion for skills; non-recursive `.md`
14//! children for prompts) but leave the cross-directory merge to the caller. This
15//! module is that caller-side merge: load project dir then global dir, then
16//! dedupe first-wins-by-name so project wins.
17//!
18//! **Trust gate (v1 divergence):** pi gates project `SYSTEM.md` /
19//! `APPEND_SYSTEM.md` (and some project resources) behind
20//! `settingsManager.isProjectTrusted()`. rpi v1 has **no trust prompt**
21//! (`config.rs:349`: "does not gate any project resources behind trust in v1"),
22//! so project resources are read unconditionally here. A copied `.rpi/`
23//! directory drops in and works (the documented intent). Full trust gating is
24//! deferred.
25//!
26//! **Deferred (documented):** `.agents/skills` + `~/.agents/skills`;
27//! shadowed-context-file dedup (`findShadowedContextFile`); full structured
28//! winner/loser collision diagnostics (rpi v1 encodes collisions as a
29//! `SkillDiagnostic`/`PromptTemplateDiagnostic` with a descriptive message).
30
31use std::collections::HashMap;
32use std::path::{Path, PathBuf};
33use std::sync::Arc;
34
35use rpi_harness::prompt_templates::{
36    load_prompt_templates, LoadPromptTemplatesResult, PromptTemplateDiagnostic,
37    PromptTemplateDiagnosticCode,
38};
39use rpi_harness::skills::{load_skills, LoadSkillsResult, SkillDiagnostic, SkillDiagnosticCode};
40use rpi_harness::types::{PromptTemplate, Skill};
41use rpi_tools::env::ExecutionEnv;
42
43/// The project-local rpi configuration directory.
44pub const PROJECT_CONFIG_DIR_NAME: &str = ".rpi";
45
46/// Resolve the preferred project-local resource subdir `<cwd>/.rpi/<sub>`.
47pub fn project_dir(cwd: &Path, sub: &str) -> PathBuf {
48    cwd.join(PROJECT_CONFIG_DIR_NAME).join(sub)
49}
50
51/// Resolve project-local resource dirs. Only `.rpi` is read.
52pub fn project_dirs(cwd: &Path, sub: &str) -> Vec<PathBuf> {
53    vec![project_dir(cwd, sub)]
54}
55
56/// Resolve the global resource subdir `<agent_dir>/<sub>` (e.g.
57/// `~/.rpi/agent/skills`). Returns `None` if the agent dir can't be resolved
58/// (no home dir + no `RPI_CODING_AGENT_DIR`) — callers then proceed project-only.
59pub fn global_dir(sub: &str) -> Option<PathBuf> {
60    crate::config::agent_dir().ok().map(|d| d.join(sub))
61}
62
63/// The candidate context/SYSTEM/APPEND filenames live directly under
64/// `<cwd>/.rpi/` and `<agent_dir>/`.
65pub fn project_config_file(cwd: &Path, name: &str) -> PathBuf {
66    cwd.join(PROJECT_CONFIG_DIR_NAME).join(name)
67}
68
69/// Resolve project config files. Only `.rpi` is read.
70pub fn project_config_files(cwd: &Path, name: &str) -> Vec<PathBuf> {
71    vec![project_config_file(cwd, name)]
72}
73
74/// Global config file under `<agent_dir>/<name>` (`~/.rpi/agent/SYSTEM.md`).
75pub fn global_config_file(name: &str) -> Option<PathBuf> {
76    crate::config::agent_dir().ok().map(|d| d.join(name))
77}
78
79/// Discover `SYSTEM.md` from the project `.rpi/` directory or global agent dir.
80pub fn discover_system_prompt_file(cwd: &Path) -> Option<PathBuf> {
81    discover_system_prompt_file_with_trust(cwd, true)
82}
83
84/// Discover `SYSTEM.md` with explicit project trust control.
85pub fn discover_system_prompt_file_with_trust(
86    cwd: &Path,
87    project_trusted: bool,
88) -> Option<PathBuf> {
89    if project_trusted {
90        for project in project_config_files(cwd, "SYSTEM.md") {
91            if project.is_file() {
92                return Some(project);
93            }
94        }
95    }
96    global_config_file("SYSTEM.md").filter(|p| p.is_file())
97}
98
99/// Discover `APPEND_SYSTEM.md` using project-over-global precedence.
100pub fn discover_append_system_prompt_file(cwd: &Path) -> Option<PathBuf> {
101    for project in project_config_files(cwd, "APPEND_SYSTEM.md") {
102        if project.is_file() {
103            return Some(project);
104        }
105    }
106    global_config_file("APPEND_SYSTEM.md").filter(|p| p.is_file())
107}
108
109// ---------------------------------------------------------------------------
110// Dedupe: first-wins-by-name (project wins when loaded project→global)
111// ---------------------------------------------------------------------------
112
113/// Dedupe skills by name, **first-wins**. Mirrors pi `addSkills`
114/// (`skills.ts:399-428`): the first skill with a given name is kept; later
115/// duplicates emit a collision diagnostic naming the winner (kept) and loser
116/// (dropped) paths. Load dirs in **project→global** order so project wins.
117///
118/// **v1 divergence:** rpi's `SkillDiagnostic` has no structured
119/// `winnerPath`/`loserPath` fields (pi's `SkillCollisionDiagnostic`); the
120/// collision is encoded as an `InvalidMetadata` diagnostic with a descriptive
121/// message naming both paths and `path` set to the loser.
122pub fn dedupe_skills(skills: Vec<Skill>, diagnostics: &mut Vec<SkillDiagnostic>) -> Vec<Skill> {
123    let mut winner_path: HashMap<String, String> = HashMap::new();
124    let mut out: Vec<Skill> = Vec::with_capacity(skills.len());
125    for skill in skills {
126        if let Some(winner) = winner_path.get(&skill.name) {
127            diagnostics.push(SkillDiagnostic {
128                code: SkillDiagnosticCode::InvalidMetadata,
129                message: format!(
130                    "Skill name \"{}\" from {} is shadowed by {} \
131                     (first-registration wins; load project before global so project wins)",
132                    skill.name, skill.file_path, winner
133                ),
134                path: skill.file_path.clone(),
135            });
136        } else {
137            winner_path.insert(skill.name.clone(), skill.file_path.clone());
138            out.push(skill);
139        }
140    }
141    out
142}
143
144/// Dedupe prompt templates by name, **first-wins**. Mirrors pi `dedupePrompts`
145/// (`resource-loader.ts:969-993`): the first template with a given name is kept;
146/// later duplicates emit a collision diagnostic. Load paths in **project→global**
147/// order so project wins.
148///
149/// **v1 divergence:** `PromptTemplate` carries no `file_path` (only
150/// name/description/content), so the collision diagnostic's `path` is set to the
151/// colliding template **name** rather than a file path; and the code is
152/// `ParseFailed` (rpi has no dedicated collision code) with a descriptive
153/// message. Structured winner/loser diagnostics are deferred.
154pub fn dedupe_prompt_templates(
155    templates: Vec<PromptTemplate>,
156    diagnostics: &mut Vec<PromptTemplateDiagnostic>,
157) -> Vec<PromptTemplate> {
158    let mut seen: HashMap<String, ()> = HashMap::new();
159    let mut out: Vec<PromptTemplate> = Vec::with_capacity(templates.len());
160    for t in templates {
161        if seen.contains_key(&t.name) {
162            diagnostics.push(PromptTemplateDiagnostic {
163                code: PromptTemplateDiagnosticCode::ParseFailed,
164                message: format!(
165                    "Prompt template name \"{}\" is shadowed by an earlier registration \
166                     (first-registration wins; load project before global so project wins)",
167                    t.name
168                ),
169                // PromptTemplate carries no file path; the name is the collision
170                // key, so use it as the diagnostic path.
171                path: t.name.clone(),
172            });
173        } else {
174            seen.insert(t.name.clone(), ());
175            out.push(t);
176        }
177    }
178    out
179}
180
181/// Structured variant of [`dedupe_skills`]: emits a [`ResourceDiagnostic`] per
182/// shadowed skill (winner/loser path) instead of a stringly-typed diagnostic.
183/// Callers that want to surface collisions in the UI (e.g. `/reload`) use this.
184pub fn dedupe_skills_structured(
185    skills: Vec<Skill>,
186    diagnostics: &mut Vec<rpi_harness::diagnostics::ResourceDiagnostic>,
187) -> Vec<Skill> {
188    use rpi_harness::diagnostics::{dedupe_by_name, ResourceType};
189    let keys: Vec<(String, String)> = skills
190        .iter()
191        .map(|s| (s.name.clone(), s.file_path.clone()))
192        .collect();
193    let refs: Vec<(&str, &str)> = keys.iter().map(|(n, p)| (n.as_str(), p.as_str())).collect();
194    let (winners, diags) = dedupe_by_name(ResourceType::Skill, refs);
195    diagnostics.extend(diags);
196    let winner_set: std::collections::HashSet<usize> = winners.into_iter().collect();
197    skills
198        .into_iter()
199        .enumerate()
200        .filter_map(|(index, skill)| winner_set.contains(&index).then_some(skill))
201        .collect()
202}
203
204/// Structured variant of [`dedupe_prompt_templates`]. `PromptTemplate` carries
205/// no file path, so the collision loser/winner paths fall back to the template
206/// name (documented divergence).
207pub fn dedupe_prompt_templates_structured(
208    templates: Vec<PromptTemplate>,
209    diagnostics: &mut Vec<rpi_harness::diagnostics::ResourceDiagnostic>,
210) -> Vec<PromptTemplate> {
211    use rpi_harness::diagnostics::{dedupe_by_name, ResourceType};
212    let keys: Vec<String> = templates.iter().map(|t| t.name.clone()).collect();
213    let refs: Vec<(&str, &str)> = keys.iter().map(|n| (n.as_str(), n.as_str())).collect();
214    let (winners, diags) = dedupe_by_name(ResourceType::Prompt, refs);
215    diagnostics.extend(diags);
216    let winner_set: std::collections::HashSet<usize> = winners.into_iter().collect();
217    templates
218        .into_iter()
219        .enumerate()
220        .filter_map(|(index, t)| winner_set.contains(&index).then_some(t))
221        .collect()
222}
223
224// ---------------------------------------------------------------------------
225// Precedence-aware loaders: project dir → global dir → dedupe (project wins)
226// ---------------------------------------------------------------------------
227
228/// Load skills from `dirs` in order, then dedupe first-wins-by-name. Missing
229/// directories are skipped silently by the underlying loader (NotFound →
230/// `continue`, mirroring pi). Pass dirs in **project→global** order so project
231/// wins on name collisions.
232pub async fn load_skills_with_precedence(
233    env: &Arc<dyn ExecutionEnv>,
234    dirs: &[PathBuf],
235) -> LoadSkillsResult {
236    let dir_strs: Vec<String> = dirs
237        .iter()
238        .map(|d| d.to_string_lossy().into_owned())
239        .collect();
240    let mut result = load_skills(env, &dir_strs).await;
241    // Collect structured collisions from the pre-dedupe list so the UI can
242    // show winner/loser paths (native `ResourceDiagnostic`), then dedupe.
243    let mut structured = Vec::new();
244    let raw = std::mem::take(&mut result.skills);
245    let _ = dedupe_skills_structured(raw.clone(), &mut structured);
246    result.skills = dedupe_skills(raw, &mut result.diagnostics);
247    result.resource_diagnostics = structured;
248    result
249}
250
251/// Load prompt templates from `paths` (dirs or `.md` files) in order, then
252/// dedupe first-wins-by-name. Missing paths are skipped silently. Pass paths in
253/// **project→global** order so project wins on name collisions.
254pub async fn load_prompt_templates_with_precedence(
255    env: &Arc<dyn ExecutionEnv>,
256    paths: &[PathBuf],
257) -> LoadPromptTemplatesResult {
258    let path_strs: Vec<String> = paths
259        .iter()
260        .map(|p| p.to_string_lossy().into_owned())
261        .collect();
262    let mut result = load_prompt_templates(env, &path_strs).await;
263    let mut structured = Vec::new();
264    let raw = std::mem::take(&mut result.prompt_templates);
265    let _ = dedupe_prompt_templates_structured(raw.clone(), &mut structured);
266    result.prompt_templates = dedupe_prompt_templates(raw, &mut result.diagnostics);
267    result.resource_diagnostics = structured;
268    result
269}
270
271/// The ordered skill dirs for a project: configured project paths, then
272/// `[<cwd>/.rpi/skills, <agent_dir>/skills]`, followed by
273/// configured and conventional global paths.
274/// The global dir is omitted when `agent_dir()` can't be resolved (no home dir).
275pub fn skill_dirs(cwd: &Path) -> Vec<PathBuf> {
276    let mut dirs = project_skill_dirs(cwd);
277    if let Some(g) = global_dir("skills") {
278        dirs.extend(configured_global_dirs(g.clone(), ResourceKind::Skills));
279        dirs.push(g);
280    }
281    dirs
282}
283
284/// Project-only skill directories, excluding user/global configuration. This
285/// is used by `rpi dev-local` so the current extension can be debugged with
286/// the skills in the current checkout without importing unrelated skills.
287pub fn project_skill_dirs(cwd: &Path) -> Vec<PathBuf> {
288    project_resource_dirs(cwd, "skills", ResourceKind::Skills)
289}
290
291/// Resource directories that do not depend on the current project. Used when
292/// the project trust gate denies local configuration/resources.
293pub fn global_skill_dirs() -> Vec<PathBuf> {
294    global_resource_dirs("skills", ResourceKind::Skills)
295}
296
297/// The ordered prompt-template paths for a project: configured paths, then
298/// `[<cwd>/.rpi/prompts, <agent_dir>/prompts]`.
299pub fn prompt_template_dirs(cwd: &Path) -> Vec<PathBuf> {
300    let mut dirs = project_prompt_template_dirs(cwd);
301    if let Some(g) = global_dir("prompts") {
302        dirs.extend(configured_global_dirs(g.clone(), ResourceKind::Prompts));
303        dirs.push(g);
304    }
305    dirs
306}
307
308/// Project-only prompt-template directories, excluding user/global settings.
309pub fn project_prompt_template_dirs(cwd: &Path) -> Vec<PathBuf> {
310    project_resource_dirs(cwd, "prompts", ResourceKind::Prompts)
311}
312
313pub fn global_prompt_template_dirs() -> Vec<PathBuf> {
314    global_resource_dirs("prompts", ResourceKind::Prompts)
315}
316
317/// The ordered Rust extension directories for a project. Configured paths are
318/// added before conventional directories so an explicit project path can be
319/// used for development while `.rpi` defaults stay lower precedence.
320pub fn extension_dirs(cwd: &Path) -> Vec<PathBuf> {
321    let mut dirs = project_resource_dirs(cwd, "extensions", ResourceKind::Extensions);
322    if let Some(g) = global_dir("extensions") {
323        dirs.extend(configured_global_dirs(g.clone(), ResourceKind::Extensions));
324        dirs.push(g);
325    }
326    dirs
327}
328
329pub fn global_extension_dirs() -> Vec<PathBuf> {
330    global_resource_dirs("extensions", ResourceKind::Extensions)
331}
332
333#[derive(Clone, Copy)]
334enum ResourceKind {
335    Skills,
336    Prompts,
337    Extensions,
338}
339
340fn project_resource_dirs(cwd: &Path, sub: &str, kind: ResourceKind) -> Vec<PathBuf> {
341    let loaded = crate::settings::load_project_settings_with_paths(cwd);
342    let mut dirs = Vec::new();
343    for (_, settings) in loaded.iter() {
344        dirs.extend(configured_paths(settings, cwd, kind));
345    }
346    dirs.push(project_dir(cwd, sub));
347    dirs
348}
349
350fn configured_global_dirs(agent_dir: PathBuf, kind: ResourceKind) -> Vec<PathBuf> {
351    crate::settings::load_settings()
352        .ok()
353        .into_iter()
354        .flat_map(|settings| configured_paths(&settings, &agent_dir, kind))
355        .collect()
356}
357
358fn global_resource_dirs(sub: &str, kind: ResourceKind) -> Vec<PathBuf> {
359    let Some(g) = global_dir(sub) else {
360        return Vec::new();
361    };
362    let mut dirs = configured_global_dirs(g.clone(), kind);
363    dirs.push(g);
364    dirs
365}
366
367fn configured_paths(
368    settings: &crate::settings::Settings,
369    base: &Path,
370    kind: ResourceKind,
371) -> Vec<PathBuf> {
372    let values = match kind {
373        ResourceKind::Skills => settings.skill_dirs.as_ref(),
374        ResourceKind::Prompts => settings.prompt_dirs.as_ref(),
375        ResourceKind::Extensions => settings.extension_dirs.as_ref(),
376    };
377    values
378        .map(|paths| crate::settings::resolve_configured_paths(base, paths))
379        .unwrap_or_default()
380}
381
382#[cfg(test)]
383mod tests {
384    use super::*;
385
386    fn skill(name: &str, path: &str) -> Skill {
387        Skill {
388            name: name.to_string(),
389            description: "d".to_string(),
390            content: "c".to_string(),
391            file_path: path.to_string(),
392            disable_model_invocation: None,
393        }
394    }
395
396    fn tmpl(name: &str) -> PromptTemplate {
397        PromptTemplate {
398            name: name.to_string(),
399            description: None,
400            content: "c".to_string(),
401        }
402    }
403
404    #[test]
405    fn dedupe_skills_first_wins_keeps_project() {
406        // Project loaded first, global second; same name → project (first) wins.
407        let skills = vec![
408            skill("echo", "/proj/.rpi/skills/echo/SKILL.md"),
409            skill("echo", "/home/.rpi/agent/skills/echo/SKILL.md"),
410        ];
411        let mut diags = Vec::new();
412        let out = dedupe_skills(skills, &mut diags);
413        assert_eq!(out.len(), 1);
414        assert_eq!(out[0].file_path, "/proj/.rpi/skills/echo/SKILL.md");
415        assert_eq!(diags.len(), 1);
416        assert!(diags[0]
417            .message
418            .contains("/home/.rpi/agent/skills/echo/SKILL.md"));
419        assert!(diags[0].message.contains("/proj/.rpi/skills/echo/SKILL.md"));
420        assert_eq!(diags[0].path, "/home/.rpi/agent/skills/echo/SKILL.md");
421    }
422
423    #[test]
424    fn dedupe_skills_distinct_names_all_kept() {
425        let skills = vec![skill("a", "/p/a"), skill("b", "/p/b"), skill("c", "/g/c")];
426        let mut diags = Vec::new();
427        let out = dedupe_skills(skills, &mut diags);
428        assert_eq!(out.len(), 3);
429        assert!(diags.is_empty());
430    }
431
432    #[test]
433    fn dedupe_skills_third_duplicate_drops_against_first() {
434        let skills = vec![
435            skill("x", "/proj/x"),
436            skill("x", "/global/x"),
437            skill("x", "/pkg/x"),
438        ];
439        let mut diags = Vec::new();
440        let out = dedupe_skills(skills, &mut diags);
441        assert_eq!(out.len(), 1);
442        assert_eq!(out[0].file_path, "/proj/x");
443        // Both later duplicates emit a collision diagnostic vs the same winner.
444        assert_eq!(diags.len(), 2);
445    }
446
447    #[test]
448    fn dedupe_skills_empty_input() {
449        let mut diags = Vec::new();
450        let out = dedupe_skills(Vec::new(), &mut diags);
451        assert!(out.is_empty());
452        assert!(diags.is_empty());
453    }
454
455    #[test]
456    fn dedupe_prompts_first_wins() {
457        let templates = vec![tmpl("greet"), tmpl("greet")];
458        let mut diags = Vec::new();
459        let out = dedupe_prompt_templates(templates, &mut diags);
460        assert_eq!(out.len(), 1);
461        assert_eq!(out[0].name, "greet");
462        assert_eq!(diags.len(), 1);
463        assert_eq!(diags[0].path, "greet");
464    }
465
466    #[test]
467    fn dedupe_prompts_distinct_all_kept() {
468        let templates = vec![tmpl("a"), tmpl("b"), tmpl("c")];
469        let mut diags = Vec::new();
470        let out = dedupe_prompt_templates(templates, &mut diags);
471        assert_eq!(out.len(), 3);
472        assert!(diags.is_empty());
473    }
474
475    #[test]
476    fn project_dir_uses_rpi_name() {
477        let d = project_dir(Path::new("/proj"), "skills");
478        assert_eq!(d, PathBuf::from("/proj/.rpi/skills"));
479    }
480
481    #[test]
482    fn project_dirs_only_use_rpi_after_de_pi() {
483        let dirs = project_dirs(Path::new("/proj"), "extensions");
484        assert_eq!(dirs, vec![PathBuf::from("/proj/.rpi/extensions")]);
485    }
486
487    #[test]
488    fn project_config_files_only_use_rpi_after_de_pi() {
489        let p = project_config_file(Path::new("/proj"), "SYSTEM.md");
490        assert_eq!(p, PathBuf::from("/proj/.rpi/SYSTEM.md"));
491        assert_eq!(
492            project_config_files(Path::new("/proj"), "SYSTEM.md"),
493            vec![p]
494        );
495    }
496
497    #[test]
498    fn project_settings_add_configured_resource_paths() {
499        let tmp = tempfile::tempdir().unwrap();
500        std::fs::create_dir_all(tmp.path().join(".rpi")).unwrap();
501        std::fs::write(
502            tmp.path().join(".rpi/settings.json"),
503            r#"{"skills":["shared-skills"],"promptDirs":["prompt-pack"],"extensionDirs":["target/debug"]}"#,
504        )
505        .unwrap();
506
507        let skills = skill_dirs(tmp.path());
508        assert_eq!(skills[0], tmp.path().join("shared-skills"));
509        assert!(skills.contains(&tmp.path().join(".rpi/skills")));
510
511        let prompts = prompt_template_dirs(tmp.path());
512        assert_eq!(prompts[0], tmp.path().join("prompt-pack"));
513
514        let extensions = extension_dirs(tmp.path());
515        assert_eq!(extensions[0], tmp.path().join("target/debug"));
516        assert!(extensions.contains(&tmp.path().join(".rpi/extensions")));
517    }
518
519    #[test]
520    fn local_resource_dirs_exclude_global_paths() {
521        let tmp = tempfile::tempdir().unwrap();
522        std::fs::create_dir_all(tmp.path().join(".rpi")).unwrap();
523        std::fs::write(
524            tmp.path().join(".rpi/settings.json"),
525            r#"{"skills":["local-skills"],"promptDirs":["local-prompts"]}"#,
526        )
527        .unwrap();
528
529        let skills = project_skill_dirs(tmp.path());
530        let prompts = project_prompt_template_dirs(tmp.path());
531        assert!(skills.contains(&tmp.path().join("local-skills")));
532        assert!(skills.contains(&tmp.path().join(".rpi/skills")));
533        assert!(prompts.contains(&tmp.path().join("local-prompts")));
534        assert!(prompts.contains(&tmp.path().join(".rpi/prompts")));
535    }
536}