Skip to main content

harn_vm/orchestration/policy/
mod.rs

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