Skip to main content

algocline_app/service/
resolve.rs

1use std::path::{Path, PathBuf};
2
3use algocline_core::AppDir;
4
5use super::path::ContainedPath;
6
7// ─── Search path (package resolution chain) ─────────────────────
8
9/// Source of a package search path entry.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub enum SearchPathSource {
12    /// From `ALC_PACKAGES_PATH` environment variable.
13    Env,
14    /// Default `~/.algocline/packages/`.
15    Default,
16}
17
18impl std::fmt::Display for SearchPathSource {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::Env => write!(f, "ALC_PACKAGES_PATH"),
22            Self::Default => write!(f, "default"),
23        }
24    }
25}
26
27/// A package search path with its origin, ordered by priority (first = highest).
28#[derive(Clone, Debug)]
29pub struct SearchPath {
30    pub path: PathBuf,
31    pub source: SearchPathSource,
32}
33
34impl SearchPath {
35    pub fn env(path: PathBuf) -> Self {
36        Self {
37            path,
38            source: SearchPathSource::Env,
39        }
40    }
41
42    pub fn default_global(path: PathBuf) -> Self {
43        Self {
44            path,
45            source: SearchPathSource::Default,
46        }
47    }
48}
49
50// Re-export from core for backward compatibility.
51pub use algocline_core::QueryResponse;
52
53// ─── Code resolution ────────────────────────────────────────────
54
55pub(crate) fn resolve_code(
56    code: Option<String>,
57    code_file: Option<String>,
58) -> Result<String, String> {
59    match (code, code_file) {
60        (Some(c), None) => Ok(c),
61        (None, Some(path)) => std::fs::read_to_string(Path::new(&path))
62            .map_err(|e| format!("Failed to read {path}: {e}")),
63        (Some(_), Some(_)) => Err("Provide either `code` or `code_file`, not both.".into()),
64        (None, None) => Err("Either `code` or `code_file` must be provided.".into()),
65    }
66}
67
68/// Build Lua code that loads a package by name and calls `pkg.run(ctx)`.
69///
70/// # Security: `name` is not sanitized
71///
72/// `name` is interpolated directly into a Lua `require()` call without
73/// sanitization. This is intentional in the current architecture:
74///
75/// - algocline is a **local development/execution tool** that runs Lua in
76///   the user's own environment via mlua (not a multi-tenant service).
77/// - The same caller has access to `alc_run`, which executes **arbitrary
78///   Lua code**. Sanitizing `name` here would not reduce the attack surface.
79/// - The MCP trust boundary lies at the **host/client** level — the host
80///   decides whether to invoke `alc_advice` at all.
81///
82/// If algocline is extended to a shared backend (e.g. a package registry
83/// server accepting untrusted strategy names), `name` **must** be validated
84/// (allowlist of `[a-zA-Z0-9_-]` or equivalent) before interpolation.
85///
86/// References:
87/// - [MCP Security Best Practices — Local MCP Server Compromise](https://modelcontextprotocol.io/specification/draft/basic/security_best_practices)
88/// - [OWASP MCP Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/MCP_Security_Cheat_Sheet.html)
89pub(crate) fn make_require_code(name: &str) -> String {
90    format!(
91        r#"local pkg = require("{name}")
92return pkg.run(ctx)"#
93    )
94}
95
96pub(crate) fn types_stub_path(app_dir: &AppDir) -> Option<String> {
97    let p = app_dir.types_dir().join("alc.d.lua");
98    if p.exists() {
99        Some(p.display().to_string())
100    } else {
101        None
102    }
103}
104
105pub(crate) fn packages_dir(app_dir: &AppDir) -> PathBuf {
106    app_dir.packages_dir()
107}
108
109pub(crate) fn scenarios_dir(app_dir: &AppDir) -> PathBuf {
110    app_dir.scenarios_dir()
111}
112
113/// Resolve scenario code from one of three mutually exclusive sources:
114/// inline code, file path, or scenario name (looked up in `{app_dir}/scenarios/`).
115pub(crate) fn resolve_scenario_code(
116    app_dir: &AppDir,
117    scenario: Option<String>,
118    scenario_file: Option<String>,
119    scenario_name: Option<String>,
120) -> Result<String, String> {
121    match (scenario, scenario_file, scenario_name) {
122        (Some(c), None, None) => Ok(c),
123        (None, Some(path), None) => std::fs::read_to_string(Path::new(&path))
124            .map_err(|e| format!("Failed to read {path}: {e}")),
125        (None, None, Some(name)) => {
126            let dir = scenarios_dir(app_dir);
127            let path = ContainedPath::child(&dir, &format!("{name}.lua"))
128                .map_err(|e| format!("Invalid scenario name: {e}"))?;
129            if !path.as_ref().exists() {
130                return Err(format!(
131                    "Scenario '{name}' not found at {}",
132                    path.as_ref().display()
133                ));
134            }
135            std::fs::read_to_string(path.as_ref())
136                .map_err(|e| format!("Failed to read scenario '{name}': {e}"))
137        }
138        (None, None, None) => {
139            Err("Provide one of: scenario, scenario_file, or scenario_name.".into())
140        }
141        _ => Err(
142            "Provide only one of: scenario, scenario_file, or scenario_name (not multiple).".into(),
143        ),
144    }
145}
146
147/// Git URLs for auto-installation. Collection repos contain multiple packages
148/// as subdirectories; single repos have init.lua at root.
149pub(super) const AUTO_INSTALL_SOURCES: &[&str] = &[
150    "https://github.com/ynishi/algocline-bundled-packages",
151    "https://github.com/ynishi/evalframe",
152];
153
154/// System packages: installed alongside user packages but not user-facing strategies.
155/// Excluded from `pkg_list` and not loaded via `require` for meta extraction.
156const SYSTEM_PACKAGES: &[&str] = &["evalframe"];
157
158/// Check whether a package is a system (non-user-facing) package.
159pub(super) fn is_system_package(name: &str) -> bool {
160    SYSTEM_PACKAGES.contains(&name)
161}
162
163/// Check whether a package is installed (has `init.lua`).
164pub(super) fn is_package_installed(app_dir: &AppDir, name: &str) -> bool {
165    packages_dir(app_dir).join(name).join("init.lua").exists()
166}
167
168/// Per-entry I/O failures collected during resilient batch operations.
169///
170/// **Resilience pattern:** Directory iteration and file operations may encounter
171/// per-entry I/O errors (permission denied, broken symlinks, etc.) that should
172/// not abort the entire operation. Failures are collected and returned alongside
173/// successful results so the caller has both the available data and diagnostics.
174///
175/// Included in JSON responses as `"failures": [...]`.
176pub(super) type DirEntryFailures = Vec<String>;
177
178/// Extract a display name from a path: file_stem if available, otherwise file_name.
179pub(super) fn display_name(path: &Path, file_name: &str) -> String {
180    path.file_stem()
181        .and_then(|s| s.to_str())
182        .map(String::from)
183        .unwrap_or_else(|| file_name.to_string())
184}
185
186/// Determine the scenario source directory within a cloned/downloaded tree.
187///
188/// Prefers a `scenarios/` subdirectory when present, falling back to the root.
189///
190/// # `.git` and other non-Lua entries
191///
192/// When falling back to the root, the directory may contain `.git/`, `README.md`,
193/// `LICENSE`, etc. This is safe because [`install_scenarios_from_dir`] applies two
194/// filters: `is_file()` (excludes `.git/` and other subdirectories) and
195/// `.lua` extension check (excludes non-Lua files). No explicit `.git` exclusion
196/// is needed.
197pub(super) fn resolve_scenario_source(clone_root: &Path) -> PathBuf {
198    let subdir = clone_root.join("scenarios");
199    if subdir.is_dir() {
200        subdir
201    } else {
202        clone_root.to_path_buf()
203    }
204}
205
206/// Copy all `.lua` files from `source` directory into `dest` (scenarios dir).
207/// Skips files that already exist. Collects per-entry I/O errors as `failures`
208/// rather than aborting.
209pub(super) fn install_scenarios_from_dir(source: &Path, dest: &Path) -> Result<String, String> {
210    let entries =
211        std::fs::read_dir(source).map_err(|e| format!("Failed to read source dir: {e}"))?;
212
213    let mut installed = Vec::new();
214    let mut skipped = Vec::new();
215    let mut failures: DirEntryFailures = Vec::new();
216
217    for entry_result in entries {
218        let entry = match entry_result {
219            Ok(e) => e,
220            Err(e) => {
221                failures.push(format!("readdir entry: {e}"));
222                continue;
223            }
224        };
225        let path = entry.path();
226        if !path.is_file() {
227            continue;
228        }
229        let ext = path.extension().and_then(|s| s.to_str());
230        if ext != Some("lua") {
231            continue;
232        }
233        let file_name = entry.file_name().to_string_lossy().to_string();
234        let dest_path = match ContainedPath::child(dest, &file_name) {
235            Ok(p) => p,
236            Err(_) => continue,
237        };
238        let name = display_name(&path, &file_name);
239        if dest_path.as_ref().exists() {
240            skipped.push(name);
241            continue;
242        }
243        match std::fs::copy(&path, dest_path.as_ref()) {
244            Ok(_) => installed.push(name),
245            Err(e) => failures.push(format!("{}: {e}", path.display())),
246        }
247    }
248
249    if installed.is_empty() && skipped.is_empty() && failures.is_empty() {
250        return Err("No .lua scenario files found in source.".into());
251    }
252
253    Ok(serde_json::json!({
254        "installed": installed,
255        "skipped": skipped,
256        "failures": failures,
257    })
258    .to_string())
259}