|
1 | 1 | use crate::agentic::tools::framework::{PermissionIntent, ToolPathResolution, ToolUseContext}; |
2 | 2 | use crate::agentic::tools::restrictions::{ |
3 | 3 | canonicalize_local_path_best_effort, is_local_path_within_root, |
| 4 | + is_remote_posix_path_within_root, |
4 | 5 | }; |
5 | 6 | use crate::infrastructure::get_path_manager_arc; |
6 | 7 | use crate::util::errors::{OpenBitFunError, OpenBitFunResult}; |
@@ -90,9 +91,28 @@ fn external_directory_resource( |
90 | 91 | context: &ToolUseContext, |
91 | 92 | resolved: &ToolPathResolution, |
92 | 93 | ) -> OpenBitFunResult<Option<String>> { |
93 | | - if resolved.uses_remote_workspace_backend() || resolved.is_runtime_artifact() { |
| 94 | + if resolved.is_runtime_artifact() { |
94 | 95 | return Ok(None); |
95 | 96 | } |
| 97 | + if resolved.uses_remote_workspace_backend() { |
| 98 | + let workspace = context.workspace.as_ref().ok_or_else(|| { |
| 99 | + OpenBitFunError::validation("A workspace is required for file permissions".to_string()) |
| 100 | + })?; |
| 101 | + if is_remote_posix_path_within_root(&resolved.resolved_path, &workspace.root_path_string()) |
| 102 | + { |
| 103 | + return Ok(None); |
| 104 | + } |
| 105 | + // Remote paths are POSIX even when the controller runs Windows. Do not |
| 106 | + // inspect the controller filesystem to classify a target-side path. |
| 107 | + let directory = resolved |
| 108 | + .resolved_path |
| 109 | + .rsplit_once('/') |
| 110 | + .map(|(parent, _)| if parent.is_empty() { "/" } else { parent }) |
| 111 | + .ok_or_else(|| { |
| 112 | + OpenBitFunError::validation("Remote path has no parent directory".to_string()) |
| 113 | + })?; |
| 114 | + return Ok(Some(directory.to_string())); |
| 115 | + } |
96 | 116 |
|
97 | 117 | if is_openbitfun_managed_local_path(context, resolved)? { |
98 | 118 | return Ok(None); |
@@ -198,6 +218,47 @@ mod tests { |
198 | 218 | use serde_json::{json, Value}; |
199 | 219 | use std::fs; |
200 | 220 |
|
| 221 | + #[test] |
| 222 | + fn permission_defaults_remote_edits_keep_posix_external_directory_approval() { |
| 223 | + use crate::agentic::workspace::WorkspaceBackend; |
| 224 | + use openbitfun_agent_runtime::permission::{plan_permission_intents, PermissionIntentPlan}; |
| 225 | + use openbitfun_runtime_ports::{ |
| 226 | + PermissionPolicyPreset, PermissionResourceCaseSensitivity, ResolvedPermissionPolicy, |
| 227 | + }; |
| 228 | + let mut workspace = WorkspaceBinding::new(None, PathBuf::from("/remote/workspace")); |
| 229 | + workspace.backend = WorkspaceBackend::Remote { |
| 230 | + connection_id: "ssh-test".into(), |
| 231 | + connection_name: "Test SSH".into(), |
| 232 | + }; |
| 233 | + let context = ToolUseContext::for_tool_listing(Some(workspace), None); |
| 234 | + let policy = |
| 235 | + ResolvedPermissionPolicy::new(PermissionPolicyPreset::Ask.baseline_rules(), Vec::new()); |
| 236 | + for (path, allowed) in [ |
| 237 | + ("src/main.rs", true), |
| 238 | + (".env", false), |
| 239 | + ("/remote/outside/file.rs", false), |
| 240 | + ("../outside/file.rs", false), |
| 241 | + ] { |
| 242 | + let intents = file_permission_intents("edit", [path], &context).unwrap(); |
| 243 | + assert!(intents[0].resources[0].starts_with('/')); |
| 244 | + if path.contains("outside") { |
| 245 | + assert_eq!(intents[1].action, "external_directory"); |
| 246 | + assert_eq!(intents[1].resources, ["/remote/outside"]); |
| 247 | + } |
| 248 | + let plan = plan_permission_intents( |
| 249 | + intents, |
| 250 | + &policy, |
| 251 | + &[], |
| 252 | + PermissionResourceCaseSensitivity::Sensitive, |
| 253 | + ); |
| 254 | + assert_eq!( |
| 255 | + matches!(plan, PermissionIntentPlan::Allowed), |
| 256 | + allowed, |
| 257 | + "{path}: {plan:?}" |
| 258 | + ); |
| 259 | + } |
| 260 | + } |
| 261 | + |
201 | 262 | #[test] |
202 | 263 | fn local_external_file_adds_external_directory_intent() { |
203 | 264 | let temp = tempfile::tempdir().expect("temp dir"); |
@@ -423,8 +484,9 @@ mod tests { |
423 | 484 | let delete = DeleteFileTool::new() |
424 | 485 | .permission_intents(&json!({ "path": plan_path.as_ref() }), &context) |
425 | 486 | .expect("plan delete permission intents"); |
426 | | - assert_eq!(delete.len(), 1); |
| 487 | + assert_eq!(delete.len(), 2); |
427 | 488 | assert_eq!(delete[0].action, "edit"); |
| 489 | + assert_eq!(delete[1].action, "delete"); |
428 | 490 |
|
429 | 491 | let transcript_path = transcript.to_string_lossy(); |
430 | 492 | let transcript_edit = FileEditTool::new() |
@@ -558,7 +620,12 @@ mod tests { |
558 | 620 | let intents = tool |
559 | 621 | .permission_intents(&input, &context) |
560 | 622 | .expect("file tool permission intent"); |
561 | | - assert_eq!(intents.len(), 1, "{}", tool.name()); |
| 623 | + assert_eq!( |
| 624 | + intents.len(), |
| 625 | + if tool.name() == "Delete" { 2 } else { 1 }, |
| 626 | + "{}", |
| 627 | + tool.name() |
| 628 | + ); |
562 | 629 | assert_eq!(intents[0].action, expected_action, "{}", tool.name()); |
563 | 630 | assert_eq!(intents[0].resources.len(), 1, "{}", tool.name()); |
564 | 631 | } |
|
0 commit comments