feat(permissions): implement permission system with plan mode enforcement (M1 complete)

This commit implements the complete M1 milestone (Config & Permissions) including:

- New permissions crate with Tool, Action, Mode, and PermissionManager
- Three permission modes: Plan (read-only default), AcceptEdits, Code
- Pattern matching for permission rules (exact match and prefix with *)
- Integration with config-agent for mode-based permission management
- CLI integration with --mode flag to override configured mode
- Permission checks for Read, Glob, and Grep operations
- Comprehensive test suite (10 tests in permissions, 4 in config, 4 in CLI)

Also fixes:
- Fixed failing test in tools-fs (glob pattern issue)
- Improved glob_list() root extraction to handle patterns like "/*.txt"

All 21 workspace tests passing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-01 19:14:54 +01:00
parent baf833427a
commit a6cf8585ef
12 changed files with 493 additions and 24 deletions

View File

@@ -2,8 +2,10 @@
members = [
"crates/app/cli",
"crates/llm/ollama",
"crates/platform/config"
, "crates/tools/fs"]
"crates/platform/config",
"crates/platform/permissions",
"crates/tools/fs",
]
resolver = "2"
[workspace.package]

View File

@@ -14,6 +14,7 @@ color-eyre = "0.6"
llm-ollama = { path = "../../llm/ollama" }
tools-fs = { path = "../../tools/fs" }
config-agent = { package = "config-agent", path = "../../platform/config" }
permissions = { path = "../../platform/permissions" }
futures-util = "0.3.31"
[dev-dependencies]
@@ -21,3 +22,4 @@ assert_cmd = "2.0"
predicates = "3.1"
httpmock = "0.7"
tokio = { version = "1.39", features = ["macros", "rt-multi-thread"] }
tempfile = "3.23.0"

View File

@@ -1,8 +1,9 @@
use clap::Parser;
use color_eyre::eyre::Result;
use color_eyre::eyre::{Result, eyre};
use config_agent::load_settings;
use futures_util::TryStreamExt;
use llm_ollama::{OllamaClient, OllamaOptions, types::ChatMessage};
use permissions::{PermissionDecision, Tool};
use std::io::{self, Write};
#[derive(clap::Subcommand, Debug)]
@@ -23,6 +24,9 @@ struct Args {
api_key: Option<String>,
#[arg(long)]
print: bool,
/// Override the permission mode (plan, acceptEdits, code)
#[arg(long)]
mode: Option<String>,
#[arg()]
prompt: Vec<String>,
#[command(subcommand)]
@@ -33,27 +37,74 @@ struct Args {
async fn main() -> Result<()> {
color_eyre::install()?;
let args = Args::parse();
let settings = load_settings(None).unwrap_or_default();
let mut settings = load_settings(None).unwrap_or_default();
// Override mode if specified via CLI
if let Some(mode) = args.mode {
settings.mode = mode;
}
// Create permission manager from settings
let perms = settings.create_permission_manager();
if let Some(cmd) = args.cmd {
match cmd {
Cmd::Read { path } => {
// Check permission
match perms.check(Tool::Read, None) {
PermissionDecision::Allow => {
let s = tools_fs::read_file(&path)?;
println!("{}", s);
return Ok(());
}
PermissionDecision::Ask => {
return Err(eyre!(
"Permission denied: Read operation requires approval. Use --mode code to allow."
));
}
PermissionDecision::Deny => {
return Err(eyre!("Permission denied: Read operation is blocked."));
}
}
}
Cmd::Glob { pattern } => {
// Check permission
match perms.check(Tool::Glob, None) {
PermissionDecision::Allow => {
for p in tools_fs::glob_list(&pattern)? {
println!("{}", p);
}
return Ok(());
}
PermissionDecision::Ask => {
return Err(eyre!(
"Permission denied: Glob operation requires approval. Use --mode code to allow."
));
}
PermissionDecision::Deny => {
return Err(eyre!("Permission denied: Glob operation is blocked."));
}
}
}
Cmd::Grep { root, pattern } => {
// Check permission
match perms.check(Tool::Grep, None) {
PermissionDecision::Allow => {
for (path, line_number, text) in tools_fs::grep(&root, &pattern)? {
println!("{path}:{line_number}:{text}")
}
return Ok(());
}
PermissionDecision::Ask => {
return Err(eyre!(
"Permission denied: Grep operation requires approval. Use --mode code to allow."
));
}
PermissionDecision::Deny => {
return Err(eyre!("Permission denied: Grep operation is blocked."));
}
}
}
}
}

View File

@@ -0,0 +1,56 @@
use assert_cmd::Command;
use std::fs;
use tempfile::tempdir;
#[test]
fn plan_mode_allows_read_operations() {
// Create a temp file to read
let dir = tempdir().unwrap();
let file = dir.path().join("test.txt");
fs::write(&file, "hello world").unwrap();
// Read operation should work in plan mode (default)
let mut cmd = Command::new(assert_cmd::cargo::cargo_bin!("owlen"));
cmd.arg("read").arg(file.to_str().unwrap());
cmd.assert().success().stdout("hello world\n");
}
#[test]
fn plan_mode_allows_glob_operations() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("a.txt"), "test").unwrap();
fs::write(dir.path().join("b.txt"), "test").unwrap();
let pattern = format!("{}/*.txt", dir.path().display());
// Glob operation should work in plan mode (default)
let mut cmd = Command::new(assert_cmd::cargo::cargo_bin!("owlen"));
cmd.arg("glob").arg(&pattern);
cmd.assert().success();
}
#[test]
fn plan_mode_allows_grep_operations() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("test.txt"), "hello world\nfoo bar").unwrap();
// Grep operation should work in plan mode (default)
let mut cmd = Command::new(assert_cmd::cargo::cargo_bin!("owlen"));
cmd.arg("grep").arg(dir.path().to_str().unwrap()).arg("hello");
cmd.assert().success();
}
#[test]
fn mode_override_via_cli_flag() {
let dir = tempdir().unwrap();
let file = dir.path().join("test.txt");
fs::write(&file, "content").unwrap();
// Test with --mode code (should also allow read)
let mut cmd = Command::new(assert_cmd::cargo::cargo_bin!("owlen"));
cmd.arg("--mode")
.arg("code")
.arg("read")
.arg(file.to_str().unwrap());
cmd.assert().success().stdout("content\n");
}

View File

@@ -9,4 +9,7 @@ rust-version.workspace = true
serde = { version = "1", features = ["derive"] }
directories = "5"
figment = { version = "0.10", features = ["toml", "env"] }
permissions = { path = "../permissions" }
[dev-dependencies]
tempfile = "3.23.0"

View File

@@ -5,6 +5,7 @@ use figment::{
};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use permissions::{Mode, PermissionManager};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
@@ -39,6 +40,19 @@ impl Default for Settings {
}
}
impl Settings {
/// Create a PermissionManager based on the configured mode
pub fn create_permission_manager(&self) -> PermissionManager {
let mode = Mode::from_str(&self.mode).unwrap_or(Mode::Plan);
PermissionManager::new(mode)
}
/// Get the Mode enum from the mode string
pub fn get_mode(&self) -> Mode {
Mode::from_str(&self.mode).unwrap_or(Mode::Plan)
}
}
pub fn load_settings(project_root: Option<&str>) -> Result<Settings, figment::Error> {
let mut fig = Figment::from(Serialized::defaults(Settings::default()));

View File

@@ -1,4 +1,5 @@
use config_agent::{load_settings, Settings};
use permissions::{Mode, PermissionDecision, Tool};
use std::{env, fs};
#[test]
@@ -17,3 +18,31 @@ fn default_mode_is_plan() {
let s = Settings::default();
assert_eq!(s.mode, "plan");
}
#[test]
fn settings_create_permission_manager_with_plan_mode() {
let s = Settings::default();
let mgr = s.create_permission_manager();
// Plan mode should allow read operations
assert_eq!(mgr.check(Tool::Read, None), PermissionDecision::Allow);
// Plan mode should ask for write operations
assert_eq!(mgr.check(Tool::Write, None), PermissionDecision::Ask);
}
#[test]
fn settings_parse_mode_from_config() {
let tmp = tempfile::tempdir().unwrap();
let project_file = tmp.path().join(".owlen.toml");
fs::write(&project_file, r#"mode="code""#).unwrap();
let s = load_settings(Some(tmp.path().to_str().unwrap())).unwrap();
assert_eq!(s.mode, "code");
assert_eq!(s.get_mode(), Mode::Code);
let mgr = s.create_permission_manager();
// Code mode should allow everything
assert_eq!(mgr.check(Tool::Write, None), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Bash, None), PermissionDecision::Allow);
}

View File

@@ -0,0 +1,10 @@
[package]
name = "permissions"
version = "0.1.0"
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[dependencies]
serde = { version = "1", features = ["derive"] }
thiserror = "1"

View File

@@ -0,0 +1,212 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Tool {
Read,
Write,
Edit,
Bash,
Grep,
Glob,
WebFetch,
WebSearch,
NotebookRead,
NotebookEdit,
SlashCommand,
Task,
TodoWrite,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Action {
Allow,
Ask,
Deny,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Mode {
Plan, // Read-only: Read/Grep/Glob allowed, others Ask
AcceptEdits, // Auto-allow Edit/Write, Bash still Ask
Code, // Full access (all allowed)
}
impl Mode {
pub fn from_str(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"plan" => Some(Mode::Plan),
"acceptedits" | "accept_edits" => Some(Mode::AcceptEdits),
"code" => Some(Mode::Code),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PermissionDecision {
Allow,
Ask,
Deny,
}
#[derive(Debug, Clone)]
pub struct PermissionRule {
pub tool: Tool,
pub pattern: Option<String>,
pub action: Action,
}
impl PermissionRule {
fn matches(&self, tool: Tool, context: Option<&str>) -> bool {
if self.tool != tool {
return false;
}
match (&self.pattern, context) {
(None, _) => true, // No pattern means match all
(Some(_), None) => false, // Pattern specified but no context
(Some(pattern), Some(ctx)) => {
// Support prefix matching with wildcard
if pattern.ends_with('*') {
let prefix = pattern.trim_end_matches('*');
ctx.starts_with(prefix)
} else {
// Exact match
pattern == ctx
}
}
}
}
}
#[derive(Debug)]
pub struct PermissionManager {
mode: Mode,
rules: Vec<PermissionRule>,
}
impl PermissionManager {
pub fn new(mode: Mode) -> Self {
Self {
mode,
rules: Vec::new(),
}
}
pub fn add_rule(&mut self, tool: Tool, pattern: Option<String>, action: Action) {
self.rules.push(PermissionRule {
tool,
pattern,
action,
});
}
pub fn check(&self, tool: Tool, context: Option<&str>) -> PermissionDecision {
// Check explicit rules first (most specific to least specific)
// Deny rules take precedence
for rule in &self.rules {
if rule.matches(tool, context) {
return match rule.action {
Action::Allow => PermissionDecision::Allow,
Action::Ask => PermissionDecision::Ask,
Action::Deny => PermissionDecision::Deny,
};
}
}
// Fall back to mode-based defaults
self.check_mode_default(tool)
}
fn check_mode_default(&self, tool: Tool) -> PermissionDecision {
match self.mode {
Mode::Plan => match tool {
// Read-only tools are allowed in plan mode
Tool::Read | Tool::Grep | Tool::Glob | Tool::NotebookRead => {
PermissionDecision::Allow
}
// Everything else requires asking
_ => PermissionDecision::Ask,
},
Mode::AcceptEdits => match tool {
// Read operations allowed
Tool::Read | Tool::Grep | Tool::Glob | Tool::NotebookRead => {
PermissionDecision::Allow
}
// Edit/Write operations allowed
Tool::Edit | Tool::Write | Tool::NotebookEdit => PermissionDecision::Allow,
// Bash and other dangerous operations still require asking
Tool::Bash | Tool::WebFetch | Tool::WebSearch => PermissionDecision::Ask,
// Utility tools allowed
Tool::TodoWrite | Tool::SlashCommand | Tool::Task => PermissionDecision::Allow,
},
Mode::Code => {
// Everything allowed in code mode
PermissionDecision::Allow
}
}
}
pub fn set_mode(&mut self, mode: Mode) {
self.mode = mode;
}
pub fn mode(&self) -> Mode {
self.mode
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pattern_exact_match() {
let rule = PermissionRule {
tool: Tool::Bash,
pattern: Some("npm test".to_string()),
action: Action::Allow,
};
assert!(rule.matches(Tool::Bash, Some("npm test")));
assert!(!rule.matches(Tool::Bash, Some("npm install")));
assert!(!rule.matches(Tool::Read, Some("npm test")));
}
#[test]
fn pattern_prefix_match() {
let rule = PermissionRule {
tool: Tool::Bash,
pattern: Some("npm test:*".to_string()),
action: Action::Allow,
};
assert!(rule.matches(Tool::Bash, Some("npm test:unit")));
assert!(rule.matches(Tool::Bash, Some("npm test:integration")));
assert!(!rule.matches(Tool::Bash, Some("npm install")));
}
#[test]
fn pattern_no_context() {
let rule = PermissionRule {
tool: Tool::Bash,
pattern: Some("npm test".to_string()),
action: Action::Allow,
};
// Pattern specified but no context provided
assert!(!rule.matches(Tool::Bash, None));
}
#[test]
fn no_pattern_matches_all() {
let rule = PermissionRule {
tool: Tool::Read,
pattern: None,
action: Action::Allow,
};
assert!(rule.matches(Tool::Read, Some("any context")));
assert!(rule.matches(Tool::Read, None));
}
}

View File

@@ -0,0 +1,85 @@
use permissions::{PermissionManager, Mode, Tool, PermissionDecision};
#[test]
fn plan_mode_blocks_write_bash_by_default() {
let mgr = PermissionManager::new(Mode::Plan);
// Plan mode should allow read operations
assert_eq!(mgr.check(Tool::Read, None), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Grep, None), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Glob, None), PermissionDecision::Allow);
// Plan mode should ask for write operations
assert_eq!(mgr.check(Tool::Write, None), PermissionDecision::Ask);
assert_eq!(mgr.check(Tool::Edit, None), PermissionDecision::Ask);
// Plan mode should ask for Bash
assert_eq!(mgr.check(Tool::Bash, None), PermissionDecision::Ask);
}
#[test]
fn accept_edits_mode_allows_edit_write() {
let mgr = PermissionManager::new(Mode::AcceptEdits);
// AcceptEdits mode should allow read operations
assert_eq!(mgr.check(Tool::Read, None), PermissionDecision::Allow);
// AcceptEdits mode should allow edit/write
assert_eq!(mgr.check(Tool::Edit, None), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Write, None), PermissionDecision::Allow);
// But still ask for Bash
assert_eq!(mgr.check(Tool::Bash, None), PermissionDecision::Ask);
}
#[test]
fn code_mode_allows_everything() {
let mgr = PermissionManager::new(Mode::Code);
assert_eq!(mgr.check(Tool::Read, None), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Write, None), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Edit, None), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Bash, None), PermissionDecision::Allow);
}
#[test]
fn bash_pattern_matching() {
let mut mgr = PermissionManager::new(Mode::Plan);
// Add a rule to allow "npm test"
mgr.add_rule(Tool::Bash, Some("npm test".to_string()), permissions::Action::Allow);
// Should allow the exact command
assert_eq!(mgr.check(Tool::Bash, Some("npm test")), PermissionDecision::Allow);
// Should still ask for other commands
assert_eq!(mgr.check(Tool::Bash, Some("rm -rf /")), PermissionDecision::Ask);
}
#[test]
fn bash_prefix_matching() {
let mut mgr = PermissionManager::new(Mode::Plan);
// Add a rule to allow "npm test:*" (prefix match)
mgr.add_rule(Tool::Bash, Some("npm test:*".to_string()), permissions::Action::Allow);
// Should allow commands matching the prefix
assert_eq!(mgr.check(Tool::Bash, Some("npm test:unit")), PermissionDecision::Allow);
assert_eq!(mgr.check(Tool::Bash, Some("npm test:integration")), PermissionDecision::Allow);
// Should not allow non-matching commands
assert_eq!(mgr.check(Tool::Bash, Some("npm install")), PermissionDecision::Ask);
}
#[test]
fn deny_rules_take_precedence() {
let mut mgr = PermissionManager::new(Mode::Code);
// Even in Code mode, we can deny specific operations
mgr.add_rule(Tool::Bash, Some("rm -rf*".to_string()), permissions::Action::Deny);
assert_eq!(mgr.check(Tool::Bash, Some("rm -rf /")), PermissionDecision::Deny);
// But other commands are still allowed
assert_eq!(mgr.check(Tool::Bash, Some("ls")), PermissionDecision::Allow);
}

View File

@@ -12,14 +12,18 @@ pub fn glob_list(pattern: &str) -> Result<Vec<String>> {
let glob = Glob::new(pattern)?.compile_matcher();
// Extract the literal prefix to determine the root directory
let root = pattern
.split("**")
.next()
.and_then(|s| {
let trimmed = s.trim_end_matches('/');
if trimmed.is_empty() { None } else { Some(trimmed) }
})
.unwrap_or(".");
// Find the position of the first glob metacharacter
let first_glob = pattern
.find(|c| matches!(c, '*' | '?' | '[' | '{'))
.unwrap_or(pattern.len());
// Find the last directory separator before the first glob metacharacter
let root = if first_glob > 0 {
let prefix = &pattern[..first_glob];
prefix.rfind('/').map(|pos| &prefix[..pos]).unwrap_or(".")
} else {
"."
};
let mut out = Vec::new();
for result in WalkBuilder::new(root)

View File

@@ -11,7 +11,8 @@ fn read_and_glob_respect_gitignore() {
fs::write(root.join("secret/secret.txt"), "token=123").unwrap();
fs::write(root.join(".gitignore"), "secret/\n").unwrap();
let files = glob_list(root.to_str().unwrap()).unwrap();
let pattern = format!("{}/**/*", root.display());
let files = glob_list(&pattern).unwrap();
assert!(files.iter().any(|p| p.ends_with("a.txt")));
assert!(!files.iter().any(|p| p.contains("secret.txt")));
assert_eq!(read_file(root.join("a.txt").to_str().unwrap()).unwrap(), "hello");