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"
314        | "http_post"
315        | "http_put"
316        | "http_patch"
317        | "http_delete"
318        | "http_download"
319        | "http_request"
320        | "unix_socket_json_request"
321        | "__net_unix_socket_json_request"
322            if !policy_allows_side_effect(&policy, "network") =>
323        {
324            return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
325        }
326        "__files_upload"
327            if !policy_allows_capability(&policy, "workspace", "read_text")
328                || !policy_allows_side_effect(&policy, "network") =>
329        {
330            return reject_policy(
331                "builtin '__files_upload' exceeds workspace.read_text/network ceiling".to_string(),
332            );
333        }
334        "http_session_request"
335        | "http_stream_open"
336        | "http_stream_read"
337        | "http_stream_close"
338        | "http_stream_info"
339        | "sse_connect"
340        | "sse_receive"
341        | "websocket_accept"
342        | "websocket_connect"
343        | "websocket_route"
344        | "websocket_send"
345        | "websocket_receive"
346        | "websocket_server"
347            if !policy_allows_side_effect(&policy, "network") =>
348        {
349            return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
350        }
351        "llm_call" | "llm_call_safe" | "llm_completion" | "llm_stream" | "llm_stream_call"
352        | "llm_healthcheck" | "agent_loop"
353            if !policy_allows_capability(&policy, "llm", "call") =>
354        {
355            return reject_policy(format!("builtin '{name}' exceeds llm.call ceiling"));
356        }
357        "connector_call"
358            if !policy_allows_capability(&policy, "connector", "call")
359                || !policy_allows_side_effect(&policy, "network") =>
360        {
361            return reject_policy(
362                "builtin 'connector_call' exceeds connector.call/network ceiling".to_string(),
363            );
364        }
365        "secret_get" if !policy_allows_capability(&policy, "connector", "secret_get") => {
366            return reject_policy(
367                "builtin 'secret_get' exceeds connector.secret_get ceiling".to_string(),
368            );
369        }
370        "event_log_emit" if !policy_allows_capability(&policy, "connector", "event_log_emit") => {
371            return reject_policy(
372                "builtin 'event_log_emit' exceeds connector.event_log_emit ceiling".to_string(),
373            );
374        }
375        "metrics_inc" if !policy_allows_capability(&policy, "connector", "metrics_inc") => {
376            return reject_policy(
377                "builtin 'metrics_inc' exceeds connector.metrics_inc ceiling".to_string(),
378            );
379        }
380        "project_fingerprint"
381        | "project_context_profile_native"
382        | "project_scan_native"
383        | "project_scan_tree_native"
384        | "project_walk_tree_native"
385        | "project_catalog_native"
386            if !policy_allows_capability(&policy, "workspace", "list")
387                || !policy_allows_side_effect(&policy, "read_only") =>
388        {
389            return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
390        }
391        "__agent_state_init"
392        | "__agent_state_resume"
393        | "__agent_state_write"
394        | "__agent_state_read"
395        | "__agent_state_list"
396        | "__agent_state_delete"
397        | "__agent_state_handoff"
398            if !policy_allows_capability(&policy, "agent_state", "access") =>
399        {
400            return reject_policy(format!(
401                "builtin '{name}' exceeds agent_state.access ceiling"
402            ));
403        }
404        "vision_ocr"
405            if !policy_allows_capability(&policy, "vision", "ocr")
406                || !policy_allows_side_effect(&policy, "process_exec") =>
407        {
408            return reject_policy(format!(
409                "builtin '{name}' exceeds vision.ocr/process ceiling"
410            ));
411        }
412        "mcp_connect"
413        | "mcp_ensure_active"
414        | "mcp_call"
415        | "mcp_list_tools"
416        | "mcp_list_resources"
417        | "mcp_list_resource_templates"
418        | "mcp_read_resource"
419        | "mcp_list_prompts"
420        | "mcp_get_prompt"
421        | "mcp_server_info"
422        | "mcp_disconnect"
423            if !policy_allows_capability(&policy, "process", "exec")
424                || !policy_allows_side_effect(&policy, "process_exec") =>
425        {
426            return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
427        }
428        "host_call" => {
429            let name = args.first().map(|v| v.display()).unwrap_or_default();
430            let Some((capability, op)) = name.split_once('.') else {
431                return reject_policy(format!(
432                    "host_call '{name}' must use capability.operation naming"
433                ));
434            };
435            if !policy_allows_capability(&policy, capability, op) {
436                return reject_policy(format!(
437                    "host_call {capability}.{op} exceeds capability ceiling"
438                ));
439            }
440            let requested_side_effect = match (capability, op) {
441                ("workspace", "write_text" | "apply_edit" | "delete") => "workspace_write",
442                ("process", "exec") => "process_exec",
443                _ => "read_only",
444            };
445            if !policy_allows_side_effect(&policy, requested_side_effect) {
446                return reject_policy(format!(
447                    "host_call {capability}.{op} exceeds side-effect ceiling"
448                ));
449            }
450        }
451        "host_tool_list" | "host_tool_call"
452            if !policy_allows_capability(&policy, "host", "tool_call") =>
453        {
454            return reject_policy(format!("builtin '{name}' exceeds host.tool_call ceiling"));
455        }
456        _ => {}
457    }
458    Ok(())
459}
460
461pub fn enforce_current_policy_for_bridge_builtin(name: &str) -> Result<(), VmError> {
462    let trusted = TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow() > 0);
463    if trusted {
464        return Ok(());
465    }
466    if current_execution_policy().is_some() {
467        return reject_policy(format!(
468            "bridged builtin '{name}' exceeds execution policy; declare an explicit capability/tool surface instead"
469        ));
470    }
471    Ok(())
472}
473
474pub fn enforce_current_policy_for_tool(tool_name: &str) -> Result<(), PolicyDenial> {
475    use crate::agent_events::DenialGate;
476    let Some(policy) = current_execution_policy() else {
477        return Ok(());
478    };
479    if !policy_allows_tool(&policy, tool_name) {
480        return reject_tool(
481            DenialGate::ToolCeiling,
482            None,
483            format!("tool '{tool_name}' exceeds tool ceiling"),
484        );
485    }
486    if let Some(annotations) = policy.tool_annotations.get(tool_name) {
487        for (capability, ops) in &annotations.capabilities {
488            for op in ops {
489                if !policy_allows_capability(&policy, capability, op) {
490                    return reject_tool(
491                        DenialGate::CapabilityCeiling,
492                        Some(format!("{capability}.{op}")),
493                        format!("tool '{tool_name}' exceeds capability ceiling: {capability}.{op}"),
494                    );
495                }
496            }
497        }
498        let requested_level = annotations.side_effect_level;
499        if requested_level != SideEffectLevel::None
500            && !policy_allows_side_effect(&policy, requested_level.as_str())
501        {
502            return reject_tool(
503                DenialGate::SideEffectCeiling,
504                None,
505                format!(
506                    "tool '{tool_name}' exceeds side-effect ceiling: {}",
507                    requested_level.as_str()
508                ),
509            );
510        }
511    }
512    Ok(())
513}
514
515// ── Output visibility redaction ─────────────────────────────────────
516//
517// Transcript lifecycle (reset, fork, trim, compact) now lives on
518// `crate::agent_sessions` as explicit imperative builtins. All that
519// remains here is the per-call visibility filter, which is
520// output-shaping (not lifecycle).
521
522/// Filter a transcript dict down to the caller-visible subset, based
523/// on the `output_visibility` node option. `None` or any unknown
524/// visibility returns the transcript unchanged — callers are expected
525/// to validate the string against a known set upstream.
526pub fn redact_transcript_visibility(
527    transcript: &VmValue,
528    visibility: Option<&str>,
529) -> Option<VmValue> {
530    let Some(visibility) = visibility else {
531        return Some(transcript.clone());
532    };
533    if visibility != "public" && visibility != "public_only" {
534        return Some(transcript.clone());
535    }
536    let dict = transcript.as_dict()?;
537    let public_messages = match dict.get("messages") {
538        Some(VmValue::List(list)) => list
539            .iter()
540            .filter_map(redact_public_message)
541            .collect::<Vec<_>>(),
542        _ => Vec::new(),
543    };
544    let public_events = match dict.get("events") {
545        Some(VmValue::List(list)) => list
546            .iter()
547            .filter(|event| {
548                event
549                    .as_dict()
550                    .and_then(|d| d.get("visibility"))
551                    .map(|v| v.display())
552                    .map(|value| value == "public")
553                    .unwrap_or(true)
554            })
555            .cloned()
556            .collect::<Vec<_>>(),
557        _ => Vec::new(),
558    };
559    let mut redacted = dict.clone();
560    redacted.insert(
561        "messages".to_string(),
562        VmValue::List(std::sync::Arc::new(public_messages)),
563    );
564    redacted.insert(
565        "events".to_string(),
566        VmValue::List(std::sync::Arc::new(public_events)),
567    );
568    Some(VmValue::Dict(std::sync::Arc::new(redacted)))
569}
570
571fn redact_public_message(message: &VmValue) -> Option<VmValue> {
572    let Some(dict) = message.as_dict() else {
573        return Some(message.clone());
574    };
575    if dict.get("role").map(|value| value.display()).as_deref() == Some("tool_result") {
576        return None;
577    }
578    if dict
579        .get("visibility")
580        .map(|value| value.display())
581        .is_some_and(|visibility| visibility != "public")
582    {
583        return None;
584    }
585
586    let mut redacted = dict.clone();
587    let mut saw_structured_blocks = false;
588    let mut public_text = Vec::new();
589    for key in ["content", "blocks"] {
590        if let Some(VmValue::List(blocks)) = dict.get(key) {
591            saw_structured_blocks = true;
592            let public_blocks = blocks
593                .iter()
594                .filter_map(redact_public_block)
595                .collect::<Vec<_>>();
596            if key == "blocks" || public_text.is_empty() {
597                public_text = text_fragments_from_blocks(&public_blocks);
598            }
599            redacted.insert(
600                key.to_string(),
601                VmValue::List(std::sync::Arc::new(public_blocks)),
602            );
603        }
604    }
605    if saw_structured_blocks {
606        if public_text.is_empty() {
607            redacted.remove("text");
608        } else {
609            redacted.insert(
610                "text".to_string(),
611                VmValue::String(std::sync::Arc::from(public_text.join("\n"))),
612            );
613        }
614    }
615    Some(VmValue::Dict(std::sync::Arc::new(redacted)))
616}
617
618fn redact_public_block(block: &VmValue) -> Option<VmValue> {
619    let Some(dict) = block.as_dict() else {
620        return Some(block.clone());
621    };
622    if dict
623        .get("visibility")
624        .map(|value| value.display())
625        .is_some_and(|visibility| visibility != "public")
626    {
627        return None;
628    }
629    Some(block.clone())
630}
631
632fn text_fragments_from_blocks(blocks: &[VmValue]) -> Vec<String> {
633    blocks
634        .iter()
635        .filter_map(|block| block.as_dict())
636        .filter_map(|dict| dict.get("text"))
637        .filter_map(|text| match text {
638            VmValue::String(value) if !value.is_empty() => Some(value.to_string()),
639            _ => None,
640        })
641        .collect()
642}
643
644pub fn builtin_ceiling() -> CapabilityPolicy {
645    CapabilityPolicy {
646        // `capabilities` is intentionally empty: the host capability manifest
647        // is the sole authority, and an allowlist here would silently block
648        // any capability the host adds later.
649        tools: Vec::new(),
650        capabilities: BTreeMap::new(),
651        workspace_roots: Vec::new(),
652        read_only_roots: Vec::new(),
653        side_effect_level: Some("network".to_string()),
654        recursion_limit: Some(RuntimeLimits::DEFAULT.max_nested_execution_depth),
655        tool_arg_constraints: Vec::new(),
656        tool_annotations: BTreeMap::new(),
657        sandbox_profile: SandboxProfile::Worktree,
658        process_sandbox: Default::default(),
659    }
660}
661
662/// Declarative policy for tool approval gating. Allows pipelines to
663/// specify which tools are auto-approved, auto-denied, or require
664/// host confirmation, plus write-path allowlists.
665#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
666#[serde(default)]
667pub struct ToolApprovalPolicy {
668    /// Ordered allow/ask/deny rules over tool metadata, path, command,
669    /// URL, MCP, agent/persona/mode, and repeat-count dimensions.
670    #[serde(default)]
671    pub rules: Vec<PolicyRule>,
672    /// Glob patterns for tools that should be auto-approved.
673    #[serde(default)]
674    pub auto_approve: Vec<String>,
675    /// Glob patterns for tools that should always be denied.
676    #[serde(default)]
677    pub auto_deny: Vec<String>,
678    /// Glob patterns for tools that require host confirmation.
679    #[serde(default)]
680    pub require_approval: Vec<String>,
681    /// Glob patterns for writable paths.
682    #[serde(default)]
683    pub write_path_allowlist: Vec<String>,
684    /// Explicit opt-out for the deny-by-default sensitive-path guard.
685    #[serde(default)]
686    pub allow_sensitive_paths: bool,
687    /// Additional or replacement sensitive path globs. Empty uses the
688    /// runtime defaults such as `.env`, private keys, and credential files.
689    #[serde(default)]
690    pub sensitive_path_patterns: Vec<String>,
691    /// Explicit opt-out for the external-path guard on declared path args.
692    #[serde(default)]
693    pub allow_external_paths: bool,
694    /// Host-absolute roots allowed when `allow_external_paths` is false.
695    #[serde(default)]
696    pub external_roots: Vec<String>,
697    /// Optional repeated-call threshold for the same `(session, tool, args)`.
698    #[serde(default, alias = "repeated_call_limit")]
699    pub repeat_limit: Option<u64>,
700    /// Action for `repeat_limit`; defaults to `ask`.
701    #[serde(default, alias = "repeated_call_action")]
702    pub repeat_action: Option<PolicyAction>,
703}
704
705/// Result of evaluating a tool call against a ToolApprovalPolicy.
706#[derive(Debug, Clone, PartialEq, Eq)]
707pub enum ToolApprovalDecision {
708    /// Tool is auto-approved by policy.
709    AutoApproved,
710    /// Tool is auto-denied by policy.
711    AutoDenied { reason: String },
712    /// Tool requires explicit host approval; the caller already owns the
713    /// tool name and args and forwards them to the host bridge.
714    RequiresHostApproval,
715}
716
717impl ToolApprovalPolicy {
718    pub fn evaluate_detailed(&self, tool_name: &str, args: &serde_json::Value) -> PolicyEvaluation {
719        approval_rules::evaluate_tool_approval_policy(self, tool_name, args, None)
720    }
721
722    pub fn evaluate_detailed_with_repeat(
723        &self,
724        tool_name: &str,
725        args: &serde_json::Value,
726        repeat_count: u64,
727    ) -> PolicyEvaluation {
728        approval_rules::evaluate_tool_approval_policy(self, tool_name, args, Some(repeat_count))
729    }
730
731    /// Evaluate whether a tool call should be approved, denied, or needs
732    /// host confirmation.
733    pub fn evaluate(&self, tool_name: &str, args: &serde_json::Value) -> ToolApprovalDecision {
734        let decision = self.evaluate_detailed(tool_name, args);
735        if decision.is_deny() {
736            return ToolApprovalDecision::AutoDenied {
737                reason: decision.reason,
738            };
739        }
740        if decision.is_ask() {
741            return ToolApprovalDecision::RequiresHostApproval;
742        }
743        ToolApprovalDecision::AutoApproved
744    }
745
746    /// Merge two approval policies, taking the most restrictive combination.
747    /// - auto_approve: only tools approved by BOTH policies stay approved
748    ///   (if either policy has no patterns, the other's patterns are used)
749    /// - auto_deny / require_approval: union (either policy can deny/gate)
750    /// - write_path_allowlist: intersection (both must allow the path)
751    pub fn intersect(&self, other: &ToolApprovalPolicy) -> ToolApprovalPolicy {
752        let auto_approve = if self.auto_approve.is_empty() {
753            other.auto_approve.clone()
754        } else if other.auto_approve.is_empty() {
755            self.auto_approve.clone()
756        } else {
757            self.auto_approve
758                .iter()
759                .filter(|p| other.auto_approve.contains(p))
760                .cloned()
761                .collect()
762        };
763        let mut auto_deny = self.auto_deny.clone();
764        auto_deny.extend(other.auto_deny.iter().cloned());
765        let mut require_approval = self.require_approval.clone();
766        require_approval.extend(other.require_approval.iter().cloned());
767        let write_path_allowlist = if self.write_path_allowlist.is_empty() {
768            other.write_path_allowlist.clone()
769        } else if other.write_path_allowlist.is_empty() {
770            self.write_path_allowlist.clone()
771        } else {
772            self.write_path_allowlist
773                .iter()
774                .filter(|p| other.write_path_allowlist.contains(p))
775                .cloned()
776                .collect()
777        };
778        let mut rules = self.rules.clone();
779        rules.extend(other.rules.iter().cloned());
780        let mut sensitive_path_patterns = self.sensitive_path_patterns.clone();
781        sensitive_path_patterns.extend(other.sensitive_path_patterns.iter().cloned());
782        sensitive_path_patterns.sort();
783        sensitive_path_patterns.dedup();
784        let external_roots = if self.external_roots.is_empty() {
785            other.external_roots.clone()
786        } else if other.external_roots.is_empty() {
787            self.external_roots.clone()
788        } else {
789            self.external_roots
790                .iter()
791                .filter(|root| other.external_roots.contains(root))
792                .cloned()
793                .collect()
794        };
795        ToolApprovalPolicy {
796            rules,
797            auto_approve,
798            auto_deny,
799            require_approval,
800            write_path_allowlist,
801            allow_sensitive_paths: self.allow_sensitive_paths && other.allow_sensitive_paths,
802            sensitive_path_patterns,
803            allow_external_paths: self.allow_external_paths && other.allow_external_paths,
804            external_roots,
805            repeat_limit: match (self.repeat_limit, other.repeat_limit) {
806                (Some(left), Some(right)) => Some(left.min(right)),
807                (Some(left), None) => Some(left),
808                (None, Some(right)) => Some(right),
809                (None, None) => None,
810            },
811            repeat_action: match (self.repeat_action, other.repeat_action) {
812                (Some(PolicyAction::Deny), _) | (_, Some(PolicyAction::Deny)) => {
813                    Some(PolicyAction::Deny)
814                }
815                (Some(PolicyAction::Ask), _) | (_, Some(PolicyAction::Ask)) => {
816                    Some(PolicyAction::Ask)
817                }
818                (Some(PolicyAction::Allow), Some(PolicyAction::Allow)) => Some(PolicyAction::Allow),
819                (Some(action), None) | (None, Some(action)) => Some(action),
820                (None, None) => None,
821            },
822        }
823    }
824}
825
826#[cfg(test)]
827mod approval_policy_tests {
828    use super::*;
829    use crate::orchestration::{pop_execution_policy, push_execution_policy, CapabilityPolicy};
830    use crate::tool_annotations::{ToolAnnotations, ToolArgSchema, ToolKind};
831
832    #[test]
833    fn auto_deny_takes_precedence_over_auto_approve() {
834        let policy = ToolApprovalPolicy {
835            auto_approve: vec!["*".to_string()],
836            auto_deny: vec!["dangerous_*".to_string()],
837            ..Default::default()
838        };
839        assert_eq!(
840            policy.evaluate("dangerous_rm", &serde_json::json!({})),
841            ToolApprovalDecision::AutoDenied {
842                reason: "tool 'dangerous_rm' matches deny pattern 'dangerous_*'".to_string()
843            }
844        );
845    }
846
847    #[test]
848    fn auto_approve_matches_glob() {
849        let policy = ToolApprovalPolicy {
850            auto_approve: vec!["read*".to_string(), "search*".to_string()],
851            ..Default::default()
852        };
853        assert_eq!(
854            policy.evaluate("read_file", &serde_json::json!({})),
855            ToolApprovalDecision::AutoApproved
856        );
857        assert_eq!(
858            policy.evaluate("search", &serde_json::json!({})),
859            ToolApprovalDecision::AutoApproved
860        );
861    }
862
863    #[test]
864    fn require_approval_emits_decision() {
865        let policy = ToolApprovalPolicy {
866            require_approval: vec!["edit*".to_string()],
867            ..Default::default()
868        };
869        let decision = policy.evaluate("edit_file", &serde_json::json!({"path": "foo.rs"}));
870        assert!(matches!(
871            decision,
872            ToolApprovalDecision::RequiresHostApproval
873        ));
874    }
875
876    #[test]
877    fn unmatched_tool_defaults_to_approved() {
878        let policy = ToolApprovalPolicy {
879            auto_approve: vec!["read*".to_string()],
880            require_approval: vec!["edit*".to_string()],
881            ..Default::default()
882        };
883        assert_eq!(
884            policy.evaluate("unknown_tool", &serde_json::json!({})),
885            ToolApprovalDecision::AutoApproved
886        );
887    }
888
889    #[test]
890    fn intersect_merges_deny_lists() {
891        let a = ToolApprovalPolicy {
892            auto_deny: vec!["rm*".to_string()],
893            ..Default::default()
894        };
895        let b = ToolApprovalPolicy {
896            auto_deny: vec!["drop*".to_string()],
897            ..Default::default()
898        };
899        let merged = a.intersect(&b);
900        assert_eq!(merged.auto_deny.len(), 2);
901    }
902
903    #[test]
904    fn intersect_restricts_auto_approve_to_common_patterns() {
905        let a = ToolApprovalPolicy {
906            auto_approve: vec!["read*".to_string(), "search*".to_string()],
907            ..Default::default()
908        };
909        let b = ToolApprovalPolicy {
910            auto_approve: vec!["read*".to_string(), "write*".to_string()],
911            ..Default::default()
912        };
913        let merged = a.intersect(&b);
914        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
915    }
916
917    #[test]
918    fn intersect_defers_auto_approve_when_one_side_empty() {
919        let a = ToolApprovalPolicy {
920            auto_approve: vec!["read*".to_string()],
921            ..Default::default()
922        };
923        let b = ToolApprovalPolicy::default();
924        let merged = a.intersect(&b);
925        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
926    }
927
928    #[test]
929    fn write_path_allowlist_matches_recovered_workspace_relative_path() {
930        let temp = tempfile::tempdir().unwrap();
931        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
932        std::fs::write(temp.path().join("packages/demo/file.txt"), "ok").unwrap();
933        crate::stdlib::process::set_thread_execution_context(Some(
934            crate::orchestration::RunExecutionRecord {
935                cwd: Some(temp.path().to_string_lossy().into_owned()),
936                source_dir: Some(temp.path().to_string_lossy().into_owned()),
937                env: BTreeMap::new(),
938                adapter: None,
939                repo_path: None,
940                worktree_path: None,
941                branch: None,
942                base_ref: None,
943                cleanup: None,
944            },
945        ));
946
947        let mut tool_annotations = BTreeMap::new();
948        tool_annotations.insert(
949            "write_file".to_string(),
950            ToolAnnotations {
951                kind: ToolKind::Edit,
952                arg_schema: ToolArgSchema {
953                    path_params: vec!["path".to_string()],
954                    ..Default::default()
955                },
956                ..Default::default()
957            },
958        );
959        push_execution_policy(CapabilityPolicy {
960            tool_annotations,
961            ..Default::default()
962        });
963
964        let policy = ToolApprovalPolicy {
965            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
966            ..Default::default()
967        };
968        let decision = policy.evaluate(
969            "write_file",
970            &serde_json::json!({"path": "/packages/demo/file.txt"}),
971        );
972        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
973
974        pop_execution_policy();
975        crate::stdlib::process::set_thread_execution_context(None);
976    }
977
978    #[test]
979    fn write_path_allowlist_does_not_block_read_only_tools() {
980        let temp = tempfile::tempdir().unwrap();
981        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
982        std::fs::write(temp.path().join("packages/demo/context.txt"), "ok").unwrap();
983        crate::stdlib::process::set_thread_execution_context(Some(
984            crate::orchestration::RunExecutionRecord {
985                cwd: Some(temp.path().to_string_lossy().into_owned()),
986                source_dir: Some(temp.path().to_string_lossy().into_owned()),
987                env: BTreeMap::new(),
988                adapter: None,
989                repo_path: None,
990                worktree_path: None,
991                branch: None,
992                base_ref: None,
993                cleanup: None,
994            },
995        ));
996
997        let mut tool_annotations = BTreeMap::new();
998        tool_annotations.insert(
999            "read_file".to_string(),
1000            ToolAnnotations {
1001                kind: ToolKind::Read,
1002                arg_schema: ToolArgSchema {
1003                    path_params: vec!["path".to_string()],
1004                    ..Default::default()
1005                },
1006                ..Default::default()
1007            },
1008        );
1009        push_execution_policy(CapabilityPolicy {
1010            tool_annotations,
1011            ..Default::default()
1012        });
1013
1014        let policy = ToolApprovalPolicy {
1015            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
1016            ..Default::default()
1017        };
1018        let decision = policy.evaluate(
1019            "read_file",
1020            &serde_json::json!({"path": "/packages/demo/context.txt"}),
1021        );
1022        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
1023
1024        pop_execution_policy();
1025        crate::stdlib::process::set_thread_execution_context(None);
1026    }
1027
1028    #[test]
1029    fn builtin_policy_covers_fs_read_and_list_helpers() {
1030        clear_execution_policy_stacks();
1031        push_execution_policy(CapabilityPolicy {
1032            capabilities: BTreeMap::from([("workspace".to_string(), vec!["exists".to_string()])]),
1033            side_effect_level: Some("read_only".to_string()),
1034            ..CapabilityPolicy::default()
1035        });
1036
1037        for name in [
1038            "read_lines",
1039            "find_text",
1040            "walk_dir",
1041            "glob",
1042            "project_context_profile_native",
1043        ] {
1044            assert!(
1045                enforce_current_policy_for_builtin(name, &[]).is_err(),
1046                "{name} should be rejected when the matching workspace capability is absent"
1047            );
1048        }
1049
1050        pop_execution_policy();
1051    }
1052
1053    #[test]
1054    fn move_file_requires_workspace_write_side_effect() {
1055        clear_execution_policy_stacks();
1056        push_execution_policy(CapabilityPolicy {
1057            capabilities: BTreeMap::from([(
1058                "workspace".to_string(),
1059                vec!["write_text".to_string()],
1060            )]),
1061            side_effect_level: Some("read_only".to_string()),
1062            ..CapabilityPolicy::default()
1063        });
1064
1065        let error = enforce_current_policy_for_builtin("move_file", &[]).unwrap_err();
1066        assert!(
1067            error.to_string().contains("workspace write ceiling"),
1068            "unexpected error: {error}"
1069        );
1070
1071        pop_execution_policy();
1072    }
1073
1074    #[test]
1075    fn unix_socket_json_request_requires_network_side_effect() {
1076        clear_execution_policy_stacks();
1077        push_execution_policy(CapabilityPolicy {
1078            side_effect_level: Some("read_only".to_string()),
1079            ..CapabilityPolicy::default()
1080        });
1081
1082        let error =
1083            enforce_current_policy_for_builtin("__net_unix_socket_json_request", &[]).unwrap_err();
1084        assert!(
1085            error.to_string().contains("network ceiling"),
1086            "unexpected error: {error}"
1087        );
1088
1089        pop_execution_policy();
1090    }
1091
1092    #[test]
1093    fn files_upload_requires_workspace_read_and_network_side_effect() {
1094        clear_execution_policy_stacks();
1095        push_execution_policy(CapabilityPolicy {
1096            capabilities: BTreeMap::from([(
1097                "workspace".to_string(),
1098                vec!["read_text".to_string()],
1099            )]),
1100            side_effect_level: Some("read_only".to_string()),
1101            ..CapabilityPolicy::default()
1102        });
1103
1104        let network_error = enforce_current_policy_for_builtin("__files_upload", &[]).unwrap_err();
1105        assert!(
1106            network_error.to_string().contains("network ceiling"),
1107            "unexpected error: {network_error}"
1108        );
1109        pop_execution_policy();
1110
1111        push_execution_policy(CapabilityPolicy {
1112            capabilities: BTreeMap::from([("workspace".to_string(), vec!["exists".to_string()])]),
1113            side_effect_level: Some("network".to_string()),
1114            ..CapabilityPolicy::default()
1115        });
1116        let read_error = enforce_current_policy_for_builtin("__files_upload", &[]).unwrap_err();
1117        assert!(
1118            read_error.to_string().contains("workspace.read_text"),
1119            "unexpected error: {read_error}"
1120        );
1121
1122        pop_execution_policy();
1123    }
1124}
1125
1126#[cfg(test)]
1127mod turn_policy_tests {
1128    use super::TurnPolicy;
1129
1130    #[test]
1131    fn default_allows_done_sentinel() {
1132        let policy = TurnPolicy::default();
1133        assert!(policy.allow_done_sentinel);
1134        assert!(!policy.require_action_or_yield);
1135        assert!(policy.max_prose_chars.is_none());
1136    }
1137
1138    #[test]
1139    fn deserializing_partial_dict_preserves_done_sentinel_pathway() {
1140        // Pre-existing workflows passed `turn_policy: { require_action_or_yield: true }`
1141        // without knowing about `allow_done_sentinel`. Deserializing such a dict
1142        // must keep the done-sentinel pathway enabled so loop-until-done agents
1143        // don't lose their completion signal.
1144        let policy: TurnPolicy =
1145            serde_json::from_value(serde_json::json!({ "require_action_or_yield": true }))
1146                .expect("deserialize");
1147        assert!(policy.require_action_or_yield);
1148        assert!(policy.allow_done_sentinel);
1149    }
1150
1151    #[test]
1152    fn deserializing_explicit_false_disables_done_sentinel() {
1153        let policy: TurnPolicy = serde_json::from_value(serde_json::json!({
1154            "require_action_or_yield": true,
1155            "allow_done_sentinel": false,
1156        }))
1157        .expect("deserialize");
1158        assert!(policy.require_action_or_yield);
1159        assert!(!policy.allow_done_sentinel);
1160    }
1161}
1162
1163#[cfg(test)]
1164mod visibility_redaction_tests {
1165    use super::*;
1166    use crate::value::VmValue;
1167
1168    fn mock_transcript() -> VmValue {
1169        let messages = vec![
1170            serde_json::json!({"role": "user", "content": "hi"}),
1171            serde_json::json!({"role": "assistant", "content": "hello"}),
1172            serde_json::json!({"role": "tool_result", "content": "internal tool output"}),
1173        ];
1174        crate::llm::helpers::transcript_to_vm_with_events(
1175            Some("test-id".to_string()),
1176            None,
1177            None,
1178            &messages,
1179            Vec::new(),
1180            Vec::new(),
1181            Some("active"),
1182        )
1183    }
1184
1185    fn message_count(transcript: &VmValue) -> usize {
1186        transcript
1187            .as_dict()
1188            .and_then(|d| d.get("messages"))
1189            .and_then(|v| match v {
1190                VmValue::List(list) => Some(list.len()),
1191                _ => None,
1192            })
1193            .unwrap_or(0)
1194    }
1195
1196    #[test]
1197    fn visibility_none_returns_unchanged() {
1198        let t = mock_transcript();
1199        let result = redact_transcript_visibility(&t, None).unwrap();
1200        assert_eq!(message_count(&result), 3);
1201    }
1202
1203    #[test]
1204    fn visibility_public_drops_tool_results() {
1205        let t = mock_transcript();
1206        let result = redact_transcript_visibility(&t, Some("public")).unwrap();
1207        assert_eq!(message_count(&result), 2);
1208    }
1209
1210    #[test]
1211    fn visibility_public_drops_private_content_blocks() {
1212        let t = crate::schema::json_to_vm_value(&serde_json::json!({
1213            "messages": [
1214                {
1215                    "role": "assistant",
1216                    "visibility": "public",
1217                    "text": "visible answer\nsecret chain",
1218                    "content": [
1219                        {"type": "output_text", "text": "visible answer", "visibility": "public"},
1220                        {"type": "reasoning", "text": "secret chain", "visibility": "private"}
1221                    ],
1222                    "blocks": [
1223                        {"type": "output_text", "text": "visible block", "visibility": "public"},
1224                        {"type": "tool_call", "text": "internal args", "visibility": "internal"}
1225                    ]
1226                }
1227            ],
1228            "events": []
1229        }));
1230
1231        let result = redact_transcript_visibility(&t, Some("public")).unwrap();
1232        let rendered = result.display();
1233        assert!(rendered.contains("visible answer"));
1234        assert!(rendered.contains("visible block"));
1235        assert!(!rendered.contains("secret chain"));
1236        assert!(!rendered.contains("internal args"));
1237    }
1238
1239    #[test]
1240    fn visibility_unknown_string_is_pass_through() {
1241        let t = mock_transcript();
1242        let result = redact_transcript_visibility(&t, Some("internal")).unwrap();
1243        assert_eq!(message_count(&result), 3);
1244    }
1245}