Skip to main content

harn_vm/orchestration/policy/
mod.rs

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