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        "mcp_connect"
328        | "mcp_call"
329        | "mcp_list_tools"
330        | "mcp_list_resources"
331        | "mcp_list_resource_templates"
332        | "mcp_read_resource"
333        | "mcp_list_prompts"
334        | "mcp_get_prompt"
335        | "mcp_server_info"
336        | "mcp_disconnect"
337            if !policy_allows_capability(&policy, "process", "exec")
338                || !policy_allows_side_effect(&policy, "process_exec") =>
339        {
340            return reject_policy(format!("builtin '{name}' exceeds process.exec ceiling"));
341        }
342        "host_call" => {
343            let name = args.first().map(|v| v.display()).unwrap_or_default();
344            let Some((capability, op)) = name.split_once('.') else {
345                return reject_policy(format!(
346                    "host_call '{name}' must use capability.operation naming"
347                ));
348            };
349            if !policy_allows_capability(&policy, capability, op) {
350                return reject_policy(format!(
351                    "host_call {capability}.{op} exceeds capability ceiling"
352                ));
353            }
354            let requested_side_effect = match (capability, op) {
355                ("workspace", "write_text" | "apply_edit" | "delete") => "workspace_write",
356                ("process", "exec") => "process_exec",
357                _ => "read_only",
358            };
359            if !policy_allows_side_effect(&policy, requested_side_effect) {
360                return reject_policy(format!(
361                    "host_call {capability}.{op} exceeds side-effect ceiling"
362                ));
363            }
364        }
365        _ => {}
366    }
367    Ok(())
368}
369
370pub fn enforce_current_policy_for_bridge_builtin(name: &str) -> Result<(), VmError> {
371    let trusted = TRUSTED_BRIDGE_CALL_DEPTH.with(|depth| *depth.borrow() > 0);
372    if trusted {
373        return Ok(());
374    }
375    if current_execution_policy().is_some() {
376        return reject_policy(format!(
377            "bridged builtin '{name}' exceeds execution policy; declare an explicit capability/tool surface instead"
378        ));
379    }
380    Ok(())
381}
382
383pub fn enforce_current_policy_for_tool(tool_name: &str) -> Result<(), VmError> {
384    let Some(policy) = current_execution_policy() else {
385        return Ok(());
386    };
387    if !policy_allows_tool(&policy, tool_name) {
388        return reject_policy(format!("tool '{tool_name}' exceeds tool ceiling"));
389    }
390    if let Some(annotations) = policy.tool_annotations.get(tool_name) {
391        for (capability, ops) in &annotations.capabilities {
392            for op in ops {
393                if !policy_allows_capability(&policy, capability, op) {
394                    return reject_policy(format!(
395                        "tool '{tool_name}' exceeds capability ceiling: {capability}.{op}"
396                    ));
397                }
398            }
399        }
400        let requested_level = annotations.side_effect_level;
401        if requested_level != SideEffectLevel::None
402            && !policy_allows_side_effect(&policy, requested_level.as_str())
403        {
404            return reject_policy(format!(
405                "tool '{tool_name}' exceeds side-effect ceiling: {}",
406                requested_level.as_str()
407            ));
408        }
409    }
410    Ok(())
411}
412
413// ── Output visibility redaction ─────────────────────────────────────
414//
415// Transcript lifecycle (reset, fork, trim, compact) now lives on
416// `crate::agent_sessions` as explicit imperative builtins. All that
417// remains here is the per-call visibility filter, which is
418// output-shaping (not lifecycle).
419
420/// Filter a transcript dict down to the caller-visible subset, based
421/// on the `output_visibility` node option. `None` or any unknown
422/// visibility returns the transcript unchanged — callers are expected
423/// to validate the string against a known set upstream.
424pub fn redact_transcript_visibility(
425    transcript: &VmValue,
426    visibility: Option<&str>,
427) -> Option<VmValue> {
428    let Some(visibility) = visibility else {
429        return Some(transcript.clone());
430    };
431    if visibility != "public" && visibility != "public_only" {
432        return Some(transcript.clone());
433    }
434    let dict = transcript.as_dict()?;
435    let public_messages = match dict.get("messages") {
436        Some(VmValue::List(list)) => list
437            .iter()
438            .filter(|message| {
439                message
440                    .as_dict()
441                    .and_then(|d| d.get("role"))
442                    .map(|v| v.display())
443                    .map(|role| role != "tool_result")
444                    .unwrap_or(true)
445            })
446            .cloned()
447            .collect::<Vec<_>>(),
448        _ => Vec::new(),
449    };
450    let public_events = match dict.get("events") {
451        Some(VmValue::List(list)) => list
452            .iter()
453            .filter(|event| {
454                event
455                    .as_dict()
456                    .and_then(|d| d.get("visibility"))
457                    .map(|v| v.display())
458                    .map(|value| value == "public")
459                    .unwrap_or(true)
460            })
461            .cloned()
462            .collect::<Vec<_>>(),
463        _ => Vec::new(),
464    };
465    let mut redacted = dict.clone();
466    redacted.insert(
467        "messages".to_string(),
468        VmValue::List(Rc::new(public_messages)),
469    );
470    redacted.insert("events".to_string(), VmValue::List(Rc::new(public_events)));
471    Some(VmValue::Dict(Rc::new(redacted)))
472}
473
474pub fn builtin_ceiling() -> CapabilityPolicy {
475    CapabilityPolicy {
476        // `capabilities` is intentionally empty: the host capability manifest
477        // is the sole authority, and an allowlist here would silently block
478        // any capability the host adds later.
479        tools: Vec::new(),
480        capabilities: BTreeMap::new(),
481        workspace_roots: Vec::new(),
482        side_effect_level: Some("network".to_string()),
483        recursion_limit: Some(8),
484        tool_arg_constraints: Vec::new(),
485        tool_annotations: BTreeMap::new(),
486    }
487}
488
489/// Declarative policy for tool approval gating. Allows pipelines to
490/// specify which tools are auto-approved, auto-denied, or require
491/// host confirmation, plus write-path allowlists.
492#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
493#[serde(default)]
494pub struct ToolApprovalPolicy {
495    /// Glob patterns for tools that should be auto-approved.
496    #[serde(default)]
497    pub auto_approve: Vec<String>,
498    /// Glob patterns for tools that should always be denied.
499    #[serde(default)]
500    pub auto_deny: Vec<String>,
501    /// Glob patterns for tools that require host confirmation.
502    #[serde(default)]
503    pub require_approval: Vec<String>,
504    /// Glob patterns for writable paths.
505    #[serde(default)]
506    pub write_path_allowlist: Vec<String>,
507}
508
509/// Result of evaluating a tool call against a ToolApprovalPolicy.
510#[derive(Debug, Clone, PartialEq, Eq)]
511pub enum ToolApprovalDecision {
512    /// Tool is auto-approved by policy.
513    AutoApproved,
514    /// Tool is auto-denied by policy.
515    AutoDenied { reason: String },
516    /// Tool requires explicit host approval; the caller already owns the
517    /// tool name and args and forwards them to the host bridge.
518    RequiresHostApproval,
519}
520
521impl ToolApprovalPolicy {
522    /// Evaluate whether a tool call should be approved, denied, or needs
523    /// host confirmation.
524    pub fn evaluate(&self, tool_name: &str, args: &serde_json::Value) -> ToolApprovalDecision {
525        // Auto-deny takes precedence over every other pattern list.
526        for pattern in &self.auto_deny {
527            if glob_match(pattern, tool_name) {
528                return ToolApprovalDecision::AutoDenied {
529                    reason: format!("tool '{tool_name}' matches deny pattern '{pattern}'"),
530                };
531            }
532        }
533
534        if !self.write_path_allowlist.is_empty()
535            && tool_kind_participates_in_write_allowlist(tool_name)
536        {
537            let paths = super::current_tool_declared_path_entries(tool_name, args);
538            for path in &paths {
539                let allowed = self.write_path_allowlist.iter().any(|pattern| {
540                    path.policy_candidates()
541                        .iter()
542                        .any(|candidate| glob_match(pattern, candidate))
543                });
544                if !allowed {
545                    return ToolApprovalDecision::AutoDenied {
546                        reason: format!(
547                            "tool '{tool_name}' targets '{}' which is not in the write-path allowlist",
548                            path.display_path()
549                        ),
550                    };
551                }
552            }
553        }
554
555        for pattern in &self.auto_approve {
556            if glob_match(pattern, tool_name) {
557                return ToolApprovalDecision::AutoApproved;
558            }
559        }
560
561        for pattern in &self.require_approval {
562            if glob_match(pattern, tool_name) {
563                return ToolApprovalDecision::RequiresHostApproval;
564            }
565        }
566
567        ToolApprovalDecision::AutoApproved
568    }
569
570    /// Merge two approval policies, taking the most restrictive combination.
571    /// - auto_approve: only tools approved by BOTH policies stay approved
572    ///   (if either policy has no patterns, the other's patterns are used)
573    /// - auto_deny / require_approval: union (either policy can deny/gate)
574    /// - write_path_allowlist: intersection (both must allow the path)
575    pub fn intersect(&self, other: &ToolApprovalPolicy) -> ToolApprovalPolicy {
576        let auto_approve = if self.auto_approve.is_empty() {
577            other.auto_approve.clone()
578        } else if other.auto_approve.is_empty() {
579            self.auto_approve.clone()
580        } else {
581            self.auto_approve
582                .iter()
583                .filter(|p| other.auto_approve.contains(p))
584                .cloned()
585                .collect()
586        };
587        let mut auto_deny = self.auto_deny.clone();
588        auto_deny.extend(other.auto_deny.iter().cloned());
589        let mut require_approval = self.require_approval.clone();
590        require_approval.extend(other.require_approval.iter().cloned());
591        let write_path_allowlist = if self.write_path_allowlist.is_empty() {
592            other.write_path_allowlist.clone()
593        } else if other.write_path_allowlist.is_empty() {
594            self.write_path_allowlist.clone()
595        } else {
596            self.write_path_allowlist
597                .iter()
598                .filter(|p| other.write_path_allowlist.contains(p))
599                .cloned()
600                .collect()
601        };
602        ToolApprovalPolicy {
603            auto_approve,
604            auto_deny,
605            require_approval,
606            write_path_allowlist,
607        }
608    }
609}
610
611#[cfg(test)]
612mod approval_policy_tests {
613    use super::*;
614    use crate::orchestration::{pop_execution_policy, push_execution_policy, CapabilityPolicy};
615    use crate::tool_annotations::{ToolAnnotations, ToolArgSchema, ToolKind};
616
617    #[test]
618    fn auto_deny_takes_precedence_over_auto_approve() {
619        let policy = ToolApprovalPolicy {
620            auto_approve: vec!["*".to_string()],
621            auto_deny: vec!["dangerous_*".to_string()],
622            ..Default::default()
623        };
624        assert_eq!(
625            policy.evaluate("dangerous_rm", &serde_json::json!({})),
626            ToolApprovalDecision::AutoDenied {
627                reason: "tool 'dangerous_rm' matches deny pattern 'dangerous_*'".to_string()
628            }
629        );
630    }
631
632    #[test]
633    fn auto_approve_matches_glob() {
634        let policy = ToolApprovalPolicy {
635            auto_approve: vec!["read*".to_string(), "search*".to_string()],
636            ..Default::default()
637        };
638        assert_eq!(
639            policy.evaluate("read_file", &serde_json::json!({})),
640            ToolApprovalDecision::AutoApproved
641        );
642        assert_eq!(
643            policy.evaluate("search", &serde_json::json!({})),
644            ToolApprovalDecision::AutoApproved
645        );
646    }
647
648    #[test]
649    fn require_approval_emits_decision() {
650        let policy = ToolApprovalPolicy {
651            require_approval: vec!["edit*".to_string()],
652            ..Default::default()
653        };
654        let decision = policy.evaluate("edit_file", &serde_json::json!({"path": "foo.rs"}));
655        assert!(matches!(
656            decision,
657            ToolApprovalDecision::RequiresHostApproval
658        ));
659    }
660
661    #[test]
662    fn unmatched_tool_defaults_to_approved() {
663        let policy = ToolApprovalPolicy {
664            auto_approve: vec!["read*".to_string()],
665            require_approval: vec!["edit*".to_string()],
666            ..Default::default()
667        };
668        assert_eq!(
669            policy.evaluate("unknown_tool", &serde_json::json!({})),
670            ToolApprovalDecision::AutoApproved
671        );
672    }
673
674    #[test]
675    fn intersect_merges_deny_lists() {
676        let a = ToolApprovalPolicy {
677            auto_deny: vec!["rm*".to_string()],
678            ..Default::default()
679        };
680        let b = ToolApprovalPolicy {
681            auto_deny: vec!["drop*".to_string()],
682            ..Default::default()
683        };
684        let merged = a.intersect(&b);
685        assert_eq!(merged.auto_deny.len(), 2);
686    }
687
688    #[test]
689    fn intersect_restricts_auto_approve_to_common_patterns() {
690        let a = ToolApprovalPolicy {
691            auto_approve: vec!["read*".to_string(), "search*".to_string()],
692            ..Default::default()
693        };
694        let b = ToolApprovalPolicy {
695            auto_approve: vec!["read*".to_string(), "write*".to_string()],
696            ..Default::default()
697        };
698        let merged = a.intersect(&b);
699        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
700    }
701
702    #[test]
703    fn intersect_defers_auto_approve_when_one_side_empty() {
704        let a = ToolApprovalPolicy {
705            auto_approve: vec!["read*".to_string()],
706            ..Default::default()
707        };
708        let b = ToolApprovalPolicy::default();
709        let merged = a.intersect(&b);
710        assert_eq!(merged.auto_approve, vec!["read*".to_string()]);
711    }
712
713    #[test]
714    fn write_path_allowlist_matches_recovered_workspace_relative_path() {
715        let temp = tempfile::tempdir().unwrap();
716        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
717        std::fs::write(temp.path().join("packages/demo/file.txt"), "ok").unwrap();
718        crate::stdlib::process::set_thread_execution_context(Some(
719            crate::orchestration::RunExecutionRecord {
720                cwd: Some(temp.path().to_string_lossy().into_owned()),
721                source_dir: Some(temp.path().to_string_lossy().into_owned()),
722                env: BTreeMap::new(),
723                adapter: None,
724                repo_path: None,
725                worktree_path: None,
726                branch: None,
727                base_ref: None,
728                cleanup: None,
729            },
730        ));
731
732        let mut tool_annotations = BTreeMap::new();
733        tool_annotations.insert(
734            "write_file".to_string(),
735            ToolAnnotations {
736                kind: ToolKind::Edit,
737                arg_schema: ToolArgSchema {
738                    path_params: vec!["path".to_string()],
739                    ..Default::default()
740                },
741                ..Default::default()
742            },
743        );
744        push_execution_policy(CapabilityPolicy {
745            tool_annotations,
746            ..Default::default()
747        });
748
749        let policy = ToolApprovalPolicy {
750            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
751            ..Default::default()
752        };
753        let decision = policy.evaluate(
754            "write_file",
755            &serde_json::json!({"path": "/packages/demo/file.txt"}),
756        );
757        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
758
759        pop_execution_policy();
760        crate::stdlib::process::set_thread_execution_context(None);
761    }
762
763    #[test]
764    fn write_path_allowlist_does_not_block_read_only_tools() {
765        let temp = tempfile::tempdir().unwrap();
766        std::fs::create_dir_all(temp.path().join("packages/demo")).unwrap();
767        std::fs::write(temp.path().join("packages/demo/context.txt"), "ok").unwrap();
768        crate::stdlib::process::set_thread_execution_context(Some(
769            crate::orchestration::RunExecutionRecord {
770                cwd: Some(temp.path().to_string_lossy().into_owned()),
771                source_dir: Some(temp.path().to_string_lossy().into_owned()),
772                env: BTreeMap::new(),
773                adapter: None,
774                repo_path: None,
775                worktree_path: None,
776                branch: None,
777                base_ref: None,
778                cleanup: None,
779            },
780        ));
781
782        let mut tool_annotations = BTreeMap::new();
783        tool_annotations.insert(
784            "read_file".to_string(),
785            ToolAnnotations {
786                kind: ToolKind::Read,
787                arg_schema: ToolArgSchema {
788                    path_params: vec!["path".to_string()],
789                    ..Default::default()
790                },
791                ..Default::default()
792            },
793        );
794        push_execution_policy(CapabilityPolicy {
795            tool_annotations,
796            ..Default::default()
797        });
798
799        let policy = ToolApprovalPolicy {
800            write_path_allowlist: vec!["packages/demo/file.txt".to_string()],
801            ..Default::default()
802        };
803        let decision = policy.evaluate(
804            "read_file",
805            &serde_json::json!({"path": "/packages/demo/context.txt"}),
806        );
807        assert_eq!(decision, ToolApprovalDecision::AutoApproved);
808
809        pop_execution_policy();
810        crate::stdlib::process::set_thread_execution_context(None);
811    }
812}
813
814#[cfg(test)]
815mod turn_policy_tests {
816    use super::TurnPolicy;
817
818    #[test]
819    fn default_allows_done_sentinel() {
820        let policy = TurnPolicy::default();
821        assert!(policy.allow_done_sentinel);
822        assert!(!policy.require_action_or_yield);
823        assert!(policy.max_prose_chars.is_none());
824    }
825
826    #[test]
827    fn deserializing_partial_dict_preserves_done_sentinel_pathway() {
828        // Pre-existing workflows passed `turn_policy: { require_action_or_yield: true }`
829        // without knowing about `allow_done_sentinel`. Deserializing such a dict
830        // must keep the done-sentinel pathway enabled so persistent agent loops
831        // don't lose their completion signal in this release.
832        let policy: TurnPolicy =
833            serde_json::from_value(serde_json::json!({ "require_action_or_yield": true }))
834                .expect("deserialize");
835        assert!(policy.require_action_or_yield);
836        assert!(policy.allow_done_sentinel);
837    }
838
839    #[test]
840    fn deserializing_explicit_false_disables_done_sentinel() {
841        let policy: TurnPolicy = serde_json::from_value(serde_json::json!({
842            "require_action_or_yield": true,
843            "allow_done_sentinel": false,
844        }))
845        .expect("deserialize");
846        assert!(policy.require_action_or_yield);
847        assert!(!policy.allow_done_sentinel);
848    }
849}
850
851#[cfg(test)]
852mod visibility_redaction_tests {
853    use super::*;
854    use crate::value::VmValue;
855
856    fn mock_transcript() -> VmValue {
857        let messages = vec![
858            serde_json::json!({"role": "user", "content": "hi"}),
859            serde_json::json!({"role": "assistant", "content": "hello"}),
860            serde_json::json!({"role": "tool_result", "content": "internal tool output"}),
861        ];
862        crate::llm::helpers::transcript_to_vm_with_events(
863            Some("test-id".to_string()),
864            None,
865            None,
866            &messages,
867            Vec::new(),
868            Vec::new(),
869            Some("active"),
870        )
871    }
872
873    fn message_count(transcript: &VmValue) -> usize {
874        transcript
875            .as_dict()
876            .and_then(|d| d.get("messages"))
877            .and_then(|v| match v {
878                VmValue::List(list) => Some(list.len()),
879                _ => None,
880            })
881            .unwrap_or(0)
882    }
883
884    #[test]
885    fn visibility_none_returns_unchanged() {
886        let t = mock_transcript();
887        let result = redact_transcript_visibility(&t, None).unwrap();
888        assert_eq!(message_count(&result), 3);
889    }
890
891    #[test]
892    fn visibility_public_drops_tool_results() {
893        let t = mock_transcript();
894        let result = redact_transcript_visibility(&t, Some("public")).unwrap();
895        assert_eq!(message_count(&result), 2);
896    }
897
898    #[test]
899    fn visibility_unknown_string_is_pass_through() {
900        let t = mock_transcript();
901        let result = redact_transcript_visibility(&t, Some("internal")).unwrap();
902        assert_eq!(message_count(&result), 3);
903    }
904}