Skip to main content

oxi/storage/
packages.rs

1//! Package system for oxi CLI
2//!
3//! Packages bundle extensions, skills, prompts, and themes for sharing.
4//! Supports local directories, npm packages, git repositories, GitHub
5//! shorthand, and URL-based archives.
6//!
7//! ## Package sources
8//!
9//! - **Local path**: a directory with `oxi-package.toml` or auto-discoverable resources
10//! - **npm**: `npm:<package>[@<version>]` — resolved from the npm registry
11//! - **git**: `https://github.com/org/repo.git[@ref]`, `git://…`, `git+ssh://…`
12//! - **GitHub shorthand**: `github:org/repo[@ref]`
13//! - **URL**: direct `.tar.gz` / `.zip` archive
14//!
15//! ## Package manifest
16//!
17//! A package is a directory containing an `oxi-package.toml` file:
18//!
19//! ```toml
20//! name = "@foo/oxi-tools"
21//! version = "1.0.0"
22//! extensions = ["ext/index.ts"]
23//! skills = ["skills/code-review/SKILL.md"]
24//! prompts = ["prompts/review.md"]
25//! themes = ["themes/dark-pro.json"]
26//! ```
27//!
28//! ## Resource discovery
29//!
30//! When a package lacks explicit resource lists, resources are discovered
31//! automatically by scanning the package directory:
32//! - **Extensions**: `.so`, `.dylib`, `.dll` files, or `index.ts`/`index.js` entries
33//! - **Skills**: Directories containing `SKILL.md`
34//! - **Prompts**: `.md` files in `prompts/` subdirectory
35//! - **Themes**: `.json` files in `themes/` subdirectory
36//!
37//! ## Lockfile
38//!
39//! An `oxi-lock.json` file records exact versions/refs for reproducibility.
40
41use crate::util::http_client::shared_http_client;
42use anyhow::{Context, Result, bail};
43use serde::{Deserialize, Serialize};
44use sha2::{Digest, Sha256};
45use std::collections::{BTreeMap, HashMap, HashSet};
46use std::fs;
47use std::path::{Path, PathBuf};
48use std::sync::LazyLock;
49
50/// Run an async future on a fresh tokio runtime created on a dedicated OS thread.
51///
52/// This avoids the "Cannot start a runtime from within a runtime" panic that
53/// `Runtime::new()?.block_on(future)` causes when called from inside an
54/// existing tokio context (e.g., from an agent tool callback or TUI handler).
55fn run_on_fresh_runtime<F, T>(future: F) -> Result<T>
56where
57    F: Future<Output = Result<T>> + Send,
58    T: Send,
59{
60    std::thread::scope(|s| {
61        s.spawn(|| {
62            let rt = tokio::runtime::Builder::new_current_thread()
63                .enable_all()
64                .build()
65                .context("failed to build temp runtime")?;
66            rt.block_on(future)
67        })
68        .join()
69        .map_err(|_| anyhow::anyhow!("runtime thread panicked"))?
70    })
71}
72
73/// Cached regex for parsing npm package specs
74static NPM_SPEC_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
75    regex::Regex::new(r"^(@?[^@]+(?:/[^@]+)?)(?:@(.+))?$").expect("valid static regex")
76});
77
78// ── Constants ─────────────────────────────────────────────────────────
79
80const LOCKFILE_NAME: &str = "oxi-lock.json";
81const MANIFEST_NAME: &str = "oxi-package.toml";
82const NPM_MANIFEST_NAME: &str = "package.json";
83
84// ── Types ─────────────────────────────────────────────────────────────
85
86/// Types of resources a package can contribute
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
88#[serde(rename_all = "snake_case")]
89pub enum ResourceKind {
90    /// extension variant.
91    Extension,
92    /// skill variant.
93    Skill,
94    /// prompt variant.
95    Prompt,
96    /// theme variant.
97    Theme,
98}
99
100impl std::fmt::Display for ResourceKind {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        match self {
103            ResourceKind::Extension => write!(f, "extension"),
104            ResourceKind::Skill => write!(f, "skill"),
105            ResourceKind::Prompt => write!(f, "prompt"),
106            ResourceKind::Theme => write!(f, "theme"),
107        }
108    }
109}
110
111// All resource kinds for iteration
112
113/// Package manifest describing bundled resources
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct PackageManifest {
116    /// Package name (e.g. "@foo/oxi-tools")
117    pub name: String,
118    /// Semantic version (e.g. "1.0.0")
119    pub version: String,
120    /// Extension paths relative to the package root
121    #[serde(default)]
122    pub extensions: Vec<String>,
123    /// Skill names/paths
124    #[serde(default)]
125    pub skills: Vec<String>,
126    /// Prompt template paths
127    #[serde(default)]
128    pub prompts: Vec<String>,
129    /// Theme paths
130    #[serde(default)]
131    pub themes: Vec<String>,
132    /// Optional description
133    #[serde(default)]
134    pub description: Option<String>,
135    /// Package dependencies (name -> version constraint)
136    #[serde(default)]
137    pub dependencies: BTreeMap<String, String>,
138}
139
140/// A discovered resource within a package
141#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct DiscoveredResource {
143    /// Resource type
144    pub kind: ResourceKind,
145    /// Absolute path to the resource
146    pub path: PathBuf,
147    /// Relative path within the package
148    pub relative_path: String,
149}
150
151/// Metadata about a resolved resource path
152#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct PathMetadata {
154    /// Source specifier
155    pub source: String,
156    /// Scope (user / project)
157    pub scope: SourceScope,
158    /// Whether this is a package resource or top-level
159    pub origin: ResourceOrigin,
160    /// Base directory for resolving relative paths
161    pub base_dir: Option<PathBuf>,
162}
163
164/// Origin of a resource
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
166#[serde(rename_all = "snake_case")]
167pub enum ResourceOrigin {
168    /// package variant.
169    Package,
170    /// top level variant.
171    TopLevel,
172}
173
174/// Scope for package sources
175#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
176#[serde(rename_all = "snake_case")]
177pub enum SourceScope {
178    /// user variant.
179    User,
180    /// project variant.
181    Project,
182}
183
184impl std::fmt::Display for SourceScope {
185    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
186        match self {
187            SourceScope::User => write!(f, "user"),
188            SourceScope::Project => write!(f, "project"),
189        }
190    }
191}
192
193/// A resolved resource with metadata
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct ResolvedResource {
196    /// Absolute path to the resource
197    pub path: PathBuf,
198    /// Whether this resource is enabled
199    pub enabled: bool,
200    /// Metadata about the resource
201    pub metadata: PathMetadata,
202}
203
204/// Resolved paths for all resource types
205#[derive(Debug, Clone, Default, Serialize, Deserialize)]
206pub struct ResolvedPaths {
207    /// pub.
208    pub extensions: Vec<ResolvedResource>,
209    /// pub.
210    pub skills: Vec<ResolvedResource>,
211    /// pub.
212    pub prompts: Vec<ResolvedResource>,
213    /// pub.
214    pub themes: Vec<ResolvedResource>,
215}
216
217/// Progress events for package operations
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct ProgressEvent {
220    /// pub.
221    pub event_type: ProgressEventType,
222    /// pub.
223    pub action: ProgressAction,
224    /// pub.
225    pub source: String,
226    /// pub.
227    pub message: Option<String>,
228}
229
230/// Progress event type
231#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
232#[serde(rename_all = "snake_case")]
233pub enum ProgressEventType {
234    /// start variant.
235    Start,
236    /// progress variant.
237    Progress,
238    /// complete variant.
239    Complete,
240    /// error variant.
241    Error,
242}
243
244/// Action being performed
245#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
246#[serde(rename_all = "snake_case")]
247pub enum ProgressAction {
248    /// install variant.
249    Install,
250    /// remove variant.
251    Remove,
252    /// update variant.
253    Update,
254    /// clone variant.
255    Clone,
256    /// pull variant.
257    Pull,
258}
259
260impl std::fmt::Display for ProgressAction {
261    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
262        match self {
263            ProgressAction::Install => write!(f, "install"),
264            ProgressAction::Remove => write!(f, "remove"),
265            ProgressAction::Update => write!(f, "update"),
266            ProgressAction::Clone => write!(f, "clone"),
267            ProgressAction::Pull => write!(f, "pull"),
268        }
269    }
270}
271
272/// Callback for progress events
273pub type ProgressCallback = Box<dyn Fn(ProgressEvent) + Send + Sync>;
274
275// ── Source parsing ────────────────────────────────────────────────────
276
277/// Parsed package source
278#[derive(Debug, Clone, Serialize, Deserialize)]
279#[serde(tag = "type", rename_all = "snake_case")]
280pub enum ParsedSource {
281    /// Variant.
282    Npm {
283        /// Full spec (e.g. "express@4.18.0")
284        spec: String,
285        /// Package name without version
286        name: String,
287        /// Whether a version was pinned
288        pinned: bool,
289    },
290    /// Variant.
291    Git {
292        /// Full repository URL
293        repo: String,
294        /// Host (e.g. "github.com")
295        host: String,
296        /// Path on host (e.g. "org/repo")
297        path: String,
298        /// Optional ref (branch / tag / commit)
299        ref_: Option<String>,
300    },
301    /// Variant.
302    Local {
303        /// Local path
304        path: String,
305    },
306    /// Variant.
307    Url {
308        /// URL to archive
309        url: String,
310    },
311}
312
313impl ParsedSource {
314    /// Parse a source string into a ParsedSource
315    pub fn parse(source: &str) -> Self {
316        if let Some(rest) = source.strip_prefix("npm:") {
317            let spec = rest.trim();
318            let (name, pinned) = parse_npm_spec(spec);
319            return ParsedSource::Npm {
320                spec: spec.to_string(),
321                name,
322                pinned,
323            };
324        }
325
326        if let Some(rest) = source.strip_prefix("github:") {
327            let parts: Vec<&str> = rest.splitn(2, '/').collect();
328            if parts.len() == 2 {
329                let (path, ref_) = split_git_path_ref(rest);
330                return ParsedSource::Git {
331                    repo: format!("https://github.com/{}.git", path),
332                    host: "github.com".to_string(),
333                    path,
334                    ref_,
335                };
336            }
337        }
338
339        if source.starts_with("git+") || source.starts_with("git://") || source.starts_with("git@")
340        {
341            return parse_git_source(source);
342        }
343
344        if source.starts_with("https://") || source.starts_with("http://") {
345            // Distinguish git URLs from plain archive URLs
346            if source.ends_with(".git")
347                || source.contains("github.com")
348                || source.contains("gitlab.com")
349            {
350                return parse_git_source(source);
351            }
352            // Archive URL (.tar.gz, .zip, .tgz)
353            if source.ends_with(".tar.gz")
354                || source.ends_with(".tgz")
355                || source.ends_with(".zip")
356                || source.ends_with(".tar.bz2")
357            {
358                return ParsedSource::Url {
359                    url: source.to_string(),
360                };
361            }
362            // Default to git for http(s) URLs that look like repos
363            return parse_git_source(source);
364        }
365
366        // Local path
367        ParsedSource::Local {
368            path: source.to_string(),
369        }
370    }
371
372    /// Get a unique identity key for this source (ignoring version/ref)
373    pub fn identity(&self) -> String {
374        match self {
375            ParsedSource::Npm { name, .. } => format!("npm:{}", name),
376            ParsedSource::Git { host, path, .. } => format!("git:{}/{}", host, path),
377            ParsedSource::Local { path } => format!("local:{}", path),
378            ParsedSource::Url { url } => format!("url:{}", url),
379        }
380    }
381
382    /// Get a display-friendly name
383    pub fn display_name(&self) -> String {
384        match self {
385            ParsedSource::Npm { name, .. } => name.clone(),
386            ParsedSource::Git { host, path, .. } => format!("{}/{}", host, path),
387            ParsedSource::Local { path } => path.clone(),
388            ParsedSource::Url { url } => url.clone(),
389        }
390    }
391}
392
393/// Parse an npm spec into (name, pinned)
394fn parse_npm_spec(spec: &str) -> (String, bool) {
395    // Handle scoped packages like @scope/name@version
396    if let Some(caps) = NPM_SPEC_RE.captures(spec) {
397        let name = caps.get(1).map(|m| m.as_str()).unwrap_or(spec);
398        let has_version = caps.get(2).is_some();
399        return (name.to_string(), has_version);
400    }
401    (spec.to_string(), false)
402}
403
404/// Split a git path like "org/repo@ref" into ("org/repo", Some("ref"))
405fn split_git_path_ref(input: &str) -> (String, Option<String>) {
406    if let Some(at_pos) = input.rfind('@') {
407        // Make sure it's not part of an email (don't split if there's no / before @)
408        if input[..at_pos].contains('/') {
409            return (
410                input[..at_pos].to_string(),
411                Some(input[at_pos + 1..].to_string()),
412            );
413        }
414    }
415    (input.to_string(), None)
416}
417
418/// Parse a git source string
419fn parse_git_source(source: &str) -> ParsedSource {
420    // Handle git@host:path format (SSH)
421    if let Some(rest) = source.strip_prefix("git@") {
422        let colon_pos = rest.find(':').unwrap_or(rest.len());
423        let host = &rest[..colon_pos];
424        let path_part = rest.get(colon_pos + 1..).unwrap_or("");
425        let (path, ref_) = if let Some(hash_pos) = path_part.rfind('#') {
426            (
427                path_part[..hash_pos].to_string(),
428                Some(path_part[hash_pos + 1..].to_string()),
429            )
430        } else {
431            split_git_path_ref(path_part)
432        };
433        let repo = format!("git@{}:{}", host, path_part);
434        let host = host.to_string();
435        return ParsedSource::Git {
436            repo,
437            host,
438            path: path.trim_end_matches(".git").to_string(),
439            ref_,
440        };
441    }
442
443    // Handle git+ssh://, git+https://, git:// prefixes
444    let url_str = source
445        .strip_prefix("git+")
446        .unwrap_or(source)
447        .strip_prefix("git://")
448        .map(|s| format!("https://{}", s))
449        .unwrap_or_else(|| source.strip_prefix("git+").unwrap_or(source).to_string());
450
451    // Parse URL to extract host and path
452    let url = match url::Url::parse(&url_str) {
453        Ok(u) => u,
454        Err(_) => {
455            return ParsedSource::Local {
456                path: source.to_string(),
457            };
458        }
459    };
460
461    let host = url.host_str().unwrap_or("unknown").to_string();
462    let full_path = url.path().trim_start_matches('/').to_string();
463
464    // Check for #ref fragment
465    let fragment = url.fragment().map(|f| f.to_string());
466
467    let (path, ref_) = if let Some(frag) = fragment {
468        (full_path.trim_end_matches(".git").to_string(), Some(frag))
469    } else {
470        let (p, r) = split_git_path_ref(&full_path);
471        (p.trim_end_matches(".git").to_string(), r)
472    };
473
474    let repo = url_str.clone();
475
476    ParsedSource::Git {
477        repo,
478        host,
479        path,
480        ref_,
481    }
482}
483
484// ── NPM Registry ──────────────────────────────────────────────────────
485
486/// Information fetched from the npm registry
487#[derive(Debug, Clone, Serialize, Deserialize)]
488pub struct NpmPackageInfo {
489    /// pub.
490    pub name: String,
491    /// pub.
492    pub versions: BTreeMap<String, serde_json::Value>,
493    /// pub.
494    #[serde(rename = "dist-tags")]
495    pub dist_tags: BTreeMap<String, String>,
496}
497
498impl NpmPackageInfo {
499    /// Fetch package info from the npm registry
500    pub async fn fetch(name: &str) -> Result<Self> {
501        let url = format!("https://registry.npmjs.org/{}", name);
502        let client = shared_http_client();
503
504        let resp = client
505            .get(&url)
506            .header("Accept", "application/json")
507            .send()
508            .await
509            .with_context(|| format!("Failed to fetch npm info for '{}'", name))?;
510
511        if !resp.status().is_success() {
512            bail!("npm registry returned {} for '{}'", resp.status(), name);
513        }
514
515        let info: NpmPackageInfo = resp
516            .json()
517            .await
518            .with_context(|| format!("Failed to parse npm registry response for '{}'", name))?;
519
520        Ok(info)
521    }
522
523    /// Get the latest version from dist-tags
524    pub fn latest_version(&self) -> Option<&str> {
525        self.dist_tags.get("latest").map(|s| s.as_str())
526    }
527
528    /// Find the best matching version for a constraint
529    pub fn resolve_version(&self, constraint: &str) -> Option<String> {
530        if constraint == "latest" || constraint.is_empty() {
531            return self.latest_version().map(|s| s.to_string());
532        }
533
534        // Try exact match first
535        if self.versions.contains_key(constraint) {
536            return Some(constraint.to_string());
537        }
538
539        // Try semver range matching
540        if let Ok(req) = semver::VersionReq::parse(constraint) {
541            let mut best: Option<semver::Version> = None;
542            for ver_str in self.versions.keys() {
543                if let Ok(ver) = semver::Version::parse(ver_str)
544                    && req.matches(&ver)
545                {
546                    match &best {
547                        Some(b) if ver > *b => best = Some(ver),
548                        None => best = Some(ver),
549                        _ => {}
550                    }
551                }
552            }
553            if let Some(v) = best {
554                return Some(v.to_string());
555            }
556        }
557
558        None
559    }
560}
561
562/// Get the latest version of an npm package
563pub async fn get_latest_npm_version(name: &str) -> Result<String> {
564    let info = NpmPackageInfo::fetch(name).await?;
565    info.latest_version()
566        .map(|s| s.to_string())
567        .context(format!("No latest version found for '{}'", name))
568}
569
570// ── Git Operations ────────────────────────────────────────────────────
571
572/// Run a git command and capture stdout
573fn git_command(args: &[&str], cwd: Option<&Path>) -> Result<String> {
574    let mut cmd = std::process::Command::new("git");
575    cmd.args(args)
576        .env("GIT_TERMINAL_PROMPT", "0")
577        .stdout(std::process::Stdio::piped())
578        .stderr(std::process::Stdio::piped());
579
580    if let Some(dir) = cwd {
581        cmd.current_dir(dir);
582    }
583
584    let output = cmd.output().context("Failed to execute git")?;
585
586    if !output.status.success() {
587        let stderr = String::from_utf8_lossy(&output.stderr);
588        bail!(
589            "git {} failed ({}): {}",
590            args.join(" "),
591            output.status,
592            stderr.trim()
593        );
594    }
595
596    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
597}
598
599/// Run a git command (no capture)
600fn git_command_silent(args: &[&str], cwd: Option<&Path>) -> Result<()> {
601    let mut cmd = std::process::Command::new("git");
602    cmd.args(args)
603        .env("GIT_TERMINAL_PROMPT", "0")
604        .stdout(std::process::Stdio::null())
605        .stderr(std::process::Stdio::null());
606
607    if let Some(dir) = cwd {
608        cmd.current_dir(dir);
609    }
610
611    let status = cmd.status().context("Failed to execute git")?;
612    if !status.success() {
613        bail!("git {} failed ({})", args.join(" "), status);
614    }
615    Ok(())
616}
617
618/// Clone a git repository
619pub fn git_clone(repo_url: &str, target_dir: &Path, ref_: Option<&str>) -> Result<()> {
620    if target_dir.exists() {
621        bail!("Target directory already exists: {}", target_dir.display());
622    }
623    fs::create_dir_all(target_dir)
624        .with_context(|| format!("Failed to create {}", target_dir.display()))?;
625
626    let target_str = target_dir.to_string_lossy().to_string();
627    let args = vec!["clone", repo_url, &target_str];
628
629    git_command_silent(&args, None)?;
630
631    if let Some(r) = ref_ {
632        git_command_silent(&["checkout", r], Some(target_dir))?;
633    }
634
635    Ok(())
636}
637
638/// Pull/update a git repository in place
639pub fn git_update(repo_dir: &Path, ref_: Option<&str>) -> Result<bool> {
640    if !repo_dir.exists() {
641        bail!(
642            "Repository directory does not exist: {}",
643            repo_dir.display()
644        );
645    }
646
647    // Get current HEAD
648    let local_head = git_command(&["rev-parse", "HEAD"], Some(repo_dir))?;
649
650    // Determine what to fetch
651    let fetch_ref = if let Some(r) = ref_ {
652        r.to_string()
653    } else {
654        // Try to get upstream ref
655        match git_command(
656            &["rev-parse", "--abbrev-ref", "@{upstream}"],
657            Some(repo_dir),
658        ) {
659            Ok(upstream) => {
660                if let Some(branch) = upstream.strip_prefix("origin/") {
661                    format!("+refs/heads/{branch}:refs/remotes/origin/{branch}")
662                } else {
663                    "+HEAD:refs/remotes/origin/HEAD".to_string()
664                }
665            }
666            Err(_) => "+HEAD:refs/remotes/origin/HEAD".to_string(),
667        }
668    };
669
670    git_command_silent(
671        &["fetch", "--prune", "--no-tags", "origin", &fetch_ref],
672        Some(repo_dir),
673    )?;
674
675    // Determine what to reset to
676    let target_ref = ref_.unwrap_or("origin/HEAD");
677    let remote_head = git_command(&["rev-parse", target_ref], Some(repo_dir))?;
678
679    if local_head == remote_head {
680        return Ok(false); // No update needed
681    }
682
683    git_command_silent(&["reset", "--hard", target_ref], Some(repo_dir))?;
684    git_command_silent(&["clean", "-fdx"], Some(repo_dir))?;
685
686    Ok(true) // Updated
687}
688
689/// Check if a git repo has remote updates available
690pub fn git_has_update(repo_dir: &Path) -> Result<bool> {
691    let local_head = git_command(&["rev-parse", "HEAD"], Some(repo_dir))?;
692
693    // Try to get upstream
694    let upstream_ref = match git_command(
695        &["rev-parse", "--abbrev-ref", "@{upstream}"],
696        Some(repo_dir),
697    ) {
698        Ok(u) if u.starts_with("origin/") => {
699            let branch = &u["origin/".len()..];
700            format!("refs/heads/{branch}")
701        }
702        _ => "HEAD".to_string(),
703    };
704
705    // Fetch quietly and check remote
706    let _ = git_command_silent(&["fetch", "--prune", "--no-tags", "origin"], Some(repo_dir));
707
708    let remote_head = git_command(&["ls-remote", "origin", &upstream_ref], None)?;
709
710    // Parse first hash from ls-remote output
711    let remote_hash = remote_head
712        .lines()
713        .next()
714        .and_then(|line| line.split_whitespace().next())
715        .unwrap_or("");
716
717    Ok(local_head != remote_hash)
718}
719
720// ── Lockfile ──────────────────────────────────────────────────────────
721
722/// Lockfile entry for an installed package
723#[derive(Debug, Clone, Serialize, Deserialize)]
724pub struct LockEntry {
725    /// Source specifier
726    pub source: String,
727    /// Package name
728    pub name: String,
729    /// Resolved version or ref
730    pub version: String,
731    /// Integrity hash (sha256)
732    pub integrity: Option<String>,
733    /// Scope
734    pub scope: SourceScope,
735    /// Type of source
736    pub source_type: String,
737    /// Dependencies
738    #[serde(default)]
739    pub dependencies: BTreeMap<String, String>,
740}
741
742/// The lockfile structure
743#[derive(Debug, Clone, Serialize, Deserialize)]
744pub struct Lockfile {
745    /// Lockfile version
746    pub version: u32,
747    /// Locked packages
748    pub packages: BTreeMap<String, LockEntry>,
749}
750
751impl Lockfile {
752    /// Create a new empty lockfile
753    pub fn new() -> Self {
754        Self {
755            version: 1,
756            packages: BTreeMap::new(),
757        }
758    }
759
760    /// Read lockfile from disk
761    pub fn read(path: &Path) -> Result<Option<Self>> {
762        if !path.exists() {
763            return Ok(None);
764        }
765        let content = fs::read_to_string(path)
766            .with_context(|| format!("Failed to read lockfile {}", path.display()))?;
767        let lock: Lockfile = serde_json::from_str(&content)
768            .with_context(|| format!("Failed to parse lockfile {}", path.display()))?;
769        Ok(Some(lock))
770    }
771
772    /// Write lockfile to disk
773    pub fn write(&self, path: &Path) -> Result<()> {
774        let content = serde_json::to_string_pretty(self).context("Failed to serialize lockfile")?;
775        fs::write(path, content)
776            .with_context(|| format!("Failed to write lockfile {}", path.display()))?;
777        Ok(())
778    }
779
780    /// Add or update an entry
781    pub fn insert(&mut self, entry: LockEntry) {
782        self.packages.insert(entry.name.clone(), entry);
783    }
784
785    /// Remove an entry
786    pub fn remove(&mut self, name: &str) -> Option<LockEntry> {
787        self.packages.remove(name)
788    }
789
790    /// Check if a package is locked
791    pub fn contains(&self, name: &str) -> bool {
792        self.packages.contains_key(name)
793    }
794
795    /// Get an entry
796    pub fn get(&self, name: &str) -> Option<&LockEntry> {
797        self.packages.get(name)
798    }
799}
800
801impl Default for Lockfile {
802    fn default() -> Self {
803        Self::new()
804    }
805}
806
807// ── Counts ────────────────────────────────────────────────────────────
808
809/// Counts of each resource type in a package
810#[derive(Debug, Clone, Default, Serialize, Deserialize)]
811pub struct ResourceCounts {
812    /// pub.
813    pub extensions: usize,
814    /// pub.
815    pub skills: usize,
816    /// pub.
817    pub prompts: usize,
818    /// pub.
819    pub themes: usize,
820}
821
822impl std::fmt::Display for ResourceCounts {
823    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
824        let mut parts = Vec::new();
825        if self.extensions > 0 {
826            parts.push(format!("{} ext", self.extensions));
827        }
828        if self.skills > 0 {
829            parts.push(format!("{} skill", self.skills));
830        }
831        if self.prompts > 0 {
832            parts.push(format!("{} prompt", self.prompts));
833        }
834        if self.themes > 0 {
835            parts.push(format!("{} theme", self.themes));
836        }
837        if parts.is_empty() {
838            write!(f, "-")?;
839        } else {
840            write!(f, "{}", parts.join(", "))?;
841        }
842        Ok(())
843    }
844}
845
846// ── Package Manager ───────────────────────────────────────────────────
847
848/// Information about an available package update
849#[derive(Debug, Clone, Serialize, Deserialize)]
850pub struct PackageUpdateInfo {
851    /// pub.
852    pub source: String,
853    /// pub.
854    pub display_name: String,
855    /// pub.
856    pub source_type: String, // "npm" or "git"
857    /// pub.
858    pub scope: SourceScope,
859}
860
861/// A configured package
862#[derive(Debug, Clone, Serialize, Deserialize)]
863pub struct ConfiguredPackage {
864    /// pub.
865    pub source: String,
866    /// pub.
867    pub scope: SourceScope,
868    /// pub.
869    pub filtered: bool,
870    /// pub.
871    pub installed_path: Option<PathBuf>,
872}
873
874/// Manages installation, removal, and listing of packages
875pub struct PackageManager {
876    packages_dir: PathBuf,
877    /// Base directory for project-scoped packages
878    project_dir: PathBuf,
879    installed: HashMap<String, PackageManifest>,
880    lockfile: Lockfile,
881    progress_callback: Option<Box<dyn Fn(ProgressEvent) + Send + Sync>>,
882}
883
884impl PackageManager {
885    /// Create a new PackageManager using the default packages directory
886    pub fn new() -> Result<Self> {
887        let base = dirs::home_dir().context("Cannot determine home directory")?;
888        let packages_dir = base.join(".oxi").join("packages");
889        let project_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
890        let mut mgr = Self {
891            packages_dir,
892            project_dir,
893            installed: HashMap::new(),
894            lockfile: Lockfile::new(),
895            progress_callback: None,
896        };
897        mgr.load_installed()?;
898        mgr.load_lockfile()?;
899        Ok(mgr)
900    }
901
902    /// Create a PackageManager with a custom packages directory (for testing)
903    pub fn with_dir(packages_dir: PathBuf) -> Result<Self> {
904        let project_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
905        let mut mgr = Self {
906            packages_dir,
907            project_dir,
908            installed: HashMap::new(),
909            lockfile: Lockfile::new(),
910            progress_callback: None,
911        };
912        mgr.load_installed()?;
913        mgr.load_lockfile()?;
914        Ok(mgr)
915    }
916
917    /// Set the project directory for project-scoped packages
918    pub fn set_project_dir(&mut self, dir: PathBuf) {
919        self.project_dir = dir;
920    }
921
922    /// Set a progress callback
923    pub fn set_progress_callback(&mut self, callback: Box<dyn Fn(ProgressEvent) + Send + Sync>) {
924        self.progress_callback = Some(callback);
925    }
926
927    fn emit_progress(&self, event: ProgressEvent) {
928        if let Some(ref cb) = self.progress_callback {
929            cb(event);
930        }
931    }
932
933    // ── Loading ───────────────────────────────────────────────────────
934
935    /// Load all installed package manifests from disk.
936    ///
937    /// For every installed package whose `LockEntry` records an integrity
938    /// hash, the on-disk SHA-256 is recomputed and compared. Mismatches
939    /// (caused by tampering, partial disk failure, or any non-atomic write
940    /// that survived a crash) remove the package from the in-memory `installed`
941    /// map AND invalidate the matching lockfile entry, so subsequent
942    /// `update_all` re-fetches the package instead of trusting the cached copy.
943    /// A mismatch is logged at `warn` level — the CLI keeps booting (other
944    /// packages remain usable) but the affected package is treated as
945    /// un-installed.
946    fn load_installed(&mut self) -> Result<()> {
947        if !self.packages_dir.exists() {
948            return Ok(());
949        }
950        for entry in fs::read_dir(&self.packages_dir)? {
951            let entry = entry?;
952            let manifest_path = entry.path().join(MANIFEST_NAME);
953            if manifest_path.exists() {
954                match Self::read_manifest(&manifest_path) {
955                    Ok(manifest) => {
956                        let name = manifest.name.clone();
957                        let install_dir = entry.path();
958
959                        // F-1 (audit 2026-06-21): verify lockfile integrity
960                        // before trusting the installed package. Without this,
961                        // `compute_dir_hash` is only computed at install and
962                        // never re-checked on subsequent loads — a local
963                        // attacker (or a partial-write crash) could swap files
964                        // under `~/.oxi/packages/<name>/` and the next session
965                        // would silently load the tampered manifest.
966                        if let Some(expected) = self
967                            .lockfile
968                            .packages
969                            .get(&name)
970                            .and_then(|e| e.integrity.as_ref())
971                        {
972                            match verify_lockfile_integrity(&install_dir, expected) {
973                                Ok(()) => {}
974                                Err(reason) => {
975                                    tracing::warn!(
976                                        package = %name,
977                                        expected = %expected,
978                                        reason = %reason,
979                                        "package integrity mismatch on load — treating as un-installed; re-install with `oxi pkg install`"
980                                    );
981                                    // Drop the lockfile entry so `update_all`
982                                    // re-resolves from the source.
983                                    self.lockfile.packages.remove(&name);
984                                    continue;
985                                }
986                            }
987                        }
988
989                        self.installed.insert(name, manifest);
990                    }
991                    Err(e) => {
992                        tracing::warn!(
993                            "Failed to load manifest {}: {}",
994                            manifest_path.display(),
995                            e
996                        );
997                    }
998                }
999            }
1000        }
1001        Ok(())
1002    }
1003
1004    /// Load lockfile from disk
1005    fn load_lockfile(&mut self) -> Result<()> {
1006        let lock_path = self.packages_dir.join(LOCKFILE_NAME);
1007        if let Some(lock) = Lockfile::read(&lock_path)? {
1008            self.lockfile = lock;
1009        }
1010        Ok(())
1011    }
1012
1013    /// Save lockfile to disk
1014    fn save_lockfile(&self) -> Result<()> {
1015        let lock_path = self.packages_dir.join(LOCKFILE_NAME);
1016        self.lockfile.write(&lock_path)
1017    }
1018
1019    // ── Manifest ──────────────────────────────────────────────────────
1020
1021    /// Read and parse a package manifest from disk
1022    fn read_manifest(path: &Path) -> Result<PackageManifest> {
1023        let content = fs::read_to_string(path)
1024            .with_context(|| format!("Failed to read manifest {}", path.display()))?;
1025        let manifest: PackageManifest = toml::from_str(&content)
1026            .with_context(|| format!("Failed to parse manifest {}", path.display()))?;
1027        Ok(manifest)
1028    }
1029
1030    /// Try to read a `package.json` manifest (for npm packages)
1031    fn read_package_json(dir: &Path) -> Option<serde_json::Value> {
1032        let path = dir.join(NPM_MANIFEST_NAME);
1033        let content = fs::read_to_string(path).ok()?;
1034        serde_json::from_str(&content).ok()
1035    }
1036
1037    // ── Path helpers ──────────────────────────────────────────────────
1038
1039    /// Get the installation directory for a package
1040    fn pkg_install_dir(&self, name: &str) -> PathBuf {
1041        let safe_name = name.replace('@', "").replace('/', "-");
1042        self.packages_dir.join(safe_name)
1043    }
1044
1045    /// Get the packages directory path
1046    pub fn packages_dir(&self) -> &Path {
1047        &self.packages_dir
1048    }
1049
1050    /// Get install dir for a git source
1051    fn git_install_path(&self, host: &str, path: &str, scope: SourceScope) -> PathBuf {
1052        match scope {
1053            SourceScope::Project => self
1054                .project_dir
1055                .join(".oxi")
1056                .join("git")
1057                .join(host)
1058                .join(path),
1059            SourceScope::User => self.packages_dir.join("git").join(host).join(path),
1060        }
1061    }
1062
1063    /// Get install dir for an npm source
1064    fn npm_install_path(&self, name: &str, scope: SourceScope) -> PathBuf {
1065        let safe_name = name.replace('@', "").replace('/', "-");
1066        match scope {
1067            SourceScope::Project => self.project_dir.join(".oxi").join("npm").join(safe_name),
1068            SourceScope::User => self.packages_dir.join("npm").join(safe_name),
1069        }
1070    }
1071
1072    // ── Install ───────────────────────────────────────────────────────
1073
1074    /// Ensure packages directory exists
1075    fn ensure_packages_dir(&self) -> Result<()> {
1076        fs::create_dir_all(&self.packages_dir).with_context(|| {
1077            format!(
1078                "Failed to create packages directory {}",
1079                self.packages_dir.display()
1080            )
1081        })
1082    }
1083
1084    /// Install a package from a local directory path
1085    pub fn install(&mut self, source: &str) -> Result<PackageManifest> {
1086        let parsed = ParsedSource::parse(source);
1087        match parsed {
1088            ParsedSource::Local { path } => self.install_local(&path),
1089            _ => bail!("Use install_from_source() for non-local packages"),
1090        }
1091    }
1092
1093    /// Install a package from a local directory path
1094    fn install_local(&mut self, path: &str) -> Result<PackageManifest> {
1095        let source_path = Path::new(path);
1096        let manifest_path = source_path.join(MANIFEST_NAME);
1097
1098        let manifest = if manifest_path.exists() {
1099            Self::read_manifest(&manifest_path)
1100                .with_context(|| format!("No valid {} found in {}", MANIFEST_NAME, path))?
1101        } else {
1102            // Synthesise a minimal manifest
1103            let name = source_path
1104                .file_name()
1105                .map(|n| n.to_string_lossy().to_string())
1106                .unwrap_or_else(|| "unknown".to_string());
1107            PackageManifest {
1108                name,
1109                version: "0.0.0".to_string(),
1110                extensions: Vec::new(),
1111                skills: Vec::new(),
1112                prompts: Vec::new(),
1113                themes: Vec::new(),
1114                description: None,
1115                dependencies: BTreeMap::new(),
1116            }
1117        };
1118
1119        let dest = self.pkg_install_dir(&manifest.name);
1120        self.ensure_packages_dir()?;
1121
1122        if dest.exists() {
1123            fs::remove_dir_all(&dest).with_context(|| {
1124                format!("Failed to remove existing package at {}", dest.display())
1125            })?;
1126        }
1127
1128        copy_dir_recursive(source_path, &dest).with_context(|| {
1129            format!("Failed to copy package from {} to {}", path, dest.display())
1130        })?;
1131
1132        let integrity = compute_dir_hash(&dest);
1133
1134        self.lockfile.insert(LockEntry {
1135            source: path.to_string(),
1136            name: manifest.name.clone(),
1137            version: manifest.version.clone(),
1138            integrity,
1139            scope: SourceScope::User,
1140            source_type: "local".to_string(),
1141            dependencies: manifest.dependencies.clone(),
1142        });
1143
1144        self.installed
1145            .insert(manifest.name.clone(), manifest.clone());
1146        let _ = self.save_lockfile();
1147        Ok(manifest)
1148    }
1149
1150    /// Install from any source
1151    pub fn install_from_source(
1152        &mut self,
1153        source: &str,
1154        scope: SourceScope,
1155    ) -> Result<PackageManifest> {
1156        let parsed = ParsedSource::parse(source);
1157        self.emit_progress(ProgressEvent {
1158            event_type: ProgressEventType::Start,
1159            action: ProgressAction::Install,
1160            source: source.to_string(),
1161            message: Some(format!("Installing {}...", source)),
1162        });
1163        let result = match &parsed {
1164            ParsedSource::Npm { .. } => run_on_fresh_runtime(self.install_npm_async(source, scope)),
1165            ParsedSource::Git { repo, ref_, .. } => {
1166                self.install_git_sync(source, repo, ref_.as_deref(), scope)
1167            }
1168            ParsedSource::Local { path } => self.install_local(path),
1169            ParsedSource::Url { url } => run_on_fresh_runtime(self.install_url(url, scope)),
1170        };
1171        match &result {
1172            Ok(_) => self.emit_progress(ProgressEvent {
1173                event_type: ProgressEventType::Complete,
1174                action: ProgressAction::Install,
1175                source: source.to_string(),
1176                message: None,
1177            }),
1178            Err(e) => self.emit_progress(ProgressEvent {
1179                event_type: ProgressEventType::Error,
1180                action: ProgressAction::Install,
1181                source: source.to_string(),
1182                message: Some(e.to_string()),
1183            }),
1184        }
1185        result
1186    }
1187
1188    /// Async install from npm using registry
1189    async fn install_npm_async(
1190        &mut self,
1191        source: &str,
1192        scope: SourceScope,
1193    ) -> Result<PackageManifest> {
1194        let parsed = ParsedSource::parse(source);
1195        let (spec, name, pinned) = match &parsed {
1196            ParsedSource::Npm { spec, name, pinned } => (spec.clone(), name.clone(), *pinned),
1197            _ => bail!("Expected npm source"),
1198        };
1199
1200        // Resolve version
1201        let _version = if pinned {
1202            // Extract version from spec
1203            let (_, ver) = parse_npm_spec(&spec);
1204            if ver {
1205                spec.rsplit('@').next().unwrap_or("latest").to_string()
1206            } else {
1207                "latest".to_string()
1208            }
1209        } else {
1210            get_latest_npm_version(&name)
1211                .await
1212                .unwrap_or_else(|_| "latest".to_string())
1213        };
1214
1215        // Use npm pack approach
1216        self.install_npm_pack(&spec, scope)
1217    }
1218
1219    /// Install npm package using `npm pack`
1220    fn install_npm_pack(&mut self, spec: &str, scope: SourceScope) -> Result<PackageManifest> {
1221        let tmp_dir =
1222            tempfile::tempdir().context("Failed to create temp directory for npm install")?;
1223
1224        let output = std::process::Command::new("npm")
1225            .args(["pack", spec, "--pack-destination"])
1226            .arg(tmp_dir.path())
1227            .current_dir(tmp_dir.path())
1228            .output()
1229            .context("Failed to run npm pack")?;
1230
1231        if !output.status.success() {
1232            let stderr = String::from_utf8_lossy(&output.stderr);
1233            bail!("npm pack failed for '{}': {}", spec, stderr);
1234        }
1235
1236        // Find the tarball
1237        let tarball = fs::read_dir(tmp_dir.path())?
1238            .filter_map(|e| e.ok())
1239            .find(|e| {
1240                e.path()
1241                    .extension()
1242                    .map(|ext| ext == "tgz")
1243                    .unwrap_or(false)
1244            })
1245            .map(|e| e.path())
1246            .context("No .tgz file found after npm pack")?;
1247
1248        // Extract tarball
1249        let extract_dir = tmp_dir.path().join("extracted");
1250        fs::create_dir_all(&extract_dir)?;
1251
1252        let tar_status = std::process::Command::new("tar")
1253            .args(["-xzf", &tarball.to_string_lossy(), "-C"])
1254            .arg(&extract_dir)
1255            .output()
1256            .context("Failed to run tar")?;
1257
1258        if !tar_status.status.success() {
1259            let stderr = String::from_utf8_lossy(&tar_status.stderr);
1260            bail!("tar extraction failed: {}", stderr);
1261        }
1262
1263        // npm pack extracts into a "package" subdirectory
1264        let pkg_source = extract_dir.join("package");
1265        let source_for_copy = if pkg_source.exists() {
1266            &pkg_source
1267        } else {
1268            // Might be just the extracted dir
1269            extract_dir.as_path()
1270        };
1271
1272        self.ensure_packages_dir()?;
1273
1274        // Determine package name from manifest or spec
1275        let manifest = if source_for_copy.join(MANIFEST_NAME).exists() {
1276            Self::read_manifest(&source_for_copy.join(MANIFEST_NAME))?
1277        } else if source_for_copy.join(NPM_MANIFEST_NAME).exists() {
1278            let pj = Self::read_package_json(source_for_copy);
1279            let (pkg_name, pkg_version) = pj
1280                .as_ref()
1281                .map(|v| {
1282                    (
1283                        v.get("name")
1284                            .and_then(|n| n.as_str())
1285                            .unwrap_or(spec)
1286                            .to_string(),
1287                        v.get("version")
1288                            .and_then(|v| v.as_str())
1289                            .unwrap_or("0.0.0")
1290                            .to_string(),
1291                    )
1292                })
1293                .unwrap_or((spec.to_string(), "0.0.0".to_string()));
1294
1295            PackageManifest {
1296                name: pkg_name,
1297                version: pkg_version,
1298                extensions: Vec::new(),
1299                skills: Vec::new(),
1300                prompts: Vec::new(),
1301                themes: Vec::new(),
1302                description: None,
1303                dependencies: BTreeMap::new(),
1304            }
1305        } else {
1306            PackageManifest {
1307                name: spec.to_string(),
1308                version: "0.0.0".to_string(),
1309                extensions: Vec::new(),
1310                skills: Vec::new(),
1311                prompts: Vec::new(),
1312                themes: Vec::new(),
1313                description: None,
1314                dependencies: BTreeMap::new(),
1315            }
1316        };
1317
1318        let dest = self.pkg_install_dir(&manifest.name);
1319        if dest.exists() {
1320            fs::remove_dir_all(&dest).with_context(|| {
1321                format!("Failed to remove existing package at {}", dest.display())
1322            })?;
1323        }
1324
1325        copy_dir_recursive(source_for_copy, &dest)
1326            .with_context(|| format!("Failed to copy npm package for '{}'", spec))?;
1327
1328        let integrity = compute_dir_hash(&dest);
1329
1330        self.lockfile.insert(LockEntry {
1331            source: format!("npm:{}", spec),
1332            name: manifest.name.clone(),
1333            version: manifest.version.clone(),
1334            integrity,
1335            scope,
1336            source_type: "npm".to_string(),
1337            dependencies: manifest.dependencies.clone(),
1338        });
1339
1340        self.installed
1341            .insert(manifest.name.clone(), manifest.clone());
1342        let _ = self.save_lockfile();
1343        Ok(manifest)
1344    }
1345
1346    /// Install from git
1347    fn install_git_sync(
1348        &mut self,
1349        source: &str,
1350        repo: &str,
1351        ref_: Option<&str>,
1352        scope: SourceScope,
1353    ) -> Result<PackageManifest> {
1354        let parsed = ParsedSource::parse(source);
1355        let (host, path) = match &parsed {
1356            ParsedSource::Git { host, path, .. } => (host.clone(), path.clone()),
1357            _ => bail!("Expected git source"),
1358        };
1359
1360        let target_dir = self.git_install_path(&host, &path, scope);
1361
1362        if target_dir.exists() {
1363            // Already installed
1364            return self.load_manifest_from_dir(&target_dir, source, scope);
1365        }
1366
1367        let Some(parent) = target_dir.parent() else {
1368            bail!(
1369                "Invalid install path: no parent directory for {}",
1370                target_dir.display()
1371            );
1372        };
1373        fs::create_dir_all(parent)
1374            .with_context(|| format!("Failed to create parent dir for {}", target_dir.display()))?;
1375
1376        git_clone(repo, &target_dir, ref_)?;
1377
1378        // Install npm dependencies if package.json exists
1379        if target_dir.join(NPM_MANIFEST_NAME).exists() {
1380            let _ = std::process::Command::new("npm")
1381                .args(["install", "--omit=dev"])
1382                .current_dir(&target_dir)
1383                .output();
1384        }
1385
1386        self.load_manifest_from_dir(&target_dir, source, scope)
1387    }
1388
1389    /// Load manifest from a directory and register it
1390    fn load_manifest_from_dir(
1391        &mut self,
1392        dir: &Path,
1393        source: &str,
1394        scope: SourceScope,
1395    ) -> Result<PackageManifest> {
1396        let manifest = if dir.join(MANIFEST_NAME).exists() {
1397            Self::read_manifest(&dir.join(MANIFEST_NAME))?
1398        } else {
1399            let name = dir
1400                .file_name()
1401                .map(|n| n.to_string_lossy().to_string())
1402                .unwrap_or_else(|| "unknown".to_string());
1403            PackageManifest {
1404                name,
1405                version: "0.0.0".to_string(),
1406                extensions: Vec::new(),
1407                skills: Vec::new(),
1408                prompts: Vec::new(),
1409                themes: Vec::new(),
1410                description: None,
1411                dependencies: BTreeMap::new(),
1412            }
1413        };
1414
1415        let integrity = compute_dir_hash(dir);
1416
1417        self.lockfile.insert(LockEntry {
1418            source: source.to_string(),
1419            name: manifest.name.clone(),
1420            version: manifest.version.clone(),
1421            integrity,
1422            scope,
1423            source_type: "git".to_string(),
1424            dependencies: manifest.dependencies.clone(),
1425        });
1426
1427        self.installed
1428            .insert(manifest.name.clone(), manifest.clone());
1429        let _ = self.save_lockfile();
1430        Ok(manifest)
1431    }
1432
1433    /// Install from a URL (archive)
1434    async fn install_url(&mut self, url: &str, scope: SourceScope) -> Result<PackageManifest> {
1435        let client = shared_http_client();
1436
1437        let resp = client.get(url).send().await?;
1438        if !resp.status().is_success() {
1439            bail!("Failed to download {}: {}", url, resp.status());
1440        }
1441
1442        let bytes = resp.bytes().await?;
1443
1444        let tmp_dir = tempfile::tempdir()?;
1445        let archive_name = url.split('/').next_back().unwrap_or("archive");
1446        let archive_path = tmp_dir.path().join(archive_name);
1447        fs::write(&archive_path, &bytes)?;
1448
1449        let extract_dir = tmp_dir.path().join("extracted");
1450        fs::create_dir_all(&extract_dir)?;
1451
1452        if archive_name.ends_with(".tar.gz") || archive_name.ends_with(".tgz") {
1453            let status = std::process::Command::new("tar")
1454                .args(["-xzf", &archive_path.to_string_lossy(), "-C"])
1455                .arg(&extract_dir)
1456                .output()?;
1457            if !status.status.success() {
1458                bail!("Failed to extract archive");
1459            }
1460        } else if archive_name.ends_with(".zip") {
1461            // Use unzip if available
1462            let status = std::process::Command::new("unzip")
1463                .arg("-o")
1464                .arg(&archive_path)
1465                .arg("-d")
1466                .arg(&extract_dir)
1467                .output()?;
1468            if !status.status.success() {
1469                bail!("Failed to extract zip archive");
1470            }
1471        } else {
1472            bail!("Unsupported archive format: {}", archive_name);
1473        }
1474
1475        // Find the extracted package directory
1476        let pkg_dir = find_single_subdir(&extract_dir).unwrap_or_else(|| extract_dir.to_path_buf());
1477
1478        self.ensure_packages_dir()?;
1479
1480        let manifest = if pkg_dir.join(MANIFEST_NAME).exists() {
1481            Self::read_manifest(&pkg_dir.join(MANIFEST_NAME))?
1482        } else {
1483            let name = url
1484                .split('/')
1485                .next_back()
1486                .unwrap_or("url-package")
1487                .trim_end_matches(".tar.gz")
1488                .trim_end_matches(".tgz")
1489                .trim_end_matches(".zip")
1490                .to_string();
1491            PackageManifest {
1492                name,
1493                version: "0.0.0".to_string(),
1494                extensions: Vec::new(),
1495                skills: Vec::new(),
1496                prompts: Vec::new(),
1497                themes: Vec::new(),
1498                description: None,
1499                dependencies: BTreeMap::new(),
1500            }
1501        };
1502
1503        let dest = self.pkg_install_dir(&manifest.name);
1504        if dest.exists() {
1505            fs::remove_dir_all(&dest)?;
1506        }
1507
1508        copy_dir_recursive(&pkg_dir, &dest)?;
1509
1510        let integrity = compute_dir_hash(&dest);
1511
1512        self.lockfile.insert(LockEntry {
1513            source: url.to_string(),
1514            name: manifest.name.clone(),
1515            version: manifest.version.clone(),
1516            integrity,
1517            scope,
1518            source_type: "url".to_string(),
1519            dependencies: manifest.dependencies.clone(),
1520        });
1521
1522        self.installed
1523            .insert(manifest.name.clone(), manifest.clone());
1524        let _ = self.save_lockfile();
1525        Ok(manifest)
1526    }
1527
1528    /// Install from npm using `npm pack` (legacy sync method)
1529    pub fn install_npm(&mut self, name: &str) -> Result<PackageManifest> {
1530        self.install_npm_pack(name, SourceScope::User)
1531    }
1532
1533    // ── Uninstall ─────────────────────────────────────────────────────
1534
1535    /// Uninstall a package by name
1536    pub fn uninstall(&mut self, name: &str) -> Result<()> {
1537        if !self.installed.contains_key(name) {
1538            bail!("Package '{}' is not installed", name);
1539        }
1540
1541        let dest = self.pkg_install_dir(name);
1542        if dest.exists() {
1543            fs::remove_dir_all(&dest).with_context(|| {
1544                format!("Failed to remove package directory {}", dest.display())
1545            })?;
1546        }
1547
1548        // Also try to clean up git/npm scoped dirs
1549        // (best effort)
1550        let _ = self.lockfile.remove(name);
1551        let _ = self.save_lockfile();
1552
1553        self.installed.remove(name);
1554        Ok(())
1555    }
1556
1557    /// Uninstall a package from a specific source
1558    pub fn uninstall_from_source(&mut self, source: &str, scope: SourceScope) -> Result<()> {
1559        let parsed = ParsedSource::parse(source);
1560        self.emit_progress(ProgressEvent {
1561            event_type: ProgressEventType::Start,
1562            action: ProgressAction::Remove,
1563            source: source.to_string(),
1564            message: Some(format!("Removing {}...", source)),
1565        });
1566        let result = self.do_uninstall_from_source(&parsed, scope);
1567        match &result {
1568            Ok(_) => self.emit_progress(ProgressEvent {
1569                event_type: ProgressEventType::Complete,
1570                action: ProgressAction::Remove,
1571                source: source.to_string(),
1572                message: None,
1573            }),
1574            Err(e) => self.emit_progress(ProgressEvent {
1575                event_type: ProgressEventType::Error,
1576                action: ProgressAction::Remove,
1577                source: source.to_string(),
1578                message: Some(e.to_string()),
1579            }),
1580        }
1581        result
1582    }
1583
1584    fn do_uninstall_from_source(
1585        &mut self,
1586        parsed: &ParsedSource,
1587        scope: SourceScope,
1588    ) -> Result<()> {
1589        match parsed {
1590            ParsedSource::Npm { name, .. } => {
1591                let dest = self.npm_install_path(name, scope);
1592                if dest.exists() {
1593                    fs::remove_dir_all(&dest)?;
1594                }
1595                self.installed.remove(name);
1596                self.lockfile.remove(name);
1597                let _ = self.save_lockfile();
1598                Ok(())
1599            }
1600            ParsedSource::Git { host, path, .. } => {
1601                let dest = self.git_install_path(host, path, scope);
1602                if dest.exists() {
1603                    fs::remove_dir_all(&dest)?;
1604                    prune_empty_parents(&dest, &self.packages_dir);
1605                }
1606                self.installed.retain(|_, m| {
1607                    let parsed_m = ParsedSource::parse(m.name.as_str());
1608                    parsed_m.identity() != parsed.identity()
1609                });
1610                self.lockfile.packages.retain(|_, entry| {
1611                    let parsed_e = ParsedSource::parse(&entry.source);
1612                    parsed_e.identity() != parsed.identity()
1613                });
1614                let _ = self.save_lockfile();
1615                Ok(())
1616            }
1617            ParsedSource::Local { .. } => Ok(()),
1618            ParsedSource::Url { .. } => {
1619                let identity = parsed.identity();
1620                self.lockfile
1621                    .packages
1622                    .retain(|_, e| ParsedSource::parse(&e.source).identity() != identity);
1623                let _ = self.save_lockfile();
1624                Ok(())
1625            }
1626        }
1627    }
1628
1629    // ── Update ────────────────────────────────────────────────────────
1630
1631    /// Update a package (re-install from the same source).
1632    /// For npm packages, re-runs `npm pack` to get the latest version.
1633    /// For local packages, re-copies from the source path (if available).
1634    /// For git packages, does a git pull.
1635    pub fn update(&mut self, name: &str) -> Result<PackageManifest> {
1636        let lock_entry = self.lockfile.get(name).cloned();
1637
1638        if let Some(entry) = lock_entry {
1639            let parsed = ParsedSource::parse(&entry.source);
1640            return match &parsed {
1641                ParsedSource::Npm { spec, .. } => {
1642                    self.emit_progress(ProgressEvent {
1643                        event_type: ProgressEventType::Start,
1644                        action: ProgressAction::Update,
1645                        source: entry.source.clone(),
1646                        message: Some(format!("Updating {}...", name)),
1647                    });
1648                    let result = self.install_npm_pack(spec, entry.scope);
1649                    match &result {
1650                        Ok(_) => self.emit_progress(ProgressEvent {
1651                            event_type: ProgressEventType::Complete,
1652                            action: ProgressAction::Update,
1653                            source: entry.source.clone(),
1654                            message: None,
1655                        }),
1656                        Err(e) => self.emit_progress(ProgressEvent {
1657                            event_type: ProgressEventType::Error,
1658                            action: ProgressAction::Update,
1659                            source: entry.source.clone(),
1660                            message: Some(e.to_string()),
1661                        }),
1662                    }
1663                    result
1664                }
1665                ParsedSource::Git { repo, ref_, .. } => {
1666                    let target_dir = match &parsed {
1667                        ParsedSource::Git { host, path, .. } => {
1668                            self.git_install_path(host, path, entry.scope)
1669                        }
1670                        _ => unreachable!(),
1671                    };
1672                    if target_dir.exists() {
1673                        let updated = git_update(&target_dir, ref_.as_deref())?;
1674                        if updated && target_dir.join(NPM_MANIFEST_NAME).exists() {
1675                            let _ = std::process::Command::new("npm")
1676                                .args(["install", "--omit=dev"])
1677                                .current_dir(&target_dir)
1678                                .output();
1679                        }
1680                        self.load_manifest_from_dir(&target_dir, &entry.source, entry.scope)
1681                    } else {
1682                        self.install_git_sync(&entry.source, repo, ref_.as_deref(), entry.scope)
1683                    }
1684                }
1685                ParsedSource::Local { path } => self.install_local(path),
1686                ParsedSource::Url { url } => {
1687                    run_on_fresh_runtime(self.install_url(url, entry.scope))
1688                }
1689            };
1690        }
1691
1692        // Fallback: try npm re-install
1693        if self.installed.contains_key(name) {
1694            self.install_npm_pack(name, SourceScope::User)
1695        } else {
1696            bail!("Package '{}' is not installed", name);
1697        }
1698    }
1699
1700    /// Update all installed packages
1701    pub fn update_all(&mut self) -> Vec<(String, Result<PackageManifest>)> {
1702        let names: Vec<String> = self.installed.keys().cloned().collect();
1703        let mut results = Vec::new();
1704        for name in names {
1705            let result = self.update(&name);
1706            results.push((name, result));
1707        }
1708        results
1709    }
1710
1711    /// Check for available updates across all packages
1712    pub async fn check_for_updates(&self) -> Vec<PackageUpdateInfo> {
1713        let mut updates = Vec::new();
1714
1715        for lock_entry in self.lockfile.packages.values() {
1716            let parsed = ParsedSource::parse(&lock_entry.source);
1717
1718            match &parsed {
1719                ParsedSource::Npm { name: pkg_name, .. } => {
1720                    // Check npm for newer version
1721                    match NpmPackageInfo::fetch(pkg_name).await {
1722                        Ok(info) => {
1723                            if let Some(latest) = info.latest_version()
1724                                && latest != lock_entry.version
1725                            {
1726                                updates.push(PackageUpdateInfo {
1727                                    source: lock_entry.source.clone(),
1728                                    display_name: pkg_name.clone(),
1729                                    source_type: "npm".to_string(),
1730                                    scope: lock_entry.scope,
1731                                });
1732                            }
1733                        }
1734                        Err(_) => continue,
1735                    }
1736                }
1737                ParsedSource::Git { host, path, .. } => {
1738                    let install_path = self.git_install_path(host, path, lock_entry.scope);
1739                    if install_path.exists() {
1740                        match git_has_update(&install_path) {
1741                            Ok(true) => {
1742                                updates.push(PackageUpdateInfo {
1743                                    source: lock_entry.source.clone(),
1744                                    display_name: format!("{}/{}", host, path),
1745                                    source_type: "git".to_string(),
1746                                    scope: lock_entry.scope,
1747                                });
1748                            }
1749                            _ => continue,
1750                        }
1751                    }
1752                }
1753                _ => continue,
1754            }
1755        }
1756
1757        updates
1758    }
1759
1760    // ── List / query ──────────────────────────────────────────────────
1761
1762    /// List all installed packages
1763    pub fn list(&self) -> Vec<&PackageManifest> {
1764        self.installed.values().collect()
1765    }
1766
1767    /// List configured packages with metadata
1768    pub fn list_configured(&self) -> Vec<ConfiguredPackage> {
1769        let mut result = Vec::new();
1770        for name in self.installed.keys() {
1771            let installed_path = self.get_install_dir(name);
1772            let lock_entry = self.lockfile.get(name);
1773            result.push(ConfiguredPackage {
1774                source: lock_entry
1775                    .map(|e| e.source.clone())
1776                    .unwrap_or_else(|| name.clone()),
1777                scope: lock_entry.map(|e| e.scope).unwrap_or(SourceScope::User),
1778                filtered: false,
1779                installed_path,
1780            });
1781        }
1782        result
1783    }
1784
1785    /// Check whether a package is installed
1786    pub fn is_installed(&self, name: &str) -> bool {
1787        self.installed.contains_key(name)
1788    }
1789
1790    /// Get the install directory for a package (if it exists on disk)
1791    pub fn get_install_dir(&self, name: &str) -> Option<PathBuf> {
1792        let dir = self.pkg_install_dir(name);
1793        if dir.exists() { Some(dir) } else { None }
1794    }
1795
1796    /// Get the installed path for a source at a given scope
1797    pub fn get_installed_path_for_source(
1798        &self,
1799        source: &str,
1800        scope: SourceScope,
1801    ) -> Option<PathBuf> {
1802        let parsed = ParsedSource::parse(source);
1803        match &parsed {
1804            ParsedSource::Npm { name, .. } => {
1805                let path = self.npm_install_path(name, scope);
1806                if path.exists() { Some(path) } else { None }
1807            }
1808            ParsedSource::Git { host, path, .. } => {
1809                let path = self.git_install_path(host, path, scope);
1810                if path.exists() { Some(path) } else { None }
1811            }
1812            ParsedSource::Local { path } => {
1813                let p = PathBuf::from(path);
1814                if p.exists() { Some(p) } else { None }
1815            }
1816            ParsedSource::Url { .. } => None,
1817        }
1818    }
1819
1820    // ── Resource discovery ────────────────────────────────────────────
1821
1822    /// Discover all resources from an installed package.
1823    pub fn discover_resources(&self, name: &str) -> Result<Vec<DiscoveredResource>> {
1824        let manifest = self
1825            .installed
1826            .get(name)
1827            .with_context(|| format!("Package '{}' not found", name))?;
1828
1829        let install_dir = self.pkg_install_dir(name);
1830        if !install_dir.exists() {
1831            bail!("Install directory for '{}' does not exist", name);
1832        }
1833
1834        let mut resources = Vec::new();
1835
1836        let has_explicit = !manifest.extensions.is_empty()
1837            || !manifest.skills.is_empty()
1838            || !manifest.prompts.is_empty()
1839            || !manifest.themes.is_empty();
1840
1841        if has_explicit {
1842            for ext in &manifest.extensions {
1843                let path = install_dir.join(ext);
1844                if path.exists() {
1845                    resources.push(DiscoveredResource {
1846                        kind: ResourceKind::Extension,
1847                        path,
1848                        relative_path: ext.clone(),
1849                    });
1850                }
1851            }
1852            for skill in &manifest.skills {
1853                let path = install_dir.join(skill);
1854                if path.exists() {
1855                    resources.push(DiscoveredResource {
1856                        kind: ResourceKind::Skill,
1857                        path,
1858                        relative_path: skill.clone(),
1859                    });
1860                }
1861            }
1862            for prompt in &manifest.prompts {
1863                let path = install_dir.join(prompt);
1864                if path.exists() {
1865                    resources.push(DiscoveredResource {
1866                        kind: ResourceKind::Prompt,
1867                        path,
1868                        relative_path: prompt.clone(),
1869                    });
1870                }
1871            }
1872            for theme in &manifest.themes {
1873                let path = install_dir.join(theme);
1874                if path.exists() {
1875                    resources.push(DiscoveredResource {
1876                        kind: ResourceKind::Theme,
1877                        path,
1878                        relative_path: theme.clone(),
1879                    });
1880                }
1881            }
1882        } else {
1883            resources.extend(discover_extensions(&install_dir));
1884            resources.extend(discover_skills(&install_dir));
1885            resources.extend(discover_prompts(&install_dir));
1886            resources.extend(discover_themes(&install_dir));
1887        }
1888
1889        Ok(resources)
1890    }
1891
1892    /// Get resource counts for a package
1893    pub fn resource_counts(&self, name: &str) -> Result<ResourceCounts> {
1894        let resources = self.discover_resources(name)?;
1895        let mut counts = ResourceCounts::default();
1896        for r in &resources {
1897            match r.kind {
1898                ResourceKind::Extension => counts.extensions += 1,
1899                ResourceKind::Skill => counts.skills += 1,
1900                ResourceKind::Prompt => counts.prompts += 1,
1901                ResourceKind::Theme => counts.themes += 1,
1902            }
1903        }
1904        Ok(counts)
1905    }
1906
1907    /// Resolve all resources from all installed packages, producing ResolvedPaths
1908    pub fn resolve(&self) -> ResolvedPaths {
1909        let mut extensions = Vec::new();
1910        let mut skills = Vec::new();
1911        let mut prompts = Vec::new();
1912        let mut themes = Vec::new();
1913
1914        for name in self.installed.keys() {
1915            let install_dir = self.pkg_install_dir(name);
1916            if !install_dir.exists() {
1917                continue;
1918            }
1919
1920            let metadata = PathMetadata {
1921                source: name.clone(),
1922                scope: SourceScope::User,
1923                origin: ResourceOrigin::Package,
1924                base_dir: Some(install_dir.clone()),
1925            };
1926
1927            // Use discover_resources logic
1928            if let Ok(resources) = self.discover_resources(name) {
1929                for r in resources {
1930                    match r.kind {
1931                        ResourceKind::Extension => extensions.push(ResolvedResource {
1932                            path: r.path,
1933                            enabled: true,
1934                            metadata: metadata.clone(),
1935                        }),
1936                        ResourceKind::Skill => skills.push(ResolvedResource {
1937                            path: r.path,
1938                            enabled: true,
1939                            metadata: metadata.clone(),
1940                        }),
1941                        ResourceKind::Prompt => prompts.push(ResolvedResource {
1942                            path: r.path,
1943                            enabled: true,
1944                            metadata: metadata.clone(),
1945                        }),
1946                        ResourceKind::Theme => themes.push(ResolvedResource {
1947                            path: r.path,
1948                            enabled: true,
1949                            metadata: metadata.clone(),
1950                        }),
1951                    }
1952                }
1953            }
1954        }
1955
1956        ResolvedPaths {
1957            extensions,
1958            skills,
1959            prompts,
1960            themes,
1961        }
1962    }
1963
1964    // ── Dependency resolution ─────────────────────────────────────────
1965
1966    /// Resolve dependencies for all installed packages.
1967    /// Returns a list of (package, missing_dependencies) tuples.
1968    pub fn resolve_dependencies(&self) -> Vec<(String, Vec<String>)> {
1969        let mut result = Vec::new();
1970        let installed_names: HashSet<&str> = self.installed.keys().map(|s| s.as_str()).collect();
1971
1972        for (name, manifest) in &self.installed {
1973            let missing: Vec<String> = manifest
1974                .dependencies
1975                .keys()
1976                .filter(|dep| !installed_names.contains(dep.as_str()))
1977                .cloned()
1978                .collect();
1979
1980            if !missing.is_empty() {
1981                result.push((name.clone(), missing));
1982            }
1983        }
1984
1985        result
1986    }
1987
1988    /// Validate a package structure
1989    pub fn validate_package(dir: &Path) -> Result<Vec<String>> {
1990        let mut warnings = Vec::new();
1991
1992        // Check for manifest
1993        if !dir.join(MANIFEST_NAME).exists() && !dir.join(NPM_MANIFEST_NAME).exists() {
1994            warnings.push(format!(
1995                "No {} or {} found",
1996                MANIFEST_NAME, NPM_MANIFEST_NAME
1997            ));
1998        }
1999
2000        // Try to parse manifest
2001        if dir.join(MANIFEST_NAME).exists() {
2002            match Self::read_manifest(&dir.join(MANIFEST_NAME)) {
2003                Ok(m) => {
2004                    if m.name.is_empty() {
2005                        warnings.push("Package name is empty".to_string());
2006                    }
2007                    if m.version.is_empty() {
2008                        warnings.push("Package version is empty".to_string());
2009                    }
2010                    if semver::Version::parse(&m.version).is_err() {
2011                        warnings.push(format!("Version '{}' is not valid semver", m.version));
2012                    }
2013                    let has_resources = !m.extensions.is_empty()
2014                        || !m.skills.is_empty()
2015                        || !m.prompts.is_empty()
2016                        || !m.themes.is_empty();
2017                    if !has_resources {
2018                        // Check if auto-discovery would find anything
2019                        let discovered = discover_extensions(dir)
2020                            .into_iter()
2021                            .chain(discover_skills(dir))
2022                            .chain(discover_prompts(dir))
2023                            .chain(discover_themes(dir))
2024                            .count();
2025                        if discovered == 0 {
2026                            warnings.push(
2027                                "Package has no explicit resources and auto-discovery found nothing"
2028                                    .to_string(),
2029                            );
2030                        }
2031                    }
2032
2033                    // Check that explicit paths exist
2034                    for ext in &m.extensions {
2035                        if !dir.join(ext).exists() {
2036                            warnings.push(format!("Extension path '{}' does not exist", ext));
2037                        }
2038                    }
2039                    for skill in &m.skills {
2040                        if !dir.join(skill).exists() {
2041                            warnings.push(format!("Skill path '{}' does not exist", skill));
2042                        }
2043                    }
2044                    for prompt in &m.prompts {
2045                        if !dir.join(prompt).exists() {
2046                            warnings.push(format!("Prompt path '{}' does not exist", prompt));
2047                        }
2048                    }
2049                    for theme in &m.themes {
2050                        if !dir.join(theme).exists() {
2051                            warnings.push(format!("Theme path '{}' does not exist", theme));
2052                        }
2053                    }
2054                }
2055                Err(e) => {
2056                    warnings.push(format!("Failed to parse {}: {}", MANIFEST_NAME, e));
2057                }
2058            }
2059        }
2060
2061        // Check for .gitignore or .ignore
2062        if !dir.join(".gitignore").exists() && !dir.join(".ignore").exists() {
2063            warnings.push("No .gitignore or .ignore file found".to_string());
2064        }
2065
2066        Ok(warnings)
2067    }
2068
2069    // ── Version queries ───────────────────────────────────────────────
2070
2071    /// Get installed version of a package
2072    pub fn get_installed_version(&self, name: &str) -> Option<&str> {
2073        self.installed.get(name).map(|m| m.version.as_str())
2074    }
2075
2076    /// Check if an installed version satisfies a semver requirement
2077    pub fn version_satisfies(&self, name: &str, requirement: &str) -> bool {
2078        if let Some(version) = self.get_installed_version(name)
2079            && let Ok(v) = semver::Version::parse(version)
2080            && let Ok(req) = semver::VersionReq::parse(requirement)
2081        {
2082            return req.matches(&v);
2083        }
2084        false
2085    }
2086
2087    /// Get the lockfile
2088    pub fn lockfile(&self) -> &Lockfile {
2089        &self.lockfile
2090    }
2091}
2092
2093// ── Auto-discovery helpers ────────────────────────────────────────────
2094
2095/// Discover extension files in a directory.
2096fn discover_extensions(dir: &Path) -> Vec<DiscoveredResource> {
2097    let mut results = Vec::new();
2098    discover_extensions_recursive(dir, dir, &mut results);
2099    results
2100}
2101
2102fn discover_extensions_recursive(
2103    base: &Path,
2104    current: &Path,
2105    results: &mut Vec<DiscoveredResource>,
2106) {
2107    if !current.exists() {
2108        return;
2109    }
2110
2111    let entries = match fs::read_dir(current) {
2112        Ok(e) => e,
2113        Err(_) => return,
2114    };
2115
2116    for entry in entries.flatten() {
2117        let path = entry.path();
2118        let name = entry.file_name();
2119        let name_str = name.to_string_lossy();
2120
2121        if name_str.starts_with('.') || name_str == "node_modules" {
2122            continue;
2123        }
2124
2125        if path.is_dir() {
2126            // Check for index.ts / index.js in subdirectory
2127            for index in &["index.ts", "index.js"] {
2128                let index_path = path.join(index);
2129                if index_path.exists() {
2130                    let rel = path.strip_prefix(base).unwrap_or(&path);
2131                    results.push(DiscoveredResource {
2132                        kind: ResourceKind::Extension,
2133                        path: index_path,
2134                        relative_path: rel.join(index).to_string_lossy().to_string(),
2135                    });
2136                }
2137            }
2138        } else {
2139            let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
2140            if matches!(ext, "so" | "dylib" | "dll" | "ts" | "js") {
2141                let rel = path.strip_prefix(base).unwrap_or(&path);
2142                results.push(DiscoveredResource {
2143                    kind: ResourceKind::Extension,
2144                    path: path.clone(),
2145                    relative_path: rel.to_string_lossy().to_string(),
2146                });
2147            }
2148        }
2149    }
2150}
2151
2152/// Discover skill directories containing SKILL.md
2153fn discover_skills(dir: &Path) -> Vec<DiscoveredResource> {
2154    let mut results = Vec::new();
2155    discover_skills_recursive(dir, dir, &mut results);
2156    results
2157}
2158
2159fn discover_skills_recursive(base: &Path, current: &Path, results: &mut Vec<DiscoveredResource>) {
2160    if !current.exists() {
2161        return;
2162    }
2163
2164    let entries = match fs::read_dir(current) {
2165        Ok(e) => e,
2166        Err(_) => return,
2167    };
2168
2169    for entry in entries.flatten() {
2170        let path = entry.path();
2171        let name = entry.file_name();
2172        let name_str = name.to_string_lossy();
2173
2174        if name_str.starts_with('.') || name_str == "node_modules" {
2175            continue;
2176        }
2177
2178        if path.is_dir() {
2179            let skill_file = path.join("SKILL.md");
2180            if skill_file.exists() {
2181                let rel = path.strip_prefix(base).unwrap_or(&path);
2182                results.push(DiscoveredResource {
2183                    kind: ResourceKind::Skill,
2184                    path: skill_file,
2185                    relative_path: rel.join("SKILL.md").to_string_lossy().to_string(),
2186                });
2187            }
2188            discover_skills_recursive(base, &path, results);
2189        }
2190    }
2191}
2192
2193/// Discover prompt template files (.md in prompts/ subdirectory)
2194fn discover_prompts(dir: &Path) -> Vec<DiscoveredResource> {
2195    let prompts_dir = dir.join("prompts");
2196    discover_files_by_ext(
2197        if prompts_dir.exists() {
2198            &prompts_dir
2199        } else {
2200            dir
2201        },
2202        "md",
2203        ResourceKind::Prompt,
2204    )
2205}
2206
2207/// Discover theme files (.json in themes/ subdirectory)
2208fn discover_themes(dir: &Path) -> Vec<DiscoveredResource> {
2209    let themes_dir = dir.join("themes");
2210    discover_files_by_ext(
2211        if themes_dir.exists() {
2212            &themes_dir
2213        } else {
2214            dir
2215        },
2216        "json",
2217        ResourceKind::Theme,
2218    )
2219}
2220
2221/// Recursively find files with a given extension
2222fn discover_files_by_ext(dir: &Path, ext: &str, kind: ResourceKind) -> Vec<DiscoveredResource> {
2223    let mut results = Vec::new();
2224    discover_files_recursive(dir, dir, ext, kind, &mut results);
2225    results
2226}
2227
2228fn discover_files_recursive(
2229    base: &Path,
2230    current: &Path,
2231    ext: &str,
2232    kind: ResourceKind,
2233    results: &mut Vec<DiscoveredResource>,
2234) {
2235    if !current.exists() {
2236        return;
2237    }
2238
2239    let entries = match fs::read_dir(current) {
2240        Ok(e) => e,
2241        Err(_) => return,
2242    };
2243
2244    for entry in entries.flatten() {
2245        let path = entry.path();
2246        let name = entry.file_name();
2247        let name_str = name.to_string_lossy();
2248
2249        if name_str.starts_with('.') || name_str == "node_modules" {
2250            continue;
2251        }
2252
2253        if path.is_dir() {
2254            discover_files_recursive(base, &path, ext, kind, results);
2255        } else if path.extension().and_then(|e| e.to_str()) == Some(ext) {
2256            let rel = path.strip_prefix(base).unwrap_or(&path);
2257            results.push(DiscoveredResource {
2258                kind,
2259                path: path.clone(),
2260                relative_path: rel.to_string_lossy().to_string(),
2261            });
2262        }
2263    }
2264}
2265
2266// ── Utility functions ─────────────────────────────────────────────────
2267
2268/// Recursively copy a directory
2269fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<()> {
2270    if !dst.exists() {
2271        fs::create_dir_all(dst)?;
2272    }
2273
2274    for entry in fs::read_dir(src)? {
2275        let entry = entry?;
2276        let src_path = entry.path();
2277        let dst_path = dst.join(entry.file_name());
2278
2279        if src_path.is_dir() {
2280            copy_dir_recursive(&src_path, &dst_path)?;
2281        } else {
2282            fs::copy(&src_path, &dst_path)?;
2283        }
2284    }
2285
2286    Ok(())
2287}
2288
2289/// Compute a SHA-256 hash of a directory's contents for integrity checking
2290fn compute_dir_hash(dir: &Path) -> Option<String> {
2291    let mut hasher = Sha256::new();
2292    let mut files = collect_file_paths(dir);
2293    files.sort();
2294
2295    for file_path in &files {
2296        if let Ok(content) = fs::read(file_path) {
2297            hasher.update(&content);
2298        }
2299    }
2300
2301    let result = hasher.finalize();
2302    Some(format!("sha256-{:x}", result))
2303}
2304
2305/// Verify that an installed package directory matches its lockfile integrity hash.
2306///
2307/// `expected` must be in the `"sha256-<hex>"` format produced by
2308/// [`compute_dir_hash`]. Returns `Ok(())` on match, `Err(reason)` on
2309/// mismatch, missing directory, or hash format error. Missing files are
2310/// skipped silently (same as `compute_dir_hash`) so a partially-installed
2311/// package still hashes consistently with its lockfile.
2312///
2313/// This is the *consumer-side* companion to `compute_dir_hash`: the writer
2314/// (install) computes and stores; the reader (load) recomputes and compares.
2315/// Before this function existed (audit finding F-1), `integrity` was a
2316/// write-only field and a local attacker could swap files under
2317/// `~/.oxi/packages/<name>/` without detection.
2318fn verify_lockfile_integrity(install_dir: &Path, expected: &str) -> Result<(), String> {
2319    let expected_hex = expected.strip_prefix("sha256-").ok_or_else(|| {
2320        format!("lockfile integrity value not in `sha256-<hex>` form: {expected}")
2321    })?;
2322
2323    let actual = compute_dir_hash(install_dir)
2324        .ok_or_else(|| format!("could not hash install dir {}", install_dir.display()))?;
2325    let actual_hex = actual
2326        .strip_prefix("sha256-")
2327        .ok_or_else(|| format!("recomputed hash not in expected form: {actual}"))?;
2328
2329    if actual_hex.eq_ignore_ascii_case(expected_hex) {
2330        Ok(())
2331    } else {
2332        Err(format!(
2333            "sha256 mismatch: expected sha256-{expected_hex}, got {actual_hex}"
2334        ))
2335    }
2336}
2337
2338/// Collect all file paths in a directory recursively
2339fn collect_file_paths(dir: &Path) -> Vec<PathBuf> {
2340    let mut paths = Vec::new();
2341    if !dir.exists() {
2342        return paths;
2343    }
2344
2345    let entries = match fs::read_dir(dir) {
2346        Ok(e) => e,
2347        Err(_) => return paths,
2348    };
2349
2350    for entry in entries.flatten() {
2351        let path = entry.path();
2352        if path.is_dir() {
2353            paths.extend(collect_file_paths(&path));
2354        } else {
2355            paths.push(path);
2356        }
2357    }
2358
2359    paths
2360}
2361
2362/// Find the single subdirectory inside an extracted archive
2363fn find_single_subdir(dir: &Path) -> Option<PathBuf> {
2364    let entries: Vec<_> = fs::read_dir(dir).ok()?.filter_map(|e| e.ok()).collect();
2365    if entries.len() == 1 && entries[0].path().is_dir() {
2366        Some(entries[0].path())
2367    } else {
2368        None
2369    }
2370}
2371
2372/// Remove empty parent directories up to a root
2373fn prune_empty_parents(target: &Path, root: &Path) {
2374    let mut current = target.parent();
2375    while let Some(dir) = current {
2376        if dir == root || !dir.starts_with(root) {
2377            break;
2378        }
2379        if dir.exists() {
2380            let is_empty = fs::read_dir(dir)
2381                .map(|mut rd| rd.next().is_none())
2382                .unwrap_or(false);
2383            if is_empty {
2384                let _ = fs::remove_dir(dir);
2385            } else {
2386                break;
2387            }
2388        }
2389        current = dir.parent();
2390    }
2391}
2392
2393#[cfg(test)]
2394mod tests {
2395    use super::*;
2396
2397    fn setup_temp_packages_dir() -> (tempfile::TempDir, PathBuf) {
2398        let tmp = tempfile::tempdir().unwrap();
2399        let packages_dir = tmp.path().join("packages");
2400        fs::create_dir_all(&packages_dir).unwrap();
2401        (tmp, packages_dir)
2402    }
2403
2404    fn create_test_package(base: &Path, name: &str, version: &str) -> PathBuf {
2405        let pkg_dir = base.join("source-pkg");
2406        fs::create_dir_all(&pkg_dir).unwrap();
2407
2408        let manifest = PackageManifest {
2409            name: name.to_string(),
2410            version: version.to_string(),
2411            extensions: vec!["ext1.so".to_string()],
2412            skills: vec!["skill-a".to_string()],
2413            prompts: vec![],
2414            themes: vec![],
2415            description: None,
2416            dependencies: BTreeMap::new(),
2417        };
2418
2419        let toml_content = toml::to_string_pretty(&manifest).unwrap();
2420        fs::write(pkg_dir.join(MANIFEST_NAME), toml_content).unwrap();
2421        fs::write(pkg_dir.join("ext1.so"), "fake extension").unwrap();
2422        fs::create_dir_all(pkg_dir.join("skill-a")).unwrap();
2423        fs::write(pkg_dir.join("skill-a").join("SKILL.md"), "# Skill A").unwrap();
2424
2425        pkg_dir
2426    }
2427
2428    fn create_test_package_with_auto_discovery(base: &Path, name: &str, version: &str) -> PathBuf {
2429        let pkg_dir = base.join("source-pkg-auto");
2430        fs::create_dir_all(&pkg_dir).unwrap();
2431
2432        let manifest = PackageManifest {
2433            name: name.to_string(),
2434            version: version.to_string(),
2435            extensions: vec![],
2436            skills: vec![],
2437            prompts: vec![],
2438            themes: vec![],
2439            description: None,
2440            dependencies: BTreeMap::new(),
2441        };
2442        let toml_content = toml::to_string_pretty(&manifest).unwrap();
2443        fs::write(pkg_dir.join(MANIFEST_NAME), toml_content).unwrap();
2444
2445        fs::write(pkg_dir.join("myext.so"), "extension").unwrap();
2446        fs::create_dir_all(pkg_dir.join("my-skill")).unwrap();
2447        fs::write(pkg_dir.join("my-skill").join("SKILL.md"), "# My Skill").unwrap();
2448        fs::create_dir_all(pkg_dir.join("prompts")).unwrap();
2449        fs::write(pkg_dir.join("prompts").join("review.md"), "# Review").unwrap();
2450        fs::create_dir_all(pkg_dir.join("themes")).unwrap();
2451        fs::write(pkg_dir.join("themes").join("dark.json"), "{}").unwrap();
2452
2453        pkg_dir
2454    }
2455
2456    #[test]
2457    fn test_install_and_list() {
2458        let (tmp, packages_dir) = setup_temp_packages_dir();
2459
2460        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2461        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2462
2463        let manifest = mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2464        assert_eq!(manifest.name, "test-pkg");
2465        assert_eq!(manifest.version, "1.0.0");
2466
2467        let installed = mgr.list();
2468        assert_eq!(installed.len(), 1);
2469        assert_eq!(installed[0].name, "test-pkg");
2470    }
2471
2472    #[test]
2473    fn test_uninstall() {
2474        let (tmp, packages_dir) = setup_temp_packages_dir();
2475
2476        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2477        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2478
2479        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2480        assert!(mgr.is_installed("test-pkg"));
2481
2482        mgr.uninstall("test-pkg").unwrap();
2483        assert!(!mgr.is_installed("test-pkg"));
2484        assert!(mgr.list().is_empty());
2485    }
2486
2487    #[test]
2488    fn test_uninstall_not_installed() {
2489        let (_tmp, packages_dir) = setup_temp_packages_dir();
2490        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2491
2492        let result = mgr.uninstall("nonexistent");
2493        assert!(result.is_err());
2494    }
2495
2496    #[test]
2497    fn test_install_scoped_package() {
2498        let (tmp, packages_dir) = setup_temp_packages_dir();
2499
2500        let pkg_dir = create_test_package(tmp.path(), "@foo/oxi-tools", "2.0.0");
2501        let mut mgr = PackageManager::with_dir(packages_dir.clone()).unwrap();
2502
2503        let manifest = mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2504        assert_eq!(manifest.name, "@foo/oxi-tools");
2505
2506        let expected_dir = packages_dir.join("foo-oxi-tools");
2507        assert!(expected_dir.exists());
2508    }
2509
2510    #[test]
2511    fn test_reinstall_overwrites() {
2512        let (tmp, packages_dir) = setup_temp_packages_dir();
2513
2514        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2515        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2516
2517        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2518
2519        let pkg_dir_v2 = tmp.path().join("source-pkg-v2");
2520        fs::create_dir_all(&pkg_dir_v2).unwrap();
2521        let manifest_v2 = PackageManifest {
2522            name: "test-pkg".to_string(),
2523            version: "2.0.0".to_string(),
2524            extensions: vec![],
2525            skills: vec![],
2526            prompts: vec![],
2527            themes: vec![],
2528            description: None,
2529            dependencies: BTreeMap::new(),
2530        };
2531        fs::write(
2532            pkg_dir_v2.join(MANIFEST_NAME),
2533            toml::to_string_pretty(&manifest_v2).unwrap(),
2534        )
2535        .unwrap();
2536
2537        mgr.install(pkg_dir_v2.to_str().unwrap()).unwrap();
2538
2539        let installed = mgr.list();
2540        assert_eq!(installed.len(), 1);
2541        assert_eq!(installed[0].version, "2.0.0");
2542    }
2543
2544    #[test]
2545    fn test_empty_packages_dir() {
2546        let (_tmp, packages_dir) = setup_temp_packages_dir();
2547        let mgr = PackageManager::with_dir(packages_dir).unwrap();
2548        assert!(mgr.list().is_empty());
2549    }
2550
2551    #[test]
2552    fn test_packages_dir_not_exists() {
2553        let tmp = tempfile::tempdir().unwrap();
2554        let nonexistent = tmp.path().join("does-not-exist");
2555        let mgr = PackageManager::with_dir(nonexistent).unwrap();
2556        assert!(mgr.list().is_empty());
2557    }
2558
2559    #[test]
2560    fn test_discover_resources_explicit() {
2561        let (tmp, packages_dir) = setup_temp_packages_dir();
2562
2563        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2564        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2565        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2566
2567        let resources = mgr.discover_resources("test-pkg").unwrap();
2568        assert_eq!(resources.len(), 2);
2569
2570        let extensions: Vec<_> = resources
2571            .iter()
2572            .filter(|r| r.kind == ResourceKind::Extension)
2573            .collect();
2574        let skills: Vec<_> = resources
2575            .iter()
2576            .filter(|r| r.kind == ResourceKind::Skill)
2577            .collect();
2578        assert_eq!(extensions.len(), 1);
2579        assert_eq!(skills.len(), 1);
2580    }
2581
2582    #[test]
2583    fn test_discover_resources_auto() {
2584        let (tmp, packages_dir) = setup_temp_packages_dir();
2585
2586        let pkg_dir = create_test_package_with_auto_discovery(tmp.path(), "auto-pkg", "1.0.0");
2587        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2588        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2589
2590        let resources = mgr.discover_resources("auto-pkg").unwrap();
2591
2592        let ext_count = resources
2593            .iter()
2594            .filter(|r| r.kind == ResourceKind::Extension)
2595            .count();
2596        let skill_count = resources
2597            .iter()
2598            .filter(|r| r.kind == ResourceKind::Skill)
2599            .count();
2600        let prompt_count = resources
2601            .iter()
2602            .filter(|r| r.kind == ResourceKind::Prompt)
2603            .count();
2604        let theme_count = resources
2605            .iter()
2606            .filter(|r| r.kind == ResourceKind::Theme)
2607            .count();
2608
2609        assert!(
2610            ext_count >= 1,
2611            "Expected at least 1 extension, got {}",
2612            ext_count
2613        );
2614        assert!(
2615            skill_count >= 1,
2616            "Expected at least 1 skill, got {}",
2617            skill_count
2618        );
2619        assert!(
2620            prompt_count >= 1,
2621            "Expected at least 1 prompt, got {}",
2622            prompt_count
2623        );
2624        assert!(
2625            theme_count >= 1,
2626            "Expected at least 1 theme, got {}",
2627            theme_count
2628        );
2629    }
2630
2631    #[test]
2632    fn test_resource_counts() {
2633        let (tmp, packages_dir) = setup_temp_packages_dir();
2634
2635        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2636        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2637        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2638
2639        let counts = mgr.resource_counts("test-pkg").unwrap();
2640        assert_eq!(counts.extensions, 1);
2641        assert_eq!(counts.skills, 1);
2642        assert_eq!(counts.prompts, 0);
2643        assert_eq!(counts.themes, 0);
2644    }
2645
2646    #[test]
2647    fn test_resource_counts_display() {
2648        let counts = ResourceCounts {
2649            extensions: 2,
2650            skills: 1,
2651            prompts: 0,
2652            themes: 3,
2653        };
2654        assert_eq!(counts.to_string(), "2 ext, 1 skill, 3 theme");
2655
2656        let empty = ResourceCounts::default();
2657        assert_eq!(empty.to_string(), "-");
2658    }
2659
2660    #[test]
2661    fn test_resource_kind_display() {
2662        assert_eq!(ResourceKind::Extension.to_string(), "extension");
2663        assert_eq!(ResourceKind::Skill.to_string(), "skill");
2664        assert_eq!(ResourceKind::Prompt.to_string(), "prompt");
2665        assert_eq!(ResourceKind::Theme.to_string(), "theme");
2666    }
2667
2668    #[test]
2669    fn test_get_install_dir() {
2670        let (tmp, packages_dir) = setup_temp_packages_dir();
2671
2672        let pkg_dir = create_test_package(tmp.path(), "test-pkg", "1.0.0");
2673        let mut mgr = PackageManager::with_dir(packages_dir.clone()).unwrap();
2674        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2675
2676        let dir = mgr.get_install_dir("test-pkg").unwrap();
2677        assert!(dir.exists());
2678        assert!(dir.join(MANIFEST_NAME).exists());
2679
2680        assert!(mgr.get_install_dir("nonexistent").is_none());
2681    }
2682
2683    #[test]
2684    fn test_discover_resources_not_installed() {
2685        let (_tmp, packages_dir) = setup_temp_packages_dir();
2686        let mgr = PackageManager::with_dir(packages_dir).unwrap();
2687
2688        let result = mgr.discover_resources("nonexistent");
2689        assert!(result.is_err());
2690    }
2691
2692    #[test]
2693    fn test_update_not_installed() {
2694        let (_tmp, packages_dir) = setup_temp_packages_dir();
2695        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2696
2697        let result = mgr.update("nonexistent");
2698        assert!(result.is_err());
2699    }
2700
2701    // ── Source parsing tests ──────────────────────────────────────────
2702
2703    #[test]
2704    fn test_parse_npm_source() {
2705        let parsed = ParsedSource::parse("npm:express@4.18.0");
2706        match parsed {
2707            ParsedSource::Npm { spec, name, pinned } => {
2708                assert_eq!(spec, "express@4.18.0");
2709                assert_eq!(name, "express");
2710                assert!(pinned);
2711            }
2712            _ => panic!("Expected Npm source"),
2713        }
2714
2715        let parsed = ParsedSource::parse("npm:lodash");
2716        match parsed {
2717            ParsedSource::Npm { name, pinned, .. } => {
2718                assert_eq!(name, "lodash");
2719                assert!(!pinned);
2720            }
2721            _ => panic!("Expected Npm source"),
2722        }
2723    }
2724
2725    #[test]
2726    fn test_parse_git_source() {
2727        let parsed = ParsedSource::parse("https://github.com/org/repo.git");
2728        match parsed {
2729            ParsedSource::Git {
2730                host, path, ref_, ..
2731            } => {
2732                assert_eq!(host, "github.com");
2733                assert_eq!(path, "org/repo");
2734                assert!(ref_.is_none());
2735            }
2736            _ => panic!("Expected Git source"),
2737        }
2738
2739        let parsed = ParsedSource::parse("https://github.com/org/repo.git@v1.0.0");
2740        match parsed {
2741            ParsedSource::Git { path, ref_, .. } => {
2742                assert_eq!(path, "org/repo");
2743                assert_eq!(ref_.as_deref(), Some("v1.0.0"));
2744            }
2745            _ => panic!("Expected Git source"),
2746        }
2747    }
2748
2749    #[test]
2750    fn test_parse_github_shorthand() {
2751        let parsed = ParsedSource::parse("github:org/repo@main");
2752        match parsed {
2753            ParsedSource::Git {
2754                host, path, ref_, ..
2755            } => {
2756                assert_eq!(host, "github.com");
2757                assert_eq!(path, "org/repo");
2758                assert_eq!(ref_.as_deref(), Some("main"));
2759            }
2760            _ => panic!("Expected Git source"),
2761        }
2762    }
2763
2764    #[test]
2765    fn test_parse_local_source() {
2766        let parsed = ParsedSource::parse("/path/to/package");
2767        match parsed {
2768            ParsedSource::Local { path } => {
2769                assert_eq!(path, "/path/to/package");
2770            }
2771            _ => panic!("Expected Local source"),
2772        }
2773
2774        let parsed = ParsedSource::parse("./relative/path");
2775        match parsed {
2776            ParsedSource::Local { path } => {
2777                assert_eq!(path, "./relative/path");
2778            }
2779            _ => panic!("Expected Local source"),
2780        }
2781    }
2782
2783    #[test]
2784    fn test_parse_url_source() {
2785        let parsed = ParsedSource::parse("https://example.com/pkg.tar.gz");
2786        match parsed {
2787            ParsedSource::Url { url } => {
2788                assert_eq!(url, "https://example.com/pkg.tar.gz");
2789            }
2790            _ => panic!("Expected Url source"),
2791        }
2792    }
2793
2794    #[test]
2795    fn test_source_identity() {
2796        let npm = ParsedSource::parse("npm:express@4.18.0");
2797        assert_eq!(npm.identity(), "npm:express");
2798
2799        let git = ParsedSource::parse("https://github.com/org/repo.git");
2800        assert_eq!(git.identity(), "git:github.com/org/repo");
2801
2802        let local = ParsedSource::parse("/path/to/pkg");
2803        assert_eq!(local.identity(), "local:/path/to/pkg");
2804    }
2805
2806    #[test]
2807    fn test_parse_npm_spec() {
2808        let (name, pinned) = parse_npm_spec("express@4.18.0");
2809        assert_eq!(name, "express");
2810        assert!(pinned);
2811
2812        let (name, pinned) = parse_npm_spec("express");
2813        assert_eq!(name, "express");
2814        assert!(!pinned);
2815
2816        let (name, pinned) = parse_npm_spec("@scope/pkg@1.0.0");
2817        assert_eq!(name, "@scope/pkg");
2818        assert!(pinned);
2819    }
2820
2821    // ── Lockfile tests ────────────────────────────────────────────────
2822
2823    #[test]
2824    fn test_lockfile_roundtrip() {
2825        let (tmp, _) = setup_temp_packages_dir();
2826        let lock_path = tmp.path().join(LOCKFILE_NAME);
2827
2828        let mut lock = Lockfile::new();
2829        lock.insert(LockEntry {
2830            source: "npm:express@4.18.0".to_string(),
2831            name: "express".to_string(),
2832            version: "4.18.0".to_string(),
2833            integrity: Some("sha256-abc123".to_string()),
2834            scope: SourceScope::User,
2835            source_type: "npm".to_string(),
2836            dependencies: BTreeMap::new(),
2837        });
2838
2839        lock.write(&lock_path).unwrap();
2840
2841        let loaded = Lockfile::read(&lock_path).unwrap().unwrap();
2842        assert_eq!(loaded.packages.len(), 1);
2843        assert_eq!(loaded.packages["express"].version, "4.18.0");
2844        assert_eq!(
2845            loaded.packages["express"].integrity.as_deref(),
2846            Some("sha256-abc123")
2847        );
2848    }
2849
2850    #[test]
2851    fn test_lockfile_install_roundtrip() {
2852        let (tmp, packages_dir) = setup_temp_packages_dir();
2853        let pkg_dir = create_test_package(tmp.path(), "locked-pkg", "1.0.0");
2854
2855        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2856        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2857
2858        // Lockfile should have been written
2859        let lock_path = mgr.packages_dir().join(LOCKFILE_NAME);
2860        assert!(lock_path.exists());
2861
2862        let lock = Lockfile::read(&lock_path).unwrap().unwrap();
2863        assert!(lock.contains("locked-pkg"));
2864        let entry = lock.get("locked-pkg").unwrap();
2865        assert_eq!(entry.version, "1.0.0");
2866    }
2867
2868    // ── Validation tests ──────────────────────────────────────────────
2869
2870    #[test]
2871    fn test_validate_valid_package() {
2872        let (tmp, _) = setup_temp_packages_dir();
2873        let pkg_dir = create_test_package(tmp.path(), "valid-pkg", "1.0.0");
2874        let warnings = PackageManager::validate_package(&pkg_dir).unwrap();
2875        // Should have minimal warnings (maybe just about .gitignore)
2876        assert!(
2877            warnings.len() <= 1,
2878            "Expected <= 1 warning, got {:?}",
2879            warnings
2880        );
2881    }
2882
2883    #[test]
2884    fn test_validate_empty_dir() {
2885        let tmp = tempfile::tempdir().unwrap();
2886        let empty_dir = tmp.path().join("empty-pkg");
2887        fs::create_dir_all(&empty_dir).unwrap();
2888        let warnings = PackageManager::validate_package(&empty_dir).unwrap();
2889        assert!(!warnings.is_empty());
2890    }
2891
2892    // ── Dependency tests ──────────────────────────────────────────────
2893
2894    #[test]
2895    fn test_resolve_dependencies() {
2896        let (tmp, packages_dir) = setup_temp_packages_dir();
2897
2898        // Create a package with dependencies
2899        let pkg_dir = tmp.path().join("dep-pkg");
2900        fs::create_dir_all(&pkg_dir).unwrap();
2901        let mut deps = BTreeMap::new();
2902        deps.insert("lodash".to_string(), "^4.0.0".to_string());
2903        deps.insert("nonexistent-pkg".to_string(), "^1.0.0".to_string());
2904
2905        let manifest = PackageManifest {
2906            name: "dep-pkg".to_string(),
2907            version: "1.0.0".to_string(),
2908            extensions: vec![],
2909            skills: vec![],
2910            prompts: vec![],
2911            themes: vec![],
2912            description: None,
2913            dependencies: deps,
2914        };
2915        fs::write(
2916            pkg_dir.join(MANIFEST_NAME),
2917            toml::to_string_pretty(&manifest).unwrap(),
2918        )
2919        .unwrap();
2920
2921        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2922        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2923
2924        let missing = mgr.resolve_dependencies();
2925        assert_eq!(missing.len(), 1);
2926        assert_eq!(missing[0].0, "dep-pkg");
2927        assert!(
2928            missing[0].1.contains(&"lodash".to_string())
2929                || missing[0].1.contains(&"nonexistent-pkg".to_string())
2930        );
2931    }
2932
2933    // ── Version tests ─────────────────────────────────────────────────
2934
2935    #[test]
2936    fn test_version_satisfies() {
2937        let (tmp, packages_dir) = setup_temp_packages_dir();
2938        let pkg_dir = create_test_package(tmp.path(), "ver-pkg", "1.2.3");
2939        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2940        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2941
2942        assert!(mgr.version_satisfies("ver-pkg", "^1.0.0"));
2943        assert!(mgr.version_satisfies("ver-pkg", ">=1.0.0"));
2944        assert!(!mgr.version_satisfies("ver-pkg", "^2.0.0"));
2945        assert!(!mgr.version_satisfies("ver-pkg", "<1.0.0"));
2946    }
2947
2948    #[test]
2949    fn test_get_installed_version() {
2950        let (tmp, packages_dir) = setup_temp_packages_dir();
2951        let pkg_dir = create_test_package(tmp.path(), "ver-pkg", "3.1.4");
2952        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2953        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2954
2955        assert_eq!(mgr.get_installed_version("ver-pkg"), Some("3.1.4"));
2956        assert_eq!(mgr.get_installed_version("nonexistent"), None);
2957    }
2958
2959    // ── Resolve tests ─────────────────────────────────────────────────
2960
2961    #[test]
2962    fn test_resolve() {
2963        let (tmp, packages_dir) = setup_temp_packages_dir();
2964        let pkg_dir = create_test_package(tmp.path(), "resolve-pkg", "1.0.0");
2965        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2966        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2967
2968        let resolved = mgr.resolve();
2969        assert!(!resolved.extensions.is_empty() || !resolved.skills.is_empty());
2970    }
2971
2972    // ── Progress callback tests ───────────────────────────────────────
2973
2974    #[test]
2975    fn test_progress_callback() {
2976        use std::sync::{Arc, Mutex};
2977
2978        let events: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
2979        let events_clone = events.clone();
2980
2981        let (tmp, packages_dir) = setup_temp_packages_dir();
2982        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
2983
2984        mgr.set_progress_callback(Box::new(move |event| {
2985            let mut e = events_clone.lock().unwrap();
2986            e.push(format!("{:?}:{:?}", event.event_type, event.action));
2987        }));
2988
2989        let pkg_dir = create_test_package(tmp.path(), "progress-pkg", "1.0.0");
2990        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
2991
2992        // install_local doesn't use with_progress, so no events expected from install()
2993        // Just verify the progress event mechanism exists and doesn't panic
2994        let _event_count = events.lock().unwrap().len();
2995    }
2996
2997    #[test]
2998    fn test_list_configured() {
2999        let (tmp, packages_dir) = setup_temp_packages_dir();
3000        let pkg_dir = create_test_package(tmp.path(), "cfg-pkg", "1.0.0");
3001        let mut mgr = PackageManager::with_dir(packages_dir).unwrap();
3002        mgr.install(pkg_dir.to_str().unwrap()).unwrap();
3003
3004        let configured = mgr.list_configured();
3005        assert_eq!(configured.len(), 1);
3006        assert!(configured[0].source.contains("source-pkg"));
3007        // source comes from lockfile, might be the local path
3008    }
3009
3010    // ── F-1 regression: lockfile integrity verify on load ──────────
3011
3012    /// `verify_lockfile_integrity` returns Ok(()) when the directory
3013    /// contents hash to the lockfile-recorded `sha256-<hex>` value.
3014    #[test]
3015    fn verify_lockfile_integrity_accepts_matching_dir() {
3016        let tmp = tempfile::tempdir().unwrap();
3017        let pkg_dir = tmp.path().join("pkg");
3018        fs::create_dir_all(&pkg_dir).unwrap();
3019        fs::write(pkg_dir.join("a.txt"), b"hello").unwrap();
3020        fs::write(pkg_dir.join("b.txt"), b"world").unwrap();
3021
3022        let expected = compute_dir_hash(&pkg_dir).expect("compute_dir_hash must succeed");
3023
3024        assert!(verify_lockfile_integrity(&pkg_dir, &expected).is_ok());
3025    }
3026
3027    /// A directory that has been mutated after install must fail the
3028    /// integrity check. This is the F-1 supply-chain scenario the audit
3029    /// flagged: previously `LockEntry.integrity` was computed at install
3030    /// and never re-checked, so a tampered package would silently load.
3031    #[test]
3032    fn verify_lockfile_integrity_rejects_tampered_dir() {
3033        let tmp = tempfile::tempdir().unwrap();
3034        let pkg_dir = tmp.path().join("pkg");
3035        fs::create_dir_all(&pkg_dir).unwrap();
3036        fs::write(pkg_dir.join("a.txt"), b"hello").unwrap();
3037
3038        let expected = compute_dir_hash(&pkg_dir).expect("compute_dir_hash must succeed");
3039
3040        // Tamper: replace file contents.
3041        fs::write(pkg_dir.join("a.txt"), b"tampered").unwrap();
3042
3043        let err = verify_lockfile_integrity(&pkg_dir, &expected)
3044            .expect_err("tampered dir must not verify");
3045        assert!(err.contains("sha256 mismatch"), "unexpected error: {err}");
3046    }
3047
3048    /// Missing `sha256-` prefix is a clear lockfile-format error.
3049    #[test]
3050    fn verify_lockfile_integrity_rejects_bad_prefix() {
3051        let tmp = tempfile::tempdir().unwrap();
3052        let pkg_dir = tmp.path().join("pkg");
3053        fs::create_dir_all(&pkg_dir).unwrap();
3054
3055        let err = verify_lockfile_integrity(&pkg_dir, "abc123")
3056            .expect_err("missing sha256- prefix must be rejected");
3057        assert!(err.contains("not in `sha256-<hex>` form"));
3058    }
3059
3060    /// `load_installed` must drop a package whose lockfile-recorded
3061    /// integrity no longer matches the on-disk directory.
3062    #[test]
3063    fn load_installed_skips_tampered_package() {
3064        let (tmp, packages_dir) = setup_temp_packages_dir();
3065        let pkg_dir = create_test_package(tmp.path(), "tamper-pkg", "1.0.0");
3066
3067        // Install normally — this writes integrity to the lockfile.
3068        {
3069            let mut mgr = PackageManager::with_dir(packages_dir.clone()).unwrap();
3070            mgr.install(pkg_dir.to_str().unwrap()).unwrap();
3071        }
3072
3073        // Tamper with the installed package after install.
3074        let installed_name = "tamper-pkg";
3075        let installed_safe = installed_name.replace('@', "").replace('/', "-");
3076        let on_disk = packages_dir.join(installed_safe);
3077        fs::write(on_disk.join(MANIFEST_NAME), "tampered = true\n").unwrap();
3078
3079        // Re-open the manager — `load_installed` should drop the package.
3080        let mgr2 = PackageManager::with_dir(packages_dir).unwrap();
3081        let names: Vec<&str> = mgr2.list().iter().map(|m| m.name.as_str()).collect();
3082        assert!(
3083            !names.contains(&"tamper-pkg"),
3084            "tampered package must be excluded from load_installed; loaded names: {names:?}"
3085        );
3086    }
3087}