Skip to main content

harn_modules/
asset_paths.rs

1//! Package-root prompt asset addressing (issue #742).
2//!
3//! Pipelines that move files around want stable, refactor-safe paths to
4//! `.harn.prompt` assets. Source-relative `../../partials/foo.harn.prompt`
5//! paths break the moment a caller is renamed or relocated. This module
6//! resolves a small URI scheme that anchors prompt assets at the
7//! project root or a project-defined alias instead:
8//!
9//! - `@/<rel>` → resolved from the calling module's project root
10//!   (the nearest `harn.toml` ancestor of the *calling file*, not the
11//!   workspace cwd).
12//! - `@<alias>/<rel>` → resolved from a `[asset_roots]` entry in the
13//!   project's `harn.toml`, e.g. `[asset_roots] partials = "..."`.
14//!
15//! Plain (non-`@`) paths fall through to the caller's existing
16//! source-relative resolver — back-compat is exact.
17//!
18//! Lives in `harn-modules` so the VM, the LSP, and the CLI's preflight
19//! checker can share one resolver and produce identical errors.
20
21use std::path::{Component, Path, PathBuf};
22
23const ASSET_PREFIX: char = '@';
24
25/// A parsed `@`-prefixed asset reference.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub enum AssetRef<'a> {
28    /// `@/<rel>` — anchored at the project root.
29    ProjectRoot { rel: &'a str },
30    /// `@<alias>/<rel>` — anchored at a `[asset_roots]` alias.
31    Alias { alias: &'a str, rel: &'a str },
32}
33
34/// Returns true when `path` starts with the `@`-asset prefix.
35pub fn is_asset_path(path: &str) -> bool {
36    path.starts_with(ASSET_PREFIX)
37}
38
39/// Parse an `@`-prefixed path. Returns `None` for plain paths so callers
40/// can fall back to source-relative resolution. Malformed `@`-paths
41/// (e.g. `@foo` with no slash) also return `None`; the resolver wraps
42/// `None` cases into a parse-time error when the caller knows it has
43/// an `@` prefix.
44pub fn parse(path: &str) -> Option<AssetRef<'_>> {
45    let stripped = path.strip_prefix(ASSET_PREFIX)?;
46    if let Some(rel) = stripped.strip_prefix('/') {
47        return Some(AssetRef::ProjectRoot { rel });
48    }
49    let (alias, rel) = stripped.split_once('/')?;
50    Some(AssetRef::Alias { alias, rel })
51}
52
53/// Walk up from `base` looking for the nearest ancestor containing
54/// `harn.toml`. Mirrors `harn-vm`'s in-VM walker so the resolver can
55/// run from the LSP/CLI without dragging in the VM crate.
56pub fn find_project_root(base: &Path) -> Option<PathBuf> {
57    let mut dir = base.to_path_buf();
58    loop {
59        if dir.join("harn.toml").exists() {
60            return Some(dir);
61        }
62        if !dir.pop() {
63            return None;
64        }
65    }
66}
67
68/// Resolve an `@`-prefixed asset path to an absolute filesystem path.
69///
70/// `anchor` is the directory the project-root walk starts from — the
71/// caller's choice depends on context:
72///
73/// - **VM runtime**: pass the thread-local source dir
74///   (`source_root_path()`), which always reflects the file currently
75///   executing.
76/// - **LSP / preflight**: pass the calling source file's parent, so the
77///   project root is derived from the file under analysis.
78///
79/// In every case the project root is derived from the *call site*, not
80/// the user's cwd, so an imported pipeline resolves prompts the same
81/// way regardless of who called it.
82pub fn resolve(asset_ref: &AssetRef<'_>, anchor: &Path) -> Result<PathBuf, String> {
83    let project_root = find_project_root(anchor).ok_or_else(|| {
84        format!(
85            "package-root prompt path '{}' has no project root: no harn.toml found above {}",
86            display_asset(asset_ref),
87            anchor.display()
88        )
89    })?;
90    match asset_ref {
91        AssetRef::ProjectRoot { rel } => {
92            let safe = safe_relative(rel)
93                .ok_or_else(|| format!("invalid project-root asset path '@/{rel}'"))?;
94            Ok(project_root.join(safe))
95        }
96        AssetRef::Alias { alias, rel } => {
97            let safe =
98                safe_relative(rel).ok_or_else(|| format!("invalid asset path '@{alias}/{rel}'"))?;
99            let asset_root = lookup_alias(&project_root, alias).ok_or_else(|| {
100                format!(
101                    "asset alias '{alias}' is not defined in [asset_roots] of {}",
102                    project_root.join("harn.toml").display()
103                )
104            })?;
105            let safe_root = safe_relative(&asset_root).ok_or_else(|| {
106                format!(
107                    "asset alias '{alias}' resolves to an unsafe path '{asset_root}' \
108                     (must be a project-relative directory without `..` segments)"
109                )
110            })?;
111            Ok(project_root.join(safe_root).join(safe))
112        }
113    }
114}
115
116/// Convenience for the common case in `render_prompt(path, ...)`:
117/// resolve `@`-prefixed paths against the project root, otherwise apply
118/// the caller's source-relative fallback.
119pub fn resolve_or<F>(path: &str, anchor: &Path, fallback: F) -> Result<PathBuf, String>
120where
121    F: FnOnce(&str) -> PathBuf,
122{
123    if let Some(asset_ref) = parse(path) {
124        return resolve(&asset_ref, anchor);
125    }
126    Ok(fallback(path))
127}
128
129/// Reject paths that would escape the anchor or contain shell-relative
130/// shenanigans. Mirrors the safety check in
131/// `safe_package_relative_path` so package-rooted prompts can't reach
132/// outside the project root via `..` traversal.
133fn safe_relative(raw: &str) -> Option<PathBuf> {
134    if raw.is_empty() || raw.contains('\\') {
135        return None;
136    }
137    let mut out = PathBuf::new();
138    let mut saw_component = false;
139    for component in Path::new(raw).components() {
140        match component {
141            Component::Normal(part) => {
142                saw_component = true;
143                out.push(part);
144            }
145            Component::CurDir => {}
146            Component::ParentDir | Component::RootDir | Component::Prefix(_) => return None,
147        }
148    }
149    saw_component.then_some(out)
150}
151
152fn display_asset(asset_ref: &AssetRef<'_>) -> String {
153    match asset_ref {
154        AssetRef::ProjectRoot { rel } => format!("@/{rel}"),
155        AssetRef::Alias { alias, rel } => format!("@{alias}/{rel}"),
156    }
157}
158
159/// Look up `[asset_roots] <alias> = "..."` in the project's harn.toml.
160/// Missing manifest, missing table, or missing key all return `None`
161/// so the caller can produce one uniform diagnostic.
162fn lookup_alias(project_root: &Path, alias: &str) -> Option<String> {
163    let manifest = std::fs::read_to_string(project_root.join("harn.toml")).ok()?;
164    let parsed: toml::Value = toml::from_str(&manifest).ok()?;
165    let table = parsed.get("asset_roots")?.as_table()?;
166    table.get(alias)?.as_str().map(str::to_string)
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172    use std::fs;
173    use tempfile::TempDir;
174
175    #[test]
176    fn parses_project_root_form() {
177        assert_eq!(
178            parse("@/partials/foo.harn.prompt"),
179            Some(AssetRef::ProjectRoot {
180                rel: "partials/foo.harn.prompt"
181            })
182        );
183    }
184
185    #[test]
186    fn parses_alias_form() {
187        assert_eq!(
188            parse("@partials/foo.harn.prompt"),
189            Some(AssetRef::Alias {
190                alias: "partials",
191                rel: "foo.harn.prompt"
192            })
193        );
194    }
195
196    #[test]
197    fn plain_paths_pass_through() {
198        assert!(parse("relative/path").is_none());
199        assert!(parse("/absolute/path").is_none());
200        assert!(parse("../sibling").is_none());
201    }
202
203    #[test]
204    fn parent_traversal_rejected() {
205        assert!(safe_relative("foo/../bar").is_none());
206        assert!(safe_relative("/abs").is_none());
207        assert!(safe_relative("").is_none());
208    }
209
210    #[test]
211    fn resolves_project_root_path_anchored_at_caller_root() {
212        let temp = TempDir::new().unwrap();
213        let root = temp.path();
214        fs::write(root.join("harn.toml"), "[package]\nname = \"x\"\n").unwrap();
215        fs::create_dir_all(root.join("a/b/c")).unwrap();
216        let resolved = resolve(
217            &parse("@/prompts/foo.harn.prompt").unwrap(),
218            &root.join("a/b/c"),
219        )
220        .unwrap();
221        assert_eq!(resolved, root.join("prompts/foo.harn.prompt"));
222    }
223
224    #[test]
225    fn resolves_alias_path_via_asset_roots() {
226        let temp = TempDir::new().unwrap();
227        let root = temp.path();
228        fs::write(
229            root.join("harn.toml"),
230            "[package]\nname = \"x\"\n[asset_roots]\npartials = \"src/prompts\"\n",
231        )
232        .unwrap();
233        fs::create_dir_all(root.join("a/b")).unwrap();
234        let resolved = resolve(
235            &parse("@partials/foo.harn.prompt").unwrap(),
236            &root.join("a/b"),
237        )
238        .unwrap();
239        assert_eq!(resolved, root.join("src/prompts/foo.harn.prompt"));
240    }
241
242    #[test]
243    fn missing_alias_produces_clear_error() {
244        let temp = TempDir::new().unwrap();
245        let root = temp.path();
246        fs::write(root.join("harn.toml"), "[package]\nname = \"x\"\n").unwrap();
247        let err = resolve(&parse("@unknown/foo.harn.prompt").unwrap(), root).unwrap_err();
248        assert!(err.contains("[asset_roots]"));
249        assert!(err.contains("unknown"));
250    }
251
252    #[test]
253    fn no_project_root_produces_error() {
254        let temp = TempDir::new().unwrap();
255        let err = resolve(&parse("@/foo.harn.prompt").unwrap(), temp.path()).unwrap_err();
256        assert!(err.contains("no harn.toml"));
257    }
258}