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