1mod approval_rules;
4mod effects;
5mod nested_budget;
6mod types;
7
8use crate::value::VmDictExt;
9use std::cell::RefCell;
10use std::collections::BTreeMap;
11use std::thread_local;
12
13use serde::{Deserialize, Serialize};
14
15use crate::runtime_limits::RuntimeLimits;
16use crate::tool_annotations::{SideEffectLevel, ToolAnnotations};
17use crate::value::{VmError, VmValue};
18use crate::workspace_path::{classify_workspace_path, WorkspacePathInfo};
19
20pub use crate::tool_annotations::{ToolArgSchema, ToolKind};
21pub use approval_rules::{
22 clear_all_approval_policy_repeat_counts, clear_approval_policy_repeat_counts,
23 next_approval_policy_repeat_count, ApprovalShape, PolicyAction, PolicyEvaluation,
24 PolicyMatchedRule, PolicyRule, PolicyRuleMatch,
25};
26pub use effects::{
27 compute_handoff_effects, effect_kind_label, effect_record_summary, effect_subset_violations,
28 effects_from_metadata, EffectKind, EffectRecord, EffectScope,
29};
30pub use nested_budget::{
31 annotate_nested_execution_options, enter_nested_execution_policy, NestedExecutionGuard,
32 NestedExecutionKind, NESTED_KIND_OPTION_KEY, NESTED_LABEL_OPTION_KEY,
33};
34pub use types::{
35 enforce_tool_arg_constraints, AutoCompactPolicy, BranchSemantics, CapabilityPolicy,
36 ContextPolicy, EqIgnored, EscalationPolicy, JoinPolicy, MapPolicy, ModelPolicy,
37 NativeToolFallbackPolicy, ProcessSandboxPolicy, ProcessSandboxPreset, ReducePolicy,
38 RetryPolicy, SandboxProfile, StageContract, ToolArgConstraint, TurnPolicy,
39};
40
41thread_local! {
42 static EXECUTION_POLICY_STACK: RefCell<Vec<CapabilityPolicy>> = const { RefCell::new(Vec::new()) };
43 static EXECUTION_APPROVAL_POLICY_STACK: RefCell<Vec<ToolApprovalPolicy>> = const { RefCell::new(Vec::new()) };
44 static TRUSTED_BRIDGE_CALL_DEPTH: RefCell<usize> = const { RefCell::new(0) };
45}
46
47pub fn push_execution_policy(policy: CapabilityPolicy) {
48 EXECUTION_POLICY_STACK.with(|stack| stack.borrow_mut().push(policy));
49}
50
51pub fn pop_execution_policy() {
52 EXECUTION_POLICY_STACK.with(|stack| {
53 stack.borrow_mut().pop();
54 });
55}
56
57pub fn clear_execution_policy_stacks() {
58 EXECUTION_POLICY_STACK.with(|stack| stack.borrow_mut().clear());
59 EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow_mut().clear());
60 TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow_mut() = 0);
61}
62
63pub fn current_execution_policy() -> Option<CapabilityPolicy> {
64 EXECUTION_POLICY_STACK.with(|stack| stack.borrow().last().cloned())
65}
66
67pub fn push_approval_policy(policy: ToolApprovalPolicy) {
68 EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow_mut().push(policy));
69}
70
71pub fn pop_approval_policy() {
72 EXECUTION_APPROVAL_POLICY_STACK.with(|stack| {
73 stack.borrow_mut().pop();
74 });
75}
76
77pub fn current_approval_policy() -> Option<ToolApprovalPolicy> {
78 EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow().last().cloned())
79}
80
81pub fn current_tool_annotations(tool: &str) -> Option<ToolAnnotations> {
82 current_execution_policy().and_then(|policy| policy.tool_annotations.get(tool).cloned())
83}
84
85pub fn current_allowed_tool_names() -> Vec<String> {
93 let Some(policy) = current_execution_policy() else {
94 return Vec::new();
95 };
96 if !policy.tools.is_empty() {
97 return policy.tools;
98 }
99 policy.tool_annotations.keys().cloned().collect()
100}
101
102pub(super) fn tool_kind_participates_in_write_allowlist(tool_name: &str) -> bool {
103 current_tool_annotations(tool_name)
104 .map(|annotations| !annotations.kind.is_read_only())
105 .unwrap_or(true)
106}
107
108pub struct TrustedBridgeCallGuard;
109
110pub fn allow_trusted_bridge_calls() -> TrustedBridgeCallGuard {
111 TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| {
112 *depth.borrow_mut() += 1;
113 });
114 TrustedBridgeCallGuard
115}
116
117impl Drop for TrustedBridgeCallGuard {
118 fn drop(&mut self) {
119 TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| {
120 let mut depth = depth.borrow_mut();
121 *depth = depth.saturating_sub(1);
122 });
123 }
124}
125
126fn policy_allows_tool(policy: &CapabilityPolicy, tool: &str) -> bool {
127 policy.tools.is_empty() || policy.tools.iter().any(|allowed| allowed == tool)
128}
129
130fn policy_allows_capability(policy: &CapabilityPolicy, capability: &str, op: &str) -> bool {
131 policy.capabilities.is_empty()
132 || policy
133 .capabilities
134 .get(capability)
135 .is_some_and(|ops| ops.is_empty() || ops.iter().any(|allowed| allowed == op))
136}
137
138fn policy_allows_side_effect(policy: &CapabilityPolicy, requested: &str) -> bool {
139 fn rank(v: &str) -> usize {
140 match v {
141 "none" => 0,
142 "read_only" => 1,
143 "workspace_write" => 2,
144 "process_exec" => 3,
145 "network" => 4,
146 _ => 5,
147 }
148 }
149 policy
150 .side_effect_level
151 .as_ref()
152 .map(|allowed| rank(allowed) >= rank(requested))
153 .unwrap_or(true)
154}
155
156pub(super) fn reject_policy(reason: String) -> Result<(), VmError> {
157 Err(VmError::CategorizedError {
158 message: reason,
159 category: crate::value::ErrorCategory::ToolRejected,
160 })
161}
162
163#[derive(Clone, Debug, PartialEq, Eq)]
170pub struct PolicyDenial {
171 pub gate: crate::agent_events::DenialGate,
172 pub capability: Option<String>,
173 pub reason: String,
174}
175
176impl From<PolicyDenial> for VmError {
177 fn from(denial: PolicyDenial) -> Self {
178 VmError::CategorizedError {
179 message: denial.reason,
180 category: crate::value::ErrorCategory::ToolRejected,
181 }
182 }
183}
184
185pub(super) fn reject_tool(
186 gate: crate::agent_events::DenialGate,
187 capability: Option<String>,
188 reason: String,
189) -> Result<(), PolicyDenial> {
190 Err(PolicyDenial {
191 gate,
192 capability,
193 reason,
194 })
195}
196
197pub fn current_tool_mutation_classification(tool_name: &str) -> String {
202 current_tool_annotations(tool_name)
203 .map(|annotations| annotations.kind.mutation_class().to_string())
204 .unwrap_or_else(|| "other".to_string())
205}
206
207pub fn current_tool_declared_paths(tool_name: &str, args: &serde_json::Value) -> Vec<String> {
211 current_tool_declared_path_entries(tool_name, args)
212 .into_iter()
213 .map(|entry| entry.display_path().to_string())
214 .collect()
215}
216
217pub fn current_tool_declared_path_entries(
222 tool_name: &str,
223 args: &serde_json::Value,
224) -> Vec<WorkspacePathInfo> {
225 let Some(map) = args.as_object() else {
226 return Vec::new();
227 };
228 let Some(annotations) = current_tool_annotations(tool_name) else {
229 return Vec::new();
230 };
231 let workspace_root = crate::stdlib::process::execution_root_path();
232 let mut entries = Vec::new();
233 for key in &annotations.arg_schema.path_params {
234 if let Some(value) = map.get(key) {
235 match value {
236 serde_json::Value::String(path) if !path.is_empty() => {
237 entries.push(classify_workspace_path(path, Some(&workspace_root)));
238 }
239 serde_json::Value::Array(items) => {
240 for item in items.iter().filter_map(|item| item.as_str()) {
241 if !item.is_empty() {
242 entries.push(classify_workspace_path(item, Some(&workspace_root)));
243 }
244 }
245 }
246 _ => {}
247 }
248 }
249 }
250 entries.sort_by(|a, b| a.display_path().cmp(b.display_path()));
251 entries.dedup_by(|left, right| left.policy_candidates() == right.policy_candidates());
252 entries
253}
254
255pub fn enforce_current_policy_for_builtin(name: &str, args: &[VmValue]) -> Result<(), VmError> {
256 let Some(policy) = current_execution_policy() else {
257 return Ok(());
258 };
259 match name {
260 "find_text"
261 if !policy_allows_capability(&policy, "workspace", "read_text")
262 || !policy_allows_capability(&policy, "workspace", "list") =>
263 {
264 return reject_policy(
265 "builtin 'find_text' exceeds workspace.read_text/workspace.list ceiling"
266 .to_string(),
267 );
268 }
269 "read_file"
270 | "read_file_result"
271 | "read_file_bytes"
272 | "render"
273 | "render_prompt"
274 | "render_with_provenance"
275 | "read_lines"
276 if !policy_allows_capability(&policy, "workspace", "read_text") =>
277 {
278 return reject_policy(format!(
279 "builtin '{name}' exceeds workspace.read_text ceiling"
280 ));
281 }
282 "list_dir" | "walk_dir" | "glob"
283 if !policy_allows_capability(&policy, "workspace", "list") =>
284 {
285 return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
286 }
287 "file_exists" | "stat" if !policy_allows_capability(&policy, "workspace", "exists") => {
288 return reject_policy(format!("builtin '{name}' exceeds workspace.exists ceiling"));
289 }
290 "write_file" | "write_file_bytes" | "append_file" | "mkdir" | "copy_file" | "move_file"
291 if !policy_allows_capability(&policy, "workspace", "write_text")
292 || !policy_allows_side_effect(&policy, "workspace_write") =>
293 {
294 return reject_policy(format!("builtin '{name}' exceeds workspace write ceiling"));
295 }
296 "delete_file"
297 if !policy_allows_capability(&policy, "workspace", "delete")
298 || !policy_allows_side_effect(&policy, "workspace_write") =>
299 {
300 return reject_policy(
301 "builtin 'delete_file' exceeds workspace.delete ceiling".to_string(),
302 );
303 }
304 "apply_edit"
305 if !policy_allows_capability(&policy, "workspace", "apply_edit")
306 || !policy_allows_side_effect(&policy, "workspace_write") =>
307 {
308 return reject_policy(
309 "builtin 'apply_edit' exceeds workspace.apply_edit ceiling".to_string(),
310 );
311 }
312 "exec"
313 | "exec_at"
314 | "shell"
315 | "shell_at"
316 | "git.repo.discover"
317 | "git.worktree.create"
318 | "git.worktree.remove"
319 | "git.fetch"
320 | "git.rebase"
321 | "git.status"
322 | "git.conflicts"
323 | "git.push"
324 | "git.diff"
325 | "git.merge_base"
326 if !policy_allows_capability(&policy, "process", "exec")
327 || !policy_allows_side_effect(&policy, "process_exec") =>
328 {
329 return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
330 }
331 "http_get"
332 | "http_post"
333 | "http_put"
334 | "http_patch"
335 | "http_delete"
336 | "http_download"
337 | "http_request"
338 | "unix_socket_json_request"
339 | "__net_unix_socket_json_request"
340 if !policy_allows_side_effect(&policy, "network") =>
341 {
342 return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
343 }
344 "__files_upload"
345 if !policy_allows_capability(&policy, "workspace", "read_text")
346 || !policy_allows_side_effect(&policy, "network") =>
347 {
348 return reject_policy(
349 "builtin '__files_upload' exceeds workspace.read_text/network ceiling".to_string(),
350 );
351 }
352 "http_session_request"
353 | "http_stream_open"
354 | "http_stream_read"
355 | "http_stream_close"
356 | "http_stream_info"
357 | "sse_connect"
358 | "sse_receive"
359 | "websocket_accept"
360 | "websocket_connect"
361 | "websocket_route"
362 | "websocket_send"
363 | "websocket_receive"
364 | "websocket_server"
365 if !policy_allows_side_effect(&policy, "network") =>
366 {
367 return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
368 }
369 "llm_call" | "llm_call_safe" | "llm_completion" | "llm_stream" | "llm_stream_call"
370 | "llm_healthcheck" | "agent_loop"
371 if !policy_allows_capability(&policy, "llm", "call") =>
372 {
373 return reject_policy(format!("builtin '{name}' exceeds llm.call ceiling"));
374 }
375 "connector_call"
376 if !policy_allows_capability(&policy, "connector", "call")
377 || !policy_allows_side_effect(&policy, "network") =>
378 {
379 return reject_policy(
380 "builtin 'connector_call' exceeds connector.call/network ceiling".to_string(),
381 );
382 }
383 "secret_get" if !policy_allows_capability(&policy, "connector", "secret_get") => {
384 return reject_policy(
385 "builtin 'secret_get' exceeds connector.secret_get ceiling".to_string(),
386 );
387 }
388 "event_log_emit" if !policy_allows_capability(&policy, "connector", "event_log_emit") => {
389 return reject_policy(
390 "builtin 'event_log_emit' exceeds connector.event_log_emit ceiling".to_string(),
391 );
392 }
393 "metrics_inc" if !policy_allows_capability(&policy, "connector", "metrics_inc") => {
394 return reject_policy(
395 "builtin 'metrics_inc' exceeds connector.metrics_inc ceiling".to_string(),
396 );
397 }
398 "project_fingerprint"
399 | "project_context_profile_native"
400 | "project_scan_native"
401 | "project_scan_tree_native"
402 | "project_walk_tree_native"
403 | "project_catalog_native"
404 if !policy_allows_capability(&policy, "workspace", "list")
405 || !policy_allows_side_effect(&policy, "read_only") =>
406 {
407 return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
408 }
409 "__agent_state_init"
410 | "__agent_state_resume"
411 | "__agent_state_write"
412 | "__agent_state_read"
413 | "__agent_state_list"
414 | "__agent_state_delete"
415 | "__agent_state_handoff"
416 if !policy_allows_capability(&policy, "agent_state", "access") =>
417 {
418 return reject_policy(format!(
419 "builtin '{name}' exceeds agent_state.access ceiling"
420 ));
421 }
422 "vision_ocr"
423 if !policy_allows_capability(&policy, "vision", "ocr")
424 || !policy_allows_side_effect(&policy, "process_exec") =>
425 {
426 return reject_policy(format!(
427 "builtin '{name}' exceeds vision.ocr/process ceiling"
428 ));
429 }
430 "mcp_connect"
431 | "mcp_ensure_active"
432 | "mcp_call"
433 | "mcp_list_tools"
434 | "mcp_list_resources"
435 | "mcp_list_resource_templates"
436 | "mcp_read_resource"
437 | "mcp_list_prompts"
438 | "mcp_get_prompt"
439 | "mcp_server_info"
440 | "mcp_disconnect"
441 if !policy_allows_capability(&policy, "process", "exec")
442 || !policy_allows_side_effect(&policy, "process_exec") =>
443 {
444 return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
445 }
446 "host_call" => {
447 let name = args.first().map(|v| v.display()).unwrap_or_default();
448 let Some((capability, op)) = name.split_once('.') else {
449 return reject_policy(format!(
450 "host_call '{name}' must use capability.operation naming"
451 ));
452 };
453 if !policy_allows_capability(&policy, capability, op) {
454 return reject_policy(format!(
455 "host_call {capability}.{op} exceeds capability ceiling"
456 ));
457 }
458 let requested_side_effect = match (capability, op) {
459 ("workspace", "write_text" | "apply_edit" | "delete") => "workspace_write",
460 ("process", "exec") => "process_exec",
461 _ => "read_only",
462 };
463 if !policy_allows_side_effect(&policy, requested_side_effect) {
464 return reject_policy(format!(
465 "host_call {capability}.{op} exceeds side-effect ceiling"
466 ));
467 }
468 }
469 "host_tool_list" | "host_tool_call"
470 if !policy_allows_capability(&policy, "host", "tool_call") =>
471 {
472 return reject_policy(format!("builtin '{name}' exceeds host.tool_call ceiling"));
473 }
474 _ => {}
475 }
476 Ok(())
477}
478
479pub fn enforce_current_policy_for_bridge_builtin(name: &str) -> Result<(), VmError> {
480 let trusted = TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow() > 0);
481 if trusted {
482 return Ok(());
483 }
484 if current_execution_policy().is_some() {
485 return reject_policy(format!(
486 "bridged builtin '{name}' exceeds execution policy; declare an explicit capability/tool surface instead"
487 ));
488 }
489 Ok(())
490}
491
492pub fn enforce_current_policy_for_tool(tool_name: &str) -> Result<(), PolicyDenial> {
493 use crate::agent_events::DenialGate;
494 let Some(policy) = current_execution_policy() else {
495 return Ok(());
496 };
497 if !policy_allows_tool(&policy, tool_name) {
498 return reject_tool(
499 DenialGate::ToolCeiling,
500 None,
501 format!("tool '{tool_name}' exceeds tool ceiling"),
502 );
503 }
504 if let Some(annotations) = policy.tool_annotations.get(tool_name) {
505 for (capability, ops) in &annotations.capabilities {
506 for op in ops {
507 if !policy_allows_capability(&policy, capability, op) {
508 return reject_tool(
509 DenialGate::CapabilityCeiling,
510 Some(format!("{capability}.{op}")),
511 format!("tool '{tool_name}' exceeds capability ceiling: {capability}.{op}"),
512 );
513 }
514 }
515 }
516 let requested_level = annotations.side_effect_level;
517 if requested_level != SideEffectLevel::None
518 && !policy_allows_side_effect(&policy, requested_level.as_str())
519 {
520 return reject_tool(
521 DenialGate::SideEffectCeiling,
522 None,
523 format!(
524 "tool '{tool_name}' exceeds side-effect ceiling: {}",
525 requested_level.as_str()
526 ),
527 );
528 }
529 }
530 Ok(())
531}
532
533pub fn redact_transcript_visibility(
545 transcript: &VmValue,
546 visibility: Option<&str>,
547) -> Option<VmValue> {
548 let Some(visibility) = visibility else {
549 return Some(transcript.clone());
550 };
551 if visibility != "public" && visibility != "public_only" {
552 return Some(transcript.clone());
553 }
554 let dict = transcript.as_dict()?;
555 let public_messages = match dict.get("messages") {
556 Some(VmValue::List(list)) => list
557 .iter()
558 .filter_map(redact_public_message)
559 .collect::<Vec<_>>(),
560 _ => Vec::new(),
561 };
562 let public_events = match dict.get("events") {
563 Some(VmValue::List(list)) => list
564 .iter()
565 .filter(|event| {
566 event
567 .as_dict()
568 .and_then(|d| d.get("visibility"))
569 .map(|v| v.display())
570 .map(|value| value == "public")
571 .unwrap_or(true)
572 })
573 .cloned()
574 .collect::<Vec<_>>(),
575 _ => Vec::new(),
576 };
577 let mut redacted = dict.clone();
578 redacted.insert(
579 crate::value::intern_key("messages"),
580 VmValue::List(std::sync::Arc::new(public_messages)),
581 );
582 redacted.insert(
583 crate::value::intern_key("events"),
584 VmValue::List(std::sync::Arc::new(public_events)),
585 );
586 Some(VmValue::dict(redacted))
587}
588
589fn redact_public_message(message: &VmValue) -> Option<VmValue> {
590 let Some(dict) = message.as_dict() else {
591 return Some(message.clone());
592 };
593 if dict.get("role").map(|value| value.display()).as_deref() == Some("tool_result") {
594 return None;
595 }
596 if dict
597 .get("visibility")
598 .map(|value| value.display())
599 .is_some_and(|visibility| visibility != "public")
600 {
601 return None;
602 }
603
604 let mut redacted = dict.clone();
605 let mut saw_structured_blocks = false;
606 let mut public_text = Vec::new();
607 for key in ["content", "blocks"] {
608 if let Some(VmValue::List(blocks)) = dict.get(key) {
609 saw_structured_blocks = true;
610 let public_blocks = blocks
611 .iter()
612 .filter_map(redact_public_block)
613 .collect::<Vec<_>>();
614 if key == "blocks" || public_text.is_empty() {
615 public_text = text_fragments_from_blocks(&public_blocks);
616 }
617 redacted.insert(
618 crate::value::intern_key(key),
619 VmValue::List(std::sync::Arc::new(public_blocks)),
620 );
621 }
622 }
623 if saw_structured_blocks {
624 if public_text.is_empty() {
625 redacted.remove("text");
626 } else {
627 redacted.put_str("text", public_text.join("\n"));
628 }
629 }
630 Some(VmValue::dict(redacted))
631}
632
633fn redact_public_block(block: &VmValue) -> Option<VmValue> {
634 let Some(dict) = block.as_dict() else {
635 return Some(block.clone());
636 };
637 if dict
638 .get("visibility")
639 .map(|value| value.display())
640 .is_some_and(|visibility| visibility != "public")
641 {
642 return None;
643 }
644 Some(block.clone())
645}
646
647fn text_fragments_from_blocks(blocks: &[VmValue]) -> Vec<String> {
648 blocks
649 .iter()
650 .filter_map(|block| block.as_dict())
651 .filter_map(|dict| dict.get("text"))
652 .filter_map(|text| match text {
653 VmValue::String(value) if !value.is_empty() => Some(value.to_string()),
654 _ => None,
655 })
656 .collect()
657}
658
659pub fn builtin_ceiling() -> CapabilityPolicy {
660 CapabilityPolicy {
661 tools: Vec::new(),
665 capabilities: BTreeMap::new(),
666 workspace_roots: Vec::new(),
667 read_only_roots: Vec::new(),
668 side_effect_level: Some("network".to_string()),
669 recursion_limit: Some(RuntimeLimits::DEFAULT.max_nested_execution_depth),
670 tool_arg_constraints: Vec::new(),
671 tool_annotations: BTreeMap::new(),
672 sandbox_profile: SandboxProfile::Worktree,
673 process_sandbox: Default::default(),
674 }
675}
676
677#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
681#[serde(default)]
682pub struct ToolApprovalPolicy {
683 #[serde(default)]
686 pub rules: Vec<PolicyRule>,
687 #[serde(default)]
689 pub auto_approve: Vec<String>,
690 #[serde(default)]
692 pub auto_deny: Vec<String>,
693 #[serde(default)]
695 pub require_approval: Vec<String>,
696 #[serde(default)]
698 pub write_path_allowlist: Vec<String>,
699 #[serde(default)]
701 pub allow_sensitive_paths: bool,
702 #[serde(default)]
705 pub sensitive_path_patterns: Vec<String>,
706 #[serde(default)]
708 pub allow_external_paths: bool,
709 #[serde(default)]
711 pub external_roots: Vec<String>,
712 #[serde(default, alias = "repeated_call_limit")]
714 pub repeat_limit: Option<u64>,
715 #[serde(default, alias = "repeated_call_action")]
717 pub repeat_action: Option<PolicyAction>,
718}
719
720#[derive(Debug, Clone, PartialEq, Eq)]
722pub enum ToolApprovalDecision {
723 AutoApproved,
725 AutoDenied { reason: String },
727 RequiresHostApproval,
730}
731
732impl ToolApprovalPolicy {
733 pub fn evaluate_detailed(&self, tool_name: &str, args: &serde_json::Value) -> PolicyEvaluation {
734 approval_rules::evaluate_tool_approval_policy(self, tool_name, args, None)
735 }
736
737 pub fn evaluate_detailed_with_repeat(
738 &self,
739 tool_name: &str,
740 args: &serde_json::Value,
741 repeat_count: u64,
742 ) -> PolicyEvaluation {
743 approval_rules::evaluate_tool_approval_policy(self, tool_name, args, Some(repeat_count))
744 }
745
746 pub fn evaluate(&self, tool_name: &str, args: &serde_json::Value) -> ToolApprovalDecision {
749 let decision = self.evaluate_detailed(tool_name, args);
750 if decision.is_deny() {
751 return ToolApprovalDecision::AutoDenied {
752 reason: decision.reason,
753 };
754 }
755 if decision.is_ask() {
756 return ToolApprovalDecision::RequiresHostApproval;
757 }
758 ToolApprovalDecision::AutoApproved
759 }
760
761 pub fn intersect(&self, other: &ToolApprovalPolicy) -> ToolApprovalPolicy {
767 let auto_approve = if self.auto_approve.is_empty() {
768 other.auto_approve.clone()
769 } else if other.auto_approve.is_empty() {
770 self.auto_approve.clone()
771 } else {
772 self.auto_approve
773 .iter()
774 .filter(|p| other.auto_approve.contains(p))
775 .cloned()
776 .collect()
777 };
778 let mut auto_deny = self.auto_deny.clone();
779 auto_deny.extend(other.auto_deny.iter().cloned());
780 let mut require_approval = self.require_approval.clone();
781 require_approval.extend(other.require_approval.iter().cloned());
782 let write_path_allowlist = if self.write_path_allowlist.is_empty() {
783 other.write_path_allowlist.clone()
784 } else if other.write_path_allowlist.is_empty() {
785 self.write_path_allowlist.clone()
786 } else {
787 self.write_path_allowlist
788 .iter()
789 .filter(|p| other.write_path_allowlist.contains(p))
790 .cloned()
791 .collect()
792 };
793 let mut rules = self.rules.clone();
794 rules.extend(other.rules.iter().cloned());
795 let mut sensitive_path_patterns = self.sensitive_path_patterns.clone();
796 sensitive_path_patterns.extend(other.sensitive_path_patterns.iter().cloned());
797 sensitive_path_patterns.sort();
798 sensitive_path_patterns.dedup();
799 let external_roots = if self.external_roots.is_empty() {
800 other.external_roots.clone()
801 } else if other.external_roots.is_empty() {
802 self.external_roots.clone()
803 } else {
804 self.external_roots
805 .iter()
806 .filter(|root| other.external_roots.contains(root))
807 .cloned()
808 .collect()
809 };
810 ToolApprovalPolicy {
811 rules,
812 auto_approve,
813 auto_deny,
814 require_approval,
815 write_path_allowlist,
816 allow_sensitive_paths: self.allow_sensitive_paths && other.allow_sensitive_paths,
817 sensitive_path_patterns,
818 allow_external_paths: self.allow_external_paths && other.allow_external_paths,
819 external_roots,
820 repeat_limit: match (self.repeat_limit, other.repeat_limit) {
821 (Some(left), Some(right)) => Some(left.min(right)),
822 (Some(left), None) => Some(left),
823 (None, Some(right)) => Some(right),
824 (None, None) => None,
825 },
826 repeat_action: match (self.repeat_action, other.repeat_action) {
827 (Some(PolicyAction::Deny), _) | (_, Some(PolicyAction::Deny)) => {
828 Some(PolicyAction::Deny)
829 }
830 (Some(PolicyAction::Ask), _) | (_, Some(PolicyAction::Ask)) => {
831 Some(PolicyAction::Ask)
832 }
833 (Some(PolicyAction::Allow), Some(PolicyAction::Allow)) => Some(PolicyAction::Allow),
834 (Some(action), None) | (None, Some(action)) => Some(action),
835 (None, None) => None,
836 },
837 }
838 }
839}
840
841#[cfg(test)]
842mod approval_policy_tests {
843 use super::*;
844 use crate::orchestration::{pop_execution_policy, push_execution_policy, CapabilityPolicy};
845 use crate::tool_annotations::{ToolAnnotations, ToolArgSchema, ToolKind};
846
847 #[test]
848 fn auto_deny_takes_precedence_over_auto_approve() {
849 let policy = ToolApprovalPolicy {
850 auto_approve: vec!["*".to_string()],
851 auto_deny: vec!["dangerous_*".to_string()],
852 ..Default::default()
853 };
854 assert_eq!(
855 policy.evaluate("dangerous_rm", &serde_json::json!({})),
856 ToolApprovalDecision::AutoDenied {
857 reason: "tool 'dangerous_rm' matches deny pattern 'dangerous_*'".to_string()
858 }
859 );
860 }
861
862 #[test]
863 fn auto_approve_matches_glob() {
864 let policy = ToolApprovalPolicy {
865 auto_approve: vec!["read*".to_string(), "search*".to_string()],
866 ..Default::default()
867 };
868 assert_eq!(
869 policy.evaluate("read_file", &serde_json::json!({})),
870 ToolApprovalDecision::AutoApproved
871 );
872 assert_eq!(
873 policy.evaluate("search", &serde_json::json!({})),
874 ToolApprovalDecision::AutoApproved
875 );
876 }
877
878 #[test]
879 fn require_approval_emits_decision() {
880 let policy = ToolApprovalPolicy {
881 require_approval: vec!["edit*".to_string()],
882 ..Default::default()
883 };
884 let decision = policy.evaluate("edit_file", &serde_json::json!({"path": "foo.rs"}));
885 assert!(matches!(
886 decision,
887 ToolApprovalDecision::RequiresHostApproval
888 ));
889 }
890
891 #[test]
892 fn unmatched_tool_defaults_to_approved() {
893 let policy = ToolApprovalPolicy {
894 auto_approve: vec!["read*".to_string()],
895 require_approval: vec!["edit*".to_string()],
896 ..Default::default()
897 };
898 assert_eq!(
899 policy.evaluate("unknown_tool", &serde_json::json!({})),
900 ToolApprovalDecision::AutoApproved
901 );
902 }
903
904 #[test]
905 fn intersect_merges_deny_lists() {
906 let a = ToolApprovalPolicy {
907 auto_deny: vec!["rm*".to_string()],
908 ..Default::default()
909 };
910 let b = ToolApprovalPolicy {
911 auto_deny: vec!["drop*".to_string()],
912 ..Default::default()
913 };
914 let merged = a.intersect(&b);
915 assert_eq!(merged.auto_deny.len(), 2);
916 }
917
918 #[test]
919 fn intersect_restricts_auto_approve_to_common_patterns() {
920 let a = ToolApprovalPolicy {
921 auto_approve: vec!["read*".to_string(), "search*".to_string()],
922 ..Default::default()
923 };
924 let b = ToolApprovalPolicy {
925 auto_approve: vec!["read*".to_string(), "write*".to_string()],
926 ..Default::default()
927 };
928 let merged = a.intersect(&b);
929 assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
930 }
931
932 #[test]
933 fn intersect_defers_auto_approve_when_one_side_empty() {
934 let a = ToolApprovalPolicy {
935 auto_approve: vec!["read*".to_string()],
936 ..Default::default()
937 };
938 let b = ToolApprovalPolicy::default();
939 let merged = a.intersect(&b);
940 assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
941 }
942
943 #[test]
944 fn write_path_allowlist_matches_recovered_workspace_relative_path() {
945 let temp = tempfile::tempdir().unwrap();
946 std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
947 std::fs::write(temp.path().join("packages/demo/file.txt"), "ok").unwrap();
948 crate::stdlib::process::set_thread_execution_context(Some(
949 crate::orchestration::RunExecutionRecord {
950 cwd: Some(temp.path().to_string_lossy().into_owned()),
951 source_dir: Some(temp.path().to_string_lossy().into_owned()),
952 env: BTreeMap::new(),
953 adapter: None,
954 repo_path: None,
955 worktree_path: None,
956 branch: None,
957 base_ref: None,
958 cleanup: None,
959 },
960 ));
961
962 let mut tool_annotations = BTreeMap::new();
963 tool_annotations.insert(
964 "write_file".to_string(),
965 ToolAnnotations {
966 kind: ToolKind::Edit,
967 arg_schema: ToolArgSchema {
968 path_params: vec!["path".to_string()],
969 ..Default::default()
970 },
971 ..Default::default()
972 },
973 );
974 push_execution_policy(CapabilityPolicy {
975 tool_annotations,
976 ..Default::default()
977 });
978
979 let policy = ToolApprovalPolicy {
980 write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
981 ..Default::default()
982 };
983 let decision = policy.evaluate(
984 "write_file",
985 &serde_json::json!({"path": "/packages/demo/file.txt"}),
986 );
987 assert_eq!(decision, ToolApprovalDecision::AutoApproved);
988
989 pop_execution_policy();
990 crate::stdlib::process::set_thread_execution_context(None);
991 }
992
993 #[test]
994 fn write_path_allowlist_does_not_block_read_only_tools() {
995 let temp = tempfile::tempdir().unwrap();
996 std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
997 std::fs::write(temp.path().join("packages/demo/context.txt"), "ok").unwrap();
998 crate::stdlib::process::set_thread_execution_context(Some(
999 crate::orchestration::RunExecutionRecord {
1000 cwd: Some(temp.path().to_string_lossy().into_owned()),
1001 source_dir: Some(temp.path().to_string_lossy().into_owned()),
1002 env: BTreeMap::new(),
1003 adapter: None,
1004 repo_path: None,
1005 worktree_path: None,
1006 branch: None,
1007 base_ref: None,
1008 cleanup: None,
1009 },
1010 ));
1011
1012 let mut tool_annotations = BTreeMap::new();
1013 tool_annotations.insert(
1014 "read_file".to_string(),
1015 ToolAnnotations {
1016 kind: ToolKind::Read,
1017 arg_schema: ToolArgSchema {
1018 path_params: vec!["path".to_string()],
1019 ..Default::default()
1020 },
1021 ..Default::default()
1022 },
1023 );
1024 push_execution_policy(CapabilityPolicy {
1025 tool_annotations,
1026 ..Default::default()
1027 });
1028
1029 let policy = ToolApprovalPolicy {
1030 write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
1031 ..Default::default()
1032 };
1033 let decision = policy.evaluate(
1034 "read_file",
1035 &serde_json::json!({"path": "/packages/demo/context.txt"}),
1036 );
1037 assert_eq!(decision, ToolApprovalDecision::AutoApproved);
1038
1039 pop_execution_policy();
1040 crate::stdlib::process::set_thread_execution_context(None);
1041 }
1042
1043 #[test]
1044 fn builtin_policy_covers_fs_read_and_list_helpers() {
1045 clear_execution_policy_stacks();
1046 push_execution_policy(CapabilityPolicy {
1047 capabilities: BTreeMap::from([("workspace".to_string(), vec!["exists".to_string()])]),
1048 side_effect_level: Some("read_only".to_string()),
1049 ..CapabilityPolicy::default()
1050 });
1051
1052 for name in [
1053 "read_lines",
1054 "find_text",
1055 "walk_dir",
1056 "glob",
1057 "project_context_profile_native",
1058 ] {
1059 assert!(
1060 enforce_current_policy_for_builtin(name, &[]).is_err(),
1061 "{name} should be rejected when the matching workspace capability is absent"
1062 );
1063 }
1064
1065 pop_execution_policy();
1066 }
1067
1068 #[test]
1069 fn move_file_requires_workspace_write_side_effect() {
1070 clear_execution_policy_stacks();
1071 push_execution_policy(CapabilityPolicy {
1072 capabilities: BTreeMap::from([(
1073 "workspace".to_string(),
1074 vec!["write_text".to_string()],
1075 )]),
1076 side_effect_level: Some("read_only".to_string()),
1077 ..CapabilityPolicy::default()
1078 });
1079
1080 let error = enforce_current_policy_for_builtin("move_file", &[]).unwrap_err();
1081 assert!(
1082 error.to_string().contains("workspace write ceiling"),
1083 "unexpected error: {error}"
1084 );
1085
1086 pop_execution_policy();
1087 }
1088
1089 #[test]
1090 fn unix_socket_json_request_requires_network_side_effect() {
1091 clear_execution_policy_stacks();
1092 push_execution_policy(CapabilityPolicy {
1093 side_effect_level: Some("read_only".to_string()),
1094 ..CapabilityPolicy::default()
1095 });
1096
1097 let error =
1098 enforce_current_policy_for_builtin("__net_unix_socket_json_request", &[]).unwrap_err();
1099 assert!(
1100 error.to_string().contains("network ceiling"),
1101 "unexpected error: {error}"
1102 );
1103
1104 pop_execution_policy();
1105 }
1106
1107 #[test]
1108 fn files_upload_requires_workspace_read_and_network_side_effect() {
1109 clear_execution_policy_stacks();
1110 push_execution_policy(CapabilityPolicy {
1111 capabilities: BTreeMap::from([(
1112 "workspace".to_string(),
1113 vec!["read_text".to_string()],
1114 )]),
1115 side_effect_level: Some("read_only".to_string()),
1116 ..CapabilityPolicy::default()
1117 });
1118
1119 let network_error = enforce_current_policy_for_builtin("__files_upload", &[]).unwrap_err();
1120 assert!(
1121 network_error.to_string().contains("network ceiling"),
1122 "unexpected error: {network_error}"
1123 );
1124 pop_execution_policy();
1125
1126 push_execution_policy(CapabilityPolicy {
1127 capabilities: BTreeMap::from([("workspace".to_string(), vec!["exists".to_string()])]),
1128 side_effect_level: Some("network".to_string()),
1129 ..CapabilityPolicy::default()
1130 });
1131 let read_error = enforce_current_policy_for_builtin("__files_upload", &[]).unwrap_err();
1132 assert!(
1133 read_error.to_string().contains("workspace.read_text"),
1134 "unexpected error: {read_error}"
1135 );
1136
1137 pop_execution_policy();
1138 }
1139}
1140
1141#[cfg(test)]
1142mod turn_policy_tests {
1143 use super::TurnPolicy;
1144
1145 #[test]
1146 fn default_allows_done_sentinel() {
1147 let policy = TurnPolicy::default();
1148 assert!(policy.allow_done_sentinel);
1149 assert!(!policy.require_action_or_yield);
1150 assert!(policy.max_prose_chars.is_none());
1151 }
1152
1153 #[test]
1154 fn deserializing_partial_dict_preserves_done_sentinel_pathway() {
1155 let policy: TurnPolicy =
1160 serde_json::from_value(serde_json::json!({ "require_action_or_yield": true }))
1161 .expect("deserialize");
1162 assert!(policy.require_action_or_yield);
1163 assert!(policy.allow_done_sentinel);
1164 }
1165
1166 #[test]
1167 fn deserializing_explicit_false_disables_done_sentinel() {
1168 let policy: TurnPolicy = serde_json::from_value(serde_json::json!({
1169 "require_action_or_yield": true,
1170 "allow_done_sentinel": false,
1171 }))
1172 .expect("deserialize");
1173 assert!(policy.require_action_or_yield);
1174 assert!(!policy.allow_done_sentinel);
1175 }
1176}
1177
1178#[cfg(test)]
1179mod visibility_redaction_tests {
1180 use super::*;
1181 use crate::value::VmValue;
1182
1183 fn mock_transcript() -> VmValue {
1184 let messages = vec![
1185 serde_json::json!({"role": "user", "content": "hi"}),
1186 serde_json::json!({"role": "assistant", "content": "hello"}),
1187 serde_json::json!({"role": "tool_result", "content": "internal tool output"}),
1188 ];
1189 crate::llm::helpers::transcript_to_vm_with_events(
1190 Some("test-id".to_string()),
1191 None,
1192 None,
1193 &messages,
1194 Vec::new(),
1195 Vec::new(),
1196 Some("active"),
1197 )
1198 }
1199
1200 fn message_count(transcript: &VmValue) -> usize {
1201 transcript
1202 .as_dict()
1203 .and_then(|d| d.get("messages"))
1204 .and_then(|v| match v {
1205 VmValue::List(list) => Some(list.len()),
1206 _ => None,
1207 })
1208 .unwrap_or(0)
1209 }
1210
1211 #[test]
1212 fn visibility_none_returns_unchanged() {
1213 let t = mock_transcript();
1214 let result = redact_transcript_visibility(&t, None).unwrap();
1215 assert_eq!(message_count(&result), 3);
1216 }
1217
1218 #[test]
1219 fn visibility_public_drops_tool_results() {
1220 let t = mock_transcript();
1221 let result = redact_transcript_visibility(&t, Some("public")).unwrap();
1222 assert_eq!(message_count(&result), 2);
1223 }
1224
1225 #[test]
1226 fn visibility_public_drops_private_content_blocks() {
1227 let t = crate::schema::json_to_vm_value(&serde_json::json!({
1228 "messages": [
1229 {
1230 "role": "assistant",
1231 "visibility": "public",
1232 "text": "visible answer\nsecret chain",
1233 "content": [
1234 {"type": "output_text", "text": "visible answer", "visibility": "public"},
1235 {"type": "reasoning", "text": "secret chain", "visibility": "private"}
1236 ],
1237 "blocks": [
1238 {"type": "output_text", "text": "visible block", "visibility": "public"},
1239 {"type": "tool_call", "text": "internal args", "visibility": "internal"}
1240 ]
1241 }
1242 ],
1243 "events": []
1244 }));
1245
1246 let result = redact_transcript_visibility(&t, Some("public")).unwrap();
1247 let rendered = result.display();
1248 assert!(rendered.contains("visible answer"));
1249 assert!(rendered.contains("visible block"));
1250 assert!(!rendered.contains("secret chain"));
1251 assert!(!rendered.contains("internal args"));
1252 }
1253
1254 #[test]
1255 fn visibility_unknown_string_is_pass_through() {
1256 let t = mock_transcript();
1257 let result = redact_transcript_visibility(&t, Some("internal")).unwrap();
1258 assert_eq!(message_count(&result), 3);
1259 }
1260}