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