Skip to main content

harn_vm/stdlib/
host.rs

1use crate::value::VmDictExt;
2use std::cell::RefCell;
3use std::collections::BTreeMap;
4use std::sync::Arc;
5use std::time::Instant;
6
7use serde_json::Value as JsonValue;
8
9use crate::stdlib::macros::{harn_builtin, VmBuiltinDef};
10use crate::value::{values_equal, VmError, VmValue};
11use crate::vm::{AsyncBuiltinCtx, Vm};
12
13/// Audited wrapper for `chrono::Utc::now().to_rfc3339()`. Routes through
14/// the testbench leak audit so a paused-clock session can surface every
15/// host capability that observed real wall-clock time.
16pub(crate) fn audited_utc_now_rfc3339(capability_id: &'static str) -> String {
17    let dt: chrono::DateTime<chrono::Utc> =
18        crate::clock_mock::leak_audit::wall_now(capability_id).into();
19    dt.to_rfc3339()
20}
21
22pub(crate) const MODULE_BUILTINS: &[&VmBuiltinDef] = &[
23    &HOST_MOCK_BUILTIN_DEF,
24    &HOST_MOCK_CLEAR_BUILTIN_DEF,
25    &HOST_MOCK_CALLS_BUILTIN_DEF,
26    &HOST_MOCK_PUSH_SCOPE_BUILTIN_DEF,
27    &HOST_MOCK_POP_SCOPE_BUILTIN_DEF,
28    &HOST_CAPABILITIES_BUILTIN_DEF,
29    &HOST_HAS_BUILTIN_DEF,
30    &HOST_CALL_BUILTIN_DEF,
31    &HOST_TOOL_LIST_BUILTIN_DEF,
32    &HOST_TOOL_CALL_BUILTIN_DEF,
33];
34
35#[derive(Clone)]
36struct HostMock {
37    capability: String,
38    operation: String,
39    params: Option<crate::value::DictMap>,
40    result: Option<VmValue>,
41    error: Option<String>,
42}
43
44#[derive(Clone)]
45struct HostMockCall {
46    capability: String,
47    operation: String,
48    params: crate::value::DictMap,
49}
50
51thread_local! {
52    static HOST_MOCKS: RefCell<Vec<HostMock>> = const { RefCell::new(Vec::new()) };
53    static HOST_MOCK_CALLS: RefCell<Vec<HostMockCall>> = const { RefCell::new(Vec::new()) };
54    static HOST_MOCK_SCOPES: RefCell<Vec<(Vec<HostMock>, Vec<HostMockCall>)>> =
55        const { RefCell::new(Vec::new()) };
56}
57
58pub(crate) fn reset_host_state() {
59    HOST_MOCKS.with(|mocks| mocks.borrow_mut().clear());
60    HOST_MOCK_CALLS.with(|calls| calls.borrow_mut().clear());
61    HOST_MOCK_SCOPES.with(|scopes| scopes.borrow_mut().clear());
62}
63
64/// Push the current host-mock state onto an internal stack and start a
65/// fresh empty scope. Paired with `pop_host_mock_scope`. Used by the
66/// `with_host_mocks` helper in `std/testing` to give tests automatic
67/// cleanup, including when the body throws.
68fn push_host_mock_scope() {
69    let mocks = HOST_MOCKS.with(|v| std::mem::take(&mut *v.borrow_mut()));
70    let calls = HOST_MOCK_CALLS.with(|v| std::mem::take(&mut *v.borrow_mut()));
71    HOST_MOCK_SCOPES.with(|v| v.borrow_mut().push((mocks, calls)));
72}
73
74/// Restore the most recently pushed host-mock state, replacing any
75/// mocks or recorded calls accumulated inside the scope. Returns
76/// `false` if there is no saved scope to pop, so callers can surface a
77/// clear "imbalanced scope" error rather than silently no-op'ing.
78fn pop_host_mock_scope() -> bool {
79    let entry = HOST_MOCK_SCOPES.with(|v| v.borrow_mut().pop());
80    match entry {
81        Some((mocks, calls)) => {
82            HOST_MOCKS.with(|v| *v.borrow_mut() = mocks);
83            HOST_MOCK_CALLS.with(|v| *v.borrow_mut() = calls);
84            true
85        }
86        None => false,
87    }
88}
89
90fn capability_manifest_map() -> crate::value::DictMap {
91    let mut root = crate::value::DictMap::new();
92    root.insert(
93        crate::value::intern_key("process"),
94        capability(
95            "Process execution.",
96            &[
97                op("exec", "Execute a process in argv or shell mode."),
98                op(
99                    "spawn",
100                    "Spawn a process non-blocking; returns a handle immediately for poll/wait/kill.",
101                ),
102                op(
103                    "poll",
104                    "Non-blocking snapshot of a spawned process: status, captured stdout/stderr.",
105                ),
106                op(
107                    "wait",
108                    "Await a spawned process to completion (optional timeout_ms); returns final result.",
109                ),
110                op(
111                    "kill",
112                    "Terminate a spawned process by handle and await the status transition.",
113                ),
114                op(
115                    "release",
116                    "Release a spawned-process handle and free its retained output.",
117                ),
118                op("list_shells", "List shells discovered by the host/session."),
119                op(
120                    "get_default_shell",
121                    "Return the selected default shell for this host/session.",
122                ),
123                op(
124                    "set_default_shell",
125                    "Select the default shell for this host/session.",
126                ),
127                op(
128                    "shell_invocation",
129                    "Resolve shell selection and login/interactive flags into argv.",
130                ),
131            ],
132        ),
133    );
134    root.insert(
135        crate::value::intern_key("template"),
136        capability(
137            "Template rendering.",
138            &[op("render", "Render a template file.")],
139        ),
140    );
141    root.insert(
142        crate::value::intern_key("interaction"),
143        capability(
144            "User interaction.",
145            &[op("ask", "Ask the user a question.")],
146        ),
147    );
148    root.insert(
149        crate::value::intern_key("memory"),
150        capability(
151            "Vector-aware memory: host-provided embeddings.",
152            &[op(
153                "embed",
154                "Embed text for semantic recall. Params: {text, model_hint?}. \
155                 Returns {vector: list<float>, model: string, dim: int}.",
156            )],
157        ),
158    );
159    root
160}
161
162fn mocked_operation_entry() -> VmValue {
163    op(
164        "mocked",
165        "Mocked host operation registered at runtime for tests.",
166    )
167    .1
168}
169
170fn ensure_mocked_capability(
171    root: &mut crate::value::DictMap,
172    capability_name: &str,
173    operation_name: &str,
174) {
175    let Some(existing) = root.get(capability_name).cloned() else {
176        root.insert(
177            crate::value::intern_key(capability_name),
178            capability(
179                "Mocked host capability registered at runtime for tests.",
180                &[(operation_name.to_string(), mocked_operation_entry())],
181            ),
182        );
183        return;
184    };
185
186    let Some(existing_dict) = existing.as_dict() else {
187        return;
188    };
189    let mut entry = (*existing_dict).clone();
190    let mut ops = entry
191        .get("ops")
192        .and_then(|value| match value {
193            VmValue::List(list) => Some((**list).clone()),
194            _ => None,
195        })
196        .unwrap_or_default();
197    if !ops.iter().any(|value| value.display() == operation_name) {
198        ops.push(VmValue::String(arcstr::ArcStr::from(
199            operation_name.to_string(),
200        )));
201    }
202
203    let mut operations = entry
204        .get("operations")
205        .and_then(|value| value.as_dict())
206        .map(|dict| (*dict).clone())
207        .unwrap_or_default();
208    operations
209        .entry(crate::value::intern_key(operation_name))
210        .or_insert_with(mocked_operation_entry);
211
212    entry.insert(
213        crate::value::intern_key("ops"),
214        VmValue::List(std::sync::Arc::new(ops)),
215    );
216    entry.insert(
217        crate::value::intern_key("operations"),
218        VmValue::dict(operations),
219    );
220    root.insert(
221        crate::value::intern_key(capability_name),
222        VmValue::dict(entry),
223    );
224}
225
226fn capability_manifest_with_mocks() -> VmValue {
227    let mut root = capability_manifest_map();
228    HOST_MOCKS.with(|mocks| {
229        for host_mock in mocks.borrow().iter() {
230            ensure_mocked_capability(&mut root, &host_mock.capability, &host_mock.operation);
231        }
232    });
233    VmValue::dict(root)
234}
235
236fn op(name: &str, description: &str) -> (String, VmValue) {
237    let mut entry = crate::value::DictMap::new();
238    entry.put_str("description", description);
239    (name.to_string(), VmValue::dict(entry))
240}
241
242fn capability(description: &str, ops: &[(String, VmValue)]) -> VmValue {
243    let mut entry = crate::value::DictMap::new();
244    entry.put_str("description", description);
245    entry.insert(
246        crate::value::intern_key("ops"),
247        VmValue::List(std::sync::Arc::new(
248            ops.iter()
249                .map(|(name, _)| VmValue::String(arcstr::ArcStr::from(name.as_str())))
250                .collect(),
251        )),
252    );
253    let mut op_dict = crate::value::DictMap::new();
254    for (name, op) in ops {
255        op_dict.insert(crate::value::intern_key(name), op.clone());
256    }
257    entry.insert(
258        crate::value::intern_key("operations"),
259        VmValue::dict(op_dict),
260    );
261    VmValue::dict(entry)
262}
263
264pub(crate) fn require_param(params: &crate::value::DictMap, key: &str) -> Result<String, VmError> {
265    params
266        .get(key)
267        .map(|v| v.display())
268        .filter(|v| !v.is_empty())
269        .ok_or_else(|| {
270            VmError::Thrown(VmValue::String(arcstr::ArcStr::from(format!(
271                "host_call: missing required parameter '{key}'"
272            ))))
273        })
274}
275
276fn render_template(
277    path: &str,
278    bindings: Option<&crate::value::DictMap>,
279) -> Result<String, VmError> {
280    let asset = crate::stdlib::template::TemplateAsset::render_target(path).map_err(|msg| {
281        VmError::Thrown(VmValue::String(arcstr::ArcStr::from(format!(
282            "host_call template.render: {msg}"
283        ))))
284    })?;
285    crate::stdlib::template::render_asset_result(&asset, bindings).map_err(VmError::from)
286}
287
288fn params_match(expected: Option<&crate::value::DictMap>, actual: &crate::value::DictMap) -> bool {
289    let Some(expected) = expected else {
290        return true;
291    };
292    expected.iter().all(|(key, value)| {
293        actual
294            .get(key)
295            .is_some_and(|candidate| values_equal(candidate, value))
296    })
297}
298
299fn parse_host_mock(args: &[VmValue]) -> Result<HostMock, VmError> {
300    let capability = args
301        .first()
302        .map(|value| value.display())
303        .unwrap_or_default();
304    let operation = args.get(1).map(|value| value.display()).unwrap_or_default();
305    if capability.is_empty() || operation.is_empty() {
306        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
307            "host_mock: capability and operation are required",
308        ))));
309    }
310
311    let mut params = args
312        .get(3)
313        .and_then(|value| value.as_dict())
314        .map(|dict| (*dict).clone());
315    let mut result = args.get(2).cloned().or(Some(VmValue::Nil));
316    let mut error = None;
317
318    if let Some(config) = args.get(2).and_then(|value| value.as_dict()) {
319        if config.contains_key("result")
320            || config.contains_key("params")
321            || config.contains_key("error")
322        {
323            params = config
324                .get("params")
325                .and_then(|value| value.as_dict())
326                .map(|dict| (*dict).clone());
327            result = config.get("result").cloned();
328            error = config
329                .get("error")
330                .map(|value| value.display())
331                .filter(|value| !value.is_empty());
332        }
333    }
334
335    Ok(HostMock {
336        capability,
337        operation,
338        params,
339        result,
340        error,
341    })
342}
343
344fn push_host_mock(host_mock: HostMock) {
345    HOST_MOCKS.with(|mocks| mocks.borrow_mut().push(host_mock));
346}
347
348fn mock_call_value(call: &HostMockCall) -> VmValue {
349    let mut item = crate::value::DictMap::new();
350    item.put_str("capability", call.capability.clone());
351    item.put_str("operation", call.operation.clone());
352    item.insert(
353        crate::value::intern_key("params"),
354        VmValue::dict(call.params.clone()),
355    );
356    VmValue::dict(item)
357}
358
359fn record_mock_call(capability: &str, operation: &str, params: &crate::value::DictMap) {
360    HOST_MOCK_CALLS.with(|calls| {
361        calls.borrow_mut().push(HostMockCall {
362            capability: capability.to_string(),
363            operation: operation.to_string(),
364            params: params.clone(),
365        });
366    });
367}
368
369pub(crate) fn dispatch_mock_host_call(
370    capability: &str,
371    operation: &str,
372    params: &crate::value::DictMap,
373) -> Option<Result<VmValue, VmError>> {
374    let matched = HOST_MOCKS.with(|mocks| {
375        mocks
376            .borrow()
377            .iter()
378            .rev()
379            .find(|host_mock| {
380                host_mock.capability == capability
381                    && host_mock.operation == operation
382                    && params_match(host_mock.params.as_ref(), params)
383            })
384            .cloned()
385    })?;
386
387    record_mock_call(capability, operation, params);
388    if let Some(error) = matched.error {
389        return Some(Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
390            error,
391        )))));
392    }
393    Some(Ok(matched.result.unwrap_or(VmValue::Nil)))
394}
395
396/// Embedder-supplied bridge for `host_call` ops.
397///
398/// Embedders (debug adapters, CLIs, IDE hosts) implement this trait to
399/// satisfy capability/operation pairs that harn-vm itself doesn't know how
400/// to handle. Returning `Ok(None)` means "I don't handle this op — fall
401/// through to the built-in fallbacks (env-derived defaults, then the
402/// `unsupported operation` error)". `Ok(Some(value))` is the result;
403/// `Err(VmError::Thrown(_))` surfaces as a Harn exception.
404///
405/// The trait is intentionally synchronous. Bridges that need async I/O
406/// (e.g. DAP reverse requests) should drive their own runtime or use a
407/// blocking channel — see `harn-dap`'s `DapHostBridge` for the canonical
408/// pattern. Sync keeps the boundary simple and avoids forcing the entire
409/// dispatch path into an opaque future.
410pub trait HostCallBridge: Send + Sync {
411    fn dispatch(
412        &self,
413        capability: &str,
414        operation: &str,
415        params: &crate::value::DictMap,
416    ) -> Result<Option<VmValue>, VmError>;
417
418    fn list_tools(&self) -> Result<Option<VmValue>, VmError> {
419        Ok(None)
420    }
421
422    fn call_tool(&self, _name: &str, _args: &VmValue) -> Result<Option<VmValue>, VmError> {
423        Ok(None)
424    }
425}
426
427thread_local! {
428    static HOST_CALL_BRIDGE: RefCell<Option<Arc<dyn HostCallBridge>>> = const { RefCell::new(None) };
429}
430
431/// Install a bridge for the current thread. The bridge is consulted on
432/// every `host_call` *after* mock matching but *before* the built-in
433/// match arms, so embedders can override anything they like (and equally
434/// punt on anything they don't, by returning `Ok(None)`).
435pub fn set_host_call_bridge(bridge: Arc<dyn HostCallBridge>) {
436    HOST_CALL_BRIDGE.with(|b| *b.borrow_mut() = Some(bridge));
437}
438
439/// Remove the current thread's bridge. Idempotent.
440pub fn clear_host_call_bridge() {
441    HOST_CALL_BRIDGE.with(|b| *b.borrow_mut() = None);
442}
443
444/// Dispatch `(capability, operation, params)` to the currently-installed
445/// `HostCallBridge`, if any. `Some(Ok(_))` means the bridge handled the
446/// call; `Some(Err(_))` means it tried but raised; `None` means there is
447/// no bridge or the bridge declined this op (returned `Ok(None)`).
448///
449/// Mirrors the inner block of `dispatch_host_operation` but without the
450/// mock-call check or the built-in fallbacks — useful for callers that
451/// want to treat the bridge as one of several sinks (e.g. inbound MCP
452/// `elicitation/create` requests).
453pub fn dispatch_host_call_bridge(
454    capability: &str,
455    operation: &str,
456    params: &crate::value::DictMap,
457) -> Option<Result<VmValue, VmError>> {
458    let bridge = HOST_CALL_BRIDGE.with(|b| b.borrow().clone())?;
459    match bridge.dispatch(capability, operation, params) {
460        Ok(Some(value)) => Some(Ok(value)),
461        Ok(None) => None,
462        Err(error) => Some(Err(error)),
463    }
464}
465
466fn empty_tool_list_value() -> VmValue {
467    VmValue::List(std::sync::Arc::new(Vec::new()))
468}
469
470fn current_vm_host_bridge(
471    ctx: Option<&AsyncBuiltinCtx>,
472) -> Option<std::sync::Arc<crate::bridge::HostBridge>> {
473    ctx.and_then(|ctx| ctx.child_vm().bridge.clone())
474}
475
476#[cfg(test)]
477async fn dispatch_host_tool_list() -> Result<VmValue, VmError> {
478    dispatch_host_tool_list_with_ctx(None).await
479}
480
481async fn dispatch_host_tool_list_with_ctx(
482    ctx: Option<&AsyncBuiltinCtx>,
483) -> Result<VmValue, VmError> {
484    let bridge = HOST_CALL_BRIDGE.with(|b| b.borrow().clone());
485    if let Some(bridge) = bridge {
486        if let Some(value) = bridge.list_tools()? {
487            return Ok(value);
488        }
489    }
490
491    let Some(bridge) = current_vm_host_bridge(ctx) else {
492        return Ok(empty_tool_list_value());
493    };
494    let tools = bridge.list_host_tools().await?;
495    Ok(crate::bridge::json_result_to_vm_value(&JsonValue::Array(
496        tools.into_iter().collect(),
497    )))
498}
499
500pub(crate) async fn dispatch_host_tool_call(
501    name: &str,
502    args: &VmValue,
503) -> Result<VmValue, VmError> {
504    dispatch_host_tool_call_with_ctx(None, name, args).await
505}
506
507pub(crate) async fn dispatch_host_tool_call_with_ctx(
508    ctx: Option<&AsyncBuiltinCtx>,
509    name: &str,
510    args: &VmValue,
511) -> Result<VmValue, VmError> {
512    let bridge = HOST_CALL_BRIDGE.with(|b| b.borrow().clone());
513    if let Some(bridge) = bridge {
514        if let Some(value) = bridge.call_tool(name, args)? {
515            return Ok(value);
516        }
517    }
518
519    let Some(bridge) = current_vm_host_bridge(ctx) else {
520        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
521            "host_tool_call: no host bridge is attached",
522        ))));
523    };
524
525    let result = bridge
526        .call(
527            "builtin_call",
528            serde_json::json!({
529                "name": name,
530                "args": [crate::llm::vm_value_to_json(args)],
531            }),
532        )
533        .await?;
534    Ok(crate::bridge::json_result_to_vm_value(&result))
535}
536
537pub(crate) async fn dispatch_host_operation(
538    capability: &str,
539    operation: &str,
540    params: &crate::value::DictMap,
541) -> Result<VmValue, VmError> {
542    dispatch_host_operation_with_ctx(None, capability, operation, params).await
543}
544
545pub(crate) async fn dispatch_host_operation_with_ctx(
546    ctx: Option<&AsyncBuiltinCtx>,
547    capability: &str,
548    operation: &str,
549    params: &crate::value::DictMap,
550) -> Result<VmValue, VmError> {
551    if let Some(mocked) = dispatch_mock_host_call(capability, operation, params) {
552        return mocked;
553    }
554
555    if (capability, operation) == ("process", "exec") {
556        let caller = serde_json::json!({
557            "surface": "host_call",
558            "capability": "process",
559            "operation": "exec",
560            "session_id": crate::llm::current_agent_session_id(),
561        });
562        return dispatch_process_exec_with_policy(ctx, params, caller).await;
563    }
564
565    // process.spawn is the non-blocking sibling of exec. Route it through the
566    // SAME command-policy preflight so deny-patterns/approval/sandbox gating
567    // are identical; only the completion semantics differ (returns a handle
568    // immediately instead of awaiting). poll/wait/kill/release are pure
569    // registry operations on an already-gated spawn, so they bypass the
570    // command policy.
571    if (capability, operation) == ("process", "spawn") {
572        let caller = serde_json::json!({
573            "surface": "host_call",
574            "capability": "process",
575            "operation": "spawn",
576            "session_id": crate::llm::current_agent_session_id(),
577        });
578        return dispatch_process_spawn_with_policy(ctx, params, caller).await;
579    }
580    if capability == "process" && matches!(operation, "poll" | "wait" | "kill" | "release") {
581        if let Some(result) = crate::stdlib::process_spawn::dispatch(operation, params).await {
582            return result;
583        }
584    }
585
586    let bridge = HOST_CALL_BRIDGE.with(|b| b.borrow().clone());
587    if let Some(bridge) = bridge {
588        if let Some(value) = bridge.dispatch(capability, operation, params)? {
589            return Ok(value);
590        }
591    }
592
593    dispatch_builtin_host_operation(capability, operation, params).await
594}
595
596async fn dispatch_builtin_host_operation(
597    capability: &str,
598    operation: &str,
599    params: &crate::value::DictMap,
600) -> Result<VmValue, VmError> {
601    match (capability, operation) {
602        ("process", "list_shells") => Ok(crate::shells::list_shells_vm_value()),
603        ("process", "get_default_shell") => Ok(crate::shells::default_shell_vm_value()),
604        ("process", "set_default_shell") => crate::shells::set_default_shell_vm_value(params),
605        ("process", "shell_invocation") => crate::shells::shell_invocation_vm_value(params),
606        ("template", "render") => {
607            let path = require_param(params, "path")?;
608            let bindings = params.get("bindings").and_then(|v| v.as_dict());
609            Ok(VmValue::String(arcstr::ArcStr::from(render_template(
610                &path, bindings,
611            )?)))
612        }
613        ("interaction", "ask") => {
614            let question = require_param(params, "question")?;
615            use std::io::BufRead;
616            print!("{question}");
617            let _ = std::io::Write::flush(&mut std::io::stdout());
618            let mut input = String::new();
619            if std::io::stdin().lock().read_line(&mut input).is_ok() {
620                Ok(VmValue::String(arcstr::ArcStr::from(input.trim_end())))
621            } else {
622                Ok(VmValue::Nil)
623            }
624        }
625        // Standalone-run fallbacks for capabilities normally supplied by
626        // an embedder's JSON-RPC bridge. `runtime.task` lets a debugger or
627        // CLI invocation read the pipeline input from `HARN_TASK` without
628        // the host explicitly wiring a callback for every op.
629        ("runtime", "task") => Ok(VmValue::String(arcstr::ArcStr::from(
630            std::env::var("HARN_TASK").unwrap_or_default(),
631        ))),
632        ("runtime", "set_result") => {
633            // No-op when no host is attached; swallow silently so standalone
634            // scripts can still call `set_result` without crashing.
635            Ok(VmValue::Nil)
636        }
637        ("workspace", "project_root") => {
638            // Standalone fallback: prefer HARN_PROJECT_ROOT, then the
639            // current working directory. Pipelines call this very early so
640            // crashing here would block any debug-launched script.
641            let path = std::env::var("HARN_PROJECT_ROOT").unwrap_or_else(|_| {
642                std::env::current_dir()
643                    .map(|p| p.display().to_string())
644                    .unwrap_or_default()
645            });
646            Ok(VmValue::String(arcstr::ArcStr::from(path)))
647        }
648        ("workspace", "cwd") => {
649            let path = std::env::current_dir()
650                .map(|p| p.display().to_string())
651                .unwrap_or_default();
652            Ok(VmValue::String(arcstr::ArcStr::from(path)))
653        }
654        _ => Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
655            format!("host_call: unsupported operation {capability}.{operation}"),
656        )))),
657    }
658}
659
660pub(crate) async fn dispatch_process_exec(
661    params: &crate::value::DictMap,
662    caller: serde_json::Value,
663) -> Result<VmValue, VmError> {
664    dispatch_process_exec_with_policy(None, params, caller).await
665}
666
667async fn dispatch_process_exec_with_policy(
668    ctx: Option<&AsyncBuiltinCtx>,
669    params: &crate::value::DictMap,
670    caller: serde_json::Value,
671) -> Result<VmValue, VmError> {
672    let (params, command_policy_context, command_policy_decisions) =
673        match crate::orchestration::run_command_policy_preflight_with_ctx(ctx, params, caller)
674            .await?
675        {
676            crate::orchestration::CommandPolicyPreflight::Proceed {
677                params,
678                context,
679                decisions,
680            } => (params, context, decisions),
681            crate::orchestration::CommandPolicyPreflight::Blocked {
682                status,
683                message,
684                context,
685                decisions,
686            } => {
687                return Ok(crate::orchestration::blocked_command_response(
688                    params, status, &message, context, decisions,
689                ));
690            }
691        };
692
693    let bridge = HOST_CALL_BRIDGE.with(|b| b.borrow().clone());
694    if let Some(bridge) = bridge {
695        if let Some(value) = bridge.dispatch("process", "exec", &params)? {
696            return crate::orchestration::run_command_policy_postflight_with_ctx(
697                ctx,
698                &params,
699                value,
700                command_policy_context,
701                command_policy_decisions,
702            )
703            .await;
704        }
705    }
706
707    dispatch_process_exec_after_policy(
708        ctx,
709        &params,
710        command_policy_context,
711        command_policy_decisions,
712    )
713    .await
714}
715
716/// Apply the command-policy preflight (deny-patterns, approval gating,
717/// sandbox decisions) and then spawn the process non-blocking. Mirrors
718/// [`dispatch_process_exec_with_policy`] so spawn is gated identically to
719/// exec. There is no postflight here: spawn returns a handle immediately,
720/// not a completed command result; completion is observed later via
721/// poll/wait, which are not themselves command executions.
722async fn dispatch_process_spawn_with_policy(
723    ctx: Option<&AsyncBuiltinCtx>,
724    params: &crate::value::DictMap,
725    caller: serde_json::Value,
726) -> Result<VmValue, VmError> {
727    let params =
728        match crate::orchestration::run_command_policy_preflight_with_ctx(ctx, params, caller)
729            .await?
730        {
731            crate::orchestration::CommandPolicyPreflight::Proceed { params, .. } => params,
732            crate::orchestration::CommandPolicyPreflight::Blocked {
733                status,
734                message,
735                context,
736                decisions,
737            } => {
738                return Ok(crate::orchestration::blocked_command_response(
739                    params, status, &message, context, decisions,
740                ));
741            }
742        };
743
744    match crate::stdlib::process_spawn::dispatch("spawn", &params).await {
745        Some(result) => result,
746        None => Err(VmError::Runtime(
747            "host_call process.spawn: dispatch returned None".to_string(),
748        )),
749    }
750}
751
752async fn dispatch_process_exec_after_policy(
753    ctx: Option<&AsyncBuiltinCtx>,
754    params: &crate::value::DictMap,
755    command_policy_context: JsonValue,
756    command_policy_decisions: Vec<crate::orchestration::CommandPolicyDecision>,
757) -> Result<VmValue, VmError> {
758    let timeout_ms = optional_i64(params, "timeout")
759        .or_else(|| optional_i64(params, "timeout_ms"))
760        .filter(|value| *value > 0)
761        .map(|value| value as u64);
762    // Optional per-call profile override. Pipelines that want to
763    // promote a single spawn to `os_hardened` (e.g. running
764    // attacker-controlled code) pass `sandbox_profile: "os_hardened"`
765    // without having to rewrite the surrounding policy. The override
766    // is scoped to this call and pops with the guard at end-of-scope.
767    let profile_guard = match optional_string(params, "sandbox_profile") {
768        Some(value) => Some(push_sandbox_profile_override(&value)?),
769        None => None,
770    };
771    let mut cmd = build_sandboxed_command(params, "process.exec")?;
772    cmd.stdin(std::process::Stdio::null())
773        .stdout(std::process::Stdio::piped())
774        .stderr(std::process::Stdio::piped())
775        .kill_on_drop(true);
776    let started_at = audited_utc_now_rfc3339("host_call/process.exec.started_at");
777    let started = crate::clock_mock::leak_audit::instant_now("host_call/process.exec.started");
778    let child = cmd
779        .spawn()
780        .map_err(|e| VmError::Runtime(format!("host_call process.exec: {e}")))?;
781    drop(profile_guard);
782    let pid = child.id();
783    let timed_out;
784    let output_result = if let Some(timeout_ms) = timeout_ms {
785        match tokio::time::timeout(
786            std::time::Duration::from_millis(timeout_ms),
787            child.wait_with_output(),
788        )
789        .await
790        {
791            Ok(result) => {
792                timed_out = false;
793                result
794            }
795            Err(_) => {
796                let response = process_exec_response(ProcessExecResponse {
797                    pid,
798                    started_at,
799                    started,
800                    stdout: "",
801                    stderr: "",
802                    exit_code: -1,
803                    status: "timed_out",
804                    success: false,
805                    timed_out: true,
806                });
807                return crate::orchestration::run_command_policy_postflight_with_ctx(
808                    ctx,
809                    params,
810                    response,
811                    command_policy_context,
812                    command_policy_decisions,
813                )
814                .await;
815            }
816        }
817    } else {
818        timed_out = false;
819        child.wait_with_output().await
820    };
821    let output =
822        output_result.map_err(|e| VmError::Runtime(format!("host_call process.exec: {e}")))?;
823    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
824    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
825    let exit_code = output.status.code().unwrap_or(-1);
826    let response = process_exec_response(ProcessExecResponse {
827        pid,
828        started_at,
829        started,
830        stdout: &stdout,
831        stderr: &stderr,
832        exit_code,
833        status: if timed_out { "timed_out" } else { "completed" },
834        success: output.status.success(),
835        timed_out,
836    });
837    crate::orchestration::run_command_policy_postflight_with_ctx(
838        ctx,
839        params,
840        response,
841        command_policy_context,
842        command_policy_decisions,
843    )
844    .await
845}
846
847/// Build a sandboxed `tokio::process::Command` from process-call params,
848/// applying argv/shell resolution, the active sandbox policy via
849/// [`crate::process_sandbox::tokio_command_for`], cwd enforcement, and
850/// env/env_mode/env_remove handling.
851///
852/// Shared by `process.exec` (synchronous) and `process.spawn`
853/// (non-blocking) so both go through the identical sandbox-gated build
854/// path. The caller is responsible for any `sandbox_profile` override
855/// guard (it must be live across this call) and for setting stdio/kill
856/// behaviour on the returned command. `label` ("process.exec" or
857/// "process.spawn") is woven into error messages.
858pub(crate) fn build_sandboxed_command(
859    params: &crate::value::DictMap,
860    label: &str,
861) -> Result<tokio::process::Command, VmError> {
862    let (program, args) = process_exec_argv(params)?;
863    let mut cmd = crate::process_sandbox::tokio_command_for(&program, &args)
864        .map_err(|e| VmError::Runtime(format!("host_call {label} sandbox setup: {e}")))?;
865    if let Some(cwd) = optional_string(params, "cwd") {
866        let cwd = resolve_process_exec_cwd(&cwd);
867        crate::process_sandbox::enforce_process_cwd(&cwd)
868            .map_err(|e| VmError::Runtime(format!("host_call {label} cwd: {e}")))?;
869        cmd.current_dir(cwd);
870    }
871    // Track keys the caller set explicitly so the sandbox-local TMPDIR overlay
872    // below never clobbers an intentional per-call value.
873    let mut caller_env_keys: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
874    if let Some(env) = optional_string_dict(params, "env")? {
875        // `env_mode` controls how the provided `env` keys combine with the
876        // parent environment:
877        //   - "merge" (default): inherit the parent env and overlay the
878        //     provided keys. This is the least-surprising behavior — a
879        //     caller passing `env: {ONE_VAR: "x"}` keeps PATH/HOME/etc.
880        //   - "replace": clear the parent env entirely, then set only the
881        //     provided keys. Must be requested explicitly now; previously
882        //     this was the (footgun) default whenever `env` was supplied.
883        let env_mode = optional_string(params, "env_mode");
884        match env_mode.as_deref().unwrap_or("merge") {
885            "replace" => {
886                cmd.env_clear();
887            }
888            "merge" => {}
889            other => {
890                return Err(VmError::Runtime(format!(
891                    "host_call {label}: unknown env_mode {other:?}; expected \"merge\" or \"replace\""
892                )));
893            }
894        }
895        for (key, value) in env {
896            caller_env_keys.insert(key.clone());
897            cmd.env(key, value);
898        }
899    }
900    // env_remove: list of environment variable names to strip before
901    // spawning. Applied after `env` so callers can both inherit and
902    // selectively unset (e.g. the git stdlib strips `GIT_*` so its
903    // operations are self-contained even when Harn is invoked from
904    // inside a git hook that sets `GIT_DIR`).
905    if let Some(env_remove) = optional_string_list(params, "env_remove") {
906        for key in env_remove {
907            caller_env_keys.insert(key.clone());
908            cmd.env_remove(key);
909        }
910    }
911    // Point the child's temp dir at a sandbox-writable, workspace-local
912    // location so compiler linkers (rustc/cc/ld, Go, Swift, …) and other
913    // toolchains that honor TMPDIR/TMP/TEMP don't false-fail trying to write
914    // intermediates to the unwritable system /tmp. A key the caller set (via
915    // `env`) or explicitly stripped (via `env_remove`) is left as the caller
916    // intended; only keys the caller did not touch receive the overlay. No-op
917    // when the active profile is unrestricted or no writable workspace root is
918    // available.
919    for (key, value) in crate::process_sandbox::active_workspace_tmpdir_env() {
920        if caller_env_keys.contains(&key) {
921            continue;
922        }
923        cmd.env(key, value);
924    }
925    Ok(cmd)
926}
927
928struct ProcessExecResponse<'a> {
929    pid: Option<u32>,
930    started_at: String,
931    started: Instant,
932    stdout: &'a str,
933    stderr: &'a str,
934    exit_code: i32,
935    status: &'a str,
936    success: bool,
937    timed_out: bool,
938}
939
940fn process_exec_response(response: ProcessExecResponse<'_>) -> VmValue {
941    let combined = format!("{}{}", response.stdout, response.stderr);
942    let mut result = crate::value::DictMap::new();
943    result.put_str(
944        "command_id",
945        format!(
946            "cmd_{}_{}",
947            std::process::id(),
948            response.started.elapsed().as_nanos()
949        ),
950    );
951    result.put_str("status", response.status);
952    result.insert(
953        crate::value::intern_key("pid"),
954        response
955            .pid
956            .map(|pid| VmValue::Int(pid as i64))
957            .unwrap_or(VmValue::Nil),
958    );
959    result.insert(
960        crate::value::intern_key("process_group_id"),
961        response
962            .pid
963            .map(|pid| VmValue::Int(pid as i64))
964            .unwrap_or(VmValue::Nil),
965    );
966    result.insert(crate::value::intern_key("handle_id"), VmValue::Nil);
967    result.put_str("started_at", response.started_at);
968    result.put_str(
969        "ended_at",
970        audited_utc_now_rfc3339("host_call/process.exec.ended_at"),
971    );
972    result.insert(
973        crate::value::intern_key("duration_ms"),
974        VmValue::Int(response.started.elapsed().as_millis() as i64),
975    );
976    result.insert(
977        crate::value::intern_key("exit_code"),
978        VmValue::Int(response.exit_code as i64),
979    );
980    result.insert(crate::value::intern_key("signal"), VmValue::Nil);
981    result.insert(
982        crate::value::intern_key("timed_out"),
983        VmValue::Bool(response.timed_out),
984    );
985    result.put_str("stdout", response.stdout);
986    result.put_str("stderr", response.stderr);
987    result.put_str("combined", combined);
988    result.insert(
989        crate::value::intern_key("exit_status"),
990        VmValue::Int(response.exit_code as i64),
991    );
992    result.insert(
993        crate::value::intern_key("legacy_status"),
994        VmValue::Int(response.exit_code as i64),
995    );
996    result.insert(
997        crate::value::intern_key("success"),
998        VmValue::Bool(response.success),
999    );
1000    VmValue::dict(result)
1001}
1002
1003fn resolve_process_exec_cwd(cwd: &str) -> std::path::PathBuf {
1004    crate::stdlib::process::resolve_source_relative_path(cwd)
1005}
1006
1007fn process_exec_argv(params: &crate::value::DictMap) -> Result<(String, Vec<String>), VmError> {
1008    match optional_string(params, "mode")
1009        .as_deref()
1010        .unwrap_or("shell")
1011    {
1012        "argv" => {
1013            let argv = optional_string_list(params, "argv").ok_or_else(|| {
1014                VmError::Runtime("host_call process.exec missing argv".to_string())
1015            })?;
1016            split_argv(argv)
1017        }
1018        "shell" => {
1019            let command = require_param(params, "command")?;
1020            let mut invocation_params = params.clone();
1021            invocation_params.put_str("command", command);
1022            let invocation =
1023                crate::shells::resolve_invocation_from_vm_params(&invocation_params)
1024                    .map_err(|err| VmError::Runtime(format!("host_call process.exec: {err}")))?;
1025            Ok((invocation.program, invocation.args))
1026        }
1027        other => Err(VmError::Runtime(format!(
1028            "host_call process.exec unsupported mode {other:?}"
1029        ))),
1030    }
1031}
1032
1033fn split_argv(mut argv: Vec<String>) -> Result<(String, Vec<String>), VmError> {
1034    if argv.is_empty() {
1035        return Err(VmError::Runtime(
1036            "host_call process.exec argv must not be empty".to_string(),
1037        ));
1038    }
1039    let program = argv.remove(0);
1040    if program.is_empty() {
1041        return Err(VmError::Runtime(
1042            "host_call process.exec argv[0] must not be empty".to_string(),
1043        ));
1044    }
1045    Ok((program, argv))
1046}
1047
1048/// Push a transient policy onto the execution stack with the
1049/// requested sandbox profile, returning a guard that pops on drop.
1050/// Used by `host_call("process", "exec", ...)` to honor a per-call
1051/// `sandbox_profile` override without rewriting the surrounding
1052/// orchestration policy.
1053pub(crate) fn push_sandbox_profile_override(value: &str) -> Result<SandboxProfileGuard, VmError> {
1054    let profile = crate::orchestration::SandboxProfile::parse(value).ok_or_else(|| {
1055        VmError::Thrown(VmValue::String(arcstr::ArcStr::from(format!(
1056            "host_call process.exec: unknown sandbox_profile {value:?}; expected one of \"unrestricted\", \"worktree\", \"os_hardened\", \"wasi\""
1057        ))))
1058    })?;
1059    let mut policy = crate::orchestration::current_execution_policy().unwrap_or_default();
1060    policy.sandbox_profile = profile;
1061    crate::orchestration::push_execution_policy(policy);
1062    Ok(SandboxProfileGuard {
1063        _private: std::marker::PhantomData,
1064    })
1065}
1066
1067pub(crate) struct SandboxProfileGuard {
1068    _private: std::marker::PhantomData<*const ()>,
1069}
1070
1071impl Drop for SandboxProfileGuard {
1072    fn drop(&mut self) {
1073        crate::orchestration::pop_execution_policy();
1074    }
1075}
1076
1077pub(crate) fn optional_i64(params: &crate::value::DictMap, key: &str) -> Option<i64> {
1078    match params.get(key) {
1079        Some(VmValue::Int(value)) => Some(*value),
1080        Some(VmValue::Float(value)) if value.fract() == 0.0 => Some(*value as i64),
1081        _ => None,
1082    }
1083}
1084
1085pub(crate) fn optional_string(params: &crate::value::DictMap, key: &str) -> Option<String> {
1086    params.get(key).and_then(vm_string).map(ToString::to_string)
1087}
1088
1089fn optional_string_list(params: &crate::value::DictMap, key: &str) -> Option<Vec<String>> {
1090    let VmValue::List(values) = params.get(key)? else {
1091        return None;
1092    };
1093    values
1094        .iter()
1095        .map(|value| vm_string(value).map(ToString::to_string))
1096        .collect()
1097}
1098
1099fn optional_string_dict(
1100    params: &crate::value::DictMap,
1101    key: &str,
1102) -> Result<Option<BTreeMap<String, String>>, VmError> {
1103    let Some(value) = params.get(key) else {
1104        return Ok(None);
1105    };
1106    let Some(dict) = value.as_dict() else {
1107        return Err(VmError::Runtime(format!(
1108            "host_call process.exec {key} must be a dict"
1109        )));
1110    };
1111    let mut out = std::collections::BTreeMap::new();
1112    for (key, value) in dict.iter() {
1113        let Some(value) = vm_string(value) else {
1114            return Err(VmError::Runtime(format!(
1115                "host_call process.exec env value for {key:?} must be a string"
1116            )));
1117        };
1118        out.insert(key.to_string(), value.to_string());
1119    }
1120    Ok(Some(out))
1121}
1122
1123fn vm_string(value: &VmValue) -> Option<&str> {
1124    match value {
1125        VmValue::String(value) => Some(value.as_ref()),
1126        _ => None,
1127    }
1128}
1129
1130pub(crate) fn register_host_builtins(vm: &mut Vm) {
1131    for def in MODULE_BUILTINS {
1132        vm.register_builtin_def(def);
1133    }
1134}
1135
1136#[harn_builtin(
1137    sig = "host_mock(capability: string, op: string, response_or_config?: any, params?: dict) -> nil",
1138    category = "host"
1139)]
1140fn host_mock_builtin(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1141    let host_mock = parse_host_mock(args)?;
1142    push_host_mock(host_mock);
1143    Ok(VmValue::Nil)
1144}
1145
1146#[harn_builtin(sig = "host_mock_clear() -> nil", category = "host")]
1147fn host_mock_clear_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1148    reset_host_state();
1149    Ok(VmValue::Nil)
1150}
1151
1152#[harn_builtin(sig = "host_mock_calls() -> list", category = "host")]
1153fn host_mock_calls_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1154    let calls = HOST_MOCK_CALLS.with(|calls| {
1155        calls
1156            .borrow()
1157            .iter()
1158            .map(mock_call_value)
1159            .collect::<Vec<_>>()
1160    });
1161    Ok(VmValue::List(std::sync::Arc::new(calls)))
1162}
1163
1164#[harn_builtin(sig = "host_mock_push_scope() -> nil", category = "host")]
1165fn host_mock_push_scope_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1166    push_host_mock_scope();
1167    Ok(VmValue::Nil)
1168}
1169
1170#[harn_builtin(sig = "host_mock_pop_scope() -> nil", category = "host")]
1171fn host_mock_pop_scope_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1172    if !pop_host_mock_scope() {
1173        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
1174            "host_mock_pop_scope: no scope to pop",
1175        ))));
1176    }
1177    Ok(VmValue::Nil)
1178}
1179
1180#[harn_builtin(sig = "host_capabilities() -> dict", category = "host")]
1181fn host_capabilities_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1182    Ok(capability_manifest_with_mocks())
1183}
1184
1185#[harn_builtin(
1186    sig = "host_has(capability: string, op?: string) -> bool",
1187    category = "host"
1188)]
1189fn host_has_builtin(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1190    let capability = args.first().map(|a| a.display()).unwrap_or_default();
1191    let operation = args.get(1).map(|a| a.display());
1192    let manifest = capability_manifest_with_mocks();
1193    let has = manifest
1194        .as_dict()
1195        .and_then(|d| d.get(capability.as_str()))
1196        .and_then(|v| v.as_dict())
1197        .is_some_and(|cap| {
1198            if let Some(operation) = operation {
1199                cap.get("ops")
1200                    .and_then(|v| match v {
1201                        VmValue::List(list) => {
1202                            Some(list.iter().any(|item| item.display() == operation))
1203                        }
1204                        _ => None,
1205                    })
1206                    .unwrap_or(false)
1207            } else {
1208                true
1209            }
1210        });
1211    Ok(VmValue::Bool(has))
1212}
1213
1214#[harn_builtin(
1215    sig = "host_call(name: string, args?: dict) -> any",
1216    kind = "async",
1217    category = "host"
1218)]
1219async fn host_call_builtin(
1220    ctx: crate::vm::AsyncBuiltinCtx,
1221    args: Vec<VmValue>,
1222) -> Result<VmValue, VmError> {
1223    let name = args.first().map(|a| a.display()).unwrap_or_default();
1224    let params = args
1225        .get(1)
1226        .and_then(|a| a.as_dict())
1227        .cloned()
1228        .unwrap_or_default();
1229    let Some((capability, operation)) = name.split_once('.') else {
1230        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
1231            format!("host_call: unsupported operation name '{name}'"),
1232        ))));
1233    };
1234    dispatch_host_operation_with_ctx(Some(&ctx), capability, operation, &params).await
1235}
1236
1237#[harn_builtin(sig = "host_tool_list() -> list", kind = "async", category = "host")]
1238async fn host_tool_list_builtin(
1239    ctx: crate::vm::AsyncBuiltinCtx,
1240    _args: Vec<VmValue>,
1241) -> Result<VmValue, VmError> {
1242    dispatch_host_tool_list_with_ctx(Some(&ctx)).await
1243}
1244
1245#[harn_builtin(
1246    sig = "host_tool_call(name: string, args?: any) -> any",
1247    kind = "async",
1248    category = "host"
1249)]
1250async fn host_tool_call_builtin(
1251    ctx: crate::vm::AsyncBuiltinCtx,
1252    args: Vec<VmValue>,
1253) -> Result<VmValue, VmError> {
1254    let name = args.first().map(|a| a.display()).unwrap_or_default();
1255    if name.is_empty() {
1256        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
1257            "host_tool_call: tool name is required",
1258        ))));
1259    }
1260    let call_args = args.get(1).cloned().unwrap_or(VmValue::Nil);
1261    dispatch_host_tool_call_with_ctx(Some(&ctx), &name, &call_args).await
1262}
1263
1264#[cfg(test)]
1265mod tests {
1266    use super::{
1267        capability_manifest_with_mocks, clear_host_call_bridge, dispatch_host_operation,
1268        dispatch_host_tool_call, dispatch_host_tool_list, dispatch_mock_host_call, push_host_mock,
1269        reset_host_state, resolve_process_exec_cwd, set_host_call_bridge, HostCallBridge, HostMock,
1270    };
1271    use crate::value::VmDictExt;
1272
1273    use std::sync::{
1274        atomic::{AtomicUsize, Ordering},
1275        Arc,
1276    };
1277
1278    use crate::value::{VmError, VmValue};
1279
1280    #[test]
1281    fn process_exec_relative_cwd_resolves_against_execution_root() {
1282        let dir = tempfile::tempdir().expect("tempdir");
1283        crate::stdlib::process::set_thread_execution_context(Some(
1284            crate::orchestration::RunExecutionRecord {
1285                cwd: Some(dir.path().to_string_lossy().into_owned()),
1286                source_dir: Some(dir.path().join("src").to_string_lossy().into_owned()),
1287                env: std::collections::BTreeMap::new(),
1288                adapter: None,
1289                repo_path: None,
1290                worktree_path: None,
1291                branch: None,
1292                base_ref: None,
1293                cleanup: None,
1294            },
1295        ));
1296
1297        assert_eq!(
1298            resolve_process_exec_cwd("subdir"),
1299            dir.path().join("subdir")
1300        );
1301
1302        crate::stdlib::process::set_thread_execution_context(None);
1303    }
1304
1305    #[test]
1306    fn manifest_includes_operation_metadata() {
1307        let manifest = capability_manifest_with_mocks();
1308        let process = manifest
1309            .as_dict()
1310            .and_then(|d| d.get("process"))
1311            .and_then(|v| v.as_dict())
1312            .expect("process capability");
1313        assert!(process.get("description").is_some());
1314        let operations = process
1315            .get("operations")
1316            .and_then(|v| v.as_dict())
1317            .expect("operations dict");
1318        assert!(operations.get("exec").is_some());
1319    }
1320
1321    #[test]
1322    fn mocked_capabilities_appear_in_manifest() {
1323        reset_host_state();
1324        push_host_mock(HostMock {
1325            capability: "project".to_string(),
1326            operation: "metadata_get".to_string(),
1327            params: None,
1328            result: Some(VmValue::dict(crate::value::DictMap::new())),
1329            error: None,
1330        });
1331        let manifest = capability_manifest_with_mocks();
1332        let project = manifest
1333            .as_dict()
1334            .and_then(|d| d.get("project"))
1335            .and_then(|v| v.as_dict())
1336            .expect("project capability");
1337        let operations = project
1338            .get("operations")
1339            .and_then(|v| v.as_dict())
1340            .expect("operations dict");
1341        assert!(operations.get("metadata_get").is_some());
1342        reset_host_state();
1343    }
1344
1345    #[test]
1346    fn mock_host_call_matches_partial_params_and_overrides_order() {
1347        reset_host_state();
1348        let mut exact_params = crate::value::DictMap::new();
1349        exact_params.put_str("namespace", "facts");
1350        push_host_mock(HostMock {
1351            capability: "project".to_string(),
1352            operation: "metadata_get".to_string(),
1353            params: None,
1354            result: Some(VmValue::String(arcstr::ArcStr::from("fallback"))),
1355            error: None,
1356        });
1357        push_host_mock(HostMock {
1358            capability: "project".to_string(),
1359            operation: "metadata_get".to_string(),
1360            params: Some(exact_params),
1361            result: Some(VmValue::String(arcstr::ArcStr::from("facts"))),
1362            error: None,
1363        });
1364
1365        let mut call_params = crate::value::DictMap::new();
1366        call_params.put_str("dir", "pkg");
1367        call_params.put_str("namespace", "facts");
1368        let exact = dispatch_mock_host_call("project", "metadata_get", &call_params)
1369            .expect("expected exact mock")
1370            .expect("exact mock should succeed");
1371        assert_eq!(exact.display(), "facts");
1372
1373        call_params.put_str("namespace", "classification");
1374        let fallback = dispatch_mock_host_call("project", "metadata_get", &call_params)
1375            .expect("expected fallback mock")
1376            .expect("fallback mock should succeed");
1377        assert_eq!(fallback.display(), "fallback");
1378        reset_host_state();
1379    }
1380
1381    #[test]
1382    fn mock_host_call_can_throw_errors() {
1383        reset_host_state();
1384        push_host_mock(HostMock {
1385            capability: "project".to_string(),
1386            operation: "metadata_get".to_string(),
1387            params: None,
1388            result: None,
1389            error: Some("boom".to_string()),
1390        });
1391        let params = crate::value::DictMap::new();
1392        let result = dispatch_mock_host_call("project", "metadata_get", &params)
1393            .expect("expected mock result");
1394        match result {
1395            Err(VmError::Thrown(VmValue::String(message))) => assert_eq!(message.as_str(), "boom"),
1396            other => panic!("unexpected result: {other:?}"),
1397        }
1398        reset_host_state();
1399    }
1400
1401    #[derive(Default)]
1402    struct TestHostToolBridge;
1403
1404    impl HostCallBridge for TestHostToolBridge {
1405        fn dispatch(
1406            &self,
1407            _capability: &str,
1408            _operation: &str,
1409            _params: &crate::value::DictMap,
1410        ) -> Result<Option<VmValue>, VmError> {
1411            Ok(None)
1412        }
1413
1414        fn list_tools(&self) -> Result<Option<VmValue>, VmError> {
1415            let tool = VmValue::dict(crate::value::DictMap::from_iter([
1416                (
1417                    crate::value::intern_key("name"),
1418                    VmValue::String(arcstr::ArcStr::from("Read".to_string())),
1419                ),
1420                (
1421                    crate::value::intern_key("description"),
1422                    VmValue::String(arcstr::ArcStr::from(
1423                        "Read a file from the host".to_string(),
1424                    )),
1425                ),
1426                (
1427                    crate::value::intern_key("schema"),
1428                    VmValue::dict(crate::value::DictMap::from_iter([(
1429                        crate::value::intern_key("type"),
1430                        VmValue::String(arcstr::ArcStr::from("object".to_string())),
1431                    )])),
1432                ),
1433                (crate::value::intern_key("deprecated"), VmValue::Bool(false)),
1434            ]));
1435            Ok(Some(VmValue::List(std::sync::Arc::new(vec![tool]))))
1436        }
1437
1438        fn call_tool(&self, name: &str, args: &VmValue) -> Result<Option<VmValue>, VmError> {
1439            if name != "Read" {
1440                return Ok(None);
1441            }
1442            let path = args
1443                .as_dict()
1444                .and_then(|dict| dict.get("path"))
1445                .map(|value| value.display())
1446                .unwrap_or_default();
1447            Ok(Some(VmValue::String(arcstr::ArcStr::from(format!(
1448                "read:{path}"
1449            )))))
1450        }
1451    }
1452
1453    struct CountingProcessExecBridge {
1454        calls: Arc<AtomicUsize>,
1455    }
1456
1457    impl HostCallBridge for CountingProcessExecBridge {
1458        fn dispatch(
1459            &self,
1460            capability: &str,
1461            operation: &str,
1462            _params: &crate::value::DictMap,
1463        ) -> Result<Option<VmValue>, VmError> {
1464            if (capability, operation) != ("process", "exec") {
1465                return Ok(None);
1466            }
1467            self.calls.fetch_add(1, Ordering::SeqCst);
1468            Ok(Some(VmValue::dict(crate::value::DictMap::from_iter([
1469                (
1470                    crate::value::intern_key("status"),
1471                    VmValue::String(arcstr::ArcStr::from("completed".to_string())),
1472                ),
1473                (crate::value::intern_key("exit_code"), VmValue::Int(0)),
1474                (crate::value::intern_key("success"), VmValue::Bool(true)),
1475            ]))))
1476        }
1477    }
1478
1479    fn run_host_async_test<F, Fut>(test: F)
1480    where
1481        F: FnOnce() -> Fut,
1482        Fut: std::future::Future<Output = ()>,
1483    {
1484        let rt = tokio::runtime::Builder::new_current_thread()
1485            .enable_all()
1486            .build()
1487            .expect("runtime");
1488        rt.block_on(async {
1489            let local = tokio::task::LocalSet::new();
1490            local.run_until(test()).await;
1491        });
1492    }
1493
1494    #[test]
1495    fn host_tool_list_uses_installed_host_call_bridge() {
1496        run_host_async_test(|| async {
1497            reset_host_state();
1498            set_host_call_bridge(Arc::new(TestHostToolBridge));
1499            let tools = dispatch_host_tool_list().await.expect("tool list");
1500            clear_host_call_bridge();
1501
1502            let VmValue::List(items) = tools else {
1503                panic!("expected tool list");
1504            };
1505            assert_eq!(items.len(), 1);
1506            let tool = items[0].as_dict().expect("tool dict");
1507            assert_eq!(tool.get("name").unwrap().display(), "Read");
1508            assert_eq!(tool.get("deprecated").unwrap().display(), "false");
1509        });
1510    }
1511
1512    #[test]
1513    fn host_tool_call_uses_installed_host_call_bridge() {
1514        run_host_async_test(|| async {
1515            set_host_call_bridge(Arc::new(TestHostToolBridge));
1516            let args = VmValue::dict(crate::value::DictMap::from_iter([(
1517                crate::value::intern_key("path"),
1518                VmValue::String(arcstr::ArcStr::from("README.md".to_string())),
1519            )]));
1520            let value = dispatch_host_tool_call("Read", &args)
1521                .await
1522                .expect("tool call");
1523            clear_host_call_bridge();
1524            assert_eq!(value.display(), "read:README.md");
1525        });
1526    }
1527
1528    #[test]
1529    fn process_exec_bridge_is_gated_by_command_policy() {
1530        run_host_async_test(|| async {
1531            crate::orchestration::clear_command_policies();
1532            let calls = Arc::new(AtomicUsize::new(0));
1533            set_host_call_bridge(Arc::new(CountingProcessExecBridge {
1534                calls: calls.clone(),
1535            }));
1536            crate::orchestration::push_command_policy(crate::orchestration::CommandPolicy {
1537                tools: vec!["run".to_string()],
1538                workspace_roots: Vec::new(),
1539                default_shell_mode: "shell".to_string(),
1540                deny_patterns: vec!["cat *".to_string()],
1541                require_approval: Default::default(),
1542                pre: None,
1543                post: None,
1544                allow_recursive: false,
1545            });
1546
1547            let result = dispatch_host_operation(
1548                "process",
1549                "exec",
1550                &crate::value::DictMap::from_iter([
1551                    (
1552                        crate::value::intern_key("mode"),
1553                        VmValue::String(arcstr::ArcStr::from("shell")),
1554                    ),
1555                    (
1556                        crate::value::intern_key("command"),
1557                        VmValue::String(arcstr::ArcStr::from("cat Cargo.toml")),
1558                    ),
1559                ]),
1560            )
1561            .await
1562            .expect("process.exec result");
1563
1564            crate::orchestration::clear_command_policies();
1565            clear_host_call_bridge();
1566
1567            assert_eq!(
1568                calls.load(Ordering::SeqCst),
1569                0,
1570                "blocked command must not reach host bridge"
1571            );
1572            let result = result.as_dict().expect("blocked result dict");
1573            assert_eq!(result.get("status").unwrap().display(), "blocked");
1574            assert!(
1575                result
1576                    .get("reason")
1577                    .map(VmValue::display)
1578                    .unwrap_or_default()
1579                    .contains("cat *"),
1580                "blocked result should name the matched policy pattern"
1581            );
1582        });
1583    }
1584
1585    #[cfg(unix)]
1586    async fn process_exec_env_probe(env: VmValue, env_mode: Option<&str>) -> (String, String) {
1587        // Run `sh -c 'printf "%s|%s" "$PARENT_VAR" "$CHILD_VAR"'` so we can
1588        // observe whether an inherited parent var survives alongside the
1589        // explicitly-provided child var. The parent var is set on this
1590        // process's environment immediately before the spawn.
1591        std::env::set_var("PARENT_VAR", "inherited");
1592        let mut params = crate::value::DictMap::from_iter([
1593            (
1594                crate::value::intern_key("mode"),
1595                VmValue::String(arcstr::ArcStr::from("argv")),
1596            ),
1597            (
1598                crate::value::intern_key("argv"),
1599                VmValue::List(std::sync::Arc::new(vec![
1600                    // Absolute path so the spawn does not depend on PATH,
1601                    // which the `replace` case intentionally clears.
1602                    VmValue::String(arcstr::ArcStr::from("/bin/sh")),
1603                    VmValue::String(arcstr::ArcStr::from("-c")),
1604                    VmValue::String(arcstr::ArcStr::from(
1605                        "printf '%s|%s' \"$PARENT_VAR\" \"$CHILD_VAR\"",
1606                    )),
1607                ])),
1608            ),
1609            (crate::value::intern_key("env"), env),
1610        ]);
1611        if let Some(mode) = env_mode {
1612            params.put_str("env_mode", mode);
1613        }
1614        let result = super::dispatch_process_exec(&params, serde_json::Value::Null)
1615            .await
1616            .expect("process.exec result");
1617        let dict = result.as_dict().expect("result dict");
1618        let stdout = dict.get("stdout").map(VmValue::display).unwrap_or_default();
1619        std::env::remove_var("PARENT_VAR");
1620        let (parent, child) = stdout.split_once('|').unwrap_or((&stdout, ""));
1621        (parent.to_string(), child.to_string())
1622    }
1623
1624    #[cfg(unix)]
1625    #[test]
1626    fn process_exec_env_default_merges_with_parent() {
1627        run_host_async_test(|| async {
1628            // No `env_mode`: the provided key must be added WITHOUT clearing
1629            // the inherited parent environment (the env-clear footgun fix).
1630            let child_env = VmValue::dict(crate::value::DictMap::from_iter([(
1631                crate::value::intern_key("CHILD_VAR"),
1632                VmValue::String(arcstr::ArcStr::from("provided")),
1633            )]));
1634            let (parent, child) = process_exec_env_probe(child_env, None).await;
1635            assert_eq!(
1636                parent, "inherited",
1637                "default env_mode must inherit parent env"
1638            );
1639            assert_eq!(
1640                child, "provided",
1641                "default env_mode must apply provided keys"
1642            );
1643        });
1644    }
1645
1646    #[cfg(unix)]
1647    #[test]
1648    fn process_exec_env_mode_replace_clears_parent() {
1649        run_host_async_test(|| async {
1650            // Explicit `replace`: the inherited parent var must be gone and
1651            // only the provided key survives. This preserves the ability to
1652            // fully replace the environment when intentionally requested.
1653            let child_env = VmValue::dict(crate::value::DictMap::from_iter([(
1654                crate::value::intern_key("CHILD_VAR"),
1655                VmValue::String(arcstr::ArcStr::from("provided")),
1656            )]));
1657            let (parent, child) = process_exec_env_probe(child_env, Some("replace")).await;
1658            assert_eq!(parent, "", "explicit replace must clear parent env");
1659            assert_eq!(
1660                child, "provided",
1661                "explicit replace must keep provided keys"
1662            );
1663        });
1664    }
1665
1666    #[cfg(unix)]
1667    #[test]
1668    fn process_exec_env_mode_unknown_is_rejected() {
1669        run_host_async_test(|| async {
1670            let params = crate::value::DictMap::from_iter([
1671                (
1672                    crate::value::intern_key("mode"),
1673                    VmValue::String(arcstr::ArcStr::from("argv")),
1674                ),
1675                (
1676                    crate::value::intern_key("argv"),
1677                    VmValue::List(std::sync::Arc::new(vec![VmValue::String(
1678                        arcstr::ArcStr::from("true"),
1679                    )])),
1680                ),
1681                (
1682                    crate::value::intern_key("env"),
1683                    VmValue::dict(crate::value::DictMap::from_iter([(
1684                        crate::value::intern_key("CHILD_VAR"),
1685                        VmValue::String(arcstr::ArcStr::from("x")),
1686                    )])),
1687                ),
1688                (
1689                    crate::value::intern_key("env_mode"),
1690                    VmValue::String(arcstr::ArcStr::from("bogus")),
1691                ),
1692            ]);
1693            let err = super::dispatch_process_exec(&params, serde_json::Value::Null)
1694                .await
1695                .expect_err("unknown env_mode must error");
1696            assert!(
1697                format!("{err:?}").contains("env_mode"),
1698                "error should name env_mode, got {err:?}"
1699            );
1700        });
1701    }
1702
1703    // Drive the real `host_call("process","exec")` builder under a restricted
1704    // policy and read back the `$TMPDIR` the child actually saw. This is the
1705    // agent-facing path; the assertion is OS-independent (it observes the
1706    // injected env, not OS-sandbox enforcement), so it pins the mechanism on
1707    // every CI host while the live OS-level link proof runs on tornadough.
1708    #[cfg(unix)]
1709    async fn process_exec_tmpdir_probe(
1710        workspace: &std::path::Path,
1711        caller_env: Option<VmValue>,
1712    ) -> String {
1713        let mut env_pairs = vec![(
1714            crate::value::intern_key("mode"),
1715            VmValue::String(arcstr::ArcStr::from("argv")),
1716        )];
1717        env_pairs.push((
1718            crate::value::intern_key("argv"),
1719            VmValue::List(std::sync::Arc::new(vec![
1720                VmValue::String(arcstr::ArcStr::from("/bin/sh")),
1721                VmValue::String(arcstr::ArcStr::from("-c")),
1722                VmValue::String(arcstr::ArcStr::from("printf '%s' \"$TMPDIR\"")),
1723            ])),
1724        ));
1725        if let Some(env) = caller_env {
1726            env_pairs.push((crate::value::intern_key("env"), env));
1727        }
1728        let params = crate::value::DictMap::from_iter(env_pairs);
1729
1730        crate::orchestration::push_execution_policy(crate::orchestration::CapabilityPolicy {
1731            sandbox_profile: crate::orchestration::SandboxProfile::Worktree,
1732            workspace_roots: vec![workspace.to_string_lossy().into_owned()],
1733            // Keep OS confinement out of this unit assertion regardless of host
1734            // Landlock/seatbelt availability; we are pinning the env injection,
1735            // not OS enforcement (which the tornadough run proves end-to-end).
1736            ..crate::orchestration::CapabilityPolicy::default()
1737        });
1738        std::env::set_var("HARN_HANDLER_SANDBOX", "off");
1739        let result = super::dispatch_process_exec(&params, serde_json::Value::Null)
1740            .await
1741            .expect("process.exec result");
1742        std::env::remove_var("HARN_HANDLER_SANDBOX");
1743        crate::orchestration::pop_execution_policy();
1744        result
1745            .as_dict()
1746            .and_then(|d| d.get("stdout"))
1747            .map(VmValue::display)
1748            .unwrap_or_default()
1749    }
1750
1751    #[cfg(unix)]
1752    #[test]
1753    fn process_exec_injects_workspace_local_tmpdir() {
1754        run_host_async_test(|| async {
1755            let workspace = tempfile::tempdir().expect("workspace");
1756            let tmpdir = process_exec_tmpdir_probe(workspace.path(), None).await;
1757
1758            assert!(
1759                !tmpdir.is_empty(),
1760                "sandboxed child must receive a non-empty TMPDIR"
1761            );
1762            let tmpdir_path = std::path::PathBuf::from(&tmpdir);
1763            assert!(
1764                tmpdir_path.starts_with(workspace.path()),
1765                "child TMPDIR {tmpdir:?} must live inside the workspace {:?}",
1766                workspace.path()
1767            );
1768            assert!(
1769                tmpdir_path.ends_with(".harn-tmp"),
1770                "child TMPDIR {tmpdir:?} must be the workspace-local .harn-tmp dir"
1771            );
1772            assert!(
1773                tmpdir_path.is_dir(),
1774                "the workspace-local TMPDIR must have been created on disk"
1775            );
1776        });
1777    }
1778
1779    #[cfg(unix)]
1780    #[test]
1781    fn process_exec_respects_caller_pinned_tmpdir() {
1782        run_host_async_test(|| async {
1783            let workspace = tempfile::tempdir().expect("workspace");
1784            let caller_tmp = workspace.path().join("caller-chosen");
1785            std::fs::create_dir_all(&caller_tmp).unwrap();
1786            let caller_env = VmValue::dict(crate::value::DictMap::from_iter([(
1787                crate::value::intern_key("TMPDIR"),
1788                VmValue::String(arcstr::ArcStr::from(
1789                    caller_tmp.to_string_lossy().into_owned(),
1790                )),
1791            )]));
1792
1793            let tmpdir = process_exec_tmpdir_probe(workspace.path(), Some(caller_env)).await;
1794
1795            assert_eq!(
1796                std::path::PathBuf::from(&tmpdir),
1797                caller_tmp,
1798                "an explicit caller TMPDIR must override the workspace-local default"
1799            );
1800        });
1801    }
1802
1803    #[test]
1804    fn host_tool_list_is_empty_without_bridge() {
1805        run_host_async_test(|| async {
1806            clear_host_call_bridge();
1807            let tools = dispatch_host_tool_list().await.expect("tool list");
1808            let VmValue::List(items) = tools else {
1809                panic!("expected tool list");
1810            };
1811            assert!(items.is_empty());
1812        });
1813    }
1814}