| 1 | use std::path::Path; |
| 2 | |
| 3 | use codewhale_execpolicy::ToolAskRule; |
| 4 | use codewhale_execpolicy::canonical_action::canonical_action_alias; |
| 5 | use codewhale_execpolicy::command_safety::{SafetyLevel, analyze_command}; |
| 6 | use serde_json::Value; |
| 7 | |
| 8 | use super::auto_review::{ToolActionKind, credential_upload_texts}; |
| 9 | use super::{RiskLevel, ToolCategory, classify_risk, get_tool_category_for_call}; |
| 10 | |
| 11 | /// Presentation-level stakes for an approval prompt. |
| 12 | /// |
| 13 | /// `RiskLevel` stays conservative ("not provably read-only" is |
| 14 | /// `Destructive`); stakes split it three ways for presentation and for |
| 15 | /// deciding whether a standing allow may be offered: |
| 16 | /// |
| 17 | /// - `Routine` - provably read-only. |
| 18 | /// - `Elevated` - ordinary state-touching work (edits, builds, MCP actions). |
| 19 | /// - `Critical` - destructive, publish-like, or secret-touching per |
| 20 | /// `ToolActionKind`; never eligible for a standing allow. |
| 21 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 22 | pub enum ApprovalStakes { |
| 23 | Routine, |
| 24 | Elevated, |
| 25 | Critical, |
| 26 | } |
| 27 | |
| 28 | #[must_use] |
| 29 | pub fn classify_stakes( |
| 30 | tool_name: &str, |
| 31 | category: ToolCategory, |
| 32 | risk: RiskLevel, |
| 33 | params: &Value, |
| 34 | ) -> ApprovalStakes { |
| 35 | if !credential_upload_texts(tool_name, params).is_empty() { |
| 36 | return ApprovalStakes::Critical; |
| 37 | } |
| 38 | if matches!(risk, RiskLevel::Benign) { |
| 39 | return ApprovalStakes::Routine; |
| 40 | } |
| 41 | let semantic_name = canonical_action_alias(tool_name, params); |
| 42 | match ToolActionKind::from_tool_call(semantic_name, params, category, None) { |
| 43 | ToolActionKind::Publish | ToolActionKind::Destructive => ApprovalStakes::Critical, |
| 44 | _ => ApprovalStakes::Elevated, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[must_use] |
| 49 | pub fn is_repo_law_prompt(description: &str) -> bool { |
| 50 | description.starts_with("Repo law holds this write:") |
| 51 | && description.contains(".codewhale/constitution.json") |
| 52 | } |
| 53 | |
| 54 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 55 | pub struct PersistentRuleCandidates { |
| 56 | pub ask: Vec<ToolAskRule>, |
| 57 | pub allow: Vec<ToolAskRule>, |
| 58 | } |
| 59 | |
| 60 | #[must_use] |
| 61 | pub fn persistent_rule_candidates( |
| 62 | tool_name: &str, |
| 63 | params: &Value, |
| 64 | description: &str, |
| 65 | workspace: &Path, |
| 66 | ) -> PersistentRuleCandidates { |
| 67 | let semantic = canonical_action_alias(tool_name, params); |
| 68 | let ask = exact_ask_rules(semantic, params, workspace); |
| 69 | let category = get_tool_category_for_call(tool_name, params); |
| 70 | let risk = classify_risk(tool_name, category, params); |
| 71 | let allow = if classify_stakes(tool_name, category, risk, params) == ApprovalStakes::Critical |
| 72 | || is_repo_law_prompt(description) |
| 73 | { |
| 74 | Vec::new() |
| 75 | } else { |
| 76 | exact_workspace_allow_rules(semantic, params, workspace, &ask) |
| 77 | }; |
| 78 | PersistentRuleCandidates { ask, allow } |
| 79 | } |
| 80 | |
| 81 | fn exact_ask_rules(semantic: &str, params: &Value, workspace: &Path) -> Vec<ToolAskRule> { |
| 82 | match semantic { |
| 83 | "exec_shell" => exec_shell_ask_rules(params), |
| 84 | "write_file" | "edit_file" => file_write_ask_rules(semantic, params, workspace), |
| 85 | "apply_patch" => apply_patch_ask_rules(params, workspace), |
| 86 | _ => Vec::new(), |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn exact_workspace_allow_rules( |
| 91 | semantic: &str, |
| 92 | params: &Value, |
| 93 | workspace: &Path, |
| 94 | exact_rules: &[ToolAskRule], |
| 95 | ) -> Vec<ToolAskRule> { |
| 96 | if exact_rules.is_empty() { |
| 97 | return Vec::new(); |
| 98 | } |
| 99 | if semantic == "exec_shell" { |
| 100 | let Some(command) = params.get("command").and_then(Value::as_str) else { |
| 101 | return Vec::new(); |
| 102 | }; |
| 103 | if !matches!( |
| 104 | analyze_command(command).level, |
| 105 | SafetyLevel::Safe | SafetyLevel::WorkspaceSafe |
| 106 | ) { |
| 107 | return Vec::new(); |
| 108 | } |
| 109 | } |
| 110 | let workspace = workspace.to_string_lossy(); |
| 111 | let Some(workspace) = codewhale_execpolicy::normalize_workspace_scope(workspace.as_ref()) |
| 112 | else { |
| 113 | return Vec::new(); |
| 114 | }; |
| 115 | exact_rules |
| 116 | .iter() |
| 117 | .cloned() |
| 118 | .map(|rule| rule.into_exact_workspace_allow(workspace.clone())) |
| 119 | .collect() |
| 120 | } |
| 121 | |
| 122 | fn exec_shell_ask_rules(params: &Value) -> Vec<ToolAskRule> { |
| 123 | let Some(command) = params |
| 124 | .get("command") |
| 125 | .and_then(Value::as_str) |
| 126 | .map(str::trim) |
| 127 | .filter(|command| !command.is_empty()) |
| 128 | else { |
| 129 | return Vec::new(); |
| 130 | }; |
| 131 | vec![ToolAskRule::exec_shell(command)] |
| 132 | } |
| 133 | |
| 134 | fn file_write_ask_rules(tool_name: &str, params: &Value, workspace: &Path) -> Vec<ToolAskRule> { |
| 135 | let Some(path) = params |
| 136 | .get("path") |
| 137 | .and_then(Value::as_str) |
| 138 | .map(str::trim) |
| 139 | .filter(|path| !path.is_empty()) |
| 140 | else { |
| 141 | return Vec::new(); |
| 142 | }; |
| 143 | let workspace = workspace.to_string_lossy(); |
| 144 | let Some(relative) = |
| 145 | codewhale_execpolicy::normalize_workspace_relative_path(path, workspace.as_ref()) |
| 146 | .filter(|relative| !relative.is_empty()) |
| 147 | else { |
| 148 | return Vec::new(); |
| 149 | }; |
| 150 | vec![ToolAskRule::file_path(tool_name, relative)] |
| 151 | } |
| 152 | |
| 153 | fn apply_patch_ask_rules(params: &Value, workspace: &Path) -> Vec<ToolAskRule> { |
| 154 | let Ok(preflight) = codewhale_tools::patch_preflight::preflight_apply_patch(params) else { |
| 155 | return Vec::new(); |
| 156 | }; |
| 157 | let workspace = workspace.to_string_lossy(); |
| 158 | let mut rules = Vec::new(); |
| 159 | for path in preflight.touched_files { |
| 160 | let Some(relative) = |
| 161 | codewhale_execpolicy::normalize_workspace_relative_path(&path, workspace.as_ref()) |
| 162 | .filter(|relative| !relative.is_empty()) |
| 163 | else { |
| 164 | return Vec::new(); |
| 165 | }; |
| 166 | let rule = ToolAskRule::file_path("apply_patch", relative); |
| 167 | if !rules.contains(&rule) { |
| 168 | rules.push(rule); |
| 169 | } |
| 170 | } |
| 171 | rules |
| 172 | } |
| 173 |