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    if let Some(env) = optional_string_dict(params, "env")? {
872        // `env_mode` controls how the provided `env` keys combine with the
873        // parent environment:
874        //   - "merge" (default): inherit the parent env and overlay the
875        //     provided keys. This is the least-surprising behavior — a
876        //     caller passing `env: {ONE_VAR: "x"}` keeps PATH/HOME/etc.
877        //   - "replace": clear the parent env entirely, then set only the
878        //     provided keys. Must be requested explicitly now; previously
879        //     this was the (footgun) default whenever `env` was supplied.
880        let env_mode = optional_string(params, "env_mode");
881        match env_mode.as_deref().unwrap_or("merge") {
882            "replace" => {
883                cmd.env_clear();
884            }
885            "merge" => {}
886            other => {
887                return Err(VmError::Runtime(format!(
888                    "host_call {label}: unknown env_mode {other:?}; expected \"merge\" or \"replace\""
889                )));
890            }
891        }
892        for (key, value) in env {
893            cmd.env(key, value);
894        }
895    }
896    // env_remove: list of environment variable names to strip before
897    // spawning. Applied after `env` so callers can both inherit and
898    // selectively unset (e.g. the git stdlib strips `GIT_*` so its
899    // operations are self-contained even when Harn is invoked from
900    // inside a git hook that sets `GIT_DIR`).
901    if let Some(env_remove) = optional_string_list(params, "env_remove") {
902        for key in env_remove {
903            cmd.env_remove(key);
904        }
905    }
906    Ok(cmd)
907}
908
909struct ProcessExecResponse<'a> {
910    pid: Option<u32>,
911    started_at: String,
912    started: Instant,
913    stdout: &'a str,
914    stderr: &'a str,
915    exit_code: i32,
916    status: &'a str,
917    success: bool,
918    timed_out: bool,
919}
920
921fn process_exec_response(response: ProcessExecResponse<'_>) -> VmValue {
922    let combined = format!("{}{}", response.stdout, response.stderr);
923    let mut result = crate::value::DictMap::new();
924    result.put_str(
925        "command_id",
926        format!(
927            "cmd_{}_{}",
928            std::process::id(),
929            response.started.elapsed().as_nanos()
930        ),
931    );
932    result.put_str("status", response.status);
933    result.insert(
934        crate::value::intern_key("pid"),
935        response
936            .pid
937            .map(|pid| VmValue::Int(pid as i64))
938            .unwrap_or(VmValue::Nil),
939    );
940    result.insert(
941        crate::value::intern_key("process_group_id"),
942        response
943            .pid
944            .map(|pid| VmValue::Int(pid as i64))
945            .unwrap_or(VmValue::Nil),
946    );
947    result.insert(crate::value::intern_key("handle_id"), VmValue::Nil);
948    result.put_str("started_at", response.started_at);
949    result.put_str(
950        "ended_at",
951        audited_utc_now_rfc3339("host_call/process.exec.ended_at"),
952    );
953    result.insert(
954        crate::value::intern_key("duration_ms"),
955        VmValue::Int(response.started.elapsed().as_millis() as i64),
956    );
957    result.insert(
958        crate::value::intern_key("exit_code"),
959        VmValue::Int(response.exit_code as i64),
960    );
961    result.insert(crate::value::intern_key("signal"), VmValue::Nil);
962    result.insert(
963        crate::value::intern_key("timed_out"),
964        VmValue::Bool(response.timed_out),
965    );
966    result.put_str("stdout", response.stdout);
967    result.put_str("stderr", response.stderr);
968    result.put_str("combined", combined);
969    result.insert(
970        crate::value::intern_key("exit_status"),
971        VmValue::Int(response.exit_code as i64),
972    );
973    result.insert(
974        crate::value::intern_key("legacy_status"),
975        VmValue::Int(response.exit_code as i64),
976    );
977    result.insert(
978        crate::value::intern_key("success"),
979        VmValue::Bool(response.success),
980    );
981    VmValue::dict(result)
982}
983
984fn resolve_process_exec_cwd(cwd: &str) -> std::path::PathBuf {
985    crate::stdlib::process::resolve_source_relative_path(cwd)
986}
987
988fn process_exec_argv(params: &crate::value::DictMap) -> Result<(String, Vec<String>), VmError> {
989    match optional_string(params, "mode")
990        .as_deref()
991        .unwrap_or("shell")
992    {
993        "argv" => {
994            let argv = optional_string_list(params, "argv").ok_or_else(|| {
995                VmError::Runtime("host_call process.exec missing argv".to_string())
996            })?;
997            split_argv(argv)
998        }
999        "shell" => {
1000            let command = require_param(params, "command")?;
1001            let mut invocation_params = params.clone();
1002            invocation_params.put_str("command", command);
1003            let invocation =
1004                crate::shells::resolve_invocation_from_vm_params(&invocation_params)
1005                    .map_err(|err| VmError::Runtime(format!("host_call process.exec: {err}")))?;
1006            Ok((invocation.program, invocation.args))
1007        }
1008        other => Err(VmError::Runtime(format!(
1009            "host_call process.exec unsupported mode {other:?}"
1010        ))),
1011    }
1012}
1013
1014fn split_argv(mut argv: Vec<String>) -> Result<(String, Vec<String>), VmError> {
1015    if argv.is_empty() {
1016        return Err(VmError::Runtime(
1017            "host_call process.exec argv must not be empty".to_string(),
1018        ));
1019    }
1020    let program = argv.remove(0);
1021    if program.is_empty() {
1022        return Err(VmError::Runtime(
1023            "host_call process.exec argv[0] must not be empty".to_string(),
1024        ));
1025    }
1026    Ok((program, argv))
1027}
1028
1029/// Push a transient policy onto the execution stack with the
1030/// requested sandbox profile, returning a guard that pops on drop.
1031/// Used by `host_call("process", "exec", ...)` to honor a per-call
1032/// `sandbox_profile` override without rewriting the surrounding
1033/// orchestration policy.
1034pub(crate) fn push_sandbox_profile_override(value: &str) -> Result<SandboxProfileGuard, VmError> {
1035    let profile = crate::orchestration::SandboxProfile::parse(value).ok_or_else(|| {
1036        VmError::Thrown(VmValue::String(arcstr::ArcStr::from(format!(
1037            "host_call process.exec: unknown sandbox_profile {value:?}; expected one of \"unrestricted\", \"worktree\", \"os_hardened\", \"wasi\""
1038        ))))
1039    })?;
1040    let mut policy = crate::orchestration::current_execution_policy().unwrap_or_default();
1041    policy.sandbox_profile = profile;
1042    crate::orchestration::push_execution_policy(policy);
1043    Ok(SandboxProfileGuard {
1044        _private: std::marker::PhantomData,
1045    })
1046}
1047
1048pub(crate) struct SandboxProfileGuard {
1049    _private: std::marker::PhantomData<*const ()>,
1050}
1051
1052impl Drop for SandboxProfileGuard {
1053    fn drop(&mut self) {
1054        crate::orchestration::pop_execution_policy();
1055    }
1056}
1057
1058pub(crate) fn optional_i64(params: &crate::value::DictMap, key: &str) -> Option<i64> {
1059    match params.get(key) {
1060        Some(VmValue::Int(value)) => Some(*value),
1061        Some(VmValue::Float(value)) if value.fract() == 0.0 => Some(*value as i64),
1062        _ => None,
1063    }
1064}
1065
1066pub(crate) fn optional_string(params: &crate::value::DictMap, key: &str) -> Option<String> {
1067    params.get(key).and_then(vm_string).map(ToString::to_string)
1068}
1069
1070fn optional_string_list(params: &crate::value::DictMap, key: &str) -> Option<Vec<String>> {
1071    let VmValue::List(values) = params.get(key)? else {
1072        return None;
1073    };
1074    values
1075        .iter()
1076        .map(|value| vm_string(value).map(ToString::to_string))
1077        .collect()
1078}
1079
1080fn optional_string_dict(
1081    params: &crate::value::DictMap,
1082    key: &str,
1083) -> Result<Option<BTreeMap<String, String>>, VmError> {
1084    let Some(value) = params.get(key) else {
1085        return Ok(None);
1086    };
1087    let Some(dict) = value.as_dict() else {
1088        return Err(VmError::Runtime(format!(
1089            "host_call process.exec {key} must be a dict"
1090        )));
1091    };
1092    let mut out = std::collections::BTreeMap::new();
1093    for (key, value) in dict.iter() {
1094        let Some(value) = vm_string(value) else {
1095            return Err(VmError::Runtime(format!(
1096                "host_call process.exec env value for {key:?} must be a string"
1097            )));
1098        };
1099        out.insert(key.to_string(), value.to_string());
1100    }
1101    Ok(Some(out))
1102}
1103
1104fn vm_string(value: &VmValue) -> Option<&str> {
1105    match value {
1106        VmValue::String(value) => Some(value.as_ref()),
1107        _ => None,
1108    }
1109}
1110
1111pub(crate) fn register_host_builtins(vm: &mut Vm) {
1112    for def in MODULE_BUILTINS {
1113        vm.register_builtin_def(def);
1114    }
1115}
1116
1117#[harn_builtin(
1118    sig = "host_mock(capability: string, op: string, response_or_config?: any, params?: dict) -> nil",
1119    category = "host"
1120)]
1121fn host_mock_builtin(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1122    let host_mock = parse_host_mock(args)?;
1123    push_host_mock(host_mock);
1124    Ok(VmValue::Nil)
1125}
1126
1127#[harn_builtin(sig = "host_mock_clear() -> nil", category = "host")]
1128fn host_mock_clear_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1129    reset_host_state();
1130    Ok(VmValue::Nil)
1131}
1132
1133#[harn_builtin(sig = "host_mock_calls() -> list", category = "host")]
1134fn host_mock_calls_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1135    let calls = HOST_MOCK_CALLS.with(|calls| {
1136        calls
1137            .borrow()
1138            .iter()
1139            .map(mock_call_value)
1140            .collect::<Vec<_>>()
1141    });
1142    Ok(VmValue::List(std::sync::Arc::new(calls)))
1143}
1144
1145#[harn_builtin(sig = "host_mock_push_scope() -> nil", category = "host")]
1146fn host_mock_push_scope_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1147    push_host_mock_scope();
1148    Ok(VmValue::Nil)
1149}
1150
1151#[harn_builtin(sig = "host_mock_pop_scope() -> nil", category = "host")]
1152fn host_mock_pop_scope_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1153    if !pop_host_mock_scope() {
1154        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
1155            "host_mock_pop_scope: no scope to pop",
1156        ))));
1157    }
1158    Ok(VmValue::Nil)
1159}
1160
1161#[harn_builtin(sig = "host_capabilities() -> dict", category = "host")]
1162fn host_capabilities_builtin(_args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1163    Ok(capability_manifest_with_mocks())
1164}
1165
1166#[harn_builtin(
1167    sig = "host_has(capability: string, op?: string) -> bool",
1168    category = "host"
1169)]
1170fn host_has_builtin(args: &[VmValue], _out: &mut String) -> Result<VmValue, VmError> {
1171    let capability = args.first().map(|a| a.display()).unwrap_or_default();
1172    let operation = args.get(1).map(|a| a.display());
1173    let manifest = capability_manifest_with_mocks();
1174    let has = manifest
1175        .as_dict()
1176        .and_then(|d| d.get(capability.as_str()))
1177        .and_then(|v| v.as_dict())
1178        .is_some_and(|cap| {
1179            if let Some(operation) = operation {
1180                cap.get("ops")
1181                    .and_then(|v| match v {
1182                        VmValue::List(list) => {
1183                            Some(list.iter().any(|item| item.display() == operation))
1184                        }
1185                        _ => None,
1186                    })
1187                    .unwrap_or(false)
1188            } else {
1189                true
1190            }
1191        });
1192    Ok(VmValue::Bool(has))
1193}
1194
1195#[harn_builtin(
1196    sig = "host_call(name: string, args?: dict) -> any",
1197    kind = "async",
1198    category = "host"
1199)]
1200async fn host_call_builtin(
1201    ctx: crate::vm::AsyncBuiltinCtx,
1202    args: Vec<VmValue>,
1203) -> Result<VmValue, VmError> {
1204    let name = args.first().map(|a| a.display()).unwrap_or_default();
1205    let params = args
1206        .get(1)
1207        .and_then(|a| a.as_dict())
1208        .cloned()
1209        .unwrap_or_default();
1210    let Some((capability, operation)) = name.split_once('.') else {
1211        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
1212            format!("host_call: unsupported operation name '{name}'"),
1213        ))));
1214    };
1215    dispatch_host_operation_with_ctx(Some(&ctx), capability, operation, &params).await
1216}
1217
1218#[harn_builtin(sig = "host_tool_list() -> list", kind = "async", category = "host")]
1219async fn host_tool_list_builtin(
1220    ctx: crate::vm::AsyncBuiltinCtx,
1221    _args: Vec<VmValue>,
1222) -> Result<VmValue, VmError> {
1223    dispatch_host_tool_list_with_ctx(Some(&ctx)).await
1224}
1225
1226#[harn_builtin(
1227    sig = "host_tool_call(name: string, args?: any) -> any",
1228    kind = "async",
1229    category = "host"
1230)]
1231async fn host_tool_call_builtin(
1232    ctx: crate::vm::AsyncBuiltinCtx,
1233    args: Vec<VmValue>,
1234) -> Result<VmValue, VmError> {
1235    let name = args.first().map(|a| a.display()).unwrap_or_default();
1236    if name.is_empty() {
1237        return Err(VmError::Thrown(VmValue::String(arcstr::ArcStr::from(
1238            "host_tool_call: tool name is required",
1239        ))));
1240    }
1241    let call_args = args.get(1).cloned().unwrap_or(VmValue::Nil);
1242    dispatch_host_tool_call_with_ctx(Some(&ctx), &name, &call_args).await
1243}
1244
1245#[cfg(test)]
1246mod tests {
1247    use super::{
1248        capability_manifest_with_mocks, clear_host_call_bridge, dispatch_host_operation,
1249        dispatch_host_tool_call, dispatch_host_tool_list, dispatch_mock_host_call, push_host_mock,
1250        reset_host_state, resolve_process_exec_cwd, set_host_call_bridge, HostCallBridge, HostMock,
1251    };
1252    use crate::value::VmDictExt;
1253
1254    use std::sync::{
1255        atomic::{AtomicUsize, Ordering},
1256        Arc,
1257    };
1258
1259    use crate::value::{VmError, VmValue};
1260
1261    #[test]
1262    fn process_exec_relative_cwd_resolves_against_execution_root() {
1263        let dir = tempfile::tempdir().expect("tempdir");
1264        crate::stdlib::process::set_thread_execution_context(Some(
1265            crate::orchestration::RunExecutionRecord {
1266                cwd: Some(dir.path().to_string_lossy().into_owned()),
1267                source_dir: Some(dir.path().join("src").to_string_lossy().into_owned()),
1268                env: std::collections::BTreeMap::new(),
1269                adapter: None,
1270                repo_path: None,
1271                worktree_path: None,
1272                branch: None,
1273                base_ref: None,
1274                cleanup: None,
1275            },
1276        ));
1277
1278        assert_eq!(
1279            resolve_process_exec_cwd("subdir"),
1280            dir.path().join("subdir")
1281        );
1282
1283        crate::stdlib::process::set_thread_execution_context(None);
1284    }
1285
1286    #[test]
1287    fn manifest_includes_operation_metadata() {
1288        let manifest = capability_manifest_with_mocks();
1289        let process = manifest
1290            .as_dict()
1291            .and_then(|d| d.get("process"))
1292            .and_then(|v| v.as_dict())
1293            .expect("process capability");
1294        assert!(process.get("description").is_some());
1295        let operations = process
1296            .get("operations")
1297            .and_then(|v| v.as_dict())
1298            .expect("operations dict");
1299        assert!(operations.get("exec").is_some());
1300    }
1301
1302    #[test]
1303    fn mocked_capabilities_appear_in_manifest() {
1304        reset_host_state();
1305        push_host_mock(HostMock {
1306            capability: "project".to_string(),
1307            operation: "metadata_get".to_string(),
1308            params: None,
1309            result: Some(VmValue::dict(crate::value::DictMap::new())),
1310            error: None,
1311        });
1312        let manifest = capability_manifest_with_mocks();
1313        let project = manifest
1314            .as_dict()
1315            .and_then(|d| d.get("project"))
1316            .and_then(|v| v.as_dict())
1317            .expect("project capability");
1318        let operations = project
1319            .get("operations")
1320            .and_then(|v| v.as_dict())
1321            .expect("operations dict");
1322        assert!(operations.get("metadata_get").is_some());
1323        reset_host_state();
1324    }
1325
1326    #[test]
1327    fn mock_host_call_matches_partial_params_and_overrides_order() {
1328        reset_host_state();
1329        let mut exact_params = crate::value::DictMap::new();
1330        exact_params.put_str("namespace", "facts");
1331        push_host_mock(HostMock {
1332            capability: "project".to_string(),
1333            operation: "metadata_get".to_string(),
1334            params: None,
1335            result: Some(VmValue::String(arcstr::ArcStr::from("fallback"))),
1336            error: None,
1337        });
1338        push_host_mock(HostMock {
1339            capability: "project".to_string(),
1340            operation: "metadata_get".to_string(),
1341            params: Some(exact_params),
1342            result: Some(VmValue::String(arcstr::ArcStr::from("facts"))),
1343            error: None,
1344        });
1345
1346        let mut call_params = crate::value::DictMap::new();
1347        call_params.put_str("dir", "pkg");
1348        call_params.put_str("namespace", "facts");
1349        let exact = dispatch_mock_host_call("project", "metadata_get", &call_params)
1350            .expect("expected exact mock")
1351            .expect("exact mock should succeed");
1352        assert_eq!(exact.display(), "facts");
1353
1354        call_params.put_str("namespace", "classification");
1355        let fallback = dispatch_mock_host_call("project", "metadata_get", &call_params)
1356            .expect("expected fallback mock")
1357            .expect("fallback mock should succeed");
1358        assert_eq!(fallback.display(), "fallback");
1359        reset_host_state();
1360    }
1361
1362    #[test]
1363    fn mock_host_call_can_throw_errors() {
1364        reset_host_state();
1365        push_host_mock(HostMock {
1366            capability: "project".to_string(),
1367            operation: "metadata_get".to_string(),
1368            params: None,
1369            result: None,
1370            error: Some("boom".to_string()),
1371        });
1372        let params = crate::value::DictMap::new();
1373        let result = dispatch_mock_host_call("project", "metadata_get", &params)
1374            .expect("expected mock result");
1375        match result {
1376            Err(VmError::Thrown(VmValue::String(message))) => assert_eq!(message.as_str(), "boom"),
1377            other => panic!("unexpected result: {other:?}"),
1378        }
1379        reset_host_state();
1380    }
1381
1382    #[derive(Default)]
1383    struct TestHostToolBridge;
1384
1385    impl HostCallBridge for TestHostToolBridge {
1386        fn dispatch(
1387            &self,
1388            _capability: &str,
1389            _operation: &str,
1390            _params: &crate::value::DictMap,
1391        ) -> Result<Option<VmValue>, VmError> {
1392            Ok(None)
1393        }
1394
1395        fn list_tools(&self) -> Result<Option<VmValue>, VmError> {
1396            let tool = VmValue::dict(crate::value::DictMap::from_iter([
1397                (
1398                    crate::value::intern_key("name"),
1399                    VmValue::String(arcstr::ArcStr::from("Read".to_string())),
1400                ),
1401                (
1402                    crate::value::intern_key("description"),
1403                    VmValue::String(arcstr::ArcStr::from(
1404                        "Read a file from the host".to_string(),
1405                    )),
1406                ),
1407                (
1408                    crate::value::intern_key("schema"),
1409                    VmValue::dict(crate::value::DictMap::from_iter([(
1410                        crate::value::intern_key("type"),
1411                        VmValue::String(arcstr::ArcStr::from("object".to_string())),
1412                    )])),
1413                ),
1414                (crate::value::intern_key("deprecated"), VmValue::Bool(false)),
1415            ]));
1416            Ok(Some(VmValue::List(std::sync::Arc::new(vec![tool]))))
1417        }
1418
1419        fn call_tool(&self, name: &str, args: &VmValue) -> Result<Option<VmValue>, VmError> {
1420            if name != "Read" {
1421                return Ok(None);
1422            }
1423            let path = args
1424                .as_dict()
1425                .and_then(|dict| dict.get("path"))
1426                .map(|value| value.display())
1427                .unwrap_or_default();
1428            Ok(Some(VmValue::String(arcstr::ArcStr::from(format!(
1429                "read:{path}"
1430            )))))
1431        }
1432    }
1433
1434    struct CountingProcessExecBridge {
1435        calls: Arc<AtomicUsize>,
1436    }
1437
1438    impl HostCallBridge for CountingProcessExecBridge {
1439        fn dispatch(
1440            &self,
1441            capability: &str,
1442            operation: &str,
1443            _params: &crate::value::DictMap,
1444        ) -> Result<Option<VmValue>, VmError> {
1445            if (capability, operation) != ("process", "exec") {
1446                return Ok(None);
1447            }
1448            self.calls.fetch_add(1, Ordering::SeqCst);
1449            Ok(Some(VmValue::dict(crate::value::DictMap::from_iter([
1450                (
1451                    crate::value::intern_key("status"),
1452                    VmValue::String(arcstr::ArcStr::from("completed".to_string())),
1453                ),
1454                (crate::value::intern_key("exit_code"), VmValue::Int(0)),
1455                (crate::value::intern_key("success"), VmValue::Bool(true)),
1456            ]))))
1457        }
1458    }
1459
1460    fn run_host_async_test<F, Fut>(test: F)
1461    where
1462        F: FnOnce() -> Fut,
1463        Fut: std::future::Future<Output = ()>,
1464    {
1465        let rt = tokio::runtime::Builder::new_current_thread()
1466            .enable_all()
1467            .build()
1468            .expect("runtime");
1469        rt.block_on(async {
1470            let local = tokio::task::LocalSet::new();
1471            local.run_until(test()).await;
1472        });
1473    }
1474
1475    #[test]
1476    fn host_tool_list_uses_installed_host_call_bridge() {
1477        run_host_async_test(|| async {
1478            reset_host_state();
1479            set_host_call_bridge(Arc::new(TestHostToolBridge));
1480            let tools = dispatch_host_tool_list().await.expect("tool list");
1481            clear_host_call_bridge();
1482
1483            let VmValue::List(items) = tools else {
1484                panic!("expected tool list");
1485            };
1486            assert_eq!(items.len(), 1);
1487            let tool = items[0].as_dict().expect("tool dict");
1488            assert_eq!(tool.get("name").unwrap().display(), "Read");
1489            assert_eq!(tool.get("deprecated").unwrap().display(), "false");
1490        });
1491    }
1492
1493    #[test]
1494    fn host_tool_call_uses_installed_host_call_bridge() {
1495        run_host_async_test(|| async {
1496            set_host_call_bridge(Arc::new(TestHostToolBridge));
1497            let args = VmValue::dict(crate::value::DictMap::from_iter([(
1498                crate::value::intern_key("path"),
1499                VmValue::String(arcstr::ArcStr::from("README.md".to_string())),
1500            )]));
1501            let value = dispatch_host_tool_call("Read", &args)
1502                .await
1503                .expect("tool call");
1504            clear_host_call_bridge();
1505            assert_eq!(value.display(), "read:README.md");
1506        });
1507    }
1508
1509    #[test]
1510    fn process_exec_bridge_is_gated_by_command_policy() {
1511        run_host_async_test(|| async {
1512            crate::orchestration::clear_command_policies();
1513            let calls = Arc::new(AtomicUsize::new(0));
1514            set_host_call_bridge(Arc::new(CountingProcessExecBridge {
1515                calls: calls.clone(),
1516            }));
1517            crate::orchestration::push_command_policy(crate::orchestration::CommandPolicy {
1518                tools: vec!["run".to_string()],
1519                workspace_roots: Vec::new(),
1520                default_shell_mode: "shell".to_string(),
1521                deny_patterns: vec!["cat *".to_string()],
1522                require_approval: Default::default(),
1523                pre: None,
1524                post: None,
1525                allow_recursive: false,
1526            });
1527
1528            let result = dispatch_host_operation(
1529                "process",
1530                "exec",
1531                &crate::value::DictMap::from_iter([
1532                    (
1533                        crate::value::intern_key("mode"),
1534                        VmValue::String(arcstr::ArcStr::from("shell")),
1535                    ),
1536                    (
1537                        crate::value::intern_key("command"),
1538                        VmValue::String(arcstr::ArcStr::from("cat Cargo.toml")),
1539                    ),
1540                ]),
1541            )
1542            .await
1543            .expect("process.exec result");
1544
1545            crate::orchestration::clear_command_policies();
1546            clear_host_call_bridge();
1547
1548            assert_eq!(
1549                calls.load(Ordering::SeqCst),
1550                0,
1551                "blocked command must not reach host bridge"
1552            );
1553            let result = result.as_dict().expect("blocked result dict");
1554            assert_eq!(result.get("status").unwrap().display(), "blocked");
1555            assert!(
1556                result
1557                    .get("reason")
1558                    .map(VmValue::display)
1559                    .unwrap_or_default()
1560                    .contains("cat *"),
1561                "blocked result should name the matched policy pattern"
1562            );
1563        });
1564    }
1565
1566    #[cfg(unix)]
1567    async fn process_exec_env_probe(env: VmValue, env_mode: Option<&str>) -> (String, String) {
1568        // Run `sh -c 'printf "%s|%s" "$PARENT_VAR" "$CHILD_VAR"'` so we can
1569        // observe whether an inherited parent var survives alongside the
1570        // explicitly-provided child var. The parent var is set on this
1571        // process's environment immediately before the spawn.
1572        std::env::set_var("PARENT_VAR", "inherited");
1573        let mut params = crate::value::DictMap::from_iter([
1574            (
1575                crate::value::intern_key("mode"),
1576                VmValue::String(arcstr::ArcStr::from("argv")),
1577            ),
1578            (
1579                crate::value::intern_key("argv"),
1580                VmValue::List(std::sync::Arc::new(vec![
1581                    // Absolute path so the spawn does not depend on PATH,
1582                    // which the `replace` case intentionally clears.
1583                    VmValue::String(arcstr::ArcStr::from("/bin/sh")),
1584                    VmValue::String(arcstr::ArcStr::from("-c")),
1585                    VmValue::String(arcstr::ArcStr::from(
1586                        "printf '%s|%s' \"$PARENT_VAR\" \"$CHILD_VAR\"",
1587                    )),
1588                ])),
1589            ),
1590            (crate::value::intern_key("env"), env),
1591        ]);
1592        if let Some(mode) = env_mode {
1593            params.put_str("env_mode", mode);
1594        }
1595        let result = super::dispatch_process_exec(&params, serde_json::Value::Null)
1596            .await
1597            .expect("process.exec result");
1598        let dict = result.as_dict().expect("result dict");
1599        let stdout = dict.get("stdout").map(VmValue::display).unwrap_or_default();
1600        std::env::remove_var("PARENT_VAR");
1601        let (parent, child) = stdout.split_once('|').unwrap_or((&stdout, ""));
1602        (parent.to_string(), child.to_string())
1603    }
1604
1605    #[cfg(unix)]
1606    #[test]
1607    fn process_exec_env_default_merges_with_parent() {
1608        run_host_async_test(|| async {
1609            // No `env_mode`: the provided key must be added WITHOUT clearing
1610            // the inherited parent environment (the env-clear footgun fix).
1611            let child_env = VmValue::dict(crate::value::DictMap::from_iter([(
1612                crate::value::intern_key("CHILD_VAR"),
1613                VmValue::String(arcstr::ArcStr::from("provided")),
1614            )]));
1615            let (parent, child) = process_exec_env_probe(child_env, None).await;
1616            assert_eq!(
1617                parent, "inherited",
1618                "default env_mode must inherit parent env"
1619            );
1620            assert_eq!(
1621                child, "provided",
1622                "default env_mode must apply provided keys"
1623            );
1624        });
1625    }
1626
1627    #[cfg(unix)]
1628    #[test]
1629    fn process_exec_env_mode_replace_clears_parent() {
1630        run_host_async_test(|| async {
1631            // Explicit `replace`: the inherited parent var must be gone and
1632            // only the provided key survives. This preserves the ability to
1633            // fully replace the environment when intentionally requested.
1634            let child_env = VmValue::dict(crate::value::DictMap::from_iter([(
1635                crate::value::intern_key("CHILD_VAR"),
1636                VmValue::String(arcstr::ArcStr::from("provided")),
1637            )]));
1638            let (parent, child) = process_exec_env_probe(child_env, Some("replace")).await;
1639            assert_eq!(parent, "", "explicit replace must clear parent env");
1640            assert_eq!(
1641                child, "provided",
1642                "explicit replace must keep provided keys"
1643            );
1644        });
1645    }
1646
1647    #[cfg(unix)]
1648    #[test]
1649    fn process_exec_env_mode_unknown_is_rejected() {
1650        run_host_async_test(|| async {
1651            let params = crate::value::DictMap::from_iter([
1652                (
1653                    crate::value::intern_key("mode"),
1654                    VmValue::String(arcstr::ArcStr::from("argv")),
1655                ),
1656                (
1657                    crate::value::intern_key("argv"),
1658                    VmValue::List(std::sync::Arc::new(vec![VmValue::String(
1659                        arcstr::ArcStr::from("true"),
1660                    )])),
1661                ),
1662                (
1663                    crate::value::intern_key("env"),
1664                    VmValue::dict(crate::value::DictMap::from_iter([(
1665                        crate::value::intern_key("CHILD_VAR"),
1666                        VmValue::String(arcstr::ArcStr::from("x")),
1667                    )])),
1668                ),
1669                (
1670                    crate::value::intern_key("env_mode"),
1671                    VmValue::String(arcstr::ArcStr::from("bogus")),
1672                ),
1673            ]);
1674            let err = super::dispatch_process_exec(&params, serde_json::Value::Null)
1675                .await
1676                .expect_err("unknown env_mode must error");
1677            assert!(
1678                format!("{err:?}").contains("env_mode"),
1679                "error should name env_mode, got {err:?}"
1680            );
1681        });
1682    }
1683
1684    #[test]
1685    fn host_tool_list_is_empty_without_bridge() {
1686        run_host_async_test(|| async {
1687            clear_host_call_bridge();
1688            let tools = dispatch_host_tool_list().await.expect("tool list");
1689            let VmValue::List(items) = tools else {
1690                panic!("expected tool list");
1691            };
1692            assert!(items.is_empty());
1693        });
1694    }
1695}