Skip to main content

harn_vm/orchestration/policy/
mod.rs

1//! Policy types and capability-ceiling enforcement.
2
3mod approval_rules;
4mod types;
5
6use std::cell::RefCell;
7use std::collections::BTreeMap;
8use std::rc::Rc;
9use std::thread_local;
10
11use serde::{Deserialize, Serialize};
12
13use crate::tool_annotations::{SideEffectLevel, ToolAnnotations};
14use crate::value::{VmError, VmValue};
15use crate::workspace_path::{classify_workspace_path, WorkspacePathInfo};
16
17pub use crate::tool_annotations::{ToolArgSchema, ToolKind};
18pub use approval_rules::{
19    clear_all_approval_policy_repeat_counts, clear_approval_policy_repeat_counts,
20    next_approval_policy_repeat_count, ApprovalShape, PolicyAction, PolicyEvaluation,
21    PolicyMatchedRule, PolicyRule, PolicyRuleMatch,
22};
23pub use types::{
24    enforce_tool_arg_constraints, AutoCompactPolicy, BranchSemantics, CapabilityPolicy,
25    ContextPolicy, EqIgnored, EscalationPolicy, JoinPolicy, MapPolicy, ModelPolicy,
26    NativeToolFallbackPolicy, ReducePolicy, RetryPolicy, SandboxProfile, StageContract,
27    ToolArgConstraint, TurnPolicy,
28};
29
30thread_local! {
31    static EXECUTION_POLICY_STACK: RefCell<Vec<CapabilityPolicy>> = const { RefCell::new(Vec::new()) };
32    static EXECUTION_APPROVAL_POLICY_STACK: RefCell<Vec<ToolApprovalPolicy>> = const { RefCell::new(Vec::new()) };
33    static TRUSTED_BRIDGE_CALL_DEPTH: RefCell<usize> = const { RefCell::new(0) };
34}
35
36pub fn push_execution_policy(policy: CapabilityPolicy) {
37    EXECUTION_POLICY_STACK.with(|stack| stack.borrow_mut().push(policy));
38}
39
40pub fn pop_execution_policy() {
41    EXECUTION_POLICY_STACK.with(|stack| {
42        stack.borrow_mut().pop();
43    });
44}
45
46pub fn clear_execution_policy_stacks() {
47    EXECUTION_POLICY_STACK.with(|stack| stack.borrow_mut().clear());
48    EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow_mut().clear());
49    TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow_mut() = 0);
50}
51
52pub fn current_execution_policy() -> Option<CapabilityPolicy> {
53    EXECUTION_POLICY_STACK.with(|stack| stack.borrow().last().cloned())
54}
55
56pub fn push_approval_policy(policy: ToolApprovalPolicy) {
57    EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow_mut().push(policy));
58}
59
60pub fn pop_approval_policy() {
61    EXECUTION_APPROVAL_POLICY_STACK.with(|stack| {
62        stack.borrow_mut().pop();
63    });
64}
65
66pub fn current_approval_policy() -> Option<ToolApprovalPolicy> {
67    EXECUTION_APPROVAL_POLICY_STACK.with(|stack| stack.borrow().last().cloned())
68}
69
70pub fn current_tool_annotations(tool: &str) -> Option<ToolAnnotations> {
71    current_execution_policy().and_then(|policy| policy.tool_annotations.get(tool).cloned())
72}
73
74pub(super) fn tool_kind_participates_in_write_allowlist(tool_name: &str) -> bool {
75    current_tool_annotations(tool_name)
76        .map(|annotations| !annotations.kind.is_read_only())
77        .unwrap_or(true)
78}
79
80pub struct TrustedBridgeCallGuard;
81
82pub fn allow_trusted_bridge_calls() -> TrustedBridgeCallGuard {
83    TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| {
84        *depth.borrow_mut() += 1;
85    });
86    TrustedBridgeCallGuard
87}
88
89impl Drop for TrustedBridgeCallGuard {
90    fn drop(&mut self) {
91        TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| {
92            let mut depth = depth.borrow_mut();
93            *depth = depth.saturating_sub(1);
94        });
95    }
96}
97
98fn policy_allows_tool(policy: &CapabilityPolicy, tool: &str) -> bool {
99    policy.tools.is_empty() || policy.tools.iter().any(|allowed| allowed == tool)
100}
101
102fn policy_allows_capability(policy: &CapabilityPolicy, capability: &str, op: &str) -> bool {
103    policy.capabilities.is_empty()
104        || policy
105            .capabilities
106            .get(capability)
107            .is_some_and(|ops| ops.is_empty() || ops.iter().any(|allowed| allowed == op))
108}
109
110fn policy_allows_side_effect(policy: &CapabilityPolicy, requested: &str) -> bool {
111    fn rank(v: &str) -> usize {
112        match v {
113            "none" => 0,
114            "read_only" => 1,
115            "workspace_write" => 2,
116            "process_exec" => 3,
117            "network" => 4,
118            _ => 5,
119        }
120    }
121    policy
122        .side_effect_level
123        .as_ref()
124        .map(|allowed| rank(allowed) >= rank(requested))
125        .unwrap_or(true)
126}
127
128pub(super) fn reject_policy(reason: String) -> Result<(), VmError> {
129    Err(VmError::CategorizedError {
130        message: reason,
131        category: crate::value::ErrorCategory::ToolRejected,
132    })
133}
134
135/// Mutation classification for a tool, derived from the pipeline's
136/// declared `ToolKind`. Used in telemetry and pre/post-bridge payloads
137/// while those methods still exist. Returns `"other"` for unannotated
138/// tools (fail-safe; unknown tools don't auto-classify).
139pub fn current_tool_mutation_classification(tool_name: &str) -> String {
140    current_tool_annotations(tool_name)
141        .map(|annotations| annotations.kind.mutation_class().to_string())
142        .unwrap_or_else(|| "other".to_string())
143}
144
145/// Workspace paths declared by this tool call, read from the tool's
146/// annotated `arg_schema.path_params`. Unannotated tools declare no
147/// paths — the VM no longer guesses by common argument names.
148pub fn current_tool_declared_paths(tool_name: &str, args: &serde_json::Value) -> Vec<String> {
149    current_tool_declared_path_entries(tool_name, args)
150        .into_iter()
151        .map(|entry| entry.display_path().to_string())
152        .collect()
153}
154
155/// Rich workspace-path descriptors declared by this tool call. Each
156/// entry preserves the original input while also projecting the path
157/// into workspace-relative and host-absolute forms when that mapping is
158/// known.
159pub fn current_tool_declared_path_entries(
160    tool_name: &str,
161    args: &serde_json::Value,
162) -> Vec<WorkspacePathInfo> {
163    let Some(map) = args.as_object() else {
164        return Vec::new();
165    };
166    let Some(annotations) = current_tool_annotations(tool_name) else {
167        return Vec::new();
168    };
169    let workspace_root = crate::stdlib::process::execution_root_path();
170    let mut entries = Vec::new();
171    for key in &annotations.arg_schema.path_params {
172        if let Some(value) = map.get(key) {
173            match value {
174                serde_json::Value::String(path) if !path.is_empty() => {
175                    entries.push(classify_workspace_path(path, Some(&workspace_root)));
176                }
177                serde_json::Value::Array(items) => {
178                    for item in items.iter().filter_map(|item| item.as_str()) {
179                        if !item.is_empty() {
180                            entries.push(classify_workspace_path(item, Some(&workspace_root)));
181                        }
182                    }
183                }
184                _ => {}
185            }
186        }
187    }
188    entries.sort_by(|a, b| a.display_path().cmp(b.display_path()));
189    entries.dedup_by(|left, right| left.policy_candidates() == right.policy_candidates());
190    entries
191}
192
193pub fn enforce_current_policy_for_builtin(name: &str, args: &[VmValue]) -> Result<(), VmError> {
194    let Some(policy) = current_execution_policy() else {
195        return Ok(());
196    };
197    match name {
198        "read_file" | "read_file_result" | "read_file_bytes"
199            if !policy_allows_capability(&policy, "workspace", "read_text") =>
200        {
201            return reject_policy(format!(
202                "builtin '{name}' exceeds workspace.read_text ceiling"
203            ));
204        }
205        "list_dir" if !policy_allows_capability(&policy, "workspace", "list") => {
206            return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
207        }
208        "file_exists" | "stat" if !policy_allows_capability(&policy, "workspace", "exists") => {
209            return reject_policy(format!("builtin '{name}' exceeds workspace.exists ceiling"));
210        }
211        "write_file" | "write_file_bytes" | "append_file" | "mkdir" | "copy_file"
212            if !policy_allows_capability(&policy, "workspace", "write_text")
213                || !policy_allows_side_effect(&policy, "workspace_write") =>
214        {
215            return reject_policy(format!("builtin '{name}' exceeds workspace write ceiling"));
216        }
217        "delete_file"
218            if !policy_allows_capability(&policy, "workspace", "delete")
219                || !policy_allows_side_effect(&policy, "workspace_write") =>
220        {
221            return reject_policy(
222                "builtin 'delete_file' exceeds workspace.delete ceiling".to_string(),
223            );
224        }
225        "apply_edit"
226            if !policy_allows_capability(&policy, "workspace", "apply_edit")
227                || !policy_allows_side_effect(&policy, "workspace_write") =>
228        {
229            return reject_policy(
230                "builtin 'apply_edit' exceeds workspace.apply_edit ceiling".to_string(),
231            );
232        }
233        "exec"
234        | "exec_at"
235        | "shell"
236        | "shell_at"
237        | "git.repo.discover"
238        | "git.worktree.create"
239        | "git.worktree.remove"
240        | "git.fetch"
241        | "git.rebase"
242        | "git.status"
243        | "git.conflicts"
244        | "git.push"
245        | "git.diff"
246        | "git.merge_base"
247            if !policy_allows_capability(&policy, "process", "exec")
248                || !policy_allows_side_effect(&policy, "process_exec") =>
249        {
250            return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
251        }
252        "http_get" | "http_post" | "http_put" | "http_patch" | "http_delete" | "http_download"
253        | "http_request"
254            if !policy_allows_side_effect(&policy, "network") =>
255        {
256            return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
257        }
258        "http_session_request"
259        | "http_stream_open"
260        | "http_stream_read"
261        | "http_stream_close"
262        | "http_stream_info"
263        | "sse_connect"
264        | "sse_receive"
265        | "websocket_accept"
266        | "websocket_connect"
267        | "websocket_route"
268        | "websocket_send"
269        | "websocket_receive"
270        | "websocket_server"
271            if !policy_allows_side_effect(&policy, "network") =>
272        {
273            return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
274        }
275        "llm_call" | "llm_call_safe" | "llm_completion" | "llm_stream" | "llm_stream_call"
276        | "llm_healthcheck" | "agent_loop"
277            if !policy_allows_capability(&policy, "llm", "call") =>
278        {
279            return reject_policy(format!("builtin '{name}' exceeds llm.call ceiling"));
280        }
281        "connector_call"
282            if !policy_allows_capability(&policy, "connector", "call")
283                || !policy_allows_side_effect(&policy, "network") =>
284        {
285            return reject_policy(
286                "builtin 'connector_call' exceeds connector.call/network ceiling".to_string(),
287            );
288        }
289        "secret_get" if !policy_allows_capability(&policy, "connector", "secret_get") => {
290            return reject_policy(
291                "builtin 'secret_get' exceeds connector.secret_get ceiling".to_string(),
292            );
293        }
294        "event_log_emit" if !policy_allows_capability(&policy, "connector", "event_log_emit") => {
295            return reject_policy(
296                "builtin 'event_log_emit' exceeds connector.event_log_emit ceiling".to_string(),
297            );
298        }
299        "metrics_inc" if !policy_allows_capability(&policy, "connector", "metrics_inc") => {
300            return reject_policy(
301                "builtin 'metrics_inc' exceeds connector.metrics_inc ceiling".to_string(),
302            );
303        }
304        "project_fingerprint"
305        | "project_scan_native"
306        | "project_scan_tree_native"
307        | "project_walk_tree_native"
308        | "project_catalog_native"
309            if !policy_allows_capability(&policy, "workspace", "list")
310                || !policy_allows_side_effect(&policy, "read_only") =>
311        {
312            return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
313        }
314        "__agent_state_init"
315        | "__agent_state_resume"
316        | "__agent_state_write"
317        | "__agent_state_read"
318        | "__agent_state_list"
319        | "__agent_state_delete"
320        | "__agent_state_handoff"
321            if !policy_allows_capability(&policy, "agent_state", "access") =>
322        {
323            return reject_policy(format!(
324                "builtin '{name}' exceeds agent_state.access ceiling"
325            ));
326        }
327        "vision_ocr"
328            if !policy_allows_capability(&policy, "vision", "ocr")
329                || !policy_allows_side_effect(&policy, "process_exec") =>
330        {
331            return reject_policy(format!(
332                "builtin '{name}' exceeds vision.ocr/process ceiling"
333            ));
334        }
335        "mcp_connect"
336        | "mcp_ensure_active"
337        | "mcp_call"
338        | "mcp_list_tools"
339        | "mcp_list_resources"
340        | "mcp_list_resource_templates"
341        | "mcp_read_resource"
342        | "mcp_list_prompts"
343        | "mcp_get_prompt"
344        | "mcp_server_info"
345        | "mcp_disconnect"
346            if !policy_allows_capability(&policy, "process", "exec")
347                || !policy_allows_side_effect(&policy, "process_exec") =>
348        {
349            return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
350        }
351        "host_call" => {
352            let name = args.first().map(|v| v.display()).unwrap_or_default();
353            let Some((capability, op)) = name.split_once('.') else {
354                return reject_policy(format!(
355                    "host_call '{name}' must use capability.operation naming"
356                ));
357            };
358            if !policy_allows_capability(&policy, capability, op) {
359                return reject_policy(format!(
360                    "host_call {capability}.{op} exceeds capability ceiling"
361                ));
362            }
363            let requested_side_effect = match (capability, op) {
364                ("workspace", "write_text" | "apply_edit" | "delete") => "workspace_write",
365                ("process", "exec") => "process_exec",
366                _ => "read_only",
367            };
368            if !policy_allows_side_effect(&policy, requested_side_effect) {
369                return reject_policy(format!(
370                    "host_call {capability}.{op} exceeds side-effect ceiling"
371                ));
372            }
373        }
374        "host_tool_list" | "host_tool_call"
375            if !policy_allows_capability(&policy, "host", "tool_call") =>
376        {
377            return reject_policy(format!("builtin '{name}' exceeds host.tool_call ceiling"));
378        }
379        _ => {}
380    }
381    Ok(())
382}
383
384pub fn enforce_current_policy_for_bridge_builtin(name: &str) -> Result<(), VmError> {
385    let trusted = TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow() > 0);
386    if trusted {
387        return Ok(());
388    }
389    if current_execution_policy().is_some() {
390        return reject_policy(format!(
391            "bridged builtin '{name}' exceeds execution policy; declare an explicit capability/tool surface instead"
392        ));
393    }
394    Ok(())
395}
396
397pub fn enforce_current_policy_for_tool(tool_name: &str) -> Result<(), VmError> {
398    let Some(policy) = current_execution_policy() else {
399        return Ok(());
400    };
401    if !policy_allows_tool(&policy, tool_name) {
402        return reject_policy(format!("tool '{tool_name}' exceeds tool ceiling"));
403    }
404    if let Some(annotations) = policy.tool_annotations.get(tool_name) {
405        for (capability, ops) in &annotations.capabilities {
406            for op in ops {
407                if !policy_allows_capability(&policy, capability, op) {
408                    return reject_policy(format!(
409                        "tool '{tool_name}' exceeds capability ceiling: {capability}.{op}"
410                    ));
411                }
412            }
413        }
414        let requested_level = annotations.side_effect_level;
415        if requested_level != SideEffectLevel::None
416            && !policy_allows_side_effect(&policy, requested_level.as_str())
417        {
418            return reject_policy(format!(
419                "tool '{tool_name}' exceeds side-effect ceiling: {}",
420                requested_level.as_str()
421            ));
422        }
423    }
424    Ok(())
425}
426
427// ── Output visibility redaction ─────────────────────────────────────
428//
429// Transcript lifecycle (reset, fork, trim, compact) now lives on
430// `crate::agent_sessions` as explicit imperative builtins. All that
431// remains here is the per-call visibility filter, which is
432// output-shaping (not lifecycle).
433
434/// Filter a transcript dict down to the caller-visible subset, based
435/// on the `output_visibility` node option. `None` or any unknown
436/// visibility returns the transcript unchanged — callers are expected
437/// to validate the string against a known set upstream.
438pub fn redact_transcript_visibility(
439    transcript: &VmValue,
440    visibility: Option<&str>,
441) -> Option<VmValue> {
442    let Some(visibility) = visibility else {
443        return Some(transcript.clone());
444    };
445    if visibility != "public" && visibility != "public_only" {
446        return Some(transcript.clone());
447    }
448    let dict = transcript.as_dict()?;
449    let public_messages = match dict.get("messages") {
450        Some(VmValue::List(list)) => list
451            .iter()
452            .filter(|message| {
453                message
454                    .as_dict()
455                    .and_then(|d| d.get("role"))
456                    .map(|v| v.display())
457                    .map(|role| role != "tool_result")
458                    .unwrap_or(true)
459            })
460            .cloned()
461            .collect::<Vec<_>>(),
462        _ => Vec::new(),
463    };
464    let public_events = match dict.get("events") {
465        Some(VmValue::List(list)) => list
466            .iter()
467            .filter(|event| {
468                event
469                    .as_dict()
470                    .and_then(|d| d.get("visibility"))
471                    .map(|v| v.display())
472                    .map(|value| value == "public")
473                    .unwrap_or(true)
474            })
475            .cloned()
476            .collect::<Vec<_>>(),
477        _ => Vec::new(),
478    };
479    let mut redacted = dict.clone();
480    redacted.insert(
481        "messages".to_string(),
482        VmValue::List(Rc::new(public_messages)),
483    );
484    redacted.insert("events".to_string(), VmValue::List(Rc::new(public_events)));
485    Some(VmValue::Dict(Rc::new(redacted)))
486}
487
488pub fn builtin_ceiling() -> CapabilityPolicy {
489    CapabilityPolicy {
490        // `capabilities` is intentionally empty: the host capability manifest
491        // is the sole authority, and an allowlist here would silently block
492        // any capability the host adds later.
493        tools: Vec::new(),
494        capabilities: BTreeMap::new(),
495        workspace_roots: Vec::new(),
496        side_effect_level: Some("network".to_string()),
497        recursion_limit: Some(8),
498        tool_arg_constraints: Vec::new(),
499        tool_annotations: BTreeMap::new(),
500        sandbox_profile: SandboxProfile::Worktree,
501    }
502}
503
504/// Declarative policy for tool approval gating. Allows pipelines to
505/// specify which tools are auto-approved, auto-denied, or require
506/// host confirmation, plus write-path allowlists.
507#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
508#[serde(default)]
509pub struct ToolApprovalPolicy {
510    /// Ordered allow/ask/deny rules over tool metadata, path, command,
511    /// URL, MCP, agent/persona/mode, and repeat-count dimensions.
512    #[serde(default)]
513    pub rules: Vec<PolicyRule>,
514    /// Glob patterns for tools that should be auto-approved.
515    #[serde(default)]
516    pub auto_approve: Vec<String>,
517    /// Glob patterns for tools that should always be denied.
518    #[serde(default)]
519    pub auto_deny: Vec<String>,
520    /// Glob patterns for tools that require host confirmation.
521    #[serde(default)]
522    pub require_approval: Vec<String>,
523    /// Glob patterns for writable paths.
524    #[serde(default)]
525    pub write_path_allowlist: Vec<String>,
526    /// Explicit opt-out for the deny-by-default sensitive-path guard.
527    #[serde(default)]
528    pub allow_sensitive_paths: bool,
529    /// Additional or replacement sensitive path globs. Empty uses the
530    /// runtime defaults such as `.env`, private keys, and credential files.
531    #[serde(default)]
532    pub sensitive_path_patterns: Vec<String>,
533    /// Explicit opt-out for the external-path guard on declared path args.
534    #[serde(default)]
535    pub allow_external_paths: bool,
536    /// Host-absolute roots allowed when `allow_external_paths` is false.
537    #[serde(default)]
538    pub external_roots: Vec<String>,
539    /// Optional repeated-call threshold for the same `(session, tool, args)`.
540    #[serde(default, alias = "repeated_call_limit")]
541    pub repeat_limit: Option<u64>,
542    /// Action for `repeat_limit`; defaults to `ask`.
543    #[serde(default, alias = "repeated_call_action")]
544    pub repeat_action: Option<PolicyAction>,
545}
546
547/// Result of evaluating a tool call against a ToolApprovalPolicy.
548#[derive(Debug, Clone, PartialEq, Eq)]
549pub enum ToolApprovalDecision {
550    /// Tool is auto-approved by policy.
551    AutoApproved,
552    /// Tool is auto-denied by policy.
553    AutoDenied { reason: String },
554    /// Tool requires explicit host approval; the caller already owns the
555    /// tool name and args and forwards them to the host bridge.
556    RequiresHostApproval,
557}
558
559impl ToolApprovalPolicy {
560    pub fn evaluate_detailed(&self, tool_name: &str, args: &serde_json::Value) -> PolicyEvaluation {
561        approval_rules::evaluate_tool_approval_policy(self, tool_name, args, None)
562    }
563
564    pub fn evaluate_detailed_with_repeat(
565        &self,
566        tool_name: &str,
567        args: &serde_json::Value,
568        repeat_count: u64,
569    ) -> PolicyEvaluation {
570        approval_rules::evaluate_tool_approval_policy(self, tool_name, args, Some(repeat_count))
571    }
572
573    /// Evaluate whether a tool call should be approved, denied, or needs
574    /// host confirmation.
575    pub fn evaluate(&self, tool_name: &str, args: &serde_json::Value) -> ToolApprovalDecision {
576        let decision = self.evaluate_detailed(tool_name, args);
577        if decision.is_deny() {
578            return ToolApprovalDecision::AutoDenied {
579                reason: decision.reason,
580            };
581        }
582        if decision.is_ask() {
583            return ToolApprovalDecision::RequiresHostApproval;
584        }
585        ToolApprovalDecision::AutoApproved
586    }
587
588    /// Merge two approval policies, taking the most restrictive combination.
589    /// - auto_approve: only tools approved by BOTH policies stay approved
590    ///   (if either policy has no patterns, the other's patterns are used)
591    /// - auto_deny / require_approval: union (either policy can deny/gate)
592    /// - write_path_allowlist: intersection (both must allow the path)
593    pub fn intersect(&self, other: &ToolApprovalPolicy) -> ToolApprovalPolicy {
594        let auto_approve = if self.auto_approve.is_empty() {
595            other.auto_approve.clone()
596        } else if other.auto_approve.is_empty() {
597            self.auto_approve.clone()
598        } else {
599            self.auto_approve
600                .iter()
601                .filter(|p| other.auto_approve.contains(p))
602                .cloned()
603                .collect()
604        };
605        let mut auto_deny = self.auto_deny.clone();
606        auto_deny.extend(other.auto_deny.iter().cloned());
607        let mut require_approval = self.require_approval.clone();
608        require_approval.extend(other.require_approval.iter().cloned());
609        let write_path_allowlist = if self.write_path_allowlist.is_empty() {
610            other.write_path_allowlist.clone()
611        } else if other.write_path_allowlist.is_empty() {
612            self.write_path_allowlist.clone()
613        } else {
614            self.write_path_allowlist
615                .iter()
616                .filter(|p| other.write_path_allowlist.contains(p))
617                .cloned()
618                .collect()
619        };
620        let mut rules = self.rules.clone();
621        rules.extend(other.rules.iter().cloned());
622        let mut sensitive_path_patterns = self.sensitive_path_patterns.clone();
623        sensitive_path_patterns.extend(other.sensitive_path_patterns.iter().cloned());
624        sensitive_path_patterns.sort();
625        sensitive_path_patterns.dedup();
626        let external_roots = if self.external_roots.is_empty() {
627            other.external_roots.clone()
628        } else if other.external_roots.is_empty() {
629            self.external_roots.clone()
630        } else {
631            self.external_roots
632                .iter()
633                .filter(|root| other.external_roots.contains(root))
634                .cloned()
635                .collect()
636        };
637        ToolApprovalPolicy {
638            rules,
639            auto_approve,
640            auto_deny,
641            require_approval,
642            write_path_allowlist,
643            allow_sensitive_paths: self.allow_sensitive_paths && other.allow_sensitive_paths,
644            sensitive_path_patterns,
645            allow_external_paths: self.allow_external_paths && other.allow_external_paths,
646            external_roots,
647            repeat_limit: match (self.repeat_limit, other.repeat_limit) {
648                (Some(left), Some(right)) => Some(left.min(right)),
649                (Some(left), None) => Some(left),
650                (None, Some(right)) => Some(right),
651                (None, None) => None,
652            },
653            repeat_action: match (self.repeat_action, other.repeat_action) {
654                (Some(PolicyAction::Deny), _) | (_, Some(PolicyAction::Deny)) => {
655                    Some(PolicyAction::Deny)
656                }
657                (Some(PolicyAction::Ask), _) | (_, Some(PolicyAction::Ask)) => {
658                    Some(PolicyAction::Ask)
659                }
660                (Some(PolicyAction::Allow), Some(PolicyAction::Allow)) => Some(PolicyAction::Allow),
661                (Some(action), None) | (None, Some(action)) => Some(action),
662                (None, None) => None,
663            },
664        }
665    }
666}
667
668#[cfg(test)]
669mod approval_policy_tests {
670    use super::*;
671    use crate::orchestration::{pop_execution_policy, push_execution_policy, CapabilityPolicy};
672    use crate::tool_annotations::{ToolAnnotations, ToolArgSchema, ToolKind};
673
674    #[test]
675    fn auto_deny_takes_precedence_over_auto_approve() {
676        let policy = ToolApprovalPolicy {
677            auto_approve: vec!["*".to_string()],
678            auto_deny: vec!["dangerous_*".to_string()],
679            ..Default::default()
680        };
681        assert_eq!(
682            policy.evaluate("dangerous_rm", &serde_json::json!({})),
683            ToolApprovalDecision::AutoDenied {
684                reason: "tool 'dangerous_rm' matches deny pattern 'dangerous_*'".to_string()
685            }
686        );
687    }
688
689    #[test]
690    fn auto_approve_matches_glob() {
691        let policy = ToolApprovalPolicy {
692            auto_approve: vec!["read*".to_string(), "search*".to_string()],
693            ..Default::default()
694        };
695        assert_eq!(
696            policy.evaluate("read_file", &serde_json::json!({})),
697            ToolApprovalDecision::AutoApproved
698        );
699        assert_eq!(
700            policy.evaluate("search", &serde_json::json!({})),
701            ToolApprovalDecision::AutoApproved
702        );
703    }
704
705    #[test]
706    fn require_approval_emits_decision() {
707        let policy = ToolApprovalPolicy {
708            require_approval: vec!["edit*".to_string()],
709            ..Default::default()
710        };
711        let decision = policy.evaluate("edit_file", &serde_json::json!({"path": "foo.rs"}));
712        assert!(matches!(
713            decision,
714            ToolApprovalDecision::RequiresHostApproval
715        ));
716    }
717
718    #[test]
719    fn unmatched_tool_defaults_to_approved() {
720        let policy = ToolApprovalPolicy {
721            auto_approve: vec!["read*".to_string()],
722            require_approval: vec!["edit*".to_string()],
723            ..Default::default()
724        };
725        assert_eq!(
726            policy.evaluate("unknown_tool", &serde_json::json!({})),
727            ToolApprovalDecision::AutoApproved
728        );
729    }
730
731    #[test]
732    fn intersect_merges_deny_lists() {
733        let a = ToolApprovalPolicy {
734            auto_deny: vec!["rm*".to_string()],
735            ..Default::default()
736        };
737        let b = ToolApprovalPolicy {
738            auto_deny: vec!["drop*".to_string()],
739            ..Default::default()
740        };
741        let merged = a.intersect(&b);
742        assert_eq!(merged.auto_deny.len(), 2);
743    }
744
745    #[test]
746    fn intersect_restricts_auto_approve_to_common_patterns() {
747        let a = ToolApprovalPolicy {
748            auto_approve: vec!["read*".to_string(), "search*".to_string()],
749            ..Default::default()
750        };
751        let b = ToolApprovalPolicy {
752            auto_approve: vec!["read*".to_string(), "write*".to_string()],
753            ..Default::default()
754        };
755        let merged = a.intersect(&b);
756        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
757    }
758
759    #[test]
760    fn intersect_defers_auto_approve_when_one_side_empty() {
761        let a = ToolApprovalPolicy {
762            auto_approve: vec!["read*".to_string()],
763            ..Default::default()
764        };
765        let b = ToolApprovalPolicy::default();
766        let merged = a.intersect(&b);
767        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
768    }
769
770    #[test]
771    fn write_path_allowlist_matches_recovered_workspace_relative_path() {
772        let temp = tempfile::tempdir().unwrap();
773        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
774        std::fs::write(temp.path().join("packages/demo/file.txt"), "ok").unwrap();
775        crate::stdlib::process::set_thread_execution_context(Some(
776            crate::orchestration::RunExecutionRecord {
777                cwd: Some(temp.path().to_string_lossy().into_owned()),
778                source_dir: Some(temp.path().to_string_lossy().into_owned()),
779                env: BTreeMap::new(),
780                adapter: None,
781                repo_path: None,
782                worktree_path: None,
783                branch: None,
784                base_ref: None,
785                cleanup: None,
786            },
787        ));
788
789        let mut tool_annotations = BTreeMap::new();
790        tool_annotations.insert(
791            "write_file".to_string(),
792            ToolAnnotations {
793                kind: ToolKind::Edit,
794                arg_schema: ToolArgSchema {
795                    path_params: vec!["path".to_string()],
796                    ..Default::default()
797                },
798                ..Default::default()
799            },
800        );
801        push_execution_policy(CapabilityPolicy {
802            tool_annotations,
803            ..Default::default()
804        });
805
806        let policy = ToolApprovalPolicy {
807            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
808            ..Default::default()
809        };
810        let decision = policy.evaluate(
811            "write_file",
812            &serde_json::json!({"path": "/packages/demo/file.txt"}),
813        );
814        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
815
816        pop_execution_policy();
817        crate::stdlib::process::set_thread_execution_context(None);
818    }
819
820    #[test]
821    fn write_path_allowlist_does_not_block_read_only_tools() {
822        let temp = tempfile::tempdir().unwrap();
823        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
824        std::fs::write(temp.path().join("packages/demo/context.txt"), "ok").unwrap();
825        crate::stdlib::process::set_thread_execution_context(Some(
826            crate::orchestration::RunExecutionRecord {
827                cwd: Some(temp.path().to_string_lossy().into_owned()),
828                source_dir: Some(temp.path().to_string_lossy().into_owned()),
829                env: BTreeMap::new(),
830                adapter: None,
831                repo_path: None,
832                worktree_path: None,
833                branch: None,
834                base_ref: None,
835                cleanup: None,
836            },
837        ));
838
839        let mut tool_annotations = BTreeMap::new();
840        tool_annotations.insert(
841            "read_file".to_string(),
842            ToolAnnotations {
843                kind: ToolKind::Read,
844                arg_schema: ToolArgSchema {
845                    path_params: vec!["path".to_string()],
846                    ..Default::default()
847                },
848                ..Default::default()
849            },
850        );
851        push_execution_policy(CapabilityPolicy {
852            tool_annotations,
853            ..Default::default()
854        });
855
856        let policy = ToolApprovalPolicy {
857            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
858            ..Default::default()
859        };
860        let decision = policy.evaluate(
861            "read_file",
862            &serde_json::json!({"path": "/packages/demo/context.txt"}),
863        );
864        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
865
866        pop_execution_policy();
867        crate::stdlib::process::set_thread_execution_context(None);
868    }
869}
870
871#[cfg(test)]
872mod turn_policy_tests {
873    use super::TurnPolicy;
874
875    #[test]
876    fn default_allows_done_sentinel() {
877        let policy = TurnPolicy::default();
878        assert!(policy.allow_done_sentinel);
879        assert!(!policy.require_action_or_yield);
880        assert!(policy.max_prose_chars.is_none());
881    }
882
883    #[test]
884    fn deserializing_partial_dict_preserves_done_sentinel_pathway() {
885        // Pre-existing workflows passed `turn_policy: { require_action_or_yield: true }`
886        // without knowing about `allow_done_sentinel`. Deserializing such a dict
887        // must keep the done-sentinel pathway enabled so loop-until-done agents
888        // don't lose their completion signal.
889        let policy: TurnPolicy =
890            serde_json::from_value(serde_json::json!({ "require_action_or_yield": true }))
891                .expect("deserialize");
892        assert!(policy.require_action_or_yield);
893        assert!(policy.allow_done_sentinel);
894    }
895
896    #[test]
897    fn deserializing_explicit_false_disables_done_sentinel() {
898        let policy: TurnPolicy = serde_json::from_value(serde_json::json!({
899            "require_action_or_yield": true,
900            "allow_done_sentinel": false,
901        }))
902        .expect("deserialize");
903        assert!(policy.require_action_or_yield);
904        assert!(!policy.allow_done_sentinel);
905    }
906}
907
908#[cfg(test)]
909mod visibility_redaction_tests {
910    use super::*;
911    use crate::value::VmValue;
912
913    fn mock_transcript() -> VmValue {
914        let messages = vec![
915            serde_json::json!({"role": "user", "content": "hi"}),
916            serde_json::json!({"role": "assistant", "content": "hello"}),
917            serde_json::json!({"role": "tool_result", "content": "internal tool output"}),
918        ];
919        crate::llm::helpers::transcript_to_vm_with_events(
920            Some("test-id".to_string()),
921            None,
922            None,
923            &messages,
924            Vec::new(),
925            Vec::new(),
926            Some("active"),
927        )
928    }
929
930    fn message_count(transcript: &VmValue) -> usize {
931        transcript
932            .as_dict()
933            .and_then(|d| d.get("messages"))
934            .and_then(|v| match v {
935                VmValue::List(list) => Some(list.len()),
936                _ => None,
937            })
938            .unwrap_or(0)
939    }
940
941    #[test]
942    fn visibility_none_returns_unchanged() {
943        let t = mock_transcript();
944        let result = redact_transcript_visibility(&t, None).unwrap();
945        assert_eq!(message_count(&result), 3);
946    }
947
948    #[test]
949    fn visibility_public_drops_tool_results() {
950        let t = mock_transcript();
951        let result = redact_transcript_visibility(&t, Some("public")).unwrap();
952        assert_eq!(message_count(&result), 2);
953    }
954
955    #[test]
956    fn visibility_unknown_string_is_pass_through() {
957        let t = mock_transcript();
958        let result = redact_transcript_visibility(&t, Some("internal")).unwrap();
959        assert_eq!(message_count(&result), 3);
960    }
961}