1mod types;
4
5use std::cell::RefCell;
6use std::collections::BTreeMap;
7use std::rc::Rc;
8use std::thread_local;
9
10use serde::{Deserialize, Serialize};
11
12use super::glob_match;
13use crate::event_log::{active_event_log, EventLog, LogEvent, Topic};
14use crate::tool_annotations::{SideEffectLevel, ToolAnnotations};
15use crate::triggers::dispatcher::current_dispatch_context;
16use crate::trust_graph::AutonomyTier;
17use crate::value::{VmError, VmValue};
18use crate::workspace_path::{classify_workspace_path, WorkspacePathInfo};
19
20pub use crate::tool_annotations::{ToolArgSchema, ToolKind};
21pub use types::{
22 enforce_tool_arg_constraints, AutoCompactPolicy, BranchSemantics, CapabilityPolicy,
23 ContextPolicy, EqIgnored, EscalationPolicy, JoinPolicy, MapPolicy, ModelPolicy,
24 NativeToolFallbackPolicy, ReducePolicy, RetryPolicy, StageContract, ToolArgConstraint,
25 TurnPolicy,
26};
27
28thread_local! {
29 static EXECUTION_POLICY_STACK: RefCell<Vec<CapabilityPolicy>> = const { RefCell::new(Vec::new()) };
30 static EXECUTION_APPROVAL_POLICY_STACK: RefCell<Vec<ToolApprovalPolicy>> = const { RefCell::new(Vec::new()) };
31 static TRUSTED_BRIDGE_CALL_DEPTH: RefCell<usize> = const { RefCell::new(0) };
32}
33
34pub fn push_execution_policy(policy: CapabilityPolicy) {
35 EXECUTION_POLICY_STACK.with(|stack| stack.borrow_mut().push(policy));
36}
37
38pub fn pop_execution_policy() {
39 EXECUTION_POLICY_STACK.with(|stack| {
40 stack.borrow_mut().pop();
41 });
42}
43
44pub fn current_execution_policy() -> Option<CapabilityPolicy> {
45 EXECUTION_POLICY_STACK.with(|stack| stack.borrow().last().cloned())
46}
47
48pub fn push_approval_policy(policy: ToolApprovalPolicy) {
49 EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow_mut().push(policy));
50}
51
52pub fn pop_approval_policy() {
53 EXECUTION_APPROVAL_POLICY_STACK.with(|stack| {
54 stack.borrow_mut().pop();
55 });
56}
57
58pub fn current_approval_policy() -> Option<ToolApprovalPolicy> {
59 EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow().last().cloned())
60}
61
62pub fn current_tool_annotations(tool: &str) -> Option<ToolAnnotations> {
63 current_execution_policy().and_then(|policy| policy.tool_annotations.get(tool).cloned())
64}
65
66fn tool_kind_participates_in_write_allowlist(tool_name: &str) -> bool {
67 current_tool_annotations(tool_name)
68 .map(|annotations| !annotations.kind.is_read_only())
69 .unwrap_or(true)
70}
71
72pub struct TrustedBridgeCallGuard;
73
74pub fn allow_trusted_bridge_calls() -> TrustedBridgeCallGuard {
75 TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| {
76 *depth.borrow_mut() += 1;
77 });
78 TrustedBridgeCallGuard
79}
80
81impl Drop for TrustedBridgeCallGuard {
82 fn drop(&mut self) {
83 TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| {
84 let mut depth = depth.borrow_mut();
85 *depth = depth.saturating_sub(1);
86 });
87 }
88}
89
90fn policy_allows_tool(policy: &CapabilityPolicy, tool: &str) -> bool {
91 policy.tools.is_empty() || policy.tools.iter().any(|allowed| allowed == tool)
92}
93
94fn policy_allows_capability(policy: &CapabilityPolicy, capability: &str, op: &str) -> bool {
95 policy.capabilities.is_empty()
96 || policy
97 .capabilities
98 .get(capability)
99 .is_some_and(|ops| ops.is_empty() || ops.iter().any(|allowed| allowed == op))
100}
101
102fn policy_allows_side_effect(policy: &CapabilityPolicy, requested: &str) -> bool {
103 fn rank(v: &str) -> usize {
104 match v {
105 "none" => 0,
106 "read_only" => 1,
107 "workspace_write" => 2,
108 "process_exec" => 3,
109 "network" => 4,
110 _ => 5,
111 }
112 }
113 policy
114 .side_effect_level
115 .as_ref()
116 .map(|allowed| rank(allowed) >= rank(requested))
117 .unwrap_or(true)
118}
119
120pub(super) fn reject_policy(reason: String) -> Result<(), VmError> {
121 Err(VmError::CategorizedError {
122 message: reason,
123 category: crate::value::ErrorCategory::ToolRejected,
124 })
125}
126
127pub fn current_tool_mutation_classification(tool_name: &str) -> String {
132 current_tool_annotations(tool_name)
133 .map(|annotations| annotations.kind.mutation_class().to_string())
134 .unwrap_or_else(|| "other".to_string())
135}
136
137pub fn current_tool_declared_paths(tool_name: &str, args: &serde_json::Value) -> Vec<String> {
141 current_tool_declared_path_entries(tool_name, args)
142 .into_iter()
143 .map(|entry| entry.display_path().to_string())
144 .collect()
145}
146
147pub fn current_tool_declared_path_entries(
152 tool_name: &str,
153 args: &serde_json::Value,
154) -> Vec<WorkspacePathInfo> {
155 let Some(map) = args.as_object() else {
156 return Vec::new();
157 };
158 let Some(annotations) = current_tool_annotations(tool_name) else {
159 return Vec::new();
160 };
161 let workspace_root = crate::stdlib::process::execution_root_path();
162 let mut entries = Vec::new();
163 for key in &annotations.arg_schema.path_params {
164 if let Some(value) = map.get(key) {
165 match value {
166 serde_json::Value::String(path) if !path.is_empty() => {
167 entries.push(classify_workspace_path(path, Some(&workspace_root)));
168 }
169 serde_json::Value::Array(items) => {
170 for item in items.iter().filter_map(|item| item.as_str()) {
171 if !item.is_empty() {
172 entries.push(classify_workspace_path(item, Some(&workspace_root)));
173 }
174 }
175 }
176 _ => {}
177 }
178 }
179 }
180 entries.sort_by(|a, b| a.display_path().cmp(b.display_path()));
181 entries.dedup_by(|left, right| left.policy_candidates() == right.policy_candidates());
182 entries
183}
184
185fn builtin_mutates_state(name: &str) -> bool {
186 matches!(
187 name,
188 "write_file"
189 | "append_file"
190 | "mkdir"
191 | "copy_file"
192 | "delete_file"
193 | "apply_edit"
194 | "exec"
195 | "exec_at"
196 | "shell"
197 | "shell_at"
198 | "host_call"
199 | "store_set"
200 | "store_delete"
201 | "store_save"
202 | "store_clear"
203 | "metadata_set"
204 | "metadata_save"
205 | "metadata_refresh_hashes"
206 | "invalidate_facts"
207 | "checkpoint"
208 | "checkpoint_delete"
209 | "checkpoint_clear"
210 | "__agent_state_write"
211 | "__agent_state_delete"
212 | "__agent_state_handoff"
213 | "mcp_release"
214 )
215}
216
217fn emit_autonomy_proposal_event(
218 tier: AutonomyTier,
219 builtin_name: &str,
220 args: &[VmValue],
221) -> Result<(), VmError> {
222 let Some(context) = current_dispatch_context() else {
223 return Ok(());
224 };
225 let Some(log) = active_event_log() else {
226 return Ok(());
227 };
228 let topic = Topic::new(crate::TRIGGER_OUTBOX_TOPIC)
229 .map_err(|error| VmError::Runtime(format!("autonomy proposal topic error: {error}")))?;
230 let mut headers = BTreeMap::new();
231 headers.insert(
232 "trace_id".to_string(),
233 context.trigger_event.trace_id.0.clone(),
234 );
235 headers.insert("agent".to_string(), context.agent_id.clone());
236 headers.insert("autonomy_tier".to_string(), tier.as_str().to_string());
237 let payload = serde_json::json!({
238 "agent": context.agent_id,
239 "action": context.action,
240 "builtin": builtin_name,
241 "args": args.iter().map(crate::llm::vm_value_to_json).collect::<Vec<_>>(),
242 "trace_id": context.trigger_event.trace_id.0,
243 "replay_of_event_id": context.replay_of_event_id,
244 "autonomy_tier": tier,
245 "proposal": true,
246 });
247 futures::executor::block_on(log.append(
248 &topic,
249 LogEvent::new("dispatch_proposed", payload).with_headers(headers),
250 ))
251 .map(|_| ())
252 .map_err(|error| VmError::Runtime(format!("failed to append autonomy proposal: {error}")))
253}
254
255fn enforce_dispatch_autonomy_for_builtin(name: &str, args: &[VmValue]) -> Result<(), VmError> {
256 let Some(context) = current_dispatch_context() else {
257 return Ok(());
258 };
259 if !builtin_mutates_state(name) {
260 return Ok(());
261 }
262 match context.autonomy_tier {
263 AutonomyTier::ActAuto => Ok(()),
264 AutonomyTier::ActWithApproval => reject_policy(format!(
265 "mutating builtin '{name}' is blocked for autonomy tier 'act_with_approval'; route the action through an approval-aware host/tool path"
266 )),
267 AutonomyTier::Shadow => {
268 emit_autonomy_proposal_event(AutonomyTier::Shadow, name, args)?;
269 reject_policy(format!(
270 "mutating builtin '{name}' is blocked for autonomy tier 'shadow'; proposal event emitted instead"
271 ))
272 }
273 AutonomyTier::Suggest => {
274 emit_autonomy_proposal_event(AutonomyTier::Suggest, name, args)?;
275 reject_policy(format!(
276 "mutating builtin '{name}' is blocked for autonomy tier 'suggest'; proposal event emitted and approval is required"
277 ))
278 }
279 }
280}
281
282pub fn enforce_current_policy_for_builtin(name: &str, args: &[VmValue]) -> Result<(), VmError> {
283 enforce_dispatch_autonomy_for_builtin(name, args)?;
284 let Some(policy) = current_execution_policy() else {
285 return Ok(());
286 };
287 match name {
288 "read_file" if !policy_allows_capability(&policy, "workspace", "read_text") => {
289 return reject_policy(format!(
290 "builtin '{name}' exceeds workspace.read_text ceiling"
291 ));
292 }
293 "list_dir" if !policy_allows_capability(&policy, "workspace", "list") => {
294 return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
295 }
296 "file_exists" | "stat" if !policy_allows_capability(&policy, "workspace", "exists") => {
297 return reject_policy(format!("builtin '{name}' exceeds workspace.exists ceiling"));
298 }
299 "write_file" | "append_file" | "mkdir" | "copy_file"
300 if !policy_allows_capability(&policy, "workspace", "write_text")
301 || !policy_allows_side_effect(&policy, "workspace_write") =>
302 {
303 return reject_policy(format!("builtin '{name}' exceeds workspace write ceiling"));
304 }
305 "delete_file"
306 if !policy_allows_capability(&policy, "workspace", "delete")
307 || !policy_allows_side_effect(&policy, "workspace_write") =>
308 {
309 return reject_policy(
310 "builtin 'delete_file' exceeds workspace.delete ceiling".to_string(),
311 );
312 }
313 "apply_edit"
314 if !policy_allows_capability(&policy, "workspace", "apply_edit")
315 || !policy_allows_side_effect(&policy, "workspace_write") =>
316 {
317 return reject_policy(
318 "builtin 'apply_edit' exceeds workspace.apply_edit ceiling".to_string(),
319 );
320 }
321 "exec" | "exec_at" | "shell" | "shell_at"
322 if !policy_allows_capability(&policy, "process", "exec")
323 || !policy_allows_side_effect(&policy, "process_exec") =>
324 {
325 return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
326 }
327 "http_get" | "http_post" | "http_put" | "http_patch" | "http_delete" | "http_request"
328 if !policy_allows_side_effect(&policy, "network") =>
329 {
330 return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
331 }
332 "mcp_connect"
333 | "mcp_call"
334 | "mcp_list_tools"
335 | "mcp_list_resources"
336 | "mcp_list_resource_templates"
337 | "mcp_read_resource"
338 | "mcp_list_prompts"
339 | "mcp_get_prompt"
340 | "mcp_server_info"
341 | "mcp_disconnect"
342 if !policy_allows_capability(&policy, "process", "exec")
343 || !policy_allows_side_effect(&policy, "process_exec") =>
344 {
345 return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
346 }
347 "host_call" => {
348 let name = args.first().map(|v| v.display()).unwrap_or_default();
349 let Some((capability, op)) = name.split_once('.') else {
350 return reject_policy(format!(
351 "host_call '{name}' must use capability.operation naming"
352 ));
353 };
354 if !policy_allows_capability(&policy, capability, op) {
355 return reject_policy(format!(
356 "host_call {capability}.{op} exceeds capability ceiling"
357 ));
358 }
359 let requested_side_effect = match (capability, op) {
360 ("workspace", "write_text" | "apply_edit" | "delete") => "workspace_write",
361 ("process", "exec") => "process_exec",
362 _ => "read_only",
363 };
364 if !policy_allows_side_effect(&policy, requested_side_effect) {
365 return reject_policy(format!(
366 "host_call {capability}.{op} exceeds side-effect ceiling"
367 ));
368 }
369 }
370 _ => {}
371 }
372 Ok(())
373}
374
375pub fn enforce_current_policy_for_bridge_builtin(name: &str) -> Result<(), VmError> {
376 let trusted = TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow() > 0);
377 if trusted {
378 return Ok(());
379 }
380 if current_execution_policy().is_some() {
381 return reject_policy(format!(
382 "bridged builtin '{name}' exceeds execution policy; declare an explicit capability/tool surface instead"
383 ));
384 }
385 Ok(())
386}
387
388pub fn enforce_current_policy_for_tool(tool_name: &str) -> Result<(), VmError> {
389 let Some(policy) = current_execution_policy() else {
390 return Ok(());
391 };
392 if !policy_allows_tool(&policy, tool_name) {
393 return reject_policy(format!("tool '{tool_name}' exceeds tool ceiling"));
394 }
395 if let Some(annotations) = policy.tool_annotations.get(tool_name) {
396 for (capability, ops) in &annotations.capabilities {
397 for op in ops {
398 if !policy_allows_capability(&policy, capability, op) {
399 return reject_policy(format!(
400 "tool '{tool_name}' exceeds capability ceiling: {capability}.{op}"
401 ));
402 }
403 }
404 }
405 let requested_level = annotations.side_effect_level;
406 if requested_level != SideEffectLevel::None
407 && !policy_allows_side_effect(&policy, requested_level.as_str())
408 {
409 return reject_policy(format!(
410 "tool '{tool_name}' exceeds side-effect ceiling: {}",
411 requested_level.as_str()
412 ));
413 }
414 }
415 Ok(())
416}
417
418pub fn redact_transcript_visibility(
430 transcript: &VmValue,
431 visibility: Option<&str>,
432) -> Option<VmValue> {
433 let Some(visibility) = visibility else {
434 return Some(transcript.clone());
435 };
436 if visibility != "public" && visibility != "public_only" {
437 return Some(transcript.clone());
438 }
439 let dict = transcript.as_dict()?;
440 let public_messages = match dict.get("messages") {
441 Some(VmValue::List(list)) => list
442 .iter()
443 .filter(|message| {
444 message
445 .as_dict()
446 .and_then(|d| d.get("role"))
447 .map(|v| v.display())
448 .map(|role| role != "tool_result")
449 .unwrap_or(true)
450 })
451 .cloned()
452 .collect::<Vec<_>>(),
453 _ => Vec::new(),
454 };
455 let public_events = match dict.get("events") {
456 Some(VmValue::List(list)) => list
457 .iter()
458 .filter(|event| {
459 event
460 .as_dict()
461 .and_then(|d| d.get("visibility"))
462 .map(|v| v.display())
463 .map(|value| value == "public")
464 .unwrap_or(true)
465 })
466 .cloned()
467 .collect::<Vec<_>>(),
468 _ => Vec::new(),
469 };
470 let mut redacted = dict.clone();
471 redacted.insert(
472 "messages".to_string(),
473 VmValue::List(Rc::new(public_messages)),
474 );
475 redacted.insert("events".to_string(), VmValue::List(Rc::new(public_events)));
476 Some(VmValue::Dict(Rc::new(redacted)))
477}
478
479pub fn builtin_ceiling() -> CapabilityPolicy {
480 CapabilityPolicy {
481 tools: Vec::new(),
485 capabilities: BTreeMap::new(),
486 workspace_roots: Vec::new(),
487 side_effect_level: Some("network".to_string()),
488 recursion_limit: Some(8),
489 tool_arg_constraints: Vec::new(),
490 tool_annotations: BTreeMap::new(),
491 }
492}
493
494#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
498#[serde(default)]
499pub struct ToolApprovalPolicy {
500 #[serde(default)]
502 pub auto_approve: Vec<String>,
503 #[serde(default)]
505 pub auto_deny: Vec<String>,
506 #[serde(default)]
508 pub require_approval: Vec<String>,
509 #[serde(default)]
511 pub write_path_allowlist: Vec<String>,
512}
513
514#[derive(Debug, Clone, PartialEq, Eq)]
516pub enum ToolApprovalDecision {
517 AutoApproved,
519 AutoDenied { reason: String },
521 RequiresHostApproval,
524}
525
526impl ToolApprovalPolicy {
527 pub fn evaluate(&self, tool_name: &str, args: &serde_json::Value) -> ToolApprovalDecision {
530 for pattern in &self.auto_deny {
532 if glob_match(pattern, tool_name) {
533 return ToolApprovalDecision::AutoDenied {
534 reason: format!("tool '{tool_name}' matches deny pattern '{pattern}'"),
535 };
536 }
537 }
538
539 if !self.write_path_allowlist.is_empty()
540 && tool_kind_participates_in_write_allowlist(tool_name)
541 {
542 let paths = super::current_tool_declared_path_entries(tool_name, args);
543 for path in &paths {
544 let allowed = self.write_path_allowlist.iter().any(|pattern| {
545 path.policy_candidates()
546 .iter()
547 .any(|candidate| glob_match(pattern, candidate))
548 });
549 if !allowed {
550 return ToolApprovalDecision::AutoDenied {
551 reason: format!(
552 "tool '{tool_name}' targets '{}' which is not in the write-path allowlist",
553 path.display_path()
554 ),
555 };
556 }
557 }
558 }
559
560 for pattern in &self.auto_approve {
561 if glob_match(pattern, tool_name) {
562 return ToolApprovalDecision::AutoApproved;
563 }
564 }
565
566 for pattern in &self.require_approval {
567 if glob_match(pattern, tool_name) {
568 return ToolApprovalDecision::RequiresHostApproval;
569 }
570 }
571
572 ToolApprovalDecision::AutoApproved
573 }
574
575 pub fn intersect(&self, other: &ToolApprovalPolicy) -> ToolApprovalPolicy {
581 let auto_approve = if self.auto_approve.is_empty() {
582 other.auto_approve.clone()
583 } else if other.auto_approve.is_empty() {
584 self.auto_approve.clone()
585 } else {
586 self.auto_approve
587 .iter()
588 .filter(|p| other.auto_approve.contains(p))
589 .cloned()
590 .collect()
591 };
592 let mut auto_deny = self.auto_deny.clone();
593 auto_deny.extend(other.auto_deny.iter().cloned());
594 let mut require_approval = self.require_approval.clone();
595 require_approval.extend(other.require_approval.iter().cloned());
596 let write_path_allowlist = if self.write_path_allowlist.is_empty() {
597 other.write_path_allowlist.clone()
598 } else if other.write_path_allowlist.is_empty() {
599 self.write_path_allowlist.clone()
600 } else {
601 self.write_path_allowlist
602 .iter()
603 .filter(|p| other.write_path_allowlist.contains(p))
604 .cloned()
605 .collect()
606 };
607 ToolApprovalPolicy {
608 auto_approve,
609 auto_deny,
610 require_approval,
611 write_path_allowlist,
612 }
613 }
614}
615
616#[cfg(test)]
617mod approval_policy_tests {
618 use super::*;
619 use crate::orchestration::{pop_execution_policy, push_execution_policy, CapabilityPolicy};
620 use crate::tool_annotations::{ToolAnnotations, ToolArgSchema, ToolKind};
621
622 #[test]
623 fn auto_deny_takes_precedence_over_auto_approve() {
624 let policy = ToolApprovalPolicy {
625 auto_approve: vec!["*".to_string()],
626 auto_deny: vec!["dangerous_*".to_string()],
627 ..Default::default()
628 };
629 assert_eq!(
630 policy.evaluate("dangerous_rm", &serde_json::json!({})),
631 ToolApprovalDecision::AutoDenied {
632 reason: "tool 'dangerous_rm' matches deny pattern 'dangerous_*'".to_string()
633 }
634 );
635 }
636
637 #[test]
638 fn auto_approve_matches_glob() {
639 let policy = ToolApprovalPolicy {
640 auto_approve: vec!["read*".to_string(), "search*".to_string()],
641 ..Default::default()
642 };
643 assert_eq!(
644 policy.evaluate("read_file", &serde_json::json!({})),
645 ToolApprovalDecision::AutoApproved
646 );
647 assert_eq!(
648 policy.evaluate("search", &serde_json::json!({})),
649 ToolApprovalDecision::AutoApproved
650 );
651 }
652
653 #[test]
654 fn require_approval_emits_decision() {
655 let policy = ToolApprovalPolicy {
656 require_approval: vec!["edit*".to_string()],
657 ..Default::default()
658 };
659 let decision = policy.evaluate("edit_file", &serde_json::json!({"path": "foo.rs"}));
660 assert!(matches!(
661 decision,
662 ToolApprovalDecision::RequiresHostApproval
663 ));
664 }
665
666 #[test]
667 fn unmatched_tool_defaults_to_approved() {
668 let policy = ToolApprovalPolicy {
669 auto_approve: vec!["read*".to_string()],
670 require_approval: vec!["edit*".to_string()],
671 ..Default::default()
672 };
673 assert_eq!(
674 policy.evaluate("unknown_tool", &serde_json::json!({})),
675 ToolApprovalDecision::AutoApproved
676 );
677 }
678
679 #[test]
680 fn intersect_merges_deny_lists() {
681 let a = ToolApprovalPolicy {
682 auto_deny: vec!["rm*".to_string()],
683 ..Default::default()
684 };
685 let b = ToolApprovalPolicy {
686 auto_deny: vec!["drop*".to_string()],
687 ..Default::default()
688 };
689 let merged = a.intersect(&b);
690 assert_eq!(merged.auto_deny.len(), 2);
691 }
692
693 #[test]
694 fn intersect_restricts_auto_approve_to_common_patterns() {
695 let a = ToolApprovalPolicy {
696 auto_approve: vec!["read*".to_string(), "search*".to_string()],
697 ..Default::default()
698 };
699 let b = ToolApprovalPolicy {
700 auto_approve: vec!["read*".to_string(), "write*".to_string()],
701 ..Default::default()
702 };
703 let merged = a.intersect(&b);
704 assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
705 }
706
707 #[test]
708 fn intersect_defers_auto_approve_when_one_side_empty() {
709 let a = ToolApprovalPolicy {
710 auto_approve: vec!["read*".to_string()],
711 ..Default::default()
712 };
713 let b = ToolApprovalPolicy::default();
714 let merged = a.intersect(&b);
715 assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
716 }
717
718 #[test]
719 fn write_path_allowlist_matches_recovered_workspace_relative_path() {
720 let temp = tempfile::tempdir().unwrap();
721 std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
722 std::fs::write(temp.path().join("packages/demo/file.txt"), "ok").unwrap();
723 crate::stdlib::process::set_thread_execution_context(Some(
724 crate::orchestration::RunExecutionRecord {
725 cwd: Some(temp.path().to_string_lossy().into_owned()),
726 source_dir: Some(temp.path().to_string_lossy().into_owned()),
727 env: BTreeMap::new(),
728 adapter: None,
729 repo_path: None,
730 worktree_path: None,
731 branch: None,
732 base_ref: None,
733 cleanup: None,
734 },
735 ));
736
737 let mut tool_annotations = BTreeMap::new();
738 tool_annotations.insert(
739 "write_file".to_string(),
740 ToolAnnotations {
741 kind: ToolKind::Edit,
742 arg_schema: ToolArgSchema {
743 path_params: vec!["path".to_string()],
744 ..Default::default()
745 },
746 ..Default::default()
747 },
748 );
749 push_execution_policy(CapabilityPolicy {
750 tool_annotations,
751 ..Default::default()
752 });
753
754 let policy = ToolApprovalPolicy {
755 write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
756 ..Default::default()
757 };
758 let decision = policy.evaluate(
759 "write_file",
760 &serde_json::json!({"path": "/packages/demo/file.txt"}),
761 );
762 assert_eq!(decision, ToolApprovalDecision::AutoApproved);
763
764 pop_execution_policy();
765 crate::stdlib::process::set_thread_execution_context(None);
766 }
767
768 #[test]
769 fn write_path_allowlist_does_not_block_read_only_tools() {
770 let temp = tempfile::tempdir().unwrap();
771 std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
772 std::fs::write(temp.path().join("packages/demo/context.txt"), "ok").unwrap();
773 crate::stdlib::process::set_thread_execution_context(Some(
774 crate::orchestration::RunExecutionRecord {
775 cwd: Some(temp.path().to_string_lossy().into_owned()),
776 source_dir: Some(temp.path().to_string_lossy().into_owned()),
777 env: BTreeMap::new(),
778 adapter: None,
779 repo_path: None,
780 worktree_path: None,
781 branch: None,
782 base_ref: None,
783 cleanup: None,
784 },
785 ));
786
787 let mut tool_annotations = BTreeMap::new();
788 tool_annotations.insert(
789 "read_file".to_string(),
790 ToolAnnotations {
791 kind: ToolKind::Read,
792 arg_schema: ToolArgSchema {
793 path_params: vec!["path".to_string()],
794 ..Default::default()
795 },
796 ..Default::default()
797 },
798 );
799 push_execution_policy(CapabilityPolicy {
800 tool_annotations,
801 ..Default::default()
802 });
803
804 let policy = ToolApprovalPolicy {
805 write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
806 ..Default::default()
807 };
808 let decision = policy.evaluate(
809 "read_file",
810 &serde_json::json!({"path": "/packages/demo/context.txt"}),
811 );
812 assert_eq!(decision, ToolApprovalDecision::AutoApproved);
813
814 pop_execution_policy();
815 crate::stdlib::process::set_thread_execution_context(None);
816 }
817}
818
819#[cfg(test)]
820mod turn_policy_tests {
821 use super::TurnPolicy;
822
823 #[test]
824 fn default_allows_done_sentinel() {
825 let policy = TurnPolicy::default();
826 assert!(policy.allow_done_sentinel);
827 assert!(!policy.require_action_or_yield);
828 assert!(policy.max_prose_chars.is_none());
829 }
830
831 #[test]
832 fn deserializing_partial_dict_preserves_done_sentinel_pathway() {
833 let policy: TurnPolicy =
838 serde_json::from_value(serde_json::json!({ "require_action_or_yield": true }))
839 .expect("deserialize");
840 assert!(policy.require_action_or_yield);
841 assert!(policy.allow_done_sentinel);
842 }
843
844 #[test]
845 fn deserializing_explicit_false_disables_done_sentinel() {
846 let policy: TurnPolicy = serde_json::from_value(serde_json::json!({
847 "require_action_or_yield": true,
848 "allow_done_sentinel": false,
849 }))
850 .expect("deserialize");
851 assert!(policy.require_action_or_yield);
852 assert!(!policy.allow_done_sentinel);
853 }
854}
855
856#[cfg(test)]
857mod visibility_redaction_tests {
858 use super::*;
859 use crate::value::VmValue;
860
861 fn mock_transcript() -> VmValue {
862 let messages = vec![
863 serde_json::json!({"role": "user", "content": "hi"}),
864 serde_json::json!({"role": "assistant", "content": "hello"}),
865 serde_json::json!({"role": "tool_result", "content": "internal tool output"}),
866 ];
867 crate::llm::helpers::transcript_to_vm_with_events(
868 Some("test-id".to_string()),
869 None,
870 None,
871 &messages,
872 Vec::new(),
873 Vec::new(),
874 Some("active"),
875 )
876 }
877
878 fn message_count(transcript: &VmValue) -> usize {
879 transcript
880 .as_dict()
881 .and_then(|d| d.get("messages"))
882 .and_then(|v| match v {
883 VmValue::List(list) => Some(list.len()),
884 _ => None,
885 })
886 .unwrap_or(0)
887 }
888
889 #[test]
890 fn visibility_none_returns_unchanged() {
891 let t = mock_transcript();
892 let result = redact_transcript_visibility(&t, None).unwrap();
893 assert_eq!(message_count(&result), 3);
894 }
895
896 #[test]
897 fn visibility_public_drops_tool_results() {
898 let t = mock_transcript();
899 let result = redact_transcript_visibility(&t, Some("public")).unwrap();
900 assert_eq!(message_count(&result), 2);
901 }
902
903 #[test]
904 fn visibility_unknown_string_is_pass_through() {
905 let t = mock_transcript();
906 let result = redact_transcript_visibility(&t, Some("internal")).unwrap();
907 assert_eq!(message_count(&result), 3);
908 }
909}