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            | "sse_server_response"
211            | "sse_server_send"
212            | "sse_server_heartbeat"
213            | "sse_server_flush"
214            | "sse_server_close"
215            | "sse_server_cancel"
216            | "sse_server_mock_receive"
217            | "sse_server_mock_disconnect"
218            | "__agent_state_write"
219            | "__agent_state_delete"
220            | "__agent_state_handoff"
221            | "mcp_release"
222    )
223}
224
225fn emit_autonomy_proposal_event(
226    tier: AutonomyTier,
227    builtin_name: &str,
228    args: &[VmValue],
229) -> Result<(), VmError> {
230    let Some(context) = current_dispatch_context() else {
231        return Ok(());
232    };
233    let Some(log) = active_event_log() else {
234        return Ok(());
235    };
236    let topic = Topic::new(crate::TRIGGER_OUTBOX_TOPIC)
237        .map_err(|error| VmError::Runtime(format!("autonomy proposal topic error: {error}")))?;
238    let mut headers = BTreeMap::new();
239    headers.insert(
240        "trace_id".to_string(),
241        context.trigger_event.trace_id.0.clone(),
242    );
243    headers.insert("agent".to_string(), context.agent_id.clone());
244    headers.insert("autonomy_tier".to_string(), tier.as_str().to_string());
245    let payload = serde_json::json!({
246        "agent": context.agent_id,
247        "action": context.action,
248        "builtin": builtin_name,
249        "args": args.iter().map(crate::llm::vm_value_to_json).collect::<Vec<_>>(),
250        "trace_id": context.trigger_event.trace_id.0,
251        "replay_of_event_id": context.replay_of_event_id,
252        "autonomy_tier": tier,
253        "proposal": true,
254    });
255    futures::executor::block_on(log.append(
256        &topic,
257        LogEvent::new("dispatch_proposed", payload).with_headers(headers),
258    ))
259    .map(|_| ())
260    .map_err(|error| VmError::Runtime(format!("failed to append autonomy proposal: {error}")))
261}
262
263fn enforce_dispatch_autonomy_for_builtin(name: &str, args: &[VmValue]) -> Result<(), VmError> {
264    let Some(context) = current_dispatch_context() else {
265        return Ok(());
266    };
267    if !builtin_mutates_state(name) {
268        return Ok(());
269    }
270    match context.autonomy_tier {
271        AutonomyTier::Shadow => {
272            emit_autonomy_proposal_event(AutonomyTier::Shadow, name, args)?;
273            Ok(())
274        }
275        AutonomyTier::Suggest => {
276            emit_autonomy_proposal_event(AutonomyTier::Suggest, name, args)?;
277            Ok(())
278        }
279        AutonomyTier::ActWithApproval | AutonomyTier::ActAuto => Ok(()),
280    }
281}
282
283pub fn enforce_current_policy_for_builtin(name: &str, args: &[VmValue]) -> Result<(), VmError> {
284    enforce_dispatch_autonomy_for_builtin(name, args)?;
285    let Some(policy) = current_execution_policy() else {
286        return Ok(());
287    };
288    match name {
289        "read_file" | "read_file_result" | "read_file_bytes"
290            if !policy_allows_capability(&policy, "workspace", "read_text") =>
291        {
292            return reject_policy(format!(
293                "builtin '{name}' exceeds workspace.read_text ceiling"
294            ));
295        }
296        "list_dir" if !policy_allows_capability(&policy, "workspace", "list") => {
297            return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
298        }
299        "file_exists" | "stat" if !policy_allows_capability(&policy, "workspace", "exists") => {
300            return reject_policy(format!("builtin '{name}' exceeds workspace.exists ceiling"));
301        }
302        "write_file" | "write_file_bytes" | "append_file" | "mkdir" | "copy_file"
303            if !policy_allows_capability(&policy, "workspace", "write_text")
304                || !policy_allows_side_effect(&policy, "workspace_write") =>
305        {
306            return reject_policy(format!("builtin '{name}' exceeds workspace write ceiling"));
307        }
308        "delete_file"
309            if !policy_allows_capability(&policy, "workspace", "delete")
310                || !policy_allows_side_effect(&policy, "workspace_write") =>
311        {
312            return reject_policy(
313                "builtin 'delete_file' exceeds workspace.delete ceiling".to_string(),
314            );
315        }
316        "apply_edit"
317            if !policy_allows_capability(&policy, "workspace", "apply_edit")
318                || !policy_allows_side_effect(&policy, "workspace_write") =>
319        {
320            return reject_policy(
321                "builtin 'apply_edit' exceeds workspace.apply_edit ceiling".to_string(),
322            );
323        }
324        "exec" | "exec_at" | "shell" | "shell_at"
325            if !policy_allows_capability(&policy, "process", "exec")
326                || !policy_allows_side_effect(&policy, "process_exec") =>
327        {
328            return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
329        }
330        "http_get" | "http_post" | "http_put" | "http_patch" | "http_delete" | "http_download"
331        | "http_request"
332            if !policy_allows_side_effect(&policy, "network") =>
333        {
334            return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
335        }
336        "http_session_request"
337        | "http_stream_open"
338        | "http_stream_read"
339        | "http_stream_close"
340        | "http_stream_info"
341        | "sse_connect"
342        | "sse_receive"
343        | "websocket_accept"
344        | "websocket_connect"
345        | "websocket_route"
346        | "websocket_send"
347        | "websocket_receive"
348        | "websocket_server"
349            if !policy_allows_side_effect(&policy, "network") =>
350        {
351            return reject_policy(format!("builtin '{name}' exceeds network ceiling"));
352        }
353        "llm_call" | "llm_call_safe" | "llm_completion" | "llm_stream" | "llm_healthcheck"
354        | "agent_loop"
355            if !policy_allows_capability(&policy, "llm", "call")
356                || !policy_allows_side_effect(&policy, "network") =>
357        {
358            return reject_policy(format!("builtin '{name}' exceeds LLM/network ceiling"));
359        }
360        "connector_call"
361            if !policy_allows_capability(&policy, "connector", "call")
362                || !policy_allows_side_effect(&policy, "network") =>
363        {
364            return reject_policy(
365                "builtin 'connector_call' exceeds connector.call/network ceiling".to_string(),
366            );
367        }
368        "secret_get" if !policy_allows_capability(&policy, "connector", "secret_get") => {
369            return reject_policy(
370                "builtin 'secret_get' exceeds connector.secret_get ceiling".to_string(),
371            );
372        }
373        "event_log_emit" if !policy_allows_capability(&policy, "connector", "event_log_emit") => {
374            return reject_policy(
375                "builtin 'event_log_emit' exceeds connector.event_log_emit ceiling".to_string(),
376            );
377        }
378        "metrics_inc" if !policy_allows_capability(&policy, "connector", "metrics_inc") => {
379            return reject_policy(
380                "builtin 'metrics_inc' exceeds connector.metrics_inc ceiling".to_string(),
381            );
382        }
383        "project_fingerprint"
384        | "project_scan_native"
385        | "project_scan_tree_native"
386        | "project_walk_tree_native"
387        | "project_catalog_native"
388            if !policy_allows_capability(&policy, "workspace", "list")
389                || !policy_allows_side_effect(&policy, "read_only") =>
390        {
391            return reject_policy(format!("builtin '{name}' exceeds workspace.list ceiling"));
392        }
393        "__agent_state_init"
394        | "__agent_state_resume"
395        | "__agent_state_write"
396        | "__agent_state_read"
397        | "__agent_state_list"
398        | "__agent_state_delete"
399        | "__agent_state_handoff"
400            if !policy_allows_capability(&policy, "agent_state", "access") =>
401        {
402            return reject_policy(format!(
403                "builtin '{name}' exceeds agent_state.access ceiling"
404            ));
405        }
406        "vision_ocr"
407            if !policy_allows_capability(&policy, "vision", "ocr")
408                || !policy_allows_side_effect(&policy, "process_exec") =>
409        {
410            return reject_policy(format!(
411                "builtin '{name}' exceeds vision.ocr/process ceiling"
412            ));
413        }
414        "mcp_connect"
415        | "mcp_ensure_active"
416        | "mcp_call"
417        | "mcp_list_tools"
418        | "mcp_list_resources"
419        | "mcp_list_resource_templates"
420        | "mcp_read_resource"
421        | "mcp_list_prompts"
422        | "mcp_get_prompt"
423        | "mcp_server_info"
424        | "mcp_disconnect"
425            if !policy_allows_capability(&policy, "process", "exec")
426                || !policy_allows_side_effect(&policy, "process_exec") =>
427        {
428            return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
429        }
430        "host_call" => {
431            let name = args.first().map(|v| v.display()).unwrap_or_default();
432            let Some((capability, op)) = name.split_once('.') else {
433                return reject_policy(format!(
434                    "host_call '{name}' must use capability.operation naming"
435                ));
436            };
437            if !policy_allows_capability(&policy, capability, op) {
438                return reject_policy(format!(
439                    "host_call {capability}.{op} exceeds capability ceiling"
440                ));
441            }
442            let requested_side_effect = match (capability, op) {
443                ("workspace", "write_text" | "apply_edit" | "delete") => "workspace_write",
444                ("process", "exec") => "process_exec",
445                _ => "read_only",
446            };
447            if !policy_allows_side_effect(&policy, requested_side_effect) {
448                return reject_policy(format!(
449                    "host_call {capability}.{op} exceeds side-effect ceiling"
450                ));
451            }
452        }
453        "host_tool_list" | "host_tool_call"
454            if !policy_allows_capability(&policy, "host", "tool_call") =>
455        {
456            return reject_policy(format!("builtin '{name}' exceeds host.tool_call ceiling"));
457        }
458        _ => {}
459    }
460    Ok(())
461}
462
463pub fn enforce_current_policy_for_bridge_builtin(name: &str) -> Result<(), VmError> {
464    let trusted = TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow() > 0);
465    if trusted {
466        return Ok(());
467    }
468    if current_execution_policy().is_some() {
469        return reject_policy(format!(
470            "bridged builtin '{name}' exceeds execution policy; declare an explicit capability/tool surface instead"
471        ));
472    }
473    Ok(())
474}
475
476pub fn enforce_current_policy_for_tool(tool_name: &str) -> Result<(), VmError> {
477    let Some(policy) = current_execution_policy() else {
478        return Ok(());
479    };
480    if !policy_allows_tool(&policy, tool_name) {
481        return reject_policy(format!("tool '{tool_name}' exceeds tool ceiling"));
482    }
483    if let Some(annotations) = policy.tool_annotations.get(tool_name) {
484        for (capability, ops) in &annotations.capabilities {
485            for op in ops {
486                if !policy_allows_capability(&policy, capability, op) {
487                    return reject_policy(format!(
488                        "tool '{tool_name}' exceeds capability ceiling: {capability}.{op}"
489                    ));
490                }
491            }
492        }
493        let requested_level = annotations.side_effect_level;
494        if requested_level != SideEffectLevel::None
495            && !policy_allows_side_effect(&policy, requested_level.as_str())
496        {
497            return reject_policy(format!(
498                "tool '{tool_name}' exceeds side-effect ceiling: {}",
499                requested_level.as_str()
500            ));
501        }
502    }
503    Ok(())
504}
505
506// ── Output visibility redaction ─────────────────────────────────────
507//
508// Transcript lifecycle (reset, fork, trim, compact) now lives on
509// `crate::agent_sessions` as explicit imperative builtins. All that
510// remains here is the per-call visibility filter, which is
511// output-shaping (not lifecycle).
512
513/// Filter a transcript dict down to the caller-visible subset, based
514/// on the `output_visibility` node option. `None` or any unknown
515/// visibility returns the transcript unchanged — callers are expected
516/// to validate the string against a known set upstream.
517pub fn redact_transcript_visibility(
518    transcript: &VmValue,
519    visibility: Option<&str>,
520) -> Option<VmValue> {
521    let Some(visibility) = visibility else {
522        return Some(transcript.clone());
523    };
524    if visibility != "public" && visibility != "public_only" {
525        return Some(transcript.clone());
526    }
527    let dict = transcript.as_dict()?;
528    let public_messages = match dict.get("messages") {
529        Some(VmValue::List(list)) => list
530            .iter()
531            .filter(|message| {
532                message
533                    .as_dict()
534                    .and_then(|d| d.get("role"))
535                    .map(|v| v.display())
536                    .map(|role| role != "tool_result")
537                    .unwrap_or(true)
538            })
539            .cloned()
540            .collect::<Vec<_>>(),
541        _ => Vec::new(),
542    };
543    let public_events = match dict.get("events") {
544        Some(VmValue::List(list)) => list
545            .iter()
546            .filter(|event| {
547                event
548                    .as_dict()
549                    .and_then(|d| d.get("visibility"))
550                    .map(|v| v.display())
551                    .map(|value| value == "public")
552                    .unwrap_or(true)
553            })
554            .cloned()
555            .collect::<Vec<_>>(),
556        _ => Vec::new(),
557    };
558    let mut redacted = dict.clone();
559    redacted.insert(
560        "messages".to_string(),
561        VmValue::List(Rc::new(public_messages)),
562    );
563    redacted.insert("events".to_string(), VmValue::List(Rc::new(public_events)));
564    Some(VmValue::Dict(Rc::new(redacted)))
565}
566
567pub fn builtin_ceiling() -> CapabilityPolicy {
568    CapabilityPolicy {
569        // `capabilities` is intentionally empty: the host capability manifest
570        // is the sole authority, and an allowlist here would silently block
571        // any capability the host adds later.
572        tools: Vec::new(),
573        capabilities: BTreeMap::new(),
574        workspace_roots: Vec::new(),
575        side_effect_level: Some("network".to_string()),
576        recursion_limit: Some(8),
577        tool_arg_constraints: Vec::new(),
578        tool_annotations: BTreeMap::new(),
579    }
580}
581
582/// Declarative policy for tool approval gating. Allows pipelines to
583/// specify which tools are auto-approved, auto-denied, or require
584/// host confirmation, plus write-path allowlists.
585#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
586#[serde(default)]
587pub struct ToolApprovalPolicy {
588    /// Glob patterns for tools that should be auto-approved.
589    #[serde(default)]
590    pub auto_approve: Vec<String>,
591    /// Glob patterns for tools that should always be denied.
592    #[serde(default)]
593    pub auto_deny: Vec<String>,
594    /// Glob patterns for tools that require host confirmation.
595    #[serde(default)]
596    pub require_approval: Vec<String>,
597    /// Glob patterns for writable paths.
598    #[serde(default)]
599    pub write_path_allowlist: Vec<String>,
600}
601
602/// Result of evaluating a tool call against a ToolApprovalPolicy.
603#[derive(Debug, Clone, PartialEq, Eq)]
604pub enum ToolApprovalDecision {
605    /// Tool is auto-approved by policy.
606    AutoApproved,
607    /// Tool is auto-denied by policy.
608    AutoDenied { reason: String },
609    /// Tool requires explicit host approval; the caller already owns the
610    /// tool name and args and forwards them to the host bridge.
611    RequiresHostApproval,
612}
613
614impl ToolApprovalPolicy {
615    /// Evaluate whether a tool call should be approved, denied, or needs
616    /// host confirmation.
617    pub fn evaluate(&self, tool_name: &str, args: &serde_json::Value) -> ToolApprovalDecision {
618        // Auto-deny takes precedence over every other pattern list.
619        for pattern in &self.auto_deny {
620            if glob_match(pattern, tool_name) {
621                return ToolApprovalDecision::AutoDenied {
622                    reason: format!("tool '{tool_name}' matches deny pattern '{pattern}'"),
623                };
624            }
625        }
626
627        if !self.write_path_allowlist.is_empty()
628            && tool_kind_participates_in_write_allowlist(tool_name)
629        {
630            let paths = super::current_tool_declared_path_entries(tool_name, args);
631            for path in &paths {
632                let allowed = self.write_path_allowlist.iter().any(|pattern| {
633                    path.policy_candidates()
634                        .iter()
635                        .any(|candidate| glob_match(pattern, candidate))
636                });
637                if !allowed {
638                    return ToolApprovalDecision::AutoDenied {
639                        reason: format!(
640                            "tool '{tool_name}' targets '{}' which is not in the write-path allowlist",
641                            path.display_path()
642                        ),
643                    };
644                }
645            }
646        }
647
648        for pattern in &self.auto_approve {
649            if glob_match(pattern, tool_name) {
650                return ToolApprovalDecision::AutoApproved;
651            }
652        }
653
654        for pattern in &self.require_approval {
655            if glob_match(pattern, tool_name) {
656                return ToolApprovalDecision::RequiresHostApproval;
657            }
658        }
659
660        ToolApprovalDecision::AutoApproved
661    }
662
663    /// Merge two approval policies, taking the most restrictive combination.
664    /// - auto_approve: only tools approved by BOTH policies stay approved
665    ///   (if either policy has no patterns, the other's patterns are used)
666    /// - auto_deny / require_approval: union (either policy can deny/gate)
667    /// - write_path_allowlist: intersection (both must allow the path)
668    pub fn intersect(&self, other: &ToolApprovalPolicy) -> ToolApprovalPolicy {
669        let auto_approve = if self.auto_approve.is_empty() {
670            other.auto_approve.clone()
671        } else if other.auto_approve.is_empty() {
672            self.auto_approve.clone()
673        } else {
674            self.auto_approve
675                .iter()
676                .filter(|p| other.auto_approve.contains(p))
677                .cloned()
678                .collect()
679        };
680        let mut auto_deny = self.auto_deny.clone();
681        auto_deny.extend(other.auto_deny.iter().cloned());
682        let mut require_approval = self.require_approval.clone();
683        require_approval.extend(other.require_approval.iter().cloned());
684        let write_path_allowlist = if self.write_path_allowlist.is_empty() {
685            other.write_path_allowlist.clone()
686        } else if other.write_path_allowlist.is_empty() {
687            self.write_path_allowlist.clone()
688        } else {
689            self.write_path_allowlist
690                .iter()
691                .filter(|p| other.write_path_allowlist.contains(p))
692                .cloned()
693                .collect()
694        };
695        ToolApprovalPolicy {
696            auto_approve,
697            auto_deny,
698            require_approval,
699            write_path_allowlist,
700        }
701    }
702}
703
704#[cfg(test)]
705mod approval_policy_tests {
706    use super::*;
707    use crate::orchestration::{pop_execution_policy, push_execution_policy, CapabilityPolicy};
708    use crate::tool_annotations::{ToolAnnotations, ToolArgSchema, ToolKind};
709
710    #[test]
711    fn auto_deny_takes_precedence_over_auto_approve() {
712        let policy = ToolApprovalPolicy {
713            auto_approve: vec!["*".to_string()],
714            auto_deny: vec!["dangerous_*".to_string()],
715            ..Default::default()
716        };
717        assert_eq!(
718            policy.evaluate("dangerous_rm", &serde_json::json!({})),
719            ToolApprovalDecision::AutoDenied {
720                reason: "tool 'dangerous_rm' matches deny pattern 'dangerous_*'".to_string()
721            }
722        );
723    }
724
725    #[test]
726    fn auto_approve_matches_glob() {
727        let policy = ToolApprovalPolicy {
728            auto_approve: vec!["read*".to_string(), "search*".to_string()],
729            ..Default::default()
730        };
731        assert_eq!(
732            policy.evaluate("read_file", &serde_json::json!({})),
733            ToolApprovalDecision::AutoApproved
734        );
735        assert_eq!(
736            policy.evaluate("search", &serde_json::json!({})),
737            ToolApprovalDecision::AutoApproved
738        );
739    }
740
741    #[test]
742    fn require_approval_emits_decision() {
743        let policy = ToolApprovalPolicy {
744            require_approval: vec!["edit*".to_string()],
745            ..Default::default()
746        };
747        let decision = policy.evaluate("edit_file", &serde_json::json!({"path": "foo.rs"}));
748        assert!(matches!(
749            decision,
750            ToolApprovalDecision::RequiresHostApproval
751        ));
752    }
753
754    #[test]
755    fn unmatched_tool_defaults_to_approved() {
756        let policy = ToolApprovalPolicy {
757            auto_approve: vec!["read*".to_string()],
758            require_approval: vec!["edit*".to_string()],
759            ..Default::default()
760        };
761        assert_eq!(
762            policy.evaluate("unknown_tool", &serde_json::json!({})),
763            ToolApprovalDecision::AutoApproved
764        );
765    }
766
767    #[test]
768    fn intersect_merges_deny_lists() {
769        let a = ToolApprovalPolicy {
770            auto_deny: vec!["rm*".to_string()],
771            ..Default::default()
772        };
773        let b = ToolApprovalPolicy {
774            auto_deny: vec!["drop*".to_string()],
775            ..Default::default()
776        };
777        let merged = a.intersect(&b);
778        assert_eq!(merged.auto_deny.len(), 2);
779    }
780
781    #[test]
782    fn intersect_restricts_auto_approve_to_common_patterns() {
783        let a = ToolApprovalPolicy {
784            auto_approve: vec!["read*".to_string(), "search*".to_string()],
785            ..Default::default()
786        };
787        let b = ToolApprovalPolicy {
788            auto_approve: vec!["read*".to_string(), "write*".to_string()],
789            ..Default::default()
790        };
791        let merged = a.intersect(&b);
792        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
793    }
794
795    #[test]
796    fn intersect_defers_auto_approve_when_one_side_empty() {
797        let a = ToolApprovalPolicy {
798            auto_approve: vec!["read*".to_string()],
799            ..Default::default()
800        };
801        let b = ToolApprovalPolicy::default();
802        let merged = a.intersect(&b);
803        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
804    }
805
806    #[test]
807    fn write_path_allowlist_matches_recovered_workspace_relative_path() {
808        let temp = tempfile::tempdir().unwrap();
809        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
810        std::fs::write(temp.path().join("packages/demo/file.txt"), "ok").unwrap();
811        crate::stdlib::process::set_thread_execution_context(Some(
812            crate::orchestration::RunExecutionRecord {
813                cwd: Some(temp.path().to_string_lossy().into_owned()),
814                source_dir: Some(temp.path().to_string_lossy().into_owned()),
815                env: BTreeMap::new(),
816                adapter: None,
817                repo_path: None,
818                worktree_path: None,
819                branch: None,
820                base_ref: None,
821                cleanup: None,
822            },
823        ));
824
825        let mut tool_annotations = BTreeMap::new();
826        tool_annotations.insert(
827            "write_file".to_string(),
828            ToolAnnotations {
829                kind: ToolKind::Edit,
830                arg_schema: ToolArgSchema {
831                    path_params: vec!["path".to_string()],
832                    ..Default::default()
833                },
834                ..Default::default()
835            },
836        );
837        push_execution_policy(CapabilityPolicy {
838            tool_annotations,
839            ..Default::default()
840        });
841
842        let policy = ToolApprovalPolicy {
843            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
844            ..Default::default()
845        };
846        let decision = policy.evaluate(
847            "write_file",
848            &serde_json::json!({"path": "/packages/demo/file.txt"}),
849        );
850        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
851
852        pop_execution_policy();
853        crate::stdlib::process::set_thread_execution_context(None);
854    }
855
856    #[test]
857    fn write_path_allowlist_does_not_block_read_only_tools() {
858        let temp = tempfile::tempdir().unwrap();
859        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
860        std::fs::write(temp.path().join("packages/demo/context.txt"), "ok").unwrap();
861        crate::stdlib::process::set_thread_execution_context(Some(
862            crate::orchestration::RunExecutionRecord {
863                cwd: Some(temp.path().to_string_lossy().into_owned()),
864                source_dir: Some(temp.path().to_string_lossy().into_owned()),
865                env: BTreeMap::new(),
866                adapter: None,
867                repo_path: None,
868                worktree_path: None,
869                branch: None,
870                base_ref: None,
871                cleanup: None,
872            },
873        ));
874
875        let mut tool_annotations = BTreeMap::new();
876        tool_annotations.insert(
877            "read_file".to_string(),
878            ToolAnnotations {
879                kind: ToolKind::Read,
880                arg_schema: ToolArgSchema {
881                    path_params: vec!["path".to_string()],
882                    ..Default::default()
883                },
884                ..Default::default()
885            },
886        );
887        push_execution_policy(CapabilityPolicy {
888            tool_annotations,
889            ..Default::default()
890        });
891
892        let policy = ToolApprovalPolicy {
893            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
894            ..Default::default()
895        };
896        let decision = policy.evaluate(
897            "read_file",
898            &serde_json::json!({"path": "/packages/demo/context.txt"}),
899        );
900        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
901
902        pop_execution_policy();
903        crate::stdlib::process::set_thread_execution_context(None);
904    }
905}
906
907#[cfg(test)]
908mod turn_policy_tests {
909    use super::TurnPolicy;
910
911    #[test]
912    fn default_allows_done_sentinel() {
913        let policy = TurnPolicy::default();
914        assert!(policy.allow_done_sentinel);
915        assert!(!policy.require_action_or_yield);
916        assert!(policy.max_prose_chars.is_none());
917    }
918
919    #[test]
920    fn deserializing_partial_dict_preserves_done_sentinel_pathway() {
921        // Pre-existing workflows passed `turn_policy: { require_action_or_yield: true }`
922        // without knowing about `allow_done_sentinel`. Deserializing such a dict
923        // must keep the done-sentinel pathway enabled so persistent agent loops
924        // don't lose their completion signal in this release.
925        let policy: TurnPolicy =
926            serde_json::from_value(serde_json::json!({ "require_action_or_yield": true }))
927                .expect("deserialize");
928        assert!(policy.require_action_or_yield);
929        assert!(policy.allow_done_sentinel);
930    }
931
932    #[test]
933    fn deserializing_explicit_false_disables_done_sentinel() {
934        let policy: TurnPolicy = serde_json::from_value(serde_json::json!({
935            "require_action_or_yield": true,
936            "allow_done_sentinel": false,
937        }))
938        .expect("deserialize");
939        assert!(policy.require_action_or_yield);
940        assert!(!policy.allow_done_sentinel);
941    }
942}
943
944#[cfg(test)]
945mod visibility_redaction_tests {
946    use super::*;
947    use crate::value::VmValue;
948
949    fn mock_transcript() -> VmValue {
950        let messages = vec![
951            serde_json::json!({"role": "user", "content": "hi"}),
952            serde_json::json!({"role": "assistant", "content": "hello"}),
953            serde_json::json!({"role": "tool_result", "content": "internal tool output"}),
954        ];
955        crate::llm::helpers::transcript_to_vm_with_events(
956            Some("test-id".to_string()),
957            None,
958            None,
959            &messages,
960            Vec::new(),
961            Vec::new(),
962            Some("active"),
963        )
964    }
965
966    fn message_count(transcript: &VmValue) -> usize {
967        transcript
968            .as_dict()
969            .and_then(|d| d.get("messages"))
970            .and_then(|v| match v {
971                VmValue::List(list) => Some(list.len()),
972                _ => None,
973            })
974            .unwrap_or(0)
975    }
976
977    #[test]
978    fn visibility_none_returns_unchanged() {
979        let t = mock_transcript();
980        let result = redact_transcript_visibility(&t, None).unwrap();
981        assert_eq!(message_count(&result), 3);
982    }
983
984    #[test]
985    fn visibility_public_drops_tool_results() {
986        let t = mock_transcript();
987        let result = redact_transcript_visibility(&t, Some("public")).unwrap();
988        assert_eq!(message_count(&result), 2);
989    }
990
991    #[test]
992    fn visibility_unknown_string_is_pass_through() {
993        let t = mock_transcript();
994        let result = redact_transcript_visibility(&t, Some("internal")).unwrap();
995        assert_eq!(message_count(&result), 3);
996    }
997}