agix-base 0.5.0

Agix — foundation types, traits, and context
Documentation
//! The judgments the engine makes on its own account, outside any tool's
//! facts, about how far a scope reaches: what a rewind may restore, where a
//! plugin command or a host-started delegate may write, and whether a
//! narrow-only grant stays narrow. The permission engine implements them; a
//! build without it has none, and the engine then writes nowhere and keeps no
//! narrow-only grant on them.

use async_trait::async_trait;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use super::{Grant, GrantError};
use crate::interface::tool::ToolContext;
use crate::paths::SensitivePaths;
use crate::permission::PermissionRule;

#[async_trait]
pub trait PathGuard: Send + Sync + std::fmt::Debug {
    /// Whether a rewind may write `target` back, judged from `ctx`:
    /// `Ok(true)` when it lies outside the session's writable roots, `Err`
    /// with why when it must not be written at all.
    async fn restore_check(&self, ctx: &ToolContext, target: &Path) -> Result<bool, String>;
    /// A plugin command's `writes` globs below `root`: the scope kept, and the
    /// globs left out because they reach beyond the project.
    fn command_writes(&self, globs: &[String], root: &Path) -> (Arc<dyn WriteScope>, Vec<String>);
    /// A host-started delegate's `writes`, every glob kept, a relative one
    /// anchored at `cwd`.
    fn host_writes(&self, globs: &[String], cwd: &Path) -> Arc<dyn WriteScope>;
    /// Whether writing `path` from `cwd` reaches configuration under any of
    /// its spellings.
    fn reaches_configuration(&self, path: &str, cwd: &Path, paths: &SensitivePaths) -> bool;
    /// `Err(TooBroad)` for a narrow-only grant that reaches as far as the
    /// bare tool, or out of the directory it is narrow within.
    fn admits(&self, grant: &Grant) -> Result<(), GrantError>;
}

/// The files one run may write.
pub trait WriteScope: Send + Sync + std::fmt::Debug {
    /// Whether writing `path`, as a tool resolves it from `cwd`, stays inside.
    fn covers(&self, path: &str, cwd: &Path) -> bool;
    /// An `Edit(...)` allow per glob: what a delegated grant takes.
    fn grants(&self) -> Vec<PermissionRule>;
    /// The directories the globs lie under, each inside `root`.
    fn fixed_prefixes_within(&self, root: &Path) -> Vec<PathBuf>;
}

/// The scope where no [`PathGuard`] can judge one: it covers nothing.
#[derive(Debug, Default)]
pub struct NoWrites;

impl WriteScope for NoWrites {
    fn covers(&self, _: &str, _: &Path) -> bool {
        false
    }
    fn grants(&self) -> Vec<PermissionRule> {
        Vec::new()
    }
    fn fixed_prefixes_within(&self, _: &Path) -> Vec<PathBuf> {
        Vec::new()
    }
}