Skip to main content

harn_vm/orchestration/
nested_invocation.rs

1//! Capability-ceiling guard for nested Harn invocations.
2//!
3//! When a script that already runs under a parent execution policy
4//! launches another Harn invocation — `harn run`, `harn workflow run`,
5//! `harn supervisor fire/replay`, or a Burin harness — Harn must scan
6//! the target and reject anything that asks for *more* than the parent
7//! ceiling. This module hosts the scanner: it accepts a parent
8//! [`CapabilityPolicy`] plus a description of the nested target, and
9//! returns the list of dimensions where the target widens the parent.
10//!
11//! The scanner is deliberately conservative: when in doubt about what
12//! the target requests, it errs on the side of "ask for more" so the
13//! parent rejects rather than silently widens. It is the integration
14//! layer's job to wire this into the actual launch points (the `exec`,
15//! `shell`, `host_call` builtins, the workflow CLI, the supervisor
16//! API).
17
18use std::collections::BTreeSet;
19
20use serde::{Deserialize, Serialize};
21
22use super::workflow_bundle::WorkflowBundle;
23use super::workflow_patch::{bundle_capability_ceiling, CapabilityCeilingViolation};
24use super::CapabilityPolicy;
25
26/// One Harn invocation that could be launched from inside another.
27/// Each variant carries the data the scanner needs to project an
28/// effective requested ceiling.
29#[derive(Clone, Debug)]
30pub enum NestedInvocationTarget<'a> {
31    /// A `harn workflow run`-style invocation against a parsed bundle.
32    WorkflowBundle(&'a WorkflowBundle),
33    /// A `harn run`-style invocation against a Harn script source. The
34    /// scanner does a coarse string scan for builtins that require
35    /// elevated capabilities; that is enough to catch the obvious
36    /// widening attempts without forcing the AST into this layer.
37    HarnScript { path: &'a str, source: &'a str },
38    /// A Burin harness manifest. The scanner reads
39    /// `capability_ceiling` and `tools` if present, falling back to
40    /// "request everything" so the parent rejects unstructured
41    /// manifests rather than rubber-stamping them.
42    BurinHarness { manifest: &'a serde_json::Value },
43}
44
45#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
46pub struct NestedInvocationCeilingReport {
47    pub target_kind: String,
48    pub target_label: String,
49    pub parent: CapabilityPolicy,
50    pub requested: CapabilityPolicy,
51    pub violations: Vec<CapabilityCeilingViolation>,
52}
53
54impl NestedInvocationCeilingReport {
55    pub fn allowed(&self) -> bool {
56        self.violations.is_empty()
57    }
58}
59
60/// Compute the capability ceiling that running `target` would request
61/// of its parent runtime. This mirrors the projection used inside
62/// [`bundle_capability_ceiling`] so workflow bundles and standalone
63/// scripts share one comparison axis.
64pub fn requested_ceiling_for_target(target: &NestedInvocationTarget<'_>) -> CapabilityPolicy {
65    match target {
66        NestedInvocationTarget::WorkflowBundle(bundle) => bundle_capability_ceiling(bundle),
67        NestedInvocationTarget::HarnScript { source, .. } => scan_harn_script_ceiling(source),
68        NestedInvocationTarget::BurinHarness { manifest } => scan_burin_manifest_ceiling(manifest),
69    }
70}
71
72/// Enforce the parent ceiling against a nested invocation target.
73/// Returns a populated report; callers reject the launch if
74/// `report.allowed()` is false.
75pub fn enforce_nested_invocation_ceiling(
76    parent: &CapabilityPolicy,
77    target: &NestedInvocationTarget<'_>,
78) -> NestedInvocationCeilingReport {
79    let requested = requested_ceiling_for_target(target);
80    let violations = collect_violations(parent, &requested);
81    let (kind, label) = match target {
82        NestedInvocationTarget::WorkflowBundle(bundle) => {
83            ("workflow_bundle".to_string(), bundle.id.clone())
84        }
85        NestedInvocationTarget::HarnScript { path, .. } => {
86            ("harn_script".to_string(), path.to_string())
87        }
88        NestedInvocationTarget::BurinHarness { manifest } => {
89            let label = manifest
90                .get("id")
91                .and_then(|value| value.as_str())
92                .unwrap_or("<unknown>")
93                .to_string();
94            ("burin_harness".to_string(), label)
95        }
96    };
97    NestedInvocationCeilingReport {
98        target_kind: kind,
99        target_label: label,
100        parent: parent.clone(),
101        requested,
102        violations,
103    }
104}
105
106fn collect_violations(
107    parent: &CapabilityPolicy,
108    requested: &CapabilityPolicy,
109) -> Vec<CapabilityCeilingViolation> {
110    let mut violations = Vec::new();
111    if !parent.tools.is_empty() {
112        for tool in &requested.tools {
113            if !parent.tools.contains(tool) {
114                violations.push(CapabilityCeilingViolation {
115                    kind: "tool".to_string(),
116                    detail: format!("nested target requests tool '{tool}' outside parent ceiling"),
117                });
118            }
119        }
120    }
121    for (capability, ops) in &requested.capabilities {
122        match parent.capabilities.get(capability) {
123            Some(parent_ops) => {
124                for op in ops {
125                    if !parent_ops.contains(op) {
126                        violations.push(CapabilityCeilingViolation {
127                            kind: "capability".to_string(),
128                            detail: format!(
129                                "nested target requests '{capability}.{op}' outside parent ceiling"
130                            ),
131                        });
132                    }
133                }
134            }
135            None if !parent.capabilities.is_empty() => {
136                violations.push(CapabilityCeilingViolation {
137                    kind: "capability".to_string(),
138                    detail: format!(
139                        "nested target requests capability '{capability}' outside parent ceiling"
140                    ),
141                });
142            }
143            _ => {}
144        }
145    }
146    if let (Some(parent_level), Some(requested_level)) = (
147        parent.side_effect_level.as_deref(),
148        requested.side_effect_level.as_deref(),
149    ) {
150        if rank(requested_level) > rank(parent_level) {
151            violations.push(CapabilityCeilingViolation {
152                kind: "side_effect_level".to_string(),
153                detail: format!(
154                    "nested target requests side_effect_level '{requested_level}' outside parent ceiling '{parent_level}'"
155                ),
156            });
157        }
158    }
159    if !parent.workspace_roots.is_empty() {
160        for root in &requested.workspace_roots {
161            if !parent.workspace_roots.contains(root) {
162                violations.push(CapabilityCeilingViolation {
163                    kind: "workspace_root".to_string(),
164                    detail: format!(
165                        "nested target requests workspace_root '{root}' outside parent allowlist"
166                    ),
167                });
168            }
169        }
170    }
171    violations
172}
173
174fn rank(level: &str) -> usize {
175    match level {
176        "none" => 0,
177        "read_only" => 1,
178        "workspace_write" => 2,
179        "process_exec" => 3,
180        "network" => 4,
181        _ => 5,
182    }
183}
184
185/// Coarse capability projection for a Harn script source. We look for
186/// stdlib builtin tokens (`exec`, `shell`, `http_*`, `write_file`,
187/// `connector_call`, `llm_call`, etc.) and project an `(operation,
188/// side_effect_level)` set that the parent must allow. This is not
189/// type-aware — it intentionally over-includes rather than miss a
190/// widening attempt.
191fn scan_harn_script_ceiling(source: &str) -> CapabilityPolicy {
192    let stripped = strip_comments(source);
193    let mut capabilities: std::collections::BTreeMap<String, BTreeSet<String>> =
194        std::collections::BTreeMap::new();
195    let mut max_side_effect: Option<&'static str> = None;
196
197    for (token, capability, op, side_effect) in BUILTIN_CAPABILITIES {
198        if contains_call(&stripped, token) {
199            capabilities
200                .entry((*capability).to_string())
201                .or_default()
202                .insert((*op).to_string());
203            max_side_effect = match max_side_effect {
204                Some(current) if rank(current) >= rank(side_effect) => Some(current),
205                _ => Some(side_effect),
206            };
207        }
208    }
209
210    CapabilityPolicy {
211        tools: Vec::new(),
212        capabilities: capabilities
213            .into_iter()
214            .map(|(k, v)| (k, v.into_iter().collect()))
215            .collect(),
216        workspace_roots: Vec::new(),
217        side_effect_level: max_side_effect.map(|level| level.to_string()),
218        recursion_limit: None,
219        tool_arg_constraints: Vec::new(),
220        tool_annotations: std::collections::BTreeMap::new(),
221    }
222}
223
224fn scan_burin_manifest_ceiling(manifest: &serde_json::Value) -> CapabilityPolicy {
225    if let Some(ceiling) = manifest.get("capability_ceiling") {
226        if let Ok(parsed) = serde_json::from_value::<CapabilityPolicy>(ceiling.clone()) {
227            return parsed;
228        }
229    }
230    let tools = manifest
231        .get("tools")
232        .and_then(|value| value.as_array())
233        .map(|tools| {
234            tools
235                .iter()
236                .filter_map(|tool| tool.as_str().map(str::to_string))
237                .collect::<Vec<_>>()
238        })
239        .unwrap_or_default();
240
241    CapabilityPolicy {
242        tools,
243        capabilities: std::collections::BTreeMap::new(),
244        workspace_roots: Vec::new(),
245        side_effect_level: Some("network".to_string()),
246        recursion_limit: None,
247        tool_arg_constraints: Vec::new(),
248        tool_annotations: std::collections::BTreeMap::new(),
249    }
250}
251
252/// Strip line/block comments and the contents of string literals so the
253/// builtin scanner only sees actual source identifiers. Quoted text is
254/// replaced with whitespace of the same length to keep byte offsets and
255/// line numbers stable for any future diagnostics.
256fn strip_comments(source: &str) -> String {
257    let mut out = String::with_capacity(source.len());
258    let mut in_block = false;
259    let mut chars = source.chars().peekable();
260    while let Some(c) = chars.next() {
261        if in_block {
262            if c == '*' && matches!(chars.peek(), Some('/')) {
263                chars.next();
264                in_block = false;
265            }
266            continue;
267        }
268        if c == '/' {
269            match chars.peek() {
270                Some('/') => {
271                    for next in chars.by_ref() {
272                        if next == '\n' {
273                            out.push('\n');
274                            break;
275                        }
276                    }
277                    continue;
278                }
279                Some('*') => {
280                    chars.next();
281                    in_block = true;
282                    continue;
283                }
284                _ => {}
285            }
286        }
287        if c == '#' {
288            for next in chars.by_ref() {
289                if next == '\n' {
290                    out.push('\n');
291                    break;
292                }
293            }
294            continue;
295        }
296        if c == '"' || c == '\'' {
297            out.push(' ');
298            let quote = c;
299            while let Some(next) = chars.next() {
300                if next == '\\' {
301                    chars.next();
302                    out.push(' ');
303                    out.push(' ');
304                    continue;
305                }
306                if next == quote {
307                    out.push(' ');
308                    break;
309                }
310                out.push(if next == '\n' { '\n' } else { ' ' });
311            }
312            continue;
313        }
314        out.push(c);
315    }
316    out
317}
318
319fn contains_call(source: &str, token: &str) -> bool {
320    let bytes = source.as_bytes();
321    let needle = token.as_bytes();
322    if bytes.len() < needle.len() + 1 {
323        return false;
324    }
325    let mut start = 0;
326    while let Some(pos) = find_subslice(&bytes[start..], needle) {
327        let absolute = start + pos;
328        let before = if absolute == 0 {
329            None
330        } else {
331            Some(bytes[absolute - 1])
332        };
333        let after = bytes.get(absolute + needle.len()).copied();
334        let valid_before = match before {
335            None => true,
336            Some(c) => !is_identifier_byte(c),
337        };
338        let valid_after = matches!(after, Some(b'(') | Some(b' ') | Some(b'\t'))
339            || matches!(after, Some(b'\n') | Some(b'\r'));
340        if valid_before && valid_after {
341            return true;
342        }
343        start = absolute + needle.len();
344    }
345    false
346}
347
348fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
349    if needle.is_empty() || haystack.len() < needle.len() {
350        return None;
351    }
352    haystack
353        .windows(needle.len())
354        .position(|window| window == needle)
355}
356
357fn is_identifier_byte(b: u8) -> bool {
358    b.is_ascii_alphanumeric() || b == b'_'
359}
360
361const BUILTIN_CAPABILITIES: &[(&str, &str, &str, &str)] = &[
362    ("read_file", "workspace", "read_text", "read_only"),
363    ("read_file_result", "workspace", "read_text", "read_only"),
364    ("read_file_bytes", "workspace", "read_text", "read_only"),
365    ("list_dir", "workspace", "list", "read_only"),
366    ("file_exists", "workspace", "exists", "read_only"),
367    ("stat", "workspace", "exists", "read_only"),
368    ("write_file", "workspace", "write_text", "workspace_write"),
369    (
370        "write_file_bytes",
371        "workspace",
372        "write_text",
373        "workspace_write",
374    ),
375    ("append_file", "workspace", "write_text", "workspace_write"),
376    ("mkdir", "workspace", "write_text", "workspace_write"),
377    ("copy_file", "workspace", "write_text", "workspace_write"),
378    ("delete_file", "workspace", "delete", "workspace_write"),
379    ("apply_edit", "workspace", "apply_edit", "workspace_write"),
380    ("exec", "process", "exec", "process_exec"),
381    ("exec_at", "process", "exec", "process_exec"),
382    ("shell", "process", "exec", "process_exec"),
383    ("shell_at", "process", "exec", "process_exec"),
384    ("http_get", "network", "http", "network"),
385    ("http_post", "network", "http", "network"),
386    ("http_put", "network", "http", "network"),
387    ("http_patch", "network", "http", "network"),
388    ("http_delete", "network", "http", "network"),
389    ("http_request", "network", "http", "network"),
390    ("http_download", "network", "http", "network"),
391    ("connector_call", "connector", "call", "network"),
392    ("secret_get", "connector", "secret_get", "read_only"),
393    ("llm_call", "llm", "call", "network"),
394    ("llm_call_safe", "llm", "call", "network"),
395    ("llm_completion", "llm", "call", "network"),
396    ("llm_stream", "llm", "call", "network"),
397    ("agent_loop", "llm", "call", "network"),
398    ("mcp_call", "process", "exec", "process_exec"),
399    ("mcp_connect", "process", "exec", "process_exec"),
400];
401
402#[cfg(test)]
403mod tests {
404    use super::*;
405    use std::collections::BTreeMap;
406
407    fn permissive_parent() -> CapabilityPolicy {
408        let mut capabilities = BTreeMap::new();
409        capabilities.insert(
410            "workspace".to_string(),
411            vec!["read_text".to_string(), "list".to_string()],
412        );
413        capabilities.insert("connector".to_string(), vec!["call".to_string()]);
414        capabilities.insert("process".to_string(), vec!["exec".to_string()]);
415        capabilities.insert("network".to_string(), vec!["http".to_string()]);
416        capabilities.insert("llm".to_string(), vec!["call".to_string()]);
417        CapabilityPolicy {
418            tools: Vec::new(),
419            capabilities,
420            workspace_roots: Vec::new(),
421            side_effect_level: Some("network".to_string()),
422            recursion_limit: None,
423            tool_arg_constraints: Vec::new(),
424            tool_annotations: BTreeMap::new(),
425        }
426    }
427
428    fn read_only_parent() -> CapabilityPolicy {
429        let mut capabilities = BTreeMap::new();
430        capabilities.insert(
431            "workspace".to_string(),
432            vec![
433                "read_text".to_string(),
434                "list".to_string(),
435                "exists".to_string(),
436            ],
437        );
438        CapabilityPolicy {
439            tools: Vec::new(),
440            capabilities,
441            workspace_roots: Vec::new(),
442            side_effect_level: Some("read_only".to_string()),
443            recursion_limit: None,
444            tool_arg_constraints: Vec::new(),
445            tool_annotations: BTreeMap::new(),
446        }
447    }
448
449    #[test]
450    fn harn_script_with_only_reads_passes_under_read_only_parent() {
451        let source = r#"
452            let body = read_file("README.md")
453            let exists = file_exists("Cargo.toml")
454        "#;
455        let report = enforce_nested_invocation_ceiling(
456            &read_only_parent(),
457            &NestedInvocationTarget::HarnScript {
458                path: "test.harn",
459                source,
460            },
461        );
462        assert!(report.allowed(), "{report:#?}");
463    }
464
465    #[test]
466    fn harn_script_with_exec_is_rejected_under_read_only_parent() {
467        let source = r#"
468            let result = exec("ls", ["-la"])
469        "#;
470        let report = enforce_nested_invocation_ceiling(
471            &read_only_parent(),
472            &NestedInvocationTarget::HarnScript {
473                path: "exec.harn",
474                source,
475            },
476        );
477        assert!(!report.allowed());
478        let kinds: Vec<&str> = report.violations.iter().map(|v| v.kind.as_str()).collect();
479        assert!(kinds.contains(&"capability"));
480        assert!(kinds.contains(&"side_effect_level"));
481    }
482
483    #[test]
484    fn harn_script_with_http_is_rejected_under_read_only_parent() {
485        let source = r#"
486            http_get("https://example.com")
487        "#;
488        let report = enforce_nested_invocation_ceiling(
489            &read_only_parent(),
490            &NestedInvocationTarget::HarnScript {
491                path: "http.harn",
492                source,
493            },
494        );
495        assert!(!report.allowed());
496    }
497
498    #[test]
499    fn harn_script_keyword_inside_string_does_not_trigger() {
500        let source = r#"
501            let label = "exec is not invoked here"
502            let body = read_file("README.md")
503        "#;
504        let report = enforce_nested_invocation_ceiling(
505            &read_only_parent(),
506            &NestedInvocationTarget::HarnScript {
507                path: "string.harn",
508                source,
509            },
510        );
511        assert!(
512            report.allowed(),
513            "false positive on quoted token: {report:#?}"
514        );
515    }
516
517    #[test]
518    fn harn_script_keyword_in_comment_is_ignored() {
519        let source = r#"
520            // exec("rm -rf /") is what we used to do but no longer
521            let x = read_file("README.md")
522        "#;
523        let report = enforce_nested_invocation_ceiling(
524            &read_only_parent(),
525            &NestedInvocationTarget::HarnScript {
526                path: "comments.harn",
527                source,
528            },
529        );
530        assert!(
531            report.allowed(),
532            "false positive on commented token: {report:#?}"
533        );
534    }
535
536    #[test]
537    fn workflow_bundle_with_act_auto_is_rejected_under_read_only_parent() {
538        let mut bundle = super::super::workflow_test_fixtures::pr_monitor_bundle();
539        bundle.policy.autonomy_tier = "act_auto".to_string();
540        let report = enforce_nested_invocation_ceiling(
541            &read_only_parent(),
542            &NestedInvocationTarget::WorkflowBundle(&bundle),
543        );
544        assert!(!report.allowed());
545    }
546
547    #[test]
548    fn burin_manifest_with_explicit_ceiling_is_used_directly() {
549        let manifest = serde_json::json!({
550            "id": "burin.harness.repair",
551            "capability_ceiling": {
552                "capabilities": {
553                    "workspace": ["read_text"]
554                },
555                "side_effect_level": "read_only"
556            }
557        });
558        let report = enforce_nested_invocation_ceiling(
559            &read_only_parent(),
560            &NestedInvocationTarget::BurinHarness {
561                manifest: &manifest,
562            },
563        );
564        assert!(report.allowed(), "{report:#?}");
565    }
566
567    #[test]
568    fn burin_manifest_without_ceiling_falls_back_to_network_and_is_rejected() {
569        let manifest = serde_json::json!({"id": "burin.harness.unknown"});
570        let report = enforce_nested_invocation_ceiling(
571            &read_only_parent(),
572            &NestedInvocationTarget::BurinHarness {
573                manifest: &manifest,
574            },
575        );
576        assert!(!report.allowed());
577    }
578
579    #[test]
580    fn permissive_parent_accepts_workflow_bundle() {
581        let bundle = super::super::workflow_test_fixtures::pr_monitor_bundle();
582        let report = enforce_nested_invocation_ceiling(
583            &permissive_parent(),
584            &NestedInvocationTarget::WorkflowBundle(&bundle),
585        );
586        assert!(report.allowed(), "{report:#?}");
587    }
588}