Skip to main content

rpi_cli/
resource_management.rs

1//! Native local snapshot management. No compilation, dlopen, provider or network.
2use serde::{Deserialize, Serialize};
3use serde_json::{json, Value};
4use sha2::{Digest, Sha256};
5use std::{
6    collections::BTreeSet,
7    fs::{self, File, OpenOptions},
8    io::{Read, Write},
9    path::{Component, Path, PathBuf},
10    sync::Arc,
11};
12
13const RECEIPTS: &str = "resource-receipts.json";
14const JOURNAL: &str = "resource-transaction.json";
15const LEASE: &str = ".resource-lease";
16const MAX_META: usize = 256 * 1024;
17const MAX_FILES: usize = 2048;
18const MAX_FILE: u64 = 64 * 1024 * 1024;
19const MAX_BYTES: u64 = 256 * 1024 * 1024;
20/// Static protocol description only: never resolves a root or opens metadata.
21pub(crate) fn capabilities() -> Value {
22    json!({"version":1,"command":"resource","kinds":["package","skill","extension"],
23        "scopes":["project","global"],"explicitScopeRequired":true,
24        "operations":["list","inspect","install","update","reinstall","remove","recover"],
25        "localSnapshotsOnly":true,"build":false,"network":false,"hotApply":false,
26        "bounds":{"maxMetadataBytes":MAX_META,"maxResultBytes":MAX_META,"maxResourcesPerScope":128,
27            "maxInventoryEntries":MAX_FILES,"maxDepth":32,"maxFileBytes":MAX_FILE,"maxTotalBytes":MAX_BYTES}})
28}
29
30type Result<T> = std::result::Result<T, String>;
31fn err(e: impl std::fmt::Display) -> String {
32    e.to_string()
33}
34
35#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
36#[serde(rename_all = "lowercase")]
37pub enum Scope {
38    #[default]
39    Project,
40    Global,
41}
42impl Scope {
43    pub fn is_project(&self) -> bool {
44        *self == Self::Project
45    }
46    pub fn root(self, cwd: &Path) -> Result<PathBuf> {
47        let logical = match self {
48            Self::Project => cwd.join(".rpi"),
49            Self::Global => crate::config::agent_dir().map_err(err)?,
50        };
51        resolve_scope_root(&logical)
52    }
53}
54
55/// Resolve only the external prefix of an owned scope root.
56///
57/// The root itself is deliberately appended after canonicalization so a link
58/// at `.rpi`/`agent` remains visible to `checked_path`. Missing parents are
59/// represented by a tail appended to the nearest existing parent; this is
60/// path resolution only and never creates directories or reads metadata files.
61fn resolve_scope_root(root: &Path) -> Result<PathBuf> {
62    if !root.is_absolute() {
63        return Err("resource root must be absolute".into());
64    }
65    if root.components().any(|c| matches!(c, Component::ParentDir)) {
66        return Err("resource path traversal".into());
67    }
68    let Some(name) = root.file_name() else {
69        // A filesystem root has no external prefix to resolve.
70        checked_path(root)?;
71        return Ok(root.to_path_buf());
72    };
73    #[cfg(windows)]
74    checked_scope_tail_component(name)?;
75    let mut parent = root.parent().ok_or("resource root has no parent")?;
76    let mut missing = Vec::new();
77    loop {
78        match fs::symlink_metadata(parent) {
79            Ok(_) => break,
80            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
81                missing.push(
82                    parent
83                        .file_name()
84                        .ok_or("resource root has no existing parent")?
85                        .to_os_string(),
86                );
87                parent = parent
88                    .parent()
89                    .ok_or("resource root has no existing parent")?;
90            }
91            Err(e) => {
92                return Err(format!(
93                    "checking resource root parent {}: {e}",
94                    parent.display()
95                ))
96            }
97        }
98    }
99    let mut resolved = fs::canonicalize(parent).map_err(err)?;
100    for component in missing.into_iter().rev() {
101        #[cfg(windows)]
102        checked_scope_tail_component(&component)?;
103        resolved.push(component);
104    }
105    resolved.push(name);
106    checked_path(&resolved)?;
107    Ok(resolved)
108}
109
110#[cfg(windows)]
111fn checked_scope_tail_component(component: &std::ffi::OsStr) -> Result<()> {
112    let name = component
113        .to_str()
114        .ok_or("non-UTF-8 resource scope component")?;
115    // canonicalize returns an extended-length path. Appending a DOS name
116    // with trailing dots/spaces, a device name, or an ADS suffix could then
117    // address a different directory than loaders using the original path.
118    if name.ends_with(['.', ' ']) || name.contains(':') {
119        return Err("ambiguous Windows resource scope component".into());
120    }
121    reserved_identity(name)
122}
123
124#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
125#[serde(rename_all = "lowercase")]
126enum Kind {
127    Package,
128    Skill,
129    Extension,
130}
131impl Kind {
132    fn parse(s: &str) -> Result<Self> {
133        match s {
134            "package" => Ok(Self::Package),
135            "skill" => Ok(Self::Skill),
136            "extension" => Ok(Self::Extension),
137            _ => Err("kind must be package, skill or extension".into()),
138        }
139    }
140    fn dir(self) -> &'static str {
141        match self {
142            Self::Package => "packages",
143            Self::Skill => "skills",
144            Self::Extension => "extensions",
145        }
146    }
147    fn target(self, name: &str) -> PathBuf {
148        Path::new(self.dir()).join(if self == Self::Extension {
149            format!("{name}.{}", std::env::consts::DLL_EXTENSION)
150        } else {
151            name.into()
152        })
153    }
154}
155fn identifier(s: &str) -> Result<()> {
156    if s.is_empty()
157        || s.len() > 96
158        || !s.as_bytes()[0].is_ascii_alphanumeric()
159        || !s
160            .bytes()
161            .all(|c| c.is_ascii_alphanumeric() || matches!(c, b'-' | b'_' | b'.'))
162        || s.ends_with('.')
163    {
164        return Err("invalid resource identity (portable ASCII name required)".into());
165    }
166    reserved_identity(s)
167}
168
169// Existing native link records allow leading '_' and '.', unlike new managed
170// identities. Accept that grammar only for safe on-disk package components.
171fn legacy_package_identity(s: &str) -> Result<()> {
172    if !crate::packages::valid_name(s) {
173        return Err("invalid native package identity".into());
174    }
175    relative(Path::new(s))?;
176    reserved_identity(s)
177}
178
179fn reserved_identity(s: &str) -> Result<()> {
180    let stem = s.split('.').next().unwrap().to_ascii_uppercase();
181    if matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")
182        || (stem.len() == 4
183            && (stem.starts_with("COM") || stem.starts_with("LPT"))
184            && stem.as_bytes()[3].is_ascii_digit())
185    {
186        return Err("reserved resource identity".into());
187    }
188    Ok(())
189}
190fn relative(path: &Path) -> Result<()> {
191    if path.as_os_str().is_empty()
192        || path
193            .components()
194            .any(|c| !matches!(c, Component::Normal(_)))
195    {
196        return Err("unsafe relative resource path".into());
197    }
198    for part in path {
199        let s = part.to_str().ok_or("non-UTF-8 resource path")?;
200        if s.len() > 128 || s.contains([':', '\\']) || s.ends_with(['.', ' ']) {
201            return Err("unsafe resource filename".into());
202        }
203    }
204    Ok(())
205}
206fn is_link(meta: &fs::Metadata) -> bool {
207    #[cfg(windows)]
208    {
209        use std::os::windows::fs::MetadataExt;
210        meta.file_type().is_symlink() || meta.file_attributes() & 0x400 != 0
211    }
212    #[cfg(not(windows))]
213    {
214        meta.file_type().is_symlink()
215    }
216}
217/// Validate every existing ancestor too, including Windows junctions.
218pub(crate) fn checked_path(path: &Path) -> Result<()> {
219    if !path.is_absolute() {
220        return Err("resource path must be absolute".into());
221    }
222    let mut current = PathBuf::new();
223    for c in path.components() {
224        if matches!(c, Component::ParentDir) {
225            return Err("resource path traversal".into());
226        }
227        current.push(c);
228        // Prefix alone is not an addressable ancestor (notably `\\\\?\\C:`).
229        // Inspect the filesystem root once RootDir has made it absolute.
230        if matches!(c, Component::Prefix(_)) || !current.is_absolute() {
231            continue;
232        }
233        match fs::symlink_metadata(&current) {
234            Ok(m) if is_link(&m) => {
235                return Err(format!(
236                    "links/reparse points are not owned resource paths: {}",
237                    current.display()
238                ))
239            }
240            Ok(_) => {}
241            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
242            Err(e) => {
243                return Err(format!(
244                    "checking resource ancestor {}: {e}",
245                    current.display()
246                ))
247            }
248        }
249    }
250    Ok(())
251}
252fn read_bytes(path: &Path, limit: usize) -> Result<Option<Vec<u8>>> {
253    checked_path(path)?;
254    let mut file = match File::open(path) {
255        Ok(f) => f,
256        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
257        Err(e) => return Err(err(e)),
258    };
259    if !file.metadata().map_err(err)?.is_file() {
260        return Err("resource metadata is not a regular file".into());
261    }
262    let mut bytes = Vec::new();
263    Read::by_ref(&mut file)
264        .take(limit as u64 + 1)
265        .read_to_end(&mut bytes)
266        .map_err(err)?;
267    if bytes.len() > limit {
268        return Err(format!("resource metadata exceeds {limit} bytes"));
269    }
270    Ok(Some(bytes))
271}
272fn read_json<T: for<'a> Deserialize<'a> + Default>(path: &Path) -> Result<T> {
273    match read_bytes(path, MAX_META)? {
274        None => Ok(T::default()),
275        Some(b) => {
276            serde_json::from_slice(&b).map_err(|e| format!("invalid {}: {e}", path.display()))
277        }
278    }
279}
280fn json_bytes(value: &impl Serialize) -> Result<Vec<u8>> {
281    let b = serde_json::to_vec_pretty(value).map_err(err)?;
282    if b.len() > MAX_META {
283        Err("resource metadata/output exceeds 256 KiB".into())
284    } else {
285        Ok(b)
286    }
287}
288
289/// Stable lock inode: never removed, no PID heuristics, no blocking wait.
290#[derive(Debug)]
291pub struct Lease {
292    _file: File,
293}
294#[derive(Debug)]
295enum LeaseError {
296    Contended,
297    Other(String),
298}
299impl From<String> for LeaseError {
300    fn from(error: String) -> Self {
301        Self::Other(error)
302    }
303}
304impl From<LeaseError> for String {
305    fn from(error: LeaseError) -> Self {
306        match error {
307            LeaseError::Contended => {
308                "resource scope is in use (host or mutation lease); retry after owners exit".into()
309            }
310            LeaseError::Other(e) => format!("resource lease error: {e}"),
311        }
312    }
313}
314fn lock_error(error: std::io::Error) -> LeaseError {
315    let busy = fs2::lock_contended_error();
316    if error.raw_os_error().is_some() && error.raw_os_error() == busy.raw_os_error() {
317        LeaseError::Contended
318    } else {
319        LeaseError::Other(error.to_string())
320    }
321}
322impl Lease {
323    fn acquire(
324        root: &Path,
325        exclusive: bool,
326        create: bool,
327    ) -> std::result::Result<Option<Self>, LeaseError> {
328        checked_path(root)?;
329        if create {
330            fs::create_dir_all(root).map_err(err)?;
331        }
332        let path = root.join(LEASE);
333        checked_path(&path)?;
334        let file = match OpenOptions::new()
335            .read(true)
336            .write(true)
337            .create(create)
338            .open(path)
339        {
340            Ok(f) => f,
341            Err(e) if !create && e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
342            Err(e) => return Err(LeaseError::Other(err(e))),
343        };
344        let result = if exclusive {
345            fs2::FileExt::try_lock_exclusive(&file)
346        } else {
347            fs2::FileExt::try_lock_shared(&file)
348        };
349        result.map_err(lock_error)?;
350        Ok(Some(Self { _file: file }))
351    }
352}
353impl Drop for Lease {
354    fn drop(&mut self) {
355        let _ = fs2::FileExt::unlock(&self._file);
356    }
357}
358pub(crate) fn mutation_lease(root: &Path) -> Result<Lease> {
359    let lease = Lease::acquire(root, true, true)?.ok_or("missing resource lease")?;
360    ensure_complete(root)?;
361    Ok(lease)
362}
363
364/// Hold a shared resource lease while updating native package metadata. Native
365/// package installation is append/version based and therefore safe alongside
366/// live hosts, but it must still exclude resource mutations that could race
367/// with the registry/receipt ownership checks.
368pub(crate) fn shared_lease(root: &Path) -> Result<Lease> {
369    let lease = Lease::acquire(root, false, true)?.ok_or("missing resource lease")?;
370    ensure_complete(root)?;
371    Ok(lease)
372}
373
374fn ensure_complete(root: &Path) -> Result<()> {
375    if read_bytes(&root.join(JOURNAL), MAX_META)?.is_some() {
376        return Err(
377            "incomplete resource transaction; run rpi resource recover with this scope".into(),
378        );
379    }
380    Ok(())
381}
382pub(crate) fn host_leases(cwd: &Path) -> Result<Arc<Vec<Lease>>> {
383    let mut roots = vec![Scope::Project.root(cwd)?, Scope::Global.root(cwd)?];
384    roots.sort();
385    roots.dedup();
386    let mut leases = Vec::new();
387    for root in roots {
388        leases.push(shared_lease(&root)?);
389    }
390    Ok(Arc::new(leases))
391}
392fn observation_lease(root: &Path) -> Result<(Option<Lease>, bool)> {
393    // Retain the idle exclusive probe, or a shared lease alongside hosts.
394    // A mutator's exclusive lease refuses both; no unguarded half-state read.
395    match Lease::acquire(root, true, false) {
396        Ok(lease) => Ok((lease, false)),
397        Err(LeaseError::Contended) => Ok((Lease::acquire(root, false, false)?, true)),
398        Err(e) => Err(e.into()),
399    }
400}
401
402#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
403#[serde(deny_unknown_fields)]
404struct ResourceFile {
405    path: PathBuf,
406    bytes: u64,
407    sha256: String,
408}
409#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
410#[serde(deny_unknown_fields)]
411struct Inventory {
412    files: Vec<ResourceFile>,
413    directories: Vec<PathBuf>,
414    sha256: String,
415}
416#[derive(Default)]
417struct InventoryBudget {
418    entries: usize,
419    bytes: u64,
420}
421fn inventory(path: &Path) -> Result<Inventory> {
422    inventory_bounded(path, &mut InventoryBudget::default())
423}
424fn inventory_bounded(path: &Path, budget: &mut InventoryBudget) -> Result<Inventory> {
425    checked_path(path)?;
426    let mut files = Vec::new();
427    let mut directories = Vec::new();
428    fn walk(
429        root: &Path,
430        path: &Path,
431        files: &mut Vec<ResourceFile>,
432        dirs: &mut Vec<PathBuf>,
433        budget: &mut InventoryBudget,
434        depth: usize,
435    ) -> Result<()> {
436        if depth > 32 {
437            return Err("resource recursion depth exceeds 32".into());
438        }
439        if budget.entries >= MAX_FILES {
440            return Err("resource inventory exceeds 2048 entries".into());
441        }
442        budget.entries += 1;
443        let meta = fs::symlink_metadata(path).map_err(err)?;
444        if is_link(&meta) {
445            return Err(format!(
446                "resource link/reparse point rejected: {}",
447                path.display()
448            ));
449        }
450        let rel = path.strip_prefix(root).map_err(err)?;
451        if !rel.as_os_str().is_empty() {
452            relative(rel)?;
453        }
454        if meta.is_dir() {
455            if !rel.as_os_str().is_empty() {
456                dirs.push(rel.to_path_buf());
457            }
458            let remaining = MAX_FILES - budget.entries;
459            let mut entries = Vec::new();
460            for entry in fs::read_dir(path).map_err(err)?.take(remaining + 1) {
461                if entries.len() == remaining {
462                    return Err("resource directory exceeds remaining entry budget".into());
463                }
464                entries.push(entry.map_err(err)?);
465            }
466            entries.sort_by_key(|e| e.file_name());
467            for e in entries {
468                walk(root, &e.path(), files, dirs, budget, depth + 1)?;
469            }
470        } else if meta.is_file() {
471            if meta.len() > MAX_FILE || budget.bytes + meta.len() > MAX_BYTES {
472                return Err("resource files exceed byte limit (64 MiB/file, 256 MiB total)".into());
473            }
474            let mut input = File::open(path).map_err(err)?;
475            let mut hash = Sha256::new();
476            let mut size = 0;
477            let mut buf = [0; 16384];
478            loop {
479                let n = input.read(&mut buf).map_err(err)?;
480                if n == 0 {
481                    break;
482                }
483                size += n as u64;
484                budget.bytes += n as u64;
485                if size > MAX_FILE || budget.bytes > MAX_BYTES {
486                    return Err("resource changed/exceeded byte limit".into());
487                }
488                hash.update(&buf[..n]);
489            }
490            if size != meta.len() {
491                return Err("resource changed while reading".into());
492            }
493            files.push(ResourceFile {
494                path: if rel.as_os_str().is_empty() {
495                    PathBuf::from(".")
496                } else {
497                    rel.into()
498                },
499                bytes: size,
500                sha256: format!("{:x}", hash.finalize()),
501            });
502        } else {
503            return Err("special resource file rejected".into());
504        }
505        Ok(())
506    }
507    walk(path, path, &mut files, &mut directories, budget, 0)?;
508    let sha256 = format!(
509        "{:x}",
510        Sha256::digest(serde_json::to_vec(&(&files, &directories)).map_err(err)?)
511    );
512    Ok(Inventory {
513        files,
514        directories,
515        sha256,
516    })
517}
518#[derive(Debug, Clone, Serialize, Deserialize)]
519#[serde(deny_unknown_fields)]
520struct Receipt {
521    kind: Kind,
522    name: String,
523    source: PathBuf,
524    version: Option<String>,
525    target: PathBuf,
526    inventory: Inventory,
527}
528#[derive(Default, Serialize, Deserialize)]
529#[serde(deny_unknown_fields)]
530struct Receipts {
531    #[serde(default)]
532    resources: Vec<Receipt>,
533}
534fn receipts(root: &Path) -> Result<Receipts> {
535    let r: Receipts = read_json(&root.join(RECEIPTS))?;
536    let mut keys = BTreeSet::new();
537    if r.resources.len() > 128 {
538        return Err("resource receipts exceed 128 resources per scope".into());
539    }
540    for item in &r.resources {
541        identifier(&item.name)?;
542        if item.version.as_ref().is_some_and(|v| {
543            v.trim().is_empty() || v.len() > 128 || v.chars().any(char::is_control)
544        }) {
545            return Err("invalid resource receipt version".into());
546        }
547        if item.target != item.kind.target(&item.name)
548            || !item.source.is_absolute()
549            || !keys.insert(item.target.to_string_lossy().to_ascii_lowercase())
550        {
551            return Err("unsafe/duplicate resource receipt".into());
552        }
553        checked_path(&root.join(&item.target))?;
554        if item.inventory.files.len() + item.inventory.directories.len() > MAX_FILES {
555            return Err("oversized receipt inventory".into());
556        }
557        for file in &item.inventory.files {
558            if file.path != Path::new(".") {
559                relative(&file.path)?;
560            }
561        }
562        for dir in &item.inventory.directories {
563            relative(dir)?;
564        }
565    }
566    Ok(r)
567}
568#[derive(Default, Serialize, Deserialize)]
569#[serde(deny_unknown_fields)]
570struct Packages {
571    #[serde(default)]
572    packages: Vec<crate::packages::ProjectPackage>,
573}
574fn packages(root: &Path) -> Result<Packages> {
575    let p: Packages = read_json(&root.join("packages.json"))?;
576    let mut names = BTreeSet::new();
577    for item in &p.packages {
578        legacy_package_identity(&item.name)?;
579        if !names.insert(item.name.to_ascii_lowercase()) {
580            return Err("duplicate native package identity".into());
581        }
582        let mut artifacts = BTreeSet::new();
583        for a in &item.artifacts {
584            relative(Path::new(a))?;
585            reserved_identity(a)?;
586            if Path::new(a).components().count() != 1 || !artifacts.insert(a.to_ascii_lowercase()) {
587                return Err("unsafe/duplicate native artifact".into());
588            }
589        }
590    }
591    Ok(p)
592}
593pub(crate) fn legacy_unowned(root: &Path, kind: &str, name: &str) -> Result<()> {
594    let kind = Kind::parse(kind)?;
595    if receipts(root)?
596        .resources
597        .iter()
598        .any(|r| r.kind == kind && r.name.eq_ignore_ascii_case(name))
599    {
600        return Err("resource is managed by rpi resource; use its update/remove commands".into());
601    }
602    if kind == Kind::Package {
603        legacy_package_identity(name)?;
604        let package = root.join(kind.target(name));
605        legacy_unowned_targets(root, &[package.clone(), package.join("extensions")])?;
606    }
607    Ok(())
608}
609fn path_identity(path: &Path) -> Result<PathBuf> {
610    let path = if path.is_absolute() {
611        path.to_path_buf()
612    } else {
613        std::env::current_dir().map_err(err)?.join(path)
614    };
615    checked_path(&path)?;
616    // Existing components are canonicalized; absent leaves still compare by
617    // their absolute addressed path. Reparse paths are rejected, not followed.
618    let mut ancestor = path.as_path();
619    let mut tail = Vec::new();
620    while !ancestor.try_exists().map_err(err)? {
621        tail.push(
622            ancestor
623                .file_name()
624                .ok_or("no path ancestor")?
625                .to_os_string(),
626        );
627        ancestor = ancestor.parent().ok_or("no path ancestor")?;
628    }
629    let mut identity = fs::canonicalize(ancestor).map_err(err)?;
630    for component in tail.into_iter().rev() {
631        identity.push(component);
632    }
633    if cfg!(windows) {
634        Ok(PathBuf::from(identity.to_string_lossy().to_lowercase()))
635    } else {
636        Ok(identity)
637    }
638}
639pub(crate) fn resource_owned_names(root: &Path, kind: &str) -> Result<BTreeSet<String>> {
640    let kind = Kind::parse(kind)?;
641    Ok(receipts(root)?
642        .resources
643        .into_iter()
644        .filter(|r| r.kind == kind)
645        .map(|r| r.name)
646        .collect())
647}
648pub(crate) fn legacy_unowned_targets(root: &Path, targets: &[PathBuf]) -> Result<()> {
649    let targets = targets
650        .iter()
651        .map(|t| path_identity(t))
652        .collect::<Result<Vec<_>>>()?;
653    for r in receipts(root)?.resources {
654        let owned = path_identity(&root.join(&r.target))?;
655        if targets.iter().any(|t| t == &owned || t.starts_with(&owned)) {
656            return Err("target owned by rpi resource; refusing legacy replacement/removal".into());
657        }
658    }
659    Ok(())
660}
661
662#[derive(Serialize, Deserialize)]
663#[serde(deny_unknown_fields)]
664struct Change {
665    target: PathBuf,
666    old: Option<Inventory>,
667    new: Option<Inventory>,
668}
669#[derive(Serialize, Deserialize)]
670#[serde(deny_unknown_fields)]
671struct Journal {
672    directory: String,
673    changes: Vec<Change>,
674}
675fn transaction_target(p: &Path) -> Result<()> {
676    relative(p)?;
677    if p == Path::new(RECEIPTS) || p == Path::new("packages.json") {
678        return Ok(());
679    }
680    let parts = p
681        .iter()
682        .map(|p| p.to_str().ok_or("non-UTF8 target"))
683        .collect::<std::result::Result<Vec<_>, _>>()?;
684    if parts.len() != 2 || !matches!(parts[0], "packages" | "skills" | "extensions") {
685        return Err("unsafe transaction target".into());
686    }
687    identifier(parts[1])
688}
689fn remove_path(path: &Path) -> Result<()> {
690    checked_path(path)?;
691    let m = fs::symlink_metadata(path).map_err(err)?;
692    if m.is_dir() {
693        fs::remove_dir_all(path).map_err(err)
694    } else if m.is_file() {
695        fs::remove_file(path).map_err(err)
696    } else {
697        Err("unsafe removal".into())
698    }
699}
700fn rollback(root: &Path, j: &Journal) -> Result<()> {
701    let dir = root.join(&j.directory);
702    checked_path(&dir)?;
703    // Validate the complete recovery plan before changing any file.
704    for (i, c) in j.changes.iter().enumerate() {
705        transaction_target(&c.target)?;
706        let target = root.join(&c.target);
707        let backup = dir.join(format!("old-{i}"));
708        if backup.try_exists().map_err(err)? {
709            if Some(inventory(&backup)?) != c.old {
710                return Err("transaction backup changed; manual recovery required".into());
711            }
712            if target.try_exists().map_err(err)? && Some(inventory(&target)?) != c.new {
713                return Err("transaction target changed; refusing recovery deletion".into());
714            }
715        } else if target.try_exists().map_err(err)? {
716            let observed = inventory(&target)?;
717            if c.old.as_ref() != Some(&observed) && c.new.as_ref() != Some(&observed) {
718                return Err("transaction target differs from old/new inventory".into());
719            }
720            if c.old.is_some() && c.old.as_ref() != Some(&observed) {
721                return Err("missing rollback backup; manual recovery required".into());
722            }
723        } else if c.old.is_some() {
724            return Err("missing original/backup; manual recovery required".into());
725        }
726    }
727    for (i, c) in j.changes.iter().enumerate().rev() {
728        let target = root.join(&c.target);
729        let backup = dir.join(format!("old-{i}"));
730        if backup.try_exists().map_err(err)? {
731            if target.try_exists().map_err(err)? {
732                remove_path(&target)?;
733            }
734            fs::rename(&backup, &target).map_err(err)?;
735        } else if c.old.is_none() && target.try_exists().map_err(err)? {
736            remove_path(&target)?;
737        }
738    }
739    // Journal last: a failed rollback stays diagnosable and retryable.
740    fs::remove_file(root.join(JOURNAL)).map_err(err)?;
741    fs::remove_dir_all(dir).map_err(err)
742}
743fn recover(root: &Path) -> Result<bool> {
744    let Some(bytes) = read_bytes(&root.join(JOURNAL), MAX_META)? else {
745        return Ok(false);
746    };
747    let j: Journal = serde_json::from_slice(&bytes).map_err(err)?;
748    let suffix = j
749        .directory
750        .strip_prefix(".resource-txn-")
751        .ok_or("invalid transaction directory")?;
752    uuid::Uuid::parse_str(suffix).map_err(err)?;
753    if j.changes.len() > 3 {
754        return Err("oversized transaction plan".into());
755    }
756    let mut seen = BTreeSet::new();
757    for c in &j.changes {
758        transaction_target(&c.target)?;
759        if !seen.insert(&c.target) {
760            return Err("duplicate transaction target".into());
761        }
762    }
763    rollback(root, &j)?;
764    Ok(true)
765}
766fn write_new(path: &Path, bytes: &[u8]) -> Result<()> {
767    let mut f = OpenOptions::new()
768        .write(true)
769        .create_new(true)
770        .open(path)
771        .map_err(err)?;
772    f.write_all(bytes).and_then(|_| f.sync_all()).map_err(err)
773}
774/// Copy a resource snapshot into `target` and return the inventory of the copy.
775///
776/// The returned inventory can differ from `observed` on macOS: native artifacts
777/// are re-signed after copying (see `resign_snapshot_native`), which rewrites
778/// their bytes. Callers must record the **returned** inventory, not the source
779/// one, or every later ownership check fails.
780fn copy_snapshot(
781    kind: Kind,
782    source: &Path,
783    target: &Path,
784    observed: &Inventory,
785) -> Result<Inventory> {
786    fn copy_file(source: &Path, target: &Path, expected: u64) -> Result<()> {
787        checked_path(source)?;
788        let input = File::open(source).map_err(err)?;
789        #[cfg(unix)]
790        let mode = {
791            use std::os::unix::fs::PermissionsExt;
792            // Only ordinary rwx permissions; never copy setuid/setgid/sticky.
793            input.metadata().map_err(err)?.permissions().mode() & 0o777
794        };
795        let mut output = OpenOptions::new()
796            .write(true)
797            .create_new(true)
798            .open(target)
799            .map_err(err)?;
800        let copied = std::io::copy(&mut input.take(expected + 1), &mut output).map_err(err)?;
801        if copied != expected {
802            return Err("resource changed during bounded copy".into());
803        }
804        #[cfg(unix)]
805        {
806            use std::os::unix::fs::PermissionsExt;
807            output
808                .set_permissions(fs::Permissions::from_mode(mode))
809                .map_err(err)?;
810        }
811        output.sync_all().map_err(err)
812    }
813    if source.is_file() {
814        copy_file(
815            source,
816            target,
817            observed
818                .files
819                .first()
820                .ok_or("missing file inventory")?
821                .bytes,
822        )?;
823    } else {
824        fs::create_dir(target).map_err(err)?;
825        for d in &observed.directories {
826            fs::create_dir_all(target.join(d)).map_err(err)?;
827        }
828        for f in &observed.files {
829            let to = target.join(&f.path);
830            if let Some(p) = to.parent() {
831                fs::create_dir_all(p).map_err(err)?;
832            }
833            copy_file(&source.join(&f.path), &to, f.bytes)?;
834        }
835    }
836    if inventory(source)? != *observed {
837        return Err("resource source changed during snapshot copy".into());
838    }
839    // Which copied files are loadable native artifacts is decided by the resource
840    // KIND, because only the kind's validator reserves a place for one:
841    //   - Extension: the single-file source is the artifact, even though its
842    //     inventory path is `.` with no extension
843    //   - Package: only direct children of `extensions/` (see `validate_source`),
844    //     so a `.dylib` under `skills/`, `prompts/` or `themes/` is data
845    //   - Skill: nothing. A skill only has to contain SKILL.md, so a same-suffix
846    //     asset means nothing and must never be handed to `codesign`
847    let native: std::collections::BTreeSet<PathBuf> = match kind {
848        Kind::Extension => observed.files.iter().map(|f| f.path.clone()).collect(),
849        Kind::Package => observed
850            .files
851            .iter()
852            .map(|f| f.path.clone())
853            .filter(|p| is_package_artifact(p))
854            .collect(),
855        Kind::Skill => std::collections::BTreeSet::new(),
856    };
857    let artifacts: Vec<PathBuf> = native.iter().map(|p| target.join(p)).collect();
858    resign_snapshot_native(&artifacts)?;
859    let copied = inventory(target)?;
860    // Every byte outside the re-signed artifacts must still match the source.
861    let comparable = |inv: &Inventory| Inventory {
862        files: inv
863            .files
864            .iter()
865            .filter(|f| !native.contains(&f.path))
866            .cloned()
867            .collect(),
868        directories: inv.directories.clone(),
869        sha256: String::new(),
870    };
871    if comparable(&copied) != comparable(observed) {
872        return Err("resource snapshot copy does not match the source".into());
873    }
874    Ok(copied)
875}
876
877/// Whether a Package file is a native artifact: a direct child of `extensions/`
878/// with the platform library extension. This mirrors what `validate_source`
879/// accepts, so data files that merely use the same suffix elsewhere in the tree
880/// are never handed to `codesign`.
881fn is_package_artifact(path: &Path) -> bool {
882    is_native_artifact(path) && path.components().count() == 2 && path.starts_with("extensions")
883}
884
885/// Whether a path uses this platform's native library extension.
886fn is_native_artifact(path: &Path) -> bool {
887    path.extension().and_then(|s| s.to_str()) == Some(std::env::consts::DLL_EXTENSION)
888}
889/// macOS only: re-sign copied native artifacts so the kernel will map them.
890///
891/// A signed Mach-O carries a code directory whose hashes must match the bytes
892/// the kernel maps. Copying is byte-exact, so a well-formed copy stays valid —
893/// but a plain `io::copy` of a freshly linked cargo `cdylib` can also carry a
894/// linker signature the kernel refuses. When it refuses, it does not return an
895/// error: it kills the host that touched the page.
896///
897/// ```text
898/// CODE SIGNING: process 20087[rpi]: rejecting invalid page at address 0x…
899///   in file "~/.pidd/rpi-agent/extensions/librpi_superagent.dylib"
900///   (cs_mtime:1794052202.55 != mtime:1794056571.77)
901/// CODE SIGNING: cs_invalid_page(0x…): denying page sending SIGKILL
902/// ```
903///
904/// No stderr reaches the caller and the only exit status is 137, so the symptom
905/// reads as "rpi cannot start at all". `codesign -v` also reports these copies
906/// as valid, so verifying first is not a substitute for signing.
907///
908/// Only native artifacts are re-signed; text resources are left untouched.
909fn resign_snapshot_native(artifacts: &[PathBuf]) -> Result<()> {
910    if !cfg!(target_os = "macos") {
911        return Ok(());
912    }
913    for path in artifacts {
914        // Pin the ad-hoc identifier: `codesign --sign -` otherwise derives it from
915        // the file NAME, so the same bytes staged under two different names sign
916        // to different bytes. That would make an unchanged install look changed.
917        let output = std::process::Command::new("/usr/bin/codesign")
918            .args(["--force", "--sign", "-", "--identifier", "com.rpi.native"])
919            .arg(path)
920            .output()
921            .map_err(|e| format!("could not run codesign on {}: {e}", path.display()))?;
922        if !output.status.success() {
923            return Err(format!(
924                "could not re-sign {} (the kernel will refuse to load it): {}",
925                path.display(),
926                String::from_utf8_lossy(&output.stderr).trim()
927            ));
928        }
929    }
930    Ok(())
931}
932
933/// Small same-filesystem transaction; injection is unit-test-only, never an env backdoor.
934fn publish(
935    root: &Path,
936    changes: Vec<(PathBuf, Option<PathBuf>)>,
937    stage: tempfile::TempDir,
938    mut before: impl FnMut(usize) -> Result<()>,
939) -> Result<Vec<String>> {
940    let directory = stage
941        .path()
942        .file_name()
943        .and_then(|s| s.to_str())
944        .ok_or("invalid staging directory")?
945        .to_owned();
946    let mut j = Journal {
947        directory,
948        changes: Vec::new(),
949    };
950    for (target, new) in &changes {
951        transaction_target(target)?;
952        let path = root.join(target);
953        checked_path(&path)?;
954        j.changes.push(Change {
955            target: target.clone(),
956            old: if path.try_exists().map_err(err)? {
957                Some(inventory(&path)?)
958            } else {
959                None
960            },
961            new: new.as_ref().map(|p| inventory(p)).transpose()?,
962        });
963    }
964    let journal_stage = stage.path().join("journal");
965    write_new(&journal_stage, &json_bytes(&j)?)?;
966    fs::rename(&journal_stage, root.join(JOURNAL)).map_err(err)?;
967    // Do not auto-delete backup evidence if interrupted or rollback itself fails.
968    let _owned = stage.keep();
969    let result = (|| {
970        for (i, (target, new)) in changes.iter().enumerate() {
971            before(i * 2)?;
972            let path = root.join(target);
973            if j.changes[i].old.is_some() {
974                fs::rename(&path, root.join(&j.directory).join(format!("old-{i}"))).map_err(err)?;
975            }
976            before(i * 2 + 1)?;
977            if let Some(new) = new {
978                fs::rename(new, &path).map_err(err)?;
979            }
980        }
981        before(changes.len() * 2)?;
982        Ok(())
983    })();
984    if let Err(error) = result {
985        return match rollback(root, &j) {
986            Ok(()) => Err(error),
987            Err(r) => Err(format!(
988                "{error}; rollback incomplete: {r}; run resource recover"
989            )),
990        };
991    }
992    // Commit point: removing the journal makes installed bytes authoritative.
993    // Orphan staging after this point contains only old bytes, never active data.
994    if let Err(error) = fs::remove_file(root.join(JOURNAL)) {
995        return match rollback(root,&j) {
996            Ok(())=>Err(format!("commit marker removal failed: {error}; rolled back")),
997            Err(rollback)=>Err(format!("commit marker removal failed: {error}; rollback incomplete: {rollback}; run resource recover")),
998        };
999    }
1000    if let Err(e) = fs::remove_dir_all(root.join(&j.directory)) {
1001        return Ok(vec![format!(
1002            "committed; backup cleanup pending (OS error {:?}, {:?})",
1003            e.raw_os_error(),
1004            e.kind()
1005        )]);
1006    }
1007    Ok(Vec::new())
1008}
1009fn stage_dir(root: &Path) -> Result<tempfile::TempDir> {
1010    tempfile::Builder::new()
1011        .prefix(&format!(".resource-txn-{}", uuid::Uuid::new_v4().simple()))
1012        .rand_bytes(0)
1013        .tempdir_in(root)
1014        .map_err(err)
1015}
1016
1017#[derive(Default)]
1018struct Options {
1019    op: String,
1020    kind: Option<Kind>,
1021    name: Option<String>,
1022    scope: Option<Scope>,
1023    source: Option<PathBuf>,
1024    version: Option<String>,
1025}
1026fn parse(args: &[String]) -> Result<Options> {
1027    let mut o = Options {
1028        op: args.first().ok_or("missing resource operation")?.clone(),
1029        ..Default::default()
1030    };
1031    let mut pos = Vec::new();
1032    let mut i = 1;
1033    while i < args.len() {
1034        let a = &args[i];
1035        if matches!(a.as_str(), "--scope" | "--path" | "--version") {
1036            i += 1;
1037            let v = args.get(i).ok_or("missing option value")?;
1038            if v.starts_with('-') {
1039                return Err("missing option value".into());
1040            }
1041            match a.as_str() {
1042                "--scope" => {
1043                    if o.scope.is_some() {
1044                        return Err("duplicate scope".into());
1045                    }
1046                    o.scope = Some(match v.as_str() {
1047                        "project" => Scope::Project,
1048                        "global" => Scope::Global,
1049                        _ => return Err("scope must be project or global".into()),
1050                    });
1051                }
1052                "--path" => {
1053                    if o.source.replace(v.into()).is_some() {
1054                        return Err("duplicate path".into());
1055                    }
1056                }
1057                _ => {
1058                    if v.trim().is_empty()
1059                        || v.len() > 128
1060                        || v.chars().any(char::is_control)
1061                        || o.version.replace(v.clone()).is_some()
1062                    {
1063                        return Err("invalid/duplicate version".into());
1064                    }
1065                }
1066            }
1067        } else if a.starts_with('-') {
1068            return Err(format!(
1069                "unknown resource option {a}; builds require explicit native package link/install"
1070            ));
1071        } else {
1072            pos.push(a.clone());
1073        }
1074        i += 1;
1075    }
1076    if o.scope.is_none() {
1077        return Err("explicit --scope project|global is required".into());
1078    }
1079    if matches!(o.op.as_str(), "list" | "recover") {
1080        if !pos.is_empty() || o.source.is_some() || o.version.is_some() {
1081            return Err("unexpected list/recover arguments".into());
1082        }
1083    } else if matches!(
1084        o.op.as_str(),
1085        "inspect" | "install" | "update" | "reinstall" | "remove"
1086    ) {
1087        if pos.len() != 2 {
1088            return Err("expected KIND NAME".into());
1089        }
1090        o.kind = Some(Kind::parse(&pos[0])?);
1091        if o.op == "inspect" && o.kind == Some(Kind::Package) {
1092            legacy_package_identity(&pos[1])?;
1093        } else {
1094            identifier(&pos[1])?;
1095        }
1096        o.name = Some(pos[1].clone());
1097        let mutation = matches!(o.op.as_str(), "install" | "update" | "reinstall");
1098        if mutation != o.source.is_some() || (!mutation && o.version.is_some()) {
1099            return Err("install/update/reinstall require --path; inspect/remove reject it".into());
1100        }
1101    } else {
1102        return Err("unknown resource operation".into());
1103    }
1104    Ok(o)
1105}
1106fn validate_source(kind: Kind, source: &Path, inv: &Inventory) -> Result<()> {
1107    match kind {
1108        Kind::Skill => {
1109            if !source.is_dir() || !source.join("SKILL.md").is_file() {
1110                return Err("standalone Skill requires a directory containing SKILL.md".into());
1111            }
1112            let text =
1113                read_bytes(&source.join("SKILL.md"), 64 * 1024)?.ok_or("missing SKILL.md")?;
1114            std::str::from_utf8(&text).map_err(err)?;
1115        }
1116        Kind::Extension => {
1117            if !source.is_file()
1118                || source.extension().and_then(|s| s.to_str())
1119                    != Some(std::env::consts::DLL_EXTENSION)
1120            {
1121                return Err("extension source must be a platform native library file".into());
1122            }
1123        }
1124        Kind::Package => {
1125            if !source.is_dir() {
1126                return Err("package source must be a directory".into());
1127            }
1128            if !["extensions", "skills", "prompts", "themes"]
1129                .iter()
1130                .any(|s| source.join(s).is_dir())
1131                && inv.files.is_empty()
1132            {
1133                return Err("empty package".into());
1134            }
1135            for f in &inv.files {
1136                if f.path.starts_with("extensions")
1137                    && (f.path.components().count() != 2
1138                        || f.path.extension().and_then(|s| s.to_str())
1139                            != Some(std::env::consts::DLL_EXTENSION))
1140                {
1141                    return Err(
1142                        "Package extensions must be direct platform native artifacts".into(),
1143                    );
1144                }
1145            }
1146        }
1147    }
1148    Ok(())
1149}
1150fn explicit_source(path: &Path) -> Result<PathBuf> {
1151    let addressed = std::env::current_dir().map_err(err)?.join(path);
1152    let mut normalized = PathBuf::new();
1153    for component in addressed.components() {
1154        match component {
1155            Component::CurDir => {}
1156            Component::ParentDir => {
1157                checked_path(&normalized)?;
1158                if !normalized.pop() {
1159                    return Err("source escapes filesystem root".into());
1160                }
1161            }
1162            other => {
1163                normalized.push(other);
1164                // A Windows drive prefix (`C:` / `\\\\?\\C:`) becomes
1165                // absolute only after its RootDir component is appended.
1166                if !matches!(other, Component::Prefix(_)) && normalized.is_absolute() {
1167                    checked_path(&normalized)?;
1168                }
1169            }
1170        }
1171    }
1172    fs::canonicalize(normalized).map_err(err)
1173}
1174fn mutate(root: &Path, o: &Options) -> Result<Value> {
1175    let _lease = mutation_lease(root)?;
1176    let mut r = receipts(root)?;
1177    let mut p = packages(root)?;
1178    let native: Vec<crate::install::InstalledNativePackage> =
1179        read_json(&root.join("native-packages.json"))?;
1180    let kind = o.kind.ok_or("missing kind")?;
1181    let name = o.name.as_deref().ok_or("missing name")?;
1182    let target = kind.target(name);
1183    let path = root.join(&target);
1184    checked_path(&path)?;
1185    let previous = r
1186        .resources
1187        .iter()
1188        .find(|r| r.kind == kind && r.name.eq_ignore_ascii_case(name))
1189        .cloned();
1190    for owned in r.resources.iter().filter(|r| r.kind == Kind::Package) {
1191        if p.packages.iter().find(|p| p.name == owned.name) != Some(&package_record(owned)) {
1192            return Err(
1193                "owned package receipt disagrees with native packages.json; refusing mutation"
1194                    .into(),
1195            );
1196        }
1197    }
1198    if kind == Kind::Extension
1199        && native.iter().any(|p| {
1200            p.name.eq_ignore_ascii_case(name)
1201                || p.artifacts.iter().any(|a| {
1202                    target
1203                        .file_name()
1204                        .is_some_and(|f| f.to_string_lossy().eq_ignore_ascii_case(a))
1205                })
1206        })
1207    {
1208        return Err(
1209            "extension identity/artifact owned by legacy native registry (even if missing)".into(),
1210        );
1211    }
1212    if let Some(old) = &previous {
1213        if old.name != name {
1214            return Err("identity case collision".into());
1215        }
1216        if inventory(&path)? != old.inventory {
1217            return Err("owned resource content changed; refusing overwrite/removal".into());
1218        }
1219    }
1220    if o.op == "remove" && previous.is_none() {
1221        if path.try_exists().map_err(err)? {
1222            return Err("unmanaged destination: refusing removal".into());
1223        }
1224        return Ok(json!({"outcome":"absent","kind":kind,"name":name}));
1225    }
1226    if previous.is_none() && path.try_exists().map_err(err)? {
1227        return Err("unmanaged destination conflict".into());
1228    }
1229    if kind == Kind::Package
1230        && previous.is_none()
1231        && p.packages.iter().any(|p| p.name.eq_ignore_ascii_case(name))
1232    {
1233        return Err("package identity owned by native package link".into());
1234    }
1235    if matches!(o.op.as_str(), "update" | "reinstall") && previous.is_none() {
1236        return Err("resource is not owned/installed; use install".into());
1237    }
1238    let source = o
1239        .source
1240        .as_ref()
1241        .map(|path| explicit_source(path))
1242        .transpose()?;
1243    if let Some(source) = &source {
1244        let source_identity = path_identity(source)?;
1245        let root_identity = path_identity(root)?;
1246        if source_identity.starts_with(&root_identity)
1247            || root_identity.starts_with(&source_identity)
1248        {
1249            return Err("source/destination resource trees overlap".into());
1250        }
1251    }
1252    let stage = stage_dir(root)?;
1253    let mut next = None;
1254    if let Some(source) = source {
1255        // No Cargo metadata/build or native loader is called here.
1256        let inv = inventory(&source)?;
1257        validate_source(kind, &source, &inv)?;
1258        let version = o
1259            .version
1260            .clone()
1261            .or_else(|| previous.as_ref().and_then(|r| r.version.clone()));
1262        // Copy before building the receipt: on macOS the staged native artifacts
1263        // are re-signed, so the copy's inventory (which is what gets installed and
1264        // later re-verified against) differs from the source's.
1265        let staged = stage.path().join("snapshot");
1266        let copied = copy_snapshot(kind, &source, &staged, &inv)?;
1267        let receipt = Receipt {
1268            kind,
1269            name: name.into(),
1270            source,
1271            version,
1272            target: target.clone(),
1273            inventory: copied,
1274        };
1275        if o.op == "install" {
1276            if let Some(old) = &previous {
1277                if old.source == receipt.source
1278                    && old.version == receipt.version
1279                    && old.inventory == receipt.inventory
1280                {
1281                    return Ok(
1282                        json!({"outcome":"unchanged","resource":observe(root,o.scope.unwrap(),old,false)?}),
1283                    );
1284                }
1285                return Err("already installed; use update or reinstall with --path".into());
1286            }
1287        }
1288        next = Some((receipt, staged));
1289    }
1290    r.resources.retain(|r| !(r.kind == kind && r.name == name));
1291    if kind == Kind::Package {
1292        p.packages.retain(|p| p.name != name);
1293        if let Some((new, _)) = &next {
1294            p.packages.push(package_record(new));
1295        }
1296    }
1297    if let Some((receipt, _)) = &next {
1298        r.resources.push(receipt.clone());
1299    }
1300    if r.resources.len() > 128 {
1301        return Err("resource receipts exceed 128 resources per scope".into());
1302    }
1303    r.resources.sort_by_key(|r| r.target.clone());
1304    p.packages.sort_by(|a, b| a.name.cmp(&b.name));
1305    let receipt_stage = stage.path().join("receipts");
1306    write_new(&receipt_stage, &json_bytes(&r)?)?;
1307    let mut changes = vec![
1308        (target, next.as_ref().map(|(_, p)| p.clone())),
1309        (PathBuf::from(RECEIPTS), Some(receipt_stage)),
1310    ];
1311    if kind == Kind::Package {
1312        let staged = stage.path().join("packages");
1313        write_new(&staged, &json_bytes(&p)?)?;
1314        changes.push((PathBuf::from("packages.json"), Some(staged)));
1315    }
1316    let mut data = json!({"outcome":if o.op=="remove"{"removed"}else if previous.is_some(){"replaced"}else{"installed"},"kind":kind,"name":name,"diagnostics":[],"resource":next.as_ref().map(|(r,_)|observe_inventory(root,o.scope.unwrap(),r,false,Ok(r.inventory.clone()))).transpose()?});
1317    // Bound the exact public envelope before publication. Reserve space for a
1318    // bounded post-commit backup-cleanup diagnostic (not a false operation error).
1319    if json_bytes(&success(o, data.clone()))
1320        .map_err(|error| format!("resource success result rejected before publication: {error}"))?
1321        .len()
1322        > MAX_META - 4096
1323    {
1324        return Err("resource success result exceeds bounded publication budget".into());
1325    }
1326    let parent = path.parent().ok_or("no resource parent")?;
1327    checked_path(parent)?;
1328    fs::create_dir_all(parent).map_err(err)?;
1329    let diagnostics = publish(root, changes, stage, |_| Ok(()))?;
1330    data["diagnostics"] = json!(diagnostics);
1331    Ok(data)
1332}
1333fn package_record(r: &Receipt) -> crate::packages::ProjectPackage {
1334    crate::packages::ProjectPackage {
1335        name: r.name.clone(),
1336        version: r.version.clone().unwrap_or_default(),
1337        source: r.source.to_string_lossy().into_owned(),
1338        cargo_package: String::new(),
1339        artifacts: r
1340            .inventory
1341            .files
1342            .iter()
1343            .filter(|f| f.path.starts_with("extensions"))
1344            .filter_map(|f| {
1345                f.path
1346                    .file_name()
1347                    .and_then(|s| s.to_str())
1348                    .map(str::to_owned)
1349            })
1350            .collect(),
1351    }
1352}
1353fn observe(root: &Path, scope: Scope, r: &Receipt, busy: bool) -> Result<Value> {
1354    observe_bounded(root, scope, r, busy, &mut InventoryBudget::default())
1355}
1356fn observe_bounded(
1357    root: &Path,
1358    scope: Scope,
1359    r: &Receipt,
1360    busy: bool,
1361    budget: &mut InventoryBudget,
1362) -> Result<Value> {
1363    let observed = inventory_bounded(&root.join(&r.target), budget);
1364    observe_inventory(root, scope, r, busy, observed)
1365}
1366fn observe_inventory(
1367    root: &Path,
1368    scope: Scope,
1369    r: &Receipt,
1370    busy: bool,
1371    observed: Result<Inventory>,
1372) -> Result<Value> {
1373    let mut diagnostics =
1374        vec!["native ABI compatibility is unverified; inspection never loads code".to_string()];
1375    if observed.as_ref().ok() != Some(&r.inventory) {
1376        diagnostics.push("installed content is missing or differs from ownership receipt".into());
1377    }
1378    if let Err(e) = &observed {
1379        diagnostics.push(e.clone());
1380    }
1381    Ok(
1382        json!({"kind":r.kind,"scope":scope,"identity":format!("{}:{}:{}",if scope==Scope::Project{"project"}else{"global"},r.kind.dir(),r.name),"name":r.name,"source":r.source,"version":r.version,"ownership":"resource","installed":true,"inUse":busy,"loaded":null,"applied":null,"path":root.join(&r.target),"selectable":selectable(root,scope,r),"recordedDigest":r.inventory.sha256,"observed":observed.ok(),"diagnostics":diagnostics}),
1383    )
1384}
1385fn selectable(root: &Path, scope: Scope, r: &Receipt) -> Value {
1386    match r.kind {
1387        Kind::Package => json!({"main_package":{"name":r.name,"scope":scope,"instructions":[]}}),
1388        Kind::Skill => json!({"skills":[root.join(&r.target)]}),
1389        Kind::Extension => json!({"extensions":[root.join(&r.target)]}),
1390    }
1391}
1392fn catalog(root: &Path, o: &Options) -> Result<Value> {
1393    checked_path(root)?;
1394    let (_observation_lease, busy) = observation_lease(root)?;
1395    let r = receipts(root)?;
1396    let p = packages(root)?;
1397    let native: Vec<crate::install::InstalledNativePackage> =
1398        read_json(&root.join("native-packages.json"))?;
1399    let scope = o.scope.unwrap();
1400    let mut items = Vec::new();
1401    let mut owned = BTreeSet::new();
1402    let mut budget = InventoryBudget::default();
1403    for receipt in &r.resources {
1404        owned.insert(receipt.target.clone());
1405        items.push(observe_bounded(root, scope, receipt, busy, &mut budget)?);
1406    }
1407    for kind in [Kind::Package, Kind::Skill, Kind::Extension] {
1408        let directory = root.join(kind.dir());
1409        checked_path(&directory)?;
1410        let entries = match fs::read_dir(directory) {
1411            Ok(e) => e,
1412            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
1413            Err(e) => return Err(err(e)),
1414        };
1415        for entry in entries {
1416            let entry = entry.map_err(err)?;
1417            let target = Path::new(kind.dir()).join(entry.file_name());
1418            if owned.contains(&target) {
1419                continue;
1420            }
1421            if items.len() >= MAX_FILES {
1422                return Err("catalog exceeds entry limit".into());
1423            }
1424            let name = if kind == Kind::Extension {
1425                entry
1426                    .path()
1427                    .file_stem()
1428                    .and_then(|s| s.to_str())
1429                    .unwrap_or("")
1430                    .to_string()
1431            } else {
1432                entry
1433                    .file_name()
1434                    .to_str()
1435                    .ok_or("non UTF8 candidate")?
1436                    .to_string()
1437            };
1438            let pkg = p
1439                .packages
1440                .iter()
1441                .find(|p| kind == Kind::Package && p.name == name);
1442            let ext = native.iter().find(|p| {
1443                kind == Kind::Extension
1444                    && p.artifacts
1445                        .iter()
1446                        .any(|a| entry.file_name() == std::ffi::OsStr::new(a))
1447            });
1448            let inv = inventory_bounded(&entry.path(), &mut budget);
1449            let diagnostics = match &inv {
1450                Ok(_) => vec!["unverified candidate; no code executed".to_string()],
1451                Err(e) => vec![e.clone()],
1452            };
1453            items.push(json!({"kind":kind,"scope":scope,"identity":format!("{}:{}:{}",if scope==Scope::Project{"project"}else{"global"},kind.dir(),name),"name":name,"source":pkg.map(|p|p.source.clone()).or_else(||ext.and_then(|p|p.source.clone())),"version":pkg.and_then(|p|if p.version.is_empty(){None}else{Some(&p.version)}).or_else(||ext.filter(|p|!p.version.trim().is_empty()).map(|p|&p.version)),"ownership":if pkg.is_some()||ext.is_some(){"legacy-native"}else{"unmanaged"},"installed":pkg.is_some()||ext.is_some(),"inUse":busy,"loaded":null,"applied":null,"path":entry.path(),"observed":inv.ok(),"diagnostics":diagnostics}));
1454        }
1455    }
1456    if o.op == "inspect" {
1457        items.retain(|item| item["name"] == json!(o.name) && item["kind"] == json!(o.kind));
1458        if items.is_empty() {
1459            return Err("resource not found".into());
1460        }
1461    }
1462    items.sort_by_key(|i| i["identity"].as_str().unwrap_or("").to_string());
1463    // When no persistent lock existed initially, first publication could have
1464    // started during traversal. Its lock inode is never unlinked: reject and
1465    // retry rather than returning any potentially mixed observation.
1466    if _observation_lease.is_none() && root.join(LEASE).try_exists().map_err(err)? {
1467        return Err("resource ownership initialized during observation; retry catalog".into());
1468    }
1469    Ok(
1470        json!({"outcome":"observed","resources":items,"diagnostics":if root.join(JOURNAL).try_exists().map_err(err)?{vec!["incomplete transaction; run resource recover"]}else{vec![]}}),
1471    )
1472}
1473fn success(o: &Options, data: Value) -> Value {
1474    json!({"version":1,"success":true,"operation":o.op,"scope":o.scope,"kind":o.kind,"name":o.name,"data":data})
1475}
1476pub fn run(args: &[String]) -> i32 {
1477    if args.is_empty() || args.iter().any(|a| matches!(a.as_str(), "--help" | "-h")) {
1478        println!("rpi resource list|inspect|install|update|reinstall|remove|recover --scope project|global\nMutations: KIND NAME --path PATH [--version TEXT]; remove/inspect: KIND NAME.\nLocal snapshots only; no builds or native registration. JSON v1, 256 KiB bound.");
1479        return 0;
1480    }
1481    let result: Result<Value> = (|| {
1482        let o = parse(args)?;
1483        let cwd = std::env::current_dir().map_err(err)?;
1484        let root = o.scope.unwrap().root(&cwd)?;
1485        let data = if matches!(o.op.as_str(), "list" | "inspect") {
1486            catalog(&root, &o)?
1487        } else if o.op == "recover" {
1488            let _lease = Lease::acquire(&root, true, true)?;
1489            json!({"outcome":if recover(&root)?{"recovered"}else{"unchanged"}})
1490        } else {
1491            mutate(&root, &o)?
1492        };
1493        Ok(success(&o, data))
1494    })();
1495    match result.and_then(|v| json_bytes(&v)) {
1496        Ok(bytes) => {
1497            println!("{}", String::from_utf8_lossy(&bytes));
1498            0
1499        }
1500        Err(e) => {
1501            let bounded = e.chars().take(2048).collect::<String>();
1502            let context = parse(args).ok();
1503            println!(
1504                "{}",
1505                json!({"version":1,"success":false,"operation":context.as_ref().map(|o|&o.op),"scope":context.as_ref().and_then(|o|o.scope),"kind":context.as_ref().and_then(|o|o.kind),"name":context.as_ref().and_then(|o|o.name.as_ref()),"error":bounded})
1506            );
1507            1
1508        }
1509    }
1510}
1511
1512#[cfg(test)]
1513mod regressions;
1514#[cfg(test)]
1515mod tests;