Skip to main content

harn_vm/orchestration/policy/
mod.rs

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